blob: adb31d127a2ec3509e971a5e10ee92a3812dd4a9 [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
Bill Wendling4607f4b2012-12-20 01:36:59 +000016#include "AttributeImpl.h"
Bill Wendlinge38b8042012-09-26 21:07:29 +000017#include "LLVMContextImpl.h"
Eugene Zelenkodeaf6952017-02-17 00:00:09 +000018#include "llvm/ADT/ArrayRef.h"
19#include "llvm/ADT/FoldingSet.h"
20#include "llvm/ADT/Optional.h"
21#include "llvm/ADT/SmallVector.h"
Benjamin Kramer502b9e12014-04-12 16:15:53 +000022#include "llvm/ADT/STLExtras.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000023#include "llvm/ADT/StringExtras.h"
Eugene Zelenkodeaf6952017-02-17 00:00:09 +000024#include "llvm/ADT/StringRef.h"
25#include "llvm/ADT/Twine.h"
26#include "llvm/IR/Attributes.h"
27#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";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000248 if (hasAttribute(Attribute::AlwaysInline))
249 return "alwaysinline";
Igor Laevsky39d662f2015-07-11 10:30:36 +0000250 if (hasAttribute(Attribute::ArgMemOnly))
251 return "argmemonly";
Michael Gottesman41748d72013-06-27 00:25:01 +0000252 if (hasAttribute(Attribute::Builtin))
253 return "builtin";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000254 if (hasAttribute(Attribute::ByVal))
255 return "byval";
Owen Anderson85fa7d52015-05-26 23:48:40 +0000256 if (hasAttribute(Attribute::Convergent))
257 return "convergent";
Manman Ren9bfd0d02016-04-01 21:41:15 +0000258 if (hasAttribute(Attribute::SwiftError))
259 return "swifterror";
Manman Renf46262e2016-03-29 17:37:21 +0000260 if (hasAttribute(Attribute::SwiftSelf))
261 return "swiftself";
Vaivaswatha Nagarajfb3f4902015-12-16 16:16:19 +0000262 if (hasAttribute(Attribute::InaccessibleMemOnly))
263 return "inaccessiblememonly";
264 if (hasAttribute(Attribute::InaccessibleMemOrArgMemOnly))
265 return "inaccessiblemem_or_argmemonly";
Reid Klecknera534a382013-12-19 02:14:12 +0000266 if (hasAttribute(Attribute::InAlloca))
267 return "inalloca";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000268 if (hasAttribute(Attribute::InlineHint))
269 return "inlinehint";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000270 if (hasAttribute(Attribute::InReg))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000271 return "inreg";
Tom Roeder44cb65f2014-06-05 19:29:43 +0000272 if (hasAttribute(Attribute::JumpTable))
273 return "jumptable";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000274 if (hasAttribute(Attribute::MinSize))
275 return "minsize";
276 if (hasAttribute(Attribute::Naked))
277 return "naked";
278 if (hasAttribute(Attribute::Nest))
279 return "nest";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000280 if (hasAttribute(Attribute::NoAlias))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000281 return "noalias";
Bill Wendling09bd1f72013-02-22 00:12:35 +0000282 if (hasAttribute(Attribute::NoBuiltin))
283 return "nobuiltin";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000284 if (hasAttribute(Attribute::NoCapture))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000285 return "nocapture";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000286 if (hasAttribute(Attribute::NoDuplicate))
287 return "noduplicate";
288 if (hasAttribute(Attribute::NoImplicitFloat))
289 return "noimplicitfloat";
290 if (hasAttribute(Attribute::NoInline))
291 return "noinline";
292 if (hasAttribute(Attribute::NonLazyBind))
293 return "nonlazybind";
Nick Lewyckyd52b1522014-05-20 01:23:40 +0000294 if (hasAttribute(Attribute::NonNull))
295 return "nonnull";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000296 if (hasAttribute(Attribute::NoRedZone))
297 return "noredzone";
298 if (hasAttribute(Attribute::NoReturn))
299 return "noreturn";
James Molloye6f87ca2015-11-06 10:32:53 +0000300 if (hasAttribute(Attribute::NoRecurse))
301 return "norecurse";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000302 if (hasAttribute(Attribute::NoUnwind))
303 return "nounwind";
Andrea Di Biagio377496b2013-08-23 11:53:55 +0000304 if (hasAttribute(Attribute::OptimizeNone))
305 return "optnone";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000306 if (hasAttribute(Attribute::OptimizeForSize))
307 return "optsize";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000308 if (hasAttribute(Attribute::ReadNone))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000309 return "readnone";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000310 if (hasAttribute(Attribute::ReadOnly))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000311 return "readonly";
Nicolai Haehnle84c9f992016-07-04 08:01:29 +0000312 if (hasAttribute(Attribute::WriteOnly))
313 return "writeonly";
Stephen Linb8bd2322013-04-20 05:14:40 +0000314 if (hasAttribute(Attribute::Returned))
315 return "returned";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000316 if (hasAttribute(Attribute::ReturnsTwice))
317 return "returns_twice";
318 if (hasAttribute(Attribute::SExt))
319 return "signext";
Matt Arsenaultb19b57e2017-04-28 20:25:27 +0000320 if (hasAttribute(Attribute::Speculatable))
321 return "speculatable";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000322 if (hasAttribute(Attribute::StackProtect))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000323 return "ssp";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000324 if (hasAttribute(Attribute::StackProtectReq))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000325 return "sspreq";
Bill Wendlingd154e2832013-01-23 06:41:41 +0000326 if (hasAttribute(Attribute::StackProtectStrong))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000327 return "sspstrong";
Peter Collingbourne82437bf2015-06-15 21:07:11 +0000328 if (hasAttribute(Attribute::SafeStack))
329 return "safestack";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000330 if (hasAttribute(Attribute::StructRet))
331 return "sret";
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000332 if (hasAttribute(Attribute::SanitizeThread))
333 return "sanitize_thread";
334 if (hasAttribute(Attribute::SanitizeMemory))
335 return "sanitize_memory";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000336 if (hasAttribute(Attribute::UWTable))
337 return "uwtable";
338 if (hasAttribute(Attribute::ZExt))
339 return "zeroext";
Diego Novilloc6399532013-05-24 12:26:52 +0000340 if (hasAttribute(Attribute::Cold))
341 return "cold";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000342
343 // FIXME: These should be output like this:
344 //
345 // align=4
346 // alignstack=8
347 //
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000348 if (hasAttribute(Attribute::Alignment)) {
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000349 std::string Result;
Bill Wendling829b4782013-02-11 08:43:33 +0000350 Result += "align";
351 Result += (InAttrGrp) ? "=" : " ";
Bill Wendling3f12ac22013-02-05 22:37:24 +0000352 Result += utostr(getValueAsInt());
353 return Result;
354 }
Bill Wendling829b4782013-02-11 08:43:33 +0000355
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000356 auto AttrWithBytesToString = [&](const char *Name) {
Bill Wendling3f12ac22013-02-05 22:37:24 +0000357 std::string Result;
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000358 Result += Name;
Bill Wendling829b4782013-02-11 08:43:33 +0000359 if (InAttrGrp) {
360 Result += "=";
361 Result += utostr(getValueAsInt());
362 } else {
363 Result += "(";
364 Result += utostr(getValueAsInt());
365 Result += ")";
366 }
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000367 return Result;
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000368 };
Bill Wendling9c2eba92013-01-31 20:59:05 +0000369
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000370 if (hasAttribute(Attribute::StackAlignment))
371 return AttrWithBytesToString("alignstack");
372
373 if (hasAttribute(Attribute::Dereferenceable))
374 return AttrWithBytesToString("dereferenceable");
375
376 if (hasAttribute(Attribute::DereferenceableOrNull))
377 return AttrWithBytesToString("dereferenceable_or_null");
Hal Finkelb0407ba2014-07-18 15:51:28 +0000378
George Burgess IV278199f2016-04-12 01:05:35 +0000379 if (hasAttribute(Attribute::AllocSize)) {
380 unsigned ElemSize;
381 Optional<unsigned> NumElems;
382 std::tie(ElemSize, NumElems) = getAllocSizeArgs();
383
384 std::string Result = "allocsize(";
385 Result += utostr(ElemSize);
386 if (NumElems.hasValue()) {
387 Result += ',';
388 Result += utostr(*NumElems);
389 }
390 Result += ')';
391 return Result;
392 }
393
Bill Wendling9c2eba92013-01-31 20:59:05 +0000394 // Convert target-dependent attributes to strings of the form:
395 //
396 // "kind"
397 // "kind" = "value"
Bill Wendling9c2eba92013-01-31 20:59:05 +0000398 //
Bill Wendling3f12ac22013-02-05 22:37:24 +0000399 if (isStringAttribute()) {
Bill Wendling9c2eba92013-01-31 20:59:05 +0000400 std::string Result;
Yaron Keren075759a2015-03-30 15:42:36 +0000401 Result += (Twine('"') + getKindAsString() + Twine('"')).str();
Bill Wendling9c2eba92013-01-31 20:59:05 +0000402
Honggyu Kim9eb6a102016-09-01 11:44:06 +0000403 std::string AttrVal = pImpl->getValueAsString();
404 if (AttrVal.empty()) return Result;
Bill Wendling7a33f772013-02-01 22:32:30 +0000405
Honggyu Kim9eb6a102016-09-01 11:44:06 +0000406 // Since some attribute strings contain special characters that cannot be
407 // printable, those have to be escaped to make the attribute value printable
408 // as is. e.g. "\01__gnu_mcount_nc"
409 {
410 raw_string_ostream OS(Result);
411 OS << "=\"";
412 PrintEscapedString(AttrVal, OS);
413 OS << "\"";
414 }
Bill Wendlingc79cdff2013-02-01 01:04:27 +0000415 return Result;
Bill Wendling9c2eba92013-01-31 20:59:05 +0000416 }
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000417
418 llvm_unreachable("Unknown attribute");
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000419}
420
Bill Wendlingd509a662013-01-29 00:34:06 +0000421bool Attribute::operator<(Attribute A) const {
422 if (!pImpl && !A.pImpl) return false;
423 if (!pImpl) return true;
424 if (!A.pImpl) return false;
425 return *pImpl < *A.pImpl;
426}
427
Bill Wendlingd509a662013-01-29 00:34:06 +0000428//===----------------------------------------------------------------------===//
429// AttributeImpl Definition
430//===----------------------------------------------------------------------===//
431
Eric Christopher0eaa5412014-07-02 22:05:40 +0000432// Pin the vtables to this file.
Eugene Zelenkodeaf6952017-02-17 00:00:09 +0000433AttributeImpl::~AttributeImpl() = default;
434
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +0000435void EnumAttributeImpl::anchor() {}
Eugene Zelenkodeaf6952017-02-17 00:00:09 +0000436
Hal Finkele15442c2014-07-18 06:51:55 +0000437void IntAttributeImpl::anchor() {}
Eugene Zelenkodeaf6952017-02-17 00:00:09 +0000438
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +0000439void StringAttributeImpl::anchor() {}
Alexey Samsonov49109a22013-11-18 09:31:53 +0000440
Bill Wendlingd509a662013-01-29 00:34:06 +0000441bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
Bill Wendling3f12ac22013-02-05 22:37:24 +0000442 if (isStringAttribute()) return false;
443 return getKindAsEnum() == A;
Bill Wendlingd509a662013-01-29 00:34:06 +0000444}
445
Bill Wendling3f12ac22013-02-05 22:37:24 +0000446bool AttributeImpl::hasAttribute(StringRef Kind) const {
447 if (!isStringAttribute()) return false;
448 return getKindAsString() == Kind;
Bill Wendlingd509a662013-01-29 00:34:06 +0000449}
450
Bill Wendling3f12ac22013-02-05 22:37:24 +0000451Attribute::AttrKind AttributeImpl::getKindAsEnum() const {
Hal Finkele15442c2014-07-18 06:51:55 +0000452 assert(isEnumAttribute() || isIntAttribute());
Benjamin Kramer741146b2013-07-11 12:13:16 +0000453 return static_cast<const EnumAttributeImpl *>(this)->getEnumKind();
Bill Wendlingd509a662013-01-29 00:34:06 +0000454}
455
Bill Wendling3f12ac22013-02-05 22:37:24 +0000456uint64_t AttributeImpl::getValueAsInt() const {
Hal Finkele15442c2014-07-18 06:51:55 +0000457 assert(isIntAttribute());
458 return static_cast<const IntAttributeImpl *>(this)->getValue();
Bill Wendlingd509a662013-01-29 00:34:06 +0000459}
460
Bill Wendling3f12ac22013-02-05 22:37:24 +0000461StringRef AttributeImpl::getKindAsString() const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000462 assert(isStringAttribute());
463 return static_cast<const StringAttributeImpl *>(this)->getStringKind();
Bill Wendlingd509a662013-01-29 00:34:06 +0000464}
465
Bill Wendling3f12ac22013-02-05 22:37:24 +0000466StringRef AttributeImpl::getValueAsString() const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000467 assert(isStringAttribute());
468 return static_cast<const StringAttributeImpl *>(this)->getStringValue();
Bill Wendlingd509a662013-01-29 00:34:06 +0000469}
470
471bool AttributeImpl::operator<(const AttributeImpl &AI) const {
Bill Wendlingc79cdff2013-02-01 01:04:27 +0000472 // This sorts the attributes with Attribute::AttrKinds coming first (sorted
473 // relative to their enum value) and then strings.
Bill Wendling26b95752013-02-15 05:25:26 +0000474 if (isEnumAttribute()) {
475 if (AI.isEnumAttribute()) return getKindAsEnum() < AI.getKindAsEnum();
Hal Finkele15442c2014-07-18 06:51:55 +0000476 if (AI.isIntAttribute()) return true;
Bill Wendling26b95752013-02-15 05:25:26 +0000477 if (AI.isStringAttribute()) return true;
478 }
Bill Wendlingc79cdff2013-02-01 01:04:27 +0000479
Hal Finkele15442c2014-07-18 06:51:55 +0000480 if (isIntAttribute()) {
Bill Wendling26b95752013-02-15 05:25:26 +0000481 if (AI.isEnumAttribute()) return false;
Reid Kleckner7de67612016-04-04 23:06:05 +0000482 if (AI.isIntAttribute()) {
483 if (getKindAsEnum() == AI.getKindAsEnum())
484 return getValueAsInt() < AI.getValueAsInt();
485 return getKindAsEnum() < AI.getKindAsEnum();
486 }
Bill Wendling26b95752013-02-15 05:25:26 +0000487 if (AI.isStringAttribute()) return true;
Bill Wendling3f12ac22013-02-05 22:37:24 +0000488 }
Bill Wendlingd509a662013-01-29 00:34:06 +0000489
Bill Wendling26b95752013-02-15 05:25:26 +0000490 if (AI.isEnumAttribute()) return false;
Hal Finkele15442c2014-07-18 06:51:55 +0000491 if (AI.isIntAttribute()) return false;
Bill Wendling26b95752013-02-15 05:25:26 +0000492 if (getKindAsString() == AI.getKindAsString())
493 return getValueAsString() < AI.getValueAsString();
494 return getKindAsString() < AI.getKindAsString();
Bill Wendlingd509a662013-01-29 00:34:06 +0000495}
496
Bill Wendlingd509a662013-01-29 00:34:06 +0000497//===----------------------------------------------------------------------===//
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000498// AttributeSet Definition
499//===----------------------------------------------------------------------===//
500
501AttributeSet AttributeSet::get(LLVMContext &C, const AttrBuilder &B) {
502 return AttributeSet(AttributeSetNode::get(C, B));
503}
504
505AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<Attribute> Attrs) {
506 return AttributeSet(AttributeSetNode::get(C, Attrs));
507}
508
Javed Absarf3d79042017-05-11 12:28:08 +0000509AttributeSet AttributeSet::addAttribute(LLVMContext &C,
510 Attribute::AttrKind Kind) const {
511 if (hasAttribute(Kind)) return *this;
512 AttrBuilder B;
513 B.addAttribute(Kind);
514 return addAttributes(C, AttributeSet::get(C, B));
515}
516
517AttributeSet AttributeSet::addAttribute(LLVMContext &C, StringRef Kind,
518 StringRef Value) const {
519 AttrBuilder B;
520 B.addAttribute(Kind, Value);
521 return addAttributes(C, AttributeSet::get(C, B));
522}
523
524AttributeSet AttributeSet::addAttributes(LLVMContext &C,
525 const AttributeSet AS) const {
526 if (!hasAttributes())
527 return AS;
528
529 if (!AS.hasAttributes())
530 return *this;
531
532 AttrBuilder B(AS);
533 for (Attribute I : *this)
534 B.addAttribute(I);
535
536 return get(C, B);
537}
538
539AttributeSet AttributeSet::removeAttribute(LLVMContext &C,
540 Attribute::AttrKind Kind) const {
541 if (!hasAttribute(Kind)) return *this;
542 AttrBuilder B;
543 B.addAttribute(Kind);
544 return removeAttributes(C, B);
545}
546
547AttributeSet AttributeSet::removeAttribute(LLVMContext &C,
548 StringRef Kind) const {
549 if (!hasAttribute(Kind)) return *this;
550 AttrBuilder B;
551 B.addAttribute(Kind);
552 return removeAttributes(C, B);
553}
554
555AttributeSet AttributeSet::removeAttributes(LLVMContext &C,
556 const AttrBuilder &Attrs) const {
557
558 // FIXME it is not obvious how this should work for alignment.
559 // For now, say we can't pass in alignment, which no current use does.
560 assert(!Attrs.hasAlignmentAttr() && "Attempt to change alignment!");
561
562 AttrBuilder B(*this);
563 B.remove(Attrs);
564 return get(C, B);
565}
566
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000567unsigned AttributeSet::getNumAttributes() const {
568 return SetNode ? SetNode->getNumAttributes() : 0;
569}
570
571bool AttributeSet::hasAttribute(Attribute::AttrKind Kind) const {
Eugene Zelenkod761e2c2017-05-15 21:57:41 +0000572 return SetNode ? SetNode->hasAttribute(Kind) : false;
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000573}
574
575bool AttributeSet::hasAttribute(StringRef 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
579Attribute AttributeSet::getAttribute(Attribute::AttrKind Kind) const {
580 return SetNode ? SetNode->getAttribute(Kind) : Attribute();
581}
582
583Attribute AttributeSet::getAttribute(StringRef Kind) const {
584 return SetNode ? SetNode->getAttribute(Kind) : Attribute();
585}
586
587unsigned AttributeSet::getAlignment() const {
588 return SetNode ? SetNode->getAlignment() : 0;
589}
590
591unsigned AttributeSet::getStackAlignment() const {
592 return SetNode ? SetNode->getStackAlignment() : 0;
593}
594
595uint64_t AttributeSet::getDereferenceableBytes() const {
596 return SetNode ? SetNode->getDereferenceableBytes() : 0;
597}
598
599uint64_t AttributeSet::getDereferenceableOrNullBytes() const {
600 return SetNode ? SetNode->getDereferenceableOrNullBytes() : 0;
601}
602
603std::pair<unsigned, Optional<unsigned>> AttributeSet::getAllocSizeArgs() const {
Konstantin Zhuravlyov6df95b72017-04-12 23:57:37 +0000604 return SetNode ? SetNode->getAllocSizeArgs()
605 : std::pair<unsigned, Optional<unsigned>>(0, 0);
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000606}
607
608std::string AttributeSet::getAsString(bool InAttrGrp) const {
609 return SetNode ? SetNode->getAsString(InAttrGrp) : "";
610}
611
612AttributeSet::iterator AttributeSet::begin() const {
613 return SetNode ? SetNode->begin() : nullptr;
614}
615
616AttributeSet::iterator AttributeSet::end() const {
617 return SetNode ? SetNode->end() : nullptr;
618}
619
Javed Absarf3d79042017-05-11 12:28:08 +0000620#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
621LLVM_DUMP_METHOD void AttributeSet::dump() const {
622 dbgs() << "AS =\n";
623 dbgs() << " { ";
624 dbgs() << getAsString(true) << " }\n";
625}
626#endif
627
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000628//===----------------------------------------------------------------------===//
Bill Wendlingd509a662013-01-29 00:34:06 +0000629// AttributeSetNode Definition
630//===----------------------------------------------------------------------===//
631
Reid Kleckner8ff77852017-04-10 23:46:08 +0000632AttributeSetNode::AttributeSetNode(ArrayRef<Attribute> Attrs)
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000633 : AvailableAttrs(0), NumAttrs(Attrs.size()) {
Reid Kleckner8ff77852017-04-10 23:46:08 +0000634 // There's memory after the node where we can store the entries in.
635 std::copy(Attrs.begin(), Attrs.end(), getTrailingObjects<Attribute>());
636
637 for (Attribute I : *this) {
638 if (!I.isStringAttribute()) {
639 AvailableAttrs |= ((uint64_t)1) << I.getKindAsEnum();
640 }
641 }
642}
643
Bill Wendlingd509a662013-01-29 00:34:06 +0000644AttributeSetNode *AttributeSetNode::get(LLVMContext &C,
645 ArrayRef<Attribute> Attrs) {
646 if (Attrs.empty())
Craig Topperc6207612014-04-09 06:08:46 +0000647 return nullptr;
Bill Wendlingd509a662013-01-29 00:34:06 +0000648
649 // Otherwise, build a key to look up the existing attributes.
650 LLVMContextImpl *pImpl = C.pImpl;
651 FoldingSetNodeID ID;
652
653 SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end());
Reid Kleckner7de67612016-04-04 23:06:05 +0000654 std::sort(SortedAttrs.begin(), SortedAttrs.end());
Bill Wendlingd509a662013-01-29 00:34:06 +0000655
George Burgess IV500d3032015-12-16 05:21:02 +0000656 for (Attribute Attr : SortedAttrs)
657 Attr.Profile(ID);
Bill Wendlingd509a662013-01-29 00:34:06 +0000658
659 void *InsertPoint;
660 AttributeSetNode *PA =
661 pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint);
662
663 // If we didn't find any existing attributes of the same shape then create a
664 // new one and insert it.
665 if (!PA) {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000666 // Coallocate entries after the AttributeSetNode itself.
James Y Knightaa365b22015-08-05 22:57:34 +0000667 void *Mem = ::operator new(totalSizeToAlloc<Attribute>(SortedAttrs.size()));
Benjamin Kramer741146b2013-07-11 12:13:16 +0000668 PA = new (Mem) AttributeSetNode(SortedAttrs);
Bill Wendlingd509a662013-01-29 00:34:06 +0000669 pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint);
670 }
671
Reid Klecknerb5180542017-03-21 16:57:19 +0000672 // Return the AttributeSetNode that we found or created.
Bill Wendlingd509a662013-01-29 00:34:06 +0000673 return PA;
674}
675
Reid Klecknereb9dd5b2017-04-10 23:31:05 +0000676AttributeSetNode *AttributeSetNode::get(LLVMContext &C, const AttrBuilder &B) {
677 // Add target-independent attributes.
678 SmallVector<Attribute, 8> Attrs;
679 for (Attribute::AttrKind Kind = Attribute::None;
680 Kind != Attribute::EndAttrKinds; Kind = Attribute::AttrKind(Kind + 1)) {
681 if (!B.contains(Kind))
682 continue;
683
684 Attribute Attr;
685 switch (Kind) {
686 case Attribute::Alignment:
687 Attr = Attribute::getWithAlignment(C, B.getAlignment());
688 break;
689 case Attribute::StackAlignment:
690 Attr = Attribute::getWithStackAlignment(C, B.getStackAlignment());
691 break;
692 case Attribute::Dereferenceable:
693 Attr = Attribute::getWithDereferenceableBytes(
694 C, B.getDereferenceableBytes());
695 break;
696 case Attribute::DereferenceableOrNull:
697 Attr = Attribute::getWithDereferenceableOrNullBytes(
698 C, B.getDereferenceableOrNullBytes());
699 break;
700 case Attribute::AllocSize: {
701 auto A = B.getAllocSizeArgs();
702 Attr = Attribute::getWithAllocSizeArgs(C, A.first, A.second);
703 break;
704 }
705 default:
706 Attr = Attribute::get(C, Kind);
707 }
708 Attrs.push_back(Attr);
709 }
710
711 // Add target-dependent (string) attributes.
712 for (const auto &TDA : B.td_attrs())
713 Attrs.emplace_back(Attribute::get(C, TDA.first, TDA.second));
714
715 return get(C, Attrs);
716}
717
Bill Wendlingbce7b972013-02-13 08:42:21 +0000718bool AttributeSetNode::hasAttribute(StringRef Kind) const {
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000719 for (Attribute I : *this)
720 if (I.hasAttribute(Kind))
Bill Wendlingbce7b972013-02-13 08:42:21 +0000721 return true;
722 return false;
723}
724
725Attribute AttributeSetNode::getAttribute(Attribute::AttrKind Kind) const {
Matthias Braun31eeb76f2016-01-29 22:25:13 +0000726 if (hasAttribute(Kind)) {
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000727 for (Attribute I : *this)
728 if (I.hasAttribute(Kind))
729 return I;
Matthias Braun31eeb76f2016-01-29 22:25:13 +0000730 }
Bill Wendlingbce7b972013-02-13 08:42:21 +0000731 return Attribute();
732}
733
734Attribute AttributeSetNode::getAttribute(StringRef Kind) const {
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000735 for (Attribute I : *this)
736 if (I.hasAttribute(Kind))
737 return I;
Bill Wendlingbce7b972013-02-13 08:42:21 +0000738 return Attribute();
739}
740
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000741unsigned AttributeSetNode::getAlignment() const {
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000742 for (Attribute I : *this)
743 if (I.hasAttribute(Attribute::Alignment))
744 return I.getAlignment();
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000745 return 0;
746}
747
748unsigned AttributeSetNode::getStackAlignment() const {
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000749 for (Attribute I : *this)
750 if (I.hasAttribute(Attribute::StackAlignment))
751 return I.getStackAlignment();
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000752 return 0;
753}
754
Hal Finkelb0407ba2014-07-18 15:51:28 +0000755uint64_t AttributeSetNode::getDereferenceableBytes() const {
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000756 for (Attribute I : *this)
757 if (I.hasAttribute(Attribute::Dereferenceable))
758 return I.getDereferenceableBytes();
Hal Finkelb0407ba2014-07-18 15:51:28 +0000759 return 0;
760}
761
Sanjoy Das06cf33f2015-05-06 17:41:54 +0000762uint64_t AttributeSetNode::getDereferenceableOrNullBytes() const {
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000763 for (Attribute I : *this)
764 if (I.hasAttribute(Attribute::DereferenceableOrNull))
765 return I.getDereferenceableOrNullBytes();
Sanjoy Das06cf33f2015-05-06 17:41:54 +0000766 return 0;
767}
768
George Burgess IV278199f2016-04-12 01:05:35 +0000769std::pair<unsigned, Optional<unsigned>>
770AttributeSetNode::getAllocSizeArgs() const {
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000771 for (Attribute I : *this)
772 if (I.hasAttribute(Attribute::AllocSize))
773 return I.getAllocSizeArgs();
George Burgess IV278199f2016-04-12 01:05:35 +0000774 return std::make_pair(0, 0);
775}
776
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +0000777std::string AttributeSetNode::getAsString(bool InAttrGrp) const {
Benjamin Kramer0baf8f42013-04-19 11:43:21 +0000778 std::string Str;
Benjamin Kramer741146b2013-07-11 12:13:16 +0000779 for (iterator I = begin(), E = end(); I != E; ++I) {
780 if (I != begin())
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +0000781 Str += ' ';
782 Str += I->getAsString(InAttrGrp);
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000783 }
784 return Str;
785}
786
Bill Wendlingd509a662013-01-29 00:34:06 +0000787//===----------------------------------------------------------------------===//
Reid Klecknerb5180542017-03-21 16:57:19 +0000788// AttributeListImpl Definition
Bill Wendlingd509a662013-01-29 00:34:06 +0000789//===----------------------------------------------------------------------===//
790
Reid Klecknera82be602017-04-11 00:16:00 +0000791AttributeListImpl::AttributeListImpl(
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000792 LLVMContext &C, ArrayRef<std::pair<unsigned, AttributeSet>> Slots)
Reid Klecknera82be602017-04-11 00:16:00 +0000793 : Context(C), NumSlots(Slots.size()), AvailableFunctionAttrs(0) {
794#ifndef NDEBUG
Reid Klecknerec0fc032017-04-12 22:22:01 +0000795 assert(!Slots.empty() && "pointless AttributeListImpl");
Reid Klecknera82be602017-04-11 00:16:00 +0000796 if (Slots.size() >= 2) {
797 auto &PrevPair = Slots.front();
798 for (auto &CurPair : Slots.drop_front()) {
799 assert(PrevPair.first <= CurPair.first && "Attribute set not ordered!");
800 }
801 }
802#endif
803
804 // There's memory after the node where we can store the entries in.
805 std::copy(Slots.begin(), Slots.end(), getTrailingObjects<IndexAttrPair>());
806
807 // Initialize AvailableFunctionAttrs summary bitset.
Reid Klecknerec0fc032017-04-12 22:22:01 +0000808 static_assert(Attribute::EndAttrKinds <=
809 sizeof(AvailableFunctionAttrs) * CHAR_BIT,
810 "Too many attributes");
811 static_assert(AttributeList::FunctionIndex == ~0u,
812 "FunctionIndex should be biggest possible index");
813 const auto &Last = Slots.back();
814 if (Last.first == AttributeList::FunctionIndex) {
815 AttributeSet Node = Last.second;
816 for (Attribute I : Node) {
817 if (!I.isStringAttribute())
818 AvailableFunctionAttrs |= ((uint64_t)1) << I.getKindAsEnum();
Reid Klecknera82be602017-04-11 00:16:00 +0000819 }
820 }
821}
822
823void AttributeListImpl::Profile(FoldingSetNodeID &ID) const {
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000824 Profile(ID, makeArrayRef(getSlotPair(0), getNumSlots()));
Reid Klecknera82be602017-04-11 00:16:00 +0000825}
826
827void AttributeListImpl::Profile(
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000828 FoldingSetNodeID &ID, ArrayRef<std::pair<unsigned, AttributeSet>> Nodes) {
Reid Klecknera82be602017-04-11 00:16:00 +0000829 for (const auto &Node : Nodes) {
830 ID.AddInteger(Node.first);
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000831 ID.AddPointer(Node.second.SetNode);
Reid Klecknera82be602017-04-11 00:16:00 +0000832 }
833}
834
Matthias Braun8c209aa2017-01-28 02:02:38 +0000835#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Reid Klecknerb5180542017-03-21 16:57:19 +0000836LLVM_DUMP_METHOD void AttributeListImpl::dump() const {
837 AttributeList(const_cast<AttributeListImpl *>(this)).dump();
Peter Collingbourneabca2ec2013-08-02 22:34:30 +0000838}
Matthias Braun8c209aa2017-01-28 02:02:38 +0000839#endif
Peter Collingbourneabca2ec2013-08-02 22:34:30 +0000840
Bill Wendlingd509a662013-01-29 00:34:06 +0000841//===----------------------------------------------------------------------===//
Reid Klecknerb5180542017-03-21 16:57:19 +0000842// AttributeList Construction and Mutation Methods
Bill Wendlingd509a662013-01-29 00:34:06 +0000843//===----------------------------------------------------------------------===//
844
Reid Klecknerb5180542017-03-21 16:57:19 +0000845AttributeList AttributeList::getImpl(
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000846 LLVMContext &C, ArrayRef<std::pair<unsigned, AttributeSet>> Attrs) {
Reid Klecknereb9dd5b2017-04-10 23:31:05 +0000847 assert(!Attrs.empty() && "creating pointless AttributeList");
848#ifndef NDEBUG
849 unsigned LastIndex = 0;
850 bool IsFirst = true;
Reid Kleckner7f720332017-04-13 00:58:09 +0000851 for (auto &&AttrPair : Attrs) {
Reid Klecknereb9dd5b2017-04-10 23:31:05 +0000852 assert((IsFirst || LastIndex < AttrPair.first) &&
853 "unsorted or duplicate AttributeList indices");
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000854 assert(AttrPair.second.hasAttributes() && "pointless AttributeList slot");
Reid Klecknereb9dd5b2017-04-10 23:31:05 +0000855 LastIndex = AttrPair.first;
856 IsFirst = false;
857 }
858#endif
859
Bill Wendlingd509a662013-01-29 00:34:06 +0000860 LLVMContextImpl *pImpl = C.pImpl;
861 FoldingSetNodeID ID;
Reid Klecknerb5180542017-03-21 16:57:19 +0000862 AttributeListImpl::Profile(ID, Attrs);
Bill Wendlingd509a662013-01-29 00:34:06 +0000863
864 void *InsertPoint;
Reid Klecknerb5180542017-03-21 16:57:19 +0000865 AttributeListImpl *PA =
866 pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
Bill Wendlingd509a662013-01-29 00:34:06 +0000867
868 // If we didn't find any existing attributes of the same shape then
869 // create a new one and insert it.
870 if (!PA) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000871 // Coallocate entries after the AttributeListImpl itself.
James Y Knightaa365b22015-08-05 22:57:34 +0000872 void *Mem = ::operator new(
Reid Klecknerb5180542017-03-21 16:57:19 +0000873 AttributeListImpl::totalSizeToAlloc<IndexAttrPair>(Attrs.size()));
874 PA = new (Mem) AttributeListImpl(C, Attrs);
Bill Wendlingd509a662013-01-29 00:34:06 +0000875 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
876 }
877
878 // Return the AttributesList that we found or created.
Reid Klecknerb5180542017-03-21 16:57:19 +0000879 return AttributeList(PA);
Bill Wendlingd509a662013-01-29 00:34:06 +0000880}
881
Reid Klecknerb5180542017-03-21 16:57:19 +0000882AttributeList
883AttributeList::get(LLVMContext &C,
884 ArrayRef<std::pair<unsigned, Attribute>> Attrs) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000885 // If there are no attributes then return a null AttributesList pointer.
886 if (Attrs.empty())
Reid Klecknerb5180542017-03-21 16:57:19 +0000887 return AttributeList();
Bill Wendlingd509a662013-01-29 00:34:06 +0000888
Craig Toppere30b8ca2016-01-03 19:43:40 +0000889 assert(std::is_sorted(Attrs.begin(), Attrs.end(),
890 [](const std::pair<unsigned, Attribute> &LHS,
891 const std::pair<unsigned, Attribute> &RHS) {
892 return LHS.first < RHS.first;
893 }) && "Misordered Attributes list!");
David Majnemer0a16c222016-08-11 21:15:00 +0000894 assert(none_of(Attrs,
895 [](const std::pair<unsigned, Attribute> &Pair) {
896 return Pair.second.hasAttribute(Attribute::None);
897 }) &&
898 "Pointless attribute!");
Bill Wendlingd509a662013-01-29 00:34:06 +0000899
900 // Create a vector if (unsigned, AttributeSetNode*) pairs from the attributes
901 // list.
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000902 SmallVector<std::pair<unsigned, AttributeSet>, 8> AttrPairVec;
Eugene Zelenkodeaf6952017-02-17 00:00:09 +0000903 for (ArrayRef<std::pair<unsigned, Attribute>>::iterator I = Attrs.begin(),
Bill Wendlingd509a662013-01-29 00:34:06 +0000904 E = Attrs.end(); I != E; ) {
905 unsigned Index = I->first;
906 SmallVector<Attribute, 4> AttrVec;
NAKAMURA Takumif05d2f22013-01-29 15:18:16 +0000907 while (I != E && I->first == Index) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000908 AttrVec.push_back(I->second);
909 ++I;
910 }
911
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000912 AttrPairVec.emplace_back(Index, AttributeSet::get(C, AttrVec));
Bill Wendlingd509a662013-01-29 00:34:06 +0000913 }
914
915 return getImpl(C, AttrPairVec);
916}
917
Reid Klecknerb5180542017-03-21 16:57:19 +0000918AttributeList
919AttributeList::get(LLVMContext &C,
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000920 ArrayRef<std::pair<unsigned, AttributeSet>> Attrs) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000921 // If there are no attributes then return a null AttributesList pointer.
922 if (Attrs.empty())
Reid Klecknerb5180542017-03-21 16:57:19 +0000923 return AttributeList();
Bill Wendlingd509a662013-01-29 00:34:06 +0000924
925 return getImpl(C, Attrs);
926}
927
Reid Kleckner7f720332017-04-13 00:58:09 +0000928AttributeList AttributeList::get(LLVMContext &C, AttributeSet FnAttrs,
929 AttributeSet RetAttrs,
930 ArrayRef<AttributeSet> ArgAttrs) {
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000931 SmallVector<std::pair<unsigned, AttributeSet>, 8> AttrPairs;
Reid Kleckner7f720332017-04-13 00:58:09 +0000932 if (RetAttrs.hasAttributes())
933 AttrPairs.emplace_back(ReturnIndex, RetAttrs);
934 size_t Index = 1;
935 for (AttributeSet AS : ArgAttrs) {
936 if (AS.hasAttributes())
Reid Klecknereb9dd5b2017-04-10 23:31:05 +0000937 AttrPairs.emplace_back(Index, AS);
Reid Klecknereb9dd5b2017-04-10 23:31:05 +0000938 ++Index;
939 }
Reid Kleckner7f720332017-04-13 00:58:09 +0000940 if (FnAttrs.hasAttributes())
941 AttrPairs.emplace_back(FunctionIndex, FnAttrs);
Reid Klecknereb9dd5b2017-04-10 23:31:05 +0000942 if (AttrPairs.empty())
943 return AttributeList();
944 return getImpl(C, AttrPairs);
945}
946
Reid Klecknerb5180542017-03-21 16:57:19 +0000947AttributeList AttributeList::get(LLVMContext &C, unsigned Index,
948 const AttrBuilder &B) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000949 if (!B.hasAttributes())
Reid Klecknerb5180542017-03-21 16:57:19 +0000950 return AttributeList();
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000951 AttributeSet AS = AttributeSet::get(C, B);
952 std::pair<unsigned, AttributeSet> Arr[1] = {{Index, AS}};
Reid Klecknereb9dd5b2017-04-10 23:31:05 +0000953 return getImpl(C, Arr);
Bill Wendlingd509a662013-01-29 00:34:06 +0000954}
955
Reid Klecknerb5180542017-03-21 16:57:19 +0000956AttributeList AttributeList::get(LLVMContext &C, unsigned Index,
957 ArrayRef<Attribute::AttrKind> Kinds) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000958 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
Amaury Sechet392638d2016-06-14 20:27:35 +0000959 for (Attribute::AttrKind K : Kinds)
David Majnemer0a16c222016-08-11 21:15:00 +0000960 Attrs.emplace_back(Index, Attribute::get(C, K));
Bill Wendlingd509a662013-01-29 00:34:06 +0000961 return get(C, Attrs);
962}
963
Reid Klecknerb5180542017-03-21 16:57:19 +0000964AttributeList AttributeList::get(LLVMContext &C, unsigned Index,
965 ArrayRef<StringRef> Kinds) {
Amaury Sechet6100adf2016-06-15 17:50:39 +0000966 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
967 for (StringRef K : Kinds)
David Majnemer0a16c222016-08-11 21:15:00 +0000968 Attrs.emplace_back(Index, Attribute::get(C, K));
Amaury Sechet6100adf2016-06-15 17:50:39 +0000969 return get(C, Attrs);
970}
971
Reid Klecknerb5180542017-03-21 16:57:19 +0000972AttributeList AttributeList::get(LLVMContext &C,
973 ArrayRef<AttributeList> Attrs) {
974 if (Attrs.empty())
975 return AttributeList();
Peter Collingbournebd6c7452013-08-02 22:29:40 +0000976 if (Attrs.size() == 1) return Attrs[0];
Bill Wendlingd509a662013-01-29 00:34:06 +0000977
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000978 SmallVector<std::pair<unsigned, AttributeSet>, 8> AttrNodeVec;
Reid Klecknerb5180542017-03-21 16:57:19 +0000979 AttributeListImpl *A0 = Attrs[0].pImpl;
Peter Collingbournebd6c7452013-08-02 22:29:40 +0000980 if (A0)
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000981 AttrNodeVec.append(A0->getSlotPair(0), A0->getSlotPair(A0->getNumSlots()));
Peter Collingbournebd6c7452013-08-02 22:29:40 +0000982 // Copy all attributes from Attrs into AttrNodeVec while keeping AttrNodeVec
983 // ordered by index. Because we know that each list in Attrs is ordered by
984 // index we only need to merge each successive list in rather than doing a
985 // full sort.
986 for (unsigned I = 1, E = Attrs.size(); I != E; ++I) {
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000987 AttributeListImpl *ALI = Attrs[I].pImpl;
988 if (!ALI) continue;
989 SmallVector<std::pair<unsigned, AttributeSet>, 8>::iterator
Peter Collingbournebd6c7452013-08-02 22:29:40 +0000990 ANVI = AttrNodeVec.begin(), ANVE;
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000991 for (const IndexAttrPair *AI = ALI->getSlotPair(0),
992 *AE = ALI->getSlotPair(ALI->getNumSlots());
Peter Collingbournebd6c7452013-08-02 22:29:40 +0000993 AI != AE; ++AI) {
994 ANVE = AttrNodeVec.end();
995 while (ANVI != ANVE && ANVI->first <= AI->first)
996 ++ANVI;
997 ANVI = AttrNodeVec.insert(ANVI, *AI) + 1;
998 }
Bill Wendlingd509a662013-01-29 00:34:06 +0000999 }
1000
1001 return getImpl(C, AttrNodeVec);
1002}
1003
Reid Klecknerb5180542017-03-21 16:57:19 +00001004AttributeList AttributeList::addAttribute(LLVMContext &C, unsigned Index,
1005 Attribute::AttrKind Kind) const {
Amaury Sechet392638d2016-06-14 20:27:35 +00001006 if (hasAttribute(Index, Kind)) return *this;
Reid Kleckneree4930b2017-05-02 22:07:37 +00001007 AttrBuilder B;
1008 B.addAttribute(Kind);
1009 return addAttributes(C, Index, B);
Reed Kotler795c7b42013-03-13 20:20:08 +00001010}
1011
Reid Klecknerb5180542017-03-21 16:57:19 +00001012AttributeList AttributeList::addAttribute(LLVMContext &C, unsigned Index,
1013 StringRef Kind,
1014 StringRef Value) const {
Eugene Zelenkodeaf6952017-02-17 00:00:09 +00001015 AttrBuilder B;
Bill Wendling3b2f6102013-07-25 18:34:24 +00001016 B.addAttribute(Kind, Value);
Reid Kleckneree4930b2017-05-02 22:07:37 +00001017 return addAttributes(C, Index, B);
Bill Wendling3b2f6102013-07-25 18:34:24 +00001018}
1019
Reid Klecknerb5180542017-03-21 16:57:19 +00001020AttributeList AttributeList::addAttribute(LLVMContext &C,
1021 ArrayRef<unsigned> Indices,
1022 Attribute A) const {
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00001023 assert(std::is_sorted(Indices.begin(), Indices.end()));
Reid Kleckner324c99d2017-04-10 20:18:10 +00001024
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00001025 unsigned I = 0, E = pImpl ? pImpl->getNumSlots() : 0;
1026 SmallVector<IndexAttrPair, 4> AttrVec;
1027 for (unsigned Index : Indices) {
1028 // Add all attribute slots before the current index.
1029 for (; I < E && getSlotIndex(I) < Index; ++I)
Reid Kleckner63b26f02017-04-24 22:25:02 +00001030 AttrVec.emplace_back(getSlotIndex(I), pImpl->getSlotAttributes(I));
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00001031
1032 // Add the attribute at this index. If we already have attributes at this
1033 // index, merge them into a new set.
1034 AttrBuilder B;
1035 if (I < E && getSlotIndex(I) == Index) {
Reid Kleckner63b26f02017-04-24 22:25:02 +00001036 B.merge(AttrBuilder(pImpl->getSlotAttributes(I)));
Akira Hatanaka237916b2015-12-02 06:58:49 +00001037 ++I;
Akira Hatanaka237916b2015-12-02 06:58:49 +00001038 }
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00001039 B.addAttribute(A);
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001040 AttrVec.emplace_back(Index, AttributeSet::get(C, B));
Akira Hatanaka237916b2015-12-02 06:58:49 +00001041 }
1042
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00001043 // Add remaining attributes.
1044 for (; I < E; ++I)
Reid Kleckner63b26f02017-04-24 22:25:02 +00001045 AttrVec.emplace_back(getSlotIndex(I), pImpl->getSlotAttributes(I));
Akira Hatanaka237916b2015-12-02 06:58:49 +00001046
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00001047 return get(C, AttrVec);
Akira Hatanaka237916b2015-12-02 06:58:49 +00001048}
1049
Reid Klecknerb5180542017-03-21 16:57:19 +00001050AttributeList AttributeList::addAttributes(LLVMContext &C, unsigned Index,
Reid Kleckner61906252017-04-19 01:51:13 +00001051 const AttrBuilder &B) const {
1052 if (!B.hasAttributes())
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00001053 return *this;
1054
Reid Klecknerfe64c012017-04-18 22:10:18 +00001055 if (!pImpl)
Reid Kleckner61906252017-04-19 01:51:13 +00001056 return AttributeList::get(C, {{Index, AttributeSet::get(C, B)}});
Reid Klecknerfe64c012017-04-18 22:10:18 +00001057
Bill Wendlingd509a662013-01-29 00:34:06 +00001058#ifndef NDEBUG
1059 // FIXME it is not obvious how this should work for alignment. For now, say
1060 // we can't change a known alignment.
Reid Klecknerbf6b3b152017-05-19 22:23:47 +00001061 unsigned OldAlign = getAttributes(Index).getAlignment();
Reid Kleckner61906252017-04-19 01:51:13 +00001062 unsigned NewAlign = B.getAlignment();
Bill Wendlingd509a662013-01-29 00:34:06 +00001063 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
1064 "Attempt to change alignment!");
1065#endif
1066
Reid Kleckner61906252017-04-19 01:51:13 +00001067 SmallVector<IndexAttrPair, 4> AttrVec;
Amaury Sechet24c84fd2016-06-14 22:04:16 +00001068 uint64_t NumAttrs = pImpl->getNumSlots();
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00001069 unsigned I;
1070
1071 // Add all the attribute slots before the one we need to merge.
1072 for (I = 0; I < NumAttrs; ++I) {
1073 if (getSlotIndex(I) >= Index)
Bill Wendlingd509a662013-01-29 00:34:06 +00001074 break;
Reid Kleckner63b26f02017-04-24 22:25:02 +00001075 AttrVec.emplace_back(getSlotIndex(I), pImpl->getSlotAttributes(I));
Bill Wendlingd509a662013-01-29 00:34:06 +00001076 }
1077
Reid Kleckner61906252017-04-19 01:51:13 +00001078 AttrBuilder NewAttrs;
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00001079 if (I < NumAttrs && getSlotIndex(I) == Index) {
Reid Kleckner61906252017-04-19 01:51:13 +00001080 // We need to merge the attribute sets.
Reid Kleckner63b26f02017-04-24 22:25:02 +00001081 NewAttrs.merge(pImpl->getSlotAttributes(I));
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00001082 ++I;
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00001083 }
Reid Kleckner61906252017-04-19 01:51:13 +00001084 NewAttrs.merge(B);
1085
1086 // Add the new or merged attribute set at this index.
1087 AttrVec.emplace_back(Index, AttributeSet::get(C, NewAttrs));
Bill Wendlingd509a662013-01-29 00:34:06 +00001088
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00001089 // Add the remaining entries.
1090 for (; I < NumAttrs; ++I)
Reid Kleckner63b26f02017-04-24 22:25:02 +00001091 AttrVec.emplace_back(getSlotIndex(I), pImpl->getSlotAttributes(I));
Bill Wendlingd509a662013-01-29 00:34:06 +00001092
Reid Kleckner61906252017-04-19 01:51:13 +00001093 return get(C, AttrVec);
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00001094}
1095
Reid Klecknerb5180542017-03-21 16:57:19 +00001096AttributeList AttributeList::removeAttribute(LLVMContext &C, unsigned Index,
1097 Attribute::AttrKind Kind) const {
Amaury Sechet392638d2016-06-14 20:27:35 +00001098 if (!hasAttribute(Index, Kind)) return *this;
Reid Kleckneree4930b2017-05-02 22:07:37 +00001099 AttrBuilder B;
1100 B.addAttribute(Kind);
1101 return removeAttributes(C, Index, B);
Bill Wendlingd509a662013-01-29 00:34:06 +00001102}
1103
Reid Klecknerb5180542017-03-21 16:57:19 +00001104AttributeList AttributeList::removeAttribute(LLVMContext &C, unsigned Index,
1105 StringRef Kind) const {
Amaury Sechet6100adf2016-06-15 17:50:39 +00001106 if (!hasAttribute(Index, Kind)) return *this;
Reid Kleckneree4930b2017-05-02 22:07:37 +00001107 AttrBuilder B;
1108 B.addAttribute(Kind);
1109 return removeAttributes(C, Index, B);
Bill Wendlingd509a662013-01-29 00:34:06 +00001110}
1111
Reid Klecknerb5180542017-03-21 16:57:19 +00001112AttributeList AttributeList::removeAttributes(LLVMContext &C, unsigned Index,
1113 const AttrBuilder &Attrs) const {
1114 if (!pImpl)
1115 return AttributeList();
Pete Cooperd2a44612015-05-06 23:19:43 +00001116
1117 // FIXME it is not obvious how this should work for alignment.
1118 // For now, say we can't pass in alignment, which no current use does.
1119 assert(!Attrs.hasAlignmentAttr() && "Attempt to change alignment!");
1120
1121 // Add the attribute slots before the one we're trying to add.
Reid Kleckner62731e12017-04-20 18:08:36 +00001122 SmallVector<IndexAttrPair, 4> AttrSets;
Amaury Sechet24c84fd2016-06-14 22:04:16 +00001123 uint64_t NumAttrs = pImpl->getNumSlots();
Reid Kleckner62731e12017-04-20 18:08:36 +00001124 AttrBuilder B;
Pete Cooperd2a44612015-05-06 23:19:43 +00001125 uint64_t LastIndex = 0;
1126 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
1127 if (getSlotIndex(I) >= Index) {
Reid Kleckner62731e12017-04-20 18:08:36 +00001128 if (getSlotIndex(I) == Index)
Reid Kleckner63b26f02017-04-24 22:25:02 +00001129 B = AttrBuilder(getSlotAttributes(LastIndex++));
Pete Cooperd2a44612015-05-06 23:19:43 +00001130 break;
1131 }
1132 LastIndex = I + 1;
Reid Kleckner63b26f02017-04-24 22:25:02 +00001133 AttrSets.push_back({getSlotIndex(I), getSlotAttributes(I)});
Pete Cooperd2a44612015-05-06 23:19:43 +00001134 }
1135
Reid Kleckner62731e12017-04-20 18:08:36 +00001136 // Remove the attributes from the existing set and add them.
Pete Cooperd2a44612015-05-06 23:19:43 +00001137 B.remove(Attrs);
Reid Kleckner62731e12017-04-20 18:08:36 +00001138 if (B.hasAttributes())
1139 AttrSets.push_back({Index, AttributeSet::get(C, B)});
Pete Cooperd2a44612015-05-06 23:19:43 +00001140
1141 // Add the remaining attribute slots.
1142 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
Reid Kleckner63b26f02017-04-24 22:25:02 +00001143 AttrSets.push_back({getSlotIndex(I), getSlotAttributes(I)});
Pete Cooperd2a44612015-05-06 23:19:43 +00001144
Reid Kleckner62731e12017-04-20 18:08:36 +00001145 return get(C, AttrSets);
Pete Cooperd2a44612015-05-06 23:19:43 +00001146}
1147
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00001148AttributeList AttributeList::removeAttributes(LLVMContext &C,
1149 unsigned WithoutIndex) const {
1150 if (!pImpl)
1151 return AttributeList();
1152
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001153 SmallVector<std::pair<unsigned, AttributeSet>, 4> AttrSet;
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00001154 for (unsigned I = 0, E = pImpl->getNumSlots(); I != E; ++I) {
1155 unsigned Index = getSlotIndex(I);
1156 if (Index != WithoutIndex)
Reid Kleckner63b26f02017-04-24 22:25:02 +00001157 AttrSet.push_back({Index, pImpl->getSlotAttributes(I)});
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00001158 }
1159 return get(C, AttrSet);
1160}
1161
Reid Klecknerb5180542017-03-21 16:57:19 +00001162AttributeList AttributeList::addDereferenceableAttr(LLVMContext &C,
1163 unsigned Index,
1164 uint64_t Bytes) const {
Eugene Zelenkodeaf6952017-02-17 00:00:09 +00001165 AttrBuilder B;
Ramkumar Ramachandra8fcb4982015-02-14 19:37:54 +00001166 B.addDereferenceableAttr(Bytes);
Reid Kleckneree4930b2017-05-02 22:07:37 +00001167 return addAttributes(C, Index, B);
Ramkumar Ramachandra8fcb4982015-02-14 19:37:54 +00001168}
1169
Reid Klecknerb5180542017-03-21 16:57:19 +00001170AttributeList
1171AttributeList::addDereferenceableOrNullAttr(LLVMContext &C, unsigned Index,
1172 uint64_t Bytes) const {
Eugene Zelenkodeaf6952017-02-17 00:00:09 +00001173 AttrBuilder B;
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001174 B.addDereferenceableOrNullAttr(Bytes);
Reid Kleckneree4930b2017-05-02 22:07:37 +00001175 return addAttributes(C, Index, B);
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001176}
1177
Reid Klecknerb5180542017-03-21 16:57:19 +00001178AttributeList
1179AttributeList::addAllocSizeAttr(LLVMContext &C, unsigned Index,
1180 unsigned ElemSizeArg,
1181 const Optional<unsigned> &NumElemsArg) {
Eugene Zelenkodeaf6952017-02-17 00:00:09 +00001182 AttrBuilder B;
George Burgess IV278199f2016-04-12 01:05:35 +00001183 B.addAllocSizeAttr(ElemSizeArg, NumElemsArg);
Reid Kleckneree4930b2017-05-02 22:07:37 +00001184 return addAttributes(C, Index, B);
George Burgess IV278199f2016-04-12 01:05:35 +00001185}
1186
Bill Wendlingd509a662013-01-29 00:34:06 +00001187//===----------------------------------------------------------------------===//
Reid Klecknerb5180542017-03-21 16:57:19 +00001188// AttributeList Accessor Methods
Bill Wendlingd509a662013-01-29 00:34:06 +00001189//===----------------------------------------------------------------------===//
1190
Reid Klecknerb5180542017-03-21 16:57:19 +00001191LLVMContext &AttributeList::getContext() const { return pImpl->getContext(); }
1192
Reid Klecknerf021fab2017-04-13 23:12:13 +00001193AttributeSet AttributeList::getParamAttributes(unsigned ArgNo) const {
Reid Klecknera0b45f42017-05-03 18:17:31 +00001194 return getAttributes(ArgNo + FirstArgIndex);
Bill Wendling5d020a32013-02-10 05:00:40 +00001195}
1196
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001197AttributeSet AttributeList::getRetAttributes() const {
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00001198 return getAttributes(ReturnIndex);
Bill Wendlingd509a662013-01-29 00:34:06 +00001199}
1200
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001201AttributeSet AttributeList::getFnAttributes() const {
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00001202 return getAttributes(FunctionIndex);
Bill Wendlingd509a662013-01-29 00:34:06 +00001203}
1204
Reid Klecknerb5180542017-03-21 16:57:19 +00001205bool AttributeList::hasAttribute(unsigned Index,
1206 Attribute::AttrKind Kind) const {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001207 return getAttributes(Index).hasAttribute(Kind);
Bill Wendlingd509a662013-01-29 00:34:06 +00001208}
1209
Reid Klecknerb5180542017-03-21 16:57:19 +00001210bool AttributeList::hasAttribute(unsigned Index, StringRef Kind) const {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001211 return getAttributes(Index).hasAttribute(Kind);
Bill Wendlingbce7b972013-02-13 08:42:21 +00001212}
1213
Reid Klecknerb5180542017-03-21 16:57:19 +00001214bool AttributeList::hasAttributes(unsigned Index) const {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001215 return getAttributes(Index).hasAttributes();
Bill Wendlingd509a662013-01-29 00:34:06 +00001216}
1217
Reid Klecknerb5180542017-03-21 16:57:19 +00001218bool AttributeList::hasFnAttribute(Attribute::AttrKind Kind) const {
Matthias Braun33282812016-01-29 22:25:19 +00001219 return pImpl && pImpl->hasFnAttribute(Kind);
1220}
1221
Reid Klecknerb5180542017-03-21 16:57:19 +00001222bool AttributeList::hasFnAttribute(StringRef Kind) const {
1223 return hasAttribute(AttributeList::FunctionIndex, Kind);
Amaury Sechet5f04d812016-09-09 04:50:38 +00001224}
1225
Reid Klecknerf021fab2017-04-13 23:12:13 +00001226bool AttributeList::hasParamAttribute(unsigned ArgNo,
1227 Attribute::AttrKind Kind) const {
1228 return hasAttribute(ArgNo + 1, Kind);
1229}
1230
Reid Klecknerb5180542017-03-21 16:57:19 +00001231bool AttributeList::hasAttrSomewhere(Attribute::AttrKind Attr,
1232 unsigned *Index) const {
Craig Topperc6207612014-04-09 06:08:46 +00001233 if (!pImpl) return false;
Bill Wendlingd509a662013-01-29 00:34:06 +00001234
Amaury Sechet24c84fd2016-06-14 22:04:16 +00001235 for (unsigned I = 0, E = pImpl->getNumSlots(); I != E; ++I)
Reid Klecknerb5180542017-03-21 16:57:19 +00001236 for (AttributeListImpl::iterator II = pImpl->begin(I), IE = pImpl->end(I);
1237 II != IE; ++II)
Hal Finkele87ad542016-07-10 23:01:32 +00001238 if (II->hasAttribute(Attr)) {
1239 if (Index) *Index = pImpl->getSlotIndex(I);
Bill Wendlingd509a662013-01-29 00:34:06 +00001240 return true;
Hal Finkele87ad542016-07-10 23:01:32 +00001241 }
Bill Wendlingd509a662013-01-29 00:34:06 +00001242
1243 return false;
1244}
1245
Reid Klecknerb5180542017-03-21 16:57:19 +00001246Attribute AttributeList::getAttribute(unsigned Index,
1247 Attribute::AttrKind Kind) const {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001248 return getAttributes(Index).getAttribute(Kind);
Bill Wendlingbce7b972013-02-13 08:42:21 +00001249}
1250
Reid Klecknerb5180542017-03-21 16:57:19 +00001251Attribute AttributeList::getAttribute(unsigned Index, StringRef Kind) const {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001252 return getAttributes(Index).getAttribute(Kind);
Bill Wendlingbce7b972013-02-13 08:42:21 +00001253}
1254
Reid Kleckner859f8b52017-04-28 20:34:27 +00001255unsigned AttributeList::getRetAlignment() const {
1256 return getAttributes(ReturnIndex).getAlignment();
1257}
1258
1259unsigned AttributeList::getParamAlignment(unsigned ArgNo) const {
Reid Klecknera0b45f42017-05-03 18:17:31 +00001260 return getAttributes(ArgNo + FirstArgIndex).getAlignment();
Bill Wendlingd509a662013-01-29 00:34:06 +00001261}
1262
Reid Klecknerb5180542017-03-21 16:57:19 +00001263unsigned AttributeList::getStackAlignment(unsigned Index) const {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001264 return getAttributes(Index).getStackAlignment();
Bill Wendlingd509a662013-01-29 00:34:06 +00001265}
1266
Reid Klecknerb5180542017-03-21 16:57:19 +00001267uint64_t AttributeList::getDereferenceableBytes(unsigned Index) const {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001268 return getAttributes(Index).getDereferenceableBytes();
Hal Finkelb0407ba2014-07-18 15:51:28 +00001269}
1270
Reid Klecknerb5180542017-03-21 16:57:19 +00001271uint64_t AttributeList::getDereferenceableOrNullBytes(unsigned Index) const {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001272 return getAttributes(Index).getDereferenceableOrNullBytes();
Sanjoy Das06cf33f2015-05-06 17:41:54 +00001273}
1274
George Burgess IV278199f2016-04-12 01:05:35 +00001275std::pair<unsigned, Optional<unsigned>>
Reid Klecknerb5180542017-03-21 16:57:19 +00001276AttributeList::getAllocSizeArgs(unsigned Index) const {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001277 return getAttributes(Index).getAllocSizeArgs();
George Burgess IV278199f2016-04-12 01:05:35 +00001278}
1279
Reid Klecknerb5180542017-03-21 16:57:19 +00001280std::string AttributeList::getAsString(unsigned Index, bool InAttrGrp) const {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001281 return getAttributes(Index).getAsString(InAttrGrp);
Bill Wendlingd509a662013-01-29 00:34:06 +00001282}
1283
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001284AttributeSet AttributeList::getAttributes(unsigned Index) const {
1285 if (!pImpl) return AttributeSet();
Bill Wendlingd509a662013-01-29 00:34:06 +00001286
Bill Wendlingf2955aa2013-01-29 03:20:31 +00001287 // Loop through to find the attribute node we want.
Amaury Sechet24c84fd2016-06-14 22:04:16 +00001288 for (unsigned I = 0, E = pImpl->getNumSlots(); I != E; ++I)
Bill Wendling211316c2013-04-18 20:17:28 +00001289 if (pImpl->getSlotIndex(I) == Index)
Reid Kleckner63b26f02017-04-24 22:25:02 +00001290 return pImpl->getSlotAttributes(I);
Bill Wendlingd509a662013-01-29 00:34:06 +00001291
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001292 return AttributeSet();
Bill Wendlingd509a662013-01-29 00:34:06 +00001293}
1294
Reid Klecknerb5180542017-03-21 16:57:19 +00001295AttributeList::iterator AttributeList::begin(unsigned Slot) const {
Bill Wendlinga9174862013-01-31 23:53:05 +00001296 if (!pImpl)
1297 return ArrayRef<Attribute>().begin();
Bill Wendling211316c2013-04-18 20:17:28 +00001298 return pImpl->begin(Slot);
Bill Wendlinga9174862013-01-31 23:53:05 +00001299}
1300
Reid Klecknerb5180542017-03-21 16:57:19 +00001301AttributeList::iterator AttributeList::end(unsigned Slot) const {
Bill Wendlinga9174862013-01-31 23:53:05 +00001302 if (!pImpl)
1303 return ArrayRef<Attribute>().end();
Bill Wendling211316c2013-04-18 20:17:28 +00001304 return pImpl->end(Slot);
Bill Wendlinga9174862013-01-31 23:53:05 +00001305}
1306
Bill Wendlingd509a662013-01-29 00:34:06 +00001307//===----------------------------------------------------------------------===//
Reid Klecknerb5180542017-03-21 16:57:19 +00001308// AttributeList Introspection Methods
Bill Wendlingd509a662013-01-29 00:34:06 +00001309//===----------------------------------------------------------------------===//
1310
Reid Klecknerb5180542017-03-21 16:57:19 +00001311unsigned AttributeList::getNumSlots() const {
Amaury Sechet24c84fd2016-06-14 22:04:16 +00001312 return pImpl ? pImpl->getNumSlots() : 0;
Bill Wendlingd509a662013-01-29 00:34:06 +00001313}
1314
Reid Klecknerb5180542017-03-21 16:57:19 +00001315unsigned AttributeList::getSlotIndex(unsigned Slot) const {
Amaury Sechet24c84fd2016-06-14 22:04:16 +00001316 assert(pImpl && Slot < pImpl->getNumSlots() &&
Bill Wendlingd509a662013-01-29 00:34:06 +00001317 "Slot # out of range!");
1318 return pImpl->getSlotIndex(Slot);
1319}
1320
Reid Kleckner63b26f02017-04-24 22:25:02 +00001321AttributeSet AttributeList::getSlotAttributes(unsigned Slot) const {
Amaury Sechet24c84fd2016-06-14 22:04:16 +00001322 assert(pImpl && Slot < pImpl->getNumSlots() &&
Bill Wendlingd509a662013-01-29 00:34:06 +00001323 "Slot # out of range!");
1324 return pImpl->getSlotAttributes(Slot);
1325}
1326
Matthias Braun8c209aa2017-01-28 02:02:38 +00001327#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Reid Klecknerb5180542017-03-21 16:57:19 +00001328LLVM_DUMP_METHOD void AttributeList::dump() const {
Bill Wendlingd509a662013-01-29 00:34:06 +00001329 dbgs() << "PAL[\n";
1330
1331 for (unsigned i = 0, e = getNumSlots(); i < e; ++i) {
1332 uint64_t Index = getSlotIndex(i);
1333 dbgs() << " { ";
1334 if (Index == ~0U)
1335 dbgs() << "~0U";
1336 else
1337 dbgs() << Index;
1338 dbgs() << " => " << getAsString(Index) << " }\n";
1339 }
1340
1341 dbgs() << "]\n";
1342}
Matthias Braun8c209aa2017-01-28 02:02:38 +00001343#endif
Bill Wendlingd509a662013-01-29 00:34:06 +00001344
Bill Wendlingabf3feb2012-10-05 06:44:41 +00001345//===----------------------------------------------------------------------===//
Bill Wendlingcd330342013-01-04 23:27:34 +00001346// AttrBuilder Method Implementations
Bill Wendlingabf3feb2012-10-05 06:44:41 +00001347//===----------------------------------------------------------------------===//
1348
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001349AttrBuilder::AttrBuilder(AttributeList AL, unsigned Index) {
1350 AttributeListImpl *pImpl = AL.pImpl;
Bill Wendling096f5442013-01-07 08:24:35 +00001351 if (!pImpl) return;
1352
Amaury Sechet24c84fd2016-06-14 22:04:16 +00001353 for (unsigned I = 0, E = pImpl->getNumSlots(); I != E; ++I) {
Bill Wendling211316c2013-04-18 20:17:28 +00001354 if (pImpl->getSlotIndex(I) != Index) continue;
Bill Wendling096f5442013-01-07 08:24:35 +00001355
Reid Klecknerb5180542017-03-21 16:57:19 +00001356 for (AttributeListImpl::iterator II = pImpl->begin(I), IE = pImpl->end(I);
1357 II != IE; ++II)
Bill Wendling23804da2013-01-31 23:38:01 +00001358 addAttribute(*II);
Bill Wendling9eb689c2013-01-28 00:21:34 +00001359
1360 break;
1361 }
Bill Wendling096f5442013-01-07 08:24:35 +00001362}
1363
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001364AttrBuilder::AttrBuilder(AttributeSet AS) {
1365 if (AS.hasAttributes()) {
1366 for (const Attribute &A : AS)
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00001367 addAttribute(A);
1368 }
1369}
1370
Bill Wendlingcd330342013-01-04 23:27:34 +00001371void AttrBuilder::clear() {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001372 Attrs.reset();
Sanjoy Das88d0fde2015-09-03 22:27:42 +00001373 TargetDepAttrs.clear();
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001374 Alignment = StackAlignment = DerefBytes = DerefOrNullBytes = 0;
George Burgess IV278199f2016-04-12 01:05:35 +00001375 AllocSizeArgs = 0;
Bill Wendlingcd330342013-01-04 23:27:34 +00001376}
1377
1378AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001379 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
Bill Wendling1c7cc8a2013-01-31 23:16:25 +00001380 assert(Val != Attribute::Alignment && Val != Attribute::StackAlignment &&
George Burgess IV278199f2016-04-12 01:05:35 +00001381 Val != Attribute::Dereferenceable && Val != Attribute::AllocSize &&
Hal Finkelb0407ba2014-07-18 15:51:28 +00001382 "Adding integer attribute without adding a value!");
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001383 Attrs[Val] = true;
Bill Wendling7c04e042012-10-09 19:01:18 +00001384 return *this;
Bill Wendlingabf3feb2012-10-05 06:44:41 +00001385}
1386
Bill Wendling23804da2013-01-31 23:38:01 +00001387AttrBuilder &AttrBuilder::addAttribute(Attribute Attr) {
Bill Wendling0a437302013-02-10 10:13:23 +00001388 if (Attr.isStringAttribute()) {
1389 addAttribute(Attr.getKindAsString(), Attr.getValueAsString());
1390 return *this;
1391 }
1392
Bill Wendling3f12ac22013-02-05 22:37:24 +00001393 Attribute::AttrKind Kind = Attr.getKindAsEnum();
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001394 Attrs[Kind] = true;
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001395
Bill Wendling3f12ac22013-02-05 22:37:24 +00001396 if (Kind == Attribute::Alignment)
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001397 Alignment = Attr.getAlignment();
Bill Wendling3f12ac22013-02-05 22:37:24 +00001398 else if (Kind == Attribute::StackAlignment)
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001399 StackAlignment = Attr.getStackAlignment();
Hal Finkelb0407ba2014-07-18 15:51:28 +00001400 else if (Kind == Attribute::Dereferenceable)
1401 DerefBytes = Attr.getDereferenceableBytes();
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001402 else if (Kind == Attribute::DereferenceableOrNull)
1403 DerefOrNullBytes = Attr.getDereferenceableOrNullBytes();
George Burgess IV278199f2016-04-12 01:05:35 +00001404 else if (Kind == Attribute::AllocSize)
1405 AllocSizeArgs = Attr.getValueAsInt();
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001406 return *this;
1407}
1408
Bill Wendlingb9c5b1a2013-02-05 08:09:32 +00001409AttrBuilder &AttrBuilder::addAttribute(StringRef A, StringRef V) {
1410 TargetDepAttrs[A] = V;
1411 return *this;
1412}
1413
Bill Wendling23804da2013-01-31 23:38:01 +00001414AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001415 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
1416 Attrs[Val] = false;
Bill Wendling23804da2013-01-31 23:38:01 +00001417
1418 if (Val == Attribute::Alignment)
1419 Alignment = 0;
1420 else if (Val == Attribute::StackAlignment)
1421 StackAlignment = 0;
Hal Finkelb0407ba2014-07-18 15:51:28 +00001422 else if (Val == Attribute::Dereferenceable)
1423 DerefBytes = 0;
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001424 else if (Val == Attribute::DereferenceableOrNull)
1425 DerefOrNullBytes = 0;
George Burgess IV278199f2016-04-12 01:05:35 +00001426 else if (Val == Attribute::AllocSize)
1427 AllocSizeArgs = 0;
Bill Wendling23804da2013-01-31 23:38:01 +00001428
1429 return *this;
1430}
1431
Reid Klecknerb5180542017-03-21 16:57:19 +00001432AttrBuilder &AttrBuilder::removeAttributes(AttributeList A, uint64_t Index) {
Reid Kleckner6652a522017-04-28 18:37:16 +00001433 remove(A.getAttributes(Index));
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001434 return *this;
1435}
1436
Bill Wendlingb9c5b1a2013-02-05 08:09:32 +00001437AttrBuilder &AttrBuilder::removeAttribute(StringRef A) {
1438 std::map<std::string, std::string>::iterator I = TargetDepAttrs.find(A);
1439 if (I != TargetDepAttrs.end())
1440 TargetDepAttrs.erase(I);
1441 return *this;
1442}
1443
George Burgess IV278199f2016-04-12 01:05:35 +00001444std::pair<unsigned, Optional<unsigned>> AttrBuilder::getAllocSizeArgs() const {
1445 return unpackAllocSizeArgs(AllocSizeArgs);
1446}
1447
Bill Wendling50d27842012-10-15 20:35:56 +00001448AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
Bill Wendlingabd5ba22012-10-14 03:58:29 +00001449 if (Align == 0) return *this;
Bill Wendlingcd330342013-01-04 23:27:34 +00001450
Bill Wendlingabf3feb2012-10-05 06:44:41 +00001451 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1452 assert(Align <= 0x40000000 && "Alignment too large.");
Bill Wendlingcd330342013-01-04 23:27:34 +00001453
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001454 Attrs[Attribute::Alignment] = true;
Bill Wendlingcd330342013-01-04 23:27:34 +00001455 Alignment = Align;
Bill Wendlingabd5ba22012-10-14 03:58:29 +00001456 return *this;
Chris Lattner3e13b8c2008-01-02 23:42:30 +00001457}
1458
Bill Wendlingcd330342013-01-04 23:27:34 +00001459AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
1460 // Default alignment, allow the target to define how to align it.
1461 if (Align == 0) return *this;
1462
1463 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1464 assert(Align <= 0x100 && "Alignment too large.");
1465
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001466 Attrs[Attribute::StackAlignment] = true;
Bill Wendlingcd330342013-01-04 23:27:34 +00001467 StackAlignment = Align;
1468 return *this;
1469}
1470
Hal Finkelb0407ba2014-07-18 15:51:28 +00001471AttrBuilder &AttrBuilder::addDereferenceableAttr(uint64_t Bytes) {
1472 if (Bytes == 0) return *this;
1473
1474 Attrs[Attribute::Dereferenceable] = true;
1475 DerefBytes = Bytes;
1476 return *this;
1477}
1478
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001479AttrBuilder &AttrBuilder::addDereferenceableOrNullAttr(uint64_t Bytes) {
1480 if (Bytes == 0)
1481 return *this;
1482
1483 Attrs[Attribute::DereferenceableOrNull] = true;
1484 DerefOrNullBytes = Bytes;
1485 return *this;
1486}
1487
George Burgess IV278199f2016-04-12 01:05:35 +00001488AttrBuilder &AttrBuilder::addAllocSizeAttr(unsigned ElemSize,
1489 const Optional<unsigned> &NumElems) {
1490 return addAllocSizeAttrFromRawRepr(packAllocSizeArgs(ElemSize, NumElems));
1491}
1492
1493AttrBuilder &AttrBuilder::addAllocSizeAttrFromRawRepr(uint64_t RawArgs) {
1494 // (0, 0) is our "not present" value, so we need to check for it here.
1495 assert(RawArgs && "Invalid allocsize arguments -- given allocsize(0, 0)");
1496
1497 Attrs[Attribute::AllocSize] = true;
1498 // Reuse existing machinery to store this as a single 64-bit integer so we can
1499 // save a few bytes over using a pair<unsigned, Optional<unsigned>>.
1500 AllocSizeArgs = RawArgs;
1501 return *this;
1502}
1503
Bill Wendlinge2614922013-02-06 01:16:00 +00001504AttrBuilder &AttrBuilder::merge(const AttrBuilder &B) {
1505 // FIXME: What if both have alignments, but they don't match?!
1506 if (!Alignment)
1507 Alignment = B.Alignment;
1508
1509 if (!StackAlignment)
1510 StackAlignment = B.StackAlignment;
1511
Hal Finkelb0407ba2014-07-18 15:51:28 +00001512 if (!DerefBytes)
1513 DerefBytes = B.DerefBytes;
1514
Pete Cooperd2a44612015-05-06 23:19:43 +00001515 if (!DerefOrNullBytes)
1516 DerefOrNullBytes = B.DerefOrNullBytes;
1517
George Burgess IV278199f2016-04-12 01:05:35 +00001518 if (!AllocSizeArgs)
1519 AllocSizeArgs = B.AllocSizeArgs;
1520
Benjamin Kramer45e7d532013-02-16 19:13:18 +00001521 Attrs |= B.Attrs;
Bill Wendlinge2614922013-02-06 01:16:00 +00001522
Pete Cooperd2a44612015-05-06 23:19:43 +00001523 for (auto I : B.td_attrs())
1524 TargetDepAttrs[I.first] = I.second;
Bill Wendlinge2614922013-02-06 01:16:00 +00001525
1526 return *this;
1527}
1528
Pete Cooperd2a44612015-05-06 23:19:43 +00001529AttrBuilder &AttrBuilder::remove(const AttrBuilder &B) {
1530 // FIXME: What if both have alignments, but they don't match?!
1531 if (B.Alignment)
1532 Alignment = 0;
1533
1534 if (B.StackAlignment)
1535 StackAlignment = 0;
1536
1537 if (B.DerefBytes)
1538 DerefBytes = 0;
1539
1540 if (B.DerefOrNullBytes)
1541 DerefOrNullBytes = 0;
1542
George Burgess IV278199f2016-04-12 01:05:35 +00001543 if (B.AllocSizeArgs)
1544 AllocSizeArgs = 0;
1545
Pete Cooperd2a44612015-05-06 23:19:43 +00001546 Attrs &= ~B.Attrs;
1547
1548 for (auto I : B.td_attrs())
1549 TargetDepAttrs.erase(I.first);
1550
1551 return *this;
1552}
1553
1554bool AttrBuilder::overlaps(const AttrBuilder &B) const {
1555 // First check if any of the target independent attributes overlap.
1556 if ((Attrs & B.Attrs).any())
1557 return true;
1558
1559 // Then check if any target dependent ones do.
Sean Silva9011aca2017-02-22 06:34:04 +00001560 for (const auto &I : td_attrs())
Pete Cooperd2a44612015-05-06 23:19:43 +00001561 if (B.contains(I.first))
1562 return true;
1563
1564 return false;
1565}
1566
Bill Wendling4b001442013-02-06 01:33:42 +00001567bool AttrBuilder::contains(StringRef A) const {
1568 return TargetDepAttrs.find(A) != TargetDepAttrs.end();
1569}
1570
Bill Wendling50d27842012-10-15 20:35:56 +00001571bool AttrBuilder::hasAttributes() const {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001572 return !Attrs.none() || !TargetDepAttrs.empty();
Bill Wendlingc6daefa2012-10-08 23:27:46 +00001573}
Bill Wendling9ac69f92013-01-04 20:54:35 +00001574
Reid Kleckner6652a522017-04-28 18:37:16 +00001575bool AttrBuilder::hasAttributes(AttributeList AL, uint64_t Index) const {
1576 AttributeSet AS = AL.getAttributes(Index);
Bill Wendling9ca01da2013-02-02 00:42:06 +00001577
Reid Kleckner6652a522017-04-28 18:37:16 +00001578 for (Attribute Attr : AS) {
Hal Finkele15442c2014-07-18 06:51:55 +00001579 if (Attr.isEnumAttribute() || Attr.isIntAttribute()) {
Reid Kleckner6652a522017-04-28 18:37:16 +00001580 if (contains(Attr.getKindAsEnum()))
Bill Wendling7cde51d2013-02-12 07:56:49 +00001581 return true;
1582 } else {
1583 assert(Attr.isStringAttribute() && "Invalid attribute kind!");
Reid Kleckner6652a522017-04-28 18:37:16 +00001584 return contains(Attr.getKindAsString());
Bill Wendling7cde51d2013-02-12 07:56:49 +00001585 }
1586 }
Bill Wendling9ca01da2013-02-02 00:42:06 +00001587
1588 return false;
Bill Wendling70f39172012-10-09 00:01:21 +00001589}
Bill Wendling9ac69f92013-01-04 20:54:35 +00001590
Bill Wendling50d27842012-10-15 20:35:56 +00001591bool AttrBuilder::hasAlignmentAttr() const {
Bill Wendlingcd330342013-01-04 23:27:34 +00001592 return Alignment != 0;
Bill Wendlingc6daefa2012-10-08 23:27:46 +00001593}
1594
Bill Wendlingd509a662013-01-29 00:34:06 +00001595bool AttrBuilder::operator==(const AttrBuilder &B) {
Benjamin Kramer45e7d532013-02-16 19:13:18 +00001596 if (Attrs != B.Attrs)
1597 return false;
Bill Wendling4b001442013-02-06 01:33:42 +00001598
1599 for (td_const_iterator I = TargetDepAttrs.begin(),
1600 E = TargetDepAttrs.end(); I != E; ++I)
1601 if (B.TargetDepAttrs.find(I->first) == B.TargetDepAttrs.end())
1602 return false;
1603
Hal Finkelb0407ba2014-07-18 15:51:28 +00001604 return Alignment == B.Alignment && StackAlignment == B.StackAlignment &&
1605 DerefBytes == B.DerefBytes;
Bill Wendlingd509a662013-01-29 00:34:06 +00001606}
1607
Bill Wendling57625a42013-01-25 23:09:36 +00001608//===----------------------------------------------------------------------===//
1609// AttributeFuncs Function Defintions
1610//===----------------------------------------------------------------------===//
1611
Bill Wendlingc79cdff2013-02-01 01:04:27 +00001612/// \brief Which attributes cannot be applied to a type.
Craig Toppere3dcce92015-08-01 22:20:21 +00001613AttrBuilder AttributeFuncs::typeIncompatible(Type *Ty) {
Bill Wendling57625a42013-01-25 23:09:36 +00001614 AttrBuilder Incompatible;
1615
1616 if (!Ty->isIntegerTy())
1617 // Attribute that only apply to integers.
1618 Incompatible.addAttribute(Attribute::SExt)
1619 .addAttribute(Attribute::ZExt);
1620
1621 if (!Ty->isPointerTy())
1622 // Attribute that only apply to pointers.
1623 Incompatible.addAttribute(Attribute::ByVal)
1624 .addAttribute(Attribute::Nest)
1625 .addAttribute(Attribute::NoAlias)
1626 .addAttribute(Attribute::NoCapture)
Nick Lewyckyd52b1522014-05-20 01:23:40 +00001627 .addAttribute(Attribute::NonNull)
Hal Finkelb0407ba2014-07-18 15:51:28 +00001628 .addDereferenceableAttr(1) // the int here is ignored
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001629 .addDereferenceableOrNullAttr(1) // the int here is ignored
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001630 .addAttribute(Attribute::ReadNone)
1631 .addAttribute(Attribute::ReadOnly)
Reid Klecknera534a382013-12-19 02:14:12 +00001632 .addAttribute(Attribute::StructRet)
1633 .addAttribute(Attribute::InAlloca);
Bill Wendling57625a42013-01-25 23:09:36 +00001634
Pete Cooper2777d8872015-05-06 23:19:56 +00001635 return Incompatible;
Bill Wendling57625a42013-01-25 23:09:36 +00001636}
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001637
1638template<typename AttrClass>
1639static bool isEqual(const Function &Caller, const Function &Callee) {
1640 return Caller.getFnAttribute(AttrClass::getKind()) ==
1641 Callee.getFnAttribute(AttrClass::getKind());
1642}
1643
1644/// \brief Compute the logical AND of the attributes of the caller and the
1645/// callee.
1646///
1647/// This function sets the caller's attribute to false if the callee's attribute
1648/// is false.
1649template<typename AttrClass>
1650static void setAND(Function &Caller, const Function &Callee) {
1651 if (AttrClass::isSet(Caller, AttrClass::getKind()) &&
1652 !AttrClass::isSet(Callee, AttrClass::getKind()))
1653 AttrClass::set(Caller, AttrClass::getKind(), false);
1654}
1655
1656/// \brief Compute the logical OR of the attributes of the caller and the
1657/// callee.
1658///
1659/// This function sets the caller's attribute to true if the callee's attribute
1660/// is true.
1661template<typename AttrClass>
1662static void setOR(Function &Caller, const Function &Callee) {
1663 if (!AttrClass::isSet(Caller, AttrClass::getKind()) &&
1664 AttrClass::isSet(Callee, AttrClass::getKind()))
1665 AttrClass::set(Caller, AttrClass::getKind(), true);
1666}
1667
1668/// \brief If the inlined function had a higher stack protection level than the
1669/// calling function, then bump up the caller's stack protection level.
1670static void adjustCallerSSPLevel(Function &Caller, const Function &Callee) {
1671 // If upgrading the SSP attribute, clear out the old SSP Attributes first.
1672 // Having multiple SSP attributes doesn't actually hurt, but it adds useless
1673 // clutter to the IR.
Reid Kleckneree4930b2017-05-02 22:07:37 +00001674 AttrBuilder OldSSPAttr;
1675 OldSSPAttr.addAttribute(Attribute::StackProtect)
1676 .addAttribute(Attribute::StackProtectStrong)
1677 .addAttribute(Attribute::StackProtectReq);
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001678
Evgeniy Stepanovf17120a2016-04-11 22:27:48 +00001679 if (Callee.hasFnAttribute(Attribute::StackProtectReq)) {
Reid Klecknerb5180542017-03-21 16:57:19 +00001680 Caller.removeAttributes(AttributeList::FunctionIndex, OldSSPAttr);
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001681 Caller.addFnAttr(Attribute::StackProtectReq);
1682 } else if (Callee.hasFnAttribute(Attribute::StackProtectStrong) &&
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001683 !Caller.hasFnAttribute(Attribute::StackProtectReq)) {
Reid Klecknerb5180542017-03-21 16:57:19 +00001684 Caller.removeAttributes(AttributeList::FunctionIndex, OldSSPAttr);
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001685 Caller.addFnAttr(Attribute::StackProtectStrong);
1686 } else if (Callee.hasFnAttribute(Attribute::StackProtect) &&
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001687 !Caller.hasFnAttribute(Attribute::StackProtectReq) &&
1688 !Caller.hasFnAttribute(Attribute::StackProtectStrong))
1689 Caller.addFnAttr(Attribute::StackProtect);
1690}
1691
1692#define GET_ATTR_COMPAT_FUNC
1693#include "AttributesCompatFunc.inc"
1694
1695bool AttributeFuncs::areInlineCompatible(const Function &Caller,
1696 const Function &Callee) {
1697 return hasCompatibleFnAttrs(Caller, Callee);
1698}
1699
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001700void AttributeFuncs::mergeAttributesForInlining(Function &Caller,
1701 const Function &Callee) {
1702 mergeFnAttrs(Caller, Callee);
1703}