blob: 8623b98b9f6c032faec9e9cb25bbdb3ba77f8673 [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"
Bill Wendling3467e302013-01-24 00:06:56 +000026#include <algorithm>
Chris Lattner50ee9dd2008-01-02 23:42:30 +000027using namespace llvm;
28
Chris Lattner58d74912008-03-12 17:45:29 +000029//===----------------------------------------------------------------------===//
Bill Wendling034b94b2012-12-19 07:18:57 +000030// Attribute Implementation
Chris Lattner58d74912008-03-12 17:45:29 +000031//===----------------------------------------------------------------------===//
Chris Lattnerfabfde32008-01-03 00:10:22 +000032
Bill Wendling629fb822012-12-22 00:37:52 +000033Attribute Attribute::get(LLVMContext &Context, ArrayRef<AttrKind> Vals) {
Bill Wendling702cc912012-10-15 20:35:56 +000034 AttrBuilder B;
Bill Wendling629fb822012-12-22 00:37:52 +000035 for (ArrayRef<AttrKind>::iterator I = Vals.begin(), E = Vals.end();
Bill Wendlingcb3de0b2012-10-15 04:46:55 +000036 I != E; ++I)
37 B.addAttribute(*I);
Bill Wendling034b94b2012-12-19 07:18:57 +000038 return Attribute::get(Context, B);
Bill Wendlingcb3de0b2012-10-15 04:46:55 +000039}
Bill Wendling8e635db2012-10-08 21:47:17 +000040
Bill Wendling034b94b2012-12-19 07:18:57 +000041Attribute Attribute::get(LLVMContext &Context, AttrBuilder &B) {
42 // If there are no attributes, return an empty Attribute class.
Bill Wendlinga5c699d2012-10-16 05:55:09 +000043 if (!B.hasAttributes())
Bill Wendling034b94b2012-12-19 07:18:57 +000044 return Attribute();
Bill Wendling8e635db2012-10-08 21:47:17 +000045
46 // Otherwise, build a key to look up the existing attributes.
47 LLVMContextImpl *pImpl = Context.pImpl;
48 FoldingSetNodeID ID;
Bill Wendling1db9b692013-01-09 23:36:50 +000049 ID.AddInteger(B.Raw());
Bill Wendling8e635db2012-10-08 21:47:17 +000050
51 void *InsertPoint;
Bill Wendlingf6670722012-12-20 01:36:59 +000052 AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
Bill Wendling8e635db2012-10-08 21:47:17 +000053
54 if (!PA) {
55 // If we didn't find any existing attributes of the same shape then create a
56 // new one and insert it.
Bill Wendling1db9b692013-01-09 23:36:50 +000057 PA = new AttributeImpl(Context, B.Raw());
Bill Wendling8e635db2012-10-08 21:47:17 +000058 pImpl->AttrsSet.InsertNode(PA, InsertPoint);
59 }
60
61 // Return the AttributesList that we found or created.
Bill Wendling034b94b2012-12-19 07:18:57 +000062 return Attribute(PA);
Bill Wendling8e635db2012-10-08 21:47:17 +000063}
64
Bill Wendling629fb822012-12-22 00:37:52 +000065bool Attribute::hasAttribute(AttrKind Val) const {
Bill Wendling27107f62012-12-20 21:28:43 +000066 return pImpl && pImpl->hasAttribute(Val);
Bill Wendlinge66f3d32012-10-05 06:44:41 +000067}
68
Bill Wendling034b94b2012-12-19 07:18:57 +000069bool Attribute::hasAttributes() const {
Bill Wendling27107f62012-12-20 21:28:43 +000070 return pImpl && pImpl->hasAttributes();
Bill Wendling05cc40d2012-10-15 05:40:12 +000071}
72
Bill Wendlinge66f3d32012-10-05 06:44:41 +000073/// This returns the alignment field of an attribute as a byte alignment value.
Bill Wendling034b94b2012-12-19 07:18:57 +000074unsigned Attribute::getAlignment() const {
75 if (!hasAttribute(Attribute::Alignment))
Bill Wendling9ef99c92012-10-09 20:56:48 +000076 return 0;
Bill Wendlinga8ab5fc2013-01-23 23:00:05 +000077 return pImpl->getAlignment();
Bill Wendlinge66f3d32012-10-05 06:44:41 +000078}
79
80/// This returns the stack alignment field of an attribute as a byte alignment
81/// value.
Bill Wendling034b94b2012-12-19 07:18:57 +000082unsigned Attribute::getStackAlignment() const {
83 if (!hasAttribute(Attribute::StackAlignment))
Bill Wendling9ef99c92012-10-09 20:56:48 +000084 return 0;
Bill Wendlinga8ab5fc2013-01-23 23:00:05 +000085 return pImpl->getStackAlignment();
Bill Wendlinge66f3d32012-10-05 06:44:41 +000086}
87
Bill Wendling92e287f2012-12-31 11:51:54 +000088bool Attribute::operator==(AttrKind K) const {
Bill Wendling60507d52013-01-04 20:54:35 +000089 return pImpl && *pImpl == K;
Bill Wendling92e287f2012-12-31 11:51:54 +000090}
Bill Wendling92e287f2012-12-31 11:51:54 +000091bool Attribute::operator!=(AttrKind K) const {
Bill Wendling60507d52013-01-04 20:54:35 +000092 return !(*this == K);
Bill Wendling92e287f2012-12-31 11:51:54 +000093}
94
Bill Wendling3467e302013-01-24 00:06:56 +000095bool Attribute::operator<(Attribute A) const {
96 if (!pImpl && !A.pImpl) return false;
97 if (!pImpl) return true;
98 if (!A.pImpl) return false;
99 return *pImpl < *A.pImpl;
100}
101
102
103void Attribute::Profile(FoldingSetNodeID &ID) const {
104 ID.AddPointer(pImpl);
105}
106
Bill Wendling1db9b692013-01-09 23:36:50 +0000107uint64_t Attribute::Raw() const {
108 return pImpl ? pImpl->Raw() : 0;
Bill Wendlingb10c88f2012-10-07 08:55:05 +0000109}
110
Bill Wendling034b94b2012-12-19 07:18:57 +0000111Attribute Attribute::typeIncompatible(Type *Ty) {
Bill Wendling702cc912012-10-15 20:35:56 +0000112 AttrBuilder Incompatible;
Bill Wendling77898682012-10-16 06:01:44 +0000113
Bill Wendling3a106e62012-10-09 19:01:18 +0000114 if (!Ty->isIntegerTy())
Bill Wendling034b94b2012-12-19 07:18:57 +0000115 // Attribute that only apply to integers.
116 Incompatible.addAttribute(Attribute::SExt)
117 .addAttribute(Attribute::ZExt);
Bill Wendling77898682012-10-16 06:01:44 +0000118
Bill Wendling3a106e62012-10-09 19:01:18 +0000119 if (!Ty->isPointerTy())
Bill Wendling034b94b2012-12-19 07:18:57 +0000120 // Attribute that only apply to pointers.
121 Incompatible.addAttribute(Attribute::ByVal)
122 .addAttribute(Attribute::Nest)
123 .addAttribute(Attribute::NoAlias)
124 .addAttribute(Attribute::NoCapture)
125 .addAttribute(Attribute::StructRet);
Bill Wendling77898682012-10-16 06:01:44 +0000126
Bill Wendling034b94b2012-12-19 07:18:57 +0000127 return Attribute::get(Ty->getContext(), Incompatible);
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000128}
129
Bill Wendling702cc912012-10-15 20:35:56 +0000130/// encodeLLVMAttributesForBitcode - This returns an integer containing an
131/// encoding of all the LLVM attributes found in the given attribute bitset.
132/// Any change to this encoding is a breaking change to bitcode compatibility.
Bill Wendling034b94b2012-12-19 07:18:57 +0000133uint64_t Attribute::encodeLLVMAttributesForBitcode(Attribute Attrs) {
Bill Wendling702cc912012-10-15 20:35:56 +0000134 // FIXME: It doesn't make sense to store the alignment information as an
135 // expanded out value, we should store it as a log2 value. However, we can't
136 // just change that here without breaking bitcode compatibility. If this ever
137 // becomes a problem in practice, we should introduce new tag numbers in the
138 // bitcode file and have those tags use a more efficiently encoded alignment
139 // field.
140
141 // Store the alignment in the bitcode as a 16-bit raw value instead of a 5-bit
142 // log2 encoded value. Shift the bits above the alignment up by 11 bits.
Bill Wendling1db9b692013-01-09 23:36:50 +0000143 uint64_t EncodedAttrs = Attrs.Raw() & 0xffff;
Bill Wendling034b94b2012-12-19 07:18:57 +0000144 if (Attrs.hasAttribute(Attribute::Alignment))
Bill Wendling702cc912012-10-15 20:35:56 +0000145 EncodedAttrs |= Attrs.getAlignment() << 16;
Bill Wendling1db9b692013-01-09 23:36:50 +0000146 EncodedAttrs |= (Attrs.Raw() & (0xffffULL << 21)) << 11;
Bill Wendling702cc912012-10-15 20:35:56 +0000147 return EncodedAttrs;
148}
149
150/// decodeLLVMAttributesForBitcode - This returns an attribute bitset containing
151/// the LLVM attributes that have been decoded from the given integer. This
152/// function must stay in sync with 'encodeLLVMAttributesForBitcode'.
Bill Wendling034b94b2012-12-19 07:18:57 +0000153Attribute Attribute::decodeLLVMAttributesForBitcode(LLVMContext &C,
Bill Wendlingdefaca02013-01-22 21:15:51 +0000154 uint64_t EncodedAttrs) {
Bill Wendling702cc912012-10-15 20:35:56 +0000155 // The alignment is stored as a 16-bit raw value from bits 31--16. We shift
156 // the bits above 31 down by 11 bits.
157 unsigned Alignment = (EncodedAttrs & (0xffffULL << 16)) >> 16;
158 assert((!Alignment || isPowerOf2_32(Alignment)) &&
159 "Alignment must be a power of two.");
160
161 AttrBuilder B(EncodedAttrs & 0xffff);
162 if (Alignment)
163 B.addAlignmentAttr(Alignment);
Nadav Roteme7439422012-10-22 17:33:31 +0000164 B.addRawValue((EncodedAttrs & (0xffffULL << 32)) >> 11);
Bill Wendling034b94b2012-12-19 07:18:57 +0000165 return Attribute::get(C, B);
Bill Wendling702cc912012-10-15 20:35:56 +0000166}
167
Bill Wendling034b94b2012-12-19 07:18:57 +0000168std::string Attribute::getAsString() const {
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000169 std::string Result;
Bill Wendling034b94b2012-12-19 07:18:57 +0000170 if (hasAttribute(Attribute::ZExt))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000171 Result += "zeroext ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000172 if (hasAttribute(Attribute::SExt))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000173 Result += "signext ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000174 if (hasAttribute(Attribute::NoReturn))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000175 Result += "noreturn ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000176 if (hasAttribute(Attribute::NoUnwind))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000177 Result += "nounwind ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000178 if (hasAttribute(Attribute::UWTable))
Rafael Espindolafc2bb8c2011-05-25 03:44:17 +0000179 Result += "uwtable ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000180 if (hasAttribute(Attribute::ReturnsTwice))
Rafael Espindola25456ef2011-10-03 14:45:37 +0000181 Result += "returns_twice ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000182 if (hasAttribute(Attribute::InReg))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000183 Result += "inreg ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000184 if (hasAttribute(Attribute::NoAlias))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000185 Result += "noalias ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000186 if (hasAttribute(Attribute::NoCapture))
Nick Lewycky73ddd4f2008-12-19 09:38:31 +0000187 Result += "nocapture ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000188 if (hasAttribute(Attribute::StructRet))
Anton Korobeynikovc5ec8a72009-07-17 18:07:26 +0000189 Result += "sret ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000190 if (hasAttribute(Attribute::ByVal))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000191 Result += "byval ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000192 if (hasAttribute(Attribute::Nest))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000193 Result += "nest ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000194 if (hasAttribute(Attribute::ReadNone))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000195 Result += "readnone ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000196 if (hasAttribute(Attribute::ReadOnly))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000197 Result += "readonly ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000198 if (hasAttribute(Attribute::OptimizeForSize))
Devang Patel19c87462008-09-26 22:53:05 +0000199 Result += "optsize ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000200 if (hasAttribute(Attribute::NoInline))
Devang Patel19c87462008-09-26 22:53:05 +0000201 Result += "noinline ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000202 if (hasAttribute(Attribute::InlineHint))
Jakob Stoklund Olesen570a4a52010-02-06 01:16:28 +0000203 Result += "inlinehint ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000204 if (hasAttribute(Attribute::AlwaysInline))
Devang Patel19c87462008-09-26 22:53:05 +0000205 Result += "alwaysinline ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000206 if (hasAttribute(Attribute::StackProtect))
Bill Wendlinge9e6bdf2008-11-13 01:02:14 +0000207 Result += "ssp ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000208 if (hasAttribute(Attribute::StackProtectReq))
Bill Wendlinge9e6bdf2008-11-13 01:02:14 +0000209 Result += "sspreq ";
Bill Wendling114baee2013-01-23 06:41:41 +0000210 if (hasAttribute(Attribute::StackProtectStrong))
211 Result += "sspstrong ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000212 if (hasAttribute(Attribute::NoRedZone))
Devang Pateld18e31a2009-06-04 22:05:33 +0000213 Result += "noredzone ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000214 if (hasAttribute(Attribute::NoImplicitFloat))
Devang Patel578efa92009-06-05 21:57:13 +0000215 Result += "noimplicitfloat ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000216 if (hasAttribute(Attribute::Naked))
Anton Korobeynikovc5ec8a72009-07-17 18:07:26 +0000217 Result += "naked ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000218 if (hasAttribute(Attribute::NonLazyBind))
John McCall3a3465b2011-06-15 20:36:13 +0000219 Result += "nonlazybind ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000220 if (hasAttribute(Attribute::AddressSafety))
Kostya Serebryany164b86b2012-01-20 17:56:17 +0000221 Result += "address_safety ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000222 if (hasAttribute(Attribute::MinSize))
Quentin Colombet9a419f62012-10-30 16:32:52 +0000223 Result += "minsize ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000224 if (hasAttribute(Attribute::StackAlignment)) {
Charles Davis1e063d12010-02-12 00:31:15 +0000225 Result += "alignstack(";
Bill Wendlingef99fe82012-09-21 15:26:31 +0000226 Result += utostr(getStackAlignment());
Charles Davis1e063d12010-02-12 00:31:15 +0000227 Result += ") ";
228 }
Bill Wendling034b94b2012-12-19 07:18:57 +0000229 if (hasAttribute(Attribute::Alignment)) {
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000230 Result += "align ";
Bill Wendlingef99fe82012-09-21 15:26:31 +0000231 Result += utostr(getAlignment());
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000232 Result += " ";
233 }
James Molloy67ae1352012-12-20 16:04:27 +0000234 if (hasAttribute(Attribute::NoDuplicate))
235 Result += "noduplicate ";
Dan Gohmanc3be0fd2008-08-05 15:51:44 +0000236 // Trim the trailing space.
Nick Lewycky73ddd4f2008-12-19 09:38:31 +0000237 assert(!Result.empty() && "Unknown attribute!");
Dan Gohmanc3be0fd2008-08-05 15:51:44 +0000238 Result.erase(Result.end()-1);
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000239 return Result;
240}
241
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000242//===----------------------------------------------------------------------===//
Bill Wendling03198882013-01-04 23:27:34 +0000243// AttrBuilder Method Implementations
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000244//===----------------------------------------------------------------------===//
245
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000246AttrBuilder::AttrBuilder(AttributeSet AS, unsigned Idx)
247 : Alignment(0), StackAlignment(0) {
248 AttributeSetImpl *pImpl = AS.AttrList;
249 if (!pImpl) return;
250
251 ArrayRef<AttributeWithIndex> AttrList = pImpl->getAttributes();
252 const AttributeWithIndex *AWI = 0;
253 for (unsigned I = 0, E = AttrList.size(); I != E; ++I)
254 if (AttrList[I].Index == Idx) {
255 AWI = &AttrList[I];
256 break;
257 }
258
Bill Wendlingaec71062013-01-18 21:56:07 +0000259 if (!AWI) return;
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000260
Bill Wendling956f1342013-01-18 21:11:39 +0000261 uint64_t Mask = AWI->Attrs.Raw();
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000262
Bill Wendling956f1342013-01-18 21:11:39 +0000263 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
264 I = Attribute::AttrKind(I + 1)) {
265 if (uint64_t A = (Mask & AttributeImpl::getAttrMask(I))) {
266 Attrs.insert(I);
267
268 if (I == Attribute::Alignment)
269 Alignment = 1ULL << ((A >> 16) - 1);
270 else if (I == Attribute::StackAlignment)
271 StackAlignment = 1ULL << ((A >> 26)-1);
272 }
273 }
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000274}
275
Bill Wendling03198882013-01-04 23:27:34 +0000276void AttrBuilder::clear() {
277 Attrs.clear();
278 Alignment = StackAlignment = 0;
279}
280
281AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
282 Attrs.insert(Val);
Bill Wendling3a106e62012-10-09 19:01:18 +0000283 return *this;
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000284}
285
Bill Wendling03198882013-01-04 23:27:34 +0000286AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
287 Attrs.erase(Val);
288 if (Val == Attribute::Alignment)
289 Alignment = 0;
290 else if (Val == Attribute::StackAlignment)
291 StackAlignment = 0;
292
Bill Wendlinga19a5302012-10-14 04:10:01 +0000293 return *this;
294}
295
Bill Wendling702cc912012-10-15 20:35:56 +0000296AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
Bill Wendlingda3f9d82012-10-14 03:58:29 +0000297 if (Align == 0) return *this;
Bill Wendling03198882013-01-04 23:27:34 +0000298
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000299 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
300 assert(Align <= 0x40000000 && "Alignment too large.");
Bill Wendling03198882013-01-04 23:27:34 +0000301
302 Attrs.insert(Attribute::Alignment);
303 Alignment = Align;
Bill Wendlingda3f9d82012-10-14 03:58:29 +0000304 return *this;
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000305}
306
Bill Wendling03198882013-01-04 23:27:34 +0000307AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
308 // Default alignment, allow the target to define how to align it.
309 if (Align == 0) return *this;
310
311 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
312 assert(Align <= 0x100 && "Alignment too large.");
313
314 Attrs.insert(Attribute::StackAlignment);
315 StackAlignment = Align;
316 return *this;
317}
318
319AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) {
320 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
321 I = Attribute::AttrKind(I + 1)) {
322 if (uint64_t A = (Val & AttributeImpl::getAttrMask(I))) {
323 Attrs.insert(I);
Bill Wendlingdefaca02013-01-22 21:15:51 +0000324
Bill Wendling03198882013-01-04 23:27:34 +0000325 if (I == Attribute::Alignment)
326 Alignment = 1ULL << ((A >> 16) - 1);
327 else if (I == Attribute::StackAlignment)
328 StackAlignment = 1ULL << ((A >> 26)-1);
329 }
330 }
Bill Wendlingdefaca02013-01-22 21:15:51 +0000331
Bill Wendling3a106e62012-10-09 19:01:18 +0000332 return *this;
Bill Wendling2e879bc2012-10-09 09:11:20 +0000333}
334
Bill Wendlingdefaca02013-01-22 21:15:51 +0000335AttrBuilder &AttrBuilder::addAttributes(const Attribute &Attr) {
336 uint64_t Mask = Attr.Raw();
Bill Wendling03198882013-01-04 23:27:34 +0000337
338 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
Bill Wendlingdefaca02013-01-22 21:15:51 +0000339 I = Attribute::AttrKind(I + 1))
340 if ((Mask & AttributeImpl::getAttrMask(I)) != 0)
Bill Wendling03198882013-01-04 23:27:34 +0000341 Attrs.insert(I);
342
Bill Wendlingdefaca02013-01-22 21:15:51 +0000343 if (Attr.getAlignment())
344 Alignment = Attr.getAlignment();
345 if (Attr.getStackAlignment())
346 StackAlignment = Attr.getStackAlignment();
Bill Wendling432e6062012-10-14 07:17:34 +0000347 return *this;
348}
349
Bill Wendling034b94b2012-12-19 07:18:57 +0000350AttrBuilder &AttrBuilder::removeAttributes(const Attribute &A){
Bill Wendling1db9b692013-01-09 23:36:50 +0000351 uint64_t Mask = A.Raw();
Bill Wendling03198882013-01-04 23:27:34 +0000352
353 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
354 I = Attribute::AttrKind(I + 1)) {
355 if (Mask & AttributeImpl::getAttrMask(I)) {
356 Attrs.erase(I);
357
358 if (I == Attribute::Alignment)
359 Alignment = 0;
360 else if (I == Attribute::StackAlignment)
361 StackAlignment = 0;
362 }
363 }
364
Bill Wendling5886b7b2012-10-14 06:39:53 +0000365 return *this;
Bill Wendling8831c062012-10-09 00:01:21 +0000366}
367
Bill Wendling22bd6412013-01-03 01:54:39 +0000368bool AttrBuilder::contains(Attribute::AttrKind A) const {
Bill Wendling03198882013-01-04 23:27:34 +0000369 return Attrs.count(A);
Bill Wendling7d2f2492012-10-10 07:36:45 +0000370}
371
Bill Wendling702cc912012-10-15 20:35:56 +0000372bool AttrBuilder::hasAttributes() const {
Bill Wendling03198882013-01-04 23:27:34 +0000373 return !Attrs.empty();
Bill Wendlingf385f4c2012-10-08 23:27:46 +0000374}
Bill Wendling60507d52013-01-04 20:54:35 +0000375
Bill Wendling034b94b2012-12-19 07:18:57 +0000376bool AttrBuilder::hasAttributes(const Attribute &A) const {
Bill Wendling1db9b692013-01-09 23:36:50 +0000377 return Raw() & A.Raw();
Bill Wendling8831c062012-10-09 00:01:21 +0000378}
Bill Wendling60507d52013-01-04 20:54:35 +0000379
Bill Wendling702cc912012-10-15 20:35:56 +0000380bool AttrBuilder::hasAlignmentAttr() const {
Bill Wendling03198882013-01-04 23:27:34 +0000381 return Alignment != 0;
Bill Wendlingf385f4c2012-10-08 23:27:46 +0000382}
383
Bill Wendling1db9b692013-01-09 23:36:50 +0000384uint64_t AttrBuilder::Raw() const {
Bill Wendling03198882013-01-04 23:27:34 +0000385 uint64_t Mask = 0;
386
387 for (DenseSet<Attribute::AttrKind>::const_iterator I = Attrs.begin(),
388 E = Attrs.end(); I != E; ++I) {
389 Attribute::AttrKind Kind = *I;
390
391 if (Kind == Attribute::Alignment)
392 Mask |= (Log2_32(Alignment) + 1) << 16;
393 else if (Kind == Attribute::StackAlignment)
394 Mask |= (Log2_32(StackAlignment) + 1) << 26;
395 else
396 Mask |= AttributeImpl::getAttrMask(Kind);
397 }
398
399 return Mask;
Bill Wendlingf385f4c2012-10-08 23:27:46 +0000400}
401
Bill Wendling03198882013-01-04 23:27:34 +0000402bool AttrBuilder::operator==(const AttrBuilder &B) {
403 SmallVector<Attribute::AttrKind, 8> This(Attrs.begin(), Attrs.end());
404 SmallVector<Attribute::AttrKind, 8> That(B.Attrs.begin(), B.Attrs.end());
405 return This == That;
Bill Wendling7d2f2492012-10-10 07:36:45 +0000406}
407
Chris Lattner58d74912008-03-12 17:45:29 +0000408//===----------------------------------------------------------------------===//
Bill Wendling2c79ecb2012-09-26 21:07:29 +0000409// AttributeImpl Definition
410//===----------------------------------------------------------------------===//
411
Bill Wendling1bbd6442013-01-05 01:36:54 +0000412AttributeImpl::AttributeImpl(LLVMContext &C, uint64_t data)
413 : Context(C) {
Bill Wendling7c1683d2012-12-29 12:29:38 +0000414 Data = ConstantInt::get(Type::getInt64Ty(C), data);
415}
Bill Wendling1bbd6442013-01-05 01:36:54 +0000416AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind data)
417 : Context(C) {
Bill Wendling979aff62012-12-30 02:22:16 +0000418 Data = ConstantInt::get(Type::getInt64Ty(C), data);
419}
420AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind data,
Bill Wendling1bbd6442013-01-05 01:36:54 +0000421 ArrayRef<Constant*> values)
422 : Context(C) {
Bill Wendling979aff62012-12-30 02:22:16 +0000423 Data = ConstantInt::get(Type::getInt64Ty(C), data);
424 Vals.reserve(values.size());
425 Vals.append(values.begin(), values.end());
426}
Bill Wendling1bbd6442013-01-05 01:36:54 +0000427AttributeImpl::AttributeImpl(LLVMContext &C, StringRef data)
428 : Context(C) {
Bill Wendling979aff62012-12-30 02:22:16 +0000429 Data = ConstantDataArray::getString(C, data);
430}
Bill Wendling7c1683d2012-12-29 12:29:38 +0000431
Bill Wendling60507d52013-01-04 20:54:35 +0000432bool AttributeImpl::operator==(Attribute::AttrKind Kind) const {
Bill Wendling529ec712012-12-30 01:38:39 +0000433 if (ConstantInt *CI = dyn_cast<ConstantInt>(Data))
434 return CI->getZExtValue() == Kind;
435 return false;
436}
Bill Wendling60507d52013-01-04 20:54:35 +0000437bool AttributeImpl::operator!=(Attribute::AttrKind Kind) const {
438 return !(*this == Kind);
439}
Bill Wendling529ec712012-12-30 01:38:39 +0000440
Bill Wendling60507d52013-01-04 20:54:35 +0000441bool AttributeImpl::operator==(StringRef Kind) const {
Bill Wendling529ec712012-12-30 01:38:39 +0000442 if (ConstantDataArray *CDA = dyn_cast<ConstantDataArray>(Data))
443 if (CDA->isString())
444 return CDA->getAsString() == Kind;
445 return false;
446}
Bill Wendling3467e302013-01-24 00:06:56 +0000447
Bill Wendling60507d52013-01-04 20:54:35 +0000448bool AttributeImpl::operator!=(StringRef Kind) const {
449 return !(*this == Kind);
450}
Bill Wendling529ec712012-12-30 01:38:39 +0000451
Bill Wendling3467e302013-01-24 00:06:56 +0000452bool AttributeImpl::operator<(const AttributeImpl &AI) const {
453 if (!Data && !AI.Data) return false;
454 if (!Data && AI.Data) return true;
455 if (Data && !AI.Data) return false;
456
457 ConstantInt *ThisCI = dyn_cast<ConstantInt>(Data);
458 ConstantInt *ThatCI = dyn_cast<ConstantInt>(AI.Data);
459
460 ConstantDataArray *ThisCDA = dyn_cast<ConstantDataArray>(Data);
461 ConstantDataArray *ThatCDA = dyn_cast<ConstantDataArray>(AI.Data);
462
463 if (ThisCI && ThatCI)
464 return ThisCI->getZExtValue() < ThatCI->getZExtValue();
465
466 if (ThisCI && ThatCDA)
467 return true;
468
469 if (ThisCDA && ThatCI)
470 return false;
471
472 return ThisCDA->getAsString() < ThatCDA->getAsString();
473}
474
Bill Wendling1db9b692013-01-09 23:36:50 +0000475uint64_t AttributeImpl::Raw() const {
Bill Wendling529ec712012-12-30 01:38:39 +0000476 // FIXME: Remove this.
Bill Wendling7c1683d2012-12-29 12:29:38 +0000477 return cast<ConstantInt>(Data)->getZExtValue();
478}
479
Bill Wendling60507d52013-01-04 20:54:35 +0000480uint64_t AttributeImpl::getAttrMask(Attribute::AttrKind Val) {
Bill Wendling67658342012-10-09 07:45:08 +0000481 switch (Val) {
Chandler Carruth6f78fbb2013-01-05 08:47:26 +0000482 case Attribute::EndAttrKinds:
483 case Attribute::AttrKindEmptyKey:
484 case Attribute::AttrKindTombstoneKey:
485 llvm_unreachable("Synthetic enumerators which should never get here");
486
Bill Wendling034b94b2012-12-19 07:18:57 +0000487 case Attribute::None: return 0;
488 case Attribute::ZExt: return 1 << 0;
489 case Attribute::SExt: return 1 << 1;
490 case Attribute::NoReturn: return 1 << 2;
491 case Attribute::InReg: return 1 << 3;
492 case Attribute::StructRet: return 1 << 4;
493 case Attribute::NoUnwind: return 1 << 5;
494 case Attribute::NoAlias: return 1 << 6;
495 case Attribute::ByVal: return 1 << 7;
496 case Attribute::Nest: return 1 << 8;
497 case Attribute::ReadNone: return 1 << 9;
498 case Attribute::ReadOnly: return 1 << 10;
499 case Attribute::NoInline: return 1 << 11;
500 case Attribute::AlwaysInline: return 1 << 12;
501 case Attribute::OptimizeForSize: return 1 << 13;
502 case Attribute::StackProtect: return 1 << 14;
503 case Attribute::StackProtectReq: return 1 << 15;
504 case Attribute::Alignment: return 31 << 16;
505 case Attribute::NoCapture: return 1 << 21;
506 case Attribute::NoRedZone: return 1 << 22;
507 case Attribute::NoImplicitFloat: return 1 << 23;
508 case Attribute::Naked: return 1 << 24;
509 case Attribute::InlineHint: return 1 << 25;
510 case Attribute::StackAlignment: return 7 << 26;
511 case Attribute::ReturnsTwice: return 1 << 29;
512 case Attribute::UWTable: return 1 << 30;
513 case Attribute::NonLazyBind: return 1U << 31;
514 case Attribute::AddressSafety: return 1ULL << 32;
515 case Attribute::MinSize: return 1ULL << 33;
James Molloy67ae1352012-12-20 16:04:27 +0000516 case Attribute::NoDuplicate: return 1ULL << 34;
Bill Wendling114baee2013-01-23 06:41:41 +0000517 case Attribute::StackProtectStrong: return 1ULL << 35;
Bill Wendling67658342012-10-09 07:45:08 +0000518 }
519 llvm_unreachable("Unsupported attribute type");
520}
521
Bill Wendling60507d52013-01-04 20:54:35 +0000522bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
Bill Wendling1db9b692013-01-09 23:36:50 +0000523 return (Raw() & getAttrMask(A)) != 0;
Bill Wendling8e635db2012-10-08 21:47:17 +0000524}
Bill Wendling2c79ecb2012-09-26 21:07:29 +0000525
Bill Wendlingf6670722012-12-20 01:36:59 +0000526bool AttributeImpl::hasAttributes() const {
Bill Wendling1db9b692013-01-09 23:36:50 +0000527 return Raw() != 0;
Bill Wendling8e635db2012-10-08 21:47:17 +0000528}
Bill Wendling2c79ecb2012-09-26 21:07:29 +0000529
Bill Wendlingf6670722012-12-20 01:36:59 +0000530uint64_t AttributeImpl::getAlignment() const {
Bill Wendlinga8ab5fc2013-01-23 23:00:05 +0000531 uint64_t Mask = Raw() & getAttrMask(Attribute::Alignment);
532 return 1U << ((Mask >> 16) - 1);
Bill Wendling8e635db2012-10-08 21:47:17 +0000533}
Bill Wendling2c79ecb2012-09-26 21:07:29 +0000534
Bill Wendlingf6670722012-12-20 01:36:59 +0000535uint64_t AttributeImpl::getStackAlignment() const {
Bill Wendlinga8ab5fc2013-01-23 23:00:05 +0000536 uint64_t Mask = Raw() & getAttrMask(Attribute::StackAlignment);
537 return 1U << ((Mask >> 26) - 1);
Bill Wendling8e635db2012-10-08 21:47:17 +0000538}
Bill Wendling2c79ecb2012-09-26 21:07:29 +0000539
Bill Wendling8456efb2013-01-09 00:32:55 +0000540void AttributeImpl::Profile(FoldingSetNodeID &ID, Constant *Data,
541 ArrayRef<Constant*> Vals) {
Bill Wendlingff887162013-01-09 00:32:08 +0000542 ID.AddInteger(cast<ConstantInt>(Data)->getZExtValue());
Bill Wendling1db9b692013-01-09 23:36:50 +0000543#if 0
544 // FIXME: Not yet supported.
Bill Wendlingff887162013-01-09 00:32:08 +0000545 for (ArrayRef<Constant*>::iterator I = Vals.begin(), E = Vals.end();
546 I != E; ++I)
547 ID.AddPointer(*I);
Bill Wendling1db9b692013-01-09 23:36:50 +0000548#endif
Bill Wendlingff887162013-01-09 00:32:08 +0000549}
550
Bill Wendling2c79ecb2012-09-26 21:07:29 +0000551//===----------------------------------------------------------------------===//
Bill Wendlingc5f1bc82013-01-21 21:57:28 +0000552// AttributeWithIndex Definition
553//===----------------------------------------------------------------------===//
554
555AttributeWithIndex AttributeWithIndex::get(LLVMContext &C, unsigned Idx,
556 AttributeSet AS) {
557 // FIXME: This is temporary, but necessary for the conversion.
558 AttrBuilder B(AS, Idx);
559 return get(Idx, Attribute::get(C, B));
560}
561
562//===----------------------------------------------------------------------===//
Bill Wendling3467e302013-01-24 00:06:56 +0000563// AttributeSetNode Definition
564//===----------------------------------------------------------------------===//
565
566AttributeSetNode *AttributeSetNode::get(LLVMContext &C,
567 ArrayRef<Attribute> Attrs) {
568 if (Attrs.empty())
569 return 0;
570
571 // Otherwise, build a key to look up the existing attributes.
572 LLVMContextImpl *pImpl = C.pImpl;
573 FoldingSetNodeID ID;
574
575 SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end());
576 std::sort(SortedAttrs.begin(), SortedAttrs.end());
577
578 for (SmallVectorImpl<Attribute>::iterator I = SortedAttrs.begin(),
579 E = SortedAttrs.end(); I != E; ++I)
580 I->Profile(ID);
581
582 void *InsertPoint;
583 AttributeSetNode *PA =
584 pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint);
585
586 // If we didn't find any existing attributes of the same shape then create a
587 // new one and insert it.
588 if (!PA) {
589 PA = new AttributeSetNode(SortedAttrs);
590 pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint);
591 }
592
593 // Return the AttributesListNode that we found or created.
594 return PA;
595}
596
597//===----------------------------------------------------------------------===//
Bill Wendling18e72112012-12-19 22:42:22 +0000598// AttributeSetImpl Definition
Chris Lattner58d74912008-03-12 17:45:29 +0000599//===----------------------------------------------------------------------===//
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000600
Bill Wendling28d65722013-01-23 06:14:59 +0000601AttributeSet AttributeSet::getParamAttributes(unsigned Idx) const {
602 // FIXME: Remove.
603 return AttrList && hasAttributes(Idx) ?
604 AttributeSet::get(AttrList->getContext(),
605 AttributeWithIndex::get(Idx, getAttributes(Idx))) :
606 AttributeSet();
607}
608
Bill Wendling3fc4b962013-01-21 22:44:49 +0000609AttributeSet AttributeSet::getRetAttributes() const {
610 // FIXME: Remove.
611 return AttrList && hasAttributes(ReturnIndex) ?
612 AttributeSet::get(AttrList->getContext(),
613 AttributeWithIndex::get(ReturnIndex,
614 getAttributes(ReturnIndex))) :
615 AttributeSet();
616}
617
Bill Wendlingc5f1bc82013-01-21 21:57:28 +0000618AttributeSet AttributeSet::getFnAttributes() const {
619 // FIXME: Remove.
Bill Wendling3fc4b962013-01-21 22:44:49 +0000620 return AttrList && hasAttributes(FunctionIndex) ?
Bill Wendlingc5f1bc82013-01-21 21:57:28 +0000621 AttributeSet::get(AttrList->getContext(),
622 AttributeWithIndex::get(FunctionIndex,
623 getAttributes(FunctionIndex))) :
624 AttributeSet();
625}
626
Bill Wendling99faa3b2012-12-07 23:16:57 +0000627AttributeSet AttributeSet::get(LLVMContext &C,
Bill Wendling1ce47ac2012-12-12 19:21:53 +0000628 ArrayRef<AttributeWithIndex> Attrs) {
Devang Patel05988662008-09-25 21:00:45 +0000629 // If there are no attributes then return a null AttributesList pointer.
Chris Lattnerd509d0b2012-05-28 01:47:44 +0000630 if (Attrs.empty())
Bill Wendling99faa3b2012-12-07 23:16:57 +0000631 return AttributeSet();
Bill Wendling77898682012-10-16 06:01:44 +0000632
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000633#ifndef NDEBUG
Chris Lattnerd509d0b2012-05-28 01:47:44 +0000634 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
Bill Wendling77898682012-10-16 06:01:44 +0000635 assert(Attrs[i].Attrs.hasAttributes() &&
Devang Patel05988662008-09-25 21:00:45 +0000636 "Pointless attribute!");
Chris Lattner58d74912008-03-12 17:45:29 +0000637 assert((!i || Attrs[i-1].Index < Attrs[i].Index) &&
Devang Patel05988662008-09-25 21:00:45 +0000638 "Misordered AttributesList!");
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000639 }
640#endif
Bill Wendling77898682012-10-16 06:01:44 +0000641
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000642 // Otherwise, build a key to look up the existing attributes.
Bill Wendling0976e002012-11-20 05:09:20 +0000643 LLVMContextImpl *pImpl = C.pImpl;
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000644 FoldingSetNodeID ID;
Bill Wendling18e72112012-12-19 22:42:22 +0000645 AttributeSetImpl::Profile(ID, Attrs);
Bill Wendling77898682012-10-16 06:01:44 +0000646
Bill Wendling0976e002012-11-20 05:09:20 +0000647 void *InsertPoint;
Bill Wendling1bbd6442013-01-05 01:36:54 +0000648 AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
Bill Wendling77898682012-10-16 06:01:44 +0000649
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000650 // If we didn't find any existing attributes of the same shape then
651 // create a new one and insert it.
Bill Wendling0976e002012-11-20 05:09:20 +0000652 if (!PA) {
Bill Wendling5f93e2b2012-12-19 23:55:43 +0000653 PA = new AttributeSetImpl(C, Attrs);
Bill Wendling0976e002012-11-20 05:09:20 +0000654 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000655 }
Bill Wendling77898682012-10-16 06:01:44 +0000656
Devang Patel05988662008-09-25 21:00:45 +0000657 // Return the AttributesList that we found or created.
Bill Wendling99faa3b2012-12-07 23:16:57 +0000658 return AttributeSet(PA);
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000659}
660
Bill Wendling1bbd6442013-01-05 01:36:54 +0000661AttributeSet AttributeSet::get(LLVMContext &C, unsigned Idx, AttrBuilder &B) {
Bill Wendling3fc4b962013-01-21 22:44:49 +0000662 // FIXME: This should be implemented as a loop that creates the
663 // AttributeWithIndexes that then are used to create the AttributeSet.
664 if (!B.hasAttributes())
665 return AttributeSet();
Bill Wendlingdefaca02013-01-22 21:15:51 +0000666 return get(C, AttributeWithIndex::get(Idx, Attribute::get(C, B)));
Bill Wendling1bbd6442013-01-05 01:36:54 +0000667}
668
Bill Wendling28d65722013-01-23 06:14:59 +0000669AttributeSet AttributeSet::get(LLVMContext &C, unsigned Idx,
670 Attribute::AttrKind Kind) {
671 return get(C, AttributeWithIndex::get(Idx, Attribute::get(C, Kind)));
672}
673
Chris Lattner58d74912008-03-12 17:45:29 +0000674//===----------------------------------------------------------------------===//
Bill Wendling99faa3b2012-12-07 23:16:57 +0000675// AttributeSet Method Implementations
Chris Lattner58d74912008-03-12 17:45:29 +0000676//===----------------------------------------------------------------------===//
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000677
Bill Wendling99faa3b2012-12-07 23:16:57 +0000678const AttributeSet &AttributeSet::operator=(const AttributeSet &RHS) {
Bill Wendling0976e002012-11-20 05:09:20 +0000679 AttrList = RHS.AttrList;
680 return *this;
Chris Lattner58d74912008-03-12 17:45:29 +0000681}
682
Bill Wendling77898682012-10-16 06:01:44 +0000683/// getNumSlots - Return the number of slots used in this attribute list.
Chris Lattner58d74912008-03-12 17:45:29 +0000684/// This is the number of arguments that have an attribute set on them
685/// (including the function itself).
Bill Wendling99faa3b2012-12-07 23:16:57 +0000686unsigned AttributeSet::getNumSlots() const {
Bill Wendling60507d52013-01-04 20:54:35 +0000687 return AttrList ? AttrList->getNumAttributes() : 0;
Chris Lattner58d74912008-03-12 17:45:29 +0000688}
689
Devang Patel05988662008-09-25 21:00:45 +0000690/// getSlot - Return the AttributeWithIndex at the specified slot. This
691/// holds a number plus a set of attributes.
Bill Wendling99faa3b2012-12-07 23:16:57 +0000692const AttributeWithIndex &AttributeSet::getSlot(unsigned Slot) const {
Bill Wendling60507d52013-01-04 20:54:35 +0000693 assert(AttrList && Slot < AttrList->getNumAttributes() &&
694 "Slot # out of range!");
695 return AttrList->getAttributes()[Slot];
Chris Lattner58d74912008-03-12 17:45:29 +0000696}
697
Bill Wendling831737d2012-12-30 10:32:01 +0000698bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{
699 return getAttributes(Index).hasAttribute(Kind);
700}
701
702bool AttributeSet::hasAttributes(unsigned Index) const {
703 return getAttributes(Index).hasAttributes();
704}
705
706std::string AttributeSet::getAsString(unsigned Index) const {
707 return getAttributes(Index).getAsString();
708}
709
Bill Wendling956f1342013-01-18 21:11:39 +0000710unsigned AttributeSet::getParamAlignment(unsigned Idx) const {
711 return getAttributes(Idx).getAlignment();
712}
713
Bill Wendling831737d2012-12-30 10:32:01 +0000714unsigned AttributeSet::getStackAlignment(unsigned Index) const {
715 return getAttributes(Index).getStackAlignment();
716}
717
Bill Wendling1db9b692013-01-09 23:36:50 +0000718uint64_t AttributeSet::Raw(unsigned Index) const {
Bill Wendling831737d2012-12-30 10:32:01 +0000719 // FIXME: Remove this.
Bill Wendling1db9b692013-01-09 23:36:50 +0000720 return getAttributes(Index).Raw();
Bill Wendling831737d2012-12-30 10:32:01 +0000721}
722
Bill Wendling77898682012-10-16 06:01:44 +0000723/// getAttributes - The attributes for the specified index are returned.
Bill Wendling034b94b2012-12-19 07:18:57 +0000724Attribute AttributeSet::getAttributes(unsigned Idx) const {
725 if (AttrList == 0) return Attribute();
Bill Wendling77898682012-10-16 06:01:44 +0000726
Bill Wendling60507d52013-01-04 20:54:35 +0000727 ArrayRef<AttributeWithIndex> Attrs = AttrList->getAttributes();
Chris Lattner58d74912008-03-12 17:45:29 +0000728 for (unsigned i = 0, e = Attrs.size(); i != e && Attrs[i].Index <= Idx; ++i)
729 if (Attrs[i].Index == Idx)
730 return Attrs[i].Attrs;
Bill Wendlingef99fe82012-09-21 15:26:31 +0000731
Bill Wendling034b94b2012-12-19 07:18:57 +0000732 return Attribute();
Chris Lattner58d74912008-03-12 17:45:29 +0000733}
734
735/// hasAttrSomewhere - Return true if the specified attribute is set for at
736/// least one parameter or for the return value.
Bill Wendling629fb822012-12-22 00:37:52 +0000737bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr) const {
Devang Patel05988662008-09-25 21:00:45 +0000738 if (AttrList == 0) return false;
Bill Wendling7d2f2492012-10-10 07:36:45 +0000739
Bill Wendling60507d52013-01-04 20:54:35 +0000740 ArrayRef<AttributeWithIndex> Attrs = AttrList->getAttributes();
Chris Lattner58d74912008-03-12 17:45:29 +0000741 for (unsigned i = 0, e = Attrs.size(); i != e; ++i)
Bill Wendling7d2f2492012-10-10 07:36:45 +0000742 if (Attrs[i].Attrs.hasAttribute(Attr))
Chris Lattner58d74912008-03-12 17:45:29 +0000743 return true;
Bill Wendling0976e002012-11-20 05:09:20 +0000744
Chris Lattner58d74912008-03-12 17:45:29 +0000745 return false;
746}
747
Bill Wendlingdefaca02013-01-22 21:15:51 +0000748AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Idx,
749 Attribute::AttrKind Attr) const {
750 return addAttr(C, Idx, Attribute::get(C, Attr));
751}
752
Bill Wendlinge4e85f12013-01-22 00:53:12 +0000753AttributeSet AttributeSet::addAttributes(LLVMContext &C, unsigned Idx,
754 AttributeSet Attrs) const {
755 return addAttr(C, Idx, Attrs.getAttributes(Idx));
Bill Wendling956f1342013-01-18 21:11:39 +0000756}
757
Bill Wendling99faa3b2012-12-07 23:16:57 +0000758AttributeSet AttributeSet::addAttr(LLVMContext &C, unsigned Idx,
Bill Wendling9d30e722012-12-31 00:49:59 +0000759 Attribute Attrs) const {
Bill Wendling034b94b2012-12-19 07:18:57 +0000760 Attribute OldAttrs = getAttributes(Idx);
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000761#ifndef NDEBUG
762 // FIXME it is not obvious how this should work for alignment.
763 // For now, say we can't change a known alignment.
Bill Wendlingef99fe82012-09-21 15:26:31 +0000764 unsigned OldAlign = OldAttrs.getAlignment();
765 unsigned NewAlign = Attrs.getAlignment();
Anton Korobeynikov4aefd6b2008-02-20 12:07:57 +0000766 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000767 "Attempt to change alignment!");
768#endif
Bill Wendling77898682012-10-16 06:01:44 +0000769
Bill Wendling702cc912012-10-15 20:35:56 +0000770 AttrBuilder NewAttrs =
771 AttrBuilder(OldAttrs).addAttributes(Attrs);
772 if (NewAttrs == AttrBuilder(OldAttrs))
Chris Lattner58d74912008-03-12 17:45:29 +0000773 return *this;
Bill Wendling77898682012-10-16 06:01:44 +0000774
Devang Patel05988662008-09-25 21:00:45 +0000775 SmallVector<AttributeWithIndex, 8> NewAttrList;
776 if (AttrList == 0)
777 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Chris Lattner58d74912008-03-12 17:45:29 +0000778 else {
Bill Wendling60507d52013-01-04 20:54:35 +0000779 ArrayRef<AttributeWithIndex> OldAttrList = AttrList->getAttributes();
Chris Lattner58d74912008-03-12 17:45:29 +0000780 unsigned i = 0, e = OldAttrList.size();
781 // Copy attributes for arguments before this one.
782 for (; i != e && OldAttrList[i].Index < Idx; ++i)
783 NewAttrList.push_back(OldAttrList[i]);
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000784
Chris Lattner58d74912008-03-12 17:45:29 +0000785 // If there are attributes already at this index, merge them in.
786 if (i != e && OldAttrList[i].Index == Idx) {
Bill Wendlingc4167952012-10-14 07:35:59 +0000787 Attrs =
Bill Wendling034b94b2012-12-19 07:18:57 +0000788 Attribute::get(C, AttrBuilder(Attrs).
Bill Wendlingc4167952012-10-14 07:35:59 +0000789 addAttributes(OldAttrList[i].Attrs));
Chris Lattner58d74912008-03-12 17:45:29 +0000790 ++i;
791 }
Bill Wendling77898682012-10-16 06:01:44 +0000792
Devang Patel05988662008-09-25 21:00:45 +0000793 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Bill Wendling77898682012-10-16 06:01:44 +0000794
Chris Lattner58d74912008-03-12 17:45:29 +0000795 // Copy attributes for arguments after this one.
Bill Wendling77898682012-10-16 06:01:44 +0000796 NewAttrList.insert(NewAttrList.end(),
Chris Lattner58d74912008-03-12 17:45:29 +0000797 OldAttrList.begin()+i, OldAttrList.end());
798 }
Bill Wendling77898682012-10-16 06:01:44 +0000799
Bill Wendling0976e002012-11-20 05:09:20 +0000800 return get(C, NewAttrList);
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000801}
802
Bill Wendling8246df62013-01-23 00:45:55 +0000803AttributeSet AttributeSet::removeAttribute(LLVMContext &C, unsigned Idx,
804 Attribute::AttrKind Attr) const {
805 return removeAttr(C, Idx, Attribute::get(C, Attr));
806}
807
808AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Idx,
809 AttributeSet Attrs) const {
810 return removeAttr(C, Idx, Attrs.getAttributes(Idx));
811}
812
Bill Wendling99faa3b2012-12-07 23:16:57 +0000813AttributeSet AttributeSet::removeAttr(LLVMContext &C, unsigned Idx,
Bill Wendling9d30e722012-12-31 00:49:59 +0000814 Attribute Attrs) const {
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000815#ifndef NDEBUG
816 // FIXME it is not obvious how this should work for alignment.
817 // For now, say we can't pass in alignment, which no current use does.
Bill Wendling034b94b2012-12-19 07:18:57 +0000818 assert(!Attrs.hasAttribute(Attribute::Alignment) &&
Bill Wendling67658342012-10-09 07:45:08 +0000819 "Attempt to exclude alignment!");
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000820#endif
Bill Wendling99faa3b2012-12-07 23:16:57 +0000821 if (AttrList == 0) return AttributeSet();
Bill Wendling77898682012-10-16 06:01:44 +0000822
Bill Wendling034b94b2012-12-19 07:18:57 +0000823 Attribute OldAttrs = getAttributes(Idx);
Bill Wendling702cc912012-10-15 20:35:56 +0000824 AttrBuilder NewAttrs =
825 AttrBuilder(OldAttrs).removeAttributes(Attrs);
826 if (NewAttrs == AttrBuilder(OldAttrs))
Chris Lattner58d74912008-03-12 17:45:29 +0000827 return *this;
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000828
Devang Patel05988662008-09-25 21:00:45 +0000829 SmallVector<AttributeWithIndex, 8> NewAttrList;
Bill Wendling60507d52013-01-04 20:54:35 +0000830 ArrayRef<AttributeWithIndex> OldAttrList = AttrList->getAttributes();
Chris Lattner58d74912008-03-12 17:45:29 +0000831 unsigned i = 0, e = OldAttrList.size();
Bill Wendling77898682012-10-16 06:01:44 +0000832
Chris Lattner58d74912008-03-12 17:45:29 +0000833 // Copy attributes for arguments before this one.
834 for (; i != e && OldAttrList[i].Index < Idx; ++i)
835 NewAttrList.push_back(OldAttrList[i]);
Bill Wendling77898682012-10-16 06:01:44 +0000836
Chris Lattner58d74912008-03-12 17:45:29 +0000837 // If there are attributes already at this index, merge them in.
838 assert(OldAttrList[i].Index == Idx && "Attribute isn't set?");
Bill Wendling034b94b2012-12-19 07:18:57 +0000839 Attrs = Attribute::get(C, AttrBuilder(OldAttrList[i].Attrs).
Bill Wendling5886b7b2012-10-14 06:39:53 +0000840 removeAttributes(Attrs));
Chris Lattner58d74912008-03-12 17:45:29 +0000841 ++i;
Bill Wendling7be78482012-10-14 08:54:26 +0000842 if (Attrs.hasAttributes()) // If any attributes left for this param, add them.
Devang Patel05988662008-09-25 21:00:45 +0000843 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Bill Wendling77898682012-10-16 06:01:44 +0000844
Chris Lattner58d74912008-03-12 17:45:29 +0000845 // Copy attributes for arguments after this one.
Bill Wendling77898682012-10-16 06:01:44 +0000846 NewAttrList.insert(NewAttrList.end(),
Chris Lattner58d74912008-03-12 17:45:29 +0000847 OldAttrList.begin()+i, OldAttrList.end());
Bill Wendling77898682012-10-16 06:01:44 +0000848
Bill Wendling0976e002012-11-20 05:09:20 +0000849 return get(C, NewAttrList);
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000850}
851
Bill Wendling99faa3b2012-12-07 23:16:57 +0000852void AttributeSet::dump() const {
David Greeneef1894e2010-01-05 01:29:58 +0000853 dbgs() << "PAL[ ";
Chris Lattner58d74912008-03-12 17:45:29 +0000854 for (unsigned i = 0; i < getNumSlots(); ++i) {
Devang Patel05988662008-09-25 21:00:45 +0000855 const AttributeWithIndex &PAWI = getSlot(i);
Bill Wendling60507d52013-01-04 20:54:35 +0000856 dbgs() << "{ " << PAWI.Index << ", " << PAWI.Attrs.getAsString() << " } ";
Chris Lattner58d74912008-03-12 17:45:29 +0000857 }
Bill Wendling77898682012-10-16 06:01:44 +0000858
David Greeneef1894e2010-01-05 01:29:58 +0000859 dbgs() << "]\n";
Duncan Sandsad9a9e12008-01-06 18:27:01 +0000860}