blob: 04545ea919a4724253568c58a973b6ad6589f9c8 [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
Hal Finkele15442c2014-07-18 06:51:55 +000050 PA = new IntAttributeImpl(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
Hal Finkelb0407ba2014-07-18 15:51:28 +000091Attribute Attribute::getWithDereferenceableBytes(LLVMContext &Context,
92 uint64_t Bytes) {
93 assert(Bytes && "Bytes must be non-zero.");
94 return get(Context, Dereferenceable, Bytes);
95}
96
Bill Wendling7707c5a2013-01-29 00:48:16 +000097//===----------------------------------------------------------------------===//
98// Attribute Accessor Methods
99//===----------------------------------------------------------------------===//
100
Bill Wendling3f12ac22013-02-05 22:37:24 +0000101bool Attribute::isEnumAttribute() const {
102 return pImpl && pImpl->isEnumAttribute();
103}
104
Hal Finkele15442c2014-07-18 06:51:55 +0000105bool Attribute::isIntAttribute() const {
106 return pImpl && pImpl->isIntAttribute();
Bill Wendling3f12ac22013-02-05 22:37:24 +0000107}
108
109bool Attribute::isStringAttribute() const {
110 return pImpl && pImpl->isStringAttribute();
111}
112
113Attribute::AttrKind Attribute::getKindAsEnum() const {
Bill Wendling440e9d82013-07-25 00:34:29 +0000114 if (!pImpl) return None;
Hal Finkele15442c2014-07-18 06:51:55 +0000115 assert((isEnumAttribute() || isIntAttribute()) &&
Bill Wendling3f12ac22013-02-05 22:37:24 +0000116 "Invalid attribute type to get the kind as an enum!");
117 return pImpl ? pImpl->getKindAsEnum() : None;
118}
119
120uint64_t Attribute::getValueAsInt() const {
Bill Wendling440e9d82013-07-25 00:34:29 +0000121 if (!pImpl) return 0;
Hal Finkele15442c2014-07-18 06:51:55 +0000122 assert(isIntAttribute() &&
123 "Expected the attribute to be an integer attribute!");
Bill Wendling3f12ac22013-02-05 22:37:24 +0000124 return pImpl ? pImpl->getValueAsInt() : 0;
125}
126
127StringRef Attribute::getKindAsString() const {
Bill Wendling440e9d82013-07-25 00:34:29 +0000128 if (!pImpl) return StringRef();
Bill Wendling3f12ac22013-02-05 22:37:24 +0000129 assert(isStringAttribute() &&
130 "Invalid attribute type to get the kind as a string!");
131 return pImpl ? pImpl->getKindAsString() : StringRef();
132}
133
134StringRef Attribute::getValueAsString() const {
Bill Wendling440e9d82013-07-25 00:34:29 +0000135 if (!pImpl) return StringRef();
Bill Wendling3f12ac22013-02-05 22:37:24 +0000136 assert(isStringAttribute() &&
137 "Invalid attribute type to get the value as a string!");
138 return pImpl ? pImpl->getValueAsString() : StringRef();
139}
140
Bill Wendlingae89a0f2013-02-05 23:48:36 +0000141bool Attribute::hasAttribute(AttrKind Kind) const {
142 return (pImpl && pImpl->hasAttribute(Kind)) || (!pImpl && Kind == None);
143}
144
145bool Attribute::hasAttribute(StringRef Kind) const {
146 if (!isStringAttribute()) return false;
147 return pImpl && pImpl->hasAttribute(Kind);
Bill Wendling03eefb32013-01-29 20:45:34 +0000148}
149
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000150/// This returns the alignment field of an attribute as a byte alignment value.
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000151unsigned Attribute::getAlignment() const {
Bill Wendlingc79cdff2013-02-01 01:04:27 +0000152 assert(hasAttribute(Attribute::Alignment) &&
153 "Trying to get alignment from non-alignment attribute!");
Bill Wendling3f12ac22013-02-05 22:37:24 +0000154 return pImpl->getValueAsInt();
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000155}
156
157/// This returns the stack alignment field of an attribute as a byte alignment
158/// value.
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000159unsigned Attribute::getStackAlignment() const {
Bill Wendlingc79cdff2013-02-01 01:04:27 +0000160 assert(hasAttribute(Attribute::StackAlignment) &&
161 "Trying to get alignment from non-alignment attribute!");
Bill Wendling3f12ac22013-02-05 22:37:24 +0000162 return pImpl->getValueAsInt();
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000163}
164
Hal Finkelb0407ba2014-07-18 15:51:28 +0000165/// This returns the number of dereferenceable bytes.
166uint64_t Attribute::getDereferenceableBytes() const {
167 assert(hasAttribute(Attribute::Dereferenceable) &&
168 "Trying to get dereferenceable bytes from "
169 "non-dereferenceable attribute!");
170 return pImpl->getValueAsInt();
171}
172
Bill Wendling829b4782013-02-11 08:43:33 +0000173std::string Attribute::getAsString(bool InAttrGrp) const {
Bill Wendling9c2eba92013-01-31 20:59:05 +0000174 if (!pImpl) return "";
175
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000176 if (hasAttribute(Attribute::SanitizeAddress))
177 return "sanitize_address";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000178 if (hasAttribute(Attribute::AlwaysInline))
179 return "alwaysinline";
Michael Gottesman41748d72013-06-27 00:25:01 +0000180 if (hasAttribute(Attribute::Builtin))
181 return "builtin";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000182 if (hasAttribute(Attribute::ByVal))
183 return "byval";
Reid Klecknera534a382013-12-19 02:14:12 +0000184 if (hasAttribute(Attribute::InAlloca))
185 return "inalloca";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000186 if (hasAttribute(Attribute::InlineHint))
187 return "inlinehint";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000188 if (hasAttribute(Attribute::InReg))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000189 return "inreg";
Tom Roeder44cb65f2014-06-05 19:29:43 +0000190 if (hasAttribute(Attribute::JumpTable))
191 return "jumptable";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000192 if (hasAttribute(Attribute::MinSize))
193 return "minsize";
194 if (hasAttribute(Attribute::Naked))
195 return "naked";
196 if (hasAttribute(Attribute::Nest))
197 return "nest";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000198 if (hasAttribute(Attribute::NoAlias))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000199 return "noalias";
Bill Wendling09bd1f72013-02-22 00:12:35 +0000200 if (hasAttribute(Attribute::NoBuiltin))
201 return "nobuiltin";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000202 if (hasAttribute(Attribute::NoCapture))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000203 return "nocapture";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000204 if (hasAttribute(Attribute::NoDuplicate))
205 return "noduplicate";
206 if (hasAttribute(Attribute::NoImplicitFloat))
207 return "noimplicitfloat";
208 if (hasAttribute(Attribute::NoInline))
209 return "noinline";
210 if (hasAttribute(Attribute::NonLazyBind))
211 return "nonlazybind";
Nick Lewyckyd52b1522014-05-20 01:23:40 +0000212 if (hasAttribute(Attribute::NonNull))
213 return "nonnull";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000214 if (hasAttribute(Attribute::NoRedZone))
215 return "noredzone";
216 if (hasAttribute(Attribute::NoReturn))
217 return "noreturn";
218 if (hasAttribute(Attribute::NoUnwind))
219 return "nounwind";
Andrea Di Biagio377496b2013-08-23 11:53:55 +0000220 if (hasAttribute(Attribute::OptimizeNone))
221 return "optnone";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000222 if (hasAttribute(Attribute::OptimizeForSize))
223 return "optsize";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000224 if (hasAttribute(Attribute::ReadNone))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000225 return "readnone";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000226 if (hasAttribute(Attribute::ReadOnly))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000227 return "readonly";
Stephen Linb8bd2322013-04-20 05:14:40 +0000228 if (hasAttribute(Attribute::Returned))
229 return "returned";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000230 if (hasAttribute(Attribute::ReturnsTwice))
231 return "returns_twice";
232 if (hasAttribute(Attribute::SExt))
233 return "signext";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000234 if (hasAttribute(Attribute::StackProtect))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000235 return "ssp";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000236 if (hasAttribute(Attribute::StackProtectReq))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000237 return "sspreq";
Bill Wendlingd154e2832013-01-23 06:41:41 +0000238 if (hasAttribute(Attribute::StackProtectStrong))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000239 return "sspstrong";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000240 if (hasAttribute(Attribute::StructRet))
241 return "sret";
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000242 if (hasAttribute(Attribute::SanitizeThread))
243 return "sanitize_thread";
244 if (hasAttribute(Attribute::SanitizeMemory))
245 return "sanitize_memory";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000246 if (hasAttribute(Attribute::UWTable))
247 return "uwtable";
248 if (hasAttribute(Attribute::ZExt))
249 return "zeroext";
Diego Novilloc6399532013-05-24 12:26:52 +0000250 if (hasAttribute(Attribute::Cold))
251 return "cold";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000252
253 // FIXME: These should be output like this:
254 //
255 // align=4
256 // alignstack=8
257 //
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000258 if (hasAttribute(Attribute::Alignment)) {
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000259 std::string Result;
Bill Wendling829b4782013-02-11 08:43:33 +0000260 Result += "align";
261 Result += (InAttrGrp) ? "=" : " ";
Bill Wendling3f12ac22013-02-05 22:37:24 +0000262 Result += utostr(getValueAsInt());
263 return Result;
264 }
Bill Wendling829b4782013-02-11 08:43:33 +0000265
Bill Wendling3f12ac22013-02-05 22:37:24 +0000266 if (hasAttribute(Attribute::StackAlignment)) {
267 std::string Result;
Bill Wendling829b4782013-02-11 08:43:33 +0000268 Result += "alignstack";
269 if (InAttrGrp) {
270 Result += "=";
271 Result += utostr(getValueAsInt());
272 } else {
273 Result += "(";
274 Result += utostr(getValueAsInt());
275 Result += ")";
276 }
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000277 return Result;
Dale Johannesen11a555e2008-02-19 23:51:49 +0000278 }
Bill Wendling9c2eba92013-01-31 20:59:05 +0000279
Hal Finkelb0407ba2014-07-18 15:51:28 +0000280 if (hasAttribute(Attribute::Dereferenceable)) {
281 std::string Result;
282 Result += "dereferenceable";
283 if (InAttrGrp) {
284 Result += "=";
285 Result += utostr(getValueAsInt());
286 } else {
287 Result += "(";
288 Result += utostr(getValueAsInt());
289 Result += ")";
290 }
291 return Result;
292 }
293
Bill Wendling9c2eba92013-01-31 20:59:05 +0000294 // Convert target-dependent attributes to strings of the form:
295 //
296 // "kind"
297 // "kind" = "value"
Bill Wendling9c2eba92013-01-31 20:59:05 +0000298 //
Bill Wendling3f12ac22013-02-05 22:37:24 +0000299 if (isStringAttribute()) {
Bill Wendling9c2eba92013-01-31 20:59:05 +0000300 std::string Result;
Bill Wendling3f12ac22013-02-05 22:37:24 +0000301 Result += '\"' + getKindAsString().str() + '"';
Bill Wendling9c2eba92013-01-31 20:59:05 +0000302
Bill Wendling3f12ac22013-02-05 22:37:24 +0000303 StringRef Val = pImpl->getValueAsString();
304 if (Val.empty()) return Result;
Bill Wendling7a33f772013-02-01 22:32:30 +0000305
Bill Wendling829b4782013-02-11 08:43:33 +0000306 Result += "=\"" + Val.str() + '"';
Bill Wendlingc79cdff2013-02-01 01:04:27 +0000307 return Result;
Bill Wendling9c2eba92013-01-31 20:59:05 +0000308 }
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000309
310 llvm_unreachable("Unknown attribute");
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000311}
312
Bill Wendlingd509a662013-01-29 00:34:06 +0000313bool Attribute::operator<(Attribute A) const {
314 if (!pImpl && !A.pImpl) return false;
315 if (!pImpl) return true;
316 if (!A.pImpl) return false;
317 return *pImpl < *A.pImpl;
318}
319
Bill Wendlingd509a662013-01-29 00:34:06 +0000320//===----------------------------------------------------------------------===//
321// AttributeImpl Definition
322//===----------------------------------------------------------------------===//
323
Eric Christopher0eaa5412014-07-02 22:05:40 +0000324// Pin the vtables to this file.
Alexey Samsonov49109a22013-11-18 09:31:53 +0000325AttributeImpl::~AttributeImpl() {}
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +0000326void EnumAttributeImpl::anchor() {}
Hal Finkele15442c2014-07-18 06:51:55 +0000327void IntAttributeImpl::anchor() {}
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +0000328void StringAttributeImpl::anchor() {}
Alexey Samsonov49109a22013-11-18 09:31:53 +0000329
Bill Wendlingd509a662013-01-29 00:34:06 +0000330bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
Bill Wendling3f12ac22013-02-05 22:37:24 +0000331 if (isStringAttribute()) return false;
332 return getKindAsEnum() == A;
Bill Wendlingd509a662013-01-29 00:34:06 +0000333}
334
Bill Wendling3f12ac22013-02-05 22:37:24 +0000335bool AttributeImpl::hasAttribute(StringRef Kind) const {
336 if (!isStringAttribute()) return false;
337 return getKindAsString() == Kind;
Bill Wendlingd509a662013-01-29 00:34:06 +0000338}
339
Bill Wendling3f12ac22013-02-05 22:37:24 +0000340Attribute::AttrKind AttributeImpl::getKindAsEnum() const {
Hal Finkele15442c2014-07-18 06:51:55 +0000341 assert(isEnumAttribute() || isIntAttribute());
Benjamin Kramer741146b2013-07-11 12:13:16 +0000342 return static_cast<const EnumAttributeImpl *>(this)->getEnumKind();
Bill Wendlingd509a662013-01-29 00:34:06 +0000343}
344
Bill Wendling3f12ac22013-02-05 22:37:24 +0000345uint64_t AttributeImpl::getValueAsInt() const {
Hal Finkele15442c2014-07-18 06:51:55 +0000346 assert(isIntAttribute());
347 return static_cast<const IntAttributeImpl *>(this)->getValue();
Bill Wendlingd509a662013-01-29 00:34:06 +0000348}
349
Bill Wendling3f12ac22013-02-05 22:37:24 +0000350StringRef AttributeImpl::getKindAsString() const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000351 assert(isStringAttribute());
352 return static_cast<const StringAttributeImpl *>(this)->getStringKind();
Bill Wendlingd509a662013-01-29 00:34:06 +0000353}
354
Bill Wendling3f12ac22013-02-05 22:37:24 +0000355StringRef AttributeImpl::getValueAsString() const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000356 assert(isStringAttribute());
357 return static_cast<const StringAttributeImpl *>(this)->getStringValue();
Bill Wendlingd509a662013-01-29 00:34:06 +0000358}
359
360bool AttributeImpl::operator<(const AttributeImpl &AI) const {
Bill Wendlingc79cdff2013-02-01 01:04:27 +0000361 // This sorts the attributes with Attribute::AttrKinds coming first (sorted
362 // relative to their enum value) and then strings.
Bill Wendling26b95752013-02-15 05:25:26 +0000363 if (isEnumAttribute()) {
364 if (AI.isEnumAttribute()) return getKindAsEnum() < AI.getKindAsEnum();
Hal Finkele15442c2014-07-18 06:51:55 +0000365 if (AI.isIntAttribute()) return true;
Bill Wendling26b95752013-02-15 05:25:26 +0000366 if (AI.isStringAttribute()) return true;
367 }
Bill Wendlingc79cdff2013-02-01 01:04:27 +0000368
Hal Finkele15442c2014-07-18 06:51:55 +0000369 if (isIntAttribute()) {
Bill Wendling26b95752013-02-15 05:25:26 +0000370 if (AI.isEnumAttribute()) return false;
Hal Finkele15442c2014-07-18 06:51:55 +0000371 if (AI.isIntAttribute()) return getValueAsInt() < AI.getValueAsInt();
Bill Wendling26b95752013-02-15 05:25:26 +0000372 if (AI.isStringAttribute()) return true;
Bill Wendling3f12ac22013-02-05 22:37:24 +0000373 }
Bill Wendlingd509a662013-01-29 00:34:06 +0000374
Bill Wendling26b95752013-02-15 05:25:26 +0000375 if (AI.isEnumAttribute()) return false;
Hal Finkele15442c2014-07-18 06:51:55 +0000376 if (AI.isIntAttribute()) return false;
Bill Wendling26b95752013-02-15 05:25:26 +0000377 if (getKindAsString() == AI.getKindAsString())
378 return getValueAsString() < AI.getValueAsString();
379 return getKindAsString() < AI.getKindAsString();
Bill Wendlingd509a662013-01-29 00:34:06 +0000380}
381
Bill Wendlingd509a662013-01-29 00:34:06 +0000382uint64_t AttributeImpl::getAttrMask(Attribute::AttrKind Val) {
383 // FIXME: Remove this.
384 switch (Val) {
385 case Attribute::EndAttrKinds:
Bill Wendlingd509a662013-01-29 00:34:06 +0000386 llvm_unreachable("Synthetic enumerators which should never get here");
387
388 case Attribute::None: return 0;
389 case Attribute::ZExt: return 1 << 0;
390 case Attribute::SExt: return 1 << 1;
391 case Attribute::NoReturn: return 1 << 2;
392 case Attribute::InReg: return 1 << 3;
393 case Attribute::StructRet: return 1 << 4;
394 case Attribute::NoUnwind: return 1 << 5;
395 case Attribute::NoAlias: return 1 << 6;
396 case Attribute::ByVal: return 1 << 7;
397 case Attribute::Nest: return 1 << 8;
398 case Attribute::ReadNone: return 1 << 9;
399 case Attribute::ReadOnly: return 1 << 10;
400 case Attribute::NoInline: return 1 << 11;
401 case Attribute::AlwaysInline: return 1 << 12;
402 case Attribute::OptimizeForSize: return 1 << 13;
403 case Attribute::StackProtect: return 1 << 14;
404 case Attribute::StackProtectReq: return 1 << 15;
405 case Attribute::Alignment: return 31 << 16;
406 case Attribute::NoCapture: return 1 << 21;
407 case Attribute::NoRedZone: return 1 << 22;
408 case Attribute::NoImplicitFloat: return 1 << 23;
409 case Attribute::Naked: return 1 << 24;
410 case Attribute::InlineHint: return 1 << 25;
411 case Attribute::StackAlignment: return 7 << 26;
412 case Attribute::ReturnsTwice: return 1 << 29;
413 case Attribute::UWTable: return 1 << 30;
414 case Attribute::NonLazyBind: return 1U << 31;
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000415 case Attribute::SanitizeAddress: return 1ULL << 32;
Bill Wendlingd509a662013-01-29 00:34:06 +0000416 case Attribute::MinSize: return 1ULL << 33;
417 case Attribute::NoDuplicate: return 1ULL << 34;
418 case Attribute::StackProtectStrong: return 1ULL << 35;
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000419 case Attribute::SanitizeThread: return 1ULL << 36;
420 case Attribute::SanitizeMemory: return 1ULL << 37;
Bill Wendling7bfdfe72013-02-22 00:40:12 +0000421 case Attribute::NoBuiltin: return 1ULL << 38;
Stephen Linb8bd2322013-04-20 05:14:40 +0000422 case Attribute::Returned: return 1ULL << 39;
Diego Novilloc6399532013-05-24 12:26:52 +0000423 case Attribute::Cold: return 1ULL << 40;
Michael Gottesman41748d72013-06-27 00:25:01 +0000424 case Attribute::Builtin: return 1ULL << 41;
Andrea Di Biagio377496b2013-08-23 11:53:55 +0000425 case Attribute::OptimizeNone: return 1ULL << 42;
Reid Klecknera534a382013-12-19 02:14:12 +0000426 case Attribute::InAlloca: return 1ULL << 43;
Nick Lewyckyd52b1522014-05-20 01:23:40 +0000427 case Attribute::NonNull: return 1ULL << 44;
Tom Roeder44cb65f2014-06-05 19:29:43 +0000428 case Attribute::JumpTable: return 1ULL << 45;
Hal Finkelb0407ba2014-07-18 15:51:28 +0000429 case Attribute::Dereferenceable:
430 llvm_unreachable("dereferenceable attribute not supported in raw format");
Bill Wendling25342e12013-02-22 00:50:09 +0000431 }
432 llvm_unreachable("Unsupported attribute type");
Bill Wendlingd509a662013-01-29 00:34:06 +0000433}
434
435//===----------------------------------------------------------------------===//
436// AttributeSetNode Definition
437//===----------------------------------------------------------------------===//
438
439AttributeSetNode *AttributeSetNode::get(LLVMContext &C,
440 ArrayRef<Attribute> Attrs) {
441 if (Attrs.empty())
Craig Topperc6207612014-04-09 06:08:46 +0000442 return nullptr;
Bill Wendlingd509a662013-01-29 00:34:06 +0000443
444 // Otherwise, build a key to look up the existing attributes.
445 LLVMContextImpl *pImpl = C.pImpl;
446 FoldingSetNodeID ID;
447
448 SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end());
Bill Wendling82c2ee62013-02-13 09:26:26 +0000449 array_pod_sort(SortedAttrs.begin(), SortedAttrs.end());
Bill Wendlingd509a662013-01-29 00:34:06 +0000450
451 for (SmallVectorImpl<Attribute>::iterator I = SortedAttrs.begin(),
452 E = SortedAttrs.end(); I != E; ++I)
453 I->Profile(ID);
454
455 void *InsertPoint;
456 AttributeSetNode *PA =
457 pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint);
458
459 // If we didn't find any existing attributes of the same shape then create a
460 // new one and insert it.
461 if (!PA) {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000462 // Coallocate entries after the AttributeSetNode itself.
463 void *Mem = ::operator new(sizeof(AttributeSetNode) +
464 sizeof(Attribute) * SortedAttrs.size());
465 PA = new (Mem) AttributeSetNode(SortedAttrs);
Bill Wendlingd509a662013-01-29 00:34:06 +0000466 pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint);
467 }
468
469 // Return the AttributesListNode that we found or created.
470 return PA;
471}
472
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000473bool AttributeSetNode::hasAttribute(Attribute::AttrKind Kind) const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000474 for (iterator I = begin(), E = end(); I != E; ++I)
NAKAMURA Takumib0944392013-01-31 03:47:28 +0000475 if (I->hasAttribute(Kind))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000476 return true;
477 return false;
478}
479
Bill Wendlingbce7b972013-02-13 08:42:21 +0000480bool AttributeSetNode::hasAttribute(StringRef Kind) const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000481 for (iterator I = begin(), E = end(); I != E; ++I)
Bill Wendlingbce7b972013-02-13 08:42:21 +0000482 if (I->hasAttribute(Kind))
483 return true;
484 return false;
485}
486
487Attribute AttributeSetNode::getAttribute(Attribute::AttrKind Kind) const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000488 for (iterator I = begin(), E = end(); I != E; ++I)
Bill Wendlingbce7b972013-02-13 08:42:21 +0000489 if (I->hasAttribute(Kind))
490 return *I;
491 return Attribute();
492}
493
494Attribute AttributeSetNode::getAttribute(StringRef Kind) const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000495 for (iterator I = begin(), E = end(); I != E; ++I)
Bill Wendlingbce7b972013-02-13 08:42:21 +0000496 if (I->hasAttribute(Kind))
497 return *I;
498 return Attribute();
499}
500
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000501unsigned AttributeSetNode::getAlignment() const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000502 for (iterator I = begin(), E = end(); I != E; ++I)
NAKAMURA Takumib0944392013-01-31 03:47:28 +0000503 if (I->hasAttribute(Attribute::Alignment))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000504 return I->getAlignment();
505 return 0;
506}
507
508unsigned AttributeSetNode::getStackAlignment() const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000509 for (iterator I = begin(), E = end(); I != E; ++I)
NAKAMURA Takumib0944392013-01-31 03:47:28 +0000510 if (I->hasAttribute(Attribute::StackAlignment))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000511 return I->getStackAlignment();
512 return 0;
513}
514
Hal Finkelb0407ba2014-07-18 15:51:28 +0000515uint64_t AttributeSetNode::getDereferenceableBytes() const {
516 for (iterator I = begin(), E = end(); I != E; ++I)
517 if (I->hasAttribute(Attribute::Dereferenceable))
518 return I->getDereferenceableBytes();
519 return 0;
520}
521
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +0000522std::string AttributeSetNode::getAsString(bool InAttrGrp) const {
Benjamin Kramer0baf8f42013-04-19 11:43:21 +0000523 std::string Str;
Benjamin Kramer741146b2013-07-11 12:13:16 +0000524 for (iterator I = begin(), E = end(); I != E; ++I) {
525 if (I != begin())
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +0000526 Str += ' ';
527 Str += I->getAsString(InAttrGrp);
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000528 }
529 return Str;
530}
531
Bill Wendlingd509a662013-01-29 00:34:06 +0000532//===----------------------------------------------------------------------===//
533// AttributeSetImpl Definition
534//===----------------------------------------------------------------------===//
535
Rafael Espindoladd275302013-04-30 16:53:38 +0000536uint64_t AttributeSetImpl::Raw(unsigned Index) const {
Bill Wendlingd509a662013-01-29 00:34:06 +0000537 for (unsigned I = 0, E = getNumAttributes(); I != E; ++I) {
538 if (getSlotIndex(I) != Index) continue;
Benjamin Kramer741146b2013-07-11 12:13:16 +0000539 const AttributeSetNode *ASN = getSlotNode(I);
Bill Wendling91226182013-02-02 00:52:44 +0000540 uint64_t Mask = 0;
Bill Wendlingd509a662013-01-29 00:34:06 +0000541
Benjamin Kramer741146b2013-07-11 12:13:16 +0000542 for (AttributeSetNode::iterator II = ASN->begin(),
Bill Wendling91226182013-02-02 00:52:44 +0000543 IE = ASN->end(); II != IE; ++II) {
544 Attribute Attr = *II;
Bill Wendlingd746e402013-02-10 23:18:05 +0000545
546 // This cannot handle string attributes.
547 if (Attr.isStringAttribute()) continue;
548
Bill Wendling3f12ac22013-02-05 22:37:24 +0000549 Attribute::AttrKind Kind = Attr.getKindAsEnum();
Bill Wendling91226182013-02-02 00:52:44 +0000550
Bill Wendling3f12ac22013-02-05 22:37:24 +0000551 if (Kind == Attribute::Alignment)
Bill Wendling91226182013-02-02 00:52:44 +0000552 Mask |= (Log2_32(ASN->getAlignment()) + 1) << 16;
Bill Wendling3f12ac22013-02-05 22:37:24 +0000553 else if (Kind == Attribute::StackAlignment)
Bill Wendling91226182013-02-02 00:52:44 +0000554 Mask |= (Log2_32(ASN->getStackAlignment()) + 1) << 26;
Hal Finkelb0407ba2014-07-18 15:51:28 +0000555 else if (Kind == Attribute::Dereferenceable)
556 llvm_unreachable("dereferenceable not supported in bit mask");
Bill Wendling91226182013-02-02 00:52:44 +0000557 else
Bill Wendling3f12ac22013-02-05 22:37:24 +0000558 Mask |= AttributeImpl::getAttrMask(Kind);
Bill Wendling91226182013-02-02 00:52:44 +0000559 }
560
561 return Mask;
Bill Wendlingd509a662013-01-29 00:34:06 +0000562 }
563
564 return 0;
565}
566
Peter Collingbourneabca2ec2013-08-02 22:34:30 +0000567void AttributeSetImpl::dump() const {
568 AttributeSet(const_cast<AttributeSetImpl *>(this)).dump();
569}
570
Bill Wendlingd509a662013-01-29 00:34:06 +0000571//===----------------------------------------------------------------------===//
572// AttributeSet Construction and Mutation Methods
573//===----------------------------------------------------------------------===//
574
Bill Wendling60011b82013-01-29 01:43:29 +0000575AttributeSet
576AttributeSet::getImpl(LLVMContext &C,
577 ArrayRef<std::pair<unsigned, AttributeSetNode*> > Attrs) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000578 LLVMContextImpl *pImpl = C.pImpl;
579 FoldingSetNodeID ID;
580 AttributeSetImpl::Profile(ID, Attrs);
581
582 void *InsertPoint;
583 AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
584
585 // If we didn't find any existing attributes of the same shape then
586 // create a new one and insert it.
587 if (!PA) {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000588 // Coallocate entries after the AttributeSetImpl itself.
589 void *Mem = ::operator new(sizeof(AttributeSetImpl) +
590 sizeof(std::pair<unsigned, AttributeSetNode *>) *
591 Attrs.size());
592 PA = new (Mem) AttributeSetImpl(C, Attrs);
Bill Wendlingd509a662013-01-29 00:34:06 +0000593 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
594 }
595
596 // Return the AttributesList that we found or created.
597 return AttributeSet(PA);
598}
599
600AttributeSet AttributeSet::get(LLVMContext &C,
601 ArrayRef<std::pair<unsigned, Attribute> > Attrs){
602 // If there are no attributes then return a null AttributesList pointer.
603 if (Attrs.empty())
604 return AttributeSet();
605
606#ifndef NDEBUG
607 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
608 assert((!i || Attrs[i-1].first <= Attrs[i].first) &&
609 "Misordered Attributes list!");
Bill Wendling3f12ac22013-02-05 22:37:24 +0000610 assert(!Attrs[i].second.hasAttribute(Attribute::None) &&
Bill Wendlingd509a662013-01-29 00:34:06 +0000611 "Pointless attribute!");
612 }
613#endif
614
615 // Create a vector if (unsigned, AttributeSetNode*) pairs from the attributes
616 // list.
617 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrPairVec;
618 for (ArrayRef<std::pair<unsigned, Attribute> >::iterator I = Attrs.begin(),
619 E = Attrs.end(); I != E; ) {
620 unsigned Index = I->first;
621 SmallVector<Attribute, 4> AttrVec;
NAKAMURA Takumif05d2f22013-01-29 15:18:16 +0000622 while (I != E && I->first == Index) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000623 AttrVec.push_back(I->second);
624 ++I;
625 }
626
627 AttrPairVec.push_back(std::make_pair(Index,
628 AttributeSetNode::get(C, AttrVec)));
629 }
630
631 return getImpl(C, AttrPairVec);
632}
633
634AttributeSet AttributeSet::get(LLVMContext &C,
635 ArrayRef<std::pair<unsigned,
636 AttributeSetNode*> > Attrs) {
637 // If there are no attributes then return a null AttributesList pointer.
638 if (Attrs.empty())
639 return AttributeSet();
640
641 return getImpl(C, Attrs);
642}
643
David Majnemercf63a792014-05-03 23:00:35 +0000644AttributeSet AttributeSet::get(LLVMContext &C, unsigned Index,
645 const AttrBuilder &B) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000646 if (!B.hasAttributes())
647 return AttributeSet();
Bill Wendlingf7134812013-01-29 01:02:03 +0000648
Bill Wendlingae89a0f2013-02-05 23:48:36 +0000649 // Add target-independent attributes.
Bill Wendlingf7134812013-01-29 01:02:03 +0000650 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
Benjamin Kramer45e7d532013-02-16 19:13:18 +0000651 for (Attribute::AttrKind Kind = Attribute::None;
Benjamin Kramer6f37dac2013-02-16 19:22:28 +0000652 Kind != Attribute::EndAttrKinds; Kind = Attribute::AttrKind(Kind + 1)) {
Benjamin Kramer45e7d532013-02-16 19:13:18 +0000653 if (!B.contains(Kind))
654 continue;
655
Bill Wendlingf7134812013-01-29 01:02:03 +0000656 if (Kind == Attribute::Alignment)
Bill Wendling211316c2013-04-18 20:17:28 +0000657 Attrs.push_back(std::make_pair(Index, Attribute::
Bill Wendlingf7134812013-01-29 01:02:03 +0000658 getWithAlignment(C, B.getAlignment())));
659 else if (Kind == Attribute::StackAlignment)
Bill Wendling211316c2013-04-18 20:17:28 +0000660 Attrs.push_back(std::make_pair(Index, Attribute::
Bill Wendlingf7134812013-01-29 01:02:03 +0000661 getWithStackAlignment(C, B.getStackAlignment())));
Hal Finkelb0407ba2014-07-18 15:51:28 +0000662 else if (Kind == Attribute::Dereferenceable)
663 Attrs.push_back(std::make_pair(Index,
664 Attribute::getWithDereferenceableBytes(C,
665 B.getDereferenceableBytes())));
Bill Wendlingf7134812013-01-29 01:02:03 +0000666 else
Bill Wendling211316c2013-04-18 20:17:28 +0000667 Attrs.push_back(std::make_pair(Index, Attribute::get(C, Kind)));
Bill Wendlingf7134812013-01-29 01:02:03 +0000668 }
669
Bill Wendlingae89a0f2013-02-05 23:48:36 +0000670 // Add target-dependent (string) attributes.
David Majnemercf63a792014-05-03 23:00:35 +0000671 for (const AttrBuilder::td_type &TDA : B.td_attrs())
672 Attrs.push_back(
673 std::make_pair(Index, Attribute::get(C, TDA.first, TDA.second)));
Bill Wendlingae89a0f2013-02-05 23:48:36 +0000674
Bill Wendlingf7134812013-01-29 01:02:03 +0000675 return get(C, Attrs);
Bill Wendlingd509a662013-01-29 00:34:06 +0000676}
677
Bill Wendling211316c2013-04-18 20:17:28 +0000678AttributeSet AttributeSet::get(LLVMContext &C, unsigned Index,
Bill Wendlingd509a662013-01-29 00:34:06 +0000679 ArrayRef<Attribute::AttrKind> Kind) {
680 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
681 for (ArrayRef<Attribute::AttrKind>::iterator I = Kind.begin(),
682 E = Kind.end(); I != E; ++I)
Bill Wendling211316c2013-04-18 20:17:28 +0000683 Attrs.push_back(std::make_pair(Index, Attribute::get(C, *I)));
Bill Wendlingd509a662013-01-29 00:34:06 +0000684 return get(C, Attrs);
685}
686
687AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<AttributeSet> Attrs) {
688 if (Attrs.empty()) return AttributeSet();
Peter Collingbournebd6c7452013-08-02 22:29:40 +0000689 if (Attrs.size() == 1) return Attrs[0];
Bill Wendlingd509a662013-01-29 00:34:06 +0000690
691 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrNodeVec;
Peter Collingbournebd6c7452013-08-02 22:29:40 +0000692 AttributeSetImpl *A0 = Attrs[0].pImpl;
693 if (A0)
694 AttrNodeVec.append(A0->getNode(0), A0->getNode(A0->getNumAttributes()));
695 // Copy all attributes from Attrs into AttrNodeVec while keeping AttrNodeVec
696 // ordered by index. Because we know that each list in Attrs is ordered by
697 // index we only need to merge each successive list in rather than doing a
698 // full sort.
699 for (unsigned I = 1, E = Attrs.size(); I != E; ++I) {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000700 AttributeSetImpl *AS = Attrs[I].pImpl;
701 if (!AS) continue;
Peter Collingbournebd6c7452013-08-02 22:29:40 +0000702 SmallVector<std::pair<unsigned, AttributeSetNode *>, 8>::iterator
703 ANVI = AttrNodeVec.begin(), ANVE;
704 for (const AttributeSetImpl::IndexAttrPair
705 *AI = AS->getNode(0),
706 *AE = AS->getNode(AS->getNumAttributes());
707 AI != AE; ++AI) {
708 ANVE = AttrNodeVec.end();
709 while (ANVI != ANVE && ANVI->first <= AI->first)
710 ++ANVI;
711 ANVI = AttrNodeVec.insert(ANVI, *AI) + 1;
712 }
Bill Wendlingd509a662013-01-29 00:34:06 +0000713 }
714
715 return getImpl(C, AttrNodeVec);
716}
717
Bill Wendling211316c2013-04-18 20:17:28 +0000718AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
Bill Wendlingd509a662013-01-29 00:34:06 +0000719 Attribute::AttrKind Attr) const {
Bill Wendling211316c2013-04-18 20:17:28 +0000720 if (hasAttribute(Index, Attr)) return *this;
721 return addAttributes(C, Index, AttributeSet::get(C, Index, Attr));
Bill Wendlingd509a662013-01-29 00:34:06 +0000722}
723
Bill Wendling211316c2013-04-18 20:17:28 +0000724AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
Reed Kotler795c7b42013-03-13 20:20:08 +0000725 StringRef Kind) const {
726 llvm::AttrBuilder B;
727 B.addAttribute(Kind);
Bill Wendling211316c2013-04-18 20:17:28 +0000728 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
Reed Kotler795c7b42013-03-13 20:20:08 +0000729}
730
Bill Wendling3b2f6102013-07-25 18:34:24 +0000731AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
732 StringRef Kind, StringRef Value) const {
733 llvm::AttrBuilder B;
734 B.addAttribute(Kind, Value);
735 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
736}
737
Bill Wendling211316c2013-04-18 20:17:28 +0000738AttributeSet AttributeSet::addAttributes(LLVMContext &C, unsigned Index,
Bill Wendlingd509a662013-01-29 00:34:06 +0000739 AttributeSet Attrs) const {
740 if (!pImpl) return Attrs;
741 if (!Attrs.pImpl) return *this;
742
743#ifndef NDEBUG
744 // FIXME it is not obvious how this should work for alignment. For now, say
745 // we can't change a known alignment.
Bill Wendling211316c2013-04-18 20:17:28 +0000746 unsigned OldAlign = getParamAlignment(Index);
747 unsigned NewAlign = Attrs.getParamAlignment(Index);
Bill Wendlingd509a662013-01-29 00:34:06 +0000748 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
749 "Attempt to change alignment!");
750#endif
751
752 // Add the attribute slots before the one we're trying to add.
753 SmallVector<AttributeSet, 4> AttrSet;
754 uint64_t NumAttrs = pImpl->getNumAttributes();
755 AttributeSet AS;
756 uint64_t LastIndex = 0;
757 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
Bill Wendling211316c2013-04-18 20:17:28 +0000758 if (getSlotIndex(I) >= Index) {
759 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
Bill Wendlingd509a662013-01-29 00:34:06 +0000760 break;
761 }
762 LastIndex = I + 1;
763 AttrSet.push_back(getSlotAttributes(I));
764 }
765
766 // Now add the attribute into the correct slot. There may already be an
767 // AttributeSet there.
Bill Wendling211316c2013-04-18 20:17:28 +0000768 AttrBuilder B(AS, Index);
Bill Wendlingd509a662013-01-29 00:34:06 +0000769
770 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
Bill Wendling211316c2013-04-18 20:17:28 +0000771 if (Attrs.getSlotIndex(I) == Index) {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000772 for (AttributeSetImpl::iterator II = Attrs.pImpl->begin(I),
Bill Wendlingd509a662013-01-29 00:34:06 +0000773 IE = Attrs.pImpl->end(I); II != IE; ++II)
Bill Wendling23804da2013-01-31 23:38:01 +0000774 B.addAttribute(*II);
Bill Wendlingd509a662013-01-29 00:34:06 +0000775 break;
776 }
777
Bill Wendling211316c2013-04-18 20:17:28 +0000778 AttrSet.push_back(AttributeSet::get(C, Index, B));
Bill Wendlingd509a662013-01-29 00:34:06 +0000779
780 // Add the remaining attribute slots.
781 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
782 AttrSet.push_back(getSlotAttributes(I));
783
784 return get(C, AttrSet);
785}
786
Bill Wendling211316c2013-04-18 20:17:28 +0000787AttributeSet AttributeSet::removeAttribute(LLVMContext &C, unsigned Index,
Bill Wendlingd509a662013-01-29 00:34:06 +0000788 Attribute::AttrKind Attr) const {
Bill Wendling211316c2013-04-18 20:17:28 +0000789 if (!hasAttribute(Index, Attr)) return *this;
790 return removeAttributes(C, Index, AttributeSet::get(C, Index, Attr));
Bill Wendlingd509a662013-01-29 00:34:06 +0000791}
792
Bill Wendling211316c2013-04-18 20:17:28 +0000793AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Index,
Bill Wendlingd509a662013-01-29 00:34:06 +0000794 AttributeSet Attrs) const {
795 if (!pImpl) return AttributeSet();
796 if (!Attrs.pImpl) return *this;
797
798#ifndef NDEBUG
799 // FIXME it is not obvious how this should work for alignment.
800 // For now, say we can't pass in alignment, which no current use does.
Bill Wendling211316c2013-04-18 20:17:28 +0000801 assert(!Attrs.hasAttribute(Index, Attribute::Alignment) &&
Bill Wendlingd509a662013-01-29 00:34:06 +0000802 "Attempt to change alignment!");
803#endif
804
805 // Add the attribute slots before the one we're trying to add.
806 SmallVector<AttributeSet, 4> AttrSet;
807 uint64_t NumAttrs = pImpl->getNumAttributes();
808 AttributeSet AS;
809 uint64_t LastIndex = 0;
810 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
Bill Wendling211316c2013-04-18 20:17:28 +0000811 if (getSlotIndex(I) >= Index) {
812 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
Bill Wendlingd509a662013-01-29 00:34:06 +0000813 break;
814 }
815 LastIndex = I + 1;
816 AttrSet.push_back(getSlotAttributes(I));
817 }
818
Bill Wendlingd2196752013-01-30 23:07:40 +0000819 // Now remove the attribute from the correct slot. There may already be an
Bill Wendlingd509a662013-01-29 00:34:06 +0000820 // AttributeSet there.
Bill Wendling211316c2013-04-18 20:17:28 +0000821 AttrBuilder B(AS, Index);
Bill Wendlingd509a662013-01-29 00:34:06 +0000822
823 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
Bill Wendling211316c2013-04-18 20:17:28 +0000824 if (Attrs.getSlotIndex(I) == Index) {
825 B.removeAttributes(Attrs.pImpl->getSlotAttributes(I), Index);
Bill Wendlingd509a662013-01-29 00:34:06 +0000826 break;
827 }
828
Bill Wendling211316c2013-04-18 20:17:28 +0000829 AttrSet.push_back(AttributeSet::get(C, Index, B));
Bill Wendlingd509a662013-01-29 00:34:06 +0000830
831 // Add the remaining attribute slots.
832 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
833 AttrSet.push_back(getSlotAttributes(I));
834
835 return get(C, AttrSet);
836}
837
838//===----------------------------------------------------------------------===//
839// AttributeSet Accessor Methods
840//===----------------------------------------------------------------------===//
841
Bill Wendling5d020a32013-02-10 05:00:40 +0000842LLVMContext &AttributeSet::getContext() const {
843 return pImpl->getContext();
844}
845
Bill Wendling211316c2013-04-18 20:17:28 +0000846AttributeSet AttributeSet::getParamAttributes(unsigned Index) const {
847 return pImpl && hasAttributes(Index) ?
Bill Wendlingd509a662013-01-29 00:34:06 +0000848 AttributeSet::get(pImpl->getContext(),
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000849 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendling211316c2013-04-18 20:17:28 +0000850 std::make_pair(Index, getAttributes(Index)))) :
Bill Wendlingd509a662013-01-29 00:34:06 +0000851 AttributeSet();
852}
853
854AttributeSet AttributeSet::getRetAttributes() const {
855 return pImpl && hasAttributes(ReturnIndex) ?
856 AttributeSet::get(pImpl->getContext(),
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000857 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendlingd509a662013-01-29 00:34:06 +0000858 std::make_pair(ReturnIndex,
859 getAttributes(ReturnIndex)))) :
860 AttributeSet();
861}
862
863AttributeSet AttributeSet::getFnAttributes() const {
864 return pImpl && hasAttributes(FunctionIndex) ?
865 AttributeSet::get(pImpl->getContext(),
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000866 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendlingd509a662013-01-29 00:34:06 +0000867 std::make_pair(FunctionIndex,
868 getAttributes(FunctionIndex)))) :
869 AttributeSet();
870}
871
872bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000873 AttributeSetNode *ASN = getAttributes(Index);
874 return ASN ? ASN->hasAttribute(Kind) : false;
Bill Wendlingd509a662013-01-29 00:34:06 +0000875}
876
Bill Wendlingbce7b972013-02-13 08:42:21 +0000877bool AttributeSet::hasAttribute(unsigned Index, StringRef Kind) const {
878 AttributeSetNode *ASN = getAttributes(Index);
879 return ASN ? ASN->hasAttribute(Kind) : false;
880}
881
Bill Wendlingd509a662013-01-29 00:34:06 +0000882bool AttributeSet::hasAttributes(unsigned Index) const {
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000883 AttributeSetNode *ASN = getAttributes(Index);
884 return ASN ? ASN->hasAttributes() : false;
Bill Wendlingd509a662013-01-29 00:34:06 +0000885}
886
887/// \brief Return true if the specified attribute is set for at least one
888/// parameter or for the return value.
889bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr) const {
Craig Topperc6207612014-04-09 06:08:46 +0000890 if (!pImpl) return false;
Bill Wendlingd509a662013-01-29 00:34:06 +0000891
892 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
Benjamin Kramer741146b2013-07-11 12:13:16 +0000893 for (AttributeSetImpl::iterator II = pImpl->begin(I),
Bill Wendlingd509a662013-01-29 00:34:06 +0000894 IE = pImpl->end(I); II != IE; ++II)
NAKAMURA Takumib0944392013-01-31 03:47:28 +0000895 if (II->hasAttribute(Attr))
Bill Wendlingd509a662013-01-29 00:34:06 +0000896 return true;
897
898 return false;
899}
900
Bill Wendlingbce7b972013-02-13 08:42:21 +0000901Attribute AttributeSet::getAttribute(unsigned Index,
902 Attribute::AttrKind Kind) const {
903 AttributeSetNode *ASN = getAttributes(Index);
904 return ASN ? ASN->getAttribute(Kind) : Attribute();
905}
906
907Attribute AttributeSet::getAttribute(unsigned Index,
908 StringRef Kind) const {
909 AttributeSetNode *ASN = getAttributes(Index);
910 return ASN ? ASN->getAttribute(Kind) : Attribute();
911}
912
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000913unsigned AttributeSet::getParamAlignment(unsigned Index) const {
914 AttributeSetNode *ASN = getAttributes(Index);
915 return ASN ? ASN->getAlignment() : 0;
Bill Wendlingd509a662013-01-29 00:34:06 +0000916}
917
918unsigned AttributeSet::getStackAlignment(unsigned Index) const {
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000919 AttributeSetNode *ASN = getAttributes(Index);
920 return ASN ? ASN->getStackAlignment() : 0;
Bill Wendlingd509a662013-01-29 00:34:06 +0000921}
922
Hal Finkelb0407ba2014-07-18 15:51:28 +0000923uint64_t AttributeSet::getDereferenceableBytes(unsigned Index) const {
924 AttributeSetNode *ASN = getAttributes(Index);
925 return ASN ? ASN->getDereferenceableBytes() : 0;
926}
927
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +0000928std::string AttributeSet::getAsString(unsigned Index,
Bill Wendling829b4782013-02-11 08:43:33 +0000929 bool InAttrGrp) const {
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000930 AttributeSetNode *ASN = getAttributes(Index);
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +0000931 return ASN ? ASN->getAsString(InAttrGrp) : std::string("");
Bill Wendlingd509a662013-01-29 00:34:06 +0000932}
933
934/// \brief The attributes for the specified index are returned.
Bill Wendling211316c2013-04-18 20:17:28 +0000935AttributeSetNode *AttributeSet::getAttributes(unsigned Index) const {
Craig Topperc6207612014-04-09 06:08:46 +0000936 if (!pImpl) return nullptr;
Bill Wendlingd509a662013-01-29 00:34:06 +0000937
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000938 // Loop through to find the attribute node we want.
939 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
Bill Wendling211316c2013-04-18 20:17:28 +0000940 if (pImpl->getSlotIndex(I) == Index)
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000941 return pImpl->getSlotNode(I);
Bill Wendlingd509a662013-01-29 00:34:06 +0000942
Craig Topperc6207612014-04-09 06:08:46 +0000943 return nullptr;
Bill Wendlingd509a662013-01-29 00:34:06 +0000944}
945
Bill Wendling211316c2013-04-18 20:17:28 +0000946AttributeSet::iterator AttributeSet::begin(unsigned Slot) const {
Bill Wendlinga9174862013-01-31 23:53:05 +0000947 if (!pImpl)
948 return ArrayRef<Attribute>().begin();
Bill Wendling211316c2013-04-18 20:17:28 +0000949 return pImpl->begin(Slot);
Bill Wendlinga9174862013-01-31 23:53:05 +0000950}
951
Bill Wendling211316c2013-04-18 20:17:28 +0000952AttributeSet::iterator AttributeSet::end(unsigned Slot) const {
Bill Wendlinga9174862013-01-31 23:53:05 +0000953 if (!pImpl)
954 return ArrayRef<Attribute>().end();
Bill Wendling211316c2013-04-18 20:17:28 +0000955 return pImpl->end(Slot);
Bill Wendlinga9174862013-01-31 23:53:05 +0000956}
957
Bill Wendlingd509a662013-01-29 00:34:06 +0000958//===----------------------------------------------------------------------===//
959// AttributeSet Introspection Methods
960//===----------------------------------------------------------------------===//
961
962/// \brief Return the number of slots used in this attribute list. This is the
963/// number of arguments that have an attribute set on them (including the
964/// function itself).
965unsigned AttributeSet::getNumSlots() const {
966 return pImpl ? pImpl->getNumAttributes() : 0;
967}
968
Rafael Espindoladd275302013-04-30 16:53:38 +0000969unsigned AttributeSet::getSlotIndex(unsigned Slot) const {
Bill Wendlingd509a662013-01-29 00:34:06 +0000970 assert(pImpl && Slot < pImpl->getNumAttributes() &&
971 "Slot # out of range!");
972 return pImpl->getSlotIndex(Slot);
973}
974
975AttributeSet AttributeSet::getSlotAttributes(unsigned Slot) const {
976 assert(pImpl && Slot < pImpl->getNumAttributes() &&
977 "Slot # out of range!");
978 return pImpl->getSlotAttributes(Slot);
979}
980
981uint64_t AttributeSet::Raw(unsigned Index) const {
982 // FIXME: Remove this.
983 return pImpl ? pImpl->Raw(Index) : 0;
984}
985
986void AttributeSet::dump() const {
987 dbgs() << "PAL[\n";
988
989 for (unsigned i = 0, e = getNumSlots(); i < e; ++i) {
990 uint64_t Index = getSlotIndex(i);
991 dbgs() << " { ";
992 if (Index == ~0U)
993 dbgs() << "~0U";
994 else
995 dbgs() << Index;
996 dbgs() << " => " << getAsString(Index) << " }\n";
997 }
998
999 dbgs() << "]\n";
1000}
1001
Bill Wendlingabf3feb2012-10-05 06:44:41 +00001002//===----------------------------------------------------------------------===//
Bill Wendlingcd330342013-01-04 23:27:34 +00001003// AttrBuilder Method Implementations
Bill Wendlingabf3feb2012-10-05 06:44:41 +00001004//===----------------------------------------------------------------------===//
1005
Bill Wendling211316c2013-04-18 20:17:28 +00001006AttrBuilder::AttrBuilder(AttributeSet AS, unsigned Index)
Hal Finkelb0407ba2014-07-18 15:51:28 +00001007 : Attrs(0), Alignment(0), StackAlignment(0), DerefBytes(0) {
Bill Wendling56b0b2a2013-01-27 21:23:46 +00001008 AttributeSetImpl *pImpl = AS.pImpl;
Bill Wendling096f5442013-01-07 08:24:35 +00001009 if (!pImpl) return;
1010
Bill Wendling9eb689c2013-01-28 00:21:34 +00001011 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I) {
Bill Wendling211316c2013-04-18 20:17:28 +00001012 if (pImpl->getSlotIndex(I) != Index) continue;
Bill Wendling096f5442013-01-07 08:24:35 +00001013
Benjamin Kramer741146b2013-07-11 12:13:16 +00001014 for (AttributeSetImpl::iterator II = pImpl->begin(I),
Bill Wendling9eb689c2013-01-28 00:21:34 +00001015 IE = pImpl->end(I); II != IE; ++II)
Bill Wendling23804da2013-01-31 23:38:01 +00001016 addAttribute(*II);
Bill Wendling9eb689c2013-01-28 00:21:34 +00001017
1018 break;
1019 }
Bill Wendling096f5442013-01-07 08:24:35 +00001020}
1021
Bill Wendlingcd330342013-01-04 23:27:34 +00001022void AttrBuilder::clear() {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001023 Attrs.reset();
Hal Finkelb0407ba2014-07-18 15:51:28 +00001024 Alignment = StackAlignment = DerefBytes = 0;
Bill Wendlingcd330342013-01-04 23:27:34 +00001025}
1026
1027AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001028 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
Bill Wendling1c7cc8a2013-01-31 23:16:25 +00001029 assert(Val != Attribute::Alignment && Val != Attribute::StackAlignment &&
Hal Finkelb0407ba2014-07-18 15:51:28 +00001030 Val != Attribute::Dereferenceable &&
1031 "Adding integer attribute without adding a value!");
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001032 Attrs[Val] = true;
Bill Wendling7c04e042012-10-09 19:01:18 +00001033 return *this;
Bill Wendlingabf3feb2012-10-05 06:44:41 +00001034}
1035
Bill Wendling23804da2013-01-31 23:38:01 +00001036AttrBuilder &AttrBuilder::addAttribute(Attribute Attr) {
Bill Wendling0a437302013-02-10 10:13:23 +00001037 if (Attr.isStringAttribute()) {
1038 addAttribute(Attr.getKindAsString(), Attr.getValueAsString());
1039 return *this;
1040 }
1041
Bill Wendling3f12ac22013-02-05 22:37:24 +00001042 Attribute::AttrKind Kind = Attr.getKindAsEnum();
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001043 Attrs[Kind] = true;
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001044
Bill Wendling3f12ac22013-02-05 22:37:24 +00001045 if (Kind == Attribute::Alignment)
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001046 Alignment = Attr.getAlignment();
Bill Wendling3f12ac22013-02-05 22:37:24 +00001047 else if (Kind == Attribute::StackAlignment)
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001048 StackAlignment = Attr.getStackAlignment();
Hal Finkelb0407ba2014-07-18 15:51:28 +00001049 else if (Kind == Attribute::Dereferenceable)
1050 DerefBytes = Attr.getDereferenceableBytes();
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001051 return *this;
1052}
1053
Bill Wendlingb9c5b1a2013-02-05 08:09:32 +00001054AttrBuilder &AttrBuilder::addAttribute(StringRef A, StringRef V) {
1055 TargetDepAttrs[A] = V;
1056 return *this;
1057}
1058
Bill Wendling23804da2013-01-31 23:38:01 +00001059AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001060 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
1061 Attrs[Val] = false;
Bill Wendling23804da2013-01-31 23:38:01 +00001062
1063 if (Val == Attribute::Alignment)
1064 Alignment = 0;
1065 else if (Val == Attribute::StackAlignment)
1066 StackAlignment = 0;
Hal Finkelb0407ba2014-07-18 15:51:28 +00001067 else if (Val == Attribute::Dereferenceable)
1068 DerefBytes = 0;
Bill Wendling23804da2013-01-31 23:38:01 +00001069
1070 return *this;
1071}
1072
Bill Wendlingd2196752013-01-30 23:07:40 +00001073AttrBuilder &AttrBuilder::removeAttributes(AttributeSet A, uint64_t Index) {
Bill Wendling211316c2013-04-18 20:17:28 +00001074 unsigned Slot = ~0U;
Bill Wendlingf1c94e32013-02-01 00:13:50 +00001075 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
1076 if (A.getSlotIndex(I) == Index) {
Bill Wendling211316c2013-04-18 20:17:28 +00001077 Slot = I;
Bill Wendlingf1c94e32013-02-01 00:13:50 +00001078 break;
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001079 }
Bill Wendlingf1c94e32013-02-01 00:13:50 +00001080
Bill Wendling211316c2013-04-18 20:17:28 +00001081 assert(Slot != ~0U && "Couldn't find index in AttributeSet!");
Bill Wendlingf1c94e32013-02-01 00:13:50 +00001082
Bill Wendling211316c2013-04-18 20:17:28 +00001083 for (AttributeSet::iterator I = A.begin(Slot), E = A.end(Slot); I != E; ++I) {
Bill Wendling7cde51d2013-02-12 07:56:49 +00001084 Attribute Attr = *I;
Hal Finkele15442c2014-07-18 06:51:55 +00001085 if (Attr.isEnumAttribute() || Attr.isIntAttribute()) {
Bill Wendling7cde51d2013-02-12 07:56:49 +00001086 Attribute::AttrKind Kind = I->getKindAsEnum();
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001087 Attrs[Kind] = false;
Bill Wendlingf1c94e32013-02-01 00:13:50 +00001088
Bill Wendling7cde51d2013-02-12 07:56:49 +00001089 if (Kind == Attribute::Alignment)
1090 Alignment = 0;
1091 else if (Kind == Attribute::StackAlignment)
1092 StackAlignment = 0;
Hal Finkelb0407ba2014-07-18 15:51:28 +00001093 else if (Kind == Attribute::Dereferenceable)
1094 DerefBytes = 0;
Bill Wendling7cde51d2013-02-12 07:56:49 +00001095 } else {
1096 assert(Attr.isStringAttribute() && "Invalid attribute type!");
1097 std::map<std::string, std::string>::iterator
1098 Iter = TargetDepAttrs.find(Attr.getKindAsString());
1099 if (Iter != TargetDepAttrs.end())
1100 TargetDepAttrs.erase(Iter);
1101 }
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001102 }
1103
1104 return *this;
1105}
1106
Bill Wendlingb9c5b1a2013-02-05 08:09:32 +00001107AttrBuilder &AttrBuilder::removeAttribute(StringRef A) {
1108 std::map<std::string, std::string>::iterator I = TargetDepAttrs.find(A);
1109 if (I != TargetDepAttrs.end())
1110 TargetDepAttrs.erase(I);
1111 return *this;
1112}
1113
Bill Wendling50d27842012-10-15 20:35:56 +00001114AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
Bill Wendlingabd5ba22012-10-14 03:58:29 +00001115 if (Align == 0) return *this;
Bill Wendlingcd330342013-01-04 23:27:34 +00001116
Bill Wendlingabf3feb2012-10-05 06:44:41 +00001117 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1118 assert(Align <= 0x40000000 && "Alignment too large.");
Bill Wendlingcd330342013-01-04 23:27:34 +00001119
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001120 Attrs[Attribute::Alignment] = true;
Bill Wendlingcd330342013-01-04 23:27:34 +00001121 Alignment = Align;
Bill Wendlingabd5ba22012-10-14 03:58:29 +00001122 return *this;
Chris Lattner3e13b8c2008-01-02 23:42:30 +00001123}
1124
Bill Wendlingcd330342013-01-04 23:27:34 +00001125AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
1126 // Default alignment, allow the target to define how to align it.
1127 if (Align == 0) return *this;
1128
1129 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1130 assert(Align <= 0x100 && "Alignment too large.");
1131
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001132 Attrs[Attribute::StackAlignment] = true;
Bill Wendlingcd330342013-01-04 23:27:34 +00001133 StackAlignment = Align;
1134 return *this;
1135}
1136
Hal Finkelb0407ba2014-07-18 15:51:28 +00001137AttrBuilder &AttrBuilder::addDereferenceableAttr(uint64_t Bytes) {
1138 if (Bytes == 0) return *this;
1139
1140 Attrs[Attribute::Dereferenceable] = true;
1141 DerefBytes = Bytes;
1142 return *this;
1143}
1144
Bill Wendlinge2614922013-02-06 01:16:00 +00001145AttrBuilder &AttrBuilder::merge(const AttrBuilder &B) {
1146 // FIXME: What if both have alignments, but they don't match?!
1147 if (!Alignment)
1148 Alignment = B.Alignment;
1149
1150 if (!StackAlignment)
1151 StackAlignment = B.StackAlignment;
1152
Hal Finkelb0407ba2014-07-18 15:51:28 +00001153 if (!DerefBytes)
1154 DerefBytes = B.DerefBytes;
1155
Benjamin Kramer45e7d532013-02-16 19:13:18 +00001156 Attrs |= B.Attrs;
Bill Wendlinge2614922013-02-06 01:16:00 +00001157
1158 for (td_const_iterator I = B.TargetDepAttrs.begin(),
1159 E = B.TargetDepAttrs.end(); I != E; ++I)
1160 TargetDepAttrs[I->first] = I->second;
1161
1162 return *this;
1163}
1164
Bill Wendling4b001442013-02-06 01:33:42 +00001165bool AttrBuilder::contains(StringRef A) const {
1166 return TargetDepAttrs.find(A) != TargetDepAttrs.end();
1167}
1168
Bill Wendling50d27842012-10-15 20:35:56 +00001169bool AttrBuilder::hasAttributes() const {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001170 return !Attrs.none() || !TargetDepAttrs.empty();
Bill Wendlingc6daefa2012-10-08 23:27:46 +00001171}
Bill Wendling9ac69f92013-01-04 20:54:35 +00001172
Bill Wendlingd2196752013-01-30 23:07:40 +00001173bool AttrBuilder::hasAttributes(AttributeSet A, uint64_t Index) const {
Bill Wendling211316c2013-04-18 20:17:28 +00001174 unsigned Slot = ~0U;
Bill Wendling9ca01da2013-02-02 00:42:06 +00001175 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
1176 if (A.getSlotIndex(I) == Index) {
Bill Wendling211316c2013-04-18 20:17:28 +00001177 Slot = I;
Bill Wendling9ca01da2013-02-02 00:42:06 +00001178 break;
1179 }
1180
Bill Wendling211316c2013-04-18 20:17:28 +00001181 assert(Slot != ~0U && "Couldn't find the index!");
Bill Wendling9ca01da2013-02-02 00:42:06 +00001182
Bill Wendling211316c2013-04-18 20:17:28 +00001183 for (AttributeSet::iterator I = A.begin(Slot), E = A.end(Slot);
Bill Wendling7cde51d2013-02-12 07:56:49 +00001184 I != E; ++I) {
1185 Attribute Attr = *I;
Hal Finkele15442c2014-07-18 06:51:55 +00001186 if (Attr.isEnumAttribute() || Attr.isIntAttribute()) {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001187 if (Attrs[I->getKindAsEnum()])
Bill Wendling7cde51d2013-02-12 07:56:49 +00001188 return true;
1189 } else {
1190 assert(Attr.isStringAttribute() && "Invalid attribute kind!");
1191 return TargetDepAttrs.find(Attr.getKindAsString())!=TargetDepAttrs.end();
1192 }
1193 }
Bill Wendling9ca01da2013-02-02 00:42:06 +00001194
1195 return false;
Bill Wendling70f39172012-10-09 00:01:21 +00001196}
Bill Wendling9ac69f92013-01-04 20:54:35 +00001197
Bill Wendling50d27842012-10-15 20:35:56 +00001198bool AttrBuilder::hasAlignmentAttr() const {
Bill Wendlingcd330342013-01-04 23:27:34 +00001199 return Alignment != 0;
Bill Wendlingc6daefa2012-10-08 23:27:46 +00001200}
1201
Bill Wendlingd509a662013-01-29 00:34:06 +00001202bool AttrBuilder::operator==(const AttrBuilder &B) {
Benjamin Kramer45e7d532013-02-16 19:13:18 +00001203 if (Attrs != B.Attrs)
1204 return false;
Bill Wendling4b001442013-02-06 01:33:42 +00001205
1206 for (td_const_iterator I = TargetDepAttrs.begin(),
1207 E = TargetDepAttrs.end(); I != E; ++I)
1208 if (B.TargetDepAttrs.find(I->first) == B.TargetDepAttrs.end())
1209 return false;
1210
Hal Finkelb0407ba2014-07-18 15:51:28 +00001211 return Alignment == B.Alignment && StackAlignment == B.StackAlignment &&
1212 DerefBytes == B.DerefBytes;
Bill Wendlingd509a662013-01-29 00:34:06 +00001213}
1214
1215AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) {
Bill Wendling56aeccc2013-02-04 23:32:23 +00001216 // FIXME: Remove this in 4.0.
Bill Wendling60011b82013-01-29 01:43:29 +00001217 if (!Val) return *this;
1218
Bill Wendlingd509a662013-01-29 00:34:06 +00001219 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
1220 I = Attribute::AttrKind(I + 1)) {
Hal Finkelb0407ba2014-07-18 15:51:28 +00001221 if (I == Attribute::Dereferenceable)
1222 continue;
Bill Wendlingd509a662013-01-29 00:34:06 +00001223 if (uint64_t A = (Val & AttributeImpl::getAttrMask(I))) {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001224 Attrs[I] = true;
Bill Wendlingd509a662013-01-29 00:34:06 +00001225
1226 if (I == Attribute::Alignment)
1227 Alignment = 1ULL << ((A >> 16) - 1);
1228 else if (I == Attribute::StackAlignment)
1229 StackAlignment = 1ULL << ((A >> 26)-1);
1230 }
1231 }
1232
1233 return *this;
1234}
1235
Bill Wendling57625a42013-01-25 23:09:36 +00001236//===----------------------------------------------------------------------===//
1237// AttributeFuncs Function Defintions
1238//===----------------------------------------------------------------------===//
1239
Bill Wendlingc79cdff2013-02-01 01:04:27 +00001240/// \brief Which attributes cannot be applied to a type.
Bill Wendlingd2196752013-01-30 23:07:40 +00001241AttributeSet AttributeFuncs::typeIncompatible(Type *Ty, uint64_t Index) {
Bill Wendling57625a42013-01-25 23:09:36 +00001242 AttrBuilder Incompatible;
1243
1244 if (!Ty->isIntegerTy())
1245 // Attribute that only apply to integers.
1246 Incompatible.addAttribute(Attribute::SExt)
1247 .addAttribute(Attribute::ZExt);
1248
1249 if (!Ty->isPointerTy())
1250 // Attribute that only apply to pointers.
1251 Incompatible.addAttribute(Attribute::ByVal)
1252 .addAttribute(Attribute::Nest)
1253 .addAttribute(Attribute::NoAlias)
1254 .addAttribute(Attribute::NoCapture)
Nick Lewyckyd52b1522014-05-20 01:23:40 +00001255 .addAttribute(Attribute::NonNull)
Hal Finkelb0407ba2014-07-18 15:51:28 +00001256 .addDereferenceableAttr(1) // the int here is ignored
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001257 .addAttribute(Attribute::ReadNone)
1258 .addAttribute(Attribute::ReadOnly)
Reid Klecknera534a382013-12-19 02:14:12 +00001259 .addAttribute(Attribute::StructRet)
1260 .addAttribute(Attribute::InAlloca);
Bill Wendling57625a42013-01-25 23:09:36 +00001261
Bill Wendlingd2196752013-01-30 23:07:40 +00001262 return AttributeSet::get(Ty->getContext(), Index, Incompatible);
Bill Wendling57625a42013-01-25 23:09:36 +00001263}