blob: f8ca9f1f0420ca51c1b5c4411ecff9d645ec01ff [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
Bill Wendling5a4041e2013-02-01 22:32:30 +000087Constant *Attribute::getAttributeValues() const {
88 return pImpl ? pImpl->getAttributeValues() : 0;
Bill Wendling6dc37812013-01-29 20:45:34 +000089}
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"
Bill Wendling5a4041e2013-02-01 22:32:30 +0000189 // "kind" = ( "value1" "value2" "value3" )
Bill Wendling14292a62013-01-31 20:59:05 +0000190 //
191 if (ConstantDataArray *CDA =
192 dyn_cast<ConstantDataArray>(pImpl->getAttributeKind())) {
193 std::string Result;
194 Result += '\"' + CDA->getAsString().str() + '"';
195
Bill Wendling5a4041e2013-02-01 22:32:30 +0000196 Constant *Vals = pImpl->getAttributeValues();
197 if (!Vals) return Result;
198
199 // FIXME: This should support more than just ConstantDataArrays. Also,
200 // support a vector of attribute values.
201
Bill Wendling14292a62013-01-31 20:59:05 +0000202 Result += " = ";
Bill Wendling5a4041e2013-02-01 22:32:30 +0000203 Result += '\"' + cast<ConstantDataArray>(Vals)->getAsString().str() + '"';
204
Bill Wendling7beee282013-02-01 01:04:27 +0000205 return Result;
Bill Wendling14292a62013-01-31 20:59:05 +0000206 }
Bill Wendling606c8e32013-01-29 03:20:31 +0000207
208 llvm_unreachable("Unknown attribute");
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000209}
210
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000211bool Attribute::operator==(AttrKind K) const {
Bill Wendling14292a62013-01-31 20:59:05 +0000212 return (pImpl && *pImpl == K) || (!pImpl && K == None);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000213}
214bool Attribute::operator!=(AttrKind K) const {
215 return !(*this == K);
216}
217
218bool Attribute::operator<(Attribute A) const {
219 if (!pImpl && !A.pImpl) return false;
220 if (!pImpl) return true;
221 if (!A.pImpl) return false;
222 return *pImpl < *A.pImpl;
223}
224
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000225//===----------------------------------------------------------------------===//
226// AttributeImpl Definition
227//===----------------------------------------------------------------------===//
228
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000229bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
Bill Wendling169d5272013-01-31 23:16:25 +0000230 if (ConstantInt *CI = dyn_cast<ConstantInt>(Kind))
231 return CI->getZExtValue() == A;
232 return false;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000233}
234
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000235uint64_t AttributeImpl::getAlignment() const {
Bill Wendling169d5272013-01-31 23:16:25 +0000236 assert(hasAttribute(Attribute::Alignment) &&
237 "Trying to retrieve the alignment from a non-alignment attr!");
Bill Wendling5a4041e2013-02-01 22:32:30 +0000238 return cast<ConstantInt>(Values)->getZExtValue();
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000239}
240
241uint64_t AttributeImpl::getStackAlignment() const {
Bill Wendling169d5272013-01-31 23:16:25 +0000242 assert(hasAttribute(Attribute::StackAlignment) &&
243 "Trying to retrieve the stack alignment from a non-alignment attr!");
Bill Wendling5a4041e2013-02-01 22:32:30 +0000244 return cast<ConstantInt>(Values)->getZExtValue();
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000245}
246
Bill Wendling9f175f82013-01-29 20:37:10 +0000247bool AttributeImpl::operator==(Attribute::AttrKind kind) const {
248 if (ConstantInt *CI = dyn_cast<ConstantInt>(Kind))
249 return CI->getZExtValue() == kind;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000250 return false;
251}
Bill Wendling9f175f82013-01-29 20:37:10 +0000252bool AttributeImpl::operator!=(Attribute::AttrKind kind) const {
253 return !(*this == kind);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000254}
255
Bill Wendling9f175f82013-01-29 20:37:10 +0000256bool AttributeImpl::operator==(StringRef kind) const {
257 if (ConstantDataArray *CDA = dyn_cast<ConstantDataArray>(Kind))
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000258 if (CDA->isString())
Bill Wendling9f175f82013-01-29 20:37:10 +0000259 return CDA->getAsString() == kind;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000260 return false;
261}
262
Bill Wendling9f175f82013-01-29 20:37:10 +0000263bool AttributeImpl::operator!=(StringRef kind) const {
264 return !(*this == kind);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000265}
266
267bool AttributeImpl::operator<(const AttributeImpl &AI) const {
Bill Wendling7beee282013-02-01 01:04:27 +0000268 // This sorts the attributes with Attribute::AttrKinds coming first (sorted
269 // relative to their enum value) and then strings.
270
Bill Wendling9f175f82013-01-29 20:37:10 +0000271 if (!Kind && !AI.Kind) return false;
272 if (!Kind && AI.Kind) return true;
273 if (Kind && !AI.Kind) return false;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000274
Bill Wendling9f175f82013-01-29 20:37:10 +0000275 ConstantInt *ThisCI = dyn_cast<ConstantInt>(Kind);
276 ConstantInt *ThatCI = dyn_cast<ConstantInt>(AI.Kind);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000277
Bill Wendling9f175f82013-01-29 20:37:10 +0000278 ConstantDataArray *ThisCDA = dyn_cast<ConstantDataArray>(Kind);
279 ConstantDataArray *ThatCDA = dyn_cast<ConstantDataArray>(AI.Kind);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000280
281 if (ThisCI && ThatCI)
282 return ThisCI->getZExtValue() < ThatCI->getZExtValue();
283
284 if (ThisCI && ThatCDA)
285 return true;
286
287 if (ThisCDA && ThatCI)
288 return false;
289
290 return ThisCDA->getAsString() < ThatCDA->getAsString();
291}
292
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000293uint64_t AttributeImpl::getAttrMask(Attribute::AttrKind Val) {
294 // FIXME: Remove this.
295 switch (Val) {
296 case Attribute::EndAttrKinds:
297 case Attribute::AttrKindEmptyKey:
298 case Attribute::AttrKindTombstoneKey:
299 llvm_unreachable("Synthetic enumerators which should never get here");
300
301 case Attribute::None: return 0;
302 case Attribute::ZExt: return 1 << 0;
303 case Attribute::SExt: return 1 << 1;
304 case Attribute::NoReturn: return 1 << 2;
305 case Attribute::InReg: return 1 << 3;
306 case Attribute::StructRet: return 1 << 4;
307 case Attribute::NoUnwind: return 1 << 5;
308 case Attribute::NoAlias: return 1 << 6;
309 case Attribute::ByVal: return 1 << 7;
310 case Attribute::Nest: return 1 << 8;
311 case Attribute::ReadNone: return 1 << 9;
312 case Attribute::ReadOnly: return 1 << 10;
313 case Attribute::NoInline: return 1 << 11;
314 case Attribute::AlwaysInline: return 1 << 12;
315 case Attribute::OptimizeForSize: return 1 << 13;
316 case Attribute::StackProtect: return 1 << 14;
317 case Attribute::StackProtectReq: return 1 << 15;
318 case Attribute::Alignment: return 31 << 16;
319 case Attribute::NoCapture: return 1 << 21;
320 case Attribute::NoRedZone: return 1 << 22;
321 case Attribute::NoImplicitFloat: return 1 << 23;
322 case Attribute::Naked: return 1 << 24;
323 case Attribute::InlineHint: return 1 << 25;
324 case Attribute::StackAlignment: return 7 << 26;
325 case Attribute::ReturnsTwice: return 1 << 29;
326 case Attribute::UWTable: return 1 << 30;
327 case Attribute::NonLazyBind: return 1U << 31;
328 case Attribute::AddressSafety: return 1ULL << 32;
329 case Attribute::MinSize: return 1ULL << 33;
330 case Attribute::NoDuplicate: return 1ULL << 34;
331 case Attribute::StackProtectStrong: return 1ULL << 35;
332 }
333 llvm_unreachable("Unsupported attribute type");
334}
335
336//===----------------------------------------------------------------------===//
337// AttributeSetNode Definition
338//===----------------------------------------------------------------------===//
339
340AttributeSetNode *AttributeSetNode::get(LLVMContext &C,
341 ArrayRef<Attribute> Attrs) {
342 if (Attrs.empty())
343 return 0;
344
345 // Otherwise, build a key to look up the existing attributes.
346 LLVMContextImpl *pImpl = C.pImpl;
347 FoldingSetNodeID ID;
348
349 SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end());
350 std::sort(SortedAttrs.begin(), SortedAttrs.end());
351
352 for (SmallVectorImpl<Attribute>::iterator I = SortedAttrs.begin(),
353 E = SortedAttrs.end(); I != E; ++I)
354 I->Profile(ID);
355
356 void *InsertPoint;
357 AttributeSetNode *PA =
358 pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint);
359
360 // If we didn't find any existing attributes of the same shape then create a
361 // new one and insert it.
362 if (!PA) {
363 PA = new AttributeSetNode(SortedAttrs);
364 pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint);
365 }
366
367 // Return the AttributesListNode that we found or created.
368 return PA;
369}
370
Bill Wendling606c8e32013-01-29 03:20:31 +0000371bool AttributeSetNode::hasAttribute(Attribute::AttrKind Kind) const {
372 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
373 E = AttrList.end(); I != E; ++I)
NAKAMURA Takumieddab152013-01-31 03:47:28 +0000374 if (I->hasAttribute(Kind))
Bill Wendling606c8e32013-01-29 03:20:31 +0000375 return true;
376 return false;
377}
378
379unsigned AttributeSetNode::getAlignment() const {
380 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
381 E = AttrList.end(); I != E; ++I)
NAKAMURA Takumieddab152013-01-31 03:47:28 +0000382 if (I->hasAttribute(Attribute::Alignment))
Bill Wendling606c8e32013-01-29 03:20:31 +0000383 return I->getAlignment();
384 return 0;
385}
386
387unsigned AttributeSetNode::getStackAlignment() const {
388 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
389 E = AttrList.end(); I != E; ++I)
NAKAMURA Takumieddab152013-01-31 03:47:28 +0000390 if (I->hasAttribute(Attribute::StackAlignment))
Bill Wendling606c8e32013-01-29 03:20:31 +0000391 return I->getStackAlignment();
392 return 0;
393}
394
395std::string AttributeSetNode::getAsString() const {
396 std::string Str = "";
397 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
Bill Wendling7beee282013-02-01 01:04:27 +0000398 E = AttrList.end(); I != E; ) {
Bill Wendling606c8e32013-01-29 03:20:31 +0000399 Str += I->getAsString();
Bill Wendling7beee282013-02-01 01:04:27 +0000400 if (++I != E) Str += " ";
Bill Wendling606c8e32013-01-29 03:20:31 +0000401 }
402 return Str;
403}
404
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000405//===----------------------------------------------------------------------===//
406// AttributeSetImpl Definition
407//===----------------------------------------------------------------------===//
408
409uint64_t AttributeSetImpl::Raw(uint64_t Index) const {
410 for (unsigned I = 0, E = getNumAttributes(); I != E; ++I) {
411 if (getSlotIndex(I) != Index) continue;
412 const AttributeSetNode *ASN = AttrNodes[I].second;
413 AttrBuilder B;
414
415 for (AttributeSetNode::const_iterator II = ASN->begin(),
416 IE = ASN->end(); II != IE; ++II)
Bill Wendling39da0782013-01-31 23:38:01 +0000417 B.addAttribute(*II);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000418 return B.Raw();
419 }
420
421 return 0;
422}
423
424//===----------------------------------------------------------------------===//
425// AttributeSet Construction and Mutation Methods
426//===----------------------------------------------------------------------===//
427
Bill Wendling8232ece2013-01-29 01:43:29 +0000428AttributeSet
429AttributeSet::getImpl(LLVMContext &C,
430 ArrayRef<std::pair<unsigned, AttributeSetNode*> > Attrs) {
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000431 LLVMContextImpl *pImpl = C.pImpl;
432 FoldingSetNodeID ID;
433 AttributeSetImpl::Profile(ID, Attrs);
434
435 void *InsertPoint;
436 AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
437
438 // If we didn't find any existing attributes of the same shape then
439 // create a new one and insert it.
440 if (!PA) {
441 PA = new AttributeSetImpl(C, Attrs);
442 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
443 }
444
445 // Return the AttributesList that we found or created.
446 return AttributeSet(PA);
447}
448
449AttributeSet AttributeSet::get(LLVMContext &C,
450 ArrayRef<std::pair<unsigned, Attribute> > Attrs){
451 // If there are no attributes then return a null AttributesList pointer.
452 if (Attrs.empty())
453 return AttributeSet();
454
455#ifndef NDEBUG
456 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
457 assert((!i || Attrs[i-1].first <= Attrs[i].first) &&
458 "Misordered Attributes list!");
Bill Wendling82aea642013-01-31 06:22:35 +0000459 assert(Attrs[i].second != Attribute::None &&
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000460 "Pointless attribute!");
461 }
462#endif
463
464 // Create a vector if (unsigned, AttributeSetNode*) pairs from the attributes
465 // list.
466 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrPairVec;
467 for (ArrayRef<std::pair<unsigned, Attribute> >::iterator I = Attrs.begin(),
468 E = Attrs.end(); I != E; ) {
469 unsigned Index = I->first;
470 SmallVector<Attribute, 4> AttrVec;
NAKAMURA Takumi3ba51ce2013-01-29 15:18:16 +0000471 while (I != E && I->first == Index) {
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000472 AttrVec.push_back(I->second);
473 ++I;
474 }
475
476 AttrPairVec.push_back(std::make_pair(Index,
477 AttributeSetNode::get(C, AttrVec)));
478 }
479
480 return getImpl(C, AttrPairVec);
481}
482
483AttributeSet AttributeSet::get(LLVMContext &C,
484 ArrayRef<std::pair<unsigned,
485 AttributeSetNode*> > Attrs) {
486 // If there are no attributes then return a null AttributesList pointer.
487 if (Attrs.empty())
488 return AttributeSet();
489
490 return getImpl(C, Attrs);
491}
492
493AttributeSet AttributeSet::get(LLVMContext &C, unsigned Idx, AttrBuilder &B) {
494 if (!B.hasAttributes())
495 return AttributeSet();
Bill Wendling8fbc0c22013-01-29 01:02:03 +0000496
497 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
498 for (AttrBuilder::iterator I = B.begin(), E = B.end(); I != E; ++I) {
499 Attribute::AttrKind Kind = *I;
500 if (Kind == Attribute::Alignment)
501 Attrs.push_back(std::make_pair(Idx, Attribute::
502 getWithAlignment(C, B.getAlignment())));
503 else if (Kind == Attribute::StackAlignment)
504 Attrs.push_back(std::make_pair(Idx, Attribute::
505 getWithStackAlignment(C, B.getStackAlignment())));
506 else
507 Attrs.push_back(std::make_pair(Idx, Attribute::get(C, Kind)));
508 }
509
510 return get(C, Attrs);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000511}
512
513AttributeSet AttributeSet::get(LLVMContext &C, unsigned Idx,
514 ArrayRef<Attribute::AttrKind> Kind) {
515 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
516 for (ArrayRef<Attribute::AttrKind>::iterator I = Kind.begin(),
517 E = Kind.end(); I != E; ++I)
518 Attrs.push_back(std::make_pair(Idx, Attribute::get(C, *I)));
519 return get(C, Attrs);
520}
521
522AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<AttributeSet> Attrs) {
523 if (Attrs.empty()) return AttributeSet();
524
525 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrNodeVec;
526 for (unsigned I = 0, E = Attrs.size(); I != E; ++I) {
527 AttributeSet AS = Attrs[I];
528 if (!AS.pImpl) continue;
529 AttrNodeVec.append(AS.pImpl->AttrNodes.begin(), AS.pImpl->AttrNodes.end());
530 }
531
532 return getImpl(C, AttrNodeVec);
533}
534
535AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Idx,
536 Attribute::AttrKind Attr) const {
537 return addAttributes(C, Idx, AttributeSet::get(C, Idx, Attr));
538}
539
540AttributeSet AttributeSet::addAttributes(LLVMContext &C, unsigned Idx,
541 AttributeSet Attrs) const {
542 if (!pImpl) return Attrs;
543 if (!Attrs.pImpl) return *this;
544
545#ifndef NDEBUG
546 // FIXME it is not obvious how this should work for alignment. For now, say
547 // we can't change a known alignment.
548 unsigned OldAlign = getParamAlignment(Idx);
549 unsigned NewAlign = Attrs.getParamAlignment(Idx);
550 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
551 "Attempt to change alignment!");
552#endif
553
554 // Add the attribute slots before the one we're trying to add.
555 SmallVector<AttributeSet, 4> AttrSet;
556 uint64_t NumAttrs = pImpl->getNumAttributes();
557 AttributeSet AS;
558 uint64_t LastIndex = 0;
559 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
560 if (getSlotIndex(I) >= Idx) {
561 if (getSlotIndex(I) == Idx) AS = getSlotAttributes(LastIndex++);
562 break;
563 }
564 LastIndex = I + 1;
565 AttrSet.push_back(getSlotAttributes(I));
566 }
567
568 // Now add the attribute into the correct slot. There may already be an
569 // AttributeSet there.
570 AttrBuilder B(AS, Idx);
571
572 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
573 if (Attrs.getSlotIndex(I) == Idx) {
574 for (AttributeSetImpl::const_iterator II = Attrs.pImpl->begin(I),
575 IE = Attrs.pImpl->end(I); II != IE; ++II)
Bill Wendling39da0782013-01-31 23:38:01 +0000576 B.addAttribute(*II);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000577 break;
578 }
579
580 AttrSet.push_back(AttributeSet::get(C, Idx, B));
581
582 // Add the remaining attribute slots.
583 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
584 AttrSet.push_back(getSlotAttributes(I));
585
586 return get(C, AttrSet);
587}
588
589AttributeSet AttributeSet::removeAttribute(LLVMContext &C, unsigned Idx,
590 Attribute::AttrKind Attr) const {
591 return removeAttributes(C, Idx, AttributeSet::get(C, Idx, Attr));
592}
593
594AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Idx,
595 AttributeSet Attrs) const {
596 if (!pImpl) return AttributeSet();
597 if (!Attrs.pImpl) return *this;
598
599#ifndef NDEBUG
600 // FIXME it is not obvious how this should work for alignment.
601 // For now, say we can't pass in alignment, which no current use does.
602 assert(!Attrs.hasAttribute(Idx, Attribute::Alignment) &&
603 "Attempt to change alignment!");
604#endif
605
606 // Add the attribute slots before the one we're trying to add.
607 SmallVector<AttributeSet, 4> AttrSet;
608 uint64_t NumAttrs = pImpl->getNumAttributes();
609 AttributeSet AS;
610 uint64_t LastIndex = 0;
611 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
612 if (getSlotIndex(I) >= Idx) {
613 if (getSlotIndex(I) == Idx) AS = getSlotAttributes(LastIndex++);
614 break;
615 }
616 LastIndex = I + 1;
617 AttrSet.push_back(getSlotAttributes(I));
618 }
619
Bill Wendlinge7436542013-01-30 23:07:40 +0000620 // Now remove the attribute from the correct slot. There may already be an
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000621 // AttributeSet there.
622 AttrBuilder B(AS, Idx);
623
624 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
625 if (Attrs.getSlotIndex(I) == Idx) {
Bill Wendlinge7436542013-01-30 23:07:40 +0000626 B.removeAttributes(Attrs.pImpl->getSlotAttributes(I), Idx);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000627 break;
628 }
629
630 AttrSet.push_back(AttributeSet::get(C, Idx, B));
631
632 // Add the remaining attribute slots.
633 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
634 AttrSet.push_back(getSlotAttributes(I));
635
636 return get(C, AttrSet);
637}
638
639//===----------------------------------------------------------------------===//
640// AttributeSet Accessor Methods
641//===----------------------------------------------------------------------===//
642
643AttributeSet AttributeSet::getParamAttributes(unsigned Idx) const {
644 return pImpl && hasAttributes(Idx) ?
645 AttributeSet::get(pImpl->getContext(),
Bill Wendling606c8e32013-01-29 03:20:31 +0000646 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000647 std::make_pair(Idx, getAttributes(Idx)))) :
648 AttributeSet();
649}
650
651AttributeSet AttributeSet::getRetAttributes() const {
652 return pImpl && hasAttributes(ReturnIndex) ?
653 AttributeSet::get(pImpl->getContext(),
Bill Wendling606c8e32013-01-29 03:20:31 +0000654 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000655 std::make_pair(ReturnIndex,
656 getAttributes(ReturnIndex)))) :
657 AttributeSet();
658}
659
660AttributeSet AttributeSet::getFnAttributes() const {
661 return pImpl && hasAttributes(FunctionIndex) ?
662 AttributeSet::get(pImpl->getContext(),
Bill Wendling606c8e32013-01-29 03:20:31 +0000663 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000664 std::make_pair(FunctionIndex,
665 getAttributes(FunctionIndex)))) :
666 AttributeSet();
667}
668
669bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{
Bill Wendling606c8e32013-01-29 03:20:31 +0000670 AttributeSetNode *ASN = getAttributes(Index);
671 return ASN ? ASN->hasAttribute(Kind) : false;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000672}
673
674bool AttributeSet::hasAttributes(unsigned Index) const {
Bill Wendling606c8e32013-01-29 03:20:31 +0000675 AttributeSetNode *ASN = getAttributes(Index);
676 return ASN ? ASN->hasAttributes() : false;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000677}
678
679/// \brief Return true if the specified attribute is set for at least one
680/// parameter or for the return value.
681bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr) const {
682 if (pImpl == 0) return false;
683
684 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
685 for (AttributeSetImpl::const_iterator II = pImpl->begin(I),
686 IE = pImpl->end(I); II != IE; ++II)
NAKAMURA Takumieddab152013-01-31 03:47:28 +0000687 if (II->hasAttribute(Attr))
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000688 return true;
689
690 return false;
691}
692
Bill Wendling606c8e32013-01-29 03:20:31 +0000693unsigned AttributeSet::getParamAlignment(unsigned Index) const {
694 AttributeSetNode *ASN = getAttributes(Index);
695 return ASN ? ASN->getAlignment() : 0;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000696}
697
698unsigned AttributeSet::getStackAlignment(unsigned Index) const {
Bill Wendling606c8e32013-01-29 03:20:31 +0000699 AttributeSetNode *ASN = getAttributes(Index);
700 return ASN ? ASN->getStackAlignment() : 0;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000701}
702
703std::string AttributeSet::getAsString(unsigned Index) const {
Bill Wendling606c8e32013-01-29 03:20:31 +0000704 AttributeSetNode *ASN = getAttributes(Index);
705 return ASN ? ASN->getAsString() : std::string("");
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000706}
707
708/// \brief The attributes for the specified index are returned.
Bill Wendling606c8e32013-01-29 03:20:31 +0000709AttributeSetNode *AttributeSet::getAttributes(unsigned Idx) const {
710 if (!pImpl) return 0;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000711
Bill Wendling606c8e32013-01-29 03:20:31 +0000712 // Loop through to find the attribute node we want.
713 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
714 if (pImpl->getSlotIndex(I) == Idx)
715 return pImpl->getSlotNode(I);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000716
Bill Wendling606c8e32013-01-29 03:20:31 +0000717 return 0;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000718}
719
Bill Wendlingf715dbd2013-02-01 00:48:14 +0000720AttributeSet::iterator AttributeSet::begin(unsigned Idx) const {
Bill Wendling16c4b3c2013-01-31 23:53:05 +0000721 if (!pImpl)
722 return ArrayRef<Attribute>().begin();
723 return pImpl->begin(Idx);
724}
725
Bill Wendlingf715dbd2013-02-01 00:48:14 +0000726AttributeSet::iterator AttributeSet::end(unsigned Idx) const {
Bill Wendling16c4b3c2013-01-31 23:53:05 +0000727 if (!pImpl)
728 return ArrayRef<Attribute>().end();
Bill Wendling30d2c762013-02-01 00:13:50 +0000729 return pImpl->end(Idx);
Bill Wendling16c4b3c2013-01-31 23:53:05 +0000730}
731
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000732//===----------------------------------------------------------------------===//
733// AttributeSet Introspection Methods
734//===----------------------------------------------------------------------===//
735
736/// \brief Return the number of slots used in this attribute list. This is the
737/// number of arguments that have an attribute set on them (including the
738/// function itself).
739unsigned AttributeSet::getNumSlots() const {
740 return pImpl ? pImpl->getNumAttributes() : 0;
741}
742
743uint64_t AttributeSet::getSlotIndex(unsigned Slot) const {
744 assert(pImpl && Slot < pImpl->getNumAttributes() &&
745 "Slot # out of range!");
746 return pImpl->getSlotIndex(Slot);
747}
748
749AttributeSet AttributeSet::getSlotAttributes(unsigned Slot) const {
750 assert(pImpl && Slot < pImpl->getNumAttributes() &&
751 "Slot # out of range!");
752 return pImpl->getSlotAttributes(Slot);
753}
754
755uint64_t AttributeSet::Raw(unsigned Index) const {
756 // FIXME: Remove this.
757 return pImpl ? pImpl->Raw(Index) : 0;
758}
759
760void AttributeSet::dump() const {
761 dbgs() << "PAL[\n";
762
763 for (unsigned i = 0, e = getNumSlots(); i < e; ++i) {
764 uint64_t Index = getSlotIndex(i);
765 dbgs() << " { ";
766 if (Index == ~0U)
767 dbgs() << "~0U";
768 else
769 dbgs() << Index;
770 dbgs() << " => " << getAsString(Index) << " }\n";
771 }
772
773 dbgs() << "]\n";
774}
775
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000776//===----------------------------------------------------------------------===//
Bill Wendling03198882013-01-04 23:27:34 +0000777// AttrBuilder Method Implementations
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000778//===----------------------------------------------------------------------===//
779
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000780AttrBuilder::AttrBuilder(AttributeSet AS, unsigned Idx)
781 : Alignment(0), StackAlignment(0) {
Bill Wendlingec258982013-01-27 21:23:46 +0000782 AttributeSetImpl *pImpl = AS.pImpl;
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000783 if (!pImpl) return;
784
Bill Wendling73bc4522013-01-28 00:21:34 +0000785 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I) {
786 if (pImpl->getSlotIndex(I) != Idx) continue;
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000787
Bill Wendling383da6b2013-01-30 21:22:59 +0000788 for (AttributeSetImpl::const_iterator II = pImpl->begin(I),
Bill Wendling73bc4522013-01-28 00:21:34 +0000789 IE = pImpl->end(I); II != IE; ++II)
Bill Wendling39da0782013-01-31 23:38:01 +0000790 addAttribute(*II);
Bill Wendling73bc4522013-01-28 00:21:34 +0000791
792 break;
793 }
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000794}
795
Bill Wendling03198882013-01-04 23:27:34 +0000796void AttrBuilder::clear() {
797 Attrs.clear();
798 Alignment = StackAlignment = 0;
799}
800
801AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
Bill Wendling169d5272013-01-31 23:16:25 +0000802 assert(Val != Attribute::Alignment && Val != Attribute::StackAlignment &&
803 "Adding alignment attribute without adding alignment value!");
Bill Wendling03198882013-01-04 23:27:34 +0000804 Attrs.insert(Val);
Bill Wendling3a106e62012-10-09 19:01:18 +0000805 return *this;
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000806}
807
Bill Wendling39da0782013-01-31 23:38:01 +0000808AttrBuilder &AttrBuilder::addAttribute(Attribute Attr) {
Bill Wendling169d5272013-01-31 23:16:25 +0000809 ConstantInt *Kind = cast<ConstantInt>(Attr.getAttributeKind());
810 Attribute::AttrKind KindVal = Attribute::AttrKind(Kind->getZExtValue());
811 Attrs.insert(KindVal);
Bill Wendling49f60602013-01-28 05:23:28 +0000812
Bill Wendling169d5272013-01-31 23:16:25 +0000813 if (KindVal == Attribute::Alignment)
Bill Wendling49f60602013-01-28 05:23:28 +0000814 Alignment = Attr.getAlignment();
Bill Wendling169d5272013-01-31 23:16:25 +0000815 else if (KindVal == Attribute::StackAlignment)
Bill Wendling49f60602013-01-28 05:23:28 +0000816 StackAlignment = Attr.getStackAlignment();
817 return *this;
818}
819
Bill Wendling39da0782013-01-31 23:38:01 +0000820AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
821 Attrs.erase(Val);
822
823 if (Val == Attribute::Alignment)
824 Alignment = 0;
825 else if (Val == Attribute::StackAlignment)
826 StackAlignment = 0;
827
828 return *this;
829}
830
Bill Wendlinge7436542013-01-30 23:07:40 +0000831AttrBuilder &AttrBuilder::removeAttributes(AttributeSet A, uint64_t Index) {
Bill Wendling30d2c762013-02-01 00:13:50 +0000832 unsigned Idx = ~0U;
833 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
834 if (A.getSlotIndex(I) == Index) {
835 Idx = I;
836 break;
Bill Wendling49f60602013-01-28 05:23:28 +0000837 }
Bill Wendling30d2c762013-02-01 00:13:50 +0000838
839 assert(Idx != ~0U && "Couldn't find index in AttributeSet!");
840
841 for (AttributeSet::iterator I = A.begin(Idx), E = A.end(Idx); I != E; ++I) {
842 ConstantInt *CI = cast<ConstantInt>(I->getAttributeKind());
843 Attribute::AttrKind Kind = Attribute::AttrKind(CI->getZExtValue());
844 Attrs.erase(Kind);
845
846 if (Kind == Attribute::Alignment)
847 Alignment = 0;
848 else if (Kind == Attribute::StackAlignment)
849 StackAlignment = 0;
Bill Wendling49f60602013-01-28 05:23:28 +0000850 }
851
852 return *this;
853}
854
Bill Wendling702cc912012-10-15 20:35:56 +0000855AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
Bill Wendlingda3f9d82012-10-14 03:58:29 +0000856 if (Align == 0) return *this;
Bill Wendling03198882013-01-04 23:27:34 +0000857
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000858 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
859 assert(Align <= 0x40000000 && "Alignment too large.");
Bill Wendling03198882013-01-04 23:27:34 +0000860
861 Attrs.insert(Attribute::Alignment);
862 Alignment = Align;
Bill Wendlingda3f9d82012-10-14 03:58:29 +0000863 return *this;
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000864}
865
Bill Wendling03198882013-01-04 23:27:34 +0000866AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
867 // Default alignment, allow the target to define how to align it.
868 if (Align == 0) return *this;
869
870 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
871 assert(Align <= 0x100 && "Alignment too large.");
872
873 Attrs.insert(Attribute::StackAlignment);
874 StackAlignment = Align;
875 return *this;
876}
877
Bill Wendling22bd6412013-01-03 01:54:39 +0000878bool AttrBuilder::contains(Attribute::AttrKind A) const {
Bill Wendling03198882013-01-04 23:27:34 +0000879 return Attrs.count(A);
Bill Wendling7d2f2492012-10-10 07:36:45 +0000880}
881
Bill Wendling702cc912012-10-15 20:35:56 +0000882bool AttrBuilder::hasAttributes() const {
Bill Wendling03198882013-01-04 23:27:34 +0000883 return !Attrs.empty();
Bill Wendlingf385f4c2012-10-08 23:27:46 +0000884}
Bill Wendling60507d52013-01-04 20:54:35 +0000885
Bill Wendlinge7436542013-01-30 23:07:40 +0000886bool AttrBuilder::hasAttributes(AttributeSet A, uint64_t Index) const {
887 return Raw() & A.Raw(Index);
Bill Wendling8831c062012-10-09 00:01:21 +0000888}
Bill Wendling60507d52013-01-04 20:54:35 +0000889
Bill Wendling702cc912012-10-15 20:35:56 +0000890bool AttrBuilder::hasAlignmentAttr() const {
Bill Wendling03198882013-01-04 23:27:34 +0000891 return Alignment != 0;
Bill Wendlingf385f4c2012-10-08 23:27:46 +0000892}
893
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000894bool AttrBuilder::operator==(const AttrBuilder &B) {
895 SmallVector<Attribute::AttrKind, 8> This(Attrs.begin(), Attrs.end());
896 SmallVector<Attribute::AttrKind, 8> That(B.Attrs.begin(), B.Attrs.end());
897 return This == That;
898}
899
900AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) {
Bill Wendling8232ece2013-01-29 01:43:29 +0000901 if (!Val) return *this;
902
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000903 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
904 I = Attribute::AttrKind(I + 1)) {
905 if (uint64_t A = (Val & AttributeImpl::getAttrMask(I))) {
906 Attrs.insert(I);
907
908 if (I == Attribute::Alignment)
909 Alignment = 1ULL << ((A >> 16) - 1);
910 else if (I == Attribute::StackAlignment)
911 StackAlignment = 1ULL << ((A >> 26)-1);
912 }
913 }
914
915 return *this;
916}
917
Bill Wendling1db9b692013-01-09 23:36:50 +0000918uint64_t AttrBuilder::Raw() const {
Bill Wendling03198882013-01-04 23:27:34 +0000919 uint64_t Mask = 0;
920
921 for (DenseSet<Attribute::AttrKind>::const_iterator I = Attrs.begin(),
922 E = Attrs.end(); I != E; ++I) {
923 Attribute::AttrKind Kind = *I;
924
925 if (Kind == Attribute::Alignment)
926 Mask |= (Log2_32(Alignment) + 1) << 16;
927 else if (Kind == Attribute::StackAlignment)
928 Mask |= (Log2_32(StackAlignment) + 1) << 26;
929 else
930 Mask |= AttributeImpl::getAttrMask(Kind);
931 }
932
933 return Mask;
Bill Wendlingf385f4c2012-10-08 23:27:46 +0000934}
935
Bill Wendling8e47daf2013-01-25 23:09:36 +0000936//===----------------------------------------------------------------------===//
937// AttributeFuncs Function Defintions
938//===----------------------------------------------------------------------===//
939
Bill Wendling7beee282013-02-01 01:04:27 +0000940/// \brief Which attributes cannot be applied to a type.
Bill Wendlinge7436542013-01-30 23:07:40 +0000941AttributeSet AttributeFuncs::typeIncompatible(Type *Ty, uint64_t Index) {
Bill Wendling8e47daf2013-01-25 23:09:36 +0000942 AttrBuilder Incompatible;
943
944 if (!Ty->isIntegerTy())
945 // Attribute that only apply to integers.
946 Incompatible.addAttribute(Attribute::SExt)
947 .addAttribute(Attribute::ZExt);
948
949 if (!Ty->isPointerTy())
950 // Attribute that only apply to pointers.
951 Incompatible.addAttribute(Attribute::ByVal)
952 .addAttribute(Attribute::Nest)
953 .addAttribute(Attribute::NoAlias)
954 .addAttribute(Attribute::NoCapture)
955 .addAttribute(Attribute::StructRet);
956
Bill Wendlinge7436542013-01-30 23:07:40 +0000957 return AttributeSet::get(Ty->getContext(), Index, Incompatible);
Bill Wendling8e47daf2013-01-25 23:09:36 +0000958}
959
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000960/// \brief This returns an integer containing an encoding of all the LLVM
961/// attributes found in the given attribute bitset. Any change to this encoding
962/// is a breaking change to bitcode compatibility.
Bill Wendling8232ece2013-01-29 01:43:29 +0000963/// N.B. This should be used only by the bitcode reader!
Bill Wendling8e47daf2013-01-25 23:09:36 +0000964uint64_t AttributeFuncs::encodeLLVMAttributesForBitcode(AttributeSet Attrs,
965 unsigned Index) {
966 // FIXME: It doesn't make sense to store the alignment information as an
967 // expanded out value, we should store it as a log2 value. However, we can't
968 // just change that here without breaking bitcode compatibility. If this ever
969 // becomes a problem in practice, we should introduce new tag numbers in the
970 // bitcode file and have those tags use a more efficiently encoded alignment
971 // field.
972
973 // Store the alignment in the bitcode as a 16-bit raw value instead of a 5-bit
974 // log2 encoded value. Shift the bits above the alignment up by 11 bits.
975 uint64_t EncodedAttrs = Attrs.Raw(Index) & 0xffff;
976 if (Attrs.hasAttribute(Index, Attribute::Alignment))
977 EncodedAttrs |= Attrs.getParamAlignment(Index) << 16;
978 EncodedAttrs |= (Attrs.Raw(Index) & (0xffffULL << 21)) << 11;
979 return EncodedAttrs;
980}
981
Bill Wendling8232ece2013-01-29 01:43:29 +0000982/// \brief This fills an AttrBuilder object with the LLVM attributes that have
983/// been decoded from the given integer. This function must stay in sync with
984/// 'encodeLLVMAttributesForBitcode'.
985/// N.B. This should be used only by the bitcode reader!
986void AttributeFuncs::decodeLLVMAttributesForBitcode(LLVMContext &C,
987 AttrBuilder &B,
988 uint64_t EncodedAttrs) {
Bill Wendling8e47daf2013-01-25 23:09:36 +0000989 // The alignment is stored as a 16-bit raw value from bits 31--16. We shift
990 // the bits above 31 down by 11 bits.
991 unsigned Alignment = (EncodedAttrs & (0xffffULL << 16)) >> 16;
992 assert((!Alignment || isPowerOf2_32(Alignment)) &&
993 "Alignment must be a power of two.");
994
Bill Wendling8e47daf2013-01-25 23:09:36 +0000995 if (Alignment)
996 B.addAlignmentAttr(Alignment);
Bill Wendling606c8e32013-01-29 03:20:31 +0000997 B.addRawValue(((EncodedAttrs & (0xffffULL << 32)) >> 11) |
998 (EncodedAttrs & 0xffff));
Bill Wendling8e47daf2013-01-25 23:09:36 +0000999}