blob: 98c12b5d85590d13d7a512bb57f449153f62fa9a [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
NAKAMURA Takumieddab152013-01-31 03:47:28 +000085bool Attribute::hasAttributes() const {
86 return pImpl && pImpl->hasAttributes();
87}
88
Bill Wendling6dc37812013-01-29 20:45:34 +000089Constant *Attribute::getAttributeKind() const {
90 return pImpl ? pImpl->getAttributeKind() : 0;
91}
92
93ArrayRef<Constant*> Attribute::getAttributeValues() const {
94 return pImpl ? pImpl->getAttributeValues() : ArrayRef<Constant*>();
95}
96
Bill Wendlinge66f3d32012-10-05 06:44:41 +000097/// This returns the alignment field of an attribute as a byte alignment value.
Bill Wendling034b94b2012-12-19 07:18:57 +000098unsigned Attribute::getAlignment() const {
99 if (!hasAttribute(Attribute::Alignment))
Bill Wendling9ef99c92012-10-09 20:56:48 +0000100 return 0;
Bill Wendlinga8ab5fc2013-01-23 23:00:05 +0000101 return pImpl->getAlignment();
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000102}
103
104/// This returns the stack alignment field of an attribute as a byte alignment
105/// value.
Bill Wendling034b94b2012-12-19 07:18:57 +0000106unsigned Attribute::getStackAlignment() const {
107 if (!hasAttribute(Attribute::StackAlignment))
Bill Wendling9ef99c92012-10-09 20:56:48 +0000108 return 0;
Bill Wendlinga8ab5fc2013-01-23 23:00:05 +0000109 return pImpl->getStackAlignment();
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000110}
111
Bill Wendling034b94b2012-12-19 07:18:57 +0000112std::string Attribute::getAsString() const {
Bill Wendling034b94b2012-12-19 07:18:57 +0000113 if (hasAttribute(Attribute::ZExt))
Bill Wendling606c8e32013-01-29 03:20:31 +0000114 return "zeroext";
Bill Wendling034b94b2012-12-19 07:18:57 +0000115 if (hasAttribute(Attribute::SExt))
Bill Wendling606c8e32013-01-29 03:20:31 +0000116 return "signext";
Bill Wendling034b94b2012-12-19 07:18:57 +0000117 if (hasAttribute(Attribute::NoReturn))
Bill Wendling606c8e32013-01-29 03:20:31 +0000118 return "noreturn";
Bill Wendling034b94b2012-12-19 07:18:57 +0000119 if (hasAttribute(Attribute::NoUnwind))
Bill Wendling606c8e32013-01-29 03:20:31 +0000120 return "nounwind";
Bill Wendling034b94b2012-12-19 07:18:57 +0000121 if (hasAttribute(Attribute::UWTable))
Bill Wendling606c8e32013-01-29 03:20:31 +0000122 return "uwtable";
Bill Wendling034b94b2012-12-19 07:18:57 +0000123 if (hasAttribute(Attribute::ReturnsTwice))
Bill Wendling606c8e32013-01-29 03:20:31 +0000124 return "returns_twice";
Bill Wendling034b94b2012-12-19 07:18:57 +0000125 if (hasAttribute(Attribute::InReg))
Bill Wendling606c8e32013-01-29 03:20:31 +0000126 return "inreg";
Bill Wendling034b94b2012-12-19 07:18:57 +0000127 if (hasAttribute(Attribute::NoAlias))
Bill Wendling606c8e32013-01-29 03:20:31 +0000128 return "noalias";
Bill Wendling034b94b2012-12-19 07:18:57 +0000129 if (hasAttribute(Attribute::NoCapture))
Bill Wendling606c8e32013-01-29 03:20:31 +0000130 return "nocapture";
Bill Wendling034b94b2012-12-19 07:18:57 +0000131 if (hasAttribute(Attribute::StructRet))
Bill Wendling606c8e32013-01-29 03:20:31 +0000132 return "sret";
Bill Wendling034b94b2012-12-19 07:18:57 +0000133 if (hasAttribute(Attribute::ByVal))
Bill Wendling606c8e32013-01-29 03:20:31 +0000134 return "byval";
Bill Wendling034b94b2012-12-19 07:18:57 +0000135 if (hasAttribute(Attribute::Nest))
Bill Wendling606c8e32013-01-29 03:20:31 +0000136 return "nest";
Bill Wendling034b94b2012-12-19 07:18:57 +0000137 if (hasAttribute(Attribute::ReadNone))
Bill Wendling606c8e32013-01-29 03:20:31 +0000138 return "readnone";
Bill Wendling034b94b2012-12-19 07:18:57 +0000139 if (hasAttribute(Attribute::ReadOnly))
Bill Wendling606c8e32013-01-29 03:20:31 +0000140 return "readonly";
Bill Wendling034b94b2012-12-19 07:18:57 +0000141 if (hasAttribute(Attribute::OptimizeForSize))
Bill Wendling606c8e32013-01-29 03:20:31 +0000142 return "optsize";
Bill Wendling034b94b2012-12-19 07:18:57 +0000143 if (hasAttribute(Attribute::NoInline))
Bill Wendling606c8e32013-01-29 03:20:31 +0000144 return "noinline";
Bill Wendling034b94b2012-12-19 07:18:57 +0000145 if (hasAttribute(Attribute::InlineHint))
Bill Wendling606c8e32013-01-29 03:20:31 +0000146 return "inlinehint";
Bill Wendling034b94b2012-12-19 07:18:57 +0000147 if (hasAttribute(Attribute::AlwaysInline))
Bill Wendling606c8e32013-01-29 03:20:31 +0000148 return "alwaysinline";
Bill Wendling034b94b2012-12-19 07:18:57 +0000149 if (hasAttribute(Attribute::StackProtect))
Bill Wendling606c8e32013-01-29 03:20:31 +0000150 return "ssp";
Bill Wendling034b94b2012-12-19 07:18:57 +0000151 if (hasAttribute(Attribute::StackProtectReq))
Bill Wendling606c8e32013-01-29 03:20:31 +0000152 return "sspreq";
Bill Wendling114baee2013-01-23 06:41:41 +0000153 if (hasAttribute(Attribute::StackProtectStrong))
Bill Wendling606c8e32013-01-29 03:20:31 +0000154 return "sspstrong";
Bill Wendling034b94b2012-12-19 07:18:57 +0000155 if (hasAttribute(Attribute::NoRedZone))
Bill Wendling606c8e32013-01-29 03:20:31 +0000156 return "noredzone";
Bill Wendling034b94b2012-12-19 07:18:57 +0000157 if (hasAttribute(Attribute::NoImplicitFloat))
Bill Wendling606c8e32013-01-29 03:20:31 +0000158 return "noimplicitfloat";
Bill Wendling034b94b2012-12-19 07:18:57 +0000159 if (hasAttribute(Attribute::Naked))
Bill Wendling606c8e32013-01-29 03:20:31 +0000160 return "naked";
Bill Wendling034b94b2012-12-19 07:18:57 +0000161 if (hasAttribute(Attribute::NonLazyBind))
Bill Wendling606c8e32013-01-29 03:20:31 +0000162 return "nonlazybind";
Bill Wendling034b94b2012-12-19 07:18:57 +0000163 if (hasAttribute(Attribute::AddressSafety))
Bill Wendling606c8e32013-01-29 03:20:31 +0000164 return "address_safety";
Bill Wendling034b94b2012-12-19 07:18:57 +0000165 if (hasAttribute(Attribute::MinSize))
Bill Wendling606c8e32013-01-29 03:20:31 +0000166 return "minsize";
Bill Wendling034b94b2012-12-19 07:18:57 +0000167 if (hasAttribute(Attribute::StackAlignment)) {
Bill Wendling606c8e32013-01-29 03:20:31 +0000168 std::string Result;
Charles Davis1e063d12010-02-12 00:31:15 +0000169 Result += "alignstack(";
Bill Wendlingef99fe82012-09-21 15:26:31 +0000170 Result += utostr(getStackAlignment());
Bill Wendling606c8e32013-01-29 03:20:31 +0000171 Result += ")";
172 return Result;
Charles Davis1e063d12010-02-12 00:31:15 +0000173 }
Bill Wendling034b94b2012-12-19 07:18:57 +0000174 if (hasAttribute(Attribute::Alignment)) {
Bill Wendling606c8e32013-01-29 03:20:31 +0000175 std::string Result;
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000176 Result += "align ";
Bill Wendlingef99fe82012-09-21 15:26:31 +0000177 Result += utostr(getAlignment());
Bill Wendling606c8e32013-01-29 03:20:31 +0000178 Result += "";
179 return Result;
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000180 }
James Molloy67ae1352012-12-20 16:04:27 +0000181 if (hasAttribute(Attribute::NoDuplicate))
Bill Wendling606c8e32013-01-29 03:20:31 +0000182 return "noduplicate";
183
184 llvm_unreachable("Unknown attribute");
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000185}
186
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000187bool Attribute::operator==(AttrKind K) const {
NAKAMURA Takumieddab152013-01-31 03:47:28 +0000188 return pImpl && *pImpl == K;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000189}
190bool Attribute::operator!=(AttrKind K) const {
191 return !(*this == K);
192}
193
194bool Attribute::operator<(Attribute A) const {
195 if (!pImpl && !A.pImpl) return false;
196 if (!pImpl) return true;
197 if (!A.pImpl) return false;
198 return *pImpl < *A.pImpl;
199}
200
201uint64_t Attribute::Raw() const {
202 return pImpl ? pImpl->Raw() : 0;
203}
204
205//===----------------------------------------------------------------------===//
206// AttributeImpl Definition
207//===----------------------------------------------------------------------===//
208
Bill Wendling9f175f82013-01-29 20:37:10 +0000209AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind kind)
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000210 : Context(C) {
Bill Wendling9f175f82013-01-29 20:37:10 +0000211 Kind = ConstantInt::get(Type::getInt64Ty(C), kind);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000212}
Bill Wendling9f175f82013-01-29 20:37:10 +0000213AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind kind,
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000214 ArrayRef<Constant*> values)
215 : Context(C) {
Bill Wendling9f175f82013-01-29 20:37:10 +0000216 Kind = ConstantInt::get(Type::getInt64Ty(C), kind);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000217 Vals.reserve(values.size());
218 Vals.append(values.begin(), values.end());
219}
Bill Wendling9f175f82013-01-29 20:37:10 +0000220AttributeImpl::AttributeImpl(LLVMContext &C, StringRef kind)
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000221 : Context(C) {
Bill Wendling9f175f82013-01-29 20:37:10 +0000222 Kind = ConstantDataArray::getString(C, kind);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000223}
224
225bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
226 return (Raw() & getAttrMask(A)) != 0;
227}
228
NAKAMURA Takumieddab152013-01-31 03:47:28 +0000229bool AttributeImpl::hasAttributes() const {
230 return Raw() != 0;
231}
232
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000233uint64_t AttributeImpl::getAlignment() const {
234 uint64_t Mask = Raw() & getAttrMask(Attribute::Alignment);
235 return 1ULL << ((Mask >> 16) - 1);
236}
237
238uint64_t AttributeImpl::getStackAlignment() const {
239 uint64_t Mask = Raw() & getAttrMask(Attribute::StackAlignment);
240 return 1ULL << ((Mask >> 26) - 1);
241}
242
Bill Wendling9f175f82013-01-29 20:37:10 +0000243bool AttributeImpl::operator==(Attribute::AttrKind kind) const {
244 if (ConstantInt *CI = dyn_cast<ConstantInt>(Kind))
245 return CI->getZExtValue() == kind;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000246 return false;
247}
Bill Wendling9f175f82013-01-29 20:37:10 +0000248bool AttributeImpl::operator!=(Attribute::AttrKind kind) const {
249 return !(*this == kind);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000250}
251
Bill Wendling9f175f82013-01-29 20:37:10 +0000252bool AttributeImpl::operator==(StringRef kind) const {
253 if (ConstantDataArray *CDA = dyn_cast<ConstantDataArray>(Kind))
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000254 if (CDA->isString())
Bill Wendling9f175f82013-01-29 20:37:10 +0000255 return CDA->getAsString() == kind;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000256 return false;
257}
258
Bill Wendling9f175f82013-01-29 20:37:10 +0000259bool AttributeImpl::operator!=(StringRef kind) const {
260 return !(*this == kind);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000261}
262
263bool AttributeImpl::operator<(const AttributeImpl &AI) const {
Bill Wendling9f175f82013-01-29 20:37:10 +0000264 if (!Kind && !AI.Kind) return false;
265 if (!Kind && AI.Kind) return true;
266 if (Kind && !AI.Kind) return false;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000267
Bill Wendling9f175f82013-01-29 20:37:10 +0000268 ConstantInt *ThisCI = dyn_cast<ConstantInt>(Kind);
269 ConstantInt *ThatCI = dyn_cast<ConstantInt>(AI.Kind);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000270
Bill Wendling9f175f82013-01-29 20:37:10 +0000271 ConstantDataArray *ThisCDA = dyn_cast<ConstantDataArray>(Kind);
272 ConstantDataArray *ThatCDA = dyn_cast<ConstantDataArray>(AI.Kind);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000273
274 if (ThisCI && ThatCI)
275 return ThisCI->getZExtValue() < ThatCI->getZExtValue();
276
277 if (ThisCI && ThatCDA)
278 return true;
279
280 if (ThisCDA && ThatCI)
281 return false;
282
283 return ThisCDA->getAsString() < ThatCDA->getAsString();
284}
285
286uint64_t AttributeImpl::Raw() const {
287 // FIXME: Remove this.
Bill Wendling9f175f82013-01-29 20:37:10 +0000288 return cast<ConstantInt>(Kind)->getZExtValue();
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000289}
290
291uint64_t AttributeImpl::getAttrMask(Attribute::AttrKind Val) {
292 // FIXME: Remove this.
293 switch (Val) {
294 case Attribute::EndAttrKinds:
295 case Attribute::AttrKindEmptyKey:
296 case Attribute::AttrKindTombstoneKey:
297 llvm_unreachable("Synthetic enumerators which should never get here");
298
299 case Attribute::None: return 0;
300 case Attribute::ZExt: return 1 << 0;
301 case Attribute::SExt: return 1 << 1;
302 case Attribute::NoReturn: return 1 << 2;
303 case Attribute::InReg: return 1 << 3;
304 case Attribute::StructRet: return 1 << 4;
305 case Attribute::NoUnwind: return 1 << 5;
306 case Attribute::NoAlias: return 1 << 6;
307 case Attribute::ByVal: return 1 << 7;
308 case Attribute::Nest: return 1 << 8;
309 case Attribute::ReadNone: return 1 << 9;
310 case Attribute::ReadOnly: return 1 << 10;
311 case Attribute::NoInline: return 1 << 11;
312 case Attribute::AlwaysInline: return 1 << 12;
313 case Attribute::OptimizeForSize: return 1 << 13;
314 case Attribute::StackProtect: return 1 << 14;
315 case Attribute::StackProtectReq: return 1 << 15;
316 case Attribute::Alignment: return 31 << 16;
317 case Attribute::NoCapture: return 1 << 21;
318 case Attribute::NoRedZone: return 1 << 22;
319 case Attribute::NoImplicitFloat: return 1 << 23;
320 case Attribute::Naked: return 1 << 24;
321 case Attribute::InlineHint: return 1 << 25;
322 case Attribute::StackAlignment: return 7 << 26;
323 case Attribute::ReturnsTwice: return 1 << 29;
324 case Attribute::UWTable: return 1 << 30;
325 case Attribute::NonLazyBind: return 1U << 31;
326 case Attribute::AddressSafety: return 1ULL << 32;
327 case Attribute::MinSize: return 1ULL << 33;
328 case Attribute::NoDuplicate: return 1ULL << 34;
329 case Attribute::StackProtectStrong: return 1ULL << 35;
330 }
331 llvm_unreachable("Unsupported attribute type");
332}
333
334//===----------------------------------------------------------------------===//
335// AttributeSetNode Definition
336//===----------------------------------------------------------------------===//
337
338AttributeSetNode *AttributeSetNode::get(LLVMContext &C,
339 ArrayRef<Attribute> Attrs) {
340 if (Attrs.empty())
341 return 0;
342
343 // Otherwise, build a key to look up the existing attributes.
344 LLVMContextImpl *pImpl = C.pImpl;
345 FoldingSetNodeID ID;
346
347 SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end());
348 std::sort(SortedAttrs.begin(), SortedAttrs.end());
349
350 for (SmallVectorImpl<Attribute>::iterator I = SortedAttrs.begin(),
351 E = SortedAttrs.end(); I != E; ++I)
352 I->Profile(ID);
353
354 void *InsertPoint;
355 AttributeSetNode *PA =
356 pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint);
357
358 // If we didn't find any existing attributes of the same shape then create a
359 // new one and insert it.
360 if (!PA) {
361 PA = new AttributeSetNode(SortedAttrs);
362 pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint);
363 }
364
365 // Return the AttributesListNode that we found or created.
366 return PA;
367}
368
Bill Wendling606c8e32013-01-29 03:20:31 +0000369bool AttributeSetNode::hasAttribute(Attribute::AttrKind Kind) 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(Kind))
Bill Wendling606c8e32013-01-29 03:20:31 +0000373 return true;
374 return false;
375}
376
377unsigned AttributeSetNode::getAlignment() 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::Alignment))
Bill Wendling606c8e32013-01-29 03:20:31 +0000381 return I->getAlignment();
382 return 0;
383}
384
385unsigned AttributeSetNode::getStackAlignment() const {
386 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
387 E = AttrList.end(); I != E; ++I)
NAKAMURA Takumieddab152013-01-31 03:47:28 +0000388 if (I->hasAttribute(Attribute::StackAlignment))
Bill Wendling606c8e32013-01-29 03:20:31 +0000389 return I->getStackAlignment();
390 return 0;
391}
392
393std::string AttributeSetNode::getAsString() const {
394 std::string Str = "";
395 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
396 E = AttrList.end(); I != E; ++I) {
397 if (I != AttrList.begin()) Str += " ";
398 Str += I->getAsString();
399 }
400 return Str;
401}
402
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000403//===----------------------------------------------------------------------===//
404// AttributeSetImpl Definition
405//===----------------------------------------------------------------------===//
406
407uint64_t AttributeSetImpl::Raw(uint64_t Index) const {
408 for (unsigned I = 0, E = getNumAttributes(); I != E; ++I) {
409 if (getSlotIndex(I) != Index) continue;
410 const AttributeSetNode *ASN = AttrNodes[I].second;
411 AttrBuilder B;
412
413 for (AttributeSetNode::const_iterator II = ASN->begin(),
414 IE = ASN->end(); II != IE; ++II)
415 B.addAttributes(*II);
416 return B.Raw();
417 }
418
419 return 0;
420}
421
422//===----------------------------------------------------------------------===//
423// AttributeSet Construction and Mutation Methods
424//===----------------------------------------------------------------------===//
425
Bill Wendling8232ece2013-01-29 01:43:29 +0000426AttributeSet
427AttributeSet::getImpl(LLVMContext &C,
428 ArrayRef<std::pair<unsigned, AttributeSetNode*> > Attrs) {
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000429 LLVMContextImpl *pImpl = C.pImpl;
430 FoldingSetNodeID ID;
431 AttributeSetImpl::Profile(ID, Attrs);
432
433 void *InsertPoint;
434 AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
435
436 // If we didn't find any existing attributes of the same shape then
437 // create a new one and insert it.
438 if (!PA) {
439 PA = new AttributeSetImpl(C, Attrs);
440 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
441 }
442
443 // Return the AttributesList that we found or created.
444 return AttributeSet(PA);
445}
446
447AttributeSet AttributeSet::get(LLVMContext &C,
448 ArrayRef<std::pair<unsigned, Attribute> > Attrs){
449 // If there are no attributes then return a null AttributesList pointer.
450 if (Attrs.empty())
451 return AttributeSet();
452
453#ifndef NDEBUG
454 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
455 assert((!i || Attrs[i-1].first <= Attrs[i].first) &&
456 "Misordered Attributes list!");
NAKAMURA Takumieddab152013-01-31 03:47:28 +0000457 assert(Attrs[i].second.hasAttributes() &&
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000458 "Pointless attribute!");
459 }
460#endif
461
462 // Create a vector if (unsigned, AttributeSetNode*) pairs from the attributes
463 // list.
464 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrPairVec;
465 for (ArrayRef<std::pair<unsigned, Attribute> >::iterator I = Attrs.begin(),
466 E = Attrs.end(); I != E; ) {
467 unsigned Index = I->first;
468 SmallVector<Attribute, 4> AttrVec;
NAKAMURA Takumi3ba51ce2013-01-29 15:18:16 +0000469 while (I != E && I->first == Index) {
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000470 AttrVec.push_back(I->second);
471 ++I;
472 }
473
474 AttrPairVec.push_back(std::make_pair(Index,
475 AttributeSetNode::get(C, AttrVec)));
476 }
477
478 return getImpl(C, AttrPairVec);
479}
480
481AttributeSet AttributeSet::get(LLVMContext &C,
482 ArrayRef<std::pair<unsigned,
483 AttributeSetNode*> > Attrs) {
484 // If there are no attributes then return a null AttributesList pointer.
485 if (Attrs.empty())
486 return AttributeSet();
487
488 return getImpl(C, Attrs);
489}
490
491AttributeSet AttributeSet::get(LLVMContext &C, unsigned Idx, AttrBuilder &B) {
492 if (!B.hasAttributes())
493 return AttributeSet();
Bill Wendling8fbc0c22013-01-29 01:02:03 +0000494
495 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
496 for (AttrBuilder::iterator I = B.begin(), E = B.end(); I != E; ++I) {
497 Attribute::AttrKind Kind = *I;
498 if (Kind == Attribute::Alignment)
499 Attrs.push_back(std::make_pair(Idx, Attribute::
500 getWithAlignment(C, B.getAlignment())));
501 else if (Kind == Attribute::StackAlignment)
502 Attrs.push_back(std::make_pair(Idx, Attribute::
503 getWithStackAlignment(C, B.getStackAlignment())));
504 else
505 Attrs.push_back(std::make_pair(Idx, Attribute::get(C, Kind)));
506 }
507
508 return get(C, Attrs);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000509}
510
511AttributeSet AttributeSet::get(LLVMContext &C, unsigned Idx,
512 ArrayRef<Attribute::AttrKind> Kind) {
513 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
514 for (ArrayRef<Attribute::AttrKind>::iterator I = Kind.begin(),
515 E = Kind.end(); I != E; ++I)
516 Attrs.push_back(std::make_pair(Idx, Attribute::get(C, *I)));
517 return get(C, Attrs);
518}
519
520AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<AttributeSet> Attrs) {
521 if (Attrs.empty()) return AttributeSet();
522
523 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrNodeVec;
524 for (unsigned I = 0, E = Attrs.size(); I != E; ++I) {
525 AttributeSet AS = Attrs[I];
526 if (!AS.pImpl) continue;
527 AttrNodeVec.append(AS.pImpl->AttrNodes.begin(), AS.pImpl->AttrNodes.end());
528 }
529
530 return getImpl(C, AttrNodeVec);
531}
532
533AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Idx,
534 Attribute::AttrKind Attr) const {
535 return addAttributes(C, Idx, AttributeSet::get(C, Idx, Attr));
536}
537
538AttributeSet AttributeSet::addAttributes(LLVMContext &C, unsigned Idx,
539 AttributeSet Attrs) const {
540 if (!pImpl) return Attrs;
541 if (!Attrs.pImpl) return *this;
542
543#ifndef NDEBUG
544 // FIXME it is not obvious how this should work for alignment. For now, say
545 // we can't change a known alignment.
546 unsigned OldAlign = getParamAlignment(Idx);
547 unsigned NewAlign = Attrs.getParamAlignment(Idx);
548 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
549 "Attempt to change alignment!");
550#endif
551
552 // Add the attribute slots before the one we're trying to add.
553 SmallVector<AttributeSet, 4> AttrSet;
554 uint64_t NumAttrs = pImpl->getNumAttributes();
555 AttributeSet AS;
556 uint64_t LastIndex = 0;
557 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
558 if (getSlotIndex(I) >= Idx) {
559 if (getSlotIndex(I) == Idx) AS = getSlotAttributes(LastIndex++);
560 break;
561 }
562 LastIndex = I + 1;
563 AttrSet.push_back(getSlotAttributes(I));
564 }
565
566 // Now add the attribute into the correct slot. There may already be an
567 // AttributeSet there.
568 AttrBuilder B(AS, Idx);
569
570 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
571 if (Attrs.getSlotIndex(I) == Idx) {
572 for (AttributeSetImpl::const_iterator II = Attrs.pImpl->begin(I),
573 IE = Attrs.pImpl->end(I); II != IE; ++II)
574 B.addAttributes(*II);
575 break;
576 }
577
578 AttrSet.push_back(AttributeSet::get(C, Idx, B));
579
580 // Add the remaining attribute slots.
581 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
582 AttrSet.push_back(getSlotAttributes(I));
583
584 return get(C, AttrSet);
585}
586
587AttributeSet AttributeSet::removeAttribute(LLVMContext &C, unsigned Idx,
588 Attribute::AttrKind Attr) const {
589 return removeAttributes(C, Idx, AttributeSet::get(C, Idx, Attr));
590}
591
592AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Idx,
593 AttributeSet Attrs) const {
594 if (!pImpl) return AttributeSet();
595 if (!Attrs.pImpl) return *this;
596
597#ifndef NDEBUG
598 // FIXME it is not obvious how this should work for alignment.
599 // For now, say we can't pass in alignment, which no current use does.
600 assert(!Attrs.hasAttribute(Idx, Attribute::Alignment) &&
601 "Attempt to change alignment!");
602#endif
603
604 // Add the attribute slots before the one we're trying to add.
605 SmallVector<AttributeSet, 4> AttrSet;
606 uint64_t NumAttrs = pImpl->getNumAttributes();
607 AttributeSet AS;
608 uint64_t LastIndex = 0;
609 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
610 if (getSlotIndex(I) >= Idx) {
611 if (getSlotIndex(I) == Idx) AS = getSlotAttributes(LastIndex++);
612 break;
613 }
614 LastIndex = I + 1;
615 AttrSet.push_back(getSlotAttributes(I));
616 }
617
Bill Wendlinge7436542013-01-30 23:07:40 +0000618 // Now remove the attribute from the correct slot. There may already be an
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000619 // AttributeSet there.
620 AttrBuilder B(AS, Idx);
621
622 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
623 if (Attrs.getSlotIndex(I) == Idx) {
Bill Wendlinge7436542013-01-30 23:07:40 +0000624 B.removeAttributes(Attrs.pImpl->getSlotAttributes(I), Idx);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000625 break;
626 }
627
628 AttrSet.push_back(AttributeSet::get(C, Idx, B));
629
630 // Add the remaining attribute slots.
631 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
632 AttrSet.push_back(getSlotAttributes(I));
633
634 return get(C, AttrSet);
635}
636
637//===----------------------------------------------------------------------===//
638// AttributeSet Accessor Methods
639//===----------------------------------------------------------------------===//
640
641AttributeSet AttributeSet::getParamAttributes(unsigned Idx) const {
642 return pImpl && hasAttributes(Idx) ?
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(Idx, getAttributes(Idx)))) :
646 AttributeSet();
647}
648
649AttributeSet AttributeSet::getRetAttributes() const {
650 return pImpl && hasAttributes(ReturnIndex) ?
651 AttributeSet::get(pImpl->getContext(),
Bill Wendling606c8e32013-01-29 03:20:31 +0000652 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000653 std::make_pair(ReturnIndex,
654 getAttributes(ReturnIndex)))) :
655 AttributeSet();
656}
657
658AttributeSet AttributeSet::getFnAttributes() const {
659 return pImpl && hasAttributes(FunctionIndex) ?
660 AttributeSet::get(pImpl->getContext(),
Bill Wendling606c8e32013-01-29 03:20:31 +0000661 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000662 std::make_pair(FunctionIndex,
663 getAttributes(FunctionIndex)))) :
664 AttributeSet();
665}
666
667bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{
Bill Wendling606c8e32013-01-29 03:20:31 +0000668 AttributeSetNode *ASN = getAttributes(Index);
669 return ASN ? ASN->hasAttribute(Kind) : false;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000670}
671
672bool AttributeSet::hasAttributes(unsigned Index) const {
Bill Wendling606c8e32013-01-29 03:20:31 +0000673 AttributeSetNode *ASN = getAttributes(Index);
674 return ASN ? ASN->hasAttributes() : false;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000675}
676
677/// \brief Return true if the specified attribute is set for at least one
678/// parameter or for the return value.
679bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr) const {
680 if (pImpl == 0) return false;
681
682 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
683 for (AttributeSetImpl::const_iterator II = pImpl->begin(I),
684 IE = pImpl->end(I); II != IE; ++II)
NAKAMURA Takumieddab152013-01-31 03:47:28 +0000685 if (II->hasAttribute(Attr))
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000686 return true;
687
688 return false;
689}
690
Bill Wendling606c8e32013-01-29 03:20:31 +0000691unsigned AttributeSet::getParamAlignment(unsigned Index) const {
692 AttributeSetNode *ASN = getAttributes(Index);
693 return ASN ? ASN->getAlignment() : 0;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000694}
695
696unsigned AttributeSet::getStackAlignment(unsigned Index) const {
Bill Wendling606c8e32013-01-29 03:20:31 +0000697 AttributeSetNode *ASN = getAttributes(Index);
698 return ASN ? ASN->getStackAlignment() : 0;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000699}
700
701std::string AttributeSet::getAsString(unsigned Index) const {
Bill Wendling606c8e32013-01-29 03:20:31 +0000702 AttributeSetNode *ASN = getAttributes(Index);
703 return ASN ? ASN->getAsString() : std::string("");
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000704}
705
706/// \brief The attributes for the specified index are returned.
Bill Wendling606c8e32013-01-29 03:20:31 +0000707AttributeSetNode *AttributeSet::getAttributes(unsigned Idx) const {
708 if (!pImpl) return 0;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000709
Bill Wendling606c8e32013-01-29 03:20:31 +0000710 // Loop through to find the attribute node we want.
711 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
712 if (pImpl->getSlotIndex(I) == Idx)
713 return pImpl->getSlotNode(I);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000714
Bill Wendling606c8e32013-01-29 03:20:31 +0000715 return 0;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000716}
717
718//===----------------------------------------------------------------------===//
719// AttributeSet Introspection Methods
720//===----------------------------------------------------------------------===//
721
722/// \brief Return the number of slots used in this attribute list. This is the
723/// number of arguments that have an attribute set on them (including the
724/// function itself).
725unsigned AttributeSet::getNumSlots() const {
726 return pImpl ? pImpl->getNumAttributes() : 0;
727}
728
729uint64_t AttributeSet::getSlotIndex(unsigned Slot) const {
730 assert(pImpl && Slot < pImpl->getNumAttributes() &&
731 "Slot # out of range!");
732 return pImpl->getSlotIndex(Slot);
733}
734
735AttributeSet AttributeSet::getSlotAttributes(unsigned Slot) const {
736 assert(pImpl && Slot < pImpl->getNumAttributes() &&
737 "Slot # out of range!");
738 return pImpl->getSlotAttributes(Slot);
739}
740
741uint64_t AttributeSet::Raw(unsigned Index) const {
742 // FIXME: Remove this.
743 return pImpl ? pImpl->Raw(Index) : 0;
744}
745
746void AttributeSet::dump() const {
747 dbgs() << "PAL[\n";
748
749 for (unsigned i = 0, e = getNumSlots(); i < e; ++i) {
750 uint64_t Index = getSlotIndex(i);
751 dbgs() << " { ";
752 if (Index == ~0U)
753 dbgs() << "~0U";
754 else
755 dbgs() << Index;
756 dbgs() << " => " << getAsString(Index) << " }\n";
757 }
758
759 dbgs() << "]\n";
760}
761
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000762//===----------------------------------------------------------------------===//
Bill Wendling03198882013-01-04 23:27:34 +0000763// AttrBuilder Method Implementations
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000764//===----------------------------------------------------------------------===//
765
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000766AttrBuilder::AttrBuilder(AttributeSet AS, unsigned Idx)
767 : Alignment(0), StackAlignment(0) {
Bill Wendlingec258982013-01-27 21:23:46 +0000768 AttributeSetImpl *pImpl = AS.pImpl;
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000769 if (!pImpl) return;
770
Bill Wendling73bc4522013-01-28 00:21:34 +0000771 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I) {
772 if (pImpl->getSlotIndex(I) != Idx) continue;
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000773
Bill Wendling383da6b2013-01-30 21:22:59 +0000774 for (AttributeSetImpl::const_iterator II = pImpl->begin(I),
Bill Wendling73bc4522013-01-28 00:21:34 +0000775 IE = pImpl->end(I); II != IE; ++II)
Bill Wendling383da6b2013-01-30 21:22:59 +0000776 addAttributes(*II);
Bill Wendling73bc4522013-01-28 00:21:34 +0000777
778 break;
779 }
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000780}
781
Bill Wendling03198882013-01-04 23:27:34 +0000782void AttrBuilder::clear() {
783 Attrs.clear();
784 Alignment = StackAlignment = 0;
785}
786
787AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
788 Attrs.insert(Val);
Bill Wendling3a106e62012-10-09 19:01:18 +0000789 return *this;
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000790}
791
Bill Wendling03198882013-01-04 23:27:34 +0000792AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
793 Attrs.erase(Val);
794 if (Val == Attribute::Alignment)
795 Alignment = 0;
796 else if (Val == Attribute::StackAlignment)
797 StackAlignment = 0;
798
Bill Wendlinga19a5302012-10-14 04:10:01 +0000799 return *this;
800}
801
Bill Wendling49f60602013-01-28 05:23:28 +0000802AttrBuilder &AttrBuilder::addAttributes(Attribute Attr) {
803 uint64_t Mask = Attr.Raw();
804
805 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
806 I = Attribute::AttrKind(I + 1))
807 if ((Mask & AttributeImpl::getAttrMask(I)) != 0)
808 Attrs.insert(I);
809
810 if (Attr.getAlignment())
811 Alignment = Attr.getAlignment();
812 if (Attr.getStackAlignment())
813 StackAlignment = Attr.getStackAlignment();
814 return *this;
815}
816
Bill Wendlinge7436542013-01-30 23:07:40 +0000817AttrBuilder &AttrBuilder::removeAttributes(AttributeSet A, uint64_t Index) {
818 uint64_t Mask = A.Raw(Index);
Bill Wendling49f60602013-01-28 05:23:28 +0000819
820 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
821 I = Attribute::AttrKind(I + 1)) {
822 if (Mask & AttributeImpl::getAttrMask(I)) {
823 Attrs.erase(I);
824
825 if (I == Attribute::Alignment)
826 Alignment = 0;
827 else if (I == Attribute::StackAlignment)
828 StackAlignment = 0;
829 }
830 }
831
832 return *this;
833}
834
Bill Wendling702cc912012-10-15 20:35:56 +0000835AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
Bill Wendlingda3f9d82012-10-14 03:58:29 +0000836 if (Align == 0) return *this;
Bill Wendling03198882013-01-04 23:27:34 +0000837
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000838 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
839 assert(Align <= 0x40000000 && "Alignment too large.");
Bill Wendling03198882013-01-04 23:27:34 +0000840
841 Attrs.insert(Attribute::Alignment);
842 Alignment = Align;
Bill Wendlingda3f9d82012-10-14 03:58:29 +0000843 return *this;
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000844}
845
Bill Wendling03198882013-01-04 23:27:34 +0000846AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
847 // Default alignment, allow the target to define how to align it.
848 if (Align == 0) return *this;
849
850 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
851 assert(Align <= 0x100 && "Alignment too large.");
852
853 Attrs.insert(Attribute::StackAlignment);
854 StackAlignment = Align;
855 return *this;
856}
857
Bill Wendling22bd6412013-01-03 01:54:39 +0000858bool AttrBuilder::contains(Attribute::AttrKind A) const {
Bill Wendling03198882013-01-04 23:27:34 +0000859 return Attrs.count(A);
Bill Wendling7d2f2492012-10-10 07:36:45 +0000860}
861
Bill Wendling702cc912012-10-15 20:35:56 +0000862bool AttrBuilder::hasAttributes() const {
Bill Wendling03198882013-01-04 23:27:34 +0000863 return !Attrs.empty();
Bill Wendlingf385f4c2012-10-08 23:27:46 +0000864}
Bill Wendling60507d52013-01-04 20:54:35 +0000865
Bill Wendlinge7436542013-01-30 23:07:40 +0000866bool AttrBuilder::hasAttributes(AttributeSet A, uint64_t Index) const {
867 return Raw() & A.Raw(Index);
Bill Wendling8831c062012-10-09 00:01:21 +0000868}
Bill Wendling60507d52013-01-04 20:54:35 +0000869
Bill Wendling702cc912012-10-15 20:35:56 +0000870bool AttrBuilder::hasAlignmentAttr() const {
Bill Wendling03198882013-01-04 23:27:34 +0000871 return Alignment != 0;
Bill Wendlingf385f4c2012-10-08 23:27:46 +0000872}
873
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000874bool AttrBuilder::operator==(const AttrBuilder &B) {
875 SmallVector<Attribute::AttrKind, 8> This(Attrs.begin(), Attrs.end());
876 SmallVector<Attribute::AttrKind, 8> That(B.Attrs.begin(), B.Attrs.end());
877 return This == That;
878}
879
880AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) {
Bill Wendling8232ece2013-01-29 01:43:29 +0000881 if (!Val) return *this;
882
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000883 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
884 I = Attribute::AttrKind(I + 1)) {
885 if (uint64_t A = (Val & AttributeImpl::getAttrMask(I))) {
886 Attrs.insert(I);
887
888 if (I == Attribute::Alignment)
889 Alignment = 1ULL << ((A >> 16) - 1);
890 else if (I == Attribute::StackAlignment)
891 StackAlignment = 1ULL << ((A >> 26)-1);
892 }
893 }
894
895 return *this;
896}
897
Bill Wendling1db9b692013-01-09 23:36:50 +0000898uint64_t AttrBuilder::Raw() const {
Bill Wendling03198882013-01-04 23:27:34 +0000899 uint64_t Mask = 0;
900
901 for (DenseSet<Attribute::AttrKind>::const_iterator I = Attrs.begin(),
902 E = Attrs.end(); I != E; ++I) {
903 Attribute::AttrKind Kind = *I;
904
905 if (Kind == Attribute::Alignment)
906 Mask |= (Log2_32(Alignment) + 1) << 16;
907 else if (Kind == Attribute::StackAlignment)
908 Mask |= (Log2_32(StackAlignment) + 1) << 26;
909 else
910 Mask |= AttributeImpl::getAttrMask(Kind);
911 }
912
913 return Mask;
Bill Wendlingf385f4c2012-10-08 23:27:46 +0000914}
915
Bill Wendling8e47daf2013-01-25 23:09:36 +0000916//===----------------------------------------------------------------------===//
917// AttributeFuncs Function Defintions
918//===----------------------------------------------------------------------===//
919
Bill Wendlinge7436542013-01-30 23:07:40 +0000920AttributeSet AttributeFuncs::typeIncompatible(Type *Ty, uint64_t Index) {
Bill Wendling8e47daf2013-01-25 23:09:36 +0000921 AttrBuilder Incompatible;
922
923 if (!Ty->isIntegerTy())
924 // Attribute that only apply to integers.
925 Incompatible.addAttribute(Attribute::SExt)
926 .addAttribute(Attribute::ZExt);
927
928 if (!Ty->isPointerTy())
929 // Attribute that only apply to pointers.
930 Incompatible.addAttribute(Attribute::ByVal)
931 .addAttribute(Attribute::Nest)
932 .addAttribute(Attribute::NoAlias)
933 .addAttribute(Attribute::NoCapture)
934 .addAttribute(Attribute::StructRet);
935
Bill Wendlinge7436542013-01-30 23:07:40 +0000936 return AttributeSet::get(Ty->getContext(), Index, Incompatible);
Bill Wendling8e47daf2013-01-25 23:09:36 +0000937}
938
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000939/// \brief This returns an integer containing an encoding of all the LLVM
940/// attributes found in the given attribute bitset. Any change to this encoding
941/// is a breaking change to bitcode compatibility.
Bill Wendling8232ece2013-01-29 01:43:29 +0000942/// N.B. This should be used only by the bitcode reader!
Bill Wendling8e47daf2013-01-25 23:09:36 +0000943uint64_t AttributeFuncs::encodeLLVMAttributesForBitcode(AttributeSet Attrs,
944 unsigned Index) {
945 // FIXME: It doesn't make sense to store the alignment information as an
946 // expanded out value, we should store it as a log2 value. However, we can't
947 // just change that here without breaking bitcode compatibility. If this ever
948 // becomes a problem in practice, we should introduce new tag numbers in the
949 // bitcode file and have those tags use a more efficiently encoded alignment
950 // field.
951
952 // Store the alignment in the bitcode as a 16-bit raw value instead of a 5-bit
953 // log2 encoded value. Shift the bits above the alignment up by 11 bits.
954 uint64_t EncodedAttrs = Attrs.Raw(Index) & 0xffff;
955 if (Attrs.hasAttribute(Index, Attribute::Alignment))
956 EncodedAttrs |= Attrs.getParamAlignment(Index) << 16;
957 EncodedAttrs |= (Attrs.Raw(Index) & (0xffffULL << 21)) << 11;
958 return EncodedAttrs;
959}
960
Bill Wendling8232ece2013-01-29 01:43:29 +0000961/// \brief This fills an AttrBuilder object with the LLVM attributes that have
962/// been decoded from the given integer. This function must stay in sync with
963/// 'encodeLLVMAttributesForBitcode'.
964/// N.B. This should be used only by the bitcode reader!
965void AttributeFuncs::decodeLLVMAttributesForBitcode(LLVMContext &C,
966 AttrBuilder &B,
967 uint64_t EncodedAttrs) {
Bill Wendling8e47daf2013-01-25 23:09:36 +0000968 // The alignment is stored as a 16-bit raw value from bits 31--16. We shift
969 // the bits above 31 down by 11 bits.
970 unsigned Alignment = (EncodedAttrs & (0xffffULL << 16)) >> 16;
971 assert((!Alignment || isPowerOf2_32(Alignment)) &&
972 "Alignment must be a power of two.");
973
Bill Wendling8e47daf2013-01-25 23:09:36 +0000974 if (Alignment)
975 B.addAlignmentAttr(Alignment);
Bill Wendling606c8e32013-01-29 03:20:31 +0000976 B.addRawValue(((EncodedAttrs & (0xffffULL << 32)) >> 11) |
977 (EncodedAttrs & 0xffff));
Bill Wendling8e47daf2013-01-25 23:09:36 +0000978}