blob: 433b79d77c9ba2da3934dfd9a79b98794449e82a [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"
18#include "llvm/Support/Streams.h"
Chris Lattner3e13b8c2008-01-02 23:42:30 +000019#include "llvm/Support/ManagedStatic.h"
20using namespace llvm;
21
Chris Lattner8a923e72008-03-12 17:45:29 +000022//===----------------------------------------------------------------------===//
Devang Patel4c758ea2008-09-25 21:00:45 +000023// Attribute Function Definitions
Chris Lattner8a923e72008-03-12 17:45:29 +000024//===----------------------------------------------------------------------===//
Chris Lattnerd0e1f102008-01-03 00:10:22 +000025
Devang Patel4c758ea2008-09-25 21:00:45 +000026std::string Attribute::getAsString(Attributes Attrs) {
Chris Lattner3e13b8c2008-01-02 23:42:30 +000027 std::string Result;
Devang Patel4c758ea2008-09-25 21:00:45 +000028 if (Attrs & Attribute::ZExt)
Chris Lattner3e13b8c2008-01-02 23:42:30 +000029 Result += "zeroext ";
Devang Patel4c758ea2008-09-25 21:00:45 +000030 if (Attrs & Attribute::SExt)
Chris Lattner3e13b8c2008-01-02 23:42:30 +000031 Result += "signext ";
Devang Patel4c758ea2008-09-25 21:00:45 +000032 if (Attrs & Attribute::NoReturn)
Chris Lattner3e13b8c2008-01-02 23:42:30 +000033 Result += "noreturn ";
Devang Patel4c758ea2008-09-25 21:00:45 +000034 if (Attrs & Attribute::NoUnwind)
Chris Lattner3e13b8c2008-01-02 23:42:30 +000035 Result += "nounwind ";
Devang Patel4c758ea2008-09-25 21:00:45 +000036 if (Attrs & Attribute::InReg)
Chris Lattner3e13b8c2008-01-02 23:42:30 +000037 Result += "inreg ";
Devang Patel4c758ea2008-09-25 21:00:45 +000038 if (Attrs & Attribute::NoAlias)
Chris Lattner3e13b8c2008-01-02 23:42:30 +000039 Result += "noalias ";
Devang Patel4c758ea2008-09-25 21:00:45 +000040 if (Attrs & Attribute::StructRet)
Chris Lattner3e13b8c2008-01-02 23:42:30 +000041 Result += "sret ";
Devang Patel4c758ea2008-09-25 21:00:45 +000042 if (Attrs & Attribute::ByVal)
Chris Lattner3e13b8c2008-01-02 23:42:30 +000043 Result += "byval ";
Devang Patel4c758ea2008-09-25 21:00:45 +000044 if (Attrs & Attribute::Nest)
Chris Lattner3e13b8c2008-01-02 23:42:30 +000045 Result += "nest ";
Devang Patel4c758ea2008-09-25 21:00:45 +000046 if (Attrs & Attribute::ReadNone)
Chris Lattner3e13b8c2008-01-02 23:42:30 +000047 Result += "readnone ";
Devang Patel4c758ea2008-09-25 21:00:45 +000048 if (Attrs & Attribute::ReadOnly)
Chris Lattner3e13b8c2008-01-02 23:42:30 +000049 Result += "readonly ";
Devang Patela05633e2008-09-26 22:53:05 +000050 if (Attrs & Attribute::OptimizeForSize)
51 Result += "optsize ";
52 if (Attrs & Attribute::NoInline)
53 Result += "noinline ";
54 if (Attrs & Attribute::AlwaysInline)
55 Result += "alwaysinline ";
Devang Patel4c758ea2008-09-25 21:00:45 +000056 if (Attrs & Attribute::Alignment) {
Dale Johannesen11a555e2008-02-19 23:51:49 +000057 Result += "align ";
Devang Patel4c758ea2008-09-25 21:00:45 +000058 Result += utostr((Attrs & Attribute::Alignment) >> 16);
Dale Johannesen11a555e2008-02-19 23:51:49 +000059 Result += " ";
60 }
Dan Gohman1a70bcc2008-08-05 15:51:44 +000061 // Trim the trailing space.
62 Result.erase(Result.end()-1);
Chris Lattner3e13b8c2008-01-02 23:42:30 +000063 return Result;
64}
65
Devang Patel4c758ea2008-09-25 21:00:45 +000066Attributes Attribute::typeIncompatible(const Type *Ty) {
Devang Patelba3fa6c2008-09-23 23:03:40 +000067 Attributes Incompatible = None;
Chris Lattner8a923e72008-03-12 17:45:29 +000068
69 if (!Ty->isInteger())
70 // Attributes that only apply to integers.
71 Incompatible |= SExt | ZExt;
72
73 if (!isa<PointerType>(Ty))
74 // Attributes that only apply to pointers.
75 Incompatible |= ByVal | Nest | NoAlias | StructRet;
76
77 return Incompatible;
Chris Lattner3e13b8c2008-01-02 23:42:30 +000078}
79
Chris Lattner8a923e72008-03-12 17:45:29 +000080//===----------------------------------------------------------------------===//
Devang Patel00095052008-09-24 00:29:49 +000081// AttributeListImpl Definition
Chris Lattner8a923e72008-03-12 17:45:29 +000082//===----------------------------------------------------------------------===//
Chris Lattner3e13b8c2008-01-02 23:42:30 +000083
Chris Lattner8a923e72008-03-12 17:45:29 +000084namespace llvm {
Devang Patel00095052008-09-24 00:29:49 +000085class AttributeListImpl : public FoldingSetNode {
Chris Lattner8a923e72008-03-12 17:45:29 +000086 unsigned RefCount;
87
Devang Patel4c758ea2008-09-25 21:00:45 +000088 // AttributesList is uniqued, these should not be publicly available.
Devang Patel00095052008-09-24 00:29:49 +000089 void operator=(const AttributeListImpl &); // Do not implement
90 AttributeListImpl(const AttributeListImpl &); // Do not implement
91 ~AttributeListImpl(); // Private implementation
Chris Lattner8a923e72008-03-12 17:45:29 +000092public:
Devang Patel4c758ea2008-09-25 21:00:45 +000093 SmallVector<AttributeWithIndex, 4> Attrs;
Chris Lattner8a923e72008-03-12 17:45:29 +000094
Devang Patel4c758ea2008-09-25 21:00:45 +000095 AttributeListImpl(const AttributeWithIndex *Attr, unsigned NumAttrs)
Chris Lattner8a923e72008-03-12 17:45:29 +000096 : Attrs(Attr, Attr+NumAttrs) {
97 RefCount = 0;
98 }
99
100 void AddRef() { ++RefCount; }
101 void DropRef() { if (--RefCount == 0) delete this; }
102
103 void Profile(FoldingSetNodeID &ID) const {
104 Profile(ID, &Attrs[0], Attrs.size());
105 }
Devang Patel4c758ea2008-09-25 21:00:45 +0000106 static void Profile(FoldingSetNodeID &ID, const AttributeWithIndex *Attr,
Chris Lattner8a923e72008-03-12 17:45:29 +0000107 unsigned NumAttrs) {
108 for (unsigned i = 0; i != NumAttrs; ++i)
109 ID.AddInteger(uint64_t(Attr[i].Attrs) << 32 | unsigned(Attr[i].Index));
110 }
111};
112}
113
Devang Patel4c758ea2008-09-25 21:00:45 +0000114static ManagedStatic<FoldingSet<AttributeListImpl> > AttributesLists;
Chris Lattner8a923e72008-03-12 17:45:29 +0000115
Devang Patel00095052008-09-24 00:29:49 +0000116AttributeListImpl::~AttributeListImpl() {
Devang Patel4c758ea2008-09-25 21:00:45 +0000117 AttributesLists->RemoveNode(this);
Chris Lattner8a923e72008-03-12 17:45:29 +0000118}
119
120
Devang Patel4c758ea2008-09-25 21:00:45 +0000121AttrListPtr AttrListPtr::get(const AttributeWithIndex *Attrs, unsigned NumAttrs) {
122 // If there are no attributes then return a null AttributesList pointer.
Chris Lattner8a923e72008-03-12 17:45:29 +0000123 if (NumAttrs == 0)
Devang Patel4c758ea2008-09-25 21:00:45 +0000124 return AttrListPtr();
Chris Lattner8a923e72008-03-12 17:45:29 +0000125
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000126#ifndef NDEBUG
Chris Lattner8a923e72008-03-12 17:45:29 +0000127 for (unsigned i = 0; i != NumAttrs; ++i) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000128 assert(Attrs[i].Attrs != Attribute::None &&
129 "Pointless attribute!");
Chris Lattner8a923e72008-03-12 17:45:29 +0000130 assert((!i || Attrs[i-1].Index < Attrs[i].Index) &&
Devang Patel4c758ea2008-09-25 21:00:45 +0000131 "Misordered AttributesList!");
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000132 }
133#endif
Chris Lattner8a923e72008-03-12 17:45:29 +0000134
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000135 // Otherwise, build a key to look up the existing attributes.
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000136 FoldingSetNodeID ID;
Devang Patel00095052008-09-24 00:29:49 +0000137 AttributeListImpl::Profile(ID, Attrs, NumAttrs);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000138 void *InsertPos;
Devang Patel00095052008-09-24 00:29:49 +0000139 AttributeListImpl *PAL =
Devang Patel4c758ea2008-09-25 21:00:45 +0000140 AttributesLists->FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner8a923e72008-03-12 17:45:29 +0000141
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000142 // If we didn't find any existing attributes of the same shape then
143 // create a new one and insert it.
144 if (!PAL) {
Devang Patel00095052008-09-24 00:29:49 +0000145 PAL = new AttributeListImpl(Attrs, NumAttrs);
Devang Patel4c758ea2008-09-25 21:00:45 +0000146 AttributesLists->InsertNode(PAL, InsertPos);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000147 }
Chris Lattner8a923e72008-03-12 17:45:29 +0000148
Devang Patel4c758ea2008-09-25 21:00:45 +0000149 // Return the AttributesList that we found or created.
150 return AttrListPtr(PAL);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000151}
152
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000153
Chris Lattner8a923e72008-03-12 17:45:29 +0000154//===----------------------------------------------------------------------===//
Devang Patel4c758ea2008-09-25 21:00:45 +0000155// AttrListPtr Method Implementations
Chris Lattner8a923e72008-03-12 17:45:29 +0000156//===----------------------------------------------------------------------===//
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000157
Devang Patel4c758ea2008-09-25 21:00:45 +0000158AttrListPtr::AttrListPtr(AttributeListImpl *LI) : AttrList(LI) {
Chris Lattner8a923e72008-03-12 17:45:29 +0000159 if (LI) LI->AddRef();
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000160}
161
Devang Patel4c758ea2008-09-25 21:00:45 +0000162AttrListPtr::AttrListPtr(const AttrListPtr &P) : AttrList(P.AttrList) {
163 if (AttrList) AttrList->AddRef();
Chris Lattner8a923e72008-03-12 17:45:29 +0000164}
165
Devang Patel4c758ea2008-09-25 21:00:45 +0000166const AttrListPtr &AttrListPtr::operator=(const AttrListPtr &RHS) {
167 if (AttrList == RHS.AttrList) return *this;
168 if (AttrList) AttrList->DropRef();
169 AttrList = RHS.AttrList;
170 if (AttrList) AttrList->AddRef();
Chris Lattner8a923e72008-03-12 17:45:29 +0000171 return *this;
172}
173
Devang Patel4c758ea2008-09-25 21:00:45 +0000174AttrListPtr::~AttrListPtr() {
175 if (AttrList) AttrList->DropRef();
Chris Lattner8a923e72008-03-12 17:45:29 +0000176}
177
178/// getNumSlots - Return the number of slots used in this attribute list.
179/// This is the number of arguments that have an attribute set on them
180/// (including the function itself).
Devang Patel4c758ea2008-09-25 21:00:45 +0000181unsigned AttrListPtr::getNumSlots() const {
182 return AttrList ? AttrList->Attrs.size() : 0;
Chris Lattner8a923e72008-03-12 17:45:29 +0000183}
184
Devang Patel4c758ea2008-09-25 21:00:45 +0000185/// getSlot - Return the AttributeWithIndex at the specified slot. This
186/// holds a number plus a set of attributes.
187const AttributeWithIndex &AttrListPtr::getSlot(unsigned Slot) const {
188 assert(AttrList && Slot < AttrList->Attrs.size() && "Slot # out of range!");
189 return AttrList->Attrs[Slot];
Chris Lattner8a923e72008-03-12 17:45:29 +0000190}
191
192
Devang Patel4c758ea2008-09-25 21:00:45 +0000193/// getAttributes - The attributes for the specified index are
194/// returned. Attributes for the result are denoted with Idx = 0.
Devang Patel82fed672008-09-23 22:35:17 +0000195/// Function notes are denoted with idx = ~0.
Devang Patel4c758ea2008-09-25 21:00:45 +0000196Attributes AttrListPtr::getAttributes(unsigned Idx) const {
197 if (AttrList == 0) return Attribute::None;
Chris Lattner8a923e72008-03-12 17:45:29 +0000198
Devang Patel4c758ea2008-09-25 21:00:45 +0000199 const SmallVector<AttributeWithIndex, 4> &Attrs = AttrList->Attrs;
Chris Lattner8a923e72008-03-12 17:45:29 +0000200 for (unsigned i = 0, e = Attrs.size(); i != e && Attrs[i].Index <= Idx; ++i)
201 if (Attrs[i].Index == Idx)
202 return Attrs[i].Attrs;
Devang Patel4c758ea2008-09-25 21:00:45 +0000203 return Attribute::None;
Chris Lattner8a923e72008-03-12 17:45:29 +0000204}
205
206/// hasAttrSomewhere - Return true if the specified attribute is set for at
207/// least one parameter or for the return value.
Devang Patel4c758ea2008-09-25 21:00:45 +0000208bool AttrListPtr::hasAttrSomewhere(Attributes Attr) const {
209 if (AttrList == 0) return false;
Chris Lattner8a923e72008-03-12 17:45:29 +0000210
Devang Patel4c758ea2008-09-25 21:00:45 +0000211 const SmallVector<AttributeWithIndex, 4> &Attrs = AttrList->Attrs;
Chris Lattner8a923e72008-03-12 17:45:29 +0000212 for (unsigned i = 0, e = Attrs.size(); i != e; ++i)
213 if (Attrs[i].Attrs & Attr)
214 return true;
215 return false;
216}
217
218
Devang Patel4c758ea2008-09-25 21:00:45 +0000219AttrListPtr AttrListPtr::addAttr(unsigned Idx, Attributes Attrs) const {
220 Attributes OldAttrs = getAttributes(Idx);
Dale Johannesen11a555e2008-02-19 23:51:49 +0000221#ifndef NDEBUG
222 // FIXME it is not obvious how this should work for alignment.
223 // For now, say we can't change a known alignment.
Devang Patel4c758ea2008-09-25 21:00:45 +0000224 Attributes OldAlign = OldAttrs & Attribute::Alignment;
225 Attributes NewAlign = Attrs & Attribute::Alignment;
Anton Korobeynikov18991d72008-02-20 12:07:57 +0000226 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
Dale Johannesen11a555e2008-02-19 23:51:49 +0000227 "Attempt to change alignment!");
228#endif
Chris Lattner8a923e72008-03-12 17:45:29 +0000229
Devang Patelba3fa6c2008-09-23 23:03:40 +0000230 Attributes NewAttrs = OldAttrs | Attrs;
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000231 if (NewAttrs == OldAttrs)
Chris Lattner8a923e72008-03-12 17:45:29 +0000232 return *this;
233
Devang Patel4c758ea2008-09-25 21:00:45 +0000234 SmallVector<AttributeWithIndex, 8> NewAttrList;
235 if (AttrList == 0)
236 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Chris Lattner8a923e72008-03-12 17:45:29 +0000237 else {
Devang Patel4c758ea2008-09-25 21:00:45 +0000238 const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs;
Chris Lattner8a923e72008-03-12 17:45:29 +0000239 unsigned i = 0, e = OldAttrList.size();
240 // Copy attributes for arguments before this one.
241 for (; i != e && OldAttrList[i].Index < Idx; ++i)
242 NewAttrList.push_back(OldAttrList[i]);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000243
Chris Lattner8a923e72008-03-12 17:45:29 +0000244 // If there are attributes already at this index, merge them in.
245 if (i != e && OldAttrList[i].Index == Idx) {
246 Attrs |= OldAttrList[i].Attrs;
247 ++i;
248 }
249
Devang Patel4c758ea2008-09-25 21:00:45 +0000250 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Chris Lattner8a923e72008-03-12 17:45:29 +0000251
252 // Copy attributes for arguments after this one.
253 NewAttrList.insert(NewAttrList.end(),
254 OldAttrList.begin()+i, OldAttrList.end());
255 }
256
257 return get(&NewAttrList[0], NewAttrList.size());
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000258}
259
Devang Patel4c758ea2008-09-25 21:00:45 +0000260AttrListPtr AttrListPtr::removeAttr(unsigned Idx, Attributes Attrs) const {
Dale Johannesen11a555e2008-02-19 23:51:49 +0000261#ifndef NDEBUG
262 // FIXME it is not obvious how this should work for alignment.
263 // For now, say we can't pass in alignment, which no current use does.
Devang Patel4c758ea2008-09-25 21:00:45 +0000264 assert(!(Attrs & Attribute::Alignment) && "Attempt to exclude alignment!");
Dale Johannesen11a555e2008-02-19 23:51:49 +0000265#endif
Devang Patel4c758ea2008-09-25 21:00:45 +0000266 if (AttrList == 0) return AttrListPtr();
Chris Lattner8a923e72008-03-12 17:45:29 +0000267
Devang Patel4c758ea2008-09-25 21:00:45 +0000268 Attributes OldAttrs = getAttributes(Idx);
Devang Patelba3fa6c2008-09-23 23:03:40 +0000269 Attributes NewAttrs = OldAttrs & ~Attrs;
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000270 if (NewAttrs == OldAttrs)
Chris Lattner8a923e72008-03-12 17:45:29 +0000271 return *this;
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000272
Devang Patel4c758ea2008-09-25 21:00:45 +0000273 SmallVector<AttributeWithIndex, 8> NewAttrList;
274 const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs;
Chris Lattner8a923e72008-03-12 17:45:29 +0000275 unsigned i = 0, e = OldAttrList.size();
276
277 // Copy attributes for arguments before this one.
278 for (; i != e && OldAttrList[i].Index < Idx; ++i)
279 NewAttrList.push_back(OldAttrList[i]);
280
281 // If there are attributes already at this index, merge them in.
282 assert(OldAttrList[i].Index == Idx && "Attribute isn't set?");
283 Attrs = OldAttrList[i].Attrs & ~Attrs;
284 ++i;
285 if (Attrs) // If any attributes left for this parameter, add them.
Devang Patel4c758ea2008-09-25 21:00:45 +0000286 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Chris Lattner8a923e72008-03-12 17:45:29 +0000287
288 // Copy attributes for arguments after this one.
289 NewAttrList.insert(NewAttrList.end(),
290 OldAttrList.begin()+i, OldAttrList.end());
291
292 return get(&NewAttrList[0], NewAttrList.size());
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000293}
294
Devang Patel4c758ea2008-09-25 21:00:45 +0000295void AttrListPtr::dump() const {
Chris Lattner8a923e72008-03-12 17:45:29 +0000296 cerr << "PAL[ ";
297 for (unsigned i = 0; i < getNumSlots(); ++i) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000298 const AttributeWithIndex &PAWI = getSlot(i);
Chris Lattner8a923e72008-03-12 17:45:29 +0000299 cerr << "{" << PAWI.Index << "," << PAWI.Attrs << "} ";
300 }
301
302 cerr << "]\n";
Duncan Sands404eb052008-01-06 18:27:01 +0000303}