blob: af5fe85c5c58dfc5adc201644b14cbcbdad89660 [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";
Vaivaswatha Nagarajfb3f4902015-12-16 16:16:19 +0000198 if (hasAttribute(Attribute::InaccessibleMemOnly))
199 return "inaccessiblememonly";
200 if (hasAttribute(Attribute::InaccessibleMemOrArgMemOnly))
201 return "inaccessiblemem_or_argmemonly";
Reid Klecknera534a382013-12-19 02:14:12 +0000202 if (hasAttribute(Attribute::InAlloca))
203 return "inalloca";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000204 if (hasAttribute(Attribute::InlineHint))
205 return "inlinehint";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000206 if (hasAttribute(Attribute::InReg))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000207 return "inreg";
Tom Roeder44cb65f2014-06-05 19:29:43 +0000208 if (hasAttribute(Attribute::JumpTable))
209 return "jumptable";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000210 if (hasAttribute(Attribute::MinSize))
211 return "minsize";
212 if (hasAttribute(Attribute::Naked))
213 return "naked";
214 if (hasAttribute(Attribute::Nest))
215 return "nest";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000216 if (hasAttribute(Attribute::NoAlias))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000217 return "noalias";
Bill Wendling09bd1f72013-02-22 00:12:35 +0000218 if (hasAttribute(Attribute::NoBuiltin))
219 return "nobuiltin";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000220 if (hasAttribute(Attribute::NoCapture))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000221 return "nocapture";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000222 if (hasAttribute(Attribute::NoDuplicate))
223 return "noduplicate";
224 if (hasAttribute(Attribute::NoImplicitFloat))
225 return "noimplicitfloat";
226 if (hasAttribute(Attribute::NoInline))
227 return "noinline";
228 if (hasAttribute(Attribute::NonLazyBind))
229 return "nonlazybind";
Nick Lewyckyd52b1522014-05-20 01:23:40 +0000230 if (hasAttribute(Attribute::NonNull))
231 return "nonnull";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000232 if (hasAttribute(Attribute::NoRedZone))
233 return "noredzone";
234 if (hasAttribute(Attribute::NoReturn))
235 return "noreturn";
James Molloye6f87ca2015-11-06 10:32:53 +0000236 if (hasAttribute(Attribute::NoRecurse))
237 return "norecurse";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000238 if (hasAttribute(Attribute::NoUnwind))
239 return "nounwind";
Andrea Di Biagio377496b2013-08-23 11:53:55 +0000240 if (hasAttribute(Attribute::OptimizeNone))
241 return "optnone";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000242 if (hasAttribute(Attribute::OptimizeForSize))
243 return "optsize";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000244 if (hasAttribute(Attribute::ReadNone))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000245 return "readnone";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000246 if (hasAttribute(Attribute::ReadOnly))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000247 return "readonly";
Stephen Linb8bd2322013-04-20 05:14:40 +0000248 if (hasAttribute(Attribute::Returned))
249 return "returned";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000250 if (hasAttribute(Attribute::ReturnsTwice))
251 return "returns_twice";
252 if (hasAttribute(Attribute::SExt))
253 return "signext";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000254 if (hasAttribute(Attribute::StackProtect))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000255 return "ssp";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000256 if (hasAttribute(Attribute::StackProtectReq))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000257 return "sspreq";
Bill Wendlingd154e2832013-01-23 06:41:41 +0000258 if (hasAttribute(Attribute::StackProtectStrong))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000259 return "sspstrong";
Peter Collingbourne82437bf2015-06-15 21:07:11 +0000260 if (hasAttribute(Attribute::SafeStack))
261 return "safestack";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000262 if (hasAttribute(Attribute::StructRet))
263 return "sret";
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000264 if (hasAttribute(Attribute::SanitizeThread))
265 return "sanitize_thread";
266 if (hasAttribute(Attribute::SanitizeMemory))
267 return "sanitize_memory";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000268 if (hasAttribute(Attribute::UWTable))
269 return "uwtable";
270 if (hasAttribute(Attribute::ZExt))
271 return "zeroext";
Diego Novilloc6399532013-05-24 12:26:52 +0000272 if (hasAttribute(Attribute::Cold))
273 return "cold";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000274
275 // FIXME: These should be output like this:
276 //
277 // align=4
278 // alignstack=8
279 //
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000280 if (hasAttribute(Attribute::Alignment)) {
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000281 std::string Result;
Bill Wendling829b4782013-02-11 08:43:33 +0000282 Result += "align";
283 Result += (InAttrGrp) ? "=" : " ";
Bill Wendling3f12ac22013-02-05 22:37:24 +0000284 Result += utostr(getValueAsInt());
285 return Result;
286 }
Bill Wendling829b4782013-02-11 08:43:33 +0000287
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000288 auto AttrWithBytesToString = [&](const char *Name) {
Bill Wendling3f12ac22013-02-05 22:37:24 +0000289 std::string Result;
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000290 Result += Name;
Bill Wendling829b4782013-02-11 08:43:33 +0000291 if (InAttrGrp) {
292 Result += "=";
293 Result += utostr(getValueAsInt());
294 } else {
295 Result += "(";
296 Result += utostr(getValueAsInt());
297 Result += ")";
298 }
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000299 return Result;
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000300 };
Bill Wendling9c2eba92013-01-31 20:59:05 +0000301
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000302 if (hasAttribute(Attribute::StackAlignment))
303 return AttrWithBytesToString("alignstack");
304
305 if (hasAttribute(Attribute::Dereferenceable))
306 return AttrWithBytesToString("dereferenceable");
307
308 if (hasAttribute(Attribute::DereferenceableOrNull))
309 return AttrWithBytesToString("dereferenceable_or_null");
Hal Finkelb0407ba2014-07-18 15:51:28 +0000310
Bill Wendling9c2eba92013-01-31 20:59:05 +0000311 // Convert target-dependent attributes to strings of the form:
312 //
313 // "kind"
314 // "kind" = "value"
Bill Wendling9c2eba92013-01-31 20:59:05 +0000315 //
Bill Wendling3f12ac22013-02-05 22:37:24 +0000316 if (isStringAttribute()) {
Bill Wendling9c2eba92013-01-31 20:59:05 +0000317 std::string Result;
Yaron Keren075759a2015-03-30 15:42:36 +0000318 Result += (Twine('"') + getKindAsString() + Twine('"')).str();
Bill Wendling9c2eba92013-01-31 20:59:05 +0000319
Bill Wendling3f12ac22013-02-05 22:37:24 +0000320 StringRef Val = pImpl->getValueAsString();
321 if (Val.empty()) return Result;
Bill Wendling7a33f772013-02-01 22:32:30 +0000322
Yaron Keren075759a2015-03-30 15:42:36 +0000323 Result += ("=\"" + Val + Twine('"')).str();
Bill Wendlingc79cdff2013-02-01 01:04:27 +0000324 return Result;
Bill Wendling9c2eba92013-01-31 20:59:05 +0000325 }
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000326
327 llvm_unreachable("Unknown attribute");
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000328}
329
Bill Wendlingd509a662013-01-29 00:34:06 +0000330bool Attribute::operator<(Attribute A) const {
331 if (!pImpl && !A.pImpl) return false;
332 if (!pImpl) return true;
333 if (!A.pImpl) return false;
334 return *pImpl < *A.pImpl;
335}
336
Bill Wendlingd509a662013-01-29 00:34:06 +0000337//===----------------------------------------------------------------------===//
338// AttributeImpl Definition
339//===----------------------------------------------------------------------===//
340
Eric Christopher0eaa5412014-07-02 22:05:40 +0000341// Pin the vtables to this file.
Alexey Samsonov49109a22013-11-18 09:31:53 +0000342AttributeImpl::~AttributeImpl() {}
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +0000343void EnumAttributeImpl::anchor() {}
Hal Finkele15442c2014-07-18 06:51:55 +0000344void IntAttributeImpl::anchor() {}
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +0000345void StringAttributeImpl::anchor() {}
Alexey Samsonov49109a22013-11-18 09:31:53 +0000346
Bill Wendlingd509a662013-01-29 00:34:06 +0000347bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
Bill Wendling3f12ac22013-02-05 22:37:24 +0000348 if (isStringAttribute()) return false;
349 return getKindAsEnum() == A;
Bill Wendlingd509a662013-01-29 00:34:06 +0000350}
351
Bill Wendling3f12ac22013-02-05 22:37:24 +0000352bool AttributeImpl::hasAttribute(StringRef Kind) const {
353 if (!isStringAttribute()) return false;
354 return getKindAsString() == Kind;
Bill Wendlingd509a662013-01-29 00:34:06 +0000355}
356
Bill Wendling3f12ac22013-02-05 22:37:24 +0000357Attribute::AttrKind AttributeImpl::getKindAsEnum() const {
Hal Finkele15442c2014-07-18 06:51:55 +0000358 assert(isEnumAttribute() || isIntAttribute());
Benjamin Kramer741146b2013-07-11 12:13:16 +0000359 return static_cast<const EnumAttributeImpl *>(this)->getEnumKind();
Bill Wendlingd509a662013-01-29 00:34:06 +0000360}
361
Bill Wendling3f12ac22013-02-05 22:37:24 +0000362uint64_t AttributeImpl::getValueAsInt() const {
Hal Finkele15442c2014-07-18 06:51:55 +0000363 assert(isIntAttribute());
364 return static_cast<const IntAttributeImpl *>(this)->getValue();
Bill Wendlingd509a662013-01-29 00:34:06 +0000365}
366
Bill Wendling3f12ac22013-02-05 22:37:24 +0000367StringRef AttributeImpl::getKindAsString() const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000368 assert(isStringAttribute());
369 return static_cast<const StringAttributeImpl *>(this)->getStringKind();
Bill Wendlingd509a662013-01-29 00:34:06 +0000370}
371
Bill Wendling3f12ac22013-02-05 22:37:24 +0000372StringRef AttributeImpl::getValueAsString() const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000373 assert(isStringAttribute());
374 return static_cast<const StringAttributeImpl *>(this)->getStringValue();
Bill Wendlingd509a662013-01-29 00:34:06 +0000375}
376
377bool AttributeImpl::operator<(const AttributeImpl &AI) const {
Bill Wendlingc79cdff2013-02-01 01:04:27 +0000378 // This sorts the attributes with Attribute::AttrKinds coming first (sorted
379 // relative to their enum value) and then strings.
Bill Wendling26b95752013-02-15 05:25:26 +0000380 if (isEnumAttribute()) {
381 if (AI.isEnumAttribute()) return getKindAsEnum() < AI.getKindAsEnum();
Hal Finkele15442c2014-07-18 06:51:55 +0000382 if (AI.isIntAttribute()) return true;
Bill Wendling26b95752013-02-15 05:25:26 +0000383 if (AI.isStringAttribute()) return true;
384 }
Bill Wendlingc79cdff2013-02-01 01:04:27 +0000385
Hal Finkele15442c2014-07-18 06:51:55 +0000386 if (isIntAttribute()) {
Bill Wendling26b95752013-02-15 05:25:26 +0000387 if (AI.isEnumAttribute()) return false;
Hal Finkele15442c2014-07-18 06:51:55 +0000388 if (AI.isIntAttribute()) return getValueAsInt() < AI.getValueAsInt();
Bill Wendling26b95752013-02-15 05:25:26 +0000389 if (AI.isStringAttribute()) return true;
Bill Wendling3f12ac22013-02-05 22:37:24 +0000390 }
Bill Wendlingd509a662013-01-29 00:34:06 +0000391
Bill Wendling26b95752013-02-15 05:25:26 +0000392 if (AI.isEnumAttribute()) return false;
Hal Finkele15442c2014-07-18 06:51:55 +0000393 if (AI.isIntAttribute()) return false;
Bill Wendling26b95752013-02-15 05:25:26 +0000394 if (getKindAsString() == AI.getKindAsString())
395 return getValueAsString() < AI.getValueAsString();
396 return getKindAsString() < AI.getKindAsString();
Bill Wendlingd509a662013-01-29 00:34:06 +0000397}
398
Bill Wendlingd509a662013-01-29 00:34:06 +0000399uint64_t AttributeImpl::getAttrMask(Attribute::AttrKind Val) {
400 // FIXME: Remove this.
401 switch (Val) {
402 case Attribute::EndAttrKinds:
Bill Wendlingd509a662013-01-29 00:34:06 +0000403 llvm_unreachable("Synthetic enumerators which should never get here");
404
405 case Attribute::None: return 0;
406 case Attribute::ZExt: return 1 << 0;
407 case Attribute::SExt: return 1 << 1;
408 case Attribute::NoReturn: return 1 << 2;
409 case Attribute::InReg: return 1 << 3;
410 case Attribute::StructRet: return 1 << 4;
411 case Attribute::NoUnwind: return 1 << 5;
412 case Attribute::NoAlias: return 1 << 6;
413 case Attribute::ByVal: return 1 << 7;
414 case Attribute::Nest: return 1 << 8;
415 case Attribute::ReadNone: return 1 << 9;
416 case Attribute::ReadOnly: return 1 << 10;
417 case Attribute::NoInline: return 1 << 11;
418 case Attribute::AlwaysInline: return 1 << 12;
419 case Attribute::OptimizeForSize: return 1 << 13;
420 case Attribute::StackProtect: return 1 << 14;
421 case Attribute::StackProtectReq: return 1 << 15;
422 case Attribute::Alignment: return 31 << 16;
423 case Attribute::NoCapture: return 1 << 21;
424 case Attribute::NoRedZone: return 1 << 22;
425 case Attribute::NoImplicitFloat: return 1 << 23;
426 case Attribute::Naked: return 1 << 24;
427 case Attribute::InlineHint: return 1 << 25;
428 case Attribute::StackAlignment: return 7 << 26;
429 case Attribute::ReturnsTwice: return 1 << 29;
430 case Attribute::UWTable: return 1 << 30;
431 case Attribute::NonLazyBind: return 1U << 31;
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000432 case Attribute::SanitizeAddress: return 1ULL << 32;
Bill Wendlingd509a662013-01-29 00:34:06 +0000433 case Attribute::MinSize: return 1ULL << 33;
434 case Attribute::NoDuplicate: return 1ULL << 34;
435 case Attribute::StackProtectStrong: return 1ULL << 35;
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000436 case Attribute::SanitizeThread: return 1ULL << 36;
437 case Attribute::SanitizeMemory: return 1ULL << 37;
Bill Wendling7bfdfe72013-02-22 00:40:12 +0000438 case Attribute::NoBuiltin: return 1ULL << 38;
Stephen Linb8bd2322013-04-20 05:14:40 +0000439 case Attribute::Returned: return 1ULL << 39;
Diego Novilloc6399532013-05-24 12:26:52 +0000440 case Attribute::Cold: return 1ULL << 40;
Michael Gottesman41748d72013-06-27 00:25:01 +0000441 case Attribute::Builtin: return 1ULL << 41;
Andrea Di Biagio377496b2013-08-23 11:53:55 +0000442 case Attribute::OptimizeNone: return 1ULL << 42;
Reid Klecknera534a382013-12-19 02:14:12 +0000443 case Attribute::InAlloca: return 1ULL << 43;
Nick Lewyckyd52b1522014-05-20 01:23:40 +0000444 case Attribute::NonNull: return 1ULL << 44;
Tom Roeder44cb65f2014-06-05 19:29:43 +0000445 case Attribute::JumpTable: return 1ULL << 45;
Owen Anderson85fa7d52015-05-26 23:48:40 +0000446 case Attribute::Convergent: return 1ULL << 46;
Peter Collingbourne82437bf2015-06-15 21:07:11 +0000447 case Attribute::SafeStack: return 1ULL << 47;
James Molloye6f87ca2015-11-06 10:32:53 +0000448 case Attribute::NoRecurse: return 1ULL << 48;
Vaivaswatha Nagarajfb3f4902015-12-16 16:16:19 +0000449 case Attribute::InaccessibleMemOnly: return 1ULL << 49;
450 case Attribute::InaccessibleMemOrArgMemOnly: return 1ULL << 50;
Hal Finkelb0407ba2014-07-18 15:51:28 +0000451 case Attribute::Dereferenceable:
452 llvm_unreachable("dereferenceable attribute not supported in raw format");
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000453 break;
454 case Attribute::DereferenceableOrNull:
455 llvm_unreachable("dereferenceable_or_null attribute not supported in raw "
456 "format");
457 break;
Igor Laevsky39d662f2015-07-11 10:30:36 +0000458 case Attribute::ArgMemOnly:
459 llvm_unreachable("argmemonly attribute not supported in raw format");
460 break;
Bill Wendling25342e12013-02-22 00:50:09 +0000461 }
462 llvm_unreachable("Unsupported attribute type");
Bill Wendlingd509a662013-01-29 00:34:06 +0000463}
464
465//===----------------------------------------------------------------------===//
466// AttributeSetNode Definition
467//===----------------------------------------------------------------------===//
468
469AttributeSetNode *AttributeSetNode::get(LLVMContext &C,
470 ArrayRef<Attribute> Attrs) {
471 if (Attrs.empty())
Craig Topperc6207612014-04-09 06:08:46 +0000472 return nullptr;
Bill Wendlingd509a662013-01-29 00:34:06 +0000473
474 // Otherwise, build a key to look up the existing attributes.
475 LLVMContextImpl *pImpl = C.pImpl;
476 FoldingSetNodeID ID;
477
478 SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end());
Bill Wendling82c2ee62013-02-13 09:26:26 +0000479 array_pod_sort(SortedAttrs.begin(), SortedAttrs.end());
Bill Wendlingd509a662013-01-29 00:34:06 +0000480
George Burgess IV500d3032015-12-16 05:21:02 +0000481 for (Attribute Attr : SortedAttrs)
482 Attr.Profile(ID);
Bill Wendlingd509a662013-01-29 00:34:06 +0000483
484 void *InsertPoint;
485 AttributeSetNode *PA =
486 pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint);
487
488 // If we didn't find any existing attributes of the same shape then create a
489 // new one and insert it.
490 if (!PA) {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000491 // Coallocate entries after the AttributeSetNode itself.
James Y Knightaa365b22015-08-05 22:57:34 +0000492 void *Mem = ::operator new(totalSizeToAlloc<Attribute>(SortedAttrs.size()));
Benjamin Kramer741146b2013-07-11 12:13:16 +0000493 PA = new (Mem) AttributeSetNode(SortedAttrs);
Bill Wendlingd509a662013-01-29 00:34:06 +0000494 pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint);
495 }
496
497 // Return the AttributesListNode that we found or created.
498 return PA;
499}
500
Bill Wendlingbce7b972013-02-13 08:42:21 +0000501bool AttributeSetNode::hasAttribute(StringRef Kind) const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000502 for (iterator I = begin(), E = end(); I != E; ++I)
Bill Wendlingbce7b972013-02-13 08:42:21 +0000503 if (I->hasAttribute(Kind))
504 return true;
505 return false;
506}
507
508Attribute AttributeSetNode::getAttribute(Attribute::AttrKind Kind) const {
Matthias Braun31eeb76f2016-01-29 22:25:13 +0000509 if (hasAttribute(Kind)) {
510 for (iterator I = begin(), E = end(); I != E; ++I)
511 if (I->hasAttribute(Kind))
512 return *I;
513 }
Bill Wendlingbce7b972013-02-13 08:42:21 +0000514 return Attribute();
515}
516
517Attribute AttributeSetNode::getAttribute(StringRef Kind) const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000518 for (iterator I = begin(), E = end(); I != E; ++I)
Bill Wendlingbce7b972013-02-13 08:42:21 +0000519 if (I->hasAttribute(Kind))
520 return *I;
521 return Attribute();
522}
523
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000524unsigned AttributeSetNode::getAlignment() const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000525 for (iterator I = begin(), E = end(); I != E; ++I)
NAKAMURA Takumib0944392013-01-31 03:47:28 +0000526 if (I->hasAttribute(Attribute::Alignment))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000527 return I->getAlignment();
528 return 0;
529}
530
531unsigned AttributeSetNode::getStackAlignment() const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000532 for (iterator I = begin(), E = end(); I != E; ++I)
NAKAMURA Takumib0944392013-01-31 03:47:28 +0000533 if (I->hasAttribute(Attribute::StackAlignment))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000534 return I->getStackAlignment();
535 return 0;
536}
537
Hal Finkelb0407ba2014-07-18 15:51:28 +0000538uint64_t AttributeSetNode::getDereferenceableBytes() const {
539 for (iterator I = begin(), E = end(); I != E; ++I)
540 if (I->hasAttribute(Attribute::Dereferenceable))
541 return I->getDereferenceableBytes();
542 return 0;
543}
544
Sanjoy Das06cf33f2015-05-06 17:41:54 +0000545uint64_t AttributeSetNode::getDereferenceableOrNullBytes() const {
546 for (iterator I = begin(), E = end(); I != E; ++I)
547 if (I->hasAttribute(Attribute::DereferenceableOrNull))
548 return I->getDereferenceableOrNullBytes();
549 return 0;
550}
551
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +0000552std::string AttributeSetNode::getAsString(bool InAttrGrp) const {
Benjamin Kramer0baf8f42013-04-19 11:43:21 +0000553 std::string Str;
Benjamin Kramer741146b2013-07-11 12:13:16 +0000554 for (iterator I = begin(), E = end(); I != E; ++I) {
555 if (I != begin())
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +0000556 Str += ' ';
557 Str += I->getAsString(InAttrGrp);
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000558 }
559 return Str;
560}
561
Bill Wendlingd509a662013-01-29 00:34:06 +0000562//===----------------------------------------------------------------------===//
563// AttributeSetImpl Definition
564//===----------------------------------------------------------------------===//
565
Rafael Espindoladd275302013-04-30 16:53:38 +0000566uint64_t AttributeSetImpl::Raw(unsigned Index) const {
Bill Wendlingd509a662013-01-29 00:34:06 +0000567 for (unsigned I = 0, E = getNumAttributes(); I != E; ++I) {
568 if (getSlotIndex(I) != Index) continue;
Benjamin Kramer741146b2013-07-11 12:13:16 +0000569 const AttributeSetNode *ASN = getSlotNode(I);
Bill Wendling91226182013-02-02 00:52:44 +0000570 uint64_t Mask = 0;
Bill Wendlingd509a662013-01-29 00:34:06 +0000571
Benjamin Kramer741146b2013-07-11 12:13:16 +0000572 for (AttributeSetNode::iterator II = ASN->begin(),
Bill Wendling91226182013-02-02 00:52:44 +0000573 IE = ASN->end(); II != IE; ++II) {
574 Attribute Attr = *II;
Bill Wendlingd746e402013-02-10 23:18:05 +0000575
576 // This cannot handle string attributes.
577 if (Attr.isStringAttribute()) continue;
578
Bill Wendling3f12ac22013-02-05 22:37:24 +0000579 Attribute::AttrKind Kind = Attr.getKindAsEnum();
Bill Wendling91226182013-02-02 00:52:44 +0000580
Bill Wendling3f12ac22013-02-05 22:37:24 +0000581 if (Kind == Attribute::Alignment)
Bill Wendling91226182013-02-02 00:52:44 +0000582 Mask |= (Log2_32(ASN->getAlignment()) + 1) << 16;
Bill Wendling3f12ac22013-02-05 22:37:24 +0000583 else if (Kind == Attribute::StackAlignment)
Bill Wendling91226182013-02-02 00:52:44 +0000584 Mask |= (Log2_32(ASN->getStackAlignment()) + 1) << 26;
Hal Finkelb0407ba2014-07-18 15:51:28 +0000585 else if (Kind == Attribute::Dereferenceable)
586 llvm_unreachable("dereferenceable not supported in bit mask");
Bill Wendling91226182013-02-02 00:52:44 +0000587 else
Bill Wendling3f12ac22013-02-05 22:37:24 +0000588 Mask |= AttributeImpl::getAttrMask(Kind);
Bill Wendling91226182013-02-02 00:52:44 +0000589 }
590
591 return Mask;
Bill Wendlingd509a662013-01-29 00:34:06 +0000592 }
593
594 return 0;
595}
596
Yaron Kereneb2a2542016-01-29 20:50:44 +0000597LLVM_DUMP_METHOD void AttributeSetImpl::dump() const {
Peter Collingbourneabca2ec2013-08-02 22:34:30 +0000598 AttributeSet(const_cast<AttributeSetImpl *>(this)).dump();
599}
600
Bill Wendlingd509a662013-01-29 00:34:06 +0000601//===----------------------------------------------------------------------===//
602// AttributeSet Construction and Mutation Methods
603//===----------------------------------------------------------------------===//
604
Bill Wendling60011b82013-01-29 01:43:29 +0000605AttributeSet
606AttributeSet::getImpl(LLVMContext &C,
607 ArrayRef<std::pair<unsigned, AttributeSetNode*> > Attrs) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000608 LLVMContextImpl *pImpl = C.pImpl;
609 FoldingSetNodeID ID;
610 AttributeSetImpl::Profile(ID, Attrs);
611
612 void *InsertPoint;
613 AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
614
615 // If we didn't find any existing attributes of the same shape then
616 // create a new one and insert it.
617 if (!PA) {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000618 // Coallocate entries after the AttributeSetImpl itself.
James Y Knightaa365b22015-08-05 22:57:34 +0000619 void *Mem = ::operator new(
620 AttributeSetImpl::totalSizeToAlloc<IndexAttrPair>(Attrs.size()));
Benjamin Kramer741146b2013-07-11 12:13:16 +0000621 PA = new (Mem) AttributeSetImpl(C, Attrs);
Bill Wendlingd509a662013-01-29 00:34:06 +0000622 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
623 }
624
625 // Return the AttributesList that we found or created.
626 return AttributeSet(PA);
627}
628
629AttributeSet AttributeSet::get(LLVMContext &C,
630 ArrayRef<std::pair<unsigned, Attribute> > Attrs){
631 // If there are no attributes then return a null AttributesList pointer.
632 if (Attrs.empty())
633 return AttributeSet();
634
Craig Toppere30b8ca2016-01-03 19:43:40 +0000635 assert(std::is_sorted(Attrs.begin(), Attrs.end(),
636 [](const std::pair<unsigned, Attribute> &LHS,
637 const std::pair<unsigned, Attribute> &RHS) {
638 return LHS.first < RHS.first;
639 }) && "Misordered Attributes list!");
640 assert(std::none_of(Attrs.begin(), Attrs.end(),
641 [](const std::pair<unsigned, Attribute> &Pair) {
642 return Pair.second.hasAttribute(Attribute::None);
643 }) && "Pointless attribute!");
Bill Wendlingd509a662013-01-29 00:34:06 +0000644
645 // Create a vector if (unsigned, AttributeSetNode*) pairs from the attributes
646 // list.
647 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrPairVec;
648 for (ArrayRef<std::pair<unsigned, Attribute> >::iterator I = Attrs.begin(),
649 E = Attrs.end(); I != E; ) {
650 unsigned Index = I->first;
651 SmallVector<Attribute, 4> AttrVec;
NAKAMURA Takumif05d2f22013-01-29 15:18:16 +0000652 while (I != E && I->first == Index) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000653 AttrVec.push_back(I->second);
654 ++I;
655 }
656
657 AttrPairVec.push_back(std::make_pair(Index,
658 AttributeSetNode::get(C, AttrVec)));
659 }
660
661 return getImpl(C, AttrPairVec);
662}
663
664AttributeSet AttributeSet::get(LLVMContext &C,
665 ArrayRef<std::pair<unsigned,
666 AttributeSetNode*> > Attrs) {
667 // If there are no attributes then return a null AttributesList pointer.
668 if (Attrs.empty())
669 return AttributeSet();
670
671 return getImpl(C, Attrs);
672}
673
David Majnemercf63a792014-05-03 23:00:35 +0000674AttributeSet AttributeSet::get(LLVMContext &C, unsigned Index,
675 const AttrBuilder &B) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000676 if (!B.hasAttributes())
677 return AttributeSet();
Bill Wendlingf7134812013-01-29 01:02:03 +0000678
Bill Wendlingae89a0f2013-02-05 23:48:36 +0000679 // Add target-independent attributes.
Bill Wendlingf7134812013-01-29 01:02:03 +0000680 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
Benjamin Kramer45e7d532013-02-16 19:13:18 +0000681 for (Attribute::AttrKind Kind = Attribute::None;
Benjamin Kramer6f37dac2013-02-16 19:22:28 +0000682 Kind != Attribute::EndAttrKinds; Kind = Attribute::AttrKind(Kind + 1)) {
Benjamin Kramer45e7d532013-02-16 19:13:18 +0000683 if (!B.contains(Kind))
684 continue;
685
George Burgess IV500d3032015-12-16 05:21:02 +0000686 Attribute Attr;
687 switch (Kind) {
688 case Attribute::Alignment:
689 Attr = Attribute::getWithAlignment(C, B.getAlignment());
690 break;
691 case Attribute::StackAlignment:
692 Attr = Attribute::getWithStackAlignment(C, B.getStackAlignment());
693 break;
694 case Attribute::Dereferenceable:
695 Attr = Attribute::getWithDereferenceableBytes(
696 C, B.getDereferenceableBytes());
697 break;
698 case Attribute::DereferenceableOrNull:
699 Attr = Attribute::getWithDereferenceableOrNullBytes(
700 C, B.getDereferenceableOrNullBytes());
701 break;
702 default:
703 Attr = Attribute::get(C, Kind);
704 }
705 Attrs.push_back(std::make_pair(Index, Attr));
Bill Wendlingf7134812013-01-29 01:02:03 +0000706 }
707
Bill Wendlingae89a0f2013-02-05 23:48:36 +0000708 // Add target-dependent (string) attributes.
David Majnemercf63a792014-05-03 23:00:35 +0000709 for (const AttrBuilder::td_type &TDA : B.td_attrs())
710 Attrs.push_back(
711 std::make_pair(Index, Attribute::get(C, TDA.first, TDA.second)));
Bill Wendlingae89a0f2013-02-05 23:48:36 +0000712
Bill Wendlingf7134812013-01-29 01:02:03 +0000713 return get(C, Attrs);
Bill Wendlingd509a662013-01-29 00:34:06 +0000714}
715
Bill Wendling211316c2013-04-18 20:17:28 +0000716AttributeSet AttributeSet::get(LLVMContext &C, unsigned Index,
Bill Wendlingd509a662013-01-29 00:34:06 +0000717 ArrayRef<Attribute::AttrKind> Kind) {
718 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
George Burgess IV500d3032015-12-16 05:21:02 +0000719 for (Attribute::AttrKind K : Kind)
720 Attrs.push_back(std::make_pair(Index, Attribute::get(C, K)));
Bill Wendlingd509a662013-01-29 00:34:06 +0000721 return get(C, Attrs);
722}
723
724AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<AttributeSet> Attrs) {
725 if (Attrs.empty()) return AttributeSet();
Peter Collingbournebd6c7452013-08-02 22:29:40 +0000726 if (Attrs.size() == 1) return Attrs[0];
Bill Wendlingd509a662013-01-29 00:34:06 +0000727
728 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrNodeVec;
Peter Collingbournebd6c7452013-08-02 22:29:40 +0000729 AttributeSetImpl *A0 = Attrs[0].pImpl;
730 if (A0)
731 AttrNodeVec.append(A0->getNode(0), A0->getNode(A0->getNumAttributes()));
732 // Copy all attributes from Attrs into AttrNodeVec while keeping AttrNodeVec
733 // ordered by index. Because we know that each list in Attrs is ordered by
734 // index we only need to merge each successive list in rather than doing a
735 // full sort.
736 for (unsigned I = 1, E = Attrs.size(); I != E; ++I) {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000737 AttributeSetImpl *AS = Attrs[I].pImpl;
738 if (!AS) continue;
Peter Collingbournebd6c7452013-08-02 22:29:40 +0000739 SmallVector<std::pair<unsigned, AttributeSetNode *>, 8>::iterator
740 ANVI = AttrNodeVec.begin(), ANVE;
James Y Knightaa365b22015-08-05 22:57:34 +0000741 for (const IndexAttrPair *AI = AS->getNode(0),
742 *AE = AS->getNode(AS->getNumAttributes());
Peter Collingbournebd6c7452013-08-02 22:29:40 +0000743 AI != AE; ++AI) {
744 ANVE = AttrNodeVec.end();
745 while (ANVI != ANVE && ANVI->first <= AI->first)
746 ++ANVI;
747 ANVI = AttrNodeVec.insert(ANVI, *AI) + 1;
748 }
Bill Wendlingd509a662013-01-29 00:34:06 +0000749 }
750
751 return getImpl(C, AttrNodeVec);
752}
753
Bill Wendling211316c2013-04-18 20:17:28 +0000754AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
Bill Wendlingd509a662013-01-29 00:34:06 +0000755 Attribute::AttrKind Attr) const {
Bill Wendling211316c2013-04-18 20:17:28 +0000756 if (hasAttribute(Index, Attr)) return *this;
757 return addAttributes(C, Index, AttributeSet::get(C, Index, Attr));
Bill Wendlingd509a662013-01-29 00:34:06 +0000758}
759
Bill Wendling211316c2013-04-18 20:17:28 +0000760AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
Reed Kotler795c7b42013-03-13 20:20:08 +0000761 StringRef Kind) const {
762 llvm::AttrBuilder B;
763 B.addAttribute(Kind);
Bill Wendling211316c2013-04-18 20:17:28 +0000764 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
Reed Kotler795c7b42013-03-13 20:20:08 +0000765}
766
Bill Wendling3b2f6102013-07-25 18:34:24 +0000767AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
768 StringRef Kind, StringRef Value) const {
769 llvm::AttrBuilder B;
770 B.addAttribute(Kind, Value);
771 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
772}
773
Akira Hatanaka237916b2015-12-02 06:58:49 +0000774AttributeSet AttributeSet::addAttribute(LLVMContext &C,
775 ArrayRef<unsigned> Indices,
776 Attribute A) const {
777 unsigned I = 0, E = pImpl ? pImpl->getNumAttributes() : 0;
778 auto IdxI = Indices.begin(), IdxE = Indices.end();
779 SmallVector<AttributeSet, 4> AttrSet;
780
781 while (I != E && IdxI != IdxE) {
782 if (getSlotIndex(I) < *IdxI)
783 AttrSet.emplace_back(getSlotAttributes(I++));
784 else if (getSlotIndex(I) > *IdxI)
785 AttrSet.emplace_back(AttributeSet::get(C, std::make_pair(*IdxI++, A)));
786 else {
787 AttrBuilder B(getSlotAttributes(I), *IdxI);
788 B.addAttribute(A);
789 AttrSet.emplace_back(AttributeSet::get(C, *IdxI, B));
790 ++I;
791 ++IdxI;
792 }
793 }
794
795 while (I != E)
796 AttrSet.emplace_back(getSlotAttributes(I++));
797
798 while (IdxI != IdxE)
799 AttrSet.emplace_back(AttributeSet::get(C, std::make_pair(*IdxI++, A)));
800
801 return get(C, AttrSet);
802}
803
Bill Wendling211316c2013-04-18 20:17:28 +0000804AttributeSet AttributeSet::addAttributes(LLVMContext &C, unsigned Index,
Bill Wendlingd509a662013-01-29 00:34:06 +0000805 AttributeSet Attrs) const {
806 if (!pImpl) return Attrs;
807 if (!Attrs.pImpl) return *this;
808
809#ifndef NDEBUG
810 // FIXME it is not obvious how this should work for alignment. For now, say
811 // we can't change a known alignment.
Bill Wendling211316c2013-04-18 20:17:28 +0000812 unsigned OldAlign = getParamAlignment(Index);
813 unsigned NewAlign = Attrs.getParamAlignment(Index);
Bill Wendlingd509a662013-01-29 00:34:06 +0000814 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
815 "Attempt to change alignment!");
816#endif
817
818 // Add the attribute slots before the one we're trying to add.
819 SmallVector<AttributeSet, 4> AttrSet;
820 uint64_t NumAttrs = pImpl->getNumAttributes();
821 AttributeSet AS;
822 uint64_t LastIndex = 0;
823 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
Bill Wendling211316c2013-04-18 20:17:28 +0000824 if (getSlotIndex(I) >= Index) {
825 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
Bill Wendlingd509a662013-01-29 00:34:06 +0000826 break;
827 }
828 LastIndex = I + 1;
829 AttrSet.push_back(getSlotAttributes(I));
830 }
831
832 // Now add the attribute into the correct slot. There may already be an
833 // AttributeSet there.
Bill Wendling211316c2013-04-18 20:17:28 +0000834 AttrBuilder B(AS, Index);
Bill Wendlingd509a662013-01-29 00:34:06 +0000835
836 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
Bill Wendling211316c2013-04-18 20:17:28 +0000837 if (Attrs.getSlotIndex(I) == Index) {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000838 for (AttributeSetImpl::iterator II = Attrs.pImpl->begin(I),
Bill Wendlingd509a662013-01-29 00:34:06 +0000839 IE = Attrs.pImpl->end(I); II != IE; ++II)
Bill Wendling23804da2013-01-31 23:38:01 +0000840 B.addAttribute(*II);
Bill Wendlingd509a662013-01-29 00:34:06 +0000841 break;
842 }
843
Bill Wendling211316c2013-04-18 20:17:28 +0000844 AttrSet.push_back(AttributeSet::get(C, Index, B));
Bill Wendlingd509a662013-01-29 00:34:06 +0000845
846 // Add the remaining attribute slots.
847 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
848 AttrSet.push_back(getSlotAttributes(I));
849
850 return get(C, AttrSet);
851}
852
Bill Wendling211316c2013-04-18 20:17:28 +0000853AttributeSet AttributeSet::removeAttribute(LLVMContext &C, unsigned Index,
Bill Wendlingd509a662013-01-29 00:34:06 +0000854 Attribute::AttrKind Attr) const {
Bill Wendling211316c2013-04-18 20:17:28 +0000855 if (!hasAttribute(Index, Attr)) return *this;
856 return removeAttributes(C, Index, AttributeSet::get(C, Index, Attr));
Bill Wendlingd509a662013-01-29 00:34:06 +0000857}
858
Bill Wendling211316c2013-04-18 20:17:28 +0000859AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Index,
Bill Wendlingd509a662013-01-29 00:34:06 +0000860 AttributeSet Attrs) const {
861 if (!pImpl) return AttributeSet();
862 if (!Attrs.pImpl) return *this;
863
Pete Cooper67cf9a72015-11-19 05:56:52 +0000864 // FIXME it is not obvious how this should work for alignment.
865 // For now, say we can't pass in alignment, which no current use does.
866 assert(!Attrs.hasAttribute(Index, Attribute::Alignment) &&
867 "Attempt to change alignment!");
868
Bill Wendlingd509a662013-01-29 00:34:06 +0000869 // Add the attribute slots before the one we're trying to add.
870 SmallVector<AttributeSet, 4> AttrSet;
871 uint64_t NumAttrs = pImpl->getNumAttributes();
872 AttributeSet AS;
873 uint64_t LastIndex = 0;
874 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
Bill Wendling211316c2013-04-18 20:17:28 +0000875 if (getSlotIndex(I) >= Index) {
876 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
Bill Wendlingd509a662013-01-29 00:34:06 +0000877 break;
878 }
879 LastIndex = I + 1;
880 AttrSet.push_back(getSlotAttributes(I));
881 }
882
Bill Wendlingd2196752013-01-30 23:07:40 +0000883 // Now remove the attribute from the correct slot. There may already be an
Bill Wendlingd509a662013-01-29 00:34:06 +0000884 // AttributeSet there.
Bill Wendling211316c2013-04-18 20:17:28 +0000885 AttrBuilder B(AS, Index);
Bill Wendlingd509a662013-01-29 00:34:06 +0000886
887 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
Bill Wendling211316c2013-04-18 20:17:28 +0000888 if (Attrs.getSlotIndex(I) == Index) {
889 B.removeAttributes(Attrs.pImpl->getSlotAttributes(I), Index);
Bill Wendlingd509a662013-01-29 00:34:06 +0000890 break;
891 }
892
Bill Wendling211316c2013-04-18 20:17:28 +0000893 AttrSet.push_back(AttributeSet::get(C, Index, B));
Bill Wendlingd509a662013-01-29 00:34:06 +0000894
895 // Add the remaining attribute slots.
896 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
897 AttrSet.push_back(getSlotAttributes(I));
898
899 return get(C, AttrSet);
900}
901
Pete Cooperd2a44612015-05-06 23:19:43 +0000902AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Index,
903 const AttrBuilder &Attrs) const {
904 if (!pImpl) return AttributeSet();
905
906 // FIXME it is not obvious how this should work for alignment.
907 // For now, say we can't pass in alignment, which no current use does.
908 assert(!Attrs.hasAlignmentAttr() && "Attempt to change alignment!");
909
910 // Add the attribute slots before the one we're trying to add.
911 SmallVector<AttributeSet, 4> AttrSet;
912 uint64_t NumAttrs = pImpl->getNumAttributes();
913 AttributeSet AS;
914 uint64_t LastIndex = 0;
915 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
916 if (getSlotIndex(I) >= Index) {
917 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
918 break;
919 }
920 LastIndex = I + 1;
921 AttrSet.push_back(getSlotAttributes(I));
922 }
923
924 // Now remove the attribute from the correct slot. There may already be an
925 // AttributeSet there.
926 AttrBuilder B(AS, Index);
927 B.remove(Attrs);
928
929 AttrSet.push_back(AttributeSet::get(C, Index, B));
930
931 // Add the remaining attribute slots.
932 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
933 AttrSet.push_back(getSlotAttributes(I));
934
935 return get(C, AttrSet);
936}
937
Ramkumar Ramachandra8fcb4982015-02-14 19:37:54 +0000938AttributeSet AttributeSet::addDereferenceableAttr(LLVMContext &C, unsigned Index,
939 uint64_t Bytes) const {
940 llvm::AttrBuilder B;
941 B.addDereferenceableAttr(Bytes);
942 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
943}
944
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000945AttributeSet AttributeSet::addDereferenceableOrNullAttr(LLVMContext &C,
946 unsigned Index,
947 uint64_t Bytes) const {
948 llvm::AttrBuilder B;
949 B.addDereferenceableOrNullAttr(Bytes);
950 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
951}
952
Bill Wendlingd509a662013-01-29 00:34:06 +0000953//===----------------------------------------------------------------------===//
954// AttributeSet Accessor Methods
955//===----------------------------------------------------------------------===//
956
Bill Wendling5d020a32013-02-10 05:00:40 +0000957LLVMContext &AttributeSet::getContext() const {
958 return pImpl->getContext();
959}
960
Bill Wendling211316c2013-04-18 20:17:28 +0000961AttributeSet AttributeSet::getParamAttributes(unsigned Index) const {
962 return pImpl && hasAttributes(Index) ?
Bill Wendlingd509a662013-01-29 00:34:06 +0000963 AttributeSet::get(pImpl->getContext(),
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000964 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendling211316c2013-04-18 20:17:28 +0000965 std::make_pair(Index, getAttributes(Index)))) :
Bill Wendlingd509a662013-01-29 00:34:06 +0000966 AttributeSet();
967}
968
969AttributeSet AttributeSet::getRetAttributes() const {
970 return pImpl && hasAttributes(ReturnIndex) ?
971 AttributeSet::get(pImpl->getContext(),
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000972 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendlingd509a662013-01-29 00:34:06 +0000973 std::make_pair(ReturnIndex,
974 getAttributes(ReturnIndex)))) :
975 AttributeSet();
976}
977
978AttributeSet AttributeSet::getFnAttributes() const {
979 return pImpl && hasAttributes(FunctionIndex) ?
980 AttributeSet::get(pImpl->getContext(),
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000981 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendlingd509a662013-01-29 00:34:06 +0000982 std::make_pair(FunctionIndex,
983 getAttributes(FunctionIndex)))) :
984 AttributeSet();
985}
986
987bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000988 AttributeSetNode *ASN = getAttributes(Index);
George Burgess IV500d3032015-12-16 05:21:02 +0000989 return ASN && ASN->hasAttribute(Kind);
Bill Wendlingd509a662013-01-29 00:34:06 +0000990}
991
Bill Wendlingbce7b972013-02-13 08:42:21 +0000992bool AttributeSet::hasAttribute(unsigned Index, StringRef Kind) const {
993 AttributeSetNode *ASN = getAttributes(Index);
George Burgess IV500d3032015-12-16 05:21:02 +0000994 return ASN && ASN->hasAttribute(Kind);
Bill Wendlingbce7b972013-02-13 08:42:21 +0000995}
996
Bill Wendlingd509a662013-01-29 00:34:06 +0000997bool AttributeSet::hasAttributes(unsigned Index) 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->hasAttributes();
Bill Wendlingd509a662013-01-29 00:34:06 +00001000}
1001
Bill Wendlingd509a662013-01-29 00:34:06 +00001002bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr) const {
Craig Topperc6207612014-04-09 06:08:46 +00001003 if (!pImpl) return false;
Bill Wendlingd509a662013-01-29 00:34:06 +00001004
1005 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
Benjamin Kramer741146b2013-07-11 12:13:16 +00001006 for (AttributeSetImpl::iterator II = pImpl->begin(I),
Bill Wendlingd509a662013-01-29 00:34:06 +00001007 IE = pImpl->end(I); II != IE; ++II)
NAKAMURA Takumib0944392013-01-31 03:47:28 +00001008 if (II->hasAttribute(Attr))
Bill Wendlingd509a662013-01-29 00:34:06 +00001009 return true;
1010
1011 return false;
1012}
1013
Bill Wendlingbce7b972013-02-13 08:42:21 +00001014Attribute AttributeSet::getAttribute(unsigned Index,
1015 Attribute::AttrKind Kind) const {
1016 AttributeSetNode *ASN = getAttributes(Index);
1017 return ASN ? ASN->getAttribute(Kind) : Attribute();
1018}
1019
1020Attribute AttributeSet::getAttribute(unsigned Index,
1021 StringRef Kind) const {
1022 AttributeSetNode *ASN = getAttributes(Index);
1023 return ASN ? ASN->getAttribute(Kind) : Attribute();
1024}
1025
Bill Wendlingf2955aa2013-01-29 03:20:31 +00001026unsigned AttributeSet::getParamAlignment(unsigned Index) const {
1027 AttributeSetNode *ASN = getAttributes(Index);
1028 return ASN ? ASN->getAlignment() : 0;
Bill Wendlingd509a662013-01-29 00:34:06 +00001029}
1030
1031unsigned AttributeSet::getStackAlignment(unsigned Index) const {
Bill Wendlingf2955aa2013-01-29 03:20:31 +00001032 AttributeSetNode *ASN = getAttributes(Index);
1033 return ASN ? ASN->getStackAlignment() : 0;
Bill Wendlingd509a662013-01-29 00:34:06 +00001034}
1035
Hal Finkelb0407ba2014-07-18 15:51:28 +00001036uint64_t AttributeSet::getDereferenceableBytes(unsigned Index) const {
1037 AttributeSetNode *ASN = getAttributes(Index);
1038 return ASN ? ASN->getDereferenceableBytes() : 0;
1039}
1040
Sanjoy Das06cf33f2015-05-06 17:41:54 +00001041uint64_t AttributeSet::getDereferenceableOrNullBytes(unsigned Index) const {
1042 AttributeSetNode *ASN = getAttributes(Index);
1043 return ASN ? ASN->getDereferenceableOrNullBytes() : 0;
1044}
1045
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +00001046std::string AttributeSet::getAsString(unsigned Index,
Bill Wendling829b4782013-02-11 08:43:33 +00001047 bool InAttrGrp) const {
Bill Wendlingf2955aa2013-01-29 03:20:31 +00001048 AttributeSetNode *ASN = getAttributes(Index);
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +00001049 return ASN ? ASN->getAsString(InAttrGrp) : std::string("");
Bill Wendlingd509a662013-01-29 00:34:06 +00001050}
1051
Bill Wendling211316c2013-04-18 20:17:28 +00001052AttributeSetNode *AttributeSet::getAttributes(unsigned Index) const {
Craig Topperc6207612014-04-09 06:08:46 +00001053 if (!pImpl) return nullptr;
Bill Wendlingd509a662013-01-29 00:34:06 +00001054
Bill Wendlingf2955aa2013-01-29 03:20:31 +00001055 // Loop through to find the attribute node we want.
1056 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
Bill Wendling211316c2013-04-18 20:17:28 +00001057 if (pImpl->getSlotIndex(I) == Index)
Bill Wendlingf2955aa2013-01-29 03:20:31 +00001058 return pImpl->getSlotNode(I);
Bill Wendlingd509a662013-01-29 00:34:06 +00001059
Craig Topperc6207612014-04-09 06:08:46 +00001060 return nullptr;
Bill Wendlingd509a662013-01-29 00:34:06 +00001061}
1062
Bill Wendling211316c2013-04-18 20:17:28 +00001063AttributeSet::iterator AttributeSet::begin(unsigned Slot) const {
Bill Wendlinga9174862013-01-31 23:53:05 +00001064 if (!pImpl)
1065 return ArrayRef<Attribute>().begin();
Bill Wendling211316c2013-04-18 20:17:28 +00001066 return pImpl->begin(Slot);
Bill Wendlinga9174862013-01-31 23:53:05 +00001067}
1068
Bill Wendling211316c2013-04-18 20:17:28 +00001069AttributeSet::iterator AttributeSet::end(unsigned Slot) const {
Bill Wendlinga9174862013-01-31 23:53:05 +00001070 if (!pImpl)
1071 return ArrayRef<Attribute>().end();
Bill Wendling211316c2013-04-18 20:17:28 +00001072 return pImpl->end(Slot);
Bill Wendlinga9174862013-01-31 23:53:05 +00001073}
1074
Bill Wendlingd509a662013-01-29 00:34:06 +00001075//===----------------------------------------------------------------------===//
1076// AttributeSet Introspection Methods
1077//===----------------------------------------------------------------------===//
1078
Bill Wendlingd509a662013-01-29 00:34:06 +00001079unsigned AttributeSet::getNumSlots() const {
1080 return pImpl ? pImpl->getNumAttributes() : 0;
1081}
1082
Rafael Espindoladd275302013-04-30 16:53:38 +00001083unsigned AttributeSet::getSlotIndex(unsigned Slot) const {
Bill Wendlingd509a662013-01-29 00:34:06 +00001084 assert(pImpl && Slot < pImpl->getNumAttributes() &&
1085 "Slot # out of range!");
1086 return pImpl->getSlotIndex(Slot);
1087}
1088
1089AttributeSet AttributeSet::getSlotAttributes(unsigned Slot) const {
1090 assert(pImpl && Slot < pImpl->getNumAttributes() &&
1091 "Slot # out of range!");
1092 return pImpl->getSlotAttributes(Slot);
1093}
1094
1095uint64_t AttributeSet::Raw(unsigned Index) const {
1096 // FIXME: Remove this.
1097 return pImpl ? pImpl->Raw(Index) : 0;
1098}
1099
Yaron Kereneb2a2542016-01-29 20:50:44 +00001100LLVM_DUMP_METHOD void AttributeSet::dump() const {
Bill Wendlingd509a662013-01-29 00:34:06 +00001101 dbgs() << "PAL[\n";
1102
1103 for (unsigned i = 0, e = getNumSlots(); i < e; ++i) {
1104 uint64_t Index = getSlotIndex(i);
1105 dbgs() << " { ";
1106 if (Index == ~0U)
1107 dbgs() << "~0U";
1108 else
1109 dbgs() << Index;
1110 dbgs() << " => " << getAsString(Index) << " }\n";
1111 }
1112
1113 dbgs() << "]\n";
1114}
1115
Bill Wendlingabf3feb2012-10-05 06:44:41 +00001116//===----------------------------------------------------------------------===//
Bill Wendlingcd330342013-01-04 23:27:34 +00001117// AttrBuilder Method Implementations
Bill Wendlingabf3feb2012-10-05 06:44:41 +00001118//===----------------------------------------------------------------------===//
1119
Bill Wendling211316c2013-04-18 20:17:28 +00001120AttrBuilder::AttrBuilder(AttributeSet AS, unsigned Index)
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001121 : Attrs(0), Alignment(0), StackAlignment(0), DerefBytes(0),
1122 DerefOrNullBytes(0) {
Bill Wendling56b0b2a2013-01-27 21:23:46 +00001123 AttributeSetImpl *pImpl = AS.pImpl;
Bill Wendling096f5442013-01-07 08:24:35 +00001124 if (!pImpl) return;
1125
Bill Wendling9eb689c2013-01-28 00:21:34 +00001126 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I) {
Bill Wendling211316c2013-04-18 20:17:28 +00001127 if (pImpl->getSlotIndex(I) != Index) continue;
Bill Wendling096f5442013-01-07 08:24:35 +00001128
Benjamin Kramer741146b2013-07-11 12:13:16 +00001129 for (AttributeSetImpl::iterator II = pImpl->begin(I),
Bill Wendling9eb689c2013-01-28 00:21:34 +00001130 IE = pImpl->end(I); II != IE; ++II)
Bill Wendling23804da2013-01-31 23:38:01 +00001131 addAttribute(*II);
Bill Wendling9eb689c2013-01-28 00:21:34 +00001132
1133 break;
1134 }
Bill Wendling096f5442013-01-07 08:24:35 +00001135}
1136
Bill Wendlingcd330342013-01-04 23:27:34 +00001137void AttrBuilder::clear() {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001138 Attrs.reset();
Sanjoy Das88d0fde2015-09-03 22:27:42 +00001139 TargetDepAttrs.clear();
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001140 Alignment = StackAlignment = DerefBytes = DerefOrNullBytes = 0;
Bill Wendlingcd330342013-01-04 23:27:34 +00001141}
1142
1143AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001144 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
Bill Wendling1c7cc8a2013-01-31 23:16:25 +00001145 assert(Val != Attribute::Alignment && Val != Attribute::StackAlignment &&
Hal Finkelb0407ba2014-07-18 15:51:28 +00001146 Val != Attribute::Dereferenceable &&
1147 "Adding integer attribute without adding a value!");
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001148 Attrs[Val] = true;
Bill Wendling7c04e042012-10-09 19:01:18 +00001149 return *this;
Bill Wendlingabf3feb2012-10-05 06:44:41 +00001150}
1151
Bill Wendling23804da2013-01-31 23:38:01 +00001152AttrBuilder &AttrBuilder::addAttribute(Attribute Attr) {
Bill Wendling0a437302013-02-10 10:13:23 +00001153 if (Attr.isStringAttribute()) {
1154 addAttribute(Attr.getKindAsString(), Attr.getValueAsString());
1155 return *this;
1156 }
1157
Bill Wendling3f12ac22013-02-05 22:37:24 +00001158 Attribute::AttrKind Kind = Attr.getKindAsEnum();
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001159 Attrs[Kind] = true;
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001160
Bill Wendling3f12ac22013-02-05 22:37:24 +00001161 if (Kind == Attribute::Alignment)
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001162 Alignment = Attr.getAlignment();
Bill Wendling3f12ac22013-02-05 22:37:24 +00001163 else if (Kind == Attribute::StackAlignment)
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001164 StackAlignment = Attr.getStackAlignment();
Hal Finkelb0407ba2014-07-18 15:51:28 +00001165 else if (Kind == Attribute::Dereferenceable)
1166 DerefBytes = Attr.getDereferenceableBytes();
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001167 else if (Kind == Attribute::DereferenceableOrNull)
1168 DerefOrNullBytes = Attr.getDereferenceableOrNullBytes();
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001169 return *this;
1170}
1171
Bill Wendlingb9c5b1a2013-02-05 08:09:32 +00001172AttrBuilder &AttrBuilder::addAttribute(StringRef A, StringRef V) {
1173 TargetDepAttrs[A] = V;
1174 return *this;
1175}
1176
Bill Wendling23804da2013-01-31 23:38:01 +00001177AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001178 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
1179 Attrs[Val] = false;
Bill Wendling23804da2013-01-31 23:38:01 +00001180
1181 if (Val == Attribute::Alignment)
1182 Alignment = 0;
1183 else if (Val == Attribute::StackAlignment)
1184 StackAlignment = 0;
Hal Finkelb0407ba2014-07-18 15:51:28 +00001185 else if (Val == Attribute::Dereferenceable)
1186 DerefBytes = 0;
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001187 else if (Val == Attribute::DereferenceableOrNull)
1188 DerefOrNullBytes = 0;
Bill Wendling23804da2013-01-31 23:38:01 +00001189
1190 return *this;
1191}
1192
Bill Wendlingd2196752013-01-30 23:07:40 +00001193AttrBuilder &AttrBuilder::removeAttributes(AttributeSet A, uint64_t Index) {
Bill Wendling211316c2013-04-18 20:17:28 +00001194 unsigned Slot = ~0U;
Bill Wendlingf1c94e32013-02-01 00:13:50 +00001195 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
1196 if (A.getSlotIndex(I) == Index) {
Bill Wendling211316c2013-04-18 20:17:28 +00001197 Slot = I;
Bill Wendlingf1c94e32013-02-01 00:13:50 +00001198 break;
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001199 }
Bill Wendlingf1c94e32013-02-01 00:13:50 +00001200
Bill Wendling211316c2013-04-18 20:17:28 +00001201 assert(Slot != ~0U && "Couldn't find index in AttributeSet!");
Bill Wendlingf1c94e32013-02-01 00:13:50 +00001202
Bill Wendling211316c2013-04-18 20:17:28 +00001203 for (AttributeSet::iterator I = A.begin(Slot), E = A.end(Slot); I != E; ++I) {
Bill Wendling7cde51d2013-02-12 07:56:49 +00001204 Attribute Attr = *I;
Hal Finkele15442c2014-07-18 06:51:55 +00001205 if (Attr.isEnumAttribute() || Attr.isIntAttribute()) {
George Burgess IV500d3032015-12-16 05:21:02 +00001206 removeAttribute(Attr.getKindAsEnum());
Bill Wendling7cde51d2013-02-12 07:56:49 +00001207 } else {
1208 assert(Attr.isStringAttribute() && "Invalid attribute type!");
George Burgess IV500d3032015-12-16 05:21:02 +00001209 removeAttribute(Attr.getKindAsString());
Bill Wendling7cde51d2013-02-12 07:56:49 +00001210 }
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001211 }
1212
1213 return *this;
1214}
1215
Bill Wendlingb9c5b1a2013-02-05 08:09:32 +00001216AttrBuilder &AttrBuilder::removeAttribute(StringRef A) {
1217 std::map<std::string, std::string>::iterator I = TargetDepAttrs.find(A);
1218 if (I != TargetDepAttrs.end())
1219 TargetDepAttrs.erase(I);
1220 return *this;
1221}
1222
Bill Wendling50d27842012-10-15 20:35:56 +00001223AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
Bill Wendlingabd5ba22012-10-14 03:58:29 +00001224 if (Align == 0) return *this;
Bill Wendlingcd330342013-01-04 23:27:34 +00001225
Bill Wendlingabf3feb2012-10-05 06:44:41 +00001226 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1227 assert(Align <= 0x40000000 && "Alignment too large.");
Bill Wendlingcd330342013-01-04 23:27:34 +00001228
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001229 Attrs[Attribute::Alignment] = true;
Bill Wendlingcd330342013-01-04 23:27:34 +00001230 Alignment = Align;
Bill Wendlingabd5ba22012-10-14 03:58:29 +00001231 return *this;
Chris Lattner3e13b8c2008-01-02 23:42:30 +00001232}
1233
Bill Wendlingcd330342013-01-04 23:27:34 +00001234AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
1235 // Default alignment, allow the target to define how to align it.
1236 if (Align == 0) return *this;
1237
1238 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1239 assert(Align <= 0x100 && "Alignment too large.");
1240
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001241 Attrs[Attribute::StackAlignment] = true;
Bill Wendlingcd330342013-01-04 23:27:34 +00001242 StackAlignment = Align;
1243 return *this;
1244}
1245
Hal Finkelb0407ba2014-07-18 15:51:28 +00001246AttrBuilder &AttrBuilder::addDereferenceableAttr(uint64_t Bytes) {
1247 if (Bytes == 0) return *this;
1248
1249 Attrs[Attribute::Dereferenceable] = true;
1250 DerefBytes = Bytes;
1251 return *this;
1252}
1253
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001254AttrBuilder &AttrBuilder::addDereferenceableOrNullAttr(uint64_t Bytes) {
1255 if (Bytes == 0)
1256 return *this;
1257
1258 Attrs[Attribute::DereferenceableOrNull] = true;
1259 DerefOrNullBytes = Bytes;
1260 return *this;
1261}
1262
Bill Wendlinge2614922013-02-06 01:16:00 +00001263AttrBuilder &AttrBuilder::merge(const AttrBuilder &B) {
1264 // FIXME: What if both have alignments, but they don't match?!
1265 if (!Alignment)
1266 Alignment = B.Alignment;
1267
1268 if (!StackAlignment)
1269 StackAlignment = B.StackAlignment;
1270
Hal Finkelb0407ba2014-07-18 15:51:28 +00001271 if (!DerefBytes)
1272 DerefBytes = B.DerefBytes;
1273
Pete Cooperd2a44612015-05-06 23:19:43 +00001274 if (!DerefOrNullBytes)
1275 DerefOrNullBytes = B.DerefOrNullBytes;
1276
Benjamin Kramer45e7d532013-02-16 19:13:18 +00001277 Attrs |= B.Attrs;
Bill Wendlinge2614922013-02-06 01:16:00 +00001278
Pete Cooperd2a44612015-05-06 23:19:43 +00001279 for (auto I : B.td_attrs())
1280 TargetDepAttrs[I.first] = I.second;
Bill Wendlinge2614922013-02-06 01:16:00 +00001281
1282 return *this;
1283}
1284
Pete Cooperd2a44612015-05-06 23:19:43 +00001285AttrBuilder &AttrBuilder::remove(const AttrBuilder &B) {
1286 // FIXME: What if both have alignments, but they don't match?!
1287 if (B.Alignment)
1288 Alignment = 0;
1289
1290 if (B.StackAlignment)
1291 StackAlignment = 0;
1292
1293 if (B.DerefBytes)
1294 DerefBytes = 0;
1295
1296 if (B.DerefOrNullBytes)
1297 DerefOrNullBytes = 0;
1298
1299 Attrs &= ~B.Attrs;
1300
1301 for (auto I : B.td_attrs())
1302 TargetDepAttrs.erase(I.first);
1303
1304 return *this;
1305}
1306
1307bool AttrBuilder::overlaps(const AttrBuilder &B) const {
1308 // First check if any of the target independent attributes overlap.
1309 if ((Attrs & B.Attrs).any())
1310 return true;
1311
1312 // Then check if any target dependent ones do.
1313 for (auto I : td_attrs())
1314 if (B.contains(I.first))
1315 return true;
1316
1317 return false;
1318}
1319
Bill Wendling4b001442013-02-06 01:33:42 +00001320bool AttrBuilder::contains(StringRef A) const {
1321 return TargetDepAttrs.find(A) != TargetDepAttrs.end();
1322}
1323
Bill Wendling50d27842012-10-15 20:35:56 +00001324bool AttrBuilder::hasAttributes() const {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001325 return !Attrs.none() || !TargetDepAttrs.empty();
Bill Wendlingc6daefa2012-10-08 23:27:46 +00001326}
Bill Wendling9ac69f92013-01-04 20:54:35 +00001327
Bill Wendlingd2196752013-01-30 23:07:40 +00001328bool AttrBuilder::hasAttributes(AttributeSet A, uint64_t Index) const {
Bill Wendling211316c2013-04-18 20:17:28 +00001329 unsigned Slot = ~0U;
Bill Wendling9ca01da2013-02-02 00:42:06 +00001330 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
1331 if (A.getSlotIndex(I) == Index) {
Bill Wendling211316c2013-04-18 20:17:28 +00001332 Slot = I;
Bill Wendling9ca01da2013-02-02 00:42:06 +00001333 break;
1334 }
1335
Bill Wendling211316c2013-04-18 20:17:28 +00001336 assert(Slot != ~0U && "Couldn't find the index!");
Bill Wendling9ca01da2013-02-02 00:42:06 +00001337
George Burgess IV500d3032015-12-16 05:21:02 +00001338 for (AttributeSet::iterator I = A.begin(Slot), E = A.end(Slot); I != E; ++I) {
Bill Wendling7cde51d2013-02-12 07:56:49 +00001339 Attribute Attr = *I;
Hal Finkele15442c2014-07-18 06:51:55 +00001340 if (Attr.isEnumAttribute() || Attr.isIntAttribute()) {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001341 if (Attrs[I->getKindAsEnum()])
Bill Wendling7cde51d2013-02-12 07:56:49 +00001342 return true;
1343 } else {
1344 assert(Attr.isStringAttribute() && "Invalid attribute kind!");
1345 return TargetDepAttrs.find(Attr.getKindAsString())!=TargetDepAttrs.end();
1346 }
1347 }
Bill Wendling9ca01da2013-02-02 00:42:06 +00001348
1349 return false;
Bill Wendling70f39172012-10-09 00:01:21 +00001350}
Bill Wendling9ac69f92013-01-04 20:54:35 +00001351
Bill Wendling50d27842012-10-15 20:35:56 +00001352bool AttrBuilder::hasAlignmentAttr() const {
Bill Wendlingcd330342013-01-04 23:27:34 +00001353 return Alignment != 0;
Bill Wendlingc6daefa2012-10-08 23:27:46 +00001354}
1355
Bill Wendlingd509a662013-01-29 00:34:06 +00001356bool AttrBuilder::operator==(const AttrBuilder &B) {
Benjamin Kramer45e7d532013-02-16 19:13:18 +00001357 if (Attrs != B.Attrs)
1358 return false;
Bill Wendling4b001442013-02-06 01:33:42 +00001359
1360 for (td_const_iterator I = TargetDepAttrs.begin(),
1361 E = TargetDepAttrs.end(); I != E; ++I)
1362 if (B.TargetDepAttrs.find(I->first) == B.TargetDepAttrs.end())
1363 return false;
1364
Hal Finkelb0407ba2014-07-18 15:51:28 +00001365 return Alignment == B.Alignment && StackAlignment == B.StackAlignment &&
1366 DerefBytes == B.DerefBytes;
Bill Wendlingd509a662013-01-29 00:34:06 +00001367}
1368
1369AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) {
Bill Wendling56aeccc2013-02-04 23:32:23 +00001370 // FIXME: Remove this in 4.0.
Bill Wendling60011b82013-01-29 01:43:29 +00001371 if (!Val) return *this;
1372
Bill Wendlingd509a662013-01-29 00:34:06 +00001373 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
1374 I = Attribute::AttrKind(I + 1)) {
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001375 if (I == Attribute::Dereferenceable ||
Igor Laevsky39d662f2015-07-11 10:30:36 +00001376 I == Attribute::DereferenceableOrNull ||
1377 I == Attribute::ArgMemOnly)
Hal Finkelb0407ba2014-07-18 15:51:28 +00001378 continue;
Bill Wendlingd509a662013-01-29 00:34:06 +00001379 if (uint64_t A = (Val & AttributeImpl::getAttrMask(I))) {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001380 Attrs[I] = true;
Bill Wendlingd509a662013-01-29 00:34:06 +00001381
1382 if (I == Attribute::Alignment)
1383 Alignment = 1ULL << ((A >> 16) - 1);
1384 else if (I == Attribute::StackAlignment)
1385 StackAlignment = 1ULL << ((A >> 26)-1);
1386 }
1387 }
1388
1389 return *this;
1390}
1391
Bill Wendling57625a42013-01-25 23:09:36 +00001392//===----------------------------------------------------------------------===//
1393// AttributeFuncs Function Defintions
1394//===----------------------------------------------------------------------===//
1395
Bill Wendlingc79cdff2013-02-01 01:04:27 +00001396/// \brief Which attributes cannot be applied to a type.
Craig Toppere3dcce92015-08-01 22:20:21 +00001397AttrBuilder AttributeFuncs::typeIncompatible(Type *Ty) {
Bill Wendling57625a42013-01-25 23:09:36 +00001398 AttrBuilder Incompatible;
1399
1400 if (!Ty->isIntegerTy())
1401 // Attribute that only apply to integers.
1402 Incompatible.addAttribute(Attribute::SExt)
1403 .addAttribute(Attribute::ZExt);
1404
1405 if (!Ty->isPointerTy())
1406 // Attribute that only apply to pointers.
1407 Incompatible.addAttribute(Attribute::ByVal)
1408 .addAttribute(Attribute::Nest)
1409 .addAttribute(Attribute::NoAlias)
1410 .addAttribute(Attribute::NoCapture)
Nick Lewyckyd52b1522014-05-20 01:23:40 +00001411 .addAttribute(Attribute::NonNull)
Hal Finkelb0407ba2014-07-18 15:51:28 +00001412 .addDereferenceableAttr(1) // the int here is ignored
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001413 .addDereferenceableOrNullAttr(1) // the int here is ignored
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001414 .addAttribute(Attribute::ReadNone)
1415 .addAttribute(Attribute::ReadOnly)
Reid Klecknera534a382013-12-19 02:14:12 +00001416 .addAttribute(Attribute::StructRet)
1417 .addAttribute(Attribute::InAlloca);
Bill Wendling57625a42013-01-25 23:09:36 +00001418
Pete Cooper2777d8872015-05-06 23:19:56 +00001419 return Incompatible;
Bill Wendling57625a42013-01-25 23:09:36 +00001420}
Akira Hatanaka1cb242e2015-12-22 23:57:37 +00001421
1422template<typename AttrClass>
1423static bool isEqual(const Function &Caller, const Function &Callee) {
1424 return Caller.getFnAttribute(AttrClass::getKind()) ==
1425 Callee.getFnAttribute(AttrClass::getKind());
1426}
1427
1428/// \brief Compute the logical AND of the attributes of the caller and the
1429/// callee.
1430///
1431/// This function sets the caller's attribute to false if the callee's attribute
1432/// is false.
1433template<typename AttrClass>
1434static void setAND(Function &Caller, const Function &Callee) {
1435 if (AttrClass::isSet(Caller, AttrClass::getKind()) &&
1436 !AttrClass::isSet(Callee, AttrClass::getKind()))
1437 AttrClass::set(Caller, AttrClass::getKind(), false);
1438}
1439
1440/// \brief Compute the logical OR of the attributes of the caller and the
1441/// callee.
1442///
1443/// This function sets the caller's attribute to true if the callee's attribute
1444/// is true.
1445template<typename AttrClass>
1446static void setOR(Function &Caller, const Function &Callee) {
1447 if (!AttrClass::isSet(Caller, AttrClass::getKind()) &&
1448 AttrClass::isSet(Callee, AttrClass::getKind()))
1449 AttrClass::set(Caller, AttrClass::getKind(), true);
1450}
1451
1452/// \brief If the inlined function had a higher stack protection level than the
1453/// calling function, then bump up the caller's stack protection level.
1454static void adjustCallerSSPLevel(Function &Caller, const Function &Callee) {
1455 // If upgrading the SSP attribute, clear out the old SSP Attributes first.
1456 // Having multiple SSP attributes doesn't actually hurt, but it adds useless
1457 // clutter to the IR.
1458 AttrBuilder B;
1459 B.addAttribute(Attribute::StackProtect)
1460 .addAttribute(Attribute::StackProtectStrong)
1461 .addAttribute(Attribute::StackProtectReq);
1462 AttributeSet OldSSPAttr = AttributeSet::get(Caller.getContext(),
1463 AttributeSet::FunctionIndex,
1464 B);
1465
1466 if (Callee.hasFnAttribute(Attribute::SafeStack)) {
1467 Caller.removeAttributes(AttributeSet::FunctionIndex, OldSSPAttr);
1468 Caller.addFnAttr(Attribute::SafeStack);
1469 } else if (Callee.hasFnAttribute(Attribute::StackProtectReq) &&
1470 !Caller.hasFnAttribute(Attribute::SafeStack)) {
1471 Caller.removeAttributes(AttributeSet::FunctionIndex, OldSSPAttr);
1472 Caller.addFnAttr(Attribute::StackProtectReq);
1473 } else if (Callee.hasFnAttribute(Attribute::StackProtectStrong) &&
1474 !Caller.hasFnAttribute(Attribute::SafeStack) &&
1475 !Caller.hasFnAttribute(Attribute::StackProtectReq)) {
1476 Caller.removeAttributes(AttributeSet::FunctionIndex, OldSSPAttr);
1477 Caller.addFnAttr(Attribute::StackProtectStrong);
1478 } else if (Callee.hasFnAttribute(Attribute::StackProtect) &&
1479 !Caller.hasFnAttribute(Attribute::SafeStack) &&
1480 !Caller.hasFnAttribute(Attribute::StackProtectReq) &&
1481 !Caller.hasFnAttribute(Attribute::StackProtectStrong))
1482 Caller.addFnAttr(Attribute::StackProtect);
1483}
1484
1485#define GET_ATTR_COMPAT_FUNC
1486#include "AttributesCompatFunc.inc"
1487
1488bool AttributeFuncs::areInlineCompatible(const Function &Caller,
1489 const Function &Callee) {
1490 return hasCompatibleFnAttrs(Caller, Callee);
1491}
1492
1493
1494void AttributeFuncs::mergeAttributesForInlining(Function &Caller,
1495 const Function &Callee) {
1496 mergeFnAttrs(Caller, Callee);
1497}