blob: 2b7359dab807d1b865b7dfed28ccfb060b413620 [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 Klecknerc2cb5602017-04-12 00:38:00 +0000987 AttributeSet AS) const {
988 if (!AS.hasAttributes())
Reid Klecknereb9dd5b2017-04-10 23:31:05 +0000989 return *this;
990
Bill Wendlingd509a662013-01-29 00:34:06 +0000991#ifndef NDEBUG
992 // FIXME it is not obvious how this should work for alignment. For now, say
993 // we can't change a known alignment.
Bill Wendling211316c2013-04-18 20:17:28 +0000994 unsigned OldAlign = getParamAlignment(Index);
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000995 unsigned NewAlign = AS.getAlignment();
Bill Wendlingd509a662013-01-29 00:34:06 +0000996 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
997 "Attempt to change alignment!");
998#endif
999
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001000 SmallVector<std::pair<unsigned, AttributeSet>, 4> AttrSet;
Amaury Sechet24c84fd2016-06-14 22:04:16 +00001001 uint64_t NumAttrs = pImpl->getNumSlots();
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00001002 unsigned I;
1003
1004 // Add all the attribute slots before the one we need to merge.
1005 for (I = 0; I < NumAttrs; ++I) {
1006 if (getSlotIndex(I) >= Index)
Bill Wendlingd509a662013-01-29 00:34:06 +00001007 break;
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00001008 AttrSet.emplace_back(getSlotIndex(I), pImpl->getSlotNode(I));
Bill Wendlingd509a662013-01-29 00:34:06 +00001009 }
1010
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00001011 if (I < NumAttrs && getSlotIndex(I) == Index) {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001012 // We need to merge two AttributeSets.
1013 AttributeSet Merged = AttributeSet::get(
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00001014 C, AttrBuilder(pImpl->getSlotNode(I)).merge(AttrBuilder(AS)));
1015 AttrSet.emplace_back(Index, Merged);
1016 ++I;
1017 } else {
1018 // Otherwise, there were no attributes at this position in the original
1019 // list. Add the set as is.
1020 AttrSet.emplace_back(Index, AS);
1021 }
Bill Wendlingd509a662013-01-29 00:34:06 +00001022
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00001023 // Add the remaining entries.
1024 for (; I < NumAttrs; ++I)
1025 AttrSet.emplace_back(getSlotIndex(I), pImpl->getSlotNode(I));
Bill Wendlingd509a662013-01-29 00:34:06 +00001026
1027 return get(C, AttrSet);
1028}
1029
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00001030AttributeList AttributeList::addAttributes(LLVMContext &C, unsigned Index,
1031 const AttrBuilder &B) const {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001032 return get(C, Index, AttributeSet::get(C, B));
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00001033}
1034
Reid Klecknerb5180542017-03-21 16:57:19 +00001035AttributeList AttributeList::removeAttribute(LLVMContext &C, unsigned Index,
1036 Attribute::AttrKind Kind) const {
Amaury Sechet392638d2016-06-14 20:27:35 +00001037 if (!hasAttribute(Index, Kind)) return *this;
Reid Klecknerb5180542017-03-21 16:57:19 +00001038 return removeAttributes(C, Index, AttributeList::get(C, Index, Kind));
Bill Wendlingd509a662013-01-29 00:34:06 +00001039}
1040
Reid Klecknerb5180542017-03-21 16:57:19 +00001041AttributeList AttributeList::removeAttribute(LLVMContext &C, unsigned Index,
1042 StringRef Kind) const {
Amaury Sechet6100adf2016-06-15 17:50:39 +00001043 if (!hasAttribute(Index, Kind)) return *this;
Reid Klecknerb5180542017-03-21 16:57:19 +00001044 return removeAttributes(C, Index, AttributeList::get(C, Index, Kind));
Amaury Sechet6100adf2016-06-15 17:50:39 +00001045}
1046
Reid Klecknerb5180542017-03-21 16:57:19 +00001047AttributeList AttributeList::removeAttributes(LLVMContext &C, unsigned Index,
1048 AttributeList Attrs) const {
1049 if (!pImpl)
1050 return AttributeList();
Bill Wendlingd509a662013-01-29 00:34:06 +00001051 if (!Attrs.pImpl) return *this;
1052
Pete Cooper67cf9a72015-11-19 05:56:52 +00001053 // FIXME it is not obvious how this should work for alignment.
1054 // For now, say we can't pass in alignment, which no current use does.
1055 assert(!Attrs.hasAttribute(Index, Attribute::Alignment) &&
1056 "Attempt to change alignment!");
1057
Bill Wendlingd509a662013-01-29 00:34:06 +00001058 // Add the attribute slots before the one we're trying to add.
Reid Klecknerb5180542017-03-21 16:57:19 +00001059 SmallVector<AttributeList, 4> AttrSet;
Amaury Sechet24c84fd2016-06-14 22:04:16 +00001060 uint64_t NumAttrs = pImpl->getNumSlots();
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001061 AttributeList AL;
Bill Wendlingd509a662013-01-29 00:34:06 +00001062 uint64_t LastIndex = 0;
1063 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
Bill Wendling211316c2013-04-18 20:17:28 +00001064 if (getSlotIndex(I) >= Index) {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001065 if (getSlotIndex(I) == Index) AL = getSlotAttributes(LastIndex++);
Bill Wendlingd509a662013-01-29 00:34:06 +00001066 break;
1067 }
1068 LastIndex = I + 1;
1069 AttrSet.push_back(getSlotAttributes(I));
1070 }
1071
Bill Wendlingd2196752013-01-30 23:07:40 +00001072 // Now remove the attribute from the correct slot. There may already be an
Reid Klecknerb5180542017-03-21 16:57:19 +00001073 // AttributeList there.
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001074 AttrBuilder B(AL, Index);
Bill Wendlingd509a662013-01-29 00:34:06 +00001075
Amaury Sechet24c84fd2016-06-14 22:04:16 +00001076 for (unsigned I = 0, E = Attrs.pImpl->getNumSlots(); I != E; ++I)
Bill Wendling211316c2013-04-18 20:17:28 +00001077 if (Attrs.getSlotIndex(I) == Index) {
1078 B.removeAttributes(Attrs.pImpl->getSlotAttributes(I), Index);
Bill Wendlingd509a662013-01-29 00:34:06 +00001079 break;
1080 }
1081
Reid Klecknerb5180542017-03-21 16:57:19 +00001082 AttrSet.push_back(AttributeList::get(C, Index, B));
Bill Wendlingd509a662013-01-29 00:34:06 +00001083
1084 // Add the remaining attribute slots.
1085 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
1086 AttrSet.push_back(getSlotAttributes(I));
1087
1088 return get(C, AttrSet);
1089}
1090
Reid Klecknerb5180542017-03-21 16:57:19 +00001091AttributeList AttributeList::removeAttributes(LLVMContext &C, unsigned Index,
1092 const AttrBuilder &Attrs) const {
1093 if (!pImpl)
1094 return AttributeList();
Pete Cooperd2a44612015-05-06 23:19:43 +00001095
1096 // FIXME it is not obvious how this should work for alignment.
1097 // For now, say we can't pass in alignment, which no current use does.
1098 assert(!Attrs.hasAlignmentAttr() && "Attempt to change alignment!");
1099
1100 // Add the attribute slots before the one we're trying to add.
Reid Klecknerb5180542017-03-21 16:57:19 +00001101 SmallVector<AttributeList, 4> AttrSet;
Amaury Sechet24c84fd2016-06-14 22:04:16 +00001102 uint64_t NumAttrs = pImpl->getNumSlots();
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001103 AttributeList AL;
Pete Cooperd2a44612015-05-06 23:19:43 +00001104 uint64_t LastIndex = 0;
1105 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
1106 if (getSlotIndex(I) >= Index) {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001107 if (getSlotIndex(I) == Index) AL = getSlotAttributes(LastIndex++);
Pete Cooperd2a44612015-05-06 23:19:43 +00001108 break;
1109 }
1110 LastIndex = I + 1;
1111 AttrSet.push_back(getSlotAttributes(I));
1112 }
1113
1114 // Now remove the attribute from the correct slot. There may already be an
Reid Klecknerb5180542017-03-21 16:57:19 +00001115 // AttributeList there.
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001116 AttrBuilder B(AL, Index);
Pete Cooperd2a44612015-05-06 23:19:43 +00001117 B.remove(Attrs);
1118
Reid Klecknerb5180542017-03-21 16:57:19 +00001119 AttrSet.push_back(AttributeList::get(C, Index, B));
Pete Cooperd2a44612015-05-06 23:19:43 +00001120
1121 // Add the remaining attribute slots.
1122 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
1123 AttrSet.push_back(getSlotAttributes(I));
1124
1125 return get(C, AttrSet);
1126}
1127
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00001128AttributeList AttributeList::removeAttributes(LLVMContext &C,
1129 unsigned WithoutIndex) const {
1130 if (!pImpl)
1131 return AttributeList();
1132
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001133 SmallVector<std::pair<unsigned, AttributeSet>, 4> AttrSet;
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00001134 for (unsigned I = 0, E = pImpl->getNumSlots(); I != E; ++I) {
1135 unsigned Index = getSlotIndex(I);
1136 if (Index != WithoutIndex)
1137 AttrSet.push_back({Index, pImpl->getSlotNode(I)});
1138 }
1139 return get(C, AttrSet);
1140}
1141
Reid Klecknerb5180542017-03-21 16:57:19 +00001142AttributeList AttributeList::addDereferenceableAttr(LLVMContext &C,
1143 unsigned Index,
1144 uint64_t Bytes) const {
Eugene Zelenkodeaf6952017-02-17 00:00:09 +00001145 AttrBuilder B;
Ramkumar Ramachandra8fcb4982015-02-14 19:37:54 +00001146 B.addDereferenceableAttr(Bytes);
Reid Klecknerb5180542017-03-21 16:57:19 +00001147 return addAttributes(C, Index, AttributeList::get(C, Index, B));
Ramkumar Ramachandra8fcb4982015-02-14 19:37:54 +00001148}
1149
Reid Klecknerb5180542017-03-21 16:57:19 +00001150AttributeList
1151AttributeList::addDereferenceableOrNullAttr(LLVMContext &C, unsigned Index,
1152 uint64_t Bytes) const {
Eugene Zelenkodeaf6952017-02-17 00:00:09 +00001153 AttrBuilder B;
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001154 B.addDereferenceableOrNullAttr(Bytes);
Reid Klecknerb5180542017-03-21 16:57:19 +00001155 return addAttributes(C, Index, AttributeList::get(C, Index, B));
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001156}
1157
Reid Klecknerb5180542017-03-21 16:57:19 +00001158AttributeList
1159AttributeList::addAllocSizeAttr(LLVMContext &C, unsigned Index,
1160 unsigned ElemSizeArg,
1161 const Optional<unsigned> &NumElemsArg) {
Eugene Zelenkodeaf6952017-02-17 00:00:09 +00001162 AttrBuilder B;
George Burgess IV278199f2016-04-12 01:05:35 +00001163 B.addAllocSizeAttr(ElemSizeArg, NumElemsArg);
Reid Klecknerb5180542017-03-21 16:57:19 +00001164 return addAttributes(C, Index, AttributeList::get(C, Index, B));
George Burgess IV278199f2016-04-12 01:05:35 +00001165}
1166
Bill Wendlingd509a662013-01-29 00:34:06 +00001167//===----------------------------------------------------------------------===//
Reid Klecknerb5180542017-03-21 16:57:19 +00001168// AttributeList Accessor Methods
Bill Wendlingd509a662013-01-29 00:34:06 +00001169//===----------------------------------------------------------------------===//
1170
Reid Klecknerb5180542017-03-21 16:57:19 +00001171LLVMContext &AttributeList::getContext() const { return pImpl->getContext(); }
1172
Reid Klecknerf021fab2017-04-13 23:12:13 +00001173AttributeSet AttributeList::getParamAttributes(unsigned ArgNo) const {
1174 return getAttributes(ArgNo + 1);
Bill Wendling5d020a32013-02-10 05:00:40 +00001175}
1176
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001177AttributeSet AttributeList::getRetAttributes() const {
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00001178 return getAttributes(ReturnIndex);
Bill Wendlingd509a662013-01-29 00:34:06 +00001179}
1180
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001181AttributeSet AttributeList::getFnAttributes() const {
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00001182 return getAttributes(FunctionIndex);
Bill Wendlingd509a662013-01-29 00:34:06 +00001183}
1184
Reid Klecknerb5180542017-03-21 16:57:19 +00001185bool AttributeList::hasAttribute(unsigned Index,
1186 Attribute::AttrKind Kind) const {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001187 return getAttributes(Index).hasAttribute(Kind);
Bill Wendlingd509a662013-01-29 00:34:06 +00001188}
1189
Reid Klecknerb5180542017-03-21 16:57:19 +00001190bool AttributeList::hasAttribute(unsigned Index, StringRef Kind) const {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001191 return getAttributes(Index).hasAttribute(Kind);
Bill Wendlingbce7b972013-02-13 08:42:21 +00001192}
1193
Reid Klecknerb5180542017-03-21 16:57:19 +00001194bool AttributeList::hasAttributes(unsigned Index) const {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001195 return getAttributes(Index).hasAttributes();
Bill Wendlingd509a662013-01-29 00:34:06 +00001196}
1197
Reid Klecknerb5180542017-03-21 16:57:19 +00001198bool AttributeList::hasFnAttribute(Attribute::AttrKind Kind) const {
Matthias Braun33282812016-01-29 22:25:19 +00001199 return pImpl && pImpl->hasFnAttribute(Kind);
1200}
1201
Reid Klecknerb5180542017-03-21 16:57:19 +00001202bool AttributeList::hasFnAttribute(StringRef Kind) const {
1203 return hasAttribute(AttributeList::FunctionIndex, Kind);
Amaury Sechet5f04d812016-09-09 04:50:38 +00001204}
1205
Reid Klecknerf021fab2017-04-13 23:12:13 +00001206bool AttributeList::hasParamAttribute(unsigned ArgNo,
1207 Attribute::AttrKind Kind) const {
1208 return hasAttribute(ArgNo + 1, Kind);
1209}
1210
Reid Klecknerb5180542017-03-21 16:57:19 +00001211bool AttributeList::hasAttrSomewhere(Attribute::AttrKind Attr,
1212 unsigned *Index) const {
Craig Topperc6207612014-04-09 06:08:46 +00001213 if (!pImpl) return false;
Bill Wendlingd509a662013-01-29 00:34:06 +00001214
Amaury Sechet24c84fd2016-06-14 22:04:16 +00001215 for (unsigned I = 0, E = pImpl->getNumSlots(); I != E; ++I)
Reid Klecknerb5180542017-03-21 16:57:19 +00001216 for (AttributeListImpl::iterator II = pImpl->begin(I), IE = pImpl->end(I);
1217 II != IE; ++II)
Hal Finkele87ad542016-07-10 23:01:32 +00001218 if (II->hasAttribute(Attr)) {
1219 if (Index) *Index = pImpl->getSlotIndex(I);
Bill Wendlingd509a662013-01-29 00:34:06 +00001220 return true;
Hal Finkele87ad542016-07-10 23:01:32 +00001221 }
Bill Wendlingd509a662013-01-29 00:34:06 +00001222
1223 return false;
1224}
1225
Reid Klecknerb5180542017-03-21 16:57:19 +00001226Attribute AttributeList::getAttribute(unsigned Index,
1227 Attribute::AttrKind Kind) const {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001228 return getAttributes(Index).getAttribute(Kind);
Bill Wendlingbce7b972013-02-13 08:42:21 +00001229}
1230
Reid Klecknerb5180542017-03-21 16:57:19 +00001231Attribute AttributeList::getAttribute(unsigned Index, StringRef Kind) const {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001232 return getAttributes(Index).getAttribute(Kind);
Bill Wendlingbce7b972013-02-13 08:42:21 +00001233}
1234
Reid Klecknerb5180542017-03-21 16:57:19 +00001235unsigned AttributeList::getParamAlignment(unsigned Index) const {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001236 return getAttributes(Index).getAlignment();
Bill Wendlingd509a662013-01-29 00:34:06 +00001237}
1238
Reid Klecknerb5180542017-03-21 16:57:19 +00001239unsigned AttributeList::getStackAlignment(unsigned Index) const {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001240 return getAttributes(Index).getStackAlignment();
Bill Wendlingd509a662013-01-29 00:34:06 +00001241}
1242
Reid Klecknerb5180542017-03-21 16:57:19 +00001243uint64_t AttributeList::getDereferenceableBytes(unsigned Index) const {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001244 return getAttributes(Index).getDereferenceableBytes();
Hal Finkelb0407ba2014-07-18 15:51:28 +00001245}
1246
Reid Klecknerb5180542017-03-21 16:57:19 +00001247uint64_t AttributeList::getDereferenceableOrNullBytes(unsigned Index) const {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001248 return getAttributes(Index).getDereferenceableOrNullBytes();
Sanjoy Das06cf33f2015-05-06 17:41:54 +00001249}
1250
George Burgess IV278199f2016-04-12 01:05:35 +00001251std::pair<unsigned, Optional<unsigned>>
Reid Klecknerb5180542017-03-21 16:57:19 +00001252AttributeList::getAllocSizeArgs(unsigned Index) const {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001253 return getAttributes(Index).getAllocSizeArgs();
George Burgess IV278199f2016-04-12 01:05:35 +00001254}
1255
Reid Klecknerb5180542017-03-21 16:57:19 +00001256std::string AttributeList::getAsString(unsigned Index, bool InAttrGrp) const {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001257 return getAttributes(Index).getAsString(InAttrGrp);
Bill Wendlingd509a662013-01-29 00:34:06 +00001258}
1259
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001260AttributeSet AttributeList::getAttributes(unsigned Index) const {
1261 if (!pImpl) return AttributeSet();
Bill Wendlingd509a662013-01-29 00:34:06 +00001262
Bill Wendlingf2955aa2013-01-29 03:20:31 +00001263 // Loop through to find the attribute node we want.
Amaury Sechet24c84fd2016-06-14 22:04:16 +00001264 for (unsigned I = 0, E = pImpl->getNumSlots(); I != E; ++I)
Bill Wendling211316c2013-04-18 20:17:28 +00001265 if (pImpl->getSlotIndex(I) == Index)
Bill Wendlingf2955aa2013-01-29 03:20:31 +00001266 return pImpl->getSlotNode(I);
Bill Wendlingd509a662013-01-29 00:34:06 +00001267
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001268 return AttributeSet();
Bill Wendlingd509a662013-01-29 00:34:06 +00001269}
1270
Reid Klecknerb5180542017-03-21 16:57:19 +00001271AttributeList::iterator AttributeList::begin(unsigned Slot) const {
Bill Wendlinga9174862013-01-31 23:53:05 +00001272 if (!pImpl)
1273 return ArrayRef<Attribute>().begin();
Bill Wendling211316c2013-04-18 20:17:28 +00001274 return pImpl->begin(Slot);
Bill Wendlinga9174862013-01-31 23:53:05 +00001275}
1276
Reid Klecknerb5180542017-03-21 16:57:19 +00001277AttributeList::iterator AttributeList::end(unsigned Slot) const {
Bill Wendlinga9174862013-01-31 23:53:05 +00001278 if (!pImpl)
1279 return ArrayRef<Attribute>().end();
Bill Wendling211316c2013-04-18 20:17:28 +00001280 return pImpl->end(Slot);
Bill Wendlinga9174862013-01-31 23:53:05 +00001281}
1282
Bill Wendlingd509a662013-01-29 00:34:06 +00001283//===----------------------------------------------------------------------===//
Reid Klecknerb5180542017-03-21 16:57:19 +00001284// AttributeList Introspection Methods
Bill Wendlingd509a662013-01-29 00:34:06 +00001285//===----------------------------------------------------------------------===//
1286
Reid Klecknerb5180542017-03-21 16:57:19 +00001287unsigned AttributeList::getNumSlots() const {
Amaury Sechet24c84fd2016-06-14 22:04:16 +00001288 return pImpl ? pImpl->getNumSlots() : 0;
Bill Wendlingd509a662013-01-29 00:34:06 +00001289}
1290
Reid Klecknerb5180542017-03-21 16:57:19 +00001291unsigned AttributeList::getSlotIndex(unsigned Slot) const {
Amaury Sechet24c84fd2016-06-14 22:04:16 +00001292 assert(pImpl && Slot < pImpl->getNumSlots() &&
Bill Wendlingd509a662013-01-29 00:34:06 +00001293 "Slot # out of range!");
1294 return pImpl->getSlotIndex(Slot);
1295}
1296
Reid Klecknerb5180542017-03-21 16:57:19 +00001297AttributeList AttributeList::getSlotAttributes(unsigned Slot) const {
Amaury Sechet24c84fd2016-06-14 22:04:16 +00001298 assert(pImpl && Slot < pImpl->getNumSlots() &&
Bill Wendlingd509a662013-01-29 00:34:06 +00001299 "Slot # out of range!");
1300 return pImpl->getSlotAttributes(Slot);
1301}
1302
Matthias Braun8c209aa2017-01-28 02:02:38 +00001303#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Reid Klecknerb5180542017-03-21 16:57:19 +00001304LLVM_DUMP_METHOD void AttributeList::dump() const {
Bill Wendlingd509a662013-01-29 00:34:06 +00001305 dbgs() << "PAL[\n";
1306
1307 for (unsigned i = 0, e = getNumSlots(); i < e; ++i) {
1308 uint64_t Index = getSlotIndex(i);
1309 dbgs() << " { ";
1310 if (Index == ~0U)
1311 dbgs() << "~0U";
1312 else
1313 dbgs() << Index;
1314 dbgs() << " => " << getAsString(Index) << " }\n";
1315 }
1316
1317 dbgs() << "]\n";
1318}
Matthias Braun8c209aa2017-01-28 02:02:38 +00001319#endif
Bill Wendlingd509a662013-01-29 00:34:06 +00001320
Bill Wendlingabf3feb2012-10-05 06:44:41 +00001321//===----------------------------------------------------------------------===//
Bill Wendlingcd330342013-01-04 23:27:34 +00001322// AttrBuilder Method Implementations
Bill Wendlingabf3feb2012-10-05 06:44:41 +00001323//===----------------------------------------------------------------------===//
1324
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001325AttrBuilder::AttrBuilder(AttributeList AL, unsigned Index) {
1326 AttributeListImpl *pImpl = AL.pImpl;
Bill Wendling096f5442013-01-07 08:24:35 +00001327 if (!pImpl) return;
1328
Amaury Sechet24c84fd2016-06-14 22:04:16 +00001329 for (unsigned I = 0, E = pImpl->getNumSlots(); I != E; ++I) {
Bill Wendling211316c2013-04-18 20:17:28 +00001330 if (pImpl->getSlotIndex(I) != Index) continue;
Bill Wendling096f5442013-01-07 08:24:35 +00001331
Reid Klecknerb5180542017-03-21 16:57:19 +00001332 for (AttributeListImpl::iterator II = pImpl->begin(I), IE = pImpl->end(I);
1333 II != IE; ++II)
Bill Wendling23804da2013-01-31 23:38:01 +00001334 addAttribute(*II);
Bill Wendling9eb689c2013-01-28 00:21:34 +00001335
1336 break;
1337 }
Bill Wendling096f5442013-01-07 08:24:35 +00001338}
1339
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001340AttrBuilder::AttrBuilder(AttributeSet AS) {
1341 if (AS.hasAttributes()) {
1342 for (const Attribute &A : AS)
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00001343 addAttribute(A);
1344 }
1345}
1346
Bill Wendlingcd330342013-01-04 23:27:34 +00001347void AttrBuilder::clear() {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001348 Attrs.reset();
Sanjoy Das88d0fde2015-09-03 22:27:42 +00001349 TargetDepAttrs.clear();
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001350 Alignment = StackAlignment = DerefBytes = DerefOrNullBytes = 0;
George Burgess IV278199f2016-04-12 01:05:35 +00001351 AllocSizeArgs = 0;
Bill Wendlingcd330342013-01-04 23:27:34 +00001352}
1353
1354AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001355 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
Bill Wendling1c7cc8a2013-01-31 23:16:25 +00001356 assert(Val != Attribute::Alignment && Val != Attribute::StackAlignment &&
George Burgess IV278199f2016-04-12 01:05:35 +00001357 Val != Attribute::Dereferenceable && Val != Attribute::AllocSize &&
Hal Finkelb0407ba2014-07-18 15:51:28 +00001358 "Adding integer attribute without adding a value!");
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001359 Attrs[Val] = true;
Bill Wendling7c04e042012-10-09 19:01:18 +00001360 return *this;
Bill Wendlingabf3feb2012-10-05 06:44:41 +00001361}
1362
Bill Wendling23804da2013-01-31 23:38:01 +00001363AttrBuilder &AttrBuilder::addAttribute(Attribute Attr) {
Bill Wendling0a437302013-02-10 10:13:23 +00001364 if (Attr.isStringAttribute()) {
1365 addAttribute(Attr.getKindAsString(), Attr.getValueAsString());
1366 return *this;
1367 }
1368
Bill Wendling3f12ac22013-02-05 22:37:24 +00001369 Attribute::AttrKind Kind = Attr.getKindAsEnum();
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001370 Attrs[Kind] = true;
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001371
Bill Wendling3f12ac22013-02-05 22:37:24 +00001372 if (Kind == Attribute::Alignment)
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001373 Alignment = Attr.getAlignment();
Bill Wendling3f12ac22013-02-05 22:37:24 +00001374 else if (Kind == Attribute::StackAlignment)
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001375 StackAlignment = Attr.getStackAlignment();
Hal Finkelb0407ba2014-07-18 15:51:28 +00001376 else if (Kind == Attribute::Dereferenceable)
1377 DerefBytes = Attr.getDereferenceableBytes();
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001378 else if (Kind == Attribute::DereferenceableOrNull)
1379 DerefOrNullBytes = Attr.getDereferenceableOrNullBytes();
George Burgess IV278199f2016-04-12 01:05:35 +00001380 else if (Kind == Attribute::AllocSize)
1381 AllocSizeArgs = Attr.getValueAsInt();
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001382 return *this;
1383}
1384
Bill Wendlingb9c5b1a2013-02-05 08:09:32 +00001385AttrBuilder &AttrBuilder::addAttribute(StringRef A, StringRef V) {
1386 TargetDepAttrs[A] = V;
1387 return *this;
1388}
1389
Bill Wendling23804da2013-01-31 23:38:01 +00001390AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001391 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
1392 Attrs[Val] = false;
Bill Wendling23804da2013-01-31 23:38:01 +00001393
1394 if (Val == Attribute::Alignment)
1395 Alignment = 0;
1396 else if (Val == Attribute::StackAlignment)
1397 StackAlignment = 0;
Hal Finkelb0407ba2014-07-18 15:51:28 +00001398 else if (Val == Attribute::Dereferenceable)
1399 DerefBytes = 0;
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001400 else if (Val == Attribute::DereferenceableOrNull)
1401 DerefOrNullBytes = 0;
George Burgess IV278199f2016-04-12 01:05:35 +00001402 else if (Val == Attribute::AllocSize)
1403 AllocSizeArgs = 0;
Bill Wendling23804da2013-01-31 23:38:01 +00001404
1405 return *this;
1406}
1407
Reid Klecknerb5180542017-03-21 16:57:19 +00001408AttrBuilder &AttrBuilder::removeAttributes(AttributeList A, uint64_t Index) {
Bill Wendling211316c2013-04-18 20:17:28 +00001409 unsigned Slot = ~0U;
Bill Wendlingf1c94e32013-02-01 00:13:50 +00001410 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
1411 if (A.getSlotIndex(I) == Index) {
Bill Wendling211316c2013-04-18 20:17:28 +00001412 Slot = I;
Bill Wendlingf1c94e32013-02-01 00:13:50 +00001413 break;
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001414 }
Bill Wendlingf1c94e32013-02-01 00:13:50 +00001415
Reid Klecknerb5180542017-03-21 16:57:19 +00001416 assert(Slot != ~0U && "Couldn't find index in AttributeList!");
Bill Wendlingf1c94e32013-02-01 00:13:50 +00001417
Reid Klecknerb5180542017-03-21 16:57:19 +00001418 for (AttributeList::iterator I = A.begin(Slot), E = A.end(Slot); I != E;
1419 ++I) {
Bill Wendling7cde51d2013-02-12 07:56:49 +00001420 Attribute Attr = *I;
Hal Finkele15442c2014-07-18 06:51:55 +00001421 if (Attr.isEnumAttribute() || Attr.isIntAttribute()) {
George Burgess IV500d3032015-12-16 05:21:02 +00001422 removeAttribute(Attr.getKindAsEnum());
Bill Wendling7cde51d2013-02-12 07:56:49 +00001423 } else {
1424 assert(Attr.isStringAttribute() && "Invalid attribute type!");
George Burgess IV500d3032015-12-16 05:21:02 +00001425 removeAttribute(Attr.getKindAsString());
Bill Wendling7cde51d2013-02-12 07:56:49 +00001426 }
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001427 }
1428
1429 return *this;
1430}
1431
Bill Wendlingb9c5b1a2013-02-05 08:09:32 +00001432AttrBuilder &AttrBuilder::removeAttribute(StringRef A) {
1433 std::map<std::string, std::string>::iterator I = TargetDepAttrs.find(A);
1434 if (I != TargetDepAttrs.end())
1435 TargetDepAttrs.erase(I);
1436 return *this;
1437}
1438
George Burgess IV278199f2016-04-12 01:05:35 +00001439std::pair<unsigned, Optional<unsigned>> AttrBuilder::getAllocSizeArgs() const {
1440 return unpackAllocSizeArgs(AllocSizeArgs);
1441}
1442
Bill Wendling50d27842012-10-15 20:35:56 +00001443AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
Bill Wendlingabd5ba22012-10-14 03:58:29 +00001444 if (Align == 0) return *this;
Bill Wendlingcd330342013-01-04 23:27:34 +00001445
Bill Wendlingabf3feb2012-10-05 06:44:41 +00001446 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1447 assert(Align <= 0x40000000 && "Alignment too large.");
Bill Wendlingcd330342013-01-04 23:27:34 +00001448
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001449 Attrs[Attribute::Alignment] = true;
Bill Wendlingcd330342013-01-04 23:27:34 +00001450 Alignment = Align;
Bill Wendlingabd5ba22012-10-14 03:58:29 +00001451 return *this;
Chris Lattner3e13b8c2008-01-02 23:42:30 +00001452}
1453
Bill Wendlingcd330342013-01-04 23:27:34 +00001454AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
1455 // Default alignment, allow the target to define how to align it.
1456 if (Align == 0) return *this;
1457
1458 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1459 assert(Align <= 0x100 && "Alignment too large.");
1460
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001461 Attrs[Attribute::StackAlignment] = true;
Bill Wendlingcd330342013-01-04 23:27:34 +00001462 StackAlignment = Align;
1463 return *this;
1464}
1465
Hal Finkelb0407ba2014-07-18 15:51:28 +00001466AttrBuilder &AttrBuilder::addDereferenceableAttr(uint64_t Bytes) {
1467 if (Bytes == 0) return *this;
1468
1469 Attrs[Attribute::Dereferenceable] = true;
1470 DerefBytes = Bytes;
1471 return *this;
1472}
1473
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001474AttrBuilder &AttrBuilder::addDereferenceableOrNullAttr(uint64_t Bytes) {
1475 if (Bytes == 0)
1476 return *this;
1477
1478 Attrs[Attribute::DereferenceableOrNull] = true;
1479 DerefOrNullBytes = Bytes;
1480 return *this;
1481}
1482
George Burgess IV278199f2016-04-12 01:05:35 +00001483AttrBuilder &AttrBuilder::addAllocSizeAttr(unsigned ElemSize,
1484 const Optional<unsigned> &NumElems) {
1485 return addAllocSizeAttrFromRawRepr(packAllocSizeArgs(ElemSize, NumElems));
1486}
1487
1488AttrBuilder &AttrBuilder::addAllocSizeAttrFromRawRepr(uint64_t RawArgs) {
1489 // (0, 0) is our "not present" value, so we need to check for it here.
1490 assert(RawArgs && "Invalid allocsize arguments -- given allocsize(0, 0)");
1491
1492 Attrs[Attribute::AllocSize] = true;
1493 // Reuse existing machinery to store this as a single 64-bit integer so we can
1494 // save a few bytes over using a pair<unsigned, Optional<unsigned>>.
1495 AllocSizeArgs = RawArgs;
1496 return *this;
1497}
1498
Bill Wendlinge2614922013-02-06 01:16:00 +00001499AttrBuilder &AttrBuilder::merge(const AttrBuilder &B) {
1500 // FIXME: What if both have alignments, but they don't match?!
1501 if (!Alignment)
1502 Alignment = B.Alignment;
1503
1504 if (!StackAlignment)
1505 StackAlignment = B.StackAlignment;
1506
Hal Finkelb0407ba2014-07-18 15:51:28 +00001507 if (!DerefBytes)
1508 DerefBytes = B.DerefBytes;
1509
Pete Cooperd2a44612015-05-06 23:19:43 +00001510 if (!DerefOrNullBytes)
1511 DerefOrNullBytes = B.DerefOrNullBytes;
1512
George Burgess IV278199f2016-04-12 01:05:35 +00001513 if (!AllocSizeArgs)
1514 AllocSizeArgs = B.AllocSizeArgs;
1515
Benjamin Kramer45e7d532013-02-16 19:13:18 +00001516 Attrs |= B.Attrs;
Bill Wendlinge2614922013-02-06 01:16:00 +00001517
Pete Cooperd2a44612015-05-06 23:19:43 +00001518 for (auto I : B.td_attrs())
1519 TargetDepAttrs[I.first] = I.second;
Bill Wendlinge2614922013-02-06 01:16:00 +00001520
1521 return *this;
1522}
1523
Pete Cooperd2a44612015-05-06 23:19:43 +00001524AttrBuilder &AttrBuilder::remove(const AttrBuilder &B) {
1525 // FIXME: What if both have alignments, but they don't match?!
1526 if (B.Alignment)
1527 Alignment = 0;
1528
1529 if (B.StackAlignment)
1530 StackAlignment = 0;
1531
1532 if (B.DerefBytes)
1533 DerefBytes = 0;
1534
1535 if (B.DerefOrNullBytes)
1536 DerefOrNullBytes = 0;
1537
George Burgess IV278199f2016-04-12 01:05:35 +00001538 if (B.AllocSizeArgs)
1539 AllocSizeArgs = 0;
1540
Pete Cooperd2a44612015-05-06 23:19:43 +00001541 Attrs &= ~B.Attrs;
1542
1543 for (auto I : B.td_attrs())
1544 TargetDepAttrs.erase(I.first);
1545
1546 return *this;
1547}
1548
1549bool AttrBuilder::overlaps(const AttrBuilder &B) const {
1550 // First check if any of the target independent attributes overlap.
1551 if ((Attrs & B.Attrs).any())
1552 return true;
1553
1554 // Then check if any target dependent ones do.
Sean Silva9011aca2017-02-22 06:34:04 +00001555 for (const auto &I : td_attrs())
Pete Cooperd2a44612015-05-06 23:19:43 +00001556 if (B.contains(I.first))
1557 return true;
1558
1559 return false;
1560}
1561
Bill Wendling4b001442013-02-06 01:33:42 +00001562bool AttrBuilder::contains(StringRef A) const {
1563 return TargetDepAttrs.find(A) != TargetDepAttrs.end();
1564}
1565
Bill Wendling50d27842012-10-15 20:35:56 +00001566bool AttrBuilder::hasAttributes() const {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001567 return !Attrs.none() || !TargetDepAttrs.empty();
Bill Wendlingc6daefa2012-10-08 23:27:46 +00001568}
Bill Wendling9ac69f92013-01-04 20:54:35 +00001569
Reid Klecknerb5180542017-03-21 16:57:19 +00001570bool AttrBuilder::hasAttributes(AttributeList A, uint64_t Index) const {
Bill Wendling211316c2013-04-18 20:17:28 +00001571 unsigned Slot = ~0U;
Bill Wendling9ca01da2013-02-02 00:42:06 +00001572 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
1573 if (A.getSlotIndex(I) == Index) {
Bill Wendling211316c2013-04-18 20:17:28 +00001574 Slot = I;
Bill Wendling9ca01da2013-02-02 00:42:06 +00001575 break;
1576 }
1577
Bill Wendling211316c2013-04-18 20:17:28 +00001578 assert(Slot != ~0U && "Couldn't find the index!");
Bill Wendling9ca01da2013-02-02 00:42:06 +00001579
Reid Klecknerb5180542017-03-21 16:57:19 +00001580 for (AttributeList::iterator I = A.begin(Slot), E = A.end(Slot); I != E;
1581 ++I) {
Bill Wendling7cde51d2013-02-12 07:56:49 +00001582 Attribute Attr = *I;
Hal Finkele15442c2014-07-18 06:51:55 +00001583 if (Attr.isEnumAttribute() || Attr.isIntAttribute()) {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001584 if (Attrs[I->getKindAsEnum()])
Bill Wendling7cde51d2013-02-12 07:56:49 +00001585 return true;
1586 } else {
1587 assert(Attr.isStringAttribute() && "Invalid attribute kind!");
1588 return TargetDepAttrs.find(Attr.getKindAsString())!=TargetDepAttrs.end();
1589 }
1590 }
Bill Wendling9ca01da2013-02-02 00:42:06 +00001591
1592 return false;
Bill Wendling70f39172012-10-09 00:01:21 +00001593}
Bill Wendling9ac69f92013-01-04 20:54:35 +00001594
Bill Wendling50d27842012-10-15 20:35:56 +00001595bool AttrBuilder::hasAlignmentAttr() const {
Bill Wendlingcd330342013-01-04 23:27:34 +00001596 return Alignment != 0;
Bill Wendlingc6daefa2012-10-08 23:27:46 +00001597}
1598
Bill Wendlingd509a662013-01-29 00:34:06 +00001599bool AttrBuilder::operator==(const AttrBuilder &B) {
Benjamin Kramer45e7d532013-02-16 19:13:18 +00001600 if (Attrs != B.Attrs)
1601 return false;
Bill Wendling4b001442013-02-06 01:33:42 +00001602
1603 for (td_const_iterator I = TargetDepAttrs.begin(),
1604 E = TargetDepAttrs.end(); I != E; ++I)
1605 if (B.TargetDepAttrs.find(I->first) == B.TargetDepAttrs.end())
1606 return false;
1607
Hal Finkelb0407ba2014-07-18 15:51:28 +00001608 return Alignment == B.Alignment && StackAlignment == B.StackAlignment &&
1609 DerefBytes == B.DerefBytes;
Bill Wendlingd509a662013-01-29 00:34:06 +00001610}
1611
Bill Wendling57625a42013-01-25 23:09:36 +00001612//===----------------------------------------------------------------------===//
1613// AttributeFuncs Function Defintions
1614//===----------------------------------------------------------------------===//
1615
Bill Wendlingc79cdff2013-02-01 01:04:27 +00001616/// \brief Which attributes cannot be applied to a type.
Craig Toppere3dcce92015-08-01 22:20:21 +00001617AttrBuilder AttributeFuncs::typeIncompatible(Type *Ty) {
Bill Wendling57625a42013-01-25 23:09:36 +00001618 AttrBuilder Incompatible;
1619
1620 if (!Ty->isIntegerTy())
1621 // Attribute that only apply to integers.
1622 Incompatible.addAttribute(Attribute::SExt)
1623 .addAttribute(Attribute::ZExt);
1624
1625 if (!Ty->isPointerTy())
1626 // Attribute that only apply to pointers.
1627 Incompatible.addAttribute(Attribute::ByVal)
1628 .addAttribute(Attribute::Nest)
1629 .addAttribute(Attribute::NoAlias)
1630 .addAttribute(Attribute::NoCapture)
Nick Lewyckyd52b1522014-05-20 01:23:40 +00001631 .addAttribute(Attribute::NonNull)
Hal Finkelb0407ba2014-07-18 15:51:28 +00001632 .addDereferenceableAttr(1) // the int here is ignored
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001633 .addDereferenceableOrNullAttr(1) // the int here is ignored
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001634 .addAttribute(Attribute::ReadNone)
1635 .addAttribute(Attribute::ReadOnly)
Reid Klecknera534a382013-12-19 02:14:12 +00001636 .addAttribute(Attribute::StructRet)
1637 .addAttribute(Attribute::InAlloca);
Bill Wendling57625a42013-01-25 23:09:36 +00001638
Pete Cooper2777d8872015-05-06 23:19:56 +00001639 return Incompatible;
Bill Wendling57625a42013-01-25 23:09:36 +00001640}
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001641
1642template<typename AttrClass>
1643static bool isEqual(const Function &Caller, const Function &Callee) {
1644 return Caller.getFnAttribute(AttrClass::getKind()) ==
1645 Callee.getFnAttribute(AttrClass::getKind());
1646}
1647
1648/// \brief Compute the logical AND of the attributes of the caller and the
1649/// callee.
1650///
1651/// This function sets the caller's attribute to false if the callee's attribute
1652/// is false.
1653template<typename AttrClass>
1654static void setAND(Function &Caller, const Function &Callee) {
1655 if (AttrClass::isSet(Caller, AttrClass::getKind()) &&
1656 !AttrClass::isSet(Callee, AttrClass::getKind()))
1657 AttrClass::set(Caller, AttrClass::getKind(), false);
1658}
1659
1660/// \brief Compute the logical OR of the attributes of the caller and the
1661/// callee.
1662///
1663/// This function sets the caller's attribute to true if the callee's attribute
1664/// is true.
1665template<typename AttrClass>
1666static void setOR(Function &Caller, const Function &Callee) {
1667 if (!AttrClass::isSet(Caller, AttrClass::getKind()) &&
1668 AttrClass::isSet(Callee, AttrClass::getKind()))
1669 AttrClass::set(Caller, AttrClass::getKind(), true);
1670}
1671
1672/// \brief If the inlined function had a higher stack protection level than the
1673/// calling function, then bump up the caller's stack protection level.
1674static void adjustCallerSSPLevel(Function &Caller, const Function &Callee) {
1675 // If upgrading the SSP attribute, clear out the old SSP Attributes first.
1676 // Having multiple SSP attributes doesn't actually hurt, but it adds useless
1677 // clutter to the IR.
1678 AttrBuilder B;
1679 B.addAttribute(Attribute::StackProtect)
1680 .addAttribute(Attribute::StackProtectStrong)
1681 .addAttribute(Attribute::StackProtectReq);
Reid Klecknerb5180542017-03-21 16:57:19 +00001682 AttributeList OldSSPAttr =
1683 AttributeList::get(Caller.getContext(), AttributeList::FunctionIndex, B);
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001684
Evgeniy Stepanovf17120a2016-04-11 22:27:48 +00001685 if (Callee.hasFnAttribute(Attribute::StackProtectReq)) {
Reid Klecknerb5180542017-03-21 16:57:19 +00001686 Caller.removeAttributes(AttributeList::FunctionIndex, OldSSPAttr);
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001687 Caller.addFnAttr(Attribute::StackProtectReq);
1688 } else if (Callee.hasFnAttribute(Attribute::StackProtectStrong) &&
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001689 !Caller.hasFnAttribute(Attribute::StackProtectReq)) {
Reid Klecknerb5180542017-03-21 16:57:19 +00001690 Caller.removeAttributes(AttributeList::FunctionIndex, OldSSPAttr);
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001691 Caller.addFnAttr(Attribute::StackProtectStrong);
1692 } else if (Callee.hasFnAttribute(Attribute::StackProtect) &&
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001693 !Caller.hasFnAttribute(Attribute::StackProtectReq) &&
1694 !Caller.hasFnAttribute(Attribute::StackProtectStrong))
1695 Caller.addFnAttr(Attribute::StackProtect);
1696}
1697
1698#define GET_ATTR_COMPAT_FUNC
1699#include "AttributesCompatFunc.inc"
1700
1701bool AttributeFuncs::areInlineCompatible(const Function &Caller,
1702 const Function &Callee) {
1703 return hasCompatibleFnAttrs(Caller, Callee);
1704}
1705
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001706void AttributeFuncs::mergeAttributesForInlining(Function &Caller,
1707 const Function &Callee) {
1708 mergeFnAttrs(Caller, Callee);
1709}