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/Atomic.h"
19 #include "llvm/Support/Mutex.h"
20 #include "llvm/Support/Debug.h"
21 #include "llvm/Support/ManagedStatic.h"
22 #include "llvm/Support/raw_ostream.h"
25 //===----------------------------------------------------------------------===//
26 // Attribute Function Definitions
27 //===----------------------------------------------------------------------===//
29 std::string Attribute::getAsString(Attributes Attrs) {
31 if (Attrs & Attribute::ZExt)
33 if (Attrs & Attribute::SExt)
35 if (Attrs & Attribute::NoReturn)
36 Result += "noreturn ";
37 if (Attrs & Attribute::NoUnwind)
38 Result += "nounwind ";
39 if (Attrs & Attribute::UWTable)
41 if (Attrs & Attribute::ReturnsTwice)
42 Result += "returns_twice ";
43 if (Attrs & Attribute::InReg)
45 if (Attrs & Attribute::NoAlias)
47 if (Attrs & Attribute::NoCapture)
48 Result += "nocapture ";
49 if (Attrs & Attribute::StructRet)
51 if (Attrs & Attribute::ByVal)
53 if (Attrs & Attribute::Nest)
55 if (Attrs & Attribute::ReadNone)
56 Result += "readnone ";
57 if (Attrs & Attribute::ReadOnly)
58 Result += "readonly ";
59 if (Attrs & Attribute::OptimizeForSize)
61 if (Attrs & Attribute::NoInline)
62 Result += "noinline ";
63 if (Attrs & Attribute::InlineHint)
64 Result += "inlinehint ";
65 if (Attrs & Attribute::AlwaysInline)
66 Result += "alwaysinline ";
67 if (Attrs & Attribute::StackProtect)
69 if (Attrs & Attribute::StackProtectReq)
71 if (Attrs & Attribute::NoRedZone)
72 Result += "noredzone ";
73 if (Attrs & Attribute::NoImplicitFloat)
74 Result += "noimplicitfloat ";
75 if (Attrs & Attribute::Naked)
77 if (Attrs & Attribute::NonLazyBind)
78 Result += "nonlazybind ";
79 if (Attrs & Attribute::AddressSafety)
80 Result += "address_safety ";
81 if (Attrs & Attribute::StackAlignment) {
82 Result += "alignstack(";
83 Result += utostr(Attribute::getStackAlignmentFromAttrs(Attrs));
86 if (Attrs & Attribute::Alignment) {
88 Result += utostr(Attribute::getAlignmentFromAttrs(Attrs));
91 if (Attrs & Attribute::IANSDialect)
92 Result += "ia_nsdialect ";
94 // Trim the trailing space.
95 assert(!Result.empty() && "Unknown attribute!");
96 Result.erase(Result.end()-1);
100 Attributes Attribute::typeIncompatible(Type *Ty) {
101 Attributes Incompatible = None;
103 if (!Ty->isIntegerTy())
104 // Attributes that only apply to integers.
105 Incompatible |= SExt | ZExt;
107 if (!Ty->isPointerTy())
108 // Attributes that only apply to pointers.
109 Incompatible |= ByVal | Nest | NoAlias | StructRet | NoCapture;
114 //===----------------------------------------------------------------------===//
115 // AttributeListImpl Definition
116 //===----------------------------------------------------------------------===//
119 class AttributeListImpl;
122 static ManagedStatic<FoldingSet<AttributeListImpl> > AttributesLists;
125 static ManagedStatic<sys::SmartMutex<true> > ALMutex;
127 class AttributeListImpl : public FoldingSetNode {
128 sys::cas_flag RefCount;
130 // AttributesList is uniqued, these should not be publicly available.
131 void operator=(const AttributeListImpl &); // Do not implement
132 AttributeListImpl(const AttributeListImpl &); // Do not implement
133 ~AttributeListImpl(); // Private implementation
135 SmallVector<AttributeWithIndex, 4> Attrs;
137 AttributeListImpl(ArrayRef<AttributeWithIndex> attrs)
138 : Attrs(attrs.begin(), attrs.end()) {
143 sys::SmartScopedLock<true> Lock(*ALMutex);
147 sys::SmartScopedLock<true> Lock(*ALMutex);
148 if (!AttributesLists.isConstructed())
150 sys::cas_flag new_val = --RefCount;
155 void Profile(FoldingSetNodeID &ID) const {
158 static void Profile(FoldingSetNodeID &ID, ArrayRef<AttributeWithIndex> Attrs){
159 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
160 ID.AddInteger(Attrs[i].Attrs.Raw());
161 ID.AddInteger(Attrs[i].Index);
167 AttributeListImpl::~AttributeListImpl() {
168 // NOTE: Lock must be acquired by caller.
169 AttributesLists->RemoveNode(this);
173 AttrListPtr AttrListPtr::get(ArrayRef<AttributeWithIndex> Attrs) {
174 // If there are no attributes then return a null AttributesList pointer.
176 return AttrListPtr();
179 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
180 assert(Attrs[i].Attrs != Attribute::None &&
181 "Pointless attribute!");
182 assert((!i || Attrs[i-1].Index < Attrs[i].Index) &&
183 "Misordered AttributesList!");
187 // Otherwise, build a key to look up the existing attributes.
189 AttributeListImpl::Profile(ID, Attrs);
192 sys::SmartScopedLock<true> Lock(*ALMutex);
194 AttributeListImpl *PAL =
195 AttributesLists->FindNodeOrInsertPos(ID, InsertPos);
197 // If we didn't find any existing attributes of the same shape then
198 // create a new one and insert it.
200 PAL = new AttributeListImpl(Attrs);
201 AttributesLists->InsertNode(PAL, InsertPos);
204 // Return the AttributesList that we found or created.
205 return AttrListPtr(PAL);
209 //===----------------------------------------------------------------------===//
210 // AttrListPtr Method Implementations
211 //===----------------------------------------------------------------------===//
213 AttrListPtr::AttrListPtr(AttributeListImpl *LI) : AttrList(LI) {
214 if (LI) LI->AddRef();
217 AttrListPtr::AttrListPtr(const AttrListPtr &P) : AttrList(P.AttrList) {
218 if (AttrList) AttrList->AddRef();
221 const AttrListPtr &AttrListPtr::operator=(const AttrListPtr &RHS) {
222 sys::SmartScopedLock<true> Lock(*ALMutex);
223 if (AttrList == RHS.AttrList) return *this;
224 if (AttrList) AttrList->DropRef();
225 AttrList = RHS.AttrList;
226 if (AttrList) AttrList->AddRef();
230 AttrListPtr::~AttrListPtr() {
231 if (AttrList) AttrList->DropRef();
234 /// getNumSlots - Return the number of slots used in this attribute list.
235 /// This is the number of arguments that have an attribute set on them
236 /// (including the function itself).
237 unsigned AttrListPtr::getNumSlots() const {
238 return AttrList ? AttrList->Attrs.size() : 0;
241 /// getSlot - Return the AttributeWithIndex at the specified slot. This
242 /// holds a number plus a set of attributes.
243 const AttributeWithIndex &AttrListPtr::getSlot(unsigned Slot) const {
244 assert(AttrList && Slot < AttrList->Attrs.size() && "Slot # out of range!");
245 return AttrList->Attrs[Slot];
249 /// getAttributes - The attributes for the specified index are
250 /// returned. Attributes for the result are denoted with Idx = 0.
251 /// Function notes are denoted with idx = ~0.
252 Attributes AttrListPtr::getAttributes(unsigned Idx) const {
253 if (AttrList == 0) return Attribute::None;
255 const SmallVector<AttributeWithIndex, 4> &Attrs = AttrList->Attrs;
256 for (unsigned i = 0, e = Attrs.size(); i != e && Attrs[i].Index <= Idx; ++i)
257 if (Attrs[i].Index == Idx)
258 return Attrs[i].Attrs;
259 return Attribute::None;
262 /// hasAttrSomewhere - Return true if the specified attribute is set for at
263 /// least one parameter or for the return value.
264 bool AttrListPtr::hasAttrSomewhere(Attributes Attr) const {
265 if (AttrList == 0) return false;
267 const SmallVector<AttributeWithIndex, 4> &Attrs = AttrList->Attrs;
268 for (unsigned i = 0, e = Attrs.size(); i != e; ++i)
269 if (Attrs[i].Attrs & Attr)
275 AttrListPtr AttrListPtr::addAttr(unsigned Idx, Attributes Attrs) const {
276 Attributes OldAttrs = getAttributes(Idx);
278 // FIXME it is not obvious how this should work for alignment.
279 // For now, say we can't change a known alignment.
280 Attributes OldAlign = OldAttrs & Attribute::Alignment;
281 Attributes NewAlign = Attrs & Attribute::Alignment;
282 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
283 "Attempt to change alignment!");
286 Attributes NewAttrs = OldAttrs | Attrs;
287 if (NewAttrs == OldAttrs)
290 SmallVector<AttributeWithIndex, 8> NewAttrList;
292 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
294 const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs;
295 unsigned i = 0, e = OldAttrList.size();
296 // Copy attributes for arguments before this one.
297 for (; i != e && OldAttrList[i].Index < Idx; ++i)
298 NewAttrList.push_back(OldAttrList[i]);
300 // If there are attributes already at this index, merge them in.
301 if (i != e && OldAttrList[i].Index == Idx) {
302 Attrs |= OldAttrList[i].Attrs;
306 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
308 // Copy attributes for arguments after this one.
309 NewAttrList.insert(NewAttrList.end(),
310 OldAttrList.begin()+i, OldAttrList.end());
313 return get(NewAttrList);
316 AttrListPtr AttrListPtr::removeAttr(unsigned Idx, Attributes Attrs) const {
318 // FIXME it is not obvious how this should work for alignment.
319 // For now, say we can't pass in alignment, which no current use does.
320 assert(!(Attrs & Attribute::Alignment) && "Attempt to exclude alignment!");
322 if (AttrList == 0) return AttrListPtr();
324 Attributes OldAttrs = getAttributes(Idx);
325 Attributes NewAttrs = OldAttrs & ~Attrs;
326 if (NewAttrs == OldAttrs)
329 SmallVector<AttributeWithIndex, 8> NewAttrList;
330 const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs;
331 unsigned i = 0, e = OldAttrList.size();
333 // Copy attributes for arguments before this one.
334 for (; i != e && OldAttrList[i].Index < Idx; ++i)
335 NewAttrList.push_back(OldAttrList[i]);
337 // If there are attributes already at this index, merge them in.
338 assert(OldAttrList[i].Index == Idx && "Attribute isn't set?");
339 Attrs = OldAttrList[i].Attrs & ~Attrs;
341 if (Attrs) // If any attributes left for this parameter, add them.
342 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
344 // Copy attributes for arguments after this one.
345 NewAttrList.insert(NewAttrList.end(),
346 OldAttrList.begin()+i, OldAttrList.end());
348 return get(NewAttrList);
351 void AttrListPtr::dump() const {
353 for (unsigned i = 0; i < getNumSlots(); ++i) {
354 const AttributeWithIndex &PAWI = getSlot(i);
355 dbgs() << "{" << PAWI.Index << "," << PAWI.Attrs << "} ";