1 //===-- Attribute.cpp - Implement AttributesList -------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the Attribute, AttributeImpl, AttrBuilder,
11 // AttributeSetImpl, and AttributeSet classes.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/Attributes.h"
16 #include "AttributeImpl.h"
17 #include "LLVMContextImpl.h"
18 #include "llvm/ADT/FoldingSet.h"
19 #include "llvm/ADT/StringExtras.h"
20 #include "llvm/Support/Atomic.h"
21 #include "llvm/Support/Debug.h"
22 #include "llvm/Support/ManagedStatic.h"
23 #include "llvm/Support/Mutex.h"
24 #include "llvm/Support/raw_ostream.h"
25 #include "llvm/Type.h"
28 //===----------------------------------------------------------------------===//
29 // Attribute Implementation
30 //===----------------------------------------------------------------------===//
32 Attribute Attribute::get(LLVMContext &Context, ArrayRef<AttrKind> Vals) {
34 for (ArrayRef<AttrKind>::iterator I = Vals.begin(), E = Vals.end();
37 return Attribute::get(Context, B);
40 Attribute Attribute::get(LLVMContext &Context, AttrBuilder &B) {
41 // If there are no attributes, return an empty Attribute class.
42 if (!B.hasAttributes())
45 // Otherwise, build a key to look up the existing attributes.
46 LLVMContextImpl *pImpl = Context.pImpl;
48 ID.AddInteger(B.getBitMask());
51 AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
54 // If we didn't find any existing attributes of the same shape then create a
55 // new one and insert it.
56 PA = new AttributeImpl(Context, B.getBitMask());
57 pImpl->AttrsSet.InsertNode(PA, InsertPoint);
60 // Return the AttributesList that we found or created.
64 bool Attribute::hasAttribute(AttrKind Val) const {
65 return pImpl && pImpl->hasAttribute(Val);
68 bool Attribute::hasAttributes() const {
69 return pImpl && pImpl->hasAttributes();
72 bool Attribute::hasAttributes(const Attribute &A) const {
73 return pImpl && pImpl->hasAttributes(A);
76 /// This returns the alignment field of an attribute as a byte alignment value.
77 unsigned Attribute::getAlignment() const {
78 if (!hasAttribute(Attribute::Alignment))
80 return 1U << ((pImpl->getAlignment() >> 16) - 1);
83 /// This returns the stack alignment field of an attribute as a byte alignment
85 unsigned Attribute::getStackAlignment() const {
86 if (!hasAttribute(Attribute::StackAlignment))
88 return 1U << ((pImpl->getStackAlignment() >> 26) - 1);
91 uint64_t Attribute::getBitMask() const {
92 return pImpl ? pImpl->getBitMask() : 0;
95 Attribute Attribute::typeIncompatible(Type *Ty) {
96 AttrBuilder Incompatible;
98 if (!Ty->isIntegerTy())
99 // Attribute that only apply to integers.
100 Incompatible.addAttribute(Attribute::SExt)
101 .addAttribute(Attribute::ZExt);
103 if (!Ty->isPointerTy())
104 // Attribute that only apply to pointers.
105 Incompatible.addAttribute(Attribute::ByVal)
106 .addAttribute(Attribute::Nest)
107 .addAttribute(Attribute::NoAlias)
108 .addAttribute(Attribute::NoCapture)
109 .addAttribute(Attribute::StructRet);
111 return Attribute::get(Ty->getContext(), Incompatible);
114 /// encodeLLVMAttributesForBitcode - This returns an integer containing an
115 /// encoding of all the LLVM attributes found in the given attribute bitset.
116 /// Any change to this encoding is a breaking change to bitcode compatibility.
117 uint64_t Attribute::encodeLLVMAttributesForBitcode(Attribute Attrs) {
118 // FIXME: It doesn't make sense to store the alignment information as an
119 // expanded out value, we should store it as a log2 value. However, we can't
120 // just change that here without breaking bitcode compatibility. If this ever
121 // becomes a problem in practice, we should introduce new tag numbers in the
122 // bitcode file and have those tags use a more efficiently encoded alignment
125 // Store the alignment in the bitcode as a 16-bit raw value instead of a 5-bit
126 // log2 encoded value. Shift the bits above the alignment up by 11 bits.
127 uint64_t EncodedAttrs = Attrs.getBitMask() & 0xffff;
128 if (Attrs.hasAttribute(Attribute::Alignment))
129 EncodedAttrs |= Attrs.getAlignment() << 16;
130 EncodedAttrs |= (Attrs.getBitMask() & (0xffffULL << 21)) << 11;
134 /// decodeLLVMAttributesForBitcode - This returns an attribute bitset containing
135 /// the LLVM attributes that have been decoded from the given integer. This
136 /// function must stay in sync with 'encodeLLVMAttributesForBitcode'.
137 Attribute Attribute::decodeLLVMAttributesForBitcode(LLVMContext &C,
138 uint64_t EncodedAttrs) {
139 // The alignment is stored as a 16-bit raw value from bits 31--16. We shift
140 // the bits above 31 down by 11 bits.
141 unsigned Alignment = (EncodedAttrs & (0xffffULL << 16)) >> 16;
142 assert((!Alignment || isPowerOf2_32(Alignment)) &&
143 "Alignment must be a power of two.");
145 AttrBuilder B(EncodedAttrs & 0xffff);
147 B.addAlignmentAttr(Alignment);
148 B.addRawValue((EncodedAttrs & (0xffffULL << 32)) >> 11);
149 return Attribute::get(C, B);
152 std::string Attribute::getAsString() const {
154 if (hasAttribute(Attribute::ZExt))
155 Result += "zeroext ";
156 if (hasAttribute(Attribute::SExt))
157 Result += "signext ";
158 if (hasAttribute(Attribute::NoReturn))
159 Result += "noreturn ";
160 if (hasAttribute(Attribute::NoUnwind))
161 Result += "nounwind ";
162 if (hasAttribute(Attribute::UWTable))
163 Result += "uwtable ";
164 if (hasAttribute(Attribute::ReturnsTwice))
165 Result += "returns_twice ";
166 if (hasAttribute(Attribute::InReg))
168 if (hasAttribute(Attribute::NoAlias))
169 Result += "noalias ";
170 if (hasAttribute(Attribute::NoCapture))
171 Result += "nocapture ";
172 if (hasAttribute(Attribute::StructRet))
174 if (hasAttribute(Attribute::ByVal))
176 if (hasAttribute(Attribute::Nest))
178 if (hasAttribute(Attribute::ReadNone))
179 Result += "readnone ";
180 if (hasAttribute(Attribute::ReadOnly))
181 Result += "readonly ";
182 if (hasAttribute(Attribute::OptimizeForSize))
183 Result += "optsize ";
184 if (hasAttribute(Attribute::NoInline))
185 Result += "noinline ";
186 if (hasAttribute(Attribute::InlineHint))
187 Result += "inlinehint ";
188 if (hasAttribute(Attribute::AlwaysInline))
189 Result += "alwaysinline ";
190 if (hasAttribute(Attribute::StackProtect))
192 if (hasAttribute(Attribute::StackProtectReq))
194 if (hasAttribute(Attribute::NoRedZone))
195 Result += "noredzone ";
196 if (hasAttribute(Attribute::NoImplicitFloat))
197 Result += "noimplicitfloat ";
198 if (hasAttribute(Attribute::Naked))
200 if (hasAttribute(Attribute::NonLazyBind))
201 Result += "nonlazybind ";
202 if (hasAttribute(Attribute::AddressSafety))
203 Result += "address_safety ";
204 if (hasAttribute(Attribute::MinSize))
205 Result += "minsize ";
206 if (hasAttribute(Attribute::StackAlignment)) {
207 Result += "alignstack(";
208 Result += utostr(getStackAlignment());
211 if (hasAttribute(Attribute::Alignment)) {
213 Result += utostr(getAlignment());
216 if (hasAttribute(Attribute::NoDuplicate))
217 Result += "noduplicate ";
218 // Trim the trailing space.
219 assert(!Result.empty() && "Unknown attribute!");
220 Result.erase(Result.end()-1);
224 //===----------------------------------------------------------------------===//
225 // AttrBuilder Implementation
226 //===----------------------------------------------------------------------===//
228 AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val){
229 Bits |= AttributeImpl::getAttrMask(Val);
233 AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) {
238 AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
239 if (Align == 0) return *this;
240 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
241 assert(Align <= 0x40000000 && "Alignment too large.");
242 Bits |= (Log2_32(Align) + 1) << 16;
245 AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align){
246 // Default alignment, allow the target to define how to align it.
247 if (Align == 0) return *this;
248 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
249 assert(Align <= 0x100 && "Alignment too large.");
250 Bits |= (Log2_32(Align) + 1) << 26;
254 AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
255 Bits &= ~AttributeImpl::getAttrMask(Val);
259 AttrBuilder &AttrBuilder::addAttributes(const Attribute &A) {
260 Bits |= A.getBitMask();
264 AttrBuilder &AttrBuilder::removeAttributes(const Attribute &A){
265 Bits &= ~A.getBitMask();
269 bool AttrBuilder::hasAttribute(Attribute::AttrKind A) const {
270 return Bits & AttributeImpl::getAttrMask(A);
273 bool AttrBuilder::hasAttributes() const {
276 bool AttrBuilder::hasAttributes(const Attribute &A) const {
277 return Bits & A.getBitMask();
279 bool AttrBuilder::hasAlignmentAttr() const {
280 return Bits & AttributeImpl::getAttrMask(Attribute::Alignment);
283 uint64_t AttrBuilder::getAlignment() const {
284 if (!hasAlignmentAttr())
287 (((Bits & AttributeImpl::getAttrMask(Attribute::Alignment)) >> 16) - 1);
290 uint64_t AttrBuilder::getStackAlignment() const {
291 if (!hasAlignmentAttr())
294 (((Bits & AttributeImpl::getAttrMask(Attribute::StackAlignment))>>26)-1);
297 //===----------------------------------------------------------------------===//
298 // AttributeImpl Definition
299 //===----------------------------------------------------------------------===//
301 AttributeImpl::AttributeImpl(LLVMContext &C, uint64_t data) {
302 Data = ConstantInt::get(Type::getInt64Ty(C), data);
305 bool AttributeImpl::contains(Attribute::AttrKind Kind) const {
306 if (ConstantInt *CI = dyn_cast<ConstantInt>(Data))
307 return CI->getZExtValue() == Kind;
311 bool AttributeImpl::contains(StringRef Kind) const {
312 if (ConstantDataArray *CDA = dyn_cast<ConstantDataArray>(Data))
314 return CDA->getAsString() == Kind;
318 uint64_t AttributeImpl::getBitMask() const {
319 // FIXME: Remove this.
320 return cast<ConstantInt>(Data)->getZExtValue();
323 uint64_t AttributeImpl::getAttrMask(uint64_t Val) {
325 case Attribute::None: return 0;
326 case Attribute::ZExt: return 1 << 0;
327 case Attribute::SExt: return 1 << 1;
328 case Attribute::NoReturn: return 1 << 2;
329 case Attribute::InReg: return 1 << 3;
330 case Attribute::StructRet: return 1 << 4;
331 case Attribute::NoUnwind: return 1 << 5;
332 case Attribute::NoAlias: return 1 << 6;
333 case Attribute::ByVal: return 1 << 7;
334 case Attribute::Nest: return 1 << 8;
335 case Attribute::ReadNone: return 1 << 9;
336 case Attribute::ReadOnly: return 1 << 10;
337 case Attribute::NoInline: return 1 << 11;
338 case Attribute::AlwaysInline: return 1 << 12;
339 case Attribute::OptimizeForSize: return 1 << 13;
340 case Attribute::StackProtect: return 1 << 14;
341 case Attribute::StackProtectReq: return 1 << 15;
342 case Attribute::Alignment: return 31 << 16;
343 case Attribute::NoCapture: return 1 << 21;
344 case Attribute::NoRedZone: return 1 << 22;
345 case Attribute::NoImplicitFloat: return 1 << 23;
346 case Attribute::Naked: return 1 << 24;
347 case Attribute::InlineHint: return 1 << 25;
348 case Attribute::StackAlignment: return 7 << 26;
349 case Attribute::ReturnsTwice: return 1 << 29;
350 case Attribute::UWTable: return 1 << 30;
351 case Attribute::NonLazyBind: return 1U << 31;
352 case Attribute::AddressSafety: return 1ULL << 32;
353 case Attribute::MinSize: return 1ULL << 33;
354 case Attribute::NoDuplicate: return 1ULL << 34;
356 llvm_unreachable("Unsupported attribute type");
359 bool AttributeImpl::hasAttribute(uint64_t A) const {
360 return (getBitMask() & getAttrMask(A)) != 0;
363 bool AttributeImpl::hasAttributes() const {
364 return getBitMask() != 0;
367 bool AttributeImpl::hasAttributes(const Attribute &A) const {
368 // FIXME: getBitMask() won't work here in the future.
369 return getBitMask() & A.getBitMask();
372 uint64_t AttributeImpl::getAlignment() const {
373 return getBitMask() & getAttrMask(Attribute::Alignment);
376 uint64_t AttributeImpl::getStackAlignment() const {
377 return getBitMask() & getAttrMask(Attribute::StackAlignment);
380 //===----------------------------------------------------------------------===//
381 // AttributeSetImpl Definition
382 //===----------------------------------------------------------------------===//
384 AttributeSet AttributeSet::get(LLVMContext &C,
385 ArrayRef<AttributeWithIndex> Attrs) {
386 // If there are no attributes then return a null AttributesList pointer.
388 return AttributeSet();
391 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
392 assert(Attrs[i].Attrs.hasAttributes() &&
393 "Pointless attribute!");
394 assert((!i || Attrs[i-1].Index < Attrs[i].Index) &&
395 "Misordered AttributesList!");
399 // Otherwise, build a key to look up the existing attributes.
400 LLVMContextImpl *pImpl = C.pImpl;
402 AttributeSetImpl::Profile(ID, Attrs);
405 AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID,
408 // If we didn't find any existing attributes of the same shape then
409 // create a new one and insert it.
411 PA = new AttributeSetImpl(C, Attrs);
412 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
415 // Return the AttributesList that we found or created.
416 return AttributeSet(PA);
419 //===----------------------------------------------------------------------===//
420 // AttributeSet Method Implementations
421 //===----------------------------------------------------------------------===//
423 const AttributeSet &AttributeSet::operator=(const AttributeSet &RHS) {
424 AttrList = RHS.AttrList;
428 /// getNumSlots - Return the number of slots used in this attribute list.
429 /// This is the number of arguments that have an attribute set on them
430 /// (including the function itself).
431 unsigned AttributeSet::getNumSlots() const {
432 return AttrList ? AttrList->Attrs.size() : 0;
435 /// getSlot - Return the AttributeWithIndex at the specified slot. This
436 /// holds a number plus a set of attributes.
437 const AttributeWithIndex &AttributeSet::getSlot(unsigned Slot) const {
438 assert(AttrList && Slot < AttrList->Attrs.size() && "Slot # out of range!");
439 return AttrList->Attrs[Slot];
442 /// getAttributes - The attributes for the specified index are returned.
443 /// Attribute for the result are denoted with Idx = 0. Function notes are
444 /// denoted with idx = ~0.
445 Attribute AttributeSet::getAttributes(unsigned Idx) const {
446 if (AttrList == 0) return Attribute();
448 const SmallVector<AttributeWithIndex, 4> &Attrs = AttrList->Attrs;
449 for (unsigned i = 0, e = Attrs.size(); i != e && Attrs[i].Index <= Idx; ++i)
450 if (Attrs[i].Index == Idx)
451 return Attrs[i].Attrs;
456 /// hasAttrSomewhere - Return true if the specified attribute is set for at
457 /// least one parameter or for the return value.
458 bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr) const {
459 if (AttrList == 0) return false;
461 const SmallVector<AttributeWithIndex, 4> &Attrs = AttrList->Attrs;
462 for (unsigned i = 0, e = Attrs.size(); i != e; ++i)
463 if (Attrs[i].Attrs.hasAttribute(Attr))
469 unsigned AttributeSet::getNumAttrs() const {
470 return AttrList ? AttrList->Attrs.size() : 0;
473 Attribute &AttributeSet::getAttributesAtIndex(unsigned i) const {
474 assert(AttrList && "Trying to get an attribute from an empty list!");
475 assert(i < AttrList->Attrs.size() && "Index out of range!");
476 return AttrList->Attrs[i].Attrs;
479 AttributeSet AttributeSet::addAttr(LLVMContext &C, unsigned Idx,
480 Attribute Attrs) const {
481 Attribute OldAttrs = getAttributes(Idx);
483 // FIXME it is not obvious how this should work for alignment.
484 // For now, say we can't change a known alignment.
485 unsigned OldAlign = OldAttrs.getAlignment();
486 unsigned NewAlign = Attrs.getAlignment();
487 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
488 "Attempt to change alignment!");
491 AttrBuilder NewAttrs =
492 AttrBuilder(OldAttrs).addAttributes(Attrs);
493 if (NewAttrs == AttrBuilder(OldAttrs))
496 SmallVector<AttributeWithIndex, 8> NewAttrList;
498 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
500 const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs;
501 unsigned i = 0, e = OldAttrList.size();
502 // Copy attributes for arguments before this one.
503 for (; i != e && OldAttrList[i].Index < Idx; ++i)
504 NewAttrList.push_back(OldAttrList[i]);
506 // If there are attributes already at this index, merge them in.
507 if (i != e && OldAttrList[i].Index == Idx) {
509 Attribute::get(C, AttrBuilder(Attrs).
510 addAttributes(OldAttrList[i].Attrs));
514 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
516 // Copy attributes for arguments after this one.
517 NewAttrList.insert(NewAttrList.end(),
518 OldAttrList.begin()+i, OldAttrList.end());
521 return get(C, NewAttrList);
524 AttributeSet AttributeSet::removeAttr(LLVMContext &C, unsigned Idx,
525 Attribute Attrs) const {
527 // FIXME it is not obvious how this should work for alignment.
528 // For now, say we can't pass in alignment, which no current use does.
529 assert(!Attrs.hasAttribute(Attribute::Alignment) &&
530 "Attempt to exclude alignment!");
532 if (AttrList == 0) return AttributeSet();
534 Attribute OldAttrs = getAttributes(Idx);
535 AttrBuilder NewAttrs =
536 AttrBuilder(OldAttrs).removeAttributes(Attrs);
537 if (NewAttrs == AttrBuilder(OldAttrs))
540 SmallVector<AttributeWithIndex, 8> NewAttrList;
541 const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs;
542 unsigned i = 0, e = OldAttrList.size();
544 // Copy attributes for arguments before this one.
545 for (; i != e && OldAttrList[i].Index < Idx; ++i)
546 NewAttrList.push_back(OldAttrList[i]);
548 // If there are attributes already at this index, merge them in.
549 assert(OldAttrList[i].Index == Idx && "Attribute isn't set?");
550 Attrs = Attribute::get(C, AttrBuilder(OldAttrList[i].Attrs).
551 removeAttributes(Attrs));
553 if (Attrs.hasAttributes()) // If any attributes left for this param, add them.
554 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
556 // Copy attributes for arguments after this one.
557 NewAttrList.insert(NewAttrList.end(),
558 OldAttrList.begin()+i, OldAttrList.end());
560 return get(C, NewAttrList);
563 void AttributeSet::dump() const {
565 for (unsigned i = 0; i < getNumSlots(); ++i) {
566 const AttributeWithIndex &PAWI = getSlot(i);
567 dbgs() << "{" << PAWI.Index << "," << PAWI.Attrs.getAsString() << "} ";