blob: c2ea2b29eb62a0ed7004d65ef64df0d23d0c5b70 [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 Wendling169d5272013-01-31 23:16:25 +000033Attribute Attribute::get(LLVMContext &Context, Constant *Kind, Constant *Val) {
Bill Wendling8e635db2012-10-08 21:47:17 +000034 LLVMContextImpl *pImpl = Context.pImpl;
35 FoldingSetNodeID ID;
Bill Wendling169d5272013-01-31 23:16:25 +000036 ID.AddPointer(Kind);
37 if (Val) ID.AddPointer(Val);
Bill Wendling8e635db2012-10-08 21:47:17 +000038
39 void *InsertPoint;
Bill Wendlingf6670722012-12-20 01:36:59 +000040 AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
Bill Wendling8e635db2012-10-08 21:47:17 +000041
42 if (!PA) {
43 // If we didn't find any existing attributes of the same shape then create a
44 // new one and insert it.
Bill Wendling169d5272013-01-31 23:16:25 +000045 PA = (!Val) ?
46 new AttributeImpl(Context, Kind) :
47 new AttributeImpl(Context, Kind, Val);
Bill Wendling8e635db2012-10-08 21:47:17 +000048 pImpl->AttrsSet.InsertNode(PA, InsertPoint);
49 }
50
51 // Return the AttributesList that we found or created.
Bill Wendling034b94b2012-12-19 07:18:57 +000052 return Attribute(PA);
Bill Wendling8e635db2012-10-08 21:47:17 +000053}
54
Bill Wendling169d5272013-01-31 23:16:25 +000055Attribute Attribute::get(LLVMContext &Context, AttrKind Kind, Constant *Val) {
56 ConstantInt *KindVal = ConstantInt::get(Type::getInt64Ty(Context), Kind);
57 return get(Context, KindVal, Val);
58}
59
Bill Wendlingc08a5ef2013-01-27 22:43:04 +000060Attribute Attribute::getWithAlignment(LLVMContext &Context, uint64_t Align) {
Bill Wendling169d5272013-01-31 23:16:25 +000061 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
62 assert(Align <= 0x40000000 && "Alignment too large.");
63 return get(Context, Alignment,
64 ConstantInt::get(Type::getInt64Ty(Context), Align));
Bill Wendlingc08a5ef2013-01-27 22:43:04 +000065}
66
67Attribute Attribute::getWithStackAlignment(LLVMContext &Context,
68 uint64_t Align) {
Bill Wendling169d5272013-01-31 23:16:25 +000069 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
70 assert(Align <= 0x100 && "Alignment too large.");
71 return get(Context, StackAlignment,
72 ConstantInt::get(Type::getInt64Ty(Context), Align));
Bill Wendlingc08a5ef2013-01-27 22:43:04 +000073}
74
Bill Wendling817abdd2013-01-29 00:48:16 +000075//===----------------------------------------------------------------------===//
76// Attribute Accessor Methods
77//===----------------------------------------------------------------------===//
78
Bill Wendling629fb822012-12-22 00:37:52 +000079bool Attribute::hasAttribute(AttrKind Val) const {
Bill Wendling27107f62012-12-20 21:28:43 +000080 return pImpl && pImpl->hasAttribute(Val);
Bill Wendlinge66f3d32012-10-05 06:44:41 +000081}
82
Bill Wendling6dc37812013-01-29 20:45:34 +000083Constant *Attribute::getAttributeKind() const {
84 return pImpl ? pImpl->getAttributeKind() : 0;
85}
86
87ArrayRef<Constant*> Attribute::getAttributeValues() const {
88 return pImpl ? pImpl->getAttributeValues() : ArrayRef<Constant*>();
89}
90
Bill Wendlinge66f3d32012-10-05 06:44:41 +000091/// This returns the alignment field of an attribute as a byte alignment value.
Bill Wendling034b94b2012-12-19 07:18:57 +000092unsigned Attribute::getAlignment() const {
93 if (!hasAttribute(Attribute::Alignment))
Bill Wendling9ef99c92012-10-09 20:56:48 +000094 return 0;
Bill Wendlinga8ab5fc2013-01-23 23:00:05 +000095 return pImpl->getAlignment();
Bill Wendlinge66f3d32012-10-05 06:44:41 +000096}
97
98/// This returns the stack alignment field of an attribute as a byte alignment
99/// value.
Bill Wendling034b94b2012-12-19 07:18:57 +0000100unsigned Attribute::getStackAlignment() const {
101 if (!hasAttribute(Attribute::StackAlignment))
Bill Wendling9ef99c92012-10-09 20:56:48 +0000102 return 0;
Bill Wendlinga8ab5fc2013-01-23 23:00:05 +0000103 return pImpl->getStackAlignment();
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000104}
105
Bill Wendling034b94b2012-12-19 07:18:57 +0000106std::string Attribute::getAsString() const {
Bill Wendling14292a62013-01-31 20:59:05 +0000107 if (!pImpl) return "";
108
109 if (hasAttribute(Attribute::AddressSafety))
110 return "address_safety";
111 if (hasAttribute(Attribute::AlwaysInline))
112 return "alwaysinline";
113 if (hasAttribute(Attribute::ByVal))
114 return "byval";
115 if (hasAttribute(Attribute::InlineHint))
116 return "inlinehint";
Bill Wendling034b94b2012-12-19 07:18:57 +0000117 if (hasAttribute(Attribute::InReg))
Bill Wendling606c8e32013-01-29 03:20:31 +0000118 return "inreg";
Bill Wendling14292a62013-01-31 20:59:05 +0000119 if (hasAttribute(Attribute::MinSize))
120 return "minsize";
121 if (hasAttribute(Attribute::Naked))
122 return "naked";
123 if (hasAttribute(Attribute::Nest))
124 return "nest";
Bill Wendling034b94b2012-12-19 07:18:57 +0000125 if (hasAttribute(Attribute::NoAlias))
Bill Wendling606c8e32013-01-29 03:20:31 +0000126 return "noalias";
Bill Wendling034b94b2012-12-19 07:18:57 +0000127 if (hasAttribute(Attribute::NoCapture))
Bill Wendling606c8e32013-01-29 03:20:31 +0000128 return "nocapture";
Bill Wendling14292a62013-01-31 20:59:05 +0000129 if (hasAttribute(Attribute::NoDuplicate))
130 return "noduplicate";
131 if (hasAttribute(Attribute::NoImplicitFloat))
132 return "noimplicitfloat";
133 if (hasAttribute(Attribute::NoInline))
134 return "noinline";
135 if (hasAttribute(Attribute::NonLazyBind))
136 return "nonlazybind";
137 if (hasAttribute(Attribute::NoRedZone))
138 return "noredzone";
139 if (hasAttribute(Attribute::NoReturn))
140 return "noreturn";
141 if (hasAttribute(Attribute::NoUnwind))
142 return "nounwind";
143 if (hasAttribute(Attribute::OptimizeForSize))
144 return "optsize";
Bill Wendling034b94b2012-12-19 07:18:57 +0000145 if (hasAttribute(Attribute::ReadNone))
Bill Wendling606c8e32013-01-29 03:20:31 +0000146 return "readnone";
Bill Wendling034b94b2012-12-19 07:18:57 +0000147 if (hasAttribute(Attribute::ReadOnly))
Bill Wendling606c8e32013-01-29 03:20:31 +0000148 return "readonly";
Bill Wendling14292a62013-01-31 20:59:05 +0000149 if (hasAttribute(Attribute::ReturnsTwice))
150 return "returns_twice";
151 if (hasAttribute(Attribute::SExt))
152 return "signext";
Bill Wendling034b94b2012-12-19 07:18:57 +0000153 if (hasAttribute(Attribute::StackProtect))
Bill Wendling606c8e32013-01-29 03:20:31 +0000154 return "ssp";
Bill Wendling034b94b2012-12-19 07:18:57 +0000155 if (hasAttribute(Attribute::StackProtectReq))
Bill Wendling606c8e32013-01-29 03:20:31 +0000156 return "sspreq";
Bill Wendling114baee2013-01-23 06:41:41 +0000157 if (hasAttribute(Attribute::StackProtectStrong))
Bill Wendling606c8e32013-01-29 03:20:31 +0000158 return "sspstrong";
Bill Wendling14292a62013-01-31 20:59:05 +0000159 if (hasAttribute(Attribute::StructRet))
160 return "sret";
161 if (hasAttribute(Attribute::UWTable))
162 return "uwtable";
163 if (hasAttribute(Attribute::ZExt))
164 return "zeroext";
165
166 // FIXME: These should be output like this:
167 //
168 // align=4
169 // alignstack=8
170 //
Bill Wendling034b94b2012-12-19 07:18:57 +0000171 if (hasAttribute(Attribute::StackAlignment)) {
Bill Wendling606c8e32013-01-29 03:20:31 +0000172 std::string Result;
Charles Davis1e063d12010-02-12 00:31:15 +0000173 Result += "alignstack(";
Bill Wendlingef99fe82012-09-21 15:26:31 +0000174 Result += utostr(getStackAlignment());
Bill Wendling606c8e32013-01-29 03:20:31 +0000175 Result += ")";
176 return Result;
Charles Davis1e063d12010-02-12 00:31:15 +0000177 }
Bill Wendling034b94b2012-12-19 07:18:57 +0000178 if (hasAttribute(Attribute::Alignment)) {
Bill Wendling606c8e32013-01-29 03:20:31 +0000179 std::string Result;
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000180 Result += "align ";
Bill Wendlingef99fe82012-09-21 15:26:31 +0000181 Result += utostr(getAlignment());
Bill Wendling606c8e32013-01-29 03:20:31 +0000182 return Result;
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000183 }
Bill Wendling14292a62013-01-31 20:59:05 +0000184
185 // Convert target-dependent attributes to strings of the form:
186 //
187 // "kind"
188 // "kind" = "value"
189 // "kind" = ("value1" "value2" "value3" )
190 //
191 if (ConstantDataArray *CDA =
192 dyn_cast<ConstantDataArray>(pImpl->getAttributeKind())) {
193 std::string Result;
194 Result += '\"' + CDA->getAsString().str() + '"';
195
196 ArrayRef<Constant*> Vals = pImpl->getAttributeValues();
197 if (Vals.empty()) return Result;
198 Result += " = ";
199 if (Vals.size() > 1) Result += '(';
200 for (ArrayRef<Constant*>::iterator I = Vals.begin(), E = Vals.end();
201 I != E; ) {
202 ConstantDataArray *CDA = cast<ConstantDataArray>(*I++);
203 Result += '\"' + CDA->getAsString().str() + '"';
204 if (I != E) Result += ' ';
205 }
206 if (Vals.size() > 1) Result += ')';
207 }
Bill Wendling606c8e32013-01-29 03:20:31 +0000208
209 llvm_unreachable("Unknown attribute");
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000210}
211
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000212bool Attribute::operator==(AttrKind K) const {
Bill Wendling14292a62013-01-31 20:59:05 +0000213 return (pImpl && *pImpl == K) || (!pImpl && K == None);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000214}
215bool Attribute::operator!=(AttrKind K) const {
216 return !(*this == K);
217}
218
219bool Attribute::operator<(Attribute A) const {
220 if (!pImpl && !A.pImpl) return false;
221 if (!pImpl) return true;
222 if (!A.pImpl) return false;
223 return *pImpl < *A.pImpl;
224}
225
226uint64_t Attribute::Raw() const {
227 return pImpl ? pImpl->Raw() : 0;
228}
229
230//===----------------------------------------------------------------------===//
231// AttributeImpl Definition
232//===----------------------------------------------------------------------===//
233
Bill Wendling9f175f82013-01-29 20:37:10 +0000234AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind kind)
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000235 : Context(C) {
Bill Wendling9f175f82013-01-29 20:37:10 +0000236 Kind = ConstantInt::get(Type::getInt64Ty(C), kind);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000237}
Bill Wendling9f175f82013-01-29 20:37:10 +0000238AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind kind,
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000239 ArrayRef<Constant*> values)
240 : Context(C) {
Bill Wendling9f175f82013-01-29 20:37:10 +0000241 Kind = ConstantInt::get(Type::getInt64Ty(C), kind);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000242 Vals.reserve(values.size());
243 Vals.append(values.begin(), values.end());
244}
Bill Wendling9f175f82013-01-29 20:37:10 +0000245AttributeImpl::AttributeImpl(LLVMContext &C, StringRef kind)
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000246 : Context(C) {
Bill Wendling9f175f82013-01-29 20:37:10 +0000247 Kind = ConstantDataArray::getString(C, kind);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000248}
249
250bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
Bill Wendling169d5272013-01-31 23:16:25 +0000251 if (ConstantInt *CI = dyn_cast<ConstantInt>(Kind))
252 return CI->getZExtValue() == A;
253 return false;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000254}
255
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000256uint64_t AttributeImpl::getAlignment() const {
Bill Wendling169d5272013-01-31 23:16:25 +0000257 assert(hasAttribute(Attribute::Alignment) &&
258 "Trying to retrieve the alignment from a non-alignment attr!");
259 return cast<ConstantInt>(Vals[0])->getZExtValue();
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000260}
261
262uint64_t AttributeImpl::getStackAlignment() const {
Bill Wendling169d5272013-01-31 23:16:25 +0000263 assert(hasAttribute(Attribute::StackAlignment) &&
264 "Trying to retrieve the stack alignment from a non-alignment attr!");
265 return cast<ConstantInt>(Vals[0])->getZExtValue();
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000266}
267
Bill Wendling9f175f82013-01-29 20:37:10 +0000268bool AttributeImpl::operator==(Attribute::AttrKind kind) const {
269 if (ConstantInt *CI = dyn_cast<ConstantInt>(Kind))
270 return CI->getZExtValue() == kind;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000271 return false;
272}
Bill Wendling9f175f82013-01-29 20:37:10 +0000273bool AttributeImpl::operator!=(Attribute::AttrKind kind) const {
274 return !(*this == kind);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000275}
276
Bill Wendling9f175f82013-01-29 20:37:10 +0000277bool AttributeImpl::operator==(StringRef kind) const {
278 if (ConstantDataArray *CDA = dyn_cast<ConstantDataArray>(Kind))
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000279 if (CDA->isString())
Bill Wendling9f175f82013-01-29 20:37:10 +0000280 return CDA->getAsString() == kind;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000281 return false;
282}
283
Bill Wendling9f175f82013-01-29 20:37:10 +0000284bool AttributeImpl::operator!=(StringRef kind) const {
285 return !(*this == kind);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000286}
287
288bool AttributeImpl::operator<(const AttributeImpl &AI) const {
Bill Wendling9f175f82013-01-29 20:37:10 +0000289 if (!Kind && !AI.Kind) return false;
290 if (!Kind && AI.Kind) return true;
291 if (Kind && !AI.Kind) return false;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000292
Bill Wendling9f175f82013-01-29 20:37:10 +0000293 ConstantInt *ThisCI = dyn_cast<ConstantInt>(Kind);
294 ConstantInt *ThatCI = dyn_cast<ConstantInt>(AI.Kind);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000295
Bill Wendling9f175f82013-01-29 20:37:10 +0000296 ConstantDataArray *ThisCDA = dyn_cast<ConstantDataArray>(Kind);
297 ConstantDataArray *ThatCDA = dyn_cast<ConstantDataArray>(AI.Kind);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000298
299 if (ThisCI && ThatCI)
300 return ThisCI->getZExtValue() < ThatCI->getZExtValue();
301
302 if (ThisCI && ThatCDA)
303 return true;
304
305 if (ThisCDA && ThatCI)
306 return false;
307
308 return ThisCDA->getAsString() < ThatCDA->getAsString();
309}
310
311uint64_t AttributeImpl::Raw() const {
312 // FIXME: Remove this.
Bill Wendling9f175f82013-01-29 20:37:10 +0000313 return cast<ConstantInt>(Kind)->getZExtValue();
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000314}
315
316uint64_t AttributeImpl::getAttrMask(Attribute::AttrKind Val) {
317 // FIXME: Remove this.
318 switch (Val) {
319 case Attribute::EndAttrKinds:
320 case Attribute::AttrKindEmptyKey:
321 case Attribute::AttrKindTombstoneKey:
322 llvm_unreachable("Synthetic enumerators which should never get here");
323
324 case Attribute::None: return 0;
325 case Attribute::ZExt: return 1 << 0;
326 case Attribute::SExt: return 1 << 1;
327 case Attribute::NoReturn: return 1 << 2;
328 case Attribute::InReg: return 1 << 3;
329 case Attribute::StructRet: return 1 << 4;
330 case Attribute::NoUnwind: return 1 << 5;
331 case Attribute::NoAlias: return 1 << 6;
332 case Attribute::ByVal: return 1 << 7;
333 case Attribute::Nest: return 1 << 8;
334 case Attribute::ReadNone: return 1 << 9;
335 case Attribute::ReadOnly: return 1 << 10;
336 case Attribute::NoInline: return 1 << 11;
337 case Attribute::AlwaysInline: return 1 << 12;
338 case Attribute::OptimizeForSize: return 1 << 13;
339 case Attribute::StackProtect: return 1 << 14;
340 case Attribute::StackProtectReq: return 1 << 15;
341 case Attribute::Alignment: return 31 << 16;
342 case Attribute::NoCapture: return 1 << 21;
343 case Attribute::NoRedZone: return 1 << 22;
344 case Attribute::NoImplicitFloat: return 1 << 23;
345 case Attribute::Naked: return 1 << 24;
346 case Attribute::InlineHint: return 1 << 25;
347 case Attribute::StackAlignment: return 7 << 26;
348 case Attribute::ReturnsTwice: return 1 << 29;
349 case Attribute::UWTable: return 1 << 30;
350 case Attribute::NonLazyBind: return 1U << 31;
351 case Attribute::AddressSafety: return 1ULL << 32;
352 case Attribute::MinSize: return 1ULL << 33;
353 case Attribute::NoDuplicate: return 1ULL << 34;
354 case Attribute::StackProtectStrong: return 1ULL << 35;
355 }
356 llvm_unreachable("Unsupported attribute type");
357}
358
359//===----------------------------------------------------------------------===//
360// AttributeSetNode Definition
361//===----------------------------------------------------------------------===//
362
363AttributeSetNode *AttributeSetNode::get(LLVMContext &C,
364 ArrayRef<Attribute> Attrs) {
365 if (Attrs.empty())
366 return 0;
367
368 // Otherwise, build a key to look up the existing attributes.
369 LLVMContextImpl *pImpl = C.pImpl;
370 FoldingSetNodeID ID;
371
372 SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end());
373 std::sort(SortedAttrs.begin(), SortedAttrs.end());
374
375 for (SmallVectorImpl<Attribute>::iterator I = SortedAttrs.begin(),
376 E = SortedAttrs.end(); I != E; ++I)
377 I->Profile(ID);
378
379 void *InsertPoint;
380 AttributeSetNode *PA =
381 pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint);
382
383 // If we didn't find any existing attributes of the same shape then create a
384 // new one and insert it.
385 if (!PA) {
386 PA = new AttributeSetNode(SortedAttrs);
387 pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint);
388 }
389
390 // Return the AttributesListNode that we found or created.
391 return PA;
392}
393
Bill Wendling606c8e32013-01-29 03:20:31 +0000394bool AttributeSetNode::hasAttribute(Attribute::AttrKind Kind) const {
395 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
396 E = AttrList.end(); I != E; ++I)
NAKAMURA Takumieddab152013-01-31 03:47:28 +0000397 if (I->hasAttribute(Kind))
Bill Wendling606c8e32013-01-29 03:20:31 +0000398 return true;
399 return false;
400}
401
402unsigned AttributeSetNode::getAlignment() const {
403 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
404 E = AttrList.end(); I != E; ++I)
NAKAMURA Takumieddab152013-01-31 03:47:28 +0000405 if (I->hasAttribute(Attribute::Alignment))
Bill Wendling606c8e32013-01-29 03:20:31 +0000406 return I->getAlignment();
407 return 0;
408}
409
410unsigned AttributeSetNode::getStackAlignment() const {
411 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
412 E = AttrList.end(); I != E; ++I)
NAKAMURA Takumieddab152013-01-31 03:47:28 +0000413 if (I->hasAttribute(Attribute::StackAlignment))
Bill Wendling606c8e32013-01-29 03:20:31 +0000414 return I->getStackAlignment();
415 return 0;
416}
417
418std::string AttributeSetNode::getAsString() const {
419 std::string Str = "";
420 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
421 E = AttrList.end(); I != E; ++I) {
422 if (I != AttrList.begin()) Str += " ";
423 Str += I->getAsString();
424 }
425 return Str;
426}
427
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000428//===----------------------------------------------------------------------===//
429// AttributeSetImpl Definition
430//===----------------------------------------------------------------------===//
431
432uint64_t AttributeSetImpl::Raw(uint64_t Index) const {
433 for (unsigned I = 0, E = getNumAttributes(); I != E; ++I) {
434 if (getSlotIndex(I) != Index) continue;
435 const AttributeSetNode *ASN = AttrNodes[I].second;
436 AttrBuilder B;
437
438 for (AttributeSetNode::const_iterator II = ASN->begin(),
439 IE = ASN->end(); II != IE; ++II)
440 B.addAttributes(*II);
441 return B.Raw();
442 }
443
444 return 0;
445}
446
447//===----------------------------------------------------------------------===//
448// AttributeSet Construction and Mutation Methods
449//===----------------------------------------------------------------------===//
450
Bill Wendling8232ece2013-01-29 01:43:29 +0000451AttributeSet
452AttributeSet::getImpl(LLVMContext &C,
453 ArrayRef<std::pair<unsigned, AttributeSetNode*> > Attrs) {
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000454 LLVMContextImpl *pImpl = C.pImpl;
455 FoldingSetNodeID ID;
456 AttributeSetImpl::Profile(ID, Attrs);
457
458 void *InsertPoint;
459 AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
460
461 // If we didn't find any existing attributes of the same shape then
462 // create a new one and insert it.
463 if (!PA) {
464 PA = new AttributeSetImpl(C, Attrs);
465 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
466 }
467
468 // Return the AttributesList that we found or created.
469 return AttributeSet(PA);
470}
471
472AttributeSet AttributeSet::get(LLVMContext &C,
473 ArrayRef<std::pair<unsigned, Attribute> > Attrs){
474 // If there are no attributes then return a null AttributesList pointer.
475 if (Attrs.empty())
476 return AttributeSet();
477
478#ifndef NDEBUG
479 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
480 assert((!i || Attrs[i-1].first <= Attrs[i].first) &&
481 "Misordered Attributes list!");
Bill Wendling82aea642013-01-31 06:22:35 +0000482 assert(Attrs[i].second != Attribute::None &&
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000483 "Pointless attribute!");
484 }
485#endif
486
487 // Create a vector if (unsigned, AttributeSetNode*) pairs from the attributes
488 // list.
489 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrPairVec;
490 for (ArrayRef<std::pair<unsigned, Attribute> >::iterator I = Attrs.begin(),
491 E = Attrs.end(); I != E; ) {
492 unsigned Index = I->first;
493 SmallVector<Attribute, 4> AttrVec;
NAKAMURA Takumi3ba51ce2013-01-29 15:18:16 +0000494 while (I != E && I->first == Index) {
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000495 AttrVec.push_back(I->second);
496 ++I;
497 }
498
499 AttrPairVec.push_back(std::make_pair(Index,
500 AttributeSetNode::get(C, AttrVec)));
501 }
502
503 return getImpl(C, AttrPairVec);
504}
505
506AttributeSet AttributeSet::get(LLVMContext &C,
507 ArrayRef<std::pair<unsigned,
508 AttributeSetNode*> > Attrs) {
509 // If there are no attributes then return a null AttributesList pointer.
510 if (Attrs.empty())
511 return AttributeSet();
512
513 return getImpl(C, Attrs);
514}
515
516AttributeSet AttributeSet::get(LLVMContext &C, unsigned Idx, AttrBuilder &B) {
517 if (!B.hasAttributes())
518 return AttributeSet();
Bill Wendling8fbc0c22013-01-29 01:02:03 +0000519
520 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
521 for (AttrBuilder::iterator I = B.begin(), E = B.end(); I != E; ++I) {
522 Attribute::AttrKind Kind = *I;
523 if (Kind == Attribute::Alignment)
524 Attrs.push_back(std::make_pair(Idx, Attribute::
525 getWithAlignment(C, B.getAlignment())));
526 else if (Kind == Attribute::StackAlignment)
527 Attrs.push_back(std::make_pair(Idx, Attribute::
528 getWithStackAlignment(C, B.getStackAlignment())));
529 else
530 Attrs.push_back(std::make_pair(Idx, Attribute::get(C, Kind)));
531 }
532
533 return get(C, Attrs);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000534}
535
536AttributeSet AttributeSet::get(LLVMContext &C, unsigned Idx,
537 ArrayRef<Attribute::AttrKind> Kind) {
538 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
539 for (ArrayRef<Attribute::AttrKind>::iterator I = Kind.begin(),
540 E = Kind.end(); I != E; ++I)
541 Attrs.push_back(std::make_pair(Idx, Attribute::get(C, *I)));
542 return get(C, Attrs);
543}
544
545AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<AttributeSet> Attrs) {
546 if (Attrs.empty()) return AttributeSet();
547
548 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrNodeVec;
549 for (unsigned I = 0, E = Attrs.size(); I != E; ++I) {
550 AttributeSet AS = Attrs[I];
551 if (!AS.pImpl) continue;
552 AttrNodeVec.append(AS.pImpl->AttrNodes.begin(), AS.pImpl->AttrNodes.end());
553 }
554
555 return getImpl(C, AttrNodeVec);
556}
557
558AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Idx,
559 Attribute::AttrKind Attr) const {
560 return addAttributes(C, Idx, AttributeSet::get(C, Idx, Attr));
561}
562
563AttributeSet AttributeSet::addAttributes(LLVMContext &C, unsigned Idx,
564 AttributeSet Attrs) const {
565 if (!pImpl) return Attrs;
566 if (!Attrs.pImpl) return *this;
567
568#ifndef NDEBUG
569 // FIXME it is not obvious how this should work for alignment. For now, say
570 // we can't change a known alignment.
571 unsigned OldAlign = getParamAlignment(Idx);
572 unsigned NewAlign = Attrs.getParamAlignment(Idx);
573 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
574 "Attempt to change alignment!");
575#endif
576
577 // Add the attribute slots before the one we're trying to add.
578 SmallVector<AttributeSet, 4> AttrSet;
579 uint64_t NumAttrs = pImpl->getNumAttributes();
580 AttributeSet AS;
581 uint64_t LastIndex = 0;
582 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
583 if (getSlotIndex(I) >= Idx) {
584 if (getSlotIndex(I) == Idx) AS = getSlotAttributes(LastIndex++);
585 break;
586 }
587 LastIndex = I + 1;
588 AttrSet.push_back(getSlotAttributes(I));
589 }
590
591 // Now add the attribute into the correct slot. There may already be an
592 // AttributeSet there.
593 AttrBuilder B(AS, Idx);
594
595 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
596 if (Attrs.getSlotIndex(I) == Idx) {
597 for (AttributeSetImpl::const_iterator II = Attrs.pImpl->begin(I),
598 IE = Attrs.pImpl->end(I); II != IE; ++II)
599 B.addAttributes(*II);
600 break;
601 }
602
603 AttrSet.push_back(AttributeSet::get(C, Idx, B));
604
605 // Add the remaining attribute slots.
606 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
607 AttrSet.push_back(getSlotAttributes(I));
608
609 return get(C, AttrSet);
610}
611
612AttributeSet AttributeSet::removeAttribute(LLVMContext &C, unsigned Idx,
613 Attribute::AttrKind Attr) const {
614 return removeAttributes(C, Idx, AttributeSet::get(C, Idx, Attr));
615}
616
617AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Idx,
618 AttributeSet Attrs) const {
619 if (!pImpl) return AttributeSet();
620 if (!Attrs.pImpl) return *this;
621
622#ifndef NDEBUG
623 // FIXME it is not obvious how this should work for alignment.
624 // For now, say we can't pass in alignment, which no current use does.
625 assert(!Attrs.hasAttribute(Idx, Attribute::Alignment) &&
626 "Attempt to change alignment!");
627#endif
628
629 // Add the attribute slots before the one we're trying to add.
630 SmallVector<AttributeSet, 4> AttrSet;
631 uint64_t NumAttrs = pImpl->getNumAttributes();
632 AttributeSet AS;
633 uint64_t LastIndex = 0;
634 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
635 if (getSlotIndex(I) >= Idx) {
636 if (getSlotIndex(I) == Idx) AS = getSlotAttributes(LastIndex++);
637 break;
638 }
639 LastIndex = I + 1;
640 AttrSet.push_back(getSlotAttributes(I));
641 }
642
Bill Wendlinge7436542013-01-30 23:07:40 +0000643 // Now remove the attribute from the correct slot. There may already be an
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000644 // AttributeSet there.
645 AttrBuilder B(AS, Idx);
646
647 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
648 if (Attrs.getSlotIndex(I) == Idx) {
Bill Wendlinge7436542013-01-30 23:07:40 +0000649 B.removeAttributes(Attrs.pImpl->getSlotAttributes(I), Idx);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000650 break;
651 }
652
653 AttrSet.push_back(AttributeSet::get(C, Idx, B));
654
655 // Add the remaining attribute slots.
656 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
657 AttrSet.push_back(getSlotAttributes(I));
658
659 return get(C, AttrSet);
660}
661
662//===----------------------------------------------------------------------===//
663// AttributeSet Accessor Methods
664//===----------------------------------------------------------------------===//
665
666AttributeSet AttributeSet::getParamAttributes(unsigned Idx) const {
667 return pImpl && hasAttributes(Idx) ?
668 AttributeSet::get(pImpl->getContext(),
Bill Wendling606c8e32013-01-29 03:20:31 +0000669 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000670 std::make_pair(Idx, getAttributes(Idx)))) :
671 AttributeSet();
672}
673
674AttributeSet AttributeSet::getRetAttributes() const {
675 return pImpl && hasAttributes(ReturnIndex) ?
676 AttributeSet::get(pImpl->getContext(),
Bill Wendling606c8e32013-01-29 03:20:31 +0000677 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000678 std::make_pair(ReturnIndex,
679 getAttributes(ReturnIndex)))) :
680 AttributeSet();
681}
682
683AttributeSet AttributeSet::getFnAttributes() const {
684 return pImpl && hasAttributes(FunctionIndex) ?
685 AttributeSet::get(pImpl->getContext(),
Bill Wendling606c8e32013-01-29 03:20:31 +0000686 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000687 std::make_pair(FunctionIndex,
688 getAttributes(FunctionIndex)))) :
689 AttributeSet();
690}
691
692bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{
Bill Wendling606c8e32013-01-29 03:20:31 +0000693 AttributeSetNode *ASN = getAttributes(Index);
694 return ASN ? ASN->hasAttribute(Kind) : false;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000695}
696
697bool AttributeSet::hasAttributes(unsigned Index) const {
Bill Wendling606c8e32013-01-29 03:20:31 +0000698 AttributeSetNode *ASN = getAttributes(Index);
699 return ASN ? ASN->hasAttributes() : false;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000700}
701
702/// \brief Return true if the specified attribute is set for at least one
703/// parameter or for the return value.
704bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr) const {
705 if (pImpl == 0) return false;
706
707 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
708 for (AttributeSetImpl::const_iterator II = pImpl->begin(I),
709 IE = pImpl->end(I); II != IE; ++II)
NAKAMURA Takumieddab152013-01-31 03:47:28 +0000710 if (II->hasAttribute(Attr))
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000711 return true;
712
713 return false;
714}
715
Bill Wendling606c8e32013-01-29 03:20:31 +0000716unsigned AttributeSet::getParamAlignment(unsigned Index) const {
717 AttributeSetNode *ASN = getAttributes(Index);
718 return ASN ? ASN->getAlignment() : 0;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000719}
720
721unsigned AttributeSet::getStackAlignment(unsigned Index) const {
Bill Wendling606c8e32013-01-29 03:20:31 +0000722 AttributeSetNode *ASN = getAttributes(Index);
723 return ASN ? ASN->getStackAlignment() : 0;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000724}
725
726std::string AttributeSet::getAsString(unsigned Index) const {
Bill Wendling606c8e32013-01-29 03:20:31 +0000727 AttributeSetNode *ASN = getAttributes(Index);
728 return ASN ? ASN->getAsString() : std::string("");
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000729}
730
731/// \brief The attributes for the specified index are returned.
Bill Wendling606c8e32013-01-29 03:20:31 +0000732AttributeSetNode *AttributeSet::getAttributes(unsigned Idx) const {
733 if (!pImpl) return 0;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000734
Bill Wendling606c8e32013-01-29 03:20:31 +0000735 // Loop through to find the attribute node we want.
736 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
737 if (pImpl->getSlotIndex(I) == Idx)
738 return pImpl->getSlotNode(I);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000739
Bill Wendling606c8e32013-01-29 03:20:31 +0000740 return 0;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000741}
742
743//===----------------------------------------------------------------------===//
744// AttributeSet Introspection Methods
745//===----------------------------------------------------------------------===//
746
747/// \brief Return the number of slots used in this attribute list. This is the
748/// number of arguments that have an attribute set on them (including the
749/// function itself).
750unsigned AttributeSet::getNumSlots() const {
751 return pImpl ? pImpl->getNumAttributes() : 0;
752}
753
754uint64_t AttributeSet::getSlotIndex(unsigned Slot) const {
755 assert(pImpl && Slot < pImpl->getNumAttributes() &&
756 "Slot # out of range!");
757 return pImpl->getSlotIndex(Slot);
758}
759
760AttributeSet AttributeSet::getSlotAttributes(unsigned Slot) const {
761 assert(pImpl && Slot < pImpl->getNumAttributes() &&
762 "Slot # out of range!");
763 return pImpl->getSlotAttributes(Slot);
764}
765
766uint64_t AttributeSet::Raw(unsigned Index) const {
767 // FIXME: Remove this.
768 return pImpl ? pImpl->Raw(Index) : 0;
769}
770
771void AttributeSet::dump() const {
772 dbgs() << "PAL[\n";
773
774 for (unsigned i = 0, e = getNumSlots(); i < e; ++i) {
775 uint64_t Index = getSlotIndex(i);
776 dbgs() << " { ";
777 if (Index == ~0U)
778 dbgs() << "~0U";
779 else
780 dbgs() << Index;
781 dbgs() << " => " << getAsString(Index) << " }\n";
782 }
783
784 dbgs() << "]\n";
785}
786
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000787//===----------------------------------------------------------------------===//
Bill Wendling03198882013-01-04 23:27:34 +0000788// AttrBuilder Method Implementations
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000789//===----------------------------------------------------------------------===//
790
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000791AttrBuilder::AttrBuilder(AttributeSet AS, unsigned Idx)
792 : Alignment(0), StackAlignment(0) {
Bill Wendlingec258982013-01-27 21:23:46 +0000793 AttributeSetImpl *pImpl = AS.pImpl;
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000794 if (!pImpl) return;
795
Bill Wendling73bc4522013-01-28 00:21:34 +0000796 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I) {
797 if (pImpl->getSlotIndex(I) != Idx) continue;
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000798
Bill Wendling383da6b2013-01-30 21:22:59 +0000799 for (AttributeSetImpl::const_iterator II = pImpl->begin(I),
Bill Wendling73bc4522013-01-28 00:21:34 +0000800 IE = pImpl->end(I); II != IE; ++II)
Bill Wendling383da6b2013-01-30 21:22:59 +0000801 addAttributes(*II);
Bill Wendling73bc4522013-01-28 00:21:34 +0000802
803 break;
804 }
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000805}
806
Bill Wendling03198882013-01-04 23:27:34 +0000807void AttrBuilder::clear() {
808 Attrs.clear();
809 Alignment = StackAlignment = 0;
810}
811
812AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
Bill Wendling169d5272013-01-31 23:16:25 +0000813 assert(Val != Attribute::Alignment && Val != Attribute::StackAlignment &&
814 "Adding alignment attribute without adding alignment value!");
Bill Wendling03198882013-01-04 23:27:34 +0000815 Attrs.insert(Val);
Bill Wendling3a106e62012-10-09 19:01:18 +0000816 return *this;
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000817}
818
Bill Wendling03198882013-01-04 23:27:34 +0000819AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
820 Attrs.erase(Val);
Bill Wendling169d5272013-01-31 23:16:25 +0000821
Bill Wendling03198882013-01-04 23:27:34 +0000822 if (Val == Attribute::Alignment)
823 Alignment = 0;
824 else if (Val == Attribute::StackAlignment)
825 StackAlignment = 0;
826
Bill Wendlinga19a5302012-10-14 04:10:01 +0000827 return *this;
828}
829
Bill Wendling49f60602013-01-28 05:23:28 +0000830AttrBuilder &AttrBuilder::addAttributes(Attribute Attr) {
Bill Wendling169d5272013-01-31 23:16:25 +0000831 ConstantInt *Kind = cast<ConstantInt>(Attr.getAttributeKind());
832 Attribute::AttrKind KindVal = Attribute::AttrKind(Kind->getZExtValue());
833 Attrs.insert(KindVal);
Bill Wendling49f60602013-01-28 05:23:28 +0000834
Bill Wendling169d5272013-01-31 23:16:25 +0000835 if (KindVal == Attribute::Alignment)
Bill Wendling49f60602013-01-28 05:23:28 +0000836 Alignment = Attr.getAlignment();
Bill Wendling169d5272013-01-31 23:16:25 +0000837 else if (KindVal == Attribute::StackAlignment)
Bill Wendling49f60602013-01-28 05:23:28 +0000838 StackAlignment = Attr.getStackAlignment();
839 return *this;
840}
841
Bill Wendlinge7436542013-01-30 23:07:40 +0000842AttrBuilder &AttrBuilder::removeAttributes(AttributeSet A, uint64_t Index) {
843 uint64_t Mask = A.Raw(Index);
Bill Wendling49f60602013-01-28 05:23:28 +0000844
845 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
846 I = Attribute::AttrKind(I + 1)) {
847 if (Mask & AttributeImpl::getAttrMask(I)) {
848 Attrs.erase(I);
849
850 if (I == Attribute::Alignment)
851 Alignment = 0;
852 else if (I == Attribute::StackAlignment)
853 StackAlignment = 0;
854 }
855 }
856
857 return *this;
858}
859
Bill Wendling702cc912012-10-15 20:35:56 +0000860AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
Bill Wendlingda3f9d82012-10-14 03:58:29 +0000861 if (Align == 0) return *this;
Bill Wendling03198882013-01-04 23:27:34 +0000862
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000863 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
864 assert(Align <= 0x40000000 && "Alignment too large.");
Bill Wendling03198882013-01-04 23:27:34 +0000865
866 Attrs.insert(Attribute::Alignment);
867 Alignment = Align;
Bill Wendlingda3f9d82012-10-14 03:58:29 +0000868 return *this;
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000869}
870
Bill Wendling03198882013-01-04 23:27:34 +0000871AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
872 // Default alignment, allow the target to define how to align it.
873 if (Align == 0) return *this;
874
875 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
876 assert(Align <= 0x100 && "Alignment too large.");
877
878 Attrs.insert(Attribute::StackAlignment);
879 StackAlignment = Align;
880 return *this;
881}
882
Bill Wendling22bd6412013-01-03 01:54:39 +0000883bool AttrBuilder::contains(Attribute::AttrKind A) const {
Bill Wendling03198882013-01-04 23:27:34 +0000884 return Attrs.count(A);
Bill Wendling7d2f2492012-10-10 07:36:45 +0000885}
886
Bill Wendling702cc912012-10-15 20:35:56 +0000887bool AttrBuilder::hasAttributes() const {
Bill Wendling03198882013-01-04 23:27:34 +0000888 return !Attrs.empty();
Bill Wendlingf385f4c2012-10-08 23:27:46 +0000889}
Bill Wendling60507d52013-01-04 20:54:35 +0000890
Bill Wendlinge7436542013-01-30 23:07:40 +0000891bool AttrBuilder::hasAttributes(AttributeSet A, uint64_t Index) const {
892 return Raw() & A.Raw(Index);
Bill Wendling8831c062012-10-09 00:01:21 +0000893}
Bill Wendling60507d52013-01-04 20:54:35 +0000894
Bill Wendling702cc912012-10-15 20:35:56 +0000895bool AttrBuilder::hasAlignmentAttr() const {
Bill Wendling03198882013-01-04 23:27:34 +0000896 return Alignment != 0;
Bill Wendlingf385f4c2012-10-08 23:27:46 +0000897}
898
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000899bool AttrBuilder::operator==(const AttrBuilder &B) {
900 SmallVector<Attribute::AttrKind, 8> This(Attrs.begin(), Attrs.end());
901 SmallVector<Attribute::AttrKind, 8> That(B.Attrs.begin(), B.Attrs.end());
902 return This == That;
903}
904
905AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) {
Bill Wendling8232ece2013-01-29 01:43:29 +0000906 if (!Val) return *this;
907
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000908 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
909 I = Attribute::AttrKind(I + 1)) {
910 if (uint64_t A = (Val & AttributeImpl::getAttrMask(I))) {
911 Attrs.insert(I);
912
913 if (I == Attribute::Alignment)
914 Alignment = 1ULL << ((A >> 16) - 1);
915 else if (I == Attribute::StackAlignment)
916 StackAlignment = 1ULL << ((A >> 26)-1);
917 }
918 }
919
920 return *this;
921}
922
Bill Wendling1db9b692013-01-09 23:36:50 +0000923uint64_t AttrBuilder::Raw() const {
Bill Wendling03198882013-01-04 23:27:34 +0000924 uint64_t Mask = 0;
925
926 for (DenseSet<Attribute::AttrKind>::const_iterator I = Attrs.begin(),
927 E = Attrs.end(); I != E; ++I) {
928 Attribute::AttrKind Kind = *I;
929
930 if (Kind == Attribute::Alignment)
931 Mask |= (Log2_32(Alignment) + 1) << 16;
932 else if (Kind == Attribute::StackAlignment)
933 Mask |= (Log2_32(StackAlignment) + 1) << 26;
934 else
935 Mask |= AttributeImpl::getAttrMask(Kind);
936 }
937
938 return Mask;
Bill Wendlingf385f4c2012-10-08 23:27:46 +0000939}
940
Bill Wendling8e47daf2013-01-25 23:09:36 +0000941//===----------------------------------------------------------------------===//
942// AttributeFuncs Function Defintions
943//===----------------------------------------------------------------------===//
944
Bill Wendlinge7436542013-01-30 23:07:40 +0000945AttributeSet AttributeFuncs::typeIncompatible(Type *Ty, uint64_t Index) {
Bill Wendling8e47daf2013-01-25 23:09:36 +0000946 AttrBuilder Incompatible;
947
948 if (!Ty->isIntegerTy())
949 // Attribute that only apply to integers.
950 Incompatible.addAttribute(Attribute::SExt)
951 .addAttribute(Attribute::ZExt);
952
953 if (!Ty->isPointerTy())
954 // Attribute that only apply to pointers.
955 Incompatible.addAttribute(Attribute::ByVal)
956 .addAttribute(Attribute::Nest)
957 .addAttribute(Attribute::NoAlias)
958 .addAttribute(Attribute::NoCapture)
959 .addAttribute(Attribute::StructRet);
960
Bill Wendlinge7436542013-01-30 23:07:40 +0000961 return AttributeSet::get(Ty->getContext(), Index, Incompatible);
Bill Wendling8e47daf2013-01-25 23:09:36 +0000962}
963
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000964/// \brief This returns an integer containing an encoding of all the LLVM
965/// attributes found in the given attribute bitset. Any change to this encoding
966/// is a breaking change to bitcode compatibility.
Bill Wendling8232ece2013-01-29 01:43:29 +0000967/// N.B. This should be used only by the bitcode reader!
Bill Wendling8e47daf2013-01-25 23:09:36 +0000968uint64_t AttributeFuncs::encodeLLVMAttributesForBitcode(AttributeSet Attrs,
969 unsigned Index) {
970 // FIXME: It doesn't make sense to store the alignment information as an
971 // expanded out value, we should store it as a log2 value. However, we can't
972 // just change that here without breaking bitcode compatibility. If this ever
973 // becomes a problem in practice, we should introduce new tag numbers in the
974 // bitcode file and have those tags use a more efficiently encoded alignment
975 // field.
976
977 // Store the alignment in the bitcode as a 16-bit raw value instead of a 5-bit
978 // log2 encoded value. Shift the bits above the alignment up by 11 bits.
979 uint64_t EncodedAttrs = Attrs.Raw(Index) & 0xffff;
980 if (Attrs.hasAttribute(Index, Attribute::Alignment))
981 EncodedAttrs |= Attrs.getParamAlignment(Index) << 16;
982 EncodedAttrs |= (Attrs.Raw(Index) & (0xffffULL << 21)) << 11;
983 return EncodedAttrs;
984}
985
Bill Wendling8232ece2013-01-29 01:43:29 +0000986/// \brief This fills an AttrBuilder object with the LLVM attributes that have
987/// been decoded from the given integer. This function must stay in sync with
988/// 'encodeLLVMAttributesForBitcode'.
989/// N.B. This should be used only by the bitcode reader!
990void AttributeFuncs::decodeLLVMAttributesForBitcode(LLVMContext &C,
991 AttrBuilder &B,
992 uint64_t EncodedAttrs) {
Bill Wendling8e47daf2013-01-25 23:09:36 +0000993 // The alignment is stored as a 16-bit raw value from bits 31--16. We shift
994 // the bits above 31 down by 11 bits.
995 unsigned Alignment = (EncodedAttrs & (0xffffULL << 16)) >> 16;
996 assert((!Alignment || isPowerOf2_32(Alignment)) &&
997 "Alignment must be a power of two.");
998
Bill Wendling8e47daf2013-01-25 23:09:36 +0000999 if (Alignment)
1000 B.addAlignmentAttr(Alignment);
Bill Wendling606c8e32013-01-29 03:20:31 +00001001 B.addRawValue(((EncodedAttrs & (0xffffULL << 32)) >> 11) |
1002 (EncodedAttrs & 0xffff));
Bill Wendling8e47daf2013-01-25 23:09:36 +00001003}