blob: a2f1dd0593c8fdc4b36281043586d25eff8e4c56 [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
Matt Arsenaulte1cef6e2014-09-03 23:24:31 +000034Attribute Attribute::get(LLVMContext &Context, Attribute::AttrKind Kind) {
Bill Wendling73ea2de2012-10-08 21:47:17 +000035 LLVMContextImpl *pImpl = Context.pImpl;
36 FoldingSetNodeID ID;
Bill Wendling3f12ac22013-02-05 22:37:24 +000037 ID.AddInteger(Kind);
Bill Wendling3f12ac22013-02-05 22:37:24 +000038
39 void *InsertPoint;
40 AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
41
42 if (!PA) {
43 // If we didn't find any existing attributes of the same shape then create a
44 // new one and insert it.
Matt Arsenaulte1cef6e2014-09-03 23:24:31 +000045 PA = new EnumAttributeImpl(Kind);
46 pImpl->AttrsSet.InsertNode(PA, InsertPoint);
47 }
48
49 // Return the Attribute that we found or created.
50 return Attribute(PA);
51}
52
53Attribute Attribute::get(LLVMContext &Context, Attribute::AttrKind Kind,
54 uint64_t Val) {
55 LLVMContextImpl *pImpl = Context.pImpl;
56 FoldingSetNodeID ID;
57 ID.AddInteger(Kind);
58 ID.AddInteger(Val);
59
60 void *InsertPoint;
61 AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
62
63 if (!PA) {
64 // If we didn't find any existing attributes of the same shape then create a
65 // new one and insert it.
66 PA = new IntAttributeImpl(Kind, Val);
Bill Wendling3f12ac22013-02-05 22:37:24 +000067 pImpl->AttrsSet.InsertNode(PA, InsertPoint);
68 }
69
70 // Return the Attribute that we found or created.
71 return Attribute(PA);
72}
73
74Attribute Attribute::get(LLVMContext &Context, StringRef Kind, StringRef Val) {
75 LLVMContextImpl *pImpl = Context.pImpl;
76 FoldingSetNodeID ID;
77 ID.AddString(Kind);
78 if (!Val.empty()) ID.AddString(Val);
Bill Wendling73ea2de2012-10-08 21:47:17 +000079
80 void *InsertPoint;
Bill Wendling4607f4b2012-12-20 01:36:59 +000081 AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
Bill Wendling73ea2de2012-10-08 21:47:17 +000082
83 if (!PA) {
84 // If we didn't find any existing attributes of the same shape then create a
85 // new one and insert it.
Benjamin Kramer741146b2013-07-11 12:13:16 +000086 PA = new StringAttributeImpl(Kind, Val);
Bill Wendling73ea2de2012-10-08 21:47:17 +000087 pImpl->AttrsSet.InsertNode(PA, InsertPoint);
88 }
89
Bill Wendlingb9c5b1a2013-02-05 08:09:32 +000090 // Return the Attribute that we found or created.
Bill Wendling3d7b0b82012-12-19 07:18:57 +000091 return Attribute(PA);
Bill Wendling73ea2de2012-10-08 21:47:17 +000092}
93
Bill Wendling4bbe9db2013-01-27 22:43:04 +000094Attribute Attribute::getWithAlignment(LLVMContext &Context, uint64_t Align) {
Bill Wendling1c7cc8a2013-01-31 23:16:25 +000095 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
96 assert(Align <= 0x40000000 && "Alignment too large.");
Bill Wendling3f12ac22013-02-05 22:37:24 +000097 return get(Context, Alignment, Align);
Bill Wendling4bbe9db2013-01-27 22:43:04 +000098}
99
100Attribute Attribute::getWithStackAlignment(LLVMContext &Context,
101 uint64_t Align) {
Bill Wendling1c7cc8a2013-01-31 23:16:25 +0000102 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
103 assert(Align <= 0x100 && "Alignment too large.");
Bill Wendling3f12ac22013-02-05 22:37:24 +0000104 return get(Context, StackAlignment, Align);
Bill Wendling4bbe9db2013-01-27 22:43:04 +0000105}
106
Hal Finkelb0407ba2014-07-18 15:51:28 +0000107Attribute Attribute::getWithDereferenceableBytes(LLVMContext &Context,
108 uint64_t Bytes) {
109 assert(Bytes && "Bytes must be non-zero.");
110 return get(Context, Dereferenceable, Bytes);
111}
112
Bill Wendling7707c5a2013-01-29 00:48:16 +0000113//===----------------------------------------------------------------------===//
114// Attribute Accessor Methods
115//===----------------------------------------------------------------------===//
116
Bill Wendling3f12ac22013-02-05 22:37:24 +0000117bool Attribute::isEnumAttribute() const {
118 return pImpl && pImpl->isEnumAttribute();
119}
120
Hal Finkele15442c2014-07-18 06:51:55 +0000121bool Attribute::isIntAttribute() const {
122 return pImpl && pImpl->isIntAttribute();
Bill Wendling3f12ac22013-02-05 22:37:24 +0000123}
124
125bool Attribute::isStringAttribute() const {
126 return pImpl && pImpl->isStringAttribute();
127}
128
129Attribute::AttrKind Attribute::getKindAsEnum() const {
Bill Wendling440e9d82013-07-25 00:34:29 +0000130 if (!pImpl) return None;
Hal Finkele15442c2014-07-18 06:51:55 +0000131 assert((isEnumAttribute() || isIntAttribute()) &&
Bill Wendling3f12ac22013-02-05 22:37:24 +0000132 "Invalid attribute type to get the kind as an enum!");
133 return pImpl ? pImpl->getKindAsEnum() : None;
134}
135
136uint64_t Attribute::getValueAsInt() const {
Bill Wendling440e9d82013-07-25 00:34:29 +0000137 if (!pImpl) return 0;
Hal Finkele15442c2014-07-18 06:51:55 +0000138 assert(isIntAttribute() &&
139 "Expected the attribute to be an integer attribute!");
Bill Wendling3f12ac22013-02-05 22:37:24 +0000140 return pImpl ? pImpl->getValueAsInt() : 0;
141}
142
143StringRef Attribute::getKindAsString() const {
Bill Wendling440e9d82013-07-25 00:34:29 +0000144 if (!pImpl) return StringRef();
Bill Wendling3f12ac22013-02-05 22:37:24 +0000145 assert(isStringAttribute() &&
146 "Invalid attribute type to get the kind as a string!");
147 return pImpl ? pImpl->getKindAsString() : StringRef();
148}
149
150StringRef Attribute::getValueAsString() const {
Bill Wendling440e9d82013-07-25 00:34:29 +0000151 if (!pImpl) return StringRef();
Bill Wendling3f12ac22013-02-05 22:37:24 +0000152 assert(isStringAttribute() &&
153 "Invalid attribute type to get the value as a string!");
154 return pImpl ? pImpl->getValueAsString() : StringRef();
155}
156
Bill Wendlingae89a0f2013-02-05 23:48:36 +0000157bool Attribute::hasAttribute(AttrKind Kind) const {
158 return (pImpl && pImpl->hasAttribute(Kind)) || (!pImpl && Kind == None);
159}
160
161bool Attribute::hasAttribute(StringRef Kind) const {
162 if (!isStringAttribute()) return false;
163 return pImpl && pImpl->hasAttribute(Kind);
Bill Wendling03eefb32013-01-29 20:45:34 +0000164}
165
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000166/// This returns the alignment field of an attribute as a byte alignment value.
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000167unsigned Attribute::getAlignment() const {
Bill Wendlingc79cdff2013-02-01 01:04:27 +0000168 assert(hasAttribute(Attribute::Alignment) &&
169 "Trying to get alignment from non-alignment attribute!");
Bill Wendling3f12ac22013-02-05 22:37:24 +0000170 return pImpl->getValueAsInt();
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000171}
172
173/// This returns the stack alignment field of an attribute as a byte alignment
174/// value.
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000175unsigned Attribute::getStackAlignment() const {
Bill Wendlingc79cdff2013-02-01 01:04:27 +0000176 assert(hasAttribute(Attribute::StackAlignment) &&
177 "Trying to get alignment from non-alignment attribute!");
Bill Wendling3f12ac22013-02-05 22:37:24 +0000178 return pImpl->getValueAsInt();
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000179}
180
Hal Finkelb0407ba2014-07-18 15:51:28 +0000181/// This returns the number of dereferenceable bytes.
182uint64_t Attribute::getDereferenceableBytes() const {
183 assert(hasAttribute(Attribute::Dereferenceable) &&
184 "Trying to get dereferenceable bytes from "
185 "non-dereferenceable attribute!");
186 return pImpl->getValueAsInt();
187}
188
Bill Wendling829b4782013-02-11 08:43:33 +0000189std::string Attribute::getAsString(bool InAttrGrp) const {
Bill Wendling9c2eba92013-01-31 20:59:05 +0000190 if (!pImpl) return "";
191
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000192 if (hasAttribute(Attribute::SanitizeAddress))
193 return "sanitize_address";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000194 if (hasAttribute(Attribute::AlwaysInline))
195 return "alwaysinline";
Michael Gottesman41748d72013-06-27 00:25:01 +0000196 if (hasAttribute(Attribute::Builtin))
197 return "builtin";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000198 if (hasAttribute(Attribute::ByVal))
199 return "byval";
Reid Klecknera534a382013-12-19 02:14:12 +0000200 if (hasAttribute(Attribute::InAlloca))
201 return "inalloca";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000202 if (hasAttribute(Attribute::InlineHint))
203 return "inlinehint";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000204 if (hasAttribute(Attribute::InReg))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000205 return "inreg";
Tom Roeder44cb65f2014-06-05 19:29:43 +0000206 if (hasAttribute(Attribute::JumpTable))
207 return "jumptable";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000208 if (hasAttribute(Attribute::MinSize))
209 return "minsize";
210 if (hasAttribute(Attribute::Naked))
211 return "naked";
212 if (hasAttribute(Attribute::Nest))
213 return "nest";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000214 if (hasAttribute(Attribute::NoAlias))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000215 return "noalias";
Bill Wendling09bd1f72013-02-22 00:12:35 +0000216 if (hasAttribute(Attribute::NoBuiltin))
217 return "nobuiltin";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000218 if (hasAttribute(Attribute::NoCapture))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000219 return "nocapture";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000220 if (hasAttribute(Attribute::NoDuplicate))
221 return "noduplicate";
222 if (hasAttribute(Attribute::NoImplicitFloat))
223 return "noimplicitfloat";
224 if (hasAttribute(Attribute::NoInline))
225 return "noinline";
226 if (hasAttribute(Attribute::NonLazyBind))
227 return "nonlazybind";
Nick Lewyckyd52b1522014-05-20 01:23:40 +0000228 if (hasAttribute(Attribute::NonNull))
229 return "nonnull";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000230 if (hasAttribute(Attribute::NoRedZone))
231 return "noredzone";
232 if (hasAttribute(Attribute::NoReturn))
233 return "noreturn";
234 if (hasAttribute(Attribute::NoUnwind))
235 return "nounwind";
Andrea Di Biagio377496b2013-08-23 11:53:55 +0000236 if (hasAttribute(Attribute::OptimizeNone))
237 return "optnone";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000238 if (hasAttribute(Attribute::OptimizeForSize))
239 return "optsize";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000240 if (hasAttribute(Attribute::ReadNone))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000241 return "readnone";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000242 if (hasAttribute(Attribute::ReadOnly))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000243 return "readonly";
Stephen Linb8bd2322013-04-20 05:14:40 +0000244 if (hasAttribute(Attribute::Returned))
245 return "returned";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000246 if (hasAttribute(Attribute::ReturnsTwice))
247 return "returns_twice";
248 if (hasAttribute(Attribute::SExt))
249 return "signext";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000250 if (hasAttribute(Attribute::StackProtect))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000251 return "ssp";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000252 if (hasAttribute(Attribute::StackProtectReq))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000253 return "sspreq";
Bill Wendlingd154e2832013-01-23 06:41:41 +0000254 if (hasAttribute(Attribute::StackProtectStrong))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000255 return "sspstrong";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000256 if (hasAttribute(Attribute::StructRet))
257 return "sret";
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000258 if (hasAttribute(Attribute::SanitizeThread))
259 return "sanitize_thread";
260 if (hasAttribute(Attribute::SanitizeMemory))
261 return "sanitize_memory";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000262 if (hasAttribute(Attribute::UWTable))
263 return "uwtable";
264 if (hasAttribute(Attribute::ZExt))
265 return "zeroext";
Diego Novilloc6399532013-05-24 12:26:52 +0000266 if (hasAttribute(Attribute::Cold))
267 return "cold";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000268
269 // FIXME: These should be output like this:
270 //
271 // align=4
272 // alignstack=8
273 //
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000274 if (hasAttribute(Attribute::Alignment)) {
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000275 std::string Result;
Bill Wendling829b4782013-02-11 08:43:33 +0000276 Result += "align";
277 Result += (InAttrGrp) ? "=" : " ";
Bill Wendling3f12ac22013-02-05 22:37:24 +0000278 Result += utostr(getValueAsInt());
279 return Result;
280 }
Bill Wendling829b4782013-02-11 08:43:33 +0000281
Bill Wendling3f12ac22013-02-05 22:37:24 +0000282 if (hasAttribute(Attribute::StackAlignment)) {
283 std::string Result;
Bill Wendling829b4782013-02-11 08:43:33 +0000284 Result += "alignstack";
285 if (InAttrGrp) {
286 Result += "=";
287 Result += utostr(getValueAsInt());
288 } else {
289 Result += "(";
290 Result += utostr(getValueAsInt());
291 Result += ")";
292 }
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000293 return Result;
Dale Johannesen11a555e2008-02-19 23:51:49 +0000294 }
Bill Wendling9c2eba92013-01-31 20:59:05 +0000295
Hal Finkelb0407ba2014-07-18 15:51:28 +0000296 if (hasAttribute(Attribute::Dereferenceable)) {
297 std::string Result;
298 Result += "dereferenceable";
299 if (InAttrGrp) {
300 Result += "=";
301 Result += utostr(getValueAsInt());
302 } else {
303 Result += "(";
304 Result += utostr(getValueAsInt());
305 Result += ")";
306 }
307 return Result;
308 }
309
Bill Wendling9c2eba92013-01-31 20:59:05 +0000310 // Convert target-dependent attributes to strings of the form:
311 //
312 // "kind"
313 // "kind" = "value"
Bill Wendling9c2eba92013-01-31 20:59:05 +0000314 //
Bill Wendling3f12ac22013-02-05 22:37:24 +0000315 if (isStringAttribute()) {
Bill Wendling9c2eba92013-01-31 20:59:05 +0000316 std::string Result;
Bill Wendling3f12ac22013-02-05 22:37:24 +0000317 Result += '\"' + getKindAsString().str() + '"';
Bill Wendling9c2eba92013-01-31 20:59:05 +0000318
Bill Wendling3f12ac22013-02-05 22:37:24 +0000319 StringRef Val = pImpl->getValueAsString();
320 if (Val.empty()) return Result;
Bill Wendling7a33f772013-02-01 22:32:30 +0000321
Bill Wendling829b4782013-02-11 08:43:33 +0000322 Result += "=\"" + Val.str() + '"';
Bill Wendlingc79cdff2013-02-01 01:04:27 +0000323 return Result;
Bill Wendling9c2eba92013-01-31 20:59:05 +0000324 }
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000325
326 llvm_unreachable("Unknown attribute");
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000327}
328
Bill Wendlingd509a662013-01-29 00:34:06 +0000329bool Attribute::operator<(Attribute A) const {
330 if (!pImpl && !A.pImpl) return false;
331 if (!pImpl) return true;
332 if (!A.pImpl) return false;
333 return *pImpl < *A.pImpl;
334}
335
Bill Wendlingd509a662013-01-29 00:34:06 +0000336//===----------------------------------------------------------------------===//
337// AttributeImpl Definition
338//===----------------------------------------------------------------------===//
339
Eric Christopher0eaa5412014-07-02 22:05:40 +0000340// Pin the vtables to this file.
Alexey Samsonov49109a22013-11-18 09:31:53 +0000341AttributeImpl::~AttributeImpl() {}
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +0000342void EnumAttributeImpl::anchor() {}
Hal Finkele15442c2014-07-18 06:51:55 +0000343void IntAttributeImpl::anchor() {}
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +0000344void StringAttributeImpl::anchor() {}
Alexey Samsonov49109a22013-11-18 09:31:53 +0000345
Bill Wendlingd509a662013-01-29 00:34:06 +0000346bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
Bill Wendling3f12ac22013-02-05 22:37:24 +0000347 if (isStringAttribute()) return false;
348 return getKindAsEnum() == A;
Bill Wendlingd509a662013-01-29 00:34:06 +0000349}
350
Bill Wendling3f12ac22013-02-05 22:37:24 +0000351bool AttributeImpl::hasAttribute(StringRef Kind) const {
352 if (!isStringAttribute()) return false;
353 return getKindAsString() == Kind;
Bill Wendlingd509a662013-01-29 00:34:06 +0000354}
355
Bill Wendling3f12ac22013-02-05 22:37:24 +0000356Attribute::AttrKind AttributeImpl::getKindAsEnum() const {
Hal Finkele15442c2014-07-18 06:51:55 +0000357 assert(isEnumAttribute() || isIntAttribute());
Benjamin Kramer741146b2013-07-11 12:13:16 +0000358 return static_cast<const EnumAttributeImpl *>(this)->getEnumKind();
Bill Wendlingd509a662013-01-29 00:34:06 +0000359}
360
Bill Wendling3f12ac22013-02-05 22:37:24 +0000361uint64_t AttributeImpl::getValueAsInt() const {
Hal Finkele15442c2014-07-18 06:51:55 +0000362 assert(isIntAttribute());
363 return static_cast<const IntAttributeImpl *>(this)->getValue();
Bill Wendlingd509a662013-01-29 00:34:06 +0000364}
365
Bill Wendling3f12ac22013-02-05 22:37:24 +0000366StringRef AttributeImpl::getKindAsString() const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000367 assert(isStringAttribute());
368 return static_cast<const StringAttributeImpl *>(this)->getStringKind();
Bill Wendlingd509a662013-01-29 00:34:06 +0000369}
370
Bill Wendling3f12ac22013-02-05 22:37:24 +0000371StringRef AttributeImpl::getValueAsString() const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000372 assert(isStringAttribute());
373 return static_cast<const StringAttributeImpl *>(this)->getStringValue();
Bill Wendlingd509a662013-01-29 00:34:06 +0000374}
375
376bool AttributeImpl::operator<(const AttributeImpl &AI) const {
Bill Wendlingc79cdff2013-02-01 01:04:27 +0000377 // This sorts the attributes with Attribute::AttrKinds coming first (sorted
378 // relative to their enum value) and then strings.
Bill Wendling26b95752013-02-15 05:25:26 +0000379 if (isEnumAttribute()) {
380 if (AI.isEnumAttribute()) return getKindAsEnum() < AI.getKindAsEnum();
Hal Finkele15442c2014-07-18 06:51:55 +0000381 if (AI.isIntAttribute()) return true;
Bill Wendling26b95752013-02-15 05:25:26 +0000382 if (AI.isStringAttribute()) return true;
383 }
Bill Wendlingc79cdff2013-02-01 01:04:27 +0000384
Hal Finkele15442c2014-07-18 06:51:55 +0000385 if (isIntAttribute()) {
Bill Wendling26b95752013-02-15 05:25:26 +0000386 if (AI.isEnumAttribute()) return false;
Hal Finkele15442c2014-07-18 06:51:55 +0000387 if (AI.isIntAttribute()) return getValueAsInt() < AI.getValueAsInt();
Bill Wendling26b95752013-02-15 05:25:26 +0000388 if (AI.isStringAttribute()) return true;
Bill Wendling3f12ac22013-02-05 22:37:24 +0000389 }
Bill Wendlingd509a662013-01-29 00:34:06 +0000390
Bill Wendling26b95752013-02-15 05:25:26 +0000391 if (AI.isEnumAttribute()) return false;
Hal Finkele15442c2014-07-18 06:51:55 +0000392 if (AI.isIntAttribute()) return false;
Bill Wendling26b95752013-02-15 05:25:26 +0000393 if (getKindAsString() == AI.getKindAsString())
394 return getValueAsString() < AI.getValueAsString();
395 return getKindAsString() < AI.getKindAsString();
Bill Wendlingd509a662013-01-29 00:34:06 +0000396}
397
Bill Wendlingd509a662013-01-29 00:34:06 +0000398uint64_t AttributeImpl::getAttrMask(Attribute::AttrKind Val) {
399 // FIXME: Remove this.
400 switch (Val) {
401 case Attribute::EndAttrKinds:
Bill Wendlingd509a662013-01-29 00:34:06 +0000402 llvm_unreachable("Synthetic enumerators which should never get here");
403
404 case Attribute::None: return 0;
405 case Attribute::ZExt: return 1 << 0;
406 case Attribute::SExt: return 1 << 1;
407 case Attribute::NoReturn: return 1 << 2;
408 case Attribute::InReg: return 1 << 3;
409 case Attribute::StructRet: return 1 << 4;
410 case Attribute::NoUnwind: return 1 << 5;
411 case Attribute::NoAlias: return 1 << 6;
412 case Attribute::ByVal: return 1 << 7;
413 case Attribute::Nest: return 1 << 8;
414 case Attribute::ReadNone: return 1 << 9;
415 case Attribute::ReadOnly: return 1 << 10;
416 case Attribute::NoInline: return 1 << 11;
417 case Attribute::AlwaysInline: return 1 << 12;
418 case Attribute::OptimizeForSize: return 1 << 13;
419 case Attribute::StackProtect: return 1 << 14;
420 case Attribute::StackProtectReq: return 1 << 15;
421 case Attribute::Alignment: return 31 << 16;
422 case Attribute::NoCapture: return 1 << 21;
423 case Attribute::NoRedZone: return 1 << 22;
424 case Attribute::NoImplicitFloat: return 1 << 23;
425 case Attribute::Naked: return 1 << 24;
426 case Attribute::InlineHint: return 1 << 25;
427 case Attribute::StackAlignment: return 7 << 26;
428 case Attribute::ReturnsTwice: return 1 << 29;
429 case Attribute::UWTable: return 1 << 30;
430 case Attribute::NonLazyBind: return 1U << 31;
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000431 case Attribute::SanitizeAddress: return 1ULL << 32;
Bill Wendlingd509a662013-01-29 00:34:06 +0000432 case Attribute::MinSize: return 1ULL << 33;
433 case Attribute::NoDuplicate: return 1ULL << 34;
434 case Attribute::StackProtectStrong: return 1ULL << 35;
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000435 case Attribute::SanitizeThread: return 1ULL << 36;
436 case Attribute::SanitizeMemory: return 1ULL << 37;
Bill Wendling7bfdfe72013-02-22 00:40:12 +0000437 case Attribute::NoBuiltin: return 1ULL << 38;
Stephen Linb8bd2322013-04-20 05:14:40 +0000438 case Attribute::Returned: return 1ULL << 39;
Diego Novilloc6399532013-05-24 12:26:52 +0000439 case Attribute::Cold: return 1ULL << 40;
Michael Gottesman41748d72013-06-27 00:25:01 +0000440 case Attribute::Builtin: return 1ULL << 41;
Andrea Di Biagio377496b2013-08-23 11:53:55 +0000441 case Attribute::OptimizeNone: return 1ULL << 42;
Reid Klecknera534a382013-12-19 02:14:12 +0000442 case Attribute::InAlloca: return 1ULL << 43;
Nick Lewyckyd52b1522014-05-20 01:23:40 +0000443 case Attribute::NonNull: return 1ULL << 44;
Tom Roeder44cb65f2014-06-05 19:29:43 +0000444 case Attribute::JumpTable: return 1ULL << 45;
Hal Finkelb0407ba2014-07-18 15:51:28 +0000445 case Attribute::Dereferenceable:
446 llvm_unreachable("dereferenceable attribute not supported in raw format");
Bill Wendling25342e12013-02-22 00:50:09 +0000447 }
448 llvm_unreachable("Unsupported attribute type");
Bill Wendlingd509a662013-01-29 00:34:06 +0000449}
450
451//===----------------------------------------------------------------------===//
452// AttributeSetNode Definition
453//===----------------------------------------------------------------------===//
454
455AttributeSetNode *AttributeSetNode::get(LLVMContext &C,
456 ArrayRef<Attribute> Attrs) {
457 if (Attrs.empty())
Craig Topperc6207612014-04-09 06:08:46 +0000458 return nullptr;
Bill Wendlingd509a662013-01-29 00:34:06 +0000459
460 // Otherwise, build a key to look up the existing attributes.
461 LLVMContextImpl *pImpl = C.pImpl;
462 FoldingSetNodeID ID;
463
464 SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end());
Bill Wendling82c2ee62013-02-13 09:26:26 +0000465 array_pod_sort(SortedAttrs.begin(), SortedAttrs.end());
Bill Wendlingd509a662013-01-29 00:34:06 +0000466
467 for (SmallVectorImpl<Attribute>::iterator I = SortedAttrs.begin(),
468 E = SortedAttrs.end(); I != E; ++I)
469 I->Profile(ID);
470
471 void *InsertPoint;
472 AttributeSetNode *PA =
473 pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint);
474
475 // If we didn't find any existing attributes of the same shape then create a
476 // new one and insert it.
477 if (!PA) {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000478 // Coallocate entries after the AttributeSetNode itself.
479 void *Mem = ::operator new(sizeof(AttributeSetNode) +
480 sizeof(Attribute) * SortedAttrs.size());
481 PA = new (Mem) AttributeSetNode(SortedAttrs);
Bill Wendlingd509a662013-01-29 00:34:06 +0000482 pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint);
483 }
484
485 // Return the AttributesListNode that we found or created.
486 return PA;
487}
488
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000489bool AttributeSetNode::hasAttribute(Attribute::AttrKind Kind) const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000490 for (iterator I = begin(), E = end(); I != E; ++I)
NAKAMURA Takumib0944392013-01-31 03:47:28 +0000491 if (I->hasAttribute(Kind))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000492 return true;
493 return false;
494}
495
Bill Wendlingbce7b972013-02-13 08:42:21 +0000496bool AttributeSetNode::hasAttribute(StringRef Kind) const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000497 for (iterator I = begin(), E = end(); I != E; ++I)
Bill Wendlingbce7b972013-02-13 08:42:21 +0000498 if (I->hasAttribute(Kind))
499 return true;
500 return false;
501}
502
503Attribute AttributeSetNode::getAttribute(Attribute::AttrKind Kind) const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000504 for (iterator I = begin(), E = end(); I != E; ++I)
Bill Wendlingbce7b972013-02-13 08:42:21 +0000505 if (I->hasAttribute(Kind))
506 return *I;
507 return Attribute();
508}
509
510Attribute AttributeSetNode::getAttribute(StringRef Kind) const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000511 for (iterator I = begin(), E = end(); I != E; ++I)
Bill Wendlingbce7b972013-02-13 08:42:21 +0000512 if (I->hasAttribute(Kind))
513 return *I;
514 return Attribute();
515}
516
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000517unsigned AttributeSetNode::getAlignment() const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000518 for (iterator I = begin(), E = end(); I != E; ++I)
NAKAMURA Takumib0944392013-01-31 03:47:28 +0000519 if (I->hasAttribute(Attribute::Alignment))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000520 return I->getAlignment();
521 return 0;
522}
523
524unsigned AttributeSetNode::getStackAlignment() const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000525 for (iterator I = begin(), E = end(); I != E; ++I)
NAKAMURA Takumib0944392013-01-31 03:47:28 +0000526 if (I->hasAttribute(Attribute::StackAlignment))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000527 return I->getStackAlignment();
528 return 0;
529}
530
Hal Finkelb0407ba2014-07-18 15:51:28 +0000531uint64_t AttributeSetNode::getDereferenceableBytes() const {
532 for (iterator I = begin(), E = end(); I != E; ++I)
533 if (I->hasAttribute(Attribute::Dereferenceable))
534 return I->getDereferenceableBytes();
535 return 0;
536}
537
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +0000538std::string AttributeSetNode::getAsString(bool InAttrGrp) const {
Benjamin Kramer0baf8f42013-04-19 11:43:21 +0000539 std::string Str;
Benjamin Kramer741146b2013-07-11 12:13:16 +0000540 for (iterator I = begin(), E = end(); I != E; ++I) {
541 if (I != begin())
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +0000542 Str += ' ';
543 Str += I->getAsString(InAttrGrp);
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000544 }
545 return Str;
546}
547
Bill Wendlingd509a662013-01-29 00:34:06 +0000548//===----------------------------------------------------------------------===//
549// AttributeSetImpl Definition
550//===----------------------------------------------------------------------===//
551
Rafael Espindoladd275302013-04-30 16:53:38 +0000552uint64_t AttributeSetImpl::Raw(unsigned Index) const {
Bill Wendlingd509a662013-01-29 00:34:06 +0000553 for (unsigned I = 0, E = getNumAttributes(); I != E; ++I) {
554 if (getSlotIndex(I) != Index) continue;
Benjamin Kramer741146b2013-07-11 12:13:16 +0000555 const AttributeSetNode *ASN = getSlotNode(I);
Bill Wendling91226182013-02-02 00:52:44 +0000556 uint64_t Mask = 0;
Bill Wendlingd509a662013-01-29 00:34:06 +0000557
Benjamin Kramer741146b2013-07-11 12:13:16 +0000558 for (AttributeSetNode::iterator II = ASN->begin(),
Bill Wendling91226182013-02-02 00:52:44 +0000559 IE = ASN->end(); II != IE; ++II) {
560 Attribute Attr = *II;
Bill Wendlingd746e402013-02-10 23:18:05 +0000561
562 // This cannot handle string attributes.
563 if (Attr.isStringAttribute()) continue;
564
Bill Wendling3f12ac22013-02-05 22:37:24 +0000565 Attribute::AttrKind Kind = Attr.getKindAsEnum();
Bill Wendling91226182013-02-02 00:52:44 +0000566
Bill Wendling3f12ac22013-02-05 22:37:24 +0000567 if (Kind == Attribute::Alignment)
Bill Wendling91226182013-02-02 00:52:44 +0000568 Mask |= (Log2_32(ASN->getAlignment()) + 1) << 16;
Bill Wendling3f12ac22013-02-05 22:37:24 +0000569 else if (Kind == Attribute::StackAlignment)
Bill Wendling91226182013-02-02 00:52:44 +0000570 Mask |= (Log2_32(ASN->getStackAlignment()) + 1) << 26;
Hal Finkelb0407ba2014-07-18 15:51:28 +0000571 else if (Kind == Attribute::Dereferenceable)
572 llvm_unreachable("dereferenceable not supported in bit mask");
Bill Wendling91226182013-02-02 00:52:44 +0000573 else
Bill Wendling3f12ac22013-02-05 22:37:24 +0000574 Mask |= AttributeImpl::getAttrMask(Kind);
Bill Wendling91226182013-02-02 00:52:44 +0000575 }
576
577 return Mask;
Bill Wendlingd509a662013-01-29 00:34:06 +0000578 }
579
580 return 0;
581}
582
Peter Collingbourneabca2ec2013-08-02 22:34:30 +0000583void AttributeSetImpl::dump() const {
584 AttributeSet(const_cast<AttributeSetImpl *>(this)).dump();
585}
586
Bill Wendlingd509a662013-01-29 00:34:06 +0000587//===----------------------------------------------------------------------===//
588// AttributeSet Construction and Mutation Methods
589//===----------------------------------------------------------------------===//
590
Bill Wendling60011b82013-01-29 01:43:29 +0000591AttributeSet
592AttributeSet::getImpl(LLVMContext &C,
593 ArrayRef<std::pair<unsigned, AttributeSetNode*> > Attrs) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000594 LLVMContextImpl *pImpl = C.pImpl;
595 FoldingSetNodeID ID;
596 AttributeSetImpl::Profile(ID, Attrs);
597
598 void *InsertPoint;
599 AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
600
601 // If we didn't find any existing attributes of the same shape then
602 // create a new one and insert it.
603 if (!PA) {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000604 // Coallocate entries after the AttributeSetImpl itself.
605 void *Mem = ::operator new(sizeof(AttributeSetImpl) +
606 sizeof(std::pair<unsigned, AttributeSetNode *>) *
607 Attrs.size());
608 PA = new (Mem) AttributeSetImpl(C, Attrs);
Bill Wendlingd509a662013-01-29 00:34:06 +0000609 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
610 }
611
612 // Return the AttributesList that we found or created.
613 return AttributeSet(PA);
614}
615
616AttributeSet AttributeSet::get(LLVMContext &C,
617 ArrayRef<std::pair<unsigned, Attribute> > Attrs){
618 // If there are no attributes then return a null AttributesList pointer.
619 if (Attrs.empty())
620 return AttributeSet();
621
622#ifndef NDEBUG
623 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
624 assert((!i || Attrs[i-1].first <= Attrs[i].first) &&
625 "Misordered Attributes list!");
Bill Wendling3f12ac22013-02-05 22:37:24 +0000626 assert(!Attrs[i].second.hasAttribute(Attribute::None) &&
Bill Wendlingd509a662013-01-29 00:34:06 +0000627 "Pointless attribute!");
628 }
629#endif
630
631 // Create a vector if (unsigned, AttributeSetNode*) pairs from the attributes
632 // list.
633 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrPairVec;
634 for (ArrayRef<std::pair<unsigned, Attribute> >::iterator I = Attrs.begin(),
635 E = Attrs.end(); I != E; ) {
636 unsigned Index = I->first;
637 SmallVector<Attribute, 4> AttrVec;
NAKAMURA Takumif05d2f22013-01-29 15:18:16 +0000638 while (I != E && I->first == Index) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000639 AttrVec.push_back(I->second);
640 ++I;
641 }
642
643 AttrPairVec.push_back(std::make_pair(Index,
644 AttributeSetNode::get(C, AttrVec)));
645 }
646
647 return getImpl(C, AttrPairVec);
648}
649
650AttributeSet AttributeSet::get(LLVMContext &C,
651 ArrayRef<std::pair<unsigned,
652 AttributeSetNode*> > Attrs) {
653 // If there are no attributes then return a null AttributesList pointer.
654 if (Attrs.empty())
655 return AttributeSet();
656
657 return getImpl(C, Attrs);
658}
659
David Majnemercf63a792014-05-03 23:00:35 +0000660AttributeSet AttributeSet::get(LLVMContext &C, unsigned Index,
661 const AttrBuilder &B) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000662 if (!B.hasAttributes())
663 return AttributeSet();
Bill Wendlingf7134812013-01-29 01:02:03 +0000664
Bill Wendlingae89a0f2013-02-05 23:48:36 +0000665 // Add target-independent attributes.
Bill Wendlingf7134812013-01-29 01:02:03 +0000666 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
Benjamin Kramer45e7d532013-02-16 19:13:18 +0000667 for (Attribute::AttrKind Kind = Attribute::None;
Benjamin Kramer6f37dac2013-02-16 19:22:28 +0000668 Kind != Attribute::EndAttrKinds; Kind = Attribute::AttrKind(Kind + 1)) {
Benjamin Kramer45e7d532013-02-16 19:13:18 +0000669 if (!B.contains(Kind))
670 continue;
671
Bill Wendlingf7134812013-01-29 01:02:03 +0000672 if (Kind == Attribute::Alignment)
Bill Wendling211316c2013-04-18 20:17:28 +0000673 Attrs.push_back(std::make_pair(Index, Attribute::
Bill Wendlingf7134812013-01-29 01:02:03 +0000674 getWithAlignment(C, B.getAlignment())));
675 else if (Kind == Attribute::StackAlignment)
Bill Wendling211316c2013-04-18 20:17:28 +0000676 Attrs.push_back(std::make_pair(Index, Attribute::
Bill Wendlingf7134812013-01-29 01:02:03 +0000677 getWithStackAlignment(C, B.getStackAlignment())));
Hal Finkelb0407ba2014-07-18 15:51:28 +0000678 else if (Kind == Attribute::Dereferenceable)
679 Attrs.push_back(std::make_pair(Index,
680 Attribute::getWithDereferenceableBytes(C,
681 B.getDereferenceableBytes())));
Bill Wendlingf7134812013-01-29 01:02:03 +0000682 else
Bill Wendling211316c2013-04-18 20:17:28 +0000683 Attrs.push_back(std::make_pair(Index, Attribute::get(C, Kind)));
Bill Wendlingf7134812013-01-29 01:02:03 +0000684 }
685
Bill Wendlingae89a0f2013-02-05 23:48:36 +0000686 // Add target-dependent (string) attributes.
David Majnemercf63a792014-05-03 23:00:35 +0000687 for (const AttrBuilder::td_type &TDA : B.td_attrs())
688 Attrs.push_back(
689 std::make_pair(Index, Attribute::get(C, TDA.first, TDA.second)));
Bill Wendlingae89a0f2013-02-05 23:48:36 +0000690
Bill Wendlingf7134812013-01-29 01:02:03 +0000691 return get(C, Attrs);
Bill Wendlingd509a662013-01-29 00:34:06 +0000692}
693
Bill Wendling211316c2013-04-18 20:17:28 +0000694AttributeSet AttributeSet::get(LLVMContext &C, unsigned Index,
Bill Wendlingd509a662013-01-29 00:34:06 +0000695 ArrayRef<Attribute::AttrKind> Kind) {
696 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
697 for (ArrayRef<Attribute::AttrKind>::iterator I = Kind.begin(),
698 E = Kind.end(); I != E; ++I)
Bill Wendling211316c2013-04-18 20:17:28 +0000699 Attrs.push_back(std::make_pair(Index, Attribute::get(C, *I)));
Bill Wendlingd509a662013-01-29 00:34:06 +0000700 return get(C, Attrs);
701}
702
703AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<AttributeSet> Attrs) {
704 if (Attrs.empty()) return AttributeSet();
Peter Collingbournebd6c7452013-08-02 22:29:40 +0000705 if (Attrs.size() == 1) return Attrs[0];
Bill Wendlingd509a662013-01-29 00:34:06 +0000706
707 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrNodeVec;
Peter Collingbournebd6c7452013-08-02 22:29:40 +0000708 AttributeSetImpl *A0 = Attrs[0].pImpl;
709 if (A0)
710 AttrNodeVec.append(A0->getNode(0), A0->getNode(A0->getNumAttributes()));
711 // Copy all attributes from Attrs into AttrNodeVec while keeping AttrNodeVec
712 // ordered by index. Because we know that each list in Attrs is ordered by
713 // index we only need to merge each successive list in rather than doing a
714 // full sort.
715 for (unsigned I = 1, E = Attrs.size(); I != E; ++I) {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000716 AttributeSetImpl *AS = Attrs[I].pImpl;
717 if (!AS) continue;
Peter Collingbournebd6c7452013-08-02 22:29:40 +0000718 SmallVector<std::pair<unsigned, AttributeSetNode *>, 8>::iterator
719 ANVI = AttrNodeVec.begin(), ANVE;
720 for (const AttributeSetImpl::IndexAttrPair
721 *AI = AS->getNode(0),
722 *AE = AS->getNode(AS->getNumAttributes());
723 AI != AE; ++AI) {
724 ANVE = AttrNodeVec.end();
725 while (ANVI != ANVE && ANVI->first <= AI->first)
726 ++ANVI;
727 ANVI = AttrNodeVec.insert(ANVI, *AI) + 1;
728 }
Bill Wendlingd509a662013-01-29 00:34:06 +0000729 }
730
731 return getImpl(C, AttrNodeVec);
732}
733
Bill Wendling211316c2013-04-18 20:17:28 +0000734AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
Bill Wendlingd509a662013-01-29 00:34:06 +0000735 Attribute::AttrKind Attr) const {
Bill Wendling211316c2013-04-18 20:17:28 +0000736 if (hasAttribute(Index, Attr)) return *this;
737 return addAttributes(C, Index, AttributeSet::get(C, Index, Attr));
Bill Wendlingd509a662013-01-29 00:34:06 +0000738}
739
Bill Wendling211316c2013-04-18 20:17:28 +0000740AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
Reed Kotler795c7b42013-03-13 20:20:08 +0000741 StringRef Kind) const {
742 llvm::AttrBuilder B;
743 B.addAttribute(Kind);
Bill Wendling211316c2013-04-18 20:17:28 +0000744 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
Reed Kotler795c7b42013-03-13 20:20:08 +0000745}
746
Bill Wendling3b2f6102013-07-25 18:34:24 +0000747AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
748 StringRef Kind, StringRef Value) const {
749 llvm::AttrBuilder B;
750 B.addAttribute(Kind, Value);
751 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
752}
753
Bill Wendling211316c2013-04-18 20:17:28 +0000754AttributeSet AttributeSet::addAttributes(LLVMContext &C, unsigned Index,
Bill Wendlingd509a662013-01-29 00:34:06 +0000755 AttributeSet Attrs) const {
756 if (!pImpl) return Attrs;
757 if (!Attrs.pImpl) return *this;
758
759#ifndef NDEBUG
760 // FIXME it is not obvious how this should work for alignment. For now, say
761 // we can't change a known alignment.
Bill Wendling211316c2013-04-18 20:17:28 +0000762 unsigned OldAlign = getParamAlignment(Index);
763 unsigned NewAlign = Attrs.getParamAlignment(Index);
Bill Wendlingd509a662013-01-29 00:34:06 +0000764 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
765 "Attempt to change alignment!");
766#endif
767
768 // Add the attribute slots before the one we're trying to add.
769 SmallVector<AttributeSet, 4> AttrSet;
770 uint64_t NumAttrs = pImpl->getNumAttributes();
771 AttributeSet AS;
772 uint64_t LastIndex = 0;
773 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
Bill Wendling211316c2013-04-18 20:17:28 +0000774 if (getSlotIndex(I) >= Index) {
775 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
Bill Wendlingd509a662013-01-29 00:34:06 +0000776 break;
777 }
778 LastIndex = I + 1;
779 AttrSet.push_back(getSlotAttributes(I));
780 }
781
782 // Now add the attribute into the correct slot. There may already be an
783 // AttributeSet there.
Bill Wendling211316c2013-04-18 20:17:28 +0000784 AttrBuilder B(AS, Index);
Bill Wendlingd509a662013-01-29 00:34:06 +0000785
786 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
Bill Wendling211316c2013-04-18 20:17:28 +0000787 if (Attrs.getSlotIndex(I) == Index) {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000788 for (AttributeSetImpl::iterator II = Attrs.pImpl->begin(I),
Bill Wendlingd509a662013-01-29 00:34:06 +0000789 IE = Attrs.pImpl->end(I); II != IE; ++II)
Bill Wendling23804da2013-01-31 23:38:01 +0000790 B.addAttribute(*II);
Bill Wendlingd509a662013-01-29 00:34:06 +0000791 break;
792 }
793
Bill Wendling211316c2013-04-18 20:17:28 +0000794 AttrSet.push_back(AttributeSet::get(C, Index, B));
Bill Wendlingd509a662013-01-29 00:34:06 +0000795
796 // Add the remaining attribute slots.
797 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
798 AttrSet.push_back(getSlotAttributes(I));
799
800 return get(C, AttrSet);
801}
802
Bill Wendling211316c2013-04-18 20:17:28 +0000803AttributeSet AttributeSet::removeAttribute(LLVMContext &C, unsigned Index,
Bill Wendlingd509a662013-01-29 00:34:06 +0000804 Attribute::AttrKind Attr) const {
Bill Wendling211316c2013-04-18 20:17:28 +0000805 if (!hasAttribute(Index, Attr)) return *this;
806 return removeAttributes(C, Index, AttributeSet::get(C, Index, Attr));
Bill Wendlingd509a662013-01-29 00:34:06 +0000807}
808
Bill Wendling211316c2013-04-18 20:17:28 +0000809AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Index,
Bill Wendlingd509a662013-01-29 00:34:06 +0000810 AttributeSet Attrs) const {
811 if (!pImpl) return AttributeSet();
812 if (!Attrs.pImpl) return *this;
813
814#ifndef NDEBUG
815 // FIXME it is not obvious how this should work for alignment.
816 // For now, say we can't pass in alignment, which no current use does.
Bill Wendling211316c2013-04-18 20:17:28 +0000817 assert(!Attrs.hasAttribute(Index, Attribute::Alignment) &&
Bill Wendlingd509a662013-01-29 00:34:06 +0000818 "Attempt to change alignment!");
819#endif
820
821 // Add the attribute slots before the one we're trying to add.
822 SmallVector<AttributeSet, 4> AttrSet;
823 uint64_t NumAttrs = pImpl->getNumAttributes();
824 AttributeSet AS;
825 uint64_t LastIndex = 0;
826 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
Bill Wendling211316c2013-04-18 20:17:28 +0000827 if (getSlotIndex(I) >= Index) {
828 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
Bill Wendlingd509a662013-01-29 00:34:06 +0000829 break;
830 }
831 LastIndex = I + 1;
832 AttrSet.push_back(getSlotAttributes(I));
833 }
834
Bill Wendlingd2196752013-01-30 23:07:40 +0000835 // Now remove the attribute from the correct slot. There may already be an
Bill Wendlingd509a662013-01-29 00:34:06 +0000836 // AttributeSet there.
Bill Wendling211316c2013-04-18 20:17:28 +0000837 AttrBuilder B(AS, Index);
Bill Wendlingd509a662013-01-29 00:34:06 +0000838
839 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
Bill Wendling211316c2013-04-18 20:17:28 +0000840 if (Attrs.getSlotIndex(I) == Index) {
841 B.removeAttributes(Attrs.pImpl->getSlotAttributes(I), Index);
Bill Wendlingd509a662013-01-29 00:34:06 +0000842 break;
843 }
844
Bill Wendling211316c2013-04-18 20:17:28 +0000845 AttrSet.push_back(AttributeSet::get(C, Index, B));
Bill Wendlingd509a662013-01-29 00:34:06 +0000846
847 // Add the remaining attribute slots.
848 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
849 AttrSet.push_back(getSlotAttributes(I));
850
851 return get(C, AttrSet);
852}
853
854//===----------------------------------------------------------------------===//
855// AttributeSet Accessor Methods
856//===----------------------------------------------------------------------===//
857
Bill Wendling5d020a32013-02-10 05:00:40 +0000858LLVMContext &AttributeSet::getContext() const {
859 return pImpl->getContext();
860}
861
Bill Wendling211316c2013-04-18 20:17:28 +0000862AttributeSet AttributeSet::getParamAttributes(unsigned Index) const {
863 return pImpl && hasAttributes(Index) ?
Bill Wendlingd509a662013-01-29 00:34:06 +0000864 AttributeSet::get(pImpl->getContext(),
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000865 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendling211316c2013-04-18 20:17:28 +0000866 std::make_pair(Index, getAttributes(Index)))) :
Bill Wendlingd509a662013-01-29 00:34:06 +0000867 AttributeSet();
868}
869
870AttributeSet AttributeSet::getRetAttributes() const {
871 return pImpl && hasAttributes(ReturnIndex) ?
872 AttributeSet::get(pImpl->getContext(),
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000873 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendlingd509a662013-01-29 00:34:06 +0000874 std::make_pair(ReturnIndex,
875 getAttributes(ReturnIndex)))) :
876 AttributeSet();
877}
878
879AttributeSet AttributeSet::getFnAttributes() const {
880 return pImpl && hasAttributes(FunctionIndex) ?
881 AttributeSet::get(pImpl->getContext(),
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000882 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendlingd509a662013-01-29 00:34:06 +0000883 std::make_pair(FunctionIndex,
884 getAttributes(FunctionIndex)))) :
885 AttributeSet();
886}
887
888bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000889 AttributeSetNode *ASN = getAttributes(Index);
890 return ASN ? ASN->hasAttribute(Kind) : false;
Bill Wendlingd509a662013-01-29 00:34:06 +0000891}
892
Bill Wendlingbce7b972013-02-13 08:42:21 +0000893bool AttributeSet::hasAttribute(unsigned Index, StringRef Kind) const {
894 AttributeSetNode *ASN = getAttributes(Index);
895 return ASN ? ASN->hasAttribute(Kind) : false;
896}
897
Bill Wendlingd509a662013-01-29 00:34:06 +0000898bool AttributeSet::hasAttributes(unsigned Index) const {
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000899 AttributeSetNode *ASN = getAttributes(Index);
900 return ASN ? ASN->hasAttributes() : false;
Bill Wendlingd509a662013-01-29 00:34:06 +0000901}
902
903/// \brief Return true if the specified attribute is set for at least one
904/// parameter or for the return value.
905bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr) const {
Craig Topperc6207612014-04-09 06:08:46 +0000906 if (!pImpl) return false;
Bill Wendlingd509a662013-01-29 00:34:06 +0000907
908 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
Benjamin Kramer741146b2013-07-11 12:13:16 +0000909 for (AttributeSetImpl::iterator II = pImpl->begin(I),
Bill Wendlingd509a662013-01-29 00:34:06 +0000910 IE = pImpl->end(I); II != IE; ++II)
NAKAMURA Takumib0944392013-01-31 03:47:28 +0000911 if (II->hasAttribute(Attr))
Bill Wendlingd509a662013-01-29 00:34:06 +0000912 return true;
913
914 return false;
915}
916
Bill Wendlingbce7b972013-02-13 08:42:21 +0000917Attribute AttributeSet::getAttribute(unsigned Index,
918 Attribute::AttrKind Kind) const {
919 AttributeSetNode *ASN = getAttributes(Index);
920 return ASN ? ASN->getAttribute(Kind) : Attribute();
921}
922
923Attribute AttributeSet::getAttribute(unsigned Index,
924 StringRef Kind) const {
925 AttributeSetNode *ASN = getAttributes(Index);
926 return ASN ? ASN->getAttribute(Kind) : Attribute();
927}
928
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000929unsigned AttributeSet::getParamAlignment(unsigned Index) const {
930 AttributeSetNode *ASN = getAttributes(Index);
931 return ASN ? ASN->getAlignment() : 0;
Bill Wendlingd509a662013-01-29 00:34:06 +0000932}
933
934unsigned AttributeSet::getStackAlignment(unsigned Index) const {
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000935 AttributeSetNode *ASN = getAttributes(Index);
936 return ASN ? ASN->getStackAlignment() : 0;
Bill Wendlingd509a662013-01-29 00:34:06 +0000937}
938
Hal Finkelb0407ba2014-07-18 15:51:28 +0000939uint64_t AttributeSet::getDereferenceableBytes(unsigned Index) const {
940 AttributeSetNode *ASN = getAttributes(Index);
941 return ASN ? ASN->getDereferenceableBytes() : 0;
942}
943
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +0000944std::string AttributeSet::getAsString(unsigned Index,
Bill Wendling829b4782013-02-11 08:43:33 +0000945 bool InAttrGrp) const {
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000946 AttributeSetNode *ASN = getAttributes(Index);
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +0000947 return ASN ? ASN->getAsString(InAttrGrp) : std::string("");
Bill Wendlingd509a662013-01-29 00:34:06 +0000948}
949
950/// \brief The attributes for the specified index are returned.
Bill Wendling211316c2013-04-18 20:17:28 +0000951AttributeSetNode *AttributeSet::getAttributes(unsigned Index) const {
Craig Topperc6207612014-04-09 06:08:46 +0000952 if (!pImpl) return nullptr;
Bill Wendlingd509a662013-01-29 00:34:06 +0000953
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000954 // Loop through to find the attribute node we want.
955 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
Bill Wendling211316c2013-04-18 20:17:28 +0000956 if (pImpl->getSlotIndex(I) == Index)
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000957 return pImpl->getSlotNode(I);
Bill Wendlingd509a662013-01-29 00:34:06 +0000958
Craig Topperc6207612014-04-09 06:08:46 +0000959 return nullptr;
Bill Wendlingd509a662013-01-29 00:34:06 +0000960}
961
Bill Wendling211316c2013-04-18 20:17:28 +0000962AttributeSet::iterator AttributeSet::begin(unsigned Slot) const {
Bill Wendlinga9174862013-01-31 23:53:05 +0000963 if (!pImpl)
964 return ArrayRef<Attribute>().begin();
Bill Wendling211316c2013-04-18 20:17:28 +0000965 return pImpl->begin(Slot);
Bill Wendlinga9174862013-01-31 23:53:05 +0000966}
967
Bill Wendling211316c2013-04-18 20:17:28 +0000968AttributeSet::iterator AttributeSet::end(unsigned Slot) const {
Bill Wendlinga9174862013-01-31 23:53:05 +0000969 if (!pImpl)
970 return ArrayRef<Attribute>().end();
Bill Wendling211316c2013-04-18 20:17:28 +0000971 return pImpl->end(Slot);
Bill Wendlinga9174862013-01-31 23:53:05 +0000972}
973
Bill Wendlingd509a662013-01-29 00:34:06 +0000974//===----------------------------------------------------------------------===//
975// AttributeSet Introspection Methods
976//===----------------------------------------------------------------------===//
977
978/// \brief Return the number of slots used in this attribute list. This is the
979/// number of arguments that have an attribute set on them (including the
980/// function itself).
981unsigned AttributeSet::getNumSlots() const {
982 return pImpl ? pImpl->getNumAttributes() : 0;
983}
984
Rafael Espindoladd275302013-04-30 16:53:38 +0000985unsigned AttributeSet::getSlotIndex(unsigned Slot) const {
Bill Wendlingd509a662013-01-29 00:34:06 +0000986 assert(pImpl && Slot < pImpl->getNumAttributes() &&
987 "Slot # out of range!");
988 return pImpl->getSlotIndex(Slot);
989}
990
991AttributeSet AttributeSet::getSlotAttributes(unsigned Slot) const {
992 assert(pImpl && Slot < pImpl->getNumAttributes() &&
993 "Slot # out of range!");
994 return pImpl->getSlotAttributes(Slot);
995}
996
997uint64_t AttributeSet::Raw(unsigned Index) const {
998 // FIXME: Remove this.
999 return pImpl ? pImpl->Raw(Index) : 0;
1000}
1001
1002void AttributeSet::dump() const {
1003 dbgs() << "PAL[\n";
1004
1005 for (unsigned i = 0, e = getNumSlots(); i < e; ++i) {
1006 uint64_t Index = getSlotIndex(i);
1007 dbgs() << " { ";
1008 if (Index == ~0U)
1009 dbgs() << "~0U";
1010 else
1011 dbgs() << Index;
1012 dbgs() << " => " << getAsString(Index) << " }\n";
1013 }
1014
1015 dbgs() << "]\n";
1016}
1017
Bill Wendlingabf3feb2012-10-05 06:44:41 +00001018//===----------------------------------------------------------------------===//
Bill Wendlingcd330342013-01-04 23:27:34 +00001019// AttrBuilder Method Implementations
Bill Wendlingabf3feb2012-10-05 06:44:41 +00001020//===----------------------------------------------------------------------===//
1021
Bill Wendling211316c2013-04-18 20:17:28 +00001022AttrBuilder::AttrBuilder(AttributeSet AS, unsigned Index)
Hal Finkelb0407ba2014-07-18 15:51:28 +00001023 : Attrs(0), Alignment(0), StackAlignment(0), DerefBytes(0) {
Bill Wendling56b0b2a2013-01-27 21:23:46 +00001024 AttributeSetImpl *pImpl = AS.pImpl;
Bill Wendling096f5442013-01-07 08:24:35 +00001025 if (!pImpl) return;
1026
Bill Wendling9eb689c2013-01-28 00:21:34 +00001027 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I) {
Bill Wendling211316c2013-04-18 20:17:28 +00001028 if (pImpl->getSlotIndex(I) != Index) continue;
Bill Wendling096f5442013-01-07 08:24:35 +00001029
Benjamin Kramer741146b2013-07-11 12:13:16 +00001030 for (AttributeSetImpl::iterator II = pImpl->begin(I),
Bill Wendling9eb689c2013-01-28 00:21:34 +00001031 IE = pImpl->end(I); II != IE; ++II)
Bill Wendling23804da2013-01-31 23:38:01 +00001032 addAttribute(*II);
Bill Wendling9eb689c2013-01-28 00:21:34 +00001033
1034 break;
1035 }
Bill Wendling096f5442013-01-07 08:24:35 +00001036}
1037
Bill Wendlingcd330342013-01-04 23:27:34 +00001038void AttrBuilder::clear() {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001039 Attrs.reset();
Hal Finkelb0407ba2014-07-18 15:51:28 +00001040 Alignment = StackAlignment = DerefBytes = 0;
Bill Wendlingcd330342013-01-04 23:27:34 +00001041}
1042
1043AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001044 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
Bill Wendling1c7cc8a2013-01-31 23:16:25 +00001045 assert(Val != Attribute::Alignment && Val != Attribute::StackAlignment &&
Hal Finkelb0407ba2014-07-18 15:51:28 +00001046 Val != Attribute::Dereferenceable &&
1047 "Adding integer attribute without adding a value!");
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001048 Attrs[Val] = true;
Bill Wendling7c04e042012-10-09 19:01:18 +00001049 return *this;
Bill Wendlingabf3feb2012-10-05 06:44:41 +00001050}
1051
Bill Wendling23804da2013-01-31 23:38:01 +00001052AttrBuilder &AttrBuilder::addAttribute(Attribute Attr) {
Bill Wendling0a437302013-02-10 10:13:23 +00001053 if (Attr.isStringAttribute()) {
1054 addAttribute(Attr.getKindAsString(), Attr.getValueAsString());
1055 return *this;
1056 }
1057
Bill Wendling3f12ac22013-02-05 22:37:24 +00001058 Attribute::AttrKind Kind = Attr.getKindAsEnum();
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001059 Attrs[Kind] = true;
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001060
Bill Wendling3f12ac22013-02-05 22:37:24 +00001061 if (Kind == Attribute::Alignment)
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001062 Alignment = Attr.getAlignment();
Bill Wendling3f12ac22013-02-05 22:37:24 +00001063 else if (Kind == Attribute::StackAlignment)
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001064 StackAlignment = Attr.getStackAlignment();
Hal Finkelb0407ba2014-07-18 15:51:28 +00001065 else if (Kind == Attribute::Dereferenceable)
1066 DerefBytes = Attr.getDereferenceableBytes();
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001067 return *this;
1068}
1069
Bill Wendlingb9c5b1a2013-02-05 08:09:32 +00001070AttrBuilder &AttrBuilder::addAttribute(StringRef A, StringRef V) {
1071 TargetDepAttrs[A] = V;
1072 return *this;
1073}
1074
Bill Wendling23804da2013-01-31 23:38:01 +00001075AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001076 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
1077 Attrs[Val] = false;
Bill Wendling23804da2013-01-31 23:38:01 +00001078
1079 if (Val == Attribute::Alignment)
1080 Alignment = 0;
1081 else if (Val == Attribute::StackAlignment)
1082 StackAlignment = 0;
Hal Finkelb0407ba2014-07-18 15:51:28 +00001083 else if (Val == Attribute::Dereferenceable)
1084 DerefBytes = 0;
Bill Wendling23804da2013-01-31 23:38:01 +00001085
1086 return *this;
1087}
1088
Bill Wendlingd2196752013-01-30 23:07:40 +00001089AttrBuilder &AttrBuilder::removeAttributes(AttributeSet A, uint64_t Index) {
Bill Wendling211316c2013-04-18 20:17:28 +00001090 unsigned Slot = ~0U;
Bill Wendlingf1c94e32013-02-01 00:13:50 +00001091 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
1092 if (A.getSlotIndex(I) == Index) {
Bill Wendling211316c2013-04-18 20:17:28 +00001093 Slot = I;
Bill Wendlingf1c94e32013-02-01 00:13:50 +00001094 break;
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001095 }
Bill Wendlingf1c94e32013-02-01 00:13:50 +00001096
Bill Wendling211316c2013-04-18 20:17:28 +00001097 assert(Slot != ~0U && "Couldn't find index in AttributeSet!");
Bill Wendlingf1c94e32013-02-01 00:13:50 +00001098
Bill Wendling211316c2013-04-18 20:17:28 +00001099 for (AttributeSet::iterator I = A.begin(Slot), E = A.end(Slot); I != E; ++I) {
Bill Wendling7cde51d2013-02-12 07:56:49 +00001100 Attribute Attr = *I;
Hal Finkele15442c2014-07-18 06:51:55 +00001101 if (Attr.isEnumAttribute() || Attr.isIntAttribute()) {
Bill Wendling7cde51d2013-02-12 07:56:49 +00001102 Attribute::AttrKind Kind = I->getKindAsEnum();
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001103 Attrs[Kind] = false;
Bill Wendlingf1c94e32013-02-01 00:13:50 +00001104
Bill Wendling7cde51d2013-02-12 07:56:49 +00001105 if (Kind == Attribute::Alignment)
1106 Alignment = 0;
1107 else if (Kind == Attribute::StackAlignment)
1108 StackAlignment = 0;
Hal Finkelb0407ba2014-07-18 15:51:28 +00001109 else if (Kind == Attribute::Dereferenceable)
1110 DerefBytes = 0;
Bill Wendling7cde51d2013-02-12 07:56:49 +00001111 } else {
1112 assert(Attr.isStringAttribute() && "Invalid attribute type!");
1113 std::map<std::string, std::string>::iterator
1114 Iter = TargetDepAttrs.find(Attr.getKindAsString());
1115 if (Iter != TargetDepAttrs.end())
1116 TargetDepAttrs.erase(Iter);
1117 }
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001118 }
1119
1120 return *this;
1121}
1122
Bill Wendlingb9c5b1a2013-02-05 08:09:32 +00001123AttrBuilder &AttrBuilder::removeAttribute(StringRef A) {
1124 std::map<std::string, std::string>::iterator I = TargetDepAttrs.find(A);
1125 if (I != TargetDepAttrs.end())
1126 TargetDepAttrs.erase(I);
1127 return *this;
1128}
1129
Bill Wendling50d27842012-10-15 20:35:56 +00001130AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
Bill Wendlingabd5ba22012-10-14 03:58:29 +00001131 if (Align == 0) return *this;
Bill Wendlingcd330342013-01-04 23:27:34 +00001132
Bill Wendlingabf3feb2012-10-05 06:44:41 +00001133 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1134 assert(Align <= 0x40000000 && "Alignment too large.");
Bill Wendlingcd330342013-01-04 23:27:34 +00001135
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001136 Attrs[Attribute::Alignment] = true;
Bill Wendlingcd330342013-01-04 23:27:34 +00001137 Alignment = Align;
Bill Wendlingabd5ba22012-10-14 03:58:29 +00001138 return *this;
Chris Lattner3e13b8c2008-01-02 23:42:30 +00001139}
1140
Bill Wendlingcd330342013-01-04 23:27:34 +00001141AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
1142 // Default alignment, allow the target to define how to align it.
1143 if (Align == 0) return *this;
1144
1145 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1146 assert(Align <= 0x100 && "Alignment too large.");
1147
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001148 Attrs[Attribute::StackAlignment] = true;
Bill Wendlingcd330342013-01-04 23:27:34 +00001149 StackAlignment = Align;
1150 return *this;
1151}
1152
Hal Finkelb0407ba2014-07-18 15:51:28 +00001153AttrBuilder &AttrBuilder::addDereferenceableAttr(uint64_t Bytes) {
1154 if (Bytes == 0) return *this;
1155
1156 Attrs[Attribute::Dereferenceable] = true;
1157 DerefBytes = Bytes;
1158 return *this;
1159}
1160
Bill Wendlinge2614922013-02-06 01:16:00 +00001161AttrBuilder &AttrBuilder::merge(const AttrBuilder &B) {
1162 // FIXME: What if both have alignments, but they don't match?!
1163 if (!Alignment)
1164 Alignment = B.Alignment;
1165
1166 if (!StackAlignment)
1167 StackAlignment = B.StackAlignment;
1168
Hal Finkelb0407ba2014-07-18 15:51:28 +00001169 if (!DerefBytes)
1170 DerefBytes = B.DerefBytes;
1171
Benjamin Kramer45e7d532013-02-16 19:13:18 +00001172 Attrs |= B.Attrs;
Bill Wendlinge2614922013-02-06 01:16:00 +00001173
1174 for (td_const_iterator I = B.TargetDepAttrs.begin(),
1175 E = B.TargetDepAttrs.end(); I != E; ++I)
1176 TargetDepAttrs[I->first] = I->second;
1177
1178 return *this;
1179}
1180
Bill Wendling4b001442013-02-06 01:33:42 +00001181bool AttrBuilder::contains(StringRef A) const {
1182 return TargetDepAttrs.find(A) != TargetDepAttrs.end();
1183}
1184
Bill Wendling50d27842012-10-15 20:35:56 +00001185bool AttrBuilder::hasAttributes() const {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001186 return !Attrs.none() || !TargetDepAttrs.empty();
Bill Wendlingc6daefa2012-10-08 23:27:46 +00001187}
Bill Wendling9ac69f92013-01-04 20:54:35 +00001188
Bill Wendlingd2196752013-01-30 23:07:40 +00001189bool AttrBuilder::hasAttributes(AttributeSet A, uint64_t Index) const {
Bill Wendling211316c2013-04-18 20:17:28 +00001190 unsigned Slot = ~0U;
Bill Wendling9ca01da2013-02-02 00:42:06 +00001191 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
1192 if (A.getSlotIndex(I) == Index) {
Bill Wendling211316c2013-04-18 20:17:28 +00001193 Slot = I;
Bill Wendling9ca01da2013-02-02 00:42:06 +00001194 break;
1195 }
1196
Bill Wendling211316c2013-04-18 20:17:28 +00001197 assert(Slot != ~0U && "Couldn't find the index!");
Bill Wendling9ca01da2013-02-02 00:42:06 +00001198
Bill Wendling211316c2013-04-18 20:17:28 +00001199 for (AttributeSet::iterator I = A.begin(Slot), E = A.end(Slot);
Bill Wendling7cde51d2013-02-12 07:56:49 +00001200 I != E; ++I) {
1201 Attribute Attr = *I;
Hal Finkele15442c2014-07-18 06:51:55 +00001202 if (Attr.isEnumAttribute() || Attr.isIntAttribute()) {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001203 if (Attrs[I->getKindAsEnum()])
Bill Wendling7cde51d2013-02-12 07:56:49 +00001204 return true;
1205 } else {
1206 assert(Attr.isStringAttribute() && "Invalid attribute kind!");
1207 return TargetDepAttrs.find(Attr.getKindAsString())!=TargetDepAttrs.end();
1208 }
1209 }
Bill Wendling9ca01da2013-02-02 00:42:06 +00001210
1211 return false;
Bill Wendling70f39172012-10-09 00:01:21 +00001212}
Bill Wendling9ac69f92013-01-04 20:54:35 +00001213
Bill Wendling50d27842012-10-15 20:35:56 +00001214bool AttrBuilder::hasAlignmentAttr() const {
Bill Wendlingcd330342013-01-04 23:27:34 +00001215 return Alignment != 0;
Bill Wendlingc6daefa2012-10-08 23:27:46 +00001216}
1217
Bill Wendlingd509a662013-01-29 00:34:06 +00001218bool AttrBuilder::operator==(const AttrBuilder &B) {
Benjamin Kramer45e7d532013-02-16 19:13:18 +00001219 if (Attrs != B.Attrs)
1220 return false;
Bill Wendling4b001442013-02-06 01:33:42 +00001221
1222 for (td_const_iterator I = TargetDepAttrs.begin(),
1223 E = TargetDepAttrs.end(); I != E; ++I)
1224 if (B.TargetDepAttrs.find(I->first) == B.TargetDepAttrs.end())
1225 return false;
1226
Hal Finkelb0407ba2014-07-18 15:51:28 +00001227 return Alignment == B.Alignment && StackAlignment == B.StackAlignment &&
1228 DerefBytes == B.DerefBytes;
Bill Wendlingd509a662013-01-29 00:34:06 +00001229}
1230
1231AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) {
Bill Wendling56aeccc2013-02-04 23:32:23 +00001232 // FIXME: Remove this in 4.0.
Bill Wendling60011b82013-01-29 01:43:29 +00001233 if (!Val) return *this;
1234
Bill Wendlingd509a662013-01-29 00:34:06 +00001235 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
1236 I = Attribute::AttrKind(I + 1)) {
Hal Finkelb0407ba2014-07-18 15:51:28 +00001237 if (I == Attribute::Dereferenceable)
1238 continue;
Bill Wendlingd509a662013-01-29 00:34:06 +00001239 if (uint64_t A = (Val & AttributeImpl::getAttrMask(I))) {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001240 Attrs[I] = true;
Bill Wendlingd509a662013-01-29 00:34:06 +00001241
1242 if (I == Attribute::Alignment)
1243 Alignment = 1ULL << ((A >> 16) - 1);
1244 else if (I == Attribute::StackAlignment)
1245 StackAlignment = 1ULL << ((A >> 26)-1);
1246 }
1247 }
1248
1249 return *this;
1250}
1251
Bill Wendling57625a42013-01-25 23:09:36 +00001252//===----------------------------------------------------------------------===//
1253// AttributeFuncs Function Defintions
1254//===----------------------------------------------------------------------===//
1255
Bill Wendlingc79cdff2013-02-01 01:04:27 +00001256/// \brief Which attributes cannot be applied to a type.
Bill Wendlingd2196752013-01-30 23:07:40 +00001257AttributeSet AttributeFuncs::typeIncompatible(Type *Ty, uint64_t Index) {
Bill Wendling57625a42013-01-25 23:09:36 +00001258 AttrBuilder Incompatible;
1259
1260 if (!Ty->isIntegerTy())
1261 // Attribute that only apply to integers.
1262 Incompatible.addAttribute(Attribute::SExt)
1263 .addAttribute(Attribute::ZExt);
1264
1265 if (!Ty->isPointerTy())
1266 // Attribute that only apply to pointers.
1267 Incompatible.addAttribute(Attribute::ByVal)
1268 .addAttribute(Attribute::Nest)
1269 .addAttribute(Attribute::NoAlias)
1270 .addAttribute(Attribute::NoCapture)
Nick Lewyckyd52b1522014-05-20 01:23:40 +00001271 .addAttribute(Attribute::NonNull)
Hal Finkelb0407ba2014-07-18 15:51:28 +00001272 .addDereferenceableAttr(1) // the int here is ignored
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001273 .addAttribute(Attribute::ReadNone)
1274 .addAttribute(Attribute::ReadOnly)
Reid Klecknera534a382013-12-19 02:14:12 +00001275 .addAttribute(Attribute::StructRet)
1276 .addAttribute(Attribute::InAlloca);
Bill Wendling57625a42013-01-25 23:09:36 +00001277
Bill Wendlingd2196752013-01-30 23:07:40 +00001278 return AttributeSet::get(Ty->getContext(), Index, Incompatible);
Bill Wendling57625a42013-01-25 23:09:36 +00001279}