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::SanitizeAddress))
157 return "sanitize_address";
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::Returned))
200 if (hasAttribute(Attribute::ReturnsTwice))
201 return "returns_twice";
202 if (hasAttribute(Attribute::SExt))
204 if (hasAttribute(Attribute::StackProtect))
206 if (hasAttribute(Attribute::StackProtectReq))
208 if (hasAttribute(Attribute::StackProtectStrong))
210 if (hasAttribute(Attribute::StructRet))
212 if (hasAttribute(Attribute::SanitizeThread))
213 return "sanitize_thread";
214 if (hasAttribute(Attribute::SanitizeMemory))
215 return "sanitize_memory";
216 if (hasAttribute(Attribute::UWTable))
218 if (hasAttribute(Attribute::ZExt))
221 // FIXME: These should be output like this:
226 if (hasAttribute(Attribute::Alignment)) {
229 Result += (InAttrGrp) ? "=" : " ";
230 Result += utostr(getValueAsInt());
234 if (hasAttribute(Attribute::StackAlignment)) {
236 Result += "alignstack";
239 Result += utostr(getValueAsInt());
242 Result += utostr(getValueAsInt());
248 // Convert target-dependent attributes to strings of the form:
253 if (isStringAttribute()) {
255 Result += '\"' + getKindAsString().str() + '"';
257 StringRef Val = pImpl->getValueAsString();
258 if (Val.empty()) return Result;
260 Result += "=\"" + Val.str() + '"';
264 llvm_unreachable("Unknown attribute");
267 bool Attribute::operator<(Attribute A) const {
268 if (!pImpl && !A.pImpl) return false;
269 if (!pImpl) return true;
270 if (!A.pImpl) return false;
271 return *pImpl < *A.pImpl;
274 //===----------------------------------------------------------------------===//
275 // AttributeImpl Definition
276 //===----------------------------------------------------------------------===//
278 AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind Kind)
279 : Context(C), Entry(new EnumAttributeEntry(Kind)) {}
281 AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind Kind,
284 assert((Kind == Attribute::Alignment || Kind == Attribute::StackAlignment) &&
285 "Wrong kind for alignment attribute!");
286 Entry = new AlignAttributeEntry(Kind, Align);
289 AttributeImpl::AttributeImpl(LLVMContext &C, StringRef Kind, StringRef Val)
290 : Context(C), Entry(new StringAttributeEntry(Kind, Val)) {}
292 AttributeImpl::~AttributeImpl() {
296 bool AttributeImpl::isEnumAttribute() const {
297 return isa<EnumAttributeEntry>(Entry);
300 bool AttributeImpl::isAlignAttribute() const {
301 return isa<AlignAttributeEntry>(Entry);
304 bool AttributeImpl::isStringAttribute() const {
305 return isa<StringAttributeEntry>(Entry);
308 bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
309 if (isStringAttribute()) return false;
310 return getKindAsEnum() == A;
313 bool AttributeImpl::hasAttribute(StringRef Kind) const {
314 if (!isStringAttribute()) return false;
315 return getKindAsString() == Kind;
318 Attribute::AttrKind AttributeImpl::getKindAsEnum() const {
319 if (EnumAttributeEntry *E = dyn_cast<EnumAttributeEntry>(Entry))
320 return E->getEnumKind();
321 return cast<AlignAttributeEntry>(Entry)->getEnumKind();
324 uint64_t AttributeImpl::getValueAsInt() const {
325 return cast<AlignAttributeEntry>(Entry)->getAlignment();
328 StringRef AttributeImpl::getKindAsString() const {
329 return cast<StringAttributeEntry>(Entry)->getStringKind();
332 StringRef AttributeImpl::getValueAsString() const {
333 return cast<StringAttributeEntry>(Entry)->getStringValue();
336 bool AttributeImpl::operator<(const AttributeImpl &AI) const {
337 // This sorts the attributes with Attribute::AttrKinds coming first (sorted
338 // relative to their enum value) and then strings.
339 if (isEnumAttribute()) {
340 if (AI.isEnumAttribute()) return getKindAsEnum() < AI.getKindAsEnum();
341 if (AI.isAlignAttribute()) return true;
342 if (AI.isStringAttribute()) return true;
345 if (isAlignAttribute()) {
346 if (AI.isEnumAttribute()) return false;
347 if (AI.isAlignAttribute()) return getValueAsInt() < AI.getValueAsInt();
348 if (AI.isStringAttribute()) return true;
351 if (AI.isEnumAttribute()) return false;
352 if (AI.isAlignAttribute()) return false;
353 if (getKindAsString() == AI.getKindAsString())
354 return getValueAsString() < AI.getValueAsString();
355 return getKindAsString() < AI.getKindAsString();
358 uint64_t AttributeImpl::getAttrMask(Attribute::AttrKind Val) {
359 // FIXME: Remove this.
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::SanitizeAddress: 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::SanitizeThread: return 1ULL << 36;
396 case Attribute::SanitizeMemory: return 1ULL << 37;
397 case Attribute::NoBuiltin: return 1ULL << 38;
398 case Attribute::Returned: return 1ULL << 39;
400 llvm_unreachable("Unsupported attribute type");
403 //===----------------------------------------------------------------------===//
404 // AttributeSetNode Definition
405 //===----------------------------------------------------------------------===//
407 AttributeSetNode *AttributeSetNode::get(LLVMContext &C,
408 ArrayRef<Attribute> Attrs) {
412 // Otherwise, build a key to look up the existing attributes.
413 LLVMContextImpl *pImpl = C.pImpl;
416 SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end());
417 array_pod_sort(SortedAttrs.begin(), SortedAttrs.end());
419 for (SmallVectorImpl<Attribute>::iterator I = SortedAttrs.begin(),
420 E = SortedAttrs.end(); I != E; ++I)
424 AttributeSetNode *PA =
425 pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint);
427 // If we didn't find any existing attributes of the same shape then create a
428 // new one and insert it.
430 PA = new AttributeSetNode(SortedAttrs);
431 pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint);
434 // Return the AttributesListNode that we found or created.
438 bool AttributeSetNode::hasAttribute(Attribute::AttrKind Kind) const {
439 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
440 E = AttrList.end(); I != E; ++I)
441 if (I->hasAttribute(Kind))
446 bool AttributeSetNode::hasAttribute(StringRef Kind) const {
447 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
448 E = AttrList.end(); I != E; ++I)
449 if (I->hasAttribute(Kind))
454 Attribute AttributeSetNode::getAttribute(Attribute::AttrKind Kind) const {
455 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
456 E = AttrList.end(); I != E; ++I)
457 if (I->hasAttribute(Kind))
462 Attribute AttributeSetNode::getAttribute(StringRef Kind) const {
463 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
464 E = AttrList.end(); I != E; ++I)
465 if (I->hasAttribute(Kind))
470 unsigned AttributeSetNode::getAlignment() const {
471 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
472 E = AttrList.end(); I != E; ++I)
473 if (I->hasAttribute(Attribute::Alignment))
474 return I->getAlignment();
478 unsigned AttributeSetNode::getStackAlignment() const {
479 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
480 E = AttrList.end(); I != E; ++I)
481 if (I->hasAttribute(Attribute::StackAlignment))
482 return I->getStackAlignment();
486 std::string AttributeSetNode::getAsString(bool TargetIndependent,
487 bool InAttrGrp) const {
489 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
490 E = AttrList.end(); I != E; ++I) {
491 if (TargetIndependent || !I->isStringAttribute()) {
492 if (I != AttrList.begin())
494 Str += I->getAsString(InAttrGrp);
500 //===----------------------------------------------------------------------===//
501 // AttributeSetImpl Definition
502 //===----------------------------------------------------------------------===//
504 uint64_t AttributeSetImpl::Raw(uint64_t Index) const {
505 for (unsigned I = 0, E = getNumAttributes(); I != E; ++I) {
506 if (getSlotIndex(I) != Index) continue;
507 const AttributeSetNode *ASN = AttrNodes[I].second;
510 for (AttributeSetNode::const_iterator II = ASN->begin(),
511 IE = ASN->end(); II != IE; ++II) {
512 Attribute Attr = *II;
514 // This cannot handle string attributes.
515 if (Attr.isStringAttribute()) continue;
517 Attribute::AttrKind Kind = Attr.getKindAsEnum();
519 if (Kind == Attribute::Alignment)
520 Mask |= (Log2_32(ASN->getAlignment()) + 1) << 16;
521 else if (Kind == Attribute::StackAlignment)
522 Mask |= (Log2_32(ASN->getStackAlignment()) + 1) << 26;
524 Mask |= AttributeImpl::getAttrMask(Kind);
533 //===----------------------------------------------------------------------===//
534 // AttributeSet Construction and Mutation Methods
535 //===----------------------------------------------------------------------===//
538 AttributeSet::getImpl(LLVMContext &C,
539 ArrayRef<std::pair<unsigned, AttributeSetNode*> > Attrs) {
540 LLVMContextImpl *pImpl = C.pImpl;
542 AttributeSetImpl::Profile(ID, Attrs);
545 AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
547 // If we didn't find any existing attributes of the same shape then
548 // create a new one and insert it.
550 PA = new AttributeSetImpl(C, Attrs);
551 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
554 // Return the AttributesList that we found or created.
555 return AttributeSet(PA);
558 AttributeSet AttributeSet::get(LLVMContext &C,
559 ArrayRef<std::pair<unsigned, Attribute> > Attrs){
560 // If there are no attributes then return a null AttributesList pointer.
562 return AttributeSet();
565 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
566 assert((!i || Attrs[i-1].first <= Attrs[i].first) &&
567 "Misordered Attributes list!");
568 assert(!Attrs[i].second.hasAttribute(Attribute::None) &&
569 "Pointless attribute!");
573 // Create a vector if (unsigned, AttributeSetNode*) pairs from the attributes
575 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrPairVec;
576 for (ArrayRef<std::pair<unsigned, Attribute> >::iterator I = Attrs.begin(),
577 E = Attrs.end(); I != E; ) {
578 unsigned Index = I->first;
579 SmallVector<Attribute, 4> AttrVec;
580 while (I != E && I->first == Index) {
581 AttrVec.push_back(I->second);
585 AttrPairVec.push_back(std::make_pair(Index,
586 AttributeSetNode::get(C, AttrVec)));
589 return getImpl(C, AttrPairVec);
592 AttributeSet AttributeSet::get(LLVMContext &C,
593 ArrayRef<std::pair<unsigned,
594 AttributeSetNode*> > Attrs) {
595 // If there are no attributes then return a null AttributesList pointer.
597 return AttributeSet();
599 return getImpl(C, Attrs);
602 AttributeSet AttributeSet::get(LLVMContext &C, unsigned Index, AttrBuilder &B) {
603 if (!B.hasAttributes())
604 return AttributeSet();
606 // Add target-independent attributes.
607 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
608 for (Attribute::AttrKind Kind = Attribute::None;
609 Kind != Attribute::EndAttrKinds; Kind = Attribute::AttrKind(Kind + 1)) {
610 if (!B.contains(Kind))
613 if (Kind == Attribute::Alignment)
614 Attrs.push_back(std::make_pair(Index, Attribute::
615 getWithAlignment(C, B.getAlignment())));
616 else if (Kind == Attribute::StackAlignment)
617 Attrs.push_back(std::make_pair(Index, Attribute::
618 getWithStackAlignment(C, B.getStackAlignment())));
620 Attrs.push_back(std::make_pair(Index, Attribute::get(C, Kind)));
623 // Add target-dependent (string) attributes.
624 for (AttrBuilder::td_iterator I = B.td_begin(), E = B.td_end();
626 Attrs.push_back(std::make_pair(Index, Attribute::get(C, I->first,I->second)));
628 return get(C, Attrs);
631 AttributeSet AttributeSet::get(LLVMContext &C, unsigned Index,
632 ArrayRef<Attribute::AttrKind> Kind) {
633 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
634 for (ArrayRef<Attribute::AttrKind>::iterator I = Kind.begin(),
635 E = Kind.end(); I != E; ++I)
636 Attrs.push_back(std::make_pair(Index, Attribute::get(C, *I)));
637 return get(C, Attrs);
640 AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<AttributeSet> Attrs) {
641 if (Attrs.empty()) return AttributeSet();
643 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrNodeVec;
644 for (unsigned I = 0, E = Attrs.size(); I != E; ++I) {
645 AttributeSet AS = Attrs[I];
646 if (!AS.pImpl) continue;
647 AttrNodeVec.append(AS.pImpl->AttrNodes.begin(), AS.pImpl->AttrNodes.end());
650 return getImpl(C, AttrNodeVec);
653 AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
654 Attribute::AttrKind Attr) const {
655 if (hasAttribute(Index, Attr)) return *this;
656 return addAttributes(C, Index, AttributeSet::get(C, Index, Attr));
659 AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
660 StringRef Kind) const {
662 B.addAttribute(Kind);
663 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
666 AttributeSet AttributeSet::addAttributes(LLVMContext &C, unsigned Index,
667 AttributeSet Attrs) const {
668 if (!pImpl) return Attrs;
669 if (!Attrs.pImpl) return *this;
672 // FIXME it is not obvious how this should work for alignment. For now, say
673 // we can't change a known alignment.
674 unsigned OldAlign = getParamAlignment(Index);
675 unsigned NewAlign = Attrs.getParamAlignment(Index);
676 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
677 "Attempt to change alignment!");
680 // Add the attribute slots before the one we're trying to add.
681 SmallVector<AttributeSet, 4> AttrSet;
682 uint64_t NumAttrs = pImpl->getNumAttributes();
684 uint64_t LastIndex = 0;
685 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
686 if (getSlotIndex(I) >= Index) {
687 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
691 AttrSet.push_back(getSlotAttributes(I));
694 // Now add the attribute into the correct slot. There may already be an
695 // AttributeSet there.
696 AttrBuilder B(AS, Index);
698 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
699 if (Attrs.getSlotIndex(I) == Index) {
700 for (AttributeSetImpl::const_iterator II = Attrs.pImpl->begin(I),
701 IE = Attrs.pImpl->end(I); II != IE; ++II)
706 AttrSet.push_back(AttributeSet::get(C, Index, B));
708 // Add the remaining attribute slots.
709 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
710 AttrSet.push_back(getSlotAttributes(I));
712 return get(C, AttrSet);
715 AttributeSet AttributeSet::removeAttribute(LLVMContext &C, unsigned Index,
716 Attribute::AttrKind Attr) const {
717 if (!hasAttribute(Index, Attr)) return *this;
718 return removeAttributes(C, Index, AttributeSet::get(C, Index, Attr));
721 AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Index,
722 AttributeSet Attrs) const {
723 if (!pImpl) return AttributeSet();
724 if (!Attrs.pImpl) return *this;
727 // FIXME it is not obvious how this should work for alignment.
728 // For now, say we can't pass in alignment, which no current use does.
729 assert(!Attrs.hasAttribute(Index, Attribute::Alignment) &&
730 "Attempt to change alignment!");
733 // Add the attribute slots before the one we're trying to add.
734 SmallVector<AttributeSet, 4> AttrSet;
735 uint64_t NumAttrs = pImpl->getNumAttributes();
737 uint64_t LastIndex = 0;
738 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
739 if (getSlotIndex(I) >= Index) {
740 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
744 AttrSet.push_back(getSlotAttributes(I));
747 // Now remove the attribute from the correct slot. There may already be an
748 // AttributeSet there.
749 AttrBuilder B(AS, Index);
751 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
752 if (Attrs.getSlotIndex(I) == Index) {
753 B.removeAttributes(Attrs.pImpl->getSlotAttributes(I), Index);
757 AttrSet.push_back(AttributeSet::get(C, Index, B));
759 // Add the remaining attribute slots.
760 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
761 AttrSet.push_back(getSlotAttributes(I));
763 return get(C, AttrSet);
766 //===----------------------------------------------------------------------===//
767 // AttributeSet Accessor Methods
768 //===----------------------------------------------------------------------===//
770 LLVMContext &AttributeSet::getContext() const {
771 return pImpl->getContext();
774 AttributeSet AttributeSet::getParamAttributes(unsigned Index) const {
775 return pImpl && hasAttributes(Index) ?
776 AttributeSet::get(pImpl->getContext(),
777 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
778 std::make_pair(Index, getAttributes(Index)))) :
782 AttributeSet AttributeSet::getRetAttributes() const {
783 return pImpl && hasAttributes(ReturnIndex) ?
784 AttributeSet::get(pImpl->getContext(),
785 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
786 std::make_pair(ReturnIndex,
787 getAttributes(ReturnIndex)))) :
791 AttributeSet AttributeSet::getFnAttributes() const {
792 return pImpl && hasAttributes(FunctionIndex) ?
793 AttributeSet::get(pImpl->getContext(),
794 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
795 std::make_pair(FunctionIndex,
796 getAttributes(FunctionIndex)))) :
800 bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{
801 AttributeSetNode *ASN = getAttributes(Index);
802 return ASN ? ASN->hasAttribute(Kind) : false;
805 bool AttributeSet::hasAttribute(unsigned Index, StringRef Kind) const {
806 AttributeSetNode *ASN = getAttributes(Index);
807 return ASN ? ASN->hasAttribute(Kind) : false;
810 bool AttributeSet::hasAttributes(unsigned Index) const {
811 AttributeSetNode *ASN = getAttributes(Index);
812 return ASN ? ASN->hasAttributes() : false;
815 /// \brief Return true if the specified attribute is set for at least one
816 /// parameter or for the return value.
817 bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr) const {
818 if (pImpl == 0) return false;
820 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
821 for (AttributeSetImpl::const_iterator II = pImpl->begin(I),
822 IE = pImpl->end(I); II != IE; ++II)
823 if (II->hasAttribute(Attr))
829 Attribute AttributeSet::getAttribute(unsigned Index,
830 Attribute::AttrKind Kind) const {
831 AttributeSetNode *ASN = getAttributes(Index);
832 return ASN ? ASN->getAttribute(Kind) : Attribute();
835 Attribute AttributeSet::getAttribute(unsigned Index,
836 StringRef Kind) const {
837 AttributeSetNode *ASN = getAttributes(Index);
838 return ASN ? ASN->getAttribute(Kind) : Attribute();
841 unsigned AttributeSet::getParamAlignment(unsigned Index) const {
842 AttributeSetNode *ASN = getAttributes(Index);
843 return ASN ? ASN->getAlignment() : 0;
846 unsigned AttributeSet::getStackAlignment(unsigned Index) const {
847 AttributeSetNode *ASN = getAttributes(Index);
848 return ASN ? ASN->getStackAlignment() : 0;
851 std::string AttributeSet::getAsString(unsigned Index, bool TargetIndependent,
852 bool InAttrGrp) const {
853 AttributeSetNode *ASN = getAttributes(Index);
854 return ASN ? ASN->getAsString(TargetIndependent, InAttrGrp) :
858 /// \brief The attributes for the specified index are returned.
859 AttributeSetNode *AttributeSet::getAttributes(unsigned Index) const {
860 if (!pImpl) return 0;
862 // Loop through to find the attribute node we want.
863 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
864 if (pImpl->getSlotIndex(I) == Index)
865 return pImpl->getSlotNode(I);
870 AttributeSet::iterator AttributeSet::begin(unsigned Slot) const {
872 return ArrayRef<Attribute>().begin();
873 return pImpl->begin(Slot);
876 AttributeSet::iterator AttributeSet::end(unsigned Slot) const {
878 return ArrayRef<Attribute>().end();
879 return pImpl->end(Slot);
882 //===----------------------------------------------------------------------===//
883 // AttributeSet Introspection Methods
884 //===----------------------------------------------------------------------===//
886 /// \brief Return the number of slots used in this attribute list. This is the
887 /// number of arguments that have an attribute set on them (including the
888 /// function itself).
889 unsigned AttributeSet::getNumSlots() const {
890 return pImpl ? pImpl->getNumAttributes() : 0;
893 uint64_t AttributeSet::getSlotIndex(unsigned Slot) const {
894 assert(pImpl && Slot < pImpl->getNumAttributes() &&
895 "Slot # out of range!");
896 return pImpl->getSlotIndex(Slot);
899 AttributeSet AttributeSet::getSlotAttributes(unsigned Slot) const {
900 assert(pImpl && Slot < pImpl->getNumAttributes() &&
901 "Slot # out of range!");
902 return pImpl->getSlotAttributes(Slot);
905 uint64_t AttributeSet::Raw(unsigned Index) const {
906 // FIXME: Remove this.
907 return pImpl ? pImpl->Raw(Index) : 0;
910 void AttributeSet::dump() const {
913 for (unsigned i = 0, e = getNumSlots(); i < e; ++i) {
914 uint64_t Index = getSlotIndex(i);
920 dbgs() << " => " << getAsString(Index) << " }\n";
926 //===----------------------------------------------------------------------===//
927 // AttrBuilder Method Implementations
928 //===----------------------------------------------------------------------===//
930 AttrBuilder::AttrBuilder(AttributeSet AS, unsigned Index)
931 : Attrs(0), Alignment(0), StackAlignment(0) {
932 AttributeSetImpl *pImpl = AS.pImpl;
935 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I) {
936 if (pImpl->getSlotIndex(I) != Index) continue;
938 for (AttributeSetImpl::const_iterator II = pImpl->begin(I),
939 IE = pImpl->end(I); II != IE; ++II)
946 void AttrBuilder::clear() {
948 Alignment = StackAlignment = 0;
951 AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
952 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
953 assert(Val != Attribute::Alignment && Val != Attribute::StackAlignment &&
954 "Adding alignment attribute without adding alignment value!");
959 AttrBuilder &AttrBuilder::addAttribute(Attribute Attr) {
960 if (Attr.isStringAttribute()) {
961 addAttribute(Attr.getKindAsString(), Attr.getValueAsString());
965 Attribute::AttrKind Kind = Attr.getKindAsEnum();
968 if (Kind == Attribute::Alignment)
969 Alignment = Attr.getAlignment();
970 else if (Kind == Attribute::StackAlignment)
971 StackAlignment = Attr.getStackAlignment();
975 AttrBuilder &AttrBuilder::addAttribute(StringRef A, StringRef V) {
976 TargetDepAttrs[A] = V;
980 AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
981 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
984 if (Val == Attribute::Alignment)
986 else if (Val == Attribute::StackAlignment)
992 AttrBuilder &AttrBuilder::removeAttributes(AttributeSet A, uint64_t Index) {
994 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
995 if (A.getSlotIndex(I) == Index) {
1000 assert(Slot != ~0U && "Couldn't find index in AttributeSet!");
1002 for (AttributeSet::iterator I = A.begin(Slot), E = A.end(Slot); I != E; ++I) {
1003 Attribute Attr = *I;
1004 if (Attr.isEnumAttribute() || Attr.isAlignAttribute()) {
1005 Attribute::AttrKind Kind = I->getKindAsEnum();
1006 Attrs[Kind] = false;
1008 if (Kind == Attribute::Alignment)
1010 else if (Kind == Attribute::StackAlignment)
1013 assert(Attr.isStringAttribute() && "Invalid attribute type!");
1014 std::map<std::string, std::string>::iterator
1015 Iter = TargetDepAttrs.find(Attr.getKindAsString());
1016 if (Iter != TargetDepAttrs.end())
1017 TargetDepAttrs.erase(Iter);
1024 AttrBuilder &AttrBuilder::removeAttribute(StringRef A) {
1025 std::map<std::string, std::string>::iterator I = TargetDepAttrs.find(A);
1026 if (I != TargetDepAttrs.end())
1027 TargetDepAttrs.erase(I);
1031 AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
1032 if (Align == 0) return *this;
1034 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1035 assert(Align <= 0x40000000 && "Alignment too large.");
1037 Attrs[Attribute::Alignment] = true;
1042 AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
1043 // Default alignment, allow the target to define how to align it.
1044 if (Align == 0) return *this;
1046 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1047 assert(Align <= 0x100 && "Alignment too large.");
1049 Attrs[Attribute::StackAlignment] = true;
1050 StackAlignment = Align;
1054 AttrBuilder &AttrBuilder::merge(const AttrBuilder &B) {
1055 // FIXME: What if both have alignments, but they don't match?!
1057 Alignment = B.Alignment;
1059 if (!StackAlignment)
1060 StackAlignment = B.StackAlignment;
1064 for (td_const_iterator I = B.TargetDepAttrs.begin(),
1065 E = B.TargetDepAttrs.end(); I != E; ++I)
1066 TargetDepAttrs[I->first] = I->second;
1071 bool AttrBuilder::contains(StringRef A) const {
1072 return TargetDepAttrs.find(A) != TargetDepAttrs.end();
1075 bool AttrBuilder::hasAttributes() const {
1076 return !Attrs.none() || !TargetDepAttrs.empty();
1079 bool AttrBuilder::hasAttributes(AttributeSet A, uint64_t Index) const {
1080 unsigned Slot = ~0U;
1081 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
1082 if (A.getSlotIndex(I) == Index) {
1087 assert(Slot != ~0U && "Couldn't find the index!");
1089 for (AttributeSet::iterator I = A.begin(Slot), E = A.end(Slot);
1091 Attribute Attr = *I;
1092 if (Attr.isEnumAttribute() || Attr.isAlignAttribute()) {
1093 if (Attrs[I->getKindAsEnum()])
1096 assert(Attr.isStringAttribute() && "Invalid attribute kind!");
1097 return TargetDepAttrs.find(Attr.getKindAsString())!=TargetDepAttrs.end();
1104 bool AttrBuilder::hasAlignmentAttr() const {
1105 return Alignment != 0;
1108 bool AttrBuilder::operator==(const AttrBuilder &B) {
1109 if (Attrs != B.Attrs)
1112 for (td_const_iterator I = TargetDepAttrs.begin(),
1113 E = TargetDepAttrs.end(); I != E; ++I)
1114 if (B.TargetDepAttrs.find(I->first) == B.TargetDepAttrs.end())
1117 return Alignment == B.Alignment && StackAlignment == B.StackAlignment;
1120 AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) {
1121 // FIXME: Remove this in 4.0.
1122 if (!Val) return *this;
1124 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
1125 I = Attribute::AttrKind(I + 1)) {
1126 if (uint64_t A = (Val & AttributeImpl::getAttrMask(I))) {
1129 if (I == Attribute::Alignment)
1130 Alignment = 1ULL << ((A >> 16) - 1);
1131 else if (I == Attribute::StackAlignment)
1132 StackAlignment = 1ULL << ((A >> 26)-1);
1139 //===----------------------------------------------------------------------===//
1140 // AttributeFuncs Function Defintions
1141 //===----------------------------------------------------------------------===//
1143 /// \brief Which attributes cannot be applied to a type.
1144 AttributeSet AttributeFuncs::typeIncompatible(Type *Ty, uint64_t Index) {
1145 AttrBuilder Incompatible;
1147 if (!Ty->isIntegerTy())
1148 // Attribute that only apply to integers.
1149 Incompatible.addAttribute(Attribute::SExt)
1150 .addAttribute(Attribute::ZExt);
1152 if (!Ty->isPointerTy())
1153 // Attribute that only apply to pointers.
1154 Incompatible.addAttribute(Attribute::ByVal)
1155 .addAttribute(Attribute::Nest)
1156 .addAttribute(Attribute::NoAlias)
1157 .addAttribute(Attribute::NoCapture)
1158 .addAttribute(Attribute::StructRet);
1160 return AttributeSet::get(Ty->getContext(), Index, Incompatible);