blob: 1a060ba2768c57efe7e42edb69da97a5e8e0791a [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>
41#include <map>
42#include <string>
43#include <tuple>
44#include <utility>
45
Chris Lattner3e13b8c2008-01-02 23:42:30 +000046using namespace llvm;
47
Chris Lattner8a923e72008-03-12 17:45:29 +000048//===----------------------------------------------------------------------===//
Bill Wendling7707c5a2013-01-29 00:48:16 +000049// Attribute Construction Methods
Chris Lattner8a923e72008-03-12 17:45:29 +000050//===----------------------------------------------------------------------===//
Chris Lattnerd0e1f102008-01-03 00:10:22 +000051
George Burgess IV278199f2016-04-12 01:05:35 +000052// allocsize has two integer arguments, but because they're both 32 bits, we can
53// pack them into one 64-bit value, at the cost of making said value
54// nonsensical.
55//
56// In order to do this, we need to reserve one value of the second (optional)
57// allocsize argument to signify "not present."
George Burgess IV381fc0e2016-08-25 01:05:08 +000058static const unsigned AllocSizeNumElemsNotPresent = -1;
George Burgess IV278199f2016-04-12 01:05:35 +000059
60static uint64_t packAllocSizeArgs(unsigned ElemSizeArg,
61 const Optional<unsigned> &NumElemsArg) {
62 assert((!NumElemsArg.hasValue() ||
63 *NumElemsArg != AllocSizeNumElemsNotPresent) &&
64 "Attempting to pack a reserved value");
65
66 return uint64_t(ElemSizeArg) << 32 |
67 NumElemsArg.getValueOr(AllocSizeNumElemsNotPresent);
68}
69
70static std::pair<unsigned, Optional<unsigned>>
71unpackAllocSizeArgs(uint64_t Num) {
72 unsigned NumElems = Num & std::numeric_limits<unsigned>::max();
73 unsigned ElemSizeArg = Num >> 32;
74
75 Optional<unsigned> NumElemsArg;
76 if (NumElems != AllocSizeNumElemsNotPresent)
77 NumElemsArg = NumElems;
78 return std::make_pair(ElemSizeArg, NumElemsArg);
79}
80
Matt Arsenaulte1cef6e2014-09-03 23:24:31 +000081Attribute Attribute::get(LLVMContext &Context, Attribute::AttrKind Kind,
82 uint64_t Val) {
83 LLVMContextImpl *pImpl = Context.pImpl;
84 FoldingSetNodeID ID;
85 ID.AddInteger(Kind);
Matt Arsenaultfb4308e2014-09-03 23:38:05 +000086 if (Val) ID.AddInteger(Val);
Matt Arsenaulte1cef6e2014-09-03 23:24:31 +000087
88 void *InsertPoint;
89 AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
90
91 if (!PA) {
92 // If we didn't find any existing attributes of the same shape then create a
93 // new one and insert it.
Matt Arsenaultfb4308e2014-09-03 23:38:05 +000094 if (!Val)
95 PA = new EnumAttributeImpl(Kind);
96 else
97 PA = new IntAttributeImpl(Kind, Val);
Bill Wendling3f12ac22013-02-05 22:37:24 +000098 pImpl->AttrsSet.InsertNode(PA, InsertPoint);
99 }
100
101 // Return the Attribute that we found or created.
102 return Attribute(PA);
103}
104
105Attribute Attribute::get(LLVMContext &Context, StringRef Kind, StringRef Val) {
106 LLVMContextImpl *pImpl = Context.pImpl;
107 FoldingSetNodeID ID;
108 ID.AddString(Kind);
109 if (!Val.empty()) ID.AddString(Val);
Bill Wendling73ea2de2012-10-08 21:47:17 +0000110
111 void *InsertPoint;
Bill Wendling4607f4b2012-12-20 01:36:59 +0000112 AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
Bill Wendling73ea2de2012-10-08 21:47:17 +0000113
114 if (!PA) {
115 // If we didn't find any existing attributes of the same shape then create a
116 // new one and insert it.
Benjamin Kramer741146b2013-07-11 12:13:16 +0000117 PA = new StringAttributeImpl(Kind, Val);
Bill Wendling73ea2de2012-10-08 21:47:17 +0000118 pImpl->AttrsSet.InsertNode(PA, InsertPoint);
119 }
120
Bill Wendlingb9c5b1a2013-02-05 08:09:32 +0000121 // Return the Attribute that we found or created.
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000122 return Attribute(PA);
Bill Wendling73ea2de2012-10-08 21:47:17 +0000123}
124
Bill Wendling4bbe9db2013-01-27 22:43:04 +0000125Attribute Attribute::getWithAlignment(LLVMContext &Context, uint64_t Align) {
Bill Wendling1c7cc8a2013-01-31 23:16:25 +0000126 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
127 assert(Align <= 0x40000000 && "Alignment too large.");
Bill Wendling3f12ac22013-02-05 22:37:24 +0000128 return get(Context, Alignment, Align);
Bill Wendling4bbe9db2013-01-27 22:43:04 +0000129}
130
131Attribute Attribute::getWithStackAlignment(LLVMContext &Context,
132 uint64_t Align) {
Bill Wendling1c7cc8a2013-01-31 23:16:25 +0000133 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
134 assert(Align <= 0x100 && "Alignment too large.");
Bill Wendling3f12ac22013-02-05 22:37:24 +0000135 return get(Context, StackAlignment, Align);
Bill Wendling4bbe9db2013-01-27 22:43:04 +0000136}
137
Hal Finkelb0407ba2014-07-18 15:51:28 +0000138Attribute Attribute::getWithDereferenceableBytes(LLVMContext &Context,
139 uint64_t Bytes) {
140 assert(Bytes && "Bytes must be non-zero.");
141 return get(Context, Dereferenceable, Bytes);
142}
143
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000144Attribute Attribute::getWithDereferenceableOrNullBytes(LLVMContext &Context,
145 uint64_t Bytes) {
146 assert(Bytes && "Bytes must be non-zero.");
147 return get(Context, DereferenceableOrNull, Bytes);
148}
149
George Burgess IV278199f2016-04-12 01:05:35 +0000150Attribute
151Attribute::getWithAllocSizeArgs(LLVMContext &Context, unsigned ElemSizeArg,
152 const Optional<unsigned> &NumElemsArg) {
153 assert(!(ElemSizeArg == 0 && NumElemsArg && *NumElemsArg == 0) &&
154 "Invalid allocsize arguments -- given allocsize(0, 0)");
155 return get(Context, AllocSize, packAllocSizeArgs(ElemSizeArg, NumElemsArg));
156}
157
Bill Wendling7707c5a2013-01-29 00:48:16 +0000158//===----------------------------------------------------------------------===//
159// Attribute Accessor Methods
160//===----------------------------------------------------------------------===//
161
Bill Wendling3f12ac22013-02-05 22:37:24 +0000162bool Attribute::isEnumAttribute() const {
163 return pImpl && pImpl->isEnumAttribute();
164}
165
Hal Finkele15442c2014-07-18 06:51:55 +0000166bool Attribute::isIntAttribute() const {
167 return pImpl && pImpl->isIntAttribute();
Bill Wendling3f12ac22013-02-05 22:37:24 +0000168}
169
170bool Attribute::isStringAttribute() const {
171 return pImpl && pImpl->isStringAttribute();
172}
173
174Attribute::AttrKind Attribute::getKindAsEnum() const {
Bill Wendling440e9d82013-07-25 00:34:29 +0000175 if (!pImpl) return None;
Hal Finkele15442c2014-07-18 06:51:55 +0000176 assert((isEnumAttribute() || isIntAttribute()) &&
Bill Wendling3f12ac22013-02-05 22:37:24 +0000177 "Invalid attribute type to get the kind as an enum!");
George Burgess IV500d3032015-12-16 05:21:02 +0000178 return pImpl->getKindAsEnum();
Bill Wendling3f12ac22013-02-05 22:37:24 +0000179}
180
181uint64_t Attribute::getValueAsInt() const {
Bill Wendling440e9d82013-07-25 00:34:29 +0000182 if (!pImpl) return 0;
Hal Finkele15442c2014-07-18 06:51:55 +0000183 assert(isIntAttribute() &&
184 "Expected the attribute to be an integer attribute!");
George Burgess IV500d3032015-12-16 05:21:02 +0000185 return pImpl->getValueAsInt();
Bill Wendling3f12ac22013-02-05 22:37:24 +0000186}
187
188StringRef Attribute::getKindAsString() const {
Bill Wendling440e9d82013-07-25 00:34:29 +0000189 if (!pImpl) return StringRef();
Bill Wendling3f12ac22013-02-05 22:37:24 +0000190 assert(isStringAttribute() &&
191 "Invalid attribute type to get the kind as a string!");
George Burgess IV500d3032015-12-16 05:21:02 +0000192 return pImpl->getKindAsString();
Bill Wendling3f12ac22013-02-05 22:37:24 +0000193}
194
195StringRef Attribute::getValueAsString() const {
Bill Wendling440e9d82013-07-25 00:34:29 +0000196 if (!pImpl) return StringRef();
Bill Wendling3f12ac22013-02-05 22:37:24 +0000197 assert(isStringAttribute() &&
198 "Invalid attribute type to get the value as a string!");
George Burgess IV500d3032015-12-16 05:21:02 +0000199 return pImpl->getValueAsString();
Bill Wendling3f12ac22013-02-05 22:37:24 +0000200}
201
Bill Wendlingae89a0f2013-02-05 23:48:36 +0000202bool Attribute::hasAttribute(AttrKind Kind) const {
203 return (pImpl && pImpl->hasAttribute(Kind)) || (!pImpl && Kind == None);
204}
205
206bool Attribute::hasAttribute(StringRef Kind) const {
207 if (!isStringAttribute()) return false;
208 return pImpl && pImpl->hasAttribute(Kind);
Bill Wendling03eefb32013-01-29 20:45:34 +0000209}
210
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000211unsigned Attribute::getAlignment() const {
Bill Wendlingc79cdff2013-02-01 01:04:27 +0000212 assert(hasAttribute(Attribute::Alignment) &&
213 "Trying to get alignment from non-alignment attribute!");
Bill Wendling3f12ac22013-02-05 22:37:24 +0000214 return pImpl->getValueAsInt();
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000215}
216
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000217unsigned Attribute::getStackAlignment() const {
Bill Wendlingc79cdff2013-02-01 01:04:27 +0000218 assert(hasAttribute(Attribute::StackAlignment) &&
219 "Trying to get alignment from non-alignment attribute!");
Bill Wendling3f12ac22013-02-05 22:37:24 +0000220 return pImpl->getValueAsInt();
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000221}
222
Hal Finkelb0407ba2014-07-18 15:51:28 +0000223uint64_t Attribute::getDereferenceableBytes() const {
224 assert(hasAttribute(Attribute::Dereferenceable) &&
225 "Trying to get dereferenceable bytes from "
226 "non-dereferenceable attribute!");
227 return pImpl->getValueAsInt();
228}
229
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000230uint64_t Attribute::getDereferenceableOrNullBytes() const {
231 assert(hasAttribute(Attribute::DereferenceableOrNull) &&
232 "Trying to get dereferenceable bytes from "
233 "non-dereferenceable attribute!");
234 return pImpl->getValueAsInt();
235}
236
George Burgess IV278199f2016-04-12 01:05:35 +0000237std::pair<unsigned, Optional<unsigned>> Attribute::getAllocSizeArgs() const {
238 assert(hasAttribute(Attribute::AllocSize) &&
239 "Trying to get allocsize args from non-allocsize attribute");
240 return unpackAllocSizeArgs(pImpl->getValueAsInt());
241}
242
Bill Wendling829b4782013-02-11 08:43:33 +0000243std::string Attribute::getAsString(bool InAttrGrp) const {
Bill Wendling9c2eba92013-01-31 20:59:05 +0000244 if (!pImpl) return "";
245
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000246 if (hasAttribute(Attribute::SanitizeAddress))
247 return "sanitize_address";
Evgeniy Stepanovc667c1f2017-12-09 00:21:41 +0000248 if (hasAttribute(Attribute::SanitizeHWAddress))
249 return "sanitize_hwaddress";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000250 if (hasAttribute(Attribute::AlwaysInline))
251 return "alwaysinline";
Igor Laevsky39d662f2015-07-11 10:30:36 +0000252 if (hasAttribute(Attribute::ArgMemOnly))
253 return "argmemonly";
Michael Gottesman41748d72013-06-27 00:25:01 +0000254 if (hasAttribute(Attribute::Builtin))
255 return "builtin";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000256 if (hasAttribute(Attribute::ByVal))
257 return "byval";
Owen Anderson85fa7d52015-05-26 23:48:40 +0000258 if (hasAttribute(Attribute::Convergent))
259 return "convergent";
Manman Ren9bfd0d02016-04-01 21:41:15 +0000260 if (hasAttribute(Attribute::SwiftError))
261 return "swifterror";
Manman Renf46262e2016-03-29 17:37:21 +0000262 if (hasAttribute(Attribute::SwiftSelf))
263 return "swiftself";
Vaivaswatha Nagarajfb3f4902015-12-16 16:16:19 +0000264 if (hasAttribute(Attribute::InaccessibleMemOnly))
265 return "inaccessiblememonly";
266 if (hasAttribute(Attribute::InaccessibleMemOrArgMemOnly))
267 return "inaccessiblemem_or_argmemonly";
Reid Klecknera534a382013-12-19 02:14:12 +0000268 if (hasAttribute(Attribute::InAlloca))
269 return "inalloca";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000270 if (hasAttribute(Attribute::InlineHint))
271 return "inlinehint";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000272 if (hasAttribute(Attribute::InReg))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000273 return "inreg";
Tom Roeder44cb65f2014-06-05 19:29:43 +0000274 if (hasAttribute(Attribute::JumpTable))
275 return "jumptable";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000276 if (hasAttribute(Attribute::MinSize))
277 return "minsize";
278 if (hasAttribute(Attribute::Naked))
279 return "naked";
280 if (hasAttribute(Attribute::Nest))
281 return "nest";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000282 if (hasAttribute(Attribute::NoAlias))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000283 return "noalias";
Bill Wendling09bd1f72013-02-22 00:12:35 +0000284 if (hasAttribute(Attribute::NoBuiltin))
285 return "nobuiltin";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000286 if (hasAttribute(Attribute::NoCapture))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000287 return "nocapture";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000288 if (hasAttribute(Attribute::NoDuplicate))
289 return "noduplicate";
290 if (hasAttribute(Attribute::NoImplicitFloat))
291 return "noimplicitfloat";
292 if (hasAttribute(Attribute::NoInline))
293 return "noinline";
294 if (hasAttribute(Attribute::NonLazyBind))
295 return "nonlazybind";
Nick Lewyckyd52b1522014-05-20 01:23:40 +0000296 if (hasAttribute(Attribute::NonNull))
297 return "nonnull";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000298 if (hasAttribute(Attribute::NoRedZone))
299 return "noredzone";
300 if (hasAttribute(Attribute::NoReturn))
301 return "noreturn";
Oren Ben Simhonfdd72fd2018-03-17 13:29:46 +0000302 if (hasAttribute(Attribute::NoCfCheck))
303 return "nocf_check";
James Molloye6f87ca2015-11-06 10:32:53 +0000304 if (hasAttribute(Attribute::NoRecurse))
305 return "norecurse";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000306 if (hasAttribute(Attribute::NoUnwind))
307 return "nounwind";
Matt Morehouse236cdaf2018-03-22 17:07:51 +0000308 if (hasAttribute(Attribute::OptForFuzzing))
309 return "optforfuzzing";
Andrea Di Biagio377496b2013-08-23 11:53:55 +0000310 if (hasAttribute(Attribute::OptimizeNone))
311 return "optnone";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000312 if (hasAttribute(Attribute::OptimizeForSize))
313 return "optsize";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000314 if (hasAttribute(Attribute::ReadNone))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000315 return "readnone";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000316 if (hasAttribute(Attribute::ReadOnly))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000317 return "readonly";
Nicolai Haehnle84c9f992016-07-04 08:01:29 +0000318 if (hasAttribute(Attribute::WriteOnly))
319 return "writeonly";
Stephen Linb8bd2322013-04-20 05:14:40 +0000320 if (hasAttribute(Attribute::Returned))
321 return "returned";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000322 if (hasAttribute(Attribute::ReturnsTwice))
323 return "returns_twice";
324 if (hasAttribute(Attribute::SExt))
325 return "signext";
Matt Arsenaultb19b57e2017-04-28 20:25:27 +0000326 if (hasAttribute(Attribute::Speculatable))
327 return "speculatable";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000328 if (hasAttribute(Attribute::StackProtect))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000329 return "ssp";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000330 if (hasAttribute(Attribute::StackProtectReq))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000331 return "sspreq";
Bill Wendlingd154e2832013-01-23 06:41:41 +0000332 if (hasAttribute(Attribute::StackProtectStrong))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000333 return "sspstrong";
Peter Collingbourne82437bf2015-06-15 21:07:11 +0000334 if (hasAttribute(Attribute::SafeStack))
335 return "safestack";
Andrew Kaylor53a5fbb2017-08-14 21:15:13 +0000336 if (hasAttribute(Attribute::StrictFP))
337 return "strictfp";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000338 if (hasAttribute(Attribute::StructRet))
339 return "sret";
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000340 if (hasAttribute(Attribute::SanitizeThread))
341 return "sanitize_thread";
342 if (hasAttribute(Attribute::SanitizeMemory))
343 return "sanitize_memory";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000344 if (hasAttribute(Attribute::UWTable))
345 return "uwtable";
346 if (hasAttribute(Attribute::ZExt))
347 return "zeroext";
Diego Novilloc6399532013-05-24 12:26:52 +0000348 if (hasAttribute(Attribute::Cold))
349 return "cold";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000350
351 // FIXME: These should be output like this:
352 //
353 // align=4
354 // alignstack=8
355 //
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000356 if (hasAttribute(Attribute::Alignment)) {
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000357 std::string Result;
Bill Wendling829b4782013-02-11 08:43:33 +0000358 Result += "align";
359 Result += (InAttrGrp) ? "=" : " ";
Bill Wendling3f12ac22013-02-05 22:37:24 +0000360 Result += utostr(getValueAsInt());
361 return Result;
362 }
Bill Wendling829b4782013-02-11 08:43:33 +0000363
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000364 auto AttrWithBytesToString = [&](const char *Name) {
Bill Wendling3f12ac22013-02-05 22:37:24 +0000365 std::string Result;
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000366 Result += Name;
Bill Wendling829b4782013-02-11 08:43:33 +0000367 if (InAttrGrp) {
368 Result += "=";
369 Result += utostr(getValueAsInt());
370 } else {
371 Result += "(";
372 Result += utostr(getValueAsInt());
373 Result += ")";
374 }
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000375 return Result;
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000376 };
Bill Wendling9c2eba92013-01-31 20:59:05 +0000377
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000378 if (hasAttribute(Attribute::StackAlignment))
379 return AttrWithBytesToString("alignstack");
380
381 if (hasAttribute(Attribute::Dereferenceable))
382 return AttrWithBytesToString("dereferenceable");
383
384 if (hasAttribute(Attribute::DereferenceableOrNull))
385 return AttrWithBytesToString("dereferenceable_or_null");
Hal Finkelb0407ba2014-07-18 15:51:28 +0000386
George Burgess IV278199f2016-04-12 01:05:35 +0000387 if (hasAttribute(Attribute::AllocSize)) {
388 unsigned ElemSize;
389 Optional<unsigned> NumElems;
390 std::tie(ElemSize, NumElems) = getAllocSizeArgs();
391
392 std::string Result = "allocsize(";
393 Result += utostr(ElemSize);
394 if (NumElems.hasValue()) {
395 Result += ',';
396 Result += utostr(*NumElems);
397 }
398 Result += ')';
399 return Result;
400 }
401
Bill Wendling9c2eba92013-01-31 20:59:05 +0000402 // Convert target-dependent attributes to strings of the form:
403 //
404 // "kind"
405 // "kind" = "value"
Bill Wendling9c2eba92013-01-31 20:59:05 +0000406 //
Bill Wendling3f12ac22013-02-05 22:37:24 +0000407 if (isStringAttribute()) {
Bill Wendling9c2eba92013-01-31 20:59:05 +0000408 std::string Result;
Yaron Keren075759a2015-03-30 15:42:36 +0000409 Result += (Twine('"') + getKindAsString() + Twine('"')).str();
Bill Wendling9c2eba92013-01-31 20:59:05 +0000410
Honggyu Kim9eb6a102016-09-01 11:44:06 +0000411 std::string AttrVal = pImpl->getValueAsString();
412 if (AttrVal.empty()) return Result;
Bill Wendling7a33f772013-02-01 22:32:30 +0000413
Honggyu Kim9eb6a102016-09-01 11:44:06 +0000414 // Since some attribute strings contain special characters that cannot be
415 // printable, those have to be escaped to make the attribute value printable
416 // as is. e.g. "\01__gnu_mcount_nc"
417 {
418 raw_string_ostream OS(Result);
419 OS << "=\"";
420 PrintEscapedString(AttrVal, OS);
421 OS << "\"";
422 }
Bill Wendlingc79cdff2013-02-01 01:04:27 +0000423 return Result;
Bill Wendling9c2eba92013-01-31 20:59:05 +0000424 }
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000425
426 llvm_unreachable("Unknown attribute");
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000427}
428
Bill Wendlingd509a662013-01-29 00:34:06 +0000429bool Attribute::operator<(Attribute A) const {
430 if (!pImpl && !A.pImpl) return false;
431 if (!pImpl) return true;
432 if (!A.pImpl) return false;
433 return *pImpl < *A.pImpl;
434}
435
Bill Wendlingd509a662013-01-29 00:34:06 +0000436//===----------------------------------------------------------------------===//
437// AttributeImpl Definition
438//===----------------------------------------------------------------------===//
439
Eric Christopher0eaa5412014-07-02 22:05:40 +0000440// Pin the vtables to this file.
Eugene Zelenkodeaf6952017-02-17 00:00:09 +0000441AttributeImpl::~AttributeImpl() = default;
442
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +0000443void EnumAttributeImpl::anchor() {}
Eugene Zelenkodeaf6952017-02-17 00:00:09 +0000444
Hal Finkele15442c2014-07-18 06:51:55 +0000445void IntAttributeImpl::anchor() {}
Eugene Zelenkodeaf6952017-02-17 00:00:09 +0000446
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +0000447void StringAttributeImpl::anchor() {}
Alexey Samsonov49109a22013-11-18 09:31:53 +0000448
Bill Wendlingd509a662013-01-29 00:34:06 +0000449bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
Bill Wendling3f12ac22013-02-05 22:37:24 +0000450 if (isStringAttribute()) return false;
451 return getKindAsEnum() == A;
Bill Wendlingd509a662013-01-29 00:34:06 +0000452}
453
Bill Wendling3f12ac22013-02-05 22:37:24 +0000454bool AttributeImpl::hasAttribute(StringRef Kind) const {
455 if (!isStringAttribute()) return false;
456 return getKindAsString() == Kind;
Bill Wendlingd509a662013-01-29 00:34:06 +0000457}
458
Bill Wendling3f12ac22013-02-05 22:37:24 +0000459Attribute::AttrKind AttributeImpl::getKindAsEnum() const {
Hal Finkele15442c2014-07-18 06:51:55 +0000460 assert(isEnumAttribute() || isIntAttribute());
Benjamin Kramer741146b2013-07-11 12:13:16 +0000461 return static_cast<const EnumAttributeImpl *>(this)->getEnumKind();
Bill Wendlingd509a662013-01-29 00:34:06 +0000462}
463
Bill Wendling3f12ac22013-02-05 22:37:24 +0000464uint64_t AttributeImpl::getValueAsInt() const {
Hal Finkele15442c2014-07-18 06:51:55 +0000465 assert(isIntAttribute());
466 return static_cast<const IntAttributeImpl *>(this)->getValue();
Bill Wendlingd509a662013-01-29 00:34:06 +0000467}
468
Bill Wendling3f12ac22013-02-05 22:37:24 +0000469StringRef AttributeImpl::getKindAsString() const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000470 assert(isStringAttribute());
471 return static_cast<const StringAttributeImpl *>(this)->getStringKind();
Bill Wendlingd509a662013-01-29 00:34:06 +0000472}
473
Bill Wendling3f12ac22013-02-05 22:37:24 +0000474StringRef AttributeImpl::getValueAsString() const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000475 assert(isStringAttribute());
476 return static_cast<const StringAttributeImpl *>(this)->getStringValue();
Bill Wendlingd509a662013-01-29 00:34:06 +0000477}
478
479bool AttributeImpl::operator<(const AttributeImpl &AI) const {
Bill Wendlingc79cdff2013-02-01 01:04:27 +0000480 // This sorts the attributes with Attribute::AttrKinds coming first (sorted
481 // relative to their enum value) and then strings.
Bill Wendling26b95752013-02-15 05:25:26 +0000482 if (isEnumAttribute()) {
483 if (AI.isEnumAttribute()) return getKindAsEnum() < AI.getKindAsEnum();
Hal Finkele15442c2014-07-18 06:51:55 +0000484 if (AI.isIntAttribute()) return true;
Bill Wendling26b95752013-02-15 05:25:26 +0000485 if (AI.isStringAttribute()) return true;
486 }
Bill Wendlingc79cdff2013-02-01 01:04:27 +0000487
Hal Finkele15442c2014-07-18 06:51:55 +0000488 if (isIntAttribute()) {
Bill Wendling26b95752013-02-15 05:25:26 +0000489 if (AI.isEnumAttribute()) return false;
Reid Kleckner7de67612016-04-04 23:06:05 +0000490 if (AI.isIntAttribute()) {
491 if (getKindAsEnum() == AI.getKindAsEnum())
492 return getValueAsInt() < AI.getValueAsInt();
493 return getKindAsEnum() < AI.getKindAsEnum();
494 }
Bill Wendling26b95752013-02-15 05:25:26 +0000495 if (AI.isStringAttribute()) return true;
Bill Wendling3f12ac22013-02-05 22:37:24 +0000496 }
Bill Wendlingd509a662013-01-29 00:34:06 +0000497
Bill Wendling26b95752013-02-15 05:25:26 +0000498 if (AI.isEnumAttribute()) return false;
Hal Finkele15442c2014-07-18 06:51:55 +0000499 if (AI.isIntAttribute()) return false;
Bill Wendling26b95752013-02-15 05:25:26 +0000500 if (getKindAsString() == AI.getKindAsString())
501 return getValueAsString() < AI.getValueAsString();
502 return getKindAsString() < AI.getKindAsString();
Bill Wendlingd509a662013-01-29 00:34:06 +0000503}
504
Bill Wendlingd509a662013-01-29 00:34:06 +0000505//===----------------------------------------------------------------------===//
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000506// AttributeSet Definition
507//===----------------------------------------------------------------------===//
508
509AttributeSet AttributeSet::get(LLVMContext &C, const AttrBuilder &B) {
510 return AttributeSet(AttributeSetNode::get(C, B));
511}
512
513AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<Attribute> Attrs) {
514 return AttributeSet(AttributeSetNode::get(C, Attrs));
515}
516
Javed Absarf3d79042017-05-11 12:28:08 +0000517AttributeSet AttributeSet::addAttribute(LLVMContext &C,
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000518 Attribute::AttrKind Kind) const {
Javed Absarf3d79042017-05-11 12:28:08 +0000519 if (hasAttribute(Kind)) return *this;
520 AttrBuilder B;
521 B.addAttribute(Kind);
522 return addAttributes(C, AttributeSet::get(C, B));
523}
524
525AttributeSet AttributeSet::addAttribute(LLVMContext &C, StringRef Kind,
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000526 StringRef Value) const {
Javed Absarf3d79042017-05-11 12:28:08 +0000527 AttrBuilder B;
528 B.addAttribute(Kind, Value);
529 return addAttributes(C, AttributeSet::get(C, B));
530}
531
532AttributeSet AttributeSet::addAttributes(LLVMContext &C,
533 const AttributeSet AS) const {
534 if (!hasAttributes())
535 return AS;
536
537 if (!AS.hasAttributes())
538 return *this;
539
540 AttrBuilder B(AS);
541 for (Attribute I : *this)
542 B.addAttribute(I);
543
544 return get(C, B);
545}
546
547AttributeSet AttributeSet::removeAttribute(LLVMContext &C,
548 Attribute::AttrKind Kind) const {
549 if (!hasAttribute(Kind)) return *this;
Daniel Neilson88dddb82018-01-17 19:15:21 +0000550 AttrBuilder B(*this);
551 B.removeAttribute(Kind);
552 return get(C, B);
Javed Absarf3d79042017-05-11 12:28:08 +0000553}
554
555AttributeSet AttributeSet::removeAttribute(LLVMContext &C,
556 StringRef Kind) const {
557 if (!hasAttribute(Kind)) return *this;
Daniel Neilson88dddb82018-01-17 19:15:21 +0000558 AttrBuilder B(*this);
559 B.removeAttribute(Kind);
560 return get(C, B);
Javed Absarf3d79042017-05-11 12:28:08 +0000561}
562
563AttributeSet AttributeSet::removeAttributes(LLVMContext &C,
564 const AttrBuilder &Attrs) const {
Javed Absarf3d79042017-05-11 12:28:08 +0000565 AttrBuilder B(*this);
566 B.remove(Attrs);
567 return get(C, B);
568}
569
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000570unsigned AttributeSet::getNumAttributes() const {
571 return SetNode ? SetNode->getNumAttributes() : 0;
572}
573
574bool AttributeSet::hasAttribute(Attribute::AttrKind Kind) const {
Eugene Zelenkod761e2c2017-05-15 21:57:41 +0000575 return SetNode ? SetNode->hasAttribute(Kind) : false;
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000576}
577
578bool AttributeSet::hasAttribute(StringRef Kind) const {
Eugene Zelenkod761e2c2017-05-15 21:57:41 +0000579 return SetNode ? SetNode->hasAttribute(Kind) : false;
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000580}
581
582Attribute AttributeSet::getAttribute(Attribute::AttrKind Kind) const {
583 return SetNode ? SetNode->getAttribute(Kind) : Attribute();
584}
585
586Attribute AttributeSet::getAttribute(StringRef Kind) const {
587 return SetNode ? SetNode->getAttribute(Kind) : Attribute();
588}
589
590unsigned AttributeSet::getAlignment() const {
591 return SetNode ? SetNode->getAlignment() : 0;
592}
593
594unsigned AttributeSet::getStackAlignment() const {
595 return SetNode ? SetNode->getStackAlignment() : 0;
596}
597
598uint64_t AttributeSet::getDereferenceableBytes() const {
599 return SetNode ? SetNode->getDereferenceableBytes() : 0;
600}
601
602uint64_t AttributeSet::getDereferenceableOrNullBytes() const {
603 return SetNode ? SetNode->getDereferenceableOrNullBytes() : 0;
604}
605
606std::pair<unsigned, Optional<unsigned>> AttributeSet::getAllocSizeArgs() const {
Konstantin Zhuravlyov6df95b72017-04-12 23:57:37 +0000607 return SetNode ? SetNode->getAllocSizeArgs()
608 : std::pair<unsigned, Optional<unsigned>>(0, 0);
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000609}
610
611std::string AttributeSet::getAsString(bool InAttrGrp) const {
612 return SetNode ? SetNode->getAsString(InAttrGrp) : "";
613}
614
615AttributeSet::iterator AttributeSet::begin() const {
616 return SetNode ? SetNode->begin() : nullptr;
617}
618
619AttributeSet::iterator AttributeSet::end() const {
620 return SetNode ? SetNode->end() : nullptr;
621}
622
Aaron Ballman615eb472017-10-15 14:32:27 +0000623#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Javed Absarf3d79042017-05-11 12:28:08 +0000624LLVM_DUMP_METHOD void AttributeSet::dump() const {
625 dbgs() << "AS =\n";
626 dbgs() << " { ";
627 dbgs() << getAsString(true) << " }\n";
628}
629#endif
630
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000631//===----------------------------------------------------------------------===//
Bill Wendlingd509a662013-01-29 00:34:06 +0000632// AttributeSetNode Definition
633//===----------------------------------------------------------------------===//
634
Reid Kleckner8ff77852017-04-10 23:46:08 +0000635AttributeSetNode::AttributeSetNode(ArrayRef<Attribute> Attrs)
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000636 : AvailableAttrs(0), NumAttrs(Attrs.size()) {
Reid Kleckner8ff77852017-04-10 23:46:08 +0000637 // There's memory after the node where we can store the entries in.
638 std::copy(Attrs.begin(), Attrs.end(), getTrailingObjects<Attribute>());
639
640 for (Attribute I : *this) {
641 if (!I.isStringAttribute()) {
642 AvailableAttrs |= ((uint64_t)1) << I.getKindAsEnum();
643 }
644 }
645}
646
Bill Wendlingd509a662013-01-29 00:34:06 +0000647AttributeSetNode *AttributeSetNode::get(LLVMContext &C,
648 ArrayRef<Attribute> Attrs) {
649 if (Attrs.empty())
Craig Topperc6207612014-04-09 06:08:46 +0000650 return nullptr;
Bill Wendlingd509a662013-01-29 00:34:06 +0000651
652 // Otherwise, build a key to look up the existing attributes.
653 LLVMContextImpl *pImpl = C.pImpl;
654 FoldingSetNodeID ID;
655
656 SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end());
Reid Kleckner7de67612016-04-04 23:06:05 +0000657 std::sort(SortedAttrs.begin(), SortedAttrs.end());
Bill Wendlingd509a662013-01-29 00:34:06 +0000658
George Burgess IV500d3032015-12-16 05:21:02 +0000659 for (Attribute Attr : SortedAttrs)
660 Attr.Profile(ID);
Bill Wendlingd509a662013-01-29 00:34:06 +0000661
662 void *InsertPoint;
663 AttributeSetNode *PA =
664 pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint);
665
666 // If we didn't find any existing attributes of the same shape then create a
667 // new one and insert it.
668 if (!PA) {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000669 // Coallocate entries after the AttributeSetNode itself.
James Y Knightaa365b22015-08-05 22:57:34 +0000670 void *Mem = ::operator new(totalSizeToAlloc<Attribute>(SortedAttrs.size()));
Benjamin Kramer741146b2013-07-11 12:13:16 +0000671 PA = new (Mem) AttributeSetNode(SortedAttrs);
Bill Wendlingd509a662013-01-29 00:34:06 +0000672 pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint);
673 }
674
Reid Klecknerb5180542017-03-21 16:57:19 +0000675 // Return the AttributeSetNode that we found or created.
Bill Wendlingd509a662013-01-29 00:34:06 +0000676 return PA;
677}
678
Reid Klecknereb9dd5b2017-04-10 23:31:05 +0000679AttributeSetNode *AttributeSetNode::get(LLVMContext &C, const AttrBuilder &B) {
680 // Add target-independent attributes.
681 SmallVector<Attribute, 8> Attrs;
682 for (Attribute::AttrKind Kind = Attribute::None;
683 Kind != Attribute::EndAttrKinds; Kind = Attribute::AttrKind(Kind + 1)) {
684 if (!B.contains(Kind))
685 continue;
686
687 Attribute Attr;
688 switch (Kind) {
689 case Attribute::Alignment:
690 Attr = Attribute::getWithAlignment(C, B.getAlignment());
691 break;
692 case Attribute::StackAlignment:
693 Attr = Attribute::getWithStackAlignment(C, B.getStackAlignment());
694 break;
695 case Attribute::Dereferenceable:
696 Attr = Attribute::getWithDereferenceableBytes(
697 C, B.getDereferenceableBytes());
698 break;
699 case Attribute::DereferenceableOrNull:
700 Attr = Attribute::getWithDereferenceableOrNullBytes(
701 C, B.getDereferenceableOrNullBytes());
702 break;
703 case Attribute::AllocSize: {
704 auto A = B.getAllocSizeArgs();
705 Attr = Attribute::getWithAllocSizeArgs(C, A.first, A.second);
706 break;
707 }
708 default:
709 Attr = Attribute::get(C, Kind);
710 }
711 Attrs.push_back(Attr);
712 }
713
714 // Add target-dependent (string) attributes.
715 for (const auto &TDA : B.td_attrs())
716 Attrs.emplace_back(Attribute::get(C, TDA.first, TDA.second));
717
718 return get(C, Attrs);
719}
720
Bill Wendlingbce7b972013-02-13 08:42:21 +0000721bool AttributeSetNode::hasAttribute(StringRef Kind) const {
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000722 for (Attribute I : *this)
723 if (I.hasAttribute(Kind))
Bill Wendlingbce7b972013-02-13 08:42:21 +0000724 return true;
725 return false;
726}
727
728Attribute AttributeSetNode::getAttribute(Attribute::AttrKind Kind) const {
Matthias Braun31eeb76f2016-01-29 22:25:13 +0000729 if (hasAttribute(Kind)) {
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000730 for (Attribute I : *this)
731 if (I.hasAttribute(Kind))
732 return I;
Matthias Braun31eeb76f2016-01-29 22:25:13 +0000733 }
Bill Wendlingbce7b972013-02-13 08:42:21 +0000734 return Attribute();
735}
736
737Attribute AttributeSetNode::getAttribute(StringRef Kind) const {
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000738 for (Attribute I : *this)
739 if (I.hasAttribute(Kind))
740 return I;
Bill Wendlingbce7b972013-02-13 08:42:21 +0000741 return Attribute();
742}
743
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000744unsigned AttributeSetNode::getAlignment() const {
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000745 for (Attribute I : *this)
746 if (I.hasAttribute(Attribute::Alignment))
747 return I.getAlignment();
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000748 return 0;
749}
750
751unsigned AttributeSetNode::getStackAlignment() const {
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000752 for (Attribute I : *this)
753 if (I.hasAttribute(Attribute::StackAlignment))
754 return I.getStackAlignment();
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000755 return 0;
756}
757
Hal Finkelb0407ba2014-07-18 15:51:28 +0000758uint64_t AttributeSetNode::getDereferenceableBytes() const {
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000759 for (Attribute I : *this)
760 if (I.hasAttribute(Attribute::Dereferenceable))
761 return I.getDereferenceableBytes();
Hal Finkelb0407ba2014-07-18 15:51:28 +0000762 return 0;
763}
764
Sanjoy Das06cf33f2015-05-06 17:41:54 +0000765uint64_t AttributeSetNode::getDereferenceableOrNullBytes() const {
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000766 for (Attribute I : *this)
767 if (I.hasAttribute(Attribute::DereferenceableOrNull))
768 return I.getDereferenceableOrNullBytes();
Sanjoy Das06cf33f2015-05-06 17:41:54 +0000769 return 0;
770}
771
George Burgess IV278199f2016-04-12 01:05:35 +0000772std::pair<unsigned, Optional<unsigned>>
773AttributeSetNode::getAllocSizeArgs() const {
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000774 for (Attribute I : *this)
775 if (I.hasAttribute(Attribute::AllocSize))
776 return I.getAllocSizeArgs();
George Burgess IV278199f2016-04-12 01:05:35 +0000777 return std::make_pair(0, 0);
778}
779
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +0000780std::string AttributeSetNode::getAsString(bool InAttrGrp) const {
Benjamin Kramer0baf8f42013-04-19 11:43:21 +0000781 std::string Str;
Benjamin Kramer741146b2013-07-11 12:13:16 +0000782 for (iterator I = begin(), E = end(); I != E; ++I) {
783 if (I != begin())
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +0000784 Str += ' ';
785 Str += I->getAsString(InAttrGrp);
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000786 }
787 return Str;
788}
789
Bill Wendlingd509a662013-01-29 00:34:06 +0000790//===----------------------------------------------------------------------===//
Reid Klecknerb5180542017-03-21 16:57:19 +0000791// AttributeListImpl Definition
Bill Wendlingd509a662013-01-29 00:34:06 +0000792//===----------------------------------------------------------------------===//
793
Reid Kleckner51b2cd82017-10-11 01:40:38 +0000794/// Map from AttributeList index to the internal array index. Adding one happens
795/// to work, but it relies on unsigned integer wrapping. MSVC warns about
796/// unsigned wrapping in constexpr functions, so write out the conditional. LLVM
797/// folds it to add anyway.
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000798static constexpr unsigned attrIdxToArrayIdx(unsigned Index) {
Reid Kleckner51b2cd82017-10-11 01:40:38 +0000799 return Index == AttributeList::FunctionIndex ? 0 : Index + 1;
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000800}
801
802AttributeListImpl::AttributeListImpl(LLVMContext &C,
803 ArrayRef<AttributeSet> Sets)
804 : AvailableFunctionAttrs(0), Context(C), NumAttrSets(Sets.size()) {
805 assert(!Sets.empty() && "pointless AttributeListImpl");
Reid Klecknera82be602017-04-11 00:16:00 +0000806
807 // There's memory after the node where we can store the entries in.
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000808 std::copy(Sets.begin(), Sets.end(), getTrailingObjects<AttributeSet>());
Reid Klecknera82be602017-04-11 00:16:00 +0000809
810 // Initialize AvailableFunctionAttrs summary bitset.
Reid Klecknerec0fc032017-04-12 22:22:01 +0000811 static_assert(Attribute::EndAttrKinds <=
812 sizeof(AvailableFunctionAttrs) * CHAR_BIT,
813 "Too many attributes");
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000814 static_assert(attrIdxToArrayIdx(AttributeList::FunctionIndex) == 0U,
815 "function should be stored in slot 0");
816 for (Attribute I : Sets[0]) {
817 if (!I.isStringAttribute())
818 AvailableFunctionAttrs |= 1ULL << I.getKindAsEnum();
Reid Klecknera82be602017-04-11 00:16:00 +0000819 }
820}
821
822void AttributeListImpl::Profile(FoldingSetNodeID &ID) const {
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000823 Profile(ID, makeArrayRef(begin(), end()));
Reid Klecknera82be602017-04-11 00:16:00 +0000824}
825
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000826void AttributeListImpl::Profile(FoldingSetNodeID &ID,
827 ArrayRef<AttributeSet> Sets) {
828 for (const auto &Set : Sets)
829 ID.AddPointer(Set.SetNode);
Reid Klecknera82be602017-04-11 00:16:00 +0000830}
831
Aaron Ballman615eb472017-10-15 14:32:27 +0000832#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Reid Klecknerb5180542017-03-21 16:57:19 +0000833LLVM_DUMP_METHOD void AttributeListImpl::dump() const {
834 AttributeList(const_cast<AttributeListImpl *>(this)).dump();
Peter Collingbourneabca2ec2013-08-02 22:34:30 +0000835}
Matthias Braun8c209aa2017-01-28 02:02:38 +0000836#endif
Peter Collingbourneabca2ec2013-08-02 22:34:30 +0000837
Bill Wendlingd509a662013-01-29 00:34:06 +0000838//===----------------------------------------------------------------------===//
Reid Klecknerb5180542017-03-21 16:57:19 +0000839// AttributeList Construction and Mutation Methods
Bill Wendlingd509a662013-01-29 00:34:06 +0000840//===----------------------------------------------------------------------===//
841
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000842AttributeList AttributeList::getImpl(LLVMContext &C,
843 ArrayRef<AttributeSet> AttrSets) {
844 assert(!AttrSets.empty() && "pointless AttributeListImpl");
Reid Klecknereb9dd5b2017-04-10 23:31:05 +0000845
Bill Wendlingd509a662013-01-29 00:34:06 +0000846 LLVMContextImpl *pImpl = C.pImpl;
847 FoldingSetNodeID ID;
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000848 AttributeListImpl::Profile(ID, AttrSets);
Bill Wendlingd509a662013-01-29 00:34:06 +0000849
850 void *InsertPoint;
Reid Klecknerb5180542017-03-21 16:57:19 +0000851 AttributeListImpl *PA =
852 pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
Bill Wendlingd509a662013-01-29 00:34:06 +0000853
854 // If we didn't find any existing attributes of the same shape then
855 // create a new one and insert it.
856 if (!PA) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000857 // Coallocate entries after the AttributeListImpl itself.
James Y Knightaa365b22015-08-05 22:57:34 +0000858 void *Mem = ::operator new(
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000859 AttributeListImpl::totalSizeToAlloc<AttributeSet>(AttrSets.size()));
860 PA = new (Mem) AttributeListImpl(C, AttrSets);
Bill Wendlingd509a662013-01-29 00:34:06 +0000861 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
862 }
863
864 // Return the AttributesList that we found or created.
Reid Klecknerb5180542017-03-21 16:57:19 +0000865 return AttributeList(PA);
Bill Wendlingd509a662013-01-29 00:34:06 +0000866}
867
Reid Klecknerb5180542017-03-21 16:57:19 +0000868AttributeList
869AttributeList::get(LLVMContext &C,
870 ArrayRef<std::pair<unsigned, Attribute>> Attrs) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000871 // If there are no attributes then return a null AttributesList pointer.
872 if (Attrs.empty())
Reid Klecknerb5180542017-03-21 16:57:19 +0000873 return AttributeList();
Bill Wendlingd509a662013-01-29 00:34:06 +0000874
Craig Toppere30b8ca2016-01-03 19:43:40 +0000875 assert(std::is_sorted(Attrs.begin(), Attrs.end(),
876 [](const std::pair<unsigned, Attribute> &LHS,
877 const std::pair<unsigned, Attribute> &RHS) {
878 return LHS.first < RHS.first;
879 }) && "Misordered Attributes list!");
David Majnemer0a16c222016-08-11 21:15:00 +0000880 assert(none_of(Attrs,
881 [](const std::pair<unsigned, Attribute> &Pair) {
882 return Pair.second.hasAttribute(Attribute::None);
883 }) &&
884 "Pointless attribute!");
Bill Wendlingd509a662013-01-29 00:34:06 +0000885
886 // Create a vector if (unsigned, AttributeSetNode*) pairs from the attributes
887 // list.
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000888 SmallVector<std::pair<unsigned, AttributeSet>, 8> AttrPairVec;
Eugene Zelenkodeaf6952017-02-17 00:00:09 +0000889 for (ArrayRef<std::pair<unsigned, Attribute>>::iterator I = Attrs.begin(),
Bill Wendlingd509a662013-01-29 00:34:06 +0000890 E = Attrs.end(); I != E; ) {
891 unsigned Index = I->first;
892 SmallVector<Attribute, 4> AttrVec;
NAKAMURA Takumif05d2f22013-01-29 15:18:16 +0000893 while (I != E && I->first == Index) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000894 AttrVec.push_back(I->second);
895 ++I;
896 }
897
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000898 AttrPairVec.emplace_back(Index, AttributeSet::get(C, AttrVec));
Bill Wendlingd509a662013-01-29 00:34:06 +0000899 }
900
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000901 return get(C, AttrPairVec);
Bill Wendlingd509a662013-01-29 00:34:06 +0000902}
903
Reid Klecknerb5180542017-03-21 16:57:19 +0000904AttributeList
905AttributeList::get(LLVMContext &C,
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000906 ArrayRef<std::pair<unsigned, AttributeSet>> Attrs) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000907 // If there are no attributes then return a null AttributesList pointer.
908 if (Attrs.empty())
Reid Klecknerb5180542017-03-21 16:57:19 +0000909 return AttributeList();
Bill Wendlingd509a662013-01-29 00:34:06 +0000910
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000911 assert(std::is_sorted(Attrs.begin(), Attrs.end(),
912 [](const std::pair<unsigned, AttributeSet> &LHS,
913 const std::pair<unsigned, AttributeSet> &RHS) {
914 return LHS.first < RHS.first;
915 }) &&
916 "Misordered Attributes list!");
917 assert(none_of(Attrs,
918 [](const std::pair<unsigned, AttributeSet> &Pair) {
919 return !Pair.second.hasAttributes();
920 }) &&
921 "Pointless attribute!");
922
923 unsigned MaxIndex = Attrs.back().first;
924
925 SmallVector<AttributeSet, 4> AttrVec(attrIdxToArrayIdx(MaxIndex) + 1);
926 for (auto Pair : Attrs)
927 AttrVec[attrIdxToArrayIdx(Pair.first)] = Pair.second;
928
929 return getImpl(C, AttrVec);
Bill Wendlingd509a662013-01-29 00:34:06 +0000930}
931
Reid Kleckner7f720332017-04-13 00:58:09 +0000932AttributeList AttributeList::get(LLVMContext &C, AttributeSet FnAttrs,
933 AttributeSet RetAttrs,
934 ArrayRef<AttributeSet> ArgAttrs) {
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000935 // Scan from the end to find the last argument with attributes. Most
936 // arguments don't have attributes, so it's nice if we can have fewer unique
937 // AttributeListImpls by dropping empty attribute sets at the end of the list.
938 unsigned NumSets = 0;
939 for (size_t I = ArgAttrs.size(); I != 0; --I) {
940 if (ArgAttrs[I - 1].hasAttributes()) {
941 NumSets = I + 2;
942 break;
943 }
Reid Klecknereb9dd5b2017-04-10 23:31:05 +0000944 }
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000945 if (NumSets == 0) {
946 // Check function and return attributes if we didn't have argument
947 // attributes.
948 if (RetAttrs.hasAttributes())
949 NumSets = 2;
950 else if (FnAttrs.hasAttributes())
951 NumSets = 1;
952 }
953
954 // If all attribute sets were empty, we can use the empty attribute list.
955 if (NumSets == 0)
Reid Klecknereb9dd5b2017-04-10 23:31:05 +0000956 return AttributeList();
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000957
958 SmallVector<AttributeSet, 8> AttrSets;
959 AttrSets.reserve(NumSets);
960 // If we have any attributes, we always have function attributes.
961 AttrSets.push_back(FnAttrs);
962 if (NumSets > 1)
963 AttrSets.push_back(RetAttrs);
964 if (NumSets > 2) {
965 // Drop the empty argument attribute sets at the end.
966 ArgAttrs = ArgAttrs.take_front(NumSets - 2);
967 AttrSets.insert(AttrSets.end(), ArgAttrs.begin(), ArgAttrs.end());
968 }
969
970 return getImpl(C, AttrSets);
Reid Klecknereb9dd5b2017-04-10 23:31:05 +0000971}
972
Reid Klecknerb5180542017-03-21 16:57:19 +0000973AttributeList AttributeList::get(LLVMContext &C, unsigned Index,
974 const AttrBuilder &B) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000975 if (!B.hasAttributes())
Reid Klecknerb5180542017-03-21 16:57:19 +0000976 return AttributeList();
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000977 Index = attrIdxToArrayIdx(Index);
978 SmallVector<AttributeSet, 8> AttrSets(Index + 1);
979 AttrSets[Index] = AttributeSet::get(C, B);
980 return getImpl(C, AttrSets);
Bill Wendlingd509a662013-01-29 00:34:06 +0000981}
982
Reid Klecknerb5180542017-03-21 16:57:19 +0000983AttributeList AttributeList::get(LLVMContext &C, unsigned Index,
984 ArrayRef<Attribute::AttrKind> Kinds) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000985 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
Amaury Sechet392638d2016-06-14 20:27:35 +0000986 for (Attribute::AttrKind K : Kinds)
David Majnemer0a16c222016-08-11 21:15:00 +0000987 Attrs.emplace_back(Index, Attribute::get(C, K));
Bill Wendlingd509a662013-01-29 00:34:06 +0000988 return get(C, Attrs);
989}
990
Reid Klecknerb5180542017-03-21 16:57:19 +0000991AttributeList AttributeList::get(LLVMContext &C, unsigned Index,
992 ArrayRef<StringRef> Kinds) {
Amaury Sechet6100adf2016-06-15 17:50:39 +0000993 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
994 for (StringRef K : Kinds)
David Majnemer0a16c222016-08-11 21:15:00 +0000995 Attrs.emplace_back(Index, Attribute::get(C, K));
Amaury Sechet6100adf2016-06-15 17:50:39 +0000996 return get(C, Attrs);
997}
998
Reid Klecknerb5180542017-03-21 16:57:19 +0000999AttributeList AttributeList::get(LLVMContext &C,
1000 ArrayRef<AttributeList> Attrs) {
1001 if (Attrs.empty())
1002 return AttributeList();
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001003 if (Attrs.size() == 1)
1004 return Attrs[0];
Bill Wendlingd509a662013-01-29 00:34:06 +00001005
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001006 unsigned MaxSize = 0;
1007 for (AttributeList List : Attrs)
1008 MaxSize = std::max(MaxSize, List.getNumAttrSets());
1009
Reid Kleckner1d7cbdf2017-05-31 14:24:06 +00001010 // If every list was empty, there is no point in merging the lists.
1011 if (MaxSize == 0)
1012 return AttributeList();
1013
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001014 SmallVector<AttributeSet, 8> NewAttrSets(MaxSize);
1015 for (unsigned I = 0; I < MaxSize; ++I) {
1016 AttrBuilder CurBuilder;
1017 for (AttributeList List : Attrs)
1018 CurBuilder.merge(List.getAttributes(I - 1));
1019 NewAttrSets[I] = AttributeSet::get(C, CurBuilder);
Bill Wendlingd509a662013-01-29 00:34:06 +00001020 }
1021
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001022 return getImpl(C, NewAttrSets);
Bill Wendlingd509a662013-01-29 00:34:06 +00001023}
1024
Reid Klecknerb5180542017-03-21 16:57:19 +00001025AttributeList AttributeList::addAttribute(LLVMContext &C, unsigned Index,
1026 Attribute::AttrKind Kind) const {
Amaury Sechet392638d2016-06-14 20:27:35 +00001027 if (hasAttribute(Index, Kind)) return *this;
Reid Kleckneree4930b2017-05-02 22:07:37 +00001028 AttrBuilder B;
1029 B.addAttribute(Kind);
1030 return addAttributes(C, Index, B);
Reed Kotler795c7b42013-03-13 20:20:08 +00001031}
1032
Reid Klecknerb5180542017-03-21 16:57:19 +00001033AttributeList AttributeList::addAttribute(LLVMContext &C, unsigned Index,
1034 StringRef Kind,
1035 StringRef Value) const {
Eugene Zelenkodeaf6952017-02-17 00:00:09 +00001036 AttrBuilder B;
Bill Wendling3b2f6102013-07-25 18:34:24 +00001037 B.addAttribute(Kind, Value);
Reid Kleckneree4930b2017-05-02 22:07:37 +00001038 return addAttributes(C, Index, B);
Bill Wendling3b2f6102013-07-25 18:34:24 +00001039}
1040
Reid Kleckner5fbdd172017-05-31 19:23:09 +00001041AttributeList AttributeList::addAttribute(LLVMContext &C, unsigned Index,
Reid Klecknerb5180542017-03-21 16:57:19 +00001042 Attribute A) const {
Reid Kleckner5fbdd172017-05-31 19:23:09 +00001043 AttrBuilder B;
1044 B.addAttribute(A);
1045 return addAttributes(C, Index, B);
Akira Hatanaka237916b2015-12-02 06:58:49 +00001046}
1047
Reid Klecknerb5180542017-03-21 16:57:19 +00001048AttributeList AttributeList::addAttributes(LLVMContext &C, unsigned Index,
Reid Kleckner61906252017-04-19 01:51:13 +00001049 const AttrBuilder &B) const {
1050 if (!B.hasAttributes())
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00001051 return *this;
1052
Reid Klecknerfe64c012017-04-18 22:10:18 +00001053 if (!pImpl)
Reid Kleckner61906252017-04-19 01:51:13 +00001054 return AttributeList::get(C, {{Index, AttributeSet::get(C, B)}});
Reid Klecknerfe64c012017-04-18 22:10:18 +00001055
Bill Wendlingd509a662013-01-29 00:34:06 +00001056#ifndef NDEBUG
1057 // FIXME it is not obvious how this should work for alignment. For now, say
1058 // we can't change a known alignment.
Reid Klecknerbf6b3b152017-05-19 22:23:47 +00001059 unsigned OldAlign = getAttributes(Index).getAlignment();
Reid Kleckner61906252017-04-19 01:51:13 +00001060 unsigned NewAlign = B.getAlignment();
Bill Wendlingd509a662013-01-29 00:34:06 +00001061 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
1062 "Attempt to change alignment!");
1063#endif
1064
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001065 Index = attrIdxToArrayIdx(Index);
1066 SmallVector<AttributeSet, 4> AttrSets(this->begin(), this->end());
1067 if (Index >= AttrSets.size())
1068 AttrSets.resize(Index + 1);
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00001069
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001070 AttrBuilder Merged(AttrSets[Index]);
1071 Merged.merge(B);
1072 AttrSets[Index] = AttributeSet::get(C, Merged);
Bill Wendlingd509a662013-01-29 00:34:06 +00001073
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001074 return getImpl(C, AttrSets);
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00001075}
1076
Reid Kleckner5fbdd172017-05-31 19:23:09 +00001077AttributeList AttributeList::addParamAttribute(LLVMContext &C,
1078 ArrayRef<unsigned> ArgNos,
1079 Attribute A) const {
1080 assert(std::is_sorted(ArgNos.begin(), ArgNos.end()));
1081
1082 SmallVector<AttributeSet, 4> AttrSets(this->begin(), this->end());
1083 unsigned MaxIndex = attrIdxToArrayIdx(ArgNos.back() + FirstArgIndex);
1084 if (MaxIndex >= AttrSets.size())
1085 AttrSets.resize(MaxIndex + 1);
1086
1087 for (unsigned ArgNo : ArgNos) {
1088 unsigned Index = attrIdxToArrayIdx(ArgNo + FirstArgIndex);
1089 AttrBuilder B(AttrSets[Index]);
1090 B.addAttribute(A);
1091 AttrSets[Index] = AttributeSet::get(C, B);
1092 }
1093
1094 return getImpl(C, AttrSets);
1095}
1096
Reid Klecknerb5180542017-03-21 16:57:19 +00001097AttributeList AttributeList::removeAttribute(LLVMContext &C, unsigned Index,
1098 Attribute::AttrKind Kind) const {
Amaury Sechet392638d2016-06-14 20:27:35 +00001099 if (!hasAttribute(Index, Kind)) return *this;
Daniel Neilson88dddb82018-01-17 19:15:21 +00001100
1101 Index = attrIdxToArrayIdx(Index);
1102 SmallVector<AttributeSet, 4> AttrSets(this->begin(), this->end());
1103 assert(Index < AttrSets.size());
1104
1105 AttrSets[Index] = AttrSets[Index].removeAttribute(C, Kind);
1106
1107 return getImpl(C, AttrSets);
Bill Wendlingd509a662013-01-29 00:34:06 +00001108}
1109
Reid Klecknerb5180542017-03-21 16:57:19 +00001110AttributeList AttributeList::removeAttribute(LLVMContext &C, unsigned Index,
1111 StringRef Kind) const {
Amaury Sechet6100adf2016-06-15 17:50:39 +00001112 if (!hasAttribute(Index, Kind)) return *this;
Daniel Neilson88dddb82018-01-17 19:15:21 +00001113
1114 Index = attrIdxToArrayIdx(Index);
1115 SmallVector<AttributeSet, 4> AttrSets(this->begin(), this->end());
1116 assert(Index < AttrSets.size());
1117
1118 AttrSets[Index] = AttrSets[Index].removeAttribute(C, Kind);
1119
1120 return getImpl(C, AttrSets);
Bill Wendlingd509a662013-01-29 00:34:06 +00001121}
1122
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001123AttributeList
1124AttributeList::removeAttributes(LLVMContext &C, unsigned Index,
1125 const AttrBuilder &AttrsToRemove) const {
Reid Klecknerb5180542017-03-21 16:57:19 +00001126 if (!pImpl)
1127 return AttributeList();
Pete Cooperd2a44612015-05-06 23:19:43 +00001128
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001129 Index = attrIdxToArrayIdx(Index);
1130 SmallVector<AttributeSet, 4> AttrSets(this->begin(), this->end());
1131 if (Index >= AttrSets.size())
1132 AttrSets.resize(Index + 1);
Pete Cooperd2a44612015-05-06 23:19:43 +00001133
Daniel Neilson88dddb82018-01-17 19:15:21 +00001134 AttrSets[Index] = AttrSets[Index].removeAttributes(C, AttrsToRemove);
Pete Cooperd2a44612015-05-06 23:19:43 +00001135
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001136 return getImpl(C, AttrSets);
Pete Cooperd2a44612015-05-06 23:19:43 +00001137}
1138
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00001139AttributeList AttributeList::removeAttributes(LLVMContext &C,
1140 unsigned WithoutIndex) const {
1141 if (!pImpl)
1142 return AttributeList();
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001143 WithoutIndex = attrIdxToArrayIdx(WithoutIndex);
1144 if (WithoutIndex >= getNumAttrSets())
1145 return *this;
1146 SmallVector<AttributeSet, 4> AttrSets(this->begin(), this->end());
1147 AttrSets[WithoutIndex] = AttributeSet();
1148 return getImpl(C, AttrSets);
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00001149}
1150
Reid Klecknerb5180542017-03-21 16:57:19 +00001151AttributeList AttributeList::addDereferenceableAttr(LLVMContext &C,
1152 unsigned Index,
1153 uint64_t Bytes) const {
Eugene Zelenkodeaf6952017-02-17 00:00:09 +00001154 AttrBuilder B;
Ramkumar Ramachandra8fcb4982015-02-14 19:37:54 +00001155 B.addDereferenceableAttr(Bytes);
Reid Kleckneree4930b2017-05-02 22:07:37 +00001156 return addAttributes(C, Index, B);
Ramkumar Ramachandra8fcb4982015-02-14 19:37:54 +00001157}
1158
Reid Klecknerb5180542017-03-21 16:57:19 +00001159AttributeList
1160AttributeList::addDereferenceableOrNullAttr(LLVMContext &C, unsigned Index,
1161 uint64_t Bytes) const {
Eugene Zelenkodeaf6952017-02-17 00:00:09 +00001162 AttrBuilder B;
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001163 B.addDereferenceableOrNullAttr(Bytes);
Reid Kleckneree4930b2017-05-02 22:07:37 +00001164 return addAttributes(C, Index, B);
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001165}
1166
Reid Klecknerb5180542017-03-21 16:57:19 +00001167AttributeList
1168AttributeList::addAllocSizeAttr(LLVMContext &C, unsigned Index,
1169 unsigned ElemSizeArg,
1170 const Optional<unsigned> &NumElemsArg) {
Eugene Zelenkodeaf6952017-02-17 00:00:09 +00001171 AttrBuilder B;
George Burgess IV278199f2016-04-12 01:05:35 +00001172 B.addAllocSizeAttr(ElemSizeArg, NumElemsArg);
Reid Kleckneree4930b2017-05-02 22:07:37 +00001173 return addAttributes(C, Index, B);
George Burgess IV278199f2016-04-12 01:05:35 +00001174}
1175
Bill Wendlingd509a662013-01-29 00:34:06 +00001176//===----------------------------------------------------------------------===//
Reid Klecknerb5180542017-03-21 16:57:19 +00001177// AttributeList Accessor Methods
Bill Wendlingd509a662013-01-29 00:34:06 +00001178//===----------------------------------------------------------------------===//
1179
Reid Klecknerb5180542017-03-21 16:57:19 +00001180LLVMContext &AttributeList::getContext() const { return pImpl->getContext(); }
1181
Reid Klecknerf021fab2017-04-13 23:12:13 +00001182AttributeSet AttributeList::getParamAttributes(unsigned ArgNo) const {
Reid Klecknera0b45f42017-05-03 18:17:31 +00001183 return getAttributes(ArgNo + FirstArgIndex);
Bill Wendling5d020a32013-02-10 05:00:40 +00001184}
1185
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001186AttributeSet AttributeList::getRetAttributes() const {
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00001187 return getAttributes(ReturnIndex);
Bill Wendlingd509a662013-01-29 00:34:06 +00001188}
1189
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001190AttributeSet AttributeList::getFnAttributes() const {
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00001191 return getAttributes(FunctionIndex);
Bill Wendlingd509a662013-01-29 00:34:06 +00001192}
1193
Reid Klecknerb5180542017-03-21 16:57:19 +00001194bool AttributeList::hasAttribute(unsigned Index,
1195 Attribute::AttrKind Kind) const {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001196 return getAttributes(Index).hasAttribute(Kind);
Bill Wendlingd509a662013-01-29 00:34:06 +00001197}
1198
Reid Klecknerb5180542017-03-21 16:57:19 +00001199bool AttributeList::hasAttribute(unsigned Index, StringRef Kind) const {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001200 return getAttributes(Index).hasAttribute(Kind);
Bill Wendlingbce7b972013-02-13 08:42:21 +00001201}
1202
Reid Klecknerb5180542017-03-21 16:57:19 +00001203bool AttributeList::hasAttributes(unsigned Index) const {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001204 return getAttributes(Index).hasAttributes();
Bill Wendlingd509a662013-01-29 00:34:06 +00001205}
1206
Reid Klecknerb5180542017-03-21 16:57:19 +00001207bool AttributeList::hasFnAttribute(Attribute::AttrKind Kind) const {
Matthias Braun33282812016-01-29 22:25:19 +00001208 return pImpl && pImpl->hasFnAttribute(Kind);
1209}
1210
Reid Klecknerb5180542017-03-21 16:57:19 +00001211bool AttributeList::hasFnAttribute(StringRef Kind) const {
1212 return hasAttribute(AttributeList::FunctionIndex, Kind);
Amaury Sechet5f04d812016-09-09 04:50:38 +00001213}
1214
Reid Klecknerf021fab2017-04-13 23:12:13 +00001215bool AttributeList::hasParamAttribute(unsigned ArgNo,
1216 Attribute::AttrKind Kind) const {
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001217 return hasAttribute(ArgNo + FirstArgIndex, Kind);
Reid Klecknerf021fab2017-04-13 23:12:13 +00001218}
1219
Reid Klecknerb5180542017-03-21 16:57:19 +00001220bool AttributeList::hasAttrSomewhere(Attribute::AttrKind Attr,
1221 unsigned *Index) const {
Craig Topperc6207612014-04-09 06:08:46 +00001222 if (!pImpl) return false;
Bill Wendlingd509a662013-01-29 00:34:06 +00001223
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001224 for (unsigned I = index_begin(), E = index_end(); I != E; ++I) {
1225 if (hasAttribute(I, Attr)) {
1226 if (Index)
1227 *Index = I;
1228 return true;
1229 }
1230 }
Bill Wendlingd509a662013-01-29 00:34:06 +00001231
1232 return false;
1233}
1234
Reid Klecknerb5180542017-03-21 16:57:19 +00001235Attribute AttributeList::getAttribute(unsigned Index,
1236 Attribute::AttrKind Kind) const {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001237 return getAttributes(Index).getAttribute(Kind);
Bill Wendlingbce7b972013-02-13 08:42:21 +00001238}
1239
Reid Klecknerb5180542017-03-21 16:57:19 +00001240Attribute AttributeList::getAttribute(unsigned Index, StringRef Kind) const {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001241 return getAttributes(Index).getAttribute(Kind);
Bill Wendlingbce7b972013-02-13 08:42:21 +00001242}
1243
Reid Kleckner859f8b52017-04-28 20:34:27 +00001244unsigned AttributeList::getRetAlignment() const {
1245 return getAttributes(ReturnIndex).getAlignment();
1246}
1247
1248unsigned AttributeList::getParamAlignment(unsigned ArgNo) const {
Reid Klecknera0b45f42017-05-03 18:17:31 +00001249 return getAttributes(ArgNo + FirstArgIndex).getAlignment();
Bill Wendlingd509a662013-01-29 00:34:06 +00001250}
1251
Reid Klecknerb5180542017-03-21 16:57:19 +00001252unsigned AttributeList::getStackAlignment(unsigned Index) const {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001253 return getAttributes(Index).getStackAlignment();
Bill Wendlingd509a662013-01-29 00:34:06 +00001254}
1255
Reid Klecknerb5180542017-03-21 16:57:19 +00001256uint64_t AttributeList::getDereferenceableBytes(unsigned Index) const {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001257 return getAttributes(Index).getDereferenceableBytes();
Hal Finkelb0407ba2014-07-18 15:51:28 +00001258}
1259
Reid Klecknerb5180542017-03-21 16:57:19 +00001260uint64_t AttributeList::getDereferenceableOrNullBytes(unsigned Index) const {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001261 return getAttributes(Index).getDereferenceableOrNullBytes();
Sanjoy Das06cf33f2015-05-06 17:41:54 +00001262}
1263
George Burgess IV278199f2016-04-12 01:05:35 +00001264std::pair<unsigned, Optional<unsigned>>
Reid Klecknerb5180542017-03-21 16:57:19 +00001265AttributeList::getAllocSizeArgs(unsigned Index) const {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001266 return getAttributes(Index).getAllocSizeArgs();
George Burgess IV278199f2016-04-12 01:05:35 +00001267}
1268
Reid Klecknerb5180542017-03-21 16:57:19 +00001269std::string AttributeList::getAsString(unsigned Index, bool InAttrGrp) const {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001270 return getAttributes(Index).getAsString(InAttrGrp);
Bill Wendlingd509a662013-01-29 00:34:06 +00001271}
1272
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001273AttributeSet AttributeList::getAttributes(unsigned Index) const {
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001274 Index = attrIdxToArrayIdx(Index);
1275 if (!pImpl || Index >= getNumAttrSets())
1276 return AttributeSet();
1277 return pImpl->begin()[Index];
Bill Wendlingd509a662013-01-29 00:34:06 +00001278}
1279
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001280AttributeList::iterator AttributeList::begin() const {
1281 return pImpl ? pImpl->begin() : nullptr;
Bill Wendlinga9174862013-01-31 23:53:05 +00001282}
1283
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001284AttributeList::iterator AttributeList::end() const {
1285 return pImpl ? pImpl->end() : nullptr;
Bill Wendlinga9174862013-01-31 23:53:05 +00001286}
1287
Bill Wendlingd509a662013-01-29 00:34:06 +00001288//===----------------------------------------------------------------------===//
Reid Klecknerb5180542017-03-21 16:57:19 +00001289// AttributeList Introspection Methods
Bill Wendlingd509a662013-01-29 00:34:06 +00001290//===----------------------------------------------------------------------===//
1291
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001292unsigned AttributeList::getNumAttrSets() const {
1293 return pImpl ? pImpl->NumAttrSets : 0;
Bill Wendlingd509a662013-01-29 00:34:06 +00001294}
1295
Aaron Ballman615eb472017-10-15 14:32:27 +00001296#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Reid Klecknerb5180542017-03-21 16:57:19 +00001297LLVM_DUMP_METHOD void AttributeList::dump() const {
Bill Wendlingd509a662013-01-29 00:34:06 +00001298 dbgs() << "PAL[\n";
1299
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001300 for (unsigned i = index_begin(), e = index_end(); i != e; ++i) {
1301 if (getAttributes(i).hasAttributes())
1302 dbgs() << " { " << i << " => " << getAsString(i) << " }\n";
Bill Wendlingd509a662013-01-29 00:34:06 +00001303 }
1304
1305 dbgs() << "]\n";
1306}
Matthias Braun8c209aa2017-01-28 02:02:38 +00001307#endif
Bill Wendlingd509a662013-01-29 00:34:06 +00001308
Bill Wendlingabf3feb2012-10-05 06:44:41 +00001309//===----------------------------------------------------------------------===//
Bill Wendlingcd330342013-01-04 23:27:34 +00001310// AttrBuilder Method Implementations
Bill Wendlingabf3feb2012-10-05 06:44:41 +00001311//===----------------------------------------------------------------------===//
1312
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001313// FIXME: Remove this ctor, use AttributeSet.
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001314AttrBuilder::AttrBuilder(AttributeList AL, unsigned Index) {
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001315 AttributeSet AS = AL.getAttributes(Index);
1316 for (const Attribute &A : AS)
1317 addAttribute(A);
Bill Wendling096f5442013-01-07 08:24:35 +00001318}
1319
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001320AttrBuilder::AttrBuilder(AttributeSet AS) {
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001321 for (const Attribute &A : AS)
1322 addAttribute(A);
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00001323}
1324
Bill Wendlingcd330342013-01-04 23:27:34 +00001325void AttrBuilder::clear() {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001326 Attrs.reset();
Sanjoy Das88d0fde2015-09-03 22:27:42 +00001327 TargetDepAttrs.clear();
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001328 Alignment = StackAlignment = DerefBytes = DerefOrNullBytes = 0;
George Burgess IV278199f2016-04-12 01:05:35 +00001329 AllocSizeArgs = 0;
Bill Wendlingcd330342013-01-04 23:27:34 +00001330}
1331
1332AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001333 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
Bill Wendling1c7cc8a2013-01-31 23:16:25 +00001334 assert(Val != Attribute::Alignment && Val != Attribute::StackAlignment &&
George Burgess IV278199f2016-04-12 01:05:35 +00001335 Val != Attribute::Dereferenceable && Val != Attribute::AllocSize &&
Hal Finkelb0407ba2014-07-18 15:51:28 +00001336 "Adding integer attribute without adding a value!");
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001337 Attrs[Val] = true;
Bill Wendling7c04e042012-10-09 19:01:18 +00001338 return *this;
Bill Wendlingabf3feb2012-10-05 06:44:41 +00001339}
1340
Bill Wendling23804da2013-01-31 23:38:01 +00001341AttrBuilder &AttrBuilder::addAttribute(Attribute Attr) {
Bill Wendling0a437302013-02-10 10:13:23 +00001342 if (Attr.isStringAttribute()) {
1343 addAttribute(Attr.getKindAsString(), Attr.getValueAsString());
1344 return *this;
1345 }
1346
Bill Wendling3f12ac22013-02-05 22:37:24 +00001347 Attribute::AttrKind Kind = Attr.getKindAsEnum();
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001348 Attrs[Kind] = true;
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001349
Bill Wendling3f12ac22013-02-05 22:37:24 +00001350 if (Kind == Attribute::Alignment)
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001351 Alignment = Attr.getAlignment();
Bill Wendling3f12ac22013-02-05 22:37:24 +00001352 else if (Kind == Attribute::StackAlignment)
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001353 StackAlignment = Attr.getStackAlignment();
Hal Finkelb0407ba2014-07-18 15:51:28 +00001354 else if (Kind == Attribute::Dereferenceable)
1355 DerefBytes = Attr.getDereferenceableBytes();
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001356 else if (Kind == Attribute::DereferenceableOrNull)
1357 DerefOrNullBytes = Attr.getDereferenceableOrNullBytes();
George Burgess IV278199f2016-04-12 01:05:35 +00001358 else if (Kind == Attribute::AllocSize)
1359 AllocSizeArgs = Attr.getValueAsInt();
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001360 return *this;
1361}
1362
Bill Wendlingb9c5b1a2013-02-05 08:09:32 +00001363AttrBuilder &AttrBuilder::addAttribute(StringRef A, StringRef V) {
1364 TargetDepAttrs[A] = V;
1365 return *this;
1366}
1367
Bill Wendling23804da2013-01-31 23:38:01 +00001368AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001369 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
1370 Attrs[Val] = false;
Bill Wendling23804da2013-01-31 23:38:01 +00001371
1372 if (Val == Attribute::Alignment)
1373 Alignment = 0;
1374 else if (Val == Attribute::StackAlignment)
1375 StackAlignment = 0;
Hal Finkelb0407ba2014-07-18 15:51:28 +00001376 else if (Val == Attribute::Dereferenceable)
1377 DerefBytes = 0;
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001378 else if (Val == Attribute::DereferenceableOrNull)
1379 DerefOrNullBytes = 0;
George Burgess IV278199f2016-04-12 01:05:35 +00001380 else if (Val == Attribute::AllocSize)
1381 AllocSizeArgs = 0;
Bill Wendling23804da2013-01-31 23:38:01 +00001382
1383 return *this;
1384}
1385
Reid Klecknerb5180542017-03-21 16:57:19 +00001386AttrBuilder &AttrBuilder::removeAttributes(AttributeList A, uint64_t Index) {
Reid Kleckner6652a522017-04-28 18:37:16 +00001387 remove(A.getAttributes(Index));
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001388 return *this;
1389}
1390
Bill Wendlingb9c5b1a2013-02-05 08:09:32 +00001391AttrBuilder &AttrBuilder::removeAttribute(StringRef A) {
1392 std::map<std::string, std::string>::iterator I = TargetDepAttrs.find(A);
1393 if (I != TargetDepAttrs.end())
1394 TargetDepAttrs.erase(I);
1395 return *this;
1396}
1397
George Burgess IV278199f2016-04-12 01:05:35 +00001398std::pair<unsigned, Optional<unsigned>> AttrBuilder::getAllocSizeArgs() const {
1399 return unpackAllocSizeArgs(AllocSizeArgs);
1400}
1401
Bill Wendling50d27842012-10-15 20:35:56 +00001402AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
Bill Wendlingabd5ba22012-10-14 03:58:29 +00001403 if (Align == 0) return *this;
Bill Wendlingcd330342013-01-04 23:27:34 +00001404
Bill Wendlingabf3feb2012-10-05 06:44:41 +00001405 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1406 assert(Align <= 0x40000000 && "Alignment too large.");
Bill Wendlingcd330342013-01-04 23:27:34 +00001407
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001408 Attrs[Attribute::Alignment] = true;
Bill Wendlingcd330342013-01-04 23:27:34 +00001409 Alignment = Align;
Bill Wendlingabd5ba22012-10-14 03:58:29 +00001410 return *this;
Chris Lattner3e13b8c2008-01-02 23:42:30 +00001411}
1412
Bill Wendlingcd330342013-01-04 23:27:34 +00001413AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
1414 // Default alignment, allow the target to define how to align it.
1415 if (Align == 0) return *this;
1416
1417 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1418 assert(Align <= 0x100 && "Alignment too large.");
1419
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001420 Attrs[Attribute::StackAlignment] = true;
Bill Wendlingcd330342013-01-04 23:27:34 +00001421 StackAlignment = Align;
1422 return *this;
1423}
1424
Hal Finkelb0407ba2014-07-18 15:51:28 +00001425AttrBuilder &AttrBuilder::addDereferenceableAttr(uint64_t Bytes) {
1426 if (Bytes == 0) return *this;
1427
1428 Attrs[Attribute::Dereferenceable] = true;
1429 DerefBytes = Bytes;
1430 return *this;
1431}
1432
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001433AttrBuilder &AttrBuilder::addDereferenceableOrNullAttr(uint64_t Bytes) {
1434 if (Bytes == 0)
1435 return *this;
1436
1437 Attrs[Attribute::DereferenceableOrNull] = true;
1438 DerefOrNullBytes = Bytes;
1439 return *this;
1440}
1441
George Burgess IV278199f2016-04-12 01:05:35 +00001442AttrBuilder &AttrBuilder::addAllocSizeAttr(unsigned ElemSize,
1443 const Optional<unsigned> &NumElems) {
1444 return addAllocSizeAttrFromRawRepr(packAllocSizeArgs(ElemSize, NumElems));
1445}
1446
1447AttrBuilder &AttrBuilder::addAllocSizeAttrFromRawRepr(uint64_t RawArgs) {
1448 // (0, 0) is our "not present" value, so we need to check for it here.
1449 assert(RawArgs && "Invalid allocsize arguments -- given allocsize(0, 0)");
1450
1451 Attrs[Attribute::AllocSize] = true;
1452 // Reuse existing machinery to store this as a single 64-bit integer so we can
1453 // save a few bytes over using a pair<unsigned, Optional<unsigned>>.
1454 AllocSizeArgs = RawArgs;
1455 return *this;
1456}
1457
Bill Wendlinge2614922013-02-06 01:16:00 +00001458AttrBuilder &AttrBuilder::merge(const AttrBuilder &B) {
1459 // FIXME: What if both have alignments, but they don't match?!
1460 if (!Alignment)
1461 Alignment = B.Alignment;
1462
1463 if (!StackAlignment)
1464 StackAlignment = B.StackAlignment;
1465
Hal Finkelb0407ba2014-07-18 15:51:28 +00001466 if (!DerefBytes)
1467 DerefBytes = B.DerefBytes;
1468
Pete Cooperd2a44612015-05-06 23:19:43 +00001469 if (!DerefOrNullBytes)
1470 DerefOrNullBytes = B.DerefOrNullBytes;
1471
George Burgess IV278199f2016-04-12 01:05:35 +00001472 if (!AllocSizeArgs)
1473 AllocSizeArgs = B.AllocSizeArgs;
1474
Benjamin Kramer45e7d532013-02-16 19:13:18 +00001475 Attrs |= B.Attrs;
Bill Wendlinge2614922013-02-06 01:16:00 +00001476
Pete Cooperd2a44612015-05-06 23:19:43 +00001477 for (auto I : B.td_attrs())
1478 TargetDepAttrs[I.first] = I.second;
Bill Wendlinge2614922013-02-06 01:16:00 +00001479
1480 return *this;
1481}
1482
Pete Cooperd2a44612015-05-06 23:19:43 +00001483AttrBuilder &AttrBuilder::remove(const AttrBuilder &B) {
1484 // FIXME: What if both have alignments, but they don't match?!
1485 if (B.Alignment)
1486 Alignment = 0;
1487
1488 if (B.StackAlignment)
1489 StackAlignment = 0;
1490
1491 if (B.DerefBytes)
1492 DerefBytes = 0;
1493
1494 if (B.DerefOrNullBytes)
1495 DerefOrNullBytes = 0;
1496
George Burgess IV278199f2016-04-12 01:05:35 +00001497 if (B.AllocSizeArgs)
1498 AllocSizeArgs = 0;
1499
Pete Cooperd2a44612015-05-06 23:19:43 +00001500 Attrs &= ~B.Attrs;
1501
1502 for (auto I : B.td_attrs())
1503 TargetDepAttrs.erase(I.first);
1504
1505 return *this;
1506}
1507
1508bool AttrBuilder::overlaps(const AttrBuilder &B) const {
1509 // First check if any of the target independent attributes overlap.
1510 if ((Attrs & B.Attrs).any())
1511 return true;
1512
1513 // Then check if any target dependent ones do.
Sean Silva9011aca2017-02-22 06:34:04 +00001514 for (const auto &I : td_attrs())
Pete Cooperd2a44612015-05-06 23:19:43 +00001515 if (B.contains(I.first))
1516 return true;
1517
1518 return false;
1519}
1520
Bill Wendling4b001442013-02-06 01:33:42 +00001521bool AttrBuilder::contains(StringRef A) const {
1522 return TargetDepAttrs.find(A) != TargetDepAttrs.end();
1523}
1524
Bill Wendling50d27842012-10-15 20:35:56 +00001525bool AttrBuilder::hasAttributes() const {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001526 return !Attrs.none() || !TargetDepAttrs.empty();
Bill Wendlingc6daefa2012-10-08 23:27:46 +00001527}
Bill Wendling9ac69f92013-01-04 20:54:35 +00001528
Reid Kleckner6652a522017-04-28 18:37:16 +00001529bool AttrBuilder::hasAttributes(AttributeList AL, uint64_t Index) const {
1530 AttributeSet AS = AL.getAttributes(Index);
Bill Wendling9ca01da2013-02-02 00:42:06 +00001531
Reid Kleckner6652a522017-04-28 18:37:16 +00001532 for (Attribute Attr : AS) {
Hal Finkele15442c2014-07-18 06:51:55 +00001533 if (Attr.isEnumAttribute() || Attr.isIntAttribute()) {
Reid Kleckner6652a522017-04-28 18:37:16 +00001534 if (contains(Attr.getKindAsEnum()))
Bill Wendling7cde51d2013-02-12 07:56:49 +00001535 return true;
1536 } else {
1537 assert(Attr.isStringAttribute() && "Invalid attribute kind!");
Reid Kleckner6652a522017-04-28 18:37:16 +00001538 return contains(Attr.getKindAsString());
Bill Wendling7cde51d2013-02-12 07:56:49 +00001539 }
1540 }
Bill Wendling9ca01da2013-02-02 00:42:06 +00001541
1542 return false;
Bill Wendling70f39172012-10-09 00:01:21 +00001543}
Bill Wendling9ac69f92013-01-04 20:54:35 +00001544
Bill Wendling50d27842012-10-15 20:35:56 +00001545bool AttrBuilder::hasAlignmentAttr() const {
Bill Wendlingcd330342013-01-04 23:27:34 +00001546 return Alignment != 0;
Bill Wendlingc6daefa2012-10-08 23:27:46 +00001547}
1548
Bill Wendlingd509a662013-01-29 00:34:06 +00001549bool AttrBuilder::operator==(const AttrBuilder &B) {
Benjamin Kramer45e7d532013-02-16 19:13:18 +00001550 if (Attrs != B.Attrs)
1551 return false;
Bill Wendling4b001442013-02-06 01:33:42 +00001552
1553 for (td_const_iterator I = TargetDepAttrs.begin(),
1554 E = TargetDepAttrs.end(); I != E; ++I)
1555 if (B.TargetDepAttrs.find(I->first) == B.TargetDepAttrs.end())
1556 return false;
1557
Hal Finkelb0407ba2014-07-18 15:51:28 +00001558 return Alignment == B.Alignment && StackAlignment == B.StackAlignment &&
1559 DerefBytes == B.DerefBytes;
Bill Wendlingd509a662013-01-29 00:34:06 +00001560}
1561
Bill Wendling57625a42013-01-25 23:09:36 +00001562//===----------------------------------------------------------------------===//
1563// AttributeFuncs Function Defintions
1564//===----------------------------------------------------------------------===//
1565
Bill Wendlingc79cdff2013-02-01 01:04:27 +00001566/// \brief Which attributes cannot be applied to a type.
Craig Toppere3dcce92015-08-01 22:20:21 +00001567AttrBuilder AttributeFuncs::typeIncompatible(Type *Ty) {
Bill Wendling57625a42013-01-25 23:09:36 +00001568 AttrBuilder Incompatible;
1569
1570 if (!Ty->isIntegerTy())
1571 // Attribute that only apply to integers.
1572 Incompatible.addAttribute(Attribute::SExt)
1573 .addAttribute(Attribute::ZExt);
1574
1575 if (!Ty->isPointerTy())
1576 // Attribute that only apply to pointers.
1577 Incompatible.addAttribute(Attribute::ByVal)
1578 .addAttribute(Attribute::Nest)
1579 .addAttribute(Attribute::NoAlias)
1580 .addAttribute(Attribute::NoCapture)
Nick Lewyckyd52b1522014-05-20 01:23:40 +00001581 .addAttribute(Attribute::NonNull)
Hal Finkelb0407ba2014-07-18 15:51:28 +00001582 .addDereferenceableAttr(1) // the int here is ignored
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001583 .addDereferenceableOrNullAttr(1) // the int here is ignored
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001584 .addAttribute(Attribute::ReadNone)
1585 .addAttribute(Attribute::ReadOnly)
Reid Klecknera534a382013-12-19 02:14:12 +00001586 .addAttribute(Attribute::StructRet)
1587 .addAttribute(Attribute::InAlloca);
Bill Wendling57625a42013-01-25 23:09:36 +00001588
Pete Cooper2777d8872015-05-06 23:19:56 +00001589 return Incompatible;
Bill Wendling57625a42013-01-25 23:09:36 +00001590}
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001591
1592template<typename AttrClass>
1593static bool isEqual(const Function &Caller, const Function &Callee) {
1594 return Caller.getFnAttribute(AttrClass::getKind()) ==
1595 Callee.getFnAttribute(AttrClass::getKind());
1596}
1597
1598/// \brief Compute the logical AND of the attributes of the caller and the
1599/// callee.
1600///
1601/// This function sets the caller's attribute to false if the callee's attribute
1602/// is false.
1603template<typename AttrClass>
1604static void setAND(Function &Caller, const Function &Callee) {
1605 if (AttrClass::isSet(Caller, AttrClass::getKind()) &&
1606 !AttrClass::isSet(Callee, AttrClass::getKind()))
1607 AttrClass::set(Caller, AttrClass::getKind(), false);
1608}
1609
1610/// \brief Compute the logical OR of the attributes of the caller and the
1611/// callee.
1612///
1613/// This function sets the caller's attribute to true if the callee's attribute
1614/// is true.
1615template<typename AttrClass>
1616static void setOR(Function &Caller, const Function &Callee) {
1617 if (!AttrClass::isSet(Caller, AttrClass::getKind()) &&
1618 AttrClass::isSet(Callee, AttrClass::getKind()))
1619 AttrClass::set(Caller, AttrClass::getKind(), true);
1620}
1621
1622/// \brief If the inlined function had a higher stack protection level than the
1623/// calling function, then bump up the caller's stack protection level.
1624static void adjustCallerSSPLevel(Function &Caller, const Function &Callee) {
1625 // If upgrading the SSP attribute, clear out the old SSP Attributes first.
1626 // Having multiple SSP attributes doesn't actually hurt, but it adds useless
1627 // clutter to the IR.
Reid Kleckneree4930b2017-05-02 22:07:37 +00001628 AttrBuilder OldSSPAttr;
1629 OldSSPAttr.addAttribute(Attribute::StackProtect)
1630 .addAttribute(Attribute::StackProtectStrong)
1631 .addAttribute(Attribute::StackProtectReq);
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001632
Evgeniy Stepanovf17120a2016-04-11 22:27:48 +00001633 if (Callee.hasFnAttribute(Attribute::StackProtectReq)) {
Reid Klecknerb5180542017-03-21 16:57:19 +00001634 Caller.removeAttributes(AttributeList::FunctionIndex, OldSSPAttr);
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001635 Caller.addFnAttr(Attribute::StackProtectReq);
1636 } else if (Callee.hasFnAttribute(Attribute::StackProtectStrong) &&
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001637 !Caller.hasFnAttribute(Attribute::StackProtectReq)) {
Reid Klecknerb5180542017-03-21 16:57:19 +00001638 Caller.removeAttributes(AttributeList::FunctionIndex, OldSSPAttr);
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001639 Caller.addFnAttr(Attribute::StackProtectStrong);
1640 } else if (Callee.hasFnAttribute(Attribute::StackProtect) &&
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001641 !Caller.hasFnAttribute(Attribute::StackProtectReq) &&
1642 !Caller.hasFnAttribute(Attribute::StackProtectStrong))
1643 Caller.addFnAttr(Attribute::StackProtect);
1644}
1645
whitequarked54b4a2017-06-21 18:46:50 +00001646/// \brief If the inlined function required stack probes, then ensure that
1647/// the calling function has those too.
1648static void adjustCallerStackProbes(Function &Caller, const Function &Callee) {
whitequark08b20352017-06-22 23:22:36 +00001649 if (!Caller.hasFnAttribute("probe-stack") &&
1650 Callee.hasFnAttribute("probe-stack")) {
1651 Caller.addFnAttr(Callee.getFnAttribute("probe-stack"));
1652 }
1653}
1654
1655/// \brief If the inlined function defines the size of guard region
1656/// on the stack, then ensure that the calling function defines a guard region
1657/// that is no larger.
1658static void
1659adjustCallerStackProbeSize(Function &Caller, const Function &Callee) {
1660 if (Callee.hasFnAttribute("stack-probe-size")) {
1661 uint64_t CalleeStackProbeSize;
1662 Callee.getFnAttribute("stack-probe-size")
1663 .getValueAsString()
1664 .getAsInteger(0, CalleeStackProbeSize);
1665 if (Caller.hasFnAttribute("stack-probe-size")) {
1666 uint64_t CallerStackProbeSize;
1667 Caller.getFnAttribute("stack-probe-size")
1668 .getValueAsString()
1669 .getAsInteger(0, CallerStackProbeSize);
1670 if (CallerStackProbeSize > CalleeStackProbeSize) {
1671 Caller.addFnAttr(Callee.getFnAttribute("stack-probe-size"));
1672 }
1673 } else {
1674 Caller.addFnAttr(Callee.getFnAttribute("stack-probe-size"));
1675 }
1676 }
whitequarked54b4a2017-06-21 18:46:50 +00001677}
1678
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001679#define GET_ATTR_COMPAT_FUNC
1680#include "AttributesCompatFunc.inc"
1681
1682bool AttributeFuncs::areInlineCompatible(const Function &Caller,
1683 const Function &Callee) {
1684 return hasCompatibleFnAttrs(Caller, Callee);
1685}
1686
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001687void AttributeFuncs::mergeAttributesForInlining(Function &Caller,
1688 const Function &Callee) {
1689 mergeFnAttrs(Caller, Callee);
1690}