1 //===-- ParameterAttributes.cpp - Implement ParamAttrsList ----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the ParamAttrsList class and ParamAttr utilities.
12 //===----------------------------------------------------------------------===//
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"
22 //===----------------------------------------------------------------------===//
23 // ParamAttr Function Definitions
24 //===----------------------------------------------------------------------===//
26 std::string ParamAttr::getAsString(ParameterAttributes Attrs) {
28 if (Attrs & ParamAttr::ZExt)
30 if (Attrs & ParamAttr::SExt)
32 if (Attrs & ParamAttr::NoReturn)
33 Result += "noreturn ";
34 if (Attrs & ParamAttr::NoUnwind)
35 Result += "nounwind ";
36 if (Attrs & ParamAttr::InReg)
38 if (Attrs & ParamAttr::NoAlias)
40 if (Attrs & ParamAttr::StructRet)
42 if (Attrs & ParamAttr::ByVal)
44 if (Attrs & ParamAttr::Nest)
46 if (Attrs & ParamAttr::ReadNone)
47 Result += "readnone ";
48 if (Attrs & ParamAttr::ReadOnly)
49 Result += "readonly ";
50 if (Attrs & ParamAttr::Alignment) {
52 Result += utostr((Attrs & ParamAttr::Alignment) >> 16);
55 // Trim the trailing space.
56 Result.erase(Result.end()-1);
60 ParameterAttributes ParamAttr::typeIncompatible(const Type *Ty) {
61 ParameterAttributes Incompatible = None;
64 // Attributes that only apply to integers.
65 Incompatible |= SExt | ZExt;
67 if (!isa<PointerType>(Ty))
68 // Attributes that only apply to pointers.
69 Incompatible |= ByVal | Nest | NoAlias | StructRet;
74 //===----------------------------------------------------------------------===//
75 // ParamAttributeListImpl Definition
76 //===----------------------------------------------------------------------===//
79 class ParamAttributeListImpl : public FoldingSetNode {
82 // ParamAttrsList is uniqued, these should not be publicly available.
83 void operator=(const ParamAttributeListImpl &); // Do not implement
84 ParamAttributeListImpl(const ParamAttributeListImpl &); // Do not implement
85 ~ParamAttributeListImpl(); // Private implementation
87 SmallVector<ParamAttrsWithIndex, 4> Attrs;
89 ParamAttributeListImpl(const ParamAttrsWithIndex *Attr, unsigned NumAttrs)
90 : Attrs(Attr, Attr+NumAttrs) {
94 void AddRef() { ++RefCount; }
95 void DropRef() { if (--RefCount == 0) delete this; }
97 void Profile(FoldingSetNodeID &ID) const {
98 Profile(ID, &Attrs[0], Attrs.size());
100 static void Profile(FoldingSetNodeID &ID, const ParamAttrsWithIndex *Attr,
102 for (unsigned i = 0; i != NumAttrs; ++i)
103 ID.AddInteger(uint64_t(Attr[i].Attrs) << 32 | unsigned(Attr[i].Index));
108 static ManagedStatic<FoldingSet<ParamAttributeListImpl> > ParamAttrsLists;
110 ParamAttributeListImpl::~ParamAttributeListImpl() {
111 ParamAttrsLists->RemoveNode(this);
115 PAListPtr PAListPtr::get(const ParamAttrsWithIndex *Attrs, unsigned NumAttrs) {
116 // If there are no attributes then return a null ParamAttrsList pointer.
121 for (unsigned i = 0; i != NumAttrs; ++i) {
122 assert(Attrs[i].Attrs != ParamAttr::None &&
123 "Pointless parameter attribute!");
124 assert((!i || Attrs[i-1].Index < Attrs[i].Index) &&
125 "Misordered ParamAttrsList!");
129 // Otherwise, build a key to look up the existing attributes.
131 ParamAttributeListImpl::Profile(ID, Attrs, NumAttrs);
133 ParamAttributeListImpl *PAL =
134 ParamAttrsLists->FindNodeOrInsertPos(ID, InsertPos);
136 // If we didn't find any existing attributes of the same shape then
137 // create a new one and insert it.
139 PAL = new ParamAttributeListImpl(Attrs, NumAttrs);
140 ParamAttrsLists->InsertNode(PAL, InsertPos);
143 // Return the ParamAttrsList that we found or created.
144 return PAListPtr(PAL);
148 //===----------------------------------------------------------------------===//
149 // PAListPtr Method Implementations
150 //===----------------------------------------------------------------------===//
152 PAListPtr::PAListPtr(ParamAttributeListImpl *LI) : PAList(LI) {
153 if (LI) LI->AddRef();
156 PAListPtr::PAListPtr(const PAListPtr &P) : PAList(P.PAList) {
157 if (PAList) PAList->AddRef();
160 const PAListPtr &PAListPtr::operator=(const PAListPtr &RHS) {
161 if (PAList == RHS.PAList) return *this;
162 if (PAList) PAList->DropRef();
164 if (PAList) PAList->AddRef();
168 PAListPtr::~PAListPtr() {
169 if (PAList) PAList->DropRef();
172 /// getNumSlots - Return the number of slots used in this attribute list.
173 /// This is the number of arguments that have an attribute set on them
174 /// (including the function itself).
175 unsigned PAListPtr::getNumSlots() const {
176 return PAList ? PAList->Attrs.size() : 0;
179 /// getSlot - Return the ParamAttrsWithIndex at the specified slot. This
180 /// holds a parameter number plus a set of attributes.
181 const ParamAttrsWithIndex &PAListPtr::getSlot(unsigned Slot) const {
182 assert(PAList && Slot < PAList->Attrs.size() && "Slot # out of range!");
183 return PAList->Attrs[Slot];
187 /// getParamAttrs - The parameter attributes for the specified parameter are
188 /// returned. Parameters for the result are denoted with Idx = 0.
189 ParameterAttributes PAListPtr::getParamAttrs(unsigned Idx) const {
190 if (PAList == 0) return ParamAttr::None;
192 const SmallVector<ParamAttrsWithIndex, 4> &Attrs = PAList->Attrs;
193 for (unsigned i = 0, e = Attrs.size(); i != e && Attrs[i].Index <= Idx; ++i)
194 if (Attrs[i].Index == Idx)
195 return Attrs[i].Attrs;
196 return ParamAttr::None;
199 /// hasAttrSomewhere - Return true if the specified attribute is set for at
200 /// least one parameter or for the return value.
201 bool PAListPtr::hasAttrSomewhere(ParameterAttributes Attr) const {
202 if (PAList == 0) return false;
204 const SmallVector<ParamAttrsWithIndex, 4> &Attrs = PAList->Attrs;
205 for (unsigned i = 0, e = Attrs.size(); i != e; ++i)
206 if (Attrs[i].Attrs & Attr)
212 PAListPtr PAListPtr::addAttr(unsigned Idx, ParameterAttributes Attrs) const {
213 ParameterAttributes OldAttrs = getParamAttrs(Idx);
215 // FIXME it is not obvious how this should work for alignment.
216 // For now, say we can't change a known alignment.
217 ParameterAttributes OldAlign = OldAttrs & ParamAttr::Alignment;
218 ParameterAttributes NewAlign = Attrs & ParamAttr::Alignment;
219 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
220 "Attempt to change alignment!");
223 ParameterAttributes NewAttrs = OldAttrs | Attrs;
224 if (NewAttrs == OldAttrs)
227 SmallVector<ParamAttrsWithIndex, 8> NewAttrList;
229 NewAttrList.push_back(ParamAttrsWithIndex::get(Idx, Attrs));
231 const SmallVector<ParamAttrsWithIndex, 4> &OldAttrList = PAList->Attrs;
232 unsigned i = 0, e = OldAttrList.size();
233 // Copy attributes for arguments before this one.
234 for (; i != e && OldAttrList[i].Index < Idx; ++i)
235 NewAttrList.push_back(OldAttrList[i]);
237 // If there are attributes already at this index, merge them in.
238 if (i != e && OldAttrList[i].Index == Idx) {
239 Attrs |= OldAttrList[i].Attrs;
243 NewAttrList.push_back(ParamAttrsWithIndex::get(Idx, Attrs));
245 // Copy attributes for arguments after this one.
246 NewAttrList.insert(NewAttrList.end(),
247 OldAttrList.begin()+i, OldAttrList.end());
250 return get(&NewAttrList[0], NewAttrList.size());
253 PAListPtr PAListPtr::removeAttr(unsigned Idx, ParameterAttributes Attrs) const {
255 // FIXME it is not obvious how this should work for alignment.
256 // For now, say we can't pass in alignment, which no current use does.
257 assert(!(Attrs & ParamAttr::Alignment) && "Attempt to exclude alignment!");
259 if (PAList == 0) return PAListPtr();
261 ParameterAttributes OldAttrs = getParamAttrs(Idx);
262 ParameterAttributes NewAttrs = OldAttrs & ~Attrs;
263 if (NewAttrs == OldAttrs)
266 SmallVector<ParamAttrsWithIndex, 8> NewAttrList;
267 const SmallVector<ParamAttrsWithIndex, 4> &OldAttrList = PAList->Attrs;
268 unsigned i = 0, e = OldAttrList.size();
270 // Copy attributes for arguments before this one.
271 for (; i != e && OldAttrList[i].Index < Idx; ++i)
272 NewAttrList.push_back(OldAttrList[i]);
274 // If there are attributes already at this index, merge them in.
275 assert(OldAttrList[i].Index == Idx && "Attribute isn't set?");
276 Attrs = OldAttrList[i].Attrs & ~Attrs;
278 if (Attrs) // If any attributes left for this parameter, add them.
279 NewAttrList.push_back(ParamAttrsWithIndex::get(Idx, Attrs));
281 // Copy attributes for arguments after this one.
282 NewAttrList.insert(NewAttrList.end(),
283 OldAttrList.begin()+i, OldAttrList.end());
285 return get(&NewAttrList[0], NewAttrList.size());
288 void PAListPtr::dump() const {
290 for (unsigned i = 0; i < getNumSlots(); ++i) {
291 const ParamAttrsWithIndex &PAWI = getSlot(i);
292 cerr << "{" << PAWI.Index << "," << PAWI.Attrs << "} ";