blob: b2f2150bf6ee46cdd4ff3c62b3010793dc465701 [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 Patel4c758ea2008-09-25 21:00:45 +000050 if (Attrs & Attribute::Alignment) {
Dale Johannesen11a555e2008-02-19 23:51:49 +000051 Result += "align ";
Devang Patel4c758ea2008-09-25 21:00:45 +000052 Result += utostr((Attrs & Attribute::Alignment) >> 16);
Dale Johannesen11a555e2008-02-19 23:51:49 +000053 Result += " ";
54 }
Dan Gohman1a70bcc2008-08-05 15:51:44 +000055 // Trim the trailing space.
56 Result.erase(Result.end()-1);
Chris Lattner3e13b8c2008-01-02 23:42:30 +000057 return Result;
58}
59
Devang Patel4c758ea2008-09-25 21:00:45 +000060Attributes Attribute::typeIncompatible(const Type *Ty) {
Devang Patelba3fa6c2008-09-23 23:03:40 +000061 Attributes Incompatible = None;
Chris Lattner8a923e72008-03-12 17:45:29 +000062
63 if (!Ty->isInteger())
64 // Attributes that only apply to integers.
65 Incompatible |= SExt | ZExt;
66
67 if (!isa<PointerType>(Ty))
68 // Attributes that only apply to pointers.
69 Incompatible |= ByVal | Nest | NoAlias | StructRet;
70
71 return Incompatible;
Chris Lattner3e13b8c2008-01-02 23:42:30 +000072}
73
Chris Lattner8a923e72008-03-12 17:45:29 +000074//===----------------------------------------------------------------------===//
Devang Patel00095052008-09-24 00:29:49 +000075// AttributeListImpl Definition
Chris Lattner8a923e72008-03-12 17:45:29 +000076//===----------------------------------------------------------------------===//
Chris Lattner3e13b8c2008-01-02 23:42:30 +000077
Chris Lattner8a923e72008-03-12 17:45:29 +000078namespace llvm {
Devang Patel00095052008-09-24 00:29:49 +000079class AttributeListImpl : public FoldingSetNode {
Chris Lattner8a923e72008-03-12 17:45:29 +000080 unsigned RefCount;
81
Devang Patel4c758ea2008-09-25 21:00:45 +000082 // AttributesList is uniqued, these should not be publicly available.
Devang Patel00095052008-09-24 00:29:49 +000083 void operator=(const AttributeListImpl &); // Do not implement
84 AttributeListImpl(const AttributeListImpl &); // Do not implement
85 ~AttributeListImpl(); // Private implementation
Chris Lattner8a923e72008-03-12 17:45:29 +000086public:
Devang Patel4c758ea2008-09-25 21:00:45 +000087 SmallVector<AttributeWithIndex, 4> Attrs;
Chris Lattner8a923e72008-03-12 17:45:29 +000088
Devang Patel4c758ea2008-09-25 21:00:45 +000089 AttributeListImpl(const AttributeWithIndex *Attr, unsigned NumAttrs)
Chris Lattner8a923e72008-03-12 17:45:29 +000090 : Attrs(Attr, Attr+NumAttrs) {
91 RefCount = 0;
92 }
93
94 void AddRef() { ++RefCount; }
95 void DropRef() { if (--RefCount == 0) delete this; }
96
97 void Profile(FoldingSetNodeID &ID) const {
98 Profile(ID, &Attrs[0], Attrs.size());
99 }
Devang Patel4c758ea2008-09-25 21:00:45 +0000100 static void Profile(FoldingSetNodeID &ID, const AttributeWithIndex *Attr,
Chris Lattner8a923e72008-03-12 17:45:29 +0000101 unsigned NumAttrs) {
102 for (unsigned i = 0; i != NumAttrs; ++i)
103 ID.AddInteger(uint64_t(Attr[i].Attrs) << 32 | unsigned(Attr[i].Index));
104 }
105};
106}
107
Devang Patel4c758ea2008-09-25 21:00:45 +0000108static ManagedStatic<FoldingSet<AttributeListImpl> > AttributesLists;
Chris Lattner8a923e72008-03-12 17:45:29 +0000109
Devang Patel00095052008-09-24 00:29:49 +0000110AttributeListImpl::~AttributeListImpl() {
Devang Patel4c758ea2008-09-25 21:00:45 +0000111 AttributesLists->RemoveNode(this);
Chris Lattner8a923e72008-03-12 17:45:29 +0000112}
113
114
Devang Patel4c758ea2008-09-25 21:00:45 +0000115AttrListPtr AttrListPtr::get(const AttributeWithIndex *Attrs, unsigned NumAttrs) {
116 // If there are no attributes then return a null AttributesList pointer.
Chris Lattner8a923e72008-03-12 17:45:29 +0000117 if (NumAttrs == 0)
Devang Patel4c758ea2008-09-25 21:00:45 +0000118 return AttrListPtr();
Chris Lattner8a923e72008-03-12 17:45:29 +0000119
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000120#ifndef NDEBUG
Chris Lattner8a923e72008-03-12 17:45:29 +0000121 for (unsigned i = 0; i != NumAttrs; ++i) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000122 assert(Attrs[i].Attrs != Attribute::None &&
123 "Pointless attribute!");
Chris Lattner8a923e72008-03-12 17:45:29 +0000124 assert((!i || Attrs[i-1].Index < Attrs[i].Index) &&
Devang Patel4c758ea2008-09-25 21:00:45 +0000125 "Misordered AttributesList!");
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000126 }
127#endif
Chris Lattner8a923e72008-03-12 17:45:29 +0000128
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000129 // Otherwise, build a key to look up the existing attributes.
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000130 FoldingSetNodeID ID;
Devang Patel00095052008-09-24 00:29:49 +0000131 AttributeListImpl::Profile(ID, Attrs, NumAttrs);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000132 void *InsertPos;
Devang Patel00095052008-09-24 00:29:49 +0000133 AttributeListImpl *PAL =
Devang Patel4c758ea2008-09-25 21:00:45 +0000134 AttributesLists->FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner8a923e72008-03-12 17:45:29 +0000135
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000136 // If we didn't find any existing attributes of the same shape then
137 // create a new one and insert it.
138 if (!PAL) {
Devang Patel00095052008-09-24 00:29:49 +0000139 PAL = new AttributeListImpl(Attrs, NumAttrs);
Devang Patel4c758ea2008-09-25 21:00:45 +0000140 AttributesLists->InsertNode(PAL, InsertPos);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000141 }
Chris Lattner8a923e72008-03-12 17:45:29 +0000142
Devang Patel4c758ea2008-09-25 21:00:45 +0000143 // Return the AttributesList that we found or created.
144 return AttrListPtr(PAL);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000145}
146
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000147
Chris Lattner8a923e72008-03-12 17:45:29 +0000148//===----------------------------------------------------------------------===//
Devang Patel4c758ea2008-09-25 21:00:45 +0000149// AttrListPtr Method Implementations
Chris Lattner8a923e72008-03-12 17:45:29 +0000150//===----------------------------------------------------------------------===//
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000151
Devang Patel4c758ea2008-09-25 21:00:45 +0000152AttrListPtr::AttrListPtr(AttributeListImpl *LI) : AttrList(LI) {
Chris Lattner8a923e72008-03-12 17:45:29 +0000153 if (LI) LI->AddRef();
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000154}
155
Devang Patel4c758ea2008-09-25 21:00:45 +0000156AttrListPtr::AttrListPtr(const AttrListPtr &P) : AttrList(P.AttrList) {
157 if (AttrList) AttrList->AddRef();
Chris Lattner8a923e72008-03-12 17:45:29 +0000158}
159
Devang Patel4c758ea2008-09-25 21:00:45 +0000160const AttrListPtr &AttrListPtr::operator=(const AttrListPtr &RHS) {
161 if (AttrList == RHS.AttrList) return *this;
162 if (AttrList) AttrList->DropRef();
163 AttrList = RHS.AttrList;
164 if (AttrList) AttrList->AddRef();
Chris Lattner8a923e72008-03-12 17:45:29 +0000165 return *this;
166}
167
Devang Patel4c758ea2008-09-25 21:00:45 +0000168AttrListPtr::~AttrListPtr() {
169 if (AttrList) AttrList->DropRef();
Chris Lattner8a923e72008-03-12 17:45:29 +0000170}
171
172/// getNumSlots - Return the number of slots used in this attribute list.
173/// This is the number of arguments that have an attribute set on them
174/// (including the function itself).
Devang Patel4c758ea2008-09-25 21:00:45 +0000175unsigned AttrListPtr::getNumSlots() const {
176 return AttrList ? AttrList->Attrs.size() : 0;
Chris Lattner8a923e72008-03-12 17:45:29 +0000177}
178
Devang Patel4c758ea2008-09-25 21:00:45 +0000179/// getSlot - Return the AttributeWithIndex at the specified slot. This
180/// holds a number plus a set of attributes.
181const AttributeWithIndex &AttrListPtr::getSlot(unsigned Slot) const {
182 assert(AttrList && Slot < AttrList->Attrs.size() && "Slot # out of range!");
183 return AttrList->Attrs[Slot];
Chris Lattner8a923e72008-03-12 17:45:29 +0000184}
185
186
Devang Patel4c758ea2008-09-25 21:00:45 +0000187/// getAttributes - The attributes for the specified index are
188/// returned. Attributes for the result are denoted with Idx = 0.
Devang Patel82fed672008-09-23 22:35:17 +0000189/// Function notes are denoted with idx = ~0.
Devang Patel4c758ea2008-09-25 21:00:45 +0000190Attributes AttrListPtr::getAttributes(unsigned Idx) const {
191 if (AttrList == 0) return Attribute::None;
Chris Lattner8a923e72008-03-12 17:45:29 +0000192
Devang Patel4c758ea2008-09-25 21:00:45 +0000193 const SmallVector<AttributeWithIndex, 4> &Attrs = AttrList->Attrs;
Chris Lattner8a923e72008-03-12 17:45:29 +0000194 for (unsigned i = 0, e = Attrs.size(); i != e && Attrs[i].Index <= Idx; ++i)
195 if (Attrs[i].Index == Idx)
196 return Attrs[i].Attrs;
Devang Patel4c758ea2008-09-25 21:00:45 +0000197 return Attribute::None;
Chris Lattner8a923e72008-03-12 17:45:29 +0000198}
199
200/// hasAttrSomewhere - Return true if the specified attribute is set for at
201/// least one parameter or for the return value.
Devang Patel4c758ea2008-09-25 21:00:45 +0000202bool AttrListPtr::hasAttrSomewhere(Attributes Attr) const {
203 if (AttrList == 0) return false;
Chris Lattner8a923e72008-03-12 17:45:29 +0000204
Devang Patel4c758ea2008-09-25 21:00:45 +0000205 const SmallVector<AttributeWithIndex, 4> &Attrs = AttrList->Attrs;
Chris Lattner8a923e72008-03-12 17:45:29 +0000206 for (unsigned i = 0, e = Attrs.size(); i != e; ++i)
207 if (Attrs[i].Attrs & Attr)
208 return true;
209 return false;
210}
211
212
Devang Patel4c758ea2008-09-25 21:00:45 +0000213AttrListPtr AttrListPtr::addAttr(unsigned Idx, Attributes Attrs) const {
214 Attributes OldAttrs = getAttributes(Idx);
Dale Johannesen11a555e2008-02-19 23:51:49 +0000215#ifndef NDEBUG
216 // FIXME it is not obvious how this should work for alignment.
217 // For now, say we can't change a known alignment.
Devang Patel4c758ea2008-09-25 21:00:45 +0000218 Attributes OldAlign = OldAttrs & Attribute::Alignment;
219 Attributes NewAlign = Attrs & Attribute::Alignment;
Anton Korobeynikov18991d72008-02-20 12:07:57 +0000220 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
Dale Johannesen11a555e2008-02-19 23:51:49 +0000221 "Attempt to change alignment!");
222#endif
Chris Lattner8a923e72008-03-12 17:45:29 +0000223
Devang Patelba3fa6c2008-09-23 23:03:40 +0000224 Attributes NewAttrs = OldAttrs | Attrs;
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000225 if (NewAttrs == OldAttrs)
Chris Lattner8a923e72008-03-12 17:45:29 +0000226 return *this;
227
Devang Patel4c758ea2008-09-25 21:00:45 +0000228 SmallVector<AttributeWithIndex, 8> NewAttrList;
229 if (AttrList == 0)
230 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Chris Lattner8a923e72008-03-12 17:45:29 +0000231 else {
Devang Patel4c758ea2008-09-25 21:00:45 +0000232 const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs;
Chris Lattner8a923e72008-03-12 17:45:29 +0000233 unsigned i = 0, e = OldAttrList.size();
234 // Copy attributes for arguments before this one.
235 for (; i != e && OldAttrList[i].Index < Idx; ++i)
236 NewAttrList.push_back(OldAttrList[i]);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000237
Chris Lattner8a923e72008-03-12 17:45:29 +0000238 // If there are attributes already at this index, merge them in.
239 if (i != e && OldAttrList[i].Index == Idx) {
240 Attrs |= OldAttrList[i].Attrs;
241 ++i;
242 }
243
Devang Patel4c758ea2008-09-25 21:00:45 +0000244 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Chris Lattner8a923e72008-03-12 17:45:29 +0000245
246 // Copy attributes for arguments after this one.
247 NewAttrList.insert(NewAttrList.end(),
248 OldAttrList.begin()+i, OldAttrList.end());
249 }
250
251 return get(&NewAttrList[0], NewAttrList.size());
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000252}
253
Devang Patel4c758ea2008-09-25 21:00:45 +0000254AttrListPtr AttrListPtr::removeAttr(unsigned Idx, Attributes Attrs) const {
Dale Johannesen11a555e2008-02-19 23:51:49 +0000255#ifndef NDEBUG
256 // FIXME it is not obvious how this should work for alignment.
257 // For now, say we can't pass in alignment, which no current use does.
Devang Patel4c758ea2008-09-25 21:00:45 +0000258 assert(!(Attrs & Attribute::Alignment) && "Attempt to exclude alignment!");
Dale Johannesen11a555e2008-02-19 23:51:49 +0000259#endif
Devang Patel4c758ea2008-09-25 21:00:45 +0000260 if (AttrList == 0) return AttrListPtr();
Chris Lattner8a923e72008-03-12 17:45:29 +0000261
Devang Patel4c758ea2008-09-25 21:00:45 +0000262 Attributes OldAttrs = getAttributes(Idx);
Devang Patelba3fa6c2008-09-23 23:03:40 +0000263 Attributes NewAttrs = OldAttrs & ~Attrs;
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000264 if (NewAttrs == OldAttrs)
Chris Lattner8a923e72008-03-12 17:45:29 +0000265 return *this;
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000266
Devang Patel4c758ea2008-09-25 21:00:45 +0000267 SmallVector<AttributeWithIndex, 8> NewAttrList;
268 const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs;
Chris Lattner8a923e72008-03-12 17:45:29 +0000269 unsigned i = 0, e = OldAttrList.size();
270
271 // Copy attributes for arguments before this one.
272 for (; i != e && OldAttrList[i].Index < Idx; ++i)
273 NewAttrList.push_back(OldAttrList[i]);
274
275 // If there are attributes already at this index, merge them in.
276 assert(OldAttrList[i].Index == Idx && "Attribute isn't set?");
277 Attrs = OldAttrList[i].Attrs & ~Attrs;
278 ++i;
279 if (Attrs) // If any attributes left for this parameter, add them.
Devang Patel4c758ea2008-09-25 21:00:45 +0000280 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Chris Lattner8a923e72008-03-12 17:45:29 +0000281
282 // Copy attributes for arguments after this one.
283 NewAttrList.insert(NewAttrList.end(),
284 OldAttrList.begin()+i, OldAttrList.end());
285
286 return get(&NewAttrList[0], NewAttrList.size());
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000287}
288
Devang Patel4c758ea2008-09-25 21:00:45 +0000289void AttrListPtr::dump() const {
Chris Lattner8a923e72008-03-12 17:45:29 +0000290 cerr << "PAL[ ";
291 for (unsigned i = 0; i < getNumSlots(); ++i) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000292 const AttributeWithIndex &PAWI = getSlot(i);
Chris Lattner8a923e72008-03-12 17:45:29 +0000293 cerr << "{" << PAWI.Index << "," << PAWI.Attrs << "} ";
294 }
295
296 cerr << "]\n";
Duncan Sands404eb052008-01-06 18:27:01 +0000297}