blob: 7c9468cb1589f003a2f42f478ffabd46a510dde3 [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,
35 uint64_t Val) {
36 LLVMContextImpl *pImpl = Context.pImpl;
37 FoldingSetNodeID ID;
38 ID.AddInteger(Kind);
Matt Arsenaultfb4308e2014-09-03 23:38:05 +000039 if (Val) ID.AddInteger(Val);
Matt Arsenaulte1cef6e2014-09-03 23:24:31 +000040
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.
Matt Arsenaultfb4308e2014-09-03 23:38:05 +000047 if (!Val)
48 PA = new EnumAttributeImpl(Kind);
49 else
50 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
Sanjoy Das31ea6d12015-04-16 20:29:50 +000097Attribute Attribute::getWithDereferenceableOrNullBytes(LLVMContext &Context,
98 uint64_t Bytes) {
99 assert(Bytes && "Bytes must be non-zero.");
100 return get(Context, DereferenceableOrNull, Bytes);
101}
102
Bill Wendling7707c5a2013-01-29 00:48:16 +0000103//===----------------------------------------------------------------------===//
104// Attribute Accessor Methods
105//===----------------------------------------------------------------------===//
106
Bill Wendling3f12ac22013-02-05 22:37:24 +0000107bool Attribute::isEnumAttribute() const {
108 return pImpl && pImpl->isEnumAttribute();
109}
110
Hal Finkele15442c2014-07-18 06:51:55 +0000111bool Attribute::isIntAttribute() const {
112 return pImpl && pImpl->isIntAttribute();
Bill Wendling3f12ac22013-02-05 22:37:24 +0000113}
114
115bool Attribute::isStringAttribute() const {
116 return pImpl && pImpl->isStringAttribute();
117}
118
119Attribute::AttrKind Attribute::getKindAsEnum() const {
Bill Wendling440e9d82013-07-25 00:34:29 +0000120 if (!pImpl) return None;
Hal Finkele15442c2014-07-18 06:51:55 +0000121 assert((isEnumAttribute() || isIntAttribute()) &&
Bill Wendling3f12ac22013-02-05 22:37:24 +0000122 "Invalid attribute type to get the kind as an enum!");
123 return pImpl ? pImpl->getKindAsEnum() : None;
124}
125
126uint64_t Attribute::getValueAsInt() const {
Bill Wendling440e9d82013-07-25 00:34:29 +0000127 if (!pImpl) return 0;
Hal Finkele15442c2014-07-18 06:51:55 +0000128 assert(isIntAttribute() &&
129 "Expected the attribute to be an integer attribute!");
Bill Wendling3f12ac22013-02-05 22:37:24 +0000130 return pImpl ? pImpl->getValueAsInt() : 0;
131}
132
133StringRef Attribute::getKindAsString() const {
Bill Wendling440e9d82013-07-25 00:34:29 +0000134 if (!pImpl) return StringRef();
Bill Wendling3f12ac22013-02-05 22:37:24 +0000135 assert(isStringAttribute() &&
136 "Invalid attribute type to get the kind as a string!");
137 return pImpl ? pImpl->getKindAsString() : StringRef();
138}
139
140StringRef Attribute::getValueAsString() const {
Bill Wendling440e9d82013-07-25 00:34:29 +0000141 if (!pImpl) return StringRef();
Bill Wendling3f12ac22013-02-05 22:37:24 +0000142 assert(isStringAttribute() &&
143 "Invalid attribute type to get the value as a string!");
144 return pImpl ? pImpl->getValueAsString() : StringRef();
145}
146
Bill Wendlingae89a0f2013-02-05 23:48:36 +0000147bool Attribute::hasAttribute(AttrKind Kind) const {
148 return (pImpl && pImpl->hasAttribute(Kind)) || (!pImpl && Kind == None);
149}
150
151bool Attribute::hasAttribute(StringRef Kind) const {
152 if (!isStringAttribute()) return false;
153 return pImpl && pImpl->hasAttribute(Kind);
Bill Wendling03eefb32013-01-29 20:45:34 +0000154}
155
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000156/// This returns the alignment field of an attribute as a byte alignment value.
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000157unsigned Attribute::getAlignment() const {
Bill Wendlingc79cdff2013-02-01 01:04:27 +0000158 assert(hasAttribute(Attribute::Alignment) &&
159 "Trying to get alignment from non-alignment attribute!");
Bill Wendling3f12ac22013-02-05 22:37:24 +0000160 return pImpl->getValueAsInt();
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000161}
162
163/// This returns the stack alignment field of an attribute as a byte alignment
164/// value.
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000165unsigned Attribute::getStackAlignment() const {
Bill Wendlingc79cdff2013-02-01 01:04:27 +0000166 assert(hasAttribute(Attribute::StackAlignment) &&
167 "Trying to get alignment from non-alignment attribute!");
Bill Wendling3f12ac22013-02-05 22:37:24 +0000168 return pImpl->getValueAsInt();
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000169}
170
Hal Finkelb0407ba2014-07-18 15:51:28 +0000171/// This returns the number of dereferenceable bytes.
172uint64_t Attribute::getDereferenceableBytes() const {
173 assert(hasAttribute(Attribute::Dereferenceable) &&
174 "Trying to get dereferenceable bytes from "
175 "non-dereferenceable attribute!");
176 return pImpl->getValueAsInt();
177}
178
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000179uint64_t Attribute::getDereferenceableOrNullBytes() const {
180 assert(hasAttribute(Attribute::DereferenceableOrNull) &&
181 "Trying to get dereferenceable bytes from "
182 "non-dereferenceable attribute!");
183 return pImpl->getValueAsInt();
184}
185
Bill Wendling829b4782013-02-11 08:43:33 +0000186std::string Attribute::getAsString(bool InAttrGrp) const {
Bill Wendling9c2eba92013-01-31 20:59:05 +0000187 if (!pImpl) return "";
188
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000189 if (hasAttribute(Attribute::SanitizeAddress))
190 return "sanitize_address";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000191 if (hasAttribute(Attribute::AlwaysInline))
192 return "alwaysinline";
Michael Gottesman41748d72013-06-27 00:25:01 +0000193 if (hasAttribute(Attribute::Builtin))
194 return "builtin";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000195 if (hasAttribute(Attribute::ByVal))
196 return "byval";
Reid Klecknera534a382013-12-19 02:14:12 +0000197 if (hasAttribute(Attribute::InAlloca))
198 return "inalloca";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000199 if (hasAttribute(Attribute::InlineHint))
200 return "inlinehint";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000201 if (hasAttribute(Attribute::InReg))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000202 return "inreg";
Tom Roeder44cb65f2014-06-05 19:29:43 +0000203 if (hasAttribute(Attribute::JumpTable))
204 return "jumptable";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000205 if (hasAttribute(Attribute::MinSize))
206 return "minsize";
207 if (hasAttribute(Attribute::Naked))
208 return "naked";
209 if (hasAttribute(Attribute::Nest))
210 return "nest";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000211 if (hasAttribute(Attribute::NoAlias))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000212 return "noalias";
Bill Wendling09bd1f72013-02-22 00:12:35 +0000213 if (hasAttribute(Attribute::NoBuiltin))
214 return "nobuiltin";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000215 if (hasAttribute(Attribute::NoCapture))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000216 return "nocapture";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000217 if (hasAttribute(Attribute::NoDuplicate))
218 return "noduplicate";
219 if (hasAttribute(Attribute::NoImplicitFloat))
220 return "noimplicitfloat";
221 if (hasAttribute(Attribute::NoInline))
222 return "noinline";
223 if (hasAttribute(Attribute::NonLazyBind))
224 return "nonlazybind";
Nick Lewyckyd52b1522014-05-20 01:23:40 +0000225 if (hasAttribute(Attribute::NonNull))
226 return "nonnull";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000227 if (hasAttribute(Attribute::NoRedZone))
228 return "noredzone";
229 if (hasAttribute(Attribute::NoReturn))
230 return "noreturn";
231 if (hasAttribute(Attribute::NoUnwind))
232 return "nounwind";
Andrea Di Biagio377496b2013-08-23 11:53:55 +0000233 if (hasAttribute(Attribute::OptimizeNone))
234 return "optnone";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000235 if (hasAttribute(Attribute::OptimizeForSize))
236 return "optsize";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000237 if (hasAttribute(Attribute::ReadNone))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000238 return "readnone";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000239 if (hasAttribute(Attribute::ReadOnly))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000240 return "readonly";
Stephen Linb8bd2322013-04-20 05:14:40 +0000241 if (hasAttribute(Attribute::Returned))
242 return "returned";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000243 if (hasAttribute(Attribute::ReturnsTwice))
244 return "returns_twice";
245 if (hasAttribute(Attribute::SExt))
246 return "signext";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000247 if (hasAttribute(Attribute::StackProtect))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000248 return "ssp";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000249 if (hasAttribute(Attribute::StackProtectReq))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000250 return "sspreq";
Bill Wendlingd154e2832013-01-23 06:41:41 +0000251 if (hasAttribute(Attribute::StackProtectStrong))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000252 return "sspstrong";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000253 if (hasAttribute(Attribute::StructRet))
254 return "sret";
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000255 if (hasAttribute(Attribute::SanitizeThread))
256 return "sanitize_thread";
257 if (hasAttribute(Attribute::SanitizeMemory))
258 return "sanitize_memory";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000259 if (hasAttribute(Attribute::UWTable))
260 return "uwtable";
261 if (hasAttribute(Attribute::ZExt))
262 return "zeroext";
Diego Novilloc6399532013-05-24 12:26:52 +0000263 if (hasAttribute(Attribute::Cold))
264 return "cold";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000265
266 // FIXME: These should be output like this:
267 //
268 // align=4
269 // alignstack=8
270 //
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000271 if (hasAttribute(Attribute::Alignment)) {
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000272 std::string Result;
Bill Wendling829b4782013-02-11 08:43:33 +0000273 Result += "align";
274 Result += (InAttrGrp) ? "=" : " ";
Bill Wendling3f12ac22013-02-05 22:37:24 +0000275 Result += utostr(getValueAsInt());
276 return Result;
277 }
Bill Wendling829b4782013-02-11 08:43:33 +0000278
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000279 auto AttrWithBytesToString = [&](const char *Name) {
Bill Wendling3f12ac22013-02-05 22:37:24 +0000280 std::string Result;
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000281 Result += Name;
Bill Wendling829b4782013-02-11 08:43:33 +0000282 if (InAttrGrp) {
283 Result += "=";
284 Result += utostr(getValueAsInt());
285 } else {
286 Result += "(";
287 Result += utostr(getValueAsInt());
288 Result += ")";
289 }
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000290 return Result;
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000291 };
Bill Wendling9c2eba92013-01-31 20:59:05 +0000292
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000293 if (hasAttribute(Attribute::StackAlignment))
294 return AttrWithBytesToString("alignstack");
295
296 if (hasAttribute(Attribute::Dereferenceable))
297 return AttrWithBytesToString("dereferenceable");
298
299 if (hasAttribute(Attribute::DereferenceableOrNull))
300 return AttrWithBytesToString("dereferenceable_or_null");
Hal Finkelb0407ba2014-07-18 15:51:28 +0000301
Bill Wendling9c2eba92013-01-31 20:59:05 +0000302 // Convert target-dependent attributes to strings of the form:
303 //
304 // "kind"
305 // "kind" = "value"
Bill Wendling9c2eba92013-01-31 20:59:05 +0000306 //
Bill Wendling3f12ac22013-02-05 22:37:24 +0000307 if (isStringAttribute()) {
Bill Wendling9c2eba92013-01-31 20:59:05 +0000308 std::string Result;
Yaron Keren075759a2015-03-30 15:42:36 +0000309 Result += (Twine('"') + getKindAsString() + Twine('"')).str();
Bill Wendling9c2eba92013-01-31 20:59:05 +0000310
Bill Wendling3f12ac22013-02-05 22:37:24 +0000311 StringRef Val = pImpl->getValueAsString();
312 if (Val.empty()) return Result;
Bill Wendling7a33f772013-02-01 22:32:30 +0000313
Yaron Keren075759a2015-03-30 15:42:36 +0000314 Result += ("=\"" + Val + Twine('"')).str();
Bill Wendlingc79cdff2013-02-01 01:04:27 +0000315 return Result;
Bill Wendling9c2eba92013-01-31 20:59:05 +0000316 }
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000317
318 llvm_unreachable("Unknown attribute");
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000319}
320
Bill Wendlingd509a662013-01-29 00:34:06 +0000321bool Attribute::operator<(Attribute A) const {
322 if (!pImpl && !A.pImpl) return false;
323 if (!pImpl) return true;
324 if (!A.pImpl) return false;
325 return *pImpl < *A.pImpl;
326}
327
Bill Wendlingd509a662013-01-29 00:34:06 +0000328//===----------------------------------------------------------------------===//
329// AttributeImpl Definition
330//===----------------------------------------------------------------------===//
331
Eric Christopher0eaa5412014-07-02 22:05:40 +0000332// Pin the vtables to this file.
Alexey Samsonov49109a22013-11-18 09:31:53 +0000333AttributeImpl::~AttributeImpl() {}
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +0000334void EnumAttributeImpl::anchor() {}
Hal Finkele15442c2014-07-18 06:51:55 +0000335void IntAttributeImpl::anchor() {}
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +0000336void StringAttributeImpl::anchor() {}
Alexey Samsonov49109a22013-11-18 09:31:53 +0000337
Bill Wendlingd509a662013-01-29 00:34:06 +0000338bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
Bill Wendling3f12ac22013-02-05 22:37:24 +0000339 if (isStringAttribute()) return false;
340 return getKindAsEnum() == A;
Bill Wendlingd509a662013-01-29 00:34:06 +0000341}
342
Bill Wendling3f12ac22013-02-05 22:37:24 +0000343bool AttributeImpl::hasAttribute(StringRef Kind) const {
344 if (!isStringAttribute()) return false;
345 return getKindAsString() == Kind;
Bill Wendlingd509a662013-01-29 00:34:06 +0000346}
347
Bill Wendling3f12ac22013-02-05 22:37:24 +0000348Attribute::AttrKind AttributeImpl::getKindAsEnum() const {
Hal Finkele15442c2014-07-18 06:51:55 +0000349 assert(isEnumAttribute() || isIntAttribute());
Benjamin Kramer741146b2013-07-11 12:13:16 +0000350 return static_cast<const EnumAttributeImpl *>(this)->getEnumKind();
Bill Wendlingd509a662013-01-29 00:34:06 +0000351}
352
Bill Wendling3f12ac22013-02-05 22:37:24 +0000353uint64_t AttributeImpl::getValueAsInt() const {
Hal Finkele15442c2014-07-18 06:51:55 +0000354 assert(isIntAttribute());
355 return static_cast<const IntAttributeImpl *>(this)->getValue();
Bill Wendlingd509a662013-01-29 00:34:06 +0000356}
357
Bill Wendling3f12ac22013-02-05 22:37:24 +0000358StringRef AttributeImpl::getKindAsString() const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000359 assert(isStringAttribute());
360 return static_cast<const StringAttributeImpl *>(this)->getStringKind();
Bill Wendlingd509a662013-01-29 00:34:06 +0000361}
362
Bill Wendling3f12ac22013-02-05 22:37:24 +0000363StringRef AttributeImpl::getValueAsString() const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000364 assert(isStringAttribute());
365 return static_cast<const StringAttributeImpl *>(this)->getStringValue();
Bill Wendlingd509a662013-01-29 00:34:06 +0000366}
367
368bool AttributeImpl::operator<(const AttributeImpl &AI) const {
Bill Wendlingc79cdff2013-02-01 01:04:27 +0000369 // This sorts the attributes with Attribute::AttrKinds coming first (sorted
370 // relative to their enum value) and then strings.
Bill Wendling26b95752013-02-15 05:25:26 +0000371 if (isEnumAttribute()) {
372 if (AI.isEnumAttribute()) return getKindAsEnum() < AI.getKindAsEnum();
Hal Finkele15442c2014-07-18 06:51:55 +0000373 if (AI.isIntAttribute()) return true;
Bill Wendling26b95752013-02-15 05:25:26 +0000374 if (AI.isStringAttribute()) return true;
375 }
Bill Wendlingc79cdff2013-02-01 01:04:27 +0000376
Hal Finkele15442c2014-07-18 06:51:55 +0000377 if (isIntAttribute()) {
Bill Wendling26b95752013-02-15 05:25:26 +0000378 if (AI.isEnumAttribute()) return false;
Hal Finkele15442c2014-07-18 06:51:55 +0000379 if (AI.isIntAttribute()) return getValueAsInt() < AI.getValueAsInt();
Bill Wendling26b95752013-02-15 05:25:26 +0000380 if (AI.isStringAttribute()) return true;
Bill Wendling3f12ac22013-02-05 22:37:24 +0000381 }
Bill Wendlingd509a662013-01-29 00:34:06 +0000382
Bill Wendling26b95752013-02-15 05:25:26 +0000383 if (AI.isEnumAttribute()) return false;
Hal Finkele15442c2014-07-18 06:51:55 +0000384 if (AI.isIntAttribute()) return false;
Bill Wendling26b95752013-02-15 05:25:26 +0000385 if (getKindAsString() == AI.getKindAsString())
386 return getValueAsString() < AI.getValueAsString();
387 return getKindAsString() < AI.getKindAsString();
Bill Wendlingd509a662013-01-29 00:34:06 +0000388}
389
Bill Wendlingd509a662013-01-29 00:34:06 +0000390uint64_t AttributeImpl::getAttrMask(Attribute::AttrKind Val) {
391 // FIXME: Remove this.
392 switch (Val) {
393 case Attribute::EndAttrKinds:
Bill Wendlingd509a662013-01-29 00:34:06 +0000394 llvm_unreachable("Synthetic enumerators which should never get here");
395
396 case Attribute::None: return 0;
397 case Attribute::ZExt: return 1 << 0;
398 case Attribute::SExt: return 1 << 1;
399 case Attribute::NoReturn: return 1 << 2;
400 case Attribute::InReg: return 1 << 3;
401 case Attribute::StructRet: return 1 << 4;
402 case Attribute::NoUnwind: return 1 << 5;
403 case Attribute::NoAlias: return 1 << 6;
404 case Attribute::ByVal: return 1 << 7;
405 case Attribute::Nest: return 1 << 8;
406 case Attribute::ReadNone: return 1 << 9;
407 case Attribute::ReadOnly: return 1 << 10;
408 case Attribute::NoInline: return 1 << 11;
409 case Attribute::AlwaysInline: return 1 << 12;
410 case Attribute::OptimizeForSize: return 1 << 13;
411 case Attribute::StackProtect: return 1 << 14;
412 case Attribute::StackProtectReq: return 1 << 15;
413 case Attribute::Alignment: return 31 << 16;
414 case Attribute::NoCapture: return 1 << 21;
415 case Attribute::NoRedZone: return 1 << 22;
416 case Attribute::NoImplicitFloat: return 1 << 23;
417 case Attribute::Naked: return 1 << 24;
418 case Attribute::InlineHint: return 1 << 25;
419 case Attribute::StackAlignment: return 7 << 26;
420 case Attribute::ReturnsTwice: return 1 << 29;
421 case Attribute::UWTable: return 1 << 30;
422 case Attribute::NonLazyBind: return 1U << 31;
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000423 case Attribute::SanitizeAddress: return 1ULL << 32;
Bill Wendlingd509a662013-01-29 00:34:06 +0000424 case Attribute::MinSize: return 1ULL << 33;
425 case Attribute::NoDuplicate: return 1ULL << 34;
426 case Attribute::StackProtectStrong: return 1ULL << 35;
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000427 case Attribute::SanitizeThread: return 1ULL << 36;
428 case Attribute::SanitizeMemory: return 1ULL << 37;
Bill Wendling7bfdfe72013-02-22 00:40:12 +0000429 case Attribute::NoBuiltin: return 1ULL << 38;
Stephen Linb8bd2322013-04-20 05:14:40 +0000430 case Attribute::Returned: return 1ULL << 39;
Diego Novilloc6399532013-05-24 12:26:52 +0000431 case Attribute::Cold: return 1ULL << 40;
Michael Gottesman41748d72013-06-27 00:25:01 +0000432 case Attribute::Builtin: return 1ULL << 41;
Andrea Di Biagio377496b2013-08-23 11:53:55 +0000433 case Attribute::OptimizeNone: return 1ULL << 42;
Reid Klecknera534a382013-12-19 02:14:12 +0000434 case Attribute::InAlloca: return 1ULL << 43;
Nick Lewyckyd52b1522014-05-20 01:23:40 +0000435 case Attribute::NonNull: return 1ULL << 44;
Tom Roeder44cb65f2014-06-05 19:29:43 +0000436 case Attribute::JumpTable: return 1ULL << 45;
Hal Finkelb0407ba2014-07-18 15:51:28 +0000437 case Attribute::Dereferenceable:
438 llvm_unreachable("dereferenceable attribute not supported in raw format");
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000439 break;
440 case Attribute::DereferenceableOrNull:
441 llvm_unreachable("dereferenceable_or_null attribute not supported in raw "
442 "format");
443 break;
Bill Wendling25342e12013-02-22 00:50:09 +0000444 }
445 llvm_unreachable("Unsupported attribute type");
Bill Wendlingd509a662013-01-29 00:34:06 +0000446}
447
448//===----------------------------------------------------------------------===//
449// AttributeSetNode Definition
450//===----------------------------------------------------------------------===//
451
452AttributeSetNode *AttributeSetNode::get(LLVMContext &C,
453 ArrayRef<Attribute> Attrs) {
454 if (Attrs.empty())
Craig Topperc6207612014-04-09 06:08:46 +0000455 return nullptr;
Bill Wendlingd509a662013-01-29 00:34:06 +0000456
457 // Otherwise, build a key to look up the existing attributes.
458 LLVMContextImpl *pImpl = C.pImpl;
459 FoldingSetNodeID ID;
460
461 SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end());
Bill Wendling82c2ee62013-02-13 09:26:26 +0000462 array_pod_sort(SortedAttrs.begin(), SortedAttrs.end());
Bill Wendlingd509a662013-01-29 00:34:06 +0000463
464 for (SmallVectorImpl<Attribute>::iterator I = SortedAttrs.begin(),
465 E = SortedAttrs.end(); I != E; ++I)
466 I->Profile(ID);
467
468 void *InsertPoint;
469 AttributeSetNode *PA =
470 pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint);
471
472 // If we didn't find any existing attributes of the same shape then create a
473 // new one and insert it.
474 if (!PA) {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000475 // Coallocate entries after the AttributeSetNode itself.
476 void *Mem = ::operator new(sizeof(AttributeSetNode) +
477 sizeof(Attribute) * SortedAttrs.size());
478 PA = new (Mem) AttributeSetNode(SortedAttrs);
Bill Wendlingd509a662013-01-29 00:34:06 +0000479 pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint);
480 }
481
482 // Return the AttributesListNode that we found or created.
483 return PA;
484}
485
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000486bool AttributeSetNode::hasAttribute(Attribute::AttrKind Kind) const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000487 for (iterator I = begin(), E = end(); I != E; ++I)
NAKAMURA Takumib0944392013-01-31 03:47:28 +0000488 if (I->hasAttribute(Kind))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000489 return true;
490 return false;
491}
492
Bill Wendlingbce7b972013-02-13 08:42:21 +0000493bool AttributeSetNode::hasAttribute(StringRef Kind) const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000494 for (iterator I = begin(), E = end(); I != E; ++I)
Bill Wendlingbce7b972013-02-13 08:42:21 +0000495 if (I->hasAttribute(Kind))
496 return true;
497 return false;
498}
499
500Attribute AttributeSetNode::getAttribute(Attribute::AttrKind Kind) const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000501 for (iterator I = begin(), E = end(); I != E; ++I)
Bill Wendlingbce7b972013-02-13 08:42:21 +0000502 if (I->hasAttribute(Kind))
503 return *I;
504 return Attribute();
505}
506
507Attribute AttributeSetNode::getAttribute(StringRef Kind) const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000508 for (iterator I = begin(), E = end(); I != E; ++I)
Bill Wendlingbce7b972013-02-13 08:42:21 +0000509 if (I->hasAttribute(Kind))
510 return *I;
511 return Attribute();
512}
513
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000514unsigned AttributeSetNode::getAlignment() const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000515 for (iterator I = begin(), E = end(); I != E; ++I)
NAKAMURA Takumib0944392013-01-31 03:47:28 +0000516 if (I->hasAttribute(Attribute::Alignment))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000517 return I->getAlignment();
518 return 0;
519}
520
521unsigned AttributeSetNode::getStackAlignment() const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000522 for (iterator I = begin(), E = end(); I != E; ++I)
NAKAMURA Takumib0944392013-01-31 03:47:28 +0000523 if (I->hasAttribute(Attribute::StackAlignment))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000524 return I->getStackAlignment();
525 return 0;
526}
527
Hal Finkelb0407ba2014-07-18 15:51:28 +0000528uint64_t AttributeSetNode::getDereferenceableBytes() const {
529 for (iterator I = begin(), E = end(); I != E; ++I)
530 if (I->hasAttribute(Attribute::Dereferenceable))
531 return I->getDereferenceableBytes();
532 return 0;
533}
534
Sanjoy Das06cf33f2015-05-06 17:41:54 +0000535uint64_t AttributeSetNode::getDereferenceableOrNullBytes() const {
536 for (iterator I = begin(), E = end(); I != E; ++I)
537 if (I->hasAttribute(Attribute::DereferenceableOrNull))
538 return I->getDereferenceableOrNullBytes();
539 return 0;
540}
541
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +0000542std::string AttributeSetNode::getAsString(bool InAttrGrp) const {
Benjamin Kramer0baf8f42013-04-19 11:43:21 +0000543 std::string Str;
Benjamin Kramer741146b2013-07-11 12:13:16 +0000544 for (iterator I = begin(), E = end(); I != E; ++I) {
545 if (I != begin())
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +0000546 Str += ' ';
547 Str += I->getAsString(InAttrGrp);
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000548 }
549 return Str;
550}
551
Bill Wendlingd509a662013-01-29 00:34:06 +0000552//===----------------------------------------------------------------------===//
553// AttributeSetImpl Definition
554//===----------------------------------------------------------------------===//
555
Rafael Espindoladd275302013-04-30 16:53:38 +0000556uint64_t AttributeSetImpl::Raw(unsigned Index) const {
Bill Wendlingd509a662013-01-29 00:34:06 +0000557 for (unsigned I = 0, E = getNumAttributes(); I != E; ++I) {
558 if (getSlotIndex(I) != Index) continue;
Benjamin Kramer741146b2013-07-11 12:13:16 +0000559 const AttributeSetNode *ASN = getSlotNode(I);
Bill Wendling91226182013-02-02 00:52:44 +0000560 uint64_t Mask = 0;
Bill Wendlingd509a662013-01-29 00:34:06 +0000561
Benjamin Kramer741146b2013-07-11 12:13:16 +0000562 for (AttributeSetNode::iterator II = ASN->begin(),
Bill Wendling91226182013-02-02 00:52:44 +0000563 IE = ASN->end(); II != IE; ++II) {
564 Attribute Attr = *II;
Bill Wendlingd746e402013-02-10 23:18:05 +0000565
566 // This cannot handle string attributes.
567 if (Attr.isStringAttribute()) continue;
568
Bill Wendling3f12ac22013-02-05 22:37:24 +0000569 Attribute::AttrKind Kind = Attr.getKindAsEnum();
Bill Wendling91226182013-02-02 00:52:44 +0000570
Bill Wendling3f12ac22013-02-05 22:37:24 +0000571 if (Kind == Attribute::Alignment)
Bill Wendling91226182013-02-02 00:52:44 +0000572 Mask |= (Log2_32(ASN->getAlignment()) + 1) << 16;
Bill Wendling3f12ac22013-02-05 22:37:24 +0000573 else if (Kind == Attribute::StackAlignment)
Bill Wendling91226182013-02-02 00:52:44 +0000574 Mask |= (Log2_32(ASN->getStackAlignment()) + 1) << 26;
Hal Finkelb0407ba2014-07-18 15:51:28 +0000575 else if (Kind == Attribute::Dereferenceable)
576 llvm_unreachable("dereferenceable not supported in bit mask");
Bill Wendling91226182013-02-02 00:52:44 +0000577 else
Bill Wendling3f12ac22013-02-05 22:37:24 +0000578 Mask |= AttributeImpl::getAttrMask(Kind);
Bill Wendling91226182013-02-02 00:52:44 +0000579 }
580
581 return Mask;
Bill Wendlingd509a662013-01-29 00:34:06 +0000582 }
583
584 return 0;
585}
586
Peter Collingbourneabca2ec2013-08-02 22:34:30 +0000587void AttributeSetImpl::dump() const {
588 AttributeSet(const_cast<AttributeSetImpl *>(this)).dump();
589}
590
Bill Wendlingd509a662013-01-29 00:34:06 +0000591//===----------------------------------------------------------------------===//
592// AttributeSet Construction and Mutation Methods
593//===----------------------------------------------------------------------===//
594
Bill Wendling60011b82013-01-29 01:43:29 +0000595AttributeSet
596AttributeSet::getImpl(LLVMContext &C,
597 ArrayRef<std::pair<unsigned, AttributeSetNode*> > Attrs) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000598 LLVMContextImpl *pImpl = C.pImpl;
599 FoldingSetNodeID ID;
600 AttributeSetImpl::Profile(ID, Attrs);
601
602 void *InsertPoint;
603 AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
604
605 // If we didn't find any existing attributes of the same shape then
606 // create a new one and insert it.
607 if (!PA) {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000608 // Coallocate entries after the AttributeSetImpl itself.
609 void *Mem = ::operator new(sizeof(AttributeSetImpl) +
610 sizeof(std::pair<unsigned, AttributeSetNode *>) *
611 Attrs.size());
612 PA = new (Mem) AttributeSetImpl(C, Attrs);
Bill Wendlingd509a662013-01-29 00:34:06 +0000613 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
614 }
615
616 // Return the AttributesList that we found or created.
617 return AttributeSet(PA);
618}
619
620AttributeSet AttributeSet::get(LLVMContext &C,
621 ArrayRef<std::pair<unsigned, Attribute> > Attrs){
622 // If there are no attributes then return a null AttributesList pointer.
623 if (Attrs.empty())
624 return AttributeSet();
625
626#ifndef NDEBUG
627 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
628 assert((!i || Attrs[i-1].first <= Attrs[i].first) &&
629 "Misordered Attributes list!");
Bill Wendling3f12ac22013-02-05 22:37:24 +0000630 assert(!Attrs[i].second.hasAttribute(Attribute::None) &&
Bill Wendlingd509a662013-01-29 00:34:06 +0000631 "Pointless attribute!");
632 }
633#endif
634
635 // Create a vector if (unsigned, AttributeSetNode*) pairs from the attributes
636 // list.
637 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrPairVec;
638 for (ArrayRef<std::pair<unsigned, Attribute> >::iterator I = Attrs.begin(),
639 E = Attrs.end(); I != E; ) {
640 unsigned Index = I->first;
641 SmallVector<Attribute, 4> AttrVec;
NAKAMURA Takumif05d2f22013-01-29 15:18:16 +0000642 while (I != E && I->first == Index) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000643 AttrVec.push_back(I->second);
644 ++I;
645 }
646
647 AttrPairVec.push_back(std::make_pair(Index,
648 AttributeSetNode::get(C, AttrVec)));
649 }
650
651 return getImpl(C, AttrPairVec);
652}
653
654AttributeSet AttributeSet::get(LLVMContext &C,
655 ArrayRef<std::pair<unsigned,
656 AttributeSetNode*> > Attrs) {
657 // If there are no attributes then return a null AttributesList pointer.
658 if (Attrs.empty())
659 return AttributeSet();
660
661 return getImpl(C, Attrs);
662}
663
David Majnemercf63a792014-05-03 23:00:35 +0000664AttributeSet AttributeSet::get(LLVMContext &C, unsigned Index,
665 const AttrBuilder &B) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000666 if (!B.hasAttributes())
667 return AttributeSet();
Bill Wendlingf7134812013-01-29 01:02:03 +0000668
Bill Wendlingae89a0f2013-02-05 23:48:36 +0000669 // Add target-independent attributes.
Bill Wendlingf7134812013-01-29 01:02:03 +0000670 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
Benjamin Kramer45e7d532013-02-16 19:13:18 +0000671 for (Attribute::AttrKind Kind = Attribute::None;
Benjamin Kramer6f37dac2013-02-16 19:22:28 +0000672 Kind != Attribute::EndAttrKinds; Kind = Attribute::AttrKind(Kind + 1)) {
Benjamin Kramer45e7d532013-02-16 19:13:18 +0000673 if (!B.contains(Kind))
674 continue;
675
Bill Wendlingf7134812013-01-29 01:02:03 +0000676 if (Kind == Attribute::Alignment)
Bill Wendling211316c2013-04-18 20:17:28 +0000677 Attrs.push_back(std::make_pair(Index, Attribute::
Bill Wendlingf7134812013-01-29 01:02:03 +0000678 getWithAlignment(C, B.getAlignment())));
679 else if (Kind == Attribute::StackAlignment)
Bill Wendling211316c2013-04-18 20:17:28 +0000680 Attrs.push_back(std::make_pair(Index, Attribute::
Bill Wendlingf7134812013-01-29 01:02:03 +0000681 getWithStackAlignment(C, B.getStackAlignment())));
Hal Finkelb0407ba2014-07-18 15:51:28 +0000682 else if (Kind == Attribute::Dereferenceable)
683 Attrs.push_back(std::make_pair(Index,
684 Attribute::getWithDereferenceableBytes(C,
685 B.getDereferenceableBytes())));
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000686 else if (Kind == Attribute::DereferenceableOrNull)
687 Attrs.push_back(
688 std::make_pair(Index, Attribute::getWithDereferenceableOrNullBytes(
689 C, B.getDereferenceableOrNullBytes())));
Bill Wendlingf7134812013-01-29 01:02:03 +0000690 else
Bill Wendling211316c2013-04-18 20:17:28 +0000691 Attrs.push_back(std::make_pair(Index, Attribute::get(C, Kind)));
Bill Wendlingf7134812013-01-29 01:02:03 +0000692 }
693
Bill Wendlingae89a0f2013-02-05 23:48:36 +0000694 // Add target-dependent (string) attributes.
David Majnemercf63a792014-05-03 23:00:35 +0000695 for (const AttrBuilder::td_type &TDA : B.td_attrs())
696 Attrs.push_back(
697 std::make_pair(Index, Attribute::get(C, TDA.first, TDA.second)));
Bill Wendlingae89a0f2013-02-05 23:48:36 +0000698
Bill Wendlingf7134812013-01-29 01:02:03 +0000699 return get(C, Attrs);
Bill Wendlingd509a662013-01-29 00:34:06 +0000700}
701
Bill Wendling211316c2013-04-18 20:17:28 +0000702AttributeSet AttributeSet::get(LLVMContext &C, unsigned Index,
Bill Wendlingd509a662013-01-29 00:34:06 +0000703 ArrayRef<Attribute::AttrKind> Kind) {
704 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
705 for (ArrayRef<Attribute::AttrKind>::iterator I = Kind.begin(),
706 E = Kind.end(); I != E; ++I)
Bill Wendling211316c2013-04-18 20:17:28 +0000707 Attrs.push_back(std::make_pair(Index, Attribute::get(C, *I)));
Bill Wendlingd509a662013-01-29 00:34:06 +0000708 return get(C, Attrs);
709}
710
711AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<AttributeSet> Attrs) {
712 if (Attrs.empty()) return AttributeSet();
Peter Collingbournebd6c7452013-08-02 22:29:40 +0000713 if (Attrs.size() == 1) return Attrs[0];
Bill Wendlingd509a662013-01-29 00:34:06 +0000714
715 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrNodeVec;
Peter Collingbournebd6c7452013-08-02 22:29:40 +0000716 AttributeSetImpl *A0 = Attrs[0].pImpl;
717 if (A0)
718 AttrNodeVec.append(A0->getNode(0), A0->getNode(A0->getNumAttributes()));
719 // Copy all attributes from Attrs into AttrNodeVec while keeping AttrNodeVec
720 // ordered by index. Because we know that each list in Attrs is ordered by
721 // index we only need to merge each successive list in rather than doing a
722 // full sort.
723 for (unsigned I = 1, E = Attrs.size(); I != E; ++I) {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000724 AttributeSetImpl *AS = Attrs[I].pImpl;
725 if (!AS) continue;
Peter Collingbournebd6c7452013-08-02 22:29:40 +0000726 SmallVector<std::pair<unsigned, AttributeSetNode *>, 8>::iterator
727 ANVI = AttrNodeVec.begin(), ANVE;
728 for (const AttributeSetImpl::IndexAttrPair
729 *AI = AS->getNode(0),
730 *AE = AS->getNode(AS->getNumAttributes());
731 AI != AE; ++AI) {
732 ANVE = AttrNodeVec.end();
733 while (ANVI != ANVE && ANVI->first <= AI->first)
734 ++ANVI;
735 ANVI = AttrNodeVec.insert(ANVI, *AI) + 1;
736 }
Bill Wendlingd509a662013-01-29 00:34:06 +0000737 }
738
739 return getImpl(C, AttrNodeVec);
740}
741
Bill Wendling211316c2013-04-18 20:17:28 +0000742AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
Bill Wendlingd509a662013-01-29 00:34:06 +0000743 Attribute::AttrKind Attr) const {
Bill Wendling211316c2013-04-18 20:17:28 +0000744 if (hasAttribute(Index, Attr)) return *this;
745 return addAttributes(C, Index, AttributeSet::get(C, Index, Attr));
Bill Wendlingd509a662013-01-29 00:34:06 +0000746}
747
Bill Wendling211316c2013-04-18 20:17:28 +0000748AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
Reed Kotler795c7b42013-03-13 20:20:08 +0000749 StringRef Kind) const {
750 llvm::AttrBuilder B;
751 B.addAttribute(Kind);
Bill Wendling211316c2013-04-18 20:17:28 +0000752 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
Reed Kotler795c7b42013-03-13 20:20:08 +0000753}
754
Bill Wendling3b2f6102013-07-25 18:34:24 +0000755AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
756 StringRef Kind, StringRef Value) const {
757 llvm::AttrBuilder B;
758 B.addAttribute(Kind, Value);
759 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
760}
761
Bill Wendling211316c2013-04-18 20:17:28 +0000762AttributeSet AttributeSet::addAttributes(LLVMContext &C, unsigned Index,
Bill Wendlingd509a662013-01-29 00:34:06 +0000763 AttributeSet Attrs) const {
764 if (!pImpl) return Attrs;
765 if (!Attrs.pImpl) return *this;
766
767#ifndef NDEBUG
768 // FIXME it is not obvious how this should work for alignment. For now, say
769 // we can't change a known alignment.
Bill Wendling211316c2013-04-18 20:17:28 +0000770 unsigned OldAlign = getParamAlignment(Index);
771 unsigned NewAlign = Attrs.getParamAlignment(Index);
Bill Wendlingd509a662013-01-29 00:34:06 +0000772 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
773 "Attempt to change alignment!");
774#endif
775
776 // Add the attribute slots before the one we're trying to add.
777 SmallVector<AttributeSet, 4> AttrSet;
778 uint64_t NumAttrs = pImpl->getNumAttributes();
779 AttributeSet AS;
780 uint64_t LastIndex = 0;
781 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
Bill Wendling211316c2013-04-18 20:17:28 +0000782 if (getSlotIndex(I) >= Index) {
783 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
Bill Wendlingd509a662013-01-29 00:34:06 +0000784 break;
785 }
786 LastIndex = I + 1;
787 AttrSet.push_back(getSlotAttributes(I));
788 }
789
790 // Now add the attribute into the correct slot. There may already be an
791 // AttributeSet there.
Bill Wendling211316c2013-04-18 20:17:28 +0000792 AttrBuilder B(AS, Index);
Bill Wendlingd509a662013-01-29 00:34:06 +0000793
794 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
Bill Wendling211316c2013-04-18 20:17:28 +0000795 if (Attrs.getSlotIndex(I) == Index) {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000796 for (AttributeSetImpl::iterator II = Attrs.pImpl->begin(I),
Bill Wendlingd509a662013-01-29 00:34:06 +0000797 IE = Attrs.pImpl->end(I); II != IE; ++II)
Bill Wendling23804da2013-01-31 23:38:01 +0000798 B.addAttribute(*II);
Bill Wendlingd509a662013-01-29 00:34:06 +0000799 break;
800 }
801
Bill Wendling211316c2013-04-18 20:17:28 +0000802 AttrSet.push_back(AttributeSet::get(C, Index, B));
Bill Wendlingd509a662013-01-29 00:34:06 +0000803
804 // Add the remaining attribute slots.
805 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
806 AttrSet.push_back(getSlotAttributes(I));
807
808 return get(C, AttrSet);
809}
810
Bill Wendling211316c2013-04-18 20:17:28 +0000811AttributeSet AttributeSet::removeAttribute(LLVMContext &C, unsigned Index,
Bill Wendlingd509a662013-01-29 00:34:06 +0000812 Attribute::AttrKind Attr) const {
Bill Wendling211316c2013-04-18 20:17:28 +0000813 if (!hasAttribute(Index, Attr)) return *this;
814 return removeAttributes(C, Index, AttributeSet::get(C, Index, Attr));
Bill Wendlingd509a662013-01-29 00:34:06 +0000815}
816
Bill Wendling211316c2013-04-18 20:17:28 +0000817AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Index,
Bill Wendlingd509a662013-01-29 00:34:06 +0000818 AttributeSet Attrs) const {
819 if (!pImpl) return AttributeSet();
820 if (!Attrs.pImpl) return *this;
821
822#ifndef NDEBUG
823 // FIXME it is not obvious how this should work for alignment.
824 // For now, say we can't pass in alignment, which no current use does.
Bill Wendling211316c2013-04-18 20:17:28 +0000825 assert(!Attrs.hasAttribute(Index, Attribute::Alignment) &&
Bill Wendlingd509a662013-01-29 00:34:06 +0000826 "Attempt to change alignment!");
827#endif
828
829 // Add the attribute slots before the one we're trying to add.
830 SmallVector<AttributeSet, 4> AttrSet;
831 uint64_t NumAttrs = pImpl->getNumAttributes();
832 AttributeSet AS;
833 uint64_t LastIndex = 0;
834 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
Bill Wendling211316c2013-04-18 20:17:28 +0000835 if (getSlotIndex(I) >= Index) {
836 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
Bill Wendlingd509a662013-01-29 00:34:06 +0000837 break;
838 }
839 LastIndex = I + 1;
840 AttrSet.push_back(getSlotAttributes(I));
841 }
842
Bill Wendlingd2196752013-01-30 23:07:40 +0000843 // Now remove the attribute from the correct slot. There may already be an
Bill Wendlingd509a662013-01-29 00:34:06 +0000844 // AttributeSet there.
Bill Wendling211316c2013-04-18 20:17:28 +0000845 AttrBuilder B(AS, Index);
Bill Wendlingd509a662013-01-29 00:34:06 +0000846
847 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
Bill Wendling211316c2013-04-18 20:17:28 +0000848 if (Attrs.getSlotIndex(I) == Index) {
849 B.removeAttributes(Attrs.pImpl->getSlotAttributes(I), Index);
Bill Wendlingd509a662013-01-29 00:34:06 +0000850 break;
851 }
852
Bill Wendling211316c2013-04-18 20:17:28 +0000853 AttrSet.push_back(AttributeSet::get(C, Index, B));
Bill Wendlingd509a662013-01-29 00:34:06 +0000854
855 // Add the remaining attribute slots.
856 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
857 AttrSet.push_back(getSlotAttributes(I));
858
859 return get(C, AttrSet);
860}
861
Ramkumar Ramachandra8fcb4982015-02-14 19:37:54 +0000862AttributeSet AttributeSet::addDereferenceableAttr(LLVMContext &C, unsigned Index,
863 uint64_t Bytes) const {
864 llvm::AttrBuilder B;
865 B.addDereferenceableAttr(Bytes);
866 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
867}
868
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000869AttributeSet AttributeSet::addDereferenceableOrNullAttr(LLVMContext &C,
870 unsigned Index,
871 uint64_t Bytes) const {
872 llvm::AttrBuilder B;
873 B.addDereferenceableOrNullAttr(Bytes);
874 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
875}
876
Bill Wendlingd509a662013-01-29 00:34:06 +0000877//===----------------------------------------------------------------------===//
878// AttributeSet Accessor Methods
879//===----------------------------------------------------------------------===//
880
Bill Wendling5d020a32013-02-10 05:00:40 +0000881LLVMContext &AttributeSet::getContext() const {
882 return pImpl->getContext();
883}
884
Bill Wendling211316c2013-04-18 20:17:28 +0000885AttributeSet AttributeSet::getParamAttributes(unsigned Index) const {
886 return pImpl && hasAttributes(Index) ?
Bill Wendlingd509a662013-01-29 00:34:06 +0000887 AttributeSet::get(pImpl->getContext(),
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000888 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendling211316c2013-04-18 20:17:28 +0000889 std::make_pair(Index, getAttributes(Index)))) :
Bill Wendlingd509a662013-01-29 00:34:06 +0000890 AttributeSet();
891}
892
893AttributeSet AttributeSet::getRetAttributes() const {
894 return pImpl && hasAttributes(ReturnIndex) ?
895 AttributeSet::get(pImpl->getContext(),
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000896 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendlingd509a662013-01-29 00:34:06 +0000897 std::make_pair(ReturnIndex,
898 getAttributes(ReturnIndex)))) :
899 AttributeSet();
900}
901
902AttributeSet AttributeSet::getFnAttributes() const {
903 return pImpl && hasAttributes(FunctionIndex) ?
904 AttributeSet::get(pImpl->getContext(),
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000905 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendlingd509a662013-01-29 00:34:06 +0000906 std::make_pair(FunctionIndex,
907 getAttributes(FunctionIndex)))) :
908 AttributeSet();
909}
910
911bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000912 AttributeSetNode *ASN = getAttributes(Index);
913 return ASN ? ASN->hasAttribute(Kind) : false;
Bill Wendlingd509a662013-01-29 00:34:06 +0000914}
915
Bill Wendlingbce7b972013-02-13 08:42:21 +0000916bool AttributeSet::hasAttribute(unsigned Index, StringRef Kind) const {
917 AttributeSetNode *ASN = getAttributes(Index);
918 return ASN ? ASN->hasAttribute(Kind) : false;
919}
920
Bill Wendlingd509a662013-01-29 00:34:06 +0000921bool AttributeSet::hasAttributes(unsigned Index) const {
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000922 AttributeSetNode *ASN = getAttributes(Index);
923 return ASN ? ASN->hasAttributes() : false;
Bill Wendlingd509a662013-01-29 00:34:06 +0000924}
925
926/// \brief Return true if the specified attribute is set for at least one
927/// parameter or for the return value.
928bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr) const {
Craig Topperc6207612014-04-09 06:08:46 +0000929 if (!pImpl) return false;
Bill Wendlingd509a662013-01-29 00:34:06 +0000930
931 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
Benjamin Kramer741146b2013-07-11 12:13:16 +0000932 for (AttributeSetImpl::iterator II = pImpl->begin(I),
Bill Wendlingd509a662013-01-29 00:34:06 +0000933 IE = pImpl->end(I); II != IE; ++II)
NAKAMURA Takumib0944392013-01-31 03:47:28 +0000934 if (II->hasAttribute(Attr))
Bill Wendlingd509a662013-01-29 00:34:06 +0000935 return true;
936
937 return false;
938}
939
Bill Wendlingbce7b972013-02-13 08:42:21 +0000940Attribute AttributeSet::getAttribute(unsigned Index,
941 Attribute::AttrKind Kind) const {
942 AttributeSetNode *ASN = getAttributes(Index);
943 return ASN ? ASN->getAttribute(Kind) : Attribute();
944}
945
946Attribute AttributeSet::getAttribute(unsigned Index,
947 StringRef Kind) const {
948 AttributeSetNode *ASN = getAttributes(Index);
949 return ASN ? ASN->getAttribute(Kind) : Attribute();
950}
951
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000952unsigned AttributeSet::getParamAlignment(unsigned Index) const {
953 AttributeSetNode *ASN = getAttributes(Index);
954 return ASN ? ASN->getAlignment() : 0;
Bill Wendlingd509a662013-01-29 00:34:06 +0000955}
956
957unsigned AttributeSet::getStackAlignment(unsigned Index) const {
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000958 AttributeSetNode *ASN = getAttributes(Index);
959 return ASN ? ASN->getStackAlignment() : 0;
Bill Wendlingd509a662013-01-29 00:34:06 +0000960}
961
Hal Finkelb0407ba2014-07-18 15:51:28 +0000962uint64_t AttributeSet::getDereferenceableBytes(unsigned Index) const {
963 AttributeSetNode *ASN = getAttributes(Index);
964 return ASN ? ASN->getDereferenceableBytes() : 0;
965}
966
Sanjoy Das06cf33f2015-05-06 17:41:54 +0000967uint64_t AttributeSet::getDereferenceableOrNullBytes(unsigned Index) const {
968 AttributeSetNode *ASN = getAttributes(Index);
969 return ASN ? ASN->getDereferenceableOrNullBytes() : 0;
970}
971
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +0000972std::string AttributeSet::getAsString(unsigned Index,
Bill Wendling829b4782013-02-11 08:43:33 +0000973 bool InAttrGrp) const {
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000974 AttributeSetNode *ASN = getAttributes(Index);
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +0000975 return ASN ? ASN->getAsString(InAttrGrp) : std::string("");
Bill Wendlingd509a662013-01-29 00:34:06 +0000976}
977
978/// \brief The attributes for the specified index are returned.
Bill Wendling211316c2013-04-18 20:17:28 +0000979AttributeSetNode *AttributeSet::getAttributes(unsigned Index) const {
Craig Topperc6207612014-04-09 06:08:46 +0000980 if (!pImpl) return nullptr;
Bill Wendlingd509a662013-01-29 00:34:06 +0000981
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000982 // Loop through to find the attribute node we want.
983 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
Bill Wendling211316c2013-04-18 20:17:28 +0000984 if (pImpl->getSlotIndex(I) == Index)
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000985 return pImpl->getSlotNode(I);
Bill Wendlingd509a662013-01-29 00:34:06 +0000986
Craig Topperc6207612014-04-09 06:08:46 +0000987 return nullptr;
Bill Wendlingd509a662013-01-29 00:34:06 +0000988}
989
Bill Wendling211316c2013-04-18 20:17:28 +0000990AttributeSet::iterator AttributeSet::begin(unsigned Slot) const {
Bill Wendlinga9174862013-01-31 23:53:05 +0000991 if (!pImpl)
992 return ArrayRef<Attribute>().begin();
Bill Wendling211316c2013-04-18 20:17:28 +0000993 return pImpl->begin(Slot);
Bill Wendlinga9174862013-01-31 23:53:05 +0000994}
995
Bill Wendling211316c2013-04-18 20:17:28 +0000996AttributeSet::iterator AttributeSet::end(unsigned Slot) const {
Bill Wendlinga9174862013-01-31 23:53:05 +0000997 if (!pImpl)
998 return ArrayRef<Attribute>().end();
Bill Wendling211316c2013-04-18 20:17:28 +0000999 return pImpl->end(Slot);
Bill Wendlinga9174862013-01-31 23:53:05 +00001000}
1001
Bill Wendlingd509a662013-01-29 00:34:06 +00001002//===----------------------------------------------------------------------===//
1003// AttributeSet Introspection Methods
1004//===----------------------------------------------------------------------===//
1005
1006/// \brief Return the number of slots used in this attribute list. This is the
1007/// number of arguments that have an attribute set on them (including the
1008/// function itself).
1009unsigned AttributeSet::getNumSlots() const {
1010 return pImpl ? pImpl->getNumAttributes() : 0;
1011}
1012
Rafael Espindoladd275302013-04-30 16:53:38 +00001013unsigned AttributeSet::getSlotIndex(unsigned Slot) const {
Bill Wendlingd509a662013-01-29 00:34:06 +00001014 assert(pImpl && Slot < pImpl->getNumAttributes() &&
1015 "Slot # out of range!");
1016 return pImpl->getSlotIndex(Slot);
1017}
1018
1019AttributeSet AttributeSet::getSlotAttributes(unsigned Slot) const {
1020 assert(pImpl && Slot < pImpl->getNumAttributes() &&
1021 "Slot # out of range!");
1022 return pImpl->getSlotAttributes(Slot);
1023}
1024
1025uint64_t AttributeSet::Raw(unsigned Index) const {
1026 // FIXME: Remove this.
1027 return pImpl ? pImpl->Raw(Index) : 0;
1028}
1029
1030void AttributeSet::dump() const {
1031 dbgs() << "PAL[\n";
1032
1033 for (unsigned i = 0, e = getNumSlots(); i < e; ++i) {
1034 uint64_t Index = getSlotIndex(i);
1035 dbgs() << " { ";
1036 if (Index == ~0U)
1037 dbgs() << "~0U";
1038 else
1039 dbgs() << Index;
1040 dbgs() << " => " << getAsString(Index) << " }\n";
1041 }
1042
1043 dbgs() << "]\n";
1044}
1045
Bill Wendlingabf3feb2012-10-05 06:44:41 +00001046//===----------------------------------------------------------------------===//
Bill Wendlingcd330342013-01-04 23:27:34 +00001047// AttrBuilder Method Implementations
Bill Wendlingabf3feb2012-10-05 06:44:41 +00001048//===----------------------------------------------------------------------===//
1049
Bill Wendling211316c2013-04-18 20:17:28 +00001050AttrBuilder::AttrBuilder(AttributeSet AS, unsigned Index)
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001051 : Attrs(0), Alignment(0), StackAlignment(0), DerefBytes(0),
1052 DerefOrNullBytes(0) {
Bill Wendling56b0b2a2013-01-27 21:23:46 +00001053 AttributeSetImpl *pImpl = AS.pImpl;
Bill Wendling096f5442013-01-07 08:24:35 +00001054 if (!pImpl) return;
1055
Bill Wendling9eb689c2013-01-28 00:21:34 +00001056 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I) {
Bill Wendling211316c2013-04-18 20:17:28 +00001057 if (pImpl->getSlotIndex(I) != Index) continue;
Bill Wendling096f5442013-01-07 08:24:35 +00001058
Benjamin Kramer741146b2013-07-11 12:13:16 +00001059 for (AttributeSetImpl::iterator II = pImpl->begin(I),
Bill Wendling9eb689c2013-01-28 00:21:34 +00001060 IE = pImpl->end(I); II != IE; ++II)
Bill Wendling23804da2013-01-31 23:38:01 +00001061 addAttribute(*II);
Bill Wendling9eb689c2013-01-28 00:21:34 +00001062
1063 break;
1064 }
Bill Wendling096f5442013-01-07 08:24:35 +00001065}
1066
Bill Wendlingcd330342013-01-04 23:27:34 +00001067void AttrBuilder::clear() {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001068 Attrs.reset();
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001069 Alignment = StackAlignment = DerefBytes = DerefOrNullBytes = 0;
Bill Wendlingcd330342013-01-04 23:27:34 +00001070}
1071
1072AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001073 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
Bill Wendling1c7cc8a2013-01-31 23:16:25 +00001074 assert(Val != Attribute::Alignment && Val != Attribute::StackAlignment &&
Hal Finkelb0407ba2014-07-18 15:51:28 +00001075 Val != Attribute::Dereferenceable &&
1076 "Adding integer attribute without adding a value!");
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001077 Attrs[Val] = true;
Bill Wendling7c04e042012-10-09 19:01:18 +00001078 return *this;
Bill Wendlingabf3feb2012-10-05 06:44:41 +00001079}
1080
Bill Wendling23804da2013-01-31 23:38:01 +00001081AttrBuilder &AttrBuilder::addAttribute(Attribute Attr) {
Bill Wendling0a437302013-02-10 10:13:23 +00001082 if (Attr.isStringAttribute()) {
1083 addAttribute(Attr.getKindAsString(), Attr.getValueAsString());
1084 return *this;
1085 }
1086
Bill Wendling3f12ac22013-02-05 22:37:24 +00001087 Attribute::AttrKind Kind = Attr.getKindAsEnum();
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001088 Attrs[Kind] = true;
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001089
Bill Wendling3f12ac22013-02-05 22:37:24 +00001090 if (Kind == Attribute::Alignment)
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001091 Alignment = Attr.getAlignment();
Bill Wendling3f12ac22013-02-05 22:37:24 +00001092 else if (Kind == Attribute::StackAlignment)
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001093 StackAlignment = Attr.getStackAlignment();
Hal Finkelb0407ba2014-07-18 15:51:28 +00001094 else if (Kind == Attribute::Dereferenceable)
1095 DerefBytes = Attr.getDereferenceableBytes();
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001096 else if (Kind == Attribute::DereferenceableOrNull)
1097 DerefOrNullBytes = Attr.getDereferenceableOrNullBytes();
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001098 return *this;
1099}
1100
Bill Wendlingb9c5b1a2013-02-05 08:09:32 +00001101AttrBuilder &AttrBuilder::addAttribute(StringRef A, StringRef V) {
1102 TargetDepAttrs[A] = V;
1103 return *this;
1104}
1105
Bill Wendling23804da2013-01-31 23:38:01 +00001106AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001107 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
1108 Attrs[Val] = false;
Bill Wendling23804da2013-01-31 23:38:01 +00001109
1110 if (Val == Attribute::Alignment)
1111 Alignment = 0;
1112 else if (Val == Attribute::StackAlignment)
1113 StackAlignment = 0;
Hal Finkelb0407ba2014-07-18 15:51:28 +00001114 else if (Val == Attribute::Dereferenceable)
1115 DerefBytes = 0;
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001116 else if (Val == Attribute::DereferenceableOrNull)
1117 DerefOrNullBytes = 0;
Bill Wendling23804da2013-01-31 23:38:01 +00001118
1119 return *this;
1120}
1121
Bill Wendlingd2196752013-01-30 23:07:40 +00001122AttrBuilder &AttrBuilder::removeAttributes(AttributeSet A, uint64_t Index) {
Bill Wendling211316c2013-04-18 20:17:28 +00001123 unsigned Slot = ~0U;
Bill Wendlingf1c94e32013-02-01 00:13:50 +00001124 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
1125 if (A.getSlotIndex(I) == Index) {
Bill Wendling211316c2013-04-18 20:17:28 +00001126 Slot = I;
Bill Wendlingf1c94e32013-02-01 00:13:50 +00001127 break;
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001128 }
Bill Wendlingf1c94e32013-02-01 00:13:50 +00001129
Bill Wendling211316c2013-04-18 20:17:28 +00001130 assert(Slot != ~0U && "Couldn't find index in AttributeSet!");
Bill Wendlingf1c94e32013-02-01 00:13:50 +00001131
Bill Wendling211316c2013-04-18 20:17:28 +00001132 for (AttributeSet::iterator I = A.begin(Slot), E = A.end(Slot); I != E; ++I) {
Bill Wendling7cde51d2013-02-12 07:56:49 +00001133 Attribute Attr = *I;
Hal Finkele15442c2014-07-18 06:51:55 +00001134 if (Attr.isEnumAttribute() || Attr.isIntAttribute()) {
Bill Wendling7cde51d2013-02-12 07:56:49 +00001135 Attribute::AttrKind Kind = I->getKindAsEnum();
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001136 Attrs[Kind] = false;
Bill Wendlingf1c94e32013-02-01 00:13:50 +00001137
Bill Wendling7cde51d2013-02-12 07:56:49 +00001138 if (Kind == Attribute::Alignment)
1139 Alignment = 0;
1140 else if (Kind == Attribute::StackAlignment)
1141 StackAlignment = 0;
Hal Finkelb0407ba2014-07-18 15:51:28 +00001142 else if (Kind == Attribute::Dereferenceable)
1143 DerefBytes = 0;
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001144 else if (Kind == Attribute::DereferenceableOrNull)
1145 DerefOrNullBytes = 0;
Bill Wendling7cde51d2013-02-12 07:56:49 +00001146 } else {
1147 assert(Attr.isStringAttribute() && "Invalid attribute type!");
1148 std::map<std::string, std::string>::iterator
1149 Iter = TargetDepAttrs.find(Attr.getKindAsString());
1150 if (Iter != TargetDepAttrs.end())
1151 TargetDepAttrs.erase(Iter);
1152 }
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001153 }
1154
1155 return *this;
1156}
1157
Bill Wendlingb9c5b1a2013-02-05 08:09:32 +00001158AttrBuilder &AttrBuilder::removeAttribute(StringRef A) {
1159 std::map<std::string, std::string>::iterator I = TargetDepAttrs.find(A);
1160 if (I != TargetDepAttrs.end())
1161 TargetDepAttrs.erase(I);
1162 return *this;
1163}
1164
Bill Wendling50d27842012-10-15 20:35:56 +00001165AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
Bill Wendlingabd5ba22012-10-14 03:58:29 +00001166 if (Align == 0) return *this;
Bill Wendlingcd330342013-01-04 23:27:34 +00001167
Bill Wendlingabf3feb2012-10-05 06:44:41 +00001168 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1169 assert(Align <= 0x40000000 && "Alignment too large.");
Bill Wendlingcd330342013-01-04 23:27:34 +00001170
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001171 Attrs[Attribute::Alignment] = true;
Bill Wendlingcd330342013-01-04 23:27:34 +00001172 Alignment = Align;
Bill Wendlingabd5ba22012-10-14 03:58:29 +00001173 return *this;
Chris Lattner3e13b8c2008-01-02 23:42:30 +00001174}
1175
Bill Wendlingcd330342013-01-04 23:27:34 +00001176AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
1177 // Default alignment, allow the target to define how to align it.
1178 if (Align == 0) return *this;
1179
1180 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1181 assert(Align <= 0x100 && "Alignment too large.");
1182
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001183 Attrs[Attribute::StackAlignment] = true;
Bill Wendlingcd330342013-01-04 23:27:34 +00001184 StackAlignment = Align;
1185 return *this;
1186}
1187
Hal Finkelb0407ba2014-07-18 15:51:28 +00001188AttrBuilder &AttrBuilder::addDereferenceableAttr(uint64_t Bytes) {
1189 if (Bytes == 0) return *this;
1190
1191 Attrs[Attribute::Dereferenceable] = true;
1192 DerefBytes = Bytes;
1193 return *this;
1194}
1195
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001196AttrBuilder &AttrBuilder::addDereferenceableOrNullAttr(uint64_t Bytes) {
1197 if (Bytes == 0)
1198 return *this;
1199
1200 Attrs[Attribute::DereferenceableOrNull] = true;
1201 DerefOrNullBytes = Bytes;
1202 return *this;
1203}
1204
Bill Wendlinge2614922013-02-06 01:16:00 +00001205AttrBuilder &AttrBuilder::merge(const AttrBuilder &B) {
1206 // FIXME: What if both have alignments, but they don't match?!
1207 if (!Alignment)
1208 Alignment = B.Alignment;
1209
1210 if (!StackAlignment)
1211 StackAlignment = B.StackAlignment;
1212
Hal Finkelb0407ba2014-07-18 15:51:28 +00001213 if (!DerefBytes)
1214 DerefBytes = B.DerefBytes;
1215
Benjamin Kramer45e7d532013-02-16 19:13:18 +00001216 Attrs |= B.Attrs;
Bill Wendlinge2614922013-02-06 01:16:00 +00001217
1218 for (td_const_iterator I = B.TargetDepAttrs.begin(),
1219 E = B.TargetDepAttrs.end(); I != E; ++I)
1220 TargetDepAttrs[I->first] = I->second;
1221
1222 return *this;
1223}
1224
Bill Wendling4b001442013-02-06 01:33:42 +00001225bool AttrBuilder::contains(StringRef A) const {
1226 return TargetDepAttrs.find(A) != TargetDepAttrs.end();
1227}
1228
Bill Wendling50d27842012-10-15 20:35:56 +00001229bool AttrBuilder::hasAttributes() const {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001230 return !Attrs.none() || !TargetDepAttrs.empty();
Bill Wendlingc6daefa2012-10-08 23:27:46 +00001231}
Bill Wendling9ac69f92013-01-04 20:54:35 +00001232
Bill Wendlingd2196752013-01-30 23:07:40 +00001233bool AttrBuilder::hasAttributes(AttributeSet A, uint64_t Index) const {
Bill Wendling211316c2013-04-18 20:17:28 +00001234 unsigned Slot = ~0U;
Bill Wendling9ca01da2013-02-02 00:42:06 +00001235 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
1236 if (A.getSlotIndex(I) == Index) {
Bill Wendling211316c2013-04-18 20:17:28 +00001237 Slot = I;
Bill Wendling9ca01da2013-02-02 00:42:06 +00001238 break;
1239 }
1240
Bill Wendling211316c2013-04-18 20:17:28 +00001241 assert(Slot != ~0U && "Couldn't find the index!");
Bill Wendling9ca01da2013-02-02 00:42:06 +00001242
Bill Wendling211316c2013-04-18 20:17:28 +00001243 for (AttributeSet::iterator I = A.begin(Slot), E = A.end(Slot);
Bill Wendling7cde51d2013-02-12 07:56:49 +00001244 I != E; ++I) {
1245 Attribute Attr = *I;
Hal Finkele15442c2014-07-18 06:51:55 +00001246 if (Attr.isEnumAttribute() || Attr.isIntAttribute()) {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001247 if (Attrs[I->getKindAsEnum()])
Bill Wendling7cde51d2013-02-12 07:56:49 +00001248 return true;
1249 } else {
1250 assert(Attr.isStringAttribute() && "Invalid attribute kind!");
1251 return TargetDepAttrs.find(Attr.getKindAsString())!=TargetDepAttrs.end();
1252 }
1253 }
Bill Wendling9ca01da2013-02-02 00:42:06 +00001254
1255 return false;
Bill Wendling70f39172012-10-09 00:01:21 +00001256}
Bill Wendling9ac69f92013-01-04 20:54:35 +00001257
Bill Wendling50d27842012-10-15 20:35:56 +00001258bool AttrBuilder::hasAlignmentAttr() const {
Bill Wendlingcd330342013-01-04 23:27:34 +00001259 return Alignment != 0;
Bill Wendlingc6daefa2012-10-08 23:27:46 +00001260}
1261
Bill Wendlingd509a662013-01-29 00:34:06 +00001262bool AttrBuilder::operator==(const AttrBuilder &B) {
Benjamin Kramer45e7d532013-02-16 19:13:18 +00001263 if (Attrs != B.Attrs)
1264 return false;
Bill Wendling4b001442013-02-06 01:33:42 +00001265
1266 for (td_const_iterator I = TargetDepAttrs.begin(),
1267 E = TargetDepAttrs.end(); I != E; ++I)
1268 if (B.TargetDepAttrs.find(I->first) == B.TargetDepAttrs.end())
1269 return false;
1270
Hal Finkelb0407ba2014-07-18 15:51:28 +00001271 return Alignment == B.Alignment && StackAlignment == B.StackAlignment &&
1272 DerefBytes == B.DerefBytes;
Bill Wendlingd509a662013-01-29 00:34:06 +00001273}
1274
1275AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) {
Bill Wendling56aeccc2013-02-04 23:32:23 +00001276 // FIXME: Remove this in 4.0.
Bill Wendling60011b82013-01-29 01:43:29 +00001277 if (!Val) return *this;
1278
Bill Wendlingd509a662013-01-29 00:34:06 +00001279 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
1280 I = Attribute::AttrKind(I + 1)) {
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001281 if (I == Attribute::Dereferenceable ||
1282 I == Attribute::DereferenceableOrNull)
Hal Finkelb0407ba2014-07-18 15:51:28 +00001283 continue;
Bill Wendlingd509a662013-01-29 00:34:06 +00001284 if (uint64_t A = (Val & AttributeImpl::getAttrMask(I))) {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001285 Attrs[I] = true;
Bill Wendlingd509a662013-01-29 00:34:06 +00001286
1287 if (I == Attribute::Alignment)
1288 Alignment = 1ULL << ((A >> 16) - 1);
1289 else if (I == Attribute::StackAlignment)
1290 StackAlignment = 1ULL << ((A >> 26)-1);
1291 }
1292 }
1293
1294 return *this;
1295}
1296
Bill Wendling57625a42013-01-25 23:09:36 +00001297//===----------------------------------------------------------------------===//
1298// AttributeFuncs Function Defintions
1299//===----------------------------------------------------------------------===//
1300
Bill Wendlingc79cdff2013-02-01 01:04:27 +00001301/// \brief Which attributes cannot be applied to a type.
Bill Wendlingd2196752013-01-30 23:07:40 +00001302AttributeSet AttributeFuncs::typeIncompatible(Type *Ty, uint64_t Index) {
Bill Wendling57625a42013-01-25 23:09:36 +00001303 AttrBuilder Incompatible;
1304
1305 if (!Ty->isIntegerTy())
1306 // Attribute that only apply to integers.
1307 Incompatible.addAttribute(Attribute::SExt)
1308 .addAttribute(Attribute::ZExt);
1309
1310 if (!Ty->isPointerTy())
1311 // Attribute that only apply to pointers.
1312 Incompatible.addAttribute(Attribute::ByVal)
1313 .addAttribute(Attribute::Nest)
1314 .addAttribute(Attribute::NoAlias)
1315 .addAttribute(Attribute::NoCapture)
Nick Lewyckyd52b1522014-05-20 01:23:40 +00001316 .addAttribute(Attribute::NonNull)
Hal Finkelb0407ba2014-07-18 15:51:28 +00001317 .addDereferenceableAttr(1) // the int here is ignored
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001318 .addDereferenceableOrNullAttr(1) // the int here is ignored
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001319 .addAttribute(Attribute::ReadNone)
1320 .addAttribute(Attribute::ReadOnly)
Reid Klecknera534a382013-12-19 02:14:12 +00001321 .addAttribute(Attribute::StructRet)
1322 .addAttribute(Attribute::InAlloca);
Bill Wendling57625a42013-01-25 23:09:36 +00001323
Bill Wendlingd2196752013-01-30 23:07:40 +00001324 return AttributeSet::get(Ty->getContext(), Index, Incompatible);
Bill Wendling57625a42013-01-25 23:09:36 +00001325}