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/Mutex.h"
19 #include "llvm/Support/Streams.h"
20 #include "llvm/Support/ManagedStatic.h"
23 //===----------------------------------------------------------------------===//
24 // Attribute Function Definitions
25 //===----------------------------------------------------------------------===//
27 std::string Attribute::getAsString(Attributes Attrs) {
29 if (Attrs & Attribute::ZExt)
31 if (Attrs & Attribute::SExt)
33 if (Attrs & Attribute::NoReturn)
34 Result += "noreturn ";
35 if (Attrs & Attribute::NoUnwind)
36 Result += "nounwind ";
37 if (Attrs & Attribute::InReg)
39 if (Attrs & Attribute::NoAlias)
41 if (Attrs & Attribute::NoCapture)
42 Result += "nocapture ";
43 if (Attrs & Attribute::StructRet)
45 if (Attrs & Attribute::ByVal)
47 if (Attrs & Attribute::Nest)
49 if (Attrs & Attribute::ReadNone)
50 Result += "readnone ";
51 if (Attrs & Attribute::ReadOnly)
52 Result += "readonly ";
53 if (Attrs & Attribute::OptimizeForSize)
55 if (Attrs & Attribute::NoInline)
56 Result += "noinline ";
57 if (Attrs & Attribute::AlwaysInline)
58 Result += "alwaysinline ";
59 if (Attrs & Attribute::StackProtect)
61 if (Attrs & Attribute::StackProtectReq)
63 if (Attrs & Attribute::NoRedZone)
64 Result += "noredzone ";
65 if (Attrs & Attribute::NoImplicitFloat)
66 Result += "noimplicitfloat ";
67 if (Attrs & Attribute::Naked)
69 if (Attrs & Attribute::Alignment) {
71 Result += utostr(Attribute::getAlignmentFromAttrs(Attrs));
74 // Trim the trailing space.
75 assert(!Result.empty() && "Unknown attribute!");
76 Result.erase(Result.end()-1);
80 Attributes Attribute::typeIncompatible(const Type *Ty) {
81 Attributes Incompatible = None;
84 // Attributes that only apply to integers.
85 Incompatible |= SExt | ZExt;
87 if (!isa<PointerType>(Ty))
88 // Attributes that only apply to pointers.
89 Incompatible |= ByVal | Nest | NoAlias | StructRet | NoCapture;
94 //===----------------------------------------------------------------------===//
95 // AttributeListImpl Definition
96 //===----------------------------------------------------------------------===//
99 class AttributeListImpl : public FoldingSetNode {
102 // AttributesList is uniqued, these should not be publicly available.
103 void operator=(const AttributeListImpl &); // Do not implement
104 AttributeListImpl(const AttributeListImpl &); // Do not implement
105 ~AttributeListImpl(); // Private implementation
107 SmallVector<AttributeWithIndex, 4> Attrs;
109 AttributeListImpl(const AttributeWithIndex *Attr, unsigned NumAttrs)
110 : Attrs(Attr, Attr+NumAttrs) {
114 void AddRef() { ++RefCount; }
115 void DropRef() { if (--RefCount == 0) delete this; }
117 void Profile(FoldingSetNodeID &ID) const {
118 Profile(ID, Attrs.data(), Attrs.size());
120 static void Profile(FoldingSetNodeID &ID, const AttributeWithIndex *Attr,
122 for (unsigned i = 0; i != NumAttrs; ++i)
123 ID.AddInteger(uint64_t(Attr[i].Attrs) << 32 | unsigned(Attr[i].Index));
128 static ManagedStatic<sys::SmartMutex<true> > ALMutex;
129 static ManagedStatic<FoldingSet<AttributeListImpl> > AttributesLists;
131 AttributeListImpl::~AttributeListImpl() {
132 sys::SmartScopedLock<true> Lock(*ALMutex);
133 AttributesLists->RemoveNode(this);
137 AttrListPtr AttrListPtr::get(const AttributeWithIndex *Attrs, unsigned NumAttrs) {
138 // If there are no attributes then return a null AttributesList pointer.
140 return AttrListPtr();
143 for (unsigned i = 0; i != NumAttrs; ++i) {
144 assert(Attrs[i].Attrs != Attribute::None &&
145 "Pointless attribute!");
146 assert((!i || Attrs[i-1].Index < Attrs[i].Index) &&
147 "Misordered AttributesList!");
151 // Otherwise, build a key to look up the existing attributes.
153 AttributeListImpl::Profile(ID, Attrs, NumAttrs);
156 sys::SmartScopedLock<true> Lock(*ALMutex);
158 AttributeListImpl *PAL =
159 AttributesLists->FindNodeOrInsertPos(ID, InsertPos);
161 // If we didn't find any existing attributes of the same shape then
162 // create a new one and insert it.
164 PAL = new AttributeListImpl(Attrs, NumAttrs);
165 AttributesLists->InsertNode(PAL, InsertPos);
168 // Return the AttributesList that we found or created.
169 return AttrListPtr(PAL);
173 //===----------------------------------------------------------------------===//
174 // AttrListPtr Method Implementations
175 //===----------------------------------------------------------------------===//
177 AttrListPtr::AttrListPtr(AttributeListImpl *LI) : AttrList(LI) {
178 if (LI) LI->AddRef();
181 AttrListPtr::AttrListPtr(const AttrListPtr &P) : AttrList(P.AttrList) {
182 if (AttrList) AttrList->AddRef();
185 const AttrListPtr &AttrListPtr::operator=(const AttrListPtr &RHS) {
186 if (AttrList == RHS.AttrList) return *this;
187 if (AttrList) AttrList->DropRef();
188 AttrList = RHS.AttrList;
189 if (AttrList) AttrList->AddRef();
193 AttrListPtr::~AttrListPtr() {
194 if (AttrList) AttrList->DropRef();
197 /// getNumSlots - Return the number of slots used in this attribute list.
198 /// This is the number of arguments that have an attribute set on them
199 /// (including the function itself).
200 unsigned AttrListPtr::getNumSlots() const {
201 return AttrList ? AttrList->Attrs.size() : 0;
204 /// getSlot - Return the AttributeWithIndex at the specified slot. This
205 /// holds a number plus a set of attributes.
206 const AttributeWithIndex &AttrListPtr::getSlot(unsigned Slot) const {
207 assert(AttrList && Slot < AttrList->Attrs.size() && "Slot # out of range!");
208 return AttrList->Attrs[Slot];
212 /// getAttributes - The attributes for the specified index are
213 /// returned. Attributes for the result are denoted with Idx = 0.
214 /// Function notes are denoted with idx = ~0.
215 Attributes AttrListPtr::getAttributes(unsigned Idx) const {
216 if (AttrList == 0) return Attribute::None;
218 const SmallVector<AttributeWithIndex, 4> &Attrs = AttrList->Attrs;
219 for (unsigned i = 0, e = Attrs.size(); i != e && Attrs[i].Index <= Idx; ++i)
220 if (Attrs[i].Index == Idx)
221 return Attrs[i].Attrs;
222 return Attribute::None;
225 /// hasAttrSomewhere - Return true if the specified attribute is set for at
226 /// least one parameter or for the return value.
227 bool AttrListPtr::hasAttrSomewhere(Attributes Attr) const {
228 if (AttrList == 0) return false;
230 const SmallVector<AttributeWithIndex, 4> &Attrs = AttrList->Attrs;
231 for (unsigned i = 0, e = Attrs.size(); i != e; ++i)
232 if (Attrs[i].Attrs & Attr)
238 AttrListPtr AttrListPtr::addAttr(unsigned Idx, Attributes Attrs) const {
239 Attributes OldAttrs = getAttributes(Idx);
241 // FIXME it is not obvious how this should work for alignment.
242 // For now, say we can't change a known alignment.
243 Attributes OldAlign = OldAttrs & Attribute::Alignment;
244 Attributes NewAlign = Attrs & Attribute::Alignment;
245 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
246 "Attempt to change alignment!");
249 Attributes NewAttrs = OldAttrs | Attrs;
250 if (NewAttrs == OldAttrs)
253 SmallVector<AttributeWithIndex, 8> NewAttrList;
255 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
257 const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs;
258 unsigned i = 0, e = OldAttrList.size();
259 // Copy attributes for arguments before this one.
260 for (; i != e && OldAttrList[i].Index < Idx; ++i)
261 NewAttrList.push_back(OldAttrList[i]);
263 // If there are attributes already at this index, merge them in.
264 if (i != e && OldAttrList[i].Index == Idx) {
265 Attrs |= OldAttrList[i].Attrs;
269 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
271 // Copy attributes for arguments after this one.
272 NewAttrList.insert(NewAttrList.end(),
273 OldAttrList.begin()+i, OldAttrList.end());
276 return get(NewAttrList.data(), NewAttrList.size());
279 AttrListPtr AttrListPtr::removeAttr(unsigned Idx, Attributes Attrs) const {
281 // FIXME it is not obvious how this should work for alignment.
282 // For now, say we can't pass in alignment, which no current use does.
283 assert(!(Attrs & Attribute::Alignment) && "Attempt to exclude alignment!");
285 if (AttrList == 0) return AttrListPtr();
287 Attributes OldAttrs = getAttributes(Idx);
288 Attributes NewAttrs = OldAttrs & ~Attrs;
289 if (NewAttrs == OldAttrs)
292 SmallVector<AttributeWithIndex, 8> NewAttrList;
293 const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs;
294 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 assert(OldAttrList[i].Index == Idx && "Attribute isn't set?");
302 Attrs = OldAttrList[i].Attrs & ~Attrs;
304 if (Attrs) // If any attributes left for this parameter, add them.
305 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
307 // Copy attributes for arguments after this one.
308 NewAttrList.insert(NewAttrList.end(),
309 OldAttrList.begin()+i, OldAttrList.end());
311 return get(NewAttrList.data(), NewAttrList.size());
314 void AttrListPtr::dump() const {
316 for (unsigned i = 0; i < getNumSlots(); ++i) {
317 const AttributeWithIndex &PAWI = getSlot(i);
318 cerr << "{" << PAWI.Index << "," << PAWI.Attrs << "} ";