blob: f876778c45cd33d582046b0a0e90bd4e827b296c [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"
Michael J. Spencer447762d2010-11-29 18:16:10 +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";
169 if (hasAttribute(Attribute::InlineHint))
170 return "inlinehint";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000171 if (hasAttribute(Attribute::InReg))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000172 return "inreg";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000173 if (hasAttribute(Attribute::MinSize))
174 return "minsize";
175 if (hasAttribute(Attribute::Naked))
176 return "naked";
177 if (hasAttribute(Attribute::Nest))
178 return "nest";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000179 if (hasAttribute(Attribute::NoAlias))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000180 return "noalias";
Bill Wendling09bd1f72013-02-22 00:12:35 +0000181 if (hasAttribute(Attribute::NoBuiltin))
182 return "nobuiltin";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000183 if (hasAttribute(Attribute::NoCapture))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000184 return "nocapture";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000185 if (hasAttribute(Attribute::NoDuplicate))
186 return "noduplicate";
187 if (hasAttribute(Attribute::NoImplicitFloat))
188 return "noimplicitfloat";
189 if (hasAttribute(Attribute::NoInline))
190 return "noinline";
191 if (hasAttribute(Attribute::NonLazyBind))
192 return "nonlazybind";
193 if (hasAttribute(Attribute::NoRedZone))
194 return "noredzone";
195 if (hasAttribute(Attribute::NoReturn))
196 return "noreturn";
197 if (hasAttribute(Attribute::NoUnwind))
198 return "nounwind";
Andrea Di Biagio377496b2013-08-23 11:53:55 +0000199 if (hasAttribute(Attribute::OptimizeNone))
200 return "optnone";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000201 if (hasAttribute(Attribute::OptimizeForSize))
202 return "optsize";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000203 if (hasAttribute(Attribute::ReadNone))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000204 return "readnone";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000205 if (hasAttribute(Attribute::ReadOnly))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000206 return "readonly";
Stephen Linb8bd2322013-04-20 05:14:40 +0000207 if (hasAttribute(Attribute::Returned))
208 return "returned";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000209 if (hasAttribute(Attribute::ReturnsTwice))
210 return "returns_twice";
211 if (hasAttribute(Attribute::SExt))
212 return "signext";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000213 if (hasAttribute(Attribute::StackProtect))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000214 return "ssp";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000215 if (hasAttribute(Attribute::StackProtectReq))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000216 return "sspreq";
Bill Wendlingd154e2832013-01-23 06:41:41 +0000217 if (hasAttribute(Attribute::StackProtectStrong))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000218 return "sspstrong";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000219 if (hasAttribute(Attribute::StructRet))
220 return "sret";
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000221 if (hasAttribute(Attribute::SanitizeThread))
222 return "sanitize_thread";
223 if (hasAttribute(Attribute::SanitizeMemory))
224 return "sanitize_memory";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000225 if (hasAttribute(Attribute::UWTable))
226 return "uwtable";
227 if (hasAttribute(Attribute::ZExt))
228 return "zeroext";
Diego Novilloc6399532013-05-24 12:26:52 +0000229 if (hasAttribute(Attribute::Cold))
230 return "cold";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000231
232 // FIXME: These should be output like this:
233 //
234 // align=4
235 // alignstack=8
236 //
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000237 if (hasAttribute(Attribute::Alignment)) {
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000238 std::string Result;
Bill Wendling829b4782013-02-11 08:43:33 +0000239 Result += "align";
240 Result += (InAttrGrp) ? "=" : " ";
Bill Wendling3f12ac22013-02-05 22:37:24 +0000241 Result += utostr(getValueAsInt());
242 return Result;
243 }
Bill Wendling829b4782013-02-11 08:43:33 +0000244
Bill Wendling3f12ac22013-02-05 22:37:24 +0000245 if (hasAttribute(Attribute::StackAlignment)) {
246 std::string Result;
Bill Wendling829b4782013-02-11 08:43:33 +0000247 Result += "alignstack";
248 if (InAttrGrp) {
249 Result += "=";
250 Result += utostr(getValueAsInt());
251 } else {
252 Result += "(";
253 Result += utostr(getValueAsInt());
254 Result += ")";
255 }
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000256 return Result;
Dale Johannesen11a555e2008-02-19 23:51:49 +0000257 }
Bill Wendling9c2eba92013-01-31 20:59:05 +0000258
259 // Convert target-dependent attributes to strings of the form:
260 //
261 // "kind"
262 // "kind" = "value"
Bill Wendling9c2eba92013-01-31 20:59:05 +0000263 //
Bill Wendling3f12ac22013-02-05 22:37:24 +0000264 if (isStringAttribute()) {
Bill Wendling9c2eba92013-01-31 20:59:05 +0000265 std::string Result;
Bill Wendling3f12ac22013-02-05 22:37:24 +0000266 Result += '\"' + getKindAsString().str() + '"';
Bill Wendling9c2eba92013-01-31 20:59:05 +0000267
Bill Wendling3f12ac22013-02-05 22:37:24 +0000268 StringRef Val = pImpl->getValueAsString();
269 if (Val.empty()) return Result;
Bill Wendling7a33f772013-02-01 22:32:30 +0000270
Bill Wendling829b4782013-02-11 08:43:33 +0000271 Result += "=\"" + Val.str() + '"';
Bill Wendlingc79cdff2013-02-01 01:04:27 +0000272 return Result;
Bill Wendling9c2eba92013-01-31 20:59:05 +0000273 }
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000274
275 llvm_unreachable("Unknown attribute");
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000276}
277
Bill Wendlingd509a662013-01-29 00:34:06 +0000278bool Attribute::operator<(Attribute A) const {
279 if (!pImpl && !A.pImpl) return false;
280 if (!pImpl) return true;
281 if (!A.pImpl) return false;
282 return *pImpl < *A.pImpl;
283}
284
Bill Wendlingd509a662013-01-29 00:34:06 +0000285//===----------------------------------------------------------------------===//
286// AttributeImpl Definition
287//===----------------------------------------------------------------------===//
288
Bill Wendlingd509a662013-01-29 00:34:06 +0000289bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
Bill Wendling3f12ac22013-02-05 22:37:24 +0000290 if (isStringAttribute()) return false;
291 return getKindAsEnum() == A;
Bill Wendlingd509a662013-01-29 00:34:06 +0000292}
293
Bill Wendling3f12ac22013-02-05 22:37:24 +0000294bool AttributeImpl::hasAttribute(StringRef Kind) const {
295 if (!isStringAttribute()) return false;
296 return getKindAsString() == Kind;
Bill Wendlingd509a662013-01-29 00:34:06 +0000297}
298
Bill Wendling3f12ac22013-02-05 22:37:24 +0000299Attribute::AttrKind AttributeImpl::getKindAsEnum() const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000300 assert(isEnumAttribute() || isAlignAttribute());
301 return static_cast<const EnumAttributeImpl *>(this)->getEnumKind();
Bill Wendlingd509a662013-01-29 00:34:06 +0000302}
303
Bill Wendling3f12ac22013-02-05 22:37:24 +0000304uint64_t AttributeImpl::getValueAsInt() const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000305 assert(isAlignAttribute());
306 return static_cast<const AlignAttributeImpl *>(this)->getAlignment();
Bill Wendlingd509a662013-01-29 00:34:06 +0000307}
308
Bill Wendling3f12ac22013-02-05 22:37:24 +0000309StringRef AttributeImpl::getKindAsString() const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000310 assert(isStringAttribute());
311 return static_cast<const StringAttributeImpl *>(this)->getStringKind();
Bill Wendlingd509a662013-01-29 00:34:06 +0000312}
313
Bill Wendling3f12ac22013-02-05 22:37:24 +0000314StringRef AttributeImpl::getValueAsString() const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000315 assert(isStringAttribute());
316 return static_cast<const StringAttributeImpl *>(this)->getStringValue();
Bill Wendlingd509a662013-01-29 00:34:06 +0000317}
318
319bool AttributeImpl::operator<(const AttributeImpl &AI) const {
Bill Wendlingc79cdff2013-02-01 01:04:27 +0000320 // This sorts the attributes with Attribute::AttrKinds coming first (sorted
321 // relative to their enum value) and then strings.
Bill Wendling26b95752013-02-15 05:25:26 +0000322 if (isEnumAttribute()) {
323 if (AI.isEnumAttribute()) return getKindAsEnum() < AI.getKindAsEnum();
324 if (AI.isAlignAttribute()) return true;
325 if (AI.isStringAttribute()) return true;
326 }
Bill Wendlingc79cdff2013-02-01 01:04:27 +0000327
Bill Wendling3f12ac22013-02-05 22:37:24 +0000328 if (isAlignAttribute()) {
Bill Wendling26b95752013-02-15 05:25:26 +0000329 if (AI.isEnumAttribute()) return false;
330 if (AI.isAlignAttribute()) return getValueAsInt() < AI.getValueAsInt();
331 if (AI.isStringAttribute()) return true;
Bill Wendling3f12ac22013-02-05 22:37:24 +0000332 }
Bill Wendlingd509a662013-01-29 00:34:06 +0000333
Bill Wendling26b95752013-02-15 05:25:26 +0000334 if (AI.isEnumAttribute()) return false;
335 if (AI.isAlignAttribute()) return false;
336 if (getKindAsString() == AI.getKindAsString())
337 return getValueAsString() < AI.getValueAsString();
338 return getKindAsString() < AI.getKindAsString();
Bill Wendlingd509a662013-01-29 00:34:06 +0000339}
340
Bill Wendlingd509a662013-01-29 00:34:06 +0000341uint64_t AttributeImpl::getAttrMask(Attribute::AttrKind Val) {
342 // FIXME: Remove this.
343 switch (Val) {
344 case Attribute::EndAttrKinds:
Bill Wendlingd509a662013-01-29 00:34:06 +0000345 llvm_unreachable("Synthetic enumerators which should never get here");
346
347 case Attribute::None: return 0;
348 case Attribute::ZExt: return 1 << 0;
349 case Attribute::SExt: return 1 << 1;
350 case Attribute::NoReturn: return 1 << 2;
351 case Attribute::InReg: return 1 << 3;
352 case Attribute::StructRet: return 1 << 4;
353 case Attribute::NoUnwind: return 1 << 5;
354 case Attribute::NoAlias: return 1 << 6;
355 case Attribute::ByVal: return 1 << 7;
356 case Attribute::Nest: return 1 << 8;
357 case Attribute::ReadNone: return 1 << 9;
358 case Attribute::ReadOnly: return 1 << 10;
359 case Attribute::NoInline: return 1 << 11;
360 case Attribute::AlwaysInline: return 1 << 12;
361 case Attribute::OptimizeForSize: return 1 << 13;
362 case Attribute::StackProtect: return 1 << 14;
363 case Attribute::StackProtectReq: return 1 << 15;
364 case Attribute::Alignment: return 31 << 16;
365 case Attribute::NoCapture: return 1 << 21;
366 case Attribute::NoRedZone: return 1 << 22;
367 case Attribute::NoImplicitFloat: return 1 << 23;
368 case Attribute::Naked: return 1 << 24;
369 case Attribute::InlineHint: return 1 << 25;
370 case Attribute::StackAlignment: return 7 << 26;
371 case Attribute::ReturnsTwice: return 1 << 29;
372 case Attribute::UWTable: return 1 << 30;
373 case Attribute::NonLazyBind: return 1U << 31;
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000374 case Attribute::SanitizeAddress: return 1ULL << 32;
Bill Wendlingd509a662013-01-29 00:34:06 +0000375 case Attribute::MinSize: return 1ULL << 33;
376 case Attribute::NoDuplicate: return 1ULL << 34;
377 case Attribute::StackProtectStrong: return 1ULL << 35;
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000378 case Attribute::SanitizeThread: return 1ULL << 36;
379 case Attribute::SanitizeMemory: return 1ULL << 37;
Bill Wendling7bfdfe72013-02-22 00:40:12 +0000380 case Attribute::NoBuiltin: return 1ULL << 38;
Stephen Linb8bd2322013-04-20 05:14:40 +0000381 case Attribute::Returned: return 1ULL << 39;
Diego Novilloc6399532013-05-24 12:26:52 +0000382 case Attribute::Cold: return 1ULL << 40;
Michael Gottesman41748d72013-06-27 00:25:01 +0000383 case Attribute::Builtin: return 1ULL << 41;
Andrea Di Biagio377496b2013-08-23 11:53:55 +0000384 case Attribute::OptimizeNone: return 1ULL << 42;
Bill Wendling25342e12013-02-22 00:50:09 +0000385 }
386 llvm_unreachable("Unsupported attribute type");
Bill Wendlingd509a662013-01-29 00:34:06 +0000387}
388
389//===----------------------------------------------------------------------===//
390// AttributeSetNode Definition
391//===----------------------------------------------------------------------===//
392
393AttributeSetNode *AttributeSetNode::get(LLVMContext &C,
394 ArrayRef<Attribute> Attrs) {
395 if (Attrs.empty())
396 return 0;
397
398 // Otherwise, build a key to look up the existing attributes.
399 LLVMContextImpl *pImpl = C.pImpl;
400 FoldingSetNodeID ID;
401
402 SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end());
Bill Wendling82c2ee62013-02-13 09:26:26 +0000403 array_pod_sort(SortedAttrs.begin(), SortedAttrs.end());
Bill Wendlingd509a662013-01-29 00:34:06 +0000404
405 for (SmallVectorImpl<Attribute>::iterator I = SortedAttrs.begin(),
406 E = SortedAttrs.end(); I != E; ++I)
407 I->Profile(ID);
408
409 void *InsertPoint;
410 AttributeSetNode *PA =
411 pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint);
412
413 // If we didn't find any existing attributes of the same shape then create a
414 // new one and insert it.
415 if (!PA) {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000416 // Coallocate entries after the AttributeSetNode itself.
417 void *Mem = ::operator new(sizeof(AttributeSetNode) +
418 sizeof(Attribute) * SortedAttrs.size());
419 PA = new (Mem) AttributeSetNode(SortedAttrs);
Bill Wendlingd509a662013-01-29 00:34:06 +0000420 pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint);
421 }
422
423 // Return the AttributesListNode that we found or created.
424 return PA;
425}
426
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000427bool AttributeSetNode::hasAttribute(Attribute::AttrKind Kind) const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000428 for (iterator I = begin(), E = end(); I != E; ++I)
NAKAMURA Takumib0944392013-01-31 03:47:28 +0000429 if (I->hasAttribute(Kind))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000430 return true;
431 return false;
432}
433
Bill Wendlingbce7b972013-02-13 08:42:21 +0000434bool AttributeSetNode::hasAttribute(StringRef Kind) const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000435 for (iterator I = begin(), E = end(); I != E; ++I)
Bill Wendlingbce7b972013-02-13 08:42:21 +0000436 if (I->hasAttribute(Kind))
437 return true;
438 return false;
439}
440
441Attribute AttributeSetNode::getAttribute(Attribute::AttrKind Kind) const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000442 for (iterator I = begin(), E = end(); I != E; ++I)
Bill Wendlingbce7b972013-02-13 08:42:21 +0000443 if (I->hasAttribute(Kind))
444 return *I;
445 return Attribute();
446}
447
448Attribute AttributeSetNode::getAttribute(StringRef Kind) const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000449 for (iterator I = begin(), E = end(); I != E; ++I)
Bill Wendlingbce7b972013-02-13 08:42:21 +0000450 if (I->hasAttribute(Kind))
451 return *I;
452 return Attribute();
453}
454
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000455unsigned AttributeSetNode::getAlignment() const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000456 for (iterator I = begin(), E = end(); I != E; ++I)
NAKAMURA Takumib0944392013-01-31 03:47:28 +0000457 if (I->hasAttribute(Attribute::Alignment))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000458 return I->getAlignment();
459 return 0;
460}
461
462unsigned AttributeSetNode::getStackAlignment() const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000463 for (iterator I = begin(), E = end(); I != E; ++I)
NAKAMURA Takumib0944392013-01-31 03:47:28 +0000464 if (I->hasAttribute(Attribute::StackAlignment))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000465 return I->getStackAlignment();
466 return 0;
467}
468
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +0000469std::string AttributeSetNode::getAsString(bool InAttrGrp) const {
Benjamin Kramer0baf8f42013-04-19 11:43:21 +0000470 std::string Str;
Benjamin Kramer741146b2013-07-11 12:13:16 +0000471 for (iterator I = begin(), E = end(); I != E; ++I) {
472 if (I != begin())
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +0000473 Str += ' ';
474 Str += I->getAsString(InAttrGrp);
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000475 }
476 return Str;
477}
478
Bill Wendlingd509a662013-01-29 00:34:06 +0000479//===----------------------------------------------------------------------===//
480// AttributeSetImpl Definition
481//===----------------------------------------------------------------------===//
482
Rafael Espindoladd275302013-04-30 16:53:38 +0000483uint64_t AttributeSetImpl::Raw(unsigned Index) const {
Bill Wendlingd509a662013-01-29 00:34:06 +0000484 for (unsigned I = 0, E = getNumAttributes(); I != E; ++I) {
485 if (getSlotIndex(I) != Index) continue;
Benjamin Kramer741146b2013-07-11 12:13:16 +0000486 const AttributeSetNode *ASN = getSlotNode(I);
Bill Wendling91226182013-02-02 00:52:44 +0000487 uint64_t Mask = 0;
Bill Wendlingd509a662013-01-29 00:34:06 +0000488
Benjamin Kramer741146b2013-07-11 12:13:16 +0000489 for (AttributeSetNode::iterator II = ASN->begin(),
Bill Wendling91226182013-02-02 00:52:44 +0000490 IE = ASN->end(); II != IE; ++II) {
491 Attribute Attr = *II;
Bill Wendlingd746e402013-02-10 23:18:05 +0000492
493 // This cannot handle string attributes.
494 if (Attr.isStringAttribute()) continue;
495
Bill Wendling3f12ac22013-02-05 22:37:24 +0000496 Attribute::AttrKind Kind = Attr.getKindAsEnum();
Bill Wendling91226182013-02-02 00:52:44 +0000497
Bill Wendling3f12ac22013-02-05 22:37:24 +0000498 if (Kind == Attribute::Alignment)
Bill Wendling91226182013-02-02 00:52:44 +0000499 Mask |= (Log2_32(ASN->getAlignment()) + 1) << 16;
Bill Wendling3f12ac22013-02-05 22:37:24 +0000500 else if (Kind == Attribute::StackAlignment)
Bill Wendling91226182013-02-02 00:52:44 +0000501 Mask |= (Log2_32(ASN->getStackAlignment()) + 1) << 26;
502 else
Bill Wendling3f12ac22013-02-05 22:37:24 +0000503 Mask |= AttributeImpl::getAttrMask(Kind);
Bill Wendling91226182013-02-02 00:52:44 +0000504 }
505
506 return Mask;
Bill Wendlingd509a662013-01-29 00:34:06 +0000507 }
508
509 return 0;
510}
511
Peter Collingbourneabca2ec2013-08-02 22:34:30 +0000512void AttributeSetImpl::dump() const {
513 AttributeSet(const_cast<AttributeSetImpl *>(this)).dump();
514}
515
Bill Wendlingd509a662013-01-29 00:34:06 +0000516//===----------------------------------------------------------------------===//
517// AttributeSet Construction and Mutation Methods
518//===----------------------------------------------------------------------===//
519
Bill Wendling60011b82013-01-29 01:43:29 +0000520AttributeSet
521AttributeSet::getImpl(LLVMContext &C,
522 ArrayRef<std::pair<unsigned, AttributeSetNode*> > Attrs) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000523 LLVMContextImpl *pImpl = C.pImpl;
524 FoldingSetNodeID ID;
525 AttributeSetImpl::Profile(ID, Attrs);
526
527 void *InsertPoint;
528 AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
529
530 // If we didn't find any existing attributes of the same shape then
531 // create a new one and insert it.
532 if (!PA) {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000533 // Coallocate entries after the AttributeSetImpl itself.
534 void *Mem = ::operator new(sizeof(AttributeSetImpl) +
535 sizeof(std::pair<unsigned, AttributeSetNode *>) *
536 Attrs.size());
537 PA = new (Mem) AttributeSetImpl(C, Attrs);
Bill Wendlingd509a662013-01-29 00:34:06 +0000538 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
539 }
540
541 // Return the AttributesList that we found or created.
542 return AttributeSet(PA);
543}
544
545AttributeSet AttributeSet::get(LLVMContext &C,
546 ArrayRef<std::pair<unsigned, Attribute> > Attrs){
547 // If there are no attributes then return a null AttributesList pointer.
548 if (Attrs.empty())
549 return AttributeSet();
550
551#ifndef NDEBUG
552 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
553 assert((!i || Attrs[i-1].first <= Attrs[i].first) &&
554 "Misordered Attributes list!");
Bill Wendling3f12ac22013-02-05 22:37:24 +0000555 assert(!Attrs[i].second.hasAttribute(Attribute::None) &&
Bill Wendlingd509a662013-01-29 00:34:06 +0000556 "Pointless attribute!");
557 }
558#endif
559
560 // Create a vector if (unsigned, AttributeSetNode*) pairs from the attributes
561 // list.
562 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrPairVec;
563 for (ArrayRef<std::pair<unsigned, Attribute> >::iterator I = Attrs.begin(),
564 E = Attrs.end(); I != E; ) {
565 unsigned Index = I->first;
566 SmallVector<Attribute, 4> AttrVec;
NAKAMURA Takumif05d2f22013-01-29 15:18:16 +0000567 while (I != E && I->first == Index) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000568 AttrVec.push_back(I->second);
569 ++I;
570 }
571
572 AttrPairVec.push_back(std::make_pair(Index,
573 AttributeSetNode::get(C, AttrVec)));
574 }
575
576 return getImpl(C, AttrPairVec);
577}
578
579AttributeSet AttributeSet::get(LLVMContext &C,
580 ArrayRef<std::pair<unsigned,
581 AttributeSetNode*> > Attrs) {
582 // If there are no attributes then return a null AttributesList pointer.
583 if (Attrs.empty())
584 return AttributeSet();
585
586 return getImpl(C, Attrs);
587}
588
Bill Wendling211316c2013-04-18 20:17:28 +0000589AttributeSet AttributeSet::get(LLVMContext &C, unsigned Index, AttrBuilder &B) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000590 if (!B.hasAttributes())
591 return AttributeSet();
Bill Wendlingf7134812013-01-29 01:02:03 +0000592
Bill Wendlingae89a0f2013-02-05 23:48:36 +0000593 // Add target-independent attributes.
Bill Wendlingf7134812013-01-29 01:02:03 +0000594 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
Benjamin Kramer45e7d532013-02-16 19:13:18 +0000595 for (Attribute::AttrKind Kind = Attribute::None;
Benjamin Kramer6f37dac2013-02-16 19:22:28 +0000596 Kind != Attribute::EndAttrKinds; Kind = Attribute::AttrKind(Kind + 1)) {
Benjamin Kramer45e7d532013-02-16 19:13:18 +0000597 if (!B.contains(Kind))
598 continue;
599
Bill Wendlingf7134812013-01-29 01:02:03 +0000600 if (Kind == Attribute::Alignment)
Bill Wendling211316c2013-04-18 20:17:28 +0000601 Attrs.push_back(std::make_pair(Index, Attribute::
Bill Wendlingf7134812013-01-29 01:02:03 +0000602 getWithAlignment(C, B.getAlignment())));
603 else if (Kind == Attribute::StackAlignment)
Bill Wendling211316c2013-04-18 20:17:28 +0000604 Attrs.push_back(std::make_pair(Index, Attribute::
Bill Wendlingf7134812013-01-29 01:02:03 +0000605 getWithStackAlignment(C, B.getStackAlignment())));
606 else
Bill Wendling211316c2013-04-18 20:17:28 +0000607 Attrs.push_back(std::make_pair(Index, Attribute::get(C, Kind)));
Bill Wendlingf7134812013-01-29 01:02:03 +0000608 }
609
Bill Wendlingae89a0f2013-02-05 23:48:36 +0000610 // Add target-dependent (string) attributes.
611 for (AttrBuilder::td_iterator I = B.td_begin(), E = B.td_end();
612 I != E; ++I)
Bill Wendling211316c2013-04-18 20:17:28 +0000613 Attrs.push_back(std::make_pair(Index, Attribute::get(C, I->first,I->second)));
Bill Wendlingae89a0f2013-02-05 23:48:36 +0000614
Bill Wendlingf7134812013-01-29 01:02:03 +0000615 return get(C, Attrs);
Bill Wendlingd509a662013-01-29 00:34:06 +0000616}
617
Bill Wendling211316c2013-04-18 20:17:28 +0000618AttributeSet AttributeSet::get(LLVMContext &C, unsigned Index,
Bill Wendlingd509a662013-01-29 00:34:06 +0000619 ArrayRef<Attribute::AttrKind> Kind) {
620 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
621 for (ArrayRef<Attribute::AttrKind>::iterator I = Kind.begin(),
622 E = Kind.end(); I != E; ++I)
Bill Wendling211316c2013-04-18 20:17:28 +0000623 Attrs.push_back(std::make_pair(Index, Attribute::get(C, *I)));
Bill Wendlingd509a662013-01-29 00:34:06 +0000624 return get(C, Attrs);
625}
626
627AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<AttributeSet> Attrs) {
628 if (Attrs.empty()) return AttributeSet();
Peter Collingbournebd6c7452013-08-02 22:29:40 +0000629 if (Attrs.size() == 1) return Attrs[0];
Bill Wendlingd509a662013-01-29 00:34:06 +0000630
631 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrNodeVec;
Peter Collingbournebd6c7452013-08-02 22:29:40 +0000632 AttributeSetImpl *A0 = Attrs[0].pImpl;
633 if (A0)
634 AttrNodeVec.append(A0->getNode(0), A0->getNode(A0->getNumAttributes()));
635 // Copy all attributes from Attrs into AttrNodeVec while keeping AttrNodeVec
636 // ordered by index. Because we know that each list in Attrs is ordered by
637 // index we only need to merge each successive list in rather than doing a
638 // full sort.
639 for (unsigned I = 1, E = Attrs.size(); I != E; ++I) {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000640 AttributeSetImpl *AS = Attrs[I].pImpl;
641 if (!AS) continue;
Peter Collingbournebd6c7452013-08-02 22:29:40 +0000642 SmallVector<std::pair<unsigned, AttributeSetNode *>, 8>::iterator
643 ANVI = AttrNodeVec.begin(), ANVE;
644 for (const AttributeSetImpl::IndexAttrPair
645 *AI = AS->getNode(0),
646 *AE = AS->getNode(AS->getNumAttributes());
647 AI != AE; ++AI) {
648 ANVE = AttrNodeVec.end();
649 while (ANVI != ANVE && ANVI->first <= AI->first)
650 ++ANVI;
651 ANVI = AttrNodeVec.insert(ANVI, *AI) + 1;
652 }
Bill Wendlingd509a662013-01-29 00:34:06 +0000653 }
654
655 return getImpl(C, AttrNodeVec);
656}
657
Bill Wendling211316c2013-04-18 20:17:28 +0000658AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
Bill Wendlingd509a662013-01-29 00:34:06 +0000659 Attribute::AttrKind Attr) const {
Bill Wendling211316c2013-04-18 20:17:28 +0000660 if (hasAttribute(Index, Attr)) return *this;
661 return addAttributes(C, Index, AttributeSet::get(C, Index, Attr));
Bill Wendlingd509a662013-01-29 00:34:06 +0000662}
663
Bill Wendling211316c2013-04-18 20:17:28 +0000664AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
Reed Kotler795c7b42013-03-13 20:20:08 +0000665 StringRef Kind) const {
666 llvm::AttrBuilder B;
667 B.addAttribute(Kind);
Bill Wendling211316c2013-04-18 20:17:28 +0000668 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
Reed Kotler795c7b42013-03-13 20:20:08 +0000669}
670
Bill Wendling3b2f6102013-07-25 18:34:24 +0000671AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
672 StringRef Kind, StringRef Value) const {
673 llvm::AttrBuilder B;
674 B.addAttribute(Kind, Value);
675 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
676}
677
Bill Wendling211316c2013-04-18 20:17:28 +0000678AttributeSet AttributeSet::addAttributes(LLVMContext &C, unsigned Index,
Bill Wendlingd509a662013-01-29 00:34:06 +0000679 AttributeSet Attrs) const {
680 if (!pImpl) return Attrs;
681 if (!Attrs.pImpl) return *this;
682
683#ifndef NDEBUG
684 // FIXME it is not obvious how this should work for alignment. For now, say
685 // we can't change a known alignment.
Bill Wendling211316c2013-04-18 20:17:28 +0000686 unsigned OldAlign = getParamAlignment(Index);
687 unsigned NewAlign = Attrs.getParamAlignment(Index);
Bill Wendlingd509a662013-01-29 00:34:06 +0000688 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
689 "Attempt to change alignment!");
690#endif
691
692 // Add the attribute slots before the one we're trying to add.
693 SmallVector<AttributeSet, 4> AttrSet;
694 uint64_t NumAttrs = pImpl->getNumAttributes();
695 AttributeSet AS;
696 uint64_t LastIndex = 0;
697 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
Bill Wendling211316c2013-04-18 20:17:28 +0000698 if (getSlotIndex(I) >= Index) {
699 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
Bill Wendlingd509a662013-01-29 00:34:06 +0000700 break;
701 }
702 LastIndex = I + 1;
703 AttrSet.push_back(getSlotAttributes(I));
704 }
705
706 // Now add the attribute into the correct slot. There may already be an
707 // AttributeSet there.
Bill Wendling211316c2013-04-18 20:17:28 +0000708 AttrBuilder B(AS, Index);
Bill Wendlingd509a662013-01-29 00:34:06 +0000709
710 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
Bill Wendling211316c2013-04-18 20:17:28 +0000711 if (Attrs.getSlotIndex(I) == Index) {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000712 for (AttributeSetImpl::iterator II = Attrs.pImpl->begin(I),
Bill Wendlingd509a662013-01-29 00:34:06 +0000713 IE = Attrs.pImpl->end(I); II != IE; ++II)
Bill Wendling23804da2013-01-31 23:38:01 +0000714 B.addAttribute(*II);
Bill Wendlingd509a662013-01-29 00:34:06 +0000715 break;
716 }
717
Bill Wendling211316c2013-04-18 20:17:28 +0000718 AttrSet.push_back(AttributeSet::get(C, Index, B));
Bill Wendlingd509a662013-01-29 00:34:06 +0000719
720 // Add the remaining attribute slots.
721 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
722 AttrSet.push_back(getSlotAttributes(I));
723
724 return get(C, AttrSet);
725}
726
Bill Wendling211316c2013-04-18 20:17:28 +0000727AttributeSet AttributeSet::removeAttribute(LLVMContext &C, unsigned Index,
Bill Wendlingd509a662013-01-29 00:34:06 +0000728 Attribute::AttrKind Attr) const {
Bill Wendling211316c2013-04-18 20:17:28 +0000729 if (!hasAttribute(Index, Attr)) return *this;
730 return removeAttributes(C, Index, AttributeSet::get(C, Index, Attr));
Bill Wendlingd509a662013-01-29 00:34:06 +0000731}
732
Bill Wendling211316c2013-04-18 20:17:28 +0000733AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Index,
Bill Wendlingd509a662013-01-29 00:34:06 +0000734 AttributeSet Attrs) const {
735 if (!pImpl) return AttributeSet();
736 if (!Attrs.pImpl) return *this;
737
738#ifndef NDEBUG
739 // FIXME it is not obvious how this should work for alignment.
740 // For now, say we can't pass in alignment, which no current use does.
Bill Wendling211316c2013-04-18 20:17:28 +0000741 assert(!Attrs.hasAttribute(Index, Attribute::Alignment) &&
Bill Wendlingd509a662013-01-29 00:34:06 +0000742 "Attempt to change alignment!");
743#endif
744
745 // Add the attribute slots before the one we're trying to add.
746 SmallVector<AttributeSet, 4> AttrSet;
747 uint64_t NumAttrs = pImpl->getNumAttributes();
748 AttributeSet AS;
749 uint64_t LastIndex = 0;
750 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
Bill Wendling211316c2013-04-18 20:17:28 +0000751 if (getSlotIndex(I) >= Index) {
752 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
Bill Wendlingd509a662013-01-29 00:34:06 +0000753 break;
754 }
755 LastIndex = I + 1;
756 AttrSet.push_back(getSlotAttributes(I));
757 }
758
Bill Wendlingd2196752013-01-30 23:07:40 +0000759 // Now remove the attribute from the correct slot. There may already be an
Bill Wendlingd509a662013-01-29 00:34:06 +0000760 // AttributeSet there.
Bill Wendling211316c2013-04-18 20:17:28 +0000761 AttrBuilder B(AS, Index);
Bill Wendlingd509a662013-01-29 00:34:06 +0000762
763 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
Bill Wendling211316c2013-04-18 20:17:28 +0000764 if (Attrs.getSlotIndex(I) == Index) {
765 B.removeAttributes(Attrs.pImpl->getSlotAttributes(I), Index);
Bill Wendlingd509a662013-01-29 00:34:06 +0000766 break;
767 }
768
Bill Wendling211316c2013-04-18 20:17:28 +0000769 AttrSet.push_back(AttributeSet::get(C, Index, B));
Bill Wendlingd509a662013-01-29 00:34:06 +0000770
771 // Add the remaining attribute slots.
772 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
773 AttrSet.push_back(getSlotAttributes(I));
774
775 return get(C, AttrSet);
776}
777
778//===----------------------------------------------------------------------===//
779// AttributeSet Accessor Methods
780//===----------------------------------------------------------------------===//
781
Bill Wendling5d020a32013-02-10 05:00:40 +0000782LLVMContext &AttributeSet::getContext() const {
783 return pImpl->getContext();
784}
785
Bill Wendling211316c2013-04-18 20:17:28 +0000786AttributeSet AttributeSet::getParamAttributes(unsigned Index) const {
787 return pImpl && hasAttributes(Index) ?
Bill Wendlingd509a662013-01-29 00:34:06 +0000788 AttributeSet::get(pImpl->getContext(),
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000789 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendling211316c2013-04-18 20:17:28 +0000790 std::make_pair(Index, getAttributes(Index)))) :
Bill Wendlingd509a662013-01-29 00:34:06 +0000791 AttributeSet();
792}
793
794AttributeSet AttributeSet::getRetAttributes() const {
795 return pImpl && hasAttributes(ReturnIndex) ?
796 AttributeSet::get(pImpl->getContext(),
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000797 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendlingd509a662013-01-29 00:34:06 +0000798 std::make_pair(ReturnIndex,
799 getAttributes(ReturnIndex)))) :
800 AttributeSet();
801}
802
803AttributeSet AttributeSet::getFnAttributes() const {
804 return pImpl && hasAttributes(FunctionIndex) ?
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(FunctionIndex,
808 getAttributes(FunctionIndex)))) :
809 AttributeSet();
810}
811
812bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000813 AttributeSetNode *ASN = getAttributes(Index);
814 return ASN ? ASN->hasAttribute(Kind) : false;
Bill Wendlingd509a662013-01-29 00:34:06 +0000815}
816
Bill Wendlingbce7b972013-02-13 08:42:21 +0000817bool AttributeSet::hasAttribute(unsigned Index, StringRef Kind) const {
818 AttributeSetNode *ASN = getAttributes(Index);
819 return ASN ? ASN->hasAttribute(Kind) : false;
820}
821
Bill Wendlingd509a662013-01-29 00:34:06 +0000822bool AttributeSet::hasAttributes(unsigned Index) const {
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000823 AttributeSetNode *ASN = getAttributes(Index);
824 return ASN ? ASN->hasAttributes() : false;
Bill Wendlingd509a662013-01-29 00:34:06 +0000825}
826
827/// \brief Return true if the specified attribute is set for at least one
828/// parameter or for the return value.
829bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr) const {
830 if (pImpl == 0) return false;
831
832 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
Benjamin Kramer741146b2013-07-11 12:13:16 +0000833 for (AttributeSetImpl::iterator II = pImpl->begin(I),
Bill Wendlingd509a662013-01-29 00:34:06 +0000834 IE = pImpl->end(I); II != IE; ++II)
NAKAMURA Takumib0944392013-01-31 03:47:28 +0000835 if (II->hasAttribute(Attr))
Bill Wendlingd509a662013-01-29 00:34:06 +0000836 return true;
837
838 return false;
839}
840
Bill Wendlingbce7b972013-02-13 08:42:21 +0000841Attribute AttributeSet::getAttribute(unsigned Index,
842 Attribute::AttrKind Kind) const {
843 AttributeSetNode *ASN = getAttributes(Index);
844 return ASN ? ASN->getAttribute(Kind) : Attribute();
845}
846
847Attribute AttributeSet::getAttribute(unsigned Index,
848 StringRef Kind) const {
849 AttributeSetNode *ASN = getAttributes(Index);
850 return ASN ? ASN->getAttribute(Kind) : Attribute();
851}
852
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000853unsigned AttributeSet::getParamAlignment(unsigned Index) const {
854 AttributeSetNode *ASN = getAttributes(Index);
855 return ASN ? ASN->getAlignment() : 0;
Bill Wendlingd509a662013-01-29 00:34:06 +0000856}
857
858unsigned AttributeSet::getStackAlignment(unsigned Index) const {
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000859 AttributeSetNode *ASN = getAttributes(Index);
860 return ASN ? ASN->getStackAlignment() : 0;
Bill Wendlingd509a662013-01-29 00:34:06 +0000861}
862
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +0000863std::string AttributeSet::getAsString(unsigned Index,
Bill Wendling829b4782013-02-11 08:43:33 +0000864 bool InAttrGrp) const {
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000865 AttributeSetNode *ASN = getAttributes(Index);
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +0000866 return ASN ? ASN->getAsString(InAttrGrp) : std::string("");
Bill Wendlingd509a662013-01-29 00:34:06 +0000867}
868
869/// \brief The attributes for the specified index are returned.
Bill Wendling211316c2013-04-18 20:17:28 +0000870AttributeSetNode *AttributeSet::getAttributes(unsigned Index) const {
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000871 if (!pImpl) return 0;
Bill Wendlingd509a662013-01-29 00:34:06 +0000872
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000873 // Loop through to find the attribute node we want.
874 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
Bill Wendling211316c2013-04-18 20:17:28 +0000875 if (pImpl->getSlotIndex(I) == Index)
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000876 return pImpl->getSlotNode(I);
Bill Wendlingd509a662013-01-29 00:34:06 +0000877
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000878 return 0;
Bill Wendlingd509a662013-01-29 00:34:06 +0000879}
880
Bill Wendling211316c2013-04-18 20:17:28 +0000881AttributeSet::iterator AttributeSet::begin(unsigned Slot) const {
Bill Wendlinga9174862013-01-31 23:53:05 +0000882 if (!pImpl)
883 return ArrayRef<Attribute>().begin();
Bill Wendling211316c2013-04-18 20:17:28 +0000884 return pImpl->begin(Slot);
Bill Wendlinga9174862013-01-31 23:53:05 +0000885}
886
Bill Wendling211316c2013-04-18 20:17:28 +0000887AttributeSet::iterator AttributeSet::end(unsigned Slot) const {
Bill Wendlinga9174862013-01-31 23:53:05 +0000888 if (!pImpl)
889 return ArrayRef<Attribute>().end();
Bill Wendling211316c2013-04-18 20:17:28 +0000890 return pImpl->end(Slot);
Bill Wendlinga9174862013-01-31 23:53:05 +0000891}
892
Bill Wendlingd509a662013-01-29 00:34:06 +0000893//===----------------------------------------------------------------------===//
894// AttributeSet Introspection Methods
895//===----------------------------------------------------------------------===//
896
897/// \brief Return the number of slots used in this attribute list. This is the
898/// number of arguments that have an attribute set on them (including the
899/// function itself).
900unsigned AttributeSet::getNumSlots() const {
901 return pImpl ? pImpl->getNumAttributes() : 0;
902}
903
Rafael Espindoladd275302013-04-30 16:53:38 +0000904unsigned AttributeSet::getSlotIndex(unsigned Slot) const {
Bill Wendlingd509a662013-01-29 00:34:06 +0000905 assert(pImpl && Slot < pImpl->getNumAttributes() &&
906 "Slot # out of range!");
907 return pImpl->getSlotIndex(Slot);
908}
909
910AttributeSet AttributeSet::getSlotAttributes(unsigned Slot) const {
911 assert(pImpl && Slot < pImpl->getNumAttributes() &&
912 "Slot # out of range!");
913 return pImpl->getSlotAttributes(Slot);
914}
915
916uint64_t AttributeSet::Raw(unsigned Index) const {
917 // FIXME: Remove this.
918 return pImpl ? pImpl->Raw(Index) : 0;
919}
920
921void AttributeSet::dump() const {
922 dbgs() << "PAL[\n";
923
924 for (unsigned i = 0, e = getNumSlots(); i < e; ++i) {
925 uint64_t Index = getSlotIndex(i);
926 dbgs() << " { ";
927 if (Index == ~0U)
928 dbgs() << "~0U";
929 else
930 dbgs() << Index;
931 dbgs() << " => " << getAsString(Index) << " }\n";
932 }
933
934 dbgs() << "]\n";
935}
936
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000937//===----------------------------------------------------------------------===//
Bill Wendlingcd330342013-01-04 23:27:34 +0000938// AttrBuilder Method Implementations
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000939//===----------------------------------------------------------------------===//
940
Bill Wendling211316c2013-04-18 20:17:28 +0000941AttrBuilder::AttrBuilder(AttributeSet AS, unsigned Index)
Benjamin Kramer45e7d532013-02-16 19:13:18 +0000942 : Attrs(0), Alignment(0), StackAlignment(0) {
Bill Wendling56b0b2a2013-01-27 21:23:46 +0000943 AttributeSetImpl *pImpl = AS.pImpl;
Bill Wendling096f5442013-01-07 08:24:35 +0000944 if (!pImpl) return;
945
Bill Wendling9eb689c2013-01-28 00:21:34 +0000946 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I) {
Bill Wendling211316c2013-04-18 20:17:28 +0000947 if (pImpl->getSlotIndex(I) != Index) continue;
Bill Wendling096f5442013-01-07 08:24:35 +0000948
Benjamin Kramer741146b2013-07-11 12:13:16 +0000949 for (AttributeSetImpl::iterator II = pImpl->begin(I),
Bill Wendling9eb689c2013-01-28 00:21:34 +0000950 IE = pImpl->end(I); II != IE; ++II)
Bill Wendling23804da2013-01-31 23:38:01 +0000951 addAttribute(*II);
Bill Wendling9eb689c2013-01-28 00:21:34 +0000952
953 break;
954 }
Bill Wendling096f5442013-01-07 08:24:35 +0000955}
956
Bill Wendlingcd330342013-01-04 23:27:34 +0000957void AttrBuilder::clear() {
Benjamin Kramereaf706b2013-02-18 12:09:51 +0000958 Attrs.reset();
Bill Wendlingcd330342013-01-04 23:27:34 +0000959 Alignment = StackAlignment = 0;
960}
961
962AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
Benjamin Kramereaf706b2013-02-18 12:09:51 +0000963 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
Bill Wendling1c7cc8a2013-01-31 23:16:25 +0000964 assert(Val != Attribute::Alignment && Val != Attribute::StackAlignment &&
965 "Adding alignment attribute without adding alignment value!");
Benjamin Kramereaf706b2013-02-18 12:09:51 +0000966 Attrs[Val] = true;
Bill Wendling7c04e042012-10-09 19:01:18 +0000967 return *this;
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000968}
969
Bill Wendling23804da2013-01-31 23:38:01 +0000970AttrBuilder &AttrBuilder::addAttribute(Attribute Attr) {
Bill Wendling0a437302013-02-10 10:13:23 +0000971 if (Attr.isStringAttribute()) {
972 addAttribute(Attr.getKindAsString(), Attr.getValueAsString());
973 return *this;
974 }
975
Bill Wendling3f12ac22013-02-05 22:37:24 +0000976 Attribute::AttrKind Kind = Attr.getKindAsEnum();
Benjamin Kramereaf706b2013-02-18 12:09:51 +0000977 Attrs[Kind] = true;
Bill Wendling1aa9d9e2013-01-28 05:23:28 +0000978
Bill Wendling3f12ac22013-02-05 22:37:24 +0000979 if (Kind == Attribute::Alignment)
Bill Wendling1aa9d9e2013-01-28 05:23:28 +0000980 Alignment = Attr.getAlignment();
Bill Wendling3f12ac22013-02-05 22:37:24 +0000981 else if (Kind == Attribute::StackAlignment)
Bill Wendling1aa9d9e2013-01-28 05:23:28 +0000982 StackAlignment = Attr.getStackAlignment();
983 return *this;
984}
985
Bill Wendlingb9c5b1a2013-02-05 08:09:32 +0000986AttrBuilder &AttrBuilder::addAttribute(StringRef A, StringRef V) {
987 TargetDepAttrs[A] = V;
988 return *this;
989}
990
Bill Wendling23804da2013-01-31 23:38:01 +0000991AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
Benjamin Kramereaf706b2013-02-18 12:09:51 +0000992 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
993 Attrs[Val] = false;
Bill Wendling23804da2013-01-31 23:38:01 +0000994
995 if (Val == Attribute::Alignment)
996 Alignment = 0;
997 else if (Val == Attribute::StackAlignment)
998 StackAlignment = 0;
999
1000 return *this;
1001}
1002
Bill Wendlingd2196752013-01-30 23:07:40 +00001003AttrBuilder &AttrBuilder::removeAttributes(AttributeSet A, uint64_t Index) {
Bill Wendling211316c2013-04-18 20:17:28 +00001004 unsigned Slot = ~0U;
Bill Wendlingf1c94e32013-02-01 00:13:50 +00001005 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
1006 if (A.getSlotIndex(I) == Index) {
Bill Wendling211316c2013-04-18 20:17:28 +00001007 Slot = I;
Bill Wendlingf1c94e32013-02-01 00:13:50 +00001008 break;
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001009 }
Bill Wendlingf1c94e32013-02-01 00:13:50 +00001010
Bill Wendling211316c2013-04-18 20:17:28 +00001011 assert(Slot != ~0U && "Couldn't find index in AttributeSet!");
Bill Wendlingf1c94e32013-02-01 00:13:50 +00001012
Bill Wendling211316c2013-04-18 20:17:28 +00001013 for (AttributeSet::iterator I = A.begin(Slot), E = A.end(Slot); I != E; ++I) {
Bill Wendling7cde51d2013-02-12 07:56:49 +00001014 Attribute Attr = *I;
1015 if (Attr.isEnumAttribute() || Attr.isAlignAttribute()) {
1016 Attribute::AttrKind Kind = I->getKindAsEnum();
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001017 Attrs[Kind] = false;
Bill Wendlingf1c94e32013-02-01 00:13:50 +00001018
Bill Wendling7cde51d2013-02-12 07:56:49 +00001019 if (Kind == Attribute::Alignment)
1020 Alignment = 0;
1021 else if (Kind == Attribute::StackAlignment)
1022 StackAlignment = 0;
1023 } else {
1024 assert(Attr.isStringAttribute() && "Invalid attribute type!");
1025 std::map<std::string, std::string>::iterator
1026 Iter = TargetDepAttrs.find(Attr.getKindAsString());
1027 if (Iter != TargetDepAttrs.end())
1028 TargetDepAttrs.erase(Iter);
1029 }
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001030 }
1031
1032 return *this;
1033}
1034
Bill Wendlingb9c5b1a2013-02-05 08:09:32 +00001035AttrBuilder &AttrBuilder::removeAttribute(StringRef A) {
1036 std::map<std::string, std::string>::iterator I = TargetDepAttrs.find(A);
1037 if (I != TargetDepAttrs.end())
1038 TargetDepAttrs.erase(I);
1039 return *this;
1040}
1041
Bill Wendling50d27842012-10-15 20:35:56 +00001042AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
Bill Wendlingabd5ba22012-10-14 03:58:29 +00001043 if (Align == 0) return *this;
Bill Wendlingcd330342013-01-04 23:27:34 +00001044
Bill Wendlingabf3feb2012-10-05 06:44:41 +00001045 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1046 assert(Align <= 0x40000000 && "Alignment too large.");
Bill Wendlingcd330342013-01-04 23:27:34 +00001047
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001048 Attrs[Attribute::Alignment] = true;
Bill Wendlingcd330342013-01-04 23:27:34 +00001049 Alignment = Align;
Bill Wendlingabd5ba22012-10-14 03:58:29 +00001050 return *this;
Chris Lattner3e13b8c2008-01-02 23:42:30 +00001051}
1052
Bill Wendlingcd330342013-01-04 23:27:34 +00001053AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
1054 // Default alignment, allow the target to define how to align it.
1055 if (Align == 0) return *this;
1056
1057 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1058 assert(Align <= 0x100 && "Alignment too large.");
1059
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001060 Attrs[Attribute::StackAlignment] = true;
Bill Wendlingcd330342013-01-04 23:27:34 +00001061 StackAlignment = Align;
1062 return *this;
1063}
1064
Bill Wendlinge2614922013-02-06 01:16:00 +00001065AttrBuilder &AttrBuilder::merge(const AttrBuilder &B) {
1066 // FIXME: What if both have alignments, but they don't match?!
1067 if (!Alignment)
1068 Alignment = B.Alignment;
1069
1070 if (!StackAlignment)
1071 StackAlignment = B.StackAlignment;
1072
Benjamin Kramer45e7d532013-02-16 19:13:18 +00001073 Attrs |= B.Attrs;
Bill Wendlinge2614922013-02-06 01:16:00 +00001074
1075 for (td_const_iterator I = B.TargetDepAttrs.begin(),
1076 E = B.TargetDepAttrs.end(); I != E; ++I)
1077 TargetDepAttrs[I->first] = I->second;
1078
1079 return *this;
1080}
1081
Bill Wendling4b001442013-02-06 01:33:42 +00001082bool AttrBuilder::contains(StringRef A) const {
1083 return TargetDepAttrs.find(A) != TargetDepAttrs.end();
1084}
1085
Bill Wendling50d27842012-10-15 20:35:56 +00001086bool AttrBuilder::hasAttributes() const {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001087 return !Attrs.none() || !TargetDepAttrs.empty();
Bill Wendlingc6daefa2012-10-08 23:27:46 +00001088}
Bill Wendling9ac69f92013-01-04 20:54:35 +00001089
Bill Wendlingd2196752013-01-30 23:07:40 +00001090bool AttrBuilder::hasAttributes(AttributeSet A, uint64_t Index) const {
Bill Wendling211316c2013-04-18 20:17:28 +00001091 unsigned Slot = ~0U;
Bill Wendling9ca01da2013-02-02 00:42:06 +00001092 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
1093 if (A.getSlotIndex(I) == Index) {
Bill Wendling211316c2013-04-18 20:17:28 +00001094 Slot = I;
Bill Wendling9ca01da2013-02-02 00:42:06 +00001095 break;
1096 }
1097
Bill Wendling211316c2013-04-18 20:17:28 +00001098 assert(Slot != ~0U && "Couldn't find the index!");
Bill Wendling9ca01da2013-02-02 00:42:06 +00001099
Bill Wendling211316c2013-04-18 20:17:28 +00001100 for (AttributeSet::iterator I = A.begin(Slot), E = A.end(Slot);
Bill Wendling7cde51d2013-02-12 07:56:49 +00001101 I != E; ++I) {
1102 Attribute Attr = *I;
1103 if (Attr.isEnumAttribute() || Attr.isAlignAttribute()) {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001104 if (Attrs[I->getKindAsEnum()])
Bill Wendling7cde51d2013-02-12 07:56:49 +00001105 return true;
1106 } else {
1107 assert(Attr.isStringAttribute() && "Invalid attribute kind!");
1108 return TargetDepAttrs.find(Attr.getKindAsString())!=TargetDepAttrs.end();
1109 }
1110 }
Bill Wendling9ca01da2013-02-02 00:42:06 +00001111
1112 return false;
Bill Wendling70f39172012-10-09 00:01:21 +00001113}
Bill Wendling9ac69f92013-01-04 20:54:35 +00001114
Bill Wendling50d27842012-10-15 20:35:56 +00001115bool AttrBuilder::hasAlignmentAttr() const {
Bill Wendlingcd330342013-01-04 23:27:34 +00001116 return Alignment != 0;
Bill Wendlingc6daefa2012-10-08 23:27:46 +00001117}
1118
Bill Wendlingd509a662013-01-29 00:34:06 +00001119bool AttrBuilder::operator==(const AttrBuilder &B) {
Benjamin Kramer45e7d532013-02-16 19:13:18 +00001120 if (Attrs != B.Attrs)
1121 return false;
Bill Wendling4b001442013-02-06 01:33:42 +00001122
1123 for (td_const_iterator I = TargetDepAttrs.begin(),
1124 E = TargetDepAttrs.end(); I != E; ++I)
1125 if (B.TargetDepAttrs.find(I->first) == B.TargetDepAttrs.end())
1126 return false;
1127
1128 return Alignment == B.Alignment && StackAlignment == B.StackAlignment;
Bill Wendlingd509a662013-01-29 00:34:06 +00001129}
1130
1131AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) {
Bill Wendling56aeccc2013-02-04 23:32:23 +00001132 // FIXME: Remove this in 4.0.
Bill Wendling60011b82013-01-29 01:43:29 +00001133 if (!Val) return *this;
1134
Bill Wendlingd509a662013-01-29 00:34:06 +00001135 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
1136 I = Attribute::AttrKind(I + 1)) {
1137 if (uint64_t A = (Val & AttributeImpl::getAttrMask(I))) {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001138 Attrs[I] = true;
Bill Wendlingd509a662013-01-29 00:34:06 +00001139
1140 if (I == Attribute::Alignment)
1141 Alignment = 1ULL << ((A >> 16) - 1);
1142 else if (I == Attribute::StackAlignment)
1143 StackAlignment = 1ULL << ((A >> 26)-1);
1144 }
1145 }
1146
1147 return *this;
1148}
1149
Bill Wendling57625a42013-01-25 23:09:36 +00001150//===----------------------------------------------------------------------===//
1151// AttributeFuncs Function Defintions
1152//===----------------------------------------------------------------------===//
1153
Bill Wendlingc79cdff2013-02-01 01:04:27 +00001154/// \brief Which attributes cannot be applied to a type.
Bill Wendlingd2196752013-01-30 23:07:40 +00001155AttributeSet AttributeFuncs::typeIncompatible(Type *Ty, uint64_t Index) {
Bill Wendling57625a42013-01-25 23:09:36 +00001156 AttrBuilder Incompatible;
1157
1158 if (!Ty->isIntegerTy())
1159 // Attribute that only apply to integers.
1160 Incompatible.addAttribute(Attribute::SExt)
1161 .addAttribute(Attribute::ZExt);
1162
1163 if (!Ty->isPointerTy())
1164 // Attribute that only apply to pointers.
1165 Incompatible.addAttribute(Attribute::ByVal)
1166 .addAttribute(Attribute::Nest)
1167 .addAttribute(Attribute::NoAlias)
1168 .addAttribute(Attribute::NoCapture)
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001169 .addAttribute(Attribute::ReadNone)
1170 .addAttribute(Attribute::ReadOnly)
Bill Wendling57625a42013-01-25 23:09:36 +00001171 .addAttribute(Attribute::StructRet);
1172
Bill Wendlingd2196752013-01-30 23:07:40 +00001173 return AttributeSet::get(Ty->getContext(), Index, Incompatible);
Bill Wendling57625a42013-01-25 23:09:36 +00001174}