c8219eb7877777ecd646e2c3529afc852a0372d6
[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/Atomic.h"
19 #include "llvm/Support/Mutex.h"
20 #include "llvm/Support/Debug.h"
21 #include "llvm/Support/ManagedStatic.h"
22 #include "llvm/Support/raw_ostream.h"
23 using namespace llvm;
24
25 //===----------------------------------------------------------------------===//
26 // Attribute Function Definitions
27 //===----------------------------------------------------------------------===//
28
29 std::string Attribute::getAsString(Attributes Attrs) {
30   std::string Result;
31   if (Attrs & Attribute::ZExt)
32     Result += "zeroext ";
33   if (Attrs & Attribute::SExt)
34     Result += "signext ";
35   if (Attrs & Attribute::NoReturn)
36     Result += "noreturn ";
37   if (Attrs & Attribute::NoUnwind)
38     Result += "nounwind ";
39   if (Attrs & Attribute::UWTable)
40     Result += "uwtable ";
41   if (Attrs & Attribute::ReturnsTwice)
42     Result += "returns_twice ";
43   if (Attrs & Attribute::InReg)
44     Result += "inreg ";
45   if (Attrs & Attribute::NoAlias)
46     Result += "noalias ";
47   if (Attrs & Attribute::NoCapture)
48     Result += "nocapture ";
49   if (Attrs & Attribute::StructRet)
50     Result += "sret ";
51   if (Attrs & Attribute::ByVal)
52     Result += "byval ";
53   if (Attrs & Attribute::Nest)
54     Result += "nest ";
55   if (Attrs & Attribute::ReadNone)
56     Result += "readnone ";
57   if (Attrs & Attribute::ReadOnly)
58     Result += "readonly ";
59   if (Attrs & Attribute::OptimizeForSize)
60     Result += "optsize ";
61   if (Attrs & Attribute::NoInline)
62     Result += "noinline ";
63   if (Attrs & Attribute::InlineHint)
64     Result += "inlinehint ";
65   if (Attrs & Attribute::AlwaysInline)
66     Result += "alwaysinline ";
67   if (Attrs & Attribute::StackProtect)
68     Result += "ssp ";
69   if (Attrs & Attribute::StackProtectReq)
70     Result += "sspreq ";
71   if (Attrs & Attribute::NoRedZone)
72     Result += "noredzone ";
73   if (Attrs & Attribute::NoImplicitFloat)
74     Result += "noimplicitfloat ";
75   if (Attrs & Attribute::Naked)
76     Result += "naked ";
77   if (Attrs & Attribute::NonLazyBind)
78     Result += "nonlazybind ";
79   if (Attrs & Attribute::AddressSafety)
80     Result += "address_safety ";
81   if (Attrs & Attribute::StackAlignment) {
82     Result += "alignstack(";
83     Result += utostr(Attribute::getStackAlignmentFromAttrs(Attrs));
84     Result += ") ";
85   }
86   if (Attrs & Attribute::Alignment) {
87     Result += "align ";
88     Result += utostr(Attribute::getAlignmentFromAttrs(Attrs));
89     Result += " ";
90   }
91   if (Attrs & Attribute::IANSDialect)
92     Result += "ia_nsdialect ";
93
94   // Trim the trailing space.
95   assert(!Result.empty() && "Unknown attribute!");
96   Result.erase(Result.end()-1);
97   return Result;
98 }
99
100 Attributes Attribute::typeIncompatible(Type *Ty) {
101   Attributes Incompatible = None;
102   
103   if (!Ty->isIntegerTy())
104     // Attributes that only apply to integers.
105     Incompatible |= SExt | ZExt;
106   
107   if (!Ty->isPointerTy())
108     // Attributes that only apply to pointers.
109     Incompatible |= ByVal | Nest | NoAlias | StructRet | NoCapture;
110   
111   return Incompatible;
112 }
113
114 //===----------------------------------------------------------------------===//
115 // AttributeListImpl Definition
116 //===----------------------------------------------------------------------===//
117
118 namespace llvm {
119   class AttributeListImpl;
120 }
121
122 static ManagedStatic<FoldingSet<AttributeListImpl> > AttributesLists;
123
124 namespace llvm {
125 static ManagedStatic<sys::SmartMutex<true> > ALMutex;
126
127 class AttributeListImpl : public FoldingSetNode {
128   sys::cas_flag RefCount;
129   
130   // AttributesList is uniqued, these should not be publicly available.
131   void operator=(const AttributeListImpl &); // Do not implement
132   AttributeListImpl(const AttributeListImpl &); // Do not implement
133   ~AttributeListImpl();                        // Private implementation
134 public:
135   SmallVector<AttributeWithIndex, 4> Attrs;
136   
137   AttributeListImpl(ArrayRef<AttributeWithIndex> attrs)
138     : Attrs(attrs.begin(), attrs.end()) {
139     RefCount = 0;
140   }
141   
142   void AddRef() {
143     sys::SmartScopedLock<true> Lock(*ALMutex);
144     ++RefCount;
145   }
146   void DropRef() {
147     sys::SmartScopedLock<true> Lock(*ALMutex);
148     if (!AttributesLists.isConstructed())
149       return;
150     sys::cas_flag new_val = --RefCount;
151     if (new_val == 0)
152       delete this;
153   }
154   
155   void Profile(FoldingSetNodeID &ID) const {
156     Profile(ID, Attrs);
157   }
158   static void Profile(FoldingSetNodeID &ID, ArrayRef<AttributeWithIndex> Attrs){
159     for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
160       ID.AddInteger(Attrs[i].Attrs.Raw());
161       ID.AddInteger(Attrs[i].Index);
162     }
163   }
164 };
165 }
166
167 AttributeListImpl::~AttributeListImpl() {
168   // NOTE: Lock must be acquired by caller.
169   AttributesLists->RemoveNode(this);
170 }
171
172
173 AttrListPtr AttrListPtr::get(ArrayRef<AttributeWithIndex> Attrs) {
174   // If there are no attributes then return a null AttributesList pointer.
175   if (Attrs.empty())
176     return AttrListPtr();
177   
178 #ifndef NDEBUG
179   for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
180     assert(Attrs[i].Attrs != Attribute::None && 
181            "Pointless attribute!");
182     assert((!i || Attrs[i-1].Index < Attrs[i].Index) &&
183            "Misordered AttributesList!");
184   }
185 #endif
186   
187   // Otherwise, build a key to look up the existing attributes.
188   FoldingSetNodeID ID;
189   AttributeListImpl::Profile(ID, Attrs);
190   void *InsertPos;
191   
192   sys::SmartScopedLock<true> Lock(*ALMutex);
193   
194   AttributeListImpl *PAL =
195     AttributesLists->FindNodeOrInsertPos(ID, InsertPos);
196   
197   // If we didn't find any existing attributes of the same shape then
198   // create a new one and insert it.
199   if (!PAL) {
200     PAL = new AttributeListImpl(Attrs);
201     AttributesLists->InsertNode(PAL, InsertPos);
202   }
203   
204   // Return the AttributesList that we found or created.
205   return AttrListPtr(PAL);
206 }
207
208
209 //===----------------------------------------------------------------------===//
210 // AttrListPtr Method Implementations
211 //===----------------------------------------------------------------------===//
212
213 AttrListPtr::AttrListPtr(AttributeListImpl *LI) : AttrList(LI) {
214   if (LI) LI->AddRef();
215 }
216
217 AttrListPtr::AttrListPtr(const AttrListPtr &P) : AttrList(P.AttrList) {
218   if (AttrList) AttrList->AddRef();  
219 }
220
221 const AttrListPtr &AttrListPtr::operator=(const AttrListPtr &RHS) {
222   sys::SmartScopedLock<true> Lock(*ALMutex);
223   if (AttrList == RHS.AttrList) return *this;
224   if (AttrList) AttrList->DropRef();
225   AttrList = RHS.AttrList;
226   if (AttrList) AttrList->AddRef();
227   return *this;
228 }
229
230 AttrListPtr::~AttrListPtr() {
231   if (AttrList) AttrList->DropRef();
232 }
233
234 /// getNumSlots - Return the number of slots used in this attribute list. 
235 /// This is the number of arguments that have an attribute set on them
236 /// (including the function itself).
237 unsigned AttrListPtr::getNumSlots() const {
238   return AttrList ? AttrList->Attrs.size() : 0;
239 }
240
241 /// getSlot - Return the AttributeWithIndex at the specified slot.  This
242 /// holds a number plus a set of attributes.
243 const AttributeWithIndex &AttrListPtr::getSlot(unsigned Slot) const {
244   assert(AttrList && Slot < AttrList->Attrs.size() && "Slot # out of range!");
245   return AttrList->Attrs[Slot];
246 }
247
248
249 /// getAttributes - The attributes for the specified index are
250 /// returned.  Attributes for the result are denoted with Idx = 0.
251 /// Function notes are denoted with idx = ~0.
252 Attributes AttrListPtr::getAttributes(unsigned Idx) const {
253   if (AttrList == 0) return Attribute::None;
254   
255   const SmallVector<AttributeWithIndex, 4> &Attrs = AttrList->Attrs;
256   for (unsigned i = 0, e = Attrs.size(); i != e && Attrs[i].Index <= Idx; ++i)
257     if (Attrs[i].Index == Idx)
258       return Attrs[i].Attrs;
259   return Attribute::None;
260 }
261
262 /// hasAttrSomewhere - Return true if the specified attribute is set for at
263 /// least one parameter or for the return value.
264 bool AttrListPtr::hasAttrSomewhere(Attributes Attr) const {
265   if (AttrList == 0) return false;
266   
267   const SmallVector<AttributeWithIndex, 4> &Attrs = AttrList->Attrs;
268   for (unsigned i = 0, e = Attrs.size(); i != e; ++i)
269     if (Attrs[i].Attrs & Attr)
270       return true;
271   return false;
272 }
273
274
275 AttrListPtr AttrListPtr::addAttr(unsigned Idx, Attributes Attrs) const {
276   Attributes OldAttrs = getAttributes(Idx);
277 #ifndef NDEBUG
278   // FIXME it is not obvious how this should work for alignment.
279   // For now, say we can't change a known alignment.
280   Attributes OldAlign = OldAttrs & Attribute::Alignment;
281   Attributes NewAlign = Attrs & Attribute::Alignment;
282   assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
283          "Attempt to change alignment!");
284 #endif
285   
286   Attributes NewAttrs = OldAttrs | Attrs;
287   if (NewAttrs == OldAttrs)
288     return *this;
289   
290   SmallVector<AttributeWithIndex, 8> NewAttrList;
291   if (AttrList == 0)
292     NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
293   else {
294     const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs;
295     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]);
299
300     // If there are attributes already at this index, merge them in.
301     if (i != e && OldAttrList[i].Index == Idx) {
302       Attrs |= OldAttrList[i].Attrs;
303       ++i;
304     }
305     
306     NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
307     
308     // Copy attributes for arguments after this one.
309     NewAttrList.insert(NewAttrList.end(), 
310                        OldAttrList.begin()+i, OldAttrList.end());
311   }
312   
313   return get(NewAttrList);
314 }
315
316 AttrListPtr AttrListPtr::removeAttr(unsigned Idx, Attributes Attrs) const {
317 #ifndef NDEBUG
318   // FIXME it is not obvious how this should work for alignment.
319   // For now, say we can't pass in alignment, which no current use does.
320   assert(!(Attrs & Attribute::Alignment) && "Attempt to exclude alignment!");
321 #endif
322   if (AttrList == 0) return AttrListPtr();
323   
324   Attributes OldAttrs = getAttributes(Idx);
325   Attributes NewAttrs = OldAttrs & ~Attrs;
326   if (NewAttrs == OldAttrs)
327     return *this;
328
329   SmallVector<AttributeWithIndex, 8> NewAttrList;
330   const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs;
331   unsigned i = 0, e = OldAttrList.size();
332   
333   // Copy attributes for arguments before this one.
334   for (; i != e && OldAttrList[i].Index < Idx; ++i)
335     NewAttrList.push_back(OldAttrList[i]);
336   
337   // If there are attributes already at this index, merge them in.
338   assert(OldAttrList[i].Index == Idx && "Attribute isn't set?");
339   Attrs = OldAttrList[i].Attrs & ~Attrs;
340   ++i;
341   if (Attrs)  // If any attributes left for this parameter, add them.
342     NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
343   
344   // Copy attributes for arguments after this one.
345   NewAttrList.insert(NewAttrList.end(), 
346                      OldAttrList.begin()+i, OldAttrList.end());
347   
348   return get(NewAttrList);
349 }
350
351 void AttrListPtr::dump() const {
352   dbgs() << "PAL[ ";
353   for (unsigned i = 0; i < getNumSlots(); ++i) {
354     const AttributeWithIndex &PAWI = getSlot(i);
355     dbgs() << "{" << PAWI.Index << "," << PAWI.Attrs << "} ";
356   }
357   
358   dbgs() << "]\n";
359 }