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.isEnumAttribute()) return getKindAsEnum() < AI.getKindAsEnum();
337 if (AI.isAlignAttribute()) return true;
338 if (AI.isStringAttribute()) return true;
341 if (isAlignAttribute()) {
342 if (AI.isEnumAttribute()) return false;
343 if (AI.isAlignAttribute()) return getValueAsInt() < AI.getValueAsInt();
344 if (AI.isStringAttribute()) return true;
347 if (AI.isEnumAttribute()) return false;
348 if (AI.isAlignAttribute()) return false;
349 if (getKindAsString() == AI.getKindAsString())
350 return getValueAsString() < AI.getValueAsString();
351 return getKindAsString() < AI.getKindAsString();
354 uint64_t AttributeImpl::getAttrMask(Attribute::AttrKind Val) {
355 // FIXME: Remove this.
357 case Attribute::EndAttrKinds:
358 llvm_unreachable("Synthetic enumerators which should never get here");
360 case Attribute::None: return 0;
361 case Attribute::ZExt: return 1 << 0;
362 case Attribute::SExt: return 1 << 1;
363 case Attribute::NoReturn: return 1 << 2;
364 case Attribute::InReg: return 1 << 3;
365 case Attribute::StructRet: return 1 << 4;
366 case Attribute::NoUnwind: return 1 << 5;
367 case Attribute::NoAlias: return 1 << 6;
368 case Attribute::ByVal: return 1 << 7;
369 case Attribute::Nest: return 1 << 8;
370 case Attribute::ReadNone: return 1 << 9;
371 case Attribute::ReadOnly: return 1 << 10;
372 case Attribute::NoInline: return 1 << 11;
373 case Attribute::AlwaysInline: return 1 << 12;
374 case Attribute::OptimizeForSize: return 1 << 13;
375 case Attribute::StackProtect: return 1 << 14;
376 case Attribute::StackProtectReq: return 1 << 15;
377 case Attribute::Alignment: return 31 << 16;
378 case Attribute::NoCapture: return 1 << 21;
379 case Attribute::NoRedZone: return 1 << 22;
380 case Attribute::NoImplicitFloat: return 1 << 23;
381 case Attribute::Naked: return 1 << 24;
382 case Attribute::InlineHint: return 1 << 25;
383 case Attribute::StackAlignment: return 7 << 26;
384 case Attribute::ReturnsTwice: return 1 << 29;
385 case Attribute::UWTable: return 1 << 30;
386 case Attribute::NonLazyBind: return 1U << 31;
387 case Attribute::AddressSafety: return 1ULL << 32;
388 case Attribute::MinSize: return 1ULL << 33;
389 case Attribute::NoDuplicate: return 1ULL << 34;
390 case Attribute::StackProtectStrong: return 1ULL << 35;
391 case Attribute::ThreadSafety: return 1ULL << 36;
392 case Attribute::UninitializedChecks: return 1ULL << 37;
394 llvm_unreachable("Unsupported attribute type");
397 //===----------------------------------------------------------------------===//
398 // AttributeSetNode Definition
399 //===----------------------------------------------------------------------===//
401 AttributeSetNode *AttributeSetNode::get(LLVMContext &C,
402 ArrayRef<Attribute> Attrs) {
406 // Otherwise, build a key to look up the existing attributes.
407 LLVMContextImpl *pImpl = C.pImpl;
410 SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end());
411 array_pod_sort(SortedAttrs.begin(), SortedAttrs.end());
413 for (SmallVectorImpl<Attribute>::iterator I = SortedAttrs.begin(),
414 E = SortedAttrs.end(); I != E; ++I)
418 AttributeSetNode *PA =
419 pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint);
421 // If we didn't find any existing attributes of the same shape then create a
422 // new one and insert it.
424 PA = new AttributeSetNode(SortedAttrs);
425 pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint);
428 // Return the AttributesListNode that we found or created.
432 bool AttributeSetNode::hasAttribute(Attribute::AttrKind Kind) const {
433 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
434 E = AttrList.end(); I != E; ++I)
435 if (I->hasAttribute(Kind))
440 bool AttributeSetNode::hasAttribute(StringRef Kind) const {
441 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
442 E = AttrList.end(); I != E; ++I)
443 if (I->hasAttribute(Kind))
448 Attribute AttributeSetNode::getAttribute(Attribute::AttrKind Kind) const {
449 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
450 E = AttrList.end(); I != E; ++I)
451 if (I->hasAttribute(Kind))
456 Attribute AttributeSetNode::getAttribute(StringRef Kind) const {
457 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
458 E = AttrList.end(); I != E; ++I)
459 if (I->hasAttribute(Kind))
464 unsigned AttributeSetNode::getAlignment() const {
465 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
466 E = AttrList.end(); I != E; ++I)
467 if (I->hasAttribute(Attribute::Alignment))
468 return I->getAlignment();
472 unsigned AttributeSetNode::getStackAlignment() const {
473 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
474 E = AttrList.end(); I != E; ++I)
475 if (I->hasAttribute(Attribute::StackAlignment))
476 return I->getStackAlignment();
480 std::string AttributeSetNode::getAsString(bool InAttrGrp) const {
481 std::string Str = "";
482 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
483 E = AttrList.end(); I != E; ) {
484 Str += I->getAsString(InAttrGrp);
485 if (++I != E) Str += " ";
490 //===----------------------------------------------------------------------===//
491 // AttributeSetImpl Definition
492 //===----------------------------------------------------------------------===//
494 uint64_t AttributeSetImpl::Raw(uint64_t Index) const {
495 for (unsigned I = 0, E = getNumAttributes(); I != E; ++I) {
496 if (getSlotIndex(I) != Index) continue;
497 const AttributeSetNode *ASN = AttrNodes[I].second;
500 for (AttributeSetNode::const_iterator II = ASN->begin(),
501 IE = ASN->end(); II != IE; ++II) {
502 Attribute Attr = *II;
504 // This cannot handle string attributes.
505 if (Attr.isStringAttribute()) continue;
507 Attribute::AttrKind Kind = Attr.getKindAsEnum();
509 if (Kind == Attribute::Alignment)
510 Mask |= (Log2_32(ASN->getAlignment()) + 1) << 16;
511 else if (Kind == Attribute::StackAlignment)
512 Mask |= (Log2_32(ASN->getStackAlignment()) + 1) << 26;
514 Mask |= AttributeImpl::getAttrMask(Kind);
523 //===----------------------------------------------------------------------===//
524 // AttributeSet Construction and Mutation Methods
525 //===----------------------------------------------------------------------===//
528 AttributeSet::getImpl(LLVMContext &C,
529 ArrayRef<std::pair<unsigned, AttributeSetNode*> > Attrs) {
530 LLVMContextImpl *pImpl = C.pImpl;
532 AttributeSetImpl::Profile(ID, Attrs);
535 AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
537 // If we didn't find any existing attributes of the same shape then
538 // create a new one and insert it.
540 PA = new AttributeSetImpl(C, Attrs);
541 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
544 // Return the AttributesList that we found or created.
545 return AttributeSet(PA);
548 AttributeSet AttributeSet::get(LLVMContext &C,
549 ArrayRef<std::pair<unsigned, Attribute> > Attrs){
550 // If there are no attributes then return a null AttributesList pointer.
552 return AttributeSet();
555 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
556 assert((!i || Attrs[i-1].first <= Attrs[i].first) &&
557 "Misordered Attributes list!");
558 assert(!Attrs[i].second.hasAttribute(Attribute::None) &&
559 "Pointless attribute!");
563 // Create a vector if (unsigned, AttributeSetNode*) pairs from the attributes
565 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrPairVec;
566 for (ArrayRef<std::pair<unsigned, Attribute> >::iterator I = Attrs.begin(),
567 E = Attrs.end(); I != E; ) {
568 unsigned Index = I->first;
569 SmallVector<Attribute, 4> AttrVec;
570 while (I != E && I->first == Index) {
571 AttrVec.push_back(I->second);
575 AttrPairVec.push_back(std::make_pair(Index,
576 AttributeSetNode::get(C, AttrVec)));
579 return getImpl(C, AttrPairVec);
582 AttributeSet AttributeSet::get(LLVMContext &C,
583 ArrayRef<std::pair<unsigned,
584 AttributeSetNode*> > Attrs) {
585 // If there are no attributes then return a null AttributesList pointer.
587 return AttributeSet();
589 return getImpl(C, Attrs);
592 AttributeSet AttributeSet::get(LLVMContext &C, unsigned Idx, AttrBuilder &B) {
593 if (!B.hasAttributes())
594 return AttributeSet();
596 // Add target-independent attributes.
597 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
598 for (Attribute::AttrKind Kind = Attribute::None;
599 Kind != Attribute::EndAttrKinds; Kind = Attribute::AttrKind(Kind + 1)) {
600 if (!B.contains(Kind))
603 if (Kind == Attribute::Alignment)
604 Attrs.push_back(std::make_pair(Idx, Attribute::
605 getWithAlignment(C, B.getAlignment())));
606 else if (Kind == Attribute::StackAlignment)
607 Attrs.push_back(std::make_pair(Idx, Attribute::
608 getWithStackAlignment(C, B.getStackAlignment())));
610 Attrs.push_back(std::make_pair(Idx, Attribute::get(C, Kind)));
613 // Add target-dependent (string) attributes.
614 for (AttrBuilder::td_iterator I = B.td_begin(), E = B.td_end();
616 Attrs.push_back(std::make_pair(Idx, Attribute::get(C, I->first,I->second)));
618 return get(C, Attrs);
621 AttributeSet AttributeSet::get(LLVMContext &C, unsigned Idx,
622 ArrayRef<Attribute::AttrKind> Kind) {
623 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
624 for (ArrayRef<Attribute::AttrKind>::iterator I = Kind.begin(),
625 E = Kind.end(); I != E; ++I)
626 Attrs.push_back(std::make_pair(Idx, Attribute::get(C, *I)));
627 return get(C, Attrs);
630 AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<AttributeSet> Attrs) {
631 if (Attrs.empty()) return AttributeSet();
633 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrNodeVec;
634 for (unsigned I = 0, E = Attrs.size(); I != E; ++I) {
635 AttributeSet AS = Attrs[I];
636 if (!AS.pImpl) continue;
637 AttrNodeVec.append(AS.pImpl->AttrNodes.begin(), AS.pImpl->AttrNodes.end());
640 return getImpl(C, AttrNodeVec);
643 AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Idx,
644 Attribute::AttrKind Attr) const {
645 return addAttributes(C, Idx, AttributeSet::get(C, Idx, Attr));
648 AttributeSet AttributeSet::addAttributes(LLVMContext &C, unsigned Idx,
649 AttributeSet Attrs) const {
650 if (!pImpl) return Attrs;
651 if (!Attrs.pImpl) return *this;
654 // FIXME it is not obvious how this should work for alignment. For now, say
655 // we can't change a known alignment.
656 unsigned OldAlign = getParamAlignment(Idx);
657 unsigned NewAlign = Attrs.getParamAlignment(Idx);
658 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
659 "Attempt to change alignment!");
662 // Add the attribute slots before the one we're trying to add.
663 SmallVector<AttributeSet, 4> AttrSet;
664 uint64_t NumAttrs = pImpl->getNumAttributes();
666 uint64_t LastIndex = 0;
667 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
668 if (getSlotIndex(I) >= Idx) {
669 if (getSlotIndex(I) == Idx) AS = getSlotAttributes(LastIndex++);
673 AttrSet.push_back(getSlotAttributes(I));
676 // Now add the attribute into the correct slot. There may already be an
677 // AttributeSet there.
678 AttrBuilder B(AS, Idx);
680 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
681 if (Attrs.getSlotIndex(I) == Idx) {
682 for (AttributeSetImpl::const_iterator II = Attrs.pImpl->begin(I),
683 IE = Attrs.pImpl->end(I); II != IE; ++II)
688 AttrSet.push_back(AttributeSet::get(C, Idx, B));
690 // Add the remaining attribute slots.
691 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
692 AttrSet.push_back(getSlotAttributes(I));
694 return get(C, AttrSet);
697 AttributeSet AttributeSet::removeAttribute(LLVMContext &C, unsigned Idx,
698 Attribute::AttrKind Attr) const {
699 return removeAttributes(C, Idx, AttributeSet::get(C, Idx, Attr));
702 AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Idx,
703 AttributeSet Attrs) const {
704 if (!pImpl) return AttributeSet();
705 if (!Attrs.pImpl) return *this;
708 // FIXME it is not obvious how this should work for alignment.
709 // For now, say we can't pass in alignment, which no current use does.
710 assert(!Attrs.hasAttribute(Idx, Attribute::Alignment) &&
711 "Attempt to change alignment!");
714 // Add the attribute slots before the one we're trying to add.
715 SmallVector<AttributeSet, 4> AttrSet;
716 uint64_t NumAttrs = pImpl->getNumAttributes();
718 uint64_t LastIndex = 0;
719 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
720 if (getSlotIndex(I) >= Idx) {
721 if (getSlotIndex(I) == Idx) AS = getSlotAttributes(LastIndex++);
725 AttrSet.push_back(getSlotAttributes(I));
728 // Now remove the attribute from the correct slot. There may already be an
729 // AttributeSet there.
730 AttrBuilder B(AS, Idx);
732 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
733 if (Attrs.getSlotIndex(I) == Idx) {
734 B.removeAttributes(Attrs.pImpl->getSlotAttributes(I), Idx);
738 AttrSet.push_back(AttributeSet::get(C, Idx, B));
740 // Add the remaining attribute slots.
741 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
742 AttrSet.push_back(getSlotAttributes(I));
744 return get(C, AttrSet);
747 //===----------------------------------------------------------------------===//
748 // AttributeSet Accessor Methods
749 //===----------------------------------------------------------------------===//
751 LLVMContext &AttributeSet::getContext() const {
752 return pImpl->getContext();
755 AttributeSet AttributeSet::getParamAttributes(unsigned Idx) const {
756 return pImpl && hasAttributes(Idx) ?
757 AttributeSet::get(pImpl->getContext(),
758 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
759 std::make_pair(Idx, getAttributes(Idx)))) :
763 AttributeSet AttributeSet::getRetAttributes() const {
764 return pImpl && hasAttributes(ReturnIndex) ?
765 AttributeSet::get(pImpl->getContext(),
766 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
767 std::make_pair(ReturnIndex,
768 getAttributes(ReturnIndex)))) :
772 AttributeSet AttributeSet::getFnAttributes() const {
773 return pImpl && hasAttributes(FunctionIndex) ?
774 AttributeSet::get(pImpl->getContext(),
775 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
776 std::make_pair(FunctionIndex,
777 getAttributes(FunctionIndex)))) :
781 bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{
782 AttributeSetNode *ASN = getAttributes(Index);
783 return ASN ? ASN->hasAttribute(Kind) : false;
786 bool AttributeSet::hasAttribute(unsigned Index, StringRef Kind) const {
787 AttributeSetNode *ASN = getAttributes(Index);
788 return ASN ? ASN->hasAttribute(Kind) : false;
791 bool AttributeSet::hasAttributes(unsigned Index) const {
792 AttributeSetNode *ASN = getAttributes(Index);
793 return ASN ? ASN->hasAttributes() : false;
796 /// \brief Return true if the specified attribute is set for at least one
797 /// parameter or for the return value.
798 bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr) const {
799 if (pImpl == 0) return false;
801 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
802 for (AttributeSetImpl::const_iterator II = pImpl->begin(I),
803 IE = pImpl->end(I); II != IE; ++II)
804 if (II->hasAttribute(Attr))
810 Attribute AttributeSet::getAttribute(unsigned Index,
811 Attribute::AttrKind Kind) const {
812 AttributeSetNode *ASN = getAttributes(Index);
813 return ASN ? ASN->getAttribute(Kind) : Attribute();
816 Attribute AttributeSet::getAttribute(unsigned Index,
817 StringRef Kind) const {
818 AttributeSetNode *ASN = getAttributes(Index);
819 return ASN ? ASN->getAttribute(Kind) : Attribute();
822 unsigned AttributeSet::getParamAlignment(unsigned Index) const {
823 AttributeSetNode *ASN = getAttributes(Index);
824 return ASN ? ASN->getAlignment() : 0;
827 unsigned AttributeSet::getStackAlignment(unsigned Index) const {
828 AttributeSetNode *ASN = getAttributes(Index);
829 return ASN ? ASN->getStackAlignment() : 0;
832 std::string AttributeSet::getAsString(unsigned Index,
833 bool InAttrGrp) const {
834 AttributeSetNode *ASN = getAttributes(Index);
835 return ASN ? ASN->getAsString(InAttrGrp) : std::string("");
838 /// \brief The attributes for the specified index are returned.
839 AttributeSetNode *AttributeSet::getAttributes(unsigned Idx) const {
840 if (!pImpl) return 0;
842 // Loop through to find the attribute node we want.
843 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
844 if (pImpl->getSlotIndex(I) == Idx)
845 return pImpl->getSlotNode(I);
850 AttributeSet::iterator AttributeSet::begin(unsigned Idx) const {
852 return ArrayRef<Attribute>().begin();
853 return pImpl->begin(Idx);
856 AttributeSet::iterator AttributeSet::end(unsigned Idx) const {
858 return ArrayRef<Attribute>().end();
859 return pImpl->end(Idx);
862 //===----------------------------------------------------------------------===//
863 // AttributeSet Introspection Methods
864 //===----------------------------------------------------------------------===//
866 /// \brief Return the number of slots used in this attribute list. This is the
867 /// number of arguments that have an attribute set on them (including the
868 /// function itself).
869 unsigned AttributeSet::getNumSlots() const {
870 return pImpl ? pImpl->getNumAttributes() : 0;
873 uint64_t AttributeSet::getSlotIndex(unsigned Slot) const {
874 assert(pImpl && Slot < pImpl->getNumAttributes() &&
875 "Slot # out of range!");
876 return pImpl->getSlotIndex(Slot);
879 AttributeSet AttributeSet::getSlotAttributes(unsigned Slot) const {
880 assert(pImpl && Slot < pImpl->getNumAttributes() &&
881 "Slot # out of range!");
882 return pImpl->getSlotAttributes(Slot);
885 uint64_t AttributeSet::Raw(unsigned Index) const {
886 // FIXME: Remove this.
887 return pImpl ? pImpl->Raw(Index) : 0;
890 void AttributeSet::dump() const {
893 for (unsigned i = 0, e = getNumSlots(); i < e; ++i) {
894 uint64_t Index = getSlotIndex(i);
900 dbgs() << " => " << getAsString(Index) << " }\n";
906 //===----------------------------------------------------------------------===//
907 // AttrBuilder Method Implementations
908 //===----------------------------------------------------------------------===//
910 AttrBuilder::AttrBuilder(AttributeSet AS, unsigned Idx)
911 : Attrs(0), Alignment(0), StackAlignment(0) {
912 AttributeSetImpl *pImpl = AS.pImpl;
915 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I) {
916 if (pImpl->getSlotIndex(I) != Idx) continue;
918 for (AttributeSetImpl::const_iterator II = pImpl->begin(I),
919 IE = pImpl->end(I); II != IE; ++II)
926 void AttrBuilder::clear() {
928 Alignment = StackAlignment = 0;
931 AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
932 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
933 assert(Val != Attribute::Alignment && Val != Attribute::StackAlignment &&
934 "Adding alignment attribute without adding alignment value!");
939 AttrBuilder &AttrBuilder::addAttribute(Attribute Attr) {
940 if (Attr.isStringAttribute()) {
941 addAttribute(Attr.getKindAsString(), Attr.getValueAsString());
945 Attribute::AttrKind Kind = Attr.getKindAsEnum();
948 if (Kind == Attribute::Alignment)
949 Alignment = Attr.getAlignment();
950 else if (Kind == Attribute::StackAlignment)
951 StackAlignment = Attr.getStackAlignment();
955 AttrBuilder &AttrBuilder::addAttribute(StringRef A, StringRef V) {
956 TargetDepAttrs[A] = V;
960 AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
961 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
964 if (Val == Attribute::Alignment)
966 else if (Val == Attribute::StackAlignment)
972 AttrBuilder &AttrBuilder::removeAttributes(AttributeSet A, uint64_t Index) {
974 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
975 if (A.getSlotIndex(I) == Index) {
980 assert(Idx != ~0U && "Couldn't find index in AttributeSet!");
982 for (AttributeSet::iterator I = A.begin(Idx), E = A.end(Idx); I != E; ++I) {
984 if (Attr.isEnumAttribute() || Attr.isAlignAttribute()) {
985 Attribute::AttrKind Kind = I->getKindAsEnum();
988 if (Kind == Attribute::Alignment)
990 else if (Kind == Attribute::StackAlignment)
993 assert(Attr.isStringAttribute() && "Invalid attribute type!");
994 std::map<std::string, std::string>::iterator
995 Iter = TargetDepAttrs.find(Attr.getKindAsString());
996 if (Iter != TargetDepAttrs.end())
997 TargetDepAttrs.erase(Iter);
1004 AttrBuilder &AttrBuilder::removeAttribute(StringRef A) {
1005 std::map<std::string, std::string>::iterator I = TargetDepAttrs.find(A);
1006 if (I != TargetDepAttrs.end())
1007 TargetDepAttrs.erase(I);
1011 AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
1012 if (Align == 0) return *this;
1014 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1015 assert(Align <= 0x40000000 && "Alignment too large.");
1017 Attrs[Attribute::Alignment] = true;
1022 AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
1023 // Default alignment, allow the target to define how to align it.
1024 if (Align == 0) return *this;
1026 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1027 assert(Align <= 0x100 && "Alignment too large.");
1029 Attrs[Attribute::StackAlignment] = true;
1030 StackAlignment = Align;
1034 AttrBuilder &AttrBuilder::merge(const AttrBuilder &B) {
1035 // FIXME: What if both have alignments, but they don't match?!
1037 Alignment = B.Alignment;
1039 if (!StackAlignment)
1040 StackAlignment = B.StackAlignment;
1044 for (td_const_iterator I = B.TargetDepAttrs.begin(),
1045 E = B.TargetDepAttrs.end(); I != E; ++I)
1046 TargetDepAttrs[I->first] = I->second;
1051 bool AttrBuilder::contains(StringRef A) const {
1052 return TargetDepAttrs.find(A) != TargetDepAttrs.end();
1055 bool AttrBuilder::hasAttributes() const {
1056 return !Attrs.none() || !TargetDepAttrs.empty();
1059 bool AttrBuilder::hasAttributes(AttributeSet A, uint64_t Index) const {
1061 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
1062 if (A.getSlotIndex(I) == Index) {
1067 assert(Idx != ~0U && "Couldn't find the index!");
1069 for (AttributeSet::iterator I = A.begin(Idx), E = A.end(Idx);
1071 Attribute Attr = *I;
1072 if (Attr.isEnumAttribute() || Attr.isAlignAttribute()) {
1073 if (Attrs[I->getKindAsEnum()])
1076 assert(Attr.isStringAttribute() && "Invalid attribute kind!");
1077 return TargetDepAttrs.find(Attr.getKindAsString())!=TargetDepAttrs.end();
1084 bool AttrBuilder::hasAlignmentAttr() const {
1085 return Alignment != 0;
1088 bool AttrBuilder::operator==(const AttrBuilder &B) {
1089 if (Attrs != B.Attrs)
1092 for (td_const_iterator I = TargetDepAttrs.begin(),
1093 E = TargetDepAttrs.end(); I != E; ++I)
1094 if (B.TargetDepAttrs.find(I->first) == B.TargetDepAttrs.end())
1097 return Alignment == B.Alignment && StackAlignment == B.StackAlignment;
1100 AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) {
1101 // FIXME: Remove this in 4.0.
1102 if (!Val) return *this;
1104 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
1105 I = Attribute::AttrKind(I + 1)) {
1106 if (uint64_t A = (Val & AttributeImpl::getAttrMask(I))) {
1109 if (I == Attribute::Alignment)
1110 Alignment = 1ULL << ((A >> 16) - 1);
1111 else if (I == Attribute::StackAlignment)
1112 StackAlignment = 1ULL << ((A >> 26)-1);
1119 //===----------------------------------------------------------------------===//
1120 // AttributeFuncs Function Defintions
1121 //===----------------------------------------------------------------------===//
1123 /// \brief Which attributes cannot be applied to a type.
1124 AttributeSet AttributeFuncs::typeIncompatible(Type *Ty, uint64_t Index) {
1125 AttrBuilder Incompatible;
1127 if (!Ty->isIntegerTy())
1128 // Attribute that only apply to integers.
1129 Incompatible.addAttribute(Attribute::SExt)
1130 .addAttribute(Attribute::ZExt);
1132 if (!Ty->isPointerTy())
1133 // Attribute that only apply to pointers.
1134 Incompatible.addAttribute(Attribute::ByVal)
1135 .addAttribute(Attribute::Nest)
1136 .addAttribute(Attribute::NoAlias)
1137 .addAttribute(Attribute::NoCapture)
1138 .addAttribute(Attribute::StructRet);
1140 return AttributeSet::get(Ty->getContext(), Index, Incompatible);