blob: 80968dbd1f7ec02ed582f6b58cd37e18b751646d [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";
160 if (hasAttribute(Attribute::ByVal))
161 return "byval";
162 if (hasAttribute(Attribute::InlineHint))
163 return "inlinehint";
Bill Wendling034b94b2012-12-19 07:18:57 +0000164 if (hasAttribute(Attribute::InReg))
Bill Wendling606c8e32013-01-29 03:20:31 +0000165 return "inreg";
Bill Wendling14292a62013-01-31 20:59:05 +0000166 if (hasAttribute(Attribute::MinSize))
167 return "minsize";
168 if (hasAttribute(Attribute::Naked))
169 return "naked";
170 if (hasAttribute(Attribute::Nest))
171 return "nest";
Bill Wendling034b94b2012-12-19 07:18:57 +0000172 if (hasAttribute(Attribute::NoAlias))
Bill Wendling606c8e32013-01-29 03:20:31 +0000173 return "noalias";
Bill Wendling143d4642013-02-22 00:12:35 +0000174 if (hasAttribute(Attribute::NoBuiltin))
175 return "nobuiltin";
Bill Wendling034b94b2012-12-19 07:18:57 +0000176 if (hasAttribute(Attribute::NoCapture))
Bill Wendling606c8e32013-01-29 03:20:31 +0000177 return "nocapture";
Bill Wendling14292a62013-01-31 20:59:05 +0000178 if (hasAttribute(Attribute::NoDuplicate))
179 return "noduplicate";
180 if (hasAttribute(Attribute::NoImplicitFloat))
181 return "noimplicitfloat";
182 if (hasAttribute(Attribute::NoInline))
183 return "noinline";
184 if (hasAttribute(Attribute::NonLazyBind))
185 return "nonlazybind";
186 if (hasAttribute(Attribute::NoRedZone))
187 return "noredzone";
188 if (hasAttribute(Attribute::NoReturn))
189 return "noreturn";
190 if (hasAttribute(Attribute::NoUnwind))
191 return "nounwind";
192 if (hasAttribute(Attribute::OptimizeForSize))
193 return "optsize";
Bill Wendling034b94b2012-12-19 07:18:57 +0000194 if (hasAttribute(Attribute::ReadNone))
Bill Wendling606c8e32013-01-29 03:20:31 +0000195 return "readnone";
Bill Wendling034b94b2012-12-19 07:18:57 +0000196 if (hasAttribute(Attribute::ReadOnly))
Bill Wendling606c8e32013-01-29 03:20:31 +0000197 return "readonly";
Stephen Lin456ca042013-04-20 05:14:40 +0000198 if (hasAttribute(Attribute::Returned))
199 return "returned";
Bill Wendling14292a62013-01-31 20:59:05 +0000200 if (hasAttribute(Attribute::ReturnsTwice))
201 return "returns_twice";
202 if (hasAttribute(Attribute::SExt))
203 return "signext";
Bill Wendling034b94b2012-12-19 07:18:57 +0000204 if (hasAttribute(Attribute::StackProtect))
Bill Wendling606c8e32013-01-29 03:20:31 +0000205 return "ssp";
Bill Wendling034b94b2012-12-19 07:18:57 +0000206 if (hasAttribute(Attribute::StackProtectReq))
Bill Wendling606c8e32013-01-29 03:20:31 +0000207 return "sspreq";
Bill Wendling114baee2013-01-23 06:41:41 +0000208 if (hasAttribute(Attribute::StackProtectStrong))
Bill Wendling606c8e32013-01-29 03:20:31 +0000209 return "sspstrong";
Bill Wendling14292a62013-01-31 20:59:05 +0000210 if (hasAttribute(Attribute::StructRet))
211 return "sret";
Kostya Serebryany8eec41f2013-02-26 06:58:09 +0000212 if (hasAttribute(Attribute::SanitizeThread))
213 return "sanitize_thread";
214 if (hasAttribute(Attribute::SanitizeMemory))
215 return "sanitize_memory";
Bill Wendling14292a62013-01-31 20:59:05 +0000216 if (hasAttribute(Attribute::UWTable))
217 return "uwtable";
218 if (hasAttribute(Attribute::ZExt))
219 return "zeroext";
220
221 // FIXME: These should be output like this:
222 //
223 // align=4
224 // alignstack=8
225 //
Bill Wendling034b94b2012-12-19 07:18:57 +0000226 if (hasAttribute(Attribute::Alignment)) {
Bill Wendling606c8e32013-01-29 03:20:31 +0000227 std::string Result;
Bill Wendlingb29ce262013-02-11 08:43:33 +0000228 Result += "align";
229 Result += (InAttrGrp) ? "=" : " ";
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000230 Result += utostr(getValueAsInt());
231 return Result;
232 }
Bill Wendlingb29ce262013-02-11 08:43:33 +0000233
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000234 if (hasAttribute(Attribute::StackAlignment)) {
235 std::string Result;
Bill Wendlingb29ce262013-02-11 08:43:33 +0000236 Result += "alignstack";
237 if (InAttrGrp) {
238 Result += "=";
239 Result += utostr(getValueAsInt());
240 } else {
241 Result += "(";
242 Result += utostr(getValueAsInt());
243 Result += ")";
244 }
Bill Wendling606c8e32013-01-29 03:20:31 +0000245 return Result;
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000246 }
Bill Wendling14292a62013-01-31 20:59:05 +0000247
248 // Convert target-dependent attributes to strings of the form:
249 //
250 // "kind"
251 // "kind" = "value"
Bill Wendling14292a62013-01-31 20:59:05 +0000252 //
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000253 if (isStringAttribute()) {
Bill Wendling14292a62013-01-31 20:59:05 +0000254 std::string Result;
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000255 Result += '\"' + getKindAsString().str() + '"';
Bill Wendling14292a62013-01-31 20:59:05 +0000256
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000257 StringRef Val = pImpl->getValueAsString();
258 if (Val.empty()) return Result;
Bill Wendling5a4041e2013-02-01 22:32:30 +0000259
Bill Wendlingb29ce262013-02-11 08:43:33 +0000260 Result += "=\"" + Val.str() + '"';
Bill Wendling7beee282013-02-01 01:04:27 +0000261 return Result;
Bill Wendling14292a62013-01-31 20:59:05 +0000262 }
Bill Wendling606c8e32013-01-29 03:20:31 +0000263
264 llvm_unreachable("Unknown attribute");
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000265}
266
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000267bool Attribute::operator<(Attribute A) const {
268 if (!pImpl && !A.pImpl) return false;
269 if (!pImpl) return true;
270 if (!A.pImpl) return false;
271 return *pImpl < *A.pImpl;
272}
273
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000274//===----------------------------------------------------------------------===//
275// AttributeImpl Definition
276//===----------------------------------------------------------------------===//
277
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000278AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind Kind)
279 : Context(C), Entry(new EnumAttributeEntry(Kind)) {}
280
281AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind Kind,
282 unsigned Align)
283 : Context(C) {
284 assert((Kind == Attribute::Alignment || Kind == Attribute::StackAlignment) &&
285 "Wrong kind for alignment attribute!");
286 Entry = new AlignAttributeEntry(Kind, Align);
287}
288
289AttributeImpl::AttributeImpl(LLVMContext &C, StringRef Kind, StringRef Val)
290 : Context(C), Entry(new StringAttributeEntry(Kind, Val)) {}
291
292AttributeImpl::~AttributeImpl() {
293 delete Entry;
294}
295
296bool AttributeImpl::isEnumAttribute() const {
297 return isa<EnumAttributeEntry>(Entry);
298}
299
300bool AttributeImpl::isAlignAttribute() const {
301 return isa<AlignAttributeEntry>(Entry);
302}
303
304bool AttributeImpl::isStringAttribute() const {
305 return isa<StringAttributeEntry>(Entry);
306}
307
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000308bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000309 if (isStringAttribute()) return false;
310 return getKindAsEnum() == A;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000311}
312
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000313bool AttributeImpl::hasAttribute(StringRef Kind) const {
314 if (!isStringAttribute()) return false;
315 return getKindAsString() == Kind;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000316}
317
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000318Attribute::AttrKind AttributeImpl::getKindAsEnum() const {
319 if (EnumAttributeEntry *E = dyn_cast<EnumAttributeEntry>(Entry))
320 return E->getEnumKind();
321 return cast<AlignAttributeEntry>(Entry)->getEnumKind();
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000322}
323
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000324uint64_t AttributeImpl::getValueAsInt() const {
325 return cast<AlignAttributeEntry>(Entry)->getAlignment();
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000326}
327
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000328StringRef AttributeImpl::getKindAsString() const {
329 return cast<StringAttributeEntry>(Entry)->getStringKind();
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000330}
331
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000332StringRef AttributeImpl::getValueAsString() const {
333 return cast<StringAttributeEntry>(Entry)->getStringValue();
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000334}
335
336bool AttributeImpl::operator<(const AttributeImpl &AI) const {
Bill Wendling7beee282013-02-01 01:04:27 +0000337 // This sorts the attributes with Attribute::AttrKinds coming first (sorted
338 // relative to their enum value) and then strings.
Bill Wendling94328f42013-02-15 05:25:26 +0000339 if (isEnumAttribute()) {
340 if (AI.isEnumAttribute()) return getKindAsEnum() < AI.getKindAsEnum();
341 if (AI.isAlignAttribute()) return true;
342 if (AI.isStringAttribute()) return true;
343 }
Bill Wendling7beee282013-02-01 01:04:27 +0000344
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000345 if (isAlignAttribute()) {
Bill Wendling94328f42013-02-15 05:25:26 +0000346 if (AI.isEnumAttribute()) return false;
347 if (AI.isAlignAttribute()) return getValueAsInt() < AI.getValueAsInt();
348 if (AI.isStringAttribute()) return true;
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000349 }
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000350
Bill Wendling94328f42013-02-15 05:25:26 +0000351 if (AI.isEnumAttribute()) return false;
352 if (AI.isAlignAttribute()) return false;
353 if (getKindAsString() == AI.getKindAsString())
354 return getValueAsString() < AI.getValueAsString();
355 return getKindAsString() < AI.getKindAsString();
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000356}
357
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000358uint64_t AttributeImpl::getAttrMask(Attribute::AttrKind Val) {
359 // FIXME: Remove this.
360 switch (Val) {
361 case Attribute::EndAttrKinds:
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000362 llvm_unreachable("Synthetic enumerators which should never get here");
363
364 case Attribute::None: return 0;
365 case Attribute::ZExt: return 1 << 0;
366 case Attribute::SExt: return 1 << 1;
367 case Attribute::NoReturn: return 1 << 2;
368 case Attribute::InReg: return 1 << 3;
369 case Attribute::StructRet: return 1 << 4;
370 case Attribute::NoUnwind: return 1 << 5;
371 case Attribute::NoAlias: return 1 << 6;
372 case Attribute::ByVal: return 1 << 7;
373 case Attribute::Nest: return 1 << 8;
374 case Attribute::ReadNone: return 1 << 9;
375 case Attribute::ReadOnly: return 1 << 10;
376 case Attribute::NoInline: return 1 << 11;
377 case Attribute::AlwaysInline: return 1 << 12;
378 case Attribute::OptimizeForSize: return 1 << 13;
379 case Attribute::StackProtect: return 1 << 14;
380 case Attribute::StackProtectReq: return 1 << 15;
381 case Attribute::Alignment: return 31 << 16;
382 case Attribute::NoCapture: return 1 << 21;
383 case Attribute::NoRedZone: return 1 << 22;
384 case Attribute::NoImplicitFloat: return 1 << 23;
385 case Attribute::Naked: return 1 << 24;
386 case Attribute::InlineHint: return 1 << 25;
387 case Attribute::StackAlignment: return 7 << 26;
388 case Attribute::ReturnsTwice: return 1 << 29;
389 case Attribute::UWTable: return 1 << 30;
390 case Attribute::NonLazyBind: return 1U << 31;
Kostya Serebryany8eec41f2013-02-26 06:58:09 +0000391 case Attribute::SanitizeAddress: return 1ULL << 32;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000392 case Attribute::MinSize: return 1ULL << 33;
393 case Attribute::NoDuplicate: return 1ULL << 34;
394 case Attribute::StackProtectStrong: return 1ULL << 35;
Kostya Serebryany8eec41f2013-02-26 06:58:09 +0000395 case Attribute::SanitizeThread: return 1ULL << 36;
396 case Attribute::SanitizeMemory: return 1ULL << 37;
Bill Wendlingd18e0b92013-02-22 00:40:12 +0000397 case Attribute::NoBuiltin: return 1ULL << 38;
Stephen Lin456ca042013-04-20 05:14:40 +0000398 case Attribute::Returned: return 1ULL << 39;
Bill Wendlingbd2acfa2013-02-22 00:50:09 +0000399 }
400 llvm_unreachable("Unsupported attribute type");
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000401}
402
403//===----------------------------------------------------------------------===//
404// AttributeSetNode Definition
405//===----------------------------------------------------------------------===//
406
407AttributeSetNode *AttributeSetNode::get(LLVMContext &C,
408 ArrayRef<Attribute> Attrs) {
409 if (Attrs.empty())
410 return 0;
411
412 // Otherwise, build a key to look up the existing attributes.
413 LLVMContextImpl *pImpl = C.pImpl;
414 FoldingSetNodeID ID;
415
416 SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end());
Bill Wendlingf107e6c2013-02-13 09:26:26 +0000417 array_pod_sort(SortedAttrs.begin(), SortedAttrs.end());
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000418
419 for (SmallVectorImpl<Attribute>::iterator I = SortedAttrs.begin(),
420 E = SortedAttrs.end(); I != E; ++I)
421 I->Profile(ID);
422
423 void *InsertPoint;
424 AttributeSetNode *PA =
425 pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint);
426
427 // If we didn't find any existing attributes of the same shape then create a
428 // new one and insert it.
429 if (!PA) {
430 PA = new AttributeSetNode(SortedAttrs);
431 pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint);
432 }
433
434 // Return the AttributesListNode that we found or created.
435 return PA;
436}
437
Bill Wendling606c8e32013-01-29 03:20:31 +0000438bool AttributeSetNode::hasAttribute(Attribute::AttrKind Kind) const {
439 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
440 E = AttrList.end(); I != E; ++I)
NAKAMURA Takumieddab152013-01-31 03:47:28 +0000441 if (I->hasAttribute(Kind))
Bill Wendling606c8e32013-01-29 03:20:31 +0000442 return true;
443 return false;
444}
445
Bill Wendling0e9d5d02013-02-13 08:42:21 +0000446bool AttributeSetNode::hasAttribute(StringRef Kind) const {
447 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
448 E = AttrList.end(); I != E; ++I)
449 if (I->hasAttribute(Kind))
450 return true;
451 return false;
452}
453
454Attribute AttributeSetNode::getAttribute(Attribute::AttrKind Kind) const {
455 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
456 E = AttrList.end(); I != E; ++I)
457 if (I->hasAttribute(Kind))
458 return *I;
459 return Attribute();
460}
461
462Attribute AttributeSetNode::getAttribute(StringRef Kind) const {
463 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
464 E = AttrList.end(); I != E; ++I)
465 if (I->hasAttribute(Kind))
466 return *I;
467 return Attribute();
468}
469
Bill Wendling606c8e32013-01-29 03:20:31 +0000470unsigned AttributeSetNode::getAlignment() const {
471 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
472 E = AttrList.end(); I != E; ++I)
NAKAMURA Takumieddab152013-01-31 03:47:28 +0000473 if (I->hasAttribute(Attribute::Alignment))
Bill Wendling606c8e32013-01-29 03:20:31 +0000474 return I->getAlignment();
475 return 0;
476}
477
478unsigned AttributeSetNode::getStackAlignment() const {
479 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
480 E = AttrList.end(); I != E; ++I)
NAKAMURA Takumieddab152013-01-31 03:47:28 +0000481 if (I->hasAttribute(Attribute::StackAlignment))
Bill Wendling606c8e32013-01-29 03:20:31 +0000482 return I->getStackAlignment();
483 return 0;
484}
485
Bill Wendlingb1ac6e62013-04-17 23:35:59 +0000486std::string AttributeSetNode::getAsString(bool TargetIndependent,
487 bool InAttrGrp) const {
Benjamin Kramere94e4ca2013-04-19 11:43:21 +0000488 std::string Str;
Bill Wendling606c8e32013-01-29 03:20:31 +0000489 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
Benjamin Kramere94e4ca2013-04-19 11:43:21 +0000490 E = AttrList.end(); I != E; ++I) {
Bill Wendlinga88a0162013-04-18 21:45:04 +0000491 if (TargetIndependent || !I->isStringAttribute()) {
Benjamin Kramere94e4ca2013-04-19 11:43:21 +0000492 if (I != AttrList.begin())
493 Str += ' ';
Bill Wendlingb1ac6e62013-04-17 23:35:59 +0000494 Str += I->getAsString(InAttrGrp);
Bill Wendlingb1ac6e62013-04-17 23:35:59 +0000495 }
Bill Wendling606c8e32013-01-29 03:20:31 +0000496 }
497 return Str;
498}
499
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000500//===----------------------------------------------------------------------===//
501// AttributeSetImpl Definition
502//===----------------------------------------------------------------------===//
503
504uint64_t AttributeSetImpl::Raw(uint64_t Index) const {
505 for (unsigned I = 0, E = getNumAttributes(); I != E; ++I) {
506 if (getSlotIndex(I) != Index) continue;
507 const AttributeSetNode *ASN = AttrNodes[I].second;
Bill Wendlingfca0ed22013-02-02 00:52:44 +0000508 uint64_t Mask = 0;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000509
510 for (AttributeSetNode::const_iterator II = ASN->begin(),
Bill Wendlingfca0ed22013-02-02 00:52:44 +0000511 IE = ASN->end(); II != IE; ++II) {
512 Attribute Attr = *II;
Bill Wendling21536912013-02-10 23:18:05 +0000513
514 // This cannot handle string attributes.
515 if (Attr.isStringAttribute()) continue;
516
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000517 Attribute::AttrKind Kind = Attr.getKindAsEnum();
Bill Wendlingfca0ed22013-02-02 00:52:44 +0000518
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000519 if (Kind == Attribute::Alignment)
Bill Wendlingfca0ed22013-02-02 00:52:44 +0000520 Mask |= (Log2_32(ASN->getAlignment()) + 1) << 16;
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000521 else if (Kind == Attribute::StackAlignment)
Bill Wendlingfca0ed22013-02-02 00:52:44 +0000522 Mask |= (Log2_32(ASN->getStackAlignment()) + 1) << 26;
523 else
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000524 Mask |= AttributeImpl::getAttrMask(Kind);
Bill Wendlingfca0ed22013-02-02 00:52:44 +0000525 }
526
527 return Mask;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000528 }
529
530 return 0;
531}
532
533//===----------------------------------------------------------------------===//
534// AttributeSet Construction and Mutation Methods
535//===----------------------------------------------------------------------===//
536
Bill Wendling8232ece2013-01-29 01:43:29 +0000537AttributeSet
538AttributeSet::getImpl(LLVMContext &C,
539 ArrayRef<std::pair<unsigned, AttributeSetNode*> > Attrs) {
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000540 LLVMContextImpl *pImpl = C.pImpl;
541 FoldingSetNodeID ID;
542 AttributeSetImpl::Profile(ID, Attrs);
543
544 void *InsertPoint;
545 AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
546
547 // If we didn't find any existing attributes of the same shape then
548 // create a new one and insert it.
549 if (!PA) {
550 PA = new AttributeSetImpl(C, Attrs);
551 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
552 }
553
554 // Return the AttributesList that we found or created.
555 return AttributeSet(PA);
556}
557
558AttributeSet AttributeSet::get(LLVMContext &C,
559 ArrayRef<std::pair<unsigned, Attribute> > Attrs){
560 // If there are no attributes then return a null AttributesList pointer.
561 if (Attrs.empty())
562 return AttributeSet();
563
564#ifndef NDEBUG
565 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
566 assert((!i || Attrs[i-1].first <= Attrs[i].first) &&
567 "Misordered Attributes list!");
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000568 assert(!Attrs[i].second.hasAttribute(Attribute::None) &&
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000569 "Pointless attribute!");
570 }
571#endif
572
573 // Create a vector if (unsigned, AttributeSetNode*) pairs from the attributes
574 // list.
575 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrPairVec;
576 for (ArrayRef<std::pair<unsigned, Attribute> >::iterator I = Attrs.begin(),
577 E = Attrs.end(); I != E; ) {
578 unsigned Index = I->first;
579 SmallVector<Attribute, 4> AttrVec;
NAKAMURA Takumi3ba51ce2013-01-29 15:18:16 +0000580 while (I != E && I->first == Index) {
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000581 AttrVec.push_back(I->second);
582 ++I;
583 }
584
585 AttrPairVec.push_back(std::make_pair(Index,
586 AttributeSetNode::get(C, AttrVec)));
587 }
588
589 return getImpl(C, AttrPairVec);
590}
591
592AttributeSet AttributeSet::get(LLVMContext &C,
593 ArrayRef<std::pair<unsigned,
594 AttributeSetNode*> > Attrs) {
595 // If there are no attributes then return a null AttributesList pointer.
596 if (Attrs.empty())
597 return AttributeSet();
598
599 return getImpl(C, Attrs);
600}
601
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000602AttributeSet AttributeSet::get(LLVMContext &C, unsigned Index, AttrBuilder &B) {
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000603 if (!B.hasAttributes())
604 return AttributeSet();
Bill Wendling8fbc0c22013-01-29 01:02:03 +0000605
Bill Wendling64754f42013-02-05 23:48:36 +0000606 // Add target-independent attributes.
Bill Wendling8fbc0c22013-01-29 01:02:03 +0000607 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
Benjamin Kramerc835b8c2013-02-16 19:13:18 +0000608 for (Attribute::AttrKind Kind = Attribute::None;
Benjamin Kramer7c146122013-02-16 19:22:28 +0000609 Kind != Attribute::EndAttrKinds; Kind = Attribute::AttrKind(Kind + 1)) {
Benjamin Kramerc835b8c2013-02-16 19:13:18 +0000610 if (!B.contains(Kind))
611 continue;
612
Bill Wendling8fbc0c22013-01-29 01:02:03 +0000613 if (Kind == Attribute::Alignment)
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000614 Attrs.push_back(std::make_pair(Index, Attribute::
Bill Wendling8fbc0c22013-01-29 01:02:03 +0000615 getWithAlignment(C, B.getAlignment())));
616 else if (Kind == Attribute::StackAlignment)
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000617 Attrs.push_back(std::make_pair(Index, Attribute::
Bill Wendling8fbc0c22013-01-29 01:02:03 +0000618 getWithStackAlignment(C, B.getStackAlignment())));
619 else
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000620 Attrs.push_back(std::make_pair(Index, Attribute::get(C, Kind)));
Bill Wendling8fbc0c22013-01-29 01:02:03 +0000621 }
622
Bill Wendling64754f42013-02-05 23:48:36 +0000623 // Add target-dependent (string) attributes.
624 for (AttrBuilder::td_iterator I = B.td_begin(), E = B.td_end();
625 I != E; ++I)
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000626 Attrs.push_back(std::make_pair(Index, Attribute::get(C, I->first,I->second)));
Bill Wendling64754f42013-02-05 23:48:36 +0000627
Bill Wendling8fbc0c22013-01-29 01:02:03 +0000628 return get(C, Attrs);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000629}
630
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000631AttributeSet AttributeSet::get(LLVMContext &C, unsigned Index,
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000632 ArrayRef<Attribute::AttrKind> Kind) {
633 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
634 for (ArrayRef<Attribute::AttrKind>::iterator I = Kind.begin(),
635 E = Kind.end(); I != E; ++I)
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000636 Attrs.push_back(std::make_pair(Index, Attribute::get(C, *I)));
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000637 return get(C, Attrs);
638}
639
640AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<AttributeSet> Attrs) {
641 if (Attrs.empty()) return AttributeSet();
642
643 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrNodeVec;
644 for (unsigned I = 0, E = Attrs.size(); I != E; ++I) {
645 AttributeSet AS = Attrs[I];
646 if (!AS.pImpl) continue;
647 AttrNodeVec.append(AS.pImpl->AttrNodes.begin(), AS.pImpl->AttrNodes.end());
648 }
649
650 return getImpl(C, AttrNodeVec);
651}
652
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000653AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000654 Attribute::AttrKind Attr) const {
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000655 if (hasAttribute(Index, Attr)) return *this;
656 return addAttributes(C, Index, AttributeSet::get(C, Index, Attr));
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000657}
658
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000659AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
Reed Kotler9106f732013-03-13 20:20:08 +0000660 StringRef Kind) const {
661 llvm::AttrBuilder B;
662 B.addAttribute(Kind);
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000663 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
Reed Kotler9106f732013-03-13 20:20:08 +0000664}
665
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000666AttributeSet AttributeSet::addAttributes(LLVMContext &C, unsigned Index,
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000667 AttributeSet Attrs) const {
668 if (!pImpl) return Attrs;
669 if (!Attrs.pImpl) return *this;
670
671#ifndef NDEBUG
672 // FIXME it is not obvious how this should work for alignment. For now, say
673 // we can't change a known alignment.
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000674 unsigned OldAlign = getParamAlignment(Index);
675 unsigned NewAlign = Attrs.getParamAlignment(Index);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000676 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
677 "Attempt to change alignment!");
678#endif
679
680 // Add the attribute slots before the one we're trying to add.
681 SmallVector<AttributeSet, 4> AttrSet;
682 uint64_t NumAttrs = pImpl->getNumAttributes();
683 AttributeSet AS;
684 uint64_t LastIndex = 0;
685 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000686 if (getSlotIndex(I) >= Index) {
687 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000688 break;
689 }
690 LastIndex = I + 1;
691 AttrSet.push_back(getSlotAttributes(I));
692 }
693
694 // Now add the attribute into the correct slot. There may already be an
695 // AttributeSet there.
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000696 AttrBuilder B(AS, Index);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000697
698 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000699 if (Attrs.getSlotIndex(I) == Index) {
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000700 for (AttributeSetImpl::const_iterator II = Attrs.pImpl->begin(I),
701 IE = Attrs.pImpl->end(I); II != IE; ++II)
Bill Wendling39da0782013-01-31 23:38:01 +0000702 B.addAttribute(*II);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000703 break;
704 }
705
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000706 AttrSet.push_back(AttributeSet::get(C, Index, B));
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000707
708 // Add the remaining attribute slots.
709 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
710 AttrSet.push_back(getSlotAttributes(I));
711
712 return get(C, AttrSet);
713}
714
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000715AttributeSet AttributeSet::removeAttribute(LLVMContext &C, unsigned Index,
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000716 Attribute::AttrKind Attr) const {
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000717 if (!hasAttribute(Index, Attr)) return *this;
718 return removeAttributes(C, Index, AttributeSet::get(C, Index, Attr));
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000719}
720
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000721AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Index,
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000722 AttributeSet Attrs) const {
723 if (!pImpl) return AttributeSet();
724 if (!Attrs.pImpl) return *this;
725
726#ifndef NDEBUG
727 // FIXME it is not obvious how this should work for alignment.
728 // For now, say we can't pass in alignment, which no current use does.
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000729 assert(!Attrs.hasAttribute(Index, Attribute::Alignment) &&
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000730 "Attempt to change alignment!");
731#endif
732
733 // Add the attribute slots before the one we're trying to add.
734 SmallVector<AttributeSet, 4> AttrSet;
735 uint64_t NumAttrs = pImpl->getNumAttributes();
736 AttributeSet AS;
737 uint64_t LastIndex = 0;
738 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000739 if (getSlotIndex(I) >= Index) {
740 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000741 break;
742 }
743 LastIndex = I + 1;
744 AttrSet.push_back(getSlotAttributes(I));
745 }
746
Bill Wendlinge7436542013-01-30 23:07:40 +0000747 // Now remove the attribute from the correct slot. There may already be an
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000748 // AttributeSet there.
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000749 AttrBuilder B(AS, Index);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000750
751 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000752 if (Attrs.getSlotIndex(I) == Index) {
753 B.removeAttributes(Attrs.pImpl->getSlotAttributes(I), Index);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000754 break;
755 }
756
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000757 AttrSet.push_back(AttributeSet::get(C, Index, B));
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000758
759 // Add the remaining attribute slots.
760 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
761 AttrSet.push_back(getSlotAttributes(I));
762
763 return get(C, AttrSet);
764}
765
766//===----------------------------------------------------------------------===//
767// AttributeSet Accessor Methods
768//===----------------------------------------------------------------------===//
769
Bill Wendling85b3fbe2013-02-10 05:00:40 +0000770LLVMContext &AttributeSet::getContext() const {
771 return pImpl->getContext();
772}
773
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000774AttributeSet AttributeSet::getParamAttributes(unsigned Index) const {
775 return pImpl && hasAttributes(Index) ?
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000776 AttributeSet::get(pImpl->getContext(),
Bill Wendling606c8e32013-01-29 03:20:31 +0000777 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000778 std::make_pair(Index, getAttributes(Index)))) :
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000779 AttributeSet();
780}
781
782AttributeSet AttributeSet::getRetAttributes() const {
783 return pImpl && hasAttributes(ReturnIndex) ?
784 AttributeSet::get(pImpl->getContext(),
Bill Wendling606c8e32013-01-29 03:20:31 +0000785 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000786 std::make_pair(ReturnIndex,
787 getAttributes(ReturnIndex)))) :
788 AttributeSet();
789}
790
791AttributeSet AttributeSet::getFnAttributes() const {
792 return pImpl && hasAttributes(FunctionIndex) ?
793 AttributeSet::get(pImpl->getContext(),
Bill Wendling606c8e32013-01-29 03:20:31 +0000794 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000795 std::make_pair(FunctionIndex,
796 getAttributes(FunctionIndex)))) :
797 AttributeSet();
798}
799
800bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{
Bill Wendling606c8e32013-01-29 03:20:31 +0000801 AttributeSetNode *ASN = getAttributes(Index);
802 return ASN ? ASN->hasAttribute(Kind) : false;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000803}
804
Bill Wendling0e9d5d02013-02-13 08:42:21 +0000805bool AttributeSet::hasAttribute(unsigned Index, StringRef Kind) const {
806 AttributeSetNode *ASN = getAttributes(Index);
807 return ASN ? ASN->hasAttribute(Kind) : false;
808}
809
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000810bool AttributeSet::hasAttributes(unsigned Index) const {
Bill Wendling606c8e32013-01-29 03:20:31 +0000811 AttributeSetNode *ASN = getAttributes(Index);
812 return ASN ? ASN->hasAttributes() : false;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000813}
814
815/// \brief Return true if the specified attribute is set for at least one
816/// parameter or for the return value.
817bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr) const {
818 if (pImpl == 0) return false;
819
820 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
821 for (AttributeSetImpl::const_iterator II = pImpl->begin(I),
822 IE = pImpl->end(I); II != IE; ++II)
NAKAMURA Takumieddab152013-01-31 03:47:28 +0000823 if (II->hasAttribute(Attr))
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000824 return true;
825
826 return false;
827}
828
Bill Wendling0e9d5d02013-02-13 08:42:21 +0000829Attribute AttributeSet::getAttribute(unsigned Index,
830 Attribute::AttrKind Kind) const {
831 AttributeSetNode *ASN = getAttributes(Index);
832 return ASN ? ASN->getAttribute(Kind) : Attribute();
833}
834
835Attribute AttributeSet::getAttribute(unsigned Index,
836 StringRef Kind) const {
837 AttributeSetNode *ASN = getAttributes(Index);
838 return ASN ? ASN->getAttribute(Kind) : Attribute();
839}
840
Bill Wendling606c8e32013-01-29 03:20:31 +0000841unsigned AttributeSet::getParamAlignment(unsigned Index) const {
842 AttributeSetNode *ASN = getAttributes(Index);
843 return ASN ? ASN->getAlignment() : 0;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000844}
845
846unsigned AttributeSet::getStackAlignment(unsigned Index) const {
Bill Wendling606c8e32013-01-29 03:20:31 +0000847 AttributeSetNode *ASN = getAttributes(Index);
848 return ASN ? ASN->getStackAlignment() : 0;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000849}
850
Bill Wendlingb1ac6e62013-04-17 23:35:59 +0000851std::string AttributeSet::getAsString(unsigned Index, bool TargetIndependent,
Bill Wendlingb29ce262013-02-11 08:43:33 +0000852 bool InAttrGrp) const {
Bill Wendling606c8e32013-01-29 03:20:31 +0000853 AttributeSetNode *ASN = getAttributes(Index);
Bill Wendlingb1ac6e62013-04-17 23:35:59 +0000854 return ASN ? ASN->getAsString(TargetIndependent, InAttrGrp) :
855 std::string("");
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000856}
857
858/// \brief The attributes for the specified index are returned.
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000859AttributeSetNode *AttributeSet::getAttributes(unsigned Index) const {
Bill Wendling606c8e32013-01-29 03:20:31 +0000860 if (!pImpl) return 0;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000861
Bill Wendling606c8e32013-01-29 03:20:31 +0000862 // Loop through to find the attribute node we want.
863 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000864 if (pImpl->getSlotIndex(I) == Index)
Bill Wendling606c8e32013-01-29 03:20:31 +0000865 return pImpl->getSlotNode(I);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000866
Bill Wendling606c8e32013-01-29 03:20:31 +0000867 return 0;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000868}
869
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000870AttributeSet::iterator AttributeSet::begin(unsigned Slot) const {
Bill Wendling16c4b3c2013-01-31 23:53:05 +0000871 if (!pImpl)
872 return ArrayRef<Attribute>().begin();
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000873 return pImpl->begin(Slot);
Bill Wendling16c4b3c2013-01-31 23:53:05 +0000874}
875
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000876AttributeSet::iterator AttributeSet::end(unsigned Slot) const {
Bill Wendling16c4b3c2013-01-31 23:53:05 +0000877 if (!pImpl)
878 return ArrayRef<Attribute>().end();
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000879 return pImpl->end(Slot);
Bill Wendling16c4b3c2013-01-31 23:53:05 +0000880}
881
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000882//===----------------------------------------------------------------------===//
883// AttributeSet Introspection Methods
884//===----------------------------------------------------------------------===//
885
886/// \brief Return the number of slots used in this attribute list. This is the
887/// number of arguments that have an attribute set on them (including the
888/// function itself).
889unsigned AttributeSet::getNumSlots() const {
890 return pImpl ? pImpl->getNumAttributes() : 0;
891}
892
893uint64_t AttributeSet::getSlotIndex(unsigned Slot) const {
894 assert(pImpl && Slot < pImpl->getNumAttributes() &&
895 "Slot # out of range!");
896 return pImpl->getSlotIndex(Slot);
897}
898
899AttributeSet AttributeSet::getSlotAttributes(unsigned Slot) const {
900 assert(pImpl && Slot < pImpl->getNumAttributes() &&
901 "Slot # out of range!");
902 return pImpl->getSlotAttributes(Slot);
903}
904
905uint64_t AttributeSet::Raw(unsigned Index) const {
906 // FIXME: Remove this.
907 return pImpl ? pImpl->Raw(Index) : 0;
908}
909
910void AttributeSet::dump() const {
911 dbgs() << "PAL[\n";
912
913 for (unsigned i = 0, e = getNumSlots(); i < e; ++i) {
914 uint64_t Index = getSlotIndex(i);
915 dbgs() << " { ";
916 if (Index == ~0U)
917 dbgs() << "~0U";
918 else
919 dbgs() << Index;
920 dbgs() << " => " << getAsString(Index) << " }\n";
921 }
922
923 dbgs() << "]\n";
924}
925
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000926//===----------------------------------------------------------------------===//
Bill Wendling03198882013-01-04 23:27:34 +0000927// AttrBuilder Method Implementations
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000928//===----------------------------------------------------------------------===//
929
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000930AttrBuilder::AttrBuilder(AttributeSet AS, unsigned Index)
Benjamin Kramerc835b8c2013-02-16 19:13:18 +0000931 : Attrs(0), Alignment(0), StackAlignment(0) {
Bill Wendlingec258982013-01-27 21:23:46 +0000932 AttributeSetImpl *pImpl = AS.pImpl;
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000933 if (!pImpl) return;
934
Bill Wendling73bc4522013-01-28 00:21:34 +0000935 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I) {
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000936 if (pImpl->getSlotIndex(I) != Index) continue;
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000937
Bill Wendling383da6b2013-01-30 21:22:59 +0000938 for (AttributeSetImpl::const_iterator II = pImpl->begin(I),
Bill Wendling73bc4522013-01-28 00:21:34 +0000939 IE = pImpl->end(I); II != IE; ++II)
Bill Wendling39da0782013-01-31 23:38:01 +0000940 addAttribute(*II);
Bill Wendling73bc4522013-01-28 00:21:34 +0000941
942 break;
943 }
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000944}
945
Bill Wendling03198882013-01-04 23:27:34 +0000946void AttrBuilder::clear() {
Benjamin Kramer3f213e72013-02-18 12:09:51 +0000947 Attrs.reset();
Bill Wendling03198882013-01-04 23:27:34 +0000948 Alignment = StackAlignment = 0;
949}
950
951AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
Benjamin Kramer3f213e72013-02-18 12:09:51 +0000952 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
Bill Wendling169d5272013-01-31 23:16:25 +0000953 assert(Val != Attribute::Alignment && Val != Attribute::StackAlignment &&
954 "Adding alignment attribute without adding alignment value!");
Benjamin Kramer3f213e72013-02-18 12:09:51 +0000955 Attrs[Val] = true;
Bill Wendling3a106e62012-10-09 19:01:18 +0000956 return *this;
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000957}
958
Bill Wendling39da0782013-01-31 23:38:01 +0000959AttrBuilder &AttrBuilder::addAttribute(Attribute Attr) {
Bill Wendling09ed9102013-02-10 10:13:23 +0000960 if (Attr.isStringAttribute()) {
961 addAttribute(Attr.getKindAsString(), Attr.getValueAsString());
962 return *this;
963 }
964
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000965 Attribute::AttrKind Kind = Attr.getKindAsEnum();
Benjamin Kramer3f213e72013-02-18 12:09:51 +0000966 Attrs[Kind] = true;
Bill Wendling49f60602013-01-28 05:23:28 +0000967
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000968 if (Kind == Attribute::Alignment)
Bill Wendling49f60602013-01-28 05:23:28 +0000969 Alignment = Attr.getAlignment();
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000970 else if (Kind == Attribute::StackAlignment)
Bill Wendling49f60602013-01-28 05:23:28 +0000971 StackAlignment = Attr.getStackAlignment();
972 return *this;
973}
974
Bill Wendlingea59f892013-02-05 08:09:32 +0000975AttrBuilder &AttrBuilder::addAttribute(StringRef A, StringRef V) {
976 TargetDepAttrs[A] = V;
977 return *this;
978}
979
Bill Wendling39da0782013-01-31 23:38:01 +0000980AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
Benjamin Kramer3f213e72013-02-18 12:09:51 +0000981 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
982 Attrs[Val] = false;
Bill Wendling39da0782013-01-31 23:38:01 +0000983
984 if (Val == Attribute::Alignment)
985 Alignment = 0;
986 else if (Val == Attribute::StackAlignment)
987 StackAlignment = 0;
988
989 return *this;
990}
991
Bill Wendlinge7436542013-01-30 23:07:40 +0000992AttrBuilder &AttrBuilder::removeAttributes(AttributeSet A, uint64_t Index) {
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000993 unsigned Slot = ~0U;
Bill Wendling30d2c762013-02-01 00:13:50 +0000994 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
995 if (A.getSlotIndex(I) == Index) {
Bill Wendling8a6a7bb2013-04-18 20:17:28 +0000996 Slot = I;
Bill Wendling30d2c762013-02-01 00:13:50 +0000997 break;
Bill Wendling49f60602013-01-28 05:23:28 +0000998 }
Bill Wendling30d2c762013-02-01 00:13:50 +0000999
Bill Wendling8a6a7bb2013-04-18 20:17:28 +00001000 assert(Slot != ~0U && "Couldn't find index in AttributeSet!");
Bill Wendling30d2c762013-02-01 00:13:50 +00001001
Bill Wendling8a6a7bb2013-04-18 20:17:28 +00001002 for (AttributeSet::iterator I = A.begin(Slot), E = A.end(Slot); I != E; ++I) {
Bill Wendling74fe8252013-02-12 07:56:49 +00001003 Attribute Attr = *I;
1004 if (Attr.isEnumAttribute() || Attr.isAlignAttribute()) {
1005 Attribute::AttrKind Kind = I->getKindAsEnum();
Benjamin Kramer3f213e72013-02-18 12:09:51 +00001006 Attrs[Kind] = false;
Bill Wendling30d2c762013-02-01 00:13:50 +00001007
Bill Wendling74fe8252013-02-12 07:56:49 +00001008 if (Kind == Attribute::Alignment)
1009 Alignment = 0;
1010 else if (Kind == Attribute::StackAlignment)
1011 StackAlignment = 0;
1012 } else {
1013 assert(Attr.isStringAttribute() && "Invalid attribute type!");
1014 std::map<std::string, std::string>::iterator
1015 Iter = TargetDepAttrs.find(Attr.getKindAsString());
1016 if (Iter != TargetDepAttrs.end())
1017 TargetDepAttrs.erase(Iter);
1018 }
Bill Wendling49f60602013-01-28 05:23:28 +00001019 }
1020
1021 return *this;
1022}
1023
Bill Wendlingea59f892013-02-05 08:09:32 +00001024AttrBuilder &AttrBuilder::removeAttribute(StringRef A) {
1025 std::map<std::string, std::string>::iterator I = TargetDepAttrs.find(A);
1026 if (I != TargetDepAttrs.end())
1027 TargetDepAttrs.erase(I);
1028 return *this;
1029}
1030
Bill Wendling702cc912012-10-15 20:35:56 +00001031AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
Bill Wendlingda3f9d82012-10-14 03:58:29 +00001032 if (Align == 0) return *this;
Bill Wendling03198882013-01-04 23:27:34 +00001033
Bill Wendlinge66f3d32012-10-05 06:44:41 +00001034 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1035 assert(Align <= 0x40000000 && "Alignment too large.");
Bill Wendling03198882013-01-04 23:27:34 +00001036
Benjamin Kramer3f213e72013-02-18 12:09:51 +00001037 Attrs[Attribute::Alignment] = true;
Bill Wendling03198882013-01-04 23:27:34 +00001038 Alignment = Align;
Bill Wendlingda3f9d82012-10-14 03:58:29 +00001039 return *this;
Chris Lattner50ee9dd2008-01-02 23:42:30 +00001040}
1041
Bill Wendling03198882013-01-04 23:27:34 +00001042AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
1043 // Default alignment, allow the target to define how to align it.
1044 if (Align == 0) return *this;
1045
1046 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1047 assert(Align <= 0x100 && "Alignment too large.");
1048
Benjamin Kramer3f213e72013-02-18 12:09:51 +00001049 Attrs[Attribute::StackAlignment] = true;
Bill Wendling03198882013-01-04 23:27:34 +00001050 StackAlignment = Align;
1051 return *this;
1052}
1053
Bill Wendling85df6b42013-02-06 01:16:00 +00001054AttrBuilder &AttrBuilder::merge(const AttrBuilder &B) {
1055 // FIXME: What if both have alignments, but they don't match?!
1056 if (!Alignment)
1057 Alignment = B.Alignment;
1058
1059 if (!StackAlignment)
1060 StackAlignment = B.StackAlignment;
1061
Benjamin Kramerc835b8c2013-02-16 19:13:18 +00001062 Attrs |= B.Attrs;
Bill Wendling85df6b42013-02-06 01:16:00 +00001063
1064 for (td_const_iterator I = B.TargetDepAttrs.begin(),
1065 E = B.TargetDepAttrs.end(); I != E; ++I)
1066 TargetDepAttrs[I->first] = I->second;
1067
1068 return *this;
1069}
1070
Bill Wendlingc342d9d2013-02-06 01:33:42 +00001071bool AttrBuilder::contains(StringRef A) const {
1072 return TargetDepAttrs.find(A) != TargetDepAttrs.end();
1073}
1074
Bill Wendling702cc912012-10-15 20:35:56 +00001075bool AttrBuilder::hasAttributes() const {
Benjamin Kramer3f213e72013-02-18 12:09:51 +00001076 return !Attrs.none() || !TargetDepAttrs.empty();
Bill Wendlingf385f4c2012-10-08 23:27:46 +00001077}
Bill Wendling60507d52013-01-04 20:54:35 +00001078
Bill Wendlinge7436542013-01-30 23:07:40 +00001079bool AttrBuilder::hasAttributes(AttributeSet A, uint64_t Index) const {
Bill Wendling8a6a7bb2013-04-18 20:17:28 +00001080 unsigned Slot = ~0U;
Bill Wendlingbdcbccc2013-02-02 00:42:06 +00001081 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
1082 if (A.getSlotIndex(I) == Index) {
Bill Wendling8a6a7bb2013-04-18 20:17:28 +00001083 Slot = I;
Bill Wendlingbdcbccc2013-02-02 00:42:06 +00001084 break;
1085 }
1086
Bill Wendling8a6a7bb2013-04-18 20:17:28 +00001087 assert(Slot != ~0U && "Couldn't find the index!");
Bill Wendlingbdcbccc2013-02-02 00:42:06 +00001088
Bill Wendling8a6a7bb2013-04-18 20:17:28 +00001089 for (AttributeSet::iterator I = A.begin(Slot), E = A.end(Slot);
Bill Wendling74fe8252013-02-12 07:56:49 +00001090 I != E; ++I) {
1091 Attribute Attr = *I;
1092 if (Attr.isEnumAttribute() || Attr.isAlignAttribute()) {
Benjamin Kramer3f213e72013-02-18 12:09:51 +00001093 if (Attrs[I->getKindAsEnum()])
Bill Wendling74fe8252013-02-12 07:56:49 +00001094 return true;
1095 } else {
1096 assert(Attr.isStringAttribute() && "Invalid attribute kind!");
1097 return TargetDepAttrs.find(Attr.getKindAsString())!=TargetDepAttrs.end();
1098 }
1099 }
Bill Wendlingbdcbccc2013-02-02 00:42:06 +00001100
1101 return false;
Bill Wendling8831c062012-10-09 00:01:21 +00001102}
Bill Wendling60507d52013-01-04 20:54:35 +00001103
Bill Wendling702cc912012-10-15 20:35:56 +00001104bool AttrBuilder::hasAlignmentAttr() const {
Bill Wendling03198882013-01-04 23:27:34 +00001105 return Alignment != 0;
Bill Wendlingf385f4c2012-10-08 23:27:46 +00001106}
1107
Bill Wendlingc22f4aa2013-01-29 00:34:06 +00001108bool AttrBuilder::operator==(const AttrBuilder &B) {
Benjamin Kramerc835b8c2013-02-16 19:13:18 +00001109 if (Attrs != B.Attrs)
1110 return false;
Bill Wendlingc342d9d2013-02-06 01:33:42 +00001111
1112 for (td_const_iterator I = TargetDepAttrs.begin(),
1113 E = TargetDepAttrs.end(); I != E; ++I)
1114 if (B.TargetDepAttrs.find(I->first) == B.TargetDepAttrs.end())
1115 return false;
1116
1117 return Alignment == B.Alignment && StackAlignment == B.StackAlignment;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +00001118}
1119
1120AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) {
Bill Wendlingf9271ea2013-02-04 23:32:23 +00001121 // FIXME: Remove this in 4.0.
Bill Wendling8232ece2013-01-29 01:43:29 +00001122 if (!Val) return *this;
1123
Bill Wendlingc22f4aa2013-01-29 00:34:06 +00001124 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
1125 I = Attribute::AttrKind(I + 1)) {
1126 if (uint64_t A = (Val & AttributeImpl::getAttrMask(I))) {
Benjamin Kramer3f213e72013-02-18 12:09:51 +00001127 Attrs[I] = true;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +00001128
1129 if (I == Attribute::Alignment)
1130 Alignment = 1ULL << ((A >> 16) - 1);
1131 else if (I == Attribute::StackAlignment)
1132 StackAlignment = 1ULL << ((A >> 26)-1);
1133 }
1134 }
1135
1136 return *this;
1137}
1138
Bill Wendling8e47daf2013-01-25 23:09:36 +00001139//===----------------------------------------------------------------------===//
1140// AttributeFuncs Function Defintions
1141//===----------------------------------------------------------------------===//
1142
Bill Wendling7beee282013-02-01 01:04:27 +00001143/// \brief Which attributes cannot be applied to a type.
Bill Wendlinge7436542013-01-30 23:07:40 +00001144AttributeSet AttributeFuncs::typeIncompatible(Type *Ty, uint64_t Index) {
Bill Wendling8e47daf2013-01-25 23:09:36 +00001145 AttrBuilder Incompatible;
1146
1147 if (!Ty->isIntegerTy())
1148 // Attribute that only apply to integers.
1149 Incompatible.addAttribute(Attribute::SExt)
1150 .addAttribute(Attribute::ZExt);
1151
1152 if (!Ty->isPointerTy())
1153 // Attribute that only apply to pointers.
1154 Incompatible.addAttribute(Attribute::ByVal)
1155 .addAttribute(Attribute::Nest)
1156 .addAttribute(Attribute::NoAlias)
1157 .addAttribute(Attribute::NoCapture)
1158 .addAttribute(Attribute::StructRet);
1159
Bill Wendlinge7436542013-01-30 23:07:40 +00001160 return AttributeSet::get(Ty->getContext(), Index, Incompatible);
Bill Wendling8e47daf2013-01-25 23:09:36 +00001161}