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/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::InReg)
41 if (Attrs & Attribute::NoAlias)
43 if (Attrs & Attribute::NoCapture)
44 Result += "nocapture ";
45 if (Attrs & Attribute::StructRet)
47 if (Attrs & Attribute::ByVal)
49 if (Attrs & Attribute::Nest)
51 if (Attrs & Attribute::ReadNone)
52 Result += "readnone ";
53 if (Attrs & Attribute::ReadOnly)
54 Result += "readonly ";
55 if (Attrs & Attribute::OptimizeForSize)
57 if (Attrs & Attribute::NoInline)
58 Result += "noinline ";
59 if (Attrs & Attribute::AlwaysInline)
60 Result += "alwaysinline ";
61 if (Attrs & Attribute::StackProtect)
63 if (Attrs & Attribute::StackProtectReq)
65 if (Attrs & Attribute::NoRedZone)
66 Result += "noredzone ";
67 if (Attrs & Attribute::NoImplicitFloat)
68 Result += "noimplicitfloat ";
69 if (Attrs & Attribute::Naked)
71 if (Attrs & Attribute::Alignment) {
73 Result += utostr(Attribute::getAlignmentFromAttrs(Attrs));
76 // Trim the trailing space.
77 assert(!Result.empty() && "Unknown attribute!");
78 Result.erase(Result.end()-1);
82 Attributes Attribute::typeIncompatible(const Type *Ty) {
83 Attributes Incompatible = None;
86 // Attributes that only apply to integers.
87 Incompatible |= SExt | ZExt;
89 if (!isa<PointerType>(Ty))
90 // Attributes that only apply to pointers.
91 Incompatible |= ByVal | Nest | NoAlias | StructRet | NoCapture;
96 //===----------------------------------------------------------------------===//
97 // AttributeListImpl Definition
98 //===----------------------------------------------------------------------===//
101 class AttributeListImpl : public FoldingSetNode {
102 sys::cas_flag RefCount;
104 // AttributesList is uniqued, these should not be publicly available.
105 void operator=(const AttributeListImpl &); // Do not implement
106 AttributeListImpl(const AttributeListImpl &); // Do not implement
107 ~AttributeListImpl(); // Private implementation
109 SmallVector<AttributeWithIndex, 4> Attrs;
111 AttributeListImpl(const AttributeWithIndex *Attr, unsigned NumAttrs)
112 : Attrs(Attr, Attr+NumAttrs) {
116 void AddRef() { sys::AtomicIncrement(&RefCount); }
118 sys::cas_flag old = sys::AtomicDecrement(&RefCount);
119 if (old == 0) delete this;
122 void Profile(FoldingSetNodeID &ID) const {
123 Profile(ID, Attrs.data(), Attrs.size());
125 static void Profile(FoldingSetNodeID &ID, const AttributeWithIndex *Attr,
127 for (unsigned i = 0; i != NumAttrs; ++i)
128 ID.AddInteger(uint64_t(Attr[i].Attrs) << 32 | unsigned(Attr[i].Index));
133 static ManagedStatic<sys::SmartMutex<true> > ALMutex;
134 static ManagedStatic<FoldingSet<AttributeListImpl> > AttributesLists;
136 AttributeListImpl::~AttributeListImpl() {
137 sys::SmartScopedLock<true> Lock(*ALMutex);
138 AttributesLists->RemoveNode(this);
142 AttrListPtr AttrListPtr::get(const AttributeWithIndex *Attrs, unsigned NumAttrs) {
143 // If there are no attributes then return a null AttributesList pointer.
145 return AttrListPtr();
148 for (unsigned i = 0; i != NumAttrs; ++i) {
149 assert(Attrs[i].Attrs != Attribute::None &&
150 "Pointless attribute!");
151 assert((!i || Attrs[i-1].Index < Attrs[i].Index) &&
152 "Misordered AttributesList!");
156 // Otherwise, build a key to look up the existing attributes.
158 AttributeListImpl::Profile(ID, Attrs, NumAttrs);
161 sys::SmartScopedLock<true> Lock(*ALMutex);
163 AttributeListImpl *PAL =
164 AttributesLists->FindNodeOrInsertPos(ID, InsertPos);
166 // If we didn't find any existing attributes of the same shape then
167 // create a new one and insert it.
169 PAL = new AttributeListImpl(Attrs, NumAttrs);
170 AttributesLists->InsertNode(PAL, InsertPos);
173 // Return the AttributesList that we found or created.
174 return AttrListPtr(PAL);
178 //===----------------------------------------------------------------------===//
179 // AttrListPtr Method Implementations
180 //===----------------------------------------------------------------------===//
182 AttrListPtr::AttrListPtr(AttributeListImpl *LI) : AttrList(LI) {
183 if (LI) LI->AddRef();
186 AttrListPtr::AttrListPtr(const AttrListPtr &P) : AttrList(P.AttrList) {
187 if (AttrList) AttrList->AddRef();
190 const AttrListPtr &AttrListPtr::operator=(const AttrListPtr &RHS) {
191 if (AttrList == RHS.AttrList) return *this;
192 if (AttrList) AttrList->DropRef();
193 AttrList = RHS.AttrList;
194 if (AttrList) AttrList->AddRef();
198 AttrListPtr::~AttrListPtr() {
199 if (AttrList) AttrList->DropRef();
202 /// getNumSlots - Return the number of slots used in this attribute list.
203 /// This is the number of arguments that have an attribute set on them
204 /// (including the function itself).
205 unsigned AttrListPtr::getNumSlots() const {
206 return AttrList ? AttrList->Attrs.size() : 0;
209 /// getSlot - Return the AttributeWithIndex at the specified slot. This
210 /// holds a number plus a set of attributes.
211 const AttributeWithIndex &AttrListPtr::getSlot(unsigned Slot) const {
212 assert(AttrList && Slot < AttrList->Attrs.size() && "Slot # out of range!");
213 return AttrList->Attrs[Slot];
217 /// getAttributes - The attributes for the specified index are
218 /// returned. Attributes for the result are denoted with Idx = 0.
219 /// Function notes are denoted with idx = ~0.
220 Attributes AttrListPtr::getAttributes(unsigned Idx) const {
221 if (AttrList == 0) return Attribute::None;
223 const SmallVector<AttributeWithIndex, 4> &Attrs = AttrList->Attrs;
224 for (unsigned i = 0, e = Attrs.size(); i != e && Attrs[i].Index <= Idx; ++i)
225 if (Attrs[i].Index == Idx)
226 return Attrs[i].Attrs;
227 return Attribute::None;
230 /// hasAttrSomewhere - Return true if the specified attribute is set for at
231 /// least one parameter or for the return value.
232 bool AttrListPtr::hasAttrSomewhere(Attributes Attr) const {
233 if (AttrList == 0) return false;
235 const SmallVector<AttributeWithIndex, 4> &Attrs = AttrList->Attrs;
236 for (unsigned i = 0, e = Attrs.size(); i != e; ++i)
237 if (Attrs[i].Attrs & Attr)
243 AttrListPtr AttrListPtr::addAttr(unsigned Idx, Attributes Attrs) const {
244 Attributes OldAttrs = getAttributes(Idx);
246 // FIXME it is not obvious how this should work for alignment.
247 // For now, say we can't change a known alignment.
248 Attributes OldAlign = OldAttrs & Attribute::Alignment;
249 Attributes NewAlign = Attrs & Attribute::Alignment;
250 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
251 "Attempt to change alignment!");
254 Attributes NewAttrs = OldAttrs | Attrs;
255 if (NewAttrs == OldAttrs)
258 SmallVector<AttributeWithIndex, 8> NewAttrList;
260 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
262 const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs;
263 unsigned i = 0, e = OldAttrList.size();
264 // Copy attributes for arguments before this one.
265 for (; i != e && OldAttrList[i].Index < Idx; ++i)
266 NewAttrList.push_back(OldAttrList[i]);
268 // If there are attributes already at this index, merge them in.
269 if (i != e && OldAttrList[i].Index == Idx) {
270 Attrs |= OldAttrList[i].Attrs;
274 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
276 // Copy attributes for arguments after this one.
277 NewAttrList.insert(NewAttrList.end(),
278 OldAttrList.begin()+i, OldAttrList.end());
281 return get(NewAttrList.data(), NewAttrList.size());
284 AttrListPtr AttrListPtr::removeAttr(unsigned Idx, Attributes Attrs) const {
286 // FIXME it is not obvious how this should work for alignment.
287 // For now, say we can't pass in alignment, which no current use does.
288 assert(!(Attrs & Attribute::Alignment) && "Attempt to exclude alignment!");
290 if (AttrList == 0) return AttrListPtr();
292 Attributes OldAttrs = getAttributes(Idx);
293 Attributes NewAttrs = OldAttrs & ~Attrs;
294 if (NewAttrs == OldAttrs)
297 SmallVector<AttributeWithIndex, 8> NewAttrList;
298 const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs;
299 unsigned i = 0, e = OldAttrList.size();
301 // Copy attributes for arguments before this one.
302 for (; i != e && OldAttrList[i].Index < Idx; ++i)
303 NewAttrList.push_back(OldAttrList[i]);
305 // If there are attributes already at this index, merge them in.
306 assert(OldAttrList[i].Index == Idx && "Attribute isn't set?");
307 Attrs = OldAttrList[i].Attrs & ~Attrs;
309 if (Attrs) // If any attributes left for this parameter, add them.
310 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
312 // Copy attributes for arguments after this one.
313 NewAttrList.insert(NewAttrList.end(),
314 OldAttrList.begin()+i, OldAttrList.end());
316 return get(NewAttrList.data(), NewAttrList.size());
319 void AttrListPtr::dump() const {
321 for (unsigned i = 0; i < getNumSlots(); ++i) {
322 const AttributeWithIndex &PAWI = getSlot(i);
323 dbgs() << "{" << PAWI.Index << "," << PAWI.Attrs << "} ";