blob: 68b831d554d4a5030529f55e5f4d88df6d231e7f [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
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000226//===----------------------------------------------------------------------===//
227// AttributeImpl Definition
228//===----------------------------------------------------------------------===//
229
Bill Wendling9f175f82013-01-29 20:37:10 +0000230AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind kind)
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000231 : Context(C) {
Bill Wendling9f175f82013-01-29 20:37:10 +0000232 Kind = ConstantInt::get(Type::getInt64Ty(C), kind);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000233}
Bill Wendling9f175f82013-01-29 20:37:10 +0000234AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind kind,
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000235 ArrayRef<Constant*> values)
236 : Context(C) {
Bill Wendling9f175f82013-01-29 20:37:10 +0000237 Kind = ConstantInt::get(Type::getInt64Ty(C), kind);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000238 Vals.reserve(values.size());
239 Vals.append(values.begin(), values.end());
240}
Bill Wendling9f175f82013-01-29 20:37:10 +0000241AttributeImpl::AttributeImpl(LLVMContext &C, StringRef kind)
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000242 : Context(C) {
Bill Wendling9f175f82013-01-29 20:37:10 +0000243 Kind = ConstantDataArray::getString(C, kind);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000244}
245
246bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
Bill Wendling169d5272013-01-31 23:16:25 +0000247 if (ConstantInt *CI = dyn_cast<ConstantInt>(Kind))
248 return CI->getZExtValue() == A;
249 return false;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000250}
251
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000252uint64_t AttributeImpl::getAlignment() const {
Bill Wendling169d5272013-01-31 23:16:25 +0000253 assert(hasAttribute(Attribute::Alignment) &&
254 "Trying to retrieve the alignment from a non-alignment attr!");
255 return cast<ConstantInt>(Vals[0])->getZExtValue();
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000256}
257
258uint64_t AttributeImpl::getStackAlignment() const {
Bill Wendling169d5272013-01-31 23:16:25 +0000259 assert(hasAttribute(Attribute::StackAlignment) &&
260 "Trying to retrieve the stack alignment from a non-alignment attr!");
261 return cast<ConstantInt>(Vals[0])->getZExtValue();
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000262}
263
Bill Wendling9f175f82013-01-29 20:37:10 +0000264bool AttributeImpl::operator==(Attribute::AttrKind kind) const {
265 if (ConstantInt *CI = dyn_cast<ConstantInt>(Kind))
266 return CI->getZExtValue() == kind;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000267 return false;
268}
Bill Wendling9f175f82013-01-29 20:37:10 +0000269bool AttributeImpl::operator!=(Attribute::AttrKind kind) const {
270 return !(*this == kind);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000271}
272
Bill Wendling9f175f82013-01-29 20:37:10 +0000273bool AttributeImpl::operator==(StringRef kind) const {
274 if (ConstantDataArray *CDA = dyn_cast<ConstantDataArray>(Kind))
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000275 if (CDA->isString())
Bill Wendling9f175f82013-01-29 20:37:10 +0000276 return CDA->getAsString() == kind;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000277 return false;
278}
279
Bill Wendling9f175f82013-01-29 20:37:10 +0000280bool AttributeImpl::operator!=(StringRef kind) const {
281 return !(*this == kind);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000282}
283
284bool AttributeImpl::operator<(const AttributeImpl &AI) const {
Bill Wendling9f175f82013-01-29 20:37:10 +0000285 if (!Kind && !AI.Kind) return false;
286 if (!Kind && AI.Kind) return true;
287 if (Kind && !AI.Kind) return false;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000288
Bill Wendling9f175f82013-01-29 20:37:10 +0000289 ConstantInt *ThisCI = dyn_cast<ConstantInt>(Kind);
290 ConstantInt *ThatCI = dyn_cast<ConstantInt>(AI.Kind);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000291
Bill Wendling9f175f82013-01-29 20:37:10 +0000292 ConstantDataArray *ThisCDA = dyn_cast<ConstantDataArray>(Kind);
293 ConstantDataArray *ThatCDA = dyn_cast<ConstantDataArray>(AI.Kind);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000294
295 if (ThisCI && ThatCI)
296 return ThisCI->getZExtValue() < ThatCI->getZExtValue();
297
298 if (ThisCI && ThatCDA)
299 return true;
300
301 if (ThisCDA && ThatCI)
302 return false;
303
304 return ThisCDA->getAsString() < ThatCDA->getAsString();
305}
306
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000307uint64_t AttributeImpl::getAttrMask(Attribute::AttrKind Val) {
308 // FIXME: Remove this.
309 switch (Val) {
310 case Attribute::EndAttrKinds:
311 case Attribute::AttrKindEmptyKey:
312 case Attribute::AttrKindTombstoneKey:
313 llvm_unreachable("Synthetic enumerators which should never get here");
314
315 case Attribute::None: return 0;
316 case Attribute::ZExt: return 1 << 0;
317 case Attribute::SExt: return 1 << 1;
318 case Attribute::NoReturn: return 1 << 2;
319 case Attribute::InReg: return 1 << 3;
320 case Attribute::StructRet: return 1 << 4;
321 case Attribute::NoUnwind: return 1 << 5;
322 case Attribute::NoAlias: return 1 << 6;
323 case Attribute::ByVal: return 1 << 7;
324 case Attribute::Nest: return 1 << 8;
325 case Attribute::ReadNone: return 1 << 9;
326 case Attribute::ReadOnly: return 1 << 10;
327 case Attribute::NoInline: return 1 << 11;
328 case Attribute::AlwaysInline: return 1 << 12;
329 case Attribute::OptimizeForSize: return 1 << 13;
330 case Attribute::StackProtect: return 1 << 14;
331 case Attribute::StackProtectReq: return 1 << 15;
332 case Attribute::Alignment: return 31 << 16;
333 case Attribute::NoCapture: return 1 << 21;
334 case Attribute::NoRedZone: return 1 << 22;
335 case Attribute::NoImplicitFloat: return 1 << 23;
336 case Attribute::Naked: return 1 << 24;
337 case Attribute::InlineHint: return 1 << 25;
338 case Attribute::StackAlignment: return 7 << 26;
339 case Attribute::ReturnsTwice: return 1 << 29;
340 case Attribute::UWTable: return 1 << 30;
341 case Attribute::NonLazyBind: return 1U << 31;
342 case Attribute::AddressSafety: return 1ULL << 32;
343 case Attribute::MinSize: return 1ULL << 33;
344 case Attribute::NoDuplicate: return 1ULL << 34;
345 case Attribute::StackProtectStrong: return 1ULL << 35;
346 }
347 llvm_unreachable("Unsupported attribute type");
348}
349
350//===----------------------------------------------------------------------===//
351// AttributeSetNode Definition
352//===----------------------------------------------------------------------===//
353
354AttributeSetNode *AttributeSetNode::get(LLVMContext &C,
355 ArrayRef<Attribute> Attrs) {
356 if (Attrs.empty())
357 return 0;
358
359 // Otherwise, build a key to look up the existing attributes.
360 LLVMContextImpl *pImpl = C.pImpl;
361 FoldingSetNodeID ID;
362
363 SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end());
364 std::sort(SortedAttrs.begin(), SortedAttrs.end());
365
366 for (SmallVectorImpl<Attribute>::iterator I = SortedAttrs.begin(),
367 E = SortedAttrs.end(); I != E; ++I)
368 I->Profile(ID);
369
370 void *InsertPoint;
371 AttributeSetNode *PA =
372 pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint);
373
374 // If we didn't find any existing attributes of the same shape then create a
375 // new one and insert it.
376 if (!PA) {
377 PA = new AttributeSetNode(SortedAttrs);
378 pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint);
379 }
380
381 // Return the AttributesListNode that we found or created.
382 return PA;
383}
384
Bill Wendling606c8e32013-01-29 03:20:31 +0000385bool AttributeSetNode::hasAttribute(Attribute::AttrKind Kind) 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(Kind))
Bill Wendling606c8e32013-01-29 03:20:31 +0000389 return true;
390 return false;
391}
392
393unsigned AttributeSetNode::getAlignment() const {
394 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
395 E = AttrList.end(); I != E; ++I)
NAKAMURA Takumieddab152013-01-31 03:47:28 +0000396 if (I->hasAttribute(Attribute::Alignment))
Bill Wendling606c8e32013-01-29 03:20:31 +0000397 return I->getAlignment();
398 return 0;
399}
400
401unsigned AttributeSetNode::getStackAlignment() const {
402 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
403 E = AttrList.end(); I != E; ++I)
NAKAMURA Takumieddab152013-01-31 03:47:28 +0000404 if (I->hasAttribute(Attribute::StackAlignment))
Bill Wendling606c8e32013-01-29 03:20:31 +0000405 return I->getStackAlignment();
406 return 0;
407}
408
409std::string AttributeSetNode::getAsString() const {
410 std::string Str = "";
411 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
412 E = AttrList.end(); I != E; ++I) {
413 if (I != AttrList.begin()) Str += " ";
414 Str += I->getAsString();
415 }
416 return Str;
417}
418
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000419//===----------------------------------------------------------------------===//
420// AttributeSetImpl Definition
421//===----------------------------------------------------------------------===//
422
423uint64_t AttributeSetImpl::Raw(uint64_t Index) const {
424 for (unsigned I = 0, E = getNumAttributes(); I != E; ++I) {
425 if (getSlotIndex(I) != Index) continue;
426 const AttributeSetNode *ASN = AttrNodes[I].second;
427 AttrBuilder B;
428
429 for (AttributeSetNode::const_iterator II = ASN->begin(),
430 IE = ASN->end(); II != IE; ++II)
Bill Wendling39da0782013-01-31 23:38:01 +0000431 B.addAttribute(*II);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000432 return B.Raw();
433 }
434
435 return 0;
436}
437
438//===----------------------------------------------------------------------===//
439// AttributeSet Construction and Mutation Methods
440//===----------------------------------------------------------------------===//
441
Bill Wendling8232ece2013-01-29 01:43:29 +0000442AttributeSet
443AttributeSet::getImpl(LLVMContext &C,
444 ArrayRef<std::pair<unsigned, AttributeSetNode*> > Attrs) {
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000445 LLVMContextImpl *pImpl = C.pImpl;
446 FoldingSetNodeID ID;
447 AttributeSetImpl::Profile(ID, Attrs);
448
449 void *InsertPoint;
450 AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
451
452 // If we didn't find any existing attributes of the same shape then
453 // create a new one and insert it.
454 if (!PA) {
455 PA = new AttributeSetImpl(C, Attrs);
456 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
457 }
458
459 // Return the AttributesList that we found or created.
460 return AttributeSet(PA);
461}
462
463AttributeSet AttributeSet::get(LLVMContext &C,
464 ArrayRef<std::pair<unsigned, Attribute> > Attrs){
465 // If there are no attributes then return a null AttributesList pointer.
466 if (Attrs.empty())
467 return AttributeSet();
468
469#ifndef NDEBUG
470 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
471 assert((!i || Attrs[i-1].first <= Attrs[i].first) &&
472 "Misordered Attributes list!");
Bill Wendling82aea642013-01-31 06:22:35 +0000473 assert(Attrs[i].second != Attribute::None &&
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000474 "Pointless attribute!");
475 }
476#endif
477
478 // Create a vector if (unsigned, AttributeSetNode*) pairs from the attributes
479 // list.
480 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrPairVec;
481 for (ArrayRef<std::pair<unsigned, Attribute> >::iterator I = Attrs.begin(),
482 E = Attrs.end(); I != E; ) {
483 unsigned Index = I->first;
484 SmallVector<Attribute, 4> AttrVec;
NAKAMURA Takumi3ba51ce2013-01-29 15:18:16 +0000485 while (I != E && I->first == Index) {
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000486 AttrVec.push_back(I->second);
487 ++I;
488 }
489
490 AttrPairVec.push_back(std::make_pair(Index,
491 AttributeSetNode::get(C, AttrVec)));
492 }
493
494 return getImpl(C, AttrPairVec);
495}
496
497AttributeSet AttributeSet::get(LLVMContext &C,
498 ArrayRef<std::pair<unsigned,
499 AttributeSetNode*> > Attrs) {
500 // If there are no attributes then return a null AttributesList pointer.
501 if (Attrs.empty())
502 return AttributeSet();
503
504 return getImpl(C, Attrs);
505}
506
507AttributeSet AttributeSet::get(LLVMContext &C, unsigned Idx, AttrBuilder &B) {
508 if (!B.hasAttributes())
509 return AttributeSet();
Bill Wendling8fbc0c22013-01-29 01:02:03 +0000510
511 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
512 for (AttrBuilder::iterator I = B.begin(), E = B.end(); I != E; ++I) {
513 Attribute::AttrKind Kind = *I;
514 if (Kind == Attribute::Alignment)
515 Attrs.push_back(std::make_pair(Idx, Attribute::
516 getWithAlignment(C, B.getAlignment())));
517 else if (Kind == Attribute::StackAlignment)
518 Attrs.push_back(std::make_pair(Idx, Attribute::
519 getWithStackAlignment(C, B.getStackAlignment())));
520 else
521 Attrs.push_back(std::make_pair(Idx, Attribute::get(C, Kind)));
522 }
523
524 return get(C, Attrs);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000525}
526
527AttributeSet AttributeSet::get(LLVMContext &C, unsigned Idx,
528 ArrayRef<Attribute::AttrKind> Kind) {
529 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
530 for (ArrayRef<Attribute::AttrKind>::iterator I = Kind.begin(),
531 E = Kind.end(); I != E; ++I)
532 Attrs.push_back(std::make_pair(Idx, Attribute::get(C, *I)));
533 return get(C, Attrs);
534}
535
536AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<AttributeSet> Attrs) {
537 if (Attrs.empty()) return AttributeSet();
538
539 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrNodeVec;
540 for (unsigned I = 0, E = Attrs.size(); I != E; ++I) {
541 AttributeSet AS = Attrs[I];
542 if (!AS.pImpl) continue;
543 AttrNodeVec.append(AS.pImpl->AttrNodes.begin(), AS.pImpl->AttrNodes.end());
544 }
545
546 return getImpl(C, AttrNodeVec);
547}
548
549AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Idx,
550 Attribute::AttrKind Attr) const {
551 return addAttributes(C, Idx, AttributeSet::get(C, Idx, Attr));
552}
553
554AttributeSet AttributeSet::addAttributes(LLVMContext &C, unsigned Idx,
555 AttributeSet Attrs) const {
556 if (!pImpl) return Attrs;
557 if (!Attrs.pImpl) return *this;
558
559#ifndef NDEBUG
560 // FIXME it is not obvious how this should work for alignment. For now, say
561 // we can't change a known alignment.
562 unsigned OldAlign = getParamAlignment(Idx);
563 unsigned NewAlign = Attrs.getParamAlignment(Idx);
564 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
565 "Attempt to change alignment!");
566#endif
567
568 // Add the attribute slots before the one we're trying to add.
569 SmallVector<AttributeSet, 4> AttrSet;
570 uint64_t NumAttrs = pImpl->getNumAttributes();
571 AttributeSet AS;
572 uint64_t LastIndex = 0;
573 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
574 if (getSlotIndex(I) >= Idx) {
575 if (getSlotIndex(I) == Idx) AS = getSlotAttributes(LastIndex++);
576 break;
577 }
578 LastIndex = I + 1;
579 AttrSet.push_back(getSlotAttributes(I));
580 }
581
582 // Now add the attribute into the correct slot. There may already be an
583 // AttributeSet there.
584 AttrBuilder B(AS, Idx);
585
586 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
587 if (Attrs.getSlotIndex(I) == Idx) {
588 for (AttributeSetImpl::const_iterator II = Attrs.pImpl->begin(I),
589 IE = Attrs.pImpl->end(I); II != IE; ++II)
Bill Wendling39da0782013-01-31 23:38:01 +0000590 B.addAttribute(*II);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000591 break;
592 }
593
594 AttrSet.push_back(AttributeSet::get(C, Idx, B));
595
596 // Add the remaining attribute slots.
597 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
598 AttrSet.push_back(getSlotAttributes(I));
599
600 return get(C, AttrSet);
601}
602
603AttributeSet AttributeSet::removeAttribute(LLVMContext &C, unsigned Idx,
604 Attribute::AttrKind Attr) const {
605 return removeAttributes(C, Idx, AttributeSet::get(C, Idx, Attr));
606}
607
608AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Idx,
609 AttributeSet Attrs) const {
610 if (!pImpl) return AttributeSet();
611 if (!Attrs.pImpl) return *this;
612
613#ifndef NDEBUG
614 // FIXME it is not obvious how this should work for alignment.
615 // For now, say we can't pass in alignment, which no current use does.
616 assert(!Attrs.hasAttribute(Idx, Attribute::Alignment) &&
617 "Attempt to change alignment!");
618#endif
619
620 // Add the attribute slots before the one we're trying to add.
621 SmallVector<AttributeSet, 4> AttrSet;
622 uint64_t NumAttrs = pImpl->getNumAttributes();
623 AttributeSet AS;
624 uint64_t LastIndex = 0;
625 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
626 if (getSlotIndex(I) >= Idx) {
627 if (getSlotIndex(I) == Idx) AS = getSlotAttributes(LastIndex++);
628 break;
629 }
630 LastIndex = I + 1;
631 AttrSet.push_back(getSlotAttributes(I));
632 }
633
Bill Wendlinge7436542013-01-30 23:07:40 +0000634 // Now remove the attribute from the correct slot. There may already be an
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000635 // AttributeSet there.
636 AttrBuilder B(AS, Idx);
637
638 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
639 if (Attrs.getSlotIndex(I) == Idx) {
Bill Wendlinge7436542013-01-30 23:07:40 +0000640 B.removeAttributes(Attrs.pImpl->getSlotAttributes(I), Idx);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000641 break;
642 }
643
644 AttrSet.push_back(AttributeSet::get(C, Idx, B));
645
646 // Add the remaining attribute slots.
647 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
648 AttrSet.push_back(getSlotAttributes(I));
649
650 return get(C, AttrSet);
651}
652
653//===----------------------------------------------------------------------===//
654// AttributeSet Accessor Methods
655//===----------------------------------------------------------------------===//
656
657AttributeSet AttributeSet::getParamAttributes(unsigned Idx) const {
658 return pImpl && hasAttributes(Idx) ?
659 AttributeSet::get(pImpl->getContext(),
Bill Wendling606c8e32013-01-29 03:20:31 +0000660 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000661 std::make_pair(Idx, getAttributes(Idx)))) :
662 AttributeSet();
663}
664
665AttributeSet AttributeSet::getRetAttributes() const {
666 return pImpl && hasAttributes(ReturnIndex) ?
667 AttributeSet::get(pImpl->getContext(),
Bill Wendling606c8e32013-01-29 03:20:31 +0000668 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000669 std::make_pair(ReturnIndex,
670 getAttributes(ReturnIndex)))) :
671 AttributeSet();
672}
673
674AttributeSet AttributeSet::getFnAttributes() const {
675 return pImpl && hasAttributes(FunctionIndex) ?
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(FunctionIndex,
679 getAttributes(FunctionIndex)))) :
680 AttributeSet();
681}
682
683bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{
Bill Wendling606c8e32013-01-29 03:20:31 +0000684 AttributeSetNode *ASN = getAttributes(Index);
685 return ASN ? ASN->hasAttribute(Kind) : false;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000686}
687
688bool AttributeSet::hasAttributes(unsigned Index) const {
Bill Wendling606c8e32013-01-29 03:20:31 +0000689 AttributeSetNode *ASN = getAttributes(Index);
690 return ASN ? ASN->hasAttributes() : false;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000691}
692
693/// \brief Return true if the specified attribute is set for at least one
694/// parameter or for the return value.
695bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr) const {
696 if (pImpl == 0) return false;
697
698 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
699 for (AttributeSetImpl::const_iterator II = pImpl->begin(I),
700 IE = pImpl->end(I); II != IE; ++II)
NAKAMURA Takumieddab152013-01-31 03:47:28 +0000701 if (II->hasAttribute(Attr))
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000702 return true;
703
704 return false;
705}
706
Bill Wendling606c8e32013-01-29 03:20:31 +0000707unsigned AttributeSet::getParamAlignment(unsigned Index) const {
708 AttributeSetNode *ASN = getAttributes(Index);
709 return ASN ? ASN->getAlignment() : 0;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000710}
711
712unsigned AttributeSet::getStackAlignment(unsigned Index) const {
Bill Wendling606c8e32013-01-29 03:20:31 +0000713 AttributeSetNode *ASN = getAttributes(Index);
714 return ASN ? ASN->getStackAlignment() : 0;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000715}
716
717std::string AttributeSet::getAsString(unsigned Index) const {
Bill Wendling606c8e32013-01-29 03:20:31 +0000718 AttributeSetNode *ASN = getAttributes(Index);
719 return ASN ? ASN->getAsString() : std::string("");
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000720}
721
722/// \brief The attributes for the specified index are returned.
Bill Wendling606c8e32013-01-29 03:20:31 +0000723AttributeSetNode *AttributeSet::getAttributes(unsigned Idx) const {
724 if (!pImpl) return 0;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000725
Bill Wendling606c8e32013-01-29 03:20:31 +0000726 // Loop through to find the attribute node we want.
727 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
728 if (pImpl->getSlotIndex(I) == Idx)
729 return pImpl->getSlotNode(I);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000730
Bill Wendling606c8e32013-01-29 03:20:31 +0000731 return 0;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000732}
733
Bill Wendlingf715dbd2013-02-01 00:48:14 +0000734AttributeSet::iterator AttributeSet::begin(unsigned Idx) const {
Bill Wendling16c4b3c2013-01-31 23:53:05 +0000735 if (!pImpl)
736 return ArrayRef<Attribute>().begin();
737 return pImpl->begin(Idx);
738}
739
Bill Wendlingf715dbd2013-02-01 00:48:14 +0000740AttributeSet::iterator AttributeSet::end(unsigned Idx) const {
Bill Wendling16c4b3c2013-01-31 23:53:05 +0000741 if (!pImpl)
742 return ArrayRef<Attribute>().end();
Bill Wendling30d2c762013-02-01 00:13:50 +0000743 return pImpl->end(Idx);
Bill Wendling16c4b3c2013-01-31 23:53:05 +0000744}
745
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000746//===----------------------------------------------------------------------===//
747// AttributeSet Introspection Methods
748//===----------------------------------------------------------------------===//
749
750/// \brief Return the number of slots used in this attribute list. This is the
751/// number of arguments that have an attribute set on them (including the
752/// function itself).
753unsigned AttributeSet::getNumSlots() const {
754 return pImpl ? pImpl->getNumAttributes() : 0;
755}
756
757uint64_t AttributeSet::getSlotIndex(unsigned Slot) const {
758 assert(pImpl && Slot < pImpl->getNumAttributes() &&
759 "Slot # out of range!");
760 return pImpl->getSlotIndex(Slot);
761}
762
763AttributeSet AttributeSet::getSlotAttributes(unsigned Slot) const {
764 assert(pImpl && Slot < pImpl->getNumAttributes() &&
765 "Slot # out of range!");
766 return pImpl->getSlotAttributes(Slot);
767}
768
769uint64_t AttributeSet::Raw(unsigned Index) const {
770 // FIXME: Remove this.
771 return pImpl ? pImpl->Raw(Index) : 0;
772}
773
774void AttributeSet::dump() const {
775 dbgs() << "PAL[\n";
776
777 for (unsigned i = 0, e = getNumSlots(); i < e; ++i) {
778 uint64_t Index = getSlotIndex(i);
779 dbgs() << " { ";
780 if (Index == ~0U)
781 dbgs() << "~0U";
782 else
783 dbgs() << Index;
784 dbgs() << " => " << getAsString(Index) << " }\n";
785 }
786
787 dbgs() << "]\n";
788}
789
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000790//===----------------------------------------------------------------------===//
Bill Wendling03198882013-01-04 23:27:34 +0000791// AttrBuilder Method Implementations
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000792//===----------------------------------------------------------------------===//
793
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000794AttrBuilder::AttrBuilder(AttributeSet AS, unsigned Idx)
795 : Alignment(0), StackAlignment(0) {
Bill Wendlingec258982013-01-27 21:23:46 +0000796 AttributeSetImpl *pImpl = AS.pImpl;
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000797 if (!pImpl) return;
798
Bill Wendling73bc4522013-01-28 00:21:34 +0000799 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I) {
800 if (pImpl->getSlotIndex(I) != Idx) continue;
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000801
Bill Wendling383da6b2013-01-30 21:22:59 +0000802 for (AttributeSetImpl::const_iterator II = pImpl->begin(I),
Bill Wendling73bc4522013-01-28 00:21:34 +0000803 IE = pImpl->end(I); II != IE; ++II)
Bill Wendling39da0782013-01-31 23:38:01 +0000804 addAttribute(*II);
Bill Wendling73bc4522013-01-28 00:21:34 +0000805
806 break;
807 }
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000808}
809
Bill Wendling03198882013-01-04 23:27:34 +0000810void AttrBuilder::clear() {
811 Attrs.clear();
812 Alignment = StackAlignment = 0;
813}
814
815AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
Bill Wendling169d5272013-01-31 23:16:25 +0000816 assert(Val != Attribute::Alignment && Val != Attribute::StackAlignment &&
817 "Adding alignment attribute without adding alignment value!");
Bill Wendling03198882013-01-04 23:27:34 +0000818 Attrs.insert(Val);
Bill Wendling3a106e62012-10-09 19:01:18 +0000819 return *this;
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000820}
821
Bill Wendling39da0782013-01-31 23:38:01 +0000822AttrBuilder &AttrBuilder::addAttribute(Attribute Attr) {
Bill Wendling169d5272013-01-31 23:16:25 +0000823 ConstantInt *Kind = cast<ConstantInt>(Attr.getAttributeKind());
824 Attribute::AttrKind KindVal = Attribute::AttrKind(Kind->getZExtValue());
825 Attrs.insert(KindVal);
Bill Wendling49f60602013-01-28 05:23:28 +0000826
Bill Wendling169d5272013-01-31 23:16:25 +0000827 if (KindVal == Attribute::Alignment)
Bill Wendling49f60602013-01-28 05:23:28 +0000828 Alignment = Attr.getAlignment();
Bill Wendling169d5272013-01-31 23:16:25 +0000829 else if (KindVal == Attribute::StackAlignment)
Bill Wendling49f60602013-01-28 05:23:28 +0000830 StackAlignment = Attr.getStackAlignment();
831 return *this;
832}
833
Bill Wendling39da0782013-01-31 23:38:01 +0000834AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
835 Attrs.erase(Val);
836
837 if (Val == Attribute::Alignment)
838 Alignment = 0;
839 else if (Val == Attribute::StackAlignment)
840 StackAlignment = 0;
841
842 return *this;
843}
844
Bill Wendlinge7436542013-01-30 23:07:40 +0000845AttrBuilder &AttrBuilder::removeAttributes(AttributeSet A, uint64_t Index) {
Bill Wendling30d2c762013-02-01 00:13:50 +0000846 unsigned Idx = ~0U;
847 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
848 if (A.getSlotIndex(I) == Index) {
849 Idx = I;
850 break;
Bill Wendling49f60602013-01-28 05:23:28 +0000851 }
Bill Wendling30d2c762013-02-01 00:13:50 +0000852
853 assert(Idx != ~0U && "Couldn't find index in AttributeSet!");
854
855 for (AttributeSet::iterator I = A.begin(Idx), E = A.end(Idx); I != E; ++I) {
856 ConstantInt *CI = cast<ConstantInt>(I->getAttributeKind());
857 Attribute::AttrKind Kind = Attribute::AttrKind(CI->getZExtValue());
858 Attrs.erase(Kind);
859
860 if (Kind == Attribute::Alignment)
861 Alignment = 0;
862 else if (Kind == Attribute::StackAlignment)
863 StackAlignment = 0;
Bill Wendling49f60602013-01-28 05:23:28 +0000864 }
865
866 return *this;
867}
868
Bill Wendling702cc912012-10-15 20:35:56 +0000869AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
Bill Wendlingda3f9d82012-10-14 03:58:29 +0000870 if (Align == 0) return *this;
Bill Wendling03198882013-01-04 23:27:34 +0000871
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000872 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
873 assert(Align <= 0x40000000 && "Alignment too large.");
Bill Wendling03198882013-01-04 23:27:34 +0000874
875 Attrs.insert(Attribute::Alignment);
876 Alignment = Align;
Bill Wendlingda3f9d82012-10-14 03:58:29 +0000877 return *this;
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000878}
879
Bill Wendling03198882013-01-04 23:27:34 +0000880AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
881 // Default alignment, allow the target to define how to align it.
882 if (Align == 0) return *this;
883
884 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
885 assert(Align <= 0x100 && "Alignment too large.");
886
887 Attrs.insert(Attribute::StackAlignment);
888 StackAlignment = Align;
889 return *this;
890}
891
Bill Wendling22bd6412013-01-03 01:54:39 +0000892bool AttrBuilder::contains(Attribute::AttrKind A) const {
Bill Wendling03198882013-01-04 23:27:34 +0000893 return Attrs.count(A);
Bill Wendling7d2f2492012-10-10 07:36:45 +0000894}
895
Bill Wendling702cc912012-10-15 20:35:56 +0000896bool AttrBuilder::hasAttributes() const {
Bill Wendling03198882013-01-04 23:27:34 +0000897 return !Attrs.empty();
Bill Wendlingf385f4c2012-10-08 23:27:46 +0000898}
Bill Wendling60507d52013-01-04 20:54:35 +0000899
Bill Wendlinge7436542013-01-30 23:07:40 +0000900bool AttrBuilder::hasAttributes(AttributeSet A, uint64_t Index) const {
901 return Raw() & A.Raw(Index);
Bill Wendling8831c062012-10-09 00:01:21 +0000902}
Bill Wendling60507d52013-01-04 20:54:35 +0000903
Bill Wendling702cc912012-10-15 20:35:56 +0000904bool AttrBuilder::hasAlignmentAttr() const {
Bill Wendling03198882013-01-04 23:27:34 +0000905 return Alignment != 0;
Bill Wendlingf385f4c2012-10-08 23:27:46 +0000906}
907
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000908bool AttrBuilder::operator==(const AttrBuilder &B) {
909 SmallVector<Attribute::AttrKind, 8> This(Attrs.begin(), Attrs.end());
910 SmallVector<Attribute::AttrKind, 8> That(B.Attrs.begin(), B.Attrs.end());
911 return This == That;
912}
913
914AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) {
Bill Wendling8232ece2013-01-29 01:43:29 +0000915 if (!Val) return *this;
916
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000917 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
918 I = Attribute::AttrKind(I + 1)) {
919 if (uint64_t A = (Val & AttributeImpl::getAttrMask(I))) {
920 Attrs.insert(I);
921
922 if (I == Attribute::Alignment)
923 Alignment = 1ULL << ((A >> 16) - 1);
924 else if (I == Attribute::StackAlignment)
925 StackAlignment = 1ULL << ((A >> 26)-1);
926 }
927 }
928
929 return *this;
930}
931
Bill Wendling1db9b692013-01-09 23:36:50 +0000932uint64_t AttrBuilder::Raw() const {
Bill Wendling03198882013-01-04 23:27:34 +0000933 uint64_t Mask = 0;
934
935 for (DenseSet<Attribute::AttrKind>::const_iterator I = Attrs.begin(),
936 E = Attrs.end(); I != E; ++I) {
937 Attribute::AttrKind Kind = *I;
938
939 if (Kind == Attribute::Alignment)
940 Mask |= (Log2_32(Alignment) + 1) << 16;
941 else if (Kind == Attribute::StackAlignment)
942 Mask |= (Log2_32(StackAlignment) + 1) << 26;
943 else
944 Mask |= AttributeImpl::getAttrMask(Kind);
945 }
946
947 return Mask;
Bill Wendlingf385f4c2012-10-08 23:27:46 +0000948}
949
Bill Wendling8e47daf2013-01-25 23:09:36 +0000950//===----------------------------------------------------------------------===//
951// AttributeFuncs Function Defintions
952//===----------------------------------------------------------------------===//
953
Bill Wendlinge7436542013-01-30 23:07:40 +0000954AttributeSet AttributeFuncs::typeIncompatible(Type *Ty, uint64_t Index) {
Bill Wendling8e47daf2013-01-25 23:09:36 +0000955 AttrBuilder Incompatible;
956
957 if (!Ty->isIntegerTy())
958 // Attribute that only apply to integers.
959 Incompatible.addAttribute(Attribute::SExt)
960 .addAttribute(Attribute::ZExt);
961
962 if (!Ty->isPointerTy())
963 // Attribute that only apply to pointers.
964 Incompatible.addAttribute(Attribute::ByVal)
965 .addAttribute(Attribute::Nest)
966 .addAttribute(Attribute::NoAlias)
967 .addAttribute(Attribute::NoCapture)
968 .addAttribute(Attribute::StructRet);
969
Bill Wendlinge7436542013-01-30 23:07:40 +0000970 return AttributeSet::get(Ty->getContext(), Index, Incompatible);
Bill Wendling8e47daf2013-01-25 23:09:36 +0000971}
972
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000973/// \brief This returns an integer containing an encoding of all the LLVM
974/// attributes found in the given attribute bitset. Any change to this encoding
975/// is a breaking change to bitcode compatibility.
Bill Wendling8232ece2013-01-29 01:43:29 +0000976/// N.B. This should be used only by the bitcode reader!
Bill Wendling8e47daf2013-01-25 23:09:36 +0000977uint64_t AttributeFuncs::encodeLLVMAttributesForBitcode(AttributeSet Attrs,
978 unsigned Index) {
979 // FIXME: It doesn't make sense to store the alignment information as an
980 // expanded out value, we should store it as a log2 value. However, we can't
981 // just change that here without breaking bitcode compatibility. If this ever
982 // becomes a problem in practice, we should introduce new tag numbers in the
983 // bitcode file and have those tags use a more efficiently encoded alignment
984 // field.
985
986 // Store the alignment in the bitcode as a 16-bit raw value instead of a 5-bit
987 // log2 encoded value. Shift the bits above the alignment up by 11 bits.
988 uint64_t EncodedAttrs = Attrs.Raw(Index) & 0xffff;
989 if (Attrs.hasAttribute(Index, Attribute::Alignment))
990 EncodedAttrs |= Attrs.getParamAlignment(Index) << 16;
991 EncodedAttrs |= (Attrs.Raw(Index) & (0xffffULL << 21)) << 11;
992 return EncodedAttrs;
993}
994
Bill Wendling8232ece2013-01-29 01:43:29 +0000995/// \brief This fills an AttrBuilder object with the LLVM attributes that have
996/// been decoded from the given integer. This function must stay in sync with
997/// 'encodeLLVMAttributesForBitcode'.
998/// N.B. This should be used only by the bitcode reader!
999void AttributeFuncs::decodeLLVMAttributesForBitcode(LLVMContext &C,
1000 AttrBuilder &B,
1001 uint64_t EncodedAttrs) {
Bill Wendling8e47daf2013-01-25 23:09:36 +00001002 // The alignment is stored as a 16-bit raw value from bits 31--16. We shift
1003 // the bits above 31 down by 11 bits.
1004 unsigned Alignment = (EncodedAttrs & (0xffffULL << 16)) >> 16;
1005 assert((!Alignment || isPowerOf2_32(Alignment)) &&
1006 "Alignment must be a power of two.");
1007
Bill Wendling8e47daf2013-01-25 23:09:36 +00001008 if (Alignment)
1009 B.addAlignmentAttr(Alignment);
Bill Wendling606c8e32013-01-29 03:20:31 +00001010 B.addRawValue(((EncodedAttrs & (0xffffULL << 32)) >> 11) |
1011 (EncodedAttrs & 0xffff));
Bill Wendling8e47daf2013-01-25 23:09:36 +00001012}