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