blob: 59e86f02a0e42422bdadf468255881785c2f85e9 [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 Wendlingc6077ef2013-01-31 00:53:21 +000033Attribute Attribute::get(LLVMContext &Context, AttrKind Kind,
34 Constant *Val) {
35 if (Kind == None) return Attribute();
Bill Wendling73dee182013-01-31 00:29:54 +000036
Bill Wendling8e635db2012-10-08 21:47:17 +000037 // Otherwise, build a key to look up the existing attributes.
38 LLVMContextImpl *pImpl = Context.pImpl;
39 FoldingSetNodeID ID;
Bill Wendlingc6077ef2013-01-31 00:53:21 +000040 ID.AddInteger(Kind);
41 ID.AddPointer(Val);
Bill Wendling8e635db2012-10-08 21:47:17 +000042
43 void *InsertPoint;
Bill Wendlingf6670722012-12-20 01:36:59 +000044 AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
Bill Wendling8e635db2012-10-08 21:47:17 +000045
46 if (!PA) {
47 // If we didn't find any existing attributes of the same shape then create a
48 // new one and insert it.
Bill Wendlingc6077ef2013-01-31 00:53:21 +000049 PA = (!Val) ?
50 new AttributeImpl(Context, Kind) :
51 new AttributeImpl(Context, Kind, Val);
Bill Wendling8e635db2012-10-08 21:47:17 +000052 pImpl->AttrsSet.InsertNode(PA, InsertPoint);
53 }
54
55 // Return the AttributesList that we found or created.
Bill Wendling034b94b2012-12-19 07:18:57 +000056 return Attribute(PA);
Bill Wendling8e635db2012-10-08 21:47:17 +000057}
58
Bill Wendlingc08a5ef2013-01-27 22:43:04 +000059Attribute Attribute::getWithAlignment(LLVMContext &Context, uint64_t Align) {
Bill Wendlingc6077ef2013-01-31 00:53:21 +000060 return get(Context, Attribute::Alignment,
61 ConstantInt::get(Type::getInt64Ty(Context), Align));
Bill Wendlingc08a5ef2013-01-27 22:43:04 +000062}
63
64Attribute Attribute::getWithStackAlignment(LLVMContext &Context,
65 uint64_t Align) {
Bill Wendlingc6077ef2013-01-31 00:53:21 +000066 return get(Context, Attribute::StackAlignment,
67 ConstantInt::get(Type::getInt64Ty(Context), Align));
Bill Wendlingc08a5ef2013-01-27 22:43:04 +000068}
69
Bill Wendling817abdd2013-01-29 00:48:16 +000070//===----------------------------------------------------------------------===//
71// Attribute Accessor Methods
72//===----------------------------------------------------------------------===//
73
Bill Wendling629fb822012-12-22 00:37:52 +000074bool Attribute::hasAttribute(AttrKind Val) const {
Bill Wendling27107f62012-12-20 21:28:43 +000075 return pImpl && pImpl->hasAttribute(Val);
Bill Wendlinge66f3d32012-10-05 06:44:41 +000076}
77
Bill Wendling034b94b2012-12-19 07:18:57 +000078bool Attribute::hasAttributes() const {
Bill Wendling27107f62012-12-20 21:28:43 +000079 return pImpl && pImpl->hasAttributes();
Bill Wendling05cc40d2012-10-15 05:40:12 +000080}
81
Bill Wendling6dc37812013-01-29 20:45:34 +000082Constant *Attribute::getAttributeKind() const {
83 return pImpl ? pImpl->getAttributeKind() : 0;
84}
85
86ArrayRef<Constant*> Attribute::getAttributeValues() const {
87 return pImpl ? pImpl->getAttributeValues() : ArrayRef<Constant*>();
88}
89
Bill Wendlinge66f3d32012-10-05 06:44:41 +000090/// This returns the alignment field of an attribute as a byte alignment value.
Bill Wendling034b94b2012-12-19 07:18:57 +000091unsigned Attribute::getAlignment() const {
92 if (!hasAttribute(Attribute::Alignment))
Bill Wendling9ef99c92012-10-09 20:56:48 +000093 return 0;
Bill Wendlinga8ab5fc2013-01-23 23:00:05 +000094 return pImpl->getAlignment();
Bill Wendlinge66f3d32012-10-05 06:44:41 +000095}
96
97/// This returns the stack alignment field of an attribute as a byte alignment
98/// value.
Bill Wendling034b94b2012-12-19 07:18:57 +000099unsigned Attribute::getStackAlignment() const {
100 if (!hasAttribute(Attribute::StackAlignment))
Bill Wendling9ef99c92012-10-09 20:56:48 +0000101 return 0;
Bill Wendlinga8ab5fc2013-01-23 23:00:05 +0000102 return pImpl->getStackAlignment();
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000103}
104
Bill Wendling034b94b2012-12-19 07:18:57 +0000105std::string Attribute::getAsString() const {
Bill Wendling034b94b2012-12-19 07:18:57 +0000106 if (hasAttribute(Attribute::ZExt))
Bill Wendling606c8e32013-01-29 03:20:31 +0000107 return "zeroext";
Bill Wendling034b94b2012-12-19 07:18:57 +0000108 if (hasAttribute(Attribute::SExt))
Bill Wendling606c8e32013-01-29 03:20:31 +0000109 return "signext";
Bill Wendling034b94b2012-12-19 07:18:57 +0000110 if (hasAttribute(Attribute::NoReturn))
Bill Wendling606c8e32013-01-29 03:20:31 +0000111 return "noreturn";
Bill Wendling034b94b2012-12-19 07:18:57 +0000112 if (hasAttribute(Attribute::NoUnwind))
Bill Wendling606c8e32013-01-29 03:20:31 +0000113 return "nounwind";
Bill Wendling034b94b2012-12-19 07:18:57 +0000114 if (hasAttribute(Attribute::UWTable))
Bill Wendling606c8e32013-01-29 03:20:31 +0000115 return "uwtable";
Bill Wendling034b94b2012-12-19 07:18:57 +0000116 if (hasAttribute(Attribute::ReturnsTwice))
Bill Wendling606c8e32013-01-29 03:20:31 +0000117 return "returns_twice";
Bill Wendling034b94b2012-12-19 07:18:57 +0000118 if (hasAttribute(Attribute::InReg))
Bill Wendling606c8e32013-01-29 03:20:31 +0000119 return "inreg";
Bill Wendling034b94b2012-12-19 07:18:57 +0000120 if (hasAttribute(Attribute::NoAlias))
Bill Wendling606c8e32013-01-29 03:20:31 +0000121 return "noalias";
Bill Wendling034b94b2012-12-19 07:18:57 +0000122 if (hasAttribute(Attribute::NoCapture))
Bill Wendling606c8e32013-01-29 03:20:31 +0000123 return "nocapture";
Bill Wendling034b94b2012-12-19 07:18:57 +0000124 if (hasAttribute(Attribute::StructRet))
Bill Wendling606c8e32013-01-29 03:20:31 +0000125 return "sret";
Bill Wendling034b94b2012-12-19 07:18:57 +0000126 if (hasAttribute(Attribute::ByVal))
Bill Wendling606c8e32013-01-29 03:20:31 +0000127 return "byval";
Bill Wendling034b94b2012-12-19 07:18:57 +0000128 if (hasAttribute(Attribute::Nest))
Bill Wendling606c8e32013-01-29 03:20:31 +0000129 return "nest";
Bill Wendling034b94b2012-12-19 07:18:57 +0000130 if (hasAttribute(Attribute::ReadNone))
Bill Wendling606c8e32013-01-29 03:20:31 +0000131 return "readnone";
Bill Wendling034b94b2012-12-19 07:18:57 +0000132 if (hasAttribute(Attribute::ReadOnly))
Bill Wendling606c8e32013-01-29 03:20:31 +0000133 return "readonly";
Bill Wendling034b94b2012-12-19 07:18:57 +0000134 if (hasAttribute(Attribute::OptimizeForSize))
Bill Wendling606c8e32013-01-29 03:20:31 +0000135 return "optsize";
Bill Wendling034b94b2012-12-19 07:18:57 +0000136 if (hasAttribute(Attribute::NoInline))
Bill Wendling606c8e32013-01-29 03:20:31 +0000137 return "noinline";
Bill Wendling034b94b2012-12-19 07:18:57 +0000138 if (hasAttribute(Attribute::InlineHint))
Bill Wendling606c8e32013-01-29 03:20:31 +0000139 return "inlinehint";
Bill Wendling034b94b2012-12-19 07:18:57 +0000140 if (hasAttribute(Attribute::AlwaysInline))
Bill Wendling606c8e32013-01-29 03:20:31 +0000141 return "alwaysinline";
Bill Wendling034b94b2012-12-19 07:18:57 +0000142 if (hasAttribute(Attribute::StackProtect))
Bill Wendling606c8e32013-01-29 03:20:31 +0000143 return "ssp";
Bill Wendling034b94b2012-12-19 07:18:57 +0000144 if (hasAttribute(Attribute::StackProtectReq))
Bill Wendling606c8e32013-01-29 03:20:31 +0000145 return "sspreq";
Bill Wendling114baee2013-01-23 06:41:41 +0000146 if (hasAttribute(Attribute::StackProtectStrong))
Bill Wendling606c8e32013-01-29 03:20:31 +0000147 return "sspstrong";
Bill Wendling034b94b2012-12-19 07:18:57 +0000148 if (hasAttribute(Attribute::NoRedZone))
Bill Wendling606c8e32013-01-29 03:20:31 +0000149 return "noredzone";
Bill Wendling034b94b2012-12-19 07:18:57 +0000150 if (hasAttribute(Attribute::NoImplicitFloat))
Bill Wendling606c8e32013-01-29 03:20:31 +0000151 return "noimplicitfloat";
Bill Wendling034b94b2012-12-19 07:18:57 +0000152 if (hasAttribute(Attribute::Naked))
Bill Wendling606c8e32013-01-29 03:20:31 +0000153 return "naked";
Bill Wendling034b94b2012-12-19 07:18:57 +0000154 if (hasAttribute(Attribute::NonLazyBind))
Bill Wendling606c8e32013-01-29 03:20:31 +0000155 return "nonlazybind";
Bill Wendling034b94b2012-12-19 07:18:57 +0000156 if (hasAttribute(Attribute::AddressSafety))
Bill Wendling606c8e32013-01-29 03:20:31 +0000157 return "address_safety";
Bill Wendling034b94b2012-12-19 07:18:57 +0000158 if (hasAttribute(Attribute::MinSize))
Bill Wendling606c8e32013-01-29 03:20:31 +0000159 return "minsize";
Bill Wendling034b94b2012-12-19 07:18:57 +0000160 if (hasAttribute(Attribute::StackAlignment)) {
Bill Wendling606c8e32013-01-29 03:20:31 +0000161 std::string Result;
Charles Davis1e063d12010-02-12 00:31:15 +0000162 Result += "alignstack(";
Bill Wendlingef99fe82012-09-21 15:26:31 +0000163 Result += utostr(getStackAlignment());
Bill Wendling606c8e32013-01-29 03:20:31 +0000164 Result += ")";
165 return Result;
Charles Davis1e063d12010-02-12 00:31:15 +0000166 }
Bill Wendling034b94b2012-12-19 07:18:57 +0000167 if (hasAttribute(Attribute::Alignment)) {
Bill Wendling606c8e32013-01-29 03:20:31 +0000168 std::string Result;
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000169 Result += "align ";
Bill Wendlingef99fe82012-09-21 15:26:31 +0000170 Result += utostr(getAlignment());
Bill Wendling606c8e32013-01-29 03:20:31 +0000171 Result += "";
172 return Result;
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000173 }
James Molloy67ae1352012-12-20 16:04:27 +0000174 if (hasAttribute(Attribute::NoDuplicate))
Bill Wendling606c8e32013-01-29 03:20:31 +0000175 return "noduplicate";
176
177 llvm_unreachable("Unknown attribute");
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000178}
179
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000180bool Attribute::operator==(AttrKind K) const {
181 return pImpl && *pImpl == K;
182}
183bool Attribute::operator!=(AttrKind K) const {
184 return !(*this == K);
185}
186
187bool Attribute::operator<(Attribute A) const {
188 if (!pImpl && !A.pImpl) return false;
189 if (!pImpl) return true;
190 if (!A.pImpl) return false;
191 return *pImpl < *A.pImpl;
192}
193
194uint64_t Attribute::Raw() const {
195 return pImpl ? pImpl->Raw() : 0;
196}
197
198//===----------------------------------------------------------------------===//
199// AttributeImpl Definition
200//===----------------------------------------------------------------------===//
201
Bill Wendling9f175f82013-01-29 20:37:10 +0000202AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind kind)
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000203 : Context(C) {
Bill Wendling9f175f82013-01-29 20:37:10 +0000204 Kind = ConstantInt::get(Type::getInt64Ty(C), kind);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000205}
Bill Wendling9f175f82013-01-29 20:37:10 +0000206AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind kind,
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000207 ArrayRef<Constant*> values)
208 : Context(C) {
Bill Wendling9f175f82013-01-29 20:37:10 +0000209 Kind = ConstantInt::get(Type::getInt64Ty(C), kind);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000210 Vals.reserve(values.size());
211 Vals.append(values.begin(), values.end());
212}
Bill Wendling9f175f82013-01-29 20:37:10 +0000213AttributeImpl::AttributeImpl(LLVMContext &C, StringRef kind)
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000214 : Context(C) {
Bill Wendling9f175f82013-01-29 20:37:10 +0000215 Kind = ConstantDataArray::getString(C, kind);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000216}
217
218bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
219 return (Raw() & getAttrMask(A)) != 0;
220}
221
222bool AttributeImpl::hasAttributes() const {
223 return Raw() != 0;
224}
225
226uint64_t AttributeImpl::getAlignment() const {
227 uint64_t Mask = Raw() & getAttrMask(Attribute::Alignment);
228 return 1ULL << ((Mask >> 16) - 1);
229}
230
231uint64_t AttributeImpl::getStackAlignment() const {
232 uint64_t Mask = Raw() & getAttrMask(Attribute::StackAlignment);
233 return 1ULL << ((Mask >> 26) - 1);
234}
235
Bill Wendling9f175f82013-01-29 20:37:10 +0000236bool AttributeImpl::operator==(Attribute::AttrKind kind) const {
237 if (ConstantInt *CI = dyn_cast<ConstantInt>(Kind))
238 return CI->getZExtValue() == kind;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000239 return false;
240}
Bill Wendling9f175f82013-01-29 20:37:10 +0000241bool AttributeImpl::operator!=(Attribute::AttrKind kind) const {
242 return !(*this == kind);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000243}
244
Bill Wendling9f175f82013-01-29 20:37:10 +0000245bool AttributeImpl::operator==(StringRef kind) const {
246 if (ConstantDataArray *CDA = dyn_cast<ConstantDataArray>(Kind))
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000247 if (CDA->isString())
Bill Wendling9f175f82013-01-29 20:37:10 +0000248 return CDA->getAsString() == kind;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000249 return false;
250}
251
Bill Wendling9f175f82013-01-29 20:37:10 +0000252bool AttributeImpl::operator!=(StringRef kind) const {
253 return !(*this == kind);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000254}
255
256bool AttributeImpl::operator<(const AttributeImpl &AI) const {
Bill Wendling9f175f82013-01-29 20:37:10 +0000257 if (!Kind && !AI.Kind) return false;
258 if (!Kind && AI.Kind) return true;
259 if (Kind && !AI.Kind) return false;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000260
Bill Wendling9f175f82013-01-29 20:37:10 +0000261 ConstantInt *ThisCI = dyn_cast<ConstantInt>(Kind);
262 ConstantInt *ThatCI = dyn_cast<ConstantInt>(AI.Kind);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000263
Bill Wendling9f175f82013-01-29 20:37:10 +0000264 ConstantDataArray *ThisCDA = dyn_cast<ConstantDataArray>(Kind);
265 ConstantDataArray *ThatCDA = dyn_cast<ConstantDataArray>(AI.Kind);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000266
267 if (ThisCI && ThatCI)
268 return ThisCI->getZExtValue() < ThatCI->getZExtValue();
269
270 if (ThisCI && ThatCDA)
271 return true;
272
273 if (ThisCDA && ThatCI)
274 return false;
275
276 return ThisCDA->getAsString() < ThatCDA->getAsString();
277}
278
279uint64_t AttributeImpl::Raw() const {
280 // FIXME: Remove this.
Bill Wendling9f175f82013-01-29 20:37:10 +0000281 return cast<ConstantInt>(Kind)->getZExtValue();
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000282}
283
284uint64_t AttributeImpl::getAttrMask(Attribute::AttrKind Val) {
285 // FIXME: Remove this.
286 switch (Val) {
287 case Attribute::EndAttrKinds:
288 case Attribute::AttrKindEmptyKey:
289 case Attribute::AttrKindTombstoneKey:
290 llvm_unreachable("Synthetic enumerators which should never get here");
291
292 case Attribute::None: return 0;
293 case Attribute::ZExt: return 1 << 0;
294 case Attribute::SExt: return 1 << 1;
295 case Attribute::NoReturn: return 1 << 2;
296 case Attribute::InReg: return 1 << 3;
297 case Attribute::StructRet: return 1 << 4;
298 case Attribute::NoUnwind: return 1 << 5;
299 case Attribute::NoAlias: return 1 << 6;
300 case Attribute::ByVal: return 1 << 7;
301 case Attribute::Nest: return 1 << 8;
302 case Attribute::ReadNone: return 1 << 9;
303 case Attribute::ReadOnly: return 1 << 10;
304 case Attribute::NoInline: return 1 << 11;
305 case Attribute::AlwaysInline: return 1 << 12;
306 case Attribute::OptimizeForSize: return 1 << 13;
307 case Attribute::StackProtect: return 1 << 14;
308 case Attribute::StackProtectReq: return 1 << 15;
309 case Attribute::Alignment: return 31 << 16;
310 case Attribute::NoCapture: return 1 << 21;
311 case Attribute::NoRedZone: return 1 << 22;
312 case Attribute::NoImplicitFloat: return 1 << 23;
313 case Attribute::Naked: return 1 << 24;
314 case Attribute::InlineHint: return 1 << 25;
315 case Attribute::StackAlignment: return 7 << 26;
316 case Attribute::ReturnsTwice: return 1 << 29;
317 case Attribute::UWTable: return 1 << 30;
318 case Attribute::NonLazyBind: return 1U << 31;
319 case Attribute::AddressSafety: return 1ULL << 32;
320 case Attribute::MinSize: return 1ULL << 33;
321 case Attribute::NoDuplicate: return 1ULL << 34;
322 case Attribute::StackProtectStrong: return 1ULL << 35;
323 }
324 llvm_unreachable("Unsupported attribute type");
325}
326
327//===----------------------------------------------------------------------===//
328// AttributeSetNode Definition
329//===----------------------------------------------------------------------===//
330
331AttributeSetNode *AttributeSetNode::get(LLVMContext &C,
332 ArrayRef<Attribute> Attrs) {
333 if (Attrs.empty())
334 return 0;
335
336 // Otherwise, build a key to look up the existing attributes.
337 LLVMContextImpl *pImpl = C.pImpl;
338 FoldingSetNodeID ID;
339
340 SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end());
341 std::sort(SortedAttrs.begin(), SortedAttrs.end());
342
343 for (SmallVectorImpl<Attribute>::iterator I = SortedAttrs.begin(),
344 E = SortedAttrs.end(); I != E; ++I)
345 I->Profile(ID);
346
347 void *InsertPoint;
348 AttributeSetNode *PA =
349 pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint);
350
351 // If we didn't find any existing attributes of the same shape then create a
352 // new one and insert it.
353 if (!PA) {
354 PA = new AttributeSetNode(SortedAttrs);
355 pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint);
356 }
357
358 // Return the AttributesListNode that we found or created.
359 return PA;
360}
361
Bill Wendling606c8e32013-01-29 03:20:31 +0000362bool AttributeSetNode::hasAttribute(Attribute::AttrKind Kind) const {
363 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
364 E = AttrList.end(); I != E; ++I)
365 if (I->hasAttribute(Kind))
366 return true;
367 return false;
368}
369
370unsigned AttributeSetNode::getAlignment() const {
371 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
372 E = AttrList.end(); I != E; ++I)
373 if (I->hasAttribute(Attribute::Alignment))
374 return I->getAlignment();
375 return 0;
376}
377
378unsigned AttributeSetNode::getStackAlignment() const {
379 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
380 E = AttrList.end(); I != E; ++I)
381 if (I->hasAttribute(Attribute::StackAlignment))
382 return I->getStackAlignment();
383 return 0;
384}
385
386std::string AttributeSetNode::getAsString() const {
387 std::string Str = "";
388 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
389 E = AttrList.end(); I != E; ++I) {
390 if (I != AttrList.begin()) Str += " ";
391 Str += I->getAsString();
392 }
393 return Str;
394}
395
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000396//===----------------------------------------------------------------------===//
397// AttributeSetImpl Definition
398//===----------------------------------------------------------------------===//
399
400uint64_t AttributeSetImpl::Raw(uint64_t Index) const {
401 for (unsigned I = 0, E = getNumAttributes(); I != E; ++I) {
402 if (getSlotIndex(I) != Index) continue;
403 const AttributeSetNode *ASN = AttrNodes[I].second;
404 AttrBuilder B;
405
406 for (AttributeSetNode::const_iterator II = ASN->begin(),
407 IE = ASN->end(); II != IE; ++II)
408 B.addAttributes(*II);
409 return B.Raw();
410 }
411
412 return 0;
413}
414
415//===----------------------------------------------------------------------===//
416// AttributeSet Construction and Mutation Methods
417//===----------------------------------------------------------------------===//
418
Bill Wendling8232ece2013-01-29 01:43:29 +0000419AttributeSet
420AttributeSet::getImpl(LLVMContext &C,
421 ArrayRef<std::pair<unsigned, AttributeSetNode*> > Attrs) {
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000422 LLVMContextImpl *pImpl = C.pImpl;
423 FoldingSetNodeID ID;
424 AttributeSetImpl::Profile(ID, Attrs);
425
426 void *InsertPoint;
427 AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
428
429 // If we didn't find any existing attributes of the same shape then
430 // create a new one and insert it.
431 if (!PA) {
432 PA = new AttributeSetImpl(C, Attrs);
433 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
434 }
435
436 // Return the AttributesList that we found or created.
437 return AttributeSet(PA);
438}
439
440AttributeSet AttributeSet::get(LLVMContext &C,
441 ArrayRef<std::pair<unsigned, Attribute> > Attrs){
442 // If there are no attributes then return a null AttributesList pointer.
443 if (Attrs.empty())
444 return AttributeSet();
445
446#ifndef NDEBUG
447 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
448 assert((!i || Attrs[i-1].first <= Attrs[i].first) &&
449 "Misordered Attributes list!");
450 assert(Attrs[i].second.hasAttributes() &&
451 "Pointless attribute!");
452 }
453#endif
454
455 // Create a vector if (unsigned, AttributeSetNode*) pairs from the attributes
456 // list.
457 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrPairVec;
458 for (ArrayRef<std::pair<unsigned, Attribute> >::iterator I = Attrs.begin(),
459 E = Attrs.end(); I != E; ) {
460 unsigned Index = I->first;
461 SmallVector<Attribute, 4> AttrVec;
NAKAMURA Takumi3ba51ce2013-01-29 15:18:16 +0000462 while (I != E && I->first == Index) {
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000463 AttrVec.push_back(I->second);
464 ++I;
465 }
466
467 AttrPairVec.push_back(std::make_pair(Index,
468 AttributeSetNode::get(C, AttrVec)));
469 }
470
471 return getImpl(C, AttrPairVec);
472}
473
474AttributeSet AttributeSet::get(LLVMContext &C,
475 ArrayRef<std::pair<unsigned,
476 AttributeSetNode*> > Attrs) {
477 // If there are no attributes then return a null AttributesList pointer.
478 if (Attrs.empty())
479 return AttributeSet();
480
481 return getImpl(C, Attrs);
482}
483
484AttributeSet AttributeSet::get(LLVMContext &C, unsigned Idx, AttrBuilder &B) {
485 if (!B.hasAttributes())
486 return AttributeSet();
Bill Wendling8fbc0c22013-01-29 01:02:03 +0000487
488 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
489 for (AttrBuilder::iterator I = B.begin(), E = B.end(); I != E; ++I) {
490 Attribute::AttrKind Kind = *I;
491 if (Kind == Attribute::Alignment)
492 Attrs.push_back(std::make_pair(Idx, Attribute::
493 getWithAlignment(C, B.getAlignment())));
494 else if (Kind == Attribute::StackAlignment)
495 Attrs.push_back(std::make_pair(Idx, Attribute::
496 getWithStackAlignment(C, B.getStackAlignment())));
497 else
498 Attrs.push_back(std::make_pair(Idx, Attribute::get(C, Kind)));
499 }
500
501 return get(C, Attrs);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000502}
503
504AttributeSet AttributeSet::get(LLVMContext &C, unsigned Idx,
505 ArrayRef<Attribute::AttrKind> Kind) {
506 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
507 for (ArrayRef<Attribute::AttrKind>::iterator I = Kind.begin(),
508 E = Kind.end(); I != E; ++I)
509 Attrs.push_back(std::make_pair(Idx, Attribute::get(C, *I)));
510 return get(C, Attrs);
511}
512
513AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<AttributeSet> Attrs) {
514 if (Attrs.empty()) return AttributeSet();
515
516 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrNodeVec;
517 for (unsigned I = 0, E = Attrs.size(); I != E; ++I) {
518 AttributeSet AS = Attrs[I];
519 if (!AS.pImpl) continue;
520 AttrNodeVec.append(AS.pImpl->AttrNodes.begin(), AS.pImpl->AttrNodes.end());
521 }
522
523 return getImpl(C, AttrNodeVec);
524}
525
526AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Idx,
527 Attribute::AttrKind Attr) const {
528 return addAttributes(C, Idx, AttributeSet::get(C, Idx, Attr));
529}
530
531AttributeSet AttributeSet::addAttributes(LLVMContext &C, unsigned Idx,
532 AttributeSet Attrs) const {
533 if (!pImpl) return Attrs;
534 if (!Attrs.pImpl) return *this;
535
536#ifndef NDEBUG
537 // FIXME it is not obvious how this should work for alignment. For now, say
538 // we can't change a known alignment.
539 unsigned OldAlign = getParamAlignment(Idx);
540 unsigned NewAlign = Attrs.getParamAlignment(Idx);
541 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
542 "Attempt to change alignment!");
543#endif
544
545 // Add the attribute slots before the one we're trying to add.
546 SmallVector<AttributeSet, 4> AttrSet;
547 uint64_t NumAttrs = pImpl->getNumAttributes();
548 AttributeSet AS;
549 uint64_t LastIndex = 0;
550 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
551 if (getSlotIndex(I) >= Idx) {
552 if (getSlotIndex(I) == Idx) AS = getSlotAttributes(LastIndex++);
553 break;
554 }
555 LastIndex = I + 1;
556 AttrSet.push_back(getSlotAttributes(I));
557 }
558
559 // Now add the attribute into the correct slot. There may already be an
560 // AttributeSet there.
561 AttrBuilder B(AS, Idx);
562
563 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
564 if (Attrs.getSlotIndex(I) == Idx) {
565 for (AttributeSetImpl::const_iterator II = Attrs.pImpl->begin(I),
566 IE = Attrs.pImpl->end(I); II != IE; ++II)
567 B.addAttributes(*II);
568 break;
569 }
570
571 AttrSet.push_back(AttributeSet::get(C, Idx, B));
572
573 // Add the remaining attribute slots.
574 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
575 AttrSet.push_back(getSlotAttributes(I));
576
577 return get(C, AttrSet);
578}
579
580AttributeSet AttributeSet::removeAttribute(LLVMContext &C, unsigned Idx,
581 Attribute::AttrKind Attr) const {
582 return removeAttributes(C, Idx, AttributeSet::get(C, Idx, Attr));
583}
584
585AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Idx,
586 AttributeSet Attrs) const {
587 if (!pImpl) return AttributeSet();
588 if (!Attrs.pImpl) return *this;
589
590#ifndef NDEBUG
591 // FIXME it is not obvious how this should work for alignment.
592 // For now, say we can't pass in alignment, which no current use does.
593 assert(!Attrs.hasAttribute(Idx, Attribute::Alignment) &&
594 "Attempt to change alignment!");
595#endif
596
597 // Add the attribute slots before the one we're trying to add.
598 SmallVector<AttributeSet, 4> AttrSet;
599 uint64_t NumAttrs = pImpl->getNumAttributes();
600 AttributeSet AS;
601 uint64_t LastIndex = 0;
602 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
603 if (getSlotIndex(I) >= Idx) {
604 if (getSlotIndex(I) == Idx) AS = getSlotAttributes(LastIndex++);
605 break;
606 }
607 LastIndex = I + 1;
608 AttrSet.push_back(getSlotAttributes(I));
609 }
610
Bill Wendlinge7436542013-01-30 23:07:40 +0000611 // Now remove the attribute from the correct slot. There may already be an
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000612 // AttributeSet there.
613 AttrBuilder B(AS, Idx);
614
615 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
616 if (Attrs.getSlotIndex(I) == Idx) {
Bill Wendlinge7436542013-01-30 23:07:40 +0000617 B.removeAttributes(Attrs.pImpl->getSlotAttributes(I), Idx);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000618 break;
619 }
620
621 AttrSet.push_back(AttributeSet::get(C, Idx, B));
622
623 // Add the remaining attribute slots.
624 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
625 AttrSet.push_back(getSlotAttributes(I));
626
627 return get(C, AttrSet);
628}
629
630//===----------------------------------------------------------------------===//
631// AttributeSet Accessor Methods
632//===----------------------------------------------------------------------===//
633
634AttributeSet AttributeSet::getParamAttributes(unsigned Idx) const {
635 return pImpl && hasAttributes(Idx) ?
636 AttributeSet::get(pImpl->getContext(),
Bill Wendling606c8e32013-01-29 03:20:31 +0000637 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000638 std::make_pair(Idx, getAttributes(Idx)))) :
639 AttributeSet();
640}
641
642AttributeSet AttributeSet::getRetAttributes() const {
643 return pImpl && hasAttributes(ReturnIndex) ?
644 AttributeSet::get(pImpl->getContext(),
Bill Wendling606c8e32013-01-29 03:20:31 +0000645 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000646 std::make_pair(ReturnIndex,
647 getAttributes(ReturnIndex)))) :
648 AttributeSet();
649}
650
651AttributeSet AttributeSet::getFnAttributes() const {
652 return pImpl && hasAttributes(FunctionIndex) ?
653 AttributeSet::get(pImpl->getContext(),
Bill Wendling606c8e32013-01-29 03:20:31 +0000654 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000655 std::make_pair(FunctionIndex,
656 getAttributes(FunctionIndex)))) :
657 AttributeSet();
658}
659
660bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{
Bill Wendling606c8e32013-01-29 03:20:31 +0000661 AttributeSetNode *ASN = getAttributes(Index);
662 return ASN ? ASN->hasAttribute(Kind) : false;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000663}
664
665bool AttributeSet::hasAttributes(unsigned Index) const {
Bill Wendling606c8e32013-01-29 03:20:31 +0000666 AttributeSetNode *ASN = getAttributes(Index);
667 return ASN ? ASN->hasAttributes() : false;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000668}
669
670/// \brief Return true if the specified attribute is set for at least one
671/// parameter or for the return value.
672bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr) const {
673 if (pImpl == 0) return false;
674
675 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
676 for (AttributeSetImpl::const_iterator II = pImpl->begin(I),
677 IE = pImpl->end(I); II != IE; ++II)
678 if (II->hasAttribute(Attr))
679 return true;
680
681 return false;
682}
683
Bill Wendling606c8e32013-01-29 03:20:31 +0000684unsigned AttributeSet::getParamAlignment(unsigned Index) const {
685 AttributeSetNode *ASN = getAttributes(Index);
686 return ASN ? ASN->getAlignment() : 0;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000687}
688
689unsigned AttributeSet::getStackAlignment(unsigned Index) const {
Bill Wendling606c8e32013-01-29 03:20:31 +0000690 AttributeSetNode *ASN = getAttributes(Index);
691 return ASN ? ASN->getStackAlignment() : 0;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000692}
693
694std::string AttributeSet::getAsString(unsigned Index) const {
Bill Wendling606c8e32013-01-29 03:20:31 +0000695 AttributeSetNode *ASN = getAttributes(Index);
696 return ASN ? ASN->getAsString() : std::string("");
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000697}
698
699/// \brief The attributes for the specified index are returned.
Bill Wendling606c8e32013-01-29 03:20:31 +0000700AttributeSetNode *AttributeSet::getAttributes(unsigned Idx) const {
701 if (!pImpl) return 0;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000702
Bill Wendling606c8e32013-01-29 03:20:31 +0000703 // Loop through to find the attribute node we want.
704 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
705 if (pImpl->getSlotIndex(I) == Idx)
706 return pImpl->getSlotNode(I);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000707
Bill Wendling606c8e32013-01-29 03:20:31 +0000708 return 0;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000709}
710
711//===----------------------------------------------------------------------===//
712// AttributeSet Introspection Methods
713//===----------------------------------------------------------------------===//
714
715/// \brief Return the number of slots used in this attribute list. This is the
716/// number of arguments that have an attribute set on them (including the
717/// function itself).
718unsigned AttributeSet::getNumSlots() const {
719 return pImpl ? pImpl->getNumAttributes() : 0;
720}
721
722uint64_t AttributeSet::getSlotIndex(unsigned Slot) const {
723 assert(pImpl && Slot < pImpl->getNumAttributes() &&
724 "Slot # out of range!");
725 return pImpl->getSlotIndex(Slot);
726}
727
728AttributeSet AttributeSet::getSlotAttributes(unsigned Slot) const {
729 assert(pImpl && Slot < pImpl->getNumAttributes() &&
730 "Slot # out of range!");
731 return pImpl->getSlotAttributes(Slot);
732}
733
734uint64_t AttributeSet::Raw(unsigned Index) const {
735 // FIXME: Remove this.
736 return pImpl ? pImpl->Raw(Index) : 0;
737}
738
739void AttributeSet::dump() const {
740 dbgs() << "PAL[\n";
741
742 for (unsigned i = 0, e = getNumSlots(); i < e; ++i) {
743 uint64_t Index = getSlotIndex(i);
744 dbgs() << " { ";
745 if (Index == ~0U)
746 dbgs() << "~0U";
747 else
748 dbgs() << Index;
749 dbgs() << " => " << getAsString(Index) << " }\n";
750 }
751
752 dbgs() << "]\n";
753}
754
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000755//===----------------------------------------------------------------------===//
Bill Wendling03198882013-01-04 23:27:34 +0000756// AttrBuilder Method Implementations
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000757//===----------------------------------------------------------------------===//
758
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000759AttrBuilder::AttrBuilder(AttributeSet AS, unsigned Idx)
760 : Alignment(0), StackAlignment(0) {
Bill Wendlingec258982013-01-27 21:23:46 +0000761 AttributeSetImpl *pImpl = AS.pImpl;
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000762 if (!pImpl) return;
763
Bill Wendling73bc4522013-01-28 00:21:34 +0000764 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I) {
765 if (pImpl->getSlotIndex(I) != Idx) continue;
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000766
Bill Wendling383da6b2013-01-30 21:22:59 +0000767 for (AttributeSetImpl::const_iterator II = pImpl->begin(I),
Bill Wendling73bc4522013-01-28 00:21:34 +0000768 IE = pImpl->end(I); II != IE; ++II)
Bill Wendling383da6b2013-01-30 21:22:59 +0000769 addAttributes(*II);
Bill Wendling73bc4522013-01-28 00:21:34 +0000770
771 break;
772 }
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000773}
774
Bill Wendling03198882013-01-04 23:27:34 +0000775void AttrBuilder::clear() {
776 Attrs.clear();
777 Alignment = StackAlignment = 0;
778}
779
780AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
781 Attrs.insert(Val);
Bill Wendling3a106e62012-10-09 19:01:18 +0000782 return *this;
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000783}
784
Bill Wendling03198882013-01-04 23:27:34 +0000785AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
786 Attrs.erase(Val);
787 if (Val == Attribute::Alignment)
788 Alignment = 0;
789 else if (Val == Attribute::StackAlignment)
790 StackAlignment = 0;
791
Bill Wendlinga19a5302012-10-14 04:10:01 +0000792 return *this;
793}
794
Bill Wendling49f60602013-01-28 05:23:28 +0000795AttrBuilder &AttrBuilder::addAttributes(Attribute Attr) {
796 uint64_t Mask = Attr.Raw();
797
798 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
799 I = Attribute::AttrKind(I + 1))
800 if ((Mask & AttributeImpl::getAttrMask(I)) != 0)
801 Attrs.insert(I);
802
803 if (Attr.getAlignment())
804 Alignment = Attr.getAlignment();
805 if (Attr.getStackAlignment())
806 StackAlignment = Attr.getStackAlignment();
807 return *this;
808}
809
Bill Wendlinge7436542013-01-30 23:07:40 +0000810AttrBuilder &AttrBuilder::removeAttributes(AttributeSet A, uint64_t Index) {
811 uint64_t Mask = A.Raw(Index);
Bill Wendling49f60602013-01-28 05:23:28 +0000812
813 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
814 I = Attribute::AttrKind(I + 1)) {
815 if (Mask & AttributeImpl::getAttrMask(I)) {
816 Attrs.erase(I);
817
818 if (I == Attribute::Alignment)
819 Alignment = 0;
820 else if (I == Attribute::StackAlignment)
821 StackAlignment = 0;
822 }
823 }
824
825 return *this;
826}
827
Bill Wendling702cc912012-10-15 20:35:56 +0000828AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
Bill Wendlingda3f9d82012-10-14 03:58:29 +0000829 if (Align == 0) return *this;
Bill Wendling03198882013-01-04 23:27:34 +0000830
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000831 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
832 assert(Align <= 0x40000000 && "Alignment too large.");
Bill Wendling03198882013-01-04 23:27:34 +0000833
834 Attrs.insert(Attribute::Alignment);
835 Alignment = Align;
Bill Wendlingda3f9d82012-10-14 03:58:29 +0000836 return *this;
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000837}
838
Bill Wendling03198882013-01-04 23:27:34 +0000839AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
840 // Default alignment, allow the target to define how to align it.
841 if (Align == 0) return *this;
842
843 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
844 assert(Align <= 0x100 && "Alignment too large.");
845
846 Attrs.insert(Attribute::StackAlignment);
847 StackAlignment = Align;
848 return *this;
849}
850
Bill Wendling22bd6412013-01-03 01:54:39 +0000851bool AttrBuilder::contains(Attribute::AttrKind A) const {
Bill Wendling03198882013-01-04 23:27:34 +0000852 return Attrs.count(A);
Bill Wendling7d2f2492012-10-10 07:36:45 +0000853}
854
Bill Wendling702cc912012-10-15 20:35:56 +0000855bool AttrBuilder::hasAttributes() const {
Bill Wendling03198882013-01-04 23:27:34 +0000856 return !Attrs.empty();
Bill Wendlingf385f4c2012-10-08 23:27:46 +0000857}
Bill Wendling60507d52013-01-04 20:54:35 +0000858
Bill Wendlinge7436542013-01-30 23:07:40 +0000859bool AttrBuilder::hasAttributes(AttributeSet A, uint64_t Index) const {
860 return Raw() & A.Raw(Index);
Bill Wendling8831c062012-10-09 00:01:21 +0000861}
Bill Wendling60507d52013-01-04 20:54:35 +0000862
Bill Wendling702cc912012-10-15 20:35:56 +0000863bool AttrBuilder::hasAlignmentAttr() const {
Bill Wendling03198882013-01-04 23:27:34 +0000864 return Alignment != 0;
Bill Wendlingf385f4c2012-10-08 23:27:46 +0000865}
866
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000867bool AttrBuilder::operator==(const AttrBuilder &B) {
868 SmallVector<Attribute::AttrKind, 8> This(Attrs.begin(), Attrs.end());
869 SmallVector<Attribute::AttrKind, 8> That(B.Attrs.begin(), B.Attrs.end());
870 return This == That;
871}
872
873AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) {
Bill Wendling8232ece2013-01-29 01:43:29 +0000874 if (!Val) return *this;
875
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000876 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
877 I = Attribute::AttrKind(I + 1)) {
878 if (uint64_t A = (Val & AttributeImpl::getAttrMask(I))) {
879 Attrs.insert(I);
880
881 if (I == Attribute::Alignment)
882 Alignment = 1ULL << ((A >> 16) - 1);
883 else if (I == Attribute::StackAlignment)
884 StackAlignment = 1ULL << ((A >> 26)-1);
885 }
886 }
887
888 return *this;
889}
890
Bill Wendling1db9b692013-01-09 23:36:50 +0000891uint64_t AttrBuilder::Raw() const {
Bill Wendling03198882013-01-04 23:27:34 +0000892 uint64_t Mask = 0;
893
894 for (DenseSet<Attribute::AttrKind>::const_iterator I = Attrs.begin(),
895 E = Attrs.end(); I != E; ++I) {
896 Attribute::AttrKind Kind = *I;
897
898 if (Kind == Attribute::Alignment)
899 Mask |= (Log2_32(Alignment) + 1) << 16;
900 else if (Kind == Attribute::StackAlignment)
901 Mask |= (Log2_32(StackAlignment) + 1) << 26;
902 else
903 Mask |= AttributeImpl::getAttrMask(Kind);
904 }
905
906 return Mask;
Bill Wendlingf385f4c2012-10-08 23:27:46 +0000907}
908
Bill Wendling8e47daf2013-01-25 23:09:36 +0000909//===----------------------------------------------------------------------===//
910// AttributeFuncs Function Defintions
911//===----------------------------------------------------------------------===//
912
Bill Wendlinge7436542013-01-30 23:07:40 +0000913AttributeSet AttributeFuncs::typeIncompatible(Type *Ty, uint64_t Index) {
Bill Wendling8e47daf2013-01-25 23:09:36 +0000914 AttrBuilder Incompatible;
915
916 if (!Ty->isIntegerTy())
917 // Attribute that only apply to integers.
918 Incompatible.addAttribute(Attribute::SExt)
919 .addAttribute(Attribute::ZExt);
920
921 if (!Ty->isPointerTy())
922 // Attribute that only apply to pointers.
923 Incompatible.addAttribute(Attribute::ByVal)
924 .addAttribute(Attribute::Nest)
925 .addAttribute(Attribute::NoAlias)
926 .addAttribute(Attribute::NoCapture)
927 .addAttribute(Attribute::StructRet);
928
Bill Wendlinge7436542013-01-30 23:07:40 +0000929 return AttributeSet::get(Ty->getContext(), Index, Incompatible);
Bill Wendling8e47daf2013-01-25 23:09:36 +0000930}
931
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000932/// \brief This returns an integer containing an encoding of all the LLVM
933/// attributes found in the given attribute bitset. Any change to this encoding
934/// is a breaking change to bitcode compatibility.
Bill Wendling8232ece2013-01-29 01:43:29 +0000935/// N.B. This should be used only by the bitcode reader!
Bill Wendling8e47daf2013-01-25 23:09:36 +0000936uint64_t AttributeFuncs::encodeLLVMAttributesForBitcode(AttributeSet Attrs,
937 unsigned Index) {
938 // FIXME: It doesn't make sense to store the alignment information as an
939 // expanded out value, we should store it as a log2 value. However, we can't
940 // just change that here without breaking bitcode compatibility. If this ever
941 // becomes a problem in practice, we should introduce new tag numbers in the
942 // bitcode file and have those tags use a more efficiently encoded alignment
943 // field.
944
945 // Store the alignment in the bitcode as a 16-bit raw value instead of a 5-bit
946 // log2 encoded value. Shift the bits above the alignment up by 11 bits.
947 uint64_t EncodedAttrs = Attrs.Raw(Index) & 0xffff;
948 if (Attrs.hasAttribute(Index, Attribute::Alignment))
949 EncodedAttrs |= Attrs.getParamAlignment(Index) << 16;
950 EncodedAttrs |= (Attrs.Raw(Index) & (0xffffULL << 21)) << 11;
951 return EncodedAttrs;
952}
953
Bill Wendling8232ece2013-01-29 01:43:29 +0000954/// \brief This fills an AttrBuilder object with the LLVM attributes that have
955/// been decoded from the given integer. This function must stay in sync with
956/// 'encodeLLVMAttributesForBitcode'.
957/// N.B. This should be used only by the bitcode reader!
958void AttributeFuncs::decodeLLVMAttributesForBitcode(LLVMContext &C,
959 AttrBuilder &B,
960 uint64_t EncodedAttrs) {
Bill Wendling8e47daf2013-01-25 23:09:36 +0000961 // The alignment is stored as a 16-bit raw value from bits 31--16. We shift
962 // the bits above 31 down by 11 bits.
963 unsigned Alignment = (EncodedAttrs & (0xffffULL << 16)) >> 16;
964 assert((!Alignment || isPowerOf2_32(Alignment)) &&
965 "Alignment must be a power of two.");
966
Bill Wendling8e47daf2013-01-25 23:09:36 +0000967 if (Alignment)
968 B.addAlignmentAttr(Alignment);
Bill Wendling606c8e32013-01-29 03:20:31 +0000969 B.addRawValue(((EncodedAttrs & (0xffffULL << 32)) >> 11) |
970 (EncodedAttrs & 0xffff));
Bill Wendling8e47daf2013-01-25 23:09:36 +0000971}