Revert r131155 for now. It makes VMCore depend on Analysis and Transforms
[llvm/stm8.git] / lib / VMCore / Attributes.cpp
blob92152a3b90ae3f08a8c286bdf86d6c15ca61acdb
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::InReg)
40 Result += "inreg ";
41 if (Attrs & Attribute::NoAlias)
42 Result += "noalias ";
43 if (Attrs & Attribute::NoCapture)
44 Result += "nocapture ";
45 if (Attrs & Attribute::StructRet)
46 Result += "sret ";
47 if (Attrs & Attribute::ByVal)
48 Result += "byval ";
49 if (Attrs & Attribute::Nest)
50 Result += "nest ";
51 if (Attrs & Attribute::ReadNone)
52 Result += "readnone ";
53 if (Attrs & Attribute::ReadOnly)
54 Result += "readonly ";
55 if (Attrs & Attribute::OptimizeForSize)
56 Result += "optsize ";
57 if (Attrs & Attribute::NoInline)
58 Result += "noinline ";
59 if (Attrs & Attribute::InlineHint)
60 Result += "inlinehint ";
61 if (Attrs & Attribute::AlwaysInline)
62 Result += "alwaysinline ";
63 if (Attrs & Attribute::StackProtect)
64 Result += "ssp ";
65 if (Attrs & Attribute::StackProtectReq)
66 Result += "sspreq ";
67 if (Attrs & Attribute::NoRedZone)
68 Result += "noredzone ";
69 if (Attrs & Attribute::NoImplicitFloat)
70 Result += "noimplicitfloat ";
71 if (Attrs & Attribute::Naked)
72 Result += "naked ";
73 if (Attrs & Attribute::Hotpatch)
74 Result += "hotpatch ";
75 if (Attrs & Attribute::StackAlignment) {
76 Result += "alignstack(";
77 Result += utostr(Attribute::getStackAlignmentFromAttrs(Attrs));
78 Result += ") ";
80 if (Attrs & Attribute::Alignment) {
81 Result += "align ";
82 Result += utostr(Attribute::getAlignmentFromAttrs(Attrs));
83 Result += " ";
85 // Trim the trailing space.
86 assert(!Result.empty() && "Unknown attribute!");
87 Result.erase(Result.end()-1);
88 return Result;
91 Attributes Attribute::typeIncompatible(const Type *Ty) {
92 Attributes Incompatible = None;
94 if (!Ty->isIntegerTy())
95 // Attributes that only apply to integers.
96 Incompatible |= SExt | ZExt;
98 if (!Ty->isPointerTy())
99 // Attributes that only apply to pointers.
100 Incompatible |= ByVal | Nest | NoAlias | StructRet | NoCapture;
102 return Incompatible;
105 //===----------------------------------------------------------------------===//
106 // AttributeListImpl Definition
107 //===----------------------------------------------------------------------===//
109 namespace llvm {
110 class AttributeListImpl;
113 static ManagedStatic<FoldingSet<AttributeListImpl> > AttributesLists;
115 namespace llvm {
116 static ManagedStatic<sys::SmartMutex<true> > ALMutex;
118 class AttributeListImpl : public FoldingSetNode {
119 sys::cas_flag RefCount;
121 // AttributesList is uniqued, these should not be publicly available.
122 void operator=(const AttributeListImpl &); // Do not implement
123 AttributeListImpl(const AttributeListImpl &); // Do not implement
124 ~AttributeListImpl(); // Private implementation
125 public:
126 SmallVector<AttributeWithIndex, 4> Attrs;
128 AttributeListImpl(const AttributeWithIndex *Attr, unsigned NumAttrs)
129 : Attrs(Attr, Attr+NumAttrs) {
130 RefCount = 0;
133 void AddRef() {
134 sys::SmartScopedLock<true> Lock(*ALMutex);
135 ++RefCount;
137 void DropRef() {
138 sys::SmartScopedLock<true> Lock(*ALMutex);
139 if (!AttributesLists.isConstructed())
140 return;
141 sys::cas_flag new_val = --RefCount;
142 if (new_val == 0)
143 delete this;
146 void Profile(FoldingSetNodeID &ID) const {
147 Profile(ID, Attrs.data(), Attrs.size());
149 static void Profile(FoldingSetNodeID &ID, const AttributeWithIndex *Attr,
150 unsigned NumAttrs) {
151 for (unsigned i = 0; i != NumAttrs; ++i)
152 ID.AddInteger(uint64_t(Attr[i].Attrs) << 32 | unsigned(Attr[i].Index));
157 AttributeListImpl::~AttributeListImpl() {
158 // NOTE: Lock must be acquired by caller.
159 AttributesLists->RemoveNode(this);
163 AttrListPtr AttrListPtr::get(const AttributeWithIndex *Attrs, unsigned NumAttrs) {
164 // If there are no attributes then return a null AttributesList pointer.
165 if (NumAttrs == 0)
166 return AttrListPtr();
168 #ifndef NDEBUG
169 for (unsigned i = 0; i != NumAttrs; ++i) {
170 assert(Attrs[i].Attrs != Attribute::None &&
171 "Pointless attribute!");
172 assert((!i || Attrs[i-1].Index < Attrs[i].Index) &&
173 "Misordered AttributesList!");
175 #endif
177 // Otherwise, build a key to look up the existing attributes.
178 FoldingSetNodeID ID;
179 AttributeListImpl::Profile(ID, Attrs, NumAttrs);
180 void *InsertPos;
182 sys::SmartScopedLock<true> Lock(*ALMutex);
184 AttributeListImpl *PAL =
185 AttributesLists->FindNodeOrInsertPos(ID, InsertPos);
187 // If we didn't find any existing attributes of the same shape then
188 // create a new one and insert it.
189 if (!PAL) {
190 PAL = new AttributeListImpl(Attrs, NumAttrs);
191 AttributesLists->InsertNode(PAL, InsertPos);
194 // Return the AttributesList that we found or created.
195 return AttrListPtr(PAL);
199 //===----------------------------------------------------------------------===//
200 // AttrListPtr Method Implementations
201 //===----------------------------------------------------------------------===//
203 AttrListPtr::AttrListPtr(AttributeListImpl *LI) : AttrList(LI) {
204 if (LI) LI->AddRef();
207 AttrListPtr::AttrListPtr(const AttrListPtr &P) : AttrList(P.AttrList) {
208 if (AttrList) AttrList->AddRef();
211 const AttrListPtr &AttrListPtr::operator=(const AttrListPtr &RHS) {
212 sys::SmartScopedLock<true> Lock(*ALMutex);
213 if (AttrList == RHS.AttrList) return *this;
214 if (AttrList) AttrList->DropRef();
215 AttrList = RHS.AttrList;
216 if (AttrList) AttrList->AddRef();
217 return *this;
220 AttrListPtr::~AttrListPtr() {
221 if (AttrList) AttrList->DropRef();
224 /// getNumSlots - Return the number of slots used in this attribute list.
225 /// This is the number of arguments that have an attribute set on them
226 /// (including the function itself).
227 unsigned AttrListPtr::getNumSlots() const {
228 return AttrList ? AttrList->Attrs.size() : 0;
231 /// getSlot - Return the AttributeWithIndex at the specified slot. This
232 /// holds a number plus a set of attributes.
233 const AttributeWithIndex &AttrListPtr::getSlot(unsigned Slot) const {
234 assert(AttrList && Slot < AttrList->Attrs.size() && "Slot # out of range!");
235 return AttrList->Attrs[Slot];
239 /// getAttributes - The attributes for the specified index are
240 /// returned. Attributes for the result are denoted with Idx = 0.
241 /// Function notes are denoted with idx = ~0.
242 Attributes AttrListPtr::getAttributes(unsigned Idx) const {
243 if (AttrList == 0) return Attribute::None;
245 const SmallVector<AttributeWithIndex, 4> &Attrs = AttrList->Attrs;
246 for (unsigned i = 0, e = Attrs.size(); i != e && Attrs[i].Index <= Idx; ++i)
247 if (Attrs[i].Index == Idx)
248 return Attrs[i].Attrs;
249 return Attribute::None;
252 /// hasAttrSomewhere - Return true if the specified attribute is set for at
253 /// least one parameter or for the return value.
254 bool AttrListPtr::hasAttrSomewhere(Attributes Attr) const {
255 if (AttrList == 0) return false;
257 const SmallVector<AttributeWithIndex, 4> &Attrs = AttrList->Attrs;
258 for (unsigned i = 0, e = Attrs.size(); i != e; ++i)
259 if (Attrs[i].Attrs & Attr)
260 return true;
261 return false;
265 AttrListPtr AttrListPtr::addAttr(unsigned Idx, Attributes Attrs) const {
266 Attributes OldAttrs = getAttributes(Idx);
267 #ifndef NDEBUG
268 // FIXME it is not obvious how this should work for alignment.
269 // For now, say we can't change a known alignment.
270 Attributes OldAlign = OldAttrs & Attribute::Alignment;
271 Attributes NewAlign = Attrs & Attribute::Alignment;
272 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
273 "Attempt to change alignment!");
274 #endif
276 Attributes NewAttrs = OldAttrs | Attrs;
277 if (NewAttrs == OldAttrs)
278 return *this;
280 SmallVector<AttributeWithIndex, 8> NewAttrList;
281 if (AttrList == 0)
282 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
283 else {
284 const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs;
285 unsigned i = 0, e = OldAttrList.size();
286 // Copy attributes for arguments before this one.
287 for (; i != e && OldAttrList[i].Index < Idx; ++i)
288 NewAttrList.push_back(OldAttrList[i]);
290 // If there are attributes already at this index, merge them in.
291 if (i != e && OldAttrList[i].Index == Idx) {
292 Attrs |= OldAttrList[i].Attrs;
293 ++i;
296 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
298 // Copy attributes for arguments after this one.
299 NewAttrList.insert(NewAttrList.end(),
300 OldAttrList.begin()+i, OldAttrList.end());
303 return get(NewAttrList.data(), NewAttrList.size());
306 AttrListPtr AttrListPtr::removeAttr(unsigned Idx, Attributes Attrs) const {
307 #ifndef NDEBUG
308 // FIXME it is not obvious how this should work for alignment.
309 // For now, say we can't pass in alignment, which no current use does.
310 assert(!(Attrs & Attribute::Alignment) && "Attempt to exclude alignment!");
311 #endif
312 if (AttrList == 0) return AttrListPtr();
314 Attributes OldAttrs = getAttributes(Idx);
315 Attributes NewAttrs = OldAttrs & ~Attrs;
316 if (NewAttrs == OldAttrs)
317 return *this;
319 SmallVector<AttributeWithIndex, 8> NewAttrList;
320 const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs;
321 unsigned i = 0, e = OldAttrList.size();
323 // Copy attributes for arguments before this one.
324 for (; i != e && OldAttrList[i].Index < Idx; ++i)
325 NewAttrList.push_back(OldAttrList[i]);
327 // If there are attributes already at this index, merge them in.
328 assert(OldAttrList[i].Index == Idx && "Attribute isn't set?");
329 Attrs = OldAttrList[i].Attrs & ~Attrs;
330 ++i;
331 if (Attrs) // If any attributes left for this parameter, add them.
332 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
334 // Copy attributes for arguments after this one.
335 NewAttrList.insert(NewAttrList.end(),
336 OldAttrList.begin()+i, OldAttrList.end());
338 return get(NewAttrList.data(), NewAttrList.size());
341 void AttrListPtr::dump() const {
342 dbgs() << "PAL[ ";
343 for (unsigned i = 0; i < getNumSlots(); ++i) {
344 const AttributeWithIndex &PAWI = getSlot(i);
345 dbgs() << "{" << PAWI.Index << "," << PAWI.Attrs << "} ";
348 dbgs() << "]\n";