blob: 75ba93a106d4fefe1e6728a5ebefa78c72e84176 [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
Bill Wendlinge7436542013-01-30 23:07:40 +0000615 // Now remove the attribute from the correct slot. There may already be an
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000616 // 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) {
Bill Wendlinge7436542013-01-30 23:07:40 +0000621 B.removeAttributes(Attrs.pImpl->getSlotAttributes(I), Idx);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000622 break;
623 }
624
625 AttrSet.push_back(AttributeSet::get(C, Idx, B));
626
627 // Add the remaining attribute slots.
628 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
629 AttrSet.push_back(getSlotAttributes(I));
630
631 return get(C, AttrSet);
632}
633
634//===----------------------------------------------------------------------===//
635// AttributeSet Accessor Methods
636//===----------------------------------------------------------------------===//
637
638AttributeSet AttributeSet::getParamAttributes(unsigned Idx) const {
639 return pImpl && hasAttributes(Idx) ?
640 AttributeSet::get(pImpl->getContext(),
Bill Wendling606c8e32013-01-29 03:20:31 +0000641 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000642 std::make_pair(Idx, getAttributes(Idx)))) :
643 AttributeSet();
644}
645
646AttributeSet AttributeSet::getRetAttributes() const {
647 return pImpl && hasAttributes(ReturnIndex) ?
648 AttributeSet::get(pImpl->getContext(),
Bill Wendling606c8e32013-01-29 03:20:31 +0000649 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000650 std::make_pair(ReturnIndex,
651 getAttributes(ReturnIndex)))) :
652 AttributeSet();
653}
654
655AttributeSet AttributeSet::getFnAttributes() const {
656 return pImpl && hasAttributes(FunctionIndex) ?
657 AttributeSet::get(pImpl->getContext(),
Bill Wendling606c8e32013-01-29 03:20:31 +0000658 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000659 std::make_pair(FunctionIndex,
660 getAttributes(FunctionIndex)))) :
661 AttributeSet();
662}
663
664bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{
Bill Wendling606c8e32013-01-29 03:20:31 +0000665 AttributeSetNode *ASN = getAttributes(Index);
666 return ASN ? ASN->hasAttribute(Kind) : false;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000667}
668
669bool AttributeSet::hasAttributes(unsigned Index) const {
Bill Wendling606c8e32013-01-29 03:20:31 +0000670 AttributeSetNode *ASN = getAttributes(Index);
671 return ASN ? ASN->hasAttributes() : false;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000672}
673
674/// \brief Return true if the specified attribute is set for at least one
675/// parameter or for the return value.
676bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr) const {
677 if (pImpl == 0) return false;
678
679 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
680 for (AttributeSetImpl::const_iterator II = pImpl->begin(I),
681 IE = pImpl->end(I); II != IE; ++II)
682 if (II->hasAttribute(Attr))
683 return true;
684
685 return false;
686}
687
Bill Wendling606c8e32013-01-29 03:20:31 +0000688unsigned AttributeSet::getParamAlignment(unsigned Index) const {
689 AttributeSetNode *ASN = getAttributes(Index);
690 return ASN ? ASN->getAlignment() : 0;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000691}
692
693unsigned AttributeSet::getStackAlignment(unsigned Index) const {
Bill Wendling606c8e32013-01-29 03:20:31 +0000694 AttributeSetNode *ASN = getAttributes(Index);
695 return ASN ? ASN->getStackAlignment() : 0;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000696}
697
698std::string AttributeSet::getAsString(unsigned Index) const {
Bill Wendling606c8e32013-01-29 03:20:31 +0000699 AttributeSetNode *ASN = getAttributes(Index);
700 return ASN ? ASN->getAsString() : std::string("");
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000701}
702
703/// \brief The attributes for the specified index are returned.
Bill Wendling606c8e32013-01-29 03:20:31 +0000704AttributeSetNode *AttributeSet::getAttributes(unsigned Idx) const {
705 if (!pImpl) return 0;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000706
Bill Wendling606c8e32013-01-29 03:20:31 +0000707 // Loop through to find the attribute node we want.
708 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
709 if (pImpl->getSlotIndex(I) == Idx)
710 return pImpl->getSlotNode(I);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000711
Bill Wendling606c8e32013-01-29 03:20:31 +0000712 return 0;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000713}
714
715//===----------------------------------------------------------------------===//
716// AttributeSet Introspection Methods
717//===----------------------------------------------------------------------===//
718
719/// \brief Return the number of slots used in this attribute list. This is the
720/// number of arguments that have an attribute set on them (including the
721/// function itself).
722unsigned AttributeSet::getNumSlots() const {
723 return pImpl ? pImpl->getNumAttributes() : 0;
724}
725
726uint64_t AttributeSet::getSlotIndex(unsigned Slot) const {
727 assert(pImpl && Slot < pImpl->getNumAttributes() &&
728 "Slot # out of range!");
729 return pImpl->getSlotIndex(Slot);
730}
731
732AttributeSet AttributeSet::getSlotAttributes(unsigned Slot) const {
733 assert(pImpl && Slot < pImpl->getNumAttributes() &&
734 "Slot # out of range!");
735 return pImpl->getSlotAttributes(Slot);
736}
737
738uint64_t AttributeSet::Raw(unsigned Index) const {
739 // FIXME: Remove this.
740 return pImpl ? pImpl->Raw(Index) : 0;
741}
742
743void AttributeSet::dump() const {
744 dbgs() << "PAL[\n";
745
746 for (unsigned i = 0, e = getNumSlots(); i < e; ++i) {
747 uint64_t Index = getSlotIndex(i);
748 dbgs() << " { ";
749 if (Index == ~0U)
750 dbgs() << "~0U";
751 else
752 dbgs() << Index;
753 dbgs() << " => " << getAsString(Index) << " }\n";
754 }
755
756 dbgs() << "]\n";
757}
758
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000759//===----------------------------------------------------------------------===//
Bill Wendling03198882013-01-04 23:27:34 +0000760// AttrBuilder Method Implementations
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000761//===----------------------------------------------------------------------===//
762
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000763AttrBuilder::AttrBuilder(AttributeSet AS, unsigned Idx)
764 : Alignment(0), StackAlignment(0) {
Bill Wendlingec258982013-01-27 21:23:46 +0000765 AttributeSetImpl *pImpl = AS.pImpl;
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000766 if (!pImpl) return;
767
Bill Wendling73bc4522013-01-28 00:21:34 +0000768 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I) {
769 if (pImpl->getSlotIndex(I) != Idx) continue;
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000770
Bill Wendling383da6b2013-01-30 21:22:59 +0000771 for (AttributeSetImpl::const_iterator II = pImpl->begin(I),
Bill Wendling73bc4522013-01-28 00:21:34 +0000772 IE = pImpl->end(I); II != IE; ++II)
Bill Wendling383da6b2013-01-30 21:22:59 +0000773 addAttributes(*II);
Bill Wendling73bc4522013-01-28 00:21:34 +0000774
775 break;
776 }
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000777}
778
Bill Wendling03198882013-01-04 23:27:34 +0000779void AttrBuilder::clear() {
780 Attrs.clear();
781 Alignment = StackAlignment = 0;
782}
783
784AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
785 Attrs.insert(Val);
Bill Wendling3a106e62012-10-09 19:01:18 +0000786 return *this;
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000787}
788
Bill Wendling03198882013-01-04 23:27:34 +0000789AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
790 Attrs.erase(Val);
791 if (Val == Attribute::Alignment)
792 Alignment = 0;
793 else if (Val == Attribute::StackAlignment)
794 StackAlignment = 0;
795
Bill Wendlinga19a5302012-10-14 04:10:01 +0000796 return *this;
797}
798
Bill Wendling49f60602013-01-28 05:23:28 +0000799AttrBuilder &AttrBuilder::addAttributes(Attribute Attr) {
800 uint64_t Mask = Attr.Raw();
801
802 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
803 I = Attribute::AttrKind(I + 1))
804 if ((Mask & AttributeImpl::getAttrMask(I)) != 0)
805 Attrs.insert(I);
806
807 if (Attr.getAlignment())
808 Alignment = Attr.getAlignment();
809 if (Attr.getStackAlignment())
810 StackAlignment = Attr.getStackAlignment();
811 return *this;
812}
813
Bill Wendlinge7436542013-01-30 23:07:40 +0000814AttrBuilder &AttrBuilder::removeAttributes(AttributeSet A, uint64_t Index) {
815 uint64_t Mask = A.Raw(Index);
Bill Wendling49f60602013-01-28 05:23:28 +0000816
817 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
818 I = Attribute::AttrKind(I + 1)) {
819 if (Mask & AttributeImpl::getAttrMask(I)) {
820 Attrs.erase(I);
821
822 if (I == Attribute::Alignment)
823 Alignment = 0;
824 else if (I == Attribute::StackAlignment)
825 StackAlignment = 0;
826 }
827 }
828
829 return *this;
830}
831
Bill Wendling702cc912012-10-15 20:35:56 +0000832AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
Bill Wendlingda3f9d82012-10-14 03:58:29 +0000833 if (Align == 0) return *this;
Bill Wendling03198882013-01-04 23:27:34 +0000834
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000835 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
836 assert(Align <= 0x40000000 && "Alignment too large.");
Bill Wendling03198882013-01-04 23:27:34 +0000837
838 Attrs.insert(Attribute::Alignment);
839 Alignment = Align;
Bill Wendlingda3f9d82012-10-14 03:58:29 +0000840 return *this;
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000841}
842
Bill Wendling03198882013-01-04 23:27:34 +0000843AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
844 // Default alignment, allow the target to define how to align it.
845 if (Align == 0) return *this;
846
847 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
848 assert(Align <= 0x100 && "Alignment too large.");
849
850 Attrs.insert(Attribute::StackAlignment);
851 StackAlignment = Align;
852 return *this;
853}
854
Bill Wendling22bd6412013-01-03 01:54:39 +0000855bool AttrBuilder::contains(Attribute::AttrKind A) const {
Bill Wendling03198882013-01-04 23:27:34 +0000856 return Attrs.count(A);
Bill Wendling7d2f2492012-10-10 07:36:45 +0000857}
858
Bill Wendling702cc912012-10-15 20:35:56 +0000859bool AttrBuilder::hasAttributes() const {
Bill Wendling03198882013-01-04 23:27:34 +0000860 return !Attrs.empty();
Bill Wendlingf385f4c2012-10-08 23:27:46 +0000861}
Bill Wendling60507d52013-01-04 20:54:35 +0000862
Bill Wendlinge7436542013-01-30 23:07:40 +0000863bool AttrBuilder::hasAttributes(AttributeSet A, uint64_t Index) const {
864 return Raw() & A.Raw(Index);
Bill Wendling8831c062012-10-09 00:01:21 +0000865}
Bill Wendling60507d52013-01-04 20:54:35 +0000866
Bill Wendling702cc912012-10-15 20:35:56 +0000867bool AttrBuilder::hasAlignmentAttr() const {
Bill Wendling03198882013-01-04 23:27:34 +0000868 return Alignment != 0;
Bill Wendlingf385f4c2012-10-08 23:27:46 +0000869}
870
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000871bool AttrBuilder::operator==(const AttrBuilder &B) {
872 SmallVector<Attribute::AttrKind, 8> This(Attrs.begin(), Attrs.end());
873 SmallVector<Attribute::AttrKind, 8> That(B.Attrs.begin(), B.Attrs.end());
874 return This == That;
875}
876
877AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) {
Bill Wendling8232ece2013-01-29 01:43:29 +0000878 if (!Val) return *this;
879
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000880 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
881 I = Attribute::AttrKind(I + 1)) {
882 if (uint64_t A = (Val & AttributeImpl::getAttrMask(I))) {
883 Attrs.insert(I);
884
885 if (I == Attribute::Alignment)
886 Alignment = 1ULL << ((A >> 16) - 1);
887 else if (I == Attribute::StackAlignment)
888 StackAlignment = 1ULL << ((A >> 26)-1);
889 }
890 }
891
892 return *this;
893}
894
Bill Wendling1db9b692013-01-09 23:36:50 +0000895uint64_t AttrBuilder::Raw() const {
Bill Wendling03198882013-01-04 23:27:34 +0000896 uint64_t Mask = 0;
897
898 for (DenseSet<Attribute::AttrKind>::const_iterator I = Attrs.begin(),
899 E = Attrs.end(); I != E; ++I) {
900 Attribute::AttrKind Kind = *I;
901
902 if (Kind == Attribute::Alignment)
903 Mask |= (Log2_32(Alignment) + 1) << 16;
904 else if (Kind == Attribute::StackAlignment)
905 Mask |= (Log2_32(StackAlignment) + 1) << 26;
906 else
907 Mask |= AttributeImpl::getAttrMask(Kind);
908 }
909
910 return Mask;
Bill Wendlingf385f4c2012-10-08 23:27:46 +0000911}
912
Bill Wendling8e47daf2013-01-25 23:09:36 +0000913//===----------------------------------------------------------------------===//
914// AttributeFuncs Function Defintions
915//===----------------------------------------------------------------------===//
916
Bill Wendlinge7436542013-01-30 23:07:40 +0000917AttributeSet AttributeFuncs::typeIncompatible(Type *Ty, uint64_t Index) {
Bill Wendling8e47daf2013-01-25 23:09:36 +0000918 AttrBuilder Incompatible;
919
920 if (!Ty->isIntegerTy())
921 // Attribute that only apply to integers.
922 Incompatible.addAttribute(Attribute::SExt)
923 .addAttribute(Attribute::ZExt);
924
925 if (!Ty->isPointerTy())
926 // Attribute that only apply to pointers.
927 Incompatible.addAttribute(Attribute::ByVal)
928 .addAttribute(Attribute::Nest)
929 .addAttribute(Attribute::NoAlias)
930 .addAttribute(Attribute::NoCapture)
931 .addAttribute(Attribute::StructRet);
932
Bill Wendlinge7436542013-01-30 23:07:40 +0000933 return AttributeSet::get(Ty->getContext(), Index, Incompatible);
Bill Wendling8e47daf2013-01-25 23:09:36 +0000934}
935
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000936/// \brief This returns an integer containing an encoding of all the LLVM
937/// attributes found in the given attribute bitset. Any change to this encoding
938/// is a breaking change to bitcode compatibility.
Bill Wendling8232ece2013-01-29 01:43:29 +0000939/// N.B. This should be used only by the bitcode reader!
Bill Wendling8e47daf2013-01-25 23:09:36 +0000940uint64_t AttributeFuncs::encodeLLVMAttributesForBitcode(AttributeSet Attrs,
941 unsigned Index) {
942 // FIXME: It doesn't make sense to store the alignment information as an
943 // expanded out value, we should store it as a log2 value. However, we can't
944 // just change that here without breaking bitcode compatibility. If this ever
945 // becomes a problem in practice, we should introduce new tag numbers in the
946 // bitcode file and have those tags use a more efficiently encoded alignment
947 // field.
948
949 // Store the alignment in the bitcode as a 16-bit raw value instead of a 5-bit
950 // log2 encoded value. Shift the bits above the alignment up by 11 bits.
951 uint64_t EncodedAttrs = Attrs.Raw(Index) & 0xffff;
952 if (Attrs.hasAttribute(Index, Attribute::Alignment))
953 EncodedAttrs |= Attrs.getParamAlignment(Index) << 16;
954 EncodedAttrs |= (Attrs.Raw(Index) & (0xffffULL << 21)) << 11;
955 return EncodedAttrs;
956}
957
Bill Wendling8232ece2013-01-29 01:43:29 +0000958/// \brief This fills an AttrBuilder object with the LLVM attributes that have
959/// been decoded from the given integer. This function must stay in sync with
960/// 'encodeLLVMAttributesForBitcode'.
961/// N.B. This should be used only by the bitcode reader!
962void AttributeFuncs::decodeLLVMAttributesForBitcode(LLVMContext &C,
963 AttrBuilder &B,
964 uint64_t EncodedAttrs) {
Bill Wendling8e47daf2013-01-25 23:09:36 +0000965 // The alignment is stored as a 16-bit raw value from bits 31--16. We shift
966 // the bits above 31 down by 11 bits.
967 unsigned Alignment = (EncodedAttrs & (0xffffULL << 16)) >> 16;
968 assert((!Alignment || isPowerOf2_32(Alignment)) &&
969 "Alignment must be a power of two.");
970
Bill Wendling8e47daf2013-01-25 23:09:36 +0000971 if (Alignment)
972 B.addAlignmentAttr(Alignment);
Bill Wendling606c8e32013-01-29 03:20:31 +0000973 B.addRawValue(((EncodedAttrs & (0xffffULL << 32)) >> 11) |
974 (EncodedAttrs & 0xffff));
Bill Wendling8e47daf2013-01-25 23:09:36 +0000975}