blob: 01de8bb56279fe75172691f3d06be97cd5f00cae [file] [log] [blame]
Eugene Zelenkodeaf6952017-02-17 00:00:09 +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
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000011// This file implements the Attribute, AttributeImpl, AttrBuilder,
Reid Klecknerb5180542017-03-21 16:57:19 +000012// AttributeListImpl, and AttributeList classes.
Chris Lattner3e13b8c2008-01-02 23:42:30 +000013//
14//===----------------------------------------------------------------------===//
15
Chandler Carruth6bda14b2017-06-06 11:49:48 +000016#include "llvm/IR/Attributes.h"
Bill Wendling4607f4b2012-12-20 01:36:59 +000017#include "AttributeImpl.h"
Bill Wendlinge38b8042012-09-26 21:07:29 +000018#include "LLVMContextImpl.h"
Eugene Zelenkodeaf6952017-02-17 00:00:09 +000019#include "llvm/ADT/ArrayRef.h"
20#include "llvm/ADT/FoldingSet.h"
21#include "llvm/ADT/Optional.h"
Benjamin Kramer502b9e12014-04-12 16:15:53 +000022#include "llvm/ADT/STLExtras.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000023#include "llvm/ADT/SmallVector.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000024#include "llvm/ADT/StringExtras.h"
Eugene Zelenkodeaf6952017-02-17 00:00:09 +000025#include "llvm/ADT/StringRef.h"
26#include "llvm/ADT/Twine.h"
Nico Weber432a3882018-04-30 14:59:11 +000027#include "llvm/Config/llvm-config.h"
Eugene Zelenkodeaf6952017-02-17 00:00:09 +000028#include "llvm/IR/Function.h"
29#include "llvm/IR/LLVMContext.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000030#include "llvm/IR/Type.h"
Eugene Zelenkodeaf6952017-02-17 00:00:09 +000031#include "llvm/Support/Compiler.h"
David Greenef7014732010-01-05 01:29:58 +000032#include "llvm/Support/Debug.h"
Eugene Zelenkodeaf6952017-02-17 00:00:09 +000033#include "llvm/Support/ErrorHandling.h"
34#include "llvm/Support/MathExtras.h"
Benjamin Kramer1a25d732009-08-23 11:37:21 +000035#include "llvm/Support/raw_ostream.h"
Bill Wendlingd2e493b2013-01-24 00:06:56 +000036#include <algorithm>
Eugene Zelenkodeaf6952017-02-17 00:00:09 +000037#include <cassert>
Eugene Zelenkod761e2c2017-05-15 21:57:41 +000038#include <climits>
39#include <cstddef>
Eugene Zelenkodeaf6952017-02-17 00:00:09 +000040#include <cstdint>
41#include <limits>
Eugene Zelenkodeaf6952017-02-17 00:00:09 +000042#include <string>
43#include <tuple>
44#include <utility>
45
Chris Lattner3e13b8c2008-01-02 23:42:30 +000046using namespace llvm;
47
Chris Lattner8a923e72008-03-12 17:45:29 +000048//===----------------------------------------------------------------------===//
Bill Wendling7707c5a2013-01-29 00:48:16 +000049// Attribute Construction Methods
Chris Lattner8a923e72008-03-12 17:45:29 +000050//===----------------------------------------------------------------------===//
Chris Lattnerd0e1f102008-01-03 00:10:22 +000051
George Burgess IV278199f2016-04-12 01:05:35 +000052// allocsize has two integer arguments, but because they're both 32 bits, we can
53// pack them into one 64-bit value, at the cost of making said value
54// nonsensical.
55//
56// In order to do this, we need to reserve one value of the second (optional)
57// allocsize argument to signify "not present."
George Burgess IV381fc0e2016-08-25 01:05:08 +000058static const unsigned AllocSizeNumElemsNotPresent = -1;
George Burgess IV278199f2016-04-12 01:05:35 +000059
60static uint64_t packAllocSizeArgs(unsigned ElemSizeArg,
61 const Optional<unsigned> &NumElemsArg) {
62 assert((!NumElemsArg.hasValue() ||
63 *NumElemsArg != AllocSizeNumElemsNotPresent) &&
64 "Attempting to pack a reserved value");
65
66 return uint64_t(ElemSizeArg) << 32 |
67 NumElemsArg.getValueOr(AllocSizeNumElemsNotPresent);
68}
69
70static std::pair<unsigned, Optional<unsigned>>
71unpackAllocSizeArgs(uint64_t Num) {
72 unsigned NumElems = Num & std::numeric_limits<unsigned>::max();
73 unsigned ElemSizeArg = Num >> 32;
74
75 Optional<unsigned> NumElemsArg;
76 if (NumElems != AllocSizeNumElemsNotPresent)
77 NumElemsArg = NumElems;
78 return std::make_pair(ElemSizeArg, NumElemsArg);
79}
80
Matt Arsenaulte1cef6e2014-09-03 23:24:31 +000081Attribute Attribute::get(LLVMContext &Context, Attribute::AttrKind Kind,
82 uint64_t Val) {
83 LLVMContextImpl *pImpl = Context.pImpl;
84 FoldingSetNodeID ID;
85 ID.AddInteger(Kind);
Matt Arsenaultfb4308e2014-09-03 23:38:05 +000086 if (Val) ID.AddInteger(Val);
Matt Arsenaulte1cef6e2014-09-03 23:24:31 +000087
88 void *InsertPoint;
89 AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
90
91 if (!PA) {
92 // If we didn't find any existing attributes of the same shape then create a
93 // new one and insert it.
Matt Arsenaultfb4308e2014-09-03 23:38:05 +000094 if (!Val)
95 PA = new EnumAttributeImpl(Kind);
96 else
97 PA = new IntAttributeImpl(Kind, Val);
Bill Wendling3f12ac22013-02-05 22:37:24 +000098 pImpl->AttrsSet.InsertNode(PA, InsertPoint);
99 }
100
101 // Return the Attribute that we found or created.
102 return Attribute(PA);
103}
104
105Attribute Attribute::get(LLVMContext &Context, StringRef Kind, StringRef Val) {
106 LLVMContextImpl *pImpl = Context.pImpl;
107 FoldingSetNodeID ID;
108 ID.AddString(Kind);
109 if (!Val.empty()) ID.AddString(Val);
Bill Wendling73ea2de2012-10-08 21:47:17 +0000110
111 void *InsertPoint;
Bill Wendling4607f4b2012-12-20 01:36:59 +0000112 AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
Bill Wendling73ea2de2012-10-08 21:47:17 +0000113
114 if (!PA) {
115 // If we didn't find any existing attributes of the same shape then create a
116 // new one and insert it.
Benjamin Kramer741146b2013-07-11 12:13:16 +0000117 PA = new StringAttributeImpl(Kind, Val);
Bill Wendling73ea2de2012-10-08 21:47:17 +0000118 pImpl->AttrsSet.InsertNode(PA, InsertPoint);
119 }
120
Bill Wendlingb9c5b1a2013-02-05 08:09:32 +0000121 // Return the Attribute that we found or created.
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000122 return Attribute(PA);
Bill Wendling73ea2de2012-10-08 21:47:17 +0000123}
124
Bill Wendling4bbe9db2013-01-27 22:43:04 +0000125Attribute Attribute::getWithAlignment(LLVMContext &Context, uint64_t Align) {
Bill Wendling1c7cc8a2013-01-31 23:16:25 +0000126 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
127 assert(Align <= 0x40000000 && "Alignment too large.");
Bill Wendling3f12ac22013-02-05 22:37:24 +0000128 return get(Context, Alignment, Align);
Bill Wendling4bbe9db2013-01-27 22:43:04 +0000129}
130
131Attribute Attribute::getWithStackAlignment(LLVMContext &Context,
132 uint64_t Align) {
Bill Wendling1c7cc8a2013-01-31 23:16:25 +0000133 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
134 assert(Align <= 0x100 && "Alignment too large.");
Bill Wendling3f12ac22013-02-05 22:37:24 +0000135 return get(Context, StackAlignment, Align);
Bill Wendling4bbe9db2013-01-27 22:43:04 +0000136}
137
Hal Finkelb0407ba2014-07-18 15:51:28 +0000138Attribute Attribute::getWithDereferenceableBytes(LLVMContext &Context,
139 uint64_t Bytes) {
140 assert(Bytes && "Bytes must be non-zero.");
141 return get(Context, Dereferenceable, Bytes);
142}
143
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000144Attribute Attribute::getWithDereferenceableOrNullBytes(LLVMContext &Context,
145 uint64_t Bytes) {
146 assert(Bytes && "Bytes must be non-zero.");
147 return get(Context, DereferenceableOrNull, Bytes);
148}
149
George Burgess IV278199f2016-04-12 01:05:35 +0000150Attribute
151Attribute::getWithAllocSizeArgs(LLVMContext &Context, unsigned ElemSizeArg,
152 const Optional<unsigned> &NumElemsArg) {
153 assert(!(ElemSizeArg == 0 && NumElemsArg && *NumElemsArg == 0) &&
154 "Invalid allocsize arguments -- given allocsize(0, 0)");
155 return get(Context, AllocSize, packAllocSizeArgs(ElemSizeArg, NumElemsArg));
156}
157
Bill Wendling7707c5a2013-01-29 00:48:16 +0000158//===----------------------------------------------------------------------===//
159// Attribute Accessor Methods
160//===----------------------------------------------------------------------===//
161
Bill Wendling3f12ac22013-02-05 22:37:24 +0000162bool Attribute::isEnumAttribute() const {
163 return pImpl && pImpl->isEnumAttribute();
164}
165
Hal Finkele15442c2014-07-18 06:51:55 +0000166bool Attribute::isIntAttribute() const {
167 return pImpl && pImpl->isIntAttribute();
Bill Wendling3f12ac22013-02-05 22:37:24 +0000168}
169
170bool Attribute::isStringAttribute() const {
171 return pImpl && pImpl->isStringAttribute();
172}
173
174Attribute::AttrKind Attribute::getKindAsEnum() const {
Bill Wendling440e9d82013-07-25 00:34:29 +0000175 if (!pImpl) return None;
Hal Finkele15442c2014-07-18 06:51:55 +0000176 assert((isEnumAttribute() || isIntAttribute()) &&
Bill Wendling3f12ac22013-02-05 22:37:24 +0000177 "Invalid attribute type to get the kind as an enum!");
George Burgess IV500d3032015-12-16 05:21:02 +0000178 return pImpl->getKindAsEnum();
Bill Wendling3f12ac22013-02-05 22:37:24 +0000179}
180
181uint64_t Attribute::getValueAsInt() const {
Bill Wendling440e9d82013-07-25 00:34:29 +0000182 if (!pImpl) return 0;
Hal Finkele15442c2014-07-18 06:51:55 +0000183 assert(isIntAttribute() &&
184 "Expected the attribute to be an integer attribute!");
George Burgess IV500d3032015-12-16 05:21:02 +0000185 return pImpl->getValueAsInt();
Bill Wendling3f12ac22013-02-05 22:37:24 +0000186}
187
188StringRef Attribute::getKindAsString() const {
Eugene Zelenko7fb5d412018-03-30 00:47:31 +0000189 if (!pImpl) return {};
Bill Wendling3f12ac22013-02-05 22:37:24 +0000190 assert(isStringAttribute() &&
191 "Invalid attribute type to get the kind as a string!");
George Burgess IV500d3032015-12-16 05:21:02 +0000192 return pImpl->getKindAsString();
Bill Wendling3f12ac22013-02-05 22:37:24 +0000193}
194
195StringRef Attribute::getValueAsString() const {
Eugene Zelenko7fb5d412018-03-30 00:47:31 +0000196 if (!pImpl) return {};
Bill Wendling3f12ac22013-02-05 22:37:24 +0000197 assert(isStringAttribute() &&
198 "Invalid attribute type to get the value as a string!");
George Burgess IV500d3032015-12-16 05:21:02 +0000199 return pImpl->getValueAsString();
Bill Wendling3f12ac22013-02-05 22:37:24 +0000200}
201
Bill Wendlingae89a0f2013-02-05 23:48:36 +0000202bool Attribute::hasAttribute(AttrKind Kind) const {
203 return (pImpl && pImpl->hasAttribute(Kind)) || (!pImpl && Kind == None);
204}
205
206bool Attribute::hasAttribute(StringRef Kind) const {
207 if (!isStringAttribute()) return false;
208 return pImpl && pImpl->hasAttribute(Kind);
Bill Wendling03eefb32013-01-29 20:45:34 +0000209}
210
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000211unsigned Attribute::getAlignment() const {
Bill Wendlingc79cdff2013-02-01 01:04:27 +0000212 assert(hasAttribute(Attribute::Alignment) &&
213 "Trying to get alignment from non-alignment attribute!");
Bill Wendling3f12ac22013-02-05 22:37:24 +0000214 return pImpl->getValueAsInt();
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000215}
216
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000217unsigned Attribute::getStackAlignment() const {
Bill Wendlingc79cdff2013-02-01 01:04:27 +0000218 assert(hasAttribute(Attribute::StackAlignment) &&
219 "Trying to get alignment from non-alignment attribute!");
Bill Wendling3f12ac22013-02-05 22:37:24 +0000220 return pImpl->getValueAsInt();
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000221}
222
Hal Finkelb0407ba2014-07-18 15:51:28 +0000223uint64_t Attribute::getDereferenceableBytes() const {
224 assert(hasAttribute(Attribute::Dereferenceable) &&
225 "Trying to get dereferenceable bytes from "
226 "non-dereferenceable attribute!");
227 return pImpl->getValueAsInt();
228}
229
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000230uint64_t Attribute::getDereferenceableOrNullBytes() const {
231 assert(hasAttribute(Attribute::DereferenceableOrNull) &&
232 "Trying to get dereferenceable bytes from "
233 "non-dereferenceable attribute!");
234 return pImpl->getValueAsInt();
235}
236
George Burgess IV278199f2016-04-12 01:05:35 +0000237std::pair<unsigned, Optional<unsigned>> Attribute::getAllocSizeArgs() const {
238 assert(hasAttribute(Attribute::AllocSize) &&
239 "Trying to get allocsize args from non-allocsize attribute");
240 return unpackAllocSizeArgs(pImpl->getValueAsInt());
241}
242
Bill Wendling829b4782013-02-11 08:43:33 +0000243std::string Attribute::getAsString(bool InAttrGrp) const {
Eugene Zelenko7fb5d412018-03-30 00:47:31 +0000244 if (!pImpl) return {};
Bill Wendling9c2eba92013-01-31 20:59:05 +0000245
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000246 if (hasAttribute(Attribute::SanitizeAddress))
247 return "sanitize_address";
Evgeniy Stepanovc667c1f2017-12-09 00:21:41 +0000248 if (hasAttribute(Attribute::SanitizeHWAddress))
249 return "sanitize_hwaddress";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000250 if (hasAttribute(Attribute::AlwaysInline))
251 return "alwaysinline";
Igor Laevsky39d662f2015-07-11 10:30:36 +0000252 if (hasAttribute(Attribute::ArgMemOnly))
253 return "argmemonly";
Michael Gottesman41748d72013-06-27 00:25:01 +0000254 if (hasAttribute(Attribute::Builtin))
255 return "builtin";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000256 if (hasAttribute(Attribute::ByVal))
257 return "byval";
Owen Anderson85fa7d52015-05-26 23:48:40 +0000258 if (hasAttribute(Attribute::Convergent))
259 return "convergent";
Manman Ren9bfd0d02016-04-01 21:41:15 +0000260 if (hasAttribute(Attribute::SwiftError))
261 return "swifterror";
Manman Renf46262e2016-03-29 17:37:21 +0000262 if (hasAttribute(Attribute::SwiftSelf))
263 return "swiftself";
Vaivaswatha Nagarajfb3f4902015-12-16 16:16:19 +0000264 if (hasAttribute(Attribute::InaccessibleMemOnly))
265 return "inaccessiblememonly";
266 if (hasAttribute(Attribute::InaccessibleMemOrArgMemOnly))
267 return "inaccessiblemem_or_argmemonly";
Reid Klecknera534a382013-12-19 02:14:12 +0000268 if (hasAttribute(Attribute::InAlloca))
269 return "inalloca";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000270 if (hasAttribute(Attribute::InlineHint))
271 return "inlinehint";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000272 if (hasAttribute(Attribute::InReg))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000273 return "inreg";
Tom Roeder44cb65f2014-06-05 19:29:43 +0000274 if (hasAttribute(Attribute::JumpTable))
275 return "jumptable";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000276 if (hasAttribute(Attribute::MinSize))
277 return "minsize";
278 if (hasAttribute(Attribute::Naked))
279 return "naked";
280 if (hasAttribute(Attribute::Nest))
281 return "nest";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000282 if (hasAttribute(Attribute::NoAlias))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000283 return "noalias";
Bill Wendling09bd1f72013-02-22 00:12:35 +0000284 if (hasAttribute(Attribute::NoBuiltin))
285 return "nobuiltin";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000286 if (hasAttribute(Attribute::NoCapture))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000287 return "nocapture";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000288 if (hasAttribute(Attribute::NoDuplicate))
289 return "noduplicate";
290 if (hasAttribute(Attribute::NoImplicitFloat))
291 return "noimplicitfloat";
292 if (hasAttribute(Attribute::NoInline))
293 return "noinline";
294 if (hasAttribute(Attribute::NonLazyBind))
295 return "nonlazybind";
Nick Lewyckyd52b1522014-05-20 01:23:40 +0000296 if (hasAttribute(Attribute::NonNull))
297 return "nonnull";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000298 if (hasAttribute(Attribute::NoRedZone))
299 return "noredzone";
300 if (hasAttribute(Attribute::NoReturn))
301 return "noreturn";
Oren Ben Simhonfdd72fd2018-03-17 13:29:46 +0000302 if (hasAttribute(Attribute::NoCfCheck))
303 return "nocf_check";
James Molloye6f87ca2015-11-06 10:32:53 +0000304 if (hasAttribute(Attribute::NoRecurse))
305 return "norecurse";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000306 if (hasAttribute(Attribute::NoUnwind))
307 return "nounwind";
Matt Morehouse236cdaf2018-03-22 17:07:51 +0000308 if (hasAttribute(Attribute::OptForFuzzing))
309 return "optforfuzzing";
Andrea Di Biagio377496b2013-08-23 11:53:55 +0000310 if (hasAttribute(Attribute::OptimizeNone))
311 return "optnone";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000312 if (hasAttribute(Attribute::OptimizeForSize))
313 return "optsize";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000314 if (hasAttribute(Attribute::ReadNone))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000315 return "readnone";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000316 if (hasAttribute(Attribute::ReadOnly))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000317 return "readonly";
Nicolai Haehnle84c9f992016-07-04 08:01:29 +0000318 if (hasAttribute(Attribute::WriteOnly))
319 return "writeonly";
Stephen Linb8bd2322013-04-20 05:14:40 +0000320 if (hasAttribute(Attribute::Returned))
321 return "returned";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000322 if (hasAttribute(Attribute::ReturnsTwice))
323 return "returns_twice";
324 if (hasAttribute(Attribute::SExt))
325 return "signext";
Matt Arsenaultb19b57e2017-04-28 20:25:27 +0000326 if (hasAttribute(Attribute::Speculatable))
327 return "speculatable";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000328 if (hasAttribute(Attribute::StackProtect))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000329 return "ssp";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000330 if (hasAttribute(Attribute::StackProtectReq))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000331 return "sspreq";
Bill Wendlingd154e2832013-01-23 06:41:41 +0000332 if (hasAttribute(Attribute::StackProtectStrong))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000333 return "sspstrong";
Peter Collingbourne82437bf2015-06-15 21:07:11 +0000334 if (hasAttribute(Attribute::SafeStack))
335 return "safestack";
Vlad Tsyrklevichd17f61e2018-04-03 20:10:40 +0000336 if (hasAttribute(Attribute::ShadowCallStack))
337 return "shadowcallstack";
Andrew Kaylor53a5fbb2017-08-14 21:15:13 +0000338 if (hasAttribute(Attribute::StrictFP))
339 return "strictfp";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000340 if (hasAttribute(Attribute::StructRet))
341 return "sret";
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000342 if (hasAttribute(Attribute::SanitizeThread))
343 return "sanitize_thread";
344 if (hasAttribute(Attribute::SanitizeMemory))
345 return "sanitize_memory";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000346 if (hasAttribute(Attribute::UWTable))
347 return "uwtable";
348 if (hasAttribute(Attribute::ZExt))
349 return "zeroext";
Diego Novilloc6399532013-05-24 12:26:52 +0000350 if (hasAttribute(Attribute::Cold))
351 return "cold";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000352
353 // FIXME: These should be output like this:
354 //
355 // align=4
356 // alignstack=8
357 //
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000358 if (hasAttribute(Attribute::Alignment)) {
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000359 std::string Result;
Bill Wendling829b4782013-02-11 08:43:33 +0000360 Result += "align";
361 Result += (InAttrGrp) ? "=" : " ";
Bill Wendling3f12ac22013-02-05 22:37:24 +0000362 Result += utostr(getValueAsInt());
363 return Result;
364 }
Bill Wendling829b4782013-02-11 08:43:33 +0000365
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000366 auto AttrWithBytesToString = [&](const char *Name) {
Bill Wendling3f12ac22013-02-05 22:37:24 +0000367 std::string Result;
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000368 Result += Name;
Bill Wendling829b4782013-02-11 08:43:33 +0000369 if (InAttrGrp) {
370 Result += "=";
371 Result += utostr(getValueAsInt());
372 } else {
373 Result += "(";
374 Result += utostr(getValueAsInt());
375 Result += ")";
376 }
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000377 return Result;
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000378 };
Bill Wendling9c2eba92013-01-31 20:59:05 +0000379
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000380 if (hasAttribute(Attribute::StackAlignment))
381 return AttrWithBytesToString("alignstack");
382
383 if (hasAttribute(Attribute::Dereferenceable))
384 return AttrWithBytesToString("dereferenceable");
385
386 if (hasAttribute(Attribute::DereferenceableOrNull))
387 return AttrWithBytesToString("dereferenceable_or_null");
Hal Finkelb0407ba2014-07-18 15:51:28 +0000388
George Burgess IV278199f2016-04-12 01:05:35 +0000389 if (hasAttribute(Attribute::AllocSize)) {
390 unsigned ElemSize;
391 Optional<unsigned> NumElems;
392 std::tie(ElemSize, NumElems) = getAllocSizeArgs();
393
394 std::string Result = "allocsize(";
395 Result += utostr(ElemSize);
396 if (NumElems.hasValue()) {
397 Result += ',';
398 Result += utostr(*NumElems);
399 }
400 Result += ')';
401 return Result;
402 }
403
Bill Wendling9c2eba92013-01-31 20:59:05 +0000404 // Convert target-dependent attributes to strings of the form:
405 //
406 // "kind"
407 // "kind" = "value"
Bill Wendling9c2eba92013-01-31 20:59:05 +0000408 //
Bill Wendling3f12ac22013-02-05 22:37:24 +0000409 if (isStringAttribute()) {
Bill Wendling9c2eba92013-01-31 20:59:05 +0000410 std::string Result;
Yaron Keren075759a2015-03-30 15:42:36 +0000411 Result += (Twine('"') + getKindAsString() + Twine('"')).str();
Bill Wendling9c2eba92013-01-31 20:59:05 +0000412
Honggyu Kim9eb6a102016-09-01 11:44:06 +0000413 std::string AttrVal = pImpl->getValueAsString();
414 if (AttrVal.empty()) return Result;
Bill Wendling7a33f772013-02-01 22:32:30 +0000415
Honggyu Kim9eb6a102016-09-01 11:44:06 +0000416 // Since some attribute strings contain special characters that cannot be
417 // printable, those have to be escaped to make the attribute value printable
418 // as is. e.g. "\01__gnu_mcount_nc"
419 {
420 raw_string_ostream OS(Result);
421 OS << "=\"";
Jonas Devlieghere745918ff2018-05-31 17:01:42 +0000422 printEscapedString(AttrVal, OS);
Honggyu Kim9eb6a102016-09-01 11:44:06 +0000423 OS << "\"";
424 }
Bill Wendlingc79cdff2013-02-01 01:04:27 +0000425 return Result;
Bill Wendling9c2eba92013-01-31 20:59:05 +0000426 }
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000427
428 llvm_unreachable("Unknown attribute");
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000429}
430
Bill Wendlingd509a662013-01-29 00:34:06 +0000431bool Attribute::operator<(Attribute A) const {
432 if (!pImpl && !A.pImpl) return false;
433 if (!pImpl) return true;
434 if (!A.pImpl) return false;
435 return *pImpl < *A.pImpl;
436}
437
Bill Wendlingd509a662013-01-29 00:34:06 +0000438//===----------------------------------------------------------------------===//
439// AttributeImpl Definition
440//===----------------------------------------------------------------------===//
441
Eric Christopher0eaa5412014-07-02 22:05:40 +0000442// Pin the vtables to this file.
Eugene Zelenkodeaf6952017-02-17 00:00:09 +0000443AttributeImpl::~AttributeImpl() = default;
444
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +0000445void EnumAttributeImpl::anchor() {}
Eugene Zelenkodeaf6952017-02-17 00:00:09 +0000446
Hal Finkele15442c2014-07-18 06:51:55 +0000447void IntAttributeImpl::anchor() {}
Eugene Zelenkodeaf6952017-02-17 00:00:09 +0000448
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +0000449void StringAttributeImpl::anchor() {}
Alexey Samsonov49109a22013-11-18 09:31:53 +0000450
Bill Wendlingd509a662013-01-29 00:34:06 +0000451bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
Bill Wendling3f12ac22013-02-05 22:37:24 +0000452 if (isStringAttribute()) return false;
453 return getKindAsEnum() == A;
Bill Wendlingd509a662013-01-29 00:34:06 +0000454}
455
Bill Wendling3f12ac22013-02-05 22:37:24 +0000456bool AttributeImpl::hasAttribute(StringRef Kind) const {
457 if (!isStringAttribute()) return false;
458 return getKindAsString() == Kind;
Bill Wendlingd509a662013-01-29 00:34:06 +0000459}
460
Bill Wendling3f12ac22013-02-05 22:37:24 +0000461Attribute::AttrKind AttributeImpl::getKindAsEnum() const {
Hal Finkele15442c2014-07-18 06:51:55 +0000462 assert(isEnumAttribute() || isIntAttribute());
Benjamin Kramer741146b2013-07-11 12:13:16 +0000463 return static_cast<const EnumAttributeImpl *>(this)->getEnumKind();
Bill Wendlingd509a662013-01-29 00:34:06 +0000464}
465
Bill Wendling3f12ac22013-02-05 22:37:24 +0000466uint64_t AttributeImpl::getValueAsInt() const {
Hal Finkele15442c2014-07-18 06:51:55 +0000467 assert(isIntAttribute());
468 return static_cast<const IntAttributeImpl *>(this)->getValue();
Bill Wendlingd509a662013-01-29 00:34:06 +0000469}
470
Bill Wendling3f12ac22013-02-05 22:37:24 +0000471StringRef AttributeImpl::getKindAsString() const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000472 assert(isStringAttribute());
473 return static_cast<const StringAttributeImpl *>(this)->getStringKind();
Bill Wendlingd509a662013-01-29 00:34:06 +0000474}
475
Bill Wendling3f12ac22013-02-05 22:37:24 +0000476StringRef AttributeImpl::getValueAsString() const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000477 assert(isStringAttribute());
478 return static_cast<const StringAttributeImpl *>(this)->getStringValue();
Bill Wendlingd509a662013-01-29 00:34:06 +0000479}
480
481bool AttributeImpl::operator<(const AttributeImpl &AI) const {
Bill Wendlingc79cdff2013-02-01 01:04:27 +0000482 // This sorts the attributes with Attribute::AttrKinds coming first (sorted
483 // relative to their enum value) and then strings.
Bill Wendling26b95752013-02-15 05:25:26 +0000484 if (isEnumAttribute()) {
485 if (AI.isEnumAttribute()) return getKindAsEnum() < AI.getKindAsEnum();
Hal Finkele15442c2014-07-18 06:51:55 +0000486 if (AI.isIntAttribute()) return true;
Bill Wendling26b95752013-02-15 05:25:26 +0000487 if (AI.isStringAttribute()) return true;
488 }
Bill Wendlingc79cdff2013-02-01 01:04:27 +0000489
Hal Finkele15442c2014-07-18 06:51:55 +0000490 if (isIntAttribute()) {
Bill Wendling26b95752013-02-15 05:25:26 +0000491 if (AI.isEnumAttribute()) return false;
Reid Kleckner7de67612016-04-04 23:06:05 +0000492 if (AI.isIntAttribute()) {
493 if (getKindAsEnum() == AI.getKindAsEnum())
494 return getValueAsInt() < AI.getValueAsInt();
495 return getKindAsEnum() < AI.getKindAsEnum();
496 }
Bill Wendling26b95752013-02-15 05:25:26 +0000497 if (AI.isStringAttribute()) return true;
Bill Wendling3f12ac22013-02-05 22:37:24 +0000498 }
Bill Wendlingd509a662013-01-29 00:34:06 +0000499
Bill Wendling26b95752013-02-15 05:25:26 +0000500 if (AI.isEnumAttribute()) return false;
Hal Finkele15442c2014-07-18 06:51:55 +0000501 if (AI.isIntAttribute()) return false;
Bill Wendling26b95752013-02-15 05:25:26 +0000502 if (getKindAsString() == AI.getKindAsString())
503 return getValueAsString() < AI.getValueAsString();
504 return getKindAsString() < AI.getKindAsString();
Bill Wendlingd509a662013-01-29 00:34:06 +0000505}
506
Bill Wendlingd509a662013-01-29 00:34:06 +0000507//===----------------------------------------------------------------------===//
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000508// AttributeSet Definition
509//===----------------------------------------------------------------------===//
510
511AttributeSet AttributeSet::get(LLVMContext &C, const AttrBuilder &B) {
512 return AttributeSet(AttributeSetNode::get(C, B));
513}
514
515AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<Attribute> Attrs) {
516 return AttributeSet(AttributeSetNode::get(C, Attrs));
517}
518
Javed Absarf3d79042017-05-11 12:28:08 +0000519AttributeSet AttributeSet::addAttribute(LLVMContext &C,
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000520 Attribute::AttrKind Kind) const {
Javed Absarf3d79042017-05-11 12:28:08 +0000521 if (hasAttribute(Kind)) return *this;
522 AttrBuilder B;
523 B.addAttribute(Kind);
524 return addAttributes(C, AttributeSet::get(C, B));
525}
526
527AttributeSet AttributeSet::addAttribute(LLVMContext &C, StringRef Kind,
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000528 StringRef Value) const {
Javed Absarf3d79042017-05-11 12:28:08 +0000529 AttrBuilder B;
530 B.addAttribute(Kind, Value);
531 return addAttributes(C, AttributeSet::get(C, B));
532}
533
534AttributeSet AttributeSet::addAttributes(LLVMContext &C,
535 const AttributeSet AS) const {
536 if (!hasAttributes())
537 return AS;
538
539 if (!AS.hasAttributes())
540 return *this;
541
542 AttrBuilder B(AS);
Eugene Zelenko7fb5d412018-03-30 00:47:31 +0000543 for (const auto I : *this)
Javed Absarf3d79042017-05-11 12:28:08 +0000544 B.addAttribute(I);
545
546 return get(C, B);
547}
548
549AttributeSet AttributeSet::removeAttribute(LLVMContext &C,
550 Attribute::AttrKind Kind) const {
551 if (!hasAttribute(Kind)) return *this;
Daniel Neilson88dddb82018-01-17 19:15:21 +0000552 AttrBuilder B(*this);
553 B.removeAttribute(Kind);
554 return get(C, B);
Javed Absarf3d79042017-05-11 12:28:08 +0000555}
556
557AttributeSet AttributeSet::removeAttribute(LLVMContext &C,
558 StringRef Kind) const {
559 if (!hasAttribute(Kind)) return *this;
Daniel Neilson88dddb82018-01-17 19:15:21 +0000560 AttrBuilder B(*this);
561 B.removeAttribute(Kind);
562 return get(C, B);
Javed Absarf3d79042017-05-11 12:28:08 +0000563}
564
565AttributeSet AttributeSet::removeAttributes(LLVMContext &C,
566 const AttrBuilder &Attrs) const {
Javed Absarf3d79042017-05-11 12:28:08 +0000567 AttrBuilder B(*this);
568 B.remove(Attrs);
569 return get(C, B);
570}
571
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000572unsigned AttributeSet::getNumAttributes() const {
573 return SetNode ? SetNode->getNumAttributes() : 0;
574}
575
576bool AttributeSet::hasAttribute(Attribute::AttrKind Kind) const {
Eugene Zelenkod761e2c2017-05-15 21:57:41 +0000577 return SetNode ? SetNode->hasAttribute(Kind) : false;
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000578}
579
580bool AttributeSet::hasAttribute(StringRef Kind) const {
Eugene Zelenkod761e2c2017-05-15 21:57:41 +0000581 return SetNode ? SetNode->hasAttribute(Kind) : false;
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000582}
583
584Attribute AttributeSet::getAttribute(Attribute::AttrKind Kind) const {
585 return SetNode ? SetNode->getAttribute(Kind) : Attribute();
586}
587
588Attribute AttributeSet::getAttribute(StringRef Kind) const {
589 return SetNode ? SetNode->getAttribute(Kind) : Attribute();
590}
591
592unsigned AttributeSet::getAlignment() const {
593 return SetNode ? SetNode->getAlignment() : 0;
594}
595
596unsigned AttributeSet::getStackAlignment() const {
597 return SetNode ? SetNode->getStackAlignment() : 0;
598}
599
600uint64_t AttributeSet::getDereferenceableBytes() const {
601 return SetNode ? SetNode->getDereferenceableBytes() : 0;
602}
603
604uint64_t AttributeSet::getDereferenceableOrNullBytes() const {
605 return SetNode ? SetNode->getDereferenceableOrNullBytes() : 0;
606}
607
608std::pair<unsigned, Optional<unsigned>> AttributeSet::getAllocSizeArgs() const {
Konstantin Zhuravlyov6df95b72017-04-12 23:57:37 +0000609 return SetNode ? SetNode->getAllocSizeArgs()
610 : std::pair<unsigned, Optional<unsigned>>(0, 0);
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000611}
612
613std::string AttributeSet::getAsString(bool InAttrGrp) const {
614 return SetNode ? SetNode->getAsString(InAttrGrp) : "";
615}
616
617AttributeSet::iterator AttributeSet::begin() const {
618 return SetNode ? SetNode->begin() : nullptr;
619}
620
621AttributeSet::iterator AttributeSet::end() const {
622 return SetNode ? SetNode->end() : nullptr;
623}
624
Aaron Ballman615eb472017-10-15 14:32:27 +0000625#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Javed Absarf3d79042017-05-11 12:28:08 +0000626LLVM_DUMP_METHOD void AttributeSet::dump() const {
627 dbgs() << "AS =\n";
628 dbgs() << " { ";
629 dbgs() << getAsString(true) << " }\n";
630}
631#endif
632
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000633//===----------------------------------------------------------------------===//
Bill Wendlingd509a662013-01-29 00:34:06 +0000634// AttributeSetNode Definition
635//===----------------------------------------------------------------------===//
636
Reid Kleckner8ff77852017-04-10 23:46:08 +0000637AttributeSetNode::AttributeSetNode(ArrayRef<Attribute> Attrs)
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000638 : AvailableAttrs(0), NumAttrs(Attrs.size()) {
Reid Kleckner8ff77852017-04-10 23:46:08 +0000639 // There's memory after the node where we can store the entries in.
640 std::copy(Attrs.begin(), Attrs.end(), getTrailingObjects<Attribute>());
641
Eugene Zelenko7fb5d412018-03-30 00:47:31 +0000642 for (const auto I : *this) {
Reid Kleckner8ff77852017-04-10 23:46:08 +0000643 if (!I.isStringAttribute()) {
644 AvailableAttrs |= ((uint64_t)1) << I.getKindAsEnum();
645 }
646 }
647}
648
Bill Wendlingd509a662013-01-29 00:34:06 +0000649AttributeSetNode *AttributeSetNode::get(LLVMContext &C,
650 ArrayRef<Attribute> Attrs) {
651 if (Attrs.empty())
Craig Topperc6207612014-04-09 06:08:46 +0000652 return nullptr;
Bill Wendlingd509a662013-01-29 00:34:06 +0000653
654 // Otherwise, build a key to look up the existing attributes.
655 LLVMContextImpl *pImpl = C.pImpl;
656 FoldingSetNodeID ID;
657
658 SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end());
Mandeep Singh Grangf3555652018-04-05 21:52:24 +0000659 llvm::sort(SortedAttrs.begin(), SortedAttrs.end());
Bill Wendlingd509a662013-01-29 00:34:06 +0000660
Eugene Zelenko7fb5d412018-03-30 00:47:31 +0000661 for (const auto Attr : SortedAttrs)
George Burgess IV500d3032015-12-16 05:21:02 +0000662 Attr.Profile(ID);
Bill Wendlingd509a662013-01-29 00:34:06 +0000663
664 void *InsertPoint;
665 AttributeSetNode *PA =
666 pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint);
667
668 // If we didn't find any existing attributes of the same shape then create a
669 // new one and insert it.
670 if (!PA) {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000671 // Coallocate entries after the AttributeSetNode itself.
James Y Knightaa365b22015-08-05 22:57:34 +0000672 void *Mem = ::operator new(totalSizeToAlloc<Attribute>(SortedAttrs.size()));
Benjamin Kramer741146b2013-07-11 12:13:16 +0000673 PA = new (Mem) AttributeSetNode(SortedAttrs);
Bill Wendlingd509a662013-01-29 00:34:06 +0000674 pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint);
675 }
676
Reid Klecknerb5180542017-03-21 16:57:19 +0000677 // Return the AttributeSetNode that we found or created.
Bill Wendlingd509a662013-01-29 00:34:06 +0000678 return PA;
679}
680
Reid Klecknereb9dd5b2017-04-10 23:31:05 +0000681AttributeSetNode *AttributeSetNode::get(LLVMContext &C, const AttrBuilder &B) {
682 // Add target-independent attributes.
683 SmallVector<Attribute, 8> Attrs;
684 for (Attribute::AttrKind Kind = Attribute::None;
685 Kind != Attribute::EndAttrKinds; Kind = Attribute::AttrKind(Kind + 1)) {
686 if (!B.contains(Kind))
687 continue;
688
689 Attribute Attr;
690 switch (Kind) {
691 case Attribute::Alignment:
692 Attr = Attribute::getWithAlignment(C, B.getAlignment());
693 break;
694 case Attribute::StackAlignment:
695 Attr = Attribute::getWithStackAlignment(C, B.getStackAlignment());
696 break;
697 case Attribute::Dereferenceable:
698 Attr = Attribute::getWithDereferenceableBytes(
699 C, B.getDereferenceableBytes());
700 break;
701 case Attribute::DereferenceableOrNull:
702 Attr = Attribute::getWithDereferenceableOrNullBytes(
703 C, B.getDereferenceableOrNullBytes());
704 break;
705 case Attribute::AllocSize: {
706 auto A = B.getAllocSizeArgs();
707 Attr = Attribute::getWithAllocSizeArgs(C, A.first, A.second);
708 break;
709 }
710 default:
711 Attr = Attribute::get(C, Kind);
712 }
713 Attrs.push_back(Attr);
714 }
715
716 // Add target-dependent (string) attributes.
717 for (const auto &TDA : B.td_attrs())
718 Attrs.emplace_back(Attribute::get(C, TDA.first, TDA.second));
719
720 return get(C, Attrs);
721}
722
Bill Wendlingbce7b972013-02-13 08:42:21 +0000723bool AttributeSetNode::hasAttribute(StringRef Kind) const {
Eugene Zelenko7fb5d412018-03-30 00:47:31 +0000724 for (const auto I : *this)
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000725 if (I.hasAttribute(Kind))
Bill Wendlingbce7b972013-02-13 08:42:21 +0000726 return true;
727 return false;
728}
729
730Attribute AttributeSetNode::getAttribute(Attribute::AttrKind Kind) const {
Matthias Braun31eeb76f2016-01-29 22:25:13 +0000731 if (hasAttribute(Kind)) {
Eugene Zelenko7fb5d412018-03-30 00:47:31 +0000732 for (const auto I : *this)
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000733 if (I.hasAttribute(Kind))
734 return I;
Matthias Braun31eeb76f2016-01-29 22:25:13 +0000735 }
Eugene Zelenko7fb5d412018-03-30 00:47:31 +0000736 return {};
Bill Wendlingbce7b972013-02-13 08:42:21 +0000737}
738
739Attribute AttributeSetNode::getAttribute(StringRef Kind) const {
Eugene Zelenko7fb5d412018-03-30 00:47:31 +0000740 for (const auto I : *this)
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000741 if (I.hasAttribute(Kind))
742 return I;
Eugene Zelenko7fb5d412018-03-30 00:47:31 +0000743 return {};
Bill Wendlingbce7b972013-02-13 08:42:21 +0000744}
745
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000746unsigned AttributeSetNode::getAlignment() const {
Eugene Zelenko7fb5d412018-03-30 00:47:31 +0000747 for (const auto I : *this)
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000748 if (I.hasAttribute(Attribute::Alignment))
749 return I.getAlignment();
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000750 return 0;
751}
752
753unsigned AttributeSetNode::getStackAlignment() const {
Eugene Zelenko7fb5d412018-03-30 00:47:31 +0000754 for (const auto I : *this)
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000755 if (I.hasAttribute(Attribute::StackAlignment))
756 return I.getStackAlignment();
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000757 return 0;
758}
759
Hal Finkelb0407ba2014-07-18 15:51:28 +0000760uint64_t AttributeSetNode::getDereferenceableBytes() const {
Eugene Zelenko7fb5d412018-03-30 00:47:31 +0000761 for (const auto I : *this)
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000762 if (I.hasAttribute(Attribute::Dereferenceable))
763 return I.getDereferenceableBytes();
Hal Finkelb0407ba2014-07-18 15:51:28 +0000764 return 0;
765}
766
Sanjoy Das06cf33f2015-05-06 17:41:54 +0000767uint64_t AttributeSetNode::getDereferenceableOrNullBytes() const {
Eugene Zelenko7fb5d412018-03-30 00:47:31 +0000768 for (const auto I : *this)
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000769 if (I.hasAttribute(Attribute::DereferenceableOrNull))
770 return I.getDereferenceableOrNullBytes();
Sanjoy Das06cf33f2015-05-06 17:41:54 +0000771 return 0;
772}
773
George Burgess IV278199f2016-04-12 01:05:35 +0000774std::pair<unsigned, Optional<unsigned>>
775AttributeSetNode::getAllocSizeArgs() const {
Eugene Zelenko7fb5d412018-03-30 00:47:31 +0000776 for (const auto I : *this)
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000777 if (I.hasAttribute(Attribute::AllocSize))
778 return I.getAllocSizeArgs();
George Burgess IV278199f2016-04-12 01:05:35 +0000779 return std::make_pair(0, 0);
780}
781
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +0000782std::string AttributeSetNode::getAsString(bool InAttrGrp) const {
Benjamin Kramer0baf8f42013-04-19 11:43:21 +0000783 std::string Str;
Benjamin Kramer741146b2013-07-11 12:13:16 +0000784 for (iterator I = begin(), E = end(); I != E; ++I) {
785 if (I != begin())
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +0000786 Str += ' ';
787 Str += I->getAsString(InAttrGrp);
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000788 }
789 return Str;
790}
791
Bill Wendlingd509a662013-01-29 00:34:06 +0000792//===----------------------------------------------------------------------===//
Reid Klecknerb5180542017-03-21 16:57:19 +0000793// AttributeListImpl Definition
Bill Wendlingd509a662013-01-29 00:34:06 +0000794//===----------------------------------------------------------------------===//
795
Reid Kleckner51b2cd82017-10-11 01:40:38 +0000796/// Map from AttributeList index to the internal array index. Adding one happens
797/// to work, but it relies on unsigned integer wrapping. MSVC warns about
798/// unsigned wrapping in constexpr functions, so write out the conditional. LLVM
799/// folds it to add anyway.
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000800static constexpr unsigned attrIdxToArrayIdx(unsigned Index) {
Reid Kleckner51b2cd82017-10-11 01:40:38 +0000801 return Index == AttributeList::FunctionIndex ? 0 : Index + 1;
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000802}
803
804AttributeListImpl::AttributeListImpl(LLVMContext &C,
805 ArrayRef<AttributeSet> Sets)
806 : AvailableFunctionAttrs(0), Context(C), NumAttrSets(Sets.size()) {
807 assert(!Sets.empty() && "pointless AttributeListImpl");
Reid Klecknera82be602017-04-11 00:16:00 +0000808
809 // There's memory after the node where we can store the entries in.
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000810 std::copy(Sets.begin(), Sets.end(), getTrailingObjects<AttributeSet>());
Reid Klecknera82be602017-04-11 00:16:00 +0000811
812 // Initialize AvailableFunctionAttrs summary bitset.
Reid Klecknerec0fc032017-04-12 22:22:01 +0000813 static_assert(Attribute::EndAttrKinds <=
814 sizeof(AvailableFunctionAttrs) * CHAR_BIT,
815 "Too many attributes");
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000816 static_assert(attrIdxToArrayIdx(AttributeList::FunctionIndex) == 0U,
817 "function should be stored in slot 0");
Eugene Zelenko7fb5d412018-03-30 00:47:31 +0000818 for (const auto I : Sets[0]) {
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000819 if (!I.isStringAttribute())
820 AvailableFunctionAttrs |= 1ULL << I.getKindAsEnum();
Reid Klecknera82be602017-04-11 00:16:00 +0000821 }
822}
823
824void AttributeListImpl::Profile(FoldingSetNodeID &ID) const {
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000825 Profile(ID, makeArrayRef(begin(), end()));
Reid Klecknera82be602017-04-11 00:16:00 +0000826}
827
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000828void AttributeListImpl::Profile(FoldingSetNodeID &ID,
829 ArrayRef<AttributeSet> Sets) {
830 for (const auto &Set : Sets)
831 ID.AddPointer(Set.SetNode);
Reid Klecknera82be602017-04-11 00:16:00 +0000832}
833
Aaron Ballman615eb472017-10-15 14:32:27 +0000834#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Reid Klecknerb5180542017-03-21 16:57:19 +0000835LLVM_DUMP_METHOD void AttributeListImpl::dump() const {
836 AttributeList(const_cast<AttributeListImpl *>(this)).dump();
Peter Collingbourneabca2ec2013-08-02 22:34:30 +0000837}
Matthias Braun8c209aa2017-01-28 02:02:38 +0000838#endif
Peter Collingbourneabca2ec2013-08-02 22:34:30 +0000839
Bill Wendlingd509a662013-01-29 00:34:06 +0000840//===----------------------------------------------------------------------===//
Reid Klecknerb5180542017-03-21 16:57:19 +0000841// AttributeList Construction and Mutation Methods
Bill Wendlingd509a662013-01-29 00:34:06 +0000842//===----------------------------------------------------------------------===//
843
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000844AttributeList AttributeList::getImpl(LLVMContext &C,
845 ArrayRef<AttributeSet> AttrSets) {
846 assert(!AttrSets.empty() && "pointless AttributeListImpl");
Reid Klecknereb9dd5b2017-04-10 23:31:05 +0000847
Bill Wendlingd509a662013-01-29 00:34:06 +0000848 LLVMContextImpl *pImpl = C.pImpl;
849 FoldingSetNodeID ID;
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000850 AttributeListImpl::Profile(ID, AttrSets);
Bill Wendlingd509a662013-01-29 00:34:06 +0000851
852 void *InsertPoint;
Reid Klecknerb5180542017-03-21 16:57:19 +0000853 AttributeListImpl *PA =
854 pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
Bill Wendlingd509a662013-01-29 00:34:06 +0000855
856 // If we didn't find any existing attributes of the same shape then
857 // create a new one and insert it.
858 if (!PA) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000859 // Coallocate entries after the AttributeListImpl itself.
James Y Knightaa365b22015-08-05 22:57:34 +0000860 void *Mem = ::operator new(
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000861 AttributeListImpl::totalSizeToAlloc<AttributeSet>(AttrSets.size()));
862 PA = new (Mem) AttributeListImpl(C, AttrSets);
Bill Wendlingd509a662013-01-29 00:34:06 +0000863 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
864 }
865
866 // Return the AttributesList that we found or created.
Reid Klecknerb5180542017-03-21 16:57:19 +0000867 return AttributeList(PA);
Bill Wendlingd509a662013-01-29 00:34:06 +0000868}
869
Reid Klecknerb5180542017-03-21 16:57:19 +0000870AttributeList
871AttributeList::get(LLVMContext &C,
872 ArrayRef<std::pair<unsigned, Attribute>> Attrs) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000873 // If there are no attributes then return a null AttributesList pointer.
874 if (Attrs.empty())
Eugene Zelenko7fb5d412018-03-30 00:47:31 +0000875 return {};
Bill Wendlingd509a662013-01-29 00:34:06 +0000876
Craig Toppere30b8ca2016-01-03 19:43:40 +0000877 assert(std::is_sorted(Attrs.begin(), Attrs.end(),
878 [](const std::pair<unsigned, Attribute> &LHS,
879 const std::pair<unsigned, Attribute> &RHS) {
880 return LHS.first < RHS.first;
881 }) && "Misordered Attributes list!");
Eugene Zelenko7fb5d412018-03-30 00:47:31 +0000882 assert(llvm::none_of(Attrs,
883 [](const std::pair<unsigned, Attribute> &Pair) {
884 return Pair.second.hasAttribute(Attribute::None);
885 }) &&
David Majnemer0a16c222016-08-11 21:15:00 +0000886 "Pointless attribute!");
Bill Wendlingd509a662013-01-29 00:34:06 +0000887
888 // Create a vector if (unsigned, AttributeSetNode*) pairs from the attributes
889 // list.
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000890 SmallVector<std::pair<unsigned, AttributeSet>, 8> AttrPairVec;
Eugene Zelenkodeaf6952017-02-17 00:00:09 +0000891 for (ArrayRef<std::pair<unsigned, Attribute>>::iterator I = Attrs.begin(),
Bill Wendlingd509a662013-01-29 00:34:06 +0000892 E = Attrs.end(); I != E; ) {
893 unsigned Index = I->first;
894 SmallVector<Attribute, 4> AttrVec;
NAKAMURA Takumif05d2f22013-01-29 15:18:16 +0000895 while (I != E && I->first == Index) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000896 AttrVec.push_back(I->second);
897 ++I;
898 }
899
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000900 AttrPairVec.emplace_back(Index, AttributeSet::get(C, AttrVec));
Bill Wendlingd509a662013-01-29 00:34:06 +0000901 }
902
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000903 return get(C, AttrPairVec);
Bill Wendlingd509a662013-01-29 00:34:06 +0000904}
905
Reid Klecknerb5180542017-03-21 16:57:19 +0000906AttributeList
907AttributeList::get(LLVMContext &C,
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000908 ArrayRef<std::pair<unsigned, AttributeSet>> Attrs) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000909 // If there are no attributes then return a null AttributesList pointer.
910 if (Attrs.empty())
Eugene Zelenko7fb5d412018-03-30 00:47:31 +0000911 return {};
Bill Wendlingd509a662013-01-29 00:34:06 +0000912
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000913 assert(std::is_sorted(Attrs.begin(), Attrs.end(),
914 [](const std::pair<unsigned, AttributeSet> &LHS,
915 const std::pair<unsigned, AttributeSet> &RHS) {
916 return LHS.first < RHS.first;
917 }) &&
918 "Misordered Attributes list!");
Eugene Zelenko7fb5d412018-03-30 00:47:31 +0000919 assert(llvm::none_of(Attrs,
920 [](const std::pair<unsigned, AttributeSet> &Pair) {
921 return !Pair.second.hasAttributes();
922 }) &&
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000923 "Pointless attribute!");
924
925 unsigned MaxIndex = Attrs.back().first;
Craig Topperf8642502018-04-16 17:05:01 +0000926 // If the MaxIndex is FunctionIndex and there are other indices in front
927 // of it, we need to use the largest of those to get the right size.
928 if (MaxIndex == FunctionIndex && Attrs.size() > 1)
929 MaxIndex = Attrs[Attrs.size() - 2].first;
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000930
931 SmallVector<AttributeSet, 4> AttrVec(attrIdxToArrayIdx(MaxIndex) + 1);
Eugene Zelenko7fb5d412018-03-30 00:47:31 +0000932 for (const auto Pair : Attrs)
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000933 AttrVec[attrIdxToArrayIdx(Pair.first)] = Pair.second;
934
935 return getImpl(C, AttrVec);
Bill Wendlingd509a662013-01-29 00:34:06 +0000936}
937
Reid Kleckner7f720332017-04-13 00:58:09 +0000938AttributeList AttributeList::get(LLVMContext &C, AttributeSet FnAttrs,
939 AttributeSet RetAttrs,
940 ArrayRef<AttributeSet> ArgAttrs) {
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000941 // Scan from the end to find the last argument with attributes. Most
942 // arguments don't have attributes, so it's nice if we can have fewer unique
943 // AttributeListImpls by dropping empty attribute sets at the end of the list.
944 unsigned NumSets = 0;
945 for (size_t I = ArgAttrs.size(); I != 0; --I) {
946 if (ArgAttrs[I - 1].hasAttributes()) {
947 NumSets = I + 2;
948 break;
949 }
Reid Klecknereb9dd5b2017-04-10 23:31:05 +0000950 }
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000951 if (NumSets == 0) {
952 // Check function and return attributes if we didn't have argument
953 // attributes.
954 if (RetAttrs.hasAttributes())
955 NumSets = 2;
956 else if (FnAttrs.hasAttributes())
957 NumSets = 1;
958 }
959
960 // If all attribute sets were empty, we can use the empty attribute list.
961 if (NumSets == 0)
Eugene Zelenko7fb5d412018-03-30 00:47:31 +0000962 return {};
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000963
964 SmallVector<AttributeSet, 8> AttrSets;
965 AttrSets.reserve(NumSets);
966 // If we have any attributes, we always have function attributes.
967 AttrSets.push_back(FnAttrs);
968 if (NumSets > 1)
969 AttrSets.push_back(RetAttrs);
970 if (NumSets > 2) {
971 // Drop the empty argument attribute sets at the end.
972 ArgAttrs = ArgAttrs.take_front(NumSets - 2);
973 AttrSets.insert(AttrSets.end(), ArgAttrs.begin(), ArgAttrs.end());
974 }
975
976 return getImpl(C, AttrSets);
Reid Klecknereb9dd5b2017-04-10 23:31:05 +0000977}
978
Reid Klecknerb5180542017-03-21 16:57:19 +0000979AttributeList AttributeList::get(LLVMContext &C, unsigned Index,
980 const AttrBuilder &B) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000981 if (!B.hasAttributes())
Eugene Zelenko7fb5d412018-03-30 00:47:31 +0000982 return {};
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000983 Index = attrIdxToArrayIdx(Index);
984 SmallVector<AttributeSet, 8> AttrSets(Index + 1);
985 AttrSets[Index] = AttributeSet::get(C, B);
986 return getImpl(C, AttrSets);
Bill Wendlingd509a662013-01-29 00:34:06 +0000987}
988
Reid Klecknerb5180542017-03-21 16:57:19 +0000989AttributeList AttributeList::get(LLVMContext &C, unsigned Index,
990 ArrayRef<Attribute::AttrKind> Kinds) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000991 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
Eugene Zelenko7fb5d412018-03-30 00:47:31 +0000992 for (const auto K : Kinds)
David Majnemer0a16c222016-08-11 21:15:00 +0000993 Attrs.emplace_back(Index, Attribute::get(C, K));
Bill Wendlingd509a662013-01-29 00:34:06 +0000994 return get(C, Attrs);
995}
996
Reid Klecknerb5180542017-03-21 16:57:19 +0000997AttributeList AttributeList::get(LLVMContext &C, unsigned Index,
998 ArrayRef<StringRef> Kinds) {
Amaury Sechet6100adf2016-06-15 17:50:39 +0000999 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
Eugene Zelenko7fb5d412018-03-30 00:47:31 +00001000 for (const auto K : Kinds)
David Majnemer0a16c222016-08-11 21:15:00 +00001001 Attrs.emplace_back(Index, Attribute::get(C, K));
Amaury Sechet6100adf2016-06-15 17:50:39 +00001002 return get(C, Attrs);
1003}
1004
Reid Klecknerb5180542017-03-21 16:57:19 +00001005AttributeList AttributeList::get(LLVMContext &C,
1006 ArrayRef<AttributeList> Attrs) {
1007 if (Attrs.empty())
Eugene Zelenko7fb5d412018-03-30 00:47:31 +00001008 return {};
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001009 if (Attrs.size() == 1)
1010 return Attrs[0];
Bill Wendlingd509a662013-01-29 00:34:06 +00001011
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001012 unsigned MaxSize = 0;
Eugene Zelenko7fb5d412018-03-30 00:47:31 +00001013 for (const auto List : Attrs)
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001014 MaxSize = std::max(MaxSize, List.getNumAttrSets());
1015
Reid Kleckner1d7cbdf2017-05-31 14:24:06 +00001016 // If every list was empty, there is no point in merging the lists.
1017 if (MaxSize == 0)
Eugene Zelenko7fb5d412018-03-30 00:47:31 +00001018 return {};
Reid Kleckner1d7cbdf2017-05-31 14:24:06 +00001019
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001020 SmallVector<AttributeSet, 8> NewAttrSets(MaxSize);
1021 for (unsigned I = 0; I < MaxSize; ++I) {
1022 AttrBuilder CurBuilder;
Eugene Zelenko7fb5d412018-03-30 00:47:31 +00001023 for (const auto List : Attrs)
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001024 CurBuilder.merge(List.getAttributes(I - 1));
1025 NewAttrSets[I] = AttributeSet::get(C, CurBuilder);
Bill Wendlingd509a662013-01-29 00:34:06 +00001026 }
1027
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001028 return getImpl(C, NewAttrSets);
Bill Wendlingd509a662013-01-29 00:34:06 +00001029}
1030
Reid Klecknerb5180542017-03-21 16:57:19 +00001031AttributeList AttributeList::addAttribute(LLVMContext &C, unsigned Index,
1032 Attribute::AttrKind Kind) const {
Amaury Sechet392638d2016-06-14 20:27:35 +00001033 if (hasAttribute(Index, Kind)) return *this;
Reid Kleckneree4930b2017-05-02 22:07:37 +00001034 AttrBuilder B;
1035 B.addAttribute(Kind);
1036 return addAttributes(C, Index, B);
Reed Kotler795c7b42013-03-13 20:20:08 +00001037}
1038
Reid Klecknerb5180542017-03-21 16:57:19 +00001039AttributeList AttributeList::addAttribute(LLVMContext &C, unsigned Index,
1040 StringRef Kind,
1041 StringRef Value) const {
Eugene Zelenkodeaf6952017-02-17 00:00:09 +00001042 AttrBuilder B;
Bill Wendling3b2f6102013-07-25 18:34:24 +00001043 B.addAttribute(Kind, Value);
Reid Kleckneree4930b2017-05-02 22:07:37 +00001044 return addAttributes(C, Index, B);
Bill Wendling3b2f6102013-07-25 18:34:24 +00001045}
1046
Reid Kleckner5fbdd172017-05-31 19:23:09 +00001047AttributeList AttributeList::addAttribute(LLVMContext &C, unsigned Index,
Reid Klecknerb5180542017-03-21 16:57:19 +00001048 Attribute A) const {
Reid Kleckner5fbdd172017-05-31 19:23:09 +00001049 AttrBuilder B;
1050 B.addAttribute(A);
1051 return addAttributes(C, Index, B);
Akira Hatanaka237916b2015-12-02 06:58:49 +00001052}
1053
Reid Klecknerb5180542017-03-21 16:57:19 +00001054AttributeList AttributeList::addAttributes(LLVMContext &C, unsigned Index,
Reid Kleckner61906252017-04-19 01:51:13 +00001055 const AttrBuilder &B) const {
1056 if (!B.hasAttributes())
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00001057 return *this;
1058
Reid Klecknerfe64c012017-04-18 22:10:18 +00001059 if (!pImpl)
Reid Kleckner61906252017-04-19 01:51:13 +00001060 return AttributeList::get(C, {{Index, AttributeSet::get(C, B)}});
Reid Klecknerfe64c012017-04-18 22:10:18 +00001061
Bill Wendlingd509a662013-01-29 00:34:06 +00001062#ifndef NDEBUG
1063 // FIXME it is not obvious how this should work for alignment. For now, say
1064 // we can't change a known alignment.
Reid Klecknerbf6b3b152017-05-19 22:23:47 +00001065 unsigned OldAlign = getAttributes(Index).getAlignment();
Reid Kleckner61906252017-04-19 01:51:13 +00001066 unsigned NewAlign = B.getAlignment();
Bill Wendlingd509a662013-01-29 00:34:06 +00001067 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
1068 "Attempt to change alignment!");
1069#endif
1070
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001071 Index = attrIdxToArrayIdx(Index);
1072 SmallVector<AttributeSet, 4> AttrSets(this->begin(), this->end());
1073 if (Index >= AttrSets.size())
1074 AttrSets.resize(Index + 1);
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00001075
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001076 AttrBuilder Merged(AttrSets[Index]);
1077 Merged.merge(B);
1078 AttrSets[Index] = AttributeSet::get(C, Merged);
Bill Wendlingd509a662013-01-29 00:34:06 +00001079
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001080 return getImpl(C, AttrSets);
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00001081}
1082
Reid Kleckner5fbdd172017-05-31 19:23:09 +00001083AttributeList AttributeList::addParamAttribute(LLVMContext &C,
1084 ArrayRef<unsigned> ArgNos,
1085 Attribute A) const {
1086 assert(std::is_sorted(ArgNos.begin(), ArgNos.end()));
1087
1088 SmallVector<AttributeSet, 4> AttrSets(this->begin(), this->end());
1089 unsigned MaxIndex = attrIdxToArrayIdx(ArgNos.back() + FirstArgIndex);
1090 if (MaxIndex >= AttrSets.size())
1091 AttrSets.resize(MaxIndex + 1);
1092
1093 for (unsigned ArgNo : ArgNos) {
1094 unsigned Index = attrIdxToArrayIdx(ArgNo + FirstArgIndex);
1095 AttrBuilder B(AttrSets[Index]);
1096 B.addAttribute(A);
1097 AttrSets[Index] = AttributeSet::get(C, B);
1098 }
1099
1100 return getImpl(C, AttrSets);
1101}
1102
Reid Klecknerb5180542017-03-21 16:57:19 +00001103AttributeList AttributeList::removeAttribute(LLVMContext &C, unsigned Index,
1104 Attribute::AttrKind Kind) const {
Amaury Sechet392638d2016-06-14 20:27:35 +00001105 if (!hasAttribute(Index, Kind)) return *this;
Daniel Neilson88dddb82018-01-17 19:15:21 +00001106
1107 Index = attrIdxToArrayIdx(Index);
1108 SmallVector<AttributeSet, 4> AttrSets(this->begin(), this->end());
1109 assert(Index < AttrSets.size());
1110
1111 AttrSets[Index] = AttrSets[Index].removeAttribute(C, Kind);
1112
1113 return getImpl(C, AttrSets);
Bill Wendlingd509a662013-01-29 00:34:06 +00001114}
1115
Reid Klecknerb5180542017-03-21 16:57:19 +00001116AttributeList AttributeList::removeAttribute(LLVMContext &C, unsigned Index,
1117 StringRef Kind) const {
Amaury Sechet6100adf2016-06-15 17:50:39 +00001118 if (!hasAttribute(Index, Kind)) return *this;
Daniel Neilson88dddb82018-01-17 19:15:21 +00001119
1120 Index = attrIdxToArrayIdx(Index);
1121 SmallVector<AttributeSet, 4> AttrSets(this->begin(), this->end());
1122 assert(Index < AttrSets.size());
1123
1124 AttrSets[Index] = AttrSets[Index].removeAttribute(C, Kind);
1125
1126 return getImpl(C, AttrSets);
Bill Wendlingd509a662013-01-29 00:34:06 +00001127}
1128
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001129AttributeList
1130AttributeList::removeAttributes(LLVMContext &C, unsigned Index,
1131 const AttrBuilder &AttrsToRemove) const {
Reid Klecknerb5180542017-03-21 16:57:19 +00001132 if (!pImpl)
Eugene Zelenko7fb5d412018-03-30 00:47:31 +00001133 return {};
Pete Cooperd2a44612015-05-06 23:19:43 +00001134
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001135 Index = attrIdxToArrayIdx(Index);
1136 SmallVector<AttributeSet, 4> AttrSets(this->begin(), this->end());
1137 if (Index >= AttrSets.size())
1138 AttrSets.resize(Index + 1);
Pete Cooperd2a44612015-05-06 23:19:43 +00001139
Daniel Neilson88dddb82018-01-17 19:15:21 +00001140 AttrSets[Index] = AttrSets[Index].removeAttributes(C, AttrsToRemove);
Pete Cooperd2a44612015-05-06 23:19:43 +00001141
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001142 return getImpl(C, AttrSets);
Pete Cooperd2a44612015-05-06 23:19:43 +00001143}
1144
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00001145AttributeList AttributeList::removeAttributes(LLVMContext &C,
1146 unsigned WithoutIndex) const {
1147 if (!pImpl)
Eugene Zelenko7fb5d412018-03-30 00:47:31 +00001148 return {};
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001149 WithoutIndex = attrIdxToArrayIdx(WithoutIndex);
1150 if (WithoutIndex >= getNumAttrSets())
1151 return *this;
1152 SmallVector<AttributeSet, 4> AttrSets(this->begin(), this->end());
1153 AttrSets[WithoutIndex] = AttributeSet();
1154 return getImpl(C, AttrSets);
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00001155}
1156
Reid Klecknerb5180542017-03-21 16:57:19 +00001157AttributeList AttributeList::addDereferenceableAttr(LLVMContext &C,
1158 unsigned Index,
1159 uint64_t Bytes) const {
Eugene Zelenkodeaf6952017-02-17 00:00:09 +00001160 AttrBuilder B;
Ramkumar Ramachandra8fcb4982015-02-14 19:37:54 +00001161 B.addDereferenceableAttr(Bytes);
Reid Kleckneree4930b2017-05-02 22:07:37 +00001162 return addAttributes(C, Index, B);
Ramkumar Ramachandra8fcb4982015-02-14 19:37:54 +00001163}
1164
Reid Klecknerb5180542017-03-21 16:57:19 +00001165AttributeList
1166AttributeList::addDereferenceableOrNullAttr(LLVMContext &C, unsigned Index,
1167 uint64_t Bytes) const {
Eugene Zelenkodeaf6952017-02-17 00:00:09 +00001168 AttrBuilder B;
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001169 B.addDereferenceableOrNullAttr(Bytes);
Reid Kleckneree4930b2017-05-02 22:07:37 +00001170 return addAttributes(C, Index, B);
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001171}
1172
Reid Klecknerb5180542017-03-21 16:57:19 +00001173AttributeList
1174AttributeList::addAllocSizeAttr(LLVMContext &C, unsigned Index,
1175 unsigned ElemSizeArg,
1176 const Optional<unsigned> &NumElemsArg) {
Eugene Zelenkodeaf6952017-02-17 00:00:09 +00001177 AttrBuilder B;
George Burgess IV278199f2016-04-12 01:05:35 +00001178 B.addAllocSizeAttr(ElemSizeArg, NumElemsArg);
Reid Kleckneree4930b2017-05-02 22:07:37 +00001179 return addAttributes(C, Index, B);
George Burgess IV278199f2016-04-12 01:05:35 +00001180}
1181
Bill Wendlingd509a662013-01-29 00:34:06 +00001182//===----------------------------------------------------------------------===//
Reid Klecknerb5180542017-03-21 16:57:19 +00001183// AttributeList Accessor Methods
Bill Wendlingd509a662013-01-29 00:34:06 +00001184//===----------------------------------------------------------------------===//
1185
Reid Klecknerb5180542017-03-21 16:57:19 +00001186LLVMContext &AttributeList::getContext() const { return pImpl->getContext(); }
1187
Reid Klecknerf021fab2017-04-13 23:12:13 +00001188AttributeSet AttributeList::getParamAttributes(unsigned ArgNo) const {
Reid Klecknera0b45f42017-05-03 18:17:31 +00001189 return getAttributes(ArgNo + FirstArgIndex);
Bill Wendling5d020a32013-02-10 05:00:40 +00001190}
1191
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001192AttributeSet AttributeList::getRetAttributes() const {
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00001193 return getAttributes(ReturnIndex);
Bill Wendlingd509a662013-01-29 00:34:06 +00001194}
1195
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001196AttributeSet AttributeList::getFnAttributes() const {
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00001197 return getAttributes(FunctionIndex);
Bill Wendlingd509a662013-01-29 00:34:06 +00001198}
1199
Reid Klecknerb5180542017-03-21 16:57:19 +00001200bool AttributeList::hasAttribute(unsigned Index,
1201 Attribute::AttrKind Kind) const {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001202 return getAttributes(Index).hasAttribute(Kind);
Bill Wendlingd509a662013-01-29 00:34:06 +00001203}
1204
Reid Klecknerb5180542017-03-21 16:57:19 +00001205bool AttributeList::hasAttribute(unsigned Index, StringRef Kind) const {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001206 return getAttributes(Index).hasAttribute(Kind);
Bill Wendlingbce7b972013-02-13 08:42:21 +00001207}
1208
Reid Klecknerb5180542017-03-21 16:57:19 +00001209bool AttributeList::hasAttributes(unsigned Index) const {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001210 return getAttributes(Index).hasAttributes();
Bill Wendlingd509a662013-01-29 00:34:06 +00001211}
1212
Reid Klecknerb5180542017-03-21 16:57:19 +00001213bool AttributeList::hasFnAttribute(Attribute::AttrKind Kind) const {
Matthias Braun33282812016-01-29 22:25:19 +00001214 return pImpl && pImpl->hasFnAttribute(Kind);
1215}
1216
Reid Klecknerb5180542017-03-21 16:57:19 +00001217bool AttributeList::hasFnAttribute(StringRef Kind) const {
1218 return hasAttribute(AttributeList::FunctionIndex, Kind);
Amaury Sechet5f04d812016-09-09 04:50:38 +00001219}
1220
Reid Klecknerf021fab2017-04-13 23:12:13 +00001221bool AttributeList::hasParamAttribute(unsigned ArgNo,
1222 Attribute::AttrKind Kind) const {
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001223 return hasAttribute(ArgNo + FirstArgIndex, Kind);
Reid Klecknerf021fab2017-04-13 23:12:13 +00001224}
1225
Reid Klecknerb5180542017-03-21 16:57:19 +00001226bool AttributeList::hasAttrSomewhere(Attribute::AttrKind Attr,
1227 unsigned *Index) const {
Craig Topperc6207612014-04-09 06:08:46 +00001228 if (!pImpl) return false;
Bill Wendlingd509a662013-01-29 00:34:06 +00001229
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001230 for (unsigned I = index_begin(), E = index_end(); I != E; ++I) {
1231 if (hasAttribute(I, Attr)) {
1232 if (Index)
1233 *Index = I;
1234 return true;
1235 }
1236 }
Bill Wendlingd509a662013-01-29 00:34:06 +00001237
1238 return false;
1239}
1240
Reid Klecknerb5180542017-03-21 16:57:19 +00001241Attribute AttributeList::getAttribute(unsigned Index,
1242 Attribute::AttrKind Kind) const {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001243 return getAttributes(Index).getAttribute(Kind);
Bill Wendlingbce7b972013-02-13 08:42:21 +00001244}
1245
Reid Klecknerb5180542017-03-21 16:57:19 +00001246Attribute AttributeList::getAttribute(unsigned Index, StringRef Kind) const {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001247 return getAttributes(Index).getAttribute(Kind);
Bill Wendlingbce7b972013-02-13 08:42:21 +00001248}
1249
Reid Kleckner859f8b52017-04-28 20:34:27 +00001250unsigned AttributeList::getRetAlignment() const {
1251 return getAttributes(ReturnIndex).getAlignment();
1252}
1253
1254unsigned AttributeList::getParamAlignment(unsigned ArgNo) const {
Reid Klecknera0b45f42017-05-03 18:17:31 +00001255 return getAttributes(ArgNo + FirstArgIndex).getAlignment();
Bill Wendlingd509a662013-01-29 00:34:06 +00001256}
1257
Reid Klecknerb5180542017-03-21 16:57:19 +00001258unsigned AttributeList::getStackAlignment(unsigned Index) const {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001259 return getAttributes(Index).getStackAlignment();
Bill Wendlingd509a662013-01-29 00:34:06 +00001260}
1261
Reid Klecknerb5180542017-03-21 16:57:19 +00001262uint64_t AttributeList::getDereferenceableBytes(unsigned Index) const {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001263 return getAttributes(Index).getDereferenceableBytes();
Hal Finkelb0407ba2014-07-18 15:51:28 +00001264}
1265
Reid Klecknerb5180542017-03-21 16:57:19 +00001266uint64_t AttributeList::getDereferenceableOrNullBytes(unsigned Index) const {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001267 return getAttributes(Index).getDereferenceableOrNullBytes();
Sanjoy Das06cf33f2015-05-06 17:41:54 +00001268}
1269
George Burgess IV278199f2016-04-12 01:05:35 +00001270std::pair<unsigned, Optional<unsigned>>
Reid Klecknerb5180542017-03-21 16:57:19 +00001271AttributeList::getAllocSizeArgs(unsigned Index) const {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001272 return getAttributes(Index).getAllocSizeArgs();
George Burgess IV278199f2016-04-12 01:05:35 +00001273}
1274
Reid Klecknerb5180542017-03-21 16:57:19 +00001275std::string AttributeList::getAsString(unsigned Index, bool InAttrGrp) const {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001276 return getAttributes(Index).getAsString(InAttrGrp);
Bill Wendlingd509a662013-01-29 00:34:06 +00001277}
1278
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001279AttributeSet AttributeList::getAttributes(unsigned Index) const {
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001280 Index = attrIdxToArrayIdx(Index);
1281 if (!pImpl || Index >= getNumAttrSets())
Eugene Zelenko7fb5d412018-03-30 00:47:31 +00001282 return {};
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001283 return pImpl->begin()[Index];
Bill Wendlingd509a662013-01-29 00:34:06 +00001284}
1285
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001286AttributeList::iterator AttributeList::begin() const {
1287 return pImpl ? pImpl->begin() : nullptr;
Bill Wendlinga9174862013-01-31 23:53:05 +00001288}
1289
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001290AttributeList::iterator AttributeList::end() const {
1291 return pImpl ? pImpl->end() : nullptr;
Bill Wendlinga9174862013-01-31 23:53:05 +00001292}
1293
Bill Wendlingd509a662013-01-29 00:34:06 +00001294//===----------------------------------------------------------------------===//
Reid Klecknerb5180542017-03-21 16:57:19 +00001295// AttributeList Introspection Methods
Bill Wendlingd509a662013-01-29 00:34:06 +00001296//===----------------------------------------------------------------------===//
1297
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001298unsigned AttributeList::getNumAttrSets() const {
1299 return pImpl ? pImpl->NumAttrSets : 0;
Bill Wendlingd509a662013-01-29 00:34:06 +00001300}
1301
Aaron Ballman615eb472017-10-15 14:32:27 +00001302#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Reid Klecknerb5180542017-03-21 16:57:19 +00001303LLVM_DUMP_METHOD void AttributeList::dump() const {
Bill Wendlingd509a662013-01-29 00:34:06 +00001304 dbgs() << "PAL[\n";
1305
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001306 for (unsigned i = index_begin(), e = index_end(); i != e; ++i) {
1307 if (getAttributes(i).hasAttributes())
1308 dbgs() << " { " << i << " => " << getAsString(i) << " }\n";
Bill Wendlingd509a662013-01-29 00:34:06 +00001309 }
1310
1311 dbgs() << "]\n";
1312}
Matthias Braun8c209aa2017-01-28 02:02:38 +00001313#endif
Bill Wendlingd509a662013-01-29 00:34:06 +00001314
Bill Wendlingabf3feb2012-10-05 06:44:41 +00001315//===----------------------------------------------------------------------===//
Bill Wendlingcd330342013-01-04 23:27:34 +00001316// AttrBuilder Method Implementations
Bill Wendlingabf3feb2012-10-05 06:44:41 +00001317//===----------------------------------------------------------------------===//
1318
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001319// FIXME: Remove this ctor, use AttributeSet.
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001320AttrBuilder::AttrBuilder(AttributeList AL, unsigned Index) {
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001321 AttributeSet AS = AL.getAttributes(Index);
Eugene Zelenko7fb5d412018-03-30 00:47:31 +00001322 for (const auto &A : AS)
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001323 addAttribute(A);
Bill Wendling096f5442013-01-07 08:24:35 +00001324}
1325
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001326AttrBuilder::AttrBuilder(AttributeSet AS) {
Eugene Zelenko7fb5d412018-03-30 00:47:31 +00001327 for (const auto &A : AS)
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001328 addAttribute(A);
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00001329}
1330
Bill Wendlingcd330342013-01-04 23:27:34 +00001331void AttrBuilder::clear() {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001332 Attrs.reset();
Sanjoy Das88d0fde2015-09-03 22:27:42 +00001333 TargetDepAttrs.clear();
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001334 Alignment = StackAlignment = DerefBytes = DerefOrNullBytes = 0;
George Burgess IV278199f2016-04-12 01:05:35 +00001335 AllocSizeArgs = 0;
Bill Wendlingcd330342013-01-04 23:27:34 +00001336}
1337
1338AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001339 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
Bill Wendling1c7cc8a2013-01-31 23:16:25 +00001340 assert(Val != Attribute::Alignment && Val != Attribute::StackAlignment &&
George Burgess IV278199f2016-04-12 01:05:35 +00001341 Val != Attribute::Dereferenceable && Val != Attribute::AllocSize &&
Hal Finkelb0407ba2014-07-18 15:51:28 +00001342 "Adding integer attribute without adding a value!");
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001343 Attrs[Val] = true;
Bill Wendling7c04e042012-10-09 19:01:18 +00001344 return *this;
Bill Wendlingabf3feb2012-10-05 06:44:41 +00001345}
1346
Bill Wendling23804da2013-01-31 23:38:01 +00001347AttrBuilder &AttrBuilder::addAttribute(Attribute Attr) {
Bill Wendling0a437302013-02-10 10:13:23 +00001348 if (Attr.isStringAttribute()) {
1349 addAttribute(Attr.getKindAsString(), Attr.getValueAsString());
1350 return *this;
1351 }
1352
Bill Wendling3f12ac22013-02-05 22:37:24 +00001353 Attribute::AttrKind Kind = Attr.getKindAsEnum();
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001354 Attrs[Kind] = true;
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001355
Bill Wendling3f12ac22013-02-05 22:37:24 +00001356 if (Kind == Attribute::Alignment)
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001357 Alignment = Attr.getAlignment();
Bill Wendling3f12ac22013-02-05 22:37:24 +00001358 else if (Kind == Attribute::StackAlignment)
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001359 StackAlignment = Attr.getStackAlignment();
Hal Finkelb0407ba2014-07-18 15:51:28 +00001360 else if (Kind == Attribute::Dereferenceable)
1361 DerefBytes = Attr.getDereferenceableBytes();
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001362 else if (Kind == Attribute::DereferenceableOrNull)
1363 DerefOrNullBytes = Attr.getDereferenceableOrNullBytes();
George Burgess IV278199f2016-04-12 01:05:35 +00001364 else if (Kind == Attribute::AllocSize)
1365 AllocSizeArgs = Attr.getValueAsInt();
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001366 return *this;
1367}
1368
Bill Wendlingb9c5b1a2013-02-05 08:09:32 +00001369AttrBuilder &AttrBuilder::addAttribute(StringRef A, StringRef V) {
1370 TargetDepAttrs[A] = V;
1371 return *this;
1372}
1373
Bill Wendling23804da2013-01-31 23:38:01 +00001374AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001375 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
1376 Attrs[Val] = false;
Bill Wendling23804da2013-01-31 23:38:01 +00001377
1378 if (Val == Attribute::Alignment)
1379 Alignment = 0;
1380 else if (Val == Attribute::StackAlignment)
1381 StackAlignment = 0;
Hal Finkelb0407ba2014-07-18 15:51:28 +00001382 else if (Val == Attribute::Dereferenceable)
1383 DerefBytes = 0;
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001384 else if (Val == Attribute::DereferenceableOrNull)
1385 DerefOrNullBytes = 0;
George Burgess IV278199f2016-04-12 01:05:35 +00001386 else if (Val == Attribute::AllocSize)
1387 AllocSizeArgs = 0;
Bill Wendling23804da2013-01-31 23:38:01 +00001388
1389 return *this;
1390}
1391
Reid Klecknerb5180542017-03-21 16:57:19 +00001392AttrBuilder &AttrBuilder::removeAttributes(AttributeList A, uint64_t Index) {
Reid Kleckner6652a522017-04-28 18:37:16 +00001393 remove(A.getAttributes(Index));
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001394 return *this;
1395}
1396
Bill Wendlingb9c5b1a2013-02-05 08:09:32 +00001397AttrBuilder &AttrBuilder::removeAttribute(StringRef A) {
Eugene Zelenko7fb5d412018-03-30 00:47:31 +00001398 auto I = TargetDepAttrs.find(A);
Bill Wendlingb9c5b1a2013-02-05 08:09:32 +00001399 if (I != TargetDepAttrs.end())
1400 TargetDepAttrs.erase(I);
1401 return *this;
1402}
1403
George Burgess IV278199f2016-04-12 01:05:35 +00001404std::pair<unsigned, Optional<unsigned>> AttrBuilder::getAllocSizeArgs() const {
1405 return unpackAllocSizeArgs(AllocSizeArgs);
1406}
1407
Bill Wendling50d27842012-10-15 20:35:56 +00001408AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
Bill Wendlingabd5ba22012-10-14 03:58:29 +00001409 if (Align == 0) return *this;
Bill Wendlingcd330342013-01-04 23:27:34 +00001410
Bill Wendlingabf3feb2012-10-05 06:44:41 +00001411 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1412 assert(Align <= 0x40000000 && "Alignment too large.");
Bill Wendlingcd330342013-01-04 23:27:34 +00001413
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001414 Attrs[Attribute::Alignment] = true;
Bill Wendlingcd330342013-01-04 23:27:34 +00001415 Alignment = Align;
Bill Wendlingabd5ba22012-10-14 03:58:29 +00001416 return *this;
Chris Lattner3e13b8c2008-01-02 23:42:30 +00001417}
1418
Bill Wendlingcd330342013-01-04 23:27:34 +00001419AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
1420 // Default alignment, allow the target to define how to align it.
1421 if (Align == 0) return *this;
1422
1423 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1424 assert(Align <= 0x100 && "Alignment too large.");
1425
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001426 Attrs[Attribute::StackAlignment] = true;
Bill Wendlingcd330342013-01-04 23:27:34 +00001427 StackAlignment = Align;
1428 return *this;
1429}
1430
Hal Finkelb0407ba2014-07-18 15:51:28 +00001431AttrBuilder &AttrBuilder::addDereferenceableAttr(uint64_t Bytes) {
1432 if (Bytes == 0) return *this;
1433
1434 Attrs[Attribute::Dereferenceable] = true;
1435 DerefBytes = Bytes;
1436 return *this;
1437}
1438
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001439AttrBuilder &AttrBuilder::addDereferenceableOrNullAttr(uint64_t Bytes) {
1440 if (Bytes == 0)
1441 return *this;
1442
1443 Attrs[Attribute::DereferenceableOrNull] = true;
1444 DerefOrNullBytes = Bytes;
1445 return *this;
1446}
1447
George Burgess IV278199f2016-04-12 01:05:35 +00001448AttrBuilder &AttrBuilder::addAllocSizeAttr(unsigned ElemSize,
1449 const Optional<unsigned> &NumElems) {
1450 return addAllocSizeAttrFromRawRepr(packAllocSizeArgs(ElemSize, NumElems));
1451}
1452
1453AttrBuilder &AttrBuilder::addAllocSizeAttrFromRawRepr(uint64_t RawArgs) {
1454 // (0, 0) is our "not present" value, so we need to check for it here.
1455 assert(RawArgs && "Invalid allocsize arguments -- given allocsize(0, 0)");
1456
1457 Attrs[Attribute::AllocSize] = true;
1458 // Reuse existing machinery to store this as a single 64-bit integer so we can
1459 // save a few bytes over using a pair<unsigned, Optional<unsigned>>.
1460 AllocSizeArgs = RawArgs;
1461 return *this;
1462}
1463
Bill Wendlinge2614922013-02-06 01:16:00 +00001464AttrBuilder &AttrBuilder::merge(const AttrBuilder &B) {
1465 // FIXME: What if both have alignments, but they don't match?!
1466 if (!Alignment)
1467 Alignment = B.Alignment;
1468
1469 if (!StackAlignment)
1470 StackAlignment = B.StackAlignment;
1471
Hal Finkelb0407ba2014-07-18 15:51:28 +00001472 if (!DerefBytes)
1473 DerefBytes = B.DerefBytes;
1474
Pete Cooperd2a44612015-05-06 23:19:43 +00001475 if (!DerefOrNullBytes)
1476 DerefOrNullBytes = B.DerefOrNullBytes;
1477
George Burgess IV278199f2016-04-12 01:05:35 +00001478 if (!AllocSizeArgs)
1479 AllocSizeArgs = B.AllocSizeArgs;
1480
Benjamin Kramer45e7d532013-02-16 19:13:18 +00001481 Attrs |= B.Attrs;
Bill Wendlinge2614922013-02-06 01:16:00 +00001482
Pete Cooperd2a44612015-05-06 23:19:43 +00001483 for (auto I : B.td_attrs())
1484 TargetDepAttrs[I.first] = I.second;
Bill Wendlinge2614922013-02-06 01:16:00 +00001485
1486 return *this;
1487}
1488
Pete Cooperd2a44612015-05-06 23:19:43 +00001489AttrBuilder &AttrBuilder::remove(const AttrBuilder &B) {
1490 // FIXME: What if both have alignments, but they don't match?!
1491 if (B.Alignment)
1492 Alignment = 0;
1493
1494 if (B.StackAlignment)
1495 StackAlignment = 0;
1496
1497 if (B.DerefBytes)
1498 DerefBytes = 0;
1499
1500 if (B.DerefOrNullBytes)
1501 DerefOrNullBytes = 0;
1502
George Burgess IV278199f2016-04-12 01:05:35 +00001503 if (B.AllocSizeArgs)
1504 AllocSizeArgs = 0;
1505
Pete Cooperd2a44612015-05-06 23:19:43 +00001506 Attrs &= ~B.Attrs;
1507
1508 for (auto I : B.td_attrs())
1509 TargetDepAttrs.erase(I.first);
1510
1511 return *this;
1512}
1513
1514bool AttrBuilder::overlaps(const AttrBuilder &B) const {
1515 // First check if any of the target independent attributes overlap.
1516 if ((Attrs & B.Attrs).any())
1517 return true;
1518
1519 // Then check if any target dependent ones do.
Sean Silva9011aca2017-02-22 06:34:04 +00001520 for (const auto &I : td_attrs())
Pete Cooperd2a44612015-05-06 23:19:43 +00001521 if (B.contains(I.first))
1522 return true;
1523
1524 return false;
1525}
1526
Bill Wendling4b001442013-02-06 01:33:42 +00001527bool AttrBuilder::contains(StringRef A) const {
1528 return TargetDepAttrs.find(A) != TargetDepAttrs.end();
1529}
1530
Bill Wendling50d27842012-10-15 20:35:56 +00001531bool AttrBuilder::hasAttributes() const {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001532 return !Attrs.none() || !TargetDepAttrs.empty();
Bill Wendlingc6daefa2012-10-08 23:27:46 +00001533}
Bill Wendling9ac69f92013-01-04 20:54:35 +00001534
Reid Kleckner6652a522017-04-28 18:37:16 +00001535bool AttrBuilder::hasAttributes(AttributeList AL, uint64_t Index) const {
1536 AttributeSet AS = AL.getAttributes(Index);
Bill Wendling9ca01da2013-02-02 00:42:06 +00001537
Eugene Zelenko7fb5d412018-03-30 00:47:31 +00001538 for (const auto Attr : AS) {
Hal Finkele15442c2014-07-18 06:51:55 +00001539 if (Attr.isEnumAttribute() || Attr.isIntAttribute()) {
Reid Kleckner6652a522017-04-28 18:37:16 +00001540 if (contains(Attr.getKindAsEnum()))
Bill Wendling7cde51d2013-02-12 07:56:49 +00001541 return true;
1542 } else {
1543 assert(Attr.isStringAttribute() && "Invalid attribute kind!");
Reid Kleckner6652a522017-04-28 18:37:16 +00001544 return contains(Attr.getKindAsString());
Bill Wendling7cde51d2013-02-12 07:56:49 +00001545 }
1546 }
Bill Wendling9ca01da2013-02-02 00:42:06 +00001547
1548 return false;
Bill Wendling70f39172012-10-09 00:01:21 +00001549}
Bill Wendling9ac69f92013-01-04 20:54:35 +00001550
Bill Wendling50d27842012-10-15 20:35:56 +00001551bool AttrBuilder::hasAlignmentAttr() const {
Bill Wendlingcd330342013-01-04 23:27:34 +00001552 return Alignment != 0;
Bill Wendlingc6daefa2012-10-08 23:27:46 +00001553}
1554
Bill Wendlingd509a662013-01-29 00:34:06 +00001555bool AttrBuilder::operator==(const AttrBuilder &B) {
Benjamin Kramer45e7d532013-02-16 19:13:18 +00001556 if (Attrs != B.Attrs)
1557 return false;
Bill Wendling4b001442013-02-06 01:33:42 +00001558
1559 for (td_const_iterator I = TargetDepAttrs.begin(),
1560 E = TargetDepAttrs.end(); I != E; ++I)
1561 if (B.TargetDepAttrs.find(I->first) == B.TargetDepAttrs.end())
1562 return false;
1563
Hal Finkelb0407ba2014-07-18 15:51:28 +00001564 return Alignment == B.Alignment && StackAlignment == B.StackAlignment &&
1565 DerefBytes == B.DerefBytes;
Bill Wendlingd509a662013-01-29 00:34:06 +00001566}
1567
Bill Wendling57625a42013-01-25 23:09:36 +00001568//===----------------------------------------------------------------------===//
1569// AttributeFuncs Function Defintions
1570//===----------------------------------------------------------------------===//
1571
Adrian Prantl5f8f34e42018-05-01 15:54:18 +00001572/// Which attributes cannot be applied to a type.
Craig Toppere3dcce92015-08-01 22:20:21 +00001573AttrBuilder AttributeFuncs::typeIncompatible(Type *Ty) {
Bill Wendling57625a42013-01-25 23:09:36 +00001574 AttrBuilder Incompatible;
1575
1576 if (!Ty->isIntegerTy())
1577 // Attribute that only apply to integers.
1578 Incompatible.addAttribute(Attribute::SExt)
1579 .addAttribute(Attribute::ZExt);
1580
1581 if (!Ty->isPointerTy())
1582 // Attribute that only apply to pointers.
1583 Incompatible.addAttribute(Attribute::ByVal)
1584 .addAttribute(Attribute::Nest)
1585 .addAttribute(Attribute::NoAlias)
1586 .addAttribute(Attribute::NoCapture)
Nick Lewyckyd52b1522014-05-20 01:23:40 +00001587 .addAttribute(Attribute::NonNull)
Hal Finkelb0407ba2014-07-18 15:51:28 +00001588 .addDereferenceableAttr(1) // the int here is ignored
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001589 .addDereferenceableOrNullAttr(1) // the int here is ignored
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001590 .addAttribute(Attribute::ReadNone)
1591 .addAttribute(Attribute::ReadOnly)
Reid Klecknera534a382013-12-19 02:14:12 +00001592 .addAttribute(Attribute::StructRet)
1593 .addAttribute(Attribute::InAlloca);
Bill Wendling57625a42013-01-25 23:09:36 +00001594
Pete Cooper2777d8872015-05-06 23:19:56 +00001595 return Incompatible;
Bill Wendling57625a42013-01-25 23:09:36 +00001596}
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001597
1598template<typename AttrClass>
1599static bool isEqual(const Function &Caller, const Function &Callee) {
1600 return Caller.getFnAttribute(AttrClass::getKind()) ==
1601 Callee.getFnAttribute(AttrClass::getKind());
1602}
1603
Adrian Prantl5f8f34e42018-05-01 15:54:18 +00001604/// Compute the logical AND of the attributes of the caller and the
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001605/// callee.
1606///
1607/// This function sets the caller's attribute to false if the callee's attribute
1608/// is false.
1609template<typename AttrClass>
1610static void setAND(Function &Caller, const Function &Callee) {
1611 if (AttrClass::isSet(Caller, AttrClass::getKind()) &&
1612 !AttrClass::isSet(Callee, AttrClass::getKind()))
1613 AttrClass::set(Caller, AttrClass::getKind(), false);
1614}
1615
Adrian Prantl5f8f34e42018-05-01 15:54:18 +00001616/// Compute the logical OR of the attributes of the caller and the
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001617/// callee.
1618///
1619/// This function sets the caller's attribute to true if the callee's attribute
1620/// is true.
1621template<typename AttrClass>
1622static void setOR(Function &Caller, const Function &Callee) {
1623 if (!AttrClass::isSet(Caller, AttrClass::getKind()) &&
1624 AttrClass::isSet(Callee, AttrClass::getKind()))
1625 AttrClass::set(Caller, AttrClass::getKind(), true);
1626}
1627
Adrian Prantl5f8f34e42018-05-01 15:54:18 +00001628/// If the inlined function had a higher stack protection level than the
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001629/// calling function, then bump up the caller's stack protection level.
1630static void adjustCallerSSPLevel(Function &Caller, const Function &Callee) {
1631 // If upgrading the SSP attribute, clear out the old SSP Attributes first.
1632 // Having multiple SSP attributes doesn't actually hurt, but it adds useless
1633 // clutter to the IR.
Reid Kleckneree4930b2017-05-02 22:07:37 +00001634 AttrBuilder OldSSPAttr;
1635 OldSSPAttr.addAttribute(Attribute::StackProtect)
1636 .addAttribute(Attribute::StackProtectStrong)
1637 .addAttribute(Attribute::StackProtectReq);
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001638
Evgeniy Stepanovf17120a2016-04-11 22:27:48 +00001639 if (Callee.hasFnAttribute(Attribute::StackProtectReq)) {
Reid Klecknerb5180542017-03-21 16:57:19 +00001640 Caller.removeAttributes(AttributeList::FunctionIndex, OldSSPAttr);
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001641 Caller.addFnAttr(Attribute::StackProtectReq);
1642 } else if (Callee.hasFnAttribute(Attribute::StackProtectStrong) &&
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001643 !Caller.hasFnAttribute(Attribute::StackProtectReq)) {
Reid Klecknerb5180542017-03-21 16:57:19 +00001644 Caller.removeAttributes(AttributeList::FunctionIndex, OldSSPAttr);
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001645 Caller.addFnAttr(Attribute::StackProtectStrong);
1646 } else if (Callee.hasFnAttribute(Attribute::StackProtect) &&
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001647 !Caller.hasFnAttribute(Attribute::StackProtectReq) &&
1648 !Caller.hasFnAttribute(Attribute::StackProtectStrong))
1649 Caller.addFnAttr(Attribute::StackProtect);
1650}
1651
Adrian Prantl5f8f34e42018-05-01 15:54:18 +00001652/// If the inlined function required stack probes, then ensure that
whitequarked54b4a2017-06-21 18:46:50 +00001653/// the calling function has those too.
1654static void adjustCallerStackProbes(Function &Caller, const Function &Callee) {
whitequark08b20352017-06-22 23:22:36 +00001655 if (!Caller.hasFnAttribute("probe-stack") &&
1656 Callee.hasFnAttribute("probe-stack")) {
1657 Caller.addFnAttr(Callee.getFnAttribute("probe-stack"));
1658 }
1659}
1660
Adrian Prantl5f8f34e42018-05-01 15:54:18 +00001661/// If the inlined function defines the size of guard region
whitequark08b20352017-06-22 23:22:36 +00001662/// on the stack, then ensure that the calling function defines a guard region
1663/// that is no larger.
1664static void
1665adjustCallerStackProbeSize(Function &Caller, const Function &Callee) {
1666 if (Callee.hasFnAttribute("stack-probe-size")) {
1667 uint64_t CalleeStackProbeSize;
1668 Callee.getFnAttribute("stack-probe-size")
1669 .getValueAsString()
1670 .getAsInteger(0, CalleeStackProbeSize);
1671 if (Caller.hasFnAttribute("stack-probe-size")) {
1672 uint64_t CallerStackProbeSize;
1673 Caller.getFnAttribute("stack-probe-size")
1674 .getValueAsString()
1675 .getAsInteger(0, CallerStackProbeSize);
1676 if (CallerStackProbeSize > CalleeStackProbeSize) {
1677 Caller.addFnAttr(Callee.getFnAttribute("stack-probe-size"));
1678 }
1679 } else {
1680 Caller.addFnAttr(Callee.getFnAttribute("stack-probe-size"));
1681 }
1682 }
whitequarked54b4a2017-06-21 18:46:50 +00001683}
1684
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001685#define GET_ATTR_COMPAT_FUNC
1686#include "AttributesCompatFunc.inc"
1687
1688bool AttributeFuncs::areInlineCompatible(const Function &Caller,
1689 const Function &Callee) {
1690 return hasCompatibleFnAttrs(Caller, Callee);
1691}
1692
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001693void AttributeFuncs::mergeAttributesForInlining(Function &Caller,
1694 const Function &Callee) {
1695 mergeFnAttrs(Caller, Callee);
1696}