1 //===- Attributes.cpp - Implement AttributesList --------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
10 // This file implements the Attribute, AttributeImpl, AttrBuilder,
11 // AttributeListImpl, and AttributeList classes.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/IR/Attributes.h"
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/STLExtras.h"
22 #include "llvm/ADT/SmallVector.h"
23 #include "llvm/ADT/StringExtras.h"
24 #include "llvm/ADT/StringRef.h"
25 #include "llvm/ADT/Twine.h"
26 #include "llvm/Config/llvm-config.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"
47 //===----------------------------------------------------------------------===//
48 // Attribute Construction Methods
49 //===----------------------------------------------------------------------===//
51 // allocsize has two integer arguments, but because they're both 32 bits, we can
52 // pack them into one 64-bit value, at the cost of making said value
55 // In order to do this, we need to reserve one value of the second (optional)
56 // allocsize argument to signify "not present."
57 static const unsigned AllocSizeNumElemsNotPresent
= -1;
59 static uint64_t packAllocSizeArgs(unsigned ElemSizeArg
,
60 const Optional
<unsigned> &NumElemsArg
) {
61 assert((!NumElemsArg
.hasValue() ||
62 *NumElemsArg
!= AllocSizeNumElemsNotPresent
) &&
63 "Attempting to pack a reserved value");
65 return uint64_t(ElemSizeArg
) << 32 |
66 NumElemsArg
.getValueOr(AllocSizeNumElemsNotPresent
);
69 static std::pair
<unsigned, Optional
<unsigned>>
70 unpackAllocSizeArgs(uint64_t Num
) {
71 unsigned NumElems
= Num
& std::numeric_limits
<unsigned>::max();
72 unsigned ElemSizeArg
= Num
>> 32;
74 Optional
<unsigned> NumElemsArg
;
75 if (NumElems
!= AllocSizeNumElemsNotPresent
)
76 NumElemsArg
= NumElems
;
77 return std::make_pair(ElemSizeArg
, NumElemsArg
);
80 Attribute
Attribute::get(LLVMContext
&Context
, Attribute::AttrKind Kind
,
82 LLVMContextImpl
*pImpl
= Context
.pImpl
;
85 if (Val
) ID
.AddInteger(Val
);
88 AttributeImpl
*PA
= pImpl
->AttrsSet
.FindNodeOrInsertPos(ID
, InsertPoint
);
91 // If we didn't find any existing attributes of the same shape then create a
92 // new one and insert it.
94 PA
= new EnumAttributeImpl(Kind
);
96 PA
= new IntAttributeImpl(Kind
, Val
);
97 pImpl
->AttrsSet
.InsertNode(PA
, InsertPoint
);
100 // Return the Attribute that we found or created.
101 return Attribute(PA
);
104 Attribute
Attribute::get(LLVMContext
&Context
, StringRef Kind
, StringRef Val
) {
105 LLVMContextImpl
*pImpl
= Context
.pImpl
;
108 if (!Val
.empty()) ID
.AddString(Val
);
111 AttributeImpl
*PA
= pImpl
->AttrsSet
.FindNodeOrInsertPos(ID
, InsertPoint
);
114 // If we didn't find any existing attributes of the same shape then create a
115 // new one and insert it.
116 PA
= new StringAttributeImpl(Kind
, Val
);
117 pImpl
->AttrsSet
.InsertNode(PA
, InsertPoint
);
120 // Return the Attribute that we found or created.
121 return Attribute(PA
);
124 Attribute
Attribute::get(LLVMContext
&Context
, Attribute::AttrKind Kind
,
126 LLVMContextImpl
*pImpl
= Context
.pImpl
;
132 AttributeImpl
*PA
= pImpl
->AttrsSet
.FindNodeOrInsertPos(ID
, InsertPoint
);
135 // If we didn't find any existing attributes of the same shape then create a
136 // new one and insert it.
137 PA
= new TypeAttributeImpl(Kind
, Ty
);
138 pImpl
->AttrsSet
.InsertNode(PA
, InsertPoint
);
141 // Return the Attribute that we found or created.
142 return Attribute(PA
);
145 Attribute
Attribute::getWithAlignment(LLVMContext
&Context
, uint64_t Align
) {
146 assert(isPowerOf2_32(Align
) && "Alignment must be a power of two.");
147 assert(Align
<= 0x40000000 && "Alignment too large.");
148 return get(Context
, Alignment
, Align
);
151 Attribute
Attribute::getWithStackAlignment(LLVMContext
&Context
,
153 assert(isPowerOf2_32(Align
) && "Alignment must be a power of two.");
154 assert(Align
<= 0x100 && "Alignment too large.");
155 return get(Context
, StackAlignment
, Align
);
158 Attribute
Attribute::getWithDereferenceableBytes(LLVMContext
&Context
,
160 assert(Bytes
&& "Bytes must be non-zero.");
161 return get(Context
, Dereferenceable
, Bytes
);
164 Attribute
Attribute::getWithDereferenceableOrNullBytes(LLVMContext
&Context
,
166 assert(Bytes
&& "Bytes must be non-zero.");
167 return get(Context
, DereferenceableOrNull
, Bytes
);
170 Attribute
Attribute::getWithByValType(LLVMContext
&Context
, Type
*Ty
) {
171 return get(Context
, ByVal
, Ty
);
175 Attribute::getWithAllocSizeArgs(LLVMContext
&Context
, unsigned ElemSizeArg
,
176 const Optional
<unsigned> &NumElemsArg
) {
177 assert(!(ElemSizeArg
== 0 && NumElemsArg
&& *NumElemsArg
== 0) &&
178 "Invalid allocsize arguments -- given allocsize(0, 0)");
179 return get(Context
, AllocSize
, packAllocSizeArgs(ElemSizeArg
, NumElemsArg
));
182 //===----------------------------------------------------------------------===//
183 // Attribute Accessor Methods
184 //===----------------------------------------------------------------------===//
186 bool Attribute::isEnumAttribute() const {
187 return pImpl
&& pImpl
->isEnumAttribute();
190 bool Attribute::isIntAttribute() const {
191 return pImpl
&& pImpl
->isIntAttribute();
194 bool Attribute::isStringAttribute() const {
195 return pImpl
&& pImpl
->isStringAttribute();
198 bool Attribute::isTypeAttribute() const {
199 return pImpl
&& pImpl
->isTypeAttribute();
202 Attribute::AttrKind
Attribute::getKindAsEnum() const {
203 if (!pImpl
) return None
;
204 assert((isEnumAttribute() || isIntAttribute() || isTypeAttribute()) &&
205 "Invalid attribute type to get the kind as an enum!");
206 return pImpl
->getKindAsEnum();
209 uint64_t Attribute::getValueAsInt() const {
210 if (!pImpl
) return 0;
211 assert(isIntAttribute() &&
212 "Expected the attribute to be an integer attribute!");
213 return pImpl
->getValueAsInt();
216 StringRef
Attribute::getKindAsString() const {
217 if (!pImpl
) return {};
218 assert(isStringAttribute() &&
219 "Invalid attribute type to get the kind as a string!");
220 return pImpl
->getKindAsString();
223 StringRef
Attribute::getValueAsString() const {
224 if (!pImpl
) return {};
225 assert(isStringAttribute() &&
226 "Invalid attribute type to get the value as a string!");
227 return pImpl
->getValueAsString();
230 Type
*Attribute::getValueAsType() const {
231 if (!pImpl
) return {};
232 assert(isTypeAttribute() &&
233 "Invalid attribute type to get the value as a type!");
234 return pImpl
->getValueAsType();
238 bool Attribute::hasAttribute(AttrKind Kind
) const {
239 return (pImpl
&& pImpl
->hasAttribute(Kind
)) || (!pImpl
&& Kind
== None
);
242 bool Attribute::hasAttribute(StringRef Kind
) const {
243 if (!isStringAttribute()) return false;
244 return pImpl
&& pImpl
->hasAttribute(Kind
);
247 unsigned Attribute::getAlignment() const {
248 assert(hasAttribute(Attribute::Alignment
) &&
249 "Trying to get alignment from non-alignment attribute!");
250 return pImpl
->getValueAsInt();
253 unsigned Attribute::getStackAlignment() const {
254 assert(hasAttribute(Attribute::StackAlignment
) &&
255 "Trying to get alignment from non-alignment attribute!");
256 return pImpl
->getValueAsInt();
259 uint64_t Attribute::getDereferenceableBytes() const {
260 assert(hasAttribute(Attribute::Dereferenceable
) &&
261 "Trying to get dereferenceable bytes from "
262 "non-dereferenceable attribute!");
263 return pImpl
->getValueAsInt();
266 uint64_t Attribute::getDereferenceableOrNullBytes() const {
267 assert(hasAttribute(Attribute::DereferenceableOrNull
) &&
268 "Trying to get dereferenceable bytes from "
269 "non-dereferenceable attribute!");
270 return pImpl
->getValueAsInt();
273 std::pair
<unsigned, Optional
<unsigned>> Attribute::getAllocSizeArgs() const {
274 assert(hasAttribute(Attribute::AllocSize
) &&
275 "Trying to get allocsize args from non-allocsize attribute");
276 return unpackAllocSizeArgs(pImpl
->getValueAsInt());
279 std::string
Attribute::getAsString(bool InAttrGrp
) const {
280 if (!pImpl
) return {};
282 if (hasAttribute(Attribute::SanitizeAddress
))
283 return "sanitize_address";
284 if (hasAttribute(Attribute::SanitizeHWAddress
))
285 return "sanitize_hwaddress";
286 if (hasAttribute(Attribute::SanitizeMemTag
))
287 return "sanitize_memtag";
288 if (hasAttribute(Attribute::AlwaysInline
))
289 return "alwaysinline";
290 if (hasAttribute(Attribute::ArgMemOnly
))
292 if (hasAttribute(Attribute::Builtin
))
294 if (hasAttribute(Attribute::Convergent
))
296 if (hasAttribute(Attribute::SwiftError
))
298 if (hasAttribute(Attribute::SwiftSelf
))
300 if (hasAttribute(Attribute::InaccessibleMemOnly
))
301 return "inaccessiblememonly";
302 if (hasAttribute(Attribute::InaccessibleMemOrArgMemOnly
))
303 return "inaccessiblemem_or_argmemonly";
304 if (hasAttribute(Attribute::InAlloca
))
306 if (hasAttribute(Attribute::InlineHint
))
308 if (hasAttribute(Attribute::InReg
))
310 if (hasAttribute(Attribute::JumpTable
))
312 if (hasAttribute(Attribute::MinSize
))
314 if (hasAttribute(Attribute::Naked
))
316 if (hasAttribute(Attribute::Nest
))
318 if (hasAttribute(Attribute::NoAlias
))
320 if (hasAttribute(Attribute::NoBuiltin
))
322 if (hasAttribute(Attribute::NoCapture
))
324 if (hasAttribute(Attribute::NoDuplicate
))
325 return "noduplicate";
326 if (hasAttribute(Attribute::NoFree
))
328 if (hasAttribute(Attribute::NoImplicitFloat
))
329 return "noimplicitfloat";
330 if (hasAttribute(Attribute::NoInline
))
332 if (hasAttribute(Attribute::NonLazyBind
))
333 return "nonlazybind";
334 if (hasAttribute(Attribute::NonNull
))
336 if (hasAttribute(Attribute::NoRedZone
))
338 if (hasAttribute(Attribute::NoReturn
))
340 if (hasAttribute(Attribute::NoSync
))
342 if (hasAttribute(Attribute::WillReturn
))
344 if (hasAttribute(Attribute::NoCfCheck
))
346 if (hasAttribute(Attribute::NoRecurse
))
348 if (hasAttribute(Attribute::NoUnwind
))
350 if (hasAttribute(Attribute::OptForFuzzing
))
351 return "optforfuzzing";
352 if (hasAttribute(Attribute::OptimizeNone
))
354 if (hasAttribute(Attribute::OptimizeForSize
))
356 if (hasAttribute(Attribute::ReadNone
))
358 if (hasAttribute(Attribute::ReadOnly
))
360 if (hasAttribute(Attribute::WriteOnly
))
362 if (hasAttribute(Attribute::Returned
))
364 if (hasAttribute(Attribute::ReturnsTwice
))
365 return "returns_twice";
366 if (hasAttribute(Attribute::SExt
))
368 if (hasAttribute(Attribute::SpeculativeLoadHardening
))
369 return "speculative_load_hardening";
370 if (hasAttribute(Attribute::Speculatable
))
371 return "speculatable";
372 if (hasAttribute(Attribute::StackProtect
))
374 if (hasAttribute(Attribute::StackProtectReq
))
376 if (hasAttribute(Attribute::StackProtectStrong
))
378 if (hasAttribute(Attribute::SafeStack
))
380 if (hasAttribute(Attribute::ShadowCallStack
))
381 return "shadowcallstack";
382 if (hasAttribute(Attribute::StrictFP
))
384 if (hasAttribute(Attribute::StructRet
))
386 if (hasAttribute(Attribute::SanitizeThread
))
387 return "sanitize_thread";
388 if (hasAttribute(Attribute::SanitizeMemory
))
389 return "sanitize_memory";
390 if (hasAttribute(Attribute::UWTable
))
392 if (hasAttribute(Attribute::ZExt
))
394 if (hasAttribute(Attribute::Cold
))
396 if (hasAttribute(Attribute::ImmArg
))
399 if (hasAttribute(Attribute::ByVal
)) {
402 if (Type
*Ty
= getValueAsType()) {
403 raw_string_ostream
OS(Result
);
405 Ty
->print(OS
, false, true);
412 // FIXME: These should be output like this:
417 if (hasAttribute(Attribute::Alignment
)) {
420 Result
+= (InAttrGrp
) ? "=" : " ";
421 Result
+= utostr(getValueAsInt());
425 auto AttrWithBytesToString
= [&](const char *Name
) {
430 Result
+= utostr(getValueAsInt());
433 Result
+= utostr(getValueAsInt());
439 if (hasAttribute(Attribute::StackAlignment
))
440 return AttrWithBytesToString("alignstack");
442 if (hasAttribute(Attribute::Dereferenceable
))
443 return AttrWithBytesToString("dereferenceable");
445 if (hasAttribute(Attribute::DereferenceableOrNull
))
446 return AttrWithBytesToString("dereferenceable_or_null");
448 if (hasAttribute(Attribute::AllocSize
)) {
450 Optional
<unsigned> NumElems
;
451 std::tie(ElemSize
, NumElems
) = getAllocSizeArgs();
453 std::string Result
= "allocsize(";
454 Result
+= utostr(ElemSize
);
455 if (NumElems
.hasValue()) {
457 Result
+= utostr(*NumElems
);
463 // Convert target-dependent attributes to strings of the form:
468 if (isStringAttribute()) {
470 Result
+= (Twine('"') + getKindAsString() + Twine('"')).str();
472 std::string AttrVal
= pImpl
->getValueAsString();
473 if (AttrVal
.empty()) return Result
;
475 // Since some attribute strings contain special characters that cannot be
476 // printable, those have to be escaped to make the attribute value printable
477 // as is. e.g. "\01__gnu_mcount_nc"
479 raw_string_ostream
OS(Result
);
481 printEscapedString(AttrVal
, OS
);
487 llvm_unreachable("Unknown attribute");
490 bool Attribute::operator<(Attribute A
) const {
491 if (!pImpl
&& !A
.pImpl
) return false;
492 if (!pImpl
) return true;
493 if (!A
.pImpl
) return false;
494 return *pImpl
< *A
.pImpl
;
497 //===----------------------------------------------------------------------===//
498 // AttributeImpl Definition
499 //===----------------------------------------------------------------------===//
501 // Pin the vtables to this file.
502 AttributeImpl::~AttributeImpl() = default;
504 void EnumAttributeImpl::anchor() {}
506 void IntAttributeImpl::anchor() {}
508 void StringAttributeImpl::anchor() {}
510 void TypeAttributeImpl::anchor() {}
512 bool AttributeImpl::hasAttribute(Attribute::AttrKind A
) const {
513 if (isStringAttribute()) return false;
514 return getKindAsEnum() == A
;
517 bool AttributeImpl::hasAttribute(StringRef Kind
) const {
518 if (!isStringAttribute()) return false;
519 return getKindAsString() == Kind
;
522 Attribute::AttrKind
AttributeImpl::getKindAsEnum() const {
523 assert(isEnumAttribute() || isIntAttribute() || isTypeAttribute());
524 return static_cast<const EnumAttributeImpl
*>(this)->getEnumKind();
527 uint64_t AttributeImpl::getValueAsInt() const {
528 assert(isIntAttribute());
529 return static_cast<const IntAttributeImpl
*>(this)->getValue();
532 StringRef
AttributeImpl::getKindAsString() const {
533 assert(isStringAttribute());
534 return static_cast<const StringAttributeImpl
*>(this)->getStringKind();
537 StringRef
AttributeImpl::getValueAsString() const {
538 assert(isStringAttribute());
539 return static_cast<const StringAttributeImpl
*>(this)->getStringValue();
542 Type
*AttributeImpl::getValueAsType() const {
543 assert(isTypeAttribute());
544 return static_cast<const TypeAttributeImpl
*>(this)->getTypeValue();
547 bool AttributeImpl::operator<(const AttributeImpl
&AI
) const {
548 // This sorts the attributes with Attribute::AttrKinds coming first (sorted
549 // relative to their enum value) and then strings.
550 if (isEnumAttribute()) {
551 if (AI
.isEnumAttribute()) return getKindAsEnum() < AI
.getKindAsEnum();
552 if (AI
.isIntAttribute()) return true;
553 if (AI
.isStringAttribute()) return true;
554 if (AI
.isTypeAttribute()) return true;
557 if (isTypeAttribute()) {
558 if (AI
.isEnumAttribute()) return false;
559 if (AI
.isTypeAttribute()) {
560 assert(getKindAsEnum() != AI
.getKindAsEnum() &&
561 "Comparison of types would be unstable");
562 return getKindAsEnum() < AI
.getKindAsEnum();
564 if (AI
.isIntAttribute()) return true;
565 if (AI
.isStringAttribute()) return true;
568 if (isIntAttribute()) {
569 if (AI
.isEnumAttribute()) return false;
570 if (AI
.isTypeAttribute()) return false;
571 if (AI
.isIntAttribute()) {
572 if (getKindAsEnum() == AI
.getKindAsEnum())
573 return getValueAsInt() < AI
.getValueAsInt();
574 return getKindAsEnum() < AI
.getKindAsEnum();
576 if (AI
.isStringAttribute()) return true;
579 assert(isStringAttribute());
580 if (AI
.isEnumAttribute()) return false;
581 if (AI
.isTypeAttribute()) return false;
582 if (AI
.isIntAttribute()) return false;
583 if (getKindAsString() == AI
.getKindAsString())
584 return getValueAsString() < AI
.getValueAsString();
585 return getKindAsString() < AI
.getKindAsString();
588 //===----------------------------------------------------------------------===//
589 // AttributeSet Definition
590 //===----------------------------------------------------------------------===//
592 AttributeSet
AttributeSet::get(LLVMContext
&C
, const AttrBuilder
&B
) {
593 return AttributeSet(AttributeSetNode::get(C
, B
));
596 AttributeSet
AttributeSet::get(LLVMContext
&C
, ArrayRef
<Attribute
> Attrs
) {
597 return AttributeSet(AttributeSetNode::get(C
, Attrs
));
600 AttributeSet
AttributeSet::addAttribute(LLVMContext
&C
,
601 Attribute::AttrKind Kind
) const {
602 if (hasAttribute(Kind
)) return *this;
604 B
.addAttribute(Kind
);
605 return addAttributes(C
, AttributeSet::get(C
, B
));
608 AttributeSet
AttributeSet::addAttribute(LLVMContext
&C
, StringRef Kind
,
609 StringRef Value
) const {
611 B
.addAttribute(Kind
, Value
);
612 return addAttributes(C
, AttributeSet::get(C
, B
));
615 AttributeSet
AttributeSet::addAttributes(LLVMContext
&C
,
616 const AttributeSet AS
) const {
617 if (!hasAttributes())
620 if (!AS
.hasAttributes())
624 for (const auto I
: *this)
630 AttributeSet
AttributeSet::removeAttribute(LLVMContext
&C
,
631 Attribute::AttrKind Kind
) const {
632 if (!hasAttribute(Kind
)) return *this;
633 AttrBuilder
B(*this);
634 B
.removeAttribute(Kind
);
638 AttributeSet
AttributeSet::removeAttribute(LLVMContext
&C
,
639 StringRef Kind
) const {
640 if (!hasAttribute(Kind
)) return *this;
641 AttrBuilder
B(*this);
642 B
.removeAttribute(Kind
);
646 AttributeSet
AttributeSet::removeAttributes(LLVMContext
&C
,
647 const AttrBuilder
&Attrs
) const {
648 AttrBuilder
B(*this);
653 unsigned AttributeSet::getNumAttributes() const {
654 return SetNode
? SetNode
->getNumAttributes() : 0;
657 bool AttributeSet::hasAttribute(Attribute::AttrKind Kind
) const {
658 return SetNode
? SetNode
->hasAttribute(Kind
) : false;
661 bool AttributeSet::hasAttribute(StringRef Kind
) const {
662 return SetNode
? SetNode
->hasAttribute(Kind
) : false;
665 Attribute
AttributeSet::getAttribute(Attribute::AttrKind Kind
) const {
666 return SetNode
? SetNode
->getAttribute(Kind
) : Attribute();
669 Attribute
AttributeSet::getAttribute(StringRef Kind
) const {
670 return SetNode
? SetNode
->getAttribute(Kind
) : Attribute();
673 unsigned AttributeSet::getAlignment() const {
674 return SetNode
? SetNode
->getAlignment() : 0;
677 unsigned AttributeSet::getStackAlignment() const {
678 return SetNode
? SetNode
->getStackAlignment() : 0;
681 uint64_t AttributeSet::getDereferenceableBytes() const {
682 return SetNode
? SetNode
->getDereferenceableBytes() : 0;
685 uint64_t AttributeSet::getDereferenceableOrNullBytes() const {
686 return SetNode
? SetNode
->getDereferenceableOrNullBytes() : 0;
689 Type
*AttributeSet::getByValType() const {
690 return SetNode
? SetNode
->getByValType() : nullptr;
693 std::pair
<unsigned, Optional
<unsigned>> AttributeSet::getAllocSizeArgs() const {
694 return SetNode
? SetNode
->getAllocSizeArgs()
695 : std::pair
<unsigned, Optional
<unsigned>>(0, 0);
698 std::string
AttributeSet::getAsString(bool InAttrGrp
) const {
699 return SetNode
? SetNode
->getAsString(InAttrGrp
) : "";
702 AttributeSet::iterator
AttributeSet::begin() const {
703 return SetNode
? SetNode
->begin() : nullptr;
706 AttributeSet::iterator
AttributeSet::end() const {
707 return SetNode
? SetNode
->end() : nullptr;
710 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
711 LLVM_DUMP_METHOD
void AttributeSet::dump() const {
714 dbgs() << getAsString(true) << " }\n";
718 //===----------------------------------------------------------------------===//
719 // AttributeSetNode Definition
720 //===----------------------------------------------------------------------===//
722 AttributeSetNode::AttributeSetNode(ArrayRef
<Attribute
> Attrs
)
723 : NumAttrs(Attrs
.size()) {
724 // There's memory after the node where we can store the entries in.
725 llvm::copy(Attrs
, getTrailingObjects
<Attribute
>());
727 static_assert(Attribute::EndAttrKinds
<=
728 sizeof(AvailableAttrs
) * CHAR_BIT
,
729 "Too many attributes");
731 for (const auto I
: *this) {
732 if (!I
.isStringAttribute()) {
733 Attribute::AttrKind Kind
= I
.getKindAsEnum();
734 AvailableAttrs
[Kind
/ 8] |= 1ULL << (Kind
% 8);
739 AttributeSetNode
*AttributeSetNode::get(LLVMContext
&C
,
740 ArrayRef
<Attribute
> Attrs
) {
744 // Otherwise, build a key to look up the existing attributes.
745 LLVMContextImpl
*pImpl
= C
.pImpl
;
748 SmallVector
<Attribute
, 8> SortedAttrs(Attrs
.begin(), Attrs
.end());
749 llvm::sort(SortedAttrs
);
751 for (const auto Attr
: SortedAttrs
)
755 AttributeSetNode
*PA
=
756 pImpl
->AttrsSetNodes
.FindNodeOrInsertPos(ID
, InsertPoint
);
758 // If we didn't find any existing attributes of the same shape then create a
759 // new one and insert it.
761 // Coallocate entries after the AttributeSetNode itself.
762 void *Mem
= ::operator new(totalSizeToAlloc
<Attribute
>(SortedAttrs
.size()));
763 PA
= new (Mem
) AttributeSetNode(SortedAttrs
);
764 pImpl
->AttrsSetNodes
.InsertNode(PA
, InsertPoint
);
767 // Return the AttributeSetNode that we found or created.
771 AttributeSetNode
*AttributeSetNode::get(LLVMContext
&C
, const AttrBuilder
&B
) {
772 // Add target-independent attributes.
773 SmallVector
<Attribute
, 8> Attrs
;
774 for (Attribute::AttrKind Kind
= Attribute::None
;
775 Kind
!= Attribute::EndAttrKinds
; Kind
= Attribute::AttrKind(Kind
+ 1)) {
776 if (!B
.contains(Kind
))
781 case Attribute::ByVal
:
782 Attr
= Attribute::getWithByValType(C
, B
.getByValType());
784 case Attribute::Alignment
:
785 Attr
= Attribute::getWithAlignment(C
, B
.getAlignment());
787 case Attribute::StackAlignment
:
788 Attr
= Attribute::getWithStackAlignment(C
, B
.getStackAlignment());
790 case Attribute::Dereferenceable
:
791 Attr
= Attribute::getWithDereferenceableBytes(
792 C
, B
.getDereferenceableBytes());
794 case Attribute::DereferenceableOrNull
:
795 Attr
= Attribute::getWithDereferenceableOrNullBytes(
796 C
, B
.getDereferenceableOrNullBytes());
798 case Attribute::AllocSize
: {
799 auto A
= B
.getAllocSizeArgs();
800 Attr
= Attribute::getWithAllocSizeArgs(C
, A
.first
, A
.second
);
804 Attr
= Attribute::get(C
, Kind
);
806 Attrs
.push_back(Attr
);
809 // Add target-dependent (string) attributes.
810 for (const auto &TDA
: B
.td_attrs())
811 Attrs
.emplace_back(Attribute::get(C
, TDA
.first
, TDA
.second
));
813 return get(C
, Attrs
);
816 bool AttributeSetNode::hasAttribute(StringRef Kind
) const {
817 for (const auto I
: *this)
818 if (I
.hasAttribute(Kind
))
823 Attribute
AttributeSetNode::getAttribute(Attribute::AttrKind Kind
) const {
824 if (hasAttribute(Kind
)) {
825 for (const auto I
: *this)
826 if (I
.hasAttribute(Kind
))
832 Attribute
AttributeSetNode::getAttribute(StringRef Kind
) const {
833 for (const auto I
: *this)
834 if (I
.hasAttribute(Kind
))
839 unsigned AttributeSetNode::getAlignment() const {
840 for (const auto I
: *this)
841 if (I
.hasAttribute(Attribute::Alignment
))
842 return I
.getAlignment();
846 unsigned AttributeSetNode::getStackAlignment() const {
847 for (const auto I
: *this)
848 if (I
.hasAttribute(Attribute::StackAlignment
))
849 return I
.getStackAlignment();
853 Type
*AttributeSetNode::getByValType() const {
854 for (const auto I
: *this)
855 if (I
.hasAttribute(Attribute::ByVal
))
856 return I
.getValueAsType();
860 uint64_t AttributeSetNode::getDereferenceableBytes() const {
861 for (const auto I
: *this)
862 if (I
.hasAttribute(Attribute::Dereferenceable
))
863 return I
.getDereferenceableBytes();
867 uint64_t AttributeSetNode::getDereferenceableOrNullBytes() const {
868 for (const auto I
: *this)
869 if (I
.hasAttribute(Attribute::DereferenceableOrNull
))
870 return I
.getDereferenceableOrNullBytes();
874 std::pair
<unsigned, Optional
<unsigned>>
875 AttributeSetNode::getAllocSizeArgs() const {
876 for (const auto I
: *this)
877 if (I
.hasAttribute(Attribute::AllocSize
))
878 return I
.getAllocSizeArgs();
879 return std::make_pair(0, 0);
882 std::string
AttributeSetNode::getAsString(bool InAttrGrp
) const {
884 for (iterator I
= begin(), E
= end(); I
!= E
; ++I
) {
887 Str
+= I
->getAsString(InAttrGrp
);
892 //===----------------------------------------------------------------------===//
893 // AttributeListImpl Definition
894 //===----------------------------------------------------------------------===//
896 /// Map from AttributeList index to the internal array index. Adding one happens
897 /// to work, but it relies on unsigned integer wrapping. MSVC warns about
898 /// unsigned wrapping in constexpr functions, so write out the conditional. LLVM
899 /// folds it to add anyway.
900 static constexpr unsigned attrIdxToArrayIdx(unsigned Index
) {
901 return Index
== AttributeList::FunctionIndex
? 0 : Index
+ 1;
904 AttributeListImpl::AttributeListImpl(LLVMContext
&C
,
905 ArrayRef
<AttributeSet
> Sets
)
906 : Context(C
), NumAttrSets(Sets
.size()) {
907 assert(!Sets
.empty() && "pointless AttributeListImpl");
909 // There's memory after the node where we can store the entries in.
910 llvm::copy(Sets
, getTrailingObjects
<AttributeSet
>());
912 // Initialize AvailableFunctionAttrs summary bitset.
913 static_assert(Attribute::EndAttrKinds
<=
914 sizeof(AvailableFunctionAttrs
) * CHAR_BIT
,
915 "Too many attributes");
916 static_assert(attrIdxToArrayIdx(AttributeList::FunctionIndex
) == 0U,
917 "function should be stored in slot 0");
918 for (const auto I
: Sets
[0]) {
919 if (!I
.isStringAttribute()) {
920 Attribute::AttrKind Kind
= I
.getKindAsEnum();
921 AvailableFunctionAttrs
[Kind
/ 8] |= 1ULL << (Kind
% 8);
926 void AttributeListImpl::Profile(FoldingSetNodeID
&ID
) const {
927 Profile(ID
, makeArrayRef(begin(), end()));
930 void AttributeListImpl::Profile(FoldingSetNodeID
&ID
,
931 ArrayRef
<AttributeSet
> Sets
) {
932 for (const auto &Set
: Sets
)
933 ID
.AddPointer(Set
.SetNode
);
936 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
937 LLVM_DUMP_METHOD
void AttributeListImpl::dump() const {
938 AttributeList(const_cast<AttributeListImpl
*>(this)).dump();
942 //===----------------------------------------------------------------------===//
943 // AttributeList Construction and Mutation Methods
944 //===----------------------------------------------------------------------===//
946 AttributeList
AttributeList::getImpl(LLVMContext
&C
,
947 ArrayRef
<AttributeSet
> AttrSets
) {
948 assert(!AttrSets
.empty() && "pointless AttributeListImpl");
950 LLVMContextImpl
*pImpl
= C
.pImpl
;
952 AttributeListImpl::Profile(ID
, AttrSets
);
955 AttributeListImpl
*PA
=
956 pImpl
->AttrsLists
.FindNodeOrInsertPos(ID
, InsertPoint
);
958 // If we didn't find any existing attributes of the same shape then
959 // create a new one and insert it.
961 // Coallocate entries after the AttributeListImpl itself.
962 void *Mem
= ::operator new(
963 AttributeListImpl::totalSizeToAlloc
<AttributeSet
>(AttrSets
.size()));
964 PA
= new (Mem
) AttributeListImpl(C
, AttrSets
);
965 pImpl
->AttrsLists
.InsertNode(PA
, InsertPoint
);
968 // Return the AttributesList that we found or created.
969 return AttributeList(PA
);
973 AttributeList::get(LLVMContext
&C
,
974 ArrayRef
<std::pair
<unsigned, Attribute
>> Attrs
) {
975 // If there are no attributes then return a null AttributesList pointer.
979 assert(std::is_sorted(Attrs
.begin(), Attrs
.end(),
980 [](const std::pair
<unsigned, Attribute
> &LHS
,
981 const std::pair
<unsigned, Attribute
> &RHS
) {
982 return LHS
.first
< RHS
.first
;
983 }) && "Misordered Attributes list!");
984 assert(llvm::none_of(Attrs
,
985 [](const std::pair
<unsigned, Attribute
> &Pair
) {
986 return Pair
.second
.hasAttribute(Attribute::None
);
988 "Pointless attribute!");
990 // Create a vector if (unsigned, AttributeSetNode*) pairs from the attributes
992 SmallVector
<std::pair
<unsigned, AttributeSet
>, 8> AttrPairVec
;
993 for (ArrayRef
<std::pair
<unsigned, Attribute
>>::iterator I
= Attrs
.begin(),
994 E
= Attrs
.end(); I
!= E
; ) {
995 unsigned Index
= I
->first
;
996 SmallVector
<Attribute
, 4> AttrVec
;
997 while (I
!= E
&& I
->first
== Index
) {
998 AttrVec
.push_back(I
->second
);
1002 AttrPairVec
.emplace_back(Index
, AttributeSet::get(C
, AttrVec
));
1005 return get(C
, AttrPairVec
);
1009 AttributeList::get(LLVMContext
&C
,
1010 ArrayRef
<std::pair
<unsigned, AttributeSet
>> Attrs
) {
1011 // If there are no attributes then return a null AttributesList pointer.
1015 assert(std::is_sorted(Attrs
.begin(), Attrs
.end(),
1016 [](const std::pair
<unsigned, AttributeSet
> &LHS
,
1017 const std::pair
<unsigned, AttributeSet
> &RHS
) {
1018 return LHS
.first
< RHS
.first
;
1020 "Misordered Attributes list!");
1021 assert(llvm::none_of(Attrs
,
1022 [](const std::pair
<unsigned, AttributeSet
> &Pair
) {
1023 return !Pair
.second
.hasAttributes();
1025 "Pointless attribute!");
1027 unsigned MaxIndex
= Attrs
.back().first
;
1028 // If the MaxIndex is FunctionIndex and there are other indices in front
1029 // of it, we need to use the largest of those to get the right size.
1030 if (MaxIndex
== FunctionIndex
&& Attrs
.size() > 1)
1031 MaxIndex
= Attrs
[Attrs
.size() - 2].first
;
1033 SmallVector
<AttributeSet
, 4> AttrVec(attrIdxToArrayIdx(MaxIndex
) + 1);
1034 for (const auto Pair
: Attrs
)
1035 AttrVec
[attrIdxToArrayIdx(Pair
.first
)] = Pair
.second
;
1037 return getImpl(C
, AttrVec
);
1040 AttributeList
AttributeList::get(LLVMContext
&C
, AttributeSet FnAttrs
,
1041 AttributeSet RetAttrs
,
1042 ArrayRef
<AttributeSet
> ArgAttrs
) {
1043 // Scan from the end to find the last argument with attributes. Most
1044 // arguments don't have attributes, so it's nice if we can have fewer unique
1045 // AttributeListImpls by dropping empty attribute sets at the end of the list.
1046 unsigned NumSets
= 0;
1047 for (size_t I
= ArgAttrs
.size(); I
!= 0; --I
) {
1048 if (ArgAttrs
[I
- 1].hasAttributes()) {
1054 // Check function and return attributes if we didn't have argument
1056 if (RetAttrs
.hasAttributes())
1058 else if (FnAttrs
.hasAttributes())
1062 // If all attribute sets were empty, we can use the empty attribute list.
1066 SmallVector
<AttributeSet
, 8> AttrSets
;
1067 AttrSets
.reserve(NumSets
);
1068 // If we have any attributes, we always have function attributes.
1069 AttrSets
.push_back(FnAttrs
);
1071 AttrSets
.push_back(RetAttrs
);
1073 // Drop the empty argument attribute sets at the end.
1074 ArgAttrs
= ArgAttrs
.take_front(NumSets
- 2);
1075 AttrSets
.insert(AttrSets
.end(), ArgAttrs
.begin(), ArgAttrs
.end());
1078 return getImpl(C
, AttrSets
);
1081 AttributeList
AttributeList::get(LLVMContext
&C
, unsigned Index
,
1082 const AttrBuilder
&B
) {
1083 if (!B
.hasAttributes())
1085 Index
= attrIdxToArrayIdx(Index
);
1086 SmallVector
<AttributeSet
, 8> AttrSets(Index
+ 1);
1087 AttrSets
[Index
] = AttributeSet::get(C
, B
);
1088 return getImpl(C
, AttrSets
);
1091 AttributeList
AttributeList::get(LLVMContext
&C
, unsigned Index
,
1092 ArrayRef
<Attribute::AttrKind
> Kinds
) {
1093 SmallVector
<std::pair
<unsigned, Attribute
>, 8> Attrs
;
1094 for (const auto K
: Kinds
)
1095 Attrs
.emplace_back(Index
, Attribute::get(C
, K
));
1096 return get(C
, Attrs
);
1099 AttributeList
AttributeList::get(LLVMContext
&C
, unsigned Index
,
1100 ArrayRef
<StringRef
> Kinds
) {
1101 SmallVector
<std::pair
<unsigned, Attribute
>, 8> Attrs
;
1102 for (const auto K
: Kinds
)
1103 Attrs
.emplace_back(Index
, Attribute::get(C
, K
));
1104 return get(C
, Attrs
);
1107 AttributeList
AttributeList::get(LLVMContext
&C
,
1108 ArrayRef
<AttributeList
> Attrs
) {
1111 if (Attrs
.size() == 1)
1114 unsigned MaxSize
= 0;
1115 for (const auto List
: Attrs
)
1116 MaxSize
= std::max(MaxSize
, List
.getNumAttrSets());
1118 // If every list was empty, there is no point in merging the lists.
1122 SmallVector
<AttributeSet
, 8> NewAttrSets(MaxSize
);
1123 for (unsigned I
= 0; I
< MaxSize
; ++I
) {
1124 AttrBuilder CurBuilder
;
1125 for (const auto List
: Attrs
)
1126 CurBuilder
.merge(List
.getAttributes(I
- 1));
1127 NewAttrSets
[I
] = AttributeSet::get(C
, CurBuilder
);
1130 return getImpl(C
, NewAttrSets
);
1133 AttributeList
AttributeList::addAttribute(LLVMContext
&C
, unsigned Index
,
1134 Attribute::AttrKind Kind
) const {
1135 if (hasAttribute(Index
, Kind
)) return *this;
1137 B
.addAttribute(Kind
);
1138 return addAttributes(C
, Index
, B
);
1141 AttributeList
AttributeList::addAttribute(LLVMContext
&C
, unsigned Index
,
1143 StringRef Value
) const {
1145 B
.addAttribute(Kind
, Value
);
1146 return addAttributes(C
, Index
, B
);
1149 AttributeList
AttributeList::addAttribute(LLVMContext
&C
, unsigned Index
,
1150 Attribute A
) const {
1153 return addAttributes(C
, Index
, B
);
1156 AttributeList
AttributeList::addAttributes(LLVMContext
&C
, unsigned Index
,
1157 const AttrBuilder
&B
) const {
1158 if (!B
.hasAttributes())
1162 return AttributeList::get(C
, {{Index
, AttributeSet::get(C
, B
)}});
1165 // FIXME it is not obvious how this should work for alignment. For now, say
1166 // we can't change a known alignment.
1167 unsigned OldAlign
= getAttributes(Index
).getAlignment();
1168 unsigned NewAlign
= B
.getAlignment();
1169 assert((!OldAlign
|| !NewAlign
|| OldAlign
== NewAlign
) &&
1170 "Attempt to change alignment!");
1173 Index
= attrIdxToArrayIdx(Index
);
1174 SmallVector
<AttributeSet
, 4> AttrSets(this->begin(), this->end());
1175 if (Index
>= AttrSets
.size())
1176 AttrSets
.resize(Index
+ 1);
1178 AttrBuilder
Merged(AttrSets
[Index
]);
1180 AttrSets
[Index
] = AttributeSet::get(C
, Merged
);
1182 return getImpl(C
, AttrSets
);
1185 AttributeList
AttributeList::addParamAttribute(LLVMContext
&C
,
1186 ArrayRef
<unsigned> ArgNos
,
1187 Attribute A
) const {
1188 assert(std::is_sorted(ArgNos
.begin(), ArgNos
.end()));
1190 SmallVector
<AttributeSet
, 4> AttrSets(this->begin(), this->end());
1191 unsigned MaxIndex
= attrIdxToArrayIdx(ArgNos
.back() + FirstArgIndex
);
1192 if (MaxIndex
>= AttrSets
.size())
1193 AttrSets
.resize(MaxIndex
+ 1);
1195 for (unsigned ArgNo
: ArgNos
) {
1196 unsigned Index
= attrIdxToArrayIdx(ArgNo
+ FirstArgIndex
);
1197 AttrBuilder
B(AttrSets
[Index
]);
1199 AttrSets
[Index
] = AttributeSet::get(C
, B
);
1202 return getImpl(C
, AttrSets
);
1205 AttributeList
AttributeList::removeAttribute(LLVMContext
&C
, unsigned Index
,
1206 Attribute::AttrKind Kind
) const {
1207 if (!hasAttribute(Index
, Kind
)) return *this;
1209 Index
= attrIdxToArrayIdx(Index
);
1210 SmallVector
<AttributeSet
, 4> AttrSets(this->begin(), this->end());
1211 assert(Index
< AttrSets
.size());
1213 AttrSets
[Index
] = AttrSets
[Index
].removeAttribute(C
, Kind
);
1215 return getImpl(C
, AttrSets
);
1218 AttributeList
AttributeList::removeAttribute(LLVMContext
&C
, unsigned Index
,
1219 StringRef Kind
) const {
1220 if (!hasAttribute(Index
, Kind
)) return *this;
1222 Index
= attrIdxToArrayIdx(Index
);
1223 SmallVector
<AttributeSet
, 4> AttrSets(this->begin(), this->end());
1224 assert(Index
< AttrSets
.size());
1226 AttrSets
[Index
] = AttrSets
[Index
].removeAttribute(C
, Kind
);
1228 return getImpl(C
, AttrSets
);
1232 AttributeList::removeAttributes(LLVMContext
&C
, unsigned Index
,
1233 const AttrBuilder
&AttrsToRemove
) const {
1237 Index
= attrIdxToArrayIdx(Index
);
1238 SmallVector
<AttributeSet
, 4> AttrSets(this->begin(), this->end());
1239 if (Index
>= AttrSets
.size())
1240 AttrSets
.resize(Index
+ 1);
1242 AttrSets
[Index
] = AttrSets
[Index
].removeAttributes(C
, AttrsToRemove
);
1244 return getImpl(C
, AttrSets
);
1247 AttributeList
AttributeList::removeAttributes(LLVMContext
&C
,
1248 unsigned WithoutIndex
) const {
1251 WithoutIndex
= attrIdxToArrayIdx(WithoutIndex
);
1252 if (WithoutIndex
>= getNumAttrSets())
1254 SmallVector
<AttributeSet
, 4> AttrSets(this->begin(), this->end());
1255 AttrSets
[WithoutIndex
] = AttributeSet();
1256 return getImpl(C
, AttrSets
);
1259 AttributeList
AttributeList::addDereferenceableAttr(LLVMContext
&C
,
1261 uint64_t Bytes
) const {
1263 B
.addDereferenceableAttr(Bytes
);
1264 return addAttributes(C
, Index
, B
);
1268 AttributeList::addDereferenceableOrNullAttr(LLVMContext
&C
, unsigned Index
,
1269 uint64_t Bytes
) const {
1271 B
.addDereferenceableOrNullAttr(Bytes
);
1272 return addAttributes(C
, Index
, B
);
1276 AttributeList::addAllocSizeAttr(LLVMContext
&C
, unsigned Index
,
1277 unsigned ElemSizeArg
,
1278 const Optional
<unsigned> &NumElemsArg
) {
1280 B
.addAllocSizeAttr(ElemSizeArg
, NumElemsArg
);
1281 return addAttributes(C
, Index
, B
);
1284 //===----------------------------------------------------------------------===//
1285 // AttributeList Accessor Methods
1286 //===----------------------------------------------------------------------===//
1288 LLVMContext
&AttributeList::getContext() const { return pImpl
->getContext(); }
1290 AttributeSet
AttributeList::getParamAttributes(unsigned ArgNo
) const {
1291 return getAttributes(ArgNo
+ FirstArgIndex
);
1294 AttributeSet
AttributeList::getRetAttributes() const {
1295 return getAttributes(ReturnIndex
);
1298 AttributeSet
AttributeList::getFnAttributes() const {
1299 return getAttributes(FunctionIndex
);
1302 bool AttributeList::hasAttribute(unsigned Index
,
1303 Attribute::AttrKind Kind
) const {
1304 return getAttributes(Index
).hasAttribute(Kind
);
1307 bool AttributeList::hasAttribute(unsigned Index
, StringRef Kind
) const {
1308 return getAttributes(Index
).hasAttribute(Kind
);
1311 bool AttributeList::hasAttributes(unsigned Index
) const {
1312 return getAttributes(Index
).hasAttributes();
1315 bool AttributeList::hasFnAttribute(Attribute::AttrKind Kind
) const {
1316 return pImpl
&& pImpl
->hasFnAttribute(Kind
);
1319 bool AttributeList::hasFnAttribute(StringRef Kind
) const {
1320 return hasAttribute(AttributeList::FunctionIndex
, Kind
);
1323 bool AttributeList::hasParamAttribute(unsigned ArgNo
,
1324 Attribute::AttrKind Kind
) const {
1325 return hasAttribute(ArgNo
+ FirstArgIndex
, Kind
);
1328 bool AttributeList::hasAttrSomewhere(Attribute::AttrKind Attr
,
1329 unsigned *Index
) const {
1330 if (!pImpl
) return false;
1332 for (unsigned I
= index_begin(), E
= index_end(); I
!= E
; ++I
) {
1333 if (hasAttribute(I
, Attr
)) {
1343 Attribute
AttributeList::getAttribute(unsigned Index
,
1344 Attribute::AttrKind Kind
) const {
1345 return getAttributes(Index
).getAttribute(Kind
);
1348 Attribute
AttributeList::getAttribute(unsigned Index
, StringRef Kind
) const {
1349 return getAttributes(Index
).getAttribute(Kind
);
1352 unsigned AttributeList::getRetAlignment() const {
1353 return getAttributes(ReturnIndex
).getAlignment();
1356 unsigned AttributeList::getParamAlignment(unsigned ArgNo
) const {
1357 return getAttributes(ArgNo
+ FirstArgIndex
).getAlignment();
1360 Type
*AttributeList::getParamByValType(unsigned Index
) const {
1361 return getAttributes(Index
+FirstArgIndex
).getByValType();
1365 unsigned AttributeList::getStackAlignment(unsigned Index
) const {
1366 return getAttributes(Index
).getStackAlignment();
1369 uint64_t AttributeList::getDereferenceableBytes(unsigned Index
) const {
1370 return getAttributes(Index
).getDereferenceableBytes();
1373 uint64_t AttributeList::getDereferenceableOrNullBytes(unsigned Index
) const {
1374 return getAttributes(Index
).getDereferenceableOrNullBytes();
1377 std::pair
<unsigned, Optional
<unsigned>>
1378 AttributeList::getAllocSizeArgs(unsigned Index
) const {
1379 return getAttributes(Index
).getAllocSizeArgs();
1382 std::string
AttributeList::getAsString(unsigned Index
, bool InAttrGrp
) const {
1383 return getAttributes(Index
).getAsString(InAttrGrp
);
1386 AttributeSet
AttributeList::getAttributes(unsigned Index
) const {
1387 Index
= attrIdxToArrayIdx(Index
);
1388 if (!pImpl
|| Index
>= getNumAttrSets())
1390 return pImpl
->begin()[Index
];
1393 AttributeList::iterator
AttributeList::begin() const {
1394 return pImpl
? pImpl
->begin() : nullptr;
1397 AttributeList::iterator
AttributeList::end() const {
1398 return pImpl
? pImpl
->end() : nullptr;
1401 //===----------------------------------------------------------------------===//
1402 // AttributeList Introspection Methods
1403 //===----------------------------------------------------------------------===//
1405 unsigned AttributeList::getNumAttrSets() const {
1406 return pImpl
? pImpl
->NumAttrSets
: 0;
1409 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1410 LLVM_DUMP_METHOD
void AttributeList::dump() const {
1413 for (unsigned i
= index_begin(), e
= index_end(); i
!= e
; ++i
) {
1414 if (getAttributes(i
).hasAttributes())
1415 dbgs() << " { " << i
<< " => " << getAsString(i
) << " }\n";
1422 //===----------------------------------------------------------------------===//
1423 // AttrBuilder Method Implementations
1424 //===----------------------------------------------------------------------===//
1426 // FIXME: Remove this ctor, use AttributeSet.
1427 AttrBuilder::AttrBuilder(AttributeList AL
, unsigned Index
) {
1428 AttributeSet AS
= AL
.getAttributes(Index
);
1429 for (const auto &A
: AS
)
1433 AttrBuilder::AttrBuilder(AttributeSet AS
) {
1434 for (const auto &A
: AS
)
1438 void AttrBuilder::clear() {
1440 TargetDepAttrs
.clear();
1442 StackAlignment
.reset();
1443 DerefBytes
= DerefOrNullBytes
= 0;
1445 ByValType
= nullptr;
1448 AttrBuilder
&AttrBuilder::addAttribute(Attribute::AttrKind Val
) {
1449 assert((unsigned)Val
< Attribute::EndAttrKinds
&& "Attribute out of range!");
1450 assert(Val
!= Attribute::Alignment
&& Val
!= Attribute::StackAlignment
&&
1451 Val
!= Attribute::Dereferenceable
&& Val
!= Attribute::AllocSize
&&
1452 "Adding integer attribute without adding a value!");
1457 AttrBuilder
&AttrBuilder::addAttribute(Attribute Attr
) {
1458 if (Attr
.isStringAttribute()) {
1459 addAttribute(Attr
.getKindAsString(), Attr
.getValueAsString());
1463 Attribute::AttrKind Kind
= Attr
.getKindAsEnum();
1466 if (Kind
== Attribute::Alignment
)
1467 Alignment
= MaybeAlign(Attr
.getAlignment());
1468 else if (Kind
== Attribute::StackAlignment
)
1469 StackAlignment
= MaybeAlign(Attr
.getStackAlignment());
1470 else if (Kind
== Attribute::ByVal
)
1471 ByValType
= Attr
.getValueAsType();
1472 else if (Kind
== Attribute::Dereferenceable
)
1473 DerefBytes
= Attr
.getDereferenceableBytes();
1474 else if (Kind
== Attribute::DereferenceableOrNull
)
1475 DerefOrNullBytes
= Attr
.getDereferenceableOrNullBytes();
1476 else if (Kind
== Attribute::AllocSize
)
1477 AllocSizeArgs
= Attr
.getValueAsInt();
1481 AttrBuilder
&AttrBuilder::addAttribute(StringRef A
, StringRef V
) {
1482 TargetDepAttrs
[A
] = V
;
1486 AttrBuilder
&AttrBuilder::removeAttribute(Attribute::AttrKind Val
) {
1487 assert((unsigned)Val
< Attribute::EndAttrKinds
&& "Attribute out of range!");
1490 if (Val
== Attribute::Alignment
)
1492 else if (Val
== Attribute::StackAlignment
)
1493 StackAlignment
.reset();
1494 else if (Val
== Attribute::ByVal
)
1495 ByValType
= nullptr;
1496 else if (Val
== Attribute::Dereferenceable
)
1498 else if (Val
== Attribute::DereferenceableOrNull
)
1499 DerefOrNullBytes
= 0;
1500 else if (Val
== Attribute::AllocSize
)
1506 AttrBuilder
&AttrBuilder::removeAttributes(AttributeList A
, uint64_t Index
) {
1507 remove(A
.getAttributes(Index
));
1511 AttrBuilder
&AttrBuilder::removeAttribute(StringRef A
) {
1512 auto I
= TargetDepAttrs
.find(A
);
1513 if (I
!= TargetDepAttrs
.end())
1514 TargetDepAttrs
.erase(I
);
1518 std::pair
<unsigned, Optional
<unsigned>> AttrBuilder::getAllocSizeArgs() const {
1519 return unpackAllocSizeArgs(AllocSizeArgs
);
1522 AttrBuilder
&AttrBuilder::addAlignmentAttr(unsigned A
) {
1523 MaybeAlign
Align(A
);
1527 assert(*Align
<= 0x40000000 && "Alignment too large.");
1529 Attrs
[Attribute::Alignment
] = true;
1534 AttrBuilder
&AttrBuilder::addStackAlignmentAttr(unsigned A
) {
1535 MaybeAlign
Align(A
);
1536 // Default alignment, allow the target to define how to align it.
1540 assert(*Align
<= 0x100 && "Alignment too large.");
1542 Attrs
[Attribute::StackAlignment
] = true;
1543 StackAlignment
= Align
;
1547 AttrBuilder
&AttrBuilder::addDereferenceableAttr(uint64_t Bytes
) {
1548 if (Bytes
== 0) return *this;
1550 Attrs
[Attribute::Dereferenceable
] = true;
1555 AttrBuilder
&AttrBuilder::addDereferenceableOrNullAttr(uint64_t Bytes
) {
1559 Attrs
[Attribute::DereferenceableOrNull
] = true;
1560 DerefOrNullBytes
= Bytes
;
1564 AttrBuilder
&AttrBuilder::addAllocSizeAttr(unsigned ElemSize
,
1565 const Optional
<unsigned> &NumElems
) {
1566 return addAllocSizeAttrFromRawRepr(packAllocSizeArgs(ElemSize
, NumElems
));
1569 AttrBuilder
&AttrBuilder::addAllocSizeAttrFromRawRepr(uint64_t RawArgs
) {
1570 // (0, 0) is our "not present" value, so we need to check for it here.
1571 assert(RawArgs
&& "Invalid allocsize arguments -- given allocsize(0, 0)");
1573 Attrs
[Attribute::AllocSize
] = true;
1574 // Reuse existing machinery to store this as a single 64-bit integer so we can
1575 // save a few bytes over using a pair<unsigned, Optional<unsigned>>.
1576 AllocSizeArgs
= RawArgs
;
1580 AttrBuilder
&AttrBuilder::addByValAttr(Type
*Ty
) {
1581 Attrs
[Attribute::ByVal
] = true;
1586 AttrBuilder
&AttrBuilder::merge(const AttrBuilder
&B
) {
1587 // FIXME: What if both have alignments, but they don't match?!
1589 Alignment
= B
.Alignment
;
1591 if (!StackAlignment
)
1592 StackAlignment
= B
.StackAlignment
;
1595 DerefBytes
= B
.DerefBytes
;
1597 if (!DerefOrNullBytes
)
1598 DerefOrNullBytes
= B
.DerefOrNullBytes
;
1601 AllocSizeArgs
= B
.AllocSizeArgs
;
1604 ByValType
= B
.ByValType
;
1608 for (auto I
: B
.td_attrs())
1609 TargetDepAttrs
[I
.first
] = I
.second
;
1614 AttrBuilder
&AttrBuilder::remove(const AttrBuilder
&B
) {
1615 // FIXME: What if both have alignments, but they don't match?!
1619 if (B
.StackAlignment
)
1620 StackAlignment
.reset();
1625 if (B
.DerefOrNullBytes
)
1626 DerefOrNullBytes
= 0;
1628 if (B
.AllocSizeArgs
)
1632 ByValType
= nullptr;
1636 for (auto I
: B
.td_attrs())
1637 TargetDepAttrs
.erase(I
.first
);
1642 bool AttrBuilder::overlaps(const AttrBuilder
&B
) const {
1643 // First check if any of the target independent attributes overlap.
1644 if ((Attrs
& B
.Attrs
).any())
1647 // Then check if any target dependent ones do.
1648 for (const auto &I
: td_attrs())
1649 if (B
.contains(I
.first
))
1655 bool AttrBuilder::contains(StringRef A
) const {
1656 return TargetDepAttrs
.find(A
) != TargetDepAttrs
.end();
1659 bool AttrBuilder::hasAttributes() const {
1660 return !Attrs
.none() || !TargetDepAttrs
.empty();
1663 bool AttrBuilder::hasAttributes(AttributeList AL
, uint64_t Index
) const {
1664 AttributeSet AS
= AL
.getAttributes(Index
);
1666 for (const auto Attr
: AS
) {
1667 if (Attr
.isEnumAttribute() || Attr
.isIntAttribute()) {
1668 if (contains(Attr
.getKindAsEnum()))
1671 assert(Attr
.isStringAttribute() && "Invalid attribute kind!");
1672 return contains(Attr
.getKindAsString());
1679 bool AttrBuilder::hasAlignmentAttr() const {
1680 return Alignment
!= 0;
1683 bool AttrBuilder::operator==(const AttrBuilder
&B
) {
1684 if (Attrs
!= B
.Attrs
)
1687 for (td_const_iterator I
= TargetDepAttrs
.begin(),
1688 E
= TargetDepAttrs
.end(); I
!= E
; ++I
)
1689 if (B
.TargetDepAttrs
.find(I
->first
) == B
.TargetDepAttrs
.end())
1692 return Alignment
== B
.Alignment
&& StackAlignment
== B
.StackAlignment
&&
1693 DerefBytes
== B
.DerefBytes
&& ByValType
== B
.ByValType
;
1696 //===----------------------------------------------------------------------===//
1697 // AttributeFuncs Function Defintions
1698 //===----------------------------------------------------------------------===//
1700 /// Which attributes cannot be applied to a type.
1701 AttrBuilder
AttributeFuncs::typeIncompatible(Type
*Ty
) {
1702 AttrBuilder Incompatible
;
1704 if (!Ty
->isIntegerTy())
1705 // Attribute that only apply to integers.
1706 Incompatible
.addAttribute(Attribute::SExt
)
1707 .addAttribute(Attribute::ZExt
);
1709 if (!Ty
->isPointerTy())
1710 // Attribute that only apply to pointers.
1711 Incompatible
.addAttribute(Attribute::ByVal
)
1712 .addAttribute(Attribute::Nest
)
1713 .addAttribute(Attribute::NoAlias
)
1714 .addAttribute(Attribute::NoCapture
)
1715 .addAttribute(Attribute::NonNull
)
1716 .addDereferenceableAttr(1) // the int here is ignored
1717 .addDereferenceableOrNullAttr(1) // the int here is ignored
1718 .addAttribute(Attribute::ReadNone
)
1719 .addAttribute(Attribute::ReadOnly
)
1720 .addAttribute(Attribute::StructRet
)
1721 .addAttribute(Attribute::InAlloca
);
1723 return Incompatible
;
1726 template<typename AttrClass
>
1727 static bool isEqual(const Function
&Caller
, const Function
&Callee
) {
1728 return Caller
.getFnAttribute(AttrClass::getKind()) ==
1729 Callee
.getFnAttribute(AttrClass::getKind());
1732 /// Compute the logical AND of the attributes of the caller and the
1735 /// This function sets the caller's attribute to false if the callee's attribute
1737 template<typename AttrClass
>
1738 static void setAND(Function
&Caller
, const Function
&Callee
) {
1739 if (AttrClass::isSet(Caller
, AttrClass::getKind()) &&
1740 !AttrClass::isSet(Callee
, AttrClass::getKind()))
1741 AttrClass::set(Caller
, AttrClass::getKind(), false);
1744 /// Compute the logical OR of the attributes of the caller and the
1747 /// This function sets the caller's attribute to true if the callee's attribute
1749 template<typename AttrClass
>
1750 static void setOR(Function
&Caller
, const Function
&Callee
) {
1751 if (!AttrClass::isSet(Caller
, AttrClass::getKind()) &&
1752 AttrClass::isSet(Callee
, AttrClass::getKind()))
1753 AttrClass::set(Caller
, AttrClass::getKind(), true);
1756 /// If the inlined function had a higher stack protection level than the
1757 /// calling function, then bump up the caller's stack protection level.
1758 static void adjustCallerSSPLevel(Function
&Caller
, const Function
&Callee
) {
1759 // If upgrading the SSP attribute, clear out the old SSP Attributes first.
1760 // Having multiple SSP attributes doesn't actually hurt, but it adds useless
1761 // clutter to the IR.
1762 AttrBuilder OldSSPAttr
;
1763 OldSSPAttr
.addAttribute(Attribute::StackProtect
)
1764 .addAttribute(Attribute::StackProtectStrong
)
1765 .addAttribute(Attribute::StackProtectReq
);
1767 if (Callee
.hasFnAttribute(Attribute::StackProtectReq
)) {
1768 Caller
.removeAttributes(AttributeList::FunctionIndex
, OldSSPAttr
);
1769 Caller
.addFnAttr(Attribute::StackProtectReq
);
1770 } else if (Callee
.hasFnAttribute(Attribute::StackProtectStrong
) &&
1771 !Caller
.hasFnAttribute(Attribute::StackProtectReq
)) {
1772 Caller
.removeAttributes(AttributeList::FunctionIndex
, OldSSPAttr
);
1773 Caller
.addFnAttr(Attribute::StackProtectStrong
);
1774 } else if (Callee
.hasFnAttribute(Attribute::StackProtect
) &&
1775 !Caller
.hasFnAttribute(Attribute::StackProtectReq
) &&
1776 !Caller
.hasFnAttribute(Attribute::StackProtectStrong
))
1777 Caller
.addFnAttr(Attribute::StackProtect
);
1780 /// If the inlined function required stack probes, then ensure that
1781 /// the calling function has those too.
1782 static void adjustCallerStackProbes(Function
&Caller
, const Function
&Callee
) {
1783 if (!Caller
.hasFnAttribute("probe-stack") &&
1784 Callee
.hasFnAttribute("probe-stack")) {
1785 Caller
.addFnAttr(Callee
.getFnAttribute("probe-stack"));
1789 /// If the inlined function defines the size of guard region
1790 /// on the stack, then ensure that the calling function defines a guard region
1791 /// that is no larger.
1793 adjustCallerStackProbeSize(Function
&Caller
, const Function
&Callee
) {
1794 if (Callee
.hasFnAttribute("stack-probe-size")) {
1795 uint64_t CalleeStackProbeSize
;
1796 Callee
.getFnAttribute("stack-probe-size")
1798 .getAsInteger(0, CalleeStackProbeSize
);
1799 if (Caller
.hasFnAttribute("stack-probe-size")) {
1800 uint64_t CallerStackProbeSize
;
1801 Caller
.getFnAttribute("stack-probe-size")
1803 .getAsInteger(0, CallerStackProbeSize
);
1804 if (CallerStackProbeSize
> CalleeStackProbeSize
) {
1805 Caller
.addFnAttr(Callee
.getFnAttribute("stack-probe-size"));
1808 Caller
.addFnAttr(Callee
.getFnAttribute("stack-probe-size"));
1813 /// If the inlined function defines a min legal vector width, then ensure
1814 /// the calling function has the same or larger min legal vector width. If the
1815 /// caller has the attribute, but the callee doesn't, we need to remove the
1816 /// attribute from the caller since we can't make any guarantees about the
1817 /// caller's requirements.
1818 /// This function is called after the inlining decision has been made so we have
1819 /// to merge the attribute this way. Heuristics that would use
1820 /// min-legal-vector-width to determine inline compatibility would need to be
1821 /// handled as part of inline cost analysis.
1823 adjustMinLegalVectorWidth(Function
&Caller
, const Function
&Callee
) {
1824 if (Caller
.hasFnAttribute("min-legal-vector-width")) {
1825 if (Callee
.hasFnAttribute("min-legal-vector-width")) {
1826 uint64_t CallerVectorWidth
;
1827 Caller
.getFnAttribute("min-legal-vector-width")
1829 .getAsInteger(0, CallerVectorWidth
);
1830 uint64_t CalleeVectorWidth
;
1831 Callee
.getFnAttribute("min-legal-vector-width")
1833 .getAsInteger(0, CalleeVectorWidth
);
1834 if (CallerVectorWidth
< CalleeVectorWidth
)
1835 Caller
.addFnAttr(Callee
.getFnAttribute("min-legal-vector-width"));
1837 // If the callee doesn't have the attribute then we don't know anything
1838 // and must drop the attribute from the caller.
1839 Caller
.removeFnAttr("min-legal-vector-width");
1844 /// If the inlined function has "null-pointer-is-valid=true" attribute,
1845 /// set this attribute in the caller post inlining.
1847 adjustNullPointerValidAttr(Function
&Caller
, const Function
&Callee
) {
1848 if (Callee
.nullPointerIsDefined() && !Caller
.nullPointerIsDefined()) {
1849 Caller
.addFnAttr(Callee
.getFnAttribute("null-pointer-is-valid"));
1853 #define GET_ATTR_COMPAT_FUNC
1854 #include "AttributesCompatFunc.inc"
1856 bool AttributeFuncs::areInlineCompatible(const Function
&Caller
,
1857 const Function
&Callee
) {
1858 return hasCompatibleFnAttrs(Caller
, Callee
);
1861 void AttributeFuncs::mergeAttributesForInlining(Function
&Caller
,
1862 const Function
&Callee
) {
1863 mergeFnAttrs(Caller
, Callee
);