blob: 3ee622b31bad1bcd811e8da78cab665af9446ee1 [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"
Bill Wendling4607f4b2012-12-20 01:36:59 +000017#include "AttributeImpl.h"
Bill Wendlinge38b8042012-09-26 21:07:29 +000018#include "LLVMContextImpl.h"
Benjamin Kramer502b9e12014-04-12 16:15:53 +000019#include "llvm/ADT/STLExtras.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000020#include "llvm/ADT/StringExtras.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000021#include "llvm/IR/Type.h"
Benjamin Kramer17388a62014-03-03 18:02:34 +000022#include "llvm/Support/Atomic.h"
David Greenef7014732010-01-05 01:29:58 +000023#include "llvm/Support/Debug.h"
Chris Lattner3e13b8c2008-01-02 23:42:30 +000024#include "llvm/Support/ManagedStatic.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000025#include "llvm/Support/Mutex.h"
Benjamin Kramer1a25d732009-08-23 11:37:21 +000026#include "llvm/Support/raw_ostream.h"
Bill Wendlingd2e493b2013-01-24 00:06:56 +000027#include <algorithm>
Chris Lattner3e13b8c2008-01-02 23:42:30 +000028using namespace llvm;
29
Chris Lattner8a923e72008-03-12 17:45:29 +000030//===----------------------------------------------------------------------===//
Bill Wendling7707c5a2013-01-29 00:48:16 +000031// Attribute Construction Methods
Chris Lattner8a923e72008-03-12 17:45:29 +000032//===----------------------------------------------------------------------===//
Chris Lattnerd0e1f102008-01-03 00:10:22 +000033
Bill Wendling3f12ac22013-02-05 22:37:24 +000034Attribute Attribute::get(LLVMContext &Context, Attribute::AttrKind Kind,
35 uint64_t Val) {
Bill Wendling73ea2de2012-10-08 21:47:17 +000036 LLVMContextImpl *pImpl = Context.pImpl;
37 FoldingSetNodeID ID;
Bill Wendling3f12ac22013-02-05 22:37:24 +000038 ID.AddInteger(Kind);
39 if (Val) ID.AddInteger(Val);
40
41 void *InsertPoint;
42 AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
43
44 if (!PA) {
45 // If we didn't find any existing attributes of the same shape then create a
46 // new one and insert it.
Benjamin Kramer741146b2013-07-11 12:13:16 +000047 if (!Val)
48 PA = new EnumAttributeImpl(Kind);
49 else
Hal Finkele15442c2014-07-18 06:51:55 +000050 PA = new IntAttributeImpl(Kind, Val);
Bill Wendling3f12ac22013-02-05 22:37:24 +000051 pImpl->AttrsSet.InsertNode(PA, InsertPoint);
52 }
53
54 // Return the Attribute that we found or created.
55 return Attribute(PA);
56}
57
58Attribute Attribute::get(LLVMContext &Context, StringRef Kind, StringRef Val) {
59 LLVMContextImpl *pImpl = Context.pImpl;
60 FoldingSetNodeID ID;
61 ID.AddString(Kind);
62 if (!Val.empty()) ID.AddString(Val);
Bill Wendling73ea2de2012-10-08 21:47:17 +000063
64 void *InsertPoint;
Bill Wendling4607f4b2012-12-20 01:36:59 +000065 AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
Bill Wendling73ea2de2012-10-08 21:47:17 +000066
67 if (!PA) {
68 // If we didn't find any existing attributes of the same shape then create a
69 // new one and insert it.
Benjamin Kramer741146b2013-07-11 12:13:16 +000070 PA = new StringAttributeImpl(Kind, Val);
Bill Wendling73ea2de2012-10-08 21:47:17 +000071 pImpl->AttrsSet.InsertNode(PA, InsertPoint);
72 }
73
Bill Wendlingb9c5b1a2013-02-05 08:09:32 +000074 // Return the Attribute that we found or created.
Bill Wendling3d7b0b82012-12-19 07:18:57 +000075 return Attribute(PA);
Bill Wendling73ea2de2012-10-08 21:47:17 +000076}
77
Bill Wendling4bbe9db2013-01-27 22:43:04 +000078Attribute Attribute::getWithAlignment(LLVMContext &Context, uint64_t Align) {
Bill Wendling1c7cc8a2013-01-31 23:16:25 +000079 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
80 assert(Align <= 0x40000000 && "Alignment too large.");
Bill Wendling3f12ac22013-02-05 22:37:24 +000081 return get(Context, Alignment, Align);
Bill Wendling4bbe9db2013-01-27 22:43:04 +000082}
83
84Attribute Attribute::getWithStackAlignment(LLVMContext &Context,
85 uint64_t Align) {
Bill Wendling1c7cc8a2013-01-31 23:16:25 +000086 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
87 assert(Align <= 0x100 && "Alignment too large.");
Bill Wendling3f12ac22013-02-05 22:37:24 +000088 return get(Context, StackAlignment, Align);
Bill Wendling4bbe9db2013-01-27 22:43:04 +000089}
90
Bill Wendling7707c5a2013-01-29 00:48:16 +000091//===----------------------------------------------------------------------===//
92// Attribute Accessor Methods
93//===----------------------------------------------------------------------===//
94
Bill Wendling3f12ac22013-02-05 22:37:24 +000095bool Attribute::isEnumAttribute() const {
96 return pImpl && pImpl->isEnumAttribute();
97}
98
Hal Finkele15442c2014-07-18 06:51:55 +000099bool Attribute::isIntAttribute() const {
100 return pImpl && pImpl->isIntAttribute();
Bill Wendling3f12ac22013-02-05 22:37:24 +0000101}
102
103bool Attribute::isStringAttribute() const {
104 return pImpl && pImpl->isStringAttribute();
105}
106
107Attribute::AttrKind Attribute::getKindAsEnum() const {
Bill Wendling440e9d82013-07-25 00:34:29 +0000108 if (!pImpl) return None;
Hal Finkele15442c2014-07-18 06:51:55 +0000109 assert((isEnumAttribute() || isIntAttribute()) &&
Bill Wendling3f12ac22013-02-05 22:37:24 +0000110 "Invalid attribute type to get the kind as an enum!");
111 return pImpl ? pImpl->getKindAsEnum() : None;
112}
113
114uint64_t Attribute::getValueAsInt() const {
Bill Wendling440e9d82013-07-25 00:34:29 +0000115 if (!pImpl) return 0;
Hal Finkele15442c2014-07-18 06:51:55 +0000116 assert(isIntAttribute() &&
117 "Expected the attribute to be an integer attribute!");
Bill Wendling3f12ac22013-02-05 22:37:24 +0000118 return pImpl ? pImpl->getValueAsInt() : 0;
119}
120
121StringRef Attribute::getKindAsString() const {
Bill Wendling440e9d82013-07-25 00:34:29 +0000122 if (!pImpl) return StringRef();
Bill Wendling3f12ac22013-02-05 22:37:24 +0000123 assert(isStringAttribute() &&
124 "Invalid attribute type to get the kind as a string!");
125 return pImpl ? pImpl->getKindAsString() : StringRef();
126}
127
128StringRef Attribute::getValueAsString() const {
Bill Wendling440e9d82013-07-25 00:34:29 +0000129 if (!pImpl) return StringRef();
Bill Wendling3f12ac22013-02-05 22:37:24 +0000130 assert(isStringAttribute() &&
131 "Invalid attribute type to get the value as a string!");
132 return pImpl ? pImpl->getValueAsString() : StringRef();
133}
134
Bill Wendlingae89a0f2013-02-05 23:48:36 +0000135bool Attribute::hasAttribute(AttrKind Kind) const {
136 return (pImpl && pImpl->hasAttribute(Kind)) || (!pImpl && Kind == None);
137}
138
139bool Attribute::hasAttribute(StringRef Kind) const {
140 if (!isStringAttribute()) return false;
141 return pImpl && pImpl->hasAttribute(Kind);
Bill Wendling03eefb32013-01-29 20:45:34 +0000142}
143
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000144/// This returns the alignment field of an attribute as a byte alignment value.
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000145unsigned Attribute::getAlignment() const {
Bill Wendlingc79cdff2013-02-01 01:04:27 +0000146 assert(hasAttribute(Attribute::Alignment) &&
147 "Trying to get alignment from non-alignment attribute!");
Bill Wendling3f12ac22013-02-05 22:37:24 +0000148 return pImpl->getValueAsInt();
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000149}
150
151/// This returns the stack alignment field of an attribute as a byte alignment
152/// value.
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000153unsigned Attribute::getStackAlignment() const {
Bill Wendlingc79cdff2013-02-01 01:04:27 +0000154 assert(hasAttribute(Attribute::StackAlignment) &&
155 "Trying to get alignment from non-alignment attribute!");
Bill Wendling3f12ac22013-02-05 22:37:24 +0000156 return pImpl->getValueAsInt();
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000157}
158
Bill Wendling829b4782013-02-11 08:43:33 +0000159std::string Attribute::getAsString(bool InAttrGrp) const {
Bill Wendling9c2eba92013-01-31 20:59:05 +0000160 if (!pImpl) return "";
161
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000162 if (hasAttribute(Attribute::SanitizeAddress))
163 return "sanitize_address";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000164 if (hasAttribute(Attribute::AlwaysInline))
165 return "alwaysinline";
Michael Gottesman41748d72013-06-27 00:25:01 +0000166 if (hasAttribute(Attribute::Builtin))
167 return "builtin";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000168 if (hasAttribute(Attribute::ByVal))
169 return "byval";
Reid Klecknera534a382013-12-19 02:14:12 +0000170 if (hasAttribute(Attribute::InAlloca))
171 return "inalloca";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000172 if (hasAttribute(Attribute::InlineHint))
173 return "inlinehint";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000174 if (hasAttribute(Attribute::InReg))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000175 return "inreg";
Tom Roeder44cb65f2014-06-05 19:29:43 +0000176 if (hasAttribute(Attribute::JumpTable))
177 return "jumptable";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000178 if (hasAttribute(Attribute::MinSize))
179 return "minsize";
180 if (hasAttribute(Attribute::Naked))
181 return "naked";
182 if (hasAttribute(Attribute::Nest))
183 return "nest";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000184 if (hasAttribute(Attribute::NoAlias))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000185 return "noalias";
Bill Wendling09bd1f72013-02-22 00:12:35 +0000186 if (hasAttribute(Attribute::NoBuiltin))
187 return "nobuiltin";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000188 if (hasAttribute(Attribute::NoCapture))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000189 return "nocapture";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000190 if (hasAttribute(Attribute::NoDuplicate))
191 return "noduplicate";
192 if (hasAttribute(Attribute::NoImplicitFloat))
193 return "noimplicitfloat";
194 if (hasAttribute(Attribute::NoInline))
195 return "noinline";
196 if (hasAttribute(Attribute::NonLazyBind))
197 return "nonlazybind";
Nick Lewyckyd52b1522014-05-20 01:23:40 +0000198 if (hasAttribute(Attribute::NonNull))
199 return "nonnull";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000200 if (hasAttribute(Attribute::NoRedZone))
201 return "noredzone";
202 if (hasAttribute(Attribute::NoReturn))
203 return "noreturn";
204 if (hasAttribute(Attribute::NoUnwind))
205 return "nounwind";
Andrea Di Biagio377496b2013-08-23 11:53:55 +0000206 if (hasAttribute(Attribute::OptimizeNone))
207 return "optnone";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000208 if (hasAttribute(Attribute::OptimizeForSize))
209 return "optsize";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000210 if (hasAttribute(Attribute::ReadNone))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000211 return "readnone";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000212 if (hasAttribute(Attribute::ReadOnly))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000213 return "readonly";
Stephen Linb8bd2322013-04-20 05:14:40 +0000214 if (hasAttribute(Attribute::Returned))
215 return "returned";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000216 if (hasAttribute(Attribute::ReturnsTwice))
217 return "returns_twice";
218 if (hasAttribute(Attribute::SExt))
219 return "signext";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000220 if (hasAttribute(Attribute::StackProtect))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000221 return "ssp";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000222 if (hasAttribute(Attribute::StackProtectReq))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000223 return "sspreq";
Bill Wendlingd154e2832013-01-23 06:41:41 +0000224 if (hasAttribute(Attribute::StackProtectStrong))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000225 return "sspstrong";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000226 if (hasAttribute(Attribute::StructRet))
227 return "sret";
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000228 if (hasAttribute(Attribute::SanitizeThread))
229 return "sanitize_thread";
230 if (hasAttribute(Attribute::SanitizeMemory))
231 return "sanitize_memory";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000232 if (hasAttribute(Attribute::UWTable))
233 return "uwtable";
234 if (hasAttribute(Attribute::ZExt))
235 return "zeroext";
Diego Novilloc6399532013-05-24 12:26:52 +0000236 if (hasAttribute(Attribute::Cold))
237 return "cold";
Bill Wendling9c2eba92013-01-31 20:59:05 +0000238
239 // FIXME: These should be output like this:
240 //
241 // align=4
242 // alignstack=8
243 //
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000244 if (hasAttribute(Attribute::Alignment)) {
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000245 std::string Result;
Bill Wendling829b4782013-02-11 08:43:33 +0000246 Result += "align";
247 Result += (InAttrGrp) ? "=" : " ";
Bill Wendling3f12ac22013-02-05 22:37:24 +0000248 Result += utostr(getValueAsInt());
249 return Result;
250 }
Bill Wendling829b4782013-02-11 08:43:33 +0000251
Bill Wendling3f12ac22013-02-05 22:37:24 +0000252 if (hasAttribute(Attribute::StackAlignment)) {
253 std::string Result;
Bill Wendling829b4782013-02-11 08:43:33 +0000254 Result += "alignstack";
255 if (InAttrGrp) {
256 Result += "=";
257 Result += utostr(getValueAsInt());
258 } else {
259 Result += "(";
260 Result += utostr(getValueAsInt());
261 Result += ")";
262 }
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000263 return Result;
Dale Johannesen11a555e2008-02-19 23:51:49 +0000264 }
Bill Wendling9c2eba92013-01-31 20:59:05 +0000265
266 // Convert target-dependent attributes to strings of the form:
267 //
268 // "kind"
269 // "kind" = "value"
Bill Wendling9c2eba92013-01-31 20:59:05 +0000270 //
Bill Wendling3f12ac22013-02-05 22:37:24 +0000271 if (isStringAttribute()) {
Bill Wendling9c2eba92013-01-31 20:59:05 +0000272 std::string Result;
Bill Wendling3f12ac22013-02-05 22:37:24 +0000273 Result += '\"' + getKindAsString().str() + '"';
Bill Wendling9c2eba92013-01-31 20:59:05 +0000274
Bill Wendling3f12ac22013-02-05 22:37:24 +0000275 StringRef Val = pImpl->getValueAsString();
276 if (Val.empty()) return Result;
Bill Wendling7a33f772013-02-01 22:32:30 +0000277
Bill Wendling829b4782013-02-11 08:43:33 +0000278 Result += "=\"" + Val.str() + '"';
Bill Wendlingc79cdff2013-02-01 01:04:27 +0000279 return Result;
Bill Wendling9c2eba92013-01-31 20:59:05 +0000280 }
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000281
282 llvm_unreachable("Unknown attribute");
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000283}
284
Bill Wendlingd509a662013-01-29 00:34:06 +0000285bool Attribute::operator<(Attribute A) const {
286 if (!pImpl && !A.pImpl) return false;
287 if (!pImpl) return true;
288 if (!A.pImpl) return false;
289 return *pImpl < *A.pImpl;
290}
291
Bill Wendlingd509a662013-01-29 00:34:06 +0000292//===----------------------------------------------------------------------===//
293// AttributeImpl Definition
294//===----------------------------------------------------------------------===//
295
Eric Christopher0eaa5412014-07-02 22:05:40 +0000296// Pin the vtables to this file.
Alexey Samsonov49109a22013-11-18 09:31:53 +0000297AttributeImpl::~AttributeImpl() {}
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +0000298void EnumAttributeImpl::anchor() {}
Hal Finkele15442c2014-07-18 06:51:55 +0000299void IntAttributeImpl::anchor() {}
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +0000300void StringAttributeImpl::anchor() {}
Alexey Samsonov49109a22013-11-18 09:31:53 +0000301
Bill Wendlingd509a662013-01-29 00:34:06 +0000302bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
Bill Wendling3f12ac22013-02-05 22:37:24 +0000303 if (isStringAttribute()) return false;
304 return getKindAsEnum() == A;
Bill Wendlingd509a662013-01-29 00:34:06 +0000305}
306
Bill Wendling3f12ac22013-02-05 22:37:24 +0000307bool AttributeImpl::hasAttribute(StringRef Kind) const {
308 if (!isStringAttribute()) return false;
309 return getKindAsString() == Kind;
Bill Wendlingd509a662013-01-29 00:34:06 +0000310}
311
Bill Wendling3f12ac22013-02-05 22:37:24 +0000312Attribute::AttrKind AttributeImpl::getKindAsEnum() const {
Hal Finkele15442c2014-07-18 06:51:55 +0000313 assert(isEnumAttribute() || isIntAttribute());
Benjamin Kramer741146b2013-07-11 12:13:16 +0000314 return static_cast<const EnumAttributeImpl *>(this)->getEnumKind();
Bill Wendlingd509a662013-01-29 00:34:06 +0000315}
316
Bill Wendling3f12ac22013-02-05 22:37:24 +0000317uint64_t AttributeImpl::getValueAsInt() const {
Hal Finkele15442c2014-07-18 06:51:55 +0000318 assert(isIntAttribute());
319 return static_cast<const IntAttributeImpl *>(this)->getValue();
Bill Wendlingd509a662013-01-29 00:34:06 +0000320}
321
Bill Wendling3f12ac22013-02-05 22:37:24 +0000322StringRef AttributeImpl::getKindAsString() const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000323 assert(isStringAttribute());
324 return static_cast<const StringAttributeImpl *>(this)->getStringKind();
Bill Wendlingd509a662013-01-29 00:34:06 +0000325}
326
Bill Wendling3f12ac22013-02-05 22:37:24 +0000327StringRef AttributeImpl::getValueAsString() const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000328 assert(isStringAttribute());
329 return static_cast<const StringAttributeImpl *>(this)->getStringValue();
Bill Wendlingd509a662013-01-29 00:34:06 +0000330}
331
332bool AttributeImpl::operator<(const AttributeImpl &AI) const {
Bill Wendlingc79cdff2013-02-01 01:04:27 +0000333 // This sorts the attributes with Attribute::AttrKinds coming first (sorted
334 // relative to their enum value) and then strings.
Bill Wendling26b95752013-02-15 05:25:26 +0000335 if (isEnumAttribute()) {
336 if (AI.isEnumAttribute()) return getKindAsEnum() < AI.getKindAsEnum();
Hal Finkele15442c2014-07-18 06:51:55 +0000337 if (AI.isIntAttribute()) return true;
Bill Wendling26b95752013-02-15 05:25:26 +0000338 if (AI.isStringAttribute()) return true;
339 }
Bill Wendlingc79cdff2013-02-01 01:04:27 +0000340
Hal Finkele15442c2014-07-18 06:51:55 +0000341 if (isIntAttribute()) {
Bill Wendling26b95752013-02-15 05:25:26 +0000342 if (AI.isEnumAttribute()) return false;
Hal Finkele15442c2014-07-18 06:51:55 +0000343 if (AI.isIntAttribute()) return getValueAsInt() < AI.getValueAsInt();
Bill Wendling26b95752013-02-15 05:25:26 +0000344 if (AI.isStringAttribute()) return true;
Bill Wendling3f12ac22013-02-05 22:37:24 +0000345 }
Bill Wendlingd509a662013-01-29 00:34:06 +0000346
Bill Wendling26b95752013-02-15 05:25:26 +0000347 if (AI.isEnumAttribute()) return false;
Hal Finkele15442c2014-07-18 06:51:55 +0000348 if (AI.isIntAttribute()) return false;
Bill Wendling26b95752013-02-15 05:25:26 +0000349 if (getKindAsString() == AI.getKindAsString())
350 return getValueAsString() < AI.getValueAsString();
351 return getKindAsString() < AI.getKindAsString();
Bill Wendlingd509a662013-01-29 00:34:06 +0000352}
353
Bill Wendlingd509a662013-01-29 00:34:06 +0000354uint64_t AttributeImpl::getAttrMask(Attribute::AttrKind Val) {
355 // FIXME: Remove this.
356 switch (Val) {
357 case Attribute::EndAttrKinds:
Bill Wendlingd509a662013-01-29 00:34:06 +0000358 llvm_unreachable("Synthetic enumerators which should never get here");
359
360 case Attribute::None: return 0;
361 case Attribute::ZExt: return 1 << 0;
362 case Attribute::SExt: return 1 << 1;
363 case Attribute::NoReturn: return 1 << 2;
364 case Attribute::InReg: return 1 << 3;
365 case Attribute::StructRet: return 1 << 4;
366 case Attribute::NoUnwind: return 1 << 5;
367 case Attribute::NoAlias: return 1 << 6;
368 case Attribute::ByVal: return 1 << 7;
369 case Attribute::Nest: return 1 << 8;
370 case Attribute::ReadNone: return 1 << 9;
371 case Attribute::ReadOnly: return 1 << 10;
372 case Attribute::NoInline: return 1 << 11;
373 case Attribute::AlwaysInline: return 1 << 12;
374 case Attribute::OptimizeForSize: return 1 << 13;
375 case Attribute::StackProtect: return 1 << 14;
376 case Attribute::StackProtectReq: return 1 << 15;
377 case Attribute::Alignment: return 31 << 16;
378 case Attribute::NoCapture: return 1 << 21;
379 case Attribute::NoRedZone: return 1 << 22;
380 case Attribute::NoImplicitFloat: return 1 << 23;
381 case Attribute::Naked: return 1 << 24;
382 case Attribute::InlineHint: return 1 << 25;
383 case Attribute::StackAlignment: return 7 << 26;
384 case Attribute::ReturnsTwice: return 1 << 29;
385 case Attribute::UWTable: return 1 << 30;
386 case Attribute::NonLazyBind: return 1U << 31;
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000387 case Attribute::SanitizeAddress: return 1ULL << 32;
Bill Wendlingd509a662013-01-29 00:34:06 +0000388 case Attribute::MinSize: return 1ULL << 33;
389 case Attribute::NoDuplicate: return 1ULL << 34;
390 case Attribute::StackProtectStrong: return 1ULL << 35;
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000391 case Attribute::SanitizeThread: return 1ULL << 36;
392 case Attribute::SanitizeMemory: return 1ULL << 37;
Bill Wendling7bfdfe72013-02-22 00:40:12 +0000393 case Attribute::NoBuiltin: return 1ULL << 38;
Stephen Linb8bd2322013-04-20 05:14:40 +0000394 case Attribute::Returned: return 1ULL << 39;
Diego Novilloc6399532013-05-24 12:26:52 +0000395 case Attribute::Cold: return 1ULL << 40;
Michael Gottesman41748d72013-06-27 00:25:01 +0000396 case Attribute::Builtin: return 1ULL << 41;
Andrea Di Biagio377496b2013-08-23 11:53:55 +0000397 case Attribute::OptimizeNone: return 1ULL << 42;
Reid Klecknera534a382013-12-19 02:14:12 +0000398 case Attribute::InAlloca: return 1ULL << 43;
Nick Lewyckyd52b1522014-05-20 01:23:40 +0000399 case Attribute::NonNull: return 1ULL << 44;
Tom Roeder44cb65f2014-06-05 19:29:43 +0000400 case Attribute::JumpTable: return 1ULL << 45;
Bill Wendling25342e12013-02-22 00:50:09 +0000401 }
402 llvm_unreachable("Unsupported attribute type");
Bill Wendlingd509a662013-01-29 00:34:06 +0000403}
404
405//===----------------------------------------------------------------------===//
406// AttributeSetNode Definition
407//===----------------------------------------------------------------------===//
408
409AttributeSetNode *AttributeSetNode::get(LLVMContext &C,
410 ArrayRef<Attribute> Attrs) {
411 if (Attrs.empty())
Craig Topperc6207612014-04-09 06:08:46 +0000412 return nullptr;
Bill Wendlingd509a662013-01-29 00:34:06 +0000413
414 // Otherwise, build a key to look up the existing attributes.
415 LLVMContextImpl *pImpl = C.pImpl;
416 FoldingSetNodeID ID;
417
418 SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end());
Bill Wendling82c2ee62013-02-13 09:26:26 +0000419 array_pod_sort(SortedAttrs.begin(), SortedAttrs.end());
Bill Wendlingd509a662013-01-29 00:34:06 +0000420
421 for (SmallVectorImpl<Attribute>::iterator I = SortedAttrs.begin(),
422 E = SortedAttrs.end(); I != E; ++I)
423 I->Profile(ID);
424
425 void *InsertPoint;
426 AttributeSetNode *PA =
427 pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint);
428
429 // If we didn't find any existing attributes of the same shape then create a
430 // new one and insert it.
431 if (!PA) {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000432 // Coallocate entries after the AttributeSetNode itself.
433 void *Mem = ::operator new(sizeof(AttributeSetNode) +
434 sizeof(Attribute) * SortedAttrs.size());
435 PA = new (Mem) AttributeSetNode(SortedAttrs);
Bill Wendlingd509a662013-01-29 00:34:06 +0000436 pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint);
437 }
438
439 // Return the AttributesListNode that we found or created.
440 return PA;
441}
442
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000443bool AttributeSetNode::hasAttribute(Attribute::AttrKind Kind) const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000444 for (iterator I = begin(), E = end(); I != E; ++I)
NAKAMURA Takumib0944392013-01-31 03:47:28 +0000445 if (I->hasAttribute(Kind))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000446 return true;
447 return false;
448}
449
Bill Wendlingbce7b972013-02-13 08:42:21 +0000450bool AttributeSetNode::hasAttribute(StringRef Kind) const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000451 for (iterator I = begin(), E = end(); I != E; ++I)
Bill Wendlingbce7b972013-02-13 08:42:21 +0000452 if (I->hasAttribute(Kind))
453 return true;
454 return false;
455}
456
457Attribute AttributeSetNode::getAttribute(Attribute::AttrKind Kind) const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000458 for (iterator I = begin(), E = end(); I != E; ++I)
Bill Wendlingbce7b972013-02-13 08:42:21 +0000459 if (I->hasAttribute(Kind))
460 return *I;
461 return Attribute();
462}
463
464Attribute AttributeSetNode::getAttribute(StringRef Kind) const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000465 for (iterator I = begin(), E = end(); I != E; ++I)
Bill Wendlingbce7b972013-02-13 08:42:21 +0000466 if (I->hasAttribute(Kind))
467 return *I;
468 return Attribute();
469}
470
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000471unsigned AttributeSetNode::getAlignment() const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000472 for (iterator I = begin(), E = end(); I != E; ++I)
NAKAMURA Takumib0944392013-01-31 03:47:28 +0000473 if (I->hasAttribute(Attribute::Alignment))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000474 return I->getAlignment();
475 return 0;
476}
477
478unsigned AttributeSetNode::getStackAlignment() const {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000479 for (iterator I = begin(), E = end(); I != E; ++I)
NAKAMURA Takumib0944392013-01-31 03:47:28 +0000480 if (I->hasAttribute(Attribute::StackAlignment))
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000481 return I->getStackAlignment();
482 return 0;
483}
484
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +0000485std::string AttributeSetNode::getAsString(bool InAttrGrp) const {
Benjamin Kramer0baf8f42013-04-19 11:43:21 +0000486 std::string Str;
Benjamin Kramer741146b2013-07-11 12:13:16 +0000487 for (iterator I = begin(), E = end(); I != E; ++I) {
488 if (I != begin())
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +0000489 Str += ' ';
490 Str += I->getAsString(InAttrGrp);
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000491 }
492 return Str;
493}
494
Bill Wendlingd509a662013-01-29 00:34:06 +0000495//===----------------------------------------------------------------------===//
496// AttributeSetImpl Definition
497//===----------------------------------------------------------------------===//
498
Rafael Espindoladd275302013-04-30 16:53:38 +0000499uint64_t AttributeSetImpl::Raw(unsigned Index) const {
Bill Wendlingd509a662013-01-29 00:34:06 +0000500 for (unsigned I = 0, E = getNumAttributes(); I != E; ++I) {
501 if (getSlotIndex(I) != Index) continue;
Benjamin Kramer741146b2013-07-11 12:13:16 +0000502 const AttributeSetNode *ASN = getSlotNode(I);
Bill Wendling91226182013-02-02 00:52:44 +0000503 uint64_t Mask = 0;
Bill Wendlingd509a662013-01-29 00:34:06 +0000504
Benjamin Kramer741146b2013-07-11 12:13:16 +0000505 for (AttributeSetNode::iterator II = ASN->begin(),
Bill Wendling91226182013-02-02 00:52:44 +0000506 IE = ASN->end(); II != IE; ++II) {
507 Attribute Attr = *II;
Bill Wendlingd746e402013-02-10 23:18:05 +0000508
509 // This cannot handle string attributes.
510 if (Attr.isStringAttribute()) continue;
511
Bill Wendling3f12ac22013-02-05 22:37:24 +0000512 Attribute::AttrKind Kind = Attr.getKindAsEnum();
Bill Wendling91226182013-02-02 00:52:44 +0000513
Bill Wendling3f12ac22013-02-05 22:37:24 +0000514 if (Kind == Attribute::Alignment)
Bill Wendling91226182013-02-02 00:52:44 +0000515 Mask |= (Log2_32(ASN->getAlignment()) + 1) << 16;
Bill Wendling3f12ac22013-02-05 22:37:24 +0000516 else if (Kind == Attribute::StackAlignment)
Bill Wendling91226182013-02-02 00:52:44 +0000517 Mask |= (Log2_32(ASN->getStackAlignment()) + 1) << 26;
518 else
Bill Wendling3f12ac22013-02-05 22:37:24 +0000519 Mask |= AttributeImpl::getAttrMask(Kind);
Bill Wendling91226182013-02-02 00:52:44 +0000520 }
521
522 return Mask;
Bill Wendlingd509a662013-01-29 00:34:06 +0000523 }
524
525 return 0;
526}
527
Peter Collingbourneabca2ec2013-08-02 22:34:30 +0000528void AttributeSetImpl::dump() const {
529 AttributeSet(const_cast<AttributeSetImpl *>(this)).dump();
530}
531
Bill Wendlingd509a662013-01-29 00:34:06 +0000532//===----------------------------------------------------------------------===//
533// AttributeSet Construction and Mutation Methods
534//===----------------------------------------------------------------------===//
535
Bill Wendling60011b82013-01-29 01:43:29 +0000536AttributeSet
537AttributeSet::getImpl(LLVMContext &C,
538 ArrayRef<std::pair<unsigned, AttributeSetNode*> > Attrs) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000539 LLVMContextImpl *pImpl = C.pImpl;
540 FoldingSetNodeID ID;
541 AttributeSetImpl::Profile(ID, Attrs);
542
543 void *InsertPoint;
544 AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
545
546 // If we didn't find any existing attributes of the same shape then
547 // create a new one and insert it.
548 if (!PA) {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000549 // Coallocate entries after the AttributeSetImpl itself.
550 void *Mem = ::operator new(sizeof(AttributeSetImpl) +
551 sizeof(std::pair<unsigned, AttributeSetNode *>) *
552 Attrs.size());
553 PA = new (Mem) AttributeSetImpl(C, Attrs);
Bill Wendlingd509a662013-01-29 00:34:06 +0000554 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
555 }
556
557 // Return the AttributesList that we found or created.
558 return AttributeSet(PA);
559}
560
561AttributeSet AttributeSet::get(LLVMContext &C,
562 ArrayRef<std::pair<unsigned, Attribute> > Attrs){
563 // If there are no attributes then return a null AttributesList pointer.
564 if (Attrs.empty())
565 return AttributeSet();
566
567#ifndef NDEBUG
568 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
569 assert((!i || Attrs[i-1].first <= Attrs[i].first) &&
570 "Misordered Attributes list!");
Bill Wendling3f12ac22013-02-05 22:37:24 +0000571 assert(!Attrs[i].second.hasAttribute(Attribute::None) &&
Bill Wendlingd509a662013-01-29 00:34:06 +0000572 "Pointless attribute!");
573 }
574#endif
575
576 // Create a vector if (unsigned, AttributeSetNode*) pairs from the attributes
577 // list.
578 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrPairVec;
579 for (ArrayRef<std::pair<unsigned, Attribute> >::iterator I = Attrs.begin(),
580 E = Attrs.end(); I != E; ) {
581 unsigned Index = I->first;
582 SmallVector<Attribute, 4> AttrVec;
NAKAMURA Takumif05d2f22013-01-29 15:18:16 +0000583 while (I != E && I->first == Index) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000584 AttrVec.push_back(I->second);
585 ++I;
586 }
587
588 AttrPairVec.push_back(std::make_pair(Index,
589 AttributeSetNode::get(C, AttrVec)));
590 }
591
592 return getImpl(C, AttrPairVec);
593}
594
595AttributeSet AttributeSet::get(LLVMContext &C,
596 ArrayRef<std::pair<unsigned,
597 AttributeSetNode*> > Attrs) {
598 // If there are no attributes then return a null AttributesList pointer.
599 if (Attrs.empty())
600 return AttributeSet();
601
602 return getImpl(C, Attrs);
603}
604
David Majnemercf63a792014-05-03 23:00:35 +0000605AttributeSet AttributeSet::get(LLVMContext &C, unsigned Index,
606 const AttrBuilder &B) {
Bill Wendlingd509a662013-01-29 00:34:06 +0000607 if (!B.hasAttributes())
608 return AttributeSet();
Bill Wendlingf7134812013-01-29 01:02:03 +0000609
Bill Wendlingae89a0f2013-02-05 23:48:36 +0000610 // Add target-independent attributes.
Bill Wendlingf7134812013-01-29 01:02:03 +0000611 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
Benjamin Kramer45e7d532013-02-16 19:13:18 +0000612 for (Attribute::AttrKind Kind = Attribute::None;
Benjamin Kramer6f37dac2013-02-16 19:22:28 +0000613 Kind != Attribute::EndAttrKinds; Kind = Attribute::AttrKind(Kind + 1)) {
Benjamin Kramer45e7d532013-02-16 19:13:18 +0000614 if (!B.contains(Kind))
615 continue;
616
Bill Wendlingf7134812013-01-29 01:02:03 +0000617 if (Kind == Attribute::Alignment)
Bill Wendling211316c2013-04-18 20:17:28 +0000618 Attrs.push_back(std::make_pair(Index, Attribute::
Bill Wendlingf7134812013-01-29 01:02:03 +0000619 getWithAlignment(C, B.getAlignment())));
620 else if (Kind == Attribute::StackAlignment)
Bill Wendling211316c2013-04-18 20:17:28 +0000621 Attrs.push_back(std::make_pair(Index, Attribute::
Bill Wendlingf7134812013-01-29 01:02:03 +0000622 getWithStackAlignment(C, B.getStackAlignment())));
623 else
Bill Wendling211316c2013-04-18 20:17:28 +0000624 Attrs.push_back(std::make_pair(Index, Attribute::get(C, Kind)));
Bill Wendlingf7134812013-01-29 01:02:03 +0000625 }
626
Bill Wendlingae89a0f2013-02-05 23:48:36 +0000627 // Add target-dependent (string) attributes.
David Majnemercf63a792014-05-03 23:00:35 +0000628 for (const AttrBuilder::td_type &TDA : B.td_attrs())
629 Attrs.push_back(
630 std::make_pair(Index, Attribute::get(C, TDA.first, TDA.second)));
Bill Wendlingae89a0f2013-02-05 23:48:36 +0000631
Bill Wendlingf7134812013-01-29 01:02:03 +0000632 return get(C, Attrs);
Bill Wendlingd509a662013-01-29 00:34:06 +0000633}
634
Bill Wendling211316c2013-04-18 20:17:28 +0000635AttributeSet AttributeSet::get(LLVMContext &C, unsigned Index,
Bill Wendlingd509a662013-01-29 00:34:06 +0000636 ArrayRef<Attribute::AttrKind> Kind) {
637 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
638 for (ArrayRef<Attribute::AttrKind>::iterator I = Kind.begin(),
639 E = Kind.end(); I != E; ++I)
Bill Wendling211316c2013-04-18 20:17:28 +0000640 Attrs.push_back(std::make_pair(Index, Attribute::get(C, *I)));
Bill Wendlingd509a662013-01-29 00:34:06 +0000641 return get(C, Attrs);
642}
643
644AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<AttributeSet> Attrs) {
645 if (Attrs.empty()) return AttributeSet();
Peter Collingbournebd6c7452013-08-02 22:29:40 +0000646 if (Attrs.size() == 1) return Attrs[0];
Bill Wendlingd509a662013-01-29 00:34:06 +0000647
648 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrNodeVec;
Peter Collingbournebd6c7452013-08-02 22:29:40 +0000649 AttributeSetImpl *A0 = Attrs[0].pImpl;
650 if (A0)
651 AttrNodeVec.append(A0->getNode(0), A0->getNode(A0->getNumAttributes()));
652 // Copy all attributes from Attrs into AttrNodeVec while keeping AttrNodeVec
653 // ordered by index. Because we know that each list in Attrs is ordered by
654 // index we only need to merge each successive list in rather than doing a
655 // full sort.
656 for (unsigned I = 1, E = Attrs.size(); I != E; ++I) {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000657 AttributeSetImpl *AS = Attrs[I].pImpl;
658 if (!AS) continue;
Peter Collingbournebd6c7452013-08-02 22:29:40 +0000659 SmallVector<std::pair<unsigned, AttributeSetNode *>, 8>::iterator
660 ANVI = AttrNodeVec.begin(), ANVE;
661 for (const AttributeSetImpl::IndexAttrPair
662 *AI = AS->getNode(0),
663 *AE = AS->getNode(AS->getNumAttributes());
664 AI != AE; ++AI) {
665 ANVE = AttrNodeVec.end();
666 while (ANVI != ANVE && ANVI->first <= AI->first)
667 ++ANVI;
668 ANVI = AttrNodeVec.insert(ANVI, *AI) + 1;
669 }
Bill Wendlingd509a662013-01-29 00:34:06 +0000670 }
671
672 return getImpl(C, AttrNodeVec);
673}
674
Bill Wendling211316c2013-04-18 20:17:28 +0000675AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
Bill Wendlingd509a662013-01-29 00:34:06 +0000676 Attribute::AttrKind Attr) const {
Bill Wendling211316c2013-04-18 20:17:28 +0000677 if (hasAttribute(Index, Attr)) return *this;
678 return addAttributes(C, Index, AttributeSet::get(C, Index, Attr));
Bill Wendlingd509a662013-01-29 00:34:06 +0000679}
680
Bill Wendling211316c2013-04-18 20:17:28 +0000681AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
Reed Kotler795c7b42013-03-13 20:20:08 +0000682 StringRef Kind) const {
683 llvm::AttrBuilder B;
684 B.addAttribute(Kind);
Bill Wendling211316c2013-04-18 20:17:28 +0000685 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
Reed Kotler795c7b42013-03-13 20:20:08 +0000686}
687
Bill Wendling3b2f6102013-07-25 18:34:24 +0000688AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
689 StringRef Kind, StringRef Value) const {
690 llvm::AttrBuilder B;
691 B.addAttribute(Kind, Value);
692 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
693}
694
Bill Wendling211316c2013-04-18 20:17:28 +0000695AttributeSet AttributeSet::addAttributes(LLVMContext &C, unsigned Index,
Bill Wendlingd509a662013-01-29 00:34:06 +0000696 AttributeSet Attrs) const {
697 if (!pImpl) return Attrs;
698 if (!Attrs.pImpl) return *this;
699
700#ifndef NDEBUG
701 // FIXME it is not obvious how this should work for alignment. For now, say
702 // we can't change a known alignment.
Bill Wendling211316c2013-04-18 20:17:28 +0000703 unsigned OldAlign = getParamAlignment(Index);
704 unsigned NewAlign = Attrs.getParamAlignment(Index);
Bill Wendlingd509a662013-01-29 00:34:06 +0000705 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
706 "Attempt to change alignment!");
707#endif
708
709 // Add the attribute slots before the one we're trying to add.
710 SmallVector<AttributeSet, 4> AttrSet;
711 uint64_t NumAttrs = pImpl->getNumAttributes();
712 AttributeSet AS;
713 uint64_t LastIndex = 0;
714 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
Bill Wendling211316c2013-04-18 20:17:28 +0000715 if (getSlotIndex(I) >= Index) {
716 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
Bill Wendlingd509a662013-01-29 00:34:06 +0000717 break;
718 }
719 LastIndex = I + 1;
720 AttrSet.push_back(getSlotAttributes(I));
721 }
722
723 // Now add the attribute into the correct slot. There may already be an
724 // AttributeSet there.
Bill Wendling211316c2013-04-18 20:17:28 +0000725 AttrBuilder B(AS, Index);
Bill Wendlingd509a662013-01-29 00:34:06 +0000726
727 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
Bill Wendling211316c2013-04-18 20:17:28 +0000728 if (Attrs.getSlotIndex(I) == Index) {
Benjamin Kramer741146b2013-07-11 12:13:16 +0000729 for (AttributeSetImpl::iterator II = Attrs.pImpl->begin(I),
Bill Wendlingd509a662013-01-29 00:34:06 +0000730 IE = Attrs.pImpl->end(I); II != IE; ++II)
Bill Wendling23804da2013-01-31 23:38:01 +0000731 B.addAttribute(*II);
Bill Wendlingd509a662013-01-29 00:34:06 +0000732 break;
733 }
734
Bill Wendling211316c2013-04-18 20:17:28 +0000735 AttrSet.push_back(AttributeSet::get(C, Index, B));
Bill Wendlingd509a662013-01-29 00:34:06 +0000736
737 // Add the remaining attribute slots.
738 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
739 AttrSet.push_back(getSlotAttributes(I));
740
741 return get(C, AttrSet);
742}
743
Bill Wendling211316c2013-04-18 20:17:28 +0000744AttributeSet AttributeSet::removeAttribute(LLVMContext &C, unsigned Index,
Bill Wendlingd509a662013-01-29 00:34:06 +0000745 Attribute::AttrKind Attr) const {
Bill Wendling211316c2013-04-18 20:17:28 +0000746 if (!hasAttribute(Index, Attr)) return *this;
747 return removeAttributes(C, Index, AttributeSet::get(C, Index, Attr));
Bill Wendlingd509a662013-01-29 00:34:06 +0000748}
749
Bill Wendling211316c2013-04-18 20:17:28 +0000750AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Index,
Bill Wendlingd509a662013-01-29 00:34:06 +0000751 AttributeSet Attrs) const {
752 if (!pImpl) return AttributeSet();
753 if (!Attrs.pImpl) return *this;
754
755#ifndef NDEBUG
756 // FIXME it is not obvious how this should work for alignment.
757 // For now, say we can't pass in alignment, which no current use does.
Bill Wendling211316c2013-04-18 20:17:28 +0000758 assert(!Attrs.hasAttribute(Index, Attribute::Alignment) &&
Bill Wendlingd509a662013-01-29 00:34:06 +0000759 "Attempt to change alignment!");
760#endif
761
762 // Add the attribute slots before the one we're trying to add.
763 SmallVector<AttributeSet, 4> AttrSet;
764 uint64_t NumAttrs = pImpl->getNumAttributes();
765 AttributeSet AS;
766 uint64_t LastIndex = 0;
767 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
Bill Wendling211316c2013-04-18 20:17:28 +0000768 if (getSlotIndex(I) >= Index) {
769 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
Bill Wendlingd509a662013-01-29 00:34:06 +0000770 break;
771 }
772 LastIndex = I + 1;
773 AttrSet.push_back(getSlotAttributes(I));
774 }
775
Bill Wendlingd2196752013-01-30 23:07:40 +0000776 // Now remove the attribute from the correct slot. There may already be an
Bill Wendlingd509a662013-01-29 00:34:06 +0000777 // AttributeSet there.
Bill Wendling211316c2013-04-18 20:17:28 +0000778 AttrBuilder B(AS, Index);
Bill Wendlingd509a662013-01-29 00:34:06 +0000779
780 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
Bill Wendling211316c2013-04-18 20:17:28 +0000781 if (Attrs.getSlotIndex(I) == Index) {
782 B.removeAttributes(Attrs.pImpl->getSlotAttributes(I), Index);
Bill Wendlingd509a662013-01-29 00:34:06 +0000783 break;
784 }
785
Bill Wendling211316c2013-04-18 20:17:28 +0000786 AttrSet.push_back(AttributeSet::get(C, Index, B));
Bill Wendlingd509a662013-01-29 00:34:06 +0000787
788 // Add the remaining attribute slots.
789 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
790 AttrSet.push_back(getSlotAttributes(I));
791
792 return get(C, AttrSet);
793}
794
795//===----------------------------------------------------------------------===//
796// AttributeSet Accessor Methods
797//===----------------------------------------------------------------------===//
798
Bill Wendling5d020a32013-02-10 05:00:40 +0000799LLVMContext &AttributeSet::getContext() const {
800 return pImpl->getContext();
801}
802
Bill Wendling211316c2013-04-18 20:17:28 +0000803AttributeSet AttributeSet::getParamAttributes(unsigned Index) const {
804 return pImpl && hasAttributes(Index) ?
Bill Wendlingd509a662013-01-29 00:34:06 +0000805 AttributeSet::get(pImpl->getContext(),
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000806 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendling211316c2013-04-18 20:17:28 +0000807 std::make_pair(Index, getAttributes(Index)))) :
Bill Wendlingd509a662013-01-29 00:34:06 +0000808 AttributeSet();
809}
810
811AttributeSet AttributeSet::getRetAttributes() const {
812 return pImpl && hasAttributes(ReturnIndex) ?
813 AttributeSet::get(pImpl->getContext(),
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000814 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendlingd509a662013-01-29 00:34:06 +0000815 std::make_pair(ReturnIndex,
816 getAttributes(ReturnIndex)))) :
817 AttributeSet();
818}
819
820AttributeSet AttributeSet::getFnAttributes() const {
821 return pImpl && hasAttributes(FunctionIndex) ?
822 AttributeSet::get(pImpl->getContext(),
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000823 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendlingd509a662013-01-29 00:34:06 +0000824 std::make_pair(FunctionIndex,
825 getAttributes(FunctionIndex)))) :
826 AttributeSet();
827}
828
829bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000830 AttributeSetNode *ASN = getAttributes(Index);
831 return ASN ? ASN->hasAttribute(Kind) : false;
Bill Wendlingd509a662013-01-29 00:34:06 +0000832}
833
Bill Wendlingbce7b972013-02-13 08:42:21 +0000834bool AttributeSet::hasAttribute(unsigned Index, StringRef Kind) const {
835 AttributeSetNode *ASN = getAttributes(Index);
836 return ASN ? ASN->hasAttribute(Kind) : false;
837}
838
Bill Wendlingd509a662013-01-29 00:34:06 +0000839bool AttributeSet::hasAttributes(unsigned Index) const {
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000840 AttributeSetNode *ASN = getAttributes(Index);
841 return ASN ? ASN->hasAttributes() : false;
Bill Wendlingd509a662013-01-29 00:34:06 +0000842}
843
844/// \brief Return true if the specified attribute is set for at least one
845/// parameter or for the return value.
846bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr) const {
Craig Topperc6207612014-04-09 06:08:46 +0000847 if (!pImpl) return false;
Bill Wendlingd509a662013-01-29 00:34:06 +0000848
849 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
Benjamin Kramer741146b2013-07-11 12:13:16 +0000850 for (AttributeSetImpl::iterator II = pImpl->begin(I),
Bill Wendlingd509a662013-01-29 00:34:06 +0000851 IE = pImpl->end(I); II != IE; ++II)
NAKAMURA Takumib0944392013-01-31 03:47:28 +0000852 if (II->hasAttribute(Attr))
Bill Wendlingd509a662013-01-29 00:34:06 +0000853 return true;
854
855 return false;
856}
857
Bill Wendlingbce7b972013-02-13 08:42:21 +0000858Attribute AttributeSet::getAttribute(unsigned Index,
859 Attribute::AttrKind Kind) const {
860 AttributeSetNode *ASN = getAttributes(Index);
861 return ASN ? ASN->getAttribute(Kind) : Attribute();
862}
863
864Attribute AttributeSet::getAttribute(unsigned Index,
865 StringRef Kind) const {
866 AttributeSetNode *ASN = getAttributes(Index);
867 return ASN ? ASN->getAttribute(Kind) : Attribute();
868}
869
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000870unsigned AttributeSet::getParamAlignment(unsigned Index) const {
871 AttributeSetNode *ASN = getAttributes(Index);
872 return ASN ? ASN->getAlignment() : 0;
Bill Wendlingd509a662013-01-29 00:34:06 +0000873}
874
875unsigned AttributeSet::getStackAlignment(unsigned Index) const {
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000876 AttributeSetNode *ASN = getAttributes(Index);
877 return ASN ? ASN->getStackAlignment() : 0;
Bill Wendlingd509a662013-01-29 00:34:06 +0000878}
879
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +0000880std::string AttributeSet::getAsString(unsigned Index,
Bill Wendling829b4782013-02-11 08:43:33 +0000881 bool InAttrGrp) const {
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000882 AttributeSetNode *ASN = getAttributes(Index);
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +0000883 return ASN ? ASN->getAsString(InAttrGrp) : std::string("");
Bill Wendlingd509a662013-01-29 00:34:06 +0000884}
885
886/// \brief The attributes for the specified index are returned.
Bill Wendling211316c2013-04-18 20:17:28 +0000887AttributeSetNode *AttributeSet::getAttributes(unsigned Index) const {
Craig Topperc6207612014-04-09 06:08:46 +0000888 if (!pImpl) return nullptr;
Bill Wendlingd509a662013-01-29 00:34:06 +0000889
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000890 // Loop through to find the attribute node we want.
891 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
Bill Wendling211316c2013-04-18 20:17:28 +0000892 if (pImpl->getSlotIndex(I) == Index)
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000893 return pImpl->getSlotNode(I);
Bill Wendlingd509a662013-01-29 00:34:06 +0000894
Craig Topperc6207612014-04-09 06:08:46 +0000895 return nullptr;
Bill Wendlingd509a662013-01-29 00:34:06 +0000896}
897
Bill Wendling211316c2013-04-18 20:17:28 +0000898AttributeSet::iterator AttributeSet::begin(unsigned Slot) const {
Bill Wendlinga9174862013-01-31 23:53:05 +0000899 if (!pImpl)
900 return ArrayRef<Attribute>().begin();
Bill Wendling211316c2013-04-18 20:17:28 +0000901 return pImpl->begin(Slot);
Bill Wendlinga9174862013-01-31 23:53:05 +0000902}
903
Bill Wendling211316c2013-04-18 20:17:28 +0000904AttributeSet::iterator AttributeSet::end(unsigned Slot) const {
Bill Wendlinga9174862013-01-31 23:53:05 +0000905 if (!pImpl)
906 return ArrayRef<Attribute>().end();
Bill Wendling211316c2013-04-18 20:17:28 +0000907 return pImpl->end(Slot);
Bill Wendlinga9174862013-01-31 23:53:05 +0000908}
909
Bill Wendlingd509a662013-01-29 00:34:06 +0000910//===----------------------------------------------------------------------===//
911// AttributeSet Introspection Methods
912//===----------------------------------------------------------------------===//
913
914/// \brief Return the number of slots used in this attribute list. This is the
915/// number of arguments that have an attribute set on them (including the
916/// function itself).
917unsigned AttributeSet::getNumSlots() const {
918 return pImpl ? pImpl->getNumAttributes() : 0;
919}
920
Rafael Espindoladd275302013-04-30 16:53:38 +0000921unsigned AttributeSet::getSlotIndex(unsigned Slot) const {
Bill Wendlingd509a662013-01-29 00:34:06 +0000922 assert(pImpl && Slot < pImpl->getNumAttributes() &&
923 "Slot # out of range!");
924 return pImpl->getSlotIndex(Slot);
925}
926
927AttributeSet AttributeSet::getSlotAttributes(unsigned Slot) const {
928 assert(pImpl && Slot < pImpl->getNumAttributes() &&
929 "Slot # out of range!");
930 return pImpl->getSlotAttributes(Slot);
931}
932
933uint64_t AttributeSet::Raw(unsigned Index) const {
934 // FIXME: Remove this.
935 return pImpl ? pImpl->Raw(Index) : 0;
936}
937
938void AttributeSet::dump() const {
939 dbgs() << "PAL[\n";
940
941 for (unsigned i = 0, e = getNumSlots(); i < e; ++i) {
942 uint64_t Index = getSlotIndex(i);
943 dbgs() << " { ";
944 if (Index == ~0U)
945 dbgs() << "~0U";
946 else
947 dbgs() << Index;
948 dbgs() << " => " << getAsString(Index) << " }\n";
949 }
950
951 dbgs() << "]\n";
952}
953
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000954//===----------------------------------------------------------------------===//
Bill Wendlingcd330342013-01-04 23:27:34 +0000955// AttrBuilder Method Implementations
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000956//===----------------------------------------------------------------------===//
957
Bill Wendling211316c2013-04-18 20:17:28 +0000958AttrBuilder::AttrBuilder(AttributeSet AS, unsigned Index)
Benjamin Kramer45e7d532013-02-16 19:13:18 +0000959 : Attrs(0), Alignment(0), StackAlignment(0) {
Bill Wendling56b0b2a2013-01-27 21:23:46 +0000960 AttributeSetImpl *pImpl = AS.pImpl;
Bill Wendling096f5442013-01-07 08:24:35 +0000961 if (!pImpl) return;
962
Bill Wendling9eb689c2013-01-28 00:21:34 +0000963 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I) {
Bill Wendling211316c2013-04-18 20:17:28 +0000964 if (pImpl->getSlotIndex(I) != Index) continue;
Bill Wendling096f5442013-01-07 08:24:35 +0000965
Benjamin Kramer741146b2013-07-11 12:13:16 +0000966 for (AttributeSetImpl::iterator II = pImpl->begin(I),
Bill Wendling9eb689c2013-01-28 00:21:34 +0000967 IE = pImpl->end(I); II != IE; ++II)
Bill Wendling23804da2013-01-31 23:38:01 +0000968 addAttribute(*II);
Bill Wendling9eb689c2013-01-28 00:21:34 +0000969
970 break;
971 }
Bill Wendling096f5442013-01-07 08:24:35 +0000972}
973
Bill Wendlingcd330342013-01-04 23:27:34 +0000974void AttrBuilder::clear() {
Benjamin Kramereaf706b2013-02-18 12:09:51 +0000975 Attrs.reset();
Bill Wendlingcd330342013-01-04 23:27:34 +0000976 Alignment = StackAlignment = 0;
977}
978
979AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
Benjamin Kramereaf706b2013-02-18 12:09:51 +0000980 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
Bill Wendling1c7cc8a2013-01-31 23:16:25 +0000981 assert(Val != Attribute::Alignment && Val != Attribute::StackAlignment &&
982 "Adding alignment attribute without adding alignment value!");
Benjamin Kramereaf706b2013-02-18 12:09:51 +0000983 Attrs[Val] = true;
Bill Wendling7c04e042012-10-09 19:01:18 +0000984 return *this;
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000985}
986
Bill Wendling23804da2013-01-31 23:38:01 +0000987AttrBuilder &AttrBuilder::addAttribute(Attribute Attr) {
Bill Wendling0a437302013-02-10 10:13:23 +0000988 if (Attr.isStringAttribute()) {
989 addAttribute(Attr.getKindAsString(), Attr.getValueAsString());
990 return *this;
991 }
992
Bill Wendling3f12ac22013-02-05 22:37:24 +0000993 Attribute::AttrKind Kind = Attr.getKindAsEnum();
Benjamin Kramereaf706b2013-02-18 12:09:51 +0000994 Attrs[Kind] = true;
Bill Wendling1aa9d9e2013-01-28 05:23:28 +0000995
Bill Wendling3f12ac22013-02-05 22:37:24 +0000996 if (Kind == Attribute::Alignment)
Bill Wendling1aa9d9e2013-01-28 05:23:28 +0000997 Alignment = Attr.getAlignment();
Bill Wendling3f12ac22013-02-05 22:37:24 +0000998 else if (Kind == Attribute::StackAlignment)
Bill Wendling1aa9d9e2013-01-28 05:23:28 +0000999 StackAlignment = Attr.getStackAlignment();
1000 return *this;
1001}
1002
Bill Wendlingb9c5b1a2013-02-05 08:09:32 +00001003AttrBuilder &AttrBuilder::addAttribute(StringRef A, StringRef V) {
1004 TargetDepAttrs[A] = V;
1005 return *this;
1006}
1007
Bill Wendling23804da2013-01-31 23:38:01 +00001008AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001009 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
1010 Attrs[Val] = false;
Bill Wendling23804da2013-01-31 23:38:01 +00001011
1012 if (Val == Attribute::Alignment)
1013 Alignment = 0;
1014 else if (Val == Attribute::StackAlignment)
1015 StackAlignment = 0;
1016
1017 return *this;
1018}
1019
Bill Wendlingd2196752013-01-30 23:07:40 +00001020AttrBuilder &AttrBuilder::removeAttributes(AttributeSet A, uint64_t Index) {
Bill Wendling211316c2013-04-18 20:17:28 +00001021 unsigned Slot = ~0U;
Bill Wendlingf1c94e32013-02-01 00:13:50 +00001022 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
1023 if (A.getSlotIndex(I) == Index) {
Bill Wendling211316c2013-04-18 20:17:28 +00001024 Slot = I;
Bill Wendlingf1c94e32013-02-01 00:13:50 +00001025 break;
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001026 }
Bill Wendlingf1c94e32013-02-01 00:13:50 +00001027
Bill Wendling211316c2013-04-18 20:17:28 +00001028 assert(Slot != ~0U && "Couldn't find index in AttributeSet!");
Bill Wendlingf1c94e32013-02-01 00:13:50 +00001029
Bill Wendling211316c2013-04-18 20:17:28 +00001030 for (AttributeSet::iterator I = A.begin(Slot), E = A.end(Slot); I != E; ++I) {
Bill Wendling7cde51d2013-02-12 07:56:49 +00001031 Attribute Attr = *I;
Hal Finkele15442c2014-07-18 06:51:55 +00001032 if (Attr.isEnumAttribute() || Attr.isIntAttribute()) {
Bill Wendling7cde51d2013-02-12 07:56:49 +00001033 Attribute::AttrKind Kind = I->getKindAsEnum();
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001034 Attrs[Kind] = false;
Bill Wendlingf1c94e32013-02-01 00:13:50 +00001035
Bill Wendling7cde51d2013-02-12 07:56:49 +00001036 if (Kind == Attribute::Alignment)
1037 Alignment = 0;
1038 else if (Kind == Attribute::StackAlignment)
1039 StackAlignment = 0;
1040 } else {
1041 assert(Attr.isStringAttribute() && "Invalid attribute type!");
1042 std::map<std::string, std::string>::iterator
1043 Iter = TargetDepAttrs.find(Attr.getKindAsString());
1044 if (Iter != TargetDepAttrs.end())
1045 TargetDepAttrs.erase(Iter);
1046 }
Bill Wendling1aa9d9e2013-01-28 05:23:28 +00001047 }
1048
1049 return *this;
1050}
1051
Bill Wendlingb9c5b1a2013-02-05 08:09:32 +00001052AttrBuilder &AttrBuilder::removeAttribute(StringRef A) {
1053 std::map<std::string, std::string>::iterator I = TargetDepAttrs.find(A);
1054 if (I != TargetDepAttrs.end())
1055 TargetDepAttrs.erase(I);
1056 return *this;
1057}
1058
Bill Wendling50d27842012-10-15 20:35:56 +00001059AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
Bill Wendlingabd5ba22012-10-14 03:58:29 +00001060 if (Align == 0) return *this;
Bill Wendlingcd330342013-01-04 23:27:34 +00001061
Bill Wendlingabf3feb2012-10-05 06:44:41 +00001062 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1063 assert(Align <= 0x40000000 && "Alignment too large.");
Bill Wendlingcd330342013-01-04 23:27:34 +00001064
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001065 Attrs[Attribute::Alignment] = true;
Bill Wendlingcd330342013-01-04 23:27:34 +00001066 Alignment = Align;
Bill Wendlingabd5ba22012-10-14 03:58:29 +00001067 return *this;
Chris Lattner3e13b8c2008-01-02 23:42:30 +00001068}
1069
Bill Wendlingcd330342013-01-04 23:27:34 +00001070AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
1071 // Default alignment, allow the target to define how to align it.
1072 if (Align == 0) return *this;
1073
1074 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1075 assert(Align <= 0x100 && "Alignment too large.");
1076
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001077 Attrs[Attribute::StackAlignment] = true;
Bill Wendlingcd330342013-01-04 23:27:34 +00001078 StackAlignment = Align;
1079 return *this;
1080}
1081
Bill Wendlinge2614922013-02-06 01:16:00 +00001082AttrBuilder &AttrBuilder::merge(const AttrBuilder &B) {
1083 // FIXME: What if both have alignments, but they don't match?!
1084 if (!Alignment)
1085 Alignment = B.Alignment;
1086
1087 if (!StackAlignment)
1088 StackAlignment = B.StackAlignment;
1089
Benjamin Kramer45e7d532013-02-16 19:13:18 +00001090 Attrs |= B.Attrs;
Bill Wendlinge2614922013-02-06 01:16:00 +00001091
1092 for (td_const_iterator I = B.TargetDepAttrs.begin(),
1093 E = B.TargetDepAttrs.end(); I != E; ++I)
1094 TargetDepAttrs[I->first] = I->second;
1095
1096 return *this;
1097}
1098
Bill Wendling4b001442013-02-06 01:33:42 +00001099bool AttrBuilder::contains(StringRef A) const {
1100 return TargetDepAttrs.find(A) != TargetDepAttrs.end();
1101}
1102
Bill Wendling50d27842012-10-15 20:35:56 +00001103bool AttrBuilder::hasAttributes() const {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001104 return !Attrs.none() || !TargetDepAttrs.empty();
Bill Wendlingc6daefa2012-10-08 23:27:46 +00001105}
Bill Wendling9ac69f92013-01-04 20:54:35 +00001106
Bill Wendlingd2196752013-01-30 23:07:40 +00001107bool AttrBuilder::hasAttributes(AttributeSet A, uint64_t Index) const {
Bill Wendling211316c2013-04-18 20:17:28 +00001108 unsigned Slot = ~0U;
Bill Wendling9ca01da2013-02-02 00:42:06 +00001109 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
1110 if (A.getSlotIndex(I) == Index) {
Bill Wendling211316c2013-04-18 20:17:28 +00001111 Slot = I;
Bill Wendling9ca01da2013-02-02 00:42:06 +00001112 break;
1113 }
1114
Bill Wendling211316c2013-04-18 20:17:28 +00001115 assert(Slot != ~0U && "Couldn't find the index!");
Bill Wendling9ca01da2013-02-02 00:42:06 +00001116
Bill Wendling211316c2013-04-18 20:17:28 +00001117 for (AttributeSet::iterator I = A.begin(Slot), E = A.end(Slot);
Bill Wendling7cde51d2013-02-12 07:56:49 +00001118 I != E; ++I) {
1119 Attribute Attr = *I;
Hal Finkele15442c2014-07-18 06:51:55 +00001120 if (Attr.isEnumAttribute() || Attr.isIntAttribute()) {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001121 if (Attrs[I->getKindAsEnum()])
Bill Wendling7cde51d2013-02-12 07:56:49 +00001122 return true;
1123 } else {
1124 assert(Attr.isStringAttribute() && "Invalid attribute kind!");
1125 return TargetDepAttrs.find(Attr.getKindAsString())!=TargetDepAttrs.end();
1126 }
1127 }
Bill Wendling9ca01da2013-02-02 00:42:06 +00001128
1129 return false;
Bill Wendling70f39172012-10-09 00:01:21 +00001130}
Bill Wendling9ac69f92013-01-04 20:54:35 +00001131
Bill Wendling50d27842012-10-15 20:35:56 +00001132bool AttrBuilder::hasAlignmentAttr() const {
Bill Wendlingcd330342013-01-04 23:27:34 +00001133 return Alignment != 0;
Bill Wendlingc6daefa2012-10-08 23:27:46 +00001134}
1135
Bill Wendlingd509a662013-01-29 00:34:06 +00001136bool AttrBuilder::operator==(const AttrBuilder &B) {
Benjamin Kramer45e7d532013-02-16 19:13:18 +00001137 if (Attrs != B.Attrs)
1138 return false;
Bill Wendling4b001442013-02-06 01:33:42 +00001139
1140 for (td_const_iterator I = TargetDepAttrs.begin(),
1141 E = TargetDepAttrs.end(); I != E; ++I)
1142 if (B.TargetDepAttrs.find(I->first) == B.TargetDepAttrs.end())
1143 return false;
1144
1145 return Alignment == B.Alignment && StackAlignment == B.StackAlignment;
Bill Wendlingd509a662013-01-29 00:34:06 +00001146}
1147
1148AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) {
Bill Wendling56aeccc2013-02-04 23:32:23 +00001149 // FIXME: Remove this in 4.0.
Bill Wendling60011b82013-01-29 01:43:29 +00001150 if (!Val) return *this;
1151
Bill Wendlingd509a662013-01-29 00:34:06 +00001152 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
1153 I = Attribute::AttrKind(I + 1)) {
1154 if (uint64_t A = (Val & AttributeImpl::getAttrMask(I))) {
Benjamin Kramereaf706b2013-02-18 12:09:51 +00001155 Attrs[I] = true;
Bill Wendlingd509a662013-01-29 00:34:06 +00001156
1157 if (I == Attribute::Alignment)
1158 Alignment = 1ULL << ((A >> 16) - 1);
1159 else if (I == Attribute::StackAlignment)
1160 StackAlignment = 1ULL << ((A >> 26)-1);
1161 }
1162 }
1163
1164 return *this;
1165}
1166
Bill Wendling57625a42013-01-25 23:09:36 +00001167//===----------------------------------------------------------------------===//
1168// AttributeFuncs Function Defintions
1169//===----------------------------------------------------------------------===//
1170
Bill Wendlingc79cdff2013-02-01 01:04:27 +00001171/// \brief Which attributes cannot be applied to a type.
Bill Wendlingd2196752013-01-30 23:07:40 +00001172AttributeSet AttributeFuncs::typeIncompatible(Type *Ty, uint64_t Index) {
Bill Wendling57625a42013-01-25 23:09:36 +00001173 AttrBuilder Incompatible;
1174
1175 if (!Ty->isIntegerTy())
1176 // Attribute that only apply to integers.
1177 Incompatible.addAttribute(Attribute::SExt)
1178 .addAttribute(Attribute::ZExt);
1179
1180 if (!Ty->isPointerTy())
1181 // Attribute that only apply to pointers.
1182 Incompatible.addAttribute(Attribute::ByVal)
1183 .addAttribute(Attribute::Nest)
1184 .addAttribute(Attribute::NoAlias)
1185 .addAttribute(Attribute::NoCapture)
Nick Lewyckyd52b1522014-05-20 01:23:40 +00001186 .addAttribute(Attribute::NonNull)
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001187 .addAttribute(Attribute::ReadNone)
1188 .addAttribute(Attribute::ReadOnly)
Reid Klecknera534a382013-12-19 02:14:12 +00001189 .addAttribute(Attribute::StructRet)
1190 .addAttribute(Attribute::InAlloca);
Bill Wendling57625a42013-01-25 23:09:36 +00001191
Bill Wendlingd2196752013-01-30 23:07:40 +00001192 return AttributeSet::get(Ty->getContext(), Index, Incompatible);
Bill Wendling57625a42013-01-25 23:09:36 +00001193}