blob: 91c7320a708b5b3e880bc2183fe644b2a5c8d538 [file] [log] [blame]
Devang Patel4c758ea2008-09-25 21:00:45 +00001//===-- Attributes.cpp - Implement AttributesList -------------------------===//
Chris Lattner3e13b8c2008-01-02 23:42:30 +00002//
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//
Devang Patel4c758ea2008-09-25 21:00:45 +000010// This file implements the AttributesList class and Attribute utilities.
Chris Lattner3e13b8c2008-01-02 23:42:30 +000011//
12//===----------------------------------------------------------------------===//
13
Devang Patelba3fa6c2008-09-23 23:03:40 +000014#include "llvm/Attributes.h"
Chris Lattner8a923e72008-03-12 17:45:29 +000015#include "llvm/Type.h"
Dan Gohmanfc429612008-03-10 23:55:07 +000016#include "llvm/ADT/StringExtras.h"
Chris Lattner8a923e72008-03-12 17:45:29 +000017#include "llvm/ADT/FoldingSet.h"
Owen Andersond91e6b02009-08-17 17:10:58 +000018#include "llvm/System/Mutex.h"
Chris Lattner8a923e72008-03-12 17:45:29 +000019#include "llvm/Support/Streams.h"
Chris Lattner3e13b8c2008-01-02 23:42:30 +000020#include "llvm/Support/ManagedStatic.h"
21using namespace llvm;
22
Chris Lattner8a923e72008-03-12 17:45:29 +000023//===----------------------------------------------------------------------===//
Devang Patel4c758ea2008-09-25 21:00:45 +000024// Attribute Function Definitions
Chris Lattner8a923e72008-03-12 17:45:29 +000025//===----------------------------------------------------------------------===//
Chris Lattnerd0e1f102008-01-03 00:10:22 +000026
Devang Patel4c758ea2008-09-25 21:00:45 +000027std::string Attribute::getAsString(Attributes Attrs) {
Chris Lattner3e13b8c2008-01-02 23:42:30 +000028 std::string Result;
Devang Patel4c758ea2008-09-25 21:00:45 +000029 if (Attrs & Attribute::ZExt)
Chris Lattner3e13b8c2008-01-02 23:42:30 +000030 Result += "zeroext ";
Devang Patel4c758ea2008-09-25 21:00:45 +000031 if (Attrs & Attribute::SExt)
Chris Lattner3e13b8c2008-01-02 23:42:30 +000032 Result += "signext ";
Devang Patel4c758ea2008-09-25 21:00:45 +000033 if (Attrs & Attribute::NoReturn)
Chris Lattner3e13b8c2008-01-02 23:42:30 +000034 Result += "noreturn ";
Devang Patel4c758ea2008-09-25 21:00:45 +000035 if (Attrs & Attribute::NoUnwind)
Chris Lattner3e13b8c2008-01-02 23:42:30 +000036 Result += "nounwind ";
Devang Patel4c758ea2008-09-25 21:00:45 +000037 if (Attrs & Attribute::InReg)
Chris Lattner3e13b8c2008-01-02 23:42:30 +000038 Result += "inreg ";
Devang Patel4c758ea2008-09-25 21:00:45 +000039 if (Attrs & Attribute::NoAlias)
Chris Lattner3e13b8c2008-01-02 23:42:30 +000040 Result += "noalias ";
Nick Lewycky8d69f482008-12-19 09:38:31 +000041 if (Attrs & Attribute::NoCapture)
42 Result += "nocapture ";
Devang Patel4c758ea2008-09-25 21:00:45 +000043 if (Attrs & Attribute::StructRet)
Anton Korobeynikovc8ce7b082009-07-17 18:07:26 +000044 Result += "sret ";
Devang Patel4c758ea2008-09-25 21:00:45 +000045 if (Attrs & Attribute::ByVal)
Chris Lattner3e13b8c2008-01-02 23:42:30 +000046 Result += "byval ";
Devang Patel4c758ea2008-09-25 21:00:45 +000047 if (Attrs & Attribute::Nest)
Chris Lattner3e13b8c2008-01-02 23:42:30 +000048 Result += "nest ";
Devang Patel4c758ea2008-09-25 21:00:45 +000049 if (Attrs & Attribute::ReadNone)
Chris Lattner3e13b8c2008-01-02 23:42:30 +000050 Result += "readnone ";
Devang Patel4c758ea2008-09-25 21:00:45 +000051 if (Attrs & Attribute::ReadOnly)
Chris Lattner3e13b8c2008-01-02 23:42:30 +000052 Result += "readonly ";
Devang Patela05633e2008-09-26 22:53:05 +000053 if (Attrs & Attribute::OptimizeForSize)
54 Result += "optsize ";
55 if (Attrs & Attribute::NoInline)
56 Result += "noinline ";
57 if (Attrs & Attribute::AlwaysInline)
58 Result += "alwaysinline ";
Bill Wendlingccb67a3d2008-11-13 01:02:14 +000059 if (Attrs & Attribute::StackProtect)
60 Result += "ssp ";
61 if (Attrs & Attribute::StackProtectReq)
62 Result += "sspreq ";
Devang Patel72a4d2f2009-06-04 22:05:33 +000063 if (Attrs & Attribute::NoRedZone)
64 Result += "noredzone ";
Devang Pateld1c7d342009-06-05 21:57:13 +000065 if (Attrs & Attribute::NoImplicitFloat)
66 Result += "noimplicitfloat ";
Anton Korobeynikovc8ce7b082009-07-17 18:07:26 +000067 if (Attrs & Attribute::Naked)
68 Result += "naked ";
Devang Patel4c758ea2008-09-25 21:00:45 +000069 if (Attrs & Attribute::Alignment) {
Dale Johannesen11a555e2008-02-19 23:51:49 +000070 Result += "align ";
Nick Lewycky93787d12009-01-11 17:02:06 +000071 Result += utostr(Attribute::getAlignmentFromAttrs(Attrs));
Dale Johannesen11a555e2008-02-19 23:51:49 +000072 Result += " ";
73 }
Dan Gohman1a70bcc2008-08-05 15:51:44 +000074 // Trim the trailing space.
Nick Lewycky8d69f482008-12-19 09:38:31 +000075 assert(!Result.empty() && "Unknown attribute!");
Dan Gohman1a70bcc2008-08-05 15:51:44 +000076 Result.erase(Result.end()-1);
Chris Lattner3e13b8c2008-01-02 23:42:30 +000077 return Result;
78}
79
Devang Patel4c758ea2008-09-25 21:00:45 +000080Attributes Attribute::typeIncompatible(const Type *Ty) {
Devang Patelba3fa6c2008-09-23 23:03:40 +000081 Attributes Incompatible = None;
Chris Lattner8a923e72008-03-12 17:45:29 +000082
83 if (!Ty->isInteger())
84 // Attributes that only apply to integers.
85 Incompatible |= SExt | ZExt;
86
87 if (!isa<PointerType>(Ty))
88 // Attributes that only apply to pointers.
Nick Lewycky8d69f482008-12-19 09:38:31 +000089 Incompatible |= ByVal | Nest | NoAlias | StructRet | NoCapture;
Chris Lattner8a923e72008-03-12 17:45:29 +000090
91 return Incompatible;
Chris Lattner3e13b8c2008-01-02 23:42:30 +000092}
93
Chris Lattner8a923e72008-03-12 17:45:29 +000094//===----------------------------------------------------------------------===//
Devang Patel00095052008-09-24 00:29:49 +000095// AttributeListImpl Definition
Chris Lattner8a923e72008-03-12 17:45:29 +000096//===----------------------------------------------------------------------===//
Chris Lattner3e13b8c2008-01-02 23:42:30 +000097
Chris Lattner8a923e72008-03-12 17:45:29 +000098namespace llvm {
Devang Patel00095052008-09-24 00:29:49 +000099class AttributeListImpl : public FoldingSetNode {
Chris Lattner8a923e72008-03-12 17:45:29 +0000100 unsigned RefCount;
101
Devang Patel4c758ea2008-09-25 21:00:45 +0000102 // AttributesList is uniqued, these should not be publicly available.
Devang Patel00095052008-09-24 00:29:49 +0000103 void operator=(const AttributeListImpl &); // Do not implement
104 AttributeListImpl(const AttributeListImpl &); // Do not implement
105 ~AttributeListImpl(); // Private implementation
Chris Lattner8a923e72008-03-12 17:45:29 +0000106public:
Devang Patel4c758ea2008-09-25 21:00:45 +0000107 SmallVector<AttributeWithIndex, 4> Attrs;
Chris Lattner8a923e72008-03-12 17:45:29 +0000108
Devang Patel4c758ea2008-09-25 21:00:45 +0000109 AttributeListImpl(const AttributeWithIndex *Attr, unsigned NumAttrs)
Chris Lattner8a923e72008-03-12 17:45:29 +0000110 : Attrs(Attr, Attr+NumAttrs) {
111 RefCount = 0;
112 }
113
114 void AddRef() { ++RefCount; }
115 void DropRef() { if (--RefCount == 0) delete this; }
116
117 void Profile(FoldingSetNodeID &ID) const {
Jay Foad7d0479f2009-05-21 09:52:38 +0000118 Profile(ID, Attrs.data(), Attrs.size());
Chris Lattner8a923e72008-03-12 17:45:29 +0000119 }
Devang Patel4c758ea2008-09-25 21:00:45 +0000120 static void Profile(FoldingSetNodeID &ID, const AttributeWithIndex *Attr,
Chris Lattner8a923e72008-03-12 17:45:29 +0000121 unsigned NumAttrs) {
122 for (unsigned i = 0; i != NumAttrs; ++i)
123 ID.AddInteger(uint64_t(Attr[i].Attrs) << 32 | unsigned(Attr[i].Index));
124 }
125};
126}
127
Owen Andersond91e6b02009-08-17 17:10:58 +0000128static ManagedStatic<sys::SmartMutex<true> > ALMutex;
Devang Patel4c758ea2008-09-25 21:00:45 +0000129static ManagedStatic<FoldingSet<AttributeListImpl> > AttributesLists;
Chris Lattner8a923e72008-03-12 17:45:29 +0000130
Devang Patel00095052008-09-24 00:29:49 +0000131AttributeListImpl::~AttributeListImpl() {
Owen Andersond91e6b02009-08-17 17:10:58 +0000132 sys::SmartScopedLock<true> Lock(*ALMutex);
Devang Patel4c758ea2008-09-25 21:00:45 +0000133 AttributesLists->RemoveNode(this);
Chris Lattner8a923e72008-03-12 17:45:29 +0000134}
135
136
Devang Patel4c758ea2008-09-25 21:00:45 +0000137AttrListPtr AttrListPtr::get(const AttributeWithIndex *Attrs, unsigned NumAttrs) {
138 // If there are no attributes then return a null AttributesList pointer.
Chris Lattner8a923e72008-03-12 17:45:29 +0000139 if (NumAttrs == 0)
Devang Patel4c758ea2008-09-25 21:00:45 +0000140 return AttrListPtr();
Chris Lattner8a923e72008-03-12 17:45:29 +0000141
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000142#ifndef NDEBUG
Chris Lattner8a923e72008-03-12 17:45:29 +0000143 for (unsigned i = 0; i != NumAttrs; ++i) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000144 assert(Attrs[i].Attrs != Attribute::None &&
145 "Pointless attribute!");
Chris Lattner8a923e72008-03-12 17:45:29 +0000146 assert((!i || Attrs[i-1].Index < Attrs[i].Index) &&
Devang Patel4c758ea2008-09-25 21:00:45 +0000147 "Misordered AttributesList!");
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000148 }
149#endif
Chris Lattner8a923e72008-03-12 17:45:29 +0000150
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000151 // Otherwise, build a key to look up the existing attributes.
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000152 FoldingSetNodeID ID;
Devang Patel00095052008-09-24 00:29:49 +0000153 AttributeListImpl::Profile(ID, Attrs, NumAttrs);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000154 void *InsertPos;
Owen Andersond91e6b02009-08-17 17:10:58 +0000155
156 sys::SmartScopedLock<true> Lock(*ALMutex);
157
Devang Patel00095052008-09-24 00:29:49 +0000158 AttributeListImpl *PAL =
Devang Patel4c758ea2008-09-25 21:00:45 +0000159 AttributesLists->FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner8a923e72008-03-12 17:45:29 +0000160
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000161 // If we didn't find any existing attributes of the same shape then
162 // create a new one and insert it.
163 if (!PAL) {
Devang Patel00095052008-09-24 00:29:49 +0000164 PAL = new AttributeListImpl(Attrs, NumAttrs);
Devang Patel4c758ea2008-09-25 21:00:45 +0000165 AttributesLists->InsertNode(PAL, InsertPos);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000166 }
Chris Lattner8a923e72008-03-12 17:45:29 +0000167
Devang Patel4c758ea2008-09-25 21:00:45 +0000168 // Return the AttributesList that we found or created.
169 return AttrListPtr(PAL);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000170}
171
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000172
Chris Lattner8a923e72008-03-12 17:45:29 +0000173//===----------------------------------------------------------------------===//
Devang Patel4c758ea2008-09-25 21:00:45 +0000174// AttrListPtr Method Implementations
Chris Lattner8a923e72008-03-12 17:45:29 +0000175//===----------------------------------------------------------------------===//
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000176
Devang Patel4c758ea2008-09-25 21:00:45 +0000177AttrListPtr::AttrListPtr(AttributeListImpl *LI) : AttrList(LI) {
Chris Lattner8a923e72008-03-12 17:45:29 +0000178 if (LI) LI->AddRef();
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000179}
180
Devang Patel4c758ea2008-09-25 21:00:45 +0000181AttrListPtr::AttrListPtr(const AttrListPtr &P) : AttrList(P.AttrList) {
182 if (AttrList) AttrList->AddRef();
Chris Lattner8a923e72008-03-12 17:45:29 +0000183}
184
Devang Patel4c758ea2008-09-25 21:00:45 +0000185const AttrListPtr &AttrListPtr::operator=(const AttrListPtr &RHS) {
186 if (AttrList == RHS.AttrList) return *this;
187 if (AttrList) AttrList->DropRef();
188 AttrList = RHS.AttrList;
189 if (AttrList) AttrList->AddRef();
Chris Lattner8a923e72008-03-12 17:45:29 +0000190 return *this;
191}
192
Devang Patel4c758ea2008-09-25 21:00:45 +0000193AttrListPtr::~AttrListPtr() {
194 if (AttrList) AttrList->DropRef();
Chris Lattner8a923e72008-03-12 17:45:29 +0000195}
196
197/// getNumSlots - Return the number of slots used in this attribute list.
198/// This is the number of arguments that have an attribute set on them
199/// (including the function itself).
Devang Patel4c758ea2008-09-25 21:00:45 +0000200unsigned AttrListPtr::getNumSlots() const {
201 return AttrList ? AttrList->Attrs.size() : 0;
Chris Lattner8a923e72008-03-12 17:45:29 +0000202}
203
Devang Patel4c758ea2008-09-25 21:00:45 +0000204/// getSlot - Return the AttributeWithIndex at the specified slot. This
205/// holds a number plus a set of attributes.
206const AttributeWithIndex &AttrListPtr::getSlot(unsigned Slot) const {
207 assert(AttrList && Slot < AttrList->Attrs.size() && "Slot # out of range!");
208 return AttrList->Attrs[Slot];
Chris Lattner8a923e72008-03-12 17:45:29 +0000209}
210
211
Devang Patel4c758ea2008-09-25 21:00:45 +0000212/// getAttributes - The attributes for the specified index are
213/// returned. Attributes for the result are denoted with Idx = 0.
Devang Patel82fed672008-09-23 22:35:17 +0000214/// Function notes are denoted with idx = ~0.
Devang Patel4c758ea2008-09-25 21:00:45 +0000215Attributes AttrListPtr::getAttributes(unsigned Idx) const {
216 if (AttrList == 0) return Attribute::None;
Chris Lattner8a923e72008-03-12 17:45:29 +0000217
Devang Patel4c758ea2008-09-25 21:00:45 +0000218 const SmallVector<AttributeWithIndex, 4> &Attrs = AttrList->Attrs;
Chris Lattner8a923e72008-03-12 17:45:29 +0000219 for (unsigned i = 0, e = Attrs.size(); i != e && Attrs[i].Index <= Idx; ++i)
220 if (Attrs[i].Index == Idx)
221 return Attrs[i].Attrs;
Devang Patel4c758ea2008-09-25 21:00:45 +0000222 return Attribute::None;
Chris Lattner8a923e72008-03-12 17:45:29 +0000223}
224
225/// hasAttrSomewhere - Return true if the specified attribute is set for at
226/// least one parameter or for the return value.
Devang Patel4c758ea2008-09-25 21:00:45 +0000227bool AttrListPtr::hasAttrSomewhere(Attributes Attr) const {
228 if (AttrList == 0) return false;
Chris Lattner8a923e72008-03-12 17:45:29 +0000229
Devang Patel4c758ea2008-09-25 21:00:45 +0000230 const SmallVector<AttributeWithIndex, 4> &Attrs = AttrList->Attrs;
Chris Lattner8a923e72008-03-12 17:45:29 +0000231 for (unsigned i = 0, e = Attrs.size(); i != e; ++i)
232 if (Attrs[i].Attrs & Attr)
233 return true;
234 return false;
235}
236
237
Devang Patel4c758ea2008-09-25 21:00:45 +0000238AttrListPtr AttrListPtr::addAttr(unsigned Idx, Attributes Attrs) const {
239 Attributes OldAttrs = getAttributes(Idx);
Dale Johannesen11a555e2008-02-19 23:51:49 +0000240#ifndef NDEBUG
241 // FIXME it is not obvious how this should work for alignment.
242 // For now, say we can't change a known alignment.
Devang Patel4c758ea2008-09-25 21:00:45 +0000243 Attributes OldAlign = OldAttrs & Attribute::Alignment;
244 Attributes NewAlign = Attrs & Attribute::Alignment;
Anton Korobeynikov18991d72008-02-20 12:07:57 +0000245 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
Dale Johannesen11a555e2008-02-19 23:51:49 +0000246 "Attempt to change alignment!");
247#endif
Chris Lattner8a923e72008-03-12 17:45:29 +0000248
Devang Patelba3fa6c2008-09-23 23:03:40 +0000249 Attributes NewAttrs = OldAttrs | Attrs;
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000250 if (NewAttrs == OldAttrs)
Chris Lattner8a923e72008-03-12 17:45:29 +0000251 return *this;
252
Devang Patel4c758ea2008-09-25 21:00:45 +0000253 SmallVector<AttributeWithIndex, 8> NewAttrList;
254 if (AttrList == 0)
255 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Chris Lattner8a923e72008-03-12 17:45:29 +0000256 else {
Devang Patel4c758ea2008-09-25 21:00:45 +0000257 const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs;
Chris Lattner8a923e72008-03-12 17:45:29 +0000258 unsigned i = 0, e = OldAttrList.size();
259 // Copy attributes for arguments before this one.
260 for (; i != e && OldAttrList[i].Index < Idx; ++i)
261 NewAttrList.push_back(OldAttrList[i]);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000262
Chris Lattner8a923e72008-03-12 17:45:29 +0000263 // If there are attributes already at this index, merge them in.
264 if (i != e && OldAttrList[i].Index == Idx) {
265 Attrs |= OldAttrList[i].Attrs;
266 ++i;
267 }
268
Devang Patel4c758ea2008-09-25 21:00:45 +0000269 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Chris Lattner8a923e72008-03-12 17:45:29 +0000270
271 // Copy attributes for arguments after this one.
272 NewAttrList.insert(NewAttrList.end(),
273 OldAttrList.begin()+i, OldAttrList.end());
274 }
275
Jay Foad7d0479f2009-05-21 09:52:38 +0000276 return get(NewAttrList.data(), NewAttrList.size());
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000277}
278
Devang Patel4c758ea2008-09-25 21:00:45 +0000279AttrListPtr AttrListPtr::removeAttr(unsigned Idx, Attributes Attrs) const {
Dale Johannesen11a555e2008-02-19 23:51:49 +0000280#ifndef NDEBUG
281 // FIXME it is not obvious how this should work for alignment.
282 // For now, say we can't pass in alignment, which no current use does.
Devang Patel4c758ea2008-09-25 21:00:45 +0000283 assert(!(Attrs & Attribute::Alignment) && "Attempt to exclude alignment!");
Dale Johannesen11a555e2008-02-19 23:51:49 +0000284#endif
Devang Patel4c758ea2008-09-25 21:00:45 +0000285 if (AttrList == 0) return AttrListPtr();
Chris Lattner8a923e72008-03-12 17:45:29 +0000286
Devang Patel4c758ea2008-09-25 21:00:45 +0000287 Attributes OldAttrs = getAttributes(Idx);
Devang Patelba3fa6c2008-09-23 23:03:40 +0000288 Attributes NewAttrs = OldAttrs & ~Attrs;
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000289 if (NewAttrs == OldAttrs)
Chris Lattner8a923e72008-03-12 17:45:29 +0000290 return *this;
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000291
Devang Patel4c758ea2008-09-25 21:00:45 +0000292 SmallVector<AttributeWithIndex, 8> NewAttrList;
293 const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs;
Chris Lattner8a923e72008-03-12 17:45:29 +0000294 unsigned i = 0, e = OldAttrList.size();
295
296 // Copy attributes for arguments before this one.
297 for (; i != e && OldAttrList[i].Index < Idx; ++i)
298 NewAttrList.push_back(OldAttrList[i]);
299
300 // If there are attributes already at this index, merge them in.
301 assert(OldAttrList[i].Index == Idx && "Attribute isn't set?");
302 Attrs = OldAttrList[i].Attrs & ~Attrs;
303 ++i;
304 if (Attrs) // If any attributes left for this parameter, add them.
Devang Patel4c758ea2008-09-25 21:00:45 +0000305 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Chris Lattner8a923e72008-03-12 17:45:29 +0000306
307 // Copy attributes for arguments after this one.
308 NewAttrList.insert(NewAttrList.end(),
309 OldAttrList.begin()+i, OldAttrList.end());
310
Jay Foad7d0479f2009-05-21 09:52:38 +0000311 return get(NewAttrList.data(), NewAttrList.size());
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000312}
313
Devang Patel4c758ea2008-09-25 21:00:45 +0000314void AttrListPtr::dump() const {
Chris Lattner8a923e72008-03-12 17:45:29 +0000315 cerr << "PAL[ ";
316 for (unsigned i = 0; i < getNumSlots(); ++i) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000317 const AttributeWithIndex &PAWI = getSlot(i);
Chris Lattner8a923e72008-03-12 17:45:29 +0000318 cerr << "{" << PAWI.Index << "," << PAWI.Attrs << "} ";
319 }
320
321 cerr << "]\n";
Duncan Sands404eb052008-01-06 18:27:01 +0000322}