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