LLVM 22.0.0git
Public Member Functions | List of all members
llvm::JumpThreadingPass Class Reference

This pass performs 'jump threading', which looks at blocks that have multiple predecessors and multiple successors. More...

#include "llvm/Transforms/Scalar/JumpThreading.h"

Inheritance diagram for llvm::JumpThreadingPass:
[legend]

Public Member Functions

LLVM_ABI JumpThreadingPass (int T=-1)
 
LLVM_ABI bool runImpl (Function &F, FunctionAnalysisManager *FAM, TargetLibraryInfo *TLI, TargetTransformInfo *TTI, LazyValueInfo *LVI, AAResults *AA, std::unique_ptr< DomTreeUpdater > DTU, BlockFrequencyInfo *BFI, BranchProbabilityInfo *BPI)
 
LLVM_ABI PreservedAnalyses run (Function &F, FunctionAnalysisManager &AM)
 
DomTreeUpdatergetDomTreeUpdater () const
 
LLVM_ABI void findLoopHeaders (Function &F)
 findLoopHeaders - We do not want jump threading to turn proper loop structures into irreducible loops.
 
LLVM_ABI bool processBlock (BasicBlock *BB)
 processBlock - If there are any predecessors whose control can be threaded through to a successor, transform them now.
 
LLVM_ABI bool maybeMergeBasicBlockIntoOnlyPred (BasicBlock *BB)
 Merge basic block BB into its sole predecessor if possible.
 
LLVM_ABI void updateSSA (BasicBlock *BB, BasicBlock *NewBB, ValueToValueMapTy &ValueMapping)
 Update the SSA form.
 
LLVM_ABI void cloneInstructions (ValueToValueMapTy &ValueMapping, BasicBlock::iterator BI, BasicBlock::iterator BE, BasicBlock *NewBB, BasicBlock *PredBB)
 Clone instructions in range [BI, BE) to NewBB.
 
LLVM_ABI bool tryThreadEdge (BasicBlock *BB, const SmallVectorImpl< BasicBlock * > &PredBBs, BasicBlock *SuccBB)
 tryThreadEdge - Thread an edge if it's safe and profitable to do so.
 
LLVM_ABI void threadEdge (BasicBlock *BB, const SmallVectorImpl< BasicBlock * > &PredBBs, BasicBlock *SuccBB)
 threadEdge - We have decided that it is safe and profitable to factor the blocks in PredBBs to one predecessor, then thread an edge from it to SuccBB across BB.
 
LLVM_ABI bool duplicateCondBranchOnPHIIntoPred (BasicBlock *BB, const SmallVectorImpl< BasicBlock * > &PredBBs)
 duplicateCondBranchOnPHIIntoPred - PredBB contains an unconditional branch to BB which contains an i1 PHI node and a conditional branch on that PHI.
 
LLVM_ABI bool computeValueKnownInPredecessorsImpl (Value *V, BasicBlock *BB, jumpthreading::PredValueInfo &Result, jumpthreading::ConstantPreference Preference, SmallPtrSet< Value *, 4 > &RecursionSet, Instruction *CxtI=nullptr)
 computeValueKnownInPredecessors - Given a basic block BB and a value V, see if we can infer that the value is a known ConstantInt/BlockAddress or undef in any of our predecessors.
 
bool computeValueKnownInPredecessors (Value *V, BasicBlock *BB, jumpthreading::PredValueInfo &Result, jumpthreading::ConstantPreference Preference, Instruction *CxtI=nullptr)
 
LLVM_ABI ConstantevaluateOnPredecessorEdge (BasicBlock *BB, BasicBlock *PredPredBB, Value *cond, const DataLayout &DL)
 
LLVM_ABI bool maybethreadThroughTwoBasicBlocks (BasicBlock *BB, Value *Cond)
 Attempt to thread through two successive basic blocks.
 
LLVM_ABI void threadThroughTwoBasicBlocks (BasicBlock *PredPredBB, BasicBlock *PredBB, BasicBlock *BB, BasicBlock *SuccBB)
 
LLVM_ABI bool processThreadableEdges (Value *Cond, BasicBlock *BB, jumpthreading::ConstantPreference Preference, Instruction *CxtI=nullptr)
 
