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);
58 ParameterAttributes ParamAttr::typeIncompatible(const Type *Ty) {
59 ParameterAttributes Incompatible = None;
62 // Attributes that only apply to integers.
63 Incompatible |= SExt | ZExt;
65 if (!isa<PointerType>(Ty))
66 // Attributes that only apply to pointers.
67 Incompatible |= ByVal | Nest | NoAlias | StructRet;
72 //===----------------------------------------------------------------------===//
73 // ParamAttributeListImpl Definition
74 //===----------------------------------------------------------------------===//
77 class ParamAttributeListImpl : public FoldingSetNode {
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
85 SmallVector<ParamAttrsWithIndex, 4> Attrs;
87 ParamAttributeListImpl(const ParamAttrsWithIndex *Attr, unsigned NumAttrs)
88 : Attrs(Attr, Attr+NumAttrs) {
92 void AddRef() { ++RefCount; }
93 void DropRef() { if (--RefCount == 0) delete this; }
95 void Profile(FoldingSetNodeID &ID) const {
96 Profile(ID, &Attrs[0], Attrs.size());
98 static void Profile(FoldingSetNodeID &ID, const ParamAttrsWithIndex *Attr,
100 for (unsigned i = 0; i != NumAttrs; ++i)
101 ID.AddInteger(uint64_t(Attr[i].Attrs) << 32 | unsigned(Attr[i].Index));
106 static ManagedStatic<FoldingSet<ParamAttributeListImpl> > ParamAttrsLists;
108 ParamAttributeListImpl::~ParamAttributeListImpl() {
109 ParamAttrsLists->RemoveNode(this);
113 PAListPtr PAListPtr::get(const ParamAttrsWithIndex *Attrs, unsigned NumAttrs) {
114 // If there are no attributes then return a null ParamAttrsList pointer.
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!");
127 // Otherwise, build a key to look up the existing attributes.
129 ParamAttributeListImpl::Profile(ID, Attrs, NumAttrs);
131 ParamAttributeListImpl *PAL =
132 ParamAttrsLists->FindNodeOrInsertPos(ID, InsertPos);
134 // If we didn't find any existing attributes of the same shape then
135 // create a new one and insert it.
137 PAL = new ParamAttributeListImpl(Attrs, NumAttrs);
138 ParamAttrsLists->InsertNode(PAL, InsertPos);
141 // Return the ParamAttrsList that we found or created.
142 return PAListPtr(PAL);
146 //===----------------------------------------------------------------------===//
147 // PAListPtr Method Implementations
148 //===----------------------------------------------------------------------===//
150 PAListPtr::PAListPtr(ParamAttributeListImpl *LI) : PAList(LI) {
151 if (LI) LI->AddRef();
154 PAListPtr::PAListPtr(const PAListPtr &P) : PAList(P.PAList) {
155 if (PAList) PAList->AddRef();
158 const PAListPtr &PAListPtr::operator=(const PAListPtr &RHS) {
159 if (PAList == RHS.PAList) return *this;
160 if (PAList) PAList->DropRef();
162 if (PAList) PAList->AddRef();
166 PAListPtr::~PAListPtr() {
167 if (PAList) PAList->DropRef();
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;
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];
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;
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;
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;
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)
210 PAListPtr PAListPtr::addAttr(unsigned Idx, ParameterAttributes Attrs) const {
211 ParameterAttributes OldAttrs = getParamAttrs(Idx);
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!");
221 ParameterAttributes NewAttrs = OldAttrs | Attrs;
222 if (NewAttrs == OldAttrs)
225 SmallVector<ParamAttrsWithIndex, 8> NewAttrList;
227 NewAttrList.push_back(ParamAttrsWithIndex::get(Idx, Attrs));
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]);
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;
241 NewAttrList.push_back(ParamAttrsWithIndex::get(Idx, Attrs));
243 // Copy attributes for arguments after this one.
244 NewAttrList.insert(NewAttrList.end(),
245 OldAttrList.begin()+i, OldAttrList.end());
248 return get(&NewAttrList[0], NewAttrList.size());
251 PAListPtr PAListPtr::removeAttr(unsigned Idx, ParameterAttributes Attrs) const {
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!");
257 if (PAList == 0) return PAListPtr();
259 ParameterAttributes OldAttrs = getParamAttrs(Idx);
260 ParameterAttributes NewAttrs = OldAttrs & ~Attrs;
261 if (NewAttrs == OldAttrs)
264 SmallVector<ParamAttrsWithIndex, 8> NewAttrList;
265 const SmallVector<ParamAttrsWithIndex, 4> &OldAttrList = PAList->Attrs;
266 unsigned i = 0, e = OldAttrList.size();
268 // Copy attributes for arguments before this one.
269 for (; i != e && OldAttrList[i].Index < Idx; ++i)
270 NewAttrList.push_back(OldAttrList[i]);
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;
276 if (Attrs) // If any attributes left for this parameter, add them.
277 NewAttrList.push_back(ParamAttrsWithIndex::get(Idx, Attrs));
279 // Copy attributes for arguments after this one.
280 NewAttrList.insert(NewAttrList.end(),
281 OldAttrList.begin()+i, OldAttrList.end());
283 return get(&NewAttrList[0], NewAttrList.size());
286 void PAListPtr::dump() const {
288 for (unsigned i = 0; i < getNumSlots(); ++i) {
289 const ParamAttrsWithIndex &PAWI = getSlot(i);
290 cerr << "{" << PAWI.Index << "," << PAWI.Attrs << "} ";