blob: a9074bb294d9ffc62940601db9121fb35ef8108e [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"
Benjamin Kramer502b9e12014-04-12 16:15:53 +000019#include "llvm/ADT/STLExtras.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000020#include "llvm/ADT/StringExtras.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000021#include "llvm/IR/Type.h"
Benjamin Kramer17388a62014-03-03 18:02:34 +000022#include "llvm/Support/Atomic.h"
David Greenef7014732010-01-05 01:29:58 +000023#include "llvm/Support/Debug.h"
Chris Lattner3e13b8c2008-01-02 23:42:30 +000024#include "llvm/Support/ManagedStatic.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000025#include "llvm/Support/Mutex.h"
Benjamin Kramer1a25d732009-08-23 11:37:21 +000026#include "llvm/Support/raw_ostream.h"
Bill Wendlingd2e493b2013-01-24 00:06:56 +000027#include <algorithm>
Chris Lattner3e13b8c2008-01-02 23:42:30 +000028using namespace llvm;
29
Chris Lattner8a923e72008-03-12 17:45:29 +000030//===----------------------------------------------------------------------===//
Bill Wendling7707c5a2013-01-29 00:48:16 +000031// Attribute Construction Methods
Chris Lattner8a923e72008-03-12 17:45:29 +000032//===----------------------------------------------------------------------===//
Chris Lattnerd0e1f102008-01-03 00:10:22 +000033
Bill Wendling3f12ac22013-02-05 22:37:24 +000034Attribute Attribute::get(LLVMContext &Context, Attribute::AttrKind Kind,
35 uint64_t Val) {
Bill Wendling73ea2de2012-10-08 21:47:17 +000036 LLVMContextImpl *pImpl = Context.pImpl;
37 FoldingSetNodeID ID;
Bill Wendling3f12ac22013-02-05 22:37:24 +000038 ID.AddInteger(Kind);
39 if (Val) ID.AddInteger(Val);
40
41 void *InsertPoint;
42 AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
43
44 if (!PA) {
45 // If we didn't find any existing attributes of the same shape then create a
46 // new one and insert it.
Benjamin Kramer741146b2013-07-11 12:13:16 +000047 if (!Val)
48 PA = new EnumAttributeImpl(Kind);
49 else
50 PA = new AlignAttributeImpl(Kind, Val);
Bill Wendling3f12ac22013-02-05 22:37:24 +000051 pImpl->AttrsSet.InsertNode(PA, InsertPoint);
52 }
53
54 // Return the Attribute that we found or created.
55 return Attribute(PA);
56}
57
58Attribute Attribute::get(LLVMContext &Context, StringRef Kind, StringRef Val) {
59 LLVMContextImpl *pImpl = Context.pImpl;
60 FoldingSetNodeID ID;
61 ID.AddString(Kind);
62 if (!Val.empty()) ID.AddString(Val);
Bill Wendling73ea2de2012-10-08 21:47:17 +000063
64 void *InsertPoint;
Bill Wendling4607f4b2012-12-20 01:36:59 +000065 AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
Bill Wendling73ea2de2012-10-08 21:47:17 +000066
67 if (!PA) {
68 // If we didn't find any existing attributes of the same shape then create a
69 // new one and insert it.
Benjamin Kramer741146b2013-07-11 12:13:16 +000070 PA = new StringAttributeImpl(Kind, Val);
Bill Wendling73ea2de2012-10-08 21:47:17 +000071 pImpl->AttrsSet.InsertNode(PA, InsertPoint);
72 }
73
Bill Wendlingb9c5b1a2013-02-05 08:09:32 +000074 // Return the Attribute that we found or created.
Bill Wendling3d7b0b82012-12-19 07:18:57 +000075 return Attribute(PA);
Bill Wendling73ea2de2012-10-08 21:47:17 +000076}
77
Bill Wendling4bbe9db2013-01-27 22:43:04 +000078Attribute Attribute::getWithAlignment(LLVMContext &Context, uint64_t Align) {
Bill Wendling1c7cc8a2013-01-31 23:16:25 +000079 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
80 assert(Align <= 0x40000000 && "Alignment too large.");
Bill Wendling3f12ac22013-02-05 22:37:24 +000081 return get(Context, Alignment, Align);
Bill Wendling4bbe9db2013-01-27 22:43:04 +000082}
83
84Attribute Attribute::getWithStackAlignment(LLVMContext &Context,
85 uint64_t Align) {
Bill Wendling1c7cc8a2013-01-31 23:16:25 +000086 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
87 assert(Align <= 0x100 && "Alignment too large.");
Bill Wendling3f12ac22013-02-05 22:37:24 +000088 return get(Context, StackAlignment, Align);
Bill Wendling4bbe9db2013-01-27 22:43:04 +000089}
90
Bill Wendling7707c5a2013-01-29 00:48:16 +000091//===----------------------------------------------------------------------===//
92// Attribute Accessor Methods
93//===----------------------------------------------------------------------===//
94
Bill Wendling3f12ac22013-02-05 22:37:24 +000095bool Attribute::isEnumAttribute() const {
96 return pImpl && pImpl->isEnumAttribute();
97}
98
99bool Attribute::isAlignAttribute() const {
100 return pImpl && pImpl->isAlignAttribute();
101}
102
103bool Attribute::isStringAttribute() const {
104 return pImpl && pImpl->isStringAttribute();
105}
106
107Attribute::AttrKind Attribute::getKindAsEnum() const {
Bill Wendling440e9d82013-07-25 00:34:29 +0000108 if (!pImpl) return None;
Bill Wendling3f12ac22013-02-05 22:37:24 +0000109 assert((isEnumAttribute() || isAlignAttribute()) &&
110 "Invalid attribute type to get the kind as an enum!");
111 return pImpl ? pImpl->getKindAsEnum() : None;
112}
113
114uint64_t Attribute::getValueAsInt() const {
Bill Wendling440e9d82013-07-25 00:34:29 +0000115 if (!pImpl) return 0;
Bill Wendling3f12ac22013-02-05 22:37:24 +0000116 assert(isAlignAttribute() &&
117 "Expected the attribute to be an alignment attribute!");
118 return pImpl ? pImpl->getValueAsInt() : 0;
119}
120
121StringRef Attribute::getKindAsString() const {
Bill Wendling440e9d82013-07-25 00:34:29 +0000122 if (!pImpl) return StringRef();
Bill Wendling3f12ac22013-02-05 22:37:24 +0000123 assert(isStringAttribute() &&
124 "Invalid attribute type to get the kind as a string!");
125 return pImpl ? pImpl->getKindAsString() : StringRef();
126}
127
128StringRef Attribute::getValueAsString() const {
Bill Wendling440e9d82013-07-25 00:34:29 +0000129 if (!pImpl) return StringRef();
Bill Wendling3f12ac22013-02-05 22:37:24 +0000130 assert(isStringAttribute() &&
131 "Invalid attribute type to get the value as a string!");
132 return pImpl ? pImpl->getValueAsString() : StringRef();
133}
134
Bill Wendlingae89a0f2013-02-05 23:48:36 +0000135bool Attribute::hasAttribute(AttrKind Kind) const {
136 return (pImpl && pImpl->hasAttribute(Kind)) || (!pImpl && Kind == None);
137}
138
139bool Attribute::hasAttribute(StringRef Kind) const {
140 if (!isStringAttribute()) return false;
141 return pImpl && pImpl->hasAttribute(Kind);
Bill Wendling03eefb32013-01-29 20:45:34 +0000142}
143
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000144/// This returns the alignment field of an attribute as a byte alignment value.
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000145unsigned Attribute::getAlignment() const {
Bill Wendlingc79cdff2013-02-01 01:04:27 +0000146 assert(hasAttribute(Attribute::Alignment) &&
147 "Trying to get alignment from non-alignment attribute!");
Bill Wendling3f12ac22013-02-05 22:37:24 +0000148 return pImpl->getValueAsInt();
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000149}
150
151/// This returns the stack alignment field of an attribute as a byte alignment
152/// value.
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000153unsigned Attribute::getStackAlignment() const {
Bill Wendlingc79cdff2013-02-01 01:04:27 +0000154 assert(hasAttribute(Attribute::StackAlignment) &&
155 "Trying to get alignment from non-alignment attribute!");
Bill Wendling3f12ac22013-02-05 22:37:24 +0000156 return pImpl->getValueAsInt();
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000157}
158
Bill Wendling829b4782013-02-11 08:43:33 +0000159std::string Attribute::getAsString(bool InAttrGrp) const {
Bill Wendling9c2eba92013-01-31 20:59:05 +0000160 if (!pImpl) return "";
161
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000162 if (hasAttribute(Attribute::SanitizeAddress))
163 return "sanitize_address";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000164 if (hasAttribute(Attribute::AlwaysInline))
165 return "alwaysinline";
Michael Gottesman41748d72013-06-27 00:25:01 +0000166 if (hasAttribute(Attribute::Builtin))
167 return "builtin";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000168 if (hasAttribute(Attribute::ByVal))
169 return "byval";
Reid Klecknera534a382013-12-19 02:14:12 +0000170 if (hasAttribute(Attribute::InAlloca))
171 return "inalloca";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000172 if (hasAttribute(Attribute::InlineHint))
173 return "inlinehint";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000174 if (hasAttribute(Attribute::InReg))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000175 return "inreg";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000176 if (hasAttribute(Attribute::MinSize))
177 return "minsize";
178 if (hasAttribute(Attribute::Naked))
179 return "naked";
180 if (hasAttribute(Attribute::Nest))
181 return "nest";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000182 if (hasAttribute(Attribute::NoAlias))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000183 return "noalias";
Bill Wendling09bd1f72013-02-22 00:12:35 +0000184 if (hasAttribute(Attribute::NoBuiltin))
185 return "nobuiltin";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000186 if (hasAttribute(Attribute::NoCapture))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000187 return "nocapture";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000188 if (hasAttribute(Attribute::NoDuplicate))
189 return "noduplicate";
190 if (hasAttribute(Attribute::NoImplicitFloat))
191 return "noimplicitfloat";
192 if (hasAttribute(Attribute::NoInline))
193 return "noinline";
194 if (hasAttribute(Attribute::NonLazyBind))
195 return "nonlazybind";
Nick Lewyckyd52b1522014-05-20 01:23:40 +0000196 if (hasAttribute(Attribute::NonNull))
197 return "nonnull";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000198 if (hasAttribute(Attribute::NoRedZone))
199 return "noredzone";
200 if (hasAttribute(Attribute::NoReturn))
201 return "noreturn";
202 if (hasAttribute(Attribute::NoUnwind))
203 return "nounwind";
Andrea Di Biagio377496b2013-08-23 11:53:55 +0000204 if (hasAttribute(Attribute::OptimizeNone))
205 return "optnone";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000206 if (hasAttribute(Attribute::OptimizeForSize))
207 return "optsize";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000208 if (hasAttribute(Attribute::ReadNone))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000209 return "readnone";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000210 if (hasAttribute(Attribute::ReadOnly))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000211 return "readonly";
Stephen Linb8bd2322013-04-20 05:14:40 +0000212 if (hasAttribute(Attribute::Returned))
213 return "returned";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000214 if (hasAttribute(Attribute::ReturnsTwice))
215 return "returns_twice";
216 if (hasAttribute(Attribute::SExt))
217 return "signext";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000218 if (hasAttribute(Attribute::StackProtect))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000219 return "ssp";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000220 if (hasAttribute(Attribute::StackProtectReq))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000221 return "sspreq";
Bill Wendlingd154e2832013-01-23 06:41:41 +0000222 if (hasAttribute(Attribute::StackProtectStrong))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000223 return "sspstrong";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000224 if (hasAttribute(Attribute::StructRet))
225 return "sret";
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000226 if (hasAttribute(Attribute::SanitizeThread))
227 return "sanitize_thread";
228 if (hasAttribute(Attribute::SanitizeMemory))
229 return "sanitize_memory";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000230 if (hasAttribute(Attribute::UWTable))
231 return "uwtable";
232 if (hasAttribute(Attribute::ZExt))
233 return "zeroext";
Diego Novilloc6399532013-05-24 12:26:52 +0000234 if (hasAttribute(Attribute::Cold))
235 return "cold";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000236
237 // FIXME: These should be output like this:
238 //
239 // align=4
240 // alignstack=8
241 //
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000242 if (hasAttribute(Attribute::Alignment)) {
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000243 std::string Result;
Bill Wendling829b4782013-02-11 08:43:33 +0000244 Result += "align";
245 Result += (InAttrGrp) ? "=" : " ";
Bill Wendling3f12ac22013-02-05 22:37:24 +0000246 Result += utostr(getValueAsInt());
247 return Result;
248 }
Bill Wendling829b4782013-02-11 08:43:33 +0000249
Bill Wendling3f12ac22013-02-05 22:37:24 +0000250 if (hasAttribute(Attribute::StackAlignment)) {
251 std::string Result;
Bill Wendling829b4782013-02-11 08:43:33 +0000252 Result += "alignstack";
253 if (InAttrGrp) {
254 Result += "=";
255 Result += utostr(getValueAsInt());
256 } else {
257 Result += "(";
258 Result += utostr(getValueAsInt());
259 Result += ")";
260 }
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000261 return Result;
Dale Johannesen11a555e2008-02-19 23:51:49 +0000262 }
Bill Wendling9c2eba92013-01-31 20:59:05 +0000263
264 // Convert target-dependent attributes to strings of the form:
265 //
266 // "kind"
267 // "kind" = "value"
Bill Wendling9c2eba92013-01-31 20:59:05 +0000268 //
Bill Wendling3f12ac22013-02-05 22:37:24 +0000269 if (isStringAttribute()) {
Bill Wendling9c2eba92013-01-31 20:59:05 +0000270 std::string Result;
Bill Wendling3f12ac22013-02-05 22:37:24 +0000271 Result += '\"' + getKindAsString().str() + '"';
Bill Wendling9c2eba92013-01-31 20:59:05 +0000272
Bill Wendling3f12ac22013-02-05 22:37:24 +0000273 StringRef Val = pImpl->getValueAsString();
274 if (Val.empty()) return Result;
Bill Wendling7a33f772013-02-01 22:32:30 +0000275
Bill Wendling829b4782013-02-11 08:43:33 +0000276 Result += "=\"" + Val.str() + '"';
Bill Wendlingc79cdff2013-02-01 01:04:27 +0000277 return Result;
Bill Wendling9c2eba92013-01-31 20:59:05 +0000278 }
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000279
280 llvm_unreachable("Unknown attribute");
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000281}
282
Bill Wendlingd509a662013-01-29 00:34:06 +0000283bool Attribute::operator<(Attribute A) const {
284 if (!pImpl && !A.pImpl) return false;
285 if (!pImpl) return true;
286 if (!A.pImpl) return false;
287 return *pImpl < *A.pImpl;
288}
289
Bill Wendlingd509a662013-01-29 00:34:06 +0000290//===----------------------------------------------------------------------===//
291// AttributeImpl Definition
292//===----------------------------------------------------------------------===//
293
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +0000294// Pin the vtabels to this file.
Alexey Samsonov49109a22013-11-18 09:31:53 +0000295AttributeImpl::~AttributeImpl() {}
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +0000296void EnumAttributeImpl::anchor() {}
297void AlignAttributeImpl::anchor() {}
298void StringAttributeImpl::anchor() {}
Alexey Samsonov49109a22013-11-18 09:31:53 +0000299
Bill Wendlingd509a662013-01-29 00:34:06 +0000300bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
Bill Wendling3f12ac22013-02-05 22:37:24 +0000301 if (isStringAttribute()) return false;
302 return getKindAsEnum() == A;
Bill Wendlingd509a662013-01-29 00:34:06 +0000303}
304
Bill Wendling3f12ac22013-02-05 22:37:24 +0000305bool AttributeImpl::hasAttribute(StringRef Kind) const {
306 if (!isStringAttribute()) return false;
307 return getKindAsString() == Kind;
Bill Wendlingd509a662013-01-29 00:34:06 +0000308}
309
Bill Wendling3f12ac22013-02-05 22:37:24 +0000310Attribute::AttrKind AttributeImpl::getKindAsEnum() const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000311 assert(isEnumAttribute() || isAlignAttribute());
312 return static_cast<const EnumAttributeImpl *>(this)->getEnumKind();
Bill Wendlingd509a662013-01-29 00:34:06 +0000313}
314
Bill Wendling3f12ac22013-02-05 22:37:24 +0000315uint64_t AttributeImpl::getValueAsInt() const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000316 assert(isAlignAttribute());
317 return static_cast<const AlignAttributeImpl *>(this)->getAlignment();
Bill Wendlingd509a662013-01-29 00:34:06 +0000318}
319
Bill Wendling3f12ac22013-02-05 22:37:24 +0000320StringRef AttributeImpl::getKindAsString() const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000321 assert(isStringAttribute());
322 return static_cast<const StringAttributeImpl *>(this)->getStringKind();
Bill Wendlingd509a662013-01-29 00:34:06 +0000323}
324
Bill Wendling3f12ac22013-02-05 22:37:24 +0000325StringRef AttributeImpl::getValueAsString() const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000326 assert(isStringAttribute());
327 return static_cast<const StringAttributeImpl *>(this)->getStringValue();
Bill Wendlingd509a662013-01-29 00:34:06 +0000328}
329
330bool AttributeImpl::operator<(const AttributeImpl &AI) const {
Bill Wendlingc79cdff2013-02-01 01:04:27 +0000331 // This sorts the attributes with Attribute::AttrKinds coming first (sorted
332 // relative to their enum value) and then strings.
Bill Wendling26b95752013-02-15 05:25:26 +0000333 if (isEnumAttribute()) {
334 if (AI.isEnumAttribute()) return getKindAsEnum() < AI.getKindAsEnum();
335 if (AI.isAlignAttribute()) return true;
336 if (AI.isStringAttribute()) return true;
337 }
Bill Wendlingc79cdff2013-02-01 01:04:27 +0000338
Bill Wendling3f12ac22013-02-05 22:37:24 +0000339 if (isAlignAttribute()) {
Bill Wendling26b95752013-02-15 05:25:26 +0000340 if (AI.isEnumAttribute()) return false;
341 if (AI.isAlignAttribute()) return getValueAsInt() < AI.getValueAsInt();
342 if (AI.isStringAttribute()) return true;
Bill Wendling3f12ac22013-02-05 22:37:24 +0000343 }
Bill Wendlingd509a662013-01-29 00:34:06 +0000344
Bill Wendling26b95752013-02-15 05:25:26 +0000345 if (AI.isEnumAttribute()) return false;
346 if (AI.isAlignAttribute()) return false;
347 if (getKindAsString() == AI.getKindAsString())
348 return getValueAsString() < AI.getValueAsString();
349 return getKindAsString() < AI.getKindAsString();
Bill Wendlingd509a662013-01-29 00:34:06 +0000350}
351
Bill Wendlingd509a662013-01-29 00:34:06 +0000352uint64_t AttributeImpl::getAttrMask(Attribute::AttrKind Val) {
353 // FIXME: Remove this.
354 switch (Val) {
355 case Attribute::EndAttrKinds:
Bill Wendlingd509a662013-01-29 00:34:06 +0000356 llvm_unreachable("Synthetic enumerators which should never get here");
357
358 case Attribute::None: return 0;
359 case Attribute::ZExt: return 1 << 0;
360 case Attribute::SExt: return 1 << 1;
361 case Attribute::NoReturn: return 1 << 2;
362 case Attribute::InReg: return 1 << 3;
363 case Attribute::StructRet: return 1 << 4;
364 case Attribute::NoUnwind: return 1 << 5;
365 case Attribute::NoAlias: return 1 << 6;
366 case Attribute::ByVal: return 1 << 7;
367 case Attribute::Nest: return 1 << 8;
368 case Attribute::ReadNone: return 1 << 9;
369 case Attribute::ReadOnly: return 1 << 10;
370 case Attribute::NoInline: return 1 << 11;
371 case Attribute::AlwaysInline: return 1 << 12;
372 case Attribute::OptimizeForSize: return 1 << 13;
373 case Attribute::StackProtect: return 1 << 14;
374 case Attribute::StackProtectReq: return 1 << 15;
375 case Attribute::Alignment: return 31 << 16;
376 case Attribute::NoCapture: return 1 << 21;
377 case Attribute::NoRedZone: return 1 << 22;
378 case Attribute::NoImplicitFloat: return 1 << 23;
379 case Attribute::Naked: return 1 << 24;
380 case Attribute::InlineHint: return 1 << 25;
381 case Attribute::StackAlignment: return 7 << 26;
382 case Attribute::ReturnsTwice: return 1 << 29;
383 case Attribute::UWTable: return 1 << 30;
384 case Attribute::NonLazyBind: return 1U << 31;
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000385 case Attribute::SanitizeAddress: return 1ULL << 32;
Bill Wendlingd509a662013-01-29 00:34:06 +0000386 case Attribute::MinSize: return 1ULL << 33;
387 case Attribute::NoDuplicate: return 1ULL << 34;
388 case Attribute::StackProtectStrong: return 1ULL << 35;
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000389 case Attribute::SanitizeThread: return 1ULL << 36;
390 case Attribute::SanitizeMemory: return 1ULL << 37;
Bill Wendling7bfdfe72013-02-22 00:40:12 +0000391 case Attribute::NoBuiltin: return 1ULL << 38;
Stephen Linb8bd2322013-04-20 05:14:40 +0000392 case Attribute::Returned: return 1ULL << 39;
Diego Novilloc6399532013-05-24 12:26:52 +0000393 case Attribute::Cold: return 1ULL << 40;
Michael Gottesman41748d72013-06-27 00:25:01 +0000394 case Attribute::Builtin: return 1ULL << 41;
Andrea Di Biagio377496b2013-08-23 11:53:55 +0000395 case Attribute::OptimizeNone: return 1ULL << 42;
Reid Klecknera534a382013-12-19 02:14:12 +0000396 case Attribute::InAlloca: return 1ULL << 43;
Nick Lewyckyd52b1522014-05-20 01:23:40 +0000397 case Attribute::NonNull: return 1ULL << 44;
Bill Wendling25342e12013-02-22 00:50:09 +0000398 }
399 llvm_unreachable("Unsupported attribute type");
Bill Wendlingd509a662013-01-29 00:34:06 +0000400}
401
402//===----------------------------------------------------------------------===//
403// AttributeSetNode Definition
404//===----------------------------------------------------------------------===//
405
406AttributeSetNode *AttributeSetNode::get(LLVMContext &C,
407 ArrayRef<Attribute> Attrs) {
408 if (Attrs.empty())
Craig Topperc6207612014-04-09 06:08:46 +0000409 return nullptr;
Bill Wendlingd509a662013-01-29 00:34:06 +0000410
411 // Otherwise, build a key to look up the existing attributes.
412 LLVMContextImpl *pImpl = C.pImpl;
413 FoldingSetNodeID ID;
414
415 SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end());
Bill Wendling82c2ee62013-02-13 09:26:26 +0000416 array_pod_sort(SortedAttrs.begin(), SortedAttrs.end());
Bill Wendlingd509a662013-01-29 00:34:06 +0000417
418 for (SmallVectorImpl<Attribute>::iterator I = SortedAttrs.begin(),
419 E = SortedAttrs.end(); I != E; ++I)
420 I->Profile(ID);
421
422 void *InsertPoint;
423 AttributeSetNode *PA =
424 pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint);
425
426 // If we didn't find any existing attributes of the same shape then create a
427 // new one and insert it.
428 if (!PA) {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000429 // Coallocate entries after the AttributeSetNode itself.
430 void *Mem = ::operator new(sizeof(AttributeSetNode) +
431 sizeof(Attribute) * SortedAttrs.size());
432 PA = new (Mem) AttributeSetNode(SortedAttrs);
Bill Wendlingd509a662013-01-29 00:34:06 +0000433 pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint);
434 }
435
436 // Return the AttributesListNode that we found or created.
437 return PA;
438}
439
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000440bool AttributeSetNode::hasAttribute(Attribute::AttrKind Kind) const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000441 for (iterator I = begin(), E = end(); I != E; ++I)
NAKAMURA Takumib0944392013-01-31 03:47:28 +0000442 if (I->hasAttribute(Kind))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000443 return true;
444 return false;
445}
446
Bill Wendlingbce7b972013-02-13 08:42:21 +0000447bool AttributeSetNode::hasAttribute(StringRef Kind) const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000448 for (iterator I = begin(), E = end(); I != E; ++I)
Bill Wendlingbce7b972013-02-13 08:42:21 +0000449 if (I->hasAttribute(Kind))
450 return true;
451 return false;
452}
453
454Attribute AttributeSetNode::getAttribute(Attribute::AttrKind Kind) const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000455 for (iterator I = begin(), E = end(); I != E; ++I)
Bill Wendlingbce7b972013-02-13 08:42:21 +0000456 if (I->hasAttribute(Kind))
457 return *I;
458 return Attribute();
459}
460
461Attribute AttributeSetNode::getAttribute(StringRef Kind) const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000462 for (iterator I = begin(), E = end(); I != E; ++I)
Bill Wendlingbce7b972013-02-13 08:42:21 +0000463 if (I->hasAttribute(Kind))
464 return *I;
465 return Attribute();
466}
467
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000468unsigned AttributeSetNode::getAlignment() const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000469 for (iterator I = begin(), E = end(); I != E; ++I)
NAKAMURA Takumib0944392013-01-31 03:47:28 +0000470 if (I->hasAttribute(Attribute::Alignment))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000471 return I->getAlignment();
472 return 0;
473}
474
475unsigned AttributeSetNode::getStackAlignment() const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000476 for (iterator I = begin(), E = end(); I != E; ++I)
NAKAMURA Takumib0944392013-01-31 03:47:28 +0000477 if (I->hasAttribute(Attribute::StackAlignment))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000478 return I->getStackAlignment();
479 return 0;
480}
481
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +0000482std::string AttributeSetNode::getAsString(bool InAttrGrp) const {
Benjamin Kramer0baf8f42013-04-19 11:43:21 +0000483 std::string Str;
Benjamin Kramer741146b2013-07-11 12:13:16 +0000484 for (iterator I = begin(), E = end(); I != E; ++I) {
485 if (I != begin())
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +0000486 Str += ' ';
487 Str += I->getAsString(InAttrGrp);
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000488 }
489 return Str;
490}
491
Bill Wendlingd509a662013-01-29 00:34:06 +0000492//===----------------------------------------------------------------------===//
493// AttributeSetImpl Definition
494//===----------------------------------------------------------------------===//
495
Rafael Espindoladd275302013-04-30 16:53:38 +0000496uint64_t AttributeSetImpl::Raw(unsigned Index) const {
Bill Wendlingd509a662013-01-29 00:34:06 +0000497 for (unsigned I = 0, E = getNumAttributes(); I != E; ++I) {
498 if (getSlotIndex(I) != Index) continue;
Benjamin Kramer741146b2013-07-11 12:13:16 +0000499 const AttributeSetNode *ASN = getSlotNode(I);
Bill Wendling91226182013-02-02 00:52:44 +0000500 uint64_t Mask = 0;
Bill Wendlingd509a662013-01-29 00:34:06 +0000501
Benjamin Kramer741146b2013-07-11 12:13:16 +0000502 for (AttributeSetNode::iterator II = ASN->begin(),
Bill Wendling91226182013-02-02 00:52:44 +0000503 IE = ASN->end(); II != IE; ++II) {
504 Attribute Attr = *II;
Bill Wendlingd746e402013-02-10 23:18:05 +0000505
506 // This cannot handle string attributes.
507 if (Attr.isStringAttribute()) continue;
508
Bill Wendling3f12ac22013-02-05 22:37:24 +0000509 Attribute::AttrKind Kind = Attr.getKindAsEnum();
Bill Wendling91226182013-02-02 00:52:44 +0000510
Bill Wendling3f12ac22013-02-05 22:37:24 +0000511 if (Kind == Attribute::Alignment)
Bill Wendling91226182013-02-02 00:52:44 +0000512 Mask |= (Log2_32(ASN->getAlignment()) + 1) << 16;
Bill Wendling3f12ac22013-02-05 22:37:24 +0000513 else if (Kind == Attribute::StackAlignment)
Bill Wendling91226182013-02-02 00:52:44 +0000514 Mask |= (Log2_32(ASN->getStackAlignment()) + 1) << 26;
515 else
Bill Wendling3f12ac22013-02-05 22:37:24 +0000516 Mask |= AttributeImpl::getAttrMask(Kind);
Bill Wendling91226182013-02-02 00:52:44 +0000517 }
518
519 return Mask;
Bill Wendlingd509a662013-01-29 00:34:06 +0000520 }
521
522 return 0;
523}
524
Peter Collingbourneabca2ec2013-08-02 22:34:30 +0000525void AttributeSetImpl::dump() const {
526 AttributeSet(const_cast<AttributeSetImpl *>(this)).dump();
527}
528
Bill Wendlingd509a662013-01-29 00:34:06 +0000529//===----------------------------------------------------------------------===//
530// AttributeSet Construction and Mutation Methods
531//===----------------------------------------------------------------------===//
532
Bill Wendling60011b82013-01-29 01:43:29 +0000533AttributeSet
534AttributeSet::getImpl(LLVMContext &C,
535 ArrayRef<std::pair<unsigned, AttributeSetNode*> > Attrs) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000536 LLVMContextImpl *pImpl = C.pImpl;
537 FoldingSetNodeID ID;
538 AttributeSetImpl::Profile(ID, Attrs);
539
540 void *InsertPoint;
541 AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
542
543 // If we didn't find any existing attributes of the same shape then
544 // create a new one and insert it.
545 if (!PA) {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000546 // Coallocate entries after the AttributeSetImpl itself.
547 void *Mem = ::operator new(sizeof(AttributeSetImpl) +
548 sizeof(std::pair<unsigned, AttributeSetNode *>) *
549 Attrs.size());
550 PA = new (Mem) AttributeSetImpl(C, Attrs);
Bill Wendlingd509a662013-01-29 00:34:06 +0000551 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
552 }
553
554 // Return the AttributesList that we found or created.
555 return AttributeSet(PA);
556}
557
558AttributeSet AttributeSet::get(LLVMContext &C,
559 ArrayRef<std::pair<unsigned, Attribute> > Attrs){
560 // If there are no attributes then return a null AttributesList pointer.
561 if (Attrs.empty())
562 return AttributeSet();
563
564#ifndef NDEBUG
565 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
566 assert((!i || Attrs[i-1].first <= Attrs[i].first) &&
567 "Misordered Attributes list!");
Bill Wendling3f12ac22013-02-05 22:37:24 +0000568 assert(!Attrs[i].second.hasAttribute(Attribute::None) &&
Bill Wendlingd509a662013-01-29 00:34:06 +0000569 "Pointless attribute!");
570 }
571#endif
572
573 // Create a vector if (unsigned, AttributeSetNode*) pairs from the attributes
574 // list.
575 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrPairVec;
576 for (ArrayRef<std::pair<unsigned, Attribute> >::iterator I = Attrs.begin(),
577 E = Attrs.end(); I != E; ) {
578 unsigned Index = I->first;
579 SmallVector<Attribute, 4> AttrVec;
NAKAMURA Takumif05d2f22013-01-29 15:18:16 +0000580 while (I != E && I->first == Index) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000581 AttrVec.push_back(I->second);
582 ++I;
583 }
584
585 AttrPairVec.push_back(std::make_pair(Index,
586 AttributeSetNode::get(C, AttrVec)));
587 }
588
589 return getImpl(C, AttrPairVec);
590}
591
592AttributeSet AttributeSet::get(LLVMContext &C,
593 ArrayRef<std::pair<unsigned,
594 AttributeSetNode*> > Attrs) {
595 // If there are no attributes then return a null AttributesList pointer.
596 if (Attrs.empty())
597 return AttributeSet();
598
599 return getImpl(C, Attrs);
600}
601
David Majnemercf63a792014-05-03 23:00:35 +0000602AttributeSet AttributeSet::get(LLVMContext &C, unsigned Index,
603 const AttrBuilder &B) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000604 if (!B.hasAttributes())
605 return AttributeSet();
Bill Wendlingf7134812013-01-29 01:02:03 +0000606
Bill Wendlingae89a0f2013-02-05 23:48:36 +0000607 // Add target-independent attributes.
Bill Wendlingf7134812013-01-29 01:02:03 +0000608 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
Benjamin Kramer45e7d532013-02-16 19:13:18 +0000609 for (Attribute::AttrKind Kind = Attribute::None;
Benjamin Kramer6f37dac2013-02-16 19:22:28 +0000610 Kind != Attribute::EndAttrKinds; Kind = Attribute::AttrKind(Kind + 1)) {
Benjamin Kramer45e7d532013-02-16 19:13:18 +0000611 if (!B.contains(Kind))
612 continue;
613
Bill Wendlingf7134812013-01-29 01:02:03 +0000614 if (Kind == Attribute::Alignment)
Bill Wendling211316c2013-04-18 20:17:28 +0000615 Attrs.push_back(std::make_pair(Index, Attribute::
Bill Wendlingf7134812013-01-29 01:02:03 +0000616 getWithAlignment(C, B.getAlignment())));
617 else if (Kind == Attribute::StackAlignment)
Bill Wendling211316c2013-04-18 20:17:28 +0000618 Attrs.push_back(std::make_pair(Index, Attribute::
Bill Wendlingf7134812013-01-29 01:02:03 +0000619 getWithStackAlignment(C, B.getStackAlignment())));
620 else
Bill Wendling211316c2013-04-18 20:17:28 +0000621 Attrs.push_back(std::make_pair(Index, Attribute::get(C, Kind)));
Bill Wendlingf7134812013-01-29 01:02:03 +0000622 }
623
Bill Wendlingae89a0f2013-02-05 23:48:36 +0000624 // Add target-dependent (string) attributes.
David Majnemercf63a792014-05-03 23:00:35 +0000625 for (const AttrBuilder::td_type &TDA : B.td_attrs())
626 Attrs.push_back(
627 std::make_pair(Index, Attribute::get(C, TDA.first, TDA.second)));
Bill Wendlingae89a0f2013-02-05 23:48:36 +0000628
Bill Wendlingf7134812013-01-29 01:02:03 +0000629 return get(C, Attrs);
Bill Wendlingd509a662013-01-29 00:34:06 +0000630}
631
Bill Wendling211316c2013-04-18 20:17:28 +0000632AttributeSet AttributeSet::get(LLVMContext &C, unsigned Index,
Bill Wendlingd509a662013-01-29 00:34:06 +0000633 ArrayRef<Attribute::AttrKind> Kind) {
634 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
635 for (ArrayRef<Attribute::AttrKind>::iterator I = Kind.begin(),
636 E = Kind.end(); I != E; ++I)
Bill Wendling211316c2013-04-18 20:17:28 +0000637 Attrs.push_back(std::make_pair(Index, Attribute::get(C, *I)));
Bill Wendlingd509a662013-01-29 00:34:06 +0000638 return get(C, Attrs);
639}
640
641AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<AttributeSet> Attrs) {
642 if (Attrs.empty()) return AttributeSet();
Peter Collingbournebd6c7452013-08-02 22:29:40 +0000643 if (Attrs.size() == 1) return Attrs[0];
Bill Wendlingd509a662013-01-29 00:34:06 +0000644
645 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrNodeVec;
Peter Collingbournebd6c7452013-08-02 22:29:40 +0000646 AttributeSetImpl *A0 = Attrs[0].pImpl;
647 if (A0)
648 AttrNodeVec.append(A0->getNode(0), A0->getNode(A0->getNumAttributes()));
649 // Copy all attributes from Attrs into AttrNodeVec while keeping AttrNodeVec
650 // ordered by index. Because we know that each list in Attrs is ordered by
651 // index we only need to merge each successive list in rather than doing a
652 // full sort.
653 for (unsigned I = 1, E = Attrs.size(); I != E; ++I) {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000654 AttributeSetImpl *AS = Attrs[I].pImpl;
655 if (!AS) continue;
Peter Collingbournebd6c7452013-08-02 22:29:40 +0000656 SmallVector<std::pair<unsigned, AttributeSetNode *>, 8>::iterator
657 ANVI = AttrNodeVec.begin(), ANVE;
658 for (const AttributeSetImpl::IndexAttrPair
659 *AI = AS->getNode(0),
660 *AE = AS->getNode(AS->getNumAttributes());
661 AI != AE; ++AI) {
662 ANVE = AttrNodeVec.end();
663 while (ANVI != ANVE && ANVI->first <= AI->first)
664 ++ANVI;
665 ANVI = AttrNodeVec.insert(ANVI, *AI) + 1;
666 }
Bill Wendlingd509a662013-01-29 00:34:06 +0000667 }
668
669 return getImpl(C, AttrNodeVec);
670}
671
Bill Wendling211316c2013-04-18 20:17:28 +0000672AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
Bill Wendlingd509a662013-01-29 00:34:06 +0000673 Attribute::AttrKind Attr) const {
Bill Wendling211316c2013-04-18 20:17:28 +0000674 if (hasAttribute(Index, Attr)) return *this;
675 return addAttributes(C, Index, AttributeSet::get(C, Index, Attr));
Bill Wendlingd509a662013-01-29 00:34:06 +0000676}
677
Bill Wendling211316c2013-04-18 20:17:28 +0000678AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
Reed Kotler795c7b42013-03-13 20:20:08 +0000679 StringRef Kind) const {
680 llvm::AttrBuilder B;
681 B.addAttribute(Kind);
Bill Wendling211316c2013-04-18 20:17:28 +0000682 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
Reed Kotler795c7b42013-03-13 20:20:08 +0000683}
684
Bill Wendling3b2f6102013-07-25 18:34:24 +0000685AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
686 StringRef Kind, StringRef Value) const {
687 llvm::AttrBuilder B;
688 B.addAttribute(Kind, Value);
689 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
690}
691
Bill Wendling211316c2013-04-18 20:17:28 +0000692AttributeSet AttributeSet::addAttributes(LLVMContext &C, unsigned Index,
Bill Wendlingd509a662013-01-29 00:34:06 +0000693 AttributeSet Attrs) const {
694 if (!pImpl) return Attrs;
695 if (!Attrs.pImpl) return *this;
696
697#ifndef NDEBUG
698 // FIXME it is not obvious how this should work for alignment. For now, say
699 // we can't change a known alignment.
Bill Wendling211316c2013-04-18 20:17:28 +0000700 unsigned OldAlign = getParamAlignment(Index);
701 unsigned NewAlign = Attrs.getParamAlignment(Index);
Bill Wendlingd509a662013-01-29 00:34:06 +0000702 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
703 "Attempt to change alignment!");
704#endif
705
706 // Add the attribute slots before the one we're trying to add.
707 SmallVector<AttributeSet, 4> AttrSet;
708 uint64_t NumAttrs = pImpl->getNumAttributes();
709 AttributeSet AS;
710 uint64_t LastIndex = 0;
711 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
Bill Wendling211316c2013-04-18 20:17:28 +0000712 if (getSlotIndex(I) >= Index) {
713 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
Bill Wendlingd509a662013-01-29 00:34:06 +0000714 break;
715 }
716 LastIndex = I + 1;
717 AttrSet.push_back(getSlotAttributes(I));
718 }
719
720 // Now add the attribute into the correct slot. There may already be an
721 // AttributeSet there.
Bill Wendling211316c2013-04-18 20:17:28 +0000722 AttrBuilder B(AS, Index);
Bill Wendlingd509a662013-01-29 00:34:06 +0000723
724 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
Bill Wendling211316c2013-04-18 20:17:28 +0000725 if (Attrs.getSlotIndex(I) == Index) {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000726 for (AttributeSetImpl::iterator II = Attrs.pImpl->begin(I),
Bill Wendlingd509a662013-01-29 00:34:06 +0000727 IE = Attrs.pImpl->end(I); II != IE; ++II)
Bill Wendling23804da2013-01-31 23:38:01 +0000728 B.addAttribute(*II);
Bill Wendlingd509a662013-01-29 00:34:06 +0000729 break;
730 }
731
Bill Wendling211316c2013-04-18 20:17:28 +0000732 AttrSet.push_back(AttributeSet::get(C, Index, B));
Bill Wendlingd509a662013-01-29 00:34:06 +0000733
734 // Add the remaining attribute slots.
735 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
736 AttrSet.push_back(getSlotAttributes(I));
737
738 return get(C, AttrSet);
739}
740
Bill Wendling211316c2013-04-18 20:17:28 +0000741AttributeSet AttributeSet::removeAttribute(LLVMContext &C, unsigned Index,
Bill Wendlingd509a662013-01-29 00:34:06 +0000742 Attribute::AttrKind Attr) const {
Bill Wendling211316c2013-04-18 20:17:28 +0000743 if (!hasAttribute(Index, Attr)) return *this;
744 return removeAttributes(C, Index, AttributeSet::get(C, Index, Attr));
Bill Wendlingd509a662013-01-29 00:34:06 +0000745}
746
Bill Wendling211316c2013-04-18 20:17:28 +0000747AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Index,
Bill Wendlingd509a662013-01-29 00:34:06 +0000748 AttributeSet Attrs) const {
749 if (!pImpl) return AttributeSet();
750 if (!Attrs.pImpl) return *this;
751
752#ifndef NDEBUG
753 // FIXME it is not obvious how this should work for alignment.
754 // For now, say we can't pass in alignment, which no current use does.
Bill Wendling211316c2013-04-18 20:17:28 +0000755 assert(!Attrs.hasAttribute(Index, Attribute::Alignment) &&
Bill Wendlingd509a662013-01-29 00:34:06 +0000756 "Attempt to change alignment!");
757#endif
758
759 // Add the attribute slots before the one we're trying to add.
760 SmallVector<AttributeSet, 4> AttrSet;
761 uint64_t NumAttrs = pImpl->getNumAttributes();
762 AttributeSet AS;
763 uint64_t LastIndex = 0;
764 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
Bill Wendling211316c2013-04-18 20:17:28 +0000765 if (getSlotIndex(I) >= Index) {
766 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
Bill Wendlingd509a662013-01-29 00:34:06 +0000767 break;
768 }
769 LastIndex = I + 1;
770 AttrSet.push_back(getSlotAttributes(I));
771 }
772
Bill Wendlingd2196752013-01-30 23:07:40 +0000773 // Now remove the attribute from the correct slot. There may already be an
Bill Wendlingd509a662013-01-29 00:34:06 +0000774 // AttributeSet there.
Bill Wendling211316c2013-04-18 20:17:28 +0000775 AttrBuilder B(AS, Index);
Bill Wendlingd509a662013-01-29 00:34:06 +0000776
777 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
Bill Wendling211316c2013-04-18 20:17:28 +0000778 if (Attrs.getSlotIndex(I) == Index) {
779 B.removeAttributes(Attrs.pImpl->getSlotAttributes(I), Index);
Bill Wendlingd509a662013-01-29 00:34:06 +0000780 break;
781 }
782
Bill Wendling211316c2013-04-18 20:17:28 +0000783 AttrSet.push_back(AttributeSet::get(C, Index, B));
Bill Wendlingd509a662013-01-29 00:34:06 +0000784
785 // Add the remaining attribute slots.
786 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
787 AttrSet.push_back(getSlotAttributes(I));
788
789 return get(C, AttrSet);
790}
791
792//===----------------------------------------------------------------------===//
793// AttributeSet Accessor Methods
794//===----------------------------------------------------------------------===//
795
Bill Wendling5d020a32013-02-10 05:00:40 +0000796LLVMContext &AttributeSet::getContext() const {
797 return pImpl->getContext();
798}
799
Bill Wendling211316c2013-04-18 20:17:28 +0000800AttributeSet AttributeSet::getParamAttributes(unsigned Index) const {
801 return pImpl && hasAttributes(Index) ?
Bill Wendlingd509a662013-01-29 00:34:06 +0000802 AttributeSet::get(pImpl->getContext(),
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000803 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendling211316c2013-04-18 20:17:28 +0000804 std::make_pair(Index, getAttributes(Index)))) :
Bill Wendlingd509a662013-01-29 00:34:06 +0000805 AttributeSet();
806}
807
808AttributeSet AttributeSet::getRetAttributes() const {
809 return pImpl && hasAttributes(ReturnIndex) ?
810 AttributeSet::get(pImpl->getContext(),
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000811 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendlingd509a662013-01-29 00:34:06 +0000812 std::make_pair(ReturnIndex,
813 getAttributes(ReturnIndex)))) :
814 AttributeSet();
815}
816
817AttributeSet AttributeSet::getFnAttributes() const {
818 return pImpl && hasAttributes(FunctionIndex) ?
819 AttributeSet::get(pImpl->getContext(),
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000820 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendlingd509a662013-01-29 00:34:06 +0000821 std::make_pair(FunctionIndex,
822 getAttributes(FunctionIndex)))) :
823 AttributeSet();
824}
825
826bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000827 AttributeSetNode *ASN = getAttributes(Index);
828 return ASN ? ASN->hasAttribute(Kind) : false;
Bill Wendlingd509a662013-01-29 00:34:06 +0000829}
830
Bill Wendlingbce7b972013-02-13 08:42:21 +0000831bool AttributeSet::hasAttribute(unsigned Index, StringRef Kind) const {
832 AttributeSetNode *ASN = getAttributes(Index);
833 return ASN ? ASN->hasAttribute(Kind) : false;
834}
835
Bill Wendlingd509a662013-01-29 00:34:06 +0000836bool AttributeSet::hasAttributes(unsigned Index) const {
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000837 AttributeSetNode *ASN = getAttributes(Index);
838 return ASN ? ASN->hasAttributes() : false;
Bill Wendlingd509a662013-01-29 00:34:06 +0000839}
840
841/// \brief Return true if the specified attribute is set for at least one
842/// parameter or for the return value.
843bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr) const {
Craig Topperc6207612014-04-09 06:08:46 +0000844 if (!pImpl) return false;
Bill Wendlingd509a662013-01-29 00:34:06 +0000845
846 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
Benjamin Kramer741146b2013-07-11 12:13:16 +0000847 for (AttributeSetImpl::iterator II = pImpl->begin(I),
Bill Wendlingd509a662013-01-29 00:34:06 +0000848 IE = pImpl->end(I); II != IE; ++II)
NAKAMURA Takumib0944392013-01-31 03:47:28 +0000849 if (II->hasAttribute(Attr))
Bill Wendlingd509a662013-01-29 00:34:06 +0000850 return true;
851
852 return false;
853}
854
Bill Wendlingbce7b972013-02-13 08:42:21 +0000855Attribute AttributeSet::getAttribute(unsigned Index,
856 Attribute::AttrKind Kind) const {
857 AttributeSetNode *ASN = getAttributes(Index);
858 return ASN ? ASN->getAttribute(Kind) : Attribute();
859}
860
861Attribute AttributeSet::getAttribute(unsigned Index,
862 StringRef Kind) const {
863 AttributeSetNode *ASN = getAttributes(Index);
864 return ASN ? ASN->getAttribute(Kind) : Attribute();
865}
866
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000867unsigned AttributeSet::getParamAlignment(unsigned Index) const {
868 AttributeSetNode *ASN = getAttributes(Index);
869 return ASN ? ASN->getAlignment() : 0;
Bill Wendlingd509a662013-01-29 00:34:06 +0000870}
871
872unsigned AttributeSet::getStackAlignment(unsigned Index) const {
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000873 AttributeSetNode *ASN = getAttributes(Index);
874 return ASN ? ASN->getStackAlignment() : 0;
Bill Wendlingd509a662013-01-29 00:34:06 +0000875}
876
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +0000877std::string AttributeSet::getAsString(unsigned Index,
Bill Wendling829b4782013-02-11 08:43:33 +0000878 bool InAttrGrp) const {
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000879 AttributeSetNode *ASN = getAttributes(Index);
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +0000880 return ASN ? ASN->getAsString(InAttrGrp) : std::string("");
Bill Wendlingd509a662013-01-29 00:34:06 +0000881}
882
883/// \brief The attributes for the specified index are returned.
Bill Wendling211316c2013-04-18 20:17:28 +0000884AttributeSetNode *AttributeSet::getAttributes(unsigned Index) const {
Craig Topperc6207612014-04-09 06:08:46 +0000885 if (!pImpl) return nullptr;
Bill Wendlingd509a662013-01-29 00:34:06 +0000886
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000887 // Loop through to find the attribute node we want.
888 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
Bill Wendling211316c2013-04-18 20:17:28 +0000889 if (pImpl->getSlotIndex(I) == Index)
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000890 return pImpl->getSlotNode(I);
Bill Wendlingd509a662013-01-29 00:34:06 +0000891
Craig Topperc6207612014-04-09 06:08:46 +0000892 return nullptr;
Bill Wendlingd509a662013-01-29 00:34:06 +0000893}
894
Bill Wendling211316c2013-04-18 20:17:28 +0000895AttributeSet::iterator AttributeSet::begin(unsigned Slot) const {
Bill Wendlinga9174862013-01-31 23:53:05 +0000896 if (!pImpl)
897 return ArrayRef<Attribute>().begin();
Bill Wendling211316c2013-04-18 20:17:28 +0000898 return pImpl->begin(Slot);
Bill Wendlinga9174862013-01-31 23:53:05 +0000899}
900
Bill Wendling211316c2013-04-18 20:17:28 +0000901AttributeSet::iterator AttributeSet::end(unsigned Slot) const {
Bill Wendlinga9174862013-01-31 23:53:05 +0000902 if (!pImpl)
903 return ArrayRef<Attribute>().end();
Bill Wendling211316c2013-04-18 20:17:28 +0000904 return pImpl->end(Slot);
Bill Wendlinga9174862013-01-31 23:53:05 +0000905}
906
Bill Wendlingd509a662013-01-29 00:34:06 +0000907//===----------------------------------------------------------------------===//
908// AttributeSet Introspection Methods
909//===----------------------------------------------------------------------===//
910
911/// \brief Return the number of slots used in this attribute list. This is the
912/// number of arguments that have an attribute set on them (including the
913/// function itself).
914unsigned AttributeSet::getNumSlots() const {
915 return pImpl ? pImpl->getNumAttributes() : 0;
916}
917
Rafael Espindoladd275302013-04-30 16:53:38 +0000918unsigned AttributeSet::getSlotIndex(unsigned Slot) const {
Bill Wendlingd509a662013-01-29 00:34:06 +0000919 assert(pImpl && Slot < pImpl->getNumAttributes() &&
920 "Slot # out of range!");
921 return pImpl->getSlotIndex(Slot);
922}
923
924AttributeSet AttributeSet::getSlotAttributes(unsigned Slot) const {
925 assert(pImpl && Slot < pImpl->getNumAttributes() &&
926 "Slot # out of range!");
927 return pImpl->getSlotAttributes(Slot);
928}
929
930uint64_t AttributeSet::Raw(unsigned Index) const {
931 // FIXME: Remove this.
932 return pImpl ? pImpl->Raw(Index) : 0;
933}
934
935void AttributeSet::dump() const {
936 dbgs() << "PAL[\n";
937
938 for (unsigned i = 0, e = getNumSlots(); i < e; ++i) {
939 uint64_t Index = getSlotIndex(i);
940 dbgs() << " { ";
941 if (Index == ~0U)
942 dbgs() << "~0U";
943 else
944 dbgs() << Index;
945 dbgs() << " => " << getAsString(Index) << " }\n";
946 }
947
948 dbgs() << "]\n";
949}
950
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000951//===----------------------------------------------------------------------===//
Bill Wendlingcd330342013-01-04 23:27:34 +0000952// AttrBuilder Method Implementations
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000953//===----------------------------------------------------------------------===//
954
Bill Wendling211316c2013-04-18 20:17:28 +0000955AttrBuilder::AttrBuilder(AttributeSet AS, unsigned Index)
Benjamin Kramer45e7d532013-02-16 19:13:18 +0000956 : Attrs(0), Alignment(0), StackAlignment(0) {
Bill Wendling56b0b2a2013-01-27 21:23:46 +0000957 AttributeSetImpl *pImpl = AS.pImpl;
Bill Wendling096f5442013-01-07 08:24:35 +0000958 if (!pImpl) return;
959
Bill Wendling9eb689c2013-01-28 00:21:34 +0000960 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I) {
Bill Wendling211316c2013-04-18 20:17:28 +0000961 if (pImpl->getSlotIndex(I) != Index) continue;
Bill Wendling096f5442013-01-07 08:24:35 +0000962
Benjamin Kramer741146b2013-07-11 12:13:16 +0000963 for (AttributeSetImpl::iterator II = pImpl->begin(I),
Bill Wendling9eb689c2013-01-28 00:21:34 +0000964 IE = pImpl->end(I); II != IE; ++II)
Bill Wendling23804da2013-01-31 23:38:01 +0000965 addAttribute(*II);
Bill Wendling9eb689c2013-01-28 00:21:34 +0000966
967 break;
968 }
Bill Wendling096f5442013-01-07 08:24:35 +0000969}
970
Bill Wendlingcd330342013-01-04 23:27:34 +0000971void AttrBuilder::clear() {
Benjamin Kramereaf706b2013-02-18 12:09:51 +0000972 Attrs.reset();
Bill Wendlingcd330342013-01-04 23:27:34 +0000973 Alignment = StackAlignment = 0;
974}
975
976AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
Benjamin Kramereaf706b2013-02-18 12:09:51 +0000977 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
Bill Wendling1c7cc8a2013-01-31 23:16:25 +0000978 assert(Val != Attribute::Alignment && Val != Attribute::StackAlignment &&
979 "Adding alignment attribute without adding alignment value!");
Benjamin Kramereaf706b2013-02-18 12:09:51 +0000980 Attrs[Val] = true;
Bill Wendling7c04e042012-10-09 19:01:18 +0000981 return *this;
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000982}
983
Bill Wendling23804da2013-01-31 23:38:01 +0000984AttrBuilder &AttrBuilder::addAttribute(Attribute Attr) {
Bill Wendling0a437302013-02-10 10:13:23 +0000985 if (Attr.isStringAttribute()) {
986 addAttribute(Attr.getKindAsString(), Attr.getValueAsString());
987 return *this;
988 }
989
Bill Wendling3f12ac22013-02-05 22:37:24 +0000990 Attribute::AttrKind Kind = Attr.getKindAsEnum();
Benjamin Kramereaf706b2013-02-18 12:09:51 +0000991 Attrs[Kind] = true;
Bill Wendling1aa9d9e2013-01-28 05:23:28 +0000992
Bill Wendling3f12ac22013-02-05 22:37:24 +0000993 if (Kind == Attribute::Alignment)
Bill Wendling1aa9d9e2013-01-28 05:23:28 +0000994 Alignment = Attr.getAlignment();
Bill Wendling3f12ac22013-02-05 22:37:24 +0000995 else if (Kind == Attribute::StackAlignment)
Bill Wendling1aa9d9e2013-01-28 05:23:28 +0000996 StackAlignment = Attr.getStackAlignment();
997 return *this;
998}
999
Bill Wendlingb9c5b1a2013-02-05 08:09:32 +00001000AttrBuilder &AttrBuilder::addAttribute(StringRef A, StringRef V) {
1001 TargetDepAttrs[A] = V;
1002 return *this;
1003}
1004
Bill Wendling23804da2013-01-31 23:38:01 +00001005AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001006 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
1007 Attrs[Val] = false;
Bill Wendling23804da2013-01-31 23:38:01 +00001008
1009 if (Val == Attribute::Alignment)
1010 Alignment = 0;
1011 else if (Val == Attribute::StackAlignment)
1012 StackAlignment = 0;
1013
1014 return *this;
1015}
1016
Bill Wendlingd2196752013-01-30 23:07:40 +00001017AttrBuilder &AttrBuilder::removeAttributes(AttributeSet A, uint64_t Index) {
Bill Wendling211316c2013-04-18 20:17:28 +00001018 unsigned Slot = ~0U;
Bill Wendlingf1c94e32013-02-01 00:13:50 +00001019 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
1020 if (A.getSlotIndex(I) == Index) {
Bill Wendling211316c2013-04-18 20:17:28 +00001021 Slot = I;
Bill Wendlingf1c94e32013-02-01 00:13:50 +00001022 break;
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001023 }
Bill Wendlingf1c94e32013-02-01 00:13:50 +00001024
Bill Wendling211316c2013-04-18 20:17:28 +00001025 assert(Slot != ~0U && "Couldn't find index in AttributeSet!");
Bill Wendlingf1c94e32013-02-01 00:13:50 +00001026
Bill Wendling211316c2013-04-18 20:17:28 +00001027 for (AttributeSet::iterator I = A.begin(Slot), E = A.end(Slot); I != E; ++I) {
Bill Wendling7cde51d2013-02-12 07:56:49 +00001028 Attribute Attr = *I;
1029 if (Attr.isEnumAttribute() || Attr.isAlignAttribute()) {
1030 Attribute::AttrKind Kind = I->getKindAsEnum();
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001031 Attrs[Kind] = false;
Bill Wendlingf1c94e32013-02-01 00:13:50 +00001032
Bill Wendling7cde51d2013-02-12 07:56:49 +00001033 if (Kind == Attribute::Alignment)
1034 Alignment = 0;
1035 else if (Kind == Attribute::StackAlignment)
1036 StackAlignment = 0;
1037 } else {
1038 assert(Attr.isStringAttribute() && "Invalid attribute type!");
1039 std::map<std::string, std::string>::iterator
1040 Iter = TargetDepAttrs.find(Attr.getKindAsString());
1041 if (Iter != TargetDepAttrs.end())
1042 TargetDepAttrs.erase(Iter);
1043 }
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001044 }
1045
1046 return *this;
1047}
1048
Bill Wendlingb9c5b1a2013-02-05 08:09:32 +00001049AttrBuilder &AttrBuilder::removeAttribute(StringRef A) {
1050 std::map<std::string, std::string>::iterator I = TargetDepAttrs.find(A);
1051 if (I != TargetDepAttrs.end())
1052 TargetDepAttrs.erase(I);
1053 return *this;
1054}
1055
Bill Wendling50d27842012-10-15 20:35:56 +00001056AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
Bill Wendlingabd5ba22012-10-14 03:58:29 +00001057 if (Align == 0) return *this;
Bill Wendlingcd330342013-01-04 23:27:34 +00001058
Bill Wendlingabf3feb2012-10-05 06:44:41 +00001059 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1060 assert(Align <= 0x40000000 && "Alignment too large.");
Bill Wendlingcd330342013-01-04 23:27:34 +00001061
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001062 Attrs[Attribute::Alignment] = true;
Bill Wendlingcd330342013-01-04 23:27:34 +00001063 Alignment = Align;
Bill Wendlingabd5ba22012-10-14 03:58:29 +00001064 return *this;
Chris Lattner3e13b8c2008-01-02 23:42:30 +00001065}
1066
Bill Wendlingcd330342013-01-04 23:27:34 +00001067AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
1068 // Default alignment, allow the target to define how to align it.
1069 if (Align == 0) return *this;
1070
1071 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1072 assert(Align <= 0x100 && "Alignment too large.");
1073
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001074 Attrs[Attribute::StackAlignment] = true;
Bill Wendlingcd330342013-01-04 23:27:34 +00001075 StackAlignment = Align;
1076 return *this;
1077}
1078
Bill Wendlinge2614922013-02-06 01:16:00 +00001079AttrBuilder &AttrBuilder::merge(const AttrBuilder &B) {
1080 // FIXME: What if both have alignments, but they don't match?!
1081 if (!Alignment)
1082 Alignment = B.Alignment;
1083
1084 if (!StackAlignment)
1085 StackAlignment = B.StackAlignment;
1086
Benjamin Kramer45e7d532013-02-16 19:13:18 +00001087 Attrs |= B.Attrs;
Bill Wendlinge2614922013-02-06 01:16:00 +00001088
1089 for (td_const_iterator I = B.TargetDepAttrs.begin(),
1090 E = B.TargetDepAttrs.end(); I != E; ++I)
1091 TargetDepAttrs[I->first] = I->second;
1092
1093 return *this;
1094}
1095
Bill Wendling4b001442013-02-06 01:33:42 +00001096bool AttrBuilder::contains(StringRef A) const {
1097 return TargetDepAttrs.find(A) != TargetDepAttrs.end();
1098}
1099
Bill Wendling50d27842012-10-15 20:35:56 +00001100bool AttrBuilder::hasAttributes() const {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001101 return !Attrs.none() || !TargetDepAttrs.empty();
Bill Wendlingc6daefa2012-10-08 23:27:46 +00001102}
Bill Wendling9ac69f92013-01-04 20:54:35 +00001103
Bill Wendlingd2196752013-01-30 23:07:40 +00001104bool AttrBuilder::hasAttributes(AttributeSet A, uint64_t Index) const {
Bill Wendling211316c2013-04-18 20:17:28 +00001105 unsigned Slot = ~0U;
Bill Wendling9ca01da2013-02-02 00:42:06 +00001106 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
1107 if (A.getSlotIndex(I) == Index) {
Bill Wendling211316c2013-04-18 20:17:28 +00001108 Slot = I;
Bill Wendling9ca01da2013-02-02 00:42:06 +00001109 break;
1110 }
1111
Bill Wendling211316c2013-04-18 20:17:28 +00001112 assert(Slot != ~0U && "Couldn't find the index!");
Bill Wendling9ca01da2013-02-02 00:42:06 +00001113
Bill Wendling211316c2013-04-18 20:17:28 +00001114 for (AttributeSet::iterator I = A.begin(Slot), E = A.end(Slot);
Bill Wendling7cde51d2013-02-12 07:56:49 +00001115 I != E; ++I) {
1116 Attribute Attr = *I;
1117 if (Attr.isEnumAttribute() || Attr.isAlignAttribute()) {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001118 if (Attrs[I->getKindAsEnum()])
Bill Wendling7cde51d2013-02-12 07:56:49 +00001119 return true;
1120 } else {
1121 assert(Attr.isStringAttribute() && "Invalid attribute kind!");
1122 return TargetDepAttrs.find(Attr.getKindAsString())!=TargetDepAttrs.end();
1123 }
1124 }
Bill Wendling9ca01da2013-02-02 00:42:06 +00001125
1126 return false;
Bill Wendling70f39172012-10-09 00:01:21 +00001127}
Bill Wendling9ac69f92013-01-04 20:54:35 +00001128
Bill Wendling50d27842012-10-15 20:35:56 +00001129bool AttrBuilder::hasAlignmentAttr() const {
Bill Wendlingcd330342013-01-04 23:27:34 +00001130 return Alignment != 0;
Bill Wendlingc6daefa2012-10-08 23:27:46 +00001131}
1132
Bill Wendlingd509a662013-01-29 00:34:06 +00001133bool AttrBuilder::operator==(const AttrBuilder &B) {
Benjamin Kramer45e7d532013-02-16 19:13:18 +00001134 if (Attrs != B.Attrs)
1135 return false;
Bill Wendling4b001442013-02-06 01:33:42 +00001136
1137 for (td_const_iterator I = TargetDepAttrs.begin(),
1138 E = TargetDepAttrs.end(); I != E; ++I)
1139 if (B.TargetDepAttrs.find(I->first) == B.TargetDepAttrs.end())
1140 return false;
1141
1142 return Alignment == B.Alignment && StackAlignment == B.StackAlignment;
Bill Wendlingd509a662013-01-29 00:34:06 +00001143}
1144
1145AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) {
Bill Wendling56aeccc2013-02-04 23:32:23 +00001146 // FIXME: Remove this in 4.0.
Bill Wendling60011b82013-01-29 01:43:29 +00001147 if (!Val) return *this;
1148
Bill Wendlingd509a662013-01-29 00:34:06 +00001149 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
1150 I = Attribute::AttrKind(I + 1)) {
1151 if (uint64_t A = (Val & AttributeImpl::getAttrMask(I))) {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001152 Attrs[I] = true;
Bill Wendlingd509a662013-01-29 00:34:06 +00001153
1154 if (I == Attribute::Alignment)
1155 Alignment = 1ULL << ((A >> 16) - 1);
1156 else if (I == Attribute::StackAlignment)
1157 StackAlignment = 1ULL << ((A >> 26)-1);
1158 }
1159 }
1160
1161 return *this;
1162}
1163
Bill Wendling57625a42013-01-25 23:09:36 +00001164//===----------------------------------------------------------------------===//
1165// AttributeFuncs Function Defintions
1166//===----------------------------------------------------------------------===//
1167
Bill Wendlingc79cdff2013-02-01 01:04:27 +00001168/// \brief Which attributes cannot be applied to a type.
Bill Wendlingd2196752013-01-30 23:07:40 +00001169AttributeSet AttributeFuncs::typeIncompatible(Type *Ty, uint64_t Index) {
Bill Wendling57625a42013-01-25 23:09:36 +00001170 AttrBuilder Incompatible;
1171
1172 if (!Ty->isIntegerTy())
1173 // Attribute that only apply to integers.
1174 Incompatible.addAttribute(Attribute::SExt)
1175 .addAttribute(Attribute::ZExt);
1176
1177 if (!Ty->isPointerTy())
1178 // Attribute that only apply to pointers.
1179 Incompatible.addAttribute(Attribute::ByVal)
1180 .addAttribute(Attribute::Nest)
1181 .addAttribute(Attribute::NoAlias)
1182 .addAttribute(Attribute::NoCapture)
Nick Lewyckyd52b1522014-05-20 01:23:40 +00001183 .addAttribute(Attribute::NonNull)
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001184 .addAttribute(Attribute::ReadNone)
1185 .addAttribute(Attribute::ReadOnly)
Reid Klecknera534a382013-12-19 02:14:12 +00001186 .addAttribute(Attribute::StructRet)
1187 .addAttribute(Attribute::InAlloca);
Bill Wendling57625a42013-01-25 23:09:36 +00001188
Bill Wendlingd2196752013-01-30 23:07:40 +00001189 return AttributeSet::get(Ty->getContext(), Index, Incompatible);
Bill Wendling57625a42013-01-25 23:09:36 +00001190}