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/Streams.h"
19 #include "llvm/Support/ManagedStatic.h"
22 //===----------------------------------------------------------------------===//
23 // Attribute Function Definitions
24 //===----------------------------------------------------------------------===//
26 std::string Attribute::getAsString(Attributes Attrs) {
28 if (Attrs & Attribute::ZExt)
30 if (Attrs & Attribute::SExt)
32 if (Attrs & Attribute::NoReturn)
33 Result += "noreturn ";
34 if (Attrs & Attribute::NoUnwind)
35 Result += "nounwind ";
36 if (Attrs & Attribute::InReg)
38 if (Attrs & Attribute::NoAlias)
40 if (Attrs & Attribute::NoCapture)
41 Result += "nocapture ";
42 if (Attrs & Attribute::StructRet)
44 if (Attrs & Attribute::ByVal)
46 if (Attrs & Attribute::Nest)
48 if (Attrs & Attribute::ReadNone)
49 Result += "readnone ";
50 if (Attrs & Attribute::ReadOnly)
51 Result += "readonly ";
52 if (Attrs & Attribute::OptimizeForSize)
54 if (Attrs & Attribute::NoInline)
55 Result += "noinline ";
56 if (Attrs & Attribute::AlwaysInline)
57 Result += "alwaysinline ";
58 if (Attrs & Attribute::StackProtect)
60 if (Attrs & Attribute::StackProtectReq)
62 if (Attrs & Attribute::NoRedZone)
63 Result += "noredzone ";
64 if (Attrs & Attribute::NoImplicitFloat)
65 Result += "noimplicitfloat ";
66 if (Attrs & Attribute::Alignment) {
68 Result += utostr(Attribute::getAlignmentFromAttrs(Attrs));
71 // Trim the trailing space.
72 assert(!Result.empty() && "Unknown attribute!");
73 Result.erase(Result.end()-1);
77 Attributes Attribute::typeIncompatible(const Type *Ty) {
78 Attributes Incompatible = None;
81 // Attributes that only apply to integers.
82 Incompatible |= SExt | ZExt;
84 if (!isa<PointerType>(Ty))
85 // Attributes that only apply to pointers.
86 Incompatible |= ByVal | Nest | NoAlias | StructRet | NoCapture;
91 //===----------------------------------------------------------------------===//
92 // AttributeListImpl Definition
93 //===----------------------------------------------------------------------===//
96 class AttributeListImpl : public FoldingSetNode {
99 // AttributesList is uniqued, these should not be publicly available.
100 void operator=(const AttributeListImpl &); // Do not implement
101 AttributeListImpl(const AttributeListImpl &); // Do not implement
102 ~AttributeListImpl(); // Private implementation
104 SmallVector<AttributeWithIndex, 4> Attrs;
106 AttributeListImpl(const AttributeWithIndex *Attr, unsigned NumAttrs)
107 : Attrs(Attr, Attr+NumAttrs) {
111 void AddRef() { ++RefCount; }
112 void DropRef() { if (--RefCount == 0) delete this; }
114 void Profile(FoldingSetNodeID &ID) const {
115 Profile(ID, Attrs.data(), Attrs.size());
117 static void Profile(FoldingSetNodeID &ID, const AttributeWithIndex *Attr,
119 for (unsigned i = 0; i != NumAttrs; ++i)
120 ID.AddInteger(uint64_t(Attr[i].Attrs) << 32 | unsigned(Attr[i].Index));
125 static ManagedStatic<FoldingSet<AttributeListImpl> > AttributesLists;
127 AttributeListImpl::~AttributeListImpl() {
128 AttributesLists->RemoveNode(this);
132 AttrListPtr AttrListPtr::get(const AttributeWithIndex *Attrs, unsigned NumAttrs) {
133 // If there are no attributes then return a null AttributesList pointer.
135 return AttrListPtr();
138 for (unsigned i = 0; i != NumAttrs; ++i) {
139 assert(Attrs[i].Attrs != Attribute::None &&
140 "Pointless attribute!");
141 assert((!i || Attrs[i-1].Index < Attrs[i].Index) &&
142 "Misordered AttributesList!");
146 // Otherwise, build a key to look up the existing attributes.
148 AttributeListImpl::Profile(ID, Attrs, NumAttrs);
150 AttributeListImpl *PAL =
151 AttributesLists->FindNodeOrInsertPos(ID, InsertPos);
153 // If we didn't find any existing attributes of the same shape then
154 // create a new one and insert it.
156 PAL = new AttributeListImpl(Attrs, NumAttrs);
157 AttributesLists->InsertNode(PAL, InsertPos);
160 // Return the AttributesList that we found or created.
161 return AttrListPtr(PAL);
165 //===----------------------------------------------------------------------===//
166 // AttrListPtr Method Implementations
167 //===----------------------------------------------------------------------===//
169 AttrListPtr::AttrListPtr(AttributeListImpl *LI) : AttrList(LI) {
170 if (LI) LI->AddRef();
173 AttrListPtr::AttrListPtr(const AttrListPtr &P) : AttrList(P.AttrList) {
174 if (AttrList) AttrList->AddRef();
177 const AttrListPtr &AttrListPtr::operator=(const AttrListPtr &RHS) {
178 if (AttrList == RHS.AttrList) return *this;
179 if (AttrList) AttrList->DropRef();
180 AttrList = RHS.AttrList;
181 if (AttrList) AttrList->AddRef();
185 AttrListPtr::~AttrListPtr() {
186 if (AttrList) AttrList->DropRef();
189 /// getNumSlots - Return the number of slots used in this attribute list.
190 /// This is the number of arguments that have an attribute set on them
191 /// (including the function itself).
192 unsigned AttrListPtr::getNumSlots() const {
193 return AttrList ? AttrList->Attrs.size() : 0;
196 /// getSlot - Return the AttributeWithIndex at the specified slot. This
197 /// holds a number plus a set of attributes.
198 const AttributeWithIndex &AttrListPtr::getSlot(unsigned Slot) const {
199 assert(AttrList && Slot < AttrList->Attrs.size() && "Slot # out of range!");
200 return AttrList->Attrs[Slot];
204 /// getAttributes - The attributes for the specified index are
205 /// returned. Attributes for the result are denoted with Idx = 0.
206 /// Function notes are denoted with idx = ~0.
207 Attributes AttrListPtr::getAttributes(unsigned Idx) const {
208 if (AttrList == 0) return Attribute::None;
210 const SmallVector<AttributeWithIndex, 4> &Attrs = AttrList->Attrs;
211 for (unsigned i = 0, e = Attrs.size(); i != e && Attrs[i].Index <= Idx; ++i)
212 if (Attrs[i].Index == Idx)
213 return Attrs[i].Attrs;
214 return Attribute::None;
217 /// hasAttrSomewhere - Return true if the specified attribute is set for at
218 /// least one parameter or for the return value.
219 bool AttrListPtr::hasAttrSomewhere(Attributes Attr) const {
220 if (AttrList == 0) return false;
222 const SmallVector<AttributeWithIndex, 4> &Attrs = AttrList->Attrs;
223 for (unsigned i = 0, e = Attrs.size(); i != e; ++i)
224 if (Attrs[i].Attrs & Attr)
230 AttrListPtr AttrListPtr::addAttr(unsigned Idx, Attributes Attrs) const {
231 Attributes OldAttrs = getAttributes(Idx);
233 // FIXME it is not obvious how this should work for alignment.
234 // For now, say we can't change a known alignment.
235 Attributes OldAlign = OldAttrs & Attribute::Alignment;
236 Attributes NewAlign = Attrs & Attribute::Alignment;
237 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
238 "Attempt to change alignment!");
241 Attributes NewAttrs = OldAttrs | Attrs;
242 if (NewAttrs == OldAttrs)
245 SmallVector<AttributeWithIndex, 8> NewAttrList;
247 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
249 const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs;
250 unsigned i = 0, e = OldAttrList.size();
251 // Copy attributes for arguments before this one.
252 for (; i != e && OldAttrList[i].Index < Idx; ++i)
253 NewAttrList.push_back(OldAttrList[i]);
255 // If there are attributes already at this index, merge them in.
256 if (i != e && OldAttrList[i].Index == Idx) {
257 Attrs |= OldAttrList[i].Attrs;
261 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
263 // Copy attributes for arguments after this one.
264 NewAttrList.insert(NewAttrList.end(),
265 OldAttrList.begin()+i, OldAttrList.end());
268 return get(NewAttrList.data(), NewAttrList.size());
271 AttrListPtr AttrListPtr::removeAttr(unsigned Idx, Attributes Attrs) const {
273 // FIXME it is not obvious how this should work for alignment.
274 // For now, say we can't pass in alignment, which no current use does.
275 assert(!(Attrs & Attribute::Alignment) && "Attempt to exclude alignment!");
277 if (AttrList == 0) return AttrListPtr();
279 Attributes OldAttrs = getAttributes(Idx);
280 Attributes NewAttrs = OldAttrs & ~Attrs;
281 if (NewAttrs == OldAttrs)
284 SmallVector<AttributeWithIndex, 8> NewAttrList;
285 const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs;
286 unsigned i = 0, e = OldAttrList.size();
288 // Copy attributes for arguments before this one.
289 for (; i != e && OldAttrList[i].Index < Idx; ++i)
290 NewAttrList.push_back(OldAttrList[i]);
292 // If there are attributes already at this index, merge them in.
293 assert(OldAttrList[i].Index == Idx && "Attribute isn't set?");
294 Attrs = OldAttrList[i].Attrs & ~Attrs;
296 if (Attrs) // If any attributes left for this parameter, add them.
297 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
299 // Copy attributes for arguments after this one.
300 NewAttrList.insert(NewAttrList.end(),
301 OldAttrList.begin()+i, OldAttrList.end());
303 return get(NewAttrList.data(), NewAttrList.size());
306 void AttrListPtr::dump() const {
308 for (unsigned i = 0; i < getNumSlots(); ++i) {
309 const AttributeWithIndex &PAWI = getSlot(i);
310 cerr << "{" << PAWI.Index << "," << PAWI.Attrs << "} ";