1 //===-- Attributes.cpp - Implement AttributesList -------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the AttributesList class and Attribute utilities.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Attributes.h"
15 #include "llvm/Type.h"
16 #include "llvm/ADT/StringExtras.h"
17 #include "llvm/ADT/FoldingSet.h"
18 #include "llvm/Support/Streams.h"
19 #include "llvm/Support/ManagedStatic.h"
22 //===----------------------------------------------------------------------===//
23 // Attribute Function Definitions
24 //===----------------------------------------------------------------------===//
26 std::string Attribute::getAsString(Attributes Attrs) {
28 if (Attrs & Attribute::ZExt)
30 if (Attrs & Attribute::SExt)
32 if (Attrs & Attribute::NoReturn)
33 Result += "noreturn ";
34 if (Attrs & Attribute::NoUnwind)
35 Result += "nounwind ";
36 if (Attrs & Attribute::InReg)
38 if (Attrs & Attribute::NoAlias)
40 if (Attrs & Attribute::StructRet)
42 if (Attrs & Attribute::ByVal)
44 if (Attrs & Attribute::Nest)
46 if (Attrs & Attribute::ReadNone)
47 Result += "readnone ";
48 if (Attrs & Attribute::ReadOnly)
49 Result += "readonly ";
50 if (Attrs & Attribute::OptimizeForSize)
52 if (Attrs & Attribute::NoInline)
53 Result += "noinline ";
54 if (Attrs & Attribute::AlwaysInline)
55 Result += "alwaysinline ";
56 if (Attrs & Attribute::StackProtect)
58 if (Attrs & Attribute::StackProtectReq)
60 if (Attrs & Attribute::Alignment) {
62 Result += utostr((Attrs & Attribute::Alignment) >> 16);
65 // Trim the trailing space.
66 Result.erase(Result.end()-1);
70 Attributes Attribute::typeIncompatible(const Type *Ty) {
71 Attributes Incompatible = None;
74 // Attributes that only apply to integers.
75 Incompatible |= SExt | ZExt;
77 if (!isa<PointerType>(Ty))
78 // Attributes that only apply to pointers.
79 Incompatible |= ByVal | Nest | NoAlias | StructRet;
84 //===----------------------------------------------------------------------===//
85 // AttributeListImpl Definition
86 //===----------------------------------------------------------------------===//
89 class AttributeListImpl : public FoldingSetNode {
92 // AttributesList is uniqued, these should not be publicly available.
93 void operator=(const AttributeListImpl &); // Do not implement
94 AttributeListImpl(const AttributeListImpl &); // Do not implement
95 ~AttributeListImpl(); // Private implementation
97 SmallVector<AttributeWithIndex, 4> Attrs;
99 AttributeListImpl(const AttributeWithIndex *Attr, unsigned NumAttrs)
100 : Attrs(Attr, Attr+NumAttrs) {
104 void AddRef() { ++RefCount; }
105 void DropRef() { if (--RefCount == 0) delete this; }
107 void Profile(FoldingSetNodeID &ID) const {
108 Profile(ID, &Attrs[0], Attrs.size());
110 static void Profile(FoldingSetNodeID &ID, const AttributeWithIndex *Attr,
112 for (unsigned i = 0; i != NumAttrs; ++i)
113 ID.AddInteger(uint64_t(Attr[i].Attrs) << 32 | unsigned(Attr[i].Index));
118 static ManagedStatic<FoldingSet<AttributeListImpl> > AttributesLists;
120 AttributeListImpl::~AttributeListImpl() {
121 AttributesLists->RemoveNode(this);
125 AttrListPtr AttrListPtr::get(const AttributeWithIndex *Attrs, unsigned NumAttrs) {
126 // If there are no attributes then return a null AttributesList pointer.
128 return AttrListPtr();
131 for (unsigned i = 0; i != NumAttrs; ++i) {
132 assert(Attrs[i].Attrs != Attribute::None &&
133 "Pointless attribute!");
134 assert((!i || Attrs[i-1].Index < Attrs[i].Index) &&
135 "Misordered AttributesList!");
139 // Otherwise, build a key to look up the existing attributes.
141 AttributeListImpl::Profile(ID, Attrs, NumAttrs);
143 AttributeListImpl *PAL =
144 AttributesLists->FindNodeOrInsertPos(ID, InsertPos);
146 // If we didn't find any existing attributes of the same shape then
147 // create a new one and insert it.
149 PAL = new AttributeListImpl(Attrs, NumAttrs);
150 AttributesLists->InsertNode(PAL, InsertPos);
153 // Return the AttributesList that we found or created.
154 return AttrListPtr(PAL);
158 //===----------------------------------------------------------------------===//
159 // AttrListPtr Method Implementations
160 //===----------------------------------------------------------------------===//
162 AttrListPtr::AttrListPtr(AttributeListImpl *LI) : AttrList(LI) {
163 if (LI) LI->AddRef();
166 AttrListPtr::AttrListPtr(const AttrListPtr &P) : AttrList(P.AttrList) {
167 if (AttrList) AttrList->AddRef();
170 const AttrListPtr &AttrListPtr::operator=(const AttrListPtr &RHS) {
171 if (AttrList == RHS.AttrList) return *this;
172 if (AttrList) AttrList->DropRef();
173 AttrList = RHS.AttrList;
174 if (AttrList) AttrList->AddRef();
178 AttrListPtr::~AttrListPtr() {
179 if (AttrList) AttrList->DropRef();
182 /// getNumSlots - Return the number of slots used in this attribute list.
183 /// This is the number of arguments that have an attribute set on them
184 /// (including the function itself).
185 unsigned AttrListPtr::getNumSlots() const {
186 return AttrList ? AttrList->Attrs.size() : 0;
189 /// getSlot - Return the AttributeWithIndex at the specified slot. This
190 /// holds a number plus a set of attributes.
191 const AttributeWithIndex &AttrListPtr::getSlot(unsigned Slot) const {
192 assert(AttrList && Slot < AttrList->Attrs.size() && "Slot # out of range!");
193 return AttrList->Attrs[Slot];
197 /// getAttributes - The attributes for the specified index are
198 /// returned. Attributes for the result are denoted with Idx = 0.
199 /// Function notes are denoted with idx = ~0.
200 Attributes AttrListPtr::getAttributes(unsigned Idx) const {
201 if (AttrList == 0) return Attribute::None;
203 const SmallVector<AttributeWithIndex, 4> &Attrs = AttrList->Attrs;
204 for (unsigned i = 0, e = Attrs.size(); i != e && Attrs[i].Index <= Idx; ++i)
205 if (Attrs[i].Index == Idx)
206 return Attrs[i].Attrs;
207 return Attribute::None;
210 /// hasAttrSomewhere - Return true if the specified attribute is set for at
211 /// least one parameter or for the return value.
212 bool AttrListPtr::hasAttrSomewhere(Attributes Attr) const {
213 if (AttrList == 0) return false;
215 const SmallVector<AttributeWithIndex, 4> &Attrs = AttrList->Attrs;
216 for (unsigned i = 0, e = Attrs.size(); i != e; ++i)
217 if (Attrs[i].Attrs & Attr)
223 AttrListPtr AttrListPtr::addAttr(unsigned Idx, Attributes Attrs) const {
224 Attributes OldAttrs = getAttributes(Idx);
226 // FIXME it is not obvious how this should work for alignment.
227 // For now, say we can't change a known alignment.
228 Attributes OldAlign = OldAttrs & Attribute::Alignment;
229 Attributes NewAlign = Attrs & Attribute::Alignment;
230 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
231 "Attempt to change alignment!");
234 Attributes NewAttrs = OldAttrs | Attrs;
235 if (NewAttrs == OldAttrs)
238 SmallVector<AttributeWithIndex, 8> NewAttrList;
240 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
242 const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs;
243 unsigned i = 0, e = OldAttrList.size();
244 // Copy attributes for arguments before this one.
245 for (; i != e && OldAttrList[i].Index < Idx; ++i)
246 NewAttrList.push_back(OldAttrList[i]);
248 // If there are attributes already at this index, merge them in.
249 if (i != e && OldAttrList[i].Index == Idx) {
250 Attrs |= OldAttrList[i].Attrs;
254 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
256 // Copy attributes for arguments after this one.
257 NewAttrList.insert(NewAttrList.end(),
258 OldAttrList.begin()+i, OldAttrList.end());
261 return get(&NewAttrList[0], NewAttrList.size());
264 AttrListPtr AttrListPtr::removeAttr(unsigned Idx, Attributes Attrs) const {
266 // FIXME it is not obvious how this should work for alignment.
267 // For now, say we can't pass in alignment, which no current use does.
268 assert(!(Attrs & Attribute::Alignment) && "Attempt to exclude alignment!");
270 if (AttrList == 0) return AttrListPtr();
272 Attributes OldAttrs = getAttributes(Idx);
273 Attributes NewAttrs = OldAttrs & ~Attrs;
274 if (NewAttrs == OldAttrs)
277 SmallVector<AttributeWithIndex, 8> NewAttrList;
278 const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs;
279 unsigned i = 0, e = OldAttrList.size();
281 // Copy attributes for arguments before this one.
282 for (; i != e && OldAttrList[i].Index < Idx; ++i)
283 NewAttrList.push_back(OldAttrList[i]);
285 // If there are attributes already at this index, merge them in.
286 assert(OldAttrList[i].Index == Idx && "Attribute isn't set?");
287 Attrs = OldAttrList[i].Attrs & ~Attrs;
289 if (Attrs) // If any attributes left for this parameter, add them.
290 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
292 // Copy attributes for arguments after this one.
293 NewAttrList.insert(NewAttrList.end(),
294 OldAttrList.begin()+i, OldAttrList.end());
296 return get(&NewAttrList[0], NewAttrList.size());
299 void AttrListPtr::dump() const {
301 for (unsigned i = 0; i < getNumSlots(); ++i) {
302 const AttributeWithIndex &PAWI = getSlot(i);
303 cerr << "{" << PAWI.Index << "," << PAWI.Attrs << "} ";