blob: 2160ea24641cffaa01c4af19fc62df8716abd976 [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.
46 PA = !Val ?
47 new AttributeImpl(Context, Kind) :
48 new AttributeImpl(Context, Kind, Val);
49 pImpl->AttrsSet.InsertNode(PA, InsertPoint);
50 }
51
52 // Return the Attribute that we found or created.
53 return Attribute(PA);
54}
55
56Attribute Attribute::get(LLVMContext &Context, StringRef Kind, StringRef Val) {
57 LLVMContextImpl *pImpl = Context.pImpl;
58 FoldingSetNodeID ID;
59 ID.AddString(Kind);
60 if (!Val.empty()) ID.AddString(Val);
Bill Wendling8e635db2012-10-08 21:47:17 +000061
62 void *InsertPoint;
Bill Wendlingf6670722012-12-20 01:36:59 +000063 AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
Bill Wendling8e635db2012-10-08 21:47:17 +000064
65 if (!PA) {
66 // If we didn't find any existing attributes of the same shape then create a
67 // new one and insert it.
Bill Wendlingbdcbccc2013-02-02 00:42:06 +000068 PA = new AttributeImpl(Context, Kind, Val);
Bill Wendling8e635db2012-10-08 21:47:17 +000069 pImpl->AttrsSet.InsertNode(PA, InsertPoint);
70 }
71
Bill Wendlingea59f892013-02-05 08:09:32 +000072 // Return the Attribute that we found or created.
Bill Wendling034b94b2012-12-19 07:18:57 +000073 return Attribute(PA);
Bill Wendling8e635db2012-10-08 21:47:17 +000074}
75
Bill Wendlingc08a5ef2013-01-27 22:43:04 +000076Attribute Attribute::getWithAlignment(LLVMContext &Context, uint64_t Align) {
Bill Wendling169d5272013-01-31 23:16:25 +000077 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
78 assert(Align <= 0x40000000 && "Alignment too large.");
Bill Wendling8c74ecf2013-02-05 22:37:24 +000079 return get(Context, Alignment, Align);
Bill Wendlingc08a5ef2013-01-27 22:43:04 +000080}
81
82Attribute Attribute::getWithStackAlignment(LLVMContext &Context,
83 uint64_t Align) {
Bill Wendling169d5272013-01-31 23:16:25 +000084 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
85 assert(Align <= 0x100 && "Alignment too large.");
Bill Wendling8c74ecf2013-02-05 22:37:24 +000086 return get(Context, StackAlignment, Align);
Bill Wendlingc08a5ef2013-01-27 22:43:04 +000087}
88
Bill Wendling817abdd2013-01-29 00:48:16 +000089//===----------------------------------------------------------------------===//
90// Attribute Accessor Methods
91//===----------------------------------------------------------------------===//
92
Bill Wendling8c74ecf2013-02-05 22:37:24 +000093bool Attribute::isEnumAttribute() const {
94 return pImpl && pImpl->isEnumAttribute();
95}
96
97bool Attribute::isAlignAttribute() const {
98 return pImpl && pImpl->isAlignAttribute();
99}
100
101bool Attribute::isStringAttribute() const {
102 return pImpl && pImpl->isStringAttribute();
103}
104
105Attribute::AttrKind Attribute::getKindAsEnum() const {
106 assert((isEnumAttribute() || isAlignAttribute()) &&
107 "Invalid attribute type to get the kind as an enum!");
108 return pImpl ? pImpl->getKindAsEnum() : None;
109}
110
111uint64_t Attribute::getValueAsInt() const {
112 assert(isAlignAttribute() &&
113 "Expected the attribute to be an alignment attribute!");
114 return pImpl ? pImpl->getValueAsInt() : 0;
115}
116
117StringRef Attribute::getKindAsString() const {
118 assert(isStringAttribute() &&
119 "Invalid attribute type to get the kind as a string!");
120 return pImpl ? pImpl->getKindAsString() : StringRef();
121}
122
123StringRef Attribute::getValueAsString() const {
124 assert(isStringAttribute() &&
125 "Invalid attribute type to get the value as a string!");
126 return pImpl ? pImpl->getValueAsString() : StringRef();
127}
128
Bill Wendling64754f42013-02-05 23:48:36 +0000129bool Attribute::hasAttribute(AttrKind Kind) const {
130 return (pImpl && pImpl->hasAttribute(Kind)) || (!pImpl && Kind == None);
131}
132
133bool Attribute::hasAttribute(StringRef Kind) const {
134 if (!isStringAttribute()) return false;
135 return pImpl && pImpl->hasAttribute(Kind);
Bill Wendling6dc37812013-01-29 20:45:34 +0000136}
137
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000138/// This returns the alignment field of an attribute as a byte alignment value.
Bill Wendling034b94b2012-12-19 07:18:57 +0000139unsigned Attribute::getAlignment() const {
Bill Wendling7beee282013-02-01 01:04:27 +0000140 assert(hasAttribute(Attribute::Alignment) &&
141 "Trying to get alignment from non-alignment attribute!");
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000142 return pImpl->getValueAsInt();
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000143}
144
145/// This returns the stack alignment field of an attribute as a byte alignment
146/// value.
Bill Wendling034b94b2012-12-19 07:18:57 +0000147unsigned Attribute::getStackAlignment() const {
Bill Wendling7beee282013-02-01 01:04:27 +0000148 assert(hasAttribute(Attribute::StackAlignment) &&
149 "Trying to get alignment from non-alignment attribute!");
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000150 return pImpl->getValueAsInt();
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000151}
152
Bill Wendlingb29ce262013-02-11 08:43:33 +0000153std::string Attribute::getAsString(bool InAttrGrp) const {
Bill Wendling14292a62013-01-31 20:59:05 +0000154 if (!pImpl) return "";
155
Kostya Serebryany8eec41f2013-02-26 06:58:09 +0000156 if (hasAttribute(Attribute::SanitizeAddress))
157 return "sanitize_address";
Bill Wendling14292a62013-01-31 20:59:05 +0000158 if (hasAttribute(Attribute::AlwaysInline))
159 return "alwaysinline";
Michael Gottesman2253a2f2013-06-27 00:25:01 +0000160 if (hasAttribute(Attribute::Builtin))
161 return "builtin";
Bill Wendling14292a62013-01-31 20:59:05 +0000162 if (hasAttribute(Attribute::ByVal))
163 return "byval";
164 if (hasAttribute(Attribute::InlineHint))
165 return "inlinehint";
Bill Wendling034b94b2012-12-19 07:18:57 +0000166 if (hasAttribute(Attribute::InReg))
Bill Wendling606c8e32013-01-29 03:20:31 +0000167 return "inreg";
Bill Wendling14292a62013-01-31 20:59:05 +0000168 if (hasAttribute(Attribute::MinSize))
169 return "minsize";
170 if (hasAttribute(Attribute::Naked))
171 return "naked";
172 if (hasAttribute(Attribute::Nest))
173 return "nest";
Bill Wendling034b94b2012-12-19 07:18:57 +0000174 if (hasAttribute(Attribute::NoAlias))
Bill Wendling606c8e32013-01-29 03:20:31 +0000175 return "noalias";
Bill Wendling143d4642013-02-22 00:12:35 +0000176 if (hasAttribute(Attribute::NoBuiltin))
177 return "nobuiltin";
Bill Wendling034b94b2012-12-19 07:18:57 +0000178 if (hasAttribute(Attribute::NoCapture))
Bill Wendling606c8e32013-01-29 03:20:31 +0000179 return "nocapture";
Bill Wendling14292a62013-01-31 20:59:05 +0000180 if (hasAttribute(Attribute::NoDuplicate))
181 return "noduplicate";
182 if (hasAttribute(Attribute::NoImplicitFloat))
183 return "noimplicitfloat";
184 if (hasAttribute(Attribute::NoInline))
185 return "noinline";
186 if (hasAttribute(Attribute::NonLazyBind))
187 return "nonlazybind";
188 if (hasAttribute(Attribute::NoRedZone))
189 return "noredzone";
190 if (hasAttribute(Attribute::NoReturn))
191 return "noreturn";
192 if (hasAttribute(Attribute::NoUnwind))
193 return "nounwind";
194 if (hasAttribute(Attribute::OptimizeForSize))
195 return "optsize";
Bill Wendling034b94b2012-12-19 07:18:57 +0000196 if (hasAttribute(Attribute::ReadNone))
Bill Wendling606c8e32013-01-29 03:20:31 +0000197 return "readnone";
Bill Wendling034b94b2012-12-19 07:18:57 +0000198 if (hasAttribute(Attribute::ReadOnly))
Bill Wendling606c8e32013-01-29 03:20:31 +0000199 return "readonly";
Stephen Lin456ca042013-04-20 05:14:40 +0000200 if (hasAttribute(Attribute::Returned))
201 return "returned";
Bill Wendling14292a62013-01-31 20:59:05 +0000202 if (hasAttribute(Attribute::ReturnsTwice))
203 return "returns_twice";
204 if (hasAttribute(Attribute::SExt))
205 return "signext";
Bill Wendling034b94b2012-12-19 07:18:57 +0000206 if (hasAttribute(Attribute::StackProtect))
Bill Wendling606c8e32013-01-29 03:20:31 +0000207 return "ssp";
Bill Wendling034b94b2012-12-19 07:18:57 +0000208 if (hasAttribute(Attribute::StackProtectReq))
Bill Wendling606c8e32013-01-29 03:20:31 +0000209 return "sspreq";
Bill Wendling114baee2013-01-23 06:41:41 +0000210 if (hasAttribute(Attribute::StackProtectStrong))
Bill Wendling606c8e32013-01-29 03:20:31 +0000211 return "sspstrong";
Bill Wendling14292a62013-01-31 20:59:05 +0000212 if (hasAttribute(Attribute::StructRet))
213 return "sret";
Kostya Serebryany8eec41f2013-02-26 06:58:09 +0000214 if (hasAttribute(Attribute::SanitizeThread))
215 return "sanitize_thread";
216 if (hasAttribute(Attribute::SanitizeMemory))
217 return "sanitize_memory";
Bill Wendling14292a62013-01-31 20:59:05 +0000218 if (hasAttribute(Attribute::UWTable))
219 return "uwtable";
220 if (hasAttribute(Attribute::ZExt))
221 return "zeroext";
Diego Novillo77226a02013-05-24 12:26:52 +0000222 if (hasAttribute(Attribute::Cold))
223 return "cold";
Bill Wendling14292a62013-01-31 20:59:05 +0000224
225 // FIXME: These should be output like this:
226 //
227 // align=4
228 // alignstack=8
229 //
Bill Wendling034b94b2012-12-19 07:18:57 +0000230 if (hasAttribute(Attribute::Alignment)) {
Bill Wendling606c8e32013-01-29 03:20:31 +0000231 std::string Result;
Bill Wendlingb29ce262013-02-11 08:43:33 +0000232 Result += "align";
233 Result += (InAttrGrp) ? "=" : " ";
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000234 Result += utostr(getValueAsInt());
235 return Result;
236 }
Bill Wendlingb29ce262013-02-11 08:43:33 +0000237
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000238 if (hasAttribute(Attribute::StackAlignment)) {
239 std::string Result;
Bill Wendlingb29ce262013-02-11 08:43:33 +0000240 Result += "alignstack";
241 if (InAttrGrp) {
242 Result += "=";
243 Result += utostr(getValueAsInt());
244 } else {
245 Result += "(";
246 Result += utostr(getValueAsInt());
247 Result += ")";
248 }
Bill Wendling606c8e32013-01-29 03:20:31 +0000249 return Result;
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000250 }
Bill Wendling14292a62013-01-31 20:59:05 +0000251
252 // Convert target-dependent attributes to strings of the form:
253 //
254 // "kind"
255 // "kind" = "value"
Bill Wendling14292a62013-01-31 20:59:05 +0000256 //
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000257 if (isStringAttribute()) {
Bill Wendling14292a62013-01-31 20:59:05 +0000258 std::string Result;
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000259 Result += '\"' + getKindAsString().str() + '"';
Bill Wendling14292a62013-01-31 20:59:05 +0000260
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000261 StringRef Val = pImpl->getValueAsString();
262 if (Val.empty()) return Result;
Bill Wendling5a4041e2013-02-01 22:32:30 +0000263
Bill Wendlingb29ce262013-02-11 08:43:33 +0000264 Result += "=\"" + Val.str() + '"';
Bill Wendling7beee282013-02-01 01:04:27 +0000265 return Result;
Bill Wendling14292a62013-01-31 20:59:05 +0000266 }
Bill Wendling606c8e32013-01-29 03:20:31 +0000267
268 llvm_unreachable("Unknown attribute");
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000269}
270
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000271bool Attribute::operator<(Attribute A) const {
272 if (!pImpl && !A.pImpl) return false;
273 if (!pImpl) return true;
274 if (!A.pImpl) return false;
275 return *pImpl < *A.pImpl;
276}
277
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000278//===----------------------------------------------------------------------===//
279// AttributeImpl Definition
280//===----------------------------------------------------------------------===//
281
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000282AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind Kind)
283 : Context(C), Entry(new EnumAttributeEntry(Kind)) {}
284
285AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind Kind,
286 unsigned Align)
287 : Context(C) {
288 assert((Kind == Attribute::Alignment || Kind == Attribute::StackAlignment) &&
289 "Wrong kind for alignment attribute!");
290 Entry = new AlignAttributeEntry(Kind, Align);
291}
292
293AttributeImpl::AttributeImpl(LLVMContext &C, StringRef Kind, StringRef Val)
294 : Context(C), Entry(new StringAttributeEntry(Kind, Val)) {}
295
296AttributeImpl::~AttributeImpl() {
297 delete Entry;
298}
299
300bool AttributeImpl::isEnumAttribute() const {
301 return isa<EnumAttributeEntry>(Entry);
302}
303
304bool AttributeImpl::isAlignAttribute() const {
305 return isa<AlignAttributeEntry>(Entry);
306}
307
308bool AttributeImpl::isStringAttribute() const {
309 return isa<StringAttributeEntry>(Entry);
310}
311
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000312bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000313 if (isStringAttribute()) return false;
314 return getKindAsEnum() == A;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000315}
316
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000317bool AttributeImpl::hasAttribute(StringRef Kind) const {
318 if (!isStringAttribute()) return false;
319 return getKindAsString() == Kind;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000320}
321
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000322Attribute::AttrKind AttributeImpl::getKindAsEnum() const {
323 if (EnumAttributeEntry *E = dyn_cast<EnumAttributeEntry>(Entry))
324 return E->getEnumKind();
325 return cast<AlignAttributeEntry>(Entry)->getEnumKind();
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000326}
327
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000328uint64_t AttributeImpl::getValueAsInt() const {
329 return cast<AlignAttributeEntry>(Entry)->getAlignment();
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000330}
331
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000332StringRef AttributeImpl::getKindAsString() const {
333 return cast<StringAttributeEntry>(Entry)->getStringKind();
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000334}
335
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000336StringRef AttributeImpl::getValueAsString() const {
337 return cast<StringAttributeEntry>(Entry)->getStringValue();
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000338}
339
340bool AttributeImpl::operator<(const AttributeImpl &AI) const {
Bill Wendling7beee282013-02-01 01:04:27 +0000341 // This sorts the attributes with Attribute::AttrKinds coming first (sorted
342 // relative to their enum value) and then strings.
Bill Wendling94328f42013-02-15 05:25:26 +0000343 if (isEnumAttribute()) {
344 if (AI.isEnumAttribute()) return getKindAsEnum() < AI.getKindAsEnum();
345 if (AI.isAlignAttribute()) return true;
346 if (AI.isStringAttribute()) return true;
347 }
Bill Wendling7beee282013-02-01 01:04:27 +0000348
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000349 if (isAlignAttribute()) {
Bill Wendling94328f42013-02-15 05:25:26 +0000350 if (AI.isEnumAttribute()) return false;
351 if (AI.isAlignAttribute()) return getValueAsInt() < AI.getValueAsInt();
352 if (AI.isStringAttribute()) return true;
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000353 }
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000354
Bill Wendling94328f42013-02-15 05:25:26 +0000355 if (AI.isEnumAttribute()) return false;
356 if (AI.isAlignAttribute()) return false;
357 if (getKindAsString() == AI.getKindAsString())
358 return getValueAsString() < AI.getValueAsString();
359 return getKindAsString() < AI.getKindAsString();
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000360}
361
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000362uint64_t AttributeImpl::getAttrMask(Attribute::AttrKind Val) {
363 // FIXME: Remove this.
364 switch (Val) {
365 case Attribute::EndAttrKinds:
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000366 llvm_unreachable("Synthetic enumerators which should never get here");
367
368 case Attribute::None: return 0;
369 case Attribute::ZExt: return 1 << 0;
370 case Attribute::SExt: return 1 << 1;
371 case Attribute::NoReturn: return 1 << 2;
372 case Attribute::InReg: return 1 << 3;
373 case Attribute::StructRet: return 1 << 4;
374 case Attribute::NoUnwind: return 1 << 5;
375 case Attribute::NoAlias: return 1 << 6;
376 case Attribute::ByVal: return 1 << 7;
377 case Attribute::Nest: return 1 << 8;
378 case Attribute::ReadNone: return 1 << 9;
379 case Attribute::ReadOnly: return 1 << 10;
380 case Attribute::NoInline: return 1 << 11;
381 case Attribute::AlwaysInline: return 1 << 12;
382 case Attribute::OptimizeForSize: return 1 << 13;
383 case Attribute::StackProtect: return 1 << 14;
384 case Attribute::StackProtectReq: return 1 << 15;
385 case Attribute::Alignment: return 31 << 16;
386 case Attribute::NoCapture: return 1 << 21;
387 case Attribute::NoRedZone: return 1 << 22;
388 case Attribute::NoImplicitFloat: return 1 << 23;
389 case Attribute::Naked: return 1 << 24;
390 case Attribute::InlineHint: return 1 << 25;
391 case Attribute::StackAlignment: return 7 << 26;
392 case Attribute::ReturnsTwice: return 1 << 29;
393 case Attribute::UWTable: return 1 << 30;
394 case Attribute::NonLazyBind: return 1U << 31;
Kostya Serebryany8eec41f2013-02-26 06:58:09 +0000395 case Attribute::SanitizeAddress: return 1ULL << 32;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000396 case Attribute::MinSize: return 1ULL << 33;
397 case Attribute::NoDuplicate: return 1ULL << 34;
398 case Attribute::StackProtectStrong: return 1ULL << 35;
Kostya Serebryany8eec41f2013-02-26 06:58:09 +0000399 case Attribute::SanitizeThread: return 1ULL << 36;
400 case Attribute::SanitizeMemory: return 1ULL << 37;
Bill Wendlingd18e0b92013-02-22 00:40:12 +0000401 case Attribute::NoBuiltin: return 1ULL << 38;
Stephen Lin456ca042013-04-20 05:14:40 +0000402 case Attribute::Returned: return 1ULL << 39;
Diego Novillo77226a02013-05-24 12:26:52 +0000403 case Attribute::Cold: return 1ULL << 40;
Michael Gottesman2253a2f2013-06-27 00:25:01 +0000404 case Attribute::Builtin: return 1ULL << 41;
Bill Wendlingbd2acfa2013-02-22 00:50:09 +0000405 }
406 llvm_unreachable("Unsupported attribute type");
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000407}
408
409//===----------------------------------------------------------------------===//
410// AttributeSetNode Definition
411//===----------------------------------------------------------------------===//
412
413AttributeSetNode *AttributeSetNode::get(LLVMContext &C,
414 ArrayRef<Attribute> Attrs) {
415 if (Attrs.empty())
416 return 0;
417
418 // Otherwise, build a key to look up the existing attributes.
419 LLVMContextImpl *pImpl = C.pImpl;
420 FoldingSetNodeID ID;
421
422 SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end());
Bill Wendlingf107e6c2013-02-13 09:26:26 +0000423 array_pod_sort(SortedAttrs.begin(), SortedAttrs.end());
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000424
425 for (SmallVectorImpl<Attribute>::iterator I = SortedAttrs.begin(),
426 E = SortedAttrs.end(); I != E; ++I)
427 I->Profile(ID);
428
429 void *InsertPoint;
430 AttributeSetNode *PA =
431 pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint);
432
433 // If we didn't find any existing attributes of the same shape then create a
434 // new one and insert it.
435 if (!PA) {
436 PA = new AttributeSetNode(SortedAttrs);
437 pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint);
438 }
439
440 // Return the AttributesListNode that we found or created.
441 return PA;
442}
443
Bill Wendling606c8e32013-01-29 03:20:31 +0000444bool AttributeSetNode::hasAttribute(Attribute::AttrKind Kind) const {
445 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
446 E = AttrList.end(); I != E; ++I)
NAKAMURA Takumieddab152013-01-31 03:47:28 +0000447 if (I->hasAttribute(Kind))
Bill Wendling606c8e32013-01-29 03:20:31 +0000448 return true;
449 return false;
450}
451
Bill Wendling0e9d5d02013-02-13 08:42:21 +0000452bool AttributeSetNode::hasAttribute(StringRef Kind) const {
453 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
454 E = AttrList.end(); I != E; ++I)
455 if (I->hasAttribute(Kind))
456 return true;
457 return false;
458}
459
460Attribute AttributeSetNode::getAttribute(Attribute::AttrKind Kind) const {
461 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
462 E = AttrList.end(); I != E; ++I)
463 if (I->hasAttribute(Kind))
464 return *I;
465 return Attribute();
466}
467
468Attribute AttributeSetNode::getAttribute(StringRef Kind) const {
469 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
470 E = AttrList.end(); I != E; ++I)
471 if (I->hasAttribute(Kind))
472 return *I;
473 return Attribute();
474}
475
Bill Wendling606c8e32013-01-29 03:20:31 +0000476unsigned AttributeSetNode::getAlignment() const {
477 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
478 E = AttrList.end(); I != E; ++I)
NAKAMURA Takumieddab152013-01-31 03:47:28 +0000479 if (I->hasAttribute(Attribute::Alignment))
Bill Wendling606c8e32013-01-29 03:20:31 +0000480 return I->getAlignment();
481 return 0;
482}
483
484unsigned AttributeSetNode::getStackAlignment() const {
485 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
486 E = AttrList.end(); I != E; ++I)
NAKAMURA Takumieddab152013-01-31 03:47:28 +0000487 if (I->hasAttribute(Attribute::StackAlignment))
Bill Wendling606c8e32013-01-29 03:20:31 +0000488 return I->getStackAlignment();
489 return 0;
490}
491
Rafael Espindolaaae02982013-05-01 13:07:03 +0000492std::string AttributeSetNode::getAsString(bool InAttrGrp) const {
Benjamin Kramere94e4ca2013-04-19 11:43:21 +0000493 std::string Str;
Bill Wendling606c8e32013-01-29 03:20:31 +0000494 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
Benjamin Kramere94e4ca2013-04-19 11:43:21 +0000495 E = AttrList.end(); I != E; ++I) {
Rafael Espindolaaae02982013-05-01 13:07:03 +0000496 if (I != AttrList.begin())
497 Str += ' ';
498 Str += I->getAsString(InAttrGrp);
Bill Wendling606c8e32013-01-29 03:20:31 +0000499 }
500 return Str;
501}
502
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000503//===----------------------------------------------------------------------===//
504// AttributeSetImpl Definition
505//===----------------------------------------------------------------------===//
506
Rafael Espindola76f103e2013-04-30 16:53:38 +0000507uint64_t AttributeSetImpl::Raw(unsigned Index) const {
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000508 for (unsigned I = 0, E = getNumAttributes(); I != E; ++I) {
509 if (getSlotIndex(I) != Index) continue;
510 const AttributeSetNode *ASN = AttrNodes[I].second;
Bill Wendlingfca0ed22013-02-02 00:52:44 +0000511 uint64_t Mask = 0;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000512
513 for (AttributeSetNode::const_iterator II = ASN->begin(),
Bill Wendlingfca0ed22013-02-02 00:52:44 +0000514 IE = ASN->end(); II != IE; ++II) {
515 Attribute Attr = *II;
Bill Wendling21536912013-02-10 23:18:05 +0000516
517 // This cannot handle string attributes.
518 if (Attr.isStringAttribute()) continue;
519
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000520 Attribute::AttrKind Kind = Attr.getKindAsEnum();
Bill Wendlingfca0ed22013-02-02 00:52:44 +0000521
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000522 if (Kind == Attribute::Alignment)
Bill Wendlingfca0ed22013-02-02 00:52:44 +0000523 Mask |= (Log2_32(ASN->getAlignment()) + 1) << 16;
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000524 else if (Kind == Attribute::StackAlignment)
Bill Wendlingfca0ed22013-02-02 00:52:44 +0000525 Mask |= (Log2_32(ASN->getStackAlignment()) + 1) << 26;
526 else
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000527 Mask |= AttributeImpl::getAttrMask(Kind);
Bill Wendlingfca0ed22013-02-02 00:52:44 +0000528 }
529
530 return Mask;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000531 }
532
533 return 0;
534}
535
536//===----------------------------------------------------------------------===//
537// AttributeSet Construction and Mutation Methods
538//===----------------------------------------------------------------------===//
539
Bill Wendling8232ece2013-01-29 01:43:29 +0000540AttributeSet
541AttributeSet::getImpl(LLVMContext &C,
542 ArrayRef<std::pair<unsigned, AttributeSetNode*> > Attrs) {
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000543 LLVMContextImpl *pImpl = C.pImpl;
544 FoldingSetNodeID ID;
545 AttributeSetImpl::Profile(ID, Attrs);
546
547 void *InsertPoint;
548 AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
549
550 // If we didn't find any existing attributes of the same shape then
551 // create a new one and insert it.
552 if (!PA) {
553 PA = new AttributeSetImpl(C, Attrs);
554 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
555 }
556
557 // Return the AttributesList that we found or created.
558 return AttributeSet(PA);
559}
560
561AttributeSet AttributeSet::get(LLVMContext &C,
562 ArrayRef<std::pair<unsigned, Attribute> > Attrs){
563 // If there are no attributes then return a null AttributesList pointer.
564 if (Attrs.empty())
565 return AttributeSet();
566
567#ifndef NDEBUG
568 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
569 assert((!i || Attrs[i-1].first <= Attrs[i].first) &&
570 "Misordered Attributes list!");
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000571 assert(!Attrs[i].second.hasAttribute(Attribute::None) &&
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000572 "Pointless attribute!");
573 }
574#endif
575
576 // Create a vector if (unsigned, AttributeSetNode*) pairs from the attributes
577 // list.
578 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrPairVec;
579 for (ArrayRef<std::pair<unsigned, Attribute> >::iterator I = Attrs.begin(),
580 E = Attrs.end(); I != E; ) {
581 unsigned Index = I->first;
582 SmallVector<Attribute, 4> AttrVec;
NAKAMURA Takumi3ba51ce2013-01-29 15:18:16 +0000583 while (I != E && I->first == Index) {
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000584 AttrVec.push_back(I->second);
585 ++I;
586 }
587
588 AttrPairVec.push_back(std::make_pair(Index,
589 AttributeSetNode::get(C, AttrVec)));
590 }
591
592 return getImpl(C, AttrPairVec);
593}
594
595AttributeSet AttributeSet::get(LLVMContext &C,
596 ArrayRef<std::pair<unsigned,
597 AttributeSetNode*> > Attrs) {
598 // If there are no attributes then return a null AttributesList pointer.
599 if (Attrs.empty())
600 return AttributeSet();
601
602 return getImpl(C, Attrs);
603}
604
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000605AttributeSet AttributeSet::get(LLVMContext &C, unsigned Index, AttrBuilder &B) {
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000606 if (!B.hasAttributes())
607 return AttributeSet();
Bill Wendling8fbc0c22013-01-29 01:02:03 +0000608
Bill Wendling64754f42013-02-05 23:48:36 +0000609 // Add target-independent attributes.
Bill Wendling8fbc0c22013-01-29 01:02:03 +0000610 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
Benjamin Kramerc835b8c2013-02-16 19:13:18 +0000611 for (Attribute::AttrKind Kind = Attribute::None;
Benjamin Kramer7c146122013-02-16 19:22:28 +0000612 Kind != Attribute::EndAttrKinds; Kind = Attribute::AttrKind(Kind + 1)) {
Benjamin Kramerc835b8c2013-02-16 19:13:18 +0000613 if (!B.contains(Kind))
614 continue;
615
Bill Wendling8fbc0c22013-01-29 01:02:03 +0000616 if (Kind == Attribute::Alignment)
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000617 Attrs.push_back(std::make_pair(Index, Attribute::
Bill Wendling8fbc0c22013-01-29 01:02:03 +0000618 getWithAlignment(C, B.getAlignment())));
619 else if (Kind == Attribute::StackAlignment)
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000620 Attrs.push_back(std::make_pair(Index, Attribute::
Bill Wendling8fbc0c22013-01-29 01:02:03 +0000621 getWithStackAlignment(C, B.getStackAlignment())));
622 else
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000623 Attrs.push_back(std::make_pair(Index, Attribute::get(C, Kind)));
Bill Wendling8fbc0c22013-01-29 01:02:03 +0000624 }
625
Bill Wendling64754f42013-02-05 23:48:36 +0000626 // Add target-dependent (string) attributes.
627 for (AttrBuilder::td_iterator I = B.td_begin(), E = B.td_end();
628 I != E; ++I)
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000629 Attrs.push_back(std::make_pair(Index, Attribute::get(C, I->first,I->second)));
Bill Wendling64754f42013-02-05 23:48:36 +0000630
Bill Wendling8fbc0c22013-01-29 01:02:03 +0000631 return get(C, Attrs);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000632}
633
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000634AttributeSet AttributeSet::get(LLVMContext &C, unsigned Index,
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000635 ArrayRef<Attribute::AttrKind> Kind) {
636 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
637 for (ArrayRef<Attribute::AttrKind>::iterator I = Kind.begin(),
638 E = Kind.end(); I != E; ++I)
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000639 Attrs.push_back(std::make_pair(Index, Attribute::get(C, *I)));
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000640 return get(C, Attrs);
641}
642
643AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<AttributeSet> Attrs) {
644 if (Attrs.empty()) return AttributeSet();
645
646 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrNodeVec;
647 for (unsigned I = 0, E = Attrs.size(); I != E; ++I) {
648 AttributeSet AS = Attrs[I];
649 if (!AS.pImpl) continue;
650 AttrNodeVec.append(AS.pImpl->AttrNodes.begin(), AS.pImpl->AttrNodes.end());
651 }
652
653 return getImpl(C, AttrNodeVec);
654}
655
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000656AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000657 Attribute::AttrKind Attr) const {
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000658 if (hasAttribute(Index, Attr)) return *this;
659 return addAttributes(C, Index, AttributeSet::get(C, Index, Attr));
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000660}
661
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000662AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
Reed Kotler9106f732013-03-13 20:20:08 +0000663 StringRef Kind) const {
664 llvm::AttrBuilder B;
665 B.addAttribute(Kind);
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000666 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
Reed Kotler9106f732013-03-13 20:20:08 +0000667}
668
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000669AttributeSet AttributeSet::addAttributes(LLVMContext &C, unsigned Index,
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000670 AttributeSet Attrs) const {
671 if (!pImpl) return Attrs;
672 if (!Attrs.pImpl) return *this;
673
674#ifndef NDEBUG
675 // FIXME it is not obvious how this should work for alignment. For now, say
676 // we can't change a known alignment.
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000677 unsigned OldAlign = getParamAlignment(Index);
678 unsigned NewAlign = Attrs.getParamAlignment(Index);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000679 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
680 "Attempt to change alignment!");
681#endif
682
683 // Add the attribute slots before the one we're trying to add.
684 SmallVector<AttributeSet, 4> AttrSet;
685 uint64_t NumAttrs = pImpl->getNumAttributes();
686 AttributeSet AS;
687 uint64_t LastIndex = 0;
688 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000689 if (getSlotIndex(I) >= Index) {
690 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000691 break;
692 }
693 LastIndex = I + 1;
694 AttrSet.push_back(getSlotAttributes(I));
695 }
696
697 // Now add the attribute into the correct slot. There may already be an
698 // AttributeSet there.
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000699 AttrBuilder B(AS, Index);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000700
701 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000702 if (Attrs.getSlotIndex(I) == Index) {
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000703 for (AttributeSetImpl::const_iterator II = Attrs.pImpl->begin(I),
704 IE = Attrs.pImpl->end(I); II != IE; ++II)
Bill Wendling39da0782013-01-31 23:38:01 +0000705 B.addAttribute(*II);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000706 break;
707 }
708
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000709 AttrSet.push_back(AttributeSet::get(C, Index, B));
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000710
711 // Add the remaining attribute slots.
712 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
713 AttrSet.push_back(getSlotAttributes(I));
714
715 return get(C, AttrSet);
716}
717
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000718AttributeSet AttributeSet::removeAttribute(LLVMContext &C, unsigned Index,
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000719 Attribute::AttrKind Attr) const {
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000720 if (!hasAttribute(Index, Attr)) return *this;
721 return removeAttributes(C, Index, AttributeSet::get(C, Index, Attr));
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000722}
723
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000724AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Index,
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000725 AttributeSet Attrs) const {
726 if (!pImpl) return AttributeSet();
727 if (!Attrs.pImpl) return *this;
728
729#ifndef NDEBUG
730 // FIXME it is not obvious how this should work for alignment.
731 // For now, say we can't pass in alignment, which no current use does.
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000732 assert(!Attrs.hasAttribute(Index, Attribute::Alignment) &&
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000733 "Attempt to change alignment!");
734#endif
735
736 // Add the attribute slots before the one we're trying to add.
737 SmallVector<AttributeSet, 4> AttrSet;
738 uint64_t NumAttrs = pImpl->getNumAttributes();
739 AttributeSet AS;
740 uint64_t LastIndex = 0;
741 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000742 if (getSlotIndex(I) >= Index) {
743 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000744 break;
745 }
746 LastIndex = I + 1;
747 AttrSet.push_back(getSlotAttributes(I));
748 }
749
Bill Wendlinge7436542013-01-30 23:07:40 +0000750 // Now remove the attribute from the correct slot. There may already be an
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000751 // AttributeSet there.
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000752 AttrBuilder B(AS, Index);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000753
754 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000755 if (Attrs.getSlotIndex(I) == Index) {
756 B.removeAttributes(Attrs.pImpl->getSlotAttributes(I), Index);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000757 break;
758 }
759
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000760 AttrSet.push_back(AttributeSet::get(C, Index, B));
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000761
762 // Add the remaining attribute slots.
763 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
764 AttrSet.push_back(getSlotAttributes(I));
765
766 return get(C, AttrSet);
767}
768
769//===----------------------------------------------------------------------===//
770// AttributeSet Accessor Methods
771//===----------------------------------------------------------------------===//
772
Bill Wendling85b3fbe2013-02-10 05:00:40 +0000773LLVMContext &AttributeSet::getContext() const {
774 return pImpl->getContext();
775}
776
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000777AttributeSet AttributeSet::getParamAttributes(unsigned Index) const {
778 return pImpl && hasAttributes(Index) ?
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000779 AttributeSet::get(pImpl->getContext(),
Bill Wendling606c8e32013-01-29 03:20:31 +0000780 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000781 std::make_pair(Index, getAttributes(Index)))) :
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000782 AttributeSet();
783}
784
785AttributeSet AttributeSet::getRetAttributes() const {
786 return pImpl && hasAttributes(ReturnIndex) ?
787 AttributeSet::get(pImpl->getContext(),
Bill Wendling606c8e32013-01-29 03:20:31 +0000788 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000789 std::make_pair(ReturnIndex,
790 getAttributes(ReturnIndex)))) :
791 AttributeSet();
792}
793
794AttributeSet AttributeSet::getFnAttributes() const {
795 return pImpl && hasAttributes(FunctionIndex) ?
796 AttributeSet::get(pImpl->getContext(),
Bill Wendling606c8e32013-01-29 03:20:31 +0000797 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000798 std::make_pair(FunctionIndex,
799 getAttributes(FunctionIndex)))) :
800 AttributeSet();
801}
802
803bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{
Bill Wendling606c8e32013-01-29 03:20:31 +0000804 AttributeSetNode *ASN = getAttributes(Index);
805 return ASN ? ASN->hasAttribute(Kind) : false;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000806}
807
Bill Wendling0e9d5d02013-02-13 08:42:21 +0000808bool AttributeSet::hasAttribute(unsigned Index, StringRef Kind) const {
809 AttributeSetNode *ASN = getAttributes(Index);
810 return ASN ? ASN->hasAttribute(Kind) : false;
811}
812
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000813bool AttributeSet::hasAttributes(unsigned Index) const {
Bill Wendling606c8e32013-01-29 03:20:31 +0000814 AttributeSetNode *ASN = getAttributes(Index);
815 return ASN ? ASN->hasAttributes() : false;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000816}
817
818/// \brief Return true if the specified attribute is set for at least one
819/// parameter or for the return value.
820bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr) const {
821 if (pImpl == 0) return false;
822
823 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
824 for (AttributeSetImpl::const_iterator II = pImpl->begin(I),
825 IE = pImpl->end(I); II != IE; ++II)
NAKAMURA Takumieddab152013-01-31 03:47:28 +0000826 if (II->hasAttribute(Attr))
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000827 return true;
828
829 return false;
830}
831
Bill Wendling0e9d5d02013-02-13 08:42:21 +0000832Attribute AttributeSet::getAttribute(unsigned Index,
833 Attribute::AttrKind Kind) const {
834 AttributeSetNode *ASN = getAttributes(Index);
835 return ASN ? ASN->getAttribute(Kind) : Attribute();
836}
837
838Attribute AttributeSet::getAttribute(unsigned Index,
839 StringRef Kind) const {
840 AttributeSetNode *ASN = getAttributes(Index);
841 return ASN ? ASN->getAttribute(Kind) : Attribute();
842}
843
Bill Wendling606c8e32013-01-29 03:20:31 +0000844unsigned AttributeSet::getParamAlignment(unsigned Index) const {
845 AttributeSetNode *ASN = getAttributes(Index);
846 return ASN ? ASN->getAlignment() : 0;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000847}
848
849unsigned AttributeSet::getStackAlignment(unsigned Index) const {
Bill Wendling606c8e32013-01-29 03:20:31 +0000850 AttributeSetNode *ASN = getAttributes(Index);
851 return ASN ? ASN->getStackAlignment() : 0;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000852}
853
Rafael Espindolaaae02982013-05-01 13:07:03 +0000854std::string AttributeSet::getAsString(unsigned Index,
Bill Wendlingb29ce262013-02-11 08:43:33 +0000855 bool InAttrGrp) const {
Bill Wendling606c8e32013-01-29 03:20:31 +0000856 AttributeSetNode *ASN = getAttributes(Index);
Rafael Espindolaaae02982013-05-01 13:07:03 +0000857 return ASN ? ASN->getAsString(InAttrGrp) : std::string("");
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000858}
859
860/// \brief The attributes for the specified index are returned.
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000861AttributeSetNode *AttributeSet::getAttributes(unsigned Index) const {
Bill Wendling606c8e32013-01-29 03:20:31 +0000862 if (!pImpl) return 0;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000863
Bill Wendling606c8e32013-01-29 03:20:31 +0000864 // Loop through to find the attribute node we want.
865 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000866 if (pImpl->getSlotIndex(I) == Index)
Bill Wendling606c8e32013-01-29 03:20:31 +0000867 return pImpl->getSlotNode(I);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000868
Bill Wendling606c8e32013-01-29 03:20:31 +0000869 return 0;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000870}
871
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000872AttributeSet::iterator AttributeSet::begin(unsigned Slot) const {
Bill Wendling16c4b3c2013-01-31 23:53:05 +0000873 if (!pImpl)
874 return ArrayRef<Attribute>().begin();
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000875 return pImpl->begin(Slot);
Bill Wendling16c4b3c2013-01-31 23:53:05 +0000876}
877
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000878AttributeSet::iterator AttributeSet::end(unsigned Slot) const {
Bill Wendling16c4b3c2013-01-31 23:53:05 +0000879 if (!pImpl)
880 return ArrayRef<Attribute>().end();
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000881 return pImpl->end(Slot);
Bill Wendling16c4b3c2013-01-31 23:53:05 +0000882}
883
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000884//===----------------------------------------------------------------------===//
885// AttributeSet Introspection Methods
886//===----------------------------------------------------------------------===//
887
888/// \brief Return the number of slots used in this attribute list. This is the
889/// number of arguments that have an attribute set on them (including the
890/// function itself).
891unsigned AttributeSet::getNumSlots() const {
892 return pImpl ? pImpl->getNumAttributes() : 0;
893}
894
Rafael Espindola76f103e2013-04-30 16:53:38 +0000895unsigned AttributeSet::getSlotIndex(unsigned Slot) const {
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000896 assert(pImpl && Slot < pImpl->getNumAttributes() &&
897 "Slot # out of range!");
898 return pImpl->getSlotIndex(Slot);
899}
900
901AttributeSet AttributeSet::getSlotAttributes(unsigned Slot) const {
902 assert(pImpl && Slot < pImpl->getNumAttributes() &&
903 "Slot # out of range!");
904 return pImpl->getSlotAttributes(Slot);
905}
906
907uint64_t AttributeSet::Raw(unsigned Index) const {
908 // FIXME: Remove this.
909 return pImpl ? pImpl->Raw(Index) : 0;
910}
911
912void AttributeSet::dump() const {
913 dbgs() << "PAL[\n";
914
915 for (unsigned i = 0, e = getNumSlots(); i < e; ++i) {
916 uint64_t Index = getSlotIndex(i);
917 dbgs() << " { ";
918 if (Index == ~0U)
919 dbgs() << "~0U";
920 else
921 dbgs() << Index;
922 dbgs() << " => " << getAsString(Index) << " }\n";
923 }
924
925 dbgs() << "]\n";
926}
927
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000928//===----------------------------------------------------------------------===//
Bill Wendling03198882013-01-04 23:27:34 +0000929// AttrBuilder Method Implementations
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000930//===----------------------------------------------------------------------===//
931
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000932AttrBuilder::AttrBuilder(AttributeSet AS, unsigned Index)
Benjamin Kramerc835b8c2013-02-16 19:13:18 +0000933 : Attrs(0), Alignment(0), StackAlignment(0) {
Bill Wendlingec258982013-01-27 21:23:46 +0000934 AttributeSetImpl *pImpl = AS.pImpl;
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000935 if (!pImpl) return;
936
Bill Wendling73bc4522013-01-28 00:21:34 +0000937 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I) {
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000938 if (pImpl->getSlotIndex(I) != Index) continue;
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000939
Bill Wendling383da6b2013-01-30 21:22:59 +0000940 for (AttributeSetImpl::const_iterator II = pImpl->begin(I),
Bill Wendling73bc4522013-01-28 00:21:34 +0000941 IE = pImpl->end(I); II != IE; ++II)
Bill Wendling39da0782013-01-31 23:38:01 +0000942 addAttribute(*II);
Bill Wendling73bc4522013-01-28 00:21:34 +0000943
944 break;
945 }
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000946}
947
Bill Wendling03198882013-01-04 23:27:34 +0000948void AttrBuilder::clear() {
Benjamin Kramer3f213e72013-02-18 12:09:51 +0000949 Attrs.reset();
Bill Wendling03198882013-01-04 23:27:34 +0000950 Alignment = StackAlignment = 0;
951}
952
953AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
Benjamin Kramer3f213e72013-02-18 12:09:51 +0000954 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
Bill Wendling169d5272013-01-31 23:16:25 +0000955 assert(Val != Attribute::Alignment && Val != Attribute::StackAlignment &&
956 "Adding alignment attribute without adding alignment value!");
Benjamin Kramer3f213e72013-02-18 12:09:51 +0000957 Attrs[Val] = true;
Bill Wendling3a106e62012-10-09 19:01:18 +0000958 return *this;
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000959}
960
Bill Wendling39da0782013-01-31 23:38:01 +0000961AttrBuilder &AttrBuilder::addAttribute(Attribute Attr) {
Bill Wendling09ed9102013-02-10 10:13:23 +0000962 if (Attr.isStringAttribute()) {
963 addAttribute(Attr.getKindAsString(), Attr.getValueAsString());
964 return *this;
965 }
966
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000967 Attribute::AttrKind Kind = Attr.getKindAsEnum();
Benjamin Kramer3f213e72013-02-18 12:09:51 +0000968 Attrs[Kind] = true;
Bill Wendling49f60602013-01-28 05:23:28 +0000969
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000970 if (Kind == Attribute::Alignment)
Bill Wendling49f60602013-01-28 05:23:28 +0000971 Alignment = Attr.getAlignment();
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000972 else if (Kind == Attribute::StackAlignment)
Bill Wendling49f60602013-01-28 05:23:28 +0000973 StackAlignment = Attr.getStackAlignment();
974 return *this;
975}
976
Bill Wendlingea59f892013-02-05 08:09:32 +0000977AttrBuilder &AttrBuilder::addAttribute(StringRef A, StringRef V) {
978 TargetDepAttrs[A] = V;
979 return *this;
980}
981
Bill Wendling39da0782013-01-31 23:38:01 +0000982AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
Benjamin Kramer3f213e72013-02-18 12:09:51 +0000983 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
984 Attrs[Val] = false;
Bill Wendling39da0782013-01-31 23:38:01 +0000985
986 if (Val == Attribute::Alignment)
987 Alignment = 0;
988 else if (Val == Attribute::StackAlignment)
989 StackAlignment = 0;
990
991 return *this;
992}
993
Bill Wendlinge7436542013-01-30 23:07:40 +0000994AttrBuilder &AttrBuilder::removeAttributes(AttributeSet A, uint64_t Index) {
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000995 unsigned Slot = ~0U;
Bill Wendling30d2c762013-02-01 00:13:50 +0000996 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
997 if (A.getSlotIndex(I) == Index) {
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000998 Slot = I;
Bill Wendling30d2c762013-02-01 00:13:50 +0000999 break;
Bill Wendling49f60602013-01-28 05:23:28 +00001000 }
Bill Wendling30d2c762013-02-01 00:13:50 +00001001
Bill Wendling8a6a7bb2013-04-18 20:17:28 +00001002 assert(Slot != ~0U && "Couldn't find index in AttributeSet!");
Bill Wendling30d2c762013-02-01 00:13:50 +00001003
Bill Wendling8a6a7bb2013-04-18 20:17:28 +00001004 for (AttributeSet::iterator I = A.begin(Slot), E = A.end(Slot); I != E; ++I) {
Bill Wendling74fe8252013-02-12 07:56:49 +00001005 Attribute Attr = *I;
1006 if (Attr.isEnumAttribute() || Attr.isAlignAttribute()) {
1007 Attribute::AttrKind Kind = I->getKindAsEnum();
Benjamin Kramer3f213e72013-02-18 12:09:51 +00001008 Attrs[Kind] = false;
Bill Wendling30d2c762013-02-01 00:13:50 +00001009
Bill Wendling74fe8252013-02-12 07:56:49 +00001010 if (Kind == Attribute::Alignment)
1011 Alignment = 0;
1012 else if (Kind == Attribute::StackAlignment)
1013 StackAlignment = 0;
1014 } else {
1015 assert(Attr.isStringAttribute() && "Invalid attribute type!");
1016 std::map<std::string, std::string>::iterator
1017 Iter = TargetDepAttrs.find(Attr.getKindAsString());
1018 if (Iter != TargetDepAttrs.end())
1019 TargetDepAttrs.erase(Iter);
1020 }
Bill Wendling49f60602013-01-28 05:23:28 +00001021 }
1022
1023 return *this;
1024}
1025
Bill Wendlingea59f892013-02-05 08:09:32 +00001026AttrBuilder &AttrBuilder::removeAttribute(StringRef A) {
1027 std::map<std::string, std::string>::iterator I = TargetDepAttrs.find(A);
1028 if (I != TargetDepAttrs.end())
1029 TargetDepAttrs.erase(I);
1030 return *this;
1031}
1032
Bill Wendling702cc912012-10-15 20:35:56 +00001033AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
Bill Wendlingda3f9d82012-10-14 03:58:29 +00001034 if (Align == 0) return *this;
Bill Wendling03198882013-01-04 23:27:34 +00001035
Bill Wendlinge66f3d32012-10-05 06:44:41 +00001036 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1037 assert(Align <= 0x40000000 && "Alignment too large.");
Bill Wendling03198882013-01-04 23:27:34 +00001038
Benjamin Kramer3f213e72013-02-18 12:09:51 +00001039 Attrs[Attribute::Alignment] = true;
Bill Wendling03198882013-01-04 23:27:34 +00001040 Alignment = Align;
Bill Wendlingda3f9d82012-10-14 03:58:29 +00001041 return *this;
Chris Lattner50ee9dd2008-01-02 23:42:30 +00001042}
1043
Bill Wendling03198882013-01-04 23:27:34 +00001044AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
1045 // Default alignment, allow the target to define how to align it.
1046 if (Align == 0) return *this;
1047
1048 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1049 assert(Align <= 0x100 && "Alignment too large.");
1050
Benjamin Kramer3f213e72013-02-18 12:09:51 +00001051 Attrs[Attribute::StackAlignment] = true;
Bill Wendling03198882013-01-04 23:27:34 +00001052 StackAlignment = Align;
1053 return *this;
1054}
1055
Bill Wendling85df6b42013-02-06 01:16:00 +00001056AttrBuilder &AttrBuilder::merge(const AttrBuilder &B) {
1057 // FIXME: What if both have alignments, but they don't match?!
1058 if (!Alignment)
1059 Alignment = B.Alignment;
1060
1061 if (!StackAlignment)
1062 StackAlignment = B.StackAlignment;
1063
Benjamin Kramerc835b8c2013-02-16 19:13:18 +00001064 Attrs |= B.Attrs;
Bill Wendling85df6b42013-02-06 01:16:00 +00001065
1066 for (td_const_iterator I = B.TargetDepAttrs.begin(),
1067 E = B.TargetDepAttrs.end(); I != E; ++I)
1068 TargetDepAttrs[I->first] = I->second;
1069
1070 return *this;
1071}
1072
Bill Wendlingc342d9d2013-02-06 01:33:42 +00001073bool AttrBuilder::contains(StringRef A) const {
1074 return TargetDepAttrs.find(A) != TargetDepAttrs.end();
1075}
1076
Bill Wendling702cc912012-10-15 20:35:56 +00001077bool AttrBuilder::hasAttributes() const {
Benjamin Kramer3f213e72013-02-18 12:09:51 +00001078 return !Attrs.none() || !TargetDepAttrs.empty();
Bill Wendlingf385f4c2012-10-08 23:27:46 +00001079}
Bill Wendling60507d52013-01-04 20:54:35 +00001080
Bill Wendlinge7436542013-01-30 23:07:40 +00001081bool AttrBuilder::hasAttributes(AttributeSet A, uint64_t Index) const {
Bill Wendling8a6a7bb2013-04-18 20:17:28 +00001082 unsigned Slot = ~0U;
Bill Wendlingbdcbccc2013-02-02 00:42:06 +00001083 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
1084 if (A.getSlotIndex(I) == Index) {
Bill Wendling8a6a7bb2013-04-18 20:17:28 +00001085 Slot = I;
Bill Wendlingbdcbccc2013-02-02 00:42:06 +00001086 break;
1087 }
1088
Bill Wendling8a6a7bb2013-04-18 20:17:28 +00001089 assert(Slot != ~0U && "Couldn't find the index!");
Bill Wendlingbdcbccc2013-02-02 00:42:06 +00001090
Bill Wendling8a6a7bb2013-04-18 20:17:28 +00001091 for (AttributeSet::iterator I = A.begin(Slot), E = A.end(Slot);
Bill Wendling74fe8252013-02-12 07:56:49 +00001092 I != E; ++I) {
1093 Attribute Attr = *I;
1094 if (Attr.isEnumAttribute() || Attr.isAlignAttribute()) {
Benjamin Kramer3f213e72013-02-18 12:09:51 +00001095 if (Attrs[I->getKindAsEnum()])
Bill Wendling74fe8252013-02-12 07:56:49 +00001096 return true;
1097 } else {
1098 assert(Attr.isStringAttribute() && "Invalid attribute kind!");
1099 return TargetDepAttrs.find(Attr.getKindAsString())!=TargetDepAttrs.end();
1100 }
1101 }
Bill Wendlingbdcbccc2013-02-02 00:42:06 +00001102
1103 return false;
Bill Wendling8831c062012-10-09 00:01:21 +00001104}
Bill Wendling60507d52013-01-04 20:54:35 +00001105
Bill Wendling702cc912012-10-15 20:35:56 +00001106bool AttrBuilder::hasAlignmentAttr() const {
Bill Wendling03198882013-01-04 23:27:34 +00001107 return Alignment != 0;
Bill Wendlingf385f4c2012-10-08 23:27:46 +00001108}
1109
Bill Wendlingc22f4aa2013-01-29 00:34:06 +00001110bool AttrBuilder::operator==(const AttrBuilder &B) {
Benjamin Kramerc835b8c2013-02-16 19:13:18 +00001111 if (Attrs != B.Attrs)
1112 return false;
Bill Wendlingc342d9d2013-02-06 01:33:42 +00001113
1114 for (td_const_iterator I = TargetDepAttrs.begin(),
1115 E = TargetDepAttrs.end(); I != E; ++I)
1116 if (B.TargetDepAttrs.find(I->first) == B.TargetDepAttrs.end())
1117 return false;
1118
1119 return Alignment == B.Alignment && StackAlignment == B.StackAlignment;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +00001120}
1121
1122AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) {
Bill Wendlingf9271ea2013-02-04 23:32:23 +00001123 // FIXME: Remove this in 4.0.
Bill Wendling8232ece2013-01-29 01:43:29 +00001124 if (!Val) return *this;
1125
Bill Wendlingc22f4aa2013-01-29 00:34:06 +00001126 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
1127 I = Attribute::AttrKind(I + 1)) {
1128 if (uint64_t A = (Val & AttributeImpl::getAttrMask(I))) {
Benjamin Kramer3f213e72013-02-18 12:09:51 +00001129 Attrs[I] = true;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +00001130
1131 if (I == Attribute::Alignment)
1132 Alignment = 1ULL << ((A >> 16) - 1);
1133 else if (I == Attribute::StackAlignment)
1134 StackAlignment = 1ULL << ((A >> 26)-1);
1135 }
1136 }
1137
1138 return *this;
1139}
1140
Bill Wendling8e47daf2013-01-25 23:09:36 +00001141//===----------------------------------------------------------------------===//
1142// AttributeFuncs Function Defintions
1143//===----------------------------------------------------------------------===//
1144
Bill Wendling7beee282013-02-01 01:04:27 +00001145/// \brief Which attributes cannot be applied to a type.
Bill Wendlinge7436542013-01-30 23:07:40 +00001146AttributeSet AttributeFuncs::typeIncompatible(Type *Ty, uint64_t Index) {
Bill Wendling8e47daf2013-01-25 23:09:36 +00001147 AttrBuilder Incompatible;
1148
1149 if (!Ty->isIntegerTy())
1150 // Attribute that only apply to integers.
1151 Incompatible.addAttribute(Attribute::SExt)
1152 .addAttribute(Attribute::ZExt);
1153
1154 if (!Ty->isPointerTy())
1155 // Attribute that only apply to pointers.
1156 Incompatible.addAttribute(Attribute::ByVal)
1157 .addAttribute(Attribute::Nest)
1158 .addAttribute(Attribute::NoAlias)
1159 .addAttribute(Attribute::NoCapture)
1160 .addAttribute(Attribute::StructRet);
1161
Bill Wendlinge7436542013-01-30 23:07:40 +00001162 return AttributeSet::get(Ty->getContext(), Index, Incompatible);
Bill Wendling8e47daf2013-01-25 23:09:36 +00001163}