blob: 84f90ade8b2aae92326569c1ddf761f7a3cd0732 [file] [log] [blame]
Bill Wendling034b94b2012-12-19 07:18:57 +00001//===-- Attribute.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//
Bill Wendling034b94b2012-12-19 07:18:57 +000010// This file implements the Attribute, AttributeImpl, AttrBuilder,
Bill Wendling18e72112012-12-19 22:42:22 +000011// AttributeSetImpl, and AttributeSet classes.
Chris Lattner50ee9dd2008-01-02 23:42:30 +000012//
13//===----------------------------------------------------------------------===//
14
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000015#include "llvm/IR/Attributes.h"
Bill Wendlingf6670722012-12-20 01:36:59 +000016#include "AttributeImpl.h"
Bill Wendling2c79ecb2012-09-26 21:07:29 +000017#include "LLVMContextImpl.h"
Chris Lattner58d74912008-03-12 17:45:29 +000018#include "llvm/ADT/FoldingSet.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000019#include "llvm/ADT/StringExtras.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000020#include "llvm/IR/Type.h"
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000021#include "llvm/Support/Atomic.h"
David Greeneef1894e2010-01-05 01:29:58 +000022#include "llvm/Support/Debug.h"
Chris Lattner50ee9dd2008-01-02 23:42:30 +000023#include "llvm/Support/ManagedStatic.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000024#include "llvm/Support/Mutex.h"
Benjamin Kramercfa6ec92009-08-23 11:37:21 +000025#include "llvm/Support/raw_ostream.h"
Chris Lattner50ee9dd2008-01-02 23:42:30 +000026using namespace llvm;
27
Chris Lattner58d74912008-03-12 17:45:29 +000028//===----------------------------------------------------------------------===//
Bill Wendling034b94b2012-12-19 07:18:57 +000029// Attribute Implementation
Chris Lattner58d74912008-03-12 17:45:29 +000030//===----------------------------------------------------------------------===//
Chris Lattnerfabfde32008-01-03 00:10:22 +000031
Bill Wendling629fb822012-12-22 00:37:52 +000032Attribute Attribute::get(LLVMContext &Context, ArrayRef<AttrKind> Vals) {
Bill Wendling702cc912012-10-15 20:35:56 +000033 AttrBuilder B;
Bill Wendling629fb822012-12-22 00:37:52 +000034 for (ArrayRef<AttrKind>::iterator I = Vals.begin(), E = Vals.end();
Bill Wendlingcb3de0b2012-10-15 04:46:55 +000035 I != E; ++I)
36 B.addAttribute(*I);
Bill Wendling034b94b2012-12-19 07:18:57 +000037 return Attribute::get(Context, B);
Bill Wendlingcb3de0b2012-10-15 04:46:55 +000038}
Bill Wendling8e635db2012-10-08 21:47:17 +000039
Bill Wendling034b94b2012-12-19 07:18:57 +000040Attribute Attribute::get(LLVMContext &Context, AttrBuilder &B) {
41 // If there are no attributes, return an empty Attribute class.
Bill Wendlinga5c699d2012-10-16 05:55:09 +000042 if (!B.hasAttributes())
Bill Wendling034b94b2012-12-19 07:18:57 +000043 return Attribute();
Bill Wendling8e635db2012-10-08 21:47:17 +000044
45 // Otherwise, build a key to look up the existing attributes.
46 LLVMContextImpl *pImpl = Context.pImpl;
47 FoldingSetNodeID ID;
Bill Wendlingc966e082012-12-30 01:05:42 +000048 ID.AddInteger(B.getBitMask());
Bill Wendling8e635db2012-10-08 21:47:17 +000049
50 void *InsertPoint;
Bill Wendlingf6670722012-12-20 01:36:59 +000051 AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
Bill Wendling8e635db2012-10-08 21:47:17 +000052
53 if (!PA) {
54 // If we didn't find any existing attributes of the same shape then create a
55 // new one and insert it.
Bill Wendlingc966e082012-12-30 01:05:42 +000056 PA = new AttributeImpl(Context, B.getBitMask());
Bill Wendling8e635db2012-10-08 21:47:17 +000057 pImpl->AttrsSet.InsertNode(PA, InsertPoint);
58 }
59
60 // Return the AttributesList that we found or created.
Bill Wendling034b94b2012-12-19 07:18:57 +000061 return Attribute(PA);
Bill Wendling8e635db2012-10-08 21:47:17 +000062}
63
Bill Wendling629fb822012-12-22 00:37:52 +000064bool Attribute::hasAttribute(AttrKind Val) const {
Bill Wendling27107f62012-12-20 21:28:43 +000065 return pImpl && pImpl->hasAttribute(Val);
Bill Wendlinge66f3d32012-10-05 06:44:41 +000066}
67
Bill Wendling034b94b2012-12-19 07:18:57 +000068bool Attribute::hasAttributes() const {
Bill Wendling27107f62012-12-20 21:28:43 +000069 return pImpl && pImpl->hasAttributes();
Bill Wendling05cc40d2012-10-15 05:40:12 +000070}
71
Bill Wendlinge66f3d32012-10-05 06:44:41 +000072/// This returns the alignment field of an attribute as a byte alignment value.
Bill Wendling034b94b2012-12-19 07:18:57 +000073unsigned Attribute::getAlignment() const {
74 if (!hasAttribute(Attribute::Alignment))
Bill Wendling9ef99c92012-10-09 20:56:48 +000075 return 0;
Bill Wendling27107f62012-12-20 21:28:43 +000076 return 1U << ((pImpl->getAlignment() >> 16) - 1);
Bill Wendlinge66f3d32012-10-05 06:44:41 +000077}
78
Bill Wendling1bbd6442013-01-05 01:36:54 +000079void Attribute::setAlignment(unsigned Align) {
80 assert(hasAttribute(Attribute::Alignment) &&
81 "Trying to set the alignment on a non-alignment attribute!");
82 pImpl->setAlignment(Align);
83}
84
Bill Wendlinge66f3d32012-10-05 06:44:41 +000085/// This returns the stack alignment field of an attribute as a byte alignment
86/// value.
Bill Wendling034b94b2012-12-19 07:18:57 +000087unsigned Attribute::getStackAlignment() const {
88 if (!hasAttribute(Attribute::StackAlignment))
Bill Wendling9ef99c92012-10-09 20:56:48 +000089 return 0;
Bill Wendling27107f62012-12-20 21:28:43 +000090 return 1U << ((pImpl->getStackAlignment() >> 26) - 1);
Bill Wendlinge66f3d32012-10-05 06:44:41 +000091}
92
Bill Wendling1bbd6442013-01-05 01:36:54 +000093void Attribute::setStackAlignment(unsigned Align) {
94 assert(hasAttribute(Attribute::StackAlignment) &&
95 "Trying to set the stack alignment on a non-alignment attribute!");
96 pImpl->setStackAlignment(Align);
97}
98
Bill Wendling92e287f2012-12-31 11:51:54 +000099bool Attribute::operator==(AttrKind K) const {
Bill Wendling60507d52013-01-04 20:54:35 +0000100 return pImpl && *pImpl == K;
Bill Wendling92e287f2012-12-31 11:51:54 +0000101}
Bill Wendling92e287f2012-12-31 11:51:54 +0000102bool Attribute::operator!=(AttrKind K) const {
Bill Wendling60507d52013-01-04 20:54:35 +0000103 return !(*this == K);
Bill Wendling92e287f2012-12-31 11:51:54 +0000104}
105
Bill Wendlingc966e082012-12-30 01:05:42 +0000106uint64_t Attribute::getBitMask() const {
107 return pImpl ? pImpl->getBitMask() : 0;
Bill Wendlingb10c88f2012-10-07 08:55:05 +0000108}
109
Bill Wendling034b94b2012-12-19 07:18:57 +0000110Attribute Attribute::typeIncompatible(Type *Ty) {
Bill Wendling702cc912012-10-15 20:35:56 +0000111 AttrBuilder Incompatible;
Bill Wendling77898682012-10-16 06:01:44 +0000112
Bill Wendling3a106e62012-10-09 19:01:18 +0000113 if (!Ty->isIntegerTy())
Bill Wendling034b94b2012-12-19 07:18:57 +0000114 // Attribute that only apply to integers.
115 Incompatible.addAttribute(Attribute::SExt)
116 .addAttribute(Attribute::ZExt);
Bill Wendling77898682012-10-16 06:01:44 +0000117
Bill Wendling3a106e62012-10-09 19:01:18 +0000118 if (!Ty->isPointerTy())
Bill Wendling034b94b2012-12-19 07:18:57 +0000119 // Attribute that only apply to pointers.
120 Incompatible.addAttribute(Attribute::ByVal)
121 .addAttribute(Attribute::Nest)
122 .addAttribute(Attribute::NoAlias)
123 .addAttribute(Attribute::NoCapture)
124 .addAttribute(Attribute::StructRet);
Bill Wendling77898682012-10-16 06:01:44 +0000125
Bill Wendling034b94b2012-12-19 07:18:57 +0000126 return Attribute::get(Ty->getContext(), Incompatible);
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000127}
128
Bill Wendling702cc912012-10-15 20:35:56 +0000129/// encodeLLVMAttributesForBitcode - This returns an integer containing an
130/// encoding of all the LLVM attributes found in the given attribute bitset.
131/// Any change to this encoding is a breaking change to bitcode compatibility.
Bill Wendling034b94b2012-12-19 07:18:57 +0000132uint64_t Attribute::encodeLLVMAttributesForBitcode(Attribute Attrs) {
Bill Wendling702cc912012-10-15 20:35:56 +0000133 // FIXME: It doesn't make sense to store the alignment information as an
134 // expanded out value, we should store it as a log2 value. However, we can't
135 // just change that here without breaking bitcode compatibility. If this ever
136 // becomes a problem in practice, we should introduce new tag numbers in the
137 // bitcode file and have those tags use a more efficiently encoded alignment
138 // field.
139
140 // Store the alignment in the bitcode as a 16-bit raw value instead of a 5-bit
141 // log2 encoded value. Shift the bits above the alignment up by 11 bits.
Bill Wendlingc966e082012-12-30 01:05:42 +0000142 uint64_t EncodedAttrs = Attrs.getBitMask() & 0xffff;
Bill Wendling034b94b2012-12-19 07:18:57 +0000143 if (Attrs.hasAttribute(Attribute::Alignment))
Bill Wendling702cc912012-10-15 20:35:56 +0000144 EncodedAttrs |= Attrs.getAlignment() << 16;
Bill Wendlingc966e082012-12-30 01:05:42 +0000145 EncodedAttrs |= (Attrs.getBitMask() & (0xffffULL << 21)) << 11;
Bill Wendling702cc912012-10-15 20:35:56 +0000146 return EncodedAttrs;
147}
148
149/// decodeLLVMAttributesForBitcode - This returns an attribute bitset containing
150/// the LLVM attributes that have been decoded from the given integer. This
151/// function must stay in sync with 'encodeLLVMAttributesForBitcode'.
Bill Wendling034b94b2012-12-19 07:18:57 +0000152Attribute Attribute::decodeLLVMAttributesForBitcode(LLVMContext &C,
Bill Wendling702cc912012-10-15 20:35:56 +0000153 uint64_t EncodedAttrs) {
154 // The alignment is stored as a 16-bit raw value from bits 31--16. We shift
155 // the bits above 31 down by 11 bits.
156 unsigned Alignment = (EncodedAttrs & (0xffffULL << 16)) >> 16;
157 assert((!Alignment || isPowerOf2_32(Alignment)) &&
158 "Alignment must be a power of two.");
159
160 AttrBuilder B(EncodedAttrs & 0xffff);
161 if (Alignment)
162 B.addAlignmentAttr(Alignment);
Nadav Roteme7439422012-10-22 17:33:31 +0000163 B.addRawValue((EncodedAttrs & (0xffffULL << 32)) >> 11);
Bill Wendling034b94b2012-12-19 07:18:57 +0000164 return Attribute::get(C, B);
Bill Wendling702cc912012-10-15 20:35:56 +0000165}
166
Bill Wendling034b94b2012-12-19 07:18:57 +0000167std::string Attribute::getAsString() const {
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000168 std::string Result;
Bill Wendling034b94b2012-12-19 07:18:57 +0000169 if (hasAttribute(Attribute::ZExt))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000170 Result += "zeroext ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000171 if (hasAttribute(Attribute::SExt))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000172 Result += "signext ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000173 if (hasAttribute(Attribute::NoReturn))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000174 Result += "noreturn ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000175 if (hasAttribute(Attribute::NoUnwind))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000176 Result += "nounwind ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000177 if (hasAttribute(Attribute::UWTable))
Rafael Espindolafc2bb8c2011-05-25 03:44:17 +0000178 Result += "uwtable ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000179 if (hasAttribute(Attribute::ReturnsTwice))
Rafael Espindola25456ef2011-10-03 14:45:37 +0000180 Result += "returns_twice ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000181 if (hasAttribute(Attribute::InReg))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000182 Result += "inreg ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000183 if (hasAttribute(Attribute::NoAlias))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000184 Result += "noalias ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000185 if (hasAttribute(Attribute::NoCapture))
Nick Lewycky73ddd4f2008-12-19 09:38:31 +0000186 Result += "nocapture ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000187 if (hasAttribute(Attribute::StructRet))
Anton Korobeynikovc5ec8a72009-07-17 18:07:26 +0000188 Result += "sret ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000189 if (hasAttribute(Attribute::ByVal))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000190 Result += "byval ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000191 if (hasAttribute(Attribute::Nest))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000192 Result += "nest ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000193 if (hasAttribute(Attribute::ReadNone))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000194 Result += "readnone ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000195 if (hasAttribute(Attribute::ReadOnly))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000196 Result += "readonly ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000197 if (hasAttribute(Attribute::OptimizeForSize))
Devang Patel19c87462008-09-26 22:53:05 +0000198 Result += "optsize ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000199 if (hasAttribute(Attribute::NoInline))
Devang Patel19c87462008-09-26 22:53:05 +0000200 Result += "noinline ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000201 if (hasAttribute(Attribute::InlineHint))
Jakob Stoklund Olesen570a4a52010-02-06 01:16:28 +0000202 Result += "inlinehint ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000203 if (hasAttribute(Attribute::AlwaysInline))
Devang Patel19c87462008-09-26 22:53:05 +0000204 Result += "alwaysinline ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000205 if (hasAttribute(Attribute::StackProtect))
Bill Wendlinge9e6bdf2008-11-13 01:02:14 +0000206 Result += "ssp ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000207 if (hasAttribute(Attribute::StackProtectReq))
Bill Wendlinge9e6bdf2008-11-13 01:02:14 +0000208 Result += "sspreq ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000209 if (hasAttribute(Attribute::NoRedZone))
Devang Pateld18e31a2009-06-04 22:05:33 +0000210 Result += "noredzone ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000211 if (hasAttribute(Attribute::NoImplicitFloat))
Devang Patel578efa92009-06-05 21:57:13 +0000212 Result += "noimplicitfloat ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000213 if (hasAttribute(Attribute::Naked))
Anton Korobeynikovc5ec8a72009-07-17 18:07:26 +0000214 Result += "naked ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000215 if (hasAttribute(Attribute::NonLazyBind))
John McCall3a3465b2011-06-15 20:36:13 +0000216 Result += "nonlazybind ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000217 if (hasAttribute(Attribute::AddressSafety))
Kostya Serebryany164b86b2012-01-20 17:56:17 +0000218 Result += "address_safety ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000219 if (hasAttribute(Attribute::MinSize))
Quentin Colombet9a419f62012-10-30 16:32:52 +0000220 Result += "minsize ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000221 if (hasAttribute(Attribute::StackAlignment)) {
Charles Davis1e063d12010-02-12 00:31:15 +0000222 Result += "alignstack(";
Bill Wendlingef99fe82012-09-21 15:26:31 +0000223 Result += utostr(getStackAlignment());
Charles Davis1e063d12010-02-12 00:31:15 +0000224 Result += ") ";
225 }
Bill Wendling034b94b2012-12-19 07:18:57 +0000226 if (hasAttribute(Attribute::Alignment)) {
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000227 Result += "align ";
Bill Wendlingef99fe82012-09-21 15:26:31 +0000228 Result += utostr(getAlignment());
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000229 Result += " ";
230 }
James Molloy67ae1352012-12-20 16:04:27 +0000231 if (hasAttribute(Attribute::NoDuplicate))
232 Result += "noduplicate ";
Dan Gohmanc3be0fd2008-08-05 15:51:44 +0000233 // Trim the trailing space.
Nick Lewycky73ddd4f2008-12-19 09:38:31 +0000234 assert(!Result.empty() && "Unknown attribute!");
Dan Gohmanc3be0fd2008-08-05 15:51:44 +0000235 Result.erase(Result.end()-1);
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000236 return Result;
237}
238
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000239//===----------------------------------------------------------------------===//
Bill Wendling03198882013-01-04 23:27:34 +0000240// AttrBuilder Method Implementations
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000241//===----------------------------------------------------------------------===//
242
Bill Wendling03198882013-01-04 23:27:34 +0000243void AttrBuilder::clear() {
244 Attrs.clear();
245 Alignment = StackAlignment = 0;
246}
247
248AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
249 Attrs.insert(Val);
Bill Wendling3a106e62012-10-09 19:01:18 +0000250 return *this;
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000251}
252
Bill Wendling03198882013-01-04 23:27:34 +0000253AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
254 Attrs.erase(Val);
255 if (Val == Attribute::Alignment)
256 Alignment = 0;
257 else if (Val == Attribute::StackAlignment)
258 StackAlignment = 0;
259
Bill Wendlinga19a5302012-10-14 04:10:01 +0000260 return *this;
261}
262
Bill Wendling702cc912012-10-15 20:35:56 +0000263AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
Bill Wendlingda3f9d82012-10-14 03:58:29 +0000264 if (Align == 0) return *this;
Bill Wendling03198882013-01-04 23:27:34 +0000265
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000266 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
267 assert(Align <= 0x40000000 && "Alignment too large.");
Bill Wendling03198882013-01-04 23:27:34 +0000268
269 Attrs.insert(Attribute::Alignment);
270 Alignment = Align;
Bill Wendlingda3f9d82012-10-14 03:58:29 +0000271 return *this;
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000272}
273
Bill Wendling03198882013-01-04 23:27:34 +0000274AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
275 // Default alignment, allow the target to define how to align it.
276 if (Align == 0) return *this;
277
278 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
279 assert(Align <= 0x100 && "Alignment too large.");
280
281 Attrs.insert(Attribute::StackAlignment);
282 StackAlignment = Align;
283 return *this;
284}
285
286AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) {
287 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
288 I = Attribute::AttrKind(I + 1)) {
289 if (uint64_t A = (Val & AttributeImpl::getAttrMask(I))) {
290 Attrs.insert(I);
291
292 if (I == Attribute::Alignment)
293 Alignment = 1ULL << ((A >> 16) - 1);
294 else if (I == Attribute::StackAlignment)
295 StackAlignment = 1ULL << ((A >> 26)-1);
296 }
297 }
298
Bill Wendling3a106e62012-10-09 19:01:18 +0000299 return *this;
Bill Wendling2e879bc2012-10-09 09:11:20 +0000300}
301
Bill Wendling034b94b2012-12-19 07:18:57 +0000302AttrBuilder &AttrBuilder::addAttributes(const Attribute &A) {
Bill Wendling03198882013-01-04 23:27:34 +0000303 uint64_t Mask = A.getBitMask();
304
305 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
306 I = Attribute::AttrKind(I + 1)) {
307 if (uint64_t A = (Mask & AttributeImpl::getAttrMask(I))) {
308 Attrs.insert(I);
309
310 if (I == Attribute::Alignment)
311 Alignment = 1ULL << ((A >> 16) - 1);
312 else if (I == Attribute::StackAlignment)
313 StackAlignment = 1ULL << ((A >> 26)-1);
314 }
315 }
316
Bill Wendling432e6062012-10-14 07:17:34 +0000317 return *this;
318}
319
Bill Wendling034b94b2012-12-19 07:18:57 +0000320AttrBuilder &AttrBuilder::removeAttributes(const Attribute &A){
Bill Wendling03198882013-01-04 23:27:34 +0000321 uint64_t Mask = A.getBitMask();
322
323 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
324 I = Attribute::AttrKind(I + 1)) {
325 if (Mask & AttributeImpl::getAttrMask(I)) {
326 Attrs.erase(I);
327
328 if (I == Attribute::Alignment)
329 Alignment = 0;
330 else if (I == Attribute::StackAlignment)
331 StackAlignment = 0;
332 }
333 }
334
Bill Wendling5886b7b2012-10-14 06:39:53 +0000335 return *this;
Bill Wendling8831c062012-10-09 00:01:21 +0000336}
337
Bill Wendling22bd6412013-01-03 01:54:39 +0000338bool AttrBuilder::contains(Attribute::AttrKind A) const {
Bill Wendling03198882013-01-04 23:27:34 +0000339 return Attrs.count(A);
Bill Wendling7d2f2492012-10-10 07:36:45 +0000340}
341
Bill Wendling702cc912012-10-15 20:35:56 +0000342bool AttrBuilder::hasAttributes() const {
Bill Wendling03198882013-01-04 23:27:34 +0000343 return !Attrs.empty();
Bill Wendlingf385f4c2012-10-08 23:27:46 +0000344}
Bill Wendling60507d52013-01-04 20:54:35 +0000345
Bill Wendling034b94b2012-12-19 07:18:57 +0000346bool AttrBuilder::hasAttributes(const Attribute &A) const {
Bill Wendling03198882013-01-04 23:27:34 +0000347 return getBitMask() & A.getBitMask();
Bill Wendling8831c062012-10-09 00:01:21 +0000348}
Bill Wendling60507d52013-01-04 20:54:35 +0000349
Bill Wendling702cc912012-10-15 20:35:56 +0000350bool AttrBuilder::hasAlignmentAttr() const {
Bill Wendling03198882013-01-04 23:27:34 +0000351 return Alignment != 0;
Bill Wendlingf385f4c2012-10-08 23:27:46 +0000352}
353
Bill Wendling03198882013-01-04 23:27:34 +0000354uint64_t AttrBuilder::getBitMask() const {
355 uint64_t Mask = 0;
356
357 for (DenseSet<Attribute::AttrKind>::const_iterator I = Attrs.begin(),
358 E = Attrs.end(); I != E; ++I) {
359 Attribute::AttrKind Kind = *I;
360
361 if (Kind == Attribute::Alignment)
362 Mask |= (Log2_32(Alignment) + 1) << 16;
363 else if (Kind == Attribute::StackAlignment)
364 Mask |= (Log2_32(StackAlignment) + 1) << 26;
365 else
366 Mask |= AttributeImpl::getAttrMask(Kind);
367 }
368
369 return Mask;
Bill Wendlingf385f4c2012-10-08 23:27:46 +0000370}
371
Bill Wendling03198882013-01-04 23:27:34 +0000372bool AttrBuilder::operator==(const AttrBuilder &B) {
373 SmallVector<Attribute::AttrKind, 8> This(Attrs.begin(), Attrs.end());
374 SmallVector<Attribute::AttrKind, 8> That(B.Attrs.begin(), B.Attrs.end());
375 return This == That;
Bill Wendling7d2f2492012-10-10 07:36:45 +0000376}
377
Chris Lattner58d74912008-03-12 17:45:29 +0000378//===----------------------------------------------------------------------===//
Bill Wendling2c79ecb2012-09-26 21:07:29 +0000379// AttributeImpl Definition
380//===----------------------------------------------------------------------===//
381
Bill Wendling1bbd6442013-01-05 01:36:54 +0000382AttributeImpl::AttributeImpl(LLVMContext &C, uint64_t data)
383 : Context(C) {
Bill Wendling7c1683d2012-12-29 12:29:38 +0000384 Data = ConstantInt::get(Type::getInt64Ty(C), data);
385}
Bill Wendling1bbd6442013-01-05 01:36:54 +0000386AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind data)
387 : Context(C) {
Bill Wendling979aff62012-12-30 02:22:16 +0000388 Data = ConstantInt::get(Type::getInt64Ty(C), data);
389}
390AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind data,
Bill Wendling1bbd6442013-01-05 01:36:54 +0000391 ArrayRef<Constant*> values)
392 : Context(C) {
Bill Wendling979aff62012-12-30 02:22:16 +0000393 Data = ConstantInt::get(Type::getInt64Ty(C), data);
394 Vals.reserve(values.size());
395 Vals.append(values.begin(), values.end());
396}
Bill Wendling1bbd6442013-01-05 01:36:54 +0000397AttributeImpl::AttributeImpl(LLVMContext &C, StringRef data)
398 : Context(C) {
Bill Wendling979aff62012-12-30 02:22:16 +0000399 Data = ConstantDataArray::getString(C, data);
400}
Bill Wendling7c1683d2012-12-29 12:29:38 +0000401
Bill Wendling60507d52013-01-04 20:54:35 +0000402bool AttributeImpl::operator==(Attribute::AttrKind Kind) const {
Bill Wendling529ec712012-12-30 01:38:39 +0000403 if (ConstantInt *CI = dyn_cast<ConstantInt>(Data))
404 return CI->getZExtValue() == Kind;
405 return false;
406}
Bill Wendling60507d52013-01-04 20:54:35 +0000407bool AttributeImpl::operator!=(Attribute::AttrKind Kind) const {
408 return !(*this == Kind);
409}
Bill Wendling529ec712012-12-30 01:38:39 +0000410
Bill Wendling60507d52013-01-04 20:54:35 +0000411bool AttributeImpl::operator==(StringRef Kind) const {
Bill Wendling529ec712012-12-30 01:38:39 +0000412 if (ConstantDataArray *CDA = dyn_cast<ConstantDataArray>(Data))
413 if (CDA->isString())
414 return CDA->getAsString() == Kind;
415 return false;
416}
Bill Wendling60507d52013-01-04 20:54:35 +0000417bool AttributeImpl::operator!=(StringRef Kind) const {
418 return !(*this == Kind);
419}
Bill Wendling529ec712012-12-30 01:38:39 +0000420
Bill Wendlingc966e082012-12-30 01:05:42 +0000421uint64_t AttributeImpl::getBitMask() const {
Bill Wendling529ec712012-12-30 01:38:39 +0000422 // FIXME: Remove this.
Bill Wendling7c1683d2012-12-29 12:29:38 +0000423 return cast<ConstantInt>(Data)->getZExtValue();
424}
425
Bill Wendling60507d52013-01-04 20:54:35 +0000426uint64_t AttributeImpl::getAttrMask(Attribute::AttrKind Val) {
Bill Wendling67658342012-10-09 07:45:08 +0000427 switch (Val) {
Bill Wendling03198882013-01-04 23:27:34 +0000428 case Attribute::EndAttrKinds: break;
Bill Wendling034b94b2012-12-19 07:18:57 +0000429 case Attribute::None: return 0;
430 case Attribute::ZExt: return 1 << 0;
431 case Attribute::SExt: return 1 << 1;
432 case Attribute::NoReturn: return 1 << 2;
433 case Attribute::InReg: return 1 << 3;
434 case Attribute::StructRet: return 1 << 4;
435 case Attribute::NoUnwind: return 1 << 5;
436 case Attribute::NoAlias: return 1 << 6;
437 case Attribute::ByVal: return 1 << 7;
438 case Attribute::Nest: return 1 << 8;
439 case Attribute::ReadNone: return 1 << 9;
440 case Attribute::ReadOnly: return 1 << 10;
441 case Attribute::NoInline: return 1 << 11;
442 case Attribute::AlwaysInline: return 1 << 12;
443 case Attribute::OptimizeForSize: return 1 << 13;
444 case Attribute::StackProtect: return 1 << 14;
445 case Attribute::StackProtectReq: return 1 << 15;
446 case Attribute::Alignment: return 31 << 16;
447 case Attribute::NoCapture: return 1 << 21;
448 case Attribute::NoRedZone: return 1 << 22;
449 case Attribute::NoImplicitFloat: return 1 << 23;
450 case Attribute::Naked: return 1 << 24;
451 case Attribute::InlineHint: return 1 << 25;
452 case Attribute::StackAlignment: return 7 << 26;
453 case Attribute::ReturnsTwice: return 1 << 29;
454 case Attribute::UWTable: return 1 << 30;
455 case Attribute::NonLazyBind: return 1U << 31;
456 case Attribute::AddressSafety: return 1ULL << 32;
457 case Attribute::MinSize: return 1ULL << 33;
James Molloy67ae1352012-12-20 16:04:27 +0000458 case Attribute::NoDuplicate: return 1ULL << 34;
Bill Wendling67658342012-10-09 07:45:08 +0000459 }
460 llvm_unreachable("Unsupported attribute type");
461}
462
Bill Wendling60507d52013-01-04 20:54:35 +0000463bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
Bill Wendlingc966e082012-12-30 01:05:42 +0000464 return (getBitMask() & getAttrMask(A)) != 0;
Bill Wendling8e635db2012-10-08 21:47:17 +0000465}
Bill Wendling2c79ecb2012-09-26 21:07:29 +0000466
Bill Wendlingf6670722012-12-20 01:36:59 +0000467bool AttributeImpl::hasAttributes() const {
Bill Wendlingc966e082012-12-30 01:05:42 +0000468 return getBitMask() != 0;
Bill Wendling8e635db2012-10-08 21:47:17 +0000469}
Bill Wendling2c79ecb2012-09-26 21:07:29 +0000470
Bill Wendlingf6670722012-12-20 01:36:59 +0000471uint64_t AttributeImpl::getAlignment() const {
Bill Wendlingc966e082012-12-30 01:05:42 +0000472 return getBitMask() & getAttrMask(Attribute::Alignment);
Bill Wendling8e635db2012-10-08 21:47:17 +0000473}
Bill Wendling2c79ecb2012-09-26 21:07:29 +0000474
Bill Wendling1bbd6442013-01-05 01:36:54 +0000475void AttributeImpl::setAlignment(unsigned Align) {
476 Vals.push_back(ConstantInt::get(Type::getInt64Ty(Context), Align));
477}
478
Bill Wendlingf6670722012-12-20 01:36:59 +0000479uint64_t AttributeImpl::getStackAlignment() const {
Bill Wendlingc966e082012-12-30 01:05:42 +0000480 return getBitMask() & getAttrMask(Attribute::StackAlignment);
Bill Wendling8e635db2012-10-08 21:47:17 +0000481}
Bill Wendling2c79ecb2012-09-26 21:07:29 +0000482
Bill Wendling1bbd6442013-01-05 01:36:54 +0000483void AttributeImpl::setStackAlignment(unsigned Align) {
484 Vals.push_back(ConstantInt::get(Type::getInt64Ty(Context), Align));
485}
486
Bill Wendling2c79ecb2012-09-26 21:07:29 +0000487//===----------------------------------------------------------------------===//
Bill Wendling18e72112012-12-19 22:42:22 +0000488// AttributeSetImpl Definition
Chris Lattner58d74912008-03-12 17:45:29 +0000489//===----------------------------------------------------------------------===//
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000490
Bill Wendling99faa3b2012-12-07 23:16:57 +0000491AttributeSet AttributeSet::get(LLVMContext &C,
Bill Wendling1ce47ac2012-12-12 19:21:53 +0000492 ArrayRef<AttributeWithIndex> Attrs) {
Devang Patel05988662008-09-25 21:00:45 +0000493 // If there are no attributes then return a null AttributesList pointer.
Chris Lattnerd509d0b2012-05-28 01:47:44 +0000494 if (Attrs.empty())
Bill Wendling99faa3b2012-12-07 23:16:57 +0000495 return AttributeSet();
Bill Wendling77898682012-10-16 06:01:44 +0000496
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000497#ifndef NDEBUG
Chris Lattnerd509d0b2012-05-28 01:47:44 +0000498 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
Bill Wendling77898682012-10-16 06:01:44 +0000499 assert(Attrs[i].Attrs.hasAttributes() &&
Devang Patel05988662008-09-25 21:00:45 +0000500 "Pointless attribute!");
Chris Lattner58d74912008-03-12 17:45:29 +0000501 assert((!i || Attrs[i-1].Index < Attrs[i].Index) &&
Devang Patel05988662008-09-25 21:00:45 +0000502 "Misordered AttributesList!");
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000503 }
504#endif
Bill Wendling77898682012-10-16 06:01:44 +0000505
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000506 // Otherwise, build a key to look up the existing attributes.
Bill Wendling0976e002012-11-20 05:09:20 +0000507 LLVMContextImpl *pImpl = C.pImpl;
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000508 FoldingSetNodeID ID;
Bill Wendling18e72112012-12-19 22:42:22 +0000509 AttributeSetImpl::Profile(ID, Attrs);
Bill Wendling77898682012-10-16 06:01:44 +0000510
Bill Wendling0976e002012-11-20 05:09:20 +0000511 void *InsertPoint;
Bill Wendling1bbd6442013-01-05 01:36:54 +0000512 AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
Bill Wendling77898682012-10-16 06:01:44 +0000513
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000514 // If we didn't find any existing attributes of the same shape then
515 // create a new one and insert it.
Bill Wendling0976e002012-11-20 05:09:20 +0000516 if (!PA) {
Bill Wendling5f93e2b2012-12-19 23:55:43 +0000517 PA = new AttributeSetImpl(C, Attrs);
Bill Wendling0976e002012-11-20 05:09:20 +0000518 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000519 }
Bill Wendling77898682012-10-16 06:01:44 +0000520
Devang Patel05988662008-09-25 21:00:45 +0000521 // Return the AttributesList that we found or created.
Bill Wendling99faa3b2012-12-07 23:16:57 +0000522 return AttributeSet(PA);
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000523}
524
Bill Wendling1bbd6442013-01-05 01:36:54 +0000525AttributeSet AttributeSet::get(LLVMContext &C, unsigned Idx, AttrBuilder &B) {
526 SmallVector<AttributeWithIndex, 8> Attrs;
527 for (AttrBuilder::iterator I = B.begin(), E = B.end(); I != E; ++I) {
528 Attribute::AttrKind Kind = *I;
529 Attribute A = Attribute::get(C, Kind);
530
531 if (Kind == Attribute::Alignment)
532 A.setAlignment(B.getAlignment());
533 else if (Kind == Attribute::StackAlignment)
534 A.setStackAlignment(B.getStackAlignment());
535
536 Attrs.push_back(AttributeWithIndex::get(Idx, A));
537 }
538
539 return get(C, Attrs);
540}
541
Chris Lattner58d74912008-03-12 17:45:29 +0000542//===----------------------------------------------------------------------===//
Bill Wendling99faa3b2012-12-07 23:16:57 +0000543// AttributeSet Method Implementations
Chris Lattner58d74912008-03-12 17:45:29 +0000544//===----------------------------------------------------------------------===//
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000545
Bill Wendling99faa3b2012-12-07 23:16:57 +0000546const AttributeSet &AttributeSet::operator=(const AttributeSet &RHS) {
Bill Wendling0976e002012-11-20 05:09:20 +0000547 AttrList = RHS.AttrList;
548 return *this;
Chris Lattner58d74912008-03-12 17:45:29 +0000549}
550
Bill Wendling77898682012-10-16 06:01:44 +0000551/// getNumSlots - Return the number of slots used in this attribute list.
Chris Lattner58d74912008-03-12 17:45:29 +0000552/// This is the number of arguments that have an attribute set on them
553/// (including the function itself).
Bill Wendling99faa3b2012-12-07 23:16:57 +0000554unsigned AttributeSet::getNumSlots() const {
Bill Wendling60507d52013-01-04 20:54:35 +0000555 return AttrList ? AttrList->getNumAttributes() : 0;
Chris Lattner58d74912008-03-12 17:45:29 +0000556}
557
Devang Patel05988662008-09-25 21:00:45 +0000558/// getSlot - Return the AttributeWithIndex at the specified slot. This
559/// holds a number plus a set of attributes.
Bill Wendling99faa3b2012-12-07 23:16:57 +0000560const AttributeWithIndex &AttributeSet::getSlot(unsigned Slot) const {
Bill Wendling60507d52013-01-04 20:54:35 +0000561 assert(AttrList && Slot < AttrList->getNumAttributes() &&
562 "Slot # out of range!");
563 return AttrList->getAttributes()[Slot];
Chris Lattner58d74912008-03-12 17:45:29 +0000564}
565
Bill Wendling831737d2012-12-30 10:32:01 +0000566bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{
567 return getAttributes(Index).hasAttribute(Kind);
568}
569
570bool AttributeSet::hasAttributes(unsigned Index) const {
571 return getAttributes(Index).hasAttributes();
572}
573
574std::string AttributeSet::getAsString(unsigned Index) const {
575 return getAttributes(Index).getAsString();
576}
577
578unsigned AttributeSet::getStackAlignment(unsigned Index) const {
579 return getAttributes(Index).getStackAlignment();
580}
581
582uint64_t AttributeSet::getBitMask(unsigned Index) const {
583 // FIXME: Remove this.
584 return getAttributes(Index).getBitMask();
585}
586
Bill Wendling77898682012-10-16 06:01:44 +0000587/// getAttributes - The attributes for the specified index are returned.
Bill Wendling92e287f2012-12-31 11:51:54 +0000588/// Attributes for the result are denoted with Idx = 0. Function attributes are
589/// denoted with Idx = ~0.
Bill Wendling034b94b2012-12-19 07:18:57 +0000590Attribute AttributeSet::getAttributes(unsigned Idx) const {
591 if (AttrList == 0) return Attribute();
Bill Wendling77898682012-10-16 06:01:44 +0000592
Bill Wendling60507d52013-01-04 20:54:35 +0000593 ArrayRef<AttributeWithIndex> Attrs = AttrList->getAttributes();
Chris Lattner58d74912008-03-12 17:45:29 +0000594 for (unsigned i = 0, e = Attrs.size(); i != e && Attrs[i].Index <= Idx; ++i)
595 if (Attrs[i].Index == Idx)
596 return Attrs[i].Attrs;
Bill Wendlingef99fe82012-09-21 15:26:31 +0000597
Bill Wendling034b94b2012-12-19 07:18:57 +0000598 return Attribute();
Chris Lattner58d74912008-03-12 17:45:29 +0000599}
600
601/// hasAttrSomewhere - Return true if the specified attribute is set for at
602/// least one parameter or for the return value.
Bill Wendling629fb822012-12-22 00:37:52 +0000603bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr) const {
Devang Patel05988662008-09-25 21:00:45 +0000604 if (AttrList == 0) return false;
Bill Wendling7d2f2492012-10-10 07:36:45 +0000605
Bill Wendling60507d52013-01-04 20:54:35 +0000606 ArrayRef<AttributeWithIndex> Attrs = AttrList->getAttributes();
Chris Lattner58d74912008-03-12 17:45:29 +0000607 for (unsigned i = 0, e = Attrs.size(); i != e; ++i)
Bill Wendling7d2f2492012-10-10 07:36:45 +0000608 if (Attrs[i].Attrs.hasAttribute(Attr))
Chris Lattner58d74912008-03-12 17:45:29 +0000609 return true;
Bill Wendling0976e002012-11-20 05:09:20 +0000610
Chris Lattner58d74912008-03-12 17:45:29 +0000611 return false;
612}
613
Bill Wendling99faa3b2012-12-07 23:16:57 +0000614AttributeSet AttributeSet::addAttr(LLVMContext &C, unsigned Idx,
Bill Wendling9d30e722012-12-31 00:49:59 +0000615 Attribute Attrs) const {
Bill Wendling034b94b2012-12-19 07:18:57 +0000616 Attribute OldAttrs = getAttributes(Idx);
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000617#ifndef NDEBUG
618 // FIXME it is not obvious how this should work for alignment.
619 // For now, say we can't change a known alignment.
Bill Wendlingef99fe82012-09-21 15:26:31 +0000620 unsigned OldAlign = OldAttrs.getAlignment();
621 unsigned NewAlign = Attrs.getAlignment();
Anton Korobeynikov4aefd6b2008-02-20 12:07:57 +0000622 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000623 "Attempt to change alignment!");
624#endif
Bill Wendling77898682012-10-16 06:01:44 +0000625
Bill Wendling702cc912012-10-15 20:35:56 +0000626 AttrBuilder NewAttrs =
627 AttrBuilder(OldAttrs).addAttributes(Attrs);
628 if (NewAttrs == AttrBuilder(OldAttrs))
Chris Lattner58d74912008-03-12 17:45:29 +0000629 return *this;
Bill Wendling77898682012-10-16 06:01:44 +0000630
Devang Patel05988662008-09-25 21:00:45 +0000631 SmallVector<AttributeWithIndex, 8> NewAttrList;
632 if (AttrList == 0)
633 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Chris Lattner58d74912008-03-12 17:45:29 +0000634 else {
Bill Wendling60507d52013-01-04 20:54:35 +0000635 ArrayRef<AttributeWithIndex> OldAttrList = AttrList->getAttributes();
Chris Lattner58d74912008-03-12 17:45:29 +0000636 unsigned i = 0, e = OldAttrList.size();
637 // Copy attributes for arguments before this one.
638 for (; i != e && OldAttrList[i].Index < Idx; ++i)
639 NewAttrList.push_back(OldAttrList[i]);
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000640
Chris Lattner58d74912008-03-12 17:45:29 +0000641 // If there are attributes already at this index, merge them in.
642 if (i != e && OldAttrList[i].Index == Idx) {
Bill Wendlingc4167952012-10-14 07:35:59 +0000643 Attrs =
Bill Wendling034b94b2012-12-19 07:18:57 +0000644 Attribute::get(C, AttrBuilder(Attrs).
Bill Wendlingc4167952012-10-14 07:35:59 +0000645 addAttributes(OldAttrList[i].Attrs));
Chris Lattner58d74912008-03-12 17:45:29 +0000646 ++i;
647 }
Bill Wendling77898682012-10-16 06:01:44 +0000648
Devang Patel05988662008-09-25 21:00:45 +0000649 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Bill Wendling77898682012-10-16 06:01:44 +0000650
Chris Lattner58d74912008-03-12 17:45:29 +0000651 // Copy attributes for arguments after this one.
Bill Wendling77898682012-10-16 06:01:44 +0000652 NewAttrList.insert(NewAttrList.end(),
Chris Lattner58d74912008-03-12 17:45:29 +0000653 OldAttrList.begin()+i, OldAttrList.end());
654 }
Bill Wendling77898682012-10-16 06:01:44 +0000655
Bill Wendling0976e002012-11-20 05:09:20 +0000656 return get(C, NewAttrList);
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000657}
658
Bill Wendling99faa3b2012-12-07 23:16:57 +0000659AttributeSet AttributeSet::removeAttr(LLVMContext &C, unsigned Idx,
Bill Wendling9d30e722012-12-31 00:49:59 +0000660 Attribute Attrs) const {
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000661#ifndef NDEBUG
662 // FIXME it is not obvious how this should work for alignment.
663 // For now, say we can't pass in alignment, which no current use does.
Bill Wendling034b94b2012-12-19 07:18:57 +0000664 assert(!Attrs.hasAttribute(Attribute::Alignment) &&
Bill Wendling67658342012-10-09 07:45:08 +0000665 "Attempt to exclude alignment!");
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000666#endif
Bill Wendling99faa3b2012-12-07 23:16:57 +0000667 if (AttrList == 0) return AttributeSet();
Bill Wendling77898682012-10-16 06:01:44 +0000668
Bill Wendling034b94b2012-12-19 07:18:57 +0000669 Attribute OldAttrs = getAttributes(Idx);
Bill Wendling702cc912012-10-15 20:35:56 +0000670 AttrBuilder NewAttrs =
671 AttrBuilder(OldAttrs).removeAttributes(Attrs);
672 if (NewAttrs == AttrBuilder(OldAttrs))
Chris Lattner58d74912008-03-12 17:45:29 +0000673 return *this;
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000674
Devang Patel05988662008-09-25 21:00:45 +0000675 SmallVector<AttributeWithIndex, 8> NewAttrList;
Bill Wendling60507d52013-01-04 20:54:35 +0000676 ArrayRef<AttributeWithIndex> OldAttrList = AttrList->getAttributes();
Chris Lattner58d74912008-03-12 17:45:29 +0000677 unsigned i = 0, e = OldAttrList.size();
Bill Wendling77898682012-10-16 06:01:44 +0000678
Chris Lattner58d74912008-03-12 17:45:29 +0000679 // Copy attributes for arguments before this one.
680 for (; i != e && OldAttrList[i].Index < Idx; ++i)
681 NewAttrList.push_back(OldAttrList[i]);
Bill Wendling77898682012-10-16 06:01:44 +0000682
Chris Lattner58d74912008-03-12 17:45:29 +0000683 // If there are attributes already at this index, merge them in.
684 assert(OldAttrList[i].Index == Idx && "Attribute isn't set?");
Bill Wendling034b94b2012-12-19 07:18:57 +0000685 Attrs = Attribute::get(C, AttrBuilder(OldAttrList[i].Attrs).
Bill Wendling5886b7b2012-10-14 06:39:53 +0000686 removeAttributes(Attrs));
Chris Lattner58d74912008-03-12 17:45:29 +0000687 ++i;
Bill Wendling7be78482012-10-14 08:54:26 +0000688 if (Attrs.hasAttributes()) // If any attributes left for this param, add them.
Devang Patel05988662008-09-25 21:00:45 +0000689 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Bill Wendling77898682012-10-16 06:01:44 +0000690
Chris Lattner58d74912008-03-12 17:45:29 +0000691 // Copy attributes for arguments after this one.
Bill Wendling77898682012-10-16 06:01:44 +0000692 NewAttrList.insert(NewAttrList.end(),
Chris Lattner58d74912008-03-12 17:45:29 +0000693 OldAttrList.begin()+i, OldAttrList.end());
Bill Wendling77898682012-10-16 06:01:44 +0000694
Bill Wendling0976e002012-11-20 05:09:20 +0000695 return get(C, NewAttrList);
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000696}
697
Bill Wendling99faa3b2012-12-07 23:16:57 +0000698void AttributeSet::dump() const {
David Greeneef1894e2010-01-05 01:29:58 +0000699 dbgs() << "PAL[ ";
Chris Lattner58d74912008-03-12 17:45:29 +0000700 for (unsigned i = 0; i < getNumSlots(); ++i) {
Devang Patel05988662008-09-25 21:00:45 +0000701 const AttributeWithIndex &PAWI = getSlot(i);
Bill Wendling60507d52013-01-04 20:54:35 +0000702 dbgs() << "{ " << PAWI.Index << ", " << PAWI.Attrs.getAsString() << " } ";
Chris Lattner58d74912008-03-12 17:45:29 +0000703 }
Bill Wendling77898682012-10-16 06:01:44 +0000704
David Greeneef1894e2010-01-05 01:29:58 +0000705 dbgs() << "]\n";
Duncan Sandsad9a9e12008-01-06 18:27:01 +0000706}