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