blob: 701fd23842b2e54545f193bba66ce1a6a6fbef61 [file] [log] [blame]
Bill Wendlingec454542013-01-28 21:55:20 +00001//===-- Attributes.cpp - Implement AttributesList -------------------------===//
Chris Lattner3e13b8c2008-01-02 23:42:30 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Bill Wendlingec454542013-01-28 21:55:20 +000010// \file
11// \brief This file implements the Attribute, AttributeImpl, AttrBuilder,
Bill Wendling6848e382012-12-19 22:42:22 +000012// AttributeSetImpl, and AttributeSet classes.
Chris Lattner3e13b8c2008-01-02 23:42:30 +000013//
14//===----------------------------------------------------------------------===//
15
Chandler Carruth9fb823b2013-01-02 11:36:10 +000016#include "llvm/IR/Attributes.h"
Akira Hatanaka1cb242e2015-12-22 23:57:37 +000017#include "llvm/IR/Function.h"
Bill Wendling4607f4b2012-12-20 01:36:59 +000018#include "AttributeImpl.h"
Bill Wendlinge38b8042012-09-26 21:07:29 +000019#include "LLVMContextImpl.h"
Benjamin Kramer502b9e12014-04-12 16:15:53 +000020#include "llvm/ADT/STLExtras.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000021#include "llvm/ADT/StringExtras.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000022#include "llvm/IR/Type.h"
Benjamin Kramer17388a62014-03-03 18:02:34 +000023#include "llvm/Support/Atomic.h"
David Greenef7014732010-01-05 01:29:58 +000024#include "llvm/Support/Debug.h"
Chris Lattner3e13b8c2008-01-02 23:42:30 +000025#include "llvm/Support/ManagedStatic.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000026#include "llvm/Support/Mutex.h"
Benjamin Kramer1a25d732009-08-23 11:37:21 +000027#include "llvm/Support/raw_ostream.h"
Bill Wendlingd2e493b2013-01-24 00:06:56 +000028#include <algorithm>
Chris Lattner3e13b8c2008-01-02 23:42:30 +000029using namespace llvm;
30
Chris Lattner8a923e72008-03-12 17:45:29 +000031//===----------------------------------------------------------------------===//
Bill Wendling7707c5a2013-01-29 00:48:16 +000032// Attribute Construction Methods
Chris Lattner8a923e72008-03-12 17:45:29 +000033//===----------------------------------------------------------------------===//
Chris Lattnerd0e1f102008-01-03 00:10:22 +000034
Matt Arsenaulte1cef6e2014-09-03 23:24:31 +000035Attribute Attribute::get(LLVMContext &Context, Attribute::AttrKind Kind,
36 uint64_t Val) {
37 LLVMContextImpl *pImpl = Context.pImpl;
38 FoldingSetNodeID ID;
39 ID.AddInteger(Kind);
Matt Arsenaultfb4308e2014-09-03 23:38:05 +000040 if (Val) ID.AddInteger(Val);
Matt Arsenaulte1cef6e2014-09-03 23:24:31 +000041
42 void *InsertPoint;
43 AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
44
45 if (!PA) {
46 // If we didn't find any existing attributes of the same shape then create a
47 // new one and insert it.
Matt Arsenaultfb4308e2014-09-03 23:38:05 +000048 if (!Val)
49 PA = new EnumAttributeImpl(Kind);
50 else
51 PA = new IntAttributeImpl(Kind, Val);
Bill Wendling3f12ac22013-02-05 22:37:24 +000052 pImpl->AttrsSet.InsertNode(PA, InsertPoint);
53 }
54
55 // Return the Attribute that we found or created.
56 return Attribute(PA);
57}
58
59Attribute Attribute::get(LLVMContext &Context, StringRef Kind, StringRef Val) {
60 LLVMContextImpl *pImpl = Context.pImpl;
61 FoldingSetNodeID ID;
62 ID.AddString(Kind);
63 if (!Val.empty()) ID.AddString(Val);
Bill Wendling73ea2de2012-10-08 21:47:17 +000064
65 void *InsertPoint;
Bill Wendling4607f4b2012-12-20 01:36:59 +000066 AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
Bill Wendling73ea2de2012-10-08 21:47:17 +000067
68 if (!PA) {
69 // If we didn't find any existing attributes of the same shape then create a
70 // new one and insert it.
Benjamin Kramer741146b2013-07-11 12:13:16 +000071 PA = new StringAttributeImpl(Kind, Val);
Bill Wendling73ea2de2012-10-08 21:47:17 +000072 pImpl->AttrsSet.InsertNode(PA, InsertPoint);
73 }
74
Bill Wendlingb9c5b1a2013-02-05 08:09:32 +000075 // Return the Attribute that we found or created.
Bill Wendling3d7b0b82012-12-19 07:18:57 +000076 return Attribute(PA);
Bill Wendling73ea2de2012-10-08 21:47:17 +000077}
78
Bill Wendling4bbe9db2013-01-27 22:43:04 +000079Attribute Attribute::getWithAlignment(LLVMContext &Context, uint64_t Align) {
Bill Wendling1c7cc8a2013-01-31 23:16:25 +000080 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
81 assert(Align <= 0x40000000 && "Alignment too large.");
Bill Wendling3f12ac22013-02-05 22:37:24 +000082 return get(Context, Alignment, Align);
Bill Wendling4bbe9db2013-01-27 22:43:04 +000083}
84
85Attribute Attribute::getWithStackAlignment(LLVMContext &Context,
86 uint64_t Align) {
Bill Wendling1c7cc8a2013-01-31 23:16:25 +000087 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
88 assert(Align <= 0x100 && "Alignment too large.");
Bill Wendling3f12ac22013-02-05 22:37:24 +000089 return get(Context, StackAlignment, Align);
Bill Wendling4bbe9db2013-01-27 22:43:04 +000090}
91
Hal Finkelb0407ba2014-07-18 15:51:28 +000092Attribute Attribute::getWithDereferenceableBytes(LLVMContext &Context,
93 uint64_t Bytes) {
94 assert(Bytes && "Bytes must be non-zero.");
95 return get(Context, Dereferenceable, Bytes);
96}
97
Sanjoy Das31ea6d12015-04-16 20:29:50 +000098Attribute Attribute::getWithDereferenceableOrNullBytes(LLVMContext &Context,
99 uint64_t Bytes) {
100 assert(Bytes && "Bytes must be non-zero.");
101 return get(Context, DereferenceableOrNull, Bytes);
102}
103
Bill Wendling7707c5a2013-01-29 00:48:16 +0000104//===----------------------------------------------------------------------===//
105// Attribute Accessor Methods
106//===----------------------------------------------------------------------===//
107
Bill Wendling3f12ac22013-02-05 22:37:24 +0000108bool Attribute::isEnumAttribute() const {
109 return pImpl && pImpl->isEnumAttribute();
110}
111
Hal Finkele15442c2014-07-18 06:51:55 +0000112bool Attribute::isIntAttribute() const {
113 return pImpl && pImpl->isIntAttribute();
Bill Wendling3f12ac22013-02-05 22:37:24 +0000114}
115
116bool Attribute::isStringAttribute() const {
117 return pImpl && pImpl->isStringAttribute();
118}
119
120Attribute::AttrKind Attribute::getKindAsEnum() const {
Bill Wendling440e9d82013-07-25 00:34:29 +0000121 if (!pImpl) return None;
Hal Finkele15442c2014-07-18 06:51:55 +0000122 assert((isEnumAttribute() || isIntAttribute()) &&
Bill Wendling3f12ac22013-02-05 22:37:24 +0000123 "Invalid attribute type to get the kind as an enum!");
George Burgess IV500d3032015-12-16 05:21:02 +0000124 return pImpl->getKindAsEnum();
Bill Wendling3f12ac22013-02-05 22:37:24 +0000125}
126
127uint64_t Attribute::getValueAsInt() const {
Bill Wendling440e9d82013-07-25 00:34:29 +0000128 if (!pImpl) return 0;
Hal Finkele15442c2014-07-18 06:51:55 +0000129 assert(isIntAttribute() &&
130 "Expected the attribute to be an integer attribute!");
George Burgess IV500d3032015-12-16 05:21:02 +0000131 return pImpl->getValueAsInt();
Bill Wendling3f12ac22013-02-05 22:37:24 +0000132}
133
134StringRef Attribute::getKindAsString() const {
Bill Wendling440e9d82013-07-25 00:34:29 +0000135 if (!pImpl) return StringRef();
Bill Wendling3f12ac22013-02-05 22:37:24 +0000136 assert(isStringAttribute() &&
137 "Invalid attribute type to get the kind as a string!");
George Burgess IV500d3032015-12-16 05:21:02 +0000138 return pImpl->getKindAsString();
Bill Wendling3f12ac22013-02-05 22:37:24 +0000139}
140
141StringRef Attribute::getValueAsString() const {
Bill Wendling440e9d82013-07-25 00:34:29 +0000142 if (!pImpl) return StringRef();
Bill Wendling3f12ac22013-02-05 22:37:24 +0000143 assert(isStringAttribute() &&
144 "Invalid attribute type to get the value as a string!");
George Burgess IV500d3032015-12-16 05:21:02 +0000145 return pImpl->getValueAsString();
Bill Wendling3f12ac22013-02-05 22:37:24 +0000146}
147
Bill Wendlingae89a0f2013-02-05 23:48:36 +0000148bool Attribute::hasAttribute(AttrKind Kind) const {
149 return (pImpl && pImpl->hasAttribute(Kind)) || (!pImpl && Kind == None);
150}
151
152bool Attribute::hasAttribute(StringRef Kind) const {
153 if (!isStringAttribute()) return false;
154 return pImpl && pImpl->hasAttribute(Kind);
Bill Wendling03eefb32013-01-29 20:45:34 +0000155}
156
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000157unsigned Attribute::getAlignment() const {
Bill Wendlingc79cdff2013-02-01 01:04:27 +0000158 assert(hasAttribute(Attribute::Alignment) &&
159 "Trying to get alignment from non-alignment attribute!");
Bill Wendling3f12ac22013-02-05 22:37:24 +0000160 return pImpl->getValueAsInt();
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000161}
162
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000163unsigned Attribute::getStackAlignment() const {
Bill Wendlingc79cdff2013-02-01 01:04:27 +0000164 assert(hasAttribute(Attribute::StackAlignment) &&
165 "Trying to get alignment from non-alignment attribute!");
Bill Wendling3f12ac22013-02-05 22:37:24 +0000166 return pImpl->getValueAsInt();
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000167}
168
Hal Finkelb0407ba2014-07-18 15:51:28 +0000169uint64_t Attribute::getDereferenceableBytes() const {
170 assert(hasAttribute(Attribute::Dereferenceable) &&
171 "Trying to get dereferenceable bytes from "
172 "non-dereferenceable attribute!");
173 return pImpl->getValueAsInt();
174}
175
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000176uint64_t Attribute::getDereferenceableOrNullBytes() const {
177 assert(hasAttribute(Attribute::DereferenceableOrNull) &&
178 "Trying to get dereferenceable bytes from "
179 "non-dereferenceable attribute!");
180 return pImpl->getValueAsInt();
181}
182
Bill Wendling829b4782013-02-11 08:43:33 +0000183std::string Attribute::getAsString(bool InAttrGrp) const {
Bill Wendling9c2eba92013-01-31 20:59:05 +0000184 if (!pImpl) return "";
185
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000186 if (hasAttribute(Attribute::SanitizeAddress))
187 return "sanitize_address";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000188 if (hasAttribute(Attribute::AlwaysInline))
189 return "alwaysinline";
Igor Laevsky39d662f2015-07-11 10:30:36 +0000190 if (hasAttribute(Attribute::ArgMemOnly))
191 return "argmemonly";
Michael Gottesman41748d72013-06-27 00:25:01 +0000192 if (hasAttribute(Attribute::Builtin))
193 return "builtin";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000194 if (hasAttribute(Attribute::ByVal))
195 return "byval";
Owen Anderson85fa7d52015-05-26 23:48:40 +0000196 if (hasAttribute(Attribute::Convergent))
197 return "convergent";
Manman Ren9bfd0d02016-04-01 21:41:15 +0000198 if (hasAttribute(Attribute::SwiftError))
199 return "swifterror";
Manman Renf46262e2016-03-29 17:37:21 +0000200 if (hasAttribute(Attribute::SwiftSelf))
201 return "swiftself";
Vaivaswatha Nagarajfb3f4902015-12-16 16:16:19 +0000202 if (hasAttribute(Attribute::InaccessibleMemOnly))
203 return "inaccessiblememonly";
204 if (hasAttribute(Attribute::InaccessibleMemOrArgMemOnly))
205 return "inaccessiblemem_or_argmemonly";
Reid Klecknera534a382013-12-19 02:14:12 +0000206 if (hasAttribute(Attribute::InAlloca))
207 return "inalloca";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000208 if (hasAttribute(Attribute::InlineHint))
209 return "inlinehint";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000210 if (hasAttribute(Attribute::InReg))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000211 return "inreg";
Tom Roeder44cb65f2014-06-05 19:29:43 +0000212 if (hasAttribute(Attribute::JumpTable))
213 return "jumptable";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000214 if (hasAttribute(Attribute::MinSize))
215 return "minsize";
216 if (hasAttribute(Attribute::Naked))
217 return "naked";
218 if (hasAttribute(Attribute::Nest))
219 return "nest";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000220 if (hasAttribute(Attribute::NoAlias))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000221 return "noalias";
Bill Wendling09bd1f72013-02-22 00:12:35 +0000222 if (hasAttribute(Attribute::NoBuiltin))
223 return "nobuiltin";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000224 if (hasAttribute(Attribute::NoCapture))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000225 return "nocapture";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000226 if (hasAttribute(Attribute::NoDuplicate))
227 return "noduplicate";
228 if (hasAttribute(Attribute::NoImplicitFloat))
229 return "noimplicitfloat";
230 if (hasAttribute(Attribute::NoInline))
231 return "noinline";
232 if (hasAttribute(Attribute::NonLazyBind))
233 return "nonlazybind";
Nick Lewyckyd52b1522014-05-20 01:23:40 +0000234 if (hasAttribute(Attribute::NonNull))
235 return "nonnull";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000236 if (hasAttribute(Attribute::NoRedZone))
237 return "noredzone";
238 if (hasAttribute(Attribute::NoReturn))
239 return "noreturn";
James Molloye6f87ca2015-11-06 10:32:53 +0000240 if (hasAttribute(Attribute::NoRecurse))
241 return "norecurse";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000242 if (hasAttribute(Attribute::NoUnwind))
243 return "nounwind";
Andrea Di Biagio377496b2013-08-23 11:53:55 +0000244 if (hasAttribute(Attribute::OptimizeNone))
245 return "optnone";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000246 if (hasAttribute(Attribute::OptimizeForSize))
247 return "optsize";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000248 if (hasAttribute(Attribute::ReadNone))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000249 return "readnone";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000250 if (hasAttribute(Attribute::ReadOnly))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000251 return "readonly";
Stephen Linb8bd2322013-04-20 05:14:40 +0000252 if (hasAttribute(Attribute::Returned))
253 return "returned";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000254 if (hasAttribute(Attribute::ReturnsTwice))
255 return "returns_twice";
256 if (hasAttribute(Attribute::SExt))
257 return "signext";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000258 if (hasAttribute(Attribute::StackProtect))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000259 return "ssp";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000260 if (hasAttribute(Attribute::StackProtectReq))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000261 return "sspreq";
Bill Wendlingd154e2832013-01-23 06:41:41 +0000262 if (hasAttribute(Attribute::StackProtectStrong))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000263 return "sspstrong";
Peter Collingbourne82437bf2015-06-15 21:07:11 +0000264 if (hasAttribute(Attribute::SafeStack))
265 return "safestack";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000266 if (hasAttribute(Attribute::StructRet))
267 return "sret";
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000268 if (hasAttribute(Attribute::SanitizeThread))
269 return "sanitize_thread";
270 if (hasAttribute(Attribute::SanitizeMemory))
271 return "sanitize_memory";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000272 if (hasAttribute(Attribute::UWTable))
273 return "uwtable";
274 if (hasAttribute(Attribute::ZExt))
275 return "zeroext";
Diego Novilloc6399532013-05-24 12:26:52 +0000276 if (hasAttribute(Attribute::Cold))
277 return "cold";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000278
279 // FIXME: These should be output like this:
280 //
281 // align=4
282 // alignstack=8
283 //
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000284 if (hasAttribute(Attribute::Alignment)) {
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000285 std::string Result;
Bill Wendling829b4782013-02-11 08:43:33 +0000286 Result += "align";
287 Result += (InAttrGrp) ? "=" : " ";
Bill Wendling3f12ac22013-02-05 22:37:24 +0000288 Result += utostr(getValueAsInt());
289 return Result;
290 }
Bill Wendling829b4782013-02-11 08:43:33 +0000291
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000292 auto AttrWithBytesToString = [&](const char *Name) {
Bill Wendling3f12ac22013-02-05 22:37:24 +0000293 std::string Result;
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000294 Result += Name;
Bill Wendling829b4782013-02-11 08:43:33 +0000295 if (InAttrGrp) {
296 Result += "=";
297 Result += utostr(getValueAsInt());
298 } else {
299 Result += "(";
300 Result += utostr(getValueAsInt());
301 Result += ")";
302 }
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000303 return Result;
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000304 };
Bill Wendling9c2eba92013-01-31 20:59:05 +0000305
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000306 if (hasAttribute(Attribute::StackAlignment))
307 return AttrWithBytesToString("alignstack");
308
309 if (hasAttribute(Attribute::Dereferenceable))
310 return AttrWithBytesToString("dereferenceable");
311
312 if (hasAttribute(Attribute::DereferenceableOrNull))
313 return AttrWithBytesToString("dereferenceable_or_null");
Hal Finkelb0407ba2014-07-18 15:51:28 +0000314
Bill Wendling9c2eba92013-01-31 20:59:05 +0000315 // Convert target-dependent attributes to strings of the form:
316 //
317 // "kind"
318 // "kind" = "value"
Bill Wendling9c2eba92013-01-31 20:59:05 +0000319 //
Bill Wendling3f12ac22013-02-05 22:37:24 +0000320 if (isStringAttribute()) {
Bill Wendling9c2eba92013-01-31 20:59:05 +0000321 std::string Result;
Yaron Keren075759a2015-03-30 15:42:36 +0000322 Result += (Twine('"') + getKindAsString() + Twine('"')).str();
Bill Wendling9c2eba92013-01-31 20:59:05 +0000323
Bill Wendling3f12ac22013-02-05 22:37:24 +0000324 StringRef Val = pImpl->getValueAsString();
325 if (Val.empty()) return Result;
Bill Wendling7a33f772013-02-01 22:32:30 +0000326
Yaron Keren075759a2015-03-30 15:42:36 +0000327 Result += ("=\"" + Val + Twine('"')).str();
Bill Wendlingc79cdff2013-02-01 01:04:27 +0000328 return Result;
Bill Wendling9c2eba92013-01-31 20:59:05 +0000329 }
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000330
331 llvm_unreachable("Unknown attribute");
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000332}
333
Bill Wendlingd509a662013-01-29 00:34:06 +0000334bool Attribute::operator<(Attribute A) const {
335 if (!pImpl && !A.pImpl) return false;
336 if (!pImpl) return true;
337 if (!A.pImpl) return false;
338 return *pImpl < *A.pImpl;
339}
340
Bill Wendlingd509a662013-01-29 00:34:06 +0000341//===----------------------------------------------------------------------===//
342// AttributeImpl Definition
343//===----------------------------------------------------------------------===//
344
Eric Christopher0eaa5412014-07-02 22:05:40 +0000345// Pin the vtables to this file.
Alexey Samsonov49109a22013-11-18 09:31:53 +0000346AttributeImpl::~AttributeImpl() {}
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +0000347void EnumAttributeImpl::anchor() {}
Hal Finkele15442c2014-07-18 06:51:55 +0000348void IntAttributeImpl::anchor() {}
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +0000349void StringAttributeImpl::anchor() {}
Alexey Samsonov49109a22013-11-18 09:31:53 +0000350
Bill Wendlingd509a662013-01-29 00:34:06 +0000351bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
Bill Wendling3f12ac22013-02-05 22:37:24 +0000352 if (isStringAttribute()) return false;
353 return getKindAsEnum() == A;
Bill Wendlingd509a662013-01-29 00:34:06 +0000354}
355
Bill Wendling3f12ac22013-02-05 22:37:24 +0000356bool AttributeImpl::hasAttribute(StringRef Kind) const {
357 if (!isStringAttribute()) return false;
358 return getKindAsString() == Kind;
Bill Wendlingd509a662013-01-29 00:34:06 +0000359}
360
Bill Wendling3f12ac22013-02-05 22:37:24 +0000361Attribute::AttrKind AttributeImpl::getKindAsEnum() const {
Hal Finkele15442c2014-07-18 06:51:55 +0000362 assert(isEnumAttribute() || isIntAttribute());
Benjamin Kramer741146b2013-07-11 12:13:16 +0000363 return static_cast<const EnumAttributeImpl *>(this)->getEnumKind();
Bill Wendlingd509a662013-01-29 00:34:06 +0000364}
365
Bill Wendling3f12ac22013-02-05 22:37:24 +0000366uint64_t AttributeImpl::getValueAsInt() const {
Hal Finkele15442c2014-07-18 06:51:55 +0000367 assert(isIntAttribute());
368 return static_cast<const IntAttributeImpl *>(this)->getValue();
Bill Wendlingd509a662013-01-29 00:34:06 +0000369}
370
Bill Wendling3f12ac22013-02-05 22:37:24 +0000371StringRef AttributeImpl::getKindAsString() const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000372 assert(isStringAttribute());
373 return static_cast<const StringAttributeImpl *>(this)->getStringKind();
Bill Wendlingd509a662013-01-29 00:34:06 +0000374}
375
Bill Wendling3f12ac22013-02-05 22:37:24 +0000376StringRef AttributeImpl::getValueAsString() const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000377 assert(isStringAttribute());
378 return static_cast<const StringAttributeImpl *>(this)->getStringValue();
Bill Wendlingd509a662013-01-29 00:34:06 +0000379}
380
381bool AttributeImpl::operator<(const AttributeImpl &AI) const {
Bill Wendlingc79cdff2013-02-01 01:04:27 +0000382 // This sorts the attributes with Attribute::AttrKinds coming first (sorted
383 // relative to their enum value) and then strings.
Bill Wendling26b95752013-02-15 05:25:26 +0000384 if (isEnumAttribute()) {
385 if (AI.isEnumAttribute()) return getKindAsEnum() < AI.getKindAsEnum();
Hal Finkele15442c2014-07-18 06:51:55 +0000386 if (AI.isIntAttribute()) return true;
Bill Wendling26b95752013-02-15 05:25:26 +0000387 if (AI.isStringAttribute()) return true;
388 }
Bill Wendlingc79cdff2013-02-01 01:04:27 +0000389
Hal Finkele15442c2014-07-18 06:51:55 +0000390 if (isIntAttribute()) {
Bill Wendling26b95752013-02-15 05:25:26 +0000391 if (AI.isEnumAttribute()) return false;
Reid Kleckner7de67612016-04-04 23:06:05 +0000392 if (AI.isIntAttribute()) {
393 if (getKindAsEnum() == AI.getKindAsEnum())
394 return getValueAsInt() < AI.getValueAsInt();
395 return getKindAsEnum() < AI.getKindAsEnum();
396 }
Bill Wendling26b95752013-02-15 05:25:26 +0000397 if (AI.isStringAttribute()) return true;
Bill Wendling3f12ac22013-02-05 22:37:24 +0000398 }
Bill Wendlingd509a662013-01-29 00:34:06 +0000399
Bill Wendling26b95752013-02-15 05:25:26 +0000400 if (AI.isEnumAttribute()) return false;
Hal Finkele15442c2014-07-18 06:51:55 +0000401 if (AI.isIntAttribute()) return false;
Bill Wendling26b95752013-02-15 05:25:26 +0000402 if (getKindAsString() == AI.getKindAsString())
403 return getValueAsString() < AI.getValueAsString();
404 return getKindAsString() < AI.getKindAsString();
Bill Wendlingd509a662013-01-29 00:34:06 +0000405}
406
Bill Wendlingd509a662013-01-29 00:34:06 +0000407uint64_t AttributeImpl::getAttrMask(Attribute::AttrKind Val) {
408 // FIXME: Remove this.
409 switch (Val) {
410 case Attribute::EndAttrKinds:
Bill Wendlingd509a662013-01-29 00:34:06 +0000411 llvm_unreachable("Synthetic enumerators which should never get here");
412
413 case Attribute::None: return 0;
414 case Attribute::ZExt: return 1 << 0;
415 case Attribute::SExt: return 1 << 1;
416 case Attribute::NoReturn: return 1 << 2;
417 case Attribute::InReg: return 1 << 3;
418 case Attribute::StructRet: return 1 << 4;
419 case Attribute::NoUnwind: return 1 << 5;
420 case Attribute::NoAlias: return 1 << 6;
421 case Attribute::ByVal: return 1 << 7;
422 case Attribute::Nest: return 1 << 8;
423 case Attribute::ReadNone: return 1 << 9;
424 case Attribute::ReadOnly: return 1 << 10;
425 case Attribute::NoInline: return 1 << 11;
426 case Attribute::AlwaysInline: return 1 << 12;
427 case Attribute::OptimizeForSize: return 1 << 13;
428 case Attribute::StackProtect: return 1 << 14;
429 case Attribute::StackProtectReq: return 1 << 15;
430 case Attribute::Alignment: return 31 << 16;
431 case Attribute::NoCapture: return 1 << 21;
432 case Attribute::NoRedZone: return 1 << 22;
433 case Attribute::NoImplicitFloat: return 1 << 23;
434 case Attribute::Naked: return 1 << 24;
435 case Attribute::InlineHint: return 1 << 25;
436 case Attribute::StackAlignment: return 7 << 26;
437 case Attribute::ReturnsTwice: return 1 << 29;
438 case Attribute::UWTable: return 1 << 30;
439 case Attribute::NonLazyBind: return 1U << 31;
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000440 case Attribute::SanitizeAddress: return 1ULL << 32;
Bill Wendlingd509a662013-01-29 00:34:06 +0000441 case Attribute::MinSize: return 1ULL << 33;
442 case Attribute::NoDuplicate: return 1ULL << 34;
443 case Attribute::StackProtectStrong: return 1ULL << 35;
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000444 case Attribute::SanitizeThread: return 1ULL << 36;
445 case Attribute::SanitizeMemory: return 1ULL << 37;
Bill Wendling7bfdfe72013-02-22 00:40:12 +0000446 case Attribute::NoBuiltin: return 1ULL << 38;
Stephen Linb8bd2322013-04-20 05:14:40 +0000447 case Attribute::Returned: return 1ULL << 39;
Diego Novilloc6399532013-05-24 12:26:52 +0000448 case Attribute::Cold: return 1ULL << 40;
Michael Gottesman41748d72013-06-27 00:25:01 +0000449 case Attribute::Builtin: return 1ULL << 41;
Andrea Di Biagio377496b2013-08-23 11:53:55 +0000450 case Attribute::OptimizeNone: return 1ULL << 42;
Reid Klecknera534a382013-12-19 02:14:12 +0000451 case Attribute::InAlloca: return 1ULL << 43;
Nick Lewyckyd52b1522014-05-20 01:23:40 +0000452 case Attribute::NonNull: return 1ULL << 44;
Tom Roeder44cb65f2014-06-05 19:29:43 +0000453 case Attribute::JumpTable: return 1ULL << 45;
Owen Anderson85fa7d52015-05-26 23:48:40 +0000454 case Attribute::Convergent: return 1ULL << 46;
Peter Collingbourne82437bf2015-06-15 21:07:11 +0000455 case Attribute::SafeStack: return 1ULL << 47;
James Molloye6f87ca2015-11-06 10:32:53 +0000456 case Attribute::NoRecurse: return 1ULL << 48;
Vaivaswatha Nagarajfb3f4902015-12-16 16:16:19 +0000457 case Attribute::InaccessibleMemOnly: return 1ULL << 49;
458 case Attribute::InaccessibleMemOrArgMemOnly: return 1ULL << 50;
Manman Renf46262e2016-03-29 17:37:21 +0000459 case Attribute::SwiftSelf: return 1ULL << 51;
Manman Ren9bfd0d02016-04-01 21:41:15 +0000460 case Attribute::SwiftError: return 1ULL << 52;
Hal Finkelb0407ba2014-07-18 15:51:28 +0000461 case Attribute::Dereferenceable:
462 llvm_unreachable("dereferenceable attribute not supported in raw format");
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000463 break;
464 case Attribute::DereferenceableOrNull:
465 llvm_unreachable("dereferenceable_or_null attribute not supported in raw "
466 "format");
467 break;
Igor Laevsky39d662f2015-07-11 10:30:36 +0000468 case Attribute::ArgMemOnly:
469 llvm_unreachable("argmemonly attribute not supported in raw format");
470 break;
Bill Wendling25342e12013-02-22 00:50:09 +0000471 }
472 llvm_unreachable("Unsupported attribute type");
Bill Wendlingd509a662013-01-29 00:34:06 +0000473}
474
475//===----------------------------------------------------------------------===//
476// AttributeSetNode Definition
477//===----------------------------------------------------------------------===//
478
479AttributeSetNode *AttributeSetNode::get(LLVMContext &C,
480 ArrayRef<Attribute> Attrs) {
481 if (Attrs.empty())
Craig Topperc6207612014-04-09 06:08:46 +0000482 return nullptr;
Bill Wendlingd509a662013-01-29 00:34:06 +0000483
484 // Otherwise, build a key to look up the existing attributes.
485 LLVMContextImpl *pImpl = C.pImpl;
486 FoldingSetNodeID ID;
487
488 SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end());
Reid Kleckner7de67612016-04-04 23:06:05 +0000489 std::sort(SortedAttrs.begin(), SortedAttrs.end());
Bill Wendlingd509a662013-01-29 00:34:06 +0000490
George Burgess IV500d3032015-12-16 05:21:02 +0000491 for (Attribute Attr : SortedAttrs)
492 Attr.Profile(ID);
Bill Wendlingd509a662013-01-29 00:34:06 +0000493
494 void *InsertPoint;
495 AttributeSetNode *PA =
496 pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint);
497
498 // If we didn't find any existing attributes of the same shape then create a
499 // new one and insert it.
500 if (!PA) {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000501 // Coallocate entries after the AttributeSetNode itself.
James Y Knightaa365b22015-08-05 22:57:34 +0000502 void *Mem = ::operator new(totalSizeToAlloc<Attribute>(SortedAttrs.size()));
Benjamin Kramer741146b2013-07-11 12:13:16 +0000503 PA = new (Mem) AttributeSetNode(SortedAttrs);
Bill Wendlingd509a662013-01-29 00:34:06 +0000504 pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint);
505 }
506
507 // Return the AttributesListNode that we found or created.
508 return PA;
509}
510
Bill Wendlingbce7b972013-02-13 08:42:21 +0000511bool AttributeSetNode::hasAttribute(StringRef Kind) const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000512 for (iterator I = begin(), E = end(); I != E; ++I)
Bill Wendlingbce7b972013-02-13 08:42:21 +0000513 if (I->hasAttribute(Kind))
514 return true;
515 return false;
516}
517
518Attribute AttributeSetNode::getAttribute(Attribute::AttrKind Kind) const {
Matthias Braun31eeb76f2016-01-29 22:25:13 +0000519 if (hasAttribute(Kind)) {
520 for (iterator I = begin(), E = end(); I != E; ++I)
521 if (I->hasAttribute(Kind))
522 return *I;
523 }
Bill Wendlingbce7b972013-02-13 08:42:21 +0000524 return Attribute();
525}
526
527Attribute AttributeSetNode::getAttribute(StringRef Kind) const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000528 for (iterator I = begin(), E = end(); I != E; ++I)
Bill Wendlingbce7b972013-02-13 08:42:21 +0000529 if (I->hasAttribute(Kind))
530 return *I;
531 return Attribute();
532}
533
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000534unsigned AttributeSetNode::getAlignment() const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000535 for (iterator I = begin(), E = end(); I != E; ++I)
NAKAMURA Takumib0944392013-01-31 03:47:28 +0000536 if (I->hasAttribute(Attribute::Alignment))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000537 return I->getAlignment();
538 return 0;
539}
540
541unsigned AttributeSetNode::getStackAlignment() const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000542 for (iterator I = begin(), E = end(); I != E; ++I)
NAKAMURA Takumib0944392013-01-31 03:47:28 +0000543 if (I->hasAttribute(Attribute::StackAlignment))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000544 return I->getStackAlignment();
545 return 0;
546}
547
Hal Finkelb0407ba2014-07-18 15:51:28 +0000548uint64_t AttributeSetNode::getDereferenceableBytes() const {
549 for (iterator I = begin(), E = end(); I != E; ++I)
550 if (I->hasAttribute(Attribute::Dereferenceable))
551 return I->getDereferenceableBytes();
552 return 0;
553}
554
Sanjoy Das06cf33f2015-05-06 17:41:54 +0000555uint64_t AttributeSetNode::getDereferenceableOrNullBytes() const {
556 for (iterator I = begin(), E = end(); I != E; ++I)
557 if (I->hasAttribute(Attribute::DereferenceableOrNull))
558 return I->getDereferenceableOrNullBytes();
559 return 0;
560}
561
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +0000562std::string AttributeSetNode::getAsString(bool InAttrGrp) const {
Benjamin Kramer0baf8f42013-04-19 11:43:21 +0000563 std::string Str;
Benjamin Kramer741146b2013-07-11 12:13:16 +0000564 for (iterator I = begin(), E = end(); I != E; ++I) {
565 if (I != begin())
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +0000566 Str += ' ';
567 Str += I->getAsString(InAttrGrp);
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000568 }
569 return Str;
570}
571
Bill Wendlingd509a662013-01-29 00:34:06 +0000572//===----------------------------------------------------------------------===//
573// AttributeSetImpl Definition
574//===----------------------------------------------------------------------===//
575
Rafael Espindoladd275302013-04-30 16:53:38 +0000576uint64_t AttributeSetImpl::Raw(unsigned Index) const {
Bill Wendlingd509a662013-01-29 00:34:06 +0000577 for (unsigned I = 0, E = getNumAttributes(); I != E; ++I) {
578 if (getSlotIndex(I) != Index) continue;
Benjamin Kramer741146b2013-07-11 12:13:16 +0000579 const AttributeSetNode *ASN = getSlotNode(I);
Bill Wendling91226182013-02-02 00:52:44 +0000580 uint64_t Mask = 0;
Bill Wendlingd509a662013-01-29 00:34:06 +0000581
Benjamin Kramer741146b2013-07-11 12:13:16 +0000582 for (AttributeSetNode::iterator II = ASN->begin(),
Bill Wendling91226182013-02-02 00:52:44 +0000583 IE = ASN->end(); II != IE; ++II) {
584 Attribute Attr = *II;
Bill Wendlingd746e402013-02-10 23:18:05 +0000585
586 // This cannot handle string attributes.
587 if (Attr.isStringAttribute()) continue;
588
Bill Wendling3f12ac22013-02-05 22:37:24 +0000589 Attribute::AttrKind Kind = Attr.getKindAsEnum();
Bill Wendling91226182013-02-02 00:52:44 +0000590
Bill Wendling3f12ac22013-02-05 22:37:24 +0000591 if (Kind == Attribute::Alignment)
Bill Wendling91226182013-02-02 00:52:44 +0000592 Mask |= (Log2_32(ASN->getAlignment()) + 1) << 16;
Bill Wendling3f12ac22013-02-05 22:37:24 +0000593 else if (Kind == Attribute::StackAlignment)
Bill Wendling91226182013-02-02 00:52:44 +0000594 Mask |= (Log2_32(ASN->getStackAlignment()) + 1) << 26;
Hal Finkelb0407ba2014-07-18 15:51:28 +0000595 else if (Kind == Attribute::Dereferenceable)
596 llvm_unreachable("dereferenceable not supported in bit mask");
Bill Wendling91226182013-02-02 00:52:44 +0000597 else
Bill Wendling3f12ac22013-02-05 22:37:24 +0000598 Mask |= AttributeImpl::getAttrMask(Kind);
Bill Wendling91226182013-02-02 00:52:44 +0000599 }
600
601 return Mask;
Bill Wendlingd509a662013-01-29 00:34:06 +0000602 }
603
604 return 0;
605}
606
Yaron Kereneb2a2542016-01-29 20:50:44 +0000607LLVM_DUMP_METHOD void AttributeSetImpl::dump() const {
Peter Collingbourneabca2ec2013-08-02 22:34:30 +0000608 AttributeSet(const_cast<AttributeSetImpl *>(this)).dump();
609}
610
Bill Wendlingd509a662013-01-29 00:34:06 +0000611//===----------------------------------------------------------------------===//
612// AttributeSet Construction and Mutation Methods
613//===----------------------------------------------------------------------===//
614
Bill Wendling60011b82013-01-29 01:43:29 +0000615AttributeSet
616AttributeSet::getImpl(LLVMContext &C,
617 ArrayRef<std::pair<unsigned, AttributeSetNode*> > Attrs) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000618 LLVMContextImpl *pImpl = C.pImpl;
619 FoldingSetNodeID ID;
620 AttributeSetImpl::Profile(ID, Attrs);
621
622 void *InsertPoint;
623 AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
624
625 // If we didn't find any existing attributes of the same shape then
626 // create a new one and insert it.
627 if (!PA) {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000628 // Coallocate entries after the AttributeSetImpl itself.
James Y Knightaa365b22015-08-05 22:57:34 +0000629 void *Mem = ::operator new(
630 AttributeSetImpl::totalSizeToAlloc<IndexAttrPair>(Attrs.size()));
Benjamin Kramer741146b2013-07-11 12:13:16 +0000631 PA = new (Mem) AttributeSetImpl(C, Attrs);
Bill Wendlingd509a662013-01-29 00:34:06 +0000632 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
633 }
634
635 // Return the AttributesList that we found or created.
636 return AttributeSet(PA);
637}
638
639AttributeSet AttributeSet::get(LLVMContext &C,
640 ArrayRef<std::pair<unsigned, Attribute> > Attrs){
641 // If there are no attributes then return a null AttributesList pointer.
642 if (Attrs.empty())
643 return AttributeSet();
644
Craig Toppere30b8ca2016-01-03 19:43:40 +0000645 assert(std::is_sorted(Attrs.begin(), Attrs.end(),
646 [](const std::pair<unsigned, Attribute> &LHS,
647 const std::pair<unsigned, Attribute> &RHS) {
648 return LHS.first < RHS.first;
649 }) && "Misordered Attributes list!");
650 assert(std::none_of(Attrs.begin(), Attrs.end(),
651 [](const std::pair<unsigned, Attribute> &Pair) {
652 return Pair.second.hasAttribute(Attribute::None);
653 }) && "Pointless attribute!");
Bill Wendlingd509a662013-01-29 00:34:06 +0000654
655 // Create a vector if (unsigned, AttributeSetNode*) pairs from the attributes
656 // list.
657 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrPairVec;
658 for (ArrayRef<std::pair<unsigned, Attribute> >::iterator I = Attrs.begin(),
659 E = Attrs.end(); I != E; ) {
660 unsigned Index = I->first;
661 SmallVector<Attribute, 4> AttrVec;
NAKAMURA Takumif05d2f22013-01-29 15:18:16 +0000662 while (I != E && I->first == Index) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000663 AttrVec.push_back(I->second);
664 ++I;
665 }
666
667 AttrPairVec.push_back(std::make_pair(Index,
668 AttributeSetNode::get(C, AttrVec)));
669 }
670
671 return getImpl(C, AttrPairVec);
672}
673
674AttributeSet AttributeSet::get(LLVMContext &C,
675 ArrayRef<std::pair<unsigned,
676 AttributeSetNode*> > Attrs) {
677 // If there are no attributes then return a null AttributesList pointer.
678 if (Attrs.empty())
679 return AttributeSet();
680
681 return getImpl(C, Attrs);
682}
683
David Majnemercf63a792014-05-03 23:00:35 +0000684AttributeSet AttributeSet::get(LLVMContext &C, unsigned Index,
685 const AttrBuilder &B) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000686 if (!B.hasAttributes())
687 return AttributeSet();
Bill Wendlingf7134812013-01-29 01:02:03 +0000688
Bill Wendlingae89a0f2013-02-05 23:48:36 +0000689 // Add target-independent attributes.
Bill Wendlingf7134812013-01-29 01:02:03 +0000690 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
Benjamin Kramer45e7d532013-02-16 19:13:18 +0000691 for (Attribute::AttrKind Kind = Attribute::None;
Benjamin Kramer6f37dac2013-02-16 19:22:28 +0000692 Kind != Attribute::EndAttrKinds; Kind = Attribute::AttrKind(Kind + 1)) {
Benjamin Kramer45e7d532013-02-16 19:13:18 +0000693 if (!B.contains(Kind))
694 continue;
695
George Burgess IV500d3032015-12-16 05:21:02 +0000696 Attribute Attr;
697 switch (Kind) {
698 case Attribute::Alignment:
699 Attr = Attribute::getWithAlignment(C, B.getAlignment());
700 break;
701 case Attribute::StackAlignment:
702 Attr = Attribute::getWithStackAlignment(C, B.getStackAlignment());
703 break;
704 case Attribute::Dereferenceable:
705 Attr = Attribute::getWithDereferenceableBytes(
706 C, B.getDereferenceableBytes());
707 break;
708 case Attribute::DereferenceableOrNull:
709 Attr = Attribute::getWithDereferenceableOrNullBytes(
710 C, B.getDereferenceableOrNullBytes());
711 break;
712 default:
713 Attr = Attribute::get(C, Kind);
714 }
715 Attrs.push_back(std::make_pair(Index, Attr));
Bill Wendlingf7134812013-01-29 01:02:03 +0000716 }
717
Bill Wendlingae89a0f2013-02-05 23:48:36 +0000718 // Add target-dependent (string) attributes.
David Majnemercf63a792014-05-03 23:00:35 +0000719 for (const AttrBuilder::td_type &TDA : B.td_attrs())
720 Attrs.push_back(
721 std::make_pair(Index, Attribute::get(C, TDA.first, TDA.second)));
Bill Wendlingae89a0f2013-02-05 23:48:36 +0000722
Bill Wendlingf7134812013-01-29 01:02:03 +0000723 return get(C, Attrs);
Bill Wendlingd509a662013-01-29 00:34:06 +0000724}
725
Bill Wendling211316c2013-04-18 20:17:28 +0000726AttributeSet AttributeSet::get(LLVMContext &C, unsigned Index,
Bill Wendlingd509a662013-01-29 00:34:06 +0000727 ArrayRef<Attribute::AttrKind> Kind) {
728 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
George Burgess IV500d3032015-12-16 05:21:02 +0000729 for (Attribute::AttrKind K : Kind)
730 Attrs.push_back(std::make_pair(Index, Attribute::get(C, K)));
Bill Wendlingd509a662013-01-29 00:34:06 +0000731 return get(C, Attrs);
732}
733
734AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<AttributeSet> Attrs) {
735 if (Attrs.empty()) return AttributeSet();
Peter Collingbournebd6c7452013-08-02 22:29:40 +0000736 if (Attrs.size() == 1) return Attrs[0];
Bill Wendlingd509a662013-01-29 00:34:06 +0000737
738 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrNodeVec;
Peter Collingbournebd6c7452013-08-02 22:29:40 +0000739 AttributeSetImpl *A0 = Attrs[0].pImpl;
740 if (A0)
741 AttrNodeVec.append(A0->getNode(0), A0->getNode(A0->getNumAttributes()));
742 // Copy all attributes from Attrs into AttrNodeVec while keeping AttrNodeVec
743 // ordered by index. Because we know that each list in Attrs is ordered by
744 // index we only need to merge each successive list in rather than doing a
745 // full sort.
746 for (unsigned I = 1, E = Attrs.size(); I != E; ++I) {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000747 AttributeSetImpl *AS = Attrs[I].pImpl;
748 if (!AS) continue;
Peter Collingbournebd6c7452013-08-02 22:29:40 +0000749 SmallVector<std::pair<unsigned, AttributeSetNode *>, 8>::iterator
750 ANVI = AttrNodeVec.begin(), ANVE;
James Y Knightaa365b22015-08-05 22:57:34 +0000751 for (const IndexAttrPair *AI = AS->getNode(0),
752 *AE = AS->getNode(AS->getNumAttributes());
Peter Collingbournebd6c7452013-08-02 22:29:40 +0000753 AI != AE; ++AI) {
754 ANVE = AttrNodeVec.end();
755 while (ANVI != ANVE && ANVI->first <= AI->first)
756 ++ANVI;
757 ANVI = AttrNodeVec.insert(ANVI, *AI) + 1;
758 }
Bill Wendlingd509a662013-01-29 00:34:06 +0000759 }
760
761 return getImpl(C, AttrNodeVec);
762}
763
Bill Wendling211316c2013-04-18 20:17:28 +0000764AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
Bill Wendlingd509a662013-01-29 00:34:06 +0000765 Attribute::AttrKind Attr) const {
Bill Wendling211316c2013-04-18 20:17:28 +0000766 if (hasAttribute(Index, Attr)) return *this;
767 return addAttributes(C, Index, AttributeSet::get(C, Index, Attr));
Bill Wendlingd509a662013-01-29 00:34:06 +0000768}
769
Bill Wendling211316c2013-04-18 20:17:28 +0000770AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
Reed Kotler795c7b42013-03-13 20:20:08 +0000771 StringRef Kind) const {
772 llvm::AttrBuilder B;
773 B.addAttribute(Kind);
Bill Wendling211316c2013-04-18 20:17:28 +0000774 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
Reed Kotler795c7b42013-03-13 20:20:08 +0000775}
776
Bill Wendling3b2f6102013-07-25 18:34:24 +0000777AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
778 StringRef Kind, StringRef Value) const {
779 llvm::AttrBuilder B;
780 B.addAttribute(Kind, Value);
781 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
782}
783
Akira Hatanaka237916b2015-12-02 06:58:49 +0000784AttributeSet AttributeSet::addAttribute(LLVMContext &C,
785 ArrayRef<unsigned> Indices,
786 Attribute A) const {
787 unsigned I = 0, E = pImpl ? pImpl->getNumAttributes() : 0;
788 auto IdxI = Indices.begin(), IdxE = Indices.end();
789 SmallVector<AttributeSet, 4> AttrSet;
790
791 while (I != E && IdxI != IdxE) {
792 if (getSlotIndex(I) < *IdxI)
793 AttrSet.emplace_back(getSlotAttributes(I++));
794 else if (getSlotIndex(I) > *IdxI)
795 AttrSet.emplace_back(AttributeSet::get(C, std::make_pair(*IdxI++, A)));
796 else {
797 AttrBuilder B(getSlotAttributes(I), *IdxI);
798 B.addAttribute(A);
799 AttrSet.emplace_back(AttributeSet::get(C, *IdxI, B));
800 ++I;
801 ++IdxI;
802 }
803 }
804
805 while (I != E)
806 AttrSet.emplace_back(getSlotAttributes(I++));
807
808 while (IdxI != IdxE)
809 AttrSet.emplace_back(AttributeSet::get(C, std::make_pair(*IdxI++, A)));
810
811 return get(C, AttrSet);
812}
813
Bill Wendling211316c2013-04-18 20:17:28 +0000814AttributeSet AttributeSet::addAttributes(LLVMContext &C, unsigned Index,
Bill Wendlingd509a662013-01-29 00:34:06 +0000815 AttributeSet Attrs) const {
816 if (!pImpl) return Attrs;
817 if (!Attrs.pImpl) return *this;
818
819#ifndef NDEBUG
820 // FIXME it is not obvious how this should work for alignment. For now, say
821 // we can't change a known alignment.
Bill Wendling211316c2013-04-18 20:17:28 +0000822 unsigned OldAlign = getParamAlignment(Index);
823 unsigned NewAlign = Attrs.getParamAlignment(Index);
Bill Wendlingd509a662013-01-29 00:34:06 +0000824 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
825 "Attempt to change alignment!");
826#endif
827
828 // Add the attribute slots before the one we're trying to add.
829 SmallVector<AttributeSet, 4> AttrSet;
830 uint64_t NumAttrs = pImpl->getNumAttributes();
831 AttributeSet AS;
832 uint64_t LastIndex = 0;
833 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
Bill Wendling211316c2013-04-18 20:17:28 +0000834 if (getSlotIndex(I) >= Index) {
835 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
Bill Wendlingd509a662013-01-29 00:34:06 +0000836 break;
837 }
838 LastIndex = I + 1;
839 AttrSet.push_back(getSlotAttributes(I));
840 }
841
842 // Now add the attribute into the correct slot. There may already be an
843 // AttributeSet there.
Bill Wendling211316c2013-04-18 20:17:28 +0000844 AttrBuilder B(AS, Index);
Bill Wendlingd509a662013-01-29 00:34:06 +0000845
846 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
Bill Wendling211316c2013-04-18 20:17:28 +0000847 if (Attrs.getSlotIndex(I) == Index) {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000848 for (AttributeSetImpl::iterator II = Attrs.pImpl->begin(I),
Bill Wendlingd509a662013-01-29 00:34:06 +0000849 IE = Attrs.pImpl->end(I); II != IE; ++II)
Bill Wendling23804da2013-01-31 23:38:01 +0000850 B.addAttribute(*II);
Bill Wendlingd509a662013-01-29 00:34:06 +0000851 break;
852 }
853
Bill Wendling211316c2013-04-18 20:17:28 +0000854 AttrSet.push_back(AttributeSet::get(C, Index, B));
Bill Wendlingd509a662013-01-29 00:34:06 +0000855
856 // Add the remaining attribute slots.
857 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
858 AttrSet.push_back(getSlotAttributes(I));
859
860 return get(C, AttrSet);
861}
862
Bill Wendling211316c2013-04-18 20:17:28 +0000863AttributeSet AttributeSet::removeAttribute(LLVMContext &C, unsigned Index,
Bill Wendlingd509a662013-01-29 00:34:06 +0000864 Attribute::AttrKind Attr) const {
Bill Wendling211316c2013-04-18 20:17:28 +0000865 if (!hasAttribute(Index, Attr)) return *this;
866 return removeAttributes(C, Index, AttributeSet::get(C, Index, Attr));
Bill Wendlingd509a662013-01-29 00:34:06 +0000867}
868
Bill Wendling211316c2013-04-18 20:17:28 +0000869AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Index,
Bill Wendlingd509a662013-01-29 00:34:06 +0000870 AttributeSet Attrs) const {
871 if (!pImpl) return AttributeSet();
872 if (!Attrs.pImpl) return *this;
873
Pete Cooper67cf9a72015-11-19 05:56:52 +0000874 // FIXME it is not obvious how this should work for alignment.
875 // For now, say we can't pass in alignment, which no current use does.
876 assert(!Attrs.hasAttribute(Index, Attribute::Alignment) &&
877 "Attempt to change alignment!");
878
Bill Wendlingd509a662013-01-29 00:34:06 +0000879 // Add the attribute slots before the one we're trying to add.
880 SmallVector<AttributeSet, 4> AttrSet;
881 uint64_t NumAttrs = pImpl->getNumAttributes();
882 AttributeSet AS;
883 uint64_t LastIndex = 0;
884 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
Bill Wendling211316c2013-04-18 20:17:28 +0000885 if (getSlotIndex(I) >= Index) {
886 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
Bill Wendlingd509a662013-01-29 00:34:06 +0000887 break;
888 }
889 LastIndex = I + 1;
890 AttrSet.push_back(getSlotAttributes(I));
891 }
892
Bill Wendlingd2196752013-01-30 23:07:40 +0000893 // Now remove the attribute from the correct slot. There may already be an
Bill Wendlingd509a662013-01-29 00:34:06 +0000894 // AttributeSet there.
Bill Wendling211316c2013-04-18 20:17:28 +0000895 AttrBuilder B(AS, Index);
Bill Wendlingd509a662013-01-29 00:34:06 +0000896
897 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
Bill Wendling211316c2013-04-18 20:17:28 +0000898 if (Attrs.getSlotIndex(I) == Index) {
899 B.removeAttributes(Attrs.pImpl->getSlotAttributes(I), Index);
Bill Wendlingd509a662013-01-29 00:34:06 +0000900 break;
901 }
902
Bill Wendling211316c2013-04-18 20:17:28 +0000903 AttrSet.push_back(AttributeSet::get(C, Index, B));
Bill Wendlingd509a662013-01-29 00:34:06 +0000904
905 // Add the remaining attribute slots.
906 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
907 AttrSet.push_back(getSlotAttributes(I));
908
909 return get(C, AttrSet);
910}
911
Pete Cooperd2a44612015-05-06 23:19:43 +0000912AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Index,
913 const AttrBuilder &Attrs) const {
914 if (!pImpl) return AttributeSet();
915
916 // FIXME it is not obvious how this should work for alignment.
917 // For now, say we can't pass in alignment, which no current use does.
918 assert(!Attrs.hasAlignmentAttr() && "Attempt to change alignment!");
919
920 // Add the attribute slots before the one we're trying to add.
921 SmallVector<AttributeSet, 4> AttrSet;
922 uint64_t NumAttrs = pImpl->getNumAttributes();
923 AttributeSet AS;
924 uint64_t LastIndex = 0;
925 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
926 if (getSlotIndex(I) >= Index) {
927 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
928 break;
929 }
930 LastIndex = I + 1;
931 AttrSet.push_back(getSlotAttributes(I));
932 }
933
934 // Now remove the attribute from the correct slot. There may already be an
935 // AttributeSet there.
936 AttrBuilder B(AS, Index);
937 B.remove(Attrs);
938
939 AttrSet.push_back(AttributeSet::get(C, Index, B));
940
941 // Add the remaining attribute slots.
942 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
943 AttrSet.push_back(getSlotAttributes(I));
944
945 return get(C, AttrSet);
946}
947
Ramkumar Ramachandra8fcb4982015-02-14 19:37:54 +0000948AttributeSet AttributeSet::addDereferenceableAttr(LLVMContext &C, unsigned Index,
949 uint64_t Bytes) const {
950 llvm::AttrBuilder B;
951 B.addDereferenceableAttr(Bytes);
952 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
953}
954
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000955AttributeSet AttributeSet::addDereferenceableOrNullAttr(LLVMContext &C,
956 unsigned Index,
957 uint64_t Bytes) const {
958 llvm::AttrBuilder B;
959 B.addDereferenceableOrNullAttr(Bytes);
960 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
961}
962
Bill Wendlingd509a662013-01-29 00:34:06 +0000963//===----------------------------------------------------------------------===//
964// AttributeSet Accessor Methods
965//===----------------------------------------------------------------------===//
966
Bill Wendling5d020a32013-02-10 05:00:40 +0000967LLVMContext &AttributeSet::getContext() const {
968 return pImpl->getContext();
969}
970
Bill Wendling211316c2013-04-18 20:17:28 +0000971AttributeSet AttributeSet::getParamAttributes(unsigned Index) const {
972 return pImpl && hasAttributes(Index) ?
Bill Wendlingd509a662013-01-29 00:34:06 +0000973 AttributeSet::get(pImpl->getContext(),
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000974 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendling211316c2013-04-18 20:17:28 +0000975 std::make_pair(Index, getAttributes(Index)))) :
Bill Wendlingd509a662013-01-29 00:34:06 +0000976 AttributeSet();
977}
978
979AttributeSet AttributeSet::getRetAttributes() const {
980 return pImpl && hasAttributes(ReturnIndex) ?
981 AttributeSet::get(pImpl->getContext(),
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000982 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendlingd509a662013-01-29 00:34:06 +0000983 std::make_pair(ReturnIndex,
984 getAttributes(ReturnIndex)))) :
985 AttributeSet();
986}
987
988AttributeSet AttributeSet::getFnAttributes() const {
989 return pImpl && hasAttributes(FunctionIndex) ?
990 AttributeSet::get(pImpl->getContext(),
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000991 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendlingd509a662013-01-29 00:34:06 +0000992 std::make_pair(FunctionIndex,
993 getAttributes(FunctionIndex)))) :
994 AttributeSet();
995}
996
997bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000998 AttributeSetNode *ASN = getAttributes(Index);
George Burgess IV500d3032015-12-16 05:21:02 +0000999 return ASN && ASN->hasAttribute(Kind);
Bill Wendlingd509a662013-01-29 00:34:06 +00001000}
1001
Bill Wendlingbce7b972013-02-13 08:42:21 +00001002bool AttributeSet::hasAttribute(unsigned Index, StringRef Kind) const {
1003 AttributeSetNode *ASN = getAttributes(Index);
George Burgess IV500d3032015-12-16 05:21:02 +00001004 return ASN && ASN->hasAttribute(Kind);
Bill Wendlingbce7b972013-02-13 08:42:21 +00001005}
1006
Bill Wendlingd509a662013-01-29 00:34:06 +00001007bool AttributeSet::hasAttributes(unsigned Index) const {
Bill Wendlingf2955aa2013-01-29 03:20:31 +00001008 AttributeSetNode *ASN = getAttributes(Index);
George Burgess IV500d3032015-12-16 05:21:02 +00001009 return ASN && ASN->hasAttributes();
Bill Wendlingd509a662013-01-29 00:34:06 +00001010}
1011
Matthias Braun33282812016-01-29 22:25:19 +00001012bool AttributeSet::hasFnAttribute(Attribute::AttrKind Kind) const {
1013 return pImpl && pImpl->hasFnAttribute(Kind);
1014}
1015
Bill Wendlingd509a662013-01-29 00:34:06 +00001016bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr) const {
Craig Topperc6207612014-04-09 06:08:46 +00001017 if (!pImpl) return false;
Bill Wendlingd509a662013-01-29 00:34:06 +00001018
1019 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
Benjamin Kramer741146b2013-07-11 12:13:16 +00001020 for (AttributeSetImpl::iterator II = pImpl->begin(I),
Bill Wendlingd509a662013-01-29 00:34:06 +00001021 IE = pImpl->end(I); II != IE; ++II)
NAKAMURA Takumib0944392013-01-31 03:47:28 +00001022 if (II->hasAttribute(Attr))
Bill Wendlingd509a662013-01-29 00:34:06 +00001023 return true;
1024
1025 return false;
1026}
1027
Bill Wendlingbce7b972013-02-13 08:42:21 +00001028Attribute AttributeSet::getAttribute(unsigned Index,
1029 Attribute::AttrKind Kind) const {
1030 AttributeSetNode *ASN = getAttributes(Index);
1031 return ASN ? ASN->getAttribute(Kind) : Attribute();
1032}
1033
1034Attribute AttributeSet::getAttribute(unsigned Index,
1035 StringRef Kind) const {
1036 AttributeSetNode *ASN = getAttributes(Index);
1037 return ASN ? ASN->getAttribute(Kind) : Attribute();
1038}
1039
Bill Wendlingf2955aa2013-01-29 03:20:31 +00001040unsigned AttributeSet::getParamAlignment(unsigned Index) const {
1041 AttributeSetNode *ASN = getAttributes(Index);
1042 return ASN ? ASN->getAlignment() : 0;
Bill Wendlingd509a662013-01-29 00:34:06 +00001043}
1044
1045unsigned AttributeSet::getStackAlignment(unsigned Index) const {
Bill Wendlingf2955aa2013-01-29 03:20:31 +00001046 AttributeSetNode *ASN = getAttributes(Index);
1047 return ASN ? ASN->getStackAlignment() : 0;
Bill Wendlingd509a662013-01-29 00:34:06 +00001048}
1049
Hal Finkelb0407ba2014-07-18 15:51:28 +00001050uint64_t AttributeSet::getDereferenceableBytes(unsigned Index) const {
1051 AttributeSetNode *ASN = getAttributes(Index);
1052 return ASN ? ASN->getDereferenceableBytes() : 0;
1053}
1054
Sanjoy Das06cf33f2015-05-06 17:41:54 +00001055uint64_t AttributeSet::getDereferenceableOrNullBytes(unsigned Index) const {
1056 AttributeSetNode *ASN = getAttributes(Index);
1057 return ASN ? ASN->getDereferenceableOrNullBytes() : 0;
1058}
1059
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +00001060std::string AttributeSet::getAsString(unsigned Index,
Bill Wendling829b4782013-02-11 08:43:33 +00001061 bool InAttrGrp) const {
Bill Wendlingf2955aa2013-01-29 03:20:31 +00001062 AttributeSetNode *ASN = getAttributes(Index);
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +00001063 return ASN ? ASN->getAsString(InAttrGrp) : std::string("");
Bill Wendlingd509a662013-01-29 00:34:06 +00001064}
1065
Bill Wendling211316c2013-04-18 20:17:28 +00001066AttributeSetNode *AttributeSet::getAttributes(unsigned Index) const {
Craig Topperc6207612014-04-09 06:08:46 +00001067 if (!pImpl) return nullptr;
Bill Wendlingd509a662013-01-29 00:34:06 +00001068
Bill Wendlingf2955aa2013-01-29 03:20:31 +00001069 // Loop through to find the attribute node we want.
1070 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
Bill Wendling211316c2013-04-18 20:17:28 +00001071 if (pImpl->getSlotIndex(I) == Index)
Bill Wendlingf2955aa2013-01-29 03:20:31 +00001072 return pImpl->getSlotNode(I);
Bill Wendlingd509a662013-01-29 00:34:06 +00001073
Craig Topperc6207612014-04-09 06:08:46 +00001074 return nullptr;
Bill Wendlingd509a662013-01-29 00:34:06 +00001075}
1076
Bill Wendling211316c2013-04-18 20:17:28 +00001077AttributeSet::iterator AttributeSet::begin(unsigned Slot) const {
Bill Wendlinga9174862013-01-31 23:53:05 +00001078 if (!pImpl)
1079 return ArrayRef<Attribute>().begin();
Bill Wendling211316c2013-04-18 20:17:28 +00001080 return pImpl->begin(Slot);
Bill Wendlinga9174862013-01-31 23:53:05 +00001081}
1082
Bill Wendling211316c2013-04-18 20:17:28 +00001083AttributeSet::iterator AttributeSet::end(unsigned Slot) const {
Bill Wendlinga9174862013-01-31 23:53:05 +00001084 if (!pImpl)
1085 return ArrayRef<Attribute>().end();
Bill Wendling211316c2013-04-18 20:17:28 +00001086 return pImpl->end(Slot);
Bill Wendlinga9174862013-01-31 23:53:05 +00001087}
1088
Bill Wendlingd509a662013-01-29 00:34:06 +00001089//===----------------------------------------------------------------------===//
1090// AttributeSet Introspection Methods
1091//===----------------------------------------------------------------------===//
1092
Bill Wendlingd509a662013-01-29 00:34:06 +00001093unsigned AttributeSet::getNumSlots() const {
1094 return pImpl ? pImpl->getNumAttributes() : 0;
1095}
1096
Rafael Espindoladd275302013-04-30 16:53:38 +00001097unsigned AttributeSet::getSlotIndex(unsigned Slot) const {
Bill Wendlingd509a662013-01-29 00:34:06 +00001098 assert(pImpl && Slot < pImpl->getNumAttributes() &&
1099 "Slot # out of range!");
1100 return pImpl->getSlotIndex(Slot);
1101}
1102
1103AttributeSet AttributeSet::getSlotAttributes(unsigned Slot) const {
1104 assert(pImpl && Slot < pImpl->getNumAttributes() &&
1105 "Slot # out of range!");
1106 return pImpl->getSlotAttributes(Slot);
1107}
1108
1109uint64_t AttributeSet::Raw(unsigned Index) const {
1110 // FIXME: Remove this.
1111 return pImpl ? pImpl->Raw(Index) : 0;
1112}
1113
Yaron Kereneb2a2542016-01-29 20:50:44 +00001114LLVM_DUMP_METHOD void AttributeSet::dump() const {
Bill Wendlingd509a662013-01-29 00:34:06 +00001115 dbgs() << "PAL[\n";
1116
1117 for (unsigned i = 0, e = getNumSlots(); i < e; ++i) {
1118 uint64_t Index = getSlotIndex(i);
1119 dbgs() << " { ";
1120 if (Index == ~0U)
1121 dbgs() << "~0U";
1122 else
1123 dbgs() << Index;
1124 dbgs() << " => " << getAsString(Index) << " }\n";
1125 }
1126
1127 dbgs() << "]\n";
1128}
1129
Bill Wendlingabf3feb2012-10-05 06:44:41 +00001130//===----------------------------------------------------------------------===//
Bill Wendlingcd330342013-01-04 23:27:34 +00001131// AttrBuilder Method Implementations
Bill Wendlingabf3feb2012-10-05 06:44:41 +00001132//===----------------------------------------------------------------------===//
1133
Bill Wendling211316c2013-04-18 20:17:28 +00001134AttrBuilder::AttrBuilder(AttributeSet AS, unsigned Index)
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001135 : Attrs(0), Alignment(0), StackAlignment(0), DerefBytes(0),
1136 DerefOrNullBytes(0) {
Bill Wendling56b0b2a2013-01-27 21:23:46 +00001137 AttributeSetImpl *pImpl = AS.pImpl;
Bill Wendling096f5442013-01-07 08:24:35 +00001138 if (!pImpl) return;
1139
Bill Wendling9eb689c2013-01-28 00:21:34 +00001140 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I) {
Bill Wendling211316c2013-04-18 20:17:28 +00001141 if (pImpl->getSlotIndex(I) != Index) continue;
Bill Wendling096f5442013-01-07 08:24:35 +00001142
Benjamin Kramer741146b2013-07-11 12:13:16 +00001143 for (AttributeSetImpl::iterator II = pImpl->begin(I),
Bill Wendling9eb689c2013-01-28 00:21:34 +00001144 IE = pImpl->end(I); II != IE; ++II)
Bill Wendling23804da2013-01-31 23:38:01 +00001145 addAttribute(*II);
Bill Wendling9eb689c2013-01-28 00:21:34 +00001146
1147 break;
1148 }
Bill Wendling096f5442013-01-07 08:24:35 +00001149}
1150
Bill Wendlingcd330342013-01-04 23:27:34 +00001151void AttrBuilder::clear() {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001152 Attrs.reset();
Sanjoy Das88d0fde2015-09-03 22:27:42 +00001153 TargetDepAttrs.clear();
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001154 Alignment = StackAlignment = DerefBytes = DerefOrNullBytes = 0;
Bill Wendlingcd330342013-01-04 23:27:34 +00001155}
1156
1157AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001158 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
Bill Wendling1c7cc8a2013-01-31 23:16:25 +00001159 assert(Val != Attribute::Alignment && Val != Attribute::StackAlignment &&
Hal Finkelb0407ba2014-07-18 15:51:28 +00001160 Val != Attribute::Dereferenceable &&
1161 "Adding integer attribute without adding a value!");
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001162 Attrs[Val] = true;
Bill Wendling7c04e042012-10-09 19:01:18 +00001163 return *this;
Bill Wendlingabf3feb2012-10-05 06:44:41 +00001164}
1165
Bill Wendling23804da2013-01-31 23:38:01 +00001166AttrBuilder &AttrBuilder::addAttribute(Attribute Attr) {
Bill Wendling0a437302013-02-10 10:13:23 +00001167 if (Attr.isStringAttribute()) {
1168 addAttribute(Attr.getKindAsString(), Attr.getValueAsString());
1169 return *this;
1170 }
1171
Bill Wendling3f12ac22013-02-05 22:37:24 +00001172 Attribute::AttrKind Kind = Attr.getKindAsEnum();
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001173 Attrs[Kind] = true;
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001174
Bill Wendling3f12ac22013-02-05 22:37:24 +00001175 if (Kind == Attribute::Alignment)
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001176 Alignment = Attr.getAlignment();
Bill Wendling3f12ac22013-02-05 22:37:24 +00001177 else if (Kind == Attribute::StackAlignment)
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001178 StackAlignment = Attr.getStackAlignment();
Hal Finkelb0407ba2014-07-18 15:51:28 +00001179 else if (Kind == Attribute::Dereferenceable)
1180 DerefBytes = Attr.getDereferenceableBytes();
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001181 else if (Kind == Attribute::DereferenceableOrNull)
1182 DerefOrNullBytes = Attr.getDereferenceableOrNullBytes();
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001183 return *this;
1184}
1185
Bill Wendlingb9c5b1a2013-02-05 08:09:32 +00001186AttrBuilder &AttrBuilder::addAttribute(StringRef A, StringRef V) {
1187 TargetDepAttrs[A] = V;
1188 return *this;
1189}
1190
Bill Wendling23804da2013-01-31 23:38:01 +00001191AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001192 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
1193 Attrs[Val] = false;
Bill Wendling23804da2013-01-31 23:38:01 +00001194
1195 if (Val == Attribute::Alignment)
1196 Alignment = 0;
1197 else if (Val == Attribute::StackAlignment)
1198 StackAlignment = 0;
Hal Finkelb0407ba2014-07-18 15:51:28 +00001199 else if (Val == Attribute::Dereferenceable)
1200 DerefBytes = 0;
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001201 else if (Val == Attribute::DereferenceableOrNull)
1202 DerefOrNullBytes = 0;
Bill Wendling23804da2013-01-31 23:38:01 +00001203
1204 return *this;
1205}
1206
Bill Wendlingd2196752013-01-30 23:07:40 +00001207AttrBuilder &AttrBuilder::removeAttributes(AttributeSet A, uint64_t Index) {
Bill Wendling211316c2013-04-18 20:17:28 +00001208 unsigned Slot = ~0U;
Bill Wendlingf1c94e32013-02-01 00:13:50 +00001209 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
1210 if (A.getSlotIndex(I) == Index) {
Bill Wendling211316c2013-04-18 20:17:28 +00001211 Slot = I;
Bill Wendlingf1c94e32013-02-01 00:13:50 +00001212 break;
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001213 }
Bill Wendlingf1c94e32013-02-01 00:13:50 +00001214
Bill Wendling211316c2013-04-18 20:17:28 +00001215 assert(Slot != ~0U && "Couldn't find index in AttributeSet!");
Bill Wendlingf1c94e32013-02-01 00:13:50 +00001216
Bill Wendling211316c2013-04-18 20:17:28 +00001217 for (AttributeSet::iterator I = A.begin(Slot), E = A.end(Slot); I != E; ++I) {
Bill Wendling7cde51d2013-02-12 07:56:49 +00001218 Attribute Attr = *I;
Hal Finkele15442c2014-07-18 06:51:55 +00001219 if (Attr.isEnumAttribute() || Attr.isIntAttribute()) {
George Burgess IV500d3032015-12-16 05:21:02 +00001220 removeAttribute(Attr.getKindAsEnum());
Bill Wendling7cde51d2013-02-12 07:56:49 +00001221 } else {
1222 assert(Attr.isStringAttribute() && "Invalid attribute type!");
George Burgess IV500d3032015-12-16 05:21:02 +00001223 removeAttribute(Attr.getKindAsString());
Bill Wendling7cde51d2013-02-12 07:56:49 +00001224 }
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001225 }
1226
1227 return *this;
1228}
1229
Bill Wendlingb9c5b1a2013-02-05 08:09:32 +00001230AttrBuilder &AttrBuilder::removeAttribute(StringRef A) {
1231 std::map<std::string, std::string>::iterator I = TargetDepAttrs.find(A);
1232 if (I != TargetDepAttrs.end())
1233 TargetDepAttrs.erase(I);
1234 return *this;
1235}
1236
Bill Wendling50d27842012-10-15 20:35:56 +00001237AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
Bill Wendlingabd5ba22012-10-14 03:58:29 +00001238 if (Align == 0) return *this;
Bill Wendlingcd330342013-01-04 23:27:34 +00001239
Bill Wendlingabf3feb2012-10-05 06:44:41 +00001240 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1241 assert(Align <= 0x40000000 && "Alignment too large.");
Bill Wendlingcd330342013-01-04 23:27:34 +00001242
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001243 Attrs[Attribute::Alignment] = true;
Bill Wendlingcd330342013-01-04 23:27:34 +00001244 Alignment = Align;
Bill Wendlingabd5ba22012-10-14 03:58:29 +00001245 return *this;
Chris Lattner3e13b8c2008-01-02 23:42:30 +00001246}
1247
Bill Wendlingcd330342013-01-04 23:27:34 +00001248AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
1249 // Default alignment, allow the target to define how to align it.
1250 if (Align == 0) return *this;
1251
1252 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1253 assert(Align <= 0x100 && "Alignment too large.");
1254
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001255 Attrs[Attribute::StackAlignment] = true;
Bill Wendlingcd330342013-01-04 23:27:34 +00001256 StackAlignment = Align;
1257 return *this;
1258}
1259
Hal Finkelb0407ba2014-07-18 15:51:28 +00001260AttrBuilder &AttrBuilder::addDereferenceableAttr(uint64_t Bytes) {
1261 if (Bytes == 0) return *this;
1262
1263 Attrs[Attribute::Dereferenceable] = true;
1264 DerefBytes = Bytes;
1265 return *this;
1266}
1267
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001268AttrBuilder &AttrBuilder::addDereferenceableOrNullAttr(uint64_t Bytes) {
1269 if (Bytes == 0)
1270 return *this;
1271
1272 Attrs[Attribute::DereferenceableOrNull] = true;
1273 DerefOrNullBytes = Bytes;
1274 return *this;
1275}
1276
Bill Wendlinge2614922013-02-06 01:16:00 +00001277AttrBuilder &AttrBuilder::merge(const AttrBuilder &B) {
1278 // FIXME: What if both have alignments, but they don't match?!
1279 if (!Alignment)
1280 Alignment = B.Alignment;
1281
1282 if (!StackAlignment)
1283 StackAlignment = B.StackAlignment;
1284
Hal Finkelb0407ba2014-07-18 15:51:28 +00001285 if (!DerefBytes)
1286 DerefBytes = B.DerefBytes;
1287
Pete Cooperd2a44612015-05-06 23:19:43 +00001288 if (!DerefOrNullBytes)
1289 DerefOrNullBytes = B.DerefOrNullBytes;
1290
Benjamin Kramer45e7d532013-02-16 19:13:18 +00001291 Attrs |= B.Attrs;
Bill Wendlinge2614922013-02-06 01:16:00 +00001292
Pete Cooperd2a44612015-05-06 23:19:43 +00001293 for (auto I : B.td_attrs())
1294 TargetDepAttrs[I.first] = I.second;
Bill Wendlinge2614922013-02-06 01:16:00 +00001295
1296 return *this;
1297}
1298
Pete Cooperd2a44612015-05-06 23:19:43 +00001299AttrBuilder &AttrBuilder::remove(const AttrBuilder &B) {
1300 // FIXME: What if both have alignments, but they don't match?!
1301 if (B.Alignment)
1302 Alignment = 0;
1303
1304 if (B.StackAlignment)
1305 StackAlignment = 0;
1306
1307 if (B.DerefBytes)
1308 DerefBytes = 0;
1309
1310 if (B.DerefOrNullBytes)
1311 DerefOrNullBytes = 0;
1312
1313 Attrs &= ~B.Attrs;
1314
1315 for (auto I : B.td_attrs())
1316 TargetDepAttrs.erase(I.first);
1317
1318 return *this;
1319}
1320
1321bool AttrBuilder::overlaps(const AttrBuilder &B) const {
1322 // First check if any of the target independent attributes overlap.
1323 if ((Attrs & B.Attrs).any())
1324 return true;
1325
1326 // Then check if any target dependent ones do.
1327 for (auto I : td_attrs())
1328 if (B.contains(I.first))
1329 return true;
1330
1331 return false;
1332}
1333
Bill Wendling4b001442013-02-06 01:33:42 +00001334bool AttrBuilder::contains(StringRef A) const {
1335 return TargetDepAttrs.find(A) != TargetDepAttrs.end();
1336}
1337
Bill Wendling50d27842012-10-15 20:35:56 +00001338bool AttrBuilder::hasAttributes() const {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001339 return !Attrs.none() || !TargetDepAttrs.empty();
Bill Wendlingc6daefa2012-10-08 23:27:46 +00001340}
Bill Wendling9ac69f92013-01-04 20:54:35 +00001341
Bill Wendlingd2196752013-01-30 23:07:40 +00001342bool AttrBuilder::hasAttributes(AttributeSet A, uint64_t Index) const {
Bill Wendling211316c2013-04-18 20:17:28 +00001343 unsigned Slot = ~0U;
Bill Wendling9ca01da2013-02-02 00:42:06 +00001344 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
1345 if (A.getSlotIndex(I) == Index) {
Bill Wendling211316c2013-04-18 20:17:28 +00001346 Slot = I;
Bill Wendling9ca01da2013-02-02 00:42:06 +00001347 break;
1348 }
1349
Bill Wendling211316c2013-04-18 20:17:28 +00001350 assert(Slot != ~0U && "Couldn't find the index!");
Bill Wendling9ca01da2013-02-02 00:42:06 +00001351
George Burgess IV500d3032015-12-16 05:21:02 +00001352 for (AttributeSet::iterator I = A.begin(Slot), E = A.end(Slot); I != E; ++I) {
Bill Wendling7cde51d2013-02-12 07:56:49 +00001353 Attribute Attr = *I;
Hal Finkele15442c2014-07-18 06:51:55 +00001354 if (Attr.isEnumAttribute() || Attr.isIntAttribute()) {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001355 if (Attrs[I->getKindAsEnum()])
Bill Wendling7cde51d2013-02-12 07:56:49 +00001356 return true;
1357 } else {
1358 assert(Attr.isStringAttribute() && "Invalid attribute kind!");
1359 return TargetDepAttrs.find(Attr.getKindAsString())!=TargetDepAttrs.end();
1360 }
1361 }
Bill Wendling9ca01da2013-02-02 00:42:06 +00001362
1363 return false;
Bill Wendling70f39172012-10-09 00:01:21 +00001364}
Bill Wendling9ac69f92013-01-04 20:54:35 +00001365
Bill Wendling50d27842012-10-15 20:35:56 +00001366bool AttrBuilder::hasAlignmentAttr() const {
Bill Wendlingcd330342013-01-04 23:27:34 +00001367 return Alignment != 0;
Bill Wendlingc6daefa2012-10-08 23:27:46 +00001368}
1369
Bill Wendlingd509a662013-01-29 00:34:06 +00001370bool AttrBuilder::operator==(const AttrBuilder &B) {
Benjamin Kramer45e7d532013-02-16 19:13:18 +00001371 if (Attrs != B.Attrs)
1372 return false;
Bill Wendling4b001442013-02-06 01:33:42 +00001373
1374 for (td_const_iterator I = TargetDepAttrs.begin(),
1375 E = TargetDepAttrs.end(); I != E; ++I)
1376 if (B.TargetDepAttrs.find(I->first) == B.TargetDepAttrs.end())
1377 return false;
1378
Hal Finkelb0407ba2014-07-18 15:51:28 +00001379 return Alignment == B.Alignment && StackAlignment == B.StackAlignment &&
1380 DerefBytes == B.DerefBytes;
Bill Wendlingd509a662013-01-29 00:34:06 +00001381}
1382
1383AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) {
Bill Wendling56aeccc2013-02-04 23:32:23 +00001384 // FIXME: Remove this in 4.0.
Bill Wendling60011b82013-01-29 01:43:29 +00001385 if (!Val) return *this;
1386
Bill Wendlingd509a662013-01-29 00:34:06 +00001387 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
1388 I = Attribute::AttrKind(I + 1)) {
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001389 if (I == Attribute::Dereferenceable ||
Igor Laevsky39d662f2015-07-11 10:30:36 +00001390 I == Attribute::DereferenceableOrNull ||
1391 I == Attribute::ArgMemOnly)
Hal Finkelb0407ba2014-07-18 15:51:28 +00001392 continue;
Bill Wendlingd509a662013-01-29 00:34:06 +00001393 if (uint64_t A = (Val & AttributeImpl::getAttrMask(I))) {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001394 Attrs[I] = true;
Bill Wendlingd509a662013-01-29 00:34:06 +00001395
1396 if (I == Attribute::Alignment)
1397 Alignment = 1ULL << ((A >> 16) - 1);
1398 else if (I == Attribute::StackAlignment)
1399 StackAlignment = 1ULL << ((A >> 26)-1);
1400 }
1401 }
1402
1403 return *this;
1404}
1405
Bill Wendling57625a42013-01-25 23:09:36 +00001406//===----------------------------------------------------------------------===//
1407// AttributeFuncs Function Defintions
1408//===----------------------------------------------------------------------===//
1409
Bill Wendlingc79cdff2013-02-01 01:04:27 +00001410/// \brief Which attributes cannot be applied to a type.
Craig Toppere3dcce92015-08-01 22:20:21 +00001411AttrBuilder AttributeFuncs::typeIncompatible(Type *Ty) {
Bill Wendling57625a42013-01-25 23:09:36 +00001412 AttrBuilder Incompatible;
1413
1414 if (!Ty->isIntegerTy())
1415 // Attribute that only apply to integers.
1416 Incompatible.addAttribute(Attribute::SExt)
1417 .addAttribute(Attribute::ZExt);
1418
1419 if (!Ty->isPointerTy())
1420 // Attribute that only apply to pointers.
1421 Incompatible.addAttribute(Attribute::ByVal)
1422 .addAttribute(Attribute::Nest)
1423 .addAttribute(Attribute::NoAlias)
1424 .addAttribute(Attribute::NoCapture)
Nick Lewyckyd52b1522014-05-20 01:23:40 +00001425 .addAttribute(Attribute::NonNull)
Hal Finkelb0407ba2014-07-18 15:51:28 +00001426 .addDereferenceableAttr(1) // the int here is ignored
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001427 .addDereferenceableOrNullAttr(1) // the int here is ignored
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001428 .addAttribute(Attribute::ReadNone)
1429 .addAttribute(Attribute::ReadOnly)
Reid Klecknera534a382013-12-19 02:14:12 +00001430 .addAttribute(Attribute::StructRet)
1431 .addAttribute(Attribute::InAlloca);
Bill Wendling57625a42013-01-25 23:09:36 +00001432
Pete Cooper2777d8872015-05-06 23:19:56 +00001433 return Incompatible;
Bill Wendling57625a42013-01-25 23:09:36 +00001434}
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001435
1436template<typename AttrClass>
1437static bool isEqual(const Function &Caller, const Function &Callee) {
1438 return Caller.getFnAttribute(AttrClass::getKind()) ==
1439 Callee.getFnAttribute(AttrClass::getKind());
1440}
1441
1442/// \brief Compute the logical AND of the attributes of the caller and the
1443/// callee.
1444///
1445/// This function sets the caller's attribute to false if the callee's attribute
1446/// is false.
1447template<typename AttrClass>
1448static void setAND(Function &Caller, const Function &Callee) {
1449 if (AttrClass::isSet(Caller, AttrClass::getKind()) &&
1450 !AttrClass::isSet(Callee, AttrClass::getKind()))
1451 AttrClass::set(Caller, AttrClass::getKind(), false);
1452}
1453
1454/// \brief Compute the logical OR of the attributes of the caller and the
1455/// callee.
1456///
1457/// This function sets the caller's attribute to true if the callee's attribute
1458/// is true.
1459template<typename AttrClass>
1460static void setOR(Function &Caller, const Function &Callee) {
1461 if (!AttrClass::isSet(Caller, AttrClass::getKind()) &&
1462 AttrClass::isSet(Callee, AttrClass::getKind()))
1463 AttrClass::set(Caller, AttrClass::getKind(), true);
1464}
1465
1466/// \brief If the inlined function had a higher stack protection level than the
1467/// calling function, then bump up the caller's stack protection level.
1468static void adjustCallerSSPLevel(Function &Caller, const Function &Callee) {
1469 // If upgrading the SSP attribute, clear out the old SSP Attributes first.
1470 // Having multiple SSP attributes doesn't actually hurt, but it adds useless
1471 // clutter to the IR.
1472 AttrBuilder B;
1473 B.addAttribute(Attribute::StackProtect)
1474 .addAttribute(Attribute::StackProtectStrong)
1475 .addAttribute(Attribute::StackProtectReq);
1476 AttributeSet OldSSPAttr = AttributeSet::get(Caller.getContext(),
1477 AttributeSet::FunctionIndex,
1478 B);
1479
Evgeniy Stepanovf17120a2016-04-11 22:27:48 +00001480 if (Callee.hasFnAttribute(Attribute::StackProtectReq)) {
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001481 Caller.removeAttributes(AttributeSet::FunctionIndex, OldSSPAttr);
1482 Caller.addFnAttr(Attribute::StackProtectReq);
1483 } else if (Callee.hasFnAttribute(Attribute::StackProtectStrong) &&
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001484 !Caller.hasFnAttribute(Attribute::StackProtectReq)) {
1485 Caller.removeAttributes(AttributeSet::FunctionIndex, OldSSPAttr);
1486 Caller.addFnAttr(Attribute::StackProtectStrong);
1487 } else if (Callee.hasFnAttribute(Attribute::StackProtect) &&
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001488 !Caller.hasFnAttribute(Attribute::StackProtectReq) &&
1489 !Caller.hasFnAttribute(Attribute::StackProtectStrong))
1490 Caller.addFnAttr(Attribute::StackProtect);
1491}
1492
1493#define GET_ATTR_COMPAT_FUNC
1494#include "AttributesCompatFunc.inc"
1495
1496bool AttributeFuncs::areInlineCompatible(const Function &Caller,
1497 const Function &Callee) {
1498 return hasCompatibleFnAttrs(Caller, Callee);
1499}
1500
1501
1502void AttributeFuncs::mergeAttributesForInlining(Function &Caller,
1503 const Function &Callee) {
1504 mergeFnAttrs(Caller, Callee);
1505}