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::ReturnsTwice))
199 return "returns_twice";
200 if (hasAttribute(Attribute::SExt))
202 if (hasAttribute(Attribute::StackProtect))
204 if (hasAttribute(Attribute::StackProtectReq))
206 if (hasAttribute(Attribute::StackProtectStrong))
208 if (hasAttribute(Attribute::StructRet))
210 if (hasAttribute(Attribute::SanitizeThread))
211 return "sanitize_thread";
212 if (hasAttribute(Attribute::SanitizeMemory))
213 return "sanitize_memory";
214 if (hasAttribute(Attribute::UWTable))
216 if (hasAttribute(Attribute::ZExt))
219 // FIXME: These should be output like this:
224 if (hasAttribute(Attribute::Alignment)) {
227 Result += (InAttrGrp) ? "=" : " ";
228 Result += utostr(getValueAsInt());
232 if (hasAttribute(Attribute::StackAlignment)) {
234 Result += "alignstack";
237 Result += utostr(getValueAsInt());
240 Result += utostr(getValueAsInt());
246 // Convert target-dependent attributes to strings of the form:
251 if (isStringAttribute()) {
253 Result += '\"' + getKindAsString().str() + '"';
255 StringRef Val = pImpl->getValueAsString();
256 if (Val.empty()) return Result;
258 Result += "=\"" + Val.str() + '"';
262 llvm_unreachable("Unknown attribute");
265 bool Attribute::operator<(Attribute A) const {
266 if (!pImpl && !A.pImpl) return false;
267 if (!pImpl) return true;
268 if (!A.pImpl) return false;
269 return *pImpl < *A.pImpl;
272 //===----------------------------------------------------------------------===//
273 // AttributeImpl Definition
274 //===----------------------------------------------------------------------===//
276 AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind Kind)
277 : Context(C), Entry(new EnumAttributeEntry(Kind)) {}
279 AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind Kind,
282 assert((Kind == Attribute::Alignment || Kind == Attribute::StackAlignment) &&
283 "Wrong kind for alignment attribute!");
284 Entry = new AlignAttributeEntry(Kind, Align);
287 AttributeImpl::AttributeImpl(LLVMContext &C, StringRef Kind, StringRef Val)
288 : Context(C), Entry(new StringAttributeEntry(Kind, Val)) {}
290 AttributeImpl::~AttributeImpl() {
294 bool AttributeImpl::isEnumAttribute() const {
295 return isa<EnumAttributeEntry>(Entry);
298 bool AttributeImpl::isAlignAttribute() const {
299 return isa<AlignAttributeEntry>(Entry);
302 bool AttributeImpl::isStringAttribute() const {
303 return isa<StringAttributeEntry>(Entry);
306 bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
307 if (isStringAttribute()) return false;
308 return getKindAsEnum() == A;
311 bool AttributeImpl::hasAttribute(StringRef Kind) const {
312 if (!isStringAttribute()) return false;
313 return getKindAsString() == Kind;
316 Attribute::AttrKind AttributeImpl::getKindAsEnum() const {
317 if (EnumAttributeEntry *E = dyn_cast<EnumAttributeEntry>(Entry))
318 return E->getEnumKind();
319 return cast<AlignAttributeEntry>(Entry)->getEnumKind();
322 uint64_t AttributeImpl::getValueAsInt() const {
323 return cast<AlignAttributeEntry>(Entry)->getAlignment();
326 StringRef AttributeImpl::getKindAsString() const {
327 return cast<StringAttributeEntry>(Entry)->getStringKind();
330 StringRef AttributeImpl::getValueAsString() const {
331 return cast<StringAttributeEntry>(Entry)->getStringValue();
334 bool AttributeImpl::operator<(const AttributeImpl &AI) const {
335 // This sorts the attributes with Attribute::AttrKinds coming first (sorted
336 // relative to their enum value) and then strings.
337 if (isEnumAttribute()) {
338 if (AI.isEnumAttribute()) return getKindAsEnum() < AI.getKindAsEnum();
339 if (AI.isAlignAttribute()) return true;
340 if (AI.isStringAttribute()) return true;
343 if (isAlignAttribute()) {
344 if (AI.isEnumAttribute()) return false;
345 if (AI.isAlignAttribute()) return getValueAsInt() < AI.getValueAsInt();
346 if (AI.isStringAttribute()) return true;
349 if (AI.isEnumAttribute()) return false;
350 if (AI.isAlignAttribute()) return false;
351 if (getKindAsString() == AI.getKindAsString())
352 return getValueAsString() < AI.getValueAsString();
353 return getKindAsString() < AI.getKindAsString();
356 uint64_t AttributeImpl::getAttrMask(Attribute::AttrKind Val) {
357 // FIXME: Remove this.
359 case Attribute::EndAttrKinds:
360 llvm_unreachable("Synthetic enumerators which should never get here");
362 case Attribute::None: return 0;
363 case Attribute::ZExt: return 1 << 0;
364 case Attribute::SExt: return 1 << 1;
365 case Attribute::NoReturn: return 1 << 2;
366 case Attribute::InReg: return 1 << 3;
367 case Attribute::StructRet: return 1 << 4;
368 case Attribute::NoUnwind: return 1 << 5;
369 case Attribute::NoAlias: return 1 << 6;
370 case Attribute::ByVal: return 1 << 7;
371 case Attribute::Nest: return 1 << 8;
372 case Attribute::ReadNone: return 1 << 9;
373 case Attribute::ReadOnly: return 1 << 10;
374 case Attribute::NoInline: return 1 << 11;
375 case Attribute::AlwaysInline: return 1 << 12;
376 case Attribute::OptimizeForSize: return 1 << 13;
377 case Attribute::StackProtect: return 1 << 14;
378 case Attribute::StackProtectReq: return 1 << 15;
379 case Attribute::Alignment: return 31 << 16;
380 case Attribute::NoCapture: return 1 << 21;
381 case Attribute::NoRedZone: return 1 << 22;
382 case Attribute::NoImplicitFloat: return 1 << 23;
383 case Attribute::Naked: return 1 << 24;
384 case Attribute::InlineHint: return 1 << 25;
385 case Attribute::StackAlignment: return 7 << 26;
386 case Attribute::ReturnsTwice: return 1 << 29;
387 case Attribute::UWTable: return 1 << 30;
388 case Attribute::NonLazyBind: return 1U << 31;
389 case Attribute::SanitizeAddress: return 1ULL << 32;
390 case Attribute::MinSize: return 1ULL << 33;
391 case Attribute::NoDuplicate: return 1ULL << 34;
392 case Attribute::StackProtectStrong: return 1ULL << 35;
393 case Attribute::SanitizeThread: return 1ULL << 36;
394 case Attribute::SanitizeMemory: return 1ULL << 37;
395 case Attribute::NoBuiltin: return 1ULL << 38;
397 llvm_unreachable("Unsupported attribute type");
400 //===----------------------------------------------------------------------===//
401 // AttributeSetNode Definition
402 //===----------------------------------------------------------------------===//
404 AttributeSetNode *AttributeSetNode::get(LLVMContext &C,
405 ArrayRef<Attribute> Attrs) {
409 // Otherwise, build a key to look up the existing attributes.
410 LLVMContextImpl *pImpl = C.pImpl;
413 SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end());
414 array_pod_sort(SortedAttrs.begin(), SortedAttrs.end());
416 for (SmallVectorImpl<Attribute>::iterator I = SortedAttrs.begin(),
417 E = SortedAttrs.end(); I != E; ++I)
421 AttributeSetNode *PA =
422 pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint);
424 // If we didn't find any existing attributes of the same shape then create a
425 // new one and insert it.
427 PA = new AttributeSetNode(SortedAttrs);
428 pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint);
431 // Return the AttributesListNode that we found or created.
435 bool AttributeSetNode::hasAttribute(Attribute::AttrKind Kind) const {
436 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
437 E = AttrList.end(); I != E; ++I)
438 if (I->hasAttribute(Kind))
443 bool AttributeSetNode::hasAttribute(StringRef Kind) const {
444 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
445 E = AttrList.end(); I != E; ++I)
446 if (I->hasAttribute(Kind))
451 Attribute AttributeSetNode::getAttribute(Attribute::AttrKind Kind) const {
452 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
453 E = AttrList.end(); I != E; ++I)
454 if (I->hasAttribute(Kind))
459 Attribute AttributeSetNode::getAttribute(StringRef Kind) const {
460 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
461 E = AttrList.end(); I != E; ++I)
462 if (I->hasAttribute(Kind))
467 unsigned AttributeSetNode::getAlignment() const {
468 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
469 E = AttrList.end(); I != E; ++I)
470 if (I->hasAttribute(Attribute::Alignment))
471 return I->getAlignment();
475 unsigned AttributeSetNode::getStackAlignment() const {
476 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
477 E = AttrList.end(); I != E; ++I)
478 if (I->hasAttribute(Attribute::StackAlignment))
479 return I->getStackAlignment();
483 std::string AttributeSetNode::getAsString(bool TargetIndependent,
484 bool InAttrGrp) const {
486 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
487 E = AttrList.end(); I != E; ++I) {
488 if (TargetIndependent || !I->isStringAttribute()) {
489 if (I != AttrList.begin())
491 Str += I->getAsString(InAttrGrp);
497 //===----------------------------------------------------------------------===//
498 // AttributeSetImpl Definition
499 //===----------------------------------------------------------------------===//
501 uint64_t AttributeSetImpl::Raw(uint64_t Index) const {
502 for (unsigned I = 0, E = getNumAttributes(); I != E; ++I) {
503 if (getSlotIndex(I) != Index) continue;
504 const AttributeSetNode *ASN = AttrNodes[I].second;
507 for (AttributeSetNode::const_iterator II = ASN->begin(),
508 IE = ASN->end(); II != IE; ++II) {
509 Attribute Attr = *II;
511 // This cannot handle string attributes.
512 if (Attr.isStringAttribute()) continue;
514 Attribute::AttrKind Kind = Attr.getKindAsEnum();
516 if (Kind == Attribute::Alignment)
517 Mask |= (Log2_32(ASN->getAlignment()) + 1) << 16;
518 else if (Kind == Attribute::StackAlignment)
519 Mask |= (Log2_32(ASN->getStackAlignment()) + 1) << 26;
521 Mask |= AttributeImpl::getAttrMask(Kind);
530 //===----------------------------------------------------------------------===//
531 // AttributeSet Construction and Mutation Methods
532 //===----------------------------------------------------------------------===//
535 AttributeSet::getImpl(LLVMContext &C,
536 ArrayRef<std::pair<unsigned, AttributeSetNode*> > Attrs) {
537 LLVMContextImpl *pImpl = C.pImpl;
539 AttributeSetImpl::Profile(ID, Attrs);
542 AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
544 // If we didn't find any existing attributes of the same shape then
545 // create a new one and insert it.
547 PA = new AttributeSetImpl(C, Attrs);
548 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
551 // Return the AttributesList that we found or created.
552 return AttributeSet(PA);
555 AttributeSet AttributeSet::get(LLVMContext &C,
556 ArrayRef<std::pair<unsigned, Attribute> > Attrs){
557 // If there are no attributes then return a null AttributesList pointer.
559 return AttributeSet();
562 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
563 assert((!i || Attrs[i-1].first <= Attrs[i].first) &&
564 "Misordered Attributes list!");
565 assert(!Attrs[i].second.hasAttribute(Attribute::None) &&
566 "Pointless attribute!");
570 // Create a vector if (unsigned, AttributeSetNode*) pairs from the attributes
572 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrPairVec;
573 for (ArrayRef<std::pair<unsigned, Attribute> >::iterator I = Attrs.begin(),
574 E = Attrs.end(); I != E; ) {
575 unsigned Index = I->first;
576 SmallVector<Attribute, 4> AttrVec;
577 while (I != E && I->first == Index) {
578 AttrVec.push_back(I->second);
582 AttrPairVec.push_back(std::make_pair(Index,
583 AttributeSetNode::get(C, AttrVec)));
586 return getImpl(C, AttrPairVec);
589 AttributeSet AttributeSet::get(LLVMContext &C,
590 ArrayRef<std::pair<unsigned,
591 AttributeSetNode*> > Attrs) {
592 // If there are no attributes then return a null AttributesList pointer.
594 return AttributeSet();
596 return getImpl(C, Attrs);
599 AttributeSet AttributeSet::get(LLVMContext &C, unsigned Index, AttrBuilder &B) {
600 if (!B.hasAttributes())
601 return AttributeSet();
603 // Add target-independent attributes.
604 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
605 for (Attribute::AttrKind Kind = Attribute::None;
606 Kind != Attribute::EndAttrKinds; Kind = Attribute::AttrKind(Kind + 1)) {
607 if (!B.contains(Kind))
610 if (Kind == Attribute::Alignment)
611 Attrs.push_back(std::make_pair(Index, Attribute::
612 getWithAlignment(C, B.getAlignment())));
613 else if (Kind == Attribute::StackAlignment)
614 Attrs.push_back(std::make_pair(Index, Attribute::
615 getWithStackAlignment(C, B.getStackAlignment())));
617 Attrs.push_back(std::make_pair(Index, Attribute::get(C, Kind)));
620 // Add target-dependent (string) attributes.
621 for (AttrBuilder::td_iterator I = B.td_begin(), E = B.td_end();
623 Attrs.push_back(std::make_pair(Index, Attribute::get(C, I->first,I->second)));
625 return get(C, Attrs);
628 AttributeSet AttributeSet::get(LLVMContext &C, unsigned Index,
629 ArrayRef<Attribute::AttrKind> Kind) {
630 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
631 for (ArrayRef<Attribute::AttrKind>::iterator I = Kind.begin(),
632 E = Kind.end(); I != E; ++I)
633 Attrs.push_back(std::make_pair(Index, Attribute::get(C, *I)));
634 return get(C, Attrs);
637 AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<AttributeSet> Attrs) {
638 if (Attrs.empty()) return AttributeSet();
640 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrNodeVec;
641 for (unsigned I = 0, E = Attrs.size(); I != E; ++I) {
642 AttributeSet AS = Attrs[I];
643 if (!AS.pImpl) continue;
644 AttrNodeVec.append(AS.pImpl->AttrNodes.begin(), AS.pImpl->AttrNodes.end());
647 return getImpl(C, AttrNodeVec);
650 AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
651 Attribute::AttrKind Attr) const {
652 if (hasAttribute(Index, Attr)) return *this;
653 return addAttributes(C, Index, AttributeSet::get(C, Index, Attr));
656 AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
657 StringRef Kind) const {
659 B.addAttribute(Kind);
660 return addAttributes(C, Index, AttributeSet::get(C, Index, B));
663 AttributeSet AttributeSet::addAttributes(LLVMContext &C, unsigned Index,
664 AttributeSet Attrs) const {
665 if (!pImpl) return Attrs;
666 if (!Attrs.pImpl) return *this;
669 // FIXME it is not obvious how this should work for alignment. For now, say
670 // we can't change a known alignment.
671 unsigned OldAlign = getParamAlignment(Index);
672 unsigned NewAlign = Attrs.getParamAlignment(Index);
673 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
674 "Attempt to change alignment!");
677 // Add the attribute slots before the one we're trying to add.
678 SmallVector<AttributeSet, 4> AttrSet;
679 uint64_t NumAttrs = pImpl->getNumAttributes();
681 uint64_t LastIndex = 0;
682 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
683 if (getSlotIndex(I) >= Index) {
684 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
688 AttrSet.push_back(getSlotAttributes(I));
691 // Now add the attribute into the correct slot. There may already be an
692 // AttributeSet there.
693 AttrBuilder B(AS, Index);
695 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
696 if (Attrs.getSlotIndex(I) == Index) {
697 for (AttributeSetImpl::const_iterator II = Attrs.pImpl->begin(I),
698 IE = Attrs.pImpl->end(I); II != IE; ++II)
703 AttrSet.push_back(AttributeSet::get(C, Index, B));
705 // Add the remaining attribute slots.
706 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
707 AttrSet.push_back(getSlotAttributes(I));
709 return get(C, AttrSet);
712 AttributeSet AttributeSet::removeAttribute(LLVMContext &C, unsigned Index,
713 Attribute::AttrKind Attr) const {
714 if (!hasAttribute(Index, Attr)) return *this;
715 return removeAttributes(C, Index, AttributeSet::get(C, Index, Attr));
718 AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Index,
719 AttributeSet Attrs) const {
720 if (!pImpl) return AttributeSet();
721 if (!Attrs.pImpl) return *this;
724 // FIXME it is not obvious how this should work for alignment.
725 // For now, say we can't pass in alignment, which no current use does.
726 assert(!Attrs.hasAttribute(Index, Attribute::Alignment) &&
727 "Attempt to change alignment!");
730 // Add the attribute slots before the one we're trying to add.
731 SmallVector<AttributeSet, 4> AttrSet;
732 uint64_t NumAttrs = pImpl->getNumAttributes();
734 uint64_t LastIndex = 0;
735 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
736 if (getSlotIndex(I) >= Index) {
737 if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
741 AttrSet.push_back(getSlotAttributes(I));
744 // Now remove the attribute from the correct slot. There may already be an
745 // AttributeSet there.
746 AttrBuilder B(AS, Index);
748 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
749 if (Attrs.getSlotIndex(I) == Index) {
750 B.removeAttributes(Attrs.pImpl->getSlotAttributes(I), Index);
754 AttrSet.push_back(AttributeSet::get(C, Index, B));
756 // Add the remaining attribute slots.
757 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
758 AttrSet.push_back(getSlotAttributes(I));
760 return get(C, AttrSet);
763 //===----------------------------------------------------------------------===//
764 // AttributeSet Accessor Methods
765 //===----------------------------------------------------------------------===//
767 LLVMContext &AttributeSet::getContext() const {
768 return pImpl->getContext();
771 AttributeSet AttributeSet::getParamAttributes(unsigned Index) const {
772 return pImpl && hasAttributes(Index) ?
773 AttributeSet::get(pImpl->getContext(),
774 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
775 std::make_pair(Index, getAttributes(Index)))) :
779 AttributeSet AttributeSet::getRetAttributes() const {
780 return pImpl && hasAttributes(ReturnIndex) ?
781 AttributeSet::get(pImpl->getContext(),
782 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
783 std::make_pair(ReturnIndex,
784 getAttributes(ReturnIndex)))) :
788 AttributeSet AttributeSet::getFnAttributes() const {
789 return pImpl && hasAttributes(FunctionIndex) ?
790 AttributeSet::get(pImpl->getContext(),
791 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
792 std::make_pair(FunctionIndex,
793 getAttributes(FunctionIndex)))) :
797 bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{
798 AttributeSetNode *ASN = getAttributes(Index);
799 return ASN ? ASN->hasAttribute(Kind) : false;
802 bool AttributeSet::hasAttribute(unsigned Index, StringRef Kind) const {
803 AttributeSetNode *ASN = getAttributes(Index);
804 return ASN ? ASN->hasAttribute(Kind) : false;
807 bool AttributeSet::hasAttributes(unsigned Index) const {
808 AttributeSetNode *ASN = getAttributes(Index);
809 return ASN ? ASN->hasAttributes() : false;
812 /// \brief Return true if the specified attribute is set for at least one
813 /// parameter or for the return value.
814 bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr) const {
815 if (pImpl == 0) return false;
817 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
818 for (AttributeSetImpl::const_iterator II = pImpl->begin(I),
819 IE = pImpl->end(I); II != IE; ++II)
820 if (II->hasAttribute(Attr))
826 Attribute AttributeSet::getAttribute(unsigned Index,
827 Attribute::AttrKind Kind) const {
828 AttributeSetNode *ASN = getAttributes(Index);
829 return ASN ? ASN->getAttribute(Kind) : Attribute();
832 Attribute AttributeSet::getAttribute(unsigned Index,
833 StringRef Kind) const {
834 AttributeSetNode *ASN = getAttributes(Index);
835 return ASN ? ASN->getAttribute(Kind) : Attribute();
838 unsigned AttributeSet::getParamAlignment(unsigned Index) const {
839 AttributeSetNode *ASN = getAttributes(Index);
840 return ASN ? ASN->getAlignment() : 0;
843 unsigned AttributeSet::getStackAlignment(unsigned Index) const {
844 AttributeSetNode *ASN = getAttributes(Index);
845 return ASN ? ASN->getStackAlignment() : 0;
848 std::string AttributeSet::getAsString(unsigned Index, bool TargetIndependent,
849 bool InAttrGrp) const {
850 AttributeSetNode *ASN = getAttributes(Index);
851 return ASN ? ASN->getAsString(TargetIndependent, InAttrGrp) :
855 /// \brief The attributes for the specified index are returned.
856 AttributeSetNode *AttributeSet::getAttributes(unsigned Index) const {
857 if (!pImpl) return 0;
859 // Loop through to find the attribute node we want.
860 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
861 if (pImpl->getSlotIndex(I) == Index)
862 return pImpl->getSlotNode(I);
867 AttributeSet::iterator AttributeSet::begin(unsigned Slot) const {
869 return ArrayRef<Attribute>().begin();
870 return pImpl->begin(Slot);
873 AttributeSet::iterator AttributeSet::end(unsigned Slot) const {
875 return ArrayRef<Attribute>().end();
876 return pImpl->end(Slot);
879 //===----------------------------------------------------------------------===//
880 // AttributeSet Introspection Methods
881 //===----------------------------------------------------------------------===//
883 /// \brief Return the number of slots used in this attribute list. This is the
884 /// number of arguments that have an attribute set on them (including the
885 /// function itself).
886 unsigned AttributeSet::getNumSlots() const {
887 return pImpl ? pImpl->getNumAttributes() : 0;
890 uint64_t AttributeSet::getSlotIndex(unsigned Slot) const {
891 assert(pImpl && Slot < pImpl->getNumAttributes() &&
892 "Slot # out of range!");
893 return pImpl->getSlotIndex(Slot);
896 AttributeSet AttributeSet::getSlotAttributes(unsigned Slot) const {
897 assert(pImpl && Slot < pImpl->getNumAttributes() &&
898 "Slot # out of range!");
899 return pImpl->getSlotAttributes(Slot);
902 uint64_t AttributeSet::Raw(unsigned Index) const {
903 // FIXME: Remove this.
904 return pImpl ? pImpl->Raw(Index) : 0;
907 void AttributeSet::dump() const {
910 for (unsigned i = 0, e = getNumSlots(); i < e; ++i) {
911 uint64_t Index = getSlotIndex(i);
917 dbgs() << " => " << getAsString(Index) << " }\n";
923 //===----------------------------------------------------------------------===//
924 // AttrBuilder Method Implementations
925 //===----------------------------------------------------------------------===//
927 AttrBuilder::AttrBuilder(AttributeSet AS, unsigned Index)
928 : Attrs(0), Alignment(0), StackAlignment(0) {
929 AttributeSetImpl *pImpl = AS.pImpl;
932 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I) {
933 if (pImpl->getSlotIndex(I) != Index) continue;
935 for (AttributeSetImpl::const_iterator II = pImpl->begin(I),
936 IE = pImpl->end(I); II != IE; ++II)
943 void AttrBuilder::clear() {
945 Alignment = StackAlignment = 0;
948 AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
949 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
950 assert(Val != Attribute::Alignment && Val != Attribute::StackAlignment &&
951 "Adding alignment attribute without adding alignment value!");
956 AttrBuilder &AttrBuilder::addAttribute(Attribute Attr) {
957 if (Attr.isStringAttribute()) {
958 addAttribute(Attr.getKindAsString(), Attr.getValueAsString());
962 Attribute::AttrKind Kind = Attr.getKindAsEnum();
965 if (Kind == Attribute::Alignment)
966 Alignment = Attr.getAlignment();
967 else if (Kind == Attribute::StackAlignment)
968 StackAlignment = Attr.getStackAlignment();
972 AttrBuilder &AttrBuilder::addAttribute(StringRef A, StringRef V) {
973 TargetDepAttrs[A] = V;
977 AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
978 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
981 if (Val == Attribute::Alignment)
983 else if (Val == Attribute::StackAlignment)
989 AttrBuilder &AttrBuilder::removeAttributes(AttributeSet A, uint64_t Index) {
991 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
992 if (A.getSlotIndex(I) == Index) {
997 assert(Slot != ~0U && "Couldn't find index in AttributeSet!");
999 for (AttributeSet::iterator I = A.begin(Slot), E = A.end(Slot); I != E; ++I) {
1000 Attribute Attr = *I;
1001 if (Attr.isEnumAttribute() || Attr.isAlignAttribute()) {
1002 Attribute::AttrKind Kind = I->getKindAsEnum();
1003 Attrs[Kind] = false;
1005 if (Kind == Attribute::Alignment)
1007 else if (Kind == Attribute::StackAlignment)
1010 assert(Attr.isStringAttribute() && "Invalid attribute type!");
1011 std::map<std::string, std::string>::iterator
1012 Iter = TargetDepAttrs.find(Attr.getKindAsString());
1013 if (Iter != TargetDepAttrs.end())
1014 TargetDepAttrs.erase(Iter);
1021 AttrBuilder &AttrBuilder::removeAttribute(StringRef A) {
1022 std::map<std::string, std::string>::iterator I = TargetDepAttrs.find(A);
1023 if (I != TargetDepAttrs.end())
1024 TargetDepAttrs.erase(I);
1028 AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
1029 if (Align == 0) return *this;
1031 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1032 assert(Align <= 0x40000000 && "Alignment too large.");
1034 Attrs[Attribute::Alignment] = true;
1039 AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
1040 // Default alignment, allow the target to define how to align it.
1041 if (Align == 0) return *this;
1043 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1044 assert(Align <= 0x100 && "Alignment too large.");
1046 Attrs[Attribute::StackAlignment] = true;
1047 StackAlignment = Align;
1051 AttrBuilder &AttrBuilder::merge(const AttrBuilder &B) {
1052 // FIXME: What if both have alignments, but they don't match?!
1054 Alignment = B.Alignment;
1056 if (!StackAlignment)
1057 StackAlignment = B.StackAlignment;
1061 for (td_const_iterator I = B.TargetDepAttrs.begin(),
1062 E = B.TargetDepAttrs.end(); I != E; ++I)
1063 TargetDepAttrs[I->first] = I->second;
1068 bool AttrBuilder::contains(StringRef A) const {
1069 return TargetDepAttrs.find(A) != TargetDepAttrs.end();
1072 bool AttrBuilder::hasAttributes() const {
1073 return !Attrs.none() || !TargetDepAttrs.empty();
1076 bool AttrBuilder::hasAttributes(AttributeSet A, uint64_t Index) const {
1077 unsigned Slot = ~0U;
1078 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
1079 if (A.getSlotIndex(I) == Index) {
1084 assert(Slot != ~0U && "Couldn't find the index!");
1086 for (AttributeSet::iterator I = A.begin(Slot), E = A.end(Slot);
1088 Attribute Attr = *I;
1089 if (Attr.isEnumAttribute() || Attr.isAlignAttribute()) {
1090 if (Attrs[I->getKindAsEnum()])
1093 assert(Attr.isStringAttribute() && "Invalid attribute kind!");
1094 return TargetDepAttrs.find(Attr.getKindAsString())!=TargetDepAttrs.end();
1101 bool AttrBuilder::hasAlignmentAttr() const {
1102 return Alignment != 0;
1105 bool AttrBuilder::operator==(const AttrBuilder &B) {
1106 if (Attrs != B.Attrs)
1109 for (td_const_iterator I = TargetDepAttrs.begin(),
1110 E = TargetDepAttrs.end(); I != E; ++I)
1111 if (B.TargetDepAttrs.find(I->first) == B.TargetDepAttrs.end())
1114 return Alignment == B.Alignment && StackAlignment == B.StackAlignment;
1117 AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) {
1118 // FIXME: Remove this in 4.0.
1119 if (!Val) return *this;
1121 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
1122 I = Attribute::AttrKind(I + 1)) {
1123 if (uint64_t A = (Val & AttributeImpl::getAttrMask(I))) {
1126 if (I == Attribute::Alignment)
1127 Alignment = 1ULL << ((A >> 16) - 1);
1128 else if (I == Attribute::StackAlignment)
1129 StackAlignment = 1ULL << ((A >> 26)-1);
1136 //===----------------------------------------------------------------------===//
1137 // AttributeFuncs Function Defintions
1138 //===----------------------------------------------------------------------===//
1140 /// \brief Which attributes cannot be applied to a type.
1141 AttributeSet AttributeFuncs::typeIncompatible(Type *Ty, uint64_t Index) {
1142 AttrBuilder Incompatible;
1144 if (!Ty->isIntegerTy())
1145 // Attribute that only apply to integers.
1146 Incompatible.addAttribute(Attribute::SExt)
1147 .addAttribute(Attribute::ZExt);
1149 if (!Ty->isPointerTy())
1150 // Attribute that only apply to pointers.
1151 Incompatible.addAttribute(Attribute::ByVal)
1152 .addAttribute(Attribute::Nest)
1153 .addAttribute(Attribute::NoAlias)
1154 .addAttribute(Attribute::NoCapture)
1155 .addAttribute(Attribute::StructRet);
1157 return AttributeSet::get(Ty->getContext(), Index, Incompatible);