blob: 65bdc75b0f632dfbd4ae78d4f28401c43c20b8e5 [file] [log] [blame]
Bill Wendlingec454542013-01-28 21:55:20 +00001//===-- Attributes.cpp - Implement AttributesList -------------------------===//
Chris Lattner3e13b8c2008-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 Wendlingec454542013-01-28 21:55:20 +000010// \file
11// \brief This file implements the Attribute, AttributeImpl, AttrBuilder,
Bill Wendling6848e382012-12-19 22:42:22 +000012// AttributeSetImpl, and AttributeSet classes.
Chris Lattner3e13b8c2008-01-02 23:42:30 +000013//
14//===----------------------------------------------------------------------===//
15
Chandler Carruth9fb823b2013-01-02 11:36:10 +000016#include "llvm/IR/Attributes.h"
Bill Wendling4607f4b2012-12-20 01:36:59 +000017#include "AttributeImpl.h"
Bill Wendlinge38b8042012-09-26 21:07:29 +000018#include "LLVMContextImpl.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000019#include "llvm/ADT/StringExtras.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000020#include "llvm/IR/Type.h"
Benjamin Kramer17388a62014-03-03 18:02:34 +000021#include "llvm/Support/Atomic.h"
David Greenef7014732010-01-05 01:29:58 +000022#include "llvm/Support/Debug.h"
Chris Lattner3e13b8c2008-01-02 23:42:30 +000023#include "llvm/Support/ManagedStatic.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000024#include "llvm/Support/Mutex.h"
Benjamin Kramer1a25d732009-08-23 11:37:21 +000025#include "llvm/Support/raw_ostream.h"
Bill Wendlingd2e493b2013-01-24 00:06:56 +000026#include <algorithm>
Chris Lattner3e13b8c2008-01-02 23:42:30 +000027using namespace llvm;
28
Chris Lattner8a923e72008-03-12 17:45:29 +000029//===----------------------------------------------------------------------===//
Bill Wendling7707c5a2013-01-29 00:48:16 +000030// Attribute Construction Methods
Chris Lattner8a923e72008-03-12 17:45:29 +000031//===----------------------------------------------------------------------===//
Chris Lattnerd0e1f102008-01-03 00:10:22 +000032
Bill Wendling3f12ac22013-02-05 22:37:24 +000033Attribute Attribute::get(LLVMContext &Context, Attribute::AttrKind Kind,
34 uint64_t Val) {
Bill Wendling73ea2de2012-10-08 21:47:17 +000035 LLVMContextImpl *pImpl = Context.pImpl;
36 FoldingSetNodeID ID;
Bill Wendling3f12ac22013-02-05 22:37:24 +000037 ID.AddInteger(Kind);
38 if (Val) ID.AddInteger(Val);
39
40 void *InsertPoint;
41 AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
42
43 if (!PA) {
44 // If we didn't find any existing attributes of the same shape then create a
45 // new one and insert it.
Benjamin Kramer741146b2013-07-11 12:13:16 +000046 if (!Val)
47 PA = new EnumAttributeImpl(Kind);
48 else
49 PA = new AlignAttributeImpl(Kind, Val);
Bill Wendling3f12ac22013-02-05 22:37:24 +000050 pImpl->AttrsSet.InsertNode(PA, InsertPoint);
51 }
52
53 // Return the Attribute that we found or created.
54 return Attribute(PA);
55}
56
57Attribute Attribute::get(LLVMContext &Context, StringRef Kind, StringRef Val) {
58 LLVMContextImpl *pImpl = Context.pImpl;
59 FoldingSetNodeID ID;
60 ID.AddString(Kind);
61 if (!Val.empty()) ID.AddString(Val);
Bill Wendling73ea2de2012-10-08 21:47:17 +000062
63 void *InsertPoint;
Bill Wendling4607f4b2012-12-20 01:36:59 +000064 AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
Bill Wendling73ea2de2012-10-08 21:47:17 +000065
66 if (!PA) {
67 // If we didn't find any existing attributes of the same shape then create a
68 // new one and insert it.
Benjamin Kramer741146b2013-07-11 12:13:16 +000069 PA = new StringAttributeImpl(Kind, Val);
Bill Wendling73ea2de2012-10-08 21:47:17 +000070 pImpl->AttrsSet.InsertNode(PA, InsertPoint);
71 }
72
Bill Wendlingb9c5b1a2013-02-05 08:09:32 +000073 // Return the Attribute that we found or created.
Bill Wendling3d7b0b82012-12-19 07:18:57 +000074 return Attribute(PA);
Bill Wendling73ea2de2012-10-08 21:47:17 +000075}
76
Bill Wendling4bbe9db2013-01-27 22:43:04 +000077Attribute Attribute::getWithAlignment(LLVMContext &Context, uint64_t Align) {
Bill Wendling1c7cc8a2013-01-31 23:16:25 +000078 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
79 assert(Align <= 0x40000000 && "Alignment too large.");
Bill Wendling3f12ac22013-02-05 22:37:24 +000080 return get(Context, Alignment, Align);
Bill Wendling4bbe9db2013-01-27 22:43:04 +000081}
82
83Attribute Attribute::getWithStackAlignment(LLVMContext &Context,
84 uint64_t Align) {
Bill Wendling1c7cc8a2013-01-31 23:16:25 +000085 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
86 assert(Align <= 0x100 && "Alignment too large.");
Bill Wendling3f12ac22013-02-05 22:37:24 +000087 return get(Context, StackAlignment, Align);
Bill Wendling4bbe9db2013-01-27 22:43:04 +000088}
89
Bill Wendling7707c5a2013-01-29 00:48:16 +000090//===----------------------------------------------------------------------===//
91// Attribute Accessor Methods
92//===----------------------------------------------------------------------===//
93
Bill Wendling3f12ac22013-02-05 22:37:24 +000094bool Attribute::isEnumAttribute() const {
95 return pImpl && pImpl->isEnumAttribute();
96}
97
98bool Attribute::isAlignAttribute() const {
99 return pImpl && pImpl->isAlignAttribute();
100}
101
102bool Attribute::isStringAttribute() const {
103 return pImpl && pImpl->isStringAttribute();
104}
105
106Attribute::AttrKind Attribute::getKindAsEnum() const {
Bill Wendling440e9d82013-07-25 00:34:29 +0000107 if (!pImpl) return None;
Bill Wendling3f12ac22013-02-05 22:37:24 +0000108 assert((isEnumAttribute() || isAlignAttribute()) &&
109 "Invalid attribute type to get the kind as an enum!");
110 return pImpl ? pImpl->getKindAsEnum() : None;
111}
112
113uint64_t Attribute::getValueAsInt() const {
Bill Wendling440e9d82013-07-25 00:34:29 +0000114 if (!pImpl) return 0;
Bill Wendling3f12ac22013-02-05 22:37:24 +0000115 assert(isAlignAttribute() &&
116 "Expected the attribute to be an alignment attribute!");
117 return pImpl ? pImpl->getValueAsInt() : 0;
118}
119
120StringRef Attribute::getKindAsString() const {
Bill Wendling440e9d82013-07-25 00:34:29 +0000121 if (!pImpl) return StringRef();
Bill Wendling3f12ac22013-02-05 22:37:24 +0000122 assert(isStringAttribute() &&
123 "Invalid attribute type to get the kind as a string!");
124 return pImpl ? pImpl->getKindAsString() : StringRef();
125}
126
127StringRef Attribute::getValueAsString() const {
Bill Wendling440e9d82013-07-25 00:34:29 +0000128 if (!pImpl) return StringRef();
Bill Wendling3f12ac22013-02-05 22:37:24 +0000129 assert(isStringAttribute() &&
130 "Invalid attribute type to get the value as a string!");
131 return pImpl ? pImpl->getValueAsString() : StringRef();
132}
133
Bill Wendlingae89a0f2013-02-05 23:48:36 +0000134bool Attribute::hasAttribute(AttrKind Kind) const {
135 return (pImpl && pImpl->hasAttribute(Kind)) || (!pImpl && Kind == None);
136}
137
138bool Attribute::hasAttribute(StringRef Kind) const {
139 if (!isStringAttribute()) return false;
140 return pImpl && pImpl->hasAttribute(Kind);
Bill Wendling03eefb32013-01-29 20:45:34 +0000141}
142
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000143/// This returns the alignment field of an attribute as a byte alignment value.
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000144unsigned Attribute::getAlignment() const {
Bill Wendlingc79cdff2013-02-01 01:04:27 +0000145 assert(hasAttribute(Attribute::Alignment) &&
146 "Trying to get alignment from non-alignment attribute!");
Bill Wendling3f12ac22013-02-05 22:37:24 +0000147 return pImpl->getValueAsInt();
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000148}
149
150/// This returns the stack alignment field of an attribute as a byte alignment
151/// value.
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000152unsigned Attribute::getStackAlignment() const {
Bill Wendlingc79cdff2013-02-01 01:04:27 +0000153 assert(hasAttribute(Attribute::StackAlignment) &&
154 "Trying to get alignment from non-alignment attribute!");
Bill Wendling3f12ac22013-02-05 22:37:24 +0000155 return pImpl->getValueAsInt();
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000156}
157
Bill Wendling829b4782013-02-11 08:43:33 +0000158std::string Attribute::getAsString(bool InAttrGrp) const {
Bill Wendling9c2eba92013-01-31 20:59:05 +0000159 if (!pImpl) return "";
160
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000161 if (hasAttribute(Attribute::SanitizeAddress))
162 return "sanitize_address";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000163 if (hasAttribute(Attribute::AlwaysInline))
164 return "alwaysinline";
Michael Gottesman41748d72013-06-27 00:25:01 +0000165 if (hasAttribute(Attribute::Builtin))
166 return "builtin";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000167 if (hasAttribute(Attribute::ByVal))
168 return "byval";
Reid Klecknera534a382013-12-19 02:14:12 +0000169 if (hasAttribute(Attribute::InAlloca))
170 return "inalloca";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000171 if (hasAttribute(Attribute::InlineHint))
172 return "inlinehint";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000173 if (hasAttribute(Attribute::InReg))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000174 return "inreg";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000175 if (hasAttribute(Attribute::MinSize))
176 return "minsize";
177 if (hasAttribute(Attribute::Naked))
178 return "naked";
179 if (hasAttribute(Attribute::Nest))
180 return "nest";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000181 if (hasAttribute(Attribute::NoAlias))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000182 return "noalias";
Bill Wendling09bd1f72013-02-22 00:12:35 +0000183 if (hasAttribute(Attribute::NoBuiltin))
184 return "nobuiltin";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000185 if (hasAttribute(Attribute::NoCapture))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000186 return "nocapture";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000187 if (hasAttribute(Attribute::NoDuplicate))
188 return "noduplicate";
189 if (hasAttribute(Attribute::NoImplicitFloat))
190 return "noimplicitfloat";
191 if (hasAttribute(Attribute::NoInline))
192 return "noinline";
193 if (hasAttribute(Attribute::NonLazyBind))
194 return "nonlazybind";
195 if (hasAttribute(Attribute::NoRedZone))
196 return "noredzone";
197 if (hasAttribute(Attribute::NoReturn))
198 return "noreturn";
199 if (hasAttribute(Attribute::NoUnwind))
200 return "nounwind";
Andrea Di Biagio377496b2013-08-23 11:53:55 +0000201 if (hasAttribute(Attribute::OptimizeNone))
202 return "optnone";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000203 if (hasAttribute(Attribute::OptimizeForSize))
204 return "optsize";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000205 if (hasAttribute(Attribute::ReadNone))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000206 return "readnone";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000207 if (hasAttribute(Attribute::ReadOnly))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000208 return "readonly";
Stephen Linb8bd2322013-04-20 05:14:40 +0000209 if (hasAttribute(Attribute::Returned))
210 return "returned";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000211 if (hasAttribute(Attribute::ReturnsTwice))
212 return "returns_twice";
213 if (hasAttribute(Attribute::SExt))
214 return "signext";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000215 if (hasAttribute(Attribute::StackProtect))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000216 return "ssp";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000217 if (hasAttribute(Attribute::StackProtectReq))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000218 return "sspreq";
Bill Wendlingd154e2832013-01-23 06:41:41 +0000219 if (hasAttribute(Attribute::StackProtectStrong))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000220 return "sspstrong";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000221 if (hasAttribute(Attribute::StructRet))
222 return "sret";
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000223 if (hasAttribute(Attribute::SanitizeThread))
224 return "sanitize_thread";
225 if (hasAttribute(Attribute::SanitizeMemory))
226 return "sanitize_memory";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000227 if (hasAttribute(Attribute::UWTable))
228 return "uwtable";
229 if (hasAttribute(Attribute::ZExt))
230 return "zeroext";
Diego Novilloc6399532013-05-24 12:26:52 +0000231 if (hasAttribute(Attribute::Cold))
232 return "cold";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000233
234 // FIXME: These should be output like this:
235 //
236 // align=4
237 // alignstack=8
238 //
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000239 if (hasAttribute(Attribute::Alignment)) {
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000240 std::string Result;
Bill Wendling829b4782013-02-11 08:43:33 +0000241 Result += "align";
242 Result += (InAttrGrp) ? "=" : " ";
Bill Wendling3f12ac22013-02-05 22:37:24 +0000243 Result += utostr(getValueAsInt());
244 return Result;
245 }
Bill Wendling829b4782013-02-11 08:43:33 +0000246
Bill Wendling3f12ac22013-02-05 22:37:24 +0000247 if (hasAttribute(Attribute::StackAlignment)) {
248 std::string Result;
Bill Wendling829b4782013-02-11 08:43:33 +0000249 Result += "alignstack";
250 if (InAttrGrp) {
251 Result += "=";
252 Result += utostr(getValueAsInt());
253 } else {
254 Result += "(";
255 Result += utostr(getValueAsInt());
256 Result += ")";
257 }
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000258 return Result;
Dale Johannesen11a555e2008-02-19 23:51:49 +0000259 }
Bill Wendling9c2eba92013-01-31 20:59:05 +0000260
261 // Convert target-dependent attributes to strings of the form:
262 //
263 // "kind"
264 // "kind" = "value"
Bill Wendling9c2eba92013-01-31 20:59:05 +0000265 //
Bill Wendling3f12ac22013-02-05 22:37:24 +0000266 if (isStringAttribute()) {
Bill Wendling9c2eba92013-01-31 20:59:05 +0000267 std::string Result;
Bill Wendling3f12ac22013-02-05 22:37:24 +0000268 Result += '\"' + getKindAsString().str() + '"';
Bill Wendling9c2eba92013-01-31 20:59:05 +0000269
Bill Wendling3f12ac22013-02-05 22:37:24 +0000270 StringRef Val = pImpl->getValueAsString();
271 if (Val.empty()) return Result;
Bill Wendling7a33f772013-02-01 22:32:30 +0000272
Bill Wendling829b4782013-02-11 08:43:33 +0000273 Result += "=\"" + Val.str() + '"';
Bill Wendlingc79cdff2013-02-01 01:04:27 +0000274 return Result;
Bill Wendling9c2eba92013-01-31 20:59:05 +0000275 }
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000276
277 llvm_unreachable("Unknown attribute");
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000278}
279
Bill Wendlingd509a662013-01-29 00:34:06 +0000280bool Attribute::operator<(Attribute A) const {
281 if (!pImpl && !A.pImpl) return false;
282 if (!pImpl) return true;
283 if (!A.pImpl) return false;
284 return *pImpl < *A.pImpl;
285}
286
Bill Wendlingd509a662013-01-29 00:34:06 +0000287//===----------------------------------------------------------------------===//
288// AttributeImpl Definition
289//===----------------------------------------------------------------------===//
290
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +0000291// Pin the vtabels to this file.
Alexey Samsonov49109a22013-11-18 09:31:53 +0000292AttributeImpl::~AttributeImpl() {}
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +0000293void EnumAttributeImpl::anchor() {}
294void AlignAttributeImpl::anchor() {}
295void StringAttributeImpl::anchor() {}
Alexey Samsonov49109a22013-11-18 09:31:53 +0000296
Bill Wendlingd509a662013-01-29 00:34:06 +0000297bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
Bill Wendling3f12ac22013-02-05 22:37:24 +0000298 if (isStringAttribute()) return false;
299 return getKindAsEnum() == A;
Bill Wendlingd509a662013-01-29 00:34:06 +0000300}
301
Bill Wendling3f12ac22013-02-05 22:37:24 +0000302bool AttributeImpl::hasAttribute(StringRef Kind) const {
303 if (!isStringAttribute()) return false;
304 return getKindAsString() == Kind;
Bill Wendlingd509a662013-01-29 00:34:06 +0000305}
306
Bill Wendling3f12ac22013-02-05 22:37:24 +0000307Attribute::AttrKind AttributeImpl::getKindAsEnum() const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000308 assert(isEnumAttribute() || isAlignAttribute());
309 return static_cast<const EnumAttributeImpl *>(this)->getEnumKind();
Bill Wendlingd509a662013-01-29 00:34:06 +0000310}
311
Bill Wendling3f12ac22013-02-05 22:37:24 +0000312uint64_t AttributeImpl::getValueAsInt() const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000313 assert(isAlignAttribute());
314 return static_cast<const AlignAttributeImpl *>(this)->getAlignment();
Bill Wendlingd509a662013-01-29 00:34:06 +0000315}
316
Bill Wendling3f12ac22013-02-05 22:37:24 +0000317StringRef AttributeImpl::getKindAsString() const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000318 assert(isStringAttribute());
319 return static_cast<const StringAttributeImpl *>(this)->getStringKind();
Bill Wendlingd509a662013-01-29 00:34:06 +0000320}
321
Bill Wendling3f12ac22013-02-05 22:37:24 +0000322StringRef AttributeImpl::getValueAsString() const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000323 assert(isStringAttribute());
324 return static_cast<const StringAttributeImpl *>(this)->getStringValue();
Bill Wendlingd509a662013-01-29 00:34:06 +0000325}
326
327bool AttributeImpl::operator<(const AttributeImpl &AI) const {
Bill Wendlingc79cdff2013-02-01 01:04:27 +0000328 // This sorts the attributes with Attribute::AttrKinds coming first (sorted
329 // relative to their enum value) and then strings.
Bill Wendling26b95752013-02-15 05:25:26 +0000330 if (isEnumAttribute()) {
331 if (AI.isEnumAttribute()) return getKindAsEnum() < AI.getKindAsEnum();
332 if (AI.isAlignAttribute()) return true;
333 if (AI.isStringAttribute()) return true;
334 }
Bill Wendlingc79cdff2013-02-01 01:04:27 +0000335
Bill Wendling3f12ac22013-02-05 22:37:24 +0000336 if (isAlignAttribute()) {
Bill Wendling26b95752013-02-15 05:25:26 +0000337 if (AI.isEnumAttribute()) return false;
338 if (AI.isAlignAttribute()) return getValueAsInt() < AI.getValueAsInt();
339 if (AI.isStringAttribute()) return true;
Bill Wendling3f12ac22013-02-05 22:37:24 +0000340 }
Bill Wendlingd509a662013-01-29 00:34:06 +0000341
Bill Wendling26b95752013-02-15 05:25:26 +0000342 if (AI.isEnumAttribute()) return false;
343 if (AI.isAlignAttribute()) return false;
344 if (getKindAsString() == AI.getKindAsString())
345 return getValueAsString() < AI.getValueAsString();
346 return getKindAsString() < AI.getKindAsString();
Bill Wendlingd509a662013-01-29 00:34:06 +0000347}
348
Bill Wendlingd509a662013-01-29 00:34:06 +0000349uint64_t AttributeImpl::getAttrMask(Attribute::AttrKind Val) {
350 // FIXME: Remove this.
351 switch (Val) {
352 case Attribute::EndAttrKinds:
Bill Wendlingd509a662013-01-29 00:34:06 +0000353 llvm_unreachable("Synthetic enumerators which should never get here");
354
355 case Attribute::None: return 0;
356 case Attribute::ZExt: return 1 << 0;
357 case Attribute::SExt: return 1 << 1;
358 case Attribute::NoReturn: return 1 << 2;
359 case Attribute::InReg: return 1 << 3;
360 case Attribute::StructRet: return 1 << 4;
361 case Attribute::NoUnwind: return 1 << 5;
362 case Attribute::NoAlias: return 1 << 6;
363 case Attribute::ByVal: return 1 << 7;
364 case Attribute::Nest: return 1 << 8;
365 case Attribute::ReadNone: return 1 << 9;
366 case Attribute::ReadOnly: return 1 << 10;
367 case Attribute::NoInline: return 1 << 11;
368 case Attribute::AlwaysInline: return 1 << 12;
369 case Attribute::OptimizeForSize: return 1 << 13;
370 case Attribute::StackProtect: return 1 << 14;
371 case Attribute::StackProtectReq: return 1 << 15;
372 case Attribute::Alignment: return 31 << 16;
373 case Attribute::NoCapture: return 1 << 21;
374 case Attribute::NoRedZone: return 1 << 22;
375 case Attribute::NoImplicitFloat: return 1 << 23;
376 case Attribute::Naked: return 1 << 24;
377 case Attribute::InlineHint: return 1 << 25;
378 case Attribute::StackAlignment: return 7 << 26;
379 case Attribute::ReturnsTwice: return 1 << 29;
380 case Attribute::UWTable: return 1 << 30;
381 case Attribute::NonLazyBind: return 1U << 31;
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000382 case Attribute::SanitizeAddress: return 1ULL << 32;
Bill Wendlingd509a662013-01-29 00:34:06 +0000383 case Attribute::MinSize: return 1ULL << 33;
384 case Attribute::NoDuplicate: return 1ULL << 34;
385 case Attribute::StackProtectStrong: return 1ULL << 35;
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000386 case Attribute::SanitizeThread: return 1ULL << 36;
387 case Attribute::SanitizeMemory: return 1ULL << 37;
Bill Wendling7bfdfe72013-02-22 00:40:12 +0000388 case Attribute::NoBuiltin: return 1ULL << 38;
Stephen Linb8bd2322013-04-20 05:14:40 +0000389 case Attribute::Returned: return 1ULL << 39;
Diego Novilloc6399532013-05-24 12:26:52 +0000390 case Attribute::Cold: return 1ULL << 40;
Michael Gottesman41748d72013-06-27 00:25:01 +0000391 case Attribute::Builtin: return 1ULL << 41;
Andrea Di Biagio377496b2013-08-23 11:53:55 +0000392 case Attribute::OptimizeNone: return 1ULL << 42;
Reid Klecknera534a382013-12-19 02:14:12 +0000393 case Attribute::InAlloca: return 1ULL << 43;
Bill Wendling25342e12013-02-22 00:50:09 +0000394 }
395 llvm_unreachable("Unsupported attribute type");
Bill Wendlingd509a662013-01-29 00:34:06 +0000396}
397
398//===----------------------------------------------------------------------===//
399// AttributeSetNode Definition
400//===----------------------------------------------------------------------===//
401
402AttributeSetNode *AttributeSetNode::get(LLVMContext &C,
403 ArrayRef<Attribute> Attrs) {
404 if (Attrs.empty())
Craig Topperc6207612014-04-09 06:08:46 +0000405 return nullptr;
Bill Wendlingd509a662013-01-29 00:34:06 +0000406
407 // Otherwise, build a key to look up the existing attributes.
408 LLVMContextImpl *pImpl = C.pImpl;
409 FoldingSetNodeID ID;
410
411 SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end());
Bill Wendling82c2ee62013-02-13 09:26:26 +0000412 array_pod_sort(SortedAttrs.begin(), SortedAttrs.end());
Bill Wendlingd509a662013-01-29 00:34:06 +0000413
414 for (SmallVectorImpl<Attribute>::iterator I = SortedAttrs.begin(),
415 E = SortedAttrs.end(); I != E; ++I)
416 I->Profile(ID);
417
418 void *InsertPoint;
419 AttributeSetNode *PA =
420 pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint);
421
422 // If we didn't find any existing attributes of the same shape then create a
423 // new one and insert it.
424 if (!PA) {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000425 // Coallocate entries after the AttributeSetNode itself.
426 void *Mem = ::operator new(sizeof(AttributeSetNode) +
427 sizeof(Attribute) * SortedAttrs.size());
428 PA = new (Mem) AttributeSetNode(SortedAttrs);
Bill Wendlingd509a662013-01-29 00:34:06 +0000429 pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint);
430 }
431
432 // Return the AttributesListNode that we found or created.
433 return PA;
434}
435
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000436bool AttributeSetNode::hasAttribute(Attribute::AttrKind Kind) const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000437 for (iterator I = begin(), E = end(); I != E; ++I)
NAKAMURA Takumib0944392013-01-31 03:47:28 +0000438 if (I->hasAttribute(Kind))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000439 return true;
440 return false;
441}
442
Bill Wendlingbce7b972013-02-13 08:42:21 +0000443bool AttributeSetNode::hasAttribute(StringRef Kind) const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000444 for (iterator I = begin(), E = end(); I != E; ++I)
Bill Wendlingbce7b972013-02-13 08:42:21 +0000445 if (I->hasAttribute(Kind))
446 return true;
447 return false;
448}
449
450Attribute AttributeSetNode::getAttribute(Attribute::AttrKind Kind) const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000451 for (iterator I = begin(), E = end(); I != E; ++I)
Bill Wendlingbce7b972013-02-13 08:42:21 +0000452 if (I->hasAttribute(Kind))
453 return *I;
454 return Attribute();
455}
456
457Attribute AttributeSetNode::getAttribute(StringRef Kind) const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000458 for (iterator I = begin(), E = end(); I != E; ++I)
Bill Wendlingbce7b972013-02-13 08:42:21 +0000459 if (I->hasAttribute(Kind))
460 return *I;
461 return Attribute();
462}
463
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000464unsigned AttributeSetNode::getAlignment() const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000465 for (iterator I = begin(), E = end(); I != E; ++I)
NAKAMURA Takumib0944392013-01-31 03:47:28 +0000466 if (I->hasAttribute(Attribute::Alignment))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000467 return I->getAlignment();
468 return 0;
469}
470
471unsigned AttributeSetNode::getStackAlignment() const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000472 for (iterator I = begin(), E = end(); I != E; ++I)
NAKAMURA Takumib0944392013-01-31 03:47:28 +0000473 if (I->hasAttribute(Attribute::StackAlignment))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000474 return I->getStackAlignment();
475 return 0;
476}
477
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +0000478std::string AttributeSetNode::getAsString(bool InAttrGrp) const {
Benjamin Kramer0baf8f42013-04-19 11:43:21 +0000479 std::string Str;
Benjamin Kramer741146b2013-07-11 12:13:16 +0000480 for (iterator I = begin(), E = end(); I != E; ++I) {
481 if (I != begin())
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +0000482 Str += ' ';
483 Str += I->getAsString(InAttrGrp);
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000484 }
485 return Str;
486}
487
Bill Wendlingd509a662013-01-29 00:34:06 +0000488//===----------------------------------------------------------------------===//
489// AttributeSetImpl Definition
490//===----------------------------------------------------------------------===//
491
Rafael Espindoladd275302013-04-30 16:53:38 +0000492uint64_t AttributeSetImpl::Raw(unsigned Index) const {
Bill Wendlingd509a662013-01-29 00:34:06 +0000493 for (unsigned I = 0, E = getNumAttributes(); I != E; ++I) {
494 if (getSlotIndex(I) != Index) continue;
Benjamin Kramer741146b2013-07-11 12:13:16 +0000495 const AttributeSetNode *ASN = getSlotNode(I);
Bill Wendling91226182013-02-02 00:52:44 +0000496 uint64_t Mask = 0;
Bill Wendlingd509a662013-01-29 00:34:06 +0000497
Benjamin Kramer741146b2013-07-11 12:13:16 +0000498 for (AttributeSetNode::iterator II = ASN->begin(),
Bill Wendling91226182013-02-02 00:52:44 +0000499 IE = ASN->end(); II != IE; ++II) {
500 Attribute Attr = *II;
Bill Wendlingd746e402013-02-10 23:18:05 +0000501
502 // This cannot handle string attributes.
503 if (Attr.isStringAttribute()) continue;
504
Bill Wendling3f12ac22013-02-05 22:37:24 +0000505 Attribute::AttrKind Kind = Attr.getKindAsEnum();
Bill Wendling91226182013-02-02 00:52:44 +0000506
Bill Wendling3f12ac22013-02-05 22:37:24 +0000507 if (Kind == Attribute::Alignment)
Bill Wendling91226182013-02-02 00:52:44 +0000508 Mask |= (Log2_32(ASN->getAlignment()) + 1) << 16;
Bill Wendling3f12ac22013-02-05 22:37:24 +0000509 else if (Kind == Attribute::StackAlignment)
Bill Wendling91226182013-02-02 00:52:44 +0000510 Mask |= (Log2_32(ASN->getStackAlignment()) + 1) << 26;
511 else
Bill Wendling3f12ac22013-02-05 22:37:24 +0000512 Mask |= AttributeImpl::getAttrMask(Kind);
Bill Wendling91226182013-02-02 00:52:44 +0000513 }
514
515 return Mask;
Bill Wendlingd509a662013-01-29 00:34:06 +0000516 }
517
518 return 0;
519}
520
Peter Collingbourneabca2ec2013-08-02 22:34:30 +0000521void AttributeSetImpl::dump() const {
522 AttributeSet(const_cast<AttributeSetImpl *>(this)).dump();
523}
524
Bill Wendlingd509a662013-01-29 00:34:06 +0000525//===----------------------------------------------------------------------===//
526// AttributeSet Construction and Mutation Methods
527//===----------------------------------------------------------------------===//
528
Bill Wendling60011b82013-01-29 01:43:29 +0000529AttributeSet
530AttributeSet::getImpl(LLVMContext &C,
531 ArrayRef<std::pair<unsigned, AttributeSetNode*> > Attrs) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000532 LLVMContextImpl *pImpl = C.pImpl;
533 FoldingSetNodeID ID;
534 AttributeSetImpl::Profile(ID, Attrs);
535
536 void *InsertPoint;
537 AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
538
539 // If we didn't find any existing attributes of the same shape then
540 // create a new one and insert it.
541 if (!PA) {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000542 // Coallocate entries after the AttributeSetImpl itself.
543 void *Mem = ::operator new(sizeof(AttributeSetImpl) +
544 sizeof(std::pair<unsigned, AttributeSetNode *>) *
545 Attrs.size());
546 PA = new (Mem) AttributeSetImpl(C, Attrs);
Bill Wendlingd509a662013-01-29 00:34:06 +0000547 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
548 }
549
550 // Return the AttributesList that we found or created.
551 return AttributeSet(PA);
552}
553
554AttributeSet AttributeSet::get(LLVMContext &C,
555 ArrayRef<std::pair<unsigned, Attribute> > Attrs){
556 // If there are no attributes then return a null AttributesList pointer.
557 if (Attrs.empty())
558 return AttributeSet();
559
560#ifndef NDEBUG
561 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
562 assert((!i || Attrs[i-1].first <= Attrs[i].first) &&
563 "Misordered Attributes list!");
Bill Wendling3f12ac22013-02-05 22:37:24 +0000564 assert(!Attrs[i].second.hasAttribute(Attribute::None) &&
Bill Wendlingd509a662013-01-29 00:34:06 +0000565 "Pointless attribute!");
566 }
567#endif
568
569 // Create a vector if (unsigned, AttributeSetNode*) pairs from the attributes
570 // list.
571 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrPairVec;
572 for (ArrayRef<std::pair<unsigned, Attribute> >::iterator I = Attrs.begin(),
573 E = Attrs.end(); I != E; ) {
574 unsigned Index = I->first;
575 SmallVector<Attribute, 4> AttrVec;
NAKAMURA Takumif05d2f22013-01-29 15:18:16 +0000576 while (I != E && I->first == Index) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000577 AttrVec.push_back(I->second);
578 ++I;
579 }
580
581 AttrPairVec.push_back(std::make_pair(Index,
582 AttributeSetNode::get(C, AttrVec)));
583 }
584
585 return getImpl(C, AttrPairVec);
586}
587
588AttributeSet AttributeSet::get(LLVMContext &C,
589 ArrayRef<std::pair<unsigned,
590 AttributeSetNode*> > Attrs) {
591 // If there are no attributes then return a null AttributesList pointer.
592 if (Attrs.empty())
593 return AttributeSet();
594
595 return getImpl(C, Attrs);
596}
597
Bill Wendling211316c2013-04-18 20:17:28 +0000598AttributeSet AttributeSet::get(LLVMContext &C, unsigned Index, AttrBuilder &B) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000599 if (!B.hasAttributes())
600 return AttributeSet();
Bill Wendlingf7134812013-01-29 01:02:03 +0000601
Bill Wendlingae89a0f2013-02-05 23:48:36 +0000602 // Add target-independent attributes.
Bill Wendlingf7134812013-01-29 01:02:03 +0000603 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
Benjamin Kramer45e7d532013-02-16 19:13:18 +0000604 for (Attribute::AttrKind Kind = Attribute::None;
Benjamin Kramer6f37dac2013-02-16 19:22:28 +0000605 Kind != Attribute::EndAttrKinds; Kind = Attribute::AttrKind(Kind + 1)) {
Benjamin Kramer45e7d532013-02-16 19:13:18 +0000606 if (!B.contains(Kind))
607 continue;
608
Bill Wendlingf7134812013-01-29 01:02:03 +0000609 if (Kind == Attribute::Alignment)
Bill Wendling211316c2013-04-18 20:17:28 +0000610 Attrs.push_back(std::make_pair(Index, Attribute::
Bill Wendlingf7134812013-01-29 01:02:03 +0000611 getWithAlignment(C, B.getAlignment())));
612 else if (Kind == Attribute::StackAlignment)
Bill Wendling211316c2013-04-18 20:17:28 +0000613 Attrs.push_back(std::make_pair(Index, Attribute::
Bill Wendlingf7134812013-01-29 01:02:03 +0000614 getWithStackAlignment(C, B.getStackAlignment())));
615 else
Bill Wendling211316c2013-04-18 20:17:28 +0000616 Attrs.push_back(std::make_pair(Index, Attribute::get(C, Kind)));
Bill Wendlingf7134812013-01-29 01:02:03 +0000617 }
618
Bill Wendlingae89a0f2013-02-05 23:48:36 +0000619 // Add target-dependent (string) attributes.
620 for (AttrBuilder::td_iterator I = B.td_begin(), E = B.td_end();
621 I != E; ++I)
Bill Wendling211316c2013-04-18 20:17:28 +0000622 Attrs.push_back(std::make_pair(Index, Attribute::get(C, I->first,I->second)));
Bill Wendlingae89a0f2013-02-05 23:48:36 +0000623
Bill Wendlingf7134812013-01-29 01:02:03 +0000624 return get(C, Attrs);
Bill Wendlingd509a662013-01-29 00:34:06 +0000625}
626
Bill Wendling211316c2013-04-18 20:17:28 +0000627AttributeSet AttributeSet::get(LLVMContext &C, unsigned Index,
Bill Wendlingd509a662013-01-29 00:34:06 +0000628 ArrayRef<Attribute::AttrKind> Kind) {
629 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
630 for (ArrayRef<Attribute::AttrKind>::iterator I = Kind.begin(),
631 E = Kind.end(); I != E; ++I)
Bill Wendling211316c2013-04-18 20:17:28 +0000632 Attrs.push_back(std::make_pair(Index, Attribute::get(C, *I)));
Bill Wendlingd509a662013-01-29 00:34:06 +0000633 return get(C, Attrs);
634}
635
636AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<AttributeSet> Attrs) {
637 if (Attrs.empty()) return AttributeSet();
Peter Collingbournebd6c7452013-08-02 22:29:40 +0000638 if (Attrs.size() == 1) return Attrs[0];
Bill Wendlingd509a662013-01-29 00:34:06 +0000639
640 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrNodeVec;
Peter Collingbournebd6c7452013-08-02 22:29:40 +0000641 AttributeSetImpl *A0 = Attrs[0].pImpl;
642 if (A0)
643 AttrNodeVec.append(A0->getNode(0), A0->getNode(A0->getNumAttributes()));
644 // Copy all attributes from Attrs into AttrNodeVec while keeping AttrNodeVec
645 // ordered by index. Because we know that each list in Attrs is ordered by
646 // index we only need to merge each successive list in rather than doing a
647 // full sort.
648 for (unsigned I = 1, E = Attrs.size(); I != E; ++I) {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000649 AttributeSetImpl *AS = Attrs[I].pImpl;
650 if (!AS) continue;
Peter Collingbournebd6c7452013-08-02 22:29:40 +0000651 SmallVector<std::pair<unsigned, AttributeSetNode *>, 8>::iterator
652 ANVI = AttrNodeVec.begin(), ANVE;
653 for (const AttributeSetImpl::IndexAttrPair
654 *AI = AS->getNode(0),
655 *AE = AS->getNode(AS->getNumAttributes());
656 AI != AE; ++AI) {
657 ANVE = AttrNodeVec.end();
658 while (ANVI != ANVE && ANVI->first <= AI->first)
659 ++ANVI;
660 ANVI = AttrNodeVec.insert(ANVI, *AI) + 1;
661 }
Bill Wendlingd509a662013-01-29 00:34:06 +0000662 }
663
664 return getImpl(C, AttrNodeVec);
665}
666
Bill Wendling211316c2013-04-18 20:17:28 +0000667AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
Bill Wendlingd509a662013-01-29 00:34:06 +0000668 Attribute::AttrKind Attr) const {
Bill Wendling211316c2013-04-18 20:17:28 +0000669 if (hasAttribute(Index, Attr)) return *this;
670 return addAttributes(C, Index, AttributeSet::get(C, Index, Attr));
Bill Wendlingd509a662013-01-29 00:34:06 +0000671}
672
Bill Wendling211316c2013-04-18 20:17:28 +0000673AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
Reed Kotler795c7b42013-03-13 20:20:08 +0000674 StringRef Kind) const {
675 llvm::AttrBuilder B;
676 B.addAttribute(Kind);
Bill Wendling211316c2013-04-18 20:17:28 +0000677 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
Reed Kotler795c7b42013-03-13 20:20:08 +0000678}
679
Bill Wendling3b2f6102013-07-25 18:34:24 +0000680AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
681 StringRef Kind, StringRef Value) const {
682 llvm::AttrBuilder B;
683 B.addAttribute(Kind, Value);
684 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
685}
686
Bill Wendling211316c2013-04-18 20:17:28 +0000687AttributeSet AttributeSet::addAttributes(LLVMContext &C, unsigned Index,
Bill Wendlingd509a662013-01-29 00:34:06 +0000688 AttributeSet Attrs) const {
689 if (!pImpl) return Attrs;
690 if (!Attrs.pImpl) return *this;
691
692#ifndef NDEBUG
693 // FIXME it is not obvious how this should work for alignment. For now, say
694 // we can't change a known alignment.
Bill Wendling211316c2013-04-18 20:17:28 +0000695 unsigned OldAlign = getParamAlignment(Index);
696 unsigned NewAlign = Attrs.getParamAlignment(Index);
Bill Wendlingd509a662013-01-29 00:34:06 +0000697 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
698 "Attempt to change alignment!");
699#endif
700
701 // Add the attribute slots before the one we're trying to add.
702 SmallVector<AttributeSet, 4> AttrSet;
703 uint64_t NumAttrs = pImpl->getNumAttributes();
704 AttributeSet AS;
705 uint64_t LastIndex = 0;
706 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
Bill Wendling211316c2013-04-18 20:17:28 +0000707 if (getSlotIndex(I) >= Index) {
708 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
Bill Wendlingd509a662013-01-29 00:34:06 +0000709 break;
710 }
711 LastIndex = I + 1;
712 AttrSet.push_back(getSlotAttributes(I));
713 }
714
715 // Now add the attribute into the correct slot. There may already be an
716 // AttributeSet there.
Bill Wendling211316c2013-04-18 20:17:28 +0000717 AttrBuilder B(AS, Index);
Bill Wendlingd509a662013-01-29 00:34:06 +0000718
719 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
Bill Wendling211316c2013-04-18 20:17:28 +0000720 if (Attrs.getSlotIndex(I) == Index) {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000721 for (AttributeSetImpl::iterator II = Attrs.pImpl->begin(I),
Bill Wendlingd509a662013-01-29 00:34:06 +0000722 IE = Attrs.pImpl->end(I); II != IE; ++II)
Bill Wendling23804da2013-01-31 23:38:01 +0000723 B.addAttribute(*II);
Bill Wendlingd509a662013-01-29 00:34:06 +0000724 break;
725 }
726
Bill Wendling211316c2013-04-18 20:17:28 +0000727 AttrSet.push_back(AttributeSet::get(C, Index, B));
Bill Wendlingd509a662013-01-29 00:34:06 +0000728
729 // Add the remaining attribute slots.
730 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
731 AttrSet.push_back(getSlotAttributes(I));
732
733 return get(C, AttrSet);
734}
735
Bill Wendling211316c2013-04-18 20:17:28 +0000736AttributeSet AttributeSet::removeAttribute(LLVMContext &C, unsigned Index,
Bill Wendlingd509a662013-01-29 00:34:06 +0000737 Attribute::AttrKind Attr) const {
Bill Wendling211316c2013-04-18 20:17:28 +0000738 if (!hasAttribute(Index, Attr)) return *this;
739 return removeAttributes(C, Index, AttributeSet::get(C, Index, Attr));
Bill Wendlingd509a662013-01-29 00:34:06 +0000740}
741
Bill Wendling211316c2013-04-18 20:17:28 +0000742AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Index,
Bill Wendlingd509a662013-01-29 00:34:06 +0000743 AttributeSet Attrs) const {
744 if (!pImpl) return AttributeSet();
745 if (!Attrs.pImpl) return *this;
746
747#ifndef NDEBUG
748 // FIXME it is not obvious how this should work for alignment.
749 // For now, say we can't pass in alignment, which no current use does.
Bill Wendling211316c2013-04-18 20:17:28 +0000750 assert(!Attrs.hasAttribute(Index, Attribute::Alignment) &&
Bill Wendlingd509a662013-01-29 00:34:06 +0000751 "Attempt to change alignment!");
752#endif
753
754 // Add the attribute slots before the one we're trying to add.
755 SmallVector<AttributeSet, 4> AttrSet;
756 uint64_t NumAttrs = pImpl->getNumAttributes();
757 AttributeSet AS;
758 uint64_t LastIndex = 0;
759 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
Bill Wendling211316c2013-04-18 20:17:28 +0000760 if (getSlotIndex(I) >= Index) {
761 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
Bill Wendlingd509a662013-01-29 00:34:06 +0000762 break;
763 }
764 LastIndex = I + 1;
765 AttrSet.push_back(getSlotAttributes(I));
766 }
767
Bill Wendlingd2196752013-01-30 23:07:40 +0000768 // Now remove the attribute from the correct slot. There may already be an
Bill Wendlingd509a662013-01-29 00:34:06 +0000769 // AttributeSet there.
Bill Wendling211316c2013-04-18 20:17:28 +0000770 AttrBuilder B(AS, Index);
Bill Wendlingd509a662013-01-29 00:34:06 +0000771
772 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
Bill Wendling211316c2013-04-18 20:17:28 +0000773 if (Attrs.getSlotIndex(I) == Index) {
774 B.removeAttributes(Attrs.pImpl->getSlotAttributes(I), Index);
Bill Wendlingd509a662013-01-29 00:34:06 +0000775 break;
776 }
777
Bill Wendling211316c2013-04-18 20:17:28 +0000778 AttrSet.push_back(AttributeSet::get(C, Index, B));
Bill Wendlingd509a662013-01-29 00:34:06 +0000779
780 // Add the remaining attribute slots.
781 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
782 AttrSet.push_back(getSlotAttributes(I));
783
784 return get(C, AttrSet);
785}
786
787//===----------------------------------------------------------------------===//
788// AttributeSet Accessor Methods
789//===----------------------------------------------------------------------===//
790
Bill Wendling5d020a32013-02-10 05:00:40 +0000791LLVMContext &AttributeSet::getContext() const {
792 return pImpl->getContext();
793}
794
Bill Wendling211316c2013-04-18 20:17:28 +0000795AttributeSet AttributeSet::getParamAttributes(unsigned Index) const {
796 return pImpl && hasAttributes(Index) ?
Bill Wendlingd509a662013-01-29 00:34:06 +0000797 AttributeSet::get(pImpl->getContext(),
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000798 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendling211316c2013-04-18 20:17:28 +0000799 std::make_pair(Index, getAttributes(Index)))) :
Bill Wendlingd509a662013-01-29 00:34:06 +0000800 AttributeSet();
801}
802
803AttributeSet AttributeSet::getRetAttributes() const {
804 return pImpl && hasAttributes(ReturnIndex) ?
805 AttributeSet::get(pImpl->getContext(),
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000806 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendlingd509a662013-01-29 00:34:06 +0000807 std::make_pair(ReturnIndex,
808 getAttributes(ReturnIndex)))) :
809 AttributeSet();
810}
811
812AttributeSet AttributeSet::getFnAttributes() const {
813 return pImpl && hasAttributes(FunctionIndex) ?
814 AttributeSet::get(pImpl->getContext(),
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000815 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendlingd509a662013-01-29 00:34:06 +0000816 std::make_pair(FunctionIndex,
817 getAttributes(FunctionIndex)))) :
818 AttributeSet();
819}
820
821bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000822 AttributeSetNode *ASN = getAttributes(Index);
823 return ASN ? ASN->hasAttribute(Kind) : false;
Bill Wendlingd509a662013-01-29 00:34:06 +0000824}
825
Bill Wendlingbce7b972013-02-13 08:42:21 +0000826bool AttributeSet::hasAttribute(unsigned Index, StringRef Kind) const {
827 AttributeSetNode *ASN = getAttributes(Index);
828 return ASN ? ASN->hasAttribute(Kind) : false;
829}
830
Bill Wendlingd509a662013-01-29 00:34:06 +0000831bool AttributeSet::hasAttributes(unsigned Index) const {
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000832 AttributeSetNode *ASN = getAttributes(Index);
833 return ASN ? ASN->hasAttributes() : false;
Bill Wendlingd509a662013-01-29 00:34:06 +0000834}
835
836/// \brief Return true if the specified attribute is set for at least one
837/// parameter or for the return value.
838bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr) const {
Craig Topperc6207612014-04-09 06:08:46 +0000839 if (!pImpl) return false;
Bill Wendlingd509a662013-01-29 00:34:06 +0000840
841 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
Benjamin Kramer741146b2013-07-11 12:13:16 +0000842 for (AttributeSetImpl::iterator II = pImpl->begin(I),
Bill Wendlingd509a662013-01-29 00:34:06 +0000843 IE = pImpl->end(I); II != IE; ++II)
NAKAMURA Takumib0944392013-01-31 03:47:28 +0000844 if (II->hasAttribute(Attr))
Bill Wendlingd509a662013-01-29 00:34:06 +0000845 return true;
846
847 return false;
848}
849
Bill Wendlingbce7b972013-02-13 08:42:21 +0000850Attribute AttributeSet::getAttribute(unsigned Index,
851 Attribute::AttrKind Kind) const {
852 AttributeSetNode *ASN = getAttributes(Index);
853 return ASN ? ASN->getAttribute(Kind) : Attribute();
854}
855
856Attribute AttributeSet::getAttribute(unsigned Index,
857 StringRef Kind) const {
858 AttributeSetNode *ASN = getAttributes(Index);
859 return ASN ? ASN->getAttribute(Kind) : Attribute();
860}
861
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000862unsigned AttributeSet::getParamAlignment(unsigned Index) const {
863 AttributeSetNode *ASN = getAttributes(Index);
864 return ASN ? ASN->getAlignment() : 0;
Bill Wendlingd509a662013-01-29 00:34:06 +0000865}
866
867unsigned AttributeSet::getStackAlignment(unsigned Index) const {
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000868 AttributeSetNode *ASN = getAttributes(Index);
869 return ASN ? ASN->getStackAlignment() : 0;
Bill Wendlingd509a662013-01-29 00:34:06 +0000870}
871
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +0000872std::string AttributeSet::getAsString(unsigned Index,
Bill Wendling829b4782013-02-11 08:43:33 +0000873 bool InAttrGrp) const {
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000874 AttributeSetNode *ASN = getAttributes(Index);
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +0000875 return ASN ? ASN->getAsString(InAttrGrp) : std::string("");
Bill Wendlingd509a662013-01-29 00:34:06 +0000876}
877
878/// \brief The attributes for the specified index are returned.
Bill Wendling211316c2013-04-18 20:17:28 +0000879AttributeSetNode *AttributeSet::getAttributes(unsigned Index) const {
Craig Topperc6207612014-04-09 06:08:46 +0000880 if (!pImpl) return nullptr;
Bill Wendlingd509a662013-01-29 00:34:06 +0000881
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000882 // Loop through to find the attribute node we want.
883 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
Bill Wendling211316c2013-04-18 20:17:28 +0000884 if (pImpl->getSlotIndex(I) == Index)
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000885 return pImpl->getSlotNode(I);
Bill Wendlingd509a662013-01-29 00:34:06 +0000886
Craig Topperc6207612014-04-09 06:08:46 +0000887 return nullptr;
Bill Wendlingd509a662013-01-29 00:34:06 +0000888}
889
Bill Wendling211316c2013-04-18 20:17:28 +0000890AttributeSet::iterator AttributeSet::begin(unsigned Slot) const {
Bill Wendlinga9174862013-01-31 23:53:05 +0000891 if (!pImpl)
892 return ArrayRef<Attribute>().begin();
Bill Wendling211316c2013-04-18 20:17:28 +0000893 return pImpl->begin(Slot);
Bill Wendlinga9174862013-01-31 23:53:05 +0000894}
895
Bill Wendling211316c2013-04-18 20:17:28 +0000896AttributeSet::iterator AttributeSet::end(unsigned Slot) const {
Bill Wendlinga9174862013-01-31 23:53:05 +0000897 if (!pImpl)
898 return ArrayRef<Attribute>().end();
Bill Wendling211316c2013-04-18 20:17:28 +0000899 return pImpl->end(Slot);
Bill Wendlinga9174862013-01-31 23:53:05 +0000900}
901
Bill Wendlingd509a662013-01-29 00:34:06 +0000902//===----------------------------------------------------------------------===//
903// AttributeSet Introspection Methods
904//===----------------------------------------------------------------------===//
905
906/// \brief Return the number of slots used in this attribute list. This is the
907/// number of arguments that have an attribute set on them (including the
908/// function itself).
909unsigned AttributeSet::getNumSlots() const {
910 return pImpl ? pImpl->getNumAttributes() : 0;
911}
912
Rafael Espindoladd275302013-04-30 16:53:38 +0000913unsigned AttributeSet::getSlotIndex(unsigned Slot) const {
Bill Wendlingd509a662013-01-29 00:34:06 +0000914 assert(pImpl && Slot < pImpl->getNumAttributes() &&
915 "Slot # out of range!");
916 return pImpl->getSlotIndex(Slot);
917}
918
919AttributeSet AttributeSet::getSlotAttributes(unsigned Slot) const {
920 assert(pImpl && Slot < pImpl->getNumAttributes() &&
921 "Slot # out of range!");
922 return pImpl->getSlotAttributes(Slot);
923}
924
925uint64_t AttributeSet::Raw(unsigned Index) const {
926 // FIXME: Remove this.
927 return pImpl ? pImpl->Raw(Index) : 0;
928}
929
930void AttributeSet::dump() const {
931 dbgs() << "PAL[\n";
932
933 for (unsigned i = 0, e = getNumSlots(); i < e; ++i) {
934 uint64_t Index = getSlotIndex(i);
935 dbgs() << " { ";
936 if (Index == ~0U)
937 dbgs() << "~0U";
938 else
939 dbgs() << Index;
940 dbgs() << " => " << getAsString(Index) << " }\n";
941 }
942
943 dbgs() << "]\n";
944}
945
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000946//===----------------------------------------------------------------------===//
Bill Wendlingcd330342013-01-04 23:27:34 +0000947// AttrBuilder Method Implementations
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000948//===----------------------------------------------------------------------===//
949
Bill Wendling211316c2013-04-18 20:17:28 +0000950AttrBuilder::AttrBuilder(AttributeSet AS, unsigned Index)
Benjamin Kramer45e7d532013-02-16 19:13:18 +0000951 : Attrs(0), Alignment(0), StackAlignment(0) {
Bill Wendling56b0b2a2013-01-27 21:23:46 +0000952 AttributeSetImpl *pImpl = AS.pImpl;
Bill Wendling096f5442013-01-07 08:24:35 +0000953 if (!pImpl) return;
954
Bill Wendling9eb689c2013-01-28 00:21:34 +0000955 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I) {
Bill Wendling211316c2013-04-18 20:17:28 +0000956 if (pImpl->getSlotIndex(I) != Index) continue;
Bill Wendling096f5442013-01-07 08:24:35 +0000957
Benjamin Kramer741146b2013-07-11 12:13:16 +0000958 for (AttributeSetImpl::iterator II = pImpl->begin(I),
Bill Wendling9eb689c2013-01-28 00:21:34 +0000959 IE = pImpl->end(I); II != IE; ++II)
Bill Wendling23804da2013-01-31 23:38:01 +0000960 addAttribute(*II);
Bill Wendling9eb689c2013-01-28 00:21:34 +0000961
962 break;
963 }
Bill Wendling096f5442013-01-07 08:24:35 +0000964}
965
Bill Wendlingcd330342013-01-04 23:27:34 +0000966void AttrBuilder::clear() {
Benjamin Kramereaf706b2013-02-18 12:09:51 +0000967 Attrs.reset();
Bill Wendlingcd330342013-01-04 23:27:34 +0000968 Alignment = StackAlignment = 0;
969}
970
971AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
Benjamin Kramereaf706b2013-02-18 12:09:51 +0000972 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
Bill Wendling1c7cc8a2013-01-31 23:16:25 +0000973 assert(Val != Attribute::Alignment && Val != Attribute::StackAlignment &&
974 "Adding alignment attribute without adding alignment value!");
Benjamin Kramereaf706b2013-02-18 12:09:51 +0000975 Attrs[Val] = true;
Bill Wendling7c04e042012-10-09 19:01:18 +0000976 return *this;
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000977}
978
Bill Wendling23804da2013-01-31 23:38:01 +0000979AttrBuilder &AttrBuilder::addAttribute(Attribute Attr) {
Bill Wendling0a437302013-02-10 10:13:23 +0000980 if (Attr.isStringAttribute()) {
981 addAttribute(Attr.getKindAsString(), Attr.getValueAsString());
982 return *this;
983 }
984
Bill Wendling3f12ac22013-02-05 22:37:24 +0000985 Attribute::AttrKind Kind = Attr.getKindAsEnum();
Benjamin Kramereaf706b2013-02-18 12:09:51 +0000986 Attrs[Kind] = true;
Bill Wendling1aa9d9e2013-01-28 05:23:28 +0000987
Bill Wendling3f12ac22013-02-05 22:37:24 +0000988 if (Kind == Attribute::Alignment)
Bill Wendling1aa9d9e2013-01-28 05:23:28 +0000989 Alignment = Attr.getAlignment();
Bill Wendling3f12ac22013-02-05 22:37:24 +0000990 else if (Kind == Attribute::StackAlignment)
Bill Wendling1aa9d9e2013-01-28 05:23:28 +0000991 StackAlignment = Attr.getStackAlignment();
992 return *this;
993}
994
Bill Wendlingb9c5b1a2013-02-05 08:09:32 +0000995AttrBuilder &AttrBuilder::addAttribute(StringRef A, StringRef V) {
996 TargetDepAttrs[A] = V;
997 return *this;
998}
999
Bill Wendling23804da2013-01-31 23:38:01 +00001000AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001001 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
1002 Attrs[Val] = false;
Bill Wendling23804da2013-01-31 23:38:01 +00001003
1004 if (Val == Attribute::Alignment)
1005 Alignment = 0;
1006 else if (Val == Attribute::StackAlignment)
1007 StackAlignment = 0;
1008
1009 return *this;
1010}
1011
Bill Wendlingd2196752013-01-30 23:07:40 +00001012AttrBuilder &AttrBuilder::removeAttributes(AttributeSet A, uint64_t Index) {
Bill Wendling211316c2013-04-18 20:17:28 +00001013 unsigned Slot = ~0U;
Bill Wendlingf1c94e32013-02-01 00:13:50 +00001014 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
1015 if (A.getSlotIndex(I) == Index) {
Bill Wendling211316c2013-04-18 20:17:28 +00001016 Slot = I;
Bill Wendlingf1c94e32013-02-01 00:13:50 +00001017 break;
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001018 }
Bill Wendlingf1c94e32013-02-01 00:13:50 +00001019
Bill Wendling211316c2013-04-18 20:17:28 +00001020 assert(Slot != ~0U && "Couldn't find index in AttributeSet!");
Bill Wendlingf1c94e32013-02-01 00:13:50 +00001021
Bill Wendling211316c2013-04-18 20:17:28 +00001022 for (AttributeSet::iterator I = A.begin(Slot), E = A.end(Slot); I != E; ++I) {
Bill Wendling7cde51d2013-02-12 07:56:49 +00001023 Attribute Attr = *I;
1024 if (Attr.isEnumAttribute() || Attr.isAlignAttribute()) {
1025 Attribute::AttrKind Kind = I->getKindAsEnum();
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001026 Attrs[Kind] = false;
Bill Wendlingf1c94e32013-02-01 00:13:50 +00001027
Bill Wendling7cde51d2013-02-12 07:56:49 +00001028 if (Kind == Attribute::Alignment)
1029 Alignment = 0;
1030 else if (Kind == Attribute::StackAlignment)
1031 StackAlignment = 0;
1032 } else {
1033 assert(Attr.isStringAttribute() && "Invalid attribute type!");
1034 std::map<std::string, std::string>::iterator
1035 Iter = TargetDepAttrs.find(Attr.getKindAsString());
1036 if (Iter != TargetDepAttrs.end())
1037 TargetDepAttrs.erase(Iter);
1038 }
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001039 }
1040
1041 return *this;
1042}
1043
Bill Wendlingb9c5b1a2013-02-05 08:09:32 +00001044AttrBuilder &AttrBuilder::removeAttribute(StringRef A) {
1045 std::map<std::string, std::string>::iterator I = TargetDepAttrs.find(A);
1046 if (I != TargetDepAttrs.end())
1047 TargetDepAttrs.erase(I);
1048 return *this;
1049}
1050
Bill Wendling50d27842012-10-15 20:35:56 +00001051AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
Bill Wendlingabd5ba22012-10-14 03:58:29 +00001052 if (Align == 0) return *this;
Bill Wendlingcd330342013-01-04 23:27:34 +00001053
Bill Wendlingabf3feb2012-10-05 06:44:41 +00001054 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1055 assert(Align <= 0x40000000 && "Alignment too large.");
Bill Wendlingcd330342013-01-04 23:27:34 +00001056
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001057 Attrs[Attribute::Alignment] = true;
Bill Wendlingcd330342013-01-04 23:27:34 +00001058 Alignment = Align;
Bill Wendlingabd5ba22012-10-14 03:58:29 +00001059 return *this;
Chris Lattner3e13b8c2008-01-02 23:42:30 +00001060}
1061
Bill Wendlingcd330342013-01-04 23:27:34 +00001062AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
1063 // Default alignment, allow the target to define how to align it.
1064 if (Align == 0) return *this;
1065
1066 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1067 assert(Align <= 0x100 && "Alignment too large.");
1068
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001069 Attrs[Attribute::StackAlignment] = true;
Bill Wendlingcd330342013-01-04 23:27:34 +00001070 StackAlignment = Align;
1071 return *this;
1072}
1073
Bill Wendlinge2614922013-02-06 01:16:00 +00001074AttrBuilder &AttrBuilder::merge(const AttrBuilder &B) {
1075 // FIXME: What if both have alignments, but they don't match?!
1076 if (!Alignment)
1077 Alignment = B.Alignment;
1078
1079 if (!StackAlignment)
1080 StackAlignment = B.StackAlignment;
1081
Benjamin Kramer45e7d532013-02-16 19:13:18 +00001082 Attrs |= B.Attrs;
Bill Wendlinge2614922013-02-06 01:16:00 +00001083
1084 for (td_const_iterator I = B.TargetDepAttrs.begin(),
1085 E = B.TargetDepAttrs.end(); I != E; ++I)
1086 TargetDepAttrs[I->first] = I->second;
1087
1088 return *this;
1089}
1090
Bill Wendling4b001442013-02-06 01:33:42 +00001091bool AttrBuilder::contains(StringRef A) const {
1092 return TargetDepAttrs.find(A) != TargetDepAttrs.end();
1093}
1094
Bill Wendling50d27842012-10-15 20:35:56 +00001095bool AttrBuilder::hasAttributes() const {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001096 return !Attrs.none() || !TargetDepAttrs.empty();
Bill Wendlingc6daefa2012-10-08 23:27:46 +00001097}
Bill Wendling9ac69f92013-01-04 20:54:35 +00001098
Bill Wendlingd2196752013-01-30 23:07:40 +00001099bool AttrBuilder::hasAttributes(AttributeSet A, uint64_t Index) const {
Bill Wendling211316c2013-04-18 20:17:28 +00001100 unsigned Slot = ~0U;
Bill Wendling9ca01da2013-02-02 00:42:06 +00001101 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
1102 if (A.getSlotIndex(I) == Index) {
Bill Wendling211316c2013-04-18 20:17:28 +00001103 Slot = I;
Bill Wendling9ca01da2013-02-02 00:42:06 +00001104 break;
1105 }
1106
Bill Wendling211316c2013-04-18 20:17:28 +00001107 assert(Slot != ~0U && "Couldn't find the index!");
Bill Wendling9ca01da2013-02-02 00:42:06 +00001108
Bill Wendling211316c2013-04-18 20:17:28 +00001109 for (AttributeSet::iterator I = A.begin(Slot), E = A.end(Slot);
Bill Wendling7cde51d2013-02-12 07:56:49 +00001110 I != E; ++I) {
1111 Attribute Attr = *I;
1112 if (Attr.isEnumAttribute() || Attr.isAlignAttribute()) {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001113 if (Attrs[I->getKindAsEnum()])
Bill Wendling7cde51d2013-02-12 07:56:49 +00001114 return true;
1115 } else {
1116 assert(Attr.isStringAttribute() && "Invalid attribute kind!");
1117 return TargetDepAttrs.find(Attr.getKindAsString())!=TargetDepAttrs.end();
1118 }
1119 }
Bill Wendling9ca01da2013-02-02 00:42:06 +00001120
1121 return false;
Bill Wendling70f39172012-10-09 00:01:21 +00001122}
Bill Wendling9ac69f92013-01-04 20:54:35 +00001123
Bill Wendling50d27842012-10-15 20:35:56 +00001124bool AttrBuilder::hasAlignmentAttr() const {
Bill Wendlingcd330342013-01-04 23:27:34 +00001125 return Alignment != 0;
Bill Wendlingc6daefa2012-10-08 23:27:46 +00001126}
1127
Bill Wendlingd509a662013-01-29 00:34:06 +00001128bool AttrBuilder::operator==(const AttrBuilder &B) {
Benjamin Kramer45e7d532013-02-16 19:13:18 +00001129 if (Attrs != B.Attrs)
1130 return false;
Bill Wendling4b001442013-02-06 01:33:42 +00001131
1132 for (td_const_iterator I = TargetDepAttrs.begin(),
1133 E = TargetDepAttrs.end(); I != E; ++I)
1134 if (B.TargetDepAttrs.find(I->first) == B.TargetDepAttrs.end())
1135 return false;
1136
1137 return Alignment == B.Alignment && StackAlignment == B.StackAlignment;
Bill Wendlingd509a662013-01-29 00:34:06 +00001138}
1139
1140AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) {
Bill Wendling56aeccc2013-02-04 23:32:23 +00001141 // FIXME: Remove this in 4.0.
Bill Wendling60011b82013-01-29 01:43:29 +00001142 if (!Val) return *this;
1143
Bill Wendlingd509a662013-01-29 00:34:06 +00001144 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
1145 I = Attribute::AttrKind(I + 1)) {
1146 if (uint64_t A = (Val & AttributeImpl::getAttrMask(I))) {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001147 Attrs[I] = true;
Bill Wendlingd509a662013-01-29 00:34:06 +00001148
1149 if (I == Attribute::Alignment)
1150 Alignment = 1ULL << ((A >> 16) - 1);
1151 else if (I == Attribute::StackAlignment)
1152 StackAlignment = 1ULL << ((A >> 26)-1);
1153 }
1154 }
1155
1156 return *this;
1157}
1158
Bill Wendling57625a42013-01-25 23:09:36 +00001159//===----------------------------------------------------------------------===//
1160// AttributeFuncs Function Defintions
1161//===----------------------------------------------------------------------===//
1162
Bill Wendlingc79cdff2013-02-01 01:04:27 +00001163/// \brief Which attributes cannot be applied to a type.
Bill Wendlingd2196752013-01-30 23:07:40 +00001164AttributeSet AttributeFuncs::typeIncompatible(Type *Ty, uint64_t Index) {
Bill Wendling57625a42013-01-25 23:09:36 +00001165 AttrBuilder Incompatible;
1166
1167 if (!Ty->isIntegerTy())
1168 // Attribute that only apply to integers.
1169 Incompatible.addAttribute(Attribute::SExt)
1170 .addAttribute(Attribute::ZExt);
1171
1172 if (!Ty->isPointerTy())
1173 // Attribute that only apply to pointers.
1174 Incompatible.addAttribute(Attribute::ByVal)
1175 .addAttribute(Attribute::Nest)
1176 .addAttribute(Attribute::NoAlias)
1177 .addAttribute(Attribute::NoCapture)
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001178 .addAttribute(Attribute::ReadNone)
1179 .addAttribute(Attribute::ReadOnly)
Reid Klecknera534a382013-12-19 02:14:12 +00001180 .addAttribute(Attribute::StructRet)
1181 .addAttribute(Attribute::InAlloca);
Bill Wendling57625a42013-01-25 23:09:36 +00001182
Bill Wendlingd2196752013-01-30 23:07:40 +00001183 return AttributeSet::get(Ty->getContext(), Index, Incompatible);
Bill Wendling57625a42013-01-25 23:09:36 +00001184}