blob: b728b9284b4499fa701d7cc864a8d5584ce40c2d [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"
Michael J. Spencer447762d2010-11-29 18:16:10 +000018#include "llvm/Support/Atomic.h"
19#include "llvm/Support/Mutex.h"
David Greenef7014732010-01-05 01:29:58 +000020#include "llvm/Support/Debug.h"
Chris Lattner3e13b8c2008-01-02 23:42:30 +000021#include "llvm/Support/ManagedStatic.h"
Benjamin Kramer1a25d732009-08-23 11:37:21 +000022#include "llvm/Support/raw_ostream.h"
Chris Lattner3e13b8c2008-01-02 23:42:30 +000023using namespace llvm;
24
Chris Lattner8a923e72008-03-12 17:45:29 +000025//===----------------------------------------------------------------------===//
Devang Patel4c758ea2008-09-25 21:00:45 +000026// Attribute Function Definitions
Chris Lattner8a923e72008-03-12 17:45:29 +000027//===----------------------------------------------------------------------===//
Chris Lattnerd0e1f102008-01-03 00:10:22 +000028
Devang Patel4c758ea2008-09-25 21:00:45 +000029std::string Attribute::getAsString(Attributes Attrs) {
Chris Lattner3e13b8c2008-01-02 23:42:30 +000030 std::string Result;
Devang Patel4c758ea2008-09-25 21:00:45 +000031 if (Attrs & Attribute::ZExt)
Chris Lattner3e13b8c2008-01-02 23:42:30 +000032 Result += "zeroext ";
Devang Patel4c758ea2008-09-25 21:00:45 +000033 if (Attrs & Attribute::SExt)
Chris Lattner3e13b8c2008-01-02 23:42:30 +000034 Result += "signext ";
Devang Patel4c758ea2008-09-25 21:00:45 +000035 if (Attrs & Attribute::NoReturn)
Chris Lattner3e13b8c2008-01-02 23:42:30 +000036 Result += "noreturn ";
Devang Patel4c758ea2008-09-25 21:00:45 +000037 if (Attrs & Attribute::NoUnwind)
Chris Lattner3e13b8c2008-01-02 23:42:30 +000038 Result += "nounwind ";
Rafael Espindolafc9bae62011-05-25 03:44:17 +000039 if (Attrs & Attribute::UWTable)
40 Result += "uwtable ";
Devang Patel4c758ea2008-09-25 21:00:45 +000041 if (Attrs & Attribute::InReg)
Chris Lattner3e13b8c2008-01-02 23:42:30 +000042 Result += "inreg ";
Devang Patel4c758ea2008-09-25 21:00:45 +000043 if (Attrs & Attribute::NoAlias)
Chris Lattner3e13b8c2008-01-02 23:42:30 +000044 Result += "noalias ";
Nick Lewycky8d69f482008-12-19 09:38:31 +000045 if (Attrs & Attribute::NoCapture)
46 Result += "nocapture ";
Devang Patel4c758ea2008-09-25 21:00:45 +000047 if (Attrs & Attribute::StructRet)
Anton Korobeynikovc8ce7b082009-07-17 18:07:26 +000048 Result += "sret ";
Devang Patel4c758ea2008-09-25 21:00:45 +000049 if (Attrs & Attribute::ByVal)
Chris Lattner3e13b8c2008-01-02 23:42:30 +000050 Result += "byval ";
Devang Patel4c758ea2008-09-25 21:00:45 +000051 if (Attrs & Attribute::Nest)
Chris Lattner3e13b8c2008-01-02 23:42:30 +000052 Result += "nest ";
Devang Patel4c758ea2008-09-25 21:00:45 +000053 if (Attrs & Attribute::ReadNone)
Chris Lattner3e13b8c2008-01-02 23:42:30 +000054 Result += "readnone ";
Devang Patel4c758ea2008-09-25 21:00:45 +000055 if (Attrs & Attribute::ReadOnly)
Chris Lattner3e13b8c2008-01-02 23:42:30 +000056 Result += "readonly ";
Devang Patela05633e2008-09-26 22:53:05 +000057 if (Attrs & Attribute::OptimizeForSize)
58 Result += "optsize ";
59 if (Attrs & Attribute::NoInline)
60 Result += "noinline ";
Jakob Stoklund Olesen74bb06c2010-02-06 01:16:28 +000061 if (Attrs & Attribute::InlineHint)
62 Result += "inlinehint ";
Devang Patela05633e2008-09-26 22:53:05 +000063 if (Attrs & Attribute::AlwaysInline)
64 Result += "alwaysinline ";
Bill Wendlingccb67a3d2008-11-13 01:02:14 +000065 if (Attrs & Attribute::StackProtect)
66 Result += "ssp ";
67 if (Attrs & Attribute::StackProtectReq)
68 Result += "sspreq ";
Devang Patel72a4d2f2009-06-04 22:05:33 +000069 if (Attrs & Attribute::NoRedZone)
70 Result += "noredzone ";
Devang Pateld1c7d342009-06-05 21:57:13 +000071 if (Attrs & Attribute::NoImplicitFloat)
72 Result += "noimplicitfloat ";
Anton Korobeynikovc8ce7b082009-07-17 18:07:26 +000073 if (Attrs & Attribute::Naked)
74 Result += "naked ";
Charles Davis22fe1862010-10-25 15:37:09 +000075 if (Attrs & Attribute::Hotpatch)
76 Result += "hotpatch ";
John McCall4b7a8d62011-06-15 20:36:13 +000077 if (Attrs & Attribute::NonLazyBind)
78 Result += "nonlazybind ";
Charles Davisbe5557e2010-02-12 00:31:15 +000079 if (Attrs & Attribute::StackAlignment) {
80 Result += "alignstack(";
81 Result += utostr(Attribute::getStackAlignmentFromAttrs(Attrs));
82 Result += ") ";
83 }
Devang Patel4c758ea2008-09-25 21:00:45 +000084 if (Attrs & Attribute::Alignment) {
Dale Johannesen11a555e2008-02-19 23:51:49 +000085 Result += "align ";
Nick Lewycky93787d12009-01-11 17:02:06 +000086 Result += utostr(Attribute::getAlignmentFromAttrs(Attrs));
Dale Johannesen11a555e2008-02-19 23:51:49 +000087 Result += " ";
88 }
Dan Gohman1a70bcc2008-08-05 15:51:44 +000089 // Trim the trailing space.
Nick Lewycky8d69f482008-12-19 09:38:31 +000090 assert(!Result.empty() && "Unknown attribute!");
Dan Gohman1a70bcc2008-08-05 15:51:44 +000091 Result.erase(Result.end()-1);
Chris Lattner3e13b8c2008-01-02 23:42:30 +000092 return Result;
93}
94
Chris Lattner229907c2011-07-18 04:54:35 +000095Attributes Attribute::typeIncompatible(Type *Ty) {
Devang Patelba3fa6c2008-09-23 23:03:40 +000096 Attributes Incompatible = None;
Chris Lattner8a923e72008-03-12 17:45:29 +000097
Duncan Sands9dff9be2010-02-15 16:12:20 +000098 if (!Ty->isIntegerTy())
Chris Lattner8a923e72008-03-12 17:45:29 +000099 // Attributes that only apply to integers.
100 Incompatible |= SExt | ZExt;
101
Duncan Sands19d0b472010-02-16 11:11:14 +0000102 if (!Ty->isPointerTy())
Chris Lattner8a923e72008-03-12 17:45:29 +0000103 // Attributes that only apply to pointers.
Nick Lewycky8d69f482008-12-19 09:38:31 +0000104 Incompatible |= ByVal | Nest | NoAlias | StructRet | NoCapture;
Chris Lattner8a923e72008-03-12 17:45:29 +0000105
106 return Incompatible;
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000107}
108
Chris Lattner8a923e72008-03-12 17:45:29 +0000109//===----------------------------------------------------------------------===//
Devang Patel00095052008-09-24 00:29:49 +0000110// AttributeListImpl Definition
Chris Lattner8a923e72008-03-12 17:45:29 +0000111//===----------------------------------------------------------------------===//
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000112
Owen Anderson2e831892010-11-18 18:59:13 +0000113namespace llvm {
114 class AttributeListImpl;
115}
116
117static ManagedStatic<FoldingSet<AttributeListImpl> > AttributesLists;
Owen Anderson9b14a252010-11-09 00:27:03 +0000118
Chris Lattner8a923e72008-03-12 17:45:29 +0000119namespace llvm {
Owen Anderson9b14a252010-11-09 00:27:03 +0000120static ManagedStatic<sys::SmartMutex<true> > ALMutex;
121
Devang Patel00095052008-09-24 00:29:49 +0000122class AttributeListImpl : public FoldingSetNode {
Owen Andersonc1a3a472009-08-20 19:03:20 +0000123 sys::cas_flag RefCount;
Chris Lattner8a923e72008-03-12 17:45:29 +0000124
Devang Patel4c758ea2008-09-25 21:00:45 +0000125 // AttributesList is uniqued, these should not be publicly available.
Devang Patel00095052008-09-24 00:29:49 +0000126 void operator=(const AttributeListImpl &); // Do not implement
127 AttributeListImpl(const AttributeListImpl &); // Do not implement
128 ~AttributeListImpl(); // Private implementation
Chris Lattner8a923e72008-03-12 17:45:29 +0000129public:
Devang Patel4c758ea2008-09-25 21:00:45 +0000130 SmallVector<AttributeWithIndex, 4> Attrs;
Chris Lattner8a923e72008-03-12 17:45:29 +0000131
Devang Patel4c758ea2008-09-25 21:00:45 +0000132 AttributeListImpl(const AttributeWithIndex *Attr, unsigned NumAttrs)
Chris Lattner8a923e72008-03-12 17:45:29 +0000133 : Attrs(Attr, Attr+NumAttrs) {
134 RefCount = 0;
135 }
136
Owen Anderson9b14a252010-11-09 00:27:03 +0000137 void AddRef() {
138 sys::SmartScopedLock<true> Lock(*ALMutex);
139 ++RefCount;
140 }
Owen Andersonc1a3a472009-08-20 19:03:20 +0000141 void DropRef() {
Owen Anderson9b14a252010-11-09 00:27:03 +0000142 sys::SmartScopedLock<true> Lock(*ALMutex);
Owen Anderson2e831892010-11-18 18:59:13 +0000143 if (!AttributesLists.isConstructed())
144 return;
Owen Anderson91bfeb12010-11-09 17:47:10 +0000145 sys::cas_flag new_val = --RefCount;
Owen Anderson2d335432010-11-09 17:46:38 +0000146 if (new_val == 0)
Owen Anderson9b14a252010-11-09 00:27:03 +0000147 delete this;
Owen Andersonc1a3a472009-08-20 19:03:20 +0000148 }
Chris Lattner8a923e72008-03-12 17:45:29 +0000149
150 void Profile(FoldingSetNodeID &ID) const {
Jay Foad7d0479f2009-05-21 09:52:38 +0000151 Profile(ID, Attrs.data(), Attrs.size());
Chris Lattner8a923e72008-03-12 17:45:29 +0000152 }
Devang Patel4c758ea2008-09-25 21:00:45 +0000153 static void Profile(FoldingSetNodeID &ID, const AttributeWithIndex *Attr,
Chris Lattner8a923e72008-03-12 17:45:29 +0000154 unsigned NumAttrs) {
155 for (unsigned i = 0; i != NumAttrs; ++i)
156 ID.AddInteger(uint64_t(Attr[i].Attrs) << 32 | unsigned(Attr[i].Index));
157 }
158};
159}
160
Devang Patel00095052008-09-24 00:29:49 +0000161AttributeListImpl::~AttributeListImpl() {
Owen Anderson9b14a252010-11-09 00:27:03 +0000162 // NOTE: Lock must be acquired by caller.
Devang Patel4c758ea2008-09-25 21:00:45 +0000163 AttributesLists->RemoveNode(this);
Chris Lattner8a923e72008-03-12 17:45:29 +0000164}
165
166
Devang Patel4c758ea2008-09-25 21:00:45 +0000167AttrListPtr AttrListPtr::get(const AttributeWithIndex *Attrs, unsigned NumAttrs) {
168 // If there are no attributes then return a null AttributesList pointer.
Chris Lattner8a923e72008-03-12 17:45:29 +0000169 if (NumAttrs == 0)
Devang Patel4c758ea2008-09-25 21:00:45 +0000170 return AttrListPtr();
Chris Lattner8a923e72008-03-12 17:45:29 +0000171
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000172#ifndef NDEBUG
Chris Lattner8a923e72008-03-12 17:45:29 +0000173 for (unsigned i = 0; i != NumAttrs; ++i) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000174 assert(Attrs[i].Attrs != Attribute::None &&
175 "Pointless attribute!");
Chris Lattner8a923e72008-03-12 17:45:29 +0000176 assert((!i || Attrs[i-1].Index < Attrs[i].Index) &&
Devang Patel4c758ea2008-09-25 21:00:45 +0000177 "Misordered AttributesList!");
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000178 }
179#endif
Chris Lattner8a923e72008-03-12 17:45:29 +0000180
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000181 // Otherwise, build a key to look up the existing attributes.
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000182 FoldingSetNodeID ID;
Devang Patel00095052008-09-24 00:29:49 +0000183 AttributeListImpl::Profile(ID, Attrs, NumAttrs);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000184 void *InsertPos;
Owen Andersond91e6b02009-08-17 17:10:58 +0000185
186 sys::SmartScopedLock<true> Lock(*ALMutex);
187
Devang Patel00095052008-09-24 00:29:49 +0000188 AttributeListImpl *PAL =
Devang Patel4c758ea2008-09-25 21:00:45 +0000189 AttributesLists->FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner8a923e72008-03-12 17:45:29 +0000190
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000191 // If we didn't find any existing attributes of the same shape then
192 // create a new one and insert it.
193 if (!PAL) {
Devang Patel00095052008-09-24 00:29:49 +0000194 PAL = new AttributeListImpl(Attrs, NumAttrs);
Devang Patel4c758ea2008-09-25 21:00:45 +0000195 AttributesLists->InsertNode(PAL, InsertPos);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000196 }
Chris Lattner8a923e72008-03-12 17:45:29 +0000197
Devang Patel4c758ea2008-09-25 21:00:45 +0000198 // Return the AttributesList that we found or created.
199 return AttrListPtr(PAL);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000200}
201
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000202
Chris Lattner8a923e72008-03-12 17:45:29 +0000203//===----------------------------------------------------------------------===//
Devang Patel4c758ea2008-09-25 21:00:45 +0000204// AttrListPtr Method Implementations
Chris Lattner8a923e72008-03-12 17:45:29 +0000205//===----------------------------------------------------------------------===//
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000206
Devang Patel4c758ea2008-09-25 21:00:45 +0000207AttrListPtr::AttrListPtr(AttributeListImpl *LI) : AttrList(LI) {
Chris Lattner8a923e72008-03-12 17:45:29 +0000208 if (LI) LI->AddRef();
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000209}
210
Devang Patel4c758ea2008-09-25 21:00:45 +0000211AttrListPtr::AttrListPtr(const AttrListPtr &P) : AttrList(P.AttrList) {
212 if (AttrList) AttrList->AddRef();
Chris Lattner8a923e72008-03-12 17:45:29 +0000213}
214
Devang Patel4c758ea2008-09-25 21:00:45 +0000215const AttrListPtr &AttrListPtr::operator=(const AttrListPtr &RHS) {
Owen Anderson8dc0b042010-09-16 00:27:35 +0000216 sys::SmartScopedLock<true> Lock(*ALMutex);
Devang Patel4c758ea2008-09-25 21:00:45 +0000217 if (AttrList == RHS.AttrList) return *this;
218 if (AttrList) AttrList->DropRef();
219 AttrList = RHS.AttrList;
220 if (AttrList) AttrList->AddRef();
Chris Lattner8a923e72008-03-12 17:45:29 +0000221 return *this;
222}
223
Devang Patel4c758ea2008-09-25 21:00:45 +0000224AttrListPtr::~AttrListPtr() {
225 if (AttrList) AttrList->DropRef();
Chris Lattner8a923e72008-03-12 17:45:29 +0000226}
227
228/// getNumSlots - Return the number of slots used in this attribute list.
229/// This is the number of arguments that have an attribute set on them
230/// (including the function itself).
Devang Patel4c758ea2008-09-25 21:00:45 +0000231unsigned AttrListPtr::getNumSlots() const {
232 return AttrList ? AttrList->Attrs.size() : 0;
Chris Lattner8a923e72008-03-12 17:45:29 +0000233}
234
Devang Patel4c758ea2008-09-25 21:00:45 +0000235/// getSlot - Return the AttributeWithIndex at the specified slot. This
236/// holds a number plus a set of attributes.
237const AttributeWithIndex &AttrListPtr::getSlot(unsigned Slot) const {
238 assert(AttrList && Slot < AttrList->Attrs.size() && "Slot # out of range!");
239 return AttrList->Attrs[Slot];
Chris Lattner8a923e72008-03-12 17:45:29 +0000240}
241
242
Devang Patel4c758ea2008-09-25 21:00:45 +0000243/// getAttributes - The attributes for the specified index are
244/// returned. Attributes for the result are denoted with Idx = 0.
Devang Patel82fed672008-09-23 22:35:17 +0000245/// Function notes are denoted with idx = ~0.
Devang Patel4c758ea2008-09-25 21:00:45 +0000246Attributes AttrListPtr::getAttributes(unsigned Idx) const {
247 if (AttrList == 0) return Attribute::None;
Chris Lattner8a923e72008-03-12 17:45:29 +0000248
Devang Patel4c758ea2008-09-25 21:00:45 +0000249 const SmallVector<AttributeWithIndex, 4> &Attrs = AttrList->Attrs;
Chris Lattner8a923e72008-03-12 17:45:29 +0000250 for (unsigned i = 0, e = Attrs.size(); i != e && Attrs[i].Index <= Idx; ++i)
251 if (Attrs[i].Index == Idx)
252 return Attrs[i].Attrs;
Devang Patel4c758ea2008-09-25 21:00:45 +0000253 return Attribute::None;
Chris Lattner8a923e72008-03-12 17:45:29 +0000254}
255
256/// hasAttrSomewhere - Return true if the specified attribute is set for at
257/// least one parameter or for the return value.
Devang Patel4c758ea2008-09-25 21:00:45 +0000258bool AttrListPtr::hasAttrSomewhere(Attributes Attr) const {
259 if (AttrList == 0) return false;
Chris Lattner8a923e72008-03-12 17:45:29 +0000260
Devang Patel4c758ea2008-09-25 21:00:45 +0000261 const SmallVector<AttributeWithIndex, 4> &Attrs = AttrList->Attrs;
Chris Lattner8a923e72008-03-12 17:45:29 +0000262 for (unsigned i = 0, e = Attrs.size(); i != e; ++i)
263 if (Attrs[i].Attrs & Attr)
264 return true;
265 return false;
266}
267
268
Devang Patel4c758ea2008-09-25 21:00:45 +0000269AttrListPtr AttrListPtr::addAttr(unsigned Idx, Attributes Attrs) const {
270 Attributes OldAttrs = getAttributes(Idx);
Dale Johannesen11a555e2008-02-19 23:51:49 +0000271#ifndef NDEBUG
272 // FIXME it is not obvious how this should work for alignment.
273 // For now, say we can't change a known alignment.
Devang Patel4c758ea2008-09-25 21:00:45 +0000274 Attributes OldAlign = OldAttrs & Attribute::Alignment;
275 Attributes NewAlign = Attrs & Attribute::Alignment;
Anton Korobeynikov18991d72008-02-20 12:07:57 +0000276 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
Dale Johannesen11a555e2008-02-19 23:51:49 +0000277 "Attempt to change alignment!");
278#endif
Chris Lattner8a923e72008-03-12 17:45:29 +0000279
Devang Patelba3fa6c2008-09-23 23:03:40 +0000280 Attributes NewAttrs = OldAttrs | Attrs;
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000281 if (NewAttrs == OldAttrs)
Chris Lattner8a923e72008-03-12 17:45:29 +0000282 return *this;
283
Devang Patel4c758ea2008-09-25 21:00:45 +0000284 SmallVector<AttributeWithIndex, 8> NewAttrList;
285 if (AttrList == 0)
286 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Chris Lattner8a923e72008-03-12 17:45:29 +0000287 else {
Devang Patel4c758ea2008-09-25 21:00:45 +0000288 const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs;
Chris Lattner8a923e72008-03-12 17:45:29 +0000289 unsigned i = 0, e = OldAttrList.size();
290 // Copy attributes for arguments before this one.
291 for (; i != e && OldAttrList[i].Index < Idx; ++i)
292 NewAttrList.push_back(OldAttrList[i]);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000293
Chris Lattner8a923e72008-03-12 17:45:29 +0000294 // If there are attributes already at this index, merge them in.
295 if (i != e && OldAttrList[i].Index == Idx) {
296 Attrs |= OldAttrList[i].Attrs;
297 ++i;
298 }
299
Devang Patel4c758ea2008-09-25 21:00:45 +0000300 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Chris Lattner8a923e72008-03-12 17:45:29 +0000301
302 // Copy attributes for arguments after this one.
303 NewAttrList.insert(NewAttrList.end(),
304 OldAttrList.begin()+i, OldAttrList.end());
305 }
306
Jay Foad7d0479f2009-05-21 09:52:38 +0000307 return get(NewAttrList.data(), NewAttrList.size());
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000308}
309
Devang Patel4c758ea2008-09-25 21:00:45 +0000310AttrListPtr AttrListPtr::removeAttr(unsigned Idx, Attributes Attrs) const {
Dale Johannesen11a555e2008-02-19 23:51:49 +0000311#ifndef NDEBUG
312 // FIXME it is not obvious how this should work for alignment.
313 // For now, say we can't pass in alignment, which no current use does.
Devang Patel4c758ea2008-09-25 21:00:45 +0000314 assert(!(Attrs & Attribute::Alignment) && "Attempt to exclude alignment!");
Dale Johannesen11a555e2008-02-19 23:51:49 +0000315#endif
Devang Patel4c758ea2008-09-25 21:00:45 +0000316 if (AttrList == 0) return AttrListPtr();
Chris Lattner8a923e72008-03-12 17:45:29 +0000317
Devang Patel4c758ea2008-09-25 21:00:45 +0000318 Attributes OldAttrs = getAttributes(Idx);
Devang Patelba3fa6c2008-09-23 23:03:40 +0000319 Attributes NewAttrs = OldAttrs & ~Attrs;
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000320 if (NewAttrs == OldAttrs)
Chris Lattner8a923e72008-03-12 17:45:29 +0000321 return *this;
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000322
Devang Patel4c758ea2008-09-25 21:00:45 +0000323 SmallVector<AttributeWithIndex, 8> NewAttrList;
324 const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs;
Chris Lattner8a923e72008-03-12 17:45:29 +0000325 unsigned i = 0, e = OldAttrList.size();
326
327 // Copy attributes for arguments before this one.
328 for (; i != e && OldAttrList[i].Index < Idx; ++i)
329 NewAttrList.push_back(OldAttrList[i]);
330
331 // If there are attributes already at this index, merge them in.
332 assert(OldAttrList[i].Index == Idx && "Attribute isn't set?");
333 Attrs = OldAttrList[i].Attrs & ~Attrs;
334 ++i;
335 if (Attrs) // If any attributes left for this parameter, add them.
Devang Patel4c758ea2008-09-25 21:00:45 +0000336 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Chris Lattner8a923e72008-03-12 17:45:29 +0000337
338 // Copy attributes for arguments after this one.
339 NewAttrList.insert(NewAttrList.end(),
340 OldAttrList.begin()+i, OldAttrList.end());
341
Jay Foad7d0479f2009-05-21 09:52:38 +0000342 return get(NewAttrList.data(), NewAttrList.size());
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000343}
344
Devang Patel4c758ea2008-09-25 21:00:45 +0000345void AttrListPtr::dump() const {
David Greenef7014732010-01-05 01:29:58 +0000346 dbgs() << "PAL[ ";
Chris Lattner8a923e72008-03-12 17:45:29 +0000347 for (unsigned i = 0; i < getNumSlots(); ++i) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000348 const AttributeWithIndex &PAWI = getSlot(i);
David Greenef7014732010-01-05 01:29:58 +0000349 dbgs() << "{" << PAWI.Index << "," << PAWI.Attrs << "} ";
Chris Lattner8a923e72008-03-12 17:45:29 +0000350 }
351
David Greenef7014732010-01-05 01:29:58 +0000352 dbgs() << "]\n";
Duncan Sands404eb052008-01-06 18:27:01 +0000353}