zpu: wip - add pass to convert registers to stack slots
[llvm/zpu.git] / lib / VMCore / Attributes.cpp
blob6f5ecd278d880102fab47387b07ce737574a6ba5
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/System/Atomic.h"
19 #include "llvm/System/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 : public FoldingSetNode {
111 sys::cas_flag RefCount;
113 // AttributesList is uniqued, these should not be publicly available.
114 void operator=(const AttributeListImpl &); // Do not implement
115 AttributeListImpl(const AttributeListImpl &); // Do not implement
116 ~AttributeListImpl(); // Private implementation
117 public:
118 SmallVector<AttributeWithIndex, 4> Attrs;
120 AttributeListImpl(const AttributeWithIndex *Attr, unsigned NumAttrs)
121 : Attrs(Attr, Attr+NumAttrs) {
122 RefCount = 0;
125 void AddRef() { sys::AtomicIncrement(&RefCount); }
126 void DropRef() {
127 sys::cas_flag old = sys::AtomicDecrement(&RefCount);
128 if (old == 0) delete this;
131 void Profile(FoldingSetNodeID &ID) const {
132 Profile(ID, Attrs.data(), Attrs.size());
134 static void Profile(FoldingSetNodeID &ID, const AttributeWithIndex *Attr,
135 unsigned NumAttrs) {
136 for (unsigned i = 0; i != NumAttrs; ++i)
137 ID.AddInteger(uint64_t(Attr[i].Attrs) << 32 | unsigned(Attr[i].Index));
142 static ManagedStatic<sys::SmartMutex<true> > ALMutex;
143 static ManagedStatic<FoldingSet<AttributeListImpl> > AttributesLists;
145 AttributeListImpl::~AttributeListImpl() {
146 sys::SmartScopedLock<true> Lock(*ALMutex);
147 AttributesLists->RemoveNode(this);
151 AttrListPtr AttrListPtr::get(const AttributeWithIndex *Attrs, unsigned NumAttrs) {
152 // If there are no attributes then return a null AttributesList pointer.
153 if (NumAttrs == 0)
154 return AttrListPtr();
156 #ifndef NDEBUG
157 for (unsigned i = 0; i != NumAttrs; ++i) {
158 assert(Attrs[i].Attrs != Attribute::None &&
159 "Pointless attribute!");
160 assert((!i || Attrs[i-1].Index < Attrs[i].Index) &&
161 "Misordered AttributesList!");
163 #endif
165 // Otherwise, build a key to look up the existing attributes.
166 FoldingSetNodeID ID;
167 AttributeListImpl::Profile(ID, Attrs, NumAttrs);
168 void *InsertPos;
170 sys::SmartScopedLock<true> Lock(*ALMutex);
172 AttributeListImpl *PAL =
173 AttributesLists->FindNodeOrInsertPos(ID, InsertPos);
175 // If we didn't find any existing attributes of the same shape then
176 // create a new one and insert it.
177 if (!PAL) {
178 PAL = new AttributeListImpl(Attrs, NumAttrs);
179 AttributesLists->InsertNode(PAL, InsertPos);
182 // Return the AttributesList that we found or created.
183 return AttrListPtr(PAL);
187 //===----------------------------------------------------------------------===//
188 // AttrListPtr Method Implementations
189 //===----------------------------------------------------------------------===//
191 AttrListPtr::AttrListPtr(AttributeListImpl *LI) : AttrList(LI) {
192 if (LI) LI->AddRef();
195 AttrListPtr::AttrListPtr(const AttrListPtr &P) : AttrList(P.AttrList) {
196 if (AttrList) AttrList->AddRef();
199 const AttrListPtr &AttrListPtr::operator=(const AttrListPtr &RHS) {
200 sys::SmartScopedLock<true> Lock(*ALMutex);
201 if (AttrList == RHS.AttrList) return *this;
202 if (AttrList) AttrList->DropRef();
203 AttrList = RHS.AttrList;
204 if (AttrList) AttrList->AddRef();
205 return *this;
208 AttrListPtr::~AttrListPtr() {
209 if (AttrList) AttrList->DropRef();
212 /// getNumSlots - Return the number of slots used in this attribute list.
213 /// This is the number of arguments that have an attribute set on them
214 /// (including the function itself).
215 unsigned AttrListPtr::getNumSlots() const {
216 return AttrList ? AttrList->Attrs.size() : 0;
219 /// getSlot - Return the AttributeWithIndex at the specified slot. This
220 /// holds a number plus a set of attributes.
221 const AttributeWithIndex &AttrListPtr::getSlot(unsigned Slot) const {
222 assert(AttrList && Slot < AttrList->Attrs.size() && "Slot # out of range!");
223 return AttrList->Attrs[Slot];
227 /// getAttributes - The attributes for the specified index are
228 /// returned. Attributes for the result are denoted with Idx = 0.
229 /// Function notes are denoted with idx = ~0.
230 Attributes AttrListPtr::getAttributes(unsigned Idx) const {
231 if (AttrList == 0) return Attribute::None;
233 const SmallVector<AttributeWithIndex, 4> &Attrs = AttrList->Attrs;
234 for (unsigned i = 0, e = Attrs.size(); i != e && Attrs[i].Index <= Idx; ++i)
235 if (Attrs[i].Index == Idx)
236 return Attrs[i].Attrs;
237 return Attribute::None;
240 /// hasAttrSomewhere - Return true if the specified attribute is set for at
241 /// least one parameter or for the return value.
242 bool AttrListPtr::hasAttrSomewhere(Attributes Attr) const {
243 if (AttrList == 0) return false;
245 const SmallVector<AttributeWithIndex, 4> &Attrs = AttrList->Attrs;
246 for (unsigned i = 0, e = Attrs.size(); i != e; ++i)
247 if (Attrs[i].Attrs & Attr)
248 return true;
249 return false;
253 AttrListPtr AttrListPtr::addAttr(unsigned Idx, Attributes Attrs) const {
254 Attributes OldAttrs = getAttributes(Idx);
255 #ifndef NDEBUG
256 // FIXME it is not obvious how this should work for alignment.
257 // For now, say we can't change a known alignment.
258 Attributes OldAlign = OldAttrs & Attribute::Alignment;
259 Attributes NewAlign = Attrs & Attribute::Alignment;
260 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
261 "Attempt to change alignment!");
262 #endif
264 Attributes NewAttrs = OldAttrs | Attrs;
265 if (NewAttrs == OldAttrs)
266 return *this;
268 SmallVector<AttributeWithIndex, 8> NewAttrList;
269 if (AttrList == 0)
270 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
271 else {
272 const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs;
273 unsigned i = 0, e = OldAttrList.size();
274 // Copy attributes for arguments before this one.
275 for (; i != e && OldAttrList[i].Index < Idx; ++i)
276 NewAttrList.push_back(OldAttrList[i]);
278 // If there are attributes already at this index, merge them in.
279 if (i != e && OldAttrList[i].Index == Idx) {
280 Attrs |= OldAttrList[i].Attrs;
281 ++i;
284 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
286 // Copy attributes for arguments after this one.
287 NewAttrList.insert(NewAttrList.end(),
288 OldAttrList.begin()+i, OldAttrList.end());
291 return get(NewAttrList.data(), NewAttrList.size());
294 AttrListPtr AttrListPtr::removeAttr(unsigned Idx, Attributes Attrs) const {
295 #ifndef NDEBUG
296 // FIXME it is not obvious how this should work for alignment.
297 // For now, say we can't pass in alignment, which no current use does.
298 assert(!(Attrs & Attribute::Alignment) && "Attempt to exclude alignment!");
299 #endif
300 if (AttrList == 0) return AttrListPtr();
302 Attributes OldAttrs = getAttributes(Idx);
303 Attributes NewAttrs = OldAttrs & ~Attrs;
304 if (NewAttrs == OldAttrs)
305 return *this;
307 SmallVector<AttributeWithIndex, 8> NewAttrList;
308 const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs;
309 unsigned i = 0, e = OldAttrList.size();
311 // Copy attributes for arguments before this one.
312 for (; i != e && OldAttrList[i].Index < Idx; ++i)
313 NewAttrList.push_back(OldAttrList[i]);
315 // If there are attributes already at this index, merge them in.
316 assert(OldAttrList[i].Index == Idx && "Attribute isn't set?");
317 Attrs = OldAttrList[i].Attrs & ~Attrs;
318 ++i;
319 if (Attrs) // If any attributes left for this parameter, add them.
320 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
322 // Copy attributes for arguments after this one.
323 NewAttrList.insert(NewAttrList.end(),
324 OldAttrList.begin()+i, OldAttrList.end());
326 return get(NewAttrList.data(), NewAttrList.size());
329 void AttrListPtr::dump() const {
330 dbgs() << "PAL[ ";
331 for (unsigned i = 0; i < getNumSlots(); ++i) {
332 const AttributeWithIndex &PAWI = getSlot(i);
333 dbgs() << "{" << PAWI.Index << "," << PAWI.Attrs << "} ";
336 dbgs() << "]\n";