blob: 0bbbd559780f1646099e6e379762747c9ce0d58c [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";
196 if (hasAttribute(Attribute::NoRedZone))
197 return "noredzone";
198 if (hasAttribute(Attribute::NoReturn))
199 return "noreturn";
200 if (hasAttribute(Attribute::NoUnwind))
201 return "nounwind";
Andrea Di Biagio377496b2013-08-23 11:53:55 +0000202 if (hasAttribute(Attribute::OptimizeNone))
203 return "optnone";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000204 if (hasAttribute(Attribute::OptimizeForSize))
205 return "optsize";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000206 if (hasAttribute(Attribute::ReadNone))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000207 return "readnone";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000208 if (hasAttribute(Attribute::ReadOnly))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000209 return "readonly";
Stephen Linb8bd2322013-04-20 05:14:40 +0000210 if (hasAttribute(Attribute::Returned))
211 return "returned";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000212 if (hasAttribute(Attribute::ReturnsTwice))
213 return "returns_twice";
214 if (hasAttribute(Attribute::SExt))
215 return "signext";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000216 if (hasAttribute(Attribute::StackProtect))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000217 return "ssp";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000218 if (hasAttribute(Attribute::StackProtectReq))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000219 return "sspreq";
Bill Wendlingd154e2832013-01-23 06:41:41 +0000220 if (hasAttribute(Attribute::StackProtectStrong))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000221 return "sspstrong";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000222 if (hasAttribute(Attribute::StructRet))
223 return "sret";
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000224 if (hasAttribute(Attribute::SanitizeThread))
225 return "sanitize_thread";
226 if (hasAttribute(Attribute::SanitizeMemory))
227 return "sanitize_memory";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000228 if (hasAttribute(Attribute::UWTable))
229 return "uwtable";
230 if (hasAttribute(Attribute::ZExt))
231 return "zeroext";
Diego Novilloc6399532013-05-24 12:26:52 +0000232 if (hasAttribute(Attribute::Cold))
233 return "cold";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000234
235 // FIXME: These should be output like this:
236 //
237 // align=4
238 // alignstack=8
239 //
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000240 if (hasAttribute(Attribute::Alignment)) {
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000241 std::string Result;
Bill Wendling829b4782013-02-11 08:43:33 +0000242 Result += "align";
243 Result += (InAttrGrp) ? "=" : " ";
Bill Wendling3f12ac22013-02-05 22:37:24 +0000244 Result += utostr(getValueAsInt());
245 return Result;
246 }
Bill Wendling829b4782013-02-11 08:43:33 +0000247
Bill Wendling3f12ac22013-02-05 22:37:24 +0000248 if (hasAttribute(Attribute::StackAlignment)) {
249 std::string Result;
Bill Wendling829b4782013-02-11 08:43:33 +0000250 Result += "alignstack";
251 if (InAttrGrp) {
252 Result += "=";
253 Result += utostr(getValueAsInt());
254 } else {
255 Result += "(";
256 Result += utostr(getValueAsInt());
257 Result += ")";
258 }
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000259 return Result;
Dale Johannesen11a555e2008-02-19 23:51:49 +0000260 }
Bill Wendling9c2eba92013-01-31 20:59:05 +0000261
262 // Convert target-dependent attributes to strings of the form:
263 //
264 // "kind"
265 // "kind" = "value"
Bill Wendling9c2eba92013-01-31 20:59:05 +0000266 //
Bill Wendling3f12ac22013-02-05 22:37:24 +0000267 if (isStringAttribute()) {
Bill Wendling9c2eba92013-01-31 20:59:05 +0000268 std::string Result;
Bill Wendling3f12ac22013-02-05 22:37:24 +0000269 Result += '\"' + getKindAsString().str() + '"';
Bill Wendling9c2eba92013-01-31 20:59:05 +0000270
Bill Wendling3f12ac22013-02-05 22:37:24 +0000271 StringRef Val = pImpl->getValueAsString();
272 if (Val.empty()) return Result;
Bill Wendling7a33f772013-02-01 22:32:30 +0000273
Bill Wendling829b4782013-02-11 08:43:33 +0000274 Result += "=\"" + Val.str() + '"';
Bill Wendlingc79cdff2013-02-01 01:04:27 +0000275 return Result;
Bill Wendling9c2eba92013-01-31 20:59:05 +0000276 }
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000277
278 llvm_unreachable("Unknown attribute");
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000279}
280
Bill Wendlingd509a662013-01-29 00:34:06 +0000281bool Attribute::operator<(Attribute A) const {
282 if (!pImpl && !A.pImpl) return false;
283 if (!pImpl) return true;
284 if (!A.pImpl) return false;
285 return *pImpl < *A.pImpl;
286}
287
Bill Wendlingd509a662013-01-29 00:34:06 +0000288//===----------------------------------------------------------------------===//
289// AttributeImpl Definition
290//===----------------------------------------------------------------------===//
291
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +0000292// Pin the vtabels to this file.
Alexey Samsonov49109a22013-11-18 09:31:53 +0000293AttributeImpl::~AttributeImpl() {}
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +0000294void EnumAttributeImpl::anchor() {}
295void AlignAttributeImpl::anchor() {}
296void StringAttributeImpl::anchor() {}
Alexey Samsonov49109a22013-11-18 09:31:53 +0000297
Bill Wendlingd509a662013-01-29 00:34:06 +0000298bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
Bill Wendling3f12ac22013-02-05 22:37:24 +0000299 if (isStringAttribute()) return false;
300 return getKindAsEnum() == A;
Bill Wendlingd509a662013-01-29 00:34:06 +0000301}
302
Bill Wendling3f12ac22013-02-05 22:37:24 +0000303bool AttributeImpl::hasAttribute(StringRef Kind) const {
304 if (!isStringAttribute()) return false;
305 return getKindAsString() == Kind;
Bill Wendlingd509a662013-01-29 00:34:06 +0000306}
307
Bill Wendling3f12ac22013-02-05 22:37:24 +0000308Attribute::AttrKind AttributeImpl::getKindAsEnum() const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000309 assert(isEnumAttribute() || isAlignAttribute());
310 return static_cast<const EnumAttributeImpl *>(this)->getEnumKind();
Bill Wendlingd509a662013-01-29 00:34:06 +0000311}
312
Bill Wendling3f12ac22013-02-05 22:37:24 +0000313uint64_t AttributeImpl::getValueAsInt() const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000314 assert(isAlignAttribute());
315 return static_cast<const AlignAttributeImpl *>(this)->getAlignment();
Bill Wendlingd509a662013-01-29 00:34:06 +0000316}
317
Bill Wendling3f12ac22013-02-05 22:37:24 +0000318StringRef AttributeImpl::getKindAsString() const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000319 assert(isStringAttribute());
320 return static_cast<const StringAttributeImpl *>(this)->getStringKind();
Bill Wendlingd509a662013-01-29 00:34:06 +0000321}
322
Bill Wendling3f12ac22013-02-05 22:37:24 +0000323StringRef AttributeImpl::getValueAsString() const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000324 assert(isStringAttribute());
325 return static_cast<const StringAttributeImpl *>(this)->getStringValue();
Bill Wendlingd509a662013-01-29 00:34:06 +0000326}
327
328bool AttributeImpl::operator<(const AttributeImpl &AI) const {
Bill Wendlingc79cdff2013-02-01 01:04:27 +0000329 // This sorts the attributes with Attribute::AttrKinds coming first (sorted
330 // relative to their enum value) and then strings.
Bill Wendling26b95752013-02-15 05:25:26 +0000331 if (isEnumAttribute()) {
332 if (AI.isEnumAttribute()) return getKindAsEnum() < AI.getKindAsEnum();
333 if (AI.isAlignAttribute()) return true;
334 if (AI.isStringAttribute()) return true;
335 }
Bill Wendlingc79cdff2013-02-01 01:04:27 +0000336
Bill Wendling3f12ac22013-02-05 22:37:24 +0000337 if (isAlignAttribute()) {
Bill Wendling26b95752013-02-15 05:25:26 +0000338 if (AI.isEnumAttribute()) return false;
339 if (AI.isAlignAttribute()) return getValueAsInt() < AI.getValueAsInt();
340 if (AI.isStringAttribute()) return true;
Bill Wendling3f12ac22013-02-05 22:37:24 +0000341 }
Bill Wendlingd509a662013-01-29 00:34:06 +0000342
Bill Wendling26b95752013-02-15 05:25:26 +0000343 if (AI.isEnumAttribute()) return false;
344 if (AI.isAlignAttribute()) return false;
345 if (getKindAsString() == AI.getKindAsString())
346 return getValueAsString() < AI.getValueAsString();
347 return getKindAsString() < AI.getKindAsString();
Bill Wendlingd509a662013-01-29 00:34:06 +0000348}
349
Bill Wendlingd509a662013-01-29 00:34:06 +0000350uint64_t AttributeImpl::getAttrMask(Attribute::AttrKind Val) {
351 // FIXME: Remove this.
352 switch (Val) {
353 case Attribute::EndAttrKinds:
Bill Wendlingd509a662013-01-29 00:34:06 +0000354 llvm_unreachable("Synthetic enumerators which should never get here");
355
356 case Attribute::None: return 0;
357 case Attribute::ZExt: return 1 << 0;
358 case Attribute::SExt: return 1 << 1;
359 case Attribute::NoReturn: return 1 << 2;
360 case Attribute::InReg: return 1 << 3;
361 case Attribute::StructRet: return 1 << 4;
362 case Attribute::NoUnwind: return 1 << 5;
363 case Attribute::NoAlias: return 1 << 6;
364 case Attribute::ByVal: return 1 << 7;
365 case Attribute::Nest: return 1 << 8;
366 case Attribute::ReadNone: return 1 << 9;
367 case Attribute::ReadOnly: return 1 << 10;
368 case Attribute::NoInline: return 1 << 11;
369 case Attribute::AlwaysInline: return 1 << 12;
370 case Attribute::OptimizeForSize: return 1 << 13;
371 case Attribute::StackProtect: return 1 << 14;
372 case Attribute::StackProtectReq: return 1 << 15;
373 case Attribute::Alignment: return 31 << 16;
374 case Attribute::NoCapture: return 1 << 21;
375 case Attribute::NoRedZone: return 1 << 22;
376 case Attribute::NoImplicitFloat: return 1 << 23;
377 case Attribute::Naked: return 1 << 24;
378 case Attribute::InlineHint: return 1 << 25;
379 case Attribute::StackAlignment: return 7 << 26;
380 case Attribute::ReturnsTwice: return 1 << 29;
381 case Attribute::UWTable: return 1 << 30;
382 case Attribute::NonLazyBind: return 1U << 31;
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000383 case Attribute::SanitizeAddress: return 1ULL << 32;
Bill Wendlingd509a662013-01-29 00:34:06 +0000384 case Attribute::MinSize: return 1ULL << 33;
385 case Attribute::NoDuplicate: return 1ULL << 34;
386 case Attribute::StackProtectStrong: return 1ULL << 35;
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000387 case Attribute::SanitizeThread: return 1ULL << 36;
388 case Attribute::SanitizeMemory: return 1ULL << 37;
Bill Wendling7bfdfe72013-02-22 00:40:12 +0000389 case Attribute::NoBuiltin: return 1ULL << 38;
Stephen Linb8bd2322013-04-20 05:14:40 +0000390 case Attribute::Returned: return 1ULL << 39;
Diego Novilloc6399532013-05-24 12:26:52 +0000391 case Attribute::Cold: return 1ULL << 40;
Michael Gottesman41748d72013-06-27 00:25:01 +0000392 case Attribute::Builtin: return 1ULL << 41;
Andrea Di Biagio377496b2013-08-23 11:53:55 +0000393 case Attribute::OptimizeNone: return 1ULL << 42;
Reid Klecknera534a382013-12-19 02:14:12 +0000394 case Attribute::InAlloca: return 1ULL << 43;
Bill Wendling25342e12013-02-22 00:50:09 +0000395 }
396 llvm_unreachable("Unsupported attribute type");
Bill Wendlingd509a662013-01-29 00:34:06 +0000397}
398
399//===----------------------------------------------------------------------===//
400// AttributeSetNode Definition
401//===----------------------------------------------------------------------===//
402
403AttributeSetNode *AttributeSetNode::get(LLVMContext &C,
404 ArrayRef<Attribute> Attrs) {
405 if (Attrs.empty())
Craig Topperc6207612014-04-09 06:08:46 +0000406 return nullptr;
Bill Wendlingd509a662013-01-29 00:34:06 +0000407
408 // Otherwise, build a key to look up the existing attributes.
409 LLVMContextImpl *pImpl = C.pImpl;
410 FoldingSetNodeID ID;
411
412 SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end());
Bill Wendling82c2ee62013-02-13 09:26:26 +0000413 array_pod_sort(SortedAttrs.begin(), SortedAttrs.end());
Bill Wendlingd509a662013-01-29 00:34:06 +0000414
415 for (SmallVectorImpl<Attribute>::iterator I = SortedAttrs.begin(),
416 E = SortedAttrs.end(); I != E; ++I)
417 I->Profile(ID);
418
419 void *InsertPoint;
420 AttributeSetNode *PA =
421 pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint);
422
423 // If we didn't find any existing attributes of the same shape then create a
424 // new one and insert it.
425 if (!PA) {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000426 // Coallocate entries after the AttributeSetNode itself.
427 void *Mem = ::operator new(sizeof(AttributeSetNode) +
428 sizeof(Attribute) * SortedAttrs.size());
429 PA = new (Mem) AttributeSetNode(SortedAttrs);
Bill Wendlingd509a662013-01-29 00:34:06 +0000430 pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint);
431 }
432
433 // Return the AttributesListNode that we found or created.
434 return PA;
435}
436
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000437bool AttributeSetNode::hasAttribute(Attribute::AttrKind Kind) const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000438 for (iterator I = begin(), E = end(); I != E; ++I)
NAKAMURA Takumib0944392013-01-31 03:47:28 +0000439 if (I->hasAttribute(Kind))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000440 return true;
441 return false;
442}
443
Bill Wendlingbce7b972013-02-13 08:42:21 +0000444bool AttributeSetNode::hasAttribute(StringRef Kind) const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000445 for (iterator I = begin(), E = end(); I != E; ++I)
Bill Wendlingbce7b972013-02-13 08:42:21 +0000446 if (I->hasAttribute(Kind))
447 return true;
448 return false;
449}
450
451Attribute AttributeSetNode::getAttribute(Attribute::AttrKind Kind) const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000452 for (iterator I = begin(), E = end(); I != E; ++I)
Bill Wendlingbce7b972013-02-13 08:42:21 +0000453 if (I->hasAttribute(Kind))
454 return *I;
455 return Attribute();
456}
457
458Attribute AttributeSetNode::getAttribute(StringRef Kind) const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000459 for (iterator I = begin(), E = end(); I != E; ++I)
Bill Wendlingbce7b972013-02-13 08:42:21 +0000460 if (I->hasAttribute(Kind))
461 return *I;
462 return Attribute();
463}
464
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000465unsigned AttributeSetNode::getAlignment() const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000466 for (iterator I = begin(), E = end(); I != E; ++I)
NAKAMURA Takumib0944392013-01-31 03:47:28 +0000467 if (I->hasAttribute(Attribute::Alignment))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000468 return I->getAlignment();
469 return 0;
470}
471
472unsigned AttributeSetNode::getStackAlignment() const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000473 for (iterator I = begin(), E = end(); I != E; ++I)
NAKAMURA Takumib0944392013-01-31 03:47:28 +0000474 if (I->hasAttribute(Attribute::StackAlignment))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000475 return I->getStackAlignment();
476 return 0;
477}
478
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +0000479std::string AttributeSetNode::getAsString(bool InAttrGrp) const {
Benjamin Kramer0baf8f42013-04-19 11:43:21 +0000480 std::string Str;
Benjamin Kramer741146b2013-07-11 12:13:16 +0000481 for (iterator I = begin(), E = end(); I != E; ++I) {
482 if (I != begin())
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +0000483 Str += ' ';
484 Str += I->getAsString(InAttrGrp);
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000485 }
486 return Str;
487}
488
Bill Wendlingd509a662013-01-29 00:34:06 +0000489//===----------------------------------------------------------------------===//
490// AttributeSetImpl Definition
491//===----------------------------------------------------------------------===//
492
Rafael Espindoladd275302013-04-30 16:53:38 +0000493uint64_t AttributeSetImpl::Raw(unsigned Index) const {
Bill Wendlingd509a662013-01-29 00:34:06 +0000494 for (unsigned I = 0, E = getNumAttributes(); I != E; ++I) {
495 if (getSlotIndex(I) != Index) continue;
Benjamin Kramer741146b2013-07-11 12:13:16 +0000496 const AttributeSetNode *ASN = getSlotNode(I);
Bill Wendling91226182013-02-02 00:52:44 +0000497 uint64_t Mask = 0;
Bill Wendlingd509a662013-01-29 00:34:06 +0000498
Benjamin Kramer741146b2013-07-11 12:13:16 +0000499 for (AttributeSetNode::iterator II = ASN->begin(),
Bill Wendling91226182013-02-02 00:52:44 +0000500 IE = ASN->end(); II != IE; ++II) {
501 Attribute Attr = *II;
Bill Wendlingd746e402013-02-10 23:18:05 +0000502
503 // This cannot handle string attributes.
504 if (Attr.isStringAttribute()) continue;
505
Bill Wendling3f12ac22013-02-05 22:37:24 +0000506 Attribute::AttrKind Kind = Attr.getKindAsEnum();
Bill Wendling91226182013-02-02 00:52:44 +0000507
Bill Wendling3f12ac22013-02-05 22:37:24 +0000508 if (Kind == Attribute::Alignment)
Bill Wendling91226182013-02-02 00:52:44 +0000509 Mask |= (Log2_32(ASN->getAlignment()) + 1) << 16;
Bill Wendling3f12ac22013-02-05 22:37:24 +0000510 else if (Kind == Attribute::StackAlignment)
Bill Wendling91226182013-02-02 00:52:44 +0000511 Mask |= (Log2_32(ASN->getStackAlignment()) + 1) << 26;
512 else
Bill Wendling3f12ac22013-02-05 22:37:24 +0000513 Mask |= AttributeImpl::getAttrMask(Kind);
Bill Wendling91226182013-02-02 00:52:44 +0000514 }
515
516 return Mask;
Bill Wendlingd509a662013-01-29 00:34:06 +0000517 }
518
519 return 0;
520}
521
Peter Collingbourneabca2ec2013-08-02 22:34:30 +0000522void AttributeSetImpl::dump() const {
523 AttributeSet(const_cast<AttributeSetImpl *>(this)).dump();
524}
525
Bill Wendlingd509a662013-01-29 00:34:06 +0000526//===----------------------------------------------------------------------===//
527// AttributeSet Construction and Mutation Methods
528//===----------------------------------------------------------------------===//
529
Bill Wendling60011b82013-01-29 01:43:29 +0000530AttributeSet
531AttributeSet::getImpl(LLVMContext &C,
532 ArrayRef<std::pair<unsigned, AttributeSetNode*> > Attrs) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000533 LLVMContextImpl *pImpl = C.pImpl;
534 FoldingSetNodeID ID;
535 AttributeSetImpl::Profile(ID, Attrs);
536
537 void *InsertPoint;
538 AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
539
540 // If we didn't find any existing attributes of the same shape then
541 // create a new one and insert it.
542 if (!PA) {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000543 // Coallocate entries after the AttributeSetImpl itself.
544 void *Mem = ::operator new(sizeof(AttributeSetImpl) +
545 sizeof(std::pair<unsigned, AttributeSetNode *>) *
546 Attrs.size());
547 PA = new (Mem) AttributeSetImpl(C, Attrs);
Bill Wendlingd509a662013-01-29 00:34:06 +0000548 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
549 }
550
551 // Return the AttributesList that we found or created.
552 return AttributeSet(PA);
553}
554
555AttributeSet AttributeSet::get(LLVMContext &C,
556 ArrayRef<std::pair<unsigned, Attribute> > Attrs){
557 // If there are no attributes then return a null AttributesList pointer.
558 if (Attrs.empty())
559 return AttributeSet();
560
561#ifndef NDEBUG
562 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
563 assert((!i || Attrs[i-1].first <= Attrs[i].first) &&
564 "Misordered Attributes list!");
Bill Wendling3f12ac22013-02-05 22:37:24 +0000565 assert(!Attrs[i].second.hasAttribute(Attribute::None) &&
Bill Wendlingd509a662013-01-29 00:34:06 +0000566 "Pointless attribute!");
567 }
568#endif
569
570 // Create a vector if (unsigned, AttributeSetNode*) pairs from the attributes
571 // list.
572 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrPairVec;
573 for (ArrayRef<std::pair<unsigned, Attribute> >::iterator I = Attrs.begin(),
574 E = Attrs.end(); I != E; ) {
575 unsigned Index = I->first;
576 SmallVector<Attribute, 4> AttrVec;
NAKAMURA Takumif05d2f22013-01-29 15:18:16 +0000577 while (I != E && I->first == Index) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000578 AttrVec.push_back(I->second);
579 ++I;
580 }
581
582 AttrPairVec.push_back(std::make_pair(Index,
583 AttributeSetNode::get(C, AttrVec)));
584 }
585
586 return getImpl(C, AttrPairVec);
587}
588
589AttributeSet AttributeSet::get(LLVMContext &C,
590 ArrayRef<std::pair<unsigned,
591 AttributeSetNode*> > Attrs) {
592 // If there are no attributes then return a null AttributesList pointer.
593 if (Attrs.empty())
594 return AttributeSet();
595
596 return getImpl(C, Attrs);
597}
598
David Majnemercf63a792014-05-03 23:00:35 +0000599AttributeSet AttributeSet::get(LLVMContext &C, unsigned Index,
600 const AttrBuilder &B) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000601 if (!B.hasAttributes())
602 return AttributeSet();
Bill Wendlingf7134812013-01-29 01:02:03 +0000603
Bill Wendlingae89a0f2013-02-05 23:48:36 +0000604 // Add target-independent attributes.
Bill Wendlingf7134812013-01-29 01:02:03 +0000605 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
Benjamin Kramer45e7d532013-02-16 19:13:18 +0000606 for (Attribute::AttrKind Kind = Attribute::None;
Benjamin Kramer6f37dac2013-02-16 19:22:28 +0000607 Kind != Attribute::EndAttrKinds; Kind = Attribute::AttrKind(Kind + 1)) {
Benjamin Kramer45e7d532013-02-16 19:13:18 +0000608 if (!B.contains(Kind))
609 continue;
610
Bill Wendlingf7134812013-01-29 01:02:03 +0000611 if (Kind == Attribute::Alignment)
Bill Wendling211316c2013-04-18 20:17:28 +0000612 Attrs.push_back(std::make_pair(Index, Attribute::
Bill Wendlingf7134812013-01-29 01:02:03 +0000613 getWithAlignment(C, B.getAlignment())));
614 else if (Kind == Attribute::StackAlignment)
Bill Wendling211316c2013-04-18 20:17:28 +0000615 Attrs.push_back(std::make_pair(Index, Attribute::
Bill Wendlingf7134812013-01-29 01:02:03 +0000616 getWithStackAlignment(C, B.getStackAlignment())));
617 else
Bill Wendling211316c2013-04-18 20:17:28 +0000618 Attrs.push_back(std::make_pair(Index, Attribute::get(C, Kind)));
Bill Wendlingf7134812013-01-29 01:02:03 +0000619 }
620
Bill Wendlingae89a0f2013-02-05 23:48:36 +0000621 // Add target-dependent (string) attributes.
David Majnemercf63a792014-05-03 23:00:35 +0000622 for (const AttrBuilder::td_type &TDA : B.td_attrs())
623 Attrs.push_back(
624 std::make_pair(Index, Attribute::get(C, TDA.first, TDA.second)));
Bill Wendlingae89a0f2013-02-05 23:48:36 +0000625
Bill Wendlingf7134812013-01-29 01:02:03 +0000626 return get(C, Attrs);
Bill Wendlingd509a662013-01-29 00:34:06 +0000627}
628
Bill Wendling211316c2013-04-18 20:17:28 +0000629AttributeSet AttributeSet::get(LLVMContext &C, unsigned Index,
Bill Wendlingd509a662013-01-29 00:34:06 +0000630 ArrayRef<Attribute::AttrKind> Kind) {
631 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
632 for (ArrayRef<Attribute::AttrKind>::iterator I = Kind.begin(),
633 E = Kind.end(); I != E; ++I)
Bill Wendling211316c2013-04-18 20:17:28 +0000634 Attrs.push_back(std::make_pair(Index, Attribute::get(C, *I)));
Bill Wendlingd509a662013-01-29 00:34:06 +0000635 return get(C, Attrs);
636}
637
638AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<AttributeSet> Attrs) {
639 if (Attrs.empty()) return AttributeSet();
Peter Collingbournebd6c7452013-08-02 22:29:40 +0000640 if (Attrs.size() == 1) return Attrs[0];
Bill Wendlingd509a662013-01-29 00:34:06 +0000641
642 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrNodeVec;
Peter Collingbournebd6c7452013-08-02 22:29:40 +0000643 AttributeSetImpl *A0 = Attrs[0].pImpl;
644 if (A0)
645 AttrNodeVec.append(A0->getNode(0), A0->getNode(A0->getNumAttributes()));
646 // Copy all attributes from Attrs into AttrNodeVec while keeping AttrNodeVec
647 // ordered by index. Because we know that each list in Attrs is ordered by
648 // index we only need to merge each successive list in rather than doing a
649 // full sort.
650 for (unsigned I = 1, E = Attrs.size(); I != E; ++I) {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000651 AttributeSetImpl *AS = Attrs[I].pImpl;
652 if (!AS) continue;
Peter Collingbournebd6c7452013-08-02 22:29:40 +0000653 SmallVector<std::pair<unsigned, AttributeSetNode *>, 8>::iterator
654 ANVI = AttrNodeVec.begin(), ANVE;
655 for (const AttributeSetImpl::IndexAttrPair
656 *AI = AS->getNode(0),
657 *AE = AS->getNode(AS->getNumAttributes());
658 AI != AE; ++AI) {
659 ANVE = AttrNodeVec.end();
660 while (ANVI != ANVE && ANVI->first <= AI->first)
661 ++ANVI;
662 ANVI = AttrNodeVec.insert(ANVI, *AI) + 1;
663 }
Bill Wendlingd509a662013-01-29 00:34:06 +0000664 }
665
666 return getImpl(C, AttrNodeVec);
667}
668
Bill Wendling211316c2013-04-18 20:17:28 +0000669AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
Bill Wendlingd509a662013-01-29 00:34:06 +0000670 Attribute::AttrKind Attr) const {
Bill Wendling211316c2013-04-18 20:17:28 +0000671 if (hasAttribute(Index, Attr)) return *this;
672 return addAttributes(C, Index, AttributeSet::get(C, Index, Attr));
Bill Wendlingd509a662013-01-29 00:34:06 +0000673}
674
Bill Wendling211316c2013-04-18 20:17:28 +0000675AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
Reed Kotler795c7b42013-03-13 20:20:08 +0000676 StringRef Kind) const {
677 llvm::AttrBuilder B;
678 B.addAttribute(Kind);
Bill Wendling211316c2013-04-18 20:17:28 +0000679 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
Reed Kotler795c7b42013-03-13 20:20:08 +0000680}
681
Bill Wendling3b2f6102013-07-25 18:34:24 +0000682AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
683 StringRef Kind, StringRef Value) const {
684 llvm::AttrBuilder B;
685 B.addAttribute(Kind, Value);
686 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
687}
688
Bill Wendling211316c2013-04-18 20:17:28 +0000689AttributeSet AttributeSet::addAttributes(LLVMContext &C, unsigned Index,
Bill Wendlingd509a662013-01-29 00:34:06 +0000690 AttributeSet Attrs) const {
691 if (!pImpl) return Attrs;
692 if (!Attrs.pImpl) return *this;
693
694#ifndef NDEBUG
695 // FIXME it is not obvious how this should work for alignment. For now, say
696 // we can't change a known alignment.
Bill Wendling211316c2013-04-18 20:17:28 +0000697 unsigned OldAlign = getParamAlignment(Index);
698 unsigned NewAlign = Attrs.getParamAlignment(Index);
Bill Wendlingd509a662013-01-29 00:34:06 +0000699 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
700 "Attempt to change alignment!");
701#endif
702
703 // Add the attribute slots before the one we're trying to add.
704 SmallVector<AttributeSet, 4> AttrSet;
705 uint64_t NumAttrs = pImpl->getNumAttributes();
706 AttributeSet AS;
707 uint64_t LastIndex = 0;
708 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
Bill Wendling211316c2013-04-18 20:17:28 +0000709 if (getSlotIndex(I) >= Index) {
710 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
Bill Wendlingd509a662013-01-29 00:34:06 +0000711 break;
712 }
713 LastIndex = I + 1;
714 AttrSet.push_back(getSlotAttributes(I));
715 }
716
717 // Now add the attribute into the correct slot. There may already be an
718 // AttributeSet there.
Bill Wendling211316c2013-04-18 20:17:28 +0000719 AttrBuilder B(AS, Index);
Bill Wendlingd509a662013-01-29 00:34:06 +0000720
721 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
Bill Wendling211316c2013-04-18 20:17:28 +0000722 if (Attrs.getSlotIndex(I) == Index) {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000723 for (AttributeSetImpl::iterator II = Attrs.pImpl->begin(I),
Bill Wendlingd509a662013-01-29 00:34:06 +0000724 IE = Attrs.pImpl->end(I); II != IE; ++II)
Bill Wendling23804da2013-01-31 23:38:01 +0000725 B.addAttribute(*II);
Bill Wendlingd509a662013-01-29 00:34:06 +0000726 break;
727 }
728
Bill Wendling211316c2013-04-18 20:17:28 +0000729 AttrSet.push_back(AttributeSet::get(C, Index, B));
Bill Wendlingd509a662013-01-29 00:34:06 +0000730
731 // Add the remaining attribute slots.
732 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
733 AttrSet.push_back(getSlotAttributes(I));
734
735 return get(C, AttrSet);
736}
737
Bill Wendling211316c2013-04-18 20:17:28 +0000738AttributeSet AttributeSet::removeAttribute(LLVMContext &C, unsigned Index,
Bill Wendlingd509a662013-01-29 00:34:06 +0000739 Attribute::AttrKind Attr) const {
Bill Wendling211316c2013-04-18 20:17:28 +0000740 if (!hasAttribute(Index, Attr)) return *this;
741 return removeAttributes(C, Index, AttributeSet::get(C, Index, Attr));
Bill Wendlingd509a662013-01-29 00:34:06 +0000742}
743
Bill Wendling211316c2013-04-18 20:17:28 +0000744AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Index,
Bill Wendlingd509a662013-01-29 00:34:06 +0000745 AttributeSet Attrs) const {
746 if (!pImpl) return AttributeSet();
747 if (!Attrs.pImpl) return *this;
748
749#ifndef NDEBUG
750 // FIXME it is not obvious how this should work for alignment.
751 // For now, say we can't pass in alignment, which no current use does.
Bill Wendling211316c2013-04-18 20:17:28 +0000752 assert(!Attrs.hasAttribute(Index, Attribute::Alignment) &&
Bill Wendlingd509a662013-01-29 00:34:06 +0000753 "Attempt to change alignment!");
754#endif
755
756 // Add the attribute slots before the one we're trying to add.
757 SmallVector<AttributeSet, 4> AttrSet;
758 uint64_t NumAttrs = pImpl->getNumAttributes();
759 AttributeSet AS;
760 uint64_t LastIndex = 0;
761 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
Bill Wendling211316c2013-04-18 20:17:28 +0000762 if (getSlotIndex(I) >= Index) {
763 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
Bill Wendlingd509a662013-01-29 00:34:06 +0000764 break;
765 }
766 LastIndex = I + 1;
767 AttrSet.push_back(getSlotAttributes(I));
768 }
769
Bill Wendlingd2196752013-01-30 23:07:40 +0000770 // Now remove the attribute from the correct slot. There may already be an
Bill Wendlingd509a662013-01-29 00:34:06 +0000771 // AttributeSet there.
Bill Wendling211316c2013-04-18 20:17:28 +0000772 AttrBuilder B(AS, Index);
Bill Wendlingd509a662013-01-29 00:34:06 +0000773
774 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
Bill Wendling211316c2013-04-18 20:17:28 +0000775 if (Attrs.getSlotIndex(I) == Index) {
776 B.removeAttributes(Attrs.pImpl->getSlotAttributes(I), Index);
Bill Wendlingd509a662013-01-29 00:34:06 +0000777 break;
778 }
779
Bill Wendling211316c2013-04-18 20:17:28 +0000780 AttrSet.push_back(AttributeSet::get(C, Index, B));
Bill Wendlingd509a662013-01-29 00:34:06 +0000781
782 // Add the remaining attribute slots.
783 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
784 AttrSet.push_back(getSlotAttributes(I));
785
786 return get(C, AttrSet);
787}
788
789//===----------------------------------------------------------------------===//
790// AttributeSet Accessor Methods
791//===----------------------------------------------------------------------===//
792
Bill Wendling5d020a32013-02-10 05:00:40 +0000793LLVMContext &AttributeSet::getContext() const {
794 return pImpl->getContext();
795}
796
Bill Wendling211316c2013-04-18 20:17:28 +0000797AttributeSet AttributeSet::getParamAttributes(unsigned Index) const {
798 return pImpl && hasAttributes(Index) ?
Bill Wendlingd509a662013-01-29 00:34:06 +0000799 AttributeSet::get(pImpl->getContext(),
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000800 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendling211316c2013-04-18 20:17:28 +0000801 std::make_pair(Index, getAttributes(Index)))) :
Bill Wendlingd509a662013-01-29 00:34:06 +0000802 AttributeSet();
803}
804
805AttributeSet AttributeSet::getRetAttributes() const {
806 return pImpl && hasAttributes(ReturnIndex) ?
807 AttributeSet::get(pImpl->getContext(),
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000808 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendlingd509a662013-01-29 00:34:06 +0000809 std::make_pair(ReturnIndex,
810 getAttributes(ReturnIndex)))) :
811 AttributeSet();
812}
813
814AttributeSet AttributeSet::getFnAttributes() const {
815 return pImpl && hasAttributes(FunctionIndex) ?
816 AttributeSet::get(pImpl->getContext(),
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000817 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendlingd509a662013-01-29 00:34:06 +0000818 std::make_pair(FunctionIndex,
819 getAttributes(FunctionIndex)))) :
820 AttributeSet();
821}
822
823bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000824 AttributeSetNode *ASN = getAttributes(Index);
825 return ASN ? ASN->hasAttribute(Kind) : false;
Bill Wendlingd509a662013-01-29 00:34:06 +0000826}
827
Bill Wendlingbce7b972013-02-13 08:42:21 +0000828bool AttributeSet::hasAttribute(unsigned Index, StringRef Kind) const {
829 AttributeSetNode *ASN = getAttributes(Index);
830 return ASN ? ASN->hasAttribute(Kind) : false;
831}
832
Bill Wendlingd509a662013-01-29 00:34:06 +0000833bool AttributeSet::hasAttributes(unsigned Index) const {
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000834 AttributeSetNode *ASN = getAttributes(Index);
835 return ASN ? ASN->hasAttributes() : false;
Bill Wendlingd509a662013-01-29 00:34:06 +0000836}
837
838/// \brief Return true if the specified attribute is set for at least one
839/// parameter or for the return value.
840bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr) const {
Craig Topperc6207612014-04-09 06:08:46 +0000841 if (!pImpl) return false;
Bill Wendlingd509a662013-01-29 00:34:06 +0000842
843 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
Benjamin Kramer741146b2013-07-11 12:13:16 +0000844 for (AttributeSetImpl::iterator II = pImpl->begin(I),
Bill Wendlingd509a662013-01-29 00:34:06 +0000845 IE = pImpl->end(I); II != IE; ++II)
NAKAMURA Takumib0944392013-01-31 03:47:28 +0000846 if (II->hasAttribute(Attr))
Bill Wendlingd509a662013-01-29 00:34:06 +0000847 return true;
848
849 return false;
850}
851
Bill Wendlingbce7b972013-02-13 08:42:21 +0000852Attribute AttributeSet::getAttribute(unsigned Index,
853 Attribute::AttrKind Kind) const {
854 AttributeSetNode *ASN = getAttributes(Index);
855 return ASN ? ASN->getAttribute(Kind) : Attribute();
856}
857
858Attribute AttributeSet::getAttribute(unsigned Index,
859 StringRef Kind) const {
860 AttributeSetNode *ASN = getAttributes(Index);
861 return ASN ? ASN->getAttribute(Kind) : Attribute();
862}
863
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000864unsigned AttributeSet::getParamAlignment(unsigned Index) const {
865 AttributeSetNode *ASN = getAttributes(Index);
866 return ASN ? ASN->getAlignment() : 0;
Bill Wendlingd509a662013-01-29 00:34:06 +0000867}
868
869unsigned AttributeSet::getStackAlignment(unsigned Index) const {
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000870 AttributeSetNode *ASN = getAttributes(Index);
871 return ASN ? ASN->getStackAlignment() : 0;
Bill Wendlingd509a662013-01-29 00:34:06 +0000872}
873
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +0000874std::string AttributeSet::getAsString(unsigned Index,
Bill Wendling829b4782013-02-11 08:43:33 +0000875 bool InAttrGrp) const {
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000876 AttributeSetNode *ASN = getAttributes(Index);
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +0000877 return ASN ? ASN->getAsString(InAttrGrp) : std::string("");
Bill Wendlingd509a662013-01-29 00:34:06 +0000878}
879
880/// \brief The attributes for the specified index are returned.
Bill Wendling211316c2013-04-18 20:17:28 +0000881AttributeSetNode *AttributeSet::getAttributes(unsigned Index) const {
Craig Topperc6207612014-04-09 06:08:46 +0000882 if (!pImpl) return nullptr;
Bill Wendlingd509a662013-01-29 00:34:06 +0000883
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000884 // Loop through to find the attribute node we want.
885 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
Bill Wendling211316c2013-04-18 20:17:28 +0000886 if (pImpl->getSlotIndex(I) == Index)
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000887 return pImpl->getSlotNode(I);
Bill Wendlingd509a662013-01-29 00:34:06 +0000888
Craig Topperc6207612014-04-09 06:08:46 +0000889 return nullptr;
Bill Wendlingd509a662013-01-29 00:34:06 +0000890}
891
Bill Wendling211316c2013-04-18 20:17:28 +0000892AttributeSet::iterator AttributeSet::begin(unsigned Slot) const {
Bill Wendlinga9174862013-01-31 23:53:05 +0000893 if (!pImpl)
894 return ArrayRef<Attribute>().begin();
Bill Wendling211316c2013-04-18 20:17:28 +0000895 return pImpl->begin(Slot);
Bill Wendlinga9174862013-01-31 23:53:05 +0000896}
897
Bill Wendling211316c2013-04-18 20:17:28 +0000898AttributeSet::iterator AttributeSet::end(unsigned Slot) const {
Bill Wendlinga9174862013-01-31 23:53:05 +0000899 if (!pImpl)
900 return ArrayRef<Attribute>().end();
Bill Wendling211316c2013-04-18 20:17:28 +0000901 return pImpl->end(Slot);
Bill Wendlinga9174862013-01-31 23:53:05 +0000902}
903
Bill Wendlingd509a662013-01-29 00:34:06 +0000904//===----------------------------------------------------------------------===//
905// AttributeSet Introspection Methods
906//===----------------------------------------------------------------------===//
907
908/// \brief Return the number of slots used in this attribute list. This is the
909/// number of arguments that have an attribute set on them (including the
910/// function itself).
911unsigned AttributeSet::getNumSlots() const {
912 return pImpl ? pImpl->getNumAttributes() : 0;
913}
914
Rafael Espindoladd275302013-04-30 16:53:38 +0000915unsigned AttributeSet::getSlotIndex(unsigned Slot) const {
Bill Wendlingd509a662013-01-29 00:34:06 +0000916 assert(pImpl && Slot < pImpl->getNumAttributes() &&
917 "Slot # out of range!");
918 return pImpl->getSlotIndex(Slot);
919}
920
921AttributeSet AttributeSet::getSlotAttributes(unsigned Slot) const {
922 assert(pImpl && Slot < pImpl->getNumAttributes() &&
923 "Slot # out of range!");
924 return pImpl->getSlotAttributes(Slot);
925}
926
927uint64_t AttributeSet::Raw(unsigned Index) const {
928 // FIXME: Remove this.
929 return pImpl ? pImpl->Raw(Index) : 0;
930}
931
932void AttributeSet::dump() const {
933 dbgs() << "PAL[\n";
934
935 for (unsigned i = 0, e = getNumSlots(); i < e; ++i) {
936 uint64_t Index = getSlotIndex(i);
937 dbgs() << " { ";
938 if (Index == ~0U)
939 dbgs() << "~0U";
940 else
941 dbgs() << Index;
942 dbgs() << " => " << getAsString(Index) << " }\n";
943 }
944
945 dbgs() << "]\n";
946}
947
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000948//===----------------------------------------------------------------------===//
Bill Wendlingcd330342013-01-04 23:27:34 +0000949// AttrBuilder Method Implementations
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000950//===----------------------------------------------------------------------===//
951
Bill Wendling211316c2013-04-18 20:17:28 +0000952AttrBuilder::AttrBuilder(AttributeSet AS, unsigned Index)
Benjamin Kramer45e7d532013-02-16 19:13:18 +0000953 : Attrs(0), Alignment(0), StackAlignment(0) {
Bill Wendling56b0b2a2013-01-27 21:23:46 +0000954 AttributeSetImpl *pImpl = AS.pImpl;
Bill Wendling096f5442013-01-07 08:24:35 +0000955 if (!pImpl) return;
956
Bill Wendling9eb689c2013-01-28 00:21:34 +0000957 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I) {
Bill Wendling211316c2013-04-18 20:17:28 +0000958 if (pImpl->getSlotIndex(I) != Index) continue;
Bill Wendling096f5442013-01-07 08:24:35 +0000959
Benjamin Kramer741146b2013-07-11 12:13:16 +0000960 for (AttributeSetImpl::iterator II = pImpl->begin(I),
Bill Wendling9eb689c2013-01-28 00:21:34 +0000961 IE = pImpl->end(I); II != IE; ++II)
Bill Wendling23804da2013-01-31 23:38:01 +0000962 addAttribute(*II);
Bill Wendling9eb689c2013-01-28 00:21:34 +0000963
964 break;
965 }
Bill Wendling096f5442013-01-07 08:24:35 +0000966}
967
Bill Wendlingcd330342013-01-04 23:27:34 +0000968void AttrBuilder::clear() {
Benjamin Kramereaf706b2013-02-18 12:09:51 +0000969 Attrs.reset();
Bill Wendlingcd330342013-01-04 23:27:34 +0000970 Alignment = StackAlignment = 0;
971}
972
973AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
Benjamin Kramereaf706b2013-02-18 12:09:51 +0000974 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
Bill Wendling1c7cc8a2013-01-31 23:16:25 +0000975 assert(Val != Attribute::Alignment && Val != Attribute::StackAlignment &&
976 "Adding alignment attribute without adding alignment value!");
Benjamin Kramereaf706b2013-02-18 12:09:51 +0000977 Attrs[Val] = true;
Bill Wendling7c04e042012-10-09 19:01:18 +0000978 return *this;
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000979}
980
Bill Wendling23804da2013-01-31 23:38:01 +0000981AttrBuilder &AttrBuilder::addAttribute(Attribute Attr) {
Bill Wendling0a437302013-02-10 10:13:23 +0000982 if (Attr.isStringAttribute()) {
983 addAttribute(Attr.getKindAsString(), Attr.getValueAsString());
984 return *this;
985 }
986
Bill Wendling3f12ac22013-02-05 22:37:24 +0000987 Attribute::AttrKind Kind = Attr.getKindAsEnum();
Benjamin Kramereaf706b2013-02-18 12:09:51 +0000988 Attrs[Kind] = true;
Bill Wendling1aa9d9e2013-01-28 05:23:28 +0000989
Bill Wendling3f12ac22013-02-05 22:37:24 +0000990 if (Kind == Attribute::Alignment)
Bill Wendling1aa9d9e2013-01-28 05:23:28 +0000991 Alignment = Attr.getAlignment();
Bill Wendling3f12ac22013-02-05 22:37:24 +0000992 else if (Kind == Attribute::StackAlignment)
Bill Wendling1aa9d9e2013-01-28 05:23:28 +0000993 StackAlignment = Attr.getStackAlignment();
994 return *this;
995}
996
Bill Wendlingb9c5b1a2013-02-05 08:09:32 +0000997AttrBuilder &AttrBuilder::addAttribute(StringRef A, StringRef V) {
998 TargetDepAttrs[A] = V;
999 return *this;
1000}
1001
Bill Wendling23804da2013-01-31 23:38:01 +00001002AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001003 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
1004 Attrs[Val] = false;
Bill Wendling23804da2013-01-31 23:38:01 +00001005
1006 if (Val == Attribute::Alignment)
1007 Alignment = 0;
1008 else if (Val == Attribute::StackAlignment)
1009 StackAlignment = 0;
1010
1011 return *this;
1012}
1013
Bill Wendlingd2196752013-01-30 23:07:40 +00001014AttrBuilder &AttrBuilder::removeAttributes(AttributeSet A, uint64_t Index) {
Bill Wendling211316c2013-04-18 20:17:28 +00001015 unsigned Slot = ~0U;
Bill Wendlingf1c94e32013-02-01 00:13:50 +00001016 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
1017 if (A.getSlotIndex(I) == Index) {
Bill Wendling211316c2013-04-18 20:17:28 +00001018 Slot = I;
Bill Wendlingf1c94e32013-02-01 00:13:50 +00001019 break;
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001020 }
Bill Wendlingf1c94e32013-02-01 00:13:50 +00001021
Bill Wendling211316c2013-04-18 20:17:28 +00001022 assert(Slot != ~0U && "Couldn't find index in AttributeSet!");
Bill Wendlingf1c94e32013-02-01 00:13:50 +00001023
Bill Wendling211316c2013-04-18 20:17:28 +00001024 for (AttributeSet::iterator I = A.begin(Slot), E = A.end(Slot); I != E; ++I) {
Bill Wendling7cde51d2013-02-12 07:56:49 +00001025 Attribute Attr = *I;
1026 if (Attr.isEnumAttribute() || Attr.isAlignAttribute()) {
1027 Attribute::AttrKind Kind = I->getKindAsEnum();
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001028 Attrs[Kind] = false;
Bill Wendlingf1c94e32013-02-01 00:13:50 +00001029
Bill Wendling7cde51d2013-02-12 07:56:49 +00001030 if (Kind == Attribute::Alignment)
1031 Alignment = 0;
1032 else if (Kind == Attribute::StackAlignment)
1033 StackAlignment = 0;
1034 } else {
1035 assert(Attr.isStringAttribute() && "Invalid attribute type!");
1036 std::map<std::string, std::string>::iterator
1037 Iter = TargetDepAttrs.find(Attr.getKindAsString());
1038 if (Iter != TargetDepAttrs.end())
1039 TargetDepAttrs.erase(Iter);
1040 }
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001041 }
1042
1043 return *this;
1044}
1045
Bill Wendlingb9c5b1a2013-02-05 08:09:32 +00001046AttrBuilder &AttrBuilder::removeAttribute(StringRef A) {
1047 std::map<std::string, std::string>::iterator I = TargetDepAttrs.find(A);
1048 if (I != TargetDepAttrs.end())
1049 TargetDepAttrs.erase(I);
1050 return *this;
1051}
1052
Bill Wendling50d27842012-10-15 20:35:56 +00001053AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
Bill Wendlingabd5ba22012-10-14 03:58:29 +00001054 if (Align == 0) return *this;
Bill Wendlingcd330342013-01-04 23:27:34 +00001055
Bill Wendlingabf3feb2012-10-05 06:44:41 +00001056 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1057 assert(Align <= 0x40000000 && "Alignment too large.");
Bill Wendlingcd330342013-01-04 23:27:34 +00001058
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001059 Attrs[Attribute::Alignment] = true;
Bill Wendlingcd330342013-01-04 23:27:34 +00001060 Alignment = Align;
Bill Wendlingabd5ba22012-10-14 03:58:29 +00001061 return *this;
Chris Lattner3e13b8c2008-01-02 23:42:30 +00001062}
1063
Bill Wendlingcd330342013-01-04 23:27:34 +00001064AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
1065 // Default alignment, allow the target to define how to align it.
1066 if (Align == 0) return *this;
1067
1068 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1069 assert(Align <= 0x100 && "Alignment too large.");
1070
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001071 Attrs[Attribute::StackAlignment] = true;
Bill Wendlingcd330342013-01-04 23:27:34 +00001072 StackAlignment = Align;
1073 return *this;
1074}
1075
Bill Wendlinge2614922013-02-06 01:16:00 +00001076AttrBuilder &AttrBuilder::merge(const AttrBuilder &B) {
1077 // FIXME: What if both have alignments, but they don't match?!
1078 if (!Alignment)
1079 Alignment = B.Alignment;
1080
1081 if (!StackAlignment)
1082 StackAlignment = B.StackAlignment;
1083
Benjamin Kramer45e7d532013-02-16 19:13:18 +00001084 Attrs |= B.Attrs;
Bill Wendlinge2614922013-02-06 01:16:00 +00001085
1086 for (td_const_iterator I = B.TargetDepAttrs.begin(),
1087 E = B.TargetDepAttrs.end(); I != E; ++I)
1088 TargetDepAttrs[I->first] = I->second;
1089
1090 return *this;
1091}
1092
Bill Wendling4b001442013-02-06 01:33:42 +00001093bool AttrBuilder::contains(StringRef A) const {
1094 return TargetDepAttrs.find(A) != TargetDepAttrs.end();
1095}
1096
Bill Wendling50d27842012-10-15 20:35:56 +00001097bool AttrBuilder::hasAttributes() const {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001098 return !Attrs.none() || !TargetDepAttrs.empty();
Bill Wendlingc6daefa2012-10-08 23:27:46 +00001099}
Bill Wendling9ac69f92013-01-04 20:54:35 +00001100
Bill Wendlingd2196752013-01-30 23:07:40 +00001101bool AttrBuilder::hasAttributes(AttributeSet A, uint64_t Index) const {
Bill Wendling211316c2013-04-18 20:17:28 +00001102 unsigned Slot = ~0U;
Bill Wendling9ca01da2013-02-02 00:42:06 +00001103 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
1104 if (A.getSlotIndex(I) == Index) {
Bill Wendling211316c2013-04-18 20:17:28 +00001105 Slot = I;
Bill Wendling9ca01da2013-02-02 00:42:06 +00001106 break;
1107 }
1108
Bill Wendling211316c2013-04-18 20:17:28 +00001109 assert(Slot != ~0U && "Couldn't find the index!");
Bill Wendling9ca01da2013-02-02 00:42:06 +00001110
Bill Wendling211316c2013-04-18 20:17:28 +00001111 for (AttributeSet::iterator I = A.begin(Slot), E = A.end(Slot);
Bill Wendling7cde51d2013-02-12 07:56:49 +00001112 I != E; ++I) {
1113 Attribute Attr = *I;
1114 if (Attr.isEnumAttribute() || Attr.isAlignAttribute()) {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001115 if (Attrs[I->getKindAsEnum()])
Bill Wendling7cde51d2013-02-12 07:56:49 +00001116 return true;
1117 } else {
1118 assert(Attr.isStringAttribute() && "Invalid attribute kind!");
1119 return TargetDepAttrs.find(Attr.getKindAsString())!=TargetDepAttrs.end();
1120 }
1121 }
Bill Wendling9ca01da2013-02-02 00:42:06 +00001122
1123 return false;
Bill Wendling70f39172012-10-09 00:01:21 +00001124}
Bill Wendling9ac69f92013-01-04 20:54:35 +00001125
Bill Wendling50d27842012-10-15 20:35:56 +00001126bool AttrBuilder::hasAlignmentAttr() const {
Bill Wendlingcd330342013-01-04 23:27:34 +00001127 return Alignment != 0;
Bill Wendlingc6daefa2012-10-08 23:27:46 +00001128}
1129
Bill Wendlingd509a662013-01-29 00:34:06 +00001130bool AttrBuilder::operator==(const AttrBuilder &B) {
Benjamin Kramer45e7d532013-02-16 19:13:18 +00001131 if (Attrs != B.Attrs)
1132 return false;
Bill Wendling4b001442013-02-06 01:33:42 +00001133
1134 for (td_const_iterator I = TargetDepAttrs.begin(),
1135 E = TargetDepAttrs.end(); I != E; ++I)
1136 if (B.TargetDepAttrs.find(I->first) == B.TargetDepAttrs.end())
1137 return false;
1138
1139 return Alignment == B.Alignment && StackAlignment == B.StackAlignment;
Bill Wendlingd509a662013-01-29 00:34:06 +00001140}
1141
1142AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) {
Bill Wendling56aeccc2013-02-04 23:32:23 +00001143 // FIXME: Remove this in 4.0.
Bill Wendling60011b82013-01-29 01:43:29 +00001144 if (!Val) return *this;
1145
Bill Wendlingd509a662013-01-29 00:34:06 +00001146 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
1147 I = Attribute::AttrKind(I + 1)) {
1148 if (uint64_t A = (Val & AttributeImpl::getAttrMask(I))) {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001149 Attrs[I] = true;
Bill Wendlingd509a662013-01-29 00:34:06 +00001150
1151 if (I == Attribute::Alignment)
1152 Alignment = 1ULL << ((A >> 16) - 1);
1153 else if (I == Attribute::StackAlignment)
1154 StackAlignment = 1ULL << ((A >> 26)-1);
1155 }
1156 }
1157
1158 return *this;
1159}
1160
Bill Wendling57625a42013-01-25 23:09:36 +00001161//===----------------------------------------------------------------------===//
1162// AttributeFuncs Function Defintions
1163//===----------------------------------------------------------------------===//
1164
Bill Wendlingc79cdff2013-02-01 01:04:27 +00001165/// \brief Which attributes cannot be applied to a type.
Bill Wendlingd2196752013-01-30 23:07:40 +00001166AttributeSet AttributeFuncs::typeIncompatible(Type *Ty, uint64_t Index) {
Bill Wendling57625a42013-01-25 23:09:36 +00001167 AttrBuilder Incompatible;
1168
1169 if (!Ty->isIntegerTy())
1170 // Attribute that only apply to integers.
1171 Incompatible.addAttribute(Attribute::SExt)
1172 .addAttribute(Attribute::ZExt);
1173
1174 if (!Ty->isPointerTy())
1175 // Attribute that only apply to pointers.
1176 Incompatible.addAttribute(Attribute::ByVal)
1177 .addAttribute(Attribute::Nest)
1178 .addAttribute(Attribute::NoAlias)
1179 .addAttribute(Attribute::NoCapture)
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001180 .addAttribute(Attribute::ReadNone)
1181 .addAttribute(Attribute::ReadOnly)
Reid Klecknera534a382013-12-19 02:14:12 +00001182 .addAttribute(Attribute::StructRet)
1183 .addAttribute(Attribute::InAlloca);
Bill Wendling57625a42013-01-25 23:09:36 +00001184
Bill Wendlingd2196752013-01-30 23:07:40 +00001185 return AttributeSet::get(Ty->getContext(), Index, Incompatible);
Bill Wendling57625a42013-01-25 23:09:36 +00001186}