blob: 7501a350878fa387085993b49dff5054b571a480 [file] [log] [blame]
Dale Johannesen09f410b2008-02-22 22:17:59 +00001//===-- ParameterAttributes.cpp - Implement ParamAttrsList ----------------===//
Chris Lattner3e13b8c2008-01-02 23:42:30 +00002//
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//
Duncan Sands404eb052008-01-06 18:27:01 +000010// This file implements the ParamAttrsList class and ParamAttr utilities.
Chris Lattner3e13b8c2008-01-02 23:42:30 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattner8a923e72008-03-12 17:45:29 +000014#include "llvm/ParameterAttributes.h"
15#include "llvm/Type.h"
Dan Gohmanfc429612008-03-10 23:55:07 +000016#include "llvm/ADT/StringExtras.h"
Chris Lattner8a923e72008-03-12 17:45:29 +000017#include "llvm/ADT/FoldingSet.h"
18#include "llvm/Support/Streams.h"
Chris Lattner3e13b8c2008-01-02 23:42:30 +000019#include "llvm/Support/ManagedStatic.h"
20using namespace llvm;
21
Chris Lattner8a923e72008-03-12 17:45:29 +000022//===----------------------------------------------------------------------===//
23// ParamAttr Function Definitions
24//===----------------------------------------------------------------------===//
Chris Lattnerd0e1f102008-01-03 00:10:22 +000025
Chris Lattner8a923e72008-03-12 17:45:29 +000026std::string ParamAttr::getAsString(ParameterAttributes Attrs) {
Chris Lattner3e13b8c2008-01-02 23:42:30 +000027 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 ";
Dale Johannesen11a555e2008-02-19 23:51:49 +000050 if (Attrs & ParamAttr::Alignment) {
Dale Johannesen11a555e2008-02-19 23:51:49 +000051 Result += "align ";
Dan Gohmanfc429612008-03-10 23:55:07 +000052 Result += utostr((Attrs & ParamAttr::Alignment) >> 16);
Dale Johannesen11a555e2008-02-19 23:51:49 +000053 Result += " ";
54 }
Chris Lattner3e13b8c2008-01-02 23:42:30 +000055 return Result;
56}
57
Chris Lattner8a923e72008-03-12 17:45:29 +000058ParameterAttributes 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;
Chris Lattner3e13b8c2008-01-02 23:42:30 +000070}
71
Chris Lattner8a923e72008-03-12 17:45:29 +000072//===----------------------------------------------------------------------===//
73// ParamAttributeListImpl Definition
74//===----------------------------------------------------------------------===//
Chris Lattner3e13b8c2008-01-02 23:42:30 +000075
Chris Lattner8a923e72008-03-12 17:45:29 +000076namespace llvm {
77class 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
84public:
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
106static ManagedStatic<FoldingSet<ParamAttributeListImpl> > ParamAttrsLists;
107
108ParamAttributeListImpl::~ParamAttributeListImpl() {
109 ParamAttrsLists->RemoveNode(this);
110}
111
112
113PAListPtr 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
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000118#ifndef NDEBUG
Chris Lattner8a923e72008-03-12 17:45:29 +0000119 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!");
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000124 }
125#endif
Chris Lattner8a923e72008-03-12 17:45:29 +0000126
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000127 // Otherwise, build a key to look up the existing attributes.
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000128 FoldingSetNodeID ID;
Chris Lattner8a923e72008-03-12 17:45:29 +0000129 ParamAttributeListImpl::Profile(ID, Attrs, NumAttrs);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000130 void *InsertPos;
Chris Lattner8a923e72008-03-12 17:45:29 +0000131 ParamAttributeListImpl *PAL =
132 ParamAttrsLists->FindNodeOrInsertPos(ID, InsertPos);
133
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000134 // If we didn't find any existing attributes of the same shape then
135 // create a new one and insert it.
136 if (!PAL) {
Chris Lattner8a923e72008-03-12 17:45:29 +0000137 PAL = new ParamAttributeListImpl(Attrs, NumAttrs);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000138 ParamAttrsLists->InsertNode(PAL, InsertPos);
139 }
Chris Lattner8a923e72008-03-12 17:45:29 +0000140
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000141 // Return the ParamAttrsList that we found or created.
Chris Lattner8a923e72008-03-12 17:45:29 +0000142 return PAListPtr(PAL);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000143}
144
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000145
Chris Lattner8a923e72008-03-12 17:45:29 +0000146//===----------------------------------------------------------------------===//
147// PAListPtr Method Implementations
148//===----------------------------------------------------------------------===//
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000149
Chris Lattner8a923e72008-03-12 17:45:29 +0000150PAListPtr::PAListPtr(ParamAttributeListImpl *LI) : PAList(LI) {
151 if (LI) LI->AddRef();
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000152}
153
Chris Lattner8a923e72008-03-12 17:45:29 +0000154PAListPtr::PAListPtr(const PAListPtr &P) : PAList(P.PAList) {
155 if (PAList) PAList->AddRef();
156}
157
158const 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
166PAListPtr::~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).
173unsigned 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.
179const 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.
187ParameterAttributes 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.
199bool 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
210PAListPtr PAListPtr::addAttr(unsigned Idx, ParameterAttributes Attrs) const {
211 ParameterAttributes OldAttrs = getParamAttrs(Idx);
Dale Johannesen11a555e2008-02-19 23:51:49 +0000212#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;
Chris Lattner8a923e72008-03-12 17:45:29 +0000216 ParameterAttributes NewAlign = Attrs & ParamAttr::Alignment;
Anton Korobeynikov18991d72008-02-20 12:07:57 +0000217 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
Dale Johannesen11a555e2008-02-19 23:51:49 +0000218 "Attempt to change alignment!");
219#endif
Chris Lattner8a923e72008-03-12 17:45:29 +0000220
221 ParameterAttributes NewAttrs = OldAttrs | Attrs;
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000222 if (NewAttrs == OldAttrs)
Chris Lattner8a923e72008-03-12 17:45:29 +0000223 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]);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000234
Chris Lattner8a923e72008-03-12 17:45:29 +0000235 // 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());
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000249}
250
Chris Lattner8a923e72008-03-12 17:45:29 +0000251PAListPtr PAListPtr::removeAttr(unsigned Idx, ParameterAttributes Attrs) const {
Dale Johannesen11a555e2008-02-19 23:51:49 +0000252#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.
Chris Lattner8a923e72008-03-12 17:45:29 +0000255 assert(!(Attrs & ParamAttr::Alignment) && "Attempt to exclude alignment!");
Dale Johannesen11a555e2008-02-19 23:51:49 +0000256#endif
Chris Lattner8a923e72008-03-12 17:45:29 +0000257 if (PAList == 0) return PAListPtr();
258
259 ParameterAttributes OldAttrs = getParamAttrs(Idx);
260 ParameterAttributes NewAttrs = OldAttrs & ~Attrs;
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000261 if (NewAttrs == OldAttrs)
Chris Lattner8a923e72008-03-12 17:45:29 +0000262 return *this;
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000263
Chris Lattner8a923e72008-03-12 17:45:29 +0000264 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());
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000284}
285
Chris Lattner8a923e72008-03-12 17:45:29 +0000286void 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";
Duncan Sands404eb052008-01-06 18:27:01 +0000294}