Silence -Wunused-variable in release builds.
[llvm/stm8.git] / lib / VMCore / Attributes.cpp
blobbf6efa1645a2aa6deb1e8b33f55a51fea7f6c2c4
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.
12 //===----------------------------------------------------------------------===//
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/Atomic.h"
19 #include "llvm/Support/Mutex.h"
20 #include "llvm/Support/Debug.h"
21 #include "llvm/Support/ManagedStatic.h"
22 #include "llvm/Support/raw_ostream.h"
23 using namespace llvm;
25 //===----------------------------------------------------------------------===//
26 // Attribute Function Definitions
27 //===----------------------------------------------------------------------===//
29 std::string Attribute::getAsString(Attributes Attrs) {
30 std::string Result;
31 if (Attrs & Attribute::ZExt)
32 Result += "zeroext ";
33 if (Attrs & Attribute::SExt)
34 Result += "signext ";
35 if (Attrs & Attribute::NoReturn)
36 Result += "noreturn ";
37 if (Attrs & Attribute::NoUnwind)
38 Result += "nounwind ";
39 if (Attrs & Attribute::UWTable)
40 Result += "uwtable ";
41 if (Attrs & Attribute::InReg)
42 Result += "inreg ";
43 if (Attrs & Attribute::NoAlias)
44 Result += "noalias ";
45 if (Attrs & Attribute::NoCapture)
46 Result += "nocapture ";
47 if (Attrs & Attribute::StructRet)
48 Result += "sret ";
49 if (Attrs & Attribute::ByVal)
50 Result += "byval ";
51 if (Attrs & Attribute::Nest)
52 Result += "nest ";
53 if (Attrs & Attribute::ReadNone)
54 Result += "readnone ";
55 if (Attrs & Attribute::ReadOnly)
56 Result += "readonly ";
57 if (Attrs & Attribute::OptimizeForSize)
58 Result += "optsize ";
59 if (Attrs & Attribute::NoInline)
60 Result += "noinline ";
61 if (Attrs & Attribute::InlineHint)
62 Result += "inlinehint ";
63 if (Attrs & Attribute::AlwaysInline)
64 Result += "alwaysinline ";
65 if (Attrs & Attribute::StackProtect)
66 Result += "ssp ";
67 if (Attrs & Attribute::StackProtectReq)
68 Result += "sspreq ";
69 if (Attrs & Attribute::NoRedZone)
70 Result += "noredzone ";
71 if (Attrs & Attribute::NoImplicitFloat)
72 Result += "noimplicitfloat ";
73 if (Attrs & Attribute::Naked)
74 Result += "naked ";
75 if (Attrs & Attribute::Hotpatch)
76 Result += "hotpatch ";
77 if (Attrs & Attribute::NonLazyBind)
78 Result += "nonlazybind ";
79 if (Attrs & Attribute::StackAlignment) {
80 Result += "alignstack(";
81 Result += utostr(Attribute::getStackAlignmentFromAttrs(Attrs));
82 Result += ") ";
84 if (Attrs & Attribute::Alignment) {
85 Result += "align ";
86 Result += utostr(Attribute::getAlignmentFromAttrs(Attrs));
87 Result += " ";
89 // Trim the trailing space.
90 assert(!Result.empty() && "Unknown attribute!");
91 Result.erase(Result.end()-1);
92 return Result;
95 Attributes Attribute::typeIncompatible(const Type *Ty) {
96 Attributes Incompatible = None;
98 if (!Ty->isIntegerTy())
99 // Attributes that only apply to integers.
100 Incompatible |= SExt | ZExt;
102 if (!Ty->isPointerTy())
103 // Attributes that only apply to pointers.
104 Incompatible |= ByVal | Nest | NoAlias | StructRet | NoCapture;
106 return Incompatible;
109 //===----------------------------------------------------------------------===//
110 // AttributeListImpl Definition
111 //===----------------------------------------------------------------------===//
113 namespace llvm {
114 class AttributeListImpl;
117 static ManagedStatic<FoldingSet<AttributeListImpl> > AttributesLists;
119 namespace llvm {
120 static ManagedStatic<sys::SmartMutex<true> > ALMutex;
122 class AttributeListImpl : public FoldingSetNode {
123 sys::cas_flag RefCount;
125 // AttributesList is uniqued, these should not be publicly available.
126 void operator=(const AttributeListImpl &); // Do not implement
127 AttributeListImpl(const AttributeListImpl &); // Do not implement
128 ~AttributeListImpl(); // Private implementation
129 public:
130 SmallVector<AttributeWithIndex, 4> Attrs;
132 AttributeListImpl(const AttributeWithIndex *Attr, unsigned NumAttrs)
133 : Attrs(Attr, Attr+NumAttrs) {
134 RefCount = 0;
137 void AddRef() {
138 sys::SmartScopedLock<true> Lock(*ALMutex);
139 ++RefCount;
141 void DropRef() {
142 sys::SmartScopedLock<true> Lock(*ALMutex);
143 if (!AttributesLists.isConstructed())
144 return;
145 sys::cas_flag new_val = --RefCount;
146 if (new_val == 0)
147 delete this;
150 void Profile(FoldingSetNodeID &ID) const {
151 Profile(ID, Attrs.data(), Attrs.size());
153 static void Profile(FoldingSetNodeID &ID, const AttributeWithIndex *Attr,
154 unsigned NumAttrs) {
155 for (unsigned i = 0; i != NumAttrs; ++i)
156 ID.AddInteger(uint64_t(Attr[i].Attrs) << 32 | unsigned(Attr[i].Index));
161 AttributeListImpl::~AttributeListImpl() {
162 // NOTE: Lock must be acquired by caller.
163 AttributesLists->RemoveNode(this);
167 AttrListPtr AttrListPtr::get(const AttributeWithIndex *Attrs, unsigned NumAttrs) {
168 // If there are no attributes then return a null AttributesList pointer.
169 if (NumAttrs == 0)
170 return AttrListPtr();
172 #ifndef NDEBUG
173 for (unsigned i = 0; i != NumAttrs; ++i) {
174 assert(Attrs[i].Attrs != Attribute::None &&
175 "Pointless attribute!");
176 assert((!i || Attrs[i-1].Index < Attrs[i].Index) &&
177 "Misordered AttributesList!");
179 #endif
181 // Otherwise, build a key to look up the existing attributes.
182 FoldingSetNodeID ID;
183 AttributeListImpl::Profile(ID, Attrs, NumAttrs);
184 void *InsertPos;
186 sys::SmartScopedLock<true> Lock(*ALMutex);
188 AttributeListImpl *PAL =
189 AttributesLists->FindNodeOrInsertPos(ID, InsertPos);
191 // If we didn't find any existing attributes of the same shape then
192 // create a new one and insert it.
193 if (!PAL) {
194 PAL = new AttributeListImpl(Attrs, NumAttrs);
195 AttributesLists->InsertNode(PAL, InsertPos);
198 // Return the AttributesList that we found or created.
199 return AttrListPtr(PAL);
203 //===----------------------------------------------------------------------===//
204 // AttrListPtr Method Implementations
205 //===----------------------------------------------------------------------===//
207 AttrListPtr::AttrListPtr(AttributeListImpl *LI) : AttrList(LI) {
208 if (LI) LI->AddRef();
211 AttrListPtr::AttrListPtr(const AttrListPtr &P) : AttrList(P.AttrList) {
212 if (AttrList) AttrList->AddRef();
215 const AttrListPtr &AttrListPtr::operator=(const AttrListPtr &RHS) {
216 sys::SmartScopedLock<true> Lock(*ALMutex);
217 if (AttrList == RHS.AttrList) return *this;
218 if (AttrList) AttrList->DropRef();
219 AttrList = RHS.AttrList;
220 if (AttrList) AttrList->AddRef();
221 return *this;
224 AttrListPtr::~AttrListPtr() {
225 if (AttrList) AttrList->DropRef();
228 /// getNumSlots - Return the number of slots used in this attribute list.
229 /// This is the number of arguments that have an attribute set on them
230 /// (including the function itself).
231 unsigned AttrListPtr::getNumSlots() const {
232 return AttrList ? AttrList->Attrs.size() : 0;
235 /// getSlot - Return the AttributeWithIndex at the specified slot. This
236 /// holds a number plus a set of attributes.
237 const AttributeWithIndex &AttrListPtr::getSlot(unsigned Slot) const {
238 assert(AttrList && Slot < AttrList->Attrs.size() && "Slot # out of range!");
239 return AttrList->Attrs[Slot];
243 /// getAttributes - The attributes for the specified index are
244 /// returned. Attributes for the result are denoted with Idx = 0.
245 /// Function notes are denoted with idx = ~0.
246 Attributes AttrListPtr::getAttributes(unsigned Idx) const {
247 if (AttrList == 0) return Attribute::None;
249 const SmallVector<AttributeWithIndex, 4> &Attrs = AttrList->Attrs;
250 for (unsigned i = 0, e = Attrs.size(); i != e && Attrs[i].Index <= Idx; ++i)
251 if (Attrs[i].Index == Idx)
252 return Attrs[i].Attrs;
253 return Attribute::None;
256 /// hasAttrSomewhere - Return true if the specified attribute is set for at
257 /// least one parameter or for the return value.
258 bool AttrListPtr::hasAttrSomewhere(Attributes Attr) const {
259 if (AttrList == 0) return false;
261 const SmallVector<AttributeWithIndex, 4> &Attrs = AttrList->Attrs;
262 for (unsigned i = 0, e = Attrs.size(); i != e; ++i)
263 if (Attrs[i].Attrs & Attr)
264 return true;
265 return false;
269 AttrListPtr AttrListPtr::addAttr(unsigned Idx, Attributes Attrs) const {
270 Attributes OldAttrs = getAttributes(Idx);
271 #ifndef NDEBUG
272 // FIXME it is not obvious how this should work for alignment.
273 // For now, say we can't change a known alignment.
274 Attributes OldAlign = OldAttrs & Attribute::Alignment;
275 Attributes NewAlign = Attrs & Attribute::Alignment;
276 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
277 "Attempt to change alignment!");
278 #endif
280 Attributes NewAttrs = OldAttrs | Attrs;
281 if (NewAttrs == OldAttrs)
282 return *this;
284 SmallVector<AttributeWithIndex, 8> NewAttrList;
285 if (AttrList == 0)
286 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
287 else {
288 const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs;
289 unsigned i = 0, e = OldAttrList.size();
290 // Copy attributes for arguments before this one.
291 for (; i != e && OldAttrList[i].Index < Idx; ++i)
292 NewAttrList.push_back(OldAttrList[i]);
294 // If there are attributes already at this index, merge them in.
295 if (i != e && OldAttrList[i].Index == Idx) {
296 Attrs |= OldAttrList[i].Attrs;
297 ++i;
300 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
302 // Copy attributes for arguments after this one.
303 NewAttrList.insert(NewAttrList.end(),
304 OldAttrList.begin()+i, OldAttrList.end());
307 return get(NewAttrList.data(), NewAttrList.size());
310 AttrListPtr AttrListPtr::removeAttr(unsigned Idx, Attributes Attrs) const {
311 #ifndef NDEBUG
312 // FIXME it is not obvious how this should work for alignment.
313 // For now, say we can't pass in alignment, which no current use does.
314 assert(!(Attrs & Attribute::Alignment) && "Attempt to exclude alignment!");
315 #endif
316 if (AttrList == 0) return AttrListPtr();
318 Attributes OldAttrs = getAttributes(Idx);
319 Attributes NewAttrs = OldAttrs & ~Attrs;
320 if (NewAttrs == OldAttrs)
321 return *this;
323 SmallVector<AttributeWithIndex, 8> NewAttrList;
324 const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs;
325 unsigned i = 0, e = OldAttrList.size();
327 // Copy attributes for arguments before this one.
328 for (; i != e && OldAttrList[i].Index < Idx; ++i)
329 NewAttrList.push_back(OldAttrList[i]);
331 // If there are attributes already at this index, merge them in.
332 assert(OldAttrList[i].Index == Idx && "Attribute isn't set?");
333 Attrs = OldAttrList[i].Attrs & ~Attrs;
334 ++i;
335 if (Attrs) // If any attributes left for this parameter, add them.
336 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
338 // Copy attributes for arguments after this one.
339 NewAttrList.insert(NewAttrList.end(),
340 OldAttrList.begin()+i, OldAttrList.end());
342 return get(NewAttrList.data(), NewAttrList.size());
345 void AttrListPtr::dump() const {
346 dbgs() << "PAL[ ";
347 for (unsigned i = 0; i < getNumSlots(); ++i) {
348 const AttributeWithIndex &PAWI = getSlot(i);
349 dbgs() << "{" << PAWI.Index << "," << PAWI.Attrs << "} ";
352 dbgs() << "]\n";