blob: 95ddc3628a0db14af7874b43c7f5657317536d89 [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"
Akira Hatanaka1cb242e2015-12-22 23:57:37 +000017#include "llvm/IR/Function.h"
Bill Wendling4607f4b2012-12-20 01:36:59 +000018#include "AttributeImpl.h"
Bill Wendlinge38b8042012-09-26 21:07:29 +000019#include "LLVMContextImpl.h"
Benjamin Kramer502b9e12014-04-12 16:15:53 +000020#include "llvm/ADT/STLExtras.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000021#include "llvm/ADT/StringExtras.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000022#include "llvm/IR/Type.h"
Benjamin Kramer17388a62014-03-03 18:02:34 +000023#include "llvm/Support/Atomic.h"
David Greenef7014732010-01-05 01:29:58 +000024#include "llvm/Support/Debug.h"
Chris Lattner3e13b8c2008-01-02 23:42:30 +000025#include "llvm/Support/ManagedStatic.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000026#include "llvm/Support/Mutex.h"
Benjamin Kramer1a25d732009-08-23 11:37:21 +000027#include "llvm/Support/raw_ostream.h"
Bill Wendlingd2e493b2013-01-24 00:06:56 +000028#include <algorithm>
Chris Lattner3e13b8c2008-01-02 23:42:30 +000029using namespace llvm;
30
Chris Lattner8a923e72008-03-12 17:45:29 +000031//===----------------------------------------------------------------------===//
Bill Wendling7707c5a2013-01-29 00:48:16 +000032// Attribute Construction Methods
Chris Lattner8a923e72008-03-12 17:45:29 +000033//===----------------------------------------------------------------------===//
Chris Lattnerd0e1f102008-01-03 00:10:22 +000034
George Burgess IV278199f2016-04-12 01:05:35 +000035// allocsize has two integer arguments, but because they're both 32 bits, we can
36// pack them into one 64-bit value, at the cost of making said value
37// nonsensical.
38//
39// In order to do this, we need to reserve one value of the second (optional)
40// allocsize argument to signify "not present."
George Burgess IV381fc0e2016-08-25 01:05:08 +000041static const unsigned AllocSizeNumElemsNotPresent = -1;
George Burgess IV278199f2016-04-12 01:05:35 +000042
43static uint64_t packAllocSizeArgs(unsigned ElemSizeArg,
44 const Optional<unsigned> &NumElemsArg) {
45 assert((!NumElemsArg.hasValue() ||
46 *NumElemsArg != AllocSizeNumElemsNotPresent) &&
47 "Attempting to pack a reserved value");
48
49 return uint64_t(ElemSizeArg) << 32 |
50 NumElemsArg.getValueOr(AllocSizeNumElemsNotPresent);
51}
52
53static std::pair<unsigned, Optional<unsigned>>
54unpackAllocSizeArgs(uint64_t Num) {
55 unsigned NumElems = Num & std::numeric_limits<unsigned>::max();
56 unsigned ElemSizeArg = Num >> 32;
57
58 Optional<unsigned> NumElemsArg;
59 if (NumElems != AllocSizeNumElemsNotPresent)
60 NumElemsArg = NumElems;
61 return std::make_pair(ElemSizeArg, NumElemsArg);
62}
63
Matt Arsenaulte1cef6e2014-09-03 23:24:31 +000064Attribute Attribute::get(LLVMContext &Context, Attribute::AttrKind Kind,
65 uint64_t Val) {
66 LLVMContextImpl *pImpl = Context.pImpl;
67 FoldingSetNodeID ID;
68 ID.AddInteger(Kind);
Matt Arsenaultfb4308e2014-09-03 23:38:05 +000069 if (Val) ID.AddInteger(Val);
Matt Arsenaulte1cef6e2014-09-03 23:24:31 +000070
71 void *InsertPoint;
72 AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
73
74 if (!PA) {
75 // If we didn't find any existing attributes of the same shape then create a
76 // new one and insert it.
Matt Arsenaultfb4308e2014-09-03 23:38:05 +000077 if (!Val)
78 PA = new EnumAttributeImpl(Kind);
79 else
80 PA = new IntAttributeImpl(Kind, Val);
Bill Wendling3f12ac22013-02-05 22:37:24 +000081 pImpl->AttrsSet.InsertNode(PA, InsertPoint);
82 }
83
84 // Return the Attribute that we found or created.
85 return Attribute(PA);
86}
87
88Attribute Attribute::get(LLVMContext &Context, StringRef Kind, StringRef Val) {
89 LLVMContextImpl *pImpl = Context.pImpl;
90 FoldingSetNodeID ID;
91 ID.AddString(Kind);
92 if (!Val.empty()) ID.AddString(Val);
Bill Wendling73ea2de2012-10-08 21:47:17 +000093
94 void *InsertPoint;
Bill Wendling4607f4b2012-12-20 01:36:59 +000095 AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
Bill Wendling73ea2de2012-10-08 21:47:17 +000096
97 if (!PA) {
98 // If we didn't find any existing attributes of the same shape then create a
99 // new one and insert it.
Benjamin Kramer741146b2013-07-11 12:13:16 +0000100 PA = new StringAttributeImpl(Kind, Val);
Bill Wendling73ea2de2012-10-08 21:47:17 +0000101 pImpl->AttrsSet.InsertNode(PA, InsertPoint);
102 }
103
Bill Wendlingb9c5b1a2013-02-05 08:09:32 +0000104 // Return the Attribute that we found or created.
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000105 return Attribute(PA);
Bill Wendling73ea2de2012-10-08 21:47:17 +0000106}
107
Bill Wendling4bbe9db2013-01-27 22:43:04 +0000108Attribute Attribute::getWithAlignment(LLVMContext &Context, uint64_t Align) {
Bill Wendling1c7cc8a2013-01-31 23:16:25 +0000109 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
110 assert(Align <= 0x40000000 && "Alignment too large.");
Bill Wendling3f12ac22013-02-05 22:37:24 +0000111 return get(Context, Alignment, Align);
Bill Wendling4bbe9db2013-01-27 22:43:04 +0000112}
113
114Attribute Attribute::getWithStackAlignment(LLVMContext &Context,
115 uint64_t Align) {
Bill Wendling1c7cc8a2013-01-31 23:16:25 +0000116 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
117 assert(Align <= 0x100 && "Alignment too large.");
Bill Wendling3f12ac22013-02-05 22:37:24 +0000118 return get(Context, StackAlignment, Align);
Bill Wendling4bbe9db2013-01-27 22:43:04 +0000119}
120
Hal Finkelb0407ba2014-07-18 15:51:28 +0000121Attribute Attribute::getWithDereferenceableBytes(LLVMContext &Context,
122 uint64_t Bytes) {
123 assert(Bytes && "Bytes must be non-zero.");
124 return get(Context, Dereferenceable, Bytes);
125}
126
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000127Attribute Attribute::getWithDereferenceableOrNullBytes(LLVMContext &Context,
128 uint64_t Bytes) {
129 assert(Bytes && "Bytes must be non-zero.");
130 return get(Context, DereferenceableOrNull, Bytes);
131}
132
George Burgess IV278199f2016-04-12 01:05:35 +0000133Attribute
134Attribute::getWithAllocSizeArgs(LLVMContext &Context, unsigned ElemSizeArg,
135 const Optional<unsigned> &NumElemsArg) {
136 assert(!(ElemSizeArg == 0 && NumElemsArg && *NumElemsArg == 0) &&
137 "Invalid allocsize arguments -- given allocsize(0, 0)");
138 return get(Context, AllocSize, packAllocSizeArgs(ElemSizeArg, NumElemsArg));
139}
140
Bill Wendling7707c5a2013-01-29 00:48:16 +0000141//===----------------------------------------------------------------------===//
142// Attribute Accessor Methods
143//===----------------------------------------------------------------------===//
144
Bill Wendling3f12ac22013-02-05 22:37:24 +0000145bool Attribute::isEnumAttribute() const {
146 return pImpl && pImpl->isEnumAttribute();
147}
148
Hal Finkele15442c2014-07-18 06:51:55 +0000149bool Attribute::isIntAttribute() const {
150 return pImpl && pImpl->isIntAttribute();
Bill Wendling3f12ac22013-02-05 22:37:24 +0000151}
152
153bool Attribute::isStringAttribute() const {
154 return pImpl && pImpl->isStringAttribute();
155}
156
157Attribute::AttrKind Attribute::getKindAsEnum() const {
Bill Wendling440e9d82013-07-25 00:34:29 +0000158 if (!pImpl) return None;
Hal Finkele15442c2014-07-18 06:51:55 +0000159 assert((isEnumAttribute() || isIntAttribute()) &&
Bill Wendling3f12ac22013-02-05 22:37:24 +0000160 "Invalid attribute type to get the kind as an enum!");
George Burgess IV500d3032015-12-16 05:21:02 +0000161 return pImpl->getKindAsEnum();
Bill Wendling3f12ac22013-02-05 22:37:24 +0000162}
163
164uint64_t Attribute::getValueAsInt() const {
Bill Wendling440e9d82013-07-25 00:34:29 +0000165 if (!pImpl) return 0;
Hal Finkele15442c2014-07-18 06:51:55 +0000166 assert(isIntAttribute() &&
167 "Expected the attribute to be an integer attribute!");
George Burgess IV500d3032015-12-16 05:21:02 +0000168 return pImpl->getValueAsInt();
Bill Wendling3f12ac22013-02-05 22:37:24 +0000169}
170
171StringRef Attribute::getKindAsString() const {
Bill Wendling440e9d82013-07-25 00:34:29 +0000172 if (!pImpl) return StringRef();
Bill Wendling3f12ac22013-02-05 22:37:24 +0000173 assert(isStringAttribute() &&
174 "Invalid attribute type to get the kind as a string!");
George Burgess IV500d3032015-12-16 05:21:02 +0000175 return pImpl->getKindAsString();
Bill Wendling3f12ac22013-02-05 22:37:24 +0000176}
177
178StringRef Attribute::getValueAsString() const {
Bill Wendling440e9d82013-07-25 00:34:29 +0000179 if (!pImpl) return StringRef();
Bill Wendling3f12ac22013-02-05 22:37:24 +0000180 assert(isStringAttribute() &&
181 "Invalid attribute type to get the value as a string!");
George Burgess IV500d3032015-12-16 05:21:02 +0000182 return pImpl->getValueAsString();
Bill Wendling3f12ac22013-02-05 22:37:24 +0000183}
184
Bill Wendlingae89a0f2013-02-05 23:48:36 +0000185bool Attribute::hasAttribute(AttrKind Kind) const {
186 return (pImpl && pImpl->hasAttribute(Kind)) || (!pImpl && Kind == None);
187}
188
189bool Attribute::hasAttribute(StringRef Kind) const {
190 if (!isStringAttribute()) return false;
191 return pImpl && pImpl->hasAttribute(Kind);
Bill Wendling03eefb32013-01-29 20:45:34 +0000192}
193
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000194unsigned Attribute::getAlignment() const {
Bill Wendlingc79cdff2013-02-01 01:04:27 +0000195 assert(hasAttribute(Attribute::Alignment) &&
196 "Trying to get alignment from non-alignment attribute!");
Bill Wendling3f12ac22013-02-05 22:37:24 +0000197 return pImpl->getValueAsInt();
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000198}
199
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000200unsigned Attribute::getStackAlignment() const {
Bill Wendlingc79cdff2013-02-01 01:04:27 +0000201 assert(hasAttribute(Attribute::StackAlignment) &&
202 "Trying to get alignment from non-alignment attribute!");
Bill Wendling3f12ac22013-02-05 22:37:24 +0000203 return pImpl->getValueAsInt();
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000204}
205
Hal Finkelb0407ba2014-07-18 15:51:28 +0000206uint64_t Attribute::getDereferenceableBytes() const {
207 assert(hasAttribute(Attribute::Dereferenceable) &&
208 "Trying to get dereferenceable bytes from "
209 "non-dereferenceable attribute!");
210 return pImpl->getValueAsInt();
211}
212
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000213uint64_t Attribute::getDereferenceableOrNullBytes() const {
214 assert(hasAttribute(Attribute::DereferenceableOrNull) &&
215 "Trying to get dereferenceable bytes from "
216 "non-dereferenceable attribute!");
217 return pImpl->getValueAsInt();
218}
219
George Burgess IV278199f2016-04-12 01:05:35 +0000220std::pair<unsigned, Optional<unsigned>> Attribute::getAllocSizeArgs() const {
221 assert(hasAttribute(Attribute::AllocSize) &&
222 "Trying to get allocsize args from non-allocsize attribute");
223 return unpackAllocSizeArgs(pImpl->getValueAsInt());
224}
225
Bill Wendling829b4782013-02-11 08:43:33 +0000226std::string Attribute::getAsString(bool InAttrGrp) const {
Bill Wendling9c2eba92013-01-31 20:59:05 +0000227 if (!pImpl) return "";
228
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000229 if (hasAttribute(Attribute::SanitizeAddress))
230 return "sanitize_address";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000231 if (hasAttribute(Attribute::AlwaysInline))
232 return "alwaysinline";
Igor Laevsky39d662f2015-07-11 10:30:36 +0000233 if (hasAttribute(Attribute::ArgMemOnly))
234 return "argmemonly";
Michael Gottesman41748d72013-06-27 00:25:01 +0000235 if (hasAttribute(Attribute::Builtin))
236 return "builtin";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000237 if (hasAttribute(Attribute::ByVal))
238 return "byval";
Owen Anderson85fa7d52015-05-26 23:48:40 +0000239 if (hasAttribute(Attribute::Convergent))
240 return "convergent";
Manman Ren9bfd0d02016-04-01 21:41:15 +0000241 if (hasAttribute(Attribute::SwiftError))
242 return "swifterror";
Manman Renf46262e2016-03-29 17:37:21 +0000243 if (hasAttribute(Attribute::SwiftSelf))
244 return "swiftself";
Vaivaswatha Nagarajfb3f4902015-12-16 16:16:19 +0000245 if (hasAttribute(Attribute::InaccessibleMemOnly))
246 return "inaccessiblememonly";
247 if (hasAttribute(Attribute::InaccessibleMemOrArgMemOnly))
248 return "inaccessiblemem_or_argmemonly";
Reid Klecknera534a382013-12-19 02:14:12 +0000249 if (hasAttribute(Attribute::InAlloca))
250 return "inalloca";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000251 if (hasAttribute(Attribute::InlineHint))
252 return "inlinehint";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000253 if (hasAttribute(Attribute::InReg))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000254 return "inreg";
Tom Roeder44cb65f2014-06-05 19:29:43 +0000255 if (hasAttribute(Attribute::JumpTable))
256 return "jumptable";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000257 if (hasAttribute(Attribute::MinSize))
258 return "minsize";
259 if (hasAttribute(Attribute::Naked))
260 return "naked";
261 if (hasAttribute(Attribute::Nest))
262 return "nest";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000263 if (hasAttribute(Attribute::NoAlias))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000264 return "noalias";
Bill Wendling09bd1f72013-02-22 00:12:35 +0000265 if (hasAttribute(Attribute::NoBuiltin))
266 return "nobuiltin";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000267 if (hasAttribute(Attribute::NoCapture))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000268 return "nocapture";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000269 if (hasAttribute(Attribute::NoDuplicate))
270 return "noduplicate";
271 if (hasAttribute(Attribute::NoImplicitFloat))
272 return "noimplicitfloat";
273 if (hasAttribute(Attribute::NoInline))
274 return "noinline";
275 if (hasAttribute(Attribute::NonLazyBind))
276 return "nonlazybind";
Nick Lewyckyd52b1522014-05-20 01:23:40 +0000277 if (hasAttribute(Attribute::NonNull))
278 return "nonnull";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000279 if (hasAttribute(Attribute::NoRedZone))
280 return "noredzone";
281 if (hasAttribute(Attribute::NoReturn))
282 return "noreturn";
James Molloye6f87ca2015-11-06 10:32:53 +0000283 if (hasAttribute(Attribute::NoRecurse))
284 return "norecurse";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000285 if (hasAttribute(Attribute::NoUnwind))
286 return "nounwind";
Andrea Di Biagio377496b2013-08-23 11:53:55 +0000287 if (hasAttribute(Attribute::OptimizeNone))
288 return "optnone";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000289 if (hasAttribute(Attribute::OptimizeForSize))
290 return "optsize";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000291 if (hasAttribute(Attribute::ReadNone))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000292 return "readnone";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000293 if (hasAttribute(Attribute::ReadOnly))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000294 return "readonly";
Nicolai Haehnle84c9f992016-07-04 08:01:29 +0000295 if (hasAttribute(Attribute::WriteOnly))
296 return "writeonly";
Stephen Linb8bd2322013-04-20 05:14:40 +0000297 if (hasAttribute(Attribute::Returned))
298 return "returned";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000299 if (hasAttribute(Attribute::ReturnsTwice))
300 return "returns_twice";
301 if (hasAttribute(Attribute::SExt))
302 return "signext";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000303 if (hasAttribute(Attribute::StackProtect))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000304 return "ssp";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000305 if (hasAttribute(Attribute::StackProtectReq))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000306 return "sspreq";
Bill Wendlingd154e2832013-01-23 06:41:41 +0000307 if (hasAttribute(Attribute::StackProtectStrong))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000308 return "sspstrong";
Peter Collingbourne82437bf2015-06-15 21:07:11 +0000309 if (hasAttribute(Attribute::SafeStack))
310 return "safestack";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000311 if (hasAttribute(Attribute::StructRet))
312 return "sret";
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000313 if (hasAttribute(Attribute::SanitizeThread))
314 return "sanitize_thread";
315 if (hasAttribute(Attribute::SanitizeMemory))
316 return "sanitize_memory";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000317 if (hasAttribute(Attribute::UWTable))
318 return "uwtable";
319 if (hasAttribute(Attribute::ZExt))
320 return "zeroext";
Diego Novilloc6399532013-05-24 12:26:52 +0000321 if (hasAttribute(Attribute::Cold))
322 return "cold";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000323
324 // FIXME: These should be output like this:
325 //
326 // align=4
327 // alignstack=8
328 //
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000329 if (hasAttribute(Attribute::Alignment)) {
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000330 std::string Result;
Bill Wendling829b4782013-02-11 08:43:33 +0000331 Result += "align";
332 Result += (InAttrGrp) ? "=" : " ";
Bill Wendling3f12ac22013-02-05 22:37:24 +0000333 Result += utostr(getValueAsInt());
334 return Result;
335 }
Bill Wendling829b4782013-02-11 08:43:33 +0000336
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000337 auto AttrWithBytesToString = [&](const char *Name) {
Bill Wendling3f12ac22013-02-05 22:37:24 +0000338 std::string Result;
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000339 Result += Name;
Bill Wendling829b4782013-02-11 08:43:33 +0000340 if (InAttrGrp) {
341 Result += "=";
342 Result += utostr(getValueAsInt());
343 } else {
344 Result += "(";
345 Result += utostr(getValueAsInt());
346 Result += ")";
347 }
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000348 return Result;
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000349 };
Bill Wendling9c2eba92013-01-31 20:59:05 +0000350
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000351 if (hasAttribute(Attribute::StackAlignment))
352 return AttrWithBytesToString("alignstack");
353
354 if (hasAttribute(Attribute::Dereferenceable))
355 return AttrWithBytesToString("dereferenceable");
356
357 if (hasAttribute(Attribute::DereferenceableOrNull))
358 return AttrWithBytesToString("dereferenceable_or_null");
Hal Finkelb0407ba2014-07-18 15:51:28 +0000359
George Burgess IV278199f2016-04-12 01:05:35 +0000360 if (hasAttribute(Attribute::AllocSize)) {
361 unsigned ElemSize;
362 Optional<unsigned> NumElems;
363 std::tie(ElemSize, NumElems) = getAllocSizeArgs();
364
365 std::string Result = "allocsize(";
366 Result += utostr(ElemSize);
367 if (NumElems.hasValue()) {
368 Result += ',';
369 Result += utostr(*NumElems);
370 }
371 Result += ')';
372 return Result;
373 }
374
Bill Wendling9c2eba92013-01-31 20:59:05 +0000375 // Convert target-dependent attributes to strings of the form:
376 //
377 // "kind"
378 // "kind" = "value"
Bill Wendling9c2eba92013-01-31 20:59:05 +0000379 //
Bill Wendling3f12ac22013-02-05 22:37:24 +0000380 if (isStringAttribute()) {
Bill Wendling9c2eba92013-01-31 20:59:05 +0000381 std::string Result;
Yaron Keren075759a2015-03-30 15:42:36 +0000382 Result += (Twine('"') + getKindAsString() + Twine('"')).str();
Bill Wendling9c2eba92013-01-31 20:59:05 +0000383
Bill Wendling3f12ac22013-02-05 22:37:24 +0000384 StringRef Val = pImpl->getValueAsString();
385 if (Val.empty()) return Result;
Bill Wendling7a33f772013-02-01 22:32:30 +0000386
Yaron Keren075759a2015-03-30 15:42:36 +0000387 Result += ("=\"" + Val + Twine('"')).str();
Bill Wendlingc79cdff2013-02-01 01:04:27 +0000388 return Result;
Bill Wendling9c2eba92013-01-31 20:59:05 +0000389 }
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000390
391 llvm_unreachable("Unknown attribute");
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000392}
393
Bill Wendlingd509a662013-01-29 00:34:06 +0000394bool Attribute::operator<(Attribute A) const {
395 if (!pImpl && !A.pImpl) return false;
396 if (!pImpl) return true;
397 if (!A.pImpl) return false;
398 return *pImpl < *A.pImpl;
399}
400
Bill Wendlingd509a662013-01-29 00:34:06 +0000401//===----------------------------------------------------------------------===//
402// AttributeImpl Definition
403//===----------------------------------------------------------------------===//
404
Eric Christopher0eaa5412014-07-02 22:05:40 +0000405// Pin the vtables to this file.
Alexey Samsonov49109a22013-11-18 09:31:53 +0000406AttributeImpl::~AttributeImpl() {}
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +0000407void EnumAttributeImpl::anchor() {}
Hal Finkele15442c2014-07-18 06:51:55 +0000408void IntAttributeImpl::anchor() {}
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +0000409void StringAttributeImpl::anchor() {}
Alexey Samsonov49109a22013-11-18 09:31:53 +0000410
Bill Wendlingd509a662013-01-29 00:34:06 +0000411bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
Bill Wendling3f12ac22013-02-05 22:37:24 +0000412 if (isStringAttribute()) return false;
413 return getKindAsEnum() == A;
Bill Wendlingd509a662013-01-29 00:34:06 +0000414}
415
Bill Wendling3f12ac22013-02-05 22:37:24 +0000416bool AttributeImpl::hasAttribute(StringRef Kind) const {
417 if (!isStringAttribute()) return false;
418 return getKindAsString() == Kind;
Bill Wendlingd509a662013-01-29 00:34:06 +0000419}
420
Bill Wendling3f12ac22013-02-05 22:37:24 +0000421Attribute::AttrKind AttributeImpl::getKindAsEnum() const {
Hal Finkele15442c2014-07-18 06:51:55 +0000422 assert(isEnumAttribute() || isIntAttribute());
Benjamin Kramer741146b2013-07-11 12:13:16 +0000423 return static_cast<const EnumAttributeImpl *>(this)->getEnumKind();
Bill Wendlingd509a662013-01-29 00:34:06 +0000424}
425
Bill Wendling3f12ac22013-02-05 22:37:24 +0000426uint64_t AttributeImpl::getValueAsInt() const {
Hal Finkele15442c2014-07-18 06:51:55 +0000427 assert(isIntAttribute());
428 return static_cast<const IntAttributeImpl *>(this)->getValue();
Bill Wendlingd509a662013-01-29 00:34:06 +0000429}
430
Bill Wendling3f12ac22013-02-05 22:37:24 +0000431StringRef AttributeImpl::getKindAsString() const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000432 assert(isStringAttribute());
433 return static_cast<const StringAttributeImpl *>(this)->getStringKind();
Bill Wendlingd509a662013-01-29 00:34:06 +0000434}
435
Bill Wendling3f12ac22013-02-05 22:37:24 +0000436StringRef AttributeImpl::getValueAsString() const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000437 assert(isStringAttribute());
438 return static_cast<const StringAttributeImpl *>(this)->getStringValue();
Bill Wendlingd509a662013-01-29 00:34:06 +0000439}
440
441bool AttributeImpl::operator<(const AttributeImpl &AI) const {
Bill Wendlingc79cdff2013-02-01 01:04:27 +0000442 // This sorts the attributes with Attribute::AttrKinds coming first (sorted
443 // relative to their enum value) and then strings.
Bill Wendling26b95752013-02-15 05:25:26 +0000444 if (isEnumAttribute()) {
445 if (AI.isEnumAttribute()) return getKindAsEnum() < AI.getKindAsEnum();
Hal Finkele15442c2014-07-18 06:51:55 +0000446 if (AI.isIntAttribute()) return true;
Bill Wendling26b95752013-02-15 05:25:26 +0000447 if (AI.isStringAttribute()) return true;
448 }
Bill Wendlingc79cdff2013-02-01 01:04:27 +0000449
Hal Finkele15442c2014-07-18 06:51:55 +0000450 if (isIntAttribute()) {
Bill Wendling26b95752013-02-15 05:25:26 +0000451 if (AI.isEnumAttribute()) return false;
Reid Kleckner7de67612016-04-04 23:06:05 +0000452 if (AI.isIntAttribute()) {
453 if (getKindAsEnum() == AI.getKindAsEnum())
454 return getValueAsInt() < AI.getValueAsInt();
455 return getKindAsEnum() < AI.getKindAsEnum();
456 }
Bill Wendling26b95752013-02-15 05:25:26 +0000457 if (AI.isStringAttribute()) return true;
Bill Wendling3f12ac22013-02-05 22:37:24 +0000458 }
Bill Wendlingd509a662013-01-29 00:34:06 +0000459
Bill Wendling26b95752013-02-15 05:25:26 +0000460 if (AI.isEnumAttribute()) return false;
Hal Finkele15442c2014-07-18 06:51:55 +0000461 if (AI.isIntAttribute()) return false;
Bill Wendling26b95752013-02-15 05:25:26 +0000462 if (getKindAsString() == AI.getKindAsString())
463 return getValueAsString() < AI.getValueAsString();
464 return getKindAsString() < AI.getKindAsString();
Bill Wendlingd509a662013-01-29 00:34:06 +0000465}
466
Bill Wendlingd509a662013-01-29 00:34:06 +0000467uint64_t AttributeImpl::getAttrMask(Attribute::AttrKind Val) {
468 // FIXME: Remove this.
469 switch (Val) {
470 case Attribute::EndAttrKinds:
Bill Wendlingd509a662013-01-29 00:34:06 +0000471 llvm_unreachable("Synthetic enumerators which should never get here");
472
473 case Attribute::None: return 0;
474 case Attribute::ZExt: return 1 << 0;
475 case Attribute::SExt: return 1 << 1;
476 case Attribute::NoReturn: return 1 << 2;
477 case Attribute::InReg: return 1 << 3;
478 case Attribute::StructRet: return 1 << 4;
479 case Attribute::NoUnwind: return 1 << 5;
480 case Attribute::NoAlias: return 1 << 6;
481 case Attribute::ByVal: return 1 << 7;
482 case Attribute::Nest: return 1 << 8;
483 case Attribute::ReadNone: return 1 << 9;
484 case Attribute::ReadOnly: return 1 << 10;
485 case Attribute::NoInline: return 1 << 11;
486 case Attribute::AlwaysInline: return 1 << 12;
487 case Attribute::OptimizeForSize: return 1 << 13;
488 case Attribute::StackProtect: return 1 << 14;
489 case Attribute::StackProtectReq: return 1 << 15;
490 case Attribute::Alignment: return 31 << 16;
491 case Attribute::NoCapture: return 1 << 21;
492 case Attribute::NoRedZone: return 1 << 22;
493 case Attribute::NoImplicitFloat: return 1 << 23;
494 case Attribute::Naked: return 1 << 24;
495 case Attribute::InlineHint: return 1 << 25;
496 case Attribute::StackAlignment: return 7 << 26;
497 case Attribute::ReturnsTwice: return 1 << 29;
498 case Attribute::UWTable: return 1 << 30;
499 case Attribute::NonLazyBind: return 1U << 31;
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000500 case Attribute::SanitizeAddress: return 1ULL << 32;
Bill Wendlingd509a662013-01-29 00:34:06 +0000501 case Attribute::MinSize: return 1ULL << 33;
502 case Attribute::NoDuplicate: return 1ULL << 34;
503 case Attribute::StackProtectStrong: return 1ULL << 35;
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000504 case Attribute::SanitizeThread: return 1ULL << 36;
505 case Attribute::SanitizeMemory: return 1ULL << 37;
Bill Wendling7bfdfe72013-02-22 00:40:12 +0000506 case Attribute::NoBuiltin: return 1ULL << 38;
Stephen Linb8bd2322013-04-20 05:14:40 +0000507 case Attribute::Returned: return 1ULL << 39;
Diego Novilloc6399532013-05-24 12:26:52 +0000508 case Attribute::Cold: return 1ULL << 40;
Michael Gottesman41748d72013-06-27 00:25:01 +0000509 case Attribute::Builtin: return 1ULL << 41;
Andrea Di Biagio377496b2013-08-23 11:53:55 +0000510 case Attribute::OptimizeNone: return 1ULL << 42;
Reid Klecknera534a382013-12-19 02:14:12 +0000511 case Attribute::InAlloca: return 1ULL << 43;
Nick Lewyckyd52b1522014-05-20 01:23:40 +0000512 case Attribute::NonNull: return 1ULL << 44;
Tom Roeder44cb65f2014-06-05 19:29:43 +0000513 case Attribute::JumpTable: return 1ULL << 45;
Owen Anderson85fa7d52015-05-26 23:48:40 +0000514 case Attribute::Convergent: return 1ULL << 46;
Peter Collingbourne82437bf2015-06-15 21:07:11 +0000515 case Attribute::SafeStack: return 1ULL << 47;
James Molloye6f87ca2015-11-06 10:32:53 +0000516 case Attribute::NoRecurse: return 1ULL << 48;
Vaivaswatha Nagarajfb3f4902015-12-16 16:16:19 +0000517 case Attribute::InaccessibleMemOnly: return 1ULL << 49;
518 case Attribute::InaccessibleMemOrArgMemOnly: return 1ULL << 50;
Manman Renf46262e2016-03-29 17:37:21 +0000519 case Attribute::SwiftSelf: return 1ULL << 51;
Manman Ren9bfd0d02016-04-01 21:41:15 +0000520 case Attribute::SwiftError: return 1ULL << 52;
Nicolai Haehnle84c9f992016-07-04 08:01:29 +0000521 case Attribute::WriteOnly: return 1ULL << 53;
Hal Finkelb0407ba2014-07-18 15:51:28 +0000522 case Attribute::Dereferenceable:
523 llvm_unreachable("dereferenceable attribute not supported in raw format");
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000524 break;
525 case Attribute::DereferenceableOrNull:
526 llvm_unreachable("dereferenceable_or_null attribute not supported in raw "
527 "format");
528 break;
Igor Laevsky39d662f2015-07-11 10:30:36 +0000529 case Attribute::ArgMemOnly:
530 llvm_unreachable("argmemonly attribute not supported in raw format");
531 break;
George Burgess IV278199f2016-04-12 01:05:35 +0000532 case Attribute::AllocSize:
533 llvm_unreachable("allocsize not supported in raw format");
534 break;
Bill Wendling25342e12013-02-22 00:50:09 +0000535 }
536 llvm_unreachable("Unsupported attribute type");
Bill Wendlingd509a662013-01-29 00:34:06 +0000537}
538
539//===----------------------------------------------------------------------===//
540// AttributeSetNode Definition
541//===----------------------------------------------------------------------===//
542
543AttributeSetNode *AttributeSetNode::get(LLVMContext &C,
544 ArrayRef<Attribute> Attrs) {
545 if (Attrs.empty())
Craig Topperc6207612014-04-09 06:08:46 +0000546 return nullptr;
Bill Wendlingd509a662013-01-29 00:34:06 +0000547
548 // Otherwise, build a key to look up the existing attributes.
549 LLVMContextImpl *pImpl = C.pImpl;
550 FoldingSetNodeID ID;
551
552 SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end());
Reid Kleckner7de67612016-04-04 23:06:05 +0000553 std::sort(SortedAttrs.begin(), SortedAttrs.end());
Bill Wendlingd509a662013-01-29 00:34:06 +0000554
George Burgess IV500d3032015-12-16 05:21:02 +0000555 for (Attribute Attr : SortedAttrs)
556 Attr.Profile(ID);
Bill Wendlingd509a662013-01-29 00:34:06 +0000557
558 void *InsertPoint;
559 AttributeSetNode *PA =
560 pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint);
561
562 // If we didn't find any existing attributes of the same shape then create a
563 // new one and insert it.
564 if (!PA) {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000565 // Coallocate entries after the AttributeSetNode itself.
James Y Knightaa365b22015-08-05 22:57:34 +0000566 void *Mem = ::operator new(totalSizeToAlloc<Attribute>(SortedAttrs.size()));
Benjamin Kramer741146b2013-07-11 12:13:16 +0000567 PA = new (Mem) AttributeSetNode(SortedAttrs);
Bill Wendlingd509a662013-01-29 00:34:06 +0000568 pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint);
569 }
570
571 // Return the AttributesListNode that we found or created.
572 return PA;
573}
574
Bill Wendlingbce7b972013-02-13 08:42:21 +0000575bool AttributeSetNode::hasAttribute(StringRef Kind) const {
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000576 for (Attribute I : *this)
577 if (I.hasAttribute(Kind))
Bill Wendlingbce7b972013-02-13 08:42:21 +0000578 return true;
579 return false;
580}
581
582Attribute AttributeSetNode::getAttribute(Attribute::AttrKind Kind) const {
Matthias Braun31eeb76f2016-01-29 22:25:13 +0000583 if (hasAttribute(Kind)) {
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000584 for (Attribute I : *this)
585 if (I.hasAttribute(Kind))
586 return I;
Matthias Braun31eeb76f2016-01-29 22:25:13 +0000587 }
Bill Wendlingbce7b972013-02-13 08:42:21 +0000588 return Attribute();
589}
590
591Attribute AttributeSetNode::getAttribute(StringRef Kind) const {
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000592 for (Attribute I : *this)
593 if (I.hasAttribute(Kind))
594 return I;
Bill Wendlingbce7b972013-02-13 08:42:21 +0000595 return Attribute();
596}
597
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000598unsigned AttributeSetNode::getAlignment() const {
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000599 for (Attribute I : *this)
600 if (I.hasAttribute(Attribute::Alignment))
601 return I.getAlignment();
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000602 return 0;
603}
604
605unsigned AttributeSetNode::getStackAlignment() const {
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000606 for (Attribute I : *this)
607 if (I.hasAttribute(Attribute::StackAlignment))
608 return I.getStackAlignment();
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000609 return 0;
610}
611
Hal Finkelb0407ba2014-07-18 15:51:28 +0000612uint64_t AttributeSetNode::getDereferenceableBytes() const {
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000613 for (Attribute I : *this)
614 if (I.hasAttribute(Attribute::Dereferenceable))
615 return I.getDereferenceableBytes();
Hal Finkelb0407ba2014-07-18 15:51:28 +0000616 return 0;
617}
618
Sanjoy Das06cf33f2015-05-06 17:41:54 +0000619uint64_t AttributeSetNode::getDereferenceableOrNullBytes() const {
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000620 for (Attribute I : *this)
621 if (I.hasAttribute(Attribute::DereferenceableOrNull))
622 return I.getDereferenceableOrNullBytes();
Sanjoy Das06cf33f2015-05-06 17:41:54 +0000623 return 0;
624}
625
George Burgess IV278199f2016-04-12 01:05:35 +0000626std::pair<unsigned, Optional<unsigned>>
627AttributeSetNode::getAllocSizeArgs() const {
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000628 for (Attribute I : *this)
629 if (I.hasAttribute(Attribute::AllocSize))
630 return I.getAllocSizeArgs();
George Burgess IV278199f2016-04-12 01:05:35 +0000631 return std::make_pair(0, 0);
632}
633
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +0000634std::string AttributeSetNode::getAsString(bool InAttrGrp) const {
Benjamin Kramer0baf8f42013-04-19 11:43:21 +0000635 std::string Str;
Benjamin Kramer741146b2013-07-11 12:13:16 +0000636 for (iterator I = begin(), E = end(); I != E; ++I) {
637 if (I != begin())
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +0000638 Str += ' ';
639 Str += I->getAsString(InAttrGrp);
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000640 }
641 return Str;
642}
643
Bill Wendlingd509a662013-01-29 00:34:06 +0000644//===----------------------------------------------------------------------===//
645// AttributeSetImpl Definition
646//===----------------------------------------------------------------------===//
647
Rafael Espindoladd275302013-04-30 16:53:38 +0000648uint64_t AttributeSetImpl::Raw(unsigned Index) const {
Amaury Sechet24c84fd2016-06-14 22:04:16 +0000649 for (unsigned I = 0, E = getNumSlots(); I != E; ++I) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000650 if (getSlotIndex(I) != Index) continue;
Benjamin Kramer741146b2013-07-11 12:13:16 +0000651 const AttributeSetNode *ASN = getSlotNode(I);
Bill Wendling91226182013-02-02 00:52:44 +0000652 uint64_t Mask = 0;
Bill Wendlingd509a662013-01-29 00:34:06 +0000653
Benjamin Kramer741146b2013-07-11 12:13:16 +0000654 for (AttributeSetNode::iterator II = ASN->begin(),
Bill Wendling91226182013-02-02 00:52:44 +0000655 IE = ASN->end(); II != IE; ++II) {
656 Attribute Attr = *II;
Bill Wendlingd746e402013-02-10 23:18:05 +0000657
658 // This cannot handle string attributes.
659 if (Attr.isStringAttribute()) continue;
660
Bill Wendling3f12ac22013-02-05 22:37:24 +0000661 Attribute::AttrKind Kind = Attr.getKindAsEnum();
Bill Wendling91226182013-02-02 00:52:44 +0000662
Bill Wendling3f12ac22013-02-05 22:37:24 +0000663 if (Kind == Attribute::Alignment)
Bill Wendling91226182013-02-02 00:52:44 +0000664 Mask |= (Log2_32(ASN->getAlignment()) + 1) << 16;
Bill Wendling3f12ac22013-02-05 22:37:24 +0000665 else if (Kind == Attribute::StackAlignment)
Bill Wendling91226182013-02-02 00:52:44 +0000666 Mask |= (Log2_32(ASN->getStackAlignment()) + 1) << 26;
Hal Finkelb0407ba2014-07-18 15:51:28 +0000667 else if (Kind == Attribute::Dereferenceable)
668 llvm_unreachable("dereferenceable not supported in bit mask");
George Burgess IV278199f2016-04-12 01:05:35 +0000669 else if (Kind == Attribute::AllocSize)
670 llvm_unreachable("allocsize not supported in bit mask");
Bill Wendling91226182013-02-02 00:52:44 +0000671 else
Bill Wendling3f12ac22013-02-05 22:37:24 +0000672 Mask |= AttributeImpl::getAttrMask(Kind);
Bill Wendling91226182013-02-02 00:52:44 +0000673 }
674
675 return Mask;
Bill Wendlingd509a662013-01-29 00:34:06 +0000676 }
677
678 return 0;
679}
680
Yaron Kereneb2a2542016-01-29 20:50:44 +0000681LLVM_DUMP_METHOD void AttributeSetImpl::dump() const {
Peter Collingbourneabca2ec2013-08-02 22:34:30 +0000682 AttributeSet(const_cast<AttributeSetImpl *>(this)).dump();
683}
684
Bill Wendlingd509a662013-01-29 00:34:06 +0000685//===----------------------------------------------------------------------===//
686// AttributeSet Construction and Mutation Methods
687//===----------------------------------------------------------------------===//
688
Bill Wendling60011b82013-01-29 01:43:29 +0000689AttributeSet
690AttributeSet::getImpl(LLVMContext &C,
691 ArrayRef<std::pair<unsigned, AttributeSetNode*> > Attrs) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000692 LLVMContextImpl *pImpl = C.pImpl;
693 FoldingSetNodeID ID;
694 AttributeSetImpl::Profile(ID, Attrs);
695
696 void *InsertPoint;
697 AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
698
699 // If we didn't find any existing attributes of the same shape then
700 // create a new one and insert it.
701 if (!PA) {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000702 // Coallocate entries after the AttributeSetImpl itself.
James Y Knightaa365b22015-08-05 22:57:34 +0000703 void *Mem = ::operator new(
704 AttributeSetImpl::totalSizeToAlloc<IndexAttrPair>(Attrs.size()));
Benjamin Kramer741146b2013-07-11 12:13:16 +0000705 PA = new (Mem) AttributeSetImpl(C, Attrs);
Bill Wendlingd509a662013-01-29 00:34:06 +0000706 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
707 }
708
709 // Return the AttributesList that we found or created.
710 return AttributeSet(PA);
711}
712
713AttributeSet AttributeSet::get(LLVMContext &C,
714 ArrayRef<std::pair<unsigned, Attribute> > Attrs){
715 // If there are no attributes then return a null AttributesList pointer.
716 if (Attrs.empty())
717 return AttributeSet();
718
Craig Toppere30b8ca2016-01-03 19:43:40 +0000719 assert(std::is_sorted(Attrs.begin(), Attrs.end(),
720 [](const std::pair<unsigned, Attribute> &LHS,
721 const std::pair<unsigned, Attribute> &RHS) {
722 return LHS.first < RHS.first;
723 }) && "Misordered Attributes list!");
David Majnemer0a16c222016-08-11 21:15:00 +0000724 assert(none_of(Attrs,
725 [](const std::pair<unsigned, Attribute> &Pair) {
726 return Pair.second.hasAttribute(Attribute::None);
727 }) &&
728 "Pointless attribute!");
Bill Wendlingd509a662013-01-29 00:34:06 +0000729
730 // Create a vector if (unsigned, AttributeSetNode*) pairs from the attributes
731 // list.
732 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrPairVec;
733 for (ArrayRef<std::pair<unsigned, Attribute> >::iterator I = Attrs.begin(),
734 E = Attrs.end(); I != E; ) {
735 unsigned Index = I->first;
736 SmallVector<Attribute, 4> AttrVec;
NAKAMURA Takumif05d2f22013-01-29 15:18:16 +0000737 while (I != E && I->first == Index) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000738 AttrVec.push_back(I->second);
739 ++I;
740 }
741
David Majnemer0a16c222016-08-11 21:15:00 +0000742 AttrPairVec.emplace_back(Index, AttributeSetNode::get(C, AttrVec));
Bill Wendlingd509a662013-01-29 00:34:06 +0000743 }
744
745 return getImpl(C, AttrPairVec);
746}
747
748AttributeSet AttributeSet::get(LLVMContext &C,
749 ArrayRef<std::pair<unsigned,
750 AttributeSetNode*> > Attrs) {
751 // If there are no attributes then return a null AttributesList pointer.
752 if (Attrs.empty())
753 return AttributeSet();
754
755 return getImpl(C, Attrs);
756}
757
David Majnemercf63a792014-05-03 23:00:35 +0000758AttributeSet AttributeSet::get(LLVMContext &C, unsigned Index,
759 const AttrBuilder &B) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000760 if (!B.hasAttributes())
761 return AttributeSet();
Bill Wendlingf7134812013-01-29 01:02:03 +0000762
Bill Wendlingae89a0f2013-02-05 23:48:36 +0000763 // Add target-independent attributes.
Bill Wendlingf7134812013-01-29 01:02:03 +0000764 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
Benjamin Kramer45e7d532013-02-16 19:13:18 +0000765 for (Attribute::AttrKind Kind = Attribute::None;
Benjamin Kramer6f37dac2013-02-16 19:22:28 +0000766 Kind != Attribute::EndAttrKinds; Kind = Attribute::AttrKind(Kind + 1)) {
Benjamin Kramer45e7d532013-02-16 19:13:18 +0000767 if (!B.contains(Kind))
768 continue;
769
George Burgess IV500d3032015-12-16 05:21:02 +0000770 Attribute Attr;
771 switch (Kind) {
772 case Attribute::Alignment:
773 Attr = Attribute::getWithAlignment(C, B.getAlignment());
774 break;
775 case Attribute::StackAlignment:
776 Attr = Attribute::getWithStackAlignment(C, B.getStackAlignment());
777 break;
778 case Attribute::Dereferenceable:
779 Attr = Attribute::getWithDereferenceableBytes(
780 C, B.getDereferenceableBytes());
781 break;
782 case Attribute::DereferenceableOrNull:
783 Attr = Attribute::getWithDereferenceableOrNullBytes(
784 C, B.getDereferenceableOrNullBytes());
785 break;
George Burgess IV278199f2016-04-12 01:05:35 +0000786 case Attribute::AllocSize: {
787 auto A = B.getAllocSizeArgs();
788 Attr = Attribute::getWithAllocSizeArgs(C, A.first, A.second);
789 break;
790 }
George Burgess IV500d3032015-12-16 05:21:02 +0000791 default:
792 Attr = Attribute::get(C, Kind);
793 }
David Majnemer0a16c222016-08-11 21:15:00 +0000794 Attrs.emplace_back(Index, Attr);
Bill Wendlingf7134812013-01-29 01:02:03 +0000795 }
796
Bill Wendlingae89a0f2013-02-05 23:48:36 +0000797 // Add target-dependent (string) attributes.
Benjamin Kramerea76b6f2016-06-12 19:02:34 +0000798 for (const auto &TDA : B.td_attrs())
David Majnemer0a16c222016-08-11 21:15:00 +0000799 Attrs.emplace_back(Index, Attribute::get(C, TDA.first, TDA.second));
Bill Wendlingae89a0f2013-02-05 23:48:36 +0000800
Bill Wendlingf7134812013-01-29 01:02:03 +0000801 return get(C, Attrs);
Bill Wendlingd509a662013-01-29 00:34:06 +0000802}
803
Bill Wendling211316c2013-04-18 20:17:28 +0000804AttributeSet AttributeSet::get(LLVMContext &C, unsigned Index,
Amaury Sechet392638d2016-06-14 20:27:35 +0000805 ArrayRef<Attribute::AttrKind> Kinds) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000806 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
Amaury Sechet392638d2016-06-14 20:27:35 +0000807 for (Attribute::AttrKind K : Kinds)
David Majnemer0a16c222016-08-11 21:15:00 +0000808 Attrs.emplace_back(Index, Attribute::get(C, K));
Bill Wendlingd509a662013-01-29 00:34:06 +0000809 return get(C, Attrs);
810}
811
Amaury Sechet6100adf2016-06-15 17:50:39 +0000812AttributeSet AttributeSet::get(LLVMContext &C, unsigned Index,
813 ArrayRef<StringRef> Kinds) {
814 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
815 for (StringRef K : Kinds)
David Majnemer0a16c222016-08-11 21:15:00 +0000816 Attrs.emplace_back(Index, Attribute::get(C, K));
Amaury Sechet6100adf2016-06-15 17:50:39 +0000817 return get(C, Attrs);
818}
819
Bill Wendlingd509a662013-01-29 00:34:06 +0000820AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<AttributeSet> Attrs) {
821 if (Attrs.empty()) return AttributeSet();
Peter Collingbournebd6c7452013-08-02 22:29:40 +0000822 if (Attrs.size() == 1) return Attrs[0];
Bill Wendlingd509a662013-01-29 00:34:06 +0000823
824 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrNodeVec;
Peter Collingbournebd6c7452013-08-02 22:29:40 +0000825 AttributeSetImpl *A0 = Attrs[0].pImpl;
826 if (A0)
Amaury Sechet24c84fd2016-06-14 22:04:16 +0000827 AttrNodeVec.append(A0->getNode(0), A0->getNode(A0->getNumSlots()));
Peter Collingbournebd6c7452013-08-02 22:29:40 +0000828 // Copy all attributes from Attrs into AttrNodeVec while keeping AttrNodeVec
829 // ordered by index. Because we know that each list in Attrs is ordered by
830 // index we only need to merge each successive list in rather than doing a
831 // full sort.
832 for (unsigned I = 1, E = Attrs.size(); I != E; ++I) {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000833 AttributeSetImpl *AS = Attrs[I].pImpl;
834 if (!AS) continue;
Peter Collingbournebd6c7452013-08-02 22:29:40 +0000835 SmallVector<std::pair<unsigned, AttributeSetNode *>, 8>::iterator
836 ANVI = AttrNodeVec.begin(), ANVE;
James Y Knightaa365b22015-08-05 22:57:34 +0000837 for (const IndexAttrPair *AI = AS->getNode(0),
Amaury Sechet24c84fd2016-06-14 22:04:16 +0000838 *AE = AS->getNode(AS->getNumSlots());
Peter Collingbournebd6c7452013-08-02 22:29:40 +0000839 AI != AE; ++AI) {
840 ANVE = AttrNodeVec.end();
841 while (ANVI != ANVE && ANVI->first <= AI->first)
842 ++ANVI;
843 ANVI = AttrNodeVec.insert(ANVI, *AI) + 1;
844 }
Bill Wendlingd509a662013-01-29 00:34:06 +0000845 }
846
847 return getImpl(C, AttrNodeVec);
848}
849
Bill Wendling211316c2013-04-18 20:17:28 +0000850AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
Amaury Sechet392638d2016-06-14 20:27:35 +0000851 Attribute::AttrKind Kind) const {
852 if (hasAttribute(Index, Kind)) return *this;
853 return addAttributes(C, Index, AttributeSet::get(C, Index, Kind));
Reed Kotler795c7b42013-03-13 20:20:08 +0000854}
855
Bill Wendling3b2f6102013-07-25 18:34:24 +0000856AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
857 StringRef Kind, StringRef Value) const {
858 llvm::AttrBuilder B;
859 B.addAttribute(Kind, Value);
860 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
861}
862
Akira Hatanaka237916b2015-12-02 06:58:49 +0000863AttributeSet AttributeSet::addAttribute(LLVMContext &C,
864 ArrayRef<unsigned> Indices,
865 Attribute A) const {
Amaury Sechet24c84fd2016-06-14 22:04:16 +0000866 unsigned I = 0, E = pImpl ? pImpl->getNumSlots() : 0;
Akira Hatanaka237916b2015-12-02 06:58:49 +0000867 auto IdxI = Indices.begin(), IdxE = Indices.end();
868 SmallVector<AttributeSet, 4> AttrSet;
869
870 while (I != E && IdxI != IdxE) {
871 if (getSlotIndex(I) < *IdxI)
872 AttrSet.emplace_back(getSlotAttributes(I++));
873 else if (getSlotIndex(I) > *IdxI)
874 AttrSet.emplace_back(AttributeSet::get(C, std::make_pair(*IdxI++, A)));
875 else {
876 AttrBuilder B(getSlotAttributes(I), *IdxI);
877 B.addAttribute(A);
878 AttrSet.emplace_back(AttributeSet::get(C, *IdxI, B));
879 ++I;
880 ++IdxI;
881 }
882 }
883
884 while (I != E)
885 AttrSet.emplace_back(getSlotAttributes(I++));
886
887 while (IdxI != IdxE)
888 AttrSet.emplace_back(AttributeSet::get(C, std::make_pair(*IdxI++, A)));
889
890 return get(C, AttrSet);
891}
892
Bill Wendling211316c2013-04-18 20:17:28 +0000893AttributeSet AttributeSet::addAttributes(LLVMContext &C, unsigned Index,
Bill Wendlingd509a662013-01-29 00:34:06 +0000894 AttributeSet Attrs) const {
895 if (!pImpl) return Attrs;
896 if (!Attrs.pImpl) return *this;
897
898#ifndef NDEBUG
899 // FIXME it is not obvious how this should work for alignment. For now, say
900 // we can't change a known alignment.
Bill Wendling211316c2013-04-18 20:17:28 +0000901 unsigned OldAlign = getParamAlignment(Index);
902 unsigned NewAlign = Attrs.getParamAlignment(Index);
Bill Wendlingd509a662013-01-29 00:34:06 +0000903 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
904 "Attempt to change alignment!");
905#endif
906
907 // Add the attribute slots before the one we're trying to add.
908 SmallVector<AttributeSet, 4> AttrSet;
Amaury Sechet24c84fd2016-06-14 22:04:16 +0000909 uint64_t NumAttrs = pImpl->getNumSlots();
Bill Wendlingd509a662013-01-29 00:34:06 +0000910 AttributeSet AS;
911 uint64_t LastIndex = 0;
912 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
Bill Wendling211316c2013-04-18 20:17:28 +0000913 if (getSlotIndex(I) >= Index) {
914 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
Bill Wendlingd509a662013-01-29 00:34:06 +0000915 break;
916 }
917 LastIndex = I + 1;
918 AttrSet.push_back(getSlotAttributes(I));
919 }
920
921 // Now add the attribute into the correct slot. There may already be an
922 // AttributeSet there.
Bill Wendling211316c2013-04-18 20:17:28 +0000923 AttrBuilder B(AS, Index);
Bill Wendlingd509a662013-01-29 00:34:06 +0000924
Amaury Sechet24c84fd2016-06-14 22:04:16 +0000925 for (unsigned I = 0, E = Attrs.pImpl->getNumSlots(); I != E; ++I)
Bill Wendling211316c2013-04-18 20:17:28 +0000926 if (Attrs.getSlotIndex(I) == Index) {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000927 for (AttributeSetImpl::iterator II = Attrs.pImpl->begin(I),
Bill Wendlingd509a662013-01-29 00:34:06 +0000928 IE = Attrs.pImpl->end(I); II != IE; ++II)
Bill Wendling23804da2013-01-31 23:38:01 +0000929 B.addAttribute(*II);
Bill Wendlingd509a662013-01-29 00:34:06 +0000930 break;
931 }
932
Bill Wendling211316c2013-04-18 20:17:28 +0000933 AttrSet.push_back(AttributeSet::get(C, Index, B));
Bill Wendlingd509a662013-01-29 00:34:06 +0000934
935 // Add the remaining attribute slots.
936 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
937 AttrSet.push_back(getSlotAttributes(I));
938
939 return get(C, AttrSet);
940}
941
Bill Wendling211316c2013-04-18 20:17:28 +0000942AttributeSet AttributeSet::removeAttribute(LLVMContext &C, unsigned Index,
Amaury Sechet392638d2016-06-14 20:27:35 +0000943 Attribute::AttrKind Kind) const {
944 if (!hasAttribute(Index, Kind)) return *this;
945 return removeAttributes(C, Index, AttributeSet::get(C, Index, Kind));
Bill Wendlingd509a662013-01-29 00:34:06 +0000946}
947
Amaury Sechet6100adf2016-06-15 17:50:39 +0000948AttributeSet AttributeSet::removeAttribute(LLVMContext &C, unsigned Index,
949 StringRef Kind) const {
950 if (!hasAttribute(Index, Kind)) return *this;
951 return removeAttributes(C, Index, AttributeSet::get(C, Index, Kind));
952}
953
Bill Wendling211316c2013-04-18 20:17:28 +0000954AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Index,
Bill Wendlingd509a662013-01-29 00:34:06 +0000955 AttributeSet Attrs) const {
956 if (!pImpl) return AttributeSet();
957 if (!Attrs.pImpl) return *this;
958
Pete Cooper67cf9a72015-11-19 05:56:52 +0000959 // FIXME it is not obvious how this should work for alignment.
960 // For now, say we can't pass in alignment, which no current use does.
961 assert(!Attrs.hasAttribute(Index, Attribute::Alignment) &&
962 "Attempt to change alignment!");
963
Bill Wendlingd509a662013-01-29 00:34:06 +0000964 // Add the attribute slots before the one we're trying to add.
965 SmallVector<AttributeSet, 4> AttrSet;
Amaury Sechet24c84fd2016-06-14 22:04:16 +0000966 uint64_t NumAttrs = pImpl->getNumSlots();
Bill Wendlingd509a662013-01-29 00:34:06 +0000967 AttributeSet AS;
968 uint64_t LastIndex = 0;
969 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
Bill Wendling211316c2013-04-18 20:17:28 +0000970 if (getSlotIndex(I) >= Index) {
971 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
Bill Wendlingd509a662013-01-29 00:34:06 +0000972 break;
973 }
974 LastIndex = I + 1;
975 AttrSet.push_back(getSlotAttributes(I));
976 }
977
Bill Wendlingd2196752013-01-30 23:07:40 +0000978 // Now remove the attribute from the correct slot. There may already be an
Bill Wendlingd509a662013-01-29 00:34:06 +0000979 // AttributeSet there.
Bill Wendling211316c2013-04-18 20:17:28 +0000980 AttrBuilder B(AS, Index);
Bill Wendlingd509a662013-01-29 00:34:06 +0000981
Amaury Sechet24c84fd2016-06-14 22:04:16 +0000982 for (unsigned I = 0, E = Attrs.pImpl->getNumSlots(); I != E; ++I)
Bill Wendling211316c2013-04-18 20:17:28 +0000983 if (Attrs.getSlotIndex(I) == Index) {
984 B.removeAttributes(Attrs.pImpl->getSlotAttributes(I), Index);
Bill Wendlingd509a662013-01-29 00:34:06 +0000985 break;
986 }
987
Bill Wendling211316c2013-04-18 20:17:28 +0000988 AttrSet.push_back(AttributeSet::get(C, Index, B));
Bill Wendlingd509a662013-01-29 00:34:06 +0000989
990 // Add the remaining attribute slots.
991 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
992 AttrSet.push_back(getSlotAttributes(I));
993
994 return get(C, AttrSet);
995}
996
Pete Cooperd2a44612015-05-06 23:19:43 +0000997AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Index,
998 const AttrBuilder &Attrs) const {
999 if (!pImpl) return AttributeSet();
1000
1001 // FIXME it is not obvious how this should work for alignment.
1002 // For now, say we can't pass in alignment, which no current use does.
1003 assert(!Attrs.hasAlignmentAttr() && "Attempt to change alignment!");
1004
1005 // Add the attribute slots before the one we're trying to add.
1006 SmallVector<AttributeSet, 4> AttrSet;
Amaury Sechet24c84fd2016-06-14 22:04:16 +00001007 uint64_t NumAttrs = pImpl->getNumSlots();
Pete Cooperd2a44612015-05-06 23:19:43 +00001008 AttributeSet AS;
1009 uint64_t LastIndex = 0;
1010 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
1011 if (getSlotIndex(I) >= Index) {
1012 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
1013 break;
1014 }
1015 LastIndex = I + 1;
1016 AttrSet.push_back(getSlotAttributes(I));
1017 }
1018
1019 // Now remove the attribute from the correct slot. There may already be an
1020 // AttributeSet there.
1021 AttrBuilder B(AS, Index);
1022 B.remove(Attrs);
1023
1024 AttrSet.push_back(AttributeSet::get(C, Index, B));
1025
1026 // Add the remaining attribute slots.
1027 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
1028 AttrSet.push_back(getSlotAttributes(I));
1029
1030 return get(C, AttrSet);
1031}
1032
Ramkumar Ramachandra8fcb4982015-02-14 19:37:54 +00001033AttributeSet AttributeSet::addDereferenceableAttr(LLVMContext &C, unsigned Index,
1034 uint64_t Bytes) const {
1035 llvm::AttrBuilder B;
1036 B.addDereferenceableAttr(Bytes);
1037 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
1038}
1039
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001040AttributeSet AttributeSet::addDereferenceableOrNullAttr(LLVMContext &C,
1041 unsigned Index,
1042 uint64_t Bytes) const {
1043 llvm::AttrBuilder B;
1044 B.addDereferenceableOrNullAttr(Bytes);
1045 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
1046}
1047
George Burgess IV278199f2016-04-12 01:05:35 +00001048AttributeSet
1049AttributeSet::addAllocSizeAttr(LLVMContext &C, unsigned Index,
1050 unsigned ElemSizeArg,
1051 const Optional<unsigned> &NumElemsArg) {
1052 llvm::AttrBuilder B;
1053 B.addAllocSizeAttr(ElemSizeArg, NumElemsArg);
1054 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
1055}
1056
Bill Wendlingd509a662013-01-29 00:34:06 +00001057//===----------------------------------------------------------------------===//
1058// AttributeSet Accessor Methods
1059//===----------------------------------------------------------------------===//
1060
Bill Wendling5d020a32013-02-10 05:00:40 +00001061LLVMContext &AttributeSet::getContext() const {
1062 return pImpl->getContext();
1063}
1064
Bill Wendling211316c2013-04-18 20:17:28 +00001065AttributeSet AttributeSet::getParamAttributes(unsigned Index) const {
1066 return pImpl && hasAttributes(Index) ?
Bill Wendlingd509a662013-01-29 00:34:06 +00001067 AttributeSet::get(pImpl->getContext(),
Bill Wendlingf2955aa2013-01-29 03:20:31 +00001068 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendling211316c2013-04-18 20:17:28 +00001069 std::make_pair(Index, getAttributes(Index)))) :
Bill Wendlingd509a662013-01-29 00:34:06 +00001070 AttributeSet();
1071}
1072
1073AttributeSet AttributeSet::getRetAttributes() const {
1074 return pImpl && hasAttributes(ReturnIndex) ?
1075 AttributeSet::get(pImpl->getContext(),
Bill Wendlingf2955aa2013-01-29 03:20:31 +00001076 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendlingd509a662013-01-29 00:34:06 +00001077 std::make_pair(ReturnIndex,
1078 getAttributes(ReturnIndex)))) :
1079 AttributeSet();
1080}
1081
1082AttributeSet AttributeSet::getFnAttributes() const {
1083 return pImpl && hasAttributes(FunctionIndex) ?
1084 AttributeSet::get(pImpl->getContext(),
Bill Wendlingf2955aa2013-01-29 03:20:31 +00001085 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendlingd509a662013-01-29 00:34:06 +00001086 std::make_pair(FunctionIndex,
1087 getAttributes(FunctionIndex)))) :
1088 AttributeSet();
1089}
1090
1091bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{
Bill Wendlingf2955aa2013-01-29 03:20:31 +00001092 AttributeSetNode *ASN = getAttributes(Index);
George Burgess IV500d3032015-12-16 05:21:02 +00001093 return ASN && ASN->hasAttribute(Kind);
Bill Wendlingd509a662013-01-29 00:34:06 +00001094}
1095
Bill Wendlingbce7b972013-02-13 08:42:21 +00001096bool AttributeSet::hasAttribute(unsigned Index, StringRef Kind) const {
1097 AttributeSetNode *ASN = getAttributes(Index);
George Burgess IV500d3032015-12-16 05:21:02 +00001098 return ASN && ASN->hasAttribute(Kind);
Bill Wendlingbce7b972013-02-13 08:42:21 +00001099}
1100
Bill Wendlingd509a662013-01-29 00:34:06 +00001101bool AttributeSet::hasAttributes(unsigned Index) const {
Bill Wendlingf2955aa2013-01-29 03:20:31 +00001102 AttributeSetNode *ASN = getAttributes(Index);
George Burgess IV500d3032015-12-16 05:21:02 +00001103 return ASN && ASN->hasAttributes();
Bill Wendlingd509a662013-01-29 00:34:06 +00001104}
1105
Matthias Braun33282812016-01-29 22:25:19 +00001106bool AttributeSet::hasFnAttribute(Attribute::AttrKind Kind) const {
1107 return pImpl && pImpl->hasFnAttribute(Kind);
1108}
1109
Hal Finkele87ad542016-07-10 23:01:32 +00001110bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr,
1111 unsigned *Index) const {
Craig Topperc6207612014-04-09 06:08:46 +00001112 if (!pImpl) return false;
Bill Wendlingd509a662013-01-29 00:34:06 +00001113
Amaury Sechet24c84fd2016-06-14 22:04:16 +00001114 for (unsigned I = 0, E = pImpl->getNumSlots(); I != E; ++I)
Benjamin Kramer741146b2013-07-11 12:13:16 +00001115 for (AttributeSetImpl::iterator II = pImpl->begin(I),
Bill Wendlingd509a662013-01-29 00:34:06 +00001116 IE = pImpl->end(I); II != IE; ++II)
Hal Finkele87ad542016-07-10 23:01:32 +00001117 if (II->hasAttribute(Attr)) {
1118 if (Index) *Index = pImpl->getSlotIndex(I);
Bill Wendlingd509a662013-01-29 00:34:06 +00001119 return true;
Hal Finkele87ad542016-07-10 23:01:32 +00001120 }
Bill Wendlingd509a662013-01-29 00:34:06 +00001121
1122 return false;
1123}
1124
Bill Wendlingbce7b972013-02-13 08:42:21 +00001125Attribute AttributeSet::getAttribute(unsigned Index,
1126 Attribute::AttrKind Kind) const {
1127 AttributeSetNode *ASN = getAttributes(Index);
1128 return ASN ? ASN->getAttribute(Kind) : Attribute();
1129}
1130
1131Attribute AttributeSet::getAttribute(unsigned Index,
1132 StringRef Kind) const {
1133 AttributeSetNode *ASN = getAttributes(Index);
1134 return ASN ? ASN->getAttribute(Kind) : Attribute();
1135}
1136
Bill Wendlingf2955aa2013-01-29 03:20:31 +00001137unsigned AttributeSet::getParamAlignment(unsigned Index) const {
1138 AttributeSetNode *ASN = getAttributes(Index);
1139 return ASN ? ASN->getAlignment() : 0;
Bill Wendlingd509a662013-01-29 00:34:06 +00001140}
1141
1142unsigned AttributeSet::getStackAlignment(unsigned Index) const {
Bill Wendlingf2955aa2013-01-29 03:20:31 +00001143 AttributeSetNode *ASN = getAttributes(Index);
1144 return ASN ? ASN->getStackAlignment() : 0;
Bill Wendlingd509a662013-01-29 00:34:06 +00001145}
1146
Hal Finkelb0407ba2014-07-18 15:51:28 +00001147uint64_t AttributeSet::getDereferenceableBytes(unsigned Index) const {
1148 AttributeSetNode *ASN = getAttributes(Index);
1149 return ASN ? ASN->getDereferenceableBytes() : 0;
1150}
1151
Sanjoy Das06cf33f2015-05-06 17:41:54 +00001152uint64_t AttributeSet::getDereferenceableOrNullBytes(unsigned Index) const {
1153 AttributeSetNode *ASN = getAttributes(Index);
1154 return ASN ? ASN->getDereferenceableOrNullBytes() : 0;
1155}
1156
George Burgess IV278199f2016-04-12 01:05:35 +00001157std::pair<unsigned, Optional<unsigned>>
1158AttributeSet::getAllocSizeArgs(unsigned Index) const {
1159 AttributeSetNode *ASN = getAttributes(Index);
Matt Arsenault4ced16d2016-07-18 22:12:46 +00001160 return ASN ? ASN->getAllocSizeArgs() : std::make_pair(0u, Optional<unsigned>(0u));
George Burgess IV278199f2016-04-12 01:05:35 +00001161}
1162
1163std::string AttributeSet::getAsString(unsigned Index, bool InAttrGrp) const {
Bill Wendlingf2955aa2013-01-29 03:20:31 +00001164 AttributeSetNode *ASN = getAttributes(Index);
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +00001165 return ASN ? ASN->getAsString(InAttrGrp) : std::string("");
Bill Wendlingd509a662013-01-29 00:34:06 +00001166}
1167
Bill Wendling211316c2013-04-18 20:17:28 +00001168AttributeSetNode *AttributeSet::getAttributes(unsigned Index) const {
Craig Topperc6207612014-04-09 06:08:46 +00001169 if (!pImpl) return nullptr;
Bill Wendlingd509a662013-01-29 00:34:06 +00001170
Bill Wendlingf2955aa2013-01-29 03:20:31 +00001171 // Loop through to find the attribute node we want.
Amaury Sechet24c84fd2016-06-14 22:04:16 +00001172 for (unsigned I = 0, E = pImpl->getNumSlots(); I != E; ++I)
Bill Wendling211316c2013-04-18 20:17:28 +00001173 if (pImpl->getSlotIndex(I) == Index)
Bill Wendlingf2955aa2013-01-29 03:20:31 +00001174 return pImpl->getSlotNode(I);
Bill Wendlingd509a662013-01-29 00:34:06 +00001175
Craig Topperc6207612014-04-09 06:08:46 +00001176 return nullptr;
Bill Wendlingd509a662013-01-29 00:34:06 +00001177}
1178
Bill Wendling211316c2013-04-18 20:17:28 +00001179AttributeSet::iterator AttributeSet::begin(unsigned Slot) const {
Bill Wendlinga9174862013-01-31 23:53:05 +00001180 if (!pImpl)
1181 return ArrayRef<Attribute>().begin();
Bill Wendling211316c2013-04-18 20:17:28 +00001182 return pImpl->begin(Slot);
Bill Wendlinga9174862013-01-31 23:53:05 +00001183}
1184
Bill Wendling211316c2013-04-18 20:17:28 +00001185AttributeSet::iterator AttributeSet::end(unsigned Slot) const {
Bill Wendlinga9174862013-01-31 23:53:05 +00001186 if (!pImpl)
1187 return ArrayRef<Attribute>().end();
Bill Wendling211316c2013-04-18 20:17:28 +00001188 return pImpl->end(Slot);
Bill Wendlinga9174862013-01-31 23:53:05 +00001189}
1190
Bill Wendlingd509a662013-01-29 00:34:06 +00001191//===----------------------------------------------------------------------===//
1192// AttributeSet Introspection Methods
1193//===----------------------------------------------------------------------===//
1194
Bill Wendlingd509a662013-01-29 00:34:06 +00001195unsigned AttributeSet::getNumSlots() const {
Amaury Sechet24c84fd2016-06-14 22:04:16 +00001196 return pImpl ? pImpl->getNumSlots() : 0;
Bill Wendlingd509a662013-01-29 00:34:06 +00001197}
1198
Rafael Espindoladd275302013-04-30 16:53:38 +00001199unsigned AttributeSet::getSlotIndex(unsigned Slot) const {
Amaury Sechet24c84fd2016-06-14 22:04:16 +00001200 assert(pImpl && Slot < pImpl->getNumSlots() &&
Bill Wendlingd509a662013-01-29 00:34:06 +00001201 "Slot # out of range!");
1202 return pImpl->getSlotIndex(Slot);
1203}
1204
1205AttributeSet AttributeSet::getSlotAttributes(unsigned Slot) const {
Amaury Sechet24c84fd2016-06-14 22:04:16 +00001206 assert(pImpl && Slot < pImpl->getNumSlots() &&
Bill Wendlingd509a662013-01-29 00:34:06 +00001207 "Slot # out of range!");
1208 return pImpl->getSlotAttributes(Slot);
1209}
1210
1211uint64_t AttributeSet::Raw(unsigned Index) const {
1212 // FIXME: Remove this.
1213 return pImpl ? pImpl->Raw(Index) : 0;
1214}
1215
Yaron Kereneb2a2542016-01-29 20:50:44 +00001216LLVM_DUMP_METHOD void AttributeSet::dump() const {
Bill Wendlingd509a662013-01-29 00:34:06 +00001217 dbgs() << "PAL[\n";
1218
1219 for (unsigned i = 0, e = getNumSlots(); i < e; ++i) {
1220 uint64_t Index = getSlotIndex(i);
1221 dbgs() << " { ";
1222 if (Index == ~0U)
1223 dbgs() << "~0U";
1224 else
1225 dbgs() << Index;
1226 dbgs() << " => " << getAsString(Index) << " }\n";
1227 }
1228
1229 dbgs() << "]\n";
1230}
1231
Bill Wendlingabf3feb2012-10-05 06:44:41 +00001232//===----------------------------------------------------------------------===//
Bill Wendlingcd330342013-01-04 23:27:34 +00001233// AttrBuilder Method Implementations
Bill Wendlingabf3feb2012-10-05 06:44:41 +00001234//===----------------------------------------------------------------------===//
1235
Bill Wendling211316c2013-04-18 20:17:28 +00001236AttrBuilder::AttrBuilder(AttributeSet AS, unsigned Index)
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001237 : Attrs(0), Alignment(0), StackAlignment(0), DerefBytes(0),
George Burgess IV278199f2016-04-12 01:05:35 +00001238 DerefOrNullBytes(0), AllocSizeArgs(0) {
Bill Wendling56b0b2a2013-01-27 21:23:46 +00001239 AttributeSetImpl *pImpl = AS.pImpl;
Bill Wendling096f5442013-01-07 08:24:35 +00001240 if (!pImpl) return;
1241
Amaury Sechet24c84fd2016-06-14 22:04:16 +00001242 for (unsigned I = 0, E = pImpl->getNumSlots(); I != E; ++I) {
Bill Wendling211316c2013-04-18 20:17:28 +00001243 if (pImpl->getSlotIndex(I) != Index) continue;
Bill Wendling096f5442013-01-07 08:24:35 +00001244
Benjamin Kramer741146b2013-07-11 12:13:16 +00001245 for (AttributeSetImpl::iterator II = pImpl->begin(I),
Bill Wendling9eb689c2013-01-28 00:21:34 +00001246 IE = pImpl->end(I); II != IE; ++II)
Bill Wendling23804da2013-01-31 23:38:01 +00001247 addAttribute(*II);
Bill Wendling9eb689c2013-01-28 00:21:34 +00001248
1249 break;
1250 }
Bill Wendling096f5442013-01-07 08:24:35 +00001251}
1252
Bill Wendlingcd330342013-01-04 23:27:34 +00001253void AttrBuilder::clear() {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001254 Attrs.reset();
Sanjoy Das88d0fde2015-09-03 22:27:42 +00001255 TargetDepAttrs.clear();
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001256 Alignment = StackAlignment = DerefBytes = DerefOrNullBytes = 0;
George Burgess IV278199f2016-04-12 01:05:35 +00001257 AllocSizeArgs = 0;
Bill Wendlingcd330342013-01-04 23:27:34 +00001258}
1259
1260AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001261 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
Bill Wendling1c7cc8a2013-01-31 23:16:25 +00001262 assert(Val != Attribute::Alignment && Val != Attribute::StackAlignment &&
George Burgess IV278199f2016-04-12 01:05:35 +00001263 Val != Attribute::Dereferenceable && Val != Attribute::AllocSize &&
Hal Finkelb0407ba2014-07-18 15:51:28 +00001264 "Adding integer attribute without adding a value!");
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001265 Attrs[Val] = true;
Bill Wendling7c04e042012-10-09 19:01:18 +00001266 return *this;
Bill Wendlingabf3feb2012-10-05 06:44:41 +00001267}
1268
Bill Wendling23804da2013-01-31 23:38:01 +00001269AttrBuilder &AttrBuilder::addAttribute(Attribute Attr) {
Bill Wendling0a437302013-02-10 10:13:23 +00001270 if (Attr.isStringAttribute()) {
1271 addAttribute(Attr.getKindAsString(), Attr.getValueAsString());
1272 return *this;
1273 }
1274
Bill Wendling3f12ac22013-02-05 22:37:24 +00001275 Attribute::AttrKind Kind = Attr.getKindAsEnum();
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001276 Attrs[Kind] = true;
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001277
Bill Wendling3f12ac22013-02-05 22:37:24 +00001278 if (Kind == Attribute::Alignment)
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001279 Alignment = Attr.getAlignment();
Bill Wendling3f12ac22013-02-05 22:37:24 +00001280 else if (Kind == Attribute::StackAlignment)
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001281 StackAlignment = Attr.getStackAlignment();
Hal Finkelb0407ba2014-07-18 15:51:28 +00001282 else if (Kind == Attribute::Dereferenceable)
1283 DerefBytes = Attr.getDereferenceableBytes();
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001284 else if (Kind == Attribute::DereferenceableOrNull)
1285 DerefOrNullBytes = Attr.getDereferenceableOrNullBytes();
George Burgess IV278199f2016-04-12 01:05:35 +00001286 else if (Kind == Attribute::AllocSize)
1287 AllocSizeArgs = Attr.getValueAsInt();
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001288 return *this;
1289}
1290
Bill Wendlingb9c5b1a2013-02-05 08:09:32 +00001291AttrBuilder &AttrBuilder::addAttribute(StringRef A, StringRef V) {
1292 TargetDepAttrs[A] = V;
1293 return *this;
1294}
1295
Bill Wendling23804da2013-01-31 23:38:01 +00001296AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001297 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
1298 Attrs[Val] = false;
Bill Wendling23804da2013-01-31 23:38:01 +00001299
1300 if (Val == Attribute::Alignment)
1301 Alignment = 0;
1302 else if (Val == Attribute::StackAlignment)
1303 StackAlignment = 0;
Hal Finkelb0407ba2014-07-18 15:51:28 +00001304 else if (Val == Attribute::Dereferenceable)
1305 DerefBytes = 0;
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001306 else if (Val == Attribute::DereferenceableOrNull)
1307 DerefOrNullBytes = 0;
George Burgess IV278199f2016-04-12 01:05:35 +00001308 else if (Val == Attribute::AllocSize)
1309 AllocSizeArgs = 0;
Bill Wendling23804da2013-01-31 23:38:01 +00001310
1311 return *this;
1312}
1313
Bill Wendlingd2196752013-01-30 23:07:40 +00001314AttrBuilder &AttrBuilder::removeAttributes(AttributeSet A, uint64_t Index) {
Bill Wendling211316c2013-04-18 20:17:28 +00001315 unsigned Slot = ~0U;
Bill Wendlingf1c94e32013-02-01 00:13:50 +00001316 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
1317 if (A.getSlotIndex(I) == Index) {
Bill Wendling211316c2013-04-18 20:17:28 +00001318 Slot = I;
Bill Wendlingf1c94e32013-02-01 00:13:50 +00001319 break;
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001320 }
Bill Wendlingf1c94e32013-02-01 00:13:50 +00001321
Bill Wendling211316c2013-04-18 20:17:28 +00001322 assert(Slot != ~0U && "Couldn't find index in AttributeSet!");
Bill Wendlingf1c94e32013-02-01 00:13:50 +00001323
Bill Wendling211316c2013-04-18 20:17:28 +00001324 for (AttributeSet::iterator I = A.begin(Slot), E = A.end(Slot); I != E; ++I) {
Bill Wendling7cde51d2013-02-12 07:56:49 +00001325 Attribute Attr = *I;
Hal Finkele15442c2014-07-18 06:51:55 +00001326 if (Attr.isEnumAttribute() || Attr.isIntAttribute()) {
George Burgess IV500d3032015-12-16 05:21:02 +00001327 removeAttribute(Attr.getKindAsEnum());
Bill Wendling7cde51d2013-02-12 07:56:49 +00001328 } else {
1329 assert(Attr.isStringAttribute() && "Invalid attribute type!");
George Burgess IV500d3032015-12-16 05:21:02 +00001330 removeAttribute(Attr.getKindAsString());
Bill Wendling7cde51d2013-02-12 07:56:49 +00001331 }
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001332 }
1333
1334 return *this;
1335}
1336
Bill Wendlingb9c5b1a2013-02-05 08:09:32 +00001337AttrBuilder &AttrBuilder::removeAttribute(StringRef A) {
1338 std::map<std::string, std::string>::iterator I = TargetDepAttrs.find(A);
1339 if (I != TargetDepAttrs.end())
1340 TargetDepAttrs.erase(I);
1341 return *this;
1342}
1343
George Burgess IV278199f2016-04-12 01:05:35 +00001344std::pair<unsigned, Optional<unsigned>> AttrBuilder::getAllocSizeArgs() const {
1345 return unpackAllocSizeArgs(AllocSizeArgs);
1346}
1347
Bill Wendling50d27842012-10-15 20:35:56 +00001348AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
Bill Wendlingabd5ba22012-10-14 03:58:29 +00001349 if (Align == 0) return *this;
Bill Wendlingcd330342013-01-04 23:27:34 +00001350
Bill Wendlingabf3feb2012-10-05 06:44:41 +00001351 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1352 assert(Align <= 0x40000000 && "Alignment too large.");
Bill Wendlingcd330342013-01-04 23:27:34 +00001353
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001354 Attrs[Attribute::Alignment] = true;
Bill Wendlingcd330342013-01-04 23:27:34 +00001355 Alignment = Align;
Bill Wendlingabd5ba22012-10-14 03:58:29 +00001356 return *this;
Chris Lattner3e13b8c2008-01-02 23:42:30 +00001357}
1358
Bill Wendlingcd330342013-01-04 23:27:34 +00001359AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
1360 // Default alignment, allow the target to define how to align it.
1361 if (Align == 0) return *this;
1362
1363 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1364 assert(Align <= 0x100 && "Alignment too large.");
1365
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001366 Attrs[Attribute::StackAlignment] = true;
Bill Wendlingcd330342013-01-04 23:27:34 +00001367 StackAlignment = Align;
1368 return *this;
1369}
1370
Hal Finkelb0407ba2014-07-18 15:51:28 +00001371AttrBuilder &AttrBuilder::addDereferenceableAttr(uint64_t Bytes) {
1372 if (Bytes == 0) return *this;
1373
1374 Attrs[Attribute::Dereferenceable] = true;
1375 DerefBytes = Bytes;
1376 return *this;
1377}
1378
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001379AttrBuilder &AttrBuilder::addDereferenceableOrNullAttr(uint64_t Bytes) {
1380 if (Bytes == 0)
1381 return *this;
1382
1383 Attrs[Attribute::DereferenceableOrNull] = true;
1384 DerefOrNullBytes = Bytes;
1385 return *this;
1386}
1387
George Burgess IV278199f2016-04-12 01:05:35 +00001388AttrBuilder &AttrBuilder::addAllocSizeAttr(unsigned ElemSize,
1389 const Optional<unsigned> &NumElems) {
1390 return addAllocSizeAttrFromRawRepr(packAllocSizeArgs(ElemSize, NumElems));
1391}
1392
1393AttrBuilder &AttrBuilder::addAllocSizeAttrFromRawRepr(uint64_t RawArgs) {
1394 // (0, 0) is our "not present" value, so we need to check for it here.
1395 assert(RawArgs && "Invalid allocsize arguments -- given allocsize(0, 0)");
1396
1397 Attrs[Attribute::AllocSize] = true;
1398 // Reuse existing machinery to store this as a single 64-bit integer so we can
1399 // save a few bytes over using a pair<unsigned, Optional<unsigned>>.
1400 AllocSizeArgs = RawArgs;
1401 return *this;
1402}
1403
Bill Wendlinge2614922013-02-06 01:16:00 +00001404AttrBuilder &AttrBuilder::merge(const AttrBuilder &B) {
1405 // FIXME: What if both have alignments, but they don't match?!
1406 if (!Alignment)
1407 Alignment = B.Alignment;
1408
1409 if (!StackAlignment)
1410 StackAlignment = B.StackAlignment;
1411
Hal Finkelb0407ba2014-07-18 15:51:28 +00001412 if (!DerefBytes)
1413 DerefBytes = B.DerefBytes;
1414
Pete Cooperd2a44612015-05-06 23:19:43 +00001415 if (!DerefOrNullBytes)
1416 DerefOrNullBytes = B.DerefOrNullBytes;
1417
George Burgess IV278199f2016-04-12 01:05:35 +00001418 if (!AllocSizeArgs)
1419 AllocSizeArgs = B.AllocSizeArgs;
1420
Benjamin Kramer45e7d532013-02-16 19:13:18 +00001421 Attrs |= B.Attrs;
Bill Wendlinge2614922013-02-06 01:16:00 +00001422
Pete Cooperd2a44612015-05-06 23:19:43 +00001423 for (auto I : B.td_attrs())
1424 TargetDepAttrs[I.first] = I.second;
Bill Wendlinge2614922013-02-06 01:16:00 +00001425
1426 return *this;
1427}
1428
Pete Cooperd2a44612015-05-06 23:19:43 +00001429AttrBuilder &AttrBuilder::remove(const AttrBuilder &B) {
1430 // FIXME: What if both have alignments, but they don't match?!
1431 if (B.Alignment)
1432 Alignment = 0;
1433
1434 if (B.StackAlignment)
1435 StackAlignment = 0;
1436
1437 if (B.DerefBytes)
1438 DerefBytes = 0;
1439
1440 if (B.DerefOrNullBytes)
1441 DerefOrNullBytes = 0;
1442
George Burgess IV278199f2016-04-12 01:05:35 +00001443 if (B.AllocSizeArgs)
1444 AllocSizeArgs = 0;
1445
Pete Cooperd2a44612015-05-06 23:19:43 +00001446 Attrs &= ~B.Attrs;
1447
1448 for (auto I : B.td_attrs())
1449 TargetDepAttrs.erase(I.first);
1450
1451 return *this;
1452}
1453
1454bool AttrBuilder::overlaps(const AttrBuilder &B) const {
1455 // First check if any of the target independent attributes overlap.
1456 if ((Attrs & B.Attrs).any())
1457 return true;
1458
1459 // Then check if any target dependent ones do.
1460 for (auto I : td_attrs())
1461 if (B.contains(I.first))
1462 return true;
1463
1464 return false;
1465}
1466
Bill Wendling4b001442013-02-06 01:33:42 +00001467bool AttrBuilder::contains(StringRef A) const {
1468 return TargetDepAttrs.find(A) != TargetDepAttrs.end();
1469}
1470
Bill Wendling50d27842012-10-15 20:35:56 +00001471bool AttrBuilder::hasAttributes() const {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001472 return !Attrs.none() || !TargetDepAttrs.empty();
Bill Wendlingc6daefa2012-10-08 23:27:46 +00001473}
Bill Wendling9ac69f92013-01-04 20:54:35 +00001474
Bill Wendlingd2196752013-01-30 23:07:40 +00001475bool AttrBuilder::hasAttributes(AttributeSet A, uint64_t Index) const {
Bill Wendling211316c2013-04-18 20:17:28 +00001476 unsigned Slot = ~0U;
Bill Wendling9ca01da2013-02-02 00:42:06 +00001477 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
1478 if (A.getSlotIndex(I) == Index) {
Bill Wendling211316c2013-04-18 20:17:28 +00001479 Slot = I;
Bill Wendling9ca01da2013-02-02 00:42:06 +00001480 break;
1481 }
1482
Bill Wendling211316c2013-04-18 20:17:28 +00001483 assert(Slot != ~0U && "Couldn't find the index!");
Bill Wendling9ca01da2013-02-02 00:42:06 +00001484
George Burgess IV500d3032015-12-16 05:21:02 +00001485 for (AttributeSet::iterator I = A.begin(Slot), E = A.end(Slot); I != E; ++I) {
Bill Wendling7cde51d2013-02-12 07:56:49 +00001486 Attribute Attr = *I;
Hal Finkele15442c2014-07-18 06:51:55 +00001487 if (Attr.isEnumAttribute() || Attr.isIntAttribute()) {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001488 if (Attrs[I->getKindAsEnum()])
Bill Wendling7cde51d2013-02-12 07:56:49 +00001489 return true;
1490 } else {
1491 assert(Attr.isStringAttribute() && "Invalid attribute kind!");
1492 return TargetDepAttrs.find(Attr.getKindAsString())!=TargetDepAttrs.end();
1493 }
1494 }
Bill Wendling9ca01da2013-02-02 00:42:06 +00001495
1496 return false;
Bill Wendling70f39172012-10-09 00:01:21 +00001497}
Bill Wendling9ac69f92013-01-04 20:54:35 +00001498
Bill Wendling50d27842012-10-15 20:35:56 +00001499bool AttrBuilder::hasAlignmentAttr() const {
Bill Wendlingcd330342013-01-04 23:27:34 +00001500 return Alignment != 0;
Bill Wendlingc6daefa2012-10-08 23:27:46 +00001501}
1502
Bill Wendlingd509a662013-01-29 00:34:06 +00001503bool AttrBuilder::operator==(const AttrBuilder &B) {
Benjamin Kramer45e7d532013-02-16 19:13:18 +00001504 if (Attrs != B.Attrs)
1505 return false;
Bill Wendling4b001442013-02-06 01:33:42 +00001506
1507 for (td_const_iterator I = TargetDepAttrs.begin(),
1508 E = TargetDepAttrs.end(); I != E; ++I)
1509 if (B.TargetDepAttrs.find(I->first) == B.TargetDepAttrs.end())
1510 return false;
1511
Hal Finkelb0407ba2014-07-18 15:51:28 +00001512 return Alignment == B.Alignment && StackAlignment == B.StackAlignment &&
1513 DerefBytes == B.DerefBytes;
Bill Wendlingd509a662013-01-29 00:34:06 +00001514}
1515
1516AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) {
Bill Wendling56aeccc2013-02-04 23:32:23 +00001517 // FIXME: Remove this in 4.0.
Bill Wendling60011b82013-01-29 01:43:29 +00001518 if (!Val) return *this;
1519
Bill Wendlingd509a662013-01-29 00:34:06 +00001520 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
1521 I = Attribute::AttrKind(I + 1)) {
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001522 if (I == Attribute::Dereferenceable ||
Igor Laevsky39d662f2015-07-11 10:30:36 +00001523 I == Attribute::DereferenceableOrNull ||
George Burgess IV278199f2016-04-12 01:05:35 +00001524 I == Attribute::ArgMemOnly ||
1525 I == Attribute::AllocSize)
Hal Finkelb0407ba2014-07-18 15:51:28 +00001526 continue;
Bill Wendlingd509a662013-01-29 00:34:06 +00001527 if (uint64_t A = (Val & AttributeImpl::getAttrMask(I))) {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001528 Attrs[I] = true;
Bill Wendlingd509a662013-01-29 00:34:06 +00001529
1530 if (I == Attribute::Alignment)
1531 Alignment = 1ULL << ((A >> 16) - 1);
1532 else if (I == Attribute::StackAlignment)
1533 StackAlignment = 1ULL << ((A >> 26)-1);
1534 }
1535 }
1536
1537 return *this;
1538}
1539
Bill Wendling57625a42013-01-25 23:09:36 +00001540//===----------------------------------------------------------------------===//
1541// AttributeFuncs Function Defintions
1542//===----------------------------------------------------------------------===//
1543
Bill Wendlingc79cdff2013-02-01 01:04:27 +00001544/// \brief Which attributes cannot be applied to a type.
Craig Toppere3dcce92015-08-01 22:20:21 +00001545AttrBuilder AttributeFuncs::typeIncompatible(Type *Ty) {
Bill Wendling57625a42013-01-25 23:09:36 +00001546 AttrBuilder Incompatible;
1547
1548 if (!Ty->isIntegerTy())
1549 // Attribute that only apply to integers.
1550 Incompatible.addAttribute(Attribute::SExt)
1551 .addAttribute(Attribute::ZExt);
1552
1553 if (!Ty->isPointerTy())
1554 // Attribute that only apply to pointers.
1555 Incompatible.addAttribute(Attribute::ByVal)
1556 .addAttribute(Attribute::Nest)
1557 .addAttribute(Attribute::NoAlias)
1558 .addAttribute(Attribute::NoCapture)
Nick Lewyckyd52b1522014-05-20 01:23:40 +00001559 .addAttribute(Attribute::NonNull)
Hal Finkelb0407ba2014-07-18 15:51:28 +00001560 .addDereferenceableAttr(1) // the int here is ignored
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001561 .addDereferenceableOrNullAttr(1) // the int here is ignored
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001562 .addAttribute(Attribute::ReadNone)
1563 .addAttribute(Attribute::ReadOnly)
Reid Klecknera534a382013-12-19 02:14:12 +00001564 .addAttribute(Attribute::StructRet)
1565 .addAttribute(Attribute::InAlloca);
Bill Wendling57625a42013-01-25 23:09:36 +00001566
Pete Cooper2777d8872015-05-06 23:19:56 +00001567 return Incompatible;
Bill Wendling57625a42013-01-25 23:09:36 +00001568}
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001569
1570template<typename AttrClass>
1571static bool isEqual(const Function &Caller, const Function &Callee) {
1572 return Caller.getFnAttribute(AttrClass::getKind()) ==
1573 Callee.getFnAttribute(AttrClass::getKind());
1574}
1575
1576/// \brief Compute the logical AND of the attributes of the caller and the
1577/// callee.
1578///
1579/// This function sets the caller's attribute to false if the callee's attribute
1580/// is false.
1581template<typename AttrClass>
1582static void setAND(Function &Caller, const Function &Callee) {
1583 if (AttrClass::isSet(Caller, AttrClass::getKind()) &&
1584 !AttrClass::isSet(Callee, AttrClass::getKind()))
1585 AttrClass::set(Caller, AttrClass::getKind(), false);
1586}
1587
1588/// \brief Compute the logical OR of the attributes of the caller and the
1589/// callee.
1590///
1591/// This function sets the caller's attribute to true if the callee's attribute
1592/// is true.
1593template<typename AttrClass>
1594static void setOR(Function &Caller, const Function &Callee) {
1595 if (!AttrClass::isSet(Caller, AttrClass::getKind()) &&
1596 AttrClass::isSet(Callee, AttrClass::getKind()))
1597 AttrClass::set(Caller, AttrClass::getKind(), true);
1598}
1599
1600/// \brief If the inlined function had a higher stack protection level than the
1601/// calling function, then bump up the caller's stack protection level.
1602static void adjustCallerSSPLevel(Function &Caller, const Function &Callee) {
1603 // If upgrading the SSP attribute, clear out the old SSP Attributes first.
1604 // Having multiple SSP attributes doesn't actually hurt, but it adds useless
1605 // clutter to the IR.
1606 AttrBuilder B;
1607 B.addAttribute(Attribute::StackProtect)
1608 .addAttribute(Attribute::StackProtectStrong)
1609 .addAttribute(Attribute::StackProtectReq);
1610 AttributeSet OldSSPAttr = AttributeSet::get(Caller.getContext(),
1611 AttributeSet::FunctionIndex,
1612 B);
1613
Evgeniy Stepanovf17120a2016-04-11 22:27:48 +00001614 if (Callee.hasFnAttribute(Attribute::StackProtectReq)) {
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001615 Caller.removeAttributes(AttributeSet::FunctionIndex, OldSSPAttr);
1616 Caller.addFnAttr(Attribute::StackProtectReq);
1617 } else if (Callee.hasFnAttribute(Attribute::StackProtectStrong) &&
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001618 !Caller.hasFnAttribute(Attribute::StackProtectReq)) {
1619 Caller.removeAttributes(AttributeSet::FunctionIndex, OldSSPAttr);
1620 Caller.addFnAttr(Attribute::StackProtectStrong);
1621 } else if (Callee.hasFnAttribute(Attribute::StackProtect) &&
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001622 !Caller.hasFnAttribute(Attribute::StackProtectReq) &&
1623 !Caller.hasFnAttribute(Attribute::StackProtectStrong))
1624 Caller.addFnAttr(Attribute::StackProtect);
1625}
1626
1627#define GET_ATTR_COMPAT_FUNC
1628#include "AttributesCompatFunc.inc"
1629
1630bool AttributeFuncs::areInlineCompatible(const Function &Caller,
1631 const Function &Callee) {
1632 return hasCompatibleFnAttrs(Caller, Callee);
1633}
1634
1635
1636void AttributeFuncs::mergeAttributesForInlining(Function &Caller,
1637 const Function &Callee) {
1638 mergeFnAttrs(Caller, Callee);
1639}