1 //===-- Attributes.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 //===----------------------------------------------------------------------===//
11 // \brief This file implements the Attribute, AttributeImpl, AttrBuilder,
12 // AttributeSetImpl, and AttributeSet classes.
14 //===----------------------------------------------------------------------===//
16 #include "llvm/IR/Attributes.h"
17 #include "AttributeImpl.h"
18 #include "LLVMContextImpl.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"
29 //===----------------------------------------------------------------------===//
30 // Attribute Construction Methods
31 //===----------------------------------------------------------------------===//
33 Attribute Attribute::get(LLVMContext &Context, Constant *Kind, Constant *Val) {
34 LLVMContextImpl *pImpl = Context.pImpl;
37 if (Val) ID.AddPointer(Val);
40 AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
43 // If we didn't find any existing attributes of the same shape then create a
44 // new one and insert it.
46 new AttributeImpl(Context, Kind) :
47 new AttributeImpl(Context, Kind, Val);
48 pImpl->AttrsSet.InsertNode(PA, InsertPoint);
51 // Return the AttributesList that we found or created.
55 Attribute Attribute::get(LLVMContext &Context, AttrKind Kind, Constant *Val) {
56 ConstantInt *KindVal = ConstantInt::get(Type::getInt64Ty(Context), Kind);
57 return get(Context, KindVal, Val);
60 Attribute Attribute::getWithAlignment(LLVMContext &Context, uint64_t Align) {
61 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
62 assert(Align <= 0x40000000 && "Alignment too large.");
63 return get(Context, Alignment,
64 ConstantInt::get(Type::getInt64Ty(Context), Align));
67 Attribute Attribute::getWithStackAlignment(LLVMContext &Context,
69 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
70 assert(Align <= 0x100 && "Alignment too large.");
71 return get(Context, StackAlignment,
72 ConstantInt::get(Type::getInt64Ty(Context), Align));
75 //===----------------------------------------------------------------------===//
76 // Attribute Accessor Methods
77 //===----------------------------------------------------------------------===//
79 bool Attribute::hasAttribute(AttrKind Val) const {
80 return pImpl && pImpl->hasAttribute(Val);
83 Constant *Attribute::getAttributeKind() const {
84 return pImpl ? pImpl->getAttributeKind() : 0;
87 ArrayRef<Constant*> Attribute::getAttributeValues() const {
88 return pImpl ? pImpl->getAttributeValues() : ArrayRef<Constant*>();
91 /// This returns the alignment field of an attribute as a byte alignment value.
92 unsigned Attribute::getAlignment() const {
93 if (!hasAttribute(Attribute::Alignment))
95 return pImpl->getAlignment();
98 /// This returns the stack alignment field of an attribute as a byte alignment
100 unsigned Attribute::getStackAlignment() const {
101 if (!hasAttribute(Attribute::StackAlignment))
103 return pImpl->getStackAlignment();
106 std::string Attribute::getAsString() const {
107 if (!pImpl) return "";
109 if (hasAttribute(Attribute::AddressSafety))
110 return "address_safety";
111 if (hasAttribute(Attribute::AlwaysInline))
112 return "alwaysinline";
113 if (hasAttribute(Attribute::ByVal))
115 if (hasAttribute(Attribute::InlineHint))
117 if (hasAttribute(Attribute::InReg))
119 if (hasAttribute(Attribute::MinSize))
121 if (hasAttribute(Attribute::Naked))
123 if (hasAttribute(Attribute::Nest))
125 if (hasAttribute(Attribute::NoAlias))
127 if (hasAttribute(Attribute::NoCapture))
129 if (hasAttribute(Attribute::NoDuplicate))
130 return "noduplicate";
131 if (hasAttribute(Attribute::NoImplicitFloat))
132 return "noimplicitfloat";
133 if (hasAttribute(Attribute::NoInline))
135 if (hasAttribute(Attribute::NonLazyBind))
136 return "nonlazybind";
137 if (hasAttribute(Attribute::NoRedZone))
139 if (hasAttribute(Attribute::NoReturn))
141 if (hasAttribute(Attribute::NoUnwind))
143 if (hasAttribute(Attribute::OptimizeForSize))
145 if (hasAttribute(Attribute::ReadNone))
147 if (hasAttribute(Attribute::ReadOnly))
149 if (hasAttribute(Attribute::ReturnsTwice))
150 return "returns_twice";
151 if (hasAttribute(Attribute::SExt))
153 if (hasAttribute(Attribute::StackProtect))
155 if (hasAttribute(Attribute::StackProtectReq))
157 if (hasAttribute(Attribute::StackProtectStrong))
159 if (hasAttribute(Attribute::StructRet))
161 if (hasAttribute(Attribute::UWTable))
163 if (hasAttribute(Attribute::ZExt))
166 // FIXME: These should be output like this:
171 if (hasAttribute(Attribute::StackAlignment)) {
173 Result += "alignstack(";
174 Result += utostr(getStackAlignment());
178 if (hasAttribute(Attribute::Alignment)) {
181 Result += utostr(getAlignment());
185 // Convert target-dependent attributes to strings of the form:
189 // "kind" = ("value1" "value2" "value3" )
191 if (ConstantDataArray *CDA =
192 dyn_cast<ConstantDataArray>(pImpl->getAttributeKind())) {
194 Result += '\"' + CDA->getAsString().str() + '"';
196 ArrayRef<Constant*> Vals = pImpl->getAttributeValues();
197 if (Vals.empty()) return Result;
199 if (Vals.size() > 1) Result += '(';
200 for (ArrayRef<Constant*>::iterator I = Vals.begin(), E = Vals.end();
202 ConstantDataArray *CDA = cast<ConstantDataArray>(*I++);
203 Result += '\"' + CDA->getAsString().str() + '"';
204 if (I != E) Result += ' ';
206 if (Vals.size() > 1) Result += ')';
209 llvm_unreachable("Unknown attribute");
212 bool Attribute::operator==(AttrKind K) const {
213 return (pImpl && *pImpl == K) || (!pImpl && K == None);
215 bool Attribute::operator!=(AttrKind K) const {
216 return !(*this == K);
219 bool Attribute::operator<(Attribute A) const {
220 if (!pImpl && !A.pImpl) return false;
221 if (!pImpl) return true;
222 if (!A.pImpl) return false;
223 return *pImpl < *A.pImpl;
226 uint64_t Attribute::Raw() const {
227 return pImpl ? pImpl->Raw() : 0;
230 //===----------------------------------------------------------------------===//
231 // AttributeImpl Definition
232 //===----------------------------------------------------------------------===//
234 AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind kind)
236 Kind = ConstantInt::get(Type::getInt64Ty(C), kind);
238 AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind kind,
239 ArrayRef<Constant*> values)
241 Kind = ConstantInt::get(Type::getInt64Ty(C), kind);
242 Vals.reserve(values.size());
243 Vals.append(values.begin(), values.end());
245 AttributeImpl::AttributeImpl(LLVMContext &C, StringRef kind)
247 Kind = ConstantDataArray::getString(C, kind);
250 bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
251 if (ConstantInt *CI = dyn_cast<ConstantInt>(Kind))
252 return CI->getZExtValue() == A;
256 uint64_t AttributeImpl::getAlignment() const {
257 assert(hasAttribute(Attribute::Alignment) &&
258 "Trying to retrieve the alignment from a non-alignment attr!");
259 return cast<ConstantInt>(Vals[0])->getZExtValue();
262 uint64_t AttributeImpl::getStackAlignment() const {
263 assert(hasAttribute(Attribute::StackAlignment) &&
264 "Trying to retrieve the stack alignment from a non-alignment attr!");
265 return cast<ConstantInt>(Vals[0])->getZExtValue();
268 bool AttributeImpl::operator==(Attribute::AttrKind kind) const {
269 if (ConstantInt *CI = dyn_cast<ConstantInt>(Kind))
270 return CI->getZExtValue() == kind;
273 bool AttributeImpl::operator!=(Attribute::AttrKind kind) const {
274 return !(*this == kind);
277 bool AttributeImpl::operator==(StringRef kind) const {
278 if (ConstantDataArray *CDA = dyn_cast<ConstantDataArray>(Kind))
280 return CDA->getAsString() == kind;
284 bool AttributeImpl::operator!=(StringRef kind) const {
285 return !(*this == kind);
288 bool AttributeImpl::operator<(const AttributeImpl &AI) const {
289 if (!Kind && !AI.Kind) return false;
290 if (!Kind && AI.Kind) return true;
291 if (Kind && !AI.Kind) return false;
293 ConstantInt *ThisCI = dyn_cast<ConstantInt>(Kind);
294 ConstantInt *ThatCI = dyn_cast<ConstantInt>(AI.Kind);
296 ConstantDataArray *ThisCDA = dyn_cast<ConstantDataArray>(Kind);
297 ConstantDataArray *ThatCDA = dyn_cast<ConstantDataArray>(AI.Kind);
299 if (ThisCI && ThatCI)
300 return ThisCI->getZExtValue() < ThatCI->getZExtValue();
302 if (ThisCI && ThatCDA)
305 if (ThisCDA && ThatCI)
308 return ThisCDA->getAsString() < ThatCDA->getAsString();
311 uint64_t AttributeImpl::Raw() const {
312 // FIXME: Remove this.
313 return cast<ConstantInt>(Kind)->getZExtValue();
316 uint64_t AttributeImpl::getAttrMask(Attribute::AttrKind Val) {
317 // FIXME: Remove this.
319 case Attribute::EndAttrKinds:
320 case Attribute::AttrKindEmptyKey:
321 case Attribute::AttrKindTombstoneKey:
322 llvm_unreachable("Synthetic enumerators which should never get here");
324 case Attribute::None: return 0;
325 case Attribute::ZExt: return 1 << 0;
326 case Attribute::SExt: return 1 << 1;
327 case Attribute::NoReturn: return 1 << 2;
328 case Attribute::InReg: return 1 << 3;
329 case Attribute::StructRet: return 1 << 4;
330 case Attribute::NoUnwind: return 1 << 5;
331 case Attribute::NoAlias: return 1 << 6;
332 case Attribute::ByVal: return 1 << 7;
333 case Attribute::Nest: return 1 << 8;
334 case Attribute::ReadNone: return 1 << 9;
335 case Attribute::ReadOnly: return 1 << 10;
336 case Attribute::NoInline: return 1 << 11;
337 case Attribute::AlwaysInline: return 1 << 12;
338 case Attribute::OptimizeForSize: return 1 << 13;
339 case Attribute::StackProtect: return 1 << 14;
340 case Attribute::StackProtectReq: return 1 << 15;
341 case Attribute::Alignment: return 31 << 16;
342 case Attribute::NoCapture: return 1 << 21;
343 case Attribute::NoRedZone: return 1 << 22;
344 case Attribute::NoImplicitFloat: return 1 << 23;
345 case Attribute::Naked: return 1 << 24;
346 case Attribute::InlineHint: return 1 << 25;
347 case Attribute::StackAlignment: return 7 << 26;
348 case Attribute::ReturnsTwice: return 1 << 29;
349 case Attribute::UWTable: return 1 << 30;
350 case Attribute::NonLazyBind: return 1U << 31;
351 case Attribute::AddressSafety: return 1ULL << 32;
352 case Attribute::MinSize: return 1ULL << 33;
353 case Attribute::NoDuplicate: return 1ULL << 34;
354 case Attribute::StackProtectStrong: return 1ULL << 35;
356 llvm_unreachable("Unsupported attribute type");
359 //===----------------------------------------------------------------------===//
360 // AttributeSetNode Definition
361 //===----------------------------------------------------------------------===//
363 AttributeSetNode *AttributeSetNode::get(LLVMContext &C,
364 ArrayRef<Attribute> Attrs) {
368 // Otherwise, build a key to look up the existing attributes.
369 LLVMContextImpl *pImpl = C.pImpl;
372 SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end());
373 std::sort(SortedAttrs.begin(), SortedAttrs.end());
375 for (SmallVectorImpl<Attribute>::iterator I = SortedAttrs.begin(),
376 E = SortedAttrs.end(); I != E; ++I)
380 AttributeSetNode *PA =
381 pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint);
383 // If we didn't find any existing attributes of the same shape then create a
384 // new one and insert it.
386 PA = new AttributeSetNode(SortedAttrs);
387 pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint);
390 // Return the AttributesListNode that we found or created.
394 bool AttributeSetNode::hasAttribute(Attribute::AttrKind Kind) const {
395 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
396 E = AttrList.end(); I != E; ++I)
397 if (I->hasAttribute(Kind))
402 unsigned AttributeSetNode::getAlignment() const {
403 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
404 E = AttrList.end(); I != E; ++I)
405 if (I->hasAttribute(Attribute::Alignment))
406 return I->getAlignment();
410 unsigned AttributeSetNode::getStackAlignment() const {
411 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
412 E = AttrList.end(); I != E; ++I)
413 if (I->hasAttribute(Attribute::StackAlignment))
414 return I->getStackAlignment();
418 std::string AttributeSetNode::getAsString() const {
419 std::string Str = "";
420 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
421 E = AttrList.end(); I != E; ++I) {
422 if (I != AttrList.begin()) Str += " ";
423 Str += I->getAsString();
428 //===----------------------------------------------------------------------===//
429 // AttributeSetImpl Definition
430 //===----------------------------------------------------------------------===//
432 uint64_t AttributeSetImpl::Raw(uint64_t Index) const {
433 for (unsigned I = 0, E = getNumAttributes(); I != E; ++I) {
434 if (getSlotIndex(I) != Index) continue;
435 const AttributeSetNode *ASN = AttrNodes[I].second;
438 for (AttributeSetNode::const_iterator II = ASN->begin(),
439 IE = ASN->end(); II != IE; ++II)
447 //===----------------------------------------------------------------------===//
448 // AttributeSet Construction and Mutation Methods
449 //===----------------------------------------------------------------------===//
452 AttributeSet::getImpl(LLVMContext &C,
453 ArrayRef<std::pair<unsigned, AttributeSetNode*> > Attrs) {
454 LLVMContextImpl *pImpl = C.pImpl;
456 AttributeSetImpl::Profile(ID, Attrs);
459 AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
461 // If we didn't find any existing attributes of the same shape then
462 // create a new one and insert it.
464 PA = new AttributeSetImpl(C, Attrs);
465 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
468 // Return the AttributesList that we found or created.
469 return AttributeSet(PA);
472 AttributeSet AttributeSet::get(LLVMContext &C,
473 ArrayRef<std::pair<unsigned, Attribute> > Attrs){
474 // If there are no attributes then return a null AttributesList pointer.
476 return AttributeSet();
479 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
480 assert((!i || Attrs[i-1].first <= Attrs[i].first) &&
481 "Misordered Attributes list!");
482 assert(Attrs[i].second != Attribute::None &&
483 "Pointless attribute!");
487 // Create a vector if (unsigned, AttributeSetNode*) pairs from the attributes
489 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrPairVec;
490 for (ArrayRef<std::pair<unsigned, Attribute> >::iterator I = Attrs.begin(),
491 E = Attrs.end(); I != E; ) {
492 unsigned Index = I->first;
493 SmallVector<Attribute, 4> AttrVec;
494 while (I != E && I->first == Index) {
495 AttrVec.push_back(I->second);
499 AttrPairVec.push_back(std::make_pair(Index,
500 AttributeSetNode::get(C, AttrVec)));
503 return getImpl(C, AttrPairVec);
506 AttributeSet AttributeSet::get(LLVMContext &C,
507 ArrayRef<std::pair<unsigned,
508 AttributeSetNode*> > Attrs) {
509 // If there are no attributes then return a null AttributesList pointer.
511 return AttributeSet();
513 return getImpl(C, Attrs);
516 AttributeSet AttributeSet::get(LLVMContext &C, unsigned Idx, AttrBuilder &B) {
517 if (!B.hasAttributes())
518 return AttributeSet();
520 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
521 for (AttrBuilder::iterator I = B.begin(), E = B.end(); I != E; ++I) {
522 Attribute::AttrKind Kind = *I;
523 if (Kind == Attribute::Alignment)
524 Attrs.push_back(std::make_pair(Idx, Attribute::
525 getWithAlignment(C, B.getAlignment())));
526 else if (Kind == Attribute::StackAlignment)
527 Attrs.push_back(std::make_pair(Idx, Attribute::
528 getWithStackAlignment(C, B.getStackAlignment())));
530 Attrs.push_back(std::make_pair(Idx, Attribute::get(C, Kind)));
533 return get(C, Attrs);
536 AttributeSet AttributeSet::get(LLVMContext &C, unsigned Idx,
537 ArrayRef<Attribute::AttrKind> Kind) {
538 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
539 for (ArrayRef<Attribute::AttrKind>::iterator I = Kind.begin(),
540 E = Kind.end(); I != E; ++I)
541 Attrs.push_back(std::make_pair(Idx, Attribute::get(C, *I)));
542 return get(C, Attrs);
545 AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<AttributeSet> Attrs) {
546 if (Attrs.empty()) return AttributeSet();
548 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrNodeVec;
549 for (unsigned I = 0, E = Attrs.size(); I != E; ++I) {
550 AttributeSet AS = Attrs[I];
551 if (!AS.pImpl) continue;
552 AttrNodeVec.append(AS.pImpl->AttrNodes.begin(), AS.pImpl->AttrNodes.end());
555 return getImpl(C, AttrNodeVec);
558 AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Idx,
559 Attribute::AttrKind Attr) const {
560 return addAttributes(C, Idx, AttributeSet::get(C, Idx, Attr));
563 AttributeSet AttributeSet::addAttributes(LLVMContext &C, unsigned Idx,
564 AttributeSet Attrs) const {
565 if (!pImpl) return Attrs;
566 if (!Attrs.pImpl) return *this;
569 // FIXME it is not obvious how this should work for alignment. For now, say
570 // we can't change a known alignment.
571 unsigned OldAlign = getParamAlignment(Idx);
572 unsigned NewAlign = Attrs.getParamAlignment(Idx);
573 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
574 "Attempt to change alignment!");
577 // Add the attribute slots before the one we're trying to add.
578 SmallVector<AttributeSet, 4> AttrSet;
579 uint64_t NumAttrs = pImpl->getNumAttributes();
581 uint64_t LastIndex = 0;
582 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
583 if (getSlotIndex(I) >= Idx) {
584 if (getSlotIndex(I) == Idx) AS = getSlotAttributes(LastIndex++);
588 AttrSet.push_back(getSlotAttributes(I));
591 // Now add the attribute into the correct slot. There may already be an
592 // AttributeSet there.
593 AttrBuilder B(AS, Idx);
595 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
596 if (Attrs.getSlotIndex(I) == Idx) {
597 for (AttributeSetImpl::const_iterator II = Attrs.pImpl->begin(I),
598 IE = Attrs.pImpl->end(I); II != IE; ++II)
603 AttrSet.push_back(AttributeSet::get(C, Idx, B));
605 // Add the remaining attribute slots.
606 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
607 AttrSet.push_back(getSlotAttributes(I));
609 return get(C, AttrSet);
612 AttributeSet AttributeSet::removeAttribute(LLVMContext &C, unsigned Idx,
613 Attribute::AttrKind Attr) const {
614 return removeAttributes(C, Idx, AttributeSet::get(C, Idx, Attr));
617 AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Idx,
618 AttributeSet Attrs) const {
619 if (!pImpl) return AttributeSet();
620 if (!Attrs.pImpl) return *this;
623 // FIXME it is not obvious how this should work for alignment.
624 // For now, say we can't pass in alignment, which no current use does.
625 assert(!Attrs.hasAttribute(Idx, Attribute::Alignment) &&
626 "Attempt to change alignment!");
629 // Add the attribute slots before the one we're trying to add.
630 SmallVector<AttributeSet, 4> AttrSet;
631 uint64_t NumAttrs = pImpl->getNumAttributes();
633 uint64_t LastIndex = 0;
634 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
635 if (getSlotIndex(I) >= Idx) {
636 if (getSlotIndex(I) == Idx) AS = getSlotAttributes(LastIndex++);
640 AttrSet.push_back(getSlotAttributes(I));
643 // Now remove the attribute from the correct slot. There may already be an
644 // AttributeSet there.
645 AttrBuilder B(AS, Idx);
647 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
648 if (Attrs.getSlotIndex(I) == Idx) {
649 B.removeAttributes(Attrs.pImpl->getSlotAttributes(I), Idx);
653 AttrSet.push_back(AttributeSet::get(C, Idx, B));
655 // Add the remaining attribute slots.
656 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
657 AttrSet.push_back(getSlotAttributes(I));
659 return get(C, AttrSet);
662 //===----------------------------------------------------------------------===//
663 // AttributeSet Accessor Methods
664 //===----------------------------------------------------------------------===//
666 AttributeSet AttributeSet::getParamAttributes(unsigned Idx) const {
667 return pImpl && hasAttributes(Idx) ?
668 AttributeSet::get(pImpl->getContext(),
669 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
670 std::make_pair(Idx, getAttributes(Idx)))) :
674 AttributeSet AttributeSet::getRetAttributes() const {
675 return pImpl && hasAttributes(ReturnIndex) ?
676 AttributeSet::get(pImpl->getContext(),
677 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
678 std::make_pair(ReturnIndex,
679 getAttributes(ReturnIndex)))) :
683 AttributeSet AttributeSet::getFnAttributes() const {
684 return pImpl && hasAttributes(FunctionIndex) ?
685 AttributeSet::get(pImpl->getContext(),
686 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
687 std::make_pair(FunctionIndex,
688 getAttributes(FunctionIndex)))) :
692 bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{
693 AttributeSetNode *ASN = getAttributes(Index);
694 return ASN ? ASN->hasAttribute(Kind) : false;
697 bool AttributeSet::hasAttributes(unsigned Index) const {
698 AttributeSetNode *ASN = getAttributes(Index);
699 return ASN ? ASN->hasAttributes() : false;
702 /// \brief Return true if the specified attribute is set for at least one
703 /// parameter or for the return value.
704 bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr) const {
705 if (pImpl == 0) return false;
707 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
708 for (AttributeSetImpl::const_iterator II = pImpl->begin(I),
709 IE = pImpl->end(I); II != IE; ++II)
710 if (II->hasAttribute(Attr))
716 unsigned AttributeSet::getParamAlignment(unsigned Index) const {
717 AttributeSetNode *ASN = getAttributes(Index);
718 return ASN ? ASN->getAlignment() : 0;
721 unsigned AttributeSet::getStackAlignment(unsigned Index) const {
722 AttributeSetNode *ASN = getAttributes(Index);
723 return ASN ? ASN->getStackAlignment() : 0;
726 std::string AttributeSet::getAsString(unsigned Index) const {
727 AttributeSetNode *ASN = getAttributes(Index);
728 return ASN ? ASN->getAsString() : std::string("");
731 /// \brief The attributes for the specified index are returned.
732 AttributeSetNode *AttributeSet::getAttributes(unsigned Idx) const {
733 if (!pImpl) return 0;
735 // Loop through to find the attribute node we want.
736 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
737 if (pImpl->getSlotIndex(I) == Idx)
738 return pImpl->getSlotNode(I);
743 //===----------------------------------------------------------------------===//
744 // AttributeSet Introspection Methods
745 //===----------------------------------------------------------------------===//
747 /// \brief Return the number of slots used in this attribute list. This is the
748 /// number of arguments that have an attribute set on them (including the
749 /// function itself).
750 unsigned AttributeSet::getNumSlots() const {
751 return pImpl ? pImpl->getNumAttributes() : 0;
754 uint64_t AttributeSet::getSlotIndex(unsigned Slot) const {
755 assert(pImpl && Slot < pImpl->getNumAttributes() &&
756 "Slot # out of range!");
757 return pImpl->getSlotIndex(Slot);
760 AttributeSet AttributeSet::getSlotAttributes(unsigned Slot) const {
761 assert(pImpl && Slot < pImpl->getNumAttributes() &&
762 "Slot # out of range!");
763 return pImpl->getSlotAttributes(Slot);
766 uint64_t AttributeSet::Raw(unsigned Index) const {
767 // FIXME: Remove this.
768 return pImpl ? pImpl->Raw(Index) : 0;
771 void AttributeSet::dump() const {
774 for (unsigned i = 0, e = getNumSlots(); i < e; ++i) {
775 uint64_t Index = getSlotIndex(i);
781 dbgs() << " => " << getAsString(Index) << " }\n";
787 //===----------------------------------------------------------------------===//
788 // AttrBuilder Method Implementations
789 //===----------------------------------------------------------------------===//
791 AttrBuilder::AttrBuilder(AttributeSet AS, unsigned Idx)
792 : Alignment(0), StackAlignment(0) {
793 AttributeSetImpl *pImpl = AS.pImpl;
796 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I) {
797 if (pImpl->getSlotIndex(I) != Idx) continue;
799 for (AttributeSetImpl::const_iterator II = pImpl->begin(I),
800 IE = pImpl->end(I); II != IE; ++II)
807 void AttrBuilder::clear() {
809 Alignment = StackAlignment = 0;
812 AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
813 assert(Val != Attribute::Alignment && Val != Attribute::StackAlignment &&
814 "Adding alignment attribute without adding alignment value!");
819 AttrBuilder &AttrBuilder::addAttribute(Attribute Attr) {
820 ConstantInt *Kind = cast<ConstantInt>(Attr.getAttributeKind());
821 Attribute::AttrKind KindVal = Attribute::AttrKind(Kind->getZExtValue());
822 Attrs.insert(KindVal);
824 if (KindVal == Attribute::Alignment)
825 Alignment = Attr.getAlignment();
826 else if (KindVal == Attribute::StackAlignment)
827 StackAlignment = Attr.getStackAlignment();
831 AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
834 if (Val == Attribute::Alignment)
836 else if (Val == Attribute::StackAlignment)
842 AttrBuilder &AttrBuilder::removeAttributes(AttributeSet A, uint64_t Index) {
843 uint64_t Mask = A.Raw(Index);
845 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
846 I = Attribute::AttrKind(I + 1)) {
847 if (Mask & AttributeImpl::getAttrMask(I)) {
850 if (I == Attribute::Alignment)
852 else if (I == Attribute::StackAlignment)
860 AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
861 if (Align == 0) return *this;
863 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
864 assert(Align <= 0x40000000 && "Alignment too large.");
866 Attrs.insert(Attribute::Alignment);
871 AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
872 // Default alignment, allow the target to define how to align it.
873 if (Align == 0) return *this;
875 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
876 assert(Align <= 0x100 && "Alignment too large.");
878 Attrs.insert(Attribute::StackAlignment);
879 StackAlignment = Align;
883 bool AttrBuilder::contains(Attribute::AttrKind A) const {
884 return Attrs.count(A);
887 bool AttrBuilder::hasAttributes() const {
888 return !Attrs.empty();
891 bool AttrBuilder::hasAttributes(AttributeSet A, uint64_t Index) const {
892 return Raw() & A.Raw(Index);
895 bool AttrBuilder::hasAlignmentAttr() const {
896 return Alignment != 0;
899 bool AttrBuilder::operator==(const AttrBuilder &B) {
900 SmallVector<Attribute::AttrKind, 8> This(Attrs.begin(), Attrs.end());
901 SmallVector<Attribute::AttrKind, 8> That(B.Attrs.begin(), B.Attrs.end());
905 AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) {
906 if (!Val) return *this;
908 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
909 I = Attribute::AttrKind(I + 1)) {
910 if (uint64_t A = (Val & AttributeImpl::getAttrMask(I))) {
913 if (I == Attribute::Alignment)
914 Alignment = 1ULL << ((A >> 16) - 1);
915 else if (I == Attribute::StackAlignment)
916 StackAlignment = 1ULL << ((A >> 26)-1);
923 uint64_t AttrBuilder::Raw() const {
926 for (DenseSet<Attribute::AttrKind>::const_iterator I = Attrs.begin(),
927 E = Attrs.end(); I != E; ++I) {
928 Attribute::AttrKind Kind = *I;
930 if (Kind == Attribute::Alignment)
931 Mask |= (Log2_32(Alignment) + 1) << 16;
932 else if (Kind == Attribute::StackAlignment)
933 Mask |= (Log2_32(StackAlignment) + 1) << 26;
935 Mask |= AttributeImpl::getAttrMask(Kind);
941 //===----------------------------------------------------------------------===//
942 // AttributeFuncs Function Defintions
943 //===----------------------------------------------------------------------===//
945 AttributeSet AttributeFuncs::typeIncompatible(Type *Ty, uint64_t Index) {
946 AttrBuilder Incompatible;
948 if (!Ty->isIntegerTy())
949 // Attribute that only apply to integers.
950 Incompatible.addAttribute(Attribute::SExt)
951 .addAttribute(Attribute::ZExt);
953 if (!Ty->isPointerTy())
954 // Attribute that only apply to pointers.
955 Incompatible.addAttribute(Attribute::ByVal)
956 .addAttribute(Attribute::Nest)
957 .addAttribute(Attribute::NoAlias)
958 .addAttribute(Attribute::NoCapture)
959 .addAttribute(Attribute::StructRet);
961 return AttributeSet::get(Ty->getContext(), Index, Incompatible);
964 /// \brief This returns an integer containing an encoding of all the LLVM
965 /// attributes found in the given attribute bitset. Any change to this encoding
966 /// is a breaking change to bitcode compatibility.
967 /// N.B. This should be used only by the bitcode reader!
968 uint64_t AttributeFuncs::encodeLLVMAttributesForBitcode(AttributeSet Attrs,
970 // FIXME: It doesn't make sense to store the alignment information as an
971 // expanded out value, we should store it as a log2 value. However, we can't
972 // just change that here without breaking bitcode compatibility. If this ever
973 // becomes a problem in practice, we should introduce new tag numbers in the
974 // bitcode file and have those tags use a more efficiently encoded alignment
977 // Store the alignment in the bitcode as a 16-bit raw value instead of a 5-bit
978 // log2 encoded value. Shift the bits above the alignment up by 11 bits.
979 uint64_t EncodedAttrs = Attrs.Raw(Index) & 0xffff;
980 if (Attrs.hasAttribute(Index, Attribute::Alignment))
981 EncodedAttrs |= Attrs.getParamAlignment(Index) << 16;
982 EncodedAttrs |= (Attrs.Raw(Index) & (0xffffULL << 21)) << 11;
986 /// \brief This fills an AttrBuilder object with the LLVM attributes that have
987 /// been decoded from the given integer. This function must stay in sync with
988 /// 'encodeLLVMAttributesForBitcode'.
989 /// N.B. This should be used only by the bitcode reader!
990 void AttributeFuncs::decodeLLVMAttributesForBitcode(LLVMContext &C,
992 uint64_t EncodedAttrs) {
993 // The alignment is stored as a 16-bit raw value from bits 31--16. We shift
994 // the bits above 31 down by 11 bits.
995 unsigned Alignment = (EncodedAttrs & (0xffffULL << 16)) >> 16;
996 assert((!Alignment || isPowerOf2_32(Alignment)) &&
997 "Alignment must be a power of two.");
1000 B.addAlignmentAttr(Alignment);
1001 B.addRawValue(((EncodedAttrs & (0xffffULL << 32)) >> 11) |
1002 (EncodedAttrs & 0xffff));