blob: d585843e90513a1a263b734dab82dabf7d76f99a [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 Wendlingbdcbccc2013-02-02 00:42:06 +000045 PA = new AttributeImpl(Context, Kind, Val);
Bill Wendling8e635db2012-10-08 21:47:17 +000046 pImpl->AttrsSet.InsertNode(PA, InsertPoint);
47 }
48
49 // Return the AttributesList that we found or created.
Bill Wendling034b94b2012-12-19 07:18:57 +000050 return Attribute(PA);
Bill Wendling8e635db2012-10-08 21:47:17 +000051}
52
Bill Wendling169d5272013-01-31 23:16:25 +000053Attribute Attribute::get(LLVMContext &Context, AttrKind Kind, Constant *Val) {
54 ConstantInt *KindVal = ConstantInt::get(Type::getInt64Ty(Context), Kind);
55 return get(Context, KindVal, Val);
56}
57
Bill Wendlingc08a5ef2013-01-27 22:43:04 +000058Attribute Attribute::getWithAlignment(LLVMContext &Context, uint64_t Align) {
Bill Wendling169d5272013-01-31 23:16:25 +000059 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
60 assert(Align <= 0x40000000 && "Alignment too large.");
61 return get(Context, Alignment,
62 ConstantInt::get(Type::getInt64Ty(Context), Align));
Bill Wendlingc08a5ef2013-01-27 22:43:04 +000063}
64
65Attribute Attribute::getWithStackAlignment(LLVMContext &Context,
66 uint64_t Align) {
Bill Wendling169d5272013-01-31 23:16:25 +000067 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
68 assert(Align <= 0x100 && "Alignment too large.");
69 return get(Context, StackAlignment,
70 ConstantInt::get(Type::getInt64Ty(Context), Align));
Bill Wendlingc08a5ef2013-01-27 22:43:04 +000071}
72
Bill Wendling817abdd2013-01-29 00:48:16 +000073//===----------------------------------------------------------------------===//
74// Attribute Accessor Methods
75//===----------------------------------------------------------------------===//
76
Bill Wendling629fb822012-12-22 00:37:52 +000077bool Attribute::hasAttribute(AttrKind Val) const {
Bill Wendling27107f62012-12-20 21:28:43 +000078 return pImpl && pImpl->hasAttribute(Val);
Bill Wendlinge66f3d32012-10-05 06:44:41 +000079}
80
Bill Wendling6dc37812013-01-29 20:45:34 +000081Constant *Attribute::getAttributeKind() const {
82 return pImpl ? pImpl->getAttributeKind() : 0;
83}
84
Bill Wendling5a4041e2013-02-01 22:32:30 +000085Constant *Attribute::getAttributeValues() const {
86 return pImpl ? pImpl->getAttributeValues() : 0;
Bill Wendling6dc37812013-01-29 20:45:34 +000087}
88
Bill Wendlinge66f3d32012-10-05 06:44:41 +000089/// This returns the alignment field of an attribute as a byte alignment value.
Bill Wendling034b94b2012-12-19 07:18:57 +000090unsigned Attribute::getAlignment() const {
Bill Wendling7beee282013-02-01 01:04:27 +000091 assert(hasAttribute(Attribute::Alignment) &&
92 "Trying to get alignment from non-alignment attribute!");
Bill Wendlinga8ab5fc2013-01-23 23:00:05 +000093 return pImpl->getAlignment();
Bill Wendlinge66f3d32012-10-05 06:44:41 +000094}
95
96/// This returns the stack alignment field of an attribute as a byte alignment
97/// value.
Bill Wendling034b94b2012-12-19 07:18:57 +000098unsigned Attribute::getStackAlignment() const {
Bill Wendling7beee282013-02-01 01:04:27 +000099 assert(hasAttribute(Attribute::StackAlignment) &&
100 "Trying to get alignment from non-alignment attribute!");
Bill Wendlinga8ab5fc2013-01-23 23:00:05 +0000101 return pImpl->getStackAlignment();
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000102}
103
Bill Wendling034b94b2012-12-19 07:18:57 +0000104std::string Attribute::getAsString() const {
Bill Wendling14292a62013-01-31 20:59:05 +0000105 if (!pImpl) return "";
106
107 if (hasAttribute(Attribute::AddressSafety))
108 return "address_safety";
109 if (hasAttribute(Attribute::AlwaysInline))
110 return "alwaysinline";
111 if (hasAttribute(Attribute::ByVal))
112 return "byval";
113 if (hasAttribute(Attribute::InlineHint))
114 return "inlinehint";
Bill Wendling034b94b2012-12-19 07:18:57 +0000115 if (hasAttribute(Attribute::InReg))
Bill Wendling606c8e32013-01-29 03:20:31 +0000116 return "inreg";
Bill Wendling14292a62013-01-31 20:59:05 +0000117 if (hasAttribute(Attribute::MinSize))
118 return "minsize";
119 if (hasAttribute(Attribute::Naked))
120 return "naked";
121 if (hasAttribute(Attribute::Nest))
122 return "nest";
Bill Wendling034b94b2012-12-19 07:18:57 +0000123 if (hasAttribute(Attribute::NoAlias))
Bill Wendling606c8e32013-01-29 03:20:31 +0000124 return "noalias";
Bill Wendling034b94b2012-12-19 07:18:57 +0000125 if (hasAttribute(Attribute::NoCapture))
Bill Wendling606c8e32013-01-29 03:20:31 +0000126 return "nocapture";
Bill Wendling14292a62013-01-31 20:59:05 +0000127 if (hasAttribute(Attribute::NoDuplicate))
128 return "noduplicate";
129 if (hasAttribute(Attribute::NoImplicitFloat))
130 return "noimplicitfloat";
131 if (hasAttribute(Attribute::NoInline))
132 return "noinline";
133 if (hasAttribute(Attribute::NonLazyBind))
134 return "nonlazybind";
135 if (hasAttribute(Attribute::NoRedZone))
136 return "noredzone";
137 if (hasAttribute(Attribute::NoReturn))
138 return "noreturn";
139 if (hasAttribute(Attribute::NoUnwind))
140 return "nounwind";
141 if (hasAttribute(Attribute::OptimizeForSize))
142 return "optsize";
Bill Wendling034b94b2012-12-19 07:18:57 +0000143 if (hasAttribute(Attribute::ReadNone))
Bill Wendling606c8e32013-01-29 03:20:31 +0000144 return "readnone";
Bill Wendling034b94b2012-12-19 07:18:57 +0000145 if (hasAttribute(Attribute::ReadOnly))
Bill Wendling606c8e32013-01-29 03:20:31 +0000146 return "readonly";
Bill Wendling14292a62013-01-31 20:59:05 +0000147 if (hasAttribute(Attribute::ReturnsTwice))
148 return "returns_twice";
149 if (hasAttribute(Attribute::SExt))
150 return "signext";
Bill Wendling034b94b2012-12-19 07:18:57 +0000151 if (hasAttribute(Attribute::StackProtect))
Bill Wendling606c8e32013-01-29 03:20:31 +0000152 return "ssp";
Bill Wendling034b94b2012-12-19 07:18:57 +0000153 if (hasAttribute(Attribute::StackProtectReq))
Bill Wendling606c8e32013-01-29 03:20:31 +0000154 return "sspreq";
Bill Wendling114baee2013-01-23 06:41:41 +0000155 if (hasAttribute(Attribute::StackProtectStrong))
Bill Wendling606c8e32013-01-29 03:20:31 +0000156 return "sspstrong";
Bill Wendling14292a62013-01-31 20:59:05 +0000157 if (hasAttribute(Attribute::StructRet))
158 return "sret";
159 if (hasAttribute(Attribute::UWTable))
160 return "uwtable";
161 if (hasAttribute(Attribute::ZExt))
162 return "zeroext";
163
164 // FIXME: These should be output like this:
165 //
166 // align=4
167 // alignstack=8
168 //
Bill Wendling034b94b2012-12-19 07:18:57 +0000169 if (hasAttribute(Attribute::StackAlignment)) {
Bill Wendling606c8e32013-01-29 03:20:31 +0000170 std::string Result;
Charles Davis1e063d12010-02-12 00:31:15 +0000171 Result += "alignstack(";
Bill Wendlingef99fe82012-09-21 15:26:31 +0000172 Result += utostr(getStackAlignment());
Bill Wendling606c8e32013-01-29 03:20:31 +0000173 Result += ")";
174 return Result;
Charles Davis1e063d12010-02-12 00:31:15 +0000175 }
Bill Wendling034b94b2012-12-19 07:18:57 +0000176 if (hasAttribute(Attribute::Alignment)) {
Bill Wendling606c8e32013-01-29 03:20:31 +0000177 std::string Result;
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000178 Result += "align ";
Bill Wendlingef99fe82012-09-21 15:26:31 +0000179 Result += utostr(getAlignment());
Bill Wendling606c8e32013-01-29 03:20:31 +0000180 return Result;
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000181 }
Bill Wendling14292a62013-01-31 20:59:05 +0000182
183 // Convert target-dependent attributes to strings of the form:
184 //
185 // "kind"
186 // "kind" = "value"
Bill Wendling5a4041e2013-02-01 22:32:30 +0000187 // "kind" = ( "value1" "value2" "value3" )
Bill Wendling14292a62013-01-31 20:59:05 +0000188 //
189 if (ConstantDataArray *CDA =
190 dyn_cast<ConstantDataArray>(pImpl->getAttributeKind())) {
191 std::string Result;
192 Result += '\"' + CDA->getAsString().str() + '"';
193
Bill Wendling5a4041e2013-02-01 22:32:30 +0000194 Constant *Vals = pImpl->getAttributeValues();
195 if (!Vals) return Result;
196
197 // FIXME: This should support more than just ConstantDataArrays. Also,
198 // support a vector of attribute values.
199
Bill Wendling14292a62013-01-31 20:59:05 +0000200 Result += " = ";
Bill Wendling5a4041e2013-02-01 22:32:30 +0000201 Result += '\"' + cast<ConstantDataArray>(Vals)->getAsString().str() + '"';
202
Bill Wendling7beee282013-02-01 01:04:27 +0000203 return Result;
Bill Wendling14292a62013-01-31 20:59:05 +0000204 }
Bill Wendling606c8e32013-01-29 03:20:31 +0000205
206 llvm_unreachable("Unknown attribute");
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000207}
208
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000209bool Attribute::operator==(AttrKind K) const {
Bill Wendling14292a62013-01-31 20:59:05 +0000210 return (pImpl && *pImpl == K) || (!pImpl && K == None);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000211}
212bool Attribute::operator!=(AttrKind K) const {
213 return !(*this == K);
214}
215
216bool Attribute::operator<(Attribute A) const {
217 if (!pImpl && !A.pImpl) return false;
218 if (!pImpl) return true;
219 if (!A.pImpl) return false;
220 return *pImpl < *A.pImpl;
221}
222
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000223//===----------------------------------------------------------------------===//
224// AttributeImpl Definition
225//===----------------------------------------------------------------------===//
226
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000227bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
Bill Wendling169d5272013-01-31 23:16:25 +0000228 if (ConstantInt *CI = dyn_cast<ConstantInt>(Kind))
229 return CI->getZExtValue() == A;
230 return false;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000231}
232
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000233uint64_t AttributeImpl::getAlignment() const {
Bill Wendling169d5272013-01-31 23:16:25 +0000234 assert(hasAttribute(Attribute::Alignment) &&
235 "Trying to retrieve the alignment from a non-alignment attr!");
Bill Wendling5a4041e2013-02-01 22:32:30 +0000236 return cast<ConstantInt>(Values)->getZExtValue();
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000237}
238
239uint64_t AttributeImpl::getStackAlignment() const {
Bill Wendling169d5272013-01-31 23:16:25 +0000240 assert(hasAttribute(Attribute::StackAlignment) &&
241 "Trying to retrieve the stack alignment from a non-alignment attr!");
Bill Wendling5a4041e2013-02-01 22:32:30 +0000242 return cast<ConstantInt>(Values)->getZExtValue();
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000243}
244
Bill Wendling9f175f82013-01-29 20:37:10 +0000245bool AttributeImpl::operator==(Attribute::AttrKind kind) const {
246 if (ConstantInt *CI = dyn_cast<ConstantInt>(Kind))
247 return CI->getZExtValue() == kind;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000248 return false;
249}
Bill Wendling9f175f82013-01-29 20:37:10 +0000250bool AttributeImpl::operator!=(Attribute::AttrKind kind) const {
251 return !(*this == kind);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000252}
253
Bill Wendling9f175f82013-01-29 20:37:10 +0000254bool AttributeImpl::operator==(StringRef kind) const {
255 if (ConstantDataArray *CDA = dyn_cast<ConstantDataArray>(Kind))
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000256 if (CDA->isString())
Bill Wendling9f175f82013-01-29 20:37:10 +0000257 return CDA->getAsString() == kind;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000258 return false;
259}
260
Bill Wendling9f175f82013-01-29 20:37:10 +0000261bool AttributeImpl::operator!=(StringRef kind) const {
262 return !(*this == kind);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000263}
264
265bool AttributeImpl::operator<(const AttributeImpl &AI) const {
Bill Wendling7beee282013-02-01 01:04:27 +0000266 // This sorts the attributes with Attribute::AttrKinds coming first (sorted
267 // relative to their enum value) and then strings.
268
Bill Wendling9f175f82013-01-29 20:37:10 +0000269 if (!Kind && !AI.Kind) return false;
270 if (!Kind && AI.Kind) return true;
271 if (Kind && !AI.Kind) return false;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000272
Bill Wendling9f175f82013-01-29 20:37:10 +0000273 ConstantInt *ThisCI = dyn_cast<ConstantInt>(Kind);
274 ConstantInt *ThatCI = dyn_cast<ConstantInt>(AI.Kind);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000275
Bill Wendling9f175f82013-01-29 20:37:10 +0000276 ConstantDataArray *ThisCDA = dyn_cast<ConstantDataArray>(Kind);
277 ConstantDataArray *ThatCDA = dyn_cast<ConstantDataArray>(AI.Kind);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000278
279 if (ThisCI && ThatCI)
280 return ThisCI->getZExtValue() < ThatCI->getZExtValue();
281
282 if (ThisCI && ThatCDA)
283 return true;
284
285 if (ThisCDA && ThatCI)
286 return false;
287
288 return ThisCDA->getAsString() < ThatCDA->getAsString();
289}
290
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000291uint64_t AttributeImpl::getAttrMask(Attribute::AttrKind Val) {
292 // FIXME: Remove this.
293 switch (Val) {
294 case Attribute::EndAttrKinds:
295 case Attribute::AttrKindEmptyKey:
296 case Attribute::AttrKindTombstoneKey:
297 llvm_unreachable("Synthetic enumerators which should never get here");
298
299 case Attribute::None: return 0;
300 case Attribute::ZExt: return 1 << 0;
301 case Attribute::SExt: return 1 << 1;
302 case Attribute::NoReturn: return 1 << 2;
303 case Attribute::InReg: return 1 << 3;
304 case Attribute::StructRet: return 1 << 4;
305 case Attribute::NoUnwind: return 1 << 5;
306 case Attribute::NoAlias: return 1 << 6;
307 case Attribute::ByVal: return 1 << 7;
308 case Attribute::Nest: return 1 << 8;
309 case Attribute::ReadNone: return 1 << 9;
310 case Attribute::ReadOnly: return 1 << 10;
311 case Attribute::NoInline: return 1 << 11;
312 case Attribute::AlwaysInline: return 1 << 12;
313 case Attribute::OptimizeForSize: return 1 << 13;
314 case Attribute::StackProtect: return 1 << 14;
315 case Attribute::StackProtectReq: return 1 << 15;
316 case Attribute::Alignment: return 31 << 16;
317 case Attribute::NoCapture: return 1 << 21;
318 case Attribute::NoRedZone: return 1 << 22;
319 case Attribute::NoImplicitFloat: return 1 << 23;
320 case Attribute::Naked: return 1 << 24;
321 case Attribute::InlineHint: return 1 << 25;
322 case Attribute::StackAlignment: return 7 << 26;
323 case Attribute::ReturnsTwice: return 1 << 29;
324 case Attribute::UWTable: return 1 << 30;
325 case Attribute::NonLazyBind: return 1U << 31;
326 case Attribute::AddressSafety: return 1ULL << 32;
327 case Attribute::MinSize: return 1ULL << 33;
328 case Attribute::NoDuplicate: return 1ULL << 34;
329 case Attribute::StackProtectStrong: return 1ULL << 35;
330 }
331 llvm_unreachable("Unsupported attribute type");
332}
333
334//===----------------------------------------------------------------------===//
335// AttributeSetNode Definition
336//===----------------------------------------------------------------------===//
337
338AttributeSetNode *AttributeSetNode::get(LLVMContext &C,
339 ArrayRef<Attribute> Attrs) {
340 if (Attrs.empty())
341 return 0;
342
343 // Otherwise, build a key to look up the existing attributes.
344 LLVMContextImpl *pImpl = C.pImpl;
345 FoldingSetNodeID ID;
346
347 SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end());
348 std::sort(SortedAttrs.begin(), SortedAttrs.end());
349
350 for (SmallVectorImpl<Attribute>::iterator I = SortedAttrs.begin(),
351 E = SortedAttrs.end(); I != E; ++I)
352 I->Profile(ID);
353
354 void *InsertPoint;
355 AttributeSetNode *PA =
356 pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint);
357
358 // If we didn't find any existing attributes of the same shape then create a
359 // new one and insert it.
360 if (!PA) {
361 PA = new AttributeSetNode(SortedAttrs);
362 pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint);
363 }
364
365 // Return the AttributesListNode that we found or created.
366 return PA;
367}
368
Bill Wendling606c8e32013-01-29 03:20:31 +0000369bool AttributeSetNode::hasAttribute(Attribute::AttrKind Kind) const {
370 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
371 E = AttrList.end(); I != E; ++I)
NAKAMURA Takumieddab152013-01-31 03:47:28 +0000372 if (I->hasAttribute(Kind))
Bill Wendling606c8e32013-01-29 03:20:31 +0000373 return true;
374 return false;
375}
376
377unsigned AttributeSetNode::getAlignment() const {
378 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
379 E = AttrList.end(); I != E; ++I)
NAKAMURA Takumieddab152013-01-31 03:47:28 +0000380 if (I->hasAttribute(Attribute::Alignment))
Bill Wendling606c8e32013-01-29 03:20:31 +0000381 return I->getAlignment();
382 return 0;
383}
384
385unsigned AttributeSetNode::getStackAlignment() const {
386 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
387 E = AttrList.end(); I != E; ++I)
NAKAMURA Takumieddab152013-01-31 03:47:28 +0000388 if (I->hasAttribute(Attribute::StackAlignment))
Bill Wendling606c8e32013-01-29 03:20:31 +0000389 return I->getStackAlignment();
390 return 0;
391}
392
393std::string AttributeSetNode::getAsString() const {
394 std::string Str = "";
395 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
Bill Wendling7beee282013-02-01 01:04:27 +0000396 E = AttrList.end(); I != E; ) {
Bill Wendling606c8e32013-01-29 03:20:31 +0000397 Str += I->getAsString();
Bill Wendling7beee282013-02-01 01:04:27 +0000398 if (++I != E) Str += " ";
Bill Wendling606c8e32013-01-29 03:20:31 +0000399 }
400 return Str;
401}
402
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000403//===----------------------------------------------------------------------===//
404// AttributeSetImpl Definition
405//===----------------------------------------------------------------------===//
406
407uint64_t AttributeSetImpl::Raw(uint64_t Index) const {
408 for (unsigned I = 0, E = getNumAttributes(); I != E; ++I) {
409 if (getSlotIndex(I) != Index) continue;
410 const AttributeSetNode *ASN = AttrNodes[I].second;
411 AttrBuilder B;
412
413 for (AttributeSetNode::const_iterator II = ASN->begin(),
414 IE = ASN->end(); II != IE; ++II)
Bill Wendling39da0782013-01-31 23:38:01 +0000415 B.addAttribute(*II);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000416 return B.Raw();
417 }
418
419 return 0;
420}
421
422//===----------------------------------------------------------------------===//
423// AttributeSet Construction and Mutation Methods
424//===----------------------------------------------------------------------===//
425
Bill Wendling8232ece2013-01-29 01:43:29 +0000426AttributeSet
427AttributeSet::getImpl(LLVMContext &C,
428 ArrayRef<std::pair<unsigned, AttributeSetNode*> > Attrs) {
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000429 LLVMContextImpl *pImpl = C.pImpl;
430 FoldingSetNodeID ID;
431 AttributeSetImpl::Profile(ID, Attrs);
432
433 void *InsertPoint;
434 AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
435
436 // If we didn't find any existing attributes of the same shape then
437 // create a new one and insert it.
438 if (!PA) {
439 PA = new AttributeSetImpl(C, Attrs);
440 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
441 }
442
443 // Return the AttributesList that we found or created.
444 return AttributeSet(PA);
445}
446
447AttributeSet AttributeSet::get(LLVMContext &C,
448 ArrayRef<std::pair<unsigned, Attribute> > Attrs){
449 // If there are no attributes then return a null AttributesList pointer.
450 if (Attrs.empty())
451 return AttributeSet();
452
453#ifndef NDEBUG
454 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
455 assert((!i || Attrs[i-1].first <= Attrs[i].first) &&
456 "Misordered Attributes list!");
Bill Wendling82aea642013-01-31 06:22:35 +0000457 assert(Attrs[i].second != Attribute::None &&
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000458 "Pointless attribute!");
459 }
460#endif
461
462 // Create a vector if (unsigned, AttributeSetNode*) pairs from the attributes
463 // list.
464 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrPairVec;
465 for (ArrayRef<std::pair<unsigned, Attribute> >::iterator I = Attrs.begin(),
466 E = Attrs.end(); I != E; ) {
467 unsigned Index = I->first;
468 SmallVector<Attribute, 4> AttrVec;
NAKAMURA Takumi3ba51ce2013-01-29 15:18:16 +0000469 while (I != E && I->first == Index) {
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000470 AttrVec.push_back(I->second);
471 ++I;
472 }
473
474 AttrPairVec.push_back(std::make_pair(Index,
475 AttributeSetNode::get(C, AttrVec)));
476 }
477
478 return getImpl(C, AttrPairVec);
479}
480
481AttributeSet AttributeSet::get(LLVMContext &C,
482 ArrayRef<std::pair<unsigned,
483 AttributeSetNode*> > Attrs) {
484 // If there are no attributes then return a null AttributesList pointer.
485 if (Attrs.empty())
486 return AttributeSet();
487
488 return getImpl(C, Attrs);
489}
490
491AttributeSet AttributeSet::get(LLVMContext &C, unsigned Idx, AttrBuilder &B) {
492 if (!B.hasAttributes())
493 return AttributeSet();
Bill Wendling8fbc0c22013-01-29 01:02:03 +0000494
495 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
496 for (AttrBuilder::iterator I = B.begin(), E = B.end(); I != E; ++I) {
497 Attribute::AttrKind Kind = *I;
498 if (Kind == Attribute::Alignment)
499 Attrs.push_back(std::make_pair(Idx, Attribute::
500 getWithAlignment(C, B.getAlignment())));
501 else if (Kind == Attribute::StackAlignment)
502 Attrs.push_back(std::make_pair(Idx, Attribute::
503 getWithStackAlignment(C, B.getStackAlignment())));
504 else
505 Attrs.push_back(std::make_pair(Idx, Attribute::get(C, Kind)));
506 }
507
508 return get(C, Attrs);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000509}
510
511AttributeSet AttributeSet::get(LLVMContext &C, unsigned Idx,
512 ArrayRef<Attribute::AttrKind> Kind) {
513 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
514 for (ArrayRef<Attribute::AttrKind>::iterator I = Kind.begin(),
515 E = Kind.end(); I != E; ++I)
516 Attrs.push_back(std::make_pair(Idx, Attribute::get(C, *I)));
517 return get(C, Attrs);
518}
519
520AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<AttributeSet> Attrs) {
521 if (Attrs.empty()) return AttributeSet();
522
523 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrNodeVec;
524 for (unsigned I = 0, E = Attrs.size(); I != E; ++I) {
525 AttributeSet AS = Attrs[I];
526 if (!AS.pImpl) continue;
527 AttrNodeVec.append(AS.pImpl->AttrNodes.begin(), AS.pImpl->AttrNodes.end());
528 }
529
530 return getImpl(C, AttrNodeVec);
531}
532
533AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Idx,
534 Attribute::AttrKind Attr) const {
535 return addAttributes(C, Idx, AttributeSet::get(C, Idx, Attr));
536}
537
538AttributeSet AttributeSet::addAttributes(LLVMContext &C, unsigned Idx,
539 AttributeSet Attrs) const {
540 if (!pImpl) return Attrs;
541 if (!Attrs.pImpl) return *this;
542
543#ifndef NDEBUG
544 // FIXME it is not obvious how this should work for alignment. For now, say
545 // we can't change a known alignment.
546 unsigned OldAlign = getParamAlignment(Idx);
547 unsigned NewAlign = Attrs.getParamAlignment(Idx);
548 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
549 "Attempt to change alignment!");
550#endif
551
552 // Add the attribute slots before the one we're trying to add.
553 SmallVector<AttributeSet, 4> AttrSet;
554 uint64_t NumAttrs = pImpl->getNumAttributes();
555 AttributeSet AS;
556 uint64_t LastIndex = 0;
557 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
558 if (getSlotIndex(I) >= Idx) {
559 if (getSlotIndex(I) == Idx) AS = getSlotAttributes(LastIndex++);
560 break;
561 }
562 LastIndex = I + 1;
563 AttrSet.push_back(getSlotAttributes(I));
564 }
565
566 // Now add the attribute into the correct slot. There may already be an
567 // AttributeSet there.
568 AttrBuilder B(AS, Idx);
569
570 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
571 if (Attrs.getSlotIndex(I) == Idx) {
572 for (AttributeSetImpl::const_iterator II = Attrs.pImpl->begin(I),
573 IE = Attrs.pImpl->end(I); II != IE; ++II)
Bill Wendling39da0782013-01-31 23:38:01 +0000574 B.addAttribute(*II);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000575 break;
576 }
577
578 AttrSet.push_back(AttributeSet::get(C, Idx, B));
579
580 // Add the remaining attribute slots.
581 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
582 AttrSet.push_back(getSlotAttributes(I));
583
584 return get(C, AttrSet);
585}
586
587AttributeSet AttributeSet::removeAttribute(LLVMContext &C, unsigned Idx,
588 Attribute::AttrKind Attr) const {
589 return removeAttributes(C, Idx, AttributeSet::get(C, Idx, Attr));
590}
591
592AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Idx,
593 AttributeSet Attrs) const {
594 if (!pImpl) return AttributeSet();
595 if (!Attrs.pImpl) return *this;
596
597#ifndef NDEBUG
598 // FIXME it is not obvious how this should work for alignment.
599 // For now, say we can't pass in alignment, which no current use does.
600 assert(!Attrs.hasAttribute(Idx, Attribute::Alignment) &&
601 "Attempt to change alignment!");
602#endif
603
604 // Add the attribute slots before the one we're trying to add.
605 SmallVector<AttributeSet, 4> AttrSet;
606 uint64_t NumAttrs = pImpl->getNumAttributes();
607 AttributeSet AS;
608 uint64_t LastIndex = 0;
609 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
610 if (getSlotIndex(I) >= Idx) {
611 if (getSlotIndex(I) == Idx) AS = getSlotAttributes(LastIndex++);
612 break;
613 }
614 LastIndex = I + 1;
615 AttrSet.push_back(getSlotAttributes(I));
616 }
617
Bill Wendlinge7436542013-01-30 23:07:40 +0000618 // Now remove the attribute from the correct slot. There may already be an
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000619 // AttributeSet there.
620 AttrBuilder B(AS, Idx);
621
622 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
623 if (Attrs.getSlotIndex(I) == Idx) {
Bill Wendlinge7436542013-01-30 23:07:40 +0000624 B.removeAttributes(Attrs.pImpl->getSlotAttributes(I), Idx);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000625 break;
626 }
627
628 AttrSet.push_back(AttributeSet::get(C, Idx, B));
629
630 // Add the remaining attribute slots.
631 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
632 AttrSet.push_back(getSlotAttributes(I));
633
634 return get(C, AttrSet);
635}
636
637//===----------------------------------------------------------------------===//
638// AttributeSet Accessor Methods
639//===----------------------------------------------------------------------===//
640
641AttributeSet AttributeSet::getParamAttributes(unsigned Idx) const {
642 return pImpl && hasAttributes(Idx) ?
643 AttributeSet::get(pImpl->getContext(),
Bill Wendling606c8e32013-01-29 03:20:31 +0000644 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000645 std::make_pair(Idx, getAttributes(Idx)))) :
646 AttributeSet();
647}
648
649AttributeSet AttributeSet::getRetAttributes() const {
650 return pImpl && hasAttributes(ReturnIndex) ?
651 AttributeSet::get(pImpl->getContext(),
Bill Wendling606c8e32013-01-29 03:20:31 +0000652 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000653 std::make_pair(ReturnIndex,
654 getAttributes(ReturnIndex)))) :
655 AttributeSet();
656}
657
658AttributeSet AttributeSet::getFnAttributes() const {
659 return pImpl && hasAttributes(FunctionIndex) ?
660 AttributeSet::get(pImpl->getContext(),
Bill Wendling606c8e32013-01-29 03:20:31 +0000661 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000662 std::make_pair(FunctionIndex,
663 getAttributes(FunctionIndex)))) :
664 AttributeSet();
665}
666
667bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{
Bill Wendling606c8e32013-01-29 03:20:31 +0000668 AttributeSetNode *ASN = getAttributes(Index);
669 return ASN ? ASN->hasAttribute(Kind) : false;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000670}
671
672bool AttributeSet::hasAttributes(unsigned Index) const {
Bill Wendling606c8e32013-01-29 03:20:31 +0000673 AttributeSetNode *ASN = getAttributes(Index);
674 return ASN ? ASN->hasAttributes() : false;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000675}
676
677/// \brief Return true if the specified attribute is set for at least one
678/// parameter or for the return value.
679bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr) const {
680 if (pImpl == 0) return false;
681
682 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
683 for (AttributeSetImpl::const_iterator II = pImpl->begin(I),
684 IE = pImpl->end(I); II != IE; ++II)
NAKAMURA Takumieddab152013-01-31 03:47:28 +0000685 if (II->hasAttribute(Attr))
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000686 return true;
687
688 return false;
689}
690
Bill Wendling606c8e32013-01-29 03:20:31 +0000691unsigned AttributeSet::getParamAlignment(unsigned Index) const {
692 AttributeSetNode *ASN = getAttributes(Index);
693 return ASN ? ASN->getAlignment() : 0;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000694}
695
696unsigned AttributeSet::getStackAlignment(unsigned Index) const {
Bill Wendling606c8e32013-01-29 03:20:31 +0000697 AttributeSetNode *ASN = getAttributes(Index);
698 return ASN ? ASN->getStackAlignment() : 0;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000699}
700
701std::string AttributeSet::getAsString(unsigned Index) const {
Bill Wendling606c8e32013-01-29 03:20:31 +0000702 AttributeSetNode *ASN = getAttributes(Index);
703 return ASN ? ASN->getAsString() : std::string("");
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000704}
705
706/// \brief The attributes for the specified index are returned.
Bill Wendling606c8e32013-01-29 03:20:31 +0000707AttributeSetNode *AttributeSet::getAttributes(unsigned Idx) const {
708 if (!pImpl) return 0;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000709
Bill Wendling606c8e32013-01-29 03:20:31 +0000710 // Loop through to find the attribute node we want.
711 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
712 if (pImpl->getSlotIndex(I) == Idx)
713 return pImpl->getSlotNode(I);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000714
Bill Wendling606c8e32013-01-29 03:20:31 +0000715 return 0;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000716}
717
Bill Wendlingf715dbd2013-02-01 00:48:14 +0000718AttributeSet::iterator AttributeSet::begin(unsigned Idx) const {
Bill Wendling16c4b3c2013-01-31 23:53:05 +0000719 if (!pImpl)
720 return ArrayRef<Attribute>().begin();
721 return pImpl->begin(Idx);
722}
723
Bill Wendlingf715dbd2013-02-01 00:48:14 +0000724AttributeSet::iterator AttributeSet::end(unsigned Idx) const {
Bill Wendling16c4b3c2013-01-31 23:53:05 +0000725 if (!pImpl)
726 return ArrayRef<Attribute>().end();
Bill Wendling30d2c762013-02-01 00:13:50 +0000727 return pImpl->end(Idx);
Bill Wendling16c4b3c2013-01-31 23:53:05 +0000728}
729
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000730//===----------------------------------------------------------------------===//
731// AttributeSet Introspection Methods
732//===----------------------------------------------------------------------===//
733
734/// \brief Return the number of slots used in this attribute list. This is the
735/// number of arguments that have an attribute set on them (including the
736/// function itself).
737unsigned AttributeSet::getNumSlots() const {
738 return pImpl ? pImpl->getNumAttributes() : 0;
739}
740
741uint64_t AttributeSet::getSlotIndex(unsigned Slot) const {
742 assert(pImpl && Slot < pImpl->getNumAttributes() &&
743 "Slot # out of range!");
744 return pImpl->getSlotIndex(Slot);
745}
746
747AttributeSet AttributeSet::getSlotAttributes(unsigned Slot) const {
748 assert(pImpl && Slot < pImpl->getNumAttributes() &&
749 "Slot # out of range!");
750 return pImpl->getSlotAttributes(Slot);
751}
752
753uint64_t AttributeSet::Raw(unsigned Index) const {
754 // FIXME: Remove this.
755 return pImpl ? pImpl->Raw(Index) : 0;
756}
757
758void AttributeSet::dump() const {
759 dbgs() << "PAL[\n";
760
761 for (unsigned i = 0, e = getNumSlots(); i < e; ++i) {
762 uint64_t Index = getSlotIndex(i);
763 dbgs() << " { ";
764 if (Index == ~0U)
765 dbgs() << "~0U";
766 else
767 dbgs() << Index;
768 dbgs() << " => " << getAsString(Index) << " }\n";
769 }
770
771 dbgs() << "]\n";
772}
773
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000774//===----------------------------------------------------------------------===//
Bill Wendling03198882013-01-04 23:27:34 +0000775// AttrBuilder Method Implementations
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000776//===----------------------------------------------------------------------===//
777
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000778AttrBuilder::AttrBuilder(AttributeSet AS, unsigned Idx)
779 : Alignment(0), StackAlignment(0) {
Bill Wendlingec258982013-01-27 21:23:46 +0000780 AttributeSetImpl *pImpl = AS.pImpl;
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000781 if (!pImpl) return;
782
Bill Wendling73bc4522013-01-28 00:21:34 +0000783 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I) {
784 if (pImpl->getSlotIndex(I) != Idx) continue;
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000785
Bill Wendling383da6b2013-01-30 21:22:59 +0000786 for (AttributeSetImpl::const_iterator II = pImpl->begin(I),
Bill Wendling73bc4522013-01-28 00:21:34 +0000787 IE = pImpl->end(I); II != IE; ++II)
Bill Wendling39da0782013-01-31 23:38:01 +0000788 addAttribute(*II);
Bill Wendling73bc4522013-01-28 00:21:34 +0000789
790 break;
791 }
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000792}
793
Bill Wendling03198882013-01-04 23:27:34 +0000794void AttrBuilder::clear() {
795 Attrs.clear();
796 Alignment = StackAlignment = 0;
797}
798
799AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
Bill Wendling169d5272013-01-31 23:16:25 +0000800 assert(Val != Attribute::Alignment && Val != Attribute::StackAlignment &&
801 "Adding alignment attribute without adding alignment value!");
Bill Wendling03198882013-01-04 23:27:34 +0000802 Attrs.insert(Val);
Bill Wendling3a106e62012-10-09 19:01:18 +0000803 return *this;
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000804}
805
Bill Wendling39da0782013-01-31 23:38:01 +0000806AttrBuilder &AttrBuilder::addAttribute(Attribute Attr) {
Bill Wendling169d5272013-01-31 23:16:25 +0000807 ConstantInt *Kind = cast<ConstantInt>(Attr.getAttributeKind());
808 Attribute::AttrKind KindVal = Attribute::AttrKind(Kind->getZExtValue());
809 Attrs.insert(KindVal);
Bill Wendling49f60602013-01-28 05:23:28 +0000810
Bill Wendling169d5272013-01-31 23:16:25 +0000811 if (KindVal == Attribute::Alignment)
Bill Wendling49f60602013-01-28 05:23:28 +0000812 Alignment = Attr.getAlignment();
Bill Wendling169d5272013-01-31 23:16:25 +0000813 else if (KindVal == Attribute::StackAlignment)
Bill Wendling49f60602013-01-28 05:23:28 +0000814 StackAlignment = Attr.getStackAlignment();
815 return *this;
816}
817
Bill Wendling39da0782013-01-31 23:38:01 +0000818AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
819 Attrs.erase(Val);
820
821 if (Val == Attribute::Alignment)
822 Alignment = 0;
823 else if (Val == Attribute::StackAlignment)
824 StackAlignment = 0;
825
826 return *this;
827}
828
Bill Wendlinge7436542013-01-30 23:07:40 +0000829AttrBuilder &AttrBuilder::removeAttributes(AttributeSet A, uint64_t Index) {
Bill Wendling30d2c762013-02-01 00:13:50 +0000830 unsigned Idx = ~0U;
831 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
832 if (A.getSlotIndex(I) == Index) {
833 Idx = I;
834 break;
Bill Wendling49f60602013-01-28 05:23:28 +0000835 }
Bill Wendling30d2c762013-02-01 00:13:50 +0000836
837 assert(Idx != ~0U && "Couldn't find index in AttributeSet!");
838
839 for (AttributeSet::iterator I = A.begin(Idx), E = A.end(Idx); I != E; ++I) {
840 ConstantInt *CI = cast<ConstantInt>(I->getAttributeKind());
841 Attribute::AttrKind Kind = Attribute::AttrKind(CI->getZExtValue());
842 Attrs.erase(Kind);
843
844 if (Kind == Attribute::Alignment)
845 Alignment = 0;
846 else if (Kind == Attribute::StackAlignment)
847 StackAlignment = 0;
Bill Wendling49f60602013-01-28 05:23:28 +0000848 }
849
850 return *this;
851}
852
Bill Wendling702cc912012-10-15 20:35:56 +0000853AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
Bill Wendlingda3f9d82012-10-14 03:58:29 +0000854 if (Align == 0) return *this;
Bill Wendling03198882013-01-04 23:27:34 +0000855
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000856 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
857 assert(Align <= 0x40000000 && "Alignment too large.");
Bill Wendling03198882013-01-04 23:27:34 +0000858
859 Attrs.insert(Attribute::Alignment);
860 Alignment = Align;
Bill Wendlingda3f9d82012-10-14 03:58:29 +0000861 return *this;
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000862}
863
Bill Wendling03198882013-01-04 23:27:34 +0000864AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
865 // Default alignment, allow the target to define how to align it.
866 if (Align == 0) return *this;
867
868 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
869 assert(Align <= 0x100 && "Alignment too large.");
870
871 Attrs.insert(Attribute::StackAlignment);
872 StackAlignment = Align;
873 return *this;
874}
875
Bill Wendling22bd6412013-01-03 01:54:39 +0000876bool AttrBuilder::contains(Attribute::AttrKind A) const {
Bill Wendling03198882013-01-04 23:27:34 +0000877 return Attrs.count(A);
Bill Wendling7d2f2492012-10-10 07:36:45 +0000878}
879
Bill Wendling702cc912012-10-15 20:35:56 +0000880bool AttrBuilder::hasAttributes() const {
Bill Wendling03198882013-01-04 23:27:34 +0000881 return !Attrs.empty();
Bill Wendlingf385f4c2012-10-08 23:27:46 +0000882}
Bill Wendling60507d52013-01-04 20:54:35 +0000883
Bill Wendlinge7436542013-01-30 23:07:40 +0000884bool AttrBuilder::hasAttributes(AttributeSet A, uint64_t Index) const {
Bill Wendlingbdcbccc2013-02-02 00:42:06 +0000885 unsigned Idx = ~0U;
886 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
887 if (A.getSlotIndex(I) == Index) {
888 Idx = I;
889 break;
890 }
891
892 assert(Idx != ~0U && "Couldn't find the index!");
893
894 for (AttributeSet::iterator I = A.begin(Idx), E = A.end(Idx);
895 I != E; ++I) {
896 Attribute Attr = *I;
897 // FIXME: Support StringRefs.
898 Attribute::AttrKind Kind = Attribute::AttrKind(
899 cast<ConstantInt>(Attr.getAttributeKind())->getZExtValue());
900
901 if (Attrs.count(Kind))
902 return true;
903 }
904
905 return false;
Bill Wendling8831c062012-10-09 00:01:21 +0000906}
Bill Wendling60507d52013-01-04 20:54:35 +0000907
Bill Wendling702cc912012-10-15 20:35:56 +0000908bool AttrBuilder::hasAlignmentAttr() const {
Bill Wendling03198882013-01-04 23:27:34 +0000909 return Alignment != 0;
Bill Wendlingf385f4c2012-10-08 23:27:46 +0000910}
911
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000912bool AttrBuilder::operator==(const AttrBuilder &B) {
913 SmallVector<Attribute::AttrKind, 8> This(Attrs.begin(), Attrs.end());
914 SmallVector<Attribute::AttrKind, 8> That(B.Attrs.begin(), B.Attrs.end());
915 return This == That;
916}
917
918AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) {
Bill Wendling8232ece2013-01-29 01:43:29 +0000919 if (!Val) return *this;
920
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000921 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
922 I = Attribute::AttrKind(I + 1)) {
923 if (uint64_t A = (Val & AttributeImpl::getAttrMask(I))) {
924 Attrs.insert(I);
925
926 if (I == Attribute::Alignment)
927 Alignment = 1ULL << ((A >> 16) - 1);
928 else if (I == Attribute::StackAlignment)
929 StackAlignment = 1ULL << ((A >> 26)-1);
930 }
931 }
932
933 return *this;
934}
935
Bill Wendling1db9b692013-01-09 23:36:50 +0000936uint64_t AttrBuilder::Raw() const {
Bill Wendling03198882013-01-04 23:27:34 +0000937 uint64_t Mask = 0;
938
939 for (DenseSet<Attribute::AttrKind>::const_iterator I = Attrs.begin(),
940 E = Attrs.end(); I != E; ++I) {
941 Attribute::AttrKind Kind = *I;
942
943 if (Kind == Attribute::Alignment)
944 Mask |= (Log2_32(Alignment) + 1) << 16;
945 else if (Kind == Attribute::StackAlignment)
946 Mask |= (Log2_32(StackAlignment) + 1) << 26;
947 else
948 Mask |= AttributeImpl::getAttrMask(Kind);
949 }
950
951 return Mask;
Bill Wendlingf385f4c2012-10-08 23:27:46 +0000952}
953
Bill Wendling8e47daf2013-01-25 23:09:36 +0000954//===----------------------------------------------------------------------===//
955// AttributeFuncs Function Defintions
956//===----------------------------------------------------------------------===//
957
Bill Wendling7beee282013-02-01 01:04:27 +0000958/// \brief Which attributes cannot be applied to a type.
Bill Wendlinge7436542013-01-30 23:07:40 +0000959AttributeSet AttributeFuncs::typeIncompatible(Type *Ty, uint64_t Index) {
Bill Wendling8e47daf2013-01-25 23:09:36 +0000960 AttrBuilder Incompatible;
961
962 if (!Ty->isIntegerTy())
963 // Attribute that only apply to integers.
964 Incompatible.addAttribute(Attribute::SExt)
965 .addAttribute(Attribute::ZExt);
966
967 if (!Ty->isPointerTy())
968 // Attribute that only apply to pointers.
969 Incompatible.addAttribute(Attribute::ByVal)
970 .addAttribute(Attribute::Nest)
971 .addAttribute(Attribute::NoAlias)
972 .addAttribute(Attribute::NoCapture)
973 .addAttribute(Attribute::StructRet);
974
Bill Wendlinge7436542013-01-30 23:07:40 +0000975 return AttributeSet::get(Ty->getContext(), Index, Incompatible);
Bill Wendling8e47daf2013-01-25 23:09:36 +0000976}
977
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000978/// \brief This returns an integer containing an encoding of all the LLVM
979/// attributes found in the given attribute bitset. Any change to this encoding
980/// is a breaking change to bitcode compatibility.
Bill Wendling8232ece2013-01-29 01:43:29 +0000981/// N.B. This should be used only by the bitcode reader!
Bill Wendling8e47daf2013-01-25 23:09:36 +0000982uint64_t AttributeFuncs::encodeLLVMAttributesForBitcode(AttributeSet Attrs,
983 unsigned Index) {
984 // FIXME: It doesn't make sense to store the alignment information as an
985 // expanded out value, we should store it as a log2 value. However, we can't
986 // just change that here without breaking bitcode compatibility. If this ever
987 // becomes a problem in practice, we should introduce new tag numbers in the
988 // bitcode file and have those tags use a more efficiently encoded alignment
989 // field.
990
991 // Store the alignment in the bitcode as a 16-bit raw value instead of a 5-bit
992 // log2 encoded value. Shift the bits above the alignment up by 11 bits.
993 uint64_t EncodedAttrs = Attrs.Raw(Index) & 0xffff;
994 if (Attrs.hasAttribute(Index, Attribute::Alignment))
995 EncodedAttrs |= Attrs.getParamAlignment(Index) << 16;
996 EncodedAttrs |= (Attrs.Raw(Index) & (0xffffULL << 21)) << 11;
997 return EncodedAttrs;
998}
999
Bill Wendling8232ece2013-01-29 01:43:29 +00001000/// \brief This fills an AttrBuilder object with the LLVM attributes that have
1001/// been decoded from the given integer. This function must stay in sync with
1002/// 'encodeLLVMAttributesForBitcode'.
1003/// N.B. This should be used only by the bitcode reader!
1004void AttributeFuncs::decodeLLVMAttributesForBitcode(LLVMContext &C,
1005 AttrBuilder &B,
1006 uint64_t EncodedAttrs) {
Bill Wendling8e47daf2013-01-25 23:09:36 +00001007 // The alignment is stored as a 16-bit raw value from bits 31--16. We shift
1008 // the bits above 31 down by 11 bits.
1009 unsigned Alignment = (EncodedAttrs & (0xffffULL << 16)) >> 16;
1010 assert((!Alignment || isPowerOf2_32(Alignment)) &&
1011 "Alignment must be a power of two.");
1012
Bill Wendling8e47daf2013-01-25 23:09:36 +00001013 if (Alignment)
1014 B.addAlignmentAttr(Alignment);
Bill Wendling606c8e32013-01-29 03:20:31 +00001015 B.addRawValue(((EncodedAttrs & (0xffffULL << 32)) >> 11) |
1016 (EncodedAttrs & 0xffff));
Bill Wendling8e47daf2013-01-25 23:09:36 +00001017}