blob: 7fb5bf1f42fea8865e396880994dfdf2567ad3f9 [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 Renf46262e2016-03-29 17:37:21 +0000198 if (hasAttribute(Attribute::SwiftSelf))
199 return "swiftself";
Vaivaswatha Nagarajfb3f4902015-12-16 16:16:19 +0000200 if (hasAttribute(Attribute::InaccessibleMemOnly))
201 return "inaccessiblememonly";
202 if (hasAttribute(Attribute::InaccessibleMemOrArgMemOnly))
203 return "inaccessiblemem_or_argmemonly";
Reid Klecknera534a382013-12-19 02:14:12 +0000204 if (hasAttribute(Attribute::InAlloca))
205 return "inalloca";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000206 if (hasAttribute(Attribute::InlineHint))
207 return "inlinehint";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000208 if (hasAttribute(Attribute::InReg))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000209 return "inreg";
Tom Roeder44cb65f2014-06-05 19:29:43 +0000210 if (hasAttribute(Attribute::JumpTable))
211 return "jumptable";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000212 if (hasAttribute(Attribute::MinSize))
213 return "minsize";
214 if (hasAttribute(Attribute::Naked))
215 return "naked";
216 if (hasAttribute(Attribute::Nest))
217 return "nest";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000218 if (hasAttribute(Attribute::NoAlias))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000219 return "noalias";
Bill Wendling09bd1f72013-02-22 00:12:35 +0000220 if (hasAttribute(Attribute::NoBuiltin))
221 return "nobuiltin";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000222 if (hasAttribute(Attribute::NoCapture))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000223 return "nocapture";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000224 if (hasAttribute(Attribute::NoDuplicate))
225 return "noduplicate";
226 if (hasAttribute(Attribute::NoImplicitFloat))
227 return "noimplicitfloat";
228 if (hasAttribute(Attribute::NoInline))
229 return "noinline";
230 if (hasAttribute(Attribute::NonLazyBind))
231 return "nonlazybind";
Nick Lewyckyd52b1522014-05-20 01:23:40 +0000232 if (hasAttribute(Attribute::NonNull))
233 return "nonnull";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000234 if (hasAttribute(Attribute::NoRedZone))
235 return "noredzone";
236 if (hasAttribute(Attribute::NoReturn))
237 return "noreturn";
James Molloye6f87ca2015-11-06 10:32:53 +0000238 if (hasAttribute(Attribute::NoRecurse))
239 return "norecurse";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000240 if (hasAttribute(Attribute::NoUnwind))
241 return "nounwind";
Andrea Di Biagio377496b2013-08-23 11:53:55 +0000242 if (hasAttribute(Attribute::OptimizeNone))
243 return "optnone";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000244 if (hasAttribute(Attribute::OptimizeForSize))
245 return "optsize";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000246 if (hasAttribute(Attribute::ReadNone))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000247 return "readnone";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000248 if (hasAttribute(Attribute::ReadOnly))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000249 return "readonly";
Stephen Linb8bd2322013-04-20 05:14:40 +0000250 if (hasAttribute(Attribute::Returned))
251 return "returned";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000252 if (hasAttribute(Attribute::ReturnsTwice))
253 return "returns_twice";
254 if (hasAttribute(Attribute::SExt))
255 return "signext";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000256 if (hasAttribute(Attribute::StackProtect))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000257 return "ssp";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000258 if (hasAttribute(Attribute::StackProtectReq))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000259 return "sspreq";
Bill Wendlingd154e2832013-01-23 06:41:41 +0000260 if (hasAttribute(Attribute::StackProtectStrong))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000261 return "sspstrong";
Peter Collingbourne82437bf2015-06-15 21:07:11 +0000262 if (hasAttribute(Attribute::SafeStack))
263 return "safestack";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000264 if (hasAttribute(Attribute::StructRet))
265 return "sret";
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000266 if (hasAttribute(Attribute::SanitizeThread))
267 return "sanitize_thread";
268 if (hasAttribute(Attribute::SanitizeMemory))
269 return "sanitize_memory";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000270 if (hasAttribute(Attribute::UWTable))
271 return "uwtable";
272 if (hasAttribute(Attribute::ZExt))
273 return "zeroext";
Diego Novilloc6399532013-05-24 12:26:52 +0000274 if (hasAttribute(Attribute::Cold))
275 return "cold";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000276
277 // FIXME: These should be output like this:
278 //
279 // align=4
280 // alignstack=8
281 //
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000282 if (hasAttribute(Attribute::Alignment)) {
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000283 std::string Result;
Bill Wendling829b4782013-02-11 08:43:33 +0000284 Result += "align";
285 Result += (InAttrGrp) ? "=" : " ";
Bill Wendling3f12ac22013-02-05 22:37:24 +0000286 Result += utostr(getValueAsInt());
287 return Result;
288 }
Bill Wendling829b4782013-02-11 08:43:33 +0000289
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000290 auto AttrWithBytesToString = [&](const char *Name) {
Bill Wendling3f12ac22013-02-05 22:37:24 +0000291 std::string Result;
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000292 Result += Name;
Bill Wendling829b4782013-02-11 08:43:33 +0000293 if (InAttrGrp) {
294 Result += "=";
295 Result += utostr(getValueAsInt());
296 } else {
297 Result += "(";
298 Result += utostr(getValueAsInt());
299 Result += ")";
300 }
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000301 return Result;
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000302 };
Bill Wendling9c2eba92013-01-31 20:59:05 +0000303
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000304 if (hasAttribute(Attribute::StackAlignment))
305 return AttrWithBytesToString("alignstack");
306
307 if (hasAttribute(Attribute::Dereferenceable))
308 return AttrWithBytesToString("dereferenceable");
309
310 if (hasAttribute(Attribute::DereferenceableOrNull))
311 return AttrWithBytesToString("dereferenceable_or_null");
Hal Finkelb0407ba2014-07-18 15:51:28 +0000312
Bill Wendling9c2eba92013-01-31 20:59:05 +0000313 // Convert target-dependent attributes to strings of the form:
314 //
315 // "kind"
316 // "kind" = "value"
Bill Wendling9c2eba92013-01-31 20:59:05 +0000317 //
Bill Wendling3f12ac22013-02-05 22:37:24 +0000318 if (isStringAttribute()) {
Bill Wendling9c2eba92013-01-31 20:59:05 +0000319 std::string Result;
Yaron Keren075759a2015-03-30 15:42:36 +0000320 Result += (Twine('"') + getKindAsString() + Twine('"')).str();
Bill Wendling9c2eba92013-01-31 20:59:05 +0000321
Bill Wendling3f12ac22013-02-05 22:37:24 +0000322 StringRef Val = pImpl->getValueAsString();
323 if (Val.empty()) return Result;
Bill Wendling7a33f772013-02-01 22:32:30 +0000324
Yaron Keren075759a2015-03-30 15:42:36 +0000325 Result += ("=\"" + Val + Twine('"')).str();
Bill Wendlingc79cdff2013-02-01 01:04:27 +0000326 return Result;
Bill Wendling9c2eba92013-01-31 20:59:05 +0000327 }
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000328
329 llvm_unreachable("Unknown attribute");
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000330}
331
Bill Wendlingd509a662013-01-29 00:34:06 +0000332bool Attribute::operator<(Attribute A) const {
333 if (!pImpl && !A.pImpl) return false;
334 if (!pImpl) return true;
335 if (!A.pImpl) return false;
336 return *pImpl < *A.pImpl;
337}
338
Bill Wendlingd509a662013-01-29 00:34:06 +0000339//===----------------------------------------------------------------------===//
340// AttributeImpl Definition
341//===----------------------------------------------------------------------===//
342
Eric Christopher0eaa5412014-07-02 22:05:40 +0000343// Pin the vtables to this file.
Alexey Samsonov49109a22013-11-18 09:31:53 +0000344AttributeImpl::~AttributeImpl() {}
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +0000345void EnumAttributeImpl::anchor() {}
Hal Finkele15442c2014-07-18 06:51:55 +0000346void IntAttributeImpl::anchor() {}
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +0000347void StringAttributeImpl::anchor() {}
Alexey Samsonov49109a22013-11-18 09:31:53 +0000348
Bill Wendlingd509a662013-01-29 00:34:06 +0000349bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
Bill Wendling3f12ac22013-02-05 22:37:24 +0000350 if (isStringAttribute()) return false;
351 return getKindAsEnum() == A;
Bill Wendlingd509a662013-01-29 00:34:06 +0000352}
353
Bill Wendling3f12ac22013-02-05 22:37:24 +0000354bool AttributeImpl::hasAttribute(StringRef Kind) const {
355 if (!isStringAttribute()) return false;
356 return getKindAsString() == Kind;
Bill Wendlingd509a662013-01-29 00:34:06 +0000357}
358
Bill Wendling3f12ac22013-02-05 22:37:24 +0000359Attribute::AttrKind AttributeImpl::getKindAsEnum() const {
Hal Finkele15442c2014-07-18 06:51:55 +0000360 assert(isEnumAttribute() || isIntAttribute());
Benjamin Kramer741146b2013-07-11 12:13:16 +0000361 return static_cast<const EnumAttributeImpl *>(this)->getEnumKind();
Bill Wendlingd509a662013-01-29 00:34:06 +0000362}
363
Bill Wendling3f12ac22013-02-05 22:37:24 +0000364uint64_t AttributeImpl::getValueAsInt() const {
Hal Finkele15442c2014-07-18 06:51:55 +0000365 assert(isIntAttribute());
366 return static_cast<const IntAttributeImpl *>(this)->getValue();
Bill Wendlingd509a662013-01-29 00:34:06 +0000367}
368
Bill Wendling3f12ac22013-02-05 22:37:24 +0000369StringRef AttributeImpl::getKindAsString() const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000370 assert(isStringAttribute());
371 return static_cast<const StringAttributeImpl *>(this)->getStringKind();
Bill Wendlingd509a662013-01-29 00:34:06 +0000372}
373
Bill Wendling3f12ac22013-02-05 22:37:24 +0000374StringRef AttributeImpl::getValueAsString() const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000375 assert(isStringAttribute());
376 return static_cast<const StringAttributeImpl *>(this)->getStringValue();
Bill Wendlingd509a662013-01-29 00:34:06 +0000377}
378
379bool AttributeImpl::operator<(const AttributeImpl &AI) const {
Bill Wendlingc79cdff2013-02-01 01:04:27 +0000380 // This sorts the attributes with Attribute::AttrKinds coming first (sorted
381 // relative to their enum value) and then strings.
Bill Wendling26b95752013-02-15 05:25:26 +0000382 if (isEnumAttribute()) {
383 if (AI.isEnumAttribute()) return getKindAsEnum() < AI.getKindAsEnum();
Hal Finkele15442c2014-07-18 06:51:55 +0000384 if (AI.isIntAttribute()) return true;
Bill Wendling26b95752013-02-15 05:25:26 +0000385 if (AI.isStringAttribute()) return true;
386 }
Bill Wendlingc79cdff2013-02-01 01:04:27 +0000387
Hal Finkele15442c2014-07-18 06:51:55 +0000388 if (isIntAttribute()) {
Bill Wendling26b95752013-02-15 05:25:26 +0000389 if (AI.isEnumAttribute()) return false;
Hal Finkele15442c2014-07-18 06:51:55 +0000390 if (AI.isIntAttribute()) return getValueAsInt() < AI.getValueAsInt();
Bill Wendling26b95752013-02-15 05:25:26 +0000391 if (AI.isStringAttribute()) return true;
Bill Wendling3f12ac22013-02-05 22:37:24 +0000392 }
Bill Wendlingd509a662013-01-29 00:34:06 +0000393
Bill Wendling26b95752013-02-15 05:25:26 +0000394 if (AI.isEnumAttribute()) return false;
Hal Finkele15442c2014-07-18 06:51:55 +0000395 if (AI.isIntAttribute()) return false;
Bill Wendling26b95752013-02-15 05:25:26 +0000396 if (getKindAsString() == AI.getKindAsString())
397 return getValueAsString() < AI.getValueAsString();
398 return getKindAsString() < AI.getKindAsString();
Bill Wendlingd509a662013-01-29 00:34:06 +0000399}
400
Bill Wendlingd509a662013-01-29 00:34:06 +0000401uint64_t AttributeImpl::getAttrMask(Attribute::AttrKind Val) {
402 // FIXME: Remove this.
403 switch (Val) {
404 case Attribute::EndAttrKinds:
Bill Wendlingd509a662013-01-29 00:34:06 +0000405 llvm_unreachable("Synthetic enumerators which should never get here");
406
407 case Attribute::None: return 0;
408 case Attribute::ZExt: return 1 << 0;
409 case Attribute::SExt: return 1 << 1;
410 case Attribute::NoReturn: return 1 << 2;
411 case Attribute::InReg: return 1 << 3;
412 case Attribute::StructRet: return 1 << 4;
413 case Attribute::NoUnwind: return 1 << 5;
414 case Attribute::NoAlias: return 1 << 6;
415 case Attribute::ByVal: return 1 << 7;
416 case Attribute::Nest: return 1 << 8;
417 case Attribute::ReadNone: return 1 << 9;
418 case Attribute::ReadOnly: return 1 << 10;
419 case Attribute::NoInline: return 1 << 11;
420 case Attribute::AlwaysInline: return 1 << 12;
421 case Attribute::OptimizeForSize: return 1 << 13;
422 case Attribute::StackProtect: return 1 << 14;
423 case Attribute::StackProtectReq: return 1 << 15;
424 case Attribute::Alignment: return 31 << 16;
425 case Attribute::NoCapture: return 1 << 21;
426 case Attribute::NoRedZone: return 1 << 22;
427 case Attribute::NoImplicitFloat: return 1 << 23;
428 case Attribute::Naked: return 1 << 24;
429 case Attribute::InlineHint: return 1 << 25;
430 case Attribute::StackAlignment: return 7 << 26;
431 case Attribute::ReturnsTwice: return 1 << 29;
432 case Attribute::UWTable: return 1 << 30;
433 case Attribute::NonLazyBind: return 1U << 31;
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000434 case Attribute::SanitizeAddress: return 1ULL << 32;
Bill Wendlingd509a662013-01-29 00:34:06 +0000435 case Attribute::MinSize: return 1ULL << 33;
436 case Attribute::NoDuplicate: return 1ULL << 34;
437 case Attribute::StackProtectStrong: return 1ULL << 35;
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000438 case Attribute::SanitizeThread: return 1ULL << 36;
439 case Attribute::SanitizeMemory: return 1ULL << 37;
Bill Wendling7bfdfe72013-02-22 00:40:12 +0000440 case Attribute::NoBuiltin: return 1ULL << 38;
Stephen Linb8bd2322013-04-20 05:14:40 +0000441 case Attribute::Returned: return 1ULL << 39;
Diego Novilloc6399532013-05-24 12:26:52 +0000442 case Attribute::Cold: return 1ULL << 40;
Michael Gottesman41748d72013-06-27 00:25:01 +0000443 case Attribute::Builtin: return 1ULL << 41;
Andrea Di Biagio377496b2013-08-23 11:53:55 +0000444 case Attribute::OptimizeNone: return 1ULL << 42;
Reid Klecknera534a382013-12-19 02:14:12 +0000445 case Attribute::InAlloca: return 1ULL << 43;
Nick Lewyckyd52b1522014-05-20 01:23:40 +0000446 case Attribute::NonNull: return 1ULL << 44;
Tom Roeder44cb65f2014-06-05 19:29:43 +0000447 case Attribute::JumpTable: return 1ULL << 45;
Owen Anderson85fa7d52015-05-26 23:48:40 +0000448 case Attribute::Convergent: return 1ULL << 46;
Peter Collingbourne82437bf2015-06-15 21:07:11 +0000449 case Attribute::SafeStack: return 1ULL << 47;
James Molloye6f87ca2015-11-06 10:32:53 +0000450 case Attribute::NoRecurse: return 1ULL << 48;
Vaivaswatha Nagarajfb3f4902015-12-16 16:16:19 +0000451 case Attribute::InaccessibleMemOnly: return 1ULL << 49;
452 case Attribute::InaccessibleMemOrArgMemOnly: return 1ULL << 50;
Manman Renf46262e2016-03-29 17:37:21 +0000453 case Attribute::SwiftSelf: return 1ULL << 51;
Hal Finkelb0407ba2014-07-18 15:51:28 +0000454 case Attribute::Dereferenceable:
455 llvm_unreachable("dereferenceable attribute not supported in raw format");
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000456 break;
457 case Attribute::DereferenceableOrNull:
458 llvm_unreachable("dereferenceable_or_null attribute not supported in raw "
459 "format");
460 break;
Igor Laevsky39d662f2015-07-11 10:30:36 +0000461 case Attribute::ArgMemOnly:
462 llvm_unreachable("argmemonly attribute not supported in raw format");
463 break;
Bill Wendling25342e12013-02-22 00:50:09 +0000464 }
465 llvm_unreachable("Unsupported attribute type");
Bill Wendlingd509a662013-01-29 00:34:06 +0000466}
467
468//===----------------------------------------------------------------------===//
469// AttributeSetNode Definition
470//===----------------------------------------------------------------------===//
471
472AttributeSetNode *AttributeSetNode::get(LLVMContext &C,
473 ArrayRef<Attribute> Attrs) {
474 if (Attrs.empty())
Craig Topperc6207612014-04-09 06:08:46 +0000475 return nullptr;
Bill Wendlingd509a662013-01-29 00:34:06 +0000476
477 // Otherwise, build a key to look up the existing attributes.
478 LLVMContextImpl *pImpl = C.pImpl;
479 FoldingSetNodeID ID;
480
481 SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end());
Bill Wendling82c2ee62013-02-13 09:26:26 +0000482 array_pod_sort(SortedAttrs.begin(), SortedAttrs.end());
Bill Wendlingd509a662013-01-29 00:34:06 +0000483
George Burgess IV500d3032015-12-16 05:21:02 +0000484 for (Attribute Attr : SortedAttrs)
485 Attr.Profile(ID);
Bill Wendlingd509a662013-01-29 00:34:06 +0000486
487 void *InsertPoint;
488 AttributeSetNode *PA =
489 pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint);
490
491 // If we didn't find any existing attributes of the same shape then create a
492 // new one and insert it.
493 if (!PA) {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000494 // Coallocate entries after the AttributeSetNode itself.
James Y Knightaa365b22015-08-05 22:57:34 +0000495 void *Mem = ::operator new(totalSizeToAlloc<Attribute>(SortedAttrs.size()));
Benjamin Kramer741146b2013-07-11 12:13:16 +0000496 PA = new (Mem) AttributeSetNode(SortedAttrs);
Bill Wendlingd509a662013-01-29 00:34:06 +0000497 pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint);
498 }
499
500 // Return the AttributesListNode that we found or created.
501 return PA;
502}
503
Bill Wendlingbce7b972013-02-13 08:42:21 +0000504bool AttributeSetNode::hasAttribute(StringRef Kind) const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000505 for (iterator I = begin(), E = end(); I != E; ++I)
Bill Wendlingbce7b972013-02-13 08:42:21 +0000506 if (I->hasAttribute(Kind))
507 return true;
508 return false;
509}
510
511Attribute AttributeSetNode::getAttribute(Attribute::AttrKind Kind) const {
Matthias Braun31eeb76f2016-01-29 22:25:13 +0000512 if (hasAttribute(Kind)) {
513 for (iterator I = begin(), E = end(); I != E; ++I)
514 if (I->hasAttribute(Kind))
515 return *I;
516 }
Bill Wendlingbce7b972013-02-13 08:42:21 +0000517 return Attribute();
518}
519
520Attribute AttributeSetNode::getAttribute(StringRef Kind) const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000521 for (iterator I = begin(), E = end(); I != E; ++I)
Bill Wendlingbce7b972013-02-13 08:42:21 +0000522 if (I->hasAttribute(Kind))
523 return *I;
524 return Attribute();
525}
526
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000527unsigned AttributeSetNode::getAlignment() const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000528 for (iterator I = begin(), E = end(); I != E; ++I)
NAKAMURA Takumib0944392013-01-31 03:47:28 +0000529 if (I->hasAttribute(Attribute::Alignment))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000530 return I->getAlignment();
531 return 0;
532}
533
534unsigned AttributeSetNode::getStackAlignment() 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::StackAlignment))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000537 return I->getStackAlignment();
538 return 0;
539}
540
Hal Finkelb0407ba2014-07-18 15:51:28 +0000541uint64_t AttributeSetNode::getDereferenceableBytes() const {
542 for (iterator I = begin(), E = end(); I != E; ++I)
543 if (I->hasAttribute(Attribute::Dereferenceable))
544 return I->getDereferenceableBytes();
545 return 0;
546}
547
Sanjoy Das06cf33f2015-05-06 17:41:54 +0000548uint64_t AttributeSetNode::getDereferenceableOrNullBytes() const {
549 for (iterator I = begin(), E = end(); I != E; ++I)
550 if (I->hasAttribute(Attribute::DereferenceableOrNull))
551 return I->getDereferenceableOrNullBytes();
552 return 0;
553}
554
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +0000555std::string AttributeSetNode::getAsString(bool InAttrGrp) const {
Benjamin Kramer0baf8f42013-04-19 11:43:21 +0000556 std::string Str;
Benjamin Kramer741146b2013-07-11 12:13:16 +0000557 for (iterator I = begin(), E = end(); I != E; ++I) {
558 if (I != begin())
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +0000559 Str += ' ';
560 Str += I->getAsString(InAttrGrp);
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000561 }
562 return Str;
563}
564
Bill Wendlingd509a662013-01-29 00:34:06 +0000565//===----------------------------------------------------------------------===//
566// AttributeSetImpl Definition
567//===----------------------------------------------------------------------===//
568
Rafael Espindoladd275302013-04-30 16:53:38 +0000569uint64_t AttributeSetImpl::Raw(unsigned Index) const {
Bill Wendlingd509a662013-01-29 00:34:06 +0000570 for (unsigned I = 0, E = getNumAttributes(); I != E; ++I) {
571 if (getSlotIndex(I) != Index) continue;
Benjamin Kramer741146b2013-07-11 12:13:16 +0000572 const AttributeSetNode *ASN = getSlotNode(I);
Bill Wendling91226182013-02-02 00:52:44 +0000573 uint64_t Mask = 0;
Bill Wendlingd509a662013-01-29 00:34:06 +0000574
Benjamin Kramer741146b2013-07-11 12:13:16 +0000575 for (AttributeSetNode::iterator II = ASN->begin(),
Bill Wendling91226182013-02-02 00:52:44 +0000576 IE = ASN->end(); II != IE; ++II) {
577 Attribute Attr = *II;
Bill Wendlingd746e402013-02-10 23:18:05 +0000578
579 // This cannot handle string attributes.
580 if (Attr.isStringAttribute()) continue;
581
Bill Wendling3f12ac22013-02-05 22:37:24 +0000582 Attribute::AttrKind Kind = Attr.getKindAsEnum();
Bill Wendling91226182013-02-02 00:52:44 +0000583
Bill Wendling3f12ac22013-02-05 22:37:24 +0000584 if (Kind == Attribute::Alignment)
Bill Wendling91226182013-02-02 00:52:44 +0000585 Mask |= (Log2_32(ASN->getAlignment()) + 1) << 16;
Bill Wendling3f12ac22013-02-05 22:37:24 +0000586 else if (Kind == Attribute::StackAlignment)
Bill Wendling91226182013-02-02 00:52:44 +0000587 Mask |= (Log2_32(ASN->getStackAlignment()) + 1) << 26;
Hal Finkelb0407ba2014-07-18 15:51:28 +0000588 else if (Kind == Attribute::Dereferenceable)
589 llvm_unreachable("dereferenceable not supported in bit mask");
Bill Wendling91226182013-02-02 00:52:44 +0000590 else
Bill Wendling3f12ac22013-02-05 22:37:24 +0000591 Mask |= AttributeImpl::getAttrMask(Kind);
Bill Wendling91226182013-02-02 00:52:44 +0000592 }
593
594 return Mask;
Bill Wendlingd509a662013-01-29 00:34:06 +0000595 }
596
597 return 0;
598}
599
Yaron Kereneb2a2542016-01-29 20:50:44 +0000600LLVM_DUMP_METHOD void AttributeSetImpl::dump() const {
Peter Collingbourneabca2ec2013-08-02 22:34:30 +0000601 AttributeSet(const_cast<AttributeSetImpl *>(this)).dump();
602}
603
Bill Wendlingd509a662013-01-29 00:34:06 +0000604//===----------------------------------------------------------------------===//
605// AttributeSet Construction and Mutation Methods
606//===----------------------------------------------------------------------===//
607
Bill Wendling60011b82013-01-29 01:43:29 +0000608AttributeSet
609AttributeSet::getImpl(LLVMContext &C,
610 ArrayRef<std::pair<unsigned, AttributeSetNode*> > Attrs) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000611 LLVMContextImpl *pImpl = C.pImpl;
612 FoldingSetNodeID ID;
613 AttributeSetImpl::Profile(ID, Attrs);
614
615 void *InsertPoint;
616 AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
617
618 // If we didn't find any existing attributes of the same shape then
619 // create a new one and insert it.
620 if (!PA) {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000621 // Coallocate entries after the AttributeSetImpl itself.
James Y Knightaa365b22015-08-05 22:57:34 +0000622 void *Mem = ::operator new(
623 AttributeSetImpl::totalSizeToAlloc<IndexAttrPair>(Attrs.size()));
Benjamin Kramer741146b2013-07-11 12:13:16 +0000624 PA = new (Mem) AttributeSetImpl(C, Attrs);
Bill Wendlingd509a662013-01-29 00:34:06 +0000625 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
626 }
627
628 // Return the AttributesList that we found or created.
629 return AttributeSet(PA);
630}
631
632AttributeSet AttributeSet::get(LLVMContext &C,
633 ArrayRef<std::pair<unsigned, Attribute> > Attrs){
634 // If there are no attributes then return a null AttributesList pointer.
635 if (Attrs.empty())
636 return AttributeSet();
637
Craig Toppere30b8ca2016-01-03 19:43:40 +0000638 assert(std::is_sorted(Attrs.begin(), Attrs.end(),
639 [](const std::pair<unsigned, Attribute> &LHS,
640 const std::pair<unsigned, Attribute> &RHS) {
641 return LHS.first < RHS.first;
642 }) && "Misordered Attributes list!");
643 assert(std::none_of(Attrs.begin(), Attrs.end(),
644 [](const std::pair<unsigned, Attribute> &Pair) {
645 return Pair.second.hasAttribute(Attribute::None);
646 }) && "Pointless attribute!");
Bill Wendlingd509a662013-01-29 00:34:06 +0000647
648 // Create a vector if (unsigned, AttributeSetNode*) pairs from the attributes
649 // list.
650 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrPairVec;
651 for (ArrayRef<std::pair<unsigned, Attribute> >::iterator I = Attrs.begin(),
652 E = Attrs.end(); I != E; ) {
653 unsigned Index = I->first;
654 SmallVector<Attribute, 4> AttrVec;
NAKAMURA Takumif05d2f22013-01-29 15:18:16 +0000655 while (I != E && I->first == Index) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000656 AttrVec.push_back(I->second);
657 ++I;
658 }
659
660 AttrPairVec.push_back(std::make_pair(Index,
661 AttributeSetNode::get(C, AttrVec)));
662 }
663
664 return getImpl(C, AttrPairVec);
665}
666
667AttributeSet AttributeSet::get(LLVMContext &C,
668 ArrayRef<std::pair<unsigned,
669 AttributeSetNode*> > Attrs) {
670 // If there are no attributes then return a null AttributesList pointer.
671 if (Attrs.empty())
672 return AttributeSet();
673
674 return getImpl(C, Attrs);
675}
676
David Majnemercf63a792014-05-03 23:00:35 +0000677AttributeSet AttributeSet::get(LLVMContext &C, unsigned Index,
678 const AttrBuilder &B) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000679 if (!B.hasAttributes())
680 return AttributeSet();
Bill Wendlingf7134812013-01-29 01:02:03 +0000681
Bill Wendlingae89a0f2013-02-05 23:48:36 +0000682 // Add target-independent attributes.
Bill Wendlingf7134812013-01-29 01:02:03 +0000683 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
Benjamin Kramer45e7d532013-02-16 19:13:18 +0000684 for (Attribute::AttrKind Kind = Attribute::None;
Benjamin Kramer6f37dac2013-02-16 19:22:28 +0000685 Kind != Attribute::EndAttrKinds; Kind = Attribute::AttrKind(Kind + 1)) {
Benjamin Kramer45e7d532013-02-16 19:13:18 +0000686 if (!B.contains(Kind))
687 continue;
688
George Burgess IV500d3032015-12-16 05:21:02 +0000689 Attribute Attr;
690 switch (Kind) {
691 case Attribute::Alignment:
692 Attr = Attribute::getWithAlignment(C, B.getAlignment());
693 break;
694 case Attribute::StackAlignment:
695 Attr = Attribute::getWithStackAlignment(C, B.getStackAlignment());
696 break;
697 case Attribute::Dereferenceable:
698 Attr = Attribute::getWithDereferenceableBytes(
699 C, B.getDereferenceableBytes());
700 break;
701 case Attribute::DereferenceableOrNull:
702 Attr = Attribute::getWithDereferenceableOrNullBytes(
703 C, B.getDereferenceableOrNullBytes());
704 break;
705 default:
706 Attr = Attribute::get(C, Kind);
707 }
708 Attrs.push_back(std::make_pair(Index, Attr));
Bill Wendlingf7134812013-01-29 01:02:03 +0000709 }
710
Bill Wendlingae89a0f2013-02-05 23:48:36 +0000711 // Add target-dependent (string) attributes.
David Majnemercf63a792014-05-03 23:00:35 +0000712 for (const AttrBuilder::td_type &TDA : B.td_attrs())
713 Attrs.push_back(
714 std::make_pair(Index, Attribute::get(C, TDA.first, TDA.second)));
Bill Wendlingae89a0f2013-02-05 23:48:36 +0000715
Bill Wendlingf7134812013-01-29 01:02:03 +0000716 return get(C, Attrs);
Bill Wendlingd509a662013-01-29 00:34:06 +0000717}
718
Bill Wendling211316c2013-04-18 20:17:28 +0000719AttributeSet AttributeSet::get(LLVMContext &C, unsigned Index,
Bill Wendlingd509a662013-01-29 00:34:06 +0000720 ArrayRef<Attribute::AttrKind> Kind) {
721 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
George Burgess IV500d3032015-12-16 05:21:02 +0000722 for (Attribute::AttrKind K : Kind)
723 Attrs.push_back(std::make_pair(Index, Attribute::get(C, K)));
Bill Wendlingd509a662013-01-29 00:34:06 +0000724 return get(C, Attrs);
725}
726
727AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<AttributeSet> Attrs) {
728 if (Attrs.empty()) return AttributeSet();
Peter Collingbournebd6c7452013-08-02 22:29:40 +0000729 if (Attrs.size() == 1) return Attrs[0];
Bill Wendlingd509a662013-01-29 00:34:06 +0000730
731 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrNodeVec;
Peter Collingbournebd6c7452013-08-02 22:29:40 +0000732 AttributeSetImpl *A0 = Attrs[0].pImpl;
733 if (A0)
734 AttrNodeVec.append(A0->getNode(0), A0->getNode(A0->getNumAttributes()));
735 // Copy all attributes from Attrs into AttrNodeVec while keeping AttrNodeVec
736 // ordered by index. Because we know that each list in Attrs is ordered by
737 // index we only need to merge each successive list in rather than doing a
738 // full sort.
739 for (unsigned I = 1, E = Attrs.size(); I != E; ++I) {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000740 AttributeSetImpl *AS = Attrs[I].pImpl;
741 if (!AS) continue;
Peter Collingbournebd6c7452013-08-02 22:29:40 +0000742 SmallVector<std::pair<unsigned, AttributeSetNode *>, 8>::iterator
743 ANVI = AttrNodeVec.begin(), ANVE;
James Y Knightaa365b22015-08-05 22:57:34 +0000744 for (const IndexAttrPair *AI = AS->getNode(0),
745 *AE = AS->getNode(AS->getNumAttributes());
Peter Collingbournebd6c7452013-08-02 22:29:40 +0000746 AI != AE; ++AI) {
747 ANVE = AttrNodeVec.end();
748 while (ANVI != ANVE && ANVI->first <= AI->first)
749 ++ANVI;
750 ANVI = AttrNodeVec.insert(ANVI, *AI) + 1;
751 }
Bill Wendlingd509a662013-01-29 00:34:06 +0000752 }
753
754 return getImpl(C, AttrNodeVec);
755}
756
Bill Wendling211316c2013-04-18 20:17:28 +0000757AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
Bill Wendlingd509a662013-01-29 00:34:06 +0000758 Attribute::AttrKind Attr) const {
Bill Wendling211316c2013-04-18 20:17:28 +0000759 if (hasAttribute(Index, Attr)) return *this;
760 return addAttributes(C, Index, AttributeSet::get(C, Index, Attr));
Bill Wendlingd509a662013-01-29 00:34:06 +0000761}
762
Bill Wendling211316c2013-04-18 20:17:28 +0000763AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
Reed Kotler795c7b42013-03-13 20:20:08 +0000764 StringRef Kind) const {
765 llvm::AttrBuilder B;
766 B.addAttribute(Kind);
Bill Wendling211316c2013-04-18 20:17:28 +0000767 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
Reed Kotler795c7b42013-03-13 20:20:08 +0000768}
769
Bill Wendling3b2f6102013-07-25 18:34:24 +0000770AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
771 StringRef Kind, StringRef Value) const {
772 llvm::AttrBuilder B;
773 B.addAttribute(Kind, Value);
774 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
775}
776
Akira Hatanaka237916b2015-12-02 06:58:49 +0000777AttributeSet AttributeSet::addAttribute(LLVMContext &C,
778 ArrayRef<unsigned> Indices,
779 Attribute A) const {
780 unsigned I = 0, E = pImpl ? pImpl->getNumAttributes() : 0;
781 auto IdxI = Indices.begin(), IdxE = Indices.end();
782 SmallVector<AttributeSet, 4> AttrSet;
783
784 while (I != E && IdxI != IdxE) {
785 if (getSlotIndex(I) < *IdxI)
786 AttrSet.emplace_back(getSlotAttributes(I++));
787 else if (getSlotIndex(I) > *IdxI)
788 AttrSet.emplace_back(AttributeSet::get(C, std::make_pair(*IdxI++, A)));
789 else {
790 AttrBuilder B(getSlotAttributes(I), *IdxI);
791 B.addAttribute(A);
792 AttrSet.emplace_back(AttributeSet::get(C, *IdxI, B));
793 ++I;
794 ++IdxI;
795 }
796 }
797
798 while (I != E)
799 AttrSet.emplace_back(getSlotAttributes(I++));
800
801 while (IdxI != IdxE)
802 AttrSet.emplace_back(AttributeSet::get(C, std::make_pair(*IdxI++, A)));
803
804 return get(C, AttrSet);
805}
806
Bill Wendling211316c2013-04-18 20:17:28 +0000807AttributeSet AttributeSet::addAttributes(LLVMContext &C, unsigned Index,
Bill Wendlingd509a662013-01-29 00:34:06 +0000808 AttributeSet Attrs) const {
809 if (!pImpl) return Attrs;
810 if (!Attrs.pImpl) return *this;
811
812#ifndef NDEBUG
813 // FIXME it is not obvious how this should work for alignment. For now, say
814 // we can't change a known alignment.
Bill Wendling211316c2013-04-18 20:17:28 +0000815 unsigned OldAlign = getParamAlignment(Index);
816 unsigned NewAlign = Attrs.getParamAlignment(Index);
Bill Wendlingd509a662013-01-29 00:34:06 +0000817 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
818 "Attempt to change alignment!");
819#endif
820
821 // Add the attribute slots before the one we're trying to add.
822 SmallVector<AttributeSet, 4> AttrSet;
823 uint64_t NumAttrs = pImpl->getNumAttributes();
824 AttributeSet AS;
825 uint64_t LastIndex = 0;
826 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
Bill Wendling211316c2013-04-18 20:17:28 +0000827 if (getSlotIndex(I) >= Index) {
828 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
Bill Wendlingd509a662013-01-29 00:34:06 +0000829 break;
830 }
831 LastIndex = I + 1;
832 AttrSet.push_back(getSlotAttributes(I));
833 }
834
835 // Now add the attribute into the correct slot. There may already be an
836 // AttributeSet there.
Bill Wendling211316c2013-04-18 20:17:28 +0000837 AttrBuilder B(AS, Index);
Bill Wendlingd509a662013-01-29 00:34:06 +0000838
839 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
Bill Wendling211316c2013-04-18 20:17:28 +0000840 if (Attrs.getSlotIndex(I) == Index) {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000841 for (AttributeSetImpl::iterator II = Attrs.pImpl->begin(I),
Bill Wendlingd509a662013-01-29 00:34:06 +0000842 IE = Attrs.pImpl->end(I); II != IE; ++II)
Bill Wendling23804da2013-01-31 23:38:01 +0000843 B.addAttribute(*II);
Bill Wendlingd509a662013-01-29 00:34:06 +0000844 break;
845 }
846
Bill Wendling211316c2013-04-18 20:17:28 +0000847 AttrSet.push_back(AttributeSet::get(C, Index, B));
Bill Wendlingd509a662013-01-29 00:34:06 +0000848
849 // Add the remaining attribute slots.
850 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
851 AttrSet.push_back(getSlotAttributes(I));
852
853 return get(C, AttrSet);
854}
855
Bill Wendling211316c2013-04-18 20:17:28 +0000856AttributeSet AttributeSet::removeAttribute(LLVMContext &C, unsigned Index,
Bill Wendlingd509a662013-01-29 00:34:06 +0000857 Attribute::AttrKind Attr) const {
Bill Wendling211316c2013-04-18 20:17:28 +0000858 if (!hasAttribute(Index, Attr)) return *this;
859 return removeAttributes(C, Index, AttributeSet::get(C, Index, Attr));
Bill Wendlingd509a662013-01-29 00:34:06 +0000860}
861
Bill Wendling211316c2013-04-18 20:17:28 +0000862AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Index,
Bill Wendlingd509a662013-01-29 00:34:06 +0000863 AttributeSet Attrs) const {
864 if (!pImpl) return AttributeSet();
865 if (!Attrs.pImpl) return *this;
866
Pete Cooper67cf9a72015-11-19 05:56:52 +0000867 // FIXME it is not obvious how this should work for alignment.
868 // For now, say we can't pass in alignment, which no current use does.
869 assert(!Attrs.hasAttribute(Index, Attribute::Alignment) &&
870 "Attempt to change alignment!");
871
Bill Wendlingd509a662013-01-29 00:34:06 +0000872 // Add the attribute slots before the one we're trying to add.
873 SmallVector<AttributeSet, 4> AttrSet;
874 uint64_t NumAttrs = pImpl->getNumAttributes();
875 AttributeSet AS;
876 uint64_t LastIndex = 0;
877 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
Bill Wendling211316c2013-04-18 20:17:28 +0000878 if (getSlotIndex(I) >= Index) {
879 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
Bill Wendlingd509a662013-01-29 00:34:06 +0000880 break;
881 }
882 LastIndex = I + 1;
883 AttrSet.push_back(getSlotAttributes(I));
884 }
885
Bill Wendlingd2196752013-01-30 23:07:40 +0000886 // Now remove the attribute from the correct slot. There may already be an
Bill Wendlingd509a662013-01-29 00:34:06 +0000887 // AttributeSet there.
Bill Wendling211316c2013-04-18 20:17:28 +0000888 AttrBuilder B(AS, Index);
Bill Wendlingd509a662013-01-29 00:34:06 +0000889
890 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
Bill Wendling211316c2013-04-18 20:17:28 +0000891 if (Attrs.getSlotIndex(I) == Index) {
892 B.removeAttributes(Attrs.pImpl->getSlotAttributes(I), Index);
Bill Wendlingd509a662013-01-29 00:34:06 +0000893 break;
894 }
895
Bill Wendling211316c2013-04-18 20:17:28 +0000896 AttrSet.push_back(AttributeSet::get(C, Index, B));
Bill Wendlingd509a662013-01-29 00:34:06 +0000897
898 // Add the remaining attribute slots.
899 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
900 AttrSet.push_back(getSlotAttributes(I));
901
902 return get(C, AttrSet);
903}
904
Pete Cooperd2a44612015-05-06 23:19:43 +0000905AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Index,
906 const AttrBuilder &Attrs) const {
907 if (!pImpl) return AttributeSet();
908
909 // FIXME it is not obvious how this should work for alignment.
910 // For now, say we can't pass in alignment, which no current use does.
911 assert(!Attrs.hasAlignmentAttr() && "Attempt to change alignment!");
912
913 // Add the attribute slots before the one we're trying to add.
914 SmallVector<AttributeSet, 4> AttrSet;
915 uint64_t NumAttrs = pImpl->getNumAttributes();
916 AttributeSet AS;
917 uint64_t LastIndex = 0;
918 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
919 if (getSlotIndex(I) >= Index) {
920 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
921 break;
922 }
923 LastIndex = I + 1;
924 AttrSet.push_back(getSlotAttributes(I));
925 }
926
927 // Now remove the attribute from the correct slot. There may already be an
928 // AttributeSet there.
929 AttrBuilder B(AS, Index);
930 B.remove(Attrs);
931
932 AttrSet.push_back(AttributeSet::get(C, Index, B));
933
934 // Add the remaining attribute slots.
935 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
936 AttrSet.push_back(getSlotAttributes(I));
937
938 return get(C, AttrSet);
939}
940
Ramkumar Ramachandra8fcb4982015-02-14 19:37:54 +0000941AttributeSet AttributeSet::addDereferenceableAttr(LLVMContext &C, unsigned Index,
942 uint64_t Bytes) const {
943 llvm::AttrBuilder B;
944 B.addDereferenceableAttr(Bytes);
945 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
946}
947
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000948AttributeSet AttributeSet::addDereferenceableOrNullAttr(LLVMContext &C,
949 unsigned Index,
950 uint64_t Bytes) const {
951 llvm::AttrBuilder B;
952 B.addDereferenceableOrNullAttr(Bytes);
953 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
954}
955
Bill Wendlingd509a662013-01-29 00:34:06 +0000956//===----------------------------------------------------------------------===//
957// AttributeSet Accessor Methods
958//===----------------------------------------------------------------------===//
959
Bill Wendling5d020a32013-02-10 05:00:40 +0000960LLVMContext &AttributeSet::getContext() const {
961 return pImpl->getContext();
962}
963
Bill Wendling211316c2013-04-18 20:17:28 +0000964AttributeSet AttributeSet::getParamAttributes(unsigned Index) const {
965 return pImpl && hasAttributes(Index) ?
Bill Wendlingd509a662013-01-29 00:34:06 +0000966 AttributeSet::get(pImpl->getContext(),
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000967 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendling211316c2013-04-18 20:17:28 +0000968 std::make_pair(Index, getAttributes(Index)))) :
Bill Wendlingd509a662013-01-29 00:34:06 +0000969 AttributeSet();
970}
971
972AttributeSet AttributeSet::getRetAttributes() const {
973 return pImpl && hasAttributes(ReturnIndex) ?
974 AttributeSet::get(pImpl->getContext(),
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000975 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendlingd509a662013-01-29 00:34:06 +0000976 std::make_pair(ReturnIndex,
977 getAttributes(ReturnIndex)))) :
978 AttributeSet();
979}
980
981AttributeSet AttributeSet::getFnAttributes() const {
982 return pImpl && hasAttributes(FunctionIndex) ?
983 AttributeSet::get(pImpl->getContext(),
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000984 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendlingd509a662013-01-29 00:34:06 +0000985 std::make_pair(FunctionIndex,
986 getAttributes(FunctionIndex)))) :
987 AttributeSet();
988}
989
990bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000991 AttributeSetNode *ASN = getAttributes(Index);
George Burgess IV500d3032015-12-16 05:21:02 +0000992 return ASN && ASN->hasAttribute(Kind);
Bill Wendlingd509a662013-01-29 00:34:06 +0000993}
994
Bill Wendlingbce7b972013-02-13 08:42:21 +0000995bool AttributeSet::hasAttribute(unsigned Index, StringRef Kind) const {
996 AttributeSetNode *ASN = getAttributes(Index);
George Burgess IV500d3032015-12-16 05:21:02 +0000997 return ASN && ASN->hasAttribute(Kind);
Bill Wendlingbce7b972013-02-13 08:42:21 +0000998}
999
Bill Wendlingd509a662013-01-29 00:34:06 +00001000bool AttributeSet::hasAttributes(unsigned Index) const {
Bill Wendlingf2955aa2013-01-29 03:20:31 +00001001 AttributeSetNode *ASN = getAttributes(Index);
George Burgess IV500d3032015-12-16 05:21:02 +00001002 return ASN && ASN->hasAttributes();
Bill Wendlingd509a662013-01-29 00:34:06 +00001003}
1004
Matthias Braun33282812016-01-29 22:25:19 +00001005bool AttributeSet::hasFnAttribute(Attribute::AttrKind Kind) const {
1006 return pImpl && pImpl->hasFnAttribute(Kind);
1007}
1008
Bill Wendlingd509a662013-01-29 00:34:06 +00001009bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr) const {
Craig Topperc6207612014-04-09 06:08:46 +00001010 if (!pImpl) return false;
Bill Wendlingd509a662013-01-29 00:34:06 +00001011
1012 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
Benjamin Kramer741146b2013-07-11 12:13:16 +00001013 for (AttributeSetImpl::iterator II = pImpl->begin(I),
Bill Wendlingd509a662013-01-29 00:34:06 +00001014 IE = pImpl->end(I); II != IE; ++II)
NAKAMURA Takumib0944392013-01-31 03:47:28 +00001015 if (II->hasAttribute(Attr))
Bill Wendlingd509a662013-01-29 00:34:06 +00001016 return true;
1017
1018 return false;
1019}
1020
Bill Wendlingbce7b972013-02-13 08:42:21 +00001021Attribute AttributeSet::getAttribute(unsigned Index,
1022 Attribute::AttrKind Kind) const {
1023 AttributeSetNode *ASN = getAttributes(Index);
1024 return ASN ? ASN->getAttribute(Kind) : Attribute();
1025}
1026
1027Attribute AttributeSet::getAttribute(unsigned Index,
1028 StringRef Kind) const {
1029 AttributeSetNode *ASN = getAttributes(Index);
1030 return ASN ? ASN->getAttribute(Kind) : Attribute();
1031}
1032
Bill Wendlingf2955aa2013-01-29 03:20:31 +00001033unsigned AttributeSet::getParamAlignment(unsigned Index) const {
1034 AttributeSetNode *ASN = getAttributes(Index);
1035 return ASN ? ASN->getAlignment() : 0;
Bill Wendlingd509a662013-01-29 00:34:06 +00001036}
1037
1038unsigned AttributeSet::getStackAlignment(unsigned Index) const {
Bill Wendlingf2955aa2013-01-29 03:20:31 +00001039 AttributeSetNode *ASN = getAttributes(Index);
1040 return ASN ? ASN->getStackAlignment() : 0;
Bill Wendlingd509a662013-01-29 00:34:06 +00001041}
1042
Hal Finkelb0407ba2014-07-18 15:51:28 +00001043uint64_t AttributeSet::getDereferenceableBytes(unsigned Index) const {
1044 AttributeSetNode *ASN = getAttributes(Index);
1045 return ASN ? ASN->getDereferenceableBytes() : 0;
1046}
1047
Sanjoy Das06cf33f2015-05-06 17:41:54 +00001048uint64_t AttributeSet::getDereferenceableOrNullBytes(unsigned Index) const {
1049 AttributeSetNode *ASN = getAttributes(Index);
1050 return ASN ? ASN->getDereferenceableOrNullBytes() : 0;
1051}
1052
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +00001053std::string AttributeSet::getAsString(unsigned Index,
Bill Wendling829b4782013-02-11 08:43:33 +00001054 bool InAttrGrp) const {
Bill Wendlingf2955aa2013-01-29 03:20:31 +00001055 AttributeSetNode *ASN = getAttributes(Index);
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +00001056 return ASN ? ASN->getAsString(InAttrGrp) : std::string("");
Bill Wendlingd509a662013-01-29 00:34:06 +00001057}
1058
Bill Wendling211316c2013-04-18 20:17:28 +00001059AttributeSetNode *AttributeSet::getAttributes(unsigned Index) const {
Craig Topperc6207612014-04-09 06:08:46 +00001060 if (!pImpl) return nullptr;
Bill Wendlingd509a662013-01-29 00:34:06 +00001061
Bill Wendlingf2955aa2013-01-29 03:20:31 +00001062 // Loop through to find the attribute node we want.
1063 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
Bill Wendling211316c2013-04-18 20:17:28 +00001064 if (pImpl->getSlotIndex(I) == Index)
Bill Wendlingf2955aa2013-01-29 03:20:31 +00001065 return pImpl->getSlotNode(I);
Bill Wendlingd509a662013-01-29 00:34:06 +00001066
Craig Topperc6207612014-04-09 06:08:46 +00001067 return nullptr;
Bill Wendlingd509a662013-01-29 00:34:06 +00001068}
1069
Bill Wendling211316c2013-04-18 20:17:28 +00001070AttributeSet::iterator AttributeSet::begin(unsigned Slot) const {
Bill Wendlinga9174862013-01-31 23:53:05 +00001071 if (!pImpl)
1072 return ArrayRef<Attribute>().begin();
Bill Wendling211316c2013-04-18 20:17:28 +00001073 return pImpl->begin(Slot);
Bill Wendlinga9174862013-01-31 23:53:05 +00001074}
1075
Bill Wendling211316c2013-04-18 20:17:28 +00001076AttributeSet::iterator AttributeSet::end(unsigned Slot) const {
Bill Wendlinga9174862013-01-31 23:53:05 +00001077 if (!pImpl)
1078 return ArrayRef<Attribute>().end();
Bill Wendling211316c2013-04-18 20:17:28 +00001079 return pImpl->end(Slot);
Bill Wendlinga9174862013-01-31 23:53:05 +00001080}
1081
Bill Wendlingd509a662013-01-29 00:34:06 +00001082//===----------------------------------------------------------------------===//
1083// AttributeSet Introspection Methods
1084//===----------------------------------------------------------------------===//
1085
Bill Wendlingd509a662013-01-29 00:34:06 +00001086unsigned AttributeSet::getNumSlots() const {
1087 return pImpl ? pImpl->getNumAttributes() : 0;
1088}
1089
Rafael Espindoladd275302013-04-30 16:53:38 +00001090unsigned AttributeSet::getSlotIndex(unsigned Slot) const {
Bill Wendlingd509a662013-01-29 00:34:06 +00001091 assert(pImpl && Slot < pImpl->getNumAttributes() &&
1092 "Slot # out of range!");
1093 return pImpl->getSlotIndex(Slot);
1094}
1095
1096AttributeSet AttributeSet::getSlotAttributes(unsigned Slot) const {
1097 assert(pImpl && Slot < pImpl->getNumAttributes() &&
1098 "Slot # out of range!");
1099 return pImpl->getSlotAttributes(Slot);
1100}
1101
1102uint64_t AttributeSet::Raw(unsigned Index) const {
1103 // FIXME: Remove this.
1104 return pImpl ? pImpl->Raw(Index) : 0;
1105}
1106
Yaron Kereneb2a2542016-01-29 20:50:44 +00001107LLVM_DUMP_METHOD void AttributeSet::dump() const {
Bill Wendlingd509a662013-01-29 00:34:06 +00001108 dbgs() << "PAL[\n";
1109
1110 for (unsigned i = 0, e = getNumSlots(); i < e; ++i) {
1111 uint64_t Index = getSlotIndex(i);
1112 dbgs() << " { ";
1113 if (Index == ~0U)
1114 dbgs() << "~0U";
1115 else
1116 dbgs() << Index;
1117 dbgs() << " => " << getAsString(Index) << " }\n";
1118 }
1119
1120 dbgs() << "]\n";
1121}
1122
Bill Wendlingabf3feb2012-10-05 06:44:41 +00001123//===----------------------------------------------------------------------===//
Bill Wendlingcd330342013-01-04 23:27:34 +00001124// AttrBuilder Method Implementations
Bill Wendlingabf3feb2012-10-05 06:44:41 +00001125//===----------------------------------------------------------------------===//
1126
Bill Wendling211316c2013-04-18 20:17:28 +00001127AttrBuilder::AttrBuilder(AttributeSet AS, unsigned Index)
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001128 : Attrs(0), Alignment(0), StackAlignment(0), DerefBytes(0),
1129 DerefOrNullBytes(0) {
Bill Wendling56b0b2a2013-01-27 21:23:46 +00001130 AttributeSetImpl *pImpl = AS.pImpl;
Bill Wendling096f5442013-01-07 08:24:35 +00001131 if (!pImpl) return;
1132
Bill Wendling9eb689c2013-01-28 00:21:34 +00001133 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I) {
Bill Wendling211316c2013-04-18 20:17:28 +00001134 if (pImpl->getSlotIndex(I) != Index) continue;
Bill Wendling096f5442013-01-07 08:24:35 +00001135
Benjamin Kramer741146b2013-07-11 12:13:16 +00001136 for (AttributeSetImpl::iterator II = pImpl->begin(I),
Bill Wendling9eb689c2013-01-28 00:21:34 +00001137 IE = pImpl->end(I); II != IE; ++II)
Bill Wendling23804da2013-01-31 23:38:01 +00001138 addAttribute(*II);
Bill Wendling9eb689c2013-01-28 00:21:34 +00001139
1140 break;
1141 }
Bill Wendling096f5442013-01-07 08:24:35 +00001142}
1143
Bill Wendlingcd330342013-01-04 23:27:34 +00001144void AttrBuilder::clear() {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001145 Attrs.reset();
Sanjoy Das88d0fde2015-09-03 22:27:42 +00001146 TargetDepAttrs.clear();
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001147 Alignment = StackAlignment = DerefBytes = DerefOrNullBytes = 0;
Bill Wendlingcd330342013-01-04 23:27:34 +00001148}
1149
1150AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001151 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
Bill Wendling1c7cc8a2013-01-31 23:16:25 +00001152 assert(Val != Attribute::Alignment && Val != Attribute::StackAlignment &&
Hal Finkelb0407ba2014-07-18 15:51:28 +00001153 Val != Attribute::Dereferenceable &&
1154 "Adding integer attribute without adding a value!");
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001155 Attrs[Val] = true;
Bill Wendling7c04e042012-10-09 19:01:18 +00001156 return *this;
Bill Wendlingabf3feb2012-10-05 06:44:41 +00001157}
1158
Bill Wendling23804da2013-01-31 23:38:01 +00001159AttrBuilder &AttrBuilder::addAttribute(Attribute Attr) {
Bill Wendling0a437302013-02-10 10:13:23 +00001160 if (Attr.isStringAttribute()) {
1161 addAttribute(Attr.getKindAsString(), Attr.getValueAsString());
1162 return *this;
1163 }
1164
Bill Wendling3f12ac22013-02-05 22:37:24 +00001165 Attribute::AttrKind Kind = Attr.getKindAsEnum();
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001166 Attrs[Kind] = true;
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001167
Bill Wendling3f12ac22013-02-05 22:37:24 +00001168 if (Kind == Attribute::Alignment)
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001169 Alignment = Attr.getAlignment();
Bill Wendling3f12ac22013-02-05 22:37:24 +00001170 else if (Kind == Attribute::StackAlignment)
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001171 StackAlignment = Attr.getStackAlignment();
Hal Finkelb0407ba2014-07-18 15:51:28 +00001172 else if (Kind == Attribute::Dereferenceable)
1173 DerefBytes = Attr.getDereferenceableBytes();
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001174 else if (Kind == Attribute::DereferenceableOrNull)
1175 DerefOrNullBytes = Attr.getDereferenceableOrNullBytes();
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001176 return *this;
1177}
1178
Bill Wendlingb9c5b1a2013-02-05 08:09:32 +00001179AttrBuilder &AttrBuilder::addAttribute(StringRef A, StringRef V) {
1180 TargetDepAttrs[A] = V;
1181 return *this;
1182}
1183
Bill Wendling23804da2013-01-31 23:38:01 +00001184AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001185 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
1186 Attrs[Val] = false;
Bill Wendling23804da2013-01-31 23:38:01 +00001187
1188 if (Val == Attribute::Alignment)
1189 Alignment = 0;
1190 else if (Val == Attribute::StackAlignment)
1191 StackAlignment = 0;
Hal Finkelb0407ba2014-07-18 15:51:28 +00001192 else if (Val == Attribute::Dereferenceable)
1193 DerefBytes = 0;
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001194 else if (Val == Attribute::DereferenceableOrNull)
1195 DerefOrNullBytes = 0;
Bill Wendling23804da2013-01-31 23:38:01 +00001196
1197 return *this;
1198}
1199
Bill Wendlingd2196752013-01-30 23:07:40 +00001200AttrBuilder &AttrBuilder::removeAttributes(AttributeSet A, uint64_t Index) {
Bill Wendling211316c2013-04-18 20:17:28 +00001201 unsigned Slot = ~0U;
Bill Wendlingf1c94e32013-02-01 00:13:50 +00001202 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
1203 if (A.getSlotIndex(I) == Index) {
Bill Wendling211316c2013-04-18 20:17:28 +00001204 Slot = I;
Bill Wendlingf1c94e32013-02-01 00:13:50 +00001205 break;
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001206 }
Bill Wendlingf1c94e32013-02-01 00:13:50 +00001207
Bill Wendling211316c2013-04-18 20:17:28 +00001208 assert(Slot != ~0U && "Couldn't find index in AttributeSet!");
Bill Wendlingf1c94e32013-02-01 00:13:50 +00001209
Bill Wendling211316c2013-04-18 20:17:28 +00001210 for (AttributeSet::iterator I = A.begin(Slot), E = A.end(Slot); I != E; ++I) {
Bill Wendling7cde51d2013-02-12 07:56:49 +00001211 Attribute Attr = *I;
Hal Finkele15442c2014-07-18 06:51:55 +00001212 if (Attr.isEnumAttribute() || Attr.isIntAttribute()) {
George Burgess IV500d3032015-12-16 05:21:02 +00001213 removeAttribute(Attr.getKindAsEnum());
Bill Wendling7cde51d2013-02-12 07:56:49 +00001214 } else {
1215 assert(Attr.isStringAttribute() && "Invalid attribute type!");
George Burgess IV500d3032015-12-16 05:21:02 +00001216 removeAttribute(Attr.getKindAsString());
Bill Wendling7cde51d2013-02-12 07:56:49 +00001217 }
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001218 }
1219
1220 return *this;
1221}
1222
Bill Wendlingb9c5b1a2013-02-05 08:09:32 +00001223AttrBuilder &AttrBuilder::removeAttribute(StringRef A) {
1224 std::map<std::string, std::string>::iterator I = TargetDepAttrs.find(A);
1225 if (I != TargetDepAttrs.end())
1226 TargetDepAttrs.erase(I);
1227 return *this;
1228}
1229
Bill Wendling50d27842012-10-15 20:35:56 +00001230AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
Bill Wendlingabd5ba22012-10-14 03:58:29 +00001231 if (Align == 0) return *this;
Bill Wendlingcd330342013-01-04 23:27:34 +00001232
Bill Wendlingabf3feb2012-10-05 06:44:41 +00001233 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1234 assert(Align <= 0x40000000 && "Alignment too large.");
Bill Wendlingcd330342013-01-04 23:27:34 +00001235
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001236 Attrs[Attribute::Alignment] = true;
Bill Wendlingcd330342013-01-04 23:27:34 +00001237 Alignment = Align;
Bill Wendlingabd5ba22012-10-14 03:58:29 +00001238 return *this;
Chris Lattner3e13b8c2008-01-02 23:42:30 +00001239}
1240
Bill Wendlingcd330342013-01-04 23:27:34 +00001241AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
1242 // Default alignment, allow the target to define how to align it.
1243 if (Align == 0) return *this;
1244
1245 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1246 assert(Align <= 0x100 && "Alignment too large.");
1247
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001248 Attrs[Attribute::StackAlignment] = true;
Bill Wendlingcd330342013-01-04 23:27:34 +00001249 StackAlignment = Align;
1250 return *this;
1251}
1252
Hal Finkelb0407ba2014-07-18 15:51:28 +00001253AttrBuilder &AttrBuilder::addDereferenceableAttr(uint64_t Bytes) {
1254 if (Bytes == 0) return *this;
1255
1256 Attrs[Attribute::Dereferenceable] = true;
1257 DerefBytes = Bytes;
1258 return *this;
1259}
1260
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001261AttrBuilder &AttrBuilder::addDereferenceableOrNullAttr(uint64_t Bytes) {
1262 if (Bytes == 0)
1263 return *this;
1264
1265 Attrs[Attribute::DereferenceableOrNull] = true;
1266 DerefOrNullBytes = Bytes;
1267 return *this;
1268}
1269
Bill Wendlinge2614922013-02-06 01:16:00 +00001270AttrBuilder &AttrBuilder::merge(const AttrBuilder &B) {
1271 // FIXME: What if both have alignments, but they don't match?!
1272 if (!Alignment)
1273 Alignment = B.Alignment;
1274
1275 if (!StackAlignment)
1276 StackAlignment = B.StackAlignment;
1277
Hal Finkelb0407ba2014-07-18 15:51:28 +00001278 if (!DerefBytes)
1279 DerefBytes = B.DerefBytes;
1280
Pete Cooperd2a44612015-05-06 23:19:43 +00001281 if (!DerefOrNullBytes)
1282 DerefOrNullBytes = B.DerefOrNullBytes;
1283
Benjamin Kramer45e7d532013-02-16 19:13:18 +00001284 Attrs |= B.Attrs;
Bill Wendlinge2614922013-02-06 01:16:00 +00001285
Pete Cooperd2a44612015-05-06 23:19:43 +00001286 for (auto I : B.td_attrs())
1287 TargetDepAttrs[I.first] = I.second;
Bill Wendlinge2614922013-02-06 01:16:00 +00001288
1289 return *this;
1290}
1291
Pete Cooperd2a44612015-05-06 23:19:43 +00001292AttrBuilder &AttrBuilder::remove(const AttrBuilder &B) {
1293 // FIXME: What if both have alignments, but they don't match?!
1294 if (B.Alignment)
1295 Alignment = 0;
1296
1297 if (B.StackAlignment)
1298 StackAlignment = 0;
1299
1300 if (B.DerefBytes)
1301 DerefBytes = 0;
1302
1303 if (B.DerefOrNullBytes)
1304 DerefOrNullBytes = 0;
1305
1306 Attrs &= ~B.Attrs;
1307
1308 for (auto I : B.td_attrs())
1309 TargetDepAttrs.erase(I.first);
1310
1311 return *this;
1312}
1313
1314bool AttrBuilder::overlaps(const AttrBuilder &B) const {
1315 // First check if any of the target independent attributes overlap.
1316 if ((Attrs & B.Attrs).any())
1317 return true;
1318
1319 // Then check if any target dependent ones do.
1320 for (auto I : td_attrs())
1321 if (B.contains(I.first))
1322 return true;
1323
1324 return false;
1325}
1326
Bill Wendling4b001442013-02-06 01:33:42 +00001327bool AttrBuilder::contains(StringRef A) const {
1328 return TargetDepAttrs.find(A) != TargetDepAttrs.end();
1329}
1330
Bill Wendling50d27842012-10-15 20:35:56 +00001331bool AttrBuilder::hasAttributes() const {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001332 return !Attrs.none() || !TargetDepAttrs.empty();
Bill Wendlingc6daefa2012-10-08 23:27:46 +00001333}
Bill Wendling9ac69f92013-01-04 20:54:35 +00001334
Bill Wendlingd2196752013-01-30 23:07:40 +00001335bool AttrBuilder::hasAttributes(AttributeSet A, uint64_t Index) const {
Bill Wendling211316c2013-04-18 20:17:28 +00001336 unsigned Slot = ~0U;
Bill Wendling9ca01da2013-02-02 00:42:06 +00001337 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
1338 if (A.getSlotIndex(I) == Index) {
Bill Wendling211316c2013-04-18 20:17:28 +00001339 Slot = I;
Bill Wendling9ca01da2013-02-02 00:42:06 +00001340 break;
1341 }
1342
Bill Wendling211316c2013-04-18 20:17:28 +00001343 assert(Slot != ~0U && "Couldn't find the index!");
Bill Wendling9ca01da2013-02-02 00:42:06 +00001344
George Burgess IV500d3032015-12-16 05:21:02 +00001345 for (AttributeSet::iterator I = A.begin(Slot), E = A.end(Slot); I != E; ++I) {
Bill Wendling7cde51d2013-02-12 07:56:49 +00001346 Attribute Attr = *I;
Hal Finkele15442c2014-07-18 06:51:55 +00001347 if (Attr.isEnumAttribute() || Attr.isIntAttribute()) {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001348 if (Attrs[I->getKindAsEnum()])
Bill Wendling7cde51d2013-02-12 07:56:49 +00001349 return true;
1350 } else {
1351 assert(Attr.isStringAttribute() && "Invalid attribute kind!");
1352 return TargetDepAttrs.find(Attr.getKindAsString())!=TargetDepAttrs.end();
1353 }
1354 }
Bill Wendling9ca01da2013-02-02 00:42:06 +00001355
1356 return false;
Bill Wendling70f39172012-10-09 00:01:21 +00001357}
Bill Wendling9ac69f92013-01-04 20:54:35 +00001358
Bill Wendling50d27842012-10-15 20:35:56 +00001359bool AttrBuilder::hasAlignmentAttr() const {
Bill Wendlingcd330342013-01-04 23:27:34 +00001360 return Alignment != 0;
Bill Wendlingc6daefa2012-10-08 23:27:46 +00001361}
1362
Bill Wendlingd509a662013-01-29 00:34:06 +00001363bool AttrBuilder::operator==(const AttrBuilder &B) {
Benjamin Kramer45e7d532013-02-16 19:13:18 +00001364 if (Attrs != B.Attrs)
1365 return false;
Bill Wendling4b001442013-02-06 01:33:42 +00001366
1367 for (td_const_iterator I = TargetDepAttrs.begin(),
1368 E = TargetDepAttrs.end(); I != E; ++I)
1369 if (B.TargetDepAttrs.find(I->first) == B.TargetDepAttrs.end())
1370 return false;
1371
Hal Finkelb0407ba2014-07-18 15:51:28 +00001372 return Alignment == B.Alignment && StackAlignment == B.StackAlignment &&
1373 DerefBytes == B.DerefBytes;
Bill Wendlingd509a662013-01-29 00:34:06 +00001374}
1375
1376AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) {
Bill Wendling56aeccc2013-02-04 23:32:23 +00001377 // FIXME: Remove this in 4.0.
Bill Wendling60011b82013-01-29 01:43:29 +00001378 if (!Val) return *this;
1379
Bill Wendlingd509a662013-01-29 00:34:06 +00001380 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
1381 I = Attribute::AttrKind(I + 1)) {
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001382 if (I == Attribute::Dereferenceable ||
Igor Laevsky39d662f2015-07-11 10:30:36 +00001383 I == Attribute::DereferenceableOrNull ||
1384 I == Attribute::ArgMemOnly)
Hal Finkelb0407ba2014-07-18 15:51:28 +00001385 continue;
Bill Wendlingd509a662013-01-29 00:34:06 +00001386 if (uint64_t A = (Val & AttributeImpl::getAttrMask(I))) {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001387 Attrs[I] = true;
Bill Wendlingd509a662013-01-29 00:34:06 +00001388
1389 if (I == Attribute::Alignment)
1390 Alignment = 1ULL << ((A >> 16) - 1);
1391 else if (I == Attribute::StackAlignment)
1392 StackAlignment = 1ULL << ((A >> 26)-1);
1393 }
1394 }
1395
1396 return *this;
1397}
1398
Bill Wendling57625a42013-01-25 23:09:36 +00001399//===----------------------------------------------------------------------===//
1400// AttributeFuncs Function Defintions
1401//===----------------------------------------------------------------------===//
1402
Bill Wendlingc79cdff2013-02-01 01:04:27 +00001403/// \brief Which attributes cannot be applied to a type.
Craig Toppere3dcce92015-08-01 22:20:21 +00001404AttrBuilder AttributeFuncs::typeIncompatible(Type *Ty) {
Bill Wendling57625a42013-01-25 23:09:36 +00001405 AttrBuilder Incompatible;
1406
1407 if (!Ty->isIntegerTy())
1408 // Attribute that only apply to integers.
1409 Incompatible.addAttribute(Attribute::SExt)
1410 .addAttribute(Attribute::ZExt);
1411
1412 if (!Ty->isPointerTy())
1413 // Attribute that only apply to pointers.
1414 Incompatible.addAttribute(Attribute::ByVal)
1415 .addAttribute(Attribute::Nest)
1416 .addAttribute(Attribute::NoAlias)
1417 .addAttribute(Attribute::NoCapture)
Nick Lewyckyd52b1522014-05-20 01:23:40 +00001418 .addAttribute(Attribute::NonNull)
Hal Finkelb0407ba2014-07-18 15:51:28 +00001419 .addDereferenceableAttr(1) // the int here is ignored
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001420 .addDereferenceableOrNullAttr(1) // the int here is ignored
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001421 .addAttribute(Attribute::ReadNone)
1422 .addAttribute(Attribute::ReadOnly)
Reid Klecknera534a382013-12-19 02:14:12 +00001423 .addAttribute(Attribute::StructRet)
1424 .addAttribute(Attribute::InAlloca);
Bill Wendling57625a42013-01-25 23:09:36 +00001425
Pete Cooper2777d8872015-05-06 23:19:56 +00001426 return Incompatible;
Bill Wendling57625a42013-01-25 23:09:36 +00001427}
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001428
1429template<typename AttrClass>
1430static bool isEqual(const Function &Caller, const Function &Callee) {
1431 return Caller.getFnAttribute(AttrClass::getKind()) ==
1432 Callee.getFnAttribute(AttrClass::getKind());
1433}
1434
1435/// \brief Compute the logical AND of the attributes of the caller and the
1436/// callee.
1437///
1438/// This function sets the caller's attribute to false if the callee's attribute
1439/// is false.
1440template<typename AttrClass>
1441static void setAND(Function &Caller, const Function &Callee) {
1442 if (AttrClass::isSet(Caller, AttrClass::getKind()) &&
1443 !AttrClass::isSet(Callee, AttrClass::getKind()))
1444 AttrClass::set(Caller, AttrClass::getKind(), false);
1445}
1446
1447/// \brief Compute the logical OR of the attributes of the caller and the
1448/// callee.
1449///
1450/// This function sets the caller's attribute to true if the callee's attribute
1451/// is true.
1452template<typename AttrClass>
1453static void setOR(Function &Caller, const Function &Callee) {
1454 if (!AttrClass::isSet(Caller, AttrClass::getKind()) &&
1455 AttrClass::isSet(Callee, AttrClass::getKind()))
1456 AttrClass::set(Caller, AttrClass::getKind(), true);
1457}
1458
1459/// \brief If the inlined function had a higher stack protection level than the
1460/// calling function, then bump up the caller's stack protection level.
1461static void adjustCallerSSPLevel(Function &Caller, const Function &Callee) {
1462 // If upgrading the SSP attribute, clear out the old SSP Attributes first.
1463 // Having multiple SSP attributes doesn't actually hurt, but it adds useless
1464 // clutter to the IR.
1465 AttrBuilder B;
1466 B.addAttribute(Attribute::StackProtect)
1467 .addAttribute(Attribute::StackProtectStrong)
1468 .addAttribute(Attribute::StackProtectReq);
1469 AttributeSet OldSSPAttr = AttributeSet::get(Caller.getContext(),
1470 AttributeSet::FunctionIndex,
1471 B);
1472
1473 if (Callee.hasFnAttribute(Attribute::SafeStack)) {
1474 Caller.removeAttributes(AttributeSet::FunctionIndex, OldSSPAttr);
1475 Caller.addFnAttr(Attribute::SafeStack);
1476 } else if (Callee.hasFnAttribute(Attribute::StackProtectReq) &&
1477 !Caller.hasFnAttribute(Attribute::SafeStack)) {
1478 Caller.removeAttributes(AttributeSet::FunctionIndex, OldSSPAttr);
1479 Caller.addFnAttr(Attribute::StackProtectReq);
1480 } else if (Callee.hasFnAttribute(Attribute::StackProtectStrong) &&
1481 !Caller.hasFnAttribute(Attribute::SafeStack) &&
1482 !Caller.hasFnAttribute(Attribute::StackProtectReq)) {
1483 Caller.removeAttributes(AttributeSet::FunctionIndex, OldSSPAttr);
1484 Caller.addFnAttr(Attribute::StackProtectStrong);
1485 } else if (Callee.hasFnAttribute(Attribute::StackProtect) &&
1486 !Caller.hasFnAttribute(Attribute::SafeStack) &&
1487 !Caller.hasFnAttribute(Attribute::StackProtectReq) &&
1488 !Caller.hasFnAttribute(Attribute::StackProtectStrong))
1489 Caller.addFnAttr(Attribute::StackProtect);
1490}
1491
1492#define GET_ATTR_COMPAT_FUNC
1493#include "AttributesCompatFunc.inc"
1494
1495bool AttributeFuncs::areInlineCompatible(const Function &Caller,
1496 const Function &Callee) {
1497 return hasCompatibleFnAttrs(Caller, Callee);
1498}
1499
1500
1501void AttributeFuncs::mergeAttributesForInlining(Function &Caller,
1502 const Function &Callee) {
1503 mergeFnAttrs(Caller, Callee);
1504}