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() 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::UWTable))
210 if (hasAttribute(Attribute::ZExt))
213 // FIXME: These should be output like this:
218 if (hasAttribute(Attribute::Alignment)) {
221 Result += utostr(getValueAsInt());
224 if (hasAttribute(Attribute::StackAlignment)) {
226 Result += "alignstack(";
227 Result += utostr(getValueAsInt());
232 // Convert target-dependent attributes to strings of the form:
236 // "kind" = ( "value1" "value2" "value3" )
238 if (isStringAttribute()) {
240 Result += '\"' + getKindAsString().str() + '"';
242 StringRef Val = pImpl->getValueAsString();
243 if (Val.empty()) return Result;
246 Result += '\"' + Val.str() + '"';
250 llvm_unreachable("Unknown attribute");
253 bool Attribute::operator<(Attribute A) const {
254 if (!pImpl && !A.pImpl) return false;
255 if (!pImpl) return true;
256 if (!A.pImpl) return false;
257 return *pImpl < *A.pImpl;
260 //===----------------------------------------------------------------------===//
261 // AttributeImpl Definition
262 //===----------------------------------------------------------------------===//
264 AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind Kind)
265 : Context(C), Entry(new EnumAttributeEntry(Kind)) {}
267 AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind Kind,
270 assert((Kind == Attribute::Alignment || Kind == Attribute::StackAlignment) &&
271 "Wrong kind for alignment attribute!");
272 Entry = new AlignAttributeEntry(Kind, Align);
275 AttributeImpl::AttributeImpl(LLVMContext &C, StringRef Kind, StringRef Val)
276 : Context(C), Entry(new StringAttributeEntry(Kind, Val)) {}
278 AttributeImpl::~AttributeImpl() {
282 bool AttributeImpl::isEnumAttribute() const {
283 return isa<EnumAttributeEntry>(Entry);
286 bool AttributeImpl::isAlignAttribute() const {
287 return isa<AlignAttributeEntry>(Entry);
290 bool AttributeImpl::isStringAttribute() const {
291 return isa<StringAttributeEntry>(Entry);
294 bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
295 if (isStringAttribute()) return false;
296 return getKindAsEnum() == A;
299 bool AttributeImpl::hasAttribute(StringRef Kind) const {
300 if (!isStringAttribute()) return false;
301 return getKindAsString() == Kind;
304 Attribute::AttrKind AttributeImpl::getKindAsEnum() const {
305 if (EnumAttributeEntry *E = dyn_cast<EnumAttributeEntry>(Entry))
306 return E->getEnumKind();
307 return cast<AlignAttributeEntry>(Entry)->getEnumKind();
310 uint64_t AttributeImpl::getValueAsInt() const {
311 return cast<AlignAttributeEntry>(Entry)->getAlignment();
314 StringRef AttributeImpl::getKindAsString() const {
315 return cast<StringAttributeEntry>(Entry)->getStringKind();
318 StringRef AttributeImpl::getValueAsString() const {
319 return cast<StringAttributeEntry>(Entry)->getStringValue();
322 bool AttributeImpl::operator<(const AttributeImpl &AI) const {
323 // This sorts the attributes with Attribute::AttrKinds coming first (sorted
324 // relative to their enum value) and then strings.
325 if (isEnumAttribute())
326 if (AI.isAlignAttribute() || AI.isEnumAttribute())
327 return getKindAsEnum() < AI.getKindAsEnum();
329 if (isAlignAttribute()) {
330 if (!AI.isStringAttribute() && getKindAsEnum() < AI.getKindAsEnum())
332 if (AI.isAlignAttribute())
333 return getValueAsInt() < AI.getValueAsInt();
336 if (isStringAttribute()) {
337 if (!AI.isStringAttribute()) return false;
338 if (getKindAsString() < AI.getKindAsString()) return true;
339 if (getKindAsString() == AI.getKindAsString())
340 return getValueAsString() < AI.getValueAsString();
346 uint64_t AttributeImpl::getAttrMask(Attribute::AttrKind Val) {
347 // FIXME: Remove this.
349 case Attribute::EndAttrKinds:
350 case Attribute::AttrKindEmptyKey:
351 case Attribute::AttrKindTombstoneKey:
352 llvm_unreachable("Synthetic enumerators which should never get here");
354 case Attribute::None: return 0;
355 case Attribute::ZExt: return 1 << 0;
356 case Attribute::SExt: return 1 << 1;
357 case Attribute::NoReturn: return 1 << 2;
358 case Attribute::InReg: return 1 << 3;
359 case Attribute::StructRet: return 1 << 4;
360 case Attribute::NoUnwind: return 1 << 5;
361 case Attribute::NoAlias: return 1 << 6;
362 case Attribute::ByVal: return 1 << 7;
363 case Attribute::Nest: return 1 << 8;
364 case Attribute::ReadNone: return 1 << 9;
365 case Attribute::ReadOnly: return 1 << 10;
366 case Attribute::NoInline: return 1 << 11;
367 case Attribute::AlwaysInline: return 1 << 12;
368 case Attribute::OptimizeForSize: return 1 << 13;
369 case Attribute::StackProtect: return 1 << 14;
370 case Attribute::StackProtectReq: return 1 << 15;
371 case Attribute::Alignment: return 31 << 16;
372 case Attribute::NoCapture: return 1 << 21;
373 case Attribute::NoRedZone: return 1 << 22;
374 case Attribute::NoImplicitFloat: return 1 << 23;
375 case Attribute::Naked: return 1 << 24;
376 case Attribute::InlineHint: return 1 << 25;
377 case Attribute::StackAlignment: return 7 << 26;
378 case Attribute::ReturnsTwice: return 1 << 29;
379 case Attribute::UWTable: return 1 << 30;
380 case Attribute::NonLazyBind: return 1U << 31;
381 case Attribute::AddressSafety: return 1ULL << 32;
382 case Attribute::MinSize: return 1ULL << 33;
383 case Attribute::NoDuplicate: return 1ULL << 34;
384 case Attribute::StackProtectStrong: return 1ULL << 35;
386 llvm_unreachable("Unsupported attribute type");
389 //===----------------------------------------------------------------------===//
390 // AttributeSetNode Definition
391 //===----------------------------------------------------------------------===//
393 AttributeSetNode *AttributeSetNode::get(LLVMContext &C,
394 ArrayRef<Attribute> Attrs) {
398 // Otherwise, build a key to look up the existing attributes.
399 LLVMContextImpl *pImpl = C.pImpl;
402 SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end());
403 std::sort(SortedAttrs.begin(), SortedAttrs.end());
405 for (SmallVectorImpl<Attribute>::iterator I = SortedAttrs.begin(),
406 E = SortedAttrs.end(); I != E; ++I)
410 AttributeSetNode *PA =
411 pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint);
413 // If we didn't find any existing attributes of the same shape then create a
414 // new one and insert it.
416 PA = new AttributeSetNode(SortedAttrs);
417 pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint);
420 // Return the AttributesListNode that we found or created.
424 bool AttributeSetNode::hasAttribute(Attribute::AttrKind Kind) const {
425 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
426 E = AttrList.end(); I != E; ++I)
427 if (I->hasAttribute(Kind))
432 unsigned AttributeSetNode::getAlignment() const {
433 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
434 E = AttrList.end(); I != E; ++I)
435 if (I->hasAttribute(Attribute::Alignment))
436 return I->getAlignment();
440 unsigned AttributeSetNode::getStackAlignment() const {
441 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
442 E = AttrList.end(); I != E; ++I)
443 if (I->hasAttribute(Attribute::StackAlignment))
444 return I->getStackAlignment();
448 std::string AttributeSetNode::getAsString() const {
449 std::string Str = "";
450 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
451 E = AttrList.end(); I != E; ) {
452 Str += I->getAsString();
453 if (++I != E) Str += " ";
458 //===----------------------------------------------------------------------===//
459 // AttributeSetImpl Definition
460 //===----------------------------------------------------------------------===//
462 uint64_t AttributeSetImpl::Raw(uint64_t Index) const {
463 for (unsigned I = 0, E = getNumAttributes(); I != E; ++I) {
464 if (getSlotIndex(I) != Index) continue;
465 const AttributeSetNode *ASN = AttrNodes[I].second;
468 for (AttributeSetNode::const_iterator II = ASN->begin(),
469 IE = ASN->end(); II != IE; ++II) {
470 Attribute Attr = *II;
471 Attribute::AttrKind Kind = Attr.getKindAsEnum();
473 if (Kind == Attribute::Alignment)
474 Mask |= (Log2_32(ASN->getAlignment()) + 1) << 16;
475 else if (Kind == Attribute::StackAlignment)
476 Mask |= (Log2_32(ASN->getStackAlignment()) + 1) << 26;
478 Mask |= AttributeImpl::getAttrMask(Kind);
487 //===----------------------------------------------------------------------===//
488 // AttributeSet Construction and Mutation Methods
489 //===----------------------------------------------------------------------===//
492 AttributeSet::getImpl(LLVMContext &C,
493 ArrayRef<std::pair<unsigned, AttributeSetNode*> > Attrs) {
494 LLVMContextImpl *pImpl = C.pImpl;
496 AttributeSetImpl::Profile(ID, Attrs);
499 AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
501 // If we didn't find any existing attributes of the same shape then
502 // create a new one and insert it.
504 PA = new AttributeSetImpl(C, Attrs);
505 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
508 // Return the AttributesList that we found or created.
509 return AttributeSet(PA);
512 AttributeSet AttributeSet::get(LLVMContext &C,
513 ArrayRef<std::pair<unsigned, Attribute> > Attrs){
514 // If there are no attributes then return a null AttributesList pointer.
516 return AttributeSet();
519 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
520 assert((!i || Attrs[i-1].first <= Attrs[i].first) &&
521 "Misordered Attributes list!");
522 assert(!Attrs[i].second.hasAttribute(Attribute::None) &&
523 "Pointless attribute!");
527 // Create a vector if (unsigned, AttributeSetNode*) pairs from the attributes
529 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrPairVec;
530 for (ArrayRef<std::pair<unsigned, Attribute> >::iterator I = Attrs.begin(),
531 E = Attrs.end(); I != E; ) {
532 unsigned Index = I->first;
533 SmallVector<Attribute, 4> AttrVec;
534 while (I != E && I->first == Index) {
535 AttrVec.push_back(I->second);
539 AttrPairVec.push_back(std::make_pair(Index,
540 AttributeSetNode::get(C, AttrVec)));
543 return getImpl(C, AttrPairVec);
546 AttributeSet AttributeSet::get(LLVMContext &C,
547 ArrayRef<std::pair<unsigned,
548 AttributeSetNode*> > Attrs) {
549 // If there are no attributes then return a null AttributesList pointer.
551 return AttributeSet();
553 return getImpl(C, Attrs);
556 AttributeSet AttributeSet::get(LLVMContext &C, unsigned Idx, AttrBuilder &B) {
557 if (!B.hasAttributes())
558 return AttributeSet();
560 // Add target-independent attributes.
561 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
562 for (AttrBuilder::iterator I = B.begin(), E = B.end(); I != E; ++I) {
563 Attribute::AttrKind Kind = *I;
564 if (Kind == Attribute::Alignment)
565 Attrs.push_back(std::make_pair(Idx, Attribute::
566 getWithAlignment(C, B.getAlignment())));
567 else if (Kind == Attribute::StackAlignment)
568 Attrs.push_back(std::make_pair(Idx, Attribute::
569 getWithStackAlignment(C, B.getStackAlignment())));
571 Attrs.push_back(std::make_pair(Idx, Attribute::get(C, Kind)));
574 // Add target-dependent (string) attributes.
575 for (AttrBuilder::td_iterator I = B.td_begin(), E = B.td_end();
577 Attrs.push_back(std::make_pair(Idx, Attribute::get(C, I->first,I->second)));
579 return get(C, Attrs);
582 AttributeSet AttributeSet::get(LLVMContext &C, unsigned Idx,
583 ArrayRef<Attribute::AttrKind> Kind) {
584 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
585 for (ArrayRef<Attribute::AttrKind>::iterator I = Kind.begin(),
586 E = Kind.end(); I != E; ++I)
587 Attrs.push_back(std::make_pair(Idx, Attribute::get(C, *I)));
588 return get(C, Attrs);
591 AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<AttributeSet> Attrs) {
592 if (Attrs.empty()) return AttributeSet();
594 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrNodeVec;
595 for (unsigned I = 0, E = Attrs.size(); I != E; ++I) {
596 AttributeSet AS = Attrs[I];
597 if (!AS.pImpl) continue;
598 AttrNodeVec.append(AS.pImpl->AttrNodes.begin(), AS.pImpl->AttrNodes.end());
601 return getImpl(C, AttrNodeVec);
604 AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Idx,
605 Attribute::AttrKind Attr) const {
606 return addAttributes(C, Idx, AttributeSet::get(C, Idx, Attr));
609 AttributeSet AttributeSet::addAttributes(LLVMContext &C, unsigned Idx,
610 AttributeSet Attrs) const {
611 if (!pImpl) return Attrs;
612 if (!Attrs.pImpl) return *this;
615 // FIXME it is not obvious how this should work for alignment. For now, say
616 // we can't change a known alignment.
617 unsigned OldAlign = getParamAlignment(Idx);
618 unsigned NewAlign = Attrs.getParamAlignment(Idx);
619 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
620 "Attempt to change alignment!");
623 // Add the attribute slots before the one we're trying to add.
624 SmallVector<AttributeSet, 4> AttrSet;
625 uint64_t NumAttrs = pImpl->getNumAttributes();
627 uint64_t LastIndex = 0;
628 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
629 if (getSlotIndex(I) >= Idx) {
630 if (getSlotIndex(I) == Idx) AS = getSlotAttributes(LastIndex++);
634 AttrSet.push_back(getSlotAttributes(I));
637 // Now add the attribute into the correct slot. There may already be an
638 // AttributeSet there.
639 AttrBuilder B(AS, Idx);
641 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
642 if (Attrs.getSlotIndex(I) == Idx) {
643 for (AttributeSetImpl::const_iterator II = Attrs.pImpl->begin(I),
644 IE = Attrs.pImpl->end(I); II != IE; ++II)
649 AttrSet.push_back(AttributeSet::get(C, Idx, B));
651 // Add the remaining attribute slots.
652 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
653 AttrSet.push_back(getSlotAttributes(I));
655 return get(C, AttrSet);
658 AttributeSet AttributeSet::removeAttribute(LLVMContext &C, unsigned Idx,
659 Attribute::AttrKind Attr) const {
660 return removeAttributes(C, Idx, AttributeSet::get(C, Idx, Attr));
663 AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Idx,
664 AttributeSet Attrs) const {
665 if (!pImpl) return AttributeSet();
666 if (!Attrs.pImpl) return *this;
669 // FIXME it is not obvious how this should work for alignment.
670 // For now, say we can't pass in alignment, which no current use does.
671 assert(!Attrs.hasAttribute(Idx, Attribute::Alignment) &&
672 "Attempt to change alignment!");
675 // Add the attribute slots before the one we're trying to add.
676 SmallVector<AttributeSet, 4> AttrSet;
677 uint64_t NumAttrs = pImpl->getNumAttributes();
679 uint64_t LastIndex = 0;
680 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
681 if (getSlotIndex(I) >= Idx) {
682 if (getSlotIndex(I) == Idx) AS = getSlotAttributes(LastIndex++);
686 AttrSet.push_back(getSlotAttributes(I));
689 // Now remove the attribute from the correct slot. There may already be an
690 // AttributeSet there.
691 AttrBuilder B(AS, Idx);
693 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
694 if (Attrs.getSlotIndex(I) == Idx) {
695 B.removeAttributes(Attrs.pImpl->getSlotAttributes(I), Idx);
699 AttrSet.push_back(AttributeSet::get(C, Idx, B));
701 // Add the remaining attribute slots.
702 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
703 AttrSet.push_back(getSlotAttributes(I));
705 return get(C, AttrSet);
708 //===----------------------------------------------------------------------===//
709 // AttributeSet Accessor Methods
710 //===----------------------------------------------------------------------===//
712 AttributeSet AttributeSet::getParamAttributes(unsigned Idx) const {
713 return pImpl && hasAttributes(Idx) ?
714 AttributeSet::get(pImpl->getContext(),
715 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
716 std::make_pair(Idx, getAttributes(Idx)))) :
720 AttributeSet AttributeSet::getRetAttributes() const {
721 return pImpl && hasAttributes(ReturnIndex) ?
722 AttributeSet::get(pImpl->getContext(),
723 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
724 std::make_pair(ReturnIndex,
725 getAttributes(ReturnIndex)))) :
729 AttributeSet AttributeSet::getFnAttributes() const {
730 return pImpl && hasAttributes(FunctionIndex) ?
731 AttributeSet::get(pImpl->getContext(),
732 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
733 std::make_pair(FunctionIndex,
734 getAttributes(FunctionIndex)))) :
738 bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{
739 AttributeSetNode *ASN = getAttributes(Index);
740 return ASN ? ASN->hasAttribute(Kind) : false;
743 bool AttributeSet::hasAttributes(unsigned Index) const {
744 AttributeSetNode *ASN = getAttributes(Index);
745 return ASN ? ASN->hasAttributes() : false;
748 /// \brief Return true if the specified attribute is set for at least one
749 /// parameter or for the return value.
750 bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr) const {
751 if (pImpl == 0) return false;
753 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
754 for (AttributeSetImpl::const_iterator II = pImpl->begin(I),
755 IE = pImpl->end(I); II != IE; ++II)
756 if (II->hasAttribute(Attr))
762 unsigned AttributeSet::getParamAlignment(unsigned Index) const {
763 AttributeSetNode *ASN = getAttributes(Index);
764 return ASN ? ASN->getAlignment() : 0;
767 unsigned AttributeSet::getStackAlignment(unsigned Index) const {
768 AttributeSetNode *ASN = getAttributes(Index);
769 return ASN ? ASN->getStackAlignment() : 0;
772 std::string AttributeSet::getAsString(unsigned Index) const {
773 AttributeSetNode *ASN = getAttributes(Index);
774 return ASN ? ASN->getAsString() : std::string("");
777 /// \brief The attributes for the specified index are returned.
778 AttributeSetNode *AttributeSet::getAttributes(unsigned Idx) const {
779 if (!pImpl) return 0;
781 // Loop through to find the attribute node we want.
782 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
783 if (pImpl->getSlotIndex(I) == Idx)
784 return pImpl->getSlotNode(I);
789 AttributeSet::iterator AttributeSet::begin(unsigned Idx) const {
791 return ArrayRef<Attribute>().begin();
792 return pImpl->begin(Idx);
795 AttributeSet::iterator AttributeSet::end(unsigned Idx) const {
797 return ArrayRef<Attribute>().end();
798 return pImpl->end(Idx);
801 //===----------------------------------------------------------------------===//
802 // AttributeSet Introspection Methods
803 //===----------------------------------------------------------------------===//
805 /// \brief Return the number of slots used in this attribute list. This is the
806 /// number of arguments that have an attribute set on them (including the
807 /// function itself).
808 unsigned AttributeSet::getNumSlots() const {
809 return pImpl ? pImpl->getNumAttributes() : 0;
812 uint64_t AttributeSet::getSlotIndex(unsigned Slot) const {
813 assert(pImpl && Slot < pImpl->getNumAttributes() &&
814 "Slot # out of range!");
815 return pImpl->getSlotIndex(Slot);
818 AttributeSet AttributeSet::getSlotAttributes(unsigned Slot) const {
819 assert(pImpl && Slot < pImpl->getNumAttributes() &&
820 "Slot # out of range!");
821 return pImpl->getSlotAttributes(Slot);
824 uint64_t AttributeSet::Raw(unsigned Index) const {
825 // FIXME: Remove this.
826 return pImpl ? pImpl->Raw(Index) : 0;
829 void AttributeSet::dump() const {
832 for (unsigned i = 0, e = getNumSlots(); i < e; ++i) {
833 uint64_t Index = getSlotIndex(i);
839 dbgs() << " => " << getAsString(Index) << " }\n";
845 //===----------------------------------------------------------------------===//
846 // AttrBuilder Method Implementations
847 //===----------------------------------------------------------------------===//
849 AttrBuilder::AttrBuilder(AttributeSet AS, unsigned Idx)
850 : Alignment(0), StackAlignment(0) {
851 AttributeSetImpl *pImpl = AS.pImpl;
854 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I) {
855 if (pImpl->getSlotIndex(I) != Idx) continue;
857 for (AttributeSetImpl::const_iterator II = pImpl->begin(I),
858 IE = pImpl->end(I); II != IE; ++II)
865 void AttrBuilder::clear() {
867 Alignment = StackAlignment = 0;
870 AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
871 assert(Val != Attribute::Alignment && Val != Attribute::StackAlignment &&
872 "Adding alignment attribute without adding alignment value!");
877 AttrBuilder &AttrBuilder::addAttribute(Attribute Attr) {
878 // FIXME: Handle string attributes.
879 Attribute::AttrKind Kind = Attr.getKindAsEnum();
882 if (Kind == Attribute::Alignment)
883 Alignment = Attr.getAlignment();
884 else if (Kind == Attribute::StackAlignment)
885 StackAlignment = Attr.getStackAlignment();
889 AttrBuilder &AttrBuilder::addAttribute(StringRef A, StringRef V) {
890 TargetDepAttrs[A] = V;
894 AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
897 if (Val == Attribute::Alignment)
899 else if (Val == Attribute::StackAlignment)
905 AttrBuilder &AttrBuilder::removeAttributes(AttributeSet A, uint64_t Index) {
907 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
908 if (A.getSlotIndex(I) == Index) {
913 assert(Idx != ~0U && "Couldn't find index in AttributeSet!");
915 for (AttributeSet::iterator I = A.begin(Idx), E = A.end(Idx); I != E; ++I) {
916 // FIXME: Support string attributes.
917 Attribute::AttrKind Kind = I->getKindAsEnum();
920 if (Kind == Attribute::Alignment)
922 else if (Kind == Attribute::StackAlignment)
929 AttrBuilder &AttrBuilder::removeAttribute(StringRef A) {
930 std::map<std::string, std::string>::iterator I = TargetDepAttrs.find(A);
931 if (I != TargetDepAttrs.end())
932 TargetDepAttrs.erase(I);
936 AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
937 if (Align == 0) return *this;
939 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
940 assert(Align <= 0x40000000 && "Alignment too large.");
942 Attrs.insert(Attribute::Alignment);
947 AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
948 // Default alignment, allow the target to define how to align it.
949 if (Align == 0) return *this;
951 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
952 assert(Align <= 0x100 && "Alignment too large.");
954 Attrs.insert(Attribute::StackAlignment);
955 StackAlignment = Align;
959 bool AttrBuilder::contains(Attribute::AttrKind A) const {
960 return Attrs.count(A);
963 bool AttrBuilder::hasAttributes() const {
964 return !Attrs.empty();
967 bool AttrBuilder::hasAttributes(AttributeSet A, uint64_t Index) const {
969 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
970 if (A.getSlotIndex(I) == Index) {
975 assert(Idx != ~0U && "Couldn't find the index!");
977 for (AttributeSet::iterator I = A.begin(Idx), E = A.end(Idx);
979 // FIXME: Support string attributes.
980 if (Attrs.count(I->getKindAsEnum()))
986 bool AttrBuilder::hasAlignmentAttr() const {
987 return Alignment != 0;
990 bool AttrBuilder::operator==(const AttrBuilder &B) {
991 SmallVector<Attribute::AttrKind, 8> This(Attrs.begin(), Attrs.end());
992 SmallVector<Attribute::AttrKind, 8> That(B.Attrs.begin(), B.Attrs.end());
996 AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) {
997 // FIXME: Remove this in 4.0.
998 if (!Val) return *this;
1000 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
1001 I = Attribute::AttrKind(I + 1)) {
1002 if (uint64_t A = (Val & AttributeImpl::getAttrMask(I))) {
1005 if (I == Attribute::Alignment)
1006 Alignment = 1ULL << ((A >> 16) - 1);
1007 else if (I == Attribute::StackAlignment)
1008 StackAlignment = 1ULL << ((A >> 26)-1);
1015 //===----------------------------------------------------------------------===//
1016 // AttributeFuncs Function Defintions
1017 //===----------------------------------------------------------------------===//
1019 /// \brief Which attributes cannot be applied to a type.
1020 AttributeSet AttributeFuncs::typeIncompatible(Type *Ty, uint64_t Index) {
1021 AttrBuilder Incompatible;
1023 if (!Ty->isIntegerTy())
1024 // Attribute that only apply to integers.
1025 Incompatible.addAttribute(Attribute::SExt)
1026 .addAttribute(Attribute::ZExt);
1028 if (!Ty->isPointerTy())
1029 // Attribute that only apply to pointers.
1030 Incompatible.addAttribute(Attribute::ByVal)
1031 .addAttribute(Attribute::Nest)
1032 .addAttribute(Attribute::NoAlias)
1033 .addAttribute(Attribute::NoCapture)
1034 .addAttribute(Attribute::StructRet);
1036 return AttributeSet::get(Ty->getContext(), Index, Incompatible);