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, Attribute::AttrKind Kind,
35 LLVMContextImpl *pImpl = Context.pImpl;
38 if (Val) ID.AddInteger(Val);
41 AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
44 // If we didn't find any existing attributes of the same shape then create a
45 // new one and insert it.
47 new AttributeImpl(Context, Kind) :
48 new AttributeImpl(Context, Kind, Val);
49 pImpl->AttrsSet.InsertNode(PA, InsertPoint);
52 // Return the Attribute that we found or created.
56 Attribute Attribute::get(LLVMContext &Context, StringRef Kind, StringRef Val) {
57 LLVMContextImpl *pImpl = Context.pImpl;
60 if (!Val.empty()) ID.AddString(Val);
63 AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
66 // If we didn't find any existing attributes of the same shape then create a
67 // new one and insert it.
68 PA = new AttributeImpl(Context, Kind, Val);
69 pImpl->AttrsSet.InsertNode(PA, InsertPoint);
72 // Return the Attribute that we found or created.
76 Attribute Attribute::getWithAlignment(LLVMContext &Context, uint64_t Align) {
77 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
78 assert(Align <= 0x40000000 && "Alignment too large.");
79 return get(Context, Alignment, Align);
82 Attribute Attribute::getWithStackAlignment(LLVMContext &Context,
84 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
85 assert(Align <= 0x100 && "Alignment too large.");
86 return get(Context, StackAlignment, Align);
89 //===----------------------------------------------------------------------===//
90 // Attribute Accessor Methods
91 //===----------------------------------------------------------------------===//
93 bool Attribute::isEnumAttribute() const {
94 return pImpl && pImpl->isEnumAttribute();
97 bool Attribute::isAlignAttribute() const {
98 return pImpl && pImpl->isAlignAttribute();
101 bool Attribute::isStringAttribute() const {
102 return pImpl && pImpl->isStringAttribute();
105 Attribute::AttrKind Attribute::getKindAsEnum() const {
106 assert((isEnumAttribute() || isAlignAttribute()) &&
107 "Invalid attribute type to get the kind as an enum!");
108 return pImpl ? pImpl->getKindAsEnum() : None;
111 uint64_t Attribute::getValueAsInt() const {
112 assert(isAlignAttribute() &&
113 "Expected the attribute to be an alignment attribute!");
114 return pImpl ? pImpl->getValueAsInt() : 0;
117 StringRef Attribute::getKindAsString() const {
118 assert(isStringAttribute() &&
119 "Invalid attribute type to get the kind as a string!");
120 return pImpl ? pImpl->getKindAsString() : StringRef();
123 StringRef Attribute::getValueAsString() const {
124 assert(isStringAttribute() &&
125 "Invalid attribute type to get the value as a string!");
126 return pImpl ? pImpl->getValueAsString() : StringRef();
129 bool Attribute::hasAttribute(AttrKind Kind) const {
130 return (pImpl && pImpl->hasAttribute(Kind)) || (!pImpl && Kind == None);
133 bool Attribute::hasAttribute(StringRef Kind) const {
134 if (!isStringAttribute()) return false;
135 return pImpl && pImpl->hasAttribute(Kind);
138 /// This returns the alignment field of an attribute as a byte alignment value.
139 unsigned Attribute::getAlignment() const {
140 assert(hasAttribute(Attribute::Alignment) &&
141 "Trying to get alignment from non-alignment attribute!");
142 return pImpl->getValueAsInt();
145 /// This returns the stack alignment field of an attribute as a byte alignment
147 unsigned Attribute::getStackAlignment() const {
148 assert(hasAttribute(Attribute::StackAlignment) &&
149 "Trying to get alignment from non-alignment attribute!");
150 return pImpl->getValueAsInt();
153 std::string Attribute::getAsString(bool InAttrGrp) const {
154 if (!pImpl) return "";
156 if (hasAttribute(Attribute::AddressSafety))
157 return "address_safety";
158 if (hasAttribute(Attribute::AlwaysInline))
159 return "alwaysinline";
160 if (hasAttribute(Attribute::ByVal))
162 if (hasAttribute(Attribute::InlineHint))
164 if (hasAttribute(Attribute::InReg))
166 if (hasAttribute(Attribute::MinSize))
168 if (hasAttribute(Attribute::Naked))
170 if (hasAttribute(Attribute::Nest))
172 if (hasAttribute(Attribute::NoAlias))
174 if (hasAttribute(Attribute::NoCapture))
176 if (hasAttribute(Attribute::NoDuplicate))
177 return "noduplicate";
178 if (hasAttribute(Attribute::NoImplicitFloat))
179 return "noimplicitfloat";
180 if (hasAttribute(Attribute::NoInline))
182 if (hasAttribute(Attribute::NonLazyBind))
183 return "nonlazybind";
184 if (hasAttribute(Attribute::NoRedZone))
186 if (hasAttribute(Attribute::NoReturn))
188 if (hasAttribute(Attribute::NoUnwind))
190 if (hasAttribute(Attribute::OptimizeForSize))
192 if (hasAttribute(Attribute::ReadNone))
194 if (hasAttribute(Attribute::ReadOnly))
196 if (hasAttribute(Attribute::ReturnsTwice))
197 return "returns_twice";
198 if (hasAttribute(Attribute::SExt))
200 if (hasAttribute(Attribute::StackProtect))
202 if (hasAttribute(Attribute::StackProtectReq))
204 if (hasAttribute(Attribute::StackProtectStrong))
206 if (hasAttribute(Attribute::StructRet))
208 if (hasAttribute(Attribute::ThreadSafety))
209 return "thread_safety";
210 if (hasAttribute(Attribute::UninitializedChecks))
211 return "uninitialized_checks";
212 if (hasAttribute(Attribute::UWTable))
214 if (hasAttribute(Attribute::ZExt))
217 // FIXME: These should be output like this:
222 if (hasAttribute(Attribute::Alignment)) {
225 Result += (InAttrGrp) ? "=" : " ";
226 Result += utostr(getValueAsInt());
230 if (hasAttribute(Attribute::StackAlignment)) {
232 Result += "alignstack";
235 Result += utostr(getValueAsInt());
238 Result += utostr(getValueAsInt());
244 // Convert target-dependent attributes to strings of the form:
249 if (isStringAttribute()) {
251 Result += '\"' + getKindAsString().str() + '"';
253 StringRef Val = pImpl->getValueAsString();
254 if (Val.empty()) return Result;
256 Result += "=\"" + Val.str() + '"';
260 llvm_unreachable("Unknown attribute");
263 bool Attribute::operator<(Attribute A) const {
264 if (!pImpl && !A.pImpl) return false;
265 if (!pImpl) return true;
266 if (!A.pImpl) return false;
267 return *pImpl < *A.pImpl;
270 //===----------------------------------------------------------------------===//
271 // AttributeImpl Definition
272 //===----------------------------------------------------------------------===//
274 AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind Kind)
275 : Context(C), Entry(new EnumAttributeEntry(Kind)) {}
277 AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind Kind,
280 assert((Kind == Attribute::Alignment || Kind == Attribute::StackAlignment) &&
281 "Wrong kind for alignment attribute!");
282 Entry = new AlignAttributeEntry(Kind, Align);
285 AttributeImpl::AttributeImpl(LLVMContext &C, StringRef Kind, StringRef Val)
286 : Context(C), Entry(new StringAttributeEntry(Kind, Val)) {}
288 AttributeImpl::~AttributeImpl() {
292 bool AttributeImpl::isEnumAttribute() const {
293 return isa<EnumAttributeEntry>(Entry);
296 bool AttributeImpl::isAlignAttribute() const {
297 return isa<AlignAttributeEntry>(Entry);
300 bool AttributeImpl::isStringAttribute() const {
301 return isa<StringAttributeEntry>(Entry);
304 bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
305 if (isStringAttribute()) return false;
306 return getKindAsEnum() == A;
309 bool AttributeImpl::hasAttribute(StringRef Kind) const {
310 if (!isStringAttribute()) return false;
311 return getKindAsString() == Kind;
314 Attribute::AttrKind AttributeImpl::getKindAsEnum() const {
315 if (EnumAttributeEntry *E = dyn_cast<EnumAttributeEntry>(Entry))
316 return E->getEnumKind();
317 return cast<AlignAttributeEntry>(Entry)->getEnumKind();
320 uint64_t AttributeImpl::getValueAsInt() const {
321 return cast<AlignAttributeEntry>(Entry)->getAlignment();
324 StringRef AttributeImpl::getKindAsString() const {
325 return cast<StringAttributeEntry>(Entry)->getStringKind();
328 StringRef AttributeImpl::getValueAsString() const {
329 return cast<StringAttributeEntry>(Entry)->getStringValue();
332 bool AttributeImpl::operator<(const AttributeImpl &AI) const {
333 // This sorts the attributes with Attribute::AttrKinds coming first (sorted
334 // relative to their enum value) and then strings.
335 if (isEnumAttribute())
336 if (AI.isAlignAttribute() || AI.isEnumAttribute())
337 return getKindAsEnum() < AI.getKindAsEnum();
339 if (isAlignAttribute()) {
340 if (!AI.isStringAttribute() && getKindAsEnum() < AI.getKindAsEnum())
342 if (AI.isAlignAttribute())
343 return getValueAsInt() < AI.getValueAsInt();
346 if (isStringAttribute()) {
347 if (!AI.isStringAttribute()) return false;
348 if (getKindAsString() < AI.getKindAsString()) return true;
349 if (getKindAsString() == AI.getKindAsString())
350 return getValueAsString() < AI.getValueAsString();
356 uint64_t AttributeImpl::getAttrMask(Attribute::AttrKind Val) {
357 // FIXME: Remove this.
359 case Attribute::EndAttrKinds:
360 case Attribute::AttrKindEmptyKey:
361 case Attribute::AttrKindTombstoneKey:
362 llvm_unreachable("Synthetic enumerators which should never get here");
364 case Attribute::None: return 0;
365 case Attribute::ZExt: return 1 << 0;
366 case Attribute::SExt: return 1 << 1;
367 case Attribute::NoReturn: return 1 << 2;
368 case Attribute::InReg: return 1 << 3;
369 case Attribute::StructRet: return 1 << 4;
370 case Attribute::NoUnwind: return 1 << 5;
371 case Attribute::NoAlias: return 1 << 6;
372 case Attribute::ByVal: return 1 << 7;
373 case Attribute::Nest: return 1 << 8;
374 case Attribute::ReadNone: return 1 << 9;
375 case Attribute::ReadOnly: return 1 << 10;
376 case Attribute::NoInline: return 1 << 11;
377 case Attribute::AlwaysInline: return 1 << 12;
378 case Attribute::OptimizeForSize: return 1 << 13;
379 case Attribute::StackProtect: return 1 << 14;
380 case Attribute::StackProtectReq: return 1 << 15;
381 case Attribute::Alignment: return 31 << 16;
382 case Attribute::NoCapture: return 1 << 21;
383 case Attribute::NoRedZone: return 1 << 22;
384 case Attribute::NoImplicitFloat: return 1 << 23;
385 case Attribute::Naked: return 1 << 24;
386 case Attribute::InlineHint: return 1 << 25;
387 case Attribute::StackAlignment: return 7 << 26;
388 case Attribute::ReturnsTwice: return 1 << 29;
389 case Attribute::UWTable: return 1 << 30;
390 case Attribute::NonLazyBind: return 1U << 31;
391 case Attribute::AddressSafety: return 1ULL << 32;
392 case Attribute::MinSize: return 1ULL << 33;
393 case Attribute::NoDuplicate: return 1ULL << 34;
394 case Attribute::StackProtectStrong: return 1ULL << 35;
395 case Attribute::ThreadSafety: return 1ULL << 36;
396 case Attribute::UninitializedChecks: return 1ULL << 37;
398 llvm_unreachable("Unsupported attribute type");
401 //===----------------------------------------------------------------------===//
402 // AttributeSetNode Definition
403 //===----------------------------------------------------------------------===//
405 AttributeSetNode *AttributeSetNode::get(LLVMContext &C,
406 ArrayRef<Attribute> Attrs) {
410 // Otherwise, build a key to look up the existing attributes.
411 LLVMContextImpl *pImpl = C.pImpl;
414 SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end());
415 std::sort(SortedAttrs.begin(), SortedAttrs.end());
417 for (SmallVectorImpl<Attribute>::iterator I = SortedAttrs.begin(),
418 E = SortedAttrs.end(); I != E; ++I)
422 AttributeSetNode *PA =
423 pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint);
425 // If we didn't find any existing attributes of the same shape then create a
426 // new one and insert it.
428 PA = new AttributeSetNode(SortedAttrs);
429 pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint);
432 // Return the AttributesListNode that we found or created.
436 bool AttributeSetNode::hasAttribute(Attribute::AttrKind Kind) const {
437 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
438 E = AttrList.end(); I != E; ++I)
439 if (I->hasAttribute(Kind))
444 unsigned AttributeSetNode::getAlignment() const {
445 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
446 E = AttrList.end(); I != E; ++I)
447 if (I->hasAttribute(Attribute::Alignment))
448 return I->getAlignment();
452 unsigned AttributeSetNode::getStackAlignment() const {
453 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
454 E = AttrList.end(); I != E; ++I)
455 if (I->hasAttribute(Attribute::StackAlignment))
456 return I->getStackAlignment();
460 std::string AttributeSetNode::getAsString(bool InAttrGrp) const {
461 std::string Str = "";
462 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
463 E = AttrList.end(); I != E; ) {
464 Str += I->getAsString(InAttrGrp);
465 if (++I != E) Str += " ";
470 //===----------------------------------------------------------------------===//
471 // AttributeSetImpl Definition
472 //===----------------------------------------------------------------------===//
474 uint64_t AttributeSetImpl::Raw(uint64_t Index) const {
475 for (unsigned I = 0, E = getNumAttributes(); I != E; ++I) {
476 if (getSlotIndex(I) != Index) continue;
477 const AttributeSetNode *ASN = AttrNodes[I].second;
480 for (AttributeSetNode::const_iterator II = ASN->begin(),
481 IE = ASN->end(); II != IE; ++II) {
482 Attribute Attr = *II;
484 // This cannot handle string attributes.
485 if (Attr.isStringAttribute()) continue;
487 Attribute::AttrKind Kind = Attr.getKindAsEnum();
489 if (Kind == Attribute::Alignment)
490 Mask |= (Log2_32(ASN->getAlignment()) + 1) << 16;
491 else if (Kind == Attribute::StackAlignment)
492 Mask |= (Log2_32(ASN->getStackAlignment()) + 1) << 26;
494 Mask |= AttributeImpl::getAttrMask(Kind);
503 //===----------------------------------------------------------------------===//
504 // AttributeSet Construction and Mutation Methods
505 //===----------------------------------------------------------------------===//
508 AttributeSet::getImpl(LLVMContext &C,
509 ArrayRef<std::pair<unsigned, AttributeSetNode*> > Attrs) {
510 LLVMContextImpl *pImpl = C.pImpl;
512 AttributeSetImpl::Profile(ID, Attrs);
515 AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
517 // If we didn't find any existing attributes of the same shape then
518 // create a new one and insert it.
520 PA = new AttributeSetImpl(C, Attrs);
521 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
524 // Return the AttributesList that we found or created.
525 return AttributeSet(PA);
528 AttributeSet AttributeSet::get(LLVMContext &C,
529 ArrayRef<std::pair<unsigned, Attribute> > Attrs){
530 // If there are no attributes then return a null AttributesList pointer.
532 return AttributeSet();
535 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
536 assert((!i || Attrs[i-1].first <= Attrs[i].first) &&
537 "Misordered Attributes list!");
538 assert(!Attrs[i].second.hasAttribute(Attribute::None) &&
539 "Pointless attribute!");
543 // Create a vector if (unsigned, AttributeSetNode*) pairs from the attributes
545 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrPairVec;
546 for (ArrayRef<std::pair<unsigned, Attribute> >::iterator I = Attrs.begin(),
547 E = Attrs.end(); I != E; ) {
548 unsigned Index = I->first;
549 SmallVector<Attribute, 4> AttrVec;
550 while (I != E && I->first == Index) {
551 AttrVec.push_back(I->second);
555 AttrPairVec.push_back(std::make_pair(Index,
556 AttributeSetNode::get(C, AttrVec)));
559 return getImpl(C, AttrPairVec);
562 AttributeSet AttributeSet::get(LLVMContext &C,
563 ArrayRef<std::pair<unsigned,
564 AttributeSetNode*> > Attrs) {
565 // If there are no attributes then return a null AttributesList pointer.
567 return AttributeSet();
569 return getImpl(C, Attrs);
572 AttributeSet AttributeSet::get(LLVMContext &C, unsigned Idx, AttrBuilder &B) {
573 if (!B.hasAttributes())
574 return AttributeSet();
576 // Add target-independent attributes.
577 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
578 for (AttrBuilder::iterator I = B.begin(), E = B.end(); I != E; ++I) {
579 Attribute::AttrKind Kind = *I;
580 if (Kind == Attribute::Alignment)
581 Attrs.push_back(std::make_pair(Idx, Attribute::
582 getWithAlignment(C, B.getAlignment())));
583 else if (Kind == Attribute::StackAlignment)
584 Attrs.push_back(std::make_pair(Idx, Attribute::
585 getWithStackAlignment(C, B.getStackAlignment())));
587 Attrs.push_back(std::make_pair(Idx, Attribute::get(C, Kind)));
590 // Add target-dependent (string) attributes.
591 for (AttrBuilder::td_iterator I = B.td_begin(), E = B.td_end();
593 Attrs.push_back(std::make_pair(Idx, Attribute::get(C, I->first,I->second)));
595 return get(C, Attrs);
598 AttributeSet AttributeSet::get(LLVMContext &C, unsigned Idx,
599 ArrayRef<Attribute::AttrKind> Kind) {
600 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
601 for (ArrayRef<Attribute::AttrKind>::iterator I = Kind.begin(),
602 E = Kind.end(); I != E; ++I)
603 Attrs.push_back(std::make_pair(Idx, Attribute::get(C, *I)));
604 return get(C, Attrs);
607 AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<AttributeSet> Attrs) {
608 if (Attrs.empty()) return AttributeSet();
610 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrNodeVec;
611 for (unsigned I = 0, E = Attrs.size(); I != E; ++I) {
612 AttributeSet AS = Attrs[I];
613 if (!AS.pImpl) continue;
614 AttrNodeVec.append(AS.pImpl->AttrNodes.begin(), AS.pImpl->AttrNodes.end());
617 return getImpl(C, AttrNodeVec);
620 AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Idx,
621 Attribute::AttrKind Attr) const {
622 return addAttributes(C, Idx, AttributeSet::get(C, Idx, Attr));
625 AttributeSet AttributeSet::addAttributes(LLVMContext &C, unsigned Idx,
626 AttributeSet Attrs) const {
627 if (!pImpl) return Attrs;
628 if (!Attrs.pImpl) return *this;
631 // FIXME it is not obvious how this should work for alignment. For now, say
632 // we can't change a known alignment.
633 unsigned OldAlign = getParamAlignment(Idx);
634 unsigned NewAlign = Attrs.getParamAlignment(Idx);
635 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
636 "Attempt to change alignment!");
639 // Add the attribute slots before the one we're trying to add.
640 SmallVector<AttributeSet, 4> AttrSet;
641 uint64_t NumAttrs = pImpl->getNumAttributes();
643 uint64_t LastIndex = 0;
644 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
645 if (getSlotIndex(I) >= Idx) {
646 if (getSlotIndex(I) == Idx) AS = getSlotAttributes(LastIndex++);
650 AttrSet.push_back(getSlotAttributes(I));
653 // Now add the attribute into the correct slot. There may already be an
654 // AttributeSet there.
655 AttrBuilder B(AS, Idx);
657 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
658 if (Attrs.getSlotIndex(I) == Idx) {
659 for (AttributeSetImpl::const_iterator II = Attrs.pImpl->begin(I),
660 IE = Attrs.pImpl->end(I); II != IE; ++II)
665 AttrSet.push_back(AttributeSet::get(C, Idx, B));
667 // Add the remaining attribute slots.
668 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
669 AttrSet.push_back(getSlotAttributes(I));
671 return get(C, AttrSet);
674 AttributeSet AttributeSet::removeAttribute(LLVMContext &C, unsigned Idx,
675 Attribute::AttrKind Attr) const {
676 return removeAttributes(C, Idx, AttributeSet::get(C, Idx, Attr));
679 AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Idx,
680 AttributeSet Attrs) const {
681 if (!pImpl) return AttributeSet();
682 if (!Attrs.pImpl) return *this;
685 // FIXME it is not obvious how this should work for alignment.
686 // For now, say we can't pass in alignment, which no current use does.
687 assert(!Attrs.hasAttribute(Idx, Attribute::Alignment) &&
688 "Attempt to change alignment!");
691 // Add the attribute slots before the one we're trying to add.
692 SmallVector<AttributeSet, 4> AttrSet;
693 uint64_t NumAttrs = pImpl->getNumAttributes();
695 uint64_t LastIndex = 0;
696 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
697 if (getSlotIndex(I) >= Idx) {
698 if (getSlotIndex(I) == Idx) AS = getSlotAttributes(LastIndex++);
702 AttrSet.push_back(getSlotAttributes(I));
705 // Now remove the attribute from the correct slot. There may already be an
706 // AttributeSet there.
707 AttrBuilder B(AS, Idx);
709 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
710 if (Attrs.getSlotIndex(I) == Idx) {
711 B.removeAttributes(Attrs.pImpl->getSlotAttributes(I), Idx);
715 AttrSet.push_back(AttributeSet::get(C, Idx, B));
717 // Add the remaining attribute slots.
718 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
719 AttrSet.push_back(getSlotAttributes(I));
721 return get(C, AttrSet);
724 //===----------------------------------------------------------------------===//
725 // AttributeSet Accessor Methods
726 //===----------------------------------------------------------------------===//
728 LLVMContext &AttributeSet::getContext() const {
729 return pImpl->getContext();
732 AttributeSet AttributeSet::getParamAttributes(unsigned Idx) const {
733 return pImpl && hasAttributes(Idx) ?
734 AttributeSet::get(pImpl->getContext(),
735 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
736 std::make_pair(Idx, getAttributes(Idx)))) :
740 AttributeSet AttributeSet::getRetAttributes() const {
741 return pImpl && hasAttributes(ReturnIndex) ?
742 AttributeSet::get(pImpl->getContext(),
743 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
744 std::make_pair(ReturnIndex,
745 getAttributes(ReturnIndex)))) :
749 AttributeSet AttributeSet::getFnAttributes() const {
750 return pImpl && hasAttributes(FunctionIndex) ?
751 AttributeSet::get(pImpl->getContext(),
752 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
753 std::make_pair(FunctionIndex,
754 getAttributes(FunctionIndex)))) :
758 bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{
759 AttributeSetNode *ASN = getAttributes(Index);
760 return ASN ? ASN->hasAttribute(Kind) : false;
763 bool AttributeSet::hasAttributes(unsigned Index) const {
764 AttributeSetNode *ASN = getAttributes(Index);
765 return ASN ? ASN->hasAttributes() : false;
768 /// \brief Return true if the specified attribute is set for at least one
769 /// parameter or for the return value.
770 bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr) const {
771 if (pImpl == 0) return false;
773 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
774 for (AttributeSetImpl::const_iterator II = pImpl->begin(I),
775 IE = pImpl->end(I); II != IE; ++II)
776 if (II->hasAttribute(Attr))
782 unsigned AttributeSet::getParamAlignment(unsigned Index) const {
783 AttributeSetNode *ASN = getAttributes(Index);
784 return ASN ? ASN->getAlignment() : 0;
787 unsigned AttributeSet::getStackAlignment(unsigned Index) const {
788 AttributeSetNode *ASN = getAttributes(Index);
789 return ASN ? ASN->getStackAlignment() : 0;
792 std::string AttributeSet::getAsString(unsigned Index,
793 bool InAttrGrp) const {
794 AttributeSetNode *ASN = getAttributes(Index);
795 return ASN ? ASN->getAsString(InAttrGrp) : std::string("");
798 /// \brief The attributes for the specified index are returned.
799 AttributeSetNode *AttributeSet::getAttributes(unsigned Idx) const {
800 if (!pImpl) return 0;
802 // Loop through to find the attribute node we want.
803 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
804 if (pImpl->getSlotIndex(I) == Idx)
805 return pImpl->getSlotNode(I);
810 AttributeSet::iterator AttributeSet::begin(unsigned Idx) const {
812 return ArrayRef<Attribute>().begin();
813 return pImpl->begin(Idx);
816 AttributeSet::iterator AttributeSet::end(unsigned Idx) const {
818 return ArrayRef<Attribute>().end();
819 return pImpl->end(Idx);
822 //===----------------------------------------------------------------------===//
823 // AttributeSet Introspection Methods
824 //===----------------------------------------------------------------------===//
826 /// \brief Return the number of slots used in this attribute list. This is the
827 /// number of arguments that have an attribute set on them (including the
828 /// function itself).
829 unsigned AttributeSet::getNumSlots() const {
830 return pImpl ? pImpl->getNumAttributes() : 0;
833 uint64_t AttributeSet::getSlotIndex(unsigned Slot) const {
834 assert(pImpl && Slot < pImpl->getNumAttributes() &&
835 "Slot # out of range!");
836 return pImpl->getSlotIndex(Slot);
839 AttributeSet AttributeSet::getSlotAttributes(unsigned Slot) const {
840 assert(pImpl && Slot < pImpl->getNumAttributes() &&
841 "Slot # out of range!");
842 return pImpl->getSlotAttributes(Slot);
845 uint64_t AttributeSet::Raw(unsigned Index) const {
846 // FIXME: Remove this.
847 return pImpl ? pImpl->Raw(Index) : 0;
850 void AttributeSet::dump() const {
853 for (unsigned i = 0, e = getNumSlots(); i < e; ++i) {
854 uint64_t Index = getSlotIndex(i);
860 dbgs() << " => " << getAsString(Index) << " }\n";
866 //===----------------------------------------------------------------------===//
867 // AttrBuilder Method Implementations
868 //===----------------------------------------------------------------------===//
870 AttrBuilder::AttrBuilder(AttributeSet AS, unsigned Idx)
871 : Alignment(0), StackAlignment(0) {
872 AttributeSetImpl *pImpl = AS.pImpl;
875 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I) {
876 if (pImpl->getSlotIndex(I) != Idx) continue;
878 for (AttributeSetImpl::const_iterator II = pImpl->begin(I),
879 IE = pImpl->end(I); II != IE; ++II)
886 void AttrBuilder::clear() {
888 Alignment = StackAlignment = 0;
891 AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
892 assert(Val != Attribute::Alignment && Val != Attribute::StackAlignment &&
893 "Adding alignment attribute without adding alignment value!");
898 AttrBuilder &AttrBuilder::addAttribute(Attribute Attr) {
899 if (Attr.isStringAttribute()) {
900 addAttribute(Attr.getKindAsString(), Attr.getValueAsString());
904 Attribute::AttrKind Kind = Attr.getKindAsEnum();
907 if (Kind == Attribute::Alignment)
908 Alignment = Attr.getAlignment();
909 else if (Kind == Attribute::StackAlignment)
910 StackAlignment = Attr.getStackAlignment();
914 AttrBuilder &AttrBuilder::addAttribute(StringRef A, StringRef V) {
915 TargetDepAttrs[A] = V;
919 AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
922 if (Val == Attribute::Alignment)
924 else if (Val == Attribute::StackAlignment)
930 AttrBuilder &AttrBuilder::removeAttributes(AttributeSet A, uint64_t Index) {
932 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
933 if (A.getSlotIndex(I) == Index) {
938 assert(Idx != ~0U && "Couldn't find index in AttributeSet!");
940 for (AttributeSet::iterator I = A.begin(Idx), E = A.end(Idx); I != E; ++I) {
941 // FIXME: Support string attributes.
942 Attribute::AttrKind Kind = I->getKindAsEnum();
945 if (Kind == Attribute::Alignment)
947 else if (Kind == Attribute::StackAlignment)
954 AttrBuilder &AttrBuilder::removeAttribute(StringRef A) {
955 std::map<std::string, std::string>::iterator I = TargetDepAttrs.find(A);
956 if (I != TargetDepAttrs.end())
957 TargetDepAttrs.erase(I);
961 AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
962 if (Align == 0) return *this;
964 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
965 assert(Align <= 0x40000000 && "Alignment too large.");
967 Attrs.insert(Attribute::Alignment);
972 AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
973 // Default alignment, allow the target to define how to align it.
974 if (Align == 0) return *this;
976 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
977 assert(Align <= 0x100 && "Alignment too large.");
979 Attrs.insert(Attribute::StackAlignment);
980 StackAlignment = Align;
984 AttrBuilder &AttrBuilder::merge(const AttrBuilder &B) {
985 // FIXME: What if both have alignments, but they don't match?!
987 Alignment = B.Alignment;
990 StackAlignment = B.StackAlignment;
992 Attrs.insert(B.Attrs.begin(), B.Attrs.end());
994 for (td_const_iterator I = B.TargetDepAttrs.begin(),
995 E = B.TargetDepAttrs.end(); I != E; ++I)
996 TargetDepAttrs[I->first] = I->second;
1001 bool AttrBuilder::contains(Attribute::AttrKind A) const {
1002 return Attrs.count(A);
1005 bool AttrBuilder::contains(StringRef A) const {
1006 return TargetDepAttrs.find(A) != TargetDepAttrs.end();
1009 bool AttrBuilder::hasAttributes() const {
1010 return !Attrs.empty() || !TargetDepAttrs.empty();
1013 bool AttrBuilder::hasAttributes(AttributeSet A, uint64_t Index) const {
1015 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
1016 if (A.getSlotIndex(I) == Index) {
1021 assert(Idx != ~0U && "Couldn't find the index!");
1023 for (AttributeSet::iterator I = A.begin(Idx), E = A.end(Idx);
1025 // FIXME: Support string attributes.
1026 if (Attrs.count(I->getKindAsEnum()))
1032 bool AttrBuilder::hasAlignmentAttr() const {
1033 return Alignment != 0;
1036 bool AttrBuilder::operator==(const AttrBuilder &B) {
1037 for (DenseSet<Attribute::AttrKind>::iterator I = Attrs.begin(),
1038 E = Attrs.end(); I != E; ++I)
1039 if (!B.Attrs.count(*I))
1042 for (td_const_iterator I = TargetDepAttrs.begin(),
1043 E = TargetDepAttrs.end(); I != E; ++I)
1044 if (B.TargetDepAttrs.find(I->first) == B.TargetDepAttrs.end())
1047 return Alignment == B.Alignment && StackAlignment == B.StackAlignment;
1050 AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) {
1051 // FIXME: Remove this in 4.0.
1052 if (!Val) return *this;
1054 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
1055 I = Attribute::AttrKind(I + 1)) {
1056 if (uint64_t A = (Val & AttributeImpl::getAttrMask(I))) {
1059 if (I == Attribute::Alignment)
1060 Alignment = 1ULL << ((A >> 16) - 1);
1061 else if (I == Attribute::StackAlignment)
1062 StackAlignment = 1ULL << ((A >> 26)-1);
1069 //===----------------------------------------------------------------------===//
1070 // AttributeFuncs Function Defintions
1071 //===----------------------------------------------------------------------===//
1073 /// \brief Which attributes cannot be applied to a type.
1074 AttributeSet AttributeFuncs::typeIncompatible(Type *Ty, uint64_t Index) {
1075 AttrBuilder Incompatible;
1077 if (!Ty->isIntegerTy())
1078 // Attribute that only apply to integers.
1079 Incompatible.addAttribute(Attribute::SExt)
1080 .addAttribute(Attribute::ZExt);
1082 if (!Ty->isPointerTy())
1083 // Attribute that only apply to pointers.
1084 Incompatible.addAttribute(Attribute::ByVal)
1085 .addAttribute(Attribute::Nest)
1086 .addAttribute(Attribute::NoAlias)
1087 .addAttribute(Attribute::NoCapture)
1088 .addAttribute(Attribute::StructRet);
1090 return AttributeSet::get(Ty->getContext(), Index, Incompatible);