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::Builtin))
162 if (hasAttribute(Attribute::ByVal))
164 if (hasAttribute(Attribute::InlineHint))
166 if (hasAttribute(Attribute::InReg))
168 if (hasAttribute(Attribute::MinSize))
170 if (hasAttribute(Attribute::Naked))
172 if (hasAttribute(Attribute::Nest))
174 if (hasAttribute(Attribute::NoAlias))
176 if (hasAttribute(Attribute::NoBuiltin))
178 if (hasAttribute(Attribute::NoCapture))
180 if (hasAttribute(Attribute::NoDuplicate))
181 return "noduplicate";
182 if (hasAttribute(Attribute::NoImplicitFloat))
183 return "noimplicitfloat";
184 if (hasAttribute(Attribute::NoInline))
186 if (hasAttribute(Attribute::NonLazyBind))
187 return "nonlazybind";
188 if (hasAttribute(Attribute::NoRedZone))
190 if (hasAttribute(Attribute::NoReturn))
192 if (hasAttribute(Attribute::NoUnwind))
194 if (hasAttribute(Attribute::OptimizeForSize))
196 if (hasAttribute(Attribute::ReadNone))
198 if (hasAttribute(Attribute::ReadOnly))
200 if (hasAttribute(Attribute::Returned))
202 if (hasAttribute(Attribute::ReturnsTwice))
203 return "returns_twice";
204 if (hasAttribute(Attribute::SExt))
206 if (hasAttribute(Attribute::StackProtect))
208 if (hasAttribute(Attribute::StackProtectReq))
210 if (hasAttribute(Attribute::StackProtectStrong))
212 if (hasAttribute(Attribute::StructRet))
214 if (hasAttribute(Attribute::SanitizeThread))
215 return "sanitize_thread";
216 if (hasAttribute(Attribute::SanitizeMemory))
217 return "sanitize_memory";
218 if (hasAttribute(Attribute::UWTable))
220 if (hasAttribute(Attribute::ZExt))
222 if (hasAttribute(Attribute::Cold))
225 // FIXME: These should be output like this:
230 if (hasAttribute(Attribute::Alignment)) {
233 Result += (InAttrGrp) ? "=" : " ";
234 Result += utostr(getValueAsInt());
238 if (hasAttribute(Attribute::StackAlignment)) {
240 Result += "alignstack";
243 Result += utostr(getValueAsInt());
246 Result += utostr(getValueAsInt());
252 // Convert target-dependent attributes to strings of the form:
257 if (isStringAttribute()) {
259 Result += '\"' + getKindAsString().str() + '"';
261 StringRef Val = pImpl->getValueAsString();
262 if (Val.empty()) return Result;
264 Result += "=\"" + Val.str() + '"';
268 llvm_unreachable("Unknown attribute");
271 bool Attribute::operator<(Attribute A) const {
272 if (!pImpl && !A.pImpl) return false;
273 if (!pImpl) return true;
274 if (!A.pImpl) return false;
275 return *pImpl < *A.pImpl;
278 //===----------------------------------------------------------------------===//
279 // AttributeImpl Definition
280 //===----------------------------------------------------------------------===//
282 AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind Kind)
283 : Context(C), Entry(new EnumAttributeEntry(Kind)) {}
285 AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind Kind,
288 assert((Kind == Attribute::Alignment || Kind == Attribute::StackAlignment) &&
289 "Wrong kind for alignment attribute!");
290 Entry = new AlignAttributeEntry(Kind, Align);
293 AttributeImpl::AttributeImpl(LLVMContext &C, StringRef Kind, StringRef Val)
294 : Context(C), Entry(new StringAttributeEntry(Kind, Val)) {}
296 AttributeImpl::~AttributeImpl() {
300 bool AttributeImpl::isEnumAttribute() const {
301 return isa<EnumAttributeEntry>(Entry);
304 bool AttributeImpl::isAlignAttribute() const {
305 return isa<AlignAttributeEntry>(Entry);
308 bool AttributeImpl::isStringAttribute() const {
309 return isa<StringAttributeEntry>(Entry);
312 bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
313 if (isStringAttribute()) return false;
314 return getKindAsEnum() == A;
317 bool AttributeImpl::hasAttribute(StringRef Kind) const {
318 if (!isStringAttribute()) return false;
319 return getKindAsString() == Kind;
322 Attribute::AttrKind AttributeImpl::getKindAsEnum() const {
323 if (EnumAttributeEntry *E = dyn_cast<EnumAttributeEntry>(Entry))
324 return E->getEnumKind();
325 return cast<AlignAttributeEntry>(Entry)->getEnumKind();
328 uint64_t AttributeImpl::getValueAsInt() const {
329 return cast<AlignAttributeEntry>(Entry)->getAlignment();
332 StringRef AttributeImpl::getKindAsString() const {
333 return cast<StringAttributeEntry>(Entry)->getStringKind();
336 StringRef AttributeImpl::getValueAsString() const {
337 return cast<StringAttributeEntry>(Entry)->getStringValue();
340 bool AttributeImpl::operator<(const AttributeImpl &AI) const {
341 // This sorts the attributes with Attribute::AttrKinds coming first (sorted
342 // relative to their enum value) and then strings.
343 if (isEnumAttribute()) {
344 if (AI.isEnumAttribute()) return getKindAsEnum() < AI.getKindAsEnum();
345 if (AI.isAlignAttribute()) return true;
346 if (AI.isStringAttribute()) return true;
349 if (isAlignAttribute()) {
350 if (AI.isEnumAttribute()) return false;
351 if (AI.isAlignAttribute()) return getValueAsInt() < AI.getValueAsInt();
352 if (AI.isStringAttribute()) return true;
355 if (AI.isEnumAttribute()) return false;
356 if (AI.isAlignAttribute()) return false;
357 if (getKindAsString() == AI.getKindAsString())
358 return getValueAsString() < AI.getValueAsString();
359 return getKindAsString() < AI.getKindAsString();
362 uint64_t AttributeImpl::getAttrMask(Attribute::AttrKind Val) {
363 // FIXME: Remove this.
365 case Attribute::EndAttrKinds:
366 llvm_unreachable("Synthetic enumerators which should never get here");
368 case Attribute::None: return 0;
369 case Attribute::ZExt: return 1 << 0;
370 case Attribute::SExt: return 1 << 1;
371 case Attribute::NoReturn: return 1 << 2;
372 case Attribute::InReg: return 1 << 3;
373 case Attribute::StructRet: return 1 << 4;
374 case Attribute::NoUnwind: return 1 << 5;
375 case Attribute::NoAlias: return 1 << 6;
376 case Attribute::ByVal: return 1 << 7;
377 case Attribute::Nest: return 1 << 8;
378 case Attribute::ReadNone: return 1 << 9;
379 case Attribute::ReadOnly: return 1 << 10;
380 case Attribute::NoInline: return 1 << 11;
381 case Attribute::AlwaysInline: return 1 << 12;
382 case Attribute::OptimizeForSize: return 1 << 13;
383 case Attribute::StackProtect: return 1 << 14;
384 case Attribute::StackProtectReq: return 1 << 15;
385 case Attribute::Alignment: return 31 << 16;
386 case Attribute::NoCapture: return 1 << 21;
387 case Attribute::NoRedZone: return 1 << 22;
388 case Attribute::NoImplicitFloat: return 1 << 23;
389 case Attribute::Naked: return 1 << 24;
390 case Attribute::InlineHint: return 1 << 25;
391 case Attribute::StackAlignment: return 7 << 26;
392 case Attribute::ReturnsTwice: return 1 << 29;
393 case Attribute::UWTable: return 1 << 30;
394 case Attribute::NonLazyBind: return 1U << 31;
395 case Attribute::SanitizeAddress: return 1ULL << 32;
396 case Attribute::MinSize: return 1ULL << 33;
397 case Attribute::NoDuplicate: return 1ULL << 34;
398 case Attribute::StackProtectStrong: return 1ULL << 35;
399 case Attribute::SanitizeThread: return 1ULL << 36;
400 case Attribute::SanitizeMemory: return 1ULL << 37;
401 case Attribute::NoBuiltin: return 1ULL << 38;
402 case Attribute::Returned: return 1ULL << 39;
403 case Attribute::Cold: return 1ULL << 40;
404 case Attribute::Builtin: return 1ULL << 41;
406 llvm_unreachable("Unsupported attribute type");
409 //===----------------------------------------------------------------------===//
410 // AttributeSetNode Definition
411 //===----------------------------------------------------------------------===//
413 AttributeSetNode *AttributeSetNode::get(LLVMContext &C,
414 ArrayRef<Attribute> Attrs) {
418 // Otherwise, build a key to look up the existing attributes.
419 LLVMContextImpl *pImpl = C.pImpl;
422 SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end());
423 array_pod_sort(SortedAttrs.begin(), SortedAttrs.end());
425 for (SmallVectorImpl<Attribute>::iterator I = SortedAttrs.begin(),
426 E = SortedAttrs.end(); I != E; ++I)
430 AttributeSetNode *PA =
431 pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint);
433 // If we didn't find any existing attributes of the same shape then create a
434 // new one and insert it.
436 PA = new AttributeSetNode(SortedAttrs);
437 pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint);
440 // Return the AttributesListNode that we found or created.
444 bool AttributeSetNode::hasAttribute(Attribute::AttrKind Kind) const {
445 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
446 E = AttrList.end(); I != E; ++I)
447 if (I->hasAttribute(Kind))
452 bool AttributeSetNode::hasAttribute(StringRef Kind) const {
453 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
454 E = AttrList.end(); I != E; ++I)
455 if (I->hasAttribute(Kind))
460 Attribute AttributeSetNode::getAttribute(Attribute::AttrKind Kind) const {
461 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
462 E = AttrList.end(); I != E; ++I)
463 if (I->hasAttribute(Kind))
468 Attribute AttributeSetNode::getAttribute(StringRef Kind) const {
469 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
470 E = AttrList.end(); I != E; ++I)
471 if (I->hasAttribute(Kind))
476 unsigned AttributeSetNode::getAlignment() const {
477 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
478 E = AttrList.end(); I != E; ++I)
479 if (I->hasAttribute(Attribute::Alignment))
480 return I->getAlignment();
484 unsigned AttributeSetNode::getStackAlignment() const {
485 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
486 E = AttrList.end(); I != E; ++I)
487 if (I->hasAttribute(Attribute::StackAlignment))
488 return I->getStackAlignment();
492 std::string AttributeSetNode::getAsString(bool InAttrGrp) const {
494 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
495 E = AttrList.end(); I != E; ++I) {
496 if (I != AttrList.begin())
498 Str += I->getAsString(InAttrGrp);
503 //===----------------------------------------------------------------------===//
504 // AttributeSetImpl Definition
505 //===----------------------------------------------------------------------===//
507 uint64_t AttributeSetImpl::Raw(unsigned Index) const {
508 for (unsigned I = 0, E = getNumAttributes(); I != E; ++I) {
509 if (getSlotIndex(I) != Index) continue;
510 const AttributeSetNode *ASN = AttrNodes[I].second;
513 for (AttributeSetNode::const_iterator II = ASN->begin(),
514 IE = ASN->end(); II != IE; ++II) {
515 Attribute Attr = *II;
517 // This cannot handle string attributes.
518 if (Attr.isStringAttribute()) continue;
520 Attribute::AttrKind Kind = Attr.getKindAsEnum();
522 if (Kind == Attribute::Alignment)
523 Mask |= (Log2_32(ASN->getAlignment()) + 1) << 16;
524 else if (Kind == Attribute::StackAlignment)
525 Mask |= (Log2_32(ASN->getStackAlignment()) + 1) << 26;
527 Mask |= AttributeImpl::getAttrMask(Kind);
536 //===----------------------------------------------------------------------===//
537 // AttributeSet Construction and Mutation Methods
538 //===----------------------------------------------------------------------===//
541 AttributeSet::getImpl(LLVMContext &C,
542 ArrayRef<std::pair<unsigned, AttributeSetNode*> > Attrs) {
543 LLVMContextImpl *pImpl = C.pImpl;
545 AttributeSetImpl::Profile(ID, Attrs);
548 AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
550 // If we didn't find any existing attributes of the same shape then
551 // create a new one and insert it.
553 PA = new AttributeSetImpl(C, Attrs);
554 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
557 // Return the AttributesList that we found or created.
558 return AttributeSet(PA);
561 AttributeSet AttributeSet::get(LLVMContext &C,
562 ArrayRef<std::pair<unsigned, Attribute> > Attrs){
563 // If there are no attributes then return a null AttributesList pointer.
565 return AttributeSet();
568 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
569 assert((!i || Attrs[i-1].first <= Attrs[i].first) &&
570 "Misordered Attributes list!");
571 assert(!Attrs[i].second.hasAttribute(Attribute::None) &&
572 "Pointless attribute!");
576 // Create a vector if (unsigned, AttributeSetNode*) pairs from the attributes
578 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrPairVec;
579 for (ArrayRef<std::pair<unsigned, Attribute> >::iterator I = Attrs.begin(),
580 E = Attrs.end(); I != E; ) {
581 unsigned Index = I->first;
582 SmallVector<Attribute, 4> AttrVec;
583 while (I != E && I->first == Index) {
584 AttrVec.push_back(I->second);
588 AttrPairVec.push_back(std::make_pair(Index,
589 AttributeSetNode::get(C, AttrVec)));
592 return getImpl(C, AttrPairVec);
595 AttributeSet AttributeSet::get(LLVMContext &C,
596 ArrayRef<std::pair<unsigned,
597 AttributeSetNode*> > Attrs) {
598 // If there are no attributes then return a null AttributesList pointer.
600 return AttributeSet();
602 return getImpl(C, Attrs);
605 AttributeSet AttributeSet::get(LLVMContext &C, unsigned Index, AttrBuilder &B) {
606 if (!B.hasAttributes())
607 return AttributeSet();
609 // Add target-independent attributes.
610 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
611 for (Attribute::AttrKind Kind = Attribute::None;
612 Kind != Attribute::EndAttrKinds; Kind = Attribute::AttrKind(Kind + 1)) {
613 if (!B.contains(Kind))
616 if (Kind == Attribute::Alignment)
617 Attrs.push_back(std::make_pair(Index, Attribute::
618 getWithAlignment(C, B.getAlignment())));
619 else if (Kind == Attribute::StackAlignment)
620 Attrs.push_back(std::make_pair(Index, Attribute::
621 getWithStackAlignment(C, B.getStackAlignment())));
623 Attrs.push_back(std::make_pair(Index, Attribute::get(C, Kind)));
626 // Add target-dependent (string) attributes.
627 for (AttrBuilder::td_iterator I = B.td_begin(), E = B.td_end();
629 Attrs.push_back(std::make_pair(Index, Attribute::get(C, I->first,I->second)));
631 return get(C, Attrs);
634 AttributeSet AttributeSet::get(LLVMContext &C, unsigned Index,
635 ArrayRef<Attribute::AttrKind> Kind) {
636 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
637 for (ArrayRef<Attribute::AttrKind>::iterator I = Kind.begin(),
638 E = Kind.end(); I != E; ++I)
639 Attrs.push_back(std::make_pair(Index, Attribute::get(C, *I)));
640 return get(C, Attrs);
643 AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<AttributeSet> Attrs) {
644 if (Attrs.empty()) return AttributeSet();
646 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrNodeVec;
647 for (unsigned I = 0, E = Attrs.size(); I != E; ++I) {
648 AttributeSet AS = Attrs[I];
649 if (!AS.pImpl) continue;
650 AttrNodeVec.append(AS.pImpl->AttrNodes.begin(), AS.pImpl->AttrNodes.end());
653 return getImpl(C, AttrNodeVec);
656 AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
657 Attribute::AttrKind Attr) const {
658 if (hasAttribute(Index, Attr)) return *this;
659 return addAttributes(C, Index, AttributeSet::get(C, Index, Attr));
662 AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
663 StringRef Kind) const {
665 B.addAttribute(Kind);
666 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
669 AttributeSet AttributeSet::addAttributes(LLVMContext &C, unsigned Index,
670 AttributeSet Attrs) const {
671 if (!pImpl) return Attrs;
672 if (!Attrs.pImpl) return *this;
675 // FIXME it is not obvious how this should work for alignment. For now, say
676 // we can't change a known alignment.
677 unsigned OldAlign = getParamAlignment(Index);
678 unsigned NewAlign = Attrs.getParamAlignment(Index);
679 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
680 "Attempt to change alignment!");
683 // Add the attribute slots before the one we're trying to add.
684 SmallVector<AttributeSet, 4> AttrSet;
685 uint64_t NumAttrs = pImpl->getNumAttributes();
687 uint64_t LastIndex = 0;
688 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
689 if (getSlotIndex(I) >= Index) {
690 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
694 AttrSet.push_back(getSlotAttributes(I));
697 // Now add the attribute into the correct slot. There may already be an
698 // AttributeSet there.
699 AttrBuilder B(AS, Index);
701 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
702 if (Attrs.getSlotIndex(I) == Index) {
703 for (AttributeSetImpl::const_iterator II = Attrs.pImpl->begin(I),
704 IE = Attrs.pImpl->end(I); II != IE; ++II)
709 AttrSet.push_back(AttributeSet::get(C, Index, B));
711 // Add the remaining attribute slots.
712 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
713 AttrSet.push_back(getSlotAttributes(I));
715 return get(C, AttrSet);
718 AttributeSet AttributeSet::removeAttribute(LLVMContext &C, unsigned Index,
719 Attribute::AttrKind Attr) const {
720 if (!hasAttribute(Index, Attr)) return *this;
721 return removeAttributes(C, Index, AttributeSet::get(C, Index, Attr));
724 AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Index,
725 AttributeSet Attrs) const {
726 if (!pImpl) return AttributeSet();
727 if (!Attrs.pImpl) return *this;
730 // FIXME it is not obvious how this should work for alignment.
731 // For now, say we can't pass in alignment, which no current use does.
732 assert(!Attrs.hasAttribute(Index, Attribute::Alignment) &&
733 "Attempt to change alignment!");
736 // Add the attribute slots before the one we're trying to add.
737 SmallVector<AttributeSet, 4> AttrSet;
738 uint64_t NumAttrs = pImpl->getNumAttributes();
740 uint64_t LastIndex = 0;
741 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
742 if (getSlotIndex(I) >= Index) {
743 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
747 AttrSet.push_back(getSlotAttributes(I));
750 // Now remove the attribute from the correct slot. There may already be an
751 // AttributeSet there.
752 AttrBuilder B(AS, Index);
754 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
755 if (Attrs.getSlotIndex(I) == Index) {
756 B.removeAttributes(Attrs.pImpl->getSlotAttributes(I), Index);
760 AttrSet.push_back(AttributeSet::get(C, Index, B));
762 // Add the remaining attribute slots.
763 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
764 AttrSet.push_back(getSlotAttributes(I));
766 return get(C, AttrSet);
769 //===----------------------------------------------------------------------===//
770 // AttributeSet Accessor Methods
771 //===----------------------------------------------------------------------===//
773 LLVMContext &AttributeSet::getContext() const {
774 return pImpl->getContext();
777 AttributeSet AttributeSet::getParamAttributes(unsigned Index) const {
778 return pImpl && hasAttributes(Index) ?
779 AttributeSet::get(pImpl->getContext(),
780 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
781 std::make_pair(Index, getAttributes(Index)))) :
785 AttributeSet AttributeSet::getRetAttributes() const {
786 return pImpl && hasAttributes(ReturnIndex) ?
787 AttributeSet::get(pImpl->getContext(),
788 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
789 std::make_pair(ReturnIndex,
790 getAttributes(ReturnIndex)))) :
794 AttributeSet AttributeSet::getFnAttributes() const {
795 return pImpl && hasAttributes(FunctionIndex) ?
796 AttributeSet::get(pImpl->getContext(),
797 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
798 std::make_pair(FunctionIndex,
799 getAttributes(FunctionIndex)))) :
803 bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{
804 AttributeSetNode *ASN = getAttributes(Index);
805 return ASN ? ASN->hasAttribute(Kind) : false;
808 bool AttributeSet::hasAttribute(unsigned Index, StringRef Kind) const {
809 AttributeSetNode *ASN = getAttributes(Index);
810 return ASN ? ASN->hasAttribute(Kind) : false;
813 bool AttributeSet::hasAttributes(unsigned Index) const {
814 AttributeSetNode *ASN = getAttributes(Index);
815 return ASN ? ASN->hasAttributes() : false;
818 /// \brief Return true if the specified attribute is set for at least one
819 /// parameter or for the return value.
820 bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr) const {
821 if (pImpl == 0) return false;
823 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
824 for (AttributeSetImpl::const_iterator II = pImpl->begin(I),
825 IE = pImpl->end(I); II != IE; ++II)
826 if (II->hasAttribute(Attr))
832 Attribute AttributeSet::getAttribute(unsigned Index,
833 Attribute::AttrKind Kind) const {
834 AttributeSetNode *ASN = getAttributes(Index);
835 return ASN ? ASN->getAttribute(Kind) : Attribute();
838 Attribute AttributeSet::getAttribute(unsigned Index,
839 StringRef Kind) const {
840 AttributeSetNode *ASN = getAttributes(Index);
841 return ASN ? ASN->getAttribute(Kind) : Attribute();
844 unsigned AttributeSet::getParamAlignment(unsigned Index) const {
845 AttributeSetNode *ASN = getAttributes(Index);
846 return ASN ? ASN->getAlignment() : 0;
849 unsigned AttributeSet::getStackAlignment(unsigned Index) const {
850 AttributeSetNode *ASN = getAttributes(Index);
851 return ASN ? ASN->getStackAlignment() : 0;
854 std::string AttributeSet::getAsString(unsigned Index,
855 bool InAttrGrp) const {
856 AttributeSetNode *ASN = getAttributes(Index);
857 return ASN ? ASN->getAsString(InAttrGrp) : std::string("");
860 /// \brief The attributes for the specified index are returned.
861 AttributeSetNode *AttributeSet::getAttributes(unsigned Index) const {
862 if (!pImpl) return 0;
864 // Loop through to find the attribute node we want.
865 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
866 if (pImpl->getSlotIndex(I) == Index)
867 return pImpl->getSlotNode(I);
872 AttributeSet::iterator AttributeSet::begin(unsigned Slot) const {
874 return ArrayRef<Attribute>().begin();
875 return pImpl->begin(Slot);
878 AttributeSet::iterator AttributeSet::end(unsigned Slot) const {
880 return ArrayRef<Attribute>().end();
881 return pImpl->end(Slot);
884 //===----------------------------------------------------------------------===//
885 // AttributeSet Introspection Methods
886 //===----------------------------------------------------------------------===//
888 /// \brief Return the number of slots used in this attribute list. This is the
889 /// number of arguments that have an attribute set on them (including the
890 /// function itself).
891 unsigned AttributeSet::getNumSlots() const {
892 return pImpl ? pImpl->getNumAttributes() : 0;
895 unsigned AttributeSet::getSlotIndex(unsigned Slot) const {
896 assert(pImpl && Slot < pImpl->getNumAttributes() &&
897 "Slot # out of range!");
898 return pImpl->getSlotIndex(Slot);
901 AttributeSet AttributeSet::getSlotAttributes(unsigned Slot) const {
902 assert(pImpl && Slot < pImpl->getNumAttributes() &&
903 "Slot # out of range!");
904 return pImpl->getSlotAttributes(Slot);
907 uint64_t AttributeSet::Raw(unsigned Index) const {
908 // FIXME: Remove this.
909 return pImpl ? pImpl->Raw(Index) : 0;
912 void AttributeSet::dump() const {
915 for (unsigned i = 0, e = getNumSlots(); i < e; ++i) {
916 uint64_t Index = getSlotIndex(i);
922 dbgs() << " => " << getAsString(Index) << " }\n";
928 //===----------------------------------------------------------------------===//
929 // AttrBuilder Method Implementations
930 //===----------------------------------------------------------------------===//
932 AttrBuilder::AttrBuilder(AttributeSet AS, unsigned Index)
933 : Attrs(0), Alignment(0), StackAlignment(0) {
934 AttributeSetImpl *pImpl = AS.pImpl;
937 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I) {
938 if (pImpl->getSlotIndex(I) != Index) continue;
940 for (AttributeSetImpl::const_iterator II = pImpl->begin(I),
941 IE = pImpl->end(I); II != IE; ++II)
948 void AttrBuilder::clear() {
950 Alignment = StackAlignment = 0;
953 AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
954 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
955 assert(Val != Attribute::Alignment && Val != Attribute::StackAlignment &&
956 "Adding alignment attribute without adding alignment value!");
961 AttrBuilder &AttrBuilder::addAttribute(Attribute Attr) {
962 if (Attr.isStringAttribute()) {
963 addAttribute(Attr.getKindAsString(), Attr.getValueAsString());
967 Attribute::AttrKind Kind = Attr.getKindAsEnum();
970 if (Kind == Attribute::Alignment)
971 Alignment = Attr.getAlignment();
972 else if (Kind == Attribute::StackAlignment)
973 StackAlignment = Attr.getStackAlignment();
977 AttrBuilder &AttrBuilder::addAttribute(StringRef A, StringRef V) {
978 TargetDepAttrs[A] = V;
982 AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
983 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
986 if (Val == Attribute::Alignment)
988 else if (Val == Attribute::StackAlignment)
994 AttrBuilder &AttrBuilder::removeAttributes(AttributeSet A, uint64_t Index) {
996 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
997 if (A.getSlotIndex(I) == Index) {
1002 assert(Slot != ~0U && "Couldn't find index in AttributeSet!");
1004 for (AttributeSet::iterator I = A.begin(Slot), E = A.end(Slot); I != E; ++I) {
1005 Attribute Attr = *I;
1006 if (Attr.isEnumAttribute() || Attr.isAlignAttribute()) {
1007 Attribute::AttrKind Kind = I->getKindAsEnum();
1008 Attrs[Kind] = false;
1010 if (Kind == Attribute::Alignment)
1012 else if (Kind == Attribute::StackAlignment)
1015 assert(Attr.isStringAttribute() && "Invalid attribute type!");
1016 std::map<std::string, std::string>::iterator
1017 Iter = TargetDepAttrs.find(Attr.getKindAsString());
1018 if (Iter != TargetDepAttrs.end())
1019 TargetDepAttrs.erase(Iter);
1026 AttrBuilder &AttrBuilder::removeAttribute(StringRef A) {
1027 std::map<std::string, std::string>::iterator I = TargetDepAttrs.find(A);
1028 if (I != TargetDepAttrs.end())
1029 TargetDepAttrs.erase(I);
1033 AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
1034 if (Align == 0) return *this;
1036 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1037 assert(Align <= 0x40000000 && "Alignment too large.");
1039 Attrs[Attribute::Alignment] = true;
1044 AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
1045 // Default alignment, allow the target to define how to align it.
1046 if (Align == 0) return *this;
1048 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1049 assert(Align <= 0x100 && "Alignment too large.");
1051 Attrs[Attribute::StackAlignment] = true;
1052 StackAlignment = Align;
1056 AttrBuilder &AttrBuilder::merge(const AttrBuilder &B) {
1057 // FIXME: What if both have alignments, but they don't match?!
1059 Alignment = B.Alignment;
1061 if (!StackAlignment)
1062 StackAlignment = B.StackAlignment;
1066 for (td_const_iterator I = B.TargetDepAttrs.begin(),
1067 E = B.TargetDepAttrs.end(); I != E; ++I)
1068 TargetDepAttrs[I->first] = I->second;
1073 bool AttrBuilder::contains(StringRef A) const {
1074 return TargetDepAttrs.find(A) != TargetDepAttrs.end();
1077 bool AttrBuilder::hasAttributes() const {
1078 return !Attrs.none() || !TargetDepAttrs.empty();
1081 bool AttrBuilder::hasAttributes(AttributeSet A, uint64_t Index) const {
1082 unsigned Slot = ~0U;
1083 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
1084 if (A.getSlotIndex(I) == Index) {
1089 assert(Slot != ~0U && "Couldn't find the index!");
1091 for (AttributeSet::iterator I = A.begin(Slot), E = A.end(Slot);
1093 Attribute Attr = *I;
1094 if (Attr.isEnumAttribute() || Attr.isAlignAttribute()) {
1095 if (Attrs[I->getKindAsEnum()])
1098 assert(Attr.isStringAttribute() && "Invalid attribute kind!");
1099 return TargetDepAttrs.find(Attr.getKindAsString())!=TargetDepAttrs.end();
1106 bool AttrBuilder::hasAlignmentAttr() const {
1107 return Alignment != 0;
1110 bool AttrBuilder::operator==(const AttrBuilder &B) {
1111 if (Attrs != B.Attrs)
1114 for (td_const_iterator I = TargetDepAttrs.begin(),
1115 E = TargetDepAttrs.end(); I != E; ++I)
1116 if (B.TargetDepAttrs.find(I->first) == B.TargetDepAttrs.end())
1119 return Alignment == B.Alignment && StackAlignment == B.StackAlignment;
1122 AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) {
1123 // FIXME: Remove this in 4.0.
1124 if (!Val) return *this;
1126 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
1127 I = Attribute::AttrKind(I + 1)) {
1128 if (uint64_t A = (Val & AttributeImpl::getAttrMask(I))) {
1131 if (I == Attribute::Alignment)
1132 Alignment = 1ULL << ((A >> 16) - 1);
1133 else if (I == Attribute::StackAlignment)
1134 StackAlignment = 1ULL << ((A >> 26)-1);
1141 //===----------------------------------------------------------------------===//
1142 // AttributeFuncs Function Defintions
1143 //===----------------------------------------------------------------------===//
1145 /// \brief Which attributes cannot be applied to a type.
1146 AttributeSet AttributeFuncs::typeIncompatible(Type *Ty, uint64_t Index) {
1147 AttrBuilder Incompatible;
1149 if (!Ty->isIntegerTy())
1150 // Attribute that only apply to integers.
1151 Incompatible.addAttribute(Attribute::SExt)
1152 .addAttribute(Attribute::ZExt);
1154 if (!Ty->isPointerTy())
1155 // Attribute that only apply to pointers.
1156 Incompatible.addAttribute(Attribute::ByVal)
1157 .addAttribute(Attribute::Nest)
1158 .addAttribute(Attribute::NoAlias)
1159 .addAttribute(Attribute::NoCapture)
1160 .addAttribute(Attribute::ReadNone)
1161 .addAttribute(Attribute::ReadOnly)
1162 .addAttribute(Attribute::StructRet);
1164 return AttributeSet::get(Ty->getContext(), Index, Incompatible);