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 // AttributeListImpl, and AttributeList classes.
14 //===----------------------------------------------------------------------===//
16 #include "AttributeImpl.h"
17 #include "LLVMContextImpl.h"
18 #include "llvm/ADT/ArrayRef.h"
19 #include "llvm/ADT/FoldingSet.h"
20 #include "llvm/ADT/Optional.h"
21 #include "llvm/ADT/SmallVector.h"
22 #include "llvm/ADT/STLExtras.h"
23 #include "llvm/ADT/StringExtras.h"
24 #include "llvm/ADT/StringRef.h"
25 #include "llvm/ADT/Twine.h"
26 #include "llvm/IR/Attributes.h"
27 #include "llvm/IR/Function.h"
28 #include "llvm/IR/LLVMContext.h"
29 #include "llvm/IR/Type.h"
30 #include "llvm/Support/Compiler.h"
31 #include "llvm/Support/Debug.h"
32 #include "llvm/Support/ErrorHandling.h"
33 #include "llvm/Support/MathExtras.h"
34 #include "llvm/Support/raw_ostream.h"
48 //===----------------------------------------------------------------------===//
49 // Attribute Construction Methods
50 //===----------------------------------------------------------------------===//
52 // allocsize has two integer arguments, but because they're both 32 bits, we can
53 // pack them into one 64-bit value, at the cost of making said value
56 // In order to do this, we need to reserve one value of the second (optional)
57 // allocsize argument to signify "not present."
58 static const unsigned AllocSizeNumElemsNotPresent = -1;
60 static uint64_t packAllocSizeArgs(unsigned ElemSizeArg,
61 const Optional<unsigned> &NumElemsArg) {
62 assert((!NumElemsArg.hasValue() ||
63 *NumElemsArg != AllocSizeNumElemsNotPresent) &&
64 "Attempting to pack a reserved value");
66 return uint64_t(ElemSizeArg) << 32 |
67 NumElemsArg.getValueOr(AllocSizeNumElemsNotPresent);
70 static std::pair<unsigned, Optional<unsigned>>
71 unpackAllocSizeArgs(uint64_t Num) {
72 unsigned NumElems = Num & std::numeric_limits<unsigned>::max();
73 unsigned ElemSizeArg = Num >> 32;
75 Optional<unsigned> NumElemsArg;
76 if (NumElems != AllocSizeNumElemsNotPresent)
77 NumElemsArg = NumElems;
78 return std::make_pair(ElemSizeArg, NumElemsArg);
81 Attribute Attribute::get(LLVMContext &Context, Attribute::AttrKind Kind,
83 LLVMContextImpl *pImpl = Context.pImpl;
86 if (Val) ID.AddInteger(Val);
89 AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
92 // If we didn't find any existing attributes of the same shape then create a
93 // new one and insert it.
95 PA = new EnumAttributeImpl(Kind);
97 PA = new IntAttributeImpl(Kind, Val);
98 pImpl->AttrsSet.InsertNode(PA, InsertPoint);
101 // Return the Attribute that we found or created.
102 return Attribute(PA);
105 Attribute Attribute::get(LLVMContext &Context, StringRef Kind, StringRef Val) {
106 LLVMContextImpl *pImpl = Context.pImpl;
109 if (!Val.empty()) ID.AddString(Val);
112 AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
115 // If we didn't find any existing attributes of the same shape then create a
116 // new one and insert it.
117 PA = new StringAttributeImpl(Kind, Val);
118 pImpl->AttrsSet.InsertNode(PA, InsertPoint);
121 // Return the Attribute that we found or created.
122 return Attribute(PA);
125 Attribute Attribute::getWithAlignment(LLVMContext &Context, uint64_t Align) {
126 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
127 assert(Align <= 0x40000000 && "Alignment too large.");
128 return get(Context, Alignment, Align);
131 Attribute Attribute::getWithStackAlignment(LLVMContext &Context,
133 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
134 assert(Align <= 0x100 && "Alignment too large.");
135 return get(Context, StackAlignment, Align);
138 Attribute Attribute::getWithDereferenceableBytes(LLVMContext &Context,
140 assert(Bytes && "Bytes must be non-zero.");
141 return get(Context, Dereferenceable, Bytes);
144 Attribute Attribute::getWithDereferenceableOrNullBytes(LLVMContext &Context,
146 assert(Bytes && "Bytes must be non-zero.");
147 return get(Context, DereferenceableOrNull, Bytes);
151 Attribute::getWithAllocSizeArgs(LLVMContext &Context, unsigned ElemSizeArg,
152 const Optional<unsigned> &NumElemsArg) {
153 assert(!(ElemSizeArg == 0 && NumElemsArg && *NumElemsArg == 0) &&
154 "Invalid allocsize arguments -- given allocsize(0, 0)");
155 return get(Context, AllocSize, packAllocSizeArgs(ElemSizeArg, NumElemsArg));
158 //===----------------------------------------------------------------------===//
159 // Attribute Accessor Methods
160 //===----------------------------------------------------------------------===//
162 bool Attribute::isEnumAttribute() const {
163 return pImpl && pImpl->isEnumAttribute();
166 bool Attribute::isIntAttribute() const {
167 return pImpl && pImpl->isIntAttribute();
170 bool Attribute::isStringAttribute() const {
171 return pImpl && pImpl->isStringAttribute();
174 Attribute::AttrKind Attribute::getKindAsEnum() const {
175 if (!pImpl) return None;
176 assert((isEnumAttribute() || isIntAttribute()) &&
177 "Invalid attribute type to get the kind as an enum!");
178 return pImpl->getKindAsEnum();
181 uint64_t Attribute::getValueAsInt() const {
182 if (!pImpl) return 0;
183 assert(isIntAttribute() &&
184 "Expected the attribute to be an integer attribute!");
185 return pImpl->getValueAsInt();
188 StringRef Attribute::getKindAsString() const {
189 if (!pImpl) return StringRef();
190 assert(isStringAttribute() &&
191 "Invalid attribute type to get the kind as a string!");
192 return pImpl->getKindAsString();
195 StringRef Attribute::getValueAsString() const {
196 if (!pImpl) return StringRef();
197 assert(isStringAttribute() &&
198 "Invalid attribute type to get the value as a string!");
199 return pImpl->getValueAsString();
202 bool Attribute::hasAttribute(AttrKind Kind) const {
203 return (pImpl && pImpl->hasAttribute(Kind)) || (!pImpl && Kind == None);
206 bool Attribute::hasAttribute(StringRef Kind) const {
207 if (!isStringAttribute()) return false;
208 return pImpl && pImpl->hasAttribute(Kind);
211 unsigned Attribute::getAlignment() const {
212 assert(hasAttribute(Attribute::Alignment) &&
213 "Trying to get alignment from non-alignment attribute!");
214 return pImpl->getValueAsInt();
217 unsigned Attribute::getStackAlignment() const {
218 assert(hasAttribute(Attribute::StackAlignment) &&
219 "Trying to get alignment from non-alignment attribute!");
220 return pImpl->getValueAsInt();
223 uint64_t Attribute::getDereferenceableBytes() const {
224 assert(hasAttribute(Attribute::Dereferenceable) &&
225 "Trying to get dereferenceable bytes from "
226 "non-dereferenceable attribute!");
227 return pImpl->getValueAsInt();
230 uint64_t Attribute::getDereferenceableOrNullBytes() const {
231 assert(hasAttribute(Attribute::DereferenceableOrNull) &&
232 "Trying to get dereferenceable bytes from "
233 "non-dereferenceable attribute!");
234 return pImpl->getValueAsInt();
237 std::pair<unsigned, Optional<unsigned>> Attribute::getAllocSizeArgs() const {
238 assert(hasAttribute(Attribute::AllocSize) &&
239 "Trying to get allocsize args from non-allocsize attribute");
240 return unpackAllocSizeArgs(pImpl->getValueAsInt());
243 std::string Attribute::getAsString(bool InAttrGrp) const {
244 if (!pImpl) return "";
246 if (hasAttribute(Attribute::SanitizeAddress))
247 return "sanitize_address";
248 if (hasAttribute(Attribute::AlwaysInline))
249 return "alwaysinline";
250 if (hasAttribute(Attribute::ArgMemOnly))
252 if (hasAttribute(Attribute::Builtin))
254 if (hasAttribute(Attribute::ByVal))
256 if (hasAttribute(Attribute::Convergent))
258 if (hasAttribute(Attribute::SwiftError))
260 if (hasAttribute(Attribute::SwiftSelf))
262 if (hasAttribute(Attribute::InaccessibleMemOnly))
263 return "inaccessiblememonly";
264 if (hasAttribute(Attribute::InaccessibleMemOrArgMemOnly))
265 return "inaccessiblemem_or_argmemonly";
266 if (hasAttribute(Attribute::InAlloca))
268 if (hasAttribute(Attribute::InlineHint))
270 if (hasAttribute(Attribute::InReg))
272 if (hasAttribute(Attribute::JumpTable))
274 if (hasAttribute(Attribute::MinSize))
276 if (hasAttribute(Attribute::Naked))
278 if (hasAttribute(Attribute::Nest))
280 if (hasAttribute(Attribute::NoAlias))
282 if (hasAttribute(Attribute::NoBuiltin))
284 if (hasAttribute(Attribute::NoCapture))
286 if (hasAttribute(Attribute::NoDuplicate))
287 return "noduplicate";
288 if (hasAttribute(Attribute::NoImplicitFloat))
289 return "noimplicitfloat";
290 if (hasAttribute(Attribute::NoInline))
292 if (hasAttribute(Attribute::NonLazyBind))
293 return "nonlazybind";
294 if (hasAttribute(Attribute::NonNull))
296 if (hasAttribute(Attribute::NoRedZone))
298 if (hasAttribute(Attribute::NoReturn))
300 if (hasAttribute(Attribute::NoRecurse))
302 if (hasAttribute(Attribute::NoUnwind))
304 if (hasAttribute(Attribute::OptimizeNone))
306 if (hasAttribute(Attribute::OptimizeForSize))
308 if (hasAttribute(Attribute::ReadNone))
310 if (hasAttribute(Attribute::ReadOnly))
312 if (hasAttribute(Attribute::WriteOnly))
314 if (hasAttribute(Attribute::Returned))
316 if (hasAttribute(Attribute::ReturnsTwice))
317 return "returns_twice";
318 if (hasAttribute(Attribute::SExt))
320 if (hasAttribute(Attribute::Speculatable))
321 return "speculatable";
322 if (hasAttribute(Attribute::StackProtect))
324 if (hasAttribute(Attribute::StackProtectReq))
326 if (hasAttribute(Attribute::StackProtectStrong))
328 if (hasAttribute(Attribute::SafeStack))
330 if (hasAttribute(Attribute::StructRet))
332 if (hasAttribute(Attribute::SanitizeThread))
333 return "sanitize_thread";
334 if (hasAttribute(Attribute::SanitizeMemory))
335 return "sanitize_memory";
336 if (hasAttribute(Attribute::UWTable))
338 if (hasAttribute(Attribute::ZExt))
340 if (hasAttribute(Attribute::Cold))
343 // FIXME: These should be output like this:
348 if (hasAttribute(Attribute::Alignment)) {
351 Result += (InAttrGrp) ? "=" : " ";
352 Result += utostr(getValueAsInt());
356 auto AttrWithBytesToString = [&](const char *Name) {
361 Result += utostr(getValueAsInt());
364 Result += utostr(getValueAsInt());
370 if (hasAttribute(Attribute::StackAlignment))
371 return AttrWithBytesToString("alignstack");
373 if (hasAttribute(Attribute::Dereferenceable))
374 return AttrWithBytesToString("dereferenceable");
376 if (hasAttribute(Attribute::DereferenceableOrNull))
377 return AttrWithBytesToString("dereferenceable_or_null");
379 if (hasAttribute(Attribute::AllocSize)) {
381 Optional<unsigned> NumElems;
382 std::tie(ElemSize, NumElems) = getAllocSizeArgs();
384 std::string Result = "allocsize(";
385 Result += utostr(ElemSize);
386 if (NumElems.hasValue()) {
388 Result += utostr(*NumElems);
394 // Convert target-dependent attributes to strings of the form:
399 if (isStringAttribute()) {
401 Result += (Twine('"') + getKindAsString() + Twine('"')).str();
403 std::string AttrVal = pImpl->getValueAsString();
404 if (AttrVal.empty()) return Result;
406 // Since some attribute strings contain special characters that cannot be
407 // printable, those have to be escaped to make the attribute value printable
408 // as is. e.g. "\01__gnu_mcount_nc"
410 raw_string_ostream OS(Result);
412 PrintEscapedString(AttrVal, OS);
418 llvm_unreachable("Unknown attribute");
421 bool Attribute::operator<(Attribute A) const {
422 if (!pImpl && !A.pImpl) return false;
423 if (!pImpl) return true;
424 if (!A.pImpl) return false;
425 return *pImpl < *A.pImpl;
428 //===----------------------------------------------------------------------===//
429 // AttributeImpl Definition
430 //===----------------------------------------------------------------------===//
432 // Pin the vtables to this file.
433 AttributeImpl::~AttributeImpl() = default;
435 void EnumAttributeImpl::anchor() {}
437 void IntAttributeImpl::anchor() {}
439 void StringAttributeImpl::anchor() {}
441 bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
442 if (isStringAttribute()) return false;
443 return getKindAsEnum() == A;
446 bool AttributeImpl::hasAttribute(StringRef Kind) const {
447 if (!isStringAttribute()) return false;
448 return getKindAsString() == Kind;
451 Attribute::AttrKind AttributeImpl::getKindAsEnum() const {
452 assert(isEnumAttribute() || isIntAttribute());
453 return static_cast<const EnumAttributeImpl *>(this)->getEnumKind();
456 uint64_t AttributeImpl::getValueAsInt() const {
457 assert(isIntAttribute());
458 return static_cast<const IntAttributeImpl *>(this)->getValue();
461 StringRef AttributeImpl::getKindAsString() const {
462 assert(isStringAttribute());
463 return static_cast<const StringAttributeImpl *>(this)->getStringKind();
466 StringRef AttributeImpl::getValueAsString() const {
467 assert(isStringAttribute());
468 return static_cast<const StringAttributeImpl *>(this)->getStringValue();
471 bool AttributeImpl::operator<(const AttributeImpl &AI) const {
472 // This sorts the attributes with Attribute::AttrKinds coming first (sorted
473 // relative to their enum value) and then strings.
474 if (isEnumAttribute()) {
475 if (AI.isEnumAttribute()) return getKindAsEnum() < AI.getKindAsEnum();
476 if (AI.isIntAttribute()) return true;
477 if (AI.isStringAttribute()) return true;
480 if (isIntAttribute()) {
481 if (AI.isEnumAttribute()) return false;
482 if (AI.isIntAttribute()) {
483 if (getKindAsEnum() == AI.getKindAsEnum())
484 return getValueAsInt() < AI.getValueAsInt();
485 return getKindAsEnum() < AI.getKindAsEnum();
487 if (AI.isStringAttribute()) return true;
490 if (AI.isEnumAttribute()) return false;
491 if (AI.isIntAttribute()) return false;
492 if (getKindAsString() == AI.getKindAsString())
493 return getValueAsString() < AI.getValueAsString();
494 return getKindAsString() < AI.getKindAsString();
497 //===----------------------------------------------------------------------===//
498 // AttributeSet Definition
499 //===----------------------------------------------------------------------===//
501 AttributeSet AttributeSet::get(LLVMContext &C, const AttrBuilder &B) {
502 return AttributeSet(AttributeSetNode::get(C, B));
505 AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<Attribute> Attrs) {
506 return AttributeSet(AttributeSetNode::get(C, Attrs));
509 AttributeSet AttributeSet::addAttribute(LLVMContext &C,
510 Attribute::AttrKind Kind) const {
511 if (hasAttribute(Kind)) return *this;
513 B.addAttribute(Kind);
514 return addAttributes(C, AttributeSet::get(C, B));
517 AttributeSet AttributeSet::addAttribute(LLVMContext &C, StringRef Kind,
518 StringRef Value) const {
520 B.addAttribute(Kind, Value);
521 return addAttributes(C, AttributeSet::get(C, B));
524 AttributeSet AttributeSet::addAttributes(LLVMContext &C,
525 const AttributeSet AS) const {
526 if (!hasAttributes())
529 if (!AS.hasAttributes())
533 for (Attribute I : *this)
539 AttributeSet AttributeSet::removeAttribute(LLVMContext &C,
540 Attribute::AttrKind Kind) const {
541 if (!hasAttribute(Kind)) return *this;
543 B.addAttribute(Kind);
544 return removeAttributes(C, B);
547 AttributeSet AttributeSet::removeAttribute(LLVMContext &C,
548 StringRef Kind) const {
549 if (!hasAttribute(Kind)) return *this;
551 B.addAttribute(Kind);
552 return removeAttributes(C, B);
555 AttributeSet AttributeSet::removeAttributes(LLVMContext &C,
556 const AttrBuilder &Attrs) const {
558 // FIXME it is not obvious how this should work for alignment.
559 // For now, say we can't pass in alignment, which no current use does.
560 assert(!Attrs.hasAlignmentAttr() && "Attempt to change alignment!");
562 AttrBuilder B(*this);
567 unsigned AttributeSet::getNumAttributes() const {
568 return SetNode ? SetNode->getNumAttributes() : 0;
571 bool AttributeSet::hasAttribute(Attribute::AttrKind Kind) const {
572 return SetNode ? SetNode->hasAttribute(Kind) : false;
575 bool AttributeSet::hasAttribute(StringRef Kind) const {
576 return SetNode ? SetNode->hasAttribute(Kind) : false;
579 Attribute AttributeSet::getAttribute(Attribute::AttrKind Kind) const {
580 return SetNode ? SetNode->getAttribute(Kind) : Attribute();
583 Attribute AttributeSet::getAttribute(StringRef Kind) const {
584 return SetNode ? SetNode->getAttribute(Kind) : Attribute();
587 unsigned AttributeSet::getAlignment() const {
588 return SetNode ? SetNode->getAlignment() : 0;
591 unsigned AttributeSet::getStackAlignment() const {
592 return SetNode ? SetNode->getStackAlignment() : 0;
595 uint64_t AttributeSet::getDereferenceableBytes() const {
596 return SetNode ? SetNode->getDereferenceableBytes() : 0;
599 uint64_t AttributeSet::getDereferenceableOrNullBytes() const {
600 return SetNode ? SetNode->getDereferenceableOrNullBytes() : 0;
603 std::pair<unsigned, Optional<unsigned>> AttributeSet::getAllocSizeArgs() const {
604 return SetNode ? SetNode->getAllocSizeArgs()
605 : std::pair<unsigned, Optional<unsigned>>(0, 0);
608 std::string AttributeSet::getAsString(bool InAttrGrp) const {
609 return SetNode ? SetNode->getAsString(InAttrGrp) : "";
612 AttributeSet::iterator AttributeSet::begin() const {
613 return SetNode ? SetNode->begin() : nullptr;
616 AttributeSet::iterator AttributeSet::end() const {
617 return SetNode ? SetNode->end() : nullptr;
620 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
621 LLVM_DUMP_METHOD void AttributeSet::dump() const {
624 dbgs() << getAsString(true) << " }\n";
628 //===----------------------------------------------------------------------===//
629 // AttributeSetNode Definition
630 //===----------------------------------------------------------------------===//
632 AttributeSetNode::AttributeSetNode(ArrayRef<Attribute> Attrs)
633 : AvailableAttrs(0), NumAttrs(Attrs.size()) {
634 // There's memory after the node where we can store the entries in.
635 std::copy(Attrs.begin(), Attrs.end(), getTrailingObjects<Attribute>());
637 for (Attribute I : *this) {
638 if (!I.isStringAttribute()) {
639 AvailableAttrs |= ((uint64_t)1) << I.getKindAsEnum();
644 AttributeSetNode *AttributeSetNode::get(LLVMContext &C,
645 ArrayRef<Attribute> Attrs) {
649 // Otherwise, build a key to look up the existing attributes.
650 LLVMContextImpl *pImpl = C.pImpl;
653 SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end());
654 std::sort(SortedAttrs.begin(), SortedAttrs.end());
656 for (Attribute Attr : SortedAttrs)
660 AttributeSetNode *PA =
661 pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint);
663 // If we didn't find any existing attributes of the same shape then create a
664 // new one and insert it.
666 // Coallocate entries after the AttributeSetNode itself.
667 void *Mem = ::operator new(totalSizeToAlloc<Attribute>(SortedAttrs.size()));
668 PA = new (Mem) AttributeSetNode(SortedAttrs);
669 pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint);
672 // Return the AttributeSetNode that we found or created.
676 AttributeSetNode *AttributeSetNode::get(LLVMContext &C, const AttrBuilder &B) {
677 // Add target-independent attributes.
678 SmallVector<Attribute, 8> Attrs;
679 for (Attribute::AttrKind Kind = Attribute::None;
680 Kind != Attribute::EndAttrKinds; Kind = Attribute::AttrKind(Kind + 1)) {
681 if (!B.contains(Kind))
686 case Attribute::Alignment:
687 Attr = Attribute::getWithAlignment(C, B.getAlignment());
689 case Attribute::StackAlignment:
690 Attr = Attribute::getWithStackAlignment(C, B.getStackAlignment());
692 case Attribute::Dereferenceable:
693 Attr = Attribute::getWithDereferenceableBytes(
694 C, B.getDereferenceableBytes());
696 case Attribute::DereferenceableOrNull:
697 Attr = Attribute::getWithDereferenceableOrNullBytes(
698 C, B.getDereferenceableOrNullBytes());
700 case Attribute::AllocSize: {
701 auto A = B.getAllocSizeArgs();
702 Attr = Attribute::getWithAllocSizeArgs(C, A.first, A.second);
706 Attr = Attribute::get(C, Kind);
708 Attrs.push_back(Attr);
711 // Add target-dependent (string) attributes.
712 for (const auto &TDA : B.td_attrs())
713 Attrs.emplace_back(Attribute::get(C, TDA.first, TDA.second));
715 return get(C, Attrs);
718 bool AttributeSetNode::hasAttribute(StringRef Kind) const {
719 for (Attribute I : *this)
720 if (I.hasAttribute(Kind))
725 Attribute AttributeSetNode::getAttribute(Attribute::AttrKind Kind) const {
726 if (hasAttribute(Kind)) {
727 for (Attribute I : *this)
728 if (I.hasAttribute(Kind))
734 Attribute AttributeSetNode::getAttribute(StringRef Kind) const {
735 for (Attribute I : *this)
736 if (I.hasAttribute(Kind))
741 unsigned AttributeSetNode::getAlignment() const {
742 for (Attribute I : *this)
743 if (I.hasAttribute(Attribute::Alignment))
744 return I.getAlignment();
748 unsigned AttributeSetNode::getStackAlignment() const {
749 for (Attribute I : *this)
750 if (I.hasAttribute(Attribute::StackAlignment))
751 return I.getStackAlignment();
755 uint64_t AttributeSetNode::getDereferenceableBytes() const {
756 for (Attribute I : *this)
757 if (I.hasAttribute(Attribute::Dereferenceable))
758 return I.getDereferenceableBytes();
762 uint64_t AttributeSetNode::getDereferenceableOrNullBytes() const {
763 for (Attribute I : *this)
764 if (I.hasAttribute(Attribute::DereferenceableOrNull))
765 return I.getDereferenceableOrNullBytes();
769 std::pair<unsigned, Optional<unsigned>>
770 AttributeSetNode::getAllocSizeArgs() const {
771 for (Attribute I : *this)
772 if (I.hasAttribute(Attribute::AllocSize))
773 return I.getAllocSizeArgs();
774 return std::make_pair(0, 0);
777 std::string AttributeSetNode::getAsString(bool InAttrGrp) const {
779 for (iterator I = begin(), E = end(); I != E; ++I) {
782 Str += I->getAsString(InAttrGrp);
787 //===----------------------------------------------------------------------===//
788 // AttributeListImpl Definition
789 //===----------------------------------------------------------------------===//
791 /// Map from AttributeList index to the internal array index. Adding one works:
792 /// FunctionIndex: ~0U -> 0
793 /// ReturnIndex: 0 -> 1
794 /// FirstArgIndex: 1.. -> 2..
795 static constexpr unsigned attrIdxToArrayIdx(unsigned Index) {
796 // MSVC warns about '~0U + 1' wrapping around when this is called on
797 // FunctionIndex, so cast to int first.
798 return static_cast<int>(Index) + 1;
801 AttributeListImpl::AttributeListImpl(LLVMContext &C,
802 ArrayRef<AttributeSet> Sets)
803 : AvailableFunctionAttrs(0), Context(C), NumAttrSets(Sets.size()) {
804 assert(!Sets.empty() && "pointless AttributeListImpl");
806 // There's memory after the node where we can store the entries in.
807 std::copy(Sets.begin(), Sets.end(), getTrailingObjects<AttributeSet>());
809 // Initialize AvailableFunctionAttrs summary bitset.
810 static_assert(Attribute::EndAttrKinds <=
811 sizeof(AvailableFunctionAttrs) * CHAR_BIT,
812 "Too many attributes");
813 static_assert(attrIdxToArrayIdx(AttributeList::FunctionIndex) == 0U,
814 "function should be stored in slot 0");
815 for (Attribute I : Sets[0]) {
816 if (!I.isStringAttribute())
817 AvailableFunctionAttrs |= 1ULL << I.getKindAsEnum();
821 void AttributeListImpl::Profile(FoldingSetNodeID &ID) const {
822 Profile(ID, makeArrayRef(begin(), end()));
825 void AttributeListImpl::Profile(FoldingSetNodeID &ID,
826 ArrayRef<AttributeSet> Sets) {
827 for (const auto &Set : Sets)
828 ID.AddPointer(Set.SetNode);
831 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
832 LLVM_DUMP_METHOD void AttributeListImpl::dump() const {
833 AttributeList(const_cast<AttributeListImpl *>(this)).dump();
837 //===----------------------------------------------------------------------===//
838 // AttributeList Construction and Mutation Methods
839 //===----------------------------------------------------------------------===//
841 AttributeList AttributeList::getImpl(LLVMContext &C,
842 ArrayRef<AttributeSet> AttrSets) {
843 assert(!AttrSets.empty() && "pointless AttributeListImpl");
845 LLVMContextImpl *pImpl = C.pImpl;
847 AttributeListImpl::Profile(ID, AttrSets);
850 AttributeListImpl *PA =
851 pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
853 // If we didn't find any existing attributes of the same shape then
854 // create a new one and insert it.
856 // Coallocate entries after the AttributeListImpl itself.
857 void *Mem = ::operator new(
858 AttributeListImpl::totalSizeToAlloc<AttributeSet>(AttrSets.size()));
859 PA = new (Mem) AttributeListImpl(C, AttrSets);
860 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
863 // Return the AttributesList that we found or created.
864 return AttributeList(PA);
868 AttributeList::get(LLVMContext &C,
869 ArrayRef<std::pair<unsigned, Attribute>> Attrs) {
870 // If there are no attributes then return a null AttributesList pointer.
872 return AttributeList();
874 assert(std::is_sorted(Attrs.begin(), Attrs.end(),
875 [](const std::pair<unsigned, Attribute> &LHS,
876 const std::pair<unsigned, Attribute> &RHS) {
877 return LHS.first < RHS.first;
878 }) && "Misordered Attributes list!");
879 assert(none_of(Attrs,
880 [](const std::pair<unsigned, Attribute> &Pair) {
881 return Pair.second.hasAttribute(Attribute::None);
883 "Pointless attribute!");
885 // Create a vector if (unsigned, AttributeSetNode*) pairs from the attributes
887 SmallVector<std::pair<unsigned, AttributeSet>, 8> AttrPairVec;
888 for (ArrayRef<std::pair<unsigned, Attribute>>::iterator I = Attrs.begin(),
889 E = Attrs.end(); I != E; ) {
890 unsigned Index = I->first;
891 SmallVector<Attribute, 4> AttrVec;
892 while (I != E && I->first == Index) {
893 AttrVec.push_back(I->second);
897 AttrPairVec.emplace_back(Index, AttributeSet::get(C, AttrVec));
900 return get(C, AttrPairVec);
904 AttributeList::get(LLVMContext &C,
905 ArrayRef<std::pair<unsigned, AttributeSet>> Attrs) {
906 // If there are no attributes then return a null AttributesList pointer.
908 return AttributeList();
910 assert(std::is_sorted(Attrs.begin(), Attrs.end(),
911 [](const std::pair<unsigned, AttributeSet> &LHS,
912 const std::pair<unsigned, AttributeSet> &RHS) {
913 return LHS.first < RHS.first;
915 "Misordered Attributes list!");
916 assert(none_of(Attrs,
917 [](const std::pair<unsigned, AttributeSet> &Pair) {
918 return !Pair.second.hasAttributes();
920 "Pointless attribute!");
922 unsigned MaxIndex = Attrs.back().first;
924 SmallVector<AttributeSet, 4> AttrVec(attrIdxToArrayIdx(MaxIndex) + 1);
925 for (auto Pair : Attrs)
926 AttrVec[attrIdxToArrayIdx(Pair.first)] = Pair.second;
928 return getImpl(C, AttrVec);
931 AttributeList AttributeList::get(LLVMContext &C, AttributeSet FnAttrs,
932 AttributeSet RetAttrs,
933 ArrayRef<AttributeSet> ArgAttrs) {
934 // Scan from the end to find the last argument with attributes. Most
935 // arguments don't have attributes, so it's nice if we can have fewer unique
936 // AttributeListImpls by dropping empty attribute sets at the end of the list.
937 unsigned NumSets = 0;
938 for (size_t I = ArgAttrs.size(); I != 0; --I) {
939 if (ArgAttrs[I - 1].hasAttributes()) {
945 // Check function and return attributes if we didn't have argument
947 if (RetAttrs.hasAttributes())
949 else if (FnAttrs.hasAttributes())
953 // If all attribute sets were empty, we can use the empty attribute list.
955 return AttributeList();
957 SmallVector<AttributeSet, 8> AttrSets;
958 AttrSets.reserve(NumSets);
959 // If we have any attributes, we always have function attributes.
960 AttrSets.push_back(FnAttrs);
962 AttrSets.push_back(RetAttrs);
964 // Drop the empty argument attribute sets at the end.
965 ArgAttrs = ArgAttrs.take_front(NumSets - 2);
966 AttrSets.insert(AttrSets.end(), ArgAttrs.begin(), ArgAttrs.end());
969 return getImpl(C, AttrSets);
972 AttributeList AttributeList::get(LLVMContext &C, unsigned Index,
973 const AttrBuilder &B) {
974 if (!B.hasAttributes())
975 return AttributeList();
976 Index = attrIdxToArrayIdx(Index);
977 SmallVector<AttributeSet, 8> AttrSets(Index + 1);
978 AttrSets[Index] = AttributeSet::get(C, B);
979 return getImpl(C, AttrSets);
982 AttributeList AttributeList::get(LLVMContext &C, unsigned Index,
983 ArrayRef<Attribute::AttrKind> Kinds) {
984 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
985 for (Attribute::AttrKind K : Kinds)
986 Attrs.emplace_back(Index, Attribute::get(C, K));
987 return get(C, Attrs);
990 AttributeList AttributeList::get(LLVMContext &C, unsigned Index,
991 ArrayRef<StringRef> Kinds) {
992 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
993 for (StringRef K : Kinds)
994 Attrs.emplace_back(Index, Attribute::get(C, K));
995 return get(C, Attrs);
998 AttributeList AttributeList::get(LLVMContext &C,
999 ArrayRef<AttributeList> Attrs) {
1001 return AttributeList();
1002 if (Attrs.size() == 1)
1005 unsigned MaxSize = 0;
1006 for (AttributeList List : Attrs)
1007 MaxSize = std::max(MaxSize, List.getNumAttrSets());
1009 SmallVector<AttributeSet, 8> NewAttrSets(MaxSize);
1010 for (unsigned I = 0; I < MaxSize; ++I) {
1011 AttrBuilder CurBuilder;
1012 for (AttributeList List : Attrs)
1013 CurBuilder.merge(List.getAttributes(I - 1));
1014 NewAttrSets[I] = AttributeSet::get(C, CurBuilder);
1017 return getImpl(C, NewAttrSets);
1020 AttributeList AttributeList::addAttribute(LLVMContext &C, unsigned Index,
1021 Attribute::AttrKind Kind) const {
1022 if (hasAttribute(Index, Kind)) return *this;
1024 B.addAttribute(Kind);
1025 return addAttributes(C, Index, B);
1028 AttributeList AttributeList::addAttribute(LLVMContext &C, unsigned Index,
1030 StringRef Value) const {
1032 B.addAttribute(Kind, Value);
1033 return addAttributes(C, Index, B);
1036 AttributeList AttributeList::addAttribute(LLVMContext &C,
1037 ArrayRef<unsigned> Indices,
1038 Attribute A) const {
1039 assert(std::is_sorted(Indices.begin(), Indices.end()));
1041 SmallVector<AttributeSet, 4> AttrSets(this->begin(), this->end());
1042 unsigned MaxIndex = attrIdxToArrayIdx(Indices.back());
1043 if (MaxIndex >= AttrSets.size())
1044 AttrSets.resize(MaxIndex + 1);
1046 for (unsigned Index : Indices) {
1047 Index = attrIdxToArrayIdx(Index);
1048 AttrBuilder B(AttrSets[Index]);
1050 AttrSets[Index] = AttributeSet::get(C, B);
1053 return getImpl(C, AttrSets);
1056 AttributeList AttributeList::addAttributes(LLVMContext &C, unsigned Index,
1057 const AttrBuilder &B) const {
1058 if (!B.hasAttributes())
1062 return AttributeList::get(C, {{Index, AttributeSet::get(C, B)}});
1065 // FIXME it is not obvious how this should work for alignment. For now, say
1066 // we can't change a known alignment.
1067 unsigned OldAlign = getAttributes(Index).getAlignment();
1068 unsigned NewAlign = B.getAlignment();
1069 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
1070 "Attempt to change alignment!");
1073 Index = attrIdxToArrayIdx(Index);
1074 SmallVector<AttributeSet, 4> AttrSets(this->begin(), this->end());
1075 if (Index >= AttrSets.size())
1076 AttrSets.resize(Index + 1);
1078 AttrBuilder Merged(AttrSets[Index]);
1080 AttrSets[Index] = AttributeSet::get(C, Merged);
1082 return getImpl(C, AttrSets);
1085 AttributeList AttributeList::removeAttribute(LLVMContext &C, unsigned Index,
1086 Attribute::AttrKind Kind) const {
1087 if (!hasAttribute(Index, Kind)) return *this;
1089 B.addAttribute(Kind);
1090 return removeAttributes(C, Index, B);
1093 AttributeList AttributeList::removeAttribute(LLVMContext &C, unsigned Index,
1094 StringRef Kind) const {
1095 if (!hasAttribute(Index, Kind)) return *this;
1097 B.addAttribute(Kind);
1098 return removeAttributes(C, Index, B);
1102 AttributeList::removeAttributes(LLVMContext &C, unsigned Index,
1103 const AttrBuilder &AttrsToRemove) const {
1105 return AttributeList();
1107 // FIXME it is not obvious how this should work for alignment.
1108 // For now, say we can't pass in alignment, which no current use does.
1109 assert(!AttrsToRemove.hasAlignmentAttr() && "Attempt to change alignment!");
1111 Index = attrIdxToArrayIdx(Index);
1112 SmallVector<AttributeSet, 4> AttrSets(this->begin(), this->end());
1113 if (Index >= AttrSets.size())
1114 AttrSets.resize(Index + 1);
1116 AttrBuilder B(AttrSets[Index]);
1117 B.remove(AttrsToRemove);
1118 AttrSets[Index] = AttributeSet::get(C, B);
1120 return getImpl(C, AttrSets);
1123 AttributeList AttributeList::removeAttributes(LLVMContext &C,
1124 unsigned WithoutIndex) const {
1126 return AttributeList();
1127 WithoutIndex = attrIdxToArrayIdx(WithoutIndex);
1128 if (WithoutIndex >= getNumAttrSets())
1130 SmallVector<AttributeSet, 4> AttrSets(this->begin(), this->end());
1131 AttrSets[WithoutIndex] = AttributeSet();
1132 return getImpl(C, AttrSets);
1135 AttributeList AttributeList::addDereferenceableAttr(LLVMContext &C,
1137 uint64_t Bytes) const {
1139 B.addDereferenceableAttr(Bytes);
1140 return addAttributes(C, Index, B);
1144 AttributeList::addDereferenceableOrNullAttr(LLVMContext &C, unsigned Index,
1145 uint64_t Bytes) const {
1147 B.addDereferenceableOrNullAttr(Bytes);
1148 return addAttributes(C, Index, B);
1152 AttributeList::addAllocSizeAttr(LLVMContext &C, unsigned Index,
1153 unsigned ElemSizeArg,
1154 const Optional<unsigned> &NumElemsArg) {
1156 B.addAllocSizeAttr(ElemSizeArg, NumElemsArg);
1157 return addAttributes(C, Index, B);
1160 //===----------------------------------------------------------------------===//
1161 // AttributeList Accessor Methods
1162 //===----------------------------------------------------------------------===//
1164 LLVMContext &AttributeList::getContext() const { return pImpl->getContext(); }
1166 AttributeSet AttributeList::getParamAttributes(unsigned ArgNo) const {
1167 return getAttributes(ArgNo + FirstArgIndex);
1170 AttributeSet AttributeList::getRetAttributes() const {
1171 return getAttributes(ReturnIndex);
1174 AttributeSet AttributeList::getFnAttributes() const {
1175 return getAttributes(FunctionIndex);
1178 bool AttributeList::hasAttribute(unsigned Index,
1179 Attribute::AttrKind Kind) const {
1180 return getAttributes(Index).hasAttribute(Kind);
1183 bool AttributeList::hasAttribute(unsigned Index, StringRef Kind) const {
1184 return getAttributes(Index).hasAttribute(Kind);
1187 bool AttributeList::hasAttributes(unsigned Index) const {
1188 return getAttributes(Index).hasAttributes();
1191 bool AttributeList::hasFnAttribute(Attribute::AttrKind Kind) const {
1192 return pImpl && pImpl->hasFnAttribute(Kind);
1195 bool AttributeList::hasFnAttribute(StringRef Kind) const {
1196 return hasAttribute(AttributeList::FunctionIndex, Kind);
1199 bool AttributeList::hasParamAttribute(unsigned ArgNo,
1200 Attribute::AttrKind Kind) const {
1201 return hasAttribute(ArgNo + FirstArgIndex, Kind);
1204 bool AttributeList::hasAttrSomewhere(Attribute::AttrKind Attr,
1205 unsigned *Index) const {
1206 if (!pImpl) return false;
1208 for (unsigned I = index_begin(), E = index_end(); I != E; ++I) {
1209 if (hasAttribute(I, Attr)) {
1219 Attribute AttributeList::getAttribute(unsigned Index,
1220 Attribute::AttrKind Kind) const {
1221 return getAttributes(Index).getAttribute(Kind);
1224 Attribute AttributeList::getAttribute(unsigned Index, StringRef Kind) const {
1225 return getAttributes(Index).getAttribute(Kind);
1228 unsigned AttributeList::getRetAlignment() const {
1229 return getAttributes(ReturnIndex).getAlignment();
1232 unsigned AttributeList::getParamAlignment(unsigned ArgNo) const {
1233 return getAttributes(ArgNo + FirstArgIndex).getAlignment();
1236 unsigned AttributeList::getStackAlignment(unsigned Index) const {
1237 return getAttributes(Index).getStackAlignment();
1240 uint64_t AttributeList::getDereferenceableBytes(unsigned Index) const {
1241 return getAttributes(Index).getDereferenceableBytes();
1244 uint64_t AttributeList::getDereferenceableOrNullBytes(unsigned Index) const {
1245 return getAttributes(Index).getDereferenceableOrNullBytes();
1248 std::pair<unsigned, Optional<unsigned>>
1249 AttributeList::getAllocSizeArgs(unsigned Index) const {
1250 return getAttributes(Index).getAllocSizeArgs();
1253 std::string AttributeList::getAsString(unsigned Index, bool InAttrGrp) const {
1254 return getAttributes(Index).getAsString(InAttrGrp);
1257 AttributeSet AttributeList::getAttributes(unsigned Index) const {
1258 Index = attrIdxToArrayIdx(Index);
1259 if (!pImpl || Index >= getNumAttrSets())
1260 return AttributeSet();
1261 return pImpl->begin()[Index];
1264 AttributeList::iterator AttributeList::begin() const {
1265 return pImpl ? pImpl->begin() : nullptr;
1268 AttributeList::iterator AttributeList::end() const {
1269 return pImpl ? pImpl->end() : nullptr;
1272 //===----------------------------------------------------------------------===//
1273 // AttributeList Introspection Methods
1274 //===----------------------------------------------------------------------===//
1276 unsigned AttributeList::getNumAttrSets() const {
1277 return pImpl ? pImpl->NumAttrSets : 0;
1280 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1281 LLVM_DUMP_METHOD void AttributeList::dump() const {
1284 for (unsigned i = index_begin(), e = index_end(); i != e; ++i) {
1285 if (getAttributes(i).hasAttributes())
1286 dbgs() << " { " << i << " => " << getAsString(i) << " }\n";
1293 //===----------------------------------------------------------------------===//
1294 // AttrBuilder Method Implementations
1295 //===----------------------------------------------------------------------===//
1297 // FIXME: Remove this ctor, use AttributeSet.
1298 AttrBuilder::AttrBuilder(AttributeList AL, unsigned Index) {
1299 AttributeSet AS = AL.getAttributes(Index);
1300 for (const Attribute &A : AS)
1304 AttrBuilder::AttrBuilder(AttributeSet AS) {
1305 for (const Attribute &A : AS)
1309 void AttrBuilder::clear() {
1311 TargetDepAttrs.clear();
1312 Alignment = StackAlignment = DerefBytes = DerefOrNullBytes = 0;
1316 AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
1317 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
1318 assert(Val != Attribute::Alignment && Val != Attribute::StackAlignment &&
1319 Val != Attribute::Dereferenceable && Val != Attribute::AllocSize &&
1320 "Adding integer attribute without adding a value!");
1325 AttrBuilder &AttrBuilder::addAttribute(Attribute Attr) {
1326 if (Attr.isStringAttribute()) {
1327 addAttribute(Attr.getKindAsString(), Attr.getValueAsString());
1331 Attribute::AttrKind Kind = Attr.getKindAsEnum();
1334 if (Kind == Attribute::Alignment)
1335 Alignment = Attr.getAlignment();
1336 else if (Kind == Attribute::StackAlignment)
1337 StackAlignment = Attr.getStackAlignment();
1338 else if (Kind == Attribute::Dereferenceable)
1339 DerefBytes = Attr.getDereferenceableBytes();
1340 else if (Kind == Attribute::DereferenceableOrNull)
1341 DerefOrNullBytes = Attr.getDereferenceableOrNullBytes();
1342 else if (Kind == Attribute::AllocSize)
1343 AllocSizeArgs = Attr.getValueAsInt();
1347 AttrBuilder &AttrBuilder::addAttribute(StringRef A, StringRef V) {
1348 TargetDepAttrs[A] = V;
1352 AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
1353 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
1356 if (Val == Attribute::Alignment)
1358 else if (Val == Attribute::StackAlignment)
1360 else if (Val == Attribute::Dereferenceable)
1362 else if (Val == Attribute::DereferenceableOrNull)
1363 DerefOrNullBytes = 0;
1364 else if (Val == Attribute::AllocSize)
1370 AttrBuilder &AttrBuilder::removeAttributes(AttributeList A, uint64_t Index) {
1371 remove(A.getAttributes(Index));
1375 AttrBuilder &AttrBuilder::removeAttribute(StringRef A) {
1376 std::map<std::string, std::string>::iterator I = TargetDepAttrs.find(A);
1377 if (I != TargetDepAttrs.end())
1378 TargetDepAttrs.erase(I);
1382 std::pair<unsigned, Optional<unsigned>> AttrBuilder::getAllocSizeArgs() const {
1383 return unpackAllocSizeArgs(AllocSizeArgs);
1386 AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
1387 if (Align == 0) return *this;
1389 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1390 assert(Align <= 0x40000000 && "Alignment too large.");
1392 Attrs[Attribute::Alignment] = true;
1397 AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
1398 // Default alignment, allow the target to define how to align it.
1399 if (Align == 0) return *this;
1401 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1402 assert(Align <= 0x100 && "Alignment too large.");
1404 Attrs[Attribute::StackAlignment] = true;
1405 StackAlignment = Align;
1409 AttrBuilder &AttrBuilder::addDereferenceableAttr(uint64_t Bytes) {
1410 if (Bytes == 0) return *this;
1412 Attrs[Attribute::Dereferenceable] = true;
1417 AttrBuilder &AttrBuilder::addDereferenceableOrNullAttr(uint64_t Bytes) {
1421 Attrs[Attribute::DereferenceableOrNull] = true;
1422 DerefOrNullBytes = Bytes;
1426 AttrBuilder &AttrBuilder::addAllocSizeAttr(unsigned ElemSize,
1427 const Optional<unsigned> &NumElems) {
1428 return addAllocSizeAttrFromRawRepr(packAllocSizeArgs(ElemSize, NumElems));
1431 AttrBuilder &AttrBuilder::addAllocSizeAttrFromRawRepr(uint64_t RawArgs) {
1432 // (0, 0) is our "not present" value, so we need to check for it here.
1433 assert(RawArgs && "Invalid allocsize arguments -- given allocsize(0, 0)");
1435 Attrs[Attribute::AllocSize] = true;
1436 // Reuse existing machinery to store this as a single 64-bit integer so we can
1437 // save a few bytes over using a pair<unsigned, Optional<unsigned>>.
1438 AllocSizeArgs = RawArgs;
1442 AttrBuilder &AttrBuilder::merge(const AttrBuilder &B) {
1443 // FIXME: What if both have alignments, but they don't match?!
1445 Alignment = B.Alignment;
1447 if (!StackAlignment)
1448 StackAlignment = B.StackAlignment;
1451 DerefBytes = B.DerefBytes;
1453 if (!DerefOrNullBytes)
1454 DerefOrNullBytes = B.DerefOrNullBytes;
1457 AllocSizeArgs = B.AllocSizeArgs;
1461 for (auto I : B.td_attrs())
1462 TargetDepAttrs[I.first] = I.second;
1467 AttrBuilder &AttrBuilder::remove(const AttrBuilder &B) {
1468 // FIXME: What if both have alignments, but they don't match?!
1472 if (B.StackAlignment)
1478 if (B.DerefOrNullBytes)
1479 DerefOrNullBytes = 0;
1481 if (B.AllocSizeArgs)
1486 for (auto I : B.td_attrs())
1487 TargetDepAttrs.erase(I.first);
1492 bool AttrBuilder::overlaps(const AttrBuilder &B) const {
1493 // First check if any of the target independent attributes overlap.
1494 if ((Attrs & B.Attrs).any())
1497 // Then check if any target dependent ones do.
1498 for (const auto &I : td_attrs())
1499 if (B.contains(I.first))
1505 bool AttrBuilder::contains(StringRef A) const {
1506 return TargetDepAttrs.find(A) != TargetDepAttrs.end();
1509 bool AttrBuilder::hasAttributes() const {
1510 return !Attrs.none() || !TargetDepAttrs.empty();
1513 bool AttrBuilder::hasAttributes(AttributeList AL, uint64_t Index) const {
1514 AttributeSet AS = AL.getAttributes(Index);
1516 for (Attribute Attr : AS) {
1517 if (Attr.isEnumAttribute() || Attr.isIntAttribute()) {
1518 if (contains(Attr.getKindAsEnum()))
1521 assert(Attr.isStringAttribute() && "Invalid attribute kind!");
1522 return contains(Attr.getKindAsString());
1529 bool AttrBuilder::hasAlignmentAttr() const {
1530 return Alignment != 0;
1533 bool AttrBuilder::operator==(const AttrBuilder &B) {
1534 if (Attrs != B.Attrs)
1537 for (td_const_iterator I = TargetDepAttrs.begin(),
1538 E = TargetDepAttrs.end(); I != E; ++I)
1539 if (B.TargetDepAttrs.find(I->first) == B.TargetDepAttrs.end())
1542 return Alignment == B.Alignment && StackAlignment == B.StackAlignment &&
1543 DerefBytes == B.DerefBytes;
1546 //===----------------------------------------------------------------------===//
1547 // AttributeFuncs Function Defintions
1548 //===----------------------------------------------------------------------===//
1550 /// \brief Which attributes cannot be applied to a type.
1551 AttrBuilder AttributeFuncs::typeIncompatible(Type *Ty) {
1552 AttrBuilder Incompatible;
1554 if (!Ty->isIntegerTy())
1555 // Attribute that only apply to integers.
1556 Incompatible.addAttribute(Attribute::SExt)
1557 .addAttribute(Attribute::ZExt);
1559 if (!Ty->isPointerTy())
1560 // Attribute that only apply to pointers.
1561 Incompatible.addAttribute(Attribute::ByVal)
1562 .addAttribute(Attribute::Nest)
1563 .addAttribute(Attribute::NoAlias)
1564 .addAttribute(Attribute::NoCapture)
1565 .addAttribute(Attribute::NonNull)
1566 .addDereferenceableAttr(1) // the int here is ignored
1567 .addDereferenceableOrNullAttr(1) // the int here is ignored
1568 .addAttribute(Attribute::ReadNone)
1569 .addAttribute(Attribute::ReadOnly)
1570 .addAttribute(Attribute::StructRet)
1571 .addAttribute(Attribute::InAlloca);
1573 return Incompatible;
1576 template<typename AttrClass>
1577 static bool isEqual(const Function &Caller, const Function &Callee) {
1578 return Caller.getFnAttribute(AttrClass::getKind()) ==
1579 Callee.getFnAttribute(AttrClass::getKind());
1582 /// \brief Compute the logical AND of the attributes of the caller and the
1585 /// This function sets the caller's attribute to false if the callee's attribute
1587 template<typename AttrClass>
1588 static void setAND(Function &Caller, const Function &Callee) {
1589 if (AttrClass::isSet(Caller, AttrClass::getKind()) &&
1590 !AttrClass::isSet(Callee, AttrClass::getKind()))
1591 AttrClass::set(Caller, AttrClass::getKind(), false);
1594 /// \brief Compute the logical OR of the attributes of the caller and the
1597 /// This function sets the caller's attribute to true if the callee's attribute
1599 template<typename AttrClass>
1600 static void setOR(Function &Caller, const Function &Callee) {
1601 if (!AttrClass::isSet(Caller, AttrClass::getKind()) &&
1602 AttrClass::isSet(Callee, AttrClass::getKind()))
1603 AttrClass::set(Caller, AttrClass::getKind(), true);
1606 /// \brief If the inlined function had a higher stack protection level than the
1607 /// calling function, then bump up the caller's stack protection level.
1608 static void adjustCallerSSPLevel(Function &Caller, const Function &Callee) {
1609 // If upgrading the SSP attribute, clear out the old SSP Attributes first.
1610 // Having multiple SSP attributes doesn't actually hurt, but it adds useless
1611 // clutter to the IR.
1612 AttrBuilder OldSSPAttr;
1613 OldSSPAttr.addAttribute(Attribute::StackProtect)
1614 .addAttribute(Attribute::StackProtectStrong)
1615 .addAttribute(Attribute::StackProtectReq);
1617 if (Callee.hasFnAttribute(Attribute::StackProtectReq)) {
1618 Caller.removeAttributes(AttributeList::FunctionIndex, OldSSPAttr);
1619 Caller.addFnAttr(Attribute::StackProtectReq);
1620 } else if (Callee.hasFnAttribute(Attribute::StackProtectStrong) &&
1621 !Caller.hasFnAttribute(Attribute::StackProtectReq)) {
1622 Caller.removeAttributes(AttributeList::FunctionIndex, OldSSPAttr);
1623 Caller.addFnAttr(Attribute::StackProtectStrong);
1624 } else if (Callee.hasFnAttribute(Attribute::StackProtect) &&
1625 !Caller.hasFnAttribute(Attribute::StackProtectReq) &&
1626 !Caller.hasFnAttribute(Attribute::StackProtectStrong))
1627 Caller.addFnAttr(Attribute::StackProtect);
1630 #define GET_ATTR_COMPAT_FUNC
1631 #include "AttributesCompatFunc.inc"
1633 bool AttributeFuncs::areInlineCompatible(const Function &Caller,
1634 const Function &Callee) {
1635 return hasCompatibleFnAttrs(Caller, Callee);
1638 void AttributeFuncs::mergeAttributesForInlining(Function &Caller,
1639 const Function &Callee) {
1640 mergeFnAttrs(Caller, Callee);