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 // Trim the trailing space.
92 assert(!Result.empty() && "Unknown attribute!");
93 Result.erase(Result.end()-1);
97 Attributes Attribute::typeIncompatible(Type *Ty) {
98 Attributes Incompatible = None;
100 if (!Ty->isIntegerTy())
101 // Attributes that only apply to integers.
102 Incompatible |= SExt | ZExt;
104 if (!Ty->isPointerTy())
105 // Attributes that only apply to pointers.
106 Incompatible |= ByVal | Nest | NoAlias | StructRet | NoCapture;
111 //===----------------------------------------------------------------------===//
112 // AttributeListImpl Definition
113 //===----------------------------------------------------------------------===//
116 class AttributeListImpl;
119 static ManagedStatic<FoldingSet<AttributeListImpl> > AttributesLists;
122 static ManagedStatic<sys::SmartMutex<true> > ALMutex;
124 class AttributeListImpl : public FoldingSetNode {
125 sys::cas_flag RefCount;
127 // AttributesList is uniqued, these should not be publicly available.
128 void operator=(const AttributeListImpl &); // Do not implement
129 AttributeListImpl(const AttributeListImpl &); // Do not implement
130 ~AttributeListImpl(); // Private implementation
132 SmallVector<AttributeWithIndex, 4> Attrs;
134 AttributeListImpl(ArrayRef<AttributeWithIndex> attrs)
135 : Attrs(attrs.begin(), attrs.end()) {
140 sys::SmartScopedLock<true> Lock(*ALMutex);
144 sys::SmartScopedLock<true> Lock(*ALMutex);
145 if (!AttributesLists.isConstructed())
147 sys::cas_flag new_val = --RefCount;
152 void Profile(FoldingSetNodeID &ID) const {
155 static void Profile(FoldingSetNodeID &ID, ArrayRef<AttributeWithIndex> Attrs){
156 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
157 ID.AddInteger(Attrs[i].Attrs.Raw());
158 ID.AddInteger(Attrs[i].Index);
164 AttributeListImpl::~AttributeListImpl() {
165 // NOTE: Lock must be acquired by caller.
166 AttributesLists->RemoveNode(this);
170 AttrListPtr AttrListPtr::get(ArrayRef<AttributeWithIndex> Attrs) {
171 // If there are no attributes then return a null AttributesList pointer.
173 return AttrListPtr();
176 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
177 assert(Attrs[i].Attrs != Attribute::None &&
178 "Pointless attribute!");
179 assert((!i || Attrs[i-1].Index < Attrs[i].Index) &&
180 "Misordered AttributesList!");
184 // Otherwise, build a key to look up the existing attributes.
186 AttributeListImpl::Profile(ID, Attrs);
189 sys::SmartScopedLock<true> Lock(*ALMutex);
191 AttributeListImpl *PAL =
192 AttributesLists->FindNodeOrInsertPos(ID, InsertPos);
194 // If we didn't find any existing attributes of the same shape then
195 // create a new one and insert it.
197 PAL = new AttributeListImpl(Attrs);
198 AttributesLists->InsertNode(PAL, InsertPos);
201 // Return the AttributesList that we found or created.
202 return AttrListPtr(PAL);
206 //===----------------------------------------------------------------------===//
207 // AttrListPtr Method Implementations
208 //===----------------------------------------------------------------------===//
210 AttrListPtr::AttrListPtr(AttributeListImpl *LI) : AttrList(LI) {
211 if (LI) LI->AddRef();
214 AttrListPtr::AttrListPtr(const AttrListPtr &P) : AttrList(P.AttrList) {
215 if (AttrList) AttrList->AddRef();
218 const AttrListPtr &AttrListPtr::operator=(const AttrListPtr &RHS) {
219 sys::SmartScopedLock<true> Lock(*ALMutex);
220 if (AttrList == RHS.AttrList) return *this;
221 if (AttrList) AttrList->DropRef();
222 AttrList = RHS.AttrList;
223 if (AttrList) AttrList->AddRef();
227 AttrListPtr::~AttrListPtr() {
228 if (AttrList) AttrList->DropRef();
231 /// getNumSlots - Return the number of slots used in this attribute list.
232 /// This is the number of arguments that have an attribute set on them
233 /// (including the function itself).
234 unsigned AttrListPtr::getNumSlots() const {
235 return AttrList ? AttrList->Attrs.size() : 0;
238 /// getSlot - Return the AttributeWithIndex at the specified slot. This
239 /// holds a number plus a set of attributes.
240 const AttributeWithIndex &AttrListPtr::getSlot(unsigned Slot) const {
241 assert(AttrList && Slot < AttrList->Attrs.size() && "Slot # out of range!");
242 return AttrList->Attrs[Slot];
246 /// getAttributes - The attributes for the specified index are
247 /// returned. Attributes for the result are denoted with Idx = 0.
248 /// Function notes are denoted with idx = ~0.
249 Attributes AttrListPtr::getAttributes(unsigned Idx) const {
250 if (AttrList == 0) return Attribute::None;
252 const SmallVector<AttributeWithIndex, 4> &Attrs = AttrList->Attrs;
253 for (unsigned i = 0, e = Attrs.size(); i != e && Attrs[i].Index <= Idx; ++i)
254 if (Attrs[i].Index == Idx)
255 return Attrs[i].Attrs;
256 return Attribute::None;
259 /// hasAttrSomewhere - Return true if the specified attribute is set for at
260 /// least one parameter or for the return value.
261 bool AttrListPtr::hasAttrSomewhere(Attributes Attr) const {
262 if (AttrList == 0) return false;
264 const SmallVector<AttributeWithIndex, 4> &Attrs = AttrList->Attrs;
265 for (unsigned i = 0, e = Attrs.size(); i != e; ++i)
266 if (Attrs[i].Attrs & Attr)
272 AttrListPtr AttrListPtr::addAttr(unsigned Idx, Attributes Attrs) const {
273 Attributes OldAttrs = getAttributes(Idx);
275 // FIXME it is not obvious how this should work for alignment.
276 // For now, say we can't change a known alignment.
277 Attributes OldAlign = OldAttrs & Attribute::Alignment;
278 Attributes NewAlign = Attrs & Attribute::Alignment;
279 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
280 "Attempt to change alignment!");
283 Attributes NewAttrs = OldAttrs | Attrs;
284 if (NewAttrs == OldAttrs)
287 SmallVector<AttributeWithIndex, 8> NewAttrList;
289 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
291 const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs;
292 unsigned i = 0, e = OldAttrList.size();
293 // Copy attributes for arguments before this one.
294 for (; i != e && OldAttrList[i].Index < Idx; ++i)
295 NewAttrList.push_back(OldAttrList[i]);
297 // If there are attributes already at this index, merge them in.
298 if (i != e && OldAttrList[i].Index == Idx) {
299 Attrs |= OldAttrList[i].Attrs;
303 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
305 // Copy attributes for arguments after this one.
306 NewAttrList.insert(NewAttrList.end(),
307 OldAttrList.begin()+i, OldAttrList.end());
310 return get(NewAttrList);
313 AttrListPtr AttrListPtr::removeAttr(unsigned Idx, Attributes Attrs) const {
315 // FIXME it is not obvious how this should work for alignment.
316 // For now, say we can't pass in alignment, which no current use does.
317 assert(!(Attrs & Attribute::Alignment) && "Attempt to exclude alignment!");
319 if (AttrList == 0) return AttrListPtr();
321 Attributes OldAttrs = getAttributes(Idx);
322 Attributes NewAttrs = OldAttrs & ~Attrs;
323 if (NewAttrs == OldAttrs)
326 SmallVector<AttributeWithIndex, 8> NewAttrList;
327 const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs;
328 unsigned i = 0, e = OldAttrList.size();
330 // Copy attributes for arguments before this one.
331 for (; i != e && OldAttrList[i].Index < Idx; ++i)
332 NewAttrList.push_back(OldAttrList[i]);
334 // If there are attributes already at this index, merge them in.
335 assert(OldAttrList[i].Index == Idx && "Attribute isn't set?");
336 Attrs = OldAttrList[i].Attrs & ~Attrs;
338 if (Attrs) // If any attributes left for this parameter, add them.
339 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
341 // Copy attributes for arguments after this one.
342 NewAttrList.insert(NewAttrList.end(),
343 OldAttrList.begin()+i, OldAttrList.end());
345 return get(NewAttrList);
348 void AttrListPtr::dump() const {
350 for (unsigned i = 0; i < getNumSlots(); ++i) {
351 const AttributeWithIndex &PAWI = getSlot(i);
352 dbgs() << "{" << PAWI.Index << "," << PAWI.Attrs << "} ";