blob: 3a8cfe5ba6950823bbea78f52425af3bf118ce57 [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 Wendlingb96129d2013-01-31 01:04:51 +000033Attribute Attribute::get(LLVMContext &Context, AttrKind Kind) {
34 AttrBuilder B;
35 return Attribute::get(Context, B.addAttribute(Kind));
36}
37
38Attribute Attribute::get(LLVMContext &Context, AttrBuilder &B) {
39 // If there are no attributes, return an empty Attribute class.
40 if (!B.hasAttributes())
41 return Attribute();
42
43 assert(std::distance(B.begin(), B.end()) == 1 &&
44 "The Attribute object should represent one attribute only!");
Bill Wendling73dee182013-01-31 00:29:54 +000045
Bill Wendling8e635db2012-10-08 21:47:17 +000046 // Otherwise, build a key to look up the existing attributes.
47 LLVMContextImpl *pImpl = Context.pImpl;
48 FoldingSetNodeID ID;
Bill Wendlingb96129d2013-01-31 01:04:51 +000049 ConstantInt *CI = ConstantInt::get(Type::getInt64Ty(Context), B.Raw());
50 ID.AddPointer(CI);
Bill Wendling8e635db2012-10-08 21:47:17 +000051
52 void *InsertPoint;
Bill Wendlingf6670722012-12-20 01:36:59 +000053 AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
Bill Wendling8e635db2012-10-08 21:47:17 +000054
55 if (!PA) {
56 // If we didn't find any existing attributes of the same shape then create a
57 // new one and insert it.
Bill Wendlingb96129d2013-01-31 01:04:51 +000058 PA = new AttributeImpl(Context, CI);
Bill Wendling8e635db2012-10-08 21:47:17 +000059 pImpl->AttrsSet.InsertNode(PA, InsertPoint);
60 }
61
62 // Return the AttributesList that we found or created.
Bill Wendling034b94b2012-12-19 07:18:57 +000063 return Attribute(PA);
Bill Wendling8e635db2012-10-08 21:47:17 +000064}
65
Bill Wendlingc08a5ef2013-01-27 22:43:04 +000066Attribute Attribute::getWithAlignment(LLVMContext &Context, uint64_t Align) {
Bill Wendlingb96129d2013-01-31 01:04:51 +000067 AttrBuilder B;
68 return get(Context, B.addAlignmentAttr(Align));
Bill Wendlingc08a5ef2013-01-27 22:43:04 +000069}
70
71Attribute Attribute::getWithStackAlignment(LLVMContext &Context,
72 uint64_t Align) {
Bill Wendlingb96129d2013-01-31 01:04:51 +000073 AttrBuilder B;
74 return get(Context, B.addStackAlignmentAttr(Align));
Bill Wendlingc08a5ef2013-01-27 22:43:04 +000075}
76
Bill Wendling817abdd2013-01-29 00:48:16 +000077//===----------------------------------------------------------------------===//
78// Attribute Accessor Methods
79//===----------------------------------------------------------------------===//
80
Bill Wendling629fb822012-12-22 00:37:52 +000081bool Attribute::hasAttribute(AttrKind Val) const {
Bill Wendling27107f62012-12-20 21:28:43 +000082 return pImpl && pImpl->hasAttribute(Val);
Bill Wendlinge66f3d32012-10-05 06:44:41 +000083}
84
Bill Wendling6dc37812013-01-29 20:45:34 +000085Constant *Attribute::getAttributeKind() const {
86 return pImpl ? pImpl->getAttributeKind() : 0;
87}
88
89ArrayRef<Constant*> Attribute::getAttributeValues() const {
90 return pImpl ? pImpl->getAttributeValues() : ArrayRef<Constant*>();
91}
92
Bill Wendlinge66f3d32012-10-05 06:44:41 +000093/// This returns the alignment field of an attribute as a byte alignment value.
Bill Wendling034b94b2012-12-19 07:18:57 +000094unsigned Attribute::getAlignment() const {
95 if (!hasAttribute(Attribute::Alignment))
Bill Wendling9ef99c92012-10-09 20:56:48 +000096 return 0;
Bill Wendlinga8ab5fc2013-01-23 23:00:05 +000097 return pImpl->getAlignment();
Bill Wendlinge66f3d32012-10-05 06:44:41 +000098}
99
100/// This returns the stack alignment field of an attribute as a byte alignment
101/// value.
Bill Wendling034b94b2012-12-19 07:18:57 +0000102unsigned Attribute::getStackAlignment() const {
103 if (!hasAttribute(Attribute::StackAlignment))
Bill Wendling9ef99c92012-10-09 20:56:48 +0000104 return 0;
Bill Wendlinga8ab5fc2013-01-23 23:00:05 +0000105 return pImpl->getStackAlignment();
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000106}
107
Bill Wendling034b94b2012-12-19 07:18:57 +0000108std::string Attribute::getAsString() const {
Bill Wendling034b94b2012-12-19 07:18:57 +0000109 if (hasAttribute(Attribute::ZExt))
Bill Wendling606c8e32013-01-29 03:20:31 +0000110 return "zeroext";
Bill Wendling034b94b2012-12-19 07:18:57 +0000111 if (hasAttribute(Attribute::SExt))
Bill Wendling606c8e32013-01-29 03:20:31 +0000112 return "signext";
Bill Wendling034b94b2012-12-19 07:18:57 +0000113 if (hasAttribute(Attribute::NoReturn))
Bill Wendling606c8e32013-01-29 03:20:31 +0000114 return "noreturn";
Bill Wendling034b94b2012-12-19 07:18:57 +0000115 if (hasAttribute(Attribute::NoUnwind))
Bill Wendling606c8e32013-01-29 03:20:31 +0000116 return "nounwind";
Bill Wendling034b94b2012-12-19 07:18:57 +0000117 if (hasAttribute(Attribute::UWTable))
Bill Wendling606c8e32013-01-29 03:20:31 +0000118 return "uwtable";
Bill Wendling034b94b2012-12-19 07:18:57 +0000119 if (hasAttribute(Attribute::ReturnsTwice))
Bill Wendling606c8e32013-01-29 03:20:31 +0000120 return "returns_twice";
Bill Wendling034b94b2012-12-19 07:18:57 +0000121 if (hasAttribute(Attribute::InReg))
Bill Wendling606c8e32013-01-29 03:20:31 +0000122 return "inreg";
Bill Wendling034b94b2012-12-19 07:18:57 +0000123 if (hasAttribute(Attribute::NoAlias))
Bill Wendling606c8e32013-01-29 03:20:31 +0000124 return "noalias";
Bill Wendling034b94b2012-12-19 07:18:57 +0000125 if (hasAttribute(Attribute::NoCapture))
Bill Wendling606c8e32013-01-29 03:20:31 +0000126 return "nocapture";
Bill Wendling034b94b2012-12-19 07:18:57 +0000127 if (hasAttribute(Attribute::StructRet))
Bill Wendling606c8e32013-01-29 03:20:31 +0000128 return "sret";
Bill Wendling034b94b2012-12-19 07:18:57 +0000129 if (hasAttribute(Attribute::ByVal))
Bill Wendling606c8e32013-01-29 03:20:31 +0000130 return "byval";
Bill Wendling034b94b2012-12-19 07:18:57 +0000131 if (hasAttribute(Attribute::Nest))
Bill Wendling606c8e32013-01-29 03:20:31 +0000132 return "nest";
Bill Wendling034b94b2012-12-19 07:18:57 +0000133 if (hasAttribute(Attribute::ReadNone))
Bill Wendling606c8e32013-01-29 03:20:31 +0000134 return "readnone";
Bill Wendling034b94b2012-12-19 07:18:57 +0000135 if (hasAttribute(Attribute::ReadOnly))
Bill Wendling606c8e32013-01-29 03:20:31 +0000136 return "readonly";
Bill Wendling034b94b2012-12-19 07:18:57 +0000137 if (hasAttribute(Attribute::OptimizeForSize))
Bill Wendling606c8e32013-01-29 03:20:31 +0000138 return "optsize";
Bill Wendling034b94b2012-12-19 07:18:57 +0000139 if (hasAttribute(Attribute::NoInline))
Bill Wendling606c8e32013-01-29 03:20:31 +0000140 return "noinline";
Bill Wendling034b94b2012-12-19 07:18:57 +0000141 if (hasAttribute(Attribute::InlineHint))
Bill Wendling606c8e32013-01-29 03:20:31 +0000142 return "inlinehint";
Bill Wendling034b94b2012-12-19 07:18:57 +0000143 if (hasAttribute(Attribute::AlwaysInline))
Bill Wendling606c8e32013-01-29 03:20:31 +0000144 return "alwaysinline";
Bill Wendling034b94b2012-12-19 07:18:57 +0000145 if (hasAttribute(Attribute::StackProtect))
Bill Wendling606c8e32013-01-29 03:20:31 +0000146 return "ssp";
Bill Wendling034b94b2012-12-19 07:18:57 +0000147 if (hasAttribute(Attribute::StackProtectReq))
Bill Wendling606c8e32013-01-29 03:20:31 +0000148 return "sspreq";
Bill Wendling114baee2013-01-23 06:41:41 +0000149 if (hasAttribute(Attribute::StackProtectStrong))
Bill Wendling606c8e32013-01-29 03:20:31 +0000150 return "sspstrong";
Bill Wendling034b94b2012-12-19 07:18:57 +0000151 if (hasAttribute(Attribute::NoRedZone))
Bill Wendling606c8e32013-01-29 03:20:31 +0000152 return "noredzone";
Bill Wendling034b94b2012-12-19 07:18:57 +0000153 if (hasAttribute(Attribute::NoImplicitFloat))
Bill Wendling606c8e32013-01-29 03:20:31 +0000154 return "noimplicitfloat";
Bill Wendling034b94b2012-12-19 07:18:57 +0000155 if (hasAttribute(Attribute::Naked))
Bill Wendling606c8e32013-01-29 03:20:31 +0000156 return "naked";
Bill Wendling034b94b2012-12-19 07:18:57 +0000157 if (hasAttribute(Attribute::NonLazyBind))
Bill Wendling606c8e32013-01-29 03:20:31 +0000158 return "nonlazybind";
Bill Wendling034b94b2012-12-19 07:18:57 +0000159 if (hasAttribute(Attribute::AddressSafety))
Bill Wendling606c8e32013-01-29 03:20:31 +0000160 return "address_safety";
Bill Wendling034b94b2012-12-19 07:18:57 +0000161 if (hasAttribute(Attribute::MinSize))
Bill Wendling606c8e32013-01-29 03:20:31 +0000162 return "minsize";
Bill Wendling034b94b2012-12-19 07:18:57 +0000163 if (hasAttribute(Attribute::StackAlignment)) {
Bill Wendling606c8e32013-01-29 03:20:31 +0000164 std::string Result;
Charles Davis1e063d12010-02-12 00:31:15 +0000165 Result += "alignstack(";
Bill Wendlingef99fe82012-09-21 15:26:31 +0000166 Result += utostr(getStackAlignment());
Bill Wendling606c8e32013-01-29 03:20:31 +0000167 Result += ")";
168 return Result;
Charles Davis1e063d12010-02-12 00:31:15 +0000169 }
Bill Wendling034b94b2012-12-19 07:18:57 +0000170 if (hasAttribute(Attribute::Alignment)) {
Bill Wendling606c8e32013-01-29 03:20:31 +0000171 std::string Result;
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000172 Result += "align ";
Bill Wendlingef99fe82012-09-21 15:26:31 +0000173 Result += utostr(getAlignment());
Bill Wendling606c8e32013-01-29 03:20:31 +0000174 Result += "";
175 return Result;
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000176 }
James Molloy67ae1352012-12-20 16:04:27 +0000177 if (hasAttribute(Attribute::NoDuplicate))
Bill Wendling606c8e32013-01-29 03:20:31 +0000178 return "noduplicate";
179
180 llvm_unreachable("Unknown attribute");
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000181}
182
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000183bool Attribute::operator==(AttrKind K) const {
NAKAMURA Takumieddab152013-01-31 03:47:28 +0000184 return pImpl && *pImpl == K;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000185}
186bool Attribute::operator!=(AttrKind K) const {
187 return !(*this == K);
188}
189
190bool Attribute::operator<(Attribute A) const {
191 if (!pImpl && !A.pImpl) return false;
192 if (!pImpl) return true;
193 if (!A.pImpl) return false;
194 return *pImpl < *A.pImpl;
195}
196
197uint64_t Attribute::Raw() const {
198 return pImpl ? pImpl->Raw() : 0;
199}
200
201//===----------------------------------------------------------------------===//
202// AttributeImpl Definition
203//===----------------------------------------------------------------------===//
204
Bill Wendling9f175f82013-01-29 20:37:10 +0000205AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind kind)
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000206 : Context(C) {
Bill Wendling9f175f82013-01-29 20:37:10 +0000207 Kind = ConstantInt::get(Type::getInt64Ty(C), kind);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000208}
Bill Wendling9f175f82013-01-29 20:37:10 +0000209AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind kind,
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000210 ArrayRef<Constant*> values)
211 : Context(C) {
Bill Wendling9f175f82013-01-29 20:37:10 +0000212 Kind = ConstantInt::get(Type::getInt64Ty(C), kind);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000213 Vals.reserve(values.size());
214 Vals.append(values.begin(), values.end());
215}
Bill Wendling9f175f82013-01-29 20:37:10 +0000216AttributeImpl::AttributeImpl(LLVMContext &C, StringRef kind)
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000217 : Context(C) {
Bill Wendling9f175f82013-01-29 20:37:10 +0000218 Kind = ConstantDataArray::getString(C, kind);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000219}
220
221bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
222 return (Raw() & getAttrMask(A)) != 0;
223}
224
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000225uint64_t AttributeImpl::getAlignment() const {
226 uint64_t Mask = Raw() & getAttrMask(Attribute::Alignment);
227 return 1ULL << ((Mask >> 16) - 1);
228}
229
230uint64_t AttributeImpl::getStackAlignment() const {
231 uint64_t Mask = Raw() & getAttrMask(Attribute::StackAlignment);
232 return 1ULL << ((Mask >> 26) - 1);
233}
234
Bill Wendling9f175f82013-01-29 20:37:10 +0000235bool AttributeImpl::operator==(Attribute::AttrKind kind) const {
236 if (ConstantInt *CI = dyn_cast<ConstantInt>(Kind))
237 return CI->getZExtValue() == kind;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000238 return false;
239}
Bill Wendling9f175f82013-01-29 20:37:10 +0000240bool AttributeImpl::operator!=(Attribute::AttrKind kind) const {
241 return !(*this == kind);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000242}
243
Bill Wendling9f175f82013-01-29 20:37:10 +0000244bool AttributeImpl::operator==(StringRef kind) const {
245 if (ConstantDataArray *CDA = dyn_cast<ConstantDataArray>(Kind))
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000246 if (CDA->isString())
Bill Wendling9f175f82013-01-29 20:37:10 +0000247 return CDA->getAsString() == kind;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000248 return false;
249}
250
Bill Wendling9f175f82013-01-29 20:37:10 +0000251bool AttributeImpl::operator!=(StringRef kind) const {
252 return !(*this == kind);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000253}
254
255bool AttributeImpl::operator<(const AttributeImpl &AI) const {
Bill Wendling9f175f82013-01-29 20:37:10 +0000256 if (!Kind && !AI.Kind) return false;
257 if (!Kind && AI.Kind) return true;
258 if (Kind && !AI.Kind) return false;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000259
Bill Wendling9f175f82013-01-29 20:37:10 +0000260 ConstantInt *ThisCI = dyn_cast<ConstantInt>(Kind);
261 ConstantInt *ThatCI = dyn_cast<ConstantInt>(AI.Kind);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000262
Bill Wendling9f175f82013-01-29 20:37:10 +0000263 ConstantDataArray *ThisCDA = dyn_cast<ConstantDataArray>(Kind);
264 ConstantDataArray *ThatCDA = dyn_cast<ConstantDataArray>(AI.Kind);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000265
266 if (ThisCI && ThatCI)
267 return ThisCI->getZExtValue() < ThatCI->getZExtValue();
268
269 if (ThisCI && ThatCDA)
270 return true;
271
272 if (ThisCDA && ThatCI)
273 return false;
274
275 return ThisCDA->getAsString() < ThatCDA->getAsString();
276}
277
278uint64_t AttributeImpl::Raw() const {
279 // FIXME: Remove this.
Bill Wendling9f175f82013-01-29 20:37:10 +0000280 return cast<ConstantInt>(Kind)->getZExtValue();
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000281}
282
283uint64_t AttributeImpl::getAttrMask(Attribute::AttrKind Val) {
284 // FIXME: Remove this.
285 switch (Val) {
286 case Attribute::EndAttrKinds:
287 case Attribute::AttrKindEmptyKey:
288 case Attribute::AttrKindTombstoneKey:
289 llvm_unreachable("Synthetic enumerators which should never get here");
290
291 case Attribute::None: return 0;
292 case Attribute::ZExt: return 1 << 0;
293 case Attribute::SExt: return 1 << 1;
294 case Attribute::NoReturn: return 1 << 2;
295 case Attribute::InReg: return 1 << 3;
296 case Attribute::StructRet: return 1 << 4;
297 case Attribute::NoUnwind: return 1 << 5;
298 case Attribute::NoAlias: return 1 << 6;
299 case Attribute::ByVal: return 1 << 7;
300 case Attribute::Nest: return 1 << 8;
301 case Attribute::ReadNone: return 1 << 9;
302 case Attribute::ReadOnly: return 1 << 10;
303 case Attribute::NoInline: return 1 << 11;
304 case Attribute::AlwaysInline: return 1 << 12;
305 case Attribute::OptimizeForSize: return 1 << 13;
306 case Attribute::StackProtect: return 1 << 14;
307 case Attribute::StackProtectReq: return 1 << 15;
308 case Attribute::Alignment: return 31 << 16;
309 case Attribute::NoCapture: return 1 << 21;
310 case Attribute::NoRedZone: return 1 << 22;
311 case Attribute::NoImplicitFloat: return 1 << 23;
312 case Attribute::Naked: return 1 << 24;
313 case Attribute::InlineHint: return 1 << 25;
314 case Attribute::StackAlignment: return 7 << 26;
315 case Attribute::ReturnsTwice: return 1 << 29;
316 case Attribute::UWTable: return 1 << 30;
317 case Attribute::NonLazyBind: return 1U << 31;
318 case Attribute::AddressSafety: return 1ULL << 32;
319 case Attribute::MinSize: return 1ULL << 33;
320 case Attribute::NoDuplicate: return 1ULL << 34;
321 case Attribute::StackProtectStrong: return 1ULL << 35;
322 }
323 llvm_unreachable("Unsupported attribute type");
324}
325
326//===----------------------------------------------------------------------===//
327// AttributeSetNode Definition
328//===----------------------------------------------------------------------===//
329
330AttributeSetNode *AttributeSetNode::get(LLVMContext &C,
331 ArrayRef<Attribute> Attrs) {
332 if (Attrs.empty())
333 return 0;
334
335 // Otherwise, build a key to look up the existing attributes.
336 LLVMContextImpl *pImpl = C.pImpl;
337 FoldingSetNodeID ID;
338
339 SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end());
340 std::sort(SortedAttrs.begin(), SortedAttrs.end());
341
342 for (SmallVectorImpl<Attribute>::iterator I = SortedAttrs.begin(),
343 E = SortedAttrs.end(); I != E; ++I)
344 I->Profile(ID);
345
346 void *InsertPoint;
347 AttributeSetNode *PA =
348 pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint);
349
350 // If we didn't find any existing attributes of the same shape then create a
351 // new one and insert it.
352 if (!PA) {
353 PA = new AttributeSetNode(SortedAttrs);
354 pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint);
355 }
356
357 // Return the AttributesListNode that we found or created.
358 return PA;
359}
360
Bill Wendling606c8e32013-01-29 03:20:31 +0000361bool AttributeSetNode::hasAttribute(Attribute::AttrKind Kind) const {
362 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
363 E = AttrList.end(); I != E; ++I)
NAKAMURA Takumieddab152013-01-31 03:47:28 +0000364 if (I->hasAttribute(Kind))
Bill Wendling606c8e32013-01-29 03:20:31 +0000365 return true;
366 return false;
367}
368
369unsigned AttributeSetNode::getAlignment() const {
370 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
371 E = AttrList.end(); I != E; ++I)
NAKAMURA Takumieddab152013-01-31 03:47:28 +0000372 if (I->hasAttribute(Attribute::Alignment))
Bill Wendling606c8e32013-01-29 03:20:31 +0000373 return I->getAlignment();
374 return 0;
375}
376
377unsigned AttributeSetNode::getStackAlignment() const {
378 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
379 E = AttrList.end(); I != E; ++I)
NAKAMURA Takumieddab152013-01-31 03:47:28 +0000380 if (I->hasAttribute(Attribute::StackAlignment))
Bill Wendling606c8e32013-01-29 03:20:31 +0000381 return I->getStackAlignment();
382 return 0;
383}
384
385std::string AttributeSetNode::getAsString() const {
386 std::string Str = "";
387 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
388 E = AttrList.end(); I != E; ++I) {
389 if (I != AttrList.begin()) Str += " ";
390 Str += I->getAsString();
391 }
392 return Str;
393}
394
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000395//===----------------------------------------------------------------------===//
396// AttributeSetImpl Definition
397//===----------------------------------------------------------------------===//
398
399uint64_t AttributeSetImpl::Raw(uint64_t Index) const {
400 for (unsigned I = 0, E = getNumAttributes(); I != E; ++I) {
401 if (getSlotIndex(I) != Index) continue;
402 const AttributeSetNode *ASN = AttrNodes[I].second;
403 AttrBuilder B;
404
405 for (AttributeSetNode::const_iterator II = ASN->begin(),
406 IE = ASN->end(); II != IE; ++II)
407 B.addAttributes(*II);
408 return B.Raw();
409 }
410
411 return 0;
412}
413
414//===----------------------------------------------------------------------===//
415// AttributeSet Construction and Mutation Methods
416//===----------------------------------------------------------------------===//
417
Bill Wendling8232ece2013-01-29 01:43:29 +0000418AttributeSet
419AttributeSet::getImpl(LLVMContext &C,
420 ArrayRef<std::pair<unsigned, AttributeSetNode*> > Attrs) {
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000421 LLVMContextImpl *pImpl = C.pImpl;
422 FoldingSetNodeID ID;
423 AttributeSetImpl::Profile(ID, Attrs);
424
425 void *InsertPoint;
426 AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
427
428 // If we didn't find any existing attributes of the same shape then
429 // create a new one and insert it.
430 if (!PA) {
431 PA = new AttributeSetImpl(C, Attrs);
432 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
433 }
434
435 // Return the AttributesList that we found or created.
436 return AttributeSet(PA);
437}
438
439AttributeSet AttributeSet::get(LLVMContext &C,
440 ArrayRef<std::pair<unsigned, Attribute> > Attrs){
441 // If there are no attributes then return a null AttributesList pointer.
442 if (Attrs.empty())
443 return AttributeSet();
444
445#ifndef NDEBUG
446 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
447 assert((!i || Attrs[i-1].first <= Attrs[i].first) &&
448 "Misordered Attributes list!");
Bill Wendling82aea642013-01-31 06:22:35 +0000449 assert(Attrs[i].second != Attribute::None &&
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000450 "Pointless attribute!");
451 }
452#endif
453
454 // Create a vector if (unsigned, AttributeSetNode*) pairs from the attributes
455 // list.
456 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrPairVec;
457 for (ArrayRef<std::pair<unsigned, Attribute> >::iterator I = Attrs.begin(),
458 E = Attrs.end(); I != E; ) {
459 unsigned Index = I->first;
460 SmallVector<Attribute, 4> AttrVec;
NAKAMURA Takumi3ba51ce2013-01-29 15:18:16 +0000461 while (I != E && I->first == Index) {
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000462 AttrVec.push_back(I->second);
463 ++I;
464 }
465
466 AttrPairVec.push_back(std::make_pair(Index,
467 AttributeSetNode::get(C, AttrVec)));
468 }
469
470 return getImpl(C, AttrPairVec);
471}
472
473AttributeSet AttributeSet::get(LLVMContext &C,
474 ArrayRef<std::pair<unsigned,
475 AttributeSetNode*> > Attrs) {
476 // If there are no attributes then return a null AttributesList pointer.
477 if (Attrs.empty())
478 return AttributeSet();
479
480 return getImpl(C, Attrs);
481}
482
483AttributeSet AttributeSet::get(LLVMContext &C, unsigned Idx, AttrBuilder &B) {
484 if (!B.hasAttributes())
485 return AttributeSet();
Bill Wendling8fbc0c22013-01-29 01:02:03 +0000486
487 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
488 for (AttrBuilder::iterator I = B.begin(), E = B.end(); I != E; ++I) {
489 Attribute::AttrKind Kind = *I;
490 if (Kind == Attribute::Alignment)
491 Attrs.push_back(std::make_pair(Idx, Attribute::
492 getWithAlignment(C, B.getAlignment())));
493 else if (Kind == Attribute::StackAlignment)
494 Attrs.push_back(std::make_pair(Idx, Attribute::
495 getWithStackAlignment(C, B.getStackAlignment())));
496 else
497 Attrs.push_back(std::make_pair(Idx, Attribute::get(C, Kind)));
498 }
499
500 return get(C, Attrs);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000501}
502
503AttributeSet AttributeSet::get(LLVMContext &C, unsigned Idx,
504 ArrayRef<Attribute::AttrKind> Kind) {
505 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
506 for (ArrayRef<Attribute::AttrKind>::iterator I = Kind.begin(),
507 E = Kind.end(); I != E; ++I)
508 Attrs.push_back(std::make_pair(Idx, Attribute::get(C, *I)));
509 return get(C, Attrs);
510}
511
512AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<AttributeSet> Attrs) {
513 if (Attrs.empty()) return AttributeSet();
514
515 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrNodeVec;
516 for (unsigned I = 0, E = Attrs.size(); I != E; ++I) {
517 AttributeSet AS = Attrs[I];
518 if (!AS.pImpl) continue;
519 AttrNodeVec.append(AS.pImpl->AttrNodes.begin(), AS.pImpl->AttrNodes.end());
520 }
521
522 return getImpl(C, AttrNodeVec);
523}
524
525AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Idx,
526 Attribute::AttrKind Attr) const {
527 return addAttributes(C, Idx, AttributeSet::get(C, Idx, Attr));
528}
529
530AttributeSet AttributeSet::addAttributes(LLVMContext &C, unsigned Idx,
531 AttributeSet Attrs) const {
532 if (!pImpl) return Attrs;
533 if (!Attrs.pImpl) return *this;
534
535#ifndef NDEBUG
536 // FIXME it is not obvious how this should work for alignment. For now, say
537 // we can't change a known alignment.
538 unsigned OldAlign = getParamAlignment(Idx);
539 unsigned NewAlign = Attrs.getParamAlignment(Idx);
540 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
541 "Attempt to change alignment!");
542#endif
543
544 // Add the attribute slots before the one we're trying to add.
545 SmallVector<AttributeSet, 4> AttrSet;
546 uint64_t NumAttrs = pImpl->getNumAttributes();
547 AttributeSet AS;
548 uint64_t LastIndex = 0;
549 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
550 if (getSlotIndex(I) >= Idx) {
551 if (getSlotIndex(I) == Idx) AS = getSlotAttributes(LastIndex++);
552 break;
553 }
554 LastIndex = I + 1;
555 AttrSet.push_back(getSlotAttributes(I));
556 }
557
558 // Now add the attribute into the correct slot. There may already be an
559 // AttributeSet there.
560 AttrBuilder B(AS, Idx);
561
562 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
563 if (Attrs.getSlotIndex(I) == Idx) {
564 for (AttributeSetImpl::const_iterator II = Attrs.pImpl->begin(I),
565 IE = Attrs.pImpl->end(I); II != IE; ++II)
566 B.addAttributes(*II);
567 break;
568 }
569
570 AttrSet.push_back(AttributeSet::get(C, Idx, B));
571
572 // Add the remaining attribute slots.
573 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
574 AttrSet.push_back(getSlotAttributes(I));
575
576 return get(C, AttrSet);
577}
578
579AttributeSet AttributeSet::removeAttribute(LLVMContext &C, unsigned Idx,
580 Attribute::AttrKind Attr) const {
581 return removeAttributes(C, Idx, AttributeSet::get(C, Idx, Attr));
582}
583
584AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Idx,
585 AttributeSet Attrs) const {
586 if (!pImpl) return AttributeSet();
587 if (!Attrs.pImpl) return *this;
588
589#ifndef NDEBUG
590 // FIXME it is not obvious how this should work for alignment.
591 // For now, say we can't pass in alignment, which no current use does.
592 assert(!Attrs.hasAttribute(Idx, Attribute::Alignment) &&
593 "Attempt to change alignment!");
594#endif
595
596 // Add the attribute slots before the one we're trying to add.
597 SmallVector<AttributeSet, 4> AttrSet;
598 uint64_t NumAttrs = pImpl->getNumAttributes();
599 AttributeSet AS;
600 uint64_t LastIndex = 0;
601 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
602 if (getSlotIndex(I) >= Idx) {
603 if (getSlotIndex(I) == Idx) AS = getSlotAttributes(LastIndex++);
604 break;
605 }
606 LastIndex = I + 1;
607 AttrSet.push_back(getSlotAttributes(I));
608 }
609
Bill Wendlinge7436542013-01-30 23:07:40 +0000610 // Now remove the attribute from the correct slot. There may already be an
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000611 // AttributeSet there.
612 AttrBuilder B(AS, Idx);
613
614 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
615 if (Attrs.getSlotIndex(I) == Idx) {
Bill Wendlinge7436542013-01-30 23:07:40 +0000616 B.removeAttributes(Attrs.pImpl->getSlotAttributes(I), Idx);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000617 break;
618 }
619
620 AttrSet.push_back(AttributeSet::get(C, Idx, B));
621
622 // Add the remaining attribute slots.
623 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
624 AttrSet.push_back(getSlotAttributes(I));
625
626 return get(C, AttrSet);
627}
628
629//===----------------------------------------------------------------------===//
630// AttributeSet Accessor Methods
631//===----------------------------------------------------------------------===//
632
633AttributeSet AttributeSet::getParamAttributes(unsigned Idx) const {
634 return pImpl && hasAttributes(Idx) ?
635 AttributeSet::get(pImpl->getContext(),
Bill Wendling606c8e32013-01-29 03:20:31 +0000636 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000637 std::make_pair(Idx, getAttributes(Idx)))) :
638 AttributeSet();
639}
640
641AttributeSet AttributeSet::getRetAttributes() const {
642 return pImpl && hasAttributes(ReturnIndex) ?
643 AttributeSet::get(pImpl->getContext(),
Bill Wendling606c8e32013-01-29 03:20:31 +0000644 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000645 std::make_pair(ReturnIndex,
646 getAttributes(ReturnIndex)))) :
647 AttributeSet();
648}
649
650AttributeSet AttributeSet::getFnAttributes() const {
651 return pImpl && hasAttributes(FunctionIndex) ?
652 AttributeSet::get(pImpl->getContext(),
Bill Wendling606c8e32013-01-29 03:20:31 +0000653 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000654 std::make_pair(FunctionIndex,
655 getAttributes(FunctionIndex)))) :
656 AttributeSet();
657}
658
659bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{
Bill Wendling606c8e32013-01-29 03:20:31 +0000660 AttributeSetNode *ASN = getAttributes(Index);
661 return ASN ? ASN->hasAttribute(Kind) : false;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000662}
663
664bool AttributeSet::hasAttributes(unsigned Index) const {
Bill Wendling606c8e32013-01-29 03:20:31 +0000665 AttributeSetNode *ASN = getAttributes(Index);
666 return ASN ? ASN->hasAttributes() : false;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000667}
668
669/// \brief Return true if the specified attribute is set for at least one
670/// parameter or for the return value.
671bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr) const {
672 if (pImpl == 0) return false;
673
674 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
675 for (AttributeSetImpl::const_iterator II = pImpl->begin(I),
676 IE = pImpl->end(I); II != IE; ++II)
NAKAMURA Takumieddab152013-01-31 03:47:28 +0000677 if (II->hasAttribute(Attr))
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000678 return true;
679
680 return false;
681}
682
Bill Wendling606c8e32013-01-29 03:20:31 +0000683unsigned AttributeSet::getParamAlignment(unsigned Index) const {
684 AttributeSetNode *ASN = getAttributes(Index);
685 return ASN ? ASN->getAlignment() : 0;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000686}
687
688unsigned AttributeSet::getStackAlignment(unsigned Index) const {
Bill Wendling606c8e32013-01-29 03:20:31 +0000689 AttributeSetNode *ASN = getAttributes(Index);
690 return ASN ? ASN->getStackAlignment() : 0;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000691}
692
693std::string AttributeSet::getAsString(unsigned Index) const {
Bill Wendling606c8e32013-01-29 03:20:31 +0000694 AttributeSetNode *ASN = getAttributes(Index);
695 return ASN ? ASN->getAsString() : std::string("");
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000696}
697
698/// \brief The attributes for the specified index are returned.
Bill Wendling606c8e32013-01-29 03:20:31 +0000699AttributeSetNode *AttributeSet::getAttributes(unsigned Idx) const {
700 if (!pImpl) return 0;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000701
Bill Wendling606c8e32013-01-29 03:20:31 +0000702 // Loop through to find the attribute node we want.
703 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
704 if (pImpl->getSlotIndex(I) == Idx)
705 return pImpl->getSlotNode(I);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000706
Bill Wendling606c8e32013-01-29 03:20:31 +0000707 return 0;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000708}
709
710//===----------------------------------------------------------------------===//
711// AttributeSet Introspection Methods
712//===----------------------------------------------------------------------===//
713
714/// \brief Return the number of slots used in this attribute list. This is the
715/// number of arguments that have an attribute set on them (including the
716/// function itself).
717unsigned AttributeSet::getNumSlots() const {
718 return pImpl ? pImpl->getNumAttributes() : 0;
719}
720
721uint64_t AttributeSet::getSlotIndex(unsigned Slot) const {
722 assert(pImpl && Slot < pImpl->getNumAttributes() &&
723 "Slot # out of range!");
724 return pImpl->getSlotIndex(Slot);
725}
726
727AttributeSet AttributeSet::getSlotAttributes(unsigned Slot) const {
728 assert(pImpl && Slot < pImpl->getNumAttributes() &&
729 "Slot # out of range!");
730 return pImpl->getSlotAttributes(Slot);
731}
732
733uint64_t AttributeSet::Raw(unsigned Index) const {
734 // FIXME: Remove this.
735 return pImpl ? pImpl->Raw(Index) : 0;
736}
737
738void AttributeSet::dump() const {
739 dbgs() << "PAL[\n";
740
741 for (unsigned i = 0, e = getNumSlots(); i < e; ++i) {
742 uint64_t Index = getSlotIndex(i);
743 dbgs() << " { ";
744 if (Index == ~0U)
745 dbgs() << "~0U";
746 else
747 dbgs() << Index;
748 dbgs() << " => " << getAsString(Index) << " }\n";
749 }
750
751 dbgs() << "]\n";
752}
753
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000754//===----------------------------------------------------------------------===//
Bill Wendling03198882013-01-04 23:27:34 +0000755// AttrBuilder Method Implementations
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000756//===----------------------------------------------------------------------===//
757
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000758AttrBuilder::AttrBuilder(AttributeSet AS, unsigned Idx)
759 : Alignment(0), StackAlignment(0) {
Bill Wendlingec258982013-01-27 21:23:46 +0000760 AttributeSetImpl *pImpl = AS.pImpl;
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000761 if (!pImpl) return;
762
Bill Wendling73bc4522013-01-28 00:21:34 +0000763 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I) {
764 if (pImpl->getSlotIndex(I) != Idx) continue;
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000765
Bill Wendling383da6b2013-01-30 21:22:59 +0000766 for (AttributeSetImpl::const_iterator II = pImpl->begin(I),
Bill Wendling73bc4522013-01-28 00:21:34 +0000767 IE = pImpl->end(I); II != IE; ++II)
Bill Wendling383da6b2013-01-30 21:22:59 +0000768 addAttributes(*II);
Bill Wendling73bc4522013-01-28 00:21:34 +0000769
770 break;
771 }
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000772}
773
Bill Wendling03198882013-01-04 23:27:34 +0000774void AttrBuilder::clear() {
775 Attrs.clear();
776 Alignment = StackAlignment = 0;
777}
778
779AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
780 Attrs.insert(Val);
Bill Wendling3a106e62012-10-09 19:01:18 +0000781 return *this;
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000782}
783
Bill Wendling03198882013-01-04 23:27:34 +0000784AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
785 Attrs.erase(Val);
786 if (Val == Attribute::Alignment)
787 Alignment = 0;
788 else if (Val == Attribute::StackAlignment)
789 StackAlignment = 0;
790
Bill Wendlinga19a5302012-10-14 04:10:01 +0000791 return *this;
792}
793
Bill Wendling49f60602013-01-28 05:23:28 +0000794AttrBuilder &AttrBuilder::addAttributes(Attribute Attr) {
795 uint64_t Mask = Attr.Raw();
796
797 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
798 I = Attribute::AttrKind(I + 1))
799 if ((Mask & AttributeImpl::getAttrMask(I)) != 0)
800 Attrs.insert(I);
801
802 if (Attr.getAlignment())
803 Alignment = Attr.getAlignment();
804 if (Attr.getStackAlignment())
805 StackAlignment = Attr.getStackAlignment();
806 return *this;
807}
808
Bill Wendlinge7436542013-01-30 23:07:40 +0000809AttrBuilder &AttrBuilder::removeAttributes(AttributeSet A, uint64_t Index) {
810 uint64_t Mask = A.Raw(Index);
Bill Wendling49f60602013-01-28 05:23:28 +0000811
812 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
813 I = Attribute::AttrKind(I + 1)) {
814 if (Mask & AttributeImpl::getAttrMask(I)) {
815 Attrs.erase(I);
816
817 if (I == Attribute::Alignment)
818 Alignment = 0;
819 else if (I == Attribute::StackAlignment)
820 StackAlignment = 0;
821 }
822 }
823
824 return *this;
825}
826
Bill Wendling702cc912012-10-15 20:35:56 +0000827AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
Bill Wendlingda3f9d82012-10-14 03:58:29 +0000828 if (Align == 0) return *this;
Bill Wendling03198882013-01-04 23:27:34 +0000829
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000830 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
831 assert(Align <= 0x40000000 && "Alignment too large.");
Bill Wendling03198882013-01-04 23:27:34 +0000832
833 Attrs.insert(Attribute::Alignment);
834 Alignment = Align;
Bill Wendlingda3f9d82012-10-14 03:58:29 +0000835 return *this;
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000836}
837
Bill Wendling03198882013-01-04 23:27:34 +0000838AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
839 // Default alignment, allow the target to define how to align it.
840 if (Align == 0) return *this;
841
842 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
843 assert(Align <= 0x100 && "Alignment too large.");
844
845 Attrs.insert(Attribute::StackAlignment);
846 StackAlignment = Align;
847 return *this;
848}
849
Bill Wendling22bd6412013-01-03 01:54:39 +0000850bool AttrBuilder::contains(Attribute::AttrKind A) const {
Bill Wendling03198882013-01-04 23:27:34 +0000851 return Attrs.count(A);
Bill Wendling7d2f2492012-10-10 07:36:45 +0000852}
853
Bill Wendling702cc912012-10-15 20:35:56 +0000854bool AttrBuilder::hasAttributes() const {
Bill Wendling03198882013-01-04 23:27:34 +0000855 return !Attrs.empty();
Bill Wendlingf385f4c2012-10-08 23:27:46 +0000856}
Bill Wendling60507d52013-01-04 20:54:35 +0000857
Bill Wendlinge7436542013-01-30 23:07:40 +0000858bool AttrBuilder::hasAttributes(AttributeSet A, uint64_t Index) const {
859 return Raw() & A.Raw(Index);
Bill Wendling8831c062012-10-09 00:01:21 +0000860}
Bill Wendling60507d52013-01-04 20:54:35 +0000861
Bill Wendling702cc912012-10-15 20:35:56 +0000862bool AttrBuilder::hasAlignmentAttr() const {
Bill Wendling03198882013-01-04 23:27:34 +0000863 return Alignment != 0;
Bill Wendlingf385f4c2012-10-08 23:27:46 +0000864}
865
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000866bool AttrBuilder::operator==(const AttrBuilder &B) {
867 SmallVector<Attribute::AttrKind, 8> This(Attrs.begin(), Attrs.end());
868 SmallVector<Attribute::AttrKind, 8> That(B.Attrs.begin(), B.Attrs.end());
869 return This == That;
870}
871
872AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) {
Bill Wendling8232ece2013-01-29 01:43:29 +0000873 if (!Val) return *this;
874
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000875 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
876 I = Attribute::AttrKind(I + 1)) {
877 if (uint64_t A = (Val & AttributeImpl::getAttrMask(I))) {
878 Attrs.insert(I);
879
880 if (I == Attribute::Alignment)
881 Alignment = 1ULL << ((A >> 16) - 1);
882 else if (I == Attribute::StackAlignment)
883 StackAlignment = 1ULL << ((A >> 26)-1);
884 }
885 }
886
887 return *this;
888}
889
Bill Wendling1db9b692013-01-09 23:36:50 +0000890uint64_t AttrBuilder::Raw() const {
Bill Wendling03198882013-01-04 23:27:34 +0000891 uint64_t Mask = 0;
892
893 for (DenseSet<Attribute::AttrKind>::const_iterator I = Attrs.begin(),
894 E = Attrs.end(); I != E; ++I) {
895 Attribute::AttrKind Kind = *I;
896
897 if (Kind == Attribute::Alignment)
898 Mask |= (Log2_32(Alignment) + 1) << 16;
899 else if (Kind == Attribute::StackAlignment)
900 Mask |= (Log2_32(StackAlignment) + 1) << 26;
901 else
902 Mask |= AttributeImpl::getAttrMask(Kind);
903 }
904
905 return Mask;
Bill Wendlingf385f4c2012-10-08 23:27:46 +0000906}
907
Bill Wendling8e47daf2013-01-25 23:09:36 +0000908//===----------------------------------------------------------------------===//
909// AttributeFuncs Function Defintions
910//===----------------------------------------------------------------------===//
911
Bill Wendlinge7436542013-01-30 23:07:40 +0000912AttributeSet AttributeFuncs::typeIncompatible(Type *Ty, uint64_t Index) {
Bill Wendling8e47daf2013-01-25 23:09:36 +0000913 AttrBuilder Incompatible;
914
915 if (!Ty->isIntegerTy())
916 // Attribute that only apply to integers.
917 Incompatible.addAttribute(Attribute::SExt)
918 .addAttribute(Attribute::ZExt);
919
920 if (!Ty->isPointerTy())
921 // Attribute that only apply to pointers.
922 Incompatible.addAttribute(Attribute::ByVal)
923 .addAttribute(Attribute::Nest)
924 .addAttribute(Attribute::NoAlias)
925 .addAttribute(Attribute::NoCapture)
926 .addAttribute(Attribute::StructRet);
927
Bill Wendlinge7436542013-01-30 23:07:40 +0000928 return AttributeSet::get(Ty->getContext(), Index, Incompatible);
Bill Wendling8e47daf2013-01-25 23:09:36 +0000929}
930
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000931/// \brief This returns an integer containing an encoding of all the LLVM
932/// attributes found in the given attribute bitset. Any change to this encoding
933/// is a breaking change to bitcode compatibility.
Bill Wendling8232ece2013-01-29 01:43:29 +0000934/// N.B. This should be used only by the bitcode reader!
Bill Wendling8e47daf2013-01-25 23:09:36 +0000935uint64_t AttributeFuncs::encodeLLVMAttributesForBitcode(AttributeSet Attrs,
936 unsigned Index) {
937 // FIXME: It doesn't make sense to store the alignment information as an
938 // expanded out value, we should store it as a log2 value. However, we can't
939 // just change that here without breaking bitcode compatibility. If this ever
940 // becomes a problem in practice, we should introduce new tag numbers in the
941 // bitcode file and have those tags use a more efficiently encoded alignment
942 // field.
943
944 // Store the alignment in the bitcode as a 16-bit raw value instead of a 5-bit
945 // log2 encoded value. Shift the bits above the alignment up by 11 bits.
946 uint64_t EncodedAttrs = Attrs.Raw(Index) & 0xffff;
947 if (Attrs.hasAttribute(Index, Attribute::Alignment))
948 EncodedAttrs |= Attrs.getParamAlignment(Index) << 16;
949 EncodedAttrs |= (Attrs.Raw(Index) & (0xffffULL << 21)) << 11;
950 return EncodedAttrs;
951}
952
Bill Wendling8232ece2013-01-29 01:43:29 +0000953/// \brief This fills an AttrBuilder object with the LLVM attributes that have
954/// been decoded from the given integer. This function must stay in sync with
955/// 'encodeLLVMAttributesForBitcode'.
956/// N.B. This should be used only by the bitcode reader!
957void AttributeFuncs::decodeLLVMAttributesForBitcode(LLVMContext &C,
958 AttrBuilder &B,
959 uint64_t EncodedAttrs) {
Bill Wendling8e47daf2013-01-25 23:09:36 +0000960 // The alignment is stored as a 16-bit raw value from bits 31--16. We shift
961 // the bits above 31 down by 11 bits.
962 unsigned Alignment = (EncodedAttrs & (0xffffULL << 16)) >> 16;
963 assert((!Alignment || isPowerOf2_32(Alignment)) &&
964 "Alignment must be a power of two.");
965
Bill Wendling8e47daf2013-01-25 23:09:36 +0000966 if (Alignment)
967 B.addAlignmentAttr(Alignment);
Bill Wendling606c8e32013-01-29 03:20:31 +0000968 B.addRawValue(((EncodedAttrs & (0xffffULL << 32)) >> 11) |
969 (EncodedAttrs & 0xffff));
Bill Wendling8e47daf2013-01-25 23:09:36 +0000970}