blob: 92acc111be952f21ddcc3b2570c525492ceb7757 [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 ";
Bill Wendlingccb67a3d2008-11-13 01:02:14 +000056 if (Attrs & Attribute::StackProtect)
57 Result += "ssp ";
58 if (Attrs & Attribute::StackProtectReq)
59 Result += "sspreq ";
Devang Patel4c758ea2008-09-25 21:00:45 +000060 if (Attrs & Attribute::Alignment) {
Dale Johannesen11a555e2008-02-19 23:51:49 +000061 Result += "align ";
Devang Patel4c758ea2008-09-25 21:00:45 +000062 Result += utostr((Attrs & Attribute::Alignment) >> 16);
Dale Johannesen11a555e2008-02-19 23:51:49 +000063 Result += " ";
64 }
Dan Gohman1a70bcc2008-08-05 15:51:44 +000065 // Trim the trailing space.
66 Result.erase(Result.end()-1);
Chris Lattner3e13b8c2008-01-02 23:42:30 +000067 return Result;
68}
69
Devang Patel4c758ea2008-09-25 21:00:45 +000070Attributes Attribute::typeIncompatible(const Type *Ty) {
Devang Patelba3fa6c2008-09-23 23:03:40 +000071 Attributes Incompatible = None;
Chris Lattner8a923e72008-03-12 17:45:29 +000072
73 if (!Ty->isInteger())
74 // Attributes that only apply to integers.
75 Incompatible |= SExt | ZExt;
76
77 if (!isa<PointerType>(Ty))
78 // Attributes that only apply to pointers.
79 Incompatible |= ByVal | Nest | NoAlias | StructRet;
80
81 return Incompatible;
Chris Lattner3e13b8c2008-01-02 23:42:30 +000082}
83
Chris Lattner8a923e72008-03-12 17:45:29 +000084//===----------------------------------------------------------------------===//
Devang Patel00095052008-09-24 00:29:49 +000085// AttributeListImpl Definition
Chris Lattner8a923e72008-03-12 17:45:29 +000086//===----------------------------------------------------------------------===//
Chris Lattner3e13b8c2008-01-02 23:42:30 +000087
Chris Lattner8a923e72008-03-12 17:45:29 +000088namespace llvm {
Devang Patel00095052008-09-24 00:29:49 +000089class AttributeListImpl : public FoldingSetNode {
Chris Lattner8a923e72008-03-12 17:45:29 +000090 unsigned RefCount;
91
Devang Patel4c758ea2008-09-25 21:00:45 +000092 // AttributesList is uniqued, these should not be publicly available.
Devang Patel00095052008-09-24 00:29:49 +000093 void operator=(const AttributeListImpl &); // Do not implement
94 AttributeListImpl(const AttributeListImpl &); // Do not implement
95 ~AttributeListImpl(); // Private implementation
Chris Lattner8a923e72008-03-12 17:45:29 +000096public:
Devang Patel4c758ea2008-09-25 21:00:45 +000097 SmallVector<AttributeWithIndex, 4> Attrs;
Chris Lattner8a923e72008-03-12 17:45:29 +000098
Devang Patel4c758ea2008-09-25 21:00:45 +000099 AttributeListImpl(const AttributeWithIndex *Attr, unsigned NumAttrs)
Chris Lattner8a923e72008-03-12 17:45:29 +0000100 : Attrs(Attr, Attr+NumAttrs) {
101 RefCount = 0;
102 }
103
104 void AddRef() { ++RefCount; }
105 void DropRef() { if (--RefCount == 0) delete this; }
106
107 void Profile(FoldingSetNodeID &ID) const {
108 Profile(ID, &Attrs[0], Attrs.size());
109 }
Devang Patel4c758ea2008-09-25 21:00:45 +0000110 static void Profile(FoldingSetNodeID &ID, const AttributeWithIndex *Attr,
Chris Lattner8a923e72008-03-12 17:45:29 +0000111 unsigned NumAttrs) {
112 for (unsigned i = 0; i != NumAttrs; ++i)
113 ID.AddInteger(uint64_t(Attr[i].Attrs) << 32 | unsigned(Attr[i].Index));
114 }
115};
116}
117
Devang Patel4c758ea2008-09-25 21:00:45 +0000118static ManagedStatic<FoldingSet<AttributeListImpl> > AttributesLists;
Chris Lattner8a923e72008-03-12 17:45:29 +0000119
Devang Patel00095052008-09-24 00:29:49 +0000120AttributeListImpl::~AttributeListImpl() {
Devang Patel4c758ea2008-09-25 21:00:45 +0000121 AttributesLists->RemoveNode(this);
Chris Lattner8a923e72008-03-12 17:45:29 +0000122}
123
124
Devang Patel4c758ea2008-09-25 21:00:45 +0000125AttrListPtr AttrListPtr::get(const AttributeWithIndex *Attrs, unsigned NumAttrs) {
126 // If there are no attributes then return a null AttributesList pointer.
Chris Lattner8a923e72008-03-12 17:45:29 +0000127 if (NumAttrs == 0)
Devang Patel4c758ea2008-09-25 21:00:45 +0000128 return AttrListPtr();
Chris Lattner8a923e72008-03-12 17:45:29 +0000129
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000130#ifndef NDEBUG
Chris Lattner8a923e72008-03-12 17:45:29 +0000131 for (unsigned i = 0; i != NumAttrs; ++i) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000132 assert(Attrs[i].Attrs != Attribute::None &&
133 "Pointless attribute!");
Chris Lattner8a923e72008-03-12 17:45:29 +0000134 assert((!i || Attrs[i-1].Index < Attrs[i].Index) &&
Devang Patel4c758ea2008-09-25 21:00:45 +0000135 "Misordered AttributesList!");
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000136 }
137#endif
Chris Lattner8a923e72008-03-12 17:45:29 +0000138
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000139 // Otherwise, build a key to look up the existing attributes.
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000140 FoldingSetNodeID ID;
Devang Patel00095052008-09-24 00:29:49 +0000141 AttributeListImpl::Profile(ID, Attrs, NumAttrs);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000142 void *InsertPos;
Devang Patel00095052008-09-24 00:29:49 +0000143 AttributeListImpl *PAL =
Devang Patel4c758ea2008-09-25 21:00:45 +0000144 AttributesLists->FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner8a923e72008-03-12 17:45:29 +0000145
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000146 // If we didn't find any existing attributes of the same shape then
147 // create a new one and insert it.
148 if (!PAL) {
Devang Patel00095052008-09-24 00:29:49 +0000149 PAL = new AttributeListImpl(Attrs, NumAttrs);
Devang Patel4c758ea2008-09-25 21:00:45 +0000150 AttributesLists->InsertNode(PAL, InsertPos);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000151 }
Chris Lattner8a923e72008-03-12 17:45:29 +0000152
Devang Patel4c758ea2008-09-25 21:00:45 +0000153 // Return the AttributesList that we found or created.
154 return AttrListPtr(PAL);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000155}
156
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000157
Chris Lattner8a923e72008-03-12 17:45:29 +0000158//===----------------------------------------------------------------------===//
Devang Patel4c758ea2008-09-25 21:00:45 +0000159// AttrListPtr Method Implementations
Chris Lattner8a923e72008-03-12 17:45:29 +0000160//===----------------------------------------------------------------------===//
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000161
Devang Patel4c758ea2008-09-25 21:00:45 +0000162AttrListPtr::AttrListPtr(AttributeListImpl *LI) : AttrList(LI) {
Chris Lattner8a923e72008-03-12 17:45:29 +0000163 if (LI) LI->AddRef();
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000164}
165
Devang Patel4c758ea2008-09-25 21:00:45 +0000166AttrListPtr::AttrListPtr(const AttrListPtr &P) : AttrList(P.AttrList) {
167 if (AttrList) AttrList->AddRef();
Chris Lattner8a923e72008-03-12 17:45:29 +0000168}
169
Devang Patel4c758ea2008-09-25 21:00:45 +0000170const AttrListPtr &AttrListPtr::operator=(const AttrListPtr &RHS) {
171 if (AttrList == RHS.AttrList) return *this;
172 if (AttrList) AttrList->DropRef();
173 AttrList = RHS.AttrList;
174 if (AttrList) AttrList->AddRef();
Chris Lattner8a923e72008-03-12 17:45:29 +0000175 return *this;
176}
177
Devang Patel4c758ea2008-09-25 21:00:45 +0000178AttrListPtr::~AttrListPtr() {
179 if (AttrList) AttrList->DropRef();
Chris Lattner8a923e72008-03-12 17:45:29 +0000180}
181
182/// getNumSlots - Return the number of slots used in this attribute list.
183/// This is the number of arguments that have an attribute set on them
184/// (including the function itself).
Devang Patel4c758ea2008-09-25 21:00:45 +0000185unsigned AttrListPtr::getNumSlots() const {
186 return AttrList ? AttrList->Attrs.size() : 0;
Chris Lattner8a923e72008-03-12 17:45:29 +0000187}
188
Devang Patel4c758ea2008-09-25 21:00:45 +0000189/// getSlot - Return the AttributeWithIndex at the specified slot. This
190/// holds a number plus a set of attributes.
191const AttributeWithIndex &AttrListPtr::getSlot(unsigned Slot) const {
192 assert(AttrList && Slot < AttrList->Attrs.size() && "Slot # out of range!");
193 return AttrList->Attrs[Slot];
Chris Lattner8a923e72008-03-12 17:45:29 +0000194}
195
196
Devang Patel4c758ea2008-09-25 21:00:45 +0000197/// getAttributes - The attributes for the specified index are
198/// returned. Attributes for the result are denoted with Idx = 0.
Devang Patel82fed672008-09-23 22:35:17 +0000199/// Function notes are denoted with idx = ~0.
Devang Patel4c758ea2008-09-25 21:00:45 +0000200Attributes AttrListPtr::getAttributes(unsigned Idx) const {
201 if (AttrList == 0) return Attribute::None;
Chris Lattner8a923e72008-03-12 17:45:29 +0000202
Devang Patel4c758ea2008-09-25 21:00:45 +0000203 const SmallVector<AttributeWithIndex, 4> &Attrs = AttrList->Attrs;
Chris Lattner8a923e72008-03-12 17:45:29 +0000204 for (unsigned i = 0, e = Attrs.size(); i != e && Attrs[i].Index <= Idx; ++i)
205 if (Attrs[i].Index == Idx)
206 return Attrs[i].Attrs;
Devang Patel4c758ea2008-09-25 21:00:45 +0000207 return Attribute::None;
Chris Lattner8a923e72008-03-12 17:45:29 +0000208}
209
210/// hasAttrSomewhere - Return true if the specified attribute is set for at
211/// least one parameter or for the return value.
Devang Patel4c758ea2008-09-25 21:00:45 +0000212bool AttrListPtr::hasAttrSomewhere(Attributes Attr) const {
213 if (AttrList == 0) return false;
Chris Lattner8a923e72008-03-12 17:45:29 +0000214
Devang Patel4c758ea2008-09-25 21:00:45 +0000215 const SmallVector<AttributeWithIndex, 4> &Attrs = AttrList->Attrs;
Chris Lattner8a923e72008-03-12 17:45:29 +0000216 for (unsigned i = 0, e = Attrs.size(); i != e; ++i)
217 if (Attrs[i].Attrs & Attr)
218 return true;
219 return false;
220}
221
222
Devang Patel4c758ea2008-09-25 21:00:45 +0000223AttrListPtr AttrListPtr::addAttr(unsigned Idx, Attributes Attrs) const {
224 Attributes OldAttrs = getAttributes(Idx);
Dale Johannesen11a555e2008-02-19 23:51:49 +0000225#ifndef NDEBUG
226 // FIXME it is not obvious how this should work for alignment.
227 // For now, say we can't change a known alignment.
Devang Patel4c758ea2008-09-25 21:00:45 +0000228 Attributes OldAlign = OldAttrs & Attribute::Alignment;
229 Attributes NewAlign = Attrs & Attribute::Alignment;
Anton Korobeynikov18991d72008-02-20 12:07:57 +0000230 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
Dale Johannesen11a555e2008-02-19 23:51:49 +0000231 "Attempt to change alignment!");
232#endif
Chris Lattner8a923e72008-03-12 17:45:29 +0000233
Devang Patelba3fa6c2008-09-23 23:03:40 +0000234 Attributes NewAttrs = OldAttrs | Attrs;
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000235 if (NewAttrs == OldAttrs)
Chris Lattner8a923e72008-03-12 17:45:29 +0000236 return *this;
237
Devang Patel4c758ea2008-09-25 21:00:45 +0000238 SmallVector<AttributeWithIndex, 8> NewAttrList;
239 if (AttrList == 0)
240 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Chris Lattner8a923e72008-03-12 17:45:29 +0000241 else {
Devang Patel4c758ea2008-09-25 21:00:45 +0000242 const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs;
Chris Lattner8a923e72008-03-12 17:45:29 +0000243 unsigned i = 0, e = OldAttrList.size();
244 // Copy attributes for arguments before this one.
245 for (; i != e && OldAttrList[i].Index < Idx; ++i)
246 NewAttrList.push_back(OldAttrList[i]);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000247
Chris Lattner8a923e72008-03-12 17:45:29 +0000248 // If there are attributes already at this index, merge them in.
249 if (i != e && OldAttrList[i].Index == Idx) {
250 Attrs |= OldAttrList[i].Attrs;
251 ++i;
252 }
253
Devang Patel4c758ea2008-09-25 21:00:45 +0000254 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Chris Lattner8a923e72008-03-12 17:45:29 +0000255
256 // Copy attributes for arguments after this one.
257 NewAttrList.insert(NewAttrList.end(),
258 OldAttrList.begin()+i, OldAttrList.end());
259 }
260
261 return get(&NewAttrList[0], NewAttrList.size());
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000262}
263
Devang Patel4c758ea2008-09-25 21:00:45 +0000264AttrListPtr AttrListPtr::removeAttr(unsigned Idx, Attributes Attrs) const {
Dale Johannesen11a555e2008-02-19 23:51:49 +0000265#ifndef NDEBUG
266 // FIXME it is not obvious how this should work for alignment.
267 // For now, say we can't pass in alignment, which no current use does.
Devang Patel4c758ea2008-09-25 21:00:45 +0000268 assert(!(Attrs & Attribute::Alignment) && "Attempt to exclude alignment!");
Dale Johannesen11a555e2008-02-19 23:51:49 +0000269#endif
Devang Patel4c758ea2008-09-25 21:00:45 +0000270 if (AttrList == 0) return AttrListPtr();
Chris Lattner8a923e72008-03-12 17:45:29 +0000271
Devang Patel4c758ea2008-09-25 21:00:45 +0000272 Attributes OldAttrs = getAttributes(Idx);
Devang Patelba3fa6c2008-09-23 23:03:40 +0000273 Attributes NewAttrs = OldAttrs & ~Attrs;
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000274 if (NewAttrs == OldAttrs)
Chris Lattner8a923e72008-03-12 17:45:29 +0000275 return *this;
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000276
Devang Patel4c758ea2008-09-25 21:00:45 +0000277 SmallVector<AttributeWithIndex, 8> NewAttrList;
278 const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs;
Chris Lattner8a923e72008-03-12 17:45:29 +0000279 unsigned i = 0, e = OldAttrList.size();
280
281 // Copy attributes for arguments before this one.
282 for (; i != e && OldAttrList[i].Index < Idx; ++i)
283 NewAttrList.push_back(OldAttrList[i]);
284
285 // If there are attributes already at this index, merge them in.
286 assert(OldAttrList[i].Index == Idx && "Attribute isn't set?");
287 Attrs = OldAttrList[i].Attrs & ~Attrs;
288 ++i;
289 if (Attrs) // If any attributes left for this parameter, add them.
Devang Patel4c758ea2008-09-25 21:00:45 +0000290 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Chris Lattner8a923e72008-03-12 17:45:29 +0000291
292 // Copy attributes for arguments after this one.
293 NewAttrList.insert(NewAttrList.end(),
294 OldAttrList.begin()+i, OldAttrList.end());
295
296 return get(&NewAttrList[0], NewAttrList.size());
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000297}
298
Devang Patel4c758ea2008-09-25 21:00:45 +0000299void AttrListPtr::dump() const {
Chris Lattner8a923e72008-03-12 17:45:29 +0000300 cerr << "PAL[ ";
301 for (unsigned i = 0; i < getNumSlots(); ++i) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000302 const AttributeWithIndex &PAWI = getSlot(i);
Chris Lattner8a923e72008-03-12 17:45:29 +0000303 cerr << "{" << PAWI.Index << "," << PAWI.Attrs << "} ";
304 }
305
306 cerr << "]\n";
Duncan Sands404eb052008-01-06 18:27:01 +0000307}