2008-09-25 23:00:45 +02:00
|
|
|
//===-- Attributes.cpp - Implement AttributesList -------------------------===//
|
2008-01-03 00:42:30 +01:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2008-09-25 23:00:45 +02:00
|
|
|
// This file implements the AttributesList class and Attribute utilities.
|
2008-01-03 00:42:30 +01:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-09-24 01:03:40 +02:00
|
|
|
#include "llvm/Attributes.h"
|
2008-03-12 18:45:29 +01:00
|
|
|
#include "llvm/Type.h"
|
2008-03-11 00:55:07 +01:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2008-03-12 18:45:29 +01:00
|
|
|
#include "llvm/ADT/FoldingSet.h"
|
|
|
|
#include "llvm/Support/Streams.h"
|
2008-01-03 00:42:30 +01:00
|
|
|
#include "llvm/Support/ManagedStatic.h"
|
|
|
|
using namespace llvm;
|
|
|
|
|
2008-03-12 18:45:29 +01:00
|
|
|
//===----------------------------------------------------------------------===//
|
2008-09-25 23:00:45 +02:00
|
|
|
// Attribute Function Definitions
|
2008-03-12 18:45:29 +01:00
|
|
|
//===----------------------------------------------------------------------===//
|
2008-01-14 20:52:09 +01:00
|
|
|
|
2008-09-25 23:00:45 +02:00
|
|
|
std::string Attribute::getAsString(Attributes Attrs) {
|
2008-01-03 00:42:30 +01:00
|
|
|
std::string Result;
|
2008-09-25 23:00:45 +02:00
|
|
|
if (Attrs & Attribute::ZExt)
|
2008-01-03 00:42:30 +01:00
|
|
|
Result += "zeroext ";
|
2008-09-25 23:00:45 +02:00
|
|
|
if (Attrs & Attribute::SExt)
|
2008-01-03 00:42:30 +01:00
|
|
|
Result += "signext ";
|
2008-09-25 23:00:45 +02:00
|
|
|
if (Attrs & Attribute::NoReturn)
|
2008-01-03 00:42:30 +01:00
|
|
|
Result += "noreturn ";
|
2008-09-25 23:00:45 +02:00
|
|
|
if (Attrs & Attribute::NoUnwind)
|
2008-01-03 00:42:30 +01:00
|
|
|
Result += "nounwind ";
|
2008-09-25 23:00:45 +02:00
|
|
|
if (Attrs & Attribute::InReg)
|
2008-01-03 00:42:30 +01:00
|
|
|
Result += "inreg ";
|
2008-09-25 23:00:45 +02:00
|
|
|
if (Attrs & Attribute::NoAlias)
|
2008-01-03 00:42:30 +01:00
|
|
|
Result += "noalias ";
|
2008-12-19 10:38:31 +01:00
|
|
|
if (Attrs & Attribute::NoCapture)
|
|
|
|
Result += "nocapture ";
|
2008-09-25 23:00:45 +02:00
|
|
|
if (Attrs & Attribute::StructRet)
|
2008-01-03 00:42:30 +01:00
|
|
|
Result += "sret ";
|
2008-09-25 23:00:45 +02:00
|
|
|
if (Attrs & Attribute::ByVal)
|
2008-01-03 00:42:30 +01:00
|
|
|
Result += "byval ";
|
2008-09-25 23:00:45 +02:00
|
|
|
if (Attrs & Attribute::Nest)
|
2008-01-03 00:42:30 +01:00
|
|
|
Result += "nest ";
|
2008-09-25 23:00:45 +02:00
|
|
|
if (Attrs & Attribute::ReadNone)
|
2008-01-03 00:42:30 +01:00
|
|
|
Result += "readnone ";
|
2008-09-25 23:00:45 +02:00
|
|
|
if (Attrs & Attribute::ReadOnly)
|
2008-01-03 00:42:30 +01:00
|
|
|
Result += "readonly ";
|
2008-09-27 00:53:05 +02:00
|
|
|
if (Attrs & Attribute::OptimizeForSize)
|
|
|
|
Result += "optsize ";
|
|
|
|
if (Attrs & Attribute::NoInline)
|
|
|
|
Result += "noinline ";
|
|
|
|
if (Attrs & Attribute::AlwaysInline)
|
|
|
|
Result += "alwaysinline ";
|
2008-11-13 02:02:14 +01:00
|
|
|
if (Attrs & Attribute::StackProtect)
|
|
|
|
Result += "ssp ";
|
|
|
|
if (Attrs & Attribute::StackProtectReq)
|
|
|
|
Result += "sspreq ";
|
2008-09-25 23:00:45 +02:00
|
|
|
if (Attrs & Attribute::Alignment) {
|
2008-02-20 00:51:49 +01:00
|
|
|
Result += "align ";
|
2009-01-11 18:02:06 +01:00
|
|
|
Result += utostr(Attribute::getAlignmentFromAttrs(Attrs));
|
2008-02-20 00:51:49 +01:00
|
|
|
Result += " ";
|
|
|
|
}
|
2008-08-05 17:51:44 +02:00
|
|
|
// Trim the trailing space.
|
2008-12-19 10:38:31 +01:00
|
|
|
assert(!Result.empty() && "Unknown attribute!");
|
2008-08-05 17:51:44 +02:00
|
|
|
Result.erase(Result.end()-1);
|
2008-01-03 00:42:30 +01:00
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2008-09-25 23:00:45 +02:00
|
|
|
Attributes Attribute::typeIncompatible(const Type *Ty) {
|
2008-09-24 01:03:40 +02:00
|
|
|
Attributes Incompatible = None;
|
2008-03-12 18:45:29 +01:00
|
|
|
|
|
|
|
if (!Ty->isInteger())
|
|
|
|
// Attributes that only apply to integers.
|
|
|
|
Incompatible |= SExt | ZExt;
|
|
|
|
|
|
|
|
if (!isa<PointerType>(Ty))
|
|
|
|
// Attributes that only apply to pointers.
|
2008-12-19 10:38:31 +01:00
|
|
|
Incompatible |= ByVal | Nest | NoAlias | StructRet | NoCapture;
|
2008-03-12 18:45:29 +01:00
|
|
|
|
|
|
|
return Incompatible;
|
2008-01-03 00:42:30 +01:00
|
|
|
}
|
|
|
|
|
2008-03-12 18:45:29 +01:00
|
|
|
//===----------------------------------------------------------------------===//
|
2008-09-24 02:29:49 +02:00
|
|
|
// AttributeListImpl Definition
|
2008-03-12 18:45:29 +01:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace llvm {
|
2008-09-24 02:29:49 +02:00
|
|
|
class AttributeListImpl : public FoldingSetNode {
|
2008-03-12 18:45:29 +01:00
|
|
|
unsigned RefCount;
|
|
|
|
|
2008-09-25 23:00:45 +02:00
|
|
|
// AttributesList is uniqued, these should not be publicly available.
|
2008-09-24 02:29:49 +02:00
|
|
|
void operator=(const AttributeListImpl &); // Do not implement
|
|
|
|
AttributeListImpl(const AttributeListImpl &); // Do not implement
|
|
|
|
~AttributeListImpl(); // Private implementation
|
2008-03-12 18:45:29 +01:00
|
|
|
public:
|
2008-09-25 23:00:45 +02:00
|
|
|
SmallVector<AttributeWithIndex, 4> Attrs;
|
2008-03-12 18:45:29 +01:00
|
|
|
|
2008-09-25 23:00:45 +02:00
|
|
|
AttributeListImpl(const AttributeWithIndex *Attr, unsigned NumAttrs)
|
2008-03-12 18:45:29 +01:00
|
|
|
: Attrs(Attr, Attr+NumAttrs) {
|
|
|
|
RefCount = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AddRef() { ++RefCount; }
|
|
|
|
void DropRef() { if (--RefCount == 0) delete this; }
|
|
|
|
|
|
|
|
void Profile(FoldingSetNodeID &ID) const {
|
|
|
|
Profile(ID, &Attrs[0], Attrs.size());
|
|
|
|
}
|
2008-09-25 23:00:45 +02:00
|
|
|
static void Profile(FoldingSetNodeID &ID, const AttributeWithIndex *Attr,
|
2008-03-12 18:45:29 +01:00
|
|
|
unsigned NumAttrs) {
|
|
|
|
for (unsigned i = 0; i != NumAttrs; ++i)
|
|
|
|
ID.AddInteger(uint64_t(Attr[i].Attrs) << 32 | unsigned(Attr[i].Index));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2008-01-03 00:42:30 +01:00
|
|
|
|
2008-09-25 23:00:45 +02:00
|
|
|
static ManagedStatic<FoldingSet<AttributeListImpl> > AttributesLists;
|
2008-03-12 18:45:29 +01:00
|
|
|
|
2008-09-24 02:29:49 +02:00
|
|
|
AttributeListImpl::~AttributeListImpl() {
|
2008-09-25 23:00:45 +02:00
|
|
|
AttributesLists->RemoveNode(this);
|
2008-03-12 18:45:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-09-25 23:00:45 +02:00
|
|
|
AttrListPtr AttrListPtr::get(const AttributeWithIndex *Attrs, unsigned NumAttrs) {
|
|
|
|
// If there are no attributes then return a null AttributesList pointer.
|
2008-03-12 18:45:29 +01:00
|
|
|
if (NumAttrs == 0)
|
2008-09-25 23:00:45 +02:00
|
|
|
return AttrListPtr();
|
2008-03-12 18:45:29 +01:00
|
|
|
|
2008-01-03 00:42:30 +01:00
|
|
|
#ifndef NDEBUG
|
2008-03-12 18:45:29 +01:00
|
|
|
for (unsigned i = 0; i != NumAttrs; ++i) {
|
2008-09-25 23:00:45 +02:00
|
|
|
assert(Attrs[i].Attrs != Attribute::None &&
|
|
|
|
"Pointless attribute!");
|
2008-03-12 18:45:29 +01:00
|
|
|
assert((!i || Attrs[i-1].Index < Attrs[i].Index) &&
|
2008-09-25 23:00:45 +02:00
|
|
|
"Misordered AttributesList!");
|
2008-01-03 00:42:30 +01:00
|
|
|
}
|
|
|
|
#endif
|
2008-03-12 18:45:29 +01:00
|
|
|
|
2008-01-03 00:42:30 +01:00
|
|
|
// Otherwise, build a key to look up the existing attributes.
|
|
|
|
FoldingSetNodeID ID;
|
2008-09-24 02:29:49 +02:00
|
|
|
AttributeListImpl::Profile(ID, Attrs, NumAttrs);
|
2008-01-03 00:42:30 +01:00
|
|
|
void *InsertPos;
|
2008-09-24 02:29:49 +02:00
|
|
|
AttributeListImpl *PAL =
|
2008-09-25 23:00:45 +02:00
|
|
|
AttributesLists->FindNodeOrInsertPos(ID, InsertPos);
|
2008-03-12 18:45:29 +01:00
|
|
|
|
2008-01-03 00:42:30 +01:00
|
|
|
// If we didn't find any existing attributes of the same shape then
|
|
|
|
// create a new one and insert it.
|
|
|
|
if (!PAL) {
|
2008-09-24 02:29:49 +02:00
|
|
|
PAL = new AttributeListImpl(Attrs, NumAttrs);
|
2008-09-25 23:00:45 +02:00
|
|
|
AttributesLists->InsertNode(PAL, InsertPos);
|
2008-01-03 00:42:30 +01:00
|
|
|
}
|
2008-03-12 18:45:29 +01:00
|
|
|
|
2008-09-25 23:00:45 +02:00
|
|
|
// Return the AttributesList that we found or created.
|
|
|
|
return AttrListPtr(PAL);
|
2008-01-03 00:42:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-03-12 18:45:29 +01:00
|
|
|
//===----------------------------------------------------------------------===//
|
2008-09-25 23:00:45 +02:00
|
|
|
// AttrListPtr Method Implementations
|
2008-03-12 18:45:29 +01:00
|
|
|
//===----------------------------------------------------------------------===//
|
2008-01-03 00:42:30 +01:00
|
|
|
|
2008-09-25 23:00:45 +02:00
|
|
|
AttrListPtr::AttrListPtr(AttributeListImpl *LI) : AttrList(LI) {
|
2008-03-12 18:45:29 +01:00
|
|
|
if (LI) LI->AddRef();
|
|
|
|
}
|
2008-01-03 00:42:30 +01:00
|
|
|
|
2008-09-25 23:00:45 +02:00
|
|
|
AttrListPtr::AttrListPtr(const AttrListPtr &P) : AttrList(P.AttrList) {
|
|
|
|
if (AttrList) AttrList->AddRef();
|
2008-03-12 18:45:29 +01:00
|
|
|
}
|
|
|
|
|
2008-09-25 23:00:45 +02:00
|
|
|
const AttrListPtr &AttrListPtr::operator=(const AttrListPtr &RHS) {
|
|
|
|
if (AttrList == RHS.AttrList) return *this;
|
|
|
|
if (AttrList) AttrList->DropRef();
|
|
|
|
AttrList = RHS.AttrList;
|
|
|
|
if (AttrList) AttrList->AddRef();
|
2008-03-12 18:45:29 +01:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2008-09-25 23:00:45 +02:00
|
|
|
AttrListPtr::~AttrListPtr() {
|
|
|
|
if (AttrList) AttrList->DropRef();
|
2008-03-12 18:45:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// getNumSlots - Return the number of slots used in this attribute list.
|
|
|
|
/// This is the number of arguments that have an attribute set on them
|
|
|
|
/// (including the function itself).
|
2008-09-25 23:00:45 +02:00
|
|
|
unsigned AttrListPtr::getNumSlots() const {
|
|
|
|
return AttrList ? AttrList->Attrs.size() : 0;
|
2008-03-12 18:45:29 +01:00
|
|
|
}
|
2008-01-03 00:42:30 +01:00
|
|
|
|
2008-09-25 23:00:45 +02:00
|
|
|
/// getSlot - Return the AttributeWithIndex at the specified slot. This
|
|
|
|
/// holds a number plus a set of attributes.
|
|
|
|
const AttributeWithIndex &AttrListPtr::getSlot(unsigned Slot) const {
|
|
|
|
assert(AttrList && Slot < AttrList->Attrs.size() && "Slot # out of range!");
|
|
|
|
return AttrList->Attrs[Slot];
|
2008-03-12 18:45:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-09-25 23:00:45 +02:00
|
|
|
/// getAttributes - The attributes for the specified index are
|
|
|
|
/// returned. Attributes for the result are denoted with Idx = 0.
|
2008-09-24 00:35:17 +02:00
|
|
|
/// Function notes are denoted with idx = ~0.
|
2008-09-25 23:00:45 +02:00
|
|
|
Attributes AttrListPtr::getAttributes(unsigned Idx) const {
|
|
|
|
if (AttrList == 0) return Attribute::None;
|
2008-03-12 18:45:29 +01:00
|
|
|
|
2008-09-25 23:00:45 +02:00
|
|
|
const SmallVector<AttributeWithIndex, 4> &Attrs = AttrList->Attrs;
|
2008-03-12 18:45:29 +01:00
|
|
|
for (unsigned i = 0, e = Attrs.size(); i != e && Attrs[i].Index <= Idx; ++i)
|
|
|
|
if (Attrs[i].Index == Idx)
|
|
|
|
return Attrs[i].Attrs;
|
2008-09-25 23:00:45 +02:00
|
|
|
return Attribute::None;
|
2008-03-12 18:45:29 +01:00
|
|
|
}
|
2008-01-03 00:42:30 +01:00
|
|
|
|
2008-03-12 18:45:29 +01:00
|
|
|
/// hasAttrSomewhere - Return true if the specified attribute is set for at
|
|
|
|
/// least one parameter or for the return value.
|
2008-09-25 23:00:45 +02:00
|
|
|
bool AttrListPtr::hasAttrSomewhere(Attributes Attr) const {
|
|
|
|
if (AttrList == 0) return false;
|
2008-03-12 18:45:29 +01:00
|
|
|
|
2008-09-25 23:00:45 +02:00
|
|
|
const SmallVector<AttributeWithIndex, 4> &Attrs = AttrList->Attrs;
|
2008-03-12 18:45:29 +01:00
|
|
|
for (unsigned i = 0, e = Attrs.size(); i != e; ++i)
|
|
|
|
if (Attrs[i].Attrs & Attr)
|
|
|
|
return true;
|
|
|
|
return false;
|
2008-01-03 00:42:30 +01:00
|
|
|
}
|
|
|
|
|
2008-03-12 18:45:29 +01:00
|
|
|
|
2008-09-25 23:00:45 +02:00
|
|
|
AttrListPtr AttrListPtr::addAttr(unsigned Idx, Attributes Attrs) const {
|
|
|
|
Attributes OldAttrs = getAttributes(Idx);
|
2008-02-20 00:51:49 +01:00
|
|
|
#ifndef NDEBUG
|
|
|
|
// FIXME it is not obvious how this should work for alignment.
|
|
|
|
// For now, say we can't change a known alignment.
|
2008-09-25 23:00:45 +02:00
|
|
|
Attributes OldAlign = OldAttrs & Attribute::Alignment;
|
|
|
|
Attributes NewAlign = Attrs & Attribute::Alignment;
|
2008-02-20 13:07:57 +01:00
|
|
|
assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
|
2008-02-20 00:51:49 +01:00
|
|
|
"Attempt to change alignment!");
|
|
|
|
#endif
|
2008-03-12 18:45:29 +01:00
|
|
|
|
2008-09-24 01:03:40 +02:00
|
|
|
Attributes NewAttrs = OldAttrs | Attrs;
|
2008-01-03 00:42:30 +01:00
|
|
|
if (NewAttrs == OldAttrs)
|
2008-03-12 18:45:29 +01:00
|
|
|
return *this;
|
|
|
|
|
2008-09-25 23:00:45 +02:00
|
|
|
SmallVector<AttributeWithIndex, 8> NewAttrList;
|
|
|
|
if (AttrList == 0)
|
|
|
|
NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
|
2008-03-12 18:45:29 +01:00
|
|
|
else {
|
2008-09-25 23:00:45 +02:00
|
|
|
const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs;
|
2008-03-12 18:45:29 +01:00
|
|
|
unsigned i = 0, e = OldAttrList.size();
|
|
|
|
// Copy attributes for arguments before this one.
|
|
|
|
for (; i != e && OldAttrList[i].Index < Idx; ++i)
|
|
|
|
NewAttrList.push_back(OldAttrList[i]);
|
|
|
|
|
|
|
|
// If there are attributes already at this index, merge them in.
|
|
|
|
if (i != e && OldAttrList[i].Index == Idx) {
|
|
|
|
Attrs |= OldAttrList[i].Attrs;
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
|
2008-09-25 23:00:45 +02:00
|
|
|
NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
|
2008-03-12 18:45:29 +01:00
|
|
|
|
|
|
|
// Copy attributes for arguments after this one.
|
|
|
|
NewAttrList.insert(NewAttrList.end(),
|
|
|
|
OldAttrList.begin()+i, OldAttrList.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
return get(&NewAttrList[0], NewAttrList.size());
|
2008-01-03 00:42:30 +01:00
|
|
|
}
|
|
|
|
|
2008-09-25 23:00:45 +02:00
|
|
|
AttrListPtr AttrListPtr::removeAttr(unsigned Idx, Attributes Attrs) const {
|
2008-02-20 00:51:49 +01:00
|
|
|
#ifndef NDEBUG
|
|
|
|
// FIXME it is not obvious how this should work for alignment.
|
|
|
|
// For now, say we can't pass in alignment, which no current use does.
|
2008-09-25 23:00:45 +02:00
|
|
|
assert(!(Attrs & Attribute::Alignment) && "Attempt to exclude alignment!");
|
2008-02-20 00:51:49 +01:00
|
|
|
#endif
|
2008-09-25 23:00:45 +02:00
|
|
|
if (AttrList == 0) return AttrListPtr();
|
2008-03-12 18:45:29 +01:00
|
|
|
|
2008-09-25 23:00:45 +02:00
|
|
|
Attributes OldAttrs = getAttributes(Idx);
|
2008-09-24 01:03:40 +02:00
|
|
|
Attributes NewAttrs = OldAttrs & ~Attrs;
|
2008-01-03 00:42:30 +01:00
|
|
|
if (NewAttrs == OldAttrs)
|
2008-03-12 18:45:29 +01:00
|
|
|
return *this;
|
|
|
|
|
2008-09-25 23:00:45 +02:00
|
|
|
SmallVector<AttributeWithIndex, 8> NewAttrList;
|
|
|
|
const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs;
|
2008-03-12 18:45:29 +01:00
|
|
|
unsigned i = 0, e = OldAttrList.size();
|
|
|
|
|
|
|
|
// Copy attributes for arguments before this one.
|
|
|
|
for (; i != e && OldAttrList[i].Index < Idx; ++i)
|
|
|
|
NewAttrList.push_back(OldAttrList[i]);
|
|
|
|
|
|
|
|
// If there are attributes already at this index, merge them in.
|
|
|
|
assert(OldAttrList[i].Index == Idx && "Attribute isn't set?");
|
|
|
|
Attrs = OldAttrList[i].Attrs & ~Attrs;
|
|
|
|
++i;
|
|
|
|
if (Attrs) // If any attributes left for this parameter, add them.
|
2008-09-25 23:00:45 +02:00
|
|
|
NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
|
2008-03-12 18:45:29 +01:00
|
|
|
|
|
|
|
// Copy attributes for arguments after this one.
|
|
|
|
NewAttrList.insert(NewAttrList.end(),
|
|
|
|
OldAttrList.begin()+i, OldAttrList.end());
|
|
|
|
|
|
|
|
return get(&NewAttrList[0], NewAttrList.size());
|
2008-01-03 00:42:30 +01:00
|
|
|
}
|
|
|
|
|
2008-09-25 23:00:45 +02:00
|
|
|
void AttrListPtr::dump() const {
|
2008-03-12 18:45:29 +01:00
|
|
|
cerr << "PAL[ ";
|
|
|
|
for (unsigned i = 0; i < getNumSlots(); ++i) {
|
2008-09-25 23:00:45 +02:00
|
|
|
const AttributeWithIndex &PAWI = getSlot(i);
|
2008-03-12 18:45:29 +01:00
|
|
|
cerr << "{" << PAWI.Index << "," << PAWI.Attrs << "} ";
|
|
|
|
}
|
|
|
|
|
|
|
|
cerr << "]\n";
|
2008-01-06 19:27:01 +01:00
|
|
|
}
|