1 //===- Function.cpp - Implement the Global object classes -----------------===//
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 //===----------------------------------------------------------------------===//
9 // This file implements the Function class for the IR library.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/IR/Function.h"
14 #include "SymbolTableListTraitsImpl.h"
15 #include "llvm/ADT/ArrayRef.h"
16 #include "llvm/ADT/BitVector.h"
17 #include "llvm/ADT/DenseSet.h"
18 #include "llvm/ADT/STLExtras.h"
19 #include "llvm/ADT/SmallString.h"
20 #include "llvm/ADT/SmallVector.h"
21 #include "llvm/ADT/StringRef.h"
22 #include "llvm/IR/AbstractCallSite.h"
23 #include "llvm/IR/Argument.h"
24 #include "llvm/IR/Attributes.h"
25 #include "llvm/IR/BasicBlock.h"
26 #include "llvm/IR/Constant.h"
27 #include "llvm/IR/ConstantRange.h"
28 #include "llvm/IR/Constants.h"
29 #include "llvm/IR/DerivedTypes.h"
30 #include "llvm/IR/GlobalValue.h"
31 #include "llvm/IR/InstIterator.h"
32 #include "llvm/IR/Instruction.h"
33 #include "llvm/IR/IntrinsicInst.h"
34 #include "llvm/IR/Intrinsics.h"
35 #include "llvm/IR/LLVMContext.h"
36 #include "llvm/IR/MDBuilder.h"
37 #include "llvm/IR/Metadata.h"
38 #include "llvm/IR/Module.h"
39 #include "llvm/IR/Operator.h"
40 #include "llvm/IR/SymbolTableListTraits.h"
41 #include "llvm/IR/Type.h"
42 #include "llvm/IR/Use.h"
43 #include "llvm/IR/User.h"
44 #include "llvm/IR/Value.h"
45 #include "llvm/IR/ValueSymbolTable.h"
46 #include "llvm/Support/Casting.h"
47 #include "llvm/Support/CommandLine.h"
48 #include "llvm/Support/Compiler.h"
49 #include "llvm/Support/ErrorHandling.h"
50 #include "llvm/Support/ModRef.h"
58 using ProfileCount
= Function::ProfileCount
;
60 // Explicit instantiations of SymbolTableListTraits since some of the methods
61 // are not in the public header file...
62 template class llvm::SymbolTableListTraits
<BasicBlock
>;
64 static cl::opt
<int> NonGlobalValueMaxNameSize(
65 "non-global-value-max-name-size", cl::Hidden
, cl::init(1024),
66 cl::desc("Maximum size for the name of non-global values."));
68 extern cl::opt
<bool> UseNewDbgInfoFormat
;
70 void Function::renumberBlocks() {
71 validateBlockNumbers();
74 for (auto &BB
: *this)
75 BB
.Number
= NextBlockNum
++;
79 void Function::validateBlockNumbers() const {
81 BitVector
Numbers(NextBlockNum
);
82 for (const auto &BB
: *this) {
83 unsigned Num
= BB
.getNumber();
84 assert(Num
< NextBlockNum
&& "out of range block number");
85 assert(!Numbers
[Num
] && "duplicate block numbers");
91 void Function::convertToNewDbgValues() {
92 IsNewDbgInfoFormat
= true;
93 for (auto &BB
: *this) {
94 BB
.convertToNewDbgValues();
98 void Function::convertFromNewDbgValues() {
99 IsNewDbgInfoFormat
= false;
100 for (auto &BB
: *this) {
101 BB
.convertFromNewDbgValues();
105 void Function::setIsNewDbgInfoFormat(bool NewFlag
) {
106 if (NewFlag
&& !IsNewDbgInfoFormat
)
107 convertToNewDbgValues();
108 else if (!NewFlag
&& IsNewDbgInfoFormat
)
109 convertFromNewDbgValues();
111 void Function::setNewDbgInfoFormatFlag(bool NewFlag
) {
112 for (auto &BB
: *this) {
113 BB
.setNewDbgInfoFormatFlag(NewFlag
);
115 IsNewDbgInfoFormat
= NewFlag
;
118 //===----------------------------------------------------------------------===//
119 // Argument Implementation
120 //===----------------------------------------------------------------------===//
122 Argument::Argument(Type
*Ty
, const Twine
&Name
, Function
*Par
, unsigned ArgNo
)
123 : Value(Ty
, Value::ArgumentVal
), Parent(Par
), ArgNo(ArgNo
) {
127 void Argument::setParent(Function
*parent
) {
131 bool Argument::hasNonNullAttr(bool AllowUndefOrPoison
) const {
132 if (!getType()->isPointerTy()) return false;
133 if (getParent()->hasParamAttribute(getArgNo(), Attribute::NonNull
) &&
134 (AllowUndefOrPoison
||
135 getParent()->hasParamAttribute(getArgNo(), Attribute::NoUndef
)))
137 else if (getDereferenceableBytes() > 0 &&
138 !NullPointerIsDefined(getParent(),
139 getType()->getPointerAddressSpace()))
144 bool Argument::hasByValAttr() const {
145 if (!getType()->isPointerTy()) return false;
146 return hasAttribute(Attribute::ByVal
);
149 bool Argument::hasByRefAttr() const {
150 if (!getType()->isPointerTy())
152 return hasAttribute(Attribute::ByRef
);
155 bool Argument::hasSwiftSelfAttr() const {
156 return getParent()->hasParamAttribute(getArgNo(), Attribute::SwiftSelf
);
159 bool Argument::hasSwiftErrorAttr() const {
160 return getParent()->hasParamAttribute(getArgNo(), Attribute::SwiftError
);
163 bool Argument::hasInAllocaAttr() const {
164 if (!getType()->isPointerTy()) return false;
165 return hasAttribute(Attribute::InAlloca
);
168 bool Argument::hasPreallocatedAttr() const {
169 if (!getType()->isPointerTy())
171 return hasAttribute(Attribute::Preallocated
);
174 bool Argument::hasPassPointeeByValueCopyAttr() const {
175 if (!getType()->isPointerTy()) return false;
176 AttributeList Attrs
= getParent()->getAttributes();
177 return Attrs
.hasParamAttr(getArgNo(), Attribute::ByVal
) ||
178 Attrs
.hasParamAttr(getArgNo(), Attribute::InAlloca
) ||
179 Attrs
.hasParamAttr(getArgNo(), Attribute::Preallocated
);
182 bool Argument::hasPointeeInMemoryValueAttr() const {
183 if (!getType()->isPointerTy())
185 AttributeList Attrs
= getParent()->getAttributes();
186 return Attrs
.hasParamAttr(getArgNo(), Attribute::ByVal
) ||
187 Attrs
.hasParamAttr(getArgNo(), Attribute::StructRet
) ||
188 Attrs
.hasParamAttr(getArgNo(), Attribute::InAlloca
) ||
189 Attrs
.hasParamAttr(getArgNo(), Attribute::Preallocated
) ||
190 Attrs
.hasParamAttr(getArgNo(), Attribute::ByRef
);
193 /// For a byval, sret, inalloca, or preallocated parameter, get the in-memory
195 static Type
*getMemoryParamAllocType(AttributeSet ParamAttrs
) {
196 // FIXME: All the type carrying attributes are mutually exclusive, so there
197 // should be a single query to get the stored type that handles any of them.
198 if (Type
*ByValTy
= ParamAttrs
.getByValType())
200 if (Type
*ByRefTy
= ParamAttrs
.getByRefType())
202 if (Type
*PreAllocTy
= ParamAttrs
.getPreallocatedType())
204 if (Type
*InAllocaTy
= ParamAttrs
.getInAllocaType())
206 if (Type
*SRetTy
= ParamAttrs
.getStructRetType())
212 uint64_t Argument::getPassPointeeByValueCopySize(const DataLayout
&DL
) const {
213 AttributeSet ParamAttrs
=
214 getParent()->getAttributes().getParamAttrs(getArgNo());
215 if (Type
*MemTy
= getMemoryParamAllocType(ParamAttrs
))
216 return DL
.getTypeAllocSize(MemTy
);
220 Type
*Argument::getPointeeInMemoryValueType() const {
221 AttributeSet ParamAttrs
=
222 getParent()->getAttributes().getParamAttrs(getArgNo());
223 return getMemoryParamAllocType(ParamAttrs
);
226 MaybeAlign
Argument::getParamAlign() const {
227 assert(getType()->isPointerTy() && "Only pointers have alignments");
228 return getParent()->getParamAlign(getArgNo());
231 MaybeAlign
Argument::getParamStackAlign() const {
232 return getParent()->getParamStackAlign(getArgNo());
235 Type
*Argument::getParamByValType() const {
236 assert(getType()->isPointerTy() && "Only pointers have byval types");
237 return getParent()->getParamByValType(getArgNo());
240 Type
*Argument::getParamStructRetType() const {
241 assert(getType()->isPointerTy() && "Only pointers have sret types");
242 return getParent()->getParamStructRetType(getArgNo());
245 Type
*Argument::getParamByRefType() const {
246 assert(getType()->isPointerTy() && "Only pointers have byref types");
247 return getParent()->getParamByRefType(getArgNo());
250 Type
*Argument::getParamInAllocaType() const {
251 assert(getType()->isPointerTy() && "Only pointers have inalloca types");
252 return getParent()->getParamInAllocaType(getArgNo());
255 uint64_t Argument::getDereferenceableBytes() const {
256 assert(getType()->isPointerTy() &&
257 "Only pointers have dereferenceable bytes");
258 return getParent()->getParamDereferenceableBytes(getArgNo());
261 uint64_t Argument::getDereferenceableOrNullBytes() const {
262 assert(getType()->isPointerTy() &&
263 "Only pointers have dereferenceable bytes");
264 return getParent()->getParamDereferenceableOrNullBytes(getArgNo());
267 FPClassTest
Argument::getNoFPClass() const {
268 return getParent()->getParamNoFPClass(getArgNo());
271 std::optional
<ConstantRange
> Argument::getRange() const {
272 const Attribute RangeAttr
= getAttribute(llvm::Attribute::Range
);
273 if (RangeAttr
.isValid())
274 return RangeAttr
.getRange();
278 bool Argument::hasNestAttr() const {
279 if (!getType()->isPointerTy()) return false;
280 return hasAttribute(Attribute::Nest
);
283 bool Argument::hasNoAliasAttr() const {
284 if (!getType()->isPointerTy()) return false;
285 return hasAttribute(Attribute::NoAlias
);
288 bool Argument::hasNoCaptureAttr() const {
289 if (!getType()->isPointerTy()) return false;
290 return hasAttribute(Attribute::NoCapture
);
293 bool Argument::hasNoFreeAttr() const {
294 if (!getType()->isPointerTy()) return false;
295 return hasAttribute(Attribute::NoFree
);
298 bool Argument::hasStructRetAttr() const {
299 if (!getType()->isPointerTy()) return false;
300 return hasAttribute(Attribute::StructRet
);
303 bool Argument::hasInRegAttr() const {
304 return hasAttribute(Attribute::InReg
);
307 bool Argument::hasReturnedAttr() const {
308 return hasAttribute(Attribute::Returned
);
311 bool Argument::hasZExtAttr() const {
312 return hasAttribute(Attribute::ZExt
);
315 bool Argument::hasSExtAttr() const {
316 return hasAttribute(Attribute::SExt
);
319 bool Argument::onlyReadsMemory() const {
320 AttributeList Attrs
= getParent()->getAttributes();
321 return Attrs
.hasParamAttr(getArgNo(), Attribute::ReadOnly
) ||
322 Attrs
.hasParamAttr(getArgNo(), Attribute::ReadNone
);
325 void Argument::addAttrs(AttrBuilder
&B
) {
326 AttributeList AL
= getParent()->getAttributes();
327 AL
= AL
.addParamAttributes(Parent
->getContext(), getArgNo(), B
);
328 getParent()->setAttributes(AL
);
331 void Argument::addAttr(Attribute::AttrKind Kind
) {
332 getParent()->addParamAttr(getArgNo(), Kind
);
335 void Argument::addAttr(Attribute Attr
) {
336 getParent()->addParamAttr(getArgNo(), Attr
);
339 void Argument::removeAttr(Attribute::AttrKind Kind
) {
340 getParent()->removeParamAttr(getArgNo(), Kind
);
343 void Argument::removeAttrs(const AttributeMask
&AM
) {
344 AttributeList AL
= getParent()->getAttributes();
345 AL
= AL
.removeParamAttributes(Parent
->getContext(), getArgNo(), AM
);
346 getParent()->setAttributes(AL
);
349 bool Argument::hasAttribute(Attribute::AttrKind Kind
) const {
350 return getParent()->hasParamAttribute(getArgNo(), Kind
);
353 bool Argument::hasAttribute(StringRef Kind
) const {
354 return getParent()->hasParamAttribute(getArgNo(), Kind
);
357 Attribute
Argument::getAttribute(Attribute::AttrKind Kind
) const {
358 return getParent()->getParamAttribute(getArgNo(), Kind
);
361 AttributeSet
Argument::getAttributes() const {
362 return getParent()->getAttributes().getParamAttrs(getArgNo());
365 //===----------------------------------------------------------------------===//
366 // Helper Methods in Function
367 //===----------------------------------------------------------------------===//
369 LLVMContext
&Function::getContext() const {
370 return getType()->getContext();
373 const DataLayout
&Function::getDataLayout() const {
374 return getParent()->getDataLayout();
377 unsigned Function::getInstructionCount() const {
378 unsigned NumInstrs
= 0;
379 for (const BasicBlock
&BB
: BasicBlocks
)
380 NumInstrs
+= std::distance(BB
.instructionsWithoutDebug().begin(),
381 BB
.instructionsWithoutDebug().end());
385 Function
*Function::Create(FunctionType
*Ty
, LinkageTypes Linkage
,
386 const Twine
&N
, Module
&M
) {
387 return Create(Ty
, Linkage
, M
.getDataLayout().getProgramAddressSpace(), N
, &M
);
390 Function
*Function::createWithDefaultAttr(FunctionType
*Ty
,
391 LinkageTypes Linkage
,
392 unsigned AddrSpace
, const Twine
&N
,
394 auto *F
= new (AllocMarker
) Function(Ty
, Linkage
, AddrSpace
, N
, M
);
395 AttrBuilder
B(F
->getContext());
396 UWTableKind UWTable
= M
->getUwtable();
397 if (UWTable
!= UWTableKind::None
)
398 B
.addUWTableAttr(UWTable
);
399 switch (M
->getFramePointer()) {
400 case FramePointerKind::None
:
401 // 0 ("none") is the default.
403 case FramePointerKind::Reserved
:
404 B
.addAttribute("frame-pointer", "reserved");
406 case FramePointerKind::NonLeaf
:
407 B
.addAttribute("frame-pointer", "non-leaf");
409 case FramePointerKind::All
:
410 B
.addAttribute("frame-pointer", "all");
413 if (M
->getModuleFlag("function_return_thunk_extern"))
414 B
.addAttribute(Attribute::FnRetThunkExtern
);
415 StringRef DefaultCPU
= F
->getContext().getDefaultTargetCPU();
416 if (!DefaultCPU
.empty())
417 B
.addAttribute("target-cpu", DefaultCPU
);
418 StringRef DefaultFeatures
= F
->getContext().getDefaultTargetFeatures();
419 if (!DefaultFeatures
.empty())
420 B
.addAttribute("target-features", DefaultFeatures
);
422 // Check if the module attribute is present and not zero.
423 auto isModuleAttributeSet
= [&](const StringRef
&ModAttr
) -> bool {
425 mdconst::extract_or_null
<ConstantInt
>(M
->getModuleFlag(ModAttr
));
426 return Attr
&& !Attr
->isZero();
429 auto AddAttributeIfSet
= [&](const StringRef
&ModAttr
) {
430 if (isModuleAttributeSet(ModAttr
))
431 B
.addAttribute(ModAttr
);
434 StringRef SignType
= "none";
435 if (isModuleAttributeSet("sign-return-address"))
436 SignType
= "non-leaf";
437 if (isModuleAttributeSet("sign-return-address-all"))
439 if (SignType
!= "none") {
440 B
.addAttribute("sign-return-address", SignType
);
441 B
.addAttribute("sign-return-address-key",
442 isModuleAttributeSet("sign-return-address-with-bkey")
446 AddAttributeIfSet("branch-target-enforcement");
447 AddAttributeIfSet("branch-protection-pauth-lr");
448 AddAttributeIfSet("guarded-control-stack");
454 void Function::removeFromParent() {
455 getParent()->getFunctionList().remove(getIterator());
458 void Function::eraseFromParent() {
459 getParent()->getFunctionList().erase(getIterator());
462 void Function::splice(Function::iterator ToIt
, Function
*FromF
,
463 Function::iterator FromBeginIt
,
464 Function::iterator FromEndIt
) {
465 #ifdef EXPENSIVE_CHECKS
466 // Check that FromBeginIt is before FromEndIt.
467 auto FromFEnd
= FromF
->end();
468 for (auto It
= FromBeginIt
; It
!= FromEndIt
; ++It
)
469 assert(It
!= FromFEnd
&& "FromBeginIt not before FromEndIt!");
470 #endif // EXPENSIVE_CHECKS
471 BasicBlocks
.splice(ToIt
, FromF
->BasicBlocks
, FromBeginIt
, FromEndIt
);
474 Function::iterator
Function::erase(Function::iterator FromIt
,
475 Function::iterator ToIt
) {
476 return BasicBlocks
.erase(FromIt
, ToIt
);
479 //===----------------------------------------------------------------------===//
480 // Function Implementation
481 //===----------------------------------------------------------------------===//
483 static unsigned computeAddrSpace(unsigned AddrSpace
, Module
*M
) {
484 // If AS == -1 and we are passed a valid module pointer we place the function
485 // in the program address space. Otherwise we default to AS0.
486 if (AddrSpace
== static_cast<unsigned>(-1))
487 return M
? M
->getDataLayout().getProgramAddressSpace() : 0;
491 Function::Function(FunctionType
*Ty
, LinkageTypes Linkage
, unsigned AddrSpace
,
492 const Twine
&name
, Module
*ParentModule
)
493 : GlobalObject(Ty
, Value::FunctionVal
, AllocMarker
, Linkage
, name
,
494 computeAddrSpace(AddrSpace
, ParentModule
)),
495 NumArgs(Ty
->getNumParams()), IsNewDbgInfoFormat(UseNewDbgInfoFormat
) {
496 assert(FunctionType::isValidReturnType(getReturnType()) &&
497 "invalid return type");
498 setGlobalObjectSubClassData(0);
500 // We only need a symbol table for a function if the context keeps value names
501 if (!getContext().shouldDiscardValueNames())
502 SymTab
= std::make_unique
<ValueSymbolTable
>(NonGlobalValueMaxNameSize
);
504 // If the function has arguments, mark them as lazily built.
505 if (Ty
->getNumParams())
506 setValueSubclassData(1); // Set the "has lazy arguments" bit.
509 ParentModule
->getFunctionList().push_back(this);
510 IsNewDbgInfoFormat
= ParentModule
->IsNewDbgInfoFormat
;
513 HasLLVMReservedName
= getName().starts_with("llvm.");
514 // Ensure intrinsics have the right parameter attributes.
515 // Note, the IntID field will have been set in Value::setName if this function
516 // name is a valid intrinsic ID.
518 setAttributes(Intrinsic::getAttributes(getContext(), IntID
));
521 Function::~Function() {
522 validateBlockNumbers();
524 dropAllReferences(); // After this it is safe to delete instructions.
526 // Delete all of the method arguments and unlink from symbol table...
530 // Remove the function from the on-the-side GC table.
534 void Function::BuildLazyArguments() const {
535 // Create the arguments vector, all arguments start out unnamed.
536 auto *FT
= getFunctionType();
538 Arguments
= std::allocator
<Argument
>().allocate(NumArgs
);
539 for (unsigned i
= 0, e
= NumArgs
; i
!= e
; ++i
) {
540 Type
*ArgTy
= FT
->getParamType(i
);
541 assert(!ArgTy
->isVoidTy() && "Cannot have void typed arguments!");
542 new (Arguments
+ i
) Argument(ArgTy
, "", const_cast<Function
*>(this), i
);
546 // Clear the lazy arguments bit.
547 unsigned SDC
= getSubclassDataFromValue();
549 const_cast<Function
*>(this)->setValueSubclassData(SDC
);
550 assert(!hasLazyArguments());
553 static MutableArrayRef
<Argument
> makeArgArray(Argument
*Args
, size_t Count
) {
554 return MutableArrayRef
<Argument
>(Args
, Count
);
557 bool Function::isConstrainedFPIntrinsic() const {
558 return Intrinsic::isConstrainedFPIntrinsic(getIntrinsicID());
561 void Function::clearArguments() {
562 for (Argument
&A
: makeArgArray(Arguments
, NumArgs
)) {
566 std::allocator
<Argument
>().deallocate(Arguments
, NumArgs
);
570 void Function::stealArgumentListFrom(Function
&Src
) {
571 assert(isDeclaration() && "Expected no references to current arguments");
573 // Drop the current arguments, if any, and set the lazy argument bit.
574 if (!hasLazyArguments()) {
575 assert(llvm::all_of(makeArgArray(Arguments
, NumArgs
),
576 [](const Argument
&A
) { return A
.use_empty(); }) &&
577 "Expected arguments to be unused in declaration");
579 setValueSubclassData(getSubclassDataFromValue() | (1 << 0));
582 // Nothing to steal if Src has lazy arguments.
583 if (Src
.hasLazyArguments())
586 // Steal arguments from Src, and fix the lazy argument bits.
587 assert(arg_size() == Src
.arg_size());
588 Arguments
= Src
.Arguments
;
589 Src
.Arguments
= nullptr;
590 for (Argument
&A
: makeArgArray(Arguments
, NumArgs
)) {
591 // FIXME: This does the work of transferNodesFromList inefficiently.
592 SmallString
<128> Name
;
602 setValueSubclassData(getSubclassDataFromValue() & ~(1 << 0));
603 assert(!hasLazyArguments());
604 Src
.setValueSubclassData(Src
.getSubclassDataFromValue() | (1 << 0));
607 void Function::deleteBodyImpl(bool ShouldDrop
) {
608 setIsMaterializable(false);
610 for (BasicBlock
&BB
: *this)
611 BB
.dropAllReferences();
613 // Delete all basic blocks. They are now unused, except possibly by
614 // blockaddresses, but BasicBlock's destructor takes care of those.
615 while (!BasicBlocks
.empty())
616 BasicBlocks
.begin()->eraseFromParent();
618 if (getNumOperands()) {
620 // Drop uses of any optional data (real or placeholder).
621 User::dropAllReferences();
622 setNumHungOffUseOperands(0);
624 // The code needs to match Function::allocHungoffUselist().
625 auto *CPN
= ConstantPointerNull::get(PointerType::get(getContext(), 0));
630 setValueSubclassData(getSubclassDataFromValue() & ~0xe);
633 // Metadata is stored in a side-table.
637 void Function::addAttributeAtIndex(unsigned i
, Attribute Attr
) {
638 AttributeSets
= AttributeSets
.addAttributeAtIndex(getContext(), i
, Attr
);
641 void Function::addFnAttr(Attribute::AttrKind Kind
) {
642 AttributeSets
= AttributeSets
.addFnAttribute(getContext(), Kind
);
645 void Function::addFnAttr(StringRef Kind
, StringRef Val
) {
646 AttributeSets
= AttributeSets
.addFnAttribute(getContext(), Kind
, Val
);
649 void Function::addFnAttr(Attribute Attr
) {
650 AttributeSets
= AttributeSets
.addFnAttribute(getContext(), Attr
);
653 void Function::addFnAttrs(const AttrBuilder
&Attrs
) {
654 AttributeSets
= AttributeSets
.addFnAttributes(getContext(), Attrs
);
657 void Function::addRetAttr(Attribute::AttrKind Kind
) {
658 AttributeSets
= AttributeSets
.addRetAttribute(getContext(), Kind
);
661 void Function::addRetAttr(Attribute Attr
) {
662 AttributeSets
= AttributeSets
.addRetAttribute(getContext(), Attr
);
665 void Function::addRetAttrs(const AttrBuilder
&Attrs
) {
666 AttributeSets
= AttributeSets
.addRetAttributes(getContext(), Attrs
);
669 void Function::addParamAttr(unsigned ArgNo
, Attribute::AttrKind Kind
) {
670 AttributeSets
= AttributeSets
.addParamAttribute(getContext(), ArgNo
, Kind
);
673 void Function::addParamAttr(unsigned ArgNo
, Attribute Attr
) {
674 AttributeSets
= AttributeSets
.addParamAttribute(getContext(), ArgNo
, Attr
);
677 void Function::addParamAttrs(unsigned ArgNo
, const AttrBuilder
&Attrs
) {
678 AttributeSets
= AttributeSets
.addParamAttributes(getContext(), ArgNo
, Attrs
);
681 void Function::removeAttributeAtIndex(unsigned i
, Attribute::AttrKind Kind
) {
682 AttributeSets
= AttributeSets
.removeAttributeAtIndex(getContext(), i
, Kind
);
685 void Function::removeAttributeAtIndex(unsigned i
, StringRef Kind
) {
686 AttributeSets
= AttributeSets
.removeAttributeAtIndex(getContext(), i
, Kind
);
689 void Function::removeFnAttr(Attribute::AttrKind Kind
) {
690 AttributeSets
= AttributeSets
.removeFnAttribute(getContext(), Kind
);
693 void Function::removeFnAttr(StringRef Kind
) {
694 AttributeSets
= AttributeSets
.removeFnAttribute(getContext(), Kind
);
697 void Function::removeFnAttrs(const AttributeMask
&AM
) {
698 AttributeSets
= AttributeSets
.removeFnAttributes(getContext(), AM
);
701 void Function::removeRetAttr(Attribute::AttrKind Kind
) {
702 AttributeSets
= AttributeSets
.removeRetAttribute(getContext(), Kind
);
705 void Function::removeRetAttr(StringRef Kind
) {
706 AttributeSets
= AttributeSets
.removeRetAttribute(getContext(), Kind
);
709 void Function::removeRetAttrs(const AttributeMask
&Attrs
) {
710 AttributeSets
= AttributeSets
.removeRetAttributes(getContext(), Attrs
);
713 void Function::removeParamAttr(unsigned ArgNo
, Attribute::AttrKind Kind
) {
714 AttributeSets
= AttributeSets
.removeParamAttribute(getContext(), ArgNo
, Kind
);
717 void Function::removeParamAttr(unsigned ArgNo
, StringRef Kind
) {
718 AttributeSets
= AttributeSets
.removeParamAttribute(getContext(), ArgNo
, Kind
);
721 void Function::removeParamAttrs(unsigned ArgNo
, const AttributeMask
&Attrs
) {
723 AttributeSets
.removeParamAttributes(getContext(), ArgNo
, Attrs
);
726 void Function::addDereferenceableParamAttr(unsigned ArgNo
, uint64_t Bytes
) {
728 AttributeSets
.addDereferenceableParamAttr(getContext(), ArgNo
, Bytes
);
731 bool Function::hasFnAttribute(Attribute::AttrKind Kind
) const {
732 return AttributeSets
.hasFnAttr(Kind
);
735 bool Function::hasFnAttribute(StringRef Kind
) const {
736 return AttributeSets
.hasFnAttr(Kind
);
739 bool Function::hasRetAttribute(Attribute::AttrKind Kind
) const {
740 return AttributeSets
.hasRetAttr(Kind
);
743 bool Function::hasParamAttribute(unsigned ArgNo
,
744 Attribute::AttrKind Kind
) const {
745 return AttributeSets
.hasParamAttr(ArgNo
, Kind
);
748 bool Function::hasParamAttribute(unsigned ArgNo
, StringRef Kind
) const {
749 return AttributeSets
.hasParamAttr(ArgNo
, Kind
);
752 Attribute
Function::getAttributeAtIndex(unsigned i
,
753 Attribute::AttrKind Kind
) const {
754 return AttributeSets
.getAttributeAtIndex(i
, Kind
);
757 Attribute
Function::getAttributeAtIndex(unsigned i
, StringRef Kind
) const {
758 return AttributeSets
.getAttributeAtIndex(i
, Kind
);
761 bool Function::hasAttributeAtIndex(unsigned Idx
,
762 Attribute::AttrKind Kind
) const {
763 return AttributeSets
.hasAttributeAtIndex(Idx
, Kind
);
766 Attribute
Function::getFnAttribute(Attribute::AttrKind Kind
) const {
767 return AttributeSets
.getFnAttr(Kind
);
770 Attribute
Function::getFnAttribute(StringRef Kind
) const {
771 return AttributeSets
.getFnAttr(Kind
);
774 Attribute
Function::getRetAttribute(Attribute::AttrKind Kind
) const {
775 return AttributeSets
.getRetAttr(Kind
);
778 uint64_t Function::getFnAttributeAsParsedInteger(StringRef Name
,
779 uint64_t Default
) const {
780 Attribute A
= getFnAttribute(Name
);
781 uint64_t Result
= Default
;
782 if (A
.isStringAttribute()) {
783 StringRef Str
= A
.getValueAsString();
784 if (Str
.getAsInteger(0, Result
))
785 getContext().emitError("cannot parse integer attribute " + Name
);
791 /// gets the specified attribute from the list of attributes.
792 Attribute
Function::getParamAttribute(unsigned ArgNo
,
793 Attribute::AttrKind Kind
) const {
794 return AttributeSets
.getParamAttr(ArgNo
, Kind
);
797 void Function::addDereferenceableOrNullParamAttr(unsigned ArgNo
,
799 AttributeSets
= AttributeSets
.addDereferenceableOrNullParamAttr(getContext(),
803 void Function::addRangeRetAttr(const ConstantRange
&CR
) {
804 AttributeSets
= AttributeSets
.addRangeRetAttr(getContext(), CR
);
807 DenormalMode
Function::getDenormalMode(const fltSemantics
&FPType
) const {
808 if (&FPType
== &APFloat::IEEEsingle()) {
809 DenormalMode Mode
= getDenormalModeF32Raw();
810 // If the f32 variant of the attribute isn't specified, try to use the
816 return getDenormalModeRaw();
819 DenormalMode
Function::getDenormalModeRaw() const {
820 Attribute Attr
= getFnAttribute("denormal-fp-math");
821 StringRef Val
= Attr
.getValueAsString();
822 return parseDenormalFPAttribute(Val
);
825 DenormalMode
Function::getDenormalModeF32Raw() const {
826 Attribute Attr
= getFnAttribute("denormal-fp-math-f32");
827 if (Attr
.isValid()) {
828 StringRef Val
= Attr
.getValueAsString();
829 return parseDenormalFPAttribute(Val
);
832 return DenormalMode::getInvalid();
835 const std::string
&Function::getGC() const {
836 assert(hasGC() && "Function has no collector");
837 return getContext().getGC(*this);
840 void Function::setGC(std::string Str
) {
841 setValueSubclassDataBit(14, !Str
.empty());
842 getContext().setGC(*this, std::move(Str
));
845 void Function::clearGC() {
848 getContext().deleteGC(*this);
849 setValueSubclassDataBit(14, false);
852 bool Function::hasStackProtectorFnAttr() const {
853 return hasFnAttribute(Attribute::StackProtect
) ||
854 hasFnAttribute(Attribute::StackProtectStrong
) ||
855 hasFnAttribute(Attribute::StackProtectReq
);
858 /// Copy all additional attributes (those not needed to create a Function) from
859 /// the Function Src to this one.
860 void Function::copyAttributesFrom(const Function
*Src
) {
861 GlobalObject::copyAttributesFrom(Src
);
862 setCallingConv(Src
->getCallingConv());
863 setAttributes(Src
->getAttributes());
868 if (Src
->hasPersonalityFn())
869 setPersonalityFn(Src
->getPersonalityFn());
870 if (Src
->hasPrefixData())
871 setPrefixData(Src
->getPrefixData());
872 if (Src
->hasPrologueData())
873 setPrologueData(Src
->getPrologueData());
876 MemoryEffects
Function::getMemoryEffects() const {
877 return getAttributes().getMemoryEffects();
879 void Function::setMemoryEffects(MemoryEffects ME
) {
880 addFnAttr(Attribute::getWithMemoryEffects(getContext(), ME
));
883 /// Determine if the function does not access memory.
884 bool Function::doesNotAccessMemory() const {
885 return getMemoryEffects().doesNotAccessMemory();
887 void Function::setDoesNotAccessMemory() {
888 setMemoryEffects(MemoryEffects::none());
891 /// Determine if the function does not access or only reads memory.
892 bool Function::onlyReadsMemory() const {
893 return getMemoryEffects().onlyReadsMemory();
895 void Function::setOnlyReadsMemory() {
896 setMemoryEffects(getMemoryEffects() & MemoryEffects::readOnly());
899 /// Determine if the function does not access or only writes memory.
900 bool Function::onlyWritesMemory() const {
901 return getMemoryEffects().onlyWritesMemory();
903 void Function::setOnlyWritesMemory() {
904 setMemoryEffects(getMemoryEffects() & MemoryEffects::writeOnly());
907 /// Determine if the call can access memmory only using pointers based
908 /// on its arguments.
909 bool Function::onlyAccessesArgMemory() const {
910 return getMemoryEffects().onlyAccessesArgPointees();
912 void Function::setOnlyAccessesArgMemory() {
913 setMemoryEffects(getMemoryEffects() & MemoryEffects::argMemOnly());
916 /// Determine if the function may only access memory that is
917 /// inaccessible from the IR.
918 bool Function::onlyAccessesInaccessibleMemory() const {
919 return getMemoryEffects().onlyAccessesInaccessibleMem();
921 void Function::setOnlyAccessesInaccessibleMemory() {
922 setMemoryEffects(getMemoryEffects() & MemoryEffects::inaccessibleMemOnly());
925 /// Determine if the function may only access memory that is
926 /// either inaccessible from the IR or pointed to by its arguments.
927 bool Function::onlyAccessesInaccessibleMemOrArgMem() const {
928 return getMemoryEffects().onlyAccessesInaccessibleOrArgMem();
930 void Function::setOnlyAccessesInaccessibleMemOrArgMem() {
931 setMemoryEffects(getMemoryEffects() &
932 MemoryEffects::inaccessibleOrArgMemOnly());
935 bool Function::isTargetIntrinsic() const {
936 return Intrinsic::isTargetIntrinsic(IntID
);
939 void Function::updateAfterNameChange() {
940 LibFuncCache
= UnknownLibFunc
;
941 StringRef Name
= getName();
942 if (!Name
.starts_with("llvm.")) {
943 HasLLVMReservedName
= false;
944 IntID
= Intrinsic::not_intrinsic
;
947 HasLLVMReservedName
= true;
948 IntID
= Intrinsic::lookupIntrinsicID(Name
);
951 /// hasAddressTaken - returns true if there are any uses of this function
952 /// other than direct calls or invokes to it. Optionally ignores callback
953 /// uses, assume like pointer annotation calls, and references in llvm.used
954 /// and llvm.compiler.used variables.
955 bool Function::hasAddressTaken(const User
**PutOffender
,
956 bool IgnoreCallbackUses
,
957 bool IgnoreAssumeLikeCalls
, bool IgnoreLLVMUsed
,
958 bool IgnoreARCAttachedCall
,
959 bool IgnoreCastedDirectCall
) const {
960 for (const Use
&U
: uses()) {
961 const User
*FU
= U
.getUser();
962 if (isa
<BlockAddress
>(FU
))
965 if (IgnoreCallbackUses
) {
966 AbstractCallSite
ACS(&U
);
967 if (ACS
&& ACS
.isCallbackCall())
971 const auto *Call
= dyn_cast
<CallBase
>(FU
);
973 if (IgnoreAssumeLikeCalls
&&
974 isa
<BitCastOperator
, AddrSpaceCastOperator
>(FU
) &&
975 all_of(FU
->users(), [](const User
*U
) {
976 if (const auto *I
= dyn_cast
<IntrinsicInst
>(U
))
977 return I
->isAssumeLikeIntrinsic();
983 if (IgnoreLLVMUsed
&& !FU
->user_empty()) {
984 const User
*FUU
= FU
;
985 if (isa
<BitCastOperator
, AddrSpaceCastOperator
>(FU
) &&
986 FU
->hasOneUse() && !FU
->user_begin()->user_empty())
987 FUU
= *FU
->user_begin();
988 if (llvm::all_of(FUU
->users(), [](const User
*U
) {
989 if (const auto *GV
= dyn_cast
<GlobalVariable
>(U
))
990 return GV
->hasName() &&
991 (GV
->getName() == "llvm.compiler.used" ||
992 GV
->getName() == "llvm.used");
1002 if (IgnoreAssumeLikeCalls
) {
1003 if (const auto *I
= dyn_cast
<IntrinsicInst
>(Call
))
1004 if (I
->isAssumeLikeIntrinsic())
1008 if (!Call
->isCallee(&U
) || (!IgnoreCastedDirectCall
&&
1009 Call
->getFunctionType() != getFunctionType())) {
1010 if (IgnoreARCAttachedCall
&&
1011 Call
->isOperandBundleOfType(LLVMContext::OB_clang_arc_attachedcall
,
1023 bool Function::isDefTriviallyDead() const {
1024 // Check the linkage
1025 if (!hasLinkOnceLinkage() && !hasLocalLinkage() &&
1026 !hasAvailableExternallyLinkage())
1029 // Check if the function is used by anything other than a blockaddress.
1030 for (const User
*U
: users())
1031 if (!isa
<BlockAddress
>(U
))
1037 /// callsFunctionThatReturnsTwice - Return true if the function has a call to
1038 /// setjmp or other function that gcc recognizes as "returning twice".
1039 bool Function::callsFunctionThatReturnsTwice() const {
1040 for (const Instruction
&I
: instructions(this))
1041 if (const auto *Call
= dyn_cast
<CallBase
>(&I
))
1042 if (Call
->hasFnAttr(Attribute::ReturnsTwice
))
1048 Constant
*Function::getPersonalityFn() const {
1049 assert(hasPersonalityFn() && getNumOperands());
1050 return cast
<Constant
>(Op
<0>());
1053 void Function::setPersonalityFn(Constant
*Fn
) {
1054 setHungoffOperand
<0>(Fn
);
1055 setValueSubclassDataBit(3, Fn
!= nullptr);
1058 Constant
*Function::getPrefixData() const {
1059 assert(hasPrefixData() && getNumOperands());
1060 return cast
<Constant
>(Op
<1>());
1063 void Function::setPrefixData(Constant
*PrefixData
) {
1064 setHungoffOperand
<1>(PrefixData
);
1065 setValueSubclassDataBit(1, PrefixData
!= nullptr);
1068 Constant
*Function::getPrologueData() const {
1069 assert(hasPrologueData() && getNumOperands());
1070 return cast
<Constant
>(Op
<2>());
1073 void Function::setPrologueData(Constant
*PrologueData
) {
1074 setHungoffOperand
<2>(PrologueData
);
1075 setValueSubclassDataBit(2, PrologueData
!= nullptr);
1078 void Function::allocHungoffUselist() {
1079 // If we've already allocated a uselist, stop here.
1080 if (getNumOperands())
1083 allocHungoffUses(3, /*IsPhi=*/ false);
1084 setNumHungOffUseOperands(3);
1086 // Initialize the uselist with placeholder operands to allow traversal.
1087 auto *CPN
= ConstantPointerNull::get(PointerType::get(getContext(), 0));
1094 void Function::setHungoffOperand(Constant
*C
) {
1096 allocHungoffUselist();
1098 } else if (getNumOperands()) {
1099 Op
<Idx
>().set(ConstantPointerNull::get(PointerType::get(getContext(), 0)));
1103 void Function::setValueSubclassDataBit(unsigned Bit
, bool On
) {
1104 assert(Bit
< 16 && "SubclassData contains only 16 bits");
1106 setValueSubclassData(getSubclassDataFromValue() | (1 << Bit
));
1108 setValueSubclassData(getSubclassDataFromValue() & ~(1 << Bit
));
1111 void Function::setEntryCount(ProfileCount Count
,
1112 const DenseSet
<GlobalValue::GUID
> *S
) {
1113 #if !defined(NDEBUG)
1114 auto PrevCount
= getEntryCount();
1115 assert(!PrevCount
|| PrevCount
->getType() == Count
.getType());
1118 auto ImportGUIDs
= getImportGUIDs();
1119 if (S
== nullptr && ImportGUIDs
.size())
1122 MDBuilder
MDB(getContext());
1124 LLVMContext::MD_prof
,
1125 MDB
.createFunctionEntryCount(Count
.getCount(), Count
.isSynthetic(), S
));
1128 void Function::setEntryCount(uint64_t Count
, Function::ProfileCountType Type
,
1129 const DenseSet
<GlobalValue::GUID
> *Imports
) {
1130 setEntryCount(ProfileCount(Count
, Type
), Imports
);
1133 std::optional
<ProfileCount
> Function::getEntryCount(bool AllowSynthetic
) const {
1134 MDNode
*MD
= getMetadata(LLVMContext::MD_prof
);
1135 if (MD
&& MD
->getOperand(0))
1136 if (MDString
*MDS
= dyn_cast
<MDString
>(MD
->getOperand(0))) {
1137 if (MDS
->getString() == "function_entry_count") {
1138 ConstantInt
*CI
= mdconst::extract
<ConstantInt
>(MD
->getOperand(1));
1139 uint64_t Count
= CI
->getValue().getZExtValue();
1140 // A value of -1 is used for SamplePGO when there were no samples.
1141 // Treat this the same as unknown.
1142 if (Count
== (uint64_t)-1)
1143 return std::nullopt
;
1144 return ProfileCount(Count
, PCT_Real
);
1145 } else if (AllowSynthetic
&&
1146 MDS
->getString() == "synthetic_function_entry_count") {
1147 ConstantInt
*CI
= mdconst::extract
<ConstantInt
>(MD
->getOperand(1));
1148 uint64_t Count
= CI
->getValue().getZExtValue();
1149 return ProfileCount(Count
, PCT_Synthetic
);
1152 return std::nullopt
;
1155 DenseSet
<GlobalValue::GUID
> Function::getImportGUIDs() const {
1156 DenseSet
<GlobalValue::GUID
> R
;
1157 if (MDNode
*MD
= getMetadata(LLVMContext::MD_prof
))
1158 if (MDString
*MDS
= dyn_cast
<MDString
>(MD
->getOperand(0)))
1159 if (MDS
->getString() == "function_entry_count")
1160 for (unsigned i
= 2; i
< MD
->getNumOperands(); i
++)
1161 R
.insert(mdconst::extract
<ConstantInt
>(MD
->getOperand(i
))
1167 void Function::setSectionPrefix(StringRef Prefix
) {
1168 MDBuilder
MDB(getContext());
1169 setMetadata(LLVMContext::MD_section_prefix
,
1170 MDB
.createFunctionSectionPrefix(Prefix
));
1173 std::optional
<StringRef
> Function::getSectionPrefix() const {
1174 if (MDNode
*MD
= getMetadata(LLVMContext::MD_section_prefix
)) {
1175 assert(cast
<MDString
>(MD
->getOperand(0))->getString() ==
1176 "function_section_prefix" &&
1177 "Metadata not match");
1178 return cast
<MDString
>(MD
->getOperand(1))->getString();
1180 return std::nullopt
;
1183 bool Function::nullPointerIsDefined() const {
1184 return hasFnAttribute(Attribute::NullPointerIsValid
);
1187 bool llvm::NullPointerIsDefined(const Function
*F
, unsigned AS
) {
1188 if (F
&& F
->nullPointerIsDefined())