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/IR/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/IR/Type.h"
21 #include "llvm/Support/Atomic.h"
22 #include "llvm/Support/Debug.h"
23 #include "llvm/Support/ManagedStatic.h"
24 #include "llvm/Support/Mutex.h"
25 #include "llvm/Support/raw_ostream.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 /// This returns the alignment field of an attribute as a byte alignment value.
73 unsigned Attribute::getAlignment() const {
74 if (!hasAttribute(Attribute::Alignment))
76 return 1U << ((pImpl->getAlignment() >> 16) - 1);
79 /// This returns the stack alignment field of an attribute as a byte alignment
81 unsigned Attribute::getStackAlignment() const {
82 if (!hasAttribute(Attribute::StackAlignment))
84 return 1U << ((pImpl->getStackAlignment() >> 26) - 1);
87 bool Attribute::operator==(AttrKind K) const {
88 return pImpl && *pImpl == K;
90 bool Attribute::operator!=(AttrKind K) const {
94 uint64_t Attribute::getBitMask() const {
95 return pImpl ? pImpl->getBitMask() : 0;
98 Attribute Attribute::typeIncompatible(Type *Ty) {
99 AttrBuilder Incompatible;
101 if (!Ty->isIntegerTy())
102 // Attribute that only apply to integers.
103 Incompatible.addAttribute(Attribute::SExt)
104 .addAttribute(Attribute::ZExt);
106 if (!Ty->isPointerTy())
107 // Attribute that only apply to pointers.
108 Incompatible.addAttribute(Attribute::ByVal)
109 .addAttribute(Attribute::Nest)
110 .addAttribute(Attribute::NoAlias)
111 .addAttribute(Attribute::NoCapture)
112 .addAttribute(Attribute::StructRet);
114 return Attribute::get(Ty->getContext(), Incompatible);
117 /// encodeLLVMAttributesForBitcode - This returns an integer containing an
118 /// encoding of all the LLVM attributes found in the given attribute bitset.
119 /// Any change to this encoding is a breaking change to bitcode compatibility.
120 uint64_t Attribute::encodeLLVMAttributesForBitcode(Attribute Attrs) {
121 // FIXME: It doesn't make sense to store the alignment information as an
122 // expanded out value, we should store it as a log2 value. However, we can't
123 // just change that here without breaking bitcode compatibility. If this ever
124 // becomes a problem in practice, we should introduce new tag numbers in the
125 // bitcode file and have those tags use a more efficiently encoded alignment
128 // Store the alignment in the bitcode as a 16-bit raw value instead of a 5-bit
129 // log2 encoded value. Shift the bits above the alignment up by 11 bits.
130 uint64_t EncodedAttrs = Attrs.getBitMask() & 0xffff;
131 if (Attrs.hasAttribute(Attribute::Alignment))
132 EncodedAttrs |= Attrs.getAlignment() << 16;
133 EncodedAttrs |= (Attrs.getBitMask() & (0xffffULL << 21)) << 11;
137 /// decodeLLVMAttributesForBitcode - This returns an attribute bitset containing
138 /// the LLVM attributes that have been decoded from the given integer. This
139 /// function must stay in sync with 'encodeLLVMAttributesForBitcode'.
140 Attribute Attribute::decodeLLVMAttributesForBitcode(LLVMContext &C,
141 uint64_t EncodedAttrs) {
142 // The alignment is stored as a 16-bit raw value from bits 31--16. We shift
143 // the bits above 31 down by 11 bits.
144 unsigned Alignment = (EncodedAttrs & (0xffffULL << 16)) >> 16;
145 assert((!Alignment || isPowerOf2_32(Alignment)) &&
146 "Alignment must be a power of two.");
148 AttrBuilder B(EncodedAttrs & 0xffff);
150 B.addAlignmentAttr(Alignment);
151 B.addRawValue((EncodedAttrs & (0xffffULL << 32)) >> 11);
152 return Attribute::get(C, B);
155 std::string Attribute::getAsString() const {
157 if (hasAttribute(Attribute::ZExt))
158 Result += "zeroext ";
159 if (hasAttribute(Attribute::SExt))
160 Result += "signext ";
161 if (hasAttribute(Attribute::NoReturn))
162 Result += "noreturn ";
163 if (hasAttribute(Attribute::NoUnwind))
164 Result += "nounwind ";
165 if (hasAttribute(Attribute::UWTable))
166 Result += "uwtable ";
167 if (hasAttribute(Attribute::ReturnsTwice))
168 Result += "returns_twice ";
169 if (hasAttribute(Attribute::InReg))
171 if (hasAttribute(Attribute::NoAlias))
172 Result += "noalias ";
173 if (hasAttribute(Attribute::NoCapture))
174 Result += "nocapture ";
175 if (hasAttribute(Attribute::StructRet))
177 if (hasAttribute(Attribute::ByVal))
179 if (hasAttribute(Attribute::Nest))
181 if (hasAttribute(Attribute::ReadNone))
182 Result += "readnone ";
183 if (hasAttribute(Attribute::ReadOnly))
184 Result += "readonly ";
185 if (hasAttribute(Attribute::OptimizeForSize))
186 Result += "optsize ";
187 if (hasAttribute(Attribute::NoInline))
188 Result += "noinline ";
189 if (hasAttribute(Attribute::InlineHint))
190 Result += "inlinehint ";
191 if (hasAttribute(Attribute::AlwaysInline))
192 Result += "alwaysinline ";
193 if (hasAttribute(Attribute::StackProtect))
195 if (hasAttribute(Attribute::StackProtectReq))
197 if (hasAttribute(Attribute::NoRedZone))
198 Result += "noredzone ";
199 if (hasAttribute(Attribute::NoImplicitFloat))
200 Result += "noimplicitfloat ";
201 if (hasAttribute(Attribute::Naked))
203 if (hasAttribute(Attribute::NonLazyBind))
204 Result += "nonlazybind ";
205 if (hasAttribute(Attribute::AddressSafety))
206 Result += "address_safety ";
207 if (hasAttribute(Attribute::MinSize))
208 Result += "minsize ";
209 if (hasAttribute(Attribute::StackAlignment)) {
210 Result += "alignstack(";
211 Result += utostr(getStackAlignment());
214 if (hasAttribute(Attribute::Alignment)) {
216 Result += utostr(getAlignment());
219 if (hasAttribute(Attribute::NoDuplicate))
220 Result += "noduplicate ";
221 // Trim the trailing space.
222 assert(!Result.empty() && "Unknown attribute!");
223 Result.erase(Result.end()-1);
227 //===----------------------------------------------------------------------===//
228 // AttrBuilder Method Implementations
229 //===----------------------------------------------------------------------===//
231 void AttrBuilder::clear() {
233 Alignment = StackAlignment = 0;
236 AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
241 AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
243 if (Val == Attribute::Alignment)
245 else if (Val == Attribute::StackAlignment)
251 AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
252 if (Align == 0) return *this;
254 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
255 assert(Align <= 0x40000000 && "Alignment too large.");
257 Attrs.insert(Attribute::Alignment);
262 AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
263 // Default alignment, allow the target to define how to align it.
264 if (Align == 0) return *this;
266 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
267 assert(Align <= 0x100 && "Alignment too large.");
269 Attrs.insert(Attribute::StackAlignment);
270 StackAlignment = Align;
274 AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) {
275 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
276 I = Attribute::AttrKind(I + 1)) {
277 if (uint64_t A = (Val & AttributeImpl::getAttrMask(I))) {
280 if (I == Attribute::Alignment)
281 Alignment = 1ULL << ((A >> 16) - 1);
282 else if (I == Attribute::StackAlignment)
283 StackAlignment = 1ULL << ((A >> 26)-1);
290 AttrBuilder &AttrBuilder::addAttributes(const Attribute &A) {
291 uint64_t Mask = A.getBitMask();
293 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
294 I = Attribute::AttrKind(I + 1)) {
295 if (uint64_t A = (Mask & AttributeImpl::getAttrMask(I))) {
298 if (I == Attribute::Alignment)
299 Alignment = 1ULL << ((A >> 16) - 1);
300 else if (I == Attribute::StackAlignment)
301 StackAlignment = 1ULL << ((A >> 26)-1);
308 AttrBuilder &AttrBuilder::removeAttributes(const Attribute &A){
309 uint64_t Mask = A.getBitMask();
311 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
312 I = Attribute::AttrKind(I + 1)) {
313 if (Mask & AttributeImpl::getAttrMask(I)) {
316 if (I == Attribute::Alignment)
318 else if (I == Attribute::StackAlignment)
326 bool AttrBuilder::contains(Attribute::AttrKind A) const {
327 return Attrs.count(A);
330 bool AttrBuilder::hasAttributes() const {
331 return !Attrs.empty();
334 bool AttrBuilder::hasAttributes(const Attribute &A) const {
335 return getBitMask() & A.getBitMask();
338 bool AttrBuilder::hasAlignmentAttr() const {
339 return Alignment != 0;
342 uint64_t AttrBuilder::getBitMask() const {
345 for (DenseSet<Attribute::AttrKind>::const_iterator I = Attrs.begin(),
346 E = Attrs.end(); I != E; ++I) {
347 Attribute::AttrKind Kind = *I;
349 if (Kind == Attribute::Alignment)
350 Mask |= (Log2_32(Alignment) + 1) << 16;
351 else if (Kind == Attribute::StackAlignment)
352 Mask |= (Log2_32(StackAlignment) + 1) << 26;
354 Mask |= AttributeImpl::getAttrMask(Kind);
360 bool AttrBuilder::operator==(const AttrBuilder &B) {
361 SmallVector<Attribute::AttrKind, 8> This(Attrs.begin(), Attrs.end());
362 SmallVector<Attribute::AttrKind, 8> That(B.Attrs.begin(), B.Attrs.end());
366 //===----------------------------------------------------------------------===//
367 // AttributeImpl Definition
368 //===----------------------------------------------------------------------===//
370 AttributeImpl::AttributeImpl(LLVMContext &C, uint64_t data) {
371 Data = ConstantInt::get(Type::getInt64Ty(C), data);
373 AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind data) {
374 Data = ConstantInt::get(Type::getInt64Ty(C), data);
376 AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind data,
377 ArrayRef<Constant*> values) {
378 Data = ConstantInt::get(Type::getInt64Ty(C), data);
379 Vals.reserve(values.size());
380 Vals.append(values.begin(), values.end());
382 AttributeImpl::AttributeImpl(LLVMContext &C, StringRef data) {
383 Data = ConstantDataArray::getString(C, data);
386 bool AttributeImpl::operator==(Attribute::AttrKind Kind) const {
387 if (ConstantInt *CI = dyn_cast<ConstantInt>(Data))
388 return CI->getZExtValue() == Kind;
391 bool AttributeImpl::operator!=(Attribute::AttrKind Kind) const {
392 return !(*this == Kind);
395 bool AttributeImpl::operator==(StringRef Kind) const {
396 if (ConstantDataArray *CDA = dyn_cast<ConstantDataArray>(Data))
398 return CDA->getAsString() == Kind;
401 bool AttributeImpl::operator!=(StringRef Kind) const {
402 return !(*this == Kind);
405 uint64_t AttributeImpl::getBitMask() const {
406 // FIXME: Remove this.
407 return cast<ConstantInt>(Data)->getZExtValue();
410 uint64_t AttributeImpl::getAttrMask(Attribute::AttrKind Val) {
412 case Attribute::EndAttrKinds: break;
413 case Attribute::None: return 0;
414 case Attribute::ZExt: return 1 << 0;
415 case Attribute::SExt: return 1 << 1;
416 case Attribute::NoReturn: return 1 << 2;
417 case Attribute::InReg: return 1 << 3;
418 case Attribute::StructRet: return 1 << 4;
419 case Attribute::NoUnwind: return 1 << 5;
420 case Attribute::NoAlias: return 1 << 6;
421 case Attribute::ByVal: return 1 << 7;
422 case Attribute::Nest: return 1 << 8;
423 case Attribute::ReadNone: return 1 << 9;
424 case Attribute::ReadOnly: return 1 << 10;
425 case Attribute::NoInline: return 1 << 11;
426 case Attribute::AlwaysInline: return 1 << 12;
427 case Attribute::OptimizeForSize: return 1 << 13;
428 case Attribute::StackProtect: return 1 << 14;
429 case Attribute::StackProtectReq: return 1 << 15;
430 case Attribute::Alignment: return 31 << 16;
431 case Attribute::NoCapture: return 1 << 21;
432 case Attribute::NoRedZone: return 1 << 22;
433 case Attribute::NoImplicitFloat: return 1 << 23;
434 case Attribute::Naked: return 1 << 24;
435 case Attribute::InlineHint: return 1 << 25;
436 case Attribute::StackAlignment: return 7 << 26;
437 case Attribute::ReturnsTwice: return 1 << 29;
438 case Attribute::UWTable: return 1 << 30;
439 case Attribute::NonLazyBind: return 1U << 31;
440 case Attribute::AddressSafety: return 1ULL << 32;
441 case Attribute::MinSize: return 1ULL << 33;
442 case Attribute::NoDuplicate: return 1ULL << 34;
444 llvm_unreachable("Unsupported attribute type");
447 bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
448 return (getBitMask() & getAttrMask(A)) != 0;
451 bool AttributeImpl::hasAttributes() const {
452 return getBitMask() != 0;
455 uint64_t AttributeImpl::getAlignment() const {
456 return getBitMask() & getAttrMask(Attribute::Alignment);
459 uint64_t AttributeImpl::getStackAlignment() const {
460 return getBitMask() & getAttrMask(Attribute::StackAlignment);
463 //===----------------------------------------------------------------------===//
464 // AttributeSetImpl Definition
465 //===----------------------------------------------------------------------===//
467 AttributeSet AttributeSet::get(LLVMContext &C,
468 ArrayRef<AttributeWithIndex> Attrs) {
469 // If there are no attributes then return a null AttributesList pointer.
471 return AttributeSet();
474 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
475 assert(Attrs[i].Attrs.hasAttributes() &&
476 "Pointless attribute!");
477 assert((!i || Attrs[i-1].Index < Attrs[i].Index) &&
478 "Misordered AttributesList!");
482 // Otherwise, build a key to look up the existing attributes.
483 LLVMContextImpl *pImpl = C.pImpl;
485 AttributeSetImpl::Profile(ID, Attrs);
488 AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID,
491 // If we didn't find any existing attributes of the same shape then
492 // create a new one and insert it.
494 PA = new AttributeSetImpl(C, Attrs);
495 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
498 // Return the AttributesList that we found or created.
499 return AttributeSet(PA);
502 //===----------------------------------------------------------------------===//
503 // AttributeSet Method Implementations
504 //===----------------------------------------------------------------------===//
506 const AttributeSet &AttributeSet::operator=(const AttributeSet &RHS) {
507 AttrList = RHS.AttrList;
511 /// getNumSlots - Return the number of slots used in this attribute list.
512 /// This is the number of arguments that have an attribute set on them
513 /// (including the function itself).
514 unsigned AttributeSet::getNumSlots() const {
515 return AttrList ? AttrList->getNumAttributes() : 0;
518 /// getSlot - Return the AttributeWithIndex at the specified slot. This
519 /// holds a number plus a set of attributes.
520 const AttributeWithIndex &AttributeSet::getSlot(unsigned Slot) const {
521 assert(AttrList && Slot < AttrList->getNumAttributes() &&
522 "Slot # out of range!");
523 return AttrList->getAttributes()[Slot];
526 bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{
527 return getAttributes(Index).hasAttribute(Kind);
530 bool AttributeSet::hasAttributes(unsigned Index) const {
531 return getAttributes(Index).hasAttributes();
534 std::string AttributeSet::getAsString(unsigned Index) const {
535 return getAttributes(Index).getAsString();
538 unsigned AttributeSet::getStackAlignment(unsigned Index) const {
539 return getAttributes(Index).getStackAlignment();
542 uint64_t AttributeSet::getBitMask(unsigned Index) const {
543 // FIXME: Remove this.
544 return getAttributes(Index).getBitMask();
547 /// getAttributes - The attributes for the specified index are returned.
548 /// Attributes for the result are denoted with Idx = 0. Function attributes are
549 /// denoted with Idx = ~0.
550 Attribute AttributeSet::getAttributes(unsigned Idx) const {
551 if (AttrList == 0) return Attribute();
553 ArrayRef<AttributeWithIndex> Attrs = AttrList->getAttributes();
554 for (unsigned i = 0, e = Attrs.size(); i != e && Attrs[i].Index <= Idx; ++i)
555 if (Attrs[i].Index == Idx)
556 return Attrs[i].Attrs;
561 /// hasAttrSomewhere - Return true if the specified attribute is set for at
562 /// least one parameter or for the return value.
563 bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr) const {
564 if (AttrList == 0) return false;
566 ArrayRef<AttributeWithIndex> Attrs = AttrList->getAttributes();
567 for (unsigned i = 0, e = Attrs.size(); i != e; ++i)
568 if (Attrs[i].Attrs.hasAttribute(Attr))
574 AttributeSet AttributeSet::addAttr(LLVMContext &C, unsigned Idx,
575 Attribute Attrs) const {
576 Attribute OldAttrs = getAttributes(Idx);
578 // FIXME it is not obvious how this should work for alignment.
579 // For now, say we can't change a known alignment.
580 unsigned OldAlign = OldAttrs.getAlignment();
581 unsigned NewAlign = Attrs.getAlignment();
582 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
583 "Attempt to change alignment!");
586 AttrBuilder NewAttrs =
587 AttrBuilder(OldAttrs).addAttributes(Attrs);
588 if (NewAttrs == AttrBuilder(OldAttrs))
591 SmallVector<AttributeWithIndex, 8> NewAttrList;
593 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
595 ArrayRef<AttributeWithIndex> OldAttrList = AttrList->getAttributes();
596 unsigned i = 0, e = OldAttrList.size();
597 // Copy attributes for arguments before this one.
598 for (; i != e && OldAttrList[i].Index < Idx; ++i)
599 NewAttrList.push_back(OldAttrList[i]);
601 // If there are attributes already at this index, merge them in.
602 if (i != e && OldAttrList[i].Index == Idx) {
604 Attribute::get(C, AttrBuilder(Attrs).
605 addAttributes(OldAttrList[i].Attrs));
609 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
611 // Copy attributes for arguments after this one.
612 NewAttrList.insert(NewAttrList.end(),
613 OldAttrList.begin()+i, OldAttrList.end());
616 return get(C, NewAttrList);
619 AttributeSet AttributeSet::removeAttr(LLVMContext &C, unsigned Idx,
620 Attribute Attrs) const {
622 // FIXME it is not obvious how this should work for alignment.
623 // For now, say we can't pass in alignment, which no current use does.
624 assert(!Attrs.hasAttribute(Attribute::Alignment) &&
625 "Attempt to exclude alignment!");
627 if (AttrList == 0) return AttributeSet();
629 Attribute OldAttrs = getAttributes(Idx);
630 AttrBuilder NewAttrs =
631 AttrBuilder(OldAttrs).removeAttributes(Attrs);
632 if (NewAttrs == AttrBuilder(OldAttrs))
635 SmallVector<AttributeWithIndex, 8> NewAttrList;
636 ArrayRef<AttributeWithIndex> OldAttrList = AttrList->getAttributes();
637 unsigned i = 0, e = OldAttrList.size();
639 // Copy attributes for arguments before this one.
640 for (; i != e && OldAttrList[i].Index < Idx; ++i)
641 NewAttrList.push_back(OldAttrList[i]);
643 // If there are attributes already at this index, merge them in.
644 assert(OldAttrList[i].Index == Idx && "Attribute isn't set?");
645 Attrs = Attribute::get(C, AttrBuilder(OldAttrList[i].Attrs).
646 removeAttributes(Attrs));
648 if (Attrs.hasAttributes()) // If any attributes left for this param, add them.
649 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
651 // Copy attributes for arguments after this one.
652 NewAttrList.insert(NewAttrList.end(),
653 OldAttrList.begin()+i, OldAttrList.end());
655 return get(C, NewAttrList);
658 void AttributeSet::dump() const {
660 for (unsigned i = 0; i < getNumSlots(); ++i) {
661 const AttributeWithIndex &PAWI = getSlot(i);
662 dbgs() << "{ " << PAWI.Index << ", " << PAWI.Attrs.getAsString() << " } ";