blob: 3d4245983d0ef262d55d6aab584da9ce26005c86 [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,
Bill Wendling6848e382012-12-19 22:42:22 +000012// AttributeSetImpl, and AttributeSet classes.
Chris Lattner3e13b8c2008-01-02 23:42:30 +000013//
14//===----------------------------------------------------------------------===//
15
Bill Wendling4607f4b2012-12-20 01:36:59 +000016#include "AttributeImpl.h"
Eugene Zelenkodeaf6952017-02-17 00:00:09 +000017#include "AttributeSetNode.h"
Bill Wendlinge38b8042012-09-26 21:07:29 +000018#include "LLVMContextImpl.h"
Eugene Zelenkodeaf6952017-02-17 00:00:09 +000019#include "llvm/ADT/ArrayRef.h"
20#include "llvm/ADT/FoldingSet.h"
21#include "llvm/ADT/Optional.h"
22#include "llvm/ADT/SmallVector.h"
Benjamin Kramer502b9e12014-04-12 16:15:53 +000023#include "llvm/ADT/STLExtras.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000024#include "llvm/ADT/StringExtras.h"
Eugene Zelenkodeaf6952017-02-17 00:00:09 +000025#include "llvm/ADT/StringRef.h"
26#include "llvm/ADT/Twine.h"
27#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
498AttributeSetNode *AttributeSetNode::get(LLVMContext &C,
499 ArrayRef<Attribute> Attrs) {
500 if (Attrs.empty())
Craig Topperc6207612014-04-09 06:08:46 +0000501 return nullptr;
Bill Wendlingd509a662013-01-29 00:34:06 +0000502
503 // Otherwise, build a key to look up the existing attributes.
504 LLVMContextImpl *pImpl = C.pImpl;
505 FoldingSetNodeID ID;
506
507 SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end());
Reid Kleckner7de67612016-04-04 23:06:05 +0000508 std::sort(SortedAttrs.begin(), SortedAttrs.end());
Bill Wendlingd509a662013-01-29 00:34:06 +0000509
George Burgess IV500d3032015-12-16 05:21:02 +0000510 for (Attribute Attr : SortedAttrs)
511 Attr.Profile(ID);
Bill Wendlingd509a662013-01-29 00:34:06 +0000512
513 void *InsertPoint;
514 AttributeSetNode *PA =
515 pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint);
516
517 // If we didn't find any existing attributes of the same shape then create a
518 // new one and insert it.
519 if (!PA) {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000520 // Coallocate entries after the AttributeSetNode itself.
James Y Knightaa365b22015-08-05 22:57:34 +0000521 void *Mem = ::operator new(totalSizeToAlloc<Attribute>(SortedAttrs.size()));
Benjamin Kramer741146b2013-07-11 12:13:16 +0000522 PA = new (Mem) AttributeSetNode(SortedAttrs);
Bill Wendlingd509a662013-01-29 00:34:06 +0000523 pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint);
524 }
525
526 // Return the AttributesListNode that we found or created.
527 return PA;
528}
529
Bill Wendlingbce7b972013-02-13 08:42:21 +0000530bool AttributeSetNode::hasAttribute(StringRef Kind) const {
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000531 for (Attribute I : *this)
532 if (I.hasAttribute(Kind))
Bill Wendlingbce7b972013-02-13 08:42:21 +0000533 return true;
534 return false;
535}
536
537Attribute AttributeSetNode::getAttribute(Attribute::AttrKind Kind) const {
Matthias Braun31eeb76f2016-01-29 22:25:13 +0000538 if (hasAttribute(Kind)) {
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000539 for (Attribute I : *this)
540 if (I.hasAttribute(Kind))
541 return I;
Matthias Braun31eeb76f2016-01-29 22:25:13 +0000542 }
Bill Wendlingbce7b972013-02-13 08:42:21 +0000543 return Attribute();
544}
545
546Attribute AttributeSetNode::getAttribute(StringRef Kind) const {
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000547 for (Attribute I : *this)
548 if (I.hasAttribute(Kind))
549 return I;
Bill Wendlingbce7b972013-02-13 08:42:21 +0000550 return Attribute();
551}
552
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000553unsigned AttributeSetNode::getAlignment() const {
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000554 for (Attribute I : *this)
555 if (I.hasAttribute(Attribute::Alignment))
556 return I.getAlignment();
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000557 return 0;
558}
559
560unsigned AttributeSetNode::getStackAlignment() const {
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000561 for (Attribute I : *this)
562 if (I.hasAttribute(Attribute::StackAlignment))
563 return I.getStackAlignment();
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000564 return 0;
565}
566
Hal Finkelb0407ba2014-07-18 15:51:28 +0000567uint64_t AttributeSetNode::getDereferenceableBytes() const {
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000568 for (Attribute I : *this)
569 if (I.hasAttribute(Attribute::Dereferenceable))
570 return I.getDereferenceableBytes();
Hal Finkelb0407ba2014-07-18 15:51:28 +0000571 return 0;
572}
573
Sanjoy Das06cf33f2015-05-06 17:41:54 +0000574uint64_t AttributeSetNode::getDereferenceableOrNullBytes() const {
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000575 for (Attribute I : *this)
576 if (I.hasAttribute(Attribute::DereferenceableOrNull))
577 return I.getDereferenceableOrNullBytes();
Sanjoy Das06cf33f2015-05-06 17:41:54 +0000578 return 0;
579}
580
George Burgess IV278199f2016-04-12 01:05:35 +0000581std::pair<unsigned, Optional<unsigned>>
582AttributeSetNode::getAllocSizeArgs() const {
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000583 for (Attribute I : *this)
584 if (I.hasAttribute(Attribute::AllocSize))
585 return I.getAllocSizeArgs();
George Burgess IV278199f2016-04-12 01:05:35 +0000586 return std::make_pair(0, 0);
587}
588
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +0000589std::string AttributeSetNode::getAsString(bool InAttrGrp) const {
Benjamin Kramer0baf8f42013-04-19 11:43:21 +0000590 std::string Str;
Benjamin Kramer741146b2013-07-11 12:13:16 +0000591 for (iterator I = begin(), E = end(); I != E; ++I) {
592 if (I != begin())
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +0000593 Str += ' ';
594 Str += I->getAsString(InAttrGrp);
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000595 }
596 return Str;
597}
598
Bill Wendlingd509a662013-01-29 00:34:06 +0000599//===----------------------------------------------------------------------===//
600// AttributeSetImpl Definition
601//===----------------------------------------------------------------------===//
602
Matthias Braun8c209aa2017-01-28 02:02:38 +0000603#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Yaron Kereneb2a2542016-01-29 20:50:44 +0000604LLVM_DUMP_METHOD void AttributeSetImpl::dump() const {
Peter Collingbourneabca2ec2013-08-02 22:34:30 +0000605 AttributeSet(const_cast<AttributeSetImpl *>(this)).dump();
606}
Matthias Braun8c209aa2017-01-28 02:02:38 +0000607#endif
Peter Collingbourneabca2ec2013-08-02 22:34:30 +0000608
Bill Wendlingd509a662013-01-29 00:34:06 +0000609//===----------------------------------------------------------------------===//
610// AttributeSet Construction and Mutation Methods
611//===----------------------------------------------------------------------===//
612
Bill Wendling60011b82013-01-29 01:43:29 +0000613AttributeSet
614AttributeSet::getImpl(LLVMContext &C,
Eugene Zelenkodeaf6952017-02-17 00:00:09 +0000615 ArrayRef<std::pair<unsigned, AttributeSetNode*>> Attrs) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000616 LLVMContextImpl *pImpl = C.pImpl;
617 FoldingSetNodeID ID;
618 AttributeSetImpl::Profile(ID, Attrs);
619
620 void *InsertPoint;
621 AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
622
623 // If we didn't find any existing attributes of the same shape then
624 // create a new one and insert it.
625 if (!PA) {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000626 // Coallocate entries after the AttributeSetImpl itself.
James Y Knightaa365b22015-08-05 22:57:34 +0000627 void *Mem = ::operator new(
628 AttributeSetImpl::totalSizeToAlloc<IndexAttrPair>(Attrs.size()));
Benjamin Kramer741146b2013-07-11 12:13:16 +0000629 PA = new (Mem) AttributeSetImpl(C, Attrs);
Bill Wendlingd509a662013-01-29 00:34:06 +0000630 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
631 }
632
633 // Return the AttributesList that we found or created.
634 return AttributeSet(PA);
635}
636
637AttributeSet AttributeSet::get(LLVMContext &C,
Eugene Zelenkodeaf6952017-02-17 00:00:09 +0000638 ArrayRef<std::pair<unsigned, Attribute>> Attrs) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000639 // If there are no attributes then return a null AttributesList pointer.
640 if (Attrs.empty())
641 return AttributeSet();
642
Craig Toppere30b8ca2016-01-03 19:43:40 +0000643 assert(std::is_sorted(Attrs.begin(), Attrs.end(),
644 [](const std::pair<unsigned, Attribute> &LHS,
645 const std::pair<unsigned, Attribute> &RHS) {
646 return LHS.first < RHS.first;
647 }) && "Misordered Attributes list!");
David Majnemer0a16c222016-08-11 21:15:00 +0000648 assert(none_of(Attrs,
649 [](const std::pair<unsigned, Attribute> &Pair) {
650 return Pair.second.hasAttribute(Attribute::None);
651 }) &&
652 "Pointless attribute!");
Bill Wendlingd509a662013-01-29 00:34:06 +0000653
654 // Create a vector if (unsigned, AttributeSetNode*) pairs from the attributes
655 // list.
656 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrPairVec;
Eugene Zelenkodeaf6952017-02-17 00:00:09 +0000657 for (ArrayRef<std::pair<unsigned, Attribute>>::iterator I = Attrs.begin(),
Bill Wendlingd509a662013-01-29 00:34:06 +0000658 E = Attrs.end(); I != E; ) {
659 unsigned Index = I->first;
660 SmallVector<Attribute, 4> AttrVec;
NAKAMURA Takumif05d2f22013-01-29 15:18:16 +0000661 while (I != E && I->first == Index) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000662 AttrVec.push_back(I->second);
663 ++I;
664 }
665
David Majnemer0a16c222016-08-11 21:15:00 +0000666 AttrPairVec.emplace_back(Index, AttributeSetNode::get(C, AttrVec));
Bill Wendlingd509a662013-01-29 00:34:06 +0000667 }
668
669 return getImpl(C, AttrPairVec);
670}
671
672AttributeSet AttributeSet::get(LLVMContext &C,
673 ArrayRef<std::pair<unsigned,
Eugene Zelenkodeaf6952017-02-17 00:00:09 +0000674 AttributeSetNode*>> Attrs) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000675 // If there are no attributes then return a null AttributesList pointer.
676 if (Attrs.empty())
677 return AttributeSet();
678
679 return getImpl(C, Attrs);
680}
681
David Majnemercf63a792014-05-03 23:00:35 +0000682AttributeSet AttributeSet::get(LLVMContext &C, unsigned Index,
683 const AttrBuilder &B) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000684 if (!B.hasAttributes())
685 return AttributeSet();
Bill Wendlingf7134812013-01-29 01:02:03 +0000686
Bill Wendlingae89a0f2013-02-05 23:48:36 +0000687 // Add target-independent attributes.
Bill Wendlingf7134812013-01-29 01:02:03 +0000688 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
Benjamin Kramer45e7d532013-02-16 19:13:18 +0000689 for (Attribute::AttrKind Kind = Attribute::None;
Benjamin Kramer6f37dac2013-02-16 19:22:28 +0000690 Kind != Attribute::EndAttrKinds; Kind = Attribute::AttrKind(Kind + 1)) {
Benjamin Kramer45e7d532013-02-16 19:13:18 +0000691 if (!B.contains(Kind))
692 continue;
693
George Burgess IV500d3032015-12-16 05:21:02 +0000694 Attribute Attr;
695 switch (Kind) {
696 case Attribute::Alignment:
697 Attr = Attribute::getWithAlignment(C, B.getAlignment());
698 break;
699 case Attribute::StackAlignment:
700 Attr = Attribute::getWithStackAlignment(C, B.getStackAlignment());
701 break;
702 case Attribute::Dereferenceable:
703 Attr = Attribute::getWithDereferenceableBytes(
704 C, B.getDereferenceableBytes());
705 break;
706 case Attribute::DereferenceableOrNull:
707 Attr = Attribute::getWithDereferenceableOrNullBytes(
708 C, B.getDereferenceableOrNullBytes());
709 break;
George Burgess IV278199f2016-04-12 01:05:35 +0000710 case Attribute::AllocSize: {
711 auto A = B.getAllocSizeArgs();
712 Attr = Attribute::getWithAllocSizeArgs(C, A.first, A.second);
713 break;
714 }
George Burgess IV500d3032015-12-16 05:21:02 +0000715 default:
716 Attr = Attribute::get(C, Kind);
717 }
David Majnemer0a16c222016-08-11 21:15:00 +0000718 Attrs.emplace_back(Index, Attr);
Bill Wendlingf7134812013-01-29 01:02:03 +0000719 }
720
Bill Wendlingae89a0f2013-02-05 23:48:36 +0000721 // Add target-dependent (string) attributes.
Benjamin Kramerea76b6f2016-06-12 19:02:34 +0000722 for (const auto &TDA : B.td_attrs())
David Majnemer0a16c222016-08-11 21:15:00 +0000723 Attrs.emplace_back(Index, Attribute::get(C, TDA.first, TDA.second));
Bill Wendlingae89a0f2013-02-05 23:48:36 +0000724
Bill Wendlingf7134812013-01-29 01:02:03 +0000725 return get(C, Attrs);
Bill Wendlingd509a662013-01-29 00:34:06 +0000726}
727
Bill Wendling211316c2013-04-18 20:17:28 +0000728AttributeSet AttributeSet::get(LLVMContext &C, unsigned Index,
Amaury Sechet392638d2016-06-14 20:27:35 +0000729 ArrayRef<Attribute::AttrKind> Kinds) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000730 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
Amaury Sechet392638d2016-06-14 20:27:35 +0000731 for (Attribute::AttrKind K : Kinds)
David Majnemer0a16c222016-08-11 21:15:00 +0000732 Attrs.emplace_back(Index, Attribute::get(C, K));
Bill Wendlingd509a662013-01-29 00:34:06 +0000733 return get(C, Attrs);
734}
735
Amaury Sechet6100adf2016-06-15 17:50:39 +0000736AttributeSet AttributeSet::get(LLVMContext &C, unsigned Index,
737 ArrayRef<StringRef> Kinds) {
738 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
739 for (StringRef K : Kinds)
David Majnemer0a16c222016-08-11 21:15:00 +0000740 Attrs.emplace_back(Index, Attribute::get(C, K));
Amaury Sechet6100adf2016-06-15 17:50:39 +0000741 return get(C, Attrs);
742}
743
Bill Wendlingd509a662013-01-29 00:34:06 +0000744AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<AttributeSet> Attrs) {
745 if (Attrs.empty()) return AttributeSet();
Peter Collingbournebd6c7452013-08-02 22:29:40 +0000746 if (Attrs.size() == 1) return Attrs[0];
Bill Wendlingd509a662013-01-29 00:34:06 +0000747
748 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrNodeVec;
Peter Collingbournebd6c7452013-08-02 22:29:40 +0000749 AttributeSetImpl *A0 = Attrs[0].pImpl;
750 if (A0)
Amaury Sechet24c84fd2016-06-14 22:04:16 +0000751 AttrNodeVec.append(A0->getNode(0), A0->getNode(A0->getNumSlots()));
Peter Collingbournebd6c7452013-08-02 22:29:40 +0000752 // Copy all attributes from Attrs into AttrNodeVec while keeping AttrNodeVec
753 // ordered by index. Because we know that each list in Attrs is ordered by
754 // index we only need to merge each successive list in rather than doing a
755 // full sort.
756 for (unsigned I = 1, E = Attrs.size(); I != E; ++I) {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000757 AttributeSetImpl *AS = Attrs[I].pImpl;
758 if (!AS) continue;
Peter Collingbournebd6c7452013-08-02 22:29:40 +0000759 SmallVector<std::pair<unsigned, AttributeSetNode *>, 8>::iterator
760 ANVI = AttrNodeVec.begin(), ANVE;
James Y Knightaa365b22015-08-05 22:57:34 +0000761 for (const IndexAttrPair *AI = AS->getNode(0),
Amaury Sechet24c84fd2016-06-14 22:04:16 +0000762 *AE = AS->getNode(AS->getNumSlots());
Peter Collingbournebd6c7452013-08-02 22:29:40 +0000763 AI != AE; ++AI) {
764 ANVE = AttrNodeVec.end();
765 while (ANVI != ANVE && ANVI->first <= AI->first)
766 ++ANVI;
767 ANVI = AttrNodeVec.insert(ANVI, *AI) + 1;
768 }
Bill Wendlingd509a662013-01-29 00:34:06 +0000769 }
770
771 return getImpl(C, AttrNodeVec);
772}
773
Bill Wendling211316c2013-04-18 20:17:28 +0000774AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
Amaury Sechet392638d2016-06-14 20:27:35 +0000775 Attribute::AttrKind Kind) const {
776 if (hasAttribute(Index, Kind)) return *this;
777 return addAttributes(C, Index, AttributeSet::get(C, Index, Kind));
Reed Kotler795c7b42013-03-13 20:20:08 +0000778}
779
Bill Wendling3b2f6102013-07-25 18:34:24 +0000780AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
781 StringRef Kind, StringRef Value) const {
Eugene Zelenkodeaf6952017-02-17 00:00:09 +0000782 AttrBuilder B;
Bill Wendling3b2f6102013-07-25 18:34:24 +0000783 B.addAttribute(Kind, Value);
784 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
785}
786
Akira Hatanaka237916b2015-12-02 06:58:49 +0000787AttributeSet AttributeSet::addAttribute(LLVMContext &C,
788 ArrayRef<unsigned> Indices,
789 Attribute A) const {
Amaury Sechet24c84fd2016-06-14 22:04:16 +0000790 unsigned I = 0, E = pImpl ? pImpl->getNumSlots() : 0;
Akira Hatanaka237916b2015-12-02 06:58:49 +0000791 auto IdxI = Indices.begin(), IdxE = Indices.end();
792 SmallVector<AttributeSet, 4> AttrSet;
793
794 while (I != E && IdxI != IdxE) {
795 if (getSlotIndex(I) < *IdxI)
796 AttrSet.emplace_back(getSlotAttributes(I++));
797 else if (getSlotIndex(I) > *IdxI)
798 AttrSet.emplace_back(AttributeSet::get(C, std::make_pair(*IdxI++, A)));
799 else {
800 AttrBuilder B(getSlotAttributes(I), *IdxI);
801 B.addAttribute(A);
802 AttrSet.emplace_back(AttributeSet::get(C, *IdxI, B));
803 ++I;
804 ++IdxI;
805 }
806 }
807
808 while (I != E)
809 AttrSet.emplace_back(getSlotAttributes(I++));
810
811 while (IdxI != IdxE)
812 AttrSet.emplace_back(AttributeSet::get(C, std::make_pair(*IdxI++, A)));
813
814 return get(C, AttrSet);
815}
816
Bill Wendling211316c2013-04-18 20:17:28 +0000817AttributeSet AttributeSet::addAttributes(LLVMContext &C, unsigned Index,
Bill Wendlingd509a662013-01-29 00:34:06 +0000818 AttributeSet Attrs) const {
819 if (!pImpl) return Attrs;
820 if (!Attrs.pImpl) return *this;
821
822#ifndef NDEBUG
823 // FIXME it is not obvious how this should work for alignment. For now, say
824 // we can't change a known alignment.
Bill Wendling211316c2013-04-18 20:17:28 +0000825 unsigned OldAlign = getParamAlignment(Index);
826 unsigned NewAlign = Attrs.getParamAlignment(Index);
Bill Wendlingd509a662013-01-29 00:34:06 +0000827 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
828 "Attempt to change alignment!");
829#endif
830
831 // Add the attribute slots before the one we're trying to add.
832 SmallVector<AttributeSet, 4> AttrSet;
Amaury Sechet24c84fd2016-06-14 22:04:16 +0000833 uint64_t NumAttrs = pImpl->getNumSlots();
Bill Wendlingd509a662013-01-29 00:34:06 +0000834 AttributeSet AS;
835 uint64_t LastIndex = 0;
836 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
Bill Wendling211316c2013-04-18 20:17:28 +0000837 if (getSlotIndex(I) >= Index) {
838 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
Bill Wendlingd509a662013-01-29 00:34:06 +0000839 break;
840 }
841 LastIndex = I + 1;
842 AttrSet.push_back(getSlotAttributes(I));
843 }
844
845 // Now add the attribute into the correct slot. There may already be an
846 // AttributeSet there.
Bill Wendling211316c2013-04-18 20:17:28 +0000847 AttrBuilder B(AS, Index);
Bill Wendlingd509a662013-01-29 00:34:06 +0000848
Amaury Sechet24c84fd2016-06-14 22:04:16 +0000849 for (unsigned I = 0, E = Attrs.pImpl->getNumSlots(); I != E; ++I)
Bill Wendling211316c2013-04-18 20:17:28 +0000850 if (Attrs.getSlotIndex(I) == Index) {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000851 for (AttributeSetImpl::iterator II = Attrs.pImpl->begin(I),
Bill Wendlingd509a662013-01-29 00:34:06 +0000852 IE = Attrs.pImpl->end(I); II != IE; ++II)
Bill Wendling23804da2013-01-31 23:38:01 +0000853 B.addAttribute(*II);
Bill Wendlingd509a662013-01-29 00:34:06 +0000854 break;
855 }
856
Bill Wendling211316c2013-04-18 20:17:28 +0000857 AttrSet.push_back(AttributeSet::get(C, Index, B));
Bill Wendlingd509a662013-01-29 00:34:06 +0000858
859 // Add the remaining attribute slots.
860 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
861 AttrSet.push_back(getSlotAttributes(I));
862
863 return get(C, AttrSet);
864}
865
Bill Wendling211316c2013-04-18 20:17:28 +0000866AttributeSet AttributeSet::removeAttribute(LLVMContext &C, unsigned Index,
Amaury Sechet392638d2016-06-14 20:27:35 +0000867 Attribute::AttrKind Kind) const {
868 if (!hasAttribute(Index, Kind)) return *this;
869 return removeAttributes(C, Index, AttributeSet::get(C, Index, Kind));
Bill Wendlingd509a662013-01-29 00:34:06 +0000870}
871
Amaury Sechet6100adf2016-06-15 17:50:39 +0000872AttributeSet AttributeSet::removeAttribute(LLVMContext &C, unsigned Index,
873 StringRef Kind) const {
874 if (!hasAttribute(Index, Kind)) return *this;
875 return removeAttributes(C, Index, AttributeSet::get(C, Index, Kind));
876}
877
Bill Wendling211316c2013-04-18 20:17:28 +0000878AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Index,
Bill Wendlingd509a662013-01-29 00:34:06 +0000879 AttributeSet Attrs) const {
880 if (!pImpl) return AttributeSet();
881 if (!Attrs.pImpl) return *this;
882
Pete Cooper67cf9a72015-11-19 05:56:52 +0000883 // FIXME it is not obvious how this should work for alignment.
884 // For now, say we can't pass in alignment, which no current use does.
885 assert(!Attrs.hasAttribute(Index, Attribute::Alignment) &&
886 "Attempt to change alignment!");
887
Bill Wendlingd509a662013-01-29 00:34:06 +0000888 // Add the attribute slots before the one we're trying to add.
889 SmallVector<AttributeSet, 4> AttrSet;
Amaury Sechet24c84fd2016-06-14 22:04:16 +0000890 uint64_t NumAttrs = pImpl->getNumSlots();
Bill Wendlingd509a662013-01-29 00:34:06 +0000891 AttributeSet AS;
892 uint64_t LastIndex = 0;
893 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
Bill Wendling211316c2013-04-18 20:17:28 +0000894 if (getSlotIndex(I) >= Index) {
895 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
Bill Wendlingd509a662013-01-29 00:34:06 +0000896 break;
897 }
898 LastIndex = I + 1;
899 AttrSet.push_back(getSlotAttributes(I));
900 }
901
Bill Wendlingd2196752013-01-30 23:07:40 +0000902 // Now remove the attribute from the correct slot. There may already be an
Bill Wendlingd509a662013-01-29 00:34:06 +0000903 // AttributeSet there.
Bill Wendling211316c2013-04-18 20:17:28 +0000904 AttrBuilder B(AS, Index);
Bill Wendlingd509a662013-01-29 00:34:06 +0000905
Amaury Sechet24c84fd2016-06-14 22:04:16 +0000906 for (unsigned I = 0, E = Attrs.pImpl->getNumSlots(); I != E; ++I)
Bill Wendling211316c2013-04-18 20:17:28 +0000907 if (Attrs.getSlotIndex(I) == Index) {
908 B.removeAttributes(Attrs.pImpl->getSlotAttributes(I), Index);
Bill Wendlingd509a662013-01-29 00:34:06 +0000909 break;
910 }
911
Bill Wendling211316c2013-04-18 20:17:28 +0000912 AttrSet.push_back(AttributeSet::get(C, Index, B));
Bill Wendlingd509a662013-01-29 00:34:06 +0000913
914 // Add the remaining attribute slots.
915 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
916 AttrSet.push_back(getSlotAttributes(I));
917
918 return get(C, AttrSet);
919}
920
Pete Cooperd2a44612015-05-06 23:19:43 +0000921AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Index,
922 const AttrBuilder &Attrs) const {
923 if (!pImpl) return AttributeSet();
924
925 // FIXME it is not obvious how this should work for alignment.
926 // For now, say we can't pass in alignment, which no current use does.
927 assert(!Attrs.hasAlignmentAttr() && "Attempt to change alignment!");
928
929 // Add the attribute slots before the one we're trying to add.
930 SmallVector<AttributeSet, 4> AttrSet;
Amaury Sechet24c84fd2016-06-14 22:04:16 +0000931 uint64_t NumAttrs = pImpl->getNumSlots();
Pete Cooperd2a44612015-05-06 23:19:43 +0000932 AttributeSet AS;
933 uint64_t LastIndex = 0;
934 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
935 if (getSlotIndex(I) >= Index) {
936 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
937 break;
938 }
939 LastIndex = I + 1;
940 AttrSet.push_back(getSlotAttributes(I));
941 }
942
943 // Now remove the attribute from the correct slot. There may already be an
944 // AttributeSet there.
945 AttrBuilder B(AS, Index);
946 B.remove(Attrs);
947
948 AttrSet.push_back(AttributeSet::get(C, Index, B));
949
950 // Add the remaining attribute slots.
951 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
952 AttrSet.push_back(getSlotAttributes(I));
953
954 return get(C, AttrSet);
955}
956
Ramkumar Ramachandra8fcb4982015-02-14 19:37:54 +0000957AttributeSet AttributeSet::addDereferenceableAttr(LLVMContext &C, unsigned Index,
958 uint64_t Bytes) const {
Eugene Zelenkodeaf6952017-02-17 00:00:09 +0000959 AttrBuilder B;
Ramkumar Ramachandra8fcb4982015-02-14 19:37:54 +0000960 B.addDereferenceableAttr(Bytes);
961 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
962}
963
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000964AttributeSet AttributeSet::addDereferenceableOrNullAttr(LLVMContext &C,
965 unsigned Index,
966 uint64_t Bytes) const {
Eugene Zelenkodeaf6952017-02-17 00:00:09 +0000967 AttrBuilder B;
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000968 B.addDereferenceableOrNullAttr(Bytes);
969 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
970}
971
George Burgess IV278199f2016-04-12 01:05:35 +0000972AttributeSet
973AttributeSet::addAllocSizeAttr(LLVMContext &C, unsigned Index,
974 unsigned ElemSizeArg,
975 const Optional<unsigned> &NumElemsArg) {
Eugene Zelenkodeaf6952017-02-17 00:00:09 +0000976 AttrBuilder B;
George Burgess IV278199f2016-04-12 01:05:35 +0000977 B.addAllocSizeAttr(ElemSizeArg, NumElemsArg);
978 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
979}
980
Bill Wendlingd509a662013-01-29 00:34:06 +0000981//===----------------------------------------------------------------------===//
982// AttributeSet Accessor Methods
983//===----------------------------------------------------------------------===//
984
Bill Wendling5d020a32013-02-10 05:00:40 +0000985LLVMContext &AttributeSet::getContext() const {
986 return pImpl->getContext();
987}
988
Bill Wendling211316c2013-04-18 20:17:28 +0000989AttributeSet AttributeSet::getParamAttributes(unsigned Index) const {
990 return pImpl && hasAttributes(Index) ?
Bill Wendlingd509a662013-01-29 00:34:06 +0000991 AttributeSet::get(pImpl->getContext(),
Eugene Zelenkodeaf6952017-02-17 00:00:09 +0000992 ArrayRef<std::pair<unsigned, AttributeSetNode*>>(
Bill Wendling211316c2013-04-18 20:17:28 +0000993 std::make_pair(Index, getAttributes(Index)))) :
Bill Wendlingd509a662013-01-29 00:34:06 +0000994 AttributeSet();
995}
996
997AttributeSet AttributeSet::getRetAttributes() const {
998 return pImpl && hasAttributes(ReturnIndex) ?
999 AttributeSet::get(pImpl->getContext(),
Eugene Zelenkodeaf6952017-02-17 00:00:09 +00001000 ArrayRef<std::pair<unsigned, AttributeSetNode*>>(
Bill Wendlingd509a662013-01-29 00:34:06 +00001001 std::make_pair(ReturnIndex,
1002 getAttributes(ReturnIndex)))) :
1003 AttributeSet();
1004}
1005
1006AttributeSet AttributeSet::getFnAttributes() const {
1007 return pImpl && hasAttributes(FunctionIndex) ?
1008 AttributeSet::get(pImpl->getContext(),
Eugene Zelenkodeaf6952017-02-17 00:00:09 +00001009 ArrayRef<std::pair<unsigned, AttributeSetNode*>>(
Bill Wendlingd509a662013-01-29 00:34:06 +00001010 std::make_pair(FunctionIndex,
1011 getAttributes(FunctionIndex)))) :
1012 AttributeSet();
1013}
1014
1015bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{
Bill Wendlingf2955aa2013-01-29 03:20:31 +00001016 AttributeSetNode *ASN = getAttributes(Index);
George Burgess IV500d3032015-12-16 05:21:02 +00001017 return ASN && ASN->hasAttribute(Kind);
Bill Wendlingd509a662013-01-29 00:34:06 +00001018}
1019
Bill Wendlingbce7b972013-02-13 08:42:21 +00001020bool AttributeSet::hasAttribute(unsigned Index, StringRef Kind) const {
1021 AttributeSetNode *ASN = getAttributes(Index);
George Burgess IV500d3032015-12-16 05:21:02 +00001022 return ASN && ASN->hasAttribute(Kind);
Bill Wendlingbce7b972013-02-13 08:42:21 +00001023}
1024
Bill Wendlingd509a662013-01-29 00:34:06 +00001025bool AttributeSet::hasAttributes(unsigned Index) const {
Bill Wendlingf2955aa2013-01-29 03:20:31 +00001026 AttributeSetNode *ASN = getAttributes(Index);
George Burgess IV500d3032015-12-16 05:21:02 +00001027 return ASN && ASN->hasAttributes();
Bill Wendlingd509a662013-01-29 00:34:06 +00001028}
1029
Matthias Braun33282812016-01-29 22:25:19 +00001030bool AttributeSet::hasFnAttribute(Attribute::AttrKind Kind) const {
1031 return pImpl && pImpl->hasFnAttribute(Kind);
1032}
1033
Amaury Sechet5f04d812016-09-09 04:50:38 +00001034bool AttributeSet::hasFnAttribute(StringRef Kind) const {
1035 return hasAttribute(AttributeSet::FunctionIndex, Kind);
1036}
1037
Hal Finkele87ad542016-07-10 23:01:32 +00001038bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr,
1039 unsigned *Index) const {
Craig Topperc6207612014-04-09 06:08:46 +00001040 if (!pImpl) return false;
Bill Wendlingd509a662013-01-29 00:34:06 +00001041
Amaury Sechet24c84fd2016-06-14 22:04:16 +00001042 for (unsigned I = 0, E = pImpl->getNumSlots(); I != E; ++I)
Benjamin Kramer741146b2013-07-11 12:13:16 +00001043 for (AttributeSetImpl::iterator II = pImpl->begin(I),
Bill Wendlingd509a662013-01-29 00:34:06 +00001044 IE = pImpl->end(I); II != IE; ++II)
Hal Finkele87ad542016-07-10 23:01:32 +00001045 if (II->hasAttribute(Attr)) {
1046 if (Index) *Index = pImpl->getSlotIndex(I);
Bill Wendlingd509a662013-01-29 00:34:06 +00001047 return true;
Hal Finkele87ad542016-07-10 23:01:32 +00001048 }
Bill Wendlingd509a662013-01-29 00:34:06 +00001049
1050 return false;
1051}
1052
Bill Wendlingbce7b972013-02-13 08:42:21 +00001053Attribute AttributeSet::getAttribute(unsigned Index,
1054 Attribute::AttrKind Kind) const {
1055 AttributeSetNode *ASN = getAttributes(Index);
1056 return ASN ? ASN->getAttribute(Kind) : Attribute();
1057}
1058
1059Attribute AttributeSet::getAttribute(unsigned Index,
1060 StringRef Kind) const {
1061 AttributeSetNode *ASN = getAttributes(Index);
1062 return ASN ? ASN->getAttribute(Kind) : Attribute();
1063}
1064
Bill Wendlingf2955aa2013-01-29 03:20:31 +00001065unsigned AttributeSet::getParamAlignment(unsigned Index) const {
1066 AttributeSetNode *ASN = getAttributes(Index);
1067 return ASN ? ASN->getAlignment() : 0;
Bill Wendlingd509a662013-01-29 00:34:06 +00001068}
1069
1070unsigned AttributeSet::getStackAlignment(unsigned Index) const {
Bill Wendlingf2955aa2013-01-29 03:20:31 +00001071 AttributeSetNode *ASN = getAttributes(Index);
1072 return ASN ? ASN->getStackAlignment() : 0;
Bill Wendlingd509a662013-01-29 00:34:06 +00001073}
1074
Hal Finkelb0407ba2014-07-18 15:51:28 +00001075uint64_t AttributeSet::getDereferenceableBytes(unsigned Index) const {
1076 AttributeSetNode *ASN = getAttributes(Index);
1077 return ASN ? ASN->getDereferenceableBytes() : 0;
1078}
1079
Sanjoy Das06cf33f2015-05-06 17:41:54 +00001080uint64_t AttributeSet::getDereferenceableOrNullBytes(unsigned Index) const {
1081 AttributeSetNode *ASN = getAttributes(Index);
1082 return ASN ? ASN->getDereferenceableOrNullBytes() : 0;
1083}
1084
George Burgess IV278199f2016-04-12 01:05:35 +00001085std::pair<unsigned, Optional<unsigned>>
1086AttributeSet::getAllocSizeArgs(unsigned Index) const {
1087 AttributeSetNode *ASN = getAttributes(Index);
Matt Arsenault4ced16d2016-07-18 22:12:46 +00001088 return ASN ? ASN->getAllocSizeArgs() : std::make_pair(0u, Optional<unsigned>(0u));
George Burgess IV278199f2016-04-12 01:05:35 +00001089}
1090
1091std::string AttributeSet::getAsString(unsigned Index, bool InAttrGrp) const {
Bill Wendlingf2955aa2013-01-29 03:20:31 +00001092 AttributeSetNode *ASN = getAttributes(Index);
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +00001093 return ASN ? ASN->getAsString(InAttrGrp) : std::string("");
Bill Wendlingd509a662013-01-29 00:34:06 +00001094}
1095
Bill Wendling211316c2013-04-18 20:17:28 +00001096AttributeSetNode *AttributeSet::getAttributes(unsigned Index) const {
Craig Topperc6207612014-04-09 06:08:46 +00001097 if (!pImpl) return nullptr;
Bill Wendlingd509a662013-01-29 00:34:06 +00001098
Bill Wendlingf2955aa2013-01-29 03:20:31 +00001099 // Loop through to find the attribute node we want.
Amaury Sechet24c84fd2016-06-14 22:04:16 +00001100 for (unsigned I = 0, E = pImpl->getNumSlots(); I != E; ++I)
Bill Wendling211316c2013-04-18 20:17:28 +00001101 if (pImpl->getSlotIndex(I) == Index)
Bill Wendlingf2955aa2013-01-29 03:20:31 +00001102 return pImpl->getSlotNode(I);
Bill Wendlingd509a662013-01-29 00:34:06 +00001103
Craig Topperc6207612014-04-09 06:08:46 +00001104 return nullptr;
Bill Wendlingd509a662013-01-29 00:34:06 +00001105}
1106
Bill Wendling211316c2013-04-18 20:17:28 +00001107AttributeSet::iterator AttributeSet::begin(unsigned Slot) const {
Bill Wendlinga9174862013-01-31 23:53:05 +00001108 if (!pImpl)
1109 return ArrayRef<Attribute>().begin();
Bill Wendling211316c2013-04-18 20:17:28 +00001110 return pImpl->begin(Slot);
Bill Wendlinga9174862013-01-31 23:53:05 +00001111}
1112
Bill Wendling211316c2013-04-18 20:17:28 +00001113AttributeSet::iterator AttributeSet::end(unsigned Slot) const {
Bill Wendlinga9174862013-01-31 23:53:05 +00001114 if (!pImpl)
1115 return ArrayRef<Attribute>().end();
Bill Wendling211316c2013-04-18 20:17:28 +00001116 return pImpl->end(Slot);
Bill Wendlinga9174862013-01-31 23:53:05 +00001117}
1118
Bill Wendlingd509a662013-01-29 00:34:06 +00001119//===----------------------------------------------------------------------===//
1120// AttributeSet Introspection Methods
1121//===----------------------------------------------------------------------===//
1122
Bill Wendlingd509a662013-01-29 00:34:06 +00001123unsigned AttributeSet::getNumSlots() const {
Amaury Sechet24c84fd2016-06-14 22:04:16 +00001124 return pImpl ? pImpl->getNumSlots() : 0;
Bill Wendlingd509a662013-01-29 00:34:06 +00001125}
1126
Rafael Espindoladd275302013-04-30 16:53:38 +00001127unsigned AttributeSet::getSlotIndex(unsigned Slot) const {
Amaury Sechet24c84fd2016-06-14 22:04:16 +00001128 assert(pImpl && Slot < pImpl->getNumSlots() &&
Bill Wendlingd509a662013-01-29 00:34:06 +00001129 "Slot # out of range!");
1130 return pImpl->getSlotIndex(Slot);
1131}
1132
1133AttributeSet AttributeSet::getSlotAttributes(unsigned Slot) const {
Amaury Sechet24c84fd2016-06-14 22:04:16 +00001134 assert(pImpl && Slot < pImpl->getNumSlots() &&
Bill Wendlingd509a662013-01-29 00:34:06 +00001135 "Slot # out of range!");
1136 return pImpl->getSlotAttributes(Slot);
1137}
1138
Matthias Braun8c209aa2017-01-28 02:02:38 +00001139#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Yaron Kereneb2a2542016-01-29 20:50:44 +00001140LLVM_DUMP_METHOD void AttributeSet::dump() const {
Bill Wendlingd509a662013-01-29 00:34:06 +00001141 dbgs() << "PAL[\n";
1142
1143 for (unsigned i = 0, e = getNumSlots(); i < e; ++i) {
1144 uint64_t Index = getSlotIndex(i);
1145 dbgs() << " { ";
1146 if (Index == ~0U)
1147 dbgs() << "~0U";
1148 else
1149 dbgs() << Index;
1150 dbgs() << " => " << getAsString(Index) << " }\n";
1151 }
1152
1153 dbgs() << "]\n";
1154}
Matthias Braun8c209aa2017-01-28 02:02:38 +00001155#endif
Bill Wendlingd509a662013-01-29 00:34:06 +00001156
Bill Wendlingabf3feb2012-10-05 06:44:41 +00001157//===----------------------------------------------------------------------===//
Bill Wendlingcd330342013-01-04 23:27:34 +00001158// AttrBuilder Method Implementations
Bill Wendlingabf3feb2012-10-05 06:44:41 +00001159//===----------------------------------------------------------------------===//
1160
Eugene Zelenkodeaf6952017-02-17 00:00:09 +00001161AttrBuilder::AttrBuilder(AttributeSet AS, unsigned Index) {
Bill Wendling56b0b2a2013-01-27 21:23:46 +00001162 AttributeSetImpl *pImpl = AS.pImpl;
Bill Wendling096f5442013-01-07 08:24:35 +00001163 if (!pImpl) return;
1164
Amaury Sechet24c84fd2016-06-14 22:04:16 +00001165 for (unsigned I = 0, E = pImpl->getNumSlots(); I != E; ++I) {
Bill Wendling211316c2013-04-18 20:17:28 +00001166 if (pImpl->getSlotIndex(I) != Index) continue;
Bill Wendling096f5442013-01-07 08:24:35 +00001167
Benjamin Kramer741146b2013-07-11 12:13:16 +00001168 for (AttributeSetImpl::iterator II = pImpl->begin(I),
Bill Wendling9eb689c2013-01-28 00:21:34 +00001169 IE = pImpl->end(I); II != IE; ++II)
Bill Wendling23804da2013-01-31 23:38:01 +00001170 addAttribute(*II);
Bill Wendling9eb689c2013-01-28 00:21:34 +00001171
1172 break;
1173 }
Bill Wendling096f5442013-01-07 08:24:35 +00001174}
1175
Bill Wendlingcd330342013-01-04 23:27:34 +00001176void AttrBuilder::clear() {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001177 Attrs.reset();
Sanjoy Das88d0fde2015-09-03 22:27:42 +00001178 TargetDepAttrs.clear();
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001179 Alignment = StackAlignment = DerefBytes = DerefOrNullBytes = 0;
George Burgess IV278199f2016-04-12 01:05:35 +00001180 AllocSizeArgs = 0;
Bill Wendlingcd330342013-01-04 23:27:34 +00001181}
1182
1183AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001184 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
Bill Wendling1c7cc8a2013-01-31 23:16:25 +00001185 assert(Val != Attribute::Alignment && Val != Attribute::StackAlignment &&
George Burgess IV278199f2016-04-12 01:05:35 +00001186 Val != Attribute::Dereferenceable && Val != Attribute::AllocSize &&
Hal Finkelb0407ba2014-07-18 15:51:28 +00001187 "Adding integer attribute without adding a value!");
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001188 Attrs[Val] = true;
Bill Wendling7c04e042012-10-09 19:01:18 +00001189 return *this;
Bill Wendlingabf3feb2012-10-05 06:44:41 +00001190}
1191
Bill Wendling23804da2013-01-31 23:38:01 +00001192AttrBuilder &AttrBuilder::addAttribute(Attribute Attr) {
Bill Wendling0a437302013-02-10 10:13:23 +00001193 if (Attr.isStringAttribute()) {
1194 addAttribute(Attr.getKindAsString(), Attr.getValueAsString());
1195 return *this;
1196 }
1197
Bill Wendling3f12ac22013-02-05 22:37:24 +00001198 Attribute::AttrKind Kind = Attr.getKindAsEnum();
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001199 Attrs[Kind] = true;
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001200
Bill Wendling3f12ac22013-02-05 22:37:24 +00001201 if (Kind == Attribute::Alignment)
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001202 Alignment = Attr.getAlignment();
Bill Wendling3f12ac22013-02-05 22:37:24 +00001203 else if (Kind == Attribute::StackAlignment)
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001204 StackAlignment = Attr.getStackAlignment();
Hal Finkelb0407ba2014-07-18 15:51:28 +00001205 else if (Kind == Attribute::Dereferenceable)
1206 DerefBytes = Attr.getDereferenceableBytes();
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001207 else if (Kind == Attribute::DereferenceableOrNull)
1208 DerefOrNullBytes = Attr.getDereferenceableOrNullBytes();
George Burgess IV278199f2016-04-12 01:05:35 +00001209 else if (Kind == Attribute::AllocSize)
1210 AllocSizeArgs = Attr.getValueAsInt();
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001211 return *this;
1212}
1213
Bill Wendlingb9c5b1a2013-02-05 08:09:32 +00001214AttrBuilder &AttrBuilder::addAttribute(StringRef A, StringRef V) {
1215 TargetDepAttrs[A] = V;
1216 return *this;
1217}
1218
Bill Wendling23804da2013-01-31 23:38:01 +00001219AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001220 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
1221 Attrs[Val] = false;
Bill Wendling23804da2013-01-31 23:38:01 +00001222
1223 if (Val == Attribute::Alignment)
1224 Alignment = 0;
1225 else if (Val == Attribute::StackAlignment)
1226 StackAlignment = 0;
Hal Finkelb0407ba2014-07-18 15:51:28 +00001227 else if (Val == Attribute::Dereferenceable)
1228 DerefBytes = 0;
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001229 else if (Val == Attribute::DereferenceableOrNull)
1230 DerefOrNullBytes = 0;
George Burgess IV278199f2016-04-12 01:05:35 +00001231 else if (Val == Attribute::AllocSize)
1232 AllocSizeArgs = 0;
Bill Wendling23804da2013-01-31 23:38:01 +00001233
1234 return *this;
1235}
1236
Bill Wendlingd2196752013-01-30 23:07:40 +00001237AttrBuilder &AttrBuilder::removeAttributes(AttributeSet A, uint64_t Index) {
Bill Wendling211316c2013-04-18 20:17:28 +00001238 unsigned Slot = ~0U;
Bill Wendlingf1c94e32013-02-01 00:13:50 +00001239 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
1240 if (A.getSlotIndex(I) == Index) {
Bill Wendling211316c2013-04-18 20:17:28 +00001241 Slot = I;
Bill Wendlingf1c94e32013-02-01 00:13:50 +00001242 break;
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001243 }
Bill Wendlingf1c94e32013-02-01 00:13:50 +00001244
Bill Wendling211316c2013-04-18 20:17:28 +00001245 assert(Slot != ~0U && "Couldn't find index in AttributeSet!");
Bill Wendlingf1c94e32013-02-01 00:13:50 +00001246
Bill Wendling211316c2013-04-18 20:17:28 +00001247 for (AttributeSet::iterator I = A.begin(Slot), E = A.end(Slot); I != E; ++I) {
Bill Wendling7cde51d2013-02-12 07:56:49 +00001248 Attribute Attr = *I;
Hal Finkele15442c2014-07-18 06:51:55 +00001249 if (Attr.isEnumAttribute() || Attr.isIntAttribute()) {
George Burgess IV500d3032015-12-16 05:21:02 +00001250 removeAttribute(Attr.getKindAsEnum());
Bill Wendling7cde51d2013-02-12 07:56:49 +00001251 } else {
1252 assert(Attr.isStringAttribute() && "Invalid attribute type!");
George Burgess IV500d3032015-12-16 05:21:02 +00001253 removeAttribute(Attr.getKindAsString());
Bill Wendling7cde51d2013-02-12 07:56:49 +00001254 }
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001255 }
1256
1257 return *this;
1258}
1259
Bill Wendlingb9c5b1a2013-02-05 08:09:32 +00001260AttrBuilder &AttrBuilder::removeAttribute(StringRef A) {
1261 std::map<std::string, std::string>::iterator I = TargetDepAttrs.find(A);
1262 if (I != TargetDepAttrs.end())
1263 TargetDepAttrs.erase(I);
1264 return *this;
1265}
1266
George Burgess IV278199f2016-04-12 01:05:35 +00001267std::pair<unsigned, Optional<unsigned>> AttrBuilder::getAllocSizeArgs() const {
1268 return unpackAllocSizeArgs(AllocSizeArgs);
1269}
1270
Bill Wendling50d27842012-10-15 20:35:56 +00001271AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
Bill Wendlingabd5ba22012-10-14 03:58:29 +00001272 if (Align == 0) return *this;
Bill Wendlingcd330342013-01-04 23:27:34 +00001273
Bill Wendlingabf3feb2012-10-05 06:44:41 +00001274 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1275 assert(Align <= 0x40000000 && "Alignment too large.");
Bill Wendlingcd330342013-01-04 23:27:34 +00001276
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001277 Attrs[Attribute::Alignment] = true;
Bill Wendlingcd330342013-01-04 23:27:34 +00001278 Alignment = Align;
Bill Wendlingabd5ba22012-10-14 03:58:29 +00001279 return *this;
Chris Lattner3e13b8c2008-01-02 23:42:30 +00001280}
1281
Bill Wendlingcd330342013-01-04 23:27:34 +00001282AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
1283 // Default alignment, allow the target to define how to align it.
1284 if (Align == 0) return *this;
1285
1286 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1287 assert(Align <= 0x100 && "Alignment too large.");
1288
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001289 Attrs[Attribute::StackAlignment] = true;
Bill Wendlingcd330342013-01-04 23:27:34 +00001290 StackAlignment = Align;
1291 return *this;
1292}
1293
Hal Finkelb0407ba2014-07-18 15:51:28 +00001294AttrBuilder &AttrBuilder::addDereferenceableAttr(uint64_t Bytes) {
1295 if (Bytes == 0) return *this;
1296
1297 Attrs[Attribute::Dereferenceable] = true;
1298 DerefBytes = Bytes;
1299 return *this;
1300}
1301
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001302AttrBuilder &AttrBuilder::addDereferenceableOrNullAttr(uint64_t Bytes) {
1303 if (Bytes == 0)
1304 return *this;
1305
1306 Attrs[Attribute::DereferenceableOrNull] = true;
1307 DerefOrNullBytes = Bytes;
1308 return *this;
1309}
1310
George Burgess IV278199f2016-04-12 01:05:35 +00001311AttrBuilder &AttrBuilder::addAllocSizeAttr(unsigned ElemSize,
1312 const Optional<unsigned> &NumElems) {
1313 return addAllocSizeAttrFromRawRepr(packAllocSizeArgs(ElemSize, NumElems));
1314}
1315
1316AttrBuilder &AttrBuilder::addAllocSizeAttrFromRawRepr(uint64_t RawArgs) {
1317 // (0, 0) is our "not present" value, so we need to check for it here.
1318 assert(RawArgs && "Invalid allocsize arguments -- given allocsize(0, 0)");
1319
1320 Attrs[Attribute::AllocSize] = true;
1321 // Reuse existing machinery to store this as a single 64-bit integer so we can
1322 // save a few bytes over using a pair<unsigned, Optional<unsigned>>.
1323 AllocSizeArgs = RawArgs;
1324 return *this;
1325}
1326
Bill Wendlinge2614922013-02-06 01:16:00 +00001327AttrBuilder &AttrBuilder::merge(const AttrBuilder &B) {
1328 // FIXME: What if both have alignments, but they don't match?!
1329 if (!Alignment)
1330 Alignment = B.Alignment;
1331
1332 if (!StackAlignment)
1333 StackAlignment = B.StackAlignment;
1334
Hal Finkelb0407ba2014-07-18 15:51:28 +00001335 if (!DerefBytes)
1336 DerefBytes = B.DerefBytes;
1337
Pete Cooperd2a44612015-05-06 23:19:43 +00001338 if (!DerefOrNullBytes)
1339 DerefOrNullBytes = B.DerefOrNullBytes;
1340
George Burgess IV278199f2016-04-12 01:05:35 +00001341 if (!AllocSizeArgs)
1342 AllocSizeArgs = B.AllocSizeArgs;
1343
Benjamin Kramer45e7d532013-02-16 19:13:18 +00001344 Attrs |= B.Attrs;
Bill Wendlinge2614922013-02-06 01:16:00 +00001345
Pete Cooperd2a44612015-05-06 23:19:43 +00001346 for (auto I : B.td_attrs())
1347 TargetDepAttrs[I.first] = I.second;
Bill Wendlinge2614922013-02-06 01:16:00 +00001348
1349 return *this;
1350}
1351
Pete Cooperd2a44612015-05-06 23:19:43 +00001352AttrBuilder &AttrBuilder::remove(const AttrBuilder &B) {
1353 // FIXME: What if both have alignments, but they don't match?!
1354 if (B.Alignment)
1355 Alignment = 0;
1356
1357 if (B.StackAlignment)
1358 StackAlignment = 0;
1359
1360 if (B.DerefBytes)
1361 DerefBytes = 0;
1362
1363 if (B.DerefOrNullBytes)
1364 DerefOrNullBytes = 0;
1365
George Burgess IV278199f2016-04-12 01:05:35 +00001366 if (B.AllocSizeArgs)
1367 AllocSizeArgs = 0;
1368
Pete Cooperd2a44612015-05-06 23:19:43 +00001369 Attrs &= ~B.Attrs;
1370
1371 for (auto I : B.td_attrs())
1372 TargetDepAttrs.erase(I.first);
1373
1374 return *this;
1375}
1376
1377bool AttrBuilder::overlaps(const AttrBuilder &B) const {
1378 // First check if any of the target independent attributes overlap.
1379 if ((Attrs & B.Attrs).any())
1380 return true;
1381
1382 // Then check if any target dependent ones do.
Sean Silva9011aca2017-02-22 06:34:04 +00001383 for (const auto &I : td_attrs())
Pete Cooperd2a44612015-05-06 23:19:43 +00001384 if (B.contains(I.first))
1385 return true;
1386
1387 return false;
1388}
1389
Bill Wendling4b001442013-02-06 01:33:42 +00001390bool AttrBuilder::contains(StringRef A) const {
1391 return TargetDepAttrs.find(A) != TargetDepAttrs.end();
1392}
1393
Bill Wendling50d27842012-10-15 20:35:56 +00001394bool AttrBuilder::hasAttributes() const {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001395 return !Attrs.none() || !TargetDepAttrs.empty();
Bill Wendlingc6daefa2012-10-08 23:27:46 +00001396}
Bill Wendling9ac69f92013-01-04 20:54:35 +00001397
Bill Wendlingd2196752013-01-30 23:07:40 +00001398bool AttrBuilder::hasAttributes(AttributeSet A, uint64_t Index) const {
Bill Wendling211316c2013-04-18 20:17:28 +00001399 unsigned Slot = ~0U;
Bill Wendling9ca01da2013-02-02 00:42:06 +00001400 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
1401 if (A.getSlotIndex(I) == Index) {
Bill Wendling211316c2013-04-18 20:17:28 +00001402 Slot = I;
Bill Wendling9ca01da2013-02-02 00:42:06 +00001403 break;
1404 }
1405
Bill Wendling211316c2013-04-18 20:17:28 +00001406 assert(Slot != ~0U && "Couldn't find the index!");
Bill Wendling9ca01da2013-02-02 00:42:06 +00001407
George Burgess IV500d3032015-12-16 05:21:02 +00001408 for (AttributeSet::iterator I = A.begin(Slot), E = A.end(Slot); I != E; ++I) {
Bill Wendling7cde51d2013-02-12 07:56:49 +00001409 Attribute Attr = *I;
Hal Finkele15442c2014-07-18 06:51:55 +00001410 if (Attr.isEnumAttribute() || Attr.isIntAttribute()) {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001411 if (Attrs[I->getKindAsEnum()])
Bill Wendling7cde51d2013-02-12 07:56:49 +00001412 return true;
1413 } else {
1414 assert(Attr.isStringAttribute() && "Invalid attribute kind!");
1415 return TargetDepAttrs.find(Attr.getKindAsString())!=TargetDepAttrs.end();
1416 }
1417 }
Bill Wendling9ca01da2013-02-02 00:42:06 +00001418
1419 return false;
Bill Wendling70f39172012-10-09 00:01:21 +00001420}
Bill Wendling9ac69f92013-01-04 20:54:35 +00001421
Bill Wendling50d27842012-10-15 20:35:56 +00001422bool AttrBuilder::hasAlignmentAttr() const {
Bill Wendlingcd330342013-01-04 23:27:34 +00001423 return Alignment != 0;
Bill Wendlingc6daefa2012-10-08 23:27:46 +00001424}
1425
Bill Wendlingd509a662013-01-29 00:34:06 +00001426bool AttrBuilder::operator==(const AttrBuilder &B) {
Benjamin Kramer45e7d532013-02-16 19:13:18 +00001427 if (Attrs != B.Attrs)
1428 return false;
Bill Wendling4b001442013-02-06 01:33:42 +00001429
1430 for (td_const_iterator I = TargetDepAttrs.begin(),
1431 E = TargetDepAttrs.end(); I != E; ++I)
1432 if (B.TargetDepAttrs.find(I->first) == B.TargetDepAttrs.end())
1433 return false;
1434
Hal Finkelb0407ba2014-07-18 15:51:28 +00001435 return Alignment == B.Alignment && StackAlignment == B.StackAlignment &&
1436 DerefBytes == B.DerefBytes;
Bill Wendlingd509a662013-01-29 00:34:06 +00001437}
1438
Bill Wendling57625a42013-01-25 23:09:36 +00001439//===----------------------------------------------------------------------===//
1440// AttributeFuncs Function Defintions
1441//===----------------------------------------------------------------------===//
1442
Bill Wendlingc79cdff2013-02-01 01:04:27 +00001443/// \brief Which attributes cannot be applied to a type.
Craig Toppere3dcce92015-08-01 22:20:21 +00001444AttrBuilder AttributeFuncs::typeIncompatible(Type *Ty) {
Bill Wendling57625a42013-01-25 23:09:36 +00001445 AttrBuilder Incompatible;
1446
1447 if (!Ty->isIntegerTy())
1448 // Attribute that only apply to integers.
1449 Incompatible.addAttribute(Attribute::SExt)
1450 .addAttribute(Attribute::ZExt);
1451
1452 if (!Ty->isPointerTy())
1453 // Attribute that only apply to pointers.
1454 Incompatible.addAttribute(Attribute::ByVal)
1455 .addAttribute(Attribute::Nest)
1456 .addAttribute(Attribute::NoAlias)
1457 .addAttribute(Attribute::NoCapture)
Nick Lewyckyd52b1522014-05-20 01:23:40 +00001458 .addAttribute(Attribute::NonNull)
Hal Finkelb0407ba2014-07-18 15:51:28 +00001459 .addDereferenceableAttr(1) // the int here is ignored
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001460 .addDereferenceableOrNullAttr(1) // the int here is ignored
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001461 .addAttribute(Attribute::ReadNone)
1462 .addAttribute(Attribute::ReadOnly)
Reid Klecknera534a382013-12-19 02:14:12 +00001463 .addAttribute(Attribute::StructRet)
1464 .addAttribute(Attribute::InAlloca);
Bill Wendling57625a42013-01-25 23:09:36 +00001465
Pete Cooper2777d8872015-05-06 23:19:56 +00001466 return Incompatible;
Bill Wendling57625a42013-01-25 23:09:36 +00001467}
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001468
1469template<typename AttrClass>
1470static bool isEqual(const Function &Caller, const Function &Callee) {
1471 return Caller.getFnAttribute(AttrClass::getKind()) ==
1472 Callee.getFnAttribute(AttrClass::getKind());
1473}
1474
1475/// \brief Compute the logical AND of the attributes of the caller and the
1476/// callee.
1477///
1478/// This function sets the caller's attribute to false if the callee's attribute
1479/// is false.
1480template<typename AttrClass>
1481static void setAND(Function &Caller, const Function &Callee) {
1482 if (AttrClass::isSet(Caller, AttrClass::getKind()) &&
1483 !AttrClass::isSet(Callee, AttrClass::getKind()))
1484 AttrClass::set(Caller, AttrClass::getKind(), false);
1485}
1486
1487/// \brief Compute the logical OR of the attributes of the caller and the
1488/// callee.
1489///
1490/// This function sets the caller's attribute to true if the callee's attribute
1491/// is true.
1492template<typename AttrClass>
1493static void setOR(Function &Caller, const Function &Callee) {
1494 if (!AttrClass::isSet(Caller, AttrClass::getKind()) &&
1495 AttrClass::isSet(Callee, AttrClass::getKind()))
1496 AttrClass::set(Caller, AttrClass::getKind(), true);
1497}
1498
1499/// \brief If the inlined function had a higher stack protection level than the
1500/// calling function, then bump up the caller's stack protection level.
1501static void adjustCallerSSPLevel(Function &Caller, const Function &Callee) {
1502 // If upgrading the SSP attribute, clear out the old SSP Attributes first.
1503 // Having multiple SSP attributes doesn't actually hurt, but it adds useless
1504 // clutter to the IR.
1505 AttrBuilder B;
1506 B.addAttribute(Attribute::StackProtect)
1507 .addAttribute(Attribute::StackProtectStrong)
1508 .addAttribute(Attribute::StackProtectReq);
1509 AttributeSet OldSSPAttr = AttributeSet::get(Caller.getContext(),
1510 AttributeSet::FunctionIndex,
1511 B);
1512
Evgeniy Stepanovf17120a2016-04-11 22:27:48 +00001513 if (Callee.hasFnAttribute(Attribute::StackProtectReq)) {
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001514 Caller.removeAttributes(AttributeSet::FunctionIndex, OldSSPAttr);
1515 Caller.addFnAttr(Attribute::StackProtectReq);
1516 } else if (Callee.hasFnAttribute(Attribute::StackProtectStrong) &&
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001517 !Caller.hasFnAttribute(Attribute::StackProtectReq)) {
1518 Caller.removeAttributes(AttributeSet::FunctionIndex, OldSSPAttr);
1519 Caller.addFnAttr(Attribute::StackProtectStrong);
1520 } else if (Callee.hasFnAttribute(Attribute::StackProtect) &&
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001521 !Caller.hasFnAttribute(Attribute::StackProtectReq) &&
1522 !Caller.hasFnAttribute(Attribute::StackProtectStrong))
1523 Caller.addFnAttr(Attribute::StackProtect);
1524}
1525
1526#define GET_ATTR_COMPAT_FUNC
1527#include "AttributesCompatFunc.inc"
1528
1529bool AttributeFuncs::areInlineCompatible(const Function &Caller,
1530 const Function &Callee) {
1531 return hasCompatibleFnAttrs(Caller, Callee);
1532}
1533
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001534void AttributeFuncs::mergeAttributesForInlining(Function &Caller,
1535 const Function &Callee) {
1536 mergeFnAttrs(Caller, Callee);
1537}