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::StructRet)
42 if (Attrs & Attribute::ByVal)
44 if (Attrs & Attribute::Nest)
46 if (Attrs & Attribute::ReadNone)
47 Result += "readnone ";
48 if (Attrs & Attribute::ReadOnly)
49 Result += "readonly ";
50 if (Attrs & Attribute::Alignment) {
52 Result += utostr((Attrs & Attribute::Alignment) >> 16);
55 // Trim the trailing space.
56 Result.erase(Result.end()-1);
60 Attributes Attribute::typeIncompatible(const Type *Ty) {
61 Attributes Incompatible = None;
64 // Attributes that only apply to integers.
65 Incompatible |= SExt | ZExt;
67 if (!isa<PointerType>(Ty))
68 // Attributes that only apply to pointers.
69 Incompatible |= ByVal | Nest | NoAlias | StructRet;
74 //===----------------------------------------------------------------------===//
75 // AttributeListImpl Definition
76 //===----------------------------------------------------------------------===//
79 class AttributeListImpl : public FoldingSetNode {
82 // AttributesList is uniqued, these should not be publicly available.
83 void operator=(const AttributeListImpl &); // Do not implement
84 AttributeListImpl(const AttributeListImpl &); // Do not implement
85 ~AttributeListImpl(); // Private implementation
87 SmallVector<AttributeWithIndex, 4> Attrs;
89 AttributeListImpl(const AttributeWithIndex *Attr, unsigned NumAttrs)
90 : Attrs(Attr, Attr+NumAttrs) {
94 void AddRef() { ++RefCount; }
95 void DropRef() { if (--RefCount == 0) delete this; }
97 void Profile(FoldingSetNodeID &ID) const {
98 Profile(ID, &Attrs[0], Attrs.size());
100 static void Profile(FoldingSetNodeID &ID, const AttributeWithIndex *Attr,
102 for (unsigned i = 0; i != NumAttrs; ++i)
103 ID.AddInteger(uint64_t(Attr[i].Attrs) << 32 | unsigned(Attr[i].Index));
108 static ManagedStatic<FoldingSet<AttributeListImpl> > AttributesLists;
110 AttributeListImpl::~AttributeListImpl() {
111 AttributesLists->RemoveNode(this);
115 AttrListPtr AttrListPtr::get(const AttributeWithIndex *Attrs, unsigned NumAttrs) {
116 // If there are no attributes then return a null AttributesList pointer.
118 return AttrListPtr();
121 for (unsigned i = 0; i != NumAttrs; ++i) {
122 assert(Attrs[i].Attrs != Attribute::None &&
123 "Pointless attribute!");
124 assert((!i || Attrs[i-1].Index < Attrs[i].Index) &&
125 "Misordered AttributesList!");
129 // Otherwise, build a key to look up the existing attributes.
131 AttributeListImpl::Profile(ID, Attrs, NumAttrs);
133 AttributeListImpl *PAL =
134 AttributesLists->FindNodeOrInsertPos(ID, InsertPos);
136 // If we didn't find any existing attributes of the same shape then
137 // create a new one and insert it.
139 PAL = new AttributeListImpl(Attrs, NumAttrs);
140 AttributesLists->InsertNode(PAL, InsertPos);
143 // Return the AttributesList that we found or created.
144 return AttrListPtr(PAL);
148 //===----------------------------------------------------------------------===//
149 // AttrListPtr Method Implementations
150 //===----------------------------------------------------------------------===//
152 AttrListPtr::AttrListPtr(AttributeListImpl *LI) : AttrList(LI) {
153 if (LI) LI->AddRef();
156 AttrListPtr::AttrListPtr(const AttrListPtr &P) : AttrList(P.AttrList) {
157 if (AttrList) AttrList->AddRef();
160 const AttrListPtr &AttrListPtr::operator=(const AttrListPtr &RHS) {
161 if (AttrList == RHS.AttrList) return *this;
162 if (AttrList) AttrList->DropRef();
163 AttrList = RHS.AttrList;
164 if (AttrList) AttrList->AddRef();
168 AttrListPtr::~AttrListPtr() {
169 if (AttrList) AttrList->DropRef();
172 /// getNumSlots - Return the number of slots used in this attribute list.
173 /// This is the number of arguments that have an attribute set on them
174 /// (including the function itself).
175 unsigned AttrListPtr::getNumSlots() const {
176 return AttrList ? AttrList->Attrs.size() : 0;
179 /// getSlot - Return the AttributeWithIndex at the specified slot. This
180 /// holds a number plus a set of attributes.
181 const AttributeWithIndex &AttrListPtr::getSlot(unsigned Slot) const {
182 assert(AttrList && Slot < AttrList->Attrs.size() && "Slot # out of range!");
183 return AttrList->Attrs[Slot];
187 /// getAttributes - The attributes for the specified index are
188 /// returned. Attributes for the result are denoted with Idx = 0.
189 /// Function notes are denoted with idx = ~0.
190 Attributes AttrListPtr::getAttributes(unsigned Idx) const {
191 if (AttrList == 0) return Attribute::None;
193 const SmallVector<AttributeWithIndex, 4> &Attrs = AttrList->Attrs;
194 for (unsigned i = 0, e = Attrs.size(); i != e && Attrs[i].Index <= Idx; ++i)
195 if (Attrs[i].Index == Idx)
196 return Attrs[i].Attrs;
197 return Attribute::None;
200 /// hasAttrSomewhere - Return true if the specified attribute is set for at
201 /// least one parameter or for the return value.
202 bool AttrListPtr::hasAttrSomewhere(Attributes Attr) const {
203 if (AttrList == 0) return false;
205 const SmallVector<AttributeWithIndex, 4> &Attrs = AttrList->Attrs;
206 for (unsigned i = 0, e = Attrs.size(); i != e; ++i)
207 if (Attrs[i].Attrs & Attr)
213 AttrListPtr AttrListPtr::addAttr(unsigned Idx, Attributes Attrs) const {
214 Attributes OldAttrs = getAttributes(Idx);
216 // FIXME it is not obvious how this should work for alignment.
217 // For now, say we can't change a known alignment.
218 Attributes OldAlign = OldAttrs & Attribute::Alignment;
219 Attributes NewAlign = Attrs & Attribute::Alignment;
220 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
221 "Attempt to change alignment!");
224 Attributes NewAttrs = OldAttrs | Attrs;
225 if (NewAttrs == OldAttrs)
228 SmallVector<AttributeWithIndex, 8> NewAttrList;
230 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
232 const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs;
233 unsigned i = 0, e = OldAttrList.size();
234 // Copy attributes for arguments before this one.
235 for (; i != e && OldAttrList[i].Index < Idx; ++i)
236 NewAttrList.push_back(OldAttrList[i]);
238 // If there are attributes already at this index, merge them in.
239 if (i != e && OldAttrList[i].Index == Idx) {
240 Attrs |= OldAttrList[i].Attrs;
244 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
246 // Copy attributes for arguments after this one.
247 NewAttrList.insert(NewAttrList.end(),
248 OldAttrList.begin()+i, OldAttrList.end());
251 return get(&NewAttrList[0], NewAttrList.size());
254 AttrListPtr AttrListPtr::removeAttr(unsigned Idx, Attributes Attrs) const {
256 // FIXME it is not obvious how this should work for alignment.
257 // For now, say we can't pass in alignment, which no current use does.
258 assert(!(Attrs & Attribute::Alignment) && "Attempt to exclude alignment!");
260 if (AttrList == 0) return AttrListPtr();
262 Attributes OldAttrs = getAttributes(Idx);
263 Attributes NewAttrs = OldAttrs & ~Attrs;
264 if (NewAttrs == OldAttrs)
267 SmallVector<AttributeWithIndex, 8> NewAttrList;
268 const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs;
269 unsigned i = 0, e = OldAttrList.size();
271 // Copy attributes for arguments before this one.
272 for (; i != e && OldAttrList[i].Index < Idx; ++i)
273 NewAttrList.push_back(OldAttrList[i]);
275 // If there are attributes already at this index, merge them in.
276 assert(OldAttrList[i].Index == Idx && "Attribute isn't set?");
277 Attrs = OldAttrList[i].Attrs & ~Attrs;
279 if (Attrs) // If any attributes left for this parameter, add them.
280 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
282 // Copy attributes for arguments after this one.
283 NewAttrList.insert(NewAttrList.end(),
284 OldAttrList.begin()+i, OldAttrList.end());
286 return get(&NewAttrList[0], NewAttrList.size());
289 void AttrListPtr::dump() const {
291 for (unsigned i = 0; i < getNumSlots(); ++i) {
292 const AttributeWithIndex &PAWI = getSlot(i);
293 cerr << "{" << PAWI.Index << "," << PAWI.Attrs << "} ";