LLVM  10.0.0
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]
Collaboration diagram for llvm::JumpThreadingPass:
[legend]

Public Member Functions

 JumpThreadingPass (int T=-1)
 
bool runImpl (Function &F, TargetLibraryInfo *TLI_, LazyValueInfo *LVI_, AliasAnalysis *AA_, DomTreeUpdater *DTU_, bool HasProfileData_, std::unique_ptr< BlockFrequencyInfo > BFI_, std::unique_ptr< BranchProbabilityInfo > BPI_)
 
PreservedAnalyses run (Function &F, FunctionAnalysisManager &AM)
 
void releaseMemory ()
 
void FindLoopHeaders (Function &F)
 FindLoopHeaders - We do not want jump threading to turn proper loop structures into irreducible loops. More...
 
bool ProcessBlock (BasicBlock *BB)
 ProcessBlock - If there are any predecessors whose control can be threaded through to a successor, transform them now. More...
 
bool MaybeMergeBasicBlockIntoOnlyPred (BasicBlock *BB)
 Merge basic block BB into its sole predecessor if possible. More...
 
void UpdateSSA (BasicBlock *BB, BasicBlock *NewBB, DenseMap< Instruction *, Value *> &ValueMapping)
 Update the SSA form. More...
 
DenseMap< Instruction *, Value * > CloneInstructions (BasicBlock::iterator BI, BasicBlock::iterator BE, BasicBlock *NewBB, BasicBlock *PredBB)
 Clone instructions in range [BI, BE) to NewBB. More...
 
bool TryThreadEdge (BasicBlock *BB, const SmallVectorImpl< BasicBlock *> &PredBBs, BasicBlock *SuccBB)
 TryThreadEdge - Thread an edge if it's safe and profitable to do so. More...
 
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. More...
 
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. More...
 
bool ComputeValueKnownInPredecessorsImpl (Value *V, BasicBlock *BB, jumpthreading::PredValueInfo &Result, jumpthreading::ConstantPreference Preference, DenseSet< std::pair< Value *, BasicBlock *>> &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. More...
 
bool ComputeValueKnownInPredecessors (Value *V, BasicBlock *BB, jumpthreading::PredValueInfo &Result, jumpthreading::ConstantPreference Preference, Instruction *CxtI=nullptr)
 
bool ProcessThreadableEdges (Value *Cond, BasicBlock *BB, jumpthreading::ConstantPreference Preference, Instruction *CxtI=nullptr)
 
bool ProcessBranchOnPHI (PHINode *PN)
 ProcessBranchOnPHI - We have an otherwise unthreadable conditional branch on a PHI node in the current block. More...
 
bool ProcessBranchOnXOR (BinaryOperator *BO)
 ProcessBranchOnXOR - We have an otherwise unthreadable conditional branch on a xor instruction in the current block. More...
 
bool ProcessImpliedCondition (BasicBlock *BB)
 
bool SimplifyPartiallyRedundantLoad (LoadInst *LI)
 SimplifyPartiallyRedundantLoad - If LoadI is an obviously partially redundant load instruction, eliminate it by replacing it with a PHI node. More...
 
void UnfoldSelectInstr (BasicBlock *Pred, BasicBlock *BB, SelectInst *SI, PHINode *SIUse, unsigned Idx)
 
bool TryToUnfoldSelect (CmpInst *CondCmp, BasicBlock *BB)
 TryToUnfoldSelect - Look for blocks of the form bb1: a = select br bb2. More...
 
bool TryToUnfoldSelect (SwitchInst *SI, BasicBlock *BB)
 
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], ... More...
 
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. More...
 
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. More...
 

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. More...
 

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 77 of file JumpThreading.h.

Constructor & Destructor Documentation

◆ JumpThreadingPass()

JumpThreadingPass::JumpThreadingPass ( int  T = -1)

Definition at line 172 of file JumpThreading.cpp.

References BBDuplicateThreshold.

Member Function Documentation

◆ CloneInstructions()

