blob: 5024a63c3948c051d3a277db0a794341f57d6b98 [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 Wendling1db9b692013-01-09 23:36:50 +000048 ID.AddInteger(B.Raw());
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 Wendling1db9b692013-01-09 23:36:50 +000056 PA = new AttributeImpl(Context, B.Raw());
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 Wendling1db9b692013-01-09 23:36:50 +0000106uint64_t Attribute::Raw() const {
107 return pImpl ? pImpl->Raw() : 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 Wendling1db9b692013-01-09 23:36:50 +0000142 uint64_t EncodedAttrs = Attrs.Raw() & 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 Wendling1db9b692013-01-09 23:36:50 +0000145 EncodedAttrs |= (Attrs.Raw() & (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 Wendlinga90a99a2013-01-07 08:24:35 +0000243AttrBuilder::AttrBuilder(AttributeSet AS, unsigned Idx)
244 : Alignment(0), StackAlignment(0) {
245 AttributeSetImpl *pImpl = AS.AttrList;
246 if (!pImpl) return;
247
248 ArrayRef<AttributeWithIndex> AttrList = pImpl->getAttributes();
249 const AttributeWithIndex *AWI = 0;
250 for (unsigned I = 0, E = AttrList.size(); I != E; ++I)
251 if (AttrList[I].Index == Idx) {
252 AWI = &AttrList[I];
253 break;
254 }
255
256 assert(AWI && "Cannot find index in attribute set!");
257
258 /// FIXME: This will be modified in the future. Basically, the
259 /// AttributeWithIndex class will contain the
260
261}
262
Bill Wendling03198882013-01-04 23:27:34 +0000263void AttrBuilder::clear() {
264 Attrs.clear();
265 Alignment = StackAlignment = 0;
266}
267
268AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
269 Attrs.insert(Val);
Bill Wendling3a106e62012-10-09 19:01:18 +0000270 return *this;
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000271}
272
Bill Wendling03198882013-01-04 23:27:34 +0000273AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
274 Attrs.erase(Val);
275 if (Val == Attribute::Alignment)
276 Alignment = 0;
277 else if (Val == Attribute::StackAlignment)
278 StackAlignment = 0;
279
Bill Wendlinga19a5302012-10-14 04:10:01 +0000280 return *this;
281}
282
Bill Wendling702cc912012-10-15 20:35:56 +0000283AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
Bill Wendlingda3f9d82012-10-14 03:58:29 +0000284 if (Align == 0) return *this;
Bill Wendling03198882013-01-04 23:27:34 +0000285
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000286 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
287 assert(Align <= 0x40000000 && "Alignment too large.");
Bill Wendling03198882013-01-04 23:27:34 +0000288
289 Attrs.insert(Attribute::Alignment);
290 Alignment = Align;
Bill Wendlingda3f9d82012-10-14 03:58:29 +0000291 return *this;
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000292}
293
Bill Wendling03198882013-01-04 23:27:34 +0000294AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
295 // Default alignment, allow the target to define how to align it.
296 if (Align == 0) return *this;
297
298 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
299 assert(Align <= 0x100 && "Alignment too large.");
300
301 Attrs.insert(Attribute::StackAlignment);
302 StackAlignment = Align;
303 return *this;
304}
305
306AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) {
307 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
308 I = Attribute::AttrKind(I + 1)) {
309 if (uint64_t A = (Val & AttributeImpl::getAttrMask(I))) {
310 Attrs.insert(I);
311
312 if (I == Attribute::Alignment)
313 Alignment = 1ULL << ((A >> 16) - 1);
314 else if (I == Attribute::StackAlignment)
315 StackAlignment = 1ULL << ((A >> 26)-1);
316 }
317 }
318
Bill Wendling3a106e62012-10-09 19:01:18 +0000319 return *this;
Bill Wendling2e879bc2012-10-09 09:11:20 +0000320}
321
Bill Wendling034b94b2012-12-19 07:18:57 +0000322AttrBuilder &AttrBuilder::addAttributes(const Attribute &A) {
Bill Wendling1db9b692013-01-09 23:36:50 +0000323 uint64_t Mask = A.Raw();
Bill Wendling03198882013-01-04 23:27:34 +0000324
325 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
326 I = Attribute::AttrKind(I + 1)) {
327 if (uint64_t A = (Mask & AttributeImpl::getAttrMask(I))) {
328 Attrs.insert(I);
329
330 if (I == Attribute::Alignment)
331 Alignment = 1ULL << ((A >> 16) - 1);
332 else if (I == Attribute::StackAlignment)
333 StackAlignment = 1ULL << ((A >> 26)-1);
334 }
335 }
336
Bill Wendling432e6062012-10-14 07:17:34 +0000337 return *this;
338}
339
Bill Wendling034b94b2012-12-19 07:18:57 +0000340AttrBuilder &AttrBuilder::removeAttributes(const Attribute &A){
Bill Wendling1db9b692013-01-09 23:36:50 +0000341 uint64_t Mask = A.Raw();
Bill Wendling03198882013-01-04 23:27:34 +0000342
343 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
344 I = Attribute::AttrKind(I + 1)) {
345 if (Mask & AttributeImpl::getAttrMask(I)) {
346 Attrs.erase(I);
347
348 if (I == Attribute::Alignment)
349 Alignment = 0;
350 else if (I == Attribute::StackAlignment)
351 StackAlignment = 0;
352 }
353 }
354
Bill Wendling5886b7b2012-10-14 06:39:53 +0000355 return *this;
Bill Wendling8831c062012-10-09 00:01:21 +0000356}
357
Bill Wendling22bd6412013-01-03 01:54:39 +0000358bool AttrBuilder::contains(Attribute::AttrKind A) const {
Bill Wendling03198882013-01-04 23:27:34 +0000359 return Attrs.count(A);
Bill Wendling7d2f2492012-10-10 07:36:45 +0000360}
361
Bill Wendling702cc912012-10-15 20:35:56 +0000362bool AttrBuilder::hasAttributes() const {
Bill Wendling03198882013-01-04 23:27:34 +0000363 return !Attrs.empty();
Bill Wendlingf385f4c2012-10-08 23:27:46 +0000364}
Bill Wendling60507d52013-01-04 20:54:35 +0000365
Bill Wendling034b94b2012-12-19 07:18:57 +0000366bool AttrBuilder::hasAttributes(const Attribute &A) const {
Bill Wendling1db9b692013-01-09 23:36:50 +0000367 return Raw() & A.Raw();
Bill Wendling8831c062012-10-09 00:01:21 +0000368}
Bill Wendling60507d52013-01-04 20:54:35 +0000369
Bill Wendling702cc912012-10-15 20:35:56 +0000370bool AttrBuilder::hasAlignmentAttr() const {
Bill Wendling03198882013-01-04 23:27:34 +0000371 return Alignment != 0;
Bill Wendlingf385f4c2012-10-08 23:27:46 +0000372}
373
Bill Wendling1db9b692013-01-09 23:36:50 +0000374uint64_t AttrBuilder::Raw() const {
Bill Wendling03198882013-01-04 23:27:34 +0000375 uint64_t Mask = 0;
376
377 for (DenseSet<Attribute::AttrKind>::const_iterator I = Attrs.begin(),
378 E = Attrs.end(); I != E; ++I) {
379 Attribute::AttrKind Kind = *I;
380
381 if (Kind == Attribute::Alignment)
382 Mask |= (Log2_32(Alignment) + 1) << 16;
383 else if (Kind == Attribute::StackAlignment)
384 Mask |= (Log2_32(StackAlignment) + 1) << 26;
385 else
386 Mask |= AttributeImpl::getAttrMask(Kind);
387 }
388
389 return Mask;
Bill Wendlingf385f4c2012-10-08 23:27:46 +0000390}
391
Bill Wendling03198882013-01-04 23:27:34 +0000392bool AttrBuilder::operator==(const AttrBuilder &B) {
393 SmallVector<Attribute::AttrKind, 8> This(Attrs.begin(), Attrs.end());
394 SmallVector<Attribute::AttrKind, 8> That(B.Attrs.begin(), B.Attrs.end());
395 return This == That;
Bill Wendling7d2f2492012-10-10 07:36:45 +0000396}
397
Chris Lattner58d74912008-03-12 17:45:29 +0000398//===----------------------------------------------------------------------===//
Bill Wendling2c79ecb2012-09-26 21:07:29 +0000399// AttributeImpl Definition
400//===----------------------------------------------------------------------===//
401
Bill Wendling1bbd6442013-01-05 01:36:54 +0000402AttributeImpl::AttributeImpl(LLVMContext &C, uint64_t data)
403 : Context(C) {
Bill Wendling7c1683d2012-12-29 12:29:38 +0000404 Data = ConstantInt::get(Type::getInt64Ty(C), data);
405}
Bill Wendling1bbd6442013-01-05 01:36:54 +0000406AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind data)
407 : Context(C) {
Bill Wendling979aff62012-12-30 02:22:16 +0000408 Data = ConstantInt::get(Type::getInt64Ty(C), data);
409}
410AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind data,
Bill Wendling1bbd6442013-01-05 01:36:54 +0000411 ArrayRef<Constant*> values)
412 : Context(C) {
Bill Wendling979aff62012-12-30 02:22:16 +0000413 Data = ConstantInt::get(Type::getInt64Ty(C), data);
414 Vals.reserve(values.size());
415 Vals.append(values.begin(), values.end());
416}
Bill Wendling1bbd6442013-01-05 01:36:54 +0000417AttributeImpl::AttributeImpl(LLVMContext &C, StringRef data)
418 : Context(C) {
Bill Wendling979aff62012-12-30 02:22:16 +0000419 Data = ConstantDataArray::getString(C, data);
420}
Bill Wendling7c1683d2012-12-29 12:29:38 +0000421
Bill Wendling60507d52013-01-04 20:54:35 +0000422bool AttributeImpl::operator==(Attribute::AttrKind Kind) const {
Bill Wendling529ec712012-12-30 01:38:39 +0000423 if (ConstantInt *CI = dyn_cast<ConstantInt>(Data))
424 return CI->getZExtValue() == Kind;
425 return false;
426}
Bill Wendling60507d52013-01-04 20:54:35 +0000427bool AttributeImpl::operator!=(Attribute::AttrKind Kind) const {
428 return !(*this == Kind);
429}
Bill Wendling529ec712012-12-30 01:38:39 +0000430
Bill Wendling60507d52013-01-04 20:54:35 +0000431bool AttributeImpl::operator==(StringRef Kind) const {
Bill Wendling529ec712012-12-30 01:38:39 +0000432 if (ConstantDataArray *CDA = dyn_cast<ConstantDataArray>(Data))
433 if (CDA->isString())
434 return CDA->getAsString() == Kind;
435 return false;
436}
Bill Wendling60507d52013-01-04 20:54:35 +0000437bool AttributeImpl::operator!=(StringRef Kind) const {
438 return !(*this == Kind);
439}
Bill Wendling529ec712012-12-30 01:38:39 +0000440
Bill Wendling1db9b692013-01-09 23:36:50 +0000441uint64_t AttributeImpl::Raw() const {
Bill Wendling529ec712012-12-30 01:38:39 +0000442 // FIXME: Remove this.
Bill Wendling7c1683d2012-12-29 12:29:38 +0000443 return cast<ConstantInt>(Data)->getZExtValue();
444}
445
Bill Wendling60507d52013-01-04 20:54:35 +0000446uint64_t AttributeImpl::getAttrMask(Attribute::AttrKind Val) {
Bill Wendling67658342012-10-09 07:45:08 +0000447 switch (Val) {
Chandler Carruth6f78fbb2013-01-05 08:47:26 +0000448 case Attribute::EndAttrKinds:
449 case Attribute::AttrKindEmptyKey:
450 case Attribute::AttrKindTombstoneKey:
451 llvm_unreachable("Synthetic enumerators which should never get here");
452
Bill Wendling034b94b2012-12-19 07:18:57 +0000453 case Attribute::None: return 0;
454 case Attribute::ZExt: return 1 << 0;
455 case Attribute::SExt: return 1 << 1;
456 case Attribute::NoReturn: return 1 << 2;
457 case Attribute::InReg: return 1 << 3;
458 case Attribute::StructRet: return 1 << 4;
459 case Attribute::NoUnwind: return 1 << 5;
460 case Attribute::NoAlias: return 1 << 6;
461 case Attribute::ByVal: return 1 << 7;
462 case Attribute::Nest: return 1 << 8;
463 case Attribute::ReadNone: return 1 << 9;
464 case Attribute::ReadOnly: return 1 << 10;
465 case Attribute::NoInline: return 1 << 11;
466 case Attribute::AlwaysInline: return 1 << 12;
467 case Attribute::OptimizeForSize: return 1 << 13;
468 case Attribute::StackProtect: return 1 << 14;
469 case Attribute::StackProtectReq: return 1 << 15;
470 case Attribute::Alignment: return 31 << 16;
471 case Attribute::NoCapture: return 1 << 21;
472 case Attribute::NoRedZone: return 1 << 22;
473 case Attribute::NoImplicitFloat: return 1 << 23;
474 case Attribute::Naked: return 1 << 24;
475 case Attribute::InlineHint: return 1 << 25;
476 case Attribute::StackAlignment: return 7 << 26;
477 case Attribute::ReturnsTwice: return 1 << 29;
478 case Attribute::UWTable: return 1 << 30;
479 case Attribute::NonLazyBind: return 1U << 31;
480 case Attribute::AddressSafety: return 1ULL << 32;
481 case Attribute::MinSize: return 1ULL << 33;
James Molloy67ae1352012-12-20 16:04:27 +0000482 case Attribute::NoDuplicate: return 1ULL << 34;
Bill Wendling67658342012-10-09 07:45:08 +0000483 }
484 llvm_unreachable("Unsupported attribute type");
485}
486
Bill Wendling60507d52013-01-04 20:54:35 +0000487bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
Bill Wendling1db9b692013-01-09 23:36:50 +0000488 return (Raw() & getAttrMask(A)) != 0;
Bill Wendling8e635db2012-10-08 21:47:17 +0000489}
Bill Wendling2c79ecb2012-09-26 21:07:29 +0000490
Bill Wendlingf6670722012-12-20 01:36:59 +0000491bool AttributeImpl::hasAttributes() const {
Bill Wendling1db9b692013-01-09 23:36:50 +0000492 return Raw() != 0;
Bill Wendling8e635db2012-10-08 21:47:17 +0000493}
Bill Wendling2c79ecb2012-09-26 21:07:29 +0000494
Bill Wendlingf6670722012-12-20 01:36:59 +0000495uint64_t AttributeImpl::getAlignment() const {
Bill Wendling1db9b692013-01-09 23:36:50 +0000496 return Raw() & getAttrMask(Attribute::Alignment);
Bill Wendling8e635db2012-10-08 21:47:17 +0000497}
Bill Wendling2c79ecb2012-09-26 21:07:29 +0000498
Bill Wendling1bbd6442013-01-05 01:36:54 +0000499void AttributeImpl::setAlignment(unsigned Align) {
500 Vals.push_back(ConstantInt::get(Type::getInt64Ty(Context), Align));
501}
502
Bill Wendlingf6670722012-12-20 01:36:59 +0000503uint64_t AttributeImpl::getStackAlignment() const {
Bill Wendling1db9b692013-01-09 23:36:50 +0000504 return Raw() & getAttrMask(Attribute::StackAlignment);
Bill Wendling8e635db2012-10-08 21:47:17 +0000505}
Bill Wendling2c79ecb2012-09-26 21:07:29 +0000506
Bill Wendling1bbd6442013-01-05 01:36:54 +0000507void AttributeImpl::setStackAlignment(unsigned Align) {
508 Vals.push_back(ConstantInt::get(Type::getInt64Ty(Context), Align));
509}
510
Bill Wendling8456efb2013-01-09 00:32:55 +0000511void AttributeImpl::Profile(FoldingSetNodeID &ID, Constant *Data,
512 ArrayRef<Constant*> Vals) {
Bill Wendlingff887162013-01-09 00:32:08 +0000513 ID.AddInteger(cast<ConstantInt>(Data)->getZExtValue());
Bill Wendling1db9b692013-01-09 23:36:50 +0000514#if 0
515 // FIXME: Not yet supported.
Bill Wendlingff887162013-01-09 00:32:08 +0000516 for (ArrayRef<Constant*>::iterator I = Vals.begin(), E = Vals.end();
517 I != E; ++I)
518 ID.AddPointer(*I);
Bill Wendling1db9b692013-01-09 23:36:50 +0000519#endif
Bill Wendlingff887162013-01-09 00:32:08 +0000520}
521
Bill Wendling2c79ecb2012-09-26 21:07:29 +0000522//===----------------------------------------------------------------------===//
Bill Wendling18e72112012-12-19 22:42:22 +0000523// AttributeSetImpl Definition
Chris Lattner58d74912008-03-12 17:45:29 +0000524//===----------------------------------------------------------------------===//
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000525
Bill Wendling99faa3b2012-12-07 23:16:57 +0000526AttributeSet AttributeSet::get(LLVMContext &C,
Bill Wendling1ce47ac2012-12-12 19:21:53 +0000527 ArrayRef<AttributeWithIndex> Attrs) {
Devang Patel05988662008-09-25 21:00:45 +0000528 // If there are no attributes then return a null AttributesList pointer.
Chris Lattnerd509d0b2012-05-28 01:47:44 +0000529 if (Attrs.empty())
Bill Wendling99faa3b2012-12-07 23:16:57 +0000530 return AttributeSet();
Bill Wendling77898682012-10-16 06:01:44 +0000531
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000532#ifndef NDEBUG
Chris Lattnerd509d0b2012-05-28 01:47:44 +0000533 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
Bill Wendling77898682012-10-16 06:01:44 +0000534 assert(Attrs[i].Attrs.hasAttributes() &&
Devang Patel05988662008-09-25 21:00:45 +0000535 "Pointless attribute!");
Chris Lattner58d74912008-03-12 17:45:29 +0000536 assert((!i || Attrs[i-1].Index < Attrs[i].Index) &&
Devang Patel05988662008-09-25 21:00:45 +0000537 "Misordered AttributesList!");
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000538 }
539#endif
Bill Wendling77898682012-10-16 06:01:44 +0000540
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000541 // Otherwise, build a key to look up the existing attributes.
Bill Wendling0976e002012-11-20 05:09:20 +0000542 LLVMContextImpl *pImpl = C.pImpl;
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000543 FoldingSetNodeID ID;
Bill Wendling18e72112012-12-19 22:42:22 +0000544 AttributeSetImpl::Profile(ID, Attrs);
Bill Wendling77898682012-10-16 06:01:44 +0000545
Bill Wendling0976e002012-11-20 05:09:20 +0000546 void *InsertPoint;
Bill Wendling1bbd6442013-01-05 01:36:54 +0000547 AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
Bill Wendling77898682012-10-16 06:01:44 +0000548
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000549 // If we didn't find any existing attributes of the same shape then
550 // create a new one and insert it.
Bill Wendling0976e002012-11-20 05:09:20 +0000551 if (!PA) {
Bill Wendling5f93e2b2012-12-19 23:55:43 +0000552 PA = new AttributeSetImpl(C, Attrs);
Bill Wendling0976e002012-11-20 05:09:20 +0000553 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000554 }
Bill Wendling77898682012-10-16 06:01:44 +0000555
Devang Patel05988662008-09-25 21:00:45 +0000556 // Return the AttributesList that we found or created.
Bill Wendling99faa3b2012-12-07 23:16:57 +0000557 return AttributeSet(PA);
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000558}
559
Bill Wendling1bbd6442013-01-05 01:36:54 +0000560AttributeSet AttributeSet::get(LLVMContext &C, unsigned Idx, AttrBuilder &B) {
561 SmallVector<AttributeWithIndex, 8> Attrs;
562 for (AttrBuilder::iterator I = B.begin(), E = B.end(); I != E; ++I) {
563 Attribute::AttrKind Kind = *I;
564 Attribute A = Attribute::get(C, Kind);
565
566 if (Kind == Attribute::Alignment)
567 A.setAlignment(B.getAlignment());
568 else if (Kind == Attribute::StackAlignment)
569 A.setStackAlignment(B.getStackAlignment());
570
571 Attrs.push_back(AttributeWithIndex::get(Idx, A));
572 }
573
574 return get(C, Attrs);
575}
576
Chris Lattner58d74912008-03-12 17:45:29 +0000577//===----------------------------------------------------------------------===//
Bill Wendling99faa3b2012-12-07 23:16:57 +0000578// AttributeSet Method Implementations
Chris Lattner58d74912008-03-12 17:45:29 +0000579//===----------------------------------------------------------------------===//
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000580
Bill Wendling99faa3b2012-12-07 23:16:57 +0000581const AttributeSet &AttributeSet::operator=(const AttributeSet &RHS) {
Bill Wendling0976e002012-11-20 05:09:20 +0000582 AttrList = RHS.AttrList;
583 return *this;
Chris Lattner58d74912008-03-12 17:45:29 +0000584}
585
Bill Wendling77898682012-10-16 06:01:44 +0000586/// getNumSlots - Return the number of slots used in this attribute list.
Chris Lattner58d74912008-03-12 17:45:29 +0000587/// This is the number of arguments that have an attribute set on them
588/// (including the function itself).
Bill Wendling99faa3b2012-12-07 23:16:57 +0000589unsigned AttributeSet::getNumSlots() const {
Bill Wendling60507d52013-01-04 20:54:35 +0000590 return AttrList ? AttrList->getNumAttributes() : 0;
Chris Lattner58d74912008-03-12 17:45:29 +0000591}
592
Devang Patel05988662008-09-25 21:00:45 +0000593/// getSlot - Return the AttributeWithIndex at the specified slot. This
594/// holds a number plus a set of attributes.
Bill Wendling99faa3b2012-12-07 23:16:57 +0000595const AttributeWithIndex &AttributeSet::getSlot(unsigned Slot) const {
Bill Wendling60507d52013-01-04 20:54:35 +0000596 assert(AttrList && Slot < AttrList->getNumAttributes() &&
597 "Slot # out of range!");
598 return AttrList->getAttributes()[Slot];
Chris Lattner58d74912008-03-12 17:45:29 +0000599}
600
Bill Wendling831737d2012-12-30 10:32:01 +0000601bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{
602 return getAttributes(Index).hasAttribute(Kind);
603}
604
605bool AttributeSet::hasAttributes(unsigned Index) const {
606 return getAttributes(Index).hasAttributes();
607}
608
609std::string AttributeSet::getAsString(unsigned Index) const {
610 return getAttributes(Index).getAsString();
611}
612
613unsigned AttributeSet::getStackAlignment(unsigned Index) const {
614 return getAttributes(Index).getStackAlignment();
615}
616
Bill Wendling1db9b692013-01-09 23:36:50 +0000617uint64_t AttributeSet::Raw(unsigned Index) const {
Bill Wendling831737d2012-12-30 10:32:01 +0000618 // FIXME: Remove this.
Bill Wendling1db9b692013-01-09 23:36:50 +0000619 return getAttributes(Index).Raw();
Bill Wendling831737d2012-12-30 10:32:01 +0000620}
621
Bill Wendling77898682012-10-16 06:01:44 +0000622/// getAttributes - The attributes for the specified index are returned.
Bill Wendling92e287f2012-12-31 11:51:54 +0000623/// Attributes for the result are denoted with Idx = 0. Function attributes are
624/// denoted with Idx = ~0.
Bill Wendling034b94b2012-12-19 07:18:57 +0000625Attribute AttributeSet::getAttributes(unsigned Idx) const {
626 if (AttrList == 0) return Attribute();
Bill Wendling77898682012-10-16 06:01:44 +0000627
Bill Wendling60507d52013-01-04 20:54:35 +0000628 ArrayRef<AttributeWithIndex> Attrs = AttrList->getAttributes();
Chris Lattner58d74912008-03-12 17:45:29 +0000629 for (unsigned i = 0, e = Attrs.size(); i != e && Attrs[i].Index <= Idx; ++i)
630 if (Attrs[i].Index == Idx)
631 return Attrs[i].Attrs;
Bill Wendlingef99fe82012-09-21 15:26:31 +0000632
Bill Wendling034b94b2012-12-19 07:18:57 +0000633 return Attribute();
Chris Lattner58d74912008-03-12 17:45:29 +0000634}
635
636/// hasAttrSomewhere - Return true if the specified attribute is set for at
637/// least one parameter or for the return value.
Bill Wendling629fb822012-12-22 00:37:52 +0000638bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr) const {
Devang Patel05988662008-09-25 21:00:45 +0000639 if (AttrList == 0) return false;
Bill Wendling7d2f2492012-10-10 07:36:45 +0000640
Bill Wendling60507d52013-01-04 20:54:35 +0000641 ArrayRef<AttributeWithIndex> Attrs = AttrList->getAttributes();
Chris Lattner58d74912008-03-12 17:45:29 +0000642 for (unsigned i = 0, e = Attrs.size(); i != e; ++i)
Bill Wendling7d2f2492012-10-10 07:36:45 +0000643 if (Attrs[i].Attrs.hasAttribute(Attr))
Chris Lattner58d74912008-03-12 17:45:29 +0000644 return true;
Bill Wendling0976e002012-11-20 05:09:20 +0000645
Chris Lattner58d74912008-03-12 17:45:29 +0000646 return false;
647}
648
Bill Wendling99faa3b2012-12-07 23:16:57 +0000649AttributeSet AttributeSet::addAttr(LLVMContext &C, unsigned Idx,
Bill Wendling9d30e722012-12-31 00:49:59 +0000650 Attribute Attrs) const {
Bill Wendling034b94b2012-12-19 07:18:57 +0000651 Attribute OldAttrs = getAttributes(Idx);
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000652#ifndef NDEBUG
653 // FIXME it is not obvious how this should work for alignment.
654 // For now, say we can't change a known alignment.
Bill Wendlingef99fe82012-09-21 15:26:31 +0000655 unsigned OldAlign = OldAttrs.getAlignment();
656 unsigned NewAlign = Attrs.getAlignment();
Anton Korobeynikov4aefd6b2008-02-20 12:07:57 +0000657 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000658 "Attempt to change alignment!");
659#endif
Bill Wendling77898682012-10-16 06:01:44 +0000660
Bill Wendling702cc912012-10-15 20:35:56 +0000661 AttrBuilder NewAttrs =
662 AttrBuilder(OldAttrs).addAttributes(Attrs);
663 if (NewAttrs == AttrBuilder(OldAttrs))
Chris Lattner58d74912008-03-12 17:45:29 +0000664 return *this;
Bill Wendling77898682012-10-16 06:01:44 +0000665
Devang Patel05988662008-09-25 21:00:45 +0000666 SmallVector<AttributeWithIndex, 8> NewAttrList;
667 if (AttrList == 0)
668 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Chris Lattner58d74912008-03-12 17:45:29 +0000669 else {
Bill Wendling60507d52013-01-04 20:54:35 +0000670 ArrayRef<AttributeWithIndex> OldAttrList = AttrList->getAttributes();
Chris Lattner58d74912008-03-12 17:45:29 +0000671 unsigned i = 0, e = OldAttrList.size();
672 // Copy attributes for arguments before this one.
673 for (; i != e && OldAttrList[i].Index < Idx; ++i)
674 NewAttrList.push_back(OldAttrList[i]);
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000675
Chris Lattner58d74912008-03-12 17:45:29 +0000676 // If there are attributes already at this index, merge them in.
677 if (i != e && OldAttrList[i].Index == Idx) {
Bill Wendlingc4167952012-10-14 07:35:59 +0000678 Attrs =
Bill Wendling034b94b2012-12-19 07:18:57 +0000679 Attribute::get(C, AttrBuilder(Attrs).
Bill Wendlingc4167952012-10-14 07:35:59 +0000680 addAttributes(OldAttrList[i].Attrs));
Chris Lattner58d74912008-03-12 17:45:29 +0000681 ++i;
682 }
Bill Wendling77898682012-10-16 06:01:44 +0000683
Devang Patel05988662008-09-25 21:00:45 +0000684 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Bill Wendling77898682012-10-16 06:01:44 +0000685
Chris Lattner58d74912008-03-12 17:45:29 +0000686 // Copy attributes for arguments after this one.
Bill Wendling77898682012-10-16 06:01:44 +0000687 NewAttrList.insert(NewAttrList.end(),
Chris Lattner58d74912008-03-12 17:45:29 +0000688 OldAttrList.begin()+i, OldAttrList.end());
689 }
Bill Wendling77898682012-10-16 06:01:44 +0000690
Bill Wendling0976e002012-11-20 05:09:20 +0000691 return get(C, NewAttrList);
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000692}
693
Bill Wendling99faa3b2012-12-07 23:16:57 +0000694AttributeSet AttributeSet::removeAttr(LLVMContext &C, unsigned Idx,
Bill Wendling9d30e722012-12-31 00:49:59 +0000695 Attribute Attrs) const {
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000696#ifndef NDEBUG
697 // FIXME it is not obvious how this should work for alignment.
698 // For now, say we can't pass in alignment, which no current use does.
Bill Wendling034b94b2012-12-19 07:18:57 +0000699 assert(!Attrs.hasAttribute(Attribute::Alignment) &&
Bill Wendling67658342012-10-09 07:45:08 +0000700 "Attempt to exclude alignment!");
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000701#endif
Bill Wendling99faa3b2012-12-07 23:16:57 +0000702 if (AttrList == 0) return AttributeSet();
Bill Wendling77898682012-10-16 06:01:44 +0000703
Bill Wendling034b94b2012-12-19 07:18:57 +0000704 Attribute OldAttrs = getAttributes(Idx);
Bill Wendling702cc912012-10-15 20:35:56 +0000705 AttrBuilder NewAttrs =
706 AttrBuilder(OldAttrs).removeAttributes(Attrs);
707 if (NewAttrs == AttrBuilder(OldAttrs))
Chris Lattner58d74912008-03-12 17:45:29 +0000708 return *this;
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000709
Devang Patel05988662008-09-25 21:00:45 +0000710 SmallVector<AttributeWithIndex, 8> NewAttrList;
Bill Wendling60507d52013-01-04 20:54:35 +0000711 ArrayRef<AttributeWithIndex> OldAttrList = AttrList->getAttributes();
Chris Lattner58d74912008-03-12 17:45:29 +0000712 unsigned i = 0, e = OldAttrList.size();
Bill Wendling77898682012-10-16 06:01:44 +0000713
Chris Lattner58d74912008-03-12 17:45:29 +0000714 // Copy attributes for arguments before this one.
715 for (; i != e && OldAttrList[i].Index < Idx; ++i)
716 NewAttrList.push_back(OldAttrList[i]);
Bill Wendling77898682012-10-16 06:01:44 +0000717
Chris Lattner58d74912008-03-12 17:45:29 +0000718 // If there are attributes already at this index, merge them in.
719 assert(OldAttrList[i].Index == Idx && "Attribute isn't set?");
Bill Wendling034b94b2012-12-19 07:18:57 +0000720 Attrs = Attribute::get(C, AttrBuilder(OldAttrList[i].Attrs).
Bill Wendling5886b7b2012-10-14 06:39:53 +0000721 removeAttributes(Attrs));
Chris Lattner58d74912008-03-12 17:45:29 +0000722 ++i;
Bill Wendling7be78482012-10-14 08:54:26 +0000723 if (Attrs.hasAttributes()) // If any attributes left for this param, add them.
Devang Patel05988662008-09-25 21:00:45 +0000724 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Bill Wendling77898682012-10-16 06:01:44 +0000725
Chris Lattner58d74912008-03-12 17:45:29 +0000726 // Copy attributes for arguments after this one.
Bill Wendling77898682012-10-16 06:01:44 +0000727 NewAttrList.insert(NewAttrList.end(),
Chris Lattner58d74912008-03-12 17:45:29 +0000728 OldAttrList.begin()+i, OldAttrList.end());
Bill Wendling77898682012-10-16 06:01:44 +0000729
Bill Wendling0976e002012-11-20 05:09:20 +0000730 return get(C, NewAttrList);
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000731}
732
Bill Wendling99faa3b2012-12-07 23:16:57 +0000733void AttributeSet::dump() const {
David Greeneef1894e2010-01-05 01:29:58 +0000734 dbgs() << "PAL[ ";
Chris Lattner58d74912008-03-12 17:45:29 +0000735 for (unsigned i = 0; i < getNumSlots(); ++i) {
Devang Patel05988662008-09-25 21:00:45 +0000736 const AttributeWithIndex &PAWI = getSlot(i);
Bill Wendling60507d52013-01-04 20:54:35 +0000737 dbgs() << "{ " << PAWI.Index << ", " << PAWI.Attrs.getAsString() << " } ";
Chris Lattner58d74912008-03-12 17:45:29 +0000738 }
Bill Wendling77898682012-10-16 06:01:44 +0000739
David Greeneef1894e2010-01-05 01:29:58 +0000740 dbgs() << "]\n";
Duncan Sandsad9a9e12008-01-06 18:27:01 +0000741}