blob: 2b76de77dcad2d7c8834574bfa3d57635019d85e [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
Honggyu Kim9eb6a102016-09-01 11:44:06 +0000384 std::string AttrVal = pImpl->getValueAsString();
385 if (AttrVal.empty()) return Result;
Bill Wendling7a33f772013-02-01 22:32:30 +0000386
Honggyu Kim9eb6a102016-09-01 11:44:06 +0000387 // Since some attribute strings contain special characters that cannot be
388 // printable, those have to be escaped to make the attribute value printable
389 // as is. e.g. "\01__gnu_mcount_nc"
390 {
391 raw_string_ostream OS(Result);
392 OS << "=\"";
393 PrintEscapedString(AttrVal, OS);
394 OS << "\"";
395 }
Bill Wendlingc79cdff2013-02-01 01:04:27 +0000396 return Result;
Bill Wendling9c2eba92013-01-31 20:59:05 +0000397 }
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000398
399 llvm_unreachable("Unknown attribute");
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000400}
401
Bill Wendlingd509a662013-01-29 00:34:06 +0000402bool Attribute::operator<(Attribute A) const {
403 if (!pImpl && !A.pImpl) return false;
404 if (!pImpl) return true;
405 if (!A.pImpl) return false;
406 return *pImpl < *A.pImpl;
407}
408
Bill Wendlingd509a662013-01-29 00:34:06 +0000409//===----------------------------------------------------------------------===//
410// AttributeImpl Definition
411//===----------------------------------------------------------------------===//
412
Eric Christopher0eaa5412014-07-02 22:05:40 +0000413// Pin the vtables to this file.
Alexey Samsonov49109a22013-11-18 09:31:53 +0000414AttributeImpl::~AttributeImpl() {}
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +0000415void EnumAttributeImpl::anchor() {}
Hal Finkele15442c2014-07-18 06:51:55 +0000416void IntAttributeImpl::anchor() {}
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +0000417void StringAttributeImpl::anchor() {}
Alexey Samsonov49109a22013-11-18 09:31:53 +0000418
Bill Wendlingd509a662013-01-29 00:34:06 +0000419bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
Bill Wendling3f12ac22013-02-05 22:37:24 +0000420 if (isStringAttribute()) return false;
421 return getKindAsEnum() == A;
Bill Wendlingd509a662013-01-29 00:34:06 +0000422}
423
Bill Wendling3f12ac22013-02-05 22:37:24 +0000424bool AttributeImpl::hasAttribute(StringRef Kind) const {
425 if (!isStringAttribute()) return false;
426 return getKindAsString() == Kind;
Bill Wendlingd509a662013-01-29 00:34:06 +0000427}
428
Bill Wendling3f12ac22013-02-05 22:37:24 +0000429Attribute::AttrKind AttributeImpl::getKindAsEnum() const {
Hal Finkele15442c2014-07-18 06:51:55 +0000430 assert(isEnumAttribute() || isIntAttribute());
Benjamin Kramer741146b2013-07-11 12:13:16 +0000431 return static_cast<const EnumAttributeImpl *>(this)->getEnumKind();
Bill Wendlingd509a662013-01-29 00:34:06 +0000432}
433
Bill Wendling3f12ac22013-02-05 22:37:24 +0000434uint64_t AttributeImpl::getValueAsInt() const {
Hal Finkele15442c2014-07-18 06:51:55 +0000435 assert(isIntAttribute());
436 return static_cast<const IntAttributeImpl *>(this)->getValue();
Bill Wendlingd509a662013-01-29 00:34:06 +0000437}
438
Bill Wendling3f12ac22013-02-05 22:37:24 +0000439StringRef AttributeImpl::getKindAsString() const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000440 assert(isStringAttribute());
441 return static_cast<const StringAttributeImpl *>(this)->getStringKind();
Bill Wendlingd509a662013-01-29 00:34:06 +0000442}
443
Bill Wendling3f12ac22013-02-05 22:37:24 +0000444StringRef AttributeImpl::getValueAsString() const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000445 assert(isStringAttribute());
446 return static_cast<const StringAttributeImpl *>(this)->getStringValue();
Bill Wendlingd509a662013-01-29 00:34:06 +0000447}
448
449bool AttributeImpl::operator<(const AttributeImpl &AI) const {
Bill Wendlingc79cdff2013-02-01 01:04:27 +0000450 // This sorts the attributes with Attribute::AttrKinds coming first (sorted
451 // relative to their enum value) and then strings.
Bill Wendling26b95752013-02-15 05:25:26 +0000452 if (isEnumAttribute()) {
453 if (AI.isEnumAttribute()) return getKindAsEnum() < AI.getKindAsEnum();
Hal Finkele15442c2014-07-18 06:51:55 +0000454 if (AI.isIntAttribute()) return true;
Bill Wendling26b95752013-02-15 05:25:26 +0000455 if (AI.isStringAttribute()) return true;
456 }
Bill Wendlingc79cdff2013-02-01 01:04:27 +0000457
Hal Finkele15442c2014-07-18 06:51:55 +0000458 if (isIntAttribute()) {
Bill Wendling26b95752013-02-15 05:25:26 +0000459 if (AI.isEnumAttribute()) return false;
Reid Kleckner7de67612016-04-04 23:06:05 +0000460 if (AI.isIntAttribute()) {
461 if (getKindAsEnum() == AI.getKindAsEnum())
462 return getValueAsInt() < AI.getValueAsInt();
463 return getKindAsEnum() < AI.getKindAsEnum();
464 }
Bill Wendling26b95752013-02-15 05:25:26 +0000465 if (AI.isStringAttribute()) return true;
Bill Wendling3f12ac22013-02-05 22:37:24 +0000466 }
Bill Wendlingd509a662013-01-29 00:34:06 +0000467
Bill Wendling26b95752013-02-15 05:25:26 +0000468 if (AI.isEnumAttribute()) return false;
Hal Finkele15442c2014-07-18 06:51:55 +0000469 if (AI.isIntAttribute()) return false;
Bill Wendling26b95752013-02-15 05:25:26 +0000470 if (getKindAsString() == AI.getKindAsString())
471 return getValueAsString() < AI.getValueAsString();
472 return getKindAsString() < AI.getKindAsString();
Bill Wendlingd509a662013-01-29 00:34:06 +0000473}
474
Bill Wendlingd509a662013-01-29 00:34:06 +0000475uint64_t AttributeImpl::getAttrMask(Attribute::AttrKind Val) {
476 // FIXME: Remove this.
477 switch (Val) {
478 case Attribute::EndAttrKinds:
Bill Wendlingd509a662013-01-29 00:34:06 +0000479 llvm_unreachable("Synthetic enumerators which should never get here");
480
481 case Attribute::None: return 0;
482 case Attribute::ZExt: return 1 << 0;
483 case Attribute::SExt: return 1 << 1;
484 case Attribute::NoReturn: return 1 << 2;
485 case Attribute::InReg: return 1 << 3;
486 case Attribute::StructRet: return 1 << 4;
487 case Attribute::NoUnwind: return 1 << 5;
488 case Attribute::NoAlias: return 1 << 6;
489 case Attribute::ByVal: return 1 << 7;
490 case Attribute::Nest: return 1 << 8;
491 case Attribute::ReadNone: return 1 << 9;
492 case Attribute::ReadOnly: return 1 << 10;
493 case Attribute::NoInline: return 1 << 11;
494 case Attribute::AlwaysInline: return 1 << 12;
495 case Attribute::OptimizeForSize: return 1 << 13;
496 case Attribute::StackProtect: return 1 << 14;
497 case Attribute::StackProtectReq: return 1 << 15;
498 case Attribute::Alignment: return 31 << 16;
499 case Attribute::NoCapture: return 1 << 21;
500 case Attribute::NoRedZone: return 1 << 22;
501 case Attribute::NoImplicitFloat: return 1 << 23;
502 case Attribute::Naked: return 1 << 24;
503 case Attribute::InlineHint: return 1 << 25;
504 case Attribute::StackAlignment: return 7 << 26;
505 case Attribute::ReturnsTwice: return 1 << 29;
506 case Attribute::UWTable: return 1 << 30;
507 case Attribute::NonLazyBind: return 1U << 31;
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000508 case Attribute::SanitizeAddress: return 1ULL << 32;
Bill Wendlingd509a662013-01-29 00:34:06 +0000509 case Attribute::MinSize: return 1ULL << 33;
510 case Attribute::NoDuplicate: return 1ULL << 34;
511 case Attribute::StackProtectStrong: return 1ULL << 35;
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000512 case Attribute::SanitizeThread: return 1ULL << 36;
513 case Attribute::SanitizeMemory: return 1ULL << 37;
Bill Wendling7bfdfe72013-02-22 00:40:12 +0000514 case Attribute::NoBuiltin: return 1ULL << 38;
Stephen Linb8bd2322013-04-20 05:14:40 +0000515 case Attribute::Returned: return 1ULL << 39;
Diego Novilloc6399532013-05-24 12:26:52 +0000516 case Attribute::Cold: return 1ULL << 40;
Michael Gottesman41748d72013-06-27 00:25:01 +0000517 case Attribute::Builtin: return 1ULL << 41;
Andrea Di Biagio377496b2013-08-23 11:53:55 +0000518 case Attribute::OptimizeNone: return 1ULL << 42;
Reid Klecknera534a382013-12-19 02:14:12 +0000519 case Attribute::InAlloca: return 1ULL << 43;
Nick Lewyckyd52b1522014-05-20 01:23:40 +0000520 case Attribute::NonNull: return 1ULL << 44;
Tom Roeder44cb65f2014-06-05 19:29:43 +0000521 case Attribute::JumpTable: return 1ULL << 45;
Owen Anderson85fa7d52015-05-26 23:48:40 +0000522 case Attribute::Convergent: return 1ULL << 46;
Peter Collingbourne82437bf2015-06-15 21:07:11 +0000523 case Attribute::SafeStack: return 1ULL << 47;
James Molloye6f87ca2015-11-06 10:32:53 +0000524 case Attribute::NoRecurse: return 1ULL << 48;
Vaivaswatha Nagarajfb3f4902015-12-16 16:16:19 +0000525 case Attribute::InaccessibleMemOnly: return 1ULL << 49;
526 case Attribute::InaccessibleMemOrArgMemOnly: return 1ULL << 50;
Manman Renf46262e2016-03-29 17:37:21 +0000527 case Attribute::SwiftSelf: return 1ULL << 51;
Manman Ren9bfd0d02016-04-01 21:41:15 +0000528 case Attribute::SwiftError: return 1ULL << 52;
Nicolai Haehnle84c9f992016-07-04 08:01:29 +0000529 case Attribute::WriteOnly: return 1ULL << 53;
Hal Finkelb0407ba2014-07-18 15:51:28 +0000530 case Attribute::Dereferenceable:
531 llvm_unreachable("dereferenceable attribute not supported in raw format");
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000532 break;
533 case Attribute::DereferenceableOrNull:
534 llvm_unreachable("dereferenceable_or_null attribute not supported in raw "
535 "format");
536 break;
Igor Laevsky39d662f2015-07-11 10:30:36 +0000537 case Attribute::ArgMemOnly:
538 llvm_unreachable("argmemonly attribute not supported in raw format");
539 break;
George Burgess IV278199f2016-04-12 01:05:35 +0000540 case Attribute::AllocSize:
541 llvm_unreachable("allocsize not supported in raw format");
542 break;
Bill Wendling25342e12013-02-22 00:50:09 +0000543 }
544 llvm_unreachable("Unsupported attribute type");
Bill Wendlingd509a662013-01-29 00:34:06 +0000545}
546
547//===----------------------------------------------------------------------===//
548// AttributeSetNode Definition
549//===----------------------------------------------------------------------===//
550
551AttributeSetNode *AttributeSetNode::get(LLVMContext &C,
552 ArrayRef<Attribute> Attrs) {
553 if (Attrs.empty())
Craig Topperc6207612014-04-09 06:08:46 +0000554 return nullptr;
Bill Wendlingd509a662013-01-29 00:34:06 +0000555
556 // Otherwise, build a key to look up the existing attributes.
557 LLVMContextImpl *pImpl = C.pImpl;
558 FoldingSetNodeID ID;
559
560 SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end());
Reid Kleckner7de67612016-04-04 23:06:05 +0000561 std::sort(SortedAttrs.begin(), SortedAttrs.end());
Bill Wendlingd509a662013-01-29 00:34:06 +0000562
George Burgess IV500d3032015-12-16 05:21:02 +0000563 for (Attribute Attr : SortedAttrs)
564 Attr.Profile(ID);
Bill Wendlingd509a662013-01-29 00:34:06 +0000565
566 void *InsertPoint;
567 AttributeSetNode *PA =
568 pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint);
569
570 // If we didn't find any existing attributes of the same shape then create a
571 // new one and insert it.
572 if (!PA) {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000573 // Coallocate entries after the AttributeSetNode itself.
James Y Knightaa365b22015-08-05 22:57:34 +0000574 void *Mem = ::operator new(totalSizeToAlloc<Attribute>(SortedAttrs.size()));
Benjamin Kramer741146b2013-07-11 12:13:16 +0000575 PA = new (Mem) AttributeSetNode(SortedAttrs);
Bill Wendlingd509a662013-01-29 00:34:06 +0000576 pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint);
577 }
578
579 // Return the AttributesListNode that we found or created.
580 return PA;
581}
582
Bill Wendlingbce7b972013-02-13 08:42:21 +0000583bool AttributeSetNode::hasAttribute(StringRef Kind) const {
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000584 for (Attribute I : *this)
585 if (I.hasAttribute(Kind))
Bill Wendlingbce7b972013-02-13 08:42:21 +0000586 return true;
587 return false;
588}
589
590Attribute AttributeSetNode::getAttribute(Attribute::AttrKind Kind) const {
Matthias Braun31eeb76f2016-01-29 22:25:13 +0000591 if (hasAttribute(Kind)) {
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000592 for (Attribute I : *this)
593 if (I.hasAttribute(Kind))
594 return I;
Matthias Braun31eeb76f2016-01-29 22:25:13 +0000595 }
Bill Wendlingbce7b972013-02-13 08:42:21 +0000596 return Attribute();
597}
598
599Attribute AttributeSetNode::getAttribute(StringRef Kind) const {
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000600 for (Attribute I : *this)
601 if (I.hasAttribute(Kind))
602 return I;
Bill Wendlingbce7b972013-02-13 08:42:21 +0000603 return Attribute();
604}
605
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000606unsigned AttributeSetNode::getAlignment() const {
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000607 for (Attribute I : *this)
608 if (I.hasAttribute(Attribute::Alignment))
609 return I.getAlignment();
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000610 return 0;
611}
612
613unsigned AttributeSetNode::getStackAlignment() const {
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000614 for (Attribute I : *this)
615 if (I.hasAttribute(Attribute::StackAlignment))
616 return I.getStackAlignment();
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000617 return 0;
618}
619
Hal Finkelb0407ba2014-07-18 15:51:28 +0000620uint64_t AttributeSetNode::getDereferenceableBytes() const {
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000621 for (Attribute I : *this)
622 if (I.hasAttribute(Attribute::Dereferenceable))
623 return I.getDereferenceableBytes();
Hal Finkelb0407ba2014-07-18 15:51:28 +0000624 return 0;
625}
626
Sanjoy Das06cf33f2015-05-06 17:41:54 +0000627uint64_t AttributeSetNode::getDereferenceableOrNullBytes() const {
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000628 for (Attribute I : *this)
629 if (I.hasAttribute(Attribute::DereferenceableOrNull))
630 return I.getDereferenceableOrNullBytes();
Sanjoy Das06cf33f2015-05-06 17:41:54 +0000631 return 0;
632}
633
George Burgess IV278199f2016-04-12 01:05:35 +0000634std::pair<unsigned, Optional<unsigned>>
635AttributeSetNode::getAllocSizeArgs() const {
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000636 for (Attribute I : *this)
637 if (I.hasAttribute(Attribute::AllocSize))
638 return I.getAllocSizeArgs();
George Burgess IV278199f2016-04-12 01:05:35 +0000639 return std::make_pair(0, 0);
640}
641
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +0000642std::string AttributeSetNode::getAsString(bool InAttrGrp) const {
Benjamin Kramer0baf8f42013-04-19 11:43:21 +0000643 std::string Str;
Benjamin Kramer741146b2013-07-11 12:13:16 +0000644 for (iterator I = begin(), E = end(); I != E; ++I) {
645 if (I != begin())
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +0000646 Str += ' ';
647 Str += I->getAsString(InAttrGrp);
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000648 }
649 return Str;
650}
651
Bill Wendlingd509a662013-01-29 00:34:06 +0000652//===----------------------------------------------------------------------===//
653// AttributeSetImpl Definition
654//===----------------------------------------------------------------------===//
655
Rafael Espindoladd275302013-04-30 16:53:38 +0000656uint64_t AttributeSetImpl::Raw(unsigned Index) const {
Amaury Sechet24c84fd2016-06-14 22:04:16 +0000657 for (unsigned I = 0, E = getNumSlots(); I != E; ++I) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000658 if (getSlotIndex(I) != Index) continue;
Benjamin Kramer741146b2013-07-11 12:13:16 +0000659 const AttributeSetNode *ASN = getSlotNode(I);
Bill Wendling91226182013-02-02 00:52:44 +0000660 uint64_t Mask = 0;
Bill Wendlingd509a662013-01-29 00:34:06 +0000661
Benjamin Kramer741146b2013-07-11 12:13:16 +0000662 for (AttributeSetNode::iterator II = ASN->begin(),
Bill Wendling91226182013-02-02 00:52:44 +0000663 IE = ASN->end(); II != IE; ++II) {
664 Attribute Attr = *II;
Bill Wendlingd746e402013-02-10 23:18:05 +0000665
666 // This cannot handle string attributes.
667 if (Attr.isStringAttribute()) continue;
668
Bill Wendling3f12ac22013-02-05 22:37:24 +0000669 Attribute::AttrKind Kind = Attr.getKindAsEnum();
Bill Wendling91226182013-02-02 00:52:44 +0000670
Bill Wendling3f12ac22013-02-05 22:37:24 +0000671 if (Kind == Attribute::Alignment)
Bill Wendling91226182013-02-02 00:52:44 +0000672 Mask |= (Log2_32(ASN->getAlignment()) + 1) << 16;
Bill Wendling3f12ac22013-02-05 22:37:24 +0000673 else if (Kind == Attribute::StackAlignment)
Bill Wendling91226182013-02-02 00:52:44 +0000674 Mask |= (Log2_32(ASN->getStackAlignment()) + 1) << 26;
Hal Finkelb0407ba2014-07-18 15:51:28 +0000675 else if (Kind == Attribute::Dereferenceable)
676 llvm_unreachable("dereferenceable not supported in bit mask");
George Burgess IV278199f2016-04-12 01:05:35 +0000677 else if (Kind == Attribute::AllocSize)
678 llvm_unreachable("allocsize not supported in bit mask");
Bill Wendling91226182013-02-02 00:52:44 +0000679 else
Bill Wendling3f12ac22013-02-05 22:37:24 +0000680 Mask |= AttributeImpl::getAttrMask(Kind);
Bill Wendling91226182013-02-02 00:52:44 +0000681 }
682
683 return Mask;
Bill Wendlingd509a662013-01-29 00:34:06 +0000684 }
685
686 return 0;
687}
688
Yaron Kereneb2a2542016-01-29 20:50:44 +0000689LLVM_DUMP_METHOD void AttributeSetImpl::dump() const {
Peter Collingbourneabca2ec2013-08-02 22:34:30 +0000690 AttributeSet(const_cast<AttributeSetImpl *>(this)).dump();
691}
692
Bill Wendlingd509a662013-01-29 00:34:06 +0000693//===----------------------------------------------------------------------===//
694// AttributeSet Construction and Mutation Methods
695//===----------------------------------------------------------------------===//
696
Bill Wendling60011b82013-01-29 01:43:29 +0000697AttributeSet
698AttributeSet::getImpl(LLVMContext &C,
699 ArrayRef<std::pair<unsigned, AttributeSetNode*> > Attrs) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000700 LLVMContextImpl *pImpl = C.pImpl;
701 FoldingSetNodeID ID;
702 AttributeSetImpl::Profile(ID, Attrs);
703
704 void *InsertPoint;
705 AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
706
707 // If we didn't find any existing attributes of the same shape then
708 // create a new one and insert it.
709 if (!PA) {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000710 // Coallocate entries after the AttributeSetImpl itself.
James Y Knightaa365b22015-08-05 22:57:34 +0000711 void *Mem = ::operator new(
712 AttributeSetImpl::totalSizeToAlloc<IndexAttrPair>(Attrs.size()));
Benjamin Kramer741146b2013-07-11 12:13:16 +0000713 PA = new (Mem) AttributeSetImpl(C, Attrs);
Bill Wendlingd509a662013-01-29 00:34:06 +0000714 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
715 }
716
717 // Return the AttributesList that we found or created.
718 return AttributeSet(PA);
719}
720
721AttributeSet AttributeSet::get(LLVMContext &C,
722 ArrayRef<std::pair<unsigned, Attribute> > Attrs){
723 // If there are no attributes then return a null AttributesList pointer.
724 if (Attrs.empty())
725 return AttributeSet();
726
Craig Toppere30b8ca2016-01-03 19:43:40 +0000727 assert(std::is_sorted(Attrs.begin(), Attrs.end(),
728 [](const std::pair<unsigned, Attribute> &LHS,
729 const std::pair<unsigned, Attribute> &RHS) {
730 return LHS.first < RHS.first;
731 }) && "Misordered Attributes list!");
David Majnemer0a16c222016-08-11 21:15:00 +0000732 assert(none_of(Attrs,
733 [](const std::pair<unsigned, Attribute> &Pair) {
734 return Pair.second.hasAttribute(Attribute::None);
735 }) &&
736 "Pointless attribute!");
Bill Wendlingd509a662013-01-29 00:34:06 +0000737
738 // Create a vector if (unsigned, AttributeSetNode*) pairs from the attributes
739 // list.
740 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrPairVec;
741 for (ArrayRef<std::pair<unsigned, Attribute> >::iterator I = Attrs.begin(),
742 E = Attrs.end(); I != E; ) {
743 unsigned Index = I->first;
744 SmallVector<Attribute, 4> AttrVec;
NAKAMURA Takumif05d2f22013-01-29 15:18:16 +0000745 while (I != E && I->first == Index) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000746 AttrVec.push_back(I->second);
747 ++I;
748 }
749
David Majnemer0a16c222016-08-11 21:15:00 +0000750 AttrPairVec.emplace_back(Index, AttributeSetNode::get(C, AttrVec));
Bill Wendlingd509a662013-01-29 00:34:06 +0000751 }
752
753 return getImpl(C, AttrPairVec);
754}
755
756AttributeSet AttributeSet::get(LLVMContext &C,
757 ArrayRef<std::pair<unsigned,
758 AttributeSetNode*> > Attrs) {
759 // If there are no attributes then return a null AttributesList pointer.
760 if (Attrs.empty())
761 return AttributeSet();
762
763 return getImpl(C, Attrs);
764}
765
David Majnemercf63a792014-05-03 23:00:35 +0000766AttributeSet AttributeSet::get(LLVMContext &C, unsigned Index,
767 const AttrBuilder &B) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000768 if (!B.hasAttributes())
769 return AttributeSet();
Bill Wendlingf7134812013-01-29 01:02:03 +0000770
Bill Wendlingae89a0f2013-02-05 23:48:36 +0000771 // Add target-independent attributes.
Bill Wendlingf7134812013-01-29 01:02:03 +0000772 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
Benjamin Kramer45e7d532013-02-16 19:13:18 +0000773 for (Attribute::AttrKind Kind = Attribute::None;
Benjamin Kramer6f37dac2013-02-16 19:22:28 +0000774 Kind != Attribute::EndAttrKinds; Kind = Attribute::AttrKind(Kind + 1)) {
Benjamin Kramer45e7d532013-02-16 19:13:18 +0000775 if (!B.contains(Kind))
776 continue;
777
George Burgess IV500d3032015-12-16 05:21:02 +0000778 Attribute Attr;
779 switch (Kind) {
780 case Attribute::Alignment:
781 Attr = Attribute::getWithAlignment(C, B.getAlignment());
782 break;
783 case Attribute::StackAlignment:
784 Attr = Attribute::getWithStackAlignment(C, B.getStackAlignment());
785 break;
786 case Attribute::Dereferenceable:
787 Attr = Attribute::getWithDereferenceableBytes(
788 C, B.getDereferenceableBytes());
789 break;
790 case Attribute::DereferenceableOrNull:
791 Attr = Attribute::getWithDereferenceableOrNullBytes(
792 C, B.getDereferenceableOrNullBytes());
793 break;
George Burgess IV278199f2016-04-12 01:05:35 +0000794 case Attribute::AllocSize: {
795 auto A = B.getAllocSizeArgs();
796 Attr = Attribute::getWithAllocSizeArgs(C, A.first, A.second);
797 break;
798 }
George Burgess IV500d3032015-12-16 05:21:02 +0000799 default:
800 Attr = Attribute::get(C, Kind);
801 }
David Majnemer0a16c222016-08-11 21:15:00 +0000802 Attrs.emplace_back(Index, Attr);
Bill Wendlingf7134812013-01-29 01:02:03 +0000803 }
804
Bill Wendlingae89a0f2013-02-05 23:48:36 +0000805 // Add target-dependent (string) attributes.
Benjamin Kramerea76b6f2016-06-12 19:02:34 +0000806 for (const auto &TDA : B.td_attrs())
David Majnemer0a16c222016-08-11 21:15:00 +0000807 Attrs.emplace_back(Index, Attribute::get(C, TDA.first, TDA.second));
Bill Wendlingae89a0f2013-02-05 23:48:36 +0000808
Bill Wendlingf7134812013-01-29 01:02:03 +0000809 return get(C, Attrs);
Bill Wendlingd509a662013-01-29 00:34:06 +0000810}
811
Bill Wendling211316c2013-04-18 20:17:28 +0000812AttributeSet AttributeSet::get(LLVMContext &C, unsigned Index,
Amaury Sechet392638d2016-06-14 20:27:35 +0000813 ArrayRef<Attribute::AttrKind> Kinds) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000814 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
Amaury Sechet392638d2016-06-14 20:27:35 +0000815 for (Attribute::AttrKind K : Kinds)
David Majnemer0a16c222016-08-11 21:15:00 +0000816 Attrs.emplace_back(Index, Attribute::get(C, K));
Bill Wendlingd509a662013-01-29 00:34:06 +0000817 return get(C, Attrs);
818}
819
Amaury Sechet6100adf2016-06-15 17:50:39 +0000820AttributeSet AttributeSet::get(LLVMContext &C, unsigned Index,
821 ArrayRef<StringRef> Kinds) {
822 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
823 for (StringRef K : Kinds)
David Majnemer0a16c222016-08-11 21:15:00 +0000824 Attrs.emplace_back(Index, Attribute::get(C, K));
Amaury Sechet6100adf2016-06-15 17:50:39 +0000825 return get(C, Attrs);
826}
827
Bill Wendlingd509a662013-01-29 00:34:06 +0000828AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<AttributeSet> Attrs) {
829 if (Attrs.empty()) return AttributeSet();
Peter Collingbournebd6c7452013-08-02 22:29:40 +0000830 if (Attrs.size() == 1) return Attrs[0];
Bill Wendlingd509a662013-01-29 00:34:06 +0000831
832 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrNodeVec;
Peter Collingbournebd6c7452013-08-02 22:29:40 +0000833 AttributeSetImpl *A0 = Attrs[0].pImpl;
834 if (A0)
Amaury Sechet24c84fd2016-06-14 22:04:16 +0000835 AttrNodeVec.append(A0->getNode(0), A0->getNode(A0->getNumSlots()));
Peter Collingbournebd6c7452013-08-02 22:29:40 +0000836 // Copy all attributes from Attrs into AttrNodeVec while keeping AttrNodeVec
837 // ordered by index. Because we know that each list in Attrs is ordered by
838 // index we only need to merge each successive list in rather than doing a
839 // full sort.
840 for (unsigned I = 1, E = Attrs.size(); I != E; ++I) {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000841 AttributeSetImpl *AS = Attrs[I].pImpl;
842 if (!AS) continue;
Peter Collingbournebd6c7452013-08-02 22:29:40 +0000843 SmallVector<std::pair<unsigned, AttributeSetNode *>, 8>::iterator
844 ANVI = AttrNodeVec.begin(), ANVE;
James Y Knightaa365b22015-08-05 22:57:34 +0000845 for (const IndexAttrPair *AI = AS->getNode(0),
Amaury Sechet24c84fd2016-06-14 22:04:16 +0000846 *AE = AS->getNode(AS->getNumSlots());
Peter Collingbournebd6c7452013-08-02 22:29:40 +0000847 AI != AE; ++AI) {
848 ANVE = AttrNodeVec.end();
849 while (ANVI != ANVE && ANVI->first <= AI->first)
850 ++ANVI;
851 ANVI = AttrNodeVec.insert(ANVI, *AI) + 1;
852 }
Bill Wendlingd509a662013-01-29 00:34:06 +0000853 }
854
855 return getImpl(C, AttrNodeVec);
856}
857
Bill Wendling211316c2013-04-18 20:17:28 +0000858AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
Amaury Sechet392638d2016-06-14 20:27:35 +0000859 Attribute::AttrKind Kind) const {
860 if (hasAttribute(Index, Kind)) return *this;
861 return addAttributes(C, Index, AttributeSet::get(C, Index, Kind));
Reed Kotler795c7b42013-03-13 20:20:08 +0000862}
863
Bill Wendling3b2f6102013-07-25 18:34:24 +0000864AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
865 StringRef Kind, StringRef Value) const {
866 llvm::AttrBuilder B;
867 B.addAttribute(Kind, Value);
868 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
869}
870
Akira Hatanaka237916b2015-12-02 06:58:49 +0000871AttributeSet AttributeSet::addAttribute(LLVMContext &C,
872 ArrayRef<unsigned> Indices,
873 Attribute A) const {
Amaury Sechet24c84fd2016-06-14 22:04:16 +0000874 unsigned I = 0, E = pImpl ? pImpl->getNumSlots() : 0;
Akira Hatanaka237916b2015-12-02 06:58:49 +0000875 auto IdxI = Indices.begin(), IdxE = Indices.end();
876 SmallVector<AttributeSet, 4> AttrSet;
877
878 while (I != E && IdxI != IdxE) {
879 if (getSlotIndex(I) < *IdxI)
880 AttrSet.emplace_back(getSlotAttributes(I++));
881 else if (getSlotIndex(I) > *IdxI)
882 AttrSet.emplace_back(AttributeSet::get(C, std::make_pair(*IdxI++, A)));
883 else {
884 AttrBuilder B(getSlotAttributes(I), *IdxI);
885 B.addAttribute(A);
886 AttrSet.emplace_back(AttributeSet::get(C, *IdxI, B));
887 ++I;
888 ++IdxI;
889 }
890 }
891
892 while (I != E)
893 AttrSet.emplace_back(getSlotAttributes(I++));
894
895 while (IdxI != IdxE)
896 AttrSet.emplace_back(AttributeSet::get(C, std::make_pair(*IdxI++, A)));
897
898 return get(C, AttrSet);
899}
900
Bill Wendling211316c2013-04-18 20:17:28 +0000901AttributeSet AttributeSet::addAttributes(LLVMContext &C, unsigned Index,
Bill Wendlingd509a662013-01-29 00:34:06 +0000902 AttributeSet Attrs) const {
903 if (!pImpl) return Attrs;
904 if (!Attrs.pImpl) return *this;
905
906#ifndef NDEBUG
907 // FIXME it is not obvious how this should work for alignment. For now, say
908 // we can't change a known alignment.
Bill Wendling211316c2013-04-18 20:17:28 +0000909 unsigned OldAlign = getParamAlignment(Index);
910 unsigned NewAlign = Attrs.getParamAlignment(Index);
Bill Wendlingd509a662013-01-29 00:34:06 +0000911 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
912 "Attempt to change alignment!");
913#endif
914
915 // Add the attribute slots before the one we're trying to add.
916 SmallVector<AttributeSet, 4> AttrSet;
Amaury Sechet24c84fd2016-06-14 22:04:16 +0000917 uint64_t NumAttrs = pImpl->getNumSlots();
Bill Wendlingd509a662013-01-29 00:34:06 +0000918 AttributeSet AS;
919 uint64_t LastIndex = 0;
920 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
Bill Wendling211316c2013-04-18 20:17:28 +0000921 if (getSlotIndex(I) >= Index) {
922 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
Bill Wendlingd509a662013-01-29 00:34:06 +0000923 break;
924 }
925 LastIndex = I + 1;
926 AttrSet.push_back(getSlotAttributes(I));
927 }
928
929 // Now add the attribute into the correct slot. There may already be an
930 // AttributeSet there.
Bill Wendling211316c2013-04-18 20:17:28 +0000931 AttrBuilder B(AS, Index);
Bill Wendlingd509a662013-01-29 00:34:06 +0000932
Amaury Sechet24c84fd2016-06-14 22:04:16 +0000933 for (unsigned I = 0, E = Attrs.pImpl->getNumSlots(); I != E; ++I)
Bill Wendling211316c2013-04-18 20:17:28 +0000934 if (Attrs.getSlotIndex(I) == Index) {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000935 for (AttributeSetImpl::iterator II = Attrs.pImpl->begin(I),
Bill Wendlingd509a662013-01-29 00:34:06 +0000936 IE = Attrs.pImpl->end(I); II != IE; ++II)
Bill Wendling23804da2013-01-31 23:38:01 +0000937 B.addAttribute(*II);
Bill Wendlingd509a662013-01-29 00:34:06 +0000938 break;
939 }
940
Bill Wendling211316c2013-04-18 20:17:28 +0000941 AttrSet.push_back(AttributeSet::get(C, Index, B));
Bill Wendlingd509a662013-01-29 00:34:06 +0000942
943 // Add the remaining attribute slots.
944 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
945 AttrSet.push_back(getSlotAttributes(I));
946
947 return get(C, AttrSet);
948}
949
Bill Wendling211316c2013-04-18 20:17:28 +0000950AttributeSet AttributeSet::removeAttribute(LLVMContext &C, unsigned Index,
Amaury Sechet392638d2016-06-14 20:27:35 +0000951 Attribute::AttrKind Kind) const {
952 if (!hasAttribute(Index, Kind)) return *this;
953 return removeAttributes(C, Index, AttributeSet::get(C, Index, Kind));
Bill Wendlingd509a662013-01-29 00:34:06 +0000954}
955
Amaury Sechet6100adf2016-06-15 17:50:39 +0000956AttributeSet AttributeSet::removeAttribute(LLVMContext &C, unsigned Index,
957 StringRef Kind) const {
958 if (!hasAttribute(Index, Kind)) return *this;
959 return removeAttributes(C, Index, AttributeSet::get(C, Index, Kind));
960}
961
Bill Wendling211316c2013-04-18 20:17:28 +0000962AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Index,
Bill Wendlingd509a662013-01-29 00:34:06 +0000963 AttributeSet Attrs) const {
964 if (!pImpl) return AttributeSet();
965 if (!Attrs.pImpl) return *this;
966
Pete Cooper67cf9a72015-11-19 05:56:52 +0000967 // FIXME it is not obvious how this should work for alignment.
968 // For now, say we can't pass in alignment, which no current use does.
969 assert(!Attrs.hasAttribute(Index, Attribute::Alignment) &&
970 "Attempt to change alignment!");
971
Bill Wendlingd509a662013-01-29 00:34:06 +0000972 // Add the attribute slots before the one we're trying to add.
973 SmallVector<AttributeSet, 4> AttrSet;
Amaury Sechet24c84fd2016-06-14 22:04:16 +0000974 uint64_t NumAttrs = pImpl->getNumSlots();
Bill Wendlingd509a662013-01-29 00:34:06 +0000975 AttributeSet AS;
976 uint64_t LastIndex = 0;
977 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
Bill Wendling211316c2013-04-18 20:17:28 +0000978 if (getSlotIndex(I) >= Index) {
979 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
Bill Wendlingd509a662013-01-29 00:34:06 +0000980 break;
981 }
982 LastIndex = I + 1;
983 AttrSet.push_back(getSlotAttributes(I));
984 }
985
Bill Wendlingd2196752013-01-30 23:07:40 +0000986 // Now remove the attribute from the correct slot. There may already be an
Bill Wendlingd509a662013-01-29 00:34:06 +0000987 // AttributeSet there.
Bill Wendling211316c2013-04-18 20:17:28 +0000988 AttrBuilder B(AS, Index);
Bill Wendlingd509a662013-01-29 00:34:06 +0000989
Amaury Sechet24c84fd2016-06-14 22:04:16 +0000990 for (unsigned I = 0, E = Attrs.pImpl->getNumSlots(); I != E; ++I)
Bill Wendling211316c2013-04-18 20:17:28 +0000991 if (Attrs.getSlotIndex(I) == Index) {
992 B.removeAttributes(Attrs.pImpl->getSlotAttributes(I), Index);
Bill Wendlingd509a662013-01-29 00:34:06 +0000993 break;
994 }
995
Bill Wendling211316c2013-04-18 20:17:28 +0000996 AttrSet.push_back(AttributeSet::get(C, Index, B));
Bill Wendlingd509a662013-01-29 00:34:06 +0000997
998 // Add the remaining attribute slots.
999 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
1000 AttrSet.push_back(getSlotAttributes(I));
1001
1002 return get(C, AttrSet);
1003}
1004
Pete Cooperd2a44612015-05-06 23:19:43 +00001005AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Index,
1006 const AttrBuilder &Attrs) const {
1007 if (!pImpl) return AttributeSet();
1008
1009 // FIXME it is not obvious how this should work for alignment.
1010 // For now, say we can't pass in alignment, which no current use does.
1011 assert(!Attrs.hasAlignmentAttr() && "Attempt to change alignment!");
1012
1013 // Add the attribute slots before the one we're trying to add.
1014 SmallVector<AttributeSet, 4> AttrSet;
Amaury Sechet24c84fd2016-06-14 22:04:16 +00001015 uint64_t NumAttrs = pImpl->getNumSlots();
Pete Cooperd2a44612015-05-06 23:19:43 +00001016 AttributeSet AS;
1017 uint64_t LastIndex = 0;
1018 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
1019 if (getSlotIndex(I) >= Index) {
1020 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
1021 break;
1022 }
1023 LastIndex = I + 1;
1024 AttrSet.push_back(getSlotAttributes(I));
1025 }
1026
1027 // Now remove the attribute from the correct slot. There may already be an
1028 // AttributeSet there.
1029 AttrBuilder B(AS, Index);
1030 B.remove(Attrs);
1031
1032 AttrSet.push_back(AttributeSet::get(C, Index, B));
1033
1034 // Add the remaining attribute slots.
1035 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
1036 AttrSet.push_back(getSlotAttributes(I));
1037
1038 return get(C, AttrSet);
1039}
1040
Ramkumar Ramachandra8fcb4982015-02-14 19:37:54 +00001041AttributeSet AttributeSet::addDereferenceableAttr(LLVMContext &C, unsigned Index,
1042 uint64_t Bytes) const {
1043 llvm::AttrBuilder B;
1044 B.addDereferenceableAttr(Bytes);
1045 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
1046}
1047
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001048AttributeSet AttributeSet::addDereferenceableOrNullAttr(LLVMContext &C,
1049 unsigned Index,
1050 uint64_t Bytes) const {
1051 llvm::AttrBuilder B;
1052 B.addDereferenceableOrNullAttr(Bytes);
1053 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
1054}
1055
George Burgess IV278199f2016-04-12 01:05:35 +00001056AttributeSet
1057AttributeSet::addAllocSizeAttr(LLVMContext &C, unsigned Index,
1058 unsigned ElemSizeArg,
1059 const Optional<unsigned> &NumElemsArg) {
1060 llvm::AttrBuilder B;
1061 B.addAllocSizeAttr(ElemSizeArg, NumElemsArg);
1062 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
1063}
1064
Bill Wendlingd509a662013-01-29 00:34:06 +00001065//===----------------------------------------------------------------------===//
1066// AttributeSet Accessor Methods
1067//===----------------------------------------------------------------------===//
1068
Bill Wendling5d020a32013-02-10 05:00:40 +00001069LLVMContext &AttributeSet::getContext() const {
1070 return pImpl->getContext();
1071}
1072
Bill Wendling211316c2013-04-18 20:17:28 +00001073AttributeSet AttributeSet::getParamAttributes(unsigned Index) const {
1074 return pImpl && hasAttributes(Index) ?
Bill Wendlingd509a662013-01-29 00:34:06 +00001075 AttributeSet::get(pImpl->getContext(),
Bill Wendlingf2955aa2013-01-29 03:20:31 +00001076 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendling211316c2013-04-18 20:17:28 +00001077 std::make_pair(Index, getAttributes(Index)))) :
Bill Wendlingd509a662013-01-29 00:34:06 +00001078 AttributeSet();
1079}
1080
1081AttributeSet AttributeSet::getRetAttributes() const {
1082 return pImpl && hasAttributes(ReturnIndex) ?
1083 AttributeSet::get(pImpl->getContext(),
Bill Wendlingf2955aa2013-01-29 03:20:31 +00001084 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendlingd509a662013-01-29 00:34:06 +00001085 std::make_pair(ReturnIndex,
1086 getAttributes(ReturnIndex)))) :
1087 AttributeSet();
1088}
1089
1090AttributeSet AttributeSet::getFnAttributes() const {
1091 return pImpl && hasAttributes(FunctionIndex) ?
1092 AttributeSet::get(pImpl->getContext(),
Bill Wendlingf2955aa2013-01-29 03:20:31 +00001093 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendlingd509a662013-01-29 00:34:06 +00001094 std::make_pair(FunctionIndex,
1095 getAttributes(FunctionIndex)))) :
1096 AttributeSet();
1097}
1098
1099bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{
Bill Wendlingf2955aa2013-01-29 03:20:31 +00001100 AttributeSetNode *ASN = getAttributes(Index);
George Burgess IV500d3032015-12-16 05:21:02 +00001101 return ASN && ASN->hasAttribute(Kind);
Bill Wendlingd509a662013-01-29 00:34:06 +00001102}
1103
Bill Wendlingbce7b972013-02-13 08:42:21 +00001104bool AttributeSet::hasAttribute(unsigned Index, StringRef Kind) const {
1105 AttributeSetNode *ASN = getAttributes(Index);
George Burgess IV500d3032015-12-16 05:21:02 +00001106 return ASN && ASN->hasAttribute(Kind);
Bill Wendlingbce7b972013-02-13 08:42:21 +00001107}
1108
Bill Wendlingd509a662013-01-29 00:34:06 +00001109bool AttributeSet::hasAttributes(unsigned Index) const {
Bill Wendlingf2955aa2013-01-29 03:20:31 +00001110 AttributeSetNode *ASN = getAttributes(Index);
George Burgess IV500d3032015-12-16 05:21:02 +00001111 return ASN && ASN->hasAttributes();
Bill Wendlingd509a662013-01-29 00:34:06 +00001112}
1113
Matthias Braun33282812016-01-29 22:25:19 +00001114bool AttributeSet::hasFnAttribute(Attribute::AttrKind Kind) const {
1115 return pImpl && pImpl->hasFnAttribute(Kind);
1116}
1117
Hal Finkele87ad542016-07-10 23:01:32 +00001118bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr,
1119 unsigned *Index) const {
Craig Topperc6207612014-04-09 06:08:46 +00001120 if (!pImpl) return false;
Bill Wendlingd509a662013-01-29 00:34:06 +00001121
Amaury Sechet24c84fd2016-06-14 22:04:16 +00001122 for (unsigned I = 0, E = pImpl->getNumSlots(); I != E; ++I)
Benjamin Kramer741146b2013-07-11 12:13:16 +00001123 for (AttributeSetImpl::iterator II = pImpl->begin(I),
Bill Wendlingd509a662013-01-29 00:34:06 +00001124 IE = pImpl->end(I); II != IE; ++II)
Hal Finkele87ad542016-07-10 23:01:32 +00001125 if (II->hasAttribute(Attr)) {
1126 if (Index) *Index = pImpl->getSlotIndex(I);
Bill Wendlingd509a662013-01-29 00:34:06 +00001127 return true;
Hal Finkele87ad542016-07-10 23:01:32 +00001128 }
Bill Wendlingd509a662013-01-29 00:34:06 +00001129
1130 return false;
1131}
1132
Bill Wendlingbce7b972013-02-13 08:42:21 +00001133Attribute AttributeSet::getAttribute(unsigned Index,
1134 Attribute::AttrKind Kind) const {
1135 AttributeSetNode *ASN = getAttributes(Index);
1136 return ASN ? ASN->getAttribute(Kind) : Attribute();
1137}
1138
1139Attribute AttributeSet::getAttribute(unsigned Index,
1140 StringRef Kind) const {
1141 AttributeSetNode *ASN = getAttributes(Index);
1142 return ASN ? ASN->getAttribute(Kind) : Attribute();
1143}
1144
Bill Wendlingf2955aa2013-01-29 03:20:31 +00001145unsigned AttributeSet::getParamAlignment(unsigned Index) const {
1146 AttributeSetNode *ASN = getAttributes(Index);
1147 return ASN ? ASN->getAlignment() : 0;
Bill Wendlingd509a662013-01-29 00:34:06 +00001148}
1149
1150unsigned AttributeSet::getStackAlignment(unsigned Index) const {
Bill Wendlingf2955aa2013-01-29 03:20:31 +00001151 AttributeSetNode *ASN = getAttributes(Index);
1152 return ASN ? ASN->getStackAlignment() : 0;
Bill Wendlingd509a662013-01-29 00:34:06 +00001153}
1154
Hal Finkelb0407ba2014-07-18 15:51:28 +00001155uint64_t AttributeSet::getDereferenceableBytes(unsigned Index) const {
1156 AttributeSetNode *ASN = getAttributes(Index);
1157 return ASN ? ASN->getDereferenceableBytes() : 0;
1158}
1159
Sanjoy Das06cf33f2015-05-06 17:41:54 +00001160uint64_t AttributeSet::getDereferenceableOrNullBytes(unsigned Index) const {
1161 AttributeSetNode *ASN = getAttributes(Index);
1162 return ASN ? ASN->getDereferenceableOrNullBytes() : 0;
1163}
1164
George Burgess IV278199f2016-04-12 01:05:35 +00001165std::pair<unsigned, Optional<unsigned>>
1166AttributeSet::getAllocSizeArgs(unsigned Index) const {
1167 AttributeSetNode *ASN = getAttributes(Index);
Matt Arsenault4ced16d2016-07-18 22:12:46 +00001168 return ASN ? ASN->getAllocSizeArgs() : std::make_pair(0u, Optional<unsigned>(0u));
George Burgess IV278199f2016-04-12 01:05:35 +00001169}
1170
1171std::string AttributeSet::getAsString(unsigned Index, bool InAttrGrp) const {
Bill Wendlingf2955aa2013-01-29 03:20:31 +00001172 AttributeSetNode *ASN = getAttributes(Index);
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +00001173 return ASN ? ASN->getAsString(InAttrGrp) : std::string("");
Bill Wendlingd509a662013-01-29 00:34:06 +00001174}
1175
Bill Wendling211316c2013-04-18 20:17:28 +00001176AttributeSetNode *AttributeSet::getAttributes(unsigned Index) const {
Craig Topperc6207612014-04-09 06:08:46 +00001177 if (!pImpl) return nullptr;
Bill Wendlingd509a662013-01-29 00:34:06 +00001178
Bill Wendlingf2955aa2013-01-29 03:20:31 +00001179 // Loop through to find the attribute node we want.
Amaury Sechet24c84fd2016-06-14 22:04:16 +00001180 for (unsigned I = 0, E = pImpl->getNumSlots(); I != E; ++I)
Bill Wendling211316c2013-04-18 20:17:28 +00001181 if (pImpl->getSlotIndex(I) == Index)
Bill Wendlingf2955aa2013-01-29 03:20:31 +00001182 return pImpl->getSlotNode(I);
Bill Wendlingd509a662013-01-29 00:34:06 +00001183
Craig Topperc6207612014-04-09 06:08:46 +00001184 return nullptr;
Bill Wendlingd509a662013-01-29 00:34:06 +00001185}
1186
Bill Wendling211316c2013-04-18 20:17:28 +00001187AttributeSet::iterator AttributeSet::begin(unsigned Slot) const {
Bill Wendlinga9174862013-01-31 23:53:05 +00001188 if (!pImpl)
1189 return ArrayRef<Attribute>().begin();
Bill Wendling211316c2013-04-18 20:17:28 +00001190 return pImpl->begin(Slot);
Bill Wendlinga9174862013-01-31 23:53:05 +00001191}
1192
Bill Wendling211316c2013-04-18 20:17:28 +00001193AttributeSet::iterator AttributeSet::end(unsigned Slot) const {
Bill Wendlinga9174862013-01-31 23:53:05 +00001194 if (!pImpl)
1195 return ArrayRef<Attribute>().end();
Bill Wendling211316c2013-04-18 20:17:28 +00001196 return pImpl->end(Slot);
Bill Wendlinga9174862013-01-31 23:53:05 +00001197}
1198
Bill Wendlingd509a662013-01-29 00:34:06 +00001199//===----------------------------------------------------------------------===//
1200// AttributeSet Introspection Methods
1201//===----------------------------------------------------------------------===//
1202
Bill Wendlingd509a662013-01-29 00:34:06 +00001203unsigned AttributeSet::getNumSlots() const {
Amaury Sechet24c84fd2016-06-14 22:04:16 +00001204 return pImpl ? pImpl->getNumSlots() : 0;
Bill Wendlingd509a662013-01-29 00:34:06 +00001205}
1206
Rafael Espindoladd275302013-04-30 16:53:38 +00001207unsigned AttributeSet::getSlotIndex(unsigned Slot) const {
Amaury Sechet24c84fd2016-06-14 22:04:16 +00001208 assert(pImpl && Slot < pImpl->getNumSlots() &&
Bill Wendlingd509a662013-01-29 00:34:06 +00001209 "Slot # out of range!");
1210 return pImpl->getSlotIndex(Slot);
1211}
1212
1213AttributeSet AttributeSet::getSlotAttributes(unsigned Slot) const {
Amaury Sechet24c84fd2016-06-14 22:04:16 +00001214 assert(pImpl && Slot < pImpl->getNumSlots() &&
Bill Wendlingd509a662013-01-29 00:34:06 +00001215 "Slot # out of range!");
1216 return pImpl->getSlotAttributes(Slot);
1217}
1218
1219uint64_t AttributeSet::Raw(unsigned Index) const {
1220 // FIXME: Remove this.
1221 return pImpl ? pImpl->Raw(Index) : 0;
1222}
1223
Yaron Kereneb2a2542016-01-29 20:50:44 +00001224LLVM_DUMP_METHOD void AttributeSet::dump() const {
Bill Wendlingd509a662013-01-29 00:34:06 +00001225 dbgs() << "PAL[\n";
1226
1227 for (unsigned i = 0, e = getNumSlots(); i < e; ++i) {
1228 uint64_t Index = getSlotIndex(i);
1229 dbgs() << " { ";
1230 if (Index == ~0U)
1231 dbgs() << "~0U";
1232 else
1233 dbgs() << Index;
1234 dbgs() << " => " << getAsString(Index) << " }\n";
1235 }
1236
1237 dbgs() << "]\n";
1238}
1239
Bill Wendlingabf3feb2012-10-05 06:44:41 +00001240//===----------------------------------------------------------------------===//
Bill Wendlingcd330342013-01-04 23:27:34 +00001241// AttrBuilder Method Implementations
Bill Wendlingabf3feb2012-10-05 06:44:41 +00001242//===----------------------------------------------------------------------===//
1243
Bill Wendling211316c2013-04-18 20:17:28 +00001244AttrBuilder::AttrBuilder(AttributeSet AS, unsigned Index)
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001245 : Attrs(0), Alignment(0), StackAlignment(0), DerefBytes(0),
George Burgess IV278199f2016-04-12 01:05:35 +00001246 DerefOrNullBytes(0), AllocSizeArgs(0) {
Bill Wendling56b0b2a2013-01-27 21:23:46 +00001247 AttributeSetImpl *pImpl = AS.pImpl;
Bill Wendling096f5442013-01-07 08:24:35 +00001248 if (!pImpl) return;
1249
Amaury Sechet24c84fd2016-06-14 22:04:16 +00001250 for (unsigned I = 0, E = pImpl->getNumSlots(); I != E; ++I) {
Bill Wendling211316c2013-04-18 20:17:28 +00001251 if (pImpl->getSlotIndex(I) != Index) continue;
Bill Wendling096f5442013-01-07 08:24:35 +00001252
Benjamin Kramer741146b2013-07-11 12:13:16 +00001253 for (AttributeSetImpl::iterator II = pImpl->begin(I),
Bill Wendling9eb689c2013-01-28 00:21:34 +00001254 IE = pImpl->end(I); II != IE; ++II)
Bill Wendling23804da2013-01-31 23:38:01 +00001255 addAttribute(*II);
Bill Wendling9eb689c2013-01-28 00:21:34 +00001256
1257 break;
1258 }
Bill Wendling096f5442013-01-07 08:24:35 +00001259}
1260
Bill Wendlingcd330342013-01-04 23:27:34 +00001261void AttrBuilder::clear() {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001262 Attrs.reset();
Sanjoy Das88d0fde2015-09-03 22:27:42 +00001263 TargetDepAttrs.clear();
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001264 Alignment = StackAlignment = DerefBytes = DerefOrNullBytes = 0;
George Burgess IV278199f2016-04-12 01:05:35 +00001265 AllocSizeArgs = 0;
Bill Wendlingcd330342013-01-04 23:27:34 +00001266}
1267
1268AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001269 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
Bill Wendling1c7cc8a2013-01-31 23:16:25 +00001270 assert(Val != Attribute::Alignment && Val != Attribute::StackAlignment &&
George Burgess IV278199f2016-04-12 01:05:35 +00001271 Val != Attribute::Dereferenceable && Val != Attribute::AllocSize &&
Hal Finkelb0407ba2014-07-18 15:51:28 +00001272 "Adding integer attribute without adding a value!");
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001273 Attrs[Val] = true;
Bill Wendling7c04e042012-10-09 19:01:18 +00001274 return *this;
Bill Wendlingabf3feb2012-10-05 06:44:41 +00001275}
1276
Bill Wendling23804da2013-01-31 23:38:01 +00001277AttrBuilder &AttrBuilder::addAttribute(Attribute Attr) {
Bill Wendling0a437302013-02-10 10:13:23 +00001278 if (Attr.isStringAttribute()) {
1279 addAttribute(Attr.getKindAsString(), Attr.getValueAsString());
1280 return *this;
1281 }
1282
Bill Wendling3f12ac22013-02-05 22:37:24 +00001283 Attribute::AttrKind Kind = Attr.getKindAsEnum();
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001284 Attrs[Kind] = true;
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001285
Bill Wendling3f12ac22013-02-05 22:37:24 +00001286 if (Kind == Attribute::Alignment)
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001287 Alignment = Attr.getAlignment();
Bill Wendling3f12ac22013-02-05 22:37:24 +00001288 else if (Kind == Attribute::StackAlignment)
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001289 StackAlignment = Attr.getStackAlignment();
Hal Finkelb0407ba2014-07-18 15:51:28 +00001290 else if (Kind == Attribute::Dereferenceable)
1291 DerefBytes = Attr.getDereferenceableBytes();
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001292 else if (Kind == Attribute::DereferenceableOrNull)
1293 DerefOrNullBytes = Attr.getDereferenceableOrNullBytes();
George Burgess IV278199f2016-04-12 01:05:35 +00001294 else if (Kind == Attribute::AllocSize)
1295 AllocSizeArgs = Attr.getValueAsInt();
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001296 return *this;
1297}
1298
Bill Wendlingb9c5b1a2013-02-05 08:09:32 +00001299AttrBuilder &AttrBuilder::addAttribute(StringRef A, StringRef V) {
1300 TargetDepAttrs[A] = V;
1301 return *this;
1302}
1303
Bill Wendling23804da2013-01-31 23:38:01 +00001304AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001305 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
1306 Attrs[Val] = false;
Bill Wendling23804da2013-01-31 23:38:01 +00001307
1308 if (Val == Attribute::Alignment)
1309 Alignment = 0;
1310 else if (Val == Attribute::StackAlignment)
1311 StackAlignment = 0;
Hal Finkelb0407ba2014-07-18 15:51:28 +00001312 else if (Val == Attribute::Dereferenceable)
1313 DerefBytes = 0;
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001314 else if (Val == Attribute::DereferenceableOrNull)
1315 DerefOrNullBytes = 0;
George Burgess IV278199f2016-04-12 01:05:35 +00001316 else if (Val == Attribute::AllocSize)
1317 AllocSizeArgs = 0;
Bill Wendling23804da2013-01-31 23:38:01 +00001318
1319 return *this;
1320}
1321
Bill Wendlingd2196752013-01-30 23:07:40 +00001322AttrBuilder &AttrBuilder::removeAttributes(AttributeSet A, uint64_t Index) {
Bill Wendling211316c2013-04-18 20:17:28 +00001323 unsigned Slot = ~0U;
Bill Wendlingf1c94e32013-02-01 00:13:50 +00001324 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
1325 if (A.getSlotIndex(I) == Index) {
Bill Wendling211316c2013-04-18 20:17:28 +00001326 Slot = I;
Bill Wendlingf1c94e32013-02-01 00:13:50 +00001327 break;
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001328 }
Bill Wendlingf1c94e32013-02-01 00:13:50 +00001329
Bill Wendling211316c2013-04-18 20:17:28 +00001330 assert(Slot != ~0U && "Couldn't find index in AttributeSet!");
Bill Wendlingf1c94e32013-02-01 00:13:50 +00001331
Bill Wendling211316c2013-04-18 20:17:28 +00001332 for (AttributeSet::iterator I = A.begin(Slot), E = A.end(Slot); I != E; ++I) {
Bill Wendling7cde51d2013-02-12 07:56:49 +00001333 Attribute Attr = *I;
Hal Finkele15442c2014-07-18 06:51:55 +00001334 if (Attr.isEnumAttribute() || Attr.isIntAttribute()) {
George Burgess IV500d3032015-12-16 05:21:02 +00001335 removeAttribute(Attr.getKindAsEnum());
Bill Wendling7cde51d2013-02-12 07:56:49 +00001336 } else {
1337 assert(Attr.isStringAttribute() && "Invalid attribute type!");
George Burgess IV500d3032015-12-16 05:21:02 +00001338 removeAttribute(Attr.getKindAsString());
Bill Wendling7cde51d2013-02-12 07:56:49 +00001339 }
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001340 }
1341
1342 return *this;
1343}
1344
Bill Wendlingb9c5b1a2013-02-05 08:09:32 +00001345AttrBuilder &AttrBuilder::removeAttribute(StringRef A) {
1346 std::map<std::string, std::string>::iterator I = TargetDepAttrs.find(A);
1347 if (I != TargetDepAttrs.end())
1348 TargetDepAttrs.erase(I);
1349 return *this;
1350}
1351
George Burgess IV278199f2016-04-12 01:05:35 +00001352std::pair<unsigned, Optional<unsigned>> AttrBuilder::getAllocSizeArgs() const {
1353 return unpackAllocSizeArgs(AllocSizeArgs);
1354}
1355
Bill Wendling50d27842012-10-15 20:35:56 +00001356AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
Bill Wendlingabd5ba22012-10-14 03:58:29 +00001357 if (Align == 0) return *this;
Bill Wendlingcd330342013-01-04 23:27:34 +00001358
Bill Wendlingabf3feb2012-10-05 06:44:41 +00001359 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1360 assert(Align <= 0x40000000 && "Alignment too large.");
Bill Wendlingcd330342013-01-04 23:27:34 +00001361
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001362 Attrs[Attribute::Alignment] = true;
Bill Wendlingcd330342013-01-04 23:27:34 +00001363 Alignment = Align;
Bill Wendlingabd5ba22012-10-14 03:58:29 +00001364 return *this;
Chris Lattner3e13b8c2008-01-02 23:42:30 +00001365}
1366
Bill Wendlingcd330342013-01-04 23:27:34 +00001367AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
1368 // Default alignment, allow the target to define how to align it.
1369 if (Align == 0) return *this;
1370
1371 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1372 assert(Align <= 0x100 && "Alignment too large.");
1373
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001374 Attrs[Attribute::StackAlignment] = true;
Bill Wendlingcd330342013-01-04 23:27:34 +00001375 StackAlignment = Align;
1376 return *this;
1377}
1378
Hal Finkelb0407ba2014-07-18 15:51:28 +00001379AttrBuilder &AttrBuilder::addDereferenceableAttr(uint64_t Bytes) {
1380 if (Bytes == 0) return *this;
1381
1382 Attrs[Attribute::Dereferenceable] = true;
1383 DerefBytes = Bytes;
1384 return *this;
1385}
1386
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001387AttrBuilder &AttrBuilder::addDereferenceableOrNullAttr(uint64_t Bytes) {
1388 if (Bytes == 0)
1389 return *this;
1390
1391 Attrs[Attribute::DereferenceableOrNull] = true;
1392 DerefOrNullBytes = Bytes;
1393 return *this;
1394}
1395
George Burgess IV278199f2016-04-12 01:05:35 +00001396AttrBuilder &AttrBuilder::addAllocSizeAttr(unsigned ElemSize,
1397 const Optional<unsigned> &NumElems) {
1398 return addAllocSizeAttrFromRawRepr(packAllocSizeArgs(ElemSize, NumElems));
1399}
1400
1401AttrBuilder &AttrBuilder::addAllocSizeAttrFromRawRepr(uint64_t RawArgs) {
1402 // (0, 0) is our "not present" value, so we need to check for it here.
1403 assert(RawArgs && "Invalid allocsize arguments -- given allocsize(0, 0)");
1404
1405 Attrs[Attribute::AllocSize] = true;
1406 // Reuse existing machinery to store this as a single 64-bit integer so we can
1407 // save a few bytes over using a pair<unsigned, Optional<unsigned>>.
1408 AllocSizeArgs = RawArgs;
1409 return *this;
1410}
1411
Bill Wendlinge2614922013-02-06 01:16:00 +00001412AttrBuilder &AttrBuilder::merge(const AttrBuilder &B) {
1413 // FIXME: What if both have alignments, but they don't match?!
1414 if (!Alignment)
1415 Alignment = B.Alignment;
1416
1417 if (!StackAlignment)
1418 StackAlignment = B.StackAlignment;
1419
Hal Finkelb0407ba2014-07-18 15:51:28 +00001420 if (!DerefBytes)
1421 DerefBytes = B.DerefBytes;
1422
Pete Cooperd2a44612015-05-06 23:19:43 +00001423 if (!DerefOrNullBytes)
1424 DerefOrNullBytes = B.DerefOrNullBytes;
1425
George Burgess IV278199f2016-04-12 01:05:35 +00001426 if (!AllocSizeArgs)
1427 AllocSizeArgs = B.AllocSizeArgs;
1428
Benjamin Kramer45e7d532013-02-16 19:13:18 +00001429 Attrs |= B.Attrs;
Bill Wendlinge2614922013-02-06 01:16:00 +00001430
Pete Cooperd2a44612015-05-06 23:19:43 +00001431 for (auto I : B.td_attrs())
1432 TargetDepAttrs[I.first] = I.second;
Bill Wendlinge2614922013-02-06 01:16:00 +00001433
1434 return *this;
1435}
1436
Pete Cooperd2a44612015-05-06 23:19:43 +00001437AttrBuilder &AttrBuilder::remove(const AttrBuilder &B) {
1438 // FIXME: What if both have alignments, but they don't match?!
1439 if (B.Alignment)
1440 Alignment = 0;
1441
1442 if (B.StackAlignment)
1443 StackAlignment = 0;
1444
1445 if (B.DerefBytes)
1446 DerefBytes = 0;
1447
1448 if (B.DerefOrNullBytes)
1449 DerefOrNullBytes = 0;
1450
George Burgess IV278199f2016-04-12 01:05:35 +00001451 if (B.AllocSizeArgs)
1452 AllocSizeArgs = 0;
1453
Pete Cooperd2a44612015-05-06 23:19:43 +00001454 Attrs &= ~B.Attrs;
1455
1456 for (auto I : B.td_attrs())
1457 TargetDepAttrs.erase(I.first);
1458
1459 return *this;
1460}
1461
1462bool AttrBuilder::overlaps(const AttrBuilder &B) const {
1463 // First check if any of the target independent attributes overlap.
1464 if ((Attrs & B.Attrs).any())
1465 return true;
1466
1467 // Then check if any target dependent ones do.
1468 for (auto I : td_attrs())
1469 if (B.contains(I.first))
1470 return true;
1471
1472 return false;
1473}
1474
Bill Wendling4b001442013-02-06 01:33:42 +00001475bool AttrBuilder::contains(StringRef A) const {
1476 return TargetDepAttrs.find(A) != TargetDepAttrs.end();
1477}
1478
Bill Wendling50d27842012-10-15 20:35:56 +00001479bool AttrBuilder::hasAttributes() const {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001480 return !Attrs.none() || !TargetDepAttrs.empty();
Bill Wendlingc6daefa2012-10-08 23:27:46 +00001481}
Bill Wendling9ac69f92013-01-04 20:54:35 +00001482
Bill Wendlingd2196752013-01-30 23:07:40 +00001483bool AttrBuilder::hasAttributes(AttributeSet A, uint64_t Index) const {
Bill Wendling211316c2013-04-18 20:17:28 +00001484 unsigned Slot = ~0U;
Bill Wendling9ca01da2013-02-02 00:42:06 +00001485 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
1486 if (A.getSlotIndex(I) == Index) {
Bill Wendling211316c2013-04-18 20:17:28 +00001487 Slot = I;
Bill Wendling9ca01da2013-02-02 00:42:06 +00001488 break;
1489 }
1490
Bill Wendling211316c2013-04-18 20:17:28 +00001491 assert(Slot != ~0U && "Couldn't find the index!");
Bill Wendling9ca01da2013-02-02 00:42:06 +00001492
George Burgess IV500d3032015-12-16 05:21:02 +00001493 for (AttributeSet::iterator I = A.begin(Slot), E = A.end(Slot); I != E; ++I) {
Bill Wendling7cde51d2013-02-12 07:56:49 +00001494 Attribute Attr = *I;
Hal Finkele15442c2014-07-18 06:51:55 +00001495 if (Attr.isEnumAttribute() || Attr.isIntAttribute()) {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001496 if (Attrs[I->getKindAsEnum()])
Bill Wendling7cde51d2013-02-12 07:56:49 +00001497 return true;
1498 } else {
1499 assert(Attr.isStringAttribute() && "Invalid attribute kind!");
1500 return TargetDepAttrs.find(Attr.getKindAsString())!=TargetDepAttrs.end();
1501 }
1502 }
Bill Wendling9ca01da2013-02-02 00:42:06 +00001503
1504 return false;
Bill Wendling70f39172012-10-09 00:01:21 +00001505}
Bill Wendling9ac69f92013-01-04 20:54:35 +00001506
Bill Wendling50d27842012-10-15 20:35:56 +00001507bool AttrBuilder::hasAlignmentAttr() const {
Bill Wendlingcd330342013-01-04 23:27:34 +00001508 return Alignment != 0;
Bill Wendlingc6daefa2012-10-08 23:27:46 +00001509}
1510
Bill Wendlingd509a662013-01-29 00:34:06 +00001511bool AttrBuilder::operator==(const AttrBuilder &B) {
Benjamin Kramer45e7d532013-02-16 19:13:18 +00001512 if (Attrs != B.Attrs)
1513 return false;
Bill Wendling4b001442013-02-06 01:33:42 +00001514
1515 for (td_const_iterator I = TargetDepAttrs.begin(),
1516 E = TargetDepAttrs.end(); I != E; ++I)
1517 if (B.TargetDepAttrs.find(I->first) == B.TargetDepAttrs.end())
1518 return false;
1519
Hal Finkelb0407ba2014-07-18 15:51:28 +00001520 return Alignment == B.Alignment && StackAlignment == B.StackAlignment &&
1521 DerefBytes == B.DerefBytes;
Bill Wendlingd509a662013-01-29 00:34:06 +00001522}
1523
1524AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) {
Bill Wendling56aeccc2013-02-04 23:32:23 +00001525 // FIXME: Remove this in 4.0.
Bill Wendling60011b82013-01-29 01:43:29 +00001526 if (!Val) return *this;
1527
Bill Wendlingd509a662013-01-29 00:34:06 +00001528 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
1529 I = Attribute::AttrKind(I + 1)) {
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001530 if (I == Attribute::Dereferenceable ||
Igor Laevsky39d662f2015-07-11 10:30:36 +00001531 I == Attribute::DereferenceableOrNull ||
George Burgess IV278199f2016-04-12 01:05:35 +00001532 I == Attribute::ArgMemOnly ||
1533 I == Attribute::AllocSize)
Hal Finkelb0407ba2014-07-18 15:51:28 +00001534 continue;
Bill Wendlingd509a662013-01-29 00:34:06 +00001535 if (uint64_t A = (Val & AttributeImpl::getAttrMask(I))) {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001536 Attrs[I] = true;
Bill Wendlingd509a662013-01-29 00:34:06 +00001537
1538 if (I == Attribute::Alignment)
1539 Alignment = 1ULL << ((A >> 16) - 1);
1540 else if (I == Attribute::StackAlignment)
1541 StackAlignment = 1ULL << ((A >> 26)-1);
1542 }
1543 }
1544
1545 return *this;
1546}
1547
Bill Wendling57625a42013-01-25 23:09:36 +00001548//===----------------------------------------------------------------------===//
1549// AttributeFuncs Function Defintions
1550//===----------------------------------------------------------------------===//
1551
Bill Wendlingc79cdff2013-02-01 01:04:27 +00001552/// \brief Which attributes cannot be applied to a type.
Craig Toppere3dcce92015-08-01 22:20:21 +00001553AttrBuilder AttributeFuncs::typeIncompatible(Type *Ty) {
Bill Wendling57625a42013-01-25 23:09:36 +00001554 AttrBuilder Incompatible;
1555
1556 if (!Ty->isIntegerTy())
1557 // Attribute that only apply to integers.
1558 Incompatible.addAttribute(Attribute::SExt)
1559 .addAttribute(Attribute::ZExt);
1560
1561 if (!Ty->isPointerTy())
1562 // Attribute that only apply to pointers.
1563 Incompatible.addAttribute(Attribute::ByVal)
1564 .addAttribute(Attribute::Nest)
1565 .addAttribute(Attribute::NoAlias)
1566 .addAttribute(Attribute::NoCapture)
Nick Lewyckyd52b1522014-05-20 01:23:40 +00001567 .addAttribute(Attribute::NonNull)
Hal Finkelb0407ba2014-07-18 15:51:28 +00001568 .addDereferenceableAttr(1) // the int here is ignored
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001569 .addDereferenceableOrNullAttr(1) // the int here is ignored
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001570 .addAttribute(Attribute::ReadNone)
1571 .addAttribute(Attribute::ReadOnly)
Reid Klecknera534a382013-12-19 02:14:12 +00001572 .addAttribute(Attribute::StructRet)
1573 .addAttribute(Attribute::InAlloca);
Bill Wendling57625a42013-01-25 23:09:36 +00001574
Pete Cooper2777d8872015-05-06 23:19:56 +00001575 return Incompatible;
Bill Wendling57625a42013-01-25 23:09:36 +00001576}
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001577
1578template<typename AttrClass>
1579static bool isEqual(const Function &Caller, const Function &Callee) {
1580 return Caller.getFnAttribute(AttrClass::getKind()) ==
1581 Callee.getFnAttribute(AttrClass::getKind());
1582}
1583
1584/// \brief Compute the logical AND of the attributes of the caller and the
1585/// callee.
1586///
1587/// This function sets the caller's attribute to false if the callee's attribute
1588/// is false.
1589template<typename AttrClass>
1590static void setAND(Function &Caller, const Function &Callee) {
1591 if (AttrClass::isSet(Caller, AttrClass::getKind()) &&
1592 !AttrClass::isSet(Callee, AttrClass::getKind()))
1593 AttrClass::set(Caller, AttrClass::getKind(), false);
1594}
1595
1596/// \brief Compute the logical OR of the attributes of the caller and the
1597/// callee.
1598///
1599/// This function sets the caller's attribute to true if the callee's attribute
1600/// is true.
1601template<typename AttrClass>
1602static void setOR(Function &Caller, const Function &Callee) {
1603 if (!AttrClass::isSet(Caller, AttrClass::getKind()) &&
1604 AttrClass::isSet(Callee, AttrClass::getKind()))
1605 AttrClass::set(Caller, AttrClass::getKind(), true);
1606}
1607
1608/// \brief If the inlined function had a higher stack protection level than the
1609/// calling function, then bump up the caller's stack protection level.
1610static void adjustCallerSSPLevel(Function &Caller, const Function &Callee) {
1611 // If upgrading the SSP attribute, clear out the old SSP Attributes first.
1612 // Having multiple SSP attributes doesn't actually hurt, but it adds useless
1613 // clutter to the IR.
1614 AttrBuilder B;
1615 B.addAttribute(Attribute::StackProtect)
1616 .addAttribute(Attribute::StackProtectStrong)
1617 .addAttribute(Attribute::StackProtectReq);
1618 AttributeSet OldSSPAttr = AttributeSet::get(Caller.getContext(),
1619 AttributeSet::FunctionIndex,
1620 B);
1621
Evgeniy Stepanovf17120a2016-04-11 22:27:48 +00001622 if (Callee.hasFnAttribute(Attribute::StackProtectReq)) {
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001623 Caller.removeAttributes(AttributeSet::FunctionIndex, OldSSPAttr);
1624 Caller.addFnAttr(Attribute::StackProtectReq);
1625 } else if (Callee.hasFnAttribute(Attribute::StackProtectStrong) &&
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001626 !Caller.hasFnAttribute(Attribute::StackProtectReq)) {
1627 Caller.removeAttributes(AttributeSet::FunctionIndex, OldSSPAttr);
1628 Caller.addFnAttr(Attribute::StackProtectStrong);
1629 } else if (Callee.hasFnAttribute(Attribute::StackProtect) &&
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001630 !Caller.hasFnAttribute(Attribute::StackProtectReq) &&
1631 !Caller.hasFnAttribute(Attribute::StackProtectStrong))
1632 Caller.addFnAttr(Attribute::StackProtect);
1633}
1634
1635#define GET_ATTR_COMPAT_FUNC
1636#include "AttributesCompatFunc.inc"
1637
1638bool AttributeFuncs::areInlineCompatible(const Function &Caller,
1639 const Function &Callee) {
1640 return hasCompatibleFnAttrs(Caller, Callee);
1641}
1642
1643
1644void AttributeFuncs::mergeAttributesForInlining(Function &Caller,
1645 const Function &Callee) {
1646 mergeFnAttrs(Caller, Callee);
1647}