blob: 5608cbd47fa1c51bdc073a8596b5b3edaced0e59 [file] [log] [blame]
Bill Wendling87e10df2013-01-28 21:55:20 +00001//===-- Attributes.cpp - Implement AttributesList -------------------------===//
Chris Lattner50ee9dd2008-01-02 23:42:30 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Bill Wendling87e10df2013-01-28 21:55:20 +000010// \file
11// \brief This file implements the Attribute, AttributeImpl, AttrBuilder,
Bill Wendling18e72112012-12-19 22:42:22 +000012// AttributeSetImpl, and AttributeSet classes.
Chris Lattner50ee9dd2008-01-02 23:42:30 +000013//
14//===----------------------------------------------------------------------===//
15
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000016#include "llvm/IR/Attributes.h"
Bill Wendlingf6670722012-12-20 01:36:59 +000017#include "AttributeImpl.h"
Bill Wendling2c79ecb2012-09-26 21:07:29 +000018#include "LLVMContextImpl.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000019#include "llvm/ADT/StringExtras.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000020#include "llvm/IR/Type.h"
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000021#include "llvm/Support/Atomic.h"
David Greeneef1894e2010-01-05 01:29:58 +000022#include "llvm/Support/Debug.h"
Chris Lattner50ee9dd2008-01-02 23:42:30 +000023#include "llvm/Support/ManagedStatic.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000024#include "llvm/Support/Mutex.h"
Benjamin Kramercfa6ec92009-08-23 11:37:21 +000025#include "llvm/Support/raw_ostream.h"
Bill Wendling3467e302013-01-24 00:06:56 +000026#include <algorithm>
Chris Lattner50ee9dd2008-01-02 23:42:30 +000027using namespace llvm;
28
Chris Lattner58d74912008-03-12 17:45:29 +000029//===----------------------------------------------------------------------===//
Bill Wendling817abdd2013-01-29 00:48:16 +000030// Attribute Construction Methods
Chris Lattner58d74912008-03-12 17:45:29 +000031//===----------------------------------------------------------------------===//
Chris Lattnerfabfde32008-01-03 00:10:22 +000032
Bill Wendlingb96129d2013-01-31 01:04:51 +000033Attribute Attribute::get(LLVMContext &Context, AttrKind Kind) {
34 AttrBuilder B;
35 return Attribute::get(Context, B.addAttribute(Kind));
36}
37
38Attribute Attribute::get(LLVMContext &Context, AttrBuilder &B) {
39 // If there are no attributes, return an empty Attribute class.
40 if (!B.hasAttributes())
41 return Attribute();
42
43 assert(std::distance(B.begin(), B.end()) == 1 &&
44 "The Attribute object should represent one attribute only!");
Bill Wendling73dee182013-01-31 00:29:54 +000045
Bill Wendling8e635db2012-10-08 21:47:17 +000046 // Otherwise, build a key to look up the existing attributes.
47 LLVMContextImpl *pImpl = Context.pImpl;
48 FoldingSetNodeID ID;
Bill Wendlingb96129d2013-01-31 01:04:51 +000049 ConstantInt *CI = ConstantInt::get(Type::getInt64Ty(Context), B.Raw());
50 ID.AddPointer(CI);
Bill Wendling8e635db2012-10-08 21:47:17 +000051
52 void *InsertPoint;
Bill Wendlingf6670722012-12-20 01:36:59 +000053 AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
Bill Wendling8e635db2012-10-08 21:47:17 +000054
55 if (!PA) {
56 // If we didn't find any existing attributes of the same shape then create a
57 // new one and insert it.
Bill Wendlingb96129d2013-01-31 01:04:51 +000058 PA = new AttributeImpl(Context, CI);
Bill Wendling8e635db2012-10-08 21:47:17 +000059 pImpl->AttrsSet.InsertNode(PA, InsertPoint);
60 }
61
62 // Return the AttributesList that we found or created.
Bill Wendling034b94b2012-12-19 07:18:57 +000063 return Attribute(PA);
Bill Wendling8e635db2012-10-08 21:47:17 +000064}
65
Bill Wendlingc08a5ef2013-01-27 22:43:04 +000066Attribute Attribute::getWithAlignment(LLVMContext &Context, uint64_t Align) {
Bill Wendlingb96129d2013-01-31 01:04:51 +000067 AttrBuilder B;
68 return get(Context, B.addAlignmentAttr(Align));
Bill Wendlingc08a5ef2013-01-27 22:43:04 +000069}
70
71Attribute Attribute::getWithStackAlignment(LLVMContext &Context,
72 uint64_t Align) {
Bill Wendlingb96129d2013-01-31 01:04:51 +000073 AttrBuilder B;
74 return get(Context, B.addStackAlignmentAttr(Align));
Bill Wendlingc08a5ef2013-01-27 22:43:04 +000075}
76
Bill Wendling817abdd2013-01-29 00:48:16 +000077//===----------------------------------------------------------------------===//
78// Attribute Accessor Methods
79//===----------------------------------------------------------------------===//
80
Bill Wendling629fb822012-12-22 00:37:52 +000081bool Attribute::hasAttribute(AttrKind Val) const {
Bill Wendling27107f62012-12-20 21:28:43 +000082 return pImpl && pImpl->hasAttribute(Val);
Bill Wendlinge66f3d32012-10-05 06:44:41 +000083}
84
Bill Wendling6dc37812013-01-29 20:45:34 +000085Constant *Attribute::getAttributeKind() const {
86 return pImpl ? pImpl->getAttributeKind() : 0;
87}
88
89ArrayRef<Constant*> Attribute::getAttributeValues() const {
90 return pImpl ? pImpl->getAttributeValues() : ArrayRef<Constant*>();
91}
92
Bill Wendlinge66f3d32012-10-05 06:44:41 +000093/// This returns the alignment field of an attribute as a byte alignment value.
Bill Wendling034b94b2012-12-19 07:18:57 +000094unsigned Attribute::getAlignment() const {
95 if (!hasAttribute(Attribute::Alignment))
Bill Wendling9ef99c92012-10-09 20:56:48 +000096 return 0;
Bill Wendlinga8ab5fc2013-01-23 23:00:05 +000097 return pImpl->getAlignment();
Bill Wendlinge66f3d32012-10-05 06:44:41 +000098}
99
100/// This returns the stack alignment field of an attribute as a byte alignment
101/// value.
Bill Wendling034b94b2012-12-19 07:18:57 +0000102unsigned Attribute::getStackAlignment() const {
103 if (!hasAttribute(Attribute::StackAlignment))
Bill Wendling9ef99c92012-10-09 20:56:48 +0000104 return 0;
Bill Wendlinga8ab5fc2013-01-23 23:00:05 +0000105 return pImpl->getStackAlignment();
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000106}
107
Bill Wendling034b94b2012-12-19 07:18:57 +0000108std::string Attribute::getAsString() const {
Bill Wendling14292a62013-01-31 20:59:05 +0000109 if (!pImpl) return "";
110
111 if (hasAttribute(Attribute::AddressSafety))
112 return "address_safety";
113 if (hasAttribute(Attribute::AlwaysInline))
114 return "alwaysinline";
115 if (hasAttribute(Attribute::ByVal))
116 return "byval";
117 if (hasAttribute(Attribute::InlineHint))
118 return "inlinehint";
Bill Wendling034b94b2012-12-19 07:18:57 +0000119 if (hasAttribute(Attribute::InReg))
Bill Wendling606c8e32013-01-29 03:20:31 +0000120 return "inreg";
Bill Wendling14292a62013-01-31 20:59:05 +0000121 if (hasAttribute(Attribute::MinSize))
122 return "minsize";
123 if (hasAttribute(Attribute::Naked))
124 return "naked";
125 if (hasAttribute(Attribute::Nest))
126 return "nest";
Bill Wendling034b94b2012-12-19 07:18:57 +0000127 if (hasAttribute(Attribute::NoAlias))
Bill Wendling606c8e32013-01-29 03:20:31 +0000128 return "noalias";
Bill Wendling034b94b2012-12-19 07:18:57 +0000129 if (hasAttribute(Attribute::NoCapture))
Bill Wendling606c8e32013-01-29 03:20:31 +0000130 return "nocapture";
Bill Wendling14292a62013-01-31 20:59:05 +0000131 if (hasAttribute(Attribute::NoDuplicate))
132 return "noduplicate";
133 if (hasAttribute(Attribute::NoImplicitFloat))
134 return "noimplicitfloat";
135 if (hasAttribute(Attribute::NoInline))
136 return "noinline";
137 if (hasAttribute(Attribute::NonLazyBind))
138 return "nonlazybind";
139 if (hasAttribute(Attribute::NoRedZone))
140 return "noredzone";
141 if (hasAttribute(Attribute::NoReturn))
142 return "noreturn";
143 if (hasAttribute(Attribute::NoUnwind))
144 return "nounwind";
145 if (hasAttribute(Attribute::OptimizeForSize))
146 return "optsize";
Bill Wendling034b94b2012-12-19 07:18:57 +0000147 if (hasAttribute(Attribute::ReadNone))
Bill Wendling606c8e32013-01-29 03:20:31 +0000148 return "readnone";
Bill Wendling034b94b2012-12-19 07:18:57 +0000149 if (hasAttribute(Attribute::ReadOnly))
Bill Wendling606c8e32013-01-29 03:20:31 +0000150 return "readonly";
Bill Wendling14292a62013-01-31 20:59:05 +0000151 if (hasAttribute(Attribute::ReturnsTwice))
152 return "returns_twice";
153 if (hasAttribute(Attribute::SExt))
154 return "signext";
Bill Wendling034b94b2012-12-19 07:18:57 +0000155 if (hasAttribute(Attribute::StackProtect))
Bill Wendling606c8e32013-01-29 03:20:31 +0000156 return "ssp";
Bill Wendling034b94b2012-12-19 07:18:57 +0000157 if (hasAttribute(Attribute::StackProtectReq))
Bill Wendling606c8e32013-01-29 03:20:31 +0000158 return "sspreq";
Bill Wendling114baee2013-01-23 06:41:41 +0000159 if (hasAttribute(Attribute::StackProtectStrong))
Bill Wendling606c8e32013-01-29 03:20:31 +0000160 return "sspstrong";
Bill Wendling14292a62013-01-31 20:59:05 +0000161 if (hasAttribute(Attribute::StructRet))
162 return "sret";
163 if (hasAttribute(Attribute::UWTable))
164 return "uwtable";
165 if (hasAttribute(Attribute::ZExt))
166 return "zeroext";
167
168 // FIXME: These should be output like this:
169 //
170 // align=4
171 // alignstack=8
172 //
Bill Wendling034b94b2012-12-19 07:18:57 +0000173 if (hasAttribute(Attribute::StackAlignment)) {
Bill Wendling606c8e32013-01-29 03:20:31 +0000174 std::string Result;
Charles Davis1e063d12010-02-12 00:31:15 +0000175 Result += "alignstack(";
Bill Wendlingef99fe82012-09-21 15:26:31 +0000176 Result += utostr(getStackAlignment());
Bill Wendling606c8e32013-01-29 03:20:31 +0000177 Result += ")";
178 return Result;
Charles Davis1e063d12010-02-12 00:31:15 +0000179 }
Bill Wendling034b94b2012-12-19 07:18:57 +0000180 if (hasAttribute(Attribute::Alignment)) {
Bill Wendling606c8e32013-01-29 03:20:31 +0000181 std::string Result;
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000182 Result += "align ";
Bill Wendlingef99fe82012-09-21 15:26:31 +0000183 Result += utostr(getAlignment());
Bill Wendling606c8e32013-01-29 03:20:31 +0000184 return Result;
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000185 }
Bill Wendling14292a62013-01-31 20:59:05 +0000186
187 // Convert target-dependent attributes to strings of the form:
188 //
189 // "kind"
190 // "kind" = "value"
191 // "kind" = ("value1" "value2" "value3" )
192 //
193 if (ConstantDataArray *CDA =
194 dyn_cast<ConstantDataArray>(pImpl->getAttributeKind())) {
195 std::string Result;
196 Result += '\"' + CDA->getAsString().str() + '"';
197
198 ArrayRef<Constant*> Vals = pImpl->getAttributeValues();
199 if (Vals.empty()) return Result;
200 Result += " = ";
201 if (Vals.size() > 1) Result += '(';
202 for (ArrayRef<Constant*>::iterator I = Vals.begin(), E = Vals.end();
203 I != E; ) {
204 ConstantDataArray *CDA = cast<ConstantDataArray>(*I++);
205 Result += '\"' + CDA->getAsString().str() + '"';
206 if (I != E) Result += ' ';
207 }
208 if (Vals.size() > 1) Result += ')';
209 }
Bill Wendling606c8e32013-01-29 03:20:31 +0000210
211 llvm_unreachable("Unknown attribute");
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000212}
213
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000214bool Attribute::operator==(AttrKind K) const {
Bill Wendling14292a62013-01-31 20:59:05 +0000215 return (pImpl && *pImpl == K) || (!pImpl && K == None);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000216}
217bool Attribute::operator!=(AttrKind K) const {
218 return !(*this == K);
219}
220
221bool Attribute::operator<(Attribute A) const {
222 if (!pImpl && !A.pImpl) return false;
223 if (!pImpl) return true;
224 if (!A.pImpl) return false;
225 return *pImpl < *A.pImpl;
226}
227
228uint64_t Attribute::Raw() const {
229 return pImpl ? pImpl->Raw() : 0;
230}
231
232//===----------------------------------------------------------------------===//
233// AttributeImpl Definition
234//===----------------------------------------------------------------------===//
235
Bill Wendling9f175f82013-01-29 20:37:10 +0000236AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind kind)
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000237 : Context(C) {
Bill Wendling9f175f82013-01-29 20:37:10 +0000238 Kind = ConstantInt::get(Type::getInt64Ty(C), kind);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000239}
Bill Wendling9f175f82013-01-29 20:37:10 +0000240AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind kind,
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000241 ArrayRef<Constant*> values)
242 : Context(C) {
Bill Wendling9f175f82013-01-29 20:37:10 +0000243 Kind = ConstantInt::get(Type::getInt64Ty(C), kind);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000244 Vals.reserve(values.size());
245 Vals.append(values.begin(), values.end());
246}
Bill Wendling9f175f82013-01-29 20:37:10 +0000247AttributeImpl::AttributeImpl(LLVMContext &C, StringRef kind)
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000248 : Context(C) {
Bill Wendling9f175f82013-01-29 20:37:10 +0000249 Kind = ConstantDataArray::getString(C, kind);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000250}
251
252bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
253 return (Raw() & getAttrMask(A)) != 0;
254}
255
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000256uint64_t AttributeImpl::getAlignment() const {
257 uint64_t Mask = Raw() & getAttrMask(Attribute::Alignment);
258 return 1ULL << ((Mask >> 16) - 1);
259}
260
261uint64_t AttributeImpl::getStackAlignment() const {
262 uint64_t Mask = Raw() & getAttrMask(Attribute::StackAlignment);
263 return 1ULL << ((Mask >> 26) - 1);
264}
265
Bill Wendling9f175f82013-01-29 20:37:10 +0000266bool AttributeImpl::operator==(Attribute::AttrKind kind) const {
267 if (ConstantInt *CI = dyn_cast<ConstantInt>(Kind))
268 return CI->getZExtValue() == kind;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000269 return false;
270}
Bill Wendling9f175f82013-01-29 20:37:10 +0000271bool AttributeImpl::operator!=(Attribute::AttrKind kind) const {
272 return !(*this == kind);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000273}
274
Bill Wendling9f175f82013-01-29 20:37:10 +0000275bool AttributeImpl::operator==(StringRef kind) const {
276 if (ConstantDataArray *CDA = dyn_cast<ConstantDataArray>(Kind))
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000277 if (CDA->isString())
Bill Wendling9f175f82013-01-29 20:37:10 +0000278 return CDA->getAsString() == kind;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000279 return false;
280}
281
Bill Wendling9f175f82013-01-29 20:37:10 +0000282bool AttributeImpl::operator!=(StringRef kind) const {
283 return !(*this == kind);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000284}
285
286bool AttributeImpl::operator<(const AttributeImpl &AI) const {
Bill Wendling9f175f82013-01-29 20:37:10 +0000287 if (!Kind && !AI.Kind) return false;
288 if (!Kind && AI.Kind) return true;
289 if (Kind && !AI.Kind) return false;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000290
Bill Wendling9f175f82013-01-29 20:37:10 +0000291 ConstantInt *ThisCI = dyn_cast<ConstantInt>(Kind);
292 ConstantInt *ThatCI = dyn_cast<ConstantInt>(AI.Kind);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000293
Bill Wendling9f175f82013-01-29 20:37:10 +0000294 ConstantDataArray *ThisCDA = dyn_cast<ConstantDataArray>(Kind);
295 ConstantDataArray *ThatCDA = dyn_cast<ConstantDataArray>(AI.Kind);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000296
297 if (ThisCI && ThatCI)
298 return ThisCI->getZExtValue() < ThatCI->getZExtValue();
299
300 if (ThisCI && ThatCDA)
301 return true;
302
303 if (ThisCDA && ThatCI)
304 return false;
305
306 return ThisCDA->getAsString() < ThatCDA->getAsString();
307}
308
309uint64_t AttributeImpl::Raw() const {
310 // FIXME: Remove this.
Bill Wendling9f175f82013-01-29 20:37:10 +0000311 return cast<ConstantInt>(Kind)->getZExtValue();
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000312}
313
314uint64_t AttributeImpl::getAttrMask(Attribute::AttrKind Val) {
315 // FIXME: Remove this.
316 switch (Val) {
317 case Attribute::EndAttrKinds:
318 case Attribute::AttrKindEmptyKey:
319 case Attribute::AttrKindTombstoneKey:
320 llvm_unreachable("Synthetic enumerators which should never get here");
321
322 case Attribute::None: return 0;
323 case Attribute::ZExt: return 1 << 0;
324 case Attribute::SExt: return 1 << 1;
325 case Attribute::NoReturn: return 1 << 2;
326 case Attribute::InReg: return 1 << 3;
327 case Attribute::StructRet: return 1 << 4;
328 case Attribute::NoUnwind: return 1 << 5;
329 case Attribute::NoAlias: return 1 << 6;
330 case Attribute::ByVal: return 1 << 7;
331 case Attribute::Nest: return 1 << 8;
332 case Attribute::ReadNone: return 1 << 9;
333 case Attribute::ReadOnly: return 1 << 10;
334 case Attribute::NoInline: return 1 << 11;
335 case Attribute::AlwaysInline: return 1 << 12;
336 case Attribute::OptimizeForSize: return 1 << 13;
337 case Attribute::StackProtect: return 1 << 14;
338 case Attribute::StackProtectReq: return 1 << 15;
339 case Attribute::Alignment: return 31 << 16;
340 case Attribute::NoCapture: return 1 << 21;
341 case Attribute::NoRedZone: return 1 << 22;
342 case Attribute::NoImplicitFloat: return 1 << 23;
343 case Attribute::Naked: return 1 << 24;
344 case Attribute::InlineHint: return 1 << 25;
345 case Attribute::StackAlignment: return 7 << 26;
346 case Attribute::ReturnsTwice: return 1 << 29;
347 case Attribute::UWTable: return 1 << 30;
348 case Attribute::NonLazyBind: return 1U << 31;
349 case Attribute::AddressSafety: return 1ULL << 32;
350 case Attribute::MinSize: return 1ULL << 33;
351 case Attribute::NoDuplicate: return 1ULL << 34;
352 case Attribute::StackProtectStrong: return 1ULL << 35;
353 }
354 llvm_unreachable("Unsupported attribute type");
355}
356
357//===----------------------------------------------------------------------===//
358// AttributeSetNode Definition
359//===----------------------------------------------------------------------===//
360
361AttributeSetNode *AttributeSetNode::get(LLVMContext &C,
362 ArrayRef<Attribute> Attrs) {
363 if (Attrs.empty())
364 return 0;
365
366 // Otherwise, build a key to look up the existing attributes.
367 LLVMContextImpl *pImpl = C.pImpl;
368 FoldingSetNodeID ID;
369
370 SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end());
371 std::sort(SortedAttrs.begin(), SortedAttrs.end());
372
373 for (SmallVectorImpl<Attribute>::iterator I = SortedAttrs.begin(),
374 E = SortedAttrs.end(); I != E; ++I)
375 I->Profile(ID);
376
377 void *InsertPoint;
378 AttributeSetNode *PA =
379 pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint);
380
381 // If we didn't find any existing attributes of the same shape then create a
382 // new one and insert it.
383 if (!PA) {
384 PA = new AttributeSetNode(SortedAttrs);
385 pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint);
386 }
387
388 // Return the AttributesListNode that we found or created.
389 return PA;
390}
391
Bill Wendling606c8e32013-01-29 03:20:31 +0000392bool AttributeSetNode::hasAttribute(Attribute::AttrKind Kind) const {
393 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
394 E = AttrList.end(); I != E; ++I)
NAKAMURA Takumieddab152013-01-31 03:47:28 +0000395 if (I->hasAttribute(Kind))
Bill Wendling606c8e32013-01-29 03:20:31 +0000396 return true;
397 return false;
398}
399
400unsigned AttributeSetNode::getAlignment() const {
401 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
402 E = AttrList.end(); I != E; ++I)
NAKAMURA Takumieddab152013-01-31 03:47:28 +0000403 if (I->hasAttribute(Attribute::Alignment))
Bill Wendling606c8e32013-01-29 03:20:31 +0000404 return I->getAlignment();
405 return 0;
406}
407
408unsigned AttributeSetNode::getStackAlignment() const {
409 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
410 E = AttrList.end(); I != E; ++I)
NAKAMURA Takumieddab152013-01-31 03:47:28 +0000411 if (I->hasAttribute(Attribute::StackAlignment))
Bill Wendling606c8e32013-01-29 03:20:31 +0000412 return I->getStackAlignment();
413 return 0;
414}
415
416std::string AttributeSetNode::getAsString() const {
417 std::string Str = "";
418 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
419 E = AttrList.end(); I != E; ++I) {
420 if (I != AttrList.begin()) Str += " ";
421 Str += I->getAsString();
422 }
423 return Str;
424}
425
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000426//===----------------------------------------------------------------------===//
427// AttributeSetImpl Definition
428//===----------------------------------------------------------------------===//
429
430uint64_t AttributeSetImpl::Raw(uint64_t Index) const {
431 for (unsigned I = 0, E = getNumAttributes(); I != E; ++I) {
432 if (getSlotIndex(I) != Index) continue;
433 const AttributeSetNode *ASN = AttrNodes[I].second;
434 AttrBuilder B;
435
436 for (AttributeSetNode::const_iterator II = ASN->begin(),
437 IE = ASN->end(); II != IE; ++II)
438 B.addAttributes(*II);
439 return B.Raw();
440 }
441
442 return 0;
443}
444
445//===----------------------------------------------------------------------===//
446// AttributeSet Construction and Mutation Methods
447//===----------------------------------------------------------------------===//
448
Bill Wendling8232ece2013-01-29 01:43:29 +0000449AttributeSet
450AttributeSet::getImpl(LLVMContext &C,
451 ArrayRef<std::pair<unsigned, AttributeSetNode*> > Attrs) {
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000452 LLVMContextImpl *pImpl = C.pImpl;
453 FoldingSetNodeID ID;
454 AttributeSetImpl::Profile(ID, Attrs);
455
456 void *InsertPoint;
457 AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
458
459 // If we didn't find any existing attributes of the same shape then
460 // create a new one and insert it.
461 if (!PA) {
462 PA = new AttributeSetImpl(C, Attrs);
463 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
464 }
465
466 // Return the AttributesList that we found or created.
467 return AttributeSet(PA);
468}
469
470AttributeSet AttributeSet::get(LLVMContext &C,
471 ArrayRef<std::pair<unsigned, Attribute> > Attrs){
472 // If there are no attributes then return a null AttributesList pointer.
473 if (Attrs.empty())
474 return AttributeSet();
475
476#ifndef NDEBUG
477 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
478 assert((!i || Attrs[i-1].first <= Attrs[i].first) &&
479 "Misordered Attributes list!");
Bill Wendling82aea642013-01-31 06:22:35 +0000480 assert(Attrs[i].second != Attribute::None &&
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000481 "Pointless attribute!");
482 }
483#endif
484
485 // Create a vector if (unsigned, AttributeSetNode*) pairs from the attributes
486 // list.
487 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrPairVec;
488 for (ArrayRef<std::pair<unsigned, Attribute> >::iterator I = Attrs.begin(),
489 E = Attrs.end(); I != E; ) {
490 unsigned Index = I->first;
491 SmallVector<Attribute, 4> AttrVec;
NAKAMURA Takumi3ba51ce2013-01-29 15:18:16 +0000492 while (I != E && I->first == Index) {
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000493 AttrVec.push_back(I->second);
494 ++I;
495 }
496
497 AttrPairVec.push_back(std::make_pair(Index,
498 AttributeSetNode::get(C, AttrVec)));
499 }
500
501 return getImpl(C, AttrPairVec);
502}
503
504AttributeSet AttributeSet::get(LLVMContext &C,
505 ArrayRef<std::pair<unsigned,
506 AttributeSetNode*> > Attrs) {
507 // If there are no attributes then return a null AttributesList pointer.
508 if (Attrs.empty())
509 return AttributeSet();
510
511 return getImpl(C, Attrs);
512}
513
514AttributeSet AttributeSet::get(LLVMContext &C, unsigned Idx, AttrBuilder &B) {
515 if (!B.hasAttributes())
516 return AttributeSet();
Bill Wendling8fbc0c22013-01-29 01:02:03 +0000517
518 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
519 for (AttrBuilder::iterator I = B.begin(), E = B.end(); I != E; ++I) {
520 Attribute::AttrKind Kind = *I;
521 if (Kind == Attribute::Alignment)
522 Attrs.push_back(std::make_pair(Idx, Attribute::
523 getWithAlignment(C, B.getAlignment())));
524 else if (Kind == Attribute::StackAlignment)
525 Attrs.push_back(std::make_pair(Idx, Attribute::
526 getWithStackAlignment(C, B.getStackAlignment())));
527 else
528 Attrs.push_back(std::make_pair(Idx, Attribute::get(C, Kind)));
529 }
530
531 return get(C, Attrs);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000532}
533
534AttributeSet AttributeSet::get(LLVMContext &C, unsigned Idx,
535 ArrayRef<Attribute::AttrKind> Kind) {
536 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
537 for (ArrayRef<Attribute::AttrKind>::iterator I = Kind.begin(),
538 E = Kind.end(); I != E; ++I)
539 Attrs.push_back(std::make_pair(Idx, Attribute::get(C, *I)));
540 return get(C, Attrs);
541}
542
543AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<AttributeSet> Attrs) {
544 if (Attrs.empty()) return AttributeSet();
545
546 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrNodeVec;
547 for (unsigned I = 0, E = Attrs.size(); I != E; ++I) {
548 AttributeSet AS = Attrs[I];
549 if (!AS.pImpl) continue;
550 AttrNodeVec.append(AS.pImpl->AttrNodes.begin(), AS.pImpl->AttrNodes.end());
551 }
552
553 return getImpl(C, AttrNodeVec);
554}
555
556AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Idx,
557 Attribute::AttrKind Attr) const {
558 return addAttributes(C, Idx, AttributeSet::get(C, Idx, Attr));
559}
560
561AttributeSet AttributeSet::addAttributes(LLVMContext &C, unsigned Idx,
562 AttributeSet Attrs) const {
563 if (!pImpl) return Attrs;
564 if (!Attrs.pImpl) return *this;
565
566#ifndef NDEBUG
567 // FIXME it is not obvious how this should work for alignment. For now, say
568 // we can't change a known alignment.
569 unsigned OldAlign = getParamAlignment(Idx);
570 unsigned NewAlign = Attrs.getParamAlignment(Idx);
571 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
572 "Attempt to change alignment!");
573#endif
574
575 // Add the attribute slots before the one we're trying to add.
576 SmallVector<AttributeSet, 4> AttrSet;
577 uint64_t NumAttrs = pImpl->getNumAttributes();
578 AttributeSet AS;
579 uint64_t LastIndex = 0;
580 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
581 if (getSlotIndex(I) >= Idx) {
582 if (getSlotIndex(I) == Idx) AS = getSlotAttributes(LastIndex++);
583 break;
584 }
585 LastIndex = I + 1;
586 AttrSet.push_back(getSlotAttributes(I));
587 }
588
589 // Now add the attribute into the correct slot. There may already be an
590 // AttributeSet there.
591 AttrBuilder B(AS, Idx);
592
593 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
594 if (Attrs.getSlotIndex(I) == Idx) {
595 for (AttributeSetImpl::const_iterator II = Attrs.pImpl->begin(I),
596 IE = Attrs.pImpl->end(I); II != IE; ++II)
597 B.addAttributes(*II);
598 break;
599 }
600
601 AttrSet.push_back(AttributeSet::get(C, Idx, B));
602
603 // Add the remaining attribute slots.
604 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
605 AttrSet.push_back(getSlotAttributes(I));
606
607 return get(C, AttrSet);
608}
609
610AttributeSet AttributeSet::removeAttribute(LLVMContext &C, unsigned Idx,
611 Attribute::AttrKind Attr) const {
612 return removeAttributes(C, Idx, AttributeSet::get(C, Idx, Attr));
613}
614
615AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Idx,
616 AttributeSet Attrs) const {
617 if (!pImpl) return AttributeSet();
618 if (!Attrs.pImpl) return *this;
619
620#ifndef NDEBUG
621 // FIXME it is not obvious how this should work for alignment.
622 // For now, say we can't pass in alignment, which no current use does.
623 assert(!Attrs.hasAttribute(Idx, Attribute::Alignment) &&
624 "Attempt to change alignment!");
625#endif
626
627 // Add the attribute slots before the one we're trying to add.
628 SmallVector<AttributeSet, 4> AttrSet;
629 uint64_t NumAttrs = pImpl->getNumAttributes();
630 AttributeSet AS;
631 uint64_t LastIndex = 0;
632 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
633 if (getSlotIndex(I) >= Idx) {
634 if (getSlotIndex(I) == Idx) AS = getSlotAttributes(LastIndex++);
635 break;
636 }
637 LastIndex = I + 1;
638 AttrSet.push_back(getSlotAttributes(I));
639 }
640
Bill Wendlinge7436542013-01-30 23:07:40 +0000641 // Now remove the attribute from the correct slot. There may already be an
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000642 // AttributeSet there.
643 AttrBuilder B(AS, Idx);
644
645 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
646 if (Attrs.getSlotIndex(I) == Idx) {
Bill Wendlinge7436542013-01-30 23:07:40 +0000647 B.removeAttributes(Attrs.pImpl->getSlotAttributes(I), Idx);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000648 break;
649 }
650
651 AttrSet.push_back(AttributeSet::get(C, Idx, B));
652
653 // Add the remaining attribute slots.
654 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
655 AttrSet.push_back(getSlotAttributes(I));
656
657 return get(C, AttrSet);
658}
659
660//===----------------------------------------------------------------------===//
661// AttributeSet Accessor Methods
662//===----------------------------------------------------------------------===//
663
664AttributeSet AttributeSet::getParamAttributes(unsigned Idx) const {
665 return pImpl && hasAttributes(Idx) ?
666 AttributeSet::get(pImpl->getContext(),
Bill Wendling606c8e32013-01-29 03:20:31 +0000667 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000668 std::make_pair(Idx, getAttributes(Idx)))) :
669 AttributeSet();
670}
671
672AttributeSet AttributeSet::getRetAttributes() const {
673 return pImpl && hasAttributes(ReturnIndex) ?
674 AttributeSet::get(pImpl->getContext(),
Bill Wendling606c8e32013-01-29 03:20:31 +0000675 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000676 std::make_pair(ReturnIndex,
677 getAttributes(ReturnIndex)))) :
678 AttributeSet();
679}
680
681AttributeSet AttributeSet::getFnAttributes() const {
682 return pImpl && hasAttributes(FunctionIndex) ?
683 AttributeSet::get(pImpl->getContext(),
Bill Wendling606c8e32013-01-29 03:20:31 +0000684 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000685 std::make_pair(FunctionIndex,
686 getAttributes(FunctionIndex)))) :
687 AttributeSet();
688}
689
690bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{
Bill Wendling606c8e32013-01-29 03:20:31 +0000691 AttributeSetNode *ASN = getAttributes(Index);
692 return ASN ? ASN->hasAttribute(Kind) : false;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000693}
694
695bool AttributeSet::hasAttributes(unsigned Index) const {
Bill Wendling606c8e32013-01-29 03:20:31 +0000696 AttributeSetNode *ASN = getAttributes(Index);
697 return ASN ? ASN->hasAttributes() : false;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000698}
699
700/// \brief Return true if the specified attribute is set for at least one
701/// parameter or for the return value.
702bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr) const {
703 if (pImpl == 0) return false;
704
705 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
706 for (AttributeSetImpl::const_iterator II = pImpl->begin(I),
707 IE = pImpl->end(I); II != IE; ++II)
NAKAMURA Takumieddab152013-01-31 03:47:28 +0000708 if (II->hasAttribute(Attr))
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000709 return true;
710
711 return false;
712}
713
Bill Wendling606c8e32013-01-29 03:20:31 +0000714unsigned AttributeSet::getParamAlignment(unsigned Index) const {
715 AttributeSetNode *ASN = getAttributes(Index);
716 return ASN ? ASN->getAlignment() : 0;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000717}
718
719unsigned AttributeSet::getStackAlignment(unsigned Index) const {
Bill Wendling606c8e32013-01-29 03:20:31 +0000720 AttributeSetNode *ASN = getAttributes(Index);
721 return ASN ? ASN->getStackAlignment() : 0;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000722}
723
724std::string AttributeSet::getAsString(unsigned Index) const {
Bill Wendling606c8e32013-01-29 03:20:31 +0000725 AttributeSetNode *ASN = getAttributes(Index);
726 return ASN ? ASN->getAsString() : std::string("");
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000727}
728
729/// \brief The attributes for the specified index are returned.
Bill Wendling606c8e32013-01-29 03:20:31 +0000730AttributeSetNode *AttributeSet::getAttributes(unsigned Idx) const {
731 if (!pImpl) return 0;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000732
Bill Wendling606c8e32013-01-29 03:20:31 +0000733 // Loop through to find the attribute node we want.
734 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
735 if (pImpl->getSlotIndex(I) == Idx)
736 return pImpl->getSlotNode(I);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000737
Bill Wendling606c8e32013-01-29 03:20:31 +0000738 return 0;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000739}
740
741//===----------------------------------------------------------------------===//
742// AttributeSet Introspection Methods
743//===----------------------------------------------------------------------===//
744
745/// \brief Return the number of slots used in this attribute list. This is the
746/// number of arguments that have an attribute set on them (including the
747/// function itself).
748unsigned AttributeSet::getNumSlots() const {
749 return pImpl ? pImpl->getNumAttributes() : 0;
750}
751
752uint64_t AttributeSet::getSlotIndex(unsigned Slot) const {
753 assert(pImpl && Slot < pImpl->getNumAttributes() &&
754 "Slot # out of range!");
755 return pImpl->getSlotIndex(Slot);
756}
757
758AttributeSet AttributeSet::getSlotAttributes(unsigned Slot) const {
759 assert(pImpl && Slot < pImpl->getNumAttributes() &&
760 "Slot # out of range!");
761 return pImpl->getSlotAttributes(Slot);
762}
763
764uint64_t AttributeSet::Raw(unsigned Index) const {
765 // FIXME: Remove this.
766 return pImpl ? pImpl->Raw(Index) : 0;
767}
768
769void AttributeSet::dump() const {
770 dbgs() << "PAL[\n";
771
772 for (unsigned i = 0, e = getNumSlots(); i < e; ++i) {
773 uint64_t Index = getSlotIndex(i);
774 dbgs() << " { ";
775 if (Index == ~0U)
776 dbgs() << "~0U";
777 else
778 dbgs() << Index;
779 dbgs() << " => " << getAsString(Index) << " }\n";
780 }
781
782 dbgs() << "]\n";
783}
784
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000785//===----------------------------------------------------------------------===//
Bill Wendling03198882013-01-04 23:27:34 +0000786// AttrBuilder Method Implementations
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000787//===----------------------------------------------------------------------===//
788
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000789AttrBuilder::AttrBuilder(AttributeSet AS, unsigned Idx)
790 : Alignment(0), StackAlignment(0) {
Bill Wendlingec258982013-01-27 21:23:46 +0000791 AttributeSetImpl *pImpl = AS.pImpl;
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000792 if (!pImpl) return;
793
Bill Wendling73bc4522013-01-28 00:21:34 +0000794 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I) {
795 if (pImpl->getSlotIndex(I) != Idx) continue;
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000796
Bill Wendling383da6b2013-01-30 21:22:59 +0000797 for (AttributeSetImpl::const_iterator II = pImpl->begin(I),
Bill Wendling73bc4522013-01-28 00:21:34 +0000798 IE = pImpl->end(I); II != IE; ++II)
Bill Wendling383da6b2013-01-30 21:22:59 +0000799 addAttributes(*II);
Bill Wendling73bc4522013-01-28 00:21:34 +0000800
801 break;
802 }
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000803}
804
Bill Wendling03198882013-01-04 23:27:34 +0000805void AttrBuilder::clear() {
806 Attrs.clear();
807 Alignment = StackAlignment = 0;
808}
809
810AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
811 Attrs.insert(Val);
Bill Wendling3a106e62012-10-09 19:01:18 +0000812 return *this;
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000813}
814
Bill Wendling03198882013-01-04 23:27:34 +0000815AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
816 Attrs.erase(Val);
817 if (Val == Attribute::Alignment)
818 Alignment = 0;
819 else if (Val == Attribute::StackAlignment)
820 StackAlignment = 0;
821
Bill Wendlinga19a5302012-10-14 04:10:01 +0000822 return *this;
823}
824
Bill Wendling49f60602013-01-28 05:23:28 +0000825AttrBuilder &AttrBuilder::addAttributes(Attribute Attr) {
826 uint64_t Mask = Attr.Raw();
827
828 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
829 I = Attribute::AttrKind(I + 1))
830 if ((Mask & AttributeImpl::getAttrMask(I)) != 0)
831 Attrs.insert(I);
832
833 if (Attr.getAlignment())
834 Alignment = Attr.getAlignment();
835 if (Attr.getStackAlignment())
836 StackAlignment = Attr.getStackAlignment();
837 return *this;
838}
839
Bill Wendlinge7436542013-01-30 23:07:40 +0000840AttrBuilder &AttrBuilder::removeAttributes(AttributeSet A, uint64_t Index) {
841 uint64_t Mask = A.Raw(Index);
Bill Wendling49f60602013-01-28 05:23:28 +0000842
843 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
844 I = Attribute::AttrKind(I + 1)) {
845 if (Mask & AttributeImpl::getAttrMask(I)) {
846 Attrs.erase(I);
847
848 if (I == Attribute::Alignment)
849 Alignment = 0;
850 else if (I == Attribute::StackAlignment)
851 StackAlignment = 0;
852 }
853 }
854
855 return *this;
856}
857
Bill Wendling702cc912012-10-15 20:35:56 +0000858AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
Bill Wendlingda3f9d82012-10-14 03:58:29 +0000859 if (Align == 0) return *this;
Bill Wendling03198882013-01-04 23:27:34 +0000860
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000861 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
862 assert(Align <= 0x40000000 && "Alignment too large.");
Bill Wendling03198882013-01-04 23:27:34 +0000863
864 Attrs.insert(Attribute::Alignment);
865 Alignment = Align;
Bill Wendlingda3f9d82012-10-14 03:58:29 +0000866 return *this;
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000867}
868
Bill Wendling03198882013-01-04 23:27:34 +0000869AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
870 // Default alignment, allow the target to define how to align it.
871 if (Align == 0) return *this;
872
873 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
874 assert(Align <= 0x100 && "Alignment too large.");
875
876 Attrs.insert(Attribute::StackAlignment);
877 StackAlignment = Align;
878 return *this;
879}
880
Bill Wendling22bd6412013-01-03 01:54:39 +0000881bool AttrBuilder::contains(Attribute::AttrKind A) const {
Bill Wendling03198882013-01-04 23:27:34 +0000882 return Attrs.count(A);
Bill Wendling7d2f2492012-10-10 07:36:45 +0000883}
884
Bill Wendling702cc912012-10-15 20:35:56 +0000885bool AttrBuilder::hasAttributes() const {
Bill Wendling03198882013-01-04 23:27:34 +0000886 return !Attrs.empty();
Bill Wendlingf385f4c2012-10-08 23:27:46 +0000887}
Bill Wendling60507d52013-01-04 20:54:35 +0000888
Bill Wendlinge7436542013-01-30 23:07:40 +0000889bool AttrBuilder::hasAttributes(AttributeSet A, uint64_t Index) const {
890 return Raw() & A.Raw(Index);
Bill Wendling8831c062012-10-09 00:01:21 +0000891}
Bill Wendling60507d52013-01-04 20:54:35 +0000892
Bill Wendling702cc912012-10-15 20:35:56 +0000893bool AttrBuilder::hasAlignmentAttr() const {
Bill Wendling03198882013-01-04 23:27:34 +0000894 return Alignment != 0;
Bill Wendlingf385f4c2012-10-08 23:27:46 +0000895}
896
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000897bool AttrBuilder::operator==(const AttrBuilder &B) {
898 SmallVector<Attribute::AttrKind, 8> This(Attrs.begin(), Attrs.end());
899 SmallVector<Attribute::AttrKind, 8> That(B.Attrs.begin(), B.Attrs.end());
900 return This == That;
901}
902
903AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) {
Bill Wendling8232ece2013-01-29 01:43:29 +0000904 if (!Val) return *this;
905
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000906 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
907 I = Attribute::AttrKind(I + 1)) {
908 if (uint64_t A = (Val & AttributeImpl::getAttrMask(I))) {
909 Attrs.insert(I);
910
911 if (I == Attribute::Alignment)
912 Alignment = 1ULL << ((A >> 16) - 1);
913 else if (I == Attribute::StackAlignment)
914 StackAlignment = 1ULL << ((A >> 26)-1);
915 }
916 }
917
918 return *this;
919}
920
Bill Wendling1db9b692013-01-09 23:36:50 +0000921uint64_t AttrBuilder::Raw() const {
Bill Wendling03198882013-01-04 23:27:34 +0000922 uint64_t Mask = 0;
923
924 for (DenseSet<Attribute::AttrKind>::const_iterator I = Attrs.begin(),
925 E = Attrs.end(); I != E; ++I) {
926 Attribute::AttrKind Kind = *I;
927
928 if (Kind == Attribute::Alignment)
929 Mask |= (Log2_32(Alignment) + 1) << 16;
930 else if (Kind == Attribute::StackAlignment)
931 Mask |= (Log2_32(StackAlignment) + 1) << 26;
932 else
933 Mask |= AttributeImpl::getAttrMask(Kind);
934 }
935
936 return Mask;
Bill Wendlingf385f4c2012-10-08 23:27:46 +0000937}
938
Bill Wendling8e47daf2013-01-25 23:09:36 +0000939//===----------------------------------------------------------------------===//
940// AttributeFuncs Function Defintions
941//===----------------------------------------------------------------------===//
942
Bill Wendlinge7436542013-01-30 23:07:40 +0000943AttributeSet AttributeFuncs::typeIncompatible(Type *Ty, uint64_t Index) {
Bill Wendling8e47daf2013-01-25 23:09:36 +0000944 AttrBuilder Incompatible;
945
946 if (!Ty->isIntegerTy())
947 // Attribute that only apply to integers.
948 Incompatible.addAttribute(Attribute::SExt)
949 .addAttribute(Attribute::ZExt);
950
951 if (!Ty->isPointerTy())
952 // Attribute that only apply to pointers.
953 Incompatible.addAttribute(Attribute::ByVal)
954 .addAttribute(Attribute::Nest)
955 .addAttribute(Attribute::NoAlias)
956 .addAttribute(Attribute::NoCapture)
957 .addAttribute(Attribute::StructRet);
958
Bill Wendlinge7436542013-01-30 23:07:40 +0000959 return AttributeSet::get(Ty->getContext(), Index, Incompatible);
Bill Wendling8e47daf2013-01-25 23:09:36 +0000960}
961
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000962/// \brief This returns an integer containing an encoding of all the LLVM
963/// attributes found in the given attribute bitset. Any change to this encoding
964/// is a breaking change to bitcode compatibility.
Bill Wendling8232ece2013-01-29 01:43:29 +0000965/// N.B. This should be used only by the bitcode reader!
Bill Wendling8e47daf2013-01-25 23:09:36 +0000966uint64_t AttributeFuncs::encodeLLVMAttributesForBitcode(AttributeSet Attrs,
967 unsigned Index) {
968 // FIXME: It doesn't make sense to store the alignment information as an
969 // expanded out value, we should store it as a log2 value. However, we can't
970 // just change that here without breaking bitcode compatibility. If this ever
971 // becomes a problem in practice, we should introduce new tag numbers in the
972 // bitcode file and have those tags use a more efficiently encoded alignment
973 // field.
974
975 // Store the alignment in the bitcode as a 16-bit raw value instead of a 5-bit
976 // log2 encoded value. Shift the bits above the alignment up by 11 bits.
977 uint64_t EncodedAttrs = Attrs.Raw(Index) & 0xffff;
978 if (Attrs.hasAttribute(Index, Attribute::Alignment))
979 EncodedAttrs |= Attrs.getParamAlignment(Index) << 16;
980 EncodedAttrs |= (Attrs.Raw(Index) & (0xffffULL << 21)) << 11;
981 return EncodedAttrs;
982}
983
Bill Wendling8232ece2013-01-29 01:43:29 +0000984/// \brief This fills an AttrBuilder object with the LLVM attributes that have
985/// been decoded from the given integer. This function must stay in sync with
986/// 'encodeLLVMAttributesForBitcode'.
987/// N.B. This should be used only by the bitcode reader!
988void AttributeFuncs::decodeLLVMAttributesForBitcode(LLVMContext &C,
989 AttrBuilder &B,
990 uint64_t EncodedAttrs) {
Bill Wendling8e47daf2013-01-25 23:09:36 +0000991 // The alignment is stored as a 16-bit raw value from bits 31--16. We shift
992 // the bits above 31 down by 11 bits.
993 unsigned Alignment = (EncodedAttrs & (0xffffULL << 16)) >> 16;
994 assert((!Alignment || isPowerOf2_32(Alignment)) &&
995 "Alignment must be a power of two.");
996
Bill Wendling8e47daf2013-01-25 23:09:36 +0000997 if (Alignment)
998 B.addAlignmentAttr(Alignment);
Bill Wendling606c8e32013-01-29 03:20:31 +0000999 B.addRawValue(((EncodedAttrs & (0xffffULL << 32)) >> 11) |
1000 (EncodedAttrs & 0xffff));
Bill Wendling8e47daf2013-01-25 23:09:36 +00001001}