blob: bc49b9300706b3cbdbe838480a68d0ee9fcab99d [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 IV4540ca32016-04-12 01:44:13 +000041LLVM_CONSTEXPR static 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";
Stephen Linb8bd2322013-04-20 05:14:40 +0000295 if (hasAttribute(Attribute::Returned))
296 return "returned";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000297 if (hasAttribute(Attribute::ReturnsTwice))
298 return "returns_twice";
299 if (hasAttribute(Attribute::SExt))
300 return "signext";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000301 if (hasAttribute(Attribute::StackProtect))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000302 return "ssp";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000303 if (hasAttribute(Attribute::StackProtectReq))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000304 return "sspreq";
Bill Wendlingd154e2832013-01-23 06:41:41 +0000305 if (hasAttribute(Attribute::StackProtectStrong))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000306 return "sspstrong";
Peter Collingbourne82437bf2015-06-15 21:07:11 +0000307 if (hasAttribute(Attribute::SafeStack))
308 return "safestack";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000309 if (hasAttribute(Attribute::StructRet))
310 return "sret";
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000311 if (hasAttribute(Attribute::SanitizeThread))
312 return "sanitize_thread";
313 if (hasAttribute(Attribute::SanitizeMemory))
314 return "sanitize_memory";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000315 if (hasAttribute(Attribute::UWTable))
316 return "uwtable";
317 if (hasAttribute(Attribute::ZExt))
318 return "zeroext";
Diego Novilloc6399532013-05-24 12:26:52 +0000319 if (hasAttribute(Attribute::Cold))
320 return "cold";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000321
322 // FIXME: These should be output like this:
323 //
324 // align=4
325 // alignstack=8
326 //
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000327 if (hasAttribute(Attribute::Alignment)) {
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000328 std::string Result;
Bill Wendling829b4782013-02-11 08:43:33 +0000329 Result += "align";
330 Result += (InAttrGrp) ? "=" : " ";
Bill Wendling3f12ac22013-02-05 22:37:24 +0000331 Result += utostr(getValueAsInt());
332 return Result;
333 }
Bill Wendling829b4782013-02-11 08:43:33 +0000334
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000335 auto AttrWithBytesToString = [&](const char *Name) {
Bill Wendling3f12ac22013-02-05 22:37:24 +0000336 std::string Result;
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000337 Result += Name;
Bill Wendling829b4782013-02-11 08:43:33 +0000338 if (InAttrGrp) {
339 Result += "=";
340 Result += utostr(getValueAsInt());
341 } else {
342 Result += "(";
343 Result += utostr(getValueAsInt());
344 Result += ")";
345 }
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000346 return Result;
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000347 };
Bill Wendling9c2eba92013-01-31 20:59:05 +0000348
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000349 if (hasAttribute(Attribute::StackAlignment))
350 return AttrWithBytesToString("alignstack");
351
352 if (hasAttribute(Attribute::Dereferenceable))
353 return AttrWithBytesToString("dereferenceable");
354
355 if (hasAttribute(Attribute::DereferenceableOrNull))
356 return AttrWithBytesToString("dereferenceable_or_null");
Hal Finkelb0407ba2014-07-18 15:51:28 +0000357
George Burgess IV278199f2016-04-12 01:05:35 +0000358 if (hasAttribute(Attribute::AllocSize)) {
359 unsigned ElemSize;
360 Optional<unsigned> NumElems;
361 std::tie(ElemSize, NumElems) = getAllocSizeArgs();
362
363 std::string Result = "allocsize(";
364 Result += utostr(ElemSize);
365 if (NumElems.hasValue()) {
366 Result += ',';
367 Result += utostr(*NumElems);
368 }
369 Result += ')';
370 return Result;
371 }
372
Bill Wendling9c2eba92013-01-31 20:59:05 +0000373 // Convert target-dependent attributes to strings of the form:
374 //
375 // "kind"
376 // "kind" = "value"
Bill Wendling9c2eba92013-01-31 20:59:05 +0000377 //
Bill Wendling3f12ac22013-02-05 22:37:24 +0000378 if (isStringAttribute()) {
Bill Wendling9c2eba92013-01-31 20:59:05 +0000379 std::string Result;
Yaron Keren075759a2015-03-30 15:42:36 +0000380 Result += (Twine('"') + getKindAsString() + Twine('"')).str();
Bill Wendling9c2eba92013-01-31 20:59:05 +0000381
Bill Wendling3f12ac22013-02-05 22:37:24 +0000382 StringRef Val = pImpl->getValueAsString();
383 if (Val.empty()) return Result;
Bill Wendling7a33f772013-02-01 22:32:30 +0000384
Yaron Keren075759a2015-03-30 15:42:36 +0000385 Result += ("=\"" + Val + Twine('"')).str();
Bill Wendlingc79cdff2013-02-01 01:04:27 +0000386 return Result;
Bill Wendling9c2eba92013-01-31 20:59:05 +0000387 }
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000388
389 llvm_unreachable("Unknown attribute");
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000390}
391
Bill Wendlingd509a662013-01-29 00:34:06 +0000392bool Attribute::operator<(Attribute A) const {
393 if (!pImpl && !A.pImpl) return false;
394 if (!pImpl) return true;
395 if (!A.pImpl) return false;
396 return *pImpl < *A.pImpl;
397}
398
Bill Wendlingd509a662013-01-29 00:34:06 +0000399//===----------------------------------------------------------------------===//
400// AttributeImpl Definition
401//===----------------------------------------------------------------------===//
402
Eric Christopher0eaa5412014-07-02 22:05:40 +0000403// Pin the vtables to this file.
Alexey Samsonov49109a22013-11-18 09:31:53 +0000404AttributeImpl::~AttributeImpl() {}
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +0000405void EnumAttributeImpl::anchor() {}
Hal Finkele15442c2014-07-18 06:51:55 +0000406void IntAttributeImpl::anchor() {}
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +0000407void StringAttributeImpl::anchor() {}
Alexey Samsonov49109a22013-11-18 09:31:53 +0000408
Bill Wendlingd509a662013-01-29 00:34:06 +0000409bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
Bill Wendling3f12ac22013-02-05 22:37:24 +0000410 if (isStringAttribute()) return false;
411 return getKindAsEnum() == A;
Bill Wendlingd509a662013-01-29 00:34:06 +0000412}
413
Bill Wendling3f12ac22013-02-05 22:37:24 +0000414bool AttributeImpl::hasAttribute(StringRef Kind) const {
415 if (!isStringAttribute()) return false;
416 return getKindAsString() == Kind;
Bill Wendlingd509a662013-01-29 00:34:06 +0000417}
418
Bill Wendling3f12ac22013-02-05 22:37:24 +0000419Attribute::AttrKind AttributeImpl::getKindAsEnum() const {
Hal Finkele15442c2014-07-18 06:51:55 +0000420 assert(isEnumAttribute() || isIntAttribute());
Benjamin Kramer741146b2013-07-11 12:13:16 +0000421 return static_cast<const EnumAttributeImpl *>(this)->getEnumKind();
Bill Wendlingd509a662013-01-29 00:34:06 +0000422}
423
Bill Wendling3f12ac22013-02-05 22:37:24 +0000424uint64_t AttributeImpl::getValueAsInt() const {
Hal Finkele15442c2014-07-18 06:51:55 +0000425 assert(isIntAttribute());
426 return static_cast<const IntAttributeImpl *>(this)->getValue();
Bill Wendlingd509a662013-01-29 00:34:06 +0000427}
428
Bill Wendling3f12ac22013-02-05 22:37:24 +0000429StringRef AttributeImpl::getKindAsString() const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000430 assert(isStringAttribute());
431 return static_cast<const StringAttributeImpl *>(this)->getStringKind();
Bill Wendlingd509a662013-01-29 00:34:06 +0000432}
433
Bill Wendling3f12ac22013-02-05 22:37:24 +0000434StringRef AttributeImpl::getValueAsString() const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000435 assert(isStringAttribute());
436 return static_cast<const StringAttributeImpl *>(this)->getStringValue();
Bill Wendlingd509a662013-01-29 00:34:06 +0000437}
438
439bool AttributeImpl::operator<(const AttributeImpl &AI) const {
Bill Wendlingc79cdff2013-02-01 01:04:27 +0000440 // This sorts the attributes with Attribute::AttrKinds coming first (sorted
441 // relative to their enum value) and then strings.
Bill Wendling26b95752013-02-15 05:25:26 +0000442 if (isEnumAttribute()) {
443 if (AI.isEnumAttribute()) return getKindAsEnum() < AI.getKindAsEnum();
Hal Finkele15442c2014-07-18 06:51:55 +0000444 if (AI.isIntAttribute()) return true;
Bill Wendling26b95752013-02-15 05:25:26 +0000445 if (AI.isStringAttribute()) return true;
446 }
Bill Wendlingc79cdff2013-02-01 01:04:27 +0000447
Hal Finkele15442c2014-07-18 06:51:55 +0000448 if (isIntAttribute()) {
Bill Wendling26b95752013-02-15 05:25:26 +0000449 if (AI.isEnumAttribute()) return false;
Reid Kleckner7de67612016-04-04 23:06:05 +0000450 if (AI.isIntAttribute()) {
451 if (getKindAsEnum() == AI.getKindAsEnum())
452 return getValueAsInt() < AI.getValueAsInt();
453 return getKindAsEnum() < AI.getKindAsEnum();
454 }
Bill Wendling26b95752013-02-15 05:25:26 +0000455 if (AI.isStringAttribute()) return true;
Bill Wendling3f12ac22013-02-05 22:37:24 +0000456 }
Bill Wendlingd509a662013-01-29 00:34:06 +0000457
Bill Wendling26b95752013-02-15 05:25:26 +0000458 if (AI.isEnumAttribute()) return false;
Hal Finkele15442c2014-07-18 06:51:55 +0000459 if (AI.isIntAttribute()) return false;
Bill Wendling26b95752013-02-15 05:25:26 +0000460 if (getKindAsString() == AI.getKindAsString())
461 return getValueAsString() < AI.getValueAsString();
462 return getKindAsString() < AI.getKindAsString();
Bill Wendlingd509a662013-01-29 00:34:06 +0000463}
464
Bill Wendlingd509a662013-01-29 00:34:06 +0000465uint64_t AttributeImpl::getAttrMask(Attribute::AttrKind Val) {
466 // FIXME: Remove this.
467 switch (Val) {
468 case Attribute::EndAttrKinds:
Bill Wendlingd509a662013-01-29 00:34:06 +0000469 llvm_unreachable("Synthetic enumerators which should never get here");
470
471 case Attribute::None: return 0;
472 case Attribute::ZExt: return 1 << 0;
473 case Attribute::SExt: return 1 << 1;
474 case Attribute::NoReturn: return 1 << 2;
475 case Attribute::InReg: return 1 << 3;
476 case Attribute::StructRet: return 1 << 4;
477 case Attribute::NoUnwind: return 1 << 5;
478 case Attribute::NoAlias: return 1 << 6;
479 case Attribute::ByVal: return 1 << 7;
480 case Attribute::Nest: return 1 << 8;
481 case Attribute::ReadNone: return 1 << 9;
482 case Attribute::ReadOnly: return 1 << 10;
483 case Attribute::NoInline: return 1 << 11;
484 case Attribute::AlwaysInline: return 1 << 12;
485 case Attribute::OptimizeForSize: return 1 << 13;
486 case Attribute::StackProtect: return 1 << 14;
487 case Attribute::StackProtectReq: return 1 << 15;
488 case Attribute::Alignment: return 31 << 16;
489 case Attribute::NoCapture: return 1 << 21;
490 case Attribute::NoRedZone: return 1 << 22;
491 case Attribute::NoImplicitFloat: return 1 << 23;
492 case Attribute::Naked: return 1 << 24;
493 case Attribute::InlineHint: return 1 << 25;
494 case Attribute::StackAlignment: return 7 << 26;
495 case Attribute::ReturnsTwice: return 1 << 29;
496 case Attribute::UWTable: return 1 << 30;
497 case Attribute::NonLazyBind: return 1U << 31;
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000498 case Attribute::SanitizeAddress: return 1ULL << 32;
Bill Wendlingd509a662013-01-29 00:34:06 +0000499 case Attribute::MinSize: return 1ULL << 33;
500 case Attribute::NoDuplicate: return 1ULL << 34;
501 case Attribute::StackProtectStrong: return 1ULL << 35;
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000502 case Attribute::SanitizeThread: return 1ULL << 36;
503 case Attribute::SanitizeMemory: return 1ULL << 37;
Bill Wendling7bfdfe72013-02-22 00:40:12 +0000504 case Attribute::NoBuiltin: return 1ULL << 38;
Stephen Linb8bd2322013-04-20 05:14:40 +0000505 case Attribute::Returned: return 1ULL << 39;
Diego Novilloc6399532013-05-24 12:26:52 +0000506 case Attribute::Cold: return 1ULL << 40;
Michael Gottesman41748d72013-06-27 00:25:01 +0000507 case Attribute::Builtin: return 1ULL << 41;
Andrea Di Biagio377496b2013-08-23 11:53:55 +0000508 case Attribute::OptimizeNone: return 1ULL << 42;
Reid Klecknera534a382013-12-19 02:14:12 +0000509 case Attribute::InAlloca: return 1ULL << 43;
Nick Lewyckyd52b1522014-05-20 01:23:40 +0000510 case Attribute::NonNull: return 1ULL << 44;
Tom Roeder44cb65f2014-06-05 19:29:43 +0000511 case Attribute::JumpTable: return 1ULL << 45;
Owen Anderson85fa7d52015-05-26 23:48:40 +0000512 case Attribute::Convergent: return 1ULL << 46;
Peter Collingbourne82437bf2015-06-15 21:07:11 +0000513 case Attribute::SafeStack: return 1ULL << 47;
James Molloye6f87ca2015-11-06 10:32:53 +0000514 case Attribute::NoRecurse: return 1ULL << 48;
Vaivaswatha Nagarajfb3f4902015-12-16 16:16:19 +0000515 case Attribute::InaccessibleMemOnly: return 1ULL << 49;
516 case Attribute::InaccessibleMemOrArgMemOnly: return 1ULL << 50;
Manman Renf46262e2016-03-29 17:37:21 +0000517 case Attribute::SwiftSelf: return 1ULL << 51;
Manman Ren9bfd0d02016-04-01 21:41:15 +0000518 case Attribute::SwiftError: return 1ULL << 52;
Hal Finkelb0407ba2014-07-18 15:51:28 +0000519 case Attribute::Dereferenceable:
520 llvm_unreachable("dereferenceable attribute not supported in raw format");
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000521 break;
522 case Attribute::DereferenceableOrNull:
523 llvm_unreachable("dereferenceable_or_null attribute not supported in raw "
524 "format");
525 break;
Igor Laevsky39d662f2015-07-11 10:30:36 +0000526 case Attribute::ArgMemOnly:
527 llvm_unreachable("argmemonly attribute not supported in raw format");
528 break;
George Burgess IV278199f2016-04-12 01:05:35 +0000529 case Attribute::AllocSize:
530 llvm_unreachable("allocsize not supported in raw format");
531 break;
Bill Wendling25342e12013-02-22 00:50:09 +0000532 }
533 llvm_unreachable("Unsupported attribute type");
Bill Wendlingd509a662013-01-29 00:34:06 +0000534}
535
536//===----------------------------------------------------------------------===//
537// AttributeSetNode Definition
538//===----------------------------------------------------------------------===//
539
540AttributeSetNode *AttributeSetNode::get(LLVMContext &C,
541 ArrayRef<Attribute> Attrs) {
542 if (Attrs.empty())
Craig Topperc6207612014-04-09 06:08:46 +0000543 return nullptr;
Bill Wendlingd509a662013-01-29 00:34:06 +0000544
545 // Otherwise, build a key to look up the existing attributes.
546 LLVMContextImpl *pImpl = C.pImpl;
547 FoldingSetNodeID ID;
548
549 SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end());
Reid Kleckner7de67612016-04-04 23:06:05 +0000550 std::sort(SortedAttrs.begin(), SortedAttrs.end());
Bill Wendlingd509a662013-01-29 00:34:06 +0000551
George Burgess IV500d3032015-12-16 05:21:02 +0000552 for (Attribute Attr : SortedAttrs)
553 Attr.Profile(ID);
Bill Wendlingd509a662013-01-29 00:34:06 +0000554
555 void *InsertPoint;
556 AttributeSetNode *PA =
557 pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint);
558
559 // If we didn't find any existing attributes of the same shape then create a
560 // new one and insert it.
561 if (!PA) {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000562 // Coallocate entries after the AttributeSetNode itself.
James Y Knightaa365b22015-08-05 22:57:34 +0000563 void *Mem = ::operator new(totalSizeToAlloc<Attribute>(SortedAttrs.size()));
Benjamin Kramer741146b2013-07-11 12:13:16 +0000564 PA = new (Mem) AttributeSetNode(SortedAttrs);
Bill Wendlingd509a662013-01-29 00:34:06 +0000565 pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint);
566 }
567
568 // Return the AttributesListNode that we found or created.
569 return PA;
570}
571
Bill Wendlingbce7b972013-02-13 08:42:21 +0000572bool AttributeSetNode::hasAttribute(StringRef Kind) const {
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000573 for (Attribute I : *this)
574 if (I.hasAttribute(Kind))
Bill Wendlingbce7b972013-02-13 08:42:21 +0000575 return true;
576 return false;
577}
578
579Attribute AttributeSetNode::getAttribute(Attribute::AttrKind Kind) const {
Matthias Braun31eeb76f2016-01-29 22:25:13 +0000580 if (hasAttribute(Kind)) {
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000581 for (Attribute I : *this)
582 if (I.hasAttribute(Kind))
583 return I;
Matthias Braun31eeb76f2016-01-29 22:25:13 +0000584 }
Bill Wendlingbce7b972013-02-13 08:42:21 +0000585 return Attribute();
586}
587
588Attribute AttributeSetNode::getAttribute(StringRef Kind) const {
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000589 for (Attribute I : *this)
590 if (I.hasAttribute(Kind))
591 return I;
Bill Wendlingbce7b972013-02-13 08:42:21 +0000592 return Attribute();
593}
594
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000595unsigned AttributeSetNode::getAlignment() const {
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000596 for (Attribute I : *this)
597 if (I.hasAttribute(Attribute::Alignment))
598 return I.getAlignment();
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000599 return 0;
600}
601
602unsigned AttributeSetNode::getStackAlignment() const {
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000603 for (Attribute I : *this)
604 if (I.hasAttribute(Attribute::StackAlignment))
605 return I.getStackAlignment();
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000606 return 0;
607}
608
Hal Finkelb0407ba2014-07-18 15:51:28 +0000609uint64_t AttributeSetNode::getDereferenceableBytes() const {
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000610 for (Attribute I : *this)
611 if (I.hasAttribute(Attribute::Dereferenceable))
612 return I.getDereferenceableBytes();
Hal Finkelb0407ba2014-07-18 15:51:28 +0000613 return 0;
614}
615
Sanjoy Das06cf33f2015-05-06 17:41:54 +0000616uint64_t AttributeSetNode::getDereferenceableOrNullBytes() const {
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000617 for (Attribute I : *this)
618 if (I.hasAttribute(Attribute::DereferenceableOrNull))
619 return I.getDereferenceableOrNullBytes();
Sanjoy Das06cf33f2015-05-06 17:41:54 +0000620 return 0;
621}
622
George Burgess IV278199f2016-04-12 01:05:35 +0000623std::pair<unsigned, Optional<unsigned>>
624AttributeSetNode::getAllocSizeArgs() const {
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000625 for (Attribute I : *this)
626 if (I.hasAttribute(Attribute::AllocSize))
627 return I.getAllocSizeArgs();
George Burgess IV278199f2016-04-12 01:05:35 +0000628 return std::make_pair(0, 0);
629}
630
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +0000631std::string AttributeSetNode::getAsString(bool InAttrGrp) const {
Benjamin Kramer0baf8f42013-04-19 11:43:21 +0000632 std::string Str;
Benjamin Kramer741146b2013-07-11 12:13:16 +0000633 for (iterator I = begin(), E = end(); I != E; ++I) {
634 if (I != begin())
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +0000635 Str += ' ';
636 Str += I->getAsString(InAttrGrp);
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000637 }
638 return Str;
639}
640
Bill Wendlingd509a662013-01-29 00:34:06 +0000641//===----------------------------------------------------------------------===//
642// AttributeSetImpl Definition
643//===----------------------------------------------------------------------===//
644
Rafael Espindoladd275302013-04-30 16:53:38 +0000645uint64_t AttributeSetImpl::Raw(unsigned Index) const {
Amaury Sechet24c84fd2016-06-14 22:04:16 +0000646 for (unsigned I = 0, E = getNumSlots(); I != E; ++I) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000647 if (getSlotIndex(I) != Index) continue;
Benjamin Kramer741146b2013-07-11 12:13:16 +0000648 const AttributeSetNode *ASN = getSlotNode(I);
Bill Wendling91226182013-02-02 00:52:44 +0000649 uint64_t Mask = 0;
Bill Wendlingd509a662013-01-29 00:34:06 +0000650
Benjamin Kramer741146b2013-07-11 12:13:16 +0000651 for (AttributeSetNode::iterator II = ASN->begin(),
Bill Wendling91226182013-02-02 00:52:44 +0000652 IE = ASN->end(); II != IE; ++II) {
653 Attribute Attr = *II;
Bill Wendlingd746e402013-02-10 23:18:05 +0000654
655 // This cannot handle string attributes.
656 if (Attr.isStringAttribute()) continue;
657
Bill Wendling3f12ac22013-02-05 22:37:24 +0000658 Attribute::AttrKind Kind = Attr.getKindAsEnum();
Bill Wendling91226182013-02-02 00:52:44 +0000659
Bill Wendling3f12ac22013-02-05 22:37:24 +0000660 if (Kind == Attribute::Alignment)
Bill Wendling91226182013-02-02 00:52:44 +0000661 Mask |= (Log2_32(ASN->getAlignment()) + 1) << 16;
Bill Wendling3f12ac22013-02-05 22:37:24 +0000662 else if (Kind == Attribute::StackAlignment)
Bill Wendling91226182013-02-02 00:52:44 +0000663 Mask |= (Log2_32(ASN->getStackAlignment()) + 1) << 26;
Hal Finkelb0407ba2014-07-18 15:51:28 +0000664 else if (Kind == Attribute::Dereferenceable)
665 llvm_unreachable("dereferenceable not supported in bit mask");
George Burgess IV278199f2016-04-12 01:05:35 +0000666 else if (Kind == Attribute::AllocSize)
667 llvm_unreachable("allocsize not supported in bit mask");
Bill Wendling91226182013-02-02 00:52:44 +0000668 else
Bill Wendling3f12ac22013-02-05 22:37:24 +0000669 Mask |= AttributeImpl::getAttrMask(Kind);
Bill Wendling91226182013-02-02 00:52:44 +0000670 }
671
672 return Mask;
Bill Wendlingd509a662013-01-29 00:34:06 +0000673 }
674
675 return 0;
676}
677
Yaron Kereneb2a2542016-01-29 20:50:44 +0000678LLVM_DUMP_METHOD void AttributeSetImpl::dump() const {
Peter Collingbourneabca2ec2013-08-02 22:34:30 +0000679 AttributeSet(const_cast<AttributeSetImpl *>(this)).dump();
680}
681
Bill Wendlingd509a662013-01-29 00:34:06 +0000682//===----------------------------------------------------------------------===//
683// AttributeSet Construction and Mutation Methods
684//===----------------------------------------------------------------------===//
685
Bill Wendling60011b82013-01-29 01:43:29 +0000686AttributeSet
687AttributeSet::getImpl(LLVMContext &C,
688 ArrayRef<std::pair<unsigned, AttributeSetNode*> > Attrs) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000689 LLVMContextImpl *pImpl = C.pImpl;
690 FoldingSetNodeID ID;
691 AttributeSetImpl::Profile(ID, Attrs);
692
693 void *InsertPoint;
694 AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
695
696 // If we didn't find any existing attributes of the same shape then
697 // create a new one and insert it.
698 if (!PA) {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000699 // Coallocate entries after the AttributeSetImpl itself.
James Y Knightaa365b22015-08-05 22:57:34 +0000700 void *Mem = ::operator new(
701 AttributeSetImpl::totalSizeToAlloc<IndexAttrPair>(Attrs.size()));
Benjamin Kramer741146b2013-07-11 12:13:16 +0000702 PA = new (Mem) AttributeSetImpl(C, Attrs);
Bill Wendlingd509a662013-01-29 00:34:06 +0000703 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
704 }
705
706 // Return the AttributesList that we found or created.
707 return AttributeSet(PA);
708}
709
710AttributeSet AttributeSet::get(LLVMContext &C,
711 ArrayRef<std::pair<unsigned, Attribute> > Attrs){
712 // If there are no attributes then return a null AttributesList pointer.
713 if (Attrs.empty())
714 return AttributeSet();
715
Craig Toppere30b8ca2016-01-03 19:43:40 +0000716 assert(std::is_sorted(Attrs.begin(), Attrs.end(),
717 [](const std::pair<unsigned, Attribute> &LHS,
718 const std::pair<unsigned, Attribute> &RHS) {
719 return LHS.first < RHS.first;
720 }) && "Misordered Attributes list!");
721 assert(std::none_of(Attrs.begin(), Attrs.end(),
722 [](const std::pair<unsigned, Attribute> &Pair) {
723 return Pair.second.hasAttribute(Attribute::None);
724 }) && "Pointless attribute!");
Bill Wendlingd509a662013-01-29 00:34:06 +0000725
726 // Create a vector if (unsigned, AttributeSetNode*) pairs from the attributes
727 // list.
728 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrPairVec;
729 for (ArrayRef<std::pair<unsigned, Attribute> >::iterator I = Attrs.begin(),
730 E = Attrs.end(); I != E; ) {
731 unsigned Index = I->first;
732 SmallVector<Attribute, 4> AttrVec;
NAKAMURA Takumif05d2f22013-01-29 15:18:16 +0000733 while (I != E && I->first == Index) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000734 AttrVec.push_back(I->second);
735 ++I;
736 }
737
738 AttrPairVec.push_back(std::make_pair(Index,
739 AttributeSetNode::get(C, AttrVec)));
740 }
741
742 return getImpl(C, AttrPairVec);
743}
744
745AttributeSet AttributeSet::get(LLVMContext &C,
746 ArrayRef<std::pair<unsigned,
747 AttributeSetNode*> > Attrs) {
748 // If there are no attributes then return a null AttributesList pointer.
749 if (Attrs.empty())
750 return AttributeSet();
751
752 return getImpl(C, Attrs);
753}
754
David Majnemercf63a792014-05-03 23:00:35 +0000755AttributeSet AttributeSet::get(LLVMContext &C, unsigned Index,
756 const AttrBuilder &B) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000757 if (!B.hasAttributes())
758 return AttributeSet();
Bill Wendlingf7134812013-01-29 01:02:03 +0000759
Bill Wendlingae89a0f2013-02-05 23:48:36 +0000760 // Add target-independent attributes.
Bill Wendlingf7134812013-01-29 01:02:03 +0000761 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
Benjamin Kramer45e7d532013-02-16 19:13:18 +0000762 for (Attribute::AttrKind Kind = Attribute::None;
Benjamin Kramer6f37dac2013-02-16 19:22:28 +0000763 Kind != Attribute::EndAttrKinds; Kind = Attribute::AttrKind(Kind + 1)) {
Benjamin Kramer45e7d532013-02-16 19:13:18 +0000764 if (!B.contains(Kind))
765 continue;
766
George Burgess IV500d3032015-12-16 05:21:02 +0000767 Attribute Attr;
768 switch (Kind) {
769 case Attribute::Alignment:
770 Attr = Attribute::getWithAlignment(C, B.getAlignment());
771 break;
772 case Attribute::StackAlignment:
773 Attr = Attribute::getWithStackAlignment(C, B.getStackAlignment());
774 break;
775 case Attribute::Dereferenceable:
776 Attr = Attribute::getWithDereferenceableBytes(
777 C, B.getDereferenceableBytes());
778 break;
779 case Attribute::DereferenceableOrNull:
780 Attr = Attribute::getWithDereferenceableOrNullBytes(
781 C, B.getDereferenceableOrNullBytes());
782 break;
George Burgess IV278199f2016-04-12 01:05:35 +0000783 case Attribute::AllocSize: {
784 auto A = B.getAllocSizeArgs();
785 Attr = Attribute::getWithAllocSizeArgs(C, A.first, A.second);
786 break;
787 }
George Burgess IV500d3032015-12-16 05:21:02 +0000788 default:
789 Attr = Attribute::get(C, Kind);
790 }
791 Attrs.push_back(std::make_pair(Index, Attr));
Bill Wendlingf7134812013-01-29 01:02:03 +0000792 }
793
Bill Wendlingae89a0f2013-02-05 23:48:36 +0000794 // Add target-dependent (string) attributes.
Benjamin Kramerea76b6f2016-06-12 19:02:34 +0000795 for (const auto &TDA : B.td_attrs())
David Majnemercf63a792014-05-03 23:00:35 +0000796 Attrs.push_back(
797 std::make_pair(Index, Attribute::get(C, TDA.first, TDA.second)));
Bill Wendlingae89a0f2013-02-05 23:48:36 +0000798
Bill Wendlingf7134812013-01-29 01:02:03 +0000799 return get(C, Attrs);
Bill Wendlingd509a662013-01-29 00:34:06 +0000800}
801
Bill Wendling211316c2013-04-18 20:17:28 +0000802AttributeSet AttributeSet::get(LLVMContext &C, unsigned Index,
Amaury Sechet392638d2016-06-14 20:27:35 +0000803 ArrayRef<Attribute::AttrKind> Kinds) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000804 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
Amaury Sechet392638d2016-06-14 20:27:35 +0000805 for (Attribute::AttrKind K : Kinds)
George Burgess IV500d3032015-12-16 05:21:02 +0000806 Attrs.push_back(std::make_pair(Index, Attribute::get(C, K)));
Bill Wendlingd509a662013-01-29 00:34:06 +0000807 return get(C, Attrs);
808}
809
Amaury Sechet6100adf2016-06-15 17:50:39 +0000810AttributeSet AttributeSet::get(LLVMContext &C, unsigned Index,
811 ArrayRef<StringRef> Kinds) {
812 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
813 for (StringRef K : Kinds)
814 Attrs.push_back(std::make_pair(Index, Attribute::get(C, K)));
815 return get(C, Attrs);
816}
817
Bill Wendlingd509a662013-01-29 00:34:06 +0000818AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<AttributeSet> Attrs) {
819 if (Attrs.empty()) return AttributeSet();
Peter Collingbournebd6c7452013-08-02 22:29:40 +0000820 if (Attrs.size() == 1) return Attrs[0];
Bill Wendlingd509a662013-01-29 00:34:06 +0000821
822 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrNodeVec;
Peter Collingbournebd6c7452013-08-02 22:29:40 +0000823 AttributeSetImpl *A0 = Attrs[0].pImpl;
824 if (A0)
Amaury Sechet24c84fd2016-06-14 22:04:16 +0000825 AttrNodeVec.append(A0->getNode(0), A0->getNode(A0->getNumSlots()));
Peter Collingbournebd6c7452013-08-02 22:29:40 +0000826 // Copy all attributes from Attrs into AttrNodeVec while keeping AttrNodeVec
827 // ordered by index. Because we know that each list in Attrs is ordered by
828 // index we only need to merge each successive list in rather than doing a
829 // full sort.
830 for (unsigned I = 1, E = Attrs.size(); I != E; ++I) {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000831 AttributeSetImpl *AS = Attrs[I].pImpl;
832 if (!AS) continue;
Peter Collingbournebd6c7452013-08-02 22:29:40 +0000833 SmallVector<std::pair<unsigned, AttributeSetNode *>, 8>::iterator
834 ANVI = AttrNodeVec.begin(), ANVE;
James Y Knightaa365b22015-08-05 22:57:34 +0000835 for (const IndexAttrPair *AI = AS->getNode(0),
Amaury Sechet24c84fd2016-06-14 22:04:16 +0000836 *AE = AS->getNode(AS->getNumSlots());
Peter Collingbournebd6c7452013-08-02 22:29:40 +0000837 AI != AE; ++AI) {
838 ANVE = AttrNodeVec.end();
839 while (ANVI != ANVE && ANVI->first <= AI->first)
840 ++ANVI;
841 ANVI = AttrNodeVec.insert(ANVI, *AI) + 1;
842 }
Bill Wendlingd509a662013-01-29 00:34:06 +0000843 }
844
845 return getImpl(C, AttrNodeVec);
846}
847
Bill Wendling211316c2013-04-18 20:17:28 +0000848AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
Amaury Sechet392638d2016-06-14 20:27:35 +0000849 Attribute::AttrKind Kind) const {
850 if (hasAttribute(Index, Kind)) return *this;
851 return addAttributes(C, Index, AttributeSet::get(C, Index, Kind));
Reed Kotler795c7b42013-03-13 20:20:08 +0000852}
853
Bill Wendling3b2f6102013-07-25 18:34:24 +0000854AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
855 StringRef Kind, StringRef Value) const {
856 llvm::AttrBuilder B;
857 B.addAttribute(Kind, Value);
858 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
859}
860
Akira Hatanaka237916b2015-12-02 06:58:49 +0000861AttributeSet AttributeSet::addAttribute(LLVMContext &C,
862 ArrayRef<unsigned> Indices,
863 Attribute A) const {
Amaury Sechet24c84fd2016-06-14 22:04:16 +0000864 unsigned I = 0, E = pImpl ? pImpl->getNumSlots() : 0;
Akira Hatanaka237916b2015-12-02 06:58:49 +0000865 auto IdxI = Indices.begin(), IdxE = Indices.end();
866 SmallVector<AttributeSet, 4> AttrSet;
867
868 while (I != E && IdxI != IdxE) {
869 if (getSlotIndex(I) < *IdxI)
870 AttrSet.emplace_back(getSlotAttributes(I++));
871 else if (getSlotIndex(I) > *IdxI)
872 AttrSet.emplace_back(AttributeSet::get(C, std::make_pair(*IdxI++, A)));
873 else {
874 AttrBuilder B(getSlotAttributes(I), *IdxI);
875 B.addAttribute(A);
876 AttrSet.emplace_back(AttributeSet::get(C, *IdxI, B));
877 ++I;
878 ++IdxI;
879 }
880 }
881
882 while (I != E)
883 AttrSet.emplace_back(getSlotAttributes(I++));
884
885 while (IdxI != IdxE)
886 AttrSet.emplace_back(AttributeSet::get(C, std::make_pair(*IdxI++, A)));
887
888 return get(C, AttrSet);
889}
890
Bill Wendling211316c2013-04-18 20:17:28 +0000891AttributeSet AttributeSet::addAttributes(LLVMContext &C, unsigned Index,
Bill Wendlingd509a662013-01-29 00:34:06 +0000892 AttributeSet Attrs) const {
893 if (!pImpl) return Attrs;
894 if (!Attrs.pImpl) return *this;
895
896#ifndef NDEBUG
897 // FIXME it is not obvious how this should work for alignment. For now, say
898 // we can't change a known alignment.
Bill Wendling211316c2013-04-18 20:17:28 +0000899 unsigned OldAlign = getParamAlignment(Index);
900 unsigned NewAlign = Attrs.getParamAlignment(Index);
Bill Wendlingd509a662013-01-29 00:34:06 +0000901 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
902 "Attempt to change alignment!");
903#endif
904
905 // Add the attribute slots before the one we're trying to add.
906 SmallVector<AttributeSet, 4> AttrSet;
Amaury Sechet24c84fd2016-06-14 22:04:16 +0000907 uint64_t NumAttrs = pImpl->getNumSlots();
Bill Wendlingd509a662013-01-29 00:34:06 +0000908 AttributeSet AS;
909 uint64_t LastIndex = 0;
910 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
Bill Wendling211316c2013-04-18 20:17:28 +0000911 if (getSlotIndex(I) >= Index) {
912 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
Bill Wendlingd509a662013-01-29 00:34:06 +0000913 break;
914 }
915 LastIndex = I + 1;
916 AttrSet.push_back(getSlotAttributes(I));
917 }
918
919 // Now add the attribute into the correct slot. There may already be an
920 // AttributeSet there.
Bill Wendling211316c2013-04-18 20:17:28 +0000921 AttrBuilder B(AS, Index);
Bill Wendlingd509a662013-01-29 00:34:06 +0000922
Amaury Sechet24c84fd2016-06-14 22:04:16 +0000923 for (unsigned I = 0, E = Attrs.pImpl->getNumSlots(); I != E; ++I)
Bill Wendling211316c2013-04-18 20:17:28 +0000924 if (Attrs.getSlotIndex(I) == Index) {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000925 for (AttributeSetImpl::iterator II = Attrs.pImpl->begin(I),
Bill Wendlingd509a662013-01-29 00:34:06 +0000926 IE = Attrs.pImpl->end(I); II != IE; ++II)
Bill Wendling23804da2013-01-31 23:38:01 +0000927 B.addAttribute(*II);
Bill Wendlingd509a662013-01-29 00:34:06 +0000928 break;
929 }
930
Bill Wendling211316c2013-04-18 20:17:28 +0000931 AttrSet.push_back(AttributeSet::get(C, Index, B));
Bill Wendlingd509a662013-01-29 00:34:06 +0000932
933 // Add the remaining attribute slots.
934 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
935 AttrSet.push_back(getSlotAttributes(I));
936
937 return get(C, AttrSet);
938}
939
Bill Wendling211316c2013-04-18 20:17:28 +0000940AttributeSet AttributeSet::removeAttribute(LLVMContext &C, unsigned Index,
Amaury Sechet392638d2016-06-14 20:27:35 +0000941 Attribute::AttrKind Kind) const {
942 if (!hasAttribute(Index, Kind)) return *this;
943 return removeAttributes(C, Index, AttributeSet::get(C, Index, Kind));
Bill Wendlingd509a662013-01-29 00:34:06 +0000944}
945
Amaury Sechet6100adf2016-06-15 17:50:39 +0000946AttributeSet AttributeSet::removeAttribute(LLVMContext &C, unsigned Index,
947 StringRef Kind) const {
948 if (!hasAttribute(Index, Kind)) return *this;
949 return removeAttributes(C, Index, AttributeSet::get(C, Index, Kind));
950}
951
Bill Wendling211316c2013-04-18 20:17:28 +0000952AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Index,
Bill Wendlingd509a662013-01-29 00:34:06 +0000953 AttributeSet Attrs) const {
954 if (!pImpl) return AttributeSet();
955 if (!Attrs.pImpl) return *this;
956
Pete Cooper67cf9a72015-11-19 05:56:52 +0000957 // FIXME it is not obvious how this should work for alignment.
958 // For now, say we can't pass in alignment, which no current use does.
959 assert(!Attrs.hasAttribute(Index, Attribute::Alignment) &&
960 "Attempt to change alignment!");
961
Bill Wendlingd509a662013-01-29 00:34:06 +0000962 // Add the attribute slots before the one we're trying to add.
963 SmallVector<AttributeSet, 4> AttrSet;
Amaury Sechet24c84fd2016-06-14 22:04:16 +0000964 uint64_t NumAttrs = pImpl->getNumSlots();
Bill Wendlingd509a662013-01-29 00:34:06 +0000965 AttributeSet AS;
966 uint64_t LastIndex = 0;
967 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
Bill Wendling211316c2013-04-18 20:17:28 +0000968 if (getSlotIndex(I) >= Index) {
969 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
Bill Wendlingd509a662013-01-29 00:34:06 +0000970 break;
971 }
972 LastIndex = I + 1;
973 AttrSet.push_back(getSlotAttributes(I));
974 }
975
Bill Wendlingd2196752013-01-30 23:07:40 +0000976 // Now remove the attribute from the correct slot. There may already be an
Bill Wendlingd509a662013-01-29 00:34:06 +0000977 // AttributeSet there.
Bill Wendling211316c2013-04-18 20:17:28 +0000978 AttrBuilder B(AS, Index);
Bill Wendlingd509a662013-01-29 00:34:06 +0000979
Amaury Sechet24c84fd2016-06-14 22:04:16 +0000980 for (unsigned I = 0, E = Attrs.pImpl->getNumSlots(); I != E; ++I)
Bill Wendling211316c2013-04-18 20:17:28 +0000981 if (Attrs.getSlotIndex(I) == Index) {
982 B.removeAttributes(Attrs.pImpl->getSlotAttributes(I), Index);
Bill Wendlingd509a662013-01-29 00:34:06 +0000983 break;
984 }
985
Bill Wendling211316c2013-04-18 20:17:28 +0000986 AttrSet.push_back(AttributeSet::get(C, Index, B));
Bill Wendlingd509a662013-01-29 00:34:06 +0000987
988 // Add the remaining attribute slots.
989 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
990 AttrSet.push_back(getSlotAttributes(I));
991
992 return get(C, AttrSet);
993}
994
Pete Cooperd2a44612015-05-06 23:19:43 +0000995AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Index,
996 const AttrBuilder &Attrs) const {
997 if (!pImpl) return AttributeSet();
998
999 // FIXME it is not obvious how this should work for alignment.
1000 // For now, say we can't pass in alignment, which no current use does.
1001 assert(!Attrs.hasAlignmentAttr() && "Attempt to change alignment!");
1002
1003 // Add the attribute slots before the one we're trying to add.
1004 SmallVector<AttributeSet, 4> AttrSet;
Amaury Sechet24c84fd2016-06-14 22:04:16 +00001005 uint64_t NumAttrs = pImpl->getNumSlots();
Pete Cooperd2a44612015-05-06 23:19:43 +00001006 AttributeSet AS;
1007 uint64_t LastIndex = 0;
1008 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
1009 if (getSlotIndex(I) >= Index) {
1010 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
1011 break;
1012 }
1013 LastIndex = I + 1;
1014 AttrSet.push_back(getSlotAttributes(I));
1015 }
1016
1017 // Now remove the attribute from the correct slot. There may already be an
1018 // AttributeSet there.
1019 AttrBuilder B(AS, Index);
1020 B.remove(Attrs);
1021
1022 AttrSet.push_back(AttributeSet::get(C, Index, B));
1023
1024 // Add the remaining attribute slots.
1025 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
1026 AttrSet.push_back(getSlotAttributes(I));
1027
1028 return get(C, AttrSet);
1029}
1030
Ramkumar Ramachandra8fcb4982015-02-14 19:37:54 +00001031AttributeSet AttributeSet::addDereferenceableAttr(LLVMContext &C, unsigned Index,
1032 uint64_t Bytes) const {
1033 llvm::AttrBuilder B;
1034 B.addDereferenceableAttr(Bytes);
1035 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
1036}
1037
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001038AttributeSet AttributeSet::addDereferenceableOrNullAttr(LLVMContext &C,
1039 unsigned Index,
1040 uint64_t Bytes) const {
1041 llvm::AttrBuilder B;
1042 B.addDereferenceableOrNullAttr(Bytes);
1043 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
1044}
1045
George Burgess IV278199f2016-04-12 01:05:35 +00001046AttributeSet
1047AttributeSet::addAllocSizeAttr(LLVMContext &C, unsigned Index,
1048 unsigned ElemSizeArg,
1049 const Optional<unsigned> &NumElemsArg) {
1050 llvm::AttrBuilder B;
1051 B.addAllocSizeAttr(ElemSizeArg, NumElemsArg);
1052 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
1053}
1054
Bill Wendlingd509a662013-01-29 00:34:06 +00001055//===----------------------------------------------------------------------===//
1056// AttributeSet Accessor Methods
1057//===----------------------------------------------------------------------===//
1058
Bill Wendling5d020a32013-02-10 05:00:40 +00001059LLVMContext &AttributeSet::getContext() const {
1060 return pImpl->getContext();
1061}
1062
Bill Wendling211316c2013-04-18 20:17:28 +00001063AttributeSet AttributeSet::getParamAttributes(unsigned Index) const {
1064 return pImpl && hasAttributes(Index) ?
Bill Wendlingd509a662013-01-29 00:34:06 +00001065 AttributeSet::get(pImpl->getContext(),
Bill Wendlingf2955aa2013-01-29 03:20:31 +00001066 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendling211316c2013-04-18 20:17:28 +00001067 std::make_pair(Index, getAttributes(Index)))) :
Bill Wendlingd509a662013-01-29 00:34:06 +00001068 AttributeSet();
1069}
1070
1071AttributeSet AttributeSet::getRetAttributes() const {
1072 return pImpl && hasAttributes(ReturnIndex) ?
1073 AttributeSet::get(pImpl->getContext(),
Bill Wendlingf2955aa2013-01-29 03:20:31 +00001074 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendlingd509a662013-01-29 00:34:06 +00001075 std::make_pair(ReturnIndex,
1076 getAttributes(ReturnIndex)))) :
1077 AttributeSet();
1078}
1079
1080AttributeSet AttributeSet::getFnAttributes() const {
1081 return pImpl && hasAttributes(FunctionIndex) ?
1082 AttributeSet::get(pImpl->getContext(),
Bill Wendlingf2955aa2013-01-29 03:20:31 +00001083 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendlingd509a662013-01-29 00:34:06 +00001084 std::make_pair(FunctionIndex,
1085 getAttributes(FunctionIndex)))) :
1086 AttributeSet();
1087}
1088
1089bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{
Bill Wendlingf2955aa2013-01-29 03:20:31 +00001090 AttributeSetNode *ASN = getAttributes(Index);
George Burgess IV500d3032015-12-16 05:21:02 +00001091 return ASN && ASN->hasAttribute(Kind);
Bill Wendlingd509a662013-01-29 00:34:06 +00001092}
1093
Bill Wendlingbce7b972013-02-13 08:42:21 +00001094bool AttributeSet::hasAttribute(unsigned Index, StringRef Kind) const {
1095 AttributeSetNode *ASN = getAttributes(Index);
George Burgess IV500d3032015-12-16 05:21:02 +00001096 return ASN && ASN->hasAttribute(Kind);
Bill Wendlingbce7b972013-02-13 08:42:21 +00001097}
1098
Bill Wendlingd509a662013-01-29 00:34:06 +00001099bool AttributeSet::hasAttributes(unsigned Index) 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->hasAttributes();
Bill Wendlingd509a662013-01-29 00:34:06 +00001102}
1103
Matthias Braun33282812016-01-29 22:25:19 +00001104bool AttributeSet::hasFnAttribute(Attribute::AttrKind Kind) const {
1105 return pImpl && pImpl->hasFnAttribute(Kind);
1106}
1107
Bill Wendlingd509a662013-01-29 00:34:06 +00001108bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr) const {
Craig Topperc6207612014-04-09 06:08:46 +00001109 if (!pImpl) return false;
Bill Wendlingd509a662013-01-29 00:34:06 +00001110
Amaury Sechet24c84fd2016-06-14 22:04:16 +00001111 for (unsigned I = 0, E = pImpl->getNumSlots(); I != E; ++I)
Benjamin Kramer741146b2013-07-11 12:13:16 +00001112 for (AttributeSetImpl::iterator II = pImpl->begin(I),
Bill Wendlingd509a662013-01-29 00:34:06 +00001113 IE = pImpl->end(I); II != IE; ++II)
NAKAMURA Takumib0944392013-01-31 03:47:28 +00001114 if (II->hasAttribute(Attr))
Bill Wendlingd509a662013-01-29 00:34:06 +00001115 return true;
1116
1117 return false;
1118}
1119
Bill Wendlingbce7b972013-02-13 08:42:21 +00001120Attribute AttributeSet::getAttribute(unsigned Index,
1121 Attribute::AttrKind Kind) const {
1122 AttributeSetNode *ASN = getAttributes(Index);
1123 return ASN ? ASN->getAttribute(Kind) : Attribute();
1124}
1125
1126Attribute AttributeSet::getAttribute(unsigned Index,
1127 StringRef Kind) const {
1128 AttributeSetNode *ASN = getAttributes(Index);
1129 return ASN ? ASN->getAttribute(Kind) : Attribute();
1130}
1131
Bill Wendlingf2955aa2013-01-29 03:20:31 +00001132unsigned AttributeSet::getParamAlignment(unsigned Index) const {
1133 AttributeSetNode *ASN = getAttributes(Index);
1134 return ASN ? ASN->getAlignment() : 0;
Bill Wendlingd509a662013-01-29 00:34:06 +00001135}
1136
1137unsigned AttributeSet::getStackAlignment(unsigned Index) const {
Bill Wendlingf2955aa2013-01-29 03:20:31 +00001138 AttributeSetNode *ASN = getAttributes(Index);
1139 return ASN ? ASN->getStackAlignment() : 0;
Bill Wendlingd509a662013-01-29 00:34:06 +00001140}
1141
Hal Finkelb0407ba2014-07-18 15:51:28 +00001142uint64_t AttributeSet::getDereferenceableBytes(unsigned Index) const {
1143 AttributeSetNode *ASN = getAttributes(Index);
1144 return ASN ? ASN->getDereferenceableBytes() : 0;
1145}
1146
Sanjoy Das06cf33f2015-05-06 17:41:54 +00001147uint64_t AttributeSet::getDereferenceableOrNullBytes(unsigned Index) const {
1148 AttributeSetNode *ASN = getAttributes(Index);
1149 return ASN ? ASN->getDereferenceableOrNullBytes() : 0;
1150}
1151
George Burgess IV278199f2016-04-12 01:05:35 +00001152std::pair<unsigned, Optional<unsigned>>
1153AttributeSet::getAllocSizeArgs(unsigned Index) const {
1154 AttributeSetNode *ASN = getAttributes(Index);
1155 return ASN ? ASN->getAllocSizeArgs() : std::make_pair(0, 0);
1156}
1157
1158std::string AttributeSet::getAsString(unsigned Index, bool InAttrGrp) const {
Bill Wendlingf2955aa2013-01-29 03:20:31 +00001159 AttributeSetNode *ASN = getAttributes(Index);
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +00001160 return ASN ? ASN->getAsString(InAttrGrp) : std::string("");
Bill Wendlingd509a662013-01-29 00:34:06 +00001161}
1162
Bill Wendling211316c2013-04-18 20:17:28 +00001163AttributeSetNode *AttributeSet::getAttributes(unsigned Index) const {
Craig Topperc6207612014-04-09 06:08:46 +00001164 if (!pImpl) return nullptr;
Bill Wendlingd509a662013-01-29 00:34:06 +00001165
Bill Wendlingf2955aa2013-01-29 03:20:31 +00001166 // Loop through to find the attribute node we want.
Amaury Sechet24c84fd2016-06-14 22:04:16 +00001167 for (unsigned I = 0, E = pImpl->getNumSlots(); I != E; ++I)
Bill Wendling211316c2013-04-18 20:17:28 +00001168 if (pImpl->getSlotIndex(I) == Index)
Bill Wendlingf2955aa2013-01-29 03:20:31 +00001169 return pImpl->getSlotNode(I);
Bill Wendlingd509a662013-01-29 00:34:06 +00001170
Craig Topperc6207612014-04-09 06:08:46 +00001171 return nullptr;
Bill Wendlingd509a662013-01-29 00:34:06 +00001172}
1173
Bill Wendling211316c2013-04-18 20:17:28 +00001174AttributeSet::iterator AttributeSet::begin(unsigned Slot) const {
Bill Wendlinga9174862013-01-31 23:53:05 +00001175 if (!pImpl)
1176 return ArrayRef<Attribute>().begin();
Bill Wendling211316c2013-04-18 20:17:28 +00001177 return pImpl->begin(Slot);
Bill Wendlinga9174862013-01-31 23:53:05 +00001178}
1179
Bill Wendling211316c2013-04-18 20:17:28 +00001180AttributeSet::iterator AttributeSet::end(unsigned Slot) const {
Bill Wendlinga9174862013-01-31 23:53:05 +00001181 if (!pImpl)
1182 return ArrayRef<Attribute>().end();
Bill Wendling211316c2013-04-18 20:17:28 +00001183 return pImpl->end(Slot);
Bill Wendlinga9174862013-01-31 23:53:05 +00001184}
1185
Bill Wendlingd509a662013-01-29 00:34:06 +00001186//===----------------------------------------------------------------------===//
1187// AttributeSet Introspection Methods
1188//===----------------------------------------------------------------------===//
1189
Bill Wendlingd509a662013-01-29 00:34:06 +00001190unsigned AttributeSet::getNumSlots() const {
Amaury Sechet24c84fd2016-06-14 22:04:16 +00001191 return pImpl ? pImpl->getNumSlots() : 0;
Bill Wendlingd509a662013-01-29 00:34:06 +00001192}
1193
Rafael Espindoladd275302013-04-30 16:53:38 +00001194unsigned AttributeSet::getSlotIndex(unsigned Slot) const {
Amaury Sechet24c84fd2016-06-14 22:04:16 +00001195 assert(pImpl && Slot < pImpl->getNumSlots() &&
Bill Wendlingd509a662013-01-29 00:34:06 +00001196 "Slot # out of range!");
1197 return pImpl->getSlotIndex(Slot);
1198}
1199
1200AttributeSet AttributeSet::getSlotAttributes(unsigned Slot) const {
Amaury Sechet24c84fd2016-06-14 22:04:16 +00001201 assert(pImpl && Slot < pImpl->getNumSlots() &&
Bill Wendlingd509a662013-01-29 00:34:06 +00001202 "Slot # out of range!");
1203 return pImpl->getSlotAttributes(Slot);
1204}
1205
1206uint64_t AttributeSet::Raw(unsigned Index) const {
1207 // FIXME: Remove this.
1208 return pImpl ? pImpl->Raw(Index) : 0;
1209}
1210
Yaron Kereneb2a2542016-01-29 20:50:44 +00001211LLVM_DUMP_METHOD void AttributeSet::dump() const {
Bill Wendlingd509a662013-01-29 00:34:06 +00001212 dbgs() << "PAL[\n";
1213
1214 for (unsigned i = 0, e = getNumSlots(); i < e; ++i) {
1215 uint64_t Index = getSlotIndex(i);
1216 dbgs() << " { ";
1217 if (Index == ~0U)
1218 dbgs() << "~0U";
1219 else
1220 dbgs() << Index;
1221 dbgs() << " => " << getAsString(Index) << " }\n";
1222 }
1223
1224 dbgs() << "]\n";
1225}
1226
Bill Wendlingabf3feb2012-10-05 06:44:41 +00001227//===----------------------------------------------------------------------===//
Bill Wendlingcd330342013-01-04 23:27:34 +00001228// AttrBuilder Method Implementations
Bill Wendlingabf3feb2012-10-05 06:44:41 +00001229//===----------------------------------------------------------------------===//
1230
Bill Wendling211316c2013-04-18 20:17:28 +00001231AttrBuilder::AttrBuilder(AttributeSet AS, unsigned Index)
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001232 : Attrs(0), Alignment(0), StackAlignment(0), DerefBytes(0),
George Burgess IV278199f2016-04-12 01:05:35 +00001233 DerefOrNullBytes(0), AllocSizeArgs(0) {
Bill Wendling56b0b2a2013-01-27 21:23:46 +00001234 AttributeSetImpl *pImpl = AS.pImpl;
Bill Wendling096f5442013-01-07 08:24:35 +00001235 if (!pImpl) return;
1236
Amaury Sechet24c84fd2016-06-14 22:04:16 +00001237 for (unsigned I = 0, E = pImpl->getNumSlots(); I != E; ++I) {
Bill Wendling211316c2013-04-18 20:17:28 +00001238 if (pImpl->getSlotIndex(I) != Index) continue;
Bill Wendling096f5442013-01-07 08:24:35 +00001239
Benjamin Kramer741146b2013-07-11 12:13:16 +00001240 for (AttributeSetImpl::iterator II = pImpl->begin(I),
Bill Wendling9eb689c2013-01-28 00:21:34 +00001241 IE = pImpl->end(I); II != IE; ++II)
Bill Wendling23804da2013-01-31 23:38:01 +00001242 addAttribute(*II);
Bill Wendling9eb689c2013-01-28 00:21:34 +00001243
1244 break;
1245 }
Bill Wendling096f5442013-01-07 08:24:35 +00001246}
1247
Bill Wendlingcd330342013-01-04 23:27:34 +00001248void AttrBuilder::clear() {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001249 Attrs.reset();
Sanjoy Das88d0fde2015-09-03 22:27:42 +00001250 TargetDepAttrs.clear();
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001251 Alignment = StackAlignment = DerefBytes = DerefOrNullBytes = 0;
George Burgess IV278199f2016-04-12 01:05:35 +00001252 AllocSizeArgs = 0;
Bill Wendlingcd330342013-01-04 23:27:34 +00001253}
1254
1255AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001256 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
Bill Wendling1c7cc8a2013-01-31 23:16:25 +00001257 assert(Val != Attribute::Alignment && Val != Attribute::StackAlignment &&
George Burgess IV278199f2016-04-12 01:05:35 +00001258 Val != Attribute::Dereferenceable && Val != Attribute::AllocSize &&
Hal Finkelb0407ba2014-07-18 15:51:28 +00001259 "Adding integer attribute without adding a value!");
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001260 Attrs[Val] = true;
Bill Wendling7c04e042012-10-09 19:01:18 +00001261 return *this;
Bill Wendlingabf3feb2012-10-05 06:44:41 +00001262}
1263
Bill Wendling23804da2013-01-31 23:38:01 +00001264AttrBuilder &AttrBuilder::addAttribute(Attribute Attr) {
Bill Wendling0a437302013-02-10 10:13:23 +00001265 if (Attr.isStringAttribute()) {
1266 addAttribute(Attr.getKindAsString(), Attr.getValueAsString());
1267 return *this;
1268 }
1269
Bill Wendling3f12ac22013-02-05 22:37:24 +00001270 Attribute::AttrKind Kind = Attr.getKindAsEnum();
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001271 Attrs[Kind] = true;
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001272
Bill Wendling3f12ac22013-02-05 22:37:24 +00001273 if (Kind == Attribute::Alignment)
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001274 Alignment = Attr.getAlignment();
Bill Wendling3f12ac22013-02-05 22:37:24 +00001275 else if (Kind == Attribute::StackAlignment)
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001276 StackAlignment = Attr.getStackAlignment();
Hal Finkelb0407ba2014-07-18 15:51:28 +00001277 else if (Kind == Attribute::Dereferenceable)
1278 DerefBytes = Attr.getDereferenceableBytes();
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001279 else if (Kind == Attribute::DereferenceableOrNull)
1280 DerefOrNullBytes = Attr.getDereferenceableOrNullBytes();
George Burgess IV278199f2016-04-12 01:05:35 +00001281 else if (Kind == Attribute::AllocSize)
1282 AllocSizeArgs = Attr.getValueAsInt();
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001283 return *this;
1284}
1285
Bill Wendlingb9c5b1a2013-02-05 08:09:32 +00001286AttrBuilder &AttrBuilder::addAttribute(StringRef A, StringRef V) {
1287 TargetDepAttrs[A] = V;
1288 return *this;
1289}
1290
Bill Wendling23804da2013-01-31 23:38:01 +00001291AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001292 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
1293 Attrs[Val] = false;
Bill Wendling23804da2013-01-31 23:38:01 +00001294
1295 if (Val == Attribute::Alignment)
1296 Alignment = 0;
1297 else if (Val == Attribute::StackAlignment)
1298 StackAlignment = 0;
Hal Finkelb0407ba2014-07-18 15:51:28 +00001299 else if (Val == Attribute::Dereferenceable)
1300 DerefBytes = 0;
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001301 else if (Val == Attribute::DereferenceableOrNull)
1302 DerefOrNullBytes = 0;
George Burgess IV278199f2016-04-12 01:05:35 +00001303 else if (Val == Attribute::AllocSize)
1304 AllocSizeArgs = 0;
Bill Wendling23804da2013-01-31 23:38:01 +00001305
1306 return *this;
1307}
1308
Bill Wendlingd2196752013-01-30 23:07:40 +00001309AttrBuilder &AttrBuilder::removeAttributes(AttributeSet A, uint64_t Index) {
Bill Wendling211316c2013-04-18 20:17:28 +00001310 unsigned Slot = ~0U;
Bill Wendlingf1c94e32013-02-01 00:13:50 +00001311 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
1312 if (A.getSlotIndex(I) == Index) {
Bill Wendling211316c2013-04-18 20:17:28 +00001313 Slot = I;
Bill Wendlingf1c94e32013-02-01 00:13:50 +00001314 break;
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001315 }
Bill Wendlingf1c94e32013-02-01 00:13:50 +00001316
Bill Wendling211316c2013-04-18 20:17:28 +00001317 assert(Slot != ~0U && "Couldn't find index in AttributeSet!");
Bill Wendlingf1c94e32013-02-01 00:13:50 +00001318
Bill Wendling211316c2013-04-18 20:17:28 +00001319 for (AttributeSet::iterator I = A.begin(Slot), E = A.end(Slot); I != E; ++I) {
Bill Wendling7cde51d2013-02-12 07:56:49 +00001320 Attribute Attr = *I;
Hal Finkele15442c2014-07-18 06:51:55 +00001321 if (Attr.isEnumAttribute() || Attr.isIntAttribute()) {
George Burgess IV500d3032015-12-16 05:21:02 +00001322 removeAttribute(Attr.getKindAsEnum());
Bill Wendling7cde51d2013-02-12 07:56:49 +00001323 } else {
1324 assert(Attr.isStringAttribute() && "Invalid attribute type!");
George Burgess IV500d3032015-12-16 05:21:02 +00001325 removeAttribute(Attr.getKindAsString());
Bill Wendling7cde51d2013-02-12 07:56:49 +00001326 }
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001327 }
1328
1329 return *this;
1330}
1331
Bill Wendlingb9c5b1a2013-02-05 08:09:32 +00001332AttrBuilder &AttrBuilder::removeAttribute(StringRef A) {
1333 std::map<std::string, std::string>::iterator I = TargetDepAttrs.find(A);
1334 if (I != TargetDepAttrs.end())
1335 TargetDepAttrs.erase(I);
1336 return *this;
1337}
1338
George Burgess IV278199f2016-04-12 01:05:35 +00001339std::pair<unsigned, Optional<unsigned>> AttrBuilder::getAllocSizeArgs() const {
1340 return unpackAllocSizeArgs(AllocSizeArgs);
1341}
1342
Bill Wendling50d27842012-10-15 20:35:56 +00001343AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
Bill Wendlingabd5ba22012-10-14 03:58:29 +00001344 if (Align == 0) return *this;
Bill Wendlingcd330342013-01-04 23:27:34 +00001345
Bill Wendlingabf3feb2012-10-05 06:44:41 +00001346 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1347 assert(Align <= 0x40000000 && "Alignment too large.");
Bill Wendlingcd330342013-01-04 23:27:34 +00001348
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001349 Attrs[Attribute::Alignment] = true;
Bill Wendlingcd330342013-01-04 23:27:34 +00001350 Alignment = Align;
Bill Wendlingabd5ba22012-10-14 03:58:29 +00001351 return *this;
Chris Lattner3e13b8c2008-01-02 23:42:30 +00001352}
1353
Bill Wendlingcd330342013-01-04 23:27:34 +00001354AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
1355 // Default alignment, allow the target to define how to align it.
1356 if (Align == 0) return *this;
1357
1358 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1359 assert(Align <= 0x100 && "Alignment too large.");
1360
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001361 Attrs[Attribute::StackAlignment] = true;
Bill Wendlingcd330342013-01-04 23:27:34 +00001362 StackAlignment = Align;
1363 return *this;
1364}
1365
Hal Finkelb0407ba2014-07-18 15:51:28 +00001366AttrBuilder &AttrBuilder::addDereferenceableAttr(uint64_t Bytes) {
1367 if (Bytes == 0) return *this;
1368
1369 Attrs[Attribute::Dereferenceable] = true;
1370 DerefBytes = Bytes;
1371 return *this;
1372}
1373
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001374AttrBuilder &AttrBuilder::addDereferenceableOrNullAttr(uint64_t Bytes) {
1375 if (Bytes == 0)
1376 return *this;
1377
1378 Attrs[Attribute::DereferenceableOrNull] = true;
1379 DerefOrNullBytes = Bytes;
1380 return *this;
1381}
1382
George Burgess IV278199f2016-04-12 01:05:35 +00001383AttrBuilder &AttrBuilder::addAllocSizeAttr(unsigned ElemSize,
1384 const Optional<unsigned> &NumElems) {
1385 return addAllocSizeAttrFromRawRepr(packAllocSizeArgs(ElemSize, NumElems));
1386}
1387
1388AttrBuilder &AttrBuilder::addAllocSizeAttrFromRawRepr(uint64_t RawArgs) {
1389 // (0, 0) is our "not present" value, so we need to check for it here.
1390 assert(RawArgs && "Invalid allocsize arguments -- given allocsize(0, 0)");
1391
1392 Attrs[Attribute::AllocSize] = true;
1393 // Reuse existing machinery to store this as a single 64-bit integer so we can
1394 // save a few bytes over using a pair<unsigned, Optional<unsigned>>.
1395 AllocSizeArgs = RawArgs;
1396 return *this;
1397}
1398
Bill Wendlinge2614922013-02-06 01:16:00 +00001399AttrBuilder &AttrBuilder::merge(const AttrBuilder &B) {
1400 // FIXME: What if both have alignments, but they don't match?!
1401 if (!Alignment)
1402 Alignment = B.Alignment;
1403
1404 if (!StackAlignment)
1405 StackAlignment = B.StackAlignment;
1406
Hal Finkelb0407ba2014-07-18 15:51:28 +00001407 if (!DerefBytes)
1408 DerefBytes = B.DerefBytes;
1409
Pete Cooperd2a44612015-05-06 23:19:43 +00001410 if (!DerefOrNullBytes)
1411 DerefOrNullBytes = B.DerefOrNullBytes;
1412
George Burgess IV278199f2016-04-12 01:05:35 +00001413 if (!AllocSizeArgs)
1414 AllocSizeArgs = B.AllocSizeArgs;
1415
Benjamin Kramer45e7d532013-02-16 19:13:18 +00001416 Attrs |= B.Attrs;
Bill Wendlinge2614922013-02-06 01:16:00 +00001417
Pete Cooperd2a44612015-05-06 23:19:43 +00001418 for (auto I : B.td_attrs())
1419 TargetDepAttrs[I.first] = I.second;
Bill Wendlinge2614922013-02-06 01:16:00 +00001420
1421 return *this;
1422}
1423
Pete Cooperd2a44612015-05-06 23:19:43 +00001424AttrBuilder &AttrBuilder::remove(const AttrBuilder &B) {
1425 // FIXME: What if both have alignments, but they don't match?!
1426 if (B.Alignment)
1427 Alignment = 0;
1428
1429 if (B.StackAlignment)
1430 StackAlignment = 0;
1431
1432 if (B.DerefBytes)
1433 DerefBytes = 0;
1434
1435 if (B.DerefOrNullBytes)
1436 DerefOrNullBytes = 0;
1437
George Burgess IV278199f2016-04-12 01:05:35 +00001438 if (B.AllocSizeArgs)
1439 AllocSizeArgs = 0;
1440
Pete Cooperd2a44612015-05-06 23:19:43 +00001441 Attrs &= ~B.Attrs;
1442
1443 for (auto I : B.td_attrs())
1444 TargetDepAttrs.erase(I.first);
1445
1446 return *this;
1447}
1448
1449bool AttrBuilder::overlaps(const AttrBuilder &B) const {
1450 // First check if any of the target independent attributes overlap.
1451 if ((Attrs & B.Attrs).any())
1452 return true;
1453
1454 // Then check if any target dependent ones do.
1455 for (auto I : td_attrs())
1456 if (B.contains(I.first))
1457 return true;
1458
1459 return false;
1460}
1461
Bill Wendling4b001442013-02-06 01:33:42 +00001462bool AttrBuilder::contains(StringRef A) const {
1463 return TargetDepAttrs.find(A) != TargetDepAttrs.end();
1464}
1465
Bill Wendling50d27842012-10-15 20:35:56 +00001466bool AttrBuilder::hasAttributes() const {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001467 return !Attrs.none() || !TargetDepAttrs.empty();
Bill Wendlingc6daefa2012-10-08 23:27:46 +00001468}
Bill Wendling9ac69f92013-01-04 20:54:35 +00001469
Bill Wendlingd2196752013-01-30 23:07:40 +00001470bool AttrBuilder::hasAttributes(AttributeSet A, uint64_t Index) const {
Bill Wendling211316c2013-04-18 20:17:28 +00001471 unsigned Slot = ~0U;
Bill Wendling9ca01da2013-02-02 00:42:06 +00001472 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
1473 if (A.getSlotIndex(I) == Index) {
Bill Wendling211316c2013-04-18 20:17:28 +00001474 Slot = I;
Bill Wendling9ca01da2013-02-02 00:42:06 +00001475 break;
1476 }
1477
Bill Wendling211316c2013-04-18 20:17:28 +00001478 assert(Slot != ~0U && "Couldn't find the index!");
Bill Wendling9ca01da2013-02-02 00:42:06 +00001479
George Burgess IV500d3032015-12-16 05:21:02 +00001480 for (AttributeSet::iterator I = A.begin(Slot), E = A.end(Slot); I != E; ++I) {
Bill Wendling7cde51d2013-02-12 07:56:49 +00001481 Attribute Attr = *I;
Hal Finkele15442c2014-07-18 06:51:55 +00001482 if (Attr.isEnumAttribute() || Attr.isIntAttribute()) {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001483 if (Attrs[I->getKindAsEnum()])
Bill Wendling7cde51d2013-02-12 07:56:49 +00001484 return true;
1485 } else {
1486 assert(Attr.isStringAttribute() && "Invalid attribute kind!");
1487 return TargetDepAttrs.find(Attr.getKindAsString())!=TargetDepAttrs.end();
1488 }
1489 }
Bill Wendling9ca01da2013-02-02 00:42:06 +00001490
1491 return false;
Bill Wendling70f39172012-10-09 00:01:21 +00001492}
Bill Wendling9ac69f92013-01-04 20:54:35 +00001493
Bill Wendling50d27842012-10-15 20:35:56 +00001494bool AttrBuilder::hasAlignmentAttr() const {
Bill Wendlingcd330342013-01-04 23:27:34 +00001495 return Alignment != 0;
Bill Wendlingc6daefa2012-10-08 23:27:46 +00001496}
1497
Bill Wendlingd509a662013-01-29 00:34:06 +00001498bool AttrBuilder::operator==(const AttrBuilder &B) {
Benjamin Kramer45e7d532013-02-16 19:13:18 +00001499 if (Attrs != B.Attrs)
1500 return false;
Bill Wendling4b001442013-02-06 01:33:42 +00001501
1502 for (td_const_iterator I = TargetDepAttrs.begin(),
1503 E = TargetDepAttrs.end(); I != E; ++I)
1504 if (B.TargetDepAttrs.find(I->first) == B.TargetDepAttrs.end())
1505 return false;
1506
Hal Finkelb0407ba2014-07-18 15:51:28 +00001507 return Alignment == B.Alignment && StackAlignment == B.StackAlignment &&
1508 DerefBytes == B.DerefBytes;
Bill Wendlingd509a662013-01-29 00:34:06 +00001509}
1510
1511AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) {
Bill Wendling56aeccc2013-02-04 23:32:23 +00001512 // FIXME: Remove this in 4.0.
Bill Wendling60011b82013-01-29 01:43:29 +00001513 if (!Val) return *this;
1514
Bill Wendlingd509a662013-01-29 00:34:06 +00001515 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
1516 I = Attribute::AttrKind(I + 1)) {
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001517 if (I == Attribute::Dereferenceable ||
Igor Laevsky39d662f2015-07-11 10:30:36 +00001518 I == Attribute::DereferenceableOrNull ||
George Burgess IV278199f2016-04-12 01:05:35 +00001519 I == Attribute::ArgMemOnly ||
1520 I == Attribute::AllocSize)
Hal Finkelb0407ba2014-07-18 15:51:28 +00001521 continue;
Bill Wendlingd509a662013-01-29 00:34:06 +00001522 if (uint64_t A = (Val & AttributeImpl::getAttrMask(I))) {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001523 Attrs[I] = true;
Bill Wendlingd509a662013-01-29 00:34:06 +00001524
1525 if (I == Attribute::Alignment)
1526 Alignment = 1ULL << ((A >> 16) - 1);
1527 else if (I == Attribute::StackAlignment)
1528 StackAlignment = 1ULL << ((A >> 26)-1);
1529 }
1530 }
1531
1532 return *this;
1533}
1534
Bill Wendling57625a42013-01-25 23:09:36 +00001535//===----------------------------------------------------------------------===//
1536// AttributeFuncs Function Defintions
1537//===----------------------------------------------------------------------===//
1538
Bill Wendlingc79cdff2013-02-01 01:04:27 +00001539/// \brief Which attributes cannot be applied to a type.
Craig Toppere3dcce92015-08-01 22:20:21 +00001540AttrBuilder AttributeFuncs::typeIncompatible(Type *Ty) {
Bill Wendling57625a42013-01-25 23:09:36 +00001541 AttrBuilder Incompatible;
1542
1543 if (!Ty->isIntegerTy())
1544 // Attribute that only apply to integers.
1545 Incompatible.addAttribute(Attribute::SExt)
1546 .addAttribute(Attribute::ZExt);
1547
1548 if (!Ty->isPointerTy())
1549 // Attribute that only apply to pointers.
1550 Incompatible.addAttribute(Attribute::ByVal)
1551 .addAttribute(Attribute::Nest)
1552 .addAttribute(Attribute::NoAlias)
1553 .addAttribute(Attribute::NoCapture)
Nick Lewyckyd52b1522014-05-20 01:23:40 +00001554 .addAttribute(Attribute::NonNull)
Hal Finkelb0407ba2014-07-18 15:51:28 +00001555 .addDereferenceableAttr(1) // the int here is ignored
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001556 .addDereferenceableOrNullAttr(1) // the int here is ignored
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001557 .addAttribute(Attribute::ReadNone)
1558 .addAttribute(Attribute::ReadOnly)
Reid Klecknera534a382013-12-19 02:14:12 +00001559 .addAttribute(Attribute::StructRet)
1560 .addAttribute(Attribute::InAlloca);
Bill Wendling57625a42013-01-25 23:09:36 +00001561
Pete Cooper2777d8872015-05-06 23:19:56 +00001562 return Incompatible;
Bill Wendling57625a42013-01-25 23:09:36 +00001563}
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001564
1565template<typename AttrClass>
1566static bool isEqual(const Function &Caller, const Function &Callee) {
1567 return Caller.getFnAttribute(AttrClass::getKind()) ==
1568 Callee.getFnAttribute(AttrClass::getKind());
1569}
1570
1571/// \brief Compute the logical AND of the attributes of the caller and the
1572/// callee.
1573///
1574/// This function sets the caller's attribute to false if the callee's attribute
1575/// is false.
1576template<typename AttrClass>
1577static void setAND(Function &Caller, const Function &Callee) {
1578 if (AttrClass::isSet(Caller, AttrClass::getKind()) &&
1579 !AttrClass::isSet(Callee, AttrClass::getKind()))
1580 AttrClass::set(Caller, AttrClass::getKind(), false);
1581}
1582
1583/// \brief Compute the logical OR of the attributes of the caller and the
1584/// callee.
1585///
1586/// This function sets the caller's attribute to true if the callee's attribute
1587/// is true.
1588template<typename AttrClass>
1589static void setOR(Function &Caller, const Function &Callee) {
1590 if (!AttrClass::isSet(Caller, AttrClass::getKind()) &&
1591 AttrClass::isSet(Callee, AttrClass::getKind()))
1592 AttrClass::set(Caller, AttrClass::getKind(), true);
1593}
1594
1595/// \brief If the inlined function had a higher stack protection level than the
1596/// calling function, then bump up the caller's stack protection level.
1597static void adjustCallerSSPLevel(Function &Caller, const Function &Callee) {
1598 // If upgrading the SSP attribute, clear out the old SSP Attributes first.
1599 // Having multiple SSP attributes doesn't actually hurt, but it adds useless
1600 // clutter to the IR.
1601 AttrBuilder B;
1602 B.addAttribute(Attribute::StackProtect)
1603 .addAttribute(Attribute::StackProtectStrong)
1604 .addAttribute(Attribute::StackProtectReq);
1605 AttributeSet OldSSPAttr = AttributeSet::get(Caller.getContext(),
1606 AttributeSet::FunctionIndex,
1607 B);
1608
Evgeniy Stepanovf17120a2016-04-11 22:27:48 +00001609 if (Callee.hasFnAttribute(Attribute::StackProtectReq)) {
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001610 Caller.removeAttributes(AttributeSet::FunctionIndex, OldSSPAttr);
1611 Caller.addFnAttr(Attribute::StackProtectReq);
1612 } else if (Callee.hasFnAttribute(Attribute::StackProtectStrong) &&
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001613 !Caller.hasFnAttribute(Attribute::StackProtectReq)) {
1614 Caller.removeAttributes(AttributeSet::FunctionIndex, OldSSPAttr);
1615 Caller.addFnAttr(Attribute::StackProtectStrong);
1616 } else if (Callee.hasFnAttribute(Attribute::StackProtect) &&
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001617 !Caller.hasFnAttribute(Attribute::StackProtectReq) &&
1618 !Caller.hasFnAttribute(Attribute::StackProtectStrong))
1619 Caller.addFnAttr(Attribute::StackProtect);
1620}
1621
1622#define GET_ATTR_COMPAT_FUNC
1623#include "AttributesCompatFunc.inc"
1624
1625bool AttributeFuncs::areInlineCompatible(const Function &Caller,
1626 const Function &Callee) {
1627 return hasCompatibleFnAttrs(Caller, Callee);
1628}
1629
1630
1631void AttributeFuncs::mergeAttributesForInlining(Function &Caller,
1632 const Function &Callee) {
1633 mergeFnAttrs(Caller, Callee);
1634}