blob: 412d83e52ef3774d3b031b92b0a48ffbbda6913e [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 Wendling169d5272013-01-31 23:16:25 +000033Attribute Attribute::get(LLVMContext &Context, Constant *Kind, Constant *Val) {
Bill Wendling8e635db2012-10-08 21:47:17 +000034 LLVMContextImpl *pImpl = Context.pImpl;
35 FoldingSetNodeID ID;
Bill Wendling169d5272013-01-31 23:16:25 +000036 ID.AddPointer(Kind);
37 if (Val) ID.AddPointer(Val);
Bill Wendling8e635db2012-10-08 21:47:17 +000038
39 void *InsertPoint;
Bill Wendlingf6670722012-12-20 01:36:59 +000040 AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
Bill Wendling8e635db2012-10-08 21:47:17 +000041
42 if (!PA) {
43 // If we didn't find any existing attributes of the same shape then create a
44 // new one and insert it.
Bill Wendling169d5272013-01-31 23:16:25 +000045 PA = (!Val) ?
46 new AttributeImpl(Context, Kind) :
47 new AttributeImpl(Context, Kind, Val);
Bill Wendling8e635db2012-10-08 21:47:17 +000048 pImpl->AttrsSet.InsertNode(PA, InsertPoint);
49 }
50
51 // Return the AttributesList that we found or created.
Bill Wendling034b94b2012-12-19 07:18:57 +000052 return Attribute(PA);
Bill Wendling8e635db2012-10-08 21:47:17 +000053}
54
Bill Wendling169d5272013-01-31 23:16:25 +000055Attribute Attribute::get(LLVMContext &Context, AttrKind Kind, Constant *Val) {
56 ConstantInt *KindVal = ConstantInt::get(Type::getInt64Ty(Context), Kind);
57 return get(Context, KindVal, Val);
58}
59
Bill Wendlingc08a5ef2013-01-27 22:43:04 +000060Attribute Attribute::getWithAlignment(LLVMContext &Context, uint64_t Align) {
Bill Wendling169d5272013-01-31 23:16:25 +000061 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
62 assert(Align <= 0x40000000 && "Alignment too large.");
63 return get(Context, Alignment,
64 ConstantInt::get(Type::getInt64Ty(Context), Align));
Bill Wendlingc08a5ef2013-01-27 22:43:04 +000065}
66
67Attribute Attribute::getWithStackAlignment(LLVMContext &Context,
68 uint64_t Align) {
Bill Wendling169d5272013-01-31 23:16:25 +000069 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
70 assert(Align <= 0x100 && "Alignment too large.");
71 return get(Context, StackAlignment,
72 ConstantInt::get(Type::getInt64Ty(Context), Align));
Bill Wendlingc08a5ef2013-01-27 22:43:04 +000073}
74
Bill Wendling817abdd2013-01-29 00:48:16 +000075//===----------------------------------------------------------------------===//
76// Attribute Accessor Methods
77//===----------------------------------------------------------------------===//
78
Bill Wendling629fb822012-12-22 00:37:52 +000079bool Attribute::hasAttribute(AttrKind Val) const {
Bill Wendling27107f62012-12-20 21:28:43 +000080 return pImpl && pImpl->hasAttribute(Val);
Bill Wendlinge66f3d32012-10-05 06:44:41 +000081}
82
Bill Wendling6dc37812013-01-29 20:45:34 +000083Constant *Attribute::getAttributeKind() const {
84 return pImpl ? pImpl->getAttributeKind() : 0;
85}
86
87ArrayRef<Constant*> Attribute::getAttributeValues() const {
88 return pImpl ? pImpl->getAttributeValues() : ArrayRef<Constant*>();
89}
90
Bill Wendlinge66f3d32012-10-05 06:44:41 +000091/// This returns the alignment field of an attribute as a byte alignment value.
Bill Wendling034b94b2012-12-19 07:18:57 +000092unsigned Attribute::getAlignment() const {
Bill Wendling7beee282013-02-01 01:04:27 +000093 assert(hasAttribute(Attribute::Alignment) &&
94 "Trying to get alignment from non-alignment attribute!");
Bill Wendlinga8ab5fc2013-01-23 23:00:05 +000095 return pImpl->getAlignment();
Bill Wendlinge66f3d32012-10-05 06:44:41 +000096}
97
98/// This returns the stack alignment field of an attribute as a byte alignment
99/// value.
Bill Wendling034b94b2012-12-19 07:18:57 +0000100unsigned Attribute::getStackAlignment() const {
Bill Wendling7beee282013-02-01 01:04:27 +0000101 assert(hasAttribute(Attribute::StackAlignment) &&
102 "Trying to get alignment from non-alignment attribute!");
Bill Wendlinga8ab5fc2013-01-23 23:00:05 +0000103 return pImpl->getStackAlignment();
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000104}
105
Bill Wendling034b94b2012-12-19 07:18:57 +0000106std::string Attribute::getAsString() const {
Bill Wendling14292a62013-01-31 20:59:05 +0000107 if (!pImpl) return "";
108
109 if (hasAttribute(Attribute::AddressSafety))
110 return "address_safety";
111 if (hasAttribute(Attribute::AlwaysInline))
112 return "alwaysinline";
113 if (hasAttribute(Attribute::ByVal))
114 return "byval";
115 if (hasAttribute(Attribute::InlineHint))
116 return "inlinehint";
Bill Wendling034b94b2012-12-19 07:18:57 +0000117 if (hasAttribute(Attribute::InReg))
Bill Wendling606c8e32013-01-29 03:20:31 +0000118 return "inreg";
Bill Wendling14292a62013-01-31 20:59:05 +0000119 if (hasAttribute(Attribute::MinSize))
120 return "minsize";
121 if (hasAttribute(Attribute::Naked))
122 return "naked";
123 if (hasAttribute(Attribute::Nest))
124 return "nest";
Bill Wendling034b94b2012-12-19 07:18:57 +0000125 if (hasAttribute(Attribute::NoAlias))
Bill Wendling606c8e32013-01-29 03:20:31 +0000126 return "noalias";
Bill Wendling034b94b2012-12-19 07:18:57 +0000127 if (hasAttribute(Attribute::NoCapture))
Bill Wendling606c8e32013-01-29 03:20:31 +0000128 return "nocapture";
Bill Wendling14292a62013-01-31 20:59:05 +0000129 if (hasAttribute(Attribute::NoDuplicate))
130 return "noduplicate";
131 if (hasAttribute(Attribute::NoImplicitFloat))
132 return "noimplicitfloat";
133 if (hasAttribute(Attribute::NoInline))
134 return "noinline";
135 if (hasAttribute(Attribute::NonLazyBind))
136 return "nonlazybind";
137 if (hasAttribute(Attribute::NoRedZone))
138 return "noredzone";
139 if (hasAttribute(Attribute::NoReturn))
140 return "noreturn";
141 if (hasAttribute(Attribute::NoUnwind))
142 return "nounwind";
143 if (hasAttribute(Attribute::OptimizeForSize))
144 return "optsize";
Bill Wendling034b94b2012-12-19 07:18:57 +0000145 if (hasAttribute(Attribute::ReadNone))
Bill Wendling606c8e32013-01-29 03:20:31 +0000146 return "readnone";
Bill Wendling034b94b2012-12-19 07:18:57 +0000147 if (hasAttribute(Attribute::ReadOnly))
Bill Wendling606c8e32013-01-29 03:20:31 +0000148 return "readonly";
Bill Wendling14292a62013-01-31 20:59:05 +0000149 if (hasAttribute(Attribute::ReturnsTwice))
150 return "returns_twice";
151 if (hasAttribute(Attribute::SExt))
152 return "signext";
Bill Wendling034b94b2012-12-19 07:18:57 +0000153 if (hasAttribute(Attribute::StackProtect))
Bill Wendling606c8e32013-01-29 03:20:31 +0000154 return "ssp";
Bill Wendling034b94b2012-12-19 07:18:57 +0000155 if (hasAttribute(Attribute::StackProtectReq))
Bill Wendling606c8e32013-01-29 03:20:31 +0000156 return "sspreq";
Bill Wendling114baee2013-01-23 06:41:41 +0000157 if (hasAttribute(Attribute::StackProtectStrong))
Bill Wendling606c8e32013-01-29 03:20:31 +0000158 return "sspstrong";
Bill Wendling14292a62013-01-31 20:59:05 +0000159 if (hasAttribute(Attribute::StructRet))
160 return "sret";
161 if (hasAttribute(Attribute::UWTable))
162 return "uwtable";
163 if (hasAttribute(Attribute::ZExt))
164 return "zeroext";
165
166 // FIXME: These should be output like this:
167 //
168 // align=4
169 // alignstack=8
170 //
Bill Wendling034b94b2012-12-19 07:18:57 +0000171 if (hasAttribute(Attribute::StackAlignment)) {
Bill Wendling606c8e32013-01-29 03:20:31 +0000172 std::string Result;
Charles Davis1e063d12010-02-12 00:31:15 +0000173 Result += "alignstack(";
Bill Wendlingef99fe82012-09-21 15:26:31 +0000174 Result += utostr(getStackAlignment());
Bill Wendling606c8e32013-01-29 03:20:31 +0000175 Result += ")";
176 return Result;
Charles Davis1e063d12010-02-12 00:31:15 +0000177 }
Bill Wendling034b94b2012-12-19 07:18:57 +0000178 if (hasAttribute(Attribute::Alignment)) {
Bill Wendling606c8e32013-01-29 03:20:31 +0000179 std::string Result;
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000180 Result += "align ";
Bill Wendlingef99fe82012-09-21 15:26:31 +0000181 Result += utostr(getAlignment());
Bill Wendling606c8e32013-01-29 03:20:31 +0000182 return Result;
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000183 }
Bill Wendling14292a62013-01-31 20:59:05 +0000184
185 // Convert target-dependent attributes to strings of the form:
186 //
187 // "kind"
188 // "kind" = "value"
189 // "kind" = ("value1" "value2" "value3" )
190 //
191 if (ConstantDataArray *CDA =
192 dyn_cast<ConstantDataArray>(pImpl->getAttributeKind())) {
193 std::string Result;
194 Result += '\"' + CDA->getAsString().str() + '"';
195
196 ArrayRef<Constant*> Vals = pImpl->getAttributeValues();
197 if (Vals.empty()) return Result;
198 Result += " = ";
199 if (Vals.size() > 1) Result += '(';
200 for (ArrayRef<Constant*>::iterator I = Vals.begin(), E = Vals.end();
201 I != E; ) {
202 ConstantDataArray *CDA = cast<ConstantDataArray>(*I++);
203 Result += '\"' + CDA->getAsString().str() + '"';
204 if (I != E) Result += ' ';
205 }
206 if (Vals.size() > 1) Result += ')';
Bill Wendling7beee282013-02-01 01:04:27 +0000207 return Result;
Bill Wendling14292a62013-01-31 20:59:05 +0000208 }
Bill Wendling606c8e32013-01-29 03:20:31 +0000209
210 llvm_unreachable("Unknown attribute");
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000211}
212
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000213bool Attribute::operator==(AttrKind K) const {
Bill Wendling14292a62013-01-31 20:59:05 +0000214 return (pImpl && *pImpl == K) || (!pImpl && K == None);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000215}
216bool Attribute::operator!=(AttrKind K) const {
217 return !(*this == K);
218}
219
220bool Attribute::operator<(Attribute A) const {
221 if (!pImpl && !A.pImpl) return false;
222 if (!pImpl) return true;
223 if (!A.pImpl) return false;
224 return *pImpl < *A.pImpl;
225}
226
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000227//===----------------------------------------------------------------------===//
228// AttributeImpl Definition
229//===----------------------------------------------------------------------===//
230
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000231bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
Bill Wendling169d5272013-01-31 23:16:25 +0000232 if (ConstantInt *CI = dyn_cast<ConstantInt>(Kind))
233 return CI->getZExtValue() == A;
234 return false;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000235}
236
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000237uint64_t AttributeImpl::getAlignment() const {
Bill Wendling169d5272013-01-31 23:16:25 +0000238 assert(hasAttribute(Attribute::Alignment) &&
239 "Trying to retrieve the alignment from a non-alignment attr!");
240 return cast<ConstantInt>(Vals[0])->getZExtValue();
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000241}
242
243uint64_t AttributeImpl::getStackAlignment() const {
Bill Wendling169d5272013-01-31 23:16:25 +0000244 assert(hasAttribute(Attribute::StackAlignment) &&
245 "Trying to retrieve the stack alignment from a non-alignment attr!");
246 return cast<ConstantInt>(Vals[0])->getZExtValue();
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000247}
248
Bill Wendling9f175f82013-01-29 20:37:10 +0000249bool AttributeImpl::operator==(Attribute::AttrKind kind) const {
250 if (ConstantInt *CI = dyn_cast<ConstantInt>(Kind))
251 return CI->getZExtValue() == kind;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000252 return false;
253}
Bill Wendling9f175f82013-01-29 20:37:10 +0000254bool AttributeImpl::operator!=(Attribute::AttrKind kind) const {
255 return !(*this == kind);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000256}
257
Bill Wendling9f175f82013-01-29 20:37:10 +0000258bool AttributeImpl::operator==(StringRef kind) const {
259 if (ConstantDataArray *CDA = dyn_cast<ConstantDataArray>(Kind))
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000260 if (CDA->isString())
Bill Wendling9f175f82013-01-29 20:37:10 +0000261 return CDA->getAsString() == kind;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000262 return false;
263}
264
Bill Wendling9f175f82013-01-29 20:37:10 +0000265bool AttributeImpl::operator!=(StringRef kind) const {
266 return !(*this == kind);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000267}
268
269bool AttributeImpl::operator<(const AttributeImpl &AI) const {
Bill Wendling7beee282013-02-01 01:04:27 +0000270 // This sorts the attributes with Attribute::AttrKinds coming first (sorted
271 // relative to their enum value) and then strings.
272
Bill Wendling9f175f82013-01-29 20:37:10 +0000273 if (!Kind && !AI.Kind) return false;
274 if (!Kind && AI.Kind) return true;
275 if (Kind && !AI.Kind) return false;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000276
Bill Wendling9f175f82013-01-29 20:37:10 +0000277 ConstantInt *ThisCI = dyn_cast<ConstantInt>(Kind);
278 ConstantInt *ThatCI = dyn_cast<ConstantInt>(AI.Kind);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000279
Bill Wendling9f175f82013-01-29 20:37:10 +0000280 ConstantDataArray *ThisCDA = dyn_cast<ConstantDataArray>(Kind);
281 ConstantDataArray *ThatCDA = dyn_cast<ConstantDataArray>(AI.Kind);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000282
283 if (ThisCI && ThatCI)
284 return ThisCI->getZExtValue() < ThatCI->getZExtValue();
285
286 if (ThisCI && ThatCDA)
287 return true;
288
289 if (ThisCDA && ThatCI)
290 return false;
291
292 return ThisCDA->getAsString() < ThatCDA->getAsString();
293}
294
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000295uint64_t AttributeImpl::getAttrMask(Attribute::AttrKind Val) {
296 // FIXME: Remove this.
297 switch (Val) {
298 case Attribute::EndAttrKinds:
299 case Attribute::AttrKindEmptyKey:
300 case Attribute::AttrKindTombstoneKey:
301 llvm_unreachable("Synthetic enumerators which should never get here");
302
303 case Attribute::None: return 0;
304 case Attribute::ZExt: return 1 << 0;
305 case Attribute::SExt: return 1 << 1;
306 case Attribute::NoReturn: return 1 << 2;
307 case Attribute::InReg: return 1 << 3;
308 case Attribute::StructRet: return 1 << 4;
309 case Attribute::NoUnwind: return 1 << 5;
310 case Attribute::NoAlias: return 1 << 6;
311 case Attribute::ByVal: return 1 << 7;
312 case Attribute::Nest: return 1 << 8;
313 case Attribute::ReadNone: return 1 << 9;
314 case Attribute::ReadOnly: return 1 << 10;
315 case Attribute::NoInline: return 1 << 11;
316 case Attribute::AlwaysInline: return 1 << 12;
317 case Attribute::OptimizeForSize: return 1 << 13;
318 case Attribute::StackProtect: return 1 << 14;
319 case Attribute::StackProtectReq: return 1 << 15;
320 case Attribute::Alignment: return 31 << 16;
321 case Attribute::NoCapture: return 1 << 21;
322 case Attribute::NoRedZone: return 1 << 22;
323 case Attribute::NoImplicitFloat: return 1 << 23;
324 case Attribute::Naked: return 1 << 24;
325 case Attribute::InlineHint: return 1 << 25;
326 case Attribute::StackAlignment: return 7 << 26;
327 case Attribute::ReturnsTwice: return 1 << 29;
328 case Attribute::UWTable: return 1 << 30;
329 case Attribute::NonLazyBind: return 1U << 31;
330 case Attribute::AddressSafety: return 1ULL << 32;
331 case Attribute::MinSize: return 1ULL << 33;
332 case Attribute::NoDuplicate: return 1ULL << 34;
333 case Attribute::StackProtectStrong: return 1ULL << 35;
334 }
335 llvm_unreachable("Unsupported attribute type");
336}
337
338//===----------------------------------------------------------------------===//
339// AttributeSetNode Definition
340//===----------------------------------------------------------------------===//
341
342AttributeSetNode *AttributeSetNode::get(LLVMContext &C,
343 ArrayRef<Attribute> Attrs) {
344 if (Attrs.empty())
345 return 0;
346
347 // Otherwise, build a key to look up the existing attributes.
348 LLVMContextImpl *pImpl = C.pImpl;
349 FoldingSetNodeID ID;
350
351 SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end());
352 std::sort(SortedAttrs.begin(), SortedAttrs.end());
353
354 for (SmallVectorImpl<Attribute>::iterator I = SortedAttrs.begin(),
355 E = SortedAttrs.end(); I != E; ++I)
356 I->Profile(ID);
357
358 void *InsertPoint;
359 AttributeSetNode *PA =
360 pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint);
361
362 // If we didn't find any existing attributes of the same shape then create a
363 // new one and insert it.
364 if (!PA) {
365 PA = new AttributeSetNode(SortedAttrs);
366 pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint);
367 }
368
369 // Return the AttributesListNode that we found or created.
370 return PA;
371}
372
Bill Wendling606c8e32013-01-29 03:20:31 +0000373bool AttributeSetNode::hasAttribute(Attribute::AttrKind Kind) const {
374 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
375 E = AttrList.end(); I != E; ++I)
NAKAMURA Takumieddab152013-01-31 03:47:28 +0000376 if (I->hasAttribute(Kind))
Bill Wendling606c8e32013-01-29 03:20:31 +0000377 return true;
378 return false;
379}
380
381unsigned AttributeSetNode::getAlignment() const {
382 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
383 E = AttrList.end(); I != E; ++I)
NAKAMURA Takumieddab152013-01-31 03:47:28 +0000384 if (I->hasAttribute(Attribute::Alignment))
Bill Wendling606c8e32013-01-29 03:20:31 +0000385 return I->getAlignment();
386 return 0;
387}
388
389unsigned AttributeSetNode::getStackAlignment() const {
390 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
391 E = AttrList.end(); I != E; ++I)
NAKAMURA Takumieddab152013-01-31 03:47:28 +0000392 if (I->hasAttribute(Attribute::StackAlignment))
Bill Wendling606c8e32013-01-29 03:20:31 +0000393 return I->getStackAlignment();
394 return 0;
395}
396
397std::string AttributeSetNode::getAsString() const {
398 std::string Str = "";
399 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
Bill Wendling7beee282013-02-01 01:04:27 +0000400 E = AttrList.end(); I != E; ) {
Bill Wendling606c8e32013-01-29 03:20:31 +0000401 Str += I->getAsString();
Bill Wendling7beee282013-02-01 01:04:27 +0000402 if (++I != E) Str += " ";
Bill Wendling606c8e32013-01-29 03:20:31 +0000403 }
404 return Str;
405}
406
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000407//===----------------------------------------------------------------------===//
408// AttributeSetImpl Definition
409//===----------------------------------------------------------------------===//
410
411uint64_t AttributeSetImpl::Raw(uint64_t Index) const {
412 for (unsigned I = 0, E = getNumAttributes(); I != E; ++I) {
413 if (getSlotIndex(I) != Index) continue;
414 const AttributeSetNode *ASN = AttrNodes[I].second;
415 AttrBuilder B;
416
417 for (AttributeSetNode::const_iterator II = ASN->begin(),
418 IE = ASN->end(); II != IE; ++II)
Bill Wendling39da0782013-01-31 23:38:01 +0000419 B.addAttribute(*II);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000420 return B.Raw();
421 }
422
423 return 0;
424}
425
426//===----------------------------------------------------------------------===//
427// AttributeSet Construction and Mutation Methods
428//===----------------------------------------------------------------------===//
429
Bill Wendling8232ece2013-01-29 01:43:29 +0000430AttributeSet
431AttributeSet::getImpl(LLVMContext &C,
432 ArrayRef<std::pair<unsigned, AttributeSetNode*> > Attrs) {
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000433 LLVMContextImpl *pImpl = C.pImpl;
434 FoldingSetNodeID ID;
435 AttributeSetImpl::Profile(ID, Attrs);
436
437 void *InsertPoint;
438 AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
439
440 // If we didn't find any existing attributes of the same shape then
441 // create a new one and insert it.
442 if (!PA) {
443 PA = new AttributeSetImpl(C, Attrs);
444 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
445 }
446
447 // Return the AttributesList that we found or created.
448 return AttributeSet(PA);
449}
450
451AttributeSet AttributeSet::get(LLVMContext &C,
452 ArrayRef<std::pair<unsigned, Attribute> > Attrs){
453 // If there are no attributes then return a null AttributesList pointer.
454 if (Attrs.empty())
455 return AttributeSet();
456
457#ifndef NDEBUG
458 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
459 assert((!i || Attrs[i-1].first <= Attrs[i].first) &&
460 "Misordered Attributes list!");
Bill Wendling82aea642013-01-31 06:22:35 +0000461 assert(Attrs[i].second != Attribute::None &&
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000462 "Pointless attribute!");
463 }
464#endif
465
466 // Create a vector if (unsigned, AttributeSetNode*) pairs from the attributes
467 // list.
468 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrPairVec;
469 for (ArrayRef<std::pair<unsigned, Attribute> >::iterator I = Attrs.begin(),
470 E = Attrs.end(); I != E; ) {
471 unsigned Index = I->first;
472 SmallVector<Attribute, 4> AttrVec;
NAKAMURA Takumi3ba51ce2013-01-29 15:18:16 +0000473 while (I != E && I->first == Index) {
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000474 AttrVec.push_back(I->second);
475 ++I;
476 }
477
478 AttrPairVec.push_back(std::make_pair(Index,
479 AttributeSetNode::get(C, AttrVec)));
480 }
481
482 return getImpl(C, AttrPairVec);
483}
484
485AttributeSet AttributeSet::get(LLVMContext &C,
486 ArrayRef<std::pair<unsigned,
487 AttributeSetNode*> > Attrs) {
488 // If there are no attributes then return a null AttributesList pointer.
489 if (Attrs.empty())
490 return AttributeSet();
491
492 return getImpl(C, Attrs);
493}
494
495AttributeSet AttributeSet::get(LLVMContext &C, unsigned Idx, AttrBuilder &B) {
496 if (!B.hasAttributes())
497 return AttributeSet();
Bill Wendling8fbc0c22013-01-29 01:02:03 +0000498
499 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
500 for (AttrBuilder::iterator I = B.begin(), E = B.end(); I != E; ++I) {
501 Attribute::AttrKind Kind = *I;
502 if (Kind == Attribute::Alignment)
503 Attrs.push_back(std::make_pair(Idx, Attribute::
504 getWithAlignment(C, B.getAlignment())));
505 else if (Kind == Attribute::StackAlignment)
506 Attrs.push_back(std::make_pair(Idx, Attribute::
507 getWithStackAlignment(C, B.getStackAlignment())));
508 else
509 Attrs.push_back(std::make_pair(Idx, Attribute::get(C, Kind)));
510 }
511
512 return get(C, Attrs);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000513}
514
515AttributeSet AttributeSet::get(LLVMContext &C, unsigned Idx,
516 ArrayRef<Attribute::AttrKind> Kind) {
517 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
518 for (ArrayRef<Attribute::AttrKind>::iterator I = Kind.begin(),
519 E = Kind.end(); I != E; ++I)
520 Attrs.push_back(std::make_pair(Idx, Attribute::get(C, *I)));
521 return get(C, Attrs);
522}
523
524AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<AttributeSet> Attrs) {
525 if (Attrs.empty()) return AttributeSet();
526
527 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrNodeVec;
528 for (unsigned I = 0, E = Attrs.size(); I != E; ++I) {
529 AttributeSet AS = Attrs[I];
530 if (!AS.pImpl) continue;
531 AttrNodeVec.append(AS.pImpl->AttrNodes.begin(), AS.pImpl->AttrNodes.end());
532 }
533
534 return getImpl(C, AttrNodeVec);
535}
536
537AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Idx,
538 Attribute::AttrKind Attr) const {
539 return addAttributes(C, Idx, AttributeSet::get(C, Idx, Attr));
540}
541
542AttributeSet AttributeSet::addAttributes(LLVMContext &C, unsigned Idx,
543 AttributeSet Attrs) const {
544 if (!pImpl) return Attrs;
545 if (!Attrs.pImpl) return *this;
546
547#ifndef NDEBUG
548 // FIXME it is not obvious how this should work for alignment. For now, say
549 // we can't change a known alignment.
550 unsigned OldAlign = getParamAlignment(Idx);
551 unsigned NewAlign = Attrs.getParamAlignment(Idx);
552 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
553 "Attempt to change alignment!");
554#endif
555
556 // Add the attribute slots before the one we're trying to add.
557 SmallVector<AttributeSet, 4> AttrSet;
558 uint64_t NumAttrs = pImpl->getNumAttributes();
559 AttributeSet AS;
560 uint64_t LastIndex = 0;
561 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
562 if (getSlotIndex(I) >= Idx) {
563 if (getSlotIndex(I) == Idx) AS = getSlotAttributes(LastIndex++);
564 break;
565 }
566 LastIndex = I + 1;
567 AttrSet.push_back(getSlotAttributes(I));
568 }
569
570 // Now add the attribute into the correct slot. There may already be an
571 // AttributeSet there.
572 AttrBuilder B(AS, Idx);
573
574 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
575 if (Attrs.getSlotIndex(I) == Idx) {
576 for (AttributeSetImpl::const_iterator II = Attrs.pImpl->begin(I),
577 IE = Attrs.pImpl->end(I); II != IE; ++II)
Bill Wendling39da0782013-01-31 23:38:01 +0000578 B.addAttribute(*II);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000579 break;
580 }
581
582 AttrSet.push_back(AttributeSet::get(C, Idx, B));
583
584 // Add the remaining attribute slots.
585 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
586 AttrSet.push_back(getSlotAttributes(I));
587
588 return get(C, AttrSet);
589}
590
591AttributeSet AttributeSet::removeAttribute(LLVMContext &C, unsigned Idx,
592 Attribute::AttrKind Attr) const {
593 return removeAttributes(C, Idx, AttributeSet::get(C, Idx, Attr));
594}
595
596AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Idx,
597 AttributeSet Attrs) const {
598 if (!pImpl) return AttributeSet();
599 if (!Attrs.pImpl) return *this;
600
601#ifndef NDEBUG
602 // FIXME it is not obvious how this should work for alignment.
603 // For now, say we can't pass in alignment, which no current use does.
604 assert(!Attrs.hasAttribute(Idx, Attribute::Alignment) &&
605 "Attempt to change alignment!");
606#endif
607
608 // Add the attribute slots before the one we're trying to add.
609 SmallVector<AttributeSet, 4> AttrSet;
610 uint64_t NumAttrs = pImpl->getNumAttributes();
611 AttributeSet AS;
612 uint64_t LastIndex = 0;
613 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
614 if (getSlotIndex(I) >= Idx) {
615 if (getSlotIndex(I) == Idx) AS = getSlotAttributes(LastIndex++);
616 break;
617 }
618 LastIndex = I + 1;
619 AttrSet.push_back(getSlotAttributes(I));
620 }
621
Bill Wendlinge7436542013-01-30 23:07:40 +0000622 // Now remove the attribute from the correct slot. There may already be an
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000623 // AttributeSet there.
624 AttrBuilder B(AS, Idx);
625
626 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
627 if (Attrs.getSlotIndex(I) == Idx) {
Bill Wendlinge7436542013-01-30 23:07:40 +0000628 B.removeAttributes(Attrs.pImpl->getSlotAttributes(I), Idx);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000629 break;
630 }
631
632 AttrSet.push_back(AttributeSet::get(C, Idx, B));
633
634 // Add the remaining attribute slots.
635 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
636 AttrSet.push_back(getSlotAttributes(I));
637
638 return get(C, AttrSet);
639}
640
641//===----------------------------------------------------------------------===//
642// AttributeSet Accessor Methods
643//===----------------------------------------------------------------------===//
644
645AttributeSet AttributeSet::getParamAttributes(unsigned Idx) const {
646 return pImpl && hasAttributes(Idx) ?
647 AttributeSet::get(pImpl->getContext(),
Bill Wendling606c8e32013-01-29 03:20:31 +0000648 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000649 std::make_pair(Idx, getAttributes(Idx)))) :
650 AttributeSet();
651}
652
653AttributeSet AttributeSet::getRetAttributes() const {
654 return pImpl && hasAttributes(ReturnIndex) ?
655 AttributeSet::get(pImpl->getContext(),
Bill Wendling606c8e32013-01-29 03:20:31 +0000656 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000657 std::make_pair(ReturnIndex,
658 getAttributes(ReturnIndex)))) :
659 AttributeSet();
660}
661
662AttributeSet AttributeSet::getFnAttributes() const {
663 return pImpl && hasAttributes(FunctionIndex) ?
664 AttributeSet::get(pImpl->getContext(),
Bill Wendling606c8e32013-01-29 03:20:31 +0000665 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000666 std::make_pair(FunctionIndex,
667 getAttributes(FunctionIndex)))) :
668 AttributeSet();
669}
670
671bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{
Bill Wendling606c8e32013-01-29 03:20:31 +0000672 AttributeSetNode *ASN = getAttributes(Index);
673 return ASN ? ASN->hasAttribute(Kind) : false;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000674}
675
676bool AttributeSet::hasAttributes(unsigned Index) const {
Bill Wendling606c8e32013-01-29 03:20:31 +0000677 AttributeSetNode *ASN = getAttributes(Index);
678 return ASN ? ASN->hasAttributes() : false;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000679}
680
681/// \brief Return true if the specified attribute is set for at least one
682/// parameter or for the return value.
683bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr) const {
684 if (pImpl == 0) return false;
685
686 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
687 for (AttributeSetImpl::const_iterator II = pImpl->begin(I),
688 IE = pImpl->end(I); II != IE; ++II)
NAKAMURA Takumieddab152013-01-31 03:47:28 +0000689 if (II->hasAttribute(Attr))
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000690 return true;
691
692 return false;
693}
694
Bill Wendling606c8e32013-01-29 03:20:31 +0000695unsigned AttributeSet::getParamAlignment(unsigned Index) const {
696 AttributeSetNode *ASN = getAttributes(Index);
697 return ASN ? ASN->getAlignment() : 0;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000698}
699
700unsigned AttributeSet::getStackAlignment(unsigned Index) const {
Bill Wendling606c8e32013-01-29 03:20:31 +0000701 AttributeSetNode *ASN = getAttributes(Index);
702 return ASN ? ASN->getStackAlignment() : 0;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000703}
704
705std::string AttributeSet::getAsString(unsigned Index) const {
Bill Wendling606c8e32013-01-29 03:20:31 +0000706 AttributeSetNode *ASN = getAttributes(Index);
707 return ASN ? ASN->getAsString() : std::string("");
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000708}
709
710/// \brief The attributes for the specified index are returned.
Bill Wendling606c8e32013-01-29 03:20:31 +0000711AttributeSetNode *AttributeSet::getAttributes(unsigned Idx) const {
712 if (!pImpl) return 0;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000713
Bill Wendling606c8e32013-01-29 03:20:31 +0000714 // Loop through to find the attribute node we want.
715 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
716 if (pImpl->getSlotIndex(I) == Idx)
717 return pImpl->getSlotNode(I);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000718
Bill Wendling606c8e32013-01-29 03:20:31 +0000719 return 0;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000720}
721
Bill Wendlingf715dbd2013-02-01 00:48:14 +0000722AttributeSet::iterator AttributeSet::begin(unsigned Idx) const {
Bill Wendling16c4b3c2013-01-31 23:53:05 +0000723 if (!pImpl)
724 return ArrayRef<Attribute>().begin();
725 return pImpl->begin(Idx);
726}
727
Bill Wendlingf715dbd2013-02-01 00:48:14 +0000728AttributeSet::iterator AttributeSet::end(unsigned Idx) const {
Bill Wendling16c4b3c2013-01-31 23:53:05 +0000729 if (!pImpl)
730 return ArrayRef<Attribute>().end();
Bill Wendling30d2c762013-02-01 00:13:50 +0000731 return pImpl->end(Idx);
Bill Wendling16c4b3c2013-01-31 23:53:05 +0000732}
733
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000734//===----------------------------------------------------------------------===//
735// AttributeSet Introspection Methods
736//===----------------------------------------------------------------------===//
737
738/// \brief Return the number of slots used in this attribute list. This is the
739/// number of arguments that have an attribute set on them (including the
740/// function itself).
741unsigned AttributeSet::getNumSlots() const {
742 return pImpl ? pImpl->getNumAttributes() : 0;
743}
744
745uint64_t AttributeSet::getSlotIndex(unsigned Slot) const {
746 assert(pImpl && Slot < pImpl->getNumAttributes() &&
747 "Slot # out of range!");
748 return pImpl->getSlotIndex(Slot);
749}
750
751AttributeSet AttributeSet::getSlotAttributes(unsigned Slot) const {
752 assert(pImpl && Slot < pImpl->getNumAttributes() &&
753 "Slot # out of range!");
754 return pImpl->getSlotAttributes(Slot);
755}
756
757uint64_t AttributeSet::Raw(unsigned Index) const {
758 // FIXME: Remove this.
759 return pImpl ? pImpl->Raw(Index) : 0;
760}
761
762void AttributeSet::dump() const {
763 dbgs() << "PAL[\n";
764
765 for (unsigned i = 0, e = getNumSlots(); i < e; ++i) {
766 uint64_t Index = getSlotIndex(i);
767 dbgs() << " { ";
768 if (Index == ~0U)
769 dbgs() << "~0U";
770 else
771 dbgs() << Index;
772 dbgs() << " => " << getAsString(Index) << " }\n";
773 }
774
775 dbgs() << "]\n";
776}
777
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000778//===----------------------------------------------------------------------===//
Bill Wendling03198882013-01-04 23:27:34 +0000779// AttrBuilder Method Implementations
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000780//===----------------------------------------------------------------------===//
781
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000782AttrBuilder::AttrBuilder(AttributeSet AS, unsigned Idx)
783 : Alignment(0), StackAlignment(0) {
Bill Wendlingec258982013-01-27 21:23:46 +0000784 AttributeSetImpl *pImpl = AS.pImpl;
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000785 if (!pImpl) return;
786
Bill Wendling73bc4522013-01-28 00:21:34 +0000787 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I) {
788 if (pImpl->getSlotIndex(I) != Idx) continue;
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000789
Bill Wendling383da6b2013-01-30 21:22:59 +0000790 for (AttributeSetImpl::const_iterator II = pImpl->begin(I),
Bill Wendling73bc4522013-01-28 00:21:34 +0000791 IE = pImpl->end(I); II != IE; ++II)
Bill Wendling39da0782013-01-31 23:38:01 +0000792 addAttribute(*II);
Bill Wendling73bc4522013-01-28 00:21:34 +0000793
794 break;
795 }
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000796}
797
Bill Wendling03198882013-01-04 23:27:34 +0000798void AttrBuilder::clear() {
799 Attrs.clear();
800 Alignment = StackAlignment = 0;
801}
802
803AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
Bill Wendling169d5272013-01-31 23:16:25 +0000804 assert(Val != Attribute::Alignment && Val != Attribute::StackAlignment &&
805 "Adding alignment attribute without adding alignment value!");
Bill Wendling03198882013-01-04 23:27:34 +0000806 Attrs.insert(Val);
Bill Wendling3a106e62012-10-09 19:01:18 +0000807 return *this;
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000808}
809
Bill Wendling39da0782013-01-31 23:38:01 +0000810AttrBuilder &AttrBuilder::addAttribute(Attribute Attr) {
Bill Wendling169d5272013-01-31 23:16:25 +0000811 ConstantInt *Kind = cast<ConstantInt>(Attr.getAttributeKind());
812 Attribute::AttrKind KindVal = Attribute::AttrKind(Kind->getZExtValue());
813 Attrs.insert(KindVal);
Bill Wendling49f60602013-01-28 05:23:28 +0000814
Bill Wendling169d5272013-01-31 23:16:25 +0000815 if (KindVal == Attribute::Alignment)
Bill Wendling49f60602013-01-28 05:23:28 +0000816 Alignment = Attr.getAlignment();
Bill Wendling169d5272013-01-31 23:16:25 +0000817 else if (KindVal == Attribute::StackAlignment)
Bill Wendling49f60602013-01-28 05:23:28 +0000818 StackAlignment = Attr.getStackAlignment();
819 return *this;
820}
821
Bill Wendling39da0782013-01-31 23:38:01 +0000822AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
823 Attrs.erase(Val);
824
825 if (Val == Attribute::Alignment)
826 Alignment = 0;
827 else if (Val == Attribute::StackAlignment)
828 StackAlignment = 0;
829
830 return *this;
831}
832
Bill Wendlinge7436542013-01-30 23:07:40 +0000833AttrBuilder &AttrBuilder::removeAttributes(AttributeSet A, uint64_t Index) {
Bill Wendling30d2c762013-02-01 00:13:50 +0000834 unsigned Idx = ~0U;
835 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
836 if (A.getSlotIndex(I) == Index) {
837 Idx = I;
838 break;
Bill Wendling49f60602013-01-28 05:23:28 +0000839 }
Bill Wendling30d2c762013-02-01 00:13:50 +0000840
841 assert(Idx != ~0U && "Couldn't find index in AttributeSet!");
842
843 for (AttributeSet::iterator I = A.begin(Idx), E = A.end(Idx); I != E; ++I) {
844 ConstantInt *CI = cast<ConstantInt>(I->getAttributeKind());
845 Attribute::AttrKind Kind = Attribute::AttrKind(CI->getZExtValue());
846 Attrs.erase(Kind);
847
848 if (Kind == Attribute::Alignment)
849 Alignment = 0;
850 else if (Kind == Attribute::StackAlignment)
851 StackAlignment = 0;
Bill Wendling49f60602013-01-28 05:23:28 +0000852 }
853
854 return *this;
855}
856
Bill Wendling702cc912012-10-15 20:35:56 +0000857AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
Bill Wendlingda3f9d82012-10-14 03:58:29 +0000858 if (Align == 0) return *this;
Bill Wendling03198882013-01-04 23:27:34 +0000859
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000860 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
861 assert(Align <= 0x40000000 && "Alignment too large.");
Bill Wendling03198882013-01-04 23:27:34 +0000862
863 Attrs.insert(Attribute::Alignment);
864 Alignment = Align;
Bill Wendlingda3f9d82012-10-14 03:58:29 +0000865 return *this;
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000866}
867
Bill Wendling03198882013-01-04 23:27:34 +0000868AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
869 // Default alignment, allow the target to define how to align it.
870 if (Align == 0) return *this;
871
872 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
873 assert(Align <= 0x100 && "Alignment too large.");
874
875 Attrs.insert(Attribute::StackAlignment);
876 StackAlignment = Align;
877 return *this;
878}
879
Bill Wendling22bd6412013-01-03 01:54:39 +0000880bool AttrBuilder::contains(Attribute::AttrKind A) const {
Bill Wendling03198882013-01-04 23:27:34 +0000881 return Attrs.count(A);
Bill Wendling7d2f2492012-10-10 07:36:45 +0000882}
883
Bill Wendling702cc912012-10-15 20:35:56 +0000884bool AttrBuilder::hasAttributes() const {
Bill Wendling03198882013-01-04 23:27:34 +0000885 return !Attrs.empty();
Bill Wendlingf385f4c2012-10-08 23:27:46 +0000886}
Bill Wendling60507d52013-01-04 20:54:35 +0000887
Bill Wendlinge7436542013-01-30 23:07:40 +0000888bool AttrBuilder::hasAttributes(AttributeSet A, uint64_t Index) const {
889 return Raw() & A.Raw(Index);
Bill Wendling8831c062012-10-09 00:01:21 +0000890}
Bill Wendling60507d52013-01-04 20:54:35 +0000891
Bill Wendling702cc912012-10-15 20:35:56 +0000892bool AttrBuilder::hasAlignmentAttr() const {
Bill Wendling03198882013-01-04 23:27:34 +0000893 return Alignment != 0;
Bill Wendlingf385f4c2012-10-08 23:27:46 +0000894}
895
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000896bool AttrBuilder::operator==(const AttrBuilder &B) {
897 SmallVector<Attribute::AttrKind, 8> This(Attrs.begin(), Attrs.end());
898 SmallVector<Attribute::AttrKind, 8> That(B.Attrs.begin(), B.Attrs.end());
899 return This == That;
900}
901
902AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) {
Bill Wendling8232ece2013-01-29 01:43:29 +0000903 if (!Val) return *this;
904
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000905 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
906 I = Attribute::AttrKind(I + 1)) {
907 if (uint64_t A = (Val & AttributeImpl::getAttrMask(I))) {
908 Attrs.insert(I);
909
910 if (I == Attribute::Alignment)
911 Alignment = 1ULL << ((A >> 16) - 1);
912 else if (I == Attribute::StackAlignment)
913 StackAlignment = 1ULL << ((A >> 26)-1);
914 }
915 }
916
917 return *this;
918}
919
Bill Wendling1db9b692013-01-09 23:36:50 +0000920uint64_t AttrBuilder::Raw() const {
Bill Wendling03198882013-01-04 23:27:34 +0000921 uint64_t Mask = 0;
922
923 for (DenseSet<Attribute::AttrKind>::const_iterator I = Attrs.begin(),
924 E = Attrs.end(); I != E; ++I) {
925 Attribute::AttrKind Kind = *I;
926
927 if (Kind == Attribute::Alignment)
928 Mask |= (Log2_32(Alignment) + 1) << 16;
929 else if (Kind == Attribute::StackAlignment)
930 Mask |= (Log2_32(StackAlignment) + 1) << 26;
931 else
932 Mask |= AttributeImpl::getAttrMask(Kind);
933 }
934
935 return Mask;
Bill Wendlingf385f4c2012-10-08 23:27:46 +0000936}
937
Bill Wendling8e47daf2013-01-25 23:09:36 +0000938//===----------------------------------------------------------------------===//
939// AttributeFuncs Function Defintions
940//===----------------------------------------------------------------------===//
941
Bill Wendling7beee282013-02-01 01:04:27 +0000942/// \brief Which attributes cannot be applied to a type.
Bill Wendlinge7436542013-01-30 23:07:40 +0000943AttributeSet AttributeFuncs::typeIncompatible(Type *Ty, uint64_t Index) {
Bill Wendling8e47daf2013-01-25 23:09:36 +0000944 AttrBuilder Incompatible;
945
946 if (!Ty->isIntegerTy())
947 // Attribute that only apply to integers.
948 Incompatible.addAttribute(Attribute::SExt)
949 .addAttribute(Attribute::ZExt);
950
951 if (!Ty->isPointerTy())
952 // Attribute that only apply to pointers.
953 Incompatible.addAttribute(Attribute::ByVal)
954 .addAttribute(Attribute::Nest)
955 .addAttribute(Attribute::NoAlias)
956 .addAttribute(Attribute::NoCapture)
957 .addAttribute(Attribute::StructRet);
958
Bill Wendlinge7436542013-01-30 23:07:40 +0000959 return AttributeSet::get(Ty->getContext(), Index, Incompatible);
Bill Wendling8e47daf2013-01-25 23:09:36 +0000960}
961
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000962/// \brief This returns an integer containing an encoding of all the LLVM
963/// attributes found in the given attribute bitset. Any change to this encoding
964/// is a breaking change to bitcode compatibility.
Bill Wendling8232ece2013-01-29 01:43:29 +0000965/// N.B. This should be used only by the bitcode reader!
Bill Wendling8e47daf2013-01-25 23:09:36 +0000966uint64_t AttributeFuncs::encodeLLVMAttributesForBitcode(AttributeSet Attrs,
967 unsigned Index) {
968 // FIXME: It doesn't make sense to store the alignment information as an
969 // expanded out value, we should store it as a log2 value. However, we can't
970 // just change that here without breaking bitcode compatibility. If this ever
971 // becomes a problem in practice, we should introduce new tag numbers in the
972 // bitcode file and have those tags use a more efficiently encoded alignment
973 // field.
974
975 // Store the alignment in the bitcode as a 16-bit raw value instead of a 5-bit
976 // log2 encoded value. Shift the bits above the alignment up by 11 bits.
977 uint64_t EncodedAttrs = Attrs.Raw(Index) & 0xffff;
978 if (Attrs.hasAttribute(Index, Attribute::Alignment))
979 EncodedAttrs |= Attrs.getParamAlignment(Index) << 16;
980 EncodedAttrs |= (Attrs.Raw(Index) & (0xffffULL << 21)) << 11;
981 return EncodedAttrs;
982}
983
Bill Wendling8232ece2013-01-29 01:43:29 +0000984/// \brief This fills an AttrBuilder object with the LLVM attributes that have
985/// been decoded from the given integer. This function must stay in sync with
986/// 'encodeLLVMAttributesForBitcode'.
987/// N.B. This should be used only by the bitcode reader!
988void AttributeFuncs::decodeLLVMAttributesForBitcode(LLVMContext &C,
989 AttrBuilder &B,
990 uint64_t EncodedAttrs) {
Bill Wendling8e47daf2013-01-25 23:09:36 +0000991 // The alignment is stored as a 16-bit raw value from bits 31--16. We shift
992 // the bits above 31 down by 11 bits.
993 unsigned Alignment = (EncodedAttrs & (0xffffULL << 16)) >> 16;
994 assert((!Alignment || isPowerOf2_32(Alignment)) &&
995 "Alignment must be a power of two.");
996
Bill Wendling8e47daf2013-01-25 23:09:36 +0000997 if (Alignment)
998 B.addAlignmentAttr(Alignment);
Bill Wendling606c8e32013-01-29 03:20:31 +0000999 B.addRawValue(((EncodedAttrs & (0xffffULL << 32)) >> 11) |
1000 (EncodedAttrs & 0xffff));
Bill Wendling8e47daf2013-01-25 23:09:36 +00001001}