blob: ff46debb7a9e574681eac51f06272733af1b4bcc [file] [log] [blame]
Eugene Zelenkob1df7872017-02-17 00:00:09 +00001//===- Attributes.cpp - Implement AttributesList --------------------------===//
Chris Lattner50ee9dd2008-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 Wendling87e10df2013-01-28 21:55:20 +000010// \file
Adrian Prantl26b584c2018-05-01 15:54:18 +000011// This file implements the Attribute, AttributeImpl, AttrBuilder,
Reid Kleckner67077702017-03-21 16:57:19 +000012// AttributeListImpl, and AttributeList classes.
Chris Lattner50ee9dd2008-01-02 23:42:30 +000013//
14//===----------------------------------------------------------------------===//
15
Chandler Carruthe3e43d92017-06-06 11:49:48 +000016#include "llvm/IR/Attributes.h"
Bill Wendlingf6670722012-12-20 01:36:59 +000017#include "AttributeImpl.h"
Bill Wendling2c79ecb2012-09-26 21:07:29 +000018#include "LLVMContextImpl.h"
Eugene Zelenkob1df7872017-02-17 00:00:09 +000019#include "llvm/ADT/ArrayRef.h"
20#include "llvm/ADT/FoldingSet.h"
21#include "llvm/ADT/Optional.h"
Benjamin Kramer15c435a2014-04-12 16:15:53 +000022#include "llvm/ADT/STLExtras.h"
Chandler Carruthe3e43d92017-06-06 11:49:48 +000023#include "llvm/ADT/SmallVector.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000024#include "llvm/ADT/StringExtras.h"
Eugene Zelenkob1df7872017-02-17 00:00:09 +000025#include "llvm/ADT/StringRef.h"
26#include "llvm/ADT/Twine.h"
Nico Weber0f38c602018-04-30 14:59:11 +000027#include "llvm/Config/llvm-config.h"
Eugene Zelenkob1df7872017-02-17 00:00:09 +000028#include "llvm/IR/Function.h"
29#include "llvm/IR/LLVMContext.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000030#include "llvm/IR/Type.h"
Eugene Zelenkob1df7872017-02-17 00:00:09 +000031#include "llvm/Support/Compiler.h"
David Greeneef1894e2010-01-05 01:29:58 +000032#include "llvm/Support/Debug.h"
Eugene Zelenkob1df7872017-02-17 00:00:09 +000033#include "llvm/Support/ErrorHandling.h"
34#include "llvm/Support/MathExtras.h"
Benjamin Kramercfa6ec92009-08-23 11:37:21 +000035#include "llvm/Support/raw_ostream.h"
Bill Wendling3467e302013-01-24 00:06:56 +000036#include <algorithm>
Eugene Zelenkob1df7872017-02-17 00:00:09 +000037#include <cassert>
Eugene Zelenko46795222017-05-15 21:57:41 +000038#include <climits>
39#include <cstddef>
Eugene Zelenkob1df7872017-02-17 00:00:09 +000040#include <cstdint>
41#include <limits>
Eugene Zelenkob1df7872017-02-17 00:00:09 +000042#include <string>
43#include <tuple>
44#include <utility>
45
Chris Lattner50ee9dd2008-01-02 23:42:30 +000046using namespace llvm;
47
Chris Lattner58d74912008-03-12 17:45:29 +000048//===----------------------------------------------------------------------===//
Bill Wendling817abdd2013-01-29 00:48:16 +000049// Attribute Construction Methods
Chris Lattner58d74912008-03-12 17:45:29 +000050//===----------------------------------------------------------------------===//
Chris Lattnerfabfde32008-01-03 00:10:22 +000051
George Burgess IV274105b2016-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 IV37725492016-08-25 01:05:08 +000058static const unsigned AllocSizeNumElemsNotPresent = -1;
George Burgess IV274105b2016-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 Arsenault017c14e2014-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 Arsenaultc7ad7ec2014-09-03 23:38:05 +000086 if (Val) ID.AddInteger(Val);
Matt Arsenault017c14e2014-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 Arsenaultc7ad7ec2014-09-03 23:38:05 +000094 if (!Val)
95 PA = new EnumAttributeImpl(Kind);
96 else
97 PA = new IntAttributeImpl(Kind, Val);
Bill Wendling8c74ecf2013-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 Wendling8e635db2012-10-08 21:47:17 +0000110
111 void *InsertPoint;
Bill Wendlingf6670722012-12-20 01:36:59 +0000112 AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
Bill Wendling8e635db2012-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 Kramere22cde02013-07-11 12:13:16 +0000117 PA = new StringAttributeImpl(Kind, Val);
Bill Wendling8e635db2012-10-08 21:47:17 +0000118 pImpl->AttrsSet.InsertNode(PA, InsertPoint);
119 }
120
Bill Wendlingea59f892013-02-05 08:09:32 +0000121 // Return the Attribute that we found or created.
Bill Wendling034b94b2012-12-19 07:18:57 +0000122 return Attribute(PA);
Bill Wendling8e635db2012-10-08 21:47:17 +0000123}
124
Bill Wendlingc08a5ef2013-01-27 22:43:04 +0000125Attribute Attribute::getWithAlignment(LLVMContext &Context, uint64_t Align) {
Bill Wendling169d5272013-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 Wendling8c74ecf2013-02-05 22:37:24 +0000128 return get(Context, Alignment, Align);
Bill Wendlingc08a5ef2013-01-27 22:43:04 +0000129}
130
131Attribute Attribute::getWithStackAlignment(LLVMContext &Context,
132 uint64_t Align) {
Bill Wendling169d5272013-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 Wendling8c74ecf2013-02-05 22:37:24 +0000135 return get(Context, StackAlignment, Align);
Bill Wendlingc08a5ef2013-01-27 22:43:04 +0000136}
137
Hal Finkel11af4b42014-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 Das5ff59072015-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 IV274105b2016-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 Wendling817abdd2013-01-29 00:48:16 +0000158//===----------------------------------------------------------------------===//
159// Attribute Accessor Methods
160//===----------------------------------------------------------------------===//
161
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000162bool Attribute::isEnumAttribute() const {
163 return pImpl && pImpl->isEnumAttribute();
164}
165
Hal Finkeld0261682014-07-18 06:51:55 +0000166bool Attribute::isIntAttribute() const {
167 return pImpl && pImpl->isIntAttribute();
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000168}
169
170bool Attribute::isStringAttribute() const {
171 return pImpl && pImpl->isStringAttribute();
172}
173
174Attribute::AttrKind Attribute::getKindAsEnum() const {
Bill Wendlingf245ae52013-07-25 00:34:29 +0000175 if (!pImpl) return None;
Hal Finkeld0261682014-07-18 06:51:55 +0000176 assert((isEnumAttribute() || isIntAttribute()) &&
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000177 "Invalid attribute type to get the kind as an enum!");
George Burgess IV6a7da772015-12-16 05:21:02 +0000178 return pImpl->getKindAsEnum();
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000179}
180
181uint64_t Attribute::getValueAsInt() const {
Bill Wendlingf245ae52013-07-25 00:34:29 +0000182 if (!pImpl) return 0;
Hal Finkeld0261682014-07-18 06:51:55 +0000183 assert(isIntAttribute() &&
184 "Expected the attribute to be an integer attribute!");
George Burgess IV6a7da772015-12-16 05:21:02 +0000185 return pImpl->getValueAsInt();
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000186}
187
188StringRef Attribute::getKindAsString() const {
Eugene Zelenkoae117792018-03-30 00:47:31 +0000189 if (!pImpl) return {};
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000190 assert(isStringAttribute() &&
191 "Invalid attribute type to get the kind as a string!");
George Burgess IV6a7da772015-12-16 05:21:02 +0000192 return pImpl->getKindAsString();
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000193}
194
195StringRef Attribute::getValueAsString() const {
Eugene Zelenkoae117792018-03-30 00:47:31 +0000196 if (!pImpl) return {};
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000197 assert(isStringAttribute() &&
198 "Invalid attribute type to get the value as a string!");
George Burgess IV6a7da772015-12-16 05:21:02 +0000199 return pImpl->getValueAsString();
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000200}
201
Bill Wendling64754f42013-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 Wendling6dc37812013-01-29 20:45:34 +0000209}
210
Bill Wendling034b94b2012-12-19 07:18:57 +0000211unsigned Attribute::getAlignment() const {
Bill Wendling7beee282013-02-01 01:04:27 +0000212 assert(hasAttribute(Attribute::Alignment) &&
213 "Trying to get alignment from non-alignment attribute!");
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000214 return pImpl->getValueAsInt();
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000215}
216
Bill Wendling034b94b2012-12-19 07:18:57 +0000217unsigned Attribute::getStackAlignment() const {
Bill Wendling7beee282013-02-01 01:04:27 +0000218 assert(hasAttribute(Attribute::StackAlignment) &&
219 "Trying to get alignment from non-alignment attribute!");
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000220 return pImpl->getValueAsInt();
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000221}
222
Hal Finkel11af4b42014-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 Das5ff59072015-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 IV274105b2016-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 Wendlingb29ce262013-02-11 08:43:33 +0000243std::string Attribute::getAsString(bool InAttrGrp) const {
Eugene Zelenkoae117792018-03-30 00:47:31 +0000244 if (!pImpl) return {};
Bill Wendling14292a62013-01-31 20:59:05 +0000245
Kostya Serebryany8eec41f2013-02-26 06:58:09 +0000246 if (hasAttribute(Attribute::SanitizeAddress))
247 return "sanitize_address";
Evgeniy Stepanovd47b5b32017-12-09 00:21:41 +0000248 if (hasAttribute(Attribute::SanitizeHWAddress))
249 return "sanitize_hwaddress";
Bill Wendling14292a62013-01-31 20:59:05 +0000250 if (hasAttribute(Attribute::AlwaysInline))
251 return "alwaysinline";
Igor Laevsky6690dbf2015-07-11 10:30:36 +0000252 if (hasAttribute(Attribute::ArgMemOnly))
253 return "argmemonly";
Michael Gottesman2253a2f2013-06-27 00:25:01 +0000254 if (hasAttribute(Attribute::Builtin))
255 return "builtin";
Bill Wendling14292a62013-01-31 20:59:05 +0000256 if (hasAttribute(Attribute::ByVal))
257 return "byval";
Owen Anderson13146c72015-05-26 23:48:40 +0000258 if (hasAttribute(Attribute::Convergent))
259 return "convergent";
Manman Ren4bda8822016-04-01 21:41:15 +0000260 if (hasAttribute(Attribute::SwiftError))
261 return "swifterror";
Manman Rend9e9e2b2016-03-29 17:37:21 +0000262 if (hasAttribute(Attribute::SwiftSelf))
263 return "swiftself";
Vaivaswatha Nagarajee7970e2015-12-16 16:16:19 +0000264 if (hasAttribute(Attribute::InaccessibleMemOnly))
265 return "inaccessiblememonly";
266 if (hasAttribute(Attribute::InaccessibleMemOrArgMemOnly))
267 return "inaccessiblemem_or_argmemonly";
Reid Kleckner4b70bfc2013-12-19 02:14:12 +0000268 if (hasAttribute(Attribute::InAlloca))
269 return "inalloca";
Bill Wendling14292a62013-01-31 20:59:05 +0000270 if (hasAttribute(Attribute::InlineHint))
271 return "inlinehint";
Bill Wendling034b94b2012-12-19 07:18:57 +0000272 if (hasAttribute(Attribute::InReg))
Bill Wendling606c8e32013-01-29 03:20:31 +0000273 return "inreg";
Tom Roeder5d0f7af2014-06-05 19:29:43 +0000274 if (hasAttribute(Attribute::JumpTable))
275 return "jumptable";
Bill Wendling14292a62013-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 Wendling034b94b2012-12-19 07:18:57 +0000282 if (hasAttribute(Attribute::NoAlias))
Bill Wendling606c8e32013-01-29 03:20:31 +0000283 return "noalias";
Bill Wendling143d4642013-02-22 00:12:35 +0000284 if (hasAttribute(Attribute::NoBuiltin))
285 return "nobuiltin";
Bill Wendling034b94b2012-12-19 07:18:57 +0000286 if (hasAttribute(Attribute::NoCapture))
Bill Wendling606c8e32013-01-29 03:20:31 +0000287 return "nocapture";
Bill Wendling14292a62013-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 Lewyckyfe47ebf2014-05-20 01:23:40 +0000296 if (hasAttribute(Attribute::NonNull))
297 return "nonnull";
Bill Wendling14292a62013-01-31 20:59:05 +0000298 if (hasAttribute(Attribute::NoRedZone))
299 return "noredzone";
300 if (hasAttribute(Attribute::NoReturn))
301 return "noreturn";
Oren Ben Simhon10c992c2018-03-17 13:29:46 +0000302 if (hasAttribute(Attribute::NoCfCheck))
303 return "nocf_check";
James Molloyd0019322015-11-06 10:32:53 +0000304 if (hasAttribute(Attribute::NoRecurse))
305 return "norecurse";
Bill Wendling14292a62013-01-31 20:59:05 +0000306 if (hasAttribute(Attribute::NoUnwind))
307 return "nounwind";
Matt Morehouse7d085b62018-03-22 17:07:51 +0000308 if (hasAttribute(Attribute::OptForFuzzing))
309 return "optforfuzzing";
Andrea Di Biagio5768bb82013-08-23 11:53:55 +0000310 if (hasAttribute(Attribute::OptimizeNone))
311 return "optnone";
Bill Wendling14292a62013-01-31 20:59:05 +0000312 if (hasAttribute(Attribute::OptimizeForSize))
313 return "optsize";
Bill Wendling034b94b2012-12-19 07:18:57 +0000314 if (hasAttribute(Attribute::ReadNone))
Bill Wendling606c8e32013-01-29 03:20:31 +0000315 return "readnone";
Bill Wendling034b94b2012-12-19 07:18:57 +0000316 if (hasAttribute(Attribute::ReadOnly))
Bill Wendling606c8e32013-01-29 03:20:31 +0000317 return "readonly";
Nicolai Haehnleb07f5402016-07-04 08:01:29 +0000318 if (hasAttribute(Attribute::WriteOnly))
319 return "writeonly";
Stephen Lin456ca042013-04-20 05:14:40 +0000320 if (hasAttribute(Attribute::Returned))
321 return "returned";
Bill Wendling14292a62013-01-31 20:59:05 +0000322 if (hasAttribute(Attribute::ReturnsTwice))
323 return "returns_twice";
324 if (hasAttribute(Attribute::SExt))
325 return "signext";
Chandler Carruthd2b1fb12018-09-04 12:38:00 +0000326 if (hasAttribute(Attribute::SpeculativeLoadHardening))
327 return "speculative_load_hardening";
Matt Arsenaultea376da2017-04-28 20:25:27 +0000328 if (hasAttribute(Attribute::Speculatable))
329 return "speculatable";
Bill Wendling034b94b2012-12-19 07:18:57 +0000330 if (hasAttribute(Attribute::StackProtect))
Bill Wendling606c8e32013-01-29 03:20:31 +0000331 return "ssp";
Bill Wendling034b94b2012-12-19 07:18:57 +0000332 if (hasAttribute(Attribute::StackProtectReq))
Bill Wendling606c8e32013-01-29 03:20:31 +0000333 return "sspreq";
Bill Wendling114baee2013-01-23 06:41:41 +0000334 if (hasAttribute(Attribute::StackProtectStrong))
Bill Wendling606c8e32013-01-29 03:20:31 +0000335 return "sspstrong";
Peter Collingbourne7ffec832015-06-15 21:07:11 +0000336 if (hasAttribute(Attribute::SafeStack))
337 return "safestack";
Vlad Tsyrklevich45013b22018-04-03 20:10:40 +0000338 if (hasAttribute(Attribute::ShadowCallStack))
339 return "shadowcallstack";
Andrew Kaylor68d0bd12017-08-14 21:15:13 +0000340 if (hasAttribute(Attribute::StrictFP))
341 return "strictfp";
Bill Wendling14292a62013-01-31 20:59:05 +0000342 if (hasAttribute(Attribute::StructRet))
343 return "sret";
Kostya Serebryany8eec41f2013-02-26 06:58:09 +0000344 if (hasAttribute(Attribute::SanitizeThread))
345 return "sanitize_thread";
346 if (hasAttribute(Attribute::SanitizeMemory))
347 return "sanitize_memory";
Bill Wendling14292a62013-01-31 20:59:05 +0000348 if (hasAttribute(Attribute::UWTable))
349 return "uwtable";
350 if (hasAttribute(Attribute::ZExt))
351 return "zeroext";
Diego Novillo77226a02013-05-24 12:26:52 +0000352 if (hasAttribute(Attribute::Cold))
353 return "cold";
Bill Wendling14292a62013-01-31 20:59:05 +0000354
355 // FIXME: These should be output like this:
356 //
357 // align=4
358 // alignstack=8
359 //
Bill Wendling034b94b2012-12-19 07:18:57 +0000360 if (hasAttribute(Attribute::Alignment)) {
Bill Wendling606c8e32013-01-29 03:20:31 +0000361 std::string Result;
Bill Wendlingb29ce262013-02-11 08:43:33 +0000362 Result += "align";
363 Result += (InAttrGrp) ? "=" : " ";
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000364 Result += utostr(getValueAsInt());
365 return Result;
366 }
Bill Wendlingb29ce262013-02-11 08:43:33 +0000367
Sanjoy Das5ff59072015-04-16 20:29:50 +0000368 auto AttrWithBytesToString = [&](const char *Name) {
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000369 std::string Result;
Sanjoy Das5ff59072015-04-16 20:29:50 +0000370 Result += Name;
Bill Wendlingb29ce262013-02-11 08:43:33 +0000371 if (InAttrGrp) {
372 Result += "=";
373 Result += utostr(getValueAsInt());
374 } else {
375 Result += "(";
376 Result += utostr(getValueAsInt());
377 Result += ")";
378 }
Bill Wendling606c8e32013-01-29 03:20:31 +0000379 return Result;
Sanjoy Das5ff59072015-04-16 20:29:50 +0000380 };
Bill Wendling14292a62013-01-31 20:59:05 +0000381
Sanjoy Das5ff59072015-04-16 20:29:50 +0000382 if (hasAttribute(Attribute::StackAlignment))
383 return AttrWithBytesToString("alignstack");
384
385 if (hasAttribute(Attribute::Dereferenceable))
386 return AttrWithBytesToString("dereferenceable");
387
388 if (hasAttribute(Attribute::DereferenceableOrNull))
389 return AttrWithBytesToString("dereferenceable_or_null");
Hal Finkel11af4b42014-07-18 15:51:28 +0000390
George Burgess IV274105b2016-04-12 01:05:35 +0000391 if (hasAttribute(Attribute::AllocSize)) {
392 unsigned ElemSize;
393 Optional<unsigned> NumElems;
394 std::tie(ElemSize, NumElems) = getAllocSizeArgs();
395
396 std::string Result = "allocsize(";
397 Result += utostr(ElemSize);
398 if (NumElems.hasValue()) {
399 Result += ',';
400 Result += utostr(*NumElems);
401 }
402 Result += ')';
403 return Result;
404 }
405
Bill Wendling14292a62013-01-31 20:59:05 +0000406 // Convert target-dependent attributes to strings of the form:
407 //
408 // "kind"
409 // "kind" = "value"
Bill Wendling14292a62013-01-31 20:59:05 +0000410 //
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000411 if (isStringAttribute()) {
Bill Wendling14292a62013-01-31 20:59:05 +0000412 std::string Result;
Yaron Keren6e92e7b2015-03-30 15:42:36 +0000413 Result += (Twine('"') + getKindAsString() + Twine('"')).str();
Bill Wendling14292a62013-01-31 20:59:05 +0000414
Honggyu Kim89ea36c2016-09-01 11:44:06 +0000415 std::string AttrVal = pImpl->getValueAsString();
416 if (AttrVal.empty()) return Result;
Bill Wendling5a4041e2013-02-01 22:32:30 +0000417
Honggyu Kim89ea36c2016-09-01 11:44:06 +0000418 // Since some attribute strings contain special characters that cannot be
419 // printable, those have to be escaped to make the attribute value printable
420 // as is. e.g. "\01__gnu_mcount_nc"
421 {
422 raw_string_ostream OS(Result);
423 OS << "=\"";
Jonas Devlieghere7eeba252018-05-31 17:01:42 +0000424 printEscapedString(AttrVal, OS);
Honggyu Kim89ea36c2016-09-01 11:44:06 +0000425 OS << "\"";
426 }
Bill Wendling7beee282013-02-01 01:04:27 +0000427 return Result;
Bill Wendling14292a62013-01-31 20:59:05 +0000428 }
Bill Wendling606c8e32013-01-29 03:20:31 +0000429
430 llvm_unreachable("Unknown attribute");
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000431}
432
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000433bool Attribute::operator<(Attribute A) const {
434 if (!pImpl && !A.pImpl) return false;
435 if (!pImpl) return true;
436 if (!A.pImpl) return false;
437 return *pImpl < *A.pImpl;
438}
439
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000440//===----------------------------------------------------------------------===//
441// AttributeImpl Definition
442//===----------------------------------------------------------------------===//
443
Eric Christopher9c05de82014-07-02 22:05:40 +0000444// Pin the vtables to this file.
Eugene Zelenkob1df7872017-02-17 00:00:09 +0000445AttributeImpl::~AttributeImpl() = default;
446
Juergen Ributzka35436252013-11-19 00:57:56 +0000447void EnumAttributeImpl::anchor() {}
Eugene Zelenkob1df7872017-02-17 00:00:09 +0000448
Hal Finkeld0261682014-07-18 06:51:55 +0000449void IntAttributeImpl::anchor() {}
Eugene Zelenkob1df7872017-02-17 00:00:09 +0000450
Juergen Ributzka35436252013-11-19 00:57:56 +0000451void StringAttributeImpl::anchor() {}
Alexey Samsonovb21ab432013-11-18 09:31:53 +0000452
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000453bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000454 if (isStringAttribute()) return false;
455 return getKindAsEnum() == A;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000456}
457
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000458bool AttributeImpl::hasAttribute(StringRef Kind) const {
459 if (!isStringAttribute()) return false;
460 return getKindAsString() == Kind;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000461}
462
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000463Attribute::AttrKind AttributeImpl::getKindAsEnum() const {
Hal Finkeld0261682014-07-18 06:51:55 +0000464 assert(isEnumAttribute() || isIntAttribute());
Benjamin Kramere22cde02013-07-11 12:13:16 +0000465 return static_cast<const EnumAttributeImpl *>(this)->getEnumKind();
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000466}
467
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000468uint64_t AttributeImpl::getValueAsInt() const {
Hal Finkeld0261682014-07-18 06:51:55 +0000469 assert(isIntAttribute());
470 return static_cast<const IntAttributeImpl *>(this)->getValue();
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000471}
472
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000473StringRef AttributeImpl::getKindAsString() const {
Benjamin Kramere22cde02013-07-11 12:13:16 +0000474 assert(isStringAttribute());
475 return static_cast<const StringAttributeImpl *>(this)->getStringKind();
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000476}
477
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000478StringRef AttributeImpl::getValueAsString() const {
Benjamin Kramere22cde02013-07-11 12:13:16 +0000479 assert(isStringAttribute());
480 return static_cast<const StringAttributeImpl *>(this)->getStringValue();
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000481}
482
483bool AttributeImpl::operator<(const AttributeImpl &AI) const {
Bill Wendling7beee282013-02-01 01:04:27 +0000484 // This sorts the attributes with Attribute::AttrKinds coming first (sorted
485 // relative to their enum value) and then strings.
Bill Wendling94328f42013-02-15 05:25:26 +0000486 if (isEnumAttribute()) {
487 if (AI.isEnumAttribute()) return getKindAsEnum() < AI.getKindAsEnum();
Hal Finkeld0261682014-07-18 06:51:55 +0000488 if (AI.isIntAttribute()) return true;
Bill Wendling94328f42013-02-15 05:25:26 +0000489 if (AI.isStringAttribute()) return true;
490 }
Bill Wendling7beee282013-02-01 01:04:27 +0000491
Hal Finkeld0261682014-07-18 06:51:55 +0000492 if (isIntAttribute()) {
Bill Wendling94328f42013-02-15 05:25:26 +0000493 if (AI.isEnumAttribute()) return false;
Reid Kleckner39186512016-04-04 23:06:05 +0000494 if (AI.isIntAttribute()) {
495 if (getKindAsEnum() == AI.getKindAsEnum())
496 return getValueAsInt() < AI.getValueAsInt();
497 return getKindAsEnum() < AI.getKindAsEnum();
498 }
Bill Wendling94328f42013-02-15 05:25:26 +0000499 if (AI.isStringAttribute()) return true;
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000500 }
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000501
Bill Wendling94328f42013-02-15 05:25:26 +0000502 if (AI.isEnumAttribute()) return false;
Hal Finkeld0261682014-07-18 06:51:55 +0000503 if (AI.isIntAttribute()) return false;
Bill Wendling94328f42013-02-15 05:25:26 +0000504 if (getKindAsString() == AI.getKindAsString())
505 return getValueAsString() < AI.getValueAsString();
506 return getKindAsString() < AI.getKindAsString();
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000507}
508
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000509//===----------------------------------------------------------------------===//
Reid Kleckner06090402017-04-12 00:38:00 +0000510// AttributeSet Definition
511//===----------------------------------------------------------------------===//
512
513AttributeSet AttributeSet::get(LLVMContext &C, const AttrBuilder &B) {
514 return AttributeSet(AttributeSetNode::get(C, B));
515}
516
517AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<Attribute> Attrs) {
518 return AttributeSet(AttributeSetNode::get(C, Attrs));
519}
520
Javed Absara8ddcaa2017-05-11 12:28:08 +0000521AttributeSet AttributeSet::addAttribute(LLVMContext &C,
Reid Kleckner90e7ab12017-05-23 17:01:48 +0000522 Attribute::AttrKind Kind) const {
Javed Absara8ddcaa2017-05-11 12:28:08 +0000523 if (hasAttribute(Kind)) return *this;
524 AttrBuilder B;
525 B.addAttribute(Kind);
526 return addAttributes(C, AttributeSet::get(C, B));
527}
528
529AttributeSet AttributeSet::addAttribute(LLVMContext &C, StringRef Kind,
Reid Kleckner90e7ab12017-05-23 17:01:48 +0000530 StringRef Value) const {
Javed Absara8ddcaa2017-05-11 12:28:08 +0000531 AttrBuilder B;
532 B.addAttribute(Kind, Value);
533 return addAttributes(C, AttributeSet::get(C, B));
534}
535
536AttributeSet AttributeSet::addAttributes(LLVMContext &C,
537 const AttributeSet AS) const {
538 if (!hasAttributes())
539 return AS;
540
541 if (!AS.hasAttributes())
542 return *this;
543
544 AttrBuilder B(AS);
Eugene Zelenkoae117792018-03-30 00:47:31 +0000545 for (const auto I : *this)
Javed Absara8ddcaa2017-05-11 12:28:08 +0000546 B.addAttribute(I);
547
548 return get(C, B);
549}
550
551AttributeSet AttributeSet::removeAttribute(LLVMContext &C,
552 Attribute::AttrKind Kind) const {
553 if (!hasAttribute(Kind)) return *this;
Daniel Neilson9bc0b102018-01-17 19:15:21 +0000554 AttrBuilder B(*this);
555 B.removeAttribute(Kind);
556 return get(C, B);
Javed Absara8ddcaa2017-05-11 12:28:08 +0000557}
558
559AttributeSet AttributeSet::removeAttribute(LLVMContext &C,
560 StringRef Kind) const {
561 if (!hasAttribute(Kind)) return *this;
Daniel Neilson9bc0b102018-01-17 19:15:21 +0000562 AttrBuilder B(*this);
563 B.removeAttribute(Kind);
564 return get(C, B);
Javed Absara8ddcaa2017-05-11 12:28:08 +0000565}
566
567AttributeSet AttributeSet::removeAttributes(LLVMContext &C,
568 const AttrBuilder &Attrs) const {
Javed Absara8ddcaa2017-05-11 12:28:08 +0000569 AttrBuilder B(*this);
570 B.remove(Attrs);
571 return get(C, B);
572}
573
Reid Kleckner06090402017-04-12 00:38:00 +0000574unsigned AttributeSet::getNumAttributes() const {
575 return SetNode ? SetNode->getNumAttributes() : 0;
576}
577
578bool AttributeSet::hasAttribute(Attribute::AttrKind Kind) const {
Eugene Zelenko46795222017-05-15 21:57:41 +0000579 return SetNode ? SetNode->hasAttribute(Kind) : false;
Reid Kleckner06090402017-04-12 00:38:00 +0000580}
581
582bool AttributeSet::hasAttribute(StringRef Kind) const {
Eugene Zelenko46795222017-05-15 21:57:41 +0000583 return SetNode ? SetNode->hasAttribute(Kind) : false;
Reid Kleckner06090402017-04-12 00:38:00 +0000584}
585
586Attribute AttributeSet::getAttribute(Attribute::AttrKind Kind) const {
587 return SetNode ? SetNode->getAttribute(Kind) : Attribute();
588}
589
590Attribute AttributeSet::getAttribute(StringRef Kind) const {
591 return SetNode ? SetNode->getAttribute(Kind) : Attribute();
592}
593
594unsigned AttributeSet::getAlignment() const {
595 return SetNode ? SetNode->getAlignment() : 0;
596}
597
598unsigned AttributeSet::getStackAlignment() const {
599 return SetNode ? SetNode->getStackAlignment() : 0;
600}
601
602uint64_t AttributeSet::getDereferenceableBytes() const {
603 return SetNode ? SetNode->getDereferenceableBytes() : 0;
604}
605
606uint64_t AttributeSet::getDereferenceableOrNullBytes() const {
607 return SetNode ? SetNode->getDereferenceableOrNullBytes() : 0;
608}
609
610std::pair<unsigned, Optional<unsigned>> AttributeSet::getAllocSizeArgs() const {
Konstantin Zhuravlyovfae62202017-04-12 23:57:37 +0000611 return SetNode ? SetNode->getAllocSizeArgs()
612 : std::pair<unsigned, Optional<unsigned>>(0, 0);
Reid Kleckner06090402017-04-12 00:38:00 +0000613}
614
615std::string AttributeSet::getAsString(bool InAttrGrp) const {
616 return SetNode ? SetNode->getAsString(InAttrGrp) : "";
617}
618
619AttributeSet::iterator AttributeSet::begin() const {
620 return SetNode ? SetNode->begin() : nullptr;
621}
622
623AttributeSet::iterator AttributeSet::end() const {
624 return SetNode ? SetNode->end() : nullptr;
625}
626
Aaron Ballman1d03d382017-10-15 14:32:27 +0000627#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Javed Absara8ddcaa2017-05-11 12:28:08 +0000628LLVM_DUMP_METHOD void AttributeSet::dump() const {
629 dbgs() << "AS =\n";
630 dbgs() << " { ";
631 dbgs() << getAsString(true) << " }\n";
632}
633#endif
634
Reid Kleckner06090402017-04-12 00:38:00 +0000635//===----------------------------------------------------------------------===//
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000636// AttributeSetNode Definition
637//===----------------------------------------------------------------------===//
638
Reid Klecknere937c5e2017-04-10 23:46:08 +0000639AttributeSetNode::AttributeSetNode(ArrayRef<Attribute> Attrs)
Reid Kleckner06090402017-04-12 00:38:00 +0000640 : AvailableAttrs(0), NumAttrs(Attrs.size()) {
Reid Klecknere937c5e2017-04-10 23:46:08 +0000641 // There's memory after the node where we can store the entries in.
Fangrui Song53a62242018-11-17 01:44:25 +0000642 llvm::copy(Attrs, getTrailingObjects<Attribute>());
Reid Klecknere937c5e2017-04-10 23:46:08 +0000643
Eugene Zelenkoae117792018-03-30 00:47:31 +0000644 for (const auto I : *this) {
Reid Klecknere937c5e2017-04-10 23:46:08 +0000645 if (!I.isStringAttribute()) {
646 AvailableAttrs |= ((uint64_t)1) << I.getKindAsEnum();
647 }
648 }
649}
650
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000651AttributeSetNode *AttributeSetNode::get(LLVMContext &C,
652 ArrayRef<Attribute> Attrs) {
653 if (Attrs.empty())
Craig Topperec0f0bc2014-04-09 06:08:46 +0000654 return nullptr;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000655
656 // Otherwise, build a key to look up the existing attributes.
657 LLVMContextImpl *pImpl = C.pImpl;
658 FoldingSetNodeID ID;
659
660 SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end());
Fangrui Song3b35e172018-09-27 02:13:45 +0000661 llvm::sort(SortedAttrs);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000662
Eugene Zelenkoae117792018-03-30 00:47:31 +0000663 for (const auto Attr : SortedAttrs)
George Burgess IV6a7da772015-12-16 05:21:02 +0000664 Attr.Profile(ID);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000665
666 void *InsertPoint;
667 AttributeSetNode *PA =
668 pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint);
669
670 // If we didn't find any existing attributes of the same shape then create a
671 // new one and insert it.
672 if (!PA) {
Benjamin Kramere22cde02013-07-11 12:13:16 +0000673 // Coallocate entries after the AttributeSetNode itself.
James Y Knight1cf6cc72015-08-05 22:57:34 +0000674 void *Mem = ::operator new(totalSizeToAlloc<Attribute>(SortedAttrs.size()));
Benjamin Kramere22cde02013-07-11 12:13:16 +0000675 PA = new (Mem) AttributeSetNode(SortedAttrs);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000676 pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint);
677 }
678
Reid Kleckner67077702017-03-21 16:57:19 +0000679 // Return the AttributeSetNode that we found or created.
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000680 return PA;
681}
682
Reid Kleckner7dde8e82017-04-10 23:31:05 +0000683AttributeSetNode *AttributeSetNode::get(LLVMContext &C, const AttrBuilder &B) {
684 // Add target-independent attributes.
685 SmallVector<Attribute, 8> Attrs;
686 for (Attribute::AttrKind Kind = Attribute::None;
687 Kind != Attribute::EndAttrKinds; Kind = Attribute::AttrKind(Kind + 1)) {
688 if (!B.contains(Kind))
689 continue;
690
691 Attribute Attr;
692 switch (Kind) {
693 case Attribute::Alignment:
694 Attr = Attribute::getWithAlignment(C, B.getAlignment());
695 break;
696 case Attribute::StackAlignment:
697 Attr = Attribute::getWithStackAlignment(C, B.getStackAlignment());
698 break;
699 case Attribute::Dereferenceable:
700 Attr = Attribute::getWithDereferenceableBytes(
701 C, B.getDereferenceableBytes());
702 break;
703 case Attribute::DereferenceableOrNull:
704 Attr = Attribute::getWithDereferenceableOrNullBytes(
705 C, B.getDereferenceableOrNullBytes());
706 break;
707 case Attribute::AllocSize: {
708 auto A = B.getAllocSizeArgs();
709 Attr = Attribute::getWithAllocSizeArgs(C, A.first, A.second);
710 break;
711 }
712 default:
713 Attr = Attribute::get(C, Kind);
714 }
715 Attrs.push_back(Attr);
716 }
717
718 // Add target-dependent (string) attributes.
719 for (const auto &TDA : B.td_attrs())
720 Attrs.emplace_back(Attribute::get(C, TDA.first, TDA.second));
721
722 return get(C, Attrs);
723}
724
Bill Wendling0e9d5d02013-02-13 08:42:21 +0000725bool AttributeSetNode::hasAttribute(StringRef Kind) const {
Eugene Zelenkoae117792018-03-30 00:47:31 +0000726 for (const auto I : *this)
Benjamin Kramere96e21f2016-06-26 14:10:56 +0000727 if (I.hasAttribute(Kind))
Bill Wendling0e9d5d02013-02-13 08:42:21 +0000728 return true;
729 return false;
730}
731
732Attribute AttributeSetNode::getAttribute(Attribute::AttrKind Kind) const {
Matthias Brauna5c14e42016-01-29 22:25:13 +0000733 if (hasAttribute(Kind)) {
Eugene Zelenkoae117792018-03-30 00:47:31 +0000734 for (const auto I : *this)
Benjamin Kramere96e21f2016-06-26 14:10:56 +0000735 if (I.hasAttribute(Kind))
736 return I;
Matthias Brauna5c14e42016-01-29 22:25:13 +0000737 }
Eugene Zelenkoae117792018-03-30 00:47:31 +0000738 return {};
Bill Wendling0e9d5d02013-02-13 08:42:21 +0000739}
740
741Attribute AttributeSetNode::getAttribute(StringRef Kind) const {
Eugene Zelenkoae117792018-03-30 00:47:31 +0000742 for (const auto I : *this)
Benjamin Kramere96e21f2016-06-26 14:10:56 +0000743 if (I.hasAttribute(Kind))
744 return I;
Eugene Zelenkoae117792018-03-30 00:47:31 +0000745 return {};
Bill Wendling0e9d5d02013-02-13 08:42:21 +0000746}
747
Bill Wendling606c8e32013-01-29 03:20:31 +0000748unsigned AttributeSetNode::getAlignment() const {
Eugene Zelenkoae117792018-03-30 00:47:31 +0000749 for (const auto I : *this)
Benjamin Kramere96e21f2016-06-26 14:10:56 +0000750 if (I.hasAttribute(Attribute::Alignment))
751 return I.getAlignment();
Bill Wendling606c8e32013-01-29 03:20:31 +0000752 return 0;
753}
754
755unsigned AttributeSetNode::getStackAlignment() const {
Eugene Zelenkoae117792018-03-30 00:47:31 +0000756 for (const auto I : *this)
Benjamin Kramere96e21f2016-06-26 14:10:56 +0000757 if (I.hasAttribute(Attribute::StackAlignment))
758 return I.getStackAlignment();
Bill Wendling606c8e32013-01-29 03:20:31 +0000759 return 0;
760}
761
Hal Finkel11af4b42014-07-18 15:51:28 +0000762uint64_t AttributeSetNode::getDereferenceableBytes() const {
Eugene Zelenkoae117792018-03-30 00:47:31 +0000763 for (const auto I : *this)
Benjamin Kramere96e21f2016-06-26 14:10:56 +0000764 if (I.hasAttribute(Attribute::Dereferenceable))
765 return I.getDereferenceableBytes();
Hal Finkel11af4b42014-07-18 15:51:28 +0000766 return 0;
767}
768
Sanjoy Das5a6ea242015-05-06 17:41:54 +0000769uint64_t AttributeSetNode::getDereferenceableOrNullBytes() const {
Eugene Zelenkoae117792018-03-30 00:47:31 +0000770 for (const auto I : *this)
Benjamin Kramere96e21f2016-06-26 14:10:56 +0000771 if (I.hasAttribute(Attribute::DereferenceableOrNull))
772 return I.getDereferenceableOrNullBytes();
Sanjoy Das5a6ea242015-05-06 17:41:54 +0000773 return 0;
774}
775
George Burgess IV274105b2016-04-12 01:05:35 +0000776std::pair<unsigned, Optional<unsigned>>
777AttributeSetNode::getAllocSizeArgs() const {
Eugene Zelenkoae117792018-03-30 00:47:31 +0000778 for (const auto I : *this)
Benjamin Kramere96e21f2016-06-26 14:10:56 +0000779 if (I.hasAttribute(Attribute::AllocSize))
780 return I.getAllocSizeArgs();
George Burgess IV274105b2016-04-12 01:05:35 +0000781 return std::make_pair(0, 0);
782}
783
Rafael Espindolaaae02982013-05-01 13:07:03 +0000784std::string AttributeSetNode::getAsString(bool InAttrGrp) const {
Benjamin Kramere94e4ca2013-04-19 11:43:21 +0000785 std::string Str;
Benjamin Kramere22cde02013-07-11 12:13:16 +0000786 for (iterator I = begin(), E = end(); I != E; ++I) {
787 if (I != begin())
Rafael Espindolaaae02982013-05-01 13:07:03 +0000788 Str += ' ';
789 Str += I->getAsString(InAttrGrp);
Bill Wendling606c8e32013-01-29 03:20:31 +0000790 }
791 return Str;
792}
793
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000794//===----------------------------------------------------------------------===//
Reid Kleckner67077702017-03-21 16:57:19 +0000795// AttributeListImpl Definition
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000796//===----------------------------------------------------------------------===//
797
Reid Kleckner061e7012017-10-11 01:40:38 +0000798/// Map from AttributeList index to the internal array index. Adding one happens
799/// to work, but it relies on unsigned integer wrapping. MSVC warns about
800/// unsigned wrapping in constexpr functions, so write out the conditional. LLVM
801/// folds it to add anyway.
Reid Kleckner90e7ab12017-05-23 17:01:48 +0000802static constexpr unsigned attrIdxToArrayIdx(unsigned Index) {
Reid Kleckner061e7012017-10-11 01:40:38 +0000803 return Index == AttributeList::FunctionIndex ? 0 : Index + 1;
Reid Kleckner90e7ab12017-05-23 17:01:48 +0000804}
805
806AttributeListImpl::AttributeListImpl(LLVMContext &C,
807 ArrayRef<AttributeSet> Sets)
808 : AvailableFunctionAttrs(0), Context(C), NumAttrSets(Sets.size()) {
809 assert(!Sets.empty() && "pointless AttributeListImpl");
Reid Klecknera8ca3012017-04-11 00:16:00 +0000810
811 // There's memory after the node where we can store the entries in.
Fangrui Song53a62242018-11-17 01:44:25 +0000812 llvm::copy(Sets, getTrailingObjects<AttributeSet>());
Reid Klecknera8ca3012017-04-11 00:16:00 +0000813
814 // Initialize AvailableFunctionAttrs summary bitset.
Reid Kleckner7c6ef2a2017-04-12 22:22:01 +0000815 static_assert(Attribute::EndAttrKinds <=
816 sizeof(AvailableFunctionAttrs) * CHAR_BIT,
817 "Too many attributes");
Reid Kleckner90e7ab12017-05-23 17:01:48 +0000818 static_assert(attrIdxToArrayIdx(AttributeList::FunctionIndex) == 0U,
819 "function should be stored in slot 0");
Eugene Zelenkoae117792018-03-30 00:47:31 +0000820 for (const auto I : Sets[0]) {
Reid Kleckner90e7ab12017-05-23 17:01:48 +0000821 if (!I.isStringAttribute())
822 AvailableFunctionAttrs |= 1ULL << I.getKindAsEnum();
Reid Klecknera8ca3012017-04-11 00:16:00 +0000823 }
824}
825
826void AttributeListImpl::Profile(FoldingSetNodeID &ID) const {
Reid Kleckner90e7ab12017-05-23 17:01:48 +0000827 Profile(ID, makeArrayRef(begin(), end()));
Reid Klecknera8ca3012017-04-11 00:16:00 +0000828}
829
Reid Kleckner90e7ab12017-05-23 17:01:48 +0000830void AttributeListImpl::Profile(FoldingSetNodeID &ID,
831 ArrayRef<AttributeSet> Sets) {
832 for (const auto &Set : Sets)
833 ID.AddPointer(Set.SetNode);
Reid Klecknera8ca3012017-04-11 00:16:00 +0000834}
835
Aaron Ballman1d03d382017-10-15 14:32:27 +0000836#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Reid Kleckner67077702017-03-21 16:57:19 +0000837LLVM_DUMP_METHOD void AttributeListImpl::dump() const {
838 AttributeList(const_cast<AttributeListImpl *>(this)).dump();
Peter Collingbourne40bacac2013-08-02 22:34:30 +0000839}
Matthias Braun88d20752017-01-28 02:02:38 +0000840#endif
Peter Collingbourne40bacac2013-08-02 22:34:30 +0000841
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000842//===----------------------------------------------------------------------===//
Reid Kleckner67077702017-03-21 16:57:19 +0000843// AttributeList Construction and Mutation Methods
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000844//===----------------------------------------------------------------------===//
845
Reid Kleckner90e7ab12017-05-23 17:01:48 +0000846AttributeList AttributeList::getImpl(LLVMContext &C,
847 ArrayRef<AttributeSet> AttrSets) {
848 assert(!AttrSets.empty() && "pointless AttributeListImpl");
Reid Kleckner7dde8e82017-04-10 23:31:05 +0000849
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000850 LLVMContextImpl *pImpl = C.pImpl;
851 FoldingSetNodeID ID;
Reid Kleckner90e7ab12017-05-23 17:01:48 +0000852 AttributeListImpl::Profile(ID, AttrSets);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000853
854 void *InsertPoint;
Reid Kleckner67077702017-03-21 16:57:19 +0000855 AttributeListImpl *PA =
856 pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000857
858 // If we didn't find any existing attributes of the same shape then
859 // create a new one and insert it.
860 if (!PA) {
Reid Kleckner67077702017-03-21 16:57:19 +0000861 // Coallocate entries after the AttributeListImpl itself.
James Y Knight1cf6cc72015-08-05 22:57:34 +0000862 void *Mem = ::operator new(
Reid Kleckner90e7ab12017-05-23 17:01:48 +0000863 AttributeListImpl::totalSizeToAlloc<AttributeSet>(AttrSets.size()));
864 PA = new (Mem) AttributeListImpl(C, AttrSets);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000865 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
866 }
867
868 // Return the AttributesList that we found or created.
Reid Kleckner67077702017-03-21 16:57:19 +0000869 return AttributeList(PA);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000870}
871
Reid Kleckner67077702017-03-21 16:57:19 +0000872AttributeList
873AttributeList::get(LLVMContext &C,
874 ArrayRef<std::pair<unsigned, Attribute>> Attrs) {
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000875 // If there are no attributes then return a null AttributesList pointer.
876 if (Attrs.empty())
Eugene Zelenkoae117792018-03-30 00:47:31 +0000877 return {};
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000878
Craig Topper2921ff92016-01-03 19:43:40 +0000879 assert(std::is_sorted(Attrs.begin(), Attrs.end(),
880 [](const std::pair<unsigned, Attribute> &LHS,
881 const std::pair<unsigned, Attribute> &RHS) {
882 return LHS.first < RHS.first;
883 }) && "Misordered Attributes list!");
Eugene Zelenkoae117792018-03-30 00:47:31 +0000884 assert(llvm::none_of(Attrs,
885 [](const std::pair<unsigned, Attribute> &Pair) {
886 return Pair.second.hasAttribute(Attribute::None);
887 }) &&
David Majnemerdc9c7372016-08-11 21:15:00 +0000888 "Pointless attribute!");
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000889
890 // Create a vector if (unsigned, AttributeSetNode*) pairs from the attributes
891 // list.
Reid Kleckner06090402017-04-12 00:38:00 +0000892 SmallVector<std::pair<unsigned, AttributeSet>, 8> AttrPairVec;
Eugene Zelenkob1df7872017-02-17 00:00:09 +0000893 for (ArrayRef<std::pair<unsigned, Attribute>>::iterator I = Attrs.begin(),
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000894 E = Attrs.end(); I != E; ) {
895 unsigned Index = I->first;
896 SmallVector<Attribute, 4> AttrVec;
NAKAMURA Takumi3ba51ce2013-01-29 15:18:16 +0000897 while (I != E && I->first == Index) {
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000898 AttrVec.push_back(I->second);
899 ++I;
900 }
901
Reid Kleckner06090402017-04-12 00:38:00 +0000902 AttrPairVec.emplace_back(Index, AttributeSet::get(C, AttrVec));
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000903 }
904
Reid Kleckner90e7ab12017-05-23 17:01:48 +0000905 return get(C, AttrPairVec);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000906}
907
Reid Kleckner67077702017-03-21 16:57:19 +0000908AttributeList
909AttributeList::get(LLVMContext &C,
Reid Kleckner06090402017-04-12 00:38:00 +0000910 ArrayRef<std::pair<unsigned, AttributeSet>> Attrs) {
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000911 // If there are no attributes then return a null AttributesList pointer.
912 if (Attrs.empty())
Eugene Zelenkoae117792018-03-30 00:47:31 +0000913 return {};
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000914
Reid Kleckner90e7ab12017-05-23 17:01:48 +0000915 assert(std::is_sorted(Attrs.begin(), Attrs.end(),
916 [](const std::pair<unsigned, AttributeSet> &LHS,
917 const std::pair<unsigned, AttributeSet> &RHS) {
918 return LHS.first < RHS.first;
919 }) &&
920 "Misordered Attributes list!");
Eugene Zelenkoae117792018-03-30 00:47:31 +0000921 assert(llvm::none_of(Attrs,
922 [](const std::pair<unsigned, AttributeSet> &Pair) {
923 return !Pair.second.hasAttributes();
924 }) &&
Reid Kleckner90e7ab12017-05-23 17:01:48 +0000925 "Pointless attribute!");
926
927 unsigned MaxIndex = Attrs.back().first;
Craig Toppera71a3792018-04-16 17:05:01 +0000928 // If the MaxIndex is FunctionIndex and there are other indices in front
929 // of it, we need to use the largest of those to get the right size.
930 if (MaxIndex == FunctionIndex && Attrs.size() > 1)
931 MaxIndex = Attrs[Attrs.size() - 2].first;
Reid Kleckner90e7ab12017-05-23 17:01:48 +0000932
933 SmallVector<AttributeSet, 4> AttrVec(attrIdxToArrayIdx(MaxIndex) + 1);
Eugene Zelenkoae117792018-03-30 00:47:31 +0000934 for (const auto Pair : Attrs)
Reid Kleckner90e7ab12017-05-23 17:01:48 +0000935 AttrVec[attrIdxToArrayIdx(Pair.first)] = Pair.second;
936
937 return getImpl(C, AttrVec);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000938}
939
Reid Klecknere9a46bf2017-04-13 00:58:09 +0000940AttributeList AttributeList::get(LLVMContext &C, AttributeSet FnAttrs,
941 AttributeSet RetAttrs,
942 ArrayRef<AttributeSet> ArgAttrs) {
Reid Kleckner90e7ab12017-05-23 17:01:48 +0000943 // Scan from the end to find the last argument with attributes. Most
944 // arguments don't have attributes, so it's nice if we can have fewer unique
945 // AttributeListImpls by dropping empty attribute sets at the end of the list.
946 unsigned NumSets = 0;
947 for (size_t I = ArgAttrs.size(); I != 0; --I) {
948 if (ArgAttrs[I - 1].hasAttributes()) {
949 NumSets = I + 2;
950 break;
951 }
Reid Kleckner7dde8e82017-04-10 23:31:05 +0000952 }
Reid Kleckner90e7ab12017-05-23 17:01:48 +0000953 if (NumSets == 0) {
954 // Check function and return attributes if we didn't have argument
955 // attributes.
956 if (RetAttrs.hasAttributes())
957 NumSets = 2;
958 else if (FnAttrs.hasAttributes())
959 NumSets = 1;
960 }
961
962 // If all attribute sets were empty, we can use the empty attribute list.
963 if (NumSets == 0)
Eugene Zelenkoae117792018-03-30 00:47:31 +0000964 return {};
Reid Kleckner90e7ab12017-05-23 17:01:48 +0000965
966 SmallVector<AttributeSet, 8> AttrSets;
967 AttrSets.reserve(NumSets);
968 // If we have any attributes, we always have function attributes.
969 AttrSets.push_back(FnAttrs);
970 if (NumSets > 1)
971 AttrSets.push_back(RetAttrs);
972 if (NumSets > 2) {
973 // Drop the empty argument attribute sets at the end.
974 ArgAttrs = ArgAttrs.take_front(NumSets - 2);
975 AttrSets.insert(AttrSets.end(), ArgAttrs.begin(), ArgAttrs.end());
976 }
977
978 return getImpl(C, AttrSets);
Reid Kleckner7dde8e82017-04-10 23:31:05 +0000979}
980
Reid Kleckner67077702017-03-21 16:57:19 +0000981AttributeList AttributeList::get(LLVMContext &C, unsigned Index,
982 const AttrBuilder &B) {
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000983 if (!B.hasAttributes())
Eugene Zelenkoae117792018-03-30 00:47:31 +0000984 return {};
Reid Kleckner90e7ab12017-05-23 17:01:48 +0000985 Index = attrIdxToArrayIdx(Index);
986 SmallVector<AttributeSet, 8> AttrSets(Index + 1);
987 AttrSets[Index] = AttributeSet::get(C, B);
988 return getImpl(C, AttrSets);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000989}
990
Reid Kleckner67077702017-03-21 16:57:19 +0000991AttributeList AttributeList::get(LLVMContext &C, unsigned Index,
992 ArrayRef<Attribute::AttrKind> Kinds) {
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000993 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
Eugene Zelenkoae117792018-03-30 00:47:31 +0000994 for (const auto K : Kinds)
David Majnemerdc9c7372016-08-11 21:15:00 +0000995 Attrs.emplace_back(Index, Attribute::get(C, K));
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000996 return get(C, Attrs);
997}
998
Reid Kleckner67077702017-03-21 16:57:19 +0000999AttributeList AttributeList::get(LLVMContext &C, unsigned Index,
1000 ArrayRef<StringRef> Kinds) {
Amaury Secheta2f727d2016-06-15 17:50:39 +00001001 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
Eugene Zelenkoae117792018-03-30 00:47:31 +00001002 for (const auto K : Kinds)
David Majnemerdc9c7372016-08-11 21:15:00 +00001003 Attrs.emplace_back(Index, Attribute::get(C, K));
Amaury Secheta2f727d2016-06-15 17:50:39 +00001004 return get(C, Attrs);
1005}
1006
Reid Kleckner67077702017-03-21 16:57:19 +00001007AttributeList AttributeList::get(LLVMContext &C,
1008 ArrayRef<AttributeList> Attrs) {
1009 if (Attrs.empty())
Eugene Zelenkoae117792018-03-30 00:47:31 +00001010 return {};
Reid Kleckner90e7ab12017-05-23 17:01:48 +00001011 if (Attrs.size() == 1)
1012 return Attrs[0];
Bill Wendlingc22f4aa2013-01-29 00:34:06 +00001013
Reid Kleckner90e7ab12017-05-23 17:01:48 +00001014 unsigned MaxSize = 0;
Eugene Zelenkoae117792018-03-30 00:47:31 +00001015 for (const auto List : Attrs)
Reid Kleckner90e7ab12017-05-23 17:01:48 +00001016 MaxSize = std::max(MaxSize, List.getNumAttrSets());
1017
Reid Kleckner3db12992017-05-31 14:24:06 +00001018 // If every list was empty, there is no point in merging the lists.
1019 if (MaxSize == 0)
Eugene Zelenkoae117792018-03-30 00:47:31 +00001020 return {};
Reid Kleckner3db12992017-05-31 14:24:06 +00001021
Reid Kleckner90e7ab12017-05-23 17:01:48 +00001022 SmallVector<AttributeSet, 8> NewAttrSets(MaxSize);
1023 for (unsigned I = 0; I < MaxSize; ++I) {
1024 AttrBuilder CurBuilder;
Eugene Zelenkoae117792018-03-30 00:47:31 +00001025 for (const auto List : Attrs)
Reid Kleckner90e7ab12017-05-23 17:01:48 +00001026 CurBuilder.merge(List.getAttributes(I - 1));
1027 NewAttrSets[I] = AttributeSet::get(C, CurBuilder);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +00001028 }
1029
Reid Kleckner90e7ab12017-05-23 17:01:48 +00001030 return getImpl(C, NewAttrSets);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +00001031}
1032
Reid Kleckner67077702017-03-21 16:57:19 +00001033AttributeList AttributeList::addAttribute(LLVMContext &C, unsigned Index,
1034 Attribute::AttrKind Kind) const {
Amaury Sechet3564e5c2016-06-14 20:27:35 +00001035 if (hasAttribute(Index, Kind)) return *this;
Reid Klecknerdac74872017-05-02 22:07:37 +00001036 AttrBuilder B;
1037 B.addAttribute(Kind);
1038 return addAttributes(C, Index, B);
Reed Kotler9106f732013-03-13 20:20:08 +00001039}
1040
Reid Kleckner67077702017-03-21 16:57:19 +00001041AttributeList AttributeList::addAttribute(LLVMContext &C, unsigned Index,
1042 StringRef Kind,
1043 StringRef Value) const {
Eugene Zelenkob1df7872017-02-17 00:00:09 +00001044 AttrBuilder B;
Bill Wendling9e2ef772013-07-25 18:34:24 +00001045 B.addAttribute(Kind, Value);
Reid Klecknerdac74872017-05-02 22:07:37 +00001046 return addAttributes(C, Index, B);
Bill Wendling9e2ef772013-07-25 18:34:24 +00001047}
1048
Reid Kleckner1e9afac2017-05-31 19:23:09 +00001049AttributeList AttributeList::addAttribute(LLVMContext &C, unsigned Index,
Reid Kleckner67077702017-03-21 16:57:19 +00001050 Attribute A) const {
Reid Kleckner1e9afac2017-05-31 19:23:09 +00001051 AttrBuilder B;
1052 B.addAttribute(A);
1053 return addAttributes(C, Index, B);
Akira Hatanakaec268662015-12-02 06:58:49 +00001054}
1055
Reid Kleckner67077702017-03-21 16:57:19 +00001056AttributeList AttributeList::addAttributes(LLVMContext &C, unsigned Index,
Reid Kleckner4bc9eb62017-04-19 01:51:13 +00001057 const AttrBuilder &B) const {
1058 if (!B.hasAttributes())
Reid Kleckner7dde8e82017-04-10 23:31:05 +00001059 return *this;
1060
Reid Kleckner129271c2017-04-18 22:10:18 +00001061 if (!pImpl)
Reid Kleckner4bc9eb62017-04-19 01:51:13 +00001062 return AttributeList::get(C, {{Index, AttributeSet::get(C, B)}});
Reid Kleckner129271c2017-04-18 22:10:18 +00001063
Bill Wendlingc22f4aa2013-01-29 00:34:06 +00001064#ifndef NDEBUG
1065 // FIXME it is not obvious how this should work for alignment. For now, say
1066 // we can't change a known alignment.
Reid Klecknere6f20782017-05-19 22:23:47 +00001067 unsigned OldAlign = getAttributes(Index).getAlignment();
Reid Kleckner4bc9eb62017-04-19 01:51:13 +00001068 unsigned NewAlign = B.getAlignment();
Bill Wendlingc22f4aa2013-01-29 00:34:06 +00001069 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
1070 "Attempt to change alignment!");
1071#endif
1072
Reid Kleckner90e7ab12017-05-23 17:01:48 +00001073 Index = attrIdxToArrayIdx(Index);
1074 SmallVector<AttributeSet, 4> AttrSets(this->begin(), this->end());
1075 if (Index >= AttrSets.size())
1076 AttrSets.resize(Index + 1);
Reid Kleckner7dde8e82017-04-10 23:31:05 +00001077
Reid Kleckner90e7ab12017-05-23 17:01:48 +00001078 AttrBuilder Merged(AttrSets[Index]);
1079 Merged.merge(B);
1080 AttrSets[Index] = AttributeSet::get(C, Merged);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +00001081
Reid Kleckner90e7ab12017-05-23 17:01:48 +00001082 return getImpl(C, AttrSets);
Reid Kleckner7dde8e82017-04-10 23:31:05 +00001083}
1084
Reid Kleckner1e9afac2017-05-31 19:23:09 +00001085AttributeList AttributeList::addParamAttribute(LLVMContext &C,
1086 ArrayRef<unsigned> ArgNos,
1087 Attribute A) const {
1088 assert(std::is_sorted(ArgNos.begin(), ArgNos.end()));
1089
1090 SmallVector<AttributeSet, 4> AttrSets(this->begin(), this->end());
1091 unsigned MaxIndex = attrIdxToArrayIdx(ArgNos.back() + FirstArgIndex);
1092 if (MaxIndex >= AttrSets.size())
1093 AttrSets.resize(MaxIndex + 1);
1094
1095 for (unsigned ArgNo : ArgNos) {
1096 unsigned Index = attrIdxToArrayIdx(ArgNo + FirstArgIndex);
1097 AttrBuilder B(AttrSets[Index]);
1098 B.addAttribute(A);
1099 AttrSets[Index] = AttributeSet::get(C, B);
1100 }
1101
1102 return getImpl(C, AttrSets);
1103}
1104
Reid Kleckner67077702017-03-21 16:57:19 +00001105AttributeList AttributeList::removeAttribute(LLVMContext &C, unsigned Index,
1106 Attribute::AttrKind Kind) const {
Amaury Sechet3564e5c2016-06-14 20:27:35 +00001107 if (!hasAttribute(Index, Kind)) return *this;
Daniel Neilson9bc0b102018-01-17 19:15:21 +00001108
1109 Index = attrIdxToArrayIdx(Index);
1110 SmallVector<AttributeSet, 4> AttrSets(this->begin(), this->end());
1111 assert(Index < AttrSets.size());
1112
1113 AttrSets[Index] = AttrSets[Index].removeAttribute(C, Kind);
1114
1115 return getImpl(C, AttrSets);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +00001116}
1117
Reid Kleckner67077702017-03-21 16:57:19 +00001118AttributeList AttributeList::removeAttribute(LLVMContext &C, unsigned Index,
1119 StringRef Kind) const {
Amaury Secheta2f727d2016-06-15 17:50:39 +00001120 if (!hasAttribute(Index, Kind)) return *this;
Daniel Neilson9bc0b102018-01-17 19:15:21 +00001121
1122 Index = attrIdxToArrayIdx(Index);
1123 SmallVector<AttributeSet, 4> AttrSets(this->begin(), this->end());
1124 assert(Index < AttrSets.size());
1125
1126 AttrSets[Index] = AttrSets[Index].removeAttribute(C, Kind);
1127
1128 return getImpl(C, AttrSets);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +00001129}
1130
Reid Kleckner90e7ab12017-05-23 17:01:48 +00001131AttributeList
1132AttributeList::removeAttributes(LLVMContext &C, unsigned Index,
1133 const AttrBuilder &AttrsToRemove) const {
Reid Kleckner67077702017-03-21 16:57:19 +00001134 if (!pImpl)
Eugene Zelenkoae117792018-03-30 00:47:31 +00001135 return {};
Pete Cooperc58f23e2015-05-06 23:19:43 +00001136
Reid Kleckner90e7ab12017-05-23 17:01:48 +00001137 Index = attrIdxToArrayIdx(Index);
1138 SmallVector<AttributeSet, 4> AttrSets(this->begin(), this->end());
1139 if (Index >= AttrSets.size())
1140 AttrSets.resize(Index + 1);
Pete Cooperc58f23e2015-05-06 23:19:43 +00001141
Daniel Neilson9bc0b102018-01-17 19:15:21 +00001142 AttrSets[Index] = AttrSets[Index].removeAttributes(C, AttrsToRemove);
Pete Cooperc58f23e2015-05-06 23:19:43 +00001143
Reid Kleckner90e7ab12017-05-23 17:01:48 +00001144 return getImpl(C, AttrSets);
Pete Cooperc58f23e2015-05-06 23:19:43 +00001145}
1146
Reid Kleckner7dde8e82017-04-10 23:31:05 +00001147AttributeList AttributeList::removeAttributes(LLVMContext &C,
1148 unsigned WithoutIndex) const {
1149 if (!pImpl)
Eugene Zelenkoae117792018-03-30 00:47:31 +00001150 return {};
Reid Kleckner90e7ab12017-05-23 17:01:48 +00001151 WithoutIndex = attrIdxToArrayIdx(WithoutIndex);
1152 if (WithoutIndex >= getNumAttrSets())
1153 return *this;
1154 SmallVector<AttributeSet, 4> AttrSets(this->begin(), this->end());
1155 AttrSets[WithoutIndex] = AttributeSet();
1156 return getImpl(C, AttrSets);
Reid Kleckner7dde8e82017-04-10 23:31:05 +00001157}
1158
Reid Kleckner67077702017-03-21 16:57:19 +00001159AttributeList AttributeList::addDereferenceableAttr(LLVMContext &C,
1160 unsigned Index,
1161 uint64_t Bytes) const {
Eugene Zelenkob1df7872017-02-17 00:00:09 +00001162 AttrBuilder B;
Ramkumar Ramachandra0608cec2015-02-14 19:37:54 +00001163 B.addDereferenceableAttr(Bytes);
Reid Klecknerdac74872017-05-02 22:07:37 +00001164 return addAttributes(C, Index, B);
Ramkumar Ramachandra0608cec2015-02-14 19:37:54 +00001165}
1166
Reid Kleckner67077702017-03-21 16:57:19 +00001167AttributeList
1168AttributeList::addDereferenceableOrNullAttr(LLVMContext &C, unsigned Index,
1169 uint64_t Bytes) const {
Eugene Zelenkob1df7872017-02-17 00:00:09 +00001170 AttrBuilder B;
Sanjoy Das5ff59072015-04-16 20:29:50 +00001171 B.addDereferenceableOrNullAttr(Bytes);
Reid Klecknerdac74872017-05-02 22:07:37 +00001172 return addAttributes(C, Index, B);
Sanjoy Das5ff59072015-04-16 20:29:50 +00001173}
1174
Reid Kleckner67077702017-03-21 16:57:19 +00001175AttributeList
1176AttributeList::addAllocSizeAttr(LLVMContext &C, unsigned Index,
1177 unsigned ElemSizeArg,
1178 const Optional<unsigned> &NumElemsArg) {
Eugene Zelenkob1df7872017-02-17 00:00:09 +00001179 AttrBuilder B;
George Burgess IV274105b2016-04-12 01:05:35 +00001180 B.addAllocSizeAttr(ElemSizeArg, NumElemsArg);
Reid Klecknerdac74872017-05-02 22:07:37 +00001181 return addAttributes(C, Index, B);
George Burgess IV274105b2016-04-12 01:05:35 +00001182}
1183
Bill Wendlingc22f4aa2013-01-29 00:34:06 +00001184//===----------------------------------------------------------------------===//
Reid Kleckner67077702017-03-21 16:57:19 +00001185// AttributeList Accessor Methods
Bill Wendlingc22f4aa2013-01-29 00:34:06 +00001186//===----------------------------------------------------------------------===//
1187
Reid Kleckner67077702017-03-21 16:57:19 +00001188LLVMContext &AttributeList::getContext() const { return pImpl->getContext(); }
1189
Reid Kleckner1c35def2017-04-13 23:12:13 +00001190AttributeSet AttributeList::getParamAttributes(unsigned ArgNo) const {
Reid Klecknera82b3762017-05-03 18:17:31 +00001191 return getAttributes(ArgNo + FirstArgIndex);
Bill Wendling85b3fbe2013-02-10 05:00:40 +00001192}
1193
Reid Kleckner06090402017-04-12 00:38:00 +00001194AttributeSet AttributeList::getRetAttributes() const {
Reid Kleckner7dde8e82017-04-10 23:31:05 +00001195 return getAttributes(ReturnIndex);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +00001196}
1197
Reid Kleckner06090402017-04-12 00:38:00 +00001198AttributeSet AttributeList::getFnAttributes() const {
Reid Kleckner7dde8e82017-04-10 23:31:05 +00001199 return getAttributes(FunctionIndex);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +00001200}
1201
Reid Kleckner67077702017-03-21 16:57:19 +00001202bool AttributeList::hasAttribute(unsigned Index,
1203 Attribute::AttrKind Kind) const {
Reid Kleckner06090402017-04-12 00:38:00 +00001204 return getAttributes(Index).hasAttribute(Kind);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +00001205}
1206
Reid Kleckner67077702017-03-21 16:57:19 +00001207bool AttributeList::hasAttribute(unsigned Index, StringRef Kind) const {
Reid Kleckner06090402017-04-12 00:38:00 +00001208 return getAttributes(Index).hasAttribute(Kind);
Bill Wendling0e9d5d02013-02-13 08:42:21 +00001209}
1210
Reid Kleckner67077702017-03-21 16:57:19 +00001211bool AttributeList::hasAttributes(unsigned Index) const {
Reid Kleckner06090402017-04-12 00:38:00 +00001212 return getAttributes(Index).hasAttributes();
Bill Wendlingc22f4aa2013-01-29 00:34:06 +00001213}
1214
Reid Kleckner67077702017-03-21 16:57:19 +00001215bool AttributeList::hasFnAttribute(Attribute::AttrKind Kind) const {
Matthias Braun1a0e2912016-01-29 22:25:19 +00001216 return pImpl && pImpl->hasFnAttribute(Kind);
1217}
1218
Reid Kleckner67077702017-03-21 16:57:19 +00001219bool AttributeList::hasFnAttribute(StringRef Kind) const {
1220 return hasAttribute(AttributeList::FunctionIndex, Kind);
Amaury Sechet7f692c72016-09-09 04:50:38 +00001221}
1222
Reid Kleckner1c35def2017-04-13 23:12:13 +00001223bool AttributeList::hasParamAttribute(unsigned ArgNo,
1224 Attribute::AttrKind Kind) const {
Reid Kleckner90e7ab12017-05-23 17:01:48 +00001225 return hasAttribute(ArgNo + FirstArgIndex, Kind);
Reid Kleckner1c35def2017-04-13 23:12:13 +00001226}
1227
Reid Kleckner67077702017-03-21 16:57:19 +00001228bool AttributeList::hasAttrSomewhere(Attribute::AttrKind Attr,
1229 unsigned *Index) const {
Craig Topperec0f0bc2014-04-09 06:08:46 +00001230 if (!pImpl) return false;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +00001231
Reid Kleckner90e7ab12017-05-23 17:01:48 +00001232 for (unsigned I = index_begin(), E = index_end(); I != E; ++I) {
1233 if (hasAttribute(I, Attr)) {
1234 if (Index)
1235 *Index = I;
1236 return true;
1237 }
1238 }
Bill Wendlingc22f4aa2013-01-29 00:34:06 +00001239
1240 return false;
1241}
1242
Reid Kleckner67077702017-03-21 16:57:19 +00001243Attribute AttributeList::getAttribute(unsigned Index,
1244 Attribute::AttrKind Kind) const {
Reid Kleckner06090402017-04-12 00:38:00 +00001245 return getAttributes(Index).getAttribute(Kind);
Bill Wendling0e9d5d02013-02-13 08:42:21 +00001246}
1247
Reid Kleckner67077702017-03-21 16:57:19 +00001248Attribute AttributeList::getAttribute(unsigned Index, StringRef Kind) const {
Reid Kleckner06090402017-04-12 00:38:00 +00001249 return getAttributes(Index).getAttribute(Kind);
Bill Wendling0e9d5d02013-02-13 08:42:21 +00001250}
1251
Reid Kleckner52b02282017-04-28 20:34:27 +00001252unsigned AttributeList::getRetAlignment() const {
1253 return getAttributes(ReturnIndex).getAlignment();
1254}
1255
1256unsigned AttributeList::getParamAlignment(unsigned ArgNo) const {
Reid Klecknera82b3762017-05-03 18:17:31 +00001257 return getAttributes(ArgNo + FirstArgIndex).getAlignment();
Bill Wendlingc22f4aa2013-01-29 00:34:06 +00001258}
1259
Reid Kleckner67077702017-03-21 16:57:19 +00001260unsigned AttributeList::getStackAlignment(unsigned Index) const {
Reid Kleckner06090402017-04-12 00:38:00 +00001261 return getAttributes(Index).getStackAlignment();
Bill Wendlingc22f4aa2013-01-29 00:34:06 +00001262}
1263
Reid Kleckner67077702017-03-21 16:57:19 +00001264uint64_t AttributeList::getDereferenceableBytes(unsigned Index) const {
Reid Kleckner06090402017-04-12 00:38:00 +00001265 return getAttributes(Index).getDereferenceableBytes();
Hal Finkel11af4b42014-07-18 15:51:28 +00001266}
1267
Reid Kleckner67077702017-03-21 16:57:19 +00001268uint64_t AttributeList::getDereferenceableOrNullBytes(unsigned Index) const {
Reid Kleckner06090402017-04-12 00:38:00 +00001269 return getAttributes(Index).getDereferenceableOrNullBytes();
Sanjoy Das5a6ea242015-05-06 17:41:54 +00001270}
1271
George Burgess IV274105b2016-04-12 01:05:35 +00001272std::pair<unsigned, Optional<unsigned>>
Reid Kleckner67077702017-03-21 16:57:19 +00001273AttributeList::getAllocSizeArgs(unsigned Index) const {
Reid Kleckner06090402017-04-12 00:38:00 +00001274 return getAttributes(Index).getAllocSizeArgs();
George Burgess IV274105b2016-04-12 01:05:35 +00001275}
1276
Reid Kleckner67077702017-03-21 16:57:19 +00001277std::string AttributeList::getAsString(unsigned Index, bool InAttrGrp) const {
Reid Kleckner06090402017-04-12 00:38:00 +00001278 return getAttributes(Index).getAsString(InAttrGrp);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +00001279}
1280
Reid Kleckner06090402017-04-12 00:38:00 +00001281AttributeSet AttributeList::getAttributes(unsigned Index) const {
Reid Kleckner90e7ab12017-05-23 17:01:48 +00001282 Index = attrIdxToArrayIdx(Index);
1283 if (!pImpl || Index >= getNumAttrSets())
Eugene Zelenkoae117792018-03-30 00:47:31 +00001284 return {};
Reid Kleckner90e7ab12017-05-23 17:01:48 +00001285 return pImpl->begin()[Index];
Bill Wendlingc22f4aa2013-01-29 00:34:06 +00001286}
1287
Reid Kleckner90e7ab12017-05-23 17:01:48 +00001288AttributeList::iterator AttributeList::begin() const {
1289 return pImpl ? pImpl->begin() : nullptr;
Bill Wendling16c4b3c2013-01-31 23:53:05 +00001290}
1291
Reid Kleckner90e7ab12017-05-23 17:01:48 +00001292AttributeList::iterator AttributeList::end() const {
1293 return pImpl ? pImpl->end() : nullptr;
Bill Wendling16c4b3c2013-01-31 23:53:05 +00001294}
1295
Bill Wendlingc22f4aa2013-01-29 00:34:06 +00001296//===----------------------------------------------------------------------===//
Reid Kleckner67077702017-03-21 16:57:19 +00001297// AttributeList Introspection Methods
Bill Wendlingc22f4aa2013-01-29 00:34:06 +00001298//===----------------------------------------------------------------------===//
1299
Reid Kleckner90e7ab12017-05-23 17:01:48 +00001300unsigned AttributeList::getNumAttrSets() const {
1301 return pImpl ? pImpl->NumAttrSets : 0;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +00001302}
1303
Aaron Ballman1d03d382017-10-15 14:32:27 +00001304#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Reid Kleckner67077702017-03-21 16:57:19 +00001305LLVM_DUMP_METHOD void AttributeList::dump() const {
Bill Wendlingc22f4aa2013-01-29 00:34:06 +00001306 dbgs() << "PAL[\n";
1307
Reid Kleckner90e7ab12017-05-23 17:01:48 +00001308 for (unsigned i = index_begin(), e = index_end(); i != e; ++i) {
1309 if (getAttributes(i).hasAttributes())
1310 dbgs() << " { " << i << " => " << getAsString(i) << " }\n";
Bill Wendlingc22f4aa2013-01-29 00:34:06 +00001311 }
1312
1313 dbgs() << "]\n";
1314}
Matthias Braun88d20752017-01-28 02:02:38 +00001315#endif
Bill Wendlingc22f4aa2013-01-29 00:34:06 +00001316
Bill Wendlinge66f3d32012-10-05 06:44:41 +00001317//===----------------------------------------------------------------------===//
Bill Wendling03198882013-01-04 23:27:34 +00001318// AttrBuilder Method Implementations
Bill Wendlinge66f3d32012-10-05 06:44:41 +00001319//===----------------------------------------------------------------------===//
1320
Reid Kleckner90e7ab12017-05-23 17:01:48 +00001321// FIXME: Remove this ctor, use AttributeSet.
Reid Kleckner06090402017-04-12 00:38:00 +00001322AttrBuilder::AttrBuilder(AttributeList AL, unsigned Index) {
Reid Kleckner90e7ab12017-05-23 17:01:48 +00001323 AttributeSet AS = AL.getAttributes(Index);
Eugene Zelenkoae117792018-03-30 00:47:31 +00001324 for (const auto &A : AS)
Reid Kleckner90e7ab12017-05-23 17:01:48 +00001325 addAttribute(A);
Bill Wendlinga90a99a2013-01-07 08:24:35 +00001326}
1327
Reid Kleckner06090402017-04-12 00:38:00 +00001328AttrBuilder::AttrBuilder(AttributeSet AS) {
Eugene Zelenkoae117792018-03-30 00:47:31 +00001329 for (const auto &A : AS)
Reid Kleckner90e7ab12017-05-23 17:01:48 +00001330 addAttribute(A);
Reid Kleckner7dde8e82017-04-10 23:31:05 +00001331}
1332
Bill Wendling03198882013-01-04 23:27:34 +00001333void AttrBuilder::clear() {
Benjamin Kramer3f213e72013-02-18 12:09:51 +00001334 Attrs.reset();
Sanjoy Dasdc0eb172015-09-03 22:27:42 +00001335 TargetDepAttrs.clear();
Sanjoy Das5ff59072015-04-16 20:29:50 +00001336 Alignment = StackAlignment = DerefBytes = DerefOrNullBytes = 0;
George Burgess IV274105b2016-04-12 01:05:35 +00001337 AllocSizeArgs = 0;
Bill Wendling03198882013-01-04 23:27:34 +00001338}
1339
1340AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
Benjamin Kramer3f213e72013-02-18 12:09:51 +00001341 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
Bill Wendling169d5272013-01-31 23:16:25 +00001342 assert(Val != Attribute::Alignment && Val != Attribute::StackAlignment &&
George Burgess IV274105b2016-04-12 01:05:35 +00001343 Val != Attribute::Dereferenceable && Val != Attribute::AllocSize &&
Hal Finkel11af4b42014-07-18 15:51:28 +00001344 "Adding integer attribute without adding a value!");
Benjamin Kramer3f213e72013-02-18 12:09:51 +00001345 Attrs[Val] = true;
Bill Wendling3a106e62012-10-09 19:01:18 +00001346 return *this;
Bill Wendlinge66f3d32012-10-05 06:44:41 +00001347}
1348
Bill Wendling39da0782013-01-31 23:38:01 +00001349AttrBuilder &AttrBuilder::addAttribute(Attribute Attr) {
Bill Wendling09ed9102013-02-10 10:13:23 +00001350 if (Attr.isStringAttribute()) {
1351 addAttribute(Attr.getKindAsString(), Attr.getValueAsString());
1352 return *this;
1353 }
1354
Bill Wendling8c74ecf2013-02-05 22:37:24 +00001355 Attribute::AttrKind Kind = Attr.getKindAsEnum();
Benjamin Kramer3f213e72013-02-18 12:09:51 +00001356 Attrs[Kind] = true;
Bill Wendling49f60602013-01-28 05:23:28 +00001357
Bill Wendling8c74ecf2013-02-05 22:37:24 +00001358 if (Kind == Attribute::Alignment)
Bill Wendling49f60602013-01-28 05:23:28 +00001359 Alignment = Attr.getAlignment();
Bill Wendling8c74ecf2013-02-05 22:37:24 +00001360 else if (Kind == Attribute::StackAlignment)
Bill Wendling49f60602013-01-28 05:23:28 +00001361 StackAlignment = Attr.getStackAlignment();
Hal Finkel11af4b42014-07-18 15:51:28 +00001362 else if (Kind == Attribute::Dereferenceable)
1363 DerefBytes = Attr.getDereferenceableBytes();
Sanjoy Das5ff59072015-04-16 20:29:50 +00001364 else if (Kind == Attribute::DereferenceableOrNull)
1365 DerefOrNullBytes = Attr.getDereferenceableOrNullBytes();
George Burgess IV274105b2016-04-12 01:05:35 +00001366 else if (Kind == Attribute::AllocSize)
1367 AllocSizeArgs = Attr.getValueAsInt();
Bill Wendling49f60602013-01-28 05:23:28 +00001368 return *this;
1369}
1370
Bill Wendlingea59f892013-02-05 08:09:32 +00001371AttrBuilder &AttrBuilder::addAttribute(StringRef A, StringRef V) {
1372 TargetDepAttrs[A] = V;
1373 return *this;
1374}
1375
Bill Wendling39da0782013-01-31 23:38:01 +00001376AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
Benjamin Kramer3f213e72013-02-18 12:09:51 +00001377 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
1378 Attrs[Val] = false;
Bill Wendling39da0782013-01-31 23:38:01 +00001379
1380 if (Val == Attribute::Alignment)
1381 Alignment = 0;
1382 else if (Val == Attribute::StackAlignment)
1383 StackAlignment = 0;
Hal Finkel11af4b42014-07-18 15:51:28 +00001384 else if (Val == Attribute::Dereferenceable)
1385 DerefBytes = 0;
Sanjoy Das5ff59072015-04-16 20:29:50 +00001386 else if (Val == Attribute::DereferenceableOrNull)
1387 DerefOrNullBytes = 0;
George Burgess IV274105b2016-04-12 01:05:35 +00001388 else if (Val == Attribute::AllocSize)
1389 AllocSizeArgs = 0;
Bill Wendling39da0782013-01-31 23:38:01 +00001390
1391 return *this;
1392}
1393
Reid Kleckner67077702017-03-21 16:57:19 +00001394AttrBuilder &AttrBuilder::removeAttributes(AttributeList A, uint64_t Index) {
Reid Kleckner331b9af2017-04-28 18:37:16 +00001395 remove(A.getAttributes(Index));
Bill Wendling49f60602013-01-28 05:23:28 +00001396 return *this;
1397}
1398
Bill Wendlingea59f892013-02-05 08:09:32 +00001399AttrBuilder &AttrBuilder::removeAttribute(StringRef A) {
Eugene Zelenkoae117792018-03-30 00:47:31 +00001400 auto I = TargetDepAttrs.find(A);
Bill Wendlingea59f892013-02-05 08:09:32 +00001401 if (I != TargetDepAttrs.end())
1402 TargetDepAttrs.erase(I);
1403 return *this;
1404}
1405
George Burgess IV274105b2016-04-12 01:05:35 +00001406std::pair<unsigned, Optional<unsigned>> AttrBuilder::getAllocSizeArgs() const {
1407 return unpackAllocSizeArgs(AllocSizeArgs);
1408}
1409
Bill Wendling702cc912012-10-15 20:35:56 +00001410AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
Bill Wendlingda3f9d82012-10-14 03:58:29 +00001411 if (Align == 0) return *this;
Bill Wendling03198882013-01-04 23:27:34 +00001412
Bill Wendlinge66f3d32012-10-05 06:44:41 +00001413 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1414 assert(Align <= 0x40000000 && "Alignment too large.");
Bill Wendling03198882013-01-04 23:27:34 +00001415
Benjamin Kramer3f213e72013-02-18 12:09:51 +00001416 Attrs[Attribute::Alignment] = true;
Bill Wendling03198882013-01-04 23:27:34 +00001417 Alignment = Align;
Bill Wendlingda3f9d82012-10-14 03:58:29 +00001418 return *this;
Chris Lattner50ee9dd2008-01-02 23:42:30 +00001419}
1420
Bill Wendling03198882013-01-04 23:27:34 +00001421AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
1422 // Default alignment, allow the target to define how to align it.
1423 if (Align == 0) return *this;
1424
1425 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1426 assert(Align <= 0x100 && "Alignment too large.");
1427
Benjamin Kramer3f213e72013-02-18 12:09:51 +00001428 Attrs[Attribute::StackAlignment] = true;
Bill Wendling03198882013-01-04 23:27:34 +00001429 StackAlignment = Align;
1430 return *this;
1431}
1432
Hal Finkel11af4b42014-07-18 15:51:28 +00001433AttrBuilder &AttrBuilder::addDereferenceableAttr(uint64_t Bytes) {
1434 if (Bytes == 0) return *this;
1435
1436 Attrs[Attribute::Dereferenceable] = true;
1437 DerefBytes = Bytes;
1438 return *this;
1439}
1440
Sanjoy Das5ff59072015-04-16 20:29:50 +00001441AttrBuilder &AttrBuilder::addDereferenceableOrNullAttr(uint64_t Bytes) {
1442 if (Bytes == 0)
1443 return *this;
1444
1445 Attrs[Attribute::DereferenceableOrNull] = true;
1446 DerefOrNullBytes = Bytes;
1447 return *this;
1448}
1449
George Burgess IV274105b2016-04-12 01:05:35 +00001450AttrBuilder &AttrBuilder::addAllocSizeAttr(unsigned ElemSize,
1451 const Optional<unsigned> &NumElems) {
1452 return addAllocSizeAttrFromRawRepr(packAllocSizeArgs(ElemSize, NumElems));
1453}
1454
1455AttrBuilder &AttrBuilder::addAllocSizeAttrFromRawRepr(uint64_t RawArgs) {
1456 // (0, 0) is our "not present" value, so we need to check for it here.
1457 assert(RawArgs && "Invalid allocsize arguments -- given allocsize(0, 0)");
1458
1459 Attrs[Attribute::AllocSize] = true;
1460 // Reuse existing machinery to store this as a single 64-bit integer so we can
1461 // save a few bytes over using a pair<unsigned, Optional<unsigned>>.
1462 AllocSizeArgs = RawArgs;
1463 return *this;
1464}
1465
Bill Wendling85df6b42013-02-06 01:16:00 +00001466AttrBuilder &AttrBuilder::merge(const AttrBuilder &B) {
1467 // FIXME: What if both have alignments, but they don't match?!
1468 if (!Alignment)
1469 Alignment = B.Alignment;
1470
1471 if (!StackAlignment)
1472 StackAlignment = B.StackAlignment;
1473
Hal Finkel11af4b42014-07-18 15:51:28 +00001474 if (!DerefBytes)
1475 DerefBytes = B.DerefBytes;
1476
Pete Cooperc58f23e2015-05-06 23:19:43 +00001477 if (!DerefOrNullBytes)
1478 DerefOrNullBytes = B.DerefOrNullBytes;
1479
George Burgess IV274105b2016-04-12 01:05:35 +00001480 if (!AllocSizeArgs)
1481 AllocSizeArgs = B.AllocSizeArgs;
1482
Benjamin Kramerc835b8c2013-02-16 19:13:18 +00001483 Attrs |= B.Attrs;
Bill Wendling85df6b42013-02-06 01:16:00 +00001484
Pete Cooperc58f23e2015-05-06 23:19:43 +00001485 for (auto I : B.td_attrs())
1486 TargetDepAttrs[I.first] = I.second;
Bill Wendling85df6b42013-02-06 01:16:00 +00001487
1488 return *this;
1489}
1490
Pete Cooperc58f23e2015-05-06 23:19:43 +00001491AttrBuilder &AttrBuilder::remove(const AttrBuilder &B) {
1492 // FIXME: What if both have alignments, but they don't match?!
1493 if (B.Alignment)
1494 Alignment = 0;
1495
1496 if (B.StackAlignment)
1497 StackAlignment = 0;
1498
1499 if (B.DerefBytes)
1500 DerefBytes = 0;
1501
1502 if (B.DerefOrNullBytes)
1503 DerefOrNullBytes = 0;
1504
George Burgess IV274105b2016-04-12 01:05:35 +00001505 if (B.AllocSizeArgs)
1506 AllocSizeArgs = 0;
1507
Pete Cooperc58f23e2015-05-06 23:19:43 +00001508 Attrs &= ~B.Attrs;
1509
1510 for (auto I : B.td_attrs())
1511 TargetDepAttrs.erase(I.first);
1512
1513 return *this;
1514}
1515
1516bool AttrBuilder::overlaps(const AttrBuilder &B) const {
1517 // First check if any of the target independent attributes overlap.
1518 if ((Attrs & B.Attrs).any())
1519 return true;
1520
1521 // Then check if any target dependent ones do.
Sean Silva0b39c0f2017-02-22 06:34:04 +00001522 for (const auto &I : td_attrs())
Pete Cooperc58f23e2015-05-06 23:19:43 +00001523 if (B.contains(I.first))
1524 return true;
1525
1526 return false;
1527}
1528
Bill Wendlingc342d9d2013-02-06 01:33:42 +00001529bool AttrBuilder::contains(StringRef A) const {
1530 return TargetDepAttrs.find(A) != TargetDepAttrs.end();
1531}
1532
Bill Wendling702cc912012-10-15 20:35:56 +00001533bool AttrBuilder::hasAttributes() const {
Benjamin Kramer3f213e72013-02-18 12:09:51 +00001534 return !Attrs.none() || !TargetDepAttrs.empty();
Bill Wendlingf385f4c2012-10-08 23:27:46 +00001535}
Bill Wendling60507d52013-01-04 20:54:35 +00001536
Reid Kleckner331b9af2017-04-28 18:37:16 +00001537bool AttrBuilder::hasAttributes(AttributeList AL, uint64_t Index) const {
1538 AttributeSet AS = AL.getAttributes(Index);
Bill Wendlingbdcbccc2013-02-02 00:42:06 +00001539
Eugene Zelenkoae117792018-03-30 00:47:31 +00001540 for (const auto Attr : AS) {
Hal Finkeld0261682014-07-18 06:51:55 +00001541 if (Attr.isEnumAttribute() || Attr.isIntAttribute()) {
Reid Kleckner331b9af2017-04-28 18:37:16 +00001542 if (contains(Attr.getKindAsEnum()))
Bill Wendling74fe8252013-02-12 07:56:49 +00001543 return true;
1544 } else {
1545 assert(Attr.isStringAttribute() && "Invalid attribute kind!");
Reid Kleckner331b9af2017-04-28 18:37:16 +00001546 return contains(Attr.getKindAsString());
Bill Wendling74fe8252013-02-12 07:56:49 +00001547 }
1548 }
Bill Wendlingbdcbccc2013-02-02 00:42:06 +00001549
1550 return false;
Bill Wendling8831c062012-10-09 00:01:21 +00001551}
Bill Wendling60507d52013-01-04 20:54:35 +00001552
Bill Wendling702cc912012-10-15 20:35:56 +00001553bool AttrBuilder::hasAlignmentAttr() const {
Bill Wendling03198882013-01-04 23:27:34 +00001554 return Alignment != 0;
Bill Wendlingf385f4c2012-10-08 23:27:46 +00001555}
1556
Bill Wendlingc22f4aa2013-01-29 00:34:06 +00001557bool AttrBuilder::operator==(const AttrBuilder &B) {
Benjamin Kramerc835b8c2013-02-16 19:13:18 +00001558 if (Attrs != B.Attrs)
1559 return false;
Bill Wendlingc342d9d2013-02-06 01:33:42 +00001560
1561 for (td_const_iterator I = TargetDepAttrs.begin(),
1562 E = TargetDepAttrs.end(); I != E; ++I)
1563 if (B.TargetDepAttrs.find(I->first) == B.TargetDepAttrs.end())
1564 return false;
1565
Hal Finkel11af4b42014-07-18 15:51:28 +00001566 return Alignment == B.Alignment && StackAlignment == B.StackAlignment &&
1567 DerefBytes == B.DerefBytes;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +00001568}
1569
Bill Wendling8e47daf2013-01-25 23:09:36 +00001570//===----------------------------------------------------------------------===//
1571// AttributeFuncs Function Defintions
1572//===----------------------------------------------------------------------===//
1573
Adrian Prantl26b584c2018-05-01 15:54:18 +00001574/// Which attributes cannot be applied to a type.
Craig Topper84bbcfe2015-08-01 22:20:21 +00001575AttrBuilder AttributeFuncs::typeIncompatible(Type *Ty) {
Bill Wendling8e47daf2013-01-25 23:09:36 +00001576 AttrBuilder Incompatible;
1577
1578 if (!Ty->isIntegerTy())
1579 // Attribute that only apply to integers.
1580 Incompatible.addAttribute(Attribute::SExt)
1581 .addAttribute(Attribute::ZExt);
1582
1583 if (!Ty->isPointerTy())
1584 // Attribute that only apply to pointers.
1585 Incompatible.addAttribute(Attribute::ByVal)
1586 .addAttribute(Attribute::Nest)
1587 .addAttribute(Attribute::NoAlias)
1588 .addAttribute(Attribute::NoCapture)
Nick Lewyckyfe47ebf2014-05-20 01:23:40 +00001589 .addAttribute(Attribute::NonNull)
Hal Finkel11af4b42014-07-18 15:51:28 +00001590 .addDereferenceableAttr(1) // the int here is ignored
Sanjoy Das5ff59072015-04-16 20:29:50 +00001591 .addDereferenceableOrNullAttr(1) // the int here is ignored
Nick Lewyckydc897372013-07-06 00:29:58 +00001592 .addAttribute(Attribute::ReadNone)
1593 .addAttribute(Attribute::ReadOnly)
Reid Kleckner4b70bfc2013-12-19 02:14:12 +00001594 .addAttribute(Attribute::StructRet)
1595 .addAttribute(Attribute::InAlloca);
Bill Wendling8e47daf2013-01-25 23:09:36 +00001596
Pete Coopera7574632015-05-06 23:19:56 +00001597 return Incompatible;
Bill Wendling8e47daf2013-01-25 23:09:36 +00001598}
Akira Hatanaka37de9d02015-12-22 23:57:37 +00001599
1600template<typename AttrClass>
1601static bool isEqual(const Function &Caller, const Function &Callee) {
1602 return Caller.getFnAttribute(AttrClass::getKind()) ==
1603 Callee.getFnAttribute(AttrClass::getKind());
1604}
1605
Adrian Prantl26b584c2018-05-01 15:54:18 +00001606/// Compute the logical AND of the attributes of the caller and the
Akira Hatanaka37de9d02015-12-22 23:57:37 +00001607/// callee.
1608///
1609/// This function sets the caller's attribute to false if the callee's attribute
1610/// is false.
1611template<typename AttrClass>
1612static void setAND(Function &Caller, const Function &Callee) {
1613 if (AttrClass::isSet(Caller, AttrClass::getKind()) &&
1614 !AttrClass::isSet(Callee, AttrClass::getKind()))
1615 AttrClass::set(Caller, AttrClass::getKind(), false);
1616}
1617
Adrian Prantl26b584c2018-05-01 15:54:18 +00001618/// Compute the logical OR of the attributes of the caller and the
Akira Hatanaka37de9d02015-12-22 23:57:37 +00001619/// callee.
1620///
1621/// This function sets the caller's attribute to true if the callee's attribute
1622/// is true.
1623template<typename AttrClass>
1624static void setOR(Function &Caller, const Function &Callee) {
1625 if (!AttrClass::isSet(Caller, AttrClass::getKind()) &&
1626 AttrClass::isSet(Callee, AttrClass::getKind()))
1627 AttrClass::set(Caller, AttrClass::getKind(), true);
1628}
1629
Adrian Prantl26b584c2018-05-01 15:54:18 +00001630/// If the inlined function had a higher stack protection level than the
Akira Hatanaka37de9d02015-12-22 23:57:37 +00001631/// calling function, then bump up the caller's stack protection level.
1632static void adjustCallerSSPLevel(Function &Caller, const Function &Callee) {
1633 // If upgrading the SSP attribute, clear out the old SSP Attributes first.
1634 // Having multiple SSP attributes doesn't actually hurt, but it adds useless
1635 // clutter to the IR.
Reid Klecknerdac74872017-05-02 22:07:37 +00001636 AttrBuilder OldSSPAttr;
1637 OldSSPAttr.addAttribute(Attribute::StackProtect)
1638 .addAttribute(Attribute::StackProtectStrong)
1639 .addAttribute(Attribute::StackProtectReq);
Akira Hatanaka37de9d02015-12-22 23:57:37 +00001640
Evgeniy Stepanov00cd59a2016-04-11 22:27:48 +00001641 if (Callee.hasFnAttribute(Attribute::StackProtectReq)) {
Reid Kleckner67077702017-03-21 16:57:19 +00001642 Caller.removeAttributes(AttributeList::FunctionIndex, OldSSPAttr);
Akira Hatanaka37de9d02015-12-22 23:57:37 +00001643 Caller.addFnAttr(Attribute::StackProtectReq);
1644 } else if (Callee.hasFnAttribute(Attribute::StackProtectStrong) &&
Akira Hatanaka37de9d02015-12-22 23:57:37 +00001645 !Caller.hasFnAttribute(Attribute::StackProtectReq)) {
Reid Kleckner67077702017-03-21 16:57:19 +00001646 Caller.removeAttributes(AttributeList::FunctionIndex, OldSSPAttr);
Akira Hatanaka37de9d02015-12-22 23:57:37 +00001647 Caller.addFnAttr(Attribute::StackProtectStrong);
1648 } else if (Callee.hasFnAttribute(Attribute::StackProtect) &&
Akira Hatanaka37de9d02015-12-22 23:57:37 +00001649 !Caller.hasFnAttribute(Attribute::StackProtectReq) &&
1650 !Caller.hasFnAttribute(Attribute::StackProtectStrong))
1651 Caller.addFnAttr(Attribute::StackProtect);
1652}
1653
Adrian Prantl26b584c2018-05-01 15:54:18 +00001654/// If the inlined function required stack probes, then ensure that
whitequark4c34d0af2017-06-21 18:46:50 +00001655/// the calling function has those too.
1656static void adjustCallerStackProbes(Function &Caller, const Function &Callee) {
whitequarke4b18902017-06-22 23:22:36 +00001657 if (!Caller.hasFnAttribute("probe-stack") &&
1658 Callee.hasFnAttribute("probe-stack")) {
1659 Caller.addFnAttr(Callee.getFnAttribute("probe-stack"));
1660 }
1661}
1662
Adrian Prantl26b584c2018-05-01 15:54:18 +00001663/// If the inlined function defines the size of guard region
whitequarke4b18902017-06-22 23:22:36 +00001664/// on the stack, then ensure that the calling function defines a guard region
1665/// that is no larger.
1666static void
1667adjustCallerStackProbeSize(Function &Caller, const Function &Callee) {
1668 if (Callee.hasFnAttribute("stack-probe-size")) {
1669 uint64_t CalleeStackProbeSize;
1670 Callee.getFnAttribute("stack-probe-size")
1671 .getValueAsString()
1672 .getAsInteger(0, CalleeStackProbeSize);
1673 if (Caller.hasFnAttribute("stack-probe-size")) {
1674 uint64_t CallerStackProbeSize;
1675 Caller.getFnAttribute("stack-probe-size")
1676 .getValueAsString()
1677 .getAsInteger(0, CallerStackProbeSize);
1678 if (CallerStackProbeSize > CalleeStackProbeSize) {
1679 Caller.addFnAttr(Callee.getFnAttribute("stack-probe-size"));
1680 }
1681 } else {
1682 Caller.addFnAttr(Callee.getFnAttribute("stack-probe-size"));
1683 }
1684 }
whitequark4c34d0af2017-06-21 18:46:50 +00001685}
1686
Craig Toppere944ca82018-07-24 18:49:00 +00001687/// If the inlined function defines a min legal vector width, then ensure
Craig Topper75aaec52018-11-29 07:27:38 +00001688/// the calling function has the same or larger min legal vector width. If the
1689/// caller has the attribute, but the callee doesn't, we need to remove the
1690/// attribute from the caller since we can't make any guarantees about the
1691/// caller's requirements.
1692/// This function is called after the inlining decision has been made so we have
1693/// to merge the attribute this way. Heuristics that would use
Craig Toppere944ca82018-07-24 18:49:00 +00001694/// min-legal-vector-width to determine inline compatibility would need to be
1695/// handled as part of inline cost analysis.
1696static void
1697adjustMinLegalVectorWidth(Function &Caller, const Function &Callee) {
Craig Topper75aaec52018-11-29 07:27:38 +00001698 if (Caller.hasFnAttribute("min-legal-vector-width")) {
1699 if (Callee.hasFnAttribute("min-legal-vector-width")) {
Craig Toppere944ca82018-07-24 18:49:00 +00001700 uint64_t CallerVectorWidth;
1701 Caller.getFnAttribute("min-legal-vector-width")
1702 .getValueAsString()
1703 .getAsInteger(0, CallerVectorWidth);
Craig Topper75aaec52018-11-29 07:27:38 +00001704 uint64_t CalleeVectorWidth;
1705 Callee.getFnAttribute("min-legal-vector-width")
1706 .getValueAsString()
1707 .getAsInteger(0, CalleeVectorWidth);
1708 if (CallerVectorWidth < CalleeVectorWidth)
Craig Toppere944ca82018-07-24 18:49:00 +00001709 Caller.addFnAttr(Callee.getFnAttribute("min-legal-vector-width"));
Craig Toppere944ca82018-07-24 18:49:00 +00001710 } else {
Craig Topper75aaec52018-11-29 07:27:38 +00001711 // If the callee doesn't have the attribute then we don't know anything
1712 // and must drop the attribute from the caller.
1713 Caller.removeFnAttr("min-legal-vector-width");
Craig Toppere944ca82018-07-24 18:49:00 +00001714 }
1715 }
1716}
1717
Manoj Guptab22576f2018-07-30 19:33:53 +00001718/// If the inlined function has "null-pointer-is-valid=true" attribute,
1719/// set this attribute in the caller post inlining.
1720static void
1721adjustNullPointerValidAttr(Function &Caller, const Function &Callee) {
1722 if (Callee.nullPointerIsDefined() && !Caller.nullPointerIsDefined()) {
1723 Caller.addFnAttr(Callee.getFnAttribute("null-pointer-is-valid"));
1724 }
1725}
1726
Akira Hatanaka37de9d02015-12-22 23:57:37 +00001727#define GET_ATTR_COMPAT_FUNC
1728#include "AttributesCompatFunc.inc"
1729
1730bool AttributeFuncs::areInlineCompatible(const Function &Caller,
1731 const Function &Callee) {
1732 return hasCompatibleFnAttrs(Caller, Callee);
1733}
1734
Akira Hatanaka37de9d02015-12-22 23:57:37 +00001735void AttributeFuncs::mergeAttributesForInlining(Function &Caller,
1736 const Function &Callee) {
1737 mergeFnAttrs(Caller, Callee);
1738}