diff options
author | shadchin <shadchin@yandex-team.ru> | 2022-02-10 16:44:30 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:44:30 +0300 |
commit | 2598ef1d0aee359b4b6d5fdd1758916d5907d04f (patch) | |
tree | 012bb94d777798f1f56ac1cec429509766d05181 /contrib/libs/llvm12/lib/Transforms/Scalar/JumpThreading.cpp | |
parent | 6751af0b0c1b952fede40b19b71da8025b5d8bcf (diff) | |
download | ydb-2598ef1d0aee359b4b6d5fdd1758916d5907d04f.tar.gz |
Restoring authorship annotation for <shadchin@yandex-team.ru>. Commit 1 of 2.
Diffstat (limited to 'contrib/libs/llvm12/lib/Transforms/Scalar/JumpThreading.cpp')
-rw-r--r-- | contrib/libs/llvm12/lib/Transforms/Scalar/JumpThreading.cpp | 434 |
1 files changed, 217 insertions, 217 deletions
diff --git a/contrib/libs/llvm12/lib/Transforms/Scalar/JumpThreading.cpp b/contrib/libs/llvm12/lib/Transforms/Scalar/JumpThreading.cpp index 10b08b4e22..3e86ad4c14 100644 --- a/contrib/libs/llvm12/lib/Transforms/Scalar/JumpThreading.cpp +++ b/contrib/libs/llvm12/lib/Transforms/Scalar/JumpThreading.cpp @@ -32,7 +32,7 @@ #include "llvm/Analysis/Loads.h" #include "llvm/Analysis/LoopInfo.h" #include "llvm/Analysis/TargetLibraryInfo.h" -#include "llvm/Analysis/TargetTransformInfo.h" +#include "llvm/Analysis/TargetTransformInfo.h" #include "llvm/Analysis/ValueTracking.h" #include "llvm/IR/BasicBlock.h" #include "llvm/IR/CFG.h" @@ -105,11 +105,11 @@ static cl::opt<bool> PrintLVIAfterJumpThreading( cl::desc("Print the LazyValueInfo cache after JumpThreading"), cl::init(false), cl::Hidden); -static cl::opt<bool> JumpThreadingFreezeSelectCond( - "jump-threading-freeze-select-cond", - cl::desc("Freeze the condition when unfolding select"), cl::init(false), - cl::Hidden); - +static cl::opt<bool> JumpThreadingFreezeSelectCond( + "jump-threading-freeze-select-cond", + cl::desc("Freeze the condition when unfolding select"), cl::init(false), + cl::Hidden); + static cl::opt<bool> ThreadAcrossLoopHeaders( "jump-threading-across-loop-headers", cl::desc("Allow JumpThreading to thread across loop headers, for testing"), @@ -139,8 +139,8 @@ namespace { public: static char ID; // Pass identification - JumpThreading(bool InsertFreezeWhenUnfoldingSelect = false, int T = -1) - : FunctionPass(ID), Impl(InsertFreezeWhenUnfoldingSelect, T) { + JumpThreading(bool InsertFreezeWhenUnfoldingSelect = false, int T = -1) + : FunctionPass(ID), Impl(InsertFreezeWhenUnfoldingSelect, T) { initializeJumpThreadingPass(*PassRegistry::getPassRegistry()); } @@ -154,7 +154,7 @@ namespace { AU.addPreserved<LazyValueInfoWrapperPass>(); AU.addPreserved<GlobalsAAWrapperPass>(); AU.addRequired<TargetLibraryInfoWrapperPass>(); - AU.addRequired<TargetTransformInfoWrapperPass>(); + AU.addRequired<TargetTransformInfoWrapperPass>(); } void releaseMemory() override { Impl.releaseMemory(); } @@ -174,12 +174,12 @@ INITIALIZE_PASS_END(JumpThreading, "jump-threading", "Jump Threading", false, false) // Public interface to the Jump Threading pass -FunctionPass *llvm::createJumpThreadingPass(bool InsertFr, int Threshold) { - return new JumpThreading(InsertFr, Threshold); +FunctionPass *llvm::createJumpThreadingPass(bool InsertFr, int Threshold) { + return new JumpThreading(InsertFr, Threshold); } -JumpThreadingPass::JumpThreadingPass(bool InsertFr, int T) { - InsertFreezeWhenUnfoldingSelect = JumpThreadingFreezeSelectCond | InsertFr; +JumpThreadingPass::JumpThreadingPass(bool InsertFr, int T) { + InsertFreezeWhenUnfoldingSelect = JumpThreadingFreezeSelectCond | InsertFr; DefaultBBDupThreshold = (T == -1) ? BBDuplicateThreshold : unsigned(T); } @@ -313,10 +313,10 @@ static void updatePredecessorProfileMetadata(PHINode *PN, BasicBlock *BB) { bool JumpThreading::runOnFunction(Function &F) { if (skipFunction(F)) return false; - auto TTI = &getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F); - // Jump Threading has no sense for the targets with divergent CF - if (TTI->hasBranchDivergence()) - return false; + auto TTI = &getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F); + // Jump Threading has no sense for the targets with divergent CF + if (TTI->hasBranchDivergence()) + return false; auto TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F); auto DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree(); auto LVI = &getAnalysis<LazyValueInfoWrapperPass>().getLVI(); @@ -341,10 +341,10 @@ bool JumpThreading::runOnFunction(Function &F) { PreservedAnalyses JumpThreadingPass::run(Function &F, FunctionAnalysisManager &AM) { - auto &TTI = AM.getResult<TargetIRAnalysis>(F); - // Jump Threading has no sense for the targets with divergent CF - if (TTI.hasBranchDivergence()) - return PreservedAnalyses::all(); + auto &TTI = AM.getResult<TargetIRAnalysis>(F); + // Jump Threading has no sense for the targets with divergent CF + if (TTI.hasBranchDivergence()) + return PreservedAnalyses::all(); auto &TLI = AM.getResult<TargetLibraryAnalysis>(F); auto &DT = AM.getResult<DominatorTreeAnalysis>(F); auto &LVI = AM.getResult<LazyValueAnalysis>(F); @@ -362,11 +362,11 @@ PreservedAnalyses JumpThreadingPass::run(Function &F, bool Changed = runImpl(F, &TLI, &LVI, &AA, &DTU, F.hasProfileData(), std::move(BFI), std::move(BPI)); - if (PrintLVIAfterJumpThreading) { - dbgs() << "LVI for function '" << F.getName() << "':\n"; - LVI.printLVI(F, DTU.getDomTree(), dbgs()); - } - + if (PrintLVIAfterJumpThreading) { + dbgs() << "LVI for function '" << F.getName() << "':\n"; + LVI.printLVI(F, DTU.getDomTree(), dbgs()); + } + if (!Changed) return PreservedAnalyses::all(); PreservedAnalyses PA; @@ -419,7 +419,7 @@ bool JumpThreadingPass::runImpl(Function &F, TargetLibraryInfo *TLI_, Unreachable.insert(&BB); if (!ThreadAcrossLoopHeaders) - findLoopHeaders(F); + findLoopHeaders(F); bool EverChanged = false; bool Changed; @@ -428,7 +428,7 @@ bool JumpThreadingPass::runImpl(Function &F, TargetLibraryInfo *TLI_, for (auto &BB : F) { if (Unreachable.count(&BB)) continue; - while (processBlock(&BB)) // Thread all of the branches we can over BB. + while (processBlock(&BB)) // Thread all of the branches we can over BB. Changed = true; // Jump threading may have introduced redundant debug values into BB @@ -443,7 +443,7 @@ bool JumpThreadingPass::runImpl(Function &F, TargetLibraryInfo *TLI_, continue; if (pred_empty(&BB)) { - // When processBlock makes BB unreachable it doesn't bother to fix up + // When processBlock makes BB unreachable it doesn't bother to fix up // the instructions in it. We must remove BB to prevent invalid IR. LLVM_DEBUG(dbgs() << " JT: Deleting dead block '" << BB.getName() << "' with terminator: " << *BB.getTerminator() @@ -455,7 +455,7 @@ bool JumpThreadingPass::runImpl(Function &F, TargetLibraryInfo *TLI_, continue; } - // processBlock doesn't thread BBs with unconditional TIs. However, if BB + // processBlock doesn't thread BBs with unconditional TIs. However, if BB // is "almost empty", we attempt to merge BB with its sole successor. auto *BI = dyn_cast<BranchInst>(BB.getTerminator()); if (BI && BI->isUnconditional()) { @@ -489,7 +489,7 @@ bool JumpThreadingPass::runImpl(Function &F, TargetLibraryInfo *TLI_, // at the end of block. RAUW unconditionally replaces all uses // including the guards/assumes themselves and the uses before the // guard/assume. -static void replaceFoldableUses(Instruction *Cond, Value *ToVal) { +static void replaceFoldableUses(Instruction *Cond, Value *ToVal) { assert(Cond->getType() == ToVal->getType()); auto *BB = Cond->getParent(); // We can unconditionally replace all uses in non-local blocks (i.e. uses @@ -553,18 +553,18 @@ static unsigned getJumpThreadDuplicationCost(BasicBlock *BB, // Debugger intrinsics don't incur code size. if (isa<DbgInfoIntrinsic>(I)) continue; - // Pseudo-probes don't incur code size. - if (isa<PseudoProbeInst>(I)) - continue; - + // Pseudo-probes don't incur code size. + if (isa<PseudoProbeInst>(I)) + continue; + // If this is a pointer->pointer bitcast, it is free. if (isa<BitCastInst>(I) && I->getType()->isPointerTy()) continue; - // Freeze instruction is free, too. - if (isa<FreezeInst>(I)) - continue; - + // Freeze instruction is free, too. + if (isa<FreezeInst>(I)) + continue; + // Bail out if this instruction gives back a token type, it is not possible // to duplicate it if it is used outside this BB. if (I->getType()->isTokenTy() && I->isUsedOutsideOfBlock(BB)) @@ -592,7 +592,7 @@ static unsigned getJumpThreadDuplicationCost(BasicBlock *BB, return Size > Bonus ? Size - Bonus : 0; } -/// findLoopHeaders - We do not want jump threading to turn proper loop +/// 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 @@ -606,7 +606,7 @@ static unsigned getJumpThreadDuplicationCost(BasicBlock *BB, /// 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. -void JumpThreadingPass::findLoopHeaders(Function &F) { +void JumpThreadingPass::findLoopHeaders(Function &F) { SmallVector<std::pair<const BasicBlock*,const BasicBlock*>, 32> Edges; FindFunctionBackedges(F, Edges); @@ -633,13 +633,13 @@ static Constant *getKnownConstant(Value *Val, ConstantPreference Preference) { return dyn_cast<ConstantInt>(Val); } -/// computeValueKnownInPredecessors - Given a basic block BB and a value V, see +/// 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. -bool JumpThreadingPass::computeValueKnownInPredecessorsImpl( +bool JumpThreadingPass::computeValueKnownInPredecessorsImpl( Value *V, BasicBlock *BB, PredValueInfo &Result, ConstantPreference Preference, DenseSet<Value *> &RecursionSet, Instruction *CxtI) { @@ -704,10 +704,10 @@ bool JumpThreadingPass::computeValueKnownInPredecessorsImpl( return !Result.empty(); } - // Handle Cast instructions. + // Handle Cast instructions. if (CastInst *CI = dyn_cast<CastInst>(I)) { Value *Source = CI->getOperand(0); - computeValueKnownInPredecessorsImpl(Source, BB, Result, Preference, + computeValueKnownInPredecessorsImpl(Source, BB, Result, Preference, RecursionSet, CxtI); if (Result.empty()) return false; @@ -719,18 +719,18 @@ bool JumpThreadingPass::computeValueKnownInPredecessorsImpl( return true; } - if (FreezeInst *FI = dyn_cast<FreezeInst>(I)) { - Value *Source = FI->getOperand(0); - computeValueKnownInPredecessorsImpl(Source, BB, Result, Preference, - RecursionSet, CxtI); - - erase_if(Result, [](auto &Pair) { - return !isGuaranteedNotToBeUndefOrPoison(Pair.first); - }); - - return !Result.empty(); - } - + if (FreezeInst *FI = dyn_cast<FreezeInst>(I)) { + Value *Source = FI->getOperand(0); + computeValueKnownInPredecessorsImpl(Source, BB, Result, Preference, + RecursionSet, CxtI); + + erase_if(Result, [](auto &Pair) { + return !isGuaranteedNotToBeUndefOrPoison(Pair.first); + }); + + return !Result.empty(); + } + // Handle some boolean conditions. if (I->getType()->getPrimitiveSizeInBits() == 1) { assert(Preference == WantInteger && "One-bit non-integer type?"); @@ -740,9 +740,9 @@ bool JumpThreadingPass::computeValueKnownInPredecessorsImpl( I->getOpcode() == Instruction::And) { PredValueInfoTy LHSVals, RHSVals; - computeValueKnownInPredecessorsImpl(I->getOperand(0), BB, LHSVals, + computeValueKnownInPredecessorsImpl(I->getOperand(0), BB, LHSVals, WantInteger, RecursionSet, CxtI); - computeValueKnownInPredecessorsImpl(I->getOperand(1), BB, RHSVals, + computeValueKnownInPredecessorsImpl(I->getOperand(1), BB, RHSVals, WantInteger, RecursionSet, CxtI); if (LHSVals.empty() && RHSVals.empty()) @@ -778,7 +778,7 @@ bool JumpThreadingPass::computeValueKnownInPredecessorsImpl( if (I->getOpcode() == Instruction::Xor && isa<ConstantInt>(I->getOperand(1)) && cast<ConstantInt>(I->getOperand(1))->isOne()) { - computeValueKnownInPredecessorsImpl(I->getOperand(0), BB, Result, + computeValueKnownInPredecessorsImpl(I->getOperand(0), BB, Result, WantInteger, RecursionSet, CxtI); if (Result.empty()) return false; @@ -796,7 +796,7 @@ bool JumpThreadingPass::computeValueKnownInPredecessorsImpl( && "A binary operator creating a block address?"); if (ConstantInt *CI = dyn_cast<ConstantInt>(BO->getOperand(1))) { PredValueInfoTy LHSVals; - computeValueKnownInPredecessorsImpl(BO->getOperand(0), BB, LHSVals, + computeValueKnownInPredecessorsImpl(BO->getOperand(0), BB, LHSVals, WantInteger, RecursionSet, CxtI); // Try to use constant folding to simplify the binary operator. @@ -930,7 +930,7 @@ bool JumpThreadingPass::computeValueKnownInPredecessorsImpl( // Try to find a constant value for the LHS of a comparison, // and evaluate it statically if we can. PredValueInfoTy LHSVals; - computeValueKnownInPredecessorsImpl(I->getOperand(0), BB, LHSVals, + computeValueKnownInPredecessorsImpl(I->getOperand(0), BB, LHSVals, WantInteger, RecursionSet, CxtI); for (const auto &LHSVal : LHSVals) { @@ -951,7 +951,7 @@ bool JumpThreadingPass::computeValueKnownInPredecessorsImpl( Constant *FalseVal = getKnownConstant(SI->getFalseValue(), Preference); PredValueInfoTy Conds; if ((TrueVal || FalseVal) && - computeValueKnownInPredecessorsImpl(SI->getCondition(), BB, Conds, + computeValueKnownInPredecessorsImpl(SI->getCondition(), BB, Conds, WantInteger, RecursionSet, CxtI)) { for (auto &C : Conds) { Constant *Cond = C.first; @@ -979,8 +979,8 @@ bool JumpThreadingPass::computeValueKnownInPredecessorsImpl( } // If all else fails, see if LVI can figure out a constant value for us. - assert(CxtI->getParent() == BB && "CxtI should be in BB"); - Constant *CI = LVI->getConstant(V, CxtI); + assert(CxtI->getParent() == BB && "CxtI should be in BB"); + Constant *CI = LVI->getConstant(V, CxtI); if (Constant *KC = getKnownConstant(CI, Preference)) { for (BasicBlock *Pred : predecessors(BB)) Result.emplace_back(KC, Pred); @@ -994,7 +994,7 @@ bool JumpThreadingPass::computeValueKnownInPredecessorsImpl( /// /// Since we can pick an arbitrary destination, we pick the successor with the /// fewest predecessors. This should reduce the in-degree of the others. -static unsigned getBestDestForJumpOnUndef(BasicBlock *BB) { +static unsigned getBestDestForJumpOnUndef(BasicBlock *BB) { Instruction *BBTerm = BB->getTerminator(); unsigned MinSucc = 0; BasicBlock *TestBB = BBTerm->getSuccessor(MinSucc); @@ -1022,9 +1022,9 @@ static bool hasAddressTakenAndUsed(BasicBlock *BB) { return !BA->use_empty(); } -/// processBlock - If there are any predecessors whose control can be threaded +/// processBlock - If there are any predecessors whose control can be threaded /// through to a successor, transform them now. -bool JumpThreadingPass::processBlock(BasicBlock *BB) { +bool JumpThreadingPass::processBlock(BasicBlock *BB) { // If the block is trivially dead, just return and let the caller nuke it. // This simplifies other transformations. if (DTU->isBBPendingDeletion(BB) || @@ -1035,14 +1035,14 @@ bool JumpThreadingPass::processBlock(BasicBlock *BB) { // successor, merge the blocks. This encourages recursive jump threading // because now the condition in this block can be threaded through // predecessors of our predecessor block. - if (maybeMergeBasicBlockIntoOnlyPred(BB)) + if (maybeMergeBasicBlockIntoOnlyPred(BB)) return true; - if (tryToUnfoldSelectInCurrBB(BB)) + if (tryToUnfoldSelectInCurrBB(BB)) return true; // Look if we can propagate guards to predecessors. - if (HasGuards && processGuards(BB)) + if (HasGuards && processGuards(BB)) return true; // What kind of constant we're looking for. @@ -1067,9 +1067,9 @@ bool JumpThreadingPass::processBlock(BasicBlock *BB) { return false; // Must be an invoke or callbr. } - // Keep track if we constant folded the condition in this invocation. - bool ConstantFolded = false; - + // Keep track if we constant folded the condition in this invocation. + bool ConstantFolded = false; + // Run constant folding to see if we can reduce the condition to a simple // constant. if (Instruction *I = dyn_cast<Instruction>(Condition)) { @@ -1080,16 +1080,16 @@ bool JumpThreadingPass::processBlock(BasicBlock *BB) { if (isInstructionTriviallyDead(I, TLI)) I->eraseFromParent(); Condition = SimpleVal; - ConstantFolded = true; + ConstantFolded = true; } } - // If the terminator is branching on an undef or freeze undef, we can pick any - // of the successors to branch to. Let getBestDestForJumpOnUndef decide. - auto *FI = dyn_cast<FreezeInst>(Condition); - if (isa<UndefValue>(Condition) || - (FI && isa<UndefValue>(FI->getOperand(0)) && FI->hasOneUse())) { - unsigned BestSucc = getBestDestForJumpOnUndef(BB); + // If the terminator is branching on an undef or freeze undef, we can pick any + // of the successors to branch to. Let getBestDestForJumpOnUndef decide. + auto *FI = dyn_cast<FreezeInst>(Condition); + if (isa<UndefValue>(Condition) || + (FI && isa<UndefValue>(FI->getOperand(0)) && FI->hasOneUse())) { + unsigned BestSucc = getBestDestForJumpOnUndef(BB); std::vector<DominatorTree::UpdateType> Updates; // Fold the branch/switch. @@ -1107,8 +1107,8 @@ bool JumpThreadingPass::processBlock(BasicBlock *BB) { BranchInst::Create(BBTerm->getSuccessor(BestSucc), BBTerm); BBTerm->eraseFromParent(); DTU->applyUpdatesPermissive(Updates); - if (FI) - FI->eraseFromParent(); + if (FI) + FI->eraseFromParent(); return true; } @@ -1121,8 +1121,8 @@ bool JumpThreadingPass::processBlock(BasicBlock *BB) { << '\n'); ++NumFolds; ConstantFoldTerminator(BB, true, nullptr, DTU); - if (HasProfileData) - BPI->eraseBlock(BB); + if (HasProfileData) + BPI->eraseBlock(BB); return true; } @@ -1131,9 +1131,9 @@ bool JumpThreadingPass::processBlock(BasicBlock *BB) { // All the rest of our checks depend on the condition being an instruction. if (!CondInst) { // FIXME: Unify this with code below. - if (processThreadableEdges(Condition, BB, Preference, Terminator)) + if (processThreadableEdges(Condition, BB, Preference, Terminator)) return true; - return ConstantFolded; + return ConstantFolded; } if (CmpInst *CondCmp = dyn_cast<CmpInst>(CondInst)) { @@ -1174,24 +1174,24 @@ bool JumpThreadingPass::processBlock(BasicBlock *BB) { auto *CI = Ret == LazyValueInfo::True ? ConstantInt::getTrue(CondCmp->getType()) : ConstantInt::getFalse(CondCmp->getType()); - replaceFoldableUses(CondCmp, CI); + replaceFoldableUses(CondCmp, CI); } DTU->applyUpdatesPermissive( {{DominatorTree::Delete, BB, ToRemoveSucc}}); - if (HasProfileData) - BPI->eraseBlock(BB); + if (HasProfileData) + BPI->eraseBlock(BB); return true; } // We did not manage to simplify this branch, try to see whether // CondCmp depends on a known phi-select pattern. - if (tryToUnfoldSelect(CondCmp, BB)) + if (tryToUnfoldSelect(CondCmp, BB)) return true; } } if (SwitchInst *SI = dyn_cast<SwitchInst>(BB->getTerminator())) - if (tryToUnfoldSelect(SI, BB)) + if (tryToUnfoldSelect(SI, BB)) return true; // Check for some cases that are worth simplifying. Right now we want to look @@ -1199,11 +1199,11 @@ bool JumpThreadingPass::processBlock(BasicBlock *BB) { // we see one, check to see if it's partially redundant. If so, insert a PHI // which can then be used to thread the values. Value *SimplifyValue = CondInst; - - if (auto *FI = dyn_cast<FreezeInst>(SimplifyValue)) - // Look into freeze's operand - SimplifyValue = FI->getOperand(0); - + + if (auto *FI = dyn_cast<FreezeInst>(SimplifyValue)) + // Look into freeze's operand + SimplifyValue = FI->getOperand(0); + if (CmpInst *CondCmp = dyn_cast<CmpInst>(SimplifyValue)) if (isa<Constant>(CondCmp->getOperand(1))) SimplifyValue = CondCmp->getOperand(0); @@ -1211,7 +1211,7 @@ bool JumpThreadingPass::processBlock(BasicBlock *BB) { // TODO: There are other places where load PRE would be profitable, such as // more complex comparisons. if (LoadInst *LoadI = dyn_cast<LoadInst>(SimplifyValue)) - if (simplifyPartiallyRedundantLoad(LoadI)) + if (simplifyPartiallyRedundantLoad(LoadI)) return true; // Before threading, try to propagate profile data backwards: @@ -1222,32 +1222,32 @@ bool JumpThreadingPass::processBlock(BasicBlock *BB) { // Handle a variety of cases where we are branching on something derived from // a PHI node in the current block. If we can prove that any predecessors // compute a predictable value based on a PHI node, thread those predecessors. - if (processThreadableEdges(CondInst, BB, Preference, Terminator)) + if (processThreadableEdges(CondInst, BB, Preference, Terminator)) return true; - // If this is an otherwise-unfoldable branch on a phi node or freeze(phi) in - // the current block, see if we can simplify. - PHINode *PN = dyn_cast<PHINode>( - isa<FreezeInst>(CondInst) ? cast<FreezeInst>(CondInst)->getOperand(0) - : CondInst); - - if (PN && PN->getParent() == BB && isa<BranchInst>(BB->getTerminator())) - return processBranchOnPHI(PN); + // If this is an otherwise-unfoldable branch on a phi node or freeze(phi) in + // the current block, see if we can simplify. + PHINode *PN = dyn_cast<PHINode>( + isa<FreezeInst>(CondInst) ? cast<FreezeInst>(CondInst)->getOperand(0) + : CondInst); + if (PN && PN->getParent() == BB && isa<BranchInst>(BB->getTerminator())) + return processBranchOnPHI(PN); + // If this is an otherwise-unfoldable branch on a XOR, see if we can simplify. if (CondInst->getOpcode() == Instruction::Xor && CondInst->getParent() == BB && isa<BranchInst>(BB->getTerminator())) - return processBranchOnXOR(cast<BinaryOperator>(CondInst)); + return processBranchOnXOR(cast<BinaryOperator>(CondInst)); // Search for a stronger dominating condition that can be used to simplify a // conditional branch leaving BB. - if (processImpliedCondition(BB)) + if (processImpliedCondition(BB)) return true; return false; } -bool JumpThreadingPass::processImpliedCondition(BasicBlock *BB) { +bool JumpThreadingPass::processImpliedCondition(BasicBlock *BB) { auto *BI = dyn_cast<BranchInst>(BB->getTerminator()); if (!BI || !BI->isConditional()) return false; @@ -1277,8 +1277,8 @@ bool JumpThreadingPass::processImpliedCondition(BasicBlock *BB) { UncondBI->setDebugLoc(BI->getDebugLoc()); BI->eraseFromParent(); DTU->applyUpdatesPermissive({{DominatorTree::Delete, BB, RemoveSucc}}); - if (HasProfileData) - BPI->eraseBlock(BB); + if (HasProfileData) + BPI->eraseBlock(BB); return true; } CurrentBB = CurrentPred; @@ -1296,11 +1296,11 @@ static bool isOpDefinedInBlock(Value *Op, BasicBlock *BB) { return false; } -/// simplifyPartiallyRedundantLoad - If LoadI is an obviously partially +/// 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. -bool JumpThreadingPass::simplifyPartiallyRedundantLoad(LoadInst *LoadI) { +bool JumpThreadingPass::simplifyPartiallyRedundantLoad(LoadInst *LoadI) { // Don't hack volatile and ordered loads. if (!LoadI->isUnordered()) return false; @@ -1470,7 +1470,7 @@ bool JumpThreadingPass::simplifyPartiallyRedundantLoad(LoadInst *LoadI) { } // Split them out to their own block. - UnavailablePred = splitBlockPreds(LoadBB, PredsToSplit, "thread-pre-split"); + UnavailablePred = splitBlockPreds(LoadBB, PredsToSplit, "thread-pre-split"); } // If the value isn't available in all predecessors, then there will be @@ -1534,11 +1534,11 @@ bool JumpThreadingPass::simplifyPartiallyRedundantLoad(LoadInst *LoadI) { return true; } -/// findMostPopularDest - The specified list contains multiple possible +/// findMostPopularDest - The specified list contains multiple possible /// threadable destinations. Pick the one that occurs the most frequently in /// the list. static BasicBlock * -findMostPopularDest(BasicBlock *BB, +findMostPopularDest(BasicBlock *BB, const SmallVectorImpl<std::pair<BasicBlock *, BasicBlock *>> &PredToDestList) { assert(!PredToDestList.empty()); @@ -1573,7 +1573,7 @@ findMostPopularDest(BasicBlock *BB, // Try to evaluate the value of V when the control flows from PredPredBB to // BB->getSinglePredecessor() and then on to BB. -Constant *JumpThreadingPass::evaluateOnPredecessorEdge(BasicBlock *BB, +Constant *JumpThreadingPass::evaluateOnPredecessorEdge(BasicBlock *BB, BasicBlock *PredPredBB, Value *V) { BasicBlock *PredBB = BB->getSinglePredecessor(); @@ -1600,9 +1600,9 @@ Constant *JumpThreadingPass::evaluateOnPredecessorEdge(BasicBlock *BB, if (CmpInst *CondCmp = dyn_cast<CmpInst>(V)) { if (CondCmp->getParent() == BB) { Constant *Op0 = - evaluateOnPredecessorEdge(BB, PredPredBB, CondCmp->getOperand(0)); + evaluateOnPredecessorEdge(BB, PredPredBB, CondCmp->getOperand(0)); Constant *Op1 = - evaluateOnPredecessorEdge(BB, PredPredBB, CondCmp->getOperand(1)); + evaluateOnPredecessorEdge(BB, PredPredBB, CondCmp->getOperand(1)); if (Op0 && Op1) { return ConstantExpr::getCompare(CondCmp->getPredicate(), Op0, Op1); } @@ -1613,7 +1613,7 @@ Constant *JumpThreadingPass::evaluateOnPredecessorEdge(BasicBlock *BB, return nullptr; } -bool JumpThreadingPass::processThreadableEdges(Value *Cond, BasicBlock *BB, +bool JumpThreadingPass::processThreadableEdges(Value *Cond, BasicBlock *BB, ConstantPreference Preference, Instruction *CxtI) { // If threading this would thread across a loop header, don't even try to @@ -1622,15 +1622,15 @@ bool JumpThreadingPass::processThreadableEdges(Value *Cond, BasicBlock *BB, return false; PredValueInfoTy PredValues; - if (!computeValueKnownInPredecessors(Cond, BB, PredValues, Preference, + if (!computeValueKnownInPredecessors(Cond, BB, PredValues, Preference, CxtI)) { // We don't have known values in predecessors. See if we can thread through // BB and its sole predecessor. - return maybethreadThroughTwoBasicBlocks(BB, Cond); + return maybethreadThroughTwoBasicBlocks(BB, Cond); } assert(!PredValues.empty() && - "computeValueKnownInPredecessors returned true with no values"); + "computeValueKnownInPredecessors returned true with no values"); LLVM_DEBUG(dbgs() << "IN BB: " << *BB; for (const auto &PredValue : PredValues) { @@ -1722,8 +1722,8 @@ bool JumpThreadingPass::processThreadableEdges(Value *Cond, BasicBlock *BB, BranchInst::Create(OnlyDest, Term); Term->eraseFromParent(); DTU->applyUpdatesPermissive(Updates); - if (HasProfileData) - BPI->eraseBlock(BB); + if (HasProfileData) + BPI->eraseBlock(BB); // If the condition is now dead due to the removal of the old terminator, // erase it. @@ -1739,7 +1739,7 @@ bool JumpThreadingPass::processThreadableEdges(Value *Cond, BasicBlock *BB, // guard/assume. else if (OnlyVal && OnlyVal != MultipleVal && CondInst->getParent() == BB) - replaceFoldableUses(CondInst, OnlyVal); + replaceFoldableUses(CondInst, OnlyVal); } return true; } @@ -1752,18 +1752,18 @@ bool JumpThreadingPass::processThreadableEdges(Value *Cond, BasicBlock *BB, BasicBlock *MostPopularDest = OnlyDest; if (MostPopularDest == MultipleDestSentinel) { - // Remove any loop headers from the Dest list, threadEdge conservatively + // Remove any loop headers from the Dest list, threadEdge conservatively // won't process them, but we might have other destination that are eligible // and we still want to process. erase_if(PredToDestList, [&](const std::pair<BasicBlock *, BasicBlock *> &PredToDest) { - return LoopHeaders.contains(PredToDest.second); + return LoopHeaders.contains(PredToDest.second); }); if (PredToDestList.empty()) return false; - MostPopularDest = findMostPopularDest(BB, PredToDestList); + MostPopularDest = findMostPopularDest(BB, PredToDestList); } // Now that we know what the most popular destination is, factor all @@ -1785,16 +1785,16 @@ bool JumpThreadingPass::processThreadableEdges(Value *Cond, BasicBlock *BB, // the destination that these predecessors should get to. if (!MostPopularDest) MostPopularDest = BB->getTerminator()-> - getSuccessor(getBestDestForJumpOnUndef(BB)); + getSuccessor(getBestDestForJumpOnUndef(BB)); // Ok, try to thread it! - return tryThreadEdge(BB, PredsToFactor, MostPopularDest); + return tryThreadEdge(BB, PredsToFactor, MostPopularDest); } -/// 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. -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. +bool JumpThreadingPass::processBranchOnPHI(PHINode *PN) { BasicBlock *BB = PN->getParent(); // TODO: We could make use of this to do it once for blocks with common PHI @@ -1806,16 +1806,16 @@ bool JumpThreadingPass::processBranchOnPHI(PHINode *PN) { // *duplicate* the conditional branch into that block in order to further // encourage jump threading and to eliminate cases where we have branch on a // phi of an icmp (branch on icmp is much better). - // This is still beneficial when a frozen phi is used as the branch condition - // because it allows CodeGenPrepare to further canonicalize br(freeze(icmp)) - // to br(icmp(freeze ...)). + // This is still beneficial when a frozen phi is used as the branch condition + // because it allows CodeGenPrepare to further canonicalize br(freeze(icmp)) + // to br(icmp(freeze ...)). for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { BasicBlock *PredBB = PN->getIncomingBlock(i); if (BranchInst *PredBr = dyn_cast<BranchInst>(PredBB->getTerminator())) if (PredBr->isUnconditional()) { PredBBs[0] = PredBB; // Try to duplicate BB into PredBB. - if (duplicateCondBranchOnPHIIntoPred(BB, PredBBs)) + if (duplicateCondBranchOnPHIIntoPred(BB, PredBBs)) return true; } } @@ -1823,10 +1823,10 @@ bool JumpThreadingPass::processBranchOnPHI(PHINode *PN) { return false; } -/// processBranchOnXOR - We have an otherwise unthreadable conditional branch on +/// 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. -bool JumpThreadingPass::processBranchOnXOR(BinaryOperator *BO) { +bool JumpThreadingPass::processBranchOnXOR(BinaryOperator *BO) { BasicBlock *BB = BO->getParent(); // If either the LHS or RHS of the xor is a constant, don't do this @@ -1864,17 +1864,17 @@ bool JumpThreadingPass::processBranchOnXOR(BinaryOperator *BO) { PredValueInfoTy XorOpValues; bool isLHS = true; - if (!computeValueKnownInPredecessors(BO->getOperand(0), BB, XorOpValues, + if (!computeValueKnownInPredecessors(BO->getOperand(0), BB, XorOpValues, WantInteger, BO)) { assert(XorOpValues.empty()); - if (!computeValueKnownInPredecessors(BO->getOperand(1), BB, XorOpValues, + if (!computeValueKnownInPredecessors(BO->getOperand(1), BB, XorOpValues, WantInteger, BO)) return false; isLHS = false; } assert(!XorOpValues.empty() && - "computeValueKnownInPredecessors returned true with no values"); + "computeValueKnownInPredecessors returned true with no values"); // Scan the information to see which is most popular: true or false. The // predecessors can be of the set true, false, or undef. @@ -1935,13 +1935,13 @@ bool JumpThreadingPass::processBranchOnXOR(BinaryOperator *BO) { return false; // Try to duplicate BB into PredBB. - return duplicateCondBranchOnPHIIntoPred(BB, BlocksToFoldInto); + return duplicateCondBranchOnPHIIntoPred(BB, BlocksToFoldInto); } -/// addPHINodeEntriesForMappedBlock - We're adding 'NewPred' as a new +/// addPHINodeEntriesForMappedBlock - We're adding 'NewPred' as a new /// predecessor to the PHIBB block. If it has PHI nodes, add entries for /// NewPred using the entries from OldPred (suitably mapped). -static void addPHINodeEntriesForMappedBlock(BasicBlock *PHIBB, +static void addPHINodeEntriesForMappedBlock(BasicBlock *PHIBB, BasicBlock *OldPred, BasicBlock *NewPred, DenseMap<Instruction*, Value*> &ValueMap) { @@ -1962,7 +1962,7 @@ static void addPHINodeEntriesForMappedBlock(BasicBlock *PHIBB, } /// Merge basic block BB into its sole predecessor if possible. -bool JumpThreadingPass::maybeMergeBasicBlockIntoOnlyPred(BasicBlock *BB) { +bool JumpThreadingPass::maybeMergeBasicBlockIntoOnlyPred(BasicBlock *BB) { BasicBlock *SinglePred = BB->getSinglePredecessor(); if (!SinglePred) return false; @@ -2013,7 +2013,7 @@ bool JumpThreadingPass::maybeMergeBasicBlockIntoOnlyPred(BasicBlock *BB) { /// Update the SSA form. NewBB contains instructions that are copied from BB. /// ValueMapping maps old values in BB to new ones in NewBB. -void JumpThreadingPass::updateSSA( +void JumpThreadingPass::updateSSA( BasicBlock *BB, BasicBlock *NewBB, DenseMap<Instruction *, Value *> &ValueMapping) { // If there were values defined in BB that are used outside the block, then we @@ -2059,7 +2059,7 @@ void JumpThreadingPass::updateSSA( /// arguments that come from PredBB. Return the map from the variables in the /// source basic block to the variables in the newly created basic block. DenseMap<Instruction *, Value *> -JumpThreadingPass::cloneInstructions(BasicBlock::iterator BI, +JumpThreadingPass::cloneInstructions(BasicBlock::iterator BI, BasicBlock::iterator BE, BasicBlock *NewBB, BasicBlock *PredBB) { // We are going to have to map operands from the source basic block to the new @@ -2076,15 +2076,15 @@ JumpThreadingPass::cloneInstructions(BasicBlock::iterator BI, ValueMapping[PN] = NewPN; } - // Clone noalias scope declarations in the threaded block. When threading a - // loop exit, we would otherwise end up with two idential scope declarations - // visible at the same time. - SmallVector<MDNode *> NoAliasScopes; - DenseMap<MDNode *, MDNode *> ClonedScopes; - LLVMContext &Context = PredBB->getContext(); - identifyNoAliasScopesToClone(BI, BE, NoAliasScopes); - cloneNoAliasScopes(NoAliasScopes, ClonedScopes, "thread", Context); - + // Clone noalias scope declarations in the threaded block. When threading a + // loop exit, we would otherwise end up with two idential scope declarations + // visible at the same time. + SmallVector<MDNode *> NoAliasScopes; + DenseMap<MDNode *, MDNode *> ClonedScopes; + LLVMContext &Context = PredBB->getContext(); + identifyNoAliasScopesToClone(BI, BE, NoAliasScopes); + cloneNoAliasScopes(NoAliasScopes, ClonedScopes, "thread", Context); + // Clone the non-phi instructions of the source basic block into NewBB, // keeping track of the mapping and using it to remap operands in the cloned // instructions. @@ -2093,7 +2093,7 @@ JumpThreadingPass::cloneInstructions(BasicBlock::iterator BI, New->setName(BI->getName()); NewBB->getInstList().push_back(New); ValueMapping[&*BI] = New; - adaptNoAliasScopes(New, ClonedScopes, Context); + adaptNoAliasScopes(New, ClonedScopes, Context); // Remap operands to patch up intra-block references. for (unsigned i = 0, e = New->getNumOperands(); i != e; ++i) @@ -2108,7 +2108,7 @@ JumpThreadingPass::cloneInstructions(BasicBlock::iterator BI, } /// Attempt to thread through two successive basic blocks. -bool JumpThreadingPass::maybethreadThroughTwoBasicBlocks(BasicBlock *BB, +bool JumpThreadingPass::maybethreadThroughTwoBasicBlocks(BasicBlock *BB, Value *Cond) { // Consider: // @@ -2177,7 +2177,7 @@ bool JumpThreadingPass::maybethreadThroughTwoBasicBlocks(BasicBlock *BB, BasicBlock *OnePred = nullptr; for (BasicBlock *P : predecessors(PredBB)) { if (ConstantInt *CI = dyn_cast_or_null<ConstantInt>( - evaluateOnPredecessorEdge(BB, P, Cond))) { + evaluateOnPredecessorEdge(BB, P, Cond))) { if (CI->isZero()) { ZeroCount++; ZeroPred = P; @@ -2208,7 +2208,7 @@ bool JumpThreadingPass::maybethreadThroughTwoBasicBlocks(BasicBlock *BB, } // If threading this would thread across a loop header, don't thread the edge. - // See the comments above findLoopHeaders for justifications and caveats. + // See the comments above findLoopHeaders for justifications and caveats. if (LoopHeaders.count(BB) || LoopHeaders.count(SuccBB)) { LLVM_DEBUG({ bool BBIsHeader = LoopHeaders.count(BB); @@ -2241,11 +2241,11 @@ bool JumpThreadingPass::maybethreadThroughTwoBasicBlocks(BasicBlock *BB, } // Now we are ready to duplicate PredBB. - threadThroughTwoBasicBlocks(PredPredBB, PredBB, BB, SuccBB); + threadThroughTwoBasicBlocks(PredPredBB, PredBB, BB, SuccBB); return true; } -void JumpThreadingPass::threadThroughTwoBasicBlocks(BasicBlock *PredPredBB, +void JumpThreadingPass::threadThroughTwoBasicBlocks(BasicBlock *PredPredBB, BasicBlock *PredBB, BasicBlock *BB, BasicBlock *SuccBB) { @@ -2271,12 +2271,12 @@ void JumpThreadingPass::threadThroughTwoBasicBlocks(BasicBlock *PredPredBB, // copy of the block 'NewBB'. If there are PHI nodes in PredBB, evaluate them // to account for entry from PredPredBB. DenseMap<Instruction *, Value *> ValueMapping = - cloneInstructions(PredBB->begin(), PredBB->end(), NewBB, PredPredBB); - - // Copy the edge probabilities from PredBB to NewBB. - if (HasProfileData) - BPI->copyEdgeProbabilities(PredBB, NewBB); + cloneInstructions(PredBB->begin(), PredBB->end(), NewBB, PredPredBB); + // Copy the edge probabilities from PredBB to NewBB. + if (HasProfileData) + BPI->copyEdgeProbabilities(PredBB, NewBB); + // Update the terminator of PredPredBB to jump to NewBB instead of PredBB. // This eliminates predecessors from PredPredBB, which requires us to simplify // any PHI nodes in PredBB. @@ -2287,9 +2287,9 @@ void JumpThreadingPass::threadThroughTwoBasicBlocks(BasicBlock *PredPredBB, PredPredTerm->setSuccessor(i, NewBB); } - addPHINodeEntriesForMappedBlock(PredBBBranch->getSuccessor(0), PredBB, NewBB, + addPHINodeEntriesForMappedBlock(PredBBBranch->getSuccessor(0), PredBB, NewBB, ValueMapping); - addPHINodeEntriesForMappedBlock(PredBBBranch->getSuccessor(1), PredBB, NewBB, + addPHINodeEntriesForMappedBlock(PredBBBranch->getSuccessor(1), PredBB, NewBB, ValueMapping); DTU->applyUpdatesPermissive( @@ -2298,7 +2298,7 @@ void JumpThreadingPass::threadThroughTwoBasicBlocks(BasicBlock *PredPredBB, {DominatorTree::Insert, PredPredBB, NewBB}, {DominatorTree::Delete, PredPredBB, PredBB}}); - updateSSA(PredBB, NewBB, ValueMapping); + updateSSA(PredBB, NewBB, ValueMapping); // Clean up things like PHI nodes with single operands, dead instructions, // etc. @@ -2307,11 +2307,11 @@ void JumpThreadingPass::threadThroughTwoBasicBlocks(BasicBlock *PredPredBB, SmallVector<BasicBlock *, 1> PredsToFactor; PredsToFactor.push_back(NewBB); - threadEdge(BB, PredsToFactor, SuccBB); + threadEdge(BB, PredsToFactor, SuccBB); } -/// tryThreadEdge - Thread an edge if it's safe and profitable to do so. -bool JumpThreadingPass::tryThreadEdge( +/// tryThreadEdge - Thread an edge if it's safe and profitable to do so. +bool JumpThreadingPass::tryThreadEdge( BasicBlock *BB, const SmallVectorImpl<BasicBlock *> &PredBBs, BasicBlock *SuccBB) { // If threading to the same block as we come from, we would infinite loop. @@ -2322,7 +2322,7 @@ bool JumpThreadingPass::tryThreadEdge( } // If threading this would thread across a loop header, don't thread the edge. - // See the comments above findLoopHeaders for justifications and caveats. + // See the comments above findLoopHeaders for justifications and caveats. if (LoopHeaders.count(BB) || LoopHeaders.count(SuccBB)) { LLVM_DEBUG({ bool BBIsHeader = LoopHeaders.count(BB); @@ -2343,14 +2343,14 @@ bool JumpThreadingPass::tryThreadEdge( return false; } - threadEdge(BB, PredBBs, SuccBB); + threadEdge(BB, PredBBs, SuccBB); return true; } -/// threadEdge - We have decided that it is safe and profitable to factor the +/// 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. -void JumpThreadingPass::threadEdge(BasicBlock *BB, +void JumpThreadingPass::threadEdge(BasicBlock *BB, const SmallVectorImpl<BasicBlock *> &PredBBs, BasicBlock *SuccBB) { assert(SuccBB != BB && "Don't create an infinite loop"); @@ -2365,7 +2365,7 @@ void JumpThreadingPass::threadEdge(BasicBlock *BB, else { LLVM_DEBUG(dbgs() << " Factoring out " << PredBBs.size() << " common predecessors.\n"); - PredBB = splitBlockPreds(BB, PredBBs, ".thr_comm"); + PredBB = splitBlockPreds(BB, PredBBs, ".thr_comm"); } // And finally, do it! @@ -2389,7 +2389,7 @@ void JumpThreadingPass::threadEdge(BasicBlock *BB, // Copy all the instructions from BB to NewBB except the terminator. DenseMap<Instruction *, Value *> ValueMapping = - cloneInstructions(BB->begin(), std::prev(BB->end()), NewBB, PredBB); + cloneInstructions(BB->begin(), std::prev(BB->end()), NewBB, PredBB); // We didn't copy the terminator from BB over to NewBB, because there is now // an unconditional jump to SuccBB. Insert the unconditional jump. @@ -2398,7 +2398,7 @@ void JumpThreadingPass::threadEdge(BasicBlock *BB, // Check to see if SuccBB has PHI nodes. If so, we need to add entries to the // PHI nodes for NewBB now. - addPHINodeEntriesForMappedBlock(SuccBB, BB, NewBB, ValueMapping); + addPHINodeEntriesForMappedBlock(SuccBB, BB, NewBB, ValueMapping); // Update the terminator of PredBB to jump to NewBB instead of BB. This // eliminates predecessors from BB, which requires us to simplify any PHI @@ -2415,7 +2415,7 @@ void JumpThreadingPass::threadEdge(BasicBlock *BB, {DominatorTree::Insert, PredBB, NewBB}, {DominatorTree::Delete, PredBB, BB}}); - updateSSA(BB, NewBB, ValueMapping); + updateSSA(BB, NewBB, ValueMapping); // At this point, the IR is fully up to date and consistent. Do a quick scan // over the new instructions and zap any that are constants or dead. This @@ -2423,7 +2423,7 @@ void JumpThreadingPass::threadEdge(BasicBlock *BB, SimplifyInstructionsInBlock(NewBB, TLI); // Update the edge weight from BB to SuccBB, which should be less than before. - updateBlockFreqAndEdgeWeight(PredBB, BB, NewBB, SuccBB); + updateBlockFreqAndEdgeWeight(PredBB, BB, NewBB, SuccBB); // Threaded an edge! ++NumThreads; @@ -2432,7 +2432,7 @@ void JumpThreadingPass::threadEdge(BasicBlock *BB, /// Create a new basic block that will be the predecessor of BB and successor of /// all blocks in Preds. When profile data is available, update the frequency of /// this new block. -BasicBlock *JumpThreadingPass::splitBlockPreds(BasicBlock *BB, +BasicBlock *JumpThreadingPass::splitBlockPreds(BasicBlock *BB, ArrayRef<BasicBlock *> Preds, const char *Suffix) { SmallVector<BasicBlock *, 2> NewBBs; @@ -2493,7 +2493,7 @@ bool JumpThreadingPass::doesBlockHaveProfileData(BasicBlock *BB) { /// Update the block frequency of BB and branch weight and the metadata on the /// edge BB->SuccBB. This is done by scaling the weight of BB->SuccBB by 1 - /// Freq(PredBB->BB) / Freq(BB->SuccBB). -void JumpThreadingPass::updateBlockFreqAndEdgeWeight(BasicBlock *PredBB, +void JumpThreadingPass::updateBlockFreqAndEdgeWeight(BasicBlock *PredBB, BasicBlock *BB, BasicBlock *NewBB, BasicBlock *SuccBB) { @@ -2585,18 +2585,18 @@ void JumpThreadingPass::updateBlockFreqAndEdgeWeight(BasicBlock *PredBB, } } -/// duplicateCondBranchOnPHIIntoPred - PredBB contains an unconditional branch +/// 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. -bool JumpThreadingPass::duplicateCondBranchOnPHIIntoPred( +bool JumpThreadingPass::duplicateCondBranchOnPHIIntoPred( BasicBlock *BB, const SmallVectorImpl<BasicBlock *> &PredBBs) { assert(!PredBBs.empty() && "Can't handle an empty set"); // If BB is a loop header, then duplicating this block outside the loop would // cause us to transform this into an irreducible loop, don't do this. - // See the comments above findLoopHeaders for justifications and caveats. + // See the comments above findLoopHeaders for justifications and caveats. if (LoopHeaders.count(BB)) { LLVM_DEBUG(dbgs() << " Not duplicating loop header '" << BB->getName() << "' into predecessor block '" << PredBBs[0]->getName() @@ -2620,7 +2620,7 @@ bool JumpThreadingPass::duplicateCondBranchOnPHIIntoPred( else { LLVM_DEBUG(dbgs() << " Factoring out " << PredBBs.size() << " common predecessors.\n"); - PredBB = splitBlockPreds(BB, PredBBs, ".thr_comm"); + PredBB = splitBlockPreds(BB, PredBBs, ".thr_comm"); } Updates.push_back({DominatorTree::Delete, PredBB, BB}); @@ -2692,12 +2692,12 @@ bool JumpThreadingPass::duplicateCondBranchOnPHIIntoPred( // Check to see if the targets of the branch had PHI nodes. If so, we need to // add entries to the PHI nodes for branch from PredBB now. BranchInst *BBBranch = cast<BranchInst>(BB->getTerminator()); - addPHINodeEntriesForMappedBlock(BBBranch->getSuccessor(0), BB, PredBB, + addPHINodeEntriesForMappedBlock(BBBranch->getSuccessor(0), BB, PredBB, ValueMapping); - addPHINodeEntriesForMappedBlock(BBBranch->getSuccessor(1), BB, PredBB, + addPHINodeEntriesForMappedBlock(BBBranch->getSuccessor(1), BB, PredBB, ValueMapping); - updateSSA(BB, PredBB, ValueMapping); + updateSSA(BB, PredBB, ValueMapping); // PredBB no longer jumps to BB, remove entries in the PHI node for the edge // that we nuked. @@ -2705,8 +2705,8 @@ bool JumpThreadingPass::duplicateCondBranchOnPHIIntoPred( // Remove the unconditional branch at the end of the PredBB block. OldPredBranch->eraseFromParent(); - if (HasProfileData) - BPI->copyEdgeProbabilities(BB, PredBB); + if (HasProfileData) + BPI->copyEdgeProbabilities(BB, PredBB); DTU->applyUpdatesPermissive(Updates); ++NumDupes; @@ -2718,7 +2718,7 @@ bool JumpThreadingPass::duplicateCondBranchOnPHIIntoPred( // a PHI node in BB. SI has no other use. // A new basic block, NewBB, is created and SI is converted to compare and // conditional branch. SI is erased from parent. -void JumpThreadingPass::unfoldSelectInstr(BasicBlock *Pred, BasicBlock *BB, +void JumpThreadingPass::unfoldSelectInstr(BasicBlock *Pred, BasicBlock *BB, SelectInst *SI, PHINode *SIUse, unsigned Idx) { // Expand the select. @@ -2753,7 +2753,7 @@ void JumpThreadingPass::unfoldSelectInstr(BasicBlock *Pred, BasicBlock *BB, Phi->addIncoming(Phi->getIncomingValueForBlock(Pred), NewBB); } -bool JumpThreadingPass::tryToUnfoldSelect(SwitchInst *SI, BasicBlock *BB) { +bool JumpThreadingPass::tryToUnfoldSelect(SwitchInst *SI, BasicBlock *BB) { PHINode *CondPHI = dyn_cast<PHINode>(SI->getCondition()); if (!CondPHI || CondPHI->getParent() != BB) @@ -2765,7 +2765,7 @@ bool JumpThreadingPass::tryToUnfoldSelect(SwitchInst *SI, BasicBlock *BB) { // The second and third condition can be potentially relaxed. Currently // the conditions help to simplify the code and allow us to reuse existing - // code, developed for tryToUnfoldSelect(CmpInst *, BasicBlock *) + // code, developed for tryToUnfoldSelect(CmpInst *, BasicBlock *) if (!PredSI || PredSI->getParent() != Pred || !PredSI->hasOneUse()) continue; @@ -2773,13 +2773,13 @@ bool JumpThreadingPass::tryToUnfoldSelect(SwitchInst *SI, BasicBlock *BB) { if (!PredTerm || !PredTerm->isUnconditional()) continue; - unfoldSelectInstr(Pred, BB, PredSI, CondPHI, I); + unfoldSelectInstr(Pred, BB, PredSI, CondPHI, I); return true; } return false; } -/// tryToUnfoldSelect - Look for blocks of the form +/// tryToUnfoldSelect - Look for blocks of the form /// bb1: /// %a = select /// br bb2 @@ -2791,7 +2791,7 @@ bool JumpThreadingPass::tryToUnfoldSelect(SwitchInst *SI, BasicBlock *BB) { /// /// 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. -bool JumpThreadingPass::tryToUnfoldSelect(CmpInst *CondCmp, BasicBlock *BB) { +bool JumpThreadingPass::tryToUnfoldSelect(CmpInst *CondCmp, BasicBlock *BB) { BranchInst *CondBr = dyn_cast<BranchInst>(BB->getTerminator()); PHINode *CondLHS = dyn_cast<PHINode>(CondCmp->getOperand(0)); Constant *CondRHS = cast<Constant>(CondCmp->getOperand(1)); @@ -2825,14 +2825,14 @@ bool JumpThreadingPass::tryToUnfoldSelect(CmpInst *CondCmp, BasicBlock *BB) { if ((LHSFolds != LazyValueInfo::Unknown || RHSFolds != LazyValueInfo::Unknown) && LHSFolds != RHSFolds) { - unfoldSelectInstr(Pred, BB, SI, CondLHS, I); + unfoldSelectInstr(Pred, BB, SI, CondLHS, I); return true; } } return false; } -/// tryToUnfoldSelectInCurrBB - Look for PHI/Select or PHI/CMP/Select in the +/// 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], ... @@ -2852,14 +2852,14 @@ bool JumpThreadingPass::tryToUnfoldSelect(CmpInst *CondCmp, BasicBlock *BB) { /// 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. -bool JumpThreadingPass::tryToUnfoldSelectInCurrBB(BasicBlock *BB) { - // This transform would reduce the quality of msan diagnostics. +bool JumpThreadingPass::tryToUnfoldSelectInCurrBB(BasicBlock *BB) { + // This transform would reduce the quality of msan diagnostics. // Disable this transform under MemorySanitizer. if (BB->getParent()->hasFnAttribute(Attribute::SanitizeMemory)) return false; // If threading this would thread across a loop header, don't thread the edge. - // See the comments above findLoopHeaders for justifications and caveats. + // See the comments above findLoopHeaders for justifications and caveats. if (LoopHeaders.count(BB)) return false; @@ -2902,12 +2902,12 @@ bool JumpThreadingPass::tryToUnfoldSelectInCurrBB(BasicBlock *BB) { if (!SI) continue; // Expand the select. - Value *Cond = SI->getCondition(); - if (InsertFreezeWhenUnfoldingSelect && - !isGuaranteedNotToBeUndefOrPoison(Cond, nullptr, SI, - &DTU->getDomTree())) - Cond = new FreezeInst(Cond, "cond.fr", SI); - Instruction *Term = SplitBlockAndInsertIfThen(Cond, SI, false); + Value *Cond = SI->getCondition(); + if (InsertFreezeWhenUnfoldingSelect && + !isGuaranteedNotToBeUndefOrPoison(Cond, nullptr, SI, + &DTU->getDomTree())) + Cond = new FreezeInst(Cond, "cond.fr", SI); + Instruction *Term = SplitBlockAndInsertIfThen(Cond, SI, false); BasicBlock *SplitBB = SI->getParent(); BasicBlock *NewBB = Term->getParent(); PHINode *NewPN = PHINode::Create(SI->getType(), 2, "", SI); @@ -2951,7 +2951,7 @@ bool JumpThreadingPass::tryToUnfoldSelectInCurrBB(BasicBlock *BB) { /// 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. -bool JumpThreadingPass::processGuards(BasicBlock *BB) { +bool JumpThreadingPass::processGuards(BasicBlock *BB) { using namespace PatternMatch; // We only want to deal with two predecessors. @@ -2976,7 +2976,7 @@ bool JumpThreadingPass::processGuards(BasicBlock *BB) { if (auto *BI = dyn_cast<BranchInst>(Parent->getTerminator())) for (auto &I : *BB) - if (isGuard(&I) && threadGuard(BB, cast<IntrinsicInst>(&I), BI)) + if (isGuard(&I) && threadGuard(BB, cast<IntrinsicInst>(&I), BI)) return true; return false; @@ -2985,7 +2985,7 @@ bool JumpThreadingPass::processGuards(BasicBlock *BB) { /// 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. -bool JumpThreadingPass::threadGuard(BasicBlock *BB, IntrinsicInst *Guard, +bool JumpThreadingPass::threadGuard(BasicBlock *BB, IntrinsicInst *Guard, BranchInst *BI) { assert(BI->getNumSuccessors() == 2 && "Wrong number of successors?"); assert(BI->isConditional() && "Unconditional branch has 2 successors?"); |