blob: 94615da6d055066eed52bb4b5e2d123ffedbe9d1 [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 Wendlinga8ab5fc2013-01-23 23:00:05 +000076 return pImpl->getAlignment();
Bill Wendlinge66f3d32012-10-05 06:44:41 +000077}
78
79/// This returns the stack alignment field of an attribute as a byte alignment
80/// value.
Bill Wendling034b94b2012-12-19 07:18:57 +000081unsigned Attribute::getStackAlignment() const {
82 if (!hasAttribute(Attribute::StackAlignment))
Bill Wendling9ef99c92012-10-09 20:56:48 +000083 return 0;
Bill Wendlinga8ab5fc2013-01-23 23:00:05 +000084 return pImpl->getStackAlignment();
Bill Wendlinge66f3d32012-10-05 06:44:41 +000085}
86
Bill Wendling92e287f2012-12-31 11:51:54 +000087bool Attribute::operator==(AttrKind K) const {
Bill Wendling60507d52013-01-04 20:54:35 +000088 return pImpl && *pImpl == K;
Bill Wendling92e287f2012-12-31 11:51:54 +000089}
Bill Wendling92e287f2012-12-31 11:51:54 +000090bool Attribute::operator!=(AttrKind K) const {
Bill Wendling60507d52013-01-04 20:54:35 +000091 return !(*this == K);
Bill Wendling92e287f2012-12-31 11:51:54 +000092}
93
Bill Wendling1db9b692013-01-09 23:36:50 +000094uint64_t Attribute::Raw() const {
95 return pImpl ? pImpl->Raw() : 0;
Bill Wendlingb10c88f2012-10-07 08:55:05 +000096}
97
Bill Wendling034b94b2012-12-19 07:18:57 +000098Attribute Attribute::typeIncompatible(Type *Ty) {
Bill Wendling702cc912012-10-15 20:35:56 +000099 AttrBuilder Incompatible;
Bill Wendling77898682012-10-16 06:01:44 +0000100
Bill Wendling3a106e62012-10-09 19:01:18 +0000101 if (!Ty->isIntegerTy())
Bill Wendling034b94b2012-12-19 07:18:57 +0000102 // Attribute that only apply to integers.
103 Incompatible.addAttribute(Attribute::SExt)
104 .addAttribute(Attribute::ZExt);
Bill Wendling77898682012-10-16 06:01:44 +0000105
Bill Wendling3a106e62012-10-09 19:01:18 +0000106 if (!Ty->isPointerTy())
Bill Wendling034b94b2012-12-19 07:18:57 +0000107 // Attribute that only apply to pointers.
108 Incompatible.addAttribute(Attribute::ByVal)
109 .addAttribute(Attribute::Nest)
110 .addAttribute(Attribute::NoAlias)
111 .addAttribute(Attribute::NoCapture)
112 .addAttribute(Attribute::StructRet);
Bill Wendling77898682012-10-16 06:01:44 +0000113
Bill Wendling034b94b2012-12-19 07:18:57 +0000114 return Attribute::get(Ty->getContext(), Incompatible);
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000115}
116
Bill Wendling702cc912012-10-15 20:35:56 +0000117/// encodeLLVMAttributesForBitcode - This returns an integer containing an
118/// encoding of all the LLVM attributes found in the given attribute bitset.
119/// Any change to this encoding is a breaking change to bitcode compatibility.
Bill Wendling034b94b2012-12-19 07:18:57 +0000120uint64_t Attribute::encodeLLVMAttributesForBitcode(Attribute Attrs) {
Bill Wendling702cc912012-10-15 20:35:56 +0000121 // FIXME: It doesn't make sense to store the alignment information as an
122 // expanded out value, we should store it as a log2 value. However, we can't
123 // just change that here without breaking bitcode compatibility. If this ever
124 // becomes a problem in practice, we should introduce new tag numbers in the
125 // bitcode file and have those tags use a more efficiently encoded alignment
126 // field.
127
128 // Store the alignment in the bitcode as a 16-bit raw value instead of a 5-bit
129 // log2 encoded value. Shift the bits above the alignment up by 11 bits.
Bill Wendling1db9b692013-01-09 23:36:50 +0000130 uint64_t EncodedAttrs = Attrs.Raw() & 0xffff;
Bill Wendling034b94b2012-12-19 07:18:57 +0000131 if (Attrs.hasAttribute(Attribute::Alignment))
Bill Wendling702cc912012-10-15 20:35:56 +0000132 EncodedAttrs |= Attrs.getAlignment() << 16;
Bill Wendling1db9b692013-01-09 23:36:50 +0000133 EncodedAttrs |= (Attrs.Raw() & (0xffffULL << 21)) << 11;
Bill Wendling702cc912012-10-15 20:35:56 +0000134 return EncodedAttrs;
135}
136
137/// decodeLLVMAttributesForBitcode - This returns an attribute bitset containing
138/// the LLVM attributes that have been decoded from the given integer. This
139/// function must stay in sync with 'encodeLLVMAttributesForBitcode'.
Bill Wendling034b94b2012-12-19 07:18:57 +0000140Attribute Attribute::decodeLLVMAttributesForBitcode(LLVMContext &C,
Bill Wendlingdefaca02013-01-22 21:15:51 +0000141 uint64_t EncodedAttrs) {
Bill Wendling702cc912012-10-15 20:35:56 +0000142 // The alignment is stored as a 16-bit raw value from bits 31--16. We shift
143 // the bits above 31 down by 11 bits.
144 unsigned Alignment = (EncodedAttrs & (0xffffULL << 16)) >> 16;
145 assert((!Alignment || isPowerOf2_32(Alignment)) &&
146 "Alignment must be a power of two.");
147
148 AttrBuilder B(EncodedAttrs & 0xffff);
149 if (Alignment)
150 B.addAlignmentAttr(Alignment);
Nadav Roteme7439422012-10-22 17:33:31 +0000151 B.addRawValue((EncodedAttrs & (0xffffULL << 32)) >> 11);
Bill Wendling034b94b2012-12-19 07:18:57 +0000152 return Attribute::get(C, B);
Bill Wendling702cc912012-10-15 20:35:56 +0000153}
154
Bill Wendling034b94b2012-12-19 07:18:57 +0000155std::string Attribute::getAsString() const {
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000156 std::string Result;
Bill Wendling034b94b2012-12-19 07:18:57 +0000157 if (hasAttribute(Attribute::ZExt))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000158 Result += "zeroext ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000159 if (hasAttribute(Attribute::SExt))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000160 Result += "signext ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000161 if (hasAttribute(Attribute::NoReturn))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000162 Result += "noreturn ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000163 if (hasAttribute(Attribute::NoUnwind))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000164 Result += "nounwind ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000165 if (hasAttribute(Attribute::UWTable))
Rafael Espindolafc2bb8c2011-05-25 03:44:17 +0000166 Result += "uwtable ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000167 if (hasAttribute(Attribute::ReturnsTwice))
Rafael Espindola25456ef2011-10-03 14:45:37 +0000168 Result += "returns_twice ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000169 if (hasAttribute(Attribute::InReg))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000170 Result += "inreg ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000171 if (hasAttribute(Attribute::NoAlias))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000172 Result += "noalias ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000173 if (hasAttribute(Attribute::NoCapture))
Nick Lewycky73ddd4f2008-12-19 09:38:31 +0000174 Result += "nocapture ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000175 if (hasAttribute(Attribute::StructRet))
Anton Korobeynikovc5ec8a72009-07-17 18:07:26 +0000176 Result += "sret ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000177 if (hasAttribute(Attribute::ByVal))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000178 Result += "byval ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000179 if (hasAttribute(Attribute::Nest))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000180 Result += "nest ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000181 if (hasAttribute(Attribute::ReadNone))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000182 Result += "readnone ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000183 if (hasAttribute(Attribute::ReadOnly))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000184 Result += "readonly ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000185 if (hasAttribute(Attribute::OptimizeForSize))
Devang Patel19c87462008-09-26 22:53:05 +0000186 Result += "optsize ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000187 if (hasAttribute(Attribute::NoInline))
Devang Patel19c87462008-09-26 22:53:05 +0000188 Result += "noinline ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000189 if (hasAttribute(Attribute::InlineHint))
Jakob Stoklund Olesen570a4a52010-02-06 01:16:28 +0000190 Result += "inlinehint ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000191 if (hasAttribute(Attribute::AlwaysInline))
Devang Patel19c87462008-09-26 22:53:05 +0000192 Result += "alwaysinline ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000193 if (hasAttribute(Attribute::StackProtect))
Bill Wendlinge9e6bdf2008-11-13 01:02:14 +0000194 Result += "ssp ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000195 if (hasAttribute(Attribute::StackProtectReq))
Bill Wendlinge9e6bdf2008-11-13 01:02:14 +0000196 Result += "sspreq ";
Bill Wendling114baee2013-01-23 06:41:41 +0000197 if (hasAttribute(Attribute::StackProtectStrong))
198 Result += "sspstrong ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000199 if (hasAttribute(Attribute::NoRedZone))
Devang Pateld18e31a2009-06-04 22:05:33 +0000200 Result += "noredzone ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000201 if (hasAttribute(Attribute::NoImplicitFloat))
Devang Patel578efa92009-06-05 21:57:13 +0000202 Result += "noimplicitfloat ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000203 if (hasAttribute(Attribute::Naked))
Anton Korobeynikovc5ec8a72009-07-17 18:07:26 +0000204 Result += "naked ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000205 if (hasAttribute(Attribute::NonLazyBind))
John McCall3a3465b2011-06-15 20:36:13 +0000206 Result += "nonlazybind ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000207 if (hasAttribute(Attribute::AddressSafety))
Kostya Serebryany164b86b2012-01-20 17:56:17 +0000208 Result += "address_safety ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000209 if (hasAttribute(Attribute::MinSize))
Quentin Colombet9a419f62012-10-30 16:32:52 +0000210 Result += "minsize ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000211 if (hasAttribute(Attribute::StackAlignment)) {
Charles Davis1e063d12010-02-12 00:31:15 +0000212 Result += "alignstack(";
Bill Wendlingef99fe82012-09-21 15:26:31 +0000213 Result += utostr(getStackAlignment());
Charles Davis1e063d12010-02-12 00:31:15 +0000214 Result += ") ";
215 }
Bill Wendling034b94b2012-12-19 07:18:57 +0000216 if (hasAttribute(Attribute::Alignment)) {
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000217 Result += "align ";
Bill Wendlingef99fe82012-09-21 15:26:31 +0000218 Result += utostr(getAlignment());
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000219 Result += " ";
220 }
James Molloy67ae1352012-12-20 16:04:27 +0000221 if (hasAttribute(Attribute::NoDuplicate))
222 Result += "noduplicate ";
Dan Gohmanc3be0fd2008-08-05 15:51:44 +0000223 // Trim the trailing space.
Nick Lewycky73ddd4f2008-12-19 09:38:31 +0000224 assert(!Result.empty() && "Unknown attribute!");
Dan Gohmanc3be0fd2008-08-05 15:51:44 +0000225 Result.erase(Result.end()-1);
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000226 return Result;
227}
228
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000229//===----------------------------------------------------------------------===//
Bill Wendling03198882013-01-04 23:27:34 +0000230// AttrBuilder Method Implementations
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000231//===----------------------------------------------------------------------===//
232
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000233AttrBuilder::AttrBuilder(AttributeSet AS, unsigned Idx)
234 : Alignment(0), StackAlignment(0) {
235 AttributeSetImpl *pImpl = AS.AttrList;
236 if (!pImpl) return;
237
238 ArrayRef<AttributeWithIndex> AttrList = pImpl->getAttributes();
239 const AttributeWithIndex *AWI = 0;
240 for (unsigned I = 0, E = AttrList.size(); I != E; ++I)
241 if (AttrList[I].Index == Idx) {
242 AWI = &AttrList[I];
243 break;
244 }
245
Bill Wendlingaec71062013-01-18 21:56:07 +0000246 if (!AWI) return;
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000247
Bill Wendling956f1342013-01-18 21:11:39 +0000248 uint64_t Mask = AWI->Attrs.Raw();
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000249
Bill Wendling956f1342013-01-18 21:11:39 +0000250 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
251 I = Attribute::AttrKind(I + 1)) {
252 if (uint64_t A = (Mask & AttributeImpl::getAttrMask(I))) {
253 Attrs.insert(I);
254
255 if (I == Attribute::Alignment)
256 Alignment = 1ULL << ((A >> 16) - 1);
257 else if (I == Attribute::StackAlignment)
258 StackAlignment = 1ULL << ((A >> 26)-1);
259 }
260 }
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000261}
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);
Bill Wendlingdefaca02013-01-22 21:15:51 +0000311
Bill Wendling03198882013-01-04 23:27:34 +0000312 if (I == Attribute::Alignment)
313 Alignment = 1ULL << ((A >> 16) - 1);
314 else if (I == Attribute::StackAlignment)
315 StackAlignment = 1ULL << ((A >> 26)-1);
316 }
317 }
Bill Wendlingdefaca02013-01-22 21:15:51 +0000318
Bill Wendling3a106e62012-10-09 19:01:18 +0000319 return *this;
Bill Wendling2e879bc2012-10-09 09:11:20 +0000320}
321
Bill Wendlingdefaca02013-01-22 21:15:51 +0000322AttrBuilder &AttrBuilder::addAttributes(const Attribute &Attr) {
323 uint64_t Mask = Attr.Raw();
Bill Wendling03198882013-01-04 23:27:34 +0000324
325 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
Bill Wendlingdefaca02013-01-22 21:15:51 +0000326 I = Attribute::AttrKind(I + 1))
327 if ((Mask & AttributeImpl::getAttrMask(I)) != 0)
Bill Wendling03198882013-01-04 23:27:34 +0000328 Attrs.insert(I);
329
Bill Wendlingdefaca02013-01-22 21:15:51 +0000330 if (Attr.getAlignment())
331 Alignment = Attr.getAlignment();
332 if (Attr.getStackAlignment())
333 StackAlignment = Attr.getStackAlignment();
Bill Wendling432e6062012-10-14 07:17:34 +0000334 return *this;
335}
336
Bill Wendling034b94b2012-12-19 07:18:57 +0000337AttrBuilder &AttrBuilder::removeAttributes(const Attribute &A){
Bill Wendling1db9b692013-01-09 23:36:50 +0000338 uint64_t Mask = A.Raw();
Bill Wendling03198882013-01-04 23:27:34 +0000339
340 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
341 I = Attribute::AttrKind(I + 1)) {
342 if (Mask & AttributeImpl::getAttrMask(I)) {
343 Attrs.erase(I);
344
345 if (I == Attribute::Alignment)
346 Alignment = 0;
347 else if (I == Attribute::StackAlignment)
348 StackAlignment = 0;
349 }
350 }
351
Bill Wendling5886b7b2012-10-14 06:39:53 +0000352 return *this;
Bill Wendling8831c062012-10-09 00:01:21 +0000353}
354
Bill Wendling22bd6412013-01-03 01:54:39 +0000355bool AttrBuilder::contains(Attribute::AttrKind A) const {
Bill Wendling03198882013-01-04 23:27:34 +0000356 return Attrs.count(A);
Bill Wendling7d2f2492012-10-10 07:36:45 +0000357}
358
Bill Wendling702cc912012-10-15 20:35:56 +0000359bool AttrBuilder::hasAttributes() const {
Bill Wendling03198882013-01-04 23:27:34 +0000360 return !Attrs.empty();
Bill Wendlingf385f4c2012-10-08 23:27:46 +0000361}
Bill Wendling60507d52013-01-04 20:54:35 +0000362
Bill Wendling034b94b2012-12-19 07:18:57 +0000363bool AttrBuilder::hasAttributes(const Attribute &A) const {
Bill Wendling1db9b692013-01-09 23:36:50 +0000364 return Raw() & A.Raw();
Bill Wendling8831c062012-10-09 00:01:21 +0000365}
Bill Wendling60507d52013-01-04 20:54:35 +0000366
Bill Wendling702cc912012-10-15 20:35:56 +0000367bool AttrBuilder::hasAlignmentAttr() const {
Bill Wendling03198882013-01-04 23:27:34 +0000368 return Alignment != 0;
Bill Wendlingf385f4c2012-10-08 23:27:46 +0000369}
370
Bill Wendling1db9b692013-01-09 23:36:50 +0000371uint64_t AttrBuilder::Raw() const {
Bill Wendling03198882013-01-04 23:27:34 +0000372 uint64_t Mask = 0;
373
374 for (DenseSet<Attribute::AttrKind>::const_iterator I = Attrs.begin(),
375 E = Attrs.end(); I != E; ++I) {
376 Attribute::AttrKind Kind = *I;
377
378 if (Kind == Attribute::Alignment)
379 Mask |= (Log2_32(Alignment) + 1) << 16;
380 else if (Kind == Attribute::StackAlignment)
381 Mask |= (Log2_32(StackAlignment) + 1) << 26;
382 else
383 Mask |= AttributeImpl::getAttrMask(Kind);
384 }
385
386 return Mask;
Bill Wendlingf385f4c2012-10-08 23:27:46 +0000387}
388
Bill Wendling03198882013-01-04 23:27:34 +0000389bool AttrBuilder::operator==(const AttrBuilder &B) {
390 SmallVector<Attribute::AttrKind, 8> This(Attrs.begin(), Attrs.end());
391 SmallVector<Attribute::AttrKind, 8> That(B.Attrs.begin(), B.Attrs.end());
392 return This == That;
Bill Wendling7d2f2492012-10-10 07:36:45 +0000393}
394
Chris Lattner58d74912008-03-12 17:45:29 +0000395//===----------------------------------------------------------------------===//
Bill Wendling2c79ecb2012-09-26 21:07:29 +0000396// AttributeImpl Definition
397//===----------------------------------------------------------------------===//
398
Bill Wendling1bbd6442013-01-05 01:36:54 +0000399AttributeImpl::AttributeImpl(LLVMContext &C, uint64_t data)
400 : Context(C) {
Bill Wendling7c1683d2012-12-29 12:29:38 +0000401 Data = ConstantInt::get(Type::getInt64Ty(C), data);
402}
Bill Wendling1bbd6442013-01-05 01:36:54 +0000403AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind data)
404 : Context(C) {
Bill Wendling979aff62012-12-30 02:22:16 +0000405 Data = ConstantInt::get(Type::getInt64Ty(C), data);
406}
407AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind data,
Bill Wendling1bbd6442013-01-05 01:36:54 +0000408 ArrayRef<Constant*> values)
409 : Context(C) {
Bill Wendling979aff62012-12-30 02:22:16 +0000410 Data = ConstantInt::get(Type::getInt64Ty(C), data);
411 Vals.reserve(values.size());
412 Vals.append(values.begin(), values.end());
413}
Bill Wendling1bbd6442013-01-05 01:36:54 +0000414AttributeImpl::AttributeImpl(LLVMContext &C, StringRef data)
415 : Context(C) {
Bill Wendling979aff62012-12-30 02:22:16 +0000416 Data = ConstantDataArray::getString(C, data);
417}
Bill Wendling7c1683d2012-12-29 12:29:38 +0000418
Bill Wendling60507d52013-01-04 20:54:35 +0000419bool AttributeImpl::operator==(Attribute::AttrKind Kind) const {
Bill Wendling529ec712012-12-30 01:38:39 +0000420 if (ConstantInt *CI = dyn_cast<ConstantInt>(Data))
421 return CI->getZExtValue() == Kind;
422 return false;
423}
Bill Wendling60507d52013-01-04 20:54:35 +0000424bool AttributeImpl::operator!=(Attribute::AttrKind Kind) const {
425 return !(*this == Kind);
426}
Bill Wendling529ec712012-12-30 01:38:39 +0000427
Bill Wendling60507d52013-01-04 20:54:35 +0000428bool AttributeImpl::operator==(StringRef Kind) const {
Bill Wendling529ec712012-12-30 01:38:39 +0000429 if (ConstantDataArray *CDA = dyn_cast<ConstantDataArray>(Data))
430 if (CDA->isString())
431 return CDA->getAsString() == Kind;
432 return false;
433}
Bill Wendling60507d52013-01-04 20:54:35 +0000434bool AttributeImpl::operator!=(StringRef Kind) const {
435 return !(*this == Kind);
436}
Bill Wendling529ec712012-12-30 01:38:39 +0000437
Bill Wendling1db9b692013-01-09 23:36:50 +0000438uint64_t AttributeImpl::Raw() const {
Bill Wendling529ec712012-12-30 01:38:39 +0000439 // FIXME: Remove this.
Bill Wendling7c1683d2012-12-29 12:29:38 +0000440 return cast<ConstantInt>(Data)->getZExtValue();
441}
442
Bill Wendling60507d52013-01-04 20:54:35 +0000443uint64_t AttributeImpl::getAttrMask(Attribute::AttrKind Val) {
Bill Wendling67658342012-10-09 07:45:08 +0000444 switch (Val) {
Chandler Carruth6f78fbb2013-01-05 08:47:26 +0000445 case Attribute::EndAttrKinds:
446 case Attribute::AttrKindEmptyKey:
447 case Attribute::AttrKindTombstoneKey:
448 llvm_unreachable("Synthetic enumerators which should never get here");
449
Bill Wendling034b94b2012-12-19 07:18:57 +0000450 case Attribute::None: return 0;
451 case Attribute::ZExt: return 1 << 0;
452 case Attribute::SExt: return 1 << 1;
453 case Attribute::NoReturn: return 1 << 2;
454 case Attribute::InReg: return 1 << 3;
455 case Attribute::StructRet: return 1 << 4;
456 case Attribute::NoUnwind: return 1 << 5;
457 case Attribute::NoAlias: return 1 << 6;
458 case Attribute::ByVal: return 1 << 7;
459 case Attribute::Nest: return 1 << 8;
460 case Attribute::ReadNone: return 1 << 9;
461 case Attribute::ReadOnly: return 1 << 10;
462 case Attribute::NoInline: return 1 << 11;
463 case Attribute::AlwaysInline: return 1 << 12;
464 case Attribute::OptimizeForSize: return 1 << 13;
465 case Attribute::StackProtect: return 1 << 14;
466 case Attribute::StackProtectReq: return 1 << 15;
467 case Attribute::Alignment: return 31 << 16;
468 case Attribute::NoCapture: return 1 << 21;
469 case Attribute::NoRedZone: return 1 << 22;
470 case Attribute::NoImplicitFloat: return 1 << 23;
471 case Attribute::Naked: return 1 << 24;
472 case Attribute::InlineHint: return 1 << 25;
473 case Attribute::StackAlignment: return 7 << 26;
474 case Attribute::ReturnsTwice: return 1 << 29;
475 case Attribute::UWTable: return 1 << 30;
476 case Attribute::NonLazyBind: return 1U << 31;
477 case Attribute::AddressSafety: return 1ULL << 32;
478 case Attribute::MinSize: return 1ULL << 33;
James Molloy67ae1352012-12-20 16:04:27 +0000479 case Attribute::NoDuplicate: return 1ULL << 34;
Bill Wendling114baee2013-01-23 06:41:41 +0000480 case Attribute::StackProtectStrong: return 1ULL << 35;
Bill Wendling67658342012-10-09 07:45:08 +0000481 }
482 llvm_unreachable("Unsupported attribute type");
483}
484
Bill Wendling60507d52013-01-04 20:54:35 +0000485bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
Bill Wendling1db9b692013-01-09 23:36:50 +0000486 return (Raw() & getAttrMask(A)) != 0;
Bill Wendling8e635db2012-10-08 21:47:17 +0000487}
Bill Wendling2c79ecb2012-09-26 21:07:29 +0000488
Bill Wendlingf6670722012-12-20 01:36:59 +0000489bool AttributeImpl::hasAttributes() const {
Bill Wendling1db9b692013-01-09 23:36:50 +0000490 return Raw() != 0;
Bill Wendling8e635db2012-10-08 21:47:17 +0000491}
Bill Wendling2c79ecb2012-09-26 21:07:29 +0000492
Bill Wendlingf6670722012-12-20 01:36:59 +0000493uint64_t AttributeImpl::getAlignment() const {
Bill Wendlinga8ab5fc2013-01-23 23:00:05 +0000494 uint64_t Mask = Raw() & getAttrMask(Attribute::Alignment);
495 return 1U << ((Mask >> 16) - 1);
Bill Wendling8e635db2012-10-08 21:47:17 +0000496}
Bill Wendling2c79ecb2012-09-26 21:07:29 +0000497
Bill Wendlingf6670722012-12-20 01:36:59 +0000498uint64_t AttributeImpl::getStackAlignment() const {
Bill Wendlinga8ab5fc2013-01-23 23:00:05 +0000499 uint64_t Mask = Raw() & getAttrMask(Attribute::StackAlignment);
500 return 1U << ((Mask >> 26) - 1);
Bill Wendling8e635db2012-10-08 21:47:17 +0000501}
Bill Wendling2c79ecb2012-09-26 21:07:29 +0000502
Bill Wendling8456efb2013-01-09 00:32:55 +0000503void AttributeImpl::Profile(FoldingSetNodeID &ID, Constant *Data,
504 ArrayRef<Constant*> Vals) {
Bill Wendlingff887162013-01-09 00:32:08 +0000505 ID.AddInteger(cast<ConstantInt>(Data)->getZExtValue());
Bill Wendling1db9b692013-01-09 23:36:50 +0000506#if 0
507 // FIXME: Not yet supported.
Bill Wendlingff887162013-01-09 00:32:08 +0000508 for (ArrayRef<Constant*>::iterator I = Vals.begin(), E = Vals.end();
509 I != E; ++I)
510 ID.AddPointer(*I);
Bill Wendling1db9b692013-01-09 23:36:50 +0000511#endif
Bill Wendlingff887162013-01-09 00:32:08 +0000512}
513
Bill Wendling2c79ecb2012-09-26 21:07:29 +0000514//===----------------------------------------------------------------------===//
Bill Wendlingc5f1bc82013-01-21 21:57:28 +0000515// AttributeWithIndex Definition
516//===----------------------------------------------------------------------===//
517
518AttributeWithIndex AttributeWithIndex::get(LLVMContext &C, unsigned Idx,
519 AttributeSet AS) {
520 // FIXME: This is temporary, but necessary for the conversion.
521 AttrBuilder B(AS, Idx);
522 return get(Idx, Attribute::get(C, B));
523}
524
525//===----------------------------------------------------------------------===//
Bill Wendling18e72112012-12-19 22:42:22 +0000526// AttributeSetImpl Definition
Chris Lattner58d74912008-03-12 17:45:29 +0000527//===----------------------------------------------------------------------===//
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000528
Bill Wendling28d65722013-01-23 06:14:59 +0000529AttributeSet AttributeSet::getParamAttributes(unsigned Idx) const {
530 // FIXME: Remove.
531 return AttrList && hasAttributes(Idx) ?
532 AttributeSet::get(AttrList->getContext(),
533 AttributeWithIndex::get(Idx, getAttributes(Idx))) :
534 AttributeSet();
535}
536
Bill Wendling3fc4b962013-01-21 22:44:49 +0000537AttributeSet AttributeSet::getRetAttributes() const {
538 // FIXME: Remove.
539 return AttrList && hasAttributes(ReturnIndex) ?
540 AttributeSet::get(AttrList->getContext(),
541 AttributeWithIndex::get(ReturnIndex,
542 getAttributes(ReturnIndex))) :
543 AttributeSet();
544}
545
Bill Wendlingc5f1bc82013-01-21 21:57:28 +0000546AttributeSet AttributeSet::getFnAttributes() const {
547 // FIXME: Remove.
Bill Wendling3fc4b962013-01-21 22:44:49 +0000548 return AttrList && hasAttributes(FunctionIndex) ?
Bill Wendlingc5f1bc82013-01-21 21:57:28 +0000549 AttributeSet::get(AttrList->getContext(),
550 AttributeWithIndex::get(FunctionIndex,
551 getAttributes(FunctionIndex))) :
552 AttributeSet();
553}
554
Bill Wendling99faa3b2012-12-07 23:16:57 +0000555AttributeSet AttributeSet::get(LLVMContext &C,
Bill Wendling1ce47ac2012-12-12 19:21:53 +0000556 ArrayRef<AttributeWithIndex> Attrs) {
Devang Patel05988662008-09-25 21:00:45 +0000557 // If there are no attributes then return a null AttributesList pointer.
Chris Lattnerd509d0b2012-05-28 01:47:44 +0000558 if (Attrs.empty())
Bill Wendling99faa3b2012-12-07 23:16:57 +0000559 return AttributeSet();
Bill Wendling77898682012-10-16 06:01:44 +0000560
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000561#ifndef NDEBUG
Chris Lattnerd509d0b2012-05-28 01:47:44 +0000562 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
Bill Wendling77898682012-10-16 06:01:44 +0000563 assert(Attrs[i].Attrs.hasAttributes() &&
Devang Patel05988662008-09-25 21:00:45 +0000564 "Pointless attribute!");
Chris Lattner58d74912008-03-12 17:45:29 +0000565 assert((!i || Attrs[i-1].Index < Attrs[i].Index) &&
Devang Patel05988662008-09-25 21:00:45 +0000566 "Misordered AttributesList!");
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000567 }
568#endif
Bill Wendling77898682012-10-16 06:01:44 +0000569
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000570 // Otherwise, build a key to look up the existing attributes.
Bill Wendling0976e002012-11-20 05:09:20 +0000571 LLVMContextImpl *pImpl = C.pImpl;
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000572 FoldingSetNodeID ID;
Bill Wendling18e72112012-12-19 22:42:22 +0000573 AttributeSetImpl::Profile(ID, Attrs);
Bill Wendling77898682012-10-16 06:01:44 +0000574
Bill Wendling0976e002012-11-20 05:09:20 +0000575 void *InsertPoint;
Bill Wendling1bbd6442013-01-05 01:36:54 +0000576 AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
Bill Wendling77898682012-10-16 06:01:44 +0000577
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000578 // If we didn't find any existing attributes of the same shape then
579 // create a new one and insert it.
Bill Wendling0976e002012-11-20 05:09:20 +0000580 if (!PA) {
Bill Wendling5f93e2b2012-12-19 23:55:43 +0000581 PA = new AttributeSetImpl(C, Attrs);
Bill Wendling0976e002012-11-20 05:09:20 +0000582 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000583 }
Bill Wendling77898682012-10-16 06:01:44 +0000584
Devang Patel05988662008-09-25 21:00:45 +0000585 // Return the AttributesList that we found or created.
Bill Wendling99faa3b2012-12-07 23:16:57 +0000586 return AttributeSet(PA);
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000587}
588
Bill Wendling1bbd6442013-01-05 01:36:54 +0000589AttributeSet AttributeSet::get(LLVMContext &C, unsigned Idx, AttrBuilder &B) {
Bill Wendling3fc4b962013-01-21 22:44:49 +0000590 // FIXME: This should be implemented as a loop that creates the
591 // AttributeWithIndexes that then are used to create the AttributeSet.
592 if (!B.hasAttributes())
593 return AttributeSet();
Bill Wendlingdefaca02013-01-22 21:15:51 +0000594 return get(C, AttributeWithIndex::get(Idx, Attribute::get(C, B)));
Bill Wendling1bbd6442013-01-05 01:36:54 +0000595}
596
Bill Wendling28d65722013-01-23 06:14:59 +0000597AttributeSet AttributeSet::get(LLVMContext &C, unsigned Idx,
598 Attribute::AttrKind Kind) {
599 return get(C, AttributeWithIndex::get(Idx, Attribute::get(C, Kind)));
600}
601
Chris Lattner58d74912008-03-12 17:45:29 +0000602//===----------------------------------------------------------------------===//
Bill Wendling99faa3b2012-12-07 23:16:57 +0000603// AttributeSet Method Implementations
Chris Lattner58d74912008-03-12 17:45:29 +0000604//===----------------------------------------------------------------------===//
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000605
Bill Wendling99faa3b2012-12-07 23:16:57 +0000606const AttributeSet &AttributeSet::operator=(const AttributeSet &RHS) {
Bill Wendling0976e002012-11-20 05:09:20 +0000607 AttrList = RHS.AttrList;
608 return *this;
Chris Lattner58d74912008-03-12 17:45:29 +0000609}
610
Bill Wendling77898682012-10-16 06:01:44 +0000611/// getNumSlots - Return the number of slots used in this attribute list.
Chris Lattner58d74912008-03-12 17:45:29 +0000612/// This is the number of arguments that have an attribute set on them
613/// (including the function itself).
Bill Wendling99faa3b2012-12-07 23:16:57 +0000614unsigned AttributeSet::getNumSlots() const {
Bill Wendling60507d52013-01-04 20:54:35 +0000615 return AttrList ? AttrList->getNumAttributes() : 0;
Chris Lattner58d74912008-03-12 17:45:29 +0000616}
617
Devang Patel05988662008-09-25 21:00:45 +0000618/// getSlot - Return the AttributeWithIndex at the specified slot. This
619/// holds a number plus a set of attributes.
Bill Wendling99faa3b2012-12-07 23:16:57 +0000620const AttributeWithIndex &AttributeSet::getSlot(unsigned Slot) const {
Bill Wendling60507d52013-01-04 20:54:35 +0000621 assert(AttrList && Slot < AttrList->getNumAttributes() &&
622 "Slot # out of range!");
623 return AttrList->getAttributes()[Slot];
Chris Lattner58d74912008-03-12 17:45:29 +0000624}
625
Bill Wendling831737d2012-12-30 10:32:01 +0000626bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{
627 return getAttributes(Index).hasAttribute(Kind);
628}
629
630bool AttributeSet::hasAttributes(unsigned Index) const {
631 return getAttributes(Index).hasAttributes();
632}
633
634std::string AttributeSet::getAsString(unsigned Index) const {
635 return getAttributes(Index).getAsString();
636}
637
Bill Wendling956f1342013-01-18 21:11:39 +0000638unsigned AttributeSet::getParamAlignment(unsigned Idx) const {
639 return getAttributes(Idx).getAlignment();
640}
641
Bill Wendling831737d2012-12-30 10:32:01 +0000642unsigned AttributeSet::getStackAlignment(unsigned Index) const {
643 return getAttributes(Index).getStackAlignment();
644}
645
Bill Wendling1db9b692013-01-09 23:36:50 +0000646uint64_t AttributeSet::Raw(unsigned Index) const {
Bill Wendling831737d2012-12-30 10:32:01 +0000647 // FIXME: Remove this.
Bill Wendling1db9b692013-01-09 23:36:50 +0000648 return getAttributes(Index).Raw();
Bill Wendling831737d2012-12-30 10:32:01 +0000649}
650
Bill Wendling77898682012-10-16 06:01:44 +0000651/// getAttributes - The attributes for the specified index are returned.
Bill Wendling034b94b2012-12-19 07:18:57 +0000652Attribute AttributeSet::getAttributes(unsigned Idx) const {
653 if (AttrList == 0) return Attribute();
Bill Wendling77898682012-10-16 06:01:44 +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 && Attrs[i].Index <= Idx; ++i)
657 if (Attrs[i].Index == Idx)
658 return Attrs[i].Attrs;
Bill Wendlingef99fe82012-09-21 15:26:31 +0000659
Bill Wendling034b94b2012-12-19 07:18:57 +0000660 return Attribute();
Chris Lattner58d74912008-03-12 17:45:29 +0000661}
662
663/// hasAttrSomewhere - Return true if the specified attribute is set for at
664/// least one parameter or for the return value.
Bill Wendling629fb822012-12-22 00:37:52 +0000665bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr) const {
Devang Patel05988662008-09-25 21:00:45 +0000666 if (AttrList == 0) return false;
Bill Wendling7d2f2492012-10-10 07:36:45 +0000667
Bill Wendling60507d52013-01-04 20:54:35 +0000668 ArrayRef<AttributeWithIndex> Attrs = AttrList->getAttributes();
Chris Lattner58d74912008-03-12 17:45:29 +0000669 for (unsigned i = 0, e = Attrs.size(); i != e; ++i)
Bill Wendling7d2f2492012-10-10 07:36:45 +0000670 if (Attrs[i].Attrs.hasAttribute(Attr))
Chris Lattner58d74912008-03-12 17:45:29 +0000671 return true;
Bill Wendling0976e002012-11-20 05:09:20 +0000672
Chris Lattner58d74912008-03-12 17:45:29 +0000673 return false;
674}
675
Bill Wendlingdefaca02013-01-22 21:15:51 +0000676AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Idx,
677 Attribute::AttrKind Attr) const {
678 return addAttr(C, Idx, Attribute::get(C, Attr));
679}
680
Bill Wendlinge4e85f12013-01-22 00:53:12 +0000681AttributeSet AttributeSet::addAttributes(LLVMContext &C, unsigned Idx,
682 AttributeSet Attrs) const {
683 return addAttr(C, Idx, Attrs.getAttributes(Idx));
Bill Wendling956f1342013-01-18 21:11:39 +0000684}
685
Bill Wendling99faa3b2012-12-07 23:16:57 +0000686AttributeSet AttributeSet::addAttr(LLVMContext &C, unsigned Idx,
Bill Wendling9d30e722012-12-31 00:49:59 +0000687 Attribute Attrs) const {
Bill Wendling034b94b2012-12-19 07:18:57 +0000688 Attribute OldAttrs = getAttributes(Idx);
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000689#ifndef NDEBUG
690 // FIXME it is not obvious how this should work for alignment.
691 // For now, say we can't change a known alignment.
Bill Wendlingef99fe82012-09-21 15:26:31 +0000692 unsigned OldAlign = OldAttrs.getAlignment();
693 unsigned NewAlign = Attrs.getAlignment();
Anton Korobeynikov4aefd6b2008-02-20 12:07:57 +0000694 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000695 "Attempt to change alignment!");
696#endif
Bill Wendling77898682012-10-16 06:01:44 +0000697
Bill Wendling702cc912012-10-15 20:35:56 +0000698 AttrBuilder NewAttrs =
699 AttrBuilder(OldAttrs).addAttributes(Attrs);
700 if (NewAttrs == AttrBuilder(OldAttrs))
Chris Lattner58d74912008-03-12 17:45:29 +0000701 return *this;
Bill Wendling77898682012-10-16 06:01:44 +0000702
Devang Patel05988662008-09-25 21:00:45 +0000703 SmallVector<AttributeWithIndex, 8> NewAttrList;
704 if (AttrList == 0)
705 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Chris Lattner58d74912008-03-12 17:45:29 +0000706 else {
Bill Wendling60507d52013-01-04 20:54:35 +0000707 ArrayRef<AttributeWithIndex> OldAttrList = AttrList->getAttributes();
Chris Lattner58d74912008-03-12 17:45:29 +0000708 unsigned i = 0, e = OldAttrList.size();
709 // Copy attributes for arguments before this one.
710 for (; i != e && OldAttrList[i].Index < Idx; ++i)
711 NewAttrList.push_back(OldAttrList[i]);
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000712
Chris Lattner58d74912008-03-12 17:45:29 +0000713 // If there are attributes already at this index, merge them in.
714 if (i != e && OldAttrList[i].Index == Idx) {
Bill Wendlingc4167952012-10-14 07:35:59 +0000715 Attrs =
Bill Wendling034b94b2012-12-19 07:18:57 +0000716 Attribute::get(C, AttrBuilder(Attrs).
Bill Wendlingc4167952012-10-14 07:35:59 +0000717 addAttributes(OldAttrList[i].Attrs));
Chris Lattner58d74912008-03-12 17:45:29 +0000718 ++i;
719 }
Bill Wendling77898682012-10-16 06:01:44 +0000720
Devang Patel05988662008-09-25 21:00:45 +0000721 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Bill Wendling77898682012-10-16 06:01:44 +0000722
Chris Lattner58d74912008-03-12 17:45:29 +0000723 // Copy attributes for arguments after this one.
Bill Wendling77898682012-10-16 06:01:44 +0000724 NewAttrList.insert(NewAttrList.end(),
Chris Lattner58d74912008-03-12 17:45:29 +0000725 OldAttrList.begin()+i, OldAttrList.end());
726 }
Bill Wendling77898682012-10-16 06:01:44 +0000727
Bill Wendling0976e002012-11-20 05:09:20 +0000728 return get(C, NewAttrList);
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000729}
730
Bill Wendling8246df62013-01-23 00:45:55 +0000731AttributeSet AttributeSet::removeAttribute(LLVMContext &C, unsigned Idx,
732 Attribute::AttrKind Attr) const {
733 return removeAttr(C, Idx, Attribute::get(C, Attr));
734}
735
736AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Idx,
737 AttributeSet Attrs) const {
738 return removeAttr(C, Idx, Attrs.getAttributes(Idx));
739}
740
Bill Wendling99faa3b2012-12-07 23:16:57 +0000741AttributeSet AttributeSet::removeAttr(LLVMContext &C, unsigned Idx,
Bill Wendling9d30e722012-12-31 00:49:59 +0000742 Attribute Attrs) const {
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000743#ifndef NDEBUG
744 // FIXME it is not obvious how this should work for alignment.
745 // For now, say we can't pass in alignment, which no current use does.
Bill Wendling034b94b2012-12-19 07:18:57 +0000746 assert(!Attrs.hasAttribute(Attribute::Alignment) &&
Bill Wendling67658342012-10-09 07:45:08 +0000747 "Attempt to exclude alignment!");
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000748#endif
Bill Wendling99faa3b2012-12-07 23:16:57 +0000749 if (AttrList == 0) return AttributeSet();
Bill Wendling77898682012-10-16 06:01:44 +0000750
Bill Wendling034b94b2012-12-19 07:18:57 +0000751 Attribute OldAttrs = getAttributes(Idx);
Bill Wendling702cc912012-10-15 20:35:56 +0000752 AttrBuilder NewAttrs =
753 AttrBuilder(OldAttrs).removeAttributes(Attrs);
754 if (NewAttrs == AttrBuilder(OldAttrs))
Chris Lattner58d74912008-03-12 17:45:29 +0000755 return *this;
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000756
Devang Patel05988662008-09-25 21:00:45 +0000757 SmallVector<AttributeWithIndex, 8> NewAttrList;
Bill Wendling60507d52013-01-04 20:54:35 +0000758 ArrayRef<AttributeWithIndex> OldAttrList = AttrList->getAttributes();
Chris Lattner58d74912008-03-12 17:45:29 +0000759 unsigned i = 0, e = OldAttrList.size();
Bill Wendling77898682012-10-16 06:01:44 +0000760
Chris Lattner58d74912008-03-12 17:45:29 +0000761 // Copy attributes for arguments before this one.
762 for (; i != e && OldAttrList[i].Index < Idx; ++i)
763 NewAttrList.push_back(OldAttrList[i]);
Bill Wendling77898682012-10-16 06:01:44 +0000764
Chris Lattner58d74912008-03-12 17:45:29 +0000765 // If there are attributes already at this index, merge them in.
766 assert(OldAttrList[i].Index == Idx && "Attribute isn't set?");
Bill Wendling034b94b2012-12-19 07:18:57 +0000767 Attrs = Attribute::get(C, AttrBuilder(OldAttrList[i].Attrs).
Bill Wendling5886b7b2012-10-14 06:39:53 +0000768 removeAttributes(Attrs));
Chris Lattner58d74912008-03-12 17:45:29 +0000769 ++i;
Bill Wendling7be78482012-10-14 08:54:26 +0000770 if (Attrs.hasAttributes()) // If any attributes left for this param, add them.
Devang Patel05988662008-09-25 21:00:45 +0000771 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Bill Wendling77898682012-10-16 06:01:44 +0000772
Chris Lattner58d74912008-03-12 17:45:29 +0000773 // Copy attributes for arguments after this one.
Bill Wendling77898682012-10-16 06:01:44 +0000774 NewAttrList.insert(NewAttrList.end(),
Chris Lattner58d74912008-03-12 17:45:29 +0000775 OldAttrList.begin()+i, OldAttrList.end());
Bill Wendling77898682012-10-16 06:01:44 +0000776
Bill Wendling0976e002012-11-20 05:09:20 +0000777 return get(C, NewAttrList);
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000778}
779
Bill Wendling99faa3b2012-12-07 23:16:57 +0000780void AttributeSet::dump() const {
David Greeneef1894e2010-01-05 01:29:58 +0000781 dbgs() << "PAL[ ";
Chris Lattner58d74912008-03-12 17:45:29 +0000782 for (unsigned i = 0; i < getNumSlots(); ++i) {
Devang Patel05988662008-09-25 21:00:45 +0000783 const AttributeWithIndex &PAWI = getSlot(i);
Bill Wendling60507d52013-01-04 20:54:35 +0000784 dbgs() << "{ " << PAWI.Index << ", " << PAWI.Attrs.getAsString() << " } ";
Chris Lattner58d74912008-03-12 17:45:29 +0000785 }
Bill Wendling77898682012-10-16 06:01:44 +0000786
David Greeneef1894e2010-01-05 01:29:58 +0000787 dbgs() << "]\n";
Duncan Sandsad9a9e12008-01-06 18:27:01 +0000788}