blob: 48743fae692d52122c691a2b7f5ca4e2674e5f77 [file] [log] [blame]
Bill Wendling87e10df2013-01-28 21:55:20 +00001//===-- Attributes.cpp - Implement AttributesList -------------------------===//
Chris Lattner50ee9dd2008-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 Wendling87e10df2013-01-28 21:55:20 +000010// \file
11// \brief This file implements the Attribute, AttributeImpl, AttrBuilder,
Bill Wendling18e72112012-12-19 22:42:22 +000012// AttributeSetImpl, and AttributeSet classes.
Chris Lattner50ee9dd2008-01-02 23:42:30 +000013//
14//===----------------------------------------------------------------------===//
15
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000016#include "llvm/IR/Attributes.h"
Bill Wendlingf6670722012-12-20 01:36:59 +000017#include "AttributeImpl.h"
Bill Wendling2c79ecb2012-09-26 21:07:29 +000018#include "LLVMContextImpl.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000019#include "llvm/ADT/StringExtras.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000020#include "llvm/IR/Type.h"
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000021#include "llvm/Support/Atomic.h"
David Greeneef1894e2010-01-05 01:29:58 +000022#include "llvm/Support/Debug.h"
Chris Lattner50ee9dd2008-01-02 23:42:30 +000023#include "llvm/Support/ManagedStatic.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000024#include "llvm/Support/Mutex.h"
Benjamin Kramercfa6ec92009-08-23 11:37:21 +000025#include "llvm/Support/raw_ostream.h"
Bill Wendling3467e302013-01-24 00:06:56 +000026#include <algorithm>
Chris Lattner50ee9dd2008-01-02 23:42:30 +000027using namespace llvm;
28
Chris Lattner58d74912008-03-12 17:45:29 +000029//===----------------------------------------------------------------------===//
Bill Wendling817abdd2013-01-29 00:48:16 +000030// Attribute Construction Methods
Chris Lattner58d74912008-03-12 17:45:29 +000031//===----------------------------------------------------------------------===//
Chris Lattnerfabfde32008-01-03 00:10:22 +000032
Bill Wendling8c74ecf2013-02-05 22:37:24 +000033Attribute Attribute::get(LLVMContext &Context, Attribute::AttrKind Kind,
34 uint64_t Val) {
Bill Wendling8e635db2012-10-08 21:47:17 +000035 LLVMContextImpl *pImpl = Context.pImpl;
36 FoldingSetNodeID ID;
Bill Wendling8c74ecf2013-02-05 22:37:24 +000037 ID.AddInteger(Kind);
38 if (Val) ID.AddInteger(Val);
39
40 void *InsertPoint;
41 AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
42
43 if (!PA) {
44 // If we didn't find any existing attributes of the same shape then create a
45 // new one and insert it.
Benjamin Kramere22cde02013-07-11 12:13:16 +000046 if (!Val)
47 PA = new EnumAttributeImpl(Kind);
48 else
49 PA = new AlignAttributeImpl(Kind, Val);
Bill Wendling8c74ecf2013-02-05 22:37:24 +000050 pImpl->AttrsSet.InsertNode(PA, InsertPoint);
51 }
52
53 // Return the Attribute that we found or created.
54 return Attribute(PA);
55}
56
57Attribute Attribute::get(LLVMContext &Context, StringRef Kind, StringRef Val) {
58 LLVMContextImpl *pImpl = Context.pImpl;
59 FoldingSetNodeID ID;
60 ID.AddString(Kind);
61 if (!Val.empty()) ID.AddString(Val);
Bill Wendling8e635db2012-10-08 21:47:17 +000062
63 void *InsertPoint;
Bill Wendlingf6670722012-12-20 01:36:59 +000064 AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
Bill Wendling8e635db2012-10-08 21:47:17 +000065
66 if (!PA) {
67 // If we didn't find any existing attributes of the same shape then create a
68 // new one and insert it.
Benjamin Kramere22cde02013-07-11 12:13:16 +000069 PA = new StringAttributeImpl(Kind, Val);
Bill Wendling8e635db2012-10-08 21:47:17 +000070 pImpl->AttrsSet.InsertNode(PA, InsertPoint);
71 }
72
Bill Wendlingea59f892013-02-05 08:09:32 +000073 // Return the Attribute that we found or created.
Bill Wendling034b94b2012-12-19 07:18:57 +000074 return Attribute(PA);
Bill Wendling8e635db2012-10-08 21:47:17 +000075}
76
Bill Wendlingc08a5ef2013-01-27 22:43:04 +000077Attribute Attribute::getWithAlignment(LLVMContext &Context, uint64_t Align) {
Bill Wendling169d5272013-01-31 23:16:25 +000078 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
79 assert(Align <= 0x40000000 && "Alignment too large.");
Bill Wendling8c74ecf2013-02-05 22:37:24 +000080 return get(Context, Alignment, Align);
Bill Wendlingc08a5ef2013-01-27 22:43:04 +000081}
82
83Attribute Attribute::getWithStackAlignment(LLVMContext &Context,
84 uint64_t Align) {
Bill Wendling169d5272013-01-31 23:16:25 +000085 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
86 assert(Align <= 0x100 && "Alignment too large.");
Bill Wendling8c74ecf2013-02-05 22:37:24 +000087 return get(Context, StackAlignment, Align);
Bill Wendlingc08a5ef2013-01-27 22:43:04 +000088}
89
Bill Wendling817abdd2013-01-29 00:48:16 +000090//===----------------------------------------------------------------------===//
91// Attribute Accessor Methods
92//===----------------------------------------------------------------------===//
93
Bill Wendling8c74ecf2013-02-05 22:37:24 +000094bool Attribute::isEnumAttribute() const {
95 return pImpl && pImpl->isEnumAttribute();
96}
97
98bool Attribute::isAlignAttribute() const {
99 return pImpl && pImpl->isAlignAttribute();
100}
101
102bool Attribute::isStringAttribute() const {
103 return pImpl && pImpl->isStringAttribute();
104}
105
106Attribute::AttrKind Attribute::getKindAsEnum() const {
Bill Wendlingf245ae52013-07-25 00:34:29 +0000107 if (!pImpl) return None;
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000108 assert((isEnumAttribute() || isAlignAttribute()) &&
109 "Invalid attribute type to get the kind as an enum!");
110 return pImpl ? pImpl->getKindAsEnum() : None;
111}
112
113uint64_t Attribute::getValueAsInt() const {
Bill Wendlingf245ae52013-07-25 00:34:29 +0000114 if (!pImpl) return 0;
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000115 assert(isAlignAttribute() &&
116 "Expected the attribute to be an alignment attribute!");
117 return pImpl ? pImpl->getValueAsInt() : 0;
118}
119
120StringRef Attribute::getKindAsString() const {
Bill Wendlingf245ae52013-07-25 00:34:29 +0000121 if (!pImpl) return StringRef();
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000122 assert(isStringAttribute() &&
123 "Invalid attribute type to get the kind as a string!");
124 return pImpl ? pImpl->getKindAsString() : StringRef();
125}
126
127StringRef Attribute::getValueAsString() const {
Bill Wendlingf245ae52013-07-25 00:34:29 +0000128 if (!pImpl) return StringRef();
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000129 assert(isStringAttribute() &&
130 "Invalid attribute type to get the value as a string!");
131 return pImpl ? pImpl->getValueAsString() : StringRef();
132}
133
Bill Wendling64754f42013-02-05 23:48:36 +0000134bool Attribute::hasAttribute(AttrKind Kind) const {
135 return (pImpl && pImpl->hasAttribute(Kind)) || (!pImpl && Kind == None);
136}
137
138bool Attribute::hasAttribute(StringRef Kind) const {
139 if (!isStringAttribute()) return false;
140 return pImpl && pImpl->hasAttribute(Kind);
Bill Wendling6dc37812013-01-29 20:45:34 +0000141}
142
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000143/// This returns the alignment field of an attribute as a byte alignment value.
Bill Wendling034b94b2012-12-19 07:18:57 +0000144unsigned Attribute::getAlignment() const {
Bill Wendling7beee282013-02-01 01:04:27 +0000145 assert(hasAttribute(Attribute::Alignment) &&
146 "Trying to get alignment from non-alignment attribute!");
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000147 return pImpl->getValueAsInt();
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000148}
149
150/// This returns the stack alignment field of an attribute as a byte alignment
151/// value.
Bill Wendling034b94b2012-12-19 07:18:57 +0000152unsigned Attribute::getStackAlignment() const {
Bill Wendling7beee282013-02-01 01:04:27 +0000153 assert(hasAttribute(Attribute::StackAlignment) &&
154 "Trying to get alignment from non-alignment attribute!");
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000155 return pImpl->getValueAsInt();
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000156}
157
Bill Wendlingb29ce262013-02-11 08:43:33 +0000158std::string Attribute::getAsString(bool InAttrGrp) const {
Bill Wendling14292a62013-01-31 20:59:05 +0000159 if (!pImpl) return "";
160
Kostya Serebryany8eec41f2013-02-26 06:58:09 +0000161 if (hasAttribute(Attribute::SanitizeAddress))
162 return "sanitize_address";
Bill Wendling14292a62013-01-31 20:59:05 +0000163 if (hasAttribute(Attribute::AlwaysInline))
164 return "alwaysinline";
Michael Gottesman2253a2f2013-06-27 00:25:01 +0000165 if (hasAttribute(Attribute::Builtin))
166 return "builtin";
Bill Wendling14292a62013-01-31 20:59:05 +0000167 if (hasAttribute(Attribute::ByVal))
168 return "byval";
169 if (hasAttribute(Attribute::InlineHint))
170 return "inlinehint";
Bill Wendling034b94b2012-12-19 07:18:57 +0000171 if (hasAttribute(Attribute::InReg))
Bill Wendling606c8e32013-01-29 03:20:31 +0000172 return "inreg";
Bill Wendling14292a62013-01-31 20:59:05 +0000173 if (hasAttribute(Attribute::MinSize))
174 return "minsize";
175 if (hasAttribute(Attribute::Naked))
176 return "naked";
177 if (hasAttribute(Attribute::Nest))
178 return "nest";
Bill Wendling034b94b2012-12-19 07:18:57 +0000179 if (hasAttribute(Attribute::NoAlias))
Bill Wendling606c8e32013-01-29 03:20:31 +0000180 return "noalias";
Bill Wendling143d4642013-02-22 00:12:35 +0000181 if (hasAttribute(Attribute::NoBuiltin))
182 return "nobuiltin";
Bill Wendling034b94b2012-12-19 07:18:57 +0000183 if (hasAttribute(Attribute::NoCapture))
Bill Wendling606c8e32013-01-29 03:20:31 +0000184 return "nocapture";
Bill Wendling14292a62013-01-31 20:59:05 +0000185 if (hasAttribute(Attribute::NoDuplicate))
186 return "noduplicate";
187 if (hasAttribute(Attribute::NoImplicitFloat))
188 return "noimplicitfloat";
189 if (hasAttribute(Attribute::NoInline))
190 return "noinline";
191 if (hasAttribute(Attribute::NonLazyBind))
192 return "nonlazybind";
193 if (hasAttribute(Attribute::NoRedZone))
194 return "noredzone";
195 if (hasAttribute(Attribute::NoReturn))
196 return "noreturn";
197 if (hasAttribute(Attribute::NoUnwind))
198 return "nounwind";
199 if (hasAttribute(Attribute::OptimizeForSize))
200 return "optsize";
Bill Wendling034b94b2012-12-19 07:18:57 +0000201 if (hasAttribute(Attribute::ReadNone))
Bill Wendling606c8e32013-01-29 03:20:31 +0000202 return "readnone";
Bill Wendling034b94b2012-12-19 07:18:57 +0000203 if (hasAttribute(Attribute::ReadOnly))
Bill Wendling606c8e32013-01-29 03:20:31 +0000204 return "readonly";
Stephen Lin456ca042013-04-20 05:14:40 +0000205 if (hasAttribute(Attribute::Returned))
206 return "returned";
Bill Wendling14292a62013-01-31 20:59:05 +0000207 if (hasAttribute(Attribute::ReturnsTwice))
208 return "returns_twice";
209 if (hasAttribute(Attribute::SExt))
210 return "signext";
Bill Wendling034b94b2012-12-19 07:18:57 +0000211 if (hasAttribute(Attribute::StackProtect))
Bill Wendling606c8e32013-01-29 03:20:31 +0000212 return "ssp";
Bill Wendling034b94b2012-12-19 07:18:57 +0000213 if (hasAttribute(Attribute::StackProtectReq))
Bill Wendling606c8e32013-01-29 03:20:31 +0000214 return "sspreq";
Bill Wendling114baee2013-01-23 06:41:41 +0000215 if (hasAttribute(Attribute::StackProtectStrong))
Bill Wendling606c8e32013-01-29 03:20:31 +0000216 return "sspstrong";
Bill Wendling14292a62013-01-31 20:59:05 +0000217 if (hasAttribute(Attribute::StructRet))
218 return "sret";
Kostya Serebryany8eec41f2013-02-26 06:58:09 +0000219 if (hasAttribute(Attribute::SanitizeThread))
220 return "sanitize_thread";
221 if (hasAttribute(Attribute::SanitizeMemory))
222 return "sanitize_memory";
Bill Wendling14292a62013-01-31 20:59:05 +0000223 if (hasAttribute(Attribute::UWTable))
224 return "uwtable";
225 if (hasAttribute(Attribute::ZExt))
226 return "zeroext";
Diego Novillo77226a02013-05-24 12:26:52 +0000227 if (hasAttribute(Attribute::Cold))
228 return "cold";
Bill Wendling14292a62013-01-31 20:59:05 +0000229
230 // FIXME: These should be output like this:
231 //
232 // align=4
233 // alignstack=8
234 //
Bill Wendling034b94b2012-12-19 07:18:57 +0000235 if (hasAttribute(Attribute::Alignment)) {
Bill Wendling606c8e32013-01-29 03:20:31 +0000236 std::string Result;
Bill Wendlingb29ce262013-02-11 08:43:33 +0000237 Result += "align";
238 Result += (InAttrGrp) ? "=" : " ";
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000239 Result += utostr(getValueAsInt());
240 return Result;
241 }
Bill Wendlingb29ce262013-02-11 08:43:33 +0000242
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000243 if (hasAttribute(Attribute::StackAlignment)) {
244 std::string Result;
Bill Wendlingb29ce262013-02-11 08:43:33 +0000245 Result += "alignstack";
246 if (InAttrGrp) {
247 Result += "=";
248 Result += utostr(getValueAsInt());
249 } else {
250 Result += "(";
251 Result += utostr(getValueAsInt());
252 Result += ")";
253 }
Bill Wendling606c8e32013-01-29 03:20:31 +0000254 return Result;
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000255 }
Bill Wendling14292a62013-01-31 20:59:05 +0000256
257 // Convert target-dependent attributes to strings of the form:
258 //
259 // "kind"
260 // "kind" = "value"
Bill Wendling14292a62013-01-31 20:59:05 +0000261 //
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000262 if (isStringAttribute()) {
Bill Wendling14292a62013-01-31 20:59:05 +0000263 std::string Result;
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000264 Result += '\"' + getKindAsString().str() + '"';
Bill Wendling14292a62013-01-31 20:59:05 +0000265
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000266 StringRef Val = pImpl->getValueAsString();
267 if (Val.empty()) return Result;
Bill Wendling5a4041e2013-02-01 22:32:30 +0000268
Bill Wendlingb29ce262013-02-11 08:43:33 +0000269 Result += "=\"" + Val.str() + '"';
Bill Wendling7beee282013-02-01 01:04:27 +0000270 return Result;
Bill Wendling14292a62013-01-31 20:59:05 +0000271 }
Bill Wendling606c8e32013-01-29 03:20:31 +0000272
273 llvm_unreachable("Unknown attribute");
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000274}
275
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000276bool Attribute::operator<(Attribute A) const {
277 if (!pImpl && !A.pImpl) return false;
278 if (!pImpl) return true;
279 if (!A.pImpl) return false;
280 return *pImpl < *A.pImpl;
281}
282
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000283//===----------------------------------------------------------------------===//
284// AttributeImpl Definition
285//===----------------------------------------------------------------------===//
286
Benjamin Kramere22cde02013-07-11 12:13:16 +0000287AttributeImpl::~AttributeImpl() {}
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000288
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000289bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000290 if (isStringAttribute()) return false;
291 return getKindAsEnum() == A;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000292}
293
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000294bool AttributeImpl::hasAttribute(StringRef Kind) const {
295 if (!isStringAttribute()) return false;
296 return getKindAsString() == Kind;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000297}
298
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000299Attribute::AttrKind AttributeImpl::getKindAsEnum() const {
Benjamin Kramere22cde02013-07-11 12:13:16 +0000300 assert(isEnumAttribute() || isAlignAttribute());
301 return static_cast<const EnumAttributeImpl *>(this)->getEnumKind();
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000302}
303
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000304uint64_t AttributeImpl::getValueAsInt() const {
Benjamin Kramere22cde02013-07-11 12:13:16 +0000305 assert(isAlignAttribute());
306 return static_cast<const AlignAttributeImpl *>(this)->getAlignment();
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000307}
308
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000309StringRef AttributeImpl::getKindAsString() const {
Benjamin Kramere22cde02013-07-11 12:13:16 +0000310 assert(isStringAttribute());
311 return static_cast<const StringAttributeImpl *>(this)->getStringKind();
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000312}
313
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000314StringRef AttributeImpl::getValueAsString() const {
Benjamin Kramere22cde02013-07-11 12:13:16 +0000315 assert(isStringAttribute());
316 return static_cast<const StringAttributeImpl *>(this)->getStringValue();
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000317}
318
319bool AttributeImpl::operator<(const AttributeImpl &AI) const {
Bill Wendling7beee282013-02-01 01:04:27 +0000320 // This sorts the attributes with Attribute::AttrKinds coming first (sorted
321 // relative to their enum value) and then strings.
Bill Wendling94328f42013-02-15 05:25:26 +0000322 if (isEnumAttribute()) {
323 if (AI.isEnumAttribute()) return getKindAsEnum() < AI.getKindAsEnum();
324 if (AI.isAlignAttribute()) return true;
325 if (AI.isStringAttribute()) return true;
326 }
Bill Wendling7beee282013-02-01 01:04:27 +0000327
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000328 if (isAlignAttribute()) {
Bill Wendling94328f42013-02-15 05:25:26 +0000329 if (AI.isEnumAttribute()) return false;
330 if (AI.isAlignAttribute()) return getValueAsInt() < AI.getValueAsInt();
331 if (AI.isStringAttribute()) return true;
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000332 }
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000333
Bill Wendling94328f42013-02-15 05:25:26 +0000334 if (AI.isEnumAttribute()) return false;
335 if (AI.isAlignAttribute()) return false;
336 if (getKindAsString() == AI.getKindAsString())
337 return getValueAsString() < AI.getValueAsString();
338 return getKindAsString() < AI.getKindAsString();
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000339}
340
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000341uint64_t AttributeImpl::getAttrMask(Attribute::AttrKind Val) {
342 // FIXME: Remove this.
343 switch (Val) {
344 case Attribute::EndAttrKinds:
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000345 llvm_unreachable("Synthetic enumerators which should never get here");
346
347 case Attribute::None: return 0;
348 case Attribute::ZExt: return 1 << 0;
349 case Attribute::SExt: return 1 << 1;
350 case Attribute::NoReturn: return 1 << 2;
351 case Attribute::InReg: return 1 << 3;
352 case Attribute::StructRet: return 1 << 4;
353 case Attribute::NoUnwind: return 1 << 5;
354 case Attribute::NoAlias: return 1 << 6;
355 case Attribute::ByVal: return 1 << 7;
356 case Attribute::Nest: return 1 << 8;
357 case Attribute::ReadNone: return 1 << 9;
358 case Attribute::ReadOnly: return 1 << 10;
359 case Attribute::NoInline: return 1 << 11;
360 case Attribute::AlwaysInline: return 1 << 12;
361 case Attribute::OptimizeForSize: return 1 << 13;
362 case Attribute::StackProtect: return 1 << 14;
363 case Attribute::StackProtectReq: return 1 << 15;
364 case Attribute::Alignment: return 31 << 16;
365 case Attribute::NoCapture: return 1 << 21;
366 case Attribute::NoRedZone: return 1 << 22;
367 case Attribute::NoImplicitFloat: return 1 << 23;
368 case Attribute::Naked: return 1 << 24;
369 case Attribute::InlineHint: return 1 << 25;
370 case Attribute::StackAlignment: return 7 << 26;
371 case Attribute::ReturnsTwice: return 1 << 29;
372 case Attribute::UWTable: return 1 << 30;
373 case Attribute::NonLazyBind: return 1U << 31;
Kostya Serebryany8eec41f2013-02-26 06:58:09 +0000374 case Attribute::SanitizeAddress: return 1ULL << 32;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000375 case Attribute::MinSize: return 1ULL << 33;
376 case Attribute::NoDuplicate: return 1ULL << 34;
377 case Attribute::StackProtectStrong: return 1ULL << 35;
Kostya Serebryany8eec41f2013-02-26 06:58:09 +0000378 case Attribute::SanitizeThread: return 1ULL << 36;
379 case Attribute::SanitizeMemory: return 1ULL << 37;
Bill Wendlingd18e0b92013-02-22 00:40:12 +0000380 case Attribute::NoBuiltin: return 1ULL << 38;
Stephen Lin456ca042013-04-20 05:14:40 +0000381 case Attribute::Returned: return 1ULL << 39;
Diego Novillo77226a02013-05-24 12:26:52 +0000382 case Attribute::Cold: return 1ULL << 40;
Michael Gottesman2253a2f2013-06-27 00:25:01 +0000383 case Attribute::Builtin: return 1ULL << 41;
Bill Wendlingbd2acfa2013-02-22 00:50:09 +0000384 }
385 llvm_unreachable("Unsupported attribute type");
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000386}
387
388//===----------------------------------------------------------------------===//
389// AttributeSetNode Definition
390//===----------------------------------------------------------------------===//
391
392AttributeSetNode *AttributeSetNode::get(LLVMContext &C,
393 ArrayRef<Attribute> Attrs) {
394 if (Attrs.empty())
395 return 0;
396
397 // Otherwise, build a key to look up the existing attributes.
398 LLVMContextImpl *pImpl = C.pImpl;
399 FoldingSetNodeID ID;
400
401 SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end());
Bill Wendlingf107e6c2013-02-13 09:26:26 +0000402 array_pod_sort(SortedAttrs.begin(), SortedAttrs.end());
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000403
404 for (SmallVectorImpl<Attribute>::iterator I = SortedAttrs.begin(),
405 E = SortedAttrs.end(); I != E; ++I)
406 I->Profile(ID);
407
408 void *InsertPoint;
409 AttributeSetNode *PA =
410 pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint);
411
412 // If we didn't find any existing attributes of the same shape then create a
413 // new one and insert it.
414 if (!PA) {
Benjamin Kramere22cde02013-07-11 12:13:16 +0000415 // Coallocate entries after the AttributeSetNode itself.
416 void *Mem = ::operator new(sizeof(AttributeSetNode) +
417 sizeof(Attribute) * SortedAttrs.size());
418 PA = new (Mem) AttributeSetNode(SortedAttrs);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000419 pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint);
420 }
421
422 // Return the AttributesListNode that we found or created.
423 return PA;
424}
425
Bill Wendling606c8e32013-01-29 03:20:31 +0000426bool AttributeSetNode::hasAttribute(Attribute::AttrKind Kind) const {
Benjamin Kramere22cde02013-07-11 12:13:16 +0000427 for (iterator I = begin(), E = end(); I != E; ++I)
NAKAMURA Takumieddab152013-01-31 03:47:28 +0000428 if (I->hasAttribute(Kind))
Bill Wendling606c8e32013-01-29 03:20:31 +0000429 return true;
430 return false;
431}
432
Bill Wendling0e9d5d02013-02-13 08:42:21 +0000433bool AttributeSetNode::hasAttribute(StringRef Kind) const {
Benjamin Kramere22cde02013-07-11 12:13:16 +0000434 for (iterator I = begin(), E = end(); I != E; ++I)
Bill Wendling0e9d5d02013-02-13 08:42:21 +0000435 if (I->hasAttribute(Kind))
436 return true;
437 return false;
438}
439
440Attribute AttributeSetNode::getAttribute(Attribute::AttrKind Kind) const {
Benjamin Kramere22cde02013-07-11 12:13:16 +0000441 for (iterator I = begin(), E = end(); I != E; ++I)
Bill Wendling0e9d5d02013-02-13 08:42:21 +0000442 if (I->hasAttribute(Kind))
443 return *I;
444 return Attribute();
445}
446
447Attribute AttributeSetNode::getAttribute(StringRef Kind) const {
Benjamin Kramere22cde02013-07-11 12:13:16 +0000448 for (iterator I = begin(), E = end(); I != E; ++I)
Bill Wendling0e9d5d02013-02-13 08:42:21 +0000449 if (I->hasAttribute(Kind))
450 return *I;
451 return Attribute();
452}
453
Bill Wendling606c8e32013-01-29 03:20:31 +0000454unsigned AttributeSetNode::getAlignment() const {
Benjamin Kramere22cde02013-07-11 12:13:16 +0000455 for (iterator I = begin(), E = end(); I != E; ++I)
NAKAMURA Takumieddab152013-01-31 03:47:28 +0000456 if (I->hasAttribute(Attribute::Alignment))
Bill Wendling606c8e32013-01-29 03:20:31 +0000457 return I->getAlignment();
458 return 0;
459}
460
461unsigned AttributeSetNode::getStackAlignment() const {
Benjamin Kramere22cde02013-07-11 12:13:16 +0000462 for (iterator I = begin(), E = end(); I != E; ++I)
NAKAMURA Takumieddab152013-01-31 03:47:28 +0000463 if (I->hasAttribute(Attribute::StackAlignment))
Bill Wendling606c8e32013-01-29 03:20:31 +0000464 return I->getStackAlignment();
465 return 0;
466}
467
Rafael Espindolaaae02982013-05-01 13:07:03 +0000468std::string AttributeSetNode::getAsString(bool InAttrGrp) const {
Benjamin Kramere94e4ca2013-04-19 11:43:21 +0000469 std::string Str;
Benjamin Kramere22cde02013-07-11 12:13:16 +0000470 for (iterator I = begin(), E = end(); I != E; ++I) {
471 if (I != begin())
Rafael Espindolaaae02982013-05-01 13:07:03 +0000472 Str += ' ';
473 Str += I->getAsString(InAttrGrp);
Bill Wendling606c8e32013-01-29 03:20:31 +0000474 }
475 return Str;
476}
477
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000478//===----------------------------------------------------------------------===//
479// AttributeSetImpl Definition
480//===----------------------------------------------------------------------===//
481
Rafael Espindola76f103e2013-04-30 16:53:38 +0000482uint64_t AttributeSetImpl::Raw(unsigned Index) const {
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000483 for (unsigned I = 0, E = getNumAttributes(); I != E; ++I) {
484 if (getSlotIndex(I) != Index) continue;
Benjamin Kramere22cde02013-07-11 12:13:16 +0000485 const AttributeSetNode *ASN = getSlotNode(I);
Bill Wendlingfca0ed22013-02-02 00:52:44 +0000486 uint64_t Mask = 0;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000487
Benjamin Kramere22cde02013-07-11 12:13:16 +0000488 for (AttributeSetNode::iterator II = ASN->begin(),
Bill Wendlingfca0ed22013-02-02 00:52:44 +0000489 IE = ASN->end(); II != IE; ++II) {
490 Attribute Attr = *II;
Bill Wendling21536912013-02-10 23:18:05 +0000491
492 // This cannot handle string attributes.
493 if (Attr.isStringAttribute()) continue;
494
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000495 Attribute::AttrKind Kind = Attr.getKindAsEnum();
Bill Wendlingfca0ed22013-02-02 00:52:44 +0000496
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000497 if (Kind == Attribute::Alignment)
Bill Wendlingfca0ed22013-02-02 00:52:44 +0000498 Mask |= (Log2_32(ASN->getAlignment()) + 1) << 16;
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000499 else if (Kind == Attribute::StackAlignment)
Bill Wendlingfca0ed22013-02-02 00:52:44 +0000500 Mask |= (Log2_32(ASN->getStackAlignment()) + 1) << 26;
501 else
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000502 Mask |= AttributeImpl::getAttrMask(Kind);
Bill Wendlingfca0ed22013-02-02 00:52:44 +0000503 }
504
505 return Mask;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000506 }
507
508 return 0;
509}
510
511//===----------------------------------------------------------------------===//
512// AttributeSet Construction and Mutation Methods
513//===----------------------------------------------------------------------===//
514
Bill Wendling8232ece2013-01-29 01:43:29 +0000515AttributeSet
516AttributeSet::getImpl(LLVMContext &C,
517 ArrayRef<std::pair<unsigned, AttributeSetNode*> > Attrs) {
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000518 LLVMContextImpl *pImpl = C.pImpl;
519 FoldingSetNodeID ID;
520 AttributeSetImpl::Profile(ID, Attrs);
521
522 void *InsertPoint;
523 AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
524
525 // If we didn't find any existing attributes of the same shape then
526 // create a new one and insert it.
527 if (!PA) {
Benjamin Kramere22cde02013-07-11 12:13:16 +0000528 // Coallocate entries after the AttributeSetImpl itself.
529 void *Mem = ::operator new(sizeof(AttributeSetImpl) +
530 sizeof(std::pair<unsigned, AttributeSetNode *>) *
531 Attrs.size());
532 PA = new (Mem) AttributeSetImpl(C, Attrs);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000533 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
534 }
535
536 // Return the AttributesList that we found or created.
537 return AttributeSet(PA);
538}
539
540AttributeSet AttributeSet::get(LLVMContext &C,
541 ArrayRef<std::pair<unsigned, Attribute> > Attrs){
542 // If there are no attributes then return a null AttributesList pointer.
543 if (Attrs.empty())
544 return AttributeSet();
545
546#ifndef NDEBUG
547 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
548 assert((!i || Attrs[i-1].first <= Attrs[i].first) &&
549 "Misordered Attributes list!");
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000550 assert(!Attrs[i].second.hasAttribute(Attribute::None) &&
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000551 "Pointless attribute!");
552 }
553#endif
554
555 // Create a vector if (unsigned, AttributeSetNode*) pairs from the attributes
556 // list.
557 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrPairVec;
558 for (ArrayRef<std::pair<unsigned, Attribute> >::iterator I = Attrs.begin(),
559 E = Attrs.end(); I != E; ) {
560 unsigned Index = I->first;
561 SmallVector<Attribute, 4> AttrVec;
NAKAMURA Takumi3ba51ce2013-01-29 15:18:16 +0000562 while (I != E && I->first == Index) {
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000563 AttrVec.push_back(I->second);
564 ++I;
565 }
566
567 AttrPairVec.push_back(std::make_pair(Index,
568 AttributeSetNode::get(C, AttrVec)));
569 }
570
571 return getImpl(C, AttrPairVec);
572}
573
574AttributeSet AttributeSet::get(LLVMContext &C,
575 ArrayRef<std::pair<unsigned,
576 AttributeSetNode*> > Attrs) {
577 // If there are no attributes then return a null AttributesList pointer.
578 if (Attrs.empty())
579 return AttributeSet();
580
581 return getImpl(C, Attrs);
582}
583
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000584AttributeSet AttributeSet::get(LLVMContext &C, unsigned Index, AttrBuilder &B) {
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000585 if (!B.hasAttributes())
586 return AttributeSet();
Bill Wendling8fbc0c22013-01-29 01:02:03 +0000587
Bill Wendling64754f42013-02-05 23:48:36 +0000588 // Add target-independent attributes.
Bill Wendling8fbc0c22013-01-29 01:02:03 +0000589 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
Benjamin Kramerc835b8c2013-02-16 19:13:18 +0000590 for (Attribute::AttrKind Kind = Attribute::None;
Benjamin Kramer7c146122013-02-16 19:22:28 +0000591 Kind != Attribute::EndAttrKinds; Kind = Attribute::AttrKind(Kind + 1)) {
Benjamin Kramerc835b8c2013-02-16 19:13:18 +0000592 if (!B.contains(Kind))
593 continue;
594
Bill Wendling8fbc0c22013-01-29 01:02:03 +0000595 if (Kind == Attribute::Alignment)
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000596 Attrs.push_back(std::make_pair(Index, Attribute::
Bill Wendling8fbc0c22013-01-29 01:02:03 +0000597 getWithAlignment(C, B.getAlignment())));
598 else if (Kind == Attribute::StackAlignment)
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000599 Attrs.push_back(std::make_pair(Index, Attribute::
Bill Wendling8fbc0c22013-01-29 01:02:03 +0000600 getWithStackAlignment(C, B.getStackAlignment())));
601 else
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000602 Attrs.push_back(std::make_pair(Index, Attribute::get(C, Kind)));
Bill Wendling8fbc0c22013-01-29 01:02:03 +0000603 }
604
Bill Wendling64754f42013-02-05 23:48:36 +0000605 // Add target-dependent (string) attributes.
606 for (AttrBuilder::td_iterator I = B.td_begin(), E = B.td_end();
607 I != E; ++I)
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000608 Attrs.push_back(std::make_pair(Index, Attribute::get(C, I->first,I->second)));
Bill Wendling64754f42013-02-05 23:48:36 +0000609
Bill Wendling8fbc0c22013-01-29 01:02:03 +0000610 return get(C, Attrs);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000611}
612
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000613AttributeSet AttributeSet::get(LLVMContext &C, unsigned Index,
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000614 ArrayRef<Attribute::AttrKind> Kind) {
615 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
616 for (ArrayRef<Attribute::AttrKind>::iterator I = Kind.begin(),
617 E = Kind.end(); I != E; ++I)
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000618 Attrs.push_back(std::make_pair(Index, Attribute::get(C, *I)));
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000619 return get(C, Attrs);
620}
621
622AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<AttributeSet> Attrs) {
623 if (Attrs.empty()) return AttributeSet();
624
625 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrNodeVec;
626 for (unsigned I = 0, E = Attrs.size(); I != E; ++I) {
Benjamin Kramere22cde02013-07-11 12:13:16 +0000627 AttributeSetImpl *AS = Attrs[I].pImpl;
628 if (!AS) continue;
629 AttrNodeVec.append(AS->getNode(0), AS->getNode(AS->getNumAttributes()));
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000630 }
631
632 return getImpl(C, AttrNodeVec);
633}
634
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000635AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000636 Attribute::AttrKind Attr) const {
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000637 if (hasAttribute(Index, Attr)) return *this;
638 return addAttributes(C, Index, AttributeSet::get(C, Index, Attr));
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000639}
640
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000641AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
Reed Kotler9106f732013-03-13 20:20:08 +0000642 StringRef Kind) const {
643 llvm::AttrBuilder B;
644 B.addAttribute(Kind);
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000645 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
Reed Kotler9106f732013-03-13 20:20:08 +0000646}
647
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000648AttributeSet AttributeSet::addAttributes(LLVMContext &C, unsigned Index,
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000649 AttributeSet Attrs) const {
650 if (!pImpl) return Attrs;
651 if (!Attrs.pImpl) return *this;
652
653#ifndef NDEBUG
654 // FIXME it is not obvious how this should work for alignment. For now, say
655 // we can't change a known alignment.
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000656 unsigned OldAlign = getParamAlignment(Index);
657 unsigned NewAlign = Attrs.getParamAlignment(Index);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000658 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
659 "Attempt to change alignment!");
660#endif
661
662 // Add the attribute slots before the one we're trying to add.
663 SmallVector<AttributeSet, 4> AttrSet;
664 uint64_t NumAttrs = pImpl->getNumAttributes();
665 AttributeSet AS;
666 uint64_t LastIndex = 0;
667 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000668 if (getSlotIndex(I) >= Index) {
669 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000670 break;
671 }
672 LastIndex = I + 1;
673 AttrSet.push_back(getSlotAttributes(I));
674 }
675
676 // Now add the attribute into the correct slot. There may already be an
677 // AttributeSet there.
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000678 AttrBuilder B(AS, Index);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000679
680 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000681 if (Attrs.getSlotIndex(I) == Index) {
Benjamin Kramere22cde02013-07-11 12:13:16 +0000682 for (AttributeSetImpl::iterator II = Attrs.pImpl->begin(I),
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000683 IE = Attrs.pImpl->end(I); II != IE; ++II)
Bill Wendling39da0782013-01-31 23:38:01 +0000684 B.addAttribute(*II);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000685 break;
686 }
687
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000688 AttrSet.push_back(AttributeSet::get(C, Index, B));
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000689
690 // Add the remaining attribute slots.
691 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
692 AttrSet.push_back(getSlotAttributes(I));
693
694 return get(C, AttrSet);
695}
696
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000697AttributeSet AttributeSet::removeAttribute(LLVMContext &C, unsigned Index,
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000698 Attribute::AttrKind Attr) const {
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000699 if (!hasAttribute(Index, Attr)) return *this;
700 return removeAttributes(C, Index, AttributeSet::get(C, Index, Attr));
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000701}
702
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000703AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Index,
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000704 AttributeSet Attrs) const {
705 if (!pImpl) return AttributeSet();
706 if (!Attrs.pImpl) return *this;
707
708#ifndef NDEBUG
709 // FIXME it is not obvious how this should work for alignment.
710 // For now, say we can't pass in alignment, which no current use does.
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000711 assert(!Attrs.hasAttribute(Index, Attribute::Alignment) &&
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000712 "Attempt to change alignment!");
713#endif
714
715 // Add the attribute slots before the one we're trying to add.
716 SmallVector<AttributeSet, 4> AttrSet;
717 uint64_t NumAttrs = pImpl->getNumAttributes();
718 AttributeSet AS;
719 uint64_t LastIndex = 0;
720 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000721 if (getSlotIndex(I) >= Index) {
722 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000723 break;
724 }
725 LastIndex = I + 1;
726 AttrSet.push_back(getSlotAttributes(I));
727 }
728
Bill Wendlinge7436542013-01-30 23:07:40 +0000729 // Now remove the attribute from the correct slot. There may already be an
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000730 // AttributeSet there.
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000731 AttrBuilder B(AS, Index);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000732
733 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000734 if (Attrs.getSlotIndex(I) == Index) {
735 B.removeAttributes(Attrs.pImpl->getSlotAttributes(I), Index);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000736 break;
737 }
738
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000739 AttrSet.push_back(AttributeSet::get(C, Index, B));
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000740
741 // Add the remaining attribute slots.
742 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
743 AttrSet.push_back(getSlotAttributes(I));
744
745 return get(C, AttrSet);
746}
747
748//===----------------------------------------------------------------------===//
749// AttributeSet Accessor Methods
750//===----------------------------------------------------------------------===//
751
Bill Wendling85b3fbe2013-02-10 05:00:40 +0000752LLVMContext &AttributeSet::getContext() const {
753 return pImpl->getContext();
754}
755
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000756AttributeSet AttributeSet::getParamAttributes(unsigned Index) const {
757 return pImpl && hasAttributes(Index) ?
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000758 AttributeSet::get(pImpl->getContext(),
Bill Wendling606c8e32013-01-29 03:20:31 +0000759 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000760 std::make_pair(Index, getAttributes(Index)))) :
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000761 AttributeSet();
762}
763
764AttributeSet AttributeSet::getRetAttributes() const {
765 return pImpl && hasAttributes(ReturnIndex) ?
766 AttributeSet::get(pImpl->getContext(),
Bill Wendling606c8e32013-01-29 03:20:31 +0000767 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000768 std::make_pair(ReturnIndex,
769 getAttributes(ReturnIndex)))) :
770 AttributeSet();
771}
772
773AttributeSet AttributeSet::getFnAttributes() const {
774 return pImpl && hasAttributes(FunctionIndex) ?
775 AttributeSet::get(pImpl->getContext(),
Bill Wendling606c8e32013-01-29 03:20:31 +0000776 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000777 std::make_pair(FunctionIndex,
778 getAttributes(FunctionIndex)))) :
779 AttributeSet();
780}
781
782bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{
Bill Wendling606c8e32013-01-29 03:20:31 +0000783 AttributeSetNode *ASN = getAttributes(Index);
784 return ASN ? ASN->hasAttribute(Kind) : false;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000785}
786
Bill Wendling0e9d5d02013-02-13 08:42:21 +0000787bool AttributeSet::hasAttribute(unsigned Index, StringRef Kind) const {
788 AttributeSetNode *ASN = getAttributes(Index);
789 return ASN ? ASN->hasAttribute(Kind) : false;
790}
791
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000792bool AttributeSet::hasAttributes(unsigned Index) const {
Bill Wendling606c8e32013-01-29 03:20:31 +0000793 AttributeSetNode *ASN = getAttributes(Index);
794 return ASN ? ASN->hasAttributes() : false;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000795}
796
797/// \brief Return true if the specified attribute is set for at least one
798/// parameter or for the return value.
799bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr) const {
800 if (pImpl == 0) return false;
801
802 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
Benjamin Kramere22cde02013-07-11 12:13:16 +0000803 for (AttributeSetImpl::iterator II = pImpl->begin(I),
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000804 IE = pImpl->end(I); II != IE; ++II)
NAKAMURA Takumieddab152013-01-31 03:47:28 +0000805 if (II->hasAttribute(Attr))
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000806 return true;
807
808 return false;
809}
810
Bill Wendling0e9d5d02013-02-13 08:42:21 +0000811Attribute AttributeSet::getAttribute(unsigned Index,
812 Attribute::AttrKind Kind) const {
813 AttributeSetNode *ASN = getAttributes(Index);
814 return ASN ? ASN->getAttribute(Kind) : Attribute();
815}
816
817Attribute AttributeSet::getAttribute(unsigned Index,
818 StringRef Kind) const {
819 AttributeSetNode *ASN = getAttributes(Index);
820 return ASN ? ASN->getAttribute(Kind) : Attribute();
821}
822
Bill Wendling606c8e32013-01-29 03:20:31 +0000823unsigned AttributeSet::getParamAlignment(unsigned Index) const {
824 AttributeSetNode *ASN = getAttributes(Index);
825 return ASN ? ASN->getAlignment() : 0;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000826}
827
828unsigned AttributeSet::getStackAlignment(unsigned Index) const {
Bill Wendling606c8e32013-01-29 03:20:31 +0000829 AttributeSetNode *ASN = getAttributes(Index);
830 return ASN ? ASN->getStackAlignment() : 0;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000831}
832
Rafael Espindolaaae02982013-05-01 13:07:03 +0000833std::string AttributeSet::getAsString(unsigned Index,
Bill Wendlingb29ce262013-02-11 08:43:33 +0000834 bool InAttrGrp) const {
Bill Wendling606c8e32013-01-29 03:20:31 +0000835 AttributeSetNode *ASN = getAttributes(Index);
Rafael Espindolaaae02982013-05-01 13:07:03 +0000836 return ASN ? ASN->getAsString(InAttrGrp) : std::string("");
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000837}
838
839/// \brief The attributes for the specified index are returned.
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000840AttributeSetNode *AttributeSet::getAttributes(unsigned Index) const {
Bill Wendling606c8e32013-01-29 03:20:31 +0000841 if (!pImpl) return 0;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000842
Bill Wendling606c8e32013-01-29 03:20:31 +0000843 // Loop through to find the attribute node we want.
844 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000845 if (pImpl->getSlotIndex(I) == Index)
Bill Wendling606c8e32013-01-29 03:20:31 +0000846 return pImpl->getSlotNode(I);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000847
Bill Wendling606c8e32013-01-29 03:20:31 +0000848 return 0;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000849}
850
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000851AttributeSet::iterator AttributeSet::begin(unsigned Slot) const {
Bill Wendling16c4b3c2013-01-31 23:53:05 +0000852 if (!pImpl)
853 return ArrayRef<Attribute>().begin();
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000854 return pImpl->begin(Slot);
Bill Wendling16c4b3c2013-01-31 23:53:05 +0000855}
856
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000857AttributeSet::iterator AttributeSet::end(unsigned Slot) const {
Bill Wendling16c4b3c2013-01-31 23:53:05 +0000858 if (!pImpl)
859 return ArrayRef<Attribute>().end();
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000860 return pImpl->end(Slot);
Bill Wendling16c4b3c2013-01-31 23:53:05 +0000861}
862
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000863//===----------------------------------------------------------------------===//
864// AttributeSet Introspection Methods
865//===----------------------------------------------------------------------===//
866
867/// \brief Return the number of slots used in this attribute list. This is the
868/// number of arguments that have an attribute set on them (including the
869/// function itself).
870unsigned AttributeSet::getNumSlots() const {
871 return pImpl ? pImpl->getNumAttributes() : 0;
872}
873
Rafael Espindola76f103e2013-04-30 16:53:38 +0000874unsigned AttributeSet::getSlotIndex(unsigned Slot) const {
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000875 assert(pImpl && Slot < pImpl->getNumAttributes() &&
876 "Slot # out of range!");
877 return pImpl->getSlotIndex(Slot);
878}
879
880AttributeSet AttributeSet::getSlotAttributes(unsigned Slot) const {
881 assert(pImpl && Slot < pImpl->getNumAttributes() &&
882 "Slot # out of range!");
883 return pImpl->getSlotAttributes(Slot);
884}
885
886uint64_t AttributeSet::Raw(unsigned Index) const {
887 // FIXME: Remove this.
888 return pImpl ? pImpl->Raw(Index) : 0;
889}
890
891void AttributeSet::dump() const {
892 dbgs() << "PAL[\n";
893
894 for (unsigned i = 0, e = getNumSlots(); i < e; ++i) {
895 uint64_t Index = getSlotIndex(i);
896 dbgs() << " { ";
897 if (Index == ~0U)
898 dbgs() << "~0U";
899 else
900 dbgs() << Index;
901 dbgs() << " => " << getAsString(Index) << " }\n";
902 }
903
904 dbgs() << "]\n";
905}
906
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000907//===----------------------------------------------------------------------===//
Bill Wendling03198882013-01-04 23:27:34 +0000908// AttrBuilder Method Implementations
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000909//===----------------------------------------------------------------------===//
910
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000911AttrBuilder::AttrBuilder(AttributeSet AS, unsigned Index)
Benjamin Kramerc835b8c2013-02-16 19:13:18 +0000912 : Attrs(0), Alignment(0), StackAlignment(0) {
Bill Wendlingec258982013-01-27 21:23:46 +0000913 AttributeSetImpl *pImpl = AS.pImpl;
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000914 if (!pImpl) return;
915
Bill Wendling73bc4522013-01-28 00:21:34 +0000916 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I) {
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000917 if (pImpl->getSlotIndex(I) != Index) continue;
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000918
Benjamin Kramere22cde02013-07-11 12:13:16 +0000919 for (AttributeSetImpl::iterator II = pImpl->begin(I),
Bill Wendling73bc4522013-01-28 00:21:34 +0000920 IE = pImpl->end(I); II != IE; ++II)
Bill Wendling39da0782013-01-31 23:38:01 +0000921 addAttribute(*II);
Bill Wendling73bc4522013-01-28 00:21:34 +0000922
923 break;
924 }
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000925}
926
Bill Wendling03198882013-01-04 23:27:34 +0000927void AttrBuilder::clear() {
Benjamin Kramer3f213e72013-02-18 12:09:51 +0000928 Attrs.reset();
Bill Wendling03198882013-01-04 23:27:34 +0000929 Alignment = StackAlignment = 0;
930}
931
932AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
Benjamin Kramer3f213e72013-02-18 12:09:51 +0000933 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
Bill Wendling169d5272013-01-31 23:16:25 +0000934 assert(Val != Attribute::Alignment && Val != Attribute::StackAlignment &&
935 "Adding alignment attribute without adding alignment value!");
Benjamin Kramer3f213e72013-02-18 12:09:51 +0000936 Attrs[Val] = true;
Bill Wendling3a106e62012-10-09 19:01:18 +0000937 return *this;
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000938}
939
Bill Wendling39da0782013-01-31 23:38:01 +0000940AttrBuilder &AttrBuilder::addAttribute(Attribute Attr) {
Bill Wendling09ed9102013-02-10 10:13:23 +0000941 if (Attr.isStringAttribute()) {
942 addAttribute(Attr.getKindAsString(), Attr.getValueAsString());
943 return *this;
944 }
945
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000946 Attribute::AttrKind Kind = Attr.getKindAsEnum();
Benjamin Kramer3f213e72013-02-18 12:09:51 +0000947 Attrs[Kind] = true;
Bill Wendling49f60602013-01-28 05:23:28 +0000948
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000949 if (Kind == Attribute::Alignment)
Bill Wendling49f60602013-01-28 05:23:28 +0000950 Alignment = Attr.getAlignment();
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000951 else if (Kind == Attribute::StackAlignment)
Bill Wendling49f60602013-01-28 05:23:28 +0000952 StackAlignment = Attr.getStackAlignment();
953 return *this;
954}
955
Bill Wendlingea59f892013-02-05 08:09:32 +0000956AttrBuilder &AttrBuilder::addAttribute(StringRef A, StringRef V) {
957 TargetDepAttrs[A] = V;
958 return *this;
959}
960
Bill Wendling39da0782013-01-31 23:38:01 +0000961AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
Benjamin Kramer3f213e72013-02-18 12:09:51 +0000962 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
963 Attrs[Val] = false;
Bill Wendling39da0782013-01-31 23:38:01 +0000964
965 if (Val == Attribute::Alignment)
966 Alignment = 0;
967 else if (Val == Attribute::StackAlignment)
968 StackAlignment = 0;
969
970 return *this;
971}
972
Bill Wendlinge7436542013-01-30 23:07:40 +0000973AttrBuilder &AttrBuilder::removeAttributes(AttributeSet A, uint64_t Index) {
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000974 unsigned Slot = ~0U;
Bill Wendling30d2c762013-02-01 00:13:50 +0000975 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
976 if (A.getSlotIndex(I) == Index) {
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000977 Slot = I;
Bill Wendling30d2c762013-02-01 00:13:50 +0000978 break;
Bill Wendling49f60602013-01-28 05:23:28 +0000979 }
Bill Wendling30d2c762013-02-01 00:13:50 +0000980
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000981 assert(Slot != ~0U && "Couldn't find index in AttributeSet!");
Bill Wendling30d2c762013-02-01 00:13:50 +0000982
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000983 for (AttributeSet::iterator I = A.begin(Slot), E = A.end(Slot); I != E; ++I) {
Bill Wendling74fe8252013-02-12 07:56:49 +0000984 Attribute Attr = *I;
985 if (Attr.isEnumAttribute() || Attr.isAlignAttribute()) {
986 Attribute::AttrKind Kind = I->getKindAsEnum();
Benjamin Kramer3f213e72013-02-18 12:09:51 +0000987 Attrs[Kind] = false;
Bill Wendling30d2c762013-02-01 00:13:50 +0000988
Bill Wendling74fe8252013-02-12 07:56:49 +0000989 if (Kind == Attribute::Alignment)
990 Alignment = 0;
991 else if (Kind == Attribute::StackAlignment)
992 StackAlignment = 0;
993 } else {
994 assert(Attr.isStringAttribute() && "Invalid attribute type!");
995 std::map<std::string, std::string>::iterator
996 Iter = TargetDepAttrs.find(Attr.getKindAsString());
997 if (Iter != TargetDepAttrs.end())
998 TargetDepAttrs.erase(Iter);
999 }
Bill Wendling49f60602013-01-28 05:23:28 +00001000 }
1001
1002 return *this;
1003}
1004
Bill Wendlingea59f892013-02-05 08:09:32 +00001005AttrBuilder &AttrBuilder::removeAttribute(StringRef A) {
1006 std::map<std::string, std::string>::iterator I = TargetDepAttrs.find(A);
1007 if (I != TargetDepAttrs.end())
1008 TargetDepAttrs.erase(I);
1009 return *this;
1010}
1011
Bill Wendling702cc912012-10-15 20:35:56 +00001012AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
Bill Wendlingda3f9d82012-10-14 03:58:29 +00001013 if (Align == 0) return *this;
Bill Wendling03198882013-01-04 23:27:34 +00001014
Bill Wendlinge66f3d32012-10-05 06:44:41 +00001015 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1016 assert(Align <= 0x40000000 && "Alignment too large.");
Bill Wendling03198882013-01-04 23:27:34 +00001017
Benjamin Kramer3f213e72013-02-18 12:09:51 +00001018 Attrs[Attribute::Alignment] = true;
Bill Wendling03198882013-01-04 23:27:34 +00001019 Alignment = Align;
Bill Wendlingda3f9d82012-10-14 03:58:29 +00001020 return *this;
Chris Lattner50ee9dd2008-01-02 23:42:30 +00001021}
1022
Bill Wendling03198882013-01-04 23:27:34 +00001023AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
1024 // Default alignment, allow the target to define how to align it.
1025 if (Align == 0) return *this;
1026
1027 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1028 assert(Align <= 0x100 && "Alignment too large.");
1029
Benjamin Kramer3f213e72013-02-18 12:09:51 +00001030 Attrs[Attribute::StackAlignment] = true;
Bill Wendling03198882013-01-04 23:27:34 +00001031 StackAlignment = Align;
1032 return *this;
1033}
1034
Bill Wendling85df6b42013-02-06 01:16:00 +00001035AttrBuilder &AttrBuilder::merge(const AttrBuilder &B) {
1036 // FIXME: What if both have alignments, but they don't match?!
1037 if (!Alignment)
1038 Alignment = B.Alignment;
1039
1040 if (!StackAlignment)
1041 StackAlignment = B.StackAlignment;
1042
Benjamin Kramerc835b8c2013-02-16 19:13:18 +00001043 Attrs |= B.Attrs;
Bill Wendling85df6b42013-02-06 01:16:00 +00001044
1045 for (td_const_iterator I = B.TargetDepAttrs.begin(),
1046 E = B.TargetDepAttrs.end(); I != E; ++I)
1047 TargetDepAttrs[I->first] = I->second;
1048
1049 return *this;
1050}
1051
Bill Wendlingc342d9d2013-02-06 01:33:42 +00001052bool AttrBuilder::contains(StringRef A) const {
1053 return TargetDepAttrs.find(A) != TargetDepAttrs.end();
1054}
1055
Bill Wendling702cc912012-10-15 20:35:56 +00001056bool AttrBuilder::hasAttributes() const {
Benjamin Kramer3f213e72013-02-18 12:09:51 +00001057 return !Attrs.none() || !TargetDepAttrs.empty();
Bill Wendlingf385f4c2012-10-08 23:27:46 +00001058}
Bill Wendling60507d52013-01-04 20:54:35 +00001059
Bill Wendlinge7436542013-01-30 23:07:40 +00001060bool AttrBuilder::hasAttributes(AttributeSet A, uint64_t Index) const {
Bill Wendling8a6a7bb2013-04-18 20:17:28 +00001061 unsigned Slot = ~0U;
Bill Wendlingbdcbccc2013-02-02 00:42:06 +00001062 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
1063 if (A.getSlotIndex(I) == Index) {
Bill Wendling8a6a7bb2013-04-18 20:17:28 +00001064 Slot = I;
Bill Wendlingbdcbccc2013-02-02 00:42:06 +00001065 break;
1066 }
1067
Bill Wendling8a6a7bb2013-04-18 20:17:28 +00001068 assert(Slot != ~0U && "Couldn't find the index!");
Bill Wendlingbdcbccc2013-02-02 00:42:06 +00001069
Bill Wendling8a6a7bb2013-04-18 20:17:28 +00001070 for (AttributeSet::iterator I = A.begin(Slot), E = A.end(Slot);
Bill Wendling74fe8252013-02-12 07:56:49 +00001071 I != E; ++I) {
1072 Attribute Attr = *I;
1073 if (Attr.isEnumAttribute() || Attr.isAlignAttribute()) {
Benjamin Kramer3f213e72013-02-18 12:09:51 +00001074 if (Attrs[I->getKindAsEnum()])
Bill Wendling74fe8252013-02-12 07:56:49 +00001075 return true;
1076 } else {
1077 assert(Attr.isStringAttribute() && "Invalid attribute kind!");
1078 return TargetDepAttrs.find(Attr.getKindAsString())!=TargetDepAttrs.end();
1079 }
1080 }
Bill Wendlingbdcbccc2013-02-02 00:42:06 +00001081
1082 return false;
Bill Wendling8831c062012-10-09 00:01:21 +00001083}
Bill Wendling60507d52013-01-04 20:54:35 +00001084
Bill Wendling702cc912012-10-15 20:35:56 +00001085bool AttrBuilder::hasAlignmentAttr() const {
Bill Wendling03198882013-01-04 23:27:34 +00001086 return Alignment != 0;
Bill Wendlingf385f4c2012-10-08 23:27:46 +00001087}
1088
Bill Wendlingc22f4aa2013-01-29 00:34:06 +00001089bool AttrBuilder::operator==(const AttrBuilder &B) {
Benjamin Kramerc835b8c2013-02-16 19:13:18 +00001090 if (Attrs != B.Attrs)
1091 return false;
Bill Wendlingc342d9d2013-02-06 01:33:42 +00001092
1093 for (td_const_iterator I = TargetDepAttrs.begin(),
1094 E = TargetDepAttrs.end(); I != E; ++I)
1095 if (B.TargetDepAttrs.find(I->first) == B.TargetDepAttrs.end())
1096 return false;
1097
1098 return Alignment == B.Alignment && StackAlignment == B.StackAlignment;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +00001099}
1100
1101AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) {
Bill Wendlingf9271ea2013-02-04 23:32:23 +00001102 // FIXME: Remove this in 4.0.
Bill Wendling8232ece2013-01-29 01:43:29 +00001103 if (!Val) return *this;
1104
Bill Wendlingc22f4aa2013-01-29 00:34:06 +00001105 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
1106 I = Attribute::AttrKind(I + 1)) {
1107 if (uint64_t A = (Val & AttributeImpl::getAttrMask(I))) {
Benjamin Kramer3f213e72013-02-18 12:09:51 +00001108 Attrs[I] = true;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +00001109
1110 if (I == Attribute::Alignment)
1111 Alignment = 1ULL << ((A >> 16) - 1);
1112 else if (I == Attribute::StackAlignment)
1113 StackAlignment = 1ULL << ((A >> 26)-1);
1114 }
1115 }
1116
1117 return *this;
1118}
1119
Bill Wendling8e47daf2013-01-25 23:09:36 +00001120//===----------------------------------------------------------------------===//
1121// AttributeFuncs Function Defintions
1122//===----------------------------------------------------------------------===//
1123
Bill Wendling7beee282013-02-01 01:04:27 +00001124/// \brief Which attributes cannot be applied to a type.
Bill Wendlinge7436542013-01-30 23:07:40 +00001125AttributeSet AttributeFuncs::typeIncompatible(Type *Ty, uint64_t Index) {
Bill Wendling8e47daf2013-01-25 23:09:36 +00001126 AttrBuilder Incompatible;
1127
1128 if (!Ty->isIntegerTy())
1129 // Attribute that only apply to integers.
1130 Incompatible.addAttribute(Attribute::SExt)
1131 .addAttribute(Attribute::ZExt);
1132
1133 if (!Ty->isPointerTy())
1134 // Attribute that only apply to pointers.
1135 Incompatible.addAttribute(Attribute::ByVal)
1136 .addAttribute(Attribute::Nest)
1137 .addAttribute(Attribute::NoAlias)
1138 .addAttribute(Attribute::NoCapture)
Nick Lewyckydc897372013-07-06 00:29:58 +00001139 .addAttribute(Attribute::ReadNone)
1140 .addAttribute(Attribute::ReadOnly)
Bill Wendling8e47daf2013-01-25 23:09:36 +00001141 .addAttribute(Attribute::StructRet);
1142
Bill Wendlinge7436542013-01-30 23:07:40 +00001143 return AttributeSet::get(Ty->getContext(), Index, Incompatible);
Bill Wendling8e47daf2013-01-25 23:09:36 +00001144}