blob: 4bd2391dfdbac658b267938091fedefadaf6837d [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 Wendlingdefaca02013-01-22 21:15:51 +0000153 uint64_t EncodedAttrs) {
Bill Wendling702cc912012-10-15 20:35:56 +0000154 // 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
Bill Wendlingaec71062013-01-18 21:56:07 +0000256 if (!AWI) return;
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000257
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);
Bill Wendlingdefaca02013-01-22 21:15:51 +0000321
Bill Wendling03198882013-01-04 23:27:34 +0000322 if (I == Attribute::Alignment)
323 Alignment = 1ULL << ((A >> 16) - 1);
324 else if (I == Attribute::StackAlignment)
325 StackAlignment = 1ULL << ((A >> 26)-1);
326 }
327 }
Bill Wendlingdefaca02013-01-22 21:15:51 +0000328
Bill Wendling3a106e62012-10-09 19:01:18 +0000329 return *this;
Bill Wendling2e879bc2012-10-09 09:11:20 +0000330}
331
Bill Wendlingdefaca02013-01-22 21:15:51 +0000332AttrBuilder &AttrBuilder::addAttributes(const Attribute &Attr) {
333 uint64_t Mask = Attr.Raw();
Bill Wendling03198882013-01-04 23:27:34 +0000334
335 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
Bill Wendlingdefaca02013-01-22 21:15:51 +0000336 I = Attribute::AttrKind(I + 1))
337 if ((Mask & AttributeImpl::getAttrMask(I)) != 0)
Bill Wendling03198882013-01-04 23:27:34 +0000338 Attrs.insert(I);
339
Bill Wendlingdefaca02013-01-22 21:15:51 +0000340 if (Attr.getAlignment())
341 Alignment = Attr.getAlignment();
342 if (Attr.getStackAlignment())
343 StackAlignment = Attr.getStackAlignment();
Bill Wendling432e6062012-10-14 07:17:34 +0000344 return *this;
345}
346
Bill Wendling034b94b2012-12-19 07:18:57 +0000347AttrBuilder &AttrBuilder::removeAttributes(const Attribute &A){
Bill Wendling1db9b692013-01-09 23:36:50 +0000348 uint64_t Mask = A.Raw();
Bill Wendling03198882013-01-04 23:27:34 +0000349
350 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
351 I = Attribute::AttrKind(I + 1)) {
352 if (Mask & AttributeImpl::getAttrMask(I)) {
353 Attrs.erase(I);
354
355 if (I == Attribute::Alignment)
356 Alignment = 0;
357 else if (I == Attribute::StackAlignment)
358 StackAlignment = 0;
359 }
360 }
361
Bill Wendling5886b7b2012-10-14 06:39:53 +0000362 return *this;
Bill Wendling8831c062012-10-09 00:01:21 +0000363}
364
Bill Wendling22bd6412013-01-03 01:54:39 +0000365bool AttrBuilder::contains(Attribute::AttrKind A) const {
Bill Wendling03198882013-01-04 23:27:34 +0000366 return Attrs.count(A);
Bill Wendling7d2f2492012-10-10 07:36:45 +0000367}
368
Bill Wendling702cc912012-10-15 20:35:56 +0000369bool AttrBuilder::hasAttributes() const {
Bill Wendling03198882013-01-04 23:27:34 +0000370 return !Attrs.empty();
Bill Wendlingf385f4c2012-10-08 23:27:46 +0000371}
Bill Wendling60507d52013-01-04 20:54:35 +0000372
Bill Wendling034b94b2012-12-19 07:18:57 +0000373bool AttrBuilder::hasAttributes(const Attribute &A) const {
Bill Wendling1db9b692013-01-09 23:36:50 +0000374 return Raw() & A.Raw();
Bill Wendling8831c062012-10-09 00:01:21 +0000375}
Bill Wendling60507d52013-01-04 20:54:35 +0000376
Bill Wendling702cc912012-10-15 20:35:56 +0000377bool AttrBuilder::hasAlignmentAttr() const {
Bill Wendling03198882013-01-04 23:27:34 +0000378 return Alignment != 0;
Bill Wendlingf385f4c2012-10-08 23:27:46 +0000379}
380
Bill Wendling1db9b692013-01-09 23:36:50 +0000381uint64_t AttrBuilder::Raw() const {
Bill Wendling03198882013-01-04 23:27:34 +0000382 uint64_t Mask = 0;
383
384 for (DenseSet<Attribute::AttrKind>::const_iterator I = Attrs.begin(),
385 E = Attrs.end(); I != E; ++I) {
386 Attribute::AttrKind Kind = *I;
387
388 if (Kind == Attribute::Alignment)
389 Mask |= (Log2_32(Alignment) + 1) << 16;
390 else if (Kind == Attribute::StackAlignment)
391 Mask |= (Log2_32(StackAlignment) + 1) << 26;
392 else
393 Mask |= AttributeImpl::getAttrMask(Kind);
394 }
395
396 return Mask;
Bill Wendlingf385f4c2012-10-08 23:27:46 +0000397}
398
Bill Wendling03198882013-01-04 23:27:34 +0000399bool AttrBuilder::operator==(const AttrBuilder &B) {
400 SmallVector<Attribute::AttrKind, 8> This(Attrs.begin(), Attrs.end());
401 SmallVector<Attribute::AttrKind, 8> That(B.Attrs.begin(), B.Attrs.end());
402 return This == That;
Bill Wendling7d2f2492012-10-10 07:36:45 +0000403}
404
Chris Lattner58d74912008-03-12 17:45:29 +0000405//===----------------------------------------------------------------------===//
Bill Wendling2c79ecb2012-09-26 21:07:29 +0000406// AttributeImpl Definition
407//===----------------------------------------------------------------------===//
408
Bill Wendling1bbd6442013-01-05 01:36:54 +0000409AttributeImpl::AttributeImpl(LLVMContext &C, uint64_t data)
410 : Context(C) {
Bill Wendling7c1683d2012-12-29 12:29:38 +0000411 Data = ConstantInt::get(Type::getInt64Ty(C), data);
412}
Bill Wendling1bbd6442013-01-05 01:36:54 +0000413AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind data)
414 : Context(C) {
Bill Wendling979aff62012-12-30 02:22:16 +0000415 Data = ConstantInt::get(Type::getInt64Ty(C), data);
416}
417AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind data,
Bill Wendling1bbd6442013-01-05 01:36:54 +0000418 ArrayRef<Constant*> values)
419 : Context(C) {
Bill Wendling979aff62012-12-30 02:22:16 +0000420 Data = ConstantInt::get(Type::getInt64Ty(C), data);
421 Vals.reserve(values.size());
422 Vals.append(values.begin(), values.end());
423}
Bill Wendling1bbd6442013-01-05 01:36:54 +0000424AttributeImpl::AttributeImpl(LLVMContext &C, StringRef data)
425 : Context(C) {
Bill Wendling979aff62012-12-30 02:22:16 +0000426 Data = ConstantDataArray::getString(C, data);
427}
Bill Wendling7c1683d2012-12-29 12:29:38 +0000428
Bill Wendling60507d52013-01-04 20:54:35 +0000429bool AttributeImpl::operator==(Attribute::AttrKind Kind) const {
Bill Wendling529ec712012-12-30 01:38:39 +0000430 if (ConstantInt *CI = dyn_cast<ConstantInt>(Data))
431 return CI->getZExtValue() == Kind;
432 return false;
433}
Bill Wendling60507d52013-01-04 20:54:35 +0000434bool AttributeImpl::operator!=(Attribute::AttrKind Kind) const {
435 return !(*this == Kind);
436}
Bill Wendling529ec712012-12-30 01:38:39 +0000437
Bill Wendling60507d52013-01-04 20:54:35 +0000438bool AttributeImpl::operator==(StringRef Kind) const {
Bill Wendling529ec712012-12-30 01:38:39 +0000439 if (ConstantDataArray *CDA = dyn_cast<ConstantDataArray>(Data))
440 if (CDA->isString())
441 return CDA->getAsString() == Kind;
442 return false;
443}
Bill Wendling60507d52013-01-04 20:54:35 +0000444bool AttributeImpl::operator!=(StringRef Kind) const {
445 return !(*this == Kind);
446}
Bill Wendling529ec712012-12-30 01:38:39 +0000447
Bill Wendling1db9b692013-01-09 23:36:50 +0000448uint64_t AttributeImpl::Raw() const {
Bill Wendling529ec712012-12-30 01:38:39 +0000449 // FIXME: Remove this.
Bill Wendling7c1683d2012-12-29 12:29:38 +0000450 return cast<ConstantInt>(Data)->getZExtValue();
451}
452
Bill Wendling60507d52013-01-04 20:54:35 +0000453uint64_t AttributeImpl::getAttrMask(Attribute::AttrKind Val) {
Bill Wendling67658342012-10-09 07:45:08 +0000454 switch (Val) {
Chandler Carruth6f78fbb2013-01-05 08:47:26 +0000455 case Attribute::EndAttrKinds:
456 case Attribute::AttrKindEmptyKey:
457 case Attribute::AttrKindTombstoneKey:
458 llvm_unreachable("Synthetic enumerators which should never get here");
459
Bill Wendling034b94b2012-12-19 07:18:57 +0000460 case Attribute::None: return 0;
461 case Attribute::ZExt: return 1 << 0;
462 case Attribute::SExt: return 1 << 1;
463 case Attribute::NoReturn: return 1 << 2;
464 case Attribute::InReg: return 1 << 3;
465 case Attribute::StructRet: return 1 << 4;
466 case Attribute::NoUnwind: return 1 << 5;
467 case Attribute::NoAlias: return 1 << 6;
468 case Attribute::ByVal: return 1 << 7;
469 case Attribute::Nest: return 1 << 8;
470 case Attribute::ReadNone: return 1 << 9;
471 case Attribute::ReadOnly: return 1 << 10;
472 case Attribute::NoInline: return 1 << 11;
473 case Attribute::AlwaysInline: return 1 << 12;
474 case Attribute::OptimizeForSize: return 1 << 13;
475 case Attribute::StackProtect: return 1 << 14;
476 case Attribute::StackProtectReq: return 1 << 15;
477 case Attribute::Alignment: return 31 << 16;
478 case Attribute::NoCapture: return 1 << 21;
479 case Attribute::NoRedZone: return 1 << 22;
480 case Attribute::NoImplicitFloat: return 1 << 23;
481 case Attribute::Naked: return 1 << 24;
482 case Attribute::InlineHint: return 1 << 25;
483 case Attribute::StackAlignment: return 7 << 26;
484 case Attribute::ReturnsTwice: return 1 << 29;
485 case Attribute::UWTable: return 1 << 30;
486 case Attribute::NonLazyBind: return 1U << 31;
487 case Attribute::AddressSafety: return 1ULL << 32;
488 case Attribute::MinSize: return 1ULL << 33;
James Molloy67ae1352012-12-20 16:04:27 +0000489 case Attribute::NoDuplicate: return 1ULL << 34;
Bill Wendling67658342012-10-09 07:45:08 +0000490 }
491 llvm_unreachable("Unsupported attribute type");
492}
493
Bill Wendling60507d52013-01-04 20:54:35 +0000494bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
Bill Wendling1db9b692013-01-09 23:36:50 +0000495 return (Raw() & getAttrMask(A)) != 0;
Bill Wendling8e635db2012-10-08 21:47:17 +0000496}
Bill Wendling2c79ecb2012-09-26 21:07:29 +0000497
Bill Wendlingf6670722012-12-20 01:36:59 +0000498bool AttributeImpl::hasAttributes() const {
Bill Wendling1db9b692013-01-09 23:36:50 +0000499 return Raw() != 0;
Bill Wendling8e635db2012-10-08 21:47:17 +0000500}
Bill Wendling2c79ecb2012-09-26 21:07:29 +0000501
Bill Wendlingf6670722012-12-20 01:36:59 +0000502uint64_t AttributeImpl::getAlignment() const {
Bill Wendling1db9b692013-01-09 23:36:50 +0000503 return Raw() & getAttrMask(Attribute::Alignment);
Bill Wendling8e635db2012-10-08 21:47:17 +0000504}
Bill Wendling2c79ecb2012-09-26 21:07:29 +0000505
Bill Wendling1bbd6442013-01-05 01:36:54 +0000506void AttributeImpl::setAlignment(unsigned Align) {
507 Vals.push_back(ConstantInt::get(Type::getInt64Ty(Context), Align));
508}
509
Bill Wendlingf6670722012-12-20 01:36:59 +0000510uint64_t AttributeImpl::getStackAlignment() const {
Bill Wendling1db9b692013-01-09 23:36:50 +0000511 return Raw() & getAttrMask(Attribute::StackAlignment);
Bill Wendling8e635db2012-10-08 21:47:17 +0000512}
Bill Wendling2c79ecb2012-09-26 21:07:29 +0000513
Bill Wendling1bbd6442013-01-05 01:36:54 +0000514void AttributeImpl::setStackAlignment(unsigned Align) {
515 Vals.push_back(ConstantInt::get(Type::getInt64Ty(Context), Align));
516}
517
Bill Wendling8456efb2013-01-09 00:32:55 +0000518void AttributeImpl::Profile(FoldingSetNodeID &ID, Constant *Data,
519 ArrayRef<Constant*> Vals) {
Bill Wendlingff887162013-01-09 00:32:08 +0000520 ID.AddInteger(cast<ConstantInt>(Data)->getZExtValue());
Bill Wendling1db9b692013-01-09 23:36:50 +0000521#if 0
522 // FIXME: Not yet supported.
Bill Wendlingff887162013-01-09 00:32:08 +0000523 for (ArrayRef<Constant*>::iterator I = Vals.begin(), E = Vals.end();
524 I != E; ++I)
525 ID.AddPointer(*I);
Bill Wendling1db9b692013-01-09 23:36:50 +0000526#endif
Bill Wendlingff887162013-01-09 00:32:08 +0000527}
528
Bill Wendling2c79ecb2012-09-26 21:07:29 +0000529//===----------------------------------------------------------------------===//
Bill Wendlingc5f1bc82013-01-21 21:57:28 +0000530// AttributeWithIndex Definition
531//===----------------------------------------------------------------------===//
532
533AttributeWithIndex AttributeWithIndex::get(LLVMContext &C, unsigned Idx,
534 AttributeSet AS) {
535 // FIXME: This is temporary, but necessary for the conversion.
536 AttrBuilder B(AS, Idx);
537 return get(Idx, Attribute::get(C, B));
538}
539
540//===----------------------------------------------------------------------===//
Bill Wendling18e72112012-12-19 22:42:22 +0000541// AttributeSetImpl Definition
Chris Lattner58d74912008-03-12 17:45:29 +0000542//===----------------------------------------------------------------------===//
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000543
Bill Wendling28d65722013-01-23 06:14:59 +0000544AttributeSet AttributeSet::getParamAttributes(unsigned Idx) const {
545 // FIXME: Remove.
546 return AttrList && hasAttributes(Idx) ?
547 AttributeSet::get(AttrList->getContext(),
548 AttributeWithIndex::get(Idx, getAttributes(Idx))) :
549 AttributeSet();
550}
551
Bill Wendling3fc4b962013-01-21 22:44:49 +0000552AttributeSet AttributeSet::getRetAttributes() const {
553 // FIXME: Remove.
554 return AttrList && hasAttributes(ReturnIndex) ?
555 AttributeSet::get(AttrList->getContext(),
556 AttributeWithIndex::get(ReturnIndex,
557 getAttributes(ReturnIndex))) :
558 AttributeSet();
559}
560
Bill Wendlingc5f1bc82013-01-21 21:57:28 +0000561AttributeSet AttributeSet::getFnAttributes() const {
562 // FIXME: Remove.
Bill Wendling3fc4b962013-01-21 22:44:49 +0000563 return AttrList && hasAttributes(FunctionIndex) ?
Bill Wendlingc5f1bc82013-01-21 21:57:28 +0000564 AttributeSet::get(AttrList->getContext(),
565 AttributeWithIndex::get(FunctionIndex,
566 getAttributes(FunctionIndex))) :
567 AttributeSet();
568}
569
Bill Wendling99faa3b2012-12-07 23:16:57 +0000570AttributeSet AttributeSet::get(LLVMContext &C,
Bill Wendling1ce47ac2012-12-12 19:21:53 +0000571 ArrayRef<AttributeWithIndex> Attrs) {
Devang Patel05988662008-09-25 21:00:45 +0000572 // If there are no attributes then return a null AttributesList pointer.
Chris Lattnerd509d0b2012-05-28 01:47:44 +0000573 if (Attrs.empty())
Bill Wendling99faa3b2012-12-07 23:16:57 +0000574 return AttributeSet();
Bill Wendling77898682012-10-16 06:01:44 +0000575
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000576#ifndef NDEBUG
Chris Lattnerd509d0b2012-05-28 01:47:44 +0000577 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
Bill Wendling77898682012-10-16 06:01:44 +0000578 assert(Attrs[i].Attrs.hasAttributes() &&
Devang Patel05988662008-09-25 21:00:45 +0000579 "Pointless attribute!");
Chris Lattner58d74912008-03-12 17:45:29 +0000580 assert((!i || Attrs[i-1].Index < Attrs[i].Index) &&
Devang Patel05988662008-09-25 21:00:45 +0000581 "Misordered AttributesList!");
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000582 }
583#endif
Bill Wendling77898682012-10-16 06:01:44 +0000584
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000585 // Otherwise, build a key to look up the existing attributes.
Bill Wendling0976e002012-11-20 05:09:20 +0000586 LLVMContextImpl *pImpl = C.pImpl;
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000587 FoldingSetNodeID ID;
Bill Wendling18e72112012-12-19 22:42:22 +0000588 AttributeSetImpl::Profile(ID, Attrs);
Bill Wendling77898682012-10-16 06:01:44 +0000589
Bill Wendling0976e002012-11-20 05:09:20 +0000590 void *InsertPoint;
Bill Wendling1bbd6442013-01-05 01:36:54 +0000591 AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
Bill Wendling77898682012-10-16 06:01:44 +0000592
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000593 // If we didn't find any existing attributes of the same shape then
594 // create a new one and insert it.
Bill Wendling0976e002012-11-20 05:09:20 +0000595 if (!PA) {
Bill Wendling5f93e2b2012-12-19 23:55:43 +0000596 PA = new AttributeSetImpl(C, Attrs);
Bill Wendling0976e002012-11-20 05:09:20 +0000597 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000598 }
Bill Wendling77898682012-10-16 06:01:44 +0000599
Devang Patel05988662008-09-25 21:00:45 +0000600 // Return the AttributesList that we found or created.
Bill Wendling99faa3b2012-12-07 23:16:57 +0000601 return AttributeSet(PA);
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000602}
603
Bill Wendling1bbd6442013-01-05 01:36:54 +0000604AttributeSet AttributeSet::get(LLVMContext &C, unsigned Idx, AttrBuilder &B) {
Bill Wendling3fc4b962013-01-21 22:44:49 +0000605 // FIXME: This should be implemented as a loop that creates the
606 // AttributeWithIndexes that then are used to create the AttributeSet.
607 if (!B.hasAttributes())
608 return AttributeSet();
Bill Wendlingdefaca02013-01-22 21:15:51 +0000609 return get(C, AttributeWithIndex::get(Idx, Attribute::get(C, B)));
Bill Wendling1bbd6442013-01-05 01:36:54 +0000610}
611
Bill Wendling28d65722013-01-23 06:14:59 +0000612AttributeSet AttributeSet::get(LLVMContext &C, unsigned Idx,
613 Attribute::AttrKind Kind) {
614 return get(C, AttributeWithIndex::get(Idx, Attribute::get(C, Kind)));
615}
616
Chris Lattner58d74912008-03-12 17:45:29 +0000617//===----------------------------------------------------------------------===//
Bill Wendling99faa3b2012-12-07 23:16:57 +0000618// AttributeSet Method Implementations
Chris Lattner58d74912008-03-12 17:45:29 +0000619//===----------------------------------------------------------------------===//
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000620
Bill Wendling99faa3b2012-12-07 23:16:57 +0000621const AttributeSet &AttributeSet::operator=(const AttributeSet &RHS) {
Bill Wendling0976e002012-11-20 05:09:20 +0000622 AttrList = RHS.AttrList;
623 return *this;
Chris Lattner58d74912008-03-12 17:45:29 +0000624}
625
Bill Wendling77898682012-10-16 06:01:44 +0000626/// getNumSlots - Return the number of slots used in this attribute list.
Chris Lattner58d74912008-03-12 17:45:29 +0000627/// This is the number of arguments that have an attribute set on them
628/// (including the function itself).
Bill Wendling99faa3b2012-12-07 23:16:57 +0000629unsigned AttributeSet::getNumSlots() const {
Bill Wendling60507d52013-01-04 20:54:35 +0000630 return AttrList ? AttrList->getNumAttributes() : 0;
Chris Lattner58d74912008-03-12 17:45:29 +0000631}
632
Devang Patel05988662008-09-25 21:00:45 +0000633/// getSlot - Return the AttributeWithIndex at the specified slot. This
634/// holds a number plus a set of attributes.
Bill Wendling99faa3b2012-12-07 23:16:57 +0000635const AttributeWithIndex &AttributeSet::getSlot(unsigned Slot) const {
Bill Wendling60507d52013-01-04 20:54:35 +0000636 assert(AttrList && Slot < AttrList->getNumAttributes() &&
637 "Slot # out of range!");
638 return AttrList->getAttributes()[Slot];
Chris Lattner58d74912008-03-12 17:45:29 +0000639}
640
Bill Wendling831737d2012-12-30 10:32:01 +0000641bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{
642 return getAttributes(Index).hasAttribute(Kind);
643}
644
645bool AttributeSet::hasAttributes(unsigned Index) const {
646 return getAttributes(Index).hasAttributes();
647}
648
649std::string AttributeSet::getAsString(unsigned Index) const {
650 return getAttributes(Index).getAsString();
651}
652
Bill Wendling956f1342013-01-18 21:11:39 +0000653unsigned AttributeSet::getParamAlignment(unsigned Idx) const {
654 return getAttributes(Idx).getAlignment();
655}
656
Bill Wendling831737d2012-12-30 10:32:01 +0000657unsigned AttributeSet::getStackAlignment(unsigned Index) const {
658 return getAttributes(Index).getStackAlignment();
659}
660
Bill Wendling1db9b692013-01-09 23:36:50 +0000661uint64_t AttributeSet::Raw(unsigned Index) const {
Bill Wendling831737d2012-12-30 10:32:01 +0000662 // FIXME: Remove this.
Bill Wendling1db9b692013-01-09 23:36:50 +0000663 return getAttributes(Index).Raw();
Bill Wendling831737d2012-12-30 10:32:01 +0000664}
665
Bill Wendling77898682012-10-16 06:01:44 +0000666/// getAttributes - The attributes for the specified index are returned.
Bill Wendling034b94b2012-12-19 07:18:57 +0000667Attribute AttributeSet::getAttributes(unsigned Idx) const {
668 if (AttrList == 0) return Attribute();
Bill Wendling77898682012-10-16 06:01:44 +0000669
Bill Wendling60507d52013-01-04 20:54:35 +0000670 ArrayRef<AttributeWithIndex> Attrs = AttrList->getAttributes();
Chris Lattner58d74912008-03-12 17:45:29 +0000671 for (unsigned i = 0, e = Attrs.size(); i != e && Attrs[i].Index <= Idx; ++i)
672 if (Attrs[i].Index == Idx)
673 return Attrs[i].Attrs;
Bill Wendlingef99fe82012-09-21 15:26:31 +0000674
Bill Wendling034b94b2012-12-19 07:18:57 +0000675 return Attribute();
Chris Lattner58d74912008-03-12 17:45:29 +0000676}
677
678/// hasAttrSomewhere - Return true if the specified attribute is set for at
679/// least one parameter or for the return value.
Bill Wendling629fb822012-12-22 00:37:52 +0000680bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr) const {
Devang Patel05988662008-09-25 21:00:45 +0000681 if (AttrList == 0) return false;
Bill Wendling7d2f2492012-10-10 07:36:45 +0000682
Bill Wendling60507d52013-01-04 20:54:35 +0000683 ArrayRef<AttributeWithIndex> Attrs = AttrList->getAttributes();
Chris Lattner58d74912008-03-12 17:45:29 +0000684 for (unsigned i = 0, e = Attrs.size(); i != e; ++i)
Bill Wendling7d2f2492012-10-10 07:36:45 +0000685 if (Attrs[i].Attrs.hasAttribute(Attr))
Chris Lattner58d74912008-03-12 17:45:29 +0000686 return true;
Bill Wendling0976e002012-11-20 05:09:20 +0000687
Chris Lattner58d74912008-03-12 17:45:29 +0000688 return false;
689}
690
Bill Wendlingdefaca02013-01-22 21:15:51 +0000691AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Idx,
692 Attribute::AttrKind Attr) const {
693 return addAttr(C, Idx, Attribute::get(C, Attr));
694}
695
Bill Wendlinge4e85f12013-01-22 00:53:12 +0000696AttributeSet AttributeSet::addAttributes(LLVMContext &C, unsigned Idx,
697 AttributeSet Attrs) const {
698 return addAttr(C, Idx, Attrs.getAttributes(Idx));
Bill Wendling956f1342013-01-18 21:11:39 +0000699}
700
Bill Wendling99faa3b2012-12-07 23:16:57 +0000701AttributeSet AttributeSet::addAttr(LLVMContext &C, unsigned Idx,
Bill Wendling9d30e722012-12-31 00:49:59 +0000702 Attribute Attrs) const {
Bill Wendling034b94b2012-12-19 07:18:57 +0000703 Attribute OldAttrs = getAttributes(Idx);
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000704#ifndef NDEBUG
705 // FIXME it is not obvious how this should work for alignment.
706 // For now, say we can't change a known alignment.
Bill Wendlingef99fe82012-09-21 15:26:31 +0000707 unsigned OldAlign = OldAttrs.getAlignment();
708 unsigned NewAlign = Attrs.getAlignment();
Anton Korobeynikov4aefd6b2008-02-20 12:07:57 +0000709 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000710 "Attempt to change alignment!");
711#endif
Bill Wendling77898682012-10-16 06:01:44 +0000712
Bill Wendling702cc912012-10-15 20:35:56 +0000713 AttrBuilder NewAttrs =
714 AttrBuilder(OldAttrs).addAttributes(Attrs);
715 if (NewAttrs == AttrBuilder(OldAttrs))
Chris Lattner58d74912008-03-12 17:45:29 +0000716 return *this;
Bill Wendling77898682012-10-16 06:01:44 +0000717
Devang Patel05988662008-09-25 21:00:45 +0000718 SmallVector<AttributeWithIndex, 8> NewAttrList;
719 if (AttrList == 0)
720 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Chris Lattner58d74912008-03-12 17:45:29 +0000721 else {
Bill Wendling60507d52013-01-04 20:54:35 +0000722 ArrayRef<AttributeWithIndex> OldAttrList = AttrList->getAttributes();
Chris Lattner58d74912008-03-12 17:45:29 +0000723 unsigned i = 0, e = OldAttrList.size();
724 // Copy attributes for arguments before this one.
725 for (; i != e && OldAttrList[i].Index < Idx; ++i)
726 NewAttrList.push_back(OldAttrList[i]);
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000727
Chris Lattner58d74912008-03-12 17:45:29 +0000728 // If there are attributes already at this index, merge them in.
729 if (i != e && OldAttrList[i].Index == Idx) {
Bill Wendlingc4167952012-10-14 07:35:59 +0000730 Attrs =
Bill Wendling034b94b2012-12-19 07:18:57 +0000731 Attribute::get(C, AttrBuilder(Attrs).
Bill Wendlingc4167952012-10-14 07:35:59 +0000732 addAttributes(OldAttrList[i].Attrs));
Chris Lattner58d74912008-03-12 17:45:29 +0000733 ++i;
734 }
Bill Wendling77898682012-10-16 06:01:44 +0000735
Devang Patel05988662008-09-25 21:00:45 +0000736 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Bill Wendling77898682012-10-16 06:01:44 +0000737
Chris Lattner58d74912008-03-12 17:45:29 +0000738 // Copy attributes for arguments after this one.
Bill Wendling77898682012-10-16 06:01:44 +0000739 NewAttrList.insert(NewAttrList.end(),
Chris Lattner58d74912008-03-12 17:45:29 +0000740 OldAttrList.begin()+i, OldAttrList.end());
741 }
Bill Wendling77898682012-10-16 06:01:44 +0000742
Bill Wendling0976e002012-11-20 05:09:20 +0000743 return get(C, NewAttrList);
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000744}
745
Bill Wendling8246df62013-01-23 00:45:55 +0000746AttributeSet AttributeSet::removeAttribute(LLVMContext &C, unsigned Idx,
747 Attribute::AttrKind Attr) const {
748 return removeAttr(C, Idx, Attribute::get(C, Attr));
749}
750
751AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Idx,
752 AttributeSet Attrs) const {
753 return removeAttr(C, Idx, Attrs.getAttributes(Idx));
754}
755
Bill Wendling99faa3b2012-12-07 23:16:57 +0000756AttributeSet AttributeSet::removeAttr(LLVMContext &C, unsigned Idx,
Bill Wendling9d30e722012-12-31 00:49:59 +0000757 Attribute Attrs) const {
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000758#ifndef NDEBUG
759 // FIXME it is not obvious how this should work for alignment.
760 // For now, say we can't pass in alignment, which no current use does.
Bill Wendling034b94b2012-12-19 07:18:57 +0000761 assert(!Attrs.hasAttribute(Attribute::Alignment) &&
Bill Wendling67658342012-10-09 07:45:08 +0000762 "Attempt to exclude alignment!");
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000763#endif
Bill Wendling99faa3b2012-12-07 23:16:57 +0000764 if (AttrList == 0) return AttributeSet();
Bill Wendling77898682012-10-16 06:01:44 +0000765
Bill Wendling034b94b2012-12-19 07:18:57 +0000766 Attribute OldAttrs = getAttributes(Idx);
Bill Wendling702cc912012-10-15 20:35:56 +0000767 AttrBuilder NewAttrs =
768 AttrBuilder(OldAttrs).removeAttributes(Attrs);
769 if (NewAttrs == AttrBuilder(OldAttrs))
Chris Lattner58d74912008-03-12 17:45:29 +0000770 return *this;
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000771
Devang Patel05988662008-09-25 21:00:45 +0000772 SmallVector<AttributeWithIndex, 8> NewAttrList;
Bill Wendling60507d52013-01-04 20:54:35 +0000773 ArrayRef<AttributeWithIndex> OldAttrList = AttrList->getAttributes();
Chris Lattner58d74912008-03-12 17:45:29 +0000774 unsigned i = 0, e = OldAttrList.size();
Bill Wendling77898682012-10-16 06:01:44 +0000775
Chris Lattner58d74912008-03-12 17:45:29 +0000776 // Copy attributes for arguments before this one.
777 for (; i != e && OldAttrList[i].Index < Idx; ++i)
778 NewAttrList.push_back(OldAttrList[i]);
Bill Wendling77898682012-10-16 06:01:44 +0000779
Chris Lattner58d74912008-03-12 17:45:29 +0000780 // If there are attributes already at this index, merge them in.
781 assert(OldAttrList[i].Index == Idx && "Attribute isn't set?");
Bill Wendling034b94b2012-12-19 07:18:57 +0000782 Attrs = Attribute::get(C, AttrBuilder(OldAttrList[i].Attrs).
Bill Wendling5886b7b2012-10-14 06:39:53 +0000783 removeAttributes(Attrs));
Chris Lattner58d74912008-03-12 17:45:29 +0000784 ++i;
Bill Wendling7be78482012-10-14 08:54:26 +0000785 if (Attrs.hasAttributes()) // If any attributes left for this param, add them.
Devang Patel05988662008-09-25 21:00:45 +0000786 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Bill Wendling77898682012-10-16 06:01:44 +0000787
Chris Lattner58d74912008-03-12 17:45:29 +0000788 // Copy attributes for arguments after this one.
Bill Wendling77898682012-10-16 06:01:44 +0000789 NewAttrList.insert(NewAttrList.end(),
Chris Lattner58d74912008-03-12 17:45:29 +0000790 OldAttrList.begin()+i, OldAttrList.end());
Bill Wendling77898682012-10-16 06:01:44 +0000791
Bill Wendling0976e002012-11-20 05:09:20 +0000792 return get(C, NewAttrList);
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000793}
794
Bill Wendling99faa3b2012-12-07 23:16:57 +0000795void AttributeSet::dump() const {
David Greeneef1894e2010-01-05 01:29:58 +0000796 dbgs() << "PAL[ ";
Chris Lattner58d74912008-03-12 17:45:29 +0000797 for (unsigned i = 0; i < getNumSlots(); ++i) {
Devang Patel05988662008-09-25 21:00:45 +0000798 const AttributeWithIndex &PAWI = getSlot(i);
Bill Wendling60507d52013-01-04 20:54:35 +0000799 dbgs() << "{ " << PAWI.Index << ", " << PAWI.Attrs.getAsString() << " } ";
Chris Lattner58d74912008-03-12 17:45:29 +0000800 }
Bill Wendling77898682012-10-16 06:01:44 +0000801
David Greeneef1894e2010-01-05 01:29:58 +0000802 dbgs() << "]\n";
Duncan Sandsad9a9e12008-01-06 18:27:01 +0000803}