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