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::NoBuiltin))
176 if (hasAttribute(Attribute::NoCapture))
178 if (hasAttribute(Attribute::NoDuplicate))
179 return "noduplicate";
180 if (hasAttribute(Attribute::NoImplicitFloat))
181 return "noimplicitfloat";
182 if (hasAttribute(Attribute::NoInline))
184 if (hasAttribute(Attribute::NonLazyBind))
185 return "nonlazybind";
186 if (hasAttribute(Attribute::NoRedZone))
188 if (hasAttribute(Attribute::NoReturn))
190 if (hasAttribute(Attribute::NoUnwind))
192 if (hasAttribute(Attribute::OptimizeForSize))
194 if (hasAttribute(Attribute::ReadNone))
196 if (hasAttribute(Attribute::ReadOnly))
198 if (hasAttribute(Attribute::ReturnsTwice))
199 return "returns_twice";
200 if (hasAttribute(Attribute::SExt))
202 if (hasAttribute(Attribute::StackProtect))
204 if (hasAttribute(Attribute::StackProtectReq))
206 if (hasAttribute(Attribute::StackProtectStrong))
208 if (hasAttribute(Attribute::StructRet))
210 if (hasAttribute(Attribute::ThreadSafety))
211 return "thread_safety";
212 if (hasAttribute(Attribute::UninitializedChecks))
213 return "uninitialized_checks";
214 if (hasAttribute(Attribute::UWTable))
216 if (hasAttribute(Attribute::ZExt))
219 // FIXME: These should be output like this:
224 if (hasAttribute(Attribute::Alignment)) {
227 Result += (InAttrGrp) ? "=" : " ";
228 Result += utostr(getValueAsInt());
232 if (hasAttribute(Attribute::StackAlignment)) {
234 Result += "alignstack";
237 Result += utostr(getValueAsInt());
240 Result += utostr(getValueAsInt());
246 // Convert target-dependent attributes to strings of the form:
251 if (isStringAttribute()) {
253 Result += '\"' + getKindAsString().str() + '"';
255 StringRef Val = pImpl->getValueAsString();
256 if (Val.empty()) return Result;
258 Result += "=\"" + Val.str() + '"';
262 llvm_unreachable("Unknown attribute");
265 bool Attribute::operator<(Attribute A) const {
266 if (!pImpl && !A.pImpl) return false;
267 if (!pImpl) return true;
268 if (!A.pImpl) return false;
269 return *pImpl < *A.pImpl;
272 //===----------------------------------------------------------------------===//
273 // AttributeImpl Definition
274 //===----------------------------------------------------------------------===//
276 AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind Kind)
277 : Context(C), Entry(new EnumAttributeEntry(Kind)) {}
279 AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind Kind,
282 assert((Kind == Attribute::Alignment || Kind == Attribute::StackAlignment) &&
283 "Wrong kind for alignment attribute!");
284 Entry = new AlignAttributeEntry(Kind, Align);
287 AttributeImpl::AttributeImpl(LLVMContext &C, StringRef Kind, StringRef Val)
288 : Context(C), Entry(new StringAttributeEntry(Kind, Val)) {}
290 AttributeImpl::~AttributeImpl() {
294 bool AttributeImpl::isEnumAttribute() const {
295 return isa<EnumAttributeEntry>(Entry);
298 bool AttributeImpl::isAlignAttribute() const {
299 return isa<AlignAttributeEntry>(Entry);
302 bool AttributeImpl::isStringAttribute() const {
303 return isa<StringAttributeEntry>(Entry);
306 bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
307 if (isStringAttribute()) return false;
308 return getKindAsEnum() == A;
311 bool AttributeImpl::hasAttribute(StringRef Kind) const {
312 if (!isStringAttribute()) return false;
313 return getKindAsString() == Kind;
316 Attribute::AttrKind AttributeImpl::getKindAsEnum() const {
317 if (EnumAttributeEntry *E = dyn_cast<EnumAttributeEntry>(Entry))
318 return E->getEnumKind();
319 return cast<AlignAttributeEntry>(Entry)->getEnumKind();
322 uint64_t AttributeImpl::getValueAsInt() const {
323 return cast<AlignAttributeEntry>(Entry)->getAlignment();
326 StringRef AttributeImpl::getKindAsString() const {
327 return cast<StringAttributeEntry>(Entry)->getStringKind();
330 StringRef AttributeImpl::getValueAsString() const {
331 return cast<StringAttributeEntry>(Entry)->getStringValue();
334 bool AttributeImpl::operator<(const AttributeImpl &AI) const {
335 // This sorts the attributes with Attribute::AttrKinds coming first (sorted
336 // relative to their enum value) and then strings.
337 if (isEnumAttribute()) {
338 if (AI.isEnumAttribute()) return getKindAsEnum() < AI.getKindAsEnum();
339 if (AI.isAlignAttribute()) return true;
340 if (AI.isStringAttribute()) return true;
343 if (isAlignAttribute()) {
344 if (AI.isEnumAttribute()) return false;
345 if (AI.isAlignAttribute()) return getValueAsInt() < AI.getValueAsInt();
346 if (AI.isStringAttribute()) return true;
349 if (AI.isEnumAttribute()) return false;
350 if (AI.isAlignAttribute()) return false;
351 if (getKindAsString() == AI.getKindAsString())
352 return getValueAsString() < AI.getValueAsString();
353 return getKindAsString() < AI.getKindAsString();
356 uint64_t AttributeImpl::getAttrMask(Attribute::AttrKind Val) {
357 // FIXME: Remove this.
360 llvm_unreachable("Unsupported attribute type");
361 case Attribute::EndAttrKinds:
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;
397 case Attribute::NoBuiltin: return 1ULL << 38;
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 array_pod_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 bool AttributeSetNode::hasAttribute(StringRef Kind) const {
445 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
446 E = AttrList.end(); I != E; ++I)
447 if (I->hasAttribute(Kind))
452 Attribute AttributeSetNode::getAttribute(Attribute::AttrKind Kind) const {
453 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
454 E = AttrList.end(); I != E; ++I)
455 if (I->hasAttribute(Kind))
460 Attribute AttributeSetNode::getAttribute(StringRef Kind) const {
461 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
462 E = AttrList.end(); I != E; ++I)
463 if (I->hasAttribute(Kind))
468 unsigned AttributeSetNode::getAlignment() const {
469 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
470 E = AttrList.end(); I != E; ++I)
471 if (I->hasAttribute(Attribute::Alignment))
472 return I->getAlignment();
476 unsigned AttributeSetNode::getStackAlignment() const {
477 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
478 E = AttrList.end(); I != E; ++I)
479 if (I->hasAttribute(Attribute::StackAlignment))
480 return I->getStackAlignment();
484 std::string AttributeSetNode::getAsString(bool InAttrGrp) const {
485 std::string Str = "";
486 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
487 E = AttrList.end(); I != E; ) {
488 Str += I->getAsString(InAttrGrp);
489 if (++I != E) Str += " ";
494 //===----------------------------------------------------------------------===//
495 // AttributeSetImpl Definition
496 //===----------------------------------------------------------------------===//
498 uint64_t AttributeSetImpl::Raw(uint64_t Index) const {
499 for (unsigned I = 0, E = getNumAttributes(); I != E; ++I) {
500 if (getSlotIndex(I) != Index) continue;
501 const AttributeSetNode *ASN = AttrNodes[I].second;
504 for (AttributeSetNode::const_iterator II = ASN->begin(),
505 IE = ASN->end(); II != IE; ++II) {
506 Attribute Attr = *II;
508 // This cannot handle string attributes.
509 if (Attr.isStringAttribute()) continue;
511 Attribute::AttrKind Kind = Attr.getKindAsEnum();
513 if (Kind == Attribute::Alignment)
514 Mask |= (Log2_32(ASN->getAlignment()) + 1) << 16;
515 else if (Kind == Attribute::StackAlignment)
516 Mask |= (Log2_32(ASN->getStackAlignment()) + 1) << 26;
518 Mask |= AttributeImpl::getAttrMask(Kind);
527 //===----------------------------------------------------------------------===//
528 // AttributeSet Construction and Mutation Methods
529 //===----------------------------------------------------------------------===//
532 AttributeSet::getImpl(LLVMContext &C,
533 ArrayRef<std::pair<unsigned, AttributeSetNode*> > Attrs) {
534 LLVMContextImpl *pImpl = C.pImpl;
536 AttributeSetImpl::Profile(ID, Attrs);
539 AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
541 // If we didn't find any existing attributes of the same shape then
542 // create a new one and insert it.
544 PA = new AttributeSetImpl(C, Attrs);
545 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
548 // Return the AttributesList that we found or created.
549 return AttributeSet(PA);
552 AttributeSet AttributeSet::get(LLVMContext &C,
553 ArrayRef<std::pair<unsigned, Attribute> > Attrs){
554 // If there are no attributes then return a null AttributesList pointer.
556 return AttributeSet();
559 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
560 assert((!i || Attrs[i-1].first <= Attrs[i].first) &&
561 "Misordered Attributes list!");
562 assert(!Attrs[i].second.hasAttribute(Attribute::None) &&
563 "Pointless attribute!");
567 // Create a vector if (unsigned, AttributeSetNode*) pairs from the attributes
569 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrPairVec;
570 for (ArrayRef<std::pair<unsigned, Attribute> >::iterator I = Attrs.begin(),
571 E = Attrs.end(); I != E; ) {
572 unsigned Index = I->first;
573 SmallVector<Attribute, 4> AttrVec;
574 while (I != E && I->first == Index) {
575 AttrVec.push_back(I->second);
579 AttrPairVec.push_back(std::make_pair(Index,
580 AttributeSetNode::get(C, AttrVec)));
583 return getImpl(C, AttrPairVec);
586 AttributeSet AttributeSet::get(LLVMContext &C,
587 ArrayRef<std::pair<unsigned,
588 AttributeSetNode*> > Attrs) {
589 // If there are no attributes then return a null AttributesList pointer.
591 return AttributeSet();
593 return getImpl(C, Attrs);
596 AttributeSet AttributeSet::get(LLVMContext &C, unsigned Idx, AttrBuilder &B) {
597 if (!B.hasAttributes())
598 return AttributeSet();
600 // Add target-independent attributes.
601 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
602 for (Attribute::AttrKind Kind = Attribute::None;
603 Kind != Attribute::EndAttrKinds; Kind = Attribute::AttrKind(Kind + 1)) {
604 if (!B.contains(Kind))
607 if (Kind == Attribute::Alignment)
608 Attrs.push_back(std::make_pair(Idx, Attribute::
609 getWithAlignment(C, B.getAlignment())));
610 else if (Kind == Attribute::StackAlignment)
611 Attrs.push_back(std::make_pair(Idx, Attribute::
612 getWithStackAlignment(C, B.getStackAlignment())));
614 Attrs.push_back(std::make_pair(Idx, Attribute::get(C, Kind)));
617 // Add target-dependent (string) attributes.
618 for (AttrBuilder::td_iterator I = B.td_begin(), E = B.td_end();
620 Attrs.push_back(std::make_pair(Idx, Attribute::get(C, I->first,I->second)));
622 return get(C, Attrs);
625 AttributeSet AttributeSet::get(LLVMContext &C, unsigned Idx,
626 ArrayRef<Attribute::AttrKind> Kind) {
627 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
628 for (ArrayRef<Attribute::AttrKind>::iterator I = Kind.begin(),
629 E = Kind.end(); I != E; ++I)
630 Attrs.push_back(std::make_pair(Idx, Attribute::get(C, *I)));
631 return get(C, Attrs);
634 AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<AttributeSet> Attrs) {
635 if (Attrs.empty()) return AttributeSet();
637 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrNodeVec;
638 for (unsigned I = 0, E = Attrs.size(); I != E; ++I) {
639 AttributeSet AS = Attrs[I];
640 if (!AS.pImpl) continue;
641 AttrNodeVec.append(AS.pImpl->AttrNodes.begin(), AS.pImpl->AttrNodes.end());
644 return getImpl(C, AttrNodeVec);
647 AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Idx,
648 Attribute::AttrKind Attr) const {
649 return addAttributes(C, Idx, AttributeSet::get(C, Idx, Attr));
652 AttributeSet AttributeSet::addAttributes(LLVMContext &C, unsigned Idx,
653 AttributeSet Attrs) const {
654 if (!pImpl) return Attrs;
655 if (!Attrs.pImpl) return *this;
658 // FIXME it is not obvious how this should work for alignment. For now, say
659 // we can't change a known alignment.
660 unsigned OldAlign = getParamAlignment(Idx);
661 unsigned NewAlign = Attrs.getParamAlignment(Idx);
662 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
663 "Attempt to change alignment!");
666 // Add the attribute slots before the one we're trying to add.
667 SmallVector<AttributeSet, 4> AttrSet;
668 uint64_t NumAttrs = pImpl->getNumAttributes();
670 uint64_t LastIndex = 0;
671 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
672 if (getSlotIndex(I) >= Idx) {
673 if (getSlotIndex(I) == Idx) AS = getSlotAttributes(LastIndex++);
677 AttrSet.push_back(getSlotAttributes(I));
680 // Now add the attribute into the correct slot. There may already be an
681 // AttributeSet there.
682 AttrBuilder B(AS, Idx);
684 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
685 if (Attrs.getSlotIndex(I) == Idx) {
686 for (AttributeSetImpl::const_iterator II = Attrs.pImpl->begin(I),
687 IE = Attrs.pImpl->end(I); II != IE; ++II)
692 AttrSet.push_back(AttributeSet::get(C, Idx, B));
694 // Add the remaining attribute slots.
695 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
696 AttrSet.push_back(getSlotAttributes(I));
698 return get(C, AttrSet);
701 AttributeSet AttributeSet::removeAttribute(LLVMContext &C, unsigned Idx,
702 Attribute::AttrKind Attr) const {
703 return removeAttributes(C, Idx, AttributeSet::get(C, Idx, Attr));
706 AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Idx,
707 AttributeSet Attrs) const {
708 if (!pImpl) return AttributeSet();
709 if (!Attrs.pImpl) return *this;
712 // FIXME it is not obvious how this should work for alignment.
713 // For now, say we can't pass in alignment, which no current use does.
714 assert(!Attrs.hasAttribute(Idx, Attribute::Alignment) &&
715 "Attempt to change alignment!");
718 // Add the attribute slots before the one we're trying to add.
719 SmallVector<AttributeSet, 4> AttrSet;
720 uint64_t NumAttrs = pImpl->getNumAttributes();
722 uint64_t LastIndex = 0;
723 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
724 if (getSlotIndex(I) >= Idx) {
725 if (getSlotIndex(I) == Idx) AS = getSlotAttributes(LastIndex++);
729 AttrSet.push_back(getSlotAttributes(I));
732 // Now remove the attribute from the correct slot. There may already be an
733 // AttributeSet there.
734 AttrBuilder B(AS, Idx);
736 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
737 if (Attrs.getSlotIndex(I) == Idx) {
738 B.removeAttributes(Attrs.pImpl->getSlotAttributes(I), Idx);
742 AttrSet.push_back(AttributeSet::get(C, Idx, B));
744 // Add the remaining attribute slots.
745 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
746 AttrSet.push_back(getSlotAttributes(I));
748 return get(C, AttrSet);
751 //===----------------------------------------------------------------------===//
752 // AttributeSet Accessor Methods
753 //===----------------------------------------------------------------------===//
755 LLVMContext &AttributeSet::getContext() const {
756 return pImpl->getContext();
759 AttributeSet AttributeSet::getParamAttributes(unsigned Idx) const {
760 return pImpl && hasAttributes(Idx) ?
761 AttributeSet::get(pImpl->getContext(),
762 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
763 std::make_pair(Idx, getAttributes(Idx)))) :
767 AttributeSet AttributeSet::getRetAttributes() const {
768 return pImpl && hasAttributes(ReturnIndex) ?
769 AttributeSet::get(pImpl->getContext(),
770 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
771 std::make_pair(ReturnIndex,
772 getAttributes(ReturnIndex)))) :
776 AttributeSet AttributeSet::getFnAttributes() const {
777 return pImpl && hasAttributes(FunctionIndex) ?
778 AttributeSet::get(pImpl->getContext(),
779 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
780 std::make_pair(FunctionIndex,
781 getAttributes(FunctionIndex)))) :
785 bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{
786 AttributeSetNode *ASN = getAttributes(Index);
787 return ASN ? ASN->hasAttribute(Kind) : false;
790 bool AttributeSet::hasAttribute(unsigned Index, StringRef Kind) const {
791 AttributeSetNode *ASN = getAttributes(Index);
792 return ASN ? ASN->hasAttribute(Kind) : false;
795 bool AttributeSet::hasAttributes(unsigned Index) const {
796 AttributeSetNode *ASN = getAttributes(Index);
797 return ASN ? ASN->hasAttributes() : false;
800 /// \brief Return true if the specified attribute is set for at least one
801 /// parameter or for the return value.
802 bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr) const {
803 if (pImpl == 0) return false;
805 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
806 for (AttributeSetImpl::const_iterator II = pImpl->begin(I),
807 IE = pImpl->end(I); II != IE; ++II)
808 if (II->hasAttribute(Attr))
814 Attribute AttributeSet::getAttribute(unsigned Index,
815 Attribute::AttrKind Kind) const {
816 AttributeSetNode *ASN = getAttributes(Index);
817 return ASN ? ASN->getAttribute(Kind) : Attribute();
820 Attribute AttributeSet::getAttribute(unsigned Index,
821 StringRef Kind) const {
822 AttributeSetNode *ASN = getAttributes(Index);
823 return ASN ? ASN->getAttribute(Kind) : Attribute();
826 unsigned AttributeSet::getParamAlignment(unsigned Index) const {
827 AttributeSetNode *ASN = getAttributes(Index);
828 return ASN ? ASN->getAlignment() : 0;
831 unsigned AttributeSet::getStackAlignment(unsigned Index) const {
832 AttributeSetNode *ASN = getAttributes(Index);
833 return ASN ? ASN->getStackAlignment() : 0;
836 std::string AttributeSet::getAsString(unsigned Index,
837 bool InAttrGrp) const {
838 AttributeSetNode *ASN = getAttributes(Index);
839 return ASN ? ASN->getAsString(InAttrGrp) : std::string("");
842 /// \brief The attributes for the specified index are returned.
843 AttributeSetNode *AttributeSet::getAttributes(unsigned Idx) const {
844 if (!pImpl) return 0;
846 // Loop through to find the attribute node we want.
847 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
848 if (pImpl->getSlotIndex(I) == Idx)
849 return pImpl->getSlotNode(I);
854 AttributeSet::iterator AttributeSet::begin(unsigned Idx) const {
856 return ArrayRef<Attribute>().begin();
857 return pImpl->begin(Idx);
860 AttributeSet::iterator AttributeSet::end(unsigned Idx) const {
862 return ArrayRef<Attribute>().end();
863 return pImpl->end(Idx);
866 //===----------------------------------------------------------------------===//
867 // AttributeSet Introspection Methods
868 //===----------------------------------------------------------------------===//
870 /// \brief Return the number of slots used in this attribute list. This is the
871 /// number of arguments that have an attribute set on them (including the
872 /// function itself).
873 unsigned AttributeSet::getNumSlots() const {
874 return pImpl ? pImpl->getNumAttributes() : 0;
877 uint64_t AttributeSet::getSlotIndex(unsigned Slot) const {
878 assert(pImpl && Slot < pImpl->getNumAttributes() &&
879 "Slot # out of range!");
880 return pImpl->getSlotIndex(Slot);
883 AttributeSet AttributeSet::getSlotAttributes(unsigned Slot) const {
884 assert(pImpl && Slot < pImpl->getNumAttributes() &&
885 "Slot # out of range!");
886 return pImpl->getSlotAttributes(Slot);
889 uint64_t AttributeSet::Raw(unsigned Index) const {
890 // FIXME: Remove this.
891 return pImpl ? pImpl->Raw(Index) : 0;
894 void AttributeSet::dump() const {
897 for (unsigned i = 0, e = getNumSlots(); i < e; ++i) {
898 uint64_t Index = getSlotIndex(i);
904 dbgs() << " => " << getAsString(Index) << " }\n";
910 //===----------------------------------------------------------------------===//
911 // AttrBuilder Method Implementations
912 //===----------------------------------------------------------------------===//
914 AttrBuilder::AttrBuilder(AttributeSet AS, unsigned Idx)
915 : Attrs(0), Alignment(0), StackAlignment(0) {
916 AttributeSetImpl *pImpl = AS.pImpl;
919 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I) {
920 if (pImpl->getSlotIndex(I) != Idx) continue;
922 for (AttributeSetImpl::const_iterator II = pImpl->begin(I),
923 IE = pImpl->end(I); II != IE; ++II)
930 void AttrBuilder::clear() {
932 Alignment = StackAlignment = 0;
935 AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
936 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
937 assert(Val != Attribute::Alignment && Val != Attribute::StackAlignment &&
938 "Adding alignment attribute without adding alignment value!");
943 AttrBuilder &AttrBuilder::addAttribute(Attribute Attr) {
944 if (Attr.isStringAttribute()) {
945 addAttribute(Attr.getKindAsString(), Attr.getValueAsString());
949 Attribute::AttrKind Kind = Attr.getKindAsEnum();
952 if (Kind == Attribute::Alignment)
953 Alignment = Attr.getAlignment();
954 else if (Kind == Attribute::StackAlignment)
955 StackAlignment = Attr.getStackAlignment();
959 AttrBuilder &AttrBuilder::addAttribute(StringRef A, StringRef V) {
960 TargetDepAttrs[A] = V;
964 AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
965 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
968 if (Val == Attribute::Alignment)
970 else if (Val == Attribute::StackAlignment)
976 AttrBuilder &AttrBuilder::removeAttributes(AttributeSet A, uint64_t Index) {
978 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
979 if (A.getSlotIndex(I) == Index) {
984 assert(Idx != ~0U && "Couldn't find index in AttributeSet!");
986 for (AttributeSet::iterator I = A.begin(Idx), E = A.end(Idx); I != E; ++I) {
988 if (Attr.isEnumAttribute() || Attr.isAlignAttribute()) {
989 Attribute::AttrKind Kind = I->getKindAsEnum();
992 if (Kind == Attribute::Alignment)
994 else if (Kind == Attribute::StackAlignment)
997 assert(Attr.isStringAttribute() && "Invalid attribute type!");
998 std::map<std::string, std::string>::iterator
999 Iter = TargetDepAttrs.find(Attr.getKindAsString());
1000 if (Iter != TargetDepAttrs.end())
1001 TargetDepAttrs.erase(Iter);
1008 AttrBuilder &AttrBuilder::removeAttribute(StringRef A) {
1009 std::map<std::string, std::string>::iterator I = TargetDepAttrs.find(A);
1010 if (I != TargetDepAttrs.end())
1011 TargetDepAttrs.erase(I);
1015 AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
1016 if (Align == 0) return *this;
1018 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1019 assert(Align <= 0x40000000 && "Alignment too large.");
1021 Attrs[Attribute::Alignment] = true;
1026 AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
1027 // Default alignment, allow the target to define how to align it.
1028 if (Align == 0) return *this;
1030 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1031 assert(Align <= 0x100 && "Alignment too large.");
1033 Attrs[Attribute::StackAlignment] = true;
1034 StackAlignment = Align;
1038 AttrBuilder &AttrBuilder::merge(const AttrBuilder &B) {
1039 // FIXME: What if both have alignments, but they don't match?!
1041 Alignment = B.Alignment;
1043 if (!StackAlignment)
1044 StackAlignment = B.StackAlignment;
1048 for (td_const_iterator I = B.TargetDepAttrs.begin(),
1049 E = B.TargetDepAttrs.end(); I != E; ++I)
1050 TargetDepAttrs[I->first] = I->second;
1055 bool AttrBuilder::contains(StringRef A) const {
1056 return TargetDepAttrs.find(A) != TargetDepAttrs.end();
1059 bool AttrBuilder::hasAttributes() const {
1060 return !Attrs.none() || !TargetDepAttrs.empty();
1063 bool AttrBuilder::hasAttributes(AttributeSet A, uint64_t Index) const {
1065 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
1066 if (A.getSlotIndex(I) == Index) {
1071 assert(Idx != ~0U && "Couldn't find the index!");
1073 for (AttributeSet::iterator I = A.begin(Idx), E = A.end(Idx);
1075 Attribute Attr = *I;
1076 if (Attr.isEnumAttribute() || Attr.isAlignAttribute()) {
1077 if (Attrs[I->getKindAsEnum()])
1080 assert(Attr.isStringAttribute() && "Invalid attribute kind!");
1081 return TargetDepAttrs.find(Attr.getKindAsString())!=TargetDepAttrs.end();
1088 bool AttrBuilder::hasAlignmentAttr() const {
1089 return Alignment != 0;
1092 bool AttrBuilder::operator==(const AttrBuilder &B) {
1093 if (Attrs != B.Attrs)
1096 for (td_const_iterator I = TargetDepAttrs.begin(),
1097 E = TargetDepAttrs.end(); I != E; ++I)
1098 if (B.TargetDepAttrs.find(I->first) == B.TargetDepAttrs.end())
1101 return Alignment == B.Alignment && StackAlignment == B.StackAlignment;
1104 void AttrBuilder::removeFunctionOnlyAttrs() {
1105 removeAttribute(Attribute::NoReturn)
1106 .removeAttribute(Attribute::NoUnwind)
1107 .removeAttribute(Attribute::ReadNone)
1108 .removeAttribute(Attribute::ReadOnly)
1109 .removeAttribute(Attribute::NoInline)
1110 .removeAttribute(Attribute::AlwaysInline)
1111 .removeAttribute(Attribute::OptimizeForSize)
1112 .removeAttribute(Attribute::StackProtect)
1113 .removeAttribute(Attribute::StackProtectReq)
1114 .removeAttribute(Attribute::StackProtectStrong)
1115 .removeAttribute(Attribute::NoRedZone)
1116 .removeAttribute(Attribute::NoImplicitFloat)
1117 .removeAttribute(Attribute::Naked)
1118 .removeAttribute(Attribute::InlineHint)
1119 .removeAttribute(Attribute::StackAlignment)
1120 .removeAttribute(Attribute::UWTable)
1121 .removeAttribute(Attribute::NonLazyBind)
1122 .removeAttribute(Attribute::ReturnsTwice)
1123 .removeAttribute(Attribute::AddressSafety)
1124 .removeAttribute(Attribute::ThreadSafety)
1125 .removeAttribute(Attribute::UninitializedChecks)
1126 .removeAttribute(Attribute::MinSize)
1127 .removeAttribute(Attribute::NoDuplicate)
1128 .removeAttribute(Attribute::NoBuiltin);
1131 AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) {
1132 // FIXME: Remove this in 4.0.
1133 if (!Val) return *this;
1135 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
1136 I = Attribute::AttrKind(I + 1)) {
1137 if (uint64_t A = (Val & AttributeImpl::getAttrMask(I))) {
1140 if (I == Attribute::Alignment)
1141 Alignment = 1ULL << ((A >> 16) - 1);
1142 else if (I == Attribute::StackAlignment)
1143 StackAlignment = 1ULL << ((A >> 26)-1);
1150 //===----------------------------------------------------------------------===//
1151 // AttributeFuncs Function Defintions
1152 //===----------------------------------------------------------------------===//
1154 /// \brief Which attributes cannot be applied to a type.
1155 AttributeSet AttributeFuncs::typeIncompatible(Type *Ty, uint64_t Index) {
1156 AttrBuilder Incompatible;
1158 if (!Ty->isIntegerTy())
1159 // Attribute that only apply to integers.
1160 Incompatible.addAttribute(Attribute::SExt)
1161 .addAttribute(Attribute::ZExt);
1163 if (!Ty->isPointerTy())
1164 // Attribute that only apply to pointers.
1165 Incompatible.addAttribute(Attribute::ByVal)
1166 .addAttribute(Attribute::Nest)
1167 .addAttribute(Attribute::NoAlias)
1168 .addAttribute(Attribute::NoCapture)
1169 .addAttribute(Attribute::StructRet);
1171 return AttributeSet::get(Ty->getContext(), Index, Incompatible);