blob: d098fc1a401280a0a3490a0bf9e87c4930be58e0 [file] [log] [blame]
Bill Wendlingec454542013-01-28 21:55:20 +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
Chandler Carruth9fb823b2013-01-02 11:36:10 +000016#include "llvm/IR/Attributes.h"
Akira Hatanaka1cb242e2015-12-22 23:57:37 +000017#include "llvm/IR/Function.h"
Bill Wendling4607f4b2012-12-20 01:36:59 +000018#include "AttributeImpl.h"
Bill Wendlinge38b8042012-09-26 21:07:29 +000019#include "LLVMContextImpl.h"
Benjamin Kramer502b9e12014-04-12 16:15:53 +000020#include "llvm/ADT/STLExtras.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000021#include "llvm/ADT/StringExtras.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000022#include "llvm/IR/Type.h"
Benjamin Kramer17388a62014-03-03 18:02:34 +000023#include "llvm/Support/Atomic.h"
David Greenef7014732010-01-05 01:29:58 +000024#include "llvm/Support/Debug.h"
Chris Lattner3e13b8c2008-01-02 23:42:30 +000025#include "llvm/Support/ManagedStatic.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000026#include "llvm/Support/Mutex.h"
Benjamin Kramer1a25d732009-08-23 11:37:21 +000027#include "llvm/Support/raw_ostream.h"
Bill Wendlingd2e493b2013-01-24 00:06:56 +000028#include <algorithm>
Chris Lattner3e13b8c2008-01-02 23:42:30 +000029using namespace llvm;
30
Chris Lattner8a923e72008-03-12 17:45:29 +000031//===----------------------------------------------------------------------===//
Bill Wendling7707c5a2013-01-29 00:48:16 +000032// Attribute Construction Methods
Chris Lattner8a923e72008-03-12 17:45:29 +000033//===----------------------------------------------------------------------===//
Chris Lattnerd0e1f102008-01-03 00:10:22 +000034
George Burgess IV278199f2016-04-12 01:05:35 +000035// allocsize has two integer arguments, but because they're both 32 bits, we can
36// pack them into one 64-bit value, at the cost of making said value
37// nonsensical.
38//
39// In order to do this, we need to reserve one value of the second (optional)
40// allocsize argument to signify "not present."
41LLVM_CONSTEXPR static unsigned AllocSizeNumElemsNotPresent =
42 std::numeric_limits<unsigned>::max();
43
44static uint64_t packAllocSizeArgs(unsigned ElemSizeArg,
45 const Optional<unsigned> &NumElemsArg) {
46 assert((!NumElemsArg.hasValue() ||
47 *NumElemsArg != AllocSizeNumElemsNotPresent) &&
48 "Attempting to pack a reserved value");
49
50 return uint64_t(ElemSizeArg) << 32 |
51 NumElemsArg.getValueOr(AllocSizeNumElemsNotPresent);
52}
53
54static std::pair<unsigned, Optional<unsigned>>
55unpackAllocSizeArgs(uint64_t Num) {
56 unsigned NumElems = Num & std::numeric_limits<unsigned>::max();
57 unsigned ElemSizeArg = Num >> 32;
58
59 Optional<unsigned> NumElemsArg;
60 if (NumElems != AllocSizeNumElemsNotPresent)
61 NumElemsArg = NumElems;
62 return std::make_pair(ElemSizeArg, NumElemsArg);
63}
64
Matt Arsenaulte1cef6e2014-09-03 23:24:31 +000065Attribute Attribute::get(LLVMContext &Context, Attribute::AttrKind Kind,
66 uint64_t Val) {
67 LLVMContextImpl *pImpl = Context.pImpl;
68 FoldingSetNodeID ID;
69 ID.AddInteger(Kind);
Matt Arsenaultfb4308e2014-09-03 23:38:05 +000070 if (Val) ID.AddInteger(Val);
Matt Arsenaulte1cef6e2014-09-03 23:24:31 +000071
72 void *InsertPoint;
73 AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
74
75 if (!PA) {
76 // If we didn't find any existing attributes of the same shape then create a
77 // new one and insert it.
Matt Arsenaultfb4308e2014-09-03 23:38:05 +000078 if (!Val)
79 PA = new EnumAttributeImpl(Kind);
80 else
81 PA = new IntAttributeImpl(Kind, Val);
Bill Wendling3f12ac22013-02-05 22:37:24 +000082 pImpl->AttrsSet.InsertNode(PA, InsertPoint);
83 }
84
85 // Return the Attribute that we found or created.
86 return Attribute(PA);
87}
88
89Attribute Attribute::get(LLVMContext &Context, StringRef Kind, StringRef Val) {
90 LLVMContextImpl *pImpl = Context.pImpl;
91 FoldingSetNodeID ID;
92 ID.AddString(Kind);
93 if (!Val.empty()) ID.AddString(Val);
Bill Wendling73ea2de2012-10-08 21:47:17 +000094
95 void *InsertPoint;
Bill Wendling4607f4b2012-12-20 01:36:59 +000096 AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
Bill Wendling73ea2de2012-10-08 21:47:17 +000097
98 if (!PA) {
99 // If we didn't find any existing attributes of the same shape then create a
100 // new one and insert it.
Benjamin Kramer741146b2013-07-11 12:13:16 +0000101 PA = new StringAttributeImpl(Kind, Val);
Bill Wendling73ea2de2012-10-08 21:47:17 +0000102 pImpl->AttrsSet.InsertNode(PA, InsertPoint);
103 }
104
Bill Wendlingb9c5b1a2013-02-05 08:09:32 +0000105 // Return the Attribute that we found or created.
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000106 return Attribute(PA);
Bill Wendling73ea2de2012-10-08 21:47:17 +0000107}
108
Bill Wendling4bbe9db2013-01-27 22:43:04 +0000109Attribute Attribute::getWithAlignment(LLVMContext &Context, uint64_t Align) {
Bill Wendling1c7cc8a2013-01-31 23:16:25 +0000110 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
111 assert(Align <= 0x40000000 && "Alignment too large.");
Bill Wendling3f12ac22013-02-05 22:37:24 +0000112 return get(Context, Alignment, Align);
Bill Wendling4bbe9db2013-01-27 22:43:04 +0000113}
114
115Attribute Attribute::getWithStackAlignment(LLVMContext &Context,
116 uint64_t Align) {
Bill Wendling1c7cc8a2013-01-31 23:16:25 +0000117 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
118 assert(Align <= 0x100 && "Alignment too large.");
Bill Wendling3f12ac22013-02-05 22:37:24 +0000119 return get(Context, StackAlignment, Align);
Bill Wendling4bbe9db2013-01-27 22:43:04 +0000120}
121
Hal Finkelb0407ba2014-07-18 15:51:28 +0000122Attribute Attribute::getWithDereferenceableBytes(LLVMContext &Context,
123 uint64_t Bytes) {
124 assert(Bytes && "Bytes must be non-zero.");
125 return get(Context, Dereferenceable, Bytes);
126}
127
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000128Attribute Attribute::getWithDereferenceableOrNullBytes(LLVMContext &Context,
129 uint64_t Bytes) {
130 assert(Bytes && "Bytes must be non-zero.");
131 return get(Context, DereferenceableOrNull, Bytes);
132}
133
George Burgess IV278199f2016-04-12 01:05:35 +0000134Attribute
135Attribute::getWithAllocSizeArgs(LLVMContext &Context, unsigned ElemSizeArg,
136 const Optional<unsigned> &NumElemsArg) {
137 assert(!(ElemSizeArg == 0 && NumElemsArg && *NumElemsArg == 0) &&
138 "Invalid allocsize arguments -- given allocsize(0, 0)");
139 return get(Context, AllocSize, packAllocSizeArgs(ElemSizeArg, NumElemsArg));
140}
141
Bill Wendling7707c5a2013-01-29 00:48:16 +0000142//===----------------------------------------------------------------------===//
143// Attribute Accessor Methods
144//===----------------------------------------------------------------------===//
145
Bill Wendling3f12ac22013-02-05 22:37:24 +0000146bool Attribute::isEnumAttribute() const {
147 return pImpl && pImpl->isEnumAttribute();
148}
149
Hal Finkele15442c2014-07-18 06:51:55 +0000150bool Attribute::isIntAttribute() const {
151 return pImpl && pImpl->isIntAttribute();
Bill Wendling3f12ac22013-02-05 22:37:24 +0000152}
153
154bool Attribute::isStringAttribute() const {
155 return pImpl && pImpl->isStringAttribute();
156}
157
158Attribute::AttrKind Attribute::getKindAsEnum() const {
Bill Wendling440e9d82013-07-25 00:34:29 +0000159 if (!pImpl) return None;
Hal Finkele15442c2014-07-18 06:51:55 +0000160 assert((isEnumAttribute() || isIntAttribute()) &&
Bill Wendling3f12ac22013-02-05 22:37:24 +0000161 "Invalid attribute type to get the kind as an enum!");
George Burgess IV500d3032015-12-16 05:21:02 +0000162 return pImpl->getKindAsEnum();
Bill Wendling3f12ac22013-02-05 22:37:24 +0000163}
164
165uint64_t Attribute::getValueAsInt() const {
Bill Wendling440e9d82013-07-25 00:34:29 +0000166 if (!pImpl) return 0;
Hal Finkele15442c2014-07-18 06:51:55 +0000167 assert(isIntAttribute() &&
168 "Expected the attribute to be an integer attribute!");
George Burgess IV500d3032015-12-16 05:21:02 +0000169 return pImpl->getValueAsInt();
Bill Wendling3f12ac22013-02-05 22:37:24 +0000170}
171
172StringRef Attribute::getKindAsString() const {
Bill Wendling440e9d82013-07-25 00:34:29 +0000173 if (!pImpl) return StringRef();
Bill Wendling3f12ac22013-02-05 22:37:24 +0000174 assert(isStringAttribute() &&
175 "Invalid attribute type to get the kind as a string!");
George Burgess IV500d3032015-12-16 05:21:02 +0000176 return pImpl->getKindAsString();
Bill Wendling3f12ac22013-02-05 22:37:24 +0000177}
178
179StringRef Attribute::getValueAsString() const {
Bill Wendling440e9d82013-07-25 00:34:29 +0000180 if (!pImpl) return StringRef();
Bill Wendling3f12ac22013-02-05 22:37:24 +0000181 assert(isStringAttribute() &&
182 "Invalid attribute type to get the value as a string!");
George Burgess IV500d3032015-12-16 05:21:02 +0000183 return pImpl->getValueAsString();
Bill Wendling3f12ac22013-02-05 22:37:24 +0000184}
185
Bill Wendlingae89a0f2013-02-05 23:48:36 +0000186bool Attribute::hasAttribute(AttrKind Kind) const {
187 return (pImpl && pImpl->hasAttribute(Kind)) || (!pImpl && Kind == None);
188}
189
190bool Attribute::hasAttribute(StringRef Kind) const {
191 if (!isStringAttribute()) return false;
192 return pImpl && pImpl->hasAttribute(Kind);
Bill Wendling03eefb32013-01-29 20:45:34 +0000193}
194
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000195unsigned Attribute::getAlignment() const {
Bill Wendlingc79cdff2013-02-01 01:04:27 +0000196 assert(hasAttribute(Attribute::Alignment) &&
197 "Trying to get alignment from non-alignment attribute!");
Bill Wendling3f12ac22013-02-05 22:37:24 +0000198 return pImpl->getValueAsInt();
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000199}
200
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000201unsigned Attribute::getStackAlignment() const {
Bill Wendlingc79cdff2013-02-01 01:04:27 +0000202 assert(hasAttribute(Attribute::StackAlignment) &&
203 "Trying to get alignment from non-alignment attribute!");
Bill Wendling3f12ac22013-02-05 22:37:24 +0000204 return pImpl->getValueAsInt();
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000205}
206
Hal Finkelb0407ba2014-07-18 15:51:28 +0000207uint64_t Attribute::getDereferenceableBytes() const {
208 assert(hasAttribute(Attribute::Dereferenceable) &&
209 "Trying to get dereferenceable bytes from "
210 "non-dereferenceable attribute!");
211 return pImpl->getValueAsInt();
212}
213
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000214uint64_t Attribute::getDereferenceableOrNullBytes() const {
215 assert(hasAttribute(Attribute::DereferenceableOrNull) &&
216 "Trying to get dereferenceable bytes from "
217 "non-dereferenceable attribute!");
218 return pImpl->getValueAsInt();
219}
220
George Burgess IV278199f2016-04-12 01:05:35 +0000221std::pair<unsigned, Optional<unsigned>> Attribute::getAllocSizeArgs() const {
222 assert(hasAttribute(Attribute::AllocSize) &&
223 "Trying to get allocsize args from non-allocsize attribute");
224 return unpackAllocSizeArgs(pImpl->getValueAsInt());
225}
226
Bill Wendling829b4782013-02-11 08:43:33 +0000227std::string Attribute::getAsString(bool InAttrGrp) const {
Bill Wendling9c2eba92013-01-31 20:59:05 +0000228 if (!pImpl) return "";
229
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000230 if (hasAttribute(Attribute::SanitizeAddress))
231 return "sanitize_address";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000232 if (hasAttribute(Attribute::AlwaysInline))
233 return "alwaysinline";
Igor Laevsky39d662f2015-07-11 10:30:36 +0000234 if (hasAttribute(Attribute::ArgMemOnly))
235 return "argmemonly";
Michael Gottesman41748d72013-06-27 00:25:01 +0000236 if (hasAttribute(Attribute::Builtin))
237 return "builtin";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000238 if (hasAttribute(Attribute::ByVal))
239 return "byval";
Owen Anderson85fa7d52015-05-26 23:48:40 +0000240 if (hasAttribute(Attribute::Convergent))
241 return "convergent";
Manman Ren9bfd0d02016-04-01 21:41:15 +0000242 if (hasAttribute(Attribute::SwiftError))
243 return "swifterror";
Manman Renf46262e2016-03-29 17:37:21 +0000244 if (hasAttribute(Attribute::SwiftSelf))
245 return "swiftself";
Vaivaswatha Nagarajfb3f4902015-12-16 16:16:19 +0000246 if (hasAttribute(Attribute::InaccessibleMemOnly))
247 return "inaccessiblememonly";
248 if (hasAttribute(Attribute::InaccessibleMemOrArgMemOnly))
249 return "inaccessiblemem_or_argmemonly";
Reid Klecknera534a382013-12-19 02:14:12 +0000250 if (hasAttribute(Attribute::InAlloca))
251 return "inalloca";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000252 if (hasAttribute(Attribute::InlineHint))
253 return "inlinehint";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000254 if (hasAttribute(Attribute::InReg))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000255 return "inreg";
Tom Roeder44cb65f2014-06-05 19:29:43 +0000256 if (hasAttribute(Attribute::JumpTable))
257 return "jumptable";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000258 if (hasAttribute(Attribute::MinSize))
259 return "minsize";
260 if (hasAttribute(Attribute::Naked))
261 return "naked";
262 if (hasAttribute(Attribute::Nest))
263 return "nest";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000264 if (hasAttribute(Attribute::NoAlias))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000265 return "noalias";
Bill Wendling09bd1f72013-02-22 00:12:35 +0000266 if (hasAttribute(Attribute::NoBuiltin))
267 return "nobuiltin";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000268 if (hasAttribute(Attribute::NoCapture))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000269 return "nocapture";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000270 if (hasAttribute(Attribute::NoDuplicate))
271 return "noduplicate";
272 if (hasAttribute(Attribute::NoImplicitFloat))
273 return "noimplicitfloat";
274 if (hasAttribute(Attribute::NoInline))
275 return "noinline";
276 if (hasAttribute(Attribute::NonLazyBind))
277 return "nonlazybind";
Nick Lewyckyd52b1522014-05-20 01:23:40 +0000278 if (hasAttribute(Attribute::NonNull))
279 return "nonnull";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000280 if (hasAttribute(Attribute::NoRedZone))
281 return "noredzone";
282 if (hasAttribute(Attribute::NoReturn))
283 return "noreturn";
James Molloye6f87ca2015-11-06 10:32:53 +0000284 if (hasAttribute(Attribute::NoRecurse))
285 return "norecurse";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000286 if (hasAttribute(Attribute::NoUnwind))
287 return "nounwind";
Andrea Di Biagio377496b2013-08-23 11:53:55 +0000288 if (hasAttribute(Attribute::OptimizeNone))
289 return "optnone";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000290 if (hasAttribute(Attribute::OptimizeForSize))
291 return "optsize";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000292 if (hasAttribute(Attribute::ReadNone))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000293 return "readnone";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000294 if (hasAttribute(Attribute::ReadOnly))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000295 return "readonly";
Stephen Linb8bd2322013-04-20 05:14:40 +0000296 if (hasAttribute(Attribute::Returned))
297 return "returned";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000298 if (hasAttribute(Attribute::ReturnsTwice))
299 return "returns_twice";
300 if (hasAttribute(Attribute::SExt))
301 return "signext";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000302 if (hasAttribute(Attribute::StackProtect))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000303 return "ssp";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000304 if (hasAttribute(Attribute::StackProtectReq))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000305 return "sspreq";
Bill Wendlingd154e2832013-01-23 06:41:41 +0000306 if (hasAttribute(Attribute::StackProtectStrong))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000307 return "sspstrong";
Peter Collingbourne82437bf2015-06-15 21:07:11 +0000308 if (hasAttribute(Attribute::SafeStack))
309 return "safestack";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000310 if (hasAttribute(Attribute::StructRet))
311 return "sret";
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000312 if (hasAttribute(Attribute::SanitizeThread))
313 return "sanitize_thread";
314 if (hasAttribute(Attribute::SanitizeMemory))
315 return "sanitize_memory";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000316 if (hasAttribute(Attribute::UWTable))
317 return "uwtable";
318 if (hasAttribute(Attribute::ZExt))
319 return "zeroext";
Diego Novilloc6399532013-05-24 12:26:52 +0000320 if (hasAttribute(Attribute::Cold))
321 return "cold";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000322
323 // FIXME: These should be output like this:
324 //
325 // align=4
326 // alignstack=8
327 //
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000328 if (hasAttribute(Attribute::Alignment)) {
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000329 std::string Result;
Bill Wendling829b4782013-02-11 08:43:33 +0000330 Result += "align";
331 Result += (InAttrGrp) ? "=" : " ";
Bill Wendling3f12ac22013-02-05 22:37:24 +0000332 Result += utostr(getValueAsInt());
333 return Result;
334 }
Bill Wendling829b4782013-02-11 08:43:33 +0000335
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000336 auto AttrWithBytesToString = [&](const char *Name) {
Bill Wendling3f12ac22013-02-05 22:37:24 +0000337 std::string Result;
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000338 Result += Name;
Bill Wendling829b4782013-02-11 08:43:33 +0000339 if (InAttrGrp) {
340 Result += "=";
341 Result += utostr(getValueAsInt());
342 } else {
343 Result += "(";
344 Result += utostr(getValueAsInt());
345 Result += ")";
346 }
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000347 return Result;
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000348 };
Bill Wendling9c2eba92013-01-31 20:59:05 +0000349
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000350 if (hasAttribute(Attribute::StackAlignment))
351 return AttrWithBytesToString("alignstack");
352
353 if (hasAttribute(Attribute::Dereferenceable))
354 return AttrWithBytesToString("dereferenceable");
355
356 if (hasAttribute(Attribute::DereferenceableOrNull))
357 return AttrWithBytesToString("dereferenceable_or_null");
Hal Finkelb0407ba2014-07-18 15:51:28 +0000358
George Burgess IV278199f2016-04-12 01:05:35 +0000359 if (hasAttribute(Attribute::AllocSize)) {
360 unsigned ElemSize;
361 Optional<unsigned> NumElems;
362 std::tie(ElemSize, NumElems) = getAllocSizeArgs();
363
364 std::string Result = "allocsize(";
365 Result += utostr(ElemSize);
366 if (NumElems.hasValue()) {
367 Result += ',';
368 Result += utostr(*NumElems);
369 }
370 Result += ')';
371 return Result;
372 }
373
Bill Wendling9c2eba92013-01-31 20:59:05 +0000374 // Convert target-dependent attributes to strings of the form:
375 //
376 // "kind"
377 // "kind" = "value"
Bill Wendling9c2eba92013-01-31 20:59:05 +0000378 //
Bill Wendling3f12ac22013-02-05 22:37:24 +0000379 if (isStringAttribute()) {
Bill Wendling9c2eba92013-01-31 20:59:05 +0000380 std::string Result;
Yaron Keren075759a2015-03-30 15:42:36 +0000381 Result += (Twine('"') + getKindAsString() + Twine('"')).str();
Bill Wendling9c2eba92013-01-31 20:59:05 +0000382
Bill Wendling3f12ac22013-02-05 22:37:24 +0000383 StringRef Val = pImpl->getValueAsString();
384 if (Val.empty()) return Result;
Bill Wendling7a33f772013-02-01 22:32:30 +0000385
Yaron Keren075759a2015-03-30 15:42:36 +0000386 Result += ("=\"" + Val + Twine('"')).str();
Bill Wendlingc79cdff2013-02-01 01:04:27 +0000387 return Result;
Bill Wendling9c2eba92013-01-31 20:59:05 +0000388 }
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000389
390 llvm_unreachable("Unknown attribute");
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000391}
392
Bill Wendlingd509a662013-01-29 00:34:06 +0000393bool Attribute::operator<(Attribute A) const {
394 if (!pImpl && !A.pImpl) return false;
395 if (!pImpl) return true;
396 if (!A.pImpl) return false;
397 return *pImpl < *A.pImpl;
398}
399
Bill Wendlingd509a662013-01-29 00:34:06 +0000400//===----------------------------------------------------------------------===//
401// AttributeImpl Definition
402//===----------------------------------------------------------------------===//
403
Eric Christopher0eaa5412014-07-02 22:05:40 +0000404// Pin the vtables to this file.
Alexey Samsonov49109a22013-11-18 09:31:53 +0000405AttributeImpl::~AttributeImpl() {}
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +0000406void EnumAttributeImpl::anchor() {}
Hal Finkele15442c2014-07-18 06:51:55 +0000407void IntAttributeImpl::anchor() {}
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +0000408void StringAttributeImpl::anchor() {}
Alexey Samsonov49109a22013-11-18 09:31:53 +0000409
Bill Wendlingd509a662013-01-29 00:34:06 +0000410bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
Bill Wendling3f12ac22013-02-05 22:37:24 +0000411 if (isStringAttribute()) return false;
412 return getKindAsEnum() == A;
Bill Wendlingd509a662013-01-29 00:34:06 +0000413}
414
Bill Wendling3f12ac22013-02-05 22:37:24 +0000415bool AttributeImpl::hasAttribute(StringRef Kind) const {
416 if (!isStringAttribute()) return false;
417 return getKindAsString() == Kind;
Bill Wendlingd509a662013-01-29 00:34:06 +0000418}
419
Bill Wendling3f12ac22013-02-05 22:37:24 +0000420Attribute::AttrKind AttributeImpl::getKindAsEnum() const {
Hal Finkele15442c2014-07-18 06:51:55 +0000421 assert(isEnumAttribute() || isIntAttribute());
Benjamin Kramer741146b2013-07-11 12:13:16 +0000422 return static_cast<const EnumAttributeImpl *>(this)->getEnumKind();
Bill Wendlingd509a662013-01-29 00:34:06 +0000423}
424
Bill Wendling3f12ac22013-02-05 22:37:24 +0000425uint64_t AttributeImpl::getValueAsInt() const {
Hal Finkele15442c2014-07-18 06:51:55 +0000426 assert(isIntAttribute());
427 return static_cast<const IntAttributeImpl *>(this)->getValue();
Bill Wendlingd509a662013-01-29 00:34:06 +0000428}
429
Bill Wendling3f12ac22013-02-05 22:37:24 +0000430StringRef AttributeImpl::getKindAsString() const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000431 assert(isStringAttribute());
432 return static_cast<const StringAttributeImpl *>(this)->getStringKind();
Bill Wendlingd509a662013-01-29 00:34:06 +0000433}
434
Bill Wendling3f12ac22013-02-05 22:37:24 +0000435StringRef AttributeImpl::getValueAsString() const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000436 assert(isStringAttribute());
437 return static_cast<const StringAttributeImpl *>(this)->getStringValue();
Bill Wendlingd509a662013-01-29 00:34:06 +0000438}
439
440bool AttributeImpl::operator<(const AttributeImpl &AI) const {
Bill Wendlingc79cdff2013-02-01 01:04:27 +0000441 // This sorts the attributes with Attribute::AttrKinds coming first (sorted
442 // relative to their enum value) and then strings.
Bill Wendling26b95752013-02-15 05:25:26 +0000443 if (isEnumAttribute()) {
444 if (AI.isEnumAttribute()) return getKindAsEnum() < AI.getKindAsEnum();
Hal Finkele15442c2014-07-18 06:51:55 +0000445 if (AI.isIntAttribute()) return true;
Bill Wendling26b95752013-02-15 05:25:26 +0000446 if (AI.isStringAttribute()) return true;
447 }
Bill Wendlingc79cdff2013-02-01 01:04:27 +0000448
Hal Finkele15442c2014-07-18 06:51:55 +0000449 if (isIntAttribute()) {
Bill Wendling26b95752013-02-15 05:25:26 +0000450 if (AI.isEnumAttribute()) return false;
Reid Kleckner7de67612016-04-04 23:06:05 +0000451 if (AI.isIntAttribute()) {
452 if (getKindAsEnum() == AI.getKindAsEnum())
453 return getValueAsInt() < AI.getValueAsInt();
454 return getKindAsEnum() < AI.getKindAsEnum();
455 }
Bill Wendling26b95752013-02-15 05:25:26 +0000456 if (AI.isStringAttribute()) return true;
Bill Wendling3f12ac22013-02-05 22:37:24 +0000457 }
Bill Wendlingd509a662013-01-29 00:34:06 +0000458
Bill Wendling26b95752013-02-15 05:25:26 +0000459 if (AI.isEnumAttribute()) return false;
Hal Finkele15442c2014-07-18 06:51:55 +0000460 if (AI.isIntAttribute()) return false;
Bill Wendling26b95752013-02-15 05:25:26 +0000461 if (getKindAsString() == AI.getKindAsString())
462 return getValueAsString() < AI.getValueAsString();
463 return getKindAsString() < AI.getKindAsString();
Bill Wendlingd509a662013-01-29 00:34:06 +0000464}
465
Bill Wendlingd509a662013-01-29 00:34:06 +0000466uint64_t AttributeImpl::getAttrMask(Attribute::AttrKind Val) {
467 // FIXME: Remove this.
468 switch (Val) {
469 case Attribute::EndAttrKinds:
Bill Wendlingd509a662013-01-29 00:34:06 +0000470 llvm_unreachable("Synthetic enumerators which should never get here");
471
472 case Attribute::None: return 0;
473 case Attribute::ZExt: return 1 << 0;
474 case Attribute::SExt: return 1 << 1;
475 case Attribute::NoReturn: return 1 << 2;
476 case Attribute::InReg: return 1 << 3;
477 case Attribute::StructRet: return 1 << 4;
478 case Attribute::NoUnwind: return 1 << 5;
479 case Attribute::NoAlias: return 1 << 6;
480 case Attribute::ByVal: return 1 << 7;
481 case Attribute::Nest: return 1 << 8;
482 case Attribute::ReadNone: return 1 << 9;
483 case Attribute::ReadOnly: return 1 << 10;
484 case Attribute::NoInline: return 1 << 11;
485 case Attribute::AlwaysInline: return 1 << 12;
486 case Attribute::OptimizeForSize: return 1 << 13;
487 case Attribute::StackProtect: return 1 << 14;
488 case Attribute::StackProtectReq: return 1 << 15;
489 case Attribute::Alignment: return 31 << 16;
490 case Attribute::NoCapture: return 1 << 21;
491 case Attribute::NoRedZone: return 1 << 22;
492 case Attribute::NoImplicitFloat: return 1 << 23;
493 case Attribute::Naked: return 1 << 24;
494 case Attribute::InlineHint: return 1 << 25;
495 case Attribute::StackAlignment: return 7 << 26;
496 case Attribute::ReturnsTwice: return 1 << 29;
497 case Attribute::UWTable: return 1 << 30;
498 case Attribute::NonLazyBind: return 1U << 31;
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000499 case Attribute::SanitizeAddress: return 1ULL << 32;
Bill Wendlingd509a662013-01-29 00:34:06 +0000500 case Attribute::MinSize: return 1ULL << 33;
501 case Attribute::NoDuplicate: return 1ULL << 34;
502 case Attribute::StackProtectStrong: return 1ULL << 35;
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000503 case Attribute::SanitizeThread: return 1ULL << 36;
504 case Attribute::SanitizeMemory: return 1ULL << 37;
Bill Wendling7bfdfe72013-02-22 00:40:12 +0000505 case Attribute::NoBuiltin: return 1ULL << 38;
Stephen Linb8bd2322013-04-20 05:14:40 +0000506 case Attribute::Returned: return 1ULL << 39;
Diego Novilloc6399532013-05-24 12:26:52 +0000507 case Attribute::Cold: return 1ULL << 40;
Michael Gottesman41748d72013-06-27 00:25:01 +0000508 case Attribute::Builtin: return 1ULL << 41;
Andrea Di Biagio377496b2013-08-23 11:53:55 +0000509 case Attribute::OptimizeNone: return 1ULL << 42;
Reid Klecknera534a382013-12-19 02:14:12 +0000510 case Attribute::InAlloca: return 1ULL << 43;
Nick Lewyckyd52b1522014-05-20 01:23:40 +0000511 case Attribute::NonNull: return 1ULL << 44;
Tom Roeder44cb65f2014-06-05 19:29:43 +0000512 case Attribute::JumpTable: return 1ULL << 45;
Owen Anderson85fa7d52015-05-26 23:48:40 +0000513 case Attribute::Convergent: return 1ULL << 46;
Peter Collingbourne82437bf2015-06-15 21:07:11 +0000514 case Attribute::SafeStack: return 1ULL << 47;
James Molloye6f87ca2015-11-06 10:32:53 +0000515 case Attribute::NoRecurse: return 1ULL << 48;
Vaivaswatha Nagarajfb3f4902015-12-16 16:16:19 +0000516 case Attribute::InaccessibleMemOnly: return 1ULL << 49;
517 case Attribute::InaccessibleMemOrArgMemOnly: return 1ULL << 50;
Manman Renf46262e2016-03-29 17:37:21 +0000518 case Attribute::SwiftSelf: return 1ULL << 51;
Manman Ren9bfd0d02016-04-01 21:41:15 +0000519 case Attribute::SwiftError: return 1ULL << 52;
Hal Finkelb0407ba2014-07-18 15:51:28 +0000520 case Attribute::Dereferenceable:
521 llvm_unreachable("dereferenceable attribute not supported in raw format");
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000522 break;
523 case Attribute::DereferenceableOrNull:
524 llvm_unreachable("dereferenceable_or_null attribute not supported in raw "
525 "format");
526 break;
Igor Laevsky39d662f2015-07-11 10:30:36 +0000527 case Attribute::ArgMemOnly:
528 llvm_unreachable("argmemonly attribute not supported in raw format");
529 break;
George Burgess IV278199f2016-04-12 01:05:35 +0000530 case Attribute::AllocSize:
531 llvm_unreachable("allocsize not supported in raw format");
532 break;
Bill Wendling25342e12013-02-22 00:50:09 +0000533 }
534 llvm_unreachable("Unsupported attribute type");
Bill Wendlingd509a662013-01-29 00:34:06 +0000535}
536
537//===----------------------------------------------------------------------===//
538// AttributeSetNode Definition
539//===----------------------------------------------------------------------===//
540
541AttributeSetNode *AttributeSetNode::get(LLVMContext &C,
542 ArrayRef<Attribute> Attrs) {
543 if (Attrs.empty())
Craig Topperc6207612014-04-09 06:08:46 +0000544 return nullptr;
Bill Wendlingd509a662013-01-29 00:34:06 +0000545
546 // Otherwise, build a key to look up the existing attributes.
547 LLVMContextImpl *pImpl = C.pImpl;
548 FoldingSetNodeID ID;
549
550 SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end());
Reid Kleckner7de67612016-04-04 23:06:05 +0000551 std::sort(SortedAttrs.begin(), SortedAttrs.end());
Bill Wendlingd509a662013-01-29 00:34:06 +0000552
George Burgess IV500d3032015-12-16 05:21:02 +0000553 for (Attribute Attr : SortedAttrs)
554 Attr.Profile(ID);
Bill Wendlingd509a662013-01-29 00:34:06 +0000555
556 void *InsertPoint;
557 AttributeSetNode *PA =
558 pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint);
559
560 // If we didn't find any existing attributes of the same shape then create a
561 // new one and insert it.
562 if (!PA) {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000563 // Coallocate entries after the AttributeSetNode itself.
James Y Knightaa365b22015-08-05 22:57:34 +0000564 void *Mem = ::operator new(totalSizeToAlloc<Attribute>(SortedAttrs.size()));
Benjamin Kramer741146b2013-07-11 12:13:16 +0000565 PA = new (Mem) AttributeSetNode(SortedAttrs);
Bill Wendlingd509a662013-01-29 00:34:06 +0000566 pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint);
567 }
568
569 // Return the AttributesListNode that we found or created.
570 return PA;
571}
572
Bill Wendlingbce7b972013-02-13 08:42:21 +0000573bool AttributeSetNode::hasAttribute(StringRef Kind) const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000574 for (iterator I = begin(), E = end(); I != E; ++I)
Bill Wendlingbce7b972013-02-13 08:42:21 +0000575 if (I->hasAttribute(Kind))
576 return true;
577 return false;
578}
579
580Attribute AttributeSetNode::getAttribute(Attribute::AttrKind Kind) const {
Matthias Braun31eeb76f2016-01-29 22:25:13 +0000581 if (hasAttribute(Kind)) {
582 for (iterator I = begin(), E = end(); I != E; ++I)
583 if (I->hasAttribute(Kind))
584 return *I;
585 }
Bill Wendlingbce7b972013-02-13 08:42:21 +0000586 return Attribute();
587}
588
589Attribute AttributeSetNode::getAttribute(StringRef Kind) const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000590 for (iterator I = begin(), E = end(); I != E; ++I)
Bill Wendlingbce7b972013-02-13 08:42:21 +0000591 if (I->hasAttribute(Kind))
592 return *I;
593 return Attribute();
594}
595
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000596unsigned AttributeSetNode::getAlignment() const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000597 for (iterator I = begin(), E = end(); I != E; ++I)
NAKAMURA Takumib0944392013-01-31 03:47:28 +0000598 if (I->hasAttribute(Attribute::Alignment))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000599 return I->getAlignment();
600 return 0;
601}
602
603unsigned AttributeSetNode::getStackAlignment() const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000604 for (iterator I = begin(), E = end(); I != E; ++I)
NAKAMURA Takumib0944392013-01-31 03:47:28 +0000605 if (I->hasAttribute(Attribute::StackAlignment))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000606 return I->getStackAlignment();
607 return 0;
608}
609
Hal Finkelb0407ba2014-07-18 15:51:28 +0000610uint64_t AttributeSetNode::getDereferenceableBytes() const {
611 for (iterator I = begin(), E = end(); I != E; ++I)
612 if (I->hasAttribute(Attribute::Dereferenceable))
613 return I->getDereferenceableBytes();
614 return 0;
615}
616
Sanjoy Das06cf33f2015-05-06 17:41:54 +0000617uint64_t AttributeSetNode::getDereferenceableOrNullBytes() const {
618 for (iterator I = begin(), E = end(); I != E; ++I)
619 if (I->hasAttribute(Attribute::DereferenceableOrNull))
620 return I->getDereferenceableOrNullBytes();
621 return 0;
622}
623
George Burgess IV278199f2016-04-12 01:05:35 +0000624std::pair<unsigned, Optional<unsigned>>
625AttributeSetNode::getAllocSizeArgs() const {
626 for (iterator I = begin(), E = end(); I != E; ++I)
627 if (I->hasAttribute(Attribute::AllocSize))
628 return I->getAllocSizeArgs();
629 return std::make_pair(0, 0);
630}
631
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +0000632std::string AttributeSetNode::getAsString(bool InAttrGrp) const {
Benjamin Kramer0baf8f42013-04-19 11:43:21 +0000633 std::string Str;
Benjamin Kramer741146b2013-07-11 12:13:16 +0000634 for (iterator I = begin(), E = end(); I != E; ++I) {
635 if (I != begin())
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +0000636 Str += ' ';
637 Str += I->getAsString(InAttrGrp);
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000638 }
639 return Str;
640}
641
Bill Wendlingd509a662013-01-29 00:34:06 +0000642//===----------------------------------------------------------------------===//
643// AttributeSetImpl Definition
644//===----------------------------------------------------------------------===//
645
Rafael Espindoladd275302013-04-30 16:53:38 +0000646uint64_t AttributeSetImpl::Raw(unsigned Index) const {
Bill Wendlingd509a662013-01-29 00:34:06 +0000647 for (unsigned I = 0, E = getNumAttributes(); I != E; ++I) {
648 if (getSlotIndex(I) != Index) continue;
Benjamin Kramer741146b2013-07-11 12:13:16 +0000649 const AttributeSetNode *ASN = getSlotNode(I);
Bill Wendling91226182013-02-02 00:52:44 +0000650 uint64_t Mask = 0;
Bill Wendlingd509a662013-01-29 00:34:06 +0000651
Benjamin Kramer741146b2013-07-11 12:13:16 +0000652 for (AttributeSetNode::iterator II = ASN->begin(),
Bill Wendling91226182013-02-02 00:52:44 +0000653 IE = ASN->end(); II != IE; ++II) {
654 Attribute Attr = *II;
Bill Wendlingd746e402013-02-10 23:18:05 +0000655
656 // This cannot handle string attributes.
657 if (Attr.isStringAttribute()) continue;
658
Bill Wendling3f12ac22013-02-05 22:37:24 +0000659 Attribute::AttrKind Kind = Attr.getKindAsEnum();
Bill Wendling91226182013-02-02 00:52:44 +0000660
Bill Wendling3f12ac22013-02-05 22:37:24 +0000661 if (Kind == Attribute::Alignment)
Bill Wendling91226182013-02-02 00:52:44 +0000662 Mask |= (Log2_32(ASN->getAlignment()) + 1) << 16;
Bill Wendling3f12ac22013-02-05 22:37:24 +0000663 else if (Kind == Attribute::StackAlignment)
Bill Wendling91226182013-02-02 00:52:44 +0000664 Mask |= (Log2_32(ASN->getStackAlignment()) + 1) << 26;
Hal Finkelb0407ba2014-07-18 15:51:28 +0000665 else if (Kind == Attribute::Dereferenceable)
666 llvm_unreachable("dereferenceable not supported in bit mask");
George Burgess IV278199f2016-04-12 01:05:35 +0000667 else if (Kind == Attribute::AllocSize)
668 llvm_unreachable("allocsize not supported in bit mask");
Bill Wendling91226182013-02-02 00:52:44 +0000669 else
Bill Wendling3f12ac22013-02-05 22:37:24 +0000670 Mask |= AttributeImpl::getAttrMask(Kind);
Bill Wendling91226182013-02-02 00:52:44 +0000671 }
672
673 return Mask;
Bill Wendlingd509a662013-01-29 00:34:06 +0000674 }
675
676 return 0;
677}
678
Yaron Kereneb2a2542016-01-29 20:50:44 +0000679LLVM_DUMP_METHOD void AttributeSetImpl::dump() const {
Peter Collingbourneabca2ec2013-08-02 22:34:30 +0000680 AttributeSet(const_cast<AttributeSetImpl *>(this)).dump();
681}
682
Bill Wendlingd509a662013-01-29 00:34:06 +0000683//===----------------------------------------------------------------------===//
684// AttributeSet Construction and Mutation Methods
685//===----------------------------------------------------------------------===//
686
Bill Wendling60011b82013-01-29 01:43:29 +0000687AttributeSet
688AttributeSet::getImpl(LLVMContext &C,
689 ArrayRef<std::pair<unsigned, AttributeSetNode*> > Attrs) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000690 LLVMContextImpl *pImpl = C.pImpl;
691 FoldingSetNodeID ID;
692 AttributeSetImpl::Profile(ID, Attrs);
693
694 void *InsertPoint;
695 AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
696
697 // If we didn't find any existing attributes of the same shape then
698 // create a new one and insert it.
699 if (!PA) {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000700 // Coallocate entries after the AttributeSetImpl itself.
James Y Knightaa365b22015-08-05 22:57:34 +0000701 void *Mem = ::operator new(
702 AttributeSetImpl::totalSizeToAlloc<IndexAttrPair>(Attrs.size()));
Benjamin Kramer741146b2013-07-11 12:13:16 +0000703 PA = new (Mem) AttributeSetImpl(C, Attrs);
Bill Wendlingd509a662013-01-29 00:34:06 +0000704 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
705 }
706
707 // Return the AttributesList that we found or created.
708 return AttributeSet(PA);
709}
710
711AttributeSet AttributeSet::get(LLVMContext &C,
712 ArrayRef<std::pair<unsigned, Attribute> > Attrs){
713 // If there are no attributes then return a null AttributesList pointer.
714 if (Attrs.empty())
715 return AttributeSet();
716
Craig Toppere30b8ca2016-01-03 19:43:40 +0000717 assert(std::is_sorted(Attrs.begin(), Attrs.end(),
718 [](const std::pair<unsigned, Attribute> &LHS,
719 const std::pair<unsigned, Attribute> &RHS) {
720 return LHS.first < RHS.first;
721 }) && "Misordered Attributes list!");
722 assert(std::none_of(Attrs.begin(), Attrs.end(),
723 [](const std::pair<unsigned, Attribute> &Pair) {
724 return Pair.second.hasAttribute(Attribute::None);
725 }) && "Pointless attribute!");
Bill Wendlingd509a662013-01-29 00:34:06 +0000726
727 // Create a vector if (unsigned, AttributeSetNode*) pairs from the attributes
728 // list.
729 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrPairVec;
730 for (ArrayRef<std::pair<unsigned, Attribute> >::iterator I = Attrs.begin(),
731 E = Attrs.end(); I != E; ) {
732 unsigned Index = I->first;
733 SmallVector<Attribute, 4> AttrVec;
NAKAMURA Takumif05d2f22013-01-29 15:18:16 +0000734 while (I != E && I->first == Index) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000735 AttrVec.push_back(I->second);
736 ++I;
737 }
738
739 AttrPairVec.push_back(std::make_pair(Index,
740 AttributeSetNode::get(C, AttrVec)));
741 }
742
743 return getImpl(C, AttrPairVec);
744}
745
746AttributeSet AttributeSet::get(LLVMContext &C,
747 ArrayRef<std::pair<unsigned,
748 AttributeSetNode*> > Attrs) {
749 // If there are no attributes then return a null AttributesList pointer.
750 if (Attrs.empty())
751 return AttributeSet();
752
753 return getImpl(C, Attrs);
754}
755
David Majnemercf63a792014-05-03 23:00:35 +0000756AttributeSet AttributeSet::get(LLVMContext &C, unsigned Index,
757 const AttrBuilder &B) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000758 if (!B.hasAttributes())
759 return AttributeSet();
Bill Wendlingf7134812013-01-29 01:02:03 +0000760
Bill Wendlingae89a0f2013-02-05 23:48:36 +0000761 // Add target-independent attributes.
Bill Wendlingf7134812013-01-29 01:02:03 +0000762 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
Benjamin Kramer45e7d532013-02-16 19:13:18 +0000763 for (Attribute::AttrKind Kind = Attribute::None;
Benjamin Kramer6f37dac2013-02-16 19:22:28 +0000764 Kind != Attribute::EndAttrKinds; Kind = Attribute::AttrKind(Kind + 1)) {
Benjamin Kramer45e7d532013-02-16 19:13:18 +0000765 if (!B.contains(Kind))
766 continue;
767
George Burgess IV500d3032015-12-16 05:21:02 +0000768 Attribute Attr;
769 switch (Kind) {
770 case Attribute::Alignment:
771 Attr = Attribute::getWithAlignment(C, B.getAlignment());
772 break;
773 case Attribute::StackAlignment:
774 Attr = Attribute::getWithStackAlignment(C, B.getStackAlignment());
775 break;
776 case Attribute::Dereferenceable:
777 Attr = Attribute::getWithDereferenceableBytes(
778 C, B.getDereferenceableBytes());
779 break;
780 case Attribute::DereferenceableOrNull:
781 Attr = Attribute::getWithDereferenceableOrNullBytes(
782 C, B.getDereferenceableOrNullBytes());
783 break;
George Burgess IV278199f2016-04-12 01:05:35 +0000784 case Attribute::AllocSize: {
785 auto A = B.getAllocSizeArgs();
786 Attr = Attribute::getWithAllocSizeArgs(C, A.first, A.second);
787 break;
788 }
George Burgess IV500d3032015-12-16 05:21:02 +0000789 default:
790 Attr = Attribute::get(C, Kind);
791 }
792 Attrs.push_back(std::make_pair(Index, Attr));
Bill Wendlingf7134812013-01-29 01:02:03 +0000793 }
794
Bill Wendlingae89a0f2013-02-05 23:48:36 +0000795 // Add target-dependent (string) attributes.
David Majnemercf63a792014-05-03 23:00:35 +0000796 for (const AttrBuilder::td_type &TDA : B.td_attrs())
797 Attrs.push_back(
798 std::make_pair(Index, Attribute::get(C, TDA.first, TDA.second)));
Bill Wendlingae89a0f2013-02-05 23:48:36 +0000799
Bill Wendlingf7134812013-01-29 01:02:03 +0000800 return get(C, Attrs);
Bill Wendlingd509a662013-01-29 00:34:06 +0000801}
802
Bill Wendling211316c2013-04-18 20:17:28 +0000803AttributeSet AttributeSet::get(LLVMContext &C, unsigned Index,
Bill Wendlingd509a662013-01-29 00:34:06 +0000804 ArrayRef<Attribute::AttrKind> Kind) {
805 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
George Burgess IV500d3032015-12-16 05:21:02 +0000806 for (Attribute::AttrKind K : Kind)
807 Attrs.push_back(std::make_pair(Index, Attribute::get(C, K)));
Bill Wendlingd509a662013-01-29 00:34:06 +0000808 return get(C, Attrs);
809}
810
811AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<AttributeSet> Attrs) {
812 if (Attrs.empty()) return AttributeSet();
Peter Collingbournebd6c7452013-08-02 22:29:40 +0000813 if (Attrs.size() == 1) return Attrs[0];
Bill Wendlingd509a662013-01-29 00:34:06 +0000814
815 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrNodeVec;
Peter Collingbournebd6c7452013-08-02 22:29:40 +0000816 AttributeSetImpl *A0 = Attrs[0].pImpl;
817 if (A0)
818 AttrNodeVec.append(A0->getNode(0), A0->getNode(A0->getNumAttributes()));
819 // Copy all attributes from Attrs into AttrNodeVec while keeping AttrNodeVec
820 // ordered by index. Because we know that each list in Attrs is ordered by
821 // index we only need to merge each successive list in rather than doing a
822 // full sort.
823 for (unsigned I = 1, E = Attrs.size(); I != E; ++I) {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000824 AttributeSetImpl *AS = Attrs[I].pImpl;
825 if (!AS) continue;
Peter Collingbournebd6c7452013-08-02 22:29:40 +0000826 SmallVector<std::pair<unsigned, AttributeSetNode *>, 8>::iterator
827 ANVI = AttrNodeVec.begin(), ANVE;
James Y Knightaa365b22015-08-05 22:57:34 +0000828 for (const IndexAttrPair *AI = AS->getNode(0),
829 *AE = AS->getNode(AS->getNumAttributes());
Peter Collingbournebd6c7452013-08-02 22:29:40 +0000830 AI != AE; ++AI) {
831 ANVE = AttrNodeVec.end();
832 while (ANVI != ANVE && ANVI->first <= AI->first)
833 ++ANVI;
834 ANVI = AttrNodeVec.insert(ANVI, *AI) + 1;
835 }
Bill Wendlingd509a662013-01-29 00:34:06 +0000836 }
837
838 return getImpl(C, AttrNodeVec);
839}
840
Bill Wendling211316c2013-04-18 20:17:28 +0000841AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
Bill Wendlingd509a662013-01-29 00:34:06 +0000842 Attribute::AttrKind Attr) const {
Bill Wendling211316c2013-04-18 20:17:28 +0000843 if (hasAttribute(Index, Attr)) return *this;
844 return addAttributes(C, Index, AttributeSet::get(C, Index, Attr));
Bill Wendlingd509a662013-01-29 00:34:06 +0000845}
846
Bill Wendling211316c2013-04-18 20:17:28 +0000847AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
Reed Kotler795c7b42013-03-13 20:20:08 +0000848 StringRef Kind) const {
849 llvm::AttrBuilder B;
850 B.addAttribute(Kind);
Bill Wendling211316c2013-04-18 20:17:28 +0000851 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
Reed Kotler795c7b42013-03-13 20:20:08 +0000852}
853
Bill Wendling3b2f6102013-07-25 18:34:24 +0000854AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
855 StringRef Kind, StringRef Value) const {
856 llvm::AttrBuilder B;
857 B.addAttribute(Kind, Value);
858 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
859}
860
Akira Hatanaka237916b2015-12-02 06:58:49 +0000861AttributeSet AttributeSet::addAttribute(LLVMContext &C,
862 ArrayRef<unsigned> Indices,
863 Attribute A) const {
864 unsigned I = 0, E = pImpl ? pImpl->getNumAttributes() : 0;
865 auto IdxI = Indices.begin(), IdxE = Indices.end();
866 SmallVector<AttributeSet, 4> AttrSet;
867
868 while (I != E && IdxI != IdxE) {
869 if (getSlotIndex(I) < *IdxI)
870 AttrSet.emplace_back(getSlotAttributes(I++));
871 else if (getSlotIndex(I) > *IdxI)
872 AttrSet.emplace_back(AttributeSet::get(C, std::make_pair(*IdxI++, A)));
873 else {
874 AttrBuilder B(getSlotAttributes(I), *IdxI);
875 B.addAttribute(A);
876 AttrSet.emplace_back(AttributeSet::get(C, *IdxI, B));
877 ++I;
878 ++IdxI;
879 }
880 }
881
882 while (I != E)
883 AttrSet.emplace_back(getSlotAttributes(I++));
884
885 while (IdxI != IdxE)
886 AttrSet.emplace_back(AttributeSet::get(C, std::make_pair(*IdxI++, A)));
887
888 return get(C, AttrSet);
889}
890
Bill Wendling211316c2013-04-18 20:17:28 +0000891AttributeSet AttributeSet::addAttributes(LLVMContext &C, unsigned Index,
Bill Wendlingd509a662013-01-29 00:34:06 +0000892 AttributeSet Attrs) const {
893 if (!pImpl) return Attrs;
894 if (!Attrs.pImpl) return *this;
895
896#ifndef NDEBUG
897 // FIXME it is not obvious how this should work for alignment. For now, say
898 // we can't change a known alignment.
Bill Wendling211316c2013-04-18 20:17:28 +0000899 unsigned OldAlign = getParamAlignment(Index);
900 unsigned NewAlign = Attrs.getParamAlignment(Index);
Bill Wendlingd509a662013-01-29 00:34:06 +0000901 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
902 "Attempt to change alignment!");
903#endif
904
905 // Add the attribute slots before the one we're trying to add.
906 SmallVector<AttributeSet, 4> AttrSet;
907 uint64_t NumAttrs = pImpl->getNumAttributes();
908 AttributeSet AS;
909 uint64_t LastIndex = 0;
910 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
Bill Wendling211316c2013-04-18 20:17:28 +0000911 if (getSlotIndex(I) >= Index) {
912 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
Bill Wendlingd509a662013-01-29 00:34:06 +0000913 break;
914 }
915 LastIndex = I + 1;
916 AttrSet.push_back(getSlotAttributes(I));
917 }
918
919 // Now add the attribute into the correct slot. There may already be an
920 // AttributeSet there.
Bill Wendling211316c2013-04-18 20:17:28 +0000921 AttrBuilder B(AS, Index);
Bill Wendlingd509a662013-01-29 00:34:06 +0000922
923 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
Bill Wendling211316c2013-04-18 20:17:28 +0000924 if (Attrs.getSlotIndex(I) == Index) {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000925 for (AttributeSetImpl::iterator II = Attrs.pImpl->begin(I),
Bill Wendlingd509a662013-01-29 00:34:06 +0000926 IE = Attrs.pImpl->end(I); II != IE; ++II)
Bill Wendling23804da2013-01-31 23:38:01 +0000927 B.addAttribute(*II);
Bill Wendlingd509a662013-01-29 00:34:06 +0000928 break;
929 }
930
Bill Wendling211316c2013-04-18 20:17:28 +0000931 AttrSet.push_back(AttributeSet::get(C, Index, B));
Bill Wendlingd509a662013-01-29 00:34:06 +0000932
933 // Add the remaining attribute slots.
934 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
935 AttrSet.push_back(getSlotAttributes(I));
936
937 return get(C, AttrSet);
938}
939
Bill Wendling211316c2013-04-18 20:17:28 +0000940AttributeSet AttributeSet::removeAttribute(LLVMContext &C, unsigned Index,
Bill Wendlingd509a662013-01-29 00:34:06 +0000941 Attribute::AttrKind Attr) const {
Bill Wendling211316c2013-04-18 20:17:28 +0000942 if (!hasAttribute(Index, Attr)) return *this;
943 return removeAttributes(C, Index, AttributeSet::get(C, Index, Attr));
Bill Wendlingd509a662013-01-29 00:34:06 +0000944}
945
Bill Wendling211316c2013-04-18 20:17:28 +0000946AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Index,
Bill Wendlingd509a662013-01-29 00:34:06 +0000947 AttributeSet Attrs) const {
948 if (!pImpl) return AttributeSet();
949 if (!Attrs.pImpl) return *this;
950
Pete Cooper67cf9a72015-11-19 05:56:52 +0000951 // FIXME it is not obvious how this should work for alignment.
952 // For now, say we can't pass in alignment, which no current use does.
953 assert(!Attrs.hasAttribute(Index, Attribute::Alignment) &&
954 "Attempt to change alignment!");
955
Bill Wendlingd509a662013-01-29 00:34:06 +0000956 // Add the attribute slots before the one we're trying to add.
957 SmallVector<AttributeSet, 4> AttrSet;
958 uint64_t NumAttrs = pImpl->getNumAttributes();
959 AttributeSet AS;
960 uint64_t LastIndex = 0;
961 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
Bill Wendling211316c2013-04-18 20:17:28 +0000962 if (getSlotIndex(I) >= Index) {
963 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
Bill Wendlingd509a662013-01-29 00:34:06 +0000964 break;
965 }
966 LastIndex = I + 1;
967 AttrSet.push_back(getSlotAttributes(I));
968 }
969
Bill Wendlingd2196752013-01-30 23:07:40 +0000970 // Now remove the attribute from the correct slot. There may already be an
Bill Wendlingd509a662013-01-29 00:34:06 +0000971 // AttributeSet there.
Bill Wendling211316c2013-04-18 20:17:28 +0000972 AttrBuilder B(AS, Index);
Bill Wendlingd509a662013-01-29 00:34:06 +0000973
974 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
Bill Wendling211316c2013-04-18 20:17:28 +0000975 if (Attrs.getSlotIndex(I) == Index) {
976 B.removeAttributes(Attrs.pImpl->getSlotAttributes(I), Index);
Bill Wendlingd509a662013-01-29 00:34:06 +0000977 break;
978 }
979
Bill Wendling211316c2013-04-18 20:17:28 +0000980 AttrSet.push_back(AttributeSet::get(C, Index, B));
Bill Wendlingd509a662013-01-29 00:34:06 +0000981
982 // Add the remaining attribute slots.
983 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
984 AttrSet.push_back(getSlotAttributes(I));
985
986 return get(C, AttrSet);
987}
988
Pete Cooperd2a44612015-05-06 23:19:43 +0000989AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Index,
990 const AttrBuilder &Attrs) const {
991 if (!pImpl) return AttributeSet();
992
993 // FIXME it is not obvious how this should work for alignment.
994 // For now, say we can't pass in alignment, which no current use does.
995 assert(!Attrs.hasAlignmentAttr() && "Attempt to change alignment!");
996
997 // Add the attribute slots before the one we're trying to add.
998 SmallVector<AttributeSet, 4> AttrSet;
999 uint64_t NumAttrs = pImpl->getNumAttributes();
1000 AttributeSet AS;
1001 uint64_t LastIndex = 0;
1002 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
1003 if (getSlotIndex(I) >= Index) {
1004 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
1005 break;
1006 }
1007 LastIndex = I + 1;
1008 AttrSet.push_back(getSlotAttributes(I));
1009 }
1010
1011 // Now remove the attribute from the correct slot. There may already be an
1012 // AttributeSet there.
1013 AttrBuilder B(AS, Index);
1014 B.remove(Attrs);
1015
1016 AttrSet.push_back(AttributeSet::get(C, Index, B));
1017
1018 // Add the remaining attribute slots.
1019 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
1020 AttrSet.push_back(getSlotAttributes(I));
1021
1022 return get(C, AttrSet);
1023}
1024
Ramkumar Ramachandra8fcb4982015-02-14 19:37:54 +00001025AttributeSet AttributeSet::addDereferenceableAttr(LLVMContext &C, unsigned Index,
1026 uint64_t Bytes) const {
1027 llvm::AttrBuilder B;
1028 B.addDereferenceableAttr(Bytes);
1029 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
1030}
1031
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001032AttributeSet AttributeSet::addDereferenceableOrNullAttr(LLVMContext &C,
1033 unsigned Index,
1034 uint64_t Bytes) const {
1035 llvm::AttrBuilder B;
1036 B.addDereferenceableOrNullAttr(Bytes);
1037 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
1038}
1039
George Burgess IV278199f2016-04-12 01:05:35 +00001040AttributeSet
1041AttributeSet::addAllocSizeAttr(LLVMContext &C, unsigned Index,
1042 unsigned ElemSizeArg,
1043 const Optional<unsigned> &NumElemsArg) {
1044 llvm::AttrBuilder B;
1045 B.addAllocSizeAttr(ElemSizeArg, NumElemsArg);
1046 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
1047}
1048
Bill Wendlingd509a662013-01-29 00:34:06 +00001049//===----------------------------------------------------------------------===//
1050// AttributeSet Accessor Methods
1051//===----------------------------------------------------------------------===//
1052
Bill Wendling5d020a32013-02-10 05:00:40 +00001053LLVMContext &AttributeSet::getContext() const {
1054 return pImpl->getContext();
1055}
1056
Bill Wendling211316c2013-04-18 20:17:28 +00001057AttributeSet AttributeSet::getParamAttributes(unsigned Index) const {
1058 return pImpl && hasAttributes(Index) ?
Bill Wendlingd509a662013-01-29 00:34:06 +00001059 AttributeSet::get(pImpl->getContext(),
Bill Wendlingf2955aa2013-01-29 03:20:31 +00001060 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendling211316c2013-04-18 20:17:28 +00001061 std::make_pair(Index, getAttributes(Index)))) :
Bill Wendlingd509a662013-01-29 00:34:06 +00001062 AttributeSet();
1063}
1064
1065AttributeSet AttributeSet::getRetAttributes() const {
1066 return pImpl && hasAttributes(ReturnIndex) ?
1067 AttributeSet::get(pImpl->getContext(),
Bill Wendlingf2955aa2013-01-29 03:20:31 +00001068 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendlingd509a662013-01-29 00:34:06 +00001069 std::make_pair(ReturnIndex,
1070 getAttributes(ReturnIndex)))) :
1071 AttributeSet();
1072}
1073
1074AttributeSet AttributeSet::getFnAttributes() const {
1075 return pImpl && hasAttributes(FunctionIndex) ?
1076 AttributeSet::get(pImpl->getContext(),
Bill Wendlingf2955aa2013-01-29 03:20:31 +00001077 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendlingd509a662013-01-29 00:34:06 +00001078 std::make_pair(FunctionIndex,
1079 getAttributes(FunctionIndex)))) :
1080 AttributeSet();
1081}
1082
1083bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{
Bill Wendlingf2955aa2013-01-29 03:20:31 +00001084 AttributeSetNode *ASN = getAttributes(Index);
George Burgess IV500d3032015-12-16 05:21:02 +00001085 return ASN && ASN->hasAttribute(Kind);
Bill Wendlingd509a662013-01-29 00:34:06 +00001086}
1087
Bill Wendlingbce7b972013-02-13 08:42:21 +00001088bool AttributeSet::hasAttribute(unsigned Index, StringRef Kind) const {
1089 AttributeSetNode *ASN = getAttributes(Index);
George Burgess IV500d3032015-12-16 05:21:02 +00001090 return ASN && ASN->hasAttribute(Kind);
Bill Wendlingbce7b972013-02-13 08:42:21 +00001091}
1092
Bill Wendlingd509a662013-01-29 00:34:06 +00001093bool AttributeSet::hasAttributes(unsigned Index) const {
Bill Wendlingf2955aa2013-01-29 03:20:31 +00001094 AttributeSetNode *ASN = getAttributes(Index);
George Burgess IV500d3032015-12-16 05:21:02 +00001095 return ASN && ASN->hasAttributes();
Bill Wendlingd509a662013-01-29 00:34:06 +00001096}
1097
Matthias Braun33282812016-01-29 22:25:19 +00001098bool AttributeSet::hasFnAttribute(Attribute::AttrKind Kind) const {
1099 return pImpl && pImpl->hasFnAttribute(Kind);
1100}
1101
Bill Wendlingd509a662013-01-29 00:34:06 +00001102bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr) const {
Craig Topperc6207612014-04-09 06:08:46 +00001103 if (!pImpl) return false;
Bill Wendlingd509a662013-01-29 00:34:06 +00001104
1105 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
Benjamin Kramer741146b2013-07-11 12:13:16 +00001106 for (AttributeSetImpl::iterator II = pImpl->begin(I),
Bill Wendlingd509a662013-01-29 00:34:06 +00001107 IE = pImpl->end(I); II != IE; ++II)
NAKAMURA Takumib0944392013-01-31 03:47:28 +00001108 if (II->hasAttribute(Attr))
Bill Wendlingd509a662013-01-29 00:34:06 +00001109 return true;
1110
1111 return false;
1112}
1113
Bill Wendlingbce7b972013-02-13 08:42:21 +00001114Attribute AttributeSet::getAttribute(unsigned Index,
1115 Attribute::AttrKind Kind) const {
1116 AttributeSetNode *ASN = getAttributes(Index);
1117 return ASN ? ASN->getAttribute(Kind) : Attribute();
1118}
1119
1120Attribute AttributeSet::getAttribute(unsigned Index,
1121 StringRef Kind) const {
1122 AttributeSetNode *ASN = getAttributes(Index);
1123 return ASN ? ASN->getAttribute(Kind) : Attribute();
1124}
1125
Bill Wendlingf2955aa2013-01-29 03:20:31 +00001126unsigned AttributeSet::getParamAlignment(unsigned Index) const {
1127 AttributeSetNode *ASN = getAttributes(Index);
1128 return ASN ? ASN->getAlignment() : 0;
Bill Wendlingd509a662013-01-29 00:34:06 +00001129}
1130
1131unsigned AttributeSet::getStackAlignment(unsigned Index) const {
Bill Wendlingf2955aa2013-01-29 03:20:31 +00001132 AttributeSetNode *ASN = getAttributes(Index);
1133 return ASN ? ASN->getStackAlignment() : 0;
Bill Wendlingd509a662013-01-29 00:34:06 +00001134}
1135
Hal Finkelb0407ba2014-07-18 15:51:28 +00001136uint64_t AttributeSet::getDereferenceableBytes(unsigned Index) const {
1137 AttributeSetNode *ASN = getAttributes(Index);
1138 return ASN ? ASN->getDereferenceableBytes() : 0;
1139}
1140
Sanjoy Das06cf33f2015-05-06 17:41:54 +00001141uint64_t AttributeSet::getDereferenceableOrNullBytes(unsigned Index) const {
1142 AttributeSetNode *ASN = getAttributes(Index);
1143 return ASN ? ASN->getDereferenceableOrNullBytes() : 0;
1144}
1145
George Burgess IV278199f2016-04-12 01:05:35 +00001146std::pair<unsigned, Optional<unsigned>>
1147AttributeSet::getAllocSizeArgs(unsigned Index) const {
1148 AttributeSetNode *ASN = getAttributes(Index);
1149 return ASN ? ASN->getAllocSizeArgs() : std::make_pair(0, 0);
1150}
1151
1152std::string AttributeSet::getAsString(unsigned Index, bool InAttrGrp) const {
Bill Wendlingf2955aa2013-01-29 03:20:31 +00001153 AttributeSetNode *ASN = getAttributes(Index);
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +00001154 return ASN ? ASN->getAsString(InAttrGrp) : std::string("");
Bill Wendlingd509a662013-01-29 00:34:06 +00001155}
1156
Bill Wendling211316c2013-04-18 20:17:28 +00001157AttributeSetNode *AttributeSet::getAttributes(unsigned Index) const {
Craig Topperc6207612014-04-09 06:08:46 +00001158 if (!pImpl) return nullptr;
Bill Wendlingd509a662013-01-29 00:34:06 +00001159
Bill Wendlingf2955aa2013-01-29 03:20:31 +00001160 // Loop through to find the attribute node we want.
1161 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
Bill Wendling211316c2013-04-18 20:17:28 +00001162 if (pImpl->getSlotIndex(I) == Index)
Bill Wendlingf2955aa2013-01-29 03:20:31 +00001163 return pImpl->getSlotNode(I);
Bill Wendlingd509a662013-01-29 00:34:06 +00001164
Craig Topperc6207612014-04-09 06:08:46 +00001165 return nullptr;
Bill Wendlingd509a662013-01-29 00:34:06 +00001166}
1167
Bill Wendling211316c2013-04-18 20:17:28 +00001168AttributeSet::iterator AttributeSet::begin(unsigned Slot) const {
Bill Wendlinga9174862013-01-31 23:53:05 +00001169 if (!pImpl)
1170 return ArrayRef<Attribute>().begin();
Bill Wendling211316c2013-04-18 20:17:28 +00001171 return pImpl->begin(Slot);
Bill Wendlinga9174862013-01-31 23:53:05 +00001172}
1173
Bill Wendling211316c2013-04-18 20:17:28 +00001174AttributeSet::iterator AttributeSet::end(unsigned Slot) const {
Bill Wendlinga9174862013-01-31 23:53:05 +00001175 if (!pImpl)
1176 return ArrayRef<Attribute>().end();
Bill Wendling211316c2013-04-18 20:17:28 +00001177 return pImpl->end(Slot);
Bill Wendlinga9174862013-01-31 23:53:05 +00001178}
1179
Bill Wendlingd509a662013-01-29 00:34:06 +00001180//===----------------------------------------------------------------------===//
1181// AttributeSet Introspection Methods
1182//===----------------------------------------------------------------------===//
1183
Bill Wendlingd509a662013-01-29 00:34:06 +00001184unsigned AttributeSet::getNumSlots() const {
1185 return pImpl ? pImpl->getNumAttributes() : 0;
1186}
1187
Rafael Espindoladd275302013-04-30 16:53:38 +00001188unsigned AttributeSet::getSlotIndex(unsigned Slot) const {
Bill Wendlingd509a662013-01-29 00:34:06 +00001189 assert(pImpl && Slot < pImpl->getNumAttributes() &&
1190 "Slot # out of range!");
1191 return pImpl->getSlotIndex(Slot);
1192}
1193
1194AttributeSet AttributeSet::getSlotAttributes(unsigned Slot) const {
1195 assert(pImpl && Slot < pImpl->getNumAttributes() &&
1196 "Slot # out of range!");
1197 return pImpl->getSlotAttributes(Slot);
1198}
1199
1200uint64_t AttributeSet::Raw(unsigned Index) const {
1201 // FIXME: Remove this.
1202 return pImpl ? pImpl->Raw(Index) : 0;
1203}
1204
Yaron Kereneb2a2542016-01-29 20:50:44 +00001205LLVM_DUMP_METHOD void AttributeSet::dump() const {
Bill Wendlingd509a662013-01-29 00:34:06 +00001206 dbgs() << "PAL[\n";
1207
1208 for (unsigned i = 0, e = getNumSlots(); i < e; ++i) {
1209 uint64_t Index = getSlotIndex(i);
1210 dbgs() << " { ";
1211 if (Index == ~0U)
1212 dbgs() << "~0U";
1213 else
1214 dbgs() << Index;
1215 dbgs() << " => " << getAsString(Index) << " }\n";
1216 }
1217
1218 dbgs() << "]\n";
1219}
1220
Bill Wendlingabf3feb2012-10-05 06:44:41 +00001221//===----------------------------------------------------------------------===//
Bill Wendlingcd330342013-01-04 23:27:34 +00001222// AttrBuilder Method Implementations
Bill Wendlingabf3feb2012-10-05 06:44:41 +00001223//===----------------------------------------------------------------------===//
1224
Bill Wendling211316c2013-04-18 20:17:28 +00001225AttrBuilder::AttrBuilder(AttributeSet AS, unsigned Index)
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001226 : Attrs(0), Alignment(0), StackAlignment(0), DerefBytes(0),
George Burgess IV278199f2016-04-12 01:05:35 +00001227 DerefOrNullBytes(0), AllocSizeArgs(0) {
Bill Wendling56b0b2a2013-01-27 21:23:46 +00001228 AttributeSetImpl *pImpl = AS.pImpl;
Bill Wendling096f5442013-01-07 08:24:35 +00001229 if (!pImpl) return;
1230
Bill Wendling9eb689c2013-01-28 00:21:34 +00001231 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I) {
Bill Wendling211316c2013-04-18 20:17:28 +00001232 if (pImpl->getSlotIndex(I) != Index) continue;
Bill Wendling096f5442013-01-07 08:24:35 +00001233
Benjamin Kramer741146b2013-07-11 12:13:16 +00001234 for (AttributeSetImpl::iterator II = pImpl->begin(I),
Bill Wendling9eb689c2013-01-28 00:21:34 +00001235 IE = pImpl->end(I); II != IE; ++II)
Bill Wendling23804da2013-01-31 23:38:01 +00001236 addAttribute(*II);
Bill Wendling9eb689c2013-01-28 00:21:34 +00001237
1238 break;
1239 }
Bill Wendling096f5442013-01-07 08:24:35 +00001240}
1241
Bill Wendlingcd330342013-01-04 23:27:34 +00001242void AttrBuilder::clear() {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001243 Attrs.reset();
Sanjoy Das88d0fde2015-09-03 22:27:42 +00001244 TargetDepAttrs.clear();
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001245 Alignment = StackAlignment = DerefBytes = DerefOrNullBytes = 0;
George Burgess IV278199f2016-04-12 01:05:35 +00001246 AllocSizeArgs = 0;
Bill Wendlingcd330342013-01-04 23:27:34 +00001247}
1248
1249AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001250 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
Bill Wendling1c7cc8a2013-01-31 23:16:25 +00001251 assert(Val != Attribute::Alignment && Val != Attribute::StackAlignment &&
George Burgess IV278199f2016-04-12 01:05:35 +00001252 Val != Attribute::Dereferenceable && Val != Attribute::AllocSize &&
Hal Finkelb0407ba2014-07-18 15:51:28 +00001253 "Adding integer attribute without adding a value!");
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001254 Attrs[Val] = true;
Bill Wendling7c04e042012-10-09 19:01:18 +00001255 return *this;
Bill Wendlingabf3feb2012-10-05 06:44:41 +00001256}
1257
Bill Wendling23804da2013-01-31 23:38:01 +00001258AttrBuilder &AttrBuilder::addAttribute(Attribute Attr) {
Bill Wendling0a437302013-02-10 10:13:23 +00001259 if (Attr.isStringAttribute()) {
1260 addAttribute(Attr.getKindAsString(), Attr.getValueAsString());
1261 return *this;
1262 }
1263
Bill Wendling3f12ac22013-02-05 22:37:24 +00001264 Attribute::AttrKind Kind = Attr.getKindAsEnum();
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001265 Attrs[Kind] = true;
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001266
Bill Wendling3f12ac22013-02-05 22:37:24 +00001267 if (Kind == Attribute::Alignment)
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001268 Alignment = Attr.getAlignment();
Bill Wendling3f12ac22013-02-05 22:37:24 +00001269 else if (Kind == Attribute::StackAlignment)
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001270 StackAlignment = Attr.getStackAlignment();
Hal Finkelb0407ba2014-07-18 15:51:28 +00001271 else if (Kind == Attribute::Dereferenceable)
1272 DerefBytes = Attr.getDereferenceableBytes();
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001273 else if (Kind == Attribute::DereferenceableOrNull)
1274 DerefOrNullBytes = Attr.getDereferenceableOrNullBytes();
George Burgess IV278199f2016-04-12 01:05:35 +00001275 else if (Kind == Attribute::AllocSize)
1276 AllocSizeArgs = Attr.getValueAsInt();
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001277 return *this;
1278}
1279
Bill Wendlingb9c5b1a2013-02-05 08:09:32 +00001280AttrBuilder &AttrBuilder::addAttribute(StringRef A, StringRef V) {
1281 TargetDepAttrs[A] = V;
1282 return *this;
1283}
1284
Bill Wendling23804da2013-01-31 23:38:01 +00001285AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001286 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
1287 Attrs[Val] = false;
Bill Wendling23804da2013-01-31 23:38:01 +00001288
1289 if (Val == Attribute::Alignment)
1290 Alignment = 0;
1291 else if (Val == Attribute::StackAlignment)
1292 StackAlignment = 0;
Hal Finkelb0407ba2014-07-18 15:51:28 +00001293 else if (Val == Attribute::Dereferenceable)
1294 DerefBytes = 0;
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001295 else if (Val == Attribute::DereferenceableOrNull)
1296 DerefOrNullBytes = 0;
George Burgess IV278199f2016-04-12 01:05:35 +00001297 else if (Val == Attribute::AllocSize)
1298 AllocSizeArgs = 0;
Bill Wendling23804da2013-01-31 23:38:01 +00001299
1300 return *this;
1301}
1302
Bill Wendlingd2196752013-01-30 23:07:40 +00001303AttrBuilder &AttrBuilder::removeAttributes(AttributeSet A, uint64_t Index) {
Bill Wendling211316c2013-04-18 20:17:28 +00001304 unsigned Slot = ~0U;
Bill Wendlingf1c94e32013-02-01 00:13:50 +00001305 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
1306 if (A.getSlotIndex(I) == Index) {
Bill Wendling211316c2013-04-18 20:17:28 +00001307 Slot = I;
Bill Wendlingf1c94e32013-02-01 00:13:50 +00001308 break;
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001309 }
Bill Wendlingf1c94e32013-02-01 00:13:50 +00001310
Bill Wendling211316c2013-04-18 20:17:28 +00001311 assert(Slot != ~0U && "Couldn't find index in AttributeSet!");
Bill Wendlingf1c94e32013-02-01 00:13:50 +00001312
Bill Wendling211316c2013-04-18 20:17:28 +00001313 for (AttributeSet::iterator I = A.begin(Slot), E = A.end(Slot); I != E; ++I) {
Bill Wendling7cde51d2013-02-12 07:56:49 +00001314 Attribute Attr = *I;
Hal Finkele15442c2014-07-18 06:51:55 +00001315 if (Attr.isEnumAttribute() || Attr.isIntAttribute()) {
George Burgess IV500d3032015-12-16 05:21:02 +00001316 removeAttribute(Attr.getKindAsEnum());
Bill Wendling7cde51d2013-02-12 07:56:49 +00001317 } else {
1318 assert(Attr.isStringAttribute() && "Invalid attribute type!");
George Burgess IV500d3032015-12-16 05:21:02 +00001319 removeAttribute(Attr.getKindAsString());
Bill Wendling7cde51d2013-02-12 07:56:49 +00001320 }
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001321 }
1322
1323 return *this;
1324}
1325
Bill Wendlingb9c5b1a2013-02-05 08:09:32 +00001326AttrBuilder &AttrBuilder::removeAttribute(StringRef A) {
1327 std::map<std::string, std::string>::iterator I = TargetDepAttrs.find(A);
1328 if (I != TargetDepAttrs.end())
1329 TargetDepAttrs.erase(I);
1330 return *this;
1331}
1332
George Burgess IV278199f2016-04-12 01:05:35 +00001333std::pair<unsigned, Optional<unsigned>> AttrBuilder::getAllocSizeArgs() const {
1334 return unpackAllocSizeArgs(AllocSizeArgs);
1335}
1336
Bill Wendling50d27842012-10-15 20:35:56 +00001337AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
Bill Wendlingabd5ba22012-10-14 03:58:29 +00001338 if (Align == 0) return *this;
Bill Wendlingcd330342013-01-04 23:27:34 +00001339
Bill Wendlingabf3feb2012-10-05 06:44:41 +00001340 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1341 assert(Align <= 0x40000000 && "Alignment too large.");
Bill Wendlingcd330342013-01-04 23:27:34 +00001342
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001343 Attrs[Attribute::Alignment] = true;
Bill Wendlingcd330342013-01-04 23:27:34 +00001344 Alignment = Align;
Bill Wendlingabd5ba22012-10-14 03:58:29 +00001345 return *this;
Chris Lattner3e13b8c2008-01-02 23:42:30 +00001346}
1347
Bill Wendlingcd330342013-01-04 23:27:34 +00001348AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
1349 // Default alignment, allow the target to define how to align it.
1350 if (Align == 0) return *this;
1351
1352 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1353 assert(Align <= 0x100 && "Alignment too large.");
1354
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001355 Attrs[Attribute::StackAlignment] = true;
Bill Wendlingcd330342013-01-04 23:27:34 +00001356 StackAlignment = Align;
1357 return *this;
1358}
1359
Hal Finkelb0407ba2014-07-18 15:51:28 +00001360AttrBuilder &AttrBuilder::addDereferenceableAttr(uint64_t Bytes) {
1361 if (Bytes == 0) return *this;
1362
1363 Attrs[Attribute::Dereferenceable] = true;
1364 DerefBytes = Bytes;
1365 return *this;
1366}
1367
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001368AttrBuilder &AttrBuilder::addDereferenceableOrNullAttr(uint64_t Bytes) {
1369 if (Bytes == 0)
1370 return *this;
1371
1372 Attrs[Attribute::DereferenceableOrNull] = true;
1373 DerefOrNullBytes = Bytes;
1374 return *this;
1375}
1376
George Burgess IV278199f2016-04-12 01:05:35 +00001377AttrBuilder &AttrBuilder::addAllocSizeAttr(unsigned ElemSize,
1378 const Optional<unsigned> &NumElems) {
1379 return addAllocSizeAttrFromRawRepr(packAllocSizeArgs(ElemSize, NumElems));
1380}
1381
1382AttrBuilder &AttrBuilder::addAllocSizeAttrFromRawRepr(uint64_t RawArgs) {
1383 // (0, 0) is our "not present" value, so we need to check for it here.
1384 assert(RawArgs && "Invalid allocsize arguments -- given allocsize(0, 0)");
1385
1386 Attrs[Attribute::AllocSize] = true;
1387 // Reuse existing machinery to store this as a single 64-bit integer so we can
1388 // save a few bytes over using a pair<unsigned, Optional<unsigned>>.
1389 AllocSizeArgs = RawArgs;
1390 return *this;
1391}
1392
Bill Wendlinge2614922013-02-06 01:16:00 +00001393AttrBuilder &AttrBuilder::merge(const AttrBuilder &B) {
1394 // FIXME: What if both have alignments, but they don't match?!
1395 if (!Alignment)
1396 Alignment = B.Alignment;
1397
1398 if (!StackAlignment)
1399 StackAlignment = B.StackAlignment;
1400
Hal Finkelb0407ba2014-07-18 15:51:28 +00001401 if (!DerefBytes)
1402 DerefBytes = B.DerefBytes;
1403
Pete Cooperd2a44612015-05-06 23:19:43 +00001404 if (!DerefOrNullBytes)
1405 DerefOrNullBytes = B.DerefOrNullBytes;
1406
George Burgess IV278199f2016-04-12 01:05:35 +00001407 if (!AllocSizeArgs)
1408 AllocSizeArgs = B.AllocSizeArgs;
1409
Benjamin Kramer45e7d532013-02-16 19:13:18 +00001410 Attrs |= B.Attrs;
Bill Wendlinge2614922013-02-06 01:16:00 +00001411
Pete Cooperd2a44612015-05-06 23:19:43 +00001412 for (auto I : B.td_attrs())
1413 TargetDepAttrs[I.first] = I.second;
Bill Wendlinge2614922013-02-06 01:16:00 +00001414
1415 return *this;
1416}
1417
Pete Cooperd2a44612015-05-06 23:19:43 +00001418AttrBuilder &AttrBuilder::remove(const AttrBuilder &B) {
1419 // FIXME: What if both have alignments, but they don't match?!
1420 if (B.Alignment)
1421 Alignment = 0;
1422
1423 if (B.StackAlignment)
1424 StackAlignment = 0;
1425
1426 if (B.DerefBytes)
1427 DerefBytes = 0;
1428
1429 if (B.DerefOrNullBytes)
1430 DerefOrNullBytes = 0;
1431
George Burgess IV278199f2016-04-12 01:05:35 +00001432 if (B.AllocSizeArgs)
1433 AllocSizeArgs = 0;
1434
Pete Cooperd2a44612015-05-06 23:19:43 +00001435 Attrs &= ~B.Attrs;
1436
1437 for (auto I : B.td_attrs())
1438 TargetDepAttrs.erase(I.first);
1439
1440 return *this;
1441}
1442
1443bool AttrBuilder::overlaps(const AttrBuilder &B) const {
1444 // First check if any of the target independent attributes overlap.
1445 if ((Attrs & B.Attrs).any())
1446 return true;
1447
1448 // Then check if any target dependent ones do.
1449 for (auto I : td_attrs())
1450 if (B.contains(I.first))
1451 return true;
1452
1453 return false;
1454}
1455
Bill Wendling4b001442013-02-06 01:33:42 +00001456bool AttrBuilder::contains(StringRef A) const {
1457 return TargetDepAttrs.find(A) != TargetDepAttrs.end();
1458}
1459
Bill Wendling50d27842012-10-15 20:35:56 +00001460bool AttrBuilder::hasAttributes() const {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001461 return !Attrs.none() || !TargetDepAttrs.empty();
Bill Wendlingc6daefa2012-10-08 23:27:46 +00001462}
Bill Wendling9ac69f92013-01-04 20:54:35 +00001463
Bill Wendlingd2196752013-01-30 23:07:40 +00001464bool AttrBuilder::hasAttributes(AttributeSet A, uint64_t Index) const {
Bill Wendling211316c2013-04-18 20:17:28 +00001465 unsigned Slot = ~0U;
Bill Wendling9ca01da2013-02-02 00:42:06 +00001466 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
1467 if (A.getSlotIndex(I) == Index) {
Bill Wendling211316c2013-04-18 20:17:28 +00001468 Slot = I;
Bill Wendling9ca01da2013-02-02 00:42:06 +00001469 break;
1470 }
1471
Bill Wendling211316c2013-04-18 20:17:28 +00001472 assert(Slot != ~0U && "Couldn't find the index!");
Bill Wendling9ca01da2013-02-02 00:42:06 +00001473
George Burgess IV500d3032015-12-16 05:21:02 +00001474 for (AttributeSet::iterator I = A.begin(Slot), E = A.end(Slot); I != E; ++I) {
Bill Wendling7cde51d2013-02-12 07:56:49 +00001475 Attribute Attr = *I;
Hal Finkele15442c2014-07-18 06:51:55 +00001476 if (Attr.isEnumAttribute() || Attr.isIntAttribute()) {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001477 if (Attrs[I->getKindAsEnum()])
Bill Wendling7cde51d2013-02-12 07:56:49 +00001478 return true;
1479 } else {
1480 assert(Attr.isStringAttribute() && "Invalid attribute kind!");
1481 return TargetDepAttrs.find(Attr.getKindAsString())!=TargetDepAttrs.end();
1482 }
1483 }
Bill Wendling9ca01da2013-02-02 00:42:06 +00001484
1485 return false;
Bill Wendling70f39172012-10-09 00:01:21 +00001486}
Bill Wendling9ac69f92013-01-04 20:54:35 +00001487
Bill Wendling50d27842012-10-15 20:35:56 +00001488bool AttrBuilder::hasAlignmentAttr() const {
Bill Wendlingcd330342013-01-04 23:27:34 +00001489 return Alignment != 0;
Bill Wendlingc6daefa2012-10-08 23:27:46 +00001490}
1491
Bill Wendlingd509a662013-01-29 00:34:06 +00001492bool AttrBuilder::operator==(const AttrBuilder &B) {
Benjamin Kramer45e7d532013-02-16 19:13:18 +00001493 if (Attrs != B.Attrs)
1494 return false;
Bill Wendling4b001442013-02-06 01:33:42 +00001495
1496 for (td_const_iterator I = TargetDepAttrs.begin(),
1497 E = TargetDepAttrs.end(); I != E; ++I)
1498 if (B.TargetDepAttrs.find(I->first) == B.TargetDepAttrs.end())
1499 return false;
1500
Hal Finkelb0407ba2014-07-18 15:51:28 +00001501 return Alignment == B.Alignment && StackAlignment == B.StackAlignment &&
1502 DerefBytes == B.DerefBytes;
Bill Wendlingd509a662013-01-29 00:34:06 +00001503}
1504
1505AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) {
Bill Wendling56aeccc2013-02-04 23:32:23 +00001506 // FIXME: Remove this in 4.0.
Bill Wendling60011b82013-01-29 01:43:29 +00001507 if (!Val) return *this;
1508
Bill Wendlingd509a662013-01-29 00:34:06 +00001509 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
1510 I = Attribute::AttrKind(I + 1)) {
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001511 if (I == Attribute::Dereferenceable ||
Igor Laevsky39d662f2015-07-11 10:30:36 +00001512 I == Attribute::DereferenceableOrNull ||
George Burgess IV278199f2016-04-12 01:05:35 +00001513 I == Attribute::ArgMemOnly ||
1514 I == Attribute::AllocSize)
Hal Finkelb0407ba2014-07-18 15:51:28 +00001515 continue;
Bill Wendlingd509a662013-01-29 00:34:06 +00001516 if (uint64_t A = (Val & AttributeImpl::getAttrMask(I))) {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001517 Attrs[I] = true;
Bill Wendlingd509a662013-01-29 00:34:06 +00001518
1519 if (I == Attribute::Alignment)
1520 Alignment = 1ULL << ((A >> 16) - 1);
1521 else if (I == Attribute::StackAlignment)
1522 StackAlignment = 1ULL << ((A >> 26)-1);
1523 }
1524 }
1525
1526 return *this;
1527}
1528
Bill Wendling57625a42013-01-25 23:09:36 +00001529//===----------------------------------------------------------------------===//
1530// AttributeFuncs Function Defintions
1531//===----------------------------------------------------------------------===//
1532
Bill Wendlingc79cdff2013-02-01 01:04:27 +00001533/// \brief Which attributes cannot be applied to a type.
Craig Toppere3dcce92015-08-01 22:20:21 +00001534AttrBuilder AttributeFuncs::typeIncompatible(Type *Ty) {
Bill Wendling57625a42013-01-25 23:09:36 +00001535 AttrBuilder Incompatible;
1536
1537 if (!Ty->isIntegerTy())
1538 // Attribute that only apply to integers.
1539 Incompatible.addAttribute(Attribute::SExt)
1540 .addAttribute(Attribute::ZExt);
1541
1542 if (!Ty->isPointerTy())
1543 // Attribute that only apply to pointers.
1544 Incompatible.addAttribute(Attribute::ByVal)
1545 .addAttribute(Attribute::Nest)
1546 .addAttribute(Attribute::NoAlias)
1547 .addAttribute(Attribute::NoCapture)
Nick Lewyckyd52b1522014-05-20 01:23:40 +00001548 .addAttribute(Attribute::NonNull)
Hal Finkelb0407ba2014-07-18 15:51:28 +00001549 .addDereferenceableAttr(1) // the int here is ignored
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001550 .addDereferenceableOrNullAttr(1) // the int here is ignored
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001551 .addAttribute(Attribute::ReadNone)
1552 .addAttribute(Attribute::ReadOnly)
Reid Klecknera534a382013-12-19 02:14:12 +00001553 .addAttribute(Attribute::StructRet)
1554 .addAttribute(Attribute::InAlloca);
Bill Wendling57625a42013-01-25 23:09:36 +00001555
Pete Cooper2777d8872015-05-06 23:19:56 +00001556 return Incompatible;
Bill Wendling57625a42013-01-25 23:09:36 +00001557}
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001558
1559template<typename AttrClass>
1560static bool isEqual(const Function &Caller, const Function &Callee) {
1561 return Caller.getFnAttribute(AttrClass::getKind()) ==
1562 Callee.getFnAttribute(AttrClass::getKind());
1563}
1564
1565/// \brief Compute the logical AND of the attributes of the caller and the
1566/// callee.
1567///
1568/// This function sets the caller's attribute to false if the callee's attribute
1569/// is false.
1570template<typename AttrClass>
1571static void setAND(Function &Caller, const Function &Callee) {
1572 if (AttrClass::isSet(Caller, AttrClass::getKind()) &&
1573 !AttrClass::isSet(Callee, AttrClass::getKind()))
1574 AttrClass::set(Caller, AttrClass::getKind(), false);
1575}
1576
1577/// \brief Compute the logical OR of the attributes of the caller and the
1578/// callee.
1579///
1580/// This function sets the caller's attribute to true if the callee's attribute
1581/// is true.
1582template<typename AttrClass>
1583static void setOR(Function &Caller, const Function &Callee) {
1584 if (!AttrClass::isSet(Caller, AttrClass::getKind()) &&
1585 AttrClass::isSet(Callee, AttrClass::getKind()))
1586 AttrClass::set(Caller, AttrClass::getKind(), true);
1587}
1588
1589/// \brief If the inlined function had a higher stack protection level than the
1590/// calling function, then bump up the caller's stack protection level.
1591static void adjustCallerSSPLevel(Function &Caller, const Function &Callee) {
1592 // If upgrading the SSP attribute, clear out the old SSP Attributes first.
1593 // Having multiple SSP attributes doesn't actually hurt, but it adds useless
1594 // clutter to the IR.
1595 AttrBuilder B;
1596 B.addAttribute(Attribute::StackProtect)
1597 .addAttribute(Attribute::StackProtectStrong)
1598 .addAttribute(Attribute::StackProtectReq);
1599 AttributeSet OldSSPAttr = AttributeSet::get(Caller.getContext(),
1600 AttributeSet::FunctionIndex,
1601 B);
1602
Evgeniy Stepanovf17120a2016-04-11 22:27:48 +00001603 if (Callee.hasFnAttribute(Attribute::StackProtectReq)) {
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001604 Caller.removeAttributes(AttributeSet::FunctionIndex, OldSSPAttr);
1605 Caller.addFnAttr(Attribute::StackProtectReq);
1606 } else if (Callee.hasFnAttribute(Attribute::StackProtectStrong) &&
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001607 !Caller.hasFnAttribute(Attribute::StackProtectReq)) {
1608 Caller.removeAttributes(AttributeSet::FunctionIndex, OldSSPAttr);
1609 Caller.addFnAttr(Attribute::StackProtectStrong);
1610 } else if (Callee.hasFnAttribute(Attribute::StackProtect) &&
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001611 !Caller.hasFnAttribute(Attribute::StackProtectReq) &&
1612 !Caller.hasFnAttribute(Attribute::StackProtectStrong))
1613 Caller.addFnAttr(Attribute::StackProtect);
1614}
1615
1616#define GET_ATTR_COMPAT_FUNC
1617#include "AttributesCompatFunc.inc"
1618
1619bool AttributeFuncs::areInlineCompatible(const Function &Caller,
1620 const Function &Callee) {
1621 return hasCompatibleFnAttrs(Caller, Callee);
1622}
1623
1624
1625void AttributeFuncs::mergeAttributesForInlining(Function &Caller,
1626 const Function &Callee) {
1627 mergeFnAttrs(Caller, Callee);
1628}