blob: 84d14b4419267127dad13616ada006133292362a [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
156 if (hasAttribute(Attribute::AddressSafety))
157 return "address_safety";
158 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";
Bill Wendling14292a62013-01-31 20:59:05 +0000198 if (hasAttribute(Attribute::ReturnsTwice))
199 return "returns_twice";
200 if (hasAttribute(Attribute::SExt))
201 return "signext";
Bill Wendling034b94b2012-12-19 07:18:57 +0000202 if (hasAttribute(Attribute::StackProtect))
Bill Wendling606c8e32013-01-29 03:20:31 +0000203 return "ssp";
Bill Wendling034b94b2012-12-19 07:18:57 +0000204 if (hasAttribute(Attribute::StackProtectReq))
Bill Wendling606c8e32013-01-29 03:20:31 +0000205 return "sspreq";
Bill Wendling114baee2013-01-23 06:41:41 +0000206 if (hasAttribute(Attribute::StackProtectStrong))
Bill Wendling606c8e32013-01-29 03:20:31 +0000207 return "sspstrong";
Bill Wendling14292a62013-01-31 20:59:05 +0000208 if (hasAttribute(Attribute::StructRet))
209 return "sret";
Kostya Serebryanyab39afa2013-02-11 08:13:54 +0000210 if (hasAttribute(Attribute::ThreadSafety))
211 return "thread_safety";
212 if (hasAttribute(Attribute::UninitializedChecks))
213 return "uninitialized_checks";
Bill Wendling14292a62013-01-31 20:59:05 +0000214 if (hasAttribute(Attribute::UWTable))
215 return "uwtable";
216 if (hasAttribute(Attribute::ZExt))
217 return "zeroext";
218
219 // FIXME: These should be output like this:
220 //
221 // align=4
222 // alignstack=8
223 //
Bill Wendling034b94b2012-12-19 07:18:57 +0000224 if (hasAttribute(Attribute::Alignment)) {
Bill Wendling606c8e32013-01-29 03:20:31 +0000225 std::string Result;
Bill Wendlingb29ce262013-02-11 08:43:33 +0000226 Result += "align";
227 Result += (InAttrGrp) ? "=" : " ";
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000228 Result += utostr(getValueAsInt());
229 return Result;
230 }
Bill Wendlingb29ce262013-02-11 08:43:33 +0000231
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000232 if (hasAttribute(Attribute::StackAlignment)) {
233 std::string Result;
Bill Wendlingb29ce262013-02-11 08:43:33 +0000234 Result += "alignstack";
235 if (InAttrGrp) {
236 Result += "=";
237 Result += utostr(getValueAsInt());
238 } else {
239 Result += "(";
240 Result += utostr(getValueAsInt());
241 Result += ")";
242 }
Bill Wendling606c8e32013-01-29 03:20:31 +0000243 return Result;
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000244 }
Bill Wendling14292a62013-01-31 20:59:05 +0000245
246 // Convert target-dependent attributes to strings of the form:
247 //
248 // "kind"
249 // "kind" = "value"
Bill Wendling14292a62013-01-31 20:59:05 +0000250 //
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000251 if (isStringAttribute()) {
Bill Wendling14292a62013-01-31 20:59:05 +0000252 std::string Result;
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000253 Result += '\"' + getKindAsString().str() + '"';
Bill Wendling14292a62013-01-31 20:59:05 +0000254
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000255 StringRef Val = pImpl->getValueAsString();
256 if (Val.empty()) return Result;
Bill Wendling5a4041e2013-02-01 22:32:30 +0000257
Bill Wendlingb29ce262013-02-11 08:43:33 +0000258 Result += "=\"" + Val.str() + '"';
Bill Wendling7beee282013-02-01 01:04:27 +0000259 return Result;
Bill Wendling14292a62013-01-31 20:59:05 +0000260 }
Bill Wendling606c8e32013-01-29 03:20:31 +0000261
262 llvm_unreachable("Unknown attribute");
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000263}
264
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000265bool Attribute::operator<(Attribute A) const {
266 if (!pImpl && !A.pImpl) return false;
267 if (!pImpl) return true;
268 if (!A.pImpl) return false;
269 return *pImpl < *A.pImpl;
270}
271
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000272//===----------------------------------------------------------------------===//
273// AttributeImpl Definition
274//===----------------------------------------------------------------------===//
275
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000276AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind Kind)
277 : Context(C), Entry(new EnumAttributeEntry(Kind)) {}
278
279AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind Kind,
280 unsigned Align)
281 : Context(C) {
282 assert((Kind == Attribute::Alignment || Kind == Attribute::StackAlignment) &&
283 "Wrong kind for alignment attribute!");
284 Entry = new AlignAttributeEntry(Kind, Align);
285}
286
287AttributeImpl::AttributeImpl(LLVMContext &C, StringRef Kind, StringRef Val)
288 : Context(C), Entry(new StringAttributeEntry(Kind, Val)) {}
289
290AttributeImpl::~AttributeImpl() {
291 delete Entry;
292}
293
294bool AttributeImpl::isEnumAttribute() const {
295 return isa<EnumAttributeEntry>(Entry);
296}
297
298bool AttributeImpl::isAlignAttribute() const {
299 return isa<AlignAttributeEntry>(Entry);
300}
301
302bool AttributeImpl::isStringAttribute() const {
303 return isa<StringAttributeEntry>(Entry);
304}
305
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000306bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000307 if (isStringAttribute()) return false;
308 return getKindAsEnum() == A;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000309}
310
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000311bool AttributeImpl::hasAttribute(StringRef Kind) const {
312 if (!isStringAttribute()) return false;
313 return getKindAsString() == Kind;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000314}
315
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000316Attribute::AttrKind AttributeImpl::getKindAsEnum() const {
317 if (EnumAttributeEntry *E = dyn_cast<EnumAttributeEntry>(Entry))
318 return E->getEnumKind();
319 return cast<AlignAttributeEntry>(Entry)->getEnumKind();
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000320}
321
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000322uint64_t AttributeImpl::getValueAsInt() const {
323 return cast<AlignAttributeEntry>(Entry)->getAlignment();
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000324}
325
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000326StringRef AttributeImpl::getKindAsString() const {
327 return cast<StringAttributeEntry>(Entry)->getStringKind();
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000328}
329
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000330StringRef AttributeImpl::getValueAsString() const {
331 return cast<StringAttributeEntry>(Entry)->getStringValue();
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000332}
333
334bool AttributeImpl::operator<(const AttributeImpl &AI) const {
Bill Wendling7beee282013-02-01 01:04:27 +0000335 // This sorts the attributes with Attribute::AttrKinds coming first (sorted
336 // relative to their enum value) and then strings.
Bill Wendling94328f42013-02-15 05:25:26 +0000337 if (isEnumAttribute()) {
338 if (AI.isEnumAttribute()) return getKindAsEnum() < AI.getKindAsEnum();
339 if (AI.isAlignAttribute()) return true;
340 if (AI.isStringAttribute()) return true;
341 }
Bill Wendling7beee282013-02-01 01:04:27 +0000342
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000343 if (isAlignAttribute()) {
Bill Wendling94328f42013-02-15 05:25:26 +0000344 if (AI.isEnumAttribute()) return false;
345 if (AI.isAlignAttribute()) return getValueAsInt() < AI.getValueAsInt();
346 if (AI.isStringAttribute()) return true;
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000347 }
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000348
Bill Wendling94328f42013-02-15 05:25:26 +0000349 if (AI.isEnumAttribute()) return false;
350 if (AI.isAlignAttribute()) return false;
351 if (getKindAsString() == AI.getKindAsString())
352 return getValueAsString() < AI.getValueAsString();
353 return getKindAsString() < AI.getKindAsString();
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000354}
355
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000356uint64_t AttributeImpl::getAttrMask(Attribute::AttrKind Val) {
357 // FIXME: Remove this.
358 switch (Val) {
Bill Wendling143d4642013-02-22 00:12:35 +0000359 default:
360 llvm_unreachable("Unsupported attribute type");
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000361 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;
391 case Attribute::AddressSafety: return 1ULL << 32;
392 case Attribute::MinSize: return 1ULL << 33;
393 case Attribute::NoDuplicate: return 1ULL << 34;
394 case Attribute::StackProtectStrong: return 1ULL << 35;
Kostya Serebryanyab39afa2013-02-11 08:13:54 +0000395 case Attribute::ThreadSafety: return 1ULL << 36;
396 case Attribute::UninitializedChecks: return 1ULL << 37;
Bill Wendlingd18e0b92013-02-22 00:40:12 +0000397 case Attribute::NoBuiltin: return 1ULL << 38;
398 }
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000399}
400
401//===----------------------------------------------------------------------===//
402// AttributeSetNode Definition
403//===----------------------------------------------------------------------===//
404
405AttributeSetNode *AttributeSetNode::get(LLVMContext &C,
406 ArrayRef<Attribute> Attrs) {
407 if (Attrs.empty())
408 return 0;
409
410 // Otherwise, build a key to look up the existing attributes.
411 LLVMContextImpl *pImpl = C.pImpl;
412 FoldingSetNodeID ID;
413
414 SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end());
Bill Wendlingf107e6c2013-02-13 09:26:26 +0000415 array_pod_sort(SortedAttrs.begin(), SortedAttrs.end());
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000416
417 for (SmallVectorImpl<Attribute>::iterator I = SortedAttrs.begin(),
418 E = SortedAttrs.end(); I != E; ++I)
419 I->Profile(ID);
420
421 void *InsertPoint;
422 AttributeSetNode *PA =
423 pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint);
424
425 // If we didn't find any existing attributes of the same shape then create a
426 // new one and insert it.
427 if (!PA) {
428 PA = new AttributeSetNode(SortedAttrs);
429 pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint);
430 }
431
432 // Return the AttributesListNode that we found or created.
433 return PA;
434}
435
Bill Wendling606c8e32013-01-29 03:20:31 +0000436bool AttributeSetNode::hasAttribute(Attribute::AttrKind Kind) const {
437 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
438 E = AttrList.end(); I != E; ++I)
NAKAMURA Takumieddab152013-01-31 03:47:28 +0000439 if (I->hasAttribute(Kind))
Bill Wendling606c8e32013-01-29 03:20:31 +0000440 return true;
441 return false;
442}
443
Bill Wendling0e9d5d02013-02-13 08:42:21 +0000444bool AttributeSetNode::hasAttribute(StringRef Kind) const {
445 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
446 E = AttrList.end(); I != E; ++I)
447 if (I->hasAttribute(Kind))
448 return true;
449 return false;
450}
451
452Attribute AttributeSetNode::getAttribute(Attribute::AttrKind 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 *I;
457 return Attribute();
458}
459
460Attribute AttributeSetNode::getAttribute(StringRef 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
Bill Wendling606c8e32013-01-29 03:20:31 +0000468unsigned AttributeSetNode::getAlignment() const {
469 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
470 E = AttrList.end(); I != E; ++I)
NAKAMURA Takumieddab152013-01-31 03:47:28 +0000471 if (I->hasAttribute(Attribute::Alignment))
Bill Wendling606c8e32013-01-29 03:20:31 +0000472 return I->getAlignment();
473 return 0;
474}
475
476unsigned AttributeSetNode::getStackAlignment() 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::StackAlignment))
Bill Wendling606c8e32013-01-29 03:20:31 +0000480 return I->getStackAlignment();
481 return 0;
482}
483
Bill Wendlingb29ce262013-02-11 08:43:33 +0000484std::string AttributeSetNode::getAsString(bool InAttrGrp) const {
Bill Wendling606c8e32013-01-29 03:20:31 +0000485 std::string Str = "";
486 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
Bill Wendling7beee282013-02-01 01:04:27 +0000487 E = AttrList.end(); I != E; ) {
Bill Wendlingb29ce262013-02-11 08:43:33 +0000488 Str += I->getAsString(InAttrGrp);
Bill Wendling7beee282013-02-01 01:04:27 +0000489 if (++I != E) Str += " ";
Bill Wendling606c8e32013-01-29 03:20:31 +0000490 }
491 return Str;
492}
493
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000494//===----------------------------------------------------------------------===//
495// AttributeSetImpl Definition
496//===----------------------------------------------------------------------===//
497
498uint64_t AttributeSetImpl::Raw(uint64_t Index) const {
499 for (unsigned I = 0, E = getNumAttributes(); I != E; ++I) {
500 if (getSlotIndex(I) != Index) continue;
501 const AttributeSetNode *ASN = AttrNodes[I].second;
Bill Wendlingfca0ed22013-02-02 00:52:44 +0000502 uint64_t Mask = 0;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000503
504 for (AttributeSetNode::const_iterator II = ASN->begin(),
Bill Wendlingfca0ed22013-02-02 00:52:44 +0000505 IE = ASN->end(); II != IE; ++II) {
506 Attribute Attr = *II;
Bill Wendling21536912013-02-10 23:18:05 +0000507
508 // This cannot handle string attributes.
509 if (Attr.isStringAttribute()) continue;
510
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000511 Attribute::AttrKind Kind = Attr.getKindAsEnum();
Bill Wendlingfca0ed22013-02-02 00:52:44 +0000512
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000513 if (Kind == Attribute::Alignment)
Bill Wendlingfca0ed22013-02-02 00:52:44 +0000514 Mask |= (Log2_32(ASN->getAlignment()) + 1) << 16;
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000515 else if (Kind == Attribute::StackAlignment)
Bill Wendlingfca0ed22013-02-02 00:52:44 +0000516 Mask |= (Log2_32(ASN->getStackAlignment()) + 1) << 26;
517 else
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000518 Mask |= AttributeImpl::getAttrMask(Kind);
Bill Wendlingfca0ed22013-02-02 00:52:44 +0000519 }
520
521 return Mask;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000522 }
523
524 return 0;
525}
526
527//===----------------------------------------------------------------------===//
528// AttributeSet Construction and Mutation Methods
529//===----------------------------------------------------------------------===//
530
Bill Wendling8232ece2013-01-29 01:43:29 +0000531AttributeSet
532AttributeSet::getImpl(LLVMContext &C,
533 ArrayRef<std::pair<unsigned, AttributeSetNode*> > Attrs) {
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000534 LLVMContextImpl *pImpl = C.pImpl;
535 FoldingSetNodeID ID;
536 AttributeSetImpl::Profile(ID, Attrs);
537
538 void *InsertPoint;
539 AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
540
541 // If we didn't find any existing attributes of the same shape then
542 // create a new one and insert it.
543 if (!PA) {
544 PA = new AttributeSetImpl(C, Attrs);
545 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
546 }
547
548 // Return the AttributesList that we found or created.
549 return AttributeSet(PA);
550}
551
552AttributeSet AttributeSet::get(LLVMContext &C,
553 ArrayRef<std::pair<unsigned, Attribute> > Attrs){
554 // If there are no attributes then return a null AttributesList pointer.
555 if (Attrs.empty())
556 return AttributeSet();
557
558#ifndef NDEBUG
559 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
560 assert((!i || Attrs[i-1].first <= Attrs[i].first) &&
561 "Misordered Attributes list!");
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000562 assert(!Attrs[i].second.hasAttribute(Attribute::None) &&
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000563 "Pointless attribute!");
564 }
565#endif
566
567 // Create a vector if (unsigned, AttributeSetNode*) pairs from the attributes
568 // list.
569 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrPairVec;
570 for (ArrayRef<std::pair<unsigned, Attribute> >::iterator I = Attrs.begin(),
571 E = Attrs.end(); I != E; ) {
572 unsigned Index = I->first;
573 SmallVector<Attribute, 4> AttrVec;
NAKAMURA Takumi3ba51ce2013-01-29 15:18:16 +0000574 while (I != E && I->first == Index) {
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000575 AttrVec.push_back(I->second);
576 ++I;
577 }
578
579 AttrPairVec.push_back(std::make_pair(Index,
580 AttributeSetNode::get(C, AttrVec)));
581 }
582
583 return getImpl(C, AttrPairVec);
584}
585
586AttributeSet AttributeSet::get(LLVMContext &C,
587 ArrayRef<std::pair<unsigned,
588 AttributeSetNode*> > Attrs) {
589 // If there are no attributes then return a null AttributesList pointer.
590 if (Attrs.empty())
591 return AttributeSet();
592
593 return getImpl(C, Attrs);
594}
595
596AttributeSet AttributeSet::get(LLVMContext &C, unsigned Idx, AttrBuilder &B) {
597 if (!B.hasAttributes())
598 return AttributeSet();
Bill Wendling8fbc0c22013-01-29 01:02:03 +0000599
Bill Wendling64754f42013-02-05 23:48:36 +0000600 // Add target-independent attributes.
Bill Wendling8fbc0c22013-01-29 01:02:03 +0000601 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
Benjamin Kramerc835b8c2013-02-16 19:13:18 +0000602 for (Attribute::AttrKind Kind = Attribute::None;
Benjamin Kramer7c146122013-02-16 19:22:28 +0000603 Kind != Attribute::EndAttrKinds; Kind = Attribute::AttrKind(Kind + 1)) {
Benjamin Kramerc835b8c2013-02-16 19:13:18 +0000604 if (!B.contains(Kind))
605 continue;
606
Bill Wendling8fbc0c22013-01-29 01:02:03 +0000607 if (Kind == Attribute::Alignment)
608 Attrs.push_back(std::make_pair(Idx, Attribute::
609 getWithAlignment(C, B.getAlignment())));
610 else if (Kind == Attribute::StackAlignment)
611 Attrs.push_back(std::make_pair(Idx, Attribute::
612 getWithStackAlignment(C, B.getStackAlignment())));
613 else
614 Attrs.push_back(std::make_pair(Idx, Attribute::get(C, Kind)));
615 }
616
Bill Wendling64754f42013-02-05 23:48:36 +0000617 // Add target-dependent (string) attributes.
618 for (AttrBuilder::td_iterator I = B.td_begin(), E = B.td_end();
619 I != E; ++I)
620 Attrs.push_back(std::make_pair(Idx, Attribute::get(C, I->first,I->second)));
621
Bill Wendling8fbc0c22013-01-29 01:02:03 +0000622 return get(C, Attrs);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000623}
624
625AttributeSet AttributeSet::get(LLVMContext &C, unsigned Idx,
626 ArrayRef<Attribute::AttrKind> Kind) {
627 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
628 for (ArrayRef<Attribute::AttrKind>::iterator I = Kind.begin(),
629 E = Kind.end(); I != E; ++I)
630 Attrs.push_back(std::make_pair(Idx, Attribute::get(C, *I)));
631 return get(C, Attrs);
632}
633
634AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<AttributeSet> Attrs) {
635 if (Attrs.empty()) return AttributeSet();
636
637 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrNodeVec;
638 for (unsigned I = 0, E = Attrs.size(); I != E; ++I) {
639 AttributeSet AS = Attrs[I];
640 if (!AS.pImpl) continue;
641 AttrNodeVec.append(AS.pImpl->AttrNodes.begin(), AS.pImpl->AttrNodes.end());
642 }
643
644 return getImpl(C, AttrNodeVec);
645}
646
647AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Idx,
648 Attribute::AttrKind Attr) const {
649 return addAttributes(C, Idx, AttributeSet::get(C, Idx, Attr));
650}
651
652AttributeSet AttributeSet::addAttributes(LLVMContext &C, unsigned Idx,
653 AttributeSet Attrs) const {
654 if (!pImpl) return Attrs;
655 if (!Attrs.pImpl) return *this;
656
657#ifndef NDEBUG
658 // FIXME it is not obvious how this should work for alignment. For now, say
659 // we can't change a known alignment.
660 unsigned OldAlign = getParamAlignment(Idx);
661 unsigned NewAlign = Attrs.getParamAlignment(Idx);
662 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
663 "Attempt to change alignment!");
664#endif
665
666 // Add the attribute slots before the one we're trying to add.
667 SmallVector<AttributeSet, 4> AttrSet;
668 uint64_t NumAttrs = pImpl->getNumAttributes();
669 AttributeSet AS;
670 uint64_t LastIndex = 0;
671 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
672 if (getSlotIndex(I) >= Idx) {
673 if (getSlotIndex(I) == Idx) AS = getSlotAttributes(LastIndex++);
674 break;
675 }
676 LastIndex = I + 1;
677 AttrSet.push_back(getSlotAttributes(I));
678 }
679
680 // Now add the attribute into the correct slot. There may already be an
681 // AttributeSet there.
682 AttrBuilder B(AS, Idx);
683
684 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
685 if (Attrs.getSlotIndex(I) == Idx) {
686 for (AttributeSetImpl::const_iterator II = Attrs.pImpl->begin(I),
687 IE = Attrs.pImpl->end(I); II != IE; ++II)
Bill Wendling39da0782013-01-31 23:38:01 +0000688 B.addAttribute(*II);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000689 break;
690 }
691
692 AttrSet.push_back(AttributeSet::get(C, Idx, B));
693
694 // Add the remaining attribute slots.
695 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
696 AttrSet.push_back(getSlotAttributes(I));
697
698 return get(C, AttrSet);
699}
700
701AttributeSet AttributeSet::removeAttribute(LLVMContext &C, unsigned Idx,
702 Attribute::AttrKind Attr) const {
703 return removeAttributes(C, Idx, AttributeSet::get(C, Idx, Attr));
704}
705
706AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Idx,
707 AttributeSet Attrs) const {
708 if (!pImpl) return AttributeSet();
709 if (!Attrs.pImpl) return *this;
710
711#ifndef NDEBUG
712 // FIXME it is not obvious how this should work for alignment.
713 // For now, say we can't pass in alignment, which no current use does.
714 assert(!Attrs.hasAttribute(Idx, Attribute::Alignment) &&
715 "Attempt to change alignment!");
716#endif
717
718 // Add the attribute slots before the one we're trying to add.
719 SmallVector<AttributeSet, 4> AttrSet;
720 uint64_t NumAttrs = pImpl->getNumAttributes();
721 AttributeSet AS;
722 uint64_t LastIndex = 0;
723 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
724 if (getSlotIndex(I) >= Idx) {
725 if (getSlotIndex(I) == Idx) AS = getSlotAttributes(LastIndex++);
726 break;
727 }
728 LastIndex = I + 1;
729 AttrSet.push_back(getSlotAttributes(I));
730 }
731
Bill Wendlinge7436542013-01-30 23:07:40 +0000732 // Now remove the attribute from the correct slot. There may already be an
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000733 // AttributeSet there.
734 AttrBuilder B(AS, Idx);
735
736 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
737 if (Attrs.getSlotIndex(I) == Idx) {
Bill Wendlinge7436542013-01-30 23:07:40 +0000738 B.removeAttributes(Attrs.pImpl->getSlotAttributes(I), Idx);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000739 break;
740 }
741
742 AttrSet.push_back(AttributeSet::get(C, Idx, B));
743
744 // Add the remaining attribute slots.
745 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
746 AttrSet.push_back(getSlotAttributes(I));
747
748 return get(C, AttrSet);
749}
750
751//===----------------------------------------------------------------------===//
752// AttributeSet Accessor Methods
753//===----------------------------------------------------------------------===//
754
Bill Wendling85b3fbe2013-02-10 05:00:40 +0000755LLVMContext &AttributeSet::getContext() const {
756 return pImpl->getContext();
757}
758
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000759AttributeSet AttributeSet::getParamAttributes(unsigned Idx) const {
760 return pImpl && hasAttributes(Idx) ?
761 AttributeSet::get(pImpl->getContext(),
Bill Wendling606c8e32013-01-29 03:20:31 +0000762 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000763 std::make_pair(Idx, getAttributes(Idx)))) :
764 AttributeSet();
765}
766
767AttributeSet AttributeSet::getRetAttributes() const {
768 return pImpl && hasAttributes(ReturnIndex) ?
769 AttributeSet::get(pImpl->getContext(),
Bill Wendling606c8e32013-01-29 03:20:31 +0000770 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000771 std::make_pair(ReturnIndex,
772 getAttributes(ReturnIndex)))) :
773 AttributeSet();
774}
775
776AttributeSet AttributeSet::getFnAttributes() const {
777 return pImpl && hasAttributes(FunctionIndex) ?
778 AttributeSet::get(pImpl->getContext(),
Bill Wendling606c8e32013-01-29 03:20:31 +0000779 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000780 std::make_pair(FunctionIndex,
781 getAttributes(FunctionIndex)))) :
782 AttributeSet();
783}
784
785bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{
Bill Wendling606c8e32013-01-29 03:20:31 +0000786 AttributeSetNode *ASN = getAttributes(Index);
787 return ASN ? ASN->hasAttribute(Kind) : false;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000788}
789
Bill Wendling0e9d5d02013-02-13 08:42:21 +0000790bool AttributeSet::hasAttribute(unsigned Index, StringRef Kind) const {
791 AttributeSetNode *ASN = getAttributes(Index);
792 return ASN ? ASN->hasAttribute(Kind) : false;
793}
794
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000795bool AttributeSet::hasAttributes(unsigned Index) const {
Bill Wendling606c8e32013-01-29 03:20:31 +0000796 AttributeSetNode *ASN = getAttributes(Index);
797 return ASN ? ASN->hasAttributes() : false;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000798}
799
800/// \brief Return true if the specified attribute is set for at least one
801/// parameter or for the return value.
802bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr) const {
803 if (pImpl == 0) return false;
804
805 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
806 for (AttributeSetImpl::const_iterator II = pImpl->begin(I),
807 IE = pImpl->end(I); II != IE; ++II)
NAKAMURA Takumieddab152013-01-31 03:47:28 +0000808 if (II->hasAttribute(Attr))
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000809 return true;
810
811 return false;
812}
813
Bill Wendling0e9d5d02013-02-13 08:42:21 +0000814Attribute AttributeSet::getAttribute(unsigned Index,
815 Attribute::AttrKind Kind) const {
816 AttributeSetNode *ASN = getAttributes(Index);
817 return ASN ? ASN->getAttribute(Kind) : Attribute();
818}
819
820Attribute AttributeSet::getAttribute(unsigned Index,
821 StringRef Kind) const {
822 AttributeSetNode *ASN = getAttributes(Index);
823 return ASN ? ASN->getAttribute(Kind) : Attribute();
824}
825
Bill Wendling606c8e32013-01-29 03:20:31 +0000826unsigned AttributeSet::getParamAlignment(unsigned Index) const {
827 AttributeSetNode *ASN = getAttributes(Index);
828 return ASN ? ASN->getAlignment() : 0;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000829}
830
831unsigned AttributeSet::getStackAlignment(unsigned Index) const {
Bill Wendling606c8e32013-01-29 03:20:31 +0000832 AttributeSetNode *ASN = getAttributes(Index);
833 return ASN ? ASN->getStackAlignment() : 0;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000834}
835
Bill Wendlingb29ce262013-02-11 08:43:33 +0000836std::string AttributeSet::getAsString(unsigned Index,
837 bool InAttrGrp) const {
Bill Wendling606c8e32013-01-29 03:20:31 +0000838 AttributeSetNode *ASN = getAttributes(Index);
Bill Wendlingb29ce262013-02-11 08:43:33 +0000839 return ASN ? ASN->getAsString(InAttrGrp) : std::string("");
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000840}
841
842/// \brief The attributes for the specified index are returned.
Bill Wendling606c8e32013-01-29 03:20:31 +0000843AttributeSetNode *AttributeSet::getAttributes(unsigned Idx) const {
844 if (!pImpl) return 0;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000845
Bill Wendling606c8e32013-01-29 03:20:31 +0000846 // Loop through to find the attribute node we want.
847 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
848 if (pImpl->getSlotIndex(I) == Idx)
849 return pImpl->getSlotNode(I);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000850
Bill Wendling606c8e32013-01-29 03:20:31 +0000851 return 0;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000852}
853
Bill Wendlingf715dbd2013-02-01 00:48:14 +0000854AttributeSet::iterator AttributeSet::begin(unsigned Idx) const {
Bill Wendling16c4b3c2013-01-31 23:53:05 +0000855 if (!pImpl)
856 return ArrayRef<Attribute>().begin();
857 return pImpl->begin(Idx);
858}
859
Bill Wendlingf715dbd2013-02-01 00:48:14 +0000860AttributeSet::iterator AttributeSet::end(unsigned Idx) const {
Bill Wendling16c4b3c2013-01-31 23:53:05 +0000861 if (!pImpl)
862 return ArrayRef<Attribute>().end();
Bill Wendling30d2c762013-02-01 00:13:50 +0000863 return pImpl->end(Idx);
Bill Wendling16c4b3c2013-01-31 23:53:05 +0000864}
865
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000866//===----------------------------------------------------------------------===//
867// AttributeSet Introspection Methods
868//===----------------------------------------------------------------------===//
869
870/// \brief Return the number of slots used in this attribute list. This is the
871/// number of arguments that have an attribute set on them (including the
872/// function itself).
873unsigned AttributeSet::getNumSlots() const {
874 return pImpl ? pImpl->getNumAttributes() : 0;
875}
876
877uint64_t AttributeSet::getSlotIndex(unsigned Slot) const {
878 assert(pImpl && Slot < pImpl->getNumAttributes() &&
879 "Slot # out of range!");
880 return pImpl->getSlotIndex(Slot);
881}
882
883AttributeSet AttributeSet::getSlotAttributes(unsigned Slot) const {
884 assert(pImpl && Slot < pImpl->getNumAttributes() &&
885 "Slot # out of range!");
886 return pImpl->getSlotAttributes(Slot);
887}
888
889uint64_t AttributeSet::Raw(unsigned Index) const {
890 // FIXME: Remove this.
891 return pImpl ? pImpl->Raw(Index) : 0;
892}
893
894void AttributeSet::dump() const {
895 dbgs() << "PAL[\n";
896
897 for (unsigned i = 0, e = getNumSlots(); i < e; ++i) {
898 uint64_t Index = getSlotIndex(i);
899 dbgs() << " { ";
900 if (Index == ~0U)
901 dbgs() << "~0U";
902 else
903 dbgs() << Index;
904 dbgs() << " => " << getAsString(Index) << " }\n";
905 }
906
907 dbgs() << "]\n";
908}
909
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000910//===----------------------------------------------------------------------===//
Bill Wendling03198882013-01-04 23:27:34 +0000911// AttrBuilder Method Implementations
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000912//===----------------------------------------------------------------------===//
913
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000914AttrBuilder::AttrBuilder(AttributeSet AS, unsigned Idx)
Benjamin Kramerc835b8c2013-02-16 19:13:18 +0000915 : Attrs(0), Alignment(0), StackAlignment(0) {
Bill Wendlingec258982013-01-27 21:23:46 +0000916 AttributeSetImpl *pImpl = AS.pImpl;
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000917 if (!pImpl) return;
918
Bill Wendling73bc4522013-01-28 00:21:34 +0000919 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I) {
920 if (pImpl->getSlotIndex(I) != Idx) continue;
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000921
Bill Wendling383da6b2013-01-30 21:22:59 +0000922 for (AttributeSetImpl::const_iterator II = pImpl->begin(I),
Bill Wendling73bc4522013-01-28 00:21:34 +0000923 IE = pImpl->end(I); II != IE; ++II)
Bill Wendling39da0782013-01-31 23:38:01 +0000924 addAttribute(*II);
Bill Wendling73bc4522013-01-28 00:21:34 +0000925
926 break;
927 }
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000928}
929
Bill Wendling03198882013-01-04 23:27:34 +0000930void AttrBuilder::clear() {
Benjamin Kramer3f213e72013-02-18 12:09:51 +0000931 Attrs.reset();
Bill Wendling03198882013-01-04 23:27:34 +0000932 Alignment = StackAlignment = 0;
933}
934
935AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
Benjamin Kramer3f213e72013-02-18 12:09:51 +0000936 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
Bill Wendling169d5272013-01-31 23:16:25 +0000937 assert(Val != Attribute::Alignment && Val != Attribute::StackAlignment &&
938 "Adding alignment attribute without adding alignment value!");
Benjamin Kramer3f213e72013-02-18 12:09:51 +0000939 Attrs[Val] = true;
Bill Wendling3a106e62012-10-09 19:01:18 +0000940 return *this;
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000941}
942
Bill Wendling39da0782013-01-31 23:38:01 +0000943AttrBuilder &AttrBuilder::addAttribute(Attribute Attr) {
Bill Wendling09ed9102013-02-10 10:13:23 +0000944 if (Attr.isStringAttribute()) {
945 addAttribute(Attr.getKindAsString(), Attr.getValueAsString());
946 return *this;
947 }
948
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000949 Attribute::AttrKind Kind = Attr.getKindAsEnum();
Benjamin Kramer3f213e72013-02-18 12:09:51 +0000950 Attrs[Kind] = true;
Bill Wendling49f60602013-01-28 05:23:28 +0000951
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000952 if (Kind == Attribute::Alignment)
Bill Wendling49f60602013-01-28 05:23:28 +0000953 Alignment = Attr.getAlignment();
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000954 else if (Kind == Attribute::StackAlignment)
Bill Wendling49f60602013-01-28 05:23:28 +0000955 StackAlignment = Attr.getStackAlignment();
956 return *this;
957}
958
Bill Wendlingea59f892013-02-05 08:09:32 +0000959AttrBuilder &AttrBuilder::addAttribute(StringRef A, StringRef V) {
960 TargetDepAttrs[A] = V;
961 return *this;
962}
963
Bill Wendling39da0782013-01-31 23:38:01 +0000964AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
Benjamin Kramer3f213e72013-02-18 12:09:51 +0000965 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
966 Attrs[Val] = false;
Bill Wendling39da0782013-01-31 23:38:01 +0000967
968 if (Val == Attribute::Alignment)
969 Alignment = 0;
970 else if (Val == Attribute::StackAlignment)
971 StackAlignment = 0;
972
973 return *this;
974}
975
Bill Wendlinge7436542013-01-30 23:07:40 +0000976AttrBuilder &AttrBuilder::removeAttributes(AttributeSet A, uint64_t Index) {
Bill Wendling30d2c762013-02-01 00:13:50 +0000977 unsigned Idx = ~0U;
978 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
979 if (A.getSlotIndex(I) == Index) {
980 Idx = I;
981 break;
Bill Wendling49f60602013-01-28 05:23:28 +0000982 }
Bill Wendling30d2c762013-02-01 00:13:50 +0000983
984 assert(Idx != ~0U && "Couldn't find index in AttributeSet!");
985
986 for (AttributeSet::iterator I = A.begin(Idx), E = A.end(Idx); I != E; ++I) {
Bill Wendling74fe8252013-02-12 07:56:49 +0000987 Attribute Attr = *I;
988 if (Attr.isEnumAttribute() || Attr.isAlignAttribute()) {
989 Attribute::AttrKind Kind = I->getKindAsEnum();
Benjamin Kramer3f213e72013-02-18 12:09:51 +0000990 Attrs[Kind] = false;
Bill Wendling30d2c762013-02-01 00:13:50 +0000991
Bill Wendling74fe8252013-02-12 07:56:49 +0000992 if (Kind == Attribute::Alignment)
993 Alignment = 0;
994 else if (Kind == Attribute::StackAlignment)
995 StackAlignment = 0;
996 } else {
997 assert(Attr.isStringAttribute() && "Invalid attribute type!");
998 std::map<std::string, std::string>::iterator
999 Iter = TargetDepAttrs.find(Attr.getKindAsString());
1000 if (Iter != TargetDepAttrs.end())
1001 TargetDepAttrs.erase(Iter);
1002 }
Bill Wendling49f60602013-01-28 05:23:28 +00001003 }
1004
1005 return *this;
1006}
1007
Bill Wendlingea59f892013-02-05 08:09:32 +00001008AttrBuilder &AttrBuilder::removeAttribute(StringRef A) {
1009 std::map<std::string, std::string>::iterator I = TargetDepAttrs.find(A);
1010 if (I != TargetDepAttrs.end())
1011 TargetDepAttrs.erase(I);
1012 return *this;
1013}
1014
Bill Wendling702cc912012-10-15 20:35:56 +00001015AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
Bill Wendlingda3f9d82012-10-14 03:58:29 +00001016 if (Align == 0) return *this;
Bill Wendling03198882013-01-04 23:27:34 +00001017
Bill Wendlinge66f3d32012-10-05 06:44:41 +00001018 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1019 assert(Align <= 0x40000000 && "Alignment too large.");
Bill Wendling03198882013-01-04 23:27:34 +00001020
Benjamin Kramer3f213e72013-02-18 12:09:51 +00001021 Attrs[Attribute::Alignment] = true;
Bill Wendling03198882013-01-04 23:27:34 +00001022 Alignment = Align;
Bill Wendlingda3f9d82012-10-14 03:58:29 +00001023 return *this;
Chris Lattner50ee9dd2008-01-02 23:42:30 +00001024}
1025
Bill Wendling03198882013-01-04 23:27:34 +00001026AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
1027 // Default alignment, allow the target to define how to align it.
1028 if (Align == 0) return *this;
1029
1030 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1031 assert(Align <= 0x100 && "Alignment too large.");
1032
Benjamin Kramer3f213e72013-02-18 12:09:51 +00001033 Attrs[Attribute::StackAlignment] = true;
Bill Wendling03198882013-01-04 23:27:34 +00001034 StackAlignment = Align;
1035 return *this;
1036}
1037
Bill Wendling85df6b42013-02-06 01:16:00 +00001038AttrBuilder &AttrBuilder::merge(const AttrBuilder &B) {
1039 // FIXME: What if both have alignments, but they don't match?!
1040 if (!Alignment)
1041 Alignment = B.Alignment;
1042
1043 if (!StackAlignment)
1044 StackAlignment = B.StackAlignment;
1045
Benjamin Kramerc835b8c2013-02-16 19:13:18 +00001046 Attrs |= B.Attrs;
Bill Wendling85df6b42013-02-06 01:16:00 +00001047
1048 for (td_const_iterator I = B.TargetDepAttrs.begin(),
1049 E = B.TargetDepAttrs.end(); I != E; ++I)
1050 TargetDepAttrs[I->first] = I->second;
1051
1052 return *this;
1053}
1054
Bill Wendlingc342d9d2013-02-06 01:33:42 +00001055bool AttrBuilder::contains(StringRef A) const {
1056 return TargetDepAttrs.find(A) != TargetDepAttrs.end();
1057}
1058
Bill Wendling702cc912012-10-15 20:35:56 +00001059bool AttrBuilder::hasAttributes() const {
Benjamin Kramer3f213e72013-02-18 12:09:51 +00001060 return !Attrs.none() || !TargetDepAttrs.empty();
Bill Wendlingf385f4c2012-10-08 23:27:46 +00001061}
Bill Wendling60507d52013-01-04 20:54:35 +00001062
Bill Wendlinge7436542013-01-30 23:07:40 +00001063bool AttrBuilder::hasAttributes(AttributeSet A, uint64_t Index) const {
Bill Wendlingbdcbccc2013-02-02 00:42:06 +00001064 unsigned Idx = ~0U;
1065 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
1066 if (A.getSlotIndex(I) == Index) {
1067 Idx = I;
1068 break;
1069 }
1070
1071 assert(Idx != ~0U && "Couldn't find the index!");
1072
1073 for (AttributeSet::iterator I = A.begin(Idx), E = A.end(Idx);
Bill Wendling74fe8252013-02-12 07:56:49 +00001074 I != E; ++I) {
1075 Attribute Attr = *I;
1076 if (Attr.isEnumAttribute() || Attr.isAlignAttribute()) {
Benjamin Kramer3f213e72013-02-18 12:09:51 +00001077 if (Attrs[I->getKindAsEnum()])
Bill Wendling74fe8252013-02-12 07:56:49 +00001078 return true;
1079 } else {
1080 assert(Attr.isStringAttribute() && "Invalid attribute kind!");
1081 return TargetDepAttrs.find(Attr.getKindAsString())!=TargetDepAttrs.end();
1082 }
1083 }
Bill Wendlingbdcbccc2013-02-02 00:42:06 +00001084
1085 return false;
Bill Wendling8831c062012-10-09 00:01:21 +00001086}
Bill Wendling60507d52013-01-04 20:54:35 +00001087
Bill Wendling702cc912012-10-15 20:35:56 +00001088bool AttrBuilder::hasAlignmentAttr() const {
Bill Wendling03198882013-01-04 23:27:34 +00001089 return Alignment != 0;
Bill Wendlingf385f4c2012-10-08 23:27:46 +00001090}
1091
Bill Wendlingc22f4aa2013-01-29 00:34:06 +00001092bool AttrBuilder::operator==(const AttrBuilder &B) {
Benjamin Kramerc835b8c2013-02-16 19:13:18 +00001093 if (Attrs != B.Attrs)
1094 return false;
Bill Wendlingc342d9d2013-02-06 01:33:42 +00001095
1096 for (td_const_iterator I = TargetDepAttrs.begin(),
1097 E = TargetDepAttrs.end(); I != E; ++I)
1098 if (B.TargetDepAttrs.find(I->first) == B.TargetDepAttrs.end())
1099 return false;
1100
1101 return Alignment == B.Alignment && StackAlignment == B.StackAlignment;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +00001102}
1103
Bill Wendling143d4642013-02-22 00:12:35 +00001104void AttrBuilder::removeFunctionOnlyAttrs() {
1105 removeAttribute(Attribute::NoReturn)
1106 .removeAttribute(Attribute::NoUnwind)
1107 .removeAttribute(Attribute::ReadNone)
1108 .removeAttribute(Attribute::ReadOnly)
1109 .removeAttribute(Attribute::NoInline)
1110 .removeAttribute(Attribute::AlwaysInline)
1111 .removeAttribute(Attribute::OptimizeForSize)
1112 .removeAttribute(Attribute::StackProtect)
1113 .removeAttribute(Attribute::StackProtectReq)
1114 .removeAttribute(Attribute::StackProtectStrong)
1115 .removeAttribute(Attribute::NoRedZone)
1116 .removeAttribute(Attribute::NoImplicitFloat)
1117 .removeAttribute(Attribute::Naked)
1118 .removeAttribute(Attribute::InlineHint)
1119 .removeAttribute(Attribute::StackAlignment)
1120 .removeAttribute(Attribute::UWTable)
1121 .removeAttribute(Attribute::NonLazyBind)
1122 .removeAttribute(Attribute::ReturnsTwice)
1123 .removeAttribute(Attribute::AddressSafety)
1124 .removeAttribute(Attribute::ThreadSafety)
1125 .removeAttribute(Attribute::UninitializedChecks)
1126 .removeAttribute(Attribute::MinSize)
1127 .removeAttribute(Attribute::NoDuplicate)
1128 .removeAttribute(Attribute::NoBuiltin);
1129}
1130
Bill Wendlingc22f4aa2013-01-29 00:34:06 +00001131AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) {
Bill Wendlingf9271ea2013-02-04 23:32:23 +00001132 // FIXME: Remove this in 4.0.
Bill Wendling8232ece2013-01-29 01:43:29 +00001133 if (!Val) return *this;
1134
Bill Wendlingc22f4aa2013-01-29 00:34:06 +00001135 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
1136 I = Attribute::AttrKind(I + 1)) {
1137 if (uint64_t A = (Val & AttributeImpl::getAttrMask(I))) {
Benjamin Kramer3f213e72013-02-18 12:09:51 +00001138 Attrs[I] = true;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +00001139
1140 if (I == Attribute::Alignment)
1141 Alignment = 1ULL << ((A >> 16) - 1);
1142 else if (I == Attribute::StackAlignment)
1143 StackAlignment = 1ULL << ((A >> 26)-1);
1144 }
1145 }
1146
1147 return *this;
1148}
1149
Bill Wendling8e47daf2013-01-25 23:09:36 +00001150//===----------------------------------------------------------------------===//
1151// AttributeFuncs Function Defintions
1152//===----------------------------------------------------------------------===//
1153
Bill Wendling7beee282013-02-01 01:04:27 +00001154/// \brief Which attributes cannot be applied to a type.
Bill Wendlinge7436542013-01-30 23:07:40 +00001155AttributeSet AttributeFuncs::typeIncompatible(Type *Ty, uint64_t Index) {
Bill Wendling8e47daf2013-01-25 23:09:36 +00001156 AttrBuilder Incompatible;
1157
1158 if (!Ty->isIntegerTy())
1159 // Attribute that only apply to integers.
1160 Incompatible.addAttribute(Attribute::SExt)
1161 .addAttribute(Attribute::ZExt);
1162
1163 if (!Ty->isPointerTy())
1164 // Attribute that only apply to pointers.
1165 Incompatible.addAttribute(Attribute::ByVal)
1166 .addAttribute(Attribute::Nest)
1167 .addAttribute(Attribute::NoAlias)
1168 .addAttribute(Attribute::NoCapture)
1169 .addAttribute(Attribute::StructRet);
1170
Bill Wendlinge7436542013-01-30 23:07:40 +00001171 return AttributeSet::get(Ty->getContext(), Index, Incompatible);
Bill Wendling8e47daf2013-01-25 23:09:36 +00001172}