blob: e9626ba7e3bcb6f52104bc0a1d89ec4c278c9328 [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";
Igor Laevsky39d662f2015-07-11 10:30:36 +0000193 if (hasAttribute(Attribute::ArgMemOnly))
194 return "argmemonly";
Michael Gottesman41748d72013-06-27 00:25:01 +0000195 if (hasAttribute(Attribute::Builtin))
196 return "builtin";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000197 if (hasAttribute(Attribute::ByVal))
198 return "byval";
Owen Anderson85fa7d52015-05-26 23:48:40 +0000199 if (hasAttribute(Attribute::Convergent))
200 return "convergent";
Reid Klecknera534a382013-12-19 02:14:12 +0000201 if (hasAttribute(Attribute::InAlloca))
202 return "inalloca";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000203 if (hasAttribute(Attribute::InlineHint))
204 return "inlinehint";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000205 if (hasAttribute(Attribute::InReg))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000206 return "inreg";
Tom Roeder44cb65f2014-06-05 19:29:43 +0000207 if (hasAttribute(Attribute::JumpTable))
208 return "jumptable";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000209 if (hasAttribute(Attribute::MinSize))
210 return "minsize";
211 if (hasAttribute(Attribute::Naked))
212 return "naked";
213 if (hasAttribute(Attribute::Nest))
214 return "nest";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000215 if (hasAttribute(Attribute::NoAlias))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000216 return "noalias";
Bill Wendling09bd1f72013-02-22 00:12:35 +0000217 if (hasAttribute(Attribute::NoBuiltin))
218 return "nobuiltin";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000219 if (hasAttribute(Attribute::NoCapture))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000220 return "nocapture";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000221 if (hasAttribute(Attribute::NoDuplicate))
222 return "noduplicate";
223 if (hasAttribute(Attribute::NoImplicitFloat))
224 return "noimplicitfloat";
225 if (hasAttribute(Attribute::NoInline))
226 return "noinline";
227 if (hasAttribute(Attribute::NonLazyBind))
228 return "nonlazybind";
Nick Lewyckyd52b1522014-05-20 01:23:40 +0000229 if (hasAttribute(Attribute::NonNull))
230 return "nonnull";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000231 if (hasAttribute(Attribute::NoRedZone))
232 return "noredzone";
233 if (hasAttribute(Attribute::NoReturn))
234 return "noreturn";
James Molloye6f87ca2015-11-06 10:32:53 +0000235 if (hasAttribute(Attribute::NoRecurse))
236 return "norecurse";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000237 if (hasAttribute(Attribute::NoUnwind))
238 return "nounwind";
Andrea Di Biagio377496b2013-08-23 11:53:55 +0000239 if (hasAttribute(Attribute::OptimizeNone))
240 return "optnone";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000241 if (hasAttribute(Attribute::OptimizeForSize))
242 return "optsize";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000243 if (hasAttribute(Attribute::ReadNone))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000244 return "readnone";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000245 if (hasAttribute(Attribute::ReadOnly))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000246 return "readonly";
Stephen Linb8bd2322013-04-20 05:14:40 +0000247 if (hasAttribute(Attribute::Returned))
248 return "returned";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000249 if (hasAttribute(Attribute::ReturnsTwice))
250 return "returns_twice";
251 if (hasAttribute(Attribute::SExt))
252 return "signext";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000253 if (hasAttribute(Attribute::StackProtect))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000254 return "ssp";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000255 if (hasAttribute(Attribute::StackProtectReq))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000256 return "sspreq";
Bill Wendlingd154e2832013-01-23 06:41:41 +0000257 if (hasAttribute(Attribute::StackProtectStrong))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000258 return "sspstrong";
Peter Collingbourne82437bf2015-06-15 21:07:11 +0000259 if (hasAttribute(Attribute::SafeStack))
260 return "safestack";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000261 if (hasAttribute(Attribute::StructRet))
262 return "sret";
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000263 if (hasAttribute(Attribute::SanitizeThread))
264 return "sanitize_thread";
265 if (hasAttribute(Attribute::SanitizeMemory))
266 return "sanitize_memory";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000267 if (hasAttribute(Attribute::UWTable))
268 return "uwtable";
269 if (hasAttribute(Attribute::ZExt))
270 return "zeroext";
Diego Novilloc6399532013-05-24 12:26:52 +0000271 if (hasAttribute(Attribute::Cold))
272 return "cold";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000273
274 // FIXME: These should be output like this:
275 //
276 // align=4
277 // alignstack=8
278 //
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000279 if (hasAttribute(Attribute::Alignment)) {
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000280 std::string Result;
Bill Wendling829b4782013-02-11 08:43:33 +0000281 Result += "align";
282 Result += (InAttrGrp) ? "=" : " ";
Bill Wendling3f12ac22013-02-05 22:37:24 +0000283 Result += utostr(getValueAsInt());
284 return Result;
285 }
Bill Wendling829b4782013-02-11 08:43:33 +0000286
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000287 auto AttrWithBytesToString = [&](const char *Name) {
Bill Wendling3f12ac22013-02-05 22:37:24 +0000288 std::string Result;
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000289 Result += Name;
Bill Wendling829b4782013-02-11 08:43:33 +0000290 if (InAttrGrp) {
291 Result += "=";
292 Result += utostr(getValueAsInt());
293 } else {
294 Result += "(";
295 Result += utostr(getValueAsInt());
296 Result += ")";
297 }
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000298 return Result;
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000299 };
Bill Wendling9c2eba92013-01-31 20:59:05 +0000300
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000301 if (hasAttribute(Attribute::StackAlignment))
302 return AttrWithBytesToString("alignstack");
303
304 if (hasAttribute(Attribute::Dereferenceable))
305 return AttrWithBytesToString("dereferenceable");
306
307 if (hasAttribute(Attribute::DereferenceableOrNull))
308 return AttrWithBytesToString("dereferenceable_or_null");
Hal Finkelb0407ba2014-07-18 15:51:28 +0000309
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;
Yaron Keren075759a2015-03-30 15:42:36 +0000317 Result += (Twine('"') + getKindAsString() + Twine('"')).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
Yaron Keren075759a2015-03-30 15:42:36 +0000322 Result += ("=\"" + Val + Twine('"')).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;
Owen Anderson85fa7d52015-05-26 23:48:40 +0000445 case Attribute::Convergent: return 1ULL << 46;
Peter Collingbourne82437bf2015-06-15 21:07:11 +0000446 case Attribute::SafeStack: return 1ULL << 47;
James Molloye6f87ca2015-11-06 10:32:53 +0000447 case Attribute::NoRecurse: return 1ULL << 48;
Hal Finkelb0407ba2014-07-18 15:51:28 +0000448 case Attribute::Dereferenceable:
449 llvm_unreachable("dereferenceable attribute not supported in raw format");
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000450 break;
451 case Attribute::DereferenceableOrNull:
452 llvm_unreachable("dereferenceable_or_null attribute not supported in raw "
453 "format");
454 break;
Igor Laevsky39d662f2015-07-11 10:30:36 +0000455 case Attribute::ArgMemOnly:
456 llvm_unreachable("argmemonly attribute not supported in raw format");
457 break;
Bill Wendling25342e12013-02-22 00:50:09 +0000458 }
459 llvm_unreachable("Unsupported attribute type");
Bill Wendlingd509a662013-01-29 00:34:06 +0000460}
461
462//===----------------------------------------------------------------------===//
463// AttributeSetNode Definition
464//===----------------------------------------------------------------------===//
465
466AttributeSetNode *AttributeSetNode::get(LLVMContext &C,
467 ArrayRef<Attribute> Attrs) {
468 if (Attrs.empty())
Craig Topperc6207612014-04-09 06:08:46 +0000469 return nullptr;
Bill Wendlingd509a662013-01-29 00:34:06 +0000470
471 // Otherwise, build a key to look up the existing attributes.
472 LLVMContextImpl *pImpl = C.pImpl;
473 FoldingSetNodeID ID;
474
475 SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end());
Bill Wendling82c2ee62013-02-13 09:26:26 +0000476 array_pod_sort(SortedAttrs.begin(), SortedAttrs.end());
Bill Wendlingd509a662013-01-29 00:34:06 +0000477
478 for (SmallVectorImpl<Attribute>::iterator I = SortedAttrs.begin(),
479 E = SortedAttrs.end(); I != E; ++I)
480 I->Profile(ID);
481
482 void *InsertPoint;
483 AttributeSetNode *PA =
484 pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint);
485
486 // If we didn't find any existing attributes of the same shape then create a
487 // new one and insert it.
488 if (!PA) {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000489 // Coallocate entries after the AttributeSetNode itself.
James Y Knightaa365b22015-08-05 22:57:34 +0000490 void *Mem = ::operator new(totalSizeToAlloc<Attribute>(SortedAttrs.size()));
Benjamin Kramer741146b2013-07-11 12:13:16 +0000491 PA = new (Mem) AttributeSetNode(SortedAttrs);
Bill Wendlingd509a662013-01-29 00:34:06 +0000492 pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint);
493 }
494
495 // Return the AttributesListNode that we found or created.
496 return PA;
497}
498
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000499bool AttributeSetNode::hasAttribute(Attribute::AttrKind Kind) const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000500 for (iterator I = begin(), E = end(); I != E; ++I)
NAKAMURA Takumib0944392013-01-31 03:47:28 +0000501 if (I->hasAttribute(Kind))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000502 return true;
503 return false;
504}
505
Bill Wendlingbce7b972013-02-13 08:42:21 +0000506bool AttributeSetNode::hasAttribute(StringRef Kind) const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000507 for (iterator I = begin(), E = end(); I != E; ++I)
Bill Wendlingbce7b972013-02-13 08:42:21 +0000508 if (I->hasAttribute(Kind))
509 return true;
510 return false;
511}
512
513Attribute AttributeSetNode::getAttribute(Attribute::AttrKind Kind) const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000514 for (iterator I = begin(), E = end(); I != E; ++I)
Bill Wendlingbce7b972013-02-13 08:42:21 +0000515 if (I->hasAttribute(Kind))
516 return *I;
517 return Attribute();
518}
519
520Attribute AttributeSetNode::getAttribute(StringRef Kind) const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000521 for (iterator I = begin(), E = end(); I != E; ++I)
Bill Wendlingbce7b972013-02-13 08:42:21 +0000522 if (I->hasAttribute(Kind))
523 return *I;
524 return Attribute();
525}
526
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000527unsigned AttributeSetNode::getAlignment() const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000528 for (iterator I = begin(), E = end(); I != E; ++I)
NAKAMURA Takumib0944392013-01-31 03:47:28 +0000529 if (I->hasAttribute(Attribute::Alignment))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000530 return I->getAlignment();
531 return 0;
532}
533
534unsigned AttributeSetNode::getStackAlignment() const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000535 for (iterator I = begin(), E = end(); I != E; ++I)
NAKAMURA Takumib0944392013-01-31 03:47:28 +0000536 if (I->hasAttribute(Attribute::StackAlignment))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000537 return I->getStackAlignment();
538 return 0;
539}
540
Hal Finkelb0407ba2014-07-18 15:51:28 +0000541uint64_t AttributeSetNode::getDereferenceableBytes() const {
542 for (iterator I = begin(), E = end(); I != E; ++I)
543 if (I->hasAttribute(Attribute::Dereferenceable))
544 return I->getDereferenceableBytes();
545 return 0;
546}
547
Sanjoy Das06cf33f2015-05-06 17:41:54 +0000548uint64_t AttributeSetNode::getDereferenceableOrNullBytes() const {
549 for (iterator I = begin(), E = end(); I != E; ++I)
550 if (I->hasAttribute(Attribute::DereferenceableOrNull))
551 return I->getDereferenceableOrNullBytes();
552 return 0;
553}
554
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +0000555std::string AttributeSetNode::getAsString(bool InAttrGrp) const {
Benjamin Kramer0baf8f42013-04-19 11:43:21 +0000556 std::string Str;
Benjamin Kramer741146b2013-07-11 12:13:16 +0000557 for (iterator I = begin(), E = end(); I != E; ++I) {
558 if (I != begin())
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +0000559 Str += ' ';
560 Str += I->getAsString(InAttrGrp);
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000561 }
562 return Str;
563}
564
Bill Wendlingd509a662013-01-29 00:34:06 +0000565//===----------------------------------------------------------------------===//
566// AttributeSetImpl Definition
567//===----------------------------------------------------------------------===//
568
Rafael Espindoladd275302013-04-30 16:53:38 +0000569uint64_t AttributeSetImpl::Raw(unsigned Index) const {
Bill Wendlingd509a662013-01-29 00:34:06 +0000570 for (unsigned I = 0, E = getNumAttributes(); I != E; ++I) {
571 if (getSlotIndex(I) != Index) continue;
Benjamin Kramer741146b2013-07-11 12:13:16 +0000572 const AttributeSetNode *ASN = getSlotNode(I);
Bill Wendling91226182013-02-02 00:52:44 +0000573 uint64_t Mask = 0;
Bill Wendlingd509a662013-01-29 00:34:06 +0000574
Benjamin Kramer741146b2013-07-11 12:13:16 +0000575 for (AttributeSetNode::iterator II = ASN->begin(),
Bill Wendling91226182013-02-02 00:52:44 +0000576 IE = ASN->end(); II != IE; ++II) {
577 Attribute Attr = *II;
Bill Wendlingd746e402013-02-10 23:18:05 +0000578
579 // This cannot handle string attributes.
580 if (Attr.isStringAttribute()) continue;
581
Bill Wendling3f12ac22013-02-05 22:37:24 +0000582 Attribute::AttrKind Kind = Attr.getKindAsEnum();
Bill Wendling91226182013-02-02 00:52:44 +0000583
Bill Wendling3f12ac22013-02-05 22:37:24 +0000584 if (Kind == Attribute::Alignment)
Bill Wendling91226182013-02-02 00:52:44 +0000585 Mask |= (Log2_32(ASN->getAlignment()) + 1) << 16;
Bill Wendling3f12ac22013-02-05 22:37:24 +0000586 else if (Kind == Attribute::StackAlignment)
Bill Wendling91226182013-02-02 00:52:44 +0000587 Mask |= (Log2_32(ASN->getStackAlignment()) + 1) << 26;
Hal Finkelb0407ba2014-07-18 15:51:28 +0000588 else if (Kind == Attribute::Dereferenceable)
589 llvm_unreachable("dereferenceable not supported in bit mask");
Bill Wendling91226182013-02-02 00:52:44 +0000590 else
Bill Wendling3f12ac22013-02-05 22:37:24 +0000591 Mask |= AttributeImpl::getAttrMask(Kind);
Bill Wendling91226182013-02-02 00:52:44 +0000592 }
593
594 return Mask;
Bill Wendlingd509a662013-01-29 00:34:06 +0000595 }
596
597 return 0;
598}
599
Peter Collingbourneabca2ec2013-08-02 22:34:30 +0000600void AttributeSetImpl::dump() const {
601 AttributeSet(const_cast<AttributeSetImpl *>(this)).dump();
602}
603
Bill Wendlingd509a662013-01-29 00:34:06 +0000604//===----------------------------------------------------------------------===//
605// AttributeSet Construction and Mutation Methods
606//===----------------------------------------------------------------------===//
607
Bill Wendling60011b82013-01-29 01:43:29 +0000608AttributeSet
609AttributeSet::getImpl(LLVMContext &C,
610 ArrayRef<std::pair<unsigned, AttributeSetNode*> > Attrs) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000611 LLVMContextImpl *pImpl = C.pImpl;
612 FoldingSetNodeID ID;
613 AttributeSetImpl::Profile(ID, Attrs);
614
615 void *InsertPoint;
616 AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
617
618 // If we didn't find any existing attributes of the same shape then
619 // create a new one and insert it.
620 if (!PA) {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000621 // Coallocate entries after the AttributeSetImpl itself.
James Y Knightaa365b22015-08-05 22:57:34 +0000622 void *Mem = ::operator new(
623 AttributeSetImpl::totalSizeToAlloc<IndexAttrPair>(Attrs.size()));
Benjamin Kramer741146b2013-07-11 12:13:16 +0000624 PA = new (Mem) AttributeSetImpl(C, Attrs);
Bill Wendlingd509a662013-01-29 00:34:06 +0000625 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
626 }
627
628 // Return the AttributesList that we found or created.
629 return AttributeSet(PA);
630}
631
632AttributeSet AttributeSet::get(LLVMContext &C,
633 ArrayRef<std::pair<unsigned, Attribute> > Attrs){
634 // If there are no attributes then return a null AttributesList pointer.
635 if (Attrs.empty())
636 return AttributeSet();
637
638#ifndef NDEBUG
639 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
640 assert((!i || Attrs[i-1].first <= Attrs[i].first) &&
641 "Misordered Attributes list!");
Bill Wendling3f12ac22013-02-05 22:37:24 +0000642 assert(!Attrs[i].second.hasAttribute(Attribute::None) &&
Bill Wendlingd509a662013-01-29 00:34:06 +0000643 "Pointless attribute!");
644 }
645#endif
646
647 // Create a vector if (unsigned, AttributeSetNode*) pairs from the attributes
648 // list.
649 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrPairVec;
650 for (ArrayRef<std::pair<unsigned, Attribute> >::iterator I = Attrs.begin(),
651 E = Attrs.end(); I != E; ) {
652 unsigned Index = I->first;
653 SmallVector<Attribute, 4> AttrVec;
NAKAMURA Takumif05d2f22013-01-29 15:18:16 +0000654 while (I != E && I->first == Index) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000655 AttrVec.push_back(I->second);
656 ++I;
657 }
658
659 AttrPairVec.push_back(std::make_pair(Index,
660 AttributeSetNode::get(C, AttrVec)));
661 }
662
663 return getImpl(C, AttrPairVec);
664}
665
666AttributeSet AttributeSet::get(LLVMContext &C,
667 ArrayRef<std::pair<unsigned,
668 AttributeSetNode*> > Attrs) {
669 // If there are no attributes then return a null AttributesList pointer.
670 if (Attrs.empty())
671 return AttributeSet();
672
673 return getImpl(C, Attrs);
674}
675
David Majnemercf63a792014-05-03 23:00:35 +0000676AttributeSet AttributeSet::get(LLVMContext &C, unsigned Index,
677 const AttrBuilder &B) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000678 if (!B.hasAttributes())
679 return AttributeSet();
Bill Wendlingf7134812013-01-29 01:02:03 +0000680
Bill Wendlingae89a0f2013-02-05 23:48:36 +0000681 // Add target-independent attributes.
Bill Wendlingf7134812013-01-29 01:02:03 +0000682 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
Benjamin Kramer45e7d532013-02-16 19:13:18 +0000683 for (Attribute::AttrKind Kind = Attribute::None;
Benjamin Kramer6f37dac2013-02-16 19:22:28 +0000684 Kind != Attribute::EndAttrKinds; Kind = Attribute::AttrKind(Kind + 1)) {
Benjamin Kramer45e7d532013-02-16 19:13:18 +0000685 if (!B.contains(Kind))
686 continue;
687
Bill Wendlingf7134812013-01-29 01:02:03 +0000688 if (Kind == Attribute::Alignment)
Bill Wendling211316c2013-04-18 20:17:28 +0000689 Attrs.push_back(std::make_pair(Index, Attribute::
Bill Wendlingf7134812013-01-29 01:02:03 +0000690 getWithAlignment(C, B.getAlignment())));
691 else if (Kind == Attribute::StackAlignment)
Bill Wendling211316c2013-04-18 20:17:28 +0000692 Attrs.push_back(std::make_pair(Index, Attribute::
Bill Wendlingf7134812013-01-29 01:02:03 +0000693 getWithStackAlignment(C, B.getStackAlignment())));
Hal Finkelb0407ba2014-07-18 15:51:28 +0000694 else if (Kind == Attribute::Dereferenceable)
695 Attrs.push_back(std::make_pair(Index,
696 Attribute::getWithDereferenceableBytes(C,
697 B.getDereferenceableBytes())));
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000698 else if (Kind == Attribute::DereferenceableOrNull)
699 Attrs.push_back(
700 std::make_pair(Index, Attribute::getWithDereferenceableOrNullBytes(
701 C, B.getDereferenceableOrNullBytes())));
Bill Wendlingf7134812013-01-29 01:02:03 +0000702 else
Bill Wendling211316c2013-04-18 20:17:28 +0000703 Attrs.push_back(std::make_pair(Index, Attribute::get(C, Kind)));
Bill Wendlingf7134812013-01-29 01:02:03 +0000704 }
705
Bill Wendlingae89a0f2013-02-05 23:48:36 +0000706 // Add target-dependent (string) attributes.
David Majnemercf63a792014-05-03 23:00:35 +0000707 for (const AttrBuilder::td_type &TDA : B.td_attrs())
708 Attrs.push_back(
709 std::make_pair(Index, Attribute::get(C, TDA.first, TDA.second)));
Bill Wendlingae89a0f2013-02-05 23:48:36 +0000710
Bill Wendlingf7134812013-01-29 01:02:03 +0000711 return get(C, Attrs);
Bill Wendlingd509a662013-01-29 00:34:06 +0000712}
713
Bill Wendling211316c2013-04-18 20:17:28 +0000714AttributeSet AttributeSet::get(LLVMContext &C, unsigned Index,
Bill Wendlingd509a662013-01-29 00:34:06 +0000715 ArrayRef<Attribute::AttrKind> Kind) {
716 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
717 for (ArrayRef<Attribute::AttrKind>::iterator I = Kind.begin(),
718 E = Kind.end(); I != E; ++I)
Bill Wendling211316c2013-04-18 20:17:28 +0000719 Attrs.push_back(std::make_pair(Index, Attribute::get(C, *I)));
Bill Wendlingd509a662013-01-29 00:34:06 +0000720 return get(C, Attrs);
721}
722
723AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<AttributeSet> Attrs) {
724 if (Attrs.empty()) return AttributeSet();
Peter Collingbournebd6c7452013-08-02 22:29:40 +0000725 if (Attrs.size() == 1) return Attrs[0];
Bill Wendlingd509a662013-01-29 00:34:06 +0000726
727 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrNodeVec;
Peter Collingbournebd6c7452013-08-02 22:29:40 +0000728 AttributeSetImpl *A0 = Attrs[0].pImpl;
729 if (A0)
730 AttrNodeVec.append(A0->getNode(0), A0->getNode(A0->getNumAttributes()));
731 // Copy all attributes from Attrs into AttrNodeVec while keeping AttrNodeVec
732 // ordered by index. Because we know that each list in Attrs is ordered by
733 // index we only need to merge each successive list in rather than doing a
734 // full sort.
735 for (unsigned I = 1, E = Attrs.size(); I != E; ++I) {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000736 AttributeSetImpl *AS = Attrs[I].pImpl;
737 if (!AS) continue;
Peter Collingbournebd6c7452013-08-02 22:29:40 +0000738 SmallVector<std::pair<unsigned, AttributeSetNode *>, 8>::iterator
739 ANVI = AttrNodeVec.begin(), ANVE;
James Y Knightaa365b22015-08-05 22:57:34 +0000740 for (const IndexAttrPair *AI = AS->getNode(0),
741 *AE = AS->getNode(AS->getNumAttributes());
Peter Collingbournebd6c7452013-08-02 22:29:40 +0000742 AI != AE; ++AI) {
743 ANVE = AttrNodeVec.end();
744 while (ANVI != ANVE && ANVI->first <= AI->first)
745 ++ANVI;
746 ANVI = AttrNodeVec.insert(ANVI, *AI) + 1;
747 }
Bill Wendlingd509a662013-01-29 00:34:06 +0000748 }
749
750 return getImpl(C, AttrNodeVec);
751}
752
Bill Wendling211316c2013-04-18 20:17:28 +0000753AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
Bill Wendlingd509a662013-01-29 00:34:06 +0000754 Attribute::AttrKind Attr) const {
Bill Wendling211316c2013-04-18 20:17:28 +0000755 if (hasAttribute(Index, Attr)) return *this;
756 return addAttributes(C, Index, AttributeSet::get(C, Index, Attr));
Bill Wendlingd509a662013-01-29 00:34:06 +0000757}
758
Bill Wendling211316c2013-04-18 20:17:28 +0000759AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
Reed Kotler795c7b42013-03-13 20:20:08 +0000760 StringRef Kind) const {
761 llvm::AttrBuilder B;
762 B.addAttribute(Kind);
Bill Wendling211316c2013-04-18 20:17:28 +0000763 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
Reed Kotler795c7b42013-03-13 20:20:08 +0000764}
765
Bill Wendling3b2f6102013-07-25 18:34:24 +0000766AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
767 StringRef Kind, StringRef Value) const {
768 llvm::AttrBuilder B;
769 B.addAttribute(Kind, Value);
770 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
771}
772
Akira Hatanaka237916b2015-12-02 06:58:49 +0000773AttributeSet AttributeSet::addAttribute(LLVMContext &C,
774 ArrayRef<unsigned> Indices,
775 Attribute A) const {
776 unsigned I = 0, E = pImpl ? pImpl->getNumAttributes() : 0;
777 auto IdxI = Indices.begin(), IdxE = Indices.end();
778 SmallVector<AttributeSet, 4> AttrSet;
779
780 while (I != E && IdxI != IdxE) {
781 if (getSlotIndex(I) < *IdxI)
782 AttrSet.emplace_back(getSlotAttributes(I++));
783 else if (getSlotIndex(I) > *IdxI)
784 AttrSet.emplace_back(AttributeSet::get(C, std::make_pair(*IdxI++, A)));
785 else {
786 AttrBuilder B(getSlotAttributes(I), *IdxI);
787 B.addAttribute(A);
788 AttrSet.emplace_back(AttributeSet::get(C, *IdxI, B));
789 ++I;
790 ++IdxI;
791 }
792 }
793
794 while (I != E)
795 AttrSet.emplace_back(getSlotAttributes(I++));
796
797 while (IdxI != IdxE)
798 AttrSet.emplace_back(AttributeSet::get(C, std::make_pair(*IdxI++, A)));
799
800 return get(C, AttrSet);
801}
802
Bill Wendling211316c2013-04-18 20:17:28 +0000803AttributeSet AttributeSet::addAttributes(LLVMContext &C, unsigned Index,
Bill Wendlingd509a662013-01-29 00:34:06 +0000804 AttributeSet Attrs) const {
805 if (!pImpl) return Attrs;
806 if (!Attrs.pImpl) return *this;
807
808#ifndef NDEBUG
809 // FIXME it is not obvious how this should work for alignment. For now, say
810 // we can't change a known alignment.
Bill Wendling211316c2013-04-18 20:17:28 +0000811 unsigned OldAlign = getParamAlignment(Index);
812 unsigned NewAlign = Attrs.getParamAlignment(Index);
Bill Wendlingd509a662013-01-29 00:34:06 +0000813 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
814 "Attempt to change alignment!");
815#endif
816
817 // Add the attribute slots before the one we're trying to add.
818 SmallVector<AttributeSet, 4> AttrSet;
819 uint64_t NumAttrs = pImpl->getNumAttributes();
820 AttributeSet AS;
821 uint64_t LastIndex = 0;
822 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
Bill Wendling211316c2013-04-18 20:17:28 +0000823 if (getSlotIndex(I) >= Index) {
824 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
Bill Wendlingd509a662013-01-29 00:34:06 +0000825 break;
826 }
827 LastIndex = I + 1;
828 AttrSet.push_back(getSlotAttributes(I));
829 }
830
831 // Now add the attribute into the correct slot. There may already be an
832 // AttributeSet there.
Bill Wendling211316c2013-04-18 20:17:28 +0000833 AttrBuilder B(AS, Index);
Bill Wendlingd509a662013-01-29 00:34:06 +0000834
835 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
Bill Wendling211316c2013-04-18 20:17:28 +0000836 if (Attrs.getSlotIndex(I) == Index) {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000837 for (AttributeSetImpl::iterator II = Attrs.pImpl->begin(I),
Bill Wendlingd509a662013-01-29 00:34:06 +0000838 IE = Attrs.pImpl->end(I); II != IE; ++II)
Bill Wendling23804da2013-01-31 23:38:01 +0000839 B.addAttribute(*II);
Bill Wendlingd509a662013-01-29 00:34:06 +0000840 break;
841 }
842
Bill Wendling211316c2013-04-18 20:17:28 +0000843 AttrSet.push_back(AttributeSet::get(C, Index, B));
Bill Wendlingd509a662013-01-29 00:34:06 +0000844
845 // Add the remaining attribute slots.
846 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
847 AttrSet.push_back(getSlotAttributes(I));
848
849 return get(C, AttrSet);
850}
851
Bill Wendling211316c2013-04-18 20:17:28 +0000852AttributeSet AttributeSet::removeAttribute(LLVMContext &C, unsigned Index,
Bill Wendlingd509a662013-01-29 00:34:06 +0000853 Attribute::AttrKind Attr) const {
Bill Wendling211316c2013-04-18 20:17:28 +0000854 if (!hasAttribute(Index, Attr)) return *this;
855 return removeAttributes(C, Index, AttributeSet::get(C, Index, Attr));
Bill Wendlingd509a662013-01-29 00:34:06 +0000856}
857
Bill Wendling211316c2013-04-18 20:17:28 +0000858AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Index,
Bill Wendlingd509a662013-01-29 00:34:06 +0000859 AttributeSet Attrs) const {
860 if (!pImpl) return AttributeSet();
861 if (!Attrs.pImpl) return *this;
862
Pete Cooper67cf9a72015-11-19 05:56:52 +0000863 // FIXME it is not obvious how this should work for alignment.
864 // For now, say we can't pass in alignment, which no current use does.
865 assert(!Attrs.hasAttribute(Index, Attribute::Alignment) &&
866 "Attempt to change alignment!");
867
Bill Wendlingd509a662013-01-29 00:34:06 +0000868 // Add the attribute slots before the one we're trying to add.
869 SmallVector<AttributeSet, 4> AttrSet;
870 uint64_t NumAttrs = pImpl->getNumAttributes();
871 AttributeSet AS;
872 uint64_t LastIndex = 0;
873 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
Bill Wendling211316c2013-04-18 20:17:28 +0000874 if (getSlotIndex(I) >= Index) {
875 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
Bill Wendlingd509a662013-01-29 00:34:06 +0000876 break;
877 }
878 LastIndex = I + 1;
879 AttrSet.push_back(getSlotAttributes(I));
880 }
881
Bill Wendlingd2196752013-01-30 23:07:40 +0000882 // Now remove the attribute from the correct slot. There may already be an
Bill Wendlingd509a662013-01-29 00:34:06 +0000883 // AttributeSet there.
Bill Wendling211316c2013-04-18 20:17:28 +0000884 AttrBuilder B(AS, Index);
Bill Wendlingd509a662013-01-29 00:34:06 +0000885
886 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
Bill Wendling211316c2013-04-18 20:17:28 +0000887 if (Attrs.getSlotIndex(I) == Index) {
888 B.removeAttributes(Attrs.pImpl->getSlotAttributes(I), Index);
Bill Wendlingd509a662013-01-29 00:34:06 +0000889 break;
890 }
891
Bill Wendling211316c2013-04-18 20:17:28 +0000892 AttrSet.push_back(AttributeSet::get(C, Index, B));
Bill Wendlingd509a662013-01-29 00:34:06 +0000893
894 // Add the remaining attribute slots.
895 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
896 AttrSet.push_back(getSlotAttributes(I));
897
898 return get(C, AttrSet);
899}
900
Pete Cooperd2a44612015-05-06 23:19:43 +0000901AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Index,
902 const AttrBuilder &Attrs) const {
903 if (!pImpl) return AttributeSet();
904
905 // FIXME it is not obvious how this should work for alignment.
906 // For now, say we can't pass in alignment, which no current use does.
907 assert(!Attrs.hasAlignmentAttr() && "Attempt to change alignment!");
908
909 // Add the attribute slots before the one we're trying to add.
910 SmallVector<AttributeSet, 4> AttrSet;
911 uint64_t NumAttrs = pImpl->getNumAttributes();
912 AttributeSet AS;
913 uint64_t LastIndex = 0;
914 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
915 if (getSlotIndex(I) >= Index) {
916 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
917 break;
918 }
919 LastIndex = I + 1;
920 AttrSet.push_back(getSlotAttributes(I));
921 }
922
923 // Now remove the attribute from the correct slot. There may already be an
924 // AttributeSet there.
925 AttrBuilder B(AS, Index);
926 B.remove(Attrs);
927
928 AttrSet.push_back(AttributeSet::get(C, Index, B));
929
930 // Add the remaining attribute slots.
931 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
932 AttrSet.push_back(getSlotAttributes(I));
933
934 return get(C, AttrSet);
935}
936
Ramkumar Ramachandra8fcb4982015-02-14 19:37:54 +0000937AttributeSet AttributeSet::addDereferenceableAttr(LLVMContext &C, unsigned Index,
938 uint64_t Bytes) const {
939 llvm::AttrBuilder B;
940 B.addDereferenceableAttr(Bytes);
941 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
942}
943
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000944AttributeSet AttributeSet::addDereferenceableOrNullAttr(LLVMContext &C,
945 unsigned Index,
946 uint64_t Bytes) const {
947 llvm::AttrBuilder B;
948 B.addDereferenceableOrNullAttr(Bytes);
949 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
950}
951
Bill Wendlingd509a662013-01-29 00:34:06 +0000952//===----------------------------------------------------------------------===//
953// AttributeSet Accessor Methods
954//===----------------------------------------------------------------------===//
955
Bill Wendling5d020a32013-02-10 05:00:40 +0000956LLVMContext &AttributeSet::getContext() const {
957 return pImpl->getContext();
958}
959
Bill Wendling211316c2013-04-18 20:17:28 +0000960AttributeSet AttributeSet::getParamAttributes(unsigned Index) const {
961 return pImpl && hasAttributes(Index) ?
Bill Wendlingd509a662013-01-29 00:34:06 +0000962 AttributeSet::get(pImpl->getContext(),
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000963 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendling211316c2013-04-18 20:17:28 +0000964 std::make_pair(Index, getAttributes(Index)))) :
Bill Wendlingd509a662013-01-29 00:34:06 +0000965 AttributeSet();
966}
967
968AttributeSet AttributeSet::getRetAttributes() const {
969 return pImpl && hasAttributes(ReturnIndex) ?
970 AttributeSet::get(pImpl->getContext(),
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000971 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendlingd509a662013-01-29 00:34:06 +0000972 std::make_pair(ReturnIndex,
973 getAttributes(ReturnIndex)))) :
974 AttributeSet();
975}
976
977AttributeSet AttributeSet::getFnAttributes() const {
978 return pImpl && hasAttributes(FunctionIndex) ?
979 AttributeSet::get(pImpl->getContext(),
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000980 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendlingd509a662013-01-29 00:34:06 +0000981 std::make_pair(FunctionIndex,
982 getAttributes(FunctionIndex)))) :
983 AttributeSet();
984}
985
986bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000987 AttributeSetNode *ASN = getAttributes(Index);
988 return ASN ? ASN->hasAttribute(Kind) : false;
Bill Wendlingd509a662013-01-29 00:34:06 +0000989}
990
Bill Wendlingbce7b972013-02-13 08:42:21 +0000991bool AttributeSet::hasAttribute(unsigned Index, StringRef Kind) const {
992 AttributeSetNode *ASN = getAttributes(Index);
993 return ASN ? ASN->hasAttribute(Kind) : false;
994}
995
Bill Wendlingd509a662013-01-29 00:34:06 +0000996bool AttributeSet::hasAttributes(unsigned Index) const {
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000997 AttributeSetNode *ASN = getAttributes(Index);
998 return ASN ? ASN->hasAttributes() : false;
Bill Wendlingd509a662013-01-29 00:34:06 +0000999}
1000
1001/// \brief Return true if the specified attribute is set for at least one
1002/// parameter or for the return value.
1003bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr) const {
Craig Topperc6207612014-04-09 06:08:46 +00001004 if (!pImpl) return false;
Bill Wendlingd509a662013-01-29 00:34:06 +00001005
1006 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
Benjamin Kramer741146b2013-07-11 12:13:16 +00001007 for (AttributeSetImpl::iterator II = pImpl->begin(I),
Bill Wendlingd509a662013-01-29 00:34:06 +00001008 IE = pImpl->end(I); II != IE; ++II)
NAKAMURA Takumib0944392013-01-31 03:47:28 +00001009 if (II->hasAttribute(Attr))
Bill Wendlingd509a662013-01-29 00:34:06 +00001010 return true;
1011
1012 return false;
1013}
1014
Bill Wendlingbce7b972013-02-13 08:42:21 +00001015Attribute AttributeSet::getAttribute(unsigned Index,
1016 Attribute::AttrKind Kind) const {
1017 AttributeSetNode *ASN = getAttributes(Index);
1018 return ASN ? ASN->getAttribute(Kind) : Attribute();
1019}
1020
1021Attribute AttributeSet::getAttribute(unsigned Index,
1022 StringRef Kind) const {
1023 AttributeSetNode *ASN = getAttributes(Index);
1024 return ASN ? ASN->getAttribute(Kind) : Attribute();
1025}
1026
Bill Wendlingf2955aa2013-01-29 03:20:31 +00001027unsigned AttributeSet::getParamAlignment(unsigned Index) const {
1028 AttributeSetNode *ASN = getAttributes(Index);
1029 return ASN ? ASN->getAlignment() : 0;
Bill Wendlingd509a662013-01-29 00:34:06 +00001030}
1031
1032unsigned AttributeSet::getStackAlignment(unsigned Index) const {
Bill Wendlingf2955aa2013-01-29 03:20:31 +00001033 AttributeSetNode *ASN = getAttributes(Index);
1034 return ASN ? ASN->getStackAlignment() : 0;
Bill Wendlingd509a662013-01-29 00:34:06 +00001035}
1036
Hal Finkelb0407ba2014-07-18 15:51:28 +00001037uint64_t AttributeSet::getDereferenceableBytes(unsigned Index) const {
1038 AttributeSetNode *ASN = getAttributes(Index);
1039 return ASN ? ASN->getDereferenceableBytes() : 0;
1040}
1041
Sanjoy Das06cf33f2015-05-06 17:41:54 +00001042uint64_t AttributeSet::getDereferenceableOrNullBytes(unsigned Index) const {
1043 AttributeSetNode *ASN = getAttributes(Index);
1044 return ASN ? ASN->getDereferenceableOrNullBytes() : 0;
1045}
1046
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +00001047std::string AttributeSet::getAsString(unsigned Index,
Bill Wendling829b4782013-02-11 08:43:33 +00001048 bool InAttrGrp) const {
Bill Wendlingf2955aa2013-01-29 03:20:31 +00001049 AttributeSetNode *ASN = getAttributes(Index);
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +00001050 return ASN ? ASN->getAsString(InAttrGrp) : std::string("");
Bill Wendlingd509a662013-01-29 00:34:06 +00001051}
1052
1053/// \brief The attributes for the specified index are returned.
Bill Wendling211316c2013-04-18 20:17:28 +00001054AttributeSetNode *AttributeSet::getAttributes(unsigned Index) const {
Craig Topperc6207612014-04-09 06:08:46 +00001055 if (!pImpl) return nullptr;
Bill Wendlingd509a662013-01-29 00:34:06 +00001056
Bill Wendlingf2955aa2013-01-29 03:20:31 +00001057 // Loop through to find the attribute node we want.
1058 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
Bill Wendling211316c2013-04-18 20:17:28 +00001059 if (pImpl->getSlotIndex(I) == Index)
Bill Wendlingf2955aa2013-01-29 03:20:31 +00001060 return pImpl->getSlotNode(I);
Bill Wendlingd509a662013-01-29 00:34:06 +00001061
Craig Topperc6207612014-04-09 06:08:46 +00001062 return nullptr;
Bill Wendlingd509a662013-01-29 00:34:06 +00001063}
1064
Bill Wendling211316c2013-04-18 20:17:28 +00001065AttributeSet::iterator AttributeSet::begin(unsigned Slot) const {
Bill Wendlinga9174862013-01-31 23:53:05 +00001066 if (!pImpl)
1067 return ArrayRef<Attribute>().begin();
Bill Wendling211316c2013-04-18 20:17:28 +00001068 return pImpl->begin(Slot);
Bill Wendlinga9174862013-01-31 23:53:05 +00001069}
1070
Bill Wendling211316c2013-04-18 20:17:28 +00001071AttributeSet::iterator AttributeSet::end(unsigned Slot) const {
Bill Wendlinga9174862013-01-31 23:53:05 +00001072 if (!pImpl)
1073 return ArrayRef<Attribute>().end();
Bill Wendling211316c2013-04-18 20:17:28 +00001074 return pImpl->end(Slot);
Bill Wendlinga9174862013-01-31 23:53:05 +00001075}
1076
Bill Wendlingd509a662013-01-29 00:34:06 +00001077//===----------------------------------------------------------------------===//
1078// AttributeSet Introspection Methods
1079//===----------------------------------------------------------------------===//
1080
1081/// \brief Return the number of slots used in this attribute list. This is the
1082/// number of arguments that have an attribute set on them (including the
1083/// function itself).
1084unsigned AttributeSet::getNumSlots() const {
1085 return pImpl ? pImpl->getNumAttributes() : 0;
1086}
1087
Rafael Espindoladd275302013-04-30 16:53:38 +00001088unsigned AttributeSet::getSlotIndex(unsigned Slot) const {
Bill Wendlingd509a662013-01-29 00:34:06 +00001089 assert(pImpl && Slot < pImpl->getNumAttributes() &&
1090 "Slot # out of range!");
1091 return pImpl->getSlotIndex(Slot);
1092}
1093
1094AttributeSet AttributeSet::getSlotAttributes(unsigned Slot) const {
1095 assert(pImpl && Slot < pImpl->getNumAttributes() &&
1096 "Slot # out of range!");
1097 return pImpl->getSlotAttributes(Slot);
1098}
1099
1100uint64_t AttributeSet::Raw(unsigned Index) const {
1101 // FIXME: Remove this.
1102 return pImpl ? pImpl->Raw(Index) : 0;
1103}
1104
1105void AttributeSet::dump() const {
1106 dbgs() << "PAL[\n";
1107
1108 for (unsigned i = 0, e = getNumSlots(); i < e; ++i) {
1109 uint64_t Index = getSlotIndex(i);
1110 dbgs() << " { ";
1111 if (Index == ~0U)
1112 dbgs() << "~0U";
1113 else
1114 dbgs() << Index;
1115 dbgs() << " => " << getAsString(Index) << " }\n";
1116 }
1117
1118 dbgs() << "]\n";
1119}
1120
Bill Wendlingabf3feb2012-10-05 06:44:41 +00001121//===----------------------------------------------------------------------===//
Bill Wendlingcd330342013-01-04 23:27:34 +00001122// AttrBuilder Method Implementations
Bill Wendlingabf3feb2012-10-05 06:44:41 +00001123//===----------------------------------------------------------------------===//
1124
Bill Wendling211316c2013-04-18 20:17:28 +00001125AttrBuilder::AttrBuilder(AttributeSet AS, unsigned Index)
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001126 : Attrs(0), Alignment(0), StackAlignment(0), DerefBytes(0),
1127 DerefOrNullBytes(0) {
Bill Wendling56b0b2a2013-01-27 21:23:46 +00001128 AttributeSetImpl *pImpl = AS.pImpl;
Bill Wendling096f5442013-01-07 08:24:35 +00001129 if (!pImpl) return;
1130
Bill Wendling9eb689c2013-01-28 00:21:34 +00001131 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I) {
Bill Wendling211316c2013-04-18 20:17:28 +00001132 if (pImpl->getSlotIndex(I) != Index) continue;
Bill Wendling096f5442013-01-07 08:24:35 +00001133
Benjamin Kramer741146b2013-07-11 12:13:16 +00001134 for (AttributeSetImpl::iterator II = pImpl->begin(I),
Bill Wendling9eb689c2013-01-28 00:21:34 +00001135 IE = pImpl->end(I); II != IE; ++II)
Bill Wendling23804da2013-01-31 23:38:01 +00001136 addAttribute(*II);
Bill Wendling9eb689c2013-01-28 00:21:34 +00001137
1138 break;
1139 }
Bill Wendling096f5442013-01-07 08:24:35 +00001140}
1141
Bill Wendlingcd330342013-01-04 23:27:34 +00001142void AttrBuilder::clear() {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001143 Attrs.reset();
Sanjoy Das88d0fde2015-09-03 22:27:42 +00001144 TargetDepAttrs.clear();
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001145 Alignment = StackAlignment = DerefBytes = DerefOrNullBytes = 0;
Bill Wendlingcd330342013-01-04 23:27:34 +00001146}
1147
1148AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001149 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
Bill Wendling1c7cc8a2013-01-31 23:16:25 +00001150 assert(Val != Attribute::Alignment && Val != Attribute::StackAlignment &&
Hal Finkelb0407ba2014-07-18 15:51:28 +00001151 Val != Attribute::Dereferenceable &&
1152 "Adding integer attribute without adding a value!");
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001153 Attrs[Val] = true;
Bill Wendling7c04e042012-10-09 19:01:18 +00001154 return *this;
Bill Wendlingabf3feb2012-10-05 06:44:41 +00001155}
1156
Bill Wendling23804da2013-01-31 23:38:01 +00001157AttrBuilder &AttrBuilder::addAttribute(Attribute Attr) {
Bill Wendling0a437302013-02-10 10:13:23 +00001158 if (Attr.isStringAttribute()) {
1159 addAttribute(Attr.getKindAsString(), Attr.getValueAsString());
1160 return *this;
1161 }
1162
Bill Wendling3f12ac22013-02-05 22:37:24 +00001163 Attribute::AttrKind Kind = Attr.getKindAsEnum();
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001164 Attrs[Kind] = true;
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001165
Bill Wendling3f12ac22013-02-05 22:37:24 +00001166 if (Kind == Attribute::Alignment)
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001167 Alignment = Attr.getAlignment();
Bill Wendling3f12ac22013-02-05 22:37:24 +00001168 else if (Kind == Attribute::StackAlignment)
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001169 StackAlignment = Attr.getStackAlignment();
Hal Finkelb0407ba2014-07-18 15:51:28 +00001170 else if (Kind == Attribute::Dereferenceable)
1171 DerefBytes = Attr.getDereferenceableBytes();
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001172 else if (Kind == Attribute::DereferenceableOrNull)
1173 DerefOrNullBytes = Attr.getDereferenceableOrNullBytes();
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001174 return *this;
1175}
1176
Bill Wendlingb9c5b1a2013-02-05 08:09:32 +00001177AttrBuilder &AttrBuilder::addAttribute(StringRef A, StringRef V) {
1178 TargetDepAttrs[A] = V;
1179 return *this;
1180}
1181
Bill Wendling23804da2013-01-31 23:38:01 +00001182AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001183 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
1184 Attrs[Val] = false;
Bill Wendling23804da2013-01-31 23:38:01 +00001185
1186 if (Val == Attribute::Alignment)
1187 Alignment = 0;
1188 else if (Val == Attribute::StackAlignment)
1189 StackAlignment = 0;
Hal Finkelb0407ba2014-07-18 15:51:28 +00001190 else if (Val == Attribute::Dereferenceable)
1191 DerefBytes = 0;
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001192 else if (Val == Attribute::DereferenceableOrNull)
1193 DerefOrNullBytes = 0;
Bill Wendling23804da2013-01-31 23:38:01 +00001194
1195 return *this;
1196}
1197
Bill Wendlingd2196752013-01-30 23:07:40 +00001198AttrBuilder &AttrBuilder::removeAttributes(AttributeSet A, uint64_t Index) {
Bill Wendling211316c2013-04-18 20:17:28 +00001199 unsigned Slot = ~0U;
Bill Wendlingf1c94e32013-02-01 00:13:50 +00001200 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
1201 if (A.getSlotIndex(I) == Index) {
Bill Wendling211316c2013-04-18 20:17:28 +00001202 Slot = I;
Bill Wendlingf1c94e32013-02-01 00:13:50 +00001203 break;
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001204 }
Bill Wendlingf1c94e32013-02-01 00:13:50 +00001205
Bill Wendling211316c2013-04-18 20:17:28 +00001206 assert(Slot != ~0U && "Couldn't find index in AttributeSet!");
Bill Wendlingf1c94e32013-02-01 00:13:50 +00001207
Bill Wendling211316c2013-04-18 20:17:28 +00001208 for (AttributeSet::iterator I = A.begin(Slot), E = A.end(Slot); I != E; ++I) {
Bill Wendling7cde51d2013-02-12 07:56:49 +00001209 Attribute Attr = *I;
Hal Finkele15442c2014-07-18 06:51:55 +00001210 if (Attr.isEnumAttribute() || Attr.isIntAttribute()) {
Bill Wendling7cde51d2013-02-12 07:56:49 +00001211 Attribute::AttrKind Kind = I->getKindAsEnum();
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001212 Attrs[Kind] = false;
Bill Wendlingf1c94e32013-02-01 00:13:50 +00001213
Bill Wendling7cde51d2013-02-12 07:56:49 +00001214 if (Kind == Attribute::Alignment)
1215 Alignment = 0;
1216 else if (Kind == Attribute::StackAlignment)
1217 StackAlignment = 0;
Hal Finkelb0407ba2014-07-18 15:51:28 +00001218 else if (Kind == Attribute::Dereferenceable)
1219 DerefBytes = 0;
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001220 else if (Kind == Attribute::DereferenceableOrNull)
1221 DerefOrNullBytes = 0;
Bill Wendling7cde51d2013-02-12 07:56:49 +00001222 } else {
1223 assert(Attr.isStringAttribute() && "Invalid attribute type!");
1224 std::map<std::string, std::string>::iterator
1225 Iter = TargetDepAttrs.find(Attr.getKindAsString());
1226 if (Iter != TargetDepAttrs.end())
1227 TargetDepAttrs.erase(Iter);
1228 }
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001229 }
1230
1231 return *this;
1232}
1233
Bill Wendlingb9c5b1a2013-02-05 08:09:32 +00001234AttrBuilder &AttrBuilder::removeAttribute(StringRef A) {
1235 std::map<std::string, std::string>::iterator I = TargetDepAttrs.find(A);
1236 if (I != TargetDepAttrs.end())
1237 TargetDepAttrs.erase(I);
1238 return *this;
1239}
1240
Bill Wendling50d27842012-10-15 20:35:56 +00001241AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
Bill Wendlingabd5ba22012-10-14 03:58:29 +00001242 if (Align == 0) return *this;
Bill Wendlingcd330342013-01-04 23:27:34 +00001243
Bill Wendlingabf3feb2012-10-05 06:44:41 +00001244 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1245 assert(Align <= 0x40000000 && "Alignment too large.");
Bill Wendlingcd330342013-01-04 23:27:34 +00001246
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001247 Attrs[Attribute::Alignment] = true;
Bill Wendlingcd330342013-01-04 23:27:34 +00001248 Alignment = Align;
Bill Wendlingabd5ba22012-10-14 03:58:29 +00001249 return *this;
Chris Lattner3e13b8c2008-01-02 23:42:30 +00001250}
1251
Bill Wendlingcd330342013-01-04 23:27:34 +00001252AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
1253 // Default alignment, allow the target to define how to align it.
1254 if (Align == 0) return *this;
1255
1256 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1257 assert(Align <= 0x100 && "Alignment too large.");
1258
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001259 Attrs[Attribute::StackAlignment] = true;
Bill Wendlingcd330342013-01-04 23:27:34 +00001260 StackAlignment = Align;
1261 return *this;
1262}
1263
Hal Finkelb0407ba2014-07-18 15:51:28 +00001264AttrBuilder &AttrBuilder::addDereferenceableAttr(uint64_t Bytes) {
1265 if (Bytes == 0) return *this;
1266
1267 Attrs[Attribute::Dereferenceable] = true;
1268 DerefBytes = Bytes;
1269 return *this;
1270}
1271
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001272AttrBuilder &AttrBuilder::addDereferenceableOrNullAttr(uint64_t Bytes) {
1273 if (Bytes == 0)
1274 return *this;
1275
1276 Attrs[Attribute::DereferenceableOrNull] = true;
1277 DerefOrNullBytes = Bytes;
1278 return *this;
1279}
1280
Bill Wendlinge2614922013-02-06 01:16:00 +00001281AttrBuilder &AttrBuilder::merge(const AttrBuilder &B) {
1282 // FIXME: What if both have alignments, but they don't match?!
1283 if (!Alignment)
1284 Alignment = B.Alignment;
1285
1286 if (!StackAlignment)
1287 StackAlignment = B.StackAlignment;
1288
Hal Finkelb0407ba2014-07-18 15:51:28 +00001289 if (!DerefBytes)
1290 DerefBytes = B.DerefBytes;
1291
Pete Cooperd2a44612015-05-06 23:19:43 +00001292 if (!DerefOrNullBytes)
1293 DerefOrNullBytes = B.DerefOrNullBytes;
1294
Benjamin Kramer45e7d532013-02-16 19:13:18 +00001295 Attrs |= B.Attrs;
Bill Wendlinge2614922013-02-06 01:16:00 +00001296
Pete Cooperd2a44612015-05-06 23:19:43 +00001297 for (auto I : B.td_attrs())
1298 TargetDepAttrs[I.first] = I.second;
Bill Wendlinge2614922013-02-06 01:16:00 +00001299
1300 return *this;
1301}
1302
Pete Cooperd2a44612015-05-06 23:19:43 +00001303AttrBuilder &AttrBuilder::remove(const AttrBuilder &B) {
1304 // FIXME: What if both have alignments, but they don't match?!
1305 if (B.Alignment)
1306 Alignment = 0;
1307
1308 if (B.StackAlignment)
1309 StackAlignment = 0;
1310
1311 if (B.DerefBytes)
1312 DerefBytes = 0;
1313
1314 if (B.DerefOrNullBytes)
1315 DerefOrNullBytes = 0;
1316
1317 Attrs &= ~B.Attrs;
1318
1319 for (auto I : B.td_attrs())
1320 TargetDepAttrs.erase(I.first);
1321
1322 return *this;
1323}
1324
1325bool AttrBuilder::overlaps(const AttrBuilder &B) const {
1326 // First check if any of the target independent attributes overlap.
1327 if ((Attrs & B.Attrs).any())
1328 return true;
1329
1330 // Then check if any target dependent ones do.
1331 for (auto I : td_attrs())
1332 if (B.contains(I.first))
1333 return true;
1334
1335 return false;
1336}
1337
Bill Wendling4b001442013-02-06 01:33:42 +00001338bool AttrBuilder::contains(StringRef A) const {
1339 return TargetDepAttrs.find(A) != TargetDepAttrs.end();
1340}
1341
Bill Wendling50d27842012-10-15 20:35:56 +00001342bool AttrBuilder::hasAttributes() const {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001343 return !Attrs.none() || !TargetDepAttrs.empty();
Bill Wendlingc6daefa2012-10-08 23:27:46 +00001344}
Bill Wendling9ac69f92013-01-04 20:54:35 +00001345
Bill Wendlingd2196752013-01-30 23:07:40 +00001346bool AttrBuilder::hasAttributes(AttributeSet A, uint64_t Index) const {
Bill Wendling211316c2013-04-18 20:17:28 +00001347 unsigned Slot = ~0U;
Bill Wendling9ca01da2013-02-02 00:42:06 +00001348 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
1349 if (A.getSlotIndex(I) == Index) {
Bill Wendling211316c2013-04-18 20:17:28 +00001350 Slot = I;
Bill Wendling9ca01da2013-02-02 00:42:06 +00001351 break;
1352 }
1353
Bill Wendling211316c2013-04-18 20:17:28 +00001354 assert(Slot != ~0U && "Couldn't find the index!");
Bill Wendling9ca01da2013-02-02 00:42:06 +00001355
Bill Wendling211316c2013-04-18 20:17:28 +00001356 for (AttributeSet::iterator I = A.begin(Slot), E = A.end(Slot);
Bill Wendling7cde51d2013-02-12 07:56:49 +00001357 I != E; ++I) {
1358 Attribute Attr = *I;
Hal Finkele15442c2014-07-18 06:51:55 +00001359 if (Attr.isEnumAttribute() || Attr.isIntAttribute()) {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001360 if (Attrs[I->getKindAsEnum()])
Bill Wendling7cde51d2013-02-12 07:56:49 +00001361 return true;
1362 } else {
1363 assert(Attr.isStringAttribute() && "Invalid attribute kind!");
1364 return TargetDepAttrs.find(Attr.getKindAsString())!=TargetDepAttrs.end();
1365 }
1366 }
Bill Wendling9ca01da2013-02-02 00:42:06 +00001367
1368 return false;
Bill Wendling70f39172012-10-09 00:01:21 +00001369}
Bill Wendling9ac69f92013-01-04 20:54:35 +00001370
Bill Wendling50d27842012-10-15 20:35:56 +00001371bool AttrBuilder::hasAlignmentAttr() const {
Bill Wendlingcd330342013-01-04 23:27:34 +00001372 return Alignment != 0;
Bill Wendlingc6daefa2012-10-08 23:27:46 +00001373}
1374
Bill Wendlingd509a662013-01-29 00:34:06 +00001375bool AttrBuilder::operator==(const AttrBuilder &B) {
Benjamin Kramer45e7d532013-02-16 19:13:18 +00001376 if (Attrs != B.Attrs)
1377 return false;
Bill Wendling4b001442013-02-06 01:33:42 +00001378
1379 for (td_const_iterator I = TargetDepAttrs.begin(),
1380 E = TargetDepAttrs.end(); I != E; ++I)
1381 if (B.TargetDepAttrs.find(I->first) == B.TargetDepAttrs.end())
1382 return false;
1383
Hal Finkelb0407ba2014-07-18 15:51:28 +00001384 return Alignment == B.Alignment && StackAlignment == B.StackAlignment &&
1385 DerefBytes == B.DerefBytes;
Bill Wendlingd509a662013-01-29 00:34:06 +00001386}
1387
1388AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) {
Bill Wendling56aeccc2013-02-04 23:32:23 +00001389 // FIXME: Remove this in 4.0.
Bill Wendling60011b82013-01-29 01:43:29 +00001390 if (!Val) return *this;
1391
Bill Wendlingd509a662013-01-29 00:34:06 +00001392 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
1393 I = Attribute::AttrKind(I + 1)) {
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001394 if (I == Attribute::Dereferenceable ||
Igor Laevsky39d662f2015-07-11 10:30:36 +00001395 I == Attribute::DereferenceableOrNull ||
1396 I == Attribute::ArgMemOnly)
Hal Finkelb0407ba2014-07-18 15:51:28 +00001397 continue;
Bill Wendlingd509a662013-01-29 00:34:06 +00001398 if (uint64_t A = (Val & AttributeImpl::getAttrMask(I))) {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001399 Attrs[I] = true;
Bill Wendlingd509a662013-01-29 00:34:06 +00001400
1401 if (I == Attribute::Alignment)
1402 Alignment = 1ULL << ((A >> 16) - 1);
1403 else if (I == Attribute::StackAlignment)
1404 StackAlignment = 1ULL << ((A >> 26)-1);
1405 }
1406 }
1407
1408 return *this;
1409}
1410
Bill Wendling57625a42013-01-25 23:09:36 +00001411//===----------------------------------------------------------------------===//
1412// AttributeFuncs Function Defintions
1413//===----------------------------------------------------------------------===//
1414
Bill Wendlingc79cdff2013-02-01 01:04:27 +00001415/// \brief Which attributes cannot be applied to a type.
Craig Toppere3dcce92015-08-01 22:20:21 +00001416AttrBuilder AttributeFuncs::typeIncompatible(Type *Ty) {
Bill Wendling57625a42013-01-25 23:09:36 +00001417 AttrBuilder Incompatible;
1418
1419 if (!Ty->isIntegerTy())
1420 // Attribute that only apply to integers.
1421 Incompatible.addAttribute(Attribute::SExt)
1422 .addAttribute(Attribute::ZExt);
1423
1424 if (!Ty->isPointerTy())
1425 // Attribute that only apply to pointers.
1426 Incompatible.addAttribute(Attribute::ByVal)
1427 .addAttribute(Attribute::Nest)
1428 .addAttribute(Attribute::NoAlias)
1429 .addAttribute(Attribute::NoCapture)
Nick Lewyckyd52b1522014-05-20 01:23:40 +00001430 .addAttribute(Attribute::NonNull)
Hal Finkelb0407ba2014-07-18 15:51:28 +00001431 .addDereferenceableAttr(1) // the int here is ignored
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001432 .addDereferenceableOrNullAttr(1) // the int here is ignored
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001433 .addAttribute(Attribute::ReadNone)
1434 .addAttribute(Attribute::ReadOnly)
Reid Klecknera534a382013-12-19 02:14:12 +00001435 .addAttribute(Attribute::StructRet)
1436 .addAttribute(Attribute::InAlloca);
Bill Wendling57625a42013-01-25 23:09:36 +00001437
Pete Cooper2777d8872015-05-06 23:19:56 +00001438 return Incompatible;
Bill Wendling57625a42013-01-25 23:09:36 +00001439}