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 // This file implements the Attribute, AttributeImpl, AttrBuilder,
12 // AttributeListImpl, and AttributeList classes.
14 //===----------------------------------------------------------------------===//
16 #include "llvm/IR/Attributes.h"
17 #include "AttributeImpl.h"
18 #include "LLVMContextImpl.h"
19 #include "llvm/ADT/ArrayRef.h"
20 #include "llvm/ADT/FoldingSet.h"
21 #include "llvm/ADT/Optional.h"
22 #include "llvm/ADT/STLExtras.h"
23 #include "llvm/ADT/SmallVector.h"
24 #include "llvm/ADT/StringExtras.h"
25 #include "llvm/ADT/StringRef.h"
26 #include "llvm/ADT/Twine.h"
27 #include "llvm/Config/llvm-config.h"
28 #include "llvm/IR/Function.h"
29 #include "llvm/IR/LLVMContext.h"
30 #include "llvm/IR/Type.h"
31 #include "llvm/Support/Compiler.h"
32 #include "llvm/Support/Debug.h"
33 #include "llvm/Support/ErrorHandling.h"
34 #include "llvm/Support/MathExtras.h"
35 #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 {};
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 {};
197 assert(isStringAttribute() &&
198 "Invalid attribute type to get the value as a string!");
199 return pImpl
->getValueAsString();
202 bool Attribute::hasAttribute(AttrKind Kind
) const {
203 return (pImpl
&& pImpl
->hasAttribute(Kind
)) || (!pImpl
&& Kind
== None
);
206 bool Attribute::hasAttribute(StringRef Kind
) const {
207 if (!isStringAttribute()) return false;
208 return pImpl
&& pImpl
->hasAttribute(Kind
);
211 unsigned Attribute::getAlignment() const {
212 assert(hasAttribute(Attribute::Alignment
) &&
213 "Trying to get alignment from non-alignment attribute!");
214 return pImpl
->getValueAsInt();
217 unsigned Attribute::getStackAlignment() const {
218 assert(hasAttribute(Attribute::StackAlignment
) &&
219 "Trying to get alignment from non-alignment attribute!");
220 return pImpl
->getValueAsInt();
223 uint64_t Attribute::getDereferenceableBytes() const {
224 assert(hasAttribute(Attribute::Dereferenceable
) &&
225 "Trying to get dereferenceable bytes from "
226 "non-dereferenceable attribute!");
227 return pImpl
->getValueAsInt();
230 uint64_t Attribute::getDereferenceableOrNullBytes() const {
231 assert(hasAttribute(Attribute::DereferenceableOrNull
) &&
232 "Trying to get dereferenceable bytes from "
233 "non-dereferenceable attribute!");
234 return pImpl
->getValueAsInt();
237 std::pair
<unsigned, Optional
<unsigned>> Attribute::getAllocSizeArgs() const {
238 assert(hasAttribute(Attribute::AllocSize
) &&
239 "Trying to get allocsize args from non-allocsize attribute");
240 return unpackAllocSizeArgs(pImpl
->getValueAsInt());
243 std::string
Attribute::getAsString(bool InAttrGrp
) const {
244 if (!pImpl
) return {};
246 if (hasAttribute(Attribute::SanitizeAddress
))
247 return "sanitize_address";
248 if (hasAttribute(Attribute::SanitizeHWAddress
))
249 return "sanitize_hwaddress";
250 if (hasAttribute(Attribute::AlwaysInline
))
251 return "alwaysinline";
252 if (hasAttribute(Attribute::ArgMemOnly
))
254 if (hasAttribute(Attribute::Builtin
))
256 if (hasAttribute(Attribute::ByVal
))
258 if (hasAttribute(Attribute::Convergent
))
260 if (hasAttribute(Attribute::SwiftError
))
262 if (hasAttribute(Attribute::SwiftSelf
))
264 if (hasAttribute(Attribute::InaccessibleMemOnly
))
265 return "inaccessiblememonly";
266 if (hasAttribute(Attribute::InaccessibleMemOrArgMemOnly
))
267 return "inaccessiblemem_or_argmemonly";
268 if (hasAttribute(Attribute::InAlloca
))
270 if (hasAttribute(Attribute::InlineHint
))
272 if (hasAttribute(Attribute::InReg
))
274 if (hasAttribute(Attribute::JumpTable
))
276 if (hasAttribute(Attribute::MinSize
))
278 if (hasAttribute(Attribute::Naked
))
280 if (hasAttribute(Attribute::Nest
))
282 if (hasAttribute(Attribute::NoAlias
))
284 if (hasAttribute(Attribute::NoBuiltin
))
286 if (hasAttribute(Attribute::NoCapture
))
288 if (hasAttribute(Attribute::NoDuplicate
))
289 return "noduplicate";
290 if (hasAttribute(Attribute::NoImplicitFloat
))
291 return "noimplicitfloat";
292 if (hasAttribute(Attribute::NoInline
))
294 if (hasAttribute(Attribute::NonLazyBind
))
295 return "nonlazybind";
296 if (hasAttribute(Attribute::NonNull
))
298 if (hasAttribute(Attribute::NoRedZone
))
300 if (hasAttribute(Attribute::NoReturn
))
302 if (hasAttribute(Attribute::NoCfCheck
))
304 if (hasAttribute(Attribute::NoRecurse
))
306 if (hasAttribute(Attribute::NoUnwind
))
308 if (hasAttribute(Attribute::OptForFuzzing
))
309 return "optforfuzzing";
310 if (hasAttribute(Attribute::OptimizeNone
))
312 if (hasAttribute(Attribute::OptimizeForSize
))
314 if (hasAttribute(Attribute::ReadNone
))
316 if (hasAttribute(Attribute::ReadOnly
))
318 if (hasAttribute(Attribute::WriteOnly
))
320 if (hasAttribute(Attribute::Returned
))
322 if (hasAttribute(Attribute::ReturnsTwice
))
323 return "returns_twice";
324 if (hasAttribute(Attribute::SExt
))
326 if (hasAttribute(Attribute::SpeculativeLoadHardening
))
327 return "speculative_load_hardening";
328 if (hasAttribute(Attribute::Speculatable
))
329 return "speculatable";
330 if (hasAttribute(Attribute::StackProtect
))
332 if (hasAttribute(Attribute::StackProtectReq
))
334 if (hasAttribute(Attribute::StackProtectStrong
))
336 if (hasAttribute(Attribute::SafeStack
))
338 if (hasAttribute(Attribute::ShadowCallStack
))
339 return "shadowcallstack";
340 if (hasAttribute(Attribute::StrictFP
))
342 if (hasAttribute(Attribute::StructRet
))
344 if (hasAttribute(Attribute::SanitizeThread
))
345 return "sanitize_thread";
346 if (hasAttribute(Attribute::SanitizeMemory
))
347 return "sanitize_memory";
348 if (hasAttribute(Attribute::UWTable
))
350 if (hasAttribute(Attribute::ZExt
))
352 if (hasAttribute(Attribute::Cold
))
355 // FIXME: These should be output like this:
360 if (hasAttribute(Attribute::Alignment
)) {
363 Result
+= (InAttrGrp
) ? "=" : " ";
364 Result
+= utostr(getValueAsInt());
368 auto AttrWithBytesToString
= [&](const char *Name
) {
373 Result
+= utostr(getValueAsInt());
376 Result
+= utostr(getValueAsInt());
382 if (hasAttribute(Attribute::StackAlignment
))
383 return AttrWithBytesToString("alignstack");
385 if (hasAttribute(Attribute::Dereferenceable
))
386 return AttrWithBytesToString("dereferenceable");
388 if (hasAttribute(Attribute::DereferenceableOrNull
))
389 return AttrWithBytesToString("dereferenceable_or_null");
391 if (hasAttribute(Attribute::AllocSize
)) {
393 Optional
<unsigned> NumElems
;
394 std::tie(ElemSize
, NumElems
) = getAllocSizeArgs();
396 std::string Result
= "allocsize(";
397 Result
+= utostr(ElemSize
);
398 if (NumElems
.hasValue()) {
400 Result
+= utostr(*NumElems
);
406 // Convert target-dependent attributes to strings of the form:
411 if (isStringAttribute()) {
413 Result
+= (Twine('"') + getKindAsString() + Twine('"')).str();
415 std::string AttrVal
= pImpl
->getValueAsString();
416 if (AttrVal
.empty()) return Result
;
418 // Since some attribute strings contain special characters that cannot be
419 // printable, those have to be escaped to make the attribute value printable
420 // as is. e.g. "\01__gnu_mcount_nc"
422 raw_string_ostream
OS(Result
);
424 printEscapedString(AttrVal
, OS
);
430 llvm_unreachable("Unknown attribute");
433 bool Attribute::operator<(Attribute A
) const {
434 if (!pImpl
&& !A
.pImpl
) return false;
435 if (!pImpl
) return true;
436 if (!A
.pImpl
) return false;
437 return *pImpl
< *A
.pImpl
;
440 //===----------------------------------------------------------------------===//
441 // AttributeImpl Definition
442 //===----------------------------------------------------------------------===//
444 // Pin the vtables to this file.
445 AttributeImpl::~AttributeImpl() = default;
447 void EnumAttributeImpl::anchor() {}
449 void IntAttributeImpl::anchor() {}
451 void StringAttributeImpl::anchor() {}
453 bool AttributeImpl::hasAttribute(Attribute::AttrKind A
) const {
454 if (isStringAttribute()) return false;
455 return getKindAsEnum() == A
;
458 bool AttributeImpl::hasAttribute(StringRef Kind
) const {
459 if (!isStringAttribute()) return false;
460 return getKindAsString() == Kind
;
463 Attribute::AttrKind
AttributeImpl::getKindAsEnum() const {
464 assert(isEnumAttribute() || isIntAttribute());
465 return static_cast<const EnumAttributeImpl
*>(this)->getEnumKind();
468 uint64_t AttributeImpl::getValueAsInt() const {
469 assert(isIntAttribute());
470 return static_cast<const IntAttributeImpl
*>(this)->getValue();
473 StringRef
AttributeImpl::getKindAsString() const {
474 assert(isStringAttribute());
475 return static_cast<const StringAttributeImpl
*>(this)->getStringKind();
478 StringRef
AttributeImpl::getValueAsString() const {
479 assert(isStringAttribute());
480 return static_cast<const StringAttributeImpl
*>(this)->getStringValue();
483 bool AttributeImpl::operator<(const AttributeImpl
&AI
) const {
484 // This sorts the attributes with Attribute::AttrKinds coming first (sorted
485 // relative to their enum value) and then strings.
486 if (isEnumAttribute()) {
487 if (AI
.isEnumAttribute()) return getKindAsEnum() < AI
.getKindAsEnum();
488 if (AI
.isIntAttribute()) return true;
489 if (AI
.isStringAttribute()) return true;
492 if (isIntAttribute()) {
493 if (AI
.isEnumAttribute()) return false;
494 if (AI
.isIntAttribute()) {
495 if (getKindAsEnum() == AI
.getKindAsEnum())
496 return getValueAsInt() < AI
.getValueAsInt();
497 return getKindAsEnum() < AI
.getKindAsEnum();
499 if (AI
.isStringAttribute()) return true;
502 if (AI
.isEnumAttribute()) return false;
503 if (AI
.isIntAttribute()) return false;
504 if (getKindAsString() == AI
.getKindAsString())
505 return getValueAsString() < AI
.getValueAsString();
506 return getKindAsString() < AI
.getKindAsString();
509 //===----------------------------------------------------------------------===//
510 // AttributeSet Definition
511 //===----------------------------------------------------------------------===//
513 AttributeSet
AttributeSet::get(LLVMContext
&C
, const AttrBuilder
&B
) {
514 return AttributeSet(AttributeSetNode::get(C
, B
));
517 AttributeSet
AttributeSet::get(LLVMContext
&C
, ArrayRef
<Attribute
> Attrs
) {
518 return AttributeSet(AttributeSetNode::get(C
, Attrs
));
521 AttributeSet
AttributeSet::addAttribute(LLVMContext
&C
,
522 Attribute::AttrKind Kind
) const {
523 if (hasAttribute(Kind
)) return *this;
525 B
.addAttribute(Kind
);
526 return addAttributes(C
, AttributeSet::get(C
, B
));
529 AttributeSet
AttributeSet::addAttribute(LLVMContext
&C
, StringRef Kind
,
530 StringRef Value
) const {
532 B
.addAttribute(Kind
, Value
);
533 return addAttributes(C
, AttributeSet::get(C
, B
));
536 AttributeSet
AttributeSet::addAttributes(LLVMContext
&C
,
537 const AttributeSet AS
) const {
538 if (!hasAttributes())
541 if (!AS
.hasAttributes())
545 for (const auto I
: *this)
551 AttributeSet
AttributeSet::removeAttribute(LLVMContext
&C
,
552 Attribute::AttrKind Kind
) const {
553 if (!hasAttribute(Kind
)) return *this;
554 AttrBuilder
B(*this);
555 B
.removeAttribute(Kind
);
559 AttributeSet
AttributeSet::removeAttribute(LLVMContext
&C
,
560 StringRef Kind
) const {
561 if (!hasAttribute(Kind
)) return *this;
562 AttrBuilder
B(*this);
563 B
.removeAttribute(Kind
);
567 AttributeSet
AttributeSet::removeAttributes(LLVMContext
&C
,
568 const AttrBuilder
&Attrs
) const {
569 AttrBuilder
B(*this);
574 unsigned AttributeSet::getNumAttributes() const {
575 return SetNode
? SetNode
->getNumAttributes() : 0;
578 bool AttributeSet::hasAttribute(Attribute::AttrKind Kind
) const {
579 return SetNode
? SetNode
->hasAttribute(Kind
) : false;
582 bool AttributeSet::hasAttribute(StringRef Kind
) const {
583 return SetNode
? SetNode
->hasAttribute(Kind
) : false;
586 Attribute
AttributeSet::getAttribute(Attribute::AttrKind Kind
) const {
587 return SetNode
? SetNode
->getAttribute(Kind
) : Attribute();
590 Attribute
AttributeSet::getAttribute(StringRef Kind
) const {
591 return SetNode
? SetNode
->getAttribute(Kind
) : Attribute();
594 unsigned AttributeSet::getAlignment() const {
595 return SetNode
? SetNode
->getAlignment() : 0;
598 unsigned AttributeSet::getStackAlignment() const {
599 return SetNode
? SetNode
->getStackAlignment() : 0;
602 uint64_t AttributeSet::getDereferenceableBytes() const {
603 return SetNode
? SetNode
->getDereferenceableBytes() : 0;
606 uint64_t AttributeSet::getDereferenceableOrNullBytes() const {
607 return SetNode
? SetNode
->getDereferenceableOrNullBytes() : 0;
610 std::pair
<unsigned, Optional
<unsigned>> AttributeSet::getAllocSizeArgs() const {
611 return SetNode
? SetNode
->getAllocSizeArgs()
612 : std::pair
<unsigned, Optional
<unsigned>>(0, 0);
615 std::string
AttributeSet::getAsString(bool InAttrGrp
) const {
616 return SetNode
? SetNode
->getAsString(InAttrGrp
) : "";
619 AttributeSet::iterator
AttributeSet::begin() const {
620 return SetNode
? SetNode
->begin() : nullptr;
623 AttributeSet::iterator
AttributeSet::end() const {
624 return SetNode
? SetNode
->end() : nullptr;
627 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
628 LLVM_DUMP_METHOD
void AttributeSet::dump() const {
631 dbgs() << getAsString(true) << " }\n";
635 //===----------------------------------------------------------------------===//
636 // AttributeSetNode Definition
637 //===----------------------------------------------------------------------===//
639 AttributeSetNode::AttributeSetNode(ArrayRef
<Attribute
> Attrs
)
640 : AvailableAttrs(0), NumAttrs(Attrs
.size()) {
641 // There's memory after the node where we can store the entries in.
642 std::copy(Attrs
.begin(), Attrs
.end(), getTrailingObjects
<Attribute
>());
644 for (const auto I
: *this) {
645 if (!I
.isStringAttribute()) {
646 AvailableAttrs
|= ((uint64_t)1) << I
.getKindAsEnum();
651 AttributeSetNode
*AttributeSetNode::get(LLVMContext
&C
,
652 ArrayRef
<Attribute
> Attrs
) {
656 // Otherwise, build a key to look up the existing attributes.
657 LLVMContextImpl
*pImpl
= C
.pImpl
;
660 SmallVector
<Attribute
, 8> SortedAttrs(Attrs
.begin(), Attrs
.end());
661 llvm::sort(SortedAttrs
.begin(), SortedAttrs
.end());
663 for (const auto Attr
: SortedAttrs
)
667 AttributeSetNode
*PA
=
668 pImpl
->AttrsSetNodes
.FindNodeOrInsertPos(ID
, InsertPoint
);
670 // If we didn't find any existing attributes of the same shape then create a
671 // new one and insert it.
673 // Coallocate entries after the AttributeSetNode itself.
674 void *Mem
= ::operator new(totalSizeToAlloc
<Attribute
>(SortedAttrs
.size()));
675 PA
= new (Mem
) AttributeSetNode(SortedAttrs
);
676 pImpl
->AttrsSetNodes
.InsertNode(PA
, InsertPoint
);
679 // Return the AttributeSetNode that we found or created.
683 AttributeSetNode
*AttributeSetNode::get(LLVMContext
&C
, const AttrBuilder
&B
) {
684 // Add target-independent attributes.
685 SmallVector
<Attribute
, 8> Attrs
;
686 for (Attribute::AttrKind Kind
= Attribute::None
;
687 Kind
!= Attribute::EndAttrKinds
; Kind
= Attribute::AttrKind(Kind
+ 1)) {
688 if (!B
.contains(Kind
))
693 case Attribute::Alignment
:
694 Attr
= Attribute::getWithAlignment(C
, B
.getAlignment());
696 case Attribute::StackAlignment
:
697 Attr
= Attribute::getWithStackAlignment(C
, B
.getStackAlignment());
699 case Attribute::Dereferenceable
:
700 Attr
= Attribute::getWithDereferenceableBytes(
701 C
, B
.getDereferenceableBytes());
703 case Attribute::DereferenceableOrNull
:
704 Attr
= Attribute::getWithDereferenceableOrNullBytes(
705 C
, B
.getDereferenceableOrNullBytes());
707 case Attribute::AllocSize
: {
708 auto A
= B
.getAllocSizeArgs();
709 Attr
= Attribute::getWithAllocSizeArgs(C
, A
.first
, A
.second
);
713 Attr
= Attribute::get(C
, Kind
);
715 Attrs
.push_back(Attr
);
718 // Add target-dependent (string) attributes.
719 for (const auto &TDA
: B
.td_attrs())
720 Attrs
.emplace_back(Attribute::get(C
, TDA
.first
, TDA
.second
));
722 return get(C
, Attrs
);
725 bool AttributeSetNode::hasAttribute(StringRef Kind
) const {
726 for (const auto I
: *this)
727 if (I
.hasAttribute(Kind
))
732 Attribute
AttributeSetNode::getAttribute(Attribute::AttrKind Kind
) const {
733 if (hasAttribute(Kind
)) {
734 for (const auto I
: *this)
735 if (I
.hasAttribute(Kind
))
741 Attribute
AttributeSetNode::getAttribute(StringRef Kind
) const {
742 for (const auto I
: *this)
743 if (I
.hasAttribute(Kind
))
748 unsigned AttributeSetNode::getAlignment() const {
749 for (const auto I
: *this)
750 if (I
.hasAttribute(Attribute::Alignment
))
751 return I
.getAlignment();
755 unsigned AttributeSetNode::getStackAlignment() const {
756 for (const auto I
: *this)
757 if (I
.hasAttribute(Attribute::StackAlignment
))
758 return I
.getStackAlignment();
762 uint64_t AttributeSetNode::getDereferenceableBytes() const {
763 for (const auto I
: *this)
764 if (I
.hasAttribute(Attribute::Dereferenceable
))
765 return I
.getDereferenceableBytes();
769 uint64_t AttributeSetNode::getDereferenceableOrNullBytes() const {
770 for (const auto I
: *this)
771 if (I
.hasAttribute(Attribute::DereferenceableOrNull
))
772 return I
.getDereferenceableOrNullBytes();
776 std::pair
<unsigned, Optional
<unsigned>>
777 AttributeSetNode::getAllocSizeArgs() const {
778 for (const auto I
: *this)
779 if (I
.hasAttribute(Attribute::AllocSize
))
780 return I
.getAllocSizeArgs();
781 return std::make_pair(0, 0);
784 std::string
AttributeSetNode::getAsString(bool InAttrGrp
) const {
786 for (iterator I
= begin(), E
= end(); I
!= E
; ++I
) {
789 Str
+= I
->getAsString(InAttrGrp
);
794 //===----------------------------------------------------------------------===//
795 // AttributeListImpl Definition
796 //===----------------------------------------------------------------------===//
798 /// Map from AttributeList index to the internal array index. Adding one happens
799 /// to work, but it relies on unsigned integer wrapping. MSVC warns about
800 /// unsigned wrapping in constexpr functions, so write out the conditional. LLVM
801 /// folds it to add anyway.
802 static constexpr unsigned attrIdxToArrayIdx(unsigned Index
) {
803 return Index
== AttributeList::FunctionIndex
? 0 : Index
+ 1;
806 AttributeListImpl::AttributeListImpl(LLVMContext
&C
,
807 ArrayRef
<AttributeSet
> Sets
)
808 : AvailableFunctionAttrs(0), Context(C
), NumAttrSets(Sets
.size()) {
809 assert(!Sets
.empty() && "pointless AttributeListImpl");
811 // There's memory after the node where we can store the entries in.
812 std::copy(Sets
.begin(), Sets
.end(), getTrailingObjects
<AttributeSet
>());
814 // Initialize AvailableFunctionAttrs summary bitset.
815 static_assert(Attribute::EndAttrKinds
<=
816 sizeof(AvailableFunctionAttrs
) * CHAR_BIT
,
817 "Too many attributes");
818 static_assert(attrIdxToArrayIdx(AttributeList::FunctionIndex
) == 0U,
819 "function should be stored in slot 0");
820 for (const auto I
: Sets
[0]) {
821 if (!I
.isStringAttribute())
822 AvailableFunctionAttrs
|= 1ULL << I
.getKindAsEnum();
826 void AttributeListImpl::Profile(FoldingSetNodeID
&ID
) const {
827 Profile(ID
, makeArrayRef(begin(), end()));
830 void AttributeListImpl::Profile(FoldingSetNodeID
&ID
,
831 ArrayRef
<AttributeSet
> Sets
) {
832 for (const auto &Set
: Sets
)
833 ID
.AddPointer(Set
.SetNode
);
836 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
837 LLVM_DUMP_METHOD
void AttributeListImpl::dump() const {
838 AttributeList(const_cast<AttributeListImpl
*>(this)).dump();
842 //===----------------------------------------------------------------------===//
843 // AttributeList Construction and Mutation Methods
844 //===----------------------------------------------------------------------===//
846 AttributeList
AttributeList::getImpl(LLVMContext
&C
,
847 ArrayRef
<AttributeSet
> AttrSets
) {
848 assert(!AttrSets
.empty() && "pointless AttributeListImpl");
850 LLVMContextImpl
*pImpl
= C
.pImpl
;
852 AttributeListImpl::Profile(ID
, AttrSets
);
855 AttributeListImpl
*PA
=
856 pImpl
->AttrsLists
.FindNodeOrInsertPos(ID
, InsertPoint
);
858 // If we didn't find any existing attributes of the same shape then
859 // create a new one and insert it.
861 // Coallocate entries after the AttributeListImpl itself.
862 void *Mem
= ::operator new(
863 AttributeListImpl::totalSizeToAlloc
<AttributeSet
>(AttrSets
.size()));
864 PA
= new (Mem
) AttributeListImpl(C
, AttrSets
);
865 pImpl
->AttrsLists
.InsertNode(PA
, InsertPoint
);
868 // Return the AttributesList that we found or created.
869 return AttributeList(PA
);
873 AttributeList::get(LLVMContext
&C
,
874 ArrayRef
<std::pair
<unsigned, Attribute
>> Attrs
) {
875 // If there are no attributes then return a null AttributesList pointer.
879 assert(std::is_sorted(Attrs
.begin(), Attrs
.end(),
880 [](const std::pair
<unsigned, Attribute
> &LHS
,
881 const std::pair
<unsigned, Attribute
> &RHS
) {
882 return LHS
.first
< RHS
.first
;
883 }) && "Misordered Attributes list!");
884 assert(llvm::none_of(Attrs
,
885 [](const std::pair
<unsigned, Attribute
> &Pair
) {
886 return Pair
.second
.hasAttribute(Attribute::None
);
888 "Pointless attribute!");
890 // Create a vector if (unsigned, AttributeSetNode*) pairs from the attributes
892 SmallVector
<std::pair
<unsigned, AttributeSet
>, 8> AttrPairVec
;
893 for (ArrayRef
<std::pair
<unsigned, Attribute
>>::iterator I
= Attrs
.begin(),
894 E
= Attrs
.end(); I
!= E
; ) {
895 unsigned Index
= I
->first
;
896 SmallVector
<Attribute
, 4> AttrVec
;
897 while (I
!= E
&& I
->first
== Index
) {
898 AttrVec
.push_back(I
->second
);
902 AttrPairVec
.emplace_back(Index
, AttributeSet::get(C
, AttrVec
));
905 return get(C
, AttrPairVec
);
909 AttributeList::get(LLVMContext
&C
,
910 ArrayRef
<std::pair
<unsigned, AttributeSet
>> Attrs
) {
911 // If there are no attributes then return a null AttributesList pointer.
915 assert(std::is_sorted(Attrs
.begin(), Attrs
.end(),
916 [](const std::pair
<unsigned, AttributeSet
> &LHS
,
917 const std::pair
<unsigned, AttributeSet
> &RHS
) {
918 return LHS
.first
< RHS
.first
;
920 "Misordered Attributes list!");
921 assert(llvm::none_of(Attrs
,
922 [](const std::pair
<unsigned, AttributeSet
> &Pair
) {
923 return !Pair
.second
.hasAttributes();
925 "Pointless attribute!");
927 unsigned MaxIndex
= Attrs
.back().first
;
928 // If the MaxIndex is FunctionIndex and there are other indices in front
929 // of it, we need to use the largest of those to get the right size.
930 if (MaxIndex
== FunctionIndex
&& Attrs
.size() > 1)
931 MaxIndex
= Attrs
[Attrs
.size() - 2].first
;
933 SmallVector
<AttributeSet
, 4> AttrVec(attrIdxToArrayIdx(MaxIndex
) + 1);
934 for (const auto Pair
: Attrs
)
935 AttrVec
[attrIdxToArrayIdx(Pair
.first
)] = Pair
.second
;
937 return getImpl(C
, AttrVec
);
940 AttributeList
AttributeList::get(LLVMContext
&C
, AttributeSet FnAttrs
,
941 AttributeSet RetAttrs
,
942 ArrayRef
<AttributeSet
> ArgAttrs
) {
943 // Scan from the end to find the last argument with attributes. Most
944 // arguments don't have attributes, so it's nice if we can have fewer unique
945 // AttributeListImpls by dropping empty attribute sets at the end of the list.
946 unsigned NumSets
= 0;
947 for (size_t I
= ArgAttrs
.size(); I
!= 0; --I
) {
948 if (ArgAttrs
[I
- 1].hasAttributes()) {
954 // Check function and return attributes if we didn't have argument
956 if (RetAttrs
.hasAttributes())
958 else if (FnAttrs
.hasAttributes())
962 // If all attribute sets were empty, we can use the empty attribute list.
966 SmallVector
<AttributeSet
, 8> AttrSets
;
967 AttrSets
.reserve(NumSets
);
968 // If we have any attributes, we always have function attributes.
969 AttrSets
.push_back(FnAttrs
);
971 AttrSets
.push_back(RetAttrs
);
973 // Drop the empty argument attribute sets at the end.
974 ArgAttrs
= ArgAttrs
.take_front(NumSets
- 2);
975 AttrSets
.insert(AttrSets
.end(), ArgAttrs
.begin(), ArgAttrs
.end());
978 return getImpl(C
, AttrSets
);
981 AttributeList
AttributeList::get(LLVMContext
&C
, unsigned Index
,
982 const AttrBuilder
&B
) {
983 if (!B
.hasAttributes())
985 Index
= attrIdxToArrayIdx(Index
);
986 SmallVector
<AttributeSet
, 8> AttrSets(Index
+ 1);
987 AttrSets
[Index
] = AttributeSet::get(C
, B
);
988 return getImpl(C
, AttrSets
);
991 AttributeList
AttributeList::get(LLVMContext
&C
, unsigned Index
,
992 ArrayRef
<Attribute::AttrKind
> Kinds
) {
993 SmallVector
<std::pair
<unsigned, Attribute
>, 8> Attrs
;
994 for (const auto K
: Kinds
)
995 Attrs
.emplace_back(Index
, Attribute::get(C
, K
));
996 return get(C
, Attrs
);
999 AttributeList
AttributeList::get(LLVMContext
&C
, unsigned Index
,
1000 ArrayRef
<StringRef
> Kinds
) {
1001 SmallVector
<std::pair
<unsigned, Attribute
>, 8> Attrs
;
1002 for (const auto K
: Kinds
)
1003 Attrs
.emplace_back(Index
, Attribute::get(C
, K
));
1004 return get(C
, Attrs
);
1007 AttributeList
AttributeList::get(LLVMContext
&C
,
1008 ArrayRef
<AttributeList
> Attrs
) {
1011 if (Attrs
.size() == 1)
1014 unsigned MaxSize
= 0;
1015 for (const auto List
: Attrs
)
1016 MaxSize
= std::max(MaxSize
, List
.getNumAttrSets());
1018 // If every list was empty, there is no point in merging the lists.
1022 SmallVector
<AttributeSet
, 8> NewAttrSets(MaxSize
);
1023 for (unsigned I
= 0; I
< MaxSize
; ++I
) {
1024 AttrBuilder CurBuilder
;
1025 for (const auto List
: Attrs
)
1026 CurBuilder
.merge(List
.getAttributes(I
- 1));
1027 NewAttrSets
[I
] = AttributeSet::get(C
, CurBuilder
);
1030 return getImpl(C
, NewAttrSets
);
1033 AttributeList
AttributeList::addAttribute(LLVMContext
&C
, unsigned Index
,
1034 Attribute::AttrKind Kind
) const {
1035 if (hasAttribute(Index
, Kind
)) return *this;
1037 B
.addAttribute(Kind
);
1038 return addAttributes(C
, Index
, B
);
1041 AttributeList
AttributeList::addAttribute(LLVMContext
&C
, unsigned Index
,
1043 StringRef Value
) const {
1045 B
.addAttribute(Kind
, Value
);
1046 return addAttributes(C
, Index
, B
);
1049 AttributeList
AttributeList::addAttribute(LLVMContext
&C
, unsigned Index
,
1050 Attribute A
) const {
1053 return addAttributes(C
, Index
, B
);
1056 AttributeList
AttributeList::addAttributes(LLVMContext
&C
, unsigned Index
,
1057 const AttrBuilder
&B
) const {
1058 if (!B
.hasAttributes())
1062 return AttributeList::get(C
, {{Index
, AttributeSet::get(C
, B
)}});
1065 // FIXME it is not obvious how this should work for alignment. For now, say
1066 // we can't change a known alignment.
1067 unsigned OldAlign
= getAttributes(Index
).getAlignment();
1068 unsigned NewAlign
= B
.getAlignment();
1069 assert((!OldAlign
|| !NewAlign
|| OldAlign
== NewAlign
) &&
1070 "Attempt to change alignment!");
1073 Index
= attrIdxToArrayIdx(Index
);
1074 SmallVector
<AttributeSet
, 4> AttrSets(this->begin(), this->end());
1075 if (Index
>= AttrSets
.size())
1076 AttrSets
.resize(Index
+ 1);
1078 AttrBuilder
Merged(AttrSets
[Index
]);
1080 AttrSets
[Index
] = AttributeSet::get(C
, Merged
);
1082 return getImpl(C
, AttrSets
);
1085 AttributeList
AttributeList::addParamAttribute(LLVMContext
&C
,
1086 ArrayRef
<unsigned> ArgNos
,
1087 Attribute A
) const {
1088 assert(std::is_sorted(ArgNos
.begin(), ArgNos
.end()));
1090 SmallVector
<AttributeSet
, 4> AttrSets(this->begin(), this->end());
1091 unsigned MaxIndex
= attrIdxToArrayIdx(ArgNos
.back() + FirstArgIndex
);
1092 if (MaxIndex
>= AttrSets
.size())
1093 AttrSets
.resize(MaxIndex
+ 1);
1095 for (unsigned ArgNo
: ArgNos
) {
1096 unsigned Index
= attrIdxToArrayIdx(ArgNo
+ FirstArgIndex
);
1097 AttrBuilder
B(AttrSets
[Index
]);
1099 AttrSets
[Index
] = AttributeSet::get(C
, B
);
1102 return getImpl(C
, AttrSets
);
1105 AttributeList
AttributeList::removeAttribute(LLVMContext
&C
, unsigned Index
,
1106 Attribute::AttrKind Kind
) const {
1107 if (!hasAttribute(Index
, Kind
)) return *this;
1109 Index
= attrIdxToArrayIdx(Index
);
1110 SmallVector
<AttributeSet
, 4> AttrSets(this->begin(), this->end());
1111 assert(Index
< AttrSets
.size());
1113 AttrSets
[Index
] = AttrSets
[Index
].removeAttribute(C
, Kind
);
1115 return getImpl(C
, AttrSets
);
1118 AttributeList
AttributeList::removeAttribute(LLVMContext
&C
, unsigned Index
,
1119 StringRef Kind
) const {
1120 if (!hasAttribute(Index
, Kind
)) return *this;
1122 Index
= attrIdxToArrayIdx(Index
);
1123 SmallVector
<AttributeSet
, 4> AttrSets(this->begin(), this->end());
1124 assert(Index
< AttrSets
.size());
1126 AttrSets
[Index
] = AttrSets
[Index
].removeAttribute(C
, Kind
);
1128 return getImpl(C
, AttrSets
);
1132 AttributeList::removeAttributes(LLVMContext
&C
, unsigned Index
,
1133 const AttrBuilder
&AttrsToRemove
) const {
1137 Index
= attrIdxToArrayIdx(Index
);
1138 SmallVector
<AttributeSet
, 4> AttrSets(this->begin(), this->end());
1139 if (Index
>= AttrSets
.size())
1140 AttrSets
.resize(Index
+ 1);
1142 AttrSets
[Index
] = AttrSets
[Index
].removeAttributes(C
, AttrsToRemove
);
1144 return getImpl(C
, AttrSets
);
1147 AttributeList
AttributeList::removeAttributes(LLVMContext
&C
,
1148 unsigned WithoutIndex
) const {
1151 WithoutIndex
= attrIdxToArrayIdx(WithoutIndex
);
1152 if (WithoutIndex
>= getNumAttrSets())
1154 SmallVector
<AttributeSet
, 4> AttrSets(this->begin(), this->end());
1155 AttrSets
[WithoutIndex
] = AttributeSet();
1156 return getImpl(C
, AttrSets
);
1159 AttributeList
AttributeList::addDereferenceableAttr(LLVMContext
&C
,
1161 uint64_t Bytes
) const {
1163 B
.addDereferenceableAttr(Bytes
);
1164 return addAttributes(C
, Index
, B
);
1168 AttributeList::addDereferenceableOrNullAttr(LLVMContext
&C
, unsigned Index
,
1169 uint64_t Bytes
) const {
1171 B
.addDereferenceableOrNullAttr(Bytes
);
1172 return addAttributes(C
, Index
, B
);
1176 AttributeList::addAllocSizeAttr(LLVMContext
&C
, unsigned Index
,
1177 unsigned ElemSizeArg
,
1178 const Optional
<unsigned> &NumElemsArg
) {
1180 B
.addAllocSizeAttr(ElemSizeArg
, NumElemsArg
);
1181 return addAttributes(C
, Index
, B
);
1184 //===----------------------------------------------------------------------===//
1185 // AttributeList Accessor Methods
1186 //===----------------------------------------------------------------------===//
1188 LLVMContext
&AttributeList::getContext() const { return pImpl
->getContext(); }
1190 AttributeSet
AttributeList::getParamAttributes(unsigned ArgNo
) const {
1191 return getAttributes(ArgNo
+ FirstArgIndex
);
1194 AttributeSet
AttributeList::getRetAttributes() const {
1195 return getAttributes(ReturnIndex
);
1198 AttributeSet
AttributeList::getFnAttributes() const {
1199 return getAttributes(FunctionIndex
);
1202 bool AttributeList::hasAttribute(unsigned Index
,
1203 Attribute::AttrKind Kind
) const {
1204 return getAttributes(Index
).hasAttribute(Kind
);
1207 bool AttributeList::hasAttribute(unsigned Index
, StringRef Kind
) const {
1208 return getAttributes(Index
).hasAttribute(Kind
);
1211 bool AttributeList::hasAttributes(unsigned Index
) const {
1212 return getAttributes(Index
).hasAttributes();
1215 bool AttributeList::hasFnAttribute(Attribute::AttrKind Kind
) const {
1216 return pImpl
&& pImpl
->hasFnAttribute(Kind
);
1219 bool AttributeList::hasFnAttribute(StringRef Kind
) const {
1220 return hasAttribute(AttributeList::FunctionIndex
, Kind
);
1223 bool AttributeList::hasParamAttribute(unsigned ArgNo
,
1224 Attribute::AttrKind Kind
) const {
1225 return hasAttribute(ArgNo
+ FirstArgIndex
, Kind
);
1228 bool AttributeList::hasAttrSomewhere(Attribute::AttrKind Attr
,
1229 unsigned *Index
) const {
1230 if (!pImpl
) return false;
1232 for (unsigned I
= index_begin(), E
= index_end(); I
!= E
; ++I
) {
1233 if (hasAttribute(I
, Attr
)) {
1243 Attribute
AttributeList::getAttribute(unsigned Index
,
1244 Attribute::AttrKind Kind
) const {
1245 return getAttributes(Index
).getAttribute(Kind
);
1248 Attribute
AttributeList::getAttribute(unsigned Index
, StringRef Kind
) const {
1249 return getAttributes(Index
).getAttribute(Kind
);
1252 unsigned AttributeList::getRetAlignment() const {
1253 return getAttributes(ReturnIndex
).getAlignment();
1256 unsigned AttributeList::getParamAlignment(unsigned ArgNo
) const {
1257 return getAttributes(ArgNo
+ FirstArgIndex
).getAlignment();
1260 unsigned AttributeList::getStackAlignment(unsigned Index
) const {
1261 return getAttributes(Index
).getStackAlignment();
1264 uint64_t AttributeList::getDereferenceableBytes(unsigned Index
) const {
1265 return getAttributes(Index
).getDereferenceableBytes();
1268 uint64_t AttributeList::getDereferenceableOrNullBytes(unsigned Index
) const {
1269 return getAttributes(Index
).getDereferenceableOrNullBytes();
1272 std::pair
<unsigned, Optional
<unsigned>>
1273 AttributeList::getAllocSizeArgs(unsigned Index
) const {
1274 return getAttributes(Index
).getAllocSizeArgs();
1277 std::string
AttributeList::getAsString(unsigned Index
, bool InAttrGrp
) const {
1278 return getAttributes(Index
).getAsString(InAttrGrp
);
1281 AttributeSet
AttributeList::getAttributes(unsigned Index
) const {
1282 Index
= attrIdxToArrayIdx(Index
);
1283 if (!pImpl
|| Index
>= getNumAttrSets())
1285 return pImpl
->begin()[Index
];
1288 AttributeList::iterator
AttributeList::begin() const {
1289 return pImpl
? pImpl
->begin() : nullptr;
1292 AttributeList::iterator
AttributeList::end() const {
1293 return pImpl
? pImpl
->end() : nullptr;
1296 //===----------------------------------------------------------------------===//
1297 // AttributeList Introspection Methods
1298 //===----------------------------------------------------------------------===//
1300 unsigned AttributeList::getNumAttrSets() const {
1301 return pImpl
? pImpl
->NumAttrSets
: 0;
1304 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1305 LLVM_DUMP_METHOD
void AttributeList::dump() const {
1308 for (unsigned i
= index_begin(), e
= index_end(); i
!= e
; ++i
) {
1309 if (getAttributes(i
).hasAttributes())
1310 dbgs() << " { " << i
<< " => " << getAsString(i
) << " }\n";
1317 //===----------------------------------------------------------------------===//
1318 // AttrBuilder Method Implementations
1319 //===----------------------------------------------------------------------===//
1321 // FIXME: Remove this ctor, use AttributeSet.
1322 AttrBuilder::AttrBuilder(AttributeList AL
, unsigned Index
) {
1323 AttributeSet AS
= AL
.getAttributes(Index
);
1324 for (const auto &A
: AS
)
1328 AttrBuilder::AttrBuilder(AttributeSet AS
) {
1329 for (const auto &A
: AS
)
1333 void AttrBuilder::clear() {
1335 TargetDepAttrs
.clear();
1336 Alignment
= StackAlignment
= DerefBytes
= DerefOrNullBytes
= 0;
1340 AttrBuilder
&AttrBuilder::addAttribute(Attribute::AttrKind Val
) {
1341 assert((unsigned)Val
< Attribute::EndAttrKinds
&& "Attribute out of range!");
1342 assert(Val
!= Attribute::Alignment
&& Val
!= Attribute::StackAlignment
&&
1343 Val
!= Attribute::Dereferenceable
&& Val
!= Attribute::AllocSize
&&
1344 "Adding integer attribute without adding a value!");
1349 AttrBuilder
&AttrBuilder::addAttribute(Attribute Attr
) {
1350 if (Attr
.isStringAttribute()) {
1351 addAttribute(Attr
.getKindAsString(), Attr
.getValueAsString());
1355 Attribute::AttrKind Kind
= Attr
.getKindAsEnum();
1358 if (Kind
== Attribute::Alignment
)
1359 Alignment
= Attr
.getAlignment();
1360 else if (Kind
== Attribute::StackAlignment
)
1361 StackAlignment
= Attr
.getStackAlignment();
1362 else if (Kind
== Attribute::Dereferenceable
)
1363 DerefBytes
= Attr
.getDereferenceableBytes();
1364 else if (Kind
== Attribute::DereferenceableOrNull
)
1365 DerefOrNullBytes
= Attr
.getDereferenceableOrNullBytes();
1366 else if (Kind
== Attribute::AllocSize
)
1367 AllocSizeArgs
= Attr
.getValueAsInt();
1371 AttrBuilder
&AttrBuilder::addAttribute(StringRef A
, StringRef V
) {
1372 TargetDepAttrs
[A
] = V
;
1376 AttrBuilder
&AttrBuilder::removeAttribute(Attribute::AttrKind Val
) {
1377 assert((unsigned)Val
< Attribute::EndAttrKinds
&& "Attribute out of range!");
1380 if (Val
== Attribute::Alignment
)
1382 else if (Val
== Attribute::StackAlignment
)
1384 else if (Val
== Attribute::Dereferenceable
)
1386 else if (Val
== Attribute::DereferenceableOrNull
)
1387 DerefOrNullBytes
= 0;
1388 else if (Val
== Attribute::AllocSize
)
1394 AttrBuilder
&AttrBuilder::removeAttributes(AttributeList A
, uint64_t Index
) {
1395 remove(A
.getAttributes(Index
));
1399 AttrBuilder
&AttrBuilder::removeAttribute(StringRef A
) {
1400 auto I
= TargetDepAttrs
.find(A
);
1401 if (I
!= TargetDepAttrs
.end())
1402 TargetDepAttrs
.erase(I
);
1406 std::pair
<unsigned, Optional
<unsigned>> AttrBuilder::getAllocSizeArgs() const {
1407 return unpackAllocSizeArgs(AllocSizeArgs
);
1410 AttrBuilder
&AttrBuilder::addAlignmentAttr(unsigned Align
) {
1411 if (Align
== 0) return *this;
1413 assert(isPowerOf2_32(Align
) && "Alignment must be a power of two.");
1414 assert(Align
<= 0x40000000 && "Alignment too large.");
1416 Attrs
[Attribute::Alignment
] = true;
1421 AttrBuilder
&AttrBuilder::addStackAlignmentAttr(unsigned Align
) {
1422 // Default alignment, allow the target to define how to align it.
1423 if (Align
== 0) return *this;
1425 assert(isPowerOf2_32(Align
) && "Alignment must be a power of two.");
1426 assert(Align
<= 0x100 && "Alignment too large.");
1428 Attrs
[Attribute::StackAlignment
] = true;
1429 StackAlignment
= Align
;
1433 AttrBuilder
&AttrBuilder::addDereferenceableAttr(uint64_t Bytes
) {
1434 if (Bytes
== 0) return *this;
1436 Attrs
[Attribute::Dereferenceable
] = true;
1441 AttrBuilder
&AttrBuilder::addDereferenceableOrNullAttr(uint64_t Bytes
) {
1445 Attrs
[Attribute::DereferenceableOrNull
] = true;
1446 DerefOrNullBytes
= Bytes
;
1450 AttrBuilder
&AttrBuilder::addAllocSizeAttr(unsigned ElemSize
,
1451 const Optional
<unsigned> &NumElems
) {
1452 return addAllocSizeAttrFromRawRepr(packAllocSizeArgs(ElemSize
, NumElems
));
1455 AttrBuilder
&AttrBuilder::addAllocSizeAttrFromRawRepr(uint64_t RawArgs
) {
1456 // (0, 0) is our "not present" value, so we need to check for it here.
1457 assert(RawArgs
&& "Invalid allocsize arguments -- given allocsize(0, 0)");
1459 Attrs
[Attribute::AllocSize
] = true;
1460 // Reuse existing machinery to store this as a single 64-bit integer so we can
1461 // save a few bytes over using a pair<unsigned, Optional<unsigned>>.
1462 AllocSizeArgs
= RawArgs
;
1466 AttrBuilder
&AttrBuilder::merge(const AttrBuilder
&B
) {
1467 // FIXME: What if both have alignments, but they don't match?!
1469 Alignment
= B
.Alignment
;
1471 if (!StackAlignment
)
1472 StackAlignment
= B
.StackAlignment
;
1475 DerefBytes
= B
.DerefBytes
;
1477 if (!DerefOrNullBytes
)
1478 DerefOrNullBytes
= B
.DerefOrNullBytes
;
1481 AllocSizeArgs
= B
.AllocSizeArgs
;
1485 for (auto I
: B
.td_attrs())
1486 TargetDepAttrs
[I
.first
] = I
.second
;
1491 AttrBuilder
&AttrBuilder::remove(const AttrBuilder
&B
) {
1492 // FIXME: What if both have alignments, but they don't match?!
1496 if (B
.StackAlignment
)
1502 if (B
.DerefOrNullBytes
)
1503 DerefOrNullBytes
= 0;
1505 if (B
.AllocSizeArgs
)
1510 for (auto I
: B
.td_attrs())
1511 TargetDepAttrs
.erase(I
.first
);
1516 bool AttrBuilder::overlaps(const AttrBuilder
&B
) const {
1517 // First check if any of the target independent attributes overlap.
1518 if ((Attrs
& B
.Attrs
).any())
1521 // Then check if any target dependent ones do.
1522 for (const auto &I
: td_attrs())
1523 if (B
.contains(I
.first
))
1529 bool AttrBuilder::contains(StringRef A
) const {
1530 return TargetDepAttrs
.find(A
) != TargetDepAttrs
.end();
1533 bool AttrBuilder::hasAttributes() const {
1534 return !Attrs
.none() || !TargetDepAttrs
.empty();
1537 bool AttrBuilder::hasAttributes(AttributeList AL
, uint64_t Index
) const {
1538 AttributeSet AS
= AL
.getAttributes(Index
);
1540 for (const auto Attr
: AS
) {
1541 if (Attr
.isEnumAttribute() || Attr
.isIntAttribute()) {
1542 if (contains(Attr
.getKindAsEnum()))
1545 assert(Attr
.isStringAttribute() && "Invalid attribute kind!");
1546 return contains(Attr
.getKindAsString());
1553 bool AttrBuilder::hasAlignmentAttr() const {
1554 return Alignment
!= 0;
1557 bool AttrBuilder::operator==(const AttrBuilder
&B
) {
1558 if (Attrs
!= B
.Attrs
)
1561 for (td_const_iterator I
= TargetDepAttrs
.begin(),
1562 E
= TargetDepAttrs
.end(); I
!= E
; ++I
)
1563 if (B
.TargetDepAttrs
.find(I
->first
) == B
.TargetDepAttrs
.end())
1566 return Alignment
== B
.Alignment
&& StackAlignment
== B
.StackAlignment
&&
1567 DerefBytes
== B
.DerefBytes
;
1570 //===----------------------------------------------------------------------===//
1571 // AttributeFuncs Function Defintions
1572 //===----------------------------------------------------------------------===//
1574 /// Which attributes cannot be applied to a type.
1575 AttrBuilder
AttributeFuncs::typeIncompatible(Type
*Ty
) {
1576 AttrBuilder Incompatible
;
1578 if (!Ty
->isIntegerTy())
1579 // Attribute that only apply to integers.
1580 Incompatible
.addAttribute(Attribute::SExt
)
1581 .addAttribute(Attribute::ZExt
);
1583 if (!Ty
->isPointerTy())
1584 // Attribute that only apply to pointers.
1585 Incompatible
.addAttribute(Attribute::ByVal
)
1586 .addAttribute(Attribute::Nest
)
1587 .addAttribute(Attribute::NoAlias
)
1588 .addAttribute(Attribute::NoCapture
)
1589 .addAttribute(Attribute::NonNull
)
1590 .addDereferenceableAttr(1) // the int here is ignored
1591 .addDereferenceableOrNullAttr(1) // the int here is ignored
1592 .addAttribute(Attribute::ReadNone
)
1593 .addAttribute(Attribute::ReadOnly
)
1594 .addAttribute(Attribute::StructRet
)
1595 .addAttribute(Attribute::InAlloca
);
1597 return Incompatible
;
1600 template<typename AttrClass
>
1601 static bool isEqual(const Function
&Caller
, const Function
&Callee
) {
1602 return Caller
.getFnAttribute(AttrClass::getKind()) ==
1603 Callee
.getFnAttribute(AttrClass::getKind());
1606 /// Compute the logical AND of the attributes of the caller and the
1609 /// This function sets the caller's attribute to false if the callee's attribute
1611 template<typename AttrClass
>
1612 static void setAND(Function
&Caller
, const Function
&Callee
) {
1613 if (AttrClass::isSet(Caller
, AttrClass::getKind()) &&
1614 !AttrClass::isSet(Callee
, AttrClass::getKind()))
1615 AttrClass::set(Caller
, AttrClass::getKind(), false);
1618 /// Compute the logical OR of the attributes of the caller and the
1621 /// This function sets the caller's attribute to true if the callee's attribute
1623 template<typename AttrClass
>
1624 static void setOR(Function
&Caller
, const Function
&Callee
) {
1625 if (!AttrClass::isSet(Caller
, AttrClass::getKind()) &&
1626 AttrClass::isSet(Callee
, AttrClass::getKind()))
1627 AttrClass::set(Caller
, AttrClass::getKind(), true);
1630 /// If the inlined function had a higher stack protection level than the
1631 /// calling function, then bump up the caller's stack protection level.
1632 static void adjustCallerSSPLevel(Function
&Caller
, const Function
&Callee
) {
1633 // If upgrading the SSP attribute, clear out the old SSP Attributes first.
1634 // Having multiple SSP attributes doesn't actually hurt, but it adds useless
1635 // clutter to the IR.
1636 AttrBuilder OldSSPAttr
;
1637 OldSSPAttr
.addAttribute(Attribute::StackProtect
)
1638 .addAttribute(Attribute::StackProtectStrong
)
1639 .addAttribute(Attribute::StackProtectReq
);
1641 if (Callee
.hasFnAttribute(Attribute::StackProtectReq
)) {
1642 Caller
.removeAttributes(AttributeList::FunctionIndex
, OldSSPAttr
);
1643 Caller
.addFnAttr(Attribute::StackProtectReq
);
1644 } else if (Callee
.hasFnAttribute(Attribute::StackProtectStrong
) &&
1645 !Caller
.hasFnAttribute(Attribute::StackProtectReq
)) {
1646 Caller
.removeAttributes(AttributeList::FunctionIndex
, OldSSPAttr
);
1647 Caller
.addFnAttr(Attribute::StackProtectStrong
);
1648 } else if (Callee
.hasFnAttribute(Attribute::StackProtect
) &&
1649 !Caller
.hasFnAttribute(Attribute::StackProtectReq
) &&
1650 !Caller
.hasFnAttribute(Attribute::StackProtectStrong
))
1651 Caller
.addFnAttr(Attribute::StackProtect
);
1654 /// If the inlined function required stack probes, then ensure that
1655 /// the calling function has those too.
1656 static void adjustCallerStackProbes(Function
&Caller
, const Function
&Callee
) {
1657 if (!Caller
.hasFnAttribute("probe-stack") &&
1658 Callee
.hasFnAttribute("probe-stack")) {
1659 Caller
.addFnAttr(Callee
.getFnAttribute("probe-stack"));
1663 /// If the inlined function defines the size of guard region
1664 /// on the stack, then ensure that the calling function defines a guard region
1665 /// that is no larger.
1667 adjustCallerStackProbeSize(Function
&Caller
, const Function
&Callee
) {
1668 if (Callee
.hasFnAttribute("stack-probe-size")) {
1669 uint64_t CalleeStackProbeSize
;
1670 Callee
.getFnAttribute("stack-probe-size")
1672 .getAsInteger(0, CalleeStackProbeSize
);
1673 if (Caller
.hasFnAttribute("stack-probe-size")) {
1674 uint64_t CallerStackProbeSize
;
1675 Caller
.getFnAttribute("stack-probe-size")
1677 .getAsInteger(0, CallerStackProbeSize
);
1678 if (CallerStackProbeSize
> CalleeStackProbeSize
) {
1679 Caller
.addFnAttr(Callee
.getFnAttribute("stack-probe-size"));
1682 Caller
.addFnAttr(Callee
.getFnAttribute("stack-probe-size"));
1687 /// If the inlined function defines a min legal vector width, then ensure
1688 /// the calling function has the same or larger min legal vector width. This
1689 /// function is called after the inlining decision has been made so we have to
1690 /// merge the attribute this way. Heuristics that would use
1691 /// min-legal-vector-width to determine inline compatibility would need to be
1692 /// handled as part of inline cost analysis.
1694 adjustMinLegalVectorWidth(Function
&Caller
, const Function
&Callee
) {
1695 if (Callee
.hasFnAttribute("min-legal-vector-width")) {
1696 uint64_t CalleeVectorWidth
;
1697 Callee
.getFnAttribute("min-legal-vector-width")
1699 .getAsInteger(0, CalleeVectorWidth
);
1700 if (Caller
.hasFnAttribute("min-legal-vector-width")) {
1701 uint64_t CallerVectorWidth
;
1702 Caller
.getFnAttribute("min-legal-vector-width")
1704 .getAsInteger(0, CallerVectorWidth
);
1705 if (CallerVectorWidth
< CalleeVectorWidth
) {
1706 Caller
.addFnAttr(Callee
.getFnAttribute("min-legal-vector-width"));
1709 Caller
.addFnAttr(Callee
.getFnAttribute("min-legal-vector-width"));
1714 /// If the inlined function has "null-pointer-is-valid=true" attribute,
1715 /// set this attribute in the caller post inlining.
1717 adjustNullPointerValidAttr(Function
&Caller
, const Function
&Callee
) {
1718 if (Callee
.nullPointerIsDefined() && !Caller
.nullPointerIsDefined()) {
1719 Caller
.addFnAttr(Callee
.getFnAttribute("null-pointer-is-valid"));
1723 #define GET_ATTR_COMPAT_FUNC
1724 #include "AttributesCompatFunc.inc"
1726 bool AttributeFuncs::areInlineCompatible(const Function
&Caller
,
1727 const Function
&Callee
) {
1728 return hasCompatibleFnAttrs(Caller
, Callee
);
1731 void AttributeFuncs::mergeAttributesForInlining(Function
&Caller
,
1732 const Function
&Callee
) {
1733 mergeFnAttrs(Caller
, Callee
);