blob: 24cd6c7c1a28cfad31f0393b0ea918823533ba74 [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"
David Greenef7014732010-01-05 01:29:58 +000021#include "llvm/Support/Debug.h"
Chris Lattner3e13b8c2008-01-02 23:42:30 +000022#include "llvm/Support/ManagedStatic.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000023#include "llvm/Support/Mutex.h"
Benjamin Kramer1a25d732009-08-23 11:37:21 +000024#include "llvm/Support/raw_ostream.h"
Bill Wendlingd2e493b2013-01-24 00:06:56 +000025#include <algorithm>
Chris Lattner3e13b8c2008-01-02 23:42:30 +000026using namespace llvm;
27
Chris Lattner8a923e72008-03-12 17:45:29 +000028//===----------------------------------------------------------------------===//
Bill Wendling7707c5a2013-01-29 00:48:16 +000029// Attribute Construction Methods
Chris Lattner8a923e72008-03-12 17:45:29 +000030//===----------------------------------------------------------------------===//
Chris Lattnerd0e1f102008-01-03 00:10:22 +000031
Bill Wendling3f12ac22013-02-05 22:37:24 +000032Attribute Attribute::get(LLVMContext &Context, Attribute::AttrKind Kind,
33 uint64_t Val) {
Bill Wendling73ea2de2012-10-08 21:47:17 +000034 LLVMContextImpl *pImpl = Context.pImpl;
35 FoldingSetNodeID ID;
Bill Wendling3f12ac22013-02-05 22:37:24 +000036 ID.AddInteger(Kind);
37 if (Val) ID.AddInteger(Val);
38
39 void *InsertPoint;
40 AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
41
42 if (!PA) {
43 // If we didn't find any existing attributes of the same shape then create a
44 // new one and insert it.
Benjamin Kramer741146b2013-07-11 12:13:16 +000045 if (!Val)
46 PA = new EnumAttributeImpl(Kind);
47 else
48 PA = new AlignAttributeImpl(Kind, Val);
Bill Wendling3f12ac22013-02-05 22:37:24 +000049 pImpl->AttrsSet.InsertNode(PA, InsertPoint);
50 }
51
52 // Return the Attribute that we found or created.
53 return Attribute(PA);
54}
55
56Attribute Attribute::get(LLVMContext &Context, StringRef Kind, StringRef Val) {
57 LLVMContextImpl *pImpl = Context.pImpl;
58 FoldingSetNodeID ID;
59 ID.AddString(Kind);
60 if (!Val.empty()) ID.AddString(Val);
Bill Wendling73ea2de2012-10-08 21:47:17 +000061
62 void *InsertPoint;
Bill Wendling4607f4b2012-12-20 01:36:59 +000063 AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
Bill Wendling73ea2de2012-10-08 21:47:17 +000064
65 if (!PA) {
66 // If we didn't find any existing attributes of the same shape then create a
67 // new one and insert it.
Benjamin Kramer741146b2013-07-11 12:13:16 +000068 PA = new StringAttributeImpl(Kind, Val);
Bill Wendling73ea2de2012-10-08 21:47:17 +000069 pImpl->AttrsSet.InsertNode(PA, InsertPoint);
70 }
71
Bill Wendlingb9c5b1a2013-02-05 08:09:32 +000072 // Return the Attribute that we found or created.
Bill Wendling3d7b0b82012-12-19 07:18:57 +000073 return Attribute(PA);
Bill Wendling73ea2de2012-10-08 21:47:17 +000074}
75
Bill Wendling4bbe9db2013-01-27 22:43:04 +000076Attribute Attribute::getWithAlignment(LLVMContext &Context, uint64_t Align) {
Bill Wendling1c7cc8a2013-01-31 23:16:25 +000077 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
78 assert(Align <= 0x40000000 && "Alignment too large.");
Bill Wendling3f12ac22013-02-05 22:37:24 +000079 return get(Context, Alignment, Align);
Bill Wendling4bbe9db2013-01-27 22:43:04 +000080}
81
82Attribute Attribute::getWithStackAlignment(LLVMContext &Context,
83 uint64_t Align) {
Bill Wendling1c7cc8a2013-01-31 23:16:25 +000084 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
85 assert(Align <= 0x100 && "Alignment too large.");
Bill Wendling3f12ac22013-02-05 22:37:24 +000086 return get(Context, StackAlignment, Align);
Bill Wendling4bbe9db2013-01-27 22:43:04 +000087}
88
Bill Wendling7707c5a2013-01-29 00:48:16 +000089//===----------------------------------------------------------------------===//
90// Attribute Accessor Methods
91//===----------------------------------------------------------------------===//
92
Bill Wendling3f12ac22013-02-05 22:37:24 +000093bool Attribute::isEnumAttribute() const {
94 return pImpl && pImpl->isEnumAttribute();
95}
96
97bool Attribute::isAlignAttribute() const {
98 return pImpl && pImpl->isAlignAttribute();
99}
100
101bool Attribute::isStringAttribute() const {
102 return pImpl && pImpl->isStringAttribute();
103}
104
105Attribute::AttrKind Attribute::getKindAsEnum() const {
Bill Wendling440e9d82013-07-25 00:34:29 +0000106 if (!pImpl) return None;
Bill Wendling3f12ac22013-02-05 22:37:24 +0000107 assert((isEnumAttribute() || isAlignAttribute()) &&
108 "Invalid attribute type to get the kind as an enum!");
109 return pImpl ? pImpl->getKindAsEnum() : None;
110}
111
112uint64_t Attribute::getValueAsInt() const {
Bill Wendling440e9d82013-07-25 00:34:29 +0000113 if (!pImpl) return 0;
Bill Wendling3f12ac22013-02-05 22:37:24 +0000114 assert(isAlignAttribute() &&
115 "Expected the attribute to be an alignment attribute!");
116 return pImpl ? pImpl->getValueAsInt() : 0;
117}
118
119StringRef Attribute::getKindAsString() const {
Bill Wendling440e9d82013-07-25 00:34:29 +0000120 if (!pImpl) return StringRef();
Bill Wendling3f12ac22013-02-05 22:37:24 +0000121 assert(isStringAttribute() &&
122 "Invalid attribute type to get the kind as a string!");
123 return pImpl ? pImpl->getKindAsString() : StringRef();
124}
125
126StringRef Attribute::getValueAsString() const {
Bill Wendling440e9d82013-07-25 00:34:29 +0000127 if (!pImpl) return StringRef();
Bill Wendling3f12ac22013-02-05 22:37:24 +0000128 assert(isStringAttribute() &&
129 "Invalid attribute type to get the value as a string!");
130 return pImpl ? pImpl->getValueAsString() : StringRef();
131}
132
Bill Wendlingae89a0f2013-02-05 23:48:36 +0000133bool Attribute::hasAttribute(AttrKind Kind) const {
134 return (pImpl && pImpl->hasAttribute(Kind)) || (!pImpl && Kind == None);
135}
136
137bool Attribute::hasAttribute(StringRef Kind) const {
138 if (!isStringAttribute()) return false;
139 return pImpl && pImpl->hasAttribute(Kind);
Bill Wendling03eefb32013-01-29 20:45:34 +0000140}
141
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000142/// This returns the alignment field of an attribute as a byte alignment value.
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000143unsigned Attribute::getAlignment() const {
Bill Wendlingc79cdff2013-02-01 01:04:27 +0000144 assert(hasAttribute(Attribute::Alignment) &&
145 "Trying to get alignment from non-alignment attribute!");
Bill Wendling3f12ac22013-02-05 22:37:24 +0000146 return pImpl->getValueAsInt();
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000147}
148
149/// This returns the stack alignment field of an attribute as a byte alignment
150/// value.
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000151unsigned Attribute::getStackAlignment() const {
Bill Wendlingc79cdff2013-02-01 01:04:27 +0000152 assert(hasAttribute(Attribute::StackAlignment) &&
153 "Trying to get alignment from non-alignment attribute!");
Bill Wendling3f12ac22013-02-05 22:37:24 +0000154 return pImpl->getValueAsInt();
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000155}
156
Bill Wendling829b4782013-02-11 08:43:33 +0000157std::string Attribute::getAsString(bool InAttrGrp) const {
Bill Wendling9c2eba92013-01-31 20:59:05 +0000158 if (!pImpl) return "";
159
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000160 if (hasAttribute(Attribute::SanitizeAddress))
161 return "sanitize_address";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000162 if (hasAttribute(Attribute::AlwaysInline))
163 return "alwaysinline";
Michael Gottesman41748d72013-06-27 00:25:01 +0000164 if (hasAttribute(Attribute::Builtin))
165 return "builtin";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000166 if (hasAttribute(Attribute::ByVal))
167 return "byval";
Reid Klecknera534a382013-12-19 02:14:12 +0000168 if (hasAttribute(Attribute::InAlloca))
169 return "inalloca";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000170 if (hasAttribute(Attribute::InlineHint))
171 return "inlinehint";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000172 if (hasAttribute(Attribute::InReg))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000173 return "inreg";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000174 if (hasAttribute(Attribute::MinSize))
175 return "minsize";
176 if (hasAttribute(Attribute::Naked))
177 return "naked";
178 if (hasAttribute(Attribute::Nest))
179 return "nest";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000180 if (hasAttribute(Attribute::NoAlias))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000181 return "noalias";
Bill Wendling09bd1f72013-02-22 00:12:35 +0000182 if (hasAttribute(Attribute::NoBuiltin))
183 return "nobuiltin";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000184 if (hasAttribute(Attribute::NoCapture))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000185 return "nocapture";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000186 if (hasAttribute(Attribute::NoDuplicate))
187 return "noduplicate";
188 if (hasAttribute(Attribute::NoImplicitFloat))
189 return "noimplicitfloat";
190 if (hasAttribute(Attribute::NoInline))
191 return "noinline";
192 if (hasAttribute(Attribute::NonLazyBind))
193 return "nonlazybind";
194 if (hasAttribute(Attribute::NoRedZone))
195 return "noredzone";
196 if (hasAttribute(Attribute::NoReturn))
197 return "noreturn";
198 if (hasAttribute(Attribute::NoUnwind))
199 return "nounwind";
Andrea Di Biagio377496b2013-08-23 11:53:55 +0000200 if (hasAttribute(Attribute::OptimizeNone))
201 return "optnone";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000202 if (hasAttribute(Attribute::OptimizeForSize))
203 return "optsize";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000204 if (hasAttribute(Attribute::ReadNone))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000205 return "readnone";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000206 if (hasAttribute(Attribute::ReadOnly))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000207 return "readonly";
Stephen Linb8bd2322013-04-20 05:14:40 +0000208 if (hasAttribute(Attribute::Returned))
209 return "returned";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000210 if (hasAttribute(Attribute::ReturnsTwice))
211 return "returns_twice";
212 if (hasAttribute(Attribute::SExt))
213 return "signext";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000214 if (hasAttribute(Attribute::StackProtect))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000215 return "ssp";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000216 if (hasAttribute(Attribute::StackProtectReq))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000217 return "sspreq";
Bill Wendlingd154e2832013-01-23 06:41:41 +0000218 if (hasAttribute(Attribute::StackProtectStrong))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000219 return "sspstrong";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000220 if (hasAttribute(Attribute::StructRet))
221 return "sret";
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000222 if (hasAttribute(Attribute::SanitizeThread))
223 return "sanitize_thread";
224 if (hasAttribute(Attribute::SanitizeMemory))
225 return "sanitize_memory";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000226 if (hasAttribute(Attribute::UWTable))
227 return "uwtable";
228 if (hasAttribute(Attribute::ZExt))
229 return "zeroext";
Diego Novilloc6399532013-05-24 12:26:52 +0000230 if (hasAttribute(Attribute::Cold))
231 return "cold";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000232
233 // FIXME: These should be output like this:
234 //
235 // align=4
236 // alignstack=8
237 //
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000238 if (hasAttribute(Attribute::Alignment)) {
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000239 std::string Result;
Bill Wendling829b4782013-02-11 08:43:33 +0000240 Result += "align";
241 Result += (InAttrGrp) ? "=" : " ";
Bill Wendling3f12ac22013-02-05 22:37:24 +0000242 Result += utostr(getValueAsInt());
243 return Result;
244 }
Bill Wendling829b4782013-02-11 08:43:33 +0000245
Bill Wendling3f12ac22013-02-05 22:37:24 +0000246 if (hasAttribute(Attribute::StackAlignment)) {
247 std::string Result;
Bill Wendling829b4782013-02-11 08:43:33 +0000248 Result += "alignstack";
249 if (InAttrGrp) {
250 Result += "=";
251 Result += utostr(getValueAsInt());
252 } else {
253 Result += "(";
254 Result += utostr(getValueAsInt());
255 Result += ")";
256 }
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000257 return Result;
Dale Johannesen11a555e2008-02-19 23:51:49 +0000258 }
Bill Wendling9c2eba92013-01-31 20:59:05 +0000259
260 // Convert target-dependent attributes to strings of the form:
261 //
262 // "kind"
263 // "kind" = "value"
Bill Wendling9c2eba92013-01-31 20:59:05 +0000264 //
Bill Wendling3f12ac22013-02-05 22:37:24 +0000265 if (isStringAttribute()) {
Bill Wendling9c2eba92013-01-31 20:59:05 +0000266 std::string Result;
Bill Wendling3f12ac22013-02-05 22:37:24 +0000267 Result += '\"' + getKindAsString().str() + '"';
Bill Wendling9c2eba92013-01-31 20:59:05 +0000268
Bill Wendling3f12ac22013-02-05 22:37:24 +0000269 StringRef Val = pImpl->getValueAsString();
270 if (Val.empty()) return Result;
Bill Wendling7a33f772013-02-01 22:32:30 +0000271
Bill Wendling829b4782013-02-11 08:43:33 +0000272 Result += "=\"" + Val.str() + '"';
Bill Wendlingc79cdff2013-02-01 01:04:27 +0000273 return Result;
Bill Wendling9c2eba92013-01-31 20:59:05 +0000274 }
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000275
276 llvm_unreachable("Unknown attribute");
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000277}
278
Bill Wendlingd509a662013-01-29 00:34:06 +0000279bool Attribute::operator<(Attribute A) const {
280 if (!pImpl && !A.pImpl) return false;
281 if (!pImpl) return true;
282 if (!A.pImpl) return false;
283 return *pImpl < *A.pImpl;
284}
285
Bill Wendlingd509a662013-01-29 00:34:06 +0000286//===----------------------------------------------------------------------===//
287// AttributeImpl Definition
288//===----------------------------------------------------------------------===//
289
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +0000290// Pin the vtabels to this file.
Alexey Samsonov49109a22013-11-18 09:31:53 +0000291AttributeImpl::~AttributeImpl() {}
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +0000292void EnumAttributeImpl::anchor() {}
293void AlignAttributeImpl::anchor() {}
294void StringAttributeImpl::anchor() {}
Alexey Samsonov49109a22013-11-18 09:31:53 +0000295
Bill Wendlingd509a662013-01-29 00:34:06 +0000296bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
Bill Wendling3f12ac22013-02-05 22:37:24 +0000297 if (isStringAttribute()) return false;
298 return getKindAsEnum() == A;
Bill Wendlingd509a662013-01-29 00:34:06 +0000299}
300
Bill Wendling3f12ac22013-02-05 22:37:24 +0000301bool AttributeImpl::hasAttribute(StringRef Kind) const {
302 if (!isStringAttribute()) return false;
303 return getKindAsString() == Kind;
Bill Wendlingd509a662013-01-29 00:34:06 +0000304}
305
Bill Wendling3f12ac22013-02-05 22:37:24 +0000306Attribute::AttrKind AttributeImpl::getKindAsEnum() const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000307 assert(isEnumAttribute() || isAlignAttribute());
308 return static_cast<const EnumAttributeImpl *>(this)->getEnumKind();
Bill Wendlingd509a662013-01-29 00:34:06 +0000309}
310
Bill Wendling3f12ac22013-02-05 22:37:24 +0000311uint64_t AttributeImpl::getValueAsInt() const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000312 assert(isAlignAttribute());
313 return static_cast<const AlignAttributeImpl *>(this)->getAlignment();
Bill Wendlingd509a662013-01-29 00:34:06 +0000314}
315
Bill Wendling3f12ac22013-02-05 22:37:24 +0000316StringRef AttributeImpl::getKindAsString() const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000317 assert(isStringAttribute());
318 return static_cast<const StringAttributeImpl *>(this)->getStringKind();
Bill Wendlingd509a662013-01-29 00:34:06 +0000319}
320
Bill Wendling3f12ac22013-02-05 22:37:24 +0000321StringRef AttributeImpl::getValueAsString() const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000322 assert(isStringAttribute());
323 return static_cast<const StringAttributeImpl *>(this)->getStringValue();
Bill Wendlingd509a662013-01-29 00:34:06 +0000324}
325
326bool AttributeImpl::operator<(const AttributeImpl &AI) const {
Bill Wendlingc79cdff2013-02-01 01:04:27 +0000327 // This sorts the attributes with Attribute::AttrKinds coming first (sorted
328 // relative to their enum value) and then strings.
Bill Wendling26b95752013-02-15 05:25:26 +0000329 if (isEnumAttribute()) {
330 if (AI.isEnumAttribute()) return getKindAsEnum() < AI.getKindAsEnum();
331 if (AI.isAlignAttribute()) return true;
332 if (AI.isStringAttribute()) return true;
333 }
Bill Wendlingc79cdff2013-02-01 01:04:27 +0000334
Bill Wendling3f12ac22013-02-05 22:37:24 +0000335 if (isAlignAttribute()) {
Bill Wendling26b95752013-02-15 05:25:26 +0000336 if (AI.isEnumAttribute()) return false;
337 if (AI.isAlignAttribute()) return getValueAsInt() < AI.getValueAsInt();
338 if (AI.isStringAttribute()) return true;
Bill Wendling3f12ac22013-02-05 22:37:24 +0000339 }
Bill Wendlingd509a662013-01-29 00:34:06 +0000340
Bill Wendling26b95752013-02-15 05:25:26 +0000341 if (AI.isEnumAttribute()) return false;
342 if (AI.isAlignAttribute()) return false;
343 if (getKindAsString() == AI.getKindAsString())
344 return getValueAsString() < AI.getValueAsString();
345 return getKindAsString() < AI.getKindAsString();
Bill Wendlingd509a662013-01-29 00:34:06 +0000346}
347
Bill Wendlingd509a662013-01-29 00:34:06 +0000348uint64_t AttributeImpl::getAttrMask(Attribute::AttrKind Val) {
349 // FIXME: Remove this.
350 switch (Val) {
351 case Attribute::EndAttrKinds:
Bill Wendlingd509a662013-01-29 00:34:06 +0000352 llvm_unreachable("Synthetic enumerators which should never get here");
353
354 case Attribute::None: return 0;
355 case Attribute::ZExt: return 1 << 0;
356 case Attribute::SExt: return 1 << 1;
357 case Attribute::NoReturn: return 1 << 2;
358 case Attribute::InReg: return 1 << 3;
359 case Attribute::StructRet: return 1 << 4;
360 case Attribute::NoUnwind: return 1 << 5;
361 case Attribute::NoAlias: return 1 << 6;
362 case Attribute::ByVal: return 1 << 7;
363 case Attribute::Nest: return 1 << 8;
364 case Attribute::ReadNone: return 1 << 9;
365 case Attribute::ReadOnly: return 1 << 10;
366 case Attribute::NoInline: return 1 << 11;
367 case Attribute::AlwaysInline: return 1 << 12;
368 case Attribute::OptimizeForSize: return 1 << 13;
369 case Attribute::StackProtect: return 1 << 14;
370 case Attribute::StackProtectReq: return 1 << 15;
371 case Attribute::Alignment: return 31 << 16;
372 case Attribute::NoCapture: return 1 << 21;
373 case Attribute::NoRedZone: return 1 << 22;
374 case Attribute::NoImplicitFloat: return 1 << 23;
375 case Attribute::Naked: return 1 << 24;
376 case Attribute::InlineHint: return 1 << 25;
377 case Attribute::StackAlignment: return 7 << 26;
378 case Attribute::ReturnsTwice: return 1 << 29;
379 case Attribute::UWTable: return 1 << 30;
380 case Attribute::NonLazyBind: return 1U << 31;
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000381 case Attribute::SanitizeAddress: return 1ULL << 32;
Bill Wendlingd509a662013-01-29 00:34:06 +0000382 case Attribute::MinSize: return 1ULL << 33;
383 case Attribute::NoDuplicate: return 1ULL << 34;
384 case Attribute::StackProtectStrong: return 1ULL << 35;
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000385 case Attribute::SanitizeThread: return 1ULL << 36;
386 case Attribute::SanitizeMemory: return 1ULL << 37;
Bill Wendling7bfdfe72013-02-22 00:40:12 +0000387 case Attribute::NoBuiltin: return 1ULL << 38;
Stephen Linb8bd2322013-04-20 05:14:40 +0000388 case Attribute::Returned: return 1ULL << 39;
Diego Novilloc6399532013-05-24 12:26:52 +0000389 case Attribute::Cold: return 1ULL << 40;
Michael Gottesman41748d72013-06-27 00:25:01 +0000390 case Attribute::Builtin: return 1ULL << 41;
Andrea Di Biagio377496b2013-08-23 11:53:55 +0000391 case Attribute::OptimizeNone: return 1ULL << 42;
Reid Klecknera534a382013-12-19 02:14:12 +0000392 case Attribute::InAlloca: return 1ULL << 43;
Bill Wendling25342e12013-02-22 00:50:09 +0000393 }
394 llvm_unreachable("Unsupported attribute type");
Bill Wendlingd509a662013-01-29 00:34:06 +0000395}
396
397//===----------------------------------------------------------------------===//
398// AttributeSetNode Definition
399//===----------------------------------------------------------------------===//
400
401AttributeSetNode *AttributeSetNode::get(LLVMContext &C,
402 ArrayRef<Attribute> Attrs) {
403 if (Attrs.empty())
404 return 0;
405
406 // Otherwise, build a key to look up the existing attributes.
407 LLVMContextImpl *pImpl = C.pImpl;
408 FoldingSetNodeID ID;
409
410 SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end());
Bill Wendling82c2ee62013-02-13 09:26:26 +0000411 array_pod_sort(SortedAttrs.begin(), SortedAttrs.end());
Bill Wendlingd509a662013-01-29 00:34:06 +0000412
413 for (SmallVectorImpl<Attribute>::iterator I = SortedAttrs.begin(),
414 E = SortedAttrs.end(); I != E; ++I)
415 I->Profile(ID);
416
417 void *InsertPoint;
418 AttributeSetNode *PA =
419 pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint);
420
421 // If we didn't find any existing attributes of the same shape then create a
422 // new one and insert it.
423 if (!PA) {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000424 // Coallocate entries after the AttributeSetNode itself.
425 void *Mem = ::operator new(sizeof(AttributeSetNode) +
426 sizeof(Attribute) * SortedAttrs.size());
427 PA = new (Mem) AttributeSetNode(SortedAttrs);
Bill Wendlingd509a662013-01-29 00:34:06 +0000428 pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint);
429 }
430
431 // Return the AttributesListNode that we found or created.
432 return PA;
433}
434
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000435bool AttributeSetNode::hasAttribute(Attribute::AttrKind Kind) const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000436 for (iterator I = begin(), E = end(); I != E; ++I)
NAKAMURA Takumib0944392013-01-31 03:47:28 +0000437 if (I->hasAttribute(Kind))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000438 return true;
439 return false;
440}
441
Bill Wendlingbce7b972013-02-13 08:42:21 +0000442bool AttributeSetNode::hasAttribute(StringRef Kind) const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000443 for (iterator I = begin(), E = end(); I != E; ++I)
Bill Wendlingbce7b972013-02-13 08:42:21 +0000444 if (I->hasAttribute(Kind))
445 return true;
446 return false;
447}
448
449Attribute AttributeSetNode::getAttribute(Attribute::AttrKind Kind) const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000450 for (iterator I = begin(), E = end(); I != E; ++I)
Bill Wendlingbce7b972013-02-13 08:42:21 +0000451 if (I->hasAttribute(Kind))
452 return *I;
453 return Attribute();
454}
455
456Attribute AttributeSetNode::getAttribute(StringRef Kind) const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000457 for (iterator I = begin(), E = end(); I != E; ++I)
Bill Wendlingbce7b972013-02-13 08:42:21 +0000458 if (I->hasAttribute(Kind))
459 return *I;
460 return Attribute();
461}
462
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000463unsigned AttributeSetNode::getAlignment() const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000464 for (iterator I = begin(), E = end(); I != E; ++I)
NAKAMURA Takumib0944392013-01-31 03:47:28 +0000465 if (I->hasAttribute(Attribute::Alignment))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000466 return I->getAlignment();
467 return 0;
468}
469
470unsigned AttributeSetNode::getStackAlignment() const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000471 for (iterator I = begin(), E = end(); I != E; ++I)
NAKAMURA Takumib0944392013-01-31 03:47:28 +0000472 if (I->hasAttribute(Attribute::StackAlignment))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000473 return I->getStackAlignment();
474 return 0;
475}
476
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +0000477std::string AttributeSetNode::getAsString(bool InAttrGrp) const {
Benjamin Kramer0baf8f42013-04-19 11:43:21 +0000478 std::string Str;
Benjamin Kramer741146b2013-07-11 12:13:16 +0000479 for (iterator I = begin(), E = end(); I != E; ++I) {
480 if (I != begin())
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +0000481 Str += ' ';
482 Str += I->getAsString(InAttrGrp);
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000483 }
484 return Str;
485}
486
Bill Wendlingd509a662013-01-29 00:34:06 +0000487//===----------------------------------------------------------------------===//
488// AttributeSetImpl Definition
489//===----------------------------------------------------------------------===//
490
Rafael Espindoladd275302013-04-30 16:53:38 +0000491uint64_t AttributeSetImpl::Raw(unsigned Index) const {
Bill Wendlingd509a662013-01-29 00:34:06 +0000492 for (unsigned I = 0, E = getNumAttributes(); I != E; ++I) {
493 if (getSlotIndex(I) != Index) continue;
Benjamin Kramer741146b2013-07-11 12:13:16 +0000494 const AttributeSetNode *ASN = getSlotNode(I);
Bill Wendling91226182013-02-02 00:52:44 +0000495 uint64_t Mask = 0;
Bill Wendlingd509a662013-01-29 00:34:06 +0000496
Benjamin Kramer741146b2013-07-11 12:13:16 +0000497 for (AttributeSetNode::iterator II = ASN->begin(),
Bill Wendling91226182013-02-02 00:52:44 +0000498 IE = ASN->end(); II != IE; ++II) {
499 Attribute Attr = *II;
Bill Wendlingd746e402013-02-10 23:18:05 +0000500
501 // This cannot handle string attributes.
502 if (Attr.isStringAttribute()) continue;
503
Bill Wendling3f12ac22013-02-05 22:37:24 +0000504 Attribute::AttrKind Kind = Attr.getKindAsEnum();
Bill Wendling91226182013-02-02 00:52:44 +0000505
Bill Wendling3f12ac22013-02-05 22:37:24 +0000506 if (Kind == Attribute::Alignment)
Bill Wendling91226182013-02-02 00:52:44 +0000507 Mask |= (Log2_32(ASN->getAlignment()) + 1) << 16;
Bill Wendling3f12ac22013-02-05 22:37:24 +0000508 else if (Kind == Attribute::StackAlignment)
Bill Wendling91226182013-02-02 00:52:44 +0000509 Mask |= (Log2_32(ASN->getStackAlignment()) + 1) << 26;
510 else
Bill Wendling3f12ac22013-02-05 22:37:24 +0000511 Mask |= AttributeImpl::getAttrMask(Kind);
Bill Wendling91226182013-02-02 00:52:44 +0000512 }
513
514 return Mask;
Bill Wendlingd509a662013-01-29 00:34:06 +0000515 }
516
517 return 0;
518}
519
Peter Collingbourneabca2ec2013-08-02 22:34:30 +0000520void AttributeSetImpl::dump() const {
521 AttributeSet(const_cast<AttributeSetImpl *>(this)).dump();
522}
523
Bill Wendlingd509a662013-01-29 00:34:06 +0000524//===----------------------------------------------------------------------===//
525// AttributeSet Construction and Mutation Methods
526//===----------------------------------------------------------------------===//
527
Bill Wendling60011b82013-01-29 01:43:29 +0000528AttributeSet
529AttributeSet::getImpl(LLVMContext &C,
530 ArrayRef<std::pair<unsigned, AttributeSetNode*> > Attrs) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000531 LLVMContextImpl *pImpl = C.pImpl;
532 FoldingSetNodeID ID;
533 AttributeSetImpl::Profile(ID, Attrs);
534
535 void *InsertPoint;
536 AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
537
538 // If we didn't find any existing attributes of the same shape then
539 // create a new one and insert it.
540 if (!PA) {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000541 // Coallocate entries after the AttributeSetImpl itself.
542 void *Mem = ::operator new(sizeof(AttributeSetImpl) +
543 sizeof(std::pair<unsigned, AttributeSetNode *>) *
544 Attrs.size());
545 PA = new (Mem) AttributeSetImpl(C, Attrs);
Bill Wendlingd509a662013-01-29 00:34:06 +0000546 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
547 }
548
549 // Return the AttributesList that we found or created.
550 return AttributeSet(PA);
551}
552
553AttributeSet AttributeSet::get(LLVMContext &C,
554 ArrayRef<std::pair<unsigned, Attribute> > Attrs){
555 // If there are no attributes then return a null AttributesList pointer.
556 if (Attrs.empty())
557 return AttributeSet();
558
559#ifndef NDEBUG
560 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
561 assert((!i || Attrs[i-1].first <= Attrs[i].first) &&
562 "Misordered Attributes list!");
Bill Wendling3f12ac22013-02-05 22:37:24 +0000563 assert(!Attrs[i].second.hasAttribute(Attribute::None) &&
Bill Wendlingd509a662013-01-29 00:34:06 +0000564 "Pointless attribute!");
565 }
566#endif
567
568 // Create a vector if (unsigned, AttributeSetNode*) pairs from the attributes
569 // list.
570 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrPairVec;
571 for (ArrayRef<std::pair<unsigned, Attribute> >::iterator I = Attrs.begin(),
572 E = Attrs.end(); I != E; ) {
573 unsigned Index = I->first;
574 SmallVector<Attribute, 4> AttrVec;
NAKAMURA Takumif05d2f22013-01-29 15:18:16 +0000575 while (I != E && I->first == Index) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000576 AttrVec.push_back(I->second);
577 ++I;
578 }
579
580 AttrPairVec.push_back(std::make_pair(Index,
581 AttributeSetNode::get(C, AttrVec)));
582 }
583
584 return getImpl(C, AttrPairVec);
585}
586
587AttributeSet AttributeSet::get(LLVMContext &C,
588 ArrayRef<std::pair<unsigned,
589 AttributeSetNode*> > Attrs) {
590 // If there are no attributes then return a null AttributesList pointer.
591 if (Attrs.empty())
592 return AttributeSet();
593
594 return getImpl(C, Attrs);
595}
596
Bill Wendling211316c2013-04-18 20:17:28 +0000597AttributeSet AttributeSet::get(LLVMContext &C, unsigned Index, AttrBuilder &B) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000598 if (!B.hasAttributes())
599 return AttributeSet();
Bill Wendlingf7134812013-01-29 01:02:03 +0000600
Bill Wendlingae89a0f2013-02-05 23:48:36 +0000601 // Add target-independent attributes.
Bill Wendlingf7134812013-01-29 01:02:03 +0000602 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
Benjamin Kramer45e7d532013-02-16 19:13:18 +0000603 for (Attribute::AttrKind Kind = Attribute::None;
Benjamin Kramer6f37dac2013-02-16 19:22:28 +0000604 Kind != Attribute::EndAttrKinds; Kind = Attribute::AttrKind(Kind + 1)) {
Benjamin Kramer45e7d532013-02-16 19:13:18 +0000605 if (!B.contains(Kind))
606 continue;
607
Bill Wendlingf7134812013-01-29 01:02:03 +0000608 if (Kind == Attribute::Alignment)
Bill Wendling211316c2013-04-18 20:17:28 +0000609 Attrs.push_back(std::make_pair(Index, Attribute::
Bill Wendlingf7134812013-01-29 01:02:03 +0000610 getWithAlignment(C, B.getAlignment())));
611 else if (Kind == Attribute::StackAlignment)
Bill Wendling211316c2013-04-18 20:17:28 +0000612 Attrs.push_back(std::make_pair(Index, Attribute::
Bill Wendlingf7134812013-01-29 01:02:03 +0000613 getWithStackAlignment(C, B.getStackAlignment())));
614 else
Bill Wendling211316c2013-04-18 20:17:28 +0000615 Attrs.push_back(std::make_pair(Index, Attribute::get(C, Kind)));
Bill Wendlingf7134812013-01-29 01:02:03 +0000616 }
617
Bill Wendlingae89a0f2013-02-05 23:48:36 +0000618 // Add target-dependent (string) attributes.
619 for (AttrBuilder::td_iterator I = B.td_begin(), E = B.td_end();
620 I != E; ++I)
Bill Wendling211316c2013-04-18 20:17:28 +0000621 Attrs.push_back(std::make_pair(Index, Attribute::get(C, I->first,I->second)));
Bill Wendlingae89a0f2013-02-05 23:48:36 +0000622
Bill Wendlingf7134812013-01-29 01:02:03 +0000623 return get(C, Attrs);
Bill Wendlingd509a662013-01-29 00:34:06 +0000624}
625
Bill Wendling211316c2013-04-18 20:17:28 +0000626AttributeSet AttributeSet::get(LLVMContext &C, unsigned Index,
Bill Wendlingd509a662013-01-29 00:34:06 +0000627 ArrayRef<Attribute::AttrKind> Kind) {
628 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
629 for (ArrayRef<Attribute::AttrKind>::iterator I = Kind.begin(),
630 E = Kind.end(); I != E; ++I)
Bill Wendling211316c2013-04-18 20:17:28 +0000631 Attrs.push_back(std::make_pair(Index, Attribute::get(C, *I)));
Bill Wendlingd509a662013-01-29 00:34:06 +0000632 return get(C, Attrs);
633}
634
635AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<AttributeSet> Attrs) {
636 if (Attrs.empty()) return AttributeSet();
Peter Collingbournebd6c7452013-08-02 22:29:40 +0000637 if (Attrs.size() == 1) return Attrs[0];
Bill Wendlingd509a662013-01-29 00:34:06 +0000638
639 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrNodeVec;
Peter Collingbournebd6c7452013-08-02 22:29:40 +0000640 AttributeSetImpl *A0 = Attrs[0].pImpl;
641 if (A0)
642 AttrNodeVec.append(A0->getNode(0), A0->getNode(A0->getNumAttributes()));
643 // Copy all attributes from Attrs into AttrNodeVec while keeping AttrNodeVec
644 // ordered by index. Because we know that each list in Attrs is ordered by
645 // index we only need to merge each successive list in rather than doing a
646 // full sort.
647 for (unsigned I = 1, E = Attrs.size(); I != E; ++I) {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000648 AttributeSetImpl *AS = Attrs[I].pImpl;
649 if (!AS) continue;
Peter Collingbournebd6c7452013-08-02 22:29:40 +0000650 SmallVector<std::pair<unsigned, AttributeSetNode *>, 8>::iterator
651 ANVI = AttrNodeVec.begin(), ANVE;
652 for (const AttributeSetImpl::IndexAttrPair
653 *AI = AS->getNode(0),
654 *AE = AS->getNode(AS->getNumAttributes());
655 AI != AE; ++AI) {
656 ANVE = AttrNodeVec.end();
657 while (ANVI != ANVE && ANVI->first <= AI->first)
658 ++ANVI;
659 ANVI = AttrNodeVec.insert(ANVI, *AI) + 1;
660 }
Bill Wendlingd509a662013-01-29 00:34:06 +0000661 }
662
663 return getImpl(C, AttrNodeVec);
664}
665
Bill Wendling211316c2013-04-18 20:17:28 +0000666AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
Bill Wendlingd509a662013-01-29 00:34:06 +0000667 Attribute::AttrKind Attr) const {
Bill Wendling211316c2013-04-18 20:17:28 +0000668 if (hasAttribute(Index, Attr)) return *this;
669 return addAttributes(C, Index, AttributeSet::get(C, Index, Attr));
Bill Wendlingd509a662013-01-29 00:34:06 +0000670}
671
Bill Wendling211316c2013-04-18 20:17:28 +0000672AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
Reed Kotler795c7b42013-03-13 20:20:08 +0000673 StringRef Kind) const {
674 llvm::AttrBuilder B;
675 B.addAttribute(Kind);
Bill Wendling211316c2013-04-18 20:17:28 +0000676 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
Reed Kotler795c7b42013-03-13 20:20:08 +0000677}
678
Bill Wendling3b2f6102013-07-25 18:34:24 +0000679AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
680 StringRef Kind, StringRef Value) const {
681 llvm::AttrBuilder B;
682 B.addAttribute(Kind, Value);
683 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
684}
685
Bill Wendling211316c2013-04-18 20:17:28 +0000686AttributeSet AttributeSet::addAttributes(LLVMContext &C, unsigned Index,
Bill Wendlingd509a662013-01-29 00:34:06 +0000687 AttributeSet Attrs) const {
688 if (!pImpl) return Attrs;
689 if (!Attrs.pImpl) return *this;
690
691#ifndef NDEBUG
692 // FIXME it is not obvious how this should work for alignment. For now, say
693 // we can't change a known alignment.
Bill Wendling211316c2013-04-18 20:17:28 +0000694 unsigned OldAlign = getParamAlignment(Index);
695 unsigned NewAlign = Attrs.getParamAlignment(Index);
Bill Wendlingd509a662013-01-29 00:34:06 +0000696 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
697 "Attempt to change alignment!");
698#endif
699
700 // Add the attribute slots before the one we're trying to add.
701 SmallVector<AttributeSet, 4> AttrSet;
702 uint64_t NumAttrs = pImpl->getNumAttributes();
703 AttributeSet AS;
704 uint64_t LastIndex = 0;
705 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
Bill Wendling211316c2013-04-18 20:17:28 +0000706 if (getSlotIndex(I) >= Index) {
707 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
Bill Wendlingd509a662013-01-29 00:34:06 +0000708 break;
709 }
710 LastIndex = I + 1;
711 AttrSet.push_back(getSlotAttributes(I));
712 }
713
714 // Now add the attribute into the correct slot. There may already be an
715 // AttributeSet there.
Bill Wendling211316c2013-04-18 20:17:28 +0000716 AttrBuilder B(AS, Index);
Bill Wendlingd509a662013-01-29 00:34:06 +0000717
718 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
Bill Wendling211316c2013-04-18 20:17:28 +0000719 if (Attrs.getSlotIndex(I) == Index) {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000720 for (AttributeSetImpl::iterator II = Attrs.pImpl->begin(I),
Bill Wendlingd509a662013-01-29 00:34:06 +0000721 IE = Attrs.pImpl->end(I); II != IE; ++II)
Bill Wendling23804da2013-01-31 23:38:01 +0000722 B.addAttribute(*II);
Bill Wendlingd509a662013-01-29 00:34:06 +0000723 break;
724 }
725
Bill Wendling211316c2013-04-18 20:17:28 +0000726 AttrSet.push_back(AttributeSet::get(C, Index, B));
Bill Wendlingd509a662013-01-29 00:34:06 +0000727
728 // Add the remaining attribute slots.
729 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
730 AttrSet.push_back(getSlotAttributes(I));
731
732 return get(C, AttrSet);
733}
734
Bill Wendling211316c2013-04-18 20:17:28 +0000735AttributeSet AttributeSet::removeAttribute(LLVMContext &C, unsigned Index,
Bill Wendlingd509a662013-01-29 00:34:06 +0000736 Attribute::AttrKind Attr) const {
Bill Wendling211316c2013-04-18 20:17:28 +0000737 if (!hasAttribute(Index, Attr)) return *this;
738 return removeAttributes(C, Index, AttributeSet::get(C, Index, Attr));
Bill Wendlingd509a662013-01-29 00:34:06 +0000739}
740
Bill Wendling211316c2013-04-18 20:17:28 +0000741AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Index,
Bill Wendlingd509a662013-01-29 00:34:06 +0000742 AttributeSet Attrs) const {
743 if (!pImpl) return AttributeSet();
744 if (!Attrs.pImpl) return *this;
745
746#ifndef NDEBUG
747 // FIXME it is not obvious how this should work for alignment.
748 // For now, say we can't pass in alignment, which no current use does.
Bill Wendling211316c2013-04-18 20:17:28 +0000749 assert(!Attrs.hasAttribute(Index, Attribute::Alignment) &&
Bill Wendlingd509a662013-01-29 00:34:06 +0000750 "Attempt to change alignment!");
751#endif
752
753 // Add the attribute slots before the one we're trying to add.
754 SmallVector<AttributeSet, 4> AttrSet;
755 uint64_t NumAttrs = pImpl->getNumAttributes();
756 AttributeSet AS;
757 uint64_t LastIndex = 0;
758 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
Bill Wendling211316c2013-04-18 20:17:28 +0000759 if (getSlotIndex(I) >= Index) {
760 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
Bill Wendlingd509a662013-01-29 00:34:06 +0000761 break;
762 }
763 LastIndex = I + 1;
764 AttrSet.push_back(getSlotAttributes(I));
765 }
766
Bill Wendlingd2196752013-01-30 23:07:40 +0000767 // Now remove the attribute from the correct slot. There may already be an
Bill Wendlingd509a662013-01-29 00:34:06 +0000768 // AttributeSet there.
Bill Wendling211316c2013-04-18 20:17:28 +0000769 AttrBuilder B(AS, Index);
Bill Wendlingd509a662013-01-29 00:34:06 +0000770
771 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
Bill Wendling211316c2013-04-18 20:17:28 +0000772 if (Attrs.getSlotIndex(I) == Index) {
773 B.removeAttributes(Attrs.pImpl->getSlotAttributes(I), Index);
Bill Wendlingd509a662013-01-29 00:34:06 +0000774 break;
775 }
776
Bill Wendling211316c2013-04-18 20:17:28 +0000777 AttrSet.push_back(AttributeSet::get(C, Index, B));
Bill Wendlingd509a662013-01-29 00:34:06 +0000778
779 // Add the remaining attribute slots.
780 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
781 AttrSet.push_back(getSlotAttributes(I));
782
783 return get(C, AttrSet);
784}
785
786//===----------------------------------------------------------------------===//
787// AttributeSet Accessor Methods
788//===----------------------------------------------------------------------===//
789
Bill Wendling5d020a32013-02-10 05:00:40 +0000790LLVMContext &AttributeSet::getContext() const {
791 return pImpl->getContext();
792}
793
Bill Wendling211316c2013-04-18 20:17:28 +0000794AttributeSet AttributeSet::getParamAttributes(unsigned Index) const {
795 return pImpl && hasAttributes(Index) ?
Bill Wendlingd509a662013-01-29 00:34:06 +0000796 AttributeSet::get(pImpl->getContext(),
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000797 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendling211316c2013-04-18 20:17:28 +0000798 std::make_pair(Index, getAttributes(Index)))) :
Bill Wendlingd509a662013-01-29 00:34:06 +0000799 AttributeSet();
800}
801
802AttributeSet AttributeSet::getRetAttributes() const {
803 return pImpl && hasAttributes(ReturnIndex) ?
804 AttributeSet::get(pImpl->getContext(),
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000805 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendlingd509a662013-01-29 00:34:06 +0000806 std::make_pair(ReturnIndex,
807 getAttributes(ReturnIndex)))) :
808 AttributeSet();
809}
810
811AttributeSet AttributeSet::getFnAttributes() const {
812 return pImpl && hasAttributes(FunctionIndex) ?
813 AttributeSet::get(pImpl->getContext(),
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000814 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendlingd509a662013-01-29 00:34:06 +0000815 std::make_pair(FunctionIndex,
816 getAttributes(FunctionIndex)))) :
817 AttributeSet();
818}
819
820bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000821 AttributeSetNode *ASN = getAttributes(Index);
822 return ASN ? ASN->hasAttribute(Kind) : false;
Bill Wendlingd509a662013-01-29 00:34:06 +0000823}
824
Bill Wendlingbce7b972013-02-13 08:42:21 +0000825bool AttributeSet::hasAttribute(unsigned Index, StringRef Kind) const {
826 AttributeSetNode *ASN = getAttributes(Index);
827 return ASN ? ASN->hasAttribute(Kind) : false;
828}
829
Bill Wendlingd509a662013-01-29 00:34:06 +0000830bool AttributeSet::hasAttributes(unsigned Index) const {
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000831 AttributeSetNode *ASN = getAttributes(Index);
832 return ASN ? ASN->hasAttributes() : false;
Bill Wendlingd509a662013-01-29 00:34:06 +0000833}
834
835/// \brief Return true if the specified attribute is set for at least one
836/// parameter or for the return value.
837bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr) const {
838 if (pImpl == 0) return false;
839
840 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
Benjamin Kramer741146b2013-07-11 12:13:16 +0000841 for (AttributeSetImpl::iterator II = pImpl->begin(I),
Bill Wendlingd509a662013-01-29 00:34:06 +0000842 IE = pImpl->end(I); II != IE; ++II)
NAKAMURA Takumib0944392013-01-31 03:47:28 +0000843 if (II->hasAttribute(Attr))
Bill Wendlingd509a662013-01-29 00:34:06 +0000844 return true;
845
846 return false;
847}
848
Bill Wendlingbce7b972013-02-13 08:42:21 +0000849Attribute AttributeSet::getAttribute(unsigned Index,
850 Attribute::AttrKind Kind) const {
851 AttributeSetNode *ASN = getAttributes(Index);
852 return ASN ? ASN->getAttribute(Kind) : Attribute();
853}
854
855Attribute AttributeSet::getAttribute(unsigned Index,
856 StringRef Kind) const {
857 AttributeSetNode *ASN = getAttributes(Index);
858 return ASN ? ASN->getAttribute(Kind) : Attribute();
859}
860
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000861unsigned AttributeSet::getParamAlignment(unsigned Index) const {
862 AttributeSetNode *ASN = getAttributes(Index);
863 return ASN ? ASN->getAlignment() : 0;
Bill Wendlingd509a662013-01-29 00:34:06 +0000864}
865
866unsigned AttributeSet::getStackAlignment(unsigned Index) const {
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000867 AttributeSetNode *ASN = getAttributes(Index);
868 return ASN ? ASN->getStackAlignment() : 0;
Bill Wendlingd509a662013-01-29 00:34:06 +0000869}
870
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +0000871std::string AttributeSet::getAsString(unsigned Index,
Bill Wendling829b4782013-02-11 08:43:33 +0000872 bool InAttrGrp) const {
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000873 AttributeSetNode *ASN = getAttributes(Index);
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +0000874 return ASN ? ASN->getAsString(InAttrGrp) : std::string("");
Bill Wendlingd509a662013-01-29 00:34:06 +0000875}
876
877/// \brief The attributes for the specified index are returned.
Bill Wendling211316c2013-04-18 20:17:28 +0000878AttributeSetNode *AttributeSet::getAttributes(unsigned Index) const {
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000879 if (!pImpl) return 0;
Bill Wendlingd509a662013-01-29 00:34:06 +0000880
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000881 // Loop through to find the attribute node we want.
882 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
Bill Wendling211316c2013-04-18 20:17:28 +0000883 if (pImpl->getSlotIndex(I) == Index)
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000884 return pImpl->getSlotNode(I);
Bill Wendlingd509a662013-01-29 00:34:06 +0000885
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000886 return 0;
Bill Wendlingd509a662013-01-29 00:34:06 +0000887}
888
Bill Wendling211316c2013-04-18 20:17:28 +0000889AttributeSet::iterator AttributeSet::begin(unsigned Slot) const {
Bill Wendlinga9174862013-01-31 23:53:05 +0000890 if (!pImpl)
891 return ArrayRef<Attribute>().begin();
Bill Wendling211316c2013-04-18 20:17:28 +0000892 return pImpl->begin(Slot);
Bill Wendlinga9174862013-01-31 23:53:05 +0000893}
894
Bill Wendling211316c2013-04-18 20:17:28 +0000895AttributeSet::iterator AttributeSet::end(unsigned Slot) const {
Bill Wendlinga9174862013-01-31 23:53:05 +0000896 if (!pImpl)
897 return ArrayRef<Attribute>().end();
Bill Wendling211316c2013-04-18 20:17:28 +0000898 return pImpl->end(Slot);
Bill Wendlinga9174862013-01-31 23:53:05 +0000899}
900
Bill Wendlingd509a662013-01-29 00:34:06 +0000901//===----------------------------------------------------------------------===//
902// AttributeSet Introspection Methods
903//===----------------------------------------------------------------------===//
904
905/// \brief Return the number of slots used in this attribute list. This is the
906/// number of arguments that have an attribute set on them (including the
907/// function itself).
908unsigned AttributeSet::getNumSlots() const {
909 return pImpl ? pImpl->getNumAttributes() : 0;
910}
911
Rafael Espindoladd275302013-04-30 16:53:38 +0000912unsigned AttributeSet::getSlotIndex(unsigned Slot) const {
Bill Wendlingd509a662013-01-29 00:34:06 +0000913 assert(pImpl && Slot < pImpl->getNumAttributes() &&
914 "Slot # out of range!");
915 return pImpl->getSlotIndex(Slot);
916}
917
918AttributeSet AttributeSet::getSlotAttributes(unsigned Slot) const {
919 assert(pImpl && Slot < pImpl->getNumAttributes() &&
920 "Slot # out of range!");
921 return pImpl->getSlotAttributes(Slot);
922}
923
924uint64_t AttributeSet::Raw(unsigned Index) const {
925 // FIXME: Remove this.
926 return pImpl ? pImpl->Raw(Index) : 0;
927}
928
929void AttributeSet::dump() const {
930 dbgs() << "PAL[\n";
931
932 for (unsigned i = 0, e = getNumSlots(); i < e; ++i) {
933 uint64_t Index = getSlotIndex(i);
934 dbgs() << " { ";
935 if (Index == ~0U)
936 dbgs() << "~0U";
937 else
938 dbgs() << Index;
939 dbgs() << " => " << getAsString(Index) << " }\n";
940 }
941
942 dbgs() << "]\n";
943}
944
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000945//===----------------------------------------------------------------------===//
Bill Wendlingcd330342013-01-04 23:27:34 +0000946// AttrBuilder Method Implementations
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000947//===----------------------------------------------------------------------===//
948
Bill Wendling211316c2013-04-18 20:17:28 +0000949AttrBuilder::AttrBuilder(AttributeSet AS, unsigned Index)
Benjamin Kramer45e7d532013-02-16 19:13:18 +0000950 : Attrs(0), Alignment(0), StackAlignment(0) {
Bill Wendling56b0b2a2013-01-27 21:23:46 +0000951 AttributeSetImpl *pImpl = AS.pImpl;
Bill Wendling096f5442013-01-07 08:24:35 +0000952 if (!pImpl) return;
953
Bill Wendling9eb689c2013-01-28 00:21:34 +0000954 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I) {
Bill Wendling211316c2013-04-18 20:17:28 +0000955 if (pImpl->getSlotIndex(I) != Index) continue;
Bill Wendling096f5442013-01-07 08:24:35 +0000956
Benjamin Kramer741146b2013-07-11 12:13:16 +0000957 for (AttributeSetImpl::iterator II = pImpl->begin(I),
Bill Wendling9eb689c2013-01-28 00:21:34 +0000958 IE = pImpl->end(I); II != IE; ++II)
Bill Wendling23804da2013-01-31 23:38:01 +0000959 addAttribute(*II);
Bill Wendling9eb689c2013-01-28 00:21:34 +0000960
961 break;
962 }
Bill Wendling096f5442013-01-07 08:24:35 +0000963}
964
Bill Wendlingcd330342013-01-04 23:27:34 +0000965void AttrBuilder::clear() {
Benjamin Kramereaf706b2013-02-18 12:09:51 +0000966 Attrs.reset();
Bill Wendlingcd330342013-01-04 23:27:34 +0000967 Alignment = StackAlignment = 0;
968}
969
970AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
Benjamin Kramereaf706b2013-02-18 12:09:51 +0000971 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
Bill Wendling1c7cc8a2013-01-31 23:16:25 +0000972 assert(Val != Attribute::Alignment && Val != Attribute::StackAlignment &&
973 "Adding alignment attribute without adding alignment value!");
Benjamin Kramereaf706b2013-02-18 12:09:51 +0000974 Attrs[Val] = true;
Bill Wendling7c04e042012-10-09 19:01:18 +0000975 return *this;
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000976}
977
Bill Wendling23804da2013-01-31 23:38:01 +0000978AttrBuilder &AttrBuilder::addAttribute(Attribute Attr) {
Bill Wendling0a437302013-02-10 10:13:23 +0000979 if (Attr.isStringAttribute()) {
980 addAttribute(Attr.getKindAsString(), Attr.getValueAsString());
981 return *this;
982 }
983
Bill Wendling3f12ac22013-02-05 22:37:24 +0000984 Attribute::AttrKind Kind = Attr.getKindAsEnum();
Benjamin Kramereaf706b2013-02-18 12:09:51 +0000985 Attrs[Kind] = true;
Bill Wendling1aa9d9e2013-01-28 05:23:28 +0000986
Bill Wendling3f12ac22013-02-05 22:37:24 +0000987 if (Kind == Attribute::Alignment)
Bill Wendling1aa9d9e2013-01-28 05:23:28 +0000988 Alignment = Attr.getAlignment();
Bill Wendling3f12ac22013-02-05 22:37:24 +0000989 else if (Kind == Attribute::StackAlignment)
Bill Wendling1aa9d9e2013-01-28 05:23:28 +0000990 StackAlignment = Attr.getStackAlignment();
991 return *this;
992}
993
Bill Wendlingb9c5b1a2013-02-05 08:09:32 +0000994AttrBuilder &AttrBuilder::addAttribute(StringRef A, StringRef V) {
995 TargetDepAttrs[A] = V;
996 return *this;
997}
998
Bill Wendling23804da2013-01-31 23:38:01 +0000999AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001000 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
1001 Attrs[Val] = false;
Bill Wendling23804da2013-01-31 23:38:01 +00001002
1003 if (Val == Attribute::Alignment)
1004 Alignment = 0;
1005 else if (Val == Attribute::StackAlignment)
1006 StackAlignment = 0;
1007
1008 return *this;
1009}
1010
Bill Wendlingd2196752013-01-30 23:07:40 +00001011AttrBuilder &AttrBuilder::removeAttributes(AttributeSet A, uint64_t Index) {
Bill Wendling211316c2013-04-18 20:17:28 +00001012 unsigned Slot = ~0U;
Bill Wendlingf1c94e32013-02-01 00:13:50 +00001013 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
1014 if (A.getSlotIndex(I) == Index) {
Bill Wendling211316c2013-04-18 20:17:28 +00001015 Slot = I;
Bill Wendlingf1c94e32013-02-01 00:13:50 +00001016 break;
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001017 }
Bill Wendlingf1c94e32013-02-01 00:13:50 +00001018
Bill Wendling211316c2013-04-18 20:17:28 +00001019 assert(Slot != ~0U && "Couldn't find index in AttributeSet!");
Bill Wendlingf1c94e32013-02-01 00:13:50 +00001020
Bill Wendling211316c2013-04-18 20:17:28 +00001021 for (AttributeSet::iterator I = A.begin(Slot), E = A.end(Slot); I != E; ++I) {
Bill Wendling7cde51d2013-02-12 07:56:49 +00001022 Attribute Attr = *I;
1023 if (Attr.isEnumAttribute() || Attr.isAlignAttribute()) {
1024 Attribute::AttrKind Kind = I->getKindAsEnum();
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001025 Attrs[Kind] = false;
Bill Wendlingf1c94e32013-02-01 00:13:50 +00001026
Bill Wendling7cde51d2013-02-12 07:56:49 +00001027 if (Kind == Attribute::Alignment)
1028 Alignment = 0;
1029 else if (Kind == Attribute::StackAlignment)
1030 StackAlignment = 0;
1031 } else {
1032 assert(Attr.isStringAttribute() && "Invalid attribute type!");
1033 std::map<std::string, std::string>::iterator
1034 Iter = TargetDepAttrs.find(Attr.getKindAsString());
1035 if (Iter != TargetDepAttrs.end())
1036 TargetDepAttrs.erase(Iter);
1037 }
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001038 }
1039
1040 return *this;
1041}
1042
Bill Wendlingb9c5b1a2013-02-05 08:09:32 +00001043AttrBuilder &AttrBuilder::removeAttribute(StringRef A) {
1044 std::map<std::string, std::string>::iterator I = TargetDepAttrs.find(A);
1045 if (I != TargetDepAttrs.end())
1046 TargetDepAttrs.erase(I);
1047 return *this;
1048}
1049
Bill Wendling50d27842012-10-15 20:35:56 +00001050AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
Bill Wendlingabd5ba22012-10-14 03:58:29 +00001051 if (Align == 0) return *this;
Bill Wendlingcd330342013-01-04 23:27:34 +00001052
Bill Wendlingabf3feb2012-10-05 06:44:41 +00001053 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1054 assert(Align <= 0x40000000 && "Alignment too large.");
Bill Wendlingcd330342013-01-04 23:27:34 +00001055
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001056 Attrs[Attribute::Alignment] = true;
Bill Wendlingcd330342013-01-04 23:27:34 +00001057 Alignment = Align;
Bill Wendlingabd5ba22012-10-14 03:58:29 +00001058 return *this;
Chris Lattner3e13b8c2008-01-02 23:42:30 +00001059}
1060
Bill Wendlingcd330342013-01-04 23:27:34 +00001061AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
1062 // Default alignment, allow the target to define how to align it.
1063 if (Align == 0) return *this;
1064
1065 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1066 assert(Align <= 0x100 && "Alignment too large.");
1067
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001068 Attrs[Attribute::StackAlignment] = true;
Bill Wendlingcd330342013-01-04 23:27:34 +00001069 StackAlignment = Align;
1070 return *this;
1071}
1072
Bill Wendlinge2614922013-02-06 01:16:00 +00001073AttrBuilder &AttrBuilder::merge(const AttrBuilder &B) {
1074 // FIXME: What if both have alignments, but they don't match?!
1075 if (!Alignment)
1076 Alignment = B.Alignment;
1077
1078 if (!StackAlignment)
1079 StackAlignment = B.StackAlignment;
1080
Benjamin Kramer45e7d532013-02-16 19:13:18 +00001081 Attrs |= B.Attrs;
Bill Wendlinge2614922013-02-06 01:16:00 +00001082
1083 for (td_const_iterator I = B.TargetDepAttrs.begin(),
1084 E = B.TargetDepAttrs.end(); I != E; ++I)
1085 TargetDepAttrs[I->first] = I->second;
1086
1087 return *this;
1088}
1089
Bill Wendling4b001442013-02-06 01:33:42 +00001090bool AttrBuilder::contains(StringRef A) const {
1091 return TargetDepAttrs.find(A) != TargetDepAttrs.end();
1092}
1093
Bill Wendling50d27842012-10-15 20:35:56 +00001094bool AttrBuilder::hasAttributes() const {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001095 return !Attrs.none() || !TargetDepAttrs.empty();
Bill Wendlingc6daefa2012-10-08 23:27:46 +00001096}
Bill Wendling9ac69f92013-01-04 20:54:35 +00001097
Bill Wendlingd2196752013-01-30 23:07:40 +00001098bool AttrBuilder::hasAttributes(AttributeSet A, uint64_t Index) const {
Bill Wendling211316c2013-04-18 20:17:28 +00001099 unsigned Slot = ~0U;
Bill Wendling9ca01da2013-02-02 00:42:06 +00001100 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
1101 if (A.getSlotIndex(I) == Index) {
Bill Wendling211316c2013-04-18 20:17:28 +00001102 Slot = I;
Bill Wendling9ca01da2013-02-02 00:42:06 +00001103 break;
1104 }
1105
Bill Wendling211316c2013-04-18 20:17:28 +00001106 assert(Slot != ~0U && "Couldn't find the index!");
Bill Wendling9ca01da2013-02-02 00:42:06 +00001107
Bill Wendling211316c2013-04-18 20:17:28 +00001108 for (AttributeSet::iterator I = A.begin(Slot), E = A.end(Slot);
Bill Wendling7cde51d2013-02-12 07:56:49 +00001109 I != E; ++I) {
1110 Attribute Attr = *I;
1111 if (Attr.isEnumAttribute() || Attr.isAlignAttribute()) {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001112 if (Attrs[I->getKindAsEnum()])
Bill Wendling7cde51d2013-02-12 07:56:49 +00001113 return true;
1114 } else {
1115 assert(Attr.isStringAttribute() && "Invalid attribute kind!");
1116 return TargetDepAttrs.find(Attr.getKindAsString())!=TargetDepAttrs.end();
1117 }
1118 }
Bill Wendling9ca01da2013-02-02 00:42:06 +00001119
1120 return false;
Bill Wendling70f39172012-10-09 00:01:21 +00001121}
Bill Wendling9ac69f92013-01-04 20:54:35 +00001122
Bill Wendling50d27842012-10-15 20:35:56 +00001123bool AttrBuilder::hasAlignmentAttr() const {
Bill Wendlingcd330342013-01-04 23:27:34 +00001124 return Alignment != 0;
Bill Wendlingc6daefa2012-10-08 23:27:46 +00001125}
1126
Bill Wendlingd509a662013-01-29 00:34:06 +00001127bool AttrBuilder::operator==(const AttrBuilder &B) {
Benjamin Kramer45e7d532013-02-16 19:13:18 +00001128 if (Attrs != B.Attrs)
1129 return false;
Bill Wendling4b001442013-02-06 01:33:42 +00001130
1131 for (td_const_iterator I = TargetDepAttrs.begin(),
1132 E = TargetDepAttrs.end(); I != E; ++I)
1133 if (B.TargetDepAttrs.find(I->first) == B.TargetDepAttrs.end())
1134 return false;
1135
1136 return Alignment == B.Alignment && StackAlignment == B.StackAlignment;
Bill Wendlingd509a662013-01-29 00:34:06 +00001137}
1138
1139AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) {
Bill Wendling56aeccc2013-02-04 23:32:23 +00001140 // FIXME: Remove this in 4.0.
Bill Wendling60011b82013-01-29 01:43:29 +00001141 if (!Val) return *this;
1142
Bill Wendlingd509a662013-01-29 00:34:06 +00001143 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
1144 I = Attribute::AttrKind(I + 1)) {
1145 if (uint64_t A = (Val & AttributeImpl::getAttrMask(I))) {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001146 Attrs[I] = true;
Bill Wendlingd509a662013-01-29 00:34:06 +00001147
1148 if (I == Attribute::Alignment)
1149 Alignment = 1ULL << ((A >> 16) - 1);
1150 else if (I == Attribute::StackAlignment)
1151 StackAlignment = 1ULL << ((A >> 26)-1);
1152 }
1153 }
1154
1155 return *this;
1156}
1157
Bill Wendling57625a42013-01-25 23:09:36 +00001158//===----------------------------------------------------------------------===//
1159// AttributeFuncs Function Defintions
1160//===----------------------------------------------------------------------===//
1161
Bill Wendlingc79cdff2013-02-01 01:04:27 +00001162/// \brief Which attributes cannot be applied to a type.
Bill Wendlingd2196752013-01-30 23:07:40 +00001163AttributeSet AttributeFuncs::typeIncompatible(Type *Ty, uint64_t Index) {
Bill Wendling57625a42013-01-25 23:09:36 +00001164 AttrBuilder Incompatible;
1165
1166 if (!Ty->isIntegerTy())
1167 // Attribute that only apply to integers.
1168 Incompatible.addAttribute(Attribute::SExt)
1169 .addAttribute(Attribute::ZExt);
1170
1171 if (!Ty->isPointerTy())
1172 // Attribute that only apply to pointers.
1173 Incompatible.addAttribute(Attribute::ByVal)
1174 .addAttribute(Attribute::Nest)
1175 .addAttribute(Attribute::NoAlias)
1176 .addAttribute(Attribute::NoCapture)
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001177 .addAttribute(Attribute::ReadNone)
1178 .addAttribute(Attribute::ReadOnly)
Reid Klecknera534a382013-12-19 02:14:12 +00001179 .addAttribute(Attribute::StructRet)
1180 .addAttribute(Attribute::InAlloca);
Bill Wendling57625a42013-01-25 23:09:36 +00001181
Bill Wendlingd2196752013-01-30 23:07:40 +00001182 return AttributeSet::get(Ty->getContext(), Index, Incompatible);
Bill Wendling57625a42013-01-25 23:09:36 +00001183}