29#define DEBUG_TYPE "call-promotion-utils"
59 int Idx = Phi.getBasicBlockIndex(OrigBlock);
62 Phi.setIncomingBlock(Idx, MergeBlock);
91 int Idx = Phi.getBasicBlockIndex(OrigBlock);
94 auto *
V = Phi.getIncomingValue(Idx);
95 Phi.setIncomingBlock(Idx, ThenBlock);
96 Phi.addIncoming(
V, ElseBlock);
119 Builder.SetInsertPoint(MergeBlock->
begin());
122 for (
User *U : UsersToUpdate)
123 U->replaceUsesOfWith(OrigInst, Phi);
124 Phi->addIncoming(OrigInst, OrigInst->
getParent());
125 Phi->addIncoming(NewInst, NewInst->
getParent());
189 for (
User *U : UsersToUpdate)
300 ThenBlock->
setName(
"if.true.direct_targ");
305 Value *NewRetVal = NewInst;
308 assert(BitCast->getOperand(0) == OrigInst &&
309 "bitcast following musttail call must use the call");
310 auto NewBitCast = BitCast->clone();
311 NewBitCast->replaceUsesOfWith(OrigInst, NewInst);
313 NewRetVal = NewBitCast;
314 Next = BitCast->getNextNode();
319 assert(Ret &&
"musttail call must precede a ret with an optional bitcast");
320 auto NewRet = Ret->
clone();
342 ThenBlock->
setName(
"if.true.direct_targ");
343 ElseBlock->
setName(
"if.false.orig_indirect");
344 MergeBlock->
setName(
"if.end.icp");
362 Builder.SetInsertPoint(MergeBlock);
363 Builder.CreateBr(OrigInvoke->getNormalDest());
371 OrigInvoke->setNormalDest(MergeBlock);
372 NewInvoke->setNormalDest(MergeBlock);
401 const char **FailureReason) {
404 auto &
DL = Callee->getDataLayout();
409 Type *FuncRetTy = Callee->getReturnType();
410 if (CallRetTy != FuncRetTy)
413 *FailureReason =
"Return type mismatch";
420 for (
unsigned I = 0, E = CB.
arg_size();
I != E; ++
I) {
424 *FailureReason =
"captures(ret: ...) with non-pointer return type";
431 unsigned NumParams = Callee->getFunctionType()->getNumParams();
438 if (NumArgs != NumParams && !Callee->isVarArg()) {
440 *FailureReason =
"The number of arguments mismatch";
448 for (;
I < NumParams; ++
I) {
451 if (Callee->hasParamAttribute(
I, Attribute::ByVal) !=
454 *FailureReason =
"byval mismatch";
457 if (Callee->hasParamAttribute(
I, Attribute::InAlloca) !=
460 *FailureReason =
"inalloca mismatch";
464 Type *FormalTy = Callee->getFunctionType()->getFunctionParamType(
I);
466 if (FormalTy == ActualTy)
470 *FailureReason =
"Argument type mismatch";
481 *FailureReason =
"Musttail call Argument type mismatch";
486 for (;
I < NumArgs;
I++) {
488 assert(Callee->isVarArg());
491 *FailureReason =
"SRet arg to vararg function";
520 Type *CalleeRetTy = Callee->getReturnType();
528 auto CalleeType = Callee->getFunctionType();
529 auto CalleeParamNum = CalleeType->getNumParams();
535 bool AttributeChanged =
false;
537 for (
unsigned ArgNo = 0; ArgNo < CalleeParamNum; ++ArgNo) {
539 Type *FormalTy = CalleeType->getParamType(ArgNo);
540 Type *ActualTy = Arg->getType();
541 if (FormalTy != ActualTy) {
547 AttrBuilder ArgAttrs(Ctx, CallerPAL.getParamAttrs(ArgNo));
548 ArgAttrs.remove(AttributeFuncs::typeIncompatible(
549 FormalTy, CallerPAL.getParamAttrs(ArgNo)));
552 if (ArgAttrs.getByValType())
553 ArgAttrs.addByValAttr(Callee->getParamByValType(ArgNo));
554 if (ArgAttrs.getInAllocaType())
555 ArgAttrs.addInAllocaAttr(Callee->getParamInAllocaType(ArgNo));
558 AttributeChanged =
true;
560 NewArgAttrs.
push_back(CallerPAL.getParamAttrs(ArgNo));
566 AttrBuilder RAttrs(Ctx, CallerPAL.getRetAttrs());
567 if (!CallSiteRetTy->
isVoidTy() && CallSiteRetTy != CalleeRetTy) {
570 AttributeFuncs::typeIncompatible(CalleeRetTy, CallerPAL.getRetAttrs()));
571 AttributeChanged =
true;
575 if (AttributeChanged)
576 CB.
setAttributes(AttributeList::get(Ctx, CallerPAL.getFnAttrs(),
604 const uint64_t CSIndex = CSInstr->getIndex()->getZExtValue();
611 NewCSInstr->setIndex(NewCSID);
612 NewCSInstr->setCallee(&Callee);
613 NewCSInstr->insertBefore(
DirectCall.getIterator());
618 "The ICP direct BB is new, it shouldn't have instrumentation");
620 "The ICP indirect BB is new, it shouldn't have instrumentation");
628 DirectBBIns->setIndex(DirectID);
629 DirectBBIns->insertInto(&DirectBB, DirectBB.getFirstInsertionPt());
632 IndirectBBIns->setIndex(IndirectID);
633 IndirectBBIns->insertInto(&IndirectBB, IndirectBB.getFirstInsertionPt());
636 const uint32_t NewCountersSize = IndirectID + 1;
639 assert(Ctx.guid() == Caller.getGUID());
640 assert(NewCountersSize - 2 == Ctx.counters().size());
642 Ctx.resizeCounters(NewCountersSize);
647 if (!Ctx.hasCallsite(CSIndex))
649 auto &CSData = Ctx.callsite(CSIndex);
651 uint64_t TotalCount = 0;
652 for (
const auto &[
_, V] : CSData)
653 TotalCount += V.getEntrycount();
654 uint64_t DirectCount = 0;
658 if (
auto It = CSData.find(CalleeGUID); It != CSData.end()) {
659 assert(CalleeGUID == It->second.guid());
660 DirectCount = It->second.getEntrycount();
663 assert(Ctx.callsites().count(NewCSID) == 0);
664 Ctx.ingestContext(NewCSID, std::move(It->second));
665 CSData.erase(CalleeGUID);
668 assert(TotalCount >= DirectCount);
669 uint64_t IndirectCount = TotalCount - DirectCount;
672 Ctx.counters()[DirectID] = DirectCount;
673 Ctx.counters()[IndirectID] = IndirectCount;
675 CtxProf.
update(ProfileUpdater, Caller);
683 assert(!AddressPoints.
empty() &&
"Caller should guarantee");
686 for (
auto &AddressPoint : AddressPoints) {
687 ICmps.
push_back(Builder.CreateICmpEQ(VPtr, AddressPoint));
714 if (!VTableEntryLoad)
717 APInt VTableOffset(
DL.getIndexTypeSizeInBits(VTableEntryPtr->
getType()), 0);
719 DL, VTableOffset,
true);
724 APInt ObjectOffset(
DL.getIndexTypeSizeInBits(Object->getType()), 0);
725 Value *ObjectBase = Object->stripAndAccumulateConstantOffsets(
726 DL, ObjectOffset,
true);
734 VTablePtrLoad, VTablePtrLoad->
getParent(), BBI, 0,
nullptr,
nullptr);
737 APInt VTableOffsetGVBase(
DL.getIndexTypeSizeInBits(VTablePtr->
getType()), 0);
739 DL, VTableOffsetGVBase,
true);
745 APInt VTableGVOffset = VTableOffsetGVBase + VTableOffset;
750 std::tie(DirectCallee, std::ignore) =
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Module.h This file contains the declarations for the Module class.
SmallVector< MachineOperand, 4 > Cond
Reader for contextual iFDO profile, which comes in bitstream format.
Class for arbitrary precision integers.
uint64_t getZExtValue() const
Get zero extended value.
unsigned getActiveBits() const
Compute the number of active bits in the value.
Represent a constant reference to an array (0 or more elements consecutively in memory),...
bool empty() const
Check if the array is empty.
LLVM_ABI CaptureInfo getCaptureInfo() const
static LLVM_ABI AttributeSet get(LLVMContext &C, const AttrBuilder &B)
LLVM Basic Block Representation.
iterator begin()
Instruction iterator methods.
iterator_range< const_phi_iterator > phis() const
Returns a range that iterates over the phis in the basic block.
InstListType::iterator iterator
Instruction iterators...
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Function * getCalledFunction() const
Returns the function called, or null if this is an indirect function invocation or the function signa...
AttributeSet getParamAttributes(unsigned ArgNo) const
Return the param attributes for this call.
LLVM_ABI bool paramHasAttr(unsigned ArgNo, Attribute::AttrKind Kind) const
Determine whether the argument or parameter has the given attribute.
LLVM_ABI bool isMustTailCall() const
Tests if this call site must be tail call optimized.
LLVM_ABI bool isIndirectCall() const
Return true if the callsite is an indirect call.
Value * getCalledOperand() const
void setAttributes(AttributeList A)
Set the attributes for this call.
Value * getArgOperand(unsigned i) const
void mutateFunctionType(FunctionType *FTy)
void setArgOperand(unsigned i, Value *v)
FunctionType * getFunctionType() const
void setCalledOperand(Value *V)
unsigned arg_size() const
AttributeList getAttributes() const
Return the attributes for this call.
LLVM_ABI Function * getCaller()
Helper to get the caller (the parent function).
Represents which components of the pointer may be captured in which location.
CaptureComponents getOtherComponents() const
Get components potentially captured through locations other than the return value.
CaptureComponents getRetComponents() const
Get components potentially captured by the return value.
This is the base class for all instructions that perform data casts.
static LLVM_ABI bool isBitOrNoopPointerCastable(Type *SrcTy, Type *DestTy, const DataLayout &DL)
Check whether a bitcast, inttoptr, or ptrtoint cast between these types is valid and a no-op.
static LLVM_ABI CastInst * CreateBitOrPointerCast(Value *S, Type *Ty, const Twine &Name="", InsertPosition InsertBefore=nullptr)
Create a BitCast, a PtrToInt, or an IntToPTr cast instruction.
static LLVM_ABI InstrProfIncrementInst * getBBInstrumentation(BasicBlock &BB)
Get the instruction instrumenting a BB, or nullptr if not present.
static LLVM_ABI InstrProfCallsite * getCallsiteInstrumentation(CallBase &CB)
Get the instruction instrumenting a callsite, or nullptr if that cannot be found.
A parsed version of the target data layout string in and methods for querying it.
uint64_t GUID
Declare a type to represent a global unique identifier for a global value.
Module * getParent()
Get the module that this global value is contained inside of...
bool isConstant() const
If the value is a global constant, its value is immutable throughout the runtime execution of the pro...
bool hasDefinitiveInitializer() const
hasDefinitiveInitializer - Whether the global variable has an initializer, and any other instances of...
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
LLVM_ABI Instruction * clone() const
Create a copy of 'this' instruction that is identical in all ways except the following:
LLVM_ABI void moveBefore(InstListType::iterator InsertPos)
Unlink this instruction from its current basic block and insert it into the basic block that MovePos ...
LLVM_ABI void insertBefore(InstListType::iterator InsertPos)
Insert an unlinked instruction into a basic block immediately before the specified position.
LLVM_ABI InstListType::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
LLVM_ABI const Function * getFunction() const
Return the function this instruction belongs to.
iterator_range< user_iterator > users()
LLVM_ABI void setMetadata(unsigned KindID, MDNode *Node)
Set the metadata of the specified kind to the specified node.
BasicBlock * getUnwindDest() const
BasicBlock * getNormalDest() const
This is an important class for using LLVM in a threaded context.
An instruction for reading from memory.
Value * getPointerOperand()
A Module instance is used to store all the information related to an LLVM module.
The instrumented contextual profile, produced by the CtxProfAnalysis.
LLVM_ABI void update(Visitor, const Function &F)
uint32_t allocateNextCounterIndex(const Function &F)
uint32_t allocateNextCallsiteIndex(const Function &F)
bool isFunctionKnown(const Function &F) const
A node (context) in the loaded contextual profile, suitable for mutation during IPO passes.
Class to represent pointers.
unsigned getAddressSpace() const
Return the address space of the Pointer type.
Return a value (possibly void), from a function.
Value * getReturnValue() const
Convenience accessor. Returns null if there is no return value.
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
The instances of the Type class are immutable: once they are created, they are never changed.
bool isPointerTy() const
True if this is an instance of PointerType.
bool isVoidTy() const
Return true if this is 'void'.
LLVM_ABI bool replaceUsesOfWith(Value *From, Value *To)
Replace uses of one Value with another.
LLVM Value Representation.
Type * getType() const
All values are typed, get the type of this value.
LLVM_ABI void setName(const Twine &Name)
Change the name of the value.
LLVM_ABI const Value * stripAndAccumulateConstantOffsets(const DataLayout &DL, APInt &Offset, bool AllowNonInbounds, bool AllowInvariantGroup=false, function_ref< bool(Value &Value, APInt &Offset)> ExternalAnalysis=nullptr, bool LookThroughIntToPtr=false) const
Accumulate the constant offset this value has compared to a base pointer.
const ParentTy * getParent() const
self_iterator getIterator()
NodeTy * getNextNode()
Get the next node, or nullptr for the list tail.
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI bool isLegalToPromote(const CallBase &CB, Function *Callee, const char **FailureReason=nullptr)
Return true if the given indirect call site can be made to call Callee.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
LLVM_ABI CallBase & promoteCallWithIfThenElse(CallBase &CB, Function *Callee, MDNode *BranchWeights=nullptr)
Promote the given indirect call site to conditionally call Callee.
LLVM_ABI Value * FindAvailableLoadedValue(LoadInst *Load, BasicBlock *ScanBB, BasicBlock::iterator &ScanFrom, unsigned MaxInstsToScan=DefMaxInstsToScan, BatchAAResults *AA=nullptr, bool *IsLoadCSE=nullptr, unsigned *NumScanedInst=nullptr)
Scan backwards to see if we have the value of the given load available locally within a small number ...
auto dyn_cast_or_null(const Y &Val)
LLVM_ABI void SplitBlockAndInsertIfThenElse(Value *Cond, BasicBlock::iterator SplitBefore, Instruction **ThenTerm, Instruction **ElseTerm, MDNode *BranchWeights=nullptr, DomTreeUpdater *DTU=nullptr, LoopInfo *LI=nullptr)
SplitBlockAndInsertIfThenElse is similar to SplitBlockAndInsertIfThen, but also creates the ElseBlock...
LLVM_ABI CallBase & versionCallSite(CallBase &CB, Value *Callee, MDNode *BranchWeights)
Predicate and clone the given call site.
LLVM_ABI CallBase & promoteCall(CallBase &CB, Function *Callee, CastInst **RetBitCast=nullptr)
Promote the given indirect call site to unconditionally call Callee.
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
LLVM_ABI bool tryPromoteCall(CallBase &CB)
Try to promote (devirtualize) a virtual call on an Alloca.
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Next
bool capturesAnything(CaptureComponents CC)
LLVM_ABI Instruction * SplitBlockAndInsertIfThen(Value *Cond, BasicBlock::iterator SplitBefore, bool Unreachable, MDNode *BranchWeights=nullptr, DomTreeUpdater *DTU=nullptr, LoopInfo *LI=nullptr, BasicBlock *ThenBlock=nullptr)
Split the containing block at the specified instruction - everything before SplitBefore stays in the ...
LLVM_ABI BasicBlock * SplitEdge(BasicBlock *From, BasicBlock *To, DominatorTree *DT=nullptr, LoopInfo *LI=nullptr, MemorySSAUpdater *MSSAU=nullptr, const Twine &BBName="")
Split the edge connecting the specified blocks, and return the newly created basic block between From...
LLVM_ABI CallBase & promoteCallWithVTableCmp(CallBase &CB, Instruction *VPtr, Function *Callee, ArrayRef< Constant * > AddressPoints, MDNode *BranchWeights)
This is similar to promoteCallWithIfThenElse except that the condition to promote a virtual call is t...
LLVM_ABI std::pair< Function *, Constant * > getFunctionAtVTableOffset(GlobalVariable *GV, uint64_t Offset, Module &M)
Given a vtable and a specified offset, returns the function and the trivial pointer at the specified ...