blob: 8ee7057c1962d9d028c9a1e0cdedaff0441f1123 [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
Bill Wendling1db9b692013-01-09 23:36:50 +0000102uint64_t Attribute::Raw() const {
103 return pImpl ? pImpl->Raw() : 0;
Bill Wendlingb10c88f2012-10-07 08:55:05 +0000104}
105
Bill Wendling034b94b2012-12-19 07:18:57 +0000106Attribute Attribute::typeIncompatible(Type *Ty) {
Bill Wendling702cc912012-10-15 20:35:56 +0000107 AttrBuilder Incompatible;
Bill Wendling77898682012-10-16 06:01:44 +0000108
Bill Wendling3a106e62012-10-09 19:01:18 +0000109 if (!Ty->isIntegerTy())
Bill Wendling034b94b2012-12-19 07:18:57 +0000110 // Attribute that only apply to integers.
111 Incompatible.addAttribute(Attribute::SExt)
112 .addAttribute(Attribute::ZExt);
Bill Wendling77898682012-10-16 06:01:44 +0000113
Bill Wendling3a106e62012-10-09 19:01:18 +0000114 if (!Ty->isPointerTy())
Bill Wendling034b94b2012-12-19 07:18:57 +0000115 // Attribute that only apply to pointers.
116 Incompatible.addAttribute(Attribute::ByVal)
117 .addAttribute(Attribute::Nest)
118 .addAttribute(Attribute::NoAlias)
119 .addAttribute(Attribute::NoCapture)
120 .addAttribute(Attribute::StructRet);
Bill Wendling77898682012-10-16 06:01:44 +0000121
Bill Wendling034b94b2012-12-19 07:18:57 +0000122 return Attribute::get(Ty->getContext(), Incompatible);
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000123}
124
Bill Wendling702cc912012-10-15 20:35:56 +0000125/// encodeLLVMAttributesForBitcode - This returns an integer containing an
126/// encoding of all the LLVM attributes found in the given attribute bitset.
127/// Any change to this encoding is a breaking change to bitcode compatibility.
Bill Wendling034b94b2012-12-19 07:18:57 +0000128uint64_t Attribute::encodeLLVMAttributesForBitcode(Attribute Attrs) {
Bill Wendling702cc912012-10-15 20:35:56 +0000129 // FIXME: It doesn't make sense to store the alignment information as an
130 // expanded out value, we should store it as a log2 value. However, we can't
131 // just change that here without breaking bitcode compatibility. If this ever
132 // becomes a problem in practice, we should introduce new tag numbers in the
133 // bitcode file and have those tags use a more efficiently encoded alignment
134 // field.
135
136 // Store the alignment in the bitcode as a 16-bit raw value instead of a 5-bit
137 // log2 encoded value. Shift the bits above the alignment up by 11 bits.
Bill Wendling1db9b692013-01-09 23:36:50 +0000138 uint64_t EncodedAttrs = Attrs.Raw() & 0xffff;
Bill Wendling034b94b2012-12-19 07:18:57 +0000139 if (Attrs.hasAttribute(Attribute::Alignment))
Bill Wendling702cc912012-10-15 20:35:56 +0000140 EncodedAttrs |= Attrs.getAlignment() << 16;
Bill Wendling1db9b692013-01-09 23:36:50 +0000141 EncodedAttrs |= (Attrs.Raw() & (0xffffULL << 21)) << 11;
Bill Wendling702cc912012-10-15 20:35:56 +0000142 return EncodedAttrs;
143}
144
145/// decodeLLVMAttributesForBitcode - This returns an attribute bitset containing
146/// the LLVM attributes that have been decoded from the given integer. This
147/// function must stay in sync with 'encodeLLVMAttributesForBitcode'.
Bill Wendling034b94b2012-12-19 07:18:57 +0000148Attribute Attribute::decodeLLVMAttributesForBitcode(LLVMContext &C,
Bill Wendlingdefaca02013-01-22 21:15:51 +0000149 uint64_t EncodedAttrs) {
Bill Wendling702cc912012-10-15 20:35:56 +0000150 // The alignment is stored as a 16-bit raw value from bits 31--16. We shift
151 // the bits above 31 down by 11 bits.
152 unsigned Alignment = (EncodedAttrs & (0xffffULL << 16)) >> 16;
153 assert((!Alignment || isPowerOf2_32(Alignment)) &&
154 "Alignment must be a power of two.");
155
156 AttrBuilder B(EncodedAttrs & 0xffff);
157 if (Alignment)
158 B.addAlignmentAttr(Alignment);
Nadav Roteme7439422012-10-22 17:33:31 +0000159 B.addRawValue((EncodedAttrs & (0xffffULL << 32)) >> 11);
Bill Wendling034b94b2012-12-19 07:18:57 +0000160 return Attribute::get(C, B);
Bill Wendling702cc912012-10-15 20:35:56 +0000161}
162
Bill Wendling034b94b2012-12-19 07:18:57 +0000163std::string Attribute::getAsString() const {
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000164 std::string Result;
Bill Wendling034b94b2012-12-19 07:18:57 +0000165 if (hasAttribute(Attribute::ZExt))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000166 Result += "zeroext ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000167 if (hasAttribute(Attribute::SExt))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000168 Result += "signext ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000169 if (hasAttribute(Attribute::NoReturn))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000170 Result += "noreturn ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000171 if (hasAttribute(Attribute::NoUnwind))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000172 Result += "nounwind ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000173 if (hasAttribute(Attribute::UWTable))
Rafael Espindolafc2bb8c2011-05-25 03:44:17 +0000174 Result += "uwtable ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000175 if (hasAttribute(Attribute::ReturnsTwice))
Rafael Espindola25456ef2011-10-03 14:45:37 +0000176 Result += "returns_twice ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000177 if (hasAttribute(Attribute::InReg))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000178 Result += "inreg ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000179 if (hasAttribute(Attribute::NoAlias))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000180 Result += "noalias ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000181 if (hasAttribute(Attribute::NoCapture))
Nick Lewycky73ddd4f2008-12-19 09:38:31 +0000182 Result += "nocapture ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000183 if (hasAttribute(Attribute::StructRet))
Anton Korobeynikovc5ec8a72009-07-17 18:07:26 +0000184 Result += "sret ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000185 if (hasAttribute(Attribute::ByVal))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000186 Result += "byval ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000187 if (hasAttribute(Attribute::Nest))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000188 Result += "nest ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000189 if (hasAttribute(Attribute::ReadNone))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000190 Result += "readnone ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000191 if (hasAttribute(Attribute::ReadOnly))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000192 Result += "readonly ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000193 if (hasAttribute(Attribute::OptimizeForSize))
Devang Patel19c87462008-09-26 22:53:05 +0000194 Result += "optsize ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000195 if (hasAttribute(Attribute::NoInline))
Devang Patel19c87462008-09-26 22:53:05 +0000196 Result += "noinline ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000197 if (hasAttribute(Attribute::InlineHint))
Jakob Stoklund Olesen570a4a52010-02-06 01:16:28 +0000198 Result += "inlinehint ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000199 if (hasAttribute(Attribute::AlwaysInline))
Devang Patel19c87462008-09-26 22:53:05 +0000200 Result += "alwaysinline ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000201 if (hasAttribute(Attribute::StackProtect))
Bill Wendlinge9e6bdf2008-11-13 01:02:14 +0000202 Result += "ssp ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000203 if (hasAttribute(Attribute::StackProtectReq))
Bill Wendlinge9e6bdf2008-11-13 01:02:14 +0000204 Result += "sspreq ";
Bill Wendling114baee2013-01-23 06:41:41 +0000205 if (hasAttribute(Attribute::StackProtectStrong))
206 Result += "sspstrong ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000207 if (hasAttribute(Attribute::NoRedZone))
Devang Pateld18e31a2009-06-04 22:05:33 +0000208 Result += "noredzone ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000209 if (hasAttribute(Attribute::NoImplicitFloat))
Devang Patel578efa92009-06-05 21:57:13 +0000210 Result += "noimplicitfloat ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000211 if (hasAttribute(Attribute::Naked))
Anton Korobeynikovc5ec8a72009-07-17 18:07:26 +0000212 Result += "naked ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000213 if (hasAttribute(Attribute::NonLazyBind))
John McCall3a3465b2011-06-15 20:36:13 +0000214 Result += "nonlazybind ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000215 if (hasAttribute(Attribute::AddressSafety))
Kostya Serebryany164b86b2012-01-20 17:56:17 +0000216 Result += "address_safety ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000217 if (hasAttribute(Attribute::MinSize))
Quentin Colombet9a419f62012-10-30 16:32:52 +0000218 Result += "minsize ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000219 if (hasAttribute(Attribute::StackAlignment)) {
Charles Davis1e063d12010-02-12 00:31:15 +0000220 Result += "alignstack(";
Bill Wendlingef99fe82012-09-21 15:26:31 +0000221 Result += utostr(getStackAlignment());
Charles Davis1e063d12010-02-12 00:31:15 +0000222 Result += ") ";
223 }
Bill Wendling034b94b2012-12-19 07:18:57 +0000224 if (hasAttribute(Attribute::Alignment)) {
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000225 Result += "align ";
Bill Wendlingef99fe82012-09-21 15:26:31 +0000226 Result += utostr(getAlignment());
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000227 Result += " ";
228 }
James Molloy67ae1352012-12-20 16:04:27 +0000229 if (hasAttribute(Attribute::NoDuplicate))
230 Result += "noduplicate ";
Dan Gohmanc3be0fd2008-08-05 15:51:44 +0000231 // Trim the trailing space.
Nick Lewycky73ddd4f2008-12-19 09:38:31 +0000232 assert(!Result.empty() && "Unknown attribute!");
Dan Gohmanc3be0fd2008-08-05 15:51:44 +0000233 Result.erase(Result.end()-1);
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000234 return Result;
235}
236
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000237//===----------------------------------------------------------------------===//
Bill Wendling03198882013-01-04 23:27:34 +0000238// AttrBuilder Method Implementations
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000239//===----------------------------------------------------------------------===//
240
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000241AttrBuilder::AttrBuilder(AttributeSet AS, unsigned Idx)
242 : Alignment(0), StackAlignment(0) {
243 AttributeSetImpl *pImpl = AS.AttrList;
244 if (!pImpl) return;
245
246 ArrayRef<AttributeWithIndex> AttrList = pImpl->getAttributes();
247 const AttributeWithIndex *AWI = 0;
248 for (unsigned I = 0, E = AttrList.size(); I != E; ++I)
249 if (AttrList[I].Index == Idx) {
250 AWI = &AttrList[I];
251 break;
252 }
253
Bill Wendlingaec71062013-01-18 21:56:07 +0000254 if (!AWI) return;
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000255
Bill Wendling956f1342013-01-18 21:11:39 +0000256 uint64_t Mask = AWI->Attrs.Raw();
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000257
Bill Wendling956f1342013-01-18 21:11:39 +0000258 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
259 I = Attribute::AttrKind(I + 1)) {
260 if (uint64_t A = (Mask & AttributeImpl::getAttrMask(I))) {
261 Attrs.insert(I);
262
263 if (I == Attribute::Alignment)
264 Alignment = 1ULL << ((A >> 16) - 1);
265 else if (I == Attribute::StackAlignment)
266 StackAlignment = 1ULL << ((A >> 26)-1);
267 }
268 }
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000269}
270
Bill Wendling03198882013-01-04 23:27:34 +0000271void AttrBuilder::clear() {
272 Attrs.clear();
273 Alignment = StackAlignment = 0;
274}
275
276AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
277 Attrs.insert(Val);
Bill Wendling3a106e62012-10-09 19:01:18 +0000278 return *this;
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000279}
280
Bill Wendling03198882013-01-04 23:27:34 +0000281AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
282 Attrs.erase(Val);
283 if (Val == Attribute::Alignment)
284 Alignment = 0;
285 else if (Val == Attribute::StackAlignment)
286 StackAlignment = 0;
287
Bill Wendlinga19a5302012-10-14 04:10:01 +0000288 return *this;
289}
290
Bill Wendling702cc912012-10-15 20:35:56 +0000291AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
Bill Wendlingda3f9d82012-10-14 03:58:29 +0000292 if (Align == 0) return *this;
Bill Wendling03198882013-01-04 23:27:34 +0000293
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000294 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
295 assert(Align <= 0x40000000 && "Alignment too large.");
Bill Wendling03198882013-01-04 23:27:34 +0000296
297 Attrs.insert(Attribute::Alignment);
298 Alignment = Align;
Bill Wendlingda3f9d82012-10-14 03:58:29 +0000299 return *this;
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000300}
301
Bill Wendling03198882013-01-04 23:27:34 +0000302AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
303 // Default alignment, allow the target to define how to align it.
304 if (Align == 0) return *this;
305
306 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
307 assert(Align <= 0x100 && "Alignment too large.");
308
309 Attrs.insert(Attribute::StackAlignment);
310 StackAlignment = Align;
311 return *this;
312}
313
314AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) {
315 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
316 I = Attribute::AttrKind(I + 1)) {
317 if (uint64_t A = (Val & AttributeImpl::getAttrMask(I))) {
318 Attrs.insert(I);
Bill Wendlingdefaca02013-01-22 21:15:51 +0000319
Bill Wendling03198882013-01-04 23:27:34 +0000320 if (I == Attribute::Alignment)
321 Alignment = 1ULL << ((A >> 16) - 1);
322 else if (I == Attribute::StackAlignment)
323 StackAlignment = 1ULL << ((A >> 26)-1);
324 }
325 }
Bill Wendlingdefaca02013-01-22 21:15:51 +0000326
Bill Wendling3a106e62012-10-09 19:01:18 +0000327 return *this;
Bill Wendling2e879bc2012-10-09 09:11:20 +0000328}
329
Bill Wendlingdefaca02013-01-22 21:15:51 +0000330AttrBuilder &AttrBuilder::addAttributes(const Attribute &Attr) {
331 uint64_t Mask = Attr.Raw();
Bill Wendling03198882013-01-04 23:27:34 +0000332
333 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
Bill Wendlingdefaca02013-01-22 21:15:51 +0000334 I = Attribute::AttrKind(I + 1))
335 if ((Mask & AttributeImpl::getAttrMask(I)) != 0)
Bill Wendling03198882013-01-04 23:27:34 +0000336 Attrs.insert(I);
337
Bill Wendlingdefaca02013-01-22 21:15:51 +0000338 if (Attr.getAlignment())
339 Alignment = Attr.getAlignment();
340 if (Attr.getStackAlignment())
341 StackAlignment = Attr.getStackAlignment();
Bill Wendling432e6062012-10-14 07:17:34 +0000342 return *this;
343}
344
Bill Wendling034b94b2012-12-19 07:18:57 +0000345AttrBuilder &AttrBuilder::removeAttributes(const Attribute &A){
Bill Wendling1db9b692013-01-09 23:36:50 +0000346 uint64_t Mask = A.Raw();
Bill Wendling03198882013-01-04 23:27:34 +0000347
348 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
349 I = Attribute::AttrKind(I + 1)) {
350 if (Mask & AttributeImpl::getAttrMask(I)) {
351 Attrs.erase(I);
352
353 if (I == Attribute::Alignment)
354 Alignment = 0;
355 else if (I == Attribute::StackAlignment)
356 StackAlignment = 0;
357 }
358 }
359
Bill Wendling5886b7b2012-10-14 06:39:53 +0000360 return *this;
Bill Wendling8831c062012-10-09 00:01:21 +0000361}
362
Bill Wendling22bd6412013-01-03 01:54:39 +0000363bool AttrBuilder::contains(Attribute::AttrKind A) const {
Bill Wendling03198882013-01-04 23:27:34 +0000364 return Attrs.count(A);
Bill Wendling7d2f2492012-10-10 07:36:45 +0000365}
366
Bill Wendling702cc912012-10-15 20:35:56 +0000367bool AttrBuilder::hasAttributes() const {
Bill Wendling03198882013-01-04 23:27:34 +0000368 return !Attrs.empty();
Bill Wendlingf385f4c2012-10-08 23:27:46 +0000369}
Bill Wendling60507d52013-01-04 20:54:35 +0000370
Bill Wendling034b94b2012-12-19 07:18:57 +0000371bool AttrBuilder::hasAttributes(const Attribute &A) const {
Bill Wendling1db9b692013-01-09 23:36:50 +0000372 return Raw() & A.Raw();
Bill Wendling8831c062012-10-09 00:01:21 +0000373}
Bill Wendling60507d52013-01-04 20:54:35 +0000374
Bill Wendling702cc912012-10-15 20:35:56 +0000375bool AttrBuilder::hasAlignmentAttr() const {
Bill Wendling03198882013-01-04 23:27:34 +0000376 return Alignment != 0;
Bill Wendlingf385f4c2012-10-08 23:27:46 +0000377}
378
Bill Wendling1db9b692013-01-09 23:36:50 +0000379uint64_t AttrBuilder::Raw() const {
Bill Wendling03198882013-01-04 23:27:34 +0000380 uint64_t Mask = 0;
381
382 for (DenseSet<Attribute::AttrKind>::const_iterator I = Attrs.begin(),
383 E = Attrs.end(); I != E; ++I) {
384 Attribute::AttrKind Kind = *I;
385
386 if (Kind == Attribute::Alignment)
387 Mask |= (Log2_32(Alignment) + 1) << 16;
388 else if (Kind == Attribute::StackAlignment)
389 Mask |= (Log2_32(StackAlignment) + 1) << 26;
390 else
391 Mask |= AttributeImpl::getAttrMask(Kind);
392 }
393
394 return Mask;
Bill Wendlingf385f4c2012-10-08 23:27:46 +0000395}
396
Bill Wendling03198882013-01-04 23:27:34 +0000397bool AttrBuilder::operator==(const AttrBuilder &B) {
398 SmallVector<Attribute::AttrKind, 8> This(Attrs.begin(), Attrs.end());
399 SmallVector<Attribute::AttrKind, 8> That(B.Attrs.begin(), B.Attrs.end());
400 return This == That;
Bill Wendling7d2f2492012-10-10 07:36:45 +0000401}
402
Chris Lattner58d74912008-03-12 17:45:29 +0000403//===----------------------------------------------------------------------===//
Bill Wendling2c79ecb2012-09-26 21:07:29 +0000404// AttributeImpl Definition
405//===----------------------------------------------------------------------===//
406
Bill Wendling1bbd6442013-01-05 01:36:54 +0000407AttributeImpl::AttributeImpl(LLVMContext &C, uint64_t data)
408 : Context(C) {
Bill Wendling7c1683d2012-12-29 12:29:38 +0000409 Data = ConstantInt::get(Type::getInt64Ty(C), data);
410}
Bill Wendling1bbd6442013-01-05 01:36:54 +0000411AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind data)
412 : Context(C) {
Bill Wendling979aff62012-12-30 02:22:16 +0000413 Data = ConstantInt::get(Type::getInt64Ty(C), data);
414}
415AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind data,
Bill Wendling1bbd6442013-01-05 01:36:54 +0000416 ArrayRef<Constant*> values)
417 : Context(C) {
Bill Wendling979aff62012-12-30 02:22:16 +0000418 Data = ConstantInt::get(Type::getInt64Ty(C), data);
419 Vals.reserve(values.size());
420 Vals.append(values.begin(), values.end());
421}
Bill Wendling1bbd6442013-01-05 01:36:54 +0000422AttributeImpl::AttributeImpl(LLVMContext &C, StringRef data)
423 : Context(C) {
Bill Wendling979aff62012-12-30 02:22:16 +0000424 Data = ConstantDataArray::getString(C, data);
425}
Bill Wendling7c1683d2012-12-29 12:29:38 +0000426
Bill Wendling60507d52013-01-04 20:54:35 +0000427bool AttributeImpl::operator==(Attribute::AttrKind Kind) const {
Bill Wendling529ec712012-12-30 01:38:39 +0000428 if (ConstantInt *CI = dyn_cast<ConstantInt>(Data))
429 return CI->getZExtValue() == Kind;
430 return false;
431}
Bill Wendling60507d52013-01-04 20:54:35 +0000432bool AttributeImpl::operator!=(Attribute::AttrKind Kind) const {
433 return !(*this == Kind);
434}
Bill Wendling529ec712012-12-30 01:38:39 +0000435
Bill Wendling60507d52013-01-04 20:54:35 +0000436bool AttributeImpl::operator==(StringRef Kind) const {
Bill Wendling529ec712012-12-30 01:38:39 +0000437 if (ConstantDataArray *CDA = dyn_cast<ConstantDataArray>(Data))
438 if (CDA->isString())
439 return CDA->getAsString() == Kind;
440 return false;
441}
Bill Wendling3467e302013-01-24 00:06:56 +0000442
Bill Wendling60507d52013-01-04 20:54:35 +0000443bool AttributeImpl::operator!=(StringRef Kind) const {
444 return !(*this == Kind);
445}
Bill Wendling529ec712012-12-30 01:38:39 +0000446
Bill Wendling3467e302013-01-24 00:06:56 +0000447bool AttributeImpl::operator<(const AttributeImpl &AI) const {
448 if (!Data && !AI.Data) return false;
449 if (!Data && AI.Data) return true;
450 if (Data && !AI.Data) return false;
451
452 ConstantInt *ThisCI = dyn_cast<ConstantInt>(Data);
453 ConstantInt *ThatCI = dyn_cast<ConstantInt>(AI.Data);
454
455 ConstantDataArray *ThisCDA = dyn_cast<ConstantDataArray>(Data);
456 ConstantDataArray *ThatCDA = dyn_cast<ConstantDataArray>(AI.Data);
457
458 if (ThisCI && ThatCI)
459 return ThisCI->getZExtValue() < ThatCI->getZExtValue();
460
461 if (ThisCI && ThatCDA)
462 return true;
463
464 if (ThisCDA && ThatCI)
465 return false;
466
467 return ThisCDA->getAsString() < ThatCDA->getAsString();
468}
469
Bill Wendling1db9b692013-01-09 23:36:50 +0000470uint64_t AttributeImpl::Raw() const {
Bill Wendling529ec712012-12-30 01:38:39 +0000471 // FIXME: Remove this.
Bill Wendling7c1683d2012-12-29 12:29:38 +0000472 return cast<ConstantInt>(Data)->getZExtValue();
473}
474
Bill Wendling60507d52013-01-04 20:54:35 +0000475uint64_t AttributeImpl::getAttrMask(Attribute::AttrKind Val) {
Bill Wendling67658342012-10-09 07:45:08 +0000476 switch (Val) {
Chandler Carruth6f78fbb2013-01-05 08:47:26 +0000477 case Attribute::EndAttrKinds:
478 case Attribute::AttrKindEmptyKey:
479 case Attribute::AttrKindTombstoneKey:
480 llvm_unreachable("Synthetic enumerators which should never get here");
481
Bill Wendling034b94b2012-12-19 07:18:57 +0000482 case Attribute::None: return 0;
483 case Attribute::ZExt: return 1 << 0;
484 case Attribute::SExt: return 1 << 1;
485 case Attribute::NoReturn: return 1 << 2;
486 case Attribute::InReg: return 1 << 3;
487 case Attribute::StructRet: return 1 << 4;
488 case Attribute::NoUnwind: return 1 << 5;
489 case Attribute::NoAlias: return 1 << 6;
490 case Attribute::ByVal: return 1 << 7;
491 case Attribute::Nest: return 1 << 8;
492 case Attribute::ReadNone: return 1 << 9;
493 case Attribute::ReadOnly: return 1 << 10;
494 case Attribute::NoInline: return 1 << 11;
495 case Attribute::AlwaysInline: return 1 << 12;
496 case Attribute::OptimizeForSize: return 1 << 13;
497 case Attribute::StackProtect: return 1 << 14;
498 case Attribute::StackProtectReq: return 1 << 15;
499 case Attribute::Alignment: return 31 << 16;
500 case Attribute::NoCapture: return 1 << 21;
501 case Attribute::NoRedZone: return 1 << 22;
502 case Attribute::NoImplicitFloat: return 1 << 23;
503 case Attribute::Naked: return 1 << 24;
504 case Attribute::InlineHint: return 1 << 25;
505 case Attribute::StackAlignment: return 7 << 26;
506 case Attribute::ReturnsTwice: return 1 << 29;
507 case Attribute::UWTable: return 1 << 30;
508 case Attribute::NonLazyBind: return 1U << 31;
509 case Attribute::AddressSafety: return 1ULL << 32;
510 case Attribute::MinSize: return 1ULL << 33;
James Molloy67ae1352012-12-20 16:04:27 +0000511 case Attribute::NoDuplicate: return 1ULL << 34;
Bill Wendling114baee2013-01-23 06:41:41 +0000512 case Attribute::StackProtectStrong: return 1ULL << 35;
Bill Wendling67658342012-10-09 07:45:08 +0000513 }
514 llvm_unreachable("Unsupported attribute type");
515}
516
Bill Wendling60507d52013-01-04 20:54:35 +0000517bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
Bill Wendling1db9b692013-01-09 23:36:50 +0000518 return (Raw() & getAttrMask(A)) != 0;
Bill Wendling8e635db2012-10-08 21:47:17 +0000519}
Bill Wendling2c79ecb2012-09-26 21:07:29 +0000520
Bill Wendlingf6670722012-12-20 01:36:59 +0000521bool AttributeImpl::hasAttributes() const {
Bill Wendling1db9b692013-01-09 23:36:50 +0000522 return Raw() != 0;
Bill Wendling8e635db2012-10-08 21:47:17 +0000523}
Bill Wendling2c79ecb2012-09-26 21:07:29 +0000524
Bill Wendlingf6670722012-12-20 01:36:59 +0000525uint64_t AttributeImpl::getAlignment() const {
Bill Wendlinga8ab5fc2013-01-23 23:00:05 +0000526 uint64_t Mask = Raw() & getAttrMask(Attribute::Alignment);
527 return 1U << ((Mask >> 16) - 1);
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::getStackAlignment() const {
Bill Wendlinga8ab5fc2013-01-23 23:00:05 +0000531 uint64_t Mask = Raw() & getAttrMask(Attribute::StackAlignment);
532 return 1U << ((Mask >> 26) - 1);
Bill Wendling8e635db2012-10-08 21:47:17 +0000533}
Bill Wendling2c79ecb2012-09-26 21:07:29 +0000534
Bill Wendling8456efb2013-01-09 00:32:55 +0000535void AttributeImpl::Profile(FoldingSetNodeID &ID, Constant *Data,
536 ArrayRef<Constant*> Vals) {
Bill Wendlingff887162013-01-09 00:32:08 +0000537 ID.AddInteger(cast<ConstantInt>(Data)->getZExtValue());
Bill Wendling1db9b692013-01-09 23:36:50 +0000538#if 0
539 // FIXME: Not yet supported.
Bill Wendlingff887162013-01-09 00:32:08 +0000540 for (ArrayRef<Constant*>::iterator I = Vals.begin(), E = Vals.end();
541 I != E; ++I)
542 ID.AddPointer(*I);
Bill Wendling1db9b692013-01-09 23:36:50 +0000543#endif
Bill Wendlingff887162013-01-09 00:32:08 +0000544}
545
Bill Wendling2c79ecb2012-09-26 21:07:29 +0000546//===----------------------------------------------------------------------===//
Bill Wendlingc5f1bc82013-01-21 21:57:28 +0000547// AttributeWithIndex Definition
548//===----------------------------------------------------------------------===//
549
550AttributeWithIndex AttributeWithIndex::get(LLVMContext &C, unsigned Idx,
551 AttributeSet AS) {
552 // FIXME: This is temporary, but necessary for the conversion.
553 AttrBuilder B(AS, Idx);
554 return get(Idx, Attribute::get(C, B));
555}
556
557//===----------------------------------------------------------------------===//
Bill Wendling3467e302013-01-24 00:06:56 +0000558// AttributeSetNode Definition
559//===----------------------------------------------------------------------===//
560
561AttributeSetNode *AttributeSetNode::get(LLVMContext &C,
562 ArrayRef<Attribute> Attrs) {
563 if (Attrs.empty())
564 return 0;
565
566 // Otherwise, build a key to look up the existing attributes.
567 LLVMContextImpl *pImpl = C.pImpl;
568 FoldingSetNodeID ID;
569
570 SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end());
571 std::sort(SortedAttrs.begin(), SortedAttrs.end());
572
573 for (SmallVectorImpl<Attribute>::iterator I = SortedAttrs.begin(),
574 E = SortedAttrs.end(); I != E; ++I)
575 I->Profile(ID);
576
577 void *InsertPoint;
578 AttributeSetNode *PA =
579 pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint);
580
581 // If we didn't find any existing attributes of the same shape then create a
582 // new one and insert it.
583 if (!PA) {
584 PA = new AttributeSetNode(SortedAttrs);
585 pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint);
586 }
587
588 // Return the AttributesListNode that we found or created.
589 return PA;
590}
591
592//===----------------------------------------------------------------------===//
Bill Wendling18e72112012-12-19 22:42:22 +0000593// AttributeSetImpl Definition
Chris Lattner58d74912008-03-12 17:45:29 +0000594//===----------------------------------------------------------------------===//
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000595
Bill Wendling28d65722013-01-23 06:14:59 +0000596AttributeSet AttributeSet::getParamAttributes(unsigned Idx) const {
597 // FIXME: Remove.
598 return AttrList && hasAttributes(Idx) ?
599 AttributeSet::get(AttrList->getContext(),
600 AttributeWithIndex::get(Idx, getAttributes(Idx))) :
601 AttributeSet();
602}
603
Bill Wendling3fc4b962013-01-21 22:44:49 +0000604AttributeSet AttributeSet::getRetAttributes() const {
605 // FIXME: Remove.
606 return AttrList && hasAttributes(ReturnIndex) ?
607 AttributeSet::get(AttrList->getContext(),
608 AttributeWithIndex::get(ReturnIndex,
609 getAttributes(ReturnIndex))) :
610 AttributeSet();
611}
612
Bill Wendlingc5f1bc82013-01-21 21:57:28 +0000613AttributeSet AttributeSet::getFnAttributes() const {
614 // FIXME: Remove.
Bill Wendling3fc4b962013-01-21 22:44:49 +0000615 return AttrList && hasAttributes(FunctionIndex) ?
Bill Wendlingc5f1bc82013-01-21 21:57:28 +0000616 AttributeSet::get(AttrList->getContext(),
617 AttributeWithIndex::get(FunctionIndex,
618 getAttributes(FunctionIndex))) :
619 AttributeSet();
620}
621
Bill Wendling99faa3b2012-12-07 23:16:57 +0000622AttributeSet AttributeSet::get(LLVMContext &C,
Bill Wendling1ce47ac2012-12-12 19:21:53 +0000623 ArrayRef<AttributeWithIndex> Attrs) {
Devang Patel05988662008-09-25 21:00:45 +0000624 // If there are no attributes then return a null AttributesList pointer.
Chris Lattnerd509d0b2012-05-28 01:47:44 +0000625 if (Attrs.empty())
Bill Wendling99faa3b2012-12-07 23:16:57 +0000626 return AttributeSet();
Bill Wendling77898682012-10-16 06:01:44 +0000627
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000628#ifndef NDEBUG
Chris Lattnerd509d0b2012-05-28 01:47:44 +0000629 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
Bill Wendling77898682012-10-16 06:01:44 +0000630 assert(Attrs[i].Attrs.hasAttributes() &&
Devang Patel05988662008-09-25 21:00:45 +0000631 "Pointless attribute!");
Chris Lattner58d74912008-03-12 17:45:29 +0000632 assert((!i || Attrs[i-1].Index < Attrs[i].Index) &&
Devang Patel05988662008-09-25 21:00:45 +0000633 "Misordered AttributesList!");
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000634 }
635#endif
Bill Wendling77898682012-10-16 06:01:44 +0000636
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000637 // Otherwise, build a key to look up the existing attributes.
Bill Wendling0976e002012-11-20 05:09:20 +0000638 LLVMContextImpl *pImpl = C.pImpl;
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000639 FoldingSetNodeID ID;
Bill Wendling18e72112012-12-19 22:42:22 +0000640 AttributeSetImpl::Profile(ID, Attrs);
Bill Wendling77898682012-10-16 06:01:44 +0000641
Bill Wendling0976e002012-11-20 05:09:20 +0000642 void *InsertPoint;
Bill Wendling1bbd6442013-01-05 01:36:54 +0000643 AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
Bill Wendling77898682012-10-16 06:01:44 +0000644
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000645 // If we didn't find any existing attributes of the same shape then
646 // create a new one and insert it.
Bill Wendling0976e002012-11-20 05:09:20 +0000647 if (!PA) {
Bill Wendling5f93e2b2012-12-19 23:55:43 +0000648 PA = new AttributeSetImpl(C, Attrs);
Bill Wendling0976e002012-11-20 05:09:20 +0000649 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000650 }
Bill Wendling77898682012-10-16 06:01:44 +0000651
Devang Patel05988662008-09-25 21:00:45 +0000652 // Return the AttributesList that we found or created.
Bill Wendling99faa3b2012-12-07 23:16:57 +0000653 return AttributeSet(PA);
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000654}
655
Bill Wendling1bbd6442013-01-05 01:36:54 +0000656AttributeSet AttributeSet::get(LLVMContext &C, unsigned Idx, AttrBuilder &B) {
Bill Wendling3fc4b962013-01-21 22:44:49 +0000657 // FIXME: This should be implemented as a loop that creates the
658 // AttributeWithIndexes that then are used to create the AttributeSet.
659 if (!B.hasAttributes())
660 return AttributeSet();
Bill Wendlingdefaca02013-01-22 21:15:51 +0000661 return get(C, AttributeWithIndex::get(Idx, Attribute::get(C, B)));
Bill Wendling1bbd6442013-01-05 01:36:54 +0000662}
663
Bill Wendling28d65722013-01-23 06:14:59 +0000664AttributeSet AttributeSet::get(LLVMContext &C, unsigned Idx,
665 Attribute::AttrKind Kind) {
666 return get(C, AttributeWithIndex::get(Idx, Attribute::get(C, Kind)));
667}
668
Chris Lattner58d74912008-03-12 17:45:29 +0000669//===----------------------------------------------------------------------===//
Bill Wendling99faa3b2012-12-07 23:16:57 +0000670// AttributeSet Method Implementations
Chris Lattner58d74912008-03-12 17:45:29 +0000671//===----------------------------------------------------------------------===//
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000672
Bill Wendling99faa3b2012-12-07 23:16:57 +0000673const AttributeSet &AttributeSet::operator=(const AttributeSet &RHS) {
Bill Wendling0976e002012-11-20 05:09:20 +0000674 AttrList = RHS.AttrList;
675 return *this;
Chris Lattner58d74912008-03-12 17:45:29 +0000676}
677
Bill Wendling77898682012-10-16 06:01:44 +0000678/// getNumSlots - Return the number of slots used in this attribute list.
Chris Lattner58d74912008-03-12 17:45:29 +0000679/// This is the number of arguments that have an attribute set on them
680/// (including the function itself).
Bill Wendling99faa3b2012-12-07 23:16:57 +0000681unsigned AttributeSet::getNumSlots() const {
Bill Wendling60507d52013-01-04 20:54:35 +0000682 return AttrList ? AttrList->getNumAttributes() : 0;
Chris Lattner58d74912008-03-12 17:45:29 +0000683}
684
Devang Patel05988662008-09-25 21:00:45 +0000685/// getSlot - Return the AttributeWithIndex at the specified slot. This
686/// holds a number plus a set of attributes.
Bill Wendling99faa3b2012-12-07 23:16:57 +0000687const AttributeWithIndex &AttributeSet::getSlot(unsigned Slot) const {
Bill Wendling60507d52013-01-04 20:54:35 +0000688 assert(AttrList && Slot < AttrList->getNumAttributes() &&
689 "Slot # out of range!");
690 return AttrList->getAttributes()[Slot];
Chris Lattner58d74912008-03-12 17:45:29 +0000691}
692
Bill Wendling831737d2012-12-30 10:32:01 +0000693bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{
694 return getAttributes(Index).hasAttribute(Kind);
695}
696
697bool AttributeSet::hasAttributes(unsigned Index) const {
698 return getAttributes(Index).hasAttributes();
699}
700
701std::string AttributeSet::getAsString(unsigned Index) const {
702 return getAttributes(Index).getAsString();
703}
704
Bill Wendling956f1342013-01-18 21:11:39 +0000705unsigned AttributeSet::getParamAlignment(unsigned Idx) const {
706 return getAttributes(Idx).getAlignment();
707}
708
Bill Wendling831737d2012-12-30 10:32:01 +0000709unsigned AttributeSet::getStackAlignment(unsigned Index) const {
710 return getAttributes(Index).getStackAlignment();
711}
712
Bill Wendling1db9b692013-01-09 23:36:50 +0000713uint64_t AttributeSet::Raw(unsigned Index) const {
Bill Wendling831737d2012-12-30 10:32:01 +0000714 // FIXME: Remove this.
Bill Wendling1db9b692013-01-09 23:36:50 +0000715 return getAttributes(Index).Raw();
Bill Wendling831737d2012-12-30 10:32:01 +0000716}
717
Bill Wendling77898682012-10-16 06:01:44 +0000718/// getAttributes - The attributes for the specified index are returned.
Bill Wendling034b94b2012-12-19 07:18:57 +0000719Attribute AttributeSet::getAttributes(unsigned Idx) const {
720 if (AttrList == 0) return Attribute();
Bill Wendling77898682012-10-16 06:01:44 +0000721
Bill Wendling60507d52013-01-04 20:54:35 +0000722 ArrayRef<AttributeWithIndex> Attrs = AttrList->getAttributes();
Chris Lattner58d74912008-03-12 17:45:29 +0000723 for (unsigned i = 0, e = Attrs.size(); i != e && Attrs[i].Index <= Idx; ++i)
724 if (Attrs[i].Index == Idx)
725 return Attrs[i].Attrs;
Bill Wendlingef99fe82012-09-21 15:26:31 +0000726
Bill Wendling034b94b2012-12-19 07:18:57 +0000727 return Attribute();
Chris Lattner58d74912008-03-12 17:45:29 +0000728}
729
730/// hasAttrSomewhere - Return true if the specified attribute is set for at
731/// least one parameter or for the return value.
Bill Wendling629fb822012-12-22 00:37:52 +0000732bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr) const {
Devang Patel05988662008-09-25 21:00:45 +0000733 if (AttrList == 0) return false;
Bill Wendling7d2f2492012-10-10 07:36:45 +0000734
Bill Wendling60507d52013-01-04 20:54:35 +0000735 ArrayRef<AttributeWithIndex> Attrs = AttrList->getAttributes();
Chris Lattner58d74912008-03-12 17:45:29 +0000736 for (unsigned i = 0, e = Attrs.size(); i != e; ++i)
Bill Wendling7d2f2492012-10-10 07:36:45 +0000737 if (Attrs[i].Attrs.hasAttribute(Attr))
Chris Lattner58d74912008-03-12 17:45:29 +0000738 return true;
Bill Wendling0976e002012-11-20 05:09:20 +0000739
Chris Lattner58d74912008-03-12 17:45:29 +0000740 return false;
741}
742
Bill Wendlingdefaca02013-01-22 21:15:51 +0000743AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Idx,
744 Attribute::AttrKind Attr) const {
745 return addAttr(C, Idx, Attribute::get(C, Attr));
746}
747
Bill Wendlinge4e85f12013-01-22 00:53:12 +0000748AttributeSet AttributeSet::addAttributes(LLVMContext &C, unsigned Idx,
749 AttributeSet Attrs) const {
750 return addAttr(C, Idx, Attrs.getAttributes(Idx));
Bill Wendling956f1342013-01-18 21:11:39 +0000751}
752
Bill Wendling99faa3b2012-12-07 23:16:57 +0000753AttributeSet AttributeSet::addAttr(LLVMContext &C, unsigned Idx,
Bill Wendling9d30e722012-12-31 00:49:59 +0000754 Attribute Attrs) const {
Bill Wendling034b94b2012-12-19 07:18:57 +0000755 Attribute OldAttrs = getAttributes(Idx);
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000756#ifndef NDEBUG
757 // FIXME it is not obvious how this should work for alignment.
758 // For now, say we can't change a known alignment.
Bill Wendlingef99fe82012-09-21 15:26:31 +0000759 unsigned OldAlign = OldAttrs.getAlignment();
760 unsigned NewAlign = Attrs.getAlignment();
Anton Korobeynikov4aefd6b2008-02-20 12:07:57 +0000761 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000762 "Attempt to change alignment!");
763#endif
Bill Wendling77898682012-10-16 06:01:44 +0000764
Bill Wendling702cc912012-10-15 20:35:56 +0000765 AttrBuilder NewAttrs =
766 AttrBuilder(OldAttrs).addAttributes(Attrs);
767 if (NewAttrs == AttrBuilder(OldAttrs))
Chris Lattner58d74912008-03-12 17:45:29 +0000768 return *this;
Bill Wendling77898682012-10-16 06:01:44 +0000769
Devang Patel05988662008-09-25 21:00:45 +0000770 SmallVector<AttributeWithIndex, 8> NewAttrList;
771 if (AttrList == 0)
772 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Chris Lattner58d74912008-03-12 17:45:29 +0000773 else {
Bill Wendling60507d52013-01-04 20:54:35 +0000774 ArrayRef<AttributeWithIndex> OldAttrList = AttrList->getAttributes();
Chris Lattner58d74912008-03-12 17:45:29 +0000775 unsigned i = 0, e = OldAttrList.size();
776 // Copy attributes for arguments before this one.
777 for (; i != e && OldAttrList[i].Index < Idx; ++i)
778 NewAttrList.push_back(OldAttrList[i]);
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000779
Chris Lattner58d74912008-03-12 17:45:29 +0000780 // If there are attributes already at this index, merge them in.
781 if (i != e && OldAttrList[i].Index == Idx) {
Bill Wendlingc4167952012-10-14 07:35:59 +0000782 Attrs =
Bill Wendling034b94b2012-12-19 07:18:57 +0000783 Attribute::get(C, AttrBuilder(Attrs).
Bill Wendlingc4167952012-10-14 07:35:59 +0000784 addAttributes(OldAttrList[i].Attrs));
Chris Lattner58d74912008-03-12 17:45:29 +0000785 ++i;
786 }
Bill Wendling77898682012-10-16 06:01:44 +0000787
Devang Patel05988662008-09-25 21:00:45 +0000788 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Bill Wendling77898682012-10-16 06:01:44 +0000789
Chris Lattner58d74912008-03-12 17:45:29 +0000790 // Copy attributes for arguments after this one.
Bill Wendling77898682012-10-16 06:01:44 +0000791 NewAttrList.insert(NewAttrList.end(),
Chris Lattner58d74912008-03-12 17:45:29 +0000792 OldAttrList.begin()+i, OldAttrList.end());
793 }
Bill Wendling77898682012-10-16 06:01:44 +0000794
Bill Wendling0976e002012-11-20 05:09:20 +0000795 return get(C, NewAttrList);
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000796}
797
Bill Wendling8246df62013-01-23 00:45:55 +0000798AttributeSet AttributeSet::removeAttribute(LLVMContext &C, unsigned Idx,
799 Attribute::AttrKind Attr) const {
800 return removeAttr(C, Idx, Attribute::get(C, Attr));
801}
802
803AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Idx,
804 AttributeSet Attrs) const {
805 return removeAttr(C, Idx, Attrs.getAttributes(Idx));
806}
807
Bill Wendling99faa3b2012-12-07 23:16:57 +0000808AttributeSet AttributeSet::removeAttr(LLVMContext &C, unsigned Idx,
Bill Wendling9d30e722012-12-31 00:49:59 +0000809 Attribute Attrs) const {
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000810#ifndef NDEBUG
811 // FIXME it is not obvious how this should work for alignment.
812 // For now, say we can't pass in alignment, which no current use does.
Bill Wendling034b94b2012-12-19 07:18:57 +0000813 assert(!Attrs.hasAttribute(Attribute::Alignment) &&
Bill Wendling67658342012-10-09 07:45:08 +0000814 "Attempt to exclude alignment!");
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000815#endif
Bill Wendling99faa3b2012-12-07 23:16:57 +0000816 if (AttrList == 0) return AttributeSet();
Bill Wendling77898682012-10-16 06:01:44 +0000817
Bill Wendling034b94b2012-12-19 07:18:57 +0000818 Attribute OldAttrs = getAttributes(Idx);
Bill Wendling702cc912012-10-15 20:35:56 +0000819 AttrBuilder NewAttrs =
820 AttrBuilder(OldAttrs).removeAttributes(Attrs);
821 if (NewAttrs == AttrBuilder(OldAttrs))
Chris Lattner58d74912008-03-12 17:45:29 +0000822 return *this;
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000823
Devang Patel05988662008-09-25 21:00:45 +0000824 SmallVector<AttributeWithIndex, 8> NewAttrList;
Bill Wendling60507d52013-01-04 20:54:35 +0000825 ArrayRef<AttributeWithIndex> OldAttrList = AttrList->getAttributes();
Chris Lattner58d74912008-03-12 17:45:29 +0000826 unsigned i = 0, e = OldAttrList.size();
Bill Wendling77898682012-10-16 06:01:44 +0000827
Chris Lattner58d74912008-03-12 17:45:29 +0000828 // Copy attributes for arguments before this one.
829 for (; i != e && OldAttrList[i].Index < Idx; ++i)
830 NewAttrList.push_back(OldAttrList[i]);
Bill Wendling77898682012-10-16 06:01:44 +0000831
Chris Lattner58d74912008-03-12 17:45:29 +0000832 // If there are attributes already at this index, merge them in.
833 assert(OldAttrList[i].Index == Idx && "Attribute isn't set?");
Bill Wendling034b94b2012-12-19 07:18:57 +0000834 Attrs = Attribute::get(C, AttrBuilder(OldAttrList[i].Attrs).
Bill Wendling5886b7b2012-10-14 06:39:53 +0000835 removeAttributes(Attrs));
Chris Lattner58d74912008-03-12 17:45:29 +0000836 ++i;
Bill Wendling7be78482012-10-14 08:54:26 +0000837 if (Attrs.hasAttributes()) // If any attributes left for this param, add them.
Devang Patel05988662008-09-25 21:00:45 +0000838 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Bill Wendling77898682012-10-16 06:01:44 +0000839
Chris Lattner58d74912008-03-12 17:45:29 +0000840 // Copy attributes for arguments after this one.
Bill Wendling77898682012-10-16 06:01:44 +0000841 NewAttrList.insert(NewAttrList.end(),
Chris Lattner58d74912008-03-12 17:45:29 +0000842 OldAttrList.begin()+i, OldAttrList.end());
Bill Wendling77898682012-10-16 06:01:44 +0000843
Bill Wendling0976e002012-11-20 05:09:20 +0000844 return get(C, NewAttrList);
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000845}
846
Bill Wendling99faa3b2012-12-07 23:16:57 +0000847void AttributeSet::dump() const {
David Greeneef1894e2010-01-05 01:29:58 +0000848 dbgs() << "PAL[ ";
Chris Lattner58d74912008-03-12 17:45:29 +0000849 for (unsigned i = 0; i < getNumSlots(); ++i) {
Devang Patel05988662008-09-25 21:00:45 +0000850 const AttributeWithIndex &PAWI = getSlot(i);
Bill Wendling60507d52013-01-04 20:54:35 +0000851 dbgs() << "{ " << PAWI.Index << ", " << PAWI.Attrs.getAsString() << " } ";
Chris Lattner58d74912008-03-12 17:45:29 +0000852 }
Bill Wendling77898682012-10-16 06:01:44 +0000853
David Greeneef1894e2010-01-05 01:29:58 +0000854 dbgs() << "]\n";
Duncan Sandsad9a9e12008-01-06 18:27:01 +0000855}