blob: 47ca5e4062349cdf5e873c560ec97be6f68e5087 [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";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000318 if (hasAttribute(Attribute::StackProtect))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000319 return "ssp";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000320 if (hasAttribute(Attribute::StackProtectReq))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000321 return "sspreq";
Bill Wendlingd154e2832013-01-23 06:41:41 +0000322 if (hasAttribute(Attribute::StackProtectStrong))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000323 return "sspstrong";
Peter Collingbourne82437bf2015-06-15 21:07:11 +0000324 if (hasAttribute(Attribute::SafeStack))
325 return "safestack";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000326 if (hasAttribute(Attribute::StructRet))
327 return "sret";
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000328 if (hasAttribute(Attribute::SanitizeThread))
329 return "sanitize_thread";
330 if (hasAttribute(Attribute::SanitizeMemory))
331 return "sanitize_memory";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000332 if (hasAttribute(Attribute::UWTable))
333 return "uwtable";
334 if (hasAttribute(Attribute::ZExt))
335 return "zeroext";
Diego Novilloc6399532013-05-24 12:26:52 +0000336 if (hasAttribute(Attribute::Cold))
337 return "cold";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000338
339 // FIXME: These should be output like this:
340 //
341 // align=4
342 // alignstack=8
343 //
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000344 if (hasAttribute(Attribute::Alignment)) {
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000345 std::string Result;
Bill Wendling829b4782013-02-11 08:43:33 +0000346 Result += "align";
347 Result += (InAttrGrp) ? "=" : " ";
Bill Wendling3f12ac22013-02-05 22:37:24 +0000348 Result += utostr(getValueAsInt());
349 return Result;
350 }
Bill Wendling829b4782013-02-11 08:43:33 +0000351
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000352 auto AttrWithBytesToString = [&](const char *Name) {
Bill Wendling3f12ac22013-02-05 22:37:24 +0000353 std::string Result;
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000354 Result += Name;
Bill Wendling829b4782013-02-11 08:43:33 +0000355 if (InAttrGrp) {
356 Result += "=";
357 Result += utostr(getValueAsInt());
358 } else {
359 Result += "(";
360 Result += utostr(getValueAsInt());
361 Result += ")";
362 }
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000363 return Result;
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000364 };
Bill Wendling9c2eba92013-01-31 20:59:05 +0000365
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000366 if (hasAttribute(Attribute::StackAlignment))
367 return AttrWithBytesToString("alignstack");
368
369 if (hasAttribute(Attribute::Dereferenceable))
370 return AttrWithBytesToString("dereferenceable");
371
372 if (hasAttribute(Attribute::DereferenceableOrNull))
373 return AttrWithBytesToString("dereferenceable_or_null");
Hal Finkelb0407ba2014-07-18 15:51:28 +0000374
George Burgess IV278199f2016-04-12 01:05:35 +0000375 if (hasAttribute(Attribute::AllocSize)) {
376 unsigned ElemSize;
377 Optional<unsigned> NumElems;
378 std::tie(ElemSize, NumElems) = getAllocSizeArgs();
379
380 std::string Result = "allocsize(";
381 Result += utostr(ElemSize);
382 if (NumElems.hasValue()) {
383 Result += ',';
384 Result += utostr(*NumElems);
385 }
386 Result += ')';
387 return Result;
388 }
389
Bill Wendling9c2eba92013-01-31 20:59:05 +0000390 // Convert target-dependent attributes to strings of the form:
391 //
392 // "kind"
393 // "kind" = "value"
Bill Wendling9c2eba92013-01-31 20:59:05 +0000394 //
Bill Wendling3f12ac22013-02-05 22:37:24 +0000395 if (isStringAttribute()) {
Bill Wendling9c2eba92013-01-31 20:59:05 +0000396 std::string Result;
Yaron Keren075759a2015-03-30 15:42:36 +0000397 Result += (Twine('"') + getKindAsString() + Twine('"')).str();
Bill Wendling9c2eba92013-01-31 20:59:05 +0000398
Honggyu Kim9eb6a102016-09-01 11:44:06 +0000399 std::string AttrVal = pImpl->getValueAsString();
400 if (AttrVal.empty()) return Result;
Bill Wendling7a33f772013-02-01 22:32:30 +0000401
Honggyu Kim9eb6a102016-09-01 11:44:06 +0000402 // Since some attribute strings contain special characters that cannot be
403 // printable, those have to be escaped to make the attribute value printable
404 // as is. e.g. "\01__gnu_mcount_nc"
405 {
406 raw_string_ostream OS(Result);
407 OS << "=\"";
408 PrintEscapedString(AttrVal, OS);
409 OS << "\"";
410 }
Bill Wendlingc79cdff2013-02-01 01:04:27 +0000411 return Result;
Bill Wendling9c2eba92013-01-31 20:59:05 +0000412 }
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000413
414 llvm_unreachable("Unknown attribute");
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000415}
416
Bill Wendlingd509a662013-01-29 00:34:06 +0000417bool Attribute::operator<(Attribute A) const {
418 if (!pImpl && !A.pImpl) return false;
419 if (!pImpl) return true;
420 if (!A.pImpl) return false;
421 return *pImpl < *A.pImpl;
422}
423
Bill Wendlingd509a662013-01-29 00:34:06 +0000424//===----------------------------------------------------------------------===//
425// AttributeImpl Definition
426//===----------------------------------------------------------------------===//
427
Eric Christopher0eaa5412014-07-02 22:05:40 +0000428// Pin the vtables to this file.
Eugene Zelenkodeaf6952017-02-17 00:00:09 +0000429AttributeImpl::~AttributeImpl() = default;
430
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +0000431void EnumAttributeImpl::anchor() {}
Eugene Zelenkodeaf6952017-02-17 00:00:09 +0000432
Hal Finkele15442c2014-07-18 06:51:55 +0000433void IntAttributeImpl::anchor() {}
Eugene Zelenkodeaf6952017-02-17 00:00:09 +0000434
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +0000435void StringAttributeImpl::anchor() {}
Alexey Samsonov49109a22013-11-18 09:31:53 +0000436
Bill Wendlingd509a662013-01-29 00:34:06 +0000437bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
Bill Wendling3f12ac22013-02-05 22:37:24 +0000438 if (isStringAttribute()) return false;
439 return getKindAsEnum() == A;
Bill Wendlingd509a662013-01-29 00:34:06 +0000440}
441
Bill Wendling3f12ac22013-02-05 22:37:24 +0000442bool AttributeImpl::hasAttribute(StringRef Kind) const {
443 if (!isStringAttribute()) return false;
444 return getKindAsString() == Kind;
Bill Wendlingd509a662013-01-29 00:34:06 +0000445}
446
Bill Wendling3f12ac22013-02-05 22:37:24 +0000447Attribute::AttrKind AttributeImpl::getKindAsEnum() const {
Hal Finkele15442c2014-07-18 06:51:55 +0000448 assert(isEnumAttribute() || isIntAttribute());
Benjamin Kramer741146b2013-07-11 12:13:16 +0000449 return static_cast<const EnumAttributeImpl *>(this)->getEnumKind();
Bill Wendlingd509a662013-01-29 00:34:06 +0000450}
451
Bill Wendling3f12ac22013-02-05 22:37:24 +0000452uint64_t AttributeImpl::getValueAsInt() const {
Hal Finkele15442c2014-07-18 06:51:55 +0000453 assert(isIntAttribute());
454 return static_cast<const IntAttributeImpl *>(this)->getValue();
Bill Wendlingd509a662013-01-29 00:34:06 +0000455}
456
Bill Wendling3f12ac22013-02-05 22:37:24 +0000457StringRef AttributeImpl::getKindAsString() const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000458 assert(isStringAttribute());
459 return static_cast<const StringAttributeImpl *>(this)->getStringKind();
Bill Wendlingd509a662013-01-29 00:34:06 +0000460}
461
Bill Wendling3f12ac22013-02-05 22:37:24 +0000462StringRef AttributeImpl::getValueAsString() const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000463 assert(isStringAttribute());
464 return static_cast<const StringAttributeImpl *>(this)->getStringValue();
Bill Wendlingd509a662013-01-29 00:34:06 +0000465}
466
467bool AttributeImpl::operator<(const AttributeImpl &AI) const {
Bill Wendlingc79cdff2013-02-01 01:04:27 +0000468 // This sorts the attributes with Attribute::AttrKinds coming first (sorted
469 // relative to their enum value) and then strings.
Bill Wendling26b95752013-02-15 05:25:26 +0000470 if (isEnumAttribute()) {
471 if (AI.isEnumAttribute()) return getKindAsEnum() < AI.getKindAsEnum();
Hal Finkele15442c2014-07-18 06:51:55 +0000472 if (AI.isIntAttribute()) return true;
Bill Wendling26b95752013-02-15 05:25:26 +0000473 if (AI.isStringAttribute()) return true;
474 }
Bill Wendlingc79cdff2013-02-01 01:04:27 +0000475
Hal Finkele15442c2014-07-18 06:51:55 +0000476 if (isIntAttribute()) {
Bill Wendling26b95752013-02-15 05:25:26 +0000477 if (AI.isEnumAttribute()) return false;
Reid Kleckner7de67612016-04-04 23:06:05 +0000478 if (AI.isIntAttribute()) {
479 if (getKindAsEnum() == AI.getKindAsEnum())
480 return getValueAsInt() < AI.getValueAsInt();
481 return getKindAsEnum() < AI.getKindAsEnum();
482 }
Bill Wendling26b95752013-02-15 05:25:26 +0000483 if (AI.isStringAttribute()) return true;
Bill Wendling3f12ac22013-02-05 22:37:24 +0000484 }
Bill Wendlingd509a662013-01-29 00:34:06 +0000485
Bill Wendling26b95752013-02-15 05:25:26 +0000486 if (AI.isEnumAttribute()) return false;
Hal Finkele15442c2014-07-18 06:51:55 +0000487 if (AI.isIntAttribute()) return false;
Bill Wendling26b95752013-02-15 05:25:26 +0000488 if (getKindAsString() == AI.getKindAsString())
489 return getValueAsString() < AI.getValueAsString();
490 return getKindAsString() < AI.getKindAsString();
Bill Wendlingd509a662013-01-29 00:34:06 +0000491}
492
Bill Wendlingd509a662013-01-29 00:34:06 +0000493//===----------------------------------------------------------------------===//
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000494// AttributeSet Definition
495//===----------------------------------------------------------------------===//
496
497AttributeSet AttributeSet::get(LLVMContext &C, const AttrBuilder &B) {
498 return AttributeSet(AttributeSetNode::get(C, B));
499}
500
501AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<Attribute> Attrs) {
502 return AttributeSet(AttributeSetNode::get(C, Attrs));
503}
504
505unsigned AttributeSet::getNumAttributes() const {
506 return SetNode ? SetNode->getNumAttributes() : 0;
507}
508
509bool AttributeSet::hasAttribute(Attribute::AttrKind Kind) const {
510 return SetNode ? SetNode->hasAttribute(Kind) : 0;
511}
512
513bool AttributeSet::hasAttribute(StringRef Kind) const {
514 return SetNode ? SetNode->hasAttribute(Kind) : 0;
515}
516
517Attribute AttributeSet::getAttribute(Attribute::AttrKind Kind) const {
518 return SetNode ? SetNode->getAttribute(Kind) : Attribute();
519}
520
521Attribute AttributeSet::getAttribute(StringRef Kind) const {
522 return SetNode ? SetNode->getAttribute(Kind) : Attribute();
523}
524
525unsigned AttributeSet::getAlignment() const {
526 return SetNode ? SetNode->getAlignment() : 0;
527}
528
529unsigned AttributeSet::getStackAlignment() const {
530 return SetNode ? SetNode->getStackAlignment() : 0;
531}
532
533uint64_t AttributeSet::getDereferenceableBytes() const {
534 return SetNode ? SetNode->getDereferenceableBytes() : 0;
535}
536
537uint64_t AttributeSet::getDereferenceableOrNullBytes() const {
538 return SetNode ? SetNode->getDereferenceableOrNullBytes() : 0;
539}
540
541std::pair<unsigned, Optional<unsigned>> AttributeSet::getAllocSizeArgs() const {
Konstantin Zhuravlyov6df95b72017-04-12 23:57:37 +0000542 return SetNode ? SetNode->getAllocSizeArgs()
543 : std::pair<unsigned, Optional<unsigned>>(0, 0);
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000544}
545
546std::string AttributeSet::getAsString(bool InAttrGrp) const {
547 return SetNode ? SetNode->getAsString(InAttrGrp) : "";
548}
549
550AttributeSet::iterator AttributeSet::begin() const {
551 return SetNode ? SetNode->begin() : nullptr;
552}
553
554AttributeSet::iterator AttributeSet::end() const {
555 return SetNode ? SetNode->end() : nullptr;
556}
557
558//===----------------------------------------------------------------------===//
Bill Wendlingd509a662013-01-29 00:34:06 +0000559// AttributeSetNode Definition
560//===----------------------------------------------------------------------===//
561
Reid Kleckner8ff77852017-04-10 23:46:08 +0000562AttributeSetNode::AttributeSetNode(ArrayRef<Attribute> Attrs)
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000563 : AvailableAttrs(0), NumAttrs(Attrs.size()) {
Reid Kleckner8ff77852017-04-10 23:46:08 +0000564 // There's memory after the node where we can store the entries in.
565 std::copy(Attrs.begin(), Attrs.end(), getTrailingObjects<Attribute>());
566
567 for (Attribute I : *this) {
568 if (!I.isStringAttribute()) {
569 AvailableAttrs |= ((uint64_t)1) << I.getKindAsEnum();
570 }
571 }
572}
573
Bill Wendlingd509a662013-01-29 00:34:06 +0000574AttributeSetNode *AttributeSetNode::get(LLVMContext &C,
575 ArrayRef<Attribute> Attrs) {
576 if (Attrs.empty())
Craig Topperc6207612014-04-09 06:08:46 +0000577 return nullptr;
Bill Wendlingd509a662013-01-29 00:34:06 +0000578
579 // Otherwise, build a key to look up the existing attributes.
580 LLVMContextImpl *pImpl = C.pImpl;
581 FoldingSetNodeID ID;
582
583 SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end());
Reid Kleckner7de67612016-04-04 23:06:05 +0000584 std::sort(SortedAttrs.begin(), SortedAttrs.end());
Bill Wendlingd509a662013-01-29 00:34:06 +0000585
George Burgess IV500d3032015-12-16 05:21:02 +0000586 for (Attribute Attr : SortedAttrs)
587 Attr.Profile(ID);
Bill Wendlingd509a662013-01-29 00:34:06 +0000588
589 void *InsertPoint;
590 AttributeSetNode *PA =
591 pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint);
592
593 // If we didn't find any existing attributes of the same shape then create a
594 // new one and insert it.
595 if (!PA) {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000596 // Coallocate entries after the AttributeSetNode itself.
James Y Knightaa365b22015-08-05 22:57:34 +0000597 void *Mem = ::operator new(totalSizeToAlloc<Attribute>(SortedAttrs.size()));
Benjamin Kramer741146b2013-07-11 12:13:16 +0000598 PA = new (Mem) AttributeSetNode(SortedAttrs);
Bill Wendlingd509a662013-01-29 00:34:06 +0000599 pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint);
600 }
601
Reid Klecknerb5180542017-03-21 16:57:19 +0000602 // Return the AttributeSetNode that we found or created.
Bill Wendlingd509a662013-01-29 00:34:06 +0000603 return PA;
604}
605
Reid Klecknereb9dd5b2017-04-10 23:31:05 +0000606AttributeSetNode *AttributeSetNode::get(LLVMContext &C, const AttrBuilder &B) {
607 // Add target-independent attributes.
608 SmallVector<Attribute, 8> Attrs;
609 for (Attribute::AttrKind Kind = Attribute::None;
610 Kind != Attribute::EndAttrKinds; Kind = Attribute::AttrKind(Kind + 1)) {
611 if (!B.contains(Kind))
612 continue;
613
614 Attribute Attr;
615 switch (Kind) {
616 case Attribute::Alignment:
617 Attr = Attribute::getWithAlignment(C, B.getAlignment());
618 break;
619 case Attribute::StackAlignment:
620 Attr = Attribute::getWithStackAlignment(C, B.getStackAlignment());
621 break;
622 case Attribute::Dereferenceable:
623 Attr = Attribute::getWithDereferenceableBytes(
624 C, B.getDereferenceableBytes());
625 break;
626 case Attribute::DereferenceableOrNull:
627 Attr = Attribute::getWithDereferenceableOrNullBytes(
628 C, B.getDereferenceableOrNullBytes());
629 break;
630 case Attribute::AllocSize: {
631 auto A = B.getAllocSizeArgs();
632 Attr = Attribute::getWithAllocSizeArgs(C, A.first, A.second);
633 break;
634 }
635 default:
636 Attr = Attribute::get(C, Kind);
637 }
638 Attrs.push_back(Attr);
639 }
640
641 // Add target-dependent (string) attributes.
642 for (const auto &TDA : B.td_attrs())
643 Attrs.emplace_back(Attribute::get(C, TDA.first, TDA.second));
644
645 return get(C, Attrs);
646}
647
Bill Wendlingbce7b972013-02-13 08:42:21 +0000648bool AttributeSetNode::hasAttribute(StringRef Kind) const {
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000649 for (Attribute I : *this)
650 if (I.hasAttribute(Kind))
Bill Wendlingbce7b972013-02-13 08:42:21 +0000651 return true;
652 return false;
653}
654
655Attribute AttributeSetNode::getAttribute(Attribute::AttrKind Kind) const {
Matthias Braun31eeb76f2016-01-29 22:25:13 +0000656 if (hasAttribute(Kind)) {
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000657 for (Attribute I : *this)
658 if (I.hasAttribute(Kind))
659 return I;
Matthias Braun31eeb76f2016-01-29 22:25:13 +0000660 }
Bill Wendlingbce7b972013-02-13 08:42:21 +0000661 return Attribute();
662}
663
664Attribute AttributeSetNode::getAttribute(StringRef Kind) const {
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000665 for (Attribute I : *this)
666 if (I.hasAttribute(Kind))
667 return I;
Bill Wendlingbce7b972013-02-13 08:42:21 +0000668 return Attribute();
669}
670
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000671unsigned AttributeSetNode::getAlignment() const {
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000672 for (Attribute I : *this)
673 if (I.hasAttribute(Attribute::Alignment))
674 return I.getAlignment();
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000675 return 0;
676}
677
678unsigned AttributeSetNode::getStackAlignment() const {
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000679 for (Attribute I : *this)
680 if (I.hasAttribute(Attribute::StackAlignment))
681 return I.getStackAlignment();
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000682 return 0;
683}
684
Hal Finkelb0407ba2014-07-18 15:51:28 +0000685uint64_t AttributeSetNode::getDereferenceableBytes() const {
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000686 for (Attribute I : *this)
687 if (I.hasAttribute(Attribute::Dereferenceable))
688 return I.getDereferenceableBytes();
Hal Finkelb0407ba2014-07-18 15:51:28 +0000689 return 0;
690}
691
Sanjoy Das06cf33f2015-05-06 17:41:54 +0000692uint64_t AttributeSetNode::getDereferenceableOrNullBytes() const {
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000693 for (Attribute I : *this)
694 if (I.hasAttribute(Attribute::DereferenceableOrNull))
695 return I.getDereferenceableOrNullBytes();
Sanjoy Das06cf33f2015-05-06 17:41:54 +0000696 return 0;
697}
698
George Burgess IV278199f2016-04-12 01:05:35 +0000699std::pair<unsigned, Optional<unsigned>>
700AttributeSetNode::getAllocSizeArgs() const {
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000701 for (Attribute I : *this)
702 if (I.hasAttribute(Attribute::AllocSize))
703 return I.getAllocSizeArgs();
George Burgess IV278199f2016-04-12 01:05:35 +0000704 return std::make_pair(0, 0);
705}
706
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +0000707std::string AttributeSetNode::getAsString(bool InAttrGrp) const {
Benjamin Kramer0baf8f42013-04-19 11:43:21 +0000708 std::string Str;
Benjamin Kramer741146b2013-07-11 12:13:16 +0000709 for (iterator I = begin(), E = end(); I != E; ++I) {
710 if (I != begin())
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +0000711 Str += ' ';
712 Str += I->getAsString(InAttrGrp);
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000713 }
714 return Str;
715}
716
Bill Wendlingd509a662013-01-29 00:34:06 +0000717//===----------------------------------------------------------------------===//
Reid Klecknerb5180542017-03-21 16:57:19 +0000718// AttributeListImpl Definition
Bill Wendlingd509a662013-01-29 00:34:06 +0000719//===----------------------------------------------------------------------===//
720
Reid Klecknera82be602017-04-11 00:16:00 +0000721AttributeListImpl::AttributeListImpl(
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000722 LLVMContext &C, ArrayRef<std::pair<unsigned, AttributeSet>> Slots)
Reid Klecknera82be602017-04-11 00:16:00 +0000723 : Context(C), NumSlots(Slots.size()), AvailableFunctionAttrs(0) {
724#ifndef NDEBUG
Reid Klecknerec0fc032017-04-12 22:22:01 +0000725 assert(!Slots.empty() && "pointless AttributeListImpl");
Reid Klecknera82be602017-04-11 00:16:00 +0000726 if (Slots.size() >= 2) {
727 auto &PrevPair = Slots.front();
728 for (auto &CurPair : Slots.drop_front()) {
729 assert(PrevPair.first <= CurPair.first && "Attribute set not ordered!");
730 }
731 }
732#endif
733
734 // There's memory after the node where we can store the entries in.
735 std::copy(Slots.begin(), Slots.end(), getTrailingObjects<IndexAttrPair>());
736
737 // Initialize AvailableFunctionAttrs summary bitset.
Reid Klecknerec0fc032017-04-12 22:22:01 +0000738 static_assert(Attribute::EndAttrKinds <=
739 sizeof(AvailableFunctionAttrs) * CHAR_BIT,
740 "Too many attributes");
741 static_assert(AttributeList::FunctionIndex == ~0u,
742 "FunctionIndex should be biggest possible index");
743 const auto &Last = Slots.back();
744 if (Last.first == AttributeList::FunctionIndex) {
745 AttributeSet Node = Last.second;
746 for (Attribute I : Node) {
747 if (!I.isStringAttribute())
748 AvailableFunctionAttrs |= ((uint64_t)1) << I.getKindAsEnum();
Reid Klecknera82be602017-04-11 00:16:00 +0000749 }
750 }
751}
752
753void AttributeListImpl::Profile(FoldingSetNodeID &ID) const {
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000754 Profile(ID, makeArrayRef(getSlotPair(0), getNumSlots()));
Reid Klecknera82be602017-04-11 00:16:00 +0000755}
756
757void AttributeListImpl::Profile(
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000758 FoldingSetNodeID &ID, ArrayRef<std::pair<unsigned, AttributeSet>> Nodes) {
Reid Klecknera82be602017-04-11 00:16:00 +0000759 for (const auto &Node : Nodes) {
760 ID.AddInteger(Node.first);
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000761 ID.AddPointer(Node.second.SetNode);
Reid Klecknera82be602017-04-11 00:16:00 +0000762 }
763}
764
Matthias Braun8c209aa2017-01-28 02:02:38 +0000765#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Reid Klecknerb5180542017-03-21 16:57:19 +0000766LLVM_DUMP_METHOD void AttributeListImpl::dump() const {
767 AttributeList(const_cast<AttributeListImpl *>(this)).dump();
Peter Collingbourneabca2ec2013-08-02 22:34:30 +0000768}
Matthias Braun8c209aa2017-01-28 02:02:38 +0000769#endif
Peter Collingbourneabca2ec2013-08-02 22:34:30 +0000770
Bill Wendlingd509a662013-01-29 00:34:06 +0000771//===----------------------------------------------------------------------===//
Reid Klecknerb5180542017-03-21 16:57:19 +0000772// AttributeList Construction and Mutation Methods
Bill Wendlingd509a662013-01-29 00:34:06 +0000773//===----------------------------------------------------------------------===//
774
Reid Klecknerb5180542017-03-21 16:57:19 +0000775AttributeList AttributeList::getImpl(
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000776 LLVMContext &C, ArrayRef<std::pair<unsigned, AttributeSet>> Attrs) {
Reid Klecknereb9dd5b2017-04-10 23:31:05 +0000777 assert(!Attrs.empty() && "creating pointless AttributeList");
778#ifndef NDEBUG
779 unsigned LastIndex = 0;
780 bool IsFirst = true;
Reid Kleckner7f720332017-04-13 00:58:09 +0000781 for (auto &&AttrPair : Attrs) {
Reid Klecknereb9dd5b2017-04-10 23:31:05 +0000782 assert((IsFirst || LastIndex < AttrPair.first) &&
783 "unsorted or duplicate AttributeList indices");
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000784 assert(AttrPair.second.hasAttributes() && "pointless AttributeList slot");
Reid Klecknereb9dd5b2017-04-10 23:31:05 +0000785 LastIndex = AttrPair.first;
786 IsFirst = false;
787 }
788#endif
789
Bill Wendlingd509a662013-01-29 00:34:06 +0000790 LLVMContextImpl *pImpl = C.pImpl;
791 FoldingSetNodeID ID;
Reid Klecknerb5180542017-03-21 16:57:19 +0000792 AttributeListImpl::Profile(ID, Attrs);
Bill Wendlingd509a662013-01-29 00:34:06 +0000793
794 void *InsertPoint;
Reid Klecknerb5180542017-03-21 16:57:19 +0000795 AttributeListImpl *PA =
796 pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
Bill Wendlingd509a662013-01-29 00:34:06 +0000797
798 // If we didn't find any existing attributes of the same shape then
799 // create a new one and insert it.
800 if (!PA) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000801 // Coallocate entries after the AttributeListImpl itself.
James Y Knightaa365b22015-08-05 22:57:34 +0000802 void *Mem = ::operator new(
Reid Klecknerb5180542017-03-21 16:57:19 +0000803 AttributeListImpl::totalSizeToAlloc<IndexAttrPair>(Attrs.size()));
804 PA = new (Mem) AttributeListImpl(C, Attrs);
Bill Wendlingd509a662013-01-29 00:34:06 +0000805 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
806 }
807
808 // Return the AttributesList that we found or created.
Reid Klecknerb5180542017-03-21 16:57:19 +0000809 return AttributeList(PA);
Bill Wendlingd509a662013-01-29 00:34:06 +0000810}
811
Reid Klecknerb5180542017-03-21 16:57:19 +0000812AttributeList
813AttributeList::get(LLVMContext &C,
814 ArrayRef<std::pair<unsigned, Attribute>> Attrs) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000815 // If there are no attributes then return a null AttributesList pointer.
816 if (Attrs.empty())
Reid Klecknerb5180542017-03-21 16:57:19 +0000817 return AttributeList();
Bill Wendlingd509a662013-01-29 00:34:06 +0000818
Craig Toppere30b8ca2016-01-03 19:43:40 +0000819 assert(std::is_sorted(Attrs.begin(), Attrs.end(),
820 [](const std::pair<unsigned, Attribute> &LHS,
821 const std::pair<unsigned, Attribute> &RHS) {
822 return LHS.first < RHS.first;
823 }) && "Misordered Attributes list!");
David Majnemer0a16c222016-08-11 21:15:00 +0000824 assert(none_of(Attrs,
825 [](const std::pair<unsigned, Attribute> &Pair) {
826 return Pair.second.hasAttribute(Attribute::None);
827 }) &&
828 "Pointless attribute!");
Bill Wendlingd509a662013-01-29 00:34:06 +0000829
830 // Create a vector if (unsigned, AttributeSetNode*) pairs from the attributes
831 // list.
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000832 SmallVector<std::pair<unsigned, AttributeSet>, 8> AttrPairVec;
Eugene Zelenkodeaf6952017-02-17 00:00:09 +0000833 for (ArrayRef<std::pair<unsigned, Attribute>>::iterator I = Attrs.begin(),
Bill Wendlingd509a662013-01-29 00:34:06 +0000834 E = Attrs.end(); I != E; ) {
835 unsigned Index = I->first;
836 SmallVector<Attribute, 4> AttrVec;
NAKAMURA Takumif05d2f22013-01-29 15:18:16 +0000837 while (I != E && I->first == Index) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000838 AttrVec.push_back(I->second);
839 ++I;
840 }
841
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000842 AttrPairVec.emplace_back(Index, AttributeSet::get(C, AttrVec));
Bill Wendlingd509a662013-01-29 00:34:06 +0000843 }
844
845 return getImpl(C, AttrPairVec);
846}
847
Reid Klecknerb5180542017-03-21 16:57:19 +0000848AttributeList
849AttributeList::get(LLVMContext &C,
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000850 ArrayRef<std::pair<unsigned, AttributeSet>> Attrs) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000851 // If there are no attributes then return a null AttributesList pointer.
852 if (Attrs.empty())
Reid Klecknerb5180542017-03-21 16:57:19 +0000853 return AttributeList();
Bill Wendlingd509a662013-01-29 00:34:06 +0000854
855 return getImpl(C, Attrs);
856}
857
Reid Kleckner7f720332017-04-13 00:58:09 +0000858AttributeList AttributeList::get(LLVMContext &C, AttributeSet FnAttrs,
859 AttributeSet RetAttrs,
860 ArrayRef<AttributeSet> ArgAttrs) {
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000861 SmallVector<std::pair<unsigned, AttributeSet>, 8> AttrPairs;
Reid Kleckner7f720332017-04-13 00:58:09 +0000862 if (RetAttrs.hasAttributes())
863 AttrPairs.emplace_back(ReturnIndex, RetAttrs);
864 size_t Index = 1;
865 for (AttributeSet AS : ArgAttrs) {
866 if (AS.hasAttributes())
Reid Klecknereb9dd5b2017-04-10 23:31:05 +0000867 AttrPairs.emplace_back(Index, AS);
Reid Klecknereb9dd5b2017-04-10 23:31:05 +0000868 ++Index;
869 }
Reid Kleckner7f720332017-04-13 00:58:09 +0000870 if (FnAttrs.hasAttributes())
871 AttrPairs.emplace_back(FunctionIndex, FnAttrs);
Reid Klecknereb9dd5b2017-04-10 23:31:05 +0000872 if (AttrPairs.empty())
873 return AttributeList();
874 return getImpl(C, AttrPairs);
875}
876
Reid Klecknerb5180542017-03-21 16:57:19 +0000877AttributeList AttributeList::get(LLVMContext &C, unsigned Index,
878 const AttrBuilder &B) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000879 if (!B.hasAttributes())
Reid Klecknerb5180542017-03-21 16:57:19 +0000880 return AttributeList();
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000881 AttributeSet AS = AttributeSet::get(C, B);
882 std::pair<unsigned, AttributeSet> Arr[1] = {{Index, AS}};
Reid Klecknereb9dd5b2017-04-10 23:31:05 +0000883 return getImpl(C, Arr);
Bill Wendlingd509a662013-01-29 00:34:06 +0000884}
885
Reid Klecknerb5180542017-03-21 16:57:19 +0000886AttributeList AttributeList::get(LLVMContext &C, unsigned Index,
887 ArrayRef<Attribute::AttrKind> Kinds) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000888 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
Amaury Sechet392638d2016-06-14 20:27:35 +0000889 for (Attribute::AttrKind K : Kinds)
David Majnemer0a16c222016-08-11 21:15:00 +0000890 Attrs.emplace_back(Index, Attribute::get(C, K));
Bill Wendlingd509a662013-01-29 00:34:06 +0000891 return get(C, Attrs);
892}
893
Reid Klecknerb5180542017-03-21 16:57:19 +0000894AttributeList AttributeList::get(LLVMContext &C, unsigned Index,
895 ArrayRef<StringRef> Kinds) {
Amaury Sechet6100adf2016-06-15 17:50:39 +0000896 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
897 for (StringRef K : Kinds)
David Majnemer0a16c222016-08-11 21:15:00 +0000898 Attrs.emplace_back(Index, Attribute::get(C, K));
Amaury Sechet6100adf2016-06-15 17:50:39 +0000899 return get(C, Attrs);
900}
901
Reid Klecknerb5180542017-03-21 16:57:19 +0000902AttributeList AttributeList::get(LLVMContext &C,
903 ArrayRef<AttributeList> Attrs) {
904 if (Attrs.empty())
905 return AttributeList();
Peter Collingbournebd6c7452013-08-02 22:29:40 +0000906 if (Attrs.size() == 1) return Attrs[0];
Bill Wendlingd509a662013-01-29 00:34:06 +0000907
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000908 SmallVector<std::pair<unsigned, AttributeSet>, 8> AttrNodeVec;
Reid Klecknerb5180542017-03-21 16:57:19 +0000909 AttributeListImpl *A0 = Attrs[0].pImpl;
Peter Collingbournebd6c7452013-08-02 22:29:40 +0000910 if (A0)
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000911 AttrNodeVec.append(A0->getSlotPair(0), A0->getSlotPair(A0->getNumSlots()));
Peter Collingbournebd6c7452013-08-02 22:29:40 +0000912 // Copy all attributes from Attrs into AttrNodeVec while keeping AttrNodeVec
913 // ordered by index. Because we know that each list in Attrs is ordered by
914 // index we only need to merge each successive list in rather than doing a
915 // full sort.
916 for (unsigned I = 1, E = Attrs.size(); I != E; ++I) {
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000917 AttributeListImpl *ALI = Attrs[I].pImpl;
918 if (!ALI) continue;
919 SmallVector<std::pair<unsigned, AttributeSet>, 8>::iterator
Peter Collingbournebd6c7452013-08-02 22:29:40 +0000920 ANVI = AttrNodeVec.begin(), ANVE;
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000921 for (const IndexAttrPair *AI = ALI->getSlotPair(0),
922 *AE = ALI->getSlotPair(ALI->getNumSlots());
Peter Collingbournebd6c7452013-08-02 22:29:40 +0000923 AI != AE; ++AI) {
924 ANVE = AttrNodeVec.end();
925 while (ANVI != ANVE && ANVI->first <= AI->first)
926 ++ANVI;
927 ANVI = AttrNodeVec.insert(ANVI, *AI) + 1;
928 }
Bill Wendlingd509a662013-01-29 00:34:06 +0000929 }
930
931 return getImpl(C, AttrNodeVec);
932}
933
Reid Klecknerb5180542017-03-21 16:57:19 +0000934AttributeList AttributeList::addAttribute(LLVMContext &C, unsigned Index,
935 Attribute::AttrKind Kind) const {
Amaury Sechet392638d2016-06-14 20:27:35 +0000936 if (hasAttribute(Index, Kind)) return *this;
Reid Klecknerb5180542017-03-21 16:57:19 +0000937 return addAttributes(C, Index, AttributeList::get(C, Index, Kind));
Reed Kotler795c7b42013-03-13 20:20:08 +0000938}
939
Reid Klecknerb5180542017-03-21 16:57:19 +0000940AttributeList AttributeList::addAttribute(LLVMContext &C, unsigned Index,
941 StringRef Kind,
942 StringRef Value) const {
Eugene Zelenkodeaf6952017-02-17 00:00:09 +0000943 AttrBuilder B;
Bill Wendling3b2f6102013-07-25 18:34:24 +0000944 B.addAttribute(Kind, Value);
Reid Klecknerb5180542017-03-21 16:57:19 +0000945 return addAttributes(C, Index, AttributeList::get(C, Index, B));
Bill Wendling3b2f6102013-07-25 18:34:24 +0000946}
947
Reid Klecknerb5180542017-03-21 16:57:19 +0000948AttributeList AttributeList::addAttribute(LLVMContext &C,
949 ArrayRef<unsigned> Indices,
950 Attribute A) const {
Reid Klecknereb9dd5b2017-04-10 23:31:05 +0000951 assert(std::is_sorted(Indices.begin(), Indices.end()));
Reid Kleckner324c99d2017-04-10 20:18:10 +0000952
Reid Klecknereb9dd5b2017-04-10 23:31:05 +0000953 unsigned I = 0, E = pImpl ? pImpl->getNumSlots() : 0;
954 SmallVector<IndexAttrPair, 4> AttrVec;
955 for (unsigned Index : Indices) {
956 // Add all attribute slots before the current index.
957 for (; I < E && getSlotIndex(I) < Index; ++I)
958 AttrVec.emplace_back(getSlotIndex(I), pImpl->getSlotNode(I));
959
960 // Add the attribute at this index. If we already have attributes at this
961 // index, merge them into a new set.
962 AttrBuilder B;
963 if (I < E && getSlotIndex(I) == Index) {
964 B.merge(AttrBuilder(pImpl->getSlotNode(I)));
Akira Hatanaka237916b2015-12-02 06:58:49 +0000965 ++I;
Akira Hatanaka237916b2015-12-02 06:58:49 +0000966 }
Reid Klecknereb9dd5b2017-04-10 23:31:05 +0000967 B.addAttribute(A);
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000968 AttrVec.emplace_back(Index, AttributeSet::get(C, B));
Akira Hatanaka237916b2015-12-02 06:58:49 +0000969 }
970
Reid Klecknereb9dd5b2017-04-10 23:31:05 +0000971 // Add remaining attributes.
972 for (; I < E; ++I)
973 AttrVec.emplace_back(getSlotIndex(I), pImpl->getSlotNode(I));
Akira Hatanaka237916b2015-12-02 06:58:49 +0000974
Reid Klecknereb9dd5b2017-04-10 23:31:05 +0000975 return get(C, AttrVec);
Akira Hatanaka237916b2015-12-02 06:58:49 +0000976}
977
Reid Klecknerb5180542017-03-21 16:57:19 +0000978AttributeList AttributeList::addAttributes(LLVMContext &C, unsigned Index,
979 AttributeList Attrs) const {
Bill Wendlingd509a662013-01-29 00:34:06 +0000980 if (!pImpl) return Attrs;
981 if (!Attrs.pImpl) return *this;
982
Reid Klecknereb9dd5b2017-04-10 23:31:05 +0000983 return addAttributes(C, Index, Attrs.getAttributes(Index));
984}
985
986AttributeList AttributeList::addAttributes(LLVMContext &C, unsigned Index,
Reid Kleckner61906252017-04-19 01:51:13 +0000987 const AttrBuilder &B) const {
988 if (!B.hasAttributes())
Reid Klecknereb9dd5b2017-04-10 23:31:05 +0000989 return *this;
990
Reid Klecknerfe64c012017-04-18 22:10:18 +0000991 if (!pImpl)
Reid Kleckner61906252017-04-19 01:51:13 +0000992 return AttributeList::get(C, {{Index, AttributeSet::get(C, B)}});
Reid Klecknerfe64c012017-04-18 22:10:18 +0000993
Bill Wendlingd509a662013-01-29 00:34:06 +0000994#ifndef NDEBUG
995 // FIXME it is not obvious how this should work for alignment. For now, say
996 // we can't change a known alignment.
Bill Wendling211316c2013-04-18 20:17:28 +0000997 unsigned OldAlign = getParamAlignment(Index);
Reid Kleckner61906252017-04-19 01:51:13 +0000998 unsigned NewAlign = B.getAlignment();
Bill Wendlingd509a662013-01-29 00:34:06 +0000999 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
1000 "Attempt to change alignment!");
1001#endif
1002
Reid Kleckner61906252017-04-19 01:51:13 +00001003 SmallVector<IndexAttrPair, 4> AttrVec;
Amaury Sechet24c84fd2016-06-14 22:04:16 +00001004 uint64_t NumAttrs = pImpl->getNumSlots();
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00001005 unsigned I;
1006
1007 // Add all the attribute slots before the one we need to merge.
1008 for (I = 0; I < NumAttrs; ++I) {
1009 if (getSlotIndex(I) >= Index)
Bill Wendlingd509a662013-01-29 00:34:06 +00001010 break;
Reid Kleckner61906252017-04-19 01:51:13 +00001011 AttrVec.emplace_back(getSlotIndex(I), pImpl->getSlotNode(I));
Bill Wendlingd509a662013-01-29 00:34:06 +00001012 }
1013
Reid Kleckner61906252017-04-19 01:51:13 +00001014 AttrBuilder NewAttrs;
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00001015 if (I < NumAttrs && getSlotIndex(I) == Index) {
Reid Kleckner61906252017-04-19 01:51:13 +00001016 // We need to merge the attribute sets.
1017 NewAttrs.merge(pImpl->getSlotNode(I));
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00001018 ++I;
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00001019 }
Reid Kleckner61906252017-04-19 01:51:13 +00001020 NewAttrs.merge(B);
1021
1022 // Add the new or merged attribute set at this index.
1023 AttrVec.emplace_back(Index, AttributeSet::get(C, NewAttrs));
Bill Wendlingd509a662013-01-29 00:34:06 +00001024
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00001025 // Add the remaining entries.
1026 for (; I < NumAttrs; ++I)
Reid Kleckner61906252017-04-19 01:51:13 +00001027 AttrVec.emplace_back(getSlotIndex(I), pImpl->getSlotNode(I));
Bill Wendlingd509a662013-01-29 00:34:06 +00001028
Reid Kleckner61906252017-04-19 01:51:13 +00001029 return get(C, AttrVec);
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00001030}
1031
Reid Klecknerb5180542017-03-21 16:57:19 +00001032AttributeList AttributeList::removeAttribute(LLVMContext &C, unsigned Index,
1033 Attribute::AttrKind Kind) const {
Amaury Sechet392638d2016-06-14 20:27:35 +00001034 if (!hasAttribute(Index, Kind)) return *this;
Reid Klecknerb5180542017-03-21 16:57:19 +00001035 return removeAttributes(C, Index, AttributeList::get(C, Index, Kind));
Bill Wendlingd509a662013-01-29 00:34:06 +00001036}
1037
Reid Klecknerb5180542017-03-21 16:57:19 +00001038AttributeList AttributeList::removeAttribute(LLVMContext &C, unsigned Index,
1039 StringRef Kind) const {
Amaury Sechet6100adf2016-06-15 17:50:39 +00001040 if (!hasAttribute(Index, Kind)) return *this;
Reid Klecknerb5180542017-03-21 16:57:19 +00001041 return removeAttributes(C, Index, AttributeList::get(C, Index, Kind));
Amaury Sechet6100adf2016-06-15 17:50:39 +00001042}
1043
Reid Klecknerb5180542017-03-21 16:57:19 +00001044AttributeList AttributeList::removeAttributes(LLVMContext &C, unsigned Index,
1045 AttributeList Attrs) const {
1046 if (!pImpl)
1047 return AttributeList();
Bill Wendlingd509a662013-01-29 00:34:06 +00001048 if (!Attrs.pImpl) return *this;
1049
Pete Cooper67cf9a72015-11-19 05:56:52 +00001050 // FIXME it is not obvious how this should work for alignment.
1051 // For now, say we can't pass in alignment, which no current use does.
1052 assert(!Attrs.hasAttribute(Index, Attribute::Alignment) &&
1053 "Attempt to change alignment!");
1054
Bill Wendlingd509a662013-01-29 00:34:06 +00001055 // Add the attribute slots before the one we're trying to add.
Reid Klecknerb5180542017-03-21 16:57:19 +00001056 SmallVector<AttributeList, 4> AttrSet;
Amaury Sechet24c84fd2016-06-14 22:04:16 +00001057 uint64_t NumAttrs = pImpl->getNumSlots();
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001058 AttributeList AL;
Bill Wendlingd509a662013-01-29 00:34:06 +00001059 uint64_t LastIndex = 0;
1060 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
Bill Wendling211316c2013-04-18 20:17:28 +00001061 if (getSlotIndex(I) >= Index) {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001062 if (getSlotIndex(I) == Index) AL = getSlotAttributes(LastIndex++);
Bill Wendlingd509a662013-01-29 00:34:06 +00001063 break;
1064 }
1065 LastIndex = I + 1;
1066 AttrSet.push_back(getSlotAttributes(I));
1067 }
1068
Bill Wendlingd2196752013-01-30 23:07:40 +00001069 // Now remove the attribute from the correct slot. There may already be an
Reid Klecknerb5180542017-03-21 16:57:19 +00001070 // AttributeList there.
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001071 AttrBuilder B(AL, Index);
Bill Wendlingd509a662013-01-29 00:34:06 +00001072
Amaury Sechet24c84fd2016-06-14 22:04:16 +00001073 for (unsigned I = 0, E = Attrs.pImpl->getNumSlots(); I != E; ++I)
Bill Wendling211316c2013-04-18 20:17:28 +00001074 if (Attrs.getSlotIndex(I) == Index) {
1075 B.removeAttributes(Attrs.pImpl->getSlotAttributes(I), Index);
Bill Wendlingd509a662013-01-29 00:34:06 +00001076 break;
1077 }
1078
Reid Klecknerb5180542017-03-21 16:57:19 +00001079 AttrSet.push_back(AttributeList::get(C, Index, B));
Bill Wendlingd509a662013-01-29 00:34:06 +00001080
1081 // Add the remaining attribute slots.
1082 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
1083 AttrSet.push_back(getSlotAttributes(I));
1084
1085 return get(C, AttrSet);
1086}
1087
Reid Klecknerb5180542017-03-21 16:57:19 +00001088AttributeList AttributeList::removeAttributes(LLVMContext &C, unsigned Index,
1089 const AttrBuilder &Attrs) const {
1090 if (!pImpl)
1091 return AttributeList();
Pete Cooperd2a44612015-05-06 23:19:43 +00001092
1093 // FIXME it is not obvious how this should work for alignment.
1094 // For now, say we can't pass in alignment, which no current use does.
1095 assert(!Attrs.hasAlignmentAttr() && "Attempt to change alignment!");
1096
1097 // Add the attribute slots before the one we're trying to add.
Reid Klecknerb5180542017-03-21 16:57:19 +00001098 SmallVector<AttributeList, 4> AttrSet;
Amaury Sechet24c84fd2016-06-14 22:04:16 +00001099 uint64_t NumAttrs = pImpl->getNumSlots();
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001100 AttributeList AL;
Pete Cooperd2a44612015-05-06 23:19:43 +00001101 uint64_t LastIndex = 0;
1102 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
1103 if (getSlotIndex(I) >= Index) {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001104 if (getSlotIndex(I) == Index) AL = getSlotAttributes(LastIndex++);
Pete Cooperd2a44612015-05-06 23:19:43 +00001105 break;
1106 }
1107 LastIndex = I + 1;
1108 AttrSet.push_back(getSlotAttributes(I));
1109 }
1110
1111 // Now remove the attribute from the correct slot. There may already be an
Reid Klecknerb5180542017-03-21 16:57:19 +00001112 // AttributeList there.
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001113 AttrBuilder B(AL, Index);
Pete Cooperd2a44612015-05-06 23:19:43 +00001114 B.remove(Attrs);
1115
Reid Klecknerb5180542017-03-21 16:57:19 +00001116 AttrSet.push_back(AttributeList::get(C, Index, B));
Pete Cooperd2a44612015-05-06 23:19:43 +00001117
1118 // Add the remaining attribute slots.
1119 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
1120 AttrSet.push_back(getSlotAttributes(I));
1121
1122 return get(C, AttrSet);
1123}
1124
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00001125AttributeList AttributeList::removeAttributes(LLVMContext &C,
1126 unsigned WithoutIndex) const {
1127 if (!pImpl)
1128 return AttributeList();
1129
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001130 SmallVector<std::pair<unsigned, AttributeSet>, 4> AttrSet;
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00001131 for (unsigned I = 0, E = pImpl->getNumSlots(); I != E; ++I) {
1132 unsigned Index = getSlotIndex(I);
1133 if (Index != WithoutIndex)
1134 AttrSet.push_back({Index, pImpl->getSlotNode(I)});
1135 }
1136 return get(C, AttrSet);
1137}
1138
Reid Klecknerb5180542017-03-21 16:57:19 +00001139AttributeList AttributeList::addDereferenceableAttr(LLVMContext &C,
1140 unsigned Index,
1141 uint64_t Bytes) const {
Eugene Zelenkodeaf6952017-02-17 00:00:09 +00001142 AttrBuilder B;
Ramkumar Ramachandra8fcb4982015-02-14 19:37:54 +00001143 B.addDereferenceableAttr(Bytes);
Reid Klecknerb5180542017-03-21 16:57:19 +00001144 return addAttributes(C, Index, AttributeList::get(C, Index, B));
Ramkumar Ramachandra8fcb4982015-02-14 19:37:54 +00001145}
1146
Reid Klecknerb5180542017-03-21 16:57:19 +00001147AttributeList
1148AttributeList::addDereferenceableOrNullAttr(LLVMContext &C, unsigned Index,
1149 uint64_t Bytes) const {
Eugene Zelenkodeaf6952017-02-17 00:00:09 +00001150 AttrBuilder B;
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001151 B.addDereferenceableOrNullAttr(Bytes);
Reid Klecknerb5180542017-03-21 16:57:19 +00001152 return addAttributes(C, Index, AttributeList::get(C, Index, B));
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001153}
1154
Reid Klecknerb5180542017-03-21 16:57:19 +00001155AttributeList
1156AttributeList::addAllocSizeAttr(LLVMContext &C, unsigned Index,
1157 unsigned ElemSizeArg,
1158 const Optional<unsigned> &NumElemsArg) {
Eugene Zelenkodeaf6952017-02-17 00:00:09 +00001159 AttrBuilder B;
George Burgess IV278199f2016-04-12 01:05:35 +00001160 B.addAllocSizeAttr(ElemSizeArg, NumElemsArg);
Reid Klecknerb5180542017-03-21 16:57:19 +00001161 return addAttributes(C, Index, AttributeList::get(C, Index, B));
George Burgess IV278199f2016-04-12 01:05:35 +00001162}
1163
Bill Wendlingd509a662013-01-29 00:34:06 +00001164//===----------------------------------------------------------------------===//
Reid Klecknerb5180542017-03-21 16:57:19 +00001165// AttributeList Accessor Methods
Bill Wendlingd509a662013-01-29 00:34:06 +00001166//===----------------------------------------------------------------------===//
1167
Reid Klecknerb5180542017-03-21 16:57:19 +00001168LLVMContext &AttributeList::getContext() const { return pImpl->getContext(); }
1169
Reid Klecknerf021fab2017-04-13 23:12:13 +00001170AttributeSet AttributeList::getParamAttributes(unsigned ArgNo) const {
1171 return getAttributes(ArgNo + 1);
Bill Wendling5d020a32013-02-10 05:00:40 +00001172}
1173
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001174AttributeSet AttributeList::getRetAttributes() const {
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00001175 return getAttributes(ReturnIndex);
Bill Wendlingd509a662013-01-29 00:34:06 +00001176}
1177
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001178AttributeSet AttributeList::getFnAttributes() const {
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00001179 return getAttributes(FunctionIndex);
Bill Wendlingd509a662013-01-29 00:34:06 +00001180}
1181
Reid Klecknerb5180542017-03-21 16:57:19 +00001182bool AttributeList::hasAttribute(unsigned Index,
1183 Attribute::AttrKind Kind) const {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001184 return getAttributes(Index).hasAttribute(Kind);
Bill Wendlingd509a662013-01-29 00:34:06 +00001185}
1186
Reid Klecknerb5180542017-03-21 16:57:19 +00001187bool AttributeList::hasAttribute(unsigned Index, StringRef Kind) const {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001188 return getAttributes(Index).hasAttribute(Kind);
Bill Wendlingbce7b972013-02-13 08:42:21 +00001189}
1190
Reid Klecknerb5180542017-03-21 16:57:19 +00001191bool AttributeList::hasAttributes(unsigned Index) const {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001192 return getAttributes(Index).hasAttributes();
Bill Wendlingd509a662013-01-29 00:34:06 +00001193}
1194
Reid Klecknerb5180542017-03-21 16:57:19 +00001195bool AttributeList::hasFnAttribute(Attribute::AttrKind Kind) const {
Matthias Braun33282812016-01-29 22:25:19 +00001196 return pImpl && pImpl->hasFnAttribute(Kind);
1197}
1198
Reid Klecknerb5180542017-03-21 16:57:19 +00001199bool AttributeList::hasFnAttribute(StringRef Kind) const {
1200 return hasAttribute(AttributeList::FunctionIndex, Kind);
Amaury Sechet5f04d812016-09-09 04:50:38 +00001201}
1202
Reid Klecknerf021fab2017-04-13 23:12:13 +00001203bool AttributeList::hasParamAttribute(unsigned ArgNo,
1204 Attribute::AttrKind Kind) const {
1205 return hasAttribute(ArgNo + 1, Kind);
1206}
1207
Reid Klecknerb5180542017-03-21 16:57:19 +00001208bool AttributeList::hasAttrSomewhere(Attribute::AttrKind Attr,
1209 unsigned *Index) const {
Craig Topperc6207612014-04-09 06:08:46 +00001210 if (!pImpl) return false;
Bill Wendlingd509a662013-01-29 00:34:06 +00001211
Amaury Sechet24c84fd2016-06-14 22:04:16 +00001212 for (unsigned I = 0, E = pImpl->getNumSlots(); I != E; ++I)
Reid Klecknerb5180542017-03-21 16:57:19 +00001213 for (AttributeListImpl::iterator II = pImpl->begin(I), IE = pImpl->end(I);
1214 II != IE; ++II)
Hal Finkele87ad542016-07-10 23:01:32 +00001215 if (II->hasAttribute(Attr)) {
1216 if (Index) *Index = pImpl->getSlotIndex(I);
Bill Wendlingd509a662013-01-29 00:34:06 +00001217 return true;
Hal Finkele87ad542016-07-10 23:01:32 +00001218 }
Bill Wendlingd509a662013-01-29 00:34:06 +00001219
1220 return false;
1221}
1222
Reid Klecknerb5180542017-03-21 16:57:19 +00001223Attribute AttributeList::getAttribute(unsigned Index,
1224 Attribute::AttrKind Kind) const {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001225 return getAttributes(Index).getAttribute(Kind);
Bill Wendlingbce7b972013-02-13 08:42:21 +00001226}
1227
Reid Klecknerb5180542017-03-21 16:57:19 +00001228Attribute AttributeList::getAttribute(unsigned Index, StringRef Kind) const {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001229 return getAttributes(Index).getAttribute(Kind);
Bill Wendlingbce7b972013-02-13 08:42:21 +00001230}
1231
Reid Klecknerb5180542017-03-21 16:57:19 +00001232unsigned AttributeList::getParamAlignment(unsigned Index) const {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001233 return getAttributes(Index).getAlignment();
Bill Wendlingd509a662013-01-29 00:34:06 +00001234}
1235
Reid Klecknerb5180542017-03-21 16:57:19 +00001236unsigned AttributeList::getStackAlignment(unsigned Index) const {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001237 return getAttributes(Index).getStackAlignment();
Bill Wendlingd509a662013-01-29 00:34:06 +00001238}
1239
Reid Klecknerb5180542017-03-21 16:57:19 +00001240uint64_t AttributeList::getDereferenceableBytes(unsigned Index) const {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001241 return getAttributes(Index).getDereferenceableBytes();
Hal Finkelb0407ba2014-07-18 15:51:28 +00001242}
1243
Reid Klecknerb5180542017-03-21 16:57:19 +00001244uint64_t AttributeList::getDereferenceableOrNullBytes(unsigned Index) const {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001245 return getAttributes(Index).getDereferenceableOrNullBytes();
Sanjoy Das06cf33f2015-05-06 17:41:54 +00001246}
1247
George Burgess IV278199f2016-04-12 01:05:35 +00001248std::pair<unsigned, Optional<unsigned>>
Reid Klecknerb5180542017-03-21 16:57:19 +00001249AttributeList::getAllocSizeArgs(unsigned Index) const {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001250 return getAttributes(Index).getAllocSizeArgs();
George Burgess IV278199f2016-04-12 01:05:35 +00001251}
1252
Reid Klecknerb5180542017-03-21 16:57:19 +00001253std::string AttributeList::getAsString(unsigned Index, bool InAttrGrp) const {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001254 return getAttributes(Index).getAsString(InAttrGrp);
Bill Wendlingd509a662013-01-29 00:34:06 +00001255}
1256
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001257AttributeSet AttributeList::getAttributes(unsigned Index) const {
1258 if (!pImpl) return AttributeSet();
Bill Wendlingd509a662013-01-29 00:34:06 +00001259
Bill Wendlingf2955aa2013-01-29 03:20:31 +00001260 // Loop through to find the attribute node we want.
Amaury Sechet24c84fd2016-06-14 22:04:16 +00001261 for (unsigned I = 0, E = pImpl->getNumSlots(); I != E; ++I)
Bill Wendling211316c2013-04-18 20:17:28 +00001262 if (pImpl->getSlotIndex(I) == Index)
Bill Wendlingf2955aa2013-01-29 03:20:31 +00001263 return pImpl->getSlotNode(I);
Bill Wendlingd509a662013-01-29 00:34:06 +00001264
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001265 return AttributeSet();
Bill Wendlingd509a662013-01-29 00:34:06 +00001266}
1267
Reid Klecknerb5180542017-03-21 16:57:19 +00001268AttributeList::iterator AttributeList::begin(unsigned Slot) const {
Bill Wendlinga9174862013-01-31 23:53:05 +00001269 if (!pImpl)
1270 return ArrayRef<Attribute>().begin();
Bill Wendling211316c2013-04-18 20:17:28 +00001271 return pImpl->begin(Slot);
Bill Wendlinga9174862013-01-31 23:53:05 +00001272}
1273
Reid Klecknerb5180542017-03-21 16:57:19 +00001274AttributeList::iterator AttributeList::end(unsigned Slot) const {
Bill Wendlinga9174862013-01-31 23:53:05 +00001275 if (!pImpl)
1276 return ArrayRef<Attribute>().end();
Bill Wendling211316c2013-04-18 20:17:28 +00001277 return pImpl->end(Slot);
Bill Wendlinga9174862013-01-31 23:53:05 +00001278}
1279
Bill Wendlingd509a662013-01-29 00:34:06 +00001280//===----------------------------------------------------------------------===//
Reid Klecknerb5180542017-03-21 16:57:19 +00001281// AttributeList Introspection Methods
Bill Wendlingd509a662013-01-29 00:34:06 +00001282//===----------------------------------------------------------------------===//
1283
Reid Klecknerb5180542017-03-21 16:57:19 +00001284unsigned AttributeList::getNumSlots() const {
Amaury Sechet24c84fd2016-06-14 22:04:16 +00001285 return pImpl ? pImpl->getNumSlots() : 0;
Bill Wendlingd509a662013-01-29 00:34:06 +00001286}
1287
Reid Klecknerb5180542017-03-21 16:57:19 +00001288unsigned AttributeList::getSlotIndex(unsigned Slot) const {
Amaury Sechet24c84fd2016-06-14 22:04:16 +00001289 assert(pImpl && Slot < pImpl->getNumSlots() &&
Bill Wendlingd509a662013-01-29 00:34:06 +00001290 "Slot # out of range!");
1291 return pImpl->getSlotIndex(Slot);
1292}
1293
Reid Klecknerb5180542017-03-21 16:57:19 +00001294AttributeList AttributeList::getSlotAttributes(unsigned Slot) const {
Amaury Sechet24c84fd2016-06-14 22:04:16 +00001295 assert(pImpl && Slot < pImpl->getNumSlots() &&
Bill Wendlingd509a662013-01-29 00:34:06 +00001296 "Slot # out of range!");
1297 return pImpl->getSlotAttributes(Slot);
1298}
1299
Matthias Braun8c209aa2017-01-28 02:02:38 +00001300#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Reid Klecknerb5180542017-03-21 16:57:19 +00001301LLVM_DUMP_METHOD void AttributeList::dump() const {
Bill Wendlingd509a662013-01-29 00:34:06 +00001302 dbgs() << "PAL[\n";
1303
1304 for (unsigned i = 0, e = getNumSlots(); i < e; ++i) {
1305 uint64_t Index = getSlotIndex(i);
1306 dbgs() << " { ";
1307 if (Index == ~0U)
1308 dbgs() << "~0U";
1309 else
1310 dbgs() << Index;
1311 dbgs() << " => " << getAsString(Index) << " }\n";
1312 }
1313
1314 dbgs() << "]\n";
1315}
Matthias Braun8c209aa2017-01-28 02:02:38 +00001316#endif
Bill Wendlingd509a662013-01-29 00:34:06 +00001317
Bill Wendlingabf3feb2012-10-05 06:44:41 +00001318//===----------------------------------------------------------------------===//
Bill Wendlingcd330342013-01-04 23:27:34 +00001319// AttrBuilder Method Implementations
Bill Wendlingabf3feb2012-10-05 06:44:41 +00001320//===----------------------------------------------------------------------===//
1321
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001322AttrBuilder::AttrBuilder(AttributeList AL, unsigned Index) {
1323 AttributeListImpl *pImpl = AL.pImpl;
Bill Wendling096f5442013-01-07 08:24:35 +00001324 if (!pImpl) return;
1325
Amaury Sechet24c84fd2016-06-14 22:04:16 +00001326 for (unsigned I = 0, E = pImpl->getNumSlots(); I != E; ++I) {
Bill Wendling211316c2013-04-18 20:17:28 +00001327 if (pImpl->getSlotIndex(I) != Index) continue;
Bill Wendling096f5442013-01-07 08:24:35 +00001328
Reid Klecknerb5180542017-03-21 16:57:19 +00001329 for (AttributeListImpl::iterator II = pImpl->begin(I), IE = pImpl->end(I);
1330 II != IE; ++II)
Bill Wendling23804da2013-01-31 23:38:01 +00001331 addAttribute(*II);
Bill Wendling9eb689c2013-01-28 00:21:34 +00001332
1333 break;
1334 }
Bill Wendling096f5442013-01-07 08:24:35 +00001335}
1336
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001337AttrBuilder::AttrBuilder(AttributeSet AS) {
1338 if (AS.hasAttributes()) {
1339 for (const Attribute &A : AS)
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00001340 addAttribute(A);
1341 }
1342}
1343
Bill Wendlingcd330342013-01-04 23:27:34 +00001344void AttrBuilder::clear() {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001345 Attrs.reset();
Sanjoy Das88d0fde2015-09-03 22:27:42 +00001346 TargetDepAttrs.clear();
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001347 Alignment = StackAlignment = DerefBytes = DerefOrNullBytes = 0;
George Burgess IV278199f2016-04-12 01:05:35 +00001348 AllocSizeArgs = 0;
Bill Wendlingcd330342013-01-04 23:27:34 +00001349}
1350
1351AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001352 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
Bill Wendling1c7cc8a2013-01-31 23:16:25 +00001353 assert(Val != Attribute::Alignment && Val != Attribute::StackAlignment &&
George Burgess IV278199f2016-04-12 01:05:35 +00001354 Val != Attribute::Dereferenceable && Val != Attribute::AllocSize &&
Hal Finkelb0407ba2014-07-18 15:51:28 +00001355 "Adding integer attribute without adding a value!");
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001356 Attrs[Val] = true;
Bill Wendling7c04e042012-10-09 19:01:18 +00001357 return *this;
Bill Wendlingabf3feb2012-10-05 06:44:41 +00001358}
1359
Bill Wendling23804da2013-01-31 23:38:01 +00001360AttrBuilder &AttrBuilder::addAttribute(Attribute Attr) {
Bill Wendling0a437302013-02-10 10:13:23 +00001361 if (Attr.isStringAttribute()) {
1362 addAttribute(Attr.getKindAsString(), Attr.getValueAsString());
1363 return *this;
1364 }
1365
Bill Wendling3f12ac22013-02-05 22:37:24 +00001366 Attribute::AttrKind Kind = Attr.getKindAsEnum();
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001367 Attrs[Kind] = true;
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001368
Bill Wendling3f12ac22013-02-05 22:37:24 +00001369 if (Kind == Attribute::Alignment)
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001370 Alignment = Attr.getAlignment();
Bill Wendling3f12ac22013-02-05 22:37:24 +00001371 else if (Kind == Attribute::StackAlignment)
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001372 StackAlignment = Attr.getStackAlignment();
Hal Finkelb0407ba2014-07-18 15:51:28 +00001373 else if (Kind == Attribute::Dereferenceable)
1374 DerefBytes = Attr.getDereferenceableBytes();
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001375 else if (Kind == Attribute::DereferenceableOrNull)
1376 DerefOrNullBytes = Attr.getDereferenceableOrNullBytes();
George Burgess IV278199f2016-04-12 01:05:35 +00001377 else if (Kind == Attribute::AllocSize)
1378 AllocSizeArgs = Attr.getValueAsInt();
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001379 return *this;
1380}
1381
Bill Wendlingb9c5b1a2013-02-05 08:09:32 +00001382AttrBuilder &AttrBuilder::addAttribute(StringRef A, StringRef V) {
1383 TargetDepAttrs[A] = V;
1384 return *this;
1385}
1386
Bill Wendling23804da2013-01-31 23:38:01 +00001387AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001388 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
1389 Attrs[Val] = false;
Bill Wendling23804da2013-01-31 23:38:01 +00001390
1391 if (Val == Attribute::Alignment)
1392 Alignment = 0;
1393 else if (Val == Attribute::StackAlignment)
1394 StackAlignment = 0;
Hal Finkelb0407ba2014-07-18 15:51:28 +00001395 else if (Val == Attribute::Dereferenceable)
1396 DerefBytes = 0;
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001397 else if (Val == Attribute::DereferenceableOrNull)
1398 DerefOrNullBytes = 0;
George Burgess IV278199f2016-04-12 01:05:35 +00001399 else if (Val == Attribute::AllocSize)
1400 AllocSizeArgs = 0;
Bill Wendling23804da2013-01-31 23:38:01 +00001401
1402 return *this;
1403}
1404
Reid Klecknerb5180542017-03-21 16:57:19 +00001405AttrBuilder &AttrBuilder::removeAttributes(AttributeList A, uint64_t Index) {
Bill Wendling211316c2013-04-18 20:17:28 +00001406 unsigned Slot = ~0U;
Bill Wendlingf1c94e32013-02-01 00:13:50 +00001407 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
1408 if (A.getSlotIndex(I) == Index) {
Bill Wendling211316c2013-04-18 20:17:28 +00001409 Slot = I;
Bill Wendlingf1c94e32013-02-01 00:13:50 +00001410 break;
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001411 }
Bill Wendlingf1c94e32013-02-01 00:13:50 +00001412
Reid Klecknerb5180542017-03-21 16:57:19 +00001413 assert(Slot != ~0U && "Couldn't find index in AttributeList!");
Bill Wendlingf1c94e32013-02-01 00:13:50 +00001414
Reid Klecknerb5180542017-03-21 16:57:19 +00001415 for (AttributeList::iterator I = A.begin(Slot), E = A.end(Slot); I != E;
1416 ++I) {
Bill Wendling7cde51d2013-02-12 07:56:49 +00001417 Attribute Attr = *I;
Hal Finkele15442c2014-07-18 06:51:55 +00001418 if (Attr.isEnumAttribute() || Attr.isIntAttribute()) {
George Burgess IV500d3032015-12-16 05:21:02 +00001419 removeAttribute(Attr.getKindAsEnum());
Bill Wendling7cde51d2013-02-12 07:56:49 +00001420 } else {
1421 assert(Attr.isStringAttribute() && "Invalid attribute type!");
George Burgess IV500d3032015-12-16 05:21:02 +00001422 removeAttribute(Attr.getKindAsString());
Bill Wendling7cde51d2013-02-12 07:56:49 +00001423 }
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001424 }
1425
1426 return *this;
1427}
1428
Bill Wendlingb9c5b1a2013-02-05 08:09:32 +00001429AttrBuilder &AttrBuilder::removeAttribute(StringRef A) {
1430 std::map<std::string, std::string>::iterator I = TargetDepAttrs.find(A);
1431 if (I != TargetDepAttrs.end())
1432 TargetDepAttrs.erase(I);
1433 return *this;
1434}
1435
George Burgess IV278199f2016-04-12 01:05:35 +00001436std::pair<unsigned, Optional<unsigned>> AttrBuilder::getAllocSizeArgs() const {
1437 return unpackAllocSizeArgs(AllocSizeArgs);
1438}
1439
Bill Wendling50d27842012-10-15 20:35:56 +00001440AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
Bill Wendlingabd5ba22012-10-14 03:58:29 +00001441 if (Align == 0) return *this;
Bill Wendlingcd330342013-01-04 23:27:34 +00001442
Bill Wendlingabf3feb2012-10-05 06:44:41 +00001443 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1444 assert(Align <= 0x40000000 && "Alignment too large.");
Bill Wendlingcd330342013-01-04 23:27:34 +00001445
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001446 Attrs[Attribute::Alignment] = true;
Bill Wendlingcd330342013-01-04 23:27:34 +00001447 Alignment = Align;
Bill Wendlingabd5ba22012-10-14 03:58:29 +00001448 return *this;
Chris Lattner3e13b8c2008-01-02 23:42:30 +00001449}
1450
Bill Wendlingcd330342013-01-04 23:27:34 +00001451AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
1452 // Default alignment, allow the target to define how to align it.
1453 if (Align == 0) return *this;
1454
1455 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1456 assert(Align <= 0x100 && "Alignment too large.");
1457
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001458 Attrs[Attribute::StackAlignment] = true;
Bill Wendlingcd330342013-01-04 23:27:34 +00001459 StackAlignment = Align;
1460 return *this;
1461}
1462
Hal Finkelb0407ba2014-07-18 15:51:28 +00001463AttrBuilder &AttrBuilder::addDereferenceableAttr(uint64_t Bytes) {
1464 if (Bytes == 0) return *this;
1465
1466 Attrs[Attribute::Dereferenceable] = true;
1467 DerefBytes = Bytes;
1468 return *this;
1469}
1470
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001471AttrBuilder &AttrBuilder::addDereferenceableOrNullAttr(uint64_t Bytes) {
1472 if (Bytes == 0)
1473 return *this;
1474
1475 Attrs[Attribute::DereferenceableOrNull] = true;
1476 DerefOrNullBytes = Bytes;
1477 return *this;
1478}
1479
George Burgess IV278199f2016-04-12 01:05:35 +00001480AttrBuilder &AttrBuilder::addAllocSizeAttr(unsigned ElemSize,
1481 const Optional<unsigned> &NumElems) {
1482 return addAllocSizeAttrFromRawRepr(packAllocSizeArgs(ElemSize, NumElems));
1483}
1484
1485AttrBuilder &AttrBuilder::addAllocSizeAttrFromRawRepr(uint64_t RawArgs) {
1486 // (0, 0) is our "not present" value, so we need to check for it here.
1487 assert(RawArgs && "Invalid allocsize arguments -- given allocsize(0, 0)");
1488
1489 Attrs[Attribute::AllocSize] = true;
1490 // Reuse existing machinery to store this as a single 64-bit integer so we can
1491 // save a few bytes over using a pair<unsigned, Optional<unsigned>>.
1492 AllocSizeArgs = RawArgs;
1493 return *this;
1494}
1495
Bill Wendlinge2614922013-02-06 01:16:00 +00001496AttrBuilder &AttrBuilder::merge(const AttrBuilder &B) {
1497 // FIXME: What if both have alignments, but they don't match?!
1498 if (!Alignment)
1499 Alignment = B.Alignment;
1500
1501 if (!StackAlignment)
1502 StackAlignment = B.StackAlignment;
1503
Hal Finkelb0407ba2014-07-18 15:51:28 +00001504 if (!DerefBytes)
1505 DerefBytes = B.DerefBytes;
1506
Pete Cooperd2a44612015-05-06 23:19:43 +00001507 if (!DerefOrNullBytes)
1508 DerefOrNullBytes = B.DerefOrNullBytes;
1509
George Burgess IV278199f2016-04-12 01:05:35 +00001510 if (!AllocSizeArgs)
1511 AllocSizeArgs = B.AllocSizeArgs;
1512
Benjamin Kramer45e7d532013-02-16 19:13:18 +00001513 Attrs |= B.Attrs;
Bill Wendlinge2614922013-02-06 01:16:00 +00001514
Pete Cooperd2a44612015-05-06 23:19:43 +00001515 for (auto I : B.td_attrs())
1516 TargetDepAttrs[I.first] = I.second;
Bill Wendlinge2614922013-02-06 01:16:00 +00001517
1518 return *this;
1519}
1520
Pete Cooperd2a44612015-05-06 23:19:43 +00001521AttrBuilder &AttrBuilder::remove(const AttrBuilder &B) {
1522 // FIXME: What if both have alignments, but they don't match?!
1523 if (B.Alignment)
1524 Alignment = 0;
1525
1526 if (B.StackAlignment)
1527 StackAlignment = 0;
1528
1529 if (B.DerefBytes)
1530 DerefBytes = 0;
1531
1532 if (B.DerefOrNullBytes)
1533 DerefOrNullBytes = 0;
1534
George Burgess IV278199f2016-04-12 01:05:35 +00001535 if (B.AllocSizeArgs)
1536 AllocSizeArgs = 0;
1537
Pete Cooperd2a44612015-05-06 23:19:43 +00001538 Attrs &= ~B.Attrs;
1539
1540 for (auto I : B.td_attrs())
1541 TargetDepAttrs.erase(I.first);
1542
1543 return *this;
1544}
1545
1546bool AttrBuilder::overlaps(const AttrBuilder &B) const {
1547 // First check if any of the target independent attributes overlap.
1548 if ((Attrs & B.Attrs).any())
1549 return true;
1550
1551 // Then check if any target dependent ones do.
Sean Silva9011aca2017-02-22 06:34:04 +00001552 for (const auto &I : td_attrs())
Pete Cooperd2a44612015-05-06 23:19:43 +00001553 if (B.contains(I.first))
1554 return true;
1555
1556 return false;
1557}
1558
Bill Wendling4b001442013-02-06 01:33:42 +00001559bool AttrBuilder::contains(StringRef A) const {
1560 return TargetDepAttrs.find(A) != TargetDepAttrs.end();
1561}
1562
Bill Wendling50d27842012-10-15 20:35:56 +00001563bool AttrBuilder::hasAttributes() const {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001564 return !Attrs.none() || !TargetDepAttrs.empty();
Bill Wendlingc6daefa2012-10-08 23:27:46 +00001565}
Bill Wendling9ac69f92013-01-04 20:54:35 +00001566
Reid Klecknerb5180542017-03-21 16:57:19 +00001567bool AttrBuilder::hasAttributes(AttributeList A, uint64_t Index) const {
Bill Wendling211316c2013-04-18 20:17:28 +00001568 unsigned Slot = ~0U;
Bill Wendling9ca01da2013-02-02 00:42:06 +00001569 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
1570 if (A.getSlotIndex(I) == Index) {
Bill Wendling211316c2013-04-18 20:17:28 +00001571 Slot = I;
Bill Wendling9ca01da2013-02-02 00:42:06 +00001572 break;
1573 }
1574
Bill Wendling211316c2013-04-18 20:17:28 +00001575 assert(Slot != ~0U && "Couldn't find the index!");
Bill Wendling9ca01da2013-02-02 00:42:06 +00001576
Reid Klecknerb5180542017-03-21 16:57:19 +00001577 for (AttributeList::iterator I = A.begin(Slot), E = A.end(Slot); I != E;
1578 ++I) {
Bill Wendling7cde51d2013-02-12 07:56:49 +00001579 Attribute Attr = *I;
Hal Finkele15442c2014-07-18 06:51:55 +00001580 if (Attr.isEnumAttribute() || Attr.isIntAttribute()) {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001581 if (Attrs[I->getKindAsEnum()])
Bill Wendling7cde51d2013-02-12 07:56:49 +00001582 return true;
1583 } else {
1584 assert(Attr.isStringAttribute() && "Invalid attribute kind!");
1585 return TargetDepAttrs.find(Attr.getKindAsString())!=TargetDepAttrs.end();
1586 }
1587 }
Bill Wendling9ca01da2013-02-02 00:42:06 +00001588
1589 return false;
Bill Wendling70f39172012-10-09 00:01:21 +00001590}
Bill Wendling9ac69f92013-01-04 20:54:35 +00001591
Bill Wendling50d27842012-10-15 20:35:56 +00001592bool AttrBuilder::hasAlignmentAttr() const {
Bill Wendlingcd330342013-01-04 23:27:34 +00001593 return Alignment != 0;
Bill Wendlingc6daefa2012-10-08 23:27:46 +00001594}
1595
Bill Wendlingd509a662013-01-29 00:34:06 +00001596bool AttrBuilder::operator==(const AttrBuilder &B) {
Benjamin Kramer45e7d532013-02-16 19:13:18 +00001597 if (Attrs != B.Attrs)
1598 return false;
Bill Wendling4b001442013-02-06 01:33:42 +00001599
1600 for (td_const_iterator I = TargetDepAttrs.begin(),
1601 E = TargetDepAttrs.end(); I != E; ++I)
1602 if (B.TargetDepAttrs.find(I->first) == B.TargetDepAttrs.end())
1603 return false;
1604
Hal Finkelb0407ba2014-07-18 15:51:28 +00001605 return Alignment == B.Alignment && StackAlignment == B.StackAlignment &&
1606 DerefBytes == B.DerefBytes;
Bill Wendlingd509a662013-01-29 00:34:06 +00001607}
1608
Bill Wendling57625a42013-01-25 23:09:36 +00001609//===----------------------------------------------------------------------===//
1610// AttributeFuncs Function Defintions
1611//===----------------------------------------------------------------------===//
1612
Bill Wendlingc79cdff2013-02-01 01:04:27 +00001613/// \brief Which attributes cannot be applied to a type.
Craig Toppere3dcce92015-08-01 22:20:21 +00001614AttrBuilder AttributeFuncs::typeIncompatible(Type *Ty) {
Bill Wendling57625a42013-01-25 23:09:36 +00001615 AttrBuilder Incompatible;
1616
1617 if (!Ty->isIntegerTy())
1618 // Attribute that only apply to integers.
1619 Incompatible.addAttribute(Attribute::SExt)
1620 .addAttribute(Attribute::ZExt);
1621
1622 if (!Ty->isPointerTy())
1623 // Attribute that only apply to pointers.
1624 Incompatible.addAttribute(Attribute::ByVal)
1625 .addAttribute(Attribute::Nest)
1626 .addAttribute(Attribute::NoAlias)
1627 .addAttribute(Attribute::NoCapture)
Nick Lewyckyd52b1522014-05-20 01:23:40 +00001628 .addAttribute(Attribute::NonNull)
Hal Finkelb0407ba2014-07-18 15:51:28 +00001629 .addDereferenceableAttr(1) // the int here is ignored
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001630 .addDereferenceableOrNullAttr(1) // the int here is ignored
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001631 .addAttribute(Attribute::ReadNone)
1632 .addAttribute(Attribute::ReadOnly)
Reid Klecknera534a382013-12-19 02:14:12 +00001633 .addAttribute(Attribute::StructRet)
1634 .addAttribute(Attribute::InAlloca);
Bill Wendling57625a42013-01-25 23:09:36 +00001635
Pete Cooper2777d8872015-05-06 23:19:56 +00001636 return Incompatible;
Bill Wendling57625a42013-01-25 23:09:36 +00001637}
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001638
1639template<typename AttrClass>
1640static bool isEqual(const Function &Caller, const Function &Callee) {
1641 return Caller.getFnAttribute(AttrClass::getKind()) ==
1642 Callee.getFnAttribute(AttrClass::getKind());
1643}
1644
1645/// \brief Compute the logical AND of the attributes of the caller and the
1646/// callee.
1647///
1648/// This function sets the caller's attribute to false if the callee's attribute
1649/// is false.
1650template<typename AttrClass>
1651static void setAND(Function &Caller, const Function &Callee) {
1652 if (AttrClass::isSet(Caller, AttrClass::getKind()) &&
1653 !AttrClass::isSet(Callee, AttrClass::getKind()))
1654 AttrClass::set(Caller, AttrClass::getKind(), false);
1655}
1656
1657/// \brief Compute the logical OR of the attributes of the caller and the
1658/// callee.
1659///
1660/// This function sets the caller's attribute to true if the callee's attribute
1661/// is true.
1662template<typename AttrClass>
1663static void setOR(Function &Caller, const Function &Callee) {
1664 if (!AttrClass::isSet(Caller, AttrClass::getKind()) &&
1665 AttrClass::isSet(Callee, AttrClass::getKind()))
1666 AttrClass::set(Caller, AttrClass::getKind(), true);
1667}
1668
1669/// \brief If the inlined function had a higher stack protection level than the
1670/// calling function, then bump up the caller's stack protection level.
1671static void adjustCallerSSPLevel(Function &Caller, const Function &Callee) {
1672 // If upgrading the SSP attribute, clear out the old SSP Attributes first.
1673 // Having multiple SSP attributes doesn't actually hurt, but it adds useless
1674 // clutter to the IR.
1675 AttrBuilder B;
1676 B.addAttribute(Attribute::StackProtect)
1677 .addAttribute(Attribute::StackProtectStrong)
1678 .addAttribute(Attribute::StackProtectReq);
Reid Klecknerb5180542017-03-21 16:57:19 +00001679 AttributeList OldSSPAttr =
1680 AttributeList::get(Caller.getContext(), AttributeList::FunctionIndex, B);
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001681
Evgeniy Stepanovf17120a2016-04-11 22:27:48 +00001682 if (Callee.hasFnAttribute(Attribute::StackProtectReq)) {
Reid Klecknerb5180542017-03-21 16:57:19 +00001683 Caller.removeAttributes(AttributeList::FunctionIndex, OldSSPAttr);
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001684 Caller.addFnAttr(Attribute::StackProtectReq);
1685 } else if (Callee.hasFnAttribute(Attribute::StackProtectStrong) &&
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001686 !Caller.hasFnAttribute(Attribute::StackProtectReq)) {
Reid Klecknerb5180542017-03-21 16:57:19 +00001687 Caller.removeAttributes(AttributeList::FunctionIndex, OldSSPAttr);
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001688 Caller.addFnAttr(Attribute::StackProtectStrong);
1689 } else if (Callee.hasFnAttribute(Attribute::StackProtect) &&
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001690 !Caller.hasFnAttribute(Attribute::StackProtectReq) &&
1691 !Caller.hasFnAttribute(Attribute::StackProtectStrong))
1692 Caller.addFnAttr(Attribute::StackProtect);
1693}
1694
1695#define GET_ATTR_COMPAT_FUNC
1696#include "AttributesCompatFunc.inc"
1697
1698bool AttributeFuncs::areInlineCompatible(const Function &Caller,
1699 const Function &Callee) {
1700 return hasCompatibleFnAttrs(Caller, Callee);
1701}
1702
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001703void AttributeFuncs::mergeAttributesForInlining(Function &Caller,
1704 const Function &Callee) {
1705 mergeFnAttrs(Caller, Callee);
1706}