blob: 3ebcadb2330f84af5f9c0f132a2ffe5402a8c19e [file] [log] [blame]
Devang Patel05988662008-09-25 21:00:45 +00001//===-- Attributes.cpp - Implement AttributesList -------------------------===//
Chris Lattner50ee9dd2008-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 Patel05988662008-09-25 21:00:45 +000010// This file implements the AttributesList class and Attribute utilities.
Chris Lattner50ee9dd2008-01-02 23:42:30 +000011//
12//===----------------------------------------------------------------------===//
13
Devang Pateleaf42ab2008-09-23 23:03:40 +000014#include "llvm/Attributes.h"
Chris Lattner58d74912008-03-12 17:45:29 +000015#include "llvm/Type.h"
Dan Gohmanac9dff62008-03-10 23:55:07 +000016#include "llvm/ADT/StringExtras.h"
Chris Lattner58d74912008-03-12 17:45:29 +000017#include "llvm/ADT/FoldingSet.h"
18#include "llvm/Support/Streams.h"
Chris Lattner50ee9dd2008-01-02 23:42:30 +000019#include "llvm/Support/ManagedStatic.h"
20using namespace llvm;
21
Chris Lattner58d74912008-03-12 17:45:29 +000022//===----------------------------------------------------------------------===//
Devang Patel05988662008-09-25 21:00:45 +000023// Attribute Function Definitions
Chris Lattner58d74912008-03-12 17:45:29 +000024//===----------------------------------------------------------------------===//
Chris Lattnerfabfde32008-01-03 00:10:22 +000025
Devang Patel05988662008-09-25 21:00:45 +000026std::string Attribute::getAsString(Attributes Attrs) {
Chris Lattner50ee9dd2008-01-02 23:42:30 +000027 std::string Result;
Devang Patel05988662008-09-25 21:00:45 +000028 if (Attrs & Attribute::ZExt)
Chris Lattner50ee9dd2008-01-02 23:42:30 +000029 Result += "zeroext ";
Devang Patel05988662008-09-25 21:00:45 +000030 if (Attrs & Attribute::SExt)
Chris Lattner50ee9dd2008-01-02 23:42:30 +000031 Result += "signext ";
Devang Patel05988662008-09-25 21:00:45 +000032 if (Attrs & Attribute::NoReturn)
Chris Lattner50ee9dd2008-01-02 23:42:30 +000033 Result += "noreturn ";
Devang Patel05988662008-09-25 21:00:45 +000034 if (Attrs & Attribute::NoUnwind)
Chris Lattner50ee9dd2008-01-02 23:42:30 +000035 Result += "nounwind ";
Devang Patel05988662008-09-25 21:00:45 +000036 if (Attrs & Attribute::InReg)
Chris Lattner50ee9dd2008-01-02 23:42:30 +000037 Result += "inreg ";
Devang Patel05988662008-09-25 21:00:45 +000038 if (Attrs & Attribute::NoAlias)
Chris Lattner50ee9dd2008-01-02 23:42:30 +000039 Result += "noalias ";
Nick Lewycky73ddd4f2008-12-19 09:38:31 +000040 if (Attrs & Attribute::NoCapture)
41 Result += "nocapture ";
Devang Patel05988662008-09-25 21:00:45 +000042 if (Attrs & Attribute::StructRet)
Chris Lattner50ee9dd2008-01-02 23:42:30 +000043 Result += "sret ";
Devang Patel05988662008-09-25 21:00:45 +000044 if (Attrs & Attribute::ByVal)
Chris Lattner50ee9dd2008-01-02 23:42:30 +000045 Result += "byval ";
Devang Patel05988662008-09-25 21:00:45 +000046 if (Attrs & Attribute::Nest)
Chris Lattner50ee9dd2008-01-02 23:42:30 +000047 Result += "nest ";
Devang Patel05988662008-09-25 21:00:45 +000048 if (Attrs & Attribute::ReadNone)
Chris Lattner50ee9dd2008-01-02 23:42:30 +000049 Result += "readnone ";
Devang Patel05988662008-09-25 21:00:45 +000050 if (Attrs & Attribute::ReadOnly)
Chris Lattner50ee9dd2008-01-02 23:42:30 +000051 Result += "readonly ";
Devang Patel19c87462008-09-26 22:53:05 +000052 if (Attrs & Attribute::OptimizeForSize)
53 Result += "optsize ";
54 if (Attrs & Attribute::NoInline)
55 Result += "noinline ";
56 if (Attrs & Attribute::AlwaysInline)
57 Result += "alwaysinline ";
Bill Wendlinge9e6bdf2008-11-13 01:02:14 +000058 if (Attrs & Attribute::StackProtect)
59 Result += "ssp ";
60 if (Attrs & Attribute::StackProtectReq)
61 Result += "sspreq ";
Devang Pateld18e31a2009-06-04 22:05:33 +000062 if (Attrs & Attribute::NoRedZone)
63 Result += "noredzone ";
Devang Patel05988662008-09-25 21:00:45 +000064 if (Attrs & Attribute::Alignment) {
Dale Johannesen6167c3f2008-02-19 23:51:49 +000065 Result += "align ";
Nick Lewycky1ed86d72009-01-11 17:02:06 +000066 Result += utostr(Attribute::getAlignmentFromAttrs(Attrs));
Dale Johannesen6167c3f2008-02-19 23:51:49 +000067 Result += " ";
68 }
Dan Gohmanc3be0fd2008-08-05 15:51:44 +000069 // Trim the trailing space.
Nick Lewycky73ddd4f2008-12-19 09:38:31 +000070 assert(!Result.empty() && "Unknown attribute!");
Dan Gohmanc3be0fd2008-08-05 15:51:44 +000071 Result.erase(Result.end()-1);
Chris Lattner50ee9dd2008-01-02 23:42:30 +000072 return Result;
73}
74
Devang Patel05988662008-09-25 21:00:45 +000075Attributes Attribute::typeIncompatible(const Type *Ty) {
Devang Pateleaf42ab2008-09-23 23:03:40 +000076 Attributes Incompatible = None;
Chris Lattner58d74912008-03-12 17:45:29 +000077
78 if (!Ty->isInteger())
79 // Attributes that only apply to integers.
80 Incompatible |= SExt | ZExt;
81
82 if (!isa<PointerType>(Ty))
83 // Attributes that only apply to pointers.
Nick Lewycky73ddd4f2008-12-19 09:38:31 +000084 Incompatible |= ByVal | Nest | NoAlias | StructRet | NoCapture;
Chris Lattner58d74912008-03-12 17:45:29 +000085
86 return Incompatible;
Chris Lattner50ee9dd2008-01-02 23:42:30 +000087}
88
Chris Lattner58d74912008-03-12 17:45:29 +000089//===----------------------------------------------------------------------===//
Devang Patel1e480002008-09-24 00:29:49 +000090// AttributeListImpl Definition
Chris Lattner58d74912008-03-12 17:45:29 +000091//===----------------------------------------------------------------------===//
Chris Lattner50ee9dd2008-01-02 23:42:30 +000092
Chris Lattner58d74912008-03-12 17:45:29 +000093namespace llvm {
Devang Patel1e480002008-09-24 00:29:49 +000094class AttributeListImpl : public FoldingSetNode {
Chris Lattner58d74912008-03-12 17:45:29 +000095 unsigned RefCount;
96
Devang Patel05988662008-09-25 21:00:45 +000097 // AttributesList is uniqued, these should not be publicly available.
Devang Patel1e480002008-09-24 00:29:49 +000098 void operator=(const AttributeListImpl &); // Do not implement
99 AttributeListImpl(const AttributeListImpl &); // Do not implement
100 ~AttributeListImpl(); // Private implementation
Chris Lattner58d74912008-03-12 17:45:29 +0000101public:
Devang Patel05988662008-09-25 21:00:45 +0000102 SmallVector<AttributeWithIndex, 4> Attrs;
Chris Lattner58d74912008-03-12 17:45:29 +0000103
Devang Patel05988662008-09-25 21:00:45 +0000104 AttributeListImpl(const AttributeWithIndex *Attr, unsigned NumAttrs)
Chris Lattner58d74912008-03-12 17:45:29 +0000105 : Attrs(Attr, Attr+NumAttrs) {
106 RefCount = 0;
107 }
108
109 void AddRef() { ++RefCount; }
110 void DropRef() { if (--RefCount == 0) delete this; }
111
112 void Profile(FoldingSetNodeID &ID) const {
Jay Foade3e51c02009-05-21 09:52:38 +0000113 Profile(ID, Attrs.data(), Attrs.size());
Chris Lattner58d74912008-03-12 17:45:29 +0000114 }
Devang Patel05988662008-09-25 21:00:45 +0000115 static void Profile(FoldingSetNodeID &ID, const AttributeWithIndex *Attr,
Chris Lattner58d74912008-03-12 17:45:29 +0000116 unsigned NumAttrs) {
117 for (unsigned i = 0; i != NumAttrs; ++i)
118 ID.AddInteger(uint64_t(Attr[i].Attrs) << 32 | unsigned(Attr[i].Index));
119 }
120};
121}
122
Devang Patel05988662008-09-25 21:00:45 +0000123static ManagedStatic<FoldingSet<AttributeListImpl> > AttributesLists;
Chris Lattner58d74912008-03-12 17:45:29 +0000124
Devang Patel1e480002008-09-24 00:29:49 +0000125AttributeListImpl::~AttributeListImpl() {
Devang Patel05988662008-09-25 21:00:45 +0000126 AttributesLists->RemoveNode(this);
Chris Lattner58d74912008-03-12 17:45:29 +0000127}
128
129
Devang Patel05988662008-09-25 21:00:45 +0000130AttrListPtr AttrListPtr::get(const AttributeWithIndex *Attrs, unsigned NumAttrs) {
131 // If there are no attributes then return a null AttributesList pointer.
Chris Lattner58d74912008-03-12 17:45:29 +0000132 if (NumAttrs == 0)
Devang Patel05988662008-09-25 21:00:45 +0000133 return AttrListPtr();
Chris Lattner58d74912008-03-12 17:45:29 +0000134
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000135#ifndef NDEBUG
Chris Lattner58d74912008-03-12 17:45:29 +0000136 for (unsigned i = 0; i != NumAttrs; ++i) {
Devang Patel05988662008-09-25 21:00:45 +0000137 assert(Attrs[i].Attrs != Attribute::None &&
138 "Pointless attribute!");
Chris Lattner58d74912008-03-12 17:45:29 +0000139 assert((!i || Attrs[i-1].Index < Attrs[i].Index) &&
Devang Patel05988662008-09-25 21:00:45 +0000140 "Misordered AttributesList!");
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000141 }
142#endif
Chris Lattner58d74912008-03-12 17:45:29 +0000143
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000144 // Otherwise, build a key to look up the existing attributes.
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000145 FoldingSetNodeID ID;
Devang Patel1e480002008-09-24 00:29:49 +0000146 AttributeListImpl::Profile(ID, Attrs, NumAttrs);
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000147 void *InsertPos;
Devang Patel1e480002008-09-24 00:29:49 +0000148 AttributeListImpl *PAL =
Devang Patel05988662008-09-25 21:00:45 +0000149 AttributesLists->FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner58d74912008-03-12 17:45:29 +0000150
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000151 // If we didn't find any existing attributes of the same shape then
152 // create a new one and insert it.
153 if (!PAL) {
Devang Patel1e480002008-09-24 00:29:49 +0000154 PAL = new AttributeListImpl(Attrs, NumAttrs);
Devang Patel05988662008-09-25 21:00:45 +0000155 AttributesLists->InsertNode(PAL, InsertPos);
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000156 }
Chris Lattner58d74912008-03-12 17:45:29 +0000157
Devang Patel05988662008-09-25 21:00:45 +0000158 // Return the AttributesList that we found or created.
159 return AttrListPtr(PAL);
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000160}
161
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000162
Chris Lattner58d74912008-03-12 17:45:29 +0000163//===----------------------------------------------------------------------===//
Devang Patel05988662008-09-25 21:00:45 +0000164// AttrListPtr Method Implementations
Chris Lattner58d74912008-03-12 17:45:29 +0000165//===----------------------------------------------------------------------===//
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000166
Devang Patel05988662008-09-25 21:00:45 +0000167AttrListPtr::AttrListPtr(AttributeListImpl *LI) : AttrList(LI) {
Chris Lattner58d74912008-03-12 17:45:29 +0000168 if (LI) LI->AddRef();
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000169}
170
Devang Patel05988662008-09-25 21:00:45 +0000171AttrListPtr::AttrListPtr(const AttrListPtr &P) : AttrList(P.AttrList) {
172 if (AttrList) AttrList->AddRef();
Chris Lattner58d74912008-03-12 17:45:29 +0000173}
174
Devang Patel05988662008-09-25 21:00:45 +0000175const AttrListPtr &AttrListPtr::operator=(const AttrListPtr &RHS) {
176 if (AttrList == RHS.AttrList) return *this;
177 if (AttrList) AttrList->DropRef();
178 AttrList = RHS.AttrList;
179 if (AttrList) AttrList->AddRef();
Chris Lattner58d74912008-03-12 17:45:29 +0000180 return *this;
181}
182
Devang Patel05988662008-09-25 21:00:45 +0000183AttrListPtr::~AttrListPtr() {
184 if (AttrList) AttrList->DropRef();
Chris Lattner58d74912008-03-12 17:45:29 +0000185}
186
187/// getNumSlots - Return the number of slots used in this attribute list.
188/// This is the number of arguments that have an attribute set on them
189/// (including the function itself).
Devang Patel05988662008-09-25 21:00:45 +0000190unsigned AttrListPtr::getNumSlots() const {
191 return AttrList ? AttrList->Attrs.size() : 0;
Chris Lattner58d74912008-03-12 17:45:29 +0000192}
193
Devang Patel05988662008-09-25 21:00:45 +0000194/// getSlot - Return the AttributeWithIndex at the specified slot. This
195/// holds a number plus a set of attributes.
196const AttributeWithIndex &AttrListPtr::getSlot(unsigned Slot) const {
197 assert(AttrList && Slot < AttrList->Attrs.size() && "Slot # out of range!");
198 return AttrList->Attrs[Slot];
Chris Lattner58d74912008-03-12 17:45:29 +0000199}
200
201
Devang Patel05988662008-09-25 21:00:45 +0000202/// getAttributes - The attributes for the specified index are
203/// returned. Attributes for the result are denoted with Idx = 0.
Devang Pateld9b4a5f2008-09-23 22:35:17 +0000204/// Function notes are denoted with idx = ~0.
Devang Patel05988662008-09-25 21:00:45 +0000205Attributes AttrListPtr::getAttributes(unsigned Idx) const {
206 if (AttrList == 0) return Attribute::None;
Chris Lattner58d74912008-03-12 17:45:29 +0000207
Devang Patel05988662008-09-25 21:00:45 +0000208 const SmallVector<AttributeWithIndex, 4> &Attrs = AttrList->Attrs;
Chris Lattner58d74912008-03-12 17:45:29 +0000209 for (unsigned i = 0, e = Attrs.size(); i != e && Attrs[i].Index <= Idx; ++i)
210 if (Attrs[i].Index == Idx)
211 return Attrs[i].Attrs;
Devang Patel05988662008-09-25 21:00:45 +0000212 return Attribute::None;
Chris Lattner58d74912008-03-12 17:45:29 +0000213}
214
215/// hasAttrSomewhere - Return true if the specified attribute is set for at
216/// least one parameter or for the return value.
Devang Patel05988662008-09-25 21:00:45 +0000217bool AttrListPtr::hasAttrSomewhere(Attributes Attr) const {
218 if (AttrList == 0) return false;
Chris Lattner58d74912008-03-12 17:45:29 +0000219
Devang Patel05988662008-09-25 21:00:45 +0000220 const SmallVector<AttributeWithIndex, 4> &Attrs = AttrList->Attrs;
Chris Lattner58d74912008-03-12 17:45:29 +0000221 for (unsigned i = 0, e = Attrs.size(); i != e; ++i)
222 if (Attrs[i].Attrs & Attr)
223 return true;
224 return false;
225}
226
227
Devang Patel05988662008-09-25 21:00:45 +0000228AttrListPtr AttrListPtr::addAttr(unsigned Idx, Attributes Attrs) const {
229 Attributes OldAttrs = getAttributes(Idx);
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000230#ifndef NDEBUG
231 // FIXME it is not obvious how this should work for alignment.
232 // For now, say we can't change a known alignment.
Devang Patel05988662008-09-25 21:00:45 +0000233 Attributes OldAlign = OldAttrs & Attribute::Alignment;
234 Attributes NewAlign = Attrs & Attribute::Alignment;
Anton Korobeynikov4aefd6b2008-02-20 12:07:57 +0000235 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000236 "Attempt to change alignment!");
237#endif
Chris Lattner58d74912008-03-12 17:45:29 +0000238
Devang Pateleaf42ab2008-09-23 23:03:40 +0000239 Attributes NewAttrs = OldAttrs | Attrs;
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000240 if (NewAttrs == OldAttrs)
Chris Lattner58d74912008-03-12 17:45:29 +0000241 return *this;
242
Devang Patel05988662008-09-25 21:00:45 +0000243 SmallVector<AttributeWithIndex, 8> NewAttrList;
244 if (AttrList == 0)
245 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Chris Lattner58d74912008-03-12 17:45:29 +0000246 else {
Devang Patel05988662008-09-25 21:00:45 +0000247 const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs;
Chris Lattner58d74912008-03-12 17:45:29 +0000248 unsigned i = 0, e = OldAttrList.size();
249 // Copy attributes for arguments before this one.
250 for (; i != e && OldAttrList[i].Index < Idx; ++i)
251 NewAttrList.push_back(OldAttrList[i]);
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000252
Chris Lattner58d74912008-03-12 17:45:29 +0000253 // If there are attributes already at this index, merge them in.
254 if (i != e && OldAttrList[i].Index == Idx) {
255 Attrs |= OldAttrList[i].Attrs;
256 ++i;
257 }
258
Devang Patel05988662008-09-25 21:00:45 +0000259 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Chris Lattner58d74912008-03-12 17:45:29 +0000260
261 // Copy attributes for arguments after this one.
262 NewAttrList.insert(NewAttrList.end(),
263 OldAttrList.begin()+i, OldAttrList.end());
264 }
265
Jay Foade3e51c02009-05-21 09:52:38 +0000266 return get(NewAttrList.data(), NewAttrList.size());
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000267}
268
Devang Patel05988662008-09-25 21:00:45 +0000269AttrListPtr AttrListPtr::removeAttr(unsigned Idx, Attributes Attrs) const {
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000270#ifndef NDEBUG
271 // FIXME it is not obvious how this should work for alignment.
272 // For now, say we can't pass in alignment, which no current use does.
Devang Patel05988662008-09-25 21:00:45 +0000273 assert(!(Attrs & Attribute::Alignment) && "Attempt to exclude alignment!");
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000274#endif
Devang Patel05988662008-09-25 21:00:45 +0000275 if (AttrList == 0) return AttrListPtr();
Chris Lattner58d74912008-03-12 17:45:29 +0000276
Devang Patel05988662008-09-25 21:00:45 +0000277 Attributes OldAttrs = getAttributes(Idx);
Devang Pateleaf42ab2008-09-23 23:03:40 +0000278 Attributes NewAttrs = OldAttrs & ~Attrs;
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000279 if (NewAttrs == OldAttrs)
Chris Lattner58d74912008-03-12 17:45:29 +0000280 return *this;
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000281
Devang Patel05988662008-09-25 21:00:45 +0000282 SmallVector<AttributeWithIndex, 8> NewAttrList;
283 const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs;
Chris Lattner58d74912008-03-12 17:45:29 +0000284 unsigned i = 0, e = OldAttrList.size();
285
286 // Copy attributes for arguments before this one.
287 for (; i != e && OldAttrList[i].Index < Idx; ++i)
288 NewAttrList.push_back(OldAttrList[i]);
289
290 // If there are attributes already at this index, merge them in.
291 assert(OldAttrList[i].Index == Idx && "Attribute isn't set?");
292 Attrs = OldAttrList[i].Attrs & ~Attrs;
293 ++i;
294 if (Attrs) // If any attributes left for this parameter, add them.
Devang Patel05988662008-09-25 21:00:45 +0000295 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Chris Lattner58d74912008-03-12 17:45:29 +0000296
297 // Copy attributes for arguments after this one.
298 NewAttrList.insert(NewAttrList.end(),
299 OldAttrList.begin()+i, OldAttrList.end());
300
Jay Foade3e51c02009-05-21 09:52:38 +0000301 return get(NewAttrList.data(), NewAttrList.size());
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000302}
303
Devang Patel05988662008-09-25 21:00:45 +0000304void AttrListPtr::dump() const {
Chris Lattner58d74912008-03-12 17:45:29 +0000305 cerr << "PAL[ ";
306 for (unsigned i = 0; i < getNumSlots(); ++i) {
Devang Patel05988662008-09-25 21:00:45 +0000307 const AttributeWithIndex &PAWI = getSlot(i);
Chris Lattner58d74912008-03-12 17:45:29 +0000308 cerr << "{" << PAWI.Index << "," << PAWI.Attrs << "} ";
309 }
310
311 cerr << "]\n";
Duncan Sandsad9a9e12008-01-06 18:27:01 +0000312}