blob: bb98358a0abbaa0abe8bf0f515160692f9cc3828 [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
Bill Wendling956f1342013-01-18 21:11:39 +0000258 uint64_t Mask = AWI->Attrs.Raw();
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000259
Bill Wendling956f1342013-01-18 21:11:39 +0000260 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
261 I = Attribute::AttrKind(I + 1)) {
262 if (uint64_t A = (Mask & AttributeImpl::getAttrMask(I))) {
263 Attrs.insert(I);
264
265 if (I == Attribute::Alignment)
266 Alignment = 1ULL << ((A >> 16) - 1);
267 else if (I == Attribute::StackAlignment)
268 StackAlignment = 1ULL << ((A >> 26)-1);
269 }
270 }
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000271}
272
Bill Wendling03198882013-01-04 23:27:34 +0000273void AttrBuilder::clear() {
274 Attrs.clear();
275 Alignment = StackAlignment = 0;
276}
277
278AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
279 Attrs.insert(Val);
Bill Wendling3a106e62012-10-09 19:01:18 +0000280 return *this;
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000281}
282
Bill Wendling03198882013-01-04 23:27:34 +0000283AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
284 Attrs.erase(Val);
285 if (Val == Attribute::Alignment)
286 Alignment = 0;
287 else if (Val == Attribute::StackAlignment)
288 StackAlignment = 0;
289
Bill Wendlinga19a5302012-10-14 04:10:01 +0000290 return *this;
291}
292
Bill Wendling702cc912012-10-15 20:35:56 +0000293AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
Bill Wendlingda3f9d82012-10-14 03:58:29 +0000294 if (Align == 0) return *this;
Bill Wendling03198882013-01-04 23:27:34 +0000295
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000296 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
297 assert(Align <= 0x40000000 && "Alignment too large.");
Bill Wendling03198882013-01-04 23:27:34 +0000298
299 Attrs.insert(Attribute::Alignment);
300 Alignment = Align;
Bill Wendlingda3f9d82012-10-14 03:58:29 +0000301 return *this;
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000302}
303
Bill Wendling03198882013-01-04 23:27:34 +0000304AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
305 // Default alignment, allow the target to define how to align it.
306 if (Align == 0) return *this;
307
308 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
309 assert(Align <= 0x100 && "Alignment too large.");
310
311 Attrs.insert(Attribute::StackAlignment);
312 StackAlignment = Align;
313 return *this;
314}
315
316AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) {
317 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
318 I = Attribute::AttrKind(I + 1)) {
319 if (uint64_t A = (Val & AttributeImpl::getAttrMask(I))) {
320 Attrs.insert(I);
321
322 if (I == Attribute::Alignment)
323 Alignment = 1ULL << ((A >> 16) - 1);
324 else if (I == Attribute::StackAlignment)
325 StackAlignment = 1ULL << ((A >> 26)-1);
326 }
327 }
328
Bill Wendling3a106e62012-10-09 19:01:18 +0000329 return *this;
Bill Wendling2e879bc2012-10-09 09:11:20 +0000330}
331
Bill Wendling034b94b2012-12-19 07:18:57 +0000332AttrBuilder &AttrBuilder::addAttributes(const Attribute &A) {
Bill Wendling1db9b692013-01-09 23:36:50 +0000333 uint64_t Mask = A.Raw();
Bill Wendling03198882013-01-04 23:27:34 +0000334
335 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
336 I = Attribute::AttrKind(I + 1)) {
337 if (uint64_t A = (Mask & AttributeImpl::getAttrMask(I))) {
338 Attrs.insert(I);
339
340 if (I == Attribute::Alignment)
341 Alignment = 1ULL << ((A >> 16) - 1);
342 else if (I == Attribute::StackAlignment)
343 StackAlignment = 1ULL << ((A >> 26)-1);
344 }
345 }
346
Bill Wendling432e6062012-10-14 07:17:34 +0000347 return *this;
348}
349
Bill Wendling034b94b2012-12-19 07:18:57 +0000350AttrBuilder &AttrBuilder::removeAttributes(const Attribute &A){
Bill Wendling1db9b692013-01-09 23:36:50 +0000351 uint64_t Mask = A.Raw();
Bill Wendling03198882013-01-04 23:27:34 +0000352
353 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
354 I = Attribute::AttrKind(I + 1)) {
355 if (Mask & AttributeImpl::getAttrMask(I)) {
356 Attrs.erase(I);
357
358 if (I == Attribute::Alignment)
359 Alignment = 0;
360 else if (I == Attribute::StackAlignment)
361 StackAlignment = 0;
362 }
363 }
364
Bill Wendling5886b7b2012-10-14 06:39:53 +0000365 return *this;
Bill Wendling8831c062012-10-09 00:01:21 +0000366}
367
Bill Wendling22bd6412013-01-03 01:54:39 +0000368bool AttrBuilder::contains(Attribute::AttrKind A) const {
Bill Wendling03198882013-01-04 23:27:34 +0000369 return Attrs.count(A);
Bill Wendling7d2f2492012-10-10 07:36:45 +0000370}
371
Bill Wendling702cc912012-10-15 20:35:56 +0000372bool AttrBuilder::hasAttributes() const {
Bill Wendling03198882013-01-04 23:27:34 +0000373 return !Attrs.empty();
Bill Wendlingf385f4c2012-10-08 23:27:46 +0000374}
Bill Wendling60507d52013-01-04 20:54:35 +0000375
Bill Wendling034b94b2012-12-19 07:18:57 +0000376bool AttrBuilder::hasAttributes(const Attribute &A) const {
Bill Wendling1db9b692013-01-09 23:36:50 +0000377 return Raw() & A.Raw();
Bill Wendling8831c062012-10-09 00:01:21 +0000378}
Bill Wendling60507d52013-01-04 20:54:35 +0000379
Bill Wendling702cc912012-10-15 20:35:56 +0000380bool AttrBuilder::hasAlignmentAttr() const {
Bill Wendling03198882013-01-04 23:27:34 +0000381 return Alignment != 0;
Bill Wendlingf385f4c2012-10-08 23:27:46 +0000382}
383
Bill Wendling1db9b692013-01-09 23:36:50 +0000384uint64_t AttrBuilder::Raw() const {
Bill Wendling03198882013-01-04 23:27:34 +0000385 uint64_t Mask = 0;
386
387 for (DenseSet<Attribute::AttrKind>::const_iterator I = Attrs.begin(),
388 E = Attrs.end(); I != E; ++I) {
389 Attribute::AttrKind Kind = *I;
390
391 if (Kind == Attribute::Alignment)
392 Mask |= (Log2_32(Alignment) + 1) << 16;
393 else if (Kind == Attribute::StackAlignment)
394 Mask |= (Log2_32(StackAlignment) + 1) << 26;
395 else
396 Mask |= AttributeImpl::getAttrMask(Kind);
397 }
398
399 return Mask;
Bill Wendlingf385f4c2012-10-08 23:27:46 +0000400}
401
Bill Wendling03198882013-01-04 23:27:34 +0000402bool AttrBuilder::operator==(const AttrBuilder &B) {
403 SmallVector<Attribute::AttrKind, 8> This(Attrs.begin(), Attrs.end());
404 SmallVector<Attribute::AttrKind, 8> That(B.Attrs.begin(), B.Attrs.end());
405 return This == That;
Bill Wendling7d2f2492012-10-10 07:36:45 +0000406}
407
Chris Lattner58d74912008-03-12 17:45:29 +0000408//===----------------------------------------------------------------------===//
Bill Wendling2c79ecb2012-09-26 21:07:29 +0000409// AttributeImpl Definition
410//===----------------------------------------------------------------------===//
411
Bill Wendling1bbd6442013-01-05 01:36:54 +0000412AttributeImpl::AttributeImpl(LLVMContext &C, uint64_t data)
413 : Context(C) {
Bill Wendling7c1683d2012-12-29 12:29:38 +0000414 Data = ConstantInt::get(Type::getInt64Ty(C), data);
415}
Bill Wendling1bbd6442013-01-05 01:36:54 +0000416AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind data)
417 : Context(C) {
Bill Wendling979aff62012-12-30 02:22:16 +0000418 Data = ConstantInt::get(Type::getInt64Ty(C), data);
419}
420AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind data,
Bill Wendling1bbd6442013-01-05 01:36:54 +0000421 ArrayRef<Constant*> values)
422 : Context(C) {
Bill Wendling979aff62012-12-30 02:22:16 +0000423 Data = ConstantInt::get(Type::getInt64Ty(C), data);
424 Vals.reserve(values.size());
425 Vals.append(values.begin(), values.end());
426}
Bill Wendling1bbd6442013-01-05 01:36:54 +0000427AttributeImpl::AttributeImpl(LLVMContext &C, StringRef data)
428 : Context(C) {
Bill Wendling979aff62012-12-30 02:22:16 +0000429 Data = ConstantDataArray::getString(C, data);
430}
Bill Wendling7c1683d2012-12-29 12:29:38 +0000431
Bill Wendling60507d52013-01-04 20:54:35 +0000432bool AttributeImpl::operator==(Attribute::AttrKind Kind) const {
Bill Wendling529ec712012-12-30 01:38:39 +0000433 if (ConstantInt *CI = dyn_cast<ConstantInt>(Data))
434 return CI->getZExtValue() == Kind;
435 return false;
436}
Bill Wendling60507d52013-01-04 20:54:35 +0000437bool AttributeImpl::operator!=(Attribute::AttrKind Kind) const {
438 return !(*this == Kind);
439}
Bill Wendling529ec712012-12-30 01:38:39 +0000440
Bill Wendling60507d52013-01-04 20:54:35 +0000441bool AttributeImpl::operator==(StringRef Kind) const {
Bill Wendling529ec712012-12-30 01:38:39 +0000442 if (ConstantDataArray *CDA = dyn_cast<ConstantDataArray>(Data))
443 if (CDA->isString())
444 return CDA->getAsString() == Kind;
445 return false;
446}
Bill Wendling60507d52013-01-04 20:54:35 +0000447bool AttributeImpl::operator!=(StringRef Kind) const {
448 return !(*this == Kind);
449}
Bill Wendling529ec712012-12-30 01:38:39 +0000450
Bill Wendling1db9b692013-01-09 23:36:50 +0000451uint64_t AttributeImpl::Raw() const {
Bill Wendling529ec712012-12-30 01:38:39 +0000452 // FIXME: Remove this.
Bill Wendling7c1683d2012-12-29 12:29:38 +0000453 return cast<ConstantInt>(Data)->getZExtValue();
454}
455
Bill Wendling60507d52013-01-04 20:54:35 +0000456uint64_t AttributeImpl::getAttrMask(Attribute::AttrKind Val) {
Bill Wendling67658342012-10-09 07:45:08 +0000457 switch (Val) {
Chandler Carruth6f78fbb2013-01-05 08:47:26 +0000458 case Attribute::EndAttrKinds:
459 case Attribute::AttrKindEmptyKey:
460 case Attribute::AttrKindTombstoneKey:
461 llvm_unreachable("Synthetic enumerators which should never get here");
462
Bill Wendling034b94b2012-12-19 07:18:57 +0000463 case Attribute::None: return 0;
464 case Attribute::ZExt: return 1 << 0;
465 case Attribute::SExt: return 1 << 1;
466 case Attribute::NoReturn: return 1 << 2;
467 case Attribute::InReg: return 1 << 3;
468 case Attribute::StructRet: return 1 << 4;
469 case Attribute::NoUnwind: return 1 << 5;
470 case Attribute::NoAlias: return 1 << 6;
471 case Attribute::ByVal: return 1 << 7;
472 case Attribute::Nest: return 1 << 8;
473 case Attribute::ReadNone: return 1 << 9;
474 case Attribute::ReadOnly: return 1 << 10;
475 case Attribute::NoInline: return 1 << 11;
476 case Attribute::AlwaysInline: return 1 << 12;
477 case Attribute::OptimizeForSize: return 1 << 13;
478 case Attribute::StackProtect: return 1 << 14;
479 case Attribute::StackProtectReq: return 1 << 15;
480 case Attribute::Alignment: return 31 << 16;
481 case Attribute::NoCapture: return 1 << 21;
482 case Attribute::NoRedZone: return 1 << 22;
483 case Attribute::NoImplicitFloat: return 1 << 23;
484 case Attribute::Naked: return 1 << 24;
485 case Attribute::InlineHint: return 1 << 25;
486 case Attribute::StackAlignment: return 7 << 26;
487 case Attribute::ReturnsTwice: return 1 << 29;
488 case Attribute::UWTable: return 1 << 30;
489 case Attribute::NonLazyBind: return 1U << 31;
490 case Attribute::AddressSafety: return 1ULL << 32;
491 case Attribute::MinSize: return 1ULL << 33;
James Molloy67ae1352012-12-20 16:04:27 +0000492 case Attribute::NoDuplicate: return 1ULL << 34;
Bill Wendling67658342012-10-09 07:45:08 +0000493 }
494 llvm_unreachable("Unsupported attribute type");
495}
496
Bill Wendling60507d52013-01-04 20:54:35 +0000497bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
Bill Wendling1db9b692013-01-09 23:36:50 +0000498 return (Raw() & getAttrMask(A)) != 0;
Bill Wendling8e635db2012-10-08 21:47:17 +0000499}
Bill Wendling2c79ecb2012-09-26 21:07:29 +0000500
Bill Wendlingf6670722012-12-20 01:36:59 +0000501bool AttributeImpl::hasAttributes() const {
Bill Wendling1db9b692013-01-09 23:36:50 +0000502 return Raw() != 0;
Bill Wendling8e635db2012-10-08 21:47:17 +0000503}
Bill Wendling2c79ecb2012-09-26 21:07:29 +0000504
Bill Wendlingf6670722012-12-20 01:36:59 +0000505uint64_t AttributeImpl::getAlignment() const {
Bill Wendling1db9b692013-01-09 23:36:50 +0000506 return Raw() & getAttrMask(Attribute::Alignment);
Bill Wendling8e635db2012-10-08 21:47:17 +0000507}
Bill Wendling2c79ecb2012-09-26 21:07:29 +0000508
Bill Wendling1bbd6442013-01-05 01:36:54 +0000509void AttributeImpl::setAlignment(unsigned Align) {
510 Vals.push_back(ConstantInt::get(Type::getInt64Ty(Context), Align));
511}
512
Bill Wendlingf6670722012-12-20 01:36:59 +0000513uint64_t AttributeImpl::getStackAlignment() const {
Bill Wendling1db9b692013-01-09 23:36:50 +0000514 return Raw() & getAttrMask(Attribute::StackAlignment);
Bill Wendling8e635db2012-10-08 21:47:17 +0000515}
Bill Wendling2c79ecb2012-09-26 21:07:29 +0000516
Bill Wendling1bbd6442013-01-05 01:36:54 +0000517void AttributeImpl::setStackAlignment(unsigned Align) {
518 Vals.push_back(ConstantInt::get(Type::getInt64Ty(Context), Align));
519}
520
Bill Wendling8456efb2013-01-09 00:32:55 +0000521void AttributeImpl::Profile(FoldingSetNodeID &ID, Constant *Data,
522 ArrayRef<Constant*> Vals) {
Bill Wendlingff887162013-01-09 00:32:08 +0000523 ID.AddInteger(cast<ConstantInt>(Data)->getZExtValue());
Bill Wendling1db9b692013-01-09 23:36:50 +0000524#if 0
525 // FIXME: Not yet supported.
Bill Wendlingff887162013-01-09 00:32:08 +0000526 for (ArrayRef<Constant*>::iterator I = Vals.begin(), E = Vals.end();
527 I != E; ++I)
528 ID.AddPointer(*I);
Bill Wendling1db9b692013-01-09 23:36:50 +0000529#endif
Bill Wendlingff887162013-01-09 00:32:08 +0000530}
531
Bill Wendling2c79ecb2012-09-26 21:07:29 +0000532//===----------------------------------------------------------------------===//
Bill Wendling18e72112012-12-19 22:42:22 +0000533// AttributeSetImpl Definition
Chris Lattner58d74912008-03-12 17:45:29 +0000534//===----------------------------------------------------------------------===//
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000535
Bill Wendling99faa3b2012-12-07 23:16:57 +0000536AttributeSet AttributeSet::get(LLVMContext &C,
Bill Wendling1ce47ac2012-12-12 19:21:53 +0000537 ArrayRef<AttributeWithIndex> Attrs) {
Devang Patel05988662008-09-25 21:00:45 +0000538 // If there are no attributes then return a null AttributesList pointer.
Chris Lattnerd509d0b2012-05-28 01:47:44 +0000539 if (Attrs.empty())
Bill Wendling99faa3b2012-12-07 23:16:57 +0000540 return AttributeSet();
Bill Wendling77898682012-10-16 06:01:44 +0000541
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000542#ifndef NDEBUG
Chris Lattnerd509d0b2012-05-28 01:47:44 +0000543 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
Bill Wendling77898682012-10-16 06:01:44 +0000544 assert(Attrs[i].Attrs.hasAttributes() &&
Devang Patel05988662008-09-25 21:00:45 +0000545 "Pointless attribute!");
Chris Lattner58d74912008-03-12 17:45:29 +0000546 assert((!i || Attrs[i-1].Index < Attrs[i].Index) &&
Devang Patel05988662008-09-25 21:00:45 +0000547 "Misordered AttributesList!");
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000548 }
549#endif
Bill Wendling77898682012-10-16 06:01:44 +0000550
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000551 // Otherwise, build a key to look up the existing attributes.
Bill Wendling0976e002012-11-20 05:09:20 +0000552 LLVMContextImpl *pImpl = C.pImpl;
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000553 FoldingSetNodeID ID;
Bill Wendling18e72112012-12-19 22:42:22 +0000554 AttributeSetImpl::Profile(ID, Attrs);
Bill Wendling77898682012-10-16 06:01:44 +0000555
Bill Wendling0976e002012-11-20 05:09:20 +0000556 void *InsertPoint;
Bill Wendling1bbd6442013-01-05 01:36:54 +0000557 AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
Bill Wendling77898682012-10-16 06:01:44 +0000558
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000559 // If we didn't find any existing attributes of the same shape then
560 // create a new one and insert it.
Bill Wendling0976e002012-11-20 05:09:20 +0000561 if (!PA) {
Bill Wendling5f93e2b2012-12-19 23:55:43 +0000562 PA = new AttributeSetImpl(C, Attrs);
Bill Wendling0976e002012-11-20 05:09:20 +0000563 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000564 }
Bill Wendling77898682012-10-16 06:01:44 +0000565
Devang Patel05988662008-09-25 21:00:45 +0000566 // Return the AttributesList that we found or created.
Bill Wendling99faa3b2012-12-07 23:16:57 +0000567 return AttributeSet(PA);
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000568}
569
Bill Wendling1bbd6442013-01-05 01:36:54 +0000570AttributeSet AttributeSet::get(LLVMContext &C, unsigned Idx, AttrBuilder &B) {
571 SmallVector<AttributeWithIndex, 8> Attrs;
572 for (AttrBuilder::iterator I = B.begin(), E = B.end(); I != E; ++I) {
573 Attribute::AttrKind Kind = *I;
574 Attribute A = Attribute::get(C, Kind);
575
576 if (Kind == Attribute::Alignment)
577 A.setAlignment(B.getAlignment());
578 else if (Kind == Attribute::StackAlignment)
579 A.setStackAlignment(B.getStackAlignment());
580
581 Attrs.push_back(AttributeWithIndex::get(Idx, A));
582 }
583
584 return get(C, Attrs);
585}
586
Chris Lattner58d74912008-03-12 17:45:29 +0000587//===----------------------------------------------------------------------===//
Bill Wendling99faa3b2012-12-07 23:16:57 +0000588// AttributeSet Method Implementations
Chris Lattner58d74912008-03-12 17:45:29 +0000589//===----------------------------------------------------------------------===//
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000590
Bill Wendling99faa3b2012-12-07 23:16:57 +0000591const AttributeSet &AttributeSet::operator=(const AttributeSet &RHS) {
Bill Wendling0976e002012-11-20 05:09:20 +0000592 AttrList = RHS.AttrList;
593 return *this;
Chris Lattner58d74912008-03-12 17:45:29 +0000594}
595
Bill Wendling77898682012-10-16 06:01:44 +0000596/// getNumSlots - Return the number of slots used in this attribute list.
Chris Lattner58d74912008-03-12 17:45:29 +0000597/// This is the number of arguments that have an attribute set on them
598/// (including the function itself).
Bill Wendling99faa3b2012-12-07 23:16:57 +0000599unsigned AttributeSet::getNumSlots() const {
Bill Wendling60507d52013-01-04 20:54:35 +0000600 return AttrList ? AttrList->getNumAttributes() : 0;
Chris Lattner58d74912008-03-12 17:45:29 +0000601}
602
Devang Patel05988662008-09-25 21:00:45 +0000603/// getSlot - Return the AttributeWithIndex at the specified slot. This
604/// holds a number plus a set of attributes.
Bill Wendling99faa3b2012-12-07 23:16:57 +0000605const AttributeWithIndex &AttributeSet::getSlot(unsigned Slot) const {
Bill Wendling60507d52013-01-04 20:54:35 +0000606 assert(AttrList && Slot < AttrList->getNumAttributes() &&
607 "Slot # out of range!");
608 return AttrList->getAttributes()[Slot];
Chris Lattner58d74912008-03-12 17:45:29 +0000609}
610
Bill Wendling831737d2012-12-30 10:32:01 +0000611bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{
612 return getAttributes(Index).hasAttribute(Kind);
613}
614
615bool AttributeSet::hasAttributes(unsigned Index) const {
616 return getAttributes(Index).hasAttributes();
617}
618
619std::string AttributeSet::getAsString(unsigned Index) const {
620 return getAttributes(Index).getAsString();
621}
622
Bill Wendling956f1342013-01-18 21:11:39 +0000623unsigned AttributeSet::getParamAlignment(unsigned Idx) const {
624 return getAttributes(Idx).getAlignment();
625}
626
Bill Wendling831737d2012-12-30 10:32:01 +0000627unsigned AttributeSet::getStackAlignment(unsigned Index) const {
628 return getAttributes(Index).getStackAlignment();
629}
630
Bill Wendling1db9b692013-01-09 23:36:50 +0000631uint64_t AttributeSet::Raw(unsigned Index) const {
Bill Wendling831737d2012-12-30 10:32:01 +0000632 // FIXME: Remove this.
Bill Wendling1db9b692013-01-09 23:36:50 +0000633 return getAttributes(Index).Raw();
Bill Wendling831737d2012-12-30 10:32:01 +0000634}
635
Bill Wendling77898682012-10-16 06:01:44 +0000636/// getAttributes - The attributes for the specified index are returned.
Bill Wendling92e287f2012-12-31 11:51:54 +0000637/// Attributes for the result are denoted with Idx = 0. Function attributes are
638/// denoted with Idx = ~0.
Bill Wendling034b94b2012-12-19 07:18:57 +0000639Attribute AttributeSet::getAttributes(unsigned Idx) const {
640 if (AttrList == 0) return Attribute();
Bill Wendling77898682012-10-16 06:01:44 +0000641
Bill Wendling60507d52013-01-04 20:54:35 +0000642 ArrayRef<AttributeWithIndex> Attrs = AttrList->getAttributes();
Chris Lattner58d74912008-03-12 17:45:29 +0000643 for (unsigned i = 0, e = Attrs.size(); i != e && Attrs[i].Index <= Idx; ++i)
644 if (Attrs[i].Index == Idx)
645 return Attrs[i].Attrs;
Bill Wendlingef99fe82012-09-21 15:26:31 +0000646
Bill Wendling034b94b2012-12-19 07:18:57 +0000647 return Attribute();
Chris Lattner58d74912008-03-12 17:45:29 +0000648}
649
650/// hasAttrSomewhere - Return true if the specified attribute is set for at
651/// least one parameter or for the return value.
Bill Wendling629fb822012-12-22 00:37:52 +0000652bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr) const {
Devang Patel05988662008-09-25 21:00:45 +0000653 if (AttrList == 0) return false;
Bill Wendling7d2f2492012-10-10 07:36:45 +0000654
Bill Wendling60507d52013-01-04 20:54:35 +0000655 ArrayRef<AttributeWithIndex> Attrs = AttrList->getAttributes();
Chris Lattner58d74912008-03-12 17:45:29 +0000656 for (unsigned i = 0, e = Attrs.size(); i != e; ++i)
Bill Wendling7d2f2492012-10-10 07:36:45 +0000657 if (Attrs[i].Attrs.hasAttribute(Attr))
Chris Lattner58d74912008-03-12 17:45:29 +0000658 return true;
Bill Wendling0976e002012-11-20 05:09:20 +0000659
Chris Lattner58d74912008-03-12 17:45:29 +0000660 return false;
661}
662
Bill Wendling1b0c54f2013-01-18 21:53:16 +0000663AttributeSet AttributeSet::addRetAttributes(LLVMContext &C,
664 AttributeSet Attrs) const {
665 return addAttr(C, ReturnIndex, getAttributes(ReturnIndex));
666}
667
Bill Wendling956f1342013-01-18 21:11:39 +0000668AttributeSet AttributeSet::addFnAttributes(LLVMContext &C,
669 AttributeSet Attrs) const {
670 return addAttr(C, FunctionIndex, getAttributes(FunctionIndex));
671}
672
Bill Wendling99faa3b2012-12-07 23:16:57 +0000673AttributeSet AttributeSet::addAttr(LLVMContext &C, unsigned Idx,
Bill Wendling9d30e722012-12-31 00:49:59 +0000674 Attribute Attrs) const {
Bill Wendling034b94b2012-12-19 07:18:57 +0000675 Attribute OldAttrs = getAttributes(Idx);
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000676#ifndef NDEBUG
677 // FIXME it is not obvious how this should work for alignment.
678 // For now, say we can't change a known alignment.
Bill Wendlingef99fe82012-09-21 15:26:31 +0000679 unsigned OldAlign = OldAttrs.getAlignment();
680 unsigned NewAlign = Attrs.getAlignment();
Anton Korobeynikov4aefd6b2008-02-20 12:07:57 +0000681 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000682 "Attempt to change alignment!");
683#endif
Bill Wendling77898682012-10-16 06:01:44 +0000684
Bill Wendling702cc912012-10-15 20:35:56 +0000685 AttrBuilder NewAttrs =
686 AttrBuilder(OldAttrs).addAttributes(Attrs);
687 if (NewAttrs == AttrBuilder(OldAttrs))
Chris Lattner58d74912008-03-12 17:45:29 +0000688 return *this;
Bill Wendling77898682012-10-16 06:01:44 +0000689
Devang Patel05988662008-09-25 21:00:45 +0000690 SmallVector<AttributeWithIndex, 8> NewAttrList;
691 if (AttrList == 0)
692 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Chris Lattner58d74912008-03-12 17:45:29 +0000693 else {
Bill Wendling60507d52013-01-04 20:54:35 +0000694 ArrayRef<AttributeWithIndex> OldAttrList = AttrList->getAttributes();
Chris Lattner58d74912008-03-12 17:45:29 +0000695 unsigned i = 0, e = OldAttrList.size();
696 // Copy attributes for arguments before this one.
697 for (; i != e && OldAttrList[i].Index < Idx; ++i)
698 NewAttrList.push_back(OldAttrList[i]);
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000699
Chris Lattner58d74912008-03-12 17:45:29 +0000700 // If there are attributes already at this index, merge them in.
701 if (i != e && OldAttrList[i].Index == Idx) {
Bill Wendlingc4167952012-10-14 07:35:59 +0000702 Attrs =
Bill Wendling034b94b2012-12-19 07:18:57 +0000703 Attribute::get(C, AttrBuilder(Attrs).
Bill Wendlingc4167952012-10-14 07:35:59 +0000704 addAttributes(OldAttrList[i].Attrs));
Chris Lattner58d74912008-03-12 17:45:29 +0000705 ++i;
706 }
Bill Wendling77898682012-10-16 06:01:44 +0000707
Devang Patel05988662008-09-25 21:00:45 +0000708 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Bill Wendling77898682012-10-16 06:01:44 +0000709
Chris Lattner58d74912008-03-12 17:45:29 +0000710 // Copy attributes for arguments after this one.
Bill Wendling77898682012-10-16 06:01:44 +0000711 NewAttrList.insert(NewAttrList.end(),
Chris Lattner58d74912008-03-12 17:45:29 +0000712 OldAttrList.begin()+i, OldAttrList.end());
713 }
Bill Wendling77898682012-10-16 06:01:44 +0000714
Bill Wendling0976e002012-11-20 05:09:20 +0000715 return get(C, NewAttrList);
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000716}
717
Bill Wendling99faa3b2012-12-07 23:16:57 +0000718AttributeSet AttributeSet::removeAttr(LLVMContext &C, unsigned Idx,
Bill Wendling9d30e722012-12-31 00:49:59 +0000719 Attribute Attrs) const {
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000720#ifndef NDEBUG
721 // FIXME it is not obvious how this should work for alignment.
722 // For now, say we can't pass in alignment, which no current use does.
Bill Wendling034b94b2012-12-19 07:18:57 +0000723 assert(!Attrs.hasAttribute(Attribute::Alignment) &&
Bill Wendling67658342012-10-09 07:45:08 +0000724 "Attempt to exclude alignment!");
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000725#endif
Bill Wendling99faa3b2012-12-07 23:16:57 +0000726 if (AttrList == 0) return AttributeSet();
Bill Wendling77898682012-10-16 06:01:44 +0000727
Bill Wendling034b94b2012-12-19 07:18:57 +0000728 Attribute OldAttrs = getAttributes(Idx);
Bill Wendling702cc912012-10-15 20:35:56 +0000729 AttrBuilder NewAttrs =
730 AttrBuilder(OldAttrs).removeAttributes(Attrs);
731 if (NewAttrs == AttrBuilder(OldAttrs))
Chris Lattner58d74912008-03-12 17:45:29 +0000732 return *this;
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000733
Devang Patel05988662008-09-25 21:00:45 +0000734 SmallVector<AttributeWithIndex, 8> NewAttrList;
Bill Wendling60507d52013-01-04 20:54:35 +0000735 ArrayRef<AttributeWithIndex> OldAttrList = AttrList->getAttributes();
Chris Lattner58d74912008-03-12 17:45:29 +0000736 unsigned i = 0, e = OldAttrList.size();
Bill Wendling77898682012-10-16 06:01:44 +0000737
Chris Lattner58d74912008-03-12 17:45:29 +0000738 // Copy attributes for arguments before this one.
739 for (; i != e && OldAttrList[i].Index < Idx; ++i)
740 NewAttrList.push_back(OldAttrList[i]);
Bill Wendling77898682012-10-16 06:01:44 +0000741
Chris Lattner58d74912008-03-12 17:45:29 +0000742 // If there are attributes already at this index, merge them in.
743 assert(OldAttrList[i].Index == Idx && "Attribute isn't set?");
Bill Wendling034b94b2012-12-19 07:18:57 +0000744 Attrs = Attribute::get(C, AttrBuilder(OldAttrList[i].Attrs).
Bill Wendling5886b7b2012-10-14 06:39:53 +0000745 removeAttributes(Attrs));
Chris Lattner58d74912008-03-12 17:45:29 +0000746 ++i;
Bill Wendling7be78482012-10-14 08:54:26 +0000747 if (Attrs.hasAttributes()) // If any attributes left for this param, add them.
Devang Patel05988662008-09-25 21:00:45 +0000748 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Bill Wendling77898682012-10-16 06:01:44 +0000749
Chris Lattner58d74912008-03-12 17:45:29 +0000750 // Copy attributes for arguments after this one.
Bill Wendling77898682012-10-16 06:01:44 +0000751 NewAttrList.insert(NewAttrList.end(),
Chris Lattner58d74912008-03-12 17:45:29 +0000752 OldAttrList.begin()+i, OldAttrList.end());
Bill Wendling77898682012-10-16 06:01:44 +0000753
Bill Wendling0976e002012-11-20 05:09:20 +0000754 return get(C, NewAttrList);
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000755}
756
Bill Wendling99faa3b2012-12-07 23:16:57 +0000757void AttributeSet::dump() const {
David Greeneef1894e2010-01-05 01:29:58 +0000758 dbgs() << "PAL[ ";
Chris Lattner58d74912008-03-12 17:45:29 +0000759 for (unsigned i = 0; i < getNumSlots(); ++i) {
Devang Patel05988662008-09-25 21:00:45 +0000760 const AttributeWithIndex &PAWI = getSlot(i);
Bill Wendling60507d52013-01-04 20:54:35 +0000761 dbgs() << "{ " << PAWI.Index << ", " << PAWI.Attrs.getAsString() << " } ";
Chris Lattner58d74912008-03-12 17:45:29 +0000762 }
Bill Wendling77898682012-10-16 06:01:44 +0000763
David Greeneef1894e2010-01-05 01:29:58 +0000764 dbgs() << "]\n";
Duncan Sandsad9a9e12008-01-06 18:27:01 +0000765}