blob: 2c1bc2b66aa615f3e4aa4c3a3a67e9b38a84b5e9 [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 +0000475//===----------------------------------------------------------------------===//
476// AttributeSetNode Definition
477//===----------------------------------------------------------------------===//
478
479AttributeSetNode *AttributeSetNode::get(LLVMContext &C,
480 ArrayRef<Attribute> Attrs) {
481 if (Attrs.empty())
Craig Topperc6207612014-04-09 06:08:46 +0000482 return nullptr;
Bill Wendlingd509a662013-01-29 00:34:06 +0000483
484 // Otherwise, build a key to look up the existing attributes.
485 LLVMContextImpl *pImpl = C.pImpl;
486 FoldingSetNodeID ID;
487
488 SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end());
Reid Kleckner7de67612016-04-04 23:06:05 +0000489 std::sort(SortedAttrs.begin(), SortedAttrs.end());
Bill Wendlingd509a662013-01-29 00:34:06 +0000490
George Burgess IV500d3032015-12-16 05:21:02 +0000491 for (Attribute Attr : SortedAttrs)
492 Attr.Profile(ID);
Bill Wendlingd509a662013-01-29 00:34:06 +0000493
494 void *InsertPoint;
495 AttributeSetNode *PA =
496 pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint);
497
498 // If we didn't find any existing attributes of the same shape then create a
499 // new one and insert it.
500 if (!PA) {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000501 // Coallocate entries after the AttributeSetNode itself.
James Y Knightaa365b22015-08-05 22:57:34 +0000502 void *Mem = ::operator new(totalSizeToAlloc<Attribute>(SortedAttrs.size()));
Benjamin Kramer741146b2013-07-11 12:13:16 +0000503 PA = new (Mem) AttributeSetNode(SortedAttrs);
Bill Wendlingd509a662013-01-29 00:34:06 +0000504 pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint);
505 }
506
507 // Return the AttributesListNode that we found or created.
508 return PA;
509}
510
Bill Wendlingbce7b972013-02-13 08:42:21 +0000511bool AttributeSetNode::hasAttribute(StringRef Kind) const {
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000512 for (Attribute I : *this)
513 if (I.hasAttribute(Kind))
Bill Wendlingbce7b972013-02-13 08:42:21 +0000514 return true;
515 return false;
516}
517
518Attribute AttributeSetNode::getAttribute(Attribute::AttrKind Kind) const {
Matthias Braun31eeb76f2016-01-29 22:25:13 +0000519 if (hasAttribute(Kind)) {
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000520 for (Attribute I : *this)
521 if (I.hasAttribute(Kind))
522 return I;
Matthias Braun31eeb76f2016-01-29 22:25:13 +0000523 }
Bill Wendlingbce7b972013-02-13 08:42:21 +0000524 return Attribute();
525}
526
527Attribute AttributeSetNode::getAttribute(StringRef Kind) const {
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000528 for (Attribute I : *this)
529 if (I.hasAttribute(Kind))
530 return I;
Bill Wendlingbce7b972013-02-13 08:42:21 +0000531 return Attribute();
532}
533
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000534unsigned AttributeSetNode::getAlignment() const {
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000535 for (Attribute I : *this)
536 if (I.hasAttribute(Attribute::Alignment))
537 return I.getAlignment();
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000538 return 0;
539}
540
541unsigned AttributeSetNode::getStackAlignment() const {
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000542 for (Attribute I : *this)
543 if (I.hasAttribute(Attribute::StackAlignment))
544 return I.getStackAlignment();
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000545 return 0;
546}
547
Hal Finkelb0407ba2014-07-18 15:51:28 +0000548uint64_t AttributeSetNode::getDereferenceableBytes() const {
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000549 for (Attribute I : *this)
550 if (I.hasAttribute(Attribute::Dereferenceable))
551 return I.getDereferenceableBytes();
Hal Finkelb0407ba2014-07-18 15:51:28 +0000552 return 0;
553}
554
Sanjoy Das06cf33f2015-05-06 17:41:54 +0000555uint64_t AttributeSetNode::getDereferenceableOrNullBytes() const {
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000556 for (Attribute I : *this)
557 if (I.hasAttribute(Attribute::DereferenceableOrNull))
558 return I.getDereferenceableOrNullBytes();
Sanjoy Das06cf33f2015-05-06 17:41:54 +0000559 return 0;
560}
561
George Burgess IV278199f2016-04-12 01:05:35 +0000562std::pair<unsigned, Optional<unsigned>>
563AttributeSetNode::getAllocSizeArgs() const {
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000564 for (Attribute I : *this)
565 if (I.hasAttribute(Attribute::AllocSize))
566 return I.getAllocSizeArgs();
George Burgess IV278199f2016-04-12 01:05:35 +0000567 return std::make_pair(0, 0);
568}
569
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +0000570std::string AttributeSetNode::getAsString(bool InAttrGrp) const {
Benjamin Kramer0baf8f42013-04-19 11:43:21 +0000571 std::string Str;
Benjamin Kramer741146b2013-07-11 12:13:16 +0000572 for (iterator I = begin(), E = end(); I != E; ++I) {
573 if (I != begin())
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +0000574 Str += ' ';
575 Str += I->getAsString(InAttrGrp);
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000576 }
577 return Str;
578}
579
Bill Wendlingd509a662013-01-29 00:34:06 +0000580//===----------------------------------------------------------------------===//
581// AttributeSetImpl Definition
582//===----------------------------------------------------------------------===//
583
Matthias Braun8c209aa2017-01-28 02:02:38 +0000584#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Yaron Kereneb2a2542016-01-29 20:50:44 +0000585LLVM_DUMP_METHOD void AttributeSetImpl::dump() const {
Peter Collingbourneabca2ec2013-08-02 22:34:30 +0000586 AttributeSet(const_cast<AttributeSetImpl *>(this)).dump();
587}
Matthias Braun8c209aa2017-01-28 02:02:38 +0000588#endif
Peter Collingbourneabca2ec2013-08-02 22:34:30 +0000589
Bill Wendlingd509a662013-01-29 00:34:06 +0000590//===----------------------------------------------------------------------===//
591// AttributeSet Construction and Mutation Methods
592//===----------------------------------------------------------------------===//
593
Bill Wendling60011b82013-01-29 01:43:29 +0000594AttributeSet
595AttributeSet::getImpl(LLVMContext &C,
596 ArrayRef<std::pair<unsigned, AttributeSetNode*> > Attrs) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000597 LLVMContextImpl *pImpl = C.pImpl;
598 FoldingSetNodeID ID;
599 AttributeSetImpl::Profile(ID, Attrs);
600
601 void *InsertPoint;
602 AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
603
604 // If we didn't find any existing attributes of the same shape then
605 // create a new one and insert it.
606 if (!PA) {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000607 // Coallocate entries after the AttributeSetImpl itself.
James Y Knightaa365b22015-08-05 22:57:34 +0000608 void *Mem = ::operator new(
609 AttributeSetImpl::totalSizeToAlloc<IndexAttrPair>(Attrs.size()));
Benjamin Kramer741146b2013-07-11 12:13:16 +0000610 PA = new (Mem) AttributeSetImpl(C, Attrs);
Bill Wendlingd509a662013-01-29 00:34:06 +0000611 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
612 }
613
614 // Return the AttributesList that we found or created.
615 return AttributeSet(PA);
616}
617
618AttributeSet AttributeSet::get(LLVMContext &C,
619 ArrayRef<std::pair<unsigned, Attribute> > Attrs){
620 // If there are no attributes then return a null AttributesList pointer.
621 if (Attrs.empty())
622 return AttributeSet();
623
Craig Toppere30b8ca2016-01-03 19:43:40 +0000624 assert(std::is_sorted(Attrs.begin(), Attrs.end(),
625 [](const std::pair<unsigned, Attribute> &LHS,
626 const std::pair<unsigned, Attribute> &RHS) {
627 return LHS.first < RHS.first;
628 }) && "Misordered Attributes list!");
David Majnemer0a16c222016-08-11 21:15:00 +0000629 assert(none_of(Attrs,
630 [](const std::pair<unsigned, Attribute> &Pair) {
631 return Pair.second.hasAttribute(Attribute::None);
632 }) &&
633 "Pointless attribute!");
Bill Wendlingd509a662013-01-29 00:34:06 +0000634
635 // Create a vector if (unsigned, AttributeSetNode*) pairs from the attributes
636 // list.
637 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrPairVec;
638 for (ArrayRef<std::pair<unsigned, Attribute> >::iterator I = Attrs.begin(),
639 E = Attrs.end(); I != E; ) {
640 unsigned Index = I->first;
641 SmallVector<Attribute, 4> AttrVec;
NAKAMURA Takumif05d2f22013-01-29 15:18:16 +0000642 while (I != E && I->first == Index) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000643 AttrVec.push_back(I->second);
644 ++I;
645 }
646
David Majnemer0a16c222016-08-11 21:15:00 +0000647 AttrPairVec.emplace_back(Index, AttributeSetNode::get(C, AttrVec));
Bill Wendlingd509a662013-01-29 00:34:06 +0000648 }
649
650 return getImpl(C, AttrPairVec);
651}
652
653AttributeSet AttributeSet::get(LLVMContext &C,
654 ArrayRef<std::pair<unsigned,
655 AttributeSetNode*> > Attrs) {
656 // If there are no attributes then return a null AttributesList pointer.
657 if (Attrs.empty())
658 return AttributeSet();
659
660 return getImpl(C, Attrs);
661}
662
David Majnemercf63a792014-05-03 23:00:35 +0000663AttributeSet AttributeSet::get(LLVMContext &C, unsigned Index,
664 const AttrBuilder &B) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000665 if (!B.hasAttributes())
666 return AttributeSet();
Bill Wendlingf7134812013-01-29 01:02:03 +0000667
Bill Wendlingae89a0f2013-02-05 23:48:36 +0000668 // Add target-independent attributes.
Bill Wendlingf7134812013-01-29 01:02:03 +0000669 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
Benjamin Kramer45e7d532013-02-16 19:13:18 +0000670 for (Attribute::AttrKind Kind = Attribute::None;
Benjamin Kramer6f37dac2013-02-16 19:22:28 +0000671 Kind != Attribute::EndAttrKinds; Kind = Attribute::AttrKind(Kind + 1)) {
Benjamin Kramer45e7d532013-02-16 19:13:18 +0000672 if (!B.contains(Kind))
673 continue;
674
George Burgess IV500d3032015-12-16 05:21:02 +0000675 Attribute Attr;
676 switch (Kind) {
677 case Attribute::Alignment:
678 Attr = Attribute::getWithAlignment(C, B.getAlignment());
679 break;
680 case Attribute::StackAlignment:
681 Attr = Attribute::getWithStackAlignment(C, B.getStackAlignment());
682 break;
683 case Attribute::Dereferenceable:
684 Attr = Attribute::getWithDereferenceableBytes(
685 C, B.getDereferenceableBytes());
686 break;
687 case Attribute::DereferenceableOrNull:
688 Attr = Attribute::getWithDereferenceableOrNullBytes(
689 C, B.getDereferenceableOrNullBytes());
690 break;
George Burgess IV278199f2016-04-12 01:05:35 +0000691 case Attribute::AllocSize: {
692 auto A = B.getAllocSizeArgs();
693 Attr = Attribute::getWithAllocSizeArgs(C, A.first, A.second);
694 break;
695 }
George Burgess IV500d3032015-12-16 05:21:02 +0000696 default:
697 Attr = Attribute::get(C, Kind);
698 }
David Majnemer0a16c222016-08-11 21:15:00 +0000699 Attrs.emplace_back(Index, Attr);
Bill Wendlingf7134812013-01-29 01:02:03 +0000700 }
701
Bill Wendlingae89a0f2013-02-05 23:48:36 +0000702 // Add target-dependent (string) attributes.
Benjamin Kramerea76b6f2016-06-12 19:02:34 +0000703 for (const auto &TDA : B.td_attrs())
David Majnemer0a16c222016-08-11 21:15:00 +0000704 Attrs.emplace_back(Index, Attribute::get(C, TDA.first, TDA.second));
Bill Wendlingae89a0f2013-02-05 23:48:36 +0000705
Bill Wendlingf7134812013-01-29 01:02:03 +0000706 return get(C, Attrs);
Bill Wendlingd509a662013-01-29 00:34:06 +0000707}
708
Bill Wendling211316c2013-04-18 20:17:28 +0000709AttributeSet AttributeSet::get(LLVMContext &C, unsigned Index,
Amaury Sechet392638d2016-06-14 20:27:35 +0000710 ArrayRef<Attribute::AttrKind> Kinds) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000711 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
Amaury Sechet392638d2016-06-14 20:27:35 +0000712 for (Attribute::AttrKind K : Kinds)
David Majnemer0a16c222016-08-11 21:15:00 +0000713 Attrs.emplace_back(Index, Attribute::get(C, K));
Bill Wendlingd509a662013-01-29 00:34:06 +0000714 return get(C, Attrs);
715}
716
Amaury Sechet6100adf2016-06-15 17:50:39 +0000717AttributeSet AttributeSet::get(LLVMContext &C, unsigned Index,
718 ArrayRef<StringRef> Kinds) {
719 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
720 for (StringRef K : Kinds)
David Majnemer0a16c222016-08-11 21:15:00 +0000721 Attrs.emplace_back(Index, Attribute::get(C, K));
Amaury Sechet6100adf2016-06-15 17:50:39 +0000722 return get(C, Attrs);
723}
724
Bill Wendlingd509a662013-01-29 00:34:06 +0000725AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<AttributeSet> Attrs) {
726 if (Attrs.empty()) return AttributeSet();
Peter Collingbournebd6c7452013-08-02 22:29:40 +0000727 if (Attrs.size() == 1) return Attrs[0];
Bill Wendlingd509a662013-01-29 00:34:06 +0000728
729 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrNodeVec;
Peter Collingbournebd6c7452013-08-02 22:29:40 +0000730 AttributeSetImpl *A0 = Attrs[0].pImpl;
731 if (A0)
Amaury Sechet24c84fd2016-06-14 22:04:16 +0000732 AttrNodeVec.append(A0->getNode(0), A0->getNode(A0->getNumSlots()));
Peter Collingbournebd6c7452013-08-02 22:29:40 +0000733 // Copy all attributes from Attrs into AttrNodeVec while keeping AttrNodeVec
734 // ordered by index. Because we know that each list in Attrs is ordered by
735 // index we only need to merge each successive list in rather than doing a
736 // full sort.
737 for (unsigned I = 1, E = Attrs.size(); I != E; ++I) {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000738 AttributeSetImpl *AS = Attrs[I].pImpl;
739 if (!AS) continue;
Peter Collingbournebd6c7452013-08-02 22:29:40 +0000740 SmallVector<std::pair<unsigned, AttributeSetNode *>, 8>::iterator
741 ANVI = AttrNodeVec.begin(), ANVE;
James Y Knightaa365b22015-08-05 22:57:34 +0000742 for (const IndexAttrPair *AI = AS->getNode(0),
Amaury Sechet24c84fd2016-06-14 22:04:16 +0000743 *AE = AS->getNode(AS->getNumSlots());
Peter Collingbournebd6c7452013-08-02 22:29:40 +0000744 AI != AE; ++AI) {
745 ANVE = AttrNodeVec.end();
746 while (ANVI != ANVE && ANVI->first <= AI->first)
747 ++ANVI;
748 ANVI = AttrNodeVec.insert(ANVI, *AI) + 1;
749 }
Bill Wendlingd509a662013-01-29 00:34:06 +0000750 }
751
752 return getImpl(C, AttrNodeVec);
753}
754
Bill Wendling211316c2013-04-18 20:17:28 +0000755AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
Amaury Sechet392638d2016-06-14 20:27:35 +0000756 Attribute::AttrKind Kind) const {
757 if (hasAttribute(Index, Kind)) return *this;
758 return addAttributes(C, Index, AttributeSet::get(C, Index, Kind));
Reed Kotler795c7b42013-03-13 20:20:08 +0000759}
760
Bill Wendling3b2f6102013-07-25 18:34:24 +0000761AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
762 StringRef Kind, StringRef Value) const {
763 llvm::AttrBuilder B;
764 B.addAttribute(Kind, Value);
765 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
766}
767
Akira Hatanaka237916b2015-12-02 06:58:49 +0000768AttributeSet AttributeSet::addAttribute(LLVMContext &C,
769 ArrayRef<unsigned> Indices,
770 Attribute A) const {
Amaury Sechet24c84fd2016-06-14 22:04:16 +0000771 unsigned I = 0, E = pImpl ? pImpl->getNumSlots() : 0;
Akira Hatanaka237916b2015-12-02 06:58:49 +0000772 auto IdxI = Indices.begin(), IdxE = Indices.end();
773 SmallVector<AttributeSet, 4> AttrSet;
774
775 while (I != E && IdxI != IdxE) {
776 if (getSlotIndex(I) < *IdxI)
777 AttrSet.emplace_back(getSlotAttributes(I++));
778 else if (getSlotIndex(I) > *IdxI)
779 AttrSet.emplace_back(AttributeSet::get(C, std::make_pair(*IdxI++, A)));
780 else {
781 AttrBuilder B(getSlotAttributes(I), *IdxI);
782 B.addAttribute(A);
783 AttrSet.emplace_back(AttributeSet::get(C, *IdxI, B));
784 ++I;
785 ++IdxI;
786 }
787 }
788
789 while (I != E)
790 AttrSet.emplace_back(getSlotAttributes(I++));
791
792 while (IdxI != IdxE)
793 AttrSet.emplace_back(AttributeSet::get(C, std::make_pair(*IdxI++, A)));
794
795 return get(C, AttrSet);
796}
797
Bill Wendling211316c2013-04-18 20:17:28 +0000798AttributeSet AttributeSet::addAttributes(LLVMContext &C, unsigned Index,
Bill Wendlingd509a662013-01-29 00:34:06 +0000799 AttributeSet Attrs) const {
800 if (!pImpl) return Attrs;
801 if (!Attrs.pImpl) return *this;
802
803#ifndef NDEBUG
804 // FIXME it is not obvious how this should work for alignment. For now, say
805 // we can't change a known alignment.
Bill Wendling211316c2013-04-18 20:17:28 +0000806 unsigned OldAlign = getParamAlignment(Index);
807 unsigned NewAlign = Attrs.getParamAlignment(Index);
Bill Wendlingd509a662013-01-29 00:34:06 +0000808 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
809 "Attempt to change alignment!");
810#endif
811
812 // Add the attribute slots before the one we're trying to add.
813 SmallVector<AttributeSet, 4> AttrSet;
Amaury Sechet24c84fd2016-06-14 22:04:16 +0000814 uint64_t NumAttrs = pImpl->getNumSlots();
Bill Wendlingd509a662013-01-29 00:34:06 +0000815 AttributeSet AS;
816 uint64_t LastIndex = 0;
817 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
Bill Wendling211316c2013-04-18 20:17:28 +0000818 if (getSlotIndex(I) >= Index) {
819 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
Bill Wendlingd509a662013-01-29 00:34:06 +0000820 break;
821 }
822 LastIndex = I + 1;
823 AttrSet.push_back(getSlotAttributes(I));
824 }
825
826 // Now add the attribute into the correct slot. There may already be an
827 // AttributeSet there.
Bill Wendling211316c2013-04-18 20:17:28 +0000828 AttrBuilder B(AS, Index);
Bill Wendlingd509a662013-01-29 00:34:06 +0000829
Amaury Sechet24c84fd2016-06-14 22:04:16 +0000830 for (unsigned I = 0, E = Attrs.pImpl->getNumSlots(); I != E; ++I)
Bill Wendling211316c2013-04-18 20:17:28 +0000831 if (Attrs.getSlotIndex(I) == Index) {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000832 for (AttributeSetImpl::iterator II = Attrs.pImpl->begin(I),
Bill Wendlingd509a662013-01-29 00:34:06 +0000833 IE = Attrs.pImpl->end(I); II != IE; ++II)
Bill Wendling23804da2013-01-31 23:38:01 +0000834 B.addAttribute(*II);
Bill Wendlingd509a662013-01-29 00:34:06 +0000835 break;
836 }
837
Bill Wendling211316c2013-04-18 20:17:28 +0000838 AttrSet.push_back(AttributeSet::get(C, Index, B));
Bill Wendlingd509a662013-01-29 00:34:06 +0000839
840 // Add the remaining attribute slots.
841 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
842 AttrSet.push_back(getSlotAttributes(I));
843
844 return get(C, AttrSet);
845}
846
Bill Wendling211316c2013-04-18 20:17:28 +0000847AttributeSet AttributeSet::removeAttribute(LLVMContext &C, unsigned Index,
Amaury Sechet392638d2016-06-14 20:27:35 +0000848 Attribute::AttrKind Kind) const {
849 if (!hasAttribute(Index, Kind)) return *this;
850 return removeAttributes(C, Index, AttributeSet::get(C, Index, Kind));
Bill Wendlingd509a662013-01-29 00:34:06 +0000851}
852
Amaury Sechet6100adf2016-06-15 17:50:39 +0000853AttributeSet AttributeSet::removeAttribute(LLVMContext &C, unsigned Index,
854 StringRef Kind) const {
855 if (!hasAttribute(Index, Kind)) return *this;
856 return removeAttributes(C, Index, AttributeSet::get(C, Index, Kind));
857}
858
Bill Wendling211316c2013-04-18 20:17:28 +0000859AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Index,
Bill Wendlingd509a662013-01-29 00:34:06 +0000860 AttributeSet Attrs) const {
861 if (!pImpl) return AttributeSet();
862 if (!Attrs.pImpl) return *this;
863
Pete Cooper67cf9a72015-11-19 05:56:52 +0000864 // FIXME it is not obvious how this should work for alignment.
865 // For now, say we can't pass in alignment, which no current use does.
866 assert(!Attrs.hasAttribute(Index, Attribute::Alignment) &&
867 "Attempt to change alignment!");
868
Bill Wendlingd509a662013-01-29 00:34:06 +0000869 // Add the attribute slots before the one we're trying to add.
870 SmallVector<AttributeSet, 4> AttrSet;
Amaury Sechet24c84fd2016-06-14 22:04:16 +0000871 uint64_t NumAttrs = pImpl->getNumSlots();
Bill Wendlingd509a662013-01-29 00:34:06 +0000872 AttributeSet AS;
873 uint64_t LastIndex = 0;
874 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
Bill Wendling211316c2013-04-18 20:17:28 +0000875 if (getSlotIndex(I) >= Index) {
876 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
Bill Wendlingd509a662013-01-29 00:34:06 +0000877 break;
878 }
879 LastIndex = I + 1;
880 AttrSet.push_back(getSlotAttributes(I));
881 }
882
Bill Wendlingd2196752013-01-30 23:07:40 +0000883 // Now remove the attribute from the correct slot. There may already be an
Bill Wendlingd509a662013-01-29 00:34:06 +0000884 // AttributeSet there.
Bill Wendling211316c2013-04-18 20:17:28 +0000885 AttrBuilder B(AS, Index);
Bill Wendlingd509a662013-01-29 00:34:06 +0000886
Amaury Sechet24c84fd2016-06-14 22:04:16 +0000887 for (unsigned I = 0, E = Attrs.pImpl->getNumSlots(); I != E; ++I)
Bill Wendling211316c2013-04-18 20:17:28 +0000888 if (Attrs.getSlotIndex(I) == Index) {
889 B.removeAttributes(Attrs.pImpl->getSlotAttributes(I), Index);
Bill Wendlingd509a662013-01-29 00:34:06 +0000890 break;
891 }
892
Bill Wendling211316c2013-04-18 20:17:28 +0000893 AttrSet.push_back(AttributeSet::get(C, Index, B));
Bill Wendlingd509a662013-01-29 00:34:06 +0000894
895 // Add the remaining attribute slots.
896 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
897 AttrSet.push_back(getSlotAttributes(I));
898
899 return get(C, AttrSet);
900}
901
Pete Cooperd2a44612015-05-06 23:19:43 +0000902AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Index,
903 const AttrBuilder &Attrs) const {
904 if (!pImpl) return AttributeSet();
905
906 // FIXME it is not obvious how this should work for alignment.
907 // For now, say we can't pass in alignment, which no current use does.
908 assert(!Attrs.hasAlignmentAttr() && "Attempt to change alignment!");
909
910 // Add the attribute slots before the one we're trying to add.
911 SmallVector<AttributeSet, 4> AttrSet;
Amaury Sechet24c84fd2016-06-14 22:04:16 +0000912 uint64_t NumAttrs = pImpl->getNumSlots();
Pete Cooperd2a44612015-05-06 23:19:43 +0000913 AttributeSet AS;
914 uint64_t LastIndex = 0;
915 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
916 if (getSlotIndex(I) >= Index) {
917 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
918 break;
919 }
920 LastIndex = I + 1;
921 AttrSet.push_back(getSlotAttributes(I));
922 }
923
924 // Now remove the attribute from the correct slot. There may already be an
925 // AttributeSet there.
926 AttrBuilder B(AS, Index);
927 B.remove(Attrs);
928
929 AttrSet.push_back(AttributeSet::get(C, Index, B));
930
931 // Add the remaining attribute slots.
932 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
933 AttrSet.push_back(getSlotAttributes(I));
934
935 return get(C, AttrSet);
936}
937
Ramkumar Ramachandra8fcb4982015-02-14 19:37:54 +0000938AttributeSet AttributeSet::addDereferenceableAttr(LLVMContext &C, unsigned Index,
939 uint64_t Bytes) const {
940 llvm::AttrBuilder B;
941 B.addDereferenceableAttr(Bytes);
942 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
943}
944
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000945AttributeSet AttributeSet::addDereferenceableOrNullAttr(LLVMContext &C,
946 unsigned Index,
947 uint64_t Bytes) const {
948 llvm::AttrBuilder B;
949 B.addDereferenceableOrNullAttr(Bytes);
950 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
951}
952
George Burgess IV278199f2016-04-12 01:05:35 +0000953AttributeSet
954AttributeSet::addAllocSizeAttr(LLVMContext &C, unsigned Index,
955 unsigned ElemSizeArg,
956 const Optional<unsigned> &NumElemsArg) {
957 llvm::AttrBuilder B;
958 B.addAllocSizeAttr(ElemSizeArg, NumElemsArg);
959 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
960}
961
Bill Wendlingd509a662013-01-29 00:34:06 +0000962//===----------------------------------------------------------------------===//
963// AttributeSet Accessor Methods
964//===----------------------------------------------------------------------===//
965
Bill Wendling5d020a32013-02-10 05:00:40 +0000966LLVMContext &AttributeSet::getContext() const {
967 return pImpl->getContext();
968}
969
Bill Wendling211316c2013-04-18 20:17:28 +0000970AttributeSet AttributeSet::getParamAttributes(unsigned Index) const {
971 return pImpl && hasAttributes(Index) ?
Bill Wendlingd509a662013-01-29 00:34:06 +0000972 AttributeSet::get(pImpl->getContext(),
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000973 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendling211316c2013-04-18 20:17:28 +0000974 std::make_pair(Index, getAttributes(Index)))) :
Bill Wendlingd509a662013-01-29 00:34:06 +0000975 AttributeSet();
976}
977
978AttributeSet AttributeSet::getRetAttributes() const {
979 return pImpl && hasAttributes(ReturnIndex) ?
980 AttributeSet::get(pImpl->getContext(),
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000981 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendlingd509a662013-01-29 00:34:06 +0000982 std::make_pair(ReturnIndex,
983 getAttributes(ReturnIndex)))) :
984 AttributeSet();
985}
986
987AttributeSet AttributeSet::getFnAttributes() const {
988 return pImpl && hasAttributes(FunctionIndex) ?
989 AttributeSet::get(pImpl->getContext(),
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000990 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendlingd509a662013-01-29 00:34:06 +0000991 std::make_pair(FunctionIndex,
992 getAttributes(FunctionIndex)))) :
993 AttributeSet();
994}
995
996bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000997 AttributeSetNode *ASN = getAttributes(Index);
George Burgess IV500d3032015-12-16 05:21:02 +0000998 return ASN && ASN->hasAttribute(Kind);
Bill Wendlingd509a662013-01-29 00:34:06 +0000999}
1000
Bill Wendlingbce7b972013-02-13 08:42:21 +00001001bool AttributeSet::hasAttribute(unsigned Index, StringRef Kind) const {
1002 AttributeSetNode *ASN = getAttributes(Index);
George Burgess IV500d3032015-12-16 05:21:02 +00001003 return ASN && ASN->hasAttribute(Kind);
Bill Wendlingbce7b972013-02-13 08:42:21 +00001004}
1005
Bill Wendlingd509a662013-01-29 00:34:06 +00001006bool AttributeSet::hasAttributes(unsigned Index) const {
Bill Wendlingf2955aa2013-01-29 03:20:31 +00001007 AttributeSetNode *ASN = getAttributes(Index);
George Burgess IV500d3032015-12-16 05:21:02 +00001008 return ASN && ASN->hasAttributes();
Bill Wendlingd509a662013-01-29 00:34:06 +00001009}
1010
Matthias Braun33282812016-01-29 22:25:19 +00001011bool AttributeSet::hasFnAttribute(Attribute::AttrKind Kind) const {
1012 return pImpl && pImpl->hasFnAttribute(Kind);
1013}
1014
Amaury Sechet5f04d812016-09-09 04:50:38 +00001015bool AttributeSet::hasFnAttribute(StringRef Kind) const {
1016 return hasAttribute(AttributeSet::FunctionIndex, Kind);
1017}
1018
Hal Finkele87ad542016-07-10 23:01:32 +00001019bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr,
1020 unsigned *Index) const {
Craig Topperc6207612014-04-09 06:08:46 +00001021 if (!pImpl) return false;
Bill Wendlingd509a662013-01-29 00:34:06 +00001022
Amaury Sechet24c84fd2016-06-14 22:04:16 +00001023 for (unsigned I = 0, E = pImpl->getNumSlots(); I != E; ++I)
Benjamin Kramer741146b2013-07-11 12:13:16 +00001024 for (AttributeSetImpl::iterator II = pImpl->begin(I),
Bill Wendlingd509a662013-01-29 00:34:06 +00001025 IE = pImpl->end(I); II != IE; ++II)
Hal Finkele87ad542016-07-10 23:01:32 +00001026 if (II->hasAttribute(Attr)) {
1027 if (Index) *Index = pImpl->getSlotIndex(I);
Bill Wendlingd509a662013-01-29 00:34:06 +00001028 return true;
Hal Finkele87ad542016-07-10 23:01:32 +00001029 }
Bill Wendlingd509a662013-01-29 00:34:06 +00001030
1031 return false;
1032}
1033
Bill Wendlingbce7b972013-02-13 08:42:21 +00001034Attribute AttributeSet::getAttribute(unsigned Index,
1035 Attribute::AttrKind Kind) const {
1036 AttributeSetNode *ASN = getAttributes(Index);
1037 return ASN ? ASN->getAttribute(Kind) : Attribute();
1038}
1039
1040Attribute AttributeSet::getAttribute(unsigned Index,
1041 StringRef Kind) const {
1042 AttributeSetNode *ASN = getAttributes(Index);
1043 return ASN ? ASN->getAttribute(Kind) : Attribute();
1044}
1045
Bill Wendlingf2955aa2013-01-29 03:20:31 +00001046unsigned AttributeSet::getParamAlignment(unsigned Index) const {
1047 AttributeSetNode *ASN = getAttributes(Index);
1048 return ASN ? ASN->getAlignment() : 0;
Bill Wendlingd509a662013-01-29 00:34:06 +00001049}
1050
1051unsigned AttributeSet::getStackAlignment(unsigned Index) const {
Bill Wendlingf2955aa2013-01-29 03:20:31 +00001052 AttributeSetNode *ASN = getAttributes(Index);
1053 return ASN ? ASN->getStackAlignment() : 0;
Bill Wendlingd509a662013-01-29 00:34:06 +00001054}
1055
Hal Finkelb0407ba2014-07-18 15:51:28 +00001056uint64_t AttributeSet::getDereferenceableBytes(unsigned Index) const {
1057 AttributeSetNode *ASN = getAttributes(Index);
1058 return ASN ? ASN->getDereferenceableBytes() : 0;
1059}
1060
Sanjoy Das06cf33f2015-05-06 17:41:54 +00001061uint64_t AttributeSet::getDereferenceableOrNullBytes(unsigned Index) const {
1062 AttributeSetNode *ASN = getAttributes(Index);
1063 return ASN ? ASN->getDereferenceableOrNullBytes() : 0;
1064}
1065
George Burgess IV278199f2016-04-12 01:05:35 +00001066std::pair<unsigned, Optional<unsigned>>
1067AttributeSet::getAllocSizeArgs(unsigned Index) const {
1068 AttributeSetNode *ASN = getAttributes(Index);
Matt Arsenault4ced16d2016-07-18 22:12:46 +00001069 return ASN ? ASN->getAllocSizeArgs() : std::make_pair(0u, Optional<unsigned>(0u));
George Burgess IV278199f2016-04-12 01:05:35 +00001070}
1071
1072std::string AttributeSet::getAsString(unsigned Index, bool InAttrGrp) const {
Bill Wendlingf2955aa2013-01-29 03:20:31 +00001073 AttributeSetNode *ASN = getAttributes(Index);
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +00001074 return ASN ? ASN->getAsString(InAttrGrp) : std::string("");
Bill Wendlingd509a662013-01-29 00:34:06 +00001075}
1076
Bill Wendling211316c2013-04-18 20:17:28 +00001077AttributeSetNode *AttributeSet::getAttributes(unsigned Index) const {
Craig Topperc6207612014-04-09 06:08:46 +00001078 if (!pImpl) return nullptr;
Bill Wendlingd509a662013-01-29 00:34:06 +00001079
Bill Wendlingf2955aa2013-01-29 03:20:31 +00001080 // Loop through to find the attribute node we want.
Amaury Sechet24c84fd2016-06-14 22:04:16 +00001081 for (unsigned I = 0, E = pImpl->getNumSlots(); I != E; ++I)
Bill Wendling211316c2013-04-18 20:17:28 +00001082 if (pImpl->getSlotIndex(I) == Index)
Bill Wendlingf2955aa2013-01-29 03:20:31 +00001083 return pImpl->getSlotNode(I);
Bill Wendlingd509a662013-01-29 00:34:06 +00001084
Craig Topperc6207612014-04-09 06:08:46 +00001085 return nullptr;
Bill Wendlingd509a662013-01-29 00:34:06 +00001086}
1087
Bill Wendling211316c2013-04-18 20:17:28 +00001088AttributeSet::iterator AttributeSet::begin(unsigned Slot) const {
Bill Wendlinga9174862013-01-31 23:53:05 +00001089 if (!pImpl)
1090 return ArrayRef<Attribute>().begin();
Bill Wendling211316c2013-04-18 20:17:28 +00001091 return pImpl->begin(Slot);
Bill Wendlinga9174862013-01-31 23:53:05 +00001092}
1093
Bill Wendling211316c2013-04-18 20:17:28 +00001094AttributeSet::iterator AttributeSet::end(unsigned Slot) const {
Bill Wendlinga9174862013-01-31 23:53:05 +00001095 if (!pImpl)
1096 return ArrayRef<Attribute>().end();
Bill Wendling211316c2013-04-18 20:17:28 +00001097 return pImpl->end(Slot);
Bill Wendlinga9174862013-01-31 23:53:05 +00001098}
1099
Bill Wendlingd509a662013-01-29 00:34:06 +00001100//===----------------------------------------------------------------------===//
1101// AttributeSet Introspection Methods
1102//===----------------------------------------------------------------------===//
1103
Bill Wendlingd509a662013-01-29 00:34:06 +00001104unsigned AttributeSet::getNumSlots() const {
Amaury Sechet24c84fd2016-06-14 22:04:16 +00001105 return pImpl ? pImpl->getNumSlots() : 0;
Bill Wendlingd509a662013-01-29 00:34:06 +00001106}
1107
Rafael Espindoladd275302013-04-30 16:53:38 +00001108unsigned AttributeSet::getSlotIndex(unsigned Slot) const {
Amaury Sechet24c84fd2016-06-14 22:04:16 +00001109 assert(pImpl && Slot < pImpl->getNumSlots() &&
Bill Wendlingd509a662013-01-29 00:34:06 +00001110 "Slot # out of range!");
1111 return pImpl->getSlotIndex(Slot);
1112}
1113
1114AttributeSet AttributeSet::getSlotAttributes(unsigned Slot) const {
Amaury Sechet24c84fd2016-06-14 22:04:16 +00001115 assert(pImpl && Slot < pImpl->getNumSlots() &&
Bill Wendlingd509a662013-01-29 00:34:06 +00001116 "Slot # out of range!");
1117 return pImpl->getSlotAttributes(Slot);
1118}
1119
Matthias Braun8c209aa2017-01-28 02:02:38 +00001120#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Yaron Kereneb2a2542016-01-29 20:50:44 +00001121LLVM_DUMP_METHOD void AttributeSet::dump() const {
Bill Wendlingd509a662013-01-29 00:34:06 +00001122 dbgs() << "PAL[\n";
1123
1124 for (unsigned i = 0, e = getNumSlots(); i < e; ++i) {
1125 uint64_t Index = getSlotIndex(i);
1126 dbgs() << " { ";
1127 if (Index == ~0U)
1128 dbgs() << "~0U";
1129 else
1130 dbgs() << Index;
1131 dbgs() << " => " << getAsString(Index) << " }\n";
1132 }
1133
1134 dbgs() << "]\n";
1135}
Matthias Braun8c209aa2017-01-28 02:02:38 +00001136#endif
Bill Wendlingd509a662013-01-29 00:34:06 +00001137
Bill Wendlingabf3feb2012-10-05 06:44:41 +00001138//===----------------------------------------------------------------------===//
Bill Wendlingcd330342013-01-04 23:27:34 +00001139// AttrBuilder Method Implementations
Bill Wendlingabf3feb2012-10-05 06:44:41 +00001140//===----------------------------------------------------------------------===//
1141
Bill Wendling211316c2013-04-18 20:17:28 +00001142AttrBuilder::AttrBuilder(AttributeSet AS, unsigned Index)
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001143 : Attrs(0), Alignment(0), StackAlignment(0), DerefBytes(0),
George Burgess IV278199f2016-04-12 01:05:35 +00001144 DerefOrNullBytes(0), AllocSizeArgs(0) {
Bill Wendling56b0b2a2013-01-27 21:23:46 +00001145 AttributeSetImpl *pImpl = AS.pImpl;
Bill Wendling096f5442013-01-07 08:24:35 +00001146 if (!pImpl) return;
1147
Amaury Sechet24c84fd2016-06-14 22:04:16 +00001148 for (unsigned I = 0, E = pImpl->getNumSlots(); I != E; ++I) {
Bill Wendling211316c2013-04-18 20:17:28 +00001149 if (pImpl->getSlotIndex(I) != Index) continue;
Bill Wendling096f5442013-01-07 08:24:35 +00001150
Benjamin Kramer741146b2013-07-11 12:13:16 +00001151 for (AttributeSetImpl::iterator II = pImpl->begin(I),
Bill Wendling9eb689c2013-01-28 00:21:34 +00001152 IE = pImpl->end(I); II != IE; ++II)
Bill Wendling23804da2013-01-31 23:38:01 +00001153 addAttribute(*II);
Bill Wendling9eb689c2013-01-28 00:21:34 +00001154
1155 break;
1156 }
Bill Wendling096f5442013-01-07 08:24:35 +00001157}
1158
Bill Wendlingcd330342013-01-04 23:27:34 +00001159void AttrBuilder::clear() {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001160 Attrs.reset();
Sanjoy Das88d0fde2015-09-03 22:27:42 +00001161 TargetDepAttrs.clear();
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001162 Alignment = StackAlignment = DerefBytes = DerefOrNullBytes = 0;
George Burgess IV278199f2016-04-12 01:05:35 +00001163 AllocSizeArgs = 0;
Bill Wendlingcd330342013-01-04 23:27:34 +00001164}
1165
1166AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001167 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
Bill Wendling1c7cc8a2013-01-31 23:16:25 +00001168 assert(Val != Attribute::Alignment && Val != Attribute::StackAlignment &&
George Burgess IV278199f2016-04-12 01:05:35 +00001169 Val != Attribute::Dereferenceable && Val != Attribute::AllocSize &&
Hal Finkelb0407ba2014-07-18 15:51:28 +00001170 "Adding integer attribute without adding a value!");
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001171 Attrs[Val] = true;
Bill Wendling7c04e042012-10-09 19:01:18 +00001172 return *this;
Bill Wendlingabf3feb2012-10-05 06:44:41 +00001173}
1174
Bill Wendling23804da2013-01-31 23:38:01 +00001175AttrBuilder &AttrBuilder::addAttribute(Attribute Attr) {
Bill Wendling0a437302013-02-10 10:13:23 +00001176 if (Attr.isStringAttribute()) {
1177 addAttribute(Attr.getKindAsString(), Attr.getValueAsString());
1178 return *this;
1179 }
1180
Bill Wendling3f12ac22013-02-05 22:37:24 +00001181 Attribute::AttrKind Kind = Attr.getKindAsEnum();
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001182 Attrs[Kind] = true;
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001183
Bill Wendling3f12ac22013-02-05 22:37:24 +00001184 if (Kind == Attribute::Alignment)
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001185 Alignment = Attr.getAlignment();
Bill Wendling3f12ac22013-02-05 22:37:24 +00001186 else if (Kind == Attribute::StackAlignment)
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001187 StackAlignment = Attr.getStackAlignment();
Hal Finkelb0407ba2014-07-18 15:51:28 +00001188 else if (Kind == Attribute::Dereferenceable)
1189 DerefBytes = Attr.getDereferenceableBytes();
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001190 else if (Kind == Attribute::DereferenceableOrNull)
1191 DerefOrNullBytes = Attr.getDereferenceableOrNullBytes();
George Burgess IV278199f2016-04-12 01:05:35 +00001192 else if (Kind == Attribute::AllocSize)
1193 AllocSizeArgs = Attr.getValueAsInt();
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001194 return *this;
1195}
1196
Bill Wendlingb9c5b1a2013-02-05 08:09:32 +00001197AttrBuilder &AttrBuilder::addAttribute(StringRef A, StringRef V) {
1198 TargetDepAttrs[A] = V;
1199 return *this;
1200}
1201
Bill Wendling23804da2013-01-31 23:38:01 +00001202AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001203 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
1204 Attrs[Val] = false;
Bill Wendling23804da2013-01-31 23:38:01 +00001205
1206 if (Val == Attribute::Alignment)
1207 Alignment = 0;
1208 else if (Val == Attribute::StackAlignment)
1209 StackAlignment = 0;
Hal Finkelb0407ba2014-07-18 15:51:28 +00001210 else if (Val == Attribute::Dereferenceable)
1211 DerefBytes = 0;
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001212 else if (Val == Attribute::DereferenceableOrNull)
1213 DerefOrNullBytes = 0;
George Burgess IV278199f2016-04-12 01:05:35 +00001214 else if (Val == Attribute::AllocSize)
1215 AllocSizeArgs = 0;
Bill Wendling23804da2013-01-31 23:38:01 +00001216
1217 return *this;
1218}
1219
Bill Wendlingd2196752013-01-30 23:07:40 +00001220AttrBuilder &AttrBuilder::removeAttributes(AttributeSet A, uint64_t Index) {
Bill Wendling211316c2013-04-18 20:17:28 +00001221 unsigned Slot = ~0U;
Bill Wendlingf1c94e32013-02-01 00:13:50 +00001222 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
1223 if (A.getSlotIndex(I) == Index) {
Bill Wendling211316c2013-04-18 20:17:28 +00001224 Slot = I;
Bill Wendlingf1c94e32013-02-01 00:13:50 +00001225 break;
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001226 }
Bill Wendlingf1c94e32013-02-01 00:13:50 +00001227
Bill Wendling211316c2013-04-18 20:17:28 +00001228 assert(Slot != ~0U && "Couldn't find index in AttributeSet!");
Bill Wendlingf1c94e32013-02-01 00:13:50 +00001229
Bill Wendling211316c2013-04-18 20:17:28 +00001230 for (AttributeSet::iterator I = A.begin(Slot), E = A.end(Slot); I != E; ++I) {
Bill Wendling7cde51d2013-02-12 07:56:49 +00001231 Attribute Attr = *I;
Hal Finkele15442c2014-07-18 06:51:55 +00001232 if (Attr.isEnumAttribute() || Attr.isIntAttribute()) {
George Burgess IV500d3032015-12-16 05:21:02 +00001233 removeAttribute(Attr.getKindAsEnum());
Bill Wendling7cde51d2013-02-12 07:56:49 +00001234 } else {
1235 assert(Attr.isStringAttribute() && "Invalid attribute type!");
George Burgess IV500d3032015-12-16 05:21:02 +00001236 removeAttribute(Attr.getKindAsString());
Bill Wendling7cde51d2013-02-12 07:56:49 +00001237 }
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001238 }
1239
1240 return *this;
1241}
1242
Bill Wendlingb9c5b1a2013-02-05 08:09:32 +00001243AttrBuilder &AttrBuilder::removeAttribute(StringRef A) {
1244 std::map<std::string, std::string>::iterator I = TargetDepAttrs.find(A);
1245 if (I != TargetDepAttrs.end())
1246 TargetDepAttrs.erase(I);
1247 return *this;
1248}
1249
George Burgess IV278199f2016-04-12 01:05:35 +00001250std::pair<unsigned, Optional<unsigned>> AttrBuilder::getAllocSizeArgs() const {
1251 return unpackAllocSizeArgs(AllocSizeArgs);
1252}
1253
Bill Wendling50d27842012-10-15 20:35:56 +00001254AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
Bill Wendlingabd5ba22012-10-14 03:58:29 +00001255 if (Align == 0) return *this;
Bill Wendlingcd330342013-01-04 23:27:34 +00001256
Bill Wendlingabf3feb2012-10-05 06:44:41 +00001257 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1258 assert(Align <= 0x40000000 && "Alignment too large.");
Bill Wendlingcd330342013-01-04 23:27:34 +00001259
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001260 Attrs[Attribute::Alignment] = true;
Bill Wendlingcd330342013-01-04 23:27:34 +00001261 Alignment = Align;
Bill Wendlingabd5ba22012-10-14 03:58:29 +00001262 return *this;
Chris Lattner3e13b8c2008-01-02 23:42:30 +00001263}
1264
Bill Wendlingcd330342013-01-04 23:27:34 +00001265AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
1266 // Default alignment, allow the target to define how to align it.
1267 if (Align == 0) return *this;
1268
1269 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1270 assert(Align <= 0x100 && "Alignment too large.");
1271
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001272 Attrs[Attribute::StackAlignment] = true;
Bill Wendlingcd330342013-01-04 23:27:34 +00001273 StackAlignment = Align;
1274 return *this;
1275}
1276
Hal Finkelb0407ba2014-07-18 15:51:28 +00001277AttrBuilder &AttrBuilder::addDereferenceableAttr(uint64_t Bytes) {
1278 if (Bytes == 0) return *this;
1279
1280 Attrs[Attribute::Dereferenceable] = true;
1281 DerefBytes = Bytes;
1282 return *this;
1283}
1284
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001285AttrBuilder &AttrBuilder::addDereferenceableOrNullAttr(uint64_t Bytes) {
1286 if (Bytes == 0)
1287 return *this;
1288
1289 Attrs[Attribute::DereferenceableOrNull] = true;
1290 DerefOrNullBytes = Bytes;
1291 return *this;
1292}
1293
George Burgess IV278199f2016-04-12 01:05:35 +00001294AttrBuilder &AttrBuilder::addAllocSizeAttr(unsigned ElemSize,
1295 const Optional<unsigned> &NumElems) {
1296 return addAllocSizeAttrFromRawRepr(packAllocSizeArgs(ElemSize, NumElems));
1297}
1298
1299AttrBuilder &AttrBuilder::addAllocSizeAttrFromRawRepr(uint64_t RawArgs) {
1300 // (0, 0) is our "not present" value, so we need to check for it here.
1301 assert(RawArgs && "Invalid allocsize arguments -- given allocsize(0, 0)");
1302
1303 Attrs[Attribute::AllocSize] = true;
1304 // Reuse existing machinery to store this as a single 64-bit integer so we can
1305 // save a few bytes over using a pair<unsigned, Optional<unsigned>>.
1306 AllocSizeArgs = RawArgs;
1307 return *this;
1308}
1309
Bill Wendlinge2614922013-02-06 01:16:00 +00001310AttrBuilder &AttrBuilder::merge(const AttrBuilder &B) {
1311 // FIXME: What if both have alignments, but they don't match?!
1312 if (!Alignment)
1313 Alignment = B.Alignment;
1314
1315 if (!StackAlignment)
1316 StackAlignment = B.StackAlignment;
1317
Hal Finkelb0407ba2014-07-18 15:51:28 +00001318 if (!DerefBytes)
1319 DerefBytes = B.DerefBytes;
1320
Pete Cooperd2a44612015-05-06 23:19:43 +00001321 if (!DerefOrNullBytes)
1322 DerefOrNullBytes = B.DerefOrNullBytes;
1323
George Burgess IV278199f2016-04-12 01:05:35 +00001324 if (!AllocSizeArgs)
1325 AllocSizeArgs = B.AllocSizeArgs;
1326
Benjamin Kramer45e7d532013-02-16 19:13:18 +00001327 Attrs |= B.Attrs;
Bill Wendlinge2614922013-02-06 01:16:00 +00001328
Pete Cooperd2a44612015-05-06 23:19:43 +00001329 for (auto I : B.td_attrs())
1330 TargetDepAttrs[I.first] = I.second;
Bill Wendlinge2614922013-02-06 01:16:00 +00001331
1332 return *this;
1333}
1334
Pete Cooperd2a44612015-05-06 23:19:43 +00001335AttrBuilder &AttrBuilder::remove(const AttrBuilder &B) {
1336 // FIXME: What if both have alignments, but they don't match?!
1337 if (B.Alignment)
1338 Alignment = 0;
1339
1340 if (B.StackAlignment)
1341 StackAlignment = 0;
1342
1343 if (B.DerefBytes)
1344 DerefBytes = 0;
1345
1346 if (B.DerefOrNullBytes)
1347 DerefOrNullBytes = 0;
1348
George Burgess IV278199f2016-04-12 01:05:35 +00001349 if (B.AllocSizeArgs)
1350 AllocSizeArgs = 0;
1351
Pete Cooperd2a44612015-05-06 23:19:43 +00001352 Attrs &= ~B.Attrs;
1353
1354 for (auto I : B.td_attrs())
1355 TargetDepAttrs.erase(I.first);
1356
1357 return *this;
1358}
1359
1360bool AttrBuilder::overlaps(const AttrBuilder &B) const {
1361 // First check if any of the target independent attributes overlap.
1362 if ((Attrs & B.Attrs).any())
1363 return true;
1364
1365 // Then check if any target dependent ones do.
1366 for (auto I : td_attrs())
1367 if (B.contains(I.first))
1368 return true;
1369
1370 return false;
1371}
1372
Bill Wendling4b001442013-02-06 01:33:42 +00001373bool AttrBuilder::contains(StringRef A) const {
1374 return TargetDepAttrs.find(A) != TargetDepAttrs.end();
1375}
1376
Bill Wendling50d27842012-10-15 20:35:56 +00001377bool AttrBuilder::hasAttributes() const {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001378 return !Attrs.none() || !TargetDepAttrs.empty();
Bill Wendlingc6daefa2012-10-08 23:27:46 +00001379}
Bill Wendling9ac69f92013-01-04 20:54:35 +00001380
Bill Wendlingd2196752013-01-30 23:07:40 +00001381bool AttrBuilder::hasAttributes(AttributeSet A, uint64_t Index) const {
Bill Wendling211316c2013-04-18 20:17:28 +00001382 unsigned Slot = ~0U;
Bill Wendling9ca01da2013-02-02 00:42:06 +00001383 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
1384 if (A.getSlotIndex(I) == Index) {
Bill Wendling211316c2013-04-18 20:17:28 +00001385 Slot = I;
Bill Wendling9ca01da2013-02-02 00:42:06 +00001386 break;
1387 }
1388
Bill Wendling211316c2013-04-18 20:17:28 +00001389 assert(Slot != ~0U && "Couldn't find the index!");
Bill Wendling9ca01da2013-02-02 00:42:06 +00001390
George Burgess IV500d3032015-12-16 05:21:02 +00001391 for (AttributeSet::iterator I = A.begin(Slot), E = A.end(Slot); I != E; ++I) {
Bill Wendling7cde51d2013-02-12 07:56:49 +00001392 Attribute Attr = *I;
Hal Finkele15442c2014-07-18 06:51:55 +00001393 if (Attr.isEnumAttribute() || Attr.isIntAttribute()) {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001394 if (Attrs[I->getKindAsEnum()])
Bill Wendling7cde51d2013-02-12 07:56:49 +00001395 return true;
1396 } else {
1397 assert(Attr.isStringAttribute() && "Invalid attribute kind!");
1398 return TargetDepAttrs.find(Attr.getKindAsString())!=TargetDepAttrs.end();
1399 }
1400 }
Bill Wendling9ca01da2013-02-02 00:42:06 +00001401
1402 return false;
Bill Wendling70f39172012-10-09 00:01:21 +00001403}
Bill Wendling9ac69f92013-01-04 20:54:35 +00001404
Bill Wendling50d27842012-10-15 20:35:56 +00001405bool AttrBuilder::hasAlignmentAttr() const {
Bill Wendlingcd330342013-01-04 23:27:34 +00001406 return Alignment != 0;
Bill Wendlingc6daefa2012-10-08 23:27:46 +00001407}
1408
Bill Wendlingd509a662013-01-29 00:34:06 +00001409bool AttrBuilder::operator==(const AttrBuilder &B) {
Benjamin Kramer45e7d532013-02-16 19:13:18 +00001410 if (Attrs != B.Attrs)
1411 return false;
Bill Wendling4b001442013-02-06 01:33:42 +00001412
1413 for (td_const_iterator I = TargetDepAttrs.begin(),
1414 E = TargetDepAttrs.end(); I != E; ++I)
1415 if (B.TargetDepAttrs.find(I->first) == B.TargetDepAttrs.end())
1416 return false;
1417
Hal Finkelb0407ba2014-07-18 15:51:28 +00001418 return Alignment == B.Alignment && StackAlignment == B.StackAlignment &&
1419 DerefBytes == B.DerefBytes;
Bill Wendlingd509a662013-01-29 00:34:06 +00001420}
1421
Bill Wendling57625a42013-01-25 23:09:36 +00001422//===----------------------------------------------------------------------===//
1423// AttributeFuncs Function Defintions
1424//===----------------------------------------------------------------------===//
1425
Bill Wendlingc79cdff2013-02-01 01:04:27 +00001426/// \brief Which attributes cannot be applied to a type.
Craig Toppere3dcce92015-08-01 22:20:21 +00001427AttrBuilder AttributeFuncs::typeIncompatible(Type *Ty) {
Bill Wendling57625a42013-01-25 23:09:36 +00001428 AttrBuilder Incompatible;
1429
1430 if (!Ty->isIntegerTy())
1431 // Attribute that only apply to integers.
1432 Incompatible.addAttribute(Attribute::SExt)
1433 .addAttribute(Attribute::ZExt);
1434
1435 if (!Ty->isPointerTy())
1436 // Attribute that only apply to pointers.
1437 Incompatible.addAttribute(Attribute::ByVal)
1438 .addAttribute(Attribute::Nest)
1439 .addAttribute(Attribute::NoAlias)
1440 .addAttribute(Attribute::NoCapture)
Nick Lewyckyd52b1522014-05-20 01:23:40 +00001441 .addAttribute(Attribute::NonNull)
Hal Finkelb0407ba2014-07-18 15:51:28 +00001442 .addDereferenceableAttr(1) // the int here is ignored
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001443 .addDereferenceableOrNullAttr(1) // the int here is ignored
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001444 .addAttribute(Attribute::ReadNone)
1445 .addAttribute(Attribute::ReadOnly)
Reid Klecknera534a382013-12-19 02:14:12 +00001446 .addAttribute(Attribute::StructRet)
1447 .addAttribute(Attribute::InAlloca);
Bill Wendling57625a42013-01-25 23:09:36 +00001448
Pete Cooper2777d8872015-05-06 23:19:56 +00001449 return Incompatible;
Bill Wendling57625a42013-01-25 23:09:36 +00001450}
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001451
1452template<typename AttrClass>
1453static bool isEqual(const Function &Caller, const Function &Callee) {
1454 return Caller.getFnAttribute(AttrClass::getKind()) ==
1455 Callee.getFnAttribute(AttrClass::getKind());
1456}
1457
1458/// \brief Compute the logical AND of the attributes of the caller and the
1459/// callee.
1460///
1461/// This function sets the caller's attribute to false if the callee's attribute
1462/// is false.
1463template<typename AttrClass>
1464static void setAND(Function &Caller, const Function &Callee) {
1465 if (AttrClass::isSet(Caller, AttrClass::getKind()) &&
1466 !AttrClass::isSet(Callee, AttrClass::getKind()))
1467 AttrClass::set(Caller, AttrClass::getKind(), false);
1468}
1469
1470/// \brief Compute the logical OR of the attributes of the caller and the
1471/// callee.
1472///
1473/// This function sets the caller's attribute to true if the callee's attribute
1474/// is true.
1475template<typename AttrClass>
1476static void setOR(Function &Caller, const Function &Callee) {
1477 if (!AttrClass::isSet(Caller, AttrClass::getKind()) &&
1478 AttrClass::isSet(Callee, AttrClass::getKind()))
1479 AttrClass::set(Caller, AttrClass::getKind(), true);
1480}
1481
1482/// \brief If the inlined function had a higher stack protection level than the
1483/// calling function, then bump up the caller's stack protection level.
1484static void adjustCallerSSPLevel(Function &Caller, const Function &Callee) {
1485 // If upgrading the SSP attribute, clear out the old SSP Attributes first.
1486 // Having multiple SSP attributes doesn't actually hurt, but it adds useless
1487 // clutter to the IR.
1488 AttrBuilder B;
1489 B.addAttribute(Attribute::StackProtect)
1490 .addAttribute(Attribute::StackProtectStrong)
1491 .addAttribute(Attribute::StackProtectReq);
1492 AttributeSet OldSSPAttr = AttributeSet::get(Caller.getContext(),
1493 AttributeSet::FunctionIndex,
1494 B);
1495
Evgeniy Stepanovf17120a2016-04-11 22:27:48 +00001496 if (Callee.hasFnAttribute(Attribute::StackProtectReq)) {
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001497 Caller.removeAttributes(AttributeSet::FunctionIndex, OldSSPAttr);
1498 Caller.addFnAttr(Attribute::StackProtectReq);
1499 } else if (Callee.hasFnAttribute(Attribute::StackProtectStrong) &&
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001500 !Caller.hasFnAttribute(Attribute::StackProtectReq)) {
1501 Caller.removeAttributes(AttributeSet::FunctionIndex, OldSSPAttr);
1502 Caller.addFnAttr(Attribute::StackProtectStrong);
1503 } else if (Callee.hasFnAttribute(Attribute::StackProtect) &&
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001504 !Caller.hasFnAttribute(Attribute::StackProtectReq) &&
1505 !Caller.hasFnAttribute(Attribute::StackProtectStrong))
1506 Caller.addFnAttr(Attribute::StackProtect);
1507}
1508
1509#define GET_ATTR_COMPAT_FUNC
1510#include "AttributesCompatFunc.inc"
1511
1512bool AttributeFuncs::areInlineCompatible(const Function &Caller,
1513 const Function &Callee) {
1514 return hasCompatibleFnAttrs(Caller, Callee);
1515}
1516
1517
1518void AttributeFuncs::mergeAttributesForInlining(Function &Caller,
1519 const Function &Callee) {
1520 mergeFnAttrs(Caller, Callee);
1521}