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/System/Atomic.h"
19 #include "llvm/System/Mutex.h"
20 #include "llvm/Support/Streams.h"
21 #include "llvm/Support/ManagedStatic.h"
24 //===----------------------------------------------------------------------===//
25 // Attribute Function Definitions
26 //===----------------------------------------------------------------------===//
28 std::string Attribute::getAsString(Attributes Attrs) {
30 if (Attrs & Attribute::ZExt)
32 if (Attrs & Attribute::SExt)
34 if (Attrs & Attribute::NoReturn)
35 Result += "noreturn ";
36 if (Attrs & Attribute::NoUnwind)
37 Result += "nounwind ";
38 if (Attrs & Attribute::InReg)
40 if (Attrs & Attribute::NoAlias)
42 if (Attrs & Attribute::NoCapture)
43 Result += "nocapture ";
44 if (Attrs & Attribute::StructRet)
46 if (Attrs & Attribute::ByVal)
48 if (Attrs & Attribute::Nest)
50 if (Attrs & Attribute::ReadNone)
51 Result += "readnone ";
52 if (Attrs & Attribute::ReadOnly)
53 Result += "readonly ";
54 if (Attrs & Attribute::OptimizeForSize)
56 if (Attrs & Attribute::NoInline)
57 Result += "noinline ";
58 if (Attrs & Attribute::AlwaysInline)
59 Result += "alwaysinline ";
60 if (Attrs & Attribute::StackProtect)
62 if (Attrs & Attribute::StackProtectReq)
64 if (Attrs & Attribute::NoRedZone)
65 Result += "noredzone ";
66 if (Attrs & Attribute::NoImplicitFloat)
67 Result += "noimplicitfloat ";
68 if (Attrs & Attribute::Naked)
70 if (Attrs & Attribute::Alignment) {
72 Result += utostr(Attribute::getAlignmentFromAttrs(Attrs));
75 // Trim the trailing space.
76 assert(!Result.empty() && "Unknown attribute!");
77 Result.erase(Result.end()-1);
81 Attributes Attribute::typeIncompatible(const Type *Ty) {
82 Attributes Incompatible = None;
85 // Attributes that only apply to integers.
86 Incompatible |= SExt | ZExt;
88 if (!isa<PointerType>(Ty))
89 // Attributes that only apply to pointers.
90 Incompatible |= ByVal | Nest | NoAlias | StructRet | NoCapture;
95 //===----------------------------------------------------------------------===//
96 // AttributeListImpl Definition
97 //===----------------------------------------------------------------------===//
100 class AttributeListImpl : public FoldingSetNode {
101 sys::cas_flag RefCount;
103 // AttributesList is uniqued, these should not be publicly available.
104 void operator=(const AttributeListImpl &); // Do not implement
105 AttributeListImpl(const AttributeListImpl &); // Do not implement
106 ~AttributeListImpl(); // Private implementation
108 SmallVector<AttributeWithIndex, 4> Attrs;
110 AttributeListImpl(const AttributeWithIndex *Attr, unsigned NumAttrs)
111 : Attrs(Attr, Attr+NumAttrs) {
115 void AddRef() { sys::AtomicIncrement(&RefCount); }
117 sys::cas_flag old = sys::AtomicDecrement(&RefCount);
118 if (old == 0) delete this;
121 void Profile(FoldingSetNodeID &ID) const {
122 Profile(ID, Attrs.data(), Attrs.size());
124 static void Profile(FoldingSetNodeID &ID, const AttributeWithIndex *Attr,
126 for (unsigned i = 0; i != NumAttrs; ++i)
127 ID.AddInteger(uint64_t(Attr[i].Attrs) << 32 | unsigned(Attr[i].Index));
132 static ManagedStatic<sys::SmartMutex<true> > ALMutex;
133 static ManagedStatic<FoldingSet<AttributeListImpl> > AttributesLists;
135 AttributeListImpl::~AttributeListImpl() {
136 sys::SmartScopedLock<true> Lock(*ALMutex);
137 AttributesLists->RemoveNode(this);
141 AttrListPtr AttrListPtr::get(const AttributeWithIndex *Attrs, unsigned NumAttrs) {
142 // If there are no attributes then return a null AttributesList pointer.
144 return AttrListPtr();
147 for (unsigned i = 0; i != NumAttrs; ++i) {
148 assert(Attrs[i].Attrs != Attribute::None &&
149 "Pointless attribute!");
150 assert((!i || Attrs[i-1].Index < Attrs[i].Index) &&
151 "Misordered AttributesList!");
155 // Otherwise, build a key to look up the existing attributes.
157 AttributeListImpl::Profile(ID, Attrs, NumAttrs);
160 sys::SmartScopedLock<true> Lock(*ALMutex);
162 AttributeListImpl *PAL =
163 AttributesLists->FindNodeOrInsertPos(ID, InsertPos);
165 // If we didn't find any existing attributes of the same shape then
166 // create a new one and insert it.
168 PAL = new AttributeListImpl(Attrs, NumAttrs);
169 AttributesLists->InsertNode(PAL, InsertPos);
172 // Return the AttributesList that we found or created.
173 return AttrListPtr(PAL);
177 //===----------------------------------------------------------------------===//
178 // AttrListPtr Method Implementations
179 //===----------------------------------------------------------------------===//
181 AttrListPtr::AttrListPtr(AttributeListImpl *LI) : AttrList(LI) {
182 if (LI) LI->AddRef();
185 AttrListPtr::AttrListPtr(const AttrListPtr &P) : AttrList(P.AttrList) {
186 if (AttrList) AttrList->AddRef();
189 const AttrListPtr &AttrListPtr::operator=(const AttrListPtr &RHS) {
190 if (AttrList == RHS.AttrList) return *this;
191 if (AttrList) AttrList->DropRef();
192 AttrList = RHS.AttrList;
193 if (AttrList) AttrList->AddRef();
197 AttrListPtr::~AttrListPtr() {
198 if (AttrList) AttrList->DropRef();
201 /// getNumSlots - Return the number of slots used in this attribute list.
202 /// This is the number of arguments that have an attribute set on them
203 /// (including the function itself).
204 unsigned AttrListPtr::getNumSlots() const {
205 return AttrList ? AttrList->Attrs.size() : 0;
208 /// getSlot - Return the AttributeWithIndex at the specified slot. This
209 /// holds a number plus a set of attributes.
210 const AttributeWithIndex &AttrListPtr::getSlot(unsigned Slot) const {
211 assert(AttrList && Slot < AttrList->Attrs.size() && "Slot # out of range!");
212 return AttrList->Attrs[Slot];
216 /// getAttributes - The attributes for the specified index are
217 /// returned. Attributes for the result are denoted with Idx = 0.
218 /// Function notes are denoted with idx = ~0.
219 Attributes AttrListPtr::getAttributes(unsigned Idx) const {
220 if (AttrList == 0) return Attribute::None;
222 const SmallVector<AttributeWithIndex, 4> &Attrs = AttrList->Attrs;
223 for (unsigned i = 0, e = Attrs.size(); i != e && Attrs[i].Index <= Idx; ++i)
224 if (Attrs[i].Index == Idx)
225 return Attrs[i].Attrs;
226 return Attribute::None;
229 /// hasAttrSomewhere - Return true if the specified attribute is set for at
230 /// least one parameter or for the return value.
231 bool AttrListPtr::hasAttrSomewhere(Attributes Attr) const {
232 if (AttrList == 0) return false;
234 const SmallVector<AttributeWithIndex, 4> &Attrs = AttrList->Attrs;
235 for (unsigned i = 0, e = Attrs.size(); i != e; ++i)
236 if (Attrs[i].Attrs & Attr)
242 AttrListPtr AttrListPtr::addAttr(unsigned Idx, Attributes Attrs) const {
243 Attributes OldAttrs = getAttributes(Idx);
245 // FIXME it is not obvious how this should work for alignment.
246 // For now, say we can't change a known alignment.
247 Attributes OldAlign = OldAttrs & Attribute::Alignment;
248 Attributes NewAlign = Attrs & Attribute::Alignment;
249 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
250 "Attempt to change alignment!");
253 Attributes NewAttrs = OldAttrs | Attrs;
254 if (NewAttrs == OldAttrs)
257 SmallVector<AttributeWithIndex, 8> NewAttrList;
259 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
261 const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs;
262 unsigned i = 0, e = OldAttrList.size();
263 // Copy attributes for arguments before this one.
264 for (; i != e && OldAttrList[i].Index < Idx; ++i)
265 NewAttrList.push_back(OldAttrList[i]);
267 // If there are attributes already at this index, merge them in.
268 if (i != e && OldAttrList[i].Index == Idx) {
269 Attrs |= OldAttrList[i].Attrs;
273 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
275 // Copy attributes for arguments after this one.
276 NewAttrList.insert(NewAttrList.end(),
277 OldAttrList.begin()+i, OldAttrList.end());
280 return get(NewAttrList.data(), NewAttrList.size());
283 AttrListPtr AttrListPtr::removeAttr(unsigned Idx, Attributes Attrs) const {
285 // FIXME it is not obvious how this should work for alignment.
286 // For now, say we can't pass in alignment, which no current use does.
287 assert(!(Attrs & Attribute::Alignment) && "Attempt to exclude alignment!");
289 if (AttrList == 0) return AttrListPtr();
291 Attributes OldAttrs = getAttributes(Idx);
292 Attributes NewAttrs = OldAttrs & ~Attrs;
293 if (NewAttrs == OldAttrs)
296 SmallVector<AttributeWithIndex, 8> NewAttrList;
297 const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs;
298 unsigned i = 0, e = OldAttrList.size();
300 // Copy attributes for arguments before this one.
301 for (; i != e && OldAttrList[i].Index < Idx; ++i)
302 NewAttrList.push_back(OldAttrList[i]);
304 // If there are attributes already at this index, merge them in.
305 assert(OldAttrList[i].Index == Idx && "Attribute isn't set?");
306 Attrs = OldAttrList[i].Attrs & ~Attrs;
308 if (Attrs) // If any attributes left for this parameter, add them.
309 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
311 // Copy attributes for arguments after this one.
312 NewAttrList.insert(NewAttrList.end(),
313 OldAttrList.begin()+i, OldAttrList.end());
315 return get(NewAttrList.data(), NewAttrList.size());
318 void AttrListPtr::dump() const {
320 for (unsigned i = 0; i < getNumSlots(); ++i) {
321 const AttributeWithIndex &PAWI = getSlot(i);
322 cerr << "{" << PAWI.Index << "," << PAWI.Attrs << "} ";