blob: 7d3197cb0d7f2eddbdf7609ca4fec88b1dbef1ad [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"
Bill Wendlinge38b8042012-09-26 21:07:29 +000015#include "AttributesImpl.h"
16#include "LLVMContextImpl.h"
Chris Lattner8a923e72008-03-12 17:45:29 +000017#include "llvm/Type.h"
Dan Gohmanfc429612008-03-10 23:55:07 +000018#include "llvm/ADT/StringExtras.h"
Chris Lattner8a923e72008-03-12 17:45:29 +000019#include "llvm/ADT/FoldingSet.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000020#include "llvm/Support/Atomic.h"
21#include "llvm/Support/Mutex.h"
David Greenef7014732010-01-05 01:29:58 +000022#include "llvm/Support/Debug.h"
Chris Lattner3e13b8c2008-01-02 23:42:30 +000023#include "llvm/Support/ManagedStatic.h"
Benjamin Kramer1a25d732009-08-23 11:37:21 +000024#include "llvm/Support/raw_ostream.h"
Chris Lattner3e13b8c2008-01-02 23:42:30 +000025using namespace llvm;
26
Chris Lattner8a923e72008-03-12 17:45:29 +000027//===----------------------------------------------------------------------===//
Devang Patel4c758ea2008-09-25 21:00:45 +000028// Attribute Function Definitions
Chris Lattner8a923e72008-03-12 17:45:29 +000029//===----------------------------------------------------------------------===//
Chris Lattnerd0e1f102008-01-03 00:10:22 +000030
Bill Wendlingde74cf52012-09-20 14:44:42 +000031std::string Attributes::getAsString() const {
Chris Lattner3e13b8c2008-01-02 23:42:30 +000032 std::string Result;
Bill Wendlingde74cf52012-09-20 14:44:42 +000033 if (hasZExtAttr())
Chris Lattner3e13b8c2008-01-02 23:42:30 +000034 Result += "zeroext ";
Bill Wendlingde74cf52012-09-20 14:44:42 +000035 if (hasSExtAttr())
Chris Lattner3e13b8c2008-01-02 23:42:30 +000036 Result += "signext ";
Bill Wendlingde74cf52012-09-20 14:44:42 +000037 if (hasNoReturnAttr())
Chris Lattner3e13b8c2008-01-02 23:42:30 +000038 Result += "noreturn ";
Bill Wendlingde74cf52012-09-20 14:44:42 +000039 if (hasNoUnwindAttr())
Chris Lattner3e13b8c2008-01-02 23:42:30 +000040 Result += "nounwind ";
Bill Wendlingde74cf52012-09-20 14:44:42 +000041 if (hasUWTableAttr())
Rafael Espindolafc9bae62011-05-25 03:44:17 +000042 Result += "uwtable ";
Bill Wendlingde74cf52012-09-20 14:44:42 +000043 if (hasReturnsTwiceAttr())
Rafael Espindolacc349c82011-10-03 14:45:37 +000044 Result += "returns_twice ";
Bill Wendlingde74cf52012-09-20 14:44:42 +000045 if (hasInRegAttr())
Chris Lattner3e13b8c2008-01-02 23:42:30 +000046 Result += "inreg ";
Bill Wendlingde74cf52012-09-20 14:44:42 +000047 if (hasNoAliasAttr())
Chris Lattner3e13b8c2008-01-02 23:42:30 +000048 Result += "noalias ";
Bill Wendlingde74cf52012-09-20 14:44:42 +000049 if (hasNoCaptureAttr())
Nick Lewycky8d69f482008-12-19 09:38:31 +000050 Result += "nocapture ";
Bill Wendlingde74cf52012-09-20 14:44:42 +000051 if (hasStructRetAttr())
Anton Korobeynikovc8ce7b082009-07-17 18:07:26 +000052 Result += "sret ";
Bill Wendlingde74cf52012-09-20 14:44:42 +000053 if (hasByValAttr())
Chris Lattner3e13b8c2008-01-02 23:42:30 +000054 Result += "byval ";
Bill Wendlingde74cf52012-09-20 14:44:42 +000055 if (hasNestAttr())
Chris Lattner3e13b8c2008-01-02 23:42:30 +000056 Result += "nest ";
Bill Wendlingde74cf52012-09-20 14:44:42 +000057 if (hasReadNoneAttr())
Chris Lattner3e13b8c2008-01-02 23:42:30 +000058 Result += "readnone ";
Bill Wendlingde74cf52012-09-20 14:44:42 +000059 if (hasReadOnlyAttr())
Chris Lattner3e13b8c2008-01-02 23:42:30 +000060 Result += "readonly ";
Bill Wendlingde74cf52012-09-20 14:44:42 +000061 if (hasOptimizeForSizeAttr())
Devang Patela05633e2008-09-26 22:53:05 +000062 Result += "optsize ";
Bill Wendlingde74cf52012-09-20 14:44:42 +000063 if (hasNoInlineAttr())
Devang Patela05633e2008-09-26 22:53:05 +000064 Result += "noinline ";
Bill Wendlingde74cf52012-09-20 14:44:42 +000065 if (hasInlineHintAttr())
Jakob Stoklund Olesen74bb06c2010-02-06 01:16:28 +000066 Result += "inlinehint ";
Bill Wendlingde74cf52012-09-20 14:44:42 +000067 if (hasAlwaysInlineAttr())
Devang Patela05633e2008-09-26 22:53:05 +000068 Result += "alwaysinline ";
Bill Wendlingde74cf52012-09-20 14:44:42 +000069 if (hasStackProtectAttr())
Bill Wendlingccb67a3d2008-11-13 01:02:14 +000070 Result += "ssp ";
Bill Wendlingde74cf52012-09-20 14:44:42 +000071 if (hasStackProtectReqAttr())
Bill Wendlingccb67a3d2008-11-13 01:02:14 +000072 Result += "sspreq ";
Bill Wendlingde74cf52012-09-20 14:44:42 +000073 if (hasNoRedZoneAttr())
Devang Patel72a4d2f2009-06-04 22:05:33 +000074 Result += "noredzone ";
Bill Wendlingde74cf52012-09-20 14:44:42 +000075 if (hasNoImplicitFloatAttr())
Devang Pateld1c7d342009-06-05 21:57:13 +000076 Result += "noimplicitfloat ";
Bill Wendlingde74cf52012-09-20 14:44:42 +000077 if (hasNakedAttr())
Anton Korobeynikovc8ce7b082009-07-17 18:07:26 +000078 Result += "naked ";
Bill Wendlingde74cf52012-09-20 14:44:42 +000079 if (hasNonLazyBindAttr())
John McCall4b7a8d62011-06-15 20:36:13 +000080 Result += "nonlazybind ";
Bill Wendlingde74cf52012-09-20 14:44:42 +000081 if (hasAddressSafetyAttr())
Kostya Serebryanya5054ad2012-01-20 17:56:17 +000082 Result += "address_safety ";
Bill Wendlingb4e211c2012-09-20 15:20:36 +000083 if (hasStackAlignmentAttr()) {
Charles Davisbe5557e2010-02-12 00:31:15 +000084 Result += "alignstack(";
Bill Wendling9be77592012-09-21 15:26:31 +000085 Result += utostr(getStackAlignment());
Charles Davisbe5557e2010-02-12 00:31:15 +000086 Result += ") ";
87 }
Bill Wendlingb4e211c2012-09-20 15:20:36 +000088 if (hasAlignmentAttr()) {
Dale Johannesen11a555e2008-02-19 23:51:49 +000089 Result += "align ";
Bill Wendling9be77592012-09-21 15:26:31 +000090 Result += utostr(getAlignment());
Dale Johannesen11a555e2008-02-19 23:51:49 +000091 Result += " ";
92 }
Dan Gohman1a70bcc2008-08-05 15:51:44 +000093 // Trim the trailing space.
Nick Lewycky8d69f482008-12-19 09:38:31 +000094 assert(!Result.empty() && "Unknown attribute!");
Dan Gohman1a70bcc2008-08-05 15:51:44 +000095 Result.erase(Result.end()-1);
Chris Lattner3e13b8c2008-01-02 23:42:30 +000096 return Result;
97}
98
Bill Wendlingeb337232012-09-25 20:38:59 +000099Attributes Attributes::typeIncompatible(Type *Ty) {
100 Attributes Incompatible = Attribute::None;
Chris Lattner8a923e72008-03-12 17:45:29 +0000101
Duncan Sands9dff9be2010-02-15 16:12:20 +0000102 if (!Ty->isIntegerTy())
Chris Lattner8a923e72008-03-12 17:45:29 +0000103 // Attributes that only apply to integers.
Bill Wendlingeb337232012-09-25 20:38:59 +0000104 Incompatible |= Attribute::SExt | Attribute::ZExt;
Chris Lattner8a923e72008-03-12 17:45:29 +0000105
Duncan Sands19d0b472010-02-16 11:11:14 +0000106 if (!Ty->isPointerTy())
Chris Lattner8a923e72008-03-12 17:45:29 +0000107 // Attributes that only apply to pointers.
Bill Wendlingeb337232012-09-25 20:38:59 +0000108 Incompatible |= Attribute::ByVal | Attribute::Nest | Attribute::NoAlias |
109 Attribute::StructRet | Attribute::NoCapture;
Chris Lattner8a923e72008-03-12 17:45:29 +0000110
111 return Incompatible;
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000112}
113
Chris Lattner8a923e72008-03-12 17:45:29 +0000114//===----------------------------------------------------------------------===//
Bill Wendlinge38b8042012-09-26 21:07:29 +0000115// AttributeImpl Definition
116//===----------------------------------------------------------------------===//
117
118Attributes::Attributes(AttributesImpl *A) : Bits(0) {}
119
120Attributes Attributes::get(LLVMContext &Context, Attributes::Builder &B) {
121 // If there are no attributes, return an empty Attributes class.
122 if (B.Bits == 0)
123 return Attributes();
124
125 // Otherwise, build a key to look up the existing attributes.
126 LLVMContextImpl *pImpl = Context.pImpl;
127 FoldingSetNodeID ID;
128 ID.AddInteger(B.Bits);
129
130 void *InsertPoint;
131 AttributesImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
132
133 if (!PA) {
134 // If we didn't find any existing attributes of the same shape then create a
135 // new one and insert it.
136 PA = new AttributesImpl(B.Bits);
137 pImpl->AttrsSet.InsertNode(PA, InsertPoint);
138 }
139
140 // Return the AttributesList that we found or created.
141 return Attributes(PA);
142}
143
144//===----------------------------------------------------------------------===//
Devang Patel00095052008-09-24 00:29:49 +0000145// AttributeListImpl Definition
Chris Lattner8a923e72008-03-12 17:45:29 +0000146//===----------------------------------------------------------------------===//
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000147
Owen Anderson2e831892010-11-18 18:59:13 +0000148namespace llvm {
149 class AttributeListImpl;
150}
151
152static ManagedStatic<FoldingSet<AttributeListImpl> > AttributesLists;
Owen Anderson9b14a252010-11-09 00:27:03 +0000153
Chris Lattner8a923e72008-03-12 17:45:29 +0000154namespace llvm {
Owen Anderson9b14a252010-11-09 00:27:03 +0000155static ManagedStatic<sys::SmartMutex<true> > ALMutex;
156
Devang Patel00095052008-09-24 00:29:49 +0000157class AttributeListImpl : public FoldingSetNode {
Owen Andersonc1a3a472009-08-20 19:03:20 +0000158 sys::cas_flag RefCount;
Chris Lattner8a923e72008-03-12 17:45:29 +0000159
Devang Patel4c758ea2008-09-25 21:00:45 +0000160 // AttributesList is uniqued, these should not be publicly available.
Craig Topperb1d83e82012-09-18 02:01:41 +0000161 void operator=(const AttributeListImpl &) LLVM_DELETED_FUNCTION;
162 AttributeListImpl(const AttributeListImpl &) LLVM_DELETED_FUNCTION;
Devang Patel00095052008-09-24 00:29:49 +0000163 ~AttributeListImpl(); // Private implementation
Chris Lattner8a923e72008-03-12 17:45:29 +0000164public:
Devang Patel4c758ea2008-09-25 21:00:45 +0000165 SmallVector<AttributeWithIndex, 4> Attrs;
Chris Lattner8a923e72008-03-12 17:45:29 +0000166
Chris Lattner3cb6f832012-05-28 01:47:44 +0000167 AttributeListImpl(ArrayRef<AttributeWithIndex> attrs)
168 : Attrs(attrs.begin(), attrs.end()) {
Chris Lattner8a923e72008-03-12 17:45:29 +0000169 RefCount = 0;
170 }
171
Owen Anderson9b14a252010-11-09 00:27:03 +0000172 void AddRef() {
173 sys::SmartScopedLock<true> Lock(*ALMutex);
174 ++RefCount;
175 }
Owen Andersonc1a3a472009-08-20 19:03:20 +0000176 void DropRef() {
Owen Anderson9b14a252010-11-09 00:27:03 +0000177 sys::SmartScopedLock<true> Lock(*ALMutex);
Owen Anderson2e831892010-11-18 18:59:13 +0000178 if (!AttributesLists.isConstructed())
179 return;
Owen Anderson91bfeb12010-11-09 17:47:10 +0000180 sys::cas_flag new_val = --RefCount;
Owen Anderson2d335432010-11-09 17:46:38 +0000181 if (new_val == 0)
Owen Anderson9b14a252010-11-09 00:27:03 +0000182 delete this;
Owen Andersonc1a3a472009-08-20 19:03:20 +0000183 }
Chris Lattner8a923e72008-03-12 17:45:29 +0000184
185 void Profile(FoldingSetNodeID &ID) const {
Chris Lattner3cb6f832012-05-28 01:47:44 +0000186 Profile(ID, Attrs);
Chris Lattner8a923e72008-03-12 17:45:29 +0000187 }
Chris Lattner3cb6f832012-05-28 01:47:44 +0000188 static void Profile(FoldingSetNodeID &ID, ArrayRef<AttributeWithIndex> Attrs){
189 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
190 ID.AddInteger(Attrs[i].Attrs.Raw());
191 ID.AddInteger(Attrs[i].Index);
Kostya Serebryanya5054ad2012-01-20 17:56:17 +0000192 }
Chris Lattner8a923e72008-03-12 17:45:29 +0000193 }
194};
195}
196
Devang Patel00095052008-09-24 00:29:49 +0000197AttributeListImpl::~AttributeListImpl() {
Owen Anderson9b14a252010-11-09 00:27:03 +0000198 // NOTE: Lock must be acquired by caller.
Devang Patel4c758ea2008-09-25 21:00:45 +0000199 AttributesLists->RemoveNode(this);
Chris Lattner8a923e72008-03-12 17:45:29 +0000200}
201
202
Chris Lattner3cb6f832012-05-28 01:47:44 +0000203AttrListPtr AttrListPtr::get(ArrayRef<AttributeWithIndex> Attrs) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000204 // If there are no attributes then return a null AttributesList pointer.
Chris Lattner3cb6f832012-05-28 01:47:44 +0000205 if (Attrs.empty())
Devang Patel4c758ea2008-09-25 21:00:45 +0000206 return AttrListPtr();
Chris Lattner8a923e72008-03-12 17:45:29 +0000207
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000208#ifndef NDEBUG
Chris Lattner3cb6f832012-05-28 01:47:44 +0000209 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
Bill Wendling9be77592012-09-21 15:26:31 +0000210 assert(Attrs[i].Attrs.hasAttributes() &&
Devang Patel4c758ea2008-09-25 21:00:45 +0000211 "Pointless attribute!");
Chris Lattner8a923e72008-03-12 17:45:29 +0000212 assert((!i || Attrs[i-1].Index < Attrs[i].Index) &&
Devang Patel4c758ea2008-09-25 21:00:45 +0000213 "Misordered AttributesList!");
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000214 }
215#endif
Chris Lattner8a923e72008-03-12 17:45:29 +0000216
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000217 // Otherwise, build a key to look up the existing attributes.
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000218 FoldingSetNodeID ID;
Chris Lattner3cb6f832012-05-28 01:47:44 +0000219 AttributeListImpl::Profile(ID, Attrs);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000220 void *InsertPos;
Owen Andersond91e6b02009-08-17 17:10:58 +0000221
222 sys::SmartScopedLock<true> Lock(*ALMutex);
223
Devang Patel00095052008-09-24 00:29:49 +0000224 AttributeListImpl *PAL =
Devang Patel4c758ea2008-09-25 21:00:45 +0000225 AttributesLists->FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner8a923e72008-03-12 17:45:29 +0000226
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000227 // If we didn't find any existing attributes of the same shape then
228 // create a new one and insert it.
229 if (!PAL) {
Chris Lattner3cb6f832012-05-28 01:47:44 +0000230 PAL = new AttributeListImpl(Attrs);
Devang Patel4c758ea2008-09-25 21:00:45 +0000231 AttributesLists->InsertNode(PAL, InsertPos);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000232 }
Chris Lattner8a923e72008-03-12 17:45:29 +0000233
Devang Patel4c758ea2008-09-25 21:00:45 +0000234 // Return the AttributesList that we found or created.
235 return AttrListPtr(PAL);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000236}
237
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000238
Chris Lattner8a923e72008-03-12 17:45:29 +0000239//===----------------------------------------------------------------------===//
Devang Patel4c758ea2008-09-25 21:00:45 +0000240// AttrListPtr Method Implementations
Chris Lattner8a923e72008-03-12 17:45:29 +0000241//===----------------------------------------------------------------------===//
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000242
Devang Patel4c758ea2008-09-25 21:00:45 +0000243AttrListPtr::AttrListPtr(AttributeListImpl *LI) : AttrList(LI) {
Chris Lattner8a923e72008-03-12 17:45:29 +0000244 if (LI) LI->AddRef();
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000245}
246
Devang Patel4c758ea2008-09-25 21:00:45 +0000247AttrListPtr::AttrListPtr(const AttrListPtr &P) : AttrList(P.AttrList) {
248 if (AttrList) AttrList->AddRef();
Chris Lattner8a923e72008-03-12 17:45:29 +0000249}
250
Devang Patel4c758ea2008-09-25 21:00:45 +0000251const AttrListPtr &AttrListPtr::operator=(const AttrListPtr &RHS) {
Owen Anderson8dc0b042010-09-16 00:27:35 +0000252 sys::SmartScopedLock<true> Lock(*ALMutex);
Devang Patel4c758ea2008-09-25 21:00:45 +0000253 if (AttrList == RHS.AttrList) return *this;
254 if (AttrList) AttrList->DropRef();
255 AttrList = RHS.AttrList;
256 if (AttrList) AttrList->AddRef();
Chris Lattner8a923e72008-03-12 17:45:29 +0000257 return *this;
258}
259
Devang Patel4c758ea2008-09-25 21:00:45 +0000260AttrListPtr::~AttrListPtr() {
261 if (AttrList) AttrList->DropRef();
Chris Lattner8a923e72008-03-12 17:45:29 +0000262}
263
264/// getNumSlots - Return the number of slots used in this attribute list.
265/// This is the number of arguments that have an attribute set on them
266/// (including the function itself).
Devang Patel4c758ea2008-09-25 21:00:45 +0000267unsigned AttrListPtr::getNumSlots() const {
268 return AttrList ? AttrList->Attrs.size() : 0;
Chris Lattner8a923e72008-03-12 17:45:29 +0000269}
270
Devang Patel4c758ea2008-09-25 21:00:45 +0000271/// getSlot - Return the AttributeWithIndex at the specified slot. This
272/// holds a number plus a set of attributes.
273const AttributeWithIndex &AttrListPtr::getSlot(unsigned Slot) const {
274 assert(AttrList && Slot < AttrList->Attrs.size() && "Slot # out of range!");
275 return AttrList->Attrs[Slot];
Chris Lattner8a923e72008-03-12 17:45:29 +0000276}
277
278
Devang Patel4c758ea2008-09-25 21:00:45 +0000279/// getAttributes - The attributes for the specified index are
280/// returned. Attributes for the result are denoted with Idx = 0.
Devang Patel82fed672008-09-23 22:35:17 +0000281/// Function notes are denoted with idx = ~0.
Devang Patel4c758ea2008-09-25 21:00:45 +0000282Attributes AttrListPtr::getAttributes(unsigned Idx) const {
Bill Wendling9be77592012-09-21 15:26:31 +0000283 if (AttrList == 0) return Attributes();
Chris Lattner8a923e72008-03-12 17:45:29 +0000284
Devang Patel4c758ea2008-09-25 21:00:45 +0000285 const SmallVector<AttributeWithIndex, 4> &Attrs = AttrList->Attrs;
Chris Lattner8a923e72008-03-12 17:45:29 +0000286 for (unsigned i = 0, e = Attrs.size(); i != e && Attrs[i].Index <= Idx; ++i)
287 if (Attrs[i].Index == Idx)
288 return Attrs[i].Attrs;
Bill Wendling9be77592012-09-21 15:26:31 +0000289
290 return Attributes();
Chris Lattner8a923e72008-03-12 17:45:29 +0000291}
292
293/// hasAttrSomewhere - Return true if the specified attribute is set for at
294/// least one parameter or for the return value.
Devang Patel4c758ea2008-09-25 21:00:45 +0000295bool AttrListPtr::hasAttrSomewhere(Attributes Attr) const {
296 if (AttrList == 0) return false;
Chris Lattner8a923e72008-03-12 17:45:29 +0000297
Devang Patel4c758ea2008-09-25 21:00:45 +0000298 const SmallVector<AttributeWithIndex, 4> &Attrs = AttrList->Attrs;
Chris Lattner8a923e72008-03-12 17:45:29 +0000299 for (unsigned i = 0, e = Attrs.size(); i != e; ++i)
Bill Wendlingb4e211c2012-09-20 15:20:36 +0000300 if (Attrs[i].Attrs.hasAttributes(Attr))
Chris Lattner8a923e72008-03-12 17:45:29 +0000301 return true;
302 return false;
303}
304
305
Devang Patel4c758ea2008-09-25 21:00:45 +0000306AttrListPtr AttrListPtr::addAttr(unsigned Idx, Attributes Attrs) const {
307 Attributes OldAttrs = getAttributes(Idx);
Dale Johannesen11a555e2008-02-19 23:51:49 +0000308#ifndef NDEBUG
309 // FIXME it is not obvious how this should work for alignment.
310 // For now, say we can't change a known alignment.
Bill Wendling9be77592012-09-21 15:26:31 +0000311 unsigned OldAlign = OldAttrs.getAlignment();
312 unsigned NewAlign = Attrs.getAlignment();
Anton Korobeynikov18991d72008-02-20 12:07:57 +0000313 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
Dale Johannesen11a555e2008-02-19 23:51:49 +0000314 "Attempt to change alignment!");
315#endif
Chris Lattner8a923e72008-03-12 17:45:29 +0000316
Devang Patelba3fa6c2008-09-23 23:03:40 +0000317 Attributes NewAttrs = OldAttrs | Attrs;
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000318 if (NewAttrs == OldAttrs)
Chris Lattner8a923e72008-03-12 17:45:29 +0000319 return *this;
320
Devang Patel4c758ea2008-09-25 21:00:45 +0000321 SmallVector<AttributeWithIndex, 8> NewAttrList;
322 if (AttrList == 0)
323 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Chris Lattner8a923e72008-03-12 17:45:29 +0000324 else {
Devang Patel4c758ea2008-09-25 21:00:45 +0000325 const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs;
Chris Lattner8a923e72008-03-12 17:45:29 +0000326 unsigned i = 0, e = OldAttrList.size();
327 // Copy attributes for arguments before this one.
328 for (; i != e && OldAttrList[i].Index < Idx; ++i)
329 NewAttrList.push_back(OldAttrList[i]);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000330
Chris Lattner8a923e72008-03-12 17:45:29 +0000331 // If there are attributes already at this index, merge them in.
332 if (i != e && OldAttrList[i].Index == Idx) {
333 Attrs |= OldAttrList[i].Attrs;
334 ++i;
335 }
336
Devang Patel4c758ea2008-09-25 21:00:45 +0000337 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Chris Lattner8a923e72008-03-12 17:45:29 +0000338
339 // Copy attributes for arguments after this one.
340 NewAttrList.insert(NewAttrList.end(),
341 OldAttrList.begin()+i, OldAttrList.end());
342 }
343
Chris Lattner3cb6f832012-05-28 01:47:44 +0000344 return get(NewAttrList);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000345}
346
Devang Patel4c758ea2008-09-25 21:00:45 +0000347AttrListPtr AttrListPtr::removeAttr(unsigned Idx, Attributes Attrs) const {
Dale Johannesen11a555e2008-02-19 23:51:49 +0000348#ifndef NDEBUG
349 // FIXME it is not obvious how this should work for alignment.
350 // For now, say we can't pass in alignment, which no current use does.
Bill Wendlingb4e211c2012-09-20 15:20:36 +0000351 assert(!Attrs.hasAlignmentAttr() && "Attempt to exclude alignment!");
Dale Johannesen11a555e2008-02-19 23:51:49 +0000352#endif
Devang Patel4c758ea2008-09-25 21:00:45 +0000353 if (AttrList == 0) return AttrListPtr();
Chris Lattner8a923e72008-03-12 17:45:29 +0000354
Devang Patel4c758ea2008-09-25 21:00:45 +0000355 Attributes OldAttrs = getAttributes(Idx);
Devang Patelba3fa6c2008-09-23 23:03:40 +0000356 Attributes NewAttrs = OldAttrs & ~Attrs;
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000357 if (NewAttrs == OldAttrs)
Chris Lattner8a923e72008-03-12 17:45:29 +0000358 return *this;
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000359
Devang Patel4c758ea2008-09-25 21:00:45 +0000360 SmallVector<AttributeWithIndex, 8> NewAttrList;
361 const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs;
Chris Lattner8a923e72008-03-12 17:45:29 +0000362 unsigned i = 0, e = OldAttrList.size();
363
364 // Copy attributes for arguments before this one.
365 for (; i != e && OldAttrList[i].Index < Idx; ++i)
366 NewAttrList.push_back(OldAttrList[i]);
367
368 // If there are attributes already at this index, merge them in.
369 assert(OldAttrList[i].Index == Idx && "Attribute isn't set?");
370 Attrs = OldAttrList[i].Attrs & ~Attrs;
371 ++i;
372 if (Attrs) // If any attributes left for this parameter, add them.
Devang Patel4c758ea2008-09-25 21:00:45 +0000373 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Chris Lattner8a923e72008-03-12 17:45:29 +0000374
375 // Copy attributes for arguments after this one.
376 NewAttrList.insert(NewAttrList.end(),
377 OldAttrList.begin()+i, OldAttrList.end());
378
Chris Lattner3cb6f832012-05-28 01:47:44 +0000379 return get(NewAttrList);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000380}
381
Devang Patel4c758ea2008-09-25 21:00:45 +0000382void AttrListPtr::dump() const {
David Greenef7014732010-01-05 01:29:58 +0000383 dbgs() << "PAL[ ";
Chris Lattner8a923e72008-03-12 17:45:29 +0000384 for (unsigned i = 0; i < getNumSlots(); ++i) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000385 const AttributeWithIndex &PAWI = getSlot(i);
David Greenef7014732010-01-05 01:29:58 +0000386 dbgs() << "{" << PAWI.Index << "," << PAWI.Attrs << "} ";
Chris Lattner8a923e72008-03-12 17:45:29 +0000387 }
388
David Greenef7014732010-01-05 01:29:58 +0000389 dbgs() << "]\n";
Duncan Sands404eb052008-01-06 18:27:01 +0000390}