blob: 117b510288b1727b2b8c6971a41eaddeac3331af [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;
Benjamin Kramerf06eb262013-01-05 12:08:00 +000048 // FIXME: Don't look up ConstantInts here.
49 ID.AddPointer(ConstantInt::get(Type::getInt64Ty(Context), B.getBitMask()));
Bill Wendling8e635db2012-10-08 21:47:17 +000050
51 void *InsertPoint;
Bill Wendlingf6670722012-12-20 01:36:59 +000052 AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
Bill Wendling8e635db2012-10-08 21:47:17 +000053
54 if (!PA) {
55 // If we didn't find any existing attributes of the same shape then create a
56 // new one and insert it.
Bill Wendlingc966e082012-12-30 01:05:42 +000057 PA = new AttributeImpl(Context, B.getBitMask());
Bill Wendling8e635db2012-10-08 21:47:17 +000058 pImpl->AttrsSet.InsertNode(PA, InsertPoint);
59 }
60
61 // Return the AttributesList that we found or created.
Bill Wendling034b94b2012-12-19 07:18:57 +000062 return Attribute(PA);
Bill Wendling8e635db2012-10-08 21:47:17 +000063}
64
Bill Wendling629fb822012-12-22 00:37:52 +000065bool Attribute::hasAttribute(AttrKind Val) const {
Bill Wendling27107f62012-12-20 21:28:43 +000066 return pImpl && pImpl->hasAttribute(Val);
Bill Wendlinge66f3d32012-10-05 06:44:41 +000067}
68
Bill Wendling034b94b2012-12-19 07:18:57 +000069bool Attribute::hasAttributes() const {
Bill Wendling27107f62012-12-20 21:28:43 +000070 return pImpl && pImpl->hasAttributes();
Bill Wendling05cc40d2012-10-15 05:40:12 +000071}
72
Bill Wendlinge66f3d32012-10-05 06:44:41 +000073/// This returns the alignment field of an attribute as a byte alignment value.
Bill Wendling034b94b2012-12-19 07:18:57 +000074unsigned Attribute::getAlignment() const {
75 if (!hasAttribute(Attribute::Alignment))
Bill Wendling9ef99c92012-10-09 20:56:48 +000076 return 0;
Bill Wendling27107f62012-12-20 21:28:43 +000077 return 1U << ((pImpl->getAlignment() >> 16) - 1);
Bill Wendlinge66f3d32012-10-05 06:44:41 +000078}
79
Bill Wendling1bbd6442013-01-05 01:36:54 +000080void Attribute::setAlignment(unsigned Align) {
81 assert(hasAttribute(Attribute::Alignment) &&
82 "Trying to set the alignment on a non-alignment attribute!");
83 pImpl->setAlignment(Align);
84}
85
Bill Wendlinge66f3d32012-10-05 06:44:41 +000086/// This returns the stack alignment field of an attribute as a byte alignment
87/// value.
Bill Wendling034b94b2012-12-19 07:18:57 +000088unsigned Attribute::getStackAlignment() const {
89 if (!hasAttribute(Attribute::StackAlignment))
Bill Wendling9ef99c92012-10-09 20:56:48 +000090 return 0;
Bill Wendling27107f62012-12-20 21:28:43 +000091 return 1U << ((pImpl->getStackAlignment() >> 26) - 1);
Bill Wendlinge66f3d32012-10-05 06:44:41 +000092}
93
Bill Wendling1bbd6442013-01-05 01:36:54 +000094void Attribute::setStackAlignment(unsigned Align) {
95 assert(hasAttribute(Attribute::StackAlignment) &&
96 "Trying to set the stack alignment on a non-alignment attribute!");
97 pImpl->setStackAlignment(Align);
98}
99
Bill Wendling92e287f2012-12-31 11:51:54 +0000100bool Attribute::operator==(AttrKind K) const {
Bill Wendling60507d52013-01-04 20:54:35 +0000101 return pImpl && *pImpl == K;
Bill Wendling92e287f2012-12-31 11:51:54 +0000102}
Bill Wendling92e287f2012-12-31 11:51:54 +0000103bool Attribute::operator!=(AttrKind K) const {
Bill Wendling60507d52013-01-04 20:54:35 +0000104 return !(*this == K);
Bill Wendling92e287f2012-12-31 11:51:54 +0000105}
106
Bill Wendlingc966e082012-12-30 01:05:42 +0000107uint64_t Attribute::getBitMask() const {
108 return pImpl ? pImpl->getBitMask() : 0;
Bill Wendlingb10c88f2012-10-07 08:55:05 +0000109}
110
Bill Wendling034b94b2012-12-19 07:18:57 +0000111Attribute Attribute::typeIncompatible(Type *Ty) {
Bill Wendling702cc912012-10-15 20:35:56 +0000112 AttrBuilder Incompatible;
Bill Wendling77898682012-10-16 06:01:44 +0000113
Bill Wendling3a106e62012-10-09 19:01:18 +0000114 if (!Ty->isIntegerTy())
Bill Wendling034b94b2012-12-19 07:18:57 +0000115 // Attribute that only apply to integers.
116 Incompatible.addAttribute(Attribute::SExt)
117 .addAttribute(Attribute::ZExt);
Bill Wendling77898682012-10-16 06:01:44 +0000118
Bill Wendling3a106e62012-10-09 19:01:18 +0000119 if (!Ty->isPointerTy())
Bill Wendling034b94b2012-12-19 07:18:57 +0000120 // Attribute that only apply to pointers.
121 Incompatible.addAttribute(Attribute::ByVal)
122 .addAttribute(Attribute::Nest)
123 .addAttribute(Attribute::NoAlias)
124 .addAttribute(Attribute::NoCapture)
125 .addAttribute(Attribute::StructRet);
Bill Wendling77898682012-10-16 06:01:44 +0000126
Bill Wendling034b94b2012-12-19 07:18:57 +0000127 return Attribute::get(Ty->getContext(), Incompatible);
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000128}
129
Bill Wendling702cc912012-10-15 20:35:56 +0000130/// encodeLLVMAttributesForBitcode - This returns an integer containing an
131/// encoding of all the LLVM attributes found in the given attribute bitset.
132/// Any change to this encoding is a breaking change to bitcode compatibility.
Bill Wendling034b94b2012-12-19 07:18:57 +0000133uint64_t Attribute::encodeLLVMAttributesForBitcode(Attribute Attrs) {
Bill Wendling702cc912012-10-15 20:35:56 +0000134 // FIXME: It doesn't make sense to store the alignment information as an
135 // expanded out value, we should store it as a log2 value. However, we can't
136 // just change that here without breaking bitcode compatibility. If this ever
137 // becomes a problem in practice, we should introduce new tag numbers in the
138 // bitcode file and have those tags use a more efficiently encoded alignment
139 // field.
140
141 // Store the alignment in the bitcode as a 16-bit raw value instead of a 5-bit
142 // log2 encoded value. Shift the bits above the alignment up by 11 bits.
Bill Wendlingc966e082012-12-30 01:05:42 +0000143 uint64_t EncodedAttrs = Attrs.getBitMask() & 0xffff;
Bill Wendling034b94b2012-12-19 07:18:57 +0000144 if (Attrs.hasAttribute(Attribute::Alignment))
Bill Wendling702cc912012-10-15 20:35:56 +0000145 EncodedAttrs |= Attrs.getAlignment() << 16;
Bill Wendlingc966e082012-12-30 01:05:42 +0000146 EncodedAttrs |= (Attrs.getBitMask() & (0xffffULL << 21)) << 11;
Bill Wendling702cc912012-10-15 20:35:56 +0000147 return EncodedAttrs;
148}
149
150/// decodeLLVMAttributesForBitcode - This returns an attribute bitset containing
151/// the LLVM attributes that have been decoded from the given integer. This
152/// function must stay in sync with 'encodeLLVMAttributesForBitcode'.
Bill Wendling034b94b2012-12-19 07:18:57 +0000153Attribute Attribute::decodeLLVMAttributesForBitcode(LLVMContext &C,
Bill Wendling702cc912012-10-15 20:35:56 +0000154 uint64_t EncodedAttrs) {
155 // The alignment is stored as a 16-bit raw value from bits 31--16. We shift
156 // the bits above 31 down by 11 bits.
157 unsigned Alignment = (EncodedAttrs & (0xffffULL << 16)) >> 16;
158 assert((!Alignment || isPowerOf2_32(Alignment)) &&
159 "Alignment must be a power of two.");
160
161 AttrBuilder B(EncodedAttrs & 0xffff);
162 if (Alignment)
163 B.addAlignmentAttr(Alignment);
Nadav Roteme7439422012-10-22 17:33:31 +0000164 B.addRawValue((EncodedAttrs & (0xffffULL << 32)) >> 11);
Bill Wendling034b94b2012-12-19 07:18:57 +0000165 return Attribute::get(C, B);
Bill Wendling702cc912012-10-15 20:35:56 +0000166}
167
Bill Wendling034b94b2012-12-19 07:18:57 +0000168std::string Attribute::getAsString() const {
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000169 std::string Result;
Bill Wendling034b94b2012-12-19 07:18:57 +0000170 if (hasAttribute(Attribute::ZExt))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000171 Result += "zeroext ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000172 if (hasAttribute(Attribute::SExt))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000173 Result += "signext ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000174 if (hasAttribute(Attribute::NoReturn))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000175 Result += "noreturn ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000176 if (hasAttribute(Attribute::NoUnwind))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000177 Result += "nounwind ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000178 if (hasAttribute(Attribute::UWTable))
Rafael Espindolafc2bb8c2011-05-25 03:44:17 +0000179 Result += "uwtable ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000180 if (hasAttribute(Attribute::ReturnsTwice))
Rafael Espindola25456ef2011-10-03 14:45:37 +0000181 Result += "returns_twice ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000182 if (hasAttribute(Attribute::InReg))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000183 Result += "inreg ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000184 if (hasAttribute(Attribute::NoAlias))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000185 Result += "noalias ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000186 if (hasAttribute(Attribute::NoCapture))
Nick Lewycky73ddd4f2008-12-19 09:38:31 +0000187 Result += "nocapture ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000188 if (hasAttribute(Attribute::StructRet))
Anton Korobeynikovc5ec8a72009-07-17 18:07:26 +0000189 Result += "sret ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000190 if (hasAttribute(Attribute::ByVal))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000191 Result += "byval ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000192 if (hasAttribute(Attribute::Nest))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000193 Result += "nest ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000194 if (hasAttribute(Attribute::ReadNone))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000195 Result += "readnone ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000196 if (hasAttribute(Attribute::ReadOnly))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000197 Result += "readonly ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000198 if (hasAttribute(Attribute::OptimizeForSize))
Devang Patel19c87462008-09-26 22:53:05 +0000199 Result += "optsize ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000200 if (hasAttribute(Attribute::NoInline))
Devang Patel19c87462008-09-26 22:53:05 +0000201 Result += "noinline ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000202 if (hasAttribute(Attribute::InlineHint))
Jakob Stoklund Olesen570a4a52010-02-06 01:16:28 +0000203 Result += "inlinehint ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000204 if (hasAttribute(Attribute::AlwaysInline))
Devang Patel19c87462008-09-26 22:53:05 +0000205 Result += "alwaysinline ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000206 if (hasAttribute(Attribute::StackProtect))
Bill Wendlinge9e6bdf2008-11-13 01:02:14 +0000207 Result += "ssp ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000208 if (hasAttribute(Attribute::StackProtectReq))
Bill Wendlinge9e6bdf2008-11-13 01:02:14 +0000209 Result += "sspreq ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000210 if (hasAttribute(Attribute::NoRedZone))
Devang Pateld18e31a2009-06-04 22:05:33 +0000211 Result += "noredzone ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000212 if (hasAttribute(Attribute::NoImplicitFloat))
Devang Patel578efa92009-06-05 21:57:13 +0000213 Result += "noimplicitfloat ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000214 if (hasAttribute(Attribute::Naked))
Anton Korobeynikovc5ec8a72009-07-17 18:07:26 +0000215 Result += "naked ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000216 if (hasAttribute(Attribute::NonLazyBind))
John McCall3a3465b2011-06-15 20:36:13 +0000217 Result += "nonlazybind ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000218 if (hasAttribute(Attribute::AddressSafety))
Kostya Serebryany164b86b2012-01-20 17:56:17 +0000219 Result += "address_safety ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000220 if (hasAttribute(Attribute::MinSize))
Quentin Colombet9a419f62012-10-30 16:32:52 +0000221 Result += "minsize ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000222 if (hasAttribute(Attribute::StackAlignment)) {
Charles Davis1e063d12010-02-12 00:31:15 +0000223 Result += "alignstack(";
Bill Wendlingef99fe82012-09-21 15:26:31 +0000224 Result += utostr(getStackAlignment());
Charles Davis1e063d12010-02-12 00:31:15 +0000225 Result += ") ";
226 }
Bill Wendling034b94b2012-12-19 07:18:57 +0000227 if (hasAttribute(Attribute::Alignment)) {
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000228 Result += "align ";
Bill Wendlingef99fe82012-09-21 15:26:31 +0000229 Result += utostr(getAlignment());
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000230 Result += " ";
231 }
James Molloy67ae1352012-12-20 16:04:27 +0000232 if (hasAttribute(Attribute::NoDuplicate))
233 Result += "noduplicate ";
Dan Gohmanc3be0fd2008-08-05 15:51:44 +0000234 // Trim the trailing space.
Nick Lewycky73ddd4f2008-12-19 09:38:31 +0000235 assert(!Result.empty() && "Unknown attribute!");
Dan Gohmanc3be0fd2008-08-05 15:51:44 +0000236 Result.erase(Result.end()-1);
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000237 return Result;
238}
239
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000240//===----------------------------------------------------------------------===//
Bill Wendling03198882013-01-04 23:27:34 +0000241// AttrBuilder Method Implementations
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000242//===----------------------------------------------------------------------===//
243
Bill Wendling03198882013-01-04 23:27:34 +0000244void AttrBuilder::clear() {
245 Attrs.clear();
246 Alignment = StackAlignment = 0;
247}
248
249AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
250 Attrs.insert(Val);
Bill Wendling3a106e62012-10-09 19:01:18 +0000251 return *this;
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000252}
253
Bill Wendling03198882013-01-04 23:27:34 +0000254AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
255 Attrs.erase(Val);
256 if (Val == Attribute::Alignment)
257 Alignment = 0;
258 else if (Val == Attribute::StackAlignment)
259 StackAlignment = 0;
260
Bill Wendlinga19a5302012-10-14 04:10:01 +0000261 return *this;
262}
263
Bill Wendling702cc912012-10-15 20:35:56 +0000264AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
Bill Wendlingda3f9d82012-10-14 03:58:29 +0000265 if (Align == 0) return *this;
Bill Wendling03198882013-01-04 23:27:34 +0000266
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000267 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
268 assert(Align <= 0x40000000 && "Alignment too large.");
Bill Wendling03198882013-01-04 23:27:34 +0000269
270 Attrs.insert(Attribute::Alignment);
271 Alignment = Align;
Bill Wendlingda3f9d82012-10-14 03:58:29 +0000272 return *this;
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000273}
274
Bill Wendling03198882013-01-04 23:27:34 +0000275AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
276 // Default alignment, allow the target to define how to align it.
277 if (Align == 0) return *this;
278
279 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
280 assert(Align <= 0x100 && "Alignment too large.");
281
282 Attrs.insert(Attribute::StackAlignment);
283 StackAlignment = Align;
284 return *this;
285}
286
287AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) {
288 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
289 I = Attribute::AttrKind(I + 1)) {
290 if (uint64_t A = (Val & AttributeImpl::getAttrMask(I))) {
291 Attrs.insert(I);
292
293 if (I == Attribute::Alignment)
294 Alignment = 1ULL << ((A >> 16) - 1);
295 else if (I == Attribute::StackAlignment)
296 StackAlignment = 1ULL << ((A >> 26)-1);
297 }
298 }
299
Bill Wendling3a106e62012-10-09 19:01:18 +0000300 return *this;
Bill Wendling2e879bc2012-10-09 09:11:20 +0000301}
302
Bill Wendling034b94b2012-12-19 07:18:57 +0000303AttrBuilder &AttrBuilder::addAttributes(const Attribute &A) {
Bill Wendling03198882013-01-04 23:27:34 +0000304 uint64_t Mask = A.getBitMask();
305
306 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
307 I = Attribute::AttrKind(I + 1)) {
308 if (uint64_t A = (Mask & AttributeImpl::getAttrMask(I))) {
309 Attrs.insert(I);
310
311 if (I == Attribute::Alignment)
312 Alignment = 1ULL << ((A >> 16) - 1);
313 else if (I == Attribute::StackAlignment)
314 StackAlignment = 1ULL << ((A >> 26)-1);
315 }
316 }
317
Bill Wendling432e6062012-10-14 07:17:34 +0000318 return *this;
319}
320
Bill Wendling034b94b2012-12-19 07:18:57 +0000321AttrBuilder &AttrBuilder::removeAttributes(const Attribute &A){
Bill Wendling03198882013-01-04 23:27:34 +0000322 uint64_t Mask = A.getBitMask();
323
324 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
325 I = Attribute::AttrKind(I + 1)) {
326 if (Mask & AttributeImpl::getAttrMask(I)) {
327 Attrs.erase(I);
328
329 if (I == Attribute::Alignment)
330 Alignment = 0;
331 else if (I == Attribute::StackAlignment)
332 StackAlignment = 0;
333 }
334 }
335
Bill Wendling5886b7b2012-10-14 06:39:53 +0000336 return *this;
Bill Wendling8831c062012-10-09 00:01:21 +0000337}
338
Bill Wendling22bd6412013-01-03 01:54:39 +0000339bool AttrBuilder::contains(Attribute::AttrKind A) const {
Bill Wendling03198882013-01-04 23:27:34 +0000340 return Attrs.count(A);
Bill Wendling7d2f2492012-10-10 07:36:45 +0000341}
342
Bill Wendling702cc912012-10-15 20:35:56 +0000343bool AttrBuilder::hasAttributes() const {
Bill Wendling03198882013-01-04 23:27:34 +0000344 return !Attrs.empty();
Bill Wendlingf385f4c2012-10-08 23:27:46 +0000345}
Bill Wendling60507d52013-01-04 20:54:35 +0000346
Bill Wendling034b94b2012-12-19 07:18:57 +0000347bool AttrBuilder::hasAttributes(const Attribute &A) const {
Bill Wendling03198882013-01-04 23:27:34 +0000348 return getBitMask() & A.getBitMask();
Bill Wendling8831c062012-10-09 00:01:21 +0000349}
Bill Wendling60507d52013-01-04 20:54:35 +0000350
Bill Wendling702cc912012-10-15 20:35:56 +0000351bool AttrBuilder::hasAlignmentAttr() const {
Bill Wendling03198882013-01-04 23:27:34 +0000352 return Alignment != 0;
Bill Wendlingf385f4c2012-10-08 23:27:46 +0000353}
354
Bill Wendling03198882013-01-04 23:27:34 +0000355uint64_t AttrBuilder::getBitMask() const {
356 uint64_t Mask = 0;
357
358 for (DenseSet<Attribute::AttrKind>::const_iterator I = Attrs.begin(),
359 E = Attrs.end(); I != E; ++I) {
360 Attribute::AttrKind Kind = *I;
361
362 if (Kind == Attribute::Alignment)
363 Mask |= (Log2_32(Alignment) + 1) << 16;
364 else if (Kind == Attribute::StackAlignment)
365 Mask |= (Log2_32(StackAlignment) + 1) << 26;
366 else
367 Mask |= AttributeImpl::getAttrMask(Kind);
368 }
369
370 return Mask;
Bill Wendlingf385f4c2012-10-08 23:27:46 +0000371}
372
Bill Wendling03198882013-01-04 23:27:34 +0000373bool AttrBuilder::operator==(const AttrBuilder &B) {
374 SmallVector<Attribute::AttrKind, 8> This(Attrs.begin(), Attrs.end());
375 SmallVector<Attribute::AttrKind, 8> That(B.Attrs.begin(), B.Attrs.end());
376 return This == That;
Bill Wendling7d2f2492012-10-10 07:36:45 +0000377}
378
Chris Lattner58d74912008-03-12 17:45:29 +0000379//===----------------------------------------------------------------------===//
Bill Wendling2c79ecb2012-09-26 21:07:29 +0000380// AttributeImpl Definition
381//===----------------------------------------------------------------------===//
382
Bill Wendling1bbd6442013-01-05 01:36:54 +0000383AttributeImpl::AttributeImpl(LLVMContext &C, uint64_t data)
384 : Context(C) {
Bill Wendling7c1683d2012-12-29 12:29:38 +0000385 Data = ConstantInt::get(Type::getInt64Ty(C), data);
386}
Bill Wendling1bbd6442013-01-05 01:36:54 +0000387AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind data)
388 : Context(C) {
Bill Wendling979aff62012-12-30 02:22:16 +0000389 Data = ConstantInt::get(Type::getInt64Ty(C), data);
390}
391AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind data,
Bill Wendling1bbd6442013-01-05 01:36:54 +0000392 ArrayRef<Constant*> values)
393 : Context(C) {
Bill Wendling979aff62012-12-30 02:22:16 +0000394 Data = ConstantInt::get(Type::getInt64Ty(C), data);
395 Vals.reserve(values.size());
396 Vals.append(values.begin(), values.end());
397}
Bill Wendling1bbd6442013-01-05 01:36:54 +0000398AttributeImpl::AttributeImpl(LLVMContext &C, StringRef data)
399 : Context(C) {
Bill Wendling979aff62012-12-30 02:22:16 +0000400 Data = ConstantDataArray::getString(C, data);
401}
Bill Wendling7c1683d2012-12-29 12:29:38 +0000402
Bill Wendling60507d52013-01-04 20:54:35 +0000403bool AttributeImpl::operator==(Attribute::AttrKind Kind) const {
Bill Wendling529ec712012-12-30 01:38:39 +0000404 if (ConstantInt *CI = dyn_cast<ConstantInt>(Data))
405 return CI->getZExtValue() == Kind;
406 return false;
407}
Bill Wendling60507d52013-01-04 20:54:35 +0000408bool AttributeImpl::operator!=(Attribute::AttrKind Kind) const {
409 return !(*this == Kind);
410}
Bill Wendling529ec712012-12-30 01:38:39 +0000411
Bill Wendling60507d52013-01-04 20:54:35 +0000412bool AttributeImpl::operator==(StringRef Kind) const {
Bill Wendling529ec712012-12-30 01:38:39 +0000413 if (ConstantDataArray *CDA = dyn_cast<ConstantDataArray>(Data))
414 if (CDA->isString())
415 return CDA->getAsString() == Kind;
416 return false;
417}
Bill Wendling60507d52013-01-04 20:54:35 +0000418bool AttributeImpl::operator!=(StringRef Kind) const {
419 return !(*this == Kind);
420}
Bill Wendling529ec712012-12-30 01:38:39 +0000421
Bill Wendlingc966e082012-12-30 01:05:42 +0000422uint64_t AttributeImpl::getBitMask() const {
Bill Wendling529ec712012-12-30 01:38:39 +0000423 // FIXME: Remove this.
Bill Wendling7c1683d2012-12-29 12:29:38 +0000424 return cast<ConstantInt>(Data)->getZExtValue();
425}
426
Bill Wendling60507d52013-01-04 20:54:35 +0000427uint64_t AttributeImpl::getAttrMask(Attribute::AttrKind Val) {
Bill Wendling67658342012-10-09 07:45:08 +0000428 switch (Val) {
Chandler Carruth6f78fbb2013-01-05 08:47:26 +0000429 case Attribute::EndAttrKinds:
430 case Attribute::AttrKindEmptyKey:
431 case Attribute::AttrKindTombstoneKey:
432 llvm_unreachable("Synthetic enumerators which should never get here");
433
Bill Wendling034b94b2012-12-19 07:18:57 +0000434 case Attribute::None: return 0;
435 case Attribute::ZExt: return 1 << 0;
436 case Attribute::SExt: return 1 << 1;
437 case Attribute::NoReturn: return 1 << 2;
438 case Attribute::InReg: return 1 << 3;
439 case Attribute::StructRet: return 1 << 4;
440 case Attribute::NoUnwind: return 1 << 5;
441 case Attribute::NoAlias: return 1 << 6;
442 case Attribute::ByVal: return 1 << 7;
443 case Attribute::Nest: return 1 << 8;
444 case Attribute::ReadNone: return 1 << 9;
445 case Attribute::ReadOnly: return 1 << 10;
446 case Attribute::NoInline: return 1 << 11;
447 case Attribute::AlwaysInline: return 1 << 12;
448 case Attribute::OptimizeForSize: return 1 << 13;
449 case Attribute::StackProtect: return 1 << 14;
450 case Attribute::StackProtectReq: return 1 << 15;
451 case Attribute::Alignment: return 31 << 16;
452 case Attribute::NoCapture: return 1 << 21;
453 case Attribute::NoRedZone: return 1 << 22;
454 case Attribute::NoImplicitFloat: return 1 << 23;
455 case Attribute::Naked: return 1 << 24;
456 case Attribute::InlineHint: return 1 << 25;
457 case Attribute::StackAlignment: return 7 << 26;
458 case Attribute::ReturnsTwice: return 1 << 29;
459 case Attribute::UWTable: return 1 << 30;
460 case Attribute::NonLazyBind: return 1U << 31;
461 case Attribute::AddressSafety: return 1ULL << 32;
462 case Attribute::MinSize: return 1ULL << 33;
James Molloy67ae1352012-12-20 16:04:27 +0000463 case Attribute::NoDuplicate: return 1ULL << 34;
Bill Wendling67658342012-10-09 07:45:08 +0000464 }
465 llvm_unreachable("Unsupported attribute type");
466}
467
Bill Wendling60507d52013-01-04 20:54:35 +0000468bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
Bill Wendlingc966e082012-12-30 01:05:42 +0000469 return (getBitMask() & getAttrMask(A)) != 0;
Bill Wendling8e635db2012-10-08 21:47:17 +0000470}
Bill Wendling2c79ecb2012-09-26 21:07:29 +0000471
Bill Wendlingf6670722012-12-20 01:36:59 +0000472bool AttributeImpl::hasAttributes() const {
Bill Wendlingc966e082012-12-30 01:05:42 +0000473 return getBitMask() != 0;
Bill Wendling8e635db2012-10-08 21:47:17 +0000474}
Bill Wendling2c79ecb2012-09-26 21:07:29 +0000475
Bill Wendlingf6670722012-12-20 01:36:59 +0000476uint64_t AttributeImpl::getAlignment() const {
Bill Wendlingc966e082012-12-30 01:05:42 +0000477 return getBitMask() & getAttrMask(Attribute::Alignment);
Bill Wendling8e635db2012-10-08 21:47:17 +0000478}
Bill Wendling2c79ecb2012-09-26 21:07:29 +0000479
Bill Wendling1bbd6442013-01-05 01:36:54 +0000480void AttributeImpl::setAlignment(unsigned Align) {
481 Vals.push_back(ConstantInt::get(Type::getInt64Ty(Context), Align));
482}
483
Bill Wendlingf6670722012-12-20 01:36:59 +0000484uint64_t AttributeImpl::getStackAlignment() const {
Bill Wendlingc966e082012-12-30 01:05:42 +0000485 return getBitMask() & getAttrMask(Attribute::StackAlignment);
Bill Wendling8e635db2012-10-08 21:47:17 +0000486}
Bill Wendling2c79ecb2012-09-26 21:07:29 +0000487
Bill Wendling1bbd6442013-01-05 01:36:54 +0000488void AttributeImpl::setStackAlignment(unsigned Align) {
489 Vals.push_back(ConstantInt::get(Type::getInt64Ty(Context), Align));
490}
491
Bill Wendling2c79ecb2012-09-26 21:07:29 +0000492//===----------------------------------------------------------------------===//
Bill Wendling18e72112012-12-19 22:42:22 +0000493// AttributeSetImpl Definition
Chris Lattner58d74912008-03-12 17:45:29 +0000494//===----------------------------------------------------------------------===//
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000495
Bill Wendling99faa3b2012-12-07 23:16:57 +0000496AttributeSet AttributeSet::get(LLVMContext &C,
Bill Wendling1ce47ac2012-12-12 19:21:53 +0000497 ArrayRef<AttributeWithIndex> Attrs) {
Devang Patel05988662008-09-25 21:00:45 +0000498 // If there are no attributes then return a null AttributesList pointer.
Chris Lattnerd509d0b2012-05-28 01:47:44 +0000499 if (Attrs.empty())
Bill Wendling99faa3b2012-12-07 23:16:57 +0000500 return AttributeSet();
Bill Wendling77898682012-10-16 06:01:44 +0000501
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000502#ifndef NDEBUG
Chris Lattnerd509d0b2012-05-28 01:47:44 +0000503 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
Bill Wendling77898682012-10-16 06:01:44 +0000504 assert(Attrs[i].Attrs.hasAttributes() &&
Devang Patel05988662008-09-25 21:00:45 +0000505 "Pointless attribute!");
Chris Lattner58d74912008-03-12 17:45:29 +0000506 assert((!i || Attrs[i-1].Index < Attrs[i].Index) &&
Devang Patel05988662008-09-25 21:00:45 +0000507 "Misordered AttributesList!");
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000508 }
509#endif
Bill Wendling77898682012-10-16 06:01:44 +0000510
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000511 // Otherwise, build a key to look up the existing attributes.
Bill Wendling0976e002012-11-20 05:09:20 +0000512 LLVMContextImpl *pImpl = C.pImpl;
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000513 FoldingSetNodeID ID;
Bill Wendling18e72112012-12-19 22:42:22 +0000514 AttributeSetImpl::Profile(ID, Attrs);
Bill Wendling77898682012-10-16 06:01:44 +0000515
Bill Wendling0976e002012-11-20 05:09:20 +0000516 void *InsertPoint;
Bill Wendling1bbd6442013-01-05 01:36:54 +0000517 AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
Bill Wendling77898682012-10-16 06:01:44 +0000518
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000519 // If we didn't find any existing attributes of the same shape then
520 // create a new one and insert it.
Bill Wendling0976e002012-11-20 05:09:20 +0000521 if (!PA) {
Bill Wendling5f93e2b2012-12-19 23:55:43 +0000522 PA = new AttributeSetImpl(C, Attrs);
Bill Wendling0976e002012-11-20 05:09:20 +0000523 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000524 }
Bill Wendling77898682012-10-16 06:01:44 +0000525
Devang Patel05988662008-09-25 21:00:45 +0000526 // Return the AttributesList that we found or created.
Bill Wendling99faa3b2012-12-07 23:16:57 +0000527 return AttributeSet(PA);
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000528}
529
Bill Wendling1bbd6442013-01-05 01:36:54 +0000530AttributeSet AttributeSet::get(LLVMContext &C, unsigned Idx, AttrBuilder &B) {
531 SmallVector<AttributeWithIndex, 8> Attrs;
532 for (AttrBuilder::iterator I = B.begin(), E = B.end(); I != E; ++I) {
533 Attribute::AttrKind Kind = *I;
534 Attribute A = Attribute::get(C, Kind);
535
536 if (Kind == Attribute::Alignment)
537 A.setAlignment(B.getAlignment());
538 else if (Kind == Attribute::StackAlignment)
539 A.setStackAlignment(B.getStackAlignment());
540
541 Attrs.push_back(AttributeWithIndex::get(Idx, A));
542 }
543
544 return get(C, Attrs);
545}
546
Chris Lattner58d74912008-03-12 17:45:29 +0000547//===----------------------------------------------------------------------===//
Bill Wendling99faa3b2012-12-07 23:16:57 +0000548// AttributeSet Method Implementations
Chris Lattner58d74912008-03-12 17:45:29 +0000549//===----------------------------------------------------------------------===//
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000550
Bill Wendling99faa3b2012-12-07 23:16:57 +0000551const AttributeSet &AttributeSet::operator=(const AttributeSet &RHS) {
Bill Wendling0976e002012-11-20 05:09:20 +0000552 AttrList = RHS.AttrList;
553 return *this;
Chris Lattner58d74912008-03-12 17:45:29 +0000554}
555
Bill Wendling77898682012-10-16 06:01:44 +0000556/// getNumSlots - Return the number of slots used in this attribute list.
Chris Lattner58d74912008-03-12 17:45:29 +0000557/// This is the number of arguments that have an attribute set on them
558/// (including the function itself).
Bill Wendling99faa3b2012-12-07 23:16:57 +0000559unsigned AttributeSet::getNumSlots() const {
Bill Wendling60507d52013-01-04 20:54:35 +0000560 return AttrList ? AttrList->getNumAttributes() : 0;
Chris Lattner58d74912008-03-12 17:45:29 +0000561}
562
Devang Patel05988662008-09-25 21:00:45 +0000563/// getSlot - Return the AttributeWithIndex at the specified slot. This
564/// holds a number plus a set of attributes.
Bill Wendling99faa3b2012-12-07 23:16:57 +0000565const AttributeWithIndex &AttributeSet::getSlot(unsigned Slot) const {
Bill Wendling60507d52013-01-04 20:54:35 +0000566 assert(AttrList && Slot < AttrList->getNumAttributes() &&
567 "Slot # out of range!");
568 return AttrList->getAttributes()[Slot];
Chris Lattner58d74912008-03-12 17:45:29 +0000569}
570
Bill Wendling831737d2012-12-30 10:32:01 +0000571bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{
572 return getAttributes(Index).hasAttribute(Kind);
573}
574
575bool AttributeSet::hasAttributes(unsigned Index) const {
576 return getAttributes(Index).hasAttributes();
577}
578
579std::string AttributeSet::getAsString(unsigned Index) const {
580 return getAttributes(Index).getAsString();
581}
582
583unsigned AttributeSet::getStackAlignment(unsigned Index) const {
584 return getAttributes(Index).getStackAlignment();
585}
586
587uint64_t AttributeSet::getBitMask(unsigned Index) const {
588 // FIXME: Remove this.
589 return getAttributes(Index).getBitMask();
590}
591
Bill Wendling77898682012-10-16 06:01:44 +0000592/// getAttributes - The attributes for the specified index are returned.
Bill Wendling92e287f2012-12-31 11:51:54 +0000593/// Attributes for the result are denoted with Idx = 0. Function attributes are
594/// denoted with Idx = ~0.
Bill Wendling034b94b2012-12-19 07:18:57 +0000595Attribute AttributeSet::getAttributes(unsigned Idx) const {
596 if (AttrList == 0) return Attribute();
Bill Wendling77898682012-10-16 06:01:44 +0000597
Bill Wendling60507d52013-01-04 20:54:35 +0000598 ArrayRef<AttributeWithIndex> Attrs = AttrList->getAttributes();
Chris Lattner58d74912008-03-12 17:45:29 +0000599 for (unsigned i = 0, e = Attrs.size(); i != e && Attrs[i].Index <= Idx; ++i)
600 if (Attrs[i].Index == Idx)
601 return Attrs[i].Attrs;
Bill Wendlingef99fe82012-09-21 15:26:31 +0000602
Bill Wendling034b94b2012-12-19 07:18:57 +0000603 return Attribute();
Chris Lattner58d74912008-03-12 17:45:29 +0000604}
605
606/// hasAttrSomewhere - Return true if the specified attribute is set for at
607/// least one parameter or for the return value.
Bill Wendling629fb822012-12-22 00:37:52 +0000608bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr) const {
Devang Patel05988662008-09-25 21:00:45 +0000609 if (AttrList == 0) return false;
Bill Wendling7d2f2492012-10-10 07:36:45 +0000610
Bill Wendling60507d52013-01-04 20:54:35 +0000611 ArrayRef<AttributeWithIndex> Attrs = AttrList->getAttributes();
Chris Lattner58d74912008-03-12 17:45:29 +0000612 for (unsigned i = 0, e = Attrs.size(); i != e; ++i)
Bill Wendling7d2f2492012-10-10 07:36:45 +0000613 if (Attrs[i].Attrs.hasAttribute(Attr))
Chris Lattner58d74912008-03-12 17:45:29 +0000614 return true;
Bill Wendling0976e002012-11-20 05:09:20 +0000615
Chris Lattner58d74912008-03-12 17:45:29 +0000616 return false;
617}
618
Bill Wendling99faa3b2012-12-07 23:16:57 +0000619AttributeSet AttributeSet::addAttr(LLVMContext &C, unsigned Idx,
Bill Wendling9d30e722012-12-31 00:49:59 +0000620 Attribute Attrs) const {
Bill Wendling034b94b2012-12-19 07:18:57 +0000621 Attribute OldAttrs = getAttributes(Idx);
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000622#ifndef NDEBUG
623 // FIXME it is not obvious how this should work for alignment.
624 // For now, say we can't change a known alignment.
Bill Wendlingef99fe82012-09-21 15:26:31 +0000625 unsigned OldAlign = OldAttrs.getAlignment();
626 unsigned NewAlign = Attrs.getAlignment();
Anton Korobeynikov4aefd6b2008-02-20 12:07:57 +0000627 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000628 "Attempt to change alignment!");
629#endif
Bill Wendling77898682012-10-16 06:01:44 +0000630
Bill Wendling702cc912012-10-15 20:35:56 +0000631 AttrBuilder NewAttrs =
632 AttrBuilder(OldAttrs).addAttributes(Attrs);
633 if (NewAttrs == AttrBuilder(OldAttrs))
Chris Lattner58d74912008-03-12 17:45:29 +0000634 return *this;
Bill Wendling77898682012-10-16 06:01:44 +0000635
Devang Patel05988662008-09-25 21:00:45 +0000636 SmallVector<AttributeWithIndex, 8> NewAttrList;
637 if (AttrList == 0)
638 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Chris Lattner58d74912008-03-12 17:45:29 +0000639 else {
Bill Wendling60507d52013-01-04 20:54:35 +0000640 ArrayRef<AttributeWithIndex> OldAttrList = AttrList->getAttributes();
Chris Lattner58d74912008-03-12 17:45:29 +0000641 unsigned i = 0, e = OldAttrList.size();
642 // Copy attributes for arguments before this one.
643 for (; i != e && OldAttrList[i].Index < Idx; ++i)
644 NewAttrList.push_back(OldAttrList[i]);
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000645
Chris Lattner58d74912008-03-12 17:45:29 +0000646 // If there are attributes already at this index, merge them in.
647 if (i != e && OldAttrList[i].Index == Idx) {
Bill Wendlingc4167952012-10-14 07:35:59 +0000648 Attrs =
Bill Wendling034b94b2012-12-19 07:18:57 +0000649 Attribute::get(C, AttrBuilder(Attrs).
Bill Wendlingc4167952012-10-14 07:35:59 +0000650 addAttributes(OldAttrList[i].Attrs));
Chris Lattner58d74912008-03-12 17:45:29 +0000651 ++i;
652 }
Bill Wendling77898682012-10-16 06:01:44 +0000653
Devang Patel05988662008-09-25 21:00:45 +0000654 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Bill Wendling77898682012-10-16 06:01:44 +0000655
Chris Lattner58d74912008-03-12 17:45:29 +0000656 // Copy attributes for arguments after this one.
Bill Wendling77898682012-10-16 06:01:44 +0000657 NewAttrList.insert(NewAttrList.end(),
Chris Lattner58d74912008-03-12 17:45:29 +0000658 OldAttrList.begin()+i, OldAttrList.end());
659 }
Bill Wendling77898682012-10-16 06:01:44 +0000660
Bill Wendling0976e002012-11-20 05:09:20 +0000661 return get(C, NewAttrList);
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000662}
663
Bill Wendling99faa3b2012-12-07 23:16:57 +0000664AttributeSet AttributeSet::removeAttr(LLVMContext &C, unsigned Idx,
Bill Wendling9d30e722012-12-31 00:49:59 +0000665 Attribute Attrs) const {
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000666#ifndef NDEBUG
667 // FIXME it is not obvious how this should work for alignment.
668 // For now, say we can't pass in alignment, which no current use does.
Bill Wendling034b94b2012-12-19 07:18:57 +0000669 assert(!Attrs.hasAttribute(Attribute::Alignment) &&
Bill Wendling67658342012-10-09 07:45:08 +0000670 "Attempt to exclude alignment!");
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000671#endif
Bill Wendling99faa3b2012-12-07 23:16:57 +0000672 if (AttrList == 0) return AttributeSet();
Bill Wendling77898682012-10-16 06:01:44 +0000673
Bill Wendling034b94b2012-12-19 07:18:57 +0000674 Attribute OldAttrs = getAttributes(Idx);
Bill Wendling702cc912012-10-15 20:35:56 +0000675 AttrBuilder NewAttrs =
676 AttrBuilder(OldAttrs).removeAttributes(Attrs);
677 if (NewAttrs == AttrBuilder(OldAttrs))
Chris Lattner58d74912008-03-12 17:45:29 +0000678 return *this;
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000679
Devang Patel05988662008-09-25 21:00:45 +0000680 SmallVector<AttributeWithIndex, 8> NewAttrList;
Bill Wendling60507d52013-01-04 20:54:35 +0000681 ArrayRef<AttributeWithIndex> OldAttrList = AttrList->getAttributes();
Chris Lattner58d74912008-03-12 17:45:29 +0000682 unsigned i = 0, e = OldAttrList.size();
Bill Wendling77898682012-10-16 06:01:44 +0000683
Chris Lattner58d74912008-03-12 17:45:29 +0000684 // Copy attributes for arguments before this one.
685 for (; i != e && OldAttrList[i].Index < Idx; ++i)
686 NewAttrList.push_back(OldAttrList[i]);
Bill Wendling77898682012-10-16 06:01:44 +0000687
Chris Lattner58d74912008-03-12 17:45:29 +0000688 // If there are attributes already at this index, merge them in.
689 assert(OldAttrList[i].Index == Idx && "Attribute isn't set?");
Bill Wendling034b94b2012-12-19 07:18:57 +0000690 Attrs = Attribute::get(C, AttrBuilder(OldAttrList[i].Attrs).
Bill Wendling5886b7b2012-10-14 06:39:53 +0000691 removeAttributes(Attrs));
Chris Lattner58d74912008-03-12 17:45:29 +0000692 ++i;
Bill Wendling7be78482012-10-14 08:54:26 +0000693 if (Attrs.hasAttributes()) // If any attributes left for this param, add them.
Devang Patel05988662008-09-25 21:00:45 +0000694 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Bill Wendling77898682012-10-16 06:01:44 +0000695
Chris Lattner58d74912008-03-12 17:45:29 +0000696 // Copy attributes for arguments after this one.
Bill Wendling77898682012-10-16 06:01:44 +0000697 NewAttrList.insert(NewAttrList.end(),
Chris Lattner58d74912008-03-12 17:45:29 +0000698 OldAttrList.begin()+i, OldAttrList.end());
Bill Wendling77898682012-10-16 06:01:44 +0000699
Bill Wendling0976e002012-11-20 05:09:20 +0000700 return get(C, NewAttrList);
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000701}
702
Bill Wendling99faa3b2012-12-07 23:16:57 +0000703void AttributeSet::dump() const {
David Greeneef1894e2010-01-05 01:29:58 +0000704 dbgs() << "PAL[ ";
Chris Lattner58d74912008-03-12 17:45:29 +0000705 for (unsigned i = 0; i < getNumSlots(); ++i) {
Devang Patel05988662008-09-25 21:00:45 +0000706 const AttributeWithIndex &PAWI = getSlot(i);
Bill Wendling60507d52013-01-04 20:54:35 +0000707 dbgs() << "{ " << PAWI.Index << ", " << PAWI.Attrs.getAsString() << " } ";
Chris Lattner58d74912008-03-12 17:45:29 +0000708 }
Bill Wendling77898682012-10-16 06:01:44 +0000709
David Greeneef1894e2010-01-05 01:29:58 +0000710 dbgs() << "]\n";
Duncan Sandsad9a9e12008-01-06 18:27:01 +0000711}