blob: 7077da14a186cacb2f63627ffa21fa37bd306ea2 [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"
Reid Klecknereb9dd5b2017-04-10 23:31:05 +000026#include "llvm/IR/AttributeSetNode.h"
Eugene Zelenkodeaf6952017-02-17 00:00:09 +000027#include "llvm/IR/Attributes.h"
28#include "llvm/IR/Function.h"
29#include "llvm/IR/LLVMContext.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000030#include "llvm/IR/Type.h"
Eugene Zelenkodeaf6952017-02-17 00:00:09 +000031#include "llvm/Support/Compiler.h"
David Greenef7014732010-01-05 01:29:58 +000032#include "llvm/Support/Debug.h"
Eugene Zelenkodeaf6952017-02-17 00:00:09 +000033#include "llvm/Support/ErrorHandling.h"
34#include "llvm/Support/MathExtras.h"
Benjamin Kramer1a25d732009-08-23 11:37:21 +000035#include "llvm/Support/raw_ostream.h"
Bill Wendlingd2e493b2013-01-24 00:06:56 +000036#include <algorithm>
Eugene Zelenkodeaf6952017-02-17 00:00:09 +000037#include <cassert>
38#include <cstdint>
39#include <limits>
40#include <map>
41#include <string>
42#include <tuple>
43#include <utility>
44
Chris Lattner3e13b8c2008-01-02 23:42:30 +000045using namespace llvm;
46
Chris Lattner8a923e72008-03-12 17:45:29 +000047//===----------------------------------------------------------------------===//
Bill Wendling7707c5a2013-01-29 00:48:16 +000048// Attribute Construction Methods
Chris Lattner8a923e72008-03-12 17:45:29 +000049//===----------------------------------------------------------------------===//
Chris Lattnerd0e1f102008-01-03 00:10:22 +000050
George Burgess IV278199f2016-04-12 01:05:35 +000051// allocsize has two integer arguments, but because they're both 32 bits, we can
52// pack them into one 64-bit value, at the cost of making said value
53// nonsensical.
54//
55// In order to do this, we need to reserve one value of the second (optional)
56// allocsize argument to signify "not present."
George Burgess IV381fc0e2016-08-25 01:05:08 +000057static const unsigned AllocSizeNumElemsNotPresent = -1;
George Burgess IV278199f2016-04-12 01:05:35 +000058
59static uint64_t packAllocSizeArgs(unsigned ElemSizeArg,
60 const Optional<unsigned> &NumElemsArg) {
61 assert((!NumElemsArg.hasValue() ||
62 *NumElemsArg != AllocSizeNumElemsNotPresent) &&
63 "Attempting to pack a reserved value");
64
65 return uint64_t(ElemSizeArg) << 32 |
66 NumElemsArg.getValueOr(AllocSizeNumElemsNotPresent);
67}
68
69static std::pair<unsigned, Optional<unsigned>>
70unpackAllocSizeArgs(uint64_t Num) {
71 unsigned NumElems = Num & std::numeric_limits<unsigned>::max();
72 unsigned ElemSizeArg = Num >> 32;
73
74 Optional<unsigned> NumElemsArg;
75 if (NumElems != AllocSizeNumElemsNotPresent)
76 NumElemsArg = NumElems;
77 return std::make_pair(ElemSizeArg, NumElemsArg);
78}
79
Matt Arsenaulte1cef6e2014-09-03 23:24:31 +000080Attribute Attribute::get(LLVMContext &Context, Attribute::AttrKind Kind,
81 uint64_t Val) {
82 LLVMContextImpl *pImpl = Context.pImpl;
83 FoldingSetNodeID ID;
84 ID.AddInteger(Kind);
Matt Arsenaultfb4308e2014-09-03 23:38:05 +000085 if (Val) ID.AddInteger(Val);
Matt Arsenaulte1cef6e2014-09-03 23:24:31 +000086
87 void *InsertPoint;
88 AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
89
90 if (!PA) {
91 // If we didn't find any existing attributes of the same shape then create a
92 // new one and insert it.
Matt Arsenaultfb4308e2014-09-03 23:38:05 +000093 if (!Val)
94 PA = new EnumAttributeImpl(Kind);
95 else
96 PA = new IntAttributeImpl(Kind, Val);
Bill Wendling3f12ac22013-02-05 22:37:24 +000097 pImpl->AttrsSet.InsertNode(PA, InsertPoint);
98 }
99
100 // Return the Attribute that we found or created.
101 return Attribute(PA);
102}
103
104Attribute Attribute::get(LLVMContext &Context, StringRef Kind, StringRef Val) {
105 LLVMContextImpl *pImpl = Context.pImpl;
106 FoldingSetNodeID ID;
107 ID.AddString(Kind);
108 if (!Val.empty()) ID.AddString(Val);
Bill Wendling73ea2de2012-10-08 21:47:17 +0000109
110 void *InsertPoint;
Bill Wendling4607f4b2012-12-20 01:36:59 +0000111 AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
Bill Wendling73ea2de2012-10-08 21:47:17 +0000112
113 if (!PA) {
114 // If we didn't find any existing attributes of the same shape then create a
115 // new one and insert it.
Benjamin Kramer741146b2013-07-11 12:13:16 +0000116 PA = new StringAttributeImpl(Kind, Val);
Bill Wendling73ea2de2012-10-08 21:47:17 +0000117 pImpl->AttrsSet.InsertNode(PA, InsertPoint);
118 }
119
Bill Wendlingb9c5b1a2013-02-05 08:09:32 +0000120 // Return the Attribute that we found or created.
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000121 return Attribute(PA);
Bill Wendling73ea2de2012-10-08 21:47:17 +0000122}
123
Bill Wendling4bbe9db2013-01-27 22:43:04 +0000124Attribute Attribute::getWithAlignment(LLVMContext &Context, uint64_t Align) {
Bill Wendling1c7cc8a2013-01-31 23:16:25 +0000125 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
126 assert(Align <= 0x40000000 && "Alignment too large.");
Bill Wendling3f12ac22013-02-05 22:37:24 +0000127 return get(Context, Alignment, Align);
Bill Wendling4bbe9db2013-01-27 22:43:04 +0000128}
129
130Attribute Attribute::getWithStackAlignment(LLVMContext &Context,
131 uint64_t Align) {
Bill Wendling1c7cc8a2013-01-31 23:16:25 +0000132 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
133 assert(Align <= 0x100 && "Alignment too large.");
Bill Wendling3f12ac22013-02-05 22:37:24 +0000134 return get(Context, StackAlignment, Align);
Bill Wendling4bbe9db2013-01-27 22:43:04 +0000135}
136
Hal Finkelb0407ba2014-07-18 15:51:28 +0000137Attribute Attribute::getWithDereferenceableBytes(LLVMContext &Context,
138 uint64_t Bytes) {
139 assert(Bytes && "Bytes must be non-zero.");
140 return get(Context, Dereferenceable, Bytes);
141}
142
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000143Attribute Attribute::getWithDereferenceableOrNullBytes(LLVMContext &Context,
144 uint64_t Bytes) {
145 assert(Bytes && "Bytes must be non-zero.");
146 return get(Context, DereferenceableOrNull, Bytes);
147}
148
George Burgess IV278199f2016-04-12 01:05:35 +0000149Attribute
150Attribute::getWithAllocSizeArgs(LLVMContext &Context, unsigned ElemSizeArg,
151 const Optional<unsigned> &NumElemsArg) {
152 assert(!(ElemSizeArg == 0 && NumElemsArg && *NumElemsArg == 0) &&
153 "Invalid allocsize arguments -- given allocsize(0, 0)");
154 return get(Context, AllocSize, packAllocSizeArgs(ElemSizeArg, NumElemsArg));
155}
156
Bill Wendling7707c5a2013-01-29 00:48:16 +0000157//===----------------------------------------------------------------------===//
158// Attribute Accessor Methods
159//===----------------------------------------------------------------------===//
160
Bill Wendling3f12ac22013-02-05 22:37:24 +0000161bool Attribute::isEnumAttribute() const {
162 return pImpl && pImpl->isEnumAttribute();
163}
164
Hal Finkele15442c2014-07-18 06:51:55 +0000165bool Attribute::isIntAttribute() const {
166 return pImpl && pImpl->isIntAttribute();
Bill Wendling3f12ac22013-02-05 22:37:24 +0000167}
168
169bool Attribute::isStringAttribute() const {
170 return pImpl && pImpl->isStringAttribute();
171}
172
173Attribute::AttrKind Attribute::getKindAsEnum() const {
Bill Wendling440e9d82013-07-25 00:34:29 +0000174 if (!pImpl) return None;
Hal Finkele15442c2014-07-18 06:51:55 +0000175 assert((isEnumAttribute() || isIntAttribute()) &&
Bill Wendling3f12ac22013-02-05 22:37:24 +0000176 "Invalid attribute type to get the kind as an enum!");
George Burgess IV500d3032015-12-16 05:21:02 +0000177 return pImpl->getKindAsEnum();
Bill Wendling3f12ac22013-02-05 22:37:24 +0000178}
179
180uint64_t Attribute::getValueAsInt() const {
Bill Wendling440e9d82013-07-25 00:34:29 +0000181 if (!pImpl) return 0;
Hal Finkele15442c2014-07-18 06:51:55 +0000182 assert(isIntAttribute() &&
183 "Expected the attribute to be an integer attribute!");
George Burgess IV500d3032015-12-16 05:21:02 +0000184 return pImpl->getValueAsInt();
Bill Wendling3f12ac22013-02-05 22:37:24 +0000185}
186
187StringRef Attribute::getKindAsString() const {
Bill Wendling440e9d82013-07-25 00:34:29 +0000188 if (!pImpl) return StringRef();
Bill Wendling3f12ac22013-02-05 22:37:24 +0000189 assert(isStringAttribute() &&
190 "Invalid attribute type to get the kind as a string!");
George Burgess IV500d3032015-12-16 05:21:02 +0000191 return pImpl->getKindAsString();
Bill Wendling3f12ac22013-02-05 22:37:24 +0000192}
193
194StringRef Attribute::getValueAsString() const {
Bill Wendling440e9d82013-07-25 00:34:29 +0000195 if (!pImpl) return StringRef();
Bill Wendling3f12ac22013-02-05 22:37:24 +0000196 assert(isStringAttribute() &&
197 "Invalid attribute type to get the value as a string!");
George Burgess IV500d3032015-12-16 05:21:02 +0000198 return pImpl->getValueAsString();
Bill Wendling3f12ac22013-02-05 22:37:24 +0000199}
200
Bill Wendlingae89a0f2013-02-05 23:48:36 +0000201bool Attribute::hasAttribute(AttrKind Kind) const {
202 return (pImpl && pImpl->hasAttribute(Kind)) || (!pImpl && Kind == None);
203}
204
205bool Attribute::hasAttribute(StringRef Kind) const {
206 if (!isStringAttribute()) return false;
207 return pImpl && pImpl->hasAttribute(Kind);
Bill Wendling03eefb32013-01-29 20:45:34 +0000208}
209
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000210unsigned Attribute::getAlignment() const {
Bill Wendlingc79cdff2013-02-01 01:04:27 +0000211 assert(hasAttribute(Attribute::Alignment) &&
212 "Trying to get alignment from non-alignment attribute!");
Bill Wendling3f12ac22013-02-05 22:37:24 +0000213 return pImpl->getValueAsInt();
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000214}
215
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000216unsigned Attribute::getStackAlignment() const {
Bill Wendlingc79cdff2013-02-01 01:04:27 +0000217 assert(hasAttribute(Attribute::StackAlignment) &&
218 "Trying to get alignment from non-alignment attribute!");
Bill Wendling3f12ac22013-02-05 22:37:24 +0000219 return pImpl->getValueAsInt();
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000220}
221
Hal Finkelb0407ba2014-07-18 15:51:28 +0000222uint64_t Attribute::getDereferenceableBytes() const {
223 assert(hasAttribute(Attribute::Dereferenceable) &&
224 "Trying to get dereferenceable bytes from "
225 "non-dereferenceable attribute!");
226 return pImpl->getValueAsInt();
227}
228
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000229uint64_t Attribute::getDereferenceableOrNullBytes() const {
230 assert(hasAttribute(Attribute::DereferenceableOrNull) &&
231 "Trying to get dereferenceable bytes from "
232 "non-dereferenceable attribute!");
233 return pImpl->getValueAsInt();
234}
235
George Burgess IV278199f2016-04-12 01:05:35 +0000236std::pair<unsigned, Optional<unsigned>> Attribute::getAllocSizeArgs() const {
237 assert(hasAttribute(Attribute::AllocSize) &&
238 "Trying to get allocsize args from non-allocsize attribute");
239 return unpackAllocSizeArgs(pImpl->getValueAsInt());
240}
241
Bill Wendling829b4782013-02-11 08:43:33 +0000242std::string Attribute::getAsString(bool InAttrGrp) const {
Bill Wendling9c2eba92013-01-31 20:59:05 +0000243 if (!pImpl) return "";
244
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000245 if (hasAttribute(Attribute::SanitizeAddress))
246 return "sanitize_address";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000247 if (hasAttribute(Attribute::AlwaysInline))
248 return "alwaysinline";
Igor Laevsky39d662f2015-07-11 10:30:36 +0000249 if (hasAttribute(Attribute::ArgMemOnly))
250 return "argmemonly";
Michael Gottesman41748d72013-06-27 00:25:01 +0000251 if (hasAttribute(Attribute::Builtin))
252 return "builtin";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000253 if (hasAttribute(Attribute::ByVal))
254 return "byval";
Owen Anderson85fa7d52015-05-26 23:48:40 +0000255 if (hasAttribute(Attribute::Convergent))
256 return "convergent";
Manman Ren9bfd0d02016-04-01 21:41:15 +0000257 if (hasAttribute(Attribute::SwiftError))
258 return "swifterror";
Manman Renf46262e2016-03-29 17:37:21 +0000259 if (hasAttribute(Attribute::SwiftSelf))
260 return "swiftself";
Vaivaswatha Nagarajfb3f4902015-12-16 16:16:19 +0000261 if (hasAttribute(Attribute::InaccessibleMemOnly))
262 return "inaccessiblememonly";
263 if (hasAttribute(Attribute::InaccessibleMemOrArgMemOnly))
264 return "inaccessiblemem_or_argmemonly";
Reid Klecknera534a382013-12-19 02:14:12 +0000265 if (hasAttribute(Attribute::InAlloca))
266 return "inalloca";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000267 if (hasAttribute(Attribute::InlineHint))
268 return "inlinehint";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000269 if (hasAttribute(Attribute::InReg))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000270 return "inreg";
Tom Roeder44cb65f2014-06-05 19:29:43 +0000271 if (hasAttribute(Attribute::JumpTable))
272 return "jumptable";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000273 if (hasAttribute(Attribute::MinSize))
274 return "minsize";
275 if (hasAttribute(Attribute::Naked))
276 return "naked";
277 if (hasAttribute(Attribute::Nest))
278 return "nest";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000279 if (hasAttribute(Attribute::NoAlias))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000280 return "noalias";
Bill Wendling09bd1f72013-02-22 00:12:35 +0000281 if (hasAttribute(Attribute::NoBuiltin))
282 return "nobuiltin";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000283 if (hasAttribute(Attribute::NoCapture))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000284 return "nocapture";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000285 if (hasAttribute(Attribute::NoDuplicate))
286 return "noduplicate";
287 if (hasAttribute(Attribute::NoImplicitFloat))
288 return "noimplicitfloat";
289 if (hasAttribute(Attribute::NoInline))
290 return "noinline";
291 if (hasAttribute(Attribute::NonLazyBind))
292 return "nonlazybind";
Nick Lewyckyd52b1522014-05-20 01:23:40 +0000293 if (hasAttribute(Attribute::NonNull))
294 return "nonnull";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000295 if (hasAttribute(Attribute::NoRedZone))
296 return "noredzone";
297 if (hasAttribute(Attribute::NoReturn))
298 return "noreturn";
James Molloye6f87ca2015-11-06 10:32:53 +0000299 if (hasAttribute(Attribute::NoRecurse))
300 return "norecurse";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000301 if (hasAttribute(Attribute::NoUnwind))
302 return "nounwind";
Andrea Di Biagio377496b2013-08-23 11:53:55 +0000303 if (hasAttribute(Attribute::OptimizeNone))
304 return "optnone";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000305 if (hasAttribute(Attribute::OptimizeForSize))
306 return "optsize";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000307 if (hasAttribute(Attribute::ReadNone))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000308 return "readnone";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000309 if (hasAttribute(Attribute::ReadOnly))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000310 return "readonly";
Nicolai Haehnle84c9f992016-07-04 08:01:29 +0000311 if (hasAttribute(Attribute::WriteOnly))
312 return "writeonly";
Stephen Linb8bd2322013-04-20 05:14:40 +0000313 if (hasAttribute(Attribute::Returned))
314 return "returned";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000315 if (hasAttribute(Attribute::ReturnsTwice))
316 return "returns_twice";
317 if (hasAttribute(Attribute::SExt))
318 return "signext";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000319 if (hasAttribute(Attribute::StackProtect))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000320 return "ssp";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000321 if (hasAttribute(Attribute::StackProtectReq))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000322 return "sspreq";
Bill Wendlingd154e2832013-01-23 06:41:41 +0000323 if (hasAttribute(Attribute::StackProtectStrong))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000324 return "sspstrong";
Peter Collingbourne82437bf2015-06-15 21:07:11 +0000325 if (hasAttribute(Attribute::SafeStack))
326 return "safestack";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000327 if (hasAttribute(Attribute::StructRet))
328 return "sret";
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000329 if (hasAttribute(Attribute::SanitizeThread))
330 return "sanitize_thread";
331 if (hasAttribute(Attribute::SanitizeMemory))
332 return "sanitize_memory";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000333 if (hasAttribute(Attribute::UWTable))
334 return "uwtable";
335 if (hasAttribute(Attribute::ZExt))
336 return "zeroext";
Diego Novilloc6399532013-05-24 12:26:52 +0000337 if (hasAttribute(Attribute::Cold))
338 return "cold";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000339
340 // FIXME: These should be output like this:
341 //
342 // align=4
343 // alignstack=8
344 //
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000345 if (hasAttribute(Attribute::Alignment)) {
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000346 std::string Result;
Bill Wendling829b4782013-02-11 08:43:33 +0000347 Result += "align";
348 Result += (InAttrGrp) ? "=" : " ";
Bill Wendling3f12ac22013-02-05 22:37:24 +0000349 Result += utostr(getValueAsInt());
350 return Result;
351 }
Bill Wendling829b4782013-02-11 08:43:33 +0000352
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000353 auto AttrWithBytesToString = [&](const char *Name) {
Bill Wendling3f12ac22013-02-05 22:37:24 +0000354 std::string Result;
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000355 Result += Name;
Bill Wendling829b4782013-02-11 08:43:33 +0000356 if (InAttrGrp) {
357 Result += "=";
358 Result += utostr(getValueAsInt());
359 } else {
360 Result += "(";
361 Result += utostr(getValueAsInt());
362 Result += ")";
363 }
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000364 return Result;
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000365 };
Bill Wendling9c2eba92013-01-31 20:59:05 +0000366
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000367 if (hasAttribute(Attribute::StackAlignment))
368 return AttrWithBytesToString("alignstack");
369
370 if (hasAttribute(Attribute::Dereferenceable))
371 return AttrWithBytesToString("dereferenceable");
372
373 if (hasAttribute(Attribute::DereferenceableOrNull))
374 return AttrWithBytesToString("dereferenceable_or_null");
Hal Finkelb0407ba2014-07-18 15:51:28 +0000375
George Burgess IV278199f2016-04-12 01:05:35 +0000376 if (hasAttribute(Attribute::AllocSize)) {
377 unsigned ElemSize;
378 Optional<unsigned> NumElems;
379 std::tie(ElemSize, NumElems) = getAllocSizeArgs();
380
381 std::string Result = "allocsize(";
382 Result += utostr(ElemSize);
383 if (NumElems.hasValue()) {
384 Result += ',';
385 Result += utostr(*NumElems);
386 }
387 Result += ')';
388 return Result;
389 }
390
Bill Wendling9c2eba92013-01-31 20:59:05 +0000391 // Convert target-dependent attributes to strings of the form:
392 //
393 // "kind"
394 // "kind" = "value"
Bill Wendling9c2eba92013-01-31 20:59:05 +0000395 //
Bill Wendling3f12ac22013-02-05 22:37:24 +0000396 if (isStringAttribute()) {
Bill Wendling9c2eba92013-01-31 20:59:05 +0000397 std::string Result;
Yaron Keren075759a2015-03-30 15:42:36 +0000398 Result += (Twine('"') + getKindAsString() + Twine('"')).str();
Bill Wendling9c2eba92013-01-31 20:59:05 +0000399
Honggyu Kim9eb6a102016-09-01 11:44:06 +0000400 std::string AttrVal = pImpl->getValueAsString();
401 if (AttrVal.empty()) return Result;
Bill Wendling7a33f772013-02-01 22:32:30 +0000402
Honggyu Kim9eb6a102016-09-01 11:44:06 +0000403 // Since some attribute strings contain special characters that cannot be
404 // printable, those have to be escaped to make the attribute value printable
405 // as is. e.g. "\01__gnu_mcount_nc"
406 {
407 raw_string_ostream OS(Result);
408 OS << "=\"";
409 PrintEscapedString(AttrVal, OS);
410 OS << "\"";
411 }
Bill Wendlingc79cdff2013-02-01 01:04:27 +0000412 return Result;
Bill Wendling9c2eba92013-01-31 20:59:05 +0000413 }
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000414
415 llvm_unreachable("Unknown attribute");
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000416}
417
Bill Wendlingd509a662013-01-29 00:34:06 +0000418bool Attribute::operator<(Attribute A) const {
419 if (!pImpl && !A.pImpl) return false;
420 if (!pImpl) return true;
421 if (!A.pImpl) return false;
422 return *pImpl < *A.pImpl;
423}
424
Bill Wendlingd509a662013-01-29 00:34:06 +0000425//===----------------------------------------------------------------------===//
426// AttributeImpl Definition
427//===----------------------------------------------------------------------===//
428
Eric Christopher0eaa5412014-07-02 22:05:40 +0000429// Pin the vtables to this file.
Eugene Zelenkodeaf6952017-02-17 00:00:09 +0000430AttributeImpl::~AttributeImpl() = default;
431
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +0000432void EnumAttributeImpl::anchor() {}
Eugene Zelenkodeaf6952017-02-17 00:00:09 +0000433
Hal Finkele15442c2014-07-18 06:51:55 +0000434void IntAttributeImpl::anchor() {}
Eugene Zelenkodeaf6952017-02-17 00:00:09 +0000435
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +0000436void StringAttributeImpl::anchor() {}
Alexey Samsonov49109a22013-11-18 09:31:53 +0000437
Bill Wendlingd509a662013-01-29 00:34:06 +0000438bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
Bill Wendling3f12ac22013-02-05 22:37:24 +0000439 if (isStringAttribute()) return false;
440 return getKindAsEnum() == A;
Bill Wendlingd509a662013-01-29 00:34:06 +0000441}
442
Bill Wendling3f12ac22013-02-05 22:37:24 +0000443bool AttributeImpl::hasAttribute(StringRef Kind) const {
444 if (!isStringAttribute()) return false;
445 return getKindAsString() == Kind;
Bill Wendlingd509a662013-01-29 00:34:06 +0000446}
447
Bill Wendling3f12ac22013-02-05 22:37:24 +0000448Attribute::AttrKind AttributeImpl::getKindAsEnum() const {
Hal Finkele15442c2014-07-18 06:51:55 +0000449 assert(isEnumAttribute() || isIntAttribute());
Benjamin Kramer741146b2013-07-11 12:13:16 +0000450 return static_cast<const EnumAttributeImpl *>(this)->getEnumKind();
Bill Wendlingd509a662013-01-29 00:34:06 +0000451}
452
Bill Wendling3f12ac22013-02-05 22:37:24 +0000453uint64_t AttributeImpl::getValueAsInt() const {
Hal Finkele15442c2014-07-18 06:51:55 +0000454 assert(isIntAttribute());
455 return static_cast<const IntAttributeImpl *>(this)->getValue();
Bill Wendlingd509a662013-01-29 00:34:06 +0000456}
457
Bill Wendling3f12ac22013-02-05 22:37:24 +0000458StringRef AttributeImpl::getKindAsString() const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000459 assert(isStringAttribute());
460 return static_cast<const StringAttributeImpl *>(this)->getStringKind();
Bill Wendlingd509a662013-01-29 00:34:06 +0000461}
462
Bill Wendling3f12ac22013-02-05 22:37:24 +0000463StringRef AttributeImpl::getValueAsString() const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000464 assert(isStringAttribute());
465 return static_cast<const StringAttributeImpl *>(this)->getStringValue();
Bill Wendlingd509a662013-01-29 00:34:06 +0000466}
467
468bool AttributeImpl::operator<(const AttributeImpl &AI) const {
Bill Wendlingc79cdff2013-02-01 01:04:27 +0000469 // This sorts the attributes with Attribute::AttrKinds coming first (sorted
470 // relative to their enum value) and then strings.
Bill Wendling26b95752013-02-15 05:25:26 +0000471 if (isEnumAttribute()) {
472 if (AI.isEnumAttribute()) return getKindAsEnum() < AI.getKindAsEnum();
Hal Finkele15442c2014-07-18 06:51:55 +0000473 if (AI.isIntAttribute()) return true;
Bill Wendling26b95752013-02-15 05:25:26 +0000474 if (AI.isStringAttribute()) return true;
475 }
Bill Wendlingc79cdff2013-02-01 01:04:27 +0000476
Hal Finkele15442c2014-07-18 06:51:55 +0000477 if (isIntAttribute()) {
Bill Wendling26b95752013-02-15 05:25:26 +0000478 if (AI.isEnumAttribute()) return false;
Reid Kleckner7de67612016-04-04 23:06:05 +0000479 if (AI.isIntAttribute()) {
480 if (getKindAsEnum() == AI.getKindAsEnum())
481 return getValueAsInt() < AI.getValueAsInt();
482 return getKindAsEnum() < AI.getKindAsEnum();
483 }
Bill Wendling26b95752013-02-15 05:25:26 +0000484 if (AI.isStringAttribute()) return true;
Bill Wendling3f12ac22013-02-05 22:37:24 +0000485 }
Bill Wendlingd509a662013-01-29 00:34:06 +0000486
Bill Wendling26b95752013-02-15 05:25:26 +0000487 if (AI.isEnumAttribute()) return false;
Hal Finkele15442c2014-07-18 06:51:55 +0000488 if (AI.isIntAttribute()) return false;
Bill Wendling26b95752013-02-15 05:25:26 +0000489 if (getKindAsString() == AI.getKindAsString())
490 return getValueAsString() < AI.getValueAsString();
491 return getKindAsString() < AI.getKindAsString();
Bill Wendlingd509a662013-01-29 00:34:06 +0000492}
493
Bill Wendlingd509a662013-01-29 00:34:06 +0000494//===----------------------------------------------------------------------===//
495// AttributeSetNode Definition
496//===----------------------------------------------------------------------===//
497
Reid Kleckner8ff77852017-04-10 23:46:08 +0000498AttributeSetNode::AttributeSetNode(ArrayRef<Attribute> Attrs)
499 : NumAttrs(Attrs.size()), AvailableAttrs(0) {
500 static_assert(Attribute::EndAttrKinds <= sizeof(AvailableAttrs) * CHAR_BIT,
501 "Too many attributes for AvailableAttrs");
502 // There's memory after the node where we can store the entries in.
503 std::copy(Attrs.begin(), Attrs.end(), getTrailingObjects<Attribute>());
504
505 for (Attribute I : *this) {
506 if (!I.isStringAttribute()) {
507 AvailableAttrs |= ((uint64_t)1) << I.getKindAsEnum();
508 }
509 }
510}
511
Bill Wendlingd509a662013-01-29 00:34:06 +0000512AttributeSetNode *AttributeSetNode::get(LLVMContext &C,
513 ArrayRef<Attribute> Attrs) {
514 if (Attrs.empty())
Craig Topperc6207612014-04-09 06:08:46 +0000515 return nullptr;
Bill Wendlingd509a662013-01-29 00:34:06 +0000516
517 // Otherwise, build a key to look up the existing attributes.
518 LLVMContextImpl *pImpl = C.pImpl;
519 FoldingSetNodeID ID;
520
521 SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end());
Reid Kleckner7de67612016-04-04 23:06:05 +0000522 std::sort(SortedAttrs.begin(), SortedAttrs.end());
Bill Wendlingd509a662013-01-29 00:34:06 +0000523
George Burgess IV500d3032015-12-16 05:21:02 +0000524 for (Attribute Attr : SortedAttrs)
525 Attr.Profile(ID);
Bill Wendlingd509a662013-01-29 00:34:06 +0000526
527 void *InsertPoint;
528 AttributeSetNode *PA =
529 pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint);
530
531 // If we didn't find any existing attributes of the same shape then create a
532 // new one and insert it.
533 if (!PA) {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000534 // Coallocate entries after the AttributeSetNode itself.
James Y Knightaa365b22015-08-05 22:57:34 +0000535 void *Mem = ::operator new(totalSizeToAlloc<Attribute>(SortedAttrs.size()));
Benjamin Kramer741146b2013-07-11 12:13:16 +0000536 PA = new (Mem) AttributeSetNode(SortedAttrs);
Bill Wendlingd509a662013-01-29 00:34:06 +0000537 pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint);
538 }
539
Reid Klecknerb5180542017-03-21 16:57:19 +0000540 // Return the AttributeSetNode that we found or created.
Bill Wendlingd509a662013-01-29 00:34:06 +0000541 return PA;
542}
543
Reid Klecknereb9dd5b2017-04-10 23:31:05 +0000544AttributeSetNode *AttributeSetNode::get(LLVMContext &C, const AttrBuilder &B) {
545 // Add target-independent attributes.
546 SmallVector<Attribute, 8> Attrs;
547 for (Attribute::AttrKind Kind = Attribute::None;
548 Kind != Attribute::EndAttrKinds; Kind = Attribute::AttrKind(Kind + 1)) {
549 if (!B.contains(Kind))
550 continue;
551
552 Attribute Attr;
553 switch (Kind) {
554 case Attribute::Alignment:
555 Attr = Attribute::getWithAlignment(C, B.getAlignment());
556 break;
557 case Attribute::StackAlignment:
558 Attr = Attribute::getWithStackAlignment(C, B.getStackAlignment());
559 break;
560 case Attribute::Dereferenceable:
561 Attr = Attribute::getWithDereferenceableBytes(
562 C, B.getDereferenceableBytes());
563 break;
564 case Attribute::DereferenceableOrNull:
565 Attr = Attribute::getWithDereferenceableOrNullBytes(
566 C, B.getDereferenceableOrNullBytes());
567 break;
568 case Attribute::AllocSize: {
569 auto A = B.getAllocSizeArgs();
570 Attr = Attribute::getWithAllocSizeArgs(C, A.first, A.second);
571 break;
572 }
573 default:
574 Attr = Attribute::get(C, Kind);
575 }
576 Attrs.push_back(Attr);
577 }
578
579 // Add target-dependent (string) attributes.
580 for (const auto &TDA : B.td_attrs())
581 Attrs.emplace_back(Attribute::get(C, TDA.first, TDA.second));
582
583 return get(C, Attrs);
584}
585
Bill Wendlingbce7b972013-02-13 08:42:21 +0000586bool AttributeSetNode::hasAttribute(StringRef Kind) const {
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000587 for (Attribute I : *this)
588 if (I.hasAttribute(Kind))
Bill Wendlingbce7b972013-02-13 08:42:21 +0000589 return true;
590 return false;
591}
592
593Attribute AttributeSetNode::getAttribute(Attribute::AttrKind Kind) const {
Matthias Braun31eeb76f2016-01-29 22:25:13 +0000594 if (hasAttribute(Kind)) {
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000595 for (Attribute I : *this)
596 if (I.hasAttribute(Kind))
597 return I;
Matthias Braun31eeb76f2016-01-29 22:25:13 +0000598 }
Bill Wendlingbce7b972013-02-13 08:42:21 +0000599 return Attribute();
600}
601
602Attribute AttributeSetNode::getAttribute(StringRef Kind) const {
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000603 for (Attribute I : *this)
604 if (I.hasAttribute(Kind))
605 return I;
Bill Wendlingbce7b972013-02-13 08:42:21 +0000606 return Attribute();
607}
608
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000609unsigned AttributeSetNode::getAlignment() const {
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000610 for (Attribute I : *this)
611 if (I.hasAttribute(Attribute::Alignment))
612 return I.getAlignment();
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000613 return 0;
614}
615
616unsigned AttributeSetNode::getStackAlignment() const {
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000617 for (Attribute I : *this)
618 if (I.hasAttribute(Attribute::StackAlignment))
619 return I.getStackAlignment();
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000620 return 0;
621}
622
Hal Finkelb0407ba2014-07-18 15:51:28 +0000623uint64_t AttributeSetNode::getDereferenceableBytes() const {
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000624 for (Attribute I : *this)
625 if (I.hasAttribute(Attribute::Dereferenceable))
626 return I.getDereferenceableBytes();
Hal Finkelb0407ba2014-07-18 15:51:28 +0000627 return 0;
628}
629
Sanjoy Das06cf33f2015-05-06 17:41:54 +0000630uint64_t AttributeSetNode::getDereferenceableOrNullBytes() const {
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000631 for (Attribute I : *this)
632 if (I.hasAttribute(Attribute::DereferenceableOrNull))
633 return I.getDereferenceableOrNullBytes();
Sanjoy Das06cf33f2015-05-06 17:41:54 +0000634 return 0;
635}
636
George Burgess IV278199f2016-04-12 01:05:35 +0000637std::pair<unsigned, Optional<unsigned>>
638AttributeSetNode::getAllocSizeArgs() const {
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000639 for (Attribute I : *this)
640 if (I.hasAttribute(Attribute::AllocSize))
641 return I.getAllocSizeArgs();
George Burgess IV278199f2016-04-12 01:05:35 +0000642 return std::make_pair(0, 0);
643}
644
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +0000645std::string AttributeSetNode::getAsString(bool InAttrGrp) const {
Benjamin Kramer0baf8f42013-04-19 11:43:21 +0000646 std::string Str;
Benjamin Kramer741146b2013-07-11 12:13:16 +0000647 for (iterator I = begin(), E = end(); I != E; ++I) {
648 if (I != begin())
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +0000649 Str += ' ';
650 Str += I->getAsString(InAttrGrp);
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000651 }
652 return Str;
653}
654
Bill Wendlingd509a662013-01-29 00:34:06 +0000655//===----------------------------------------------------------------------===//
Reid Klecknerb5180542017-03-21 16:57:19 +0000656// AttributeListImpl Definition
Bill Wendlingd509a662013-01-29 00:34:06 +0000657//===----------------------------------------------------------------------===//
658
Reid Klecknera82be602017-04-11 00:16:00 +0000659AttributeListImpl::AttributeListImpl(
660 LLVMContext &C, ArrayRef<std::pair<unsigned, AttributeSetNode *>> Slots)
661 : Context(C), NumSlots(Slots.size()), AvailableFunctionAttrs(0) {
662#ifndef NDEBUG
663 if (Slots.size() >= 2) {
664 auto &PrevPair = Slots.front();
665 for (auto &CurPair : Slots.drop_front()) {
666 assert(PrevPair.first <= CurPair.first && "Attribute set not ordered!");
667 }
668 }
669#endif
670
671 // There's memory after the node where we can store the entries in.
672 std::copy(Slots.begin(), Slots.end(), getTrailingObjects<IndexAttrPair>());
673
674 // Initialize AvailableFunctionAttrs summary bitset.
675 if (NumSlots > 0) {
676 static_assert(Attribute::EndAttrKinds <=
677 sizeof(AvailableFunctionAttrs) * CHAR_BIT,
678 "Too many attributes");
679 static_assert(AttributeList::FunctionIndex == ~0u,
680 "FunctionIndex should be biggest possible index");
681 const std::pair<unsigned, AttributeSetNode *> &Last = Slots.back();
682 if (Last.first == AttributeList::FunctionIndex) {
683 const AttributeSetNode *Node = Last.second;
684 for (Attribute I : *Node) {
685 if (!I.isStringAttribute())
686 AvailableFunctionAttrs |= ((uint64_t)1) << I.getKindAsEnum();
687 }
688 }
689 }
690}
691
692void AttributeListImpl::Profile(FoldingSetNodeID &ID) const {
693 Profile(ID, makeArrayRef(getNode(0), getNumSlots()));
694}
695
696void AttributeListImpl::Profile(
697 FoldingSetNodeID &ID,
698 ArrayRef<std::pair<unsigned, AttributeSetNode *>> Nodes) {
699 for (const auto &Node : Nodes) {
700 ID.AddInteger(Node.first);
701 ID.AddPointer(Node.second);
702 }
703}
704
Matthias Braun8c209aa2017-01-28 02:02:38 +0000705#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Reid Klecknerb5180542017-03-21 16:57:19 +0000706LLVM_DUMP_METHOD void AttributeListImpl::dump() const {
707 AttributeList(const_cast<AttributeListImpl *>(this)).dump();
Peter Collingbourneabca2ec2013-08-02 22:34:30 +0000708}
Matthias Braun8c209aa2017-01-28 02:02:38 +0000709#endif
Peter Collingbourneabca2ec2013-08-02 22:34:30 +0000710
Bill Wendlingd509a662013-01-29 00:34:06 +0000711//===----------------------------------------------------------------------===//
Reid Klecknerb5180542017-03-21 16:57:19 +0000712// AttributeList Construction and Mutation Methods
Bill Wendlingd509a662013-01-29 00:34:06 +0000713//===----------------------------------------------------------------------===//
714
Reid Klecknerb5180542017-03-21 16:57:19 +0000715AttributeList AttributeList::getImpl(
716 LLVMContext &C, ArrayRef<std::pair<unsigned, AttributeSetNode *>> Attrs) {
Reid Klecknereb9dd5b2017-04-10 23:31:05 +0000717 assert(!Attrs.empty() && "creating pointless AttributeList");
718#ifndef NDEBUG
719 unsigned LastIndex = 0;
720 bool IsFirst = true;
721 for (const auto &AttrPair : Attrs) {
722 assert((IsFirst || LastIndex < AttrPair.first) &&
723 "unsorted or duplicate AttributeList indices");
724 assert(AttrPair.second && "pointless AttributeList slot");
725 LastIndex = AttrPair.first;
726 IsFirst = false;
727 }
728#endif
729
Bill Wendlingd509a662013-01-29 00:34:06 +0000730 LLVMContextImpl *pImpl = C.pImpl;
731 FoldingSetNodeID ID;
Reid Klecknerb5180542017-03-21 16:57:19 +0000732 AttributeListImpl::Profile(ID, Attrs);
Bill Wendlingd509a662013-01-29 00:34:06 +0000733
734 void *InsertPoint;
Reid Klecknerb5180542017-03-21 16:57:19 +0000735 AttributeListImpl *PA =
736 pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
Bill Wendlingd509a662013-01-29 00:34:06 +0000737
738 // If we didn't find any existing attributes of the same shape then
739 // create a new one and insert it.
740 if (!PA) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000741 // Coallocate entries after the AttributeListImpl itself.
James Y Knightaa365b22015-08-05 22:57:34 +0000742 void *Mem = ::operator new(
Reid Klecknerb5180542017-03-21 16:57:19 +0000743 AttributeListImpl::totalSizeToAlloc<IndexAttrPair>(Attrs.size()));
744 PA = new (Mem) AttributeListImpl(C, Attrs);
Bill Wendlingd509a662013-01-29 00:34:06 +0000745 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
746 }
747
748 // Return the AttributesList that we found or created.
Reid Klecknerb5180542017-03-21 16:57:19 +0000749 return AttributeList(PA);
Bill Wendlingd509a662013-01-29 00:34:06 +0000750}
751
Reid Klecknerb5180542017-03-21 16:57:19 +0000752AttributeList
753AttributeList::get(LLVMContext &C,
754 ArrayRef<std::pair<unsigned, Attribute>> Attrs) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000755 // If there are no attributes then return a null AttributesList pointer.
756 if (Attrs.empty())
Reid Klecknerb5180542017-03-21 16:57:19 +0000757 return AttributeList();
Bill Wendlingd509a662013-01-29 00:34:06 +0000758
Craig Toppere30b8ca2016-01-03 19:43:40 +0000759 assert(std::is_sorted(Attrs.begin(), Attrs.end(),
760 [](const std::pair<unsigned, Attribute> &LHS,
761 const std::pair<unsigned, Attribute> &RHS) {
762 return LHS.first < RHS.first;
763 }) && "Misordered Attributes list!");
David Majnemer0a16c222016-08-11 21:15:00 +0000764 assert(none_of(Attrs,
765 [](const std::pair<unsigned, Attribute> &Pair) {
766 return Pair.second.hasAttribute(Attribute::None);
767 }) &&
768 "Pointless attribute!");
Bill Wendlingd509a662013-01-29 00:34:06 +0000769
770 // Create a vector if (unsigned, AttributeSetNode*) pairs from the attributes
771 // list.
772 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrPairVec;
Eugene Zelenkodeaf6952017-02-17 00:00:09 +0000773 for (ArrayRef<std::pair<unsigned, Attribute>>::iterator I = Attrs.begin(),
Bill Wendlingd509a662013-01-29 00:34:06 +0000774 E = Attrs.end(); I != E; ) {
775 unsigned Index = I->first;
776 SmallVector<Attribute, 4> AttrVec;
NAKAMURA Takumif05d2f22013-01-29 15:18:16 +0000777 while (I != E && I->first == Index) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000778 AttrVec.push_back(I->second);
779 ++I;
780 }
781
David Majnemer0a16c222016-08-11 21:15:00 +0000782 AttrPairVec.emplace_back(Index, AttributeSetNode::get(C, AttrVec));
Bill Wendlingd509a662013-01-29 00:34:06 +0000783 }
784
785 return getImpl(C, AttrPairVec);
786}
787
Reid Klecknerb5180542017-03-21 16:57:19 +0000788AttributeList
789AttributeList::get(LLVMContext &C,
790 ArrayRef<std::pair<unsigned, AttributeSetNode *>> Attrs) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000791 // If there are no attributes then return a null AttributesList pointer.
792 if (Attrs.empty())
Reid Klecknerb5180542017-03-21 16:57:19 +0000793 return AttributeList();
Bill Wendlingd509a662013-01-29 00:34:06 +0000794
795 return getImpl(C, Attrs);
796}
797
Reid Klecknereb9dd5b2017-04-10 23:31:05 +0000798AttributeList AttributeList::get(LLVMContext &C, ArrayRef<AttributeSetNode*> Attrs) {
799 assert(Attrs.size() >= 2 &&
800 "should always have function and return attr slots");
801 SmallVector<std::pair<unsigned, AttributeSetNode *>, 8> AttrPairs;
802 size_t Index = 0;
803 for (AttributeSetNode *AS : Attrs) {
804 if (AS) {
805 // If this is the last AttributeSetNode, it's for the function.
806 if (Index == Attrs.size() - 1)
807 Index = AttributeList::FunctionIndex;
808 AttrPairs.emplace_back(Index, AS);
809 }
810 ++Index;
811 }
812 if (AttrPairs.empty())
813 return AttributeList();
814 return getImpl(C, AttrPairs);
815}
816
Reid Klecknerb5180542017-03-21 16:57:19 +0000817AttributeList AttributeList::get(LLVMContext &C, unsigned Index,
818 const AttrBuilder &B) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000819 if (!B.hasAttributes())
Reid Klecknerb5180542017-03-21 16:57:19 +0000820 return AttributeList();
Reid Klecknereb9dd5b2017-04-10 23:31:05 +0000821 AttributeSetNode *ASN = AttributeSetNode::get(C, B);
822 std::pair<unsigned, AttributeSetNode *> Arr[1] = {{Index, ASN}};
823 return getImpl(C, Arr);
Bill Wendlingd509a662013-01-29 00:34:06 +0000824}
825
Reid Klecknerb5180542017-03-21 16:57:19 +0000826AttributeList AttributeList::get(LLVMContext &C, unsigned Index,
827 ArrayRef<Attribute::AttrKind> Kinds) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000828 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
Amaury Sechet392638d2016-06-14 20:27:35 +0000829 for (Attribute::AttrKind K : Kinds)
David Majnemer0a16c222016-08-11 21:15:00 +0000830 Attrs.emplace_back(Index, Attribute::get(C, K));
Bill Wendlingd509a662013-01-29 00:34:06 +0000831 return get(C, Attrs);
832}
833
Reid Klecknerb5180542017-03-21 16:57:19 +0000834AttributeList AttributeList::get(LLVMContext &C, unsigned Index,
835 ArrayRef<StringRef> Kinds) {
Amaury Sechet6100adf2016-06-15 17:50:39 +0000836 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
837 for (StringRef K : Kinds)
David Majnemer0a16c222016-08-11 21:15:00 +0000838 Attrs.emplace_back(Index, Attribute::get(C, K));
Amaury Sechet6100adf2016-06-15 17:50:39 +0000839 return get(C, Attrs);
840}
841
Reid Klecknerb5180542017-03-21 16:57:19 +0000842AttributeList AttributeList::get(LLVMContext &C,
843 ArrayRef<AttributeList> Attrs) {
844 if (Attrs.empty())
845 return AttributeList();
Peter Collingbournebd6c7452013-08-02 22:29:40 +0000846 if (Attrs.size() == 1) return Attrs[0];
Bill Wendlingd509a662013-01-29 00:34:06 +0000847
848 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrNodeVec;
Reid Klecknerb5180542017-03-21 16:57:19 +0000849 AttributeListImpl *A0 = Attrs[0].pImpl;
Peter Collingbournebd6c7452013-08-02 22:29:40 +0000850 if (A0)
Amaury Sechet24c84fd2016-06-14 22:04:16 +0000851 AttrNodeVec.append(A0->getNode(0), A0->getNode(A0->getNumSlots()));
Peter Collingbournebd6c7452013-08-02 22:29:40 +0000852 // Copy all attributes from Attrs into AttrNodeVec while keeping AttrNodeVec
853 // ordered by index. Because we know that each list in Attrs is ordered by
854 // index we only need to merge each successive list in rather than doing a
855 // full sort.
856 for (unsigned I = 1, E = Attrs.size(); I != E; ++I) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000857 AttributeListImpl *AS = Attrs[I].pImpl;
Benjamin Kramer741146b2013-07-11 12:13:16 +0000858 if (!AS) continue;
Peter Collingbournebd6c7452013-08-02 22:29:40 +0000859 SmallVector<std::pair<unsigned, AttributeSetNode *>, 8>::iterator
860 ANVI = AttrNodeVec.begin(), ANVE;
James Y Knightaa365b22015-08-05 22:57:34 +0000861 for (const IndexAttrPair *AI = AS->getNode(0),
Amaury Sechet24c84fd2016-06-14 22:04:16 +0000862 *AE = AS->getNode(AS->getNumSlots());
Peter Collingbournebd6c7452013-08-02 22:29:40 +0000863 AI != AE; ++AI) {
864 ANVE = AttrNodeVec.end();
865 while (ANVI != ANVE && ANVI->first <= AI->first)
866 ++ANVI;
867 ANVI = AttrNodeVec.insert(ANVI, *AI) + 1;
868 }
Bill Wendlingd509a662013-01-29 00:34:06 +0000869 }
870
871 return getImpl(C, AttrNodeVec);
872}
873
Reid Klecknerb5180542017-03-21 16:57:19 +0000874AttributeList AttributeList::addAttribute(LLVMContext &C, unsigned Index,
875 Attribute::AttrKind Kind) const {
Amaury Sechet392638d2016-06-14 20:27:35 +0000876 if (hasAttribute(Index, Kind)) return *this;
Reid Klecknerb5180542017-03-21 16:57:19 +0000877 return addAttributes(C, Index, AttributeList::get(C, Index, Kind));
Reed Kotler795c7b42013-03-13 20:20:08 +0000878}
879
Reid Klecknerb5180542017-03-21 16:57:19 +0000880AttributeList AttributeList::addAttribute(LLVMContext &C, unsigned Index,
881 StringRef Kind,
882 StringRef Value) const {
Eugene Zelenkodeaf6952017-02-17 00:00:09 +0000883 AttrBuilder B;
Bill Wendling3b2f6102013-07-25 18:34:24 +0000884 B.addAttribute(Kind, Value);
Reid Klecknerb5180542017-03-21 16:57:19 +0000885 return addAttributes(C, Index, AttributeList::get(C, Index, B));
Bill Wendling3b2f6102013-07-25 18:34:24 +0000886}
887
Reid Klecknerb5180542017-03-21 16:57:19 +0000888AttributeList AttributeList::addAttribute(LLVMContext &C,
889 ArrayRef<unsigned> Indices,
890 Attribute A) const {
Reid Klecknereb9dd5b2017-04-10 23:31:05 +0000891 assert(std::is_sorted(Indices.begin(), Indices.end()));
Reid Kleckner324c99d2017-04-10 20:18:10 +0000892
Reid Klecknereb9dd5b2017-04-10 23:31:05 +0000893 unsigned I = 0, E = pImpl ? pImpl->getNumSlots() : 0;
894 SmallVector<IndexAttrPair, 4> AttrVec;
895 for (unsigned Index : Indices) {
896 // Add all attribute slots before the current index.
897 for (; I < E && getSlotIndex(I) < Index; ++I)
898 AttrVec.emplace_back(getSlotIndex(I), pImpl->getSlotNode(I));
899
900 // Add the attribute at this index. If we already have attributes at this
901 // index, merge them into a new set.
902 AttrBuilder B;
903 if (I < E && getSlotIndex(I) == Index) {
904 B.merge(AttrBuilder(pImpl->getSlotNode(I)));
Akira Hatanaka237916b2015-12-02 06:58:49 +0000905 ++I;
Akira Hatanaka237916b2015-12-02 06:58:49 +0000906 }
Reid Klecknereb9dd5b2017-04-10 23:31:05 +0000907 B.addAttribute(A);
908 AttrVec.emplace_back(Index, AttributeSetNode::get(C, B));
Akira Hatanaka237916b2015-12-02 06:58:49 +0000909 }
910
Reid Klecknereb9dd5b2017-04-10 23:31:05 +0000911 // Add remaining attributes.
912 for (; I < E; ++I)
913 AttrVec.emplace_back(getSlotIndex(I), pImpl->getSlotNode(I));
Akira Hatanaka237916b2015-12-02 06:58:49 +0000914
Reid Klecknereb9dd5b2017-04-10 23:31:05 +0000915 return get(C, AttrVec);
Akira Hatanaka237916b2015-12-02 06:58:49 +0000916}
917
Reid Klecknerb5180542017-03-21 16:57:19 +0000918AttributeList AttributeList::addAttributes(LLVMContext &C, unsigned Index,
919 AttributeList Attrs) const {
Bill Wendlingd509a662013-01-29 00:34:06 +0000920 if (!pImpl) return Attrs;
921 if (!Attrs.pImpl) return *this;
922
Reid Klecknereb9dd5b2017-04-10 23:31:05 +0000923 return addAttributes(C, Index, Attrs.getAttributes(Index));
924}
925
926AttributeList AttributeList::addAttributes(LLVMContext &C, unsigned Index,
927 AttributeSetNode *AS) const {
928 if (!AS)
929 return *this;
930
Bill Wendlingd509a662013-01-29 00:34:06 +0000931#ifndef NDEBUG
932 // FIXME it is not obvious how this should work for alignment. For now, say
933 // we can't change a known alignment.
Bill Wendling211316c2013-04-18 20:17:28 +0000934 unsigned OldAlign = getParamAlignment(Index);
Reid Klecknereb9dd5b2017-04-10 23:31:05 +0000935 unsigned NewAlign = AS->getAlignment();
Bill Wendlingd509a662013-01-29 00:34:06 +0000936 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
937 "Attempt to change alignment!");
938#endif
939
Reid Klecknereb9dd5b2017-04-10 23:31:05 +0000940 SmallVector<std::pair<unsigned, AttributeSetNode *>, 4> AttrSet;
Amaury Sechet24c84fd2016-06-14 22:04:16 +0000941 uint64_t NumAttrs = pImpl->getNumSlots();
Reid Klecknereb9dd5b2017-04-10 23:31:05 +0000942 unsigned I;
943
944 // Add all the attribute slots before the one we need to merge.
945 for (I = 0; I < NumAttrs; ++I) {
946 if (getSlotIndex(I) >= Index)
Bill Wendlingd509a662013-01-29 00:34:06 +0000947 break;
Reid Klecknereb9dd5b2017-04-10 23:31:05 +0000948 AttrSet.emplace_back(getSlotIndex(I), pImpl->getSlotNode(I));
Bill Wendlingd509a662013-01-29 00:34:06 +0000949 }
950
Reid Klecknereb9dd5b2017-04-10 23:31:05 +0000951 if (I < NumAttrs && getSlotIndex(I) == Index) {
952 // We need to merge two AttributeSetNodes.
953 AttributeSetNode *Merged = AttributeSetNode::get(
954 C, AttrBuilder(pImpl->getSlotNode(I)).merge(AttrBuilder(AS)));
955 AttrSet.emplace_back(Index, Merged);
956 ++I;
957 } else {
958 // Otherwise, there were no attributes at this position in the original
959 // list. Add the set as is.
960 AttrSet.emplace_back(Index, AS);
961 }
Bill Wendlingd509a662013-01-29 00:34:06 +0000962
Reid Klecknereb9dd5b2017-04-10 23:31:05 +0000963 // Add the remaining entries.
964 for (; I < NumAttrs; ++I)
965 AttrSet.emplace_back(getSlotIndex(I), pImpl->getSlotNode(I));
Bill Wendlingd509a662013-01-29 00:34:06 +0000966
967 return get(C, AttrSet);
968}
969
Reid Klecknereb9dd5b2017-04-10 23:31:05 +0000970AttributeList AttributeList::addAttributes(LLVMContext &C, unsigned Index,
971 const AttrBuilder &B) const {
972 return get(C, Index, AttributeSetNode::get(C, B));
973}
974
Reid Klecknerb5180542017-03-21 16:57:19 +0000975AttributeList AttributeList::removeAttribute(LLVMContext &C, unsigned Index,
976 Attribute::AttrKind Kind) const {
Amaury Sechet392638d2016-06-14 20:27:35 +0000977 if (!hasAttribute(Index, Kind)) return *this;
Reid Klecknerb5180542017-03-21 16:57:19 +0000978 return removeAttributes(C, Index, AttributeList::get(C, Index, Kind));
Bill Wendlingd509a662013-01-29 00:34:06 +0000979}
980
Reid Klecknerb5180542017-03-21 16:57:19 +0000981AttributeList AttributeList::removeAttribute(LLVMContext &C, unsigned Index,
982 StringRef Kind) const {
Amaury Sechet6100adf2016-06-15 17:50:39 +0000983 if (!hasAttribute(Index, Kind)) return *this;
Reid Klecknerb5180542017-03-21 16:57:19 +0000984 return removeAttributes(C, Index, AttributeList::get(C, Index, Kind));
Amaury Sechet6100adf2016-06-15 17:50:39 +0000985}
986
Reid Klecknerb5180542017-03-21 16:57:19 +0000987AttributeList AttributeList::removeAttributes(LLVMContext &C, unsigned Index,
988 AttributeList Attrs) const {
989 if (!pImpl)
990 return AttributeList();
Bill Wendlingd509a662013-01-29 00:34:06 +0000991 if (!Attrs.pImpl) return *this;
992
Pete Cooper67cf9a72015-11-19 05:56:52 +0000993 // FIXME it is not obvious how this should work for alignment.
994 // For now, say we can't pass in alignment, which no current use does.
995 assert(!Attrs.hasAttribute(Index, Attribute::Alignment) &&
996 "Attempt to change alignment!");
997
Bill Wendlingd509a662013-01-29 00:34:06 +0000998 // Add the attribute slots before the one we're trying to add.
Reid Klecknerb5180542017-03-21 16:57:19 +0000999 SmallVector<AttributeList, 4> AttrSet;
Amaury Sechet24c84fd2016-06-14 22:04:16 +00001000 uint64_t NumAttrs = pImpl->getNumSlots();
Reid Klecknerb5180542017-03-21 16:57:19 +00001001 AttributeList AS;
Bill Wendlingd509a662013-01-29 00:34:06 +00001002 uint64_t LastIndex = 0;
1003 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
Bill Wendling211316c2013-04-18 20:17:28 +00001004 if (getSlotIndex(I) >= Index) {
1005 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
Bill Wendlingd509a662013-01-29 00:34:06 +00001006 break;
1007 }
1008 LastIndex = I + 1;
1009 AttrSet.push_back(getSlotAttributes(I));
1010 }
1011
Bill Wendlingd2196752013-01-30 23:07:40 +00001012 // Now remove the attribute from the correct slot. There may already be an
Reid Klecknerb5180542017-03-21 16:57:19 +00001013 // AttributeList there.
Bill Wendling211316c2013-04-18 20:17:28 +00001014 AttrBuilder B(AS, Index);
Bill Wendlingd509a662013-01-29 00:34:06 +00001015
Amaury Sechet24c84fd2016-06-14 22:04:16 +00001016 for (unsigned I = 0, E = Attrs.pImpl->getNumSlots(); I != E; ++I)
Bill Wendling211316c2013-04-18 20:17:28 +00001017 if (Attrs.getSlotIndex(I) == Index) {
1018 B.removeAttributes(Attrs.pImpl->getSlotAttributes(I), Index);
Bill Wendlingd509a662013-01-29 00:34:06 +00001019 break;
1020 }
1021
Reid Klecknerb5180542017-03-21 16:57:19 +00001022 AttrSet.push_back(AttributeList::get(C, Index, B));
Bill Wendlingd509a662013-01-29 00:34:06 +00001023
1024 // Add the remaining attribute slots.
1025 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
1026 AttrSet.push_back(getSlotAttributes(I));
1027
1028 return get(C, AttrSet);
1029}
1030
Reid Klecknerb5180542017-03-21 16:57:19 +00001031AttributeList AttributeList::removeAttributes(LLVMContext &C, unsigned Index,
1032 const AttrBuilder &Attrs) const {
1033 if (!pImpl)
1034 return AttributeList();
Pete Cooperd2a44612015-05-06 23:19:43 +00001035
1036 // FIXME it is not obvious how this should work for alignment.
1037 // For now, say we can't pass in alignment, which no current use does.
1038 assert(!Attrs.hasAlignmentAttr() && "Attempt to change alignment!");
1039
1040 // Add the attribute slots before the one we're trying to add.
Reid Klecknerb5180542017-03-21 16:57:19 +00001041 SmallVector<AttributeList, 4> AttrSet;
Amaury Sechet24c84fd2016-06-14 22:04:16 +00001042 uint64_t NumAttrs = pImpl->getNumSlots();
Reid Klecknerb5180542017-03-21 16:57:19 +00001043 AttributeList AS;
Pete Cooperd2a44612015-05-06 23:19:43 +00001044 uint64_t LastIndex = 0;
1045 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
1046 if (getSlotIndex(I) >= Index) {
1047 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
1048 break;
1049 }
1050 LastIndex = I + 1;
1051 AttrSet.push_back(getSlotAttributes(I));
1052 }
1053
1054 // Now remove the attribute from the correct slot. There may already be an
Reid Klecknerb5180542017-03-21 16:57:19 +00001055 // AttributeList there.
Pete Cooperd2a44612015-05-06 23:19:43 +00001056 AttrBuilder B(AS, Index);
1057 B.remove(Attrs);
1058
Reid Klecknerb5180542017-03-21 16:57:19 +00001059 AttrSet.push_back(AttributeList::get(C, Index, B));
Pete Cooperd2a44612015-05-06 23:19:43 +00001060
1061 // Add the remaining attribute slots.
1062 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
1063 AttrSet.push_back(getSlotAttributes(I));
1064
1065 return get(C, AttrSet);
1066}
1067
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00001068AttributeList AttributeList::removeAttributes(LLVMContext &C,
1069 unsigned WithoutIndex) const {
1070 if (!pImpl)
1071 return AttributeList();
1072
1073 SmallVector<std::pair<unsigned, AttributeSetNode *>, 4> AttrSet;
1074 for (unsigned I = 0, E = pImpl->getNumSlots(); I != E; ++I) {
1075 unsigned Index = getSlotIndex(I);
1076 if (Index != WithoutIndex)
1077 AttrSet.push_back({Index, pImpl->getSlotNode(I)});
1078 }
1079 return get(C, AttrSet);
1080}
1081
Reid Klecknerb5180542017-03-21 16:57:19 +00001082AttributeList AttributeList::addDereferenceableAttr(LLVMContext &C,
1083 unsigned Index,
1084 uint64_t Bytes) const {
Eugene Zelenkodeaf6952017-02-17 00:00:09 +00001085 AttrBuilder B;
Ramkumar Ramachandra8fcb4982015-02-14 19:37:54 +00001086 B.addDereferenceableAttr(Bytes);
Reid Klecknerb5180542017-03-21 16:57:19 +00001087 return addAttributes(C, Index, AttributeList::get(C, Index, B));
Ramkumar Ramachandra8fcb4982015-02-14 19:37:54 +00001088}
1089
Reid Klecknerb5180542017-03-21 16:57:19 +00001090AttributeList
1091AttributeList::addDereferenceableOrNullAttr(LLVMContext &C, unsigned Index,
1092 uint64_t Bytes) const {
Eugene Zelenkodeaf6952017-02-17 00:00:09 +00001093 AttrBuilder B;
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001094 B.addDereferenceableOrNullAttr(Bytes);
Reid Klecknerb5180542017-03-21 16:57:19 +00001095 return addAttributes(C, Index, AttributeList::get(C, Index, B));
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001096}
1097
Reid Klecknerb5180542017-03-21 16:57:19 +00001098AttributeList
1099AttributeList::addAllocSizeAttr(LLVMContext &C, unsigned Index,
1100 unsigned ElemSizeArg,
1101 const Optional<unsigned> &NumElemsArg) {
Eugene Zelenkodeaf6952017-02-17 00:00:09 +00001102 AttrBuilder B;
George Burgess IV278199f2016-04-12 01:05:35 +00001103 B.addAllocSizeAttr(ElemSizeArg, NumElemsArg);
Reid Klecknerb5180542017-03-21 16:57:19 +00001104 return addAttributes(C, Index, AttributeList::get(C, Index, B));
George Burgess IV278199f2016-04-12 01:05:35 +00001105}
1106
Bill Wendlingd509a662013-01-29 00:34:06 +00001107//===----------------------------------------------------------------------===//
Reid Klecknerb5180542017-03-21 16:57:19 +00001108// AttributeList Accessor Methods
Bill Wendlingd509a662013-01-29 00:34:06 +00001109//===----------------------------------------------------------------------===//
1110
Reid Klecknerb5180542017-03-21 16:57:19 +00001111LLVMContext &AttributeList::getContext() const { return pImpl->getContext(); }
1112
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00001113AttributeSetNode *AttributeList::getParamAttributes(unsigned Index) const {
1114 return getAttributes(Index);
Bill Wendling5d020a32013-02-10 05:00:40 +00001115}
1116
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00001117AttributeSetNode *AttributeList::getRetAttributes() const {
1118 return getAttributes(ReturnIndex);
Bill Wendlingd509a662013-01-29 00:34:06 +00001119}
1120
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00001121AttributeSetNode *AttributeList::getFnAttributes() const {
1122 return getAttributes(FunctionIndex);
Bill Wendlingd509a662013-01-29 00:34:06 +00001123}
1124
Reid Klecknerb5180542017-03-21 16:57:19 +00001125bool AttributeList::hasAttribute(unsigned Index,
1126 Attribute::AttrKind Kind) const {
Bill Wendlingf2955aa2013-01-29 03:20:31 +00001127 AttributeSetNode *ASN = getAttributes(Index);
George Burgess IV500d3032015-12-16 05:21:02 +00001128 return ASN && ASN->hasAttribute(Kind);
Bill Wendlingd509a662013-01-29 00:34:06 +00001129}
1130
Reid Klecknerb5180542017-03-21 16:57:19 +00001131bool AttributeList::hasAttribute(unsigned Index, StringRef Kind) const {
Bill Wendlingbce7b972013-02-13 08:42:21 +00001132 AttributeSetNode *ASN = getAttributes(Index);
George Burgess IV500d3032015-12-16 05:21:02 +00001133 return ASN && ASN->hasAttribute(Kind);
Bill Wendlingbce7b972013-02-13 08:42:21 +00001134}
1135
Reid Klecknerb5180542017-03-21 16:57:19 +00001136bool AttributeList::hasAttributes(unsigned Index) const {
Bill Wendlingf2955aa2013-01-29 03:20:31 +00001137 AttributeSetNode *ASN = getAttributes(Index);
George Burgess IV500d3032015-12-16 05:21:02 +00001138 return ASN && ASN->hasAttributes();
Bill Wendlingd509a662013-01-29 00:34:06 +00001139}
1140
Reid Klecknerb5180542017-03-21 16:57:19 +00001141bool AttributeList::hasFnAttribute(Attribute::AttrKind Kind) const {
Matthias Braun33282812016-01-29 22:25:19 +00001142 return pImpl && pImpl->hasFnAttribute(Kind);
1143}
1144
Reid Klecknerb5180542017-03-21 16:57:19 +00001145bool AttributeList::hasFnAttribute(StringRef Kind) const {
1146 return hasAttribute(AttributeList::FunctionIndex, Kind);
Amaury Sechet5f04d812016-09-09 04:50:38 +00001147}
1148
Reid Klecknerb5180542017-03-21 16:57:19 +00001149bool AttributeList::hasAttrSomewhere(Attribute::AttrKind Attr,
1150 unsigned *Index) const {
Craig Topperc6207612014-04-09 06:08:46 +00001151 if (!pImpl) return false;
Bill Wendlingd509a662013-01-29 00:34:06 +00001152
Amaury Sechet24c84fd2016-06-14 22:04:16 +00001153 for (unsigned I = 0, E = pImpl->getNumSlots(); I != E; ++I)
Reid Klecknerb5180542017-03-21 16:57:19 +00001154 for (AttributeListImpl::iterator II = pImpl->begin(I), IE = pImpl->end(I);
1155 II != IE; ++II)
Hal Finkele87ad542016-07-10 23:01:32 +00001156 if (II->hasAttribute(Attr)) {
1157 if (Index) *Index = pImpl->getSlotIndex(I);
Bill Wendlingd509a662013-01-29 00:34:06 +00001158 return true;
Hal Finkele87ad542016-07-10 23:01:32 +00001159 }
Bill Wendlingd509a662013-01-29 00:34:06 +00001160
1161 return false;
1162}
1163
Reid Klecknerb5180542017-03-21 16:57:19 +00001164Attribute AttributeList::getAttribute(unsigned Index,
1165 Attribute::AttrKind Kind) const {
Bill Wendlingbce7b972013-02-13 08:42:21 +00001166 AttributeSetNode *ASN = getAttributes(Index);
1167 return ASN ? ASN->getAttribute(Kind) : Attribute();
1168}
1169
Reid Klecknerb5180542017-03-21 16:57:19 +00001170Attribute AttributeList::getAttribute(unsigned Index, StringRef Kind) const {
Bill Wendlingbce7b972013-02-13 08:42:21 +00001171 AttributeSetNode *ASN = getAttributes(Index);
1172 return ASN ? ASN->getAttribute(Kind) : Attribute();
1173}
1174
Reid Klecknerb5180542017-03-21 16:57:19 +00001175unsigned AttributeList::getParamAlignment(unsigned Index) const {
Bill Wendlingf2955aa2013-01-29 03:20:31 +00001176 AttributeSetNode *ASN = getAttributes(Index);
1177 return ASN ? ASN->getAlignment() : 0;
Bill Wendlingd509a662013-01-29 00:34:06 +00001178}
1179
Reid Klecknerb5180542017-03-21 16:57:19 +00001180unsigned AttributeList::getStackAlignment(unsigned Index) const {
Bill Wendlingf2955aa2013-01-29 03:20:31 +00001181 AttributeSetNode *ASN = getAttributes(Index);
1182 return ASN ? ASN->getStackAlignment() : 0;
Bill Wendlingd509a662013-01-29 00:34:06 +00001183}
1184
Reid Klecknerb5180542017-03-21 16:57:19 +00001185uint64_t AttributeList::getDereferenceableBytes(unsigned Index) const {
Hal Finkelb0407ba2014-07-18 15:51:28 +00001186 AttributeSetNode *ASN = getAttributes(Index);
1187 return ASN ? ASN->getDereferenceableBytes() : 0;
1188}
1189
Reid Klecknerb5180542017-03-21 16:57:19 +00001190uint64_t AttributeList::getDereferenceableOrNullBytes(unsigned Index) const {
Sanjoy Das06cf33f2015-05-06 17:41:54 +00001191 AttributeSetNode *ASN = getAttributes(Index);
1192 return ASN ? ASN->getDereferenceableOrNullBytes() : 0;
1193}
1194
George Burgess IV278199f2016-04-12 01:05:35 +00001195std::pair<unsigned, Optional<unsigned>>
Reid Klecknerb5180542017-03-21 16:57:19 +00001196AttributeList::getAllocSizeArgs(unsigned Index) const {
George Burgess IV278199f2016-04-12 01:05:35 +00001197 AttributeSetNode *ASN = getAttributes(Index);
Matt Arsenault4ced16d2016-07-18 22:12:46 +00001198 return ASN ? ASN->getAllocSizeArgs() : std::make_pair(0u, Optional<unsigned>(0u));
George Burgess IV278199f2016-04-12 01:05:35 +00001199}
1200
Reid Klecknerb5180542017-03-21 16:57:19 +00001201std::string AttributeList::getAsString(unsigned Index, bool InAttrGrp) const {
Bill Wendlingf2955aa2013-01-29 03:20:31 +00001202 AttributeSetNode *ASN = getAttributes(Index);
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +00001203 return ASN ? ASN->getAsString(InAttrGrp) : std::string("");
Bill Wendlingd509a662013-01-29 00:34:06 +00001204}
1205
Reid Klecknerb5180542017-03-21 16:57:19 +00001206AttributeSetNode *AttributeList::getAttributes(unsigned Index) const {
Craig Topperc6207612014-04-09 06:08:46 +00001207 if (!pImpl) return nullptr;
Bill Wendlingd509a662013-01-29 00:34:06 +00001208
Bill Wendlingf2955aa2013-01-29 03:20:31 +00001209 // Loop through to find the attribute node we want.
Amaury Sechet24c84fd2016-06-14 22:04:16 +00001210 for (unsigned I = 0, E = pImpl->getNumSlots(); I != E; ++I)
Bill Wendling211316c2013-04-18 20:17:28 +00001211 if (pImpl->getSlotIndex(I) == Index)
Bill Wendlingf2955aa2013-01-29 03:20:31 +00001212 return pImpl->getSlotNode(I);
Bill Wendlingd509a662013-01-29 00:34:06 +00001213
Craig Topperc6207612014-04-09 06:08:46 +00001214 return nullptr;
Bill Wendlingd509a662013-01-29 00:34:06 +00001215}
1216
Reid Klecknerb5180542017-03-21 16:57:19 +00001217AttributeList::iterator AttributeList::begin(unsigned Slot) const {
Bill Wendlinga9174862013-01-31 23:53:05 +00001218 if (!pImpl)
1219 return ArrayRef<Attribute>().begin();
Bill Wendling211316c2013-04-18 20:17:28 +00001220 return pImpl->begin(Slot);
Bill Wendlinga9174862013-01-31 23:53:05 +00001221}
1222
Reid Klecknerb5180542017-03-21 16:57:19 +00001223AttributeList::iterator AttributeList::end(unsigned Slot) const {
Bill Wendlinga9174862013-01-31 23:53:05 +00001224 if (!pImpl)
1225 return ArrayRef<Attribute>().end();
Bill Wendling211316c2013-04-18 20:17:28 +00001226 return pImpl->end(Slot);
Bill Wendlinga9174862013-01-31 23:53:05 +00001227}
1228
Bill Wendlingd509a662013-01-29 00:34:06 +00001229//===----------------------------------------------------------------------===//
Reid Klecknerb5180542017-03-21 16:57:19 +00001230// AttributeList Introspection Methods
Bill Wendlingd509a662013-01-29 00:34:06 +00001231//===----------------------------------------------------------------------===//
1232
Reid Klecknerb5180542017-03-21 16:57:19 +00001233unsigned AttributeList::getNumSlots() const {
Amaury Sechet24c84fd2016-06-14 22:04:16 +00001234 return pImpl ? pImpl->getNumSlots() : 0;
Bill Wendlingd509a662013-01-29 00:34:06 +00001235}
1236
Reid Klecknerb5180542017-03-21 16:57:19 +00001237unsigned AttributeList::getSlotIndex(unsigned Slot) const {
Amaury Sechet24c84fd2016-06-14 22:04:16 +00001238 assert(pImpl && Slot < pImpl->getNumSlots() &&
Bill Wendlingd509a662013-01-29 00:34:06 +00001239 "Slot # out of range!");
1240 return pImpl->getSlotIndex(Slot);
1241}
1242
Reid Klecknerb5180542017-03-21 16:57:19 +00001243AttributeList AttributeList::getSlotAttributes(unsigned Slot) const {
Amaury Sechet24c84fd2016-06-14 22:04:16 +00001244 assert(pImpl && Slot < pImpl->getNumSlots() &&
Bill Wendlingd509a662013-01-29 00:34:06 +00001245 "Slot # out of range!");
1246 return pImpl->getSlotAttributes(Slot);
1247}
1248
Matthias Braun8c209aa2017-01-28 02:02:38 +00001249#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Reid Klecknerb5180542017-03-21 16:57:19 +00001250LLVM_DUMP_METHOD void AttributeList::dump() const {
Bill Wendlingd509a662013-01-29 00:34:06 +00001251 dbgs() << "PAL[\n";
1252
1253 for (unsigned i = 0, e = getNumSlots(); i < e; ++i) {
1254 uint64_t Index = getSlotIndex(i);
1255 dbgs() << " { ";
1256 if (Index == ~0U)
1257 dbgs() << "~0U";
1258 else
1259 dbgs() << Index;
1260 dbgs() << " => " << getAsString(Index) << " }\n";
1261 }
1262
1263 dbgs() << "]\n";
1264}
Matthias Braun8c209aa2017-01-28 02:02:38 +00001265#endif
Bill Wendlingd509a662013-01-29 00:34:06 +00001266
Bill Wendlingabf3feb2012-10-05 06:44:41 +00001267//===----------------------------------------------------------------------===//
Bill Wendlingcd330342013-01-04 23:27:34 +00001268// AttrBuilder Method Implementations
Bill Wendlingabf3feb2012-10-05 06:44:41 +00001269//===----------------------------------------------------------------------===//
1270
Reid Klecknerb5180542017-03-21 16:57:19 +00001271AttrBuilder::AttrBuilder(AttributeList AS, unsigned Index) {
1272 AttributeListImpl *pImpl = AS.pImpl;
Bill Wendling096f5442013-01-07 08:24:35 +00001273 if (!pImpl) return;
1274
Amaury Sechet24c84fd2016-06-14 22:04:16 +00001275 for (unsigned I = 0, E = pImpl->getNumSlots(); I != E; ++I) {
Bill Wendling211316c2013-04-18 20:17:28 +00001276 if (pImpl->getSlotIndex(I) != Index) continue;
Bill Wendling096f5442013-01-07 08:24:35 +00001277
Reid Klecknerb5180542017-03-21 16:57:19 +00001278 for (AttributeListImpl::iterator II = pImpl->begin(I), IE = pImpl->end(I);
1279 II != IE; ++II)
Bill Wendling23804da2013-01-31 23:38:01 +00001280 addAttribute(*II);
Bill Wendling9eb689c2013-01-28 00:21:34 +00001281
1282 break;
1283 }
Bill Wendling096f5442013-01-07 08:24:35 +00001284}
1285
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00001286AttrBuilder::AttrBuilder(AttributeSetNode *AS) {
1287 if (AS) {
1288 for (const Attribute &A : *AS)
1289 addAttribute(A);
1290 }
1291}
1292
Bill Wendlingcd330342013-01-04 23:27:34 +00001293void AttrBuilder::clear() {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001294 Attrs.reset();
Sanjoy Das88d0fde2015-09-03 22:27:42 +00001295 TargetDepAttrs.clear();
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001296 Alignment = StackAlignment = DerefBytes = DerefOrNullBytes = 0;
George Burgess IV278199f2016-04-12 01:05:35 +00001297 AllocSizeArgs = 0;
Bill Wendlingcd330342013-01-04 23:27:34 +00001298}
1299
1300AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001301 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
Bill Wendling1c7cc8a2013-01-31 23:16:25 +00001302 assert(Val != Attribute::Alignment && Val != Attribute::StackAlignment &&
George Burgess IV278199f2016-04-12 01:05:35 +00001303 Val != Attribute::Dereferenceable && Val != Attribute::AllocSize &&
Hal Finkelb0407ba2014-07-18 15:51:28 +00001304 "Adding integer attribute without adding a value!");
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001305 Attrs[Val] = true;
Bill Wendling7c04e042012-10-09 19:01:18 +00001306 return *this;
Bill Wendlingabf3feb2012-10-05 06:44:41 +00001307}
1308
Bill Wendling23804da2013-01-31 23:38:01 +00001309AttrBuilder &AttrBuilder::addAttribute(Attribute Attr) {
Bill Wendling0a437302013-02-10 10:13:23 +00001310 if (Attr.isStringAttribute()) {
1311 addAttribute(Attr.getKindAsString(), Attr.getValueAsString());
1312 return *this;
1313 }
1314
Bill Wendling3f12ac22013-02-05 22:37:24 +00001315 Attribute::AttrKind Kind = Attr.getKindAsEnum();
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001316 Attrs[Kind] = true;
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001317
Bill Wendling3f12ac22013-02-05 22:37:24 +00001318 if (Kind == Attribute::Alignment)
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001319 Alignment = Attr.getAlignment();
Bill Wendling3f12ac22013-02-05 22:37:24 +00001320 else if (Kind == Attribute::StackAlignment)
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001321 StackAlignment = Attr.getStackAlignment();
Hal Finkelb0407ba2014-07-18 15:51:28 +00001322 else if (Kind == Attribute::Dereferenceable)
1323 DerefBytes = Attr.getDereferenceableBytes();
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001324 else if (Kind == Attribute::DereferenceableOrNull)
1325 DerefOrNullBytes = Attr.getDereferenceableOrNullBytes();
George Burgess IV278199f2016-04-12 01:05:35 +00001326 else if (Kind == Attribute::AllocSize)
1327 AllocSizeArgs = Attr.getValueAsInt();
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001328 return *this;
1329}
1330
Bill Wendlingb9c5b1a2013-02-05 08:09:32 +00001331AttrBuilder &AttrBuilder::addAttribute(StringRef A, StringRef V) {
1332 TargetDepAttrs[A] = V;
1333 return *this;
1334}
1335
Bill Wendling23804da2013-01-31 23:38:01 +00001336AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001337 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
1338 Attrs[Val] = false;
Bill Wendling23804da2013-01-31 23:38:01 +00001339
1340 if (Val == Attribute::Alignment)
1341 Alignment = 0;
1342 else if (Val == Attribute::StackAlignment)
1343 StackAlignment = 0;
Hal Finkelb0407ba2014-07-18 15:51:28 +00001344 else if (Val == Attribute::Dereferenceable)
1345 DerefBytes = 0;
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001346 else if (Val == Attribute::DereferenceableOrNull)
1347 DerefOrNullBytes = 0;
George Burgess IV278199f2016-04-12 01:05:35 +00001348 else if (Val == Attribute::AllocSize)
1349 AllocSizeArgs = 0;
Bill Wendling23804da2013-01-31 23:38:01 +00001350
1351 return *this;
1352}
1353
Reid Klecknerb5180542017-03-21 16:57:19 +00001354AttrBuilder &AttrBuilder::removeAttributes(AttributeList A, uint64_t Index) {
Bill Wendling211316c2013-04-18 20:17:28 +00001355 unsigned Slot = ~0U;
Bill Wendlingf1c94e32013-02-01 00:13:50 +00001356 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
1357 if (A.getSlotIndex(I) == Index) {
Bill Wendling211316c2013-04-18 20:17:28 +00001358 Slot = I;
Bill Wendlingf1c94e32013-02-01 00:13:50 +00001359 break;
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001360 }
Bill Wendlingf1c94e32013-02-01 00:13:50 +00001361
Reid Klecknerb5180542017-03-21 16:57:19 +00001362 assert(Slot != ~0U && "Couldn't find index in AttributeList!");
Bill Wendlingf1c94e32013-02-01 00:13:50 +00001363
Reid Klecknerb5180542017-03-21 16:57:19 +00001364 for (AttributeList::iterator I = A.begin(Slot), E = A.end(Slot); I != E;
1365 ++I) {
Bill Wendling7cde51d2013-02-12 07:56:49 +00001366 Attribute Attr = *I;
Hal Finkele15442c2014-07-18 06:51:55 +00001367 if (Attr.isEnumAttribute() || Attr.isIntAttribute()) {
George Burgess IV500d3032015-12-16 05:21:02 +00001368 removeAttribute(Attr.getKindAsEnum());
Bill Wendling7cde51d2013-02-12 07:56:49 +00001369 } else {
1370 assert(Attr.isStringAttribute() && "Invalid attribute type!");
George Burgess IV500d3032015-12-16 05:21:02 +00001371 removeAttribute(Attr.getKindAsString());
Bill Wendling7cde51d2013-02-12 07:56:49 +00001372 }
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001373 }
1374
1375 return *this;
1376}
1377
Bill Wendlingb9c5b1a2013-02-05 08:09:32 +00001378AttrBuilder &AttrBuilder::removeAttribute(StringRef A) {
1379 std::map<std::string, std::string>::iterator I = TargetDepAttrs.find(A);
1380 if (I != TargetDepAttrs.end())
1381 TargetDepAttrs.erase(I);
1382 return *this;
1383}
1384
George Burgess IV278199f2016-04-12 01:05:35 +00001385std::pair<unsigned, Optional<unsigned>> AttrBuilder::getAllocSizeArgs() const {
1386 return unpackAllocSizeArgs(AllocSizeArgs);
1387}
1388
Bill Wendling50d27842012-10-15 20:35:56 +00001389AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
Bill Wendlingabd5ba22012-10-14 03:58:29 +00001390 if (Align == 0) return *this;
Bill Wendlingcd330342013-01-04 23:27:34 +00001391
Bill Wendlingabf3feb2012-10-05 06:44:41 +00001392 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1393 assert(Align <= 0x40000000 && "Alignment too large.");
Bill Wendlingcd330342013-01-04 23:27:34 +00001394
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001395 Attrs[Attribute::Alignment] = true;
Bill Wendlingcd330342013-01-04 23:27:34 +00001396 Alignment = Align;
Bill Wendlingabd5ba22012-10-14 03:58:29 +00001397 return *this;
Chris Lattner3e13b8c2008-01-02 23:42:30 +00001398}
1399
Bill Wendlingcd330342013-01-04 23:27:34 +00001400AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
1401 // Default alignment, allow the target to define how to align it.
1402 if (Align == 0) return *this;
1403
1404 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1405 assert(Align <= 0x100 && "Alignment too large.");
1406
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001407 Attrs[Attribute::StackAlignment] = true;
Bill Wendlingcd330342013-01-04 23:27:34 +00001408 StackAlignment = Align;
1409 return *this;
1410}
1411
Hal Finkelb0407ba2014-07-18 15:51:28 +00001412AttrBuilder &AttrBuilder::addDereferenceableAttr(uint64_t Bytes) {
1413 if (Bytes == 0) return *this;
1414
1415 Attrs[Attribute::Dereferenceable] = true;
1416 DerefBytes = Bytes;
1417 return *this;
1418}
1419
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001420AttrBuilder &AttrBuilder::addDereferenceableOrNullAttr(uint64_t Bytes) {
1421 if (Bytes == 0)
1422 return *this;
1423
1424 Attrs[Attribute::DereferenceableOrNull] = true;
1425 DerefOrNullBytes = Bytes;
1426 return *this;
1427}
1428
George Burgess IV278199f2016-04-12 01:05:35 +00001429AttrBuilder &AttrBuilder::addAllocSizeAttr(unsigned ElemSize,
1430 const Optional<unsigned> &NumElems) {
1431 return addAllocSizeAttrFromRawRepr(packAllocSizeArgs(ElemSize, NumElems));
1432}
1433
1434AttrBuilder &AttrBuilder::addAllocSizeAttrFromRawRepr(uint64_t RawArgs) {
1435 // (0, 0) is our "not present" value, so we need to check for it here.
1436 assert(RawArgs && "Invalid allocsize arguments -- given allocsize(0, 0)");
1437
1438 Attrs[Attribute::AllocSize] = true;
1439 // Reuse existing machinery to store this as a single 64-bit integer so we can
1440 // save a few bytes over using a pair<unsigned, Optional<unsigned>>.
1441 AllocSizeArgs = RawArgs;
1442 return *this;
1443}
1444
Bill Wendlinge2614922013-02-06 01:16:00 +00001445AttrBuilder &AttrBuilder::merge(const AttrBuilder &B) {
1446 // FIXME: What if both have alignments, but they don't match?!
1447 if (!Alignment)
1448 Alignment = B.Alignment;
1449
1450 if (!StackAlignment)
1451 StackAlignment = B.StackAlignment;
1452
Hal Finkelb0407ba2014-07-18 15:51:28 +00001453 if (!DerefBytes)
1454 DerefBytes = B.DerefBytes;
1455
Pete Cooperd2a44612015-05-06 23:19:43 +00001456 if (!DerefOrNullBytes)
1457 DerefOrNullBytes = B.DerefOrNullBytes;
1458
George Burgess IV278199f2016-04-12 01:05:35 +00001459 if (!AllocSizeArgs)
1460 AllocSizeArgs = B.AllocSizeArgs;
1461
Benjamin Kramer45e7d532013-02-16 19:13:18 +00001462 Attrs |= B.Attrs;
Bill Wendlinge2614922013-02-06 01:16:00 +00001463
Pete Cooperd2a44612015-05-06 23:19:43 +00001464 for (auto I : B.td_attrs())
1465 TargetDepAttrs[I.first] = I.second;
Bill Wendlinge2614922013-02-06 01:16:00 +00001466
1467 return *this;
1468}
1469
Pete Cooperd2a44612015-05-06 23:19:43 +00001470AttrBuilder &AttrBuilder::remove(const AttrBuilder &B) {
1471 // FIXME: What if both have alignments, but they don't match?!
1472 if (B.Alignment)
1473 Alignment = 0;
1474
1475 if (B.StackAlignment)
1476 StackAlignment = 0;
1477
1478 if (B.DerefBytes)
1479 DerefBytes = 0;
1480
1481 if (B.DerefOrNullBytes)
1482 DerefOrNullBytes = 0;
1483
George Burgess IV278199f2016-04-12 01:05:35 +00001484 if (B.AllocSizeArgs)
1485 AllocSizeArgs = 0;
1486
Pete Cooperd2a44612015-05-06 23:19:43 +00001487 Attrs &= ~B.Attrs;
1488
1489 for (auto I : B.td_attrs())
1490 TargetDepAttrs.erase(I.first);
1491
1492 return *this;
1493}
1494
1495bool AttrBuilder::overlaps(const AttrBuilder &B) const {
1496 // First check if any of the target independent attributes overlap.
1497 if ((Attrs & B.Attrs).any())
1498 return true;
1499
1500 // Then check if any target dependent ones do.
Sean Silva9011aca2017-02-22 06:34:04 +00001501 for (const auto &I : td_attrs())
Pete Cooperd2a44612015-05-06 23:19:43 +00001502 if (B.contains(I.first))
1503 return true;
1504
1505 return false;
1506}
1507
Bill Wendling4b001442013-02-06 01:33:42 +00001508bool AttrBuilder::contains(StringRef A) const {
1509 return TargetDepAttrs.find(A) != TargetDepAttrs.end();
1510}
1511
Bill Wendling50d27842012-10-15 20:35:56 +00001512bool AttrBuilder::hasAttributes() const {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001513 return !Attrs.none() || !TargetDepAttrs.empty();
Bill Wendlingc6daefa2012-10-08 23:27:46 +00001514}
Bill Wendling9ac69f92013-01-04 20:54:35 +00001515
Reid Klecknerb5180542017-03-21 16:57:19 +00001516bool AttrBuilder::hasAttributes(AttributeList A, uint64_t Index) const {
Bill Wendling211316c2013-04-18 20:17:28 +00001517 unsigned Slot = ~0U;
Bill Wendling9ca01da2013-02-02 00:42:06 +00001518 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
1519 if (A.getSlotIndex(I) == Index) {
Bill Wendling211316c2013-04-18 20:17:28 +00001520 Slot = I;
Bill Wendling9ca01da2013-02-02 00:42:06 +00001521 break;
1522 }
1523
Bill Wendling211316c2013-04-18 20:17:28 +00001524 assert(Slot != ~0U && "Couldn't find the index!");
Bill Wendling9ca01da2013-02-02 00:42:06 +00001525
Reid Klecknerb5180542017-03-21 16:57:19 +00001526 for (AttributeList::iterator I = A.begin(Slot), E = A.end(Slot); I != E;
1527 ++I) {
Bill Wendling7cde51d2013-02-12 07:56:49 +00001528 Attribute Attr = *I;
Hal Finkele15442c2014-07-18 06:51:55 +00001529 if (Attr.isEnumAttribute() || Attr.isIntAttribute()) {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001530 if (Attrs[I->getKindAsEnum()])
Bill Wendling7cde51d2013-02-12 07:56:49 +00001531 return true;
1532 } else {
1533 assert(Attr.isStringAttribute() && "Invalid attribute kind!");
1534 return TargetDepAttrs.find(Attr.getKindAsString())!=TargetDepAttrs.end();
1535 }
1536 }
Bill Wendling9ca01da2013-02-02 00:42:06 +00001537
1538 return false;
Bill Wendling70f39172012-10-09 00:01:21 +00001539}
Bill Wendling9ac69f92013-01-04 20:54:35 +00001540
Bill Wendling50d27842012-10-15 20:35:56 +00001541bool AttrBuilder::hasAlignmentAttr() const {
Bill Wendlingcd330342013-01-04 23:27:34 +00001542 return Alignment != 0;
Bill Wendlingc6daefa2012-10-08 23:27:46 +00001543}
1544
Bill Wendlingd509a662013-01-29 00:34:06 +00001545bool AttrBuilder::operator==(const AttrBuilder &B) {
Benjamin Kramer45e7d532013-02-16 19:13:18 +00001546 if (Attrs != B.Attrs)
1547 return false;
Bill Wendling4b001442013-02-06 01:33:42 +00001548
1549 for (td_const_iterator I = TargetDepAttrs.begin(),
1550 E = TargetDepAttrs.end(); I != E; ++I)
1551 if (B.TargetDepAttrs.find(I->first) == B.TargetDepAttrs.end())
1552 return false;
1553
Hal Finkelb0407ba2014-07-18 15:51:28 +00001554 return Alignment == B.Alignment && StackAlignment == B.StackAlignment &&
1555 DerefBytes == B.DerefBytes;
Bill Wendlingd509a662013-01-29 00:34:06 +00001556}
1557
Bill Wendling57625a42013-01-25 23:09:36 +00001558//===----------------------------------------------------------------------===//
1559// AttributeFuncs Function Defintions
1560//===----------------------------------------------------------------------===//
1561
Bill Wendlingc79cdff2013-02-01 01:04:27 +00001562/// \brief Which attributes cannot be applied to a type.
Craig Toppere3dcce92015-08-01 22:20:21 +00001563AttrBuilder AttributeFuncs::typeIncompatible(Type *Ty) {
Bill Wendling57625a42013-01-25 23:09:36 +00001564 AttrBuilder Incompatible;
1565
1566 if (!Ty->isIntegerTy())
1567 // Attribute that only apply to integers.
1568 Incompatible.addAttribute(Attribute::SExt)
1569 .addAttribute(Attribute::ZExt);
1570
1571 if (!Ty->isPointerTy())
1572 // Attribute that only apply to pointers.
1573 Incompatible.addAttribute(Attribute::ByVal)
1574 .addAttribute(Attribute::Nest)
1575 .addAttribute(Attribute::NoAlias)
1576 .addAttribute(Attribute::NoCapture)
Nick Lewyckyd52b1522014-05-20 01:23:40 +00001577 .addAttribute(Attribute::NonNull)
Hal Finkelb0407ba2014-07-18 15:51:28 +00001578 .addDereferenceableAttr(1) // the int here is ignored
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001579 .addDereferenceableOrNullAttr(1) // the int here is ignored
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001580 .addAttribute(Attribute::ReadNone)
1581 .addAttribute(Attribute::ReadOnly)
Reid Klecknera534a382013-12-19 02:14:12 +00001582 .addAttribute(Attribute::StructRet)
1583 .addAttribute(Attribute::InAlloca);
Bill Wendling57625a42013-01-25 23:09:36 +00001584
Pete Cooper2777d8872015-05-06 23:19:56 +00001585 return Incompatible;
Bill Wendling57625a42013-01-25 23:09:36 +00001586}
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001587
1588template<typename AttrClass>
1589static bool isEqual(const Function &Caller, const Function &Callee) {
1590 return Caller.getFnAttribute(AttrClass::getKind()) ==
1591 Callee.getFnAttribute(AttrClass::getKind());
1592}
1593
1594/// \brief Compute the logical AND of the attributes of the caller and the
1595/// callee.
1596///
1597/// This function sets the caller's attribute to false if the callee's attribute
1598/// is false.
1599template<typename AttrClass>
1600static void setAND(Function &Caller, const Function &Callee) {
1601 if (AttrClass::isSet(Caller, AttrClass::getKind()) &&
1602 !AttrClass::isSet(Callee, AttrClass::getKind()))
1603 AttrClass::set(Caller, AttrClass::getKind(), false);
1604}
1605
1606/// \brief Compute the logical OR of the attributes of the caller and the
1607/// callee.
1608///
1609/// This function sets the caller's attribute to true if the callee's attribute
1610/// is true.
1611template<typename AttrClass>
1612static void setOR(Function &Caller, const Function &Callee) {
1613 if (!AttrClass::isSet(Caller, AttrClass::getKind()) &&
1614 AttrClass::isSet(Callee, AttrClass::getKind()))
1615 AttrClass::set(Caller, AttrClass::getKind(), true);
1616}
1617
1618/// \brief If the inlined function had a higher stack protection level than the
1619/// calling function, then bump up the caller's stack protection level.
1620static void adjustCallerSSPLevel(Function &Caller, const Function &Callee) {
1621 // If upgrading the SSP attribute, clear out the old SSP Attributes first.
1622 // Having multiple SSP attributes doesn't actually hurt, but it adds useless
1623 // clutter to the IR.
1624 AttrBuilder B;
1625 B.addAttribute(Attribute::StackProtect)
1626 .addAttribute(Attribute::StackProtectStrong)
1627 .addAttribute(Attribute::StackProtectReq);
Reid Klecknerb5180542017-03-21 16:57:19 +00001628 AttributeList OldSSPAttr =
1629 AttributeList::get(Caller.getContext(), AttributeList::FunctionIndex, B);
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001630
Evgeniy Stepanovf17120a2016-04-11 22:27:48 +00001631 if (Callee.hasFnAttribute(Attribute::StackProtectReq)) {
Reid Klecknerb5180542017-03-21 16:57:19 +00001632 Caller.removeAttributes(AttributeList::FunctionIndex, OldSSPAttr);
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001633 Caller.addFnAttr(Attribute::StackProtectReq);
1634 } else if (Callee.hasFnAttribute(Attribute::StackProtectStrong) &&
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001635 !Caller.hasFnAttribute(Attribute::StackProtectReq)) {
Reid Klecknerb5180542017-03-21 16:57:19 +00001636 Caller.removeAttributes(AttributeList::FunctionIndex, OldSSPAttr);
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001637 Caller.addFnAttr(Attribute::StackProtectStrong);
1638 } else if (Callee.hasFnAttribute(Attribute::StackProtect) &&
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001639 !Caller.hasFnAttribute(Attribute::StackProtectReq) &&
1640 !Caller.hasFnAttribute(Attribute::StackProtectStrong))
1641 Caller.addFnAttr(Attribute::StackProtect);
1642}
1643
1644#define GET_ATTR_COMPAT_FUNC
1645#include "AttributesCompatFunc.inc"
1646
1647bool AttributeFuncs::areInlineCompatible(const Function &Caller,
1648 const Function &Callee) {
1649 return hasCompatibleFnAttrs(Caller, Callee);
1650}
1651
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001652void AttributeFuncs::mergeAttributesForInlining(Function &Caller,
1653 const Function &Callee) {
1654 mergeFnAttrs(Caller, Callee);
1655}