blob: 1b19a04747275e82f80ab9a0755a7f214aac86c1 [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";
James Molloye6f87ca2015-11-06 10:32:53 +0000302 if (hasAttribute(Attribute::NoRecurse))
303 return "norecurse";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000304 if (hasAttribute(Attribute::NoUnwind))
305 return "nounwind";
Andrea Di Biagio377496b2013-08-23 11:53:55 +0000306 if (hasAttribute(Attribute::OptimizeNone))
307 return "optnone";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000308 if (hasAttribute(Attribute::OptimizeForSize))
309 return "optsize";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000310 if (hasAttribute(Attribute::ReadNone))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000311 return "readnone";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000312 if (hasAttribute(Attribute::ReadOnly))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000313 return "readonly";
Nicolai Haehnle84c9f992016-07-04 08:01:29 +0000314 if (hasAttribute(Attribute::WriteOnly))
315 return "writeonly";
Stephen Linb8bd2322013-04-20 05:14:40 +0000316 if (hasAttribute(Attribute::Returned))
317 return "returned";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000318 if (hasAttribute(Attribute::ReturnsTwice))
319 return "returns_twice";
320 if (hasAttribute(Attribute::SExt))
321 return "signext";
Matt Arsenaultb19b57e2017-04-28 20:25:27 +0000322 if (hasAttribute(Attribute::Speculatable))
323 return "speculatable";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000324 if (hasAttribute(Attribute::StackProtect))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000325 return "ssp";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000326 if (hasAttribute(Attribute::StackProtectReq))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000327 return "sspreq";
Bill Wendlingd154e2832013-01-23 06:41:41 +0000328 if (hasAttribute(Attribute::StackProtectStrong))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000329 return "sspstrong";
Peter Collingbourne82437bf2015-06-15 21:07:11 +0000330 if (hasAttribute(Attribute::SafeStack))
331 return "safestack";
Andrew Kaylor53a5fbb2017-08-14 21:15:13 +0000332 if (hasAttribute(Attribute::StrictFP))
333 return "strictfp";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000334 if (hasAttribute(Attribute::StructRet))
335 return "sret";
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000336 if (hasAttribute(Attribute::SanitizeThread))
337 return "sanitize_thread";
338 if (hasAttribute(Attribute::SanitizeMemory))
339 return "sanitize_memory";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000340 if (hasAttribute(Attribute::UWTable))
341 return "uwtable";
342 if (hasAttribute(Attribute::ZExt))
343 return "zeroext";
Diego Novilloc6399532013-05-24 12:26:52 +0000344 if (hasAttribute(Attribute::Cold))
345 return "cold";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000346
347 // FIXME: These should be output like this:
348 //
349 // align=4
350 // alignstack=8
351 //
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000352 if (hasAttribute(Attribute::Alignment)) {
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000353 std::string Result;
Bill Wendling829b4782013-02-11 08:43:33 +0000354 Result += "align";
355 Result += (InAttrGrp) ? "=" : " ";
Bill Wendling3f12ac22013-02-05 22:37:24 +0000356 Result += utostr(getValueAsInt());
357 return Result;
358 }
Bill Wendling829b4782013-02-11 08:43:33 +0000359
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000360 auto AttrWithBytesToString = [&](const char *Name) {
Bill Wendling3f12ac22013-02-05 22:37:24 +0000361 std::string Result;
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000362 Result += Name;
Bill Wendling829b4782013-02-11 08:43:33 +0000363 if (InAttrGrp) {
364 Result += "=";
365 Result += utostr(getValueAsInt());
366 } else {
367 Result += "(";
368 Result += utostr(getValueAsInt());
369 Result += ")";
370 }
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000371 return Result;
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000372 };
Bill Wendling9c2eba92013-01-31 20:59:05 +0000373
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000374 if (hasAttribute(Attribute::StackAlignment))
375 return AttrWithBytesToString("alignstack");
376
377 if (hasAttribute(Attribute::Dereferenceable))
378 return AttrWithBytesToString("dereferenceable");
379
380 if (hasAttribute(Attribute::DereferenceableOrNull))
381 return AttrWithBytesToString("dereferenceable_or_null");
Hal Finkelb0407ba2014-07-18 15:51:28 +0000382
George Burgess IV278199f2016-04-12 01:05:35 +0000383 if (hasAttribute(Attribute::AllocSize)) {
384 unsigned ElemSize;
385 Optional<unsigned> NumElems;
386 std::tie(ElemSize, NumElems) = getAllocSizeArgs();
387
388 std::string Result = "allocsize(";
389 Result += utostr(ElemSize);
390 if (NumElems.hasValue()) {
391 Result += ',';
392 Result += utostr(*NumElems);
393 }
394 Result += ')';
395 return Result;
396 }
397
Bill Wendling9c2eba92013-01-31 20:59:05 +0000398 // Convert target-dependent attributes to strings of the form:
399 //
400 // "kind"
401 // "kind" = "value"
Bill Wendling9c2eba92013-01-31 20:59:05 +0000402 //
Bill Wendling3f12ac22013-02-05 22:37:24 +0000403 if (isStringAttribute()) {
Bill Wendling9c2eba92013-01-31 20:59:05 +0000404 std::string Result;
Yaron Keren075759a2015-03-30 15:42:36 +0000405 Result += (Twine('"') + getKindAsString() + Twine('"')).str();
Bill Wendling9c2eba92013-01-31 20:59:05 +0000406
Honggyu Kim9eb6a102016-09-01 11:44:06 +0000407 std::string AttrVal = pImpl->getValueAsString();
408 if (AttrVal.empty()) return Result;
Bill Wendling7a33f772013-02-01 22:32:30 +0000409
Honggyu Kim9eb6a102016-09-01 11:44:06 +0000410 // Since some attribute strings contain special characters that cannot be
411 // printable, those have to be escaped to make the attribute value printable
412 // as is. e.g. "\01__gnu_mcount_nc"
413 {
414 raw_string_ostream OS(Result);
415 OS << "=\"";
416 PrintEscapedString(AttrVal, OS);
417 OS << "\"";
418 }
Bill Wendlingc79cdff2013-02-01 01:04:27 +0000419 return Result;
Bill Wendling9c2eba92013-01-31 20:59:05 +0000420 }
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000421
422 llvm_unreachable("Unknown attribute");
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000423}
424
Bill Wendlingd509a662013-01-29 00:34:06 +0000425bool Attribute::operator<(Attribute A) const {
426 if (!pImpl && !A.pImpl) return false;
427 if (!pImpl) return true;
428 if (!A.pImpl) return false;
429 return *pImpl < *A.pImpl;
430}
431
Bill Wendlingd509a662013-01-29 00:34:06 +0000432//===----------------------------------------------------------------------===//
433// AttributeImpl Definition
434//===----------------------------------------------------------------------===//
435
Eric Christopher0eaa5412014-07-02 22:05:40 +0000436// Pin the vtables to this file.
Eugene Zelenkodeaf6952017-02-17 00:00:09 +0000437AttributeImpl::~AttributeImpl() = default;
438
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +0000439void EnumAttributeImpl::anchor() {}
Eugene Zelenkodeaf6952017-02-17 00:00:09 +0000440
Hal Finkele15442c2014-07-18 06:51:55 +0000441void IntAttributeImpl::anchor() {}
Eugene Zelenkodeaf6952017-02-17 00:00:09 +0000442
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +0000443void StringAttributeImpl::anchor() {}
Alexey Samsonov49109a22013-11-18 09:31:53 +0000444
Bill Wendlingd509a662013-01-29 00:34:06 +0000445bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
Bill Wendling3f12ac22013-02-05 22:37:24 +0000446 if (isStringAttribute()) return false;
447 return getKindAsEnum() == A;
Bill Wendlingd509a662013-01-29 00:34:06 +0000448}
449
Bill Wendling3f12ac22013-02-05 22:37:24 +0000450bool AttributeImpl::hasAttribute(StringRef Kind) const {
451 if (!isStringAttribute()) return false;
452 return getKindAsString() == Kind;
Bill Wendlingd509a662013-01-29 00:34:06 +0000453}
454
Bill Wendling3f12ac22013-02-05 22:37:24 +0000455Attribute::AttrKind AttributeImpl::getKindAsEnum() const {
Hal Finkele15442c2014-07-18 06:51:55 +0000456 assert(isEnumAttribute() || isIntAttribute());
Benjamin Kramer741146b2013-07-11 12:13:16 +0000457 return static_cast<const EnumAttributeImpl *>(this)->getEnumKind();
Bill Wendlingd509a662013-01-29 00:34:06 +0000458}
459
Bill Wendling3f12ac22013-02-05 22:37:24 +0000460uint64_t AttributeImpl::getValueAsInt() const {
Hal Finkele15442c2014-07-18 06:51:55 +0000461 assert(isIntAttribute());
462 return static_cast<const IntAttributeImpl *>(this)->getValue();
Bill Wendlingd509a662013-01-29 00:34:06 +0000463}
464
Bill Wendling3f12ac22013-02-05 22:37:24 +0000465StringRef AttributeImpl::getKindAsString() const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000466 assert(isStringAttribute());
467 return static_cast<const StringAttributeImpl *>(this)->getStringKind();
Bill Wendlingd509a662013-01-29 00:34:06 +0000468}
469
Bill Wendling3f12ac22013-02-05 22:37:24 +0000470StringRef AttributeImpl::getValueAsString() const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000471 assert(isStringAttribute());
472 return static_cast<const StringAttributeImpl *>(this)->getStringValue();
Bill Wendlingd509a662013-01-29 00:34:06 +0000473}
474
475bool AttributeImpl::operator<(const AttributeImpl &AI) const {
Bill Wendlingc79cdff2013-02-01 01:04:27 +0000476 // This sorts the attributes with Attribute::AttrKinds coming first (sorted
477 // relative to their enum value) and then strings.
Bill Wendling26b95752013-02-15 05:25:26 +0000478 if (isEnumAttribute()) {
479 if (AI.isEnumAttribute()) return getKindAsEnum() < AI.getKindAsEnum();
Hal Finkele15442c2014-07-18 06:51:55 +0000480 if (AI.isIntAttribute()) return true;
Bill Wendling26b95752013-02-15 05:25:26 +0000481 if (AI.isStringAttribute()) return true;
482 }
Bill Wendlingc79cdff2013-02-01 01:04:27 +0000483
Hal Finkele15442c2014-07-18 06:51:55 +0000484 if (isIntAttribute()) {
Bill Wendling26b95752013-02-15 05:25:26 +0000485 if (AI.isEnumAttribute()) return false;
Reid Kleckner7de67612016-04-04 23:06:05 +0000486 if (AI.isIntAttribute()) {
487 if (getKindAsEnum() == AI.getKindAsEnum())
488 return getValueAsInt() < AI.getValueAsInt();
489 return getKindAsEnum() < AI.getKindAsEnum();
490 }
Bill Wendling26b95752013-02-15 05:25:26 +0000491 if (AI.isStringAttribute()) return true;
Bill Wendling3f12ac22013-02-05 22:37:24 +0000492 }
Bill Wendlingd509a662013-01-29 00:34:06 +0000493
Bill Wendling26b95752013-02-15 05:25:26 +0000494 if (AI.isEnumAttribute()) return false;
Hal Finkele15442c2014-07-18 06:51:55 +0000495 if (AI.isIntAttribute()) return false;
Bill Wendling26b95752013-02-15 05:25:26 +0000496 if (getKindAsString() == AI.getKindAsString())
497 return getValueAsString() < AI.getValueAsString();
498 return getKindAsString() < AI.getKindAsString();
Bill Wendlingd509a662013-01-29 00:34:06 +0000499}
500
Bill Wendlingd509a662013-01-29 00:34:06 +0000501//===----------------------------------------------------------------------===//
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000502// AttributeSet Definition
503//===----------------------------------------------------------------------===//
504
505AttributeSet AttributeSet::get(LLVMContext &C, const AttrBuilder &B) {
506 return AttributeSet(AttributeSetNode::get(C, B));
507}
508
509AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<Attribute> Attrs) {
510 return AttributeSet(AttributeSetNode::get(C, Attrs));
511}
512
Javed Absarf3d79042017-05-11 12:28:08 +0000513AttributeSet AttributeSet::addAttribute(LLVMContext &C,
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000514 Attribute::AttrKind Kind) const {
Javed Absarf3d79042017-05-11 12:28:08 +0000515 if (hasAttribute(Kind)) return *this;
516 AttrBuilder B;
517 B.addAttribute(Kind);
518 return addAttributes(C, AttributeSet::get(C, B));
519}
520
521AttributeSet AttributeSet::addAttribute(LLVMContext &C, StringRef Kind,
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000522 StringRef Value) const {
Javed Absarf3d79042017-05-11 12:28:08 +0000523 AttrBuilder B;
524 B.addAttribute(Kind, Value);
525 return addAttributes(C, AttributeSet::get(C, B));
526}
527
528AttributeSet AttributeSet::addAttributes(LLVMContext &C,
529 const AttributeSet AS) const {
530 if (!hasAttributes())
531 return AS;
532
533 if (!AS.hasAttributes())
534 return *this;
535
536 AttrBuilder B(AS);
537 for (Attribute I : *this)
538 B.addAttribute(I);
539
540 return get(C, B);
541}
542
543AttributeSet AttributeSet::removeAttribute(LLVMContext &C,
544 Attribute::AttrKind Kind) const {
545 if (!hasAttribute(Kind)) return *this;
546 AttrBuilder B;
547 B.addAttribute(Kind);
548 return removeAttributes(C, B);
549}
550
551AttributeSet AttributeSet::removeAttribute(LLVMContext &C,
552 StringRef Kind) const {
553 if (!hasAttribute(Kind)) return *this;
554 AttrBuilder B;
555 B.addAttribute(Kind);
556 return removeAttributes(C, B);
557}
558
559AttributeSet AttributeSet::removeAttributes(LLVMContext &C,
560 const AttrBuilder &Attrs) const {
561
562 // FIXME it is not obvious how this should work for alignment.
563 // For now, say we can't pass in alignment, which no current use does.
564 assert(!Attrs.hasAlignmentAttr() && "Attempt to change alignment!");
565
566 AttrBuilder B(*this);
567 B.remove(Attrs);
568 return get(C, B);
569}
570
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000571unsigned AttributeSet::getNumAttributes() const {
572 return SetNode ? SetNode->getNumAttributes() : 0;
573}
574
575bool AttributeSet::hasAttribute(Attribute::AttrKind Kind) const {
Eugene Zelenkod761e2c2017-05-15 21:57:41 +0000576 return SetNode ? SetNode->hasAttribute(Kind) : false;
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000577}
578
579bool AttributeSet::hasAttribute(StringRef Kind) const {
Eugene Zelenkod761e2c2017-05-15 21:57:41 +0000580 return SetNode ? SetNode->hasAttribute(Kind) : false;
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000581}
582
583Attribute AttributeSet::getAttribute(Attribute::AttrKind Kind) const {
584 return SetNode ? SetNode->getAttribute(Kind) : Attribute();
585}
586
587Attribute AttributeSet::getAttribute(StringRef Kind) const {
588 return SetNode ? SetNode->getAttribute(Kind) : Attribute();
589}
590
591unsigned AttributeSet::getAlignment() const {
592 return SetNode ? SetNode->getAlignment() : 0;
593}
594
595unsigned AttributeSet::getStackAlignment() const {
596 return SetNode ? SetNode->getStackAlignment() : 0;
597}
598
599uint64_t AttributeSet::getDereferenceableBytes() const {
600 return SetNode ? SetNode->getDereferenceableBytes() : 0;
601}
602
603uint64_t AttributeSet::getDereferenceableOrNullBytes() const {
604 return SetNode ? SetNode->getDereferenceableOrNullBytes() : 0;
605}
606
607std::pair<unsigned, Optional<unsigned>> AttributeSet::getAllocSizeArgs() const {
Konstantin Zhuravlyov6df95b72017-04-12 23:57:37 +0000608 return SetNode ? SetNode->getAllocSizeArgs()
609 : std::pair<unsigned, Optional<unsigned>>(0, 0);
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000610}
611
612std::string AttributeSet::getAsString(bool InAttrGrp) const {
613 return SetNode ? SetNode->getAsString(InAttrGrp) : "";
614}
615
616AttributeSet::iterator AttributeSet::begin() const {
617 return SetNode ? SetNode->begin() : nullptr;
618}
619
620AttributeSet::iterator AttributeSet::end() const {
621 return SetNode ? SetNode->end() : nullptr;
622}
623
Aaron Ballman615eb472017-10-15 14:32:27 +0000624#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Javed Absarf3d79042017-05-11 12:28:08 +0000625LLVM_DUMP_METHOD void AttributeSet::dump() const {
626 dbgs() << "AS =\n";
627 dbgs() << " { ";
628 dbgs() << getAsString(true) << " }\n";
629}
630#endif
631
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000632//===----------------------------------------------------------------------===//
Bill Wendlingd509a662013-01-29 00:34:06 +0000633// AttributeSetNode Definition
634//===----------------------------------------------------------------------===//
635
Reid Kleckner8ff77852017-04-10 23:46:08 +0000636AttributeSetNode::AttributeSetNode(ArrayRef<Attribute> Attrs)
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000637 : AvailableAttrs(0), NumAttrs(Attrs.size()) {
Reid Kleckner8ff77852017-04-10 23:46:08 +0000638 // There's memory after the node where we can store the entries in.
639 std::copy(Attrs.begin(), Attrs.end(), getTrailingObjects<Attribute>());
640
641 for (Attribute I : *this) {
642 if (!I.isStringAttribute()) {
643 AvailableAttrs |= ((uint64_t)1) << I.getKindAsEnum();
644 }
645 }
646}
647
Bill Wendlingd509a662013-01-29 00:34:06 +0000648AttributeSetNode *AttributeSetNode::get(LLVMContext &C,
649 ArrayRef<Attribute> Attrs) {
650 if (Attrs.empty())
Craig Topperc6207612014-04-09 06:08:46 +0000651 return nullptr;
Bill Wendlingd509a662013-01-29 00:34:06 +0000652
653 // Otherwise, build a key to look up the existing attributes.
654 LLVMContextImpl *pImpl = C.pImpl;
655 FoldingSetNodeID ID;
656
657 SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end());
Reid Kleckner7de67612016-04-04 23:06:05 +0000658 std::sort(SortedAttrs.begin(), SortedAttrs.end());
Bill Wendlingd509a662013-01-29 00:34:06 +0000659
George Burgess IV500d3032015-12-16 05:21:02 +0000660 for (Attribute Attr : SortedAttrs)
661 Attr.Profile(ID);
Bill Wendlingd509a662013-01-29 00:34:06 +0000662
663 void *InsertPoint;
664 AttributeSetNode *PA =
665 pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint);
666
667 // If we didn't find any existing attributes of the same shape then create a
668 // new one and insert it.
669 if (!PA) {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000670 // Coallocate entries after the AttributeSetNode itself.
James Y Knightaa365b22015-08-05 22:57:34 +0000671 void *Mem = ::operator new(totalSizeToAlloc<Attribute>(SortedAttrs.size()));
Benjamin Kramer741146b2013-07-11 12:13:16 +0000672 PA = new (Mem) AttributeSetNode(SortedAttrs);
Bill Wendlingd509a662013-01-29 00:34:06 +0000673 pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint);
674 }
675
Reid Klecknerb5180542017-03-21 16:57:19 +0000676 // Return the AttributeSetNode that we found or created.
Bill Wendlingd509a662013-01-29 00:34:06 +0000677 return PA;
678}
679
Reid Klecknereb9dd5b2017-04-10 23:31:05 +0000680AttributeSetNode *AttributeSetNode::get(LLVMContext &C, const AttrBuilder &B) {
681 // Add target-independent attributes.
682 SmallVector<Attribute, 8> Attrs;
683 for (Attribute::AttrKind Kind = Attribute::None;
684 Kind != Attribute::EndAttrKinds; Kind = Attribute::AttrKind(Kind + 1)) {
685 if (!B.contains(Kind))
686 continue;
687
688 Attribute Attr;
689 switch (Kind) {
690 case Attribute::Alignment:
691 Attr = Attribute::getWithAlignment(C, B.getAlignment());
692 break;
693 case Attribute::StackAlignment:
694 Attr = Attribute::getWithStackAlignment(C, B.getStackAlignment());
695 break;
696 case Attribute::Dereferenceable:
697 Attr = Attribute::getWithDereferenceableBytes(
698 C, B.getDereferenceableBytes());
699 break;
700 case Attribute::DereferenceableOrNull:
701 Attr = Attribute::getWithDereferenceableOrNullBytes(
702 C, B.getDereferenceableOrNullBytes());
703 break;
704 case Attribute::AllocSize: {
705 auto A = B.getAllocSizeArgs();
706 Attr = Attribute::getWithAllocSizeArgs(C, A.first, A.second);
707 break;
708 }
709 default:
710 Attr = Attribute::get(C, Kind);
711 }
712 Attrs.push_back(Attr);
713 }
714
715 // Add target-dependent (string) attributes.
716 for (const auto &TDA : B.td_attrs())
717 Attrs.emplace_back(Attribute::get(C, TDA.first, TDA.second));
718
719 return get(C, Attrs);
720}
721
Bill Wendlingbce7b972013-02-13 08:42:21 +0000722bool AttributeSetNode::hasAttribute(StringRef Kind) const {
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000723 for (Attribute I : *this)
724 if (I.hasAttribute(Kind))
Bill Wendlingbce7b972013-02-13 08:42:21 +0000725 return true;
726 return false;
727}
728
729Attribute AttributeSetNode::getAttribute(Attribute::AttrKind Kind) const {
Matthias Braun31eeb76f2016-01-29 22:25:13 +0000730 if (hasAttribute(Kind)) {
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000731 for (Attribute I : *this)
732 if (I.hasAttribute(Kind))
733 return I;
Matthias Braun31eeb76f2016-01-29 22:25:13 +0000734 }
Bill Wendlingbce7b972013-02-13 08:42:21 +0000735 return Attribute();
736}
737
738Attribute AttributeSetNode::getAttribute(StringRef Kind) const {
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000739 for (Attribute I : *this)
740 if (I.hasAttribute(Kind))
741 return I;
Bill Wendlingbce7b972013-02-13 08:42:21 +0000742 return Attribute();
743}
744
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000745unsigned AttributeSetNode::getAlignment() const {
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000746 for (Attribute I : *this)
747 if (I.hasAttribute(Attribute::Alignment))
748 return I.getAlignment();
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000749 return 0;
750}
751
752unsigned AttributeSetNode::getStackAlignment() const {
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000753 for (Attribute I : *this)
754 if (I.hasAttribute(Attribute::StackAlignment))
755 return I.getStackAlignment();
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000756 return 0;
757}
758
Hal Finkelb0407ba2014-07-18 15:51:28 +0000759uint64_t AttributeSetNode::getDereferenceableBytes() const {
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000760 for (Attribute I : *this)
761 if (I.hasAttribute(Attribute::Dereferenceable))
762 return I.getDereferenceableBytes();
Hal Finkelb0407ba2014-07-18 15:51:28 +0000763 return 0;
764}
765
Sanjoy Das06cf33f2015-05-06 17:41:54 +0000766uint64_t AttributeSetNode::getDereferenceableOrNullBytes() const {
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000767 for (Attribute I : *this)
768 if (I.hasAttribute(Attribute::DereferenceableOrNull))
769 return I.getDereferenceableOrNullBytes();
Sanjoy Das06cf33f2015-05-06 17:41:54 +0000770 return 0;
771}
772
George Burgess IV278199f2016-04-12 01:05:35 +0000773std::pair<unsigned, Optional<unsigned>>
774AttributeSetNode::getAllocSizeArgs() const {
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000775 for (Attribute I : *this)
776 if (I.hasAttribute(Attribute::AllocSize))
777 return I.getAllocSizeArgs();
George Burgess IV278199f2016-04-12 01:05:35 +0000778 return std::make_pair(0, 0);
779}
780
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +0000781std::string AttributeSetNode::getAsString(bool InAttrGrp) const {
Benjamin Kramer0baf8f42013-04-19 11:43:21 +0000782 std::string Str;
Benjamin Kramer741146b2013-07-11 12:13:16 +0000783 for (iterator I = begin(), E = end(); I != E; ++I) {
784 if (I != begin())
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +0000785 Str += ' ';
786 Str += I->getAsString(InAttrGrp);
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000787 }
788 return Str;
789}
790
Bill Wendlingd509a662013-01-29 00:34:06 +0000791//===----------------------------------------------------------------------===//
Reid Klecknerb5180542017-03-21 16:57:19 +0000792// AttributeListImpl Definition
Bill Wendlingd509a662013-01-29 00:34:06 +0000793//===----------------------------------------------------------------------===//
794
Reid Kleckner51b2cd82017-10-11 01:40:38 +0000795/// Map from AttributeList index to the internal array index. Adding one happens
796/// to work, but it relies on unsigned integer wrapping. MSVC warns about
797/// unsigned wrapping in constexpr functions, so write out the conditional. LLVM
798/// folds it to add anyway.
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000799static constexpr unsigned attrIdxToArrayIdx(unsigned Index) {
Reid Kleckner51b2cd82017-10-11 01:40:38 +0000800 return Index == AttributeList::FunctionIndex ? 0 : Index + 1;
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000801}
802
803AttributeListImpl::AttributeListImpl(LLVMContext &C,
804 ArrayRef<AttributeSet> Sets)
805 : AvailableFunctionAttrs(0), Context(C), NumAttrSets(Sets.size()) {
806 assert(!Sets.empty() && "pointless AttributeListImpl");
Reid Klecknera82be602017-04-11 00:16:00 +0000807
808 // There's memory after the node where we can store the entries in.
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000809 std::copy(Sets.begin(), Sets.end(), getTrailingObjects<AttributeSet>());
Reid Klecknera82be602017-04-11 00:16:00 +0000810
811 // Initialize AvailableFunctionAttrs summary bitset.
Reid Klecknerec0fc032017-04-12 22:22:01 +0000812 static_assert(Attribute::EndAttrKinds <=
813 sizeof(AvailableFunctionAttrs) * CHAR_BIT,
814 "Too many attributes");
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000815 static_assert(attrIdxToArrayIdx(AttributeList::FunctionIndex) == 0U,
816 "function should be stored in slot 0");
817 for (Attribute I : Sets[0]) {
818 if (!I.isStringAttribute())
819 AvailableFunctionAttrs |= 1ULL << I.getKindAsEnum();
Reid Klecknera82be602017-04-11 00:16:00 +0000820 }
821}
822
823void AttributeListImpl::Profile(FoldingSetNodeID &ID) const {
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000824 Profile(ID, makeArrayRef(begin(), end()));
Reid Klecknera82be602017-04-11 00:16:00 +0000825}
826
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000827void AttributeListImpl::Profile(FoldingSetNodeID &ID,
828 ArrayRef<AttributeSet> Sets) {
829 for (const auto &Set : Sets)
830 ID.AddPointer(Set.SetNode);
Reid Klecknera82be602017-04-11 00:16:00 +0000831}
832
Aaron Ballman615eb472017-10-15 14:32:27 +0000833#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Reid Klecknerb5180542017-03-21 16:57:19 +0000834LLVM_DUMP_METHOD void AttributeListImpl::dump() const {
835 AttributeList(const_cast<AttributeListImpl *>(this)).dump();
Peter Collingbourneabca2ec2013-08-02 22:34:30 +0000836}
Matthias Braun8c209aa2017-01-28 02:02:38 +0000837#endif
Peter Collingbourneabca2ec2013-08-02 22:34:30 +0000838
Bill Wendlingd509a662013-01-29 00:34:06 +0000839//===----------------------------------------------------------------------===//
Reid Klecknerb5180542017-03-21 16:57:19 +0000840// AttributeList Construction and Mutation Methods
Bill Wendlingd509a662013-01-29 00:34:06 +0000841//===----------------------------------------------------------------------===//
842
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000843AttributeList AttributeList::getImpl(LLVMContext &C,
844 ArrayRef<AttributeSet> AttrSets) {
845 assert(!AttrSets.empty() && "pointless AttributeListImpl");
Reid Klecknereb9dd5b2017-04-10 23:31:05 +0000846
Bill Wendlingd509a662013-01-29 00:34:06 +0000847 LLVMContextImpl *pImpl = C.pImpl;
848 FoldingSetNodeID ID;
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000849 AttributeListImpl::Profile(ID, AttrSets);
Bill Wendlingd509a662013-01-29 00:34:06 +0000850
851 void *InsertPoint;
Reid Klecknerb5180542017-03-21 16:57:19 +0000852 AttributeListImpl *PA =
853 pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
Bill Wendlingd509a662013-01-29 00:34:06 +0000854
855 // If we didn't find any existing attributes of the same shape then
856 // create a new one and insert it.
857 if (!PA) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000858 // Coallocate entries after the AttributeListImpl itself.
James Y Knightaa365b22015-08-05 22:57:34 +0000859 void *Mem = ::operator new(
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000860 AttributeListImpl::totalSizeToAlloc<AttributeSet>(AttrSets.size()));
861 PA = new (Mem) AttributeListImpl(C, AttrSets);
Bill Wendlingd509a662013-01-29 00:34:06 +0000862 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
863 }
864
865 // Return the AttributesList that we found or created.
Reid Klecknerb5180542017-03-21 16:57:19 +0000866 return AttributeList(PA);
Bill Wendlingd509a662013-01-29 00:34:06 +0000867}
868
Reid Klecknerb5180542017-03-21 16:57:19 +0000869AttributeList
870AttributeList::get(LLVMContext &C,
871 ArrayRef<std::pair<unsigned, Attribute>> Attrs) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000872 // If there are no attributes then return a null AttributesList pointer.
873 if (Attrs.empty())
Reid Klecknerb5180542017-03-21 16:57:19 +0000874 return AttributeList();
Bill Wendlingd509a662013-01-29 00:34:06 +0000875
Craig Toppere30b8ca2016-01-03 19:43:40 +0000876 assert(std::is_sorted(Attrs.begin(), Attrs.end(),
877 [](const std::pair<unsigned, Attribute> &LHS,
878 const std::pair<unsigned, Attribute> &RHS) {
879 return LHS.first < RHS.first;
880 }) && "Misordered Attributes list!");
David Majnemer0a16c222016-08-11 21:15:00 +0000881 assert(none_of(Attrs,
882 [](const std::pair<unsigned, Attribute> &Pair) {
883 return Pair.second.hasAttribute(Attribute::None);
884 }) &&
885 "Pointless attribute!");
Bill Wendlingd509a662013-01-29 00:34:06 +0000886
887 // Create a vector if (unsigned, AttributeSetNode*) pairs from the attributes
888 // list.
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000889 SmallVector<std::pair<unsigned, AttributeSet>, 8> AttrPairVec;
Eugene Zelenkodeaf6952017-02-17 00:00:09 +0000890 for (ArrayRef<std::pair<unsigned, Attribute>>::iterator I = Attrs.begin(),
Bill Wendlingd509a662013-01-29 00:34:06 +0000891 E = Attrs.end(); I != E; ) {
892 unsigned Index = I->first;
893 SmallVector<Attribute, 4> AttrVec;
NAKAMURA Takumif05d2f22013-01-29 15:18:16 +0000894 while (I != E && I->first == Index) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000895 AttrVec.push_back(I->second);
896 ++I;
897 }
898
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000899 AttrPairVec.emplace_back(Index, AttributeSet::get(C, AttrVec));
Bill Wendlingd509a662013-01-29 00:34:06 +0000900 }
901
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000902 return get(C, AttrPairVec);
Bill Wendlingd509a662013-01-29 00:34:06 +0000903}
904
Reid Klecknerb5180542017-03-21 16:57:19 +0000905AttributeList
906AttributeList::get(LLVMContext &C,
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000907 ArrayRef<std::pair<unsigned, AttributeSet>> Attrs) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000908 // If there are no attributes then return a null AttributesList pointer.
909 if (Attrs.empty())
Reid Klecknerb5180542017-03-21 16:57:19 +0000910 return AttributeList();
Bill Wendlingd509a662013-01-29 00:34:06 +0000911
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000912 assert(std::is_sorted(Attrs.begin(), Attrs.end(),
913 [](const std::pair<unsigned, AttributeSet> &LHS,
914 const std::pair<unsigned, AttributeSet> &RHS) {
915 return LHS.first < RHS.first;
916 }) &&
917 "Misordered Attributes list!");
918 assert(none_of(Attrs,
919 [](const std::pair<unsigned, AttributeSet> &Pair) {
920 return !Pair.second.hasAttributes();
921 }) &&
922 "Pointless attribute!");
923
924 unsigned MaxIndex = Attrs.back().first;
925
926 SmallVector<AttributeSet, 4> AttrVec(attrIdxToArrayIdx(MaxIndex) + 1);
927 for (auto Pair : Attrs)
928 AttrVec[attrIdxToArrayIdx(Pair.first)] = Pair.second;
929
930 return getImpl(C, AttrVec);
Bill Wendlingd509a662013-01-29 00:34:06 +0000931}
932
Reid Kleckner7f720332017-04-13 00:58:09 +0000933AttributeList AttributeList::get(LLVMContext &C, AttributeSet FnAttrs,
934 AttributeSet RetAttrs,
935 ArrayRef<AttributeSet> ArgAttrs) {
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000936 // Scan from the end to find the last argument with attributes. Most
937 // arguments don't have attributes, so it's nice if we can have fewer unique
938 // AttributeListImpls by dropping empty attribute sets at the end of the list.
939 unsigned NumSets = 0;
940 for (size_t I = ArgAttrs.size(); I != 0; --I) {
941 if (ArgAttrs[I - 1].hasAttributes()) {
942 NumSets = I + 2;
943 break;
944 }
Reid Klecknereb9dd5b2017-04-10 23:31:05 +0000945 }
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000946 if (NumSets == 0) {
947 // Check function and return attributes if we didn't have argument
948 // attributes.
949 if (RetAttrs.hasAttributes())
950 NumSets = 2;
951 else if (FnAttrs.hasAttributes())
952 NumSets = 1;
953 }
954
955 // If all attribute sets were empty, we can use the empty attribute list.
956 if (NumSets == 0)
Reid Klecknereb9dd5b2017-04-10 23:31:05 +0000957 return AttributeList();
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000958
959 SmallVector<AttributeSet, 8> AttrSets;
960 AttrSets.reserve(NumSets);
961 // If we have any attributes, we always have function attributes.
962 AttrSets.push_back(FnAttrs);
963 if (NumSets > 1)
964 AttrSets.push_back(RetAttrs);
965 if (NumSets > 2) {
966 // Drop the empty argument attribute sets at the end.
967 ArgAttrs = ArgAttrs.take_front(NumSets - 2);
968 AttrSets.insert(AttrSets.end(), ArgAttrs.begin(), ArgAttrs.end());
969 }
970
971 return getImpl(C, AttrSets);
Reid Klecknereb9dd5b2017-04-10 23:31:05 +0000972}
973
Reid Klecknerb5180542017-03-21 16:57:19 +0000974AttributeList AttributeList::get(LLVMContext &C, unsigned Index,
975 const AttrBuilder &B) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000976 if (!B.hasAttributes())
Reid Klecknerb5180542017-03-21 16:57:19 +0000977 return AttributeList();
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000978 Index = attrIdxToArrayIdx(Index);
979 SmallVector<AttributeSet, 8> AttrSets(Index + 1);
980 AttrSets[Index] = AttributeSet::get(C, B);
981 return getImpl(C, AttrSets);
Bill Wendlingd509a662013-01-29 00:34:06 +0000982}
983
Reid Klecknerb5180542017-03-21 16:57:19 +0000984AttributeList AttributeList::get(LLVMContext &C, unsigned Index,
985 ArrayRef<Attribute::AttrKind> Kinds) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000986 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
Amaury Sechet392638d2016-06-14 20:27:35 +0000987 for (Attribute::AttrKind K : Kinds)
David Majnemer0a16c222016-08-11 21:15:00 +0000988 Attrs.emplace_back(Index, Attribute::get(C, K));
Bill Wendlingd509a662013-01-29 00:34:06 +0000989 return get(C, Attrs);
990}
991
Reid Klecknerb5180542017-03-21 16:57:19 +0000992AttributeList AttributeList::get(LLVMContext &C, unsigned Index,
993 ArrayRef<StringRef> Kinds) {
Amaury Sechet6100adf2016-06-15 17:50:39 +0000994 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
995 for (StringRef K : Kinds)
David Majnemer0a16c222016-08-11 21:15:00 +0000996 Attrs.emplace_back(Index, Attribute::get(C, K));
Amaury Sechet6100adf2016-06-15 17:50:39 +0000997 return get(C, Attrs);
998}
999
Reid Klecknerb5180542017-03-21 16:57:19 +00001000AttributeList AttributeList::get(LLVMContext &C,
1001 ArrayRef<AttributeList> Attrs) {
1002 if (Attrs.empty())
1003 return AttributeList();
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001004 if (Attrs.size() == 1)
1005 return Attrs[0];
Bill Wendlingd509a662013-01-29 00:34:06 +00001006
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001007 unsigned MaxSize = 0;
1008 for (AttributeList List : Attrs)
1009 MaxSize = std::max(MaxSize, List.getNumAttrSets());
1010
Reid Kleckner1d7cbdf2017-05-31 14:24:06 +00001011 // If every list was empty, there is no point in merging the lists.
1012 if (MaxSize == 0)
1013 return AttributeList();
1014
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001015 SmallVector<AttributeSet, 8> NewAttrSets(MaxSize);
1016 for (unsigned I = 0; I < MaxSize; ++I) {
1017 AttrBuilder CurBuilder;
1018 for (AttributeList List : Attrs)
1019 CurBuilder.merge(List.getAttributes(I - 1));
1020 NewAttrSets[I] = AttributeSet::get(C, CurBuilder);
Bill Wendlingd509a662013-01-29 00:34:06 +00001021 }
1022
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001023 return getImpl(C, NewAttrSets);
Bill Wendlingd509a662013-01-29 00:34:06 +00001024}
1025
Reid Klecknerb5180542017-03-21 16:57:19 +00001026AttributeList AttributeList::addAttribute(LLVMContext &C, unsigned Index,
1027 Attribute::AttrKind Kind) const {
Amaury Sechet392638d2016-06-14 20:27:35 +00001028 if (hasAttribute(Index, Kind)) return *this;
Reid Kleckneree4930b2017-05-02 22:07:37 +00001029 AttrBuilder B;
1030 B.addAttribute(Kind);
1031 return addAttributes(C, Index, B);
Reed Kotler795c7b42013-03-13 20:20:08 +00001032}
1033
Reid Klecknerb5180542017-03-21 16:57:19 +00001034AttributeList AttributeList::addAttribute(LLVMContext &C, unsigned Index,
1035 StringRef Kind,
1036 StringRef Value) const {
Eugene Zelenkodeaf6952017-02-17 00:00:09 +00001037 AttrBuilder B;
Bill Wendling3b2f6102013-07-25 18:34:24 +00001038 B.addAttribute(Kind, Value);
Reid Kleckneree4930b2017-05-02 22:07:37 +00001039 return addAttributes(C, Index, B);
Bill Wendling3b2f6102013-07-25 18:34:24 +00001040}
1041
Reid Kleckner5fbdd172017-05-31 19:23:09 +00001042AttributeList AttributeList::addAttribute(LLVMContext &C, unsigned Index,
Reid Klecknerb5180542017-03-21 16:57:19 +00001043 Attribute A) const {
Reid Kleckner5fbdd172017-05-31 19:23:09 +00001044 AttrBuilder B;
1045 B.addAttribute(A);
1046 return addAttributes(C, Index, B);
Akira Hatanaka237916b2015-12-02 06:58:49 +00001047}
1048
Reid Klecknerb5180542017-03-21 16:57:19 +00001049AttributeList AttributeList::addAttributes(LLVMContext &C, unsigned Index,
Reid Kleckner61906252017-04-19 01:51:13 +00001050 const AttrBuilder &B) const {
1051 if (!B.hasAttributes())
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00001052 return *this;
1053
Reid Klecknerfe64c012017-04-18 22:10:18 +00001054 if (!pImpl)
Reid Kleckner61906252017-04-19 01:51:13 +00001055 return AttributeList::get(C, {{Index, AttributeSet::get(C, B)}});
Reid Klecknerfe64c012017-04-18 22:10:18 +00001056
Bill Wendlingd509a662013-01-29 00:34:06 +00001057#ifndef NDEBUG
1058 // FIXME it is not obvious how this should work for alignment. For now, say
1059 // we can't change a known alignment.
Reid Klecknerbf6b3b152017-05-19 22:23:47 +00001060 unsigned OldAlign = getAttributes(Index).getAlignment();
Reid Kleckner61906252017-04-19 01:51:13 +00001061 unsigned NewAlign = B.getAlignment();
Bill Wendlingd509a662013-01-29 00:34:06 +00001062 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
1063 "Attempt to change alignment!");
1064#endif
1065
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001066 Index = attrIdxToArrayIdx(Index);
1067 SmallVector<AttributeSet, 4> AttrSets(this->begin(), this->end());
1068 if (Index >= AttrSets.size())
1069 AttrSets.resize(Index + 1);
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00001070
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001071 AttrBuilder Merged(AttrSets[Index]);
1072 Merged.merge(B);
1073 AttrSets[Index] = AttributeSet::get(C, Merged);
Bill Wendlingd509a662013-01-29 00:34:06 +00001074
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001075 return getImpl(C, AttrSets);
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00001076}
1077
Reid Kleckner5fbdd172017-05-31 19:23:09 +00001078AttributeList AttributeList::addParamAttribute(LLVMContext &C,
1079 ArrayRef<unsigned> ArgNos,
1080 Attribute A) const {
1081 assert(std::is_sorted(ArgNos.begin(), ArgNos.end()));
1082
1083 SmallVector<AttributeSet, 4> AttrSets(this->begin(), this->end());
1084 unsigned MaxIndex = attrIdxToArrayIdx(ArgNos.back() + FirstArgIndex);
1085 if (MaxIndex >= AttrSets.size())
1086 AttrSets.resize(MaxIndex + 1);
1087
1088 for (unsigned ArgNo : ArgNos) {
1089 unsigned Index = attrIdxToArrayIdx(ArgNo + FirstArgIndex);
1090 AttrBuilder B(AttrSets[Index]);
1091 B.addAttribute(A);
1092 AttrSets[Index] = AttributeSet::get(C, B);
1093 }
1094
1095 return getImpl(C, AttrSets);
1096}
1097
Reid Klecknerb5180542017-03-21 16:57:19 +00001098AttributeList AttributeList::removeAttribute(LLVMContext &C, unsigned Index,
1099 Attribute::AttrKind Kind) const {
Amaury Sechet392638d2016-06-14 20:27:35 +00001100 if (!hasAttribute(Index, Kind)) return *this;
Reid Kleckneree4930b2017-05-02 22:07:37 +00001101 AttrBuilder B;
1102 B.addAttribute(Kind);
1103 return removeAttributes(C, Index, B);
Bill Wendlingd509a662013-01-29 00:34:06 +00001104}
1105
Reid Klecknerb5180542017-03-21 16:57:19 +00001106AttributeList AttributeList::removeAttribute(LLVMContext &C, unsigned Index,
1107 StringRef Kind) const {
Amaury Sechet6100adf2016-06-15 17:50:39 +00001108 if (!hasAttribute(Index, Kind)) return *this;
Reid Kleckneree4930b2017-05-02 22:07:37 +00001109 AttrBuilder B;
1110 B.addAttribute(Kind);
1111 return removeAttributes(C, Index, B);
Bill Wendlingd509a662013-01-29 00:34:06 +00001112}
1113
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001114AttributeList
1115AttributeList::removeAttributes(LLVMContext &C, unsigned Index,
1116 const AttrBuilder &AttrsToRemove) const {
Reid Klecknerb5180542017-03-21 16:57:19 +00001117 if (!pImpl)
1118 return AttributeList();
Pete Cooperd2a44612015-05-06 23:19:43 +00001119
1120 // FIXME it is not obvious how this should work for alignment.
1121 // For now, say we can't pass in alignment, which no current use does.
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001122 assert(!AttrsToRemove.hasAlignmentAttr() && "Attempt to change alignment!");
Pete Cooperd2a44612015-05-06 23:19:43 +00001123
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001124 Index = attrIdxToArrayIdx(Index);
1125 SmallVector<AttributeSet, 4> AttrSets(this->begin(), this->end());
1126 if (Index >= AttrSets.size())
1127 AttrSets.resize(Index + 1);
Pete Cooperd2a44612015-05-06 23:19:43 +00001128
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001129 AttrBuilder B(AttrSets[Index]);
1130 B.remove(AttrsToRemove);
1131 AttrSets[Index] = AttributeSet::get(C, B);
Pete Cooperd2a44612015-05-06 23:19:43 +00001132
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001133 return getImpl(C, AttrSets);
Pete Cooperd2a44612015-05-06 23:19:43 +00001134}
1135
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00001136AttributeList AttributeList::removeAttributes(LLVMContext &C,
1137 unsigned WithoutIndex) const {
1138 if (!pImpl)
1139 return AttributeList();
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001140 WithoutIndex = attrIdxToArrayIdx(WithoutIndex);
1141 if (WithoutIndex >= getNumAttrSets())
1142 return *this;
1143 SmallVector<AttributeSet, 4> AttrSets(this->begin(), this->end());
1144 AttrSets[WithoutIndex] = AttributeSet();
1145 return getImpl(C, AttrSets);
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00001146}
1147
Reid Klecknerb5180542017-03-21 16:57:19 +00001148AttributeList AttributeList::addDereferenceableAttr(LLVMContext &C,
1149 unsigned Index,
1150 uint64_t Bytes) const {
Eugene Zelenkodeaf6952017-02-17 00:00:09 +00001151 AttrBuilder B;
Ramkumar Ramachandra8fcb4982015-02-14 19:37:54 +00001152 B.addDereferenceableAttr(Bytes);
Reid Kleckneree4930b2017-05-02 22:07:37 +00001153 return addAttributes(C, Index, B);
Ramkumar Ramachandra8fcb4982015-02-14 19:37:54 +00001154}
1155
Reid Klecknerb5180542017-03-21 16:57:19 +00001156AttributeList
1157AttributeList::addDereferenceableOrNullAttr(LLVMContext &C, unsigned Index,
1158 uint64_t Bytes) const {
Eugene Zelenkodeaf6952017-02-17 00:00:09 +00001159 AttrBuilder B;
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001160 B.addDereferenceableOrNullAttr(Bytes);
Reid Kleckneree4930b2017-05-02 22:07:37 +00001161 return addAttributes(C, Index, B);
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001162}
1163
Reid Klecknerb5180542017-03-21 16:57:19 +00001164AttributeList
1165AttributeList::addAllocSizeAttr(LLVMContext &C, unsigned Index,
1166 unsigned ElemSizeArg,
1167 const Optional<unsigned> &NumElemsArg) {
Eugene Zelenkodeaf6952017-02-17 00:00:09 +00001168 AttrBuilder B;
George Burgess IV278199f2016-04-12 01:05:35 +00001169 B.addAllocSizeAttr(ElemSizeArg, NumElemsArg);
Reid Kleckneree4930b2017-05-02 22:07:37 +00001170 return addAttributes(C, Index, B);
George Burgess IV278199f2016-04-12 01:05:35 +00001171}
1172
Bill Wendlingd509a662013-01-29 00:34:06 +00001173//===----------------------------------------------------------------------===//
Reid Klecknerb5180542017-03-21 16:57:19 +00001174// AttributeList Accessor Methods
Bill Wendlingd509a662013-01-29 00:34:06 +00001175//===----------------------------------------------------------------------===//
1176
Reid Klecknerb5180542017-03-21 16:57:19 +00001177LLVMContext &AttributeList::getContext() const { return pImpl->getContext(); }
1178
Reid Klecknerf021fab2017-04-13 23:12:13 +00001179AttributeSet AttributeList::getParamAttributes(unsigned ArgNo) const {
Reid Klecknera0b45f42017-05-03 18:17:31 +00001180 return getAttributes(ArgNo + FirstArgIndex);
Bill Wendling5d020a32013-02-10 05:00:40 +00001181}
1182
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001183AttributeSet AttributeList::getRetAttributes() const {
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00001184 return getAttributes(ReturnIndex);
Bill Wendlingd509a662013-01-29 00:34:06 +00001185}
1186
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001187AttributeSet AttributeList::getFnAttributes() const {
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00001188 return getAttributes(FunctionIndex);
Bill Wendlingd509a662013-01-29 00:34:06 +00001189}
1190
Reid Klecknerb5180542017-03-21 16:57:19 +00001191bool AttributeList::hasAttribute(unsigned Index,
1192 Attribute::AttrKind Kind) const {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001193 return getAttributes(Index).hasAttribute(Kind);
Bill Wendlingd509a662013-01-29 00:34:06 +00001194}
1195
Reid Klecknerb5180542017-03-21 16:57:19 +00001196bool AttributeList::hasAttribute(unsigned Index, StringRef Kind) const {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001197 return getAttributes(Index).hasAttribute(Kind);
Bill Wendlingbce7b972013-02-13 08:42:21 +00001198}
1199
Reid Klecknerb5180542017-03-21 16:57:19 +00001200bool AttributeList::hasAttributes(unsigned Index) const {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001201 return getAttributes(Index).hasAttributes();
Bill Wendlingd509a662013-01-29 00:34:06 +00001202}
1203
Reid Klecknerb5180542017-03-21 16:57:19 +00001204bool AttributeList::hasFnAttribute(Attribute::AttrKind Kind) const {
Matthias Braun33282812016-01-29 22:25:19 +00001205 return pImpl && pImpl->hasFnAttribute(Kind);
1206}
1207
Reid Klecknerb5180542017-03-21 16:57:19 +00001208bool AttributeList::hasFnAttribute(StringRef Kind) const {
1209 return hasAttribute(AttributeList::FunctionIndex, Kind);
Amaury Sechet5f04d812016-09-09 04:50:38 +00001210}
1211
Reid Klecknerf021fab2017-04-13 23:12:13 +00001212bool AttributeList::hasParamAttribute(unsigned ArgNo,
1213 Attribute::AttrKind Kind) const {
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001214 return hasAttribute(ArgNo + FirstArgIndex, Kind);
Reid Klecknerf021fab2017-04-13 23:12:13 +00001215}
1216
Reid Klecknerb5180542017-03-21 16:57:19 +00001217bool AttributeList::hasAttrSomewhere(Attribute::AttrKind Attr,
1218 unsigned *Index) const {
Craig Topperc6207612014-04-09 06:08:46 +00001219 if (!pImpl) return false;
Bill Wendlingd509a662013-01-29 00:34:06 +00001220
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001221 for (unsigned I = index_begin(), E = index_end(); I != E; ++I) {
1222 if (hasAttribute(I, Attr)) {
1223 if (Index)
1224 *Index = I;
1225 return true;
1226 }
1227 }
Bill Wendlingd509a662013-01-29 00:34:06 +00001228
1229 return false;
1230}
1231
Reid Klecknerb5180542017-03-21 16:57:19 +00001232Attribute AttributeList::getAttribute(unsigned Index,
1233 Attribute::AttrKind Kind) const {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001234 return getAttributes(Index).getAttribute(Kind);
Bill Wendlingbce7b972013-02-13 08:42:21 +00001235}
1236
Reid Klecknerb5180542017-03-21 16:57:19 +00001237Attribute AttributeList::getAttribute(unsigned Index, StringRef Kind) const {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001238 return getAttributes(Index).getAttribute(Kind);
Bill Wendlingbce7b972013-02-13 08:42:21 +00001239}
1240
Reid Kleckner859f8b52017-04-28 20:34:27 +00001241unsigned AttributeList::getRetAlignment() const {
1242 return getAttributes(ReturnIndex).getAlignment();
1243}
1244
1245unsigned AttributeList::getParamAlignment(unsigned ArgNo) const {
Reid Klecknera0b45f42017-05-03 18:17:31 +00001246 return getAttributes(ArgNo + FirstArgIndex).getAlignment();
Bill Wendlingd509a662013-01-29 00:34:06 +00001247}
1248
Reid Klecknerb5180542017-03-21 16:57:19 +00001249unsigned AttributeList::getStackAlignment(unsigned Index) const {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001250 return getAttributes(Index).getStackAlignment();
Bill Wendlingd509a662013-01-29 00:34:06 +00001251}
1252
Reid Klecknerb5180542017-03-21 16:57:19 +00001253uint64_t AttributeList::getDereferenceableBytes(unsigned Index) const {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001254 return getAttributes(Index).getDereferenceableBytes();
Hal Finkelb0407ba2014-07-18 15:51:28 +00001255}
1256
Reid Klecknerb5180542017-03-21 16:57:19 +00001257uint64_t AttributeList::getDereferenceableOrNullBytes(unsigned Index) const {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001258 return getAttributes(Index).getDereferenceableOrNullBytes();
Sanjoy Das06cf33f2015-05-06 17:41:54 +00001259}
1260
George Burgess IV278199f2016-04-12 01:05:35 +00001261std::pair<unsigned, Optional<unsigned>>
Reid Klecknerb5180542017-03-21 16:57:19 +00001262AttributeList::getAllocSizeArgs(unsigned Index) const {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001263 return getAttributes(Index).getAllocSizeArgs();
George Burgess IV278199f2016-04-12 01:05:35 +00001264}
1265
Reid Klecknerb5180542017-03-21 16:57:19 +00001266std::string AttributeList::getAsString(unsigned Index, bool InAttrGrp) const {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001267 return getAttributes(Index).getAsString(InAttrGrp);
Bill Wendlingd509a662013-01-29 00:34:06 +00001268}
1269
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001270AttributeSet AttributeList::getAttributes(unsigned Index) const {
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001271 Index = attrIdxToArrayIdx(Index);
1272 if (!pImpl || Index >= getNumAttrSets())
1273 return AttributeSet();
1274 return pImpl->begin()[Index];
Bill Wendlingd509a662013-01-29 00:34:06 +00001275}
1276
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001277AttributeList::iterator AttributeList::begin() const {
1278 return pImpl ? pImpl->begin() : nullptr;
Bill Wendlinga9174862013-01-31 23:53:05 +00001279}
1280
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001281AttributeList::iterator AttributeList::end() const {
1282 return pImpl ? pImpl->end() : nullptr;
Bill Wendlinga9174862013-01-31 23:53:05 +00001283}
1284
Bill Wendlingd509a662013-01-29 00:34:06 +00001285//===----------------------------------------------------------------------===//
Reid Klecknerb5180542017-03-21 16:57:19 +00001286// AttributeList Introspection Methods
Bill Wendlingd509a662013-01-29 00:34:06 +00001287//===----------------------------------------------------------------------===//
1288
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001289unsigned AttributeList::getNumAttrSets() const {
1290 return pImpl ? pImpl->NumAttrSets : 0;
Bill Wendlingd509a662013-01-29 00:34:06 +00001291}
1292
Aaron Ballman615eb472017-10-15 14:32:27 +00001293#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Reid Klecknerb5180542017-03-21 16:57:19 +00001294LLVM_DUMP_METHOD void AttributeList::dump() const {
Bill Wendlingd509a662013-01-29 00:34:06 +00001295 dbgs() << "PAL[\n";
1296
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001297 for (unsigned i = index_begin(), e = index_end(); i != e; ++i) {
1298 if (getAttributes(i).hasAttributes())
1299 dbgs() << " { " << i << " => " << getAsString(i) << " }\n";
Bill Wendlingd509a662013-01-29 00:34:06 +00001300 }
1301
1302 dbgs() << "]\n";
1303}
Matthias Braun8c209aa2017-01-28 02:02:38 +00001304#endif
Bill Wendlingd509a662013-01-29 00:34:06 +00001305
Bill Wendlingabf3feb2012-10-05 06:44:41 +00001306//===----------------------------------------------------------------------===//
Bill Wendlingcd330342013-01-04 23:27:34 +00001307// AttrBuilder Method Implementations
Bill Wendlingabf3feb2012-10-05 06:44:41 +00001308//===----------------------------------------------------------------------===//
1309
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001310// FIXME: Remove this ctor, use AttributeSet.
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001311AttrBuilder::AttrBuilder(AttributeList AL, unsigned Index) {
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001312 AttributeSet AS = AL.getAttributes(Index);
1313 for (const Attribute &A : AS)
1314 addAttribute(A);
Bill Wendling096f5442013-01-07 08:24:35 +00001315}
1316
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001317AttrBuilder::AttrBuilder(AttributeSet AS) {
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001318 for (const Attribute &A : AS)
1319 addAttribute(A);
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00001320}
1321
Bill Wendlingcd330342013-01-04 23:27:34 +00001322void AttrBuilder::clear() {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001323 Attrs.reset();
Sanjoy Das88d0fde2015-09-03 22:27:42 +00001324 TargetDepAttrs.clear();
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001325 Alignment = StackAlignment = DerefBytes = DerefOrNullBytes = 0;
George Burgess IV278199f2016-04-12 01:05:35 +00001326 AllocSizeArgs = 0;
Bill Wendlingcd330342013-01-04 23:27:34 +00001327}
1328
1329AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001330 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
Bill Wendling1c7cc8a2013-01-31 23:16:25 +00001331 assert(Val != Attribute::Alignment && Val != Attribute::StackAlignment &&
George Burgess IV278199f2016-04-12 01:05:35 +00001332 Val != Attribute::Dereferenceable && Val != Attribute::AllocSize &&
Hal Finkelb0407ba2014-07-18 15:51:28 +00001333 "Adding integer attribute without adding a value!");
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001334 Attrs[Val] = true;
Bill Wendling7c04e042012-10-09 19:01:18 +00001335 return *this;
Bill Wendlingabf3feb2012-10-05 06:44:41 +00001336}
1337
Bill Wendling23804da2013-01-31 23:38:01 +00001338AttrBuilder &AttrBuilder::addAttribute(Attribute Attr) {
Bill Wendling0a437302013-02-10 10:13:23 +00001339 if (Attr.isStringAttribute()) {
1340 addAttribute(Attr.getKindAsString(), Attr.getValueAsString());
1341 return *this;
1342 }
1343
Bill Wendling3f12ac22013-02-05 22:37:24 +00001344 Attribute::AttrKind Kind = Attr.getKindAsEnum();
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001345 Attrs[Kind] = true;
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001346
Bill Wendling3f12ac22013-02-05 22:37:24 +00001347 if (Kind == Attribute::Alignment)
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001348 Alignment = Attr.getAlignment();
Bill Wendling3f12ac22013-02-05 22:37:24 +00001349 else if (Kind == Attribute::StackAlignment)
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001350 StackAlignment = Attr.getStackAlignment();
Hal Finkelb0407ba2014-07-18 15:51:28 +00001351 else if (Kind == Attribute::Dereferenceable)
1352 DerefBytes = Attr.getDereferenceableBytes();
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001353 else if (Kind == Attribute::DereferenceableOrNull)
1354 DerefOrNullBytes = Attr.getDereferenceableOrNullBytes();
George Burgess IV278199f2016-04-12 01:05:35 +00001355 else if (Kind == Attribute::AllocSize)
1356 AllocSizeArgs = Attr.getValueAsInt();
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001357 return *this;
1358}
1359
Bill Wendlingb9c5b1a2013-02-05 08:09:32 +00001360AttrBuilder &AttrBuilder::addAttribute(StringRef A, StringRef V) {
1361 TargetDepAttrs[A] = V;
1362 return *this;
1363}
1364
Bill Wendling23804da2013-01-31 23:38:01 +00001365AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001366 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
1367 Attrs[Val] = false;
Bill Wendling23804da2013-01-31 23:38:01 +00001368
1369 if (Val == Attribute::Alignment)
1370 Alignment = 0;
1371 else if (Val == Attribute::StackAlignment)
1372 StackAlignment = 0;
Hal Finkelb0407ba2014-07-18 15:51:28 +00001373 else if (Val == Attribute::Dereferenceable)
1374 DerefBytes = 0;
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001375 else if (Val == Attribute::DereferenceableOrNull)
1376 DerefOrNullBytes = 0;
George Burgess IV278199f2016-04-12 01:05:35 +00001377 else if (Val == Attribute::AllocSize)
1378 AllocSizeArgs = 0;
Bill Wendling23804da2013-01-31 23:38:01 +00001379
1380 return *this;
1381}
1382
Reid Klecknerb5180542017-03-21 16:57:19 +00001383AttrBuilder &AttrBuilder::removeAttributes(AttributeList A, uint64_t Index) {
Reid Kleckner6652a522017-04-28 18:37:16 +00001384 remove(A.getAttributes(Index));
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001385 return *this;
1386}
1387
Bill Wendlingb9c5b1a2013-02-05 08:09:32 +00001388AttrBuilder &AttrBuilder::removeAttribute(StringRef A) {
1389 std::map<std::string, std::string>::iterator I = TargetDepAttrs.find(A);
1390 if (I != TargetDepAttrs.end())
1391 TargetDepAttrs.erase(I);
1392 return *this;
1393}
1394
George Burgess IV278199f2016-04-12 01:05:35 +00001395std::pair<unsigned, Optional<unsigned>> AttrBuilder::getAllocSizeArgs() const {
1396 return unpackAllocSizeArgs(AllocSizeArgs);
1397}
1398
Bill Wendling50d27842012-10-15 20:35:56 +00001399AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
Bill Wendlingabd5ba22012-10-14 03:58:29 +00001400 if (Align == 0) return *this;
Bill Wendlingcd330342013-01-04 23:27:34 +00001401
Bill Wendlingabf3feb2012-10-05 06:44:41 +00001402 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1403 assert(Align <= 0x40000000 && "Alignment too large.");
Bill Wendlingcd330342013-01-04 23:27:34 +00001404
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001405 Attrs[Attribute::Alignment] = true;
Bill Wendlingcd330342013-01-04 23:27:34 +00001406 Alignment = Align;
Bill Wendlingabd5ba22012-10-14 03:58:29 +00001407 return *this;
Chris Lattner3e13b8c2008-01-02 23:42:30 +00001408}
1409
Bill Wendlingcd330342013-01-04 23:27:34 +00001410AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
1411 // Default alignment, allow the target to define how to align it.
1412 if (Align == 0) return *this;
1413
1414 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1415 assert(Align <= 0x100 && "Alignment too large.");
1416
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001417 Attrs[Attribute::StackAlignment] = true;
Bill Wendlingcd330342013-01-04 23:27:34 +00001418 StackAlignment = Align;
1419 return *this;
1420}
1421
Hal Finkelb0407ba2014-07-18 15:51:28 +00001422AttrBuilder &AttrBuilder::addDereferenceableAttr(uint64_t Bytes) {
1423 if (Bytes == 0) return *this;
1424
1425 Attrs[Attribute::Dereferenceable] = true;
1426 DerefBytes = Bytes;
1427 return *this;
1428}
1429
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001430AttrBuilder &AttrBuilder::addDereferenceableOrNullAttr(uint64_t Bytes) {
1431 if (Bytes == 0)
1432 return *this;
1433
1434 Attrs[Attribute::DereferenceableOrNull] = true;
1435 DerefOrNullBytes = Bytes;
1436 return *this;
1437}
1438
George Burgess IV278199f2016-04-12 01:05:35 +00001439AttrBuilder &AttrBuilder::addAllocSizeAttr(unsigned ElemSize,
1440 const Optional<unsigned> &NumElems) {
1441 return addAllocSizeAttrFromRawRepr(packAllocSizeArgs(ElemSize, NumElems));
1442}
1443
1444AttrBuilder &AttrBuilder::addAllocSizeAttrFromRawRepr(uint64_t RawArgs) {
1445 // (0, 0) is our "not present" value, so we need to check for it here.
1446 assert(RawArgs && "Invalid allocsize arguments -- given allocsize(0, 0)");
1447
1448 Attrs[Attribute::AllocSize] = true;
1449 // Reuse existing machinery to store this as a single 64-bit integer so we can
1450 // save a few bytes over using a pair<unsigned, Optional<unsigned>>.
1451 AllocSizeArgs = RawArgs;
1452 return *this;
1453}
1454
Bill Wendlinge2614922013-02-06 01:16:00 +00001455AttrBuilder &AttrBuilder::merge(const AttrBuilder &B) {
1456 // FIXME: What if both have alignments, but they don't match?!
1457 if (!Alignment)
1458 Alignment = B.Alignment;
1459
1460 if (!StackAlignment)
1461 StackAlignment = B.StackAlignment;
1462
Hal Finkelb0407ba2014-07-18 15:51:28 +00001463 if (!DerefBytes)
1464 DerefBytes = B.DerefBytes;
1465
Pete Cooperd2a44612015-05-06 23:19:43 +00001466 if (!DerefOrNullBytes)
1467 DerefOrNullBytes = B.DerefOrNullBytes;
1468
George Burgess IV278199f2016-04-12 01:05:35 +00001469 if (!AllocSizeArgs)
1470 AllocSizeArgs = B.AllocSizeArgs;
1471
Benjamin Kramer45e7d532013-02-16 19:13:18 +00001472 Attrs |= B.Attrs;
Bill Wendlinge2614922013-02-06 01:16:00 +00001473
Pete Cooperd2a44612015-05-06 23:19:43 +00001474 for (auto I : B.td_attrs())
1475 TargetDepAttrs[I.first] = I.second;
Bill Wendlinge2614922013-02-06 01:16:00 +00001476
1477 return *this;
1478}
1479
Pete Cooperd2a44612015-05-06 23:19:43 +00001480AttrBuilder &AttrBuilder::remove(const AttrBuilder &B) {
1481 // FIXME: What if both have alignments, but they don't match?!
1482 if (B.Alignment)
1483 Alignment = 0;
1484
1485 if (B.StackAlignment)
1486 StackAlignment = 0;
1487
1488 if (B.DerefBytes)
1489 DerefBytes = 0;
1490
1491 if (B.DerefOrNullBytes)
1492 DerefOrNullBytes = 0;
1493
George Burgess IV278199f2016-04-12 01:05:35 +00001494 if (B.AllocSizeArgs)
1495 AllocSizeArgs = 0;
1496
Pete Cooperd2a44612015-05-06 23:19:43 +00001497 Attrs &= ~B.Attrs;
1498
1499 for (auto I : B.td_attrs())
1500 TargetDepAttrs.erase(I.first);
1501
1502 return *this;
1503}
1504
1505bool AttrBuilder::overlaps(const AttrBuilder &B) const {
1506 // First check if any of the target independent attributes overlap.
1507 if ((Attrs & B.Attrs).any())
1508 return true;
1509
1510 // Then check if any target dependent ones do.
Sean Silva9011aca2017-02-22 06:34:04 +00001511 for (const auto &I : td_attrs())
Pete Cooperd2a44612015-05-06 23:19:43 +00001512 if (B.contains(I.first))
1513 return true;
1514
1515 return false;
1516}
1517
Bill Wendling4b001442013-02-06 01:33:42 +00001518bool AttrBuilder::contains(StringRef A) const {
1519 return TargetDepAttrs.find(A) != TargetDepAttrs.end();
1520}
1521
Bill Wendling50d27842012-10-15 20:35:56 +00001522bool AttrBuilder::hasAttributes() const {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001523 return !Attrs.none() || !TargetDepAttrs.empty();
Bill Wendlingc6daefa2012-10-08 23:27:46 +00001524}
Bill Wendling9ac69f92013-01-04 20:54:35 +00001525
Reid Kleckner6652a522017-04-28 18:37:16 +00001526bool AttrBuilder::hasAttributes(AttributeList AL, uint64_t Index) const {
1527 AttributeSet AS = AL.getAttributes(Index);
Bill Wendling9ca01da2013-02-02 00:42:06 +00001528
Reid Kleckner6652a522017-04-28 18:37:16 +00001529 for (Attribute Attr : AS) {
Hal Finkele15442c2014-07-18 06:51:55 +00001530 if (Attr.isEnumAttribute() || Attr.isIntAttribute()) {
Reid Kleckner6652a522017-04-28 18:37:16 +00001531 if (contains(Attr.getKindAsEnum()))
Bill Wendling7cde51d2013-02-12 07:56:49 +00001532 return true;
1533 } else {
1534 assert(Attr.isStringAttribute() && "Invalid attribute kind!");
Reid Kleckner6652a522017-04-28 18:37:16 +00001535 return contains(Attr.getKindAsString());
Bill Wendling7cde51d2013-02-12 07:56:49 +00001536 }
1537 }
Bill Wendling9ca01da2013-02-02 00:42:06 +00001538
1539 return false;
Bill Wendling70f39172012-10-09 00:01:21 +00001540}
Bill Wendling9ac69f92013-01-04 20:54:35 +00001541
Bill Wendling50d27842012-10-15 20:35:56 +00001542bool AttrBuilder::hasAlignmentAttr() const {
Bill Wendlingcd330342013-01-04 23:27:34 +00001543 return Alignment != 0;
Bill Wendlingc6daefa2012-10-08 23:27:46 +00001544}
1545
Bill Wendlingd509a662013-01-29 00:34:06 +00001546bool AttrBuilder::operator==(const AttrBuilder &B) {
Benjamin Kramer45e7d532013-02-16 19:13:18 +00001547 if (Attrs != B.Attrs)
1548 return false;
Bill Wendling4b001442013-02-06 01:33:42 +00001549
1550 for (td_const_iterator I = TargetDepAttrs.begin(),
1551 E = TargetDepAttrs.end(); I != E; ++I)
1552 if (B.TargetDepAttrs.find(I->first) == B.TargetDepAttrs.end())
1553 return false;
1554
Hal Finkelb0407ba2014-07-18 15:51:28 +00001555 return Alignment == B.Alignment && StackAlignment == B.StackAlignment &&
1556 DerefBytes == B.DerefBytes;
Bill Wendlingd509a662013-01-29 00:34:06 +00001557}
1558
Bill Wendling57625a42013-01-25 23:09:36 +00001559//===----------------------------------------------------------------------===//
1560// AttributeFuncs Function Defintions
1561//===----------------------------------------------------------------------===//
1562
Bill Wendlingc79cdff2013-02-01 01:04:27 +00001563/// \brief Which attributes cannot be applied to a type.
Craig Toppere3dcce92015-08-01 22:20:21 +00001564AttrBuilder AttributeFuncs::typeIncompatible(Type *Ty) {
Bill Wendling57625a42013-01-25 23:09:36 +00001565 AttrBuilder Incompatible;
1566
1567 if (!Ty->isIntegerTy())
1568 // Attribute that only apply to integers.
1569 Incompatible.addAttribute(Attribute::SExt)
1570 .addAttribute(Attribute::ZExt);
1571
1572 if (!Ty->isPointerTy())
1573 // Attribute that only apply to pointers.
1574 Incompatible.addAttribute(Attribute::ByVal)
1575 .addAttribute(Attribute::Nest)
1576 .addAttribute(Attribute::NoAlias)
1577 .addAttribute(Attribute::NoCapture)
Nick Lewyckyd52b1522014-05-20 01:23:40 +00001578 .addAttribute(Attribute::NonNull)
Hal Finkelb0407ba2014-07-18 15:51:28 +00001579 .addDereferenceableAttr(1) // the int here is ignored
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001580 .addDereferenceableOrNullAttr(1) // the int here is ignored
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001581 .addAttribute(Attribute::ReadNone)
1582 .addAttribute(Attribute::ReadOnly)
Reid Klecknera534a382013-12-19 02:14:12 +00001583 .addAttribute(Attribute::StructRet)
1584 .addAttribute(Attribute::InAlloca);
Bill Wendling57625a42013-01-25 23:09:36 +00001585
Pete Cooper2777d8872015-05-06 23:19:56 +00001586 return Incompatible;
Bill Wendling57625a42013-01-25 23:09:36 +00001587}
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001588
1589template<typename AttrClass>
1590static bool isEqual(const Function &Caller, const Function &Callee) {
1591 return Caller.getFnAttribute(AttrClass::getKind()) ==
1592 Callee.getFnAttribute(AttrClass::getKind());
1593}
1594
1595/// \brief Compute the logical AND of the attributes of the caller and the
1596/// callee.
1597///
1598/// This function sets the caller's attribute to false if the callee's attribute
1599/// is false.
1600template<typename AttrClass>
1601static void setAND(Function &Caller, const Function &Callee) {
1602 if (AttrClass::isSet(Caller, AttrClass::getKind()) &&
1603 !AttrClass::isSet(Callee, AttrClass::getKind()))
1604 AttrClass::set(Caller, AttrClass::getKind(), false);
1605}
1606
1607/// \brief Compute the logical OR of the attributes of the caller and the
1608/// callee.
1609///
1610/// This function sets the caller's attribute to true if the callee's attribute
1611/// is true.
1612template<typename AttrClass>
1613static void setOR(Function &Caller, const Function &Callee) {
1614 if (!AttrClass::isSet(Caller, AttrClass::getKind()) &&
1615 AttrClass::isSet(Callee, AttrClass::getKind()))
1616 AttrClass::set(Caller, AttrClass::getKind(), true);
1617}
1618
1619/// \brief If the inlined function had a higher stack protection level than the
1620/// calling function, then bump up the caller's stack protection level.
1621static void adjustCallerSSPLevel(Function &Caller, const Function &Callee) {
1622 // If upgrading the SSP attribute, clear out the old SSP Attributes first.
1623 // Having multiple SSP attributes doesn't actually hurt, but it adds useless
1624 // clutter to the IR.
Reid Kleckneree4930b2017-05-02 22:07:37 +00001625 AttrBuilder OldSSPAttr;
1626 OldSSPAttr.addAttribute(Attribute::StackProtect)
1627 .addAttribute(Attribute::StackProtectStrong)
1628 .addAttribute(Attribute::StackProtectReq);
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001629
Evgeniy Stepanovf17120a2016-04-11 22:27:48 +00001630 if (Callee.hasFnAttribute(Attribute::StackProtectReq)) {
Reid Klecknerb5180542017-03-21 16:57:19 +00001631 Caller.removeAttributes(AttributeList::FunctionIndex, OldSSPAttr);
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001632 Caller.addFnAttr(Attribute::StackProtectReq);
1633 } else if (Callee.hasFnAttribute(Attribute::StackProtectStrong) &&
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001634 !Caller.hasFnAttribute(Attribute::StackProtectReq)) {
Reid Klecknerb5180542017-03-21 16:57:19 +00001635 Caller.removeAttributes(AttributeList::FunctionIndex, OldSSPAttr);
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001636 Caller.addFnAttr(Attribute::StackProtectStrong);
1637 } else if (Callee.hasFnAttribute(Attribute::StackProtect) &&
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001638 !Caller.hasFnAttribute(Attribute::StackProtectReq) &&
1639 !Caller.hasFnAttribute(Attribute::StackProtectStrong))
1640 Caller.addFnAttr(Attribute::StackProtect);
1641}
1642
whitequarked54b4a2017-06-21 18:46:50 +00001643/// \brief If the inlined function required stack probes, then ensure that
1644/// the calling function has those too.
1645static void adjustCallerStackProbes(Function &Caller, const Function &Callee) {
whitequark08b20352017-06-22 23:22:36 +00001646 if (!Caller.hasFnAttribute("probe-stack") &&
1647 Callee.hasFnAttribute("probe-stack")) {
1648 Caller.addFnAttr(Callee.getFnAttribute("probe-stack"));
1649 }
1650}
1651
1652/// \brief If the inlined function defines the size of guard region
1653/// on the stack, then ensure that the calling function defines a guard region
1654/// that is no larger.
1655static void
1656adjustCallerStackProbeSize(Function &Caller, const Function &Callee) {
1657 if (Callee.hasFnAttribute("stack-probe-size")) {
1658 uint64_t CalleeStackProbeSize;
1659 Callee.getFnAttribute("stack-probe-size")
1660 .getValueAsString()
1661 .getAsInteger(0, CalleeStackProbeSize);
1662 if (Caller.hasFnAttribute("stack-probe-size")) {
1663 uint64_t CallerStackProbeSize;
1664 Caller.getFnAttribute("stack-probe-size")
1665 .getValueAsString()
1666 .getAsInteger(0, CallerStackProbeSize);
1667 if (CallerStackProbeSize > CalleeStackProbeSize) {
1668 Caller.addFnAttr(Callee.getFnAttribute("stack-probe-size"));
1669 }
1670 } else {
1671 Caller.addFnAttr(Callee.getFnAttribute("stack-probe-size"));
1672 }
1673 }
whitequarked54b4a2017-06-21 18:46:50 +00001674}
1675
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001676#define GET_ATTR_COMPAT_FUNC
1677#include "AttributesCompatFunc.inc"
1678
1679bool AttributeFuncs::areInlineCompatible(const Function &Caller,
1680 const Function &Callee) {
1681 return hasCompatibleFnAttrs(Caller, Callee);
1682}
1683
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001684void AttributeFuncs::mergeAttributesForInlining(Function &Caller,
1685 const Function &Callee) {
1686 mergeFnAttrs(Caller, Callee);
1687}