blob: f0db6b4e71a515803b373bcc1046f8eb22c5a63d [file] [log] [blame]
Eugene Zelenkodeaf6952017-02-17 00:00:09 +00001//===- Attributes.cpp - Implement AttributesList --------------------------===//
Chris Lattner3e13b8c2008-01-02 23:42:30 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Bill Wendlingec454542013-01-28 21:55:20 +000010// \file
11// \brief This file implements the Attribute, AttributeImpl, AttrBuilder,
Reid Klecknerb5180542017-03-21 16:57:19 +000012// AttributeListImpl, and AttributeList classes.
Chris Lattner3e13b8c2008-01-02 23:42:30 +000013//
14//===----------------------------------------------------------------------===//
15
Bill Wendling4607f4b2012-12-20 01:36:59 +000016#include "AttributeImpl.h"
Bill Wendlinge38b8042012-09-26 21:07:29 +000017#include "LLVMContextImpl.h"
Eugene Zelenkodeaf6952017-02-17 00:00:09 +000018#include "llvm/ADT/ArrayRef.h"
19#include "llvm/ADT/FoldingSet.h"
20#include "llvm/ADT/Optional.h"
21#include "llvm/ADT/SmallVector.h"
Benjamin Kramer502b9e12014-04-12 16:15:53 +000022#include "llvm/ADT/STLExtras.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000023#include "llvm/ADT/StringExtras.h"
Eugene Zelenkodeaf6952017-02-17 00:00:09 +000024#include "llvm/ADT/StringRef.h"
25#include "llvm/ADT/Twine.h"
26#include "llvm/IR/Attributes.h"
27#include "llvm/IR/Function.h"
28#include "llvm/IR/LLVMContext.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000029#include "llvm/IR/Type.h"
Eugene Zelenkodeaf6952017-02-17 00:00:09 +000030#include "llvm/Support/Compiler.h"
David Greenef7014732010-01-05 01:29:58 +000031#include "llvm/Support/Debug.h"
Eugene Zelenkodeaf6952017-02-17 00:00:09 +000032#include "llvm/Support/ErrorHandling.h"
33#include "llvm/Support/MathExtras.h"
Benjamin Kramer1a25d732009-08-23 11:37:21 +000034#include "llvm/Support/raw_ostream.h"
Bill Wendlingd2e493b2013-01-24 00:06:56 +000035#include <algorithm>
Eugene Zelenkodeaf6952017-02-17 00:00:09 +000036#include <cassert>
37#include <cstdint>
38#include <limits>
39#include <map>
40#include <string>
41#include <tuple>
42#include <utility>
43
Chris Lattner3e13b8c2008-01-02 23:42:30 +000044using namespace llvm;
45
Chris Lattner8a923e72008-03-12 17:45:29 +000046//===----------------------------------------------------------------------===//
Bill Wendling7707c5a2013-01-29 00:48:16 +000047// Attribute Construction Methods
Chris Lattner8a923e72008-03-12 17:45:29 +000048//===----------------------------------------------------------------------===//
Chris Lattnerd0e1f102008-01-03 00:10:22 +000049
George Burgess IV278199f2016-04-12 01:05:35 +000050// allocsize has two integer arguments, but because they're both 32 bits, we can
51// pack them into one 64-bit value, at the cost of making said value
52// nonsensical.
53//
54// In order to do this, we need to reserve one value of the second (optional)
55// allocsize argument to signify "not present."
George Burgess IV381fc0e2016-08-25 01:05:08 +000056static const unsigned AllocSizeNumElemsNotPresent = -1;
George Burgess IV278199f2016-04-12 01:05:35 +000057
58static uint64_t packAllocSizeArgs(unsigned ElemSizeArg,
59 const Optional<unsigned> &NumElemsArg) {
60 assert((!NumElemsArg.hasValue() ||
61 *NumElemsArg != AllocSizeNumElemsNotPresent) &&
62 "Attempting to pack a reserved value");
63
64 return uint64_t(ElemSizeArg) << 32 |
65 NumElemsArg.getValueOr(AllocSizeNumElemsNotPresent);
66}
67
68static std::pair<unsigned, Optional<unsigned>>
69unpackAllocSizeArgs(uint64_t Num) {
70 unsigned NumElems = Num & std::numeric_limits<unsigned>::max();
71 unsigned ElemSizeArg = Num >> 32;
72
73 Optional<unsigned> NumElemsArg;
74 if (NumElems != AllocSizeNumElemsNotPresent)
75 NumElemsArg = NumElems;
76 return std::make_pair(ElemSizeArg, NumElemsArg);
77}
78
Matt Arsenaulte1cef6e2014-09-03 23:24:31 +000079Attribute Attribute::get(LLVMContext &Context, Attribute::AttrKind Kind,
80 uint64_t Val) {
81 LLVMContextImpl *pImpl = Context.pImpl;
82 FoldingSetNodeID ID;
83 ID.AddInteger(Kind);
Matt Arsenaultfb4308e2014-09-03 23:38:05 +000084 if (Val) ID.AddInteger(Val);
Matt Arsenaulte1cef6e2014-09-03 23:24:31 +000085
86 void *InsertPoint;
87 AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
88
89 if (!PA) {
90 // If we didn't find any existing attributes of the same shape then create a
91 // new one and insert it.
Matt Arsenaultfb4308e2014-09-03 23:38:05 +000092 if (!Val)
93 PA = new EnumAttributeImpl(Kind);
94 else
95 PA = new IntAttributeImpl(Kind, Val);
Bill Wendling3f12ac22013-02-05 22:37:24 +000096 pImpl->AttrsSet.InsertNode(PA, InsertPoint);
97 }
98
99 // Return the Attribute that we found or created.
100 return Attribute(PA);
101}
102
103Attribute Attribute::get(LLVMContext &Context, StringRef Kind, StringRef Val) {
104 LLVMContextImpl *pImpl = Context.pImpl;
105 FoldingSetNodeID ID;
106 ID.AddString(Kind);
107 if (!Val.empty()) ID.AddString(Val);
Bill Wendling73ea2de2012-10-08 21:47:17 +0000108
109 void *InsertPoint;
Bill Wendling4607f4b2012-12-20 01:36:59 +0000110 AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
Bill Wendling73ea2de2012-10-08 21:47:17 +0000111
112 if (!PA) {
113 // If we didn't find any existing attributes of the same shape then create a
114 // new one and insert it.
Benjamin Kramer741146b2013-07-11 12:13:16 +0000115 PA = new StringAttributeImpl(Kind, Val);
Bill Wendling73ea2de2012-10-08 21:47:17 +0000116 pImpl->AttrsSet.InsertNode(PA, InsertPoint);
117 }
118
Bill Wendlingb9c5b1a2013-02-05 08:09:32 +0000119 // Return the Attribute that we found or created.
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000120 return Attribute(PA);
Bill Wendling73ea2de2012-10-08 21:47:17 +0000121}
122
Bill Wendling4bbe9db2013-01-27 22:43:04 +0000123Attribute Attribute::getWithAlignment(LLVMContext &Context, uint64_t Align) {
Bill Wendling1c7cc8a2013-01-31 23:16:25 +0000124 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
125 assert(Align <= 0x40000000 && "Alignment too large.");
Bill Wendling3f12ac22013-02-05 22:37:24 +0000126 return get(Context, Alignment, Align);
Bill Wendling4bbe9db2013-01-27 22:43:04 +0000127}
128
129Attribute Attribute::getWithStackAlignment(LLVMContext &Context,
130 uint64_t Align) {
Bill Wendling1c7cc8a2013-01-31 23:16:25 +0000131 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
132 assert(Align <= 0x100 && "Alignment too large.");
Bill Wendling3f12ac22013-02-05 22:37:24 +0000133 return get(Context, StackAlignment, Align);
Bill Wendling4bbe9db2013-01-27 22:43:04 +0000134}
135
Hal Finkelb0407ba2014-07-18 15:51:28 +0000136Attribute Attribute::getWithDereferenceableBytes(LLVMContext &Context,
137 uint64_t Bytes) {
138 assert(Bytes && "Bytes must be non-zero.");
139 return get(Context, Dereferenceable, Bytes);
140}
141
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000142Attribute Attribute::getWithDereferenceableOrNullBytes(LLVMContext &Context,
143 uint64_t Bytes) {
144 assert(Bytes && "Bytes must be non-zero.");
145 return get(Context, DereferenceableOrNull, Bytes);
146}
147
George Burgess IV278199f2016-04-12 01:05:35 +0000148Attribute
149Attribute::getWithAllocSizeArgs(LLVMContext &Context, unsigned ElemSizeArg,
150 const Optional<unsigned> &NumElemsArg) {
151 assert(!(ElemSizeArg == 0 && NumElemsArg && *NumElemsArg == 0) &&
152 "Invalid allocsize arguments -- given allocsize(0, 0)");
153 return get(Context, AllocSize, packAllocSizeArgs(ElemSizeArg, NumElemsArg));
154}
155
Bill Wendling7707c5a2013-01-29 00:48:16 +0000156//===----------------------------------------------------------------------===//
157// Attribute Accessor Methods
158//===----------------------------------------------------------------------===//
159
Bill Wendling3f12ac22013-02-05 22:37:24 +0000160bool Attribute::isEnumAttribute() const {
161 return pImpl && pImpl->isEnumAttribute();
162}
163
Hal Finkele15442c2014-07-18 06:51:55 +0000164bool Attribute::isIntAttribute() const {
165 return pImpl && pImpl->isIntAttribute();
Bill Wendling3f12ac22013-02-05 22:37:24 +0000166}
167
168bool Attribute::isStringAttribute() const {
169 return pImpl && pImpl->isStringAttribute();
170}
171
172Attribute::AttrKind Attribute::getKindAsEnum() const {
Bill Wendling440e9d82013-07-25 00:34:29 +0000173 if (!pImpl) return None;
Hal Finkele15442c2014-07-18 06:51:55 +0000174 assert((isEnumAttribute() || isIntAttribute()) &&
Bill Wendling3f12ac22013-02-05 22:37:24 +0000175 "Invalid attribute type to get the kind as an enum!");
George Burgess IV500d3032015-12-16 05:21:02 +0000176 return pImpl->getKindAsEnum();
Bill Wendling3f12ac22013-02-05 22:37:24 +0000177}
178
179uint64_t Attribute::getValueAsInt() const {
Bill Wendling440e9d82013-07-25 00:34:29 +0000180 if (!pImpl) return 0;
Hal Finkele15442c2014-07-18 06:51:55 +0000181 assert(isIntAttribute() &&
182 "Expected the attribute to be an integer attribute!");
George Burgess IV500d3032015-12-16 05:21:02 +0000183 return pImpl->getValueAsInt();
Bill Wendling3f12ac22013-02-05 22:37:24 +0000184}
185
186StringRef Attribute::getKindAsString() const {
Bill Wendling440e9d82013-07-25 00:34:29 +0000187 if (!pImpl) return StringRef();
Bill Wendling3f12ac22013-02-05 22:37:24 +0000188 assert(isStringAttribute() &&
189 "Invalid attribute type to get the kind as a string!");
George Burgess IV500d3032015-12-16 05:21:02 +0000190 return pImpl->getKindAsString();
Bill Wendling3f12ac22013-02-05 22:37:24 +0000191}
192
193StringRef Attribute::getValueAsString() const {
Bill Wendling440e9d82013-07-25 00:34:29 +0000194 if (!pImpl) return StringRef();
Bill Wendling3f12ac22013-02-05 22:37:24 +0000195 assert(isStringAttribute() &&
196 "Invalid attribute type to get the value as a string!");
George Burgess IV500d3032015-12-16 05:21:02 +0000197 return pImpl->getValueAsString();
Bill Wendling3f12ac22013-02-05 22:37:24 +0000198}
199
Bill Wendlingae89a0f2013-02-05 23:48:36 +0000200bool Attribute::hasAttribute(AttrKind Kind) const {
201 return (pImpl && pImpl->hasAttribute(Kind)) || (!pImpl && Kind == None);
202}
203
204bool Attribute::hasAttribute(StringRef Kind) const {
205 if (!isStringAttribute()) return false;
206 return pImpl && pImpl->hasAttribute(Kind);
Bill Wendling03eefb32013-01-29 20:45:34 +0000207}
208
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000209unsigned Attribute::getAlignment() const {
Bill Wendlingc79cdff2013-02-01 01:04:27 +0000210 assert(hasAttribute(Attribute::Alignment) &&
211 "Trying to get alignment from non-alignment attribute!");
Bill Wendling3f12ac22013-02-05 22:37:24 +0000212 return pImpl->getValueAsInt();
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000213}
214
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000215unsigned Attribute::getStackAlignment() const {
Bill Wendlingc79cdff2013-02-01 01:04:27 +0000216 assert(hasAttribute(Attribute::StackAlignment) &&
217 "Trying to get alignment from non-alignment attribute!");
Bill Wendling3f12ac22013-02-05 22:37:24 +0000218 return pImpl->getValueAsInt();
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000219}
220
Hal Finkelb0407ba2014-07-18 15:51:28 +0000221uint64_t Attribute::getDereferenceableBytes() const {
222 assert(hasAttribute(Attribute::Dereferenceable) &&
223 "Trying to get dereferenceable bytes from "
224 "non-dereferenceable attribute!");
225 return pImpl->getValueAsInt();
226}
227
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000228uint64_t Attribute::getDereferenceableOrNullBytes() const {
229 assert(hasAttribute(Attribute::DereferenceableOrNull) &&
230 "Trying to get dereferenceable bytes from "
231 "non-dereferenceable attribute!");
232 return pImpl->getValueAsInt();
233}
234
George Burgess IV278199f2016-04-12 01:05:35 +0000235std::pair<unsigned, Optional<unsigned>> Attribute::getAllocSizeArgs() const {
236 assert(hasAttribute(Attribute::AllocSize) &&
237 "Trying to get allocsize args from non-allocsize attribute");
238 return unpackAllocSizeArgs(pImpl->getValueAsInt());
239}
240
Bill Wendling829b4782013-02-11 08:43:33 +0000241std::string Attribute::getAsString(bool InAttrGrp) const {
Bill Wendling9c2eba92013-01-31 20:59:05 +0000242 if (!pImpl) return "";
243
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000244 if (hasAttribute(Attribute::SanitizeAddress))
245 return "sanitize_address";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000246 if (hasAttribute(Attribute::AlwaysInline))
247 return "alwaysinline";
Igor Laevsky39d662f2015-07-11 10:30:36 +0000248 if (hasAttribute(Attribute::ArgMemOnly))
249 return "argmemonly";
Michael Gottesman41748d72013-06-27 00:25:01 +0000250 if (hasAttribute(Attribute::Builtin))
251 return "builtin";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000252 if (hasAttribute(Attribute::ByVal))
253 return "byval";
Owen Anderson85fa7d52015-05-26 23:48:40 +0000254 if (hasAttribute(Attribute::Convergent))
255 return "convergent";
Manman Ren9bfd0d02016-04-01 21:41:15 +0000256 if (hasAttribute(Attribute::SwiftError))
257 return "swifterror";
Manman Renf46262e2016-03-29 17:37:21 +0000258 if (hasAttribute(Attribute::SwiftSelf))
259 return "swiftself";
Vaivaswatha Nagarajfb3f4902015-12-16 16:16:19 +0000260 if (hasAttribute(Attribute::InaccessibleMemOnly))
261 return "inaccessiblememonly";
262 if (hasAttribute(Attribute::InaccessibleMemOrArgMemOnly))
263 return "inaccessiblemem_or_argmemonly";
Reid Klecknera534a382013-12-19 02:14:12 +0000264 if (hasAttribute(Attribute::InAlloca))
265 return "inalloca";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000266 if (hasAttribute(Attribute::InlineHint))
267 return "inlinehint";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000268 if (hasAttribute(Attribute::InReg))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000269 return "inreg";
Tom Roeder44cb65f2014-06-05 19:29:43 +0000270 if (hasAttribute(Attribute::JumpTable))
271 return "jumptable";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000272 if (hasAttribute(Attribute::MinSize))
273 return "minsize";
274 if (hasAttribute(Attribute::Naked))
275 return "naked";
276 if (hasAttribute(Attribute::Nest))
277 return "nest";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000278 if (hasAttribute(Attribute::NoAlias))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000279 return "noalias";
Bill Wendling09bd1f72013-02-22 00:12:35 +0000280 if (hasAttribute(Attribute::NoBuiltin))
281 return "nobuiltin";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000282 if (hasAttribute(Attribute::NoCapture))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000283 return "nocapture";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000284 if (hasAttribute(Attribute::NoDuplicate))
285 return "noduplicate";
286 if (hasAttribute(Attribute::NoImplicitFloat))
287 return "noimplicitfloat";
288 if (hasAttribute(Attribute::NoInline))
289 return "noinline";
290 if (hasAttribute(Attribute::NonLazyBind))
291 return "nonlazybind";
Nick Lewyckyd52b1522014-05-20 01:23:40 +0000292 if (hasAttribute(Attribute::NonNull))
293 return "nonnull";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000294 if (hasAttribute(Attribute::NoRedZone))
295 return "noredzone";
296 if (hasAttribute(Attribute::NoReturn))
297 return "noreturn";
James Molloye6f87ca2015-11-06 10:32:53 +0000298 if (hasAttribute(Attribute::NoRecurse))
299 return "norecurse";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000300 if (hasAttribute(Attribute::NoUnwind))
301 return "nounwind";
Andrea Di Biagio377496b2013-08-23 11:53:55 +0000302 if (hasAttribute(Attribute::OptimizeNone))
303 return "optnone";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000304 if (hasAttribute(Attribute::OptimizeForSize))
305 return "optsize";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000306 if (hasAttribute(Attribute::ReadNone))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000307 return "readnone";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000308 if (hasAttribute(Attribute::ReadOnly))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000309 return "readonly";
Nicolai Haehnle84c9f992016-07-04 08:01:29 +0000310 if (hasAttribute(Attribute::WriteOnly))
311 return "writeonly";
Stephen Linb8bd2322013-04-20 05:14:40 +0000312 if (hasAttribute(Attribute::Returned))
313 return "returned";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000314 if (hasAttribute(Attribute::ReturnsTwice))
315 return "returns_twice";
316 if (hasAttribute(Attribute::SExt))
317 return "signext";
Matt Arsenaultb19b57e2017-04-28 20:25:27 +0000318 if (hasAttribute(Attribute::Speculatable))
319 return "speculatable";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000320 if (hasAttribute(Attribute::StackProtect))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000321 return "ssp";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000322 if (hasAttribute(Attribute::StackProtectReq))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000323 return "sspreq";
Bill Wendlingd154e2832013-01-23 06:41:41 +0000324 if (hasAttribute(Attribute::StackProtectStrong))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000325 return "sspstrong";
Peter Collingbourne82437bf2015-06-15 21:07:11 +0000326 if (hasAttribute(Attribute::SafeStack))
327 return "safestack";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000328 if (hasAttribute(Attribute::StructRet))
329 return "sret";
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000330 if (hasAttribute(Attribute::SanitizeThread))
331 return "sanitize_thread";
332 if (hasAttribute(Attribute::SanitizeMemory))
333 return "sanitize_memory";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000334 if (hasAttribute(Attribute::UWTable))
335 return "uwtable";
336 if (hasAttribute(Attribute::ZExt))
337 return "zeroext";
Diego Novilloc6399532013-05-24 12:26:52 +0000338 if (hasAttribute(Attribute::Cold))
339 return "cold";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000340
341 // FIXME: These should be output like this:
342 //
343 // align=4
344 // alignstack=8
345 //
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000346 if (hasAttribute(Attribute::Alignment)) {
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000347 std::string Result;
Bill Wendling829b4782013-02-11 08:43:33 +0000348 Result += "align";
349 Result += (InAttrGrp) ? "=" : " ";
Bill Wendling3f12ac22013-02-05 22:37:24 +0000350 Result += utostr(getValueAsInt());
351 return Result;
352 }
Bill Wendling829b4782013-02-11 08:43:33 +0000353
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000354 auto AttrWithBytesToString = [&](const char *Name) {
Bill Wendling3f12ac22013-02-05 22:37:24 +0000355 std::string Result;
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000356 Result += Name;
Bill Wendling829b4782013-02-11 08:43:33 +0000357 if (InAttrGrp) {
358 Result += "=";
359 Result += utostr(getValueAsInt());
360 } else {
361 Result += "(";
362 Result += utostr(getValueAsInt());
363 Result += ")";
364 }
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000365 return Result;
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000366 };
Bill Wendling9c2eba92013-01-31 20:59:05 +0000367
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000368 if (hasAttribute(Attribute::StackAlignment))
369 return AttrWithBytesToString("alignstack");
370
371 if (hasAttribute(Attribute::Dereferenceable))
372 return AttrWithBytesToString("dereferenceable");
373
374 if (hasAttribute(Attribute::DereferenceableOrNull))
375 return AttrWithBytesToString("dereferenceable_or_null");
Hal Finkelb0407ba2014-07-18 15:51:28 +0000376
George Burgess IV278199f2016-04-12 01:05:35 +0000377 if (hasAttribute(Attribute::AllocSize)) {
378 unsigned ElemSize;
379 Optional<unsigned> NumElems;
380 std::tie(ElemSize, NumElems) = getAllocSizeArgs();
381
382 std::string Result = "allocsize(";
383 Result += utostr(ElemSize);
384 if (NumElems.hasValue()) {
385 Result += ',';
386 Result += utostr(*NumElems);
387 }
388 Result += ')';
389 return Result;
390 }
391
Bill Wendling9c2eba92013-01-31 20:59:05 +0000392 // Convert target-dependent attributes to strings of the form:
393 //
394 // "kind"
395 // "kind" = "value"
Bill Wendling9c2eba92013-01-31 20:59:05 +0000396 //
Bill Wendling3f12ac22013-02-05 22:37:24 +0000397 if (isStringAttribute()) {
Bill Wendling9c2eba92013-01-31 20:59:05 +0000398 std::string Result;
Yaron Keren075759a2015-03-30 15:42:36 +0000399 Result += (Twine('"') + getKindAsString() + Twine('"')).str();
Bill Wendling9c2eba92013-01-31 20:59:05 +0000400
Honggyu Kim9eb6a102016-09-01 11:44:06 +0000401 std::string AttrVal = pImpl->getValueAsString();
402 if (AttrVal.empty()) return Result;
Bill Wendling7a33f772013-02-01 22:32:30 +0000403
Honggyu Kim9eb6a102016-09-01 11:44:06 +0000404 // Since some attribute strings contain special characters that cannot be
405 // printable, those have to be escaped to make the attribute value printable
406 // as is. e.g. "\01__gnu_mcount_nc"
407 {
408 raw_string_ostream OS(Result);
409 OS << "=\"";
410 PrintEscapedString(AttrVal, OS);
411 OS << "\"";
412 }
Bill Wendlingc79cdff2013-02-01 01:04:27 +0000413 return Result;
Bill Wendling9c2eba92013-01-31 20:59:05 +0000414 }
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000415
416 llvm_unreachable("Unknown attribute");
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000417}
418
Bill Wendlingd509a662013-01-29 00:34:06 +0000419bool Attribute::operator<(Attribute A) const {
420 if (!pImpl && !A.pImpl) return false;
421 if (!pImpl) return true;
422 if (!A.pImpl) return false;
423 return *pImpl < *A.pImpl;
424}
425
Bill Wendlingd509a662013-01-29 00:34:06 +0000426//===----------------------------------------------------------------------===//
427// AttributeImpl Definition
428//===----------------------------------------------------------------------===//
429
Eric Christopher0eaa5412014-07-02 22:05:40 +0000430// Pin the vtables to this file.
Eugene Zelenkodeaf6952017-02-17 00:00:09 +0000431AttributeImpl::~AttributeImpl() = default;
432
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +0000433void EnumAttributeImpl::anchor() {}
Eugene Zelenkodeaf6952017-02-17 00:00:09 +0000434
Hal Finkele15442c2014-07-18 06:51:55 +0000435void IntAttributeImpl::anchor() {}
Eugene Zelenkodeaf6952017-02-17 00:00:09 +0000436
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +0000437void StringAttributeImpl::anchor() {}
Alexey Samsonov49109a22013-11-18 09:31:53 +0000438
Bill Wendlingd509a662013-01-29 00:34:06 +0000439bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
Bill Wendling3f12ac22013-02-05 22:37:24 +0000440 if (isStringAttribute()) return false;
441 return getKindAsEnum() == A;
Bill Wendlingd509a662013-01-29 00:34:06 +0000442}
443
Bill Wendling3f12ac22013-02-05 22:37:24 +0000444bool AttributeImpl::hasAttribute(StringRef Kind) const {
445 if (!isStringAttribute()) return false;
446 return getKindAsString() == Kind;
Bill Wendlingd509a662013-01-29 00:34:06 +0000447}
448
Bill Wendling3f12ac22013-02-05 22:37:24 +0000449Attribute::AttrKind AttributeImpl::getKindAsEnum() const {
Hal Finkele15442c2014-07-18 06:51:55 +0000450 assert(isEnumAttribute() || isIntAttribute());
Benjamin Kramer741146b2013-07-11 12:13:16 +0000451 return static_cast<const EnumAttributeImpl *>(this)->getEnumKind();
Bill Wendlingd509a662013-01-29 00:34:06 +0000452}
453
Bill Wendling3f12ac22013-02-05 22:37:24 +0000454uint64_t AttributeImpl::getValueAsInt() const {
Hal Finkele15442c2014-07-18 06:51:55 +0000455 assert(isIntAttribute());
456 return static_cast<const IntAttributeImpl *>(this)->getValue();
Bill Wendlingd509a662013-01-29 00:34:06 +0000457}
458
Bill Wendling3f12ac22013-02-05 22:37:24 +0000459StringRef AttributeImpl::getKindAsString() const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000460 assert(isStringAttribute());
461 return static_cast<const StringAttributeImpl *>(this)->getStringKind();
Bill Wendlingd509a662013-01-29 00:34:06 +0000462}
463
Bill Wendling3f12ac22013-02-05 22:37:24 +0000464StringRef AttributeImpl::getValueAsString() const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000465 assert(isStringAttribute());
466 return static_cast<const StringAttributeImpl *>(this)->getStringValue();
Bill Wendlingd509a662013-01-29 00:34:06 +0000467}
468
469bool AttributeImpl::operator<(const AttributeImpl &AI) const {
Bill Wendlingc79cdff2013-02-01 01:04:27 +0000470 // This sorts the attributes with Attribute::AttrKinds coming first (sorted
471 // relative to their enum value) and then strings.
Bill Wendling26b95752013-02-15 05:25:26 +0000472 if (isEnumAttribute()) {
473 if (AI.isEnumAttribute()) return getKindAsEnum() < AI.getKindAsEnum();
Hal Finkele15442c2014-07-18 06:51:55 +0000474 if (AI.isIntAttribute()) return true;
Bill Wendling26b95752013-02-15 05:25:26 +0000475 if (AI.isStringAttribute()) return true;
476 }
Bill Wendlingc79cdff2013-02-01 01:04:27 +0000477
Hal Finkele15442c2014-07-18 06:51:55 +0000478 if (isIntAttribute()) {
Bill Wendling26b95752013-02-15 05:25:26 +0000479 if (AI.isEnumAttribute()) return false;
Reid Kleckner7de67612016-04-04 23:06:05 +0000480 if (AI.isIntAttribute()) {
481 if (getKindAsEnum() == AI.getKindAsEnum())
482 return getValueAsInt() < AI.getValueAsInt();
483 return getKindAsEnum() < AI.getKindAsEnum();
484 }
Bill Wendling26b95752013-02-15 05:25:26 +0000485 if (AI.isStringAttribute()) return true;
Bill Wendling3f12ac22013-02-05 22:37:24 +0000486 }
Bill Wendlingd509a662013-01-29 00:34:06 +0000487
Bill Wendling26b95752013-02-15 05:25:26 +0000488 if (AI.isEnumAttribute()) return false;
Hal Finkele15442c2014-07-18 06:51:55 +0000489 if (AI.isIntAttribute()) return false;
Bill Wendling26b95752013-02-15 05:25:26 +0000490 if (getKindAsString() == AI.getKindAsString())
491 return getValueAsString() < AI.getValueAsString();
492 return getKindAsString() < AI.getKindAsString();
Bill Wendlingd509a662013-01-29 00:34:06 +0000493}
494
Bill Wendlingd509a662013-01-29 00:34:06 +0000495//===----------------------------------------------------------------------===//
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000496// AttributeSet Definition
497//===----------------------------------------------------------------------===//
498
499AttributeSet AttributeSet::get(LLVMContext &C, const AttrBuilder &B) {
500 return AttributeSet(AttributeSetNode::get(C, B));
501}
502
503AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<Attribute> Attrs) {
504 return AttributeSet(AttributeSetNode::get(C, Attrs));
505}
506
507unsigned AttributeSet::getNumAttributes() const {
508 return SetNode ? SetNode->getNumAttributes() : 0;
509}
510
511bool AttributeSet::hasAttribute(Attribute::AttrKind Kind) const {
512 return SetNode ? SetNode->hasAttribute(Kind) : 0;
513}
514
515bool AttributeSet::hasAttribute(StringRef Kind) const {
516 return SetNode ? SetNode->hasAttribute(Kind) : 0;
517}
518
519Attribute AttributeSet::getAttribute(Attribute::AttrKind Kind) const {
520 return SetNode ? SetNode->getAttribute(Kind) : Attribute();
521}
522
523Attribute AttributeSet::getAttribute(StringRef Kind) const {
524 return SetNode ? SetNode->getAttribute(Kind) : Attribute();
525}
526
527unsigned AttributeSet::getAlignment() const {
528 return SetNode ? SetNode->getAlignment() : 0;
529}
530
531unsigned AttributeSet::getStackAlignment() const {
532 return SetNode ? SetNode->getStackAlignment() : 0;
533}
534
535uint64_t AttributeSet::getDereferenceableBytes() const {
536 return SetNode ? SetNode->getDereferenceableBytes() : 0;
537}
538
539uint64_t AttributeSet::getDereferenceableOrNullBytes() const {
540 return SetNode ? SetNode->getDereferenceableOrNullBytes() : 0;
541}
542
543std::pair<unsigned, Optional<unsigned>> AttributeSet::getAllocSizeArgs() const {
Konstantin Zhuravlyov6df95b72017-04-12 23:57:37 +0000544 return SetNode ? SetNode->getAllocSizeArgs()
545 : std::pair<unsigned, Optional<unsigned>>(0, 0);
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000546}
547
548std::string AttributeSet::getAsString(bool InAttrGrp) const {
549 return SetNode ? SetNode->getAsString(InAttrGrp) : "";
550}
551
552AttributeSet::iterator AttributeSet::begin() const {
553 return SetNode ? SetNode->begin() : nullptr;
554}
555
556AttributeSet::iterator AttributeSet::end() const {
557 return SetNode ? SetNode->end() : nullptr;
558}
559
560//===----------------------------------------------------------------------===//
Bill Wendlingd509a662013-01-29 00:34:06 +0000561// AttributeSetNode Definition
562//===----------------------------------------------------------------------===//
563
Reid Kleckner8ff77852017-04-10 23:46:08 +0000564AttributeSetNode::AttributeSetNode(ArrayRef<Attribute> Attrs)
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000565 : AvailableAttrs(0), NumAttrs(Attrs.size()) {
Reid Kleckner8ff77852017-04-10 23:46:08 +0000566 // There's memory after the node where we can store the entries in.
567 std::copy(Attrs.begin(), Attrs.end(), getTrailingObjects<Attribute>());
568
569 for (Attribute I : *this) {
570 if (!I.isStringAttribute()) {
571 AvailableAttrs |= ((uint64_t)1) << I.getKindAsEnum();
572 }
573 }
574}
575
Bill Wendlingd509a662013-01-29 00:34:06 +0000576AttributeSetNode *AttributeSetNode::get(LLVMContext &C,
577 ArrayRef<Attribute> Attrs) {
578 if (Attrs.empty())
Craig Topperc6207612014-04-09 06:08:46 +0000579 return nullptr;
Bill Wendlingd509a662013-01-29 00:34:06 +0000580
581 // Otherwise, build a key to look up the existing attributes.
582 LLVMContextImpl *pImpl = C.pImpl;
583 FoldingSetNodeID ID;
584
585 SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end());
Reid Kleckner7de67612016-04-04 23:06:05 +0000586 std::sort(SortedAttrs.begin(), SortedAttrs.end());
Bill Wendlingd509a662013-01-29 00:34:06 +0000587
George Burgess IV500d3032015-12-16 05:21:02 +0000588 for (Attribute Attr : SortedAttrs)
589 Attr.Profile(ID);
Bill Wendlingd509a662013-01-29 00:34:06 +0000590
591 void *InsertPoint;
592 AttributeSetNode *PA =
593 pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint);
594
595 // If we didn't find any existing attributes of the same shape then create a
596 // new one and insert it.
597 if (!PA) {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000598 // Coallocate entries after the AttributeSetNode itself.
James Y Knightaa365b22015-08-05 22:57:34 +0000599 void *Mem = ::operator new(totalSizeToAlloc<Attribute>(SortedAttrs.size()));
Benjamin Kramer741146b2013-07-11 12:13:16 +0000600 PA = new (Mem) AttributeSetNode(SortedAttrs);
Bill Wendlingd509a662013-01-29 00:34:06 +0000601 pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint);
602 }
603
Reid Klecknerb5180542017-03-21 16:57:19 +0000604 // Return the AttributeSetNode that we found or created.
Bill Wendlingd509a662013-01-29 00:34:06 +0000605 return PA;
606}
607
Reid Klecknereb9dd5b2017-04-10 23:31:05 +0000608AttributeSetNode *AttributeSetNode::get(LLVMContext &C, const AttrBuilder &B) {
609 // Add target-independent attributes.
610 SmallVector<Attribute, 8> Attrs;
611 for (Attribute::AttrKind Kind = Attribute::None;
612 Kind != Attribute::EndAttrKinds; Kind = Attribute::AttrKind(Kind + 1)) {
613 if (!B.contains(Kind))
614 continue;
615
616 Attribute Attr;
617 switch (Kind) {
618 case Attribute::Alignment:
619 Attr = Attribute::getWithAlignment(C, B.getAlignment());
620 break;
621 case Attribute::StackAlignment:
622 Attr = Attribute::getWithStackAlignment(C, B.getStackAlignment());
623 break;
624 case Attribute::Dereferenceable:
625 Attr = Attribute::getWithDereferenceableBytes(
626 C, B.getDereferenceableBytes());
627 break;
628 case Attribute::DereferenceableOrNull:
629 Attr = Attribute::getWithDereferenceableOrNullBytes(
630 C, B.getDereferenceableOrNullBytes());
631 break;
632 case Attribute::AllocSize: {
633 auto A = B.getAllocSizeArgs();
634 Attr = Attribute::getWithAllocSizeArgs(C, A.first, A.second);
635 break;
636 }
637 default:
638 Attr = Attribute::get(C, Kind);
639 }
640 Attrs.push_back(Attr);
641 }
642
643 // Add target-dependent (string) attributes.
644 for (const auto &TDA : B.td_attrs())
645 Attrs.emplace_back(Attribute::get(C, TDA.first, TDA.second));
646
647 return get(C, Attrs);
648}
649
Bill Wendlingbce7b972013-02-13 08:42:21 +0000650bool AttributeSetNode::hasAttribute(StringRef Kind) const {
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000651 for (Attribute I : *this)
652 if (I.hasAttribute(Kind))
Bill Wendlingbce7b972013-02-13 08:42:21 +0000653 return true;
654 return false;
655}
656
657Attribute AttributeSetNode::getAttribute(Attribute::AttrKind Kind) const {
Matthias Braun31eeb76f2016-01-29 22:25:13 +0000658 if (hasAttribute(Kind)) {
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000659 for (Attribute I : *this)
660 if (I.hasAttribute(Kind))
661 return I;
Matthias Braun31eeb76f2016-01-29 22:25:13 +0000662 }
Bill Wendlingbce7b972013-02-13 08:42:21 +0000663 return Attribute();
664}
665
666Attribute AttributeSetNode::getAttribute(StringRef Kind) const {
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000667 for (Attribute I : *this)
668 if (I.hasAttribute(Kind))
669 return I;
Bill Wendlingbce7b972013-02-13 08:42:21 +0000670 return Attribute();
671}
672
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000673unsigned AttributeSetNode::getAlignment() const {
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000674 for (Attribute I : *this)
675 if (I.hasAttribute(Attribute::Alignment))
676 return I.getAlignment();
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000677 return 0;
678}
679
680unsigned AttributeSetNode::getStackAlignment() const {
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000681 for (Attribute I : *this)
682 if (I.hasAttribute(Attribute::StackAlignment))
683 return I.getStackAlignment();
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000684 return 0;
685}
686
Hal Finkelb0407ba2014-07-18 15:51:28 +0000687uint64_t AttributeSetNode::getDereferenceableBytes() const {
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000688 for (Attribute I : *this)
689 if (I.hasAttribute(Attribute::Dereferenceable))
690 return I.getDereferenceableBytes();
Hal Finkelb0407ba2014-07-18 15:51:28 +0000691 return 0;
692}
693
Sanjoy Das06cf33f2015-05-06 17:41:54 +0000694uint64_t AttributeSetNode::getDereferenceableOrNullBytes() const {
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000695 for (Attribute I : *this)
696 if (I.hasAttribute(Attribute::DereferenceableOrNull))
697 return I.getDereferenceableOrNullBytes();
Sanjoy Das06cf33f2015-05-06 17:41:54 +0000698 return 0;
699}
700
George Burgess IV278199f2016-04-12 01:05:35 +0000701std::pair<unsigned, Optional<unsigned>>
702AttributeSetNode::getAllocSizeArgs() const {
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000703 for (Attribute I : *this)
704 if (I.hasAttribute(Attribute::AllocSize))
705 return I.getAllocSizeArgs();
George Burgess IV278199f2016-04-12 01:05:35 +0000706 return std::make_pair(0, 0);
707}
708
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +0000709std::string AttributeSetNode::getAsString(bool InAttrGrp) const {
Benjamin Kramer0baf8f42013-04-19 11:43:21 +0000710 std::string Str;
Benjamin Kramer741146b2013-07-11 12:13:16 +0000711 for (iterator I = begin(), E = end(); I != E; ++I) {
712 if (I != begin())
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +0000713 Str += ' ';
714 Str += I->getAsString(InAttrGrp);
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000715 }
716 return Str;
717}
718
Bill Wendlingd509a662013-01-29 00:34:06 +0000719//===----------------------------------------------------------------------===//
Reid Klecknerb5180542017-03-21 16:57:19 +0000720// AttributeListImpl Definition
Bill Wendlingd509a662013-01-29 00:34:06 +0000721//===----------------------------------------------------------------------===//
722
Reid Klecknera82be602017-04-11 00:16:00 +0000723AttributeListImpl::AttributeListImpl(
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000724 LLVMContext &C, ArrayRef<std::pair<unsigned, AttributeSet>> Slots)
Reid Klecknera82be602017-04-11 00:16:00 +0000725 : Context(C), NumSlots(Slots.size()), AvailableFunctionAttrs(0) {
726#ifndef NDEBUG
Reid Klecknerec0fc032017-04-12 22:22:01 +0000727 assert(!Slots.empty() && "pointless AttributeListImpl");
Reid Klecknera82be602017-04-11 00:16:00 +0000728 if (Slots.size() >= 2) {
729 auto &PrevPair = Slots.front();
730 for (auto &CurPair : Slots.drop_front()) {
731 assert(PrevPair.first <= CurPair.first && "Attribute set not ordered!");
732 }
733 }
734#endif
735
736 // There's memory after the node where we can store the entries in.
737 std::copy(Slots.begin(), Slots.end(), getTrailingObjects<IndexAttrPair>());
738
739 // Initialize AvailableFunctionAttrs summary bitset.
Reid Klecknerec0fc032017-04-12 22:22:01 +0000740 static_assert(Attribute::EndAttrKinds <=
741 sizeof(AvailableFunctionAttrs) * CHAR_BIT,
742 "Too many attributes");
743 static_assert(AttributeList::FunctionIndex == ~0u,
744 "FunctionIndex should be biggest possible index");
745 const auto &Last = Slots.back();
746 if (Last.first == AttributeList::FunctionIndex) {
747 AttributeSet Node = Last.second;
748 for (Attribute I : Node) {
749 if (!I.isStringAttribute())
750 AvailableFunctionAttrs |= ((uint64_t)1) << I.getKindAsEnum();
Reid Klecknera82be602017-04-11 00:16:00 +0000751 }
752 }
753}
754
755void AttributeListImpl::Profile(FoldingSetNodeID &ID) const {
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000756 Profile(ID, makeArrayRef(getSlotPair(0), getNumSlots()));
Reid Klecknera82be602017-04-11 00:16:00 +0000757}
758
759void AttributeListImpl::Profile(
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000760 FoldingSetNodeID &ID, ArrayRef<std::pair<unsigned, AttributeSet>> Nodes) {
Reid Klecknera82be602017-04-11 00:16:00 +0000761 for (const auto &Node : Nodes) {
762 ID.AddInteger(Node.first);
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000763 ID.AddPointer(Node.second.SetNode);
Reid Klecknera82be602017-04-11 00:16:00 +0000764 }
765}
766
Matthias Braun8c209aa2017-01-28 02:02:38 +0000767#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Reid Klecknerb5180542017-03-21 16:57:19 +0000768LLVM_DUMP_METHOD void AttributeListImpl::dump() const {
769 AttributeList(const_cast<AttributeListImpl *>(this)).dump();
Peter Collingbourneabca2ec2013-08-02 22:34:30 +0000770}
Matthias Braun8c209aa2017-01-28 02:02:38 +0000771#endif
Peter Collingbourneabca2ec2013-08-02 22:34:30 +0000772
Bill Wendlingd509a662013-01-29 00:34:06 +0000773//===----------------------------------------------------------------------===//
Reid Klecknerb5180542017-03-21 16:57:19 +0000774// AttributeList Construction and Mutation Methods
Bill Wendlingd509a662013-01-29 00:34:06 +0000775//===----------------------------------------------------------------------===//
776
Reid Klecknerb5180542017-03-21 16:57:19 +0000777AttributeList AttributeList::getImpl(
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000778 LLVMContext &C, ArrayRef<std::pair<unsigned, AttributeSet>> Attrs) {
Reid Klecknereb9dd5b2017-04-10 23:31:05 +0000779 assert(!Attrs.empty() && "creating pointless AttributeList");
780#ifndef NDEBUG
781 unsigned LastIndex = 0;
782 bool IsFirst = true;
Reid Kleckner7f720332017-04-13 00:58:09 +0000783 for (auto &&AttrPair : Attrs) {
Reid Klecknereb9dd5b2017-04-10 23:31:05 +0000784 assert((IsFirst || LastIndex < AttrPair.first) &&
785 "unsorted or duplicate AttributeList indices");
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000786 assert(AttrPair.second.hasAttributes() && "pointless AttributeList slot");
Reid Klecknereb9dd5b2017-04-10 23:31:05 +0000787 LastIndex = AttrPair.first;
788 IsFirst = false;
789 }
790#endif
791
Bill Wendlingd509a662013-01-29 00:34:06 +0000792 LLVMContextImpl *pImpl = C.pImpl;
793 FoldingSetNodeID ID;
Reid Klecknerb5180542017-03-21 16:57:19 +0000794 AttributeListImpl::Profile(ID, Attrs);
Bill Wendlingd509a662013-01-29 00:34:06 +0000795
796 void *InsertPoint;
Reid Klecknerb5180542017-03-21 16:57:19 +0000797 AttributeListImpl *PA =
798 pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
Bill Wendlingd509a662013-01-29 00:34:06 +0000799
800 // If we didn't find any existing attributes of the same shape then
801 // create a new one and insert it.
802 if (!PA) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000803 // Coallocate entries after the AttributeListImpl itself.
James Y Knightaa365b22015-08-05 22:57:34 +0000804 void *Mem = ::operator new(
Reid Klecknerb5180542017-03-21 16:57:19 +0000805 AttributeListImpl::totalSizeToAlloc<IndexAttrPair>(Attrs.size()));
806 PA = new (Mem) AttributeListImpl(C, Attrs);
Bill Wendlingd509a662013-01-29 00:34:06 +0000807 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
808 }
809
810 // Return the AttributesList that we found or created.
Reid Klecknerb5180542017-03-21 16:57:19 +0000811 return AttributeList(PA);
Bill Wendlingd509a662013-01-29 00:34:06 +0000812}
813
Reid Klecknerb5180542017-03-21 16:57:19 +0000814AttributeList
815AttributeList::get(LLVMContext &C,
816 ArrayRef<std::pair<unsigned, Attribute>> Attrs) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000817 // If there are no attributes then return a null AttributesList pointer.
818 if (Attrs.empty())
Reid Klecknerb5180542017-03-21 16:57:19 +0000819 return AttributeList();
Bill Wendlingd509a662013-01-29 00:34:06 +0000820
Craig Toppere30b8ca2016-01-03 19:43:40 +0000821 assert(std::is_sorted(Attrs.begin(), Attrs.end(),
822 [](const std::pair<unsigned, Attribute> &LHS,
823 const std::pair<unsigned, Attribute> &RHS) {
824 return LHS.first < RHS.first;
825 }) && "Misordered Attributes list!");
David Majnemer0a16c222016-08-11 21:15:00 +0000826 assert(none_of(Attrs,
827 [](const std::pair<unsigned, Attribute> &Pair) {
828 return Pair.second.hasAttribute(Attribute::None);
829 }) &&
830 "Pointless attribute!");
Bill Wendlingd509a662013-01-29 00:34:06 +0000831
832 // Create a vector if (unsigned, AttributeSetNode*) pairs from the attributes
833 // list.
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000834 SmallVector<std::pair<unsigned, AttributeSet>, 8> AttrPairVec;
Eugene Zelenkodeaf6952017-02-17 00:00:09 +0000835 for (ArrayRef<std::pair<unsigned, Attribute>>::iterator I = Attrs.begin(),
Bill Wendlingd509a662013-01-29 00:34:06 +0000836 E = Attrs.end(); I != E; ) {
837 unsigned Index = I->first;
838 SmallVector<Attribute, 4> AttrVec;
NAKAMURA Takumif05d2f22013-01-29 15:18:16 +0000839 while (I != E && I->first == Index) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000840 AttrVec.push_back(I->second);
841 ++I;
842 }
843
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000844 AttrPairVec.emplace_back(Index, AttributeSet::get(C, AttrVec));
Bill Wendlingd509a662013-01-29 00:34:06 +0000845 }
846
847 return getImpl(C, AttrPairVec);
848}
849
Reid Klecknerb5180542017-03-21 16:57:19 +0000850AttributeList
851AttributeList::get(LLVMContext &C,
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000852 ArrayRef<std::pair<unsigned, AttributeSet>> Attrs) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000853 // If there are no attributes then return a null AttributesList pointer.
854 if (Attrs.empty())
Reid Klecknerb5180542017-03-21 16:57:19 +0000855 return AttributeList();
Bill Wendlingd509a662013-01-29 00:34:06 +0000856
857 return getImpl(C, Attrs);
858}
859
Reid Kleckner7f720332017-04-13 00:58:09 +0000860AttributeList AttributeList::get(LLVMContext &C, AttributeSet FnAttrs,
861 AttributeSet RetAttrs,
862 ArrayRef<AttributeSet> ArgAttrs) {
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000863 SmallVector<std::pair<unsigned, AttributeSet>, 8> AttrPairs;
Reid Kleckner7f720332017-04-13 00:58:09 +0000864 if (RetAttrs.hasAttributes())
865 AttrPairs.emplace_back(ReturnIndex, RetAttrs);
866 size_t Index = 1;
867 for (AttributeSet AS : ArgAttrs) {
868 if (AS.hasAttributes())
Reid Klecknereb9dd5b2017-04-10 23:31:05 +0000869 AttrPairs.emplace_back(Index, AS);
Reid Klecknereb9dd5b2017-04-10 23:31:05 +0000870 ++Index;
871 }
Reid Kleckner7f720332017-04-13 00:58:09 +0000872 if (FnAttrs.hasAttributes())
873 AttrPairs.emplace_back(FunctionIndex, FnAttrs);
Reid Klecknereb9dd5b2017-04-10 23:31:05 +0000874 if (AttrPairs.empty())
875 return AttributeList();
876 return getImpl(C, AttrPairs);
877}
878
Reid Klecknerb5180542017-03-21 16:57:19 +0000879AttributeList AttributeList::get(LLVMContext &C, unsigned Index,
880 const AttrBuilder &B) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000881 if (!B.hasAttributes())
Reid Klecknerb5180542017-03-21 16:57:19 +0000882 return AttributeList();
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000883 AttributeSet AS = AttributeSet::get(C, B);
884 std::pair<unsigned, AttributeSet> Arr[1] = {{Index, AS}};
Reid Klecknereb9dd5b2017-04-10 23:31:05 +0000885 return getImpl(C, Arr);
Bill Wendlingd509a662013-01-29 00:34:06 +0000886}
887
Reid Klecknerb5180542017-03-21 16:57:19 +0000888AttributeList AttributeList::get(LLVMContext &C, unsigned Index,
889 ArrayRef<Attribute::AttrKind> Kinds) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000890 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
Amaury Sechet392638d2016-06-14 20:27:35 +0000891 for (Attribute::AttrKind K : Kinds)
David Majnemer0a16c222016-08-11 21:15:00 +0000892 Attrs.emplace_back(Index, Attribute::get(C, K));
Bill Wendlingd509a662013-01-29 00:34:06 +0000893 return get(C, Attrs);
894}
895
Reid Klecknerb5180542017-03-21 16:57:19 +0000896AttributeList AttributeList::get(LLVMContext &C, unsigned Index,
897 ArrayRef<StringRef> Kinds) {
Amaury Sechet6100adf2016-06-15 17:50:39 +0000898 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
899 for (StringRef K : Kinds)
David Majnemer0a16c222016-08-11 21:15:00 +0000900 Attrs.emplace_back(Index, Attribute::get(C, K));
Amaury Sechet6100adf2016-06-15 17:50:39 +0000901 return get(C, Attrs);
902}
903
Reid Klecknerb5180542017-03-21 16:57:19 +0000904AttributeList AttributeList::get(LLVMContext &C,
905 ArrayRef<AttributeList> Attrs) {
906 if (Attrs.empty())
907 return AttributeList();
Peter Collingbournebd6c7452013-08-02 22:29:40 +0000908 if (Attrs.size() == 1) return Attrs[0];
Bill Wendlingd509a662013-01-29 00:34:06 +0000909
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000910 SmallVector<std::pair<unsigned, AttributeSet>, 8> AttrNodeVec;
Reid Klecknerb5180542017-03-21 16:57:19 +0000911 AttributeListImpl *A0 = Attrs[0].pImpl;
Peter Collingbournebd6c7452013-08-02 22:29:40 +0000912 if (A0)
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000913 AttrNodeVec.append(A0->getSlotPair(0), A0->getSlotPair(A0->getNumSlots()));
Peter Collingbournebd6c7452013-08-02 22:29:40 +0000914 // Copy all attributes from Attrs into AttrNodeVec while keeping AttrNodeVec
915 // ordered by index. Because we know that each list in Attrs is ordered by
916 // index we only need to merge each successive list in rather than doing a
917 // full sort.
918 for (unsigned I = 1, E = Attrs.size(); I != E; ++I) {
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000919 AttributeListImpl *ALI = Attrs[I].pImpl;
920 if (!ALI) continue;
921 SmallVector<std::pair<unsigned, AttributeSet>, 8>::iterator
Peter Collingbournebd6c7452013-08-02 22:29:40 +0000922 ANVI = AttrNodeVec.begin(), ANVE;
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000923 for (const IndexAttrPair *AI = ALI->getSlotPair(0),
924 *AE = ALI->getSlotPair(ALI->getNumSlots());
Peter Collingbournebd6c7452013-08-02 22:29:40 +0000925 AI != AE; ++AI) {
926 ANVE = AttrNodeVec.end();
927 while (ANVI != ANVE && ANVI->first <= AI->first)
928 ++ANVI;
929 ANVI = AttrNodeVec.insert(ANVI, *AI) + 1;
930 }
Bill Wendlingd509a662013-01-29 00:34:06 +0000931 }
932
933 return getImpl(C, AttrNodeVec);
934}
935
Reid Klecknerb5180542017-03-21 16:57:19 +0000936AttributeList AttributeList::addAttribute(LLVMContext &C, unsigned Index,
937 Attribute::AttrKind Kind) const {
Amaury Sechet392638d2016-06-14 20:27:35 +0000938 if (hasAttribute(Index, Kind)) return *this;
Reid Klecknerb5180542017-03-21 16:57:19 +0000939 return addAttributes(C, Index, AttributeList::get(C, Index, Kind));
Reed Kotler795c7b42013-03-13 20:20:08 +0000940}
941
Reid Klecknerb5180542017-03-21 16:57:19 +0000942AttributeList AttributeList::addAttribute(LLVMContext &C, unsigned Index,
943 StringRef Kind,
944 StringRef Value) const {
Eugene Zelenkodeaf6952017-02-17 00:00:09 +0000945 AttrBuilder B;
Bill Wendling3b2f6102013-07-25 18:34:24 +0000946 B.addAttribute(Kind, Value);
Reid Klecknerb5180542017-03-21 16:57:19 +0000947 return addAttributes(C, Index, AttributeList::get(C, Index, B));
Bill Wendling3b2f6102013-07-25 18:34:24 +0000948}
949
Reid Klecknerb5180542017-03-21 16:57:19 +0000950AttributeList AttributeList::addAttribute(LLVMContext &C,
951 ArrayRef<unsigned> Indices,
952 Attribute A) const {
Reid Klecknereb9dd5b2017-04-10 23:31:05 +0000953 assert(std::is_sorted(Indices.begin(), Indices.end()));
Reid Kleckner324c99d2017-04-10 20:18:10 +0000954
Reid Klecknereb9dd5b2017-04-10 23:31:05 +0000955 unsigned I = 0, E = pImpl ? pImpl->getNumSlots() : 0;
956 SmallVector<IndexAttrPair, 4> AttrVec;
957 for (unsigned Index : Indices) {
958 // Add all attribute slots before the current index.
959 for (; I < E && getSlotIndex(I) < Index; ++I)
Reid Kleckner63b26f02017-04-24 22:25:02 +0000960 AttrVec.emplace_back(getSlotIndex(I), pImpl->getSlotAttributes(I));
Reid Klecknereb9dd5b2017-04-10 23:31:05 +0000961
962 // Add the attribute at this index. If we already have attributes at this
963 // index, merge them into a new set.
964 AttrBuilder B;
965 if (I < E && getSlotIndex(I) == Index) {
Reid Kleckner63b26f02017-04-24 22:25:02 +0000966 B.merge(AttrBuilder(pImpl->getSlotAttributes(I)));
Akira Hatanaka237916b2015-12-02 06:58:49 +0000967 ++I;
Akira Hatanaka237916b2015-12-02 06:58:49 +0000968 }
Reid Klecknereb9dd5b2017-04-10 23:31:05 +0000969 B.addAttribute(A);
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000970 AttrVec.emplace_back(Index, AttributeSet::get(C, B));
Akira Hatanaka237916b2015-12-02 06:58:49 +0000971 }
972
Reid Klecknereb9dd5b2017-04-10 23:31:05 +0000973 // Add remaining attributes.
974 for (; I < E; ++I)
Reid Kleckner63b26f02017-04-24 22:25:02 +0000975 AttrVec.emplace_back(getSlotIndex(I), pImpl->getSlotAttributes(I));
Akira Hatanaka237916b2015-12-02 06:58:49 +0000976
Reid Klecknereb9dd5b2017-04-10 23:31:05 +0000977 return get(C, AttrVec);
Akira Hatanaka237916b2015-12-02 06:58:49 +0000978}
979
Reid Klecknerb5180542017-03-21 16:57:19 +0000980AttributeList AttributeList::addAttributes(LLVMContext &C, unsigned Index,
981 AttributeList Attrs) const {
Bill Wendlingd509a662013-01-29 00:34:06 +0000982 if (!pImpl) return Attrs;
983 if (!Attrs.pImpl) return *this;
984
Reid Klecknereb9dd5b2017-04-10 23:31:05 +0000985 return addAttributes(C, Index, Attrs.getAttributes(Index));
986}
987
988AttributeList AttributeList::addAttributes(LLVMContext &C, unsigned Index,
Reid Kleckner61906252017-04-19 01:51:13 +0000989 const AttrBuilder &B) const {
990 if (!B.hasAttributes())
Reid Klecknereb9dd5b2017-04-10 23:31:05 +0000991 return *this;
992
Reid Klecknerfe64c012017-04-18 22:10:18 +0000993 if (!pImpl)
Reid Kleckner61906252017-04-19 01:51:13 +0000994 return AttributeList::get(C, {{Index, AttributeSet::get(C, B)}});
Reid Klecknerfe64c012017-04-18 22:10:18 +0000995
Bill Wendlingd509a662013-01-29 00:34:06 +0000996#ifndef NDEBUG
997 // FIXME it is not obvious how this should work for alignment. For now, say
998 // we can't change a known alignment.
Bill Wendling211316c2013-04-18 20:17:28 +0000999 unsigned OldAlign = getParamAlignment(Index);
Reid Kleckner61906252017-04-19 01:51:13 +00001000 unsigned NewAlign = B.getAlignment();
Bill Wendlingd509a662013-01-29 00:34:06 +00001001 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
1002 "Attempt to change alignment!");
1003#endif
1004
Reid Kleckner61906252017-04-19 01:51:13 +00001005 SmallVector<IndexAttrPair, 4> AttrVec;
Amaury Sechet24c84fd2016-06-14 22:04:16 +00001006 uint64_t NumAttrs = pImpl->getNumSlots();
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00001007 unsigned I;
1008
1009 // Add all the attribute slots before the one we need to merge.
1010 for (I = 0; I < NumAttrs; ++I) {
1011 if (getSlotIndex(I) >= Index)
Bill Wendlingd509a662013-01-29 00:34:06 +00001012 break;
Reid Kleckner63b26f02017-04-24 22:25:02 +00001013 AttrVec.emplace_back(getSlotIndex(I), pImpl->getSlotAttributes(I));
Bill Wendlingd509a662013-01-29 00:34:06 +00001014 }
1015
Reid Kleckner61906252017-04-19 01:51:13 +00001016 AttrBuilder NewAttrs;
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00001017 if (I < NumAttrs && getSlotIndex(I) == Index) {
Reid Kleckner61906252017-04-19 01:51:13 +00001018 // We need to merge the attribute sets.
Reid Kleckner63b26f02017-04-24 22:25:02 +00001019 NewAttrs.merge(pImpl->getSlotAttributes(I));
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00001020 ++I;
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00001021 }
Reid Kleckner61906252017-04-19 01:51:13 +00001022 NewAttrs.merge(B);
1023
1024 // Add the new or merged attribute set at this index.
1025 AttrVec.emplace_back(Index, AttributeSet::get(C, NewAttrs));
Bill Wendlingd509a662013-01-29 00:34:06 +00001026
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00001027 // Add the remaining entries.
1028 for (; I < NumAttrs; ++I)
Reid Kleckner63b26f02017-04-24 22:25:02 +00001029 AttrVec.emplace_back(getSlotIndex(I), pImpl->getSlotAttributes(I));
Bill Wendlingd509a662013-01-29 00:34:06 +00001030
Reid Kleckner61906252017-04-19 01:51:13 +00001031 return get(C, AttrVec);
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00001032}
1033
Reid Klecknerb5180542017-03-21 16:57:19 +00001034AttributeList AttributeList::removeAttribute(LLVMContext &C, unsigned Index,
1035 Attribute::AttrKind Kind) const {
Amaury Sechet392638d2016-06-14 20:27:35 +00001036 if (!hasAttribute(Index, Kind)) return *this;
Reid Klecknerb5180542017-03-21 16:57:19 +00001037 return removeAttributes(C, Index, AttributeList::get(C, Index, Kind));
Bill Wendlingd509a662013-01-29 00:34:06 +00001038}
1039
Reid Klecknerb5180542017-03-21 16:57:19 +00001040AttributeList AttributeList::removeAttribute(LLVMContext &C, unsigned Index,
1041 StringRef Kind) const {
Amaury Sechet6100adf2016-06-15 17:50:39 +00001042 if (!hasAttribute(Index, Kind)) return *this;
Reid Klecknerb5180542017-03-21 16:57:19 +00001043 return removeAttributes(C, Index, AttributeList::get(C, Index, Kind));
Amaury Sechet6100adf2016-06-15 17:50:39 +00001044}
1045
Reid Klecknerb5180542017-03-21 16:57:19 +00001046AttributeList AttributeList::removeAttributes(LLVMContext &C, unsigned Index,
1047 AttributeList Attrs) const {
Reid Kleckner62731e12017-04-20 18:08:36 +00001048 return removeAttributes(C, Index, AttrBuilder(Attrs.getAttributes(Index)));
Bill Wendlingd509a662013-01-29 00:34:06 +00001049}
1050
Reid Klecknerb5180542017-03-21 16:57:19 +00001051AttributeList AttributeList::removeAttributes(LLVMContext &C, unsigned Index,
1052 const AttrBuilder &Attrs) const {
1053 if (!pImpl)
1054 return AttributeList();
Pete Cooperd2a44612015-05-06 23:19:43 +00001055
1056 // FIXME it is not obvious how this should work for alignment.
1057 // For now, say we can't pass in alignment, which no current use does.
1058 assert(!Attrs.hasAlignmentAttr() && "Attempt to change alignment!");
1059
1060 // Add the attribute slots before the one we're trying to add.
Reid Kleckner62731e12017-04-20 18:08:36 +00001061 SmallVector<IndexAttrPair, 4> AttrSets;
Amaury Sechet24c84fd2016-06-14 22:04:16 +00001062 uint64_t NumAttrs = pImpl->getNumSlots();
Reid Kleckner62731e12017-04-20 18:08:36 +00001063 AttrBuilder B;
Pete Cooperd2a44612015-05-06 23:19:43 +00001064 uint64_t LastIndex = 0;
1065 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
1066 if (getSlotIndex(I) >= Index) {
Reid Kleckner62731e12017-04-20 18:08:36 +00001067 if (getSlotIndex(I) == Index)
Reid Kleckner63b26f02017-04-24 22:25:02 +00001068 B = AttrBuilder(getSlotAttributes(LastIndex++));
Pete Cooperd2a44612015-05-06 23:19:43 +00001069 break;
1070 }
1071 LastIndex = I + 1;
Reid Kleckner63b26f02017-04-24 22:25:02 +00001072 AttrSets.push_back({getSlotIndex(I), getSlotAttributes(I)});
Pete Cooperd2a44612015-05-06 23:19:43 +00001073 }
1074
Reid Kleckner62731e12017-04-20 18:08:36 +00001075 // Remove the attributes from the existing set and add them.
Pete Cooperd2a44612015-05-06 23:19:43 +00001076 B.remove(Attrs);
Reid Kleckner62731e12017-04-20 18:08:36 +00001077 if (B.hasAttributes())
1078 AttrSets.push_back({Index, AttributeSet::get(C, B)});
Pete Cooperd2a44612015-05-06 23:19:43 +00001079
1080 // Add the remaining attribute slots.
1081 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
Reid Kleckner63b26f02017-04-24 22:25:02 +00001082 AttrSets.push_back({getSlotIndex(I), getSlotAttributes(I)});
Pete Cooperd2a44612015-05-06 23:19:43 +00001083
Reid Kleckner62731e12017-04-20 18:08:36 +00001084 return get(C, AttrSets);
Pete Cooperd2a44612015-05-06 23:19:43 +00001085}
1086
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00001087AttributeList AttributeList::removeAttributes(LLVMContext &C,
1088 unsigned WithoutIndex) const {
1089 if (!pImpl)
1090 return AttributeList();
1091
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001092 SmallVector<std::pair<unsigned, AttributeSet>, 4> AttrSet;
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00001093 for (unsigned I = 0, E = pImpl->getNumSlots(); I != E; ++I) {
1094 unsigned Index = getSlotIndex(I);
1095 if (Index != WithoutIndex)
Reid Kleckner63b26f02017-04-24 22:25:02 +00001096 AttrSet.push_back({Index, pImpl->getSlotAttributes(I)});
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00001097 }
1098 return get(C, AttrSet);
1099}
1100
Reid Klecknerb5180542017-03-21 16:57:19 +00001101AttributeList AttributeList::addDereferenceableAttr(LLVMContext &C,
1102 unsigned Index,
1103 uint64_t Bytes) const {
Eugene Zelenkodeaf6952017-02-17 00:00:09 +00001104 AttrBuilder B;
Ramkumar Ramachandra8fcb4982015-02-14 19:37:54 +00001105 B.addDereferenceableAttr(Bytes);
Reid Klecknerb5180542017-03-21 16:57:19 +00001106 return addAttributes(C, Index, AttributeList::get(C, Index, B));
Ramkumar Ramachandra8fcb4982015-02-14 19:37:54 +00001107}
1108
Reid Klecknerb5180542017-03-21 16:57:19 +00001109AttributeList
1110AttributeList::addDereferenceableOrNullAttr(LLVMContext &C, unsigned Index,
1111 uint64_t Bytes) const {
Eugene Zelenkodeaf6952017-02-17 00:00:09 +00001112 AttrBuilder B;
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001113 B.addDereferenceableOrNullAttr(Bytes);
Reid Klecknerb5180542017-03-21 16:57:19 +00001114 return addAttributes(C, Index, AttributeList::get(C, Index, B));
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001115}
1116
Reid Klecknerb5180542017-03-21 16:57:19 +00001117AttributeList
1118AttributeList::addAllocSizeAttr(LLVMContext &C, unsigned Index,
1119 unsigned ElemSizeArg,
1120 const Optional<unsigned> &NumElemsArg) {
Eugene Zelenkodeaf6952017-02-17 00:00:09 +00001121 AttrBuilder B;
George Burgess IV278199f2016-04-12 01:05:35 +00001122 B.addAllocSizeAttr(ElemSizeArg, NumElemsArg);
Reid Klecknerb5180542017-03-21 16:57:19 +00001123 return addAttributes(C, Index, AttributeList::get(C, Index, B));
George Burgess IV278199f2016-04-12 01:05:35 +00001124}
1125
Bill Wendlingd509a662013-01-29 00:34:06 +00001126//===----------------------------------------------------------------------===//
Reid Klecknerb5180542017-03-21 16:57:19 +00001127// AttributeList Accessor Methods
Bill Wendlingd509a662013-01-29 00:34:06 +00001128//===----------------------------------------------------------------------===//
1129
Reid Klecknerb5180542017-03-21 16:57:19 +00001130LLVMContext &AttributeList::getContext() const { return pImpl->getContext(); }
1131
Reid Klecknerf021fab2017-04-13 23:12:13 +00001132AttributeSet AttributeList::getParamAttributes(unsigned ArgNo) const {
1133 return getAttributes(ArgNo + 1);
Bill Wendling5d020a32013-02-10 05:00:40 +00001134}
1135
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001136AttributeSet AttributeList::getRetAttributes() const {
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00001137 return getAttributes(ReturnIndex);
Bill Wendlingd509a662013-01-29 00:34:06 +00001138}
1139
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001140AttributeSet AttributeList::getFnAttributes() const {
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00001141 return getAttributes(FunctionIndex);
Bill Wendlingd509a662013-01-29 00:34:06 +00001142}
1143
Reid Klecknerb5180542017-03-21 16:57:19 +00001144bool AttributeList::hasAttribute(unsigned Index,
1145 Attribute::AttrKind Kind) const {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001146 return getAttributes(Index).hasAttribute(Kind);
Bill Wendlingd509a662013-01-29 00:34:06 +00001147}
1148
Reid Klecknerb5180542017-03-21 16:57:19 +00001149bool AttributeList::hasAttribute(unsigned Index, StringRef Kind) const {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001150 return getAttributes(Index).hasAttribute(Kind);
Bill Wendlingbce7b972013-02-13 08:42:21 +00001151}
1152
Reid Klecknerb5180542017-03-21 16:57:19 +00001153bool AttributeList::hasAttributes(unsigned Index) const {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001154 return getAttributes(Index).hasAttributes();
Bill Wendlingd509a662013-01-29 00:34:06 +00001155}
1156
Reid Klecknerb5180542017-03-21 16:57:19 +00001157bool AttributeList::hasFnAttribute(Attribute::AttrKind Kind) const {
Matthias Braun33282812016-01-29 22:25:19 +00001158 return pImpl && pImpl->hasFnAttribute(Kind);
1159}
1160
Reid Klecknerb5180542017-03-21 16:57:19 +00001161bool AttributeList::hasFnAttribute(StringRef Kind) const {
1162 return hasAttribute(AttributeList::FunctionIndex, Kind);
Amaury Sechet5f04d812016-09-09 04:50:38 +00001163}
1164
Reid Klecknerf021fab2017-04-13 23:12:13 +00001165bool AttributeList::hasParamAttribute(unsigned ArgNo,
1166 Attribute::AttrKind Kind) const {
1167 return hasAttribute(ArgNo + 1, Kind);
1168}
1169
Reid Klecknerb5180542017-03-21 16:57:19 +00001170bool AttributeList::hasAttrSomewhere(Attribute::AttrKind Attr,
1171 unsigned *Index) const {
Craig Topperc6207612014-04-09 06:08:46 +00001172 if (!pImpl) return false;
Bill Wendlingd509a662013-01-29 00:34:06 +00001173
Amaury Sechet24c84fd2016-06-14 22:04:16 +00001174 for (unsigned I = 0, E = pImpl->getNumSlots(); I != E; ++I)
Reid Klecknerb5180542017-03-21 16:57:19 +00001175 for (AttributeListImpl::iterator II = pImpl->begin(I), IE = pImpl->end(I);
1176 II != IE; ++II)
Hal Finkele87ad542016-07-10 23:01:32 +00001177 if (II->hasAttribute(Attr)) {
1178 if (Index) *Index = pImpl->getSlotIndex(I);
Bill Wendlingd509a662013-01-29 00:34:06 +00001179 return true;
Hal Finkele87ad542016-07-10 23:01:32 +00001180 }
Bill Wendlingd509a662013-01-29 00:34:06 +00001181
1182 return false;
1183}
1184
Reid Klecknerb5180542017-03-21 16:57:19 +00001185Attribute AttributeList::getAttribute(unsigned Index,
1186 Attribute::AttrKind Kind) const {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001187 return getAttributes(Index).getAttribute(Kind);
Bill Wendlingbce7b972013-02-13 08:42:21 +00001188}
1189
Reid Klecknerb5180542017-03-21 16:57:19 +00001190Attribute AttributeList::getAttribute(unsigned Index, StringRef Kind) const {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001191 return getAttributes(Index).getAttribute(Kind);
Bill Wendlingbce7b972013-02-13 08:42:21 +00001192}
1193
Reid Klecknerb5180542017-03-21 16:57:19 +00001194unsigned AttributeList::getParamAlignment(unsigned Index) const {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001195 return getAttributes(Index).getAlignment();
Bill Wendlingd509a662013-01-29 00:34:06 +00001196}
1197
Reid Klecknerb5180542017-03-21 16:57:19 +00001198unsigned AttributeList::getStackAlignment(unsigned Index) const {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001199 return getAttributes(Index).getStackAlignment();
Bill Wendlingd509a662013-01-29 00:34:06 +00001200}
1201
Reid Klecknerb5180542017-03-21 16:57:19 +00001202uint64_t AttributeList::getDereferenceableBytes(unsigned Index) const {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001203 return getAttributes(Index).getDereferenceableBytes();
Hal Finkelb0407ba2014-07-18 15:51:28 +00001204}
1205
Reid Klecknerb5180542017-03-21 16:57:19 +00001206uint64_t AttributeList::getDereferenceableOrNullBytes(unsigned Index) const {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001207 return getAttributes(Index).getDereferenceableOrNullBytes();
Sanjoy Das06cf33f2015-05-06 17:41:54 +00001208}
1209
George Burgess IV278199f2016-04-12 01:05:35 +00001210std::pair<unsigned, Optional<unsigned>>
Reid Klecknerb5180542017-03-21 16:57:19 +00001211AttributeList::getAllocSizeArgs(unsigned Index) const {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001212 return getAttributes(Index).getAllocSizeArgs();
George Burgess IV278199f2016-04-12 01:05:35 +00001213}
1214
Reid Klecknerb5180542017-03-21 16:57:19 +00001215std::string AttributeList::getAsString(unsigned Index, bool InAttrGrp) const {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001216 return getAttributes(Index).getAsString(InAttrGrp);
Bill Wendlingd509a662013-01-29 00:34:06 +00001217}
1218
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001219AttributeSet AttributeList::getAttributes(unsigned Index) const {
1220 if (!pImpl) return AttributeSet();
Bill Wendlingd509a662013-01-29 00:34:06 +00001221
Bill Wendlingf2955aa2013-01-29 03:20:31 +00001222 // Loop through to find the attribute node we want.
Amaury Sechet24c84fd2016-06-14 22:04:16 +00001223 for (unsigned I = 0, E = pImpl->getNumSlots(); I != E; ++I)
Bill Wendling211316c2013-04-18 20:17:28 +00001224 if (pImpl->getSlotIndex(I) == Index)
Reid Kleckner63b26f02017-04-24 22:25:02 +00001225 return pImpl->getSlotAttributes(I);
Bill Wendlingd509a662013-01-29 00:34:06 +00001226
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001227 return AttributeSet();
Bill Wendlingd509a662013-01-29 00:34:06 +00001228}
1229
Reid Klecknerb5180542017-03-21 16:57:19 +00001230AttributeList::iterator AttributeList::begin(unsigned Slot) const {
Bill Wendlinga9174862013-01-31 23:53:05 +00001231 if (!pImpl)
1232 return ArrayRef<Attribute>().begin();
Bill Wendling211316c2013-04-18 20:17:28 +00001233 return pImpl->begin(Slot);
Bill Wendlinga9174862013-01-31 23:53:05 +00001234}
1235
Reid Klecknerb5180542017-03-21 16:57:19 +00001236AttributeList::iterator AttributeList::end(unsigned Slot) const {
Bill Wendlinga9174862013-01-31 23:53:05 +00001237 if (!pImpl)
1238 return ArrayRef<Attribute>().end();
Bill Wendling211316c2013-04-18 20:17:28 +00001239 return pImpl->end(Slot);
Bill Wendlinga9174862013-01-31 23:53:05 +00001240}
1241
Bill Wendlingd509a662013-01-29 00:34:06 +00001242//===----------------------------------------------------------------------===//
Reid Klecknerb5180542017-03-21 16:57:19 +00001243// AttributeList Introspection Methods
Bill Wendlingd509a662013-01-29 00:34:06 +00001244//===----------------------------------------------------------------------===//
1245
Reid Klecknerb5180542017-03-21 16:57:19 +00001246unsigned AttributeList::getNumSlots() const {
Amaury Sechet24c84fd2016-06-14 22:04:16 +00001247 return pImpl ? pImpl->getNumSlots() : 0;
Bill Wendlingd509a662013-01-29 00:34:06 +00001248}
1249
Reid Klecknerb5180542017-03-21 16:57:19 +00001250unsigned AttributeList::getSlotIndex(unsigned Slot) const {
Amaury Sechet24c84fd2016-06-14 22:04:16 +00001251 assert(pImpl && Slot < pImpl->getNumSlots() &&
Bill Wendlingd509a662013-01-29 00:34:06 +00001252 "Slot # out of range!");
1253 return pImpl->getSlotIndex(Slot);
1254}
1255
Reid Kleckner63b26f02017-04-24 22:25:02 +00001256AttributeSet AttributeList::getSlotAttributes(unsigned Slot) const {
Amaury Sechet24c84fd2016-06-14 22:04:16 +00001257 assert(pImpl && Slot < pImpl->getNumSlots() &&
Bill Wendlingd509a662013-01-29 00:34:06 +00001258 "Slot # out of range!");
1259 return pImpl->getSlotAttributes(Slot);
1260}
1261
Matthias Braun8c209aa2017-01-28 02:02:38 +00001262#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Reid Klecknerb5180542017-03-21 16:57:19 +00001263LLVM_DUMP_METHOD void AttributeList::dump() const {
Bill Wendlingd509a662013-01-29 00:34:06 +00001264 dbgs() << "PAL[\n";
1265
1266 for (unsigned i = 0, e = getNumSlots(); i < e; ++i) {
1267 uint64_t Index = getSlotIndex(i);
1268 dbgs() << " { ";
1269 if (Index == ~0U)
1270 dbgs() << "~0U";
1271 else
1272 dbgs() << Index;
1273 dbgs() << " => " << getAsString(Index) << " }\n";
1274 }
1275
1276 dbgs() << "]\n";
1277}
Matthias Braun8c209aa2017-01-28 02:02:38 +00001278#endif
Bill Wendlingd509a662013-01-29 00:34:06 +00001279
Bill Wendlingabf3feb2012-10-05 06:44:41 +00001280//===----------------------------------------------------------------------===//
Bill Wendlingcd330342013-01-04 23:27:34 +00001281// AttrBuilder Method Implementations
Bill Wendlingabf3feb2012-10-05 06:44:41 +00001282//===----------------------------------------------------------------------===//
1283
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001284AttrBuilder::AttrBuilder(AttributeList AL, unsigned Index) {
1285 AttributeListImpl *pImpl = AL.pImpl;
Bill Wendling096f5442013-01-07 08:24:35 +00001286 if (!pImpl) return;
1287
Amaury Sechet24c84fd2016-06-14 22:04:16 +00001288 for (unsigned I = 0, E = pImpl->getNumSlots(); I != E; ++I) {
Bill Wendling211316c2013-04-18 20:17:28 +00001289 if (pImpl->getSlotIndex(I) != Index) continue;
Bill Wendling096f5442013-01-07 08:24:35 +00001290
Reid Klecknerb5180542017-03-21 16:57:19 +00001291 for (AttributeListImpl::iterator II = pImpl->begin(I), IE = pImpl->end(I);
1292 II != IE; ++II)
Bill Wendling23804da2013-01-31 23:38:01 +00001293 addAttribute(*II);
Bill Wendling9eb689c2013-01-28 00:21:34 +00001294
1295 break;
1296 }
Bill Wendling096f5442013-01-07 08:24:35 +00001297}
1298
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001299AttrBuilder::AttrBuilder(AttributeSet AS) {
1300 if (AS.hasAttributes()) {
1301 for (const Attribute &A : AS)
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00001302 addAttribute(A);
1303 }
1304}
1305
Bill Wendlingcd330342013-01-04 23:27:34 +00001306void AttrBuilder::clear() {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001307 Attrs.reset();
Sanjoy Das88d0fde2015-09-03 22:27:42 +00001308 TargetDepAttrs.clear();
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001309 Alignment = StackAlignment = DerefBytes = DerefOrNullBytes = 0;
George Burgess IV278199f2016-04-12 01:05:35 +00001310 AllocSizeArgs = 0;
Bill Wendlingcd330342013-01-04 23:27:34 +00001311}
1312
1313AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001314 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
Bill Wendling1c7cc8a2013-01-31 23:16:25 +00001315 assert(Val != Attribute::Alignment && Val != Attribute::StackAlignment &&
George Burgess IV278199f2016-04-12 01:05:35 +00001316 Val != Attribute::Dereferenceable && Val != Attribute::AllocSize &&
Hal Finkelb0407ba2014-07-18 15:51:28 +00001317 "Adding integer attribute without adding a value!");
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001318 Attrs[Val] = true;
Bill Wendling7c04e042012-10-09 19:01:18 +00001319 return *this;
Bill Wendlingabf3feb2012-10-05 06:44:41 +00001320}
1321
Bill Wendling23804da2013-01-31 23:38:01 +00001322AttrBuilder &AttrBuilder::addAttribute(Attribute Attr) {
Bill Wendling0a437302013-02-10 10:13:23 +00001323 if (Attr.isStringAttribute()) {
1324 addAttribute(Attr.getKindAsString(), Attr.getValueAsString());
1325 return *this;
1326 }
1327
Bill Wendling3f12ac22013-02-05 22:37:24 +00001328 Attribute::AttrKind Kind = Attr.getKindAsEnum();
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001329 Attrs[Kind] = true;
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001330
Bill Wendling3f12ac22013-02-05 22:37:24 +00001331 if (Kind == Attribute::Alignment)
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001332 Alignment = Attr.getAlignment();
Bill Wendling3f12ac22013-02-05 22:37:24 +00001333 else if (Kind == Attribute::StackAlignment)
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001334 StackAlignment = Attr.getStackAlignment();
Hal Finkelb0407ba2014-07-18 15:51:28 +00001335 else if (Kind == Attribute::Dereferenceable)
1336 DerefBytes = Attr.getDereferenceableBytes();
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001337 else if (Kind == Attribute::DereferenceableOrNull)
1338 DerefOrNullBytes = Attr.getDereferenceableOrNullBytes();
George Burgess IV278199f2016-04-12 01:05:35 +00001339 else if (Kind == Attribute::AllocSize)
1340 AllocSizeArgs = Attr.getValueAsInt();
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001341 return *this;
1342}
1343
Bill Wendlingb9c5b1a2013-02-05 08:09:32 +00001344AttrBuilder &AttrBuilder::addAttribute(StringRef A, StringRef V) {
1345 TargetDepAttrs[A] = V;
1346 return *this;
1347}
1348
Bill Wendling23804da2013-01-31 23:38:01 +00001349AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001350 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
1351 Attrs[Val] = false;
Bill Wendling23804da2013-01-31 23:38:01 +00001352
1353 if (Val == Attribute::Alignment)
1354 Alignment = 0;
1355 else if (Val == Attribute::StackAlignment)
1356 StackAlignment = 0;
Hal Finkelb0407ba2014-07-18 15:51:28 +00001357 else if (Val == Attribute::Dereferenceable)
1358 DerefBytes = 0;
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001359 else if (Val == Attribute::DereferenceableOrNull)
1360 DerefOrNullBytes = 0;
George Burgess IV278199f2016-04-12 01:05:35 +00001361 else if (Val == Attribute::AllocSize)
1362 AllocSizeArgs = 0;
Bill Wendling23804da2013-01-31 23:38:01 +00001363
1364 return *this;
1365}
1366
Reid Klecknerb5180542017-03-21 16:57:19 +00001367AttrBuilder &AttrBuilder::removeAttributes(AttributeList A, uint64_t Index) {
Reid Kleckner6652a522017-04-28 18:37:16 +00001368 remove(A.getAttributes(Index));
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001369 return *this;
1370}
1371
Bill Wendlingb9c5b1a2013-02-05 08:09:32 +00001372AttrBuilder &AttrBuilder::removeAttribute(StringRef A) {
1373 std::map<std::string, std::string>::iterator I = TargetDepAttrs.find(A);
1374 if (I != TargetDepAttrs.end())
1375 TargetDepAttrs.erase(I);
1376 return *this;
1377}
1378
George Burgess IV278199f2016-04-12 01:05:35 +00001379std::pair<unsigned, Optional<unsigned>> AttrBuilder::getAllocSizeArgs() const {
1380 return unpackAllocSizeArgs(AllocSizeArgs);
1381}
1382
Bill Wendling50d27842012-10-15 20:35:56 +00001383AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
Bill Wendlingabd5ba22012-10-14 03:58:29 +00001384 if (Align == 0) return *this;
Bill Wendlingcd330342013-01-04 23:27:34 +00001385
Bill Wendlingabf3feb2012-10-05 06:44:41 +00001386 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1387 assert(Align <= 0x40000000 && "Alignment too large.");
Bill Wendlingcd330342013-01-04 23:27:34 +00001388
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001389 Attrs[Attribute::Alignment] = true;
Bill Wendlingcd330342013-01-04 23:27:34 +00001390 Alignment = Align;
Bill Wendlingabd5ba22012-10-14 03:58:29 +00001391 return *this;
Chris Lattner3e13b8c2008-01-02 23:42:30 +00001392}
1393
Bill Wendlingcd330342013-01-04 23:27:34 +00001394AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
1395 // Default alignment, allow the target to define how to align it.
1396 if (Align == 0) return *this;
1397
1398 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1399 assert(Align <= 0x100 && "Alignment too large.");
1400
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001401 Attrs[Attribute::StackAlignment] = true;
Bill Wendlingcd330342013-01-04 23:27:34 +00001402 StackAlignment = Align;
1403 return *this;
1404}
1405
Hal Finkelb0407ba2014-07-18 15:51:28 +00001406AttrBuilder &AttrBuilder::addDereferenceableAttr(uint64_t Bytes) {
1407 if (Bytes == 0) return *this;
1408
1409 Attrs[Attribute::Dereferenceable] = true;
1410 DerefBytes = Bytes;
1411 return *this;
1412}
1413
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001414AttrBuilder &AttrBuilder::addDereferenceableOrNullAttr(uint64_t Bytes) {
1415 if (Bytes == 0)
1416 return *this;
1417
1418 Attrs[Attribute::DereferenceableOrNull] = true;
1419 DerefOrNullBytes = Bytes;
1420 return *this;
1421}
1422
George Burgess IV278199f2016-04-12 01:05:35 +00001423AttrBuilder &AttrBuilder::addAllocSizeAttr(unsigned ElemSize,
1424 const Optional<unsigned> &NumElems) {
1425 return addAllocSizeAttrFromRawRepr(packAllocSizeArgs(ElemSize, NumElems));
1426}
1427
1428AttrBuilder &AttrBuilder::addAllocSizeAttrFromRawRepr(uint64_t RawArgs) {
1429 // (0, 0) is our "not present" value, so we need to check for it here.
1430 assert(RawArgs && "Invalid allocsize arguments -- given allocsize(0, 0)");
1431
1432 Attrs[Attribute::AllocSize] = true;
1433 // Reuse existing machinery to store this as a single 64-bit integer so we can
1434 // save a few bytes over using a pair<unsigned, Optional<unsigned>>.
1435 AllocSizeArgs = RawArgs;
1436 return *this;
1437}
1438
Bill Wendlinge2614922013-02-06 01:16:00 +00001439AttrBuilder &AttrBuilder::merge(const AttrBuilder &B) {
1440 // FIXME: What if both have alignments, but they don't match?!
1441 if (!Alignment)
1442 Alignment = B.Alignment;
1443
1444 if (!StackAlignment)
1445 StackAlignment = B.StackAlignment;
1446
Hal Finkelb0407ba2014-07-18 15:51:28 +00001447 if (!DerefBytes)
1448 DerefBytes = B.DerefBytes;
1449
Pete Cooperd2a44612015-05-06 23:19:43 +00001450 if (!DerefOrNullBytes)
1451 DerefOrNullBytes = B.DerefOrNullBytes;
1452
George Burgess IV278199f2016-04-12 01:05:35 +00001453 if (!AllocSizeArgs)
1454 AllocSizeArgs = B.AllocSizeArgs;
1455
Benjamin Kramer45e7d532013-02-16 19:13:18 +00001456 Attrs |= B.Attrs;
Bill Wendlinge2614922013-02-06 01:16:00 +00001457
Pete Cooperd2a44612015-05-06 23:19:43 +00001458 for (auto I : B.td_attrs())
1459 TargetDepAttrs[I.first] = I.second;
Bill Wendlinge2614922013-02-06 01:16:00 +00001460
1461 return *this;
1462}
1463
Pete Cooperd2a44612015-05-06 23:19:43 +00001464AttrBuilder &AttrBuilder::remove(const AttrBuilder &B) {
1465 // FIXME: What if both have alignments, but they don't match?!
1466 if (B.Alignment)
1467 Alignment = 0;
1468
1469 if (B.StackAlignment)
1470 StackAlignment = 0;
1471
1472 if (B.DerefBytes)
1473 DerefBytes = 0;
1474
1475 if (B.DerefOrNullBytes)
1476 DerefOrNullBytes = 0;
1477
George Burgess IV278199f2016-04-12 01:05:35 +00001478 if (B.AllocSizeArgs)
1479 AllocSizeArgs = 0;
1480
Pete Cooperd2a44612015-05-06 23:19:43 +00001481 Attrs &= ~B.Attrs;
1482
1483 for (auto I : B.td_attrs())
1484 TargetDepAttrs.erase(I.first);
1485
1486 return *this;
1487}
1488
1489bool AttrBuilder::overlaps(const AttrBuilder &B) const {
1490 // First check if any of the target independent attributes overlap.
1491 if ((Attrs & B.Attrs).any())
1492 return true;
1493
1494 // Then check if any target dependent ones do.
Sean Silva9011aca2017-02-22 06:34:04 +00001495 for (const auto &I : td_attrs())
Pete Cooperd2a44612015-05-06 23:19:43 +00001496 if (B.contains(I.first))
1497 return true;
1498
1499 return false;
1500}
1501
Bill Wendling4b001442013-02-06 01:33:42 +00001502bool AttrBuilder::contains(StringRef A) const {
1503 return TargetDepAttrs.find(A) != TargetDepAttrs.end();
1504}
1505
Bill Wendling50d27842012-10-15 20:35:56 +00001506bool AttrBuilder::hasAttributes() const {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001507 return !Attrs.none() || !TargetDepAttrs.empty();
Bill Wendlingc6daefa2012-10-08 23:27:46 +00001508}
Bill Wendling9ac69f92013-01-04 20:54:35 +00001509
Reid Kleckner6652a522017-04-28 18:37:16 +00001510bool AttrBuilder::hasAttributes(AttributeList AL, uint64_t Index) const {
1511 AttributeSet AS = AL.getAttributes(Index);
Bill Wendling9ca01da2013-02-02 00:42:06 +00001512
Reid Kleckner6652a522017-04-28 18:37:16 +00001513 for (Attribute Attr : AS) {
Hal Finkele15442c2014-07-18 06:51:55 +00001514 if (Attr.isEnumAttribute() || Attr.isIntAttribute()) {
Reid Kleckner6652a522017-04-28 18:37:16 +00001515 if (contains(Attr.getKindAsEnum()))
Bill Wendling7cde51d2013-02-12 07:56:49 +00001516 return true;
1517 } else {
1518 assert(Attr.isStringAttribute() && "Invalid attribute kind!");
Reid Kleckner6652a522017-04-28 18:37:16 +00001519 return contains(Attr.getKindAsString());
Bill Wendling7cde51d2013-02-12 07:56:49 +00001520 }
1521 }
Bill Wendling9ca01da2013-02-02 00:42:06 +00001522
1523 return false;
Bill Wendling70f39172012-10-09 00:01:21 +00001524}
Bill Wendling9ac69f92013-01-04 20:54:35 +00001525
Bill Wendling50d27842012-10-15 20:35:56 +00001526bool AttrBuilder::hasAlignmentAttr() const {
Bill Wendlingcd330342013-01-04 23:27:34 +00001527 return Alignment != 0;
Bill Wendlingc6daefa2012-10-08 23:27:46 +00001528}
1529
Bill Wendlingd509a662013-01-29 00:34:06 +00001530bool AttrBuilder::operator==(const AttrBuilder &B) {
Benjamin Kramer45e7d532013-02-16 19:13:18 +00001531 if (Attrs != B.Attrs)
1532 return false;
Bill Wendling4b001442013-02-06 01:33:42 +00001533
1534 for (td_const_iterator I = TargetDepAttrs.begin(),
1535 E = TargetDepAttrs.end(); I != E; ++I)
1536 if (B.TargetDepAttrs.find(I->first) == B.TargetDepAttrs.end())
1537 return false;
1538
Hal Finkelb0407ba2014-07-18 15:51:28 +00001539 return Alignment == B.Alignment && StackAlignment == B.StackAlignment &&
1540 DerefBytes == B.DerefBytes;
Bill Wendlingd509a662013-01-29 00:34:06 +00001541}
1542
Bill Wendling57625a42013-01-25 23:09:36 +00001543//===----------------------------------------------------------------------===//
1544// AttributeFuncs Function Defintions
1545//===----------------------------------------------------------------------===//
1546
Bill Wendlingc79cdff2013-02-01 01:04:27 +00001547/// \brief Which attributes cannot be applied to a type.
Craig Toppere3dcce92015-08-01 22:20:21 +00001548AttrBuilder AttributeFuncs::typeIncompatible(Type *Ty) {
Bill Wendling57625a42013-01-25 23:09:36 +00001549 AttrBuilder Incompatible;
1550
1551 if (!Ty->isIntegerTy())
1552 // Attribute that only apply to integers.
1553 Incompatible.addAttribute(Attribute::SExt)
1554 .addAttribute(Attribute::ZExt);
1555
1556 if (!Ty->isPointerTy())
1557 // Attribute that only apply to pointers.
1558 Incompatible.addAttribute(Attribute::ByVal)
1559 .addAttribute(Attribute::Nest)
1560 .addAttribute(Attribute::NoAlias)
1561 .addAttribute(Attribute::NoCapture)
Nick Lewyckyd52b1522014-05-20 01:23:40 +00001562 .addAttribute(Attribute::NonNull)
Hal Finkelb0407ba2014-07-18 15:51:28 +00001563 .addDereferenceableAttr(1) // the int here is ignored
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001564 .addDereferenceableOrNullAttr(1) // the int here is ignored
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001565 .addAttribute(Attribute::ReadNone)
1566 .addAttribute(Attribute::ReadOnly)
Reid Klecknera534a382013-12-19 02:14:12 +00001567 .addAttribute(Attribute::StructRet)
1568 .addAttribute(Attribute::InAlloca);
Bill Wendling57625a42013-01-25 23:09:36 +00001569
Pete Cooper2777d8872015-05-06 23:19:56 +00001570 return Incompatible;
Bill Wendling57625a42013-01-25 23:09:36 +00001571}
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001572
1573template<typename AttrClass>
1574static bool isEqual(const Function &Caller, const Function &Callee) {
1575 return Caller.getFnAttribute(AttrClass::getKind()) ==
1576 Callee.getFnAttribute(AttrClass::getKind());
1577}
1578
1579/// \brief Compute the logical AND of the attributes of the caller and the
1580/// callee.
1581///
1582/// This function sets the caller's attribute to false if the callee's attribute
1583/// is false.
1584template<typename AttrClass>
1585static void setAND(Function &Caller, const Function &Callee) {
1586 if (AttrClass::isSet(Caller, AttrClass::getKind()) &&
1587 !AttrClass::isSet(Callee, AttrClass::getKind()))
1588 AttrClass::set(Caller, AttrClass::getKind(), false);
1589}
1590
1591/// \brief Compute the logical OR of the attributes of the caller and the
1592/// callee.
1593///
1594/// This function sets the caller's attribute to true if the callee's attribute
1595/// is true.
1596template<typename AttrClass>
1597static void setOR(Function &Caller, const Function &Callee) {
1598 if (!AttrClass::isSet(Caller, AttrClass::getKind()) &&
1599 AttrClass::isSet(Callee, AttrClass::getKind()))
1600 AttrClass::set(Caller, AttrClass::getKind(), true);
1601}
1602
1603/// \brief If the inlined function had a higher stack protection level than the
1604/// calling function, then bump up the caller's stack protection level.
1605static void adjustCallerSSPLevel(Function &Caller, const Function &Callee) {
1606 // If upgrading the SSP attribute, clear out the old SSP Attributes first.
1607 // Having multiple SSP attributes doesn't actually hurt, but it adds useless
1608 // clutter to the IR.
1609 AttrBuilder B;
1610 B.addAttribute(Attribute::StackProtect)
1611 .addAttribute(Attribute::StackProtectStrong)
1612 .addAttribute(Attribute::StackProtectReq);
Reid Klecknerb5180542017-03-21 16:57:19 +00001613 AttributeList OldSSPAttr =
1614 AttributeList::get(Caller.getContext(), AttributeList::FunctionIndex, B);
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001615
Evgeniy Stepanovf17120a2016-04-11 22:27:48 +00001616 if (Callee.hasFnAttribute(Attribute::StackProtectReq)) {
Reid Klecknerb5180542017-03-21 16:57:19 +00001617 Caller.removeAttributes(AttributeList::FunctionIndex, OldSSPAttr);
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001618 Caller.addFnAttr(Attribute::StackProtectReq);
1619 } else if (Callee.hasFnAttribute(Attribute::StackProtectStrong) &&
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001620 !Caller.hasFnAttribute(Attribute::StackProtectReq)) {
Reid Klecknerb5180542017-03-21 16:57:19 +00001621 Caller.removeAttributes(AttributeList::FunctionIndex, OldSSPAttr);
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001622 Caller.addFnAttr(Attribute::StackProtectStrong);
1623 } else if (Callee.hasFnAttribute(Attribute::StackProtect) &&
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001624 !Caller.hasFnAttribute(Attribute::StackProtectReq) &&
1625 !Caller.hasFnAttribute(Attribute::StackProtectStrong))
1626 Caller.addFnAttr(Attribute::StackProtect);
1627}
1628
1629#define GET_ATTR_COMPAT_FUNC
1630#include "AttributesCompatFunc.inc"
1631
1632bool AttributeFuncs::areInlineCompatible(const Function &Caller,
1633 const Function &Callee) {
1634 return hasCompatibleFnAttrs(Caller, Callee);
1635}
1636
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001637void AttributeFuncs::mergeAttributesForInlining(Function &Caller,
1638 const Function &Callee) {
1639 mergeFnAttrs(Caller, Callee);
1640}