23#define DEBUG_TYPE "call-promotion-utils"
53 int Idx = Phi.getBasicBlockIndex(OrigBlock);
56 Phi.setIncomingBlock(
Idx, MergeBlock);
85 int Idx = Phi.getBasicBlockIndex(OrigBlock);
88 auto *V = Phi.getIncomingValue(
Idx);
89 Phi.setIncomingBlock(
Idx, ThenBlock);
90 Phi.addIncoming(V, ElseBlock);
116 for (
User *U : UsersToUpdate)
117 U->replaceUsesOfWith(OrigInst, Phi);
171 if (
auto *Invoke = dyn_cast<InvokeInst>(&CB))
173 &
SplitEdge(Invoke->getParent(), Invoke->getNormalDest())->
front();
183 for (
User *U : UsersToUpdate)
184 U->replaceUsesOfWith(&CB, Cast);
301 ThenBlock->
setName(
"if.true.direct_targ");
306 Value *NewRetVal = NewInst;
308 if (
auto *BitCast = dyn_cast_or_null<BitCastInst>(Next)) {
309 assert(BitCast->getOperand(0) == OrigInst &&
310 "bitcast following musttail call must use the call");
311 auto NewBitCast = BitCast->clone();
312 NewBitCast->replaceUsesOfWith(OrigInst, NewInst);
313 NewBitCast->insertBefore(ThenTerm);
314 NewRetVal = NewBitCast;
315 Next = BitCast->getNextNode();
319 ReturnInst *Ret = dyn_cast_or_null<ReturnInst>(Next);
320 assert(Ret &&
"musttail call must precede a ret with an optional bitcast");
321 auto NewRet = Ret->clone();
322 if (Ret->getReturnValue())
323 NewRet->replaceUsesOfWith(Ret->getReturnValue(), NewRetVal);
324 NewRet->insertBefore(ThenTerm);
343 ThenBlock->
setName(
"if.true.direct_targ");
344 ElseBlock->
setName(
"if.false.orig_indirect");
345 MergeBlock->
setName(
"if.end.icp");
354 if (
auto *OrigInvoke = dyn_cast<InvokeInst>(OrigInst)) {
355 auto *NewInvoke = cast<InvokeInst>(NewInst);
363 Builder.SetInsertPoint(MergeBlock);
364 Builder.CreateBr(OrigInvoke->getNormalDest());
372 OrigInvoke->setNormalDest(MergeBlock);
373 NewInvoke->setNormalDest(MergeBlock);
383 const char **FailureReason) {
386 auto &
DL =
Callee->getParent()->getDataLayout();
392 if (CallRetTy != FuncRetTy)
395 *FailureReason =
"Return type mismatch";
407 if (NumArgs != NumParams && !
Callee->isVarArg()) {
409 *FailureReason =
"The number of arguments mismatch";
417 for (;
I < NumParams; ++
I) {
420 if (
Callee->hasParamAttribute(
I, Attribute::ByVal) !=
423 *FailureReason =
"byval mismatch";
426 if (
Callee->hasParamAttribute(
I, Attribute::InAlloca) !=
429 *FailureReason =
"inalloca mismatch";
435 if (FormalTy == ActualTy)
439 *FailureReason =
"Argument type mismatch";
450 *FailureReason =
"Musttail call Argument type mismatch";
455 for (;
I < NumArgs;
I++) {
460 *FailureReason =
"SRet arg to vararg function";
504 bool AttributeChanged =
false;
506 for (
unsigned ArgNo = 0; ArgNo < CalleeParamNum; ++ArgNo) {
508 Type *FormalTy = CalleeType->getParamType(ArgNo);
509 Type *ActualTy =
Arg->getType();
510 if (FormalTy != ActualTy) {
525 AttributeChanged =
true;
534 if (!CallSiteRetTy->
isVoidTy() && CallSiteRetTy != CalleeRetTy) {
537 AttributeChanged =
true;
541 if (AttributeChanged)
568 if (!VTableEntryLoad)
571 APInt VTableOffset(
DL.getTypeSizeInBits(VTableEntryPtr->
getType()), 0);
573 DL, VTableOffset,
true);
574 LoadInst *VTablePtrLoad = dyn_cast<LoadInst>(VTableBasePtr);
578 APInt ObjectOffset(
DL.getTypeSizeInBits(Object->getType()), 0);
579 Value *ObjectBase = Object->stripAndAccumulateConstantOffsets(
580 DL, ObjectOffset,
true);
581 if (!(isa<AllocaInst>(ObjectBase) && ObjectOffset == 0))
588 VTablePtrLoad, VTablePtrLoad->
getParent(), BBI, 0,
nullptr,
nullptr);
591 APInt VTableOffsetGVBase(
DL.getTypeSizeInBits(VTablePtr->
getType()), 0);
593 DL, VTableOffsetGVBase,
true);
600 APInt VTableGVOffset = VTableOffsetGVBase + VTableOffset;
608 Function *DirectCallee = dyn_cast<Function>(
Ptr->stripPointerCasts());
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
amdgpu Simplify well known AMD library false FunctionCallee Callee
amdgpu Simplify well known AMD library false FunctionCallee Value * Arg
SmallVector< MachineOperand, 4 > Cond
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
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.
Type * getByValType() const
Retrieve the byval type.
AttrBuilder & addByValAttr(Type *Ty)
This turns a byval type into the form used internally in Attribute.
Type * getInAllocaType() const
Retrieve the inalloca type.
AttrBuilder & addInAllocaAttr(Type *Ty)
This turns an inalloca type into the form used internally in Attribute.
AttrBuilder & remove(const AttributeMask &AM)
Remove the attributes from the builder.
AttributeSet getFnAttrs() const
The function attributes are returned.
static AttributeList get(LLVMContext &C, ArrayRef< std::pair< unsigned, Attribute > > Attrs)
Create an AttributeList with the specified parameters in it.
AttributeSet getRetAttrs() const
The attributes for the ret value are returned.
bool hasParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) const
Return true if the attribute exists for the given argument.
AttributeSet getParamAttrs(unsigned ArgNo) const
The attributes for the argument or parameter at the given index are returned.
static AttributeSet get(LLVMContext &C, const AttrBuilder &B)
LLVM Basic Block Representation.
iterator_range< const_phi_iterator > phis() const
Returns a range that iterates over the phis in the basic block.
const Instruction & front() const
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...
bool paramHasAttr(unsigned ArgNo, Attribute::AttrKind Kind) const
Determine whether the argument or parameter has the given attribute.
bool isMustTailCall() const
Tests if this call site must be tail call optimized.
Value * getCalledOperand() const
void setAttributes(AttributeList A)
Set the parameter 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 parameter attributes for this call.
Function * getCaller()
Helper to get the caller (the parent function).
This is the base class for all instructions that perform data casts.
static CastInst * CreateBitOrPointerCast(Value *S, Type *Ty, const Twine &Name="", Instruction *InsertBefore=nullptr)
Create a BitCast, a PtrToInt, or an IntToPTr cast instruction.
static 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.
This is an important base class in LLVM.
A parsed version of the target data layout string in and methods for querying it.
FunctionType * getFunctionType()
unsigned getNumParams() const
Return the number of fixed parameters this function type requires.
Module * getParent()
Get the module that this global value is contained inside of...
const Constant * getInitializer() const
getInitializer - Return the initializer for this global variable.
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...
Instruction * clone() const
Create a copy of 'this' instruction that is identical in all ways except the following:
void insertBefore(Instruction *InsertPos)
Insert an unlinked instruction into a basic block immediately before the specified instruction.
const BasicBlock * getParent() const
void setMetadata(unsigned KindID, MDNode *Node)
Set the metadata of the specified kind to the specified node.
SymbolTableList< Instruction >::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
void moveBefore(Instruction *MovePos)
Unlink this instruction from its current basic block and insert it into the basic block that MovePos ...
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.
void addIncoming(Value *V, BasicBlock *BB)
Add an incoming value to the end of the PHI list.
Class to represent pointers.
unsigned getAddressSpace() const
Return the address space of the Pointer type.
Return a value (possibly void), from a function.
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.
Type * getFunctionParamType(unsigned i) const
bool isVoidTy() const
Return true if this is 'void'.
LLVM Value Representation.
Type * getType() const
All values are typed, get the type of this value.
const Value * stripAndAccumulateConstantOffsets(const DataLayout &DL, APInt &Offset, bool AllowNonInbounds, bool AllowInvariantGroup=false, function_ref< bool(Value &Value, APInt &Offset)> ExternalAnalysis=nullptr) const
Accumulate the constant offset this value has compared to a base pointer.
void setName(const Twine &Name)
Change the name of the value.
iterator_range< user_iterator > users()
self_iterator getIterator()
NodeTy * getNextNode()
Get the next node, or nullptr for the list tail.
AttributeMask typeIncompatible(Type *Ty, AttributeSafetyKind ASK=ASK_ALL)
Which attributes cannot be applied to a type.
This is an optimization pass for GlobalISel generic memory operations.
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.
CallBase & promoteCallWithIfThenElse(CallBase &CB, Function *Callee, MDNode *BranchWeights=nullptr)
Promote the given indirect call site to conditionally call Callee.
Instruction * SplitBlockAndInsertIfThen(Value *Cond, Instruction *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 ...
Value * FindAvailableLoadedValue(LoadInst *Load, BasicBlock *ScanBB, BasicBlock::iterator &ScanFrom, unsigned MaxInstsToScan=DefMaxInstsToScan, AAResults *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 ...
void SplitBlockAndInsertIfThenElse(Value *Cond, Instruction *SplitBefore, Instruction **ThenTerm, Instruction **ElseTerm, MDNode *BranchWeights=nullptr, DomTreeUpdater *DTU=nullptr)
SplitBlockAndInsertIfThenElse is similar to SplitBlockAndInsertIfThen, but also creates the ElseBlock...
CallBase & versionCallSite(CallBase &CB, Value *Callee, MDNode *BranchWeights)
Predicate and clone the given call site.
CallBase & promoteCall(CallBase &CB, Function *Callee, CastInst **RetBitCast=nullptr)
Promote the given indirect call site to unconditionally call Callee.
bool tryPromoteCall(CallBase &CB)
Try to promote (devirtualize) a virtual call on an Alloca.
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...
Constant * getPointerAtOffset(Constant *I, uint64_t Offset, Module &M, Constant *TopLevelGlobal=nullptr)
Processes a Constant recursively looking into elements of arrays, structs and expressions to find a t...