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 AttributeListImpl::AttributeListImpl(
792 LLVMContext &C, ArrayRef<std::pair<unsigned, AttributeSet>> Slots)
793 : Context(C), NumSlots(Slots.size()), AvailableFunctionAttrs(0) {
795 assert(!Slots.empty() && "pointless AttributeListImpl");
796 if (Slots.size() >= 2) {
797 auto &PrevPair = Slots.front();
798 for (auto &CurPair : Slots.drop_front()) {
799 assert(PrevPair.first <= CurPair.first && "Attribute set not ordered!");
804 // There's memory after the node where we can store the entries in.
805 std::copy(Slots.begin(), Slots.end(), getTrailingObjects<IndexAttrPair>());
807 // Initialize AvailableFunctionAttrs summary bitset.
808 static_assert(Attribute::EndAttrKinds <=
809 sizeof(AvailableFunctionAttrs) * CHAR_BIT,
810 "Too many attributes");
811 static_assert(AttributeList::FunctionIndex == ~0u,
812 "FunctionIndex should be biggest possible index");
813 const auto &Last = Slots.back();
814 if (Last.first == AttributeList::FunctionIndex) {
815 AttributeSet Node = Last.second;
816 for (Attribute I : Node) {
817 if (!I.isStringAttribute())
818 AvailableFunctionAttrs |= ((uint64_t)1) << I.getKindAsEnum();
823 void AttributeListImpl::Profile(FoldingSetNodeID &ID) const {
824 Profile(ID, makeArrayRef(getSlotPair(0), getNumSlots()));
827 void AttributeListImpl::Profile(
828 FoldingSetNodeID &ID, ArrayRef<std::pair<unsigned, AttributeSet>> Nodes) {
829 for (const auto &Node : Nodes) {
830 ID.AddInteger(Node.first);
831 ID.AddPointer(Node.second.SetNode);
835 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
836 LLVM_DUMP_METHOD void AttributeListImpl::dump() const {
837 AttributeList(const_cast<AttributeListImpl *>(this)).dump();
841 //===----------------------------------------------------------------------===//
842 // AttributeList Construction and Mutation Methods
843 //===----------------------------------------------------------------------===//
845 AttributeList AttributeList::getImpl(
846 LLVMContext &C, ArrayRef<std::pair<unsigned, AttributeSet>> Attrs) {
847 assert(!Attrs.empty() && "creating pointless AttributeList");
849 unsigned LastIndex = 0;
851 for (auto &&AttrPair : Attrs) {
852 assert((IsFirst || LastIndex < AttrPair.first) &&
853 "unsorted or duplicate AttributeList indices");
854 assert(AttrPair.second.hasAttributes() && "pointless AttributeList slot");
855 LastIndex = AttrPair.first;
860 LLVMContextImpl *pImpl = C.pImpl;
862 AttributeListImpl::Profile(ID, Attrs);
865 AttributeListImpl *PA =
866 pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
868 // If we didn't find any existing attributes of the same shape then
869 // create a new one and insert it.
871 // Coallocate entries after the AttributeListImpl itself.
872 void *Mem = ::operator new(
873 AttributeListImpl::totalSizeToAlloc<IndexAttrPair>(Attrs.size()));
874 PA = new (Mem) AttributeListImpl(C, Attrs);
875 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
878 // Return the AttributesList that we found or created.
879 return AttributeList(PA);
883 AttributeList::get(LLVMContext &C,
884 ArrayRef<std::pair<unsigned, Attribute>> Attrs) {
885 // If there are no attributes then return a null AttributesList pointer.
887 return AttributeList();
889 assert(std::is_sorted(Attrs.begin(), Attrs.end(),
890 [](const std::pair<unsigned, Attribute> &LHS,
891 const std::pair<unsigned, Attribute> &RHS) {
892 return LHS.first < RHS.first;
893 }) && "Misordered Attributes list!");
894 assert(none_of(Attrs,
895 [](const std::pair<unsigned, Attribute> &Pair) {
896 return Pair.second.hasAttribute(Attribute::None);
898 "Pointless attribute!");
900 // Create a vector if (unsigned, AttributeSetNode*) pairs from the attributes
902 SmallVector<std::pair<unsigned, AttributeSet>, 8> AttrPairVec;
903 for (ArrayRef<std::pair<unsigned, Attribute>>::iterator I = Attrs.begin(),
904 E = Attrs.end(); I != E; ) {
905 unsigned Index = I->first;
906 SmallVector<Attribute, 4> AttrVec;
907 while (I != E && I->first == Index) {
908 AttrVec.push_back(I->second);
912 AttrPairVec.emplace_back(Index, AttributeSet::get(C, AttrVec));
915 return getImpl(C, AttrPairVec);
919 AttributeList::get(LLVMContext &C,
920 ArrayRef<std::pair<unsigned, AttributeSet>> Attrs) {
921 // If there are no attributes then return a null AttributesList pointer.
923 return AttributeList();
925 return getImpl(C, Attrs);
928 AttributeList AttributeList::get(LLVMContext &C, AttributeSet FnAttrs,
929 AttributeSet RetAttrs,
930 ArrayRef<AttributeSet> ArgAttrs) {
931 SmallVector<std::pair<unsigned, AttributeSet>, 8> AttrPairs;
932 if (RetAttrs.hasAttributes())
933 AttrPairs.emplace_back(ReturnIndex, RetAttrs);
935 for (AttributeSet AS : ArgAttrs) {
936 if (AS.hasAttributes())
937 AttrPairs.emplace_back(Index, AS);
940 if (FnAttrs.hasAttributes())
941 AttrPairs.emplace_back(FunctionIndex, FnAttrs);
942 if (AttrPairs.empty())
943 return AttributeList();
944 return getImpl(C, AttrPairs);
947 AttributeList AttributeList::get(LLVMContext &C, unsigned Index,
948 const AttrBuilder &B) {
949 if (!B.hasAttributes())
950 return AttributeList();
951 AttributeSet AS = AttributeSet::get(C, B);
952 std::pair<unsigned, AttributeSet> Arr[1] = {{Index, AS}};
953 return getImpl(C, Arr);
956 AttributeList AttributeList::get(LLVMContext &C, unsigned Index,
957 ArrayRef<Attribute::AttrKind> Kinds) {
958 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
959 for (Attribute::AttrKind K : Kinds)
960 Attrs.emplace_back(Index, Attribute::get(C, K));
961 return get(C, Attrs);
964 AttributeList AttributeList::get(LLVMContext &C, unsigned Index,
965 ArrayRef<StringRef> Kinds) {
966 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
967 for (StringRef K : Kinds)
968 Attrs.emplace_back(Index, Attribute::get(C, K));
969 return get(C, Attrs);
972 AttributeList AttributeList::get(LLVMContext &C,
973 ArrayRef<AttributeList> Attrs) {
975 return AttributeList();
976 if (Attrs.size() == 1) return Attrs[0];
978 SmallVector<std::pair<unsigned, AttributeSet>, 8> AttrNodeVec;
979 AttributeListImpl *A0 = Attrs[0].pImpl;
981 AttrNodeVec.append(A0->getSlotPair(0), A0->getSlotPair(A0->getNumSlots()));
982 // Copy all attributes from Attrs into AttrNodeVec while keeping AttrNodeVec
983 // ordered by index. Because we know that each list in Attrs is ordered by
984 // index we only need to merge each successive list in rather than doing a
986 for (unsigned I = 1, E = Attrs.size(); I != E; ++I) {
987 AttributeListImpl *ALI = Attrs[I].pImpl;
989 SmallVector<std::pair<unsigned, AttributeSet>, 8>::iterator
990 ANVI = AttrNodeVec.begin(), ANVE;
991 for (const IndexAttrPair *AI = ALI->getSlotPair(0),
992 *AE = ALI->getSlotPair(ALI->getNumSlots());
994 ANVE = AttrNodeVec.end();
995 while (ANVI != ANVE && ANVI->first <= AI->first)
997 ANVI = AttrNodeVec.insert(ANVI, *AI) + 1;
1001 return getImpl(C, AttrNodeVec);
1004 AttributeList AttributeList::addAttribute(LLVMContext &C, unsigned Index,
1005 Attribute::AttrKind Kind) const {
1006 if (hasAttribute(Index, Kind)) return *this;
1008 B.addAttribute(Kind);
1009 return addAttributes(C, Index, B);
1012 AttributeList AttributeList::addAttribute(LLVMContext &C, unsigned Index,
1014 StringRef Value) const {
1016 B.addAttribute(Kind, Value);
1017 return addAttributes(C, Index, B);
1020 AttributeList AttributeList::addAttribute(LLVMContext &C,
1021 ArrayRef<unsigned> Indices,
1022 Attribute A) const {
1023 assert(std::is_sorted(Indices.begin(), Indices.end()));
1025 unsigned I = 0, E = pImpl ? pImpl->getNumSlots() : 0;
1026 SmallVector<IndexAttrPair, 4> AttrVec;
1027 for (unsigned Index : Indices) {
1028 // Add all attribute slots before the current index.
1029 for (; I < E && getSlotIndex(I) < Index; ++I)
1030 AttrVec.emplace_back(getSlotIndex(I), pImpl->getSlotAttributes(I));
1032 // Add the attribute at this index. If we already have attributes at this
1033 // index, merge them into a new set.
1035 if (I < E && getSlotIndex(I) == Index) {
1036 B.merge(AttrBuilder(pImpl->getSlotAttributes(I)));
1040 AttrVec.emplace_back(Index, AttributeSet::get(C, B));
1043 // Add remaining attributes.
1045 AttrVec.emplace_back(getSlotIndex(I), pImpl->getSlotAttributes(I));
1047 return get(C, AttrVec);
1050 AttributeList AttributeList::addAttributes(LLVMContext &C, unsigned Index,
1051 const AttrBuilder &B) const {
1052 if (!B.hasAttributes())
1056 return AttributeList::get(C, {{Index, AttributeSet::get(C, B)}});
1059 // FIXME it is not obvious how this should work for alignment. For now, say
1060 // we can't change a known alignment.
1061 unsigned OldAlign = getAttributes(Index).getAlignment();
1062 unsigned NewAlign = B.getAlignment();
1063 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
1064 "Attempt to change alignment!");
1067 SmallVector<IndexAttrPair, 4> AttrVec;
1068 uint64_t NumAttrs = pImpl->getNumSlots();
1071 // Add all the attribute slots before the one we need to merge.
1072 for (I = 0; I < NumAttrs; ++I) {
1073 if (getSlotIndex(I) >= Index)
1075 AttrVec.emplace_back(getSlotIndex(I), pImpl->getSlotAttributes(I));
1078 AttrBuilder NewAttrs;
1079 if (I < NumAttrs && getSlotIndex(I) == Index) {
1080 // We need to merge the attribute sets.
1081 NewAttrs.merge(pImpl->getSlotAttributes(I));
1086 // Add the new or merged attribute set at this index.
1087 AttrVec.emplace_back(Index, AttributeSet::get(C, NewAttrs));
1089 // Add the remaining entries.
1090 for (; I < NumAttrs; ++I)
1091 AttrVec.emplace_back(getSlotIndex(I), pImpl->getSlotAttributes(I));
1093 return get(C, AttrVec);
1096 AttributeList AttributeList::removeAttribute(LLVMContext &C, unsigned Index,
1097 Attribute::AttrKind Kind) const {
1098 if (!hasAttribute(Index, Kind)) return *this;
1100 B.addAttribute(Kind);
1101 return removeAttributes(C, Index, B);
1104 AttributeList AttributeList::removeAttribute(LLVMContext &C, unsigned Index,
1105 StringRef Kind) const {
1106 if (!hasAttribute(Index, Kind)) return *this;
1108 B.addAttribute(Kind);
1109 return removeAttributes(C, Index, B);
1112 AttributeList AttributeList::removeAttributes(LLVMContext &C, unsigned Index,
1113 const AttrBuilder &Attrs) const {
1115 return AttributeList();
1117 // FIXME it is not obvious how this should work for alignment.
1118 // For now, say we can't pass in alignment, which no current use does.
1119 assert(!Attrs.hasAlignmentAttr() && "Attempt to change alignment!");
1121 // Add the attribute slots before the one we're trying to add.
1122 SmallVector<IndexAttrPair, 4> AttrSets;
1123 uint64_t NumAttrs = pImpl->getNumSlots();
1125 uint64_t LastIndex = 0;
1126 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
1127 if (getSlotIndex(I) >= Index) {
1128 if (getSlotIndex(I) == Index)
1129 B = AttrBuilder(getSlotAttributes(LastIndex++));
1133 AttrSets.push_back({getSlotIndex(I), getSlotAttributes(I)});
1136 // Remove the attributes from the existing set and add them.
1138 if (B.hasAttributes())
1139 AttrSets.push_back({Index, AttributeSet::get(C, B)});
1141 // Add the remaining attribute slots.
1142 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
1143 AttrSets.push_back({getSlotIndex(I), getSlotAttributes(I)});
1145 return get(C, AttrSets);
1148 AttributeList AttributeList::removeAttributes(LLVMContext &C,
1149 unsigned WithoutIndex) const {
1151 return AttributeList();
1153 SmallVector<std::pair<unsigned, AttributeSet>, 4> AttrSet;
1154 for (unsigned I = 0, E = pImpl->getNumSlots(); I != E; ++I) {
1155 unsigned Index = getSlotIndex(I);
1156 if (Index != WithoutIndex)
1157 AttrSet.push_back({Index, pImpl->getSlotAttributes(I)});
1159 return get(C, AttrSet);
1162 AttributeList AttributeList::addDereferenceableAttr(LLVMContext &C,
1164 uint64_t Bytes) const {
1166 B.addDereferenceableAttr(Bytes);
1167 return addAttributes(C, Index, B);
1171 AttributeList::addDereferenceableOrNullAttr(LLVMContext &C, unsigned Index,
1172 uint64_t Bytes) const {
1174 B.addDereferenceableOrNullAttr(Bytes);
1175 return addAttributes(C, Index, B);
1179 AttributeList::addAllocSizeAttr(LLVMContext &C, unsigned Index,
1180 unsigned ElemSizeArg,
1181 const Optional<unsigned> &NumElemsArg) {
1183 B.addAllocSizeAttr(ElemSizeArg, NumElemsArg);
1184 return addAttributes(C, Index, B);
1187 //===----------------------------------------------------------------------===//
1188 // AttributeList Accessor Methods
1189 //===----------------------------------------------------------------------===//
1191 LLVMContext &AttributeList::getContext() const { return pImpl->getContext(); }
1193 AttributeSet AttributeList::getParamAttributes(unsigned ArgNo) const {
1194 return getAttributes(ArgNo + FirstArgIndex);
1197 AttributeSet AttributeList::getRetAttributes() const {
1198 return getAttributes(ReturnIndex);
1201 AttributeSet AttributeList::getFnAttributes() const {
1202 return getAttributes(FunctionIndex);
1205 bool AttributeList::hasAttribute(unsigned Index,
1206 Attribute::AttrKind Kind) const {
1207 return getAttributes(Index).hasAttribute(Kind);
1210 bool AttributeList::hasAttribute(unsigned Index, StringRef Kind) const {
1211 return getAttributes(Index).hasAttribute(Kind);
1214 bool AttributeList::hasAttributes(unsigned Index) const {
1215 return getAttributes(Index).hasAttributes();
1218 bool AttributeList::hasFnAttribute(Attribute::AttrKind Kind) const {
1219 return pImpl && pImpl->hasFnAttribute(Kind);
1222 bool AttributeList::hasFnAttribute(StringRef Kind) const {
1223 return hasAttribute(AttributeList::FunctionIndex, Kind);
1226 bool AttributeList::hasParamAttribute(unsigned ArgNo,
1227 Attribute::AttrKind Kind) const {
1228 return hasAttribute(ArgNo + 1, Kind);
1231 bool AttributeList::hasAttrSomewhere(Attribute::AttrKind Attr,
1232 unsigned *Index) const {
1233 if (!pImpl) return false;
1235 for (unsigned I = 0, E = pImpl->getNumSlots(); I != E; ++I)
1236 for (AttributeListImpl::iterator II = pImpl->begin(I), IE = pImpl->end(I);
1238 if (II->hasAttribute(Attr)) {
1239 if (Index) *Index = pImpl->getSlotIndex(I);
1246 Attribute AttributeList::getAttribute(unsigned Index,
1247 Attribute::AttrKind Kind) const {
1248 return getAttributes(Index).getAttribute(Kind);
1251 Attribute AttributeList::getAttribute(unsigned Index, StringRef Kind) const {
1252 return getAttributes(Index).getAttribute(Kind);
1255 unsigned AttributeList::getRetAlignment() const {
1256 return getAttributes(ReturnIndex).getAlignment();
1259 unsigned AttributeList::getParamAlignment(unsigned ArgNo) const {
1260 return getAttributes(ArgNo + FirstArgIndex).getAlignment();
1263 unsigned AttributeList::getStackAlignment(unsigned Index) const {
1264 return getAttributes(Index).getStackAlignment();
1267 uint64_t AttributeList::getDereferenceableBytes(unsigned Index) const {
1268 return getAttributes(Index).getDereferenceableBytes();
1271 uint64_t AttributeList::getDereferenceableOrNullBytes(unsigned Index) const {
1272 return getAttributes(Index).getDereferenceableOrNullBytes();
1275 std::pair<unsigned, Optional<unsigned>>
1276 AttributeList::getAllocSizeArgs(unsigned Index) const {
1277 return getAttributes(Index).getAllocSizeArgs();
1280 std::string AttributeList::getAsString(unsigned Index, bool InAttrGrp) const {
1281 return getAttributes(Index).getAsString(InAttrGrp);
1284 AttributeSet AttributeList::getAttributes(unsigned Index) const {
1285 if (!pImpl) return AttributeSet();
1287 // Loop through to find the attribute node we want.
1288 for (unsigned I = 0, E = pImpl->getNumSlots(); I != E; ++I)
1289 if (pImpl->getSlotIndex(I) == Index)
1290 return pImpl->getSlotAttributes(I);
1292 return AttributeSet();
1295 AttributeList::iterator AttributeList::begin(unsigned Slot) const {
1297 return ArrayRef<Attribute>().begin();
1298 return pImpl->begin(Slot);
1301 AttributeList::iterator AttributeList::end(unsigned Slot) const {
1303 return ArrayRef<Attribute>().end();
1304 return pImpl->end(Slot);
1307 //===----------------------------------------------------------------------===//
1308 // AttributeList Introspection Methods
1309 //===----------------------------------------------------------------------===//
1311 unsigned AttributeList::getNumSlots() const {
1312 return pImpl ? pImpl->getNumSlots() : 0;
1315 unsigned AttributeList::getSlotIndex(unsigned Slot) const {
1316 assert(pImpl && Slot < pImpl->getNumSlots() &&
1317 "Slot # out of range!");
1318 return pImpl->getSlotIndex(Slot);
1321 AttributeSet AttributeList::getSlotAttributes(unsigned Slot) const {
1322 assert(pImpl && Slot < pImpl->getNumSlots() &&
1323 "Slot # out of range!");
1324 return pImpl->getSlotAttributes(Slot);
1327 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1328 LLVM_DUMP_METHOD void AttributeList::dump() const {
1331 for (unsigned i = 0, e = getNumSlots(); i < e; ++i) {
1332 uint64_t Index = getSlotIndex(i);
1338 dbgs() << " => " << getAsString(Index) << " }\n";
1345 //===----------------------------------------------------------------------===//
1346 // AttrBuilder Method Implementations
1347 //===----------------------------------------------------------------------===//
1349 AttrBuilder::AttrBuilder(AttributeList AL, unsigned Index) {
1350 AttributeListImpl *pImpl = AL.pImpl;
1353 for (unsigned I = 0, E = pImpl->getNumSlots(); I != E; ++I) {
1354 if (pImpl->getSlotIndex(I) != Index) continue;
1356 for (AttributeListImpl::iterator II = pImpl->begin(I), IE = pImpl->end(I);
1364 AttrBuilder::AttrBuilder(AttributeSet AS) {
1365 if (AS.hasAttributes()) {
1366 for (const Attribute &A : AS)
1371 void AttrBuilder::clear() {
1373 TargetDepAttrs.clear();
1374 Alignment = StackAlignment = DerefBytes = DerefOrNullBytes = 0;
1378 AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
1379 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
1380 assert(Val != Attribute::Alignment && Val != Attribute::StackAlignment &&
1381 Val != Attribute::Dereferenceable && Val != Attribute::AllocSize &&
1382 "Adding integer attribute without adding a value!");
1387 AttrBuilder &AttrBuilder::addAttribute(Attribute Attr) {
1388 if (Attr.isStringAttribute()) {
1389 addAttribute(Attr.getKindAsString(), Attr.getValueAsString());
1393 Attribute::AttrKind Kind = Attr.getKindAsEnum();
1396 if (Kind == Attribute::Alignment)
1397 Alignment = Attr.getAlignment();
1398 else if (Kind == Attribute::StackAlignment)
1399 StackAlignment = Attr.getStackAlignment();
1400 else if (Kind == Attribute::Dereferenceable)
1401 DerefBytes = Attr.getDereferenceableBytes();
1402 else if (Kind == Attribute::DereferenceableOrNull)
1403 DerefOrNullBytes = Attr.getDereferenceableOrNullBytes();
1404 else if (Kind == Attribute::AllocSize)
1405 AllocSizeArgs = Attr.getValueAsInt();
1409 AttrBuilder &AttrBuilder::addAttribute(StringRef A, StringRef V) {
1410 TargetDepAttrs[A] = V;
1414 AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
1415 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
1418 if (Val == Attribute::Alignment)
1420 else if (Val == Attribute::StackAlignment)
1422 else if (Val == Attribute::Dereferenceable)
1424 else if (Val == Attribute::DereferenceableOrNull)
1425 DerefOrNullBytes = 0;
1426 else if (Val == Attribute::AllocSize)
1432 AttrBuilder &AttrBuilder::removeAttributes(AttributeList A, uint64_t Index) {
1433 remove(A.getAttributes(Index));
1437 AttrBuilder &AttrBuilder::removeAttribute(StringRef A) {
1438 std::map<std::string, std::string>::iterator I = TargetDepAttrs.find(A);
1439 if (I != TargetDepAttrs.end())
1440 TargetDepAttrs.erase(I);
1444 std::pair<unsigned, Optional<unsigned>> AttrBuilder::getAllocSizeArgs() const {
1445 return unpackAllocSizeArgs(AllocSizeArgs);
1448 AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
1449 if (Align == 0) return *this;
1451 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1452 assert(Align <= 0x40000000 && "Alignment too large.");
1454 Attrs[Attribute::Alignment] = true;
1459 AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
1460 // Default alignment, allow the target to define how to align it.
1461 if (Align == 0) return *this;
1463 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1464 assert(Align <= 0x100 && "Alignment too large.");
1466 Attrs[Attribute::StackAlignment] = true;
1467 StackAlignment = Align;
1471 AttrBuilder &AttrBuilder::addDereferenceableAttr(uint64_t Bytes) {
1472 if (Bytes == 0) return *this;
1474 Attrs[Attribute::Dereferenceable] = true;
1479 AttrBuilder &AttrBuilder::addDereferenceableOrNullAttr(uint64_t Bytes) {
1483 Attrs[Attribute::DereferenceableOrNull] = true;
1484 DerefOrNullBytes = Bytes;
1488 AttrBuilder &AttrBuilder::addAllocSizeAttr(unsigned ElemSize,
1489 const Optional<unsigned> &NumElems) {
1490 return addAllocSizeAttrFromRawRepr(packAllocSizeArgs(ElemSize, NumElems));
1493 AttrBuilder &AttrBuilder::addAllocSizeAttrFromRawRepr(uint64_t RawArgs) {
1494 // (0, 0) is our "not present" value, so we need to check for it here.
1495 assert(RawArgs && "Invalid allocsize arguments -- given allocsize(0, 0)");
1497 Attrs[Attribute::AllocSize] = true;
1498 // Reuse existing machinery to store this as a single 64-bit integer so we can
1499 // save a few bytes over using a pair<unsigned, Optional<unsigned>>.
1500 AllocSizeArgs = RawArgs;
1504 AttrBuilder &AttrBuilder::merge(const AttrBuilder &B) {
1505 // FIXME: What if both have alignments, but they don't match?!
1507 Alignment = B.Alignment;
1509 if (!StackAlignment)
1510 StackAlignment = B.StackAlignment;
1513 DerefBytes = B.DerefBytes;
1515 if (!DerefOrNullBytes)
1516 DerefOrNullBytes = B.DerefOrNullBytes;
1519 AllocSizeArgs = B.AllocSizeArgs;
1523 for (auto I : B.td_attrs())
1524 TargetDepAttrs[I.first] = I.second;
1529 AttrBuilder &AttrBuilder::remove(const AttrBuilder &B) {
1530 // FIXME: What if both have alignments, but they don't match?!
1534 if (B.StackAlignment)
1540 if (B.DerefOrNullBytes)
1541 DerefOrNullBytes = 0;
1543 if (B.AllocSizeArgs)
1548 for (auto I : B.td_attrs())
1549 TargetDepAttrs.erase(I.first);
1554 bool AttrBuilder::overlaps(const AttrBuilder &B) const {
1555 // First check if any of the target independent attributes overlap.
1556 if ((Attrs & B.Attrs).any())
1559 // Then check if any target dependent ones do.
1560 for (const auto &I : td_attrs())
1561 if (B.contains(I.first))
1567 bool AttrBuilder::contains(StringRef A) const {
1568 return TargetDepAttrs.find(A) != TargetDepAttrs.end();
1571 bool AttrBuilder::hasAttributes() const {
1572 return !Attrs.none() || !TargetDepAttrs.empty();
1575 bool AttrBuilder::hasAttributes(AttributeList AL, uint64_t Index) const {
1576 AttributeSet AS = AL.getAttributes(Index);
1578 for (Attribute Attr : AS) {
1579 if (Attr.isEnumAttribute() || Attr.isIntAttribute()) {
1580 if (contains(Attr.getKindAsEnum()))
1583 assert(Attr.isStringAttribute() && "Invalid attribute kind!");
1584 return contains(Attr.getKindAsString());
1591 bool AttrBuilder::hasAlignmentAttr() const {
1592 return Alignment != 0;
1595 bool AttrBuilder::operator==(const AttrBuilder &B) {
1596 if (Attrs != B.Attrs)
1599 for (td_const_iterator I = TargetDepAttrs.begin(),
1600 E = TargetDepAttrs.end(); I != E; ++I)
1601 if (B.TargetDepAttrs.find(I->first) == B.TargetDepAttrs.end())
1604 return Alignment == B.Alignment && StackAlignment == B.StackAlignment &&
1605 DerefBytes == B.DerefBytes;
1608 //===----------------------------------------------------------------------===//
1609 // AttributeFuncs Function Defintions
1610 //===----------------------------------------------------------------------===//
1612 /// \brief Which attributes cannot be applied to a type.
1613 AttrBuilder AttributeFuncs::typeIncompatible(Type *Ty) {
1614 AttrBuilder Incompatible;
1616 if (!Ty->isIntegerTy())
1617 // Attribute that only apply to integers.
1618 Incompatible.addAttribute(Attribute::SExt)
1619 .addAttribute(Attribute::ZExt);
1621 if (!Ty->isPointerTy())
1622 // Attribute that only apply to pointers.
1623 Incompatible.addAttribute(Attribute::ByVal)
1624 .addAttribute(Attribute::Nest)
1625 .addAttribute(Attribute::NoAlias)
1626 .addAttribute(Attribute::NoCapture)
1627 .addAttribute(Attribute::NonNull)
1628 .addDereferenceableAttr(1) // the int here is ignored
1629 .addDereferenceableOrNullAttr(1) // the int here is ignored
1630 .addAttribute(Attribute::ReadNone)
1631 .addAttribute(Attribute::ReadOnly)
1632 .addAttribute(Attribute::StructRet)
1633 .addAttribute(Attribute::InAlloca);
1635 return Incompatible;
1638 template<typename AttrClass>
1639 static bool isEqual(const Function &Caller, const Function &Callee) {
1640 return Caller.getFnAttribute(AttrClass::getKind()) ==
1641 Callee.getFnAttribute(AttrClass::getKind());
1644 /// \brief Compute the logical AND of the attributes of the caller and the
1647 /// This function sets the caller's attribute to false if the callee's attribute
1649 template<typename AttrClass>
1650 static void setAND(Function &Caller, const Function &Callee) {
1651 if (AttrClass::isSet(Caller, AttrClass::getKind()) &&
1652 !AttrClass::isSet(Callee, AttrClass::getKind()))
1653 AttrClass::set(Caller, AttrClass::getKind(), false);
1656 /// \brief Compute the logical OR of the attributes of the caller and the
1659 /// This function sets the caller's attribute to true if the callee's attribute
1661 template<typename AttrClass>
1662 static void setOR(Function &Caller, const Function &Callee) {
1663 if (!AttrClass::isSet(Caller, AttrClass::getKind()) &&
1664 AttrClass::isSet(Callee, AttrClass::getKind()))
1665 AttrClass::set(Caller, AttrClass::getKind(), true);
1668 /// \brief If the inlined function had a higher stack protection level than the
1669 /// calling function, then bump up the caller's stack protection level.
1670 static void adjustCallerSSPLevel(Function &Caller, const Function &Callee) {
1671 // If upgrading the SSP attribute, clear out the old SSP Attributes first.
1672 // Having multiple SSP attributes doesn't actually hurt, but it adds useless
1673 // clutter to the IR.
1674 AttrBuilder OldSSPAttr;
1675 OldSSPAttr.addAttribute(Attribute::StackProtect)
1676 .addAttribute(Attribute::StackProtectStrong)
1677 .addAttribute(Attribute::StackProtectReq);
1679 if (Callee.hasFnAttribute(Attribute::StackProtectReq)) {
1680 Caller.removeAttributes(AttributeList::FunctionIndex, OldSSPAttr);
1681 Caller.addFnAttr(Attribute::StackProtectReq);
1682 } else if (Callee.hasFnAttribute(Attribute::StackProtectStrong) &&
1683 !Caller.hasFnAttribute(Attribute::StackProtectReq)) {
1684 Caller.removeAttributes(AttributeList::FunctionIndex, OldSSPAttr);
1685 Caller.addFnAttr(Attribute::StackProtectStrong);
1686 } else if (Callee.hasFnAttribute(Attribute::StackProtect) &&
1687 !Caller.hasFnAttribute(Attribute::StackProtectReq) &&
1688 !Caller.hasFnAttribute(Attribute::StackProtectStrong))
1689 Caller.addFnAttr(Attribute::StackProtect);
1692 #define GET_ATTR_COMPAT_FUNC
1693 #include "AttributesCompatFunc.inc"
1695 bool AttributeFuncs::areInlineCompatible(const Function &Caller,
1696 const Function &Callee) {
1697 return hasCompatibleFnAttrs(Caller, Callee);
1700 void AttributeFuncs::mergeAttributesForInlining(Function &Caller,
1701 const Function &Callee) {
1702 mergeFnAttrs(Caller, Callee);