LLVM_ABI bool processBranchOnPHI (PHINode *PN)
 processBranchOnPHI - We have an otherwise unthreadable conditional branch on a PHI node (or freeze PHI) in the current block.
 
LLVM_ABI bool processBranchOnXOR (BinaryOperator *BO)
 processBranchOnXOR - We have an otherwise unthreadable conditional branch on a xor instruction in the current block.
 
LLVM_ABI bool processImpliedCondition (BasicBlock *BB)
 
LLVM_ABI bool simplifyPartiallyRedundantLoad (LoadInst *LI)
 simplifyPartiallyRedundantLoad - If LoadI is an obviously partially redundant load instruction, eliminate it by replacing it with a PHI node.
 
LLVM_ABI void unfoldSelectInstr (BasicBlock *Pred, BasicBlock *BB, SelectInst *SI, PHINode *SIUse, unsigned Idx)
 
LLVM_ABI bool tryToUnfoldSelect (CmpInst *CondCmp, BasicBlock *BB)
 tryToUnfoldSelect - Look for blocks of the form bb1: a = select br bb2
 
LLVM_ABI bool tryToUnfoldSelect (SwitchInst *SI, BasicBlock *BB)
 
LLVM_ABI bool tryToUnfoldSelectInCurrBB (BasicBlock *BB)
 tryToUnfoldSelectInCurrBB - Look for PHI/Select or PHI/CMP/Select in the same BB in the form bb: p = phi [false, bb1], [true, bb2], [false, bb3], [true, bb4], ... s = select p, trueval, falseval
 
LLVM_ABI bool processGuards (BasicBlock *BB)
 Try to propagate a guard from the current BB into one of its predecessors in case if another branch of execution implies that the condition of this guard is always true.
 
LLVM_ABI bool threadGuard (BasicBlock *BB, IntrinsicInst *Guard, BranchInst *BI)
 Try to propagate the guard from BB which is the lower block of a diamond to one of its branches, in case if diamond's condition implies guard's condition.
 
- Public Member Functions inherited from llvm::PassInfoMixin< JumpThreadingPass >
void printPipeline (raw_ostream &OS, function_ref< StringRef(StringRef)> MapClassName2PassName)
 

Additional Inherited Members

- Static Public Member Functions inherited from llvm::PassInfoMixin< JumpThreadingPass >
static StringRef name ()
 Gets the name of the pass we are mixed into.
 

Detailed Description

This pass performs 'jump threading', which looks at blocks that have multiple predecessors and multiple successors.

If one or more of the predecessors of the block can be proven to always jump to one of the successors, we forward the edge from the predecessor to the successor by duplicating the contents of this block.

An example of when this can occur is code like this:

