blob: b6c16a8a10c9c0cb485b7e582ac5ce90a620c330 [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
11// \brief 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"
Eugene Zelenkodeaf6952017-02-17 00:00:09 +000027#include "llvm/IR/Function.h"
28#include "llvm/IR/LLVMContext.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000029#include "llvm/IR/Type.h"
Eugene Zelenkodeaf6952017-02-17 00:00:09 +000030#include "llvm/Support/Compiler.h"
David Greenef7014732010-01-05 01:29:58 +000031#include "llvm/Support/Debug.h"
Eugene Zelenkodeaf6952017-02-17 00:00:09 +000032#include "llvm/Support/ErrorHandling.h"
33#include "llvm/Support/MathExtras.h"
Benjamin Kramer1a25d732009-08-23 11:37:21 +000034#include "llvm/Support/raw_ostream.h"
Bill Wendlingd2e493b2013-01-24 00:06:56 +000035#include <algorithm>
Eugene Zelenkodeaf6952017-02-17 00:00:09 +000036#include <cassert>
Eugene Zelenkod761e2c2017-05-15 21:57:41 +000037#include <climits>
38#include <cstddef>
Eugene Zelenkodeaf6952017-02-17 00:00:09 +000039#include <cstdint>
40#include <limits>
Eugene Zelenkodeaf6952017-02-17 00:00:09 +000041#include <string>
42#include <tuple>
43#include <utility>
44
Chris Lattner3e13b8c2008-01-02 23:42:30 +000045using namespace llvm;
46
Chris Lattner8a923e72008-03-12 17:45:29 +000047//===----------------------------------------------------------------------===//
Bill Wendling7707c5a2013-01-29 00:48:16 +000048// Attribute Construction Methods
Chris Lattner8a923e72008-03-12 17:45:29 +000049//===----------------------------------------------------------------------===//
Chris Lattnerd0e1f102008-01-03 00:10:22 +000050
George Burgess IV278199f2016-04-12 01:05:35 +000051// allocsize has two integer arguments, but because they're both 32 bits, we can
52// pack them into one 64-bit value, at the cost of making said value
53// nonsensical.
54//
55// In order to do this, we need to reserve one value of the second (optional)
56// allocsize argument to signify "not present."
George Burgess IV381fc0e2016-08-25 01:05:08 +000057static const unsigned AllocSizeNumElemsNotPresent = -1;
George Burgess IV278199f2016-04-12 01:05:35 +000058
59static uint64_t packAllocSizeArgs(unsigned ElemSizeArg,
60 const Optional<unsigned> &NumElemsArg) {
61 assert((!NumElemsArg.hasValue() ||
62 *NumElemsArg != AllocSizeNumElemsNotPresent) &&
63 "Attempting to pack a reserved value");
64
65 return uint64_t(ElemSizeArg) << 32 |
66 NumElemsArg.getValueOr(AllocSizeNumElemsNotPresent);
67}
68
69static std::pair<unsigned, Optional<unsigned>>
70unpackAllocSizeArgs(uint64_t Num) {
71 unsigned NumElems = Num & std::numeric_limits<unsigned>::max();
72 unsigned ElemSizeArg = Num >> 32;
73
74 Optional<unsigned> NumElemsArg;
75 if (NumElems != AllocSizeNumElemsNotPresent)
76 NumElemsArg = NumElems;
77 return std::make_pair(ElemSizeArg, NumElemsArg);
78}
79
Matt Arsenaulte1cef6e2014-09-03 23:24:31 +000080Attribute Attribute::get(LLVMContext &Context, Attribute::AttrKind Kind,
81 uint64_t Val) {
82 LLVMContextImpl *pImpl = Context.pImpl;
83 FoldingSetNodeID ID;
84 ID.AddInteger(Kind);
Matt Arsenaultfb4308e2014-09-03 23:38:05 +000085 if (Val) ID.AddInteger(Val);
Matt Arsenaulte1cef6e2014-09-03 23:24:31 +000086
87 void *InsertPoint;
88 AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
89
90 if (!PA) {
91 // If we didn't find any existing attributes of the same shape then create a
92 // new one and insert it.
Matt Arsenaultfb4308e2014-09-03 23:38:05 +000093 if (!Val)
94 PA = new EnumAttributeImpl(Kind);
95 else
96 PA = new IntAttributeImpl(Kind, Val);
Bill Wendling3f12ac22013-02-05 22:37:24 +000097 pImpl->AttrsSet.InsertNode(PA, InsertPoint);
98 }
99
100 // Return the Attribute that we found or created.
101 return Attribute(PA);
102}
103
104Attribute Attribute::get(LLVMContext &Context, StringRef Kind, StringRef Val) {
105 LLVMContextImpl *pImpl = Context.pImpl;
106 FoldingSetNodeID ID;
107 ID.AddString(Kind);
108 if (!Val.empty()) ID.AddString(Val);
Bill Wendling73ea2de2012-10-08 21:47:17 +0000109
110 void *InsertPoint;
Bill Wendling4607f4b2012-12-20 01:36:59 +0000111 AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
Bill Wendling73ea2de2012-10-08 21:47:17 +0000112
113 if (!PA) {
114 // If we didn't find any existing attributes of the same shape then create a
115 // new one and insert it.
Benjamin Kramer741146b2013-07-11 12:13:16 +0000116 PA = new StringAttributeImpl(Kind, Val);
Bill Wendling73ea2de2012-10-08 21:47:17 +0000117 pImpl->AttrsSet.InsertNode(PA, InsertPoint);
118 }
119
Bill Wendlingb9c5b1a2013-02-05 08:09:32 +0000120 // Return the Attribute that we found or created.
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000121 return Attribute(PA);
Bill Wendling73ea2de2012-10-08 21:47:17 +0000122}
123
Bill Wendling4bbe9db2013-01-27 22:43:04 +0000124Attribute Attribute::getWithAlignment(LLVMContext &Context, uint64_t Align) {
Bill Wendling1c7cc8a2013-01-31 23:16:25 +0000125 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
126 assert(Align <= 0x40000000 && "Alignment too large.");
Bill Wendling3f12ac22013-02-05 22:37:24 +0000127 return get(Context, Alignment, Align);
Bill Wendling4bbe9db2013-01-27 22:43:04 +0000128}
129
130Attribute Attribute::getWithStackAlignment(LLVMContext &Context,
131 uint64_t Align) {
Bill Wendling1c7cc8a2013-01-31 23:16:25 +0000132 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
133 assert(Align <= 0x100 && "Alignment too large.");
Bill Wendling3f12ac22013-02-05 22:37:24 +0000134 return get(Context, StackAlignment, Align);
Bill Wendling4bbe9db2013-01-27 22:43:04 +0000135}
136
Hal Finkelb0407ba2014-07-18 15:51:28 +0000137Attribute Attribute::getWithDereferenceableBytes(LLVMContext &Context,
138 uint64_t Bytes) {
139 assert(Bytes && "Bytes must be non-zero.");
140 return get(Context, Dereferenceable, Bytes);
141}
142
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000143Attribute Attribute::getWithDereferenceableOrNullBytes(LLVMContext &Context,
144 uint64_t Bytes) {
145 assert(Bytes && "Bytes must be non-zero.");
146 return get(Context, DereferenceableOrNull, Bytes);
147}
148
George Burgess IV278199f2016-04-12 01:05:35 +0000149Attribute
150Attribute::getWithAllocSizeArgs(LLVMContext &Context, unsigned ElemSizeArg,
151 const Optional<unsigned> &NumElemsArg) {
152 assert(!(ElemSizeArg == 0 && NumElemsArg && *NumElemsArg == 0) &&
153 "Invalid allocsize arguments -- given allocsize(0, 0)");
154 return get(Context, AllocSize, packAllocSizeArgs(ElemSizeArg, NumElemsArg));
155}
156
Bill Wendling7707c5a2013-01-29 00:48:16 +0000157//===----------------------------------------------------------------------===//
158// Attribute Accessor Methods
159//===----------------------------------------------------------------------===//
160
Bill Wendling3f12ac22013-02-05 22:37:24 +0000161bool Attribute::isEnumAttribute() const {
162 return pImpl && pImpl->isEnumAttribute();
163}
164
Hal Finkele15442c2014-07-18 06:51:55 +0000165bool Attribute::isIntAttribute() const {
166 return pImpl && pImpl->isIntAttribute();
Bill Wendling3f12ac22013-02-05 22:37:24 +0000167}
168
169bool Attribute::isStringAttribute() const {
170 return pImpl && pImpl->isStringAttribute();
171}
172
173Attribute::AttrKind Attribute::getKindAsEnum() const {
Bill Wendling440e9d82013-07-25 00:34:29 +0000174 if (!pImpl) return None;
Hal Finkele15442c2014-07-18 06:51:55 +0000175 assert((isEnumAttribute() || isIntAttribute()) &&
Bill Wendling3f12ac22013-02-05 22:37:24 +0000176 "Invalid attribute type to get the kind as an enum!");
George Burgess IV500d3032015-12-16 05:21:02 +0000177 return pImpl->getKindAsEnum();
Bill Wendling3f12ac22013-02-05 22:37:24 +0000178}
179
180uint64_t Attribute::getValueAsInt() const {
Bill Wendling440e9d82013-07-25 00:34:29 +0000181 if (!pImpl) return 0;
Hal Finkele15442c2014-07-18 06:51:55 +0000182 assert(isIntAttribute() &&
183 "Expected the attribute to be an integer attribute!");
George Burgess IV500d3032015-12-16 05:21:02 +0000184 return pImpl->getValueAsInt();
Bill Wendling3f12ac22013-02-05 22:37:24 +0000185}
186
187StringRef Attribute::getKindAsString() const {
Eugene Zelenko7fb5d412018-03-30 00:47:31 +0000188 if (!pImpl) return {};
Bill Wendling3f12ac22013-02-05 22:37:24 +0000189 assert(isStringAttribute() &&
190 "Invalid attribute type to get the kind as a string!");
George Burgess IV500d3032015-12-16 05:21:02 +0000191 return pImpl->getKindAsString();
Bill Wendling3f12ac22013-02-05 22:37:24 +0000192}
193
194StringRef Attribute::getValueAsString() const {
Eugene Zelenko7fb5d412018-03-30 00:47:31 +0000195 if (!pImpl) return {};
Bill Wendling3f12ac22013-02-05 22:37:24 +0000196 assert(isStringAttribute() &&
197 "Invalid attribute type to get the value as a string!");
George Burgess IV500d3032015-12-16 05:21:02 +0000198 return pImpl->getValueAsString();
Bill Wendling3f12ac22013-02-05 22:37:24 +0000199}
200
Bill Wendlingae89a0f2013-02-05 23:48:36 +0000201bool Attribute::hasAttribute(AttrKind Kind) const {
202 return (pImpl && pImpl->hasAttribute(Kind)) || (!pImpl && Kind == None);
203}
204
205bool Attribute::hasAttribute(StringRef Kind) const {
206 if (!isStringAttribute()) return false;
207 return pImpl && pImpl->hasAttribute(Kind);
Bill Wendling03eefb32013-01-29 20:45:34 +0000208}
209
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000210unsigned Attribute::getAlignment() const {
Bill Wendlingc79cdff2013-02-01 01:04:27 +0000211 assert(hasAttribute(Attribute::Alignment) &&
212 "Trying to get alignment from non-alignment attribute!");
Bill Wendling3f12ac22013-02-05 22:37:24 +0000213 return pImpl->getValueAsInt();
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000214}
215
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000216unsigned Attribute::getStackAlignment() const {
Bill Wendlingc79cdff2013-02-01 01:04:27 +0000217 assert(hasAttribute(Attribute::StackAlignment) &&
218 "Trying to get alignment from non-alignment attribute!");
Bill Wendling3f12ac22013-02-05 22:37:24 +0000219 return pImpl->getValueAsInt();
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000220}
221
Hal Finkelb0407ba2014-07-18 15:51:28 +0000222uint64_t Attribute::getDereferenceableBytes() const {
223 assert(hasAttribute(Attribute::Dereferenceable) &&
224 "Trying to get dereferenceable bytes from "
225 "non-dereferenceable attribute!");
226 return pImpl->getValueAsInt();
227}
228
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000229uint64_t Attribute::getDereferenceableOrNullBytes() const {
230 assert(hasAttribute(Attribute::DereferenceableOrNull) &&
231 "Trying to get dereferenceable bytes from "
232 "non-dereferenceable attribute!");
233 return pImpl->getValueAsInt();
234}
235
George Burgess IV278199f2016-04-12 01:05:35 +0000236std::pair<unsigned, Optional<unsigned>> Attribute::getAllocSizeArgs() const {
237 assert(hasAttribute(Attribute::AllocSize) &&
238 "Trying to get allocsize args from non-allocsize attribute");
239 return unpackAllocSizeArgs(pImpl->getValueAsInt());
240}
241
Bill Wendling829b4782013-02-11 08:43:33 +0000242std::string Attribute::getAsString(bool InAttrGrp) const {
Eugene Zelenko7fb5d412018-03-30 00:47:31 +0000243 if (!pImpl) return {};
Bill Wendling9c2eba92013-01-31 20:59:05 +0000244
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000245 if (hasAttribute(Attribute::SanitizeAddress))
246 return "sanitize_address";
Evgeniy Stepanovc667c1f2017-12-09 00:21:41 +0000247 if (hasAttribute(Attribute::SanitizeHWAddress))
248 return "sanitize_hwaddress";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000249 if (hasAttribute(Attribute::AlwaysInline))
250 return "alwaysinline";
Igor Laevsky39d662f2015-07-11 10:30:36 +0000251 if (hasAttribute(Attribute::ArgMemOnly))
252 return "argmemonly";
Michael Gottesman41748d72013-06-27 00:25:01 +0000253 if (hasAttribute(Attribute::Builtin))
254 return "builtin";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000255 if (hasAttribute(Attribute::ByVal))
256 return "byval";
Owen Anderson85fa7d52015-05-26 23:48:40 +0000257 if (hasAttribute(Attribute::Convergent))
258 return "convergent";
Manman Ren9bfd0d02016-04-01 21:41:15 +0000259 if (hasAttribute(Attribute::SwiftError))
260 return "swifterror";
Manman Renf46262e2016-03-29 17:37:21 +0000261 if (hasAttribute(Attribute::SwiftSelf))
262 return "swiftself";
Vaivaswatha Nagarajfb3f4902015-12-16 16:16:19 +0000263 if (hasAttribute(Attribute::InaccessibleMemOnly))
264 return "inaccessiblememonly";
265 if (hasAttribute(Attribute::InaccessibleMemOrArgMemOnly))
266 return "inaccessiblemem_or_argmemonly";
Reid Klecknera534a382013-12-19 02:14:12 +0000267 if (hasAttribute(Attribute::InAlloca))
268 return "inalloca";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000269 if (hasAttribute(Attribute::InlineHint))
270 return "inlinehint";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000271 if (hasAttribute(Attribute::InReg))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000272 return "inreg";
Tom Roeder44cb65f2014-06-05 19:29:43 +0000273 if (hasAttribute(Attribute::JumpTable))
274 return "jumptable";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000275 if (hasAttribute(Attribute::MinSize))
276 return "minsize";
277 if (hasAttribute(Attribute::Naked))
278 return "naked";
279 if (hasAttribute(Attribute::Nest))
280 return "nest";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000281 if (hasAttribute(Attribute::NoAlias))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000282 return "noalias";
Bill Wendling09bd1f72013-02-22 00:12:35 +0000283 if (hasAttribute(Attribute::NoBuiltin))
284 return "nobuiltin";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000285 if (hasAttribute(Attribute::NoCapture))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000286 return "nocapture";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000287 if (hasAttribute(Attribute::NoDuplicate))
288 return "noduplicate";
289 if (hasAttribute(Attribute::NoImplicitFloat))
290 return "noimplicitfloat";
291 if (hasAttribute(Attribute::NoInline))
292 return "noinline";
293 if (hasAttribute(Attribute::NonLazyBind))
294 return "nonlazybind";
Nick Lewyckyd52b1522014-05-20 01:23:40 +0000295 if (hasAttribute(Attribute::NonNull))
296 return "nonnull";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000297 if (hasAttribute(Attribute::NoRedZone))
298 return "noredzone";
299 if (hasAttribute(Attribute::NoReturn))
300 return "noreturn";
Oren Ben Simhonfdd72fd2018-03-17 13:29:46 +0000301 if (hasAttribute(Attribute::NoCfCheck))
302 return "nocf_check";
James Molloye6f87ca2015-11-06 10:32:53 +0000303 if (hasAttribute(Attribute::NoRecurse))
304 return "norecurse";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000305 if (hasAttribute(Attribute::NoUnwind))
306 return "nounwind";
Matt Morehouse236cdaf2018-03-22 17:07:51 +0000307 if (hasAttribute(Attribute::OptForFuzzing))
308 return "optforfuzzing";
Andrea Di Biagio377496b2013-08-23 11:53:55 +0000309 if (hasAttribute(Attribute::OptimizeNone))
310 return "optnone";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000311 if (hasAttribute(Attribute::OptimizeForSize))
312 return "optsize";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000313 if (hasAttribute(Attribute::ReadNone))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000314 return "readnone";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000315 if (hasAttribute(Attribute::ReadOnly))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000316 return "readonly";
Nicolai Haehnle84c9f992016-07-04 08:01:29 +0000317 if (hasAttribute(Attribute::WriteOnly))
318 return "writeonly";
Stephen Linb8bd2322013-04-20 05:14:40 +0000319 if (hasAttribute(Attribute::Returned))
320 return "returned";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000321 if (hasAttribute(Attribute::ReturnsTwice))
322 return "returns_twice";
323 if (hasAttribute(Attribute::SExt))
324 return "signext";
Matt Arsenaultb19b57e2017-04-28 20:25:27 +0000325 if (hasAttribute(Attribute::Speculatable))
326 return "speculatable";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000327 if (hasAttribute(Attribute::StackProtect))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000328 return "ssp";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000329 if (hasAttribute(Attribute::StackProtectReq))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000330 return "sspreq";
Bill Wendlingd154e2832013-01-23 06:41:41 +0000331 if (hasAttribute(Attribute::StackProtectStrong))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000332 return "sspstrong";
Peter Collingbourne82437bf2015-06-15 21:07:11 +0000333 if (hasAttribute(Attribute::SafeStack))
334 return "safestack";
Vlad Tsyrklevichd17f61e2018-04-03 20:10:40 +0000335 if (hasAttribute(Attribute::ShadowCallStack))
336 return "shadowcallstack";
Andrew Kaylor53a5fbb2017-08-14 21:15:13 +0000337 if (hasAttribute(Attribute::StrictFP))
338 return "strictfp";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000339 if (hasAttribute(Attribute::StructRet))
340 return "sret";
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000341 if (hasAttribute(Attribute::SanitizeThread))
342 return "sanitize_thread";
343 if (hasAttribute(Attribute::SanitizeMemory))
344 return "sanitize_memory";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000345 if (hasAttribute(Attribute::UWTable))
346 return "uwtable";
347 if (hasAttribute(Attribute::ZExt))
348 return "zeroext";
Diego Novilloc6399532013-05-24 12:26:52 +0000349 if (hasAttribute(Attribute::Cold))
350 return "cold";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000351
352 // FIXME: These should be output like this:
353 //
354 // align=4
355 // alignstack=8
356 //
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000357 if (hasAttribute(Attribute::Alignment)) {
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000358 std::string Result;
Bill Wendling829b4782013-02-11 08:43:33 +0000359 Result += "align";
360 Result += (InAttrGrp) ? "=" : " ";
Bill Wendling3f12ac22013-02-05 22:37:24 +0000361 Result += utostr(getValueAsInt());
362 return Result;
363 }
Bill Wendling829b4782013-02-11 08:43:33 +0000364
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000365 auto AttrWithBytesToString = [&](const char *Name) {
Bill Wendling3f12ac22013-02-05 22:37:24 +0000366 std::string Result;
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000367 Result += Name;
Bill Wendling829b4782013-02-11 08:43:33 +0000368 if (InAttrGrp) {
369 Result += "=";
370 Result += utostr(getValueAsInt());
371 } else {
372 Result += "(";
373 Result += utostr(getValueAsInt());
374 Result += ")";
375 }
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000376 return Result;
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000377 };
Bill Wendling9c2eba92013-01-31 20:59:05 +0000378
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000379 if (hasAttribute(Attribute::StackAlignment))
380 return AttrWithBytesToString("alignstack");
381
382 if (hasAttribute(Attribute::Dereferenceable))
383 return AttrWithBytesToString("dereferenceable");
384
385 if (hasAttribute(Attribute::DereferenceableOrNull))
386 return AttrWithBytesToString("dereferenceable_or_null");
Hal Finkelb0407ba2014-07-18 15:51:28 +0000387
George Burgess IV278199f2016-04-12 01:05:35 +0000388 if (hasAttribute(Attribute::AllocSize)) {
389 unsigned ElemSize;
390 Optional<unsigned> NumElems;
391 std::tie(ElemSize, NumElems) = getAllocSizeArgs();
392
393 std::string Result = "allocsize(";
394 Result += utostr(ElemSize);
395 if (NumElems.hasValue()) {
396 Result += ',';
397 Result += utostr(*NumElems);
398 }
399 Result += ')';
400 return Result;
401 }
402
Bill Wendling9c2eba92013-01-31 20:59:05 +0000403 // Convert target-dependent attributes to strings of the form:
404 //
405 // "kind"
406 // "kind" = "value"
Bill Wendling9c2eba92013-01-31 20:59:05 +0000407 //
Bill Wendling3f12ac22013-02-05 22:37:24 +0000408 if (isStringAttribute()) {
Bill Wendling9c2eba92013-01-31 20:59:05 +0000409 std::string Result;
Yaron Keren075759a2015-03-30 15:42:36 +0000410 Result += (Twine('"') + getKindAsString() + Twine('"')).str();
Bill Wendling9c2eba92013-01-31 20:59:05 +0000411
Honggyu Kim9eb6a102016-09-01 11:44:06 +0000412 std::string AttrVal = pImpl->getValueAsString();
413 if (AttrVal.empty()) return Result;
Bill Wendling7a33f772013-02-01 22:32:30 +0000414
Honggyu Kim9eb6a102016-09-01 11:44:06 +0000415 // Since some attribute strings contain special characters that cannot be
416 // printable, those have to be escaped to make the attribute value printable
417 // as is. e.g. "\01__gnu_mcount_nc"
418 {
419 raw_string_ostream OS(Result);
420 OS << "=\"";
421 PrintEscapedString(AttrVal, OS);
422 OS << "\"";
423 }
Bill Wendlingc79cdff2013-02-01 01:04:27 +0000424 return Result;
Bill Wendling9c2eba92013-01-31 20:59:05 +0000425 }
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000426
427 llvm_unreachable("Unknown attribute");
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000428}
429
Bill Wendlingd509a662013-01-29 00:34:06 +0000430bool Attribute::operator<(Attribute A) const {
431 if (!pImpl && !A.pImpl) return false;
432 if (!pImpl) return true;
433 if (!A.pImpl) return false;
434 return *pImpl < *A.pImpl;
435}
436
Bill Wendlingd509a662013-01-29 00:34:06 +0000437//===----------------------------------------------------------------------===//
438// AttributeImpl Definition
439//===----------------------------------------------------------------------===//
440
Eric Christopher0eaa5412014-07-02 22:05:40 +0000441// Pin the vtables to this file.
Eugene Zelenkodeaf6952017-02-17 00:00:09 +0000442AttributeImpl::~AttributeImpl() = default;
443
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +0000444void EnumAttributeImpl::anchor() {}
Eugene Zelenkodeaf6952017-02-17 00:00:09 +0000445
Hal Finkele15442c2014-07-18 06:51:55 +0000446void IntAttributeImpl::anchor() {}
Eugene Zelenkodeaf6952017-02-17 00:00:09 +0000447
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +0000448void StringAttributeImpl::anchor() {}
Alexey Samsonov49109a22013-11-18 09:31:53 +0000449
Bill Wendlingd509a662013-01-29 00:34:06 +0000450bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
Bill Wendling3f12ac22013-02-05 22:37:24 +0000451 if (isStringAttribute()) return false;
452 return getKindAsEnum() == A;
Bill Wendlingd509a662013-01-29 00:34:06 +0000453}
454
Bill Wendling3f12ac22013-02-05 22:37:24 +0000455bool AttributeImpl::hasAttribute(StringRef Kind) const {
456 if (!isStringAttribute()) return false;
457 return getKindAsString() == Kind;
Bill Wendlingd509a662013-01-29 00:34:06 +0000458}
459
Bill Wendling3f12ac22013-02-05 22:37:24 +0000460Attribute::AttrKind AttributeImpl::getKindAsEnum() const {
Hal Finkele15442c2014-07-18 06:51:55 +0000461 assert(isEnumAttribute() || isIntAttribute());
Benjamin Kramer741146b2013-07-11 12:13:16 +0000462 return static_cast<const EnumAttributeImpl *>(this)->getEnumKind();
Bill Wendlingd509a662013-01-29 00:34:06 +0000463}
464
Bill Wendling3f12ac22013-02-05 22:37:24 +0000465uint64_t AttributeImpl::getValueAsInt() const {
Hal Finkele15442c2014-07-18 06:51:55 +0000466 assert(isIntAttribute());
467 return static_cast<const IntAttributeImpl *>(this)->getValue();
Bill Wendlingd509a662013-01-29 00:34:06 +0000468}
469
Bill Wendling3f12ac22013-02-05 22:37:24 +0000470StringRef AttributeImpl::getKindAsString() const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000471 assert(isStringAttribute());
472 return static_cast<const StringAttributeImpl *>(this)->getStringKind();
Bill Wendlingd509a662013-01-29 00:34:06 +0000473}
474
Bill Wendling3f12ac22013-02-05 22:37:24 +0000475StringRef AttributeImpl::getValueAsString() const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000476 assert(isStringAttribute());
477 return static_cast<const StringAttributeImpl *>(this)->getStringValue();
Bill Wendlingd509a662013-01-29 00:34:06 +0000478}
479
480bool AttributeImpl::operator<(const AttributeImpl &AI) const {
Bill Wendlingc79cdff2013-02-01 01:04:27 +0000481 // This sorts the attributes with Attribute::AttrKinds coming first (sorted
482 // relative to their enum value) and then strings.
Bill Wendling26b95752013-02-15 05:25:26 +0000483 if (isEnumAttribute()) {
484 if (AI.isEnumAttribute()) return getKindAsEnum() < AI.getKindAsEnum();
Hal Finkele15442c2014-07-18 06:51:55 +0000485 if (AI.isIntAttribute()) return true;
Bill Wendling26b95752013-02-15 05:25:26 +0000486 if (AI.isStringAttribute()) return true;
487 }
Bill Wendlingc79cdff2013-02-01 01:04:27 +0000488
Hal Finkele15442c2014-07-18 06:51:55 +0000489 if (isIntAttribute()) {
Bill Wendling26b95752013-02-15 05:25:26 +0000490 if (AI.isEnumAttribute()) return false;
Reid Kleckner7de67612016-04-04 23:06:05 +0000491 if (AI.isIntAttribute()) {
492 if (getKindAsEnum() == AI.getKindAsEnum())
493 return getValueAsInt() < AI.getValueAsInt();
494 return getKindAsEnum() < AI.getKindAsEnum();
495 }
Bill Wendling26b95752013-02-15 05:25:26 +0000496 if (AI.isStringAttribute()) return true;
Bill Wendling3f12ac22013-02-05 22:37:24 +0000497 }
Bill Wendlingd509a662013-01-29 00:34:06 +0000498
Bill Wendling26b95752013-02-15 05:25:26 +0000499 if (AI.isEnumAttribute()) return false;
Hal Finkele15442c2014-07-18 06:51:55 +0000500 if (AI.isIntAttribute()) return false;
Bill Wendling26b95752013-02-15 05:25:26 +0000501 if (getKindAsString() == AI.getKindAsString())
502 return getValueAsString() < AI.getValueAsString();
503 return getKindAsString() < AI.getKindAsString();
Bill Wendlingd509a662013-01-29 00:34:06 +0000504}
505
Bill Wendlingd509a662013-01-29 00:34:06 +0000506//===----------------------------------------------------------------------===//
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000507// AttributeSet Definition
508//===----------------------------------------------------------------------===//
509
510AttributeSet AttributeSet::get(LLVMContext &C, const AttrBuilder &B) {
511 return AttributeSet(AttributeSetNode::get(C, B));
512}
513
514AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<Attribute> Attrs) {
515 return AttributeSet(AttributeSetNode::get(C, Attrs));
516}
517
Javed Absarf3d79042017-05-11 12:28:08 +0000518AttributeSet AttributeSet::addAttribute(LLVMContext &C,
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000519 Attribute::AttrKind Kind) const {
Javed Absarf3d79042017-05-11 12:28:08 +0000520 if (hasAttribute(Kind)) return *this;
521 AttrBuilder B;
522 B.addAttribute(Kind);
523 return addAttributes(C, AttributeSet::get(C, B));
524}
525
526AttributeSet AttributeSet::addAttribute(LLVMContext &C, StringRef Kind,
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000527 StringRef Value) const {
Javed Absarf3d79042017-05-11 12:28:08 +0000528 AttrBuilder B;
529 B.addAttribute(Kind, Value);
530 return addAttributes(C, AttributeSet::get(C, B));
531}
532
533AttributeSet AttributeSet::addAttributes(LLVMContext &C,
534 const AttributeSet AS) const {
535 if (!hasAttributes())
536 return AS;
537
538 if (!AS.hasAttributes())
539 return *this;
540
541 AttrBuilder B(AS);
Eugene Zelenko7fb5d412018-03-30 00:47:31 +0000542 for (const auto I : *this)
Javed Absarf3d79042017-05-11 12:28:08 +0000543 B.addAttribute(I);
544
545 return get(C, B);
546}
547
548AttributeSet AttributeSet::removeAttribute(LLVMContext &C,
549 Attribute::AttrKind Kind) const {
550 if (!hasAttribute(Kind)) return *this;
Daniel Neilson88dddb82018-01-17 19:15:21 +0000551 AttrBuilder B(*this);
552 B.removeAttribute(Kind);
553 return get(C, B);
Javed Absarf3d79042017-05-11 12:28:08 +0000554}
555
556AttributeSet AttributeSet::removeAttribute(LLVMContext &C,
557 StringRef Kind) const {
558 if (!hasAttribute(Kind)) return *this;
Daniel Neilson88dddb82018-01-17 19:15:21 +0000559 AttrBuilder B(*this);
560 B.removeAttribute(Kind);
561 return get(C, B);
Javed Absarf3d79042017-05-11 12:28:08 +0000562}
563
564AttributeSet AttributeSet::removeAttributes(LLVMContext &C,
565 const AttrBuilder &Attrs) const {
Javed Absarf3d79042017-05-11 12:28:08 +0000566 AttrBuilder B(*this);
567 B.remove(Attrs);
568 return get(C, B);
569}
570
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000571unsigned AttributeSet::getNumAttributes() const {
572 return SetNode ? SetNode->getNumAttributes() : 0;
573}
574
575bool AttributeSet::hasAttribute(Attribute::AttrKind Kind) const {
Eugene Zelenkod761e2c2017-05-15 21:57:41 +0000576 return SetNode ? SetNode->hasAttribute(Kind) : false;
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000577}
578
579bool AttributeSet::hasAttribute(StringRef Kind) const {
Eugene Zelenkod761e2c2017-05-15 21:57:41 +0000580 return SetNode ? SetNode->hasAttribute(Kind) : false;
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000581}
582
583Attribute AttributeSet::getAttribute(Attribute::AttrKind Kind) const {
584 return SetNode ? SetNode->getAttribute(Kind) : Attribute();
585}
586
587Attribute AttributeSet::getAttribute(StringRef Kind) const {
588 return SetNode ? SetNode->getAttribute(Kind) : Attribute();
589}
590
591unsigned AttributeSet::getAlignment() const {
592 return SetNode ? SetNode->getAlignment() : 0;
593}
594
595unsigned AttributeSet::getStackAlignment() const {
596 return SetNode ? SetNode->getStackAlignment() : 0;
597}
598
599uint64_t AttributeSet::getDereferenceableBytes() const {
600 return SetNode ? SetNode->getDereferenceableBytes() : 0;
601}
602
603uint64_t AttributeSet::getDereferenceableOrNullBytes() const {
604 return SetNode ? SetNode->getDereferenceableOrNullBytes() : 0;
605}
606
607std::pair<unsigned, Optional<unsigned>> AttributeSet::getAllocSizeArgs() const {
Konstantin Zhuravlyov6df95b72017-04-12 23:57:37 +0000608 return SetNode ? SetNode->getAllocSizeArgs()
609 : std::pair<unsigned, Optional<unsigned>>(0, 0);
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000610}
611
612std::string AttributeSet::getAsString(bool InAttrGrp) const {
613 return SetNode ? SetNode->getAsString(InAttrGrp) : "";
614}
615
616AttributeSet::iterator AttributeSet::begin() const {
617 return SetNode ? SetNode->begin() : nullptr;
618}
619
620AttributeSet::iterator AttributeSet::end() const {
621 return SetNode ? SetNode->end() : nullptr;
622}
623
Aaron Ballman615eb472017-10-15 14:32:27 +0000624#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Javed Absarf3d79042017-05-11 12:28:08 +0000625LLVM_DUMP_METHOD void AttributeSet::dump() const {
626 dbgs() << "AS =\n";
627 dbgs() << " { ";
628 dbgs() << getAsString(true) << " }\n";
629}
630#endif
631
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000632//===----------------------------------------------------------------------===//
Bill Wendlingd509a662013-01-29 00:34:06 +0000633// AttributeSetNode Definition
634//===----------------------------------------------------------------------===//
635
Reid Kleckner8ff77852017-04-10 23:46:08 +0000636AttributeSetNode::AttributeSetNode(ArrayRef<Attribute> Attrs)
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000637 : AvailableAttrs(0), NumAttrs(Attrs.size()) {
Reid Kleckner8ff77852017-04-10 23:46:08 +0000638 // There's memory after the node where we can store the entries in.
639 std::copy(Attrs.begin(), Attrs.end(), getTrailingObjects<Attribute>());
640
Eugene Zelenko7fb5d412018-03-30 00:47:31 +0000641 for (const auto I : *this) {
Reid Kleckner8ff77852017-04-10 23:46:08 +0000642 if (!I.isStringAttribute()) {
643 AvailableAttrs |= ((uint64_t)1) << I.getKindAsEnum();
644 }
645 }
646}
647
Bill Wendlingd509a662013-01-29 00:34:06 +0000648AttributeSetNode *AttributeSetNode::get(LLVMContext &C,
649 ArrayRef<Attribute> Attrs) {
650 if (Attrs.empty())
Craig Topperc6207612014-04-09 06:08:46 +0000651 return nullptr;
Bill Wendlingd509a662013-01-29 00:34:06 +0000652
653 // Otherwise, build a key to look up the existing attributes.
654 LLVMContextImpl *pImpl = C.pImpl;
655 FoldingSetNodeID ID;
656
657 SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end());
Reid Kleckner7de67612016-04-04 23:06:05 +0000658 std::sort(SortedAttrs.begin(), SortedAttrs.end());
Bill Wendlingd509a662013-01-29 00:34:06 +0000659
Eugene Zelenko7fb5d412018-03-30 00:47:31 +0000660 for (const auto Attr : SortedAttrs)
George Burgess IV500d3032015-12-16 05:21:02 +0000661 Attr.Profile(ID);
Bill Wendlingd509a662013-01-29 00:34:06 +0000662
663 void *InsertPoint;
664 AttributeSetNode *PA =
665 pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint);
666
667 // If we didn't find any existing attributes of the same shape then create a
668 // new one and insert it.
669 if (!PA) {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000670 // Coallocate entries after the AttributeSetNode itself.
James Y Knightaa365b22015-08-05 22:57:34 +0000671 void *Mem = ::operator new(totalSizeToAlloc<Attribute>(SortedAttrs.size()));
Benjamin Kramer741146b2013-07-11 12:13:16 +0000672 PA = new (Mem) AttributeSetNode(SortedAttrs);
Bill Wendlingd509a662013-01-29 00:34:06 +0000673 pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint);
674 }
675
Reid Klecknerb5180542017-03-21 16:57:19 +0000676 // Return the AttributeSetNode that we found or created.
Bill Wendlingd509a662013-01-29 00:34:06 +0000677 return PA;
678}
679
Reid Klecknereb9dd5b2017-04-10 23:31:05 +0000680AttributeSetNode *AttributeSetNode::get(LLVMContext &C, const AttrBuilder &B) {
681 // Add target-independent attributes.
682 SmallVector<Attribute, 8> Attrs;
683 for (Attribute::AttrKind Kind = Attribute::None;
684 Kind != Attribute::EndAttrKinds; Kind = Attribute::AttrKind(Kind + 1)) {
685 if (!B.contains(Kind))
686 continue;
687
688 Attribute Attr;
689 switch (Kind) {
690 case Attribute::Alignment:
691 Attr = Attribute::getWithAlignment(C, B.getAlignment());
692 break;
693 case Attribute::StackAlignment:
694 Attr = Attribute::getWithStackAlignment(C, B.getStackAlignment());
695 break;
696 case Attribute::Dereferenceable:
697 Attr = Attribute::getWithDereferenceableBytes(
698 C, B.getDereferenceableBytes());
699 break;
700 case Attribute::DereferenceableOrNull:
701 Attr = Attribute::getWithDereferenceableOrNullBytes(
702 C, B.getDereferenceableOrNullBytes());
703 break;
704 case Attribute::AllocSize: {
705 auto A = B.getAllocSizeArgs();
706 Attr = Attribute::getWithAllocSizeArgs(C, A.first, A.second);
707 break;
708 }
709 default:
710 Attr = Attribute::get(C, Kind);
711 }
712 Attrs.push_back(Attr);
713 }
714
715 // Add target-dependent (string) attributes.
716 for (const auto &TDA : B.td_attrs())
717 Attrs.emplace_back(Attribute::get(C, TDA.first, TDA.second));
718
719 return get(C, Attrs);
720}
721
Bill Wendlingbce7b972013-02-13 08:42:21 +0000722bool AttributeSetNode::hasAttribute(StringRef Kind) const {
Eugene Zelenko7fb5d412018-03-30 00:47:31 +0000723 for (const auto I : *this)
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000724 if (I.hasAttribute(Kind))
Bill Wendlingbce7b972013-02-13 08:42:21 +0000725 return true;
726 return false;
727}
728
729Attribute AttributeSetNode::getAttribute(Attribute::AttrKind Kind) const {
Matthias Braun31eeb76f2016-01-29 22:25:13 +0000730 if (hasAttribute(Kind)) {
Eugene Zelenko7fb5d412018-03-30 00:47:31 +0000731 for (const auto I : *this)
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000732 if (I.hasAttribute(Kind))
733 return I;
Matthias Braun31eeb76f2016-01-29 22:25:13 +0000734 }
Eugene Zelenko7fb5d412018-03-30 00:47:31 +0000735 return {};
Bill Wendlingbce7b972013-02-13 08:42:21 +0000736}
737
738Attribute AttributeSetNode::getAttribute(StringRef Kind) const {
Eugene Zelenko7fb5d412018-03-30 00:47:31 +0000739 for (const auto I : *this)
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000740 if (I.hasAttribute(Kind))
741 return I;
Eugene Zelenko7fb5d412018-03-30 00:47:31 +0000742 return {};
Bill Wendlingbce7b972013-02-13 08:42:21 +0000743}
744
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000745unsigned AttributeSetNode::getAlignment() const {
Eugene Zelenko7fb5d412018-03-30 00:47:31 +0000746 for (const auto I : *this)
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000747 if (I.hasAttribute(Attribute::Alignment))
748 return I.getAlignment();
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000749 return 0;
750}
751
752unsigned AttributeSetNode::getStackAlignment() const {
Eugene Zelenko7fb5d412018-03-30 00:47:31 +0000753 for (const auto I : *this)
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000754 if (I.hasAttribute(Attribute::StackAlignment))
755 return I.getStackAlignment();
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000756 return 0;
757}
758
Hal Finkelb0407ba2014-07-18 15:51:28 +0000759uint64_t AttributeSetNode::getDereferenceableBytes() const {
Eugene Zelenko7fb5d412018-03-30 00:47:31 +0000760 for (const auto I : *this)
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000761 if (I.hasAttribute(Attribute::Dereferenceable))
762 return I.getDereferenceableBytes();
Hal Finkelb0407ba2014-07-18 15:51:28 +0000763 return 0;
764}
765
Sanjoy Das06cf33f2015-05-06 17:41:54 +0000766uint64_t AttributeSetNode::getDereferenceableOrNullBytes() const {
Eugene Zelenko7fb5d412018-03-30 00:47:31 +0000767 for (const auto I : *this)
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000768 if (I.hasAttribute(Attribute::DereferenceableOrNull))
769 return I.getDereferenceableOrNullBytes();
Sanjoy Das06cf33f2015-05-06 17:41:54 +0000770 return 0;
771}
772
George Burgess IV278199f2016-04-12 01:05:35 +0000773std::pair<unsigned, Optional<unsigned>>
774AttributeSetNode::getAllocSizeArgs() const {
Eugene Zelenko7fb5d412018-03-30 00:47:31 +0000775 for (const auto I : *this)
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000776 if (I.hasAttribute(Attribute::AllocSize))
777 return I.getAllocSizeArgs();
George Burgess IV278199f2016-04-12 01:05:35 +0000778 return std::make_pair(0, 0);
779}
780
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +0000781std::string AttributeSetNode::getAsString(bool InAttrGrp) const {
Benjamin Kramer0baf8f42013-04-19 11:43:21 +0000782 std::string Str;
Benjamin Kramer741146b2013-07-11 12:13:16 +0000783 for (iterator I = begin(), E = end(); I != E; ++I) {
784 if (I != begin())
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +0000785 Str += ' ';
786 Str += I->getAsString(InAttrGrp);
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000787 }
788 return Str;
789}
790
Bill Wendlingd509a662013-01-29 00:34:06 +0000791//===----------------------------------------------------------------------===//
Reid Klecknerb5180542017-03-21 16:57:19 +0000792// AttributeListImpl Definition
Bill Wendlingd509a662013-01-29 00:34:06 +0000793//===----------------------------------------------------------------------===//
794
Reid Kleckner51b2cd82017-10-11 01:40:38 +0000795/// Map from AttributeList index to the internal array index. Adding one happens
796/// to work, but it relies on unsigned integer wrapping. MSVC warns about
797/// unsigned wrapping in constexpr functions, so write out the conditional. LLVM
798/// folds it to add anyway.
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000799static constexpr unsigned attrIdxToArrayIdx(unsigned Index) {
Reid Kleckner51b2cd82017-10-11 01:40:38 +0000800 return Index == AttributeList::FunctionIndex ? 0 : Index + 1;
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000801}
802
803AttributeListImpl::AttributeListImpl(LLVMContext &C,
804 ArrayRef<AttributeSet> Sets)
805 : AvailableFunctionAttrs(0), Context(C), NumAttrSets(Sets.size()) {
806 assert(!Sets.empty() && "pointless AttributeListImpl");
Reid Klecknera82be602017-04-11 00:16:00 +0000807
808 // There's memory after the node where we can store the entries in.
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000809 std::copy(Sets.begin(), Sets.end(), getTrailingObjects<AttributeSet>());
Reid Klecknera82be602017-04-11 00:16:00 +0000810
811 // Initialize AvailableFunctionAttrs summary bitset.
Reid Klecknerec0fc032017-04-12 22:22:01 +0000812 static_assert(Attribute::EndAttrKinds <=
813 sizeof(AvailableFunctionAttrs) * CHAR_BIT,
814 "Too many attributes");
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000815 static_assert(attrIdxToArrayIdx(AttributeList::FunctionIndex) == 0U,
816 "function should be stored in slot 0");
Eugene Zelenko7fb5d412018-03-30 00:47:31 +0000817 for (const auto I : Sets[0]) {
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000818 if (!I.isStringAttribute())
819 AvailableFunctionAttrs |= 1ULL << I.getKindAsEnum();
Reid Klecknera82be602017-04-11 00:16:00 +0000820 }
821}
822
823void AttributeListImpl::Profile(FoldingSetNodeID &ID) const {
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000824 Profile(ID, makeArrayRef(begin(), end()));
Reid Klecknera82be602017-04-11 00:16:00 +0000825}
826
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000827void AttributeListImpl::Profile(FoldingSetNodeID &ID,
828 ArrayRef<AttributeSet> Sets) {
829 for (const auto &Set : Sets)
830 ID.AddPointer(Set.SetNode);
Reid Klecknera82be602017-04-11 00:16:00 +0000831}
832
Aaron Ballman615eb472017-10-15 14:32:27 +0000833#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Reid Klecknerb5180542017-03-21 16:57:19 +0000834LLVM_DUMP_METHOD void AttributeListImpl::dump() const {
835 AttributeList(const_cast<AttributeListImpl *>(this)).dump();
Peter Collingbourneabca2ec2013-08-02 22:34:30 +0000836}
Matthias Braun8c209aa2017-01-28 02:02:38 +0000837#endif
Peter Collingbourneabca2ec2013-08-02 22:34:30 +0000838
Bill Wendlingd509a662013-01-29 00:34:06 +0000839//===----------------------------------------------------------------------===//
Reid Klecknerb5180542017-03-21 16:57:19 +0000840// AttributeList Construction and Mutation Methods
Bill Wendlingd509a662013-01-29 00:34:06 +0000841//===----------------------------------------------------------------------===//
842
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000843AttributeList AttributeList::getImpl(LLVMContext &C,
844 ArrayRef<AttributeSet> AttrSets) {
845 assert(!AttrSets.empty() && "pointless AttributeListImpl");
Reid Klecknereb9dd5b2017-04-10 23:31:05 +0000846
Bill Wendlingd509a662013-01-29 00:34:06 +0000847 LLVMContextImpl *pImpl = C.pImpl;
848 FoldingSetNodeID ID;
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000849 AttributeListImpl::Profile(ID, AttrSets);
Bill Wendlingd509a662013-01-29 00:34:06 +0000850
851 void *InsertPoint;
Reid Klecknerb5180542017-03-21 16:57:19 +0000852 AttributeListImpl *PA =
853 pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
Bill Wendlingd509a662013-01-29 00:34:06 +0000854
855 // If we didn't find any existing attributes of the same shape then
856 // create a new one and insert it.
857 if (!PA) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000858 // Coallocate entries after the AttributeListImpl itself.
James Y Knightaa365b22015-08-05 22:57:34 +0000859 void *Mem = ::operator new(
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000860 AttributeListImpl::totalSizeToAlloc<AttributeSet>(AttrSets.size()));
861 PA = new (Mem) AttributeListImpl(C, AttrSets);
Bill Wendlingd509a662013-01-29 00:34:06 +0000862 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
863 }
864
865 // Return the AttributesList that we found or created.
Reid Klecknerb5180542017-03-21 16:57:19 +0000866 return AttributeList(PA);
Bill Wendlingd509a662013-01-29 00:34:06 +0000867}
868
Reid Klecknerb5180542017-03-21 16:57:19 +0000869AttributeList
870AttributeList::get(LLVMContext &C,
871 ArrayRef<std::pair<unsigned, Attribute>> Attrs) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000872 // If there are no attributes then return a null AttributesList pointer.
873 if (Attrs.empty())
Eugene Zelenko7fb5d412018-03-30 00:47:31 +0000874 return {};
Bill Wendlingd509a662013-01-29 00:34:06 +0000875
Craig Toppere30b8ca2016-01-03 19:43:40 +0000876 assert(std::is_sorted(Attrs.begin(), Attrs.end(),
877 [](const std::pair<unsigned, Attribute> &LHS,
878 const std::pair<unsigned, Attribute> &RHS) {
879 return LHS.first < RHS.first;
880 }) && "Misordered Attributes list!");
Eugene Zelenko7fb5d412018-03-30 00:47:31 +0000881 assert(llvm::none_of(Attrs,
882 [](const std::pair<unsigned, Attribute> &Pair) {
883 return Pair.second.hasAttribute(Attribute::None);
884 }) &&
David Majnemer0a16c222016-08-11 21:15:00 +0000885 "Pointless attribute!");
Bill Wendlingd509a662013-01-29 00:34:06 +0000886
887 // Create a vector if (unsigned, AttributeSetNode*) pairs from the attributes
888 // list.
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000889 SmallVector<std::pair<unsigned, AttributeSet>, 8> AttrPairVec;
Eugene Zelenkodeaf6952017-02-17 00:00:09 +0000890 for (ArrayRef<std::pair<unsigned, Attribute>>::iterator I = Attrs.begin(),
Bill Wendlingd509a662013-01-29 00:34:06 +0000891 E = Attrs.end(); I != E; ) {
892 unsigned Index = I->first;
893 SmallVector<Attribute, 4> AttrVec;
NAKAMURA Takumif05d2f22013-01-29 15:18:16 +0000894 while (I != E && I->first == Index) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000895 AttrVec.push_back(I->second);
896 ++I;
897 }
898
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000899 AttrPairVec.emplace_back(Index, AttributeSet::get(C, AttrVec));
Bill Wendlingd509a662013-01-29 00:34:06 +0000900 }
901
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000902 return get(C, AttrPairVec);
Bill Wendlingd509a662013-01-29 00:34:06 +0000903}
904
Reid Klecknerb5180542017-03-21 16:57:19 +0000905AttributeList
906AttributeList::get(LLVMContext &C,
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000907 ArrayRef<std::pair<unsigned, AttributeSet>> Attrs) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000908 // If there are no attributes then return a null AttributesList pointer.
909 if (Attrs.empty())
Eugene Zelenko7fb5d412018-03-30 00:47:31 +0000910 return {};
Bill Wendlingd509a662013-01-29 00:34:06 +0000911
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000912 assert(std::is_sorted(Attrs.begin(), Attrs.end(),
913 [](const std::pair<unsigned, AttributeSet> &LHS,
914 const std::pair<unsigned, AttributeSet> &RHS) {
915 return LHS.first < RHS.first;
916 }) &&
917 "Misordered Attributes list!");
Eugene Zelenko7fb5d412018-03-30 00:47:31 +0000918 assert(llvm::none_of(Attrs,
919 [](const std::pair<unsigned, AttributeSet> &Pair) {
920 return !Pair.second.hasAttributes();
921 }) &&
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000922 "Pointless attribute!");
923
924 unsigned MaxIndex = Attrs.back().first;
925
926 SmallVector<AttributeSet, 4> AttrVec(attrIdxToArrayIdx(MaxIndex) + 1);
Eugene Zelenko7fb5d412018-03-30 00:47:31 +0000927 for (const auto Pair : Attrs)
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000928 AttrVec[attrIdxToArrayIdx(Pair.first)] = Pair.second;
929
930 return getImpl(C, AttrVec);
Bill Wendlingd509a662013-01-29 00:34:06 +0000931}
932
Reid Kleckner7f720332017-04-13 00:58:09 +0000933AttributeList AttributeList::get(LLVMContext &C, AttributeSet FnAttrs,
934 AttributeSet RetAttrs,
935 ArrayRef<AttributeSet> ArgAttrs) {
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000936 // Scan from the end to find the last argument with attributes. Most
937 // arguments don't have attributes, so it's nice if we can have fewer unique
938 // AttributeListImpls by dropping empty attribute sets at the end of the list.
939 unsigned NumSets = 0;
940 for (size_t I = ArgAttrs.size(); I != 0; --I) {
941 if (ArgAttrs[I - 1].hasAttributes()) {
942 NumSets = I + 2;
943 break;
944 }
Reid Klecknereb9dd5b2017-04-10 23:31:05 +0000945 }
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000946 if (NumSets == 0) {
947 // Check function and return attributes if we didn't have argument
948 // attributes.
949 if (RetAttrs.hasAttributes())
950 NumSets = 2;
951 else if (FnAttrs.hasAttributes())
952 NumSets = 1;
953 }
954
955 // If all attribute sets were empty, we can use the empty attribute list.
956 if (NumSets == 0)
Eugene Zelenko7fb5d412018-03-30 00:47:31 +0000957 return {};
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000958
959 SmallVector<AttributeSet, 8> AttrSets;
960 AttrSets.reserve(NumSets);
961 // If we have any attributes, we always have function attributes.
962 AttrSets.push_back(FnAttrs);
963 if (NumSets > 1)
964 AttrSets.push_back(RetAttrs);
965 if (NumSets > 2) {
966 // Drop the empty argument attribute sets at the end.
967 ArgAttrs = ArgAttrs.take_front(NumSets - 2);
968 AttrSets.insert(AttrSets.end(), ArgAttrs.begin(), ArgAttrs.end());
969 }
970
971 return getImpl(C, AttrSets);
Reid Klecknereb9dd5b2017-04-10 23:31:05 +0000972}
973
Reid Klecknerb5180542017-03-21 16:57:19 +0000974AttributeList AttributeList::get(LLVMContext &C, unsigned Index,
975 const AttrBuilder &B) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000976 if (!B.hasAttributes())
Eugene Zelenko7fb5d412018-03-30 00:47:31 +0000977 return {};
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000978 Index = attrIdxToArrayIdx(Index);
979 SmallVector<AttributeSet, 8> AttrSets(Index + 1);
980 AttrSets[Index] = AttributeSet::get(C, B);
981 return getImpl(C, AttrSets);
Bill Wendlingd509a662013-01-29 00:34:06 +0000982}
983
Reid Klecknerb5180542017-03-21 16:57:19 +0000984AttributeList AttributeList::get(LLVMContext &C, unsigned Index,
985 ArrayRef<Attribute::AttrKind> Kinds) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000986 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
Eugene Zelenko7fb5d412018-03-30 00:47:31 +0000987 for (const auto K : Kinds)
David Majnemer0a16c222016-08-11 21:15:00 +0000988 Attrs.emplace_back(Index, Attribute::get(C, K));
Bill Wendlingd509a662013-01-29 00:34:06 +0000989 return get(C, Attrs);
990}
991
Reid Klecknerb5180542017-03-21 16:57:19 +0000992AttributeList AttributeList::get(LLVMContext &C, unsigned Index,
993 ArrayRef<StringRef> Kinds) {
Amaury Sechet6100adf2016-06-15 17:50:39 +0000994 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
Eugene Zelenko7fb5d412018-03-30 00:47:31 +0000995 for (const auto K : Kinds)
David Majnemer0a16c222016-08-11 21:15:00 +0000996 Attrs.emplace_back(Index, Attribute::get(C, K));
Amaury Sechet6100adf2016-06-15 17:50:39 +0000997 return get(C, Attrs);
998}
999
Reid Klecknerb5180542017-03-21 16:57:19 +00001000AttributeList AttributeList::get(LLVMContext &C,
1001 ArrayRef<AttributeList> Attrs) {
1002 if (Attrs.empty())
Eugene Zelenko7fb5d412018-03-30 00:47:31 +00001003 return {};
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001004 if (Attrs.size() == 1)
1005 return Attrs[0];
Bill Wendlingd509a662013-01-29 00:34:06 +00001006
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001007 unsigned MaxSize = 0;
Eugene Zelenko7fb5d412018-03-30 00:47:31 +00001008 for (const auto List : Attrs)
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001009 MaxSize = std::max(MaxSize, List.getNumAttrSets());
1010
Reid Kleckner1d7cbdf2017-05-31 14:24:06 +00001011 // If every list was empty, there is no point in merging the lists.
1012 if (MaxSize == 0)
Eugene Zelenko7fb5d412018-03-30 00:47:31 +00001013 return {};
Reid Kleckner1d7cbdf2017-05-31 14:24:06 +00001014
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001015 SmallVector<AttributeSet, 8> NewAttrSets(MaxSize);
1016 for (unsigned I = 0; I < MaxSize; ++I) {
1017 AttrBuilder CurBuilder;
Eugene Zelenko7fb5d412018-03-30 00:47:31 +00001018 for (const auto List : Attrs)
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001019 CurBuilder.merge(List.getAttributes(I - 1));
1020 NewAttrSets[I] = AttributeSet::get(C, CurBuilder);
Bill Wendlingd509a662013-01-29 00:34:06 +00001021 }
1022
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001023 return getImpl(C, NewAttrSets);
Bill Wendlingd509a662013-01-29 00:34:06 +00001024}
1025
Reid Klecknerb5180542017-03-21 16:57:19 +00001026AttributeList AttributeList::addAttribute(LLVMContext &C, unsigned Index,
1027 Attribute::AttrKind Kind) const {
Amaury Sechet392638d2016-06-14 20:27:35 +00001028 if (hasAttribute(Index, Kind)) return *this;
Reid Kleckneree4930b2017-05-02 22:07:37 +00001029 AttrBuilder B;
1030 B.addAttribute(Kind);
1031 return addAttributes(C, Index, B);
Reed Kotler795c7b42013-03-13 20:20:08 +00001032}
1033
Reid Klecknerb5180542017-03-21 16:57:19 +00001034AttributeList AttributeList::addAttribute(LLVMContext &C, unsigned Index,
1035 StringRef Kind,
1036 StringRef Value) const {
Eugene Zelenkodeaf6952017-02-17 00:00:09 +00001037 AttrBuilder B;
Bill Wendling3b2f6102013-07-25 18:34:24 +00001038 B.addAttribute(Kind, Value);
Reid Kleckneree4930b2017-05-02 22:07:37 +00001039 return addAttributes(C, Index, B);
Bill Wendling3b2f6102013-07-25 18:34:24 +00001040}
1041
Reid Kleckner5fbdd172017-05-31 19:23:09 +00001042AttributeList AttributeList::addAttribute(LLVMContext &C, unsigned Index,
Reid Klecknerb5180542017-03-21 16:57:19 +00001043 Attribute A) const {
Reid Kleckner5fbdd172017-05-31 19:23:09 +00001044 AttrBuilder B;
1045 B.addAttribute(A);
1046 return addAttributes(C, Index, B);
Akira Hatanaka237916b2015-12-02 06:58:49 +00001047}
1048
Reid Klecknerb5180542017-03-21 16:57:19 +00001049AttributeList AttributeList::addAttributes(LLVMContext &C, unsigned Index,
Reid Kleckner61906252017-04-19 01:51:13 +00001050 const AttrBuilder &B) const {
1051 if (!B.hasAttributes())
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00001052 return *this;
1053
Reid Klecknerfe64c012017-04-18 22:10:18 +00001054 if (!pImpl)
Reid Kleckner61906252017-04-19 01:51:13 +00001055 return AttributeList::get(C, {{Index, AttributeSet::get(C, B)}});
Reid Klecknerfe64c012017-04-18 22:10:18 +00001056
Bill Wendlingd509a662013-01-29 00:34:06 +00001057#ifndef NDEBUG
1058 // FIXME it is not obvious how this should work for alignment. For now, say
1059 // we can't change a known alignment.
Reid Klecknerbf6b3b152017-05-19 22:23:47 +00001060 unsigned OldAlign = getAttributes(Index).getAlignment();
Reid Kleckner61906252017-04-19 01:51:13 +00001061 unsigned NewAlign = B.getAlignment();
Bill Wendlingd509a662013-01-29 00:34:06 +00001062 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
1063 "Attempt to change alignment!");
1064#endif
1065
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001066 Index = attrIdxToArrayIdx(Index);
1067 SmallVector<AttributeSet, 4> AttrSets(this->begin(), this->end());
1068 if (Index >= AttrSets.size())
1069 AttrSets.resize(Index + 1);
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00001070
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001071 AttrBuilder Merged(AttrSets[Index]);
1072 Merged.merge(B);
1073 AttrSets[Index] = AttributeSet::get(C, Merged);
Bill Wendlingd509a662013-01-29 00:34:06 +00001074
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001075 return getImpl(C, AttrSets);
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00001076}
1077
Reid Kleckner5fbdd172017-05-31 19:23:09 +00001078AttributeList AttributeList::addParamAttribute(LLVMContext &C,
1079 ArrayRef<unsigned> ArgNos,
1080 Attribute A) const {
1081 assert(std::is_sorted(ArgNos.begin(), ArgNos.end()));
1082
1083 SmallVector<AttributeSet, 4> AttrSets(this->begin(), this->end());
1084 unsigned MaxIndex = attrIdxToArrayIdx(ArgNos.back() + FirstArgIndex);
1085 if (MaxIndex >= AttrSets.size())
1086 AttrSets.resize(MaxIndex + 1);
1087
1088 for (unsigned ArgNo : ArgNos) {
1089 unsigned Index = attrIdxToArrayIdx(ArgNo + FirstArgIndex);
1090 AttrBuilder B(AttrSets[Index]);
1091 B.addAttribute(A);
1092 AttrSets[Index] = AttributeSet::get(C, B);
1093 }
1094
1095 return getImpl(C, AttrSets);
1096}
1097
Reid Klecknerb5180542017-03-21 16:57:19 +00001098AttributeList AttributeList::removeAttribute(LLVMContext &C, unsigned Index,
1099 Attribute::AttrKind Kind) const {
Amaury Sechet392638d2016-06-14 20:27:35 +00001100 if (!hasAttribute(Index, Kind)) return *this;
Daniel Neilson88dddb82018-01-17 19:15:21 +00001101
1102 Index = attrIdxToArrayIdx(Index);
1103 SmallVector<AttributeSet, 4> AttrSets(this->begin(), this->end());
1104 assert(Index < AttrSets.size());
1105
1106 AttrSets[Index] = AttrSets[Index].removeAttribute(C, Kind);
1107
1108 return getImpl(C, AttrSets);
Bill Wendlingd509a662013-01-29 00:34:06 +00001109}
1110
Reid Klecknerb5180542017-03-21 16:57:19 +00001111AttributeList AttributeList::removeAttribute(LLVMContext &C, unsigned Index,
1112 StringRef Kind) const {
Amaury Sechet6100adf2016-06-15 17:50:39 +00001113 if (!hasAttribute(Index, Kind)) return *this;
Daniel Neilson88dddb82018-01-17 19:15:21 +00001114
1115 Index = attrIdxToArrayIdx(Index);
1116 SmallVector<AttributeSet, 4> AttrSets(this->begin(), this->end());
1117 assert(Index < AttrSets.size());
1118
1119 AttrSets[Index] = AttrSets[Index].removeAttribute(C, Kind);
1120
1121 return getImpl(C, AttrSets);
Bill Wendlingd509a662013-01-29 00:34:06 +00001122}
1123
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001124AttributeList
1125AttributeList::removeAttributes(LLVMContext &C, unsigned Index,
1126 const AttrBuilder &AttrsToRemove) const {
Reid Klecknerb5180542017-03-21 16:57:19 +00001127 if (!pImpl)
Eugene Zelenko7fb5d412018-03-30 00:47:31 +00001128 return {};
Pete Cooperd2a44612015-05-06 23:19:43 +00001129
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001130 Index = attrIdxToArrayIdx(Index);
1131 SmallVector<AttributeSet, 4> AttrSets(this->begin(), this->end());
1132 if (Index >= AttrSets.size())
1133 AttrSets.resize(Index + 1);
Pete Cooperd2a44612015-05-06 23:19:43 +00001134
Daniel Neilson88dddb82018-01-17 19:15:21 +00001135 AttrSets[Index] = AttrSets[Index].removeAttributes(C, AttrsToRemove);
Pete Cooperd2a44612015-05-06 23:19:43 +00001136
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001137 return getImpl(C, AttrSets);
Pete Cooperd2a44612015-05-06 23:19:43 +00001138}
1139
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00001140AttributeList AttributeList::removeAttributes(LLVMContext &C,
1141 unsigned WithoutIndex) const {
1142 if (!pImpl)
Eugene Zelenko7fb5d412018-03-30 00:47:31 +00001143 return {};
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001144 WithoutIndex = attrIdxToArrayIdx(WithoutIndex);
1145 if (WithoutIndex >= getNumAttrSets())
1146 return *this;
1147 SmallVector<AttributeSet, 4> AttrSets(this->begin(), this->end());
1148 AttrSets[WithoutIndex] = AttributeSet();
1149 return getImpl(C, AttrSets);
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00001150}
1151
Reid Klecknerb5180542017-03-21 16:57:19 +00001152AttributeList AttributeList::addDereferenceableAttr(LLVMContext &C,
1153 unsigned Index,
1154 uint64_t Bytes) const {
Eugene Zelenkodeaf6952017-02-17 00:00:09 +00001155 AttrBuilder B;
Ramkumar Ramachandra8fcb4982015-02-14 19:37:54 +00001156 B.addDereferenceableAttr(Bytes);
Reid Kleckneree4930b2017-05-02 22:07:37 +00001157 return addAttributes(C, Index, B);
Ramkumar Ramachandra8fcb4982015-02-14 19:37:54 +00001158}
1159
Reid Klecknerb5180542017-03-21 16:57:19 +00001160AttributeList
1161AttributeList::addDereferenceableOrNullAttr(LLVMContext &C, unsigned Index,
1162 uint64_t Bytes) const {
Eugene Zelenkodeaf6952017-02-17 00:00:09 +00001163 AttrBuilder B;
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001164 B.addDereferenceableOrNullAttr(Bytes);
Reid Kleckneree4930b2017-05-02 22:07:37 +00001165 return addAttributes(C, Index, B);
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001166}
1167
Reid Klecknerb5180542017-03-21 16:57:19 +00001168AttributeList
1169AttributeList::addAllocSizeAttr(LLVMContext &C, unsigned Index,
1170 unsigned ElemSizeArg,
1171 const Optional<unsigned> &NumElemsArg) {
Eugene Zelenkodeaf6952017-02-17 00:00:09 +00001172 AttrBuilder B;
George Burgess IV278199f2016-04-12 01:05:35 +00001173 B.addAllocSizeAttr(ElemSizeArg, NumElemsArg);
Reid Kleckneree4930b2017-05-02 22:07:37 +00001174 return addAttributes(C, Index, B);
George Burgess IV278199f2016-04-12 01:05:35 +00001175}
1176
Bill Wendlingd509a662013-01-29 00:34:06 +00001177//===----------------------------------------------------------------------===//
Reid Klecknerb5180542017-03-21 16:57:19 +00001178// AttributeList Accessor Methods
Bill Wendlingd509a662013-01-29 00:34:06 +00001179//===----------------------------------------------------------------------===//
1180
Reid Klecknerb5180542017-03-21 16:57:19 +00001181LLVMContext &AttributeList::getContext() const { return pImpl->getContext(); }
1182
Reid Klecknerf021fab2017-04-13 23:12:13 +00001183AttributeSet AttributeList::getParamAttributes(unsigned ArgNo) const {
Reid Klecknera0b45f42017-05-03 18:17:31 +00001184 return getAttributes(ArgNo + FirstArgIndex);
Bill Wendling5d020a32013-02-10 05:00:40 +00001185}
1186
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001187AttributeSet AttributeList::getRetAttributes() const {
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00001188 return getAttributes(ReturnIndex);
Bill Wendlingd509a662013-01-29 00:34:06 +00001189}
1190
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001191AttributeSet AttributeList::getFnAttributes() const {
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00001192 return getAttributes(FunctionIndex);
Bill Wendlingd509a662013-01-29 00:34:06 +00001193}
1194
Reid Klecknerb5180542017-03-21 16:57:19 +00001195bool AttributeList::hasAttribute(unsigned Index,
1196 Attribute::AttrKind Kind) const {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001197 return getAttributes(Index).hasAttribute(Kind);
Bill Wendlingd509a662013-01-29 00:34:06 +00001198}
1199
Reid Klecknerb5180542017-03-21 16:57:19 +00001200bool AttributeList::hasAttribute(unsigned Index, StringRef Kind) const {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001201 return getAttributes(Index).hasAttribute(Kind);
Bill Wendlingbce7b972013-02-13 08:42:21 +00001202}
1203
Reid Klecknerb5180542017-03-21 16:57:19 +00001204bool AttributeList::hasAttributes(unsigned Index) const {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001205 return getAttributes(Index).hasAttributes();
Bill Wendlingd509a662013-01-29 00:34:06 +00001206}
1207
Reid Klecknerb5180542017-03-21 16:57:19 +00001208bool AttributeList::hasFnAttribute(Attribute::AttrKind Kind) const {
Matthias Braun33282812016-01-29 22:25:19 +00001209 return pImpl && pImpl->hasFnAttribute(Kind);
1210}
1211
Reid Klecknerb5180542017-03-21 16:57:19 +00001212bool AttributeList::hasFnAttribute(StringRef Kind) const {
1213 return hasAttribute(AttributeList::FunctionIndex, Kind);
Amaury Sechet5f04d812016-09-09 04:50:38 +00001214}
1215
Reid Klecknerf021fab2017-04-13 23:12:13 +00001216bool AttributeList::hasParamAttribute(unsigned ArgNo,
1217 Attribute::AttrKind Kind) const {
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001218 return hasAttribute(ArgNo + FirstArgIndex, Kind);
Reid Klecknerf021fab2017-04-13 23:12:13 +00001219}
1220
Reid Klecknerb5180542017-03-21 16:57:19 +00001221bool AttributeList::hasAttrSomewhere(Attribute::AttrKind Attr,
1222 unsigned *Index) const {
Craig Topperc6207612014-04-09 06:08:46 +00001223 if (!pImpl) return false;
Bill Wendlingd509a662013-01-29 00:34:06 +00001224
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001225 for (unsigned I = index_begin(), E = index_end(); I != E; ++I) {
1226 if (hasAttribute(I, Attr)) {
1227 if (Index)
1228 *Index = I;
1229 return true;
1230 }
1231 }
Bill Wendlingd509a662013-01-29 00:34:06 +00001232
1233 return false;
1234}
1235
Reid Klecknerb5180542017-03-21 16:57:19 +00001236Attribute AttributeList::getAttribute(unsigned Index,
1237 Attribute::AttrKind Kind) const {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001238 return getAttributes(Index).getAttribute(Kind);
Bill Wendlingbce7b972013-02-13 08:42:21 +00001239}
1240
Reid Klecknerb5180542017-03-21 16:57:19 +00001241Attribute AttributeList::getAttribute(unsigned Index, StringRef Kind) const {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001242 return getAttributes(Index).getAttribute(Kind);
Bill Wendlingbce7b972013-02-13 08:42:21 +00001243}
1244
Reid Kleckner859f8b52017-04-28 20:34:27 +00001245unsigned AttributeList::getRetAlignment() const {
1246 return getAttributes(ReturnIndex).getAlignment();
1247}
1248
1249unsigned AttributeList::getParamAlignment(unsigned ArgNo) const {
Reid Klecknera0b45f42017-05-03 18:17:31 +00001250 return getAttributes(ArgNo + FirstArgIndex).getAlignment();
Bill Wendlingd509a662013-01-29 00:34:06 +00001251}
1252
Reid Klecknerb5180542017-03-21 16:57:19 +00001253unsigned AttributeList::getStackAlignment(unsigned Index) const {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001254 return getAttributes(Index).getStackAlignment();
Bill Wendlingd509a662013-01-29 00:34:06 +00001255}
1256
Reid Klecknerb5180542017-03-21 16:57:19 +00001257uint64_t AttributeList::getDereferenceableBytes(unsigned Index) const {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001258 return getAttributes(Index).getDereferenceableBytes();
Hal Finkelb0407ba2014-07-18 15:51:28 +00001259}
1260
Reid Klecknerb5180542017-03-21 16:57:19 +00001261uint64_t AttributeList::getDereferenceableOrNullBytes(unsigned Index) const {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001262 return getAttributes(Index).getDereferenceableOrNullBytes();
Sanjoy Das06cf33f2015-05-06 17:41:54 +00001263}
1264
George Burgess IV278199f2016-04-12 01:05:35 +00001265std::pair<unsigned, Optional<unsigned>>
Reid Klecknerb5180542017-03-21 16:57:19 +00001266AttributeList::getAllocSizeArgs(unsigned Index) const {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001267 return getAttributes(Index).getAllocSizeArgs();
George Burgess IV278199f2016-04-12 01:05:35 +00001268}
1269
Reid Klecknerb5180542017-03-21 16:57:19 +00001270std::string AttributeList::getAsString(unsigned Index, bool InAttrGrp) const {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001271 return getAttributes(Index).getAsString(InAttrGrp);
Bill Wendlingd509a662013-01-29 00:34:06 +00001272}
1273
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001274AttributeSet AttributeList::getAttributes(unsigned Index) const {
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001275 Index = attrIdxToArrayIdx(Index);
1276 if (!pImpl || Index >= getNumAttrSets())
Eugene Zelenko7fb5d412018-03-30 00:47:31 +00001277 return {};
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001278 return pImpl->begin()[Index];
Bill Wendlingd509a662013-01-29 00:34:06 +00001279}
1280
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001281AttributeList::iterator AttributeList::begin() const {
1282 return pImpl ? pImpl->begin() : nullptr;
Bill Wendlinga9174862013-01-31 23:53:05 +00001283}
1284
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001285AttributeList::iterator AttributeList::end() const {
1286 return pImpl ? pImpl->end() : nullptr;
Bill Wendlinga9174862013-01-31 23:53:05 +00001287}
1288
Bill Wendlingd509a662013-01-29 00:34:06 +00001289//===----------------------------------------------------------------------===//
Reid Klecknerb5180542017-03-21 16:57:19 +00001290// AttributeList Introspection Methods
Bill Wendlingd509a662013-01-29 00:34:06 +00001291//===----------------------------------------------------------------------===//
1292
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001293unsigned AttributeList::getNumAttrSets() const {
1294 return pImpl ? pImpl->NumAttrSets : 0;
Bill Wendlingd509a662013-01-29 00:34:06 +00001295}
1296
Aaron Ballman615eb472017-10-15 14:32:27 +00001297#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Reid Klecknerb5180542017-03-21 16:57:19 +00001298LLVM_DUMP_METHOD void AttributeList::dump() const {
Bill Wendlingd509a662013-01-29 00:34:06 +00001299 dbgs() << "PAL[\n";
1300
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001301 for (unsigned i = index_begin(), e = index_end(); i != e; ++i) {
1302 if (getAttributes(i).hasAttributes())
1303 dbgs() << " { " << i << " => " << getAsString(i) << " }\n";
Bill Wendlingd509a662013-01-29 00:34:06 +00001304 }
1305
1306 dbgs() << "]\n";
1307}
Matthias Braun8c209aa2017-01-28 02:02:38 +00001308#endif
Bill Wendlingd509a662013-01-29 00:34:06 +00001309
Bill Wendlingabf3feb2012-10-05 06:44:41 +00001310//===----------------------------------------------------------------------===//
Bill Wendlingcd330342013-01-04 23:27:34 +00001311// AttrBuilder Method Implementations
Bill Wendlingabf3feb2012-10-05 06:44:41 +00001312//===----------------------------------------------------------------------===//
1313
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001314// FIXME: Remove this ctor, use AttributeSet.
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001315AttrBuilder::AttrBuilder(AttributeList AL, unsigned Index) {
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001316 AttributeSet AS = AL.getAttributes(Index);
Eugene Zelenko7fb5d412018-03-30 00:47:31 +00001317 for (const auto &A : AS)
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001318 addAttribute(A);
Bill Wendling096f5442013-01-07 08:24:35 +00001319}
1320
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001321AttrBuilder::AttrBuilder(AttributeSet AS) {
Eugene Zelenko7fb5d412018-03-30 00:47:31 +00001322 for (const auto &A : AS)
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001323 addAttribute(A);
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00001324}
1325
Bill Wendlingcd330342013-01-04 23:27:34 +00001326void AttrBuilder::clear() {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001327 Attrs.reset();
Sanjoy Das88d0fde2015-09-03 22:27:42 +00001328 TargetDepAttrs.clear();
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001329 Alignment = StackAlignment = DerefBytes = DerefOrNullBytes = 0;
George Burgess IV278199f2016-04-12 01:05:35 +00001330 AllocSizeArgs = 0;
Bill Wendlingcd330342013-01-04 23:27:34 +00001331}
1332
1333AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001334 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
Bill Wendling1c7cc8a2013-01-31 23:16:25 +00001335 assert(Val != Attribute::Alignment && Val != Attribute::StackAlignment &&
George Burgess IV278199f2016-04-12 01:05:35 +00001336 Val != Attribute::Dereferenceable && Val != Attribute::AllocSize &&
Hal Finkelb0407ba2014-07-18 15:51:28 +00001337 "Adding integer attribute without adding a value!");
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001338 Attrs[Val] = true;
Bill Wendling7c04e042012-10-09 19:01:18 +00001339 return *this;
Bill Wendlingabf3feb2012-10-05 06:44:41 +00001340}
1341
Bill Wendling23804da2013-01-31 23:38:01 +00001342AttrBuilder &AttrBuilder::addAttribute(Attribute Attr) {
Bill Wendling0a437302013-02-10 10:13:23 +00001343 if (Attr.isStringAttribute()) {
1344 addAttribute(Attr.getKindAsString(), Attr.getValueAsString());
1345 return *this;
1346 }
1347
Bill Wendling3f12ac22013-02-05 22:37:24 +00001348 Attribute::AttrKind Kind = Attr.getKindAsEnum();
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001349 Attrs[Kind] = true;
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001350
Bill Wendling3f12ac22013-02-05 22:37:24 +00001351 if (Kind == Attribute::Alignment)
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001352 Alignment = Attr.getAlignment();
Bill Wendling3f12ac22013-02-05 22:37:24 +00001353 else if (Kind == Attribute::StackAlignment)
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001354 StackAlignment = Attr.getStackAlignment();
Hal Finkelb0407ba2014-07-18 15:51:28 +00001355 else if (Kind == Attribute::Dereferenceable)
1356 DerefBytes = Attr.getDereferenceableBytes();
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001357 else if (Kind == Attribute::DereferenceableOrNull)
1358 DerefOrNullBytes = Attr.getDereferenceableOrNullBytes();
George Burgess IV278199f2016-04-12 01:05:35 +00001359 else if (Kind == Attribute::AllocSize)
1360 AllocSizeArgs = Attr.getValueAsInt();
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001361 return *this;
1362}
1363
Bill Wendlingb9c5b1a2013-02-05 08:09:32 +00001364AttrBuilder &AttrBuilder::addAttribute(StringRef A, StringRef V) {
1365 TargetDepAttrs[A] = V;
1366 return *this;
1367}
1368
Bill Wendling23804da2013-01-31 23:38:01 +00001369AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001370 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
1371 Attrs[Val] = false;
Bill Wendling23804da2013-01-31 23:38:01 +00001372
1373 if (Val == Attribute::Alignment)
1374 Alignment = 0;
1375 else if (Val == Attribute::StackAlignment)
1376 StackAlignment = 0;
Hal Finkelb0407ba2014-07-18 15:51:28 +00001377 else if (Val == Attribute::Dereferenceable)
1378 DerefBytes = 0;
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001379 else if (Val == Attribute::DereferenceableOrNull)
1380 DerefOrNullBytes = 0;
George Burgess IV278199f2016-04-12 01:05:35 +00001381 else if (Val == Attribute::AllocSize)
1382 AllocSizeArgs = 0;
Bill Wendling23804da2013-01-31 23:38:01 +00001383
1384 return *this;
1385}
1386
Reid Klecknerb5180542017-03-21 16:57:19 +00001387AttrBuilder &AttrBuilder::removeAttributes(AttributeList A, uint64_t Index) {
Reid Kleckner6652a522017-04-28 18:37:16 +00001388 remove(A.getAttributes(Index));
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001389 return *this;
1390}
1391
Bill Wendlingb9c5b1a2013-02-05 08:09:32 +00001392AttrBuilder &AttrBuilder::removeAttribute(StringRef A) {
Eugene Zelenko7fb5d412018-03-30 00:47:31 +00001393 auto I = TargetDepAttrs.find(A);
Bill Wendlingb9c5b1a2013-02-05 08:09:32 +00001394 if (I != TargetDepAttrs.end())
1395 TargetDepAttrs.erase(I);
1396 return *this;
1397}
1398
George Burgess IV278199f2016-04-12 01:05:35 +00001399std::pair<unsigned, Optional<unsigned>> AttrBuilder::getAllocSizeArgs() const {
1400 return unpackAllocSizeArgs(AllocSizeArgs);
1401}
1402
Bill Wendling50d27842012-10-15 20:35:56 +00001403AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
Bill Wendlingabd5ba22012-10-14 03:58:29 +00001404 if (Align == 0) return *this;
Bill Wendlingcd330342013-01-04 23:27:34 +00001405
Bill Wendlingabf3feb2012-10-05 06:44:41 +00001406 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1407 assert(Align <= 0x40000000 && "Alignment too large.");
Bill Wendlingcd330342013-01-04 23:27:34 +00001408
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001409 Attrs[Attribute::Alignment] = true;
Bill Wendlingcd330342013-01-04 23:27:34 +00001410 Alignment = Align;
Bill Wendlingabd5ba22012-10-14 03:58:29 +00001411 return *this;
Chris Lattner3e13b8c2008-01-02 23:42:30 +00001412}
1413
Bill Wendlingcd330342013-01-04 23:27:34 +00001414AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
1415 // Default alignment, allow the target to define how to align it.
1416 if (Align == 0) return *this;
1417
1418 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1419 assert(Align <= 0x100 && "Alignment too large.");
1420
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001421 Attrs[Attribute::StackAlignment] = true;
Bill Wendlingcd330342013-01-04 23:27:34 +00001422 StackAlignment = Align;
1423 return *this;
1424}
1425
Hal Finkelb0407ba2014-07-18 15:51:28 +00001426AttrBuilder &AttrBuilder::addDereferenceableAttr(uint64_t Bytes) {
1427 if (Bytes == 0) return *this;
1428
1429 Attrs[Attribute::Dereferenceable] = true;
1430 DerefBytes = Bytes;
1431 return *this;
1432}
1433
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001434AttrBuilder &AttrBuilder::addDereferenceableOrNullAttr(uint64_t Bytes) {
1435 if (Bytes == 0)
1436 return *this;
1437
1438 Attrs[Attribute::DereferenceableOrNull] = true;
1439 DerefOrNullBytes = Bytes;
1440 return *this;
1441}
1442
George Burgess IV278199f2016-04-12 01:05:35 +00001443AttrBuilder &AttrBuilder::addAllocSizeAttr(unsigned ElemSize,
1444 const Optional<unsigned> &NumElems) {
1445 return addAllocSizeAttrFromRawRepr(packAllocSizeArgs(ElemSize, NumElems));
1446}
1447
1448AttrBuilder &AttrBuilder::addAllocSizeAttrFromRawRepr(uint64_t RawArgs) {
1449 // (0, 0) is our "not present" value, so we need to check for it here.
1450 assert(RawArgs && "Invalid allocsize arguments -- given allocsize(0, 0)");
1451
1452 Attrs[Attribute::AllocSize] = true;
1453 // Reuse existing machinery to store this as a single 64-bit integer so we can
1454 // save a few bytes over using a pair<unsigned, Optional<unsigned>>.
1455 AllocSizeArgs = RawArgs;
1456 return *this;
1457}
1458
Bill Wendlinge2614922013-02-06 01:16:00 +00001459AttrBuilder &AttrBuilder::merge(const AttrBuilder &B) {
1460 // FIXME: What if both have alignments, but they don't match?!
1461 if (!Alignment)
1462 Alignment = B.Alignment;
1463
1464 if (!StackAlignment)
1465 StackAlignment = B.StackAlignment;
1466
Hal Finkelb0407ba2014-07-18 15:51:28 +00001467 if (!DerefBytes)
1468 DerefBytes = B.DerefBytes;
1469
Pete Cooperd2a44612015-05-06 23:19:43 +00001470 if (!DerefOrNullBytes)
1471 DerefOrNullBytes = B.DerefOrNullBytes;
1472
George Burgess IV278199f2016-04-12 01:05:35 +00001473 if (!AllocSizeArgs)
1474 AllocSizeArgs = B.AllocSizeArgs;
1475
Benjamin Kramer45e7d532013-02-16 19:13:18 +00001476 Attrs |= B.Attrs;
Bill Wendlinge2614922013-02-06 01:16:00 +00001477
Pete Cooperd2a44612015-05-06 23:19:43 +00001478 for (auto I : B.td_attrs())
1479 TargetDepAttrs[I.first] = I.second;
Bill Wendlinge2614922013-02-06 01:16:00 +00001480
1481 return *this;
1482}
1483
Pete Cooperd2a44612015-05-06 23:19:43 +00001484AttrBuilder &AttrBuilder::remove(const AttrBuilder &B) {
1485 // FIXME: What if both have alignments, but they don't match?!
1486 if (B.Alignment)
1487 Alignment = 0;
1488
1489 if (B.StackAlignment)
1490 StackAlignment = 0;
1491
1492 if (B.DerefBytes)
1493 DerefBytes = 0;
1494
1495 if (B.DerefOrNullBytes)
1496 DerefOrNullBytes = 0;
1497
George Burgess IV278199f2016-04-12 01:05:35 +00001498 if (B.AllocSizeArgs)
1499 AllocSizeArgs = 0;
1500
Pete Cooperd2a44612015-05-06 23:19:43 +00001501 Attrs &= ~B.Attrs;
1502
1503 for (auto I : B.td_attrs())
1504 TargetDepAttrs.erase(I.first);
1505
1506 return *this;
1507}
1508
1509bool AttrBuilder::overlaps(const AttrBuilder &B) const {
1510 // First check if any of the target independent attributes overlap.
1511 if ((Attrs & B.Attrs).any())
1512 return true;
1513
1514 // Then check if any target dependent ones do.
Sean Silva9011aca2017-02-22 06:34:04 +00001515 for (const auto &I : td_attrs())
Pete Cooperd2a44612015-05-06 23:19:43 +00001516 if (B.contains(I.first))
1517 return true;
1518
1519 return false;
1520}
1521
Bill Wendling4b001442013-02-06 01:33:42 +00001522bool AttrBuilder::contains(StringRef A) const {
1523 return TargetDepAttrs.find(A) != TargetDepAttrs.end();
1524}
1525
Bill Wendling50d27842012-10-15 20:35:56 +00001526bool AttrBuilder::hasAttributes() const {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001527 return !Attrs.none() || !TargetDepAttrs.empty();
Bill Wendlingc6daefa2012-10-08 23:27:46 +00001528}
Bill Wendling9ac69f92013-01-04 20:54:35 +00001529
Reid Kleckner6652a522017-04-28 18:37:16 +00001530bool AttrBuilder::hasAttributes(AttributeList AL, uint64_t Index) const {
1531 AttributeSet AS = AL.getAttributes(Index);
Bill Wendling9ca01da2013-02-02 00:42:06 +00001532
Eugene Zelenko7fb5d412018-03-30 00:47:31 +00001533 for (const auto Attr : AS) {
Hal Finkele15442c2014-07-18 06:51:55 +00001534 if (Attr.isEnumAttribute() || Attr.isIntAttribute()) {
Reid Kleckner6652a522017-04-28 18:37:16 +00001535 if (contains(Attr.getKindAsEnum()))
Bill Wendling7cde51d2013-02-12 07:56:49 +00001536 return true;
1537 } else {
1538 assert(Attr.isStringAttribute() && "Invalid attribute kind!");
Reid Kleckner6652a522017-04-28 18:37:16 +00001539 return contains(Attr.getKindAsString());
Bill Wendling7cde51d2013-02-12 07:56:49 +00001540 }
1541 }
Bill Wendling9ca01da2013-02-02 00:42:06 +00001542
1543 return false;
Bill Wendling70f39172012-10-09 00:01:21 +00001544}
Bill Wendling9ac69f92013-01-04 20:54:35 +00001545
Bill Wendling50d27842012-10-15 20:35:56 +00001546bool AttrBuilder::hasAlignmentAttr() const {
Bill Wendlingcd330342013-01-04 23:27:34 +00001547 return Alignment != 0;
Bill Wendlingc6daefa2012-10-08 23:27:46 +00001548}
1549
Bill Wendlingd509a662013-01-29 00:34:06 +00001550bool AttrBuilder::operator==(const AttrBuilder &B) {
Benjamin Kramer45e7d532013-02-16 19:13:18 +00001551 if (Attrs != B.Attrs)
1552 return false;
Bill Wendling4b001442013-02-06 01:33:42 +00001553
1554 for (td_const_iterator I = TargetDepAttrs.begin(),
1555 E = TargetDepAttrs.end(); I != E; ++I)
1556 if (B.TargetDepAttrs.find(I->first) == B.TargetDepAttrs.end())
1557 return false;
1558
Hal Finkelb0407ba2014-07-18 15:51:28 +00001559 return Alignment == B.Alignment && StackAlignment == B.StackAlignment &&
1560 DerefBytes == B.DerefBytes;
Bill Wendlingd509a662013-01-29 00:34:06 +00001561}
1562
Bill Wendling57625a42013-01-25 23:09:36 +00001563//===----------------------------------------------------------------------===//
1564// AttributeFuncs Function Defintions
1565//===----------------------------------------------------------------------===//
1566
Bill Wendlingc79cdff2013-02-01 01:04:27 +00001567/// \brief Which attributes cannot be applied to a type.
Craig Toppere3dcce92015-08-01 22:20:21 +00001568AttrBuilder AttributeFuncs::typeIncompatible(Type *Ty) {
Bill Wendling57625a42013-01-25 23:09:36 +00001569 AttrBuilder Incompatible;
1570
1571 if (!Ty->isIntegerTy())
1572 // Attribute that only apply to integers.
1573 Incompatible.addAttribute(Attribute::SExt)
1574 .addAttribute(Attribute::ZExt);
1575
1576 if (!Ty->isPointerTy())
1577 // Attribute that only apply to pointers.
1578 Incompatible.addAttribute(Attribute::ByVal)
1579 .addAttribute(Attribute::Nest)
1580 .addAttribute(Attribute::NoAlias)
1581 .addAttribute(Attribute::NoCapture)
Nick Lewyckyd52b1522014-05-20 01:23:40 +00001582 .addAttribute(Attribute::NonNull)
Hal Finkelb0407ba2014-07-18 15:51:28 +00001583 .addDereferenceableAttr(1) // the int here is ignored
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001584 .addDereferenceableOrNullAttr(1) // the int here is ignored
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001585 .addAttribute(Attribute::ReadNone)
1586 .addAttribute(Attribute::ReadOnly)
Reid Klecknera534a382013-12-19 02:14:12 +00001587 .addAttribute(Attribute::StructRet)
1588 .addAttribute(Attribute::InAlloca);
Bill Wendling57625a42013-01-25 23:09:36 +00001589
Pete Cooper2777d8872015-05-06 23:19:56 +00001590 return Incompatible;
Bill Wendling57625a42013-01-25 23:09:36 +00001591}
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001592
1593template<typename AttrClass>
1594static bool isEqual(const Function &Caller, const Function &Callee) {
1595 return Caller.getFnAttribute(AttrClass::getKind()) ==
1596 Callee.getFnAttribute(AttrClass::getKind());
1597}
1598
1599/// \brief Compute the logical AND of the attributes of the caller and the
1600/// callee.
1601///
1602/// This function sets the caller's attribute to false if the callee's attribute
1603/// is false.
1604template<typename AttrClass>
1605static void setAND(Function &Caller, const Function &Callee) {
1606 if (AttrClass::isSet(Caller, AttrClass::getKind()) &&
1607 !AttrClass::isSet(Callee, AttrClass::getKind()))
1608 AttrClass::set(Caller, AttrClass::getKind(), false);
1609}
1610
1611/// \brief Compute the logical OR of the attributes of the caller and the
1612/// callee.
1613///
1614/// This function sets the caller's attribute to true if the callee's attribute
1615/// is true.
1616template<typename AttrClass>
1617static void setOR(Function &Caller, const Function &Callee) {
1618 if (!AttrClass::isSet(Caller, AttrClass::getKind()) &&
1619 AttrClass::isSet(Callee, AttrClass::getKind()))
1620 AttrClass::set(Caller, AttrClass::getKind(), true);
1621}
1622
1623/// \brief If the inlined function had a higher stack protection level than the
1624/// calling function, then bump up the caller's stack protection level.
1625static void adjustCallerSSPLevel(Function &Caller, const Function &Callee) {
1626 // If upgrading the SSP attribute, clear out the old SSP Attributes first.
1627 // Having multiple SSP attributes doesn't actually hurt, but it adds useless
1628 // clutter to the IR.
Reid Kleckneree4930b2017-05-02 22:07:37 +00001629 AttrBuilder OldSSPAttr;
1630 OldSSPAttr.addAttribute(Attribute::StackProtect)
1631 .addAttribute(Attribute::StackProtectStrong)
1632 .addAttribute(Attribute::StackProtectReq);
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001633
Evgeniy Stepanovf17120a2016-04-11 22:27:48 +00001634 if (Callee.hasFnAttribute(Attribute::StackProtectReq)) {
Reid Klecknerb5180542017-03-21 16:57:19 +00001635 Caller.removeAttributes(AttributeList::FunctionIndex, OldSSPAttr);
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001636 Caller.addFnAttr(Attribute::StackProtectReq);
1637 } else if (Callee.hasFnAttribute(Attribute::StackProtectStrong) &&
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001638 !Caller.hasFnAttribute(Attribute::StackProtectReq)) {
Reid Klecknerb5180542017-03-21 16:57:19 +00001639 Caller.removeAttributes(AttributeList::FunctionIndex, OldSSPAttr);
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001640 Caller.addFnAttr(Attribute::StackProtectStrong);
1641 } else if (Callee.hasFnAttribute(Attribute::StackProtect) &&
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001642 !Caller.hasFnAttribute(Attribute::StackProtectReq) &&
1643 !Caller.hasFnAttribute(Attribute::StackProtectStrong))
1644 Caller.addFnAttr(Attribute::StackProtect);
1645}
1646
whitequarked54b4a2017-06-21 18:46:50 +00001647/// \brief If the inlined function required stack probes, then ensure that
1648/// the calling function has those too.
1649static void adjustCallerStackProbes(Function &Caller, const Function &Callee) {
whitequark08b20352017-06-22 23:22:36 +00001650 if (!Caller.hasFnAttribute("probe-stack") &&
1651 Callee.hasFnAttribute("probe-stack")) {
1652 Caller.addFnAttr(Callee.getFnAttribute("probe-stack"));
1653 }
1654}
1655
1656/// \brief If the inlined function defines the size of guard region
1657/// on the stack, then ensure that the calling function defines a guard region
1658/// that is no larger.
1659static void
1660adjustCallerStackProbeSize(Function &Caller, const Function &Callee) {
1661 if (Callee.hasFnAttribute("stack-probe-size")) {
1662 uint64_t CalleeStackProbeSize;
1663 Callee.getFnAttribute("stack-probe-size")
1664 .getValueAsString()
1665 .getAsInteger(0, CalleeStackProbeSize);
1666 if (Caller.hasFnAttribute("stack-probe-size")) {
1667 uint64_t CallerStackProbeSize;
1668 Caller.getFnAttribute("stack-probe-size")
1669 .getValueAsString()
1670 .getAsInteger(0, CallerStackProbeSize);
1671 if (CallerStackProbeSize > CalleeStackProbeSize) {
1672 Caller.addFnAttr(Callee.getFnAttribute("stack-probe-size"));
1673 }
1674 } else {
1675 Caller.addFnAttr(Callee.getFnAttribute("stack-probe-size"));
1676 }
1677 }
whitequarked54b4a2017-06-21 18:46:50 +00001678}
1679
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001680#define GET_ATTR_COMPAT_FUNC
1681#include "AttributesCompatFunc.inc"
1682
1683bool AttributeFuncs::areInlineCompatible(const Function &Caller,
1684 const Function &Callee) {
1685 return hasCompatibleFnAttrs(Caller, Callee);
1686}
1687
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001688void AttributeFuncs::mergeAttributesForInlining(Function &Caller,
1689 const Function &Callee) {
1690 mergeFnAttrs(Caller, Callee);
1691}