blob: bcdc97e2ed88d972c51b21079c825162a1539234 [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,
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000510 Attribute::AttrKind Kind) const {
Javed Absarf3d79042017-05-11 12:28:08 +0000511 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,
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000518 StringRef Value) const {
Javed Absarf3d79042017-05-11 12:28:08 +0000519 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 Kleckner8bf67fe2017-05-23 17:01:48 +0000791/// Map from AttributeList index to the internal array index. Adding one works:
792/// FunctionIndex: ~0U -> 0
793/// ReturnIndex: 0 -> 1
794/// FirstArgIndex: 1.. -> 2..
795static constexpr unsigned attrIdxToArrayIdx(unsigned Index) {
796 return Index + 1;
797}
798
799AttributeListImpl::AttributeListImpl(LLVMContext &C,
800 ArrayRef<AttributeSet> Sets)
801 : AvailableFunctionAttrs(0), Context(C), NumAttrSets(Sets.size()) {
802 assert(!Sets.empty() && "pointless AttributeListImpl");
Reid Klecknera82be602017-04-11 00:16:00 +0000803
804 // There's memory after the node where we can store the entries in.
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000805 std::copy(Sets.begin(), Sets.end(), getTrailingObjects<AttributeSet>());
Reid Klecknera82be602017-04-11 00:16:00 +0000806
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");
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000811 static_assert(attrIdxToArrayIdx(AttributeList::FunctionIndex) == 0U,
812 "function should be stored in slot 0");
813 for (Attribute I : Sets[0]) {
814 if (!I.isStringAttribute())
815 AvailableFunctionAttrs |= 1ULL << I.getKindAsEnum();
Reid Klecknera82be602017-04-11 00:16:00 +0000816 }
817}
818
819void AttributeListImpl::Profile(FoldingSetNodeID &ID) const {
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000820 Profile(ID, makeArrayRef(begin(), end()));
Reid Klecknera82be602017-04-11 00:16:00 +0000821}
822
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000823void AttributeListImpl::Profile(FoldingSetNodeID &ID,
824 ArrayRef<AttributeSet> Sets) {
825 for (const auto &Set : Sets)
826 ID.AddPointer(Set.SetNode);
Reid Klecknera82be602017-04-11 00:16:00 +0000827}
828
Matthias Braun8c209aa2017-01-28 02:02:38 +0000829#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Reid Klecknerb5180542017-03-21 16:57:19 +0000830LLVM_DUMP_METHOD void AttributeListImpl::dump() const {
831 AttributeList(const_cast<AttributeListImpl *>(this)).dump();
Peter Collingbourneabca2ec2013-08-02 22:34:30 +0000832}
Matthias Braun8c209aa2017-01-28 02:02:38 +0000833#endif
Peter Collingbourneabca2ec2013-08-02 22:34:30 +0000834
Bill Wendlingd509a662013-01-29 00:34:06 +0000835//===----------------------------------------------------------------------===//
Reid Klecknerb5180542017-03-21 16:57:19 +0000836// AttributeList Construction and Mutation Methods
Bill Wendlingd509a662013-01-29 00:34:06 +0000837//===----------------------------------------------------------------------===//
838
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000839AttributeList AttributeList::getImpl(LLVMContext &C,
840 ArrayRef<AttributeSet> AttrSets) {
841 assert(!AttrSets.empty() && "pointless AttributeListImpl");
Reid Klecknereb9dd5b2017-04-10 23:31:05 +0000842
Bill Wendlingd509a662013-01-29 00:34:06 +0000843 LLVMContextImpl *pImpl = C.pImpl;
844 FoldingSetNodeID ID;
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000845 AttributeListImpl::Profile(ID, AttrSets);
Bill Wendlingd509a662013-01-29 00:34:06 +0000846
847 void *InsertPoint;
Reid Klecknerb5180542017-03-21 16:57:19 +0000848 AttributeListImpl *PA =
849 pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
Bill Wendlingd509a662013-01-29 00:34:06 +0000850
851 // If we didn't find any existing attributes of the same shape then
852 // create a new one and insert it.
853 if (!PA) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000854 // Coallocate entries after the AttributeListImpl itself.
James Y Knightaa365b22015-08-05 22:57:34 +0000855 void *Mem = ::operator new(
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000856 AttributeListImpl::totalSizeToAlloc<AttributeSet>(AttrSets.size()));
857 PA = new (Mem) AttributeListImpl(C, AttrSets);
Bill Wendlingd509a662013-01-29 00:34:06 +0000858 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
859 }
860
861 // Return the AttributesList that we found or created.
Reid Klecknerb5180542017-03-21 16:57:19 +0000862 return AttributeList(PA);
Bill Wendlingd509a662013-01-29 00:34:06 +0000863}
864
Reid Klecknerb5180542017-03-21 16:57:19 +0000865AttributeList
866AttributeList::get(LLVMContext &C,
867 ArrayRef<std::pair<unsigned, Attribute>> Attrs) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000868 // If there are no attributes then return a null AttributesList pointer.
869 if (Attrs.empty())
Reid Klecknerb5180542017-03-21 16:57:19 +0000870 return AttributeList();
Bill Wendlingd509a662013-01-29 00:34:06 +0000871
Craig Toppere30b8ca2016-01-03 19:43:40 +0000872 assert(std::is_sorted(Attrs.begin(), Attrs.end(),
873 [](const std::pair<unsigned, Attribute> &LHS,
874 const std::pair<unsigned, Attribute> &RHS) {
875 return LHS.first < RHS.first;
876 }) && "Misordered Attributes list!");
David Majnemer0a16c222016-08-11 21:15:00 +0000877 assert(none_of(Attrs,
878 [](const std::pair<unsigned, Attribute> &Pair) {
879 return Pair.second.hasAttribute(Attribute::None);
880 }) &&
881 "Pointless attribute!");
Bill Wendlingd509a662013-01-29 00:34:06 +0000882
883 // Create a vector if (unsigned, AttributeSetNode*) pairs from the attributes
884 // list.
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000885 SmallVector<std::pair<unsigned, AttributeSet>, 8> AttrPairVec;
Eugene Zelenkodeaf6952017-02-17 00:00:09 +0000886 for (ArrayRef<std::pair<unsigned, Attribute>>::iterator I = Attrs.begin(),
Bill Wendlingd509a662013-01-29 00:34:06 +0000887 E = Attrs.end(); I != E; ) {
888 unsigned Index = I->first;
889 SmallVector<Attribute, 4> AttrVec;
NAKAMURA Takumif05d2f22013-01-29 15:18:16 +0000890 while (I != E && I->first == Index) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000891 AttrVec.push_back(I->second);
892 ++I;
893 }
894
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000895 AttrPairVec.emplace_back(Index, AttributeSet::get(C, AttrVec));
Bill Wendlingd509a662013-01-29 00:34:06 +0000896 }
897
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000898 return get(C, AttrPairVec);
Bill Wendlingd509a662013-01-29 00:34:06 +0000899}
900
Reid Klecknerb5180542017-03-21 16:57:19 +0000901AttributeList
902AttributeList::get(LLVMContext &C,
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000903 ArrayRef<std::pair<unsigned, AttributeSet>> Attrs) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000904 // If there are no attributes then return a null AttributesList pointer.
905 if (Attrs.empty())
Reid Klecknerb5180542017-03-21 16:57:19 +0000906 return AttributeList();
Bill Wendlingd509a662013-01-29 00:34:06 +0000907
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000908 assert(std::is_sorted(Attrs.begin(), Attrs.end(),
909 [](const std::pair<unsigned, AttributeSet> &LHS,
910 const std::pair<unsigned, AttributeSet> &RHS) {
911 return LHS.first < RHS.first;
912 }) &&
913 "Misordered Attributes list!");
914 assert(none_of(Attrs,
915 [](const std::pair<unsigned, AttributeSet> &Pair) {
916 return !Pair.second.hasAttributes();
917 }) &&
918 "Pointless attribute!");
919
920 unsigned MaxIndex = Attrs.back().first;
921
922 SmallVector<AttributeSet, 4> AttrVec(attrIdxToArrayIdx(MaxIndex) + 1);
923 for (auto Pair : Attrs)
924 AttrVec[attrIdxToArrayIdx(Pair.first)] = Pair.second;
925
926 return getImpl(C, AttrVec);
Bill Wendlingd509a662013-01-29 00:34:06 +0000927}
928
Reid Kleckner7f720332017-04-13 00:58:09 +0000929AttributeList AttributeList::get(LLVMContext &C, AttributeSet FnAttrs,
930 AttributeSet RetAttrs,
931 ArrayRef<AttributeSet> ArgAttrs) {
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000932 // Scan from the end to find the last argument with attributes. Most
933 // arguments don't have attributes, so it's nice if we can have fewer unique
934 // AttributeListImpls by dropping empty attribute sets at the end of the list.
935 unsigned NumSets = 0;
936 for (size_t I = ArgAttrs.size(); I != 0; --I) {
937 if (ArgAttrs[I - 1].hasAttributes()) {
938 NumSets = I + 2;
939 break;
940 }
Reid Klecknereb9dd5b2017-04-10 23:31:05 +0000941 }
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000942 if (NumSets == 0) {
943 // Check function and return attributes if we didn't have argument
944 // attributes.
945 if (RetAttrs.hasAttributes())
946 NumSets = 2;
947 else if (FnAttrs.hasAttributes())
948 NumSets = 1;
949 }
950
951 // If all attribute sets were empty, we can use the empty attribute list.
952 if (NumSets == 0)
Reid Klecknereb9dd5b2017-04-10 23:31:05 +0000953 return AttributeList();
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000954
955 SmallVector<AttributeSet, 8> AttrSets;
956 AttrSets.reserve(NumSets);
957 // If we have any attributes, we always have function attributes.
958 AttrSets.push_back(FnAttrs);
959 if (NumSets > 1)
960 AttrSets.push_back(RetAttrs);
961 if (NumSets > 2) {
962 // Drop the empty argument attribute sets at the end.
963 ArgAttrs = ArgAttrs.take_front(NumSets - 2);
964 AttrSets.insert(AttrSets.end(), ArgAttrs.begin(), ArgAttrs.end());
965 }
966
967 return getImpl(C, AttrSets);
Reid Klecknereb9dd5b2017-04-10 23:31:05 +0000968}
969
Reid Klecknerb5180542017-03-21 16:57:19 +0000970AttributeList AttributeList::get(LLVMContext &C, unsigned Index,
971 const AttrBuilder &B) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000972 if (!B.hasAttributes())
Reid Klecknerb5180542017-03-21 16:57:19 +0000973 return AttributeList();
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000974 Index = attrIdxToArrayIdx(Index);
975 SmallVector<AttributeSet, 8> AttrSets(Index + 1);
976 AttrSets[Index] = AttributeSet::get(C, B);
977 return getImpl(C, AttrSets);
Bill Wendlingd509a662013-01-29 00:34:06 +0000978}
979
Reid Klecknerb5180542017-03-21 16:57:19 +0000980AttributeList AttributeList::get(LLVMContext &C, unsigned Index,
981 ArrayRef<Attribute::AttrKind> Kinds) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000982 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
Amaury Sechet392638d2016-06-14 20:27:35 +0000983 for (Attribute::AttrKind K : Kinds)
David Majnemer0a16c222016-08-11 21:15:00 +0000984 Attrs.emplace_back(Index, Attribute::get(C, K));
Bill Wendlingd509a662013-01-29 00:34:06 +0000985 return get(C, Attrs);
986}
987
Reid Klecknerb5180542017-03-21 16:57:19 +0000988AttributeList AttributeList::get(LLVMContext &C, unsigned Index,
989 ArrayRef<StringRef> Kinds) {
Amaury Sechet6100adf2016-06-15 17:50:39 +0000990 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
991 for (StringRef K : Kinds)
David Majnemer0a16c222016-08-11 21:15:00 +0000992 Attrs.emplace_back(Index, Attribute::get(C, K));
Amaury Sechet6100adf2016-06-15 17:50:39 +0000993 return get(C, Attrs);
994}
995
Reid Klecknerb5180542017-03-21 16:57:19 +0000996AttributeList AttributeList::get(LLVMContext &C,
997 ArrayRef<AttributeList> Attrs) {
998 if (Attrs.empty())
999 return AttributeList();
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001000 if (Attrs.size() == 1)
1001 return Attrs[0];
Bill Wendlingd509a662013-01-29 00:34:06 +00001002
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001003 unsigned MaxSize = 0;
1004 for (AttributeList List : Attrs)
1005 MaxSize = std::max(MaxSize, List.getNumAttrSets());
1006
1007 SmallVector<AttributeSet, 8> NewAttrSets(MaxSize);
1008 for (unsigned I = 0; I < MaxSize; ++I) {
1009 AttrBuilder CurBuilder;
1010 for (AttributeList List : Attrs)
1011 CurBuilder.merge(List.getAttributes(I - 1));
1012 NewAttrSets[I] = AttributeSet::get(C, CurBuilder);
Bill Wendlingd509a662013-01-29 00:34:06 +00001013 }
1014
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001015 return getImpl(C, NewAttrSets);
Bill Wendlingd509a662013-01-29 00:34:06 +00001016}
1017
Reid Klecknerb5180542017-03-21 16:57:19 +00001018AttributeList AttributeList::addAttribute(LLVMContext &C, unsigned Index,
1019 Attribute::AttrKind Kind) const {
Amaury Sechet392638d2016-06-14 20:27:35 +00001020 if (hasAttribute(Index, Kind)) return *this;
Reid Kleckneree4930b2017-05-02 22:07:37 +00001021 AttrBuilder B;
1022 B.addAttribute(Kind);
1023 return addAttributes(C, Index, B);
Reed Kotler795c7b42013-03-13 20:20:08 +00001024}
1025
Reid Klecknerb5180542017-03-21 16:57:19 +00001026AttributeList AttributeList::addAttribute(LLVMContext &C, unsigned Index,
1027 StringRef Kind,
1028 StringRef Value) const {
Eugene Zelenkodeaf6952017-02-17 00:00:09 +00001029 AttrBuilder B;
Bill Wendling3b2f6102013-07-25 18:34:24 +00001030 B.addAttribute(Kind, Value);
Reid Kleckneree4930b2017-05-02 22:07:37 +00001031 return addAttributes(C, Index, B);
Bill Wendling3b2f6102013-07-25 18:34:24 +00001032}
1033
Reid Klecknerb5180542017-03-21 16:57:19 +00001034AttributeList AttributeList::addAttribute(LLVMContext &C,
1035 ArrayRef<unsigned> Indices,
1036 Attribute A) const {
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00001037 assert(std::is_sorted(Indices.begin(), Indices.end()));
Reid Kleckner324c99d2017-04-10 20:18:10 +00001038
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001039 SmallVector<AttributeSet, 4> AttrSets(this->begin(), this->end());
1040 unsigned MaxIndex = attrIdxToArrayIdx(Indices.back());
1041 if (MaxIndex >= AttrSets.size())
1042 AttrSets.resize(MaxIndex + 1);
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00001043
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001044 for (unsigned Index : Indices) {
1045 Index = attrIdxToArrayIdx(Index);
1046 AttrBuilder B(AttrSets[Index]);
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00001047 B.addAttribute(A);
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001048 AttrSets[Index] = AttributeSet::get(C, B);
Akira Hatanaka237916b2015-12-02 06:58:49 +00001049 }
1050
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001051 return getImpl(C, AttrSets);
Akira Hatanaka237916b2015-12-02 06:58:49 +00001052}
1053
Reid Klecknerb5180542017-03-21 16:57:19 +00001054AttributeList AttributeList::addAttributes(LLVMContext &C, unsigned Index,
Reid Kleckner61906252017-04-19 01:51:13 +00001055 const AttrBuilder &B) const {
1056 if (!B.hasAttributes())
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00001057 return *this;
1058
Reid Klecknerfe64c012017-04-18 22:10:18 +00001059 if (!pImpl)
Reid Kleckner61906252017-04-19 01:51:13 +00001060 return AttributeList::get(C, {{Index, AttributeSet::get(C, B)}});
Reid Klecknerfe64c012017-04-18 22:10:18 +00001061
Bill Wendlingd509a662013-01-29 00:34:06 +00001062#ifndef NDEBUG
1063 // FIXME it is not obvious how this should work for alignment. For now, say
1064 // we can't change a known alignment.
Reid Klecknerbf6b3b152017-05-19 22:23:47 +00001065 unsigned OldAlign = getAttributes(Index).getAlignment();
Reid Kleckner61906252017-04-19 01:51:13 +00001066 unsigned NewAlign = B.getAlignment();
Bill Wendlingd509a662013-01-29 00:34:06 +00001067 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
1068 "Attempt to change alignment!");
1069#endif
1070
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001071 Index = attrIdxToArrayIdx(Index);
1072 SmallVector<AttributeSet, 4> AttrSets(this->begin(), this->end());
1073 if (Index >= AttrSets.size())
1074 AttrSets.resize(Index + 1);
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00001075
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001076 AttrBuilder Merged(AttrSets[Index]);
1077 Merged.merge(B);
1078 AttrSets[Index] = AttributeSet::get(C, Merged);
Bill Wendlingd509a662013-01-29 00:34:06 +00001079
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001080 return getImpl(C, AttrSets);
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00001081}
1082
Reid Klecknerb5180542017-03-21 16:57:19 +00001083AttributeList AttributeList::removeAttribute(LLVMContext &C, unsigned Index,
1084 Attribute::AttrKind Kind) const {
Amaury Sechet392638d2016-06-14 20:27:35 +00001085 if (!hasAttribute(Index, Kind)) return *this;
Reid Kleckneree4930b2017-05-02 22:07:37 +00001086 AttrBuilder B;
1087 B.addAttribute(Kind);
1088 return removeAttributes(C, Index, B);
Bill Wendlingd509a662013-01-29 00:34:06 +00001089}
1090
Reid Klecknerb5180542017-03-21 16:57:19 +00001091AttributeList AttributeList::removeAttribute(LLVMContext &C, unsigned Index,
1092 StringRef Kind) const {
Amaury Sechet6100adf2016-06-15 17:50:39 +00001093 if (!hasAttribute(Index, Kind)) return *this;
Reid Kleckneree4930b2017-05-02 22:07:37 +00001094 AttrBuilder B;
1095 B.addAttribute(Kind);
1096 return removeAttributes(C, Index, B);
Bill Wendlingd509a662013-01-29 00:34:06 +00001097}
1098
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001099AttributeList
1100AttributeList::removeAttributes(LLVMContext &C, unsigned Index,
1101 const AttrBuilder &AttrsToRemove) const {
Reid Klecknerb5180542017-03-21 16:57:19 +00001102 if (!pImpl)
1103 return AttributeList();
Pete Cooperd2a44612015-05-06 23:19:43 +00001104
1105 // FIXME it is not obvious how this should work for alignment.
1106 // For now, say we can't pass in alignment, which no current use does.
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001107 assert(!AttrsToRemove.hasAlignmentAttr() && "Attempt to change alignment!");
Pete Cooperd2a44612015-05-06 23:19:43 +00001108
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001109 Index = attrIdxToArrayIdx(Index);
1110 SmallVector<AttributeSet, 4> AttrSets(this->begin(), this->end());
1111 if (Index >= AttrSets.size())
1112 AttrSets.resize(Index + 1);
Pete Cooperd2a44612015-05-06 23:19:43 +00001113
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001114 AttrBuilder B(AttrSets[Index]);
1115 B.remove(AttrsToRemove);
1116 AttrSets[Index] = AttributeSet::get(C, B);
Pete Cooperd2a44612015-05-06 23:19:43 +00001117
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001118 return getImpl(C, AttrSets);
Pete Cooperd2a44612015-05-06 23:19:43 +00001119}
1120
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00001121AttributeList AttributeList::removeAttributes(LLVMContext &C,
1122 unsigned WithoutIndex) const {
1123 if (!pImpl)
1124 return AttributeList();
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001125 WithoutIndex = attrIdxToArrayIdx(WithoutIndex);
1126 if (WithoutIndex >= getNumAttrSets())
1127 return *this;
1128 SmallVector<AttributeSet, 4> AttrSets(this->begin(), this->end());
1129 AttrSets[WithoutIndex] = AttributeSet();
1130 return getImpl(C, AttrSets);
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00001131}
1132
Reid Klecknerb5180542017-03-21 16:57:19 +00001133AttributeList AttributeList::addDereferenceableAttr(LLVMContext &C,
1134 unsigned Index,
1135 uint64_t Bytes) const {
Eugene Zelenkodeaf6952017-02-17 00:00:09 +00001136 AttrBuilder B;
Ramkumar Ramachandra8fcb4982015-02-14 19:37:54 +00001137 B.addDereferenceableAttr(Bytes);
Reid Kleckneree4930b2017-05-02 22:07:37 +00001138 return addAttributes(C, Index, B);
Ramkumar Ramachandra8fcb4982015-02-14 19:37:54 +00001139}
1140
Reid Klecknerb5180542017-03-21 16:57:19 +00001141AttributeList
1142AttributeList::addDereferenceableOrNullAttr(LLVMContext &C, unsigned Index,
1143 uint64_t Bytes) const {
Eugene Zelenkodeaf6952017-02-17 00:00:09 +00001144 AttrBuilder B;
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001145 B.addDereferenceableOrNullAttr(Bytes);
Reid Kleckneree4930b2017-05-02 22:07:37 +00001146 return addAttributes(C, Index, B);
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001147}
1148
Reid Klecknerb5180542017-03-21 16:57:19 +00001149AttributeList
1150AttributeList::addAllocSizeAttr(LLVMContext &C, unsigned Index,
1151 unsigned ElemSizeArg,
1152 const Optional<unsigned> &NumElemsArg) {
Eugene Zelenkodeaf6952017-02-17 00:00:09 +00001153 AttrBuilder B;
George Burgess IV278199f2016-04-12 01:05:35 +00001154 B.addAllocSizeAttr(ElemSizeArg, NumElemsArg);
Reid Kleckneree4930b2017-05-02 22:07:37 +00001155 return addAttributes(C, Index, B);
George Burgess IV278199f2016-04-12 01:05:35 +00001156}
1157
Bill Wendlingd509a662013-01-29 00:34:06 +00001158//===----------------------------------------------------------------------===//
Reid Klecknerb5180542017-03-21 16:57:19 +00001159// AttributeList Accessor Methods
Bill Wendlingd509a662013-01-29 00:34:06 +00001160//===----------------------------------------------------------------------===//
1161
Reid Klecknerb5180542017-03-21 16:57:19 +00001162LLVMContext &AttributeList::getContext() const { return pImpl->getContext(); }
1163
Reid Klecknerf021fab2017-04-13 23:12:13 +00001164AttributeSet AttributeList::getParamAttributes(unsigned ArgNo) const {
Reid Klecknera0b45f42017-05-03 18:17:31 +00001165 return getAttributes(ArgNo + FirstArgIndex);
Bill Wendling5d020a32013-02-10 05:00:40 +00001166}
1167
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001168AttributeSet AttributeList::getRetAttributes() const {
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00001169 return getAttributes(ReturnIndex);
Bill Wendlingd509a662013-01-29 00:34:06 +00001170}
1171
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001172AttributeSet AttributeList::getFnAttributes() const {
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00001173 return getAttributes(FunctionIndex);
Bill Wendlingd509a662013-01-29 00:34:06 +00001174}
1175
Reid Klecknerb5180542017-03-21 16:57:19 +00001176bool AttributeList::hasAttribute(unsigned Index,
1177 Attribute::AttrKind Kind) const {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001178 return getAttributes(Index).hasAttribute(Kind);
Bill Wendlingd509a662013-01-29 00:34:06 +00001179}
1180
Reid Klecknerb5180542017-03-21 16:57:19 +00001181bool AttributeList::hasAttribute(unsigned Index, StringRef Kind) const {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001182 return getAttributes(Index).hasAttribute(Kind);
Bill Wendlingbce7b972013-02-13 08:42:21 +00001183}
1184
Reid Klecknerb5180542017-03-21 16:57:19 +00001185bool AttributeList::hasAttributes(unsigned Index) const {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001186 return getAttributes(Index).hasAttributes();
Bill Wendlingd509a662013-01-29 00:34:06 +00001187}
1188
Reid Klecknerb5180542017-03-21 16:57:19 +00001189bool AttributeList::hasFnAttribute(Attribute::AttrKind Kind) const {
Matthias Braun33282812016-01-29 22:25:19 +00001190 return pImpl && pImpl->hasFnAttribute(Kind);
1191}
1192
Reid Klecknerb5180542017-03-21 16:57:19 +00001193bool AttributeList::hasFnAttribute(StringRef Kind) const {
1194 return hasAttribute(AttributeList::FunctionIndex, Kind);
Amaury Sechet5f04d812016-09-09 04:50:38 +00001195}
1196
Reid Klecknerf021fab2017-04-13 23:12:13 +00001197bool AttributeList::hasParamAttribute(unsigned ArgNo,
1198 Attribute::AttrKind Kind) const {
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001199 return hasAttribute(ArgNo + FirstArgIndex, Kind);
Reid Klecknerf021fab2017-04-13 23:12:13 +00001200}
1201
Reid Klecknerb5180542017-03-21 16:57:19 +00001202bool AttributeList::hasAttrSomewhere(Attribute::AttrKind Attr,
1203 unsigned *Index) const {
Craig Topperc6207612014-04-09 06:08:46 +00001204 if (!pImpl) return false;
Bill Wendlingd509a662013-01-29 00:34:06 +00001205
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001206 for (unsigned I = index_begin(), E = index_end(); I != E; ++I) {
1207 if (hasAttribute(I, Attr)) {
1208 if (Index)
1209 *Index = I;
1210 return true;
1211 }
1212 }
Bill Wendlingd509a662013-01-29 00:34:06 +00001213
1214 return false;
1215}
1216
Reid Klecknerb5180542017-03-21 16:57:19 +00001217Attribute AttributeList::getAttribute(unsigned Index,
1218 Attribute::AttrKind Kind) const {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001219 return getAttributes(Index).getAttribute(Kind);
Bill Wendlingbce7b972013-02-13 08:42:21 +00001220}
1221
Reid Klecknerb5180542017-03-21 16:57:19 +00001222Attribute AttributeList::getAttribute(unsigned Index, StringRef Kind) const {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001223 return getAttributes(Index).getAttribute(Kind);
Bill Wendlingbce7b972013-02-13 08:42:21 +00001224}
1225
Reid Kleckner859f8b52017-04-28 20:34:27 +00001226unsigned AttributeList::getRetAlignment() const {
1227 return getAttributes(ReturnIndex).getAlignment();
1228}
1229
1230unsigned AttributeList::getParamAlignment(unsigned ArgNo) const {
Reid Klecknera0b45f42017-05-03 18:17:31 +00001231 return getAttributes(ArgNo + FirstArgIndex).getAlignment();
Bill Wendlingd509a662013-01-29 00:34:06 +00001232}
1233
Reid Klecknerb5180542017-03-21 16:57:19 +00001234unsigned AttributeList::getStackAlignment(unsigned Index) const {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001235 return getAttributes(Index).getStackAlignment();
Bill Wendlingd509a662013-01-29 00:34:06 +00001236}
1237
Reid Klecknerb5180542017-03-21 16:57:19 +00001238uint64_t AttributeList::getDereferenceableBytes(unsigned Index) const {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001239 return getAttributes(Index).getDereferenceableBytes();
Hal Finkelb0407ba2014-07-18 15:51:28 +00001240}
1241
Reid Klecknerb5180542017-03-21 16:57:19 +00001242uint64_t AttributeList::getDereferenceableOrNullBytes(unsigned Index) const {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001243 return getAttributes(Index).getDereferenceableOrNullBytes();
Sanjoy Das06cf33f2015-05-06 17:41:54 +00001244}
1245
George Burgess IV278199f2016-04-12 01:05:35 +00001246std::pair<unsigned, Optional<unsigned>>
Reid Klecknerb5180542017-03-21 16:57:19 +00001247AttributeList::getAllocSizeArgs(unsigned Index) const {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001248 return getAttributes(Index).getAllocSizeArgs();
George Burgess IV278199f2016-04-12 01:05:35 +00001249}
1250
Reid Klecknerb5180542017-03-21 16:57:19 +00001251std::string AttributeList::getAsString(unsigned Index, bool InAttrGrp) const {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001252 return getAttributes(Index).getAsString(InAttrGrp);
Bill Wendlingd509a662013-01-29 00:34:06 +00001253}
1254
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001255AttributeSet AttributeList::getAttributes(unsigned Index) const {
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001256 Index = attrIdxToArrayIdx(Index);
1257 if (!pImpl || Index >= getNumAttrSets())
1258 return AttributeSet();
1259 return pImpl->begin()[Index];
Bill Wendlingd509a662013-01-29 00:34:06 +00001260}
1261
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001262AttributeList::iterator AttributeList::begin() const {
1263 return pImpl ? pImpl->begin() : nullptr;
Bill Wendlinga9174862013-01-31 23:53:05 +00001264}
1265
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001266AttributeList::iterator AttributeList::end() const {
1267 return pImpl ? pImpl->end() : nullptr;
Bill Wendlinga9174862013-01-31 23:53:05 +00001268}
1269
Bill Wendlingd509a662013-01-29 00:34:06 +00001270//===----------------------------------------------------------------------===//
Reid Klecknerb5180542017-03-21 16:57:19 +00001271// AttributeList Introspection Methods
Bill Wendlingd509a662013-01-29 00:34:06 +00001272//===----------------------------------------------------------------------===//
1273
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001274unsigned AttributeList::getNumAttrSets() const {
1275 return pImpl ? pImpl->NumAttrSets : 0;
Bill Wendlingd509a662013-01-29 00:34:06 +00001276}
1277
Matthias Braun8c209aa2017-01-28 02:02:38 +00001278#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Reid Klecknerb5180542017-03-21 16:57:19 +00001279LLVM_DUMP_METHOD void AttributeList::dump() const {
Bill Wendlingd509a662013-01-29 00:34:06 +00001280 dbgs() << "PAL[\n";
1281
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001282 for (unsigned i = index_begin(), e = index_end(); i != e; ++i) {
1283 if (getAttributes(i).hasAttributes())
1284 dbgs() << " { " << i << " => " << getAsString(i) << " }\n";
Bill Wendlingd509a662013-01-29 00:34:06 +00001285 }
1286
1287 dbgs() << "]\n";
1288}
Matthias Braun8c209aa2017-01-28 02:02:38 +00001289#endif
Bill Wendlingd509a662013-01-29 00:34:06 +00001290
Bill Wendlingabf3feb2012-10-05 06:44:41 +00001291//===----------------------------------------------------------------------===//
Bill Wendlingcd330342013-01-04 23:27:34 +00001292// AttrBuilder Method Implementations
Bill Wendlingabf3feb2012-10-05 06:44:41 +00001293//===----------------------------------------------------------------------===//
1294
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001295// FIXME: Remove this ctor, use AttributeSet.
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001296AttrBuilder::AttrBuilder(AttributeList AL, unsigned Index) {
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001297 AttributeSet AS = AL.getAttributes(Index);
1298 for (const Attribute &A : AS)
1299 addAttribute(A);
Bill Wendling096f5442013-01-07 08:24:35 +00001300}
1301
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001302AttrBuilder::AttrBuilder(AttributeSet AS) {
Reid Kleckner8bf67fe2017-05-23 17:01:48 +00001303 for (const Attribute &A : AS)
1304 addAttribute(A);
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00001305}
1306
Bill Wendlingcd330342013-01-04 23:27:34 +00001307void AttrBuilder::clear() {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001308 Attrs.reset();
Sanjoy Das88d0fde2015-09-03 22:27:42 +00001309 TargetDepAttrs.clear();
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001310 Alignment = StackAlignment = DerefBytes = DerefOrNullBytes = 0;
George Burgess IV278199f2016-04-12 01:05:35 +00001311 AllocSizeArgs = 0;
Bill Wendlingcd330342013-01-04 23:27:34 +00001312}
1313
1314AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001315 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
Bill Wendling1c7cc8a2013-01-31 23:16:25 +00001316 assert(Val != Attribute::Alignment && Val != Attribute::StackAlignment &&
George Burgess IV278199f2016-04-12 01:05:35 +00001317 Val != Attribute::Dereferenceable && Val != Attribute::AllocSize &&
Hal Finkelb0407ba2014-07-18 15:51:28 +00001318 "Adding integer attribute without adding a value!");
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001319 Attrs[Val] = true;
Bill Wendling7c04e042012-10-09 19:01:18 +00001320 return *this;
Bill Wendlingabf3feb2012-10-05 06:44:41 +00001321}
1322
Bill Wendling23804da2013-01-31 23:38:01 +00001323AttrBuilder &AttrBuilder::addAttribute(Attribute Attr) {
Bill Wendling0a437302013-02-10 10:13:23 +00001324 if (Attr.isStringAttribute()) {
1325 addAttribute(Attr.getKindAsString(), Attr.getValueAsString());
1326 return *this;
1327 }
1328
Bill Wendling3f12ac22013-02-05 22:37:24 +00001329 Attribute::AttrKind Kind = Attr.getKindAsEnum();
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001330 Attrs[Kind] = true;
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001331
Bill Wendling3f12ac22013-02-05 22:37:24 +00001332 if (Kind == Attribute::Alignment)
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001333 Alignment = Attr.getAlignment();
Bill Wendling3f12ac22013-02-05 22:37:24 +00001334 else if (Kind == Attribute::StackAlignment)
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001335 StackAlignment = Attr.getStackAlignment();
Hal Finkelb0407ba2014-07-18 15:51:28 +00001336 else if (Kind == Attribute::Dereferenceable)
1337 DerefBytes = Attr.getDereferenceableBytes();
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001338 else if (Kind == Attribute::DereferenceableOrNull)
1339 DerefOrNullBytes = Attr.getDereferenceableOrNullBytes();
George Burgess IV278199f2016-04-12 01:05:35 +00001340 else if (Kind == Attribute::AllocSize)
1341 AllocSizeArgs = Attr.getValueAsInt();
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001342 return *this;
1343}
1344
Bill Wendlingb9c5b1a2013-02-05 08:09:32 +00001345AttrBuilder &AttrBuilder::addAttribute(StringRef A, StringRef V) {
1346 TargetDepAttrs[A] = V;
1347 return *this;
1348}
1349
Bill Wendling23804da2013-01-31 23:38:01 +00001350AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001351 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
1352 Attrs[Val] = false;
Bill Wendling23804da2013-01-31 23:38:01 +00001353
1354 if (Val == Attribute::Alignment)
1355 Alignment = 0;
1356 else if (Val == Attribute::StackAlignment)
1357 StackAlignment = 0;
Hal Finkelb0407ba2014-07-18 15:51:28 +00001358 else if (Val == Attribute::Dereferenceable)
1359 DerefBytes = 0;
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001360 else if (Val == Attribute::DereferenceableOrNull)
1361 DerefOrNullBytes = 0;
George Burgess IV278199f2016-04-12 01:05:35 +00001362 else if (Val == Attribute::AllocSize)
1363 AllocSizeArgs = 0;
Bill Wendling23804da2013-01-31 23:38:01 +00001364
1365 return *this;
1366}
1367
Reid Klecknerb5180542017-03-21 16:57:19 +00001368AttrBuilder &AttrBuilder::removeAttributes(AttributeList A, uint64_t Index) {
Reid Kleckner6652a522017-04-28 18:37:16 +00001369 remove(A.getAttributes(Index));
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001370 return *this;
1371}
1372
Bill Wendlingb9c5b1a2013-02-05 08:09:32 +00001373AttrBuilder &AttrBuilder::removeAttribute(StringRef A) {
1374 std::map<std::string, std::string>::iterator I = TargetDepAttrs.find(A);
1375 if (I != TargetDepAttrs.end())
1376 TargetDepAttrs.erase(I);
1377 return *this;
1378}
1379
George Burgess IV278199f2016-04-12 01:05:35 +00001380std::pair<unsigned, Optional<unsigned>> AttrBuilder::getAllocSizeArgs() const {
1381 return unpackAllocSizeArgs(AllocSizeArgs);
1382}
1383
Bill Wendling50d27842012-10-15 20:35:56 +00001384AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
Bill Wendlingabd5ba22012-10-14 03:58:29 +00001385 if (Align == 0) return *this;
Bill Wendlingcd330342013-01-04 23:27:34 +00001386
Bill Wendlingabf3feb2012-10-05 06:44:41 +00001387 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1388 assert(Align <= 0x40000000 && "Alignment too large.");
Bill Wendlingcd330342013-01-04 23:27:34 +00001389
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001390 Attrs[Attribute::Alignment] = true;
Bill Wendlingcd330342013-01-04 23:27:34 +00001391 Alignment = Align;
Bill Wendlingabd5ba22012-10-14 03:58:29 +00001392 return *this;
Chris Lattner3e13b8c2008-01-02 23:42:30 +00001393}
1394
Bill Wendlingcd330342013-01-04 23:27:34 +00001395AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
1396 // Default alignment, allow the target to define how to align it.
1397 if (Align == 0) return *this;
1398
1399 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1400 assert(Align <= 0x100 && "Alignment too large.");
1401
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001402 Attrs[Attribute::StackAlignment] = true;
Bill Wendlingcd330342013-01-04 23:27:34 +00001403 StackAlignment = Align;
1404 return *this;
1405}
1406
Hal Finkelb0407ba2014-07-18 15:51:28 +00001407AttrBuilder &AttrBuilder::addDereferenceableAttr(uint64_t Bytes) {
1408 if (Bytes == 0) return *this;
1409
1410 Attrs[Attribute::Dereferenceable] = true;
1411 DerefBytes = Bytes;
1412 return *this;
1413}
1414
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001415AttrBuilder &AttrBuilder::addDereferenceableOrNullAttr(uint64_t Bytes) {
1416 if (Bytes == 0)
1417 return *this;
1418
1419 Attrs[Attribute::DereferenceableOrNull] = true;
1420 DerefOrNullBytes = Bytes;
1421 return *this;
1422}
1423
George Burgess IV278199f2016-04-12 01:05:35 +00001424AttrBuilder &AttrBuilder::addAllocSizeAttr(unsigned ElemSize,
1425 const Optional<unsigned> &NumElems) {
1426 return addAllocSizeAttrFromRawRepr(packAllocSizeArgs(ElemSize, NumElems));
1427}
1428
1429AttrBuilder &AttrBuilder::addAllocSizeAttrFromRawRepr(uint64_t RawArgs) {
1430 // (0, 0) is our "not present" value, so we need to check for it here.
1431 assert(RawArgs && "Invalid allocsize arguments -- given allocsize(0, 0)");
1432
1433 Attrs[Attribute::AllocSize] = true;
1434 // Reuse existing machinery to store this as a single 64-bit integer so we can
1435 // save a few bytes over using a pair<unsigned, Optional<unsigned>>.
1436 AllocSizeArgs = RawArgs;
1437 return *this;
1438}
1439
Bill Wendlinge2614922013-02-06 01:16:00 +00001440AttrBuilder &AttrBuilder::merge(const AttrBuilder &B) {
1441 // FIXME: What if both have alignments, but they don't match?!
1442 if (!Alignment)
1443 Alignment = B.Alignment;
1444
1445 if (!StackAlignment)
1446 StackAlignment = B.StackAlignment;
1447
Hal Finkelb0407ba2014-07-18 15:51:28 +00001448 if (!DerefBytes)
1449 DerefBytes = B.DerefBytes;
1450
Pete Cooperd2a44612015-05-06 23:19:43 +00001451 if (!DerefOrNullBytes)
1452 DerefOrNullBytes = B.DerefOrNullBytes;
1453
George Burgess IV278199f2016-04-12 01:05:35 +00001454 if (!AllocSizeArgs)
1455 AllocSizeArgs = B.AllocSizeArgs;
1456
Benjamin Kramer45e7d532013-02-16 19:13:18 +00001457 Attrs |= B.Attrs;
Bill Wendlinge2614922013-02-06 01:16:00 +00001458
Pete Cooperd2a44612015-05-06 23:19:43 +00001459 for (auto I : B.td_attrs())
1460 TargetDepAttrs[I.first] = I.second;
Bill Wendlinge2614922013-02-06 01:16:00 +00001461
1462 return *this;
1463}
1464
Pete Cooperd2a44612015-05-06 23:19:43 +00001465AttrBuilder &AttrBuilder::remove(const AttrBuilder &B) {
1466 // FIXME: What if both have alignments, but they don't match?!
1467 if (B.Alignment)
1468 Alignment = 0;
1469
1470 if (B.StackAlignment)
1471 StackAlignment = 0;
1472
1473 if (B.DerefBytes)
1474 DerefBytes = 0;
1475
1476 if (B.DerefOrNullBytes)
1477 DerefOrNullBytes = 0;
1478
George Burgess IV278199f2016-04-12 01:05:35 +00001479 if (B.AllocSizeArgs)
1480 AllocSizeArgs = 0;
1481
Pete Cooperd2a44612015-05-06 23:19:43 +00001482 Attrs &= ~B.Attrs;
1483
1484 for (auto I : B.td_attrs())
1485 TargetDepAttrs.erase(I.first);
1486
1487 return *this;
1488}
1489
1490bool AttrBuilder::overlaps(const AttrBuilder &B) const {
1491 // First check if any of the target independent attributes overlap.
1492 if ((Attrs & B.Attrs).any())
1493 return true;
1494
1495 // Then check if any target dependent ones do.
Sean Silva9011aca2017-02-22 06:34:04 +00001496 for (const auto &I : td_attrs())
Pete Cooperd2a44612015-05-06 23:19:43 +00001497 if (B.contains(I.first))
1498 return true;
1499
1500 return false;
1501}
1502
Bill Wendling4b001442013-02-06 01:33:42 +00001503bool AttrBuilder::contains(StringRef A) const {
1504 return TargetDepAttrs.find(A) != TargetDepAttrs.end();
1505}
1506
Bill Wendling50d27842012-10-15 20:35:56 +00001507bool AttrBuilder::hasAttributes() const {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001508 return !Attrs.none() || !TargetDepAttrs.empty();
Bill Wendlingc6daefa2012-10-08 23:27:46 +00001509}
Bill Wendling9ac69f92013-01-04 20:54:35 +00001510
Reid Kleckner6652a522017-04-28 18:37:16 +00001511bool AttrBuilder::hasAttributes(AttributeList AL, uint64_t Index) const {
1512 AttributeSet AS = AL.getAttributes(Index);
Bill Wendling9ca01da2013-02-02 00:42:06 +00001513
Reid Kleckner6652a522017-04-28 18:37:16 +00001514 for (Attribute Attr : AS) {
Hal Finkele15442c2014-07-18 06:51:55 +00001515 if (Attr.isEnumAttribute() || Attr.isIntAttribute()) {
Reid Kleckner6652a522017-04-28 18:37:16 +00001516 if (contains(Attr.getKindAsEnum()))
Bill Wendling7cde51d2013-02-12 07:56:49 +00001517 return true;
1518 } else {
1519 assert(Attr.isStringAttribute() && "Invalid attribute kind!");
Reid Kleckner6652a522017-04-28 18:37:16 +00001520 return contains(Attr.getKindAsString());
Bill Wendling7cde51d2013-02-12 07:56:49 +00001521 }
1522 }
Bill Wendling9ca01da2013-02-02 00:42:06 +00001523
1524 return false;
Bill Wendling70f39172012-10-09 00:01:21 +00001525}
Bill Wendling9ac69f92013-01-04 20:54:35 +00001526
Bill Wendling50d27842012-10-15 20:35:56 +00001527bool AttrBuilder::hasAlignmentAttr() const {
Bill Wendlingcd330342013-01-04 23:27:34 +00001528 return Alignment != 0;
Bill Wendlingc6daefa2012-10-08 23:27:46 +00001529}
1530
Bill Wendlingd509a662013-01-29 00:34:06 +00001531bool AttrBuilder::operator==(const AttrBuilder &B) {
Benjamin Kramer45e7d532013-02-16 19:13:18 +00001532 if (Attrs != B.Attrs)
1533 return false;
Bill Wendling4b001442013-02-06 01:33:42 +00001534
1535 for (td_const_iterator I = TargetDepAttrs.begin(),
1536 E = TargetDepAttrs.end(); I != E; ++I)
1537 if (B.TargetDepAttrs.find(I->first) == B.TargetDepAttrs.end())
1538 return false;
1539
Hal Finkelb0407ba2014-07-18 15:51:28 +00001540 return Alignment == B.Alignment && StackAlignment == B.StackAlignment &&
1541 DerefBytes == B.DerefBytes;
Bill Wendlingd509a662013-01-29 00:34:06 +00001542}
1543
Bill Wendling57625a42013-01-25 23:09:36 +00001544//===----------------------------------------------------------------------===//
1545// AttributeFuncs Function Defintions
1546//===----------------------------------------------------------------------===//
1547
Bill Wendlingc79cdff2013-02-01 01:04:27 +00001548/// \brief Which attributes cannot be applied to a type.
Craig Toppere3dcce92015-08-01 22:20:21 +00001549AttrBuilder AttributeFuncs::typeIncompatible(Type *Ty) {
Bill Wendling57625a42013-01-25 23:09:36 +00001550 AttrBuilder Incompatible;
1551
1552 if (!Ty->isIntegerTy())
1553 // Attribute that only apply to integers.
1554 Incompatible.addAttribute(Attribute::SExt)
1555 .addAttribute(Attribute::ZExt);
1556
1557 if (!Ty->isPointerTy())
1558 // Attribute that only apply to pointers.
1559 Incompatible.addAttribute(Attribute::ByVal)
1560 .addAttribute(Attribute::Nest)
1561 .addAttribute(Attribute::NoAlias)
1562 .addAttribute(Attribute::NoCapture)
Nick Lewyckyd52b1522014-05-20 01:23:40 +00001563 .addAttribute(Attribute::NonNull)
Hal Finkelb0407ba2014-07-18 15:51:28 +00001564 .addDereferenceableAttr(1) // the int here is ignored
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001565 .addDereferenceableOrNullAttr(1) // the int here is ignored
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001566 .addAttribute(Attribute::ReadNone)
1567 .addAttribute(Attribute::ReadOnly)
Reid Klecknera534a382013-12-19 02:14:12 +00001568 .addAttribute(Attribute::StructRet)
1569 .addAttribute(Attribute::InAlloca);
Bill Wendling57625a42013-01-25 23:09:36 +00001570
Pete Cooper2777d8872015-05-06 23:19:56 +00001571 return Incompatible;
Bill Wendling57625a42013-01-25 23:09:36 +00001572}
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001573
1574template<typename AttrClass>
1575static bool isEqual(const Function &Caller, const Function &Callee) {
1576 return Caller.getFnAttribute(AttrClass::getKind()) ==
1577 Callee.getFnAttribute(AttrClass::getKind());
1578}
1579
1580/// \brief Compute the logical AND of the attributes of the caller and the
1581/// callee.
1582///
1583/// This function sets the caller's attribute to false if the callee's attribute
1584/// is false.
1585template<typename AttrClass>
1586static void setAND(Function &Caller, const Function &Callee) {
1587 if (AttrClass::isSet(Caller, AttrClass::getKind()) &&
1588 !AttrClass::isSet(Callee, AttrClass::getKind()))
1589 AttrClass::set(Caller, AttrClass::getKind(), false);
1590}
1591
1592/// \brief Compute the logical OR of the attributes of the caller and the
1593/// callee.
1594///
1595/// This function sets the caller's attribute to true if the callee's attribute
1596/// is true.
1597template<typename AttrClass>
1598static void setOR(Function &Caller, const Function &Callee) {
1599 if (!AttrClass::isSet(Caller, AttrClass::getKind()) &&
1600 AttrClass::isSet(Callee, AttrClass::getKind()))
1601 AttrClass::set(Caller, AttrClass::getKind(), true);
1602}
1603
1604/// \brief If the inlined function had a higher stack protection level than the
1605/// calling function, then bump up the caller's stack protection level.
1606static void adjustCallerSSPLevel(Function &Caller, const Function &Callee) {
1607 // If upgrading the SSP attribute, clear out the old SSP Attributes first.
1608 // Having multiple SSP attributes doesn't actually hurt, but it adds useless
1609 // clutter to the IR.
Reid Kleckneree4930b2017-05-02 22:07:37 +00001610 AttrBuilder OldSSPAttr;
1611 OldSSPAttr.addAttribute(Attribute::StackProtect)
1612 .addAttribute(Attribute::StackProtectStrong)
1613 .addAttribute(Attribute::StackProtectReq);
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001614
Evgeniy Stepanovf17120a2016-04-11 22:27:48 +00001615 if (Callee.hasFnAttribute(Attribute::StackProtectReq)) {
Reid Klecknerb5180542017-03-21 16:57:19 +00001616 Caller.removeAttributes(AttributeList::FunctionIndex, OldSSPAttr);
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001617 Caller.addFnAttr(Attribute::StackProtectReq);
1618 } else if (Callee.hasFnAttribute(Attribute::StackProtectStrong) &&
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001619 !Caller.hasFnAttribute(Attribute::StackProtectReq)) {
Reid Klecknerb5180542017-03-21 16:57:19 +00001620 Caller.removeAttributes(AttributeList::FunctionIndex, OldSSPAttr);
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001621 Caller.addFnAttr(Attribute::StackProtectStrong);
1622 } else if (Callee.hasFnAttribute(Attribute::StackProtect) &&
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001623 !Caller.hasFnAttribute(Attribute::StackProtectReq) &&
1624 !Caller.hasFnAttribute(Attribute::StackProtectStrong))
1625 Caller.addFnAttr(Attribute::StackProtect);
1626}
1627
1628#define GET_ATTR_COMPAT_FUNC
1629#include "AttributesCompatFunc.inc"
1630
1631bool AttributeFuncs::areInlineCompatible(const Function &Caller,
1632 const Function &Callee) {
1633 return hasCompatibleFnAttrs(Caller, Callee);
1634}
1635
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001636void AttributeFuncs::mergeAttributesForInlining(Function &Caller,
1637 const Function &Callee) {
1638 mergeFnAttrs(Caller, Callee);
1639}