if () { ... X = 4; } if (X < 3) {

In this case, the unconditional branch at the end of the first if can be revectored to the false side of the second if.

Definition at line 80 of file JumpThreading.h.

Constructor & Destructor Documentation

◆ JumpThreadingPass()

JumpThreadingPass::JumpThreadingPass ( int  T = -1)

Definition at line 109 of file JumpThreading.cpp.

References BBDuplicateThreshold.

Member Function Documentation

◆ cloneInstructions()

void JumpThreadingPass::cloneInstructions ( ValueToValueMapTy ValueMapping,
BasicBlock::iterator  BI,
BasicBlock::iterator  BE,
BasicBlock NewBB,
BasicBlock PredBB 
)

◆ computeValueKnownInPredecessors()

bool llvm::JumpThreadingPass::computeValueKnownInPredecessors ( Value V,
BasicBlock BB,
jumpthreading::PredValueInfo Result,
jumpthreading::ConstantPreference  Preference,
Instruction CxtI = nullptr 
)
inline

Definition at line 141 of file JumpThreading.h.

References computeValueKnownInPredecessorsImpl().

Referenced by processBranchOnXOR(), and processThreadableEdges().

◆ computeValueKnownInPredecessorsImpl()

bool JumpThreadingPass::computeValueKnownInPredecessorsImpl ( Value V,
BasicBlock BB,
jumpthreading::PredValueInfo Result,
jumpthreading::ConstantPreference  Preference,
SmallPtrSet< Value *, 4 > &  RecursionSet,
Instruction CxtI = nullptr 
)

computeValueKnownInPredecessors - Given a basic block BB and a value V, see if we can infer that the value is a known ConstantInt/BlockAddress or undef in any of our predecessors.

If so, return the known list of value and pred BB in the result vector.

This returns true if there were any known values.

If I is a PHI node, then we know the incoming values for any constants.

Definition at line 556 of file JumpThreading.cpp.

References llvm::ConstantRange::add(), assert(), llvm::CallingConv::C, computeValueKnownInPredecessorsImpl(), Cond, llvm::ConstantFoldBinaryOpOperands(), llvm::ConstantFoldCastOperand(), llvm::ConstantFoldCompareInstOperands(), llvm::ConstantRange::contains(), llvm::SmallPtrSetImpl< PtrType >::count(), DL, llvm::Value::DoPHITranslation(), llvm::SmallVectorBase< Size_T >::empty(), llvm::erase_if(), llvm::LazyValueInfo::getConstant(), llvm::LazyValueInfo::getConstantOnEdge(), llvm::LazyValueInfo::getConstantRangeOnEdge(), llvm::BasicBlock::getDataLayout(), llvm::Instruction::getDataLayout(), llvm::ConstantInt::getFalse(), llvm::PHINode::getIncomingBlock(), llvm::PHINode::getIncomingValue(), getKnownConstant(), llvm::ConstantExpr::getNot(), llvm::PHINode::getNumIncomingValues(), llvm::ilist_detail::node_parent_access< NodeTy, ParentTy >::getParent(), getParent(), llvm::LazyValueInfo::getPredicateOnEdge(), llvm::ConstantInt::getTrue(), llvm::ConstantInt::getValue(), I, llvm::SmallPtrSetImpl< PtrType >::insert(), llvm::ConstantRange::inverse(), llvm::isGuaranteedNotToBeUndefOrPoison(), llvm::Type::isVectorTy(), LHS, llvm::PatternMatch::m_Add(), llvm::PatternMatch::m_Cmp(), llvm::PatternMatch::m_Constant(), llvm::PatternMatch::m_ConstantInt(), llvm::PatternMatch::m_LogicalAnd(), llvm::PatternMatch::m_LogicalOr(), llvm::PatternMatch::m_Value(), llvm::ConstantRange::makeExactICmpRegion(), llvm::PatternMatch::match(), P, llvm::predecessors(), RHS, llvm::simplifyCmpInst(), and llvm::jumpthreading::WantInteger.

Referenced by computeValueKnownInPredecessors(), and computeValueKnownInPredecessorsImpl().

◆ duplicateCondBranchOnPHIIntoPred()

bool JumpThreadingPass::duplicateCondBranchOnPHIIntoPred ( BasicBlock BB,
const SmallVectorImpl< BasicBlock * > &  PredBBs 
)

duplicateCondBranchOnPHIIntoPred - PredBB contains an unconditional branch to BB which contains an i1 PHI node and a conditional branch on that PHI.

If we can duplicate the contents of BB up into PredBB do so now, this improves the odds that the branch will be on an analyzable instruction like a compare.

Definition at line 2611 of file JumpThreading.cpp.

References addPHINodeEntriesForMappedBlock(), assert(), llvm::BasicBlock::begin(), llvm::Instruction::cloneDebugInfoFrom(), llvm::BranchProbabilityInfo::copyEdgeProbabilities(), llvm::dbgs(), llvm::DominatorTreeBase< BasicBlock, false >::Delete, DL, llvm::SmallVectorBase< Size_T >::empty(), llvm::BasicBlock::end(), llvm::ValueMap< KeyT, ValueT, Config >::end(), llvm::Instruction::eraseFromParent(), llvm::ValueMap< KeyT, ValueT, Config >::find(), llvm::BasicBlock::getDataLayout(), llvm::PHINode::getIncomingValueForBlock(), llvm::ilist_node_impl< OptionsT >::getIterator(), getJumpThreadDuplicationCost(), llvm::Value::getName(), llvm::ilist_node_impl< OptionsT >::getReverseIterator(), llvm::BranchInst::getSuccessor(), llvm::BasicBlock::getTerminator(), I, llvm::DominatorTreeBase< BasicBlock, false >::Insert, llvm::BranchInst::isUnconditional(), IV, LLVM_DEBUG, llvm::mapAtomInstance(), llvm::remapDebugVariable(), remapSourceAtoms(), llvm::BasicBlock::removePredecessor(), llvm::simplifyInstruction(), llvm::SmallVectorBase< Size_T >::size(), llvm::SplitEdge(), and updateSSA().

Referenced by processBranchOnPHI(), and processBranchOnXOR().

◆ evaluateOnPredecessorEdge()

Constant * JumpThreadingPass::evaluateOnPredecessorEdge ( BasicBlock BB,
BasicBlock PredPredBB,
Value cond,
const DataLayout DL 
)

◆ findLoopHeaders()

void JumpThreadingPass::findLoopHeaders ( Function F)

findLoopHeaders - We do not want jump threading to turn proper loop structures into irreducible loops.

Doing this breaks up the loop nesting hierarchy and pessimizes later transformations. To prevent this from happening, we first have to find the loop headers. Here we approximate this by finding targets of backedges in the CFG.

Note that there definitely are cases when we want to allow threading of edges across a loop header. For example, threading a jump from outside the loop (the preheader) to an exit block of the loop is definitely profitable. It is also almost always profitable to thread backedges from within the loop to exit blocks, and is often profitable to thread backedges to other blocks within the loop (forming a nested loop). This simple analysis is not rich enough to track all of these properties and keep it up-to-date as the CFG mutates, so we don't allow any of these transformations.

Definition at line 525 of file JumpThreading.cpp.

References F, llvm::FindFunctionBackedges(), and llvm::make_second_range().

Referenced by runImpl().

◆ getDomTreeUpdater()

DomTreeUpdater * llvm::JumpThreadingPass::getDomTreeUpdater ( ) const
inline

Definition at line 117 of file JumpThreading.h.

Referenced by run().

◆ maybeMergeBasicBlockIntoOnlyPred()

bool JumpThreadingPass::maybeMergeBasicBlockIntoOnlyPred ( BasicBlock BB)

◆ maybethreadThroughTwoBasicBlocks()

bool JumpThreadingPass::maybethreadThroughTwoBasicBlocks ( BasicBlock BB,
Value Cond 
)

◆ processBlock()

bool JumpThreadingPass::processBlock ( BasicBlock BB)

◆ processBranchOnPHI()

bool JumpThreadingPass::processBranchOnPHI ( PHINode PN)

processBranchOnPHI - We have an otherwise unthreadable conditional branch on a PHI node (or freeze PHI) in the current block.

See if there are any simplifications we can do based on inputs to the phi node.

Definition at line 1732 of file JumpThreading.cpp.

References duplicateCondBranchOnPHIIntoPred(), llvm::PHINode::getIncomingBlock(), llvm::PHINode::getNumIncomingValues(), llvm::ilist_detail::node_parent_access< NodeTy, ParentTy >::getParent(), llvm::BasicBlock::getTerminator(), and llvm::SmallVectorImpl< T >::resize().

Referenced by processBlock().

◆ processBranchOnXOR()

bool JumpThreadingPass::processBranchOnXOR ( BinaryOperator BO)

◆ processGuards()

bool JumpThreadingPass::processGuards ( BasicBlock BB)

Try to propagate a guard from the current BB into one of its predecessors in case if another branch of execution implies that the condition of this guard is always true.

Currently we only process the simplest case that looks like:

Start: cond = ... br i1 cond, label T1, label F1 T1: br label Merge F1: br label Merge Merge: condGuard = ... call void(i1, ...) @llvm.experimental.guard( i1 condGuard )[ "deopt"() ]

And cond either implies condGuard or !condGuard. In this case all the instructions before the guard can be duplicated in both branches, and the guard is then threaded to one of them.

Definition at line 3021 of file JumpThreading.cpp.

References llvm::BasicBlock::getSinglePredecessor(), I, llvm::isGuard(), llvm::pred_begin(), llvm::pred_end(), and threadGuard().

Referenced by processBlock().

◆ processImpliedCondition()

bool JumpThreadingPass::processImpliedCondition ( BasicBlock BB)

◆ processThreadableEdges()

bool JumpThreadingPass::processThreadableEdges ( Value Cond,
BasicBlock BB,
jumpthreading::ConstantPreference  Preference,
Instruction CxtI = nullptr 
)

◆ run()

PreservedAnalyses JumpThreadingPass::run ( Function F,
FunctionAnalysisManager AM 
)

◆ runImpl()

bool JumpThreadingPass::runImpl ( Function F,
FunctionAnalysisManager FAM,
TargetLibraryInfo TLI,
TargetTransformInfo TTI,
LazyValueInfo LVI,
AAResults AA,
std::unique_ptr< DomTreeUpdater DTU,
BlockFrequencyInfo BFI,
BranchProbabilityInfo BPI 
)

◆ simplifyPartiallyRedundantLoad()

bool JumpThreadingPass::simplifyPartiallyRedundantLoad ( LoadInst LoadI)

simplifyPartiallyRedundantLoad - If LoadI is an obviously partially redundant load instruction, eliminate it by replacing it with a PHI node.

This is an important optimization that encourages jump threading, and needs to be run interlaced with other jump threading tasks.

Definition at line 1220 of file JumpThreading.cpp.

References llvm::PHINode::addIncoming(), llvm::array_pod_sort(), assert(), llvm::BasicBlock::begin(), llvm::combineMetadataForCSE(), llvm::SmallPtrSetImpl< PtrType >::count(), llvm::PHINode::Create(), llvm::CastInst::CreateBitOrPointerCast(), llvm::DefMaxInstsToScan, llvm::BatchAAResults::disableDominatorTree(), DL, llvm::Value::DoPHITranslation(), llvm::SmallVectorTemplateCommon< T, typename >::end(), llvm::BasicBlock::end(), llvm::Instruction::eraseFromParent(), llvm::FindAvailableLoadedValue(), llvm::findAvailablePtrLoadStore(), llvm::LazyValueInfo::forgetValue(), llvm::from_range, llvm::PoisonValue::get(), llvm::Instruction::getAAMetadata(), llvm::LoadInst::getAlign(), llvm::Instruction::getDataLayout(), llvm::Instruction::getDebugLoc(), llvm::ilist_node_impl< OptionsT >::getIterator(), llvm::Value::getName(), llvm::Instruction::getNumSuccessors(), llvm::User::getOperand(), llvm::LoadInst::getOrdering(), llvm::ilist_detail::node_parent_access< NodeTy, ParentTy >::getParent(), llvm::BasicBlock::getSinglePredecessor(), llvm::LoadInst::getSyncScopeID(), llvm::BasicBlock::getTerminator(), llvm::Value::getType(), I, llvm::SmallPtrSetImpl< PtrType >::insert(), llvm::Instruction::insertBefore(), llvm::Instruction::isAtomic(), llvm::BasicBlock::isEHPad(), llvm::isGuaranteedToTransferExecutionToSuccessor(), isOpDefinedInBlock(), llvm::isSafeToSpeculativelyExecute(), llvm::LoadInst::isUnordered(), llvm::lower_bound(), llvm::make_first_range(), P, llvm::LocationSize::precise(), llvm::pred_size(), llvm::predecessors(), llvm::SmallVectorTemplateBase< T, bool >::push_back(), llvm::Value::replaceAllUsesWith(), llvm::Instruction::setAAMetadata(), llvm::Instruction::setDebugLoc(), llvm::SmallPtrSetImplBase::size(), and llvm::Value::takeName().

Referenced by processBlock().

◆ threadEdge()

void JumpThreadingPass::threadEdge ( BasicBlock BB,
const SmallVectorImpl< BasicBlock * > &  PredBBs,
BasicBlock SuccBB 
)

◆ threadGuard()

bool JumpThreadingPass::threadGuard ( BasicBlock BB,
IntrinsicInst Guard,
BranchInst BI 
)

◆ threadThroughTwoBasicBlocks()

void JumpThreadingPass::threadThroughTwoBasicBlocks ( BasicBlock PredPredBB,
BasicBlock PredBB,
BasicBlock BB,
BasicBlock SuccBB 
)

◆ tryThreadEdge()

bool JumpThreadingPass::tryThreadEdge ( BasicBlock BB,
const SmallVectorImpl< BasicBlock * > &  PredBBs,
BasicBlock SuccBB 
)

tryThreadEdge - Thread an edge if it's safe and profitable to do so.

Definition at line 2327 of file JumpThreading.cpp.

References llvm::dbgs(), getJumpThreadDuplicationCost(), llvm::Value::getName(), llvm::BasicBlock::getTerminator(), LLVM_DEBUG, and threadEdge().

Referenced by processThreadableEdges().

◆ tryToUnfoldSelect() [1/2]

bool JumpThreadingPass::tryToUnfoldSelect ( CmpInst CondCmp,
BasicBlock BB 
)

tryToUnfoldSelect - Look for blocks of the form bb1: a = select br bb2

bb2: p = phi [a, bb1] ... c = icmp p br i1 c

And expand the select into a branch structure if one of its arms allows c to be folded. This later enables threading from bb1 over bb2.

Definition at line 2857 of file JumpThreading.cpp.

References llvm::PHINode::getIncomingBlock(), llvm::PHINode::getIncomingValue(), llvm::PHINode::getNumIncomingValues(), llvm::User::getOperand(), llvm::ilist_detail::node_parent_access< NodeTy, ParentTy >::getParent(), llvm::CmpInst::getPredicate(), llvm::LazyValueInfo::getPredicateOnEdge(), llvm::BasicBlock::getTerminator(), I, llvm::BranchInst::isConditional(), llvm::BranchInst::isUnconditional(), and unfoldSelectInstr().

Referenced by processBlock().

◆ tryToUnfoldSelect() [2/2]

bool JumpThreadingPass::tryToUnfoldSelect ( SwitchInst SI,
BasicBlock BB 
)

◆ tryToUnfoldSelectInCurrBB()

bool JumpThreadingPass::tryToUnfoldSelectInCurrBB ( BasicBlock BB)

tryToUnfoldSelectInCurrBB - Look for PHI/Select or PHI/CMP/Select in the same BB in the form bb: p = phi [false, bb1], [true, bb2], [false, bb3], [true, bb4], ... s = select p, trueval, falseval

or

bb: p = phi [0, bb1], [1, bb2], [0, bb3], [1, bb4], ... c = cmp p, 0 s = select c, trueval, falseval

And expand the select into a branch structure. This later enables jump-threading over bb in this pass.

Using the similar approach of SimplifyCFG::FoldCondBranchOnPHI(), unfold select if the associated PHI has at least one constant. If the unfolded select is not jump-threaded, it will be folded again in the later optimizations.

Definition at line 2916 of file JumpThreading.cpp.

References llvm::PHINode::addIncoming(), llvm::all_of(), llvm::BasicBlock::begin(), Cond, llvm::PHINode::Create(), llvm::DominatorTreeBase< BasicBlock, false >::Delete, llvm::getBranchWeightMDNode(), llvm::Instruction::getNumSuccessors(), llvm::BasicBlock::getParent(), llvm::DebugLoc::getTemporary(), llvm::BasicBlock::getTerminator(), llvm::Function::hasFnAttribute(), llvm::DominatorTreeBase< BasicBlock, false >::Insert, llvm::isGuaranteedNotToBeUndefOrPoison(), llvm::PatternMatch::m_CombineOr(), llvm::PatternMatch::m_LogicalAnd(), llvm::PatternMatch::m_LogicalOr(), llvm::PatternMatch::match(), llvm::Instruction::setDebugLoc(), llvm::SplitBlockAndInsertIfThen(), and llvm::successors().

Referenced by processBlock().

◆ unfoldSelectInstr()

void JumpThreadingPass::unfoldSelectInstr ( BasicBlock Pred,
BasicBlock BB,
SelectInst SI,
PHINode SIUse,
unsigned  Idx 
)

◆ updateSSA()

void JumpThreadingPass::updateSSA ( BasicBlock BB,
BasicBlock NewBB,
ValueToValueMapTy ValueMapping 
)

The documentation for this class was generated from the following files: