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