Now Attributes are divided in three groups
[oota-llvm.git] / lib / VMCore / Attributes.cpp
1 //===-- Attributes.cpp - Implement AttributesList -------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the AttributesList class and Attribute utilities.
11 //
12 //===----------------------------------------------------------------------===//
13
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"
20 using namespace llvm;
21
22 //===----------------------------------------------------------------------===//
23 // Attribute Function Definitions
24 //===----------------------------------------------------------------------===//
25
26 std::string Attribute::getAsString(Attributes Attrs) {
27   std::string Result;
28   if (Attrs & Attribute::ZExt)
29     Result += "zeroext ";
30   if (Attrs & Attribute::SExt)
31     Result += "signext ";
32   if (Attrs & Attribute::NoReturn)
33     Result += "noreturn ";
34   if (Attrs & Attribute::NoUnwind)
35     Result += "nounwind ";
36   if (Attrs & Attribute::InReg)
37     Result += "inreg ";
38   if (Attrs & Attribute::NoAlias)
39     Result += "noalias ";
40   if (Attrs & Attribute::StructRet)
41     Result += "sret ";  
42   if (Attrs & Attribute::ByVal)
43     Result += "byval ";
44   if (Attrs & Attribute::Nest)
45     Result += "nest ";
46   if (Attrs & Attribute::ReadNone)
47     Result += "readnone ";
48   if (Attrs & Attribute::ReadOnly)
49     Result += "readonly ";
50   if (Attrs & Attribute::OptimizeForSize)
51     Result += "optsize ";
52   if (Attrs & Attribute::NoInline)
53     Result += "noinline ";
54   if (Attrs & Attribute::AlwaysInline)
55     Result += "alwaysinline ";
56   if (Attrs & Attribute::Alignment) {
57     Result += "align ";
58     Result += utostr((Attrs & Attribute::Alignment) >> 16);
59     Result += " ";
60   }
61   // Trim the trailing space.
62   Result.erase(Result.end()-1);
63   return Result;
64 }
65
66 Attributes Attribute::typeIncompatible(const Type *Ty) {
67   Attributes Incompatible = None;
68   
69   if (!Ty->isInteger())
70     // Attributes that only apply to integers.
71     Incompatible |= SExt | ZExt;
72   
73   if (!isa<PointerType>(Ty))
74     // Attributes that only apply to pointers.
75     Incompatible |= ByVal | Nest | NoAlias | StructRet;
76   
77   return Incompatible;
78 }
79
80 //===----------------------------------------------------------------------===//
81 // AttributeListImpl Definition
82 //===----------------------------------------------------------------------===//
83
84 namespace llvm {
85 class AttributeListImpl : public FoldingSetNode {
86   unsigned RefCount;
87   
88   // AttributesList is uniqued, these should not be publicly available.
89   void operator=(const AttributeListImpl &); // Do not implement
90   AttributeListImpl(const AttributeListImpl &); // Do not implement
91   ~AttributeListImpl();                        // Private implementation
92 public:
93   SmallVector<AttributeWithIndex, 4> Attrs;
94   
95   AttributeListImpl(const AttributeWithIndex *Attr, unsigned NumAttrs)
96     : Attrs(Attr, Attr+NumAttrs) {
97     RefCount = 0;
98   }
99   
100   void AddRef() { ++RefCount; }
101   void DropRef() { if (--RefCount == 0) delete this; }
102   
103   void Profile(FoldingSetNodeID &ID) const {
104     Profile(ID, &Attrs[0], Attrs.size());
105   }
106   static void Profile(FoldingSetNodeID &ID, const AttributeWithIndex *Attr,
107                       unsigned NumAttrs) {
108     for (unsigned i = 0; i != NumAttrs; ++i)
109       ID.AddInteger(uint64_t(Attr[i].Attrs) << 32 | unsigned(Attr[i].Index));
110   }
111 };
112 }
113
114 static ManagedStatic<FoldingSet<AttributeListImpl> > AttributesLists;
115
116 AttributeListImpl::~AttributeListImpl() {
117   AttributesLists->RemoveNode(this);
118 }
119
120
121 AttrListPtr AttrListPtr::get(const AttributeWithIndex *Attrs, unsigned NumAttrs) {
122   // If there are no attributes then return a null AttributesList pointer.
123   if (NumAttrs == 0)
124     return AttrListPtr();
125   
126 #ifndef NDEBUG
127   for (unsigned i = 0; i != NumAttrs; ++i) {
128     assert(Attrs[i].Attrs != Attribute::None && 
129            "Pointless attribute!");
130     assert((!i || Attrs[i-1].Index < Attrs[i].Index) &&
131            "Misordered AttributesList!");
132   }
133 #endif
134   
135   // Otherwise, build a key to look up the existing attributes.
136   FoldingSetNodeID ID;
137   AttributeListImpl::Profile(ID, Attrs, NumAttrs);
138   void *InsertPos;
139   AttributeListImpl *PAL =
140     AttributesLists->FindNodeOrInsertPos(ID, InsertPos);
141   
142   // If we didn't find any existing attributes of the same shape then
143   // create a new one and insert it.
144   if (!PAL) {
145     PAL = new AttributeListImpl(Attrs, NumAttrs);
146     AttributesLists->InsertNode(PAL, InsertPos);
147   }
148   
149   // Return the AttributesList that we found or created.
150   return AttrListPtr(PAL);
151 }
152
153
154 //===----------------------------------------------------------------------===//
155 // AttrListPtr Method Implementations
156 //===----------------------------------------------------------------------===//
157
158 AttrListPtr::AttrListPtr(AttributeListImpl *LI) : AttrList(LI) {
159   if (LI) LI->AddRef();
160 }
161
162 AttrListPtr::AttrListPtr(const AttrListPtr &P) : AttrList(P.AttrList) {
163   if (AttrList) AttrList->AddRef();  
164 }
165
166 const AttrListPtr &AttrListPtr::operator=(const AttrListPtr &RHS) {
167   if (AttrList == RHS.AttrList) return *this;
168   if (AttrList) AttrList->DropRef();
169   AttrList = RHS.AttrList;
170   if (AttrList) AttrList->AddRef();
171   return *this;
172 }
173
174 AttrListPtr::~AttrListPtr() {
175   if (AttrList) AttrList->DropRef();
176 }
177
178 /// getNumSlots - Return the number of slots used in this attribute list. 
179 /// This is the number of arguments that have an attribute set on them
180 /// (including the function itself).
181 unsigned AttrListPtr::getNumSlots() const {
182   return AttrList ? AttrList->Attrs.size() : 0;
183 }
184
185 /// getSlot - Return the AttributeWithIndex at the specified slot.  This
186 /// holds a number plus a set of attributes.
187 const AttributeWithIndex &AttrListPtr::getSlot(unsigned Slot) const {
188   assert(AttrList && Slot < AttrList->Attrs.size() && "Slot # out of range!");
189   return AttrList->Attrs[Slot];
190 }
191
192
193 /// getAttributes - The attributes for the specified index are
194 /// returned.  Attributes for the result are denoted with Idx = 0.
195 /// Function notes are denoted with idx = ~0.
196 Attributes AttrListPtr::getAttributes(unsigned Idx) const {
197   if (AttrList == 0) return Attribute::None;
198   
199   const SmallVector<AttributeWithIndex, 4> &Attrs = AttrList->Attrs;
200   for (unsigned i = 0, e = Attrs.size(); i != e && Attrs[i].Index <= Idx; ++i)
201     if (Attrs[i].Index == Idx)
202       return Attrs[i].Attrs;
203   return Attribute::None;
204 }
205
206 /// hasAttrSomewhere - Return true if the specified attribute is set for at
207 /// least one parameter or for the return value.
208 bool AttrListPtr::hasAttrSomewhere(Attributes Attr) const {
209   if (AttrList == 0) return false;
210   
211   const SmallVector<AttributeWithIndex, 4> &Attrs = AttrList->Attrs;
212   for (unsigned i = 0, e = Attrs.size(); i != e; ++i)
213     if (Attrs[i].Attrs & Attr)
214       return true;
215   return false;
216 }
217
218
219 AttrListPtr AttrListPtr::addAttr(unsigned Idx, Attributes Attrs) const {
220   Attributes OldAttrs = getAttributes(Idx);
221 #ifndef NDEBUG
222   // FIXME it is not obvious how this should work for alignment.
223   // For now, say we can't change a known alignment.
224   Attributes OldAlign = OldAttrs & Attribute::Alignment;
225   Attributes NewAlign = Attrs & Attribute::Alignment;
226   assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
227          "Attempt to change alignment!");
228 #endif
229   
230   Attributes NewAttrs = OldAttrs | Attrs;
231   if (NewAttrs == OldAttrs)
232     return *this;
233   
234   SmallVector<AttributeWithIndex, 8> NewAttrList;
235   if (AttrList == 0)
236     NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
237   else {
238     const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs;
239     unsigned i = 0, e = OldAttrList.size();
240     // Copy attributes for arguments before this one.
241     for (; i != e && OldAttrList[i].Index < Idx; ++i)
242       NewAttrList.push_back(OldAttrList[i]);
243
244     // If there are attributes already at this index, merge them in.
245     if (i != e && OldAttrList[i].Index == Idx) {
246       Attrs |= OldAttrList[i].Attrs;
247       ++i;
248     }
249     
250     NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
251     
252     // Copy attributes for arguments after this one.
253     NewAttrList.insert(NewAttrList.end(), 
254                        OldAttrList.begin()+i, OldAttrList.end());
255   }
256   
257   return get(&NewAttrList[0], NewAttrList.size());
258 }
259
260 AttrListPtr AttrListPtr::removeAttr(unsigned Idx, Attributes Attrs) const {
261 #ifndef NDEBUG
262   // FIXME it is not obvious how this should work for alignment.
263   // For now, say we can't pass in alignment, which no current use does.
264   assert(!(Attrs & Attribute::Alignment) && "Attempt to exclude alignment!");
265 #endif
266   if (AttrList == 0) return AttrListPtr();
267   
268   Attributes OldAttrs = getAttributes(Idx);
269   Attributes NewAttrs = OldAttrs & ~Attrs;
270   if (NewAttrs == OldAttrs)
271     return *this;
272
273   SmallVector<AttributeWithIndex, 8> NewAttrList;
274   const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs;
275   unsigned i = 0, e = OldAttrList.size();
276   
277   // Copy attributes for arguments before this one.
278   for (; i != e && OldAttrList[i].Index < Idx; ++i)
279     NewAttrList.push_back(OldAttrList[i]);
280   
281   // If there are attributes already at this index, merge them in.
282   assert(OldAttrList[i].Index == Idx && "Attribute isn't set?");
283   Attrs = OldAttrList[i].Attrs & ~Attrs;
284   ++i;
285   if (Attrs)  // If any attributes left for this parameter, add them.
286     NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
287   
288   // Copy attributes for arguments after this one.
289   NewAttrList.insert(NewAttrList.end(), 
290                      OldAttrList.begin()+i, OldAttrList.end());
291   
292   return get(&NewAttrList[0], NewAttrList.size());
293 }
294
295 void AttrListPtr::dump() const {
296   cerr << "PAL[ ";
297   for (unsigned i = 0; i < getNumSlots(); ++i) {
298     const AttributeWithIndex &PAWI = getSlot(i);
299     cerr << "{" << PAWI.Index << "," << PAWI.Attrs << "} ";
300   }
301   
302   cerr << "]\n";
303 }