blob: bcd324c4ffa45f9731a3ebc0964b560fa8633e1f [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
Alexey Samsonov49109a22013-11-18 09:31:53 +0000289AttributeImpl::~AttributeImpl() {}
290
Bill Wendlingd509a662013-01-29 00:34:06 +0000291bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
Bill Wendling3f12ac22013-02-05 22:37:24 +0000292 if (isStringAttribute()) return false;
293 return getKindAsEnum() == A;
Bill Wendlingd509a662013-01-29 00:34:06 +0000294}
295
Bill Wendling3f12ac22013-02-05 22:37:24 +0000296bool AttributeImpl::hasAttribute(StringRef Kind) const {
297 if (!isStringAttribute()) return false;
298 return getKindAsString() == Kind;
Bill Wendlingd509a662013-01-29 00:34:06 +0000299}
300
Bill Wendling3f12ac22013-02-05 22:37:24 +0000301Attribute::AttrKind AttributeImpl::getKindAsEnum() const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000302 assert(isEnumAttribute() || isAlignAttribute());
303 return static_cast<const EnumAttributeImpl *>(this)->getEnumKind();
Bill Wendlingd509a662013-01-29 00:34:06 +0000304}
305
Bill Wendling3f12ac22013-02-05 22:37:24 +0000306uint64_t AttributeImpl::getValueAsInt() const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000307 assert(isAlignAttribute());
308 return static_cast<const AlignAttributeImpl *>(this)->getAlignment();
Bill Wendlingd509a662013-01-29 00:34:06 +0000309}
310
Bill Wendling3f12ac22013-02-05 22:37:24 +0000311StringRef AttributeImpl::getKindAsString() const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000312 assert(isStringAttribute());
313 return static_cast<const StringAttributeImpl *>(this)->getStringKind();
Bill Wendlingd509a662013-01-29 00:34:06 +0000314}
315
Bill Wendling3f12ac22013-02-05 22:37:24 +0000316StringRef AttributeImpl::getValueAsString() const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000317 assert(isStringAttribute());
318 return static_cast<const StringAttributeImpl *>(this)->getStringValue();
Bill Wendlingd509a662013-01-29 00:34:06 +0000319}
320
321bool AttributeImpl::operator<(const AttributeImpl &AI) const {
Bill Wendlingc79cdff2013-02-01 01:04:27 +0000322 // This sorts the attributes with Attribute::AttrKinds coming first (sorted
323 // relative to their enum value) and then strings.
Bill Wendling26b95752013-02-15 05:25:26 +0000324 if (isEnumAttribute()) {
325 if (AI.isEnumAttribute()) return getKindAsEnum() < AI.getKindAsEnum();
326 if (AI.isAlignAttribute()) return true;
327 if (AI.isStringAttribute()) return true;
328 }
Bill Wendlingc79cdff2013-02-01 01:04:27 +0000329
Bill Wendling3f12ac22013-02-05 22:37:24 +0000330 if (isAlignAttribute()) {
Bill Wendling26b95752013-02-15 05:25:26 +0000331 if (AI.isEnumAttribute()) return false;
332 if (AI.isAlignAttribute()) return getValueAsInt() < AI.getValueAsInt();
333 if (AI.isStringAttribute()) return true;
Bill Wendling3f12ac22013-02-05 22:37:24 +0000334 }
Bill Wendlingd509a662013-01-29 00:34:06 +0000335
Bill Wendling26b95752013-02-15 05:25:26 +0000336 if (AI.isEnumAttribute()) return false;
337 if (AI.isAlignAttribute()) return false;
338 if (getKindAsString() == AI.getKindAsString())
339 return getValueAsString() < AI.getValueAsString();
340 return getKindAsString() < AI.getKindAsString();
Bill Wendlingd509a662013-01-29 00:34:06 +0000341}
342
Bill Wendlingd509a662013-01-29 00:34:06 +0000343uint64_t AttributeImpl::getAttrMask(Attribute::AttrKind Val) {
344 // FIXME: Remove this.
345 switch (Val) {
346 case Attribute::EndAttrKinds:
Bill Wendlingd509a662013-01-29 00:34:06 +0000347 llvm_unreachable("Synthetic enumerators which should never get here");
348
349 case Attribute::None: return 0;
350 case Attribute::ZExt: return 1 << 0;
351 case Attribute::SExt: return 1 << 1;
352 case Attribute::NoReturn: return 1 << 2;
353 case Attribute::InReg: return 1 << 3;
354 case Attribute::StructRet: return 1 << 4;
355 case Attribute::NoUnwind: return 1 << 5;
356 case Attribute::NoAlias: return 1 << 6;
357 case Attribute::ByVal: return 1 << 7;
358 case Attribute::Nest: return 1 << 8;
359 case Attribute::ReadNone: return 1 << 9;
360 case Attribute::ReadOnly: return 1 << 10;
361 case Attribute::NoInline: return 1 << 11;
362 case Attribute::AlwaysInline: return 1 << 12;
363 case Attribute::OptimizeForSize: return 1 << 13;
364 case Attribute::StackProtect: return 1 << 14;
365 case Attribute::StackProtectReq: return 1 << 15;
366 case Attribute::Alignment: return 31 << 16;
367 case Attribute::NoCapture: return 1 << 21;
368 case Attribute::NoRedZone: return 1 << 22;
369 case Attribute::NoImplicitFloat: return 1 << 23;
370 case Attribute::Naked: return 1 << 24;
371 case Attribute::InlineHint: return 1 << 25;
372 case Attribute::StackAlignment: return 7 << 26;
373 case Attribute::ReturnsTwice: return 1 << 29;
374 case Attribute::UWTable: return 1 << 30;
375 case Attribute::NonLazyBind: return 1U << 31;
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000376 case Attribute::SanitizeAddress: return 1ULL << 32;
Bill Wendlingd509a662013-01-29 00:34:06 +0000377 case Attribute::MinSize: return 1ULL << 33;
378 case Attribute::NoDuplicate: return 1ULL << 34;
379 case Attribute::StackProtectStrong: return 1ULL << 35;
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000380 case Attribute::SanitizeThread: return 1ULL << 36;
381 case Attribute::SanitizeMemory: return 1ULL << 37;
Bill Wendling7bfdfe72013-02-22 00:40:12 +0000382 case Attribute::NoBuiltin: return 1ULL << 38;
Stephen Linb8bd2322013-04-20 05:14:40 +0000383 case Attribute::Returned: return 1ULL << 39;
Diego Novilloc6399532013-05-24 12:26:52 +0000384 case Attribute::Cold: return 1ULL << 40;
Michael Gottesman41748d72013-06-27 00:25:01 +0000385 case Attribute::Builtin: return 1ULL << 41;
Andrea Di Biagio377496b2013-08-23 11:53:55 +0000386 case Attribute::OptimizeNone: return 1ULL << 42;
Bill Wendling25342e12013-02-22 00:50:09 +0000387 }
388 llvm_unreachable("Unsupported attribute type");
Bill Wendlingd509a662013-01-29 00:34:06 +0000389}
390
391//===----------------------------------------------------------------------===//
392// AttributeSetNode Definition
393//===----------------------------------------------------------------------===//
394
395AttributeSetNode *AttributeSetNode::get(LLVMContext &C,
396 ArrayRef<Attribute> Attrs) {
397 if (Attrs.empty())
398 return 0;
399
400 // Otherwise, build a key to look up the existing attributes.
401 LLVMContextImpl *pImpl = C.pImpl;
402 FoldingSetNodeID ID;
403
404 SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end());
Bill Wendling82c2ee62013-02-13 09:26:26 +0000405 array_pod_sort(SortedAttrs.begin(), SortedAttrs.end());
Bill Wendlingd509a662013-01-29 00:34:06 +0000406
407 for (SmallVectorImpl<Attribute>::iterator I = SortedAttrs.begin(),
408 E = SortedAttrs.end(); I != E; ++I)
409 I->Profile(ID);
410
411 void *InsertPoint;
412 AttributeSetNode *PA =
413 pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint);
414
415 // If we didn't find any existing attributes of the same shape then create a
416 // new one and insert it.
417 if (!PA) {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000418 // Coallocate entries after the AttributeSetNode itself.
419 void *Mem = ::operator new(sizeof(AttributeSetNode) +
420 sizeof(Attribute) * SortedAttrs.size());
421 PA = new (Mem) AttributeSetNode(SortedAttrs);
Bill Wendlingd509a662013-01-29 00:34:06 +0000422 pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint);
423 }
424
425 // Return the AttributesListNode that we found or created.
426 return PA;
427}
428
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000429bool AttributeSetNode::hasAttribute(Attribute::AttrKind Kind) const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000430 for (iterator I = begin(), E = end(); I != E; ++I)
NAKAMURA Takumib0944392013-01-31 03:47:28 +0000431 if (I->hasAttribute(Kind))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000432 return true;
433 return false;
434}
435
Bill Wendlingbce7b972013-02-13 08:42:21 +0000436bool AttributeSetNode::hasAttribute(StringRef Kind) const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000437 for (iterator I = begin(), E = end(); I != E; ++I)
Bill Wendlingbce7b972013-02-13 08:42:21 +0000438 if (I->hasAttribute(Kind))
439 return true;
440 return false;
441}
442
443Attribute AttributeSetNode::getAttribute(Attribute::AttrKind 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 *I;
447 return Attribute();
448}
449
450Attribute AttributeSetNode::getAttribute(StringRef 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
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000457unsigned AttributeSetNode::getAlignment() const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000458 for (iterator I = begin(), E = end(); I != E; ++I)
NAKAMURA Takumib0944392013-01-31 03:47:28 +0000459 if (I->hasAttribute(Attribute::Alignment))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000460 return I->getAlignment();
461 return 0;
462}
463
464unsigned AttributeSetNode::getStackAlignment() 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::StackAlignment))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000467 return I->getStackAlignment();
468 return 0;
469}
470
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +0000471std::string AttributeSetNode::getAsString(bool InAttrGrp) const {
Benjamin Kramer0baf8f42013-04-19 11:43:21 +0000472 std::string Str;
Benjamin Kramer741146b2013-07-11 12:13:16 +0000473 for (iterator I = begin(), E = end(); I != E; ++I) {
474 if (I != begin())
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +0000475 Str += ' ';
476 Str += I->getAsString(InAttrGrp);
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000477 }
478 return Str;
479}
480
Bill Wendlingd509a662013-01-29 00:34:06 +0000481//===----------------------------------------------------------------------===//
482// AttributeSetImpl Definition
483//===----------------------------------------------------------------------===//
484
Rafael Espindoladd275302013-04-30 16:53:38 +0000485uint64_t AttributeSetImpl::Raw(unsigned Index) const {
Bill Wendlingd509a662013-01-29 00:34:06 +0000486 for (unsigned I = 0, E = getNumAttributes(); I != E; ++I) {
487 if (getSlotIndex(I) != Index) continue;
Benjamin Kramer741146b2013-07-11 12:13:16 +0000488 const AttributeSetNode *ASN = getSlotNode(I);
Bill Wendling91226182013-02-02 00:52:44 +0000489 uint64_t Mask = 0;
Bill Wendlingd509a662013-01-29 00:34:06 +0000490
Benjamin Kramer741146b2013-07-11 12:13:16 +0000491 for (AttributeSetNode::iterator II = ASN->begin(),
Bill Wendling91226182013-02-02 00:52:44 +0000492 IE = ASN->end(); II != IE; ++II) {
493 Attribute Attr = *II;
Bill Wendlingd746e402013-02-10 23:18:05 +0000494
495 // This cannot handle string attributes.
496 if (Attr.isStringAttribute()) continue;
497
Bill Wendling3f12ac22013-02-05 22:37:24 +0000498 Attribute::AttrKind Kind = Attr.getKindAsEnum();
Bill Wendling91226182013-02-02 00:52:44 +0000499
Bill Wendling3f12ac22013-02-05 22:37:24 +0000500 if (Kind == Attribute::Alignment)
Bill Wendling91226182013-02-02 00:52:44 +0000501 Mask |= (Log2_32(ASN->getAlignment()) + 1) << 16;
Bill Wendling3f12ac22013-02-05 22:37:24 +0000502 else if (Kind == Attribute::StackAlignment)
Bill Wendling91226182013-02-02 00:52:44 +0000503 Mask |= (Log2_32(ASN->getStackAlignment()) + 1) << 26;
504 else
Bill Wendling3f12ac22013-02-05 22:37:24 +0000505 Mask |= AttributeImpl::getAttrMask(Kind);
Bill Wendling91226182013-02-02 00:52:44 +0000506 }
507
508 return Mask;
Bill Wendlingd509a662013-01-29 00:34:06 +0000509 }
510
511 return 0;
512}
513
Peter Collingbourneabca2ec2013-08-02 22:34:30 +0000514void AttributeSetImpl::dump() const {
515 AttributeSet(const_cast<AttributeSetImpl *>(this)).dump();
516}
517
Bill Wendlingd509a662013-01-29 00:34:06 +0000518//===----------------------------------------------------------------------===//
519// AttributeSet Construction and Mutation Methods
520//===----------------------------------------------------------------------===//
521
Bill Wendling60011b82013-01-29 01:43:29 +0000522AttributeSet
523AttributeSet::getImpl(LLVMContext &C,
524 ArrayRef<std::pair<unsigned, AttributeSetNode*> > Attrs) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000525 LLVMContextImpl *pImpl = C.pImpl;
526 FoldingSetNodeID ID;
527 AttributeSetImpl::Profile(ID, Attrs);
528
529 void *InsertPoint;
530 AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
531
532 // If we didn't find any existing attributes of the same shape then
533 // create a new one and insert it.
534 if (!PA) {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000535 // Coallocate entries after the AttributeSetImpl itself.
536 void *Mem = ::operator new(sizeof(AttributeSetImpl) +
537 sizeof(std::pair<unsigned, AttributeSetNode *>) *
538 Attrs.size());
539 PA = new (Mem) AttributeSetImpl(C, Attrs);
Bill Wendlingd509a662013-01-29 00:34:06 +0000540 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
541 }
542
543 // Return the AttributesList that we found or created.
544 return AttributeSet(PA);
545}
546
547AttributeSet AttributeSet::get(LLVMContext &C,
548 ArrayRef<std::pair<unsigned, Attribute> > Attrs){
549 // If there are no attributes then return a null AttributesList pointer.
550 if (Attrs.empty())
551 return AttributeSet();
552
553#ifndef NDEBUG
554 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
555 assert((!i || Attrs[i-1].first <= Attrs[i].first) &&
556 "Misordered Attributes list!");
Bill Wendling3f12ac22013-02-05 22:37:24 +0000557 assert(!Attrs[i].second.hasAttribute(Attribute::None) &&
Bill Wendlingd509a662013-01-29 00:34:06 +0000558 "Pointless attribute!");
559 }
560#endif
561
562 // Create a vector if (unsigned, AttributeSetNode*) pairs from the attributes
563 // list.
564 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrPairVec;
565 for (ArrayRef<std::pair<unsigned, Attribute> >::iterator I = Attrs.begin(),
566 E = Attrs.end(); I != E; ) {
567 unsigned Index = I->first;
568 SmallVector<Attribute, 4> AttrVec;
NAKAMURA Takumif05d2f22013-01-29 15:18:16 +0000569 while (I != E && I->first == Index) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000570 AttrVec.push_back(I->second);
571 ++I;
572 }
573
574 AttrPairVec.push_back(std::make_pair(Index,
575 AttributeSetNode::get(C, AttrVec)));
576 }
577
578 return getImpl(C, AttrPairVec);
579}
580
581AttributeSet AttributeSet::get(LLVMContext &C,
582 ArrayRef<std::pair<unsigned,
583 AttributeSetNode*> > Attrs) {
584 // If there are no attributes then return a null AttributesList pointer.
585 if (Attrs.empty())
586 return AttributeSet();
587
588 return getImpl(C, Attrs);
589}
590
Bill Wendling211316c2013-04-18 20:17:28 +0000591AttributeSet AttributeSet::get(LLVMContext &C, unsigned Index, AttrBuilder &B) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000592 if (!B.hasAttributes())
593 return AttributeSet();
Bill Wendlingf7134812013-01-29 01:02:03 +0000594
Bill Wendlingae89a0f2013-02-05 23:48:36 +0000595 // Add target-independent attributes.
Bill Wendlingf7134812013-01-29 01:02:03 +0000596 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
Benjamin Kramer45e7d532013-02-16 19:13:18 +0000597 for (Attribute::AttrKind Kind = Attribute::None;
Benjamin Kramer6f37dac2013-02-16 19:22:28 +0000598 Kind != Attribute::EndAttrKinds; Kind = Attribute::AttrKind(Kind + 1)) {
Benjamin Kramer45e7d532013-02-16 19:13:18 +0000599 if (!B.contains(Kind))
600 continue;
601
Bill Wendlingf7134812013-01-29 01:02:03 +0000602 if (Kind == Attribute::Alignment)
Bill Wendling211316c2013-04-18 20:17:28 +0000603 Attrs.push_back(std::make_pair(Index, Attribute::
Bill Wendlingf7134812013-01-29 01:02:03 +0000604 getWithAlignment(C, B.getAlignment())));
605 else if (Kind == Attribute::StackAlignment)
Bill Wendling211316c2013-04-18 20:17:28 +0000606 Attrs.push_back(std::make_pair(Index, Attribute::
Bill Wendlingf7134812013-01-29 01:02:03 +0000607 getWithStackAlignment(C, B.getStackAlignment())));
608 else
Bill Wendling211316c2013-04-18 20:17:28 +0000609 Attrs.push_back(std::make_pair(Index, Attribute::get(C, Kind)));
Bill Wendlingf7134812013-01-29 01:02:03 +0000610 }
611
Bill Wendlingae89a0f2013-02-05 23:48:36 +0000612 // Add target-dependent (string) attributes.
613 for (AttrBuilder::td_iterator I = B.td_begin(), E = B.td_end();
614 I != E; ++I)
Bill Wendling211316c2013-04-18 20:17:28 +0000615 Attrs.push_back(std::make_pair(Index, Attribute::get(C, I->first,I->second)));
Bill Wendlingae89a0f2013-02-05 23:48:36 +0000616
Bill Wendlingf7134812013-01-29 01:02:03 +0000617 return get(C, Attrs);
Bill Wendlingd509a662013-01-29 00:34:06 +0000618}
619
Bill Wendling211316c2013-04-18 20:17:28 +0000620AttributeSet AttributeSet::get(LLVMContext &C, unsigned Index,
Bill Wendlingd509a662013-01-29 00:34:06 +0000621 ArrayRef<Attribute::AttrKind> Kind) {
622 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
623 for (ArrayRef<Attribute::AttrKind>::iterator I = Kind.begin(),
624 E = Kind.end(); I != E; ++I)
Bill Wendling211316c2013-04-18 20:17:28 +0000625 Attrs.push_back(std::make_pair(Index, Attribute::get(C, *I)));
Bill Wendlingd509a662013-01-29 00:34:06 +0000626 return get(C, Attrs);
627}
628
629AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<AttributeSet> Attrs) {
630 if (Attrs.empty()) return AttributeSet();
Peter Collingbournebd6c7452013-08-02 22:29:40 +0000631 if (Attrs.size() == 1) return Attrs[0];
Bill Wendlingd509a662013-01-29 00:34:06 +0000632
633 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrNodeVec;
Peter Collingbournebd6c7452013-08-02 22:29:40 +0000634 AttributeSetImpl *A0 = Attrs[0].pImpl;
635 if (A0)
636 AttrNodeVec.append(A0->getNode(0), A0->getNode(A0->getNumAttributes()));
637 // Copy all attributes from Attrs into AttrNodeVec while keeping AttrNodeVec
638 // ordered by index. Because we know that each list in Attrs is ordered by
639 // index we only need to merge each successive list in rather than doing a
640 // full sort.
641 for (unsigned I = 1, E = Attrs.size(); I != E; ++I) {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000642 AttributeSetImpl *AS = Attrs[I].pImpl;
643 if (!AS) continue;
Peter Collingbournebd6c7452013-08-02 22:29:40 +0000644 SmallVector<std::pair<unsigned, AttributeSetNode *>, 8>::iterator
645 ANVI = AttrNodeVec.begin(), ANVE;
646 for (const AttributeSetImpl::IndexAttrPair
647 *AI = AS->getNode(0),
648 *AE = AS->getNode(AS->getNumAttributes());
649 AI != AE; ++AI) {
650 ANVE = AttrNodeVec.end();
651 while (ANVI != ANVE && ANVI->first <= AI->first)
652 ++ANVI;
653 ANVI = AttrNodeVec.insert(ANVI, *AI) + 1;
654 }
Bill Wendlingd509a662013-01-29 00:34:06 +0000655 }
656
657 return getImpl(C, AttrNodeVec);
658}
659
Bill Wendling211316c2013-04-18 20:17:28 +0000660AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
Bill Wendlingd509a662013-01-29 00:34:06 +0000661 Attribute::AttrKind Attr) const {
Bill Wendling211316c2013-04-18 20:17:28 +0000662 if (hasAttribute(Index, Attr)) return *this;
663 return addAttributes(C, Index, AttributeSet::get(C, Index, Attr));
Bill Wendlingd509a662013-01-29 00:34:06 +0000664}
665
Bill Wendling211316c2013-04-18 20:17:28 +0000666AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
Reed Kotler795c7b42013-03-13 20:20:08 +0000667 StringRef Kind) const {
668 llvm::AttrBuilder B;
669 B.addAttribute(Kind);
Bill Wendling211316c2013-04-18 20:17:28 +0000670 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
Reed Kotler795c7b42013-03-13 20:20:08 +0000671}
672
Bill Wendling3b2f6102013-07-25 18:34:24 +0000673AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
674 StringRef Kind, StringRef Value) const {
675 llvm::AttrBuilder B;
676 B.addAttribute(Kind, Value);
677 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
678}
679
Bill Wendling211316c2013-04-18 20:17:28 +0000680AttributeSet AttributeSet::addAttributes(LLVMContext &C, unsigned Index,
Bill Wendlingd509a662013-01-29 00:34:06 +0000681 AttributeSet Attrs) const {
682 if (!pImpl) return Attrs;
683 if (!Attrs.pImpl) return *this;
684
685#ifndef NDEBUG
686 // FIXME it is not obvious how this should work for alignment. For now, say
687 // we can't change a known alignment.
Bill Wendling211316c2013-04-18 20:17:28 +0000688 unsigned OldAlign = getParamAlignment(Index);
689 unsigned NewAlign = Attrs.getParamAlignment(Index);
Bill Wendlingd509a662013-01-29 00:34:06 +0000690 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
691 "Attempt to change alignment!");
692#endif
693
694 // Add the attribute slots before the one we're trying to add.
695 SmallVector<AttributeSet, 4> AttrSet;
696 uint64_t NumAttrs = pImpl->getNumAttributes();
697 AttributeSet AS;
698 uint64_t LastIndex = 0;
699 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
Bill Wendling211316c2013-04-18 20:17:28 +0000700 if (getSlotIndex(I) >= Index) {
701 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
Bill Wendlingd509a662013-01-29 00:34:06 +0000702 break;
703 }
704 LastIndex = I + 1;
705 AttrSet.push_back(getSlotAttributes(I));
706 }
707
708 // Now add the attribute into the correct slot. There may already be an
709 // AttributeSet there.
Bill Wendling211316c2013-04-18 20:17:28 +0000710 AttrBuilder B(AS, Index);
Bill Wendlingd509a662013-01-29 00:34:06 +0000711
712 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
Bill Wendling211316c2013-04-18 20:17:28 +0000713 if (Attrs.getSlotIndex(I) == Index) {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000714 for (AttributeSetImpl::iterator II = Attrs.pImpl->begin(I),
Bill Wendlingd509a662013-01-29 00:34:06 +0000715 IE = Attrs.pImpl->end(I); II != IE; ++II)
Bill Wendling23804da2013-01-31 23:38:01 +0000716 B.addAttribute(*II);
Bill Wendlingd509a662013-01-29 00:34:06 +0000717 break;
718 }
719
Bill Wendling211316c2013-04-18 20:17:28 +0000720 AttrSet.push_back(AttributeSet::get(C, Index, B));
Bill Wendlingd509a662013-01-29 00:34:06 +0000721
722 // Add the remaining attribute slots.
723 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
724 AttrSet.push_back(getSlotAttributes(I));
725
726 return get(C, AttrSet);
727}
728
Bill Wendling211316c2013-04-18 20:17:28 +0000729AttributeSet AttributeSet::removeAttribute(LLVMContext &C, unsigned Index,
Bill Wendlingd509a662013-01-29 00:34:06 +0000730 Attribute::AttrKind Attr) const {
Bill Wendling211316c2013-04-18 20:17:28 +0000731 if (!hasAttribute(Index, Attr)) return *this;
732 return removeAttributes(C, Index, AttributeSet::get(C, Index, Attr));
Bill Wendlingd509a662013-01-29 00:34:06 +0000733}
734
Bill Wendling211316c2013-04-18 20:17:28 +0000735AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Index,
Bill Wendlingd509a662013-01-29 00:34:06 +0000736 AttributeSet Attrs) const {
737 if (!pImpl) return AttributeSet();
738 if (!Attrs.pImpl) return *this;
739
740#ifndef NDEBUG
741 // FIXME it is not obvious how this should work for alignment.
742 // For now, say we can't pass in alignment, which no current use does.
Bill Wendling211316c2013-04-18 20:17:28 +0000743 assert(!Attrs.hasAttribute(Index, Attribute::Alignment) &&
Bill Wendlingd509a662013-01-29 00:34:06 +0000744 "Attempt to change alignment!");
745#endif
746
747 // Add the attribute slots before the one we're trying to add.
748 SmallVector<AttributeSet, 4> AttrSet;
749 uint64_t NumAttrs = pImpl->getNumAttributes();
750 AttributeSet AS;
751 uint64_t LastIndex = 0;
752 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
Bill Wendling211316c2013-04-18 20:17:28 +0000753 if (getSlotIndex(I) >= Index) {
754 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
Bill Wendlingd509a662013-01-29 00:34:06 +0000755 break;
756 }
757 LastIndex = I + 1;
758 AttrSet.push_back(getSlotAttributes(I));
759 }
760
Bill Wendlingd2196752013-01-30 23:07:40 +0000761 // Now remove the attribute from the correct slot. There may already be an
Bill Wendlingd509a662013-01-29 00:34:06 +0000762 // AttributeSet there.
Bill Wendling211316c2013-04-18 20:17:28 +0000763 AttrBuilder B(AS, Index);
Bill Wendlingd509a662013-01-29 00:34:06 +0000764
765 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
Bill Wendling211316c2013-04-18 20:17:28 +0000766 if (Attrs.getSlotIndex(I) == Index) {
767 B.removeAttributes(Attrs.pImpl->getSlotAttributes(I), Index);
Bill Wendlingd509a662013-01-29 00:34:06 +0000768 break;
769 }
770
Bill Wendling211316c2013-04-18 20:17:28 +0000771 AttrSet.push_back(AttributeSet::get(C, Index, B));
Bill Wendlingd509a662013-01-29 00:34:06 +0000772
773 // Add the remaining attribute slots.
774 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
775 AttrSet.push_back(getSlotAttributes(I));
776
777 return get(C, AttrSet);
778}
779
780//===----------------------------------------------------------------------===//
781// AttributeSet Accessor Methods
782//===----------------------------------------------------------------------===//
783
Bill Wendling5d020a32013-02-10 05:00:40 +0000784LLVMContext &AttributeSet::getContext() const {
785 return pImpl->getContext();
786}
787
Bill Wendling211316c2013-04-18 20:17:28 +0000788AttributeSet AttributeSet::getParamAttributes(unsigned Index) const {
789 return pImpl && hasAttributes(Index) ?
Bill Wendlingd509a662013-01-29 00:34:06 +0000790 AttributeSet::get(pImpl->getContext(),
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000791 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendling211316c2013-04-18 20:17:28 +0000792 std::make_pair(Index, getAttributes(Index)))) :
Bill Wendlingd509a662013-01-29 00:34:06 +0000793 AttributeSet();
794}
795
796AttributeSet AttributeSet::getRetAttributes() const {
797 return pImpl && hasAttributes(ReturnIndex) ?
798 AttributeSet::get(pImpl->getContext(),
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000799 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendlingd509a662013-01-29 00:34:06 +0000800 std::make_pair(ReturnIndex,
801 getAttributes(ReturnIndex)))) :
802 AttributeSet();
803}
804
805AttributeSet AttributeSet::getFnAttributes() const {
806 return pImpl && hasAttributes(FunctionIndex) ?
807 AttributeSet::get(pImpl->getContext(),
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000808 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendlingd509a662013-01-29 00:34:06 +0000809 std::make_pair(FunctionIndex,
810 getAttributes(FunctionIndex)))) :
811 AttributeSet();
812}
813
814bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000815 AttributeSetNode *ASN = getAttributes(Index);
816 return ASN ? ASN->hasAttribute(Kind) : false;
Bill Wendlingd509a662013-01-29 00:34:06 +0000817}
818
Bill Wendlingbce7b972013-02-13 08:42:21 +0000819bool AttributeSet::hasAttribute(unsigned Index, StringRef Kind) const {
820 AttributeSetNode *ASN = getAttributes(Index);
821 return ASN ? ASN->hasAttribute(Kind) : false;
822}
823
Bill Wendlingd509a662013-01-29 00:34:06 +0000824bool AttributeSet::hasAttributes(unsigned Index) const {
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000825 AttributeSetNode *ASN = getAttributes(Index);
826 return ASN ? ASN->hasAttributes() : false;
Bill Wendlingd509a662013-01-29 00:34:06 +0000827}
828
829/// \brief Return true if the specified attribute is set for at least one
830/// parameter or for the return value.
831bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr) const {
832 if (pImpl == 0) return false;
833
834 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
Benjamin Kramer741146b2013-07-11 12:13:16 +0000835 for (AttributeSetImpl::iterator II = pImpl->begin(I),
Bill Wendlingd509a662013-01-29 00:34:06 +0000836 IE = pImpl->end(I); II != IE; ++II)
NAKAMURA Takumib0944392013-01-31 03:47:28 +0000837 if (II->hasAttribute(Attr))
Bill Wendlingd509a662013-01-29 00:34:06 +0000838 return true;
839
840 return false;
841}
842
Bill Wendlingbce7b972013-02-13 08:42:21 +0000843Attribute AttributeSet::getAttribute(unsigned Index,
844 Attribute::AttrKind Kind) const {
845 AttributeSetNode *ASN = getAttributes(Index);
846 return ASN ? ASN->getAttribute(Kind) : Attribute();
847}
848
849Attribute AttributeSet::getAttribute(unsigned Index,
850 StringRef Kind) const {
851 AttributeSetNode *ASN = getAttributes(Index);
852 return ASN ? ASN->getAttribute(Kind) : Attribute();
853}
854
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000855unsigned AttributeSet::getParamAlignment(unsigned Index) const {
856 AttributeSetNode *ASN = getAttributes(Index);
857 return ASN ? ASN->getAlignment() : 0;
Bill Wendlingd509a662013-01-29 00:34:06 +0000858}
859
860unsigned AttributeSet::getStackAlignment(unsigned Index) const {
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000861 AttributeSetNode *ASN = getAttributes(Index);
862 return ASN ? ASN->getStackAlignment() : 0;
Bill Wendlingd509a662013-01-29 00:34:06 +0000863}
864
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +0000865std::string AttributeSet::getAsString(unsigned Index,
Bill Wendling829b4782013-02-11 08:43:33 +0000866 bool InAttrGrp) const {
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000867 AttributeSetNode *ASN = getAttributes(Index);
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +0000868 return ASN ? ASN->getAsString(InAttrGrp) : std::string("");
Bill Wendlingd509a662013-01-29 00:34:06 +0000869}
870
871/// \brief The attributes for the specified index are returned.
Bill Wendling211316c2013-04-18 20:17:28 +0000872AttributeSetNode *AttributeSet::getAttributes(unsigned Index) const {
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000873 if (!pImpl) return 0;
Bill Wendlingd509a662013-01-29 00:34:06 +0000874
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000875 // Loop through to find the attribute node we want.
876 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
Bill Wendling211316c2013-04-18 20:17:28 +0000877 if (pImpl->getSlotIndex(I) == Index)
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000878 return pImpl->getSlotNode(I);
Bill Wendlingd509a662013-01-29 00:34:06 +0000879
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000880 return 0;
Bill Wendlingd509a662013-01-29 00:34:06 +0000881}
882
Bill Wendling211316c2013-04-18 20:17:28 +0000883AttributeSet::iterator AttributeSet::begin(unsigned Slot) const {
Bill Wendlinga9174862013-01-31 23:53:05 +0000884 if (!pImpl)
885 return ArrayRef<Attribute>().begin();
Bill Wendling211316c2013-04-18 20:17:28 +0000886 return pImpl->begin(Slot);
Bill Wendlinga9174862013-01-31 23:53:05 +0000887}
888
Bill Wendling211316c2013-04-18 20:17:28 +0000889AttributeSet::iterator AttributeSet::end(unsigned Slot) const {
Bill Wendlinga9174862013-01-31 23:53:05 +0000890 if (!pImpl)
891 return ArrayRef<Attribute>().end();
Bill Wendling211316c2013-04-18 20:17:28 +0000892 return pImpl->end(Slot);
Bill Wendlinga9174862013-01-31 23:53:05 +0000893}
894
Bill Wendlingd509a662013-01-29 00:34:06 +0000895//===----------------------------------------------------------------------===//
896// AttributeSet Introspection Methods
897//===----------------------------------------------------------------------===//
898
899/// \brief Return the number of slots used in this attribute list. This is the
900/// number of arguments that have an attribute set on them (including the
901/// function itself).
902unsigned AttributeSet::getNumSlots() const {
903 return pImpl ? pImpl->getNumAttributes() : 0;
904}
905
Rafael Espindoladd275302013-04-30 16:53:38 +0000906unsigned AttributeSet::getSlotIndex(unsigned Slot) const {
Bill Wendlingd509a662013-01-29 00:34:06 +0000907 assert(pImpl && Slot < pImpl->getNumAttributes() &&
908 "Slot # out of range!");
909 return pImpl->getSlotIndex(Slot);
910}
911
912AttributeSet AttributeSet::getSlotAttributes(unsigned Slot) const {
913 assert(pImpl && Slot < pImpl->getNumAttributes() &&
914 "Slot # out of range!");
915 return pImpl->getSlotAttributes(Slot);
916}
917
918uint64_t AttributeSet::Raw(unsigned Index) const {
919 // FIXME: Remove this.
920 return pImpl ? pImpl->Raw(Index) : 0;
921}
922
923void AttributeSet::dump() const {
924 dbgs() << "PAL[\n";
925
926 for (unsigned i = 0, e = getNumSlots(); i < e; ++i) {
927 uint64_t Index = getSlotIndex(i);
928 dbgs() << " { ";
929 if (Index == ~0U)
930 dbgs() << "~0U";
931 else
932 dbgs() << Index;
933 dbgs() << " => " << getAsString(Index) << " }\n";
934 }
935
936 dbgs() << "]\n";
937}
938
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000939//===----------------------------------------------------------------------===//
Bill Wendlingcd330342013-01-04 23:27:34 +0000940// AttrBuilder Method Implementations
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000941//===----------------------------------------------------------------------===//
942
Bill Wendling211316c2013-04-18 20:17:28 +0000943AttrBuilder::AttrBuilder(AttributeSet AS, unsigned Index)
Benjamin Kramer45e7d532013-02-16 19:13:18 +0000944 : Attrs(0), Alignment(0), StackAlignment(0) {
Bill Wendling56b0b2a2013-01-27 21:23:46 +0000945 AttributeSetImpl *pImpl = AS.pImpl;
Bill Wendling096f5442013-01-07 08:24:35 +0000946 if (!pImpl) return;
947
Bill Wendling9eb689c2013-01-28 00:21:34 +0000948 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I) {
Bill Wendling211316c2013-04-18 20:17:28 +0000949 if (pImpl->getSlotIndex(I) != Index) continue;
Bill Wendling096f5442013-01-07 08:24:35 +0000950
Benjamin Kramer741146b2013-07-11 12:13:16 +0000951 for (AttributeSetImpl::iterator II = pImpl->begin(I),
Bill Wendling9eb689c2013-01-28 00:21:34 +0000952 IE = pImpl->end(I); II != IE; ++II)
Bill Wendling23804da2013-01-31 23:38:01 +0000953 addAttribute(*II);
Bill Wendling9eb689c2013-01-28 00:21:34 +0000954
955 break;
956 }
Bill Wendling096f5442013-01-07 08:24:35 +0000957}
958
Bill Wendlingcd330342013-01-04 23:27:34 +0000959void AttrBuilder::clear() {
Benjamin Kramereaf706b2013-02-18 12:09:51 +0000960 Attrs.reset();
Bill Wendlingcd330342013-01-04 23:27:34 +0000961 Alignment = StackAlignment = 0;
962}
963
964AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
Benjamin Kramereaf706b2013-02-18 12:09:51 +0000965 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
Bill Wendling1c7cc8a2013-01-31 23:16:25 +0000966 assert(Val != Attribute::Alignment && Val != Attribute::StackAlignment &&
967 "Adding alignment attribute without adding alignment value!");
Benjamin Kramereaf706b2013-02-18 12:09:51 +0000968 Attrs[Val] = true;
Bill Wendling7c04e042012-10-09 19:01:18 +0000969 return *this;
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000970}
971
Bill Wendling23804da2013-01-31 23:38:01 +0000972AttrBuilder &AttrBuilder::addAttribute(Attribute Attr) {
Bill Wendling0a437302013-02-10 10:13:23 +0000973 if (Attr.isStringAttribute()) {
974 addAttribute(Attr.getKindAsString(), Attr.getValueAsString());
975 return *this;
976 }
977
Bill Wendling3f12ac22013-02-05 22:37:24 +0000978 Attribute::AttrKind Kind = Attr.getKindAsEnum();
Benjamin Kramereaf706b2013-02-18 12:09:51 +0000979 Attrs[Kind] = true;
Bill Wendling1aa9d9e2013-01-28 05:23:28 +0000980
Bill Wendling3f12ac22013-02-05 22:37:24 +0000981 if (Kind == Attribute::Alignment)
Bill Wendling1aa9d9e2013-01-28 05:23:28 +0000982 Alignment = Attr.getAlignment();
Bill Wendling3f12ac22013-02-05 22:37:24 +0000983 else if (Kind == Attribute::StackAlignment)
Bill Wendling1aa9d9e2013-01-28 05:23:28 +0000984 StackAlignment = Attr.getStackAlignment();
985 return *this;
986}
987
Bill Wendlingb9c5b1a2013-02-05 08:09:32 +0000988AttrBuilder &AttrBuilder::addAttribute(StringRef A, StringRef V) {
989 TargetDepAttrs[A] = V;
990 return *this;
991}
992
Bill Wendling23804da2013-01-31 23:38:01 +0000993AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
Benjamin Kramereaf706b2013-02-18 12:09:51 +0000994 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
995 Attrs[Val] = false;
Bill Wendling23804da2013-01-31 23:38:01 +0000996
997 if (Val == Attribute::Alignment)
998 Alignment = 0;
999 else if (Val == Attribute::StackAlignment)
1000 StackAlignment = 0;
1001
1002 return *this;
1003}
1004
Bill Wendlingd2196752013-01-30 23:07:40 +00001005AttrBuilder &AttrBuilder::removeAttributes(AttributeSet A, uint64_t Index) {
Bill Wendling211316c2013-04-18 20:17:28 +00001006 unsigned Slot = ~0U;
Bill Wendlingf1c94e32013-02-01 00:13:50 +00001007 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
1008 if (A.getSlotIndex(I) == Index) {
Bill Wendling211316c2013-04-18 20:17:28 +00001009 Slot = I;
Bill Wendlingf1c94e32013-02-01 00:13:50 +00001010 break;
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001011 }
Bill Wendlingf1c94e32013-02-01 00:13:50 +00001012
Bill Wendling211316c2013-04-18 20:17:28 +00001013 assert(Slot != ~0U && "Couldn't find index in AttributeSet!");
Bill Wendlingf1c94e32013-02-01 00:13:50 +00001014
Bill Wendling211316c2013-04-18 20:17:28 +00001015 for (AttributeSet::iterator I = A.begin(Slot), E = A.end(Slot); I != E; ++I) {
Bill Wendling7cde51d2013-02-12 07:56:49 +00001016 Attribute Attr = *I;
1017 if (Attr.isEnumAttribute() || Attr.isAlignAttribute()) {
1018 Attribute::AttrKind Kind = I->getKindAsEnum();
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001019 Attrs[Kind] = false;
Bill Wendlingf1c94e32013-02-01 00:13:50 +00001020
Bill Wendling7cde51d2013-02-12 07:56:49 +00001021 if (Kind == Attribute::Alignment)
1022 Alignment = 0;
1023 else if (Kind == Attribute::StackAlignment)
1024 StackAlignment = 0;
1025 } else {
1026 assert(Attr.isStringAttribute() && "Invalid attribute type!");
1027 std::map<std::string, std::string>::iterator
1028 Iter = TargetDepAttrs.find(Attr.getKindAsString());
1029 if (Iter != TargetDepAttrs.end())
1030 TargetDepAttrs.erase(Iter);
1031 }
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001032 }
1033
1034 return *this;
1035}
1036
Bill Wendlingb9c5b1a2013-02-05 08:09:32 +00001037AttrBuilder &AttrBuilder::removeAttribute(StringRef A) {
1038 std::map<std::string, std::string>::iterator I = TargetDepAttrs.find(A);
1039 if (I != TargetDepAttrs.end())
1040 TargetDepAttrs.erase(I);
1041 return *this;
1042}
1043
Bill Wendling50d27842012-10-15 20:35:56 +00001044AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
Bill Wendlingabd5ba22012-10-14 03:58:29 +00001045 if (Align == 0) return *this;
Bill Wendlingcd330342013-01-04 23:27:34 +00001046
Bill Wendlingabf3feb2012-10-05 06:44:41 +00001047 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1048 assert(Align <= 0x40000000 && "Alignment too large.");
Bill Wendlingcd330342013-01-04 23:27:34 +00001049
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001050 Attrs[Attribute::Alignment] = true;
Bill Wendlingcd330342013-01-04 23:27:34 +00001051 Alignment = Align;
Bill Wendlingabd5ba22012-10-14 03:58:29 +00001052 return *this;
Chris Lattner3e13b8c2008-01-02 23:42:30 +00001053}
1054
Bill Wendlingcd330342013-01-04 23:27:34 +00001055AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
1056 // Default alignment, allow the target to define how to align it.
1057 if (Align == 0) return *this;
1058
1059 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1060 assert(Align <= 0x100 && "Alignment too large.");
1061
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001062 Attrs[Attribute::StackAlignment] = true;
Bill Wendlingcd330342013-01-04 23:27:34 +00001063 StackAlignment = Align;
1064 return *this;
1065}
1066
Bill Wendlinge2614922013-02-06 01:16:00 +00001067AttrBuilder &AttrBuilder::merge(const AttrBuilder &B) {
1068 // FIXME: What if both have alignments, but they don't match?!
1069 if (!Alignment)
1070 Alignment = B.Alignment;
1071
1072 if (!StackAlignment)
1073 StackAlignment = B.StackAlignment;
1074
Benjamin Kramer45e7d532013-02-16 19:13:18 +00001075 Attrs |= B.Attrs;
Bill Wendlinge2614922013-02-06 01:16:00 +00001076
1077 for (td_const_iterator I = B.TargetDepAttrs.begin(),
1078 E = B.TargetDepAttrs.end(); I != E; ++I)
1079 TargetDepAttrs[I->first] = I->second;
1080
1081 return *this;
1082}
1083
Bill Wendling4b001442013-02-06 01:33:42 +00001084bool AttrBuilder::contains(StringRef A) const {
1085 return TargetDepAttrs.find(A) != TargetDepAttrs.end();
1086}
1087
Bill Wendling50d27842012-10-15 20:35:56 +00001088bool AttrBuilder::hasAttributes() const {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001089 return !Attrs.none() || !TargetDepAttrs.empty();
Bill Wendlingc6daefa2012-10-08 23:27:46 +00001090}
Bill Wendling9ac69f92013-01-04 20:54:35 +00001091
Bill Wendlingd2196752013-01-30 23:07:40 +00001092bool AttrBuilder::hasAttributes(AttributeSet A, uint64_t Index) const {
Bill Wendling211316c2013-04-18 20:17:28 +00001093 unsigned Slot = ~0U;
Bill Wendling9ca01da2013-02-02 00:42:06 +00001094 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
1095 if (A.getSlotIndex(I) == Index) {
Bill Wendling211316c2013-04-18 20:17:28 +00001096 Slot = I;
Bill Wendling9ca01da2013-02-02 00:42:06 +00001097 break;
1098 }
1099
Bill Wendling211316c2013-04-18 20:17:28 +00001100 assert(Slot != ~0U && "Couldn't find the index!");
Bill Wendling9ca01da2013-02-02 00:42:06 +00001101
Bill Wendling211316c2013-04-18 20:17:28 +00001102 for (AttributeSet::iterator I = A.begin(Slot), E = A.end(Slot);
Bill Wendling7cde51d2013-02-12 07:56:49 +00001103 I != E; ++I) {
1104 Attribute Attr = *I;
1105 if (Attr.isEnumAttribute() || Attr.isAlignAttribute()) {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001106 if (Attrs[I->getKindAsEnum()])
Bill Wendling7cde51d2013-02-12 07:56:49 +00001107 return true;
1108 } else {
1109 assert(Attr.isStringAttribute() && "Invalid attribute kind!");
1110 return TargetDepAttrs.find(Attr.getKindAsString())!=TargetDepAttrs.end();
1111 }
1112 }
Bill Wendling9ca01da2013-02-02 00:42:06 +00001113
1114 return false;
Bill Wendling70f39172012-10-09 00:01:21 +00001115}
Bill Wendling9ac69f92013-01-04 20:54:35 +00001116
Bill Wendling50d27842012-10-15 20:35:56 +00001117bool AttrBuilder::hasAlignmentAttr() const {
Bill Wendlingcd330342013-01-04 23:27:34 +00001118 return Alignment != 0;
Bill Wendlingc6daefa2012-10-08 23:27:46 +00001119}
1120
Bill Wendlingd509a662013-01-29 00:34:06 +00001121bool AttrBuilder::operator==(const AttrBuilder &B) {
Benjamin Kramer45e7d532013-02-16 19:13:18 +00001122 if (Attrs != B.Attrs)
1123 return false;
Bill Wendling4b001442013-02-06 01:33:42 +00001124
1125 for (td_const_iterator I = TargetDepAttrs.begin(),
1126 E = TargetDepAttrs.end(); I != E; ++I)
1127 if (B.TargetDepAttrs.find(I->first) == B.TargetDepAttrs.end())
1128 return false;
1129
1130 return Alignment == B.Alignment && StackAlignment == B.StackAlignment;
Bill Wendlingd509a662013-01-29 00:34:06 +00001131}
1132
1133AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) {
Bill Wendling56aeccc2013-02-04 23:32:23 +00001134 // FIXME: Remove this in 4.0.
Bill Wendling60011b82013-01-29 01:43:29 +00001135 if (!Val) return *this;
1136
Bill Wendlingd509a662013-01-29 00:34:06 +00001137 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
1138 I = Attribute::AttrKind(I + 1)) {
1139 if (uint64_t A = (Val & AttributeImpl::getAttrMask(I))) {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001140 Attrs[I] = true;
Bill Wendlingd509a662013-01-29 00:34:06 +00001141
1142 if (I == Attribute::Alignment)
1143 Alignment = 1ULL << ((A >> 16) - 1);
1144 else if (I == Attribute::StackAlignment)
1145 StackAlignment = 1ULL << ((A >> 26)-1);
1146 }
1147 }
1148
1149 return *this;
1150}
1151
Bill Wendling57625a42013-01-25 23:09:36 +00001152//===----------------------------------------------------------------------===//
1153// AttributeFuncs Function Defintions
1154//===----------------------------------------------------------------------===//
1155
Bill Wendlingc79cdff2013-02-01 01:04:27 +00001156/// \brief Which attributes cannot be applied to a type.
Bill Wendlingd2196752013-01-30 23:07:40 +00001157AttributeSet AttributeFuncs::typeIncompatible(Type *Ty, uint64_t Index) {
Bill Wendling57625a42013-01-25 23:09:36 +00001158 AttrBuilder Incompatible;
1159
1160 if (!Ty->isIntegerTy())
1161 // Attribute that only apply to integers.
1162 Incompatible.addAttribute(Attribute::SExt)
1163 .addAttribute(Attribute::ZExt);
1164
1165 if (!Ty->isPointerTy())
1166 // Attribute that only apply to pointers.
1167 Incompatible.addAttribute(Attribute::ByVal)
1168 .addAttribute(Attribute::Nest)
1169 .addAttribute(Attribute::NoAlias)
1170 .addAttribute(Attribute::NoCapture)
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001171 .addAttribute(Attribute::ReadNone)
1172 .addAttribute(Attribute::ReadOnly)
Bill Wendling57625a42013-01-25 23:09:36 +00001173 .addAttribute(Attribute::StructRet);
1174
Bill Wendlingd2196752013-01-30 23:07:40 +00001175 return AttributeSet::get(Ty->getContext(), Index, Incompatible);
Bill Wendling57625a42013-01-25 23:09:36 +00001176}