blob: 938a34abdb87d876feceaaa941722e30fd4969e9 [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 Wendling6bdbf062013-01-28 22:33:39 +000033Attribute Attribute::get(LLVMContext &Context, AttrKind Kind) {
Bill Wendling16f95662013-01-27 10:28:39 +000034 AttrBuilder B;
Bill Wendling6bdbf062013-01-28 22:33:39 +000035 return Attribute::get(Context, B.addAttribute(Kind));
Bill Wendlingcb3de0b2012-10-15 04:46:55 +000036}
Bill Wendling8e635db2012-10-08 21:47:17 +000037
Bill Wendling034b94b2012-12-19 07:18:57 +000038Attribute Attribute::get(LLVMContext &Context, AttrBuilder &B) {
39 // If there are no attributes, return an empty Attribute class.
Bill Wendlinga5c699d2012-10-16 05:55:09 +000040 if (!B.hasAttributes())
Bill Wendling034b94b2012-12-19 07:18:57 +000041 return Attribute();
Bill Wendling8e635db2012-10-08 21:47:17 +000042
43 // Otherwise, build a key to look up the existing attributes.
44 LLVMContextImpl *pImpl = Context.pImpl;
45 FoldingSetNodeID ID;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +000046 ConstantInt *CI = ConstantInt::get(Type::getInt64Ty(Context), B.Raw());
47 ID.AddPointer(CI);
Bill Wendling8e635db2012-10-08 21:47:17 +000048
49 void *InsertPoint;
Bill Wendlingf6670722012-12-20 01:36:59 +000050 AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
Bill Wendling8e635db2012-10-08 21:47:17 +000051
52 if (!PA) {
53 // If we didn't find any existing attributes of the same shape then create a
54 // new one and insert it.
Bill Wendlingc22f4aa2013-01-29 00:34:06 +000055 PA = new AttributeImpl(Context, CI);
Bill Wendling8e635db2012-10-08 21:47:17 +000056 pImpl->AttrsSet.InsertNode(PA, InsertPoint);
57 }
58
59 // Return the AttributesList that we found or created.
Bill Wendling034b94b2012-12-19 07:18:57 +000060 return Attribute(PA);
Bill Wendling8e635db2012-10-08 21:47:17 +000061}
62
Bill Wendlingc08a5ef2013-01-27 22:43:04 +000063Attribute Attribute::getWithAlignment(LLVMContext &Context, uint64_t Align) {
64 AttrBuilder B;
65 return get(Context, B.addAlignmentAttr(Align));
66}
67
68Attribute Attribute::getWithStackAlignment(LLVMContext &Context,
69 uint64_t Align) {
70 AttrBuilder B;
71 return get(Context, B.addStackAlignmentAttr(Align));
72}
73
Bill Wendling817abdd2013-01-29 00:48:16 +000074//===----------------------------------------------------------------------===//
75// Attribute Accessor Methods
76//===----------------------------------------------------------------------===//
77
Bill Wendling629fb822012-12-22 00:37:52 +000078bool Attribute::hasAttribute(AttrKind Val) const {
Bill Wendling27107f62012-12-20 21:28:43 +000079 return pImpl && pImpl->hasAttribute(Val);
Bill Wendlinge66f3d32012-10-05 06:44:41 +000080}
81
Bill Wendling034b94b2012-12-19 07:18:57 +000082bool Attribute::hasAttributes() const {
Bill Wendling27107f62012-12-20 21:28:43 +000083 return pImpl && pImpl->hasAttributes();
Bill Wendling05cc40d2012-10-15 05:40:12 +000084}
85
Bill Wendling6dc37812013-01-29 20:45:34 +000086Constant *Attribute::getAttributeKind() const {
87 return pImpl ? pImpl->getAttributeKind() : 0;
88}
89
90ArrayRef<Constant*> Attribute::getAttributeValues() const {
91 return pImpl ? pImpl->getAttributeValues() : ArrayRef<Constant*>();
92}
93
Bill Wendlinge66f3d32012-10-05 06:44:41 +000094/// This returns the alignment field of an attribute as a byte alignment value.
Bill Wendling034b94b2012-12-19 07:18:57 +000095unsigned Attribute::getAlignment() const {
96 if (!hasAttribute(Attribute::Alignment))
Bill Wendling9ef99c92012-10-09 20:56:48 +000097 return 0;
Bill Wendlinga8ab5fc2013-01-23 23:00:05 +000098 return pImpl->getAlignment();
Bill Wendlinge66f3d32012-10-05 06:44:41 +000099}
100
101/// This returns the stack alignment field of an attribute as a byte alignment
102/// value.
Bill Wendling034b94b2012-12-19 07:18:57 +0000103unsigned Attribute::getStackAlignment() const {
104 if (!hasAttribute(Attribute::StackAlignment))
Bill Wendling9ef99c92012-10-09 20:56:48 +0000105 return 0;
Bill Wendlinga8ab5fc2013-01-23 23:00:05 +0000106 return pImpl->getStackAlignment();
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000107}
108
Bill Wendling034b94b2012-12-19 07:18:57 +0000109std::string Attribute::getAsString() const {
Bill Wendling034b94b2012-12-19 07:18:57 +0000110 if (hasAttribute(Attribute::ZExt))
Bill Wendling606c8e32013-01-29 03:20:31 +0000111 return "zeroext";
Bill Wendling034b94b2012-12-19 07:18:57 +0000112 if (hasAttribute(Attribute::SExt))
Bill Wendling606c8e32013-01-29 03:20:31 +0000113 return "signext";
Bill Wendling034b94b2012-12-19 07:18:57 +0000114 if (hasAttribute(Attribute::NoReturn))
Bill Wendling606c8e32013-01-29 03:20:31 +0000115 return "noreturn";
Bill Wendling034b94b2012-12-19 07:18:57 +0000116 if (hasAttribute(Attribute::NoUnwind))
Bill Wendling606c8e32013-01-29 03:20:31 +0000117 return "nounwind";
Bill Wendling034b94b2012-12-19 07:18:57 +0000118 if (hasAttribute(Attribute::UWTable))
Bill Wendling606c8e32013-01-29 03:20:31 +0000119 return "uwtable";
Bill Wendling034b94b2012-12-19 07:18:57 +0000120 if (hasAttribute(Attribute::ReturnsTwice))
Bill Wendling606c8e32013-01-29 03:20:31 +0000121 return "returns_twice";
Bill Wendling034b94b2012-12-19 07:18:57 +0000122 if (hasAttribute(Attribute::InReg))
Bill Wendling606c8e32013-01-29 03:20:31 +0000123 return "inreg";
Bill Wendling034b94b2012-12-19 07:18:57 +0000124 if (hasAttribute(Attribute::NoAlias))
Bill Wendling606c8e32013-01-29 03:20:31 +0000125 return "noalias";
Bill Wendling034b94b2012-12-19 07:18:57 +0000126 if (hasAttribute(Attribute::NoCapture))
Bill Wendling606c8e32013-01-29 03:20:31 +0000127 return "nocapture";
Bill Wendling034b94b2012-12-19 07:18:57 +0000128 if (hasAttribute(Attribute::StructRet))
Bill Wendling606c8e32013-01-29 03:20:31 +0000129 return "sret";
Bill Wendling034b94b2012-12-19 07:18:57 +0000130 if (hasAttribute(Attribute::ByVal))
Bill Wendling606c8e32013-01-29 03:20:31 +0000131 return "byval";
Bill Wendling034b94b2012-12-19 07:18:57 +0000132 if (hasAttribute(Attribute::Nest))
Bill Wendling606c8e32013-01-29 03:20:31 +0000133 return "nest";
Bill Wendling034b94b2012-12-19 07:18:57 +0000134 if (hasAttribute(Attribute::ReadNone))
Bill Wendling606c8e32013-01-29 03:20:31 +0000135 return "readnone";
Bill Wendling034b94b2012-12-19 07:18:57 +0000136 if (hasAttribute(Attribute::ReadOnly))
Bill Wendling606c8e32013-01-29 03:20:31 +0000137 return "readonly";
Bill Wendling034b94b2012-12-19 07:18:57 +0000138 if (hasAttribute(Attribute::OptimizeForSize))
Bill Wendling606c8e32013-01-29 03:20:31 +0000139 return "optsize";
Bill Wendling034b94b2012-12-19 07:18:57 +0000140 if (hasAttribute(Attribute::NoInline))
Bill Wendling606c8e32013-01-29 03:20:31 +0000141 return "noinline";
Bill Wendling034b94b2012-12-19 07:18:57 +0000142 if (hasAttribute(Attribute::InlineHint))
Bill Wendling606c8e32013-01-29 03:20:31 +0000143 return "inlinehint";
Bill Wendling034b94b2012-12-19 07:18:57 +0000144 if (hasAttribute(Attribute::AlwaysInline))
Bill Wendling606c8e32013-01-29 03:20:31 +0000145 return "alwaysinline";
Bill Wendling034b94b2012-12-19 07:18:57 +0000146 if (hasAttribute(Attribute::StackProtect))
Bill Wendling606c8e32013-01-29 03:20:31 +0000147 return "ssp";
Bill Wendling034b94b2012-12-19 07:18:57 +0000148 if (hasAttribute(Attribute::StackProtectReq))
Bill Wendling606c8e32013-01-29 03:20:31 +0000149 return "sspreq";
Bill Wendling114baee2013-01-23 06:41:41 +0000150 if (hasAttribute(Attribute::StackProtectStrong))
Bill Wendling606c8e32013-01-29 03:20:31 +0000151 return "sspstrong";
Bill Wendling034b94b2012-12-19 07:18:57 +0000152 if (hasAttribute(Attribute::NoRedZone))
Bill Wendling606c8e32013-01-29 03:20:31 +0000153 return "noredzone";
Bill Wendling034b94b2012-12-19 07:18:57 +0000154 if (hasAttribute(Attribute::NoImplicitFloat))
Bill Wendling606c8e32013-01-29 03:20:31 +0000155 return "noimplicitfloat";
Bill Wendling034b94b2012-12-19 07:18:57 +0000156 if (hasAttribute(Attribute::Naked))
Bill Wendling606c8e32013-01-29 03:20:31 +0000157 return "naked";
Bill Wendling034b94b2012-12-19 07:18:57 +0000158 if (hasAttribute(Attribute::NonLazyBind))
Bill Wendling606c8e32013-01-29 03:20:31 +0000159 return "nonlazybind";
Bill Wendling034b94b2012-12-19 07:18:57 +0000160 if (hasAttribute(Attribute::AddressSafety))
Bill Wendling606c8e32013-01-29 03:20:31 +0000161 return "address_safety";
Bill Wendling034b94b2012-12-19 07:18:57 +0000162 if (hasAttribute(Attribute::MinSize))
Bill Wendling606c8e32013-01-29 03:20:31 +0000163 return "minsize";
Bill Wendling034b94b2012-12-19 07:18:57 +0000164 if (hasAttribute(Attribute::StackAlignment)) {
Bill Wendling606c8e32013-01-29 03:20:31 +0000165 std::string Result;
Charles Davis1e063d12010-02-12 00:31:15 +0000166 Result += "alignstack(";
Bill Wendlingef99fe82012-09-21 15:26:31 +0000167 Result += utostr(getStackAlignment());
Bill Wendling606c8e32013-01-29 03:20:31 +0000168 Result += ")";
169 return Result;
Charles Davis1e063d12010-02-12 00:31:15 +0000170 }
Bill Wendling034b94b2012-12-19 07:18:57 +0000171 if (hasAttribute(Attribute::Alignment)) {
Bill Wendling606c8e32013-01-29 03:20:31 +0000172 std::string Result;
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000173 Result += "align ";
Bill Wendlingef99fe82012-09-21 15:26:31 +0000174 Result += utostr(getAlignment());
Bill Wendling606c8e32013-01-29 03:20:31 +0000175 Result += "";
176 return Result;
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000177 }
James Molloy67ae1352012-12-20 16:04:27 +0000178 if (hasAttribute(Attribute::NoDuplicate))
Bill Wendling606c8e32013-01-29 03:20:31 +0000179 return "noduplicate";
180
181 llvm_unreachable("Unknown attribute");
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000182}
183
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000184bool Attribute::operator==(AttrKind K) const {
185 return pImpl && *pImpl == K;
186}
187bool Attribute::operator!=(AttrKind K) const {
188 return !(*this == K);
189}
190
191bool Attribute::operator<(Attribute A) const {
192 if (!pImpl && !A.pImpl) return false;
193 if (!pImpl) return true;
194 if (!A.pImpl) return false;
195 return *pImpl < *A.pImpl;
196}
197
198uint64_t Attribute::Raw() const {
199 return pImpl ? pImpl->Raw() : 0;
200}
201
202//===----------------------------------------------------------------------===//
203// AttributeImpl Definition
204//===----------------------------------------------------------------------===//
205
Bill Wendling9f175f82013-01-29 20:37:10 +0000206AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind kind)
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000207 : Context(C) {
Bill Wendling9f175f82013-01-29 20:37:10 +0000208 Kind = ConstantInt::get(Type::getInt64Ty(C), kind);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000209}
Bill Wendling9f175f82013-01-29 20:37:10 +0000210AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind kind,
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000211 ArrayRef<Constant*> values)
212 : Context(C) {
Bill Wendling9f175f82013-01-29 20:37:10 +0000213 Kind = ConstantInt::get(Type::getInt64Ty(C), kind);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000214 Vals.reserve(values.size());
215 Vals.append(values.begin(), values.end());
216}
Bill Wendling9f175f82013-01-29 20:37:10 +0000217AttributeImpl::AttributeImpl(LLVMContext &C, StringRef kind)
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000218 : Context(C) {
Bill Wendling9f175f82013-01-29 20:37:10 +0000219 Kind = ConstantDataArray::getString(C, kind);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000220}
221
222bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
223 return (Raw() & getAttrMask(A)) != 0;
224}
225
226bool AttributeImpl::hasAttributes() const {
227 return Raw() != 0;
228}
229
230uint64_t AttributeImpl::getAlignment() const {
231 uint64_t Mask = Raw() & getAttrMask(Attribute::Alignment);
232 return 1ULL << ((Mask >> 16) - 1);
233}
234
235uint64_t AttributeImpl::getStackAlignment() const {
236 uint64_t Mask = Raw() & getAttrMask(Attribute::StackAlignment);
237 return 1ULL << ((Mask >> 26) - 1);
238}
239
Bill Wendling9f175f82013-01-29 20:37:10 +0000240bool AttributeImpl::operator==(Attribute::AttrKind kind) const {
241 if (ConstantInt *CI = dyn_cast<ConstantInt>(Kind))
242 return CI->getZExtValue() == kind;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000243 return false;
244}
Bill Wendling9f175f82013-01-29 20:37:10 +0000245bool AttributeImpl::operator!=(Attribute::AttrKind kind) const {
246 return !(*this == kind);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000247}
248
Bill Wendling9f175f82013-01-29 20:37:10 +0000249bool AttributeImpl::operator==(StringRef kind) const {
250 if (ConstantDataArray *CDA = dyn_cast<ConstantDataArray>(Kind))
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000251 if (CDA->isString())
Bill Wendling9f175f82013-01-29 20:37:10 +0000252 return CDA->getAsString() == kind;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000253 return false;
254}
255
Bill Wendling9f175f82013-01-29 20:37:10 +0000256bool AttributeImpl::operator!=(StringRef kind) const {
257 return !(*this == kind);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000258}
259
260bool AttributeImpl::operator<(const AttributeImpl &AI) const {
Bill Wendling9f175f82013-01-29 20:37:10 +0000261 if (!Kind && !AI.Kind) return false;
262 if (!Kind && AI.Kind) return true;
263 if (Kind && !AI.Kind) return false;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000264
Bill Wendling9f175f82013-01-29 20:37:10 +0000265 ConstantInt *ThisCI = dyn_cast<ConstantInt>(Kind);
266 ConstantInt *ThatCI = dyn_cast<ConstantInt>(AI.Kind);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000267
Bill Wendling9f175f82013-01-29 20:37:10 +0000268 ConstantDataArray *ThisCDA = dyn_cast<ConstantDataArray>(Kind);
269 ConstantDataArray *ThatCDA = dyn_cast<ConstantDataArray>(AI.Kind);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000270
271 if (ThisCI && ThatCI)
272 return ThisCI->getZExtValue() < ThatCI->getZExtValue();
273
274 if (ThisCI && ThatCDA)
275 return true;
276
277 if (ThisCDA && ThatCI)
278 return false;
279
280 return ThisCDA->getAsString() < ThatCDA->getAsString();
281}
282
283uint64_t AttributeImpl::Raw() const {
284 // FIXME: Remove this.
Bill Wendling9f175f82013-01-29 20:37:10 +0000285 return cast<ConstantInt>(Kind)->getZExtValue();
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000286}
287
288uint64_t AttributeImpl::getAttrMask(Attribute::AttrKind Val) {
289 // FIXME: Remove this.
290 switch (Val) {
291 case Attribute::EndAttrKinds:
292 case Attribute::AttrKindEmptyKey:
293 case Attribute::AttrKindTombstoneKey:
294 llvm_unreachable("Synthetic enumerators which should never get here");
295
296 case Attribute::None: return 0;
297 case Attribute::ZExt: return 1 << 0;
298 case Attribute::SExt: return 1 << 1;
299 case Attribute::NoReturn: return 1 << 2;
300 case Attribute::InReg: return 1 << 3;
301 case Attribute::StructRet: return 1 << 4;
302 case Attribute::NoUnwind: return 1 << 5;
303 case Attribute::NoAlias: return 1 << 6;
304 case Attribute::ByVal: return 1 << 7;
305 case Attribute::Nest: return 1 << 8;
306 case Attribute::ReadNone: return 1 << 9;
307 case Attribute::ReadOnly: return 1 << 10;
308 case Attribute::NoInline: return 1 << 11;
309 case Attribute::AlwaysInline: return 1 << 12;
310 case Attribute::OptimizeForSize: return 1 << 13;
311 case Attribute::StackProtect: return 1 << 14;
312 case Attribute::StackProtectReq: return 1 << 15;
313 case Attribute::Alignment: return 31 << 16;
314 case Attribute::NoCapture: return 1 << 21;
315 case Attribute::NoRedZone: return 1 << 22;
316 case Attribute::NoImplicitFloat: return 1 << 23;
317 case Attribute::Naked: return 1 << 24;
318 case Attribute::InlineHint: return 1 << 25;
319 case Attribute::StackAlignment: return 7 << 26;
320 case Attribute::ReturnsTwice: return 1 << 29;
321 case Attribute::UWTable: return 1 << 30;
322 case Attribute::NonLazyBind: return 1U << 31;
323 case Attribute::AddressSafety: return 1ULL << 32;
324 case Attribute::MinSize: return 1ULL << 33;
325 case Attribute::NoDuplicate: return 1ULL << 34;
326 case Attribute::StackProtectStrong: return 1ULL << 35;
327 }
328 llvm_unreachable("Unsupported attribute type");
329}
330
331//===----------------------------------------------------------------------===//
332// AttributeSetNode Definition
333//===----------------------------------------------------------------------===//
334
335AttributeSetNode *AttributeSetNode::get(LLVMContext &C,
336 ArrayRef<Attribute> Attrs) {
337 if (Attrs.empty())
338 return 0;
339
340 // Otherwise, build a key to look up the existing attributes.
341 LLVMContextImpl *pImpl = C.pImpl;
342 FoldingSetNodeID ID;
343
344 SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end());
345 std::sort(SortedAttrs.begin(), SortedAttrs.end());
346
347 for (SmallVectorImpl<Attribute>::iterator I = SortedAttrs.begin(),
348 E = SortedAttrs.end(); I != E; ++I)
349 I->Profile(ID);
350
351 void *InsertPoint;
352 AttributeSetNode *PA =
353 pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint);
354
355 // If we didn't find any existing attributes of the same shape then create a
356 // new one and insert it.
357 if (!PA) {
358 PA = new AttributeSetNode(SortedAttrs);
359 pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint);
360 }
361
362 // Return the AttributesListNode that we found or created.
363 return PA;
364}
365
Bill Wendling606c8e32013-01-29 03:20:31 +0000366bool AttributeSetNode::hasAttribute(Attribute::AttrKind Kind) const {
367 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
368 E = AttrList.end(); I != E; ++I)
369 if (I->hasAttribute(Kind))
370 return true;
371 return false;
372}
373
374unsigned AttributeSetNode::getAlignment() const {
375 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
376 E = AttrList.end(); I != E; ++I)
377 if (I->hasAttribute(Attribute::Alignment))
378 return I->getAlignment();
379 return 0;
380}
381
382unsigned AttributeSetNode::getStackAlignment() const {
383 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
384 E = AttrList.end(); I != E; ++I)
385 if (I->hasAttribute(Attribute::StackAlignment))
386 return I->getStackAlignment();
387 return 0;
388}
389
390std::string AttributeSetNode::getAsString() const {
391 std::string Str = "";
392 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
393 E = AttrList.end(); I != E; ++I) {
394 if (I != AttrList.begin()) Str += " ";
395 Str += I->getAsString();
396 }
397 return Str;
398}
399
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000400//===----------------------------------------------------------------------===//
401// AttributeSetImpl Definition
402//===----------------------------------------------------------------------===//
403
404uint64_t AttributeSetImpl::Raw(uint64_t Index) const {
405 for (unsigned I = 0, E = getNumAttributes(); I != E; ++I) {
406 if (getSlotIndex(I) != Index) continue;
407 const AttributeSetNode *ASN = AttrNodes[I].second;
408 AttrBuilder B;
409
410 for (AttributeSetNode::const_iterator II = ASN->begin(),
411 IE = ASN->end(); II != IE; ++II)
412 B.addAttributes(*II);
413 return B.Raw();
414 }
415
416 return 0;
417}
418
419//===----------------------------------------------------------------------===//
420// AttributeSet Construction and Mutation Methods
421//===----------------------------------------------------------------------===//
422
Bill Wendling8232ece2013-01-29 01:43:29 +0000423AttributeSet
424AttributeSet::getImpl(LLVMContext &C,
425 ArrayRef<std::pair<unsigned, AttributeSetNode*> > Attrs) {
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000426 LLVMContextImpl *pImpl = C.pImpl;
427 FoldingSetNodeID ID;
428 AttributeSetImpl::Profile(ID, Attrs);
429
430 void *InsertPoint;
431 AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
432
433 // If we didn't find any existing attributes of the same shape then
434 // create a new one and insert it.
435 if (!PA) {
436 PA = new AttributeSetImpl(C, Attrs);
437 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
438 }
439
440 // Return the AttributesList that we found or created.
441 return AttributeSet(PA);
442}
443
444AttributeSet AttributeSet::get(LLVMContext &C,
445 ArrayRef<std::pair<unsigned, Attribute> > Attrs){
446 // If there are no attributes then return a null AttributesList pointer.
447 if (Attrs.empty())
448 return AttributeSet();
449
450#ifndef NDEBUG
451 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
452 assert((!i || Attrs[i-1].first <= Attrs[i].first) &&
453 "Misordered Attributes list!");
454 assert(Attrs[i].second.hasAttributes() &&
455 "Pointless attribute!");
456 }
457#endif
458
459 // Create a vector if (unsigned, AttributeSetNode*) pairs from the attributes
460 // list.
461 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrPairVec;
462 for (ArrayRef<std::pair<unsigned, Attribute> >::iterator I = Attrs.begin(),
463 E = Attrs.end(); I != E; ) {
464 unsigned Index = I->first;
465 SmallVector<Attribute, 4> AttrVec;
NAKAMURA Takumi3ba51ce2013-01-29 15:18:16 +0000466 while (I != E && I->first == Index) {
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000467 AttrVec.push_back(I->second);
468 ++I;
469 }
470
471 AttrPairVec.push_back(std::make_pair(Index,
472 AttributeSetNode::get(C, AttrVec)));
473 }
474
475 return getImpl(C, AttrPairVec);
476}
477
478AttributeSet AttributeSet::get(LLVMContext &C,
479 ArrayRef<std::pair<unsigned,
480 AttributeSetNode*> > Attrs) {
481 // If there are no attributes then return a null AttributesList pointer.
482 if (Attrs.empty())
483 return AttributeSet();
484
485 return getImpl(C, Attrs);
486}
487
488AttributeSet AttributeSet::get(LLVMContext &C, unsigned Idx, AttrBuilder &B) {
489 if (!B.hasAttributes())
490 return AttributeSet();
Bill Wendling8fbc0c22013-01-29 01:02:03 +0000491
492 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
493 for (AttrBuilder::iterator I = B.begin(), E = B.end(); I != E; ++I) {
494 Attribute::AttrKind Kind = *I;
495 if (Kind == Attribute::Alignment)
496 Attrs.push_back(std::make_pair(Idx, Attribute::
497 getWithAlignment(C, B.getAlignment())));
498 else if (Kind == Attribute::StackAlignment)
499 Attrs.push_back(std::make_pair(Idx, Attribute::
500 getWithStackAlignment(C, B.getStackAlignment())));
501 else
502 Attrs.push_back(std::make_pair(Idx, Attribute::get(C, Kind)));
503 }
504
505 return get(C, Attrs);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000506}
507
508AttributeSet AttributeSet::get(LLVMContext &C, unsigned Idx,
509 ArrayRef<Attribute::AttrKind> Kind) {
510 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
511 for (ArrayRef<Attribute::AttrKind>::iterator I = Kind.begin(),
512 E = Kind.end(); I != E; ++I)
513 Attrs.push_back(std::make_pair(Idx, Attribute::get(C, *I)));
514 return get(C, Attrs);
515}
516
517AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<AttributeSet> Attrs) {
518 if (Attrs.empty()) return AttributeSet();
519
520 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrNodeVec;
521 for (unsigned I = 0, E = Attrs.size(); I != E; ++I) {
522 AttributeSet AS = Attrs[I];
523 if (!AS.pImpl) continue;
524 AttrNodeVec.append(AS.pImpl->AttrNodes.begin(), AS.pImpl->AttrNodes.end());
525 }
526
527 return getImpl(C, AttrNodeVec);
528}
529
530AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Idx,
531 Attribute::AttrKind Attr) const {
532 return addAttributes(C, Idx, AttributeSet::get(C, Idx, Attr));
533}
534
535AttributeSet AttributeSet::addAttributes(LLVMContext &C, unsigned Idx,
536 AttributeSet Attrs) const {
537 if (!pImpl) return Attrs;
538 if (!Attrs.pImpl) return *this;
539
540#ifndef NDEBUG
541 // FIXME it is not obvious how this should work for alignment. For now, say
542 // we can't change a known alignment.
543 unsigned OldAlign = getParamAlignment(Idx);
544 unsigned NewAlign = Attrs.getParamAlignment(Idx);
545 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
546 "Attempt to change alignment!");
547#endif
548
549 // Add the attribute slots before the one we're trying to add.
550 SmallVector<AttributeSet, 4> AttrSet;
551 uint64_t NumAttrs = pImpl->getNumAttributes();
552 AttributeSet AS;
553 uint64_t LastIndex = 0;
554 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
555 if (getSlotIndex(I) >= Idx) {
556 if (getSlotIndex(I) == Idx) AS = getSlotAttributes(LastIndex++);
557 break;
558 }
559 LastIndex = I + 1;
560 AttrSet.push_back(getSlotAttributes(I));
561 }
562
563 // Now add the attribute into the correct slot. There may already be an
564 // AttributeSet there.
565 AttrBuilder B(AS, Idx);
566
567 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
568 if (Attrs.getSlotIndex(I) == Idx) {
569 for (AttributeSetImpl::const_iterator II = Attrs.pImpl->begin(I),
570 IE = Attrs.pImpl->end(I); II != IE; ++II)
571 B.addAttributes(*II);
572 break;
573 }
574
575 AttrSet.push_back(AttributeSet::get(C, Idx, B));
576
577 // Add the remaining attribute slots.
578 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
579 AttrSet.push_back(getSlotAttributes(I));
580
581 return get(C, AttrSet);
582}
583
584AttributeSet AttributeSet::removeAttribute(LLVMContext &C, unsigned Idx,
585 Attribute::AttrKind Attr) const {
586 return removeAttributes(C, Idx, AttributeSet::get(C, Idx, Attr));
587}
588
589AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Idx,
590 AttributeSet Attrs) const {
591 if (!pImpl) return AttributeSet();
592 if (!Attrs.pImpl) return *this;
593
594#ifndef NDEBUG
595 // FIXME it is not obvious how this should work for alignment.
596 // For now, say we can't pass in alignment, which no current use does.
597 assert(!Attrs.hasAttribute(Idx, Attribute::Alignment) &&
598 "Attempt to change alignment!");
599#endif
600
601 // Add the attribute slots before the one we're trying to add.
602 SmallVector<AttributeSet, 4> AttrSet;
603 uint64_t NumAttrs = pImpl->getNumAttributes();
604 AttributeSet AS;
605 uint64_t LastIndex = 0;
606 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
607 if (getSlotIndex(I) >= Idx) {
608 if (getSlotIndex(I) == Idx) AS = getSlotAttributes(LastIndex++);
609 break;
610 }
611 LastIndex = I + 1;
612 AttrSet.push_back(getSlotAttributes(I));
613 }
614
615 // Now add the attribute into the correct slot. There may already be an
616 // AttributeSet there.
617 AttrBuilder B(AS, Idx);
618
619 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
620 if (Attrs.getSlotIndex(I) == Idx) {
621 for (AttributeSetImpl::const_iterator II = Attrs.pImpl->begin(I),
622 IE = Attrs.pImpl->end(I); II != IE; ++II)
623 B.removeAttributes(*II);
624 break;
625 }
626
627 AttrSet.push_back(AttributeSet::get(C, Idx, B));
628
629 // Add the remaining attribute slots.
630 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
631 AttrSet.push_back(getSlotAttributes(I));
632
633 return get(C, AttrSet);
634}
635
636//===----------------------------------------------------------------------===//
637// AttributeSet Accessor Methods
638//===----------------------------------------------------------------------===//
639
640AttributeSet AttributeSet::getParamAttributes(unsigned Idx) const {
641 return pImpl && hasAttributes(Idx) ?
642 AttributeSet::get(pImpl->getContext(),
Bill Wendling606c8e32013-01-29 03:20:31 +0000643 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000644 std::make_pair(Idx, getAttributes(Idx)))) :
645 AttributeSet();
646}
647
648AttributeSet AttributeSet::getRetAttributes() const {
649 return pImpl && hasAttributes(ReturnIndex) ?
650 AttributeSet::get(pImpl->getContext(),
Bill Wendling606c8e32013-01-29 03:20:31 +0000651 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000652 std::make_pair(ReturnIndex,
653 getAttributes(ReturnIndex)))) :
654 AttributeSet();
655}
656
657AttributeSet AttributeSet::getFnAttributes() const {
658 return pImpl && hasAttributes(FunctionIndex) ?
659 AttributeSet::get(pImpl->getContext(),
Bill Wendling606c8e32013-01-29 03:20:31 +0000660 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000661 std::make_pair(FunctionIndex,
662 getAttributes(FunctionIndex)))) :
663 AttributeSet();
664}
665
666bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{
Bill Wendling606c8e32013-01-29 03:20:31 +0000667 AttributeSetNode *ASN = getAttributes(Index);
668 return ASN ? ASN->hasAttribute(Kind) : false;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000669}
670
671bool AttributeSet::hasAttributes(unsigned Index) const {
Bill Wendling606c8e32013-01-29 03:20:31 +0000672 AttributeSetNode *ASN = getAttributes(Index);
673 return ASN ? ASN->hasAttributes() : false;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000674}
675
676/// \brief Return true if the specified attribute is set for at least one
677/// parameter or for the return value.
678bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr) const {
679 if (pImpl == 0) return false;
680
681 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
682 for (AttributeSetImpl::const_iterator II = pImpl->begin(I),
683 IE = pImpl->end(I); II != IE; ++II)
684 if (II->hasAttribute(Attr))
685 return true;
686
687 return false;
688}
689
Bill Wendling606c8e32013-01-29 03:20:31 +0000690unsigned AttributeSet::getParamAlignment(unsigned Index) const {
691 AttributeSetNode *ASN = getAttributes(Index);
692 return ASN ? ASN->getAlignment() : 0;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000693}
694
695unsigned AttributeSet::getStackAlignment(unsigned Index) const {
Bill Wendling606c8e32013-01-29 03:20:31 +0000696 AttributeSetNode *ASN = getAttributes(Index);
697 return ASN ? ASN->getStackAlignment() : 0;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000698}
699
700std::string AttributeSet::getAsString(unsigned Index) const {
Bill Wendling606c8e32013-01-29 03:20:31 +0000701 AttributeSetNode *ASN = getAttributes(Index);
702 return ASN ? ASN->getAsString() : std::string("");
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000703}
704
705/// \brief The attributes for the specified index are returned.
Bill Wendling606c8e32013-01-29 03:20:31 +0000706AttributeSetNode *AttributeSet::getAttributes(unsigned Idx) const {
707 if (!pImpl) return 0;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000708
Bill Wendling606c8e32013-01-29 03:20:31 +0000709 // Loop through to find the attribute node we want.
710 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
711 if (pImpl->getSlotIndex(I) == Idx)
712 return pImpl->getSlotNode(I);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000713
Bill Wendling606c8e32013-01-29 03:20:31 +0000714 return 0;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000715}
716
717//===----------------------------------------------------------------------===//
718// AttributeSet Introspection Methods
719//===----------------------------------------------------------------------===//
720
721/// \brief Return the number of slots used in this attribute list. This is the
722/// number of arguments that have an attribute set on them (including the
723/// function itself).
724unsigned AttributeSet::getNumSlots() const {
725 return pImpl ? pImpl->getNumAttributes() : 0;
726}
727
728uint64_t AttributeSet::getSlotIndex(unsigned Slot) const {
729 assert(pImpl && Slot < pImpl->getNumAttributes() &&
730 "Slot # out of range!");
731 return pImpl->getSlotIndex(Slot);
732}
733
734AttributeSet AttributeSet::getSlotAttributes(unsigned Slot) const {
735 assert(pImpl && Slot < pImpl->getNumAttributes() &&
736 "Slot # out of range!");
737 return pImpl->getSlotAttributes(Slot);
738}
739
740uint64_t AttributeSet::Raw(unsigned Index) const {
741 // FIXME: Remove this.
742 return pImpl ? pImpl->Raw(Index) : 0;
743}
744
745void AttributeSet::dump() const {
746 dbgs() << "PAL[\n";
747
748 for (unsigned i = 0, e = getNumSlots(); i < e; ++i) {
749 uint64_t Index = getSlotIndex(i);
750 dbgs() << " { ";
751 if (Index == ~0U)
752 dbgs() << "~0U";
753 else
754 dbgs() << Index;
755 dbgs() << " => " << getAsString(Index) << " }\n";
756 }
757
758 dbgs() << "]\n";
759}
760
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000761//===----------------------------------------------------------------------===//
Bill Wendling03198882013-01-04 23:27:34 +0000762// AttrBuilder Method Implementations
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000763//===----------------------------------------------------------------------===//
764
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000765AttrBuilder::AttrBuilder(AttributeSet AS, unsigned Idx)
766 : Alignment(0), StackAlignment(0) {
Bill Wendlingec258982013-01-27 21:23:46 +0000767 AttributeSetImpl *pImpl = AS.pImpl;
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000768 if (!pImpl) return;
769
Bill Wendling73bc4522013-01-28 00:21:34 +0000770 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I) {
771 if (pImpl->getSlotIndex(I) != Idx) continue;
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000772
Bill Wendling383da6b2013-01-30 21:22:59 +0000773 for (AttributeSetImpl::const_iterator II = pImpl->begin(I),
Bill Wendling73bc4522013-01-28 00:21:34 +0000774 IE = pImpl->end(I); II != IE; ++II)
Bill Wendling383da6b2013-01-30 21:22:59 +0000775 addAttributes(*II);
Bill Wendling73bc4522013-01-28 00:21:34 +0000776
777 break;
778 }
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000779}
780
Bill Wendling03198882013-01-04 23:27:34 +0000781void AttrBuilder::clear() {
782 Attrs.clear();
783 Alignment = StackAlignment = 0;
784}
785
786AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
787 Attrs.insert(Val);
Bill Wendling3a106e62012-10-09 19:01:18 +0000788 return *this;
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000789}
790
Bill Wendling03198882013-01-04 23:27:34 +0000791AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
792 Attrs.erase(Val);
793 if (Val == Attribute::Alignment)
794 Alignment = 0;
795 else if (Val == Attribute::StackAlignment)
796 StackAlignment = 0;
797
Bill Wendlinga19a5302012-10-14 04:10:01 +0000798 return *this;
799}
800
Bill Wendling49f60602013-01-28 05:23:28 +0000801AttrBuilder &AttrBuilder::addAttributes(Attribute Attr) {
802 uint64_t Mask = Attr.Raw();
803
804 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
805 I = Attribute::AttrKind(I + 1))
806 if ((Mask & AttributeImpl::getAttrMask(I)) != 0)
807 Attrs.insert(I);
808
809 if (Attr.getAlignment())
810 Alignment = Attr.getAlignment();
811 if (Attr.getStackAlignment())
812 StackAlignment = Attr.getStackAlignment();
813 return *this;
814}
815
816AttrBuilder &AttrBuilder::removeAttributes(Attribute A) {
817 uint64_t Mask = A.Raw();
818
819 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
820 I = Attribute::AttrKind(I + 1)) {
821 if (Mask & AttributeImpl::getAttrMask(I)) {
822 Attrs.erase(I);
823
824 if (I == Attribute::Alignment)
825 Alignment = 0;
826 else if (I == Attribute::StackAlignment)
827 StackAlignment = 0;
828 }
829 }
830
831 return *this;
832}
833
Bill Wendling702cc912012-10-15 20:35:56 +0000834AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
Bill Wendlingda3f9d82012-10-14 03:58:29 +0000835 if (Align == 0) return *this;
Bill Wendling03198882013-01-04 23:27:34 +0000836
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000837 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
838 assert(Align <= 0x40000000 && "Alignment too large.");
Bill Wendling03198882013-01-04 23:27:34 +0000839
840 Attrs.insert(Attribute::Alignment);
841 Alignment = Align;
Bill Wendlingda3f9d82012-10-14 03:58:29 +0000842 return *this;
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000843}
844
Bill Wendling03198882013-01-04 23:27:34 +0000845AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
846 // Default alignment, allow the target to define how to align it.
847 if (Align == 0) return *this;
848
849 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
850 assert(Align <= 0x100 && "Alignment too large.");
851
852 Attrs.insert(Attribute::StackAlignment);
853 StackAlignment = Align;
854 return *this;
855}
856
Bill Wendling22bd6412013-01-03 01:54:39 +0000857bool AttrBuilder::contains(Attribute::AttrKind A) const {
Bill Wendling03198882013-01-04 23:27:34 +0000858 return Attrs.count(A);
Bill Wendling7d2f2492012-10-10 07:36:45 +0000859}
860
Bill Wendling702cc912012-10-15 20:35:56 +0000861bool AttrBuilder::hasAttributes() const {
Bill Wendling03198882013-01-04 23:27:34 +0000862 return !Attrs.empty();
Bill Wendlingf385f4c2012-10-08 23:27:46 +0000863}
Bill Wendling60507d52013-01-04 20:54:35 +0000864
Bill Wendling034b94b2012-12-19 07:18:57 +0000865bool AttrBuilder::hasAttributes(const Attribute &A) const {
Bill Wendling1db9b692013-01-09 23:36:50 +0000866 return Raw() & A.Raw();
Bill Wendling8831c062012-10-09 00:01:21 +0000867}
Bill Wendling60507d52013-01-04 20:54:35 +0000868
Bill Wendling702cc912012-10-15 20:35:56 +0000869bool AttrBuilder::hasAlignmentAttr() const {
Bill Wendling03198882013-01-04 23:27:34 +0000870 return Alignment != 0;
Bill Wendlingf385f4c2012-10-08 23:27:46 +0000871}
872
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000873bool AttrBuilder::operator==(const AttrBuilder &B) {
874 SmallVector<Attribute::AttrKind, 8> This(Attrs.begin(), Attrs.end());
875 SmallVector<Attribute::AttrKind, 8> That(B.Attrs.begin(), B.Attrs.end());
876 return This == That;
877}
878
879AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) {
Bill Wendling8232ece2013-01-29 01:43:29 +0000880 if (!Val) return *this;
881
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000882 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
883 I = Attribute::AttrKind(I + 1)) {
884 if (uint64_t A = (Val & AttributeImpl::getAttrMask(I))) {
885 Attrs.insert(I);
886
887 if (I == Attribute::Alignment)
888 Alignment = 1ULL << ((A >> 16) - 1);
889 else if (I == Attribute::StackAlignment)
890 StackAlignment = 1ULL << ((A >> 26)-1);
891 }
892 }
893
894 return *this;
895}
896
Bill Wendling1db9b692013-01-09 23:36:50 +0000897uint64_t AttrBuilder::Raw() const {
Bill Wendling03198882013-01-04 23:27:34 +0000898 uint64_t Mask = 0;
899
900 for (DenseSet<Attribute::AttrKind>::const_iterator I = Attrs.begin(),
901 E = Attrs.end(); I != E; ++I) {
902 Attribute::AttrKind Kind = *I;
903
904 if (Kind == Attribute::Alignment)
905 Mask |= (Log2_32(Alignment) + 1) << 16;
906 else if (Kind == Attribute::StackAlignment)
907 Mask |= (Log2_32(StackAlignment) + 1) << 26;
908 else
909 Mask |= AttributeImpl::getAttrMask(Kind);
910 }
911
912 return Mask;
Bill Wendlingf385f4c2012-10-08 23:27:46 +0000913}
914
Bill Wendling8e47daf2013-01-25 23:09:36 +0000915//===----------------------------------------------------------------------===//
916// AttributeFuncs Function Defintions
917//===----------------------------------------------------------------------===//
918
919Attribute AttributeFuncs::typeIncompatible(Type *Ty) {
920 AttrBuilder Incompatible;
921
922 if (!Ty->isIntegerTy())
923 // Attribute that only apply to integers.
924 Incompatible.addAttribute(Attribute::SExt)
925 .addAttribute(Attribute::ZExt);
926
927 if (!Ty->isPointerTy())
928 // Attribute that only apply to pointers.
929 Incompatible.addAttribute(Attribute::ByVal)
930 .addAttribute(Attribute::Nest)
931 .addAttribute(Attribute::NoAlias)
932 .addAttribute(Attribute::NoCapture)
933 .addAttribute(Attribute::StructRet);
934
935 return Attribute::get(Ty->getContext(), Incompatible);
936}
937
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000938/// \brief This returns an integer containing an encoding of all the LLVM
939/// attributes found in the given attribute bitset. Any change to this encoding
940/// is a breaking change to bitcode compatibility.
Bill Wendling8232ece2013-01-29 01:43:29 +0000941/// N.B. This should be used only by the bitcode reader!
Bill Wendling8e47daf2013-01-25 23:09:36 +0000942uint64_t AttributeFuncs::encodeLLVMAttributesForBitcode(AttributeSet Attrs,
943 unsigned Index) {
944 // FIXME: It doesn't make sense to store the alignment information as an
945 // expanded out value, we should store it as a log2 value. However, we can't
946 // just change that here without breaking bitcode compatibility. If this ever
947 // becomes a problem in practice, we should introduce new tag numbers in the
948 // bitcode file and have those tags use a more efficiently encoded alignment
949 // field.
950
951 // Store the alignment in the bitcode as a 16-bit raw value instead of a 5-bit
952 // log2 encoded value. Shift the bits above the alignment up by 11 bits.
953 uint64_t EncodedAttrs = Attrs.Raw(Index) & 0xffff;
954 if (Attrs.hasAttribute(Index, Attribute::Alignment))
955 EncodedAttrs |= Attrs.getParamAlignment(Index) << 16;
956 EncodedAttrs |= (Attrs.Raw(Index) & (0xffffULL << 21)) << 11;
957 return EncodedAttrs;
958}
959
Bill Wendling8232ece2013-01-29 01:43:29 +0000960/// \brief This fills an AttrBuilder object with the LLVM attributes that have
961/// been decoded from the given integer. This function must stay in sync with
962/// 'encodeLLVMAttributesForBitcode'.
963/// N.B. This should be used only by the bitcode reader!
964void AttributeFuncs::decodeLLVMAttributesForBitcode(LLVMContext &C,
965 AttrBuilder &B,
966 uint64_t EncodedAttrs) {
Bill Wendling8e47daf2013-01-25 23:09:36 +0000967 // The alignment is stored as a 16-bit raw value from bits 31--16. We shift
968 // the bits above 31 down by 11 bits.
969 unsigned Alignment = (EncodedAttrs & (0xffffULL << 16)) >> 16;
970 assert((!Alignment || isPowerOf2_32(Alignment)) &&
971 "Alignment must be a power of two.");
972
Bill Wendling8e47daf2013-01-25 23:09:36 +0000973 if (Alignment)
974 B.addAlignmentAttr(Alignment);
Bill Wendling606c8e32013-01-29 03:20:31 +0000975 B.addRawValue(((EncodedAttrs & (0xffffULL << 32)) >> 11) |
976 (EncodedAttrs & 0xffff));
Bill Wendling8e47daf2013-01-25 23:09:36 +0000977}