blob: 0f3efa7b9ddef7b4534a7e2ca9be962ad5e06441 [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 Patel05988662008-09-25 21:00:45 +000062 if (Attrs & Attribute::Alignment) {
Dale Johannesen6167c3f2008-02-19 23:51:49 +000063 Result += "align ";
Nick Lewycky1ed86d72009-01-11 17:02:06 +000064 Result += utostr(Attribute::getAlignmentFromAttrs(Attrs));
Dale Johannesen6167c3f2008-02-19 23:51:49 +000065 Result += " ";
66 }
Dan Gohmanc3be0fd2008-08-05 15:51:44 +000067 // Trim the trailing space.
Nick Lewycky73ddd4f2008-12-19 09:38:31 +000068 assert(!Result.empty() && "Unknown attribute!");
Dan Gohmanc3be0fd2008-08-05 15:51:44 +000069 Result.erase(Result.end()-1);
Chris Lattner50ee9dd2008-01-02 23:42:30 +000070 return Result;
71}
72
Devang Patel05988662008-09-25 21:00:45 +000073Attributes Attribute::typeIncompatible(const Type *Ty) {
Devang Pateleaf42ab2008-09-23 23:03:40 +000074 Attributes Incompatible = None;
Chris Lattner58d74912008-03-12 17:45:29 +000075
76 if (!Ty->isInteger())
77 // Attributes that only apply to integers.
78 Incompatible |= SExt | ZExt;
79
80 if (!isa<PointerType>(Ty))
81 // Attributes that only apply to pointers.
Nick Lewycky73ddd4f2008-12-19 09:38:31 +000082 Incompatible |= ByVal | Nest | NoAlias | StructRet | NoCapture;
Chris Lattner58d74912008-03-12 17:45:29 +000083
84 return Incompatible;
Chris Lattner50ee9dd2008-01-02 23:42:30 +000085}
86
Chris Lattner58d74912008-03-12 17:45:29 +000087//===----------------------------------------------------------------------===//
Devang Patel1e480002008-09-24 00:29:49 +000088// AttributeListImpl Definition
Chris Lattner58d74912008-03-12 17:45:29 +000089//===----------------------------------------------------------------------===//
Chris Lattner50ee9dd2008-01-02 23:42:30 +000090
Chris Lattner58d74912008-03-12 17:45:29 +000091namespace llvm {
Devang Patel1e480002008-09-24 00:29:49 +000092class AttributeListImpl : public FoldingSetNode {
Chris Lattner58d74912008-03-12 17:45:29 +000093 unsigned RefCount;
94
Devang Patel05988662008-09-25 21:00:45 +000095 // AttributesList is uniqued, these should not be publicly available.
Devang Patel1e480002008-09-24 00:29:49 +000096 void operator=(const AttributeListImpl &); // Do not implement
97 AttributeListImpl(const AttributeListImpl &); // Do not implement
98 ~AttributeListImpl(); // Private implementation
Chris Lattner58d74912008-03-12 17:45:29 +000099public:
Devang Patel05988662008-09-25 21:00:45 +0000100 SmallVector<AttributeWithIndex, 4> Attrs;
Chris Lattner58d74912008-03-12 17:45:29 +0000101
Devang Patel05988662008-09-25 21:00:45 +0000102 AttributeListImpl(const AttributeWithIndex *Attr, unsigned NumAttrs)
Chris Lattner58d74912008-03-12 17:45:29 +0000103 : Attrs(Attr, Attr+NumAttrs) {
104 RefCount = 0;
105 }
106
107 void AddRef() { ++RefCount; }
108 void DropRef() { if (--RefCount == 0) delete this; }
109
110 void Profile(FoldingSetNodeID &ID) const {
111 Profile(ID, &Attrs[0], Attrs.size());
112 }
Devang Patel05988662008-09-25 21:00:45 +0000113 static void Profile(FoldingSetNodeID &ID, const AttributeWithIndex *Attr,
Chris Lattner58d74912008-03-12 17:45:29 +0000114 unsigned NumAttrs) {
115 for (unsigned i = 0; i != NumAttrs; ++i)
116 ID.AddInteger(uint64_t(Attr[i].Attrs) << 32 | unsigned(Attr[i].Index));
117 }
118};
119}
120
Devang Patel05988662008-09-25 21:00:45 +0000121static ManagedStatic<FoldingSet<AttributeListImpl> > AttributesLists;
Chris Lattner58d74912008-03-12 17:45:29 +0000122
Devang Patel1e480002008-09-24 00:29:49 +0000123AttributeListImpl::~AttributeListImpl() {
Devang Patel05988662008-09-25 21:00:45 +0000124 AttributesLists->RemoveNode(this);
Chris Lattner58d74912008-03-12 17:45:29 +0000125}
126
127
Devang Patel05988662008-09-25 21:00:45 +0000128AttrListPtr AttrListPtr::get(const AttributeWithIndex *Attrs, unsigned NumAttrs) {
129 // If there are no attributes then return a null AttributesList pointer.
Chris Lattner58d74912008-03-12 17:45:29 +0000130 if (NumAttrs == 0)
Devang Patel05988662008-09-25 21:00:45 +0000131 return AttrListPtr();
Chris Lattner58d74912008-03-12 17:45:29 +0000132
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000133#ifndef NDEBUG
Chris Lattner58d74912008-03-12 17:45:29 +0000134 for (unsigned i = 0; i != NumAttrs; ++i) {
Devang Patel05988662008-09-25 21:00:45 +0000135 assert(Attrs[i].Attrs != Attribute::None &&
136 "Pointless attribute!");
Chris Lattner58d74912008-03-12 17:45:29 +0000137 assert((!i || Attrs[i-1].Index < Attrs[i].Index) &&
Devang Patel05988662008-09-25 21:00:45 +0000138 "Misordered AttributesList!");
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000139 }
140#endif
Chris Lattner58d74912008-03-12 17:45:29 +0000141
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000142 // Otherwise, build a key to look up the existing attributes.
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000143 FoldingSetNodeID ID;
Devang Patel1e480002008-09-24 00:29:49 +0000144 AttributeListImpl::Profile(ID, Attrs, NumAttrs);
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000145 void *InsertPos;
Devang Patel1e480002008-09-24 00:29:49 +0000146 AttributeListImpl *PAL =
Devang Patel05988662008-09-25 21:00:45 +0000147 AttributesLists->FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner58d74912008-03-12 17:45:29 +0000148
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000149 // If we didn't find any existing attributes of the same shape then
150 // create a new one and insert it.
151 if (!PAL) {
Devang Patel1e480002008-09-24 00:29:49 +0000152 PAL = new AttributeListImpl(Attrs, NumAttrs);
Devang Patel05988662008-09-25 21:00:45 +0000153 AttributesLists->InsertNode(PAL, InsertPos);
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000154 }
Chris Lattner58d74912008-03-12 17:45:29 +0000155
Devang Patel05988662008-09-25 21:00:45 +0000156 // Return the AttributesList that we found or created.
157 return AttrListPtr(PAL);
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000158}
159
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000160
Chris Lattner58d74912008-03-12 17:45:29 +0000161//===----------------------------------------------------------------------===//
Devang Patel05988662008-09-25 21:00:45 +0000162// AttrListPtr Method Implementations
Chris Lattner58d74912008-03-12 17:45:29 +0000163//===----------------------------------------------------------------------===//
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000164
Devang Patel05988662008-09-25 21:00:45 +0000165AttrListPtr::AttrListPtr(AttributeListImpl *LI) : AttrList(LI) {
Chris Lattner58d74912008-03-12 17:45:29 +0000166 if (LI) LI->AddRef();
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000167}
168
Devang Patel05988662008-09-25 21:00:45 +0000169AttrListPtr::AttrListPtr(const AttrListPtr &P) : AttrList(P.AttrList) {
170 if (AttrList) AttrList->AddRef();
Chris Lattner58d74912008-03-12 17:45:29 +0000171}
172
Devang Patel05988662008-09-25 21:00:45 +0000173const AttrListPtr &AttrListPtr::operator=(const AttrListPtr &RHS) {
174 if (AttrList == RHS.AttrList) return *this;
175 if (AttrList) AttrList->DropRef();
176 AttrList = RHS.AttrList;
177 if (AttrList) AttrList->AddRef();
Chris Lattner58d74912008-03-12 17:45:29 +0000178 return *this;
179}
180
Devang Patel05988662008-09-25 21:00:45 +0000181AttrListPtr::~AttrListPtr() {
182 if (AttrList) AttrList->DropRef();
Chris Lattner58d74912008-03-12 17:45:29 +0000183}
184
185/// getNumSlots - Return the number of slots used in this attribute list.
186/// This is the number of arguments that have an attribute set on them
187/// (including the function itself).
Devang Patel05988662008-09-25 21:00:45 +0000188unsigned AttrListPtr::getNumSlots() const {
189 return AttrList ? AttrList->Attrs.size() : 0;
Chris Lattner58d74912008-03-12 17:45:29 +0000190}
191
Devang Patel05988662008-09-25 21:00:45 +0000192/// getSlot - Return the AttributeWithIndex at the specified slot. This
193/// holds a number plus a set of attributes.
194const AttributeWithIndex &AttrListPtr::getSlot(unsigned Slot) const {
195 assert(AttrList && Slot < AttrList->Attrs.size() && "Slot # out of range!");
196 return AttrList->Attrs[Slot];
Chris Lattner58d74912008-03-12 17:45:29 +0000197}
198
199
Devang Patel05988662008-09-25 21:00:45 +0000200/// getAttributes - The attributes for the specified index are
201/// returned. Attributes for the result are denoted with Idx = 0.
Devang Pateld9b4a5f2008-09-23 22:35:17 +0000202/// Function notes are denoted with idx = ~0.
Devang Patel05988662008-09-25 21:00:45 +0000203Attributes AttrListPtr::getAttributes(unsigned Idx) const {
204 if (AttrList == 0) return Attribute::None;
Chris Lattner58d74912008-03-12 17:45:29 +0000205
Devang Patel05988662008-09-25 21:00:45 +0000206 const SmallVector<AttributeWithIndex, 4> &Attrs = AttrList->Attrs;
Chris Lattner58d74912008-03-12 17:45:29 +0000207 for (unsigned i = 0, e = Attrs.size(); i != e && Attrs[i].Index <= Idx; ++i)
208 if (Attrs[i].Index == Idx)
209 return Attrs[i].Attrs;
Devang Patel05988662008-09-25 21:00:45 +0000210 return Attribute::None;
Chris Lattner58d74912008-03-12 17:45:29 +0000211}
212
213/// hasAttrSomewhere - Return true if the specified attribute is set for at
214/// least one parameter or for the return value.
Devang Patel05988662008-09-25 21:00:45 +0000215bool AttrListPtr::hasAttrSomewhere(Attributes Attr) const {
216 if (AttrList == 0) return false;
Chris Lattner58d74912008-03-12 17:45:29 +0000217
Devang Patel05988662008-09-25 21:00:45 +0000218 const SmallVector<AttributeWithIndex, 4> &Attrs = AttrList->Attrs;
Chris Lattner58d74912008-03-12 17:45:29 +0000219 for (unsigned i = 0, e = Attrs.size(); i != e; ++i)
220 if (Attrs[i].Attrs & Attr)
221 return true;
222 return false;
223}
224
225
Devang Patel05988662008-09-25 21:00:45 +0000226AttrListPtr AttrListPtr::addAttr(unsigned Idx, Attributes Attrs) const {
227 Attributes OldAttrs = getAttributes(Idx);
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000228#ifndef NDEBUG
229 // FIXME it is not obvious how this should work for alignment.
230 // For now, say we can't change a known alignment.
Devang Patel05988662008-09-25 21:00:45 +0000231 Attributes OldAlign = OldAttrs & Attribute::Alignment;
232 Attributes NewAlign = Attrs & Attribute::Alignment;
Anton Korobeynikov4aefd6b2008-02-20 12:07:57 +0000233 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000234 "Attempt to change alignment!");
235#endif
Chris Lattner58d74912008-03-12 17:45:29 +0000236
Devang Pateleaf42ab2008-09-23 23:03:40 +0000237 Attributes NewAttrs = OldAttrs | Attrs;
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000238 if (NewAttrs == OldAttrs)
Chris Lattner58d74912008-03-12 17:45:29 +0000239 return *this;
240
Devang Patel05988662008-09-25 21:00:45 +0000241 SmallVector<AttributeWithIndex, 8> NewAttrList;
242 if (AttrList == 0)
243 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Chris Lattner58d74912008-03-12 17:45:29 +0000244 else {
Devang Patel05988662008-09-25 21:00:45 +0000245 const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs;
Chris Lattner58d74912008-03-12 17:45:29 +0000246 unsigned i = 0, e = OldAttrList.size();
247 // Copy attributes for arguments before this one.
248 for (; i != e && OldAttrList[i].Index < Idx; ++i)
249 NewAttrList.push_back(OldAttrList[i]);
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000250
Chris Lattner58d74912008-03-12 17:45:29 +0000251 // If there are attributes already at this index, merge them in.
252 if (i != e && OldAttrList[i].Index == Idx) {
253 Attrs |= OldAttrList[i].Attrs;
254 ++i;
255 }
256
Devang Patel05988662008-09-25 21:00:45 +0000257 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Chris Lattner58d74912008-03-12 17:45:29 +0000258
259 // Copy attributes for arguments after this one.
260 NewAttrList.insert(NewAttrList.end(),
261 OldAttrList.begin()+i, OldAttrList.end());
262 }
263
264 return get(&NewAttrList[0], NewAttrList.size());
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000265}
266
Devang Patel05988662008-09-25 21:00:45 +0000267AttrListPtr AttrListPtr::removeAttr(unsigned Idx, Attributes Attrs) const {
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000268#ifndef NDEBUG
269 // FIXME it is not obvious how this should work for alignment.
270 // For now, say we can't pass in alignment, which no current use does.
Devang Patel05988662008-09-25 21:00:45 +0000271 assert(!(Attrs & Attribute::Alignment) && "Attempt to exclude alignment!");
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000272#endif
Devang Patel05988662008-09-25 21:00:45 +0000273 if (AttrList == 0) return AttrListPtr();
Chris Lattner58d74912008-03-12 17:45:29 +0000274
Devang Patel05988662008-09-25 21:00:45 +0000275 Attributes OldAttrs = getAttributes(Idx);
Devang Pateleaf42ab2008-09-23 23:03:40 +0000276 Attributes NewAttrs = OldAttrs & ~Attrs;
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000277 if (NewAttrs == OldAttrs)
Chris Lattner58d74912008-03-12 17:45:29 +0000278 return *this;
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000279
Devang Patel05988662008-09-25 21:00:45 +0000280 SmallVector<AttributeWithIndex, 8> NewAttrList;
281 const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs;
Chris Lattner58d74912008-03-12 17:45:29 +0000282 unsigned i = 0, e = OldAttrList.size();
283
284 // Copy attributes for arguments before this one.
285 for (; i != e && OldAttrList[i].Index < Idx; ++i)
286 NewAttrList.push_back(OldAttrList[i]);
287
288 // If there are attributes already at this index, merge them in.
289 assert(OldAttrList[i].Index == Idx && "Attribute isn't set?");
290 Attrs = OldAttrList[i].Attrs & ~Attrs;
291 ++i;
292 if (Attrs) // If any attributes left for this parameter, add them.
Devang Patel05988662008-09-25 21:00:45 +0000293 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Chris Lattner58d74912008-03-12 17:45:29 +0000294
295 // Copy attributes for arguments after this one.
296 NewAttrList.insert(NewAttrList.end(),
297 OldAttrList.begin()+i, OldAttrList.end());
298
299 return get(&NewAttrList[0], NewAttrList.size());
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000300}
301
Devang Patel05988662008-09-25 21:00:45 +0000302void AttrListPtr::dump() const {
Chris Lattner58d74912008-03-12 17:45:29 +0000303 cerr << "PAL[ ";
304 for (unsigned i = 0; i < getNumSlots(); ++i) {
Devang Patel05988662008-09-25 21:00:45 +0000305 const AttributeWithIndex &PAWI = getSlot(i);
Chris Lattner58d74912008-03-12 17:45:29 +0000306 cerr << "{" << PAWI.Index << "," << PAWI.Attrs << "} ";
307 }
308
309 cerr << "]\n";
Duncan Sandsad9a9e12008-01-06 18:27:01 +0000310}