Go to the documentation of this file.
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)
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();
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 (FormalTy == ActualTy)
424 *FailureReason =
"Argument type mismatch";
430 if (
Callee->hasParamAttribute(
I, Attribute::ByVal) !=
433 *FailureReason =
"byval mismatch";
436 if (
Callee->hasParamAttribute(
I, Attribute::InAlloca) !=
439 *FailureReason =
"inalloca mismatch";
443 for (;
I < NumArgs;
I++) {
448 *FailureReason =
"SRet arg to vararg function";
492 bool AttributeChanged =
false;
494 for (
unsigned ArgNo = 0; ArgNo < CalleeParamNum; ++ArgNo) {
496 Type *FormalTy = CalleeType->getParamType(ArgNo);
497 Type *ActualTy =
Arg->getType();
498 if (FormalTy != ActualTy) {
513 AttributeChanged =
true;
522 if (!CallSiteRetTy->
isVoidTy() && CallSiteRetTy != CalleeRetTy) {
525 AttributeChanged =
true;
529 if (AttributeChanged)
556 if (!VTableEntryLoad)
559 APInt VTableOffset(
DL.getTypeSizeInBits(VTableEntryPtr->
getType()), 0);
561 DL, VTableOffset,
true);
562 LoadInst *VTablePtrLoad = dyn_cast<LoadInst>(VTableBasePtr);
566 APInt ObjectOffset(
DL.getTypeSizeInBits(
Object->getType()), 0);
567 Value *ObjectBase =
Object->stripAndAccumulateConstantOffsets(
568 DL, ObjectOffset,
true);
569 if (!(isa<AllocaInst>(ObjectBase) && ObjectOffset == 0))
576 VTablePtrLoad, VTablePtrLoad->
getParent(), BBI, 0,
nullptr,
nullptr);
579 APInt VTableOffsetGVBase(
DL.getTypeSizeInBits(VTablePtr->
getType()), 0);
581 DL, VTableOffsetGVBase,
true);
588 APInt VTableGVOffset = VTableOffsetGVBase + VTableOffset;
BasicBlock * getNormalDest() const
Type * getByValType() const
Retrieve the byval type.
This is an optimization pass for GlobalISel generic memory operations.
We currently emits eax Perhaps this is what we really should generate is Is imull three or four cycles eax eax The current instruction priority is based on pattern complexity The former is more complex because it folds a load so the latter will not be emitted Perhaps we should use AddedComplexity to give LEA32r a higher priority We should always try to match LEA first since the LEA matching code does some estimate to determine whether the match is profitable if we care more about code then imull is better It s two bytes shorter than movl leal On a Pentium M
Return a value (possibly void), from a function.
A parsed version of the target data layout string in and methods for querying it.
InstListType::iterator iterator
Instruction iterators...
void mutateFunctionType(FunctionType *FTy)
NodeTy * getNextNode()
Get the next node, or nullptr for the list tail.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
static AttributeList get(LLVMContext &C, ArrayRef< std::pair< unsigned, Attribute >> Attrs)
Create an AttributeList with the specified parameters in it.
void insertBefore(Instruction *InsertPos)
Insert an unlinked instruction into a basic block immediately before the specified instruction.
The instances of the Type class are immutable: once they are created, they are never changed.
AttributeList getAttributes() const
Return the parameter attributes for this call.
FunctionType * getFunctionType() const
AttributeSet getFnAttrs() const
The function attributes are returned.
bool isMustTailCall() const
Tests if this call site must be tail call optimized.
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.
unsigned getNumParams() const
Return the number of fixed parameters this function type requires.
Value * getPointerOperand()
void setMetadata(unsigned KindID, MDNode *Node)
Set the metadata of the specified kind to the specified node.
LLVM Basic Block Representation.
amdgpu Simplify well known AMD library false FunctionCallee Value * Arg
AttrBuilder & remove(const AttributeMask &AM)
Remove the attributes from the builder.
AttrBuilder & addByValAttr(Type *Ty)
This turns a byval type into the form used internally in Attribute.
Function * getCalledFunction() const
Returns the function called, or null if this is an indirect function invocation or the function signa...
void setAttributes(AttributeList A)
Set the parameter attributes for this call.
CallBase & promoteCallWithIfThenElse(CallBase &CB, Function *Callee, MDNode *BranchWeights=nullptr)
Promote the given indirect call site to conditionally call Callee.
uint64_t getZExtValue() const
Get zero extended value.
iterator_range< const_phi_iterator > phis() const
Returns a range that iterates over the phis in the basic block.
void setName(const Twine &Name)
Change the name of the value.
FunctionType * getFunctionType()
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 & versionCallSite(CallBase &CB, Value *Callee, MDNode *BranchWeights)
Predicate and clone the given call site.
Function * getCaller()
Helper to get the caller (the parent function).
AttributeSet getRetAttrs() const
The attributes for the ret value are returned.
const Constant * getInitializer() const
getInitializer - Return the initializer for this global variable.
This is an important base class in LLVM.
SymbolTableList< Instruction >::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
Module * getParent()
Get the module that this global value is contained inside of...
bool hasDefinitiveInitializer() const
hasDefinitiveInitializer - Whether the global variable has an initializer, and any other instances of...
static AttributeSet get(LLVMContext &C, const AttrBuilder &B)
AttrBuilder & addInAllocaAttr(Type *Ty)
This turns an inalloca type into the form used internally in Attribute.
void addIncoming(Value *V, BasicBlock *BB)
Add an incoming value to the end of the PHI list.
This is an important class for using LLVM in a threaded context.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
bool isVoidTy() const
Return true if this is 'void'.
A Module instance is used to store all the information related to an LLVM module.
void SplitBlockAndInsertIfThenElse(Value *Cond, Instruction *SplitBefore, Instruction **ThenTerm, Instruction **ElseTerm, MDNode *BranchWeights=nullptr)
SplitBlockAndInsertIfThenElse is similar to SplitBlockAndInsertIfThen, but also creates the ElseBlock...
Class for arbitrary precision integers.
Instruction * clone() const
Create a copy of 'this' instruction that is identical in all ways except the following:
SmallVector< MachineOperand, 4 > Cond
Type * getType() const
All values are typed, get the type of this value.
self_iterator getIterator()
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
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 the base class for all instructions that perform data casts.
An instruction for reading from memory.
bool paramHasAttr(unsigned ArgNo, Attribute::AttrKind Kind) const
Determine whether the argument or parameter has the given attribute.
void setArgOperand(unsigned i, Value *v)
const Constant * stripPointerCasts() const
bool replaceUsesOfWith(Value *From, Value *To)
Replace uses of one Value with another.
const Instruction & front() const
amdgpu Simplify well known AMD library false FunctionCallee Callee
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...
static CastInst * CreateBitOrPointerCast(Value *S, Type *Ty, const Twine &Name="", Instruction *InsertBefore=nullptr)
Create a BitCast, a PtrToInt, or an IntToPTr cast instruction.
unsigned arg_size() const
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 ...
AttributeMask typeIncompatible(Type *Ty, AttributeSafetyKind ASK=ASK_ALL)
Which attributes cannot be applied to a type.
Value * getCalledOperand() const
void setCalledOperand(Value *V)
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...
bool isConstant() const
If the value is a global constant, its value is immutable throughout the runtime execution of the pro...
Value * getArgOperand(unsigned i) const
const BasicBlock * getParent() const
unsigned getActiveBits() const
Compute the number of active bits in the value.
CallBase & promoteCall(CallBase &CB, Function *Callee, CastInst **RetBitCast=nullptr)
Promote the given indirect call site to unconditionally call Callee.
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Instruction * SplitBlockAndInsertIfThen(Value *Cond, Instruction *SplitBefore, bool Unreachable, MDNode *BranchWeights, DominatorTree *DT, LoopInfo *LI=nullptr, BasicBlock *ThenBlock=nullptr)
Split the containing block at the specified instruction - everything before SplitBefore stays in the ...
Type * getFunctionParamType(unsigned i) const
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.
Type * getInAllocaType() const
Retrieve the inalloca type.
BasicBlock * getUnwindDest() const
LLVM Value Representation.
iterator_range< user_iterator > users()
bool tryPromoteCall(CallBase &CB)
Try to promote (devirtualize) a virtual call on an Alloca.
void moveBefore(Instruction *MovePos)
Unlink this instruction from its current basic block and insert it into the basic block that MovePos ...