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, AttrKind Kind) {
35 return Attribute::get(Context, B.addAttribute(Kind));
38 Attribute Attribute::get(LLVMContext &Context, AttrBuilder &B) {
39 // If there are no attributes, return an empty Attribute class.
40 if (!B.hasAttributes())
43 assert(std::distance(B.begin(), B.end()) == 1 &&
44 "The Attribute object should represent one attribute only!");
46 // Otherwise, build a key to look up the existing attributes.
47 LLVMContextImpl *pImpl = Context.pImpl;
49 ConstantInt *CI = ConstantInt::get(Type::getInt64Ty(Context), B.Raw());
53 AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
56 // If we didn't find any existing attributes of the same shape then create a
57 // new one and insert it.
58 PA = new AttributeImpl(Context, CI);
59 pImpl->AttrsSet.InsertNode(PA, InsertPoint);
62 // Return the AttributesList that we found or created.
66 Attribute Attribute::getWithAlignment(LLVMContext &Context, uint64_t Align) {
68 return get(Context, B.addAlignmentAttr(Align));
71 Attribute Attribute::getWithStackAlignment(LLVMContext &Context,
74 return get(Context, B.addStackAlignmentAttr(Align));
77 //===----------------------------------------------------------------------===//
78 // Attribute Accessor Methods
79 //===----------------------------------------------------------------------===//
81 bool Attribute::hasAttribute(AttrKind Val) const {
82 return pImpl && pImpl->hasAttribute(Val);
85 Constant *Attribute::getAttributeKind() const {
86 return pImpl ? pImpl->getAttributeKind() : 0;
89 ArrayRef<Constant*> Attribute::getAttributeValues() const {
90 return pImpl ? pImpl->getAttributeValues() : ArrayRef<Constant*>();
93 /// This returns the alignment field of an attribute as a byte alignment value.
94 unsigned Attribute::getAlignment() const {
95 if (!hasAttribute(Attribute::Alignment))
97 return pImpl->getAlignment();
100 /// This returns the stack alignment field of an attribute as a byte alignment
102 unsigned Attribute::getStackAlignment() const {
103 if (!hasAttribute(Attribute::StackAlignment))
105 return pImpl->getStackAlignment();
108 std::string Attribute::getAsString() const {
109 if (hasAttribute(Attribute::ZExt))
111 if (hasAttribute(Attribute::SExt))
113 if (hasAttribute(Attribute::NoReturn))
115 if (hasAttribute(Attribute::NoUnwind))
117 if (hasAttribute(Attribute::UWTable))
119 if (hasAttribute(Attribute::ReturnsTwice))
120 return "returns_twice";
121 if (hasAttribute(Attribute::InReg))
123 if (hasAttribute(Attribute::NoAlias))
125 if (hasAttribute(Attribute::NoCapture))
127 if (hasAttribute(Attribute::StructRet))
129 if (hasAttribute(Attribute::ByVal))
131 if (hasAttribute(Attribute::Nest))
133 if (hasAttribute(Attribute::ReadNone))
135 if (hasAttribute(Attribute::ReadOnly))
137 if (hasAttribute(Attribute::OptimizeForSize))
139 if (hasAttribute(Attribute::NoInline))
141 if (hasAttribute(Attribute::InlineHint))
143 if (hasAttribute(Attribute::AlwaysInline))
144 return "alwaysinline";
145 if (hasAttribute(Attribute::StackProtect))
147 if (hasAttribute(Attribute::StackProtectReq))
149 if (hasAttribute(Attribute::StackProtectStrong))
151 if (hasAttribute(Attribute::NoRedZone))
153 if (hasAttribute(Attribute::NoImplicitFloat))
154 return "noimplicitfloat";
155 if (hasAttribute(Attribute::Naked))
157 if (hasAttribute(Attribute::NonLazyBind))
158 return "nonlazybind";
159 if (hasAttribute(Attribute::AddressSafety))
160 return "address_safety";
161 if (hasAttribute(Attribute::MinSize))
163 if (hasAttribute(Attribute::StackAlignment)) {
165 Result += "alignstack(";
166 Result += utostr(getStackAlignment());
170 if (hasAttribute(Attribute::Alignment)) {
173 Result += utostr(getAlignment());
177 if (hasAttribute(Attribute::NoDuplicate))
178 return "noduplicate";
180 llvm_unreachable("Unknown attribute");
183 bool Attribute::operator==(AttrKind K) const {
184 return (pImpl && *pImpl == K) || (!pImpl && K == None);
186 bool Attribute::operator!=(AttrKind K) const {
187 return !(*this == K);
190 bool Attribute::operator<(Attribute A) const {
191 if (!pImpl && !A.pImpl) return false;
192 if (!pImpl) return true;
193 if (!A.pImpl) return false;
194 return *pImpl < *A.pImpl;
197 uint64_t Attribute::Raw() const {
198 return pImpl ? pImpl->Raw() : 0;
201 //===----------------------------------------------------------------------===//
202 // AttributeImpl Definition
203 //===----------------------------------------------------------------------===//
205 AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind kind)
207 Kind = ConstantInt::get(Type::getInt64Ty(C), kind);
209 AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind kind,
210 ArrayRef<Constant*> values)
212 Kind = ConstantInt::get(Type::getInt64Ty(C), kind);
213 Vals.reserve(values.size());
214 Vals.append(values.begin(), values.end());
216 AttributeImpl::AttributeImpl(LLVMContext &C, StringRef kind)
218 Kind = ConstantDataArray::getString(C, kind);
221 bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
222 return (Raw() & getAttrMask(A)) != 0;
225 uint64_t AttributeImpl::getAlignment() const {
226 uint64_t Mask = Raw() & getAttrMask(Attribute::Alignment);
227 return 1ULL << ((Mask >> 16) - 1);
230 uint64_t AttributeImpl::getStackAlignment() const {
231 uint64_t Mask = Raw() & getAttrMask(Attribute::StackAlignment);
232 return 1ULL << ((Mask >> 26) - 1);
235 bool AttributeImpl::operator==(Attribute::AttrKind kind) const {
236 if (ConstantInt *CI = dyn_cast<ConstantInt>(Kind))
237 return CI->getZExtValue() == kind;
240 bool AttributeImpl::operator!=(Attribute::AttrKind kind) const {
241 return !(*this == kind);
244 bool AttributeImpl::operator==(StringRef kind) const {
245 if (ConstantDataArray *CDA = dyn_cast<ConstantDataArray>(Kind))
247 return CDA->getAsString() == kind;
251 bool AttributeImpl::operator!=(StringRef kind) const {
252 return !(*this == kind);
255 bool AttributeImpl::operator<(const AttributeImpl &AI) const {
256 if (!Kind && !AI.Kind) return false;
257 if (!Kind && AI.Kind) return true;
258 if (Kind && !AI.Kind) return false;
260 ConstantInt *ThisCI = dyn_cast<ConstantInt>(Kind);
261 ConstantInt *ThatCI = dyn_cast<ConstantInt>(AI.Kind);
263 ConstantDataArray *ThisCDA = dyn_cast<ConstantDataArray>(Kind);
264 ConstantDataArray *ThatCDA = dyn_cast<ConstantDataArray>(AI.Kind);
266 if (ThisCI && ThatCI)
267 return ThisCI->getZExtValue() < ThatCI->getZExtValue();
269 if (ThisCI && ThatCDA)
272 if (ThisCDA && ThatCI)
275 return ThisCDA->getAsString() < ThatCDA->getAsString();
278 uint64_t AttributeImpl::Raw() const {
279 // FIXME: Remove this.
280 return cast<ConstantInt>(Kind)->getZExtValue();
283 uint64_t AttributeImpl::getAttrMask(Attribute::AttrKind Val) {
284 // FIXME: Remove this.
286 case Attribute::EndAttrKinds:
287 case Attribute::AttrKindEmptyKey:
288 case Attribute::AttrKindTombstoneKey:
289 llvm_unreachable("Synthetic enumerators which should never get here");
291 case Attribute::None: return 0;
292 case Attribute::ZExt: return 1 << 0;
293 case Attribute::SExt: return 1 << 1;
294 case Attribute::NoReturn: return 1 << 2;
295 case Attribute::InReg: return 1 << 3;
296 case Attribute::StructRet: return 1 << 4;
297 case Attribute::NoUnwind: return 1 << 5;
298 case Attribute::NoAlias: return 1 << 6;
299 case Attribute::ByVal: return 1 << 7;
300 case Attribute::Nest: return 1 << 8;
301 case Attribute::ReadNone: return 1 << 9;
302 case Attribute::ReadOnly: return 1 << 10;
303 case Attribute::NoInline: return 1 << 11;
304 case Attribute::AlwaysInline: return 1 << 12;
305 case Attribute::OptimizeForSize: return 1 << 13;
306 case Attribute::StackProtect: return 1 << 14;
307 case Attribute::StackProtectReq: return 1 << 15;
308 case Attribute::Alignment: return 31 << 16;
309 case Attribute::NoCapture: return 1 << 21;
310 case Attribute::NoRedZone: return 1 << 22;
311 case Attribute::NoImplicitFloat: return 1 << 23;
312 case Attribute::Naked: return 1 << 24;
313 case Attribute::InlineHint: return 1 << 25;
314 case Attribute::StackAlignment: return 7 << 26;
315 case Attribute::ReturnsTwice: return 1 << 29;
316 case Attribute::UWTable: return 1 << 30;
317 case Attribute::NonLazyBind: return 1U << 31;
318 case Attribute::AddressSafety: return 1ULL << 32;
319 case Attribute::MinSize: return 1ULL << 33;
320 case Attribute::NoDuplicate: return 1ULL << 34;
321 case Attribute::StackProtectStrong: return 1ULL << 35;
323 llvm_unreachable("Unsupported attribute type");
326 //===----------------------------------------------------------------------===//
327 // AttributeSetNode Definition
328 //===----------------------------------------------------------------------===//
330 AttributeSetNode *AttributeSetNode::get(LLVMContext &C,
331 ArrayRef<Attribute> Attrs) {
335 // Otherwise, build a key to look up the existing attributes.
336 LLVMContextImpl *pImpl = C.pImpl;
339 SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end());
340 std::sort(SortedAttrs.begin(), SortedAttrs.end());
342 for (SmallVectorImpl<Attribute>::iterator I = SortedAttrs.begin(),
343 E = SortedAttrs.end(); I != E; ++I)
347 AttributeSetNode *PA =
348 pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint);
350 // If we didn't find any existing attributes of the same shape then create a
351 // new one and insert it.
353 PA = new AttributeSetNode(SortedAttrs);
354 pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint);
357 // Return the AttributesListNode that we found or created.
361 bool AttributeSetNode::hasAttribute(Attribute::AttrKind Kind) const {
362 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
363 E = AttrList.end(); I != E; ++I)
369 unsigned AttributeSetNode::getAlignment() const {
370 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
371 E = AttrList.end(); I != E; ++I)
372 if (*I == Attribute::Alignment)
373 return I->getAlignment();
377 unsigned AttributeSetNode::getStackAlignment() const {
378 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
379 E = AttrList.end(); I != E; ++I)
380 if (*I == Attribute::StackAlignment)
381 return I->getStackAlignment();
385 std::string AttributeSetNode::getAsString() const {
386 std::string Str = "";
387 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
388 E = AttrList.end(); I != E; ++I) {
389 if (I != AttrList.begin()) Str += " ";
390 Str += I->getAsString();
395 //===----------------------------------------------------------------------===//
396 // AttributeSetImpl Definition
397 //===----------------------------------------------------------------------===//
399 uint64_t AttributeSetImpl::Raw(uint64_t Index) const {
400 for (unsigned I = 0, E = getNumAttributes(); I != E; ++I) {
401 if (getSlotIndex(I) != Index) continue;
402 const AttributeSetNode *ASN = AttrNodes[I].second;
405 for (AttributeSetNode::const_iterator II = ASN->begin(),
406 IE = ASN->end(); II != IE; ++II)
407 B.addAttributes(*II);
414 //===----------------------------------------------------------------------===//
415 // AttributeSet Construction and Mutation Methods
416 //===----------------------------------------------------------------------===//
419 AttributeSet::getImpl(LLVMContext &C,
420 ArrayRef<std::pair<unsigned, AttributeSetNode*> > Attrs) {
421 LLVMContextImpl *pImpl = C.pImpl;
423 AttributeSetImpl::Profile(ID, Attrs);
426 AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
428 // If we didn't find any existing attributes of the same shape then
429 // create a new one and insert it.
431 PA = new AttributeSetImpl(C, Attrs);
432 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
435 // Return the AttributesList that we found or created.
436 return AttributeSet(PA);
439 AttributeSet AttributeSet::get(LLVMContext &C,
440 ArrayRef<std::pair<unsigned, Attribute> > Attrs){
441 // If there are no attributes then return a null AttributesList pointer.
443 return AttributeSet();
446 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
447 assert((!i || Attrs[i-1].first <= Attrs[i].first) &&
448 "Misordered Attributes list!");
449 assert(Attrs[i].second != Attribute::None &&
450 "Pointless attribute!");
454 // Create a vector if (unsigned, AttributeSetNode*) pairs from the attributes
456 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrPairVec;
457 for (ArrayRef<std::pair<unsigned, Attribute> >::iterator I = Attrs.begin(),
458 E = Attrs.end(); I != E; ) {
459 unsigned Index = I->first;
460 SmallVector<Attribute, 4> AttrVec;
461 while (I != E && I->first == Index) {
462 AttrVec.push_back(I->second);
466 AttrPairVec.push_back(std::make_pair(Index,
467 AttributeSetNode::get(C, AttrVec)));
470 return getImpl(C, AttrPairVec);
473 AttributeSet AttributeSet::get(LLVMContext &C,
474 ArrayRef<std::pair<unsigned,
475 AttributeSetNode*> > Attrs) {
476 // If there are no attributes then return a null AttributesList pointer.
478 return AttributeSet();
480 return getImpl(C, Attrs);
483 AttributeSet AttributeSet::get(LLVMContext &C, unsigned Idx, AttrBuilder &B) {
484 if (!B.hasAttributes())
485 return AttributeSet();
487 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
488 for (AttrBuilder::iterator I = B.begin(), E = B.end(); I != E; ++I) {
489 Attribute::AttrKind Kind = *I;
490 if (Kind == Attribute::Alignment)
491 Attrs.push_back(std::make_pair(Idx, Attribute::
492 getWithAlignment(C, B.getAlignment())));
493 else if (Kind == Attribute::StackAlignment)
494 Attrs.push_back(std::make_pair(Idx, Attribute::
495 getWithStackAlignment(C, B.getStackAlignment())));
497 Attrs.push_back(std::make_pair(Idx, Attribute::get(C, Kind)));
500 return get(C, Attrs);
503 AttributeSet AttributeSet::get(LLVMContext &C, unsigned Idx,
504 ArrayRef<Attribute::AttrKind> Kind) {
505 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
506 for (ArrayRef<Attribute::AttrKind>::iterator I = Kind.begin(),
507 E = Kind.end(); I != E; ++I)
508 Attrs.push_back(std::make_pair(Idx, Attribute::get(C, *I)));
509 return get(C, Attrs);
512 AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<AttributeSet> Attrs) {
513 if (Attrs.empty()) return AttributeSet();
515 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrNodeVec;
516 for (unsigned I = 0, E = Attrs.size(); I != E; ++I) {
517 AttributeSet AS = Attrs[I];
518 if (!AS.pImpl) continue;
519 AttrNodeVec.append(AS.pImpl->AttrNodes.begin(), AS.pImpl->AttrNodes.end());
522 return getImpl(C, AttrNodeVec);
525 AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Idx,
526 Attribute::AttrKind Attr) const {
527 return addAttributes(C, Idx, AttributeSet::get(C, Idx, Attr));
530 AttributeSet AttributeSet::addAttributes(LLVMContext &C, unsigned Idx,
531 AttributeSet Attrs) const {
532 if (!pImpl) return Attrs;
533 if (!Attrs.pImpl) return *this;
536 // FIXME it is not obvious how this should work for alignment. For now, say
537 // we can't change a known alignment.
538 unsigned OldAlign = getParamAlignment(Idx);
539 unsigned NewAlign = Attrs.getParamAlignment(Idx);
540 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
541 "Attempt to change alignment!");
544 // Add the attribute slots before the one we're trying to add.
545 SmallVector<AttributeSet, 4> AttrSet;
546 uint64_t NumAttrs = pImpl->getNumAttributes();
548 uint64_t LastIndex = 0;
549 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
550 if (getSlotIndex(I) >= Idx) {
551 if (getSlotIndex(I) == Idx) AS = getSlotAttributes(LastIndex++);
555 AttrSet.push_back(getSlotAttributes(I));
558 // Now add the attribute into the correct slot. There may already be an
559 // AttributeSet there.
560 AttrBuilder B(AS, Idx);
562 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
563 if (Attrs.getSlotIndex(I) == Idx) {
564 for (AttributeSetImpl::const_iterator II = Attrs.pImpl->begin(I),
565 IE = Attrs.pImpl->end(I); II != IE; ++II)
566 B.addAttributes(*II);
570 AttrSet.push_back(AttributeSet::get(C, Idx, B));
572 // Add the remaining attribute slots.
573 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
574 AttrSet.push_back(getSlotAttributes(I));
576 return get(C, AttrSet);
579 AttributeSet AttributeSet::removeAttribute(LLVMContext &C, unsigned Idx,
580 Attribute::AttrKind Attr) const {
581 return removeAttributes(C, Idx, AttributeSet::get(C, Idx, Attr));
584 AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Idx,
585 AttributeSet Attrs) const {
586 if (!pImpl) return AttributeSet();
587 if (!Attrs.pImpl) return *this;
590 // FIXME it is not obvious how this should work for alignment.
591 // For now, say we can't pass in alignment, which no current use does.
592 assert(!Attrs.hasAttribute(Idx, Attribute::Alignment) &&
593 "Attempt to change alignment!");
596 // Add the attribute slots before the one we're trying to add.
597 SmallVector<AttributeSet, 4> AttrSet;
598 uint64_t NumAttrs = pImpl->getNumAttributes();
600 uint64_t LastIndex = 0;
601 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
602 if (getSlotIndex(I) >= Idx) {
603 if (getSlotIndex(I) == Idx) AS = getSlotAttributes(LastIndex++);
607 AttrSet.push_back(getSlotAttributes(I));
610 // Now remove the attribute from the correct slot. There may already be an
611 // AttributeSet there.
612 AttrBuilder B(AS, Idx);
614 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
615 if (Attrs.getSlotIndex(I) == Idx) {
616 B.removeAttributes(Attrs.pImpl->getSlotAttributes(I), Idx);
620 AttrSet.push_back(AttributeSet::get(C, Idx, B));
622 // Add the remaining attribute slots.
623 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
624 AttrSet.push_back(getSlotAttributes(I));
626 return get(C, AttrSet);
629 //===----------------------------------------------------------------------===//
630 // AttributeSet Accessor Methods
631 //===----------------------------------------------------------------------===//
633 AttributeSet AttributeSet::getParamAttributes(unsigned Idx) const {
634 return pImpl && hasAttributes(Idx) ?
635 AttributeSet::get(pImpl->getContext(),
636 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
637 std::make_pair(Idx, getAttributes(Idx)))) :
641 AttributeSet AttributeSet::getRetAttributes() const {
642 return pImpl && hasAttributes(ReturnIndex) ?
643 AttributeSet::get(pImpl->getContext(),
644 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
645 std::make_pair(ReturnIndex,
646 getAttributes(ReturnIndex)))) :
650 AttributeSet AttributeSet::getFnAttributes() const {
651 return pImpl && hasAttributes(FunctionIndex) ?
652 AttributeSet::get(pImpl->getContext(),
653 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
654 std::make_pair(FunctionIndex,
655 getAttributes(FunctionIndex)))) :
659 bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{
660 AttributeSetNode *ASN = getAttributes(Index);
661 return ASN ? ASN->hasAttribute(Kind) : false;
664 bool AttributeSet::hasAttributes(unsigned Index) const {
665 AttributeSetNode *ASN = getAttributes(Index);
666 return ASN ? ASN->hasAttributes() : false;
669 /// \brief Return true if the specified attribute is set for at least one
670 /// parameter or for the return value.
671 bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr) const {
672 if (pImpl == 0) return false;
674 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
675 for (AttributeSetImpl::const_iterator II = pImpl->begin(I),
676 IE = pImpl->end(I); II != IE; ++II)
683 unsigned AttributeSet::getParamAlignment(unsigned Index) const {
684 AttributeSetNode *ASN = getAttributes(Index);
685 return ASN ? ASN->getAlignment() : 0;
688 unsigned AttributeSet::getStackAlignment(unsigned Index) const {
689 AttributeSetNode *ASN = getAttributes(Index);
690 return ASN ? ASN->getStackAlignment() : 0;
693 std::string AttributeSet::getAsString(unsigned Index) const {
694 AttributeSetNode *ASN = getAttributes(Index);
695 return ASN ? ASN->getAsString() : std::string("");
698 /// \brief The attributes for the specified index are returned.
699 AttributeSetNode *AttributeSet::getAttributes(unsigned Idx) const {
700 if (!pImpl) return 0;
702 // Loop through to find the attribute node we want.
703 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
704 if (pImpl->getSlotIndex(I) == Idx)
705 return pImpl->getSlotNode(I);
710 //===----------------------------------------------------------------------===//
711 // AttributeSet Introspection Methods
712 //===----------------------------------------------------------------------===//
714 /// \brief Return the number of slots used in this attribute list. This is the
715 /// number of arguments that have an attribute set on them (including the
716 /// function itself).
717 unsigned AttributeSet::getNumSlots() const {
718 return pImpl ? pImpl->getNumAttributes() : 0;
721 uint64_t AttributeSet::getSlotIndex(unsigned Slot) const {
722 assert(pImpl && Slot < pImpl->getNumAttributes() &&
723 "Slot # out of range!");
724 return pImpl->getSlotIndex(Slot);
727 AttributeSet AttributeSet::getSlotAttributes(unsigned Slot) const {
728 assert(pImpl && Slot < pImpl->getNumAttributes() &&
729 "Slot # out of range!");
730 return pImpl->getSlotAttributes(Slot);
733 uint64_t AttributeSet::Raw(unsigned Index) const {
734 // FIXME: Remove this.
735 return pImpl ? pImpl->Raw(Index) : 0;
738 void AttributeSet::dump() const {
741 for (unsigned i = 0, e = getNumSlots(); i < e; ++i) {
742 uint64_t Index = getSlotIndex(i);
748 dbgs() << " => " << getAsString(Index) << " }\n";
754 //===----------------------------------------------------------------------===//
755 // AttrBuilder Method Implementations
756 //===----------------------------------------------------------------------===//
758 AttrBuilder::AttrBuilder(AttributeSet AS, unsigned Idx)
759 : Alignment(0), StackAlignment(0) {
760 AttributeSetImpl *pImpl = AS.pImpl;
763 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I) {
764 if (pImpl->getSlotIndex(I) != Idx) continue;
766 for (AttributeSetImpl::const_iterator II = pImpl->begin(I),
767 IE = pImpl->end(I); II != IE; ++II)
774 void AttrBuilder::clear() {
776 Alignment = StackAlignment = 0;
779 AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
784 AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
786 if (Val == Attribute::Alignment)
788 else if (Val == Attribute::StackAlignment)
794 AttrBuilder &AttrBuilder::addAttributes(Attribute Attr) {
795 uint64_t Mask = Attr.Raw();
797 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
798 I = Attribute::AttrKind(I + 1))
799 if ((Mask & AttributeImpl::getAttrMask(I)) != 0)
802 if (Attr.getAlignment())
803 Alignment = Attr.getAlignment();
804 if (Attr.getStackAlignment())
805 StackAlignment = Attr.getStackAlignment();
809 AttrBuilder &AttrBuilder::removeAttributes(AttributeSet A, uint64_t Index) {
810 uint64_t Mask = A.Raw(Index);
812 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
813 I = Attribute::AttrKind(I + 1)) {
814 if (Mask & AttributeImpl::getAttrMask(I)) {
817 if (I == Attribute::Alignment)
819 else if (I == Attribute::StackAlignment)
827 AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
828 if (Align == 0) return *this;
830 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
831 assert(Align <= 0x40000000 && "Alignment too large.");
833 Attrs.insert(Attribute::Alignment);
838 AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
839 // Default alignment, allow the target to define how to align it.
840 if (Align == 0) return *this;
842 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
843 assert(Align <= 0x100 && "Alignment too large.");
845 Attrs.insert(Attribute::StackAlignment);
846 StackAlignment = Align;
850 bool AttrBuilder::contains(Attribute::AttrKind A) const {
851 return Attrs.count(A);
854 bool AttrBuilder::hasAttributes() const {
855 return !Attrs.empty();
858 bool AttrBuilder::hasAttributes(AttributeSet A, uint64_t Index) const {
859 return Raw() & A.Raw(Index);
862 bool AttrBuilder::hasAlignmentAttr() const {
863 return Alignment != 0;
866 bool AttrBuilder::operator==(const AttrBuilder &B) {
867 SmallVector<Attribute::AttrKind, 8> This(Attrs.begin(), Attrs.end());
868 SmallVector<Attribute::AttrKind, 8> That(B.Attrs.begin(), B.Attrs.end());
872 AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) {
873 if (!Val) return *this;
875 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
876 I = Attribute::AttrKind(I + 1)) {
877 if (uint64_t A = (Val & AttributeImpl::getAttrMask(I))) {
880 if (I == Attribute::Alignment)
881 Alignment = 1ULL << ((A >> 16) - 1);
882 else if (I == Attribute::StackAlignment)
883 StackAlignment = 1ULL << ((A >> 26)-1);
890 uint64_t AttrBuilder::Raw() const {
893 for (DenseSet<Attribute::AttrKind>::const_iterator I = Attrs.begin(),
894 E = Attrs.end(); I != E; ++I) {
895 Attribute::AttrKind Kind = *I;
897 if (Kind == Attribute::Alignment)
898 Mask |= (Log2_32(Alignment) + 1) << 16;
899 else if (Kind == Attribute::StackAlignment)
900 Mask |= (Log2_32(StackAlignment) + 1) << 26;
902 Mask |= AttributeImpl::getAttrMask(Kind);
908 //===----------------------------------------------------------------------===//
909 // AttributeFuncs Function Defintions
910 //===----------------------------------------------------------------------===//
912 AttributeSet AttributeFuncs::typeIncompatible(Type *Ty, uint64_t Index) {
913 AttrBuilder Incompatible;
915 if (!Ty->isIntegerTy())
916 // Attribute that only apply to integers.
917 Incompatible.addAttribute(Attribute::SExt)
918 .addAttribute(Attribute::ZExt);
920 if (!Ty->isPointerTy())
921 // Attribute that only apply to pointers.
922 Incompatible.addAttribute(Attribute::ByVal)
923 .addAttribute(Attribute::Nest)
924 .addAttribute(Attribute::NoAlias)
925 .addAttribute(Attribute::NoCapture)
926 .addAttribute(Attribute::StructRet);
928 return AttributeSet::get(Ty->getContext(), Index, Incompatible);
931 /// \brief This returns an integer containing an encoding of all the LLVM
932 /// attributes found in the given attribute bitset. Any change to this encoding
933 /// is a breaking change to bitcode compatibility.
934 /// N.B. This should be used only by the bitcode reader!
935 uint64_t AttributeFuncs::encodeLLVMAttributesForBitcode(AttributeSet Attrs,
937 // FIXME: It doesn't make sense to store the alignment information as an
938 // expanded out value, we should store it as a log2 value. However, we can't
939 // just change that here without breaking bitcode compatibility. If this ever
940 // becomes a problem in practice, we should introduce new tag numbers in the
941 // bitcode file and have those tags use a more efficiently encoded alignment
944 // Store the alignment in the bitcode as a 16-bit raw value instead of a 5-bit
945 // log2 encoded value. Shift the bits above the alignment up by 11 bits.
946 uint64_t EncodedAttrs = Attrs.Raw(Index) & 0xffff;
947 if (Attrs.hasAttribute(Index, Attribute::Alignment))
948 EncodedAttrs |= Attrs.getParamAlignment(Index) << 16;
949 EncodedAttrs |= (Attrs.Raw(Index) & (0xffffULL << 21)) << 11;
953 /// \brief This fills an AttrBuilder object with the LLVM attributes that have
954 /// been decoded from the given integer. This function must stay in sync with
955 /// 'encodeLLVMAttributesForBitcode'.
956 /// N.B. This should be used only by the bitcode reader!
957 void AttributeFuncs::decodeLLVMAttributesForBitcode(LLVMContext &C,
959 uint64_t EncodedAttrs) {
960 // The alignment is stored as a 16-bit raw value from bits 31--16. We shift
961 // the bits above 31 down by 11 bits.
962 unsigned Alignment = (EncodedAttrs & (0xffffULL << 16)) >> 16;
963 assert((!Alignment || isPowerOf2_32(Alignment)) &&
964 "Alignment must be a power of two.");
967 B.addAlignmentAttr(Alignment);
968 B.addRawValue(((EncodedAttrs & (0xffffULL << 32)) >> 11) |
969 (EncodedAttrs & 0xffff));