|
LLVM
4.0.0
|
This pass performs 'jump threading', which looks at blocks that have multiple predecessors and multiple successors. More...
#include <JumpThreading.h>
Public Member Functions | |
| JumpThreadingPass (int T=-1) | |
| bool | runImpl (Function &F, TargetLibraryInfo *TLI_, LazyValueInfo *LVI_, 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 | 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 | ComputeValueKnownInPredecessors (Value *V, BasicBlock *BB, jumpthreading::PredValueInfo &Result, jumpthreading::ConstantPreference Preference, 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 | 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 LI is an obviously partially redundant load instruction, eliminate it by replacing it with a PHI node. More... | |
| bool | TryToUnfoldSelect (CmpInst *CondCmp, BasicBlock *BB) |
| TryToUnfoldSelect - Look for blocks of the form bb1: a = select br bb. More... | |
| bool | TryToUnfoldSelectInCurrBB (BasicBlock *BB) |
| TryToUnfoldSelectInCurrBB - Look for PHI/Select in the same BB of the form bb: p = phi [false, bb1], [true, bb2], [false, bb3], [true, bb4], ... 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... | |
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 59 of file JumpThreading.h.
| JumpThreadingPass::JumpThreadingPass | ( | int | T = -1 | ) |
Definition at line 113 of file JumpThreading.cpp.
References BBDuplicateThreshold.
| bool JumpThreadingPass::ComputeValueKnownInPredecessors | ( | Value * | V, |
| BasicBlock * | BB, | ||
| jumpthreading::PredValueInfo & | Result, | ||
| jumpthreading::ConstantPreference | Preference, | ||
| 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 375 of file JumpThreading.cpp.
References llvm::APIntOps::And(), assert(), C, llvm::Value::DoPHITranslation(), llvm::dyn_cast(), 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(), I, i, llvm::Type::isIntegerTy(), llvm::APIntOps::Or(), P, llvm::predecessors(), llvm::SmallVectorTemplateBase< T, isPodLike >::push_back(), SI, llvm::SimplifyCmpInst(), llvm::Sched::Source, llvm::LazyValueInfo::Unknown, llvm::jumpthreading::WantBlockAddress, llvm::jumpthreading::WantInteger, and llvm::APIntOps::Xor().
| 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 1745 of file JumpThreading.cpp.
References AddPHINodeEntriesForMappedBlock(), assert(), llvm::BasicBlock::begin(), llvm::dbgs(), DEBUG, llvm::dyn_cast(), llvm::SmallVectorBase::empty(), llvm::DenseMapBase< DenseMap< KeyT, ValueT, KeyInfoT, BucketT >, KeyT, ValueT, KeyInfoT, BucketT >::end(), llvm::BasicBlock::end(), llvm::DenseMapBase< DenseMap< KeyT, ValueT, KeyInfoT, BucketT >, KeyT, ValueT, KeyInfoT, BucketT >::find(), llvm::Module::getDataLayout(), llvm::PHINode::getIncomingValueForBlock(), llvm::BasicBlock::getInstList(), getJumpThreadDuplicationCost(), llvm::BasicBlock::getModule(), llvm::Value::getName(), llvm::User::getNumOperands(), llvm::User::getOperand(), llvm::Instruction::getParent(), llvm::BranchInst::getSuccessor(), llvm::BasicBlock::getTerminator(), i, llvm::iplist_impl< IntrusiveListT, TraitsT >::insert(), llvm::Instruction::mayHaveSideEffects(), llvm::SmallVectorImpl< T >::pop_back_val(), llvm::SmallVectorTemplateBase< T, isPodLike< T >::value >::push_back(), llvm::Value::setName(), llvm::User::setOperand(), llvm::SimplifyInstruction(), llvm::SmallVectorTemplateCommon< T >::size(), and llvm::SplitEdge().
| 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 341 of file JumpThreading.cpp.
References llvm::FindFunctionBackedges().
| bool JumpThreadingPass::ProcessBlock | ( | BasicBlock * | BB | ) |
ProcessBlock - If there are any predecessors whose control can be threaded through to a successor, transform them now.
Definition at line 703 of file JumpThreading.cpp.
References llvm::ConstantFoldInstruction(), llvm::ConstantFoldTerminator(), llvm::BranchInst::Create(), llvm::dbgs(), DEBUG, llvm::dyn_cast(), llvm::Instruction::eraseFromParent(), GetBestDestForJumpOnUndef(), llvm::Module::getDataLayout(), llvm::Function::getEntryBlock(), llvm::ConstantInt::getFalse(), getKnownConstant(), llvm::BasicBlock::getModule(), llvm::Value::getName(), llvm::TerminatorInst::getNumSuccessors(), llvm::Instruction::getOpcode(), llvm::Instruction::getParent(), llvm::BasicBlock::getParent(), llvm::BasicBlock::getSinglePredecessor(), llvm::TerminatorInst::getSuccessor(), llvm::BranchInst::getSuccessor(), llvm::BasicBlock::getTerminator(), llvm::ConstantInt::getTrue(), hasAddressTakenAndUsed(), I, i, llvm::BranchInst::isConditional(), llvm::TerminatorInst::isExceptional(), llvm::isInstructionTriviallyDead(), llvm::MergeBasicBlockIntoOnlyPred(), llvm::pred_empty(), llvm::BasicBlock::removePredecessor(), llvm::Value::replaceAllUsesWith(), llvm::MipsISD::Ret, SI, llvm::Value::stripPointerCasts(), llvm::MCID::Terminator, llvm::LazyValueInfo::True, llvm::LazyValueInfo::Unknown, llvm::jumpthreading::WantBlockAddress, llvm::jumpthreading::WantInteger, and llvm::APIntOps::Xor().
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 1290 of file JumpThreading.cpp.
References llvm::PHINode::getIncomingBlock(), llvm::PHINode::getNumIncomingValues(), llvm::Instruction::getParent(), llvm::BasicBlock::getTerminator(), i, and llvm::SmallVectorImpl< T >::resize().
| bool JumpThreadingPass::ProcessBranchOnXOR | ( | BinaryOperator * | BO | ) |
ProcessBranchOnXOR - We have an otherwise unthreadable conditional branch on a xor instruction in the current block.
See if there are any simplifications we can do based on inputs to the xor.
Definition at line 1320 of file JumpThreading.cpp.
References assert(), llvm::Instruction::eraseFromParent(), llvm::BasicBlock::front(), llvm::UndefValue::get(), llvm::BasicBlock::getContext(), llvm::ConstantInt::getFalse(), llvm::User::getOperand(), llvm::Instruction::getParent(), llvm::ConstantInt::getTrue(), llvm::Value::getType(), llvm::BasicBlock::isEHPad(), llvm::ConstantInt::isZero(), llvm::SmallVectorTemplateBase< T, isPodLike >::push_back(), llvm::Value::replaceAllUsesWith(), llvm::User::setOperand(), and llvm::jumpthreading::WantInteger.
| bool JumpThreadingPass::ProcessImpliedCondition | ( | BasicBlock * | BB | ) |
Definition at line 888 of file JumpThreading.cpp.
References llvm::BranchInst::Create(), llvm::dyn_cast(), llvm::Instruction::eraseFromParent(), llvm::Module::getDataLayout(), llvm::BasicBlock::getModule(), llvm::BasicBlock::getSinglePredecessor(), llvm::BranchInst::getSuccessor(), llvm::BasicBlock::getTerminator(), ImplicationSearchThreshold, and llvm::isImpliedCondition().
| bool JumpThreadingPass::ProcessThreadableEdges | ( | Value * | Cond, |
| BasicBlock * | BB, | ||
| jumpthreading::ConstantPreference | Preference, | ||
| Instruction * | CxtI = nullptr |
||
| ) |
Definition at line 1182 of file JumpThreading.cpp.
References assert(), llvm::dbgs(), DEBUG, llvm::SmallVectorBase::empty(), FindMostPopularDest(), GetBestDestForJumpOnUndef(), llvm::Value::getName(), llvm::BasicBlock::getTerminator(), isZero(), llvm::SmallVectorTemplateBase< T, isPodLike< T >::value >::push_back(), SI, and llvm::successors().
|
inline |
Definition at line 96 of file JumpThreading.h.
| PreservedAnalyses JumpThreadingPass::run | ( | Function & | F, |
| FunctionAnalysisManager & | AM | ||
| ) |
Definition at line 136 of file JumpThreading.cpp.
References llvm::PreservedAnalyses::all(), llvm::AMDGPUISD::BFI, F, llvm::Function::getEntryCount(), llvm::AnalysisManager< IRUnitT, ExtraArgTs >::getResult(), llvm::Optional< T >::hasValue(), llvm::AnalysisManager< IRUnitT, ExtraArgTs >::invalidate(), llvm::PreservedAnalyses::preserve(), llvm::DominatorTreeBase< N >::reset(), and runImpl().
| bool JumpThreadingPass::runImpl | ( | Function & | F, |
| TargetLibraryInfo * | TLI_, | ||
| LazyValueInfo * | LVI_, | ||
| bool | HasProfileData_, | ||
| std::unique_ptr< BlockFrequencyInfo > | BFI_, | ||
| std::unique_ptr< BranchProbabilityInfo > | BPI_ | ||
| ) |
Definition at line 163 of file JumpThreading.cpp.
References llvm::Function::begin(), llvm::dbgs(), DEBUG, llvm::DeleteDeadBlock(), llvm::dyn_cast(), E, llvm::Function::end(), llvm::Function::getEntryBlock(), llvm::BasicBlock::getFirstNonPHIOrDbg(), llvm::Value::getName(), llvm::BasicBlock::getParent(), llvm::BranchInst::getSuccessor(), llvm::BasicBlock::getTerminator(), I, llvm::Instruction::isTerminator(), llvm::BranchInst::isUnconditional(), llvm::pred_empty(), ProcessBlock(), llvm::removeUnreachableBlocks(), and llvm::TryToSimplifyUncondBranchFromEmptyBlock().
SimplifyPartiallyRedundantLoad - If LI 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 927 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::Instruction::eraseFromParent(), llvm::FindAvailableLoadedValue(), llvm::BasicBlock::front(), llvm::UndefValue::get(), llvm::Instruction::getAAMetadata(), llvm::LoadInst::getAlignment(), llvm::Instruction::getDebugLoc(), llvm::Value::getName(), llvm::TerminatorInst::getNumSuccessors(), llvm::User::getOperand(), llvm::LoadInst::getOrdering(), llvm::Instruction::getParent(), llvm::BasicBlock::getSinglePredecessor(), llvm::LoadInst::getSynchScope(), llvm::BasicBlock::getTerminator(), llvm::Value::getType(), I, llvm::SmallPtrSetImpl< PtrType >::insert(), llvm::BasicBlock::isEHPad(), llvm::LoadInst::isUnordered(), P, llvm::pred_begin(), llvm::pred_end(), llvm::predecessors(), llvm::SmallVectorTemplateBase< T, isPodLike< T >::value >::push_back(), llvm::Value::replaceAllUsesWith(), llvm::Instruction::setDebugLoc(), llvm::SmallPtrSetImplBase::size(), and llvm::Value::takeName().
| bool 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 1452 of file JumpThreading.cpp.
References llvm::SSAUpdater::AddAvailableValue(), AddPHINodeEntriesForMappedBlock(), llvm::BasicBlock::begin(), llvm::BasicBlock::Create(), llvm::BranchInst::Create(), llvm::dbgs(), DEBUG, llvm::dyn_cast(), llvm::SmallVectorBase::empty(), llvm::DenseMapBase< DenseMap< KeyT, ValueT, KeyInfoT, BucketT >, KeyT, ValueT, KeyInfoT, BucketT >::end(), llvm::DenseMapBase< DenseMap< KeyT, ValueT, KeyInfoT, BucketT >, KeyT, ValueT, KeyInfoT, BucketT >::find(), llvm::BasicBlock::getContext(), llvm::Instruction::getDebugLoc(), llvm::PHINode::getIncomingValueForBlock(), llvm::BasicBlock::getInstList(), getJumpThreadDuplicationCost(), llvm::Value::getName(), llvm::User::getNumOperands(), llvm::TerminatorInst::getNumSuccessors(), llvm::User::getOperand(), llvm::Instruction::getParent(), llvm::BasicBlock::getParent(), llvm::TerminatorInst::getSuccessor(), llvm::BasicBlock::getTerminator(), I, i, llvm::SSAUpdater::Initialize(), llvm::BasicBlock::moveAfter(), llvm::SmallVectorImpl< T >::pop_back_val(), llvm::SmallVectorTemplateBase< T, isPodLike< T >::value >::push_back(), llvm::iplist_impl< IntrusiveListT, TraitsT >::push_back(), llvm::SSAUpdater::RewriteUse(), llvm::Instruction::setDebugLoc(), llvm::Value::setName(), llvm::User::setOperand(), llvm::TerminatorInst::setSuccessor(), llvm::SimplifyInstructionsInBlock(), and llvm::SmallVectorTemplateCommon< T >::size().
| bool JumpThreadingPass::TryToUnfoldSelect | ( | CmpInst * | CondCmp, |
| BasicBlock * | BB | ||
| ) |
TryToUnfoldSelect - Look for blocks of the form bb1: a = select br bb.
bb2: p = phi [a, bb] ... 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 1900 of file JumpThreading.cpp.
References llvm::PHINode::addIncoming(), llvm::BasicBlock::Create(), llvm::BranchInst::Create(), llvm::dyn_cast(), E, llvm::BasicBlock::end(), llvm::Instruction::eraseFromParent(), llvm::SelectInst::getCondition(), llvm::SelectInst::getFalseValue(), llvm::PHINode::getIncomingValueForBlock(), llvm::BasicBlock::getInstList(), llvm::User::getOperand(), llvm::Instruction::getParent(), llvm::CmpInst::getPredicate(), llvm::BasicBlock::getTerminator(), llvm::SelectInst::getTrueValue(), llvm::Value::hasOneUse(), I, llvm::iplist_impl< IntrusiveListT, TraitsT >::insert(), llvm::BranchInst::isConditional(), llvm::BranchInst::isUnconditional(), llvm::Instruction::removeFromParent(), SI, and llvm::LazyValueInfo::Unknown.
| bool JumpThreadingPass::TryToUnfoldSelectInCurrBB | ( | BasicBlock * | BB | ) |
TryToUnfoldSelectInCurrBB - Look for PHI/Select in the same BB of the form bb: p = phi [false, bb1], [true, bb2], [false, bb3], [true, bb4], ...
s = select p, 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 1978 of file JumpThreading.cpp.
References llvm::PHINode::addIncoming(), llvm::BasicBlock::begin(), llvm::PHINode::Create(), llvm::dyn_cast(), llvm::Instruction::eraseFromParent(), llvm::SelectInst::getCondition(), llvm::SelectInst::getFalseValue(), llvm::PHINode::getIncomingBlock(), llvm::PHINode::getIncomingValue(), llvm::PHINode::getNumIncomingValues(), llvm::Instruction::getParent(), llvm::SelectInst::getTrueValue(), llvm::Value::getType(), llvm::Value::hasOneUse(), i, llvm::Type::isIntegerTy(), llvm::Value::replaceAllUsesWith(), SI, llvm::SplitBlockAndInsertIfThen(), and llvm::Instruction::user_back().
1.8.6