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.
45 PA = new AttributeImpl(Context, Kind, Val);
46 pImpl->AttrsSet.InsertNode(PA, InsertPoint);
49 // Return the AttributesList that we found or created.
53 Attribute Attribute::get(LLVMContext &Context, AttrKind Kind, Constant *Val) {
54 ConstantInt *KindVal = ConstantInt::get(Type::getInt64Ty(Context), Kind);
55 return get(Context, KindVal, Val);
58 Attribute Attribute::getWithAlignment(LLVMContext &Context, uint64_t Align) {
59 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));
65 Attribute Attribute::getWithStackAlignment(LLVMContext &Context,
67 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));
73 //===----------------------------------------------------------------------===//
74 // Attribute Accessor Methods
75 //===----------------------------------------------------------------------===//
77 bool Attribute::hasAttribute(AttrKind Val) const {
78 return pImpl && pImpl->hasAttribute(Val);
81 Constant *Attribute::getAttributeKind() const {
82 return pImpl ? pImpl->getAttributeKind() : 0;
85 Constant *Attribute::getAttributeValues() const {
86 return pImpl ? pImpl->getAttributeValues() : 0;
89 /// This returns the alignment field of an attribute as a byte alignment value.
90 unsigned Attribute::getAlignment() const {
91 assert(hasAttribute(Attribute::Alignment) &&
92 "Trying to get alignment from non-alignment attribute!");
93 return pImpl->getAlignment();
96 /// This returns the stack alignment field of an attribute as a byte alignment
98 unsigned Attribute::getStackAlignment() const {
99 assert(hasAttribute(Attribute::StackAlignment) &&
100 "Trying to get alignment from non-alignment attribute!");
101 return pImpl->getStackAlignment();
104 std::string Attribute::getAsString() const {
105 if (!pImpl) return "";
107 if (hasAttribute(Attribute::AddressSafety))
108 return "address_safety";
109 if (hasAttribute(Attribute::AlwaysInline))
110 return "alwaysinline";
111 if (hasAttribute(Attribute::ByVal))
113 if (hasAttribute(Attribute::InlineHint))
115 if (hasAttribute(Attribute::InReg))
117 if (hasAttribute(Attribute::MinSize))
119 if (hasAttribute(Attribute::Naked))
121 if (hasAttribute(Attribute::Nest))
123 if (hasAttribute(Attribute::NoAlias))
125 if (hasAttribute(Attribute::NoCapture))
127 if (hasAttribute(Attribute::NoDuplicate))
128 return "noduplicate";
129 if (hasAttribute(Attribute::NoImplicitFloat))
130 return "noimplicitfloat";
131 if (hasAttribute(Attribute::NoInline))
133 if (hasAttribute(Attribute::NonLazyBind))
134 return "nonlazybind";
135 if (hasAttribute(Attribute::NoRedZone))
137 if (hasAttribute(Attribute::NoReturn))
139 if (hasAttribute(Attribute::NoUnwind))
141 if (hasAttribute(Attribute::OptimizeForSize))
143 if (hasAttribute(Attribute::ReadNone))
145 if (hasAttribute(Attribute::ReadOnly))
147 if (hasAttribute(Attribute::ReturnsTwice))
148 return "returns_twice";
149 if (hasAttribute(Attribute::SExt))
151 if (hasAttribute(Attribute::StackProtect))
153 if (hasAttribute(Attribute::StackProtectReq))
155 if (hasAttribute(Attribute::StackProtectStrong))
157 if (hasAttribute(Attribute::StructRet))
159 if (hasAttribute(Attribute::UWTable))
161 if (hasAttribute(Attribute::ZExt))
164 // FIXME: These should be output like this:
169 if (hasAttribute(Attribute::StackAlignment)) {
171 Result += "alignstack(";
172 Result += utostr(getStackAlignment());
176 if (hasAttribute(Attribute::Alignment)) {
179 Result += utostr(getAlignment());
183 // Convert target-dependent attributes to strings of the form:
187 // "kind" = ( "value1" "value2" "value3" )
189 if (ConstantDataArray *CDA =
190 dyn_cast<ConstantDataArray>(pImpl->getAttributeKind())) {
192 Result += '\"' + CDA->getAsString().str() + '"';
194 Constant *Vals = pImpl->getAttributeValues();
195 if (!Vals) return Result;
197 // FIXME: This should support more than just ConstantDataArrays. Also,
198 // support a vector of attribute values.
201 Result += '\"' + cast<ConstantDataArray>(Vals)->getAsString().str() + '"';
206 llvm_unreachable("Unknown attribute");
209 bool Attribute::operator==(AttrKind K) const {
210 return (pImpl && *pImpl == K) || (!pImpl && K == None);
212 bool Attribute::operator!=(AttrKind K) const {
213 return !(*this == K);
216 bool 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;
223 //===----------------------------------------------------------------------===//
224 // AttributeImpl Definition
225 //===----------------------------------------------------------------------===//
227 bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
228 if (ConstantInt *CI = dyn_cast<ConstantInt>(Kind))
229 return CI->getZExtValue() == A;
233 uint64_t AttributeImpl::getAlignment() const {
234 assert(hasAttribute(Attribute::Alignment) &&
235 "Trying to retrieve the alignment from a non-alignment attr!");
236 return cast<ConstantInt>(Values)->getZExtValue();
239 uint64_t AttributeImpl::getStackAlignment() const {
240 assert(hasAttribute(Attribute::StackAlignment) &&
241 "Trying to retrieve the stack alignment from a non-alignment attr!");
242 return cast<ConstantInt>(Values)->getZExtValue();
245 bool AttributeImpl::operator==(Attribute::AttrKind kind) const {
246 if (ConstantInt *CI = dyn_cast<ConstantInt>(Kind))
247 return CI->getZExtValue() == kind;
250 bool AttributeImpl::operator!=(Attribute::AttrKind kind) const {
251 return !(*this == kind);
254 bool AttributeImpl::operator==(StringRef kind) const {
255 if (ConstantDataArray *CDA = dyn_cast<ConstantDataArray>(Kind))
257 return CDA->getAsString() == kind;
261 bool AttributeImpl::operator!=(StringRef kind) const {
262 return !(*this == kind);
265 bool AttributeImpl::operator<(const AttributeImpl &AI) const {
266 // This sorts the attributes with Attribute::AttrKinds coming first (sorted
267 // relative to their enum value) and then strings.
269 if (!Kind && !AI.Kind) return false;
270 if (!Kind && AI.Kind) return true;
271 if (Kind && !AI.Kind) return false;
273 ConstantInt *ThisCI = dyn_cast<ConstantInt>(Kind);
274 ConstantInt *ThatCI = dyn_cast<ConstantInt>(AI.Kind);
276 ConstantDataArray *ThisCDA = dyn_cast<ConstantDataArray>(Kind);
277 ConstantDataArray *ThatCDA = dyn_cast<ConstantDataArray>(AI.Kind);
279 if (ThisCI && ThatCI)
280 return ThisCI->getZExtValue() < ThatCI->getZExtValue();
282 if (ThisCI && ThatCDA)
285 if (ThisCDA && ThatCI)
288 return ThisCDA->getAsString() < ThatCDA->getAsString();
291 uint64_t AttributeImpl::getAttrMask(Attribute::AttrKind Val) {
292 // FIXME: Remove this.
294 case Attribute::EndAttrKinds:
295 case Attribute::AttrKindEmptyKey:
296 case Attribute::AttrKindTombstoneKey:
297 llvm_unreachable("Synthetic enumerators which should never get here");
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;
331 llvm_unreachable("Unsupported attribute type");
334 //===----------------------------------------------------------------------===//
335 // AttributeSetNode Definition
336 //===----------------------------------------------------------------------===//
338 AttributeSetNode *AttributeSetNode::get(LLVMContext &C,
339 ArrayRef<Attribute> Attrs) {
343 // Otherwise, build a key to look up the existing attributes.
344 LLVMContextImpl *pImpl = C.pImpl;
347 SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end());
348 std::sort(SortedAttrs.begin(), SortedAttrs.end());
350 for (SmallVectorImpl<Attribute>::iterator I = SortedAttrs.begin(),
351 E = SortedAttrs.end(); I != E; ++I)
355 AttributeSetNode *PA =
356 pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint);
358 // If we didn't find any existing attributes of the same shape then create a
359 // new one and insert it.
361 PA = new AttributeSetNode(SortedAttrs);
362 pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint);
365 // Return the AttributesListNode that we found or created.
369 bool AttributeSetNode::hasAttribute(Attribute::AttrKind Kind) const {
370 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
371 E = AttrList.end(); I != E; ++I)
372 if (I->hasAttribute(Kind))
377 unsigned AttributeSetNode::getAlignment() const {
378 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
379 E = AttrList.end(); I != E; ++I)
380 if (I->hasAttribute(Attribute::Alignment))
381 return I->getAlignment();
385 unsigned AttributeSetNode::getStackAlignment() const {
386 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
387 E = AttrList.end(); I != E; ++I)
388 if (I->hasAttribute(Attribute::StackAlignment))
389 return I->getStackAlignment();
393 std::string AttributeSetNode::getAsString() const {
394 std::string Str = "";
395 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
396 E = AttrList.end(); I != E; ) {
397 Str += I->getAsString();
398 if (++I != E) Str += " ";
403 //===----------------------------------------------------------------------===//
404 // AttributeSetImpl Definition
405 //===----------------------------------------------------------------------===//
407 uint64_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;
413 for (AttributeSetNode::const_iterator II = ASN->begin(),
414 IE = ASN->end(); II != IE; ++II)
422 //===----------------------------------------------------------------------===//
423 // AttributeSet Construction and Mutation Methods
424 //===----------------------------------------------------------------------===//
427 AttributeSet::getImpl(LLVMContext &C,
428 ArrayRef<std::pair<unsigned, AttributeSetNode*> > Attrs) {
429 LLVMContextImpl *pImpl = C.pImpl;
431 AttributeSetImpl::Profile(ID, Attrs);
434 AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
436 // If we didn't find any existing attributes of the same shape then
437 // create a new one and insert it.
439 PA = new AttributeSetImpl(C, Attrs);
440 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
443 // Return the AttributesList that we found or created.
444 return AttributeSet(PA);
447 AttributeSet AttributeSet::get(LLVMContext &C,
448 ArrayRef<std::pair<unsigned, Attribute> > Attrs){
449 // If there are no attributes then return a null AttributesList pointer.
451 return AttributeSet();
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!");
457 assert(Attrs[i].second != Attribute::None &&
458 "Pointless attribute!");
462 // Create a vector if (unsigned, AttributeSetNode*) pairs from the attributes
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;
469 while (I != E && I->first == Index) {
470 AttrVec.push_back(I->second);
474 AttrPairVec.push_back(std::make_pair(Index,
475 AttributeSetNode::get(C, AttrVec)));
478 return getImpl(C, AttrPairVec);
481 AttributeSet AttributeSet::get(LLVMContext &C,
482 ArrayRef<std::pair<unsigned,
483 AttributeSetNode*> > Attrs) {
484 // If there are no attributes then return a null AttributesList pointer.
486 return AttributeSet();
488 return getImpl(C, Attrs);
491 AttributeSet AttributeSet::get(LLVMContext &C, unsigned Idx, AttrBuilder &B) {
492 if (!B.hasAttributes())
493 return AttributeSet();
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())));
505 Attrs.push_back(std::make_pair(Idx, Attribute::get(C, Kind)));
508 return get(C, Attrs);
511 AttributeSet 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);
520 AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<AttributeSet> Attrs) {
521 if (Attrs.empty()) return AttributeSet();
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());
530 return getImpl(C, AttrNodeVec);
533 AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Idx,
534 Attribute::AttrKind Attr) const {
535 return addAttributes(C, Idx, AttributeSet::get(C, Idx, Attr));
538 AttributeSet AttributeSet::addAttributes(LLVMContext &C, unsigned Idx,
539 AttributeSet Attrs) const {
540 if (!pImpl) return Attrs;
541 if (!Attrs.pImpl) return *this;
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!");
552 // Add the attribute slots before the one we're trying to add.
553 SmallVector<AttributeSet, 4> AttrSet;
554 uint64_t NumAttrs = pImpl->getNumAttributes();
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++);
563 AttrSet.push_back(getSlotAttributes(I));
566 // Now add the attribute into the correct slot. There may already be an
567 // AttributeSet there.
568 AttrBuilder B(AS, Idx);
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)
578 AttrSet.push_back(AttributeSet::get(C, Idx, B));
580 // Add the remaining attribute slots.
581 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
582 AttrSet.push_back(getSlotAttributes(I));
584 return get(C, AttrSet);
587 AttributeSet AttributeSet::removeAttribute(LLVMContext &C, unsigned Idx,
588 Attribute::AttrKind Attr) const {
589 return removeAttributes(C, Idx, AttributeSet::get(C, Idx, Attr));
592 AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Idx,
593 AttributeSet Attrs) const {
594 if (!pImpl) return AttributeSet();
595 if (!Attrs.pImpl) return *this;
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!");
604 // Add the attribute slots before the one we're trying to add.
605 SmallVector<AttributeSet, 4> AttrSet;
606 uint64_t NumAttrs = pImpl->getNumAttributes();
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++);
615 AttrSet.push_back(getSlotAttributes(I));
618 // Now remove the attribute from the correct slot. There may already be an
619 // AttributeSet there.
620 AttrBuilder B(AS, Idx);
622 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
623 if (Attrs.getSlotIndex(I) == Idx) {
624 B.removeAttributes(Attrs.pImpl->getSlotAttributes(I), Idx);
628 AttrSet.push_back(AttributeSet::get(C, Idx, B));
630 // Add the remaining attribute slots.
631 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
632 AttrSet.push_back(getSlotAttributes(I));
634 return get(C, AttrSet);
637 //===----------------------------------------------------------------------===//
638 // AttributeSet Accessor Methods
639 //===----------------------------------------------------------------------===//
641 AttributeSet AttributeSet::getParamAttributes(unsigned Idx) const {
642 return pImpl && hasAttributes(Idx) ?
643 AttributeSet::get(pImpl->getContext(),
644 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
645 std::make_pair(Idx, getAttributes(Idx)))) :
649 AttributeSet AttributeSet::getRetAttributes() const {
650 return pImpl && hasAttributes(ReturnIndex) ?
651 AttributeSet::get(pImpl->getContext(),
652 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
653 std::make_pair(ReturnIndex,
654 getAttributes(ReturnIndex)))) :
658 AttributeSet AttributeSet::getFnAttributes() const {
659 return pImpl && hasAttributes(FunctionIndex) ?
660 AttributeSet::get(pImpl->getContext(),
661 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
662 std::make_pair(FunctionIndex,
663 getAttributes(FunctionIndex)))) :
667 bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{
668 AttributeSetNode *ASN = getAttributes(Index);
669 return ASN ? ASN->hasAttribute(Kind) : false;
672 bool AttributeSet::hasAttributes(unsigned Index) const {
673 AttributeSetNode *ASN = getAttributes(Index);
674 return ASN ? ASN->hasAttributes() : false;
677 /// \brief Return true if the specified attribute is set for at least one
678 /// parameter or for the return value.
679 bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr) const {
680 if (pImpl == 0) return false;
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)
685 if (II->hasAttribute(Attr))
691 unsigned AttributeSet::getParamAlignment(unsigned Index) const {
692 AttributeSetNode *ASN = getAttributes(Index);
693 return ASN ? ASN->getAlignment() : 0;
696 unsigned AttributeSet::getStackAlignment(unsigned Index) const {
697 AttributeSetNode *ASN = getAttributes(Index);
698 return ASN ? ASN->getStackAlignment() : 0;
701 std::string AttributeSet::getAsString(unsigned Index) const {
702 AttributeSetNode *ASN = getAttributes(Index);
703 return ASN ? ASN->getAsString() : std::string("");
706 /// \brief The attributes for the specified index are returned.
707 AttributeSetNode *AttributeSet::getAttributes(unsigned Idx) const {
708 if (!pImpl) return 0;
710 // 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);
718 AttributeSet::iterator AttributeSet::begin(unsigned Idx) const {
720 return ArrayRef<Attribute>().begin();
721 return pImpl->begin(Idx);
724 AttributeSet::iterator AttributeSet::end(unsigned Idx) const {
726 return ArrayRef<Attribute>().end();
727 return pImpl->end(Idx);
730 //===----------------------------------------------------------------------===//
731 // AttributeSet Introspection Methods
732 //===----------------------------------------------------------------------===//
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).
737 unsigned AttributeSet::getNumSlots() const {
738 return pImpl ? pImpl->getNumAttributes() : 0;
741 uint64_t AttributeSet::getSlotIndex(unsigned Slot) const {
742 assert(pImpl && Slot < pImpl->getNumAttributes() &&
743 "Slot # out of range!");
744 return pImpl->getSlotIndex(Slot);
747 AttributeSet AttributeSet::getSlotAttributes(unsigned Slot) const {
748 assert(pImpl && Slot < pImpl->getNumAttributes() &&
749 "Slot # out of range!");
750 return pImpl->getSlotAttributes(Slot);
753 uint64_t AttributeSet::Raw(unsigned Index) const {
754 // FIXME: Remove this.
755 return pImpl ? pImpl->Raw(Index) : 0;
758 void AttributeSet::dump() const {
761 for (unsigned i = 0, e = getNumSlots(); i < e; ++i) {
762 uint64_t Index = getSlotIndex(i);
768 dbgs() << " => " << getAsString(Index) << " }\n";
774 //===----------------------------------------------------------------------===//
775 // AttrBuilder Method Implementations
776 //===----------------------------------------------------------------------===//
778 AttrBuilder::AttrBuilder(AttributeSet AS, unsigned Idx)
779 : Alignment(0), StackAlignment(0) {
780 AttributeSetImpl *pImpl = AS.pImpl;
783 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I) {
784 if (pImpl->getSlotIndex(I) != Idx) continue;
786 for (AttributeSetImpl::const_iterator II = pImpl->begin(I),
787 IE = pImpl->end(I); II != IE; ++II)
794 void AttrBuilder::clear() {
796 Alignment = StackAlignment = 0;
799 AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
800 assert(Val != Attribute::Alignment && Val != Attribute::StackAlignment &&
801 "Adding alignment attribute without adding alignment value!");
806 AttrBuilder &AttrBuilder::addAttribute(Attribute Attr) {
807 ConstantInt *Kind = cast<ConstantInt>(Attr.getAttributeKind());
808 Attribute::AttrKind KindVal = Attribute::AttrKind(Kind->getZExtValue());
809 Attrs.insert(KindVal);
811 if (KindVal == Attribute::Alignment)
812 Alignment = Attr.getAlignment();
813 else if (KindVal == Attribute::StackAlignment)
814 StackAlignment = Attr.getStackAlignment();
818 AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
821 if (Val == Attribute::Alignment)
823 else if (Val == Attribute::StackAlignment)
829 AttrBuilder &AttrBuilder::removeAttributes(AttributeSet A, uint64_t Index) {
831 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
832 if (A.getSlotIndex(I) == Index) {
837 assert(Idx != ~0U && "Couldn't find index in AttributeSet!");
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());
844 if (Kind == Attribute::Alignment)
846 else if (Kind == Attribute::StackAlignment)
853 AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
854 if (Align == 0) return *this;
856 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
857 assert(Align <= 0x40000000 && "Alignment too large.");
859 Attrs.insert(Attribute::Alignment);
864 AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
865 // Default alignment, allow the target to define how to align it.
866 if (Align == 0) return *this;
868 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
869 assert(Align <= 0x100 && "Alignment too large.");
871 Attrs.insert(Attribute::StackAlignment);
872 StackAlignment = Align;
876 bool AttrBuilder::contains(Attribute::AttrKind A) const {
877 return Attrs.count(A);
880 bool AttrBuilder::hasAttributes() const {
881 return !Attrs.empty();
884 bool AttrBuilder::hasAttributes(AttributeSet A, uint64_t Index) const {
886 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
887 if (A.getSlotIndex(I) == Index) {
892 assert(Idx != ~0U && "Couldn't find the index!");
894 for (AttributeSet::iterator I = A.begin(Idx), E = A.end(Idx);
897 // FIXME: Support StringRefs.
898 Attribute::AttrKind Kind = Attribute::AttrKind(
899 cast<ConstantInt>(Attr.getAttributeKind())->getZExtValue());
901 if (Attrs.count(Kind))
908 bool AttrBuilder::hasAlignmentAttr() const {
909 return Alignment != 0;
912 bool 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());
918 AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) {
919 if (!Val) return *this;
921 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
922 I = Attribute::AttrKind(I + 1)) {
923 if (uint64_t A = (Val & AttributeImpl::getAttrMask(I))) {
926 if (I == Attribute::Alignment)
927 Alignment = 1ULL << ((A >> 16) - 1);
928 else if (I == Attribute::StackAlignment)
929 StackAlignment = 1ULL << ((A >> 26)-1);
936 uint64_t AttrBuilder::Raw() const {
939 for (DenseSet<Attribute::AttrKind>::const_iterator I = Attrs.begin(),
940 E = Attrs.end(); I != E; ++I) {
941 Attribute::AttrKind Kind = *I;
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;
948 Mask |= AttributeImpl::getAttrMask(Kind);
954 //===----------------------------------------------------------------------===//
955 // AttributeFuncs Function Defintions
956 //===----------------------------------------------------------------------===//
958 /// \brief Which attributes cannot be applied to a type.
959 AttributeSet AttributeFuncs::typeIncompatible(Type *Ty, uint64_t Index) {
960 AttrBuilder Incompatible;
962 if (!Ty->isIntegerTy())
963 // Attribute that only apply to integers.
964 Incompatible.addAttribute(Attribute::SExt)
965 .addAttribute(Attribute::ZExt);
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);
975 return AttributeSet::get(Ty->getContext(), Index, Incompatible);
978 /// \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.
981 /// N.B. This should be used only by the bitcode reader!
982 uint64_t AttributeFuncs::encodeLLVMAttributesForBitcode(AttributeSet Attrs,
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
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;
1000 /// \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!
1004 void AttributeFuncs::decodeLLVMAttributesForBitcode(LLVMContext &C,
1006 uint64_t EncodedAttrs) {
1007 // 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.");
1014 B.addAlignmentAttr(Alignment);
1015 B.addRawValue(((EncodedAttrs & (0xffffULL << 32)) >> 11) |
1016 (EncodedAttrs & 0xffff));