DenseMap< Instruction *, Value * > JumpThreadingPass::CloneInstructions ( 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 133 of file JumpThreading.h.

◆ ComputeValueKnownInPredecessorsImpl()

bool JumpThreadingPass::ComputeValueKnownInPredecessorsImpl ( Value *  V,
BasicBlock *  BB,
jumpthreading::PredValueInfo &  Result,
jumpthreading::ConstantPreference  Preference,
DenseSet< std::pair< Value *, BasicBlock *>> &  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.

TODO: Per PR2563, we could infer value range information about a predecessor based on its terminator.

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

Definition at line 593 of file JumpThreading.cpp.

References llvm::ConstantRange::add(), assert(), C, llvm::ConstantRange::contains(), llvm::Value::DoPHITranslation(), llvm::dyn_cast(), llvm::numbers::e, llvm::SmallVectorImpl< T >::emplace_back(), llvm::SmallVectorBase::empty(), llvm::ConstantInt::get(), llvm::ConstantExpr::get(), llvm::ConstantExpr::getCast(), llvm::ConstantExpr::getCompare(), llvm::Value::getContext(), llvm::Module::getDataLayout(), llvm::ConstantInt::getFalse(), llvm::PHINode::getIncomingBlock(), llvm::PHINode::getIncomingValue(), llvm::Type::getInt1Ty(), getKnownConstant(), llvm::Instruction::getModule(), llvm::ConstantExpr::getNot(), llvm::PHINode::getNumIncomingValues(), llvm::Instruction::getOpcode(), llvm::User::getOperand(), llvm::Instruction::getParent(), getParent(), llvm::Type::getPrimitiveSizeInBits(), llvm::ConstantInt::getTrue(), llvm::Value::getType(), llvm::ConstantInt::getValue(), I, llvm::ConstantRange::inverse(), llvm::Type::isVectorTy(), llvm::PatternMatch::m_Add(), llvm::PatternMatch::m_ConstantInt(), llvm::PatternMatch::m_Value(), llvm::ConstantRange::makeExactICmpRegion(), llvm::PatternMatch::match(), P, llvm::predecessors(), llvm::SmallVectorTemplateBase< T, bool >::push_back(), SI, llvm::SimplifyCmpInst(), llvm::Sched::Source, llvm::LazyValueInfo::Unknown, llvm::jumpthreading::WantBlockAddress, and llvm::jumpthreading::WantInteger.

◆ DuplicateCondBranchOnPHIIntoPred()

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

◆ 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 560 of file JumpThreading.cpp.

References llvm::FindFunctionBackedges().

◆ MaybeMergeBasicBlockIntoOnlyPred()

bool JumpThreadingPass::MaybeMergeBasicBlockIntoOnlyPred ( BasicBlock *  BB)

◆ ProcessBlock()

bool JumpThreadingPass::ProcessBlock ( BasicBlock *  BB)

◆ ProcessBranchOnPHI()

bool JumpThreadingPass::ProcessBranchOnPHI ( PHINode *  PN)

ProcessBranchOnPHI - We have an otherwise unthreadable conditional branch on a PHI node in the current block.

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

Definition at line 1726 of file JumpThreading.cpp.

References llvm::numbers::e, llvm::PHINode::getIncomingBlock(), llvm::PHINode::getNumIncomingValues(), llvm::Instruction::getParent(), llvm::BasicBlock::getTerminator(), and llvm::SmallVectorImpl< T >::resize().

◆ 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, ...) .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 2657 of file JumpThreading.cpp.

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

◆ ProcessImpliedCondition()

bool JumpThreadingPass::ProcessImpliedCondition ( BasicBlock *  BB)

Definition at line 1201 of file JumpThreading.cpp.

References llvm::dyn_cast(), and llvm::BasicBlock::getTerminator().

◆ ProcessThreadableEdges()

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

Definition at line 1551 of file JumpThreading.cpp.

◆ releaseMemory()

void llvm::JumpThreadingPass::releaseMemory ( )
inline

Definition at line 105 of file JumpThreading.h.

References ProcessBlock().

◆ run()

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

◆ runImpl()

bool JumpThreadingPass::runImpl ( Function &  F,
TargetLibraryInfo *  TLI_,
LazyValueInfo *  LVI_,
AliasAnalysis *  AA_,
DomTreeUpdater *  DTU_,
bool  HasProfileData_,
std::unique_ptr< BlockFrequencyInfo >  BFI_,
std::unique_ptr< 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 1252 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::Value::DoPHITranslation(), llvm::SmallVectorTemplateCommon< T, typename >::end(), llvm::BasicBlock::end(), llvm::Instruction::eraseFromParent(), llvm::FindAvailableLoadedValue(), llvm::FindAvailablePtrLoadStore(), llvm::BasicBlock::front(), llvm::UndefValue::get(), llvm::Instruction::getAAMetadata(), llvm::LoadInst::getAlignment(), llvm::Instruction::getDebugLoc(), llvm::Value::getName(), llvm::Instruction::getNumSuccessors(), llvm::User::getOperand(), llvm::LoadInst::getOrdering(), llvm::Instruction::getParent(), llvm::BasicBlock::getSinglePredecessor(), llvm::LoadInst::getSyncScopeID(), llvm::BasicBlock::getTerminator(), llvm::Value::getType(), I, llvm::SmallPtrSetImpl< PtrType >::insert(), llvm::Instruction::isAtomic(), llvm::BasicBlock::isEHPad(), llvm::isGuaranteedToTransferExecutionToSuccessor(), isOpDefinedInBlock(), llvm::isSafeToSpeculativelyExecute(), llvm::LoadInst::isUnordered(), llvm::lower_bound(), P, llvm::pred_begin(), llvm::pred_end(), llvm::predecessors(), llvm::SmallVectorTemplateBase< T >::push_back(), llvm::Value::replaceAllUsesWith(), llvm::Instruction::setDebugLoc(), llvm::SmallPtrSetImplBase::size(), and llvm::Value::takeName().

◆ ThreadEdge()

void JumpThreadingPass::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.

Transform the IR to reflect this change.

Definition at line 2058 of file JumpThreading.cpp.

References AddPHINodeEntriesForMappedBlock(), assert(), llvm::SmallVectorImpl< T >::assign(), llvm::SmallVectorTemplateCommon< T, typename >::begin(), llvm::BasicBlock::begin(), llvm::AMDGPUISD::BFI, llvm::BasicBlock::Create(), llvm::BranchInst::Create(), llvm::dbgs(), llvm::DominatorTreeBase< BasicBlock, false >::Delete, llvm::numbers::e, E, llvm::SmallVectorTemplateCommon< T, typename >::end(), llvm::BasicBlock::end(), llvm::BranchProbability::getBranchProbability(), llvm::BasicBlock::getContext(), llvm::Instruction::getDebugLoc(), llvm::BlockFrequency::getFrequency(), llvm::Instruction::getMetadata(), llvm::Value::getName(), llvm::Instruction::getNumSuccessors(), llvm::BasicBlock::getParent(), llvm::MDString::getString(), llvm::Instruction::getSuccessor(), llvm::BasicBlock::getTerminator(), I, llvm::DenseMapBase< DenseMap< KeyT, ValueT, KeyInfoT, BucketT >, KeyT, ValueT, KeyInfoT, BucketT >::insert(), llvm::DominatorTreeBase< BasicBlock, false >::Insert, llvm::BasicBlock::isLandingPad(), LLVM_DEBUG, llvm::DenseMapBase< DenseMap< KeyT, ValueT, KeyInfoT, BucketT >, KeyT, ValueT, KeyInfoT, BucketT >::lookup(), llvm::BasicBlock::moveAfter(), llvm::BranchProbability::normalizeProbabilities(), llvm::predecessors(), llvm::SmallVectorTemplateBase< T, bool >::push_back(), llvm::SmallVectorTemplateBase< T >::push_back(), llvm::BasicBlock::removePredecessor(), llvm::Instruction::setDebugLoc(), llvm::Instruction::setMetadata(), llvm::Instruction::setSuccessor(), llvm::SimplifyInstructionsInBlock(), llvm::SmallVectorBase::size(), llvm::SplitBlockPredecessors(), llvm::SplitLandingPadPredecessors(), and llvm::successors().

◆ ThreadGuard()

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

◆ 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 2019 of file JumpThreading.cpp.

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

◆ 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 2502 of file JumpThreading.cpp.

References llvm::dyn_cast(), E, llvm::User::getOperand(), llvm::Instruction::getParent(), llvm::CmpInst::getPredicate(), llvm::BasicBlock::getTerminator(), llvm::Value::hasOneUse(), I, llvm::BranchInst::isConditional(), llvm::BranchInst::isUnconditional(), SI, and llvm::LazyValueInfo::Unknown.

◆ 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 2567 of file JumpThreading.cpp.

References llvm::all_of(), llvm::BasicBlock::begin(), llvm::dyn_cast(), llvm::Instruction::getParent(), llvm::PHINode::incoming_values(), and SI.

◆ UnfoldSelectInstr()

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

◆ UpdateSSA()

void JumpThreadingPass::UpdateSSA ( BasicBlock *  BB,
BasicBlock *  NewBB,
DenseMap< Instruction *, Value *> &  ValueMapping 
)

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