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 "llvm/IR/Attributes.h"
17 #include "AttributeImpl.h"
18 #include "LLVMContextImpl.h"
19 #include "llvm/ADT/ArrayRef.h"
20 #include "llvm/ADT/FoldingSet.h"
21 #include "llvm/ADT/Optional.h"
22 #include "llvm/ADT/STLExtras.h"
23 #include "llvm/ADT/SmallVector.h"
24 #include "llvm/ADT/StringExtras.h"
25 #include "llvm/ADT/StringRef.h"
26 #include "llvm/ADT/Twine.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::SanitizeHWAddress))
249 return "sanitize_hwaddress";
250 if (hasAttribute(Attribute::AlwaysInline))
251 return "alwaysinline";
252 if (hasAttribute(Attribute::ArgMemOnly))
254 if (hasAttribute(Attribute::Builtin))
256 if (hasAttribute(Attribute::ByVal))
258 if (hasAttribute(Attribute::Convergent))
260 if (hasAttribute(Attribute::SwiftError))
262 if (hasAttribute(Attribute::SwiftSelf))
264 if (hasAttribute(Attribute::InaccessibleMemOnly))
265 return "inaccessiblememonly";
266 if (hasAttribute(Attribute::InaccessibleMemOrArgMemOnly))
267 return "inaccessiblemem_or_argmemonly";
268 if (hasAttribute(Attribute::InAlloca))
270 if (hasAttribute(Attribute::InlineHint))
272 if (hasAttribute(Attribute::InReg))
274 if (hasAttribute(Attribute::JumpTable))
276 if (hasAttribute(Attribute::MinSize))
278 if (hasAttribute(Attribute::Naked))
280 if (hasAttribute(Attribute::Nest))
282 if (hasAttribute(Attribute::NoAlias))
284 if (hasAttribute(Attribute::NoBuiltin))
286 if (hasAttribute(Attribute::NoCapture))
288 if (hasAttribute(Attribute::NoDuplicate))
289 return "noduplicate";
290 if (hasAttribute(Attribute::NoImplicitFloat))
291 return "noimplicitfloat";
292 if (hasAttribute(Attribute::NoInline))
294 if (hasAttribute(Attribute::NonLazyBind))
295 return "nonlazybind";
296 if (hasAttribute(Attribute::NonNull))
298 if (hasAttribute(Attribute::NoRedZone))
300 if (hasAttribute(Attribute::NoReturn))
302 if (hasAttribute(Attribute::NoRecurse))
304 if (hasAttribute(Attribute::NoUnwind))
306 if (hasAttribute(Attribute::OptimizeNone))
308 if (hasAttribute(Attribute::OptimizeForSize))
310 if (hasAttribute(Attribute::ReadNone))
312 if (hasAttribute(Attribute::ReadOnly))
314 if (hasAttribute(Attribute::WriteOnly))
316 if (hasAttribute(Attribute::Returned))
318 if (hasAttribute(Attribute::ReturnsTwice))
319 return "returns_twice";
320 if (hasAttribute(Attribute::SExt))
322 if (hasAttribute(Attribute::Speculatable))
323 return "speculatable";
324 if (hasAttribute(Attribute::StackProtect))
326 if (hasAttribute(Attribute::StackProtectReq))
328 if (hasAttribute(Attribute::StackProtectStrong))
330 if (hasAttribute(Attribute::SafeStack))
332 if (hasAttribute(Attribute::StrictFP))
334 if (hasAttribute(Attribute::StructRet))
336 if (hasAttribute(Attribute::SanitizeThread))
337 return "sanitize_thread";
338 if (hasAttribute(Attribute::SanitizeMemory))
339 return "sanitize_memory";
340 if (hasAttribute(Attribute::UWTable))
342 if (hasAttribute(Attribute::ZExt))
344 if (hasAttribute(Attribute::Cold))
347 // FIXME: These should be output like this:
352 if (hasAttribute(Attribute::Alignment)) {
355 Result += (InAttrGrp) ? "=" : " ";
356 Result += utostr(getValueAsInt());
360 auto AttrWithBytesToString = [&](const char *Name) {
365 Result += utostr(getValueAsInt());
368 Result += utostr(getValueAsInt());
374 if (hasAttribute(Attribute::StackAlignment))
375 return AttrWithBytesToString("alignstack");
377 if (hasAttribute(Attribute::Dereferenceable))
378 return AttrWithBytesToString("dereferenceable");
380 if (hasAttribute(Attribute::DereferenceableOrNull))
381 return AttrWithBytesToString("dereferenceable_or_null");
383 if (hasAttribute(Attribute::AllocSize)) {
385 Optional<unsigned> NumElems;
386 std::tie(ElemSize, NumElems) = getAllocSizeArgs();
388 std::string Result = "allocsize(";
389 Result += utostr(ElemSize);
390 if (NumElems.hasValue()) {
392 Result += utostr(*NumElems);
398 // Convert target-dependent attributes to strings of the form:
403 if (isStringAttribute()) {
405 Result += (Twine('"') + getKindAsString() + Twine('"')).str();
407 std::string AttrVal = pImpl->getValueAsString();
408 if (AttrVal.empty()) return Result;
410 // Since some attribute strings contain special characters that cannot be
411 // printable, those have to be escaped to make the attribute value printable
412 // as is. e.g. "\01__gnu_mcount_nc"
414 raw_string_ostream OS(Result);
416 PrintEscapedString(AttrVal, OS);
422 llvm_unreachable("Unknown attribute");
425 bool Attribute::operator<(Attribute A) const {
426 if (!pImpl && !A.pImpl) return false;
427 if (!pImpl) return true;
428 if (!A.pImpl) return false;
429 return *pImpl < *A.pImpl;
432 //===----------------------------------------------------------------------===//
433 // AttributeImpl Definition
434 //===----------------------------------------------------------------------===//
436 // Pin the vtables to this file.
437 AttributeImpl::~AttributeImpl() = default;
439 void EnumAttributeImpl::anchor() {}
441 void IntAttributeImpl::anchor() {}
443 void StringAttributeImpl::anchor() {}
445 bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
446 if (isStringAttribute()) return false;
447 return getKindAsEnum() == A;
450 bool AttributeImpl::hasAttribute(StringRef Kind) const {
451 if (!isStringAttribute()) return false;
452 return getKindAsString() == Kind;
455 Attribute::AttrKind AttributeImpl::getKindAsEnum() const {
456 assert(isEnumAttribute() || isIntAttribute());
457 return static_cast<const EnumAttributeImpl *>(this)->getEnumKind();
460 uint64_t AttributeImpl::getValueAsInt() const {
461 assert(isIntAttribute());
462 return static_cast<const IntAttributeImpl *>(this)->getValue();
465 StringRef AttributeImpl::getKindAsString() const {
466 assert(isStringAttribute());
467 return static_cast<const StringAttributeImpl *>(this)->getStringKind();
470 StringRef AttributeImpl::getValueAsString() const {
471 assert(isStringAttribute());
472 return static_cast<const StringAttributeImpl *>(this)->getStringValue();
475 bool AttributeImpl::operator<(const AttributeImpl &AI) const {
476 // This sorts the attributes with Attribute::AttrKinds coming first (sorted
477 // relative to their enum value) and then strings.
478 if (isEnumAttribute()) {
479 if (AI.isEnumAttribute()) return getKindAsEnum() < AI.getKindAsEnum();
480 if (AI.isIntAttribute()) return true;
481 if (AI.isStringAttribute()) return true;
484 if (isIntAttribute()) {
485 if (AI.isEnumAttribute()) return false;
486 if (AI.isIntAttribute()) {
487 if (getKindAsEnum() == AI.getKindAsEnum())
488 return getValueAsInt() < AI.getValueAsInt();
489 return getKindAsEnum() < AI.getKindAsEnum();
491 if (AI.isStringAttribute()) return true;
494 if (AI.isEnumAttribute()) return false;
495 if (AI.isIntAttribute()) return false;
496 if (getKindAsString() == AI.getKindAsString())
497 return getValueAsString() < AI.getValueAsString();
498 return getKindAsString() < AI.getKindAsString();
501 //===----------------------------------------------------------------------===//
502 // AttributeSet Definition
503 //===----------------------------------------------------------------------===//
505 AttributeSet AttributeSet::get(LLVMContext &C, const AttrBuilder &B) {
506 return AttributeSet(AttributeSetNode::get(C, B));
509 AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<Attribute> Attrs) {
510 return AttributeSet(AttributeSetNode::get(C, Attrs));
513 AttributeSet AttributeSet::addAttribute(LLVMContext &C,
514 Attribute::AttrKind Kind) const {
515 if (hasAttribute(Kind)) return *this;
517 B.addAttribute(Kind);
518 return addAttributes(C, AttributeSet::get(C, B));
521 AttributeSet AttributeSet::addAttribute(LLVMContext &C, StringRef Kind,
522 StringRef Value) const {
524 B.addAttribute(Kind, Value);
525 return addAttributes(C, AttributeSet::get(C, B));
528 AttributeSet AttributeSet::addAttributes(LLVMContext &C,
529 const AttributeSet AS) const {
530 if (!hasAttributes())
533 if (!AS.hasAttributes())
537 for (Attribute I : *this)
543 AttributeSet AttributeSet::removeAttribute(LLVMContext &C,
544 Attribute::AttrKind Kind) const {
545 if (!hasAttribute(Kind)) return *this;
547 B.addAttribute(Kind);
548 return removeAttributes(C, B);
551 AttributeSet AttributeSet::removeAttribute(LLVMContext &C,
552 StringRef Kind) const {
553 if (!hasAttribute(Kind)) return *this;
555 B.addAttribute(Kind);
556 return removeAttributes(C, B);
559 AttributeSet AttributeSet::removeAttributes(LLVMContext &C,
560 const AttrBuilder &Attrs) const {
562 // FIXME it is not obvious how this should work for alignment.
563 // For now, say we can't pass in alignment, which no current use does.
564 assert(!Attrs.hasAlignmentAttr() && "Attempt to change alignment!");
566 AttrBuilder B(*this);
571 unsigned AttributeSet::getNumAttributes() const {
572 return SetNode ? SetNode->getNumAttributes() : 0;
575 bool AttributeSet::hasAttribute(Attribute::AttrKind Kind) const {
576 return SetNode ? SetNode->hasAttribute(Kind) : false;
579 bool AttributeSet::hasAttribute(StringRef Kind) const {
580 return SetNode ? SetNode->hasAttribute(Kind) : false;
583 Attribute AttributeSet::getAttribute(Attribute::AttrKind Kind) const {
584 return SetNode ? SetNode->getAttribute(Kind) : Attribute();
587 Attribute AttributeSet::getAttribute(StringRef Kind) const {
588 return SetNode ? SetNode->getAttribute(Kind) : Attribute();
591 unsigned AttributeSet::getAlignment() const {
592 return SetNode ? SetNode->getAlignment() : 0;
595 unsigned AttributeSet::getStackAlignment() const {
596 return SetNode ? SetNode->getStackAlignment() : 0;
599 uint64_t AttributeSet::getDereferenceableBytes() const {
600 return SetNode ? SetNode->getDereferenceableBytes() : 0;
603 uint64_t AttributeSet::getDereferenceableOrNullBytes() const {
604 return SetNode ? SetNode->getDereferenceableOrNullBytes() : 0;
607 std::pair<unsigned, Optional<unsigned>> AttributeSet::getAllocSizeArgs() const {
608 return SetNode ? SetNode->getAllocSizeArgs()
609 : std::pair<unsigned, Optional<unsigned>>(0, 0);
612 std::string AttributeSet::getAsString(bool InAttrGrp) const {
613 return SetNode ? SetNode->getAsString(InAttrGrp) : "";
616 AttributeSet::iterator AttributeSet::begin() const {
617 return SetNode ? SetNode->begin() : nullptr;
620 AttributeSet::iterator AttributeSet::end() const {
621 return SetNode ? SetNode->end() : nullptr;
624 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
625 LLVM_DUMP_METHOD void AttributeSet::dump() const {
628 dbgs() << getAsString(true) << " }\n";
632 //===----------------------------------------------------------------------===//
633 // AttributeSetNode Definition
634 //===----------------------------------------------------------------------===//
636 AttributeSetNode::AttributeSetNode(ArrayRef<Attribute> Attrs)
637 : AvailableAttrs(0), NumAttrs(Attrs.size()) {
638 // There's memory after the node where we can store the entries in.
639 std::copy(Attrs.begin(), Attrs.end(), getTrailingObjects<Attribute>());
641 for (Attribute I : *this) {
642 if (!I.isStringAttribute()) {
643 AvailableAttrs |= ((uint64_t)1) << I.getKindAsEnum();
648 AttributeSetNode *AttributeSetNode::get(LLVMContext &C,
649 ArrayRef<Attribute> Attrs) {
653 // Otherwise, build a key to look up the existing attributes.
654 LLVMContextImpl *pImpl = C.pImpl;
657 SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end());
658 std::sort(SortedAttrs.begin(), SortedAttrs.end());
660 for (Attribute Attr : SortedAttrs)
664 AttributeSetNode *PA =
665 pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint);
667 // If we didn't find any existing attributes of the same shape then create a
668 // new one and insert it.
670 // Coallocate entries after the AttributeSetNode itself.
671 void *Mem = ::operator new(totalSizeToAlloc<Attribute>(SortedAttrs.size()));
672 PA = new (Mem) AttributeSetNode(SortedAttrs);
673 pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint);
676 // Return the AttributeSetNode that we found or created.
680 AttributeSetNode *AttributeSetNode::get(LLVMContext &C, const AttrBuilder &B) {
681 // Add target-independent attributes.
682 SmallVector<Attribute, 8> Attrs;
683 for (Attribute::AttrKind Kind = Attribute::None;
684 Kind != Attribute::EndAttrKinds; Kind = Attribute::AttrKind(Kind + 1)) {
685 if (!B.contains(Kind))
690 case Attribute::Alignment:
691 Attr = Attribute::getWithAlignment(C, B.getAlignment());
693 case Attribute::StackAlignment:
694 Attr = Attribute::getWithStackAlignment(C, B.getStackAlignment());
696 case Attribute::Dereferenceable:
697 Attr = Attribute::getWithDereferenceableBytes(
698 C, B.getDereferenceableBytes());
700 case Attribute::DereferenceableOrNull:
701 Attr = Attribute::getWithDereferenceableOrNullBytes(
702 C, B.getDereferenceableOrNullBytes());
704 case Attribute::AllocSize: {
705 auto A = B.getAllocSizeArgs();
706 Attr = Attribute::getWithAllocSizeArgs(C, A.first, A.second);
710 Attr = Attribute::get(C, Kind);
712 Attrs.push_back(Attr);
715 // Add target-dependent (string) attributes.
716 for (const auto &TDA : B.td_attrs())
717 Attrs.emplace_back(Attribute::get(C, TDA.first, TDA.second));
719 return get(C, Attrs);
722 bool AttributeSetNode::hasAttribute(StringRef Kind) const {
723 for (Attribute I : *this)
724 if (I.hasAttribute(Kind))
729 Attribute AttributeSetNode::getAttribute(Attribute::AttrKind Kind) const {
730 if (hasAttribute(Kind)) {
731 for (Attribute I : *this)
732 if (I.hasAttribute(Kind))
738 Attribute AttributeSetNode::getAttribute(StringRef Kind) const {
739 for (Attribute I : *this)
740 if (I.hasAttribute(Kind))
745 unsigned AttributeSetNode::getAlignment() const {
746 for (Attribute I : *this)
747 if (I.hasAttribute(Attribute::Alignment))
748 return I.getAlignment();
752 unsigned AttributeSetNode::getStackAlignment() const {
753 for (Attribute I : *this)
754 if (I.hasAttribute(Attribute::StackAlignment))
755 return I.getStackAlignment();
759 uint64_t AttributeSetNode::getDereferenceableBytes() const {
760 for (Attribute I : *this)
761 if (I.hasAttribute(Attribute::Dereferenceable))
762 return I.getDereferenceableBytes();
766 uint64_t AttributeSetNode::getDereferenceableOrNullBytes() const {
767 for (Attribute I : *this)
768 if (I.hasAttribute(Attribute::DereferenceableOrNull))
769 return I.getDereferenceableOrNullBytes();
773 std::pair<unsigned, Optional<unsigned>>
774 AttributeSetNode::getAllocSizeArgs() const {
775 for (Attribute I : *this)
776 if (I.hasAttribute(Attribute::AllocSize))
777 return I.getAllocSizeArgs();
778 return std::make_pair(0, 0);
781 std::string AttributeSetNode::getAsString(bool InAttrGrp) const {
783 for (iterator I = begin(), E = end(); I != E; ++I) {
786 Str += I->getAsString(InAttrGrp);
791 //===----------------------------------------------------------------------===//
792 // AttributeListImpl Definition
793 //===----------------------------------------------------------------------===//
795 /// Map from AttributeList index to the internal array index. Adding one happens
796 /// to work, but it relies on unsigned integer wrapping. MSVC warns about
797 /// unsigned wrapping in constexpr functions, so write out the conditional. LLVM
798 /// folds it to add anyway.
799 static constexpr unsigned attrIdxToArrayIdx(unsigned Index) {
800 return Index == AttributeList::FunctionIndex ? 0 : Index + 1;
803 AttributeListImpl::AttributeListImpl(LLVMContext &C,
804 ArrayRef<AttributeSet> Sets)
805 : AvailableFunctionAttrs(0), Context(C), NumAttrSets(Sets.size()) {
806 assert(!Sets.empty() && "pointless AttributeListImpl");
808 // There's memory after the node where we can store the entries in.
809 std::copy(Sets.begin(), Sets.end(), getTrailingObjects<AttributeSet>());
811 // Initialize AvailableFunctionAttrs summary bitset.
812 static_assert(Attribute::EndAttrKinds <=
813 sizeof(AvailableFunctionAttrs) * CHAR_BIT,
814 "Too many attributes");
815 static_assert(attrIdxToArrayIdx(AttributeList::FunctionIndex) == 0U,
816 "function should be stored in slot 0");
817 for (Attribute I : Sets[0]) {
818 if (!I.isStringAttribute())
819 AvailableFunctionAttrs |= 1ULL << I.getKindAsEnum();
823 void AttributeListImpl::Profile(FoldingSetNodeID &ID) const {
824 Profile(ID, makeArrayRef(begin(), end()));
827 void AttributeListImpl::Profile(FoldingSetNodeID &ID,
828 ArrayRef<AttributeSet> Sets) {
829 for (const auto &Set : Sets)
830 ID.AddPointer(Set.SetNode);
833 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
834 LLVM_DUMP_METHOD void AttributeListImpl::dump() const {
835 AttributeList(const_cast<AttributeListImpl *>(this)).dump();
839 //===----------------------------------------------------------------------===//
840 // AttributeList Construction and Mutation Methods
841 //===----------------------------------------------------------------------===//
843 AttributeList AttributeList::getImpl(LLVMContext &C,
844 ArrayRef<AttributeSet> AttrSets) {
845 assert(!AttrSets.empty() && "pointless AttributeListImpl");
847 LLVMContextImpl *pImpl = C.pImpl;
849 AttributeListImpl::Profile(ID, AttrSets);
852 AttributeListImpl *PA =
853 pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
855 // If we didn't find any existing attributes of the same shape then
856 // create a new one and insert it.
858 // Coallocate entries after the AttributeListImpl itself.
859 void *Mem = ::operator new(
860 AttributeListImpl::totalSizeToAlloc<AttributeSet>(AttrSets.size()));
861 PA = new (Mem) AttributeListImpl(C, AttrSets);
862 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
865 // Return the AttributesList that we found or created.
866 return AttributeList(PA);
870 AttributeList::get(LLVMContext &C,
871 ArrayRef<std::pair<unsigned, Attribute>> Attrs) {
872 // If there are no attributes then return a null AttributesList pointer.
874 return AttributeList();
876 assert(std::is_sorted(Attrs.begin(), Attrs.end(),
877 [](const std::pair<unsigned, Attribute> &LHS,
878 const std::pair<unsigned, Attribute> &RHS) {
879 return LHS.first < RHS.first;
880 }) && "Misordered Attributes list!");
881 assert(none_of(Attrs,
882 [](const std::pair<unsigned, Attribute> &Pair) {
883 return Pair.second.hasAttribute(Attribute::None);
885 "Pointless attribute!");
887 // Create a vector if (unsigned, AttributeSetNode*) pairs from the attributes
889 SmallVector<std::pair<unsigned, AttributeSet>, 8> AttrPairVec;
890 for (ArrayRef<std::pair<unsigned, Attribute>>::iterator I = Attrs.begin(),
891 E = Attrs.end(); I != E; ) {
892 unsigned Index = I->first;
893 SmallVector<Attribute, 4> AttrVec;
894 while (I != E && I->first == Index) {
895 AttrVec.push_back(I->second);
899 AttrPairVec.emplace_back(Index, AttributeSet::get(C, AttrVec));
902 return get(C, AttrPairVec);
906 AttributeList::get(LLVMContext &C,
907 ArrayRef<std::pair<unsigned, AttributeSet>> Attrs) {
908 // If there are no attributes then return a null AttributesList pointer.
910 return AttributeList();
912 assert(std::is_sorted(Attrs.begin(), Attrs.end(),
913 [](const std::pair<unsigned, AttributeSet> &LHS,
914 const std::pair<unsigned, AttributeSet> &RHS) {
915 return LHS.first < RHS.first;
917 "Misordered Attributes list!");
918 assert(none_of(Attrs,
919 [](const std::pair<unsigned, AttributeSet> &Pair) {
920 return !Pair.second.hasAttributes();
922 "Pointless attribute!");
924 unsigned MaxIndex = Attrs.back().first;
926 SmallVector<AttributeSet, 4> AttrVec(attrIdxToArrayIdx(MaxIndex) + 1);
927 for (auto Pair : Attrs)
928 AttrVec[attrIdxToArrayIdx(Pair.first)] = Pair.second;
930 return getImpl(C, AttrVec);
933 AttributeList AttributeList::get(LLVMContext &C, AttributeSet FnAttrs,
934 AttributeSet RetAttrs,
935 ArrayRef<AttributeSet> ArgAttrs) {
936 // Scan from the end to find the last argument with attributes. Most
937 // arguments don't have attributes, so it's nice if we can have fewer unique
938 // AttributeListImpls by dropping empty attribute sets at the end of the list.
939 unsigned NumSets = 0;
940 for (size_t I = ArgAttrs.size(); I != 0; --I) {
941 if (ArgAttrs[I - 1].hasAttributes()) {
947 // Check function and return attributes if we didn't have argument
949 if (RetAttrs.hasAttributes())
951 else if (FnAttrs.hasAttributes())
955 // If all attribute sets were empty, we can use the empty attribute list.
957 return AttributeList();
959 SmallVector<AttributeSet, 8> AttrSets;
960 AttrSets.reserve(NumSets);
961 // If we have any attributes, we always have function attributes.
962 AttrSets.push_back(FnAttrs);
964 AttrSets.push_back(RetAttrs);
966 // Drop the empty argument attribute sets at the end.
967 ArgAttrs = ArgAttrs.take_front(NumSets - 2);
968 AttrSets.insert(AttrSets.end(), ArgAttrs.begin(), ArgAttrs.end());
971 return getImpl(C, AttrSets);
974 AttributeList AttributeList::get(LLVMContext &C, unsigned Index,
975 const AttrBuilder &B) {
976 if (!B.hasAttributes())
977 return AttributeList();
978 Index = attrIdxToArrayIdx(Index);
979 SmallVector<AttributeSet, 8> AttrSets(Index + 1);
980 AttrSets[Index] = AttributeSet::get(C, B);
981 return getImpl(C, AttrSets);
984 AttributeList AttributeList::get(LLVMContext &C, unsigned Index,
985 ArrayRef<Attribute::AttrKind> Kinds) {
986 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
987 for (Attribute::AttrKind K : Kinds)
988 Attrs.emplace_back(Index, Attribute::get(C, K));
989 return get(C, Attrs);
992 AttributeList AttributeList::get(LLVMContext &C, unsigned Index,
993 ArrayRef<StringRef> Kinds) {
994 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
995 for (StringRef K : Kinds)
996 Attrs.emplace_back(Index, Attribute::get(C, K));
997 return get(C, Attrs);
1000 AttributeList AttributeList::get(LLVMContext &C,
1001 ArrayRef<AttributeList> Attrs) {
1003 return AttributeList();
1004 if (Attrs.size() == 1)
1007 unsigned MaxSize = 0;
1008 for (AttributeList List : Attrs)
1009 MaxSize = std::max(MaxSize, List.getNumAttrSets());
1011 // If every list was empty, there is no point in merging the lists.
1013 return AttributeList();
1015 SmallVector<AttributeSet, 8> NewAttrSets(MaxSize);
1016 for (unsigned I = 0; I < MaxSize; ++I) {
1017 AttrBuilder CurBuilder;
1018 for (AttributeList List : Attrs)
1019 CurBuilder.merge(List.getAttributes(I - 1));
1020 NewAttrSets[I] = AttributeSet::get(C, CurBuilder);
1023 return getImpl(C, NewAttrSets);
1026 AttributeList AttributeList::addAttribute(LLVMContext &C, unsigned Index,
1027 Attribute::AttrKind Kind) const {
1028 if (hasAttribute(Index, Kind)) return *this;
1030 B.addAttribute(Kind);
1031 return addAttributes(C, Index, B);
1034 AttributeList AttributeList::addAttribute(LLVMContext &C, unsigned Index,
1036 StringRef Value) const {
1038 B.addAttribute(Kind, Value);
1039 return addAttributes(C, Index, B);
1042 AttributeList AttributeList::addAttribute(LLVMContext &C, unsigned Index,
1043 Attribute A) const {
1046 return addAttributes(C, Index, B);
1049 AttributeList AttributeList::addAttributes(LLVMContext &C, unsigned Index,
1050 const AttrBuilder &B) const {
1051 if (!B.hasAttributes())
1055 return AttributeList::get(C, {{Index, AttributeSet::get(C, B)}});
1058 // FIXME it is not obvious how this should work for alignment. For now, say
1059 // we can't change a known alignment.
1060 unsigned OldAlign = getAttributes(Index).getAlignment();
1061 unsigned NewAlign = B.getAlignment();
1062 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
1063 "Attempt to change alignment!");
1066 Index = attrIdxToArrayIdx(Index);
1067 SmallVector<AttributeSet, 4> AttrSets(this->begin(), this->end());
1068 if (Index >= AttrSets.size())
1069 AttrSets.resize(Index + 1);
1071 AttrBuilder Merged(AttrSets[Index]);
1073 AttrSets[Index] = AttributeSet::get(C, Merged);
1075 return getImpl(C, AttrSets);
1078 AttributeList AttributeList::addParamAttribute(LLVMContext &C,
1079 ArrayRef<unsigned> ArgNos,
1080 Attribute A) const {
1081 assert(std::is_sorted(ArgNos.begin(), ArgNos.end()));
1083 SmallVector<AttributeSet, 4> AttrSets(this->begin(), this->end());
1084 unsigned MaxIndex = attrIdxToArrayIdx(ArgNos.back() + FirstArgIndex);
1085 if (MaxIndex >= AttrSets.size())
1086 AttrSets.resize(MaxIndex + 1);
1088 for (unsigned ArgNo : ArgNos) {
1089 unsigned Index = attrIdxToArrayIdx(ArgNo + FirstArgIndex);
1090 AttrBuilder B(AttrSets[Index]);
1092 AttrSets[Index] = AttributeSet::get(C, B);
1095 return getImpl(C, AttrSets);
1098 AttributeList AttributeList::removeAttribute(LLVMContext &C, unsigned Index,
1099 Attribute::AttrKind Kind) const {
1100 if (!hasAttribute(Index, Kind)) return *this;
1102 B.addAttribute(Kind);
1103 return removeAttributes(C, Index, B);
1106 AttributeList AttributeList::removeAttribute(LLVMContext &C, unsigned Index,
1107 StringRef Kind) const {
1108 if (!hasAttribute(Index, Kind)) return *this;
1110 B.addAttribute(Kind);
1111 return removeAttributes(C, Index, B);
1115 AttributeList::removeAttributes(LLVMContext &C, unsigned Index,
1116 const AttrBuilder &AttrsToRemove) const {
1118 return AttributeList();
1120 // FIXME it is not obvious how this should work for alignment.
1121 // For now, say we can't pass in alignment, which no current use does.
1122 assert(!AttrsToRemove.hasAlignmentAttr() && "Attempt to change alignment!");
1124 Index = attrIdxToArrayIdx(Index);
1125 SmallVector<AttributeSet, 4> AttrSets(this->begin(), this->end());
1126 if (Index >= AttrSets.size())
1127 AttrSets.resize(Index + 1);
1129 AttrBuilder B(AttrSets[Index]);
1130 B.remove(AttrsToRemove);
1131 AttrSets[Index] = AttributeSet::get(C, B);
1133 return getImpl(C, AttrSets);
1136 AttributeList AttributeList::removeAttributes(LLVMContext &C,
1137 unsigned WithoutIndex) const {
1139 return AttributeList();
1140 WithoutIndex = attrIdxToArrayIdx(WithoutIndex);
1141 if (WithoutIndex >= getNumAttrSets())
1143 SmallVector<AttributeSet, 4> AttrSets(this->begin(), this->end());
1144 AttrSets[WithoutIndex] = AttributeSet();
1145 return getImpl(C, AttrSets);
1148 AttributeList AttributeList::addDereferenceableAttr(LLVMContext &C,
1150 uint64_t Bytes) const {
1152 B.addDereferenceableAttr(Bytes);
1153 return addAttributes(C, Index, B);
1157 AttributeList::addDereferenceableOrNullAttr(LLVMContext &C, unsigned Index,
1158 uint64_t Bytes) const {
1160 B.addDereferenceableOrNullAttr(Bytes);
1161 return addAttributes(C, Index, B);
1165 AttributeList::addAllocSizeAttr(LLVMContext &C, unsigned Index,
1166 unsigned ElemSizeArg,
1167 const Optional<unsigned> &NumElemsArg) {
1169 B.addAllocSizeAttr(ElemSizeArg, NumElemsArg);
1170 return addAttributes(C, Index, B);
1173 //===----------------------------------------------------------------------===//
1174 // AttributeList Accessor Methods
1175 //===----------------------------------------------------------------------===//
1177 LLVMContext &AttributeList::getContext() const { return pImpl->getContext(); }
1179 AttributeSet AttributeList::getParamAttributes(unsigned ArgNo) const {
1180 return getAttributes(ArgNo + FirstArgIndex);
1183 AttributeSet AttributeList::getRetAttributes() const {
1184 return getAttributes(ReturnIndex);
1187 AttributeSet AttributeList::getFnAttributes() const {
1188 return getAttributes(FunctionIndex);
1191 bool AttributeList::hasAttribute(unsigned Index,
1192 Attribute::AttrKind Kind) const {
1193 return getAttributes(Index).hasAttribute(Kind);
1196 bool AttributeList::hasAttribute(unsigned Index, StringRef Kind) const {
1197 return getAttributes(Index).hasAttribute(Kind);
1200 bool AttributeList::hasAttributes(unsigned Index) const {
1201 return getAttributes(Index).hasAttributes();
1204 bool AttributeList::hasFnAttribute(Attribute::AttrKind Kind) const {
1205 return pImpl && pImpl->hasFnAttribute(Kind);
1208 bool AttributeList::hasFnAttribute(StringRef Kind) const {
1209 return hasAttribute(AttributeList::FunctionIndex, Kind);
1212 bool AttributeList::hasParamAttribute(unsigned ArgNo,
1213 Attribute::AttrKind Kind) const {
1214 return hasAttribute(ArgNo + FirstArgIndex, Kind);
1217 bool AttributeList::hasAttrSomewhere(Attribute::AttrKind Attr,
1218 unsigned *Index) const {
1219 if (!pImpl) return false;
1221 for (unsigned I = index_begin(), E = index_end(); I != E; ++I) {
1222 if (hasAttribute(I, Attr)) {
1232 Attribute AttributeList::getAttribute(unsigned Index,
1233 Attribute::AttrKind Kind) const {
1234 return getAttributes(Index).getAttribute(Kind);
1237 Attribute AttributeList::getAttribute(unsigned Index, StringRef Kind) const {
1238 return getAttributes(Index).getAttribute(Kind);
1241 unsigned AttributeList::getRetAlignment() const {
1242 return getAttributes(ReturnIndex).getAlignment();
1245 unsigned AttributeList::getParamAlignment(unsigned ArgNo) const {
1246 return getAttributes(ArgNo + FirstArgIndex).getAlignment();
1249 unsigned AttributeList::getStackAlignment(unsigned Index) const {
1250 return getAttributes(Index).getStackAlignment();
1253 uint64_t AttributeList::getDereferenceableBytes(unsigned Index) const {
1254 return getAttributes(Index).getDereferenceableBytes();
1257 uint64_t AttributeList::getDereferenceableOrNullBytes(unsigned Index) const {
1258 return getAttributes(Index).getDereferenceableOrNullBytes();
1261 std::pair<unsigned, Optional<unsigned>>
1262 AttributeList::getAllocSizeArgs(unsigned Index) const {
1263 return getAttributes(Index).getAllocSizeArgs();
1266 std::string AttributeList::getAsString(unsigned Index, bool InAttrGrp) const {
1267 return getAttributes(Index).getAsString(InAttrGrp);
1270 AttributeSet AttributeList::getAttributes(unsigned Index) const {
1271 Index = attrIdxToArrayIdx(Index);
1272 if (!pImpl || Index >= getNumAttrSets())
1273 return AttributeSet();
1274 return pImpl->begin()[Index];
1277 AttributeList::iterator AttributeList::begin() const {
1278 return pImpl ? pImpl->begin() : nullptr;
1281 AttributeList::iterator AttributeList::end() const {
1282 return pImpl ? pImpl->end() : nullptr;
1285 //===----------------------------------------------------------------------===//
1286 // AttributeList Introspection Methods
1287 //===----------------------------------------------------------------------===//
1289 unsigned AttributeList::getNumAttrSets() const {
1290 return pImpl ? pImpl->NumAttrSets : 0;
1293 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1294 LLVM_DUMP_METHOD void AttributeList::dump() const {
1297 for (unsigned i = index_begin(), e = index_end(); i != e; ++i) {
1298 if (getAttributes(i).hasAttributes())
1299 dbgs() << " { " << i << " => " << getAsString(i) << " }\n";
1306 //===----------------------------------------------------------------------===//
1307 // AttrBuilder Method Implementations
1308 //===----------------------------------------------------------------------===//
1310 // FIXME: Remove this ctor, use AttributeSet.
1311 AttrBuilder::AttrBuilder(AttributeList AL, unsigned Index) {
1312 AttributeSet AS = AL.getAttributes(Index);
1313 for (const Attribute &A : AS)
1317 AttrBuilder::AttrBuilder(AttributeSet AS) {
1318 for (const Attribute &A : AS)
1322 void AttrBuilder::clear() {
1324 TargetDepAttrs.clear();
1325 Alignment = StackAlignment = DerefBytes = DerefOrNullBytes = 0;
1329 AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
1330 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
1331 assert(Val != Attribute::Alignment && Val != Attribute::StackAlignment &&
1332 Val != Attribute::Dereferenceable && Val != Attribute::AllocSize &&
1333 "Adding integer attribute without adding a value!");
1338 AttrBuilder &AttrBuilder::addAttribute(Attribute Attr) {
1339 if (Attr.isStringAttribute()) {
1340 addAttribute(Attr.getKindAsString(), Attr.getValueAsString());
1344 Attribute::AttrKind Kind = Attr.getKindAsEnum();
1347 if (Kind == Attribute::Alignment)
1348 Alignment = Attr.getAlignment();
1349 else if (Kind == Attribute::StackAlignment)
1350 StackAlignment = Attr.getStackAlignment();
1351 else if (Kind == Attribute::Dereferenceable)
1352 DerefBytes = Attr.getDereferenceableBytes();
1353 else if (Kind == Attribute::DereferenceableOrNull)
1354 DerefOrNullBytes = Attr.getDereferenceableOrNullBytes();
1355 else if (Kind == Attribute::AllocSize)
1356 AllocSizeArgs = Attr.getValueAsInt();
1360 AttrBuilder &AttrBuilder::addAttribute(StringRef A, StringRef V) {
1361 TargetDepAttrs[A] = V;
1365 AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
1366 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
1369 if (Val == Attribute::Alignment)
1371 else if (Val == Attribute::StackAlignment)
1373 else if (Val == Attribute::Dereferenceable)
1375 else if (Val == Attribute::DereferenceableOrNull)
1376 DerefOrNullBytes = 0;
1377 else if (Val == Attribute::AllocSize)
1383 AttrBuilder &AttrBuilder::removeAttributes(AttributeList A, uint64_t Index) {
1384 remove(A.getAttributes(Index));
1388 AttrBuilder &AttrBuilder::removeAttribute(StringRef A) {
1389 std::map<std::string, std::string>::iterator I = TargetDepAttrs.find(A);
1390 if (I != TargetDepAttrs.end())
1391 TargetDepAttrs.erase(I);
1395 std::pair<unsigned, Optional<unsigned>> AttrBuilder::getAllocSizeArgs() const {
1396 return unpackAllocSizeArgs(AllocSizeArgs);
1399 AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
1400 if (Align == 0) return *this;
1402 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1403 assert(Align <= 0x40000000 && "Alignment too large.");
1405 Attrs[Attribute::Alignment] = true;
1410 AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
1411 // Default alignment, allow the target to define how to align it.
1412 if (Align == 0) return *this;
1414 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1415 assert(Align <= 0x100 && "Alignment too large.");
1417 Attrs[Attribute::StackAlignment] = true;
1418 StackAlignment = Align;
1422 AttrBuilder &AttrBuilder::addDereferenceableAttr(uint64_t Bytes) {
1423 if (Bytes == 0) return *this;
1425 Attrs[Attribute::Dereferenceable] = true;
1430 AttrBuilder &AttrBuilder::addDereferenceableOrNullAttr(uint64_t Bytes) {
1434 Attrs[Attribute::DereferenceableOrNull] = true;
1435 DerefOrNullBytes = Bytes;
1439 AttrBuilder &AttrBuilder::addAllocSizeAttr(unsigned ElemSize,
1440 const Optional<unsigned> &NumElems) {
1441 return addAllocSizeAttrFromRawRepr(packAllocSizeArgs(ElemSize, NumElems));
1444 AttrBuilder &AttrBuilder::addAllocSizeAttrFromRawRepr(uint64_t RawArgs) {
1445 // (0, 0) is our "not present" value, so we need to check for it here.
1446 assert(RawArgs && "Invalid allocsize arguments -- given allocsize(0, 0)");
1448 Attrs[Attribute::AllocSize] = true;
1449 // Reuse existing machinery to store this as a single 64-bit integer so we can
1450 // save a few bytes over using a pair<unsigned, Optional<unsigned>>.
1451 AllocSizeArgs = RawArgs;
1455 AttrBuilder &AttrBuilder::merge(const AttrBuilder &B) {
1456 // FIXME: What if both have alignments, but they don't match?!
1458 Alignment = B.Alignment;
1460 if (!StackAlignment)
1461 StackAlignment = B.StackAlignment;
1464 DerefBytes = B.DerefBytes;
1466 if (!DerefOrNullBytes)
1467 DerefOrNullBytes = B.DerefOrNullBytes;
1470 AllocSizeArgs = B.AllocSizeArgs;
1474 for (auto I : B.td_attrs())
1475 TargetDepAttrs[I.first] = I.second;
1480 AttrBuilder &AttrBuilder::remove(const AttrBuilder &B) {
1481 // FIXME: What if both have alignments, but they don't match?!
1485 if (B.StackAlignment)
1491 if (B.DerefOrNullBytes)
1492 DerefOrNullBytes = 0;
1494 if (B.AllocSizeArgs)
1499 for (auto I : B.td_attrs())
1500 TargetDepAttrs.erase(I.first);
1505 bool AttrBuilder::overlaps(const AttrBuilder &B) const {
1506 // First check if any of the target independent attributes overlap.
1507 if ((Attrs & B.Attrs).any())
1510 // Then check if any target dependent ones do.
1511 for (const auto &I : td_attrs())
1512 if (B.contains(I.first))
1518 bool AttrBuilder::contains(StringRef A) const {
1519 return TargetDepAttrs.find(A) != TargetDepAttrs.end();
1522 bool AttrBuilder::hasAttributes() const {
1523 return !Attrs.none() || !TargetDepAttrs.empty();
1526 bool AttrBuilder::hasAttributes(AttributeList AL, uint64_t Index) const {
1527 AttributeSet AS = AL.getAttributes(Index);
1529 for (Attribute Attr : AS) {
1530 if (Attr.isEnumAttribute() || Attr.isIntAttribute()) {
1531 if (contains(Attr.getKindAsEnum()))
1534 assert(Attr.isStringAttribute() && "Invalid attribute kind!");
1535 return contains(Attr.getKindAsString());
1542 bool AttrBuilder::hasAlignmentAttr() const {
1543 return Alignment != 0;
1546 bool AttrBuilder::operator==(const AttrBuilder &B) {
1547 if (Attrs != B.Attrs)
1550 for (td_const_iterator I = TargetDepAttrs.begin(),
1551 E = TargetDepAttrs.end(); I != E; ++I)
1552 if (B.TargetDepAttrs.find(I->first) == B.TargetDepAttrs.end())
1555 return Alignment == B.Alignment && StackAlignment == B.StackAlignment &&
1556 DerefBytes == B.DerefBytes;
1559 //===----------------------------------------------------------------------===//
1560 // AttributeFuncs Function Defintions
1561 //===----------------------------------------------------------------------===//
1563 /// \brief Which attributes cannot be applied to a type.
1564 AttrBuilder AttributeFuncs::typeIncompatible(Type *Ty) {
1565 AttrBuilder Incompatible;
1567 if (!Ty->isIntegerTy())
1568 // Attribute that only apply to integers.
1569 Incompatible.addAttribute(Attribute::SExt)
1570 .addAttribute(Attribute::ZExt);
1572 if (!Ty->isPointerTy())
1573 // Attribute that only apply to pointers.
1574 Incompatible.addAttribute(Attribute::ByVal)
1575 .addAttribute(Attribute::Nest)
1576 .addAttribute(Attribute::NoAlias)
1577 .addAttribute(Attribute::NoCapture)
1578 .addAttribute(Attribute::NonNull)
1579 .addDereferenceableAttr(1) // the int here is ignored
1580 .addDereferenceableOrNullAttr(1) // the int here is ignored
1581 .addAttribute(Attribute::ReadNone)
1582 .addAttribute(Attribute::ReadOnly)
1583 .addAttribute(Attribute::StructRet)
1584 .addAttribute(Attribute::InAlloca);
1586 return Incompatible;
1589 template<typename AttrClass>
1590 static bool isEqual(const Function &Caller, const Function &Callee) {
1591 return Caller.getFnAttribute(AttrClass::getKind()) ==
1592 Callee.getFnAttribute(AttrClass::getKind());
1595 /// \brief Compute the logical AND of the attributes of the caller and the
1598 /// This function sets the caller's attribute to false if the callee's attribute
1600 template<typename AttrClass>
1601 static void setAND(Function &Caller, const Function &Callee) {
1602 if (AttrClass::isSet(Caller, AttrClass::getKind()) &&
1603 !AttrClass::isSet(Callee, AttrClass::getKind()))
1604 AttrClass::set(Caller, AttrClass::getKind(), false);
1607 /// \brief Compute the logical OR of the attributes of the caller and the
1610 /// This function sets the caller's attribute to true if the callee's attribute
1612 template<typename AttrClass>
1613 static void setOR(Function &Caller, const Function &Callee) {
1614 if (!AttrClass::isSet(Caller, AttrClass::getKind()) &&
1615 AttrClass::isSet(Callee, AttrClass::getKind()))
1616 AttrClass::set(Caller, AttrClass::getKind(), true);
1619 /// \brief If the inlined function had a higher stack protection level than the
1620 /// calling function, then bump up the caller's stack protection level.
1621 static void adjustCallerSSPLevel(Function &Caller, const Function &Callee) {
1622 // If upgrading the SSP attribute, clear out the old SSP Attributes first.
1623 // Having multiple SSP attributes doesn't actually hurt, but it adds useless
1624 // clutter to the IR.
1625 AttrBuilder OldSSPAttr;
1626 OldSSPAttr.addAttribute(Attribute::StackProtect)
1627 .addAttribute(Attribute::StackProtectStrong)
1628 .addAttribute(Attribute::StackProtectReq);
1630 if (Callee.hasFnAttribute(Attribute::StackProtectReq)) {
1631 Caller.removeAttributes(AttributeList::FunctionIndex, OldSSPAttr);
1632 Caller.addFnAttr(Attribute::StackProtectReq);
1633 } else if (Callee.hasFnAttribute(Attribute::StackProtectStrong) &&
1634 !Caller.hasFnAttribute(Attribute::StackProtectReq)) {
1635 Caller.removeAttributes(AttributeList::FunctionIndex, OldSSPAttr);
1636 Caller.addFnAttr(Attribute::StackProtectStrong);
1637 } else if (Callee.hasFnAttribute(Attribute::StackProtect) &&
1638 !Caller.hasFnAttribute(Attribute::StackProtectReq) &&
1639 !Caller.hasFnAttribute(Attribute::StackProtectStrong))
1640 Caller.addFnAttr(Attribute::StackProtect);
1643 /// \brief If the inlined function required stack probes, then ensure that
1644 /// the calling function has those too.
1645 static void adjustCallerStackProbes(Function &Caller, const Function &Callee) {
1646 if (!Caller.hasFnAttribute("probe-stack") &&
1647 Callee.hasFnAttribute("probe-stack")) {
1648 Caller.addFnAttr(Callee.getFnAttribute("probe-stack"));
1652 /// \brief If the inlined function defines the size of guard region
1653 /// on the stack, then ensure that the calling function defines a guard region
1654 /// that is no larger.
1656 adjustCallerStackProbeSize(Function &Caller, const Function &Callee) {
1657 if (Callee.hasFnAttribute("stack-probe-size")) {
1658 uint64_t CalleeStackProbeSize;
1659 Callee.getFnAttribute("stack-probe-size")
1661 .getAsInteger(0, CalleeStackProbeSize);
1662 if (Caller.hasFnAttribute("stack-probe-size")) {
1663 uint64_t CallerStackProbeSize;
1664 Caller.getFnAttribute("stack-probe-size")
1666 .getAsInteger(0, CallerStackProbeSize);
1667 if (CallerStackProbeSize > CalleeStackProbeSize) {
1668 Caller.addFnAttr(Callee.getFnAttribute("stack-probe-size"));
1671 Caller.addFnAttr(Callee.getFnAttribute("stack-probe-size"));
1676 #define GET_ATTR_COMPAT_FUNC
1677 #include "AttributesCompatFunc.inc"
1679 bool AttributeFuncs::areInlineCompatible(const Function &Caller,
1680 const Function &Callee) {
1681 return hasCompatibleFnAttrs(Caller, Callee);
1684 void AttributeFuncs::mergeAttributesForInlining(Function &Caller,
1685 const Function &Callee) {
1686 mergeFnAttrs(Caller, Callee);