blob: 6047d4b9f009c04195483d5f5520130f4a048106 [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
Bill Wendlingde74cf52012-09-20 14:44:42 +000029std::string Attributes::getAsString() const {
Chris Lattner3e13b8c2008-01-02 23:42:30 +000030 std::string Result;
Bill Wendlingde74cf52012-09-20 14:44:42 +000031 if (hasZExtAttr())
Chris Lattner3e13b8c2008-01-02 23:42:30 +000032 Result += "zeroext ";
Bill Wendlingde74cf52012-09-20 14:44:42 +000033 if (hasSExtAttr())
Chris Lattner3e13b8c2008-01-02 23:42:30 +000034 Result += "signext ";
Bill Wendlingde74cf52012-09-20 14:44:42 +000035 if (hasNoReturnAttr())
Chris Lattner3e13b8c2008-01-02 23:42:30 +000036 Result += "noreturn ";
Bill Wendlingde74cf52012-09-20 14:44:42 +000037 if (hasNoUnwindAttr())
Chris Lattner3e13b8c2008-01-02 23:42:30 +000038 Result += "nounwind ";
Bill Wendlingde74cf52012-09-20 14:44:42 +000039 if (hasUWTableAttr())
Rafael Espindolafc9bae62011-05-25 03:44:17 +000040 Result += "uwtable ";
Bill Wendlingde74cf52012-09-20 14:44:42 +000041 if (hasReturnsTwiceAttr())
Rafael Espindolacc349c82011-10-03 14:45:37 +000042 Result += "returns_twice ";
Bill Wendlingde74cf52012-09-20 14:44:42 +000043 if (hasInRegAttr())
Chris Lattner3e13b8c2008-01-02 23:42:30 +000044 Result += "inreg ";
Bill Wendlingde74cf52012-09-20 14:44:42 +000045 if (hasNoAliasAttr())
Chris Lattner3e13b8c2008-01-02 23:42:30 +000046 Result += "noalias ";
Bill Wendlingde74cf52012-09-20 14:44:42 +000047 if (hasNoCaptureAttr())
Nick Lewycky8d69f482008-12-19 09:38:31 +000048 Result += "nocapture ";
Bill Wendlingde74cf52012-09-20 14:44:42 +000049 if (hasStructRetAttr())
Anton Korobeynikovc8ce7b082009-07-17 18:07:26 +000050 Result += "sret ";
Bill Wendlingde74cf52012-09-20 14:44:42 +000051 if (hasByValAttr())
Chris Lattner3e13b8c2008-01-02 23:42:30 +000052 Result += "byval ";
Bill Wendlingde74cf52012-09-20 14:44:42 +000053 if (hasNestAttr())
Chris Lattner3e13b8c2008-01-02 23:42:30 +000054 Result += "nest ";
Bill Wendlingde74cf52012-09-20 14:44:42 +000055 if (hasReadNoneAttr())
Chris Lattner3e13b8c2008-01-02 23:42:30 +000056 Result += "readnone ";
Bill Wendlingde74cf52012-09-20 14:44:42 +000057 if (hasReadOnlyAttr())
Chris Lattner3e13b8c2008-01-02 23:42:30 +000058 Result += "readonly ";
Bill Wendlingde74cf52012-09-20 14:44:42 +000059 if (hasOptimizeForSizeAttr())
Devang Patela05633e2008-09-26 22:53:05 +000060 Result += "optsize ";
Bill Wendlingde74cf52012-09-20 14:44:42 +000061 if (hasNoInlineAttr())
Devang Patela05633e2008-09-26 22:53:05 +000062 Result += "noinline ";
Bill Wendlingde74cf52012-09-20 14:44:42 +000063 if (hasInlineHintAttr())
Jakob Stoklund Olesen74bb06c2010-02-06 01:16:28 +000064 Result += "inlinehint ";
Bill Wendlingde74cf52012-09-20 14:44:42 +000065 if (hasAlwaysInlineAttr())
Devang Patela05633e2008-09-26 22:53:05 +000066 Result += "alwaysinline ";
Bill Wendlingde74cf52012-09-20 14:44:42 +000067 if (hasStackProtectAttr())
Bill Wendlingccb67a3d2008-11-13 01:02:14 +000068 Result += "ssp ";
Bill Wendlingde74cf52012-09-20 14:44:42 +000069 if (hasStackProtectReqAttr())
Bill Wendlingccb67a3d2008-11-13 01:02:14 +000070 Result += "sspreq ";
Bill Wendlingde74cf52012-09-20 14:44:42 +000071 if (hasNoRedZoneAttr())
Devang Patel72a4d2f2009-06-04 22:05:33 +000072 Result += "noredzone ";
Bill Wendlingde74cf52012-09-20 14:44:42 +000073 if (hasNoImplicitFloatAttr())
Devang Pateld1c7d342009-06-05 21:57:13 +000074 Result += "noimplicitfloat ";
Bill Wendlingde74cf52012-09-20 14:44:42 +000075 if (hasNakedAttr())
Anton Korobeynikovc8ce7b082009-07-17 18:07:26 +000076 Result += "naked ";
Bill Wendlingde74cf52012-09-20 14:44:42 +000077 if (hasNonLazyBindAttr())
John McCall4b7a8d62011-06-15 20:36:13 +000078 Result += "nonlazybind ";
Bill Wendlingde74cf52012-09-20 14:44:42 +000079 if (hasAddressSafetyAttr())
Kostya Serebryanya5054ad2012-01-20 17:56:17 +000080 Result += "address_safety ";
Bill Wendlingde74cf52012-09-20 14:44:42 +000081 if (*this & Attribute::StackAlignment) { // FIXME
Charles Davisbe5557e2010-02-12 00:31:15 +000082 Result += "alignstack(";
Bill Wendlingde74cf52012-09-20 14:44:42 +000083 Result += utostr(Attribute::getStackAlignmentFromAttrs(*this));
Charles Davisbe5557e2010-02-12 00:31:15 +000084 Result += ") ";
85 }
Bill Wendlingde74cf52012-09-20 14:44:42 +000086 if (*this & Attribute::Alignment) { // FIXME
Dale Johannesen11a555e2008-02-19 23:51:49 +000087 Result += "align ";
Bill Wendlingde74cf52012-09-20 14:44:42 +000088 Result += utostr(Attribute::getAlignmentFromAttrs(*this));
Dale Johannesen11a555e2008-02-19 23:51:49 +000089 Result += " ";
90 }
Dan Gohman1a70bcc2008-08-05 15:51:44 +000091 // Trim the trailing space.
Nick Lewycky8d69f482008-12-19 09:38:31 +000092 assert(!Result.empty() && "Unknown attribute!");
Dan Gohman1a70bcc2008-08-05 15:51:44 +000093 Result.erase(Result.end()-1);
Chris Lattner3e13b8c2008-01-02 23:42:30 +000094 return Result;
95}
96
Chris Lattner229907c2011-07-18 04:54:35 +000097Attributes Attribute::typeIncompatible(Type *Ty) {
Devang Patelba3fa6c2008-09-23 23:03:40 +000098 Attributes Incompatible = None;
Chris Lattner8a923e72008-03-12 17:45:29 +000099
Duncan Sands9dff9be2010-02-15 16:12:20 +0000100 if (!Ty->isIntegerTy())
Chris Lattner8a923e72008-03-12 17:45:29 +0000101 // Attributes that only apply to integers.
102 Incompatible |= SExt | ZExt;
103
Duncan Sands19d0b472010-02-16 11:11:14 +0000104 if (!Ty->isPointerTy())
Chris Lattner8a923e72008-03-12 17:45:29 +0000105 // Attributes that only apply to pointers.
Nick Lewycky8d69f482008-12-19 09:38:31 +0000106 Incompatible |= ByVal | Nest | NoAlias | StructRet | NoCapture;
Chris Lattner8a923e72008-03-12 17:45:29 +0000107
108 return Incompatible;
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000109}
110
Chris Lattner8a923e72008-03-12 17:45:29 +0000111//===----------------------------------------------------------------------===//
Devang Patel00095052008-09-24 00:29:49 +0000112// AttributeListImpl Definition
Chris Lattner8a923e72008-03-12 17:45:29 +0000113//===----------------------------------------------------------------------===//
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000114
Owen Anderson2e831892010-11-18 18:59:13 +0000115namespace llvm {
116 class AttributeListImpl;
117}
118
119static ManagedStatic<FoldingSet<AttributeListImpl> > AttributesLists;
Owen Anderson9b14a252010-11-09 00:27:03 +0000120
Chris Lattner8a923e72008-03-12 17:45:29 +0000121namespace llvm {
Owen Anderson9b14a252010-11-09 00:27:03 +0000122static ManagedStatic<sys::SmartMutex<true> > ALMutex;
123
Devang Patel00095052008-09-24 00:29:49 +0000124class AttributeListImpl : public FoldingSetNode {
Owen Andersonc1a3a472009-08-20 19:03:20 +0000125 sys::cas_flag RefCount;
Chris Lattner8a923e72008-03-12 17:45:29 +0000126
Devang Patel4c758ea2008-09-25 21:00:45 +0000127 // AttributesList is uniqued, these should not be publicly available.
Craig Topperb1d83e82012-09-18 02:01:41 +0000128 void operator=(const AttributeListImpl &) LLVM_DELETED_FUNCTION;
129 AttributeListImpl(const AttributeListImpl &) LLVM_DELETED_FUNCTION;
Devang Patel00095052008-09-24 00:29:49 +0000130 ~AttributeListImpl(); // Private implementation
Chris Lattner8a923e72008-03-12 17:45:29 +0000131public:
Devang Patel4c758ea2008-09-25 21:00:45 +0000132 SmallVector<AttributeWithIndex, 4> Attrs;
Chris Lattner8a923e72008-03-12 17:45:29 +0000133
Chris Lattner3cb6f832012-05-28 01:47:44 +0000134 AttributeListImpl(ArrayRef<AttributeWithIndex> attrs)
135 : Attrs(attrs.begin(), attrs.end()) {
Chris Lattner8a923e72008-03-12 17:45:29 +0000136 RefCount = 0;
137 }
138
Owen Anderson9b14a252010-11-09 00:27:03 +0000139 void AddRef() {
140 sys::SmartScopedLock<true> Lock(*ALMutex);
141 ++RefCount;
142 }
Owen Andersonc1a3a472009-08-20 19:03:20 +0000143 void DropRef() {
Owen Anderson9b14a252010-11-09 00:27:03 +0000144 sys::SmartScopedLock<true> Lock(*ALMutex);
Owen Anderson2e831892010-11-18 18:59:13 +0000145 if (!AttributesLists.isConstructed())
146 return;
Owen Anderson91bfeb12010-11-09 17:47:10 +0000147 sys::cas_flag new_val = --RefCount;
Owen Anderson2d335432010-11-09 17:46:38 +0000148 if (new_val == 0)
Owen Anderson9b14a252010-11-09 00:27:03 +0000149 delete this;
Owen Andersonc1a3a472009-08-20 19:03:20 +0000150 }
Chris Lattner8a923e72008-03-12 17:45:29 +0000151
152 void Profile(FoldingSetNodeID &ID) const {
Chris Lattner3cb6f832012-05-28 01:47:44 +0000153 Profile(ID, Attrs);
Chris Lattner8a923e72008-03-12 17:45:29 +0000154 }
Chris Lattner3cb6f832012-05-28 01:47:44 +0000155 static void Profile(FoldingSetNodeID &ID, ArrayRef<AttributeWithIndex> Attrs){
156 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
157 ID.AddInteger(Attrs[i].Attrs.Raw());
158 ID.AddInteger(Attrs[i].Index);
Kostya Serebryanya5054ad2012-01-20 17:56:17 +0000159 }
Chris Lattner8a923e72008-03-12 17:45:29 +0000160 }
161};
162}
163
Devang Patel00095052008-09-24 00:29:49 +0000164AttributeListImpl::~AttributeListImpl() {
Owen Anderson9b14a252010-11-09 00:27:03 +0000165 // NOTE: Lock must be acquired by caller.
Devang Patel4c758ea2008-09-25 21:00:45 +0000166 AttributesLists->RemoveNode(this);
Chris Lattner8a923e72008-03-12 17:45:29 +0000167}
168
169
Chris Lattner3cb6f832012-05-28 01:47:44 +0000170AttrListPtr AttrListPtr::get(ArrayRef<AttributeWithIndex> Attrs) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000171 // If there are no attributes then return a null AttributesList pointer.
Chris Lattner3cb6f832012-05-28 01:47:44 +0000172 if (Attrs.empty())
Devang Patel4c758ea2008-09-25 21:00:45 +0000173 return AttrListPtr();
Chris Lattner8a923e72008-03-12 17:45:29 +0000174
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000175#ifndef NDEBUG
Chris Lattner3cb6f832012-05-28 01:47:44 +0000176 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000177 assert(Attrs[i].Attrs != Attribute::None &&
178 "Pointless attribute!");
Chris Lattner8a923e72008-03-12 17:45:29 +0000179 assert((!i || Attrs[i-1].Index < Attrs[i].Index) &&
Devang Patel4c758ea2008-09-25 21:00:45 +0000180 "Misordered AttributesList!");
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000181 }
182#endif
Chris Lattner8a923e72008-03-12 17:45:29 +0000183
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000184 // Otherwise, build a key to look up the existing attributes.
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000185 FoldingSetNodeID ID;
Chris Lattner3cb6f832012-05-28 01:47:44 +0000186 AttributeListImpl::Profile(ID, Attrs);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000187 void *InsertPos;
Owen Andersond91e6b02009-08-17 17:10:58 +0000188
189 sys::SmartScopedLock<true> Lock(*ALMutex);
190
Devang Patel00095052008-09-24 00:29:49 +0000191 AttributeListImpl *PAL =
Devang Patel4c758ea2008-09-25 21:00:45 +0000192 AttributesLists->FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner8a923e72008-03-12 17:45:29 +0000193
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000194 // If we didn't find any existing attributes of the same shape then
195 // create a new one and insert it.
196 if (!PAL) {
Chris Lattner3cb6f832012-05-28 01:47:44 +0000197 PAL = new AttributeListImpl(Attrs);
Devang Patel4c758ea2008-09-25 21:00:45 +0000198 AttributesLists->InsertNode(PAL, InsertPos);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000199 }
Chris Lattner8a923e72008-03-12 17:45:29 +0000200
Devang Patel4c758ea2008-09-25 21:00:45 +0000201 // Return the AttributesList that we found or created.
202 return AttrListPtr(PAL);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000203}
204
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000205
Chris Lattner8a923e72008-03-12 17:45:29 +0000206//===----------------------------------------------------------------------===//
Devang Patel4c758ea2008-09-25 21:00:45 +0000207// AttrListPtr Method Implementations
Chris Lattner8a923e72008-03-12 17:45:29 +0000208//===----------------------------------------------------------------------===//
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000209
Devang Patel4c758ea2008-09-25 21:00:45 +0000210AttrListPtr::AttrListPtr(AttributeListImpl *LI) : AttrList(LI) {
Chris Lattner8a923e72008-03-12 17:45:29 +0000211 if (LI) LI->AddRef();
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000212}
213
Devang Patel4c758ea2008-09-25 21:00:45 +0000214AttrListPtr::AttrListPtr(const AttrListPtr &P) : AttrList(P.AttrList) {
215 if (AttrList) AttrList->AddRef();
Chris Lattner8a923e72008-03-12 17:45:29 +0000216}
217
Devang Patel4c758ea2008-09-25 21:00:45 +0000218const AttrListPtr &AttrListPtr::operator=(const AttrListPtr &RHS) {
Owen Anderson8dc0b042010-09-16 00:27:35 +0000219 sys::SmartScopedLock<true> Lock(*ALMutex);
Devang Patel4c758ea2008-09-25 21:00:45 +0000220 if (AttrList == RHS.AttrList) return *this;
221 if (AttrList) AttrList->DropRef();
222 AttrList = RHS.AttrList;
223 if (AttrList) AttrList->AddRef();
Chris Lattner8a923e72008-03-12 17:45:29 +0000224 return *this;
225}
226
Devang Patel4c758ea2008-09-25 21:00:45 +0000227AttrListPtr::~AttrListPtr() {
228 if (AttrList) AttrList->DropRef();
Chris Lattner8a923e72008-03-12 17:45:29 +0000229}
230
231/// getNumSlots - Return the number of slots used in this attribute list.
232/// This is the number of arguments that have an attribute set on them
233/// (including the function itself).
Devang Patel4c758ea2008-09-25 21:00:45 +0000234unsigned AttrListPtr::getNumSlots() const {
235 return AttrList ? AttrList->Attrs.size() : 0;
Chris Lattner8a923e72008-03-12 17:45:29 +0000236}
237
Devang Patel4c758ea2008-09-25 21:00:45 +0000238/// getSlot - Return the AttributeWithIndex at the specified slot. This
239/// holds a number plus a set of attributes.
240const AttributeWithIndex &AttrListPtr::getSlot(unsigned Slot) const {
241 assert(AttrList && Slot < AttrList->Attrs.size() && "Slot # out of range!");
242 return AttrList->Attrs[Slot];
Chris Lattner8a923e72008-03-12 17:45:29 +0000243}
244
245
Devang Patel4c758ea2008-09-25 21:00:45 +0000246/// getAttributes - The attributes for the specified index are
247/// returned. Attributes for the result are denoted with Idx = 0.
Devang Patel82fed672008-09-23 22:35:17 +0000248/// Function notes are denoted with idx = ~0.
Devang Patel4c758ea2008-09-25 21:00:45 +0000249Attributes AttrListPtr::getAttributes(unsigned Idx) const {
250 if (AttrList == 0) return Attribute::None;
Chris Lattner8a923e72008-03-12 17:45:29 +0000251
Devang Patel4c758ea2008-09-25 21:00:45 +0000252 const SmallVector<AttributeWithIndex, 4> &Attrs = AttrList->Attrs;
Chris Lattner8a923e72008-03-12 17:45:29 +0000253 for (unsigned i = 0, e = Attrs.size(); i != e && Attrs[i].Index <= Idx; ++i)
254 if (Attrs[i].Index == Idx)
255 return Attrs[i].Attrs;
Devang Patel4c758ea2008-09-25 21:00:45 +0000256 return Attribute::None;
Chris Lattner8a923e72008-03-12 17:45:29 +0000257}
258
259/// hasAttrSomewhere - Return true if the specified attribute is set for at
260/// least one parameter or for the return value.
Devang Patel4c758ea2008-09-25 21:00:45 +0000261bool AttrListPtr::hasAttrSomewhere(Attributes Attr) const {
262 if (AttrList == 0) return false;
Chris Lattner8a923e72008-03-12 17:45:29 +0000263
Devang Patel4c758ea2008-09-25 21:00:45 +0000264 const SmallVector<AttributeWithIndex, 4> &Attrs = AttrList->Attrs;
Chris Lattner8a923e72008-03-12 17:45:29 +0000265 for (unsigned i = 0, e = Attrs.size(); i != e; ++i)
266 if (Attrs[i].Attrs & Attr)
267 return true;
268 return false;
269}
270
271
Devang Patel4c758ea2008-09-25 21:00:45 +0000272AttrListPtr AttrListPtr::addAttr(unsigned Idx, Attributes Attrs) const {
273 Attributes OldAttrs = getAttributes(Idx);
Dale Johannesen11a555e2008-02-19 23:51:49 +0000274#ifndef NDEBUG
275 // FIXME it is not obvious how this should work for alignment.
276 // For now, say we can't change a known alignment.
Devang Patel4c758ea2008-09-25 21:00:45 +0000277 Attributes OldAlign = OldAttrs & Attribute::Alignment;
278 Attributes NewAlign = Attrs & Attribute::Alignment;
Anton Korobeynikov18991d72008-02-20 12:07:57 +0000279 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
Dale Johannesen11a555e2008-02-19 23:51:49 +0000280 "Attempt to change alignment!");
281#endif
Chris Lattner8a923e72008-03-12 17:45:29 +0000282
Devang Patelba3fa6c2008-09-23 23:03:40 +0000283 Attributes NewAttrs = OldAttrs | Attrs;
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000284 if (NewAttrs == OldAttrs)
Chris Lattner8a923e72008-03-12 17:45:29 +0000285 return *this;
286
Devang Patel4c758ea2008-09-25 21:00:45 +0000287 SmallVector<AttributeWithIndex, 8> NewAttrList;
288 if (AttrList == 0)
289 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Chris Lattner8a923e72008-03-12 17:45:29 +0000290 else {
Devang Patel4c758ea2008-09-25 21:00:45 +0000291 const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs;
Chris Lattner8a923e72008-03-12 17:45:29 +0000292 unsigned i = 0, e = OldAttrList.size();
293 // Copy attributes for arguments before this one.
294 for (; i != e && OldAttrList[i].Index < Idx; ++i)
295 NewAttrList.push_back(OldAttrList[i]);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000296
Chris Lattner8a923e72008-03-12 17:45:29 +0000297 // If there are attributes already at this index, merge them in.
298 if (i != e && OldAttrList[i].Index == Idx) {
299 Attrs |= OldAttrList[i].Attrs;
300 ++i;
301 }
302
Devang Patel4c758ea2008-09-25 21:00:45 +0000303 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Chris Lattner8a923e72008-03-12 17:45:29 +0000304
305 // Copy attributes for arguments after this one.
306 NewAttrList.insert(NewAttrList.end(),
307 OldAttrList.begin()+i, OldAttrList.end());
308 }
309
Chris Lattner3cb6f832012-05-28 01:47:44 +0000310 return get(NewAttrList);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000311}
312
Devang Patel4c758ea2008-09-25 21:00:45 +0000313AttrListPtr AttrListPtr::removeAttr(unsigned Idx, Attributes Attrs) const {
Dale Johannesen11a555e2008-02-19 23:51:49 +0000314#ifndef NDEBUG
315 // FIXME it is not obvious how this should work for alignment.
316 // For now, say we can't pass in alignment, which no current use does.
Devang Patel4c758ea2008-09-25 21:00:45 +0000317 assert(!(Attrs & Attribute::Alignment) && "Attempt to exclude alignment!");
Dale Johannesen11a555e2008-02-19 23:51:49 +0000318#endif
Devang Patel4c758ea2008-09-25 21:00:45 +0000319 if (AttrList == 0) return AttrListPtr();
Chris Lattner8a923e72008-03-12 17:45:29 +0000320
Devang Patel4c758ea2008-09-25 21:00:45 +0000321 Attributes OldAttrs = getAttributes(Idx);
Devang Patelba3fa6c2008-09-23 23:03:40 +0000322 Attributes NewAttrs = OldAttrs & ~Attrs;
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000323 if (NewAttrs == OldAttrs)
Chris Lattner8a923e72008-03-12 17:45:29 +0000324 return *this;
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000325
Devang Patel4c758ea2008-09-25 21:00:45 +0000326 SmallVector<AttributeWithIndex, 8> NewAttrList;
327 const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs;
Chris Lattner8a923e72008-03-12 17:45:29 +0000328 unsigned i = 0, e = OldAttrList.size();
329
330 // Copy attributes for arguments before this one.
331 for (; i != e && OldAttrList[i].Index < Idx; ++i)
332 NewAttrList.push_back(OldAttrList[i]);
333
334 // If there are attributes already at this index, merge them in.
335 assert(OldAttrList[i].Index == Idx && "Attribute isn't set?");
336 Attrs = OldAttrList[i].Attrs & ~Attrs;
337 ++i;
338 if (Attrs) // If any attributes left for this parameter, add them.
Devang Patel4c758ea2008-09-25 21:00:45 +0000339 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Chris Lattner8a923e72008-03-12 17:45:29 +0000340
341 // Copy attributes for arguments after this one.
342 NewAttrList.insert(NewAttrList.end(),
343 OldAttrList.begin()+i, OldAttrList.end());
344
Chris Lattner3cb6f832012-05-28 01:47:44 +0000345 return get(NewAttrList);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000346}
347
Devang Patel4c758ea2008-09-25 21:00:45 +0000348void AttrListPtr::dump() const {
David Greenef7014732010-01-05 01:29:58 +0000349 dbgs() << "PAL[ ";
Chris Lattner8a923e72008-03-12 17:45:29 +0000350 for (unsigned i = 0; i < getNumSlots(); ++i) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000351 const AttributeWithIndex &PAWI = getSlot(i);
David Greenef7014732010-01-05 01:29:58 +0000352 dbgs() << "{" << PAWI.Index << "," << PAWI.Attrs << "} ";
Chris Lattner8a923e72008-03-12 17:45:29 +0000353 }
354
David Greenef7014732010-01-05 01:29:58 +0000355 dbgs() << "]\n";
Duncan Sands404eb052008-01-06 18:27:01 +0000356}