blob: c8e2f6be5c0e4803c3e5c2d6cf3f42314236d999 [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 {
107 assert((isEnumAttribute() || isAlignAttribute()) &&
108 "Invalid attribute type to get the kind as an enum!");
109 return pImpl ? pImpl->getKindAsEnum() : None;
110}
111
112uint64_t Attribute::getValueAsInt() const {
113 assert(isAlignAttribute() &&
114 "Expected the attribute to be an alignment attribute!");
115 return pImpl ? pImpl->getValueAsInt() : 0;
116}
117
118StringRef Attribute::getKindAsString() const {
119 assert(isStringAttribute() &&
120 "Invalid attribute type to get the kind as a string!");
121 return pImpl ? pImpl->getKindAsString() : StringRef();
122}
123
124StringRef Attribute::getValueAsString() const {
125 assert(isStringAttribute() &&
126 "Invalid attribute type to get the value as a string!");
127 return pImpl ? pImpl->getValueAsString() : StringRef();
128}
129
Bill Wendling64754f42013-02-05 23:48:36 +0000130bool Attribute::hasAttribute(AttrKind Kind) const {
131 return (pImpl && pImpl->hasAttribute(Kind)) || (!pImpl && Kind == None);
132}
133
134bool Attribute::hasAttribute(StringRef Kind) const {
135 if (!isStringAttribute()) return false;
136 return pImpl && pImpl->hasAttribute(Kind);
Bill Wendling6dc37812013-01-29 20:45:34 +0000137}
138
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000139/// This returns the alignment field of an attribute as a byte alignment value.
Bill Wendling034b94b2012-12-19 07:18:57 +0000140unsigned Attribute::getAlignment() const {
Bill Wendling7beee282013-02-01 01:04:27 +0000141 assert(hasAttribute(Attribute::Alignment) &&
142 "Trying to get alignment from non-alignment attribute!");
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000143 return pImpl->getValueAsInt();
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000144}
145
146/// This returns the stack alignment field of an attribute as a byte alignment
147/// value.
Bill Wendling034b94b2012-12-19 07:18:57 +0000148unsigned Attribute::getStackAlignment() const {
Bill Wendling7beee282013-02-01 01:04:27 +0000149 assert(hasAttribute(Attribute::StackAlignment) &&
150 "Trying to get alignment from non-alignment attribute!");
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000151 return pImpl->getValueAsInt();
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000152}
153
Bill Wendlingb29ce262013-02-11 08:43:33 +0000154std::string Attribute::getAsString(bool InAttrGrp) const {
Bill Wendling14292a62013-01-31 20:59:05 +0000155 if (!pImpl) return "";
156
Kostya Serebryany8eec41f2013-02-26 06:58:09 +0000157 if (hasAttribute(Attribute::SanitizeAddress))
158 return "sanitize_address";
Bill Wendling14292a62013-01-31 20:59:05 +0000159 if (hasAttribute(Attribute::AlwaysInline))
160 return "alwaysinline";
Michael Gottesman2253a2f2013-06-27 00:25:01 +0000161 if (hasAttribute(Attribute::Builtin))
162 return "builtin";
Bill Wendling14292a62013-01-31 20:59:05 +0000163 if (hasAttribute(Attribute::ByVal))
164 return "byval";
165 if (hasAttribute(Attribute::InlineHint))
166 return "inlinehint";
Bill Wendling034b94b2012-12-19 07:18:57 +0000167 if (hasAttribute(Attribute::InReg))
Bill Wendling606c8e32013-01-29 03:20:31 +0000168 return "inreg";
Bill Wendling14292a62013-01-31 20:59:05 +0000169 if (hasAttribute(Attribute::MinSize))
170 return "minsize";
171 if (hasAttribute(Attribute::Naked))
172 return "naked";
173 if (hasAttribute(Attribute::Nest))
174 return "nest";
Bill Wendling034b94b2012-12-19 07:18:57 +0000175 if (hasAttribute(Attribute::NoAlias))
Bill Wendling606c8e32013-01-29 03:20:31 +0000176 return "noalias";
Bill Wendling143d4642013-02-22 00:12:35 +0000177 if (hasAttribute(Attribute::NoBuiltin))
178 return "nobuiltin";
Bill Wendling034b94b2012-12-19 07:18:57 +0000179 if (hasAttribute(Attribute::NoCapture))
Bill Wendling606c8e32013-01-29 03:20:31 +0000180 return "nocapture";
Bill Wendling14292a62013-01-31 20:59:05 +0000181 if (hasAttribute(Attribute::NoDuplicate))
182 return "noduplicate";
183 if (hasAttribute(Attribute::NoImplicitFloat))
184 return "noimplicitfloat";
185 if (hasAttribute(Attribute::NoInline))
186 return "noinline";
187 if (hasAttribute(Attribute::NonLazyBind))
188 return "nonlazybind";
189 if (hasAttribute(Attribute::NoRedZone))
190 return "noredzone";
191 if (hasAttribute(Attribute::NoReturn))
192 return "noreturn";
193 if (hasAttribute(Attribute::NoUnwind))
194 return "nounwind";
195 if (hasAttribute(Attribute::OptimizeForSize))
196 return "optsize";
Bill Wendling034b94b2012-12-19 07:18:57 +0000197 if (hasAttribute(Attribute::ReadNone))
Bill Wendling606c8e32013-01-29 03:20:31 +0000198 return "readnone";
Bill Wendling034b94b2012-12-19 07:18:57 +0000199 if (hasAttribute(Attribute::ReadOnly))
Bill Wendling606c8e32013-01-29 03:20:31 +0000200 return "readonly";
Stephen Lin456ca042013-04-20 05:14:40 +0000201 if (hasAttribute(Attribute::Returned))
202 return "returned";
Bill Wendling14292a62013-01-31 20:59:05 +0000203 if (hasAttribute(Attribute::ReturnsTwice))
204 return "returns_twice";
205 if (hasAttribute(Attribute::SExt))
206 return "signext";
Bill Wendling034b94b2012-12-19 07:18:57 +0000207 if (hasAttribute(Attribute::StackProtect))
Bill Wendling606c8e32013-01-29 03:20:31 +0000208 return "ssp";
Bill Wendling034b94b2012-12-19 07:18:57 +0000209 if (hasAttribute(Attribute::StackProtectReq))
Bill Wendling606c8e32013-01-29 03:20:31 +0000210 return "sspreq";
Bill Wendling114baee2013-01-23 06:41:41 +0000211 if (hasAttribute(Attribute::StackProtectStrong))
Bill Wendling606c8e32013-01-29 03:20:31 +0000212 return "sspstrong";
Bill Wendling14292a62013-01-31 20:59:05 +0000213 if (hasAttribute(Attribute::StructRet))
214 return "sret";
Kostya Serebryany8eec41f2013-02-26 06:58:09 +0000215 if (hasAttribute(Attribute::SanitizeThread))
216 return "sanitize_thread";
217 if (hasAttribute(Attribute::SanitizeMemory))
218 return "sanitize_memory";
Bill Wendling14292a62013-01-31 20:59:05 +0000219 if (hasAttribute(Attribute::UWTable))
220 return "uwtable";
221 if (hasAttribute(Attribute::ZExt))
222 return "zeroext";
Diego Novillo77226a02013-05-24 12:26:52 +0000223 if (hasAttribute(Attribute::Cold))
224 return "cold";
Bill Wendling14292a62013-01-31 20:59:05 +0000225
226 // FIXME: These should be output like this:
227 //
228 // align=4
229 // alignstack=8
230 //
Bill Wendling034b94b2012-12-19 07:18:57 +0000231 if (hasAttribute(Attribute::Alignment)) {
Bill Wendling606c8e32013-01-29 03:20:31 +0000232 std::string Result;
Bill Wendlingb29ce262013-02-11 08:43:33 +0000233 Result += "align";
234 Result += (InAttrGrp) ? "=" : " ";
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000235 Result += utostr(getValueAsInt());
236 return Result;
237 }
Bill Wendlingb29ce262013-02-11 08:43:33 +0000238
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000239 if (hasAttribute(Attribute::StackAlignment)) {
240 std::string Result;
Bill Wendlingb29ce262013-02-11 08:43:33 +0000241 Result += "alignstack";
242 if (InAttrGrp) {
243 Result += "=";
244 Result += utostr(getValueAsInt());
245 } else {
246 Result += "(";
247 Result += utostr(getValueAsInt());
248 Result += ")";
249 }
Bill Wendling606c8e32013-01-29 03:20:31 +0000250 return Result;
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000251 }
Bill Wendling14292a62013-01-31 20:59:05 +0000252
253 // Convert target-dependent attributes to strings of the form:
254 //
255 // "kind"
256 // "kind" = "value"
Bill Wendling14292a62013-01-31 20:59:05 +0000257 //
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000258 if (isStringAttribute()) {
Bill Wendling14292a62013-01-31 20:59:05 +0000259 std::string Result;
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000260 Result += '\"' + getKindAsString().str() + '"';
Bill Wendling14292a62013-01-31 20:59:05 +0000261
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000262 StringRef Val = pImpl->getValueAsString();
263 if (Val.empty()) return Result;
Bill Wendling5a4041e2013-02-01 22:32:30 +0000264
Bill Wendlingb29ce262013-02-11 08:43:33 +0000265 Result += "=\"" + Val.str() + '"';
Bill Wendling7beee282013-02-01 01:04:27 +0000266 return Result;
Bill Wendling14292a62013-01-31 20:59:05 +0000267 }
Bill Wendling606c8e32013-01-29 03:20:31 +0000268
269 llvm_unreachable("Unknown attribute");
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000270}
271
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000272bool Attribute::operator<(Attribute A) const {
273 if (!pImpl && !A.pImpl) return false;
274 if (!pImpl) return true;
275 if (!A.pImpl) return false;
276 return *pImpl < *A.pImpl;
277}
278
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000279//===----------------------------------------------------------------------===//
280// AttributeImpl Definition
281//===----------------------------------------------------------------------===//
282
Benjamin Kramere22cde02013-07-11 12:13:16 +0000283AttributeImpl::~AttributeImpl() {}
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000284
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000285bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000286 if (isStringAttribute()) return false;
287 return getKindAsEnum() == A;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000288}
289
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000290bool AttributeImpl::hasAttribute(StringRef Kind) const {
291 if (!isStringAttribute()) return false;
292 return getKindAsString() == Kind;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000293}
294
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000295Attribute::AttrKind AttributeImpl::getKindAsEnum() const {
Benjamin Kramere22cde02013-07-11 12:13:16 +0000296 assert(isEnumAttribute() || isAlignAttribute());
297 return static_cast<const EnumAttributeImpl *>(this)->getEnumKind();
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000298}
299
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000300uint64_t AttributeImpl::getValueAsInt() const {
Benjamin Kramere22cde02013-07-11 12:13:16 +0000301 assert(isAlignAttribute());
302 return static_cast<const AlignAttributeImpl *>(this)->getAlignment();
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000303}
304
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000305StringRef AttributeImpl::getKindAsString() const {
Benjamin Kramere22cde02013-07-11 12:13:16 +0000306 assert(isStringAttribute());
307 return static_cast<const StringAttributeImpl *>(this)->getStringKind();
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000308}
309
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000310StringRef AttributeImpl::getValueAsString() const {
Benjamin Kramere22cde02013-07-11 12:13:16 +0000311 assert(isStringAttribute());
312 return static_cast<const StringAttributeImpl *>(this)->getStringValue();
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000313}
314
315bool AttributeImpl::operator<(const AttributeImpl &AI) const {
Bill Wendling7beee282013-02-01 01:04:27 +0000316 // This sorts the attributes with Attribute::AttrKinds coming first (sorted
317 // relative to their enum value) and then strings.
Bill Wendling94328f42013-02-15 05:25:26 +0000318 if (isEnumAttribute()) {
319 if (AI.isEnumAttribute()) return getKindAsEnum() < AI.getKindAsEnum();
320 if (AI.isAlignAttribute()) return true;
321 if (AI.isStringAttribute()) return true;
322 }
Bill Wendling7beee282013-02-01 01:04:27 +0000323
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000324 if (isAlignAttribute()) {
Bill Wendling94328f42013-02-15 05:25:26 +0000325 if (AI.isEnumAttribute()) return false;
326 if (AI.isAlignAttribute()) return getValueAsInt() < AI.getValueAsInt();
327 if (AI.isStringAttribute()) return true;
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000328 }
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000329
Bill Wendling94328f42013-02-15 05:25:26 +0000330 if (AI.isEnumAttribute()) return false;
331 if (AI.isAlignAttribute()) return false;
332 if (getKindAsString() == AI.getKindAsString())
333 return getValueAsString() < AI.getValueAsString();
334 return getKindAsString() < AI.getKindAsString();
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000335}
336
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000337uint64_t AttributeImpl::getAttrMask(Attribute::AttrKind Val) {
338 // FIXME: Remove this.
339 switch (Val) {
340 case Attribute::EndAttrKinds:
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000341 llvm_unreachable("Synthetic enumerators which should never get here");
342
343 case Attribute::None: return 0;
344 case Attribute::ZExt: return 1 << 0;
345 case Attribute::SExt: return 1 << 1;
346 case Attribute::NoReturn: return 1 << 2;
347 case Attribute::InReg: return 1 << 3;
348 case Attribute::StructRet: return 1 << 4;
349 case Attribute::NoUnwind: return 1 << 5;
350 case Attribute::NoAlias: return 1 << 6;
351 case Attribute::ByVal: return 1 << 7;
352 case Attribute::Nest: return 1 << 8;
353 case Attribute::ReadNone: return 1 << 9;
354 case Attribute::ReadOnly: return 1 << 10;
355 case Attribute::NoInline: return 1 << 11;
356 case Attribute::AlwaysInline: return 1 << 12;
357 case Attribute::OptimizeForSize: return 1 << 13;
358 case Attribute::StackProtect: return 1 << 14;
359 case Attribute::StackProtectReq: return 1 << 15;
360 case Attribute::Alignment: return 31 << 16;
361 case Attribute::NoCapture: return 1 << 21;
362 case Attribute::NoRedZone: return 1 << 22;
363 case Attribute::NoImplicitFloat: return 1 << 23;
364 case Attribute::Naked: return 1 << 24;
365 case Attribute::InlineHint: return 1 << 25;
366 case Attribute::StackAlignment: return 7 << 26;
367 case Attribute::ReturnsTwice: return 1 << 29;
368 case Attribute::UWTable: return 1 << 30;
369 case Attribute::NonLazyBind: return 1U << 31;
Kostya Serebryany8eec41f2013-02-26 06:58:09 +0000370 case Attribute::SanitizeAddress: return 1ULL << 32;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000371 case Attribute::MinSize: return 1ULL << 33;
372 case Attribute::NoDuplicate: return 1ULL << 34;
373 case Attribute::StackProtectStrong: return 1ULL << 35;
Kostya Serebryany8eec41f2013-02-26 06:58:09 +0000374 case Attribute::SanitizeThread: return 1ULL << 36;
375 case Attribute::SanitizeMemory: return 1ULL << 37;
Bill Wendlingd18e0b92013-02-22 00:40:12 +0000376 case Attribute::NoBuiltin: return 1ULL << 38;
Stephen Lin456ca042013-04-20 05:14:40 +0000377 case Attribute::Returned: return 1ULL << 39;
Diego Novillo77226a02013-05-24 12:26:52 +0000378 case Attribute::Cold: return 1ULL << 40;
Michael Gottesman2253a2f2013-06-27 00:25:01 +0000379 case Attribute::Builtin: return 1ULL << 41;
Bill Wendlingbd2acfa2013-02-22 00:50:09 +0000380 }
381 llvm_unreachable("Unsupported attribute type");
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000382}
383
384//===----------------------------------------------------------------------===//
385// AttributeSetNode Definition
386//===----------------------------------------------------------------------===//
387
388AttributeSetNode *AttributeSetNode::get(LLVMContext &C,
389 ArrayRef<Attribute> Attrs) {
390 if (Attrs.empty())
391 return 0;
392
393 // Otherwise, build a key to look up the existing attributes.
394 LLVMContextImpl *pImpl = C.pImpl;
395 FoldingSetNodeID ID;
396
397 SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end());
Bill Wendlingf107e6c2013-02-13 09:26:26 +0000398 array_pod_sort(SortedAttrs.begin(), SortedAttrs.end());
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000399
400 for (SmallVectorImpl<Attribute>::iterator I = SortedAttrs.begin(),
401 E = SortedAttrs.end(); I != E; ++I)
402 I->Profile(ID);
403
404 void *InsertPoint;
405 AttributeSetNode *PA =
406 pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint);
407
408 // If we didn't find any existing attributes of the same shape then create a
409 // new one and insert it.
410 if (!PA) {
Benjamin Kramere22cde02013-07-11 12:13:16 +0000411 // Coallocate entries after the AttributeSetNode itself.
412 void *Mem = ::operator new(sizeof(AttributeSetNode) +
413 sizeof(Attribute) * SortedAttrs.size());
414 PA = new (Mem) AttributeSetNode(SortedAttrs);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000415 pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint);
416 }
417
418 // Return the AttributesListNode that we found or created.
419 return PA;
420}
421
Bill Wendling606c8e32013-01-29 03:20:31 +0000422bool AttributeSetNode::hasAttribute(Attribute::AttrKind Kind) const {
Benjamin Kramere22cde02013-07-11 12:13:16 +0000423 for (iterator I = begin(), E = end(); I != E; ++I)
NAKAMURA Takumieddab152013-01-31 03:47:28 +0000424 if (I->hasAttribute(Kind))
Bill Wendling606c8e32013-01-29 03:20:31 +0000425 return true;
426 return false;
427}
428
Bill Wendling0e9d5d02013-02-13 08:42:21 +0000429bool AttributeSetNode::hasAttribute(StringRef Kind) const {
Benjamin Kramere22cde02013-07-11 12:13:16 +0000430 for (iterator I = begin(), E = end(); I != E; ++I)
Bill Wendling0e9d5d02013-02-13 08:42:21 +0000431 if (I->hasAttribute(Kind))
432 return true;
433 return false;
434}
435
436Attribute AttributeSetNode::getAttribute(Attribute::AttrKind Kind) const {
Benjamin Kramere22cde02013-07-11 12:13:16 +0000437 for (iterator I = begin(), E = end(); I != E; ++I)
Bill Wendling0e9d5d02013-02-13 08:42:21 +0000438 if (I->hasAttribute(Kind))
439 return *I;
440 return Attribute();
441}
442
443Attribute AttributeSetNode::getAttribute(StringRef Kind) const {
Benjamin Kramere22cde02013-07-11 12:13:16 +0000444 for (iterator I = begin(), E = end(); I != E; ++I)
Bill Wendling0e9d5d02013-02-13 08:42:21 +0000445 if (I->hasAttribute(Kind))
446 return *I;
447 return Attribute();
448}
449
Bill Wendling606c8e32013-01-29 03:20:31 +0000450unsigned AttributeSetNode::getAlignment() const {
Benjamin Kramere22cde02013-07-11 12:13:16 +0000451 for (iterator I = begin(), E = end(); I != E; ++I)
NAKAMURA Takumieddab152013-01-31 03:47:28 +0000452 if (I->hasAttribute(Attribute::Alignment))
Bill Wendling606c8e32013-01-29 03:20:31 +0000453 return I->getAlignment();
454 return 0;
455}
456
457unsigned AttributeSetNode::getStackAlignment() const {
Benjamin Kramere22cde02013-07-11 12:13:16 +0000458 for (iterator I = begin(), E = end(); I != E; ++I)
NAKAMURA Takumieddab152013-01-31 03:47:28 +0000459 if (I->hasAttribute(Attribute::StackAlignment))
Bill Wendling606c8e32013-01-29 03:20:31 +0000460 return I->getStackAlignment();
461 return 0;
462}
463
Rafael Espindolaaae02982013-05-01 13:07:03 +0000464std::string AttributeSetNode::getAsString(bool InAttrGrp) const {
Benjamin Kramere94e4ca2013-04-19 11:43:21 +0000465 std::string Str;
Benjamin Kramere22cde02013-07-11 12:13:16 +0000466 for (iterator I = begin(), E = end(); I != E; ++I) {
467 if (I != begin())
Rafael Espindolaaae02982013-05-01 13:07:03 +0000468 Str += ' ';
469 Str += I->getAsString(InAttrGrp);
Bill Wendling606c8e32013-01-29 03:20:31 +0000470 }
471 return Str;
472}
473
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000474//===----------------------------------------------------------------------===//
475// AttributeSetImpl Definition
476//===----------------------------------------------------------------------===//
477
Rafael Espindola76f103e2013-04-30 16:53:38 +0000478uint64_t AttributeSetImpl::Raw(unsigned Index) const {
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000479 for (unsigned I = 0, E = getNumAttributes(); I != E; ++I) {
480 if (getSlotIndex(I) != Index) continue;
Benjamin Kramere22cde02013-07-11 12:13:16 +0000481 const AttributeSetNode *ASN = getSlotNode(I);
Bill Wendlingfca0ed22013-02-02 00:52:44 +0000482 uint64_t Mask = 0;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000483
Benjamin Kramere22cde02013-07-11 12:13:16 +0000484 for (AttributeSetNode::iterator II = ASN->begin(),
Bill Wendlingfca0ed22013-02-02 00:52:44 +0000485 IE = ASN->end(); II != IE; ++II) {
486 Attribute Attr = *II;
Bill Wendling21536912013-02-10 23:18:05 +0000487
488 // This cannot handle string attributes.
489 if (Attr.isStringAttribute()) continue;
490
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000491 Attribute::AttrKind Kind = Attr.getKindAsEnum();
Bill Wendlingfca0ed22013-02-02 00:52:44 +0000492
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000493 if (Kind == Attribute::Alignment)
Bill Wendlingfca0ed22013-02-02 00:52:44 +0000494 Mask |= (Log2_32(ASN->getAlignment()) + 1) << 16;
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000495 else if (Kind == Attribute::StackAlignment)
Bill Wendlingfca0ed22013-02-02 00:52:44 +0000496 Mask |= (Log2_32(ASN->getStackAlignment()) + 1) << 26;
497 else
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000498 Mask |= AttributeImpl::getAttrMask(Kind);
Bill Wendlingfca0ed22013-02-02 00:52:44 +0000499 }
500
501 return Mask;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000502 }
503
504 return 0;
505}
506
507//===----------------------------------------------------------------------===//
508// AttributeSet Construction and Mutation Methods
509//===----------------------------------------------------------------------===//
510
Bill Wendling8232ece2013-01-29 01:43:29 +0000511AttributeSet
512AttributeSet::getImpl(LLVMContext &C,
513 ArrayRef<std::pair<unsigned, AttributeSetNode*> > Attrs) {
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000514 LLVMContextImpl *pImpl = C.pImpl;
515 FoldingSetNodeID ID;
516 AttributeSetImpl::Profile(ID, Attrs);
517
518 void *InsertPoint;
519 AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
520
521 // If we didn't find any existing attributes of the same shape then
522 // create a new one and insert it.
523 if (!PA) {
Benjamin Kramere22cde02013-07-11 12:13:16 +0000524 // Coallocate entries after the AttributeSetImpl itself.
525 void *Mem = ::operator new(sizeof(AttributeSetImpl) +
526 sizeof(std::pair<unsigned, AttributeSetNode *>) *
527 Attrs.size());
528 PA = new (Mem) AttributeSetImpl(C, Attrs);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000529 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
530 }
531
532 // Return the AttributesList that we found or created.
533 return AttributeSet(PA);
534}
535
536AttributeSet AttributeSet::get(LLVMContext &C,
537 ArrayRef<std::pair<unsigned, Attribute> > Attrs){
538 // If there are no attributes then return a null AttributesList pointer.
539 if (Attrs.empty())
540 return AttributeSet();
541
542#ifndef NDEBUG
543 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
544 assert((!i || Attrs[i-1].first <= Attrs[i].first) &&
545 "Misordered Attributes list!");
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000546 assert(!Attrs[i].second.hasAttribute(Attribute::None) &&
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000547 "Pointless attribute!");
548 }
549#endif
550
551 // Create a vector if (unsigned, AttributeSetNode*) pairs from the attributes
552 // list.
553 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrPairVec;
554 for (ArrayRef<std::pair<unsigned, Attribute> >::iterator I = Attrs.begin(),
555 E = Attrs.end(); I != E; ) {
556 unsigned Index = I->first;
557 SmallVector<Attribute, 4> AttrVec;
NAKAMURA Takumi3ba51ce2013-01-29 15:18:16 +0000558 while (I != E && I->first == Index) {
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000559 AttrVec.push_back(I->second);
560 ++I;
561 }
562
563 AttrPairVec.push_back(std::make_pair(Index,
564 AttributeSetNode::get(C, AttrVec)));
565 }
566
567 return getImpl(C, AttrPairVec);
568}
569
570AttributeSet AttributeSet::get(LLVMContext &C,
571 ArrayRef<std::pair<unsigned,
572 AttributeSetNode*> > Attrs) {
573 // If there are no attributes then return a null AttributesList pointer.
574 if (Attrs.empty())
575 return AttributeSet();
576
577 return getImpl(C, Attrs);
578}
579
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000580AttributeSet AttributeSet::get(LLVMContext &C, unsigned Index, AttrBuilder &B) {
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000581 if (!B.hasAttributes())
582 return AttributeSet();
Bill Wendling8fbc0c22013-01-29 01:02:03 +0000583
Bill Wendling64754f42013-02-05 23:48:36 +0000584 // Add target-independent attributes.
Bill Wendling8fbc0c22013-01-29 01:02:03 +0000585 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
Benjamin Kramerc835b8c2013-02-16 19:13:18 +0000586 for (Attribute::AttrKind Kind = Attribute::None;
Benjamin Kramer7c146122013-02-16 19:22:28 +0000587 Kind != Attribute::EndAttrKinds; Kind = Attribute::AttrKind(Kind + 1)) {
Benjamin Kramerc835b8c2013-02-16 19:13:18 +0000588 if (!B.contains(Kind))
589 continue;
590
Bill Wendling8fbc0c22013-01-29 01:02:03 +0000591 if (Kind == Attribute::Alignment)
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000592 Attrs.push_back(std::make_pair(Index, Attribute::
Bill Wendling8fbc0c22013-01-29 01:02:03 +0000593 getWithAlignment(C, B.getAlignment())));
594 else if (Kind == Attribute::StackAlignment)
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000595 Attrs.push_back(std::make_pair(Index, Attribute::
Bill Wendling8fbc0c22013-01-29 01:02:03 +0000596 getWithStackAlignment(C, B.getStackAlignment())));
597 else
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000598 Attrs.push_back(std::make_pair(Index, Attribute::get(C, Kind)));
Bill Wendling8fbc0c22013-01-29 01:02:03 +0000599 }
600
Bill Wendling64754f42013-02-05 23:48:36 +0000601 // Add target-dependent (string) attributes.
602 for (AttrBuilder::td_iterator I = B.td_begin(), E = B.td_end();
603 I != E; ++I)
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000604 Attrs.push_back(std::make_pair(Index, Attribute::get(C, I->first,I->second)));
Bill Wendling64754f42013-02-05 23:48:36 +0000605
Bill Wendling8fbc0c22013-01-29 01:02:03 +0000606 return get(C, Attrs);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000607}
608
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000609AttributeSet AttributeSet::get(LLVMContext &C, unsigned Index,
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000610 ArrayRef<Attribute::AttrKind> Kind) {
611 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
612 for (ArrayRef<Attribute::AttrKind>::iterator I = Kind.begin(),
613 E = Kind.end(); I != E; ++I)
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000614 Attrs.push_back(std::make_pair(Index, Attribute::get(C, *I)));
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000615 return get(C, Attrs);
616}
617
618AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<AttributeSet> Attrs) {
619 if (Attrs.empty()) return AttributeSet();
620
621 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrNodeVec;
622 for (unsigned I = 0, E = Attrs.size(); I != E; ++I) {
Benjamin Kramere22cde02013-07-11 12:13:16 +0000623 AttributeSetImpl *AS = Attrs[I].pImpl;
624 if (!AS) continue;
625 AttrNodeVec.append(AS->getNode(0), AS->getNode(AS->getNumAttributes()));
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000626 }
627
628 return getImpl(C, AttrNodeVec);
629}
630
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000631AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000632 Attribute::AttrKind Attr) const {
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000633 if (hasAttribute(Index, Attr)) return *this;
634 return addAttributes(C, Index, AttributeSet::get(C, Index, Attr));
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000635}
636
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000637AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
Reed Kotler9106f732013-03-13 20:20:08 +0000638 StringRef Kind) const {
639 llvm::AttrBuilder B;
640 B.addAttribute(Kind);
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000641 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
Reed Kotler9106f732013-03-13 20:20:08 +0000642}
643
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000644AttributeSet AttributeSet::addAttributes(LLVMContext &C, unsigned Index,
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000645 AttributeSet Attrs) const {
646 if (!pImpl) return Attrs;
647 if (!Attrs.pImpl) return *this;
648
649#ifndef NDEBUG
650 // FIXME it is not obvious how this should work for alignment. For now, say
651 // we can't change a known alignment.
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000652 unsigned OldAlign = getParamAlignment(Index);
653 unsigned NewAlign = Attrs.getParamAlignment(Index);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000654 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
655 "Attempt to change alignment!");
656#endif
657
658 // Add the attribute slots before the one we're trying to add.
659 SmallVector<AttributeSet, 4> AttrSet;
660 uint64_t NumAttrs = pImpl->getNumAttributes();
661 AttributeSet AS;
662 uint64_t LastIndex = 0;
663 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000664 if (getSlotIndex(I) >= Index) {
665 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000666 break;
667 }
668 LastIndex = I + 1;
669 AttrSet.push_back(getSlotAttributes(I));
670 }
671
672 // Now add the attribute into the correct slot. There may already be an
673 // AttributeSet there.
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000674 AttrBuilder B(AS, Index);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000675
676 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000677 if (Attrs.getSlotIndex(I) == Index) {
Benjamin Kramere22cde02013-07-11 12:13:16 +0000678 for (AttributeSetImpl::iterator II = Attrs.pImpl->begin(I),
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000679 IE = Attrs.pImpl->end(I); II != IE; ++II)
Bill Wendling39da0782013-01-31 23:38:01 +0000680 B.addAttribute(*II);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000681 break;
682 }
683
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000684 AttrSet.push_back(AttributeSet::get(C, Index, B));
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000685
686 // Add the remaining attribute slots.
687 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
688 AttrSet.push_back(getSlotAttributes(I));
689
690 return get(C, AttrSet);
691}
692
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000693AttributeSet AttributeSet::removeAttribute(LLVMContext &C, unsigned Index,
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000694 Attribute::AttrKind Attr) const {
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000695 if (!hasAttribute(Index, Attr)) return *this;
696 return removeAttributes(C, Index, AttributeSet::get(C, Index, Attr));
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000697}
698
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000699AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Index,
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000700 AttributeSet Attrs) const {
701 if (!pImpl) return AttributeSet();
702 if (!Attrs.pImpl) return *this;
703
704#ifndef NDEBUG
705 // FIXME it is not obvious how this should work for alignment.
706 // For now, say we can't pass in alignment, which no current use does.
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000707 assert(!Attrs.hasAttribute(Index, Attribute::Alignment) &&
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000708 "Attempt to change alignment!");
709#endif
710
711 // Add the attribute slots before the one we're trying to add.
712 SmallVector<AttributeSet, 4> AttrSet;
713 uint64_t NumAttrs = pImpl->getNumAttributes();
714 AttributeSet AS;
715 uint64_t LastIndex = 0;
716 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000717 if (getSlotIndex(I) >= Index) {
718 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000719 break;
720 }
721 LastIndex = I + 1;
722 AttrSet.push_back(getSlotAttributes(I));
723 }
724
Bill Wendlinge7436542013-01-30 23:07:40 +0000725 // Now remove the attribute from the correct slot. There may already be an
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000726 // AttributeSet there.
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000727 AttrBuilder B(AS, Index);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000728
729 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000730 if (Attrs.getSlotIndex(I) == Index) {
731 B.removeAttributes(Attrs.pImpl->getSlotAttributes(I), Index);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000732 break;
733 }
734
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000735 AttrSet.push_back(AttributeSet::get(C, Index, B));
Bill Wendlingc22f4aa2013-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
744//===----------------------------------------------------------------------===//
745// AttributeSet Accessor Methods
746//===----------------------------------------------------------------------===//
747
Bill Wendling85b3fbe2013-02-10 05:00:40 +0000748LLVMContext &AttributeSet::getContext() const {
749 return pImpl->getContext();
750}
751
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000752AttributeSet AttributeSet::getParamAttributes(unsigned Index) const {
753 return pImpl && hasAttributes(Index) ?
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000754 AttributeSet::get(pImpl->getContext(),
Bill Wendling606c8e32013-01-29 03:20:31 +0000755 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000756 std::make_pair(Index, getAttributes(Index)))) :
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000757 AttributeSet();
758}
759
760AttributeSet AttributeSet::getRetAttributes() const {
761 return pImpl && hasAttributes(ReturnIndex) ?
762 AttributeSet::get(pImpl->getContext(),
Bill Wendling606c8e32013-01-29 03:20:31 +0000763 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000764 std::make_pair(ReturnIndex,
765 getAttributes(ReturnIndex)))) :
766 AttributeSet();
767}
768
769AttributeSet AttributeSet::getFnAttributes() const {
770 return pImpl && hasAttributes(FunctionIndex) ?
771 AttributeSet::get(pImpl->getContext(),
Bill Wendling606c8e32013-01-29 03:20:31 +0000772 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000773 std::make_pair(FunctionIndex,
774 getAttributes(FunctionIndex)))) :
775 AttributeSet();
776}
777
778bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{
Bill Wendling606c8e32013-01-29 03:20:31 +0000779 AttributeSetNode *ASN = getAttributes(Index);
780 return ASN ? ASN->hasAttribute(Kind) : false;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000781}
782
Bill Wendling0e9d5d02013-02-13 08:42:21 +0000783bool AttributeSet::hasAttribute(unsigned Index, StringRef Kind) const {
784 AttributeSetNode *ASN = getAttributes(Index);
785 return ASN ? ASN->hasAttribute(Kind) : false;
786}
787
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000788bool AttributeSet::hasAttributes(unsigned Index) const {
Bill Wendling606c8e32013-01-29 03:20:31 +0000789 AttributeSetNode *ASN = getAttributes(Index);
790 return ASN ? ASN->hasAttributes() : false;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000791}
792
793/// \brief Return true if the specified attribute is set for at least one
794/// parameter or for the return value.
795bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr) const {
796 if (pImpl == 0) return false;
797
798 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
Benjamin Kramere22cde02013-07-11 12:13:16 +0000799 for (AttributeSetImpl::iterator II = pImpl->begin(I),
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000800 IE = pImpl->end(I); II != IE; ++II)
NAKAMURA Takumieddab152013-01-31 03:47:28 +0000801 if (II->hasAttribute(Attr))
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000802 return true;
803
804 return false;
805}
806
Bill Wendling0e9d5d02013-02-13 08:42:21 +0000807Attribute AttributeSet::getAttribute(unsigned Index,
808 Attribute::AttrKind Kind) const {
809 AttributeSetNode *ASN = getAttributes(Index);
810 return ASN ? ASN->getAttribute(Kind) : Attribute();
811}
812
813Attribute AttributeSet::getAttribute(unsigned Index,
814 StringRef Kind) const {
815 AttributeSetNode *ASN = getAttributes(Index);
816 return ASN ? ASN->getAttribute(Kind) : Attribute();
817}
818
Bill Wendling606c8e32013-01-29 03:20:31 +0000819unsigned AttributeSet::getParamAlignment(unsigned Index) const {
820 AttributeSetNode *ASN = getAttributes(Index);
821 return ASN ? ASN->getAlignment() : 0;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000822}
823
824unsigned AttributeSet::getStackAlignment(unsigned Index) const {
Bill Wendling606c8e32013-01-29 03:20:31 +0000825 AttributeSetNode *ASN = getAttributes(Index);
826 return ASN ? ASN->getStackAlignment() : 0;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000827}
828
Rafael Espindolaaae02982013-05-01 13:07:03 +0000829std::string AttributeSet::getAsString(unsigned Index,
Bill Wendlingb29ce262013-02-11 08:43:33 +0000830 bool InAttrGrp) const {
Bill Wendling606c8e32013-01-29 03:20:31 +0000831 AttributeSetNode *ASN = getAttributes(Index);
Rafael Espindolaaae02982013-05-01 13:07:03 +0000832 return ASN ? ASN->getAsString(InAttrGrp) : std::string("");
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000833}
834
835/// \brief The attributes for the specified index are returned.
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000836AttributeSetNode *AttributeSet::getAttributes(unsigned Index) const {
Bill Wendling606c8e32013-01-29 03:20:31 +0000837 if (!pImpl) return 0;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000838
Bill Wendling606c8e32013-01-29 03:20:31 +0000839 // Loop through to find the attribute node we want.
840 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000841 if (pImpl->getSlotIndex(I) == Index)
Bill Wendling606c8e32013-01-29 03:20:31 +0000842 return pImpl->getSlotNode(I);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000843
Bill Wendling606c8e32013-01-29 03:20:31 +0000844 return 0;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000845}
846
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000847AttributeSet::iterator AttributeSet::begin(unsigned Slot) const {
Bill Wendling16c4b3c2013-01-31 23:53:05 +0000848 if (!pImpl)
849 return ArrayRef<Attribute>().begin();
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000850 return pImpl->begin(Slot);
Bill Wendling16c4b3c2013-01-31 23:53:05 +0000851}
852
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000853AttributeSet::iterator AttributeSet::end(unsigned Slot) const {
Bill Wendling16c4b3c2013-01-31 23:53:05 +0000854 if (!pImpl)
855 return ArrayRef<Attribute>().end();
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000856 return pImpl->end(Slot);
Bill Wendling16c4b3c2013-01-31 23:53:05 +0000857}
858
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000859//===----------------------------------------------------------------------===//
860// AttributeSet Introspection Methods
861//===----------------------------------------------------------------------===//
862
863/// \brief Return the number of slots used in this attribute list. This is the
864/// number of arguments that have an attribute set on them (including the
865/// function itself).
866unsigned AttributeSet::getNumSlots() const {
867 return pImpl ? pImpl->getNumAttributes() : 0;
868}
869
Rafael Espindola76f103e2013-04-30 16:53:38 +0000870unsigned AttributeSet::getSlotIndex(unsigned Slot) const {
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000871 assert(pImpl && Slot < pImpl->getNumAttributes() &&
872 "Slot # out of range!");
873 return pImpl->getSlotIndex(Slot);
874}
875
876AttributeSet AttributeSet::getSlotAttributes(unsigned Slot) const {
877 assert(pImpl && Slot < pImpl->getNumAttributes() &&
878 "Slot # out of range!");
879 return pImpl->getSlotAttributes(Slot);
880}
881
882uint64_t AttributeSet::Raw(unsigned Index) const {
883 // FIXME: Remove this.
884 return pImpl ? pImpl->Raw(Index) : 0;
885}
886
887void AttributeSet::dump() const {
888 dbgs() << "PAL[\n";
889
890 for (unsigned i = 0, e = getNumSlots(); i < e; ++i) {
891 uint64_t Index = getSlotIndex(i);
892 dbgs() << " { ";
893 if (Index == ~0U)
894 dbgs() << "~0U";
895 else
896 dbgs() << Index;
897 dbgs() << " => " << getAsString(Index) << " }\n";
898 }
899
900 dbgs() << "]\n";
901}
902
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000903//===----------------------------------------------------------------------===//
Bill Wendling03198882013-01-04 23:27:34 +0000904// AttrBuilder Method Implementations
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000905//===----------------------------------------------------------------------===//
906
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000907AttrBuilder::AttrBuilder(AttributeSet AS, unsigned Index)
Benjamin Kramerc835b8c2013-02-16 19:13:18 +0000908 : Attrs(0), Alignment(0), StackAlignment(0) {
Bill Wendlingec258982013-01-27 21:23:46 +0000909 AttributeSetImpl *pImpl = AS.pImpl;
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000910 if (!pImpl) return;
911
Bill Wendling73bc4522013-01-28 00:21:34 +0000912 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I) {
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000913 if (pImpl->getSlotIndex(I) != Index) continue;
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000914
Benjamin Kramere22cde02013-07-11 12:13:16 +0000915 for (AttributeSetImpl::iterator II = pImpl->begin(I),
Bill Wendling73bc4522013-01-28 00:21:34 +0000916 IE = pImpl->end(I); II != IE; ++II)
Bill Wendling39da0782013-01-31 23:38:01 +0000917 addAttribute(*II);
Bill Wendling73bc4522013-01-28 00:21:34 +0000918
919 break;
920 }
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000921}
922
Bill Wendling03198882013-01-04 23:27:34 +0000923void AttrBuilder::clear() {
Benjamin Kramer3f213e72013-02-18 12:09:51 +0000924 Attrs.reset();
Bill Wendling03198882013-01-04 23:27:34 +0000925 Alignment = StackAlignment = 0;
926}
927
928AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
Benjamin Kramer3f213e72013-02-18 12:09:51 +0000929 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
Bill Wendling169d5272013-01-31 23:16:25 +0000930 assert(Val != Attribute::Alignment && Val != Attribute::StackAlignment &&
931 "Adding alignment attribute without adding alignment value!");
Benjamin Kramer3f213e72013-02-18 12:09:51 +0000932 Attrs[Val] = true;
Bill Wendling3a106e62012-10-09 19:01:18 +0000933 return *this;
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000934}
935
Bill Wendling39da0782013-01-31 23:38:01 +0000936AttrBuilder &AttrBuilder::addAttribute(Attribute Attr) {
Bill Wendling09ed9102013-02-10 10:13:23 +0000937 if (Attr.isStringAttribute()) {
938 addAttribute(Attr.getKindAsString(), Attr.getValueAsString());
939 return *this;
940 }
941
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000942 Attribute::AttrKind Kind = Attr.getKindAsEnum();
Benjamin Kramer3f213e72013-02-18 12:09:51 +0000943 Attrs[Kind] = true;
Bill Wendling49f60602013-01-28 05:23:28 +0000944
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000945 if (Kind == Attribute::Alignment)
Bill Wendling49f60602013-01-28 05:23:28 +0000946 Alignment = Attr.getAlignment();
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000947 else if (Kind == Attribute::StackAlignment)
Bill Wendling49f60602013-01-28 05:23:28 +0000948 StackAlignment = Attr.getStackAlignment();
949 return *this;
950}
951
Bill Wendlingea59f892013-02-05 08:09:32 +0000952AttrBuilder &AttrBuilder::addAttribute(StringRef A, StringRef V) {
953 TargetDepAttrs[A] = V;
954 return *this;
955}
956
Bill Wendling39da0782013-01-31 23:38:01 +0000957AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
Benjamin Kramer3f213e72013-02-18 12:09:51 +0000958 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
959 Attrs[Val] = false;
Bill Wendling39da0782013-01-31 23:38:01 +0000960
961 if (Val == Attribute::Alignment)
962 Alignment = 0;
963 else if (Val == Attribute::StackAlignment)
964 StackAlignment = 0;
965
966 return *this;
967}
968
Bill Wendlinge7436542013-01-30 23:07:40 +0000969AttrBuilder &AttrBuilder::removeAttributes(AttributeSet A, uint64_t Index) {
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000970 unsigned Slot = ~0U;
Bill Wendling30d2c762013-02-01 00:13:50 +0000971 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
972 if (A.getSlotIndex(I) == Index) {
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000973 Slot = I;
Bill Wendling30d2c762013-02-01 00:13:50 +0000974 break;
Bill Wendling49f60602013-01-28 05:23:28 +0000975 }
Bill Wendling30d2c762013-02-01 00:13:50 +0000976
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000977 assert(Slot != ~0U && "Couldn't find index in AttributeSet!");
Bill Wendling30d2c762013-02-01 00:13:50 +0000978
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000979 for (AttributeSet::iterator I = A.begin(Slot), E = A.end(Slot); I != E; ++I) {
Bill Wendling74fe8252013-02-12 07:56:49 +0000980 Attribute Attr = *I;
981 if (Attr.isEnumAttribute() || Attr.isAlignAttribute()) {
982 Attribute::AttrKind Kind = I->getKindAsEnum();
Benjamin Kramer3f213e72013-02-18 12:09:51 +0000983 Attrs[Kind] = false;
Bill Wendling30d2c762013-02-01 00:13:50 +0000984
Bill Wendling74fe8252013-02-12 07:56:49 +0000985 if (Kind == Attribute::Alignment)
986 Alignment = 0;
987 else if (Kind == Attribute::StackAlignment)
988 StackAlignment = 0;
989 } else {
990 assert(Attr.isStringAttribute() && "Invalid attribute type!");
991 std::map<std::string, std::string>::iterator
992 Iter = TargetDepAttrs.find(Attr.getKindAsString());
993 if (Iter != TargetDepAttrs.end())
994 TargetDepAttrs.erase(Iter);
995 }
Bill Wendling49f60602013-01-28 05:23:28 +0000996 }
997
998 return *this;
999}
1000
Bill Wendlingea59f892013-02-05 08:09:32 +00001001AttrBuilder &AttrBuilder::removeAttribute(StringRef A) {
1002 std::map<std::string, std::string>::iterator I = TargetDepAttrs.find(A);
1003 if (I != TargetDepAttrs.end())
1004 TargetDepAttrs.erase(I);
1005 return *this;
1006}
1007
Bill Wendling702cc912012-10-15 20:35:56 +00001008AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
Bill Wendlingda3f9d82012-10-14 03:58:29 +00001009 if (Align == 0) return *this;
Bill Wendling03198882013-01-04 23:27:34 +00001010
Bill Wendlinge66f3d32012-10-05 06:44:41 +00001011 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1012 assert(Align <= 0x40000000 && "Alignment too large.");
Bill Wendling03198882013-01-04 23:27:34 +00001013
Benjamin Kramer3f213e72013-02-18 12:09:51 +00001014 Attrs[Attribute::Alignment] = true;
Bill Wendling03198882013-01-04 23:27:34 +00001015 Alignment = Align;
Bill Wendlingda3f9d82012-10-14 03:58:29 +00001016 return *this;
Chris Lattner50ee9dd2008-01-02 23:42:30 +00001017}
1018
Bill Wendling03198882013-01-04 23:27:34 +00001019AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
1020 // Default alignment, allow the target to define how to align it.
1021 if (Align == 0) return *this;
1022
1023 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1024 assert(Align <= 0x100 && "Alignment too large.");
1025
Benjamin Kramer3f213e72013-02-18 12:09:51 +00001026 Attrs[Attribute::StackAlignment] = true;
Bill Wendling03198882013-01-04 23:27:34 +00001027 StackAlignment = Align;
1028 return *this;
1029}
1030
Bill Wendling85df6b42013-02-06 01:16:00 +00001031AttrBuilder &AttrBuilder::merge(const AttrBuilder &B) {
1032 // FIXME: What if both have alignments, but they don't match?!
1033 if (!Alignment)
1034 Alignment = B.Alignment;
1035
1036 if (!StackAlignment)
1037 StackAlignment = B.StackAlignment;
1038
Benjamin Kramerc835b8c2013-02-16 19:13:18 +00001039 Attrs |= B.Attrs;
Bill Wendling85df6b42013-02-06 01:16:00 +00001040
1041 for (td_const_iterator I = B.TargetDepAttrs.begin(),
1042 E = B.TargetDepAttrs.end(); I != E; ++I)
1043 TargetDepAttrs[I->first] = I->second;
1044
1045 return *this;
1046}
1047
Bill Wendlingc342d9d2013-02-06 01:33:42 +00001048bool AttrBuilder::contains(StringRef A) const {
1049 return TargetDepAttrs.find(A) != TargetDepAttrs.end();
1050}
1051
Bill Wendling702cc912012-10-15 20:35:56 +00001052bool AttrBuilder::hasAttributes() const {
Benjamin Kramer3f213e72013-02-18 12:09:51 +00001053 return !Attrs.none() || !TargetDepAttrs.empty();
Bill Wendlingf385f4c2012-10-08 23:27:46 +00001054}
Bill Wendling60507d52013-01-04 20:54:35 +00001055
Bill Wendlinge7436542013-01-30 23:07:40 +00001056bool AttrBuilder::hasAttributes(AttributeSet A, uint64_t Index) const {
Bill Wendling8a6a7bb2013-04-18 20:17:28 +00001057 unsigned Slot = ~0U;
Bill Wendlingbdcbccc2013-02-02 00:42:06 +00001058 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
1059 if (A.getSlotIndex(I) == Index) {
Bill Wendling8a6a7bb2013-04-18 20:17:28 +00001060 Slot = I;
Bill Wendlingbdcbccc2013-02-02 00:42:06 +00001061 break;
1062 }
1063
Bill Wendling8a6a7bb2013-04-18 20:17:28 +00001064 assert(Slot != ~0U && "Couldn't find the index!");
Bill Wendlingbdcbccc2013-02-02 00:42:06 +00001065
Bill Wendling8a6a7bb2013-04-18 20:17:28 +00001066 for (AttributeSet::iterator I = A.begin(Slot), E = A.end(Slot);
Bill Wendling74fe8252013-02-12 07:56:49 +00001067 I != E; ++I) {
1068 Attribute Attr = *I;
1069 if (Attr.isEnumAttribute() || Attr.isAlignAttribute()) {
Benjamin Kramer3f213e72013-02-18 12:09:51 +00001070 if (Attrs[I->getKindAsEnum()])
Bill Wendling74fe8252013-02-12 07:56:49 +00001071 return true;
1072 } else {
1073 assert(Attr.isStringAttribute() && "Invalid attribute kind!");
1074 return TargetDepAttrs.find(Attr.getKindAsString())!=TargetDepAttrs.end();
1075 }
1076 }
Bill Wendlingbdcbccc2013-02-02 00:42:06 +00001077
1078 return false;
Bill Wendling8831c062012-10-09 00:01:21 +00001079}
Bill Wendling60507d52013-01-04 20:54:35 +00001080
Bill Wendling702cc912012-10-15 20:35:56 +00001081bool AttrBuilder::hasAlignmentAttr() const {
Bill Wendling03198882013-01-04 23:27:34 +00001082 return Alignment != 0;
Bill Wendlingf385f4c2012-10-08 23:27:46 +00001083}
1084
Bill Wendlingc22f4aa2013-01-29 00:34:06 +00001085bool AttrBuilder::operator==(const AttrBuilder &B) {
Benjamin Kramerc835b8c2013-02-16 19:13:18 +00001086 if (Attrs != B.Attrs)
1087 return false;
Bill Wendlingc342d9d2013-02-06 01:33:42 +00001088
1089 for (td_const_iterator I = TargetDepAttrs.begin(),
1090 E = TargetDepAttrs.end(); I != E; ++I)
1091 if (B.TargetDepAttrs.find(I->first) == B.TargetDepAttrs.end())
1092 return false;
1093
1094 return Alignment == B.Alignment && StackAlignment == B.StackAlignment;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +00001095}
1096
1097AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) {
Bill Wendlingf9271ea2013-02-04 23:32:23 +00001098 // FIXME: Remove this in 4.0.
Bill Wendling8232ece2013-01-29 01:43:29 +00001099 if (!Val) return *this;
1100
Bill Wendlingc22f4aa2013-01-29 00:34:06 +00001101 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
1102 I = Attribute::AttrKind(I + 1)) {
1103 if (uint64_t A = (Val & AttributeImpl::getAttrMask(I))) {
Benjamin Kramer3f213e72013-02-18 12:09:51 +00001104 Attrs[I] = true;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +00001105
1106 if (I == Attribute::Alignment)
1107 Alignment = 1ULL << ((A >> 16) - 1);
1108 else if (I == Attribute::StackAlignment)
1109 StackAlignment = 1ULL << ((A >> 26)-1);
1110 }
1111 }
1112
1113 return *this;
1114}
1115
Bill Wendling8e47daf2013-01-25 23:09:36 +00001116//===----------------------------------------------------------------------===//
1117// AttributeFuncs Function Defintions
1118//===----------------------------------------------------------------------===//
1119
Bill Wendling7beee282013-02-01 01:04:27 +00001120/// \brief Which attributes cannot be applied to a type.
Bill Wendlinge7436542013-01-30 23:07:40 +00001121AttributeSet AttributeFuncs::typeIncompatible(Type *Ty, uint64_t Index) {
Bill Wendling8e47daf2013-01-25 23:09:36 +00001122 AttrBuilder Incompatible;
1123
1124 if (!Ty->isIntegerTy())
1125 // Attribute that only apply to integers.
1126 Incompatible.addAttribute(Attribute::SExt)
1127 .addAttribute(Attribute::ZExt);
1128
1129 if (!Ty->isPointerTy())
1130 // Attribute that only apply to pointers.
1131 Incompatible.addAttribute(Attribute::ByVal)
1132 .addAttribute(Attribute::Nest)
1133 .addAttribute(Attribute::NoAlias)
1134 .addAttribute(Attribute::NoCapture)
Nick Lewyckydc897372013-07-06 00:29:58 +00001135 .addAttribute(Attribute::ReadNone)
1136 .addAttribute(Attribute::ReadOnly)
Bill Wendling8e47daf2013-01-25 23:09:36 +00001137 .addAttribute(Attribute::StructRet);
1138
Bill Wendlinge7436542013-01-30 23:07:40 +00001139 return AttributeSet::get(Ty->getContext(), Index, Incompatible);
Bill Wendling8e47daf2013-01-25 23:09:36 +00001140}