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