1f22ef01cSRoman Divacky //===- JumpThreading.cpp - Thread control through conditional blocks ------===//
2f22ef01cSRoman Divacky //
3f22ef01cSRoman Divacky // The LLVM Compiler Infrastructure
4f22ef01cSRoman Divacky //
5f22ef01cSRoman Divacky // This file is distributed under the University of Illinois Open Source
6f22ef01cSRoman Divacky // License. See LICENSE.TXT for details.
7f22ef01cSRoman Divacky //
8f22ef01cSRoman Divacky //===----------------------------------------------------------------------===//
9f22ef01cSRoman Divacky //
10f22ef01cSRoman Divacky // This file implements the Jump Threading pass.
11f22ef01cSRoman Divacky //
12f22ef01cSRoman Divacky //===----------------------------------------------------------------------===//
13f22ef01cSRoman Divacky
143ca95b02SDimitry Andric #include "llvm/Transforms/Scalar/JumpThreading.h"
15139f7f9bSDimitry Andric #include "llvm/ADT/DenseMap.h"
16139f7f9bSDimitry Andric #include "llvm/ADT/DenseSet.h"
172cab237bSDimitry Andric #include "llvm/ADT/Optional.h"
18139f7f9bSDimitry Andric #include "llvm/ADT/STLExtras.h"
192cab237bSDimitry Andric #include "llvm/ADT/SmallPtrSet.h"
202cab237bSDimitry Andric #include "llvm/ADT/SmallVector.h"
21139f7f9bSDimitry Andric #include "llvm/ADT/Statistic.h"
227a7e6055SDimitry Andric #include "llvm/Analysis/AliasAnalysis.h"
232cab237bSDimitry Andric #include "llvm/Analysis/BlockFrequencyInfo.h"
242cab237bSDimitry Andric #include "llvm/Analysis/BranchProbabilityInfo.h"
25db17bf38SDimitry Andric #include "llvm/Analysis/CFG.h"
263b0f4066SDimitry Andric #include "llvm/Analysis/ConstantFolding.h"
27db17bf38SDimitry Andric #include "llvm/Analysis/GlobalsModRef.h"
28*b5893f02SDimitry Andric #include "llvm/Analysis/GuardUtils.h"
29f22ef01cSRoman Divacky #include "llvm/Analysis/InstructionSimplify.h"
302cab237bSDimitry Andric #include "llvm/Analysis/LazyValueInfo.h"
31ffd1746dSEd Schouten #include "llvm/Analysis/Loads.h"
327d523365SDimitry Andric #include "llvm/Analysis/LoopInfo.h"
332cab237bSDimitry Andric #include "llvm/Analysis/TargetLibraryInfo.h"
347d523365SDimitry Andric #include "llvm/Analysis/ValueTracking.h"
352cab237bSDimitry Andric #include "llvm/IR/BasicBlock.h"
362cab237bSDimitry Andric #include "llvm/IR/CFG.h"
372cab237bSDimitry Andric #include "llvm/IR/Constant.h"
38edd7eaddSDimitry Andric #include "llvm/IR/ConstantRange.h"
392cab237bSDimitry Andric #include "llvm/IR/Constants.h"
40139f7f9bSDimitry Andric #include "llvm/IR/DataLayout.h"
41*b5893f02SDimitry Andric #include "llvm/IR/DomTreeUpdater.h"
422cab237bSDimitry Andric #include "llvm/IR/Dominators.h"
432cab237bSDimitry Andric #include "llvm/IR/Function.h"
442cab237bSDimitry Andric #include "llvm/IR/InstrTypes.h"
452cab237bSDimitry Andric #include "llvm/IR/Instruction.h"
462cab237bSDimitry Andric #include "llvm/IR/Instructions.h"
47139f7f9bSDimitry Andric #include "llvm/IR/IntrinsicInst.h"
482cab237bSDimitry Andric #include "llvm/IR/Intrinsics.h"
49139f7f9bSDimitry Andric #include "llvm/IR/LLVMContext.h"
507d523365SDimitry Andric #include "llvm/IR/MDBuilder.h"
5139d628a0SDimitry Andric #include "llvm/IR/Metadata.h"
522cab237bSDimitry Andric #include "llvm/IR/Module.h"
532cab237bSDimitry Andric #include "llvm/IR/PassManager.h"
547a7e6055SDimitry Andric #include "llvm/IR/PatternMatch.h"
552cab237bSDimitry Andric #include "llvm/IR/Type.h"
562cab237bSDimitry Andric #include "llvm/IR/Use.h"
572cab237bSDimitry Andric #include "llvm/IR/User.h"
582cab237bSDimitry Andric #include "llvm/IR/Value.h"
59139f7f9bSDimitry Andric #include "llvm/Pass.h"
602cab237bSDimitry Andric #include "llvm/Support/BlockFrequency.h"
612cab237bSDimitry Andric #include "llvm/Support/BranchProbability.h"
622cab237bSDimitry Andric #include "llvm/Support/Casting.h"
63f22ef01cSRoman Divacky #include "llvm/Support/CommandLine.h"
64f22ef01cSRoman Divacky #include "llvm/Support/Debug.h"
65f22ef01cSRoman Divacky #include "llvm/Support/raw_ostream.h"
66db17bf38SDimitry Andric #include "llvm/Transforms/Scalar.h"
67139f7f9bSDimitry Andric #include "llvm/Transforms/Utils/BasicBlockUtils.h"
687a7e6055SDimitry Andric #include "llvm/Transforms/Utils/Cloning.h"
69*b5893f02SDimitry Andric #include "llvm/Transforms/Utils/Local.h"
70139f7f9bSDimitry Andric #include "llvm/Transforms/Utils/SSAUpdater.h"
712cab237bSDimitry Andric #include "llvm/Transforms/Utils/ValueMapper.h"
727d523365SDimitry Andric #include <algorithm>
732cab237bSDimitry Andric #include <cassert>
742cab237bSDimitry Andric #include <cstddef>
752cab237bSDimitry Andric #include <cstdint>
762cab237bSDimitry Andric #include <iterator>
777d523365SDimitry Andric #include <memory>
782cab237bSDimitry Andric #include <utility>
792cab237bSDimitry Andric
80f22ef01cSRoman Divacky using namespace llvm;
813ca95b02SDimitry Andric using namespace jumpthreading;
82f22ef01cSRoman Divacky
8391bc56edSDimitry Andric #define DEBUG_TYPE "jump-threading"
8491bc56edSDimitry Andric
85f22ef01cSRoman Divacky STATISTIC(NumThreads, "Number of jumps threaded");
86f22ef01cSRoman Divacky STATISTIC(NumFolds, "Number of terminators folded");
87f22ef01cSRoman Divacky STATISTIC(NumDupes, "Number of branch blocks duplicated to eliminate phi");
88f22ef01cSRoman Divacky
89f22ef01cSRoman Divacky static cl::opt<unsigned>
9039d628a0SDimitry Andric BBDuplicateThreshold("jump-threading-threshold",
91f22ef01cSRoman Divacky cl::desc("Max block size to duplicate for jump threading"),
92f22ef01cSRoman Divacky cl::init(6), cl::Hidden);
93f22ef01cSRoman Divacky
947d523365SDimitry Andric static cl::opt<unsigned>
957d523365SDimitry Andric ImplicationSearchThreshold(
967d523365SDimitry Andric "jump-threading-implication-search-threshold",
977d523365SDimitry Andric cl::desc("The number of predecessors to search for a stronger "
987d523365SDimitry Andric "condition to use to thread over a weaker condition"),
997d523365SDimitry Andric cl::init(3), cl::Hidden);
1007d523365SDimitry Andric
10137cd60a3SDimitry Andric static cl::opt<bool> PrintLVIAfterJumpThreading(
10237cd60a3SDimitry Andric "print-lvi-after-jump-threading",
10337cd60a3SDimitry Andric cl::desc("Print the LazyValueInfo cache after JumpThreading"), cl::init(false),
10437cd60a3SDimitry Andric cl::Hidden);
10537cd60a3SDimitry Andric
106f22ef01cSRoman Divacky namespace {
1072cab237bSDimitry Andric
108f22ef01cSRoman Divacky /// This pass performs 'jump threading', which looks at blocks that have
109f22ef01cSRoman Divacky /// multiple predecessors and multiple successors. If one or more of the
110f22ef01cSRoman Divacky /// predecessors of the block can be proven to always jump to one of the
111f22ef01cSRoman Divacky /// successors, we forward the edge from the predecessor to the successor by
112f22ef01cSRoman Divacky /// duplicating the contents of this block.
113f22ef01cSRoman Divacky ///
114f22ef01cSRoman Divacky /// An example of when this can occur is code like this:
115f22ef01cSRoman Divacky ///
116f22ef01cSRoman Divacky /// if () { ...
117f22ef01cSRoman Divacky /// X = 4;
118f22ef01cSRoman Divacky /// }
119f22ef01cSRoman Divacky /// if (X < 3) {
120f22ef01cSRoman Divacky ///
121f22ef01cSRoman Divacky /// In this case, the unconditional branch at the end of the first if can be
122f22ef01cSRoman Divacky /// revectored to the false side of the second if.
123f22ef01cSRoman Divacky class JumpThreading : public FunctionPass {
1243ca95b02SDimitry Andric JumpThreadingPass Impl;
125e580952dSDimitry Andric
126f22ef01cSRoman Divacky public:
127f22ef01cSRoman Divacky static char ID; // Pass identification
1282cab237bSDimitry Andric
JumpThreading(int T=-1)1293ca95b02SDimitry Andric JumpThreading(int T = -1) : FunctionPass(ID), Impl(T) {
1302754fe60SDimitry Andric initializeJumpThreadingPass(*PassRegistry::getPassRegistry());
1312754fe60SDimitry Andric }
132f22ef01cSRoman Divacky
13391bc56edSDimitry Andric bool runOnFunction(Function &F) override;
134f22ef01cSRoman Divacky
getAnalysisUsage(AnalysisUsage & AU) const13591bc56edSDimitry Andric void getAnalysisUsage(AnalysisUsage &AU) const override {
13637cd60a3SDimitry Andric AU.addRequired<DominatorTreeWrapperPass>();
1374ba319b5SDimitry Andric AU.addPreserved<DominatorTreeWrapperPass>();
1387a7e6055SDimitry Andric AU.addRequired<AAResultsWrapperPass>();
1393ca95b02SDimitry Andric AU.addRequired<LazyValueInfoWrapperPass>();
1404ba319b5SDimitry Andric AU.addPreserved<LazyValueInfoWrapperPass>();
1417d523365SDimitry Andric AU.addPreserved<GlobalsAAWrapperPass>();
142ff0cc061SDimitry Andric AU.addRequired<TargetLibraryInfoWrapperPass>();
143e580952dSDimitry Andric }
144f22ef01cSRoman Divacky
releaseMemory()1453ca95b02SDimitry Andric void releaseMemory() override { Impl.releaseMemory(); }
146f22ef01cSRoman Divacky };
1472cab237bSDimitry Andric
1482cab237bSDimitry Andric } // end anonymous namespace
149f22ef01cSRoman Divacky
150f22ef01cSRoman Divacky char JumpThreading::ID = 0;
1512cab237bSDimitry Andric
1522754fe60SDimitry Andric INITIALIZE_PASS_BEGIN(JumpThreading, "jump-threading",
1532754fe60SDimitry Andric "Jump Threading", false, false)
INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)1544ba319b5SDimitry Andric INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
1553ca95b02SDimitry Andric INITIALIZE_PASS_DEPENDENCY(LazyValueInfoWrapperPass)
156ff0cc061SDimitry Andric INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
1577a7e6055SDimitry Andric INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
1582754fe60SDimitry Andric INITIALIZE_PASS_END(JumpThreading, "jump-threading",
1592754fe60SDimitry Andric "Jump Threading", false, false)
160f22ef01cSRoman Divacky
161f22ef01cSRoman Divacky // Public interface to the Jump Threading pass
1622cab237bSDimitry Andric FunctionPass *llvm::createJumpThreadingPass(int Threshold) {
1632cab237bSDimitry Andric return new JumpThreading(Threshold);
1642cab237bSDimitry Andric }
165f22ef01cSRoman Divacky
JumpThreadingPass(int T)1663ca95b02SDimitry Andric JumpThreadingPass::JumpThreadingPass(int T) {
1673ca95b02SDimitry Andric BBDupThreshold = (T == -1) ? BBDuplicateThreshold : unsigned(T);
1683ca95b02SDimitry Andric }
1693ca95b02SDimitry Andric
1702cab237bSDimitry Andric // Update branch probability information according to conditional
1714ba319b5SDimitry Andric // branch probability. This is usually made possible for cloned branches
1722cab237bSDimitry Andric // in inline instances by the context specific profile in the caller.
1732cab237bSDimitry Andric // For instance,
1742cab237bSDimitry Andric //
1752cab237bSDimitry Andric // [Block PredBB]
1762cab237bSDimitry Andric // [Branch PredBr]
1772cab237bSDimitry Andric // if (t) {
1782cab237bSDimitry Andric // Block A;
1792cab237bSDimitry Andric // } else {
1802cab237bSDimitry Andric // Block B;
1812cab237bSDimitry Andric // }
1822cab237bSDimitry Andric //
1832cab237bSDimitry Andric // [Block BB]
1842cab237bSDimitry Andric // cond = PN([true, %A], [..., %B]); // PHI node
1852cab237bSDimitry Andric // [Branch CondBr]
1862cab237bSDimitry Andric // if (cond) {
1872cab237bSDimitry Andric // ... // P(cond == true) = 1%
1882cab237bSDimitry Andric // }
1892cab237bSDimitry Andric //
1902cab237bSDimitry Andric // Here we know that when block A is taken, cond must be true, which means
1912cab237bSDimitry Andric // P(cond == true | A) = 1
1922cab237bSDimitry Andric //
1932cab237bSDimitry Andric // Given that P(cond == true) = P(cond == true | A) * P(A) +
1942cab237bSDimitry Andric // P(cond == true | B) * P(B)
1952cab237bSDimitry Andric // we get:
1962cab237bSDimitry Andric // P(cond == true ) = P(A) + P(cond == true | B) * P(B)
1972cab237bSDimitry Andric //
1982cab237bSDimitry Andric // which gives us:
1992cab237bSDimitry Andric // P(A) is less than P(cond == true), i.e.
2002cab237bSDimitry Andric // P(t == true) <= P(cond == true)
2012cab237bSDimitry Andric //
2022cab237bSDimitry Andric // In other words, if we know P(cond == true) is unlikely, we know
2032cab237bSDimitry Andric // that P(t == true) is also unlikely.
2042cab237bSDimitry Andric //
updatePredecessorProfileMetadata(PHINode * PN,BasicBlock * BB)2052cab237bSDimitry Andric static void updatePredecessorProfileMetadata(PHINode *PN, BasicBlock *BB) {
2062cab237bSDimitry Andric BranchInst *CondBr = dyn_cast<BranchInst>(BB->getTerminator());
2072cab237bSDimitry Andric if (!CondBr)
2082cab237bSDimitry Andric return;
2092cab237bSDimitry Andric
2102cab237bSDimitry Andric BranchProbability BP;
2112cab237bSDimitry Andric uint64_t TrueWeight, FalseWeight;
2122cab237bSDimitry Andric if (!CondBr->extractProfMetadata(TrueWeight, FalseWeight))
2132cab237bSDimitry Andric return;
2142cab237bSDimitry Andric
2152cab237bSDimitry Andric // Returns the outgoing edge of the dominating predecessor block
2162cab237bSDimitry Andric // that leads to the PhiNode's incoming block:
2172cab237bSDimitry Andric auto GetPredOutEdge =
2182cab237bSDimitry Andric [](BasicBlock *IncomingBB,
2192cab237bSDimitry Andric BasicBlock *PhiBB) -> std::pair<BasicBlock *, BasicBlock *> {
2202cab237bSDimitry Andric auto *PredBB = IncomingBB;
2212cab237bSDimitry Andric auto *SuccBB = PhiBB;
2222cab237bSDimitry Andric while (true) {
2232cab237bSDimitry Andric BranchInst *PredBr = dyn_cast<BranchInst>(PredBB->getTerminator());
2242cab237bSDimitry Andric if (PredBr && PredBr->isConditional())
2252cab237bSDimitry Andric return {PredBB, SuccBB};
2262cab237bSDimitry Andric auto *SinglePredBB = PredBB->getSinglePredecessor();
2272cab237bSDimitry Andric if (!SinglePredBB)
2282cab237bSDimitry Andric return {nullptr, nullptr};
2292cab237bSDimitry Andric SuccBB = PredBB;
2302cab237bSDimitry Andric PredBB = SinglePredBB;
2312cab237bSDimitry Andric }
2322cab237bSDimitry Andric };
2332cab237bSDimitry Andric
2342cab237bSDimitry Andric for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
2352cab237bSDimitry Andric Value *PhiOpnd = PN->getIncomingValue(i);
2362cab237bSDimitry Andric ConstantInt *CI = dyn_cast<ConstantInt>(PhiOpnd);
2372cab237bSDimitry Andric
2382cab237bSDimitry Andric if (!CI || !CI->getType()->isIntegerTy(1))
2392cab237bSDimitry Andric continue;
2402cab237bSDimitry Andric
2412cab237bSDimitry Andric BP = (CI->isOne() ? BranchProbability::getBranchProbability(
2422cab237bSDimitry Andric TrueWeight, TrueWeight + FalseWeight)
2432cab237bSDimitry Andric : BranchProbability::getBranchProbability(
2442cab237bSDimitry Andric FalseWeight, TrueWeight + FalseWeight));
2452cab237bSDimitry Andric
2462cab237bSDimitry Andric auto PredOutEdge = GetPredOutEdge(PN->getIncomingBlock(i), BB);
2472cab237bSDimitry Andric if (!PredOutEdge.first)
2482cab237bSDimitry Andric return;
2492cab237bSDimitry Andric
2502cab237bSDimitry Andric BasicBlock *PredBB = PredOutEdge.first;
2512cab237bSDimitry Andric BranchInst *PredBr = cast<BranchInst>(PredBB->getTerminator());
2522cab237bSDimitry Andric
2532cab237bSDimitry Andric uint64_t PredTrueWeight, PredFalseWeight;
2542cab237bSDimitry Andric // FIXME: We currently only set the profile data when it is missing.
2552cab237bSDimitry Andric // With PGO, this can be used to refine even existing profile data with
2562cab237bSDimitry Andric // context information. This needs to be done after more performance
2572cab237bSDimitry Andric // testing.
2582cab237bSDimitry Andric if (PredBr->extractProfMetadata(PredTrueWeight, PredFalseWeight))
2592cab237bSDimitry Andric continue;
2602cab237bSDimitry Andric
2612cab237bSDimitry Andric // We can not infer anything useful when BP >= 50%, because BP is the
2622cab237bSDimitry Andric // upper bound probability value.
2632cab237bSDimitry Andric if (BP >= BranchProbability(50, 100))
2642cab237bSDimitry Andric continue;
2652cab237bSDimitry Andric
2662cab237bSDimitry Andric SmallVector<uint32_t, 2> Weights;
2672cab237bSDimitry Andric if (PredBr->getSuccessor(0) == PredOutEdge.second) {
2682cab237bSDimitry Andric Weights.push_back(BP.getNumerator());
2692cab237bSDimitry Andric Weights.push_back(BP.getCompl().getNumerator());
2702cab237bSDimitry Andric } else {
2712cab237bSDimitry Andric Weights.push_back(BP.getCompl().getNumerator());
2722cab237bSDimitry Andric Weights.push_back(BP.getNumerator());
2732cab237bSDimitry Andric }
2742cab237bSDimitry Andric PredBr->setMetadata(LLVMContext::MD_prof,
2752cab237bSDimitry Andric MDBuilder(PredBr->getParent()->getContext())
2762cab237bSDimitry Andric .createBranchWeights(Weights));
2772cab237bSDimitry Andric }
2782cab237bSDimitry Andric }
2792cab237bSDimitry Andric
280f22ef01cSRoman Divacky /// runOnFunction - Toplevel algorithm.
runOnFunction(Function & F)281f22ef01cSRoman Divacky bool JumpThreading::runOnFunction(Function &F) {
2823ca95b02SDimitry Andric if (skipFunction(F))
28391bc56edSDimitry Andric return false;
2843ca95b02SDimitry Andric auto TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
2854ba319b5SDimitry Andric // Get DT analysis before LVI. When LVI is initialized it conditionally adds
2864ba319b5SDimitry Andric // DT if it's available.
2874ba319b5SDimitry Andric auto DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
2883ca95b02SDimitry Andric auto LVI = &getAnalysis<LazyValueInfoWrapperPass>().getLVI();
2897a7e6055SDimitry Andric auto AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
290*b5893f02SDimitry Andric DomTreeUpdater DTU(*DT, DomTreeUpdater::UpdateStrategy::Lazy);
2913ca95b02SDimitry Andric std::unique_ptr<BlockFrequencyInfo> BFI;
2923ca95b02SDimitry Andric std::unique_ptr<BranchProbabilityInfo> BPI;
293da09e106SDimitry Andric bool HasProfileData = F.hasProfileData();
2947d523365SDimitry Andric if (HasProfileData) {
2957d523365SDimitry Andric LoopInfo LI{DominatorTree(F)};
296db17bf38SDimitry Andric BPI.reset(new BranchProbabilityInfo(F, LI, TLI));
2977d523365SDimitry Andric BFI.reset(new BlockFrequencyInfo(F, *BPI, LI));
2987d523365SDimitry Andric }
2997a7e6055SDimitry Andric
300*b5893f02SDimitry Andric bool Changed = Impl.runImpl(F, TLI, LVI, AA, &DTU, HasProfileData,
3014ba319b5SDimitry Andric std::move(BFI), std::move(BPI));
30237cd60a3SDimitry Andric if (PrintLVIAfterJumpThreading) {
30337cd60a3SDimitry Andric dbgs() << "LVI for function '" << F.getName() << "':\n";
3044ba319b5SDimitry Andric LVI->printLVI(F, *DT, dbgs());
30537cd60a3SDimitry Andric }
30637cd60a3SDimitry Andric return Changed;
3073ca95b02SDimitry Andric }
3083ca95b02SDimitry Andric
run(Function & F,FunctionAnalysisManager & AM)3093ca95b02SDimitry Andric PreservedAnalyses JumpThreadingPass::run(Function &F,
310d88c1a5aSDimitry Andric FunctionAnalysisManager &AM) {
3113ca95b02SDimitry Andric auto &TLI = AM.getResult<TargetLibraryAnalysis>(F);
3124ba319b5SDimitry Andric // Get DT analysis before LVI. When LVI is initialized it conditionally adds
3134ba319b5SDimitry Andric // DT if it's available.
3144ba319b5SDimitry Andric auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
3153ca95b02SDimitry Andric auto &LVI = AM.getResult<LazyValueAnalysis>(F);
3167a7e6055SDimitry Andric auto &AA = AM.getResult<AAManager>(F);
317*b5893f02SDimitry Andric DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Lazy);
3187a7e6055SDimitry Andric
3193ca95b02SDimitry Andric std::unique_ptr<BlockFrequencyInfo> BFI;
3203ca95b02SDimitry Andric std::unique_ptr<BranchProbabilityInfo> BPI;
321da09e106SDimitry Andric if (F.hasProfileData()) {
3223ca95b02SDimitry Andric LoopInfo LI{DominatorTree(F)};
323db17bf38SDimitry Andric BPI.reset(new BranchProbabilityInfo(F, LI, &TLI));
3243ca95b02SDimitry Andric BFI.reset(new BlockFrequencyInfo(F, *BPI, LI));
3253ca95b02SDimitry Andric }
3263ca95b02SDimitry Andric
327*b5893f02SDimitry Andric bool Changed = runImpl(F, &TLI, &LVI, &AA, &DTU, HasProfileData,
3284ba319b5SDimitry Andric std::move(BFI), std::move(BPI));
3293ca95b02SDimitry Andric
3303ca95b02SDimitry Andric if (!Changed)
3313ca95b02SDimitry Andric return PreservedAnalyses::all();
3323ca95b02SDimitry Andric PreservedAnalyses PA;
3333ca95b02SDimitry Andric PA.preserve<GlobalsAA>();
3344ba319b5SDimitry Andric PA.preserve<DominatorTreeAnalysis>();
3354ba319b5SDimitry Andric PA.preserve<LazyValueAnalysis>();
3363ca95b02SDimitry Andric return PA;
3373ca95b02SDimitry Andric }
3383ca95b02SDimitry Andric
runImpl(Function & F,TargetLibraryInfo * TLI_,LazyValueInfo * LVI_,AliasAnalysis * AA_,DomTreeUpdater * DTU_,bool HasProfileData_,std::unique_ptr<BlockFrequencyInfo> BFI_,std::unique_ptr<BranchProbabilityInfo> BPI_)3393ca95b02SDimitry Andric bool JumpThreadingPass::runImpl(Function &F, TargetLibraryInfo *TLI_,
3407a7e6055SDimitry Andric LazyValueInfo *LVI_, AliasAnalysis *AA_,
341*b5893f02SDimitry Andric DomTreeUpdater *DTU_, bool HasProfileData_,
3423ca95b02SDimitry Andric std::unique_ptr<BlockFrequencyInfo> BFI_,
3433ca95b02SDimitry Andric std::unique_ptr<BranchProbabilityInfo> BPI_) {
3444ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "Jump threading on function '" << F.getName() << "'\n");
3453ca95b02SDimitry Andric TLI = TLI_;
3463ca95b02SDimitry Andric LVI = LVI_;
3477a7e6055SDimitry Andric AA = AA_;
348*b5893f02SDimitry Andric DTU = DTU_;
3493ca95b02SDimitry Andric BFI.reset();
3503ca95b02SDimitry Andric BPI.reset();
3513ca95b02SDimitry Andric // When profile data is available, we need to update edge weights after
3523ca95b02SDimitry Andric // successful jump threading, which requires both BPI and BFI being available.
3533ca95b02SDimitry Andric HasProfileData = HasProfileData_;
3547a7e6055SDimitry Andric auto *GuardDecl = F.getParent()->getFunction(
3557a7e6055SDimitry Andric Intrinsic::getName(Intrinsic::experimental_guard));
3567a7e6055SDimitry Andric HasGuards = GuardDecl && !GuardDecl->use_empty();
3573ca95b02SDimitry Andric if (HasProfileData) {
3583ca95b02SDimitry Andric BPI = std::move(BPI_);
3593ca95b02SDimitry Andric BFI = std::move(BFI_);
3603ca95b02SDimitry Andric }
361f22ef01cSRoman Divacky
3624ba319b5SDimitry Andric // JumpThreading must not processes blocks unreachable from entry. It's a
3634ba319b5SDimitry Andric // waste of compute time and can potentially lead to hangs.
3644ba319b5SDimitry Andric SmallPtrSet<BasicBlock *, 16> Unreachable;
365*b5893f02SDimitry Andric assert(DTU && "DTU isn't passed into JumpThreading before using it.");
366*b5893f02SDimitry Andric assert(DTU->hasDomTree() && "JumpThreading relies on DomTree to proceed.");
367*b5893f02SDimitry Andric DominatorTree &DT = DTU->getDomTree();
3684ba319b5SDimitry Andric for (auto &BB : F)
3694ba319b5SDimitry Andric if (!DT.isReachableFromEntry(&BB))
3704ba319b5SDimitry Andric Unreachable.insert(&BB);
37191bc56edSDimitry Andric
372f22ef01cSRoman Divacky FindLoopHeaders(F);
373f22ef01cSRoman Divacky
3744ba319b5SDimitry Andric bool EverChanged = false;
375444ed5c5SDimitry Andric bool Changed;
376f22ef01cSRoman Divacky do {
377f22ef01cSRoman Divacky Changed = false;
3784ba319b5SDimitry Andric for (auto &BB : F) {
3794ba319b5SDimitry Andric if (Unreachable.count(&BB))
3804ba319b5SDimitry Andric continue;
3814ba319b5SDimitry Andric while (ProcessBlock(&BB)) // Thread all of the branches we can over BB.
382f22ef01cSRoman Divacky Changed = true;
3834ba319b5SDimitry Andric // Stop processing BB if it's the entry or is now deleted. The following
3844ba319b5SDimitry Andric // routines attempt to eliminate BB and locating a suitable replacement
3854ba319b5SDimitry Andric // for the entry is non-trivial.
386*b5893f02SDimitry Andric if (&BB == &F.getEntryBlock() || DTU->isBBPendingDeletion(&BB))
3874ba319b5SDimitry Andric continue;
388f22ef01cSRoman Divacky
3894ba319b5SDimitry Andric if (pred_empty(&BB)) {
3904ba319b5SDimitry Andric // When ProcessBlock makes BB unreachable it doesn't bother to fix up
3914ba319b5SDimitry Andric // the instructions in it. We must remove BB to prevent invalid IR.
3924ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << " JT: Deleting dead block '" << BB.getName()
3934ba319b5SDimitry Andric << "' with terminator: " << *BB.getTerminator()
3944ba319b5SDimitry Andric << '\n');
3954ba319b5SDimitry Andric LoopHeaders.erase(&BB);
3964ba319b5SDimitry Andric LVI->eraseBlock(&BB);
397*b5893f02SDimitry Andric DeleteDeadBlock(&BB, DTU);
398f22ef01cSRoman Divacky Changed = true;
3992754fe60SDimitry Andric continue;
4002754fe60SDimitry Andric }
4012754fe60SDimitry Andric
4024ba319b5SDimitry Andric // ProcessBlock doesn't thread BBs with unconditional TIs. However, if BB
4034ba319b5SDimitry Andric // is "almost empty", we attempt to merge BB with its sole successor.
4044ba319b5SDimitry Andric auto *BI = dyn_cast<BranchInst>(BB.getTerminator());
4052754fe60SDimitry Andric if (BI && BI->isUnconditional() &&
4064ba319b5SDimitry Andric // The terminator must be the only non-phi instruction in BB.
4074ba319b5SDimitry Andric BB.getFirstNonPHIOrDbg()->isTerminator() &&
4084ba319b5SDimitry Andric // Don't alter Loop headers and latches to ensure another pass can
4094ba319b5SDimitry Andric // detect and transform nested loops later.
4104ba319b5SDimitry Andric !LoopHeaders.count(&BB) && !LoopHeaders.count(BI->getSuccessor(0)) &&
411*b5893f02SDimitry Andric TryToSimplifyUncondBranchFromEmptyBlock(&BB, DTU)) {
412*b5893f02SDimitry Andric // BB is valid for cleanup here because we passed in DTU. F remains
413*b5893f02SDimitry Andric // BB's parent until a DTU->getDomTree() event.
4144ba319b5SDimitry Andric LVI->eraseBlock(&BB);
415f22ef01cSRoman Divacky Changed = true;
416f22ef01cSRoman Divacky }
417f22ef01cSRoman Divacky }
418f22ef01cSRoman Divacky EverChanged |= Changed;
419f22ef01cSRoman Divacky } while (Changed);
420f22ef01cSRoman Divacky
421f22ef01cSRoman Divacky LoopHeaders.clear();
422*b5893f02SDimitry Andric // Flush only the Dominator Tree.
423*b5893f02SDimitry Andric DTU->getDomTree();
4244ba319b5SDimitry Andric LVI->enableDT();
425f22ef01cSRoman Divacky return EverChanged;
426f22ef01cSRoman Divacky }
427f22ef01cSRoman Divacky
428302affcbSDimitry Andric // Replace uses of Cond with ToVal when safe to do so. If all uses are
429302affcbSDimitry Andric // replaced, we can remove Cond. We cannot blindly replace all uses of Cond
430302affcbSDimitry Andric // because we may incorrectly replace uses when guards/assumes are uses of
431302affcbSDimitry Andric // of `Cond` and we used the guards/assume to reason about the `Cond` value
432302affcbSDimitry Andric // at the end of block. RAUW unconditionally replaces all uses
433302affcbSDimitry Andric // including the guards/assumes themselves and the uses before the
434302affcbSDimitry Andric // guard/assume.
ReplaceFoldableUses(Instruction * Cond,Value * ToVal)435302affcbSDimitry Andric static void ReplaceFoldableUses(Instruction *Cond, Value *ToVal) {
436302affcbSDimitry Andric assert(Cond->getType() == ToVal->getType());
437302affcbSDimitry Andric auto *BB = Cond->getParent();
438302affcbSDimitry Andric // We can unconditionally replace all uses in non-local blocks (i.e. uses
439302affcbSDimitry Andric // strictly dominated by BB), since LVI information is true from the
440302affcbSDimitry Andric // terminator of BB.
441302affcbSDimitry Andric replaceNonLocalUsesWith(Cond, ToVal);
442302affcbSDimitry Andric for (Instruction &I : reverse(*BB)) {
443302affcbSDimitry Andric // Reached the Cond whose uses we are trying to replace, so there are no
444302affcbSDimitry Andric // more uses.
445302affcbSDimitry Andric if (&I == Cond)
446302affcbSDimitry Andric break;
447302affcbSDimitry Andric // We only replace uses in instructions that are guaranteed to reach the end
448302affcbSDimitry Andric // of BB, where we know Cond is ToVal.
449302affcbSDimitry Andric if (!isGuaranteedToTransferExecutionToSuccessor(&I))
450302affcbSDimitry Andric break;
451302affcbSDimitry Andric I.replaceUsesOfWith(Cond, ToVal);
452302affcbSDimitry Andric }
453302affcbSDimitry Andric if (Cond->use_empty() && !Cond->mayHaveSideEffects())
454302affcbSDimitry Andric Cond->eraseFromParent();
455302affcbSDimitry Andric }
456302affcbSDimitry Andric
4577a7e6055SDimitry Andric /// Return the cost of duplicating a piece of this block from first non-phi
4587a7e6055SDimitry Andric /// and before StopAt instruction to thread across it. Stop scanning the block
4597a7e6055SDimitry Andric /// when exceeding the threshold. If duplication is impossible, returns ~0U.
getJumpThreadDuplicationCost(BasicBlock * BB,Instruction * StopAt,unsigned Threshold)4607a7e6055SDimitry Andric static unsigned getJumpThreadDuplicationCost(BasicBlock *BB,
4617a7e6055SDimitry Andric Instruction *StopAt,
462139f7f9bSDimitry Andric unsigned Threshold) {
4637a7e6055SDimitry Andric assert(StopAt->getParent() == BB && "Not an instruction from proper BB?");
464f22ef01cSRoman Divacky /// Ignore PHI nodes, these will be flattened when duplication happens.
4657d523365SDimitry Andric BasicBlock::const_iterator I(BB->getFirstNonPHI());
466f22ef01cSRoman Divacky
467f22ef01cSRoman Divacky // FIXME: THREADING will delete values that are just used to compute the
468f22ef01cSRoman Divacky // branch, so they shouldn't count against the duplication cost.
469f22ef01cSRoman Divacky
4707d523365SDimitry Andric unsigned Bonus = 0;
4717a7e6055SDimitry Andric if (BB->getTerminator() == StopAt) {
4727d523365SDimitry Andric // Threading through a switch statement is particularly profitable. If this
4737a7e6055SDimitry Andric // block ends in a switch, decrease its cost to make it more likely to
4747a7e6055SDimitry Andric // happen.
4757a7e6055SDimitry Andric if (isa<SwitchInst>(StopAt))
4767d523365SDimitry Andric Bonus = 6;
4777d523365SDimitry Andric
4787d523365SDimitry Andric // The same holds for indirect branches, but slightly more so.
4797a7e6055SDimitry Andric if (isa<IndirectBrInst>(StopAt))
4807d523365SDimitry Andric Bonus = 8;
4817a7e6055SDimitry Andric }
4827d523365SDimitry Andric
4837d523365SDimitry Andric // Bump the threshold up so the early exit from the loop doesn't skip the
4847d523365SDimitry Andric // terminator-based Size adjustment at the end.
4857d523365SDimitry Andric Threshold += Bonus;
4867d523365SDimitry Andric
487f22ef01cSRoman Divacky // Sum up the cost of each instruction until we get to the terminator. Don't
488f22ef01cSRoman Divacky // include the terminator because the copy won't include it.
489f22ef01cSRoman Divacky unsigned Size = 0;
4907a7e6055SDimitry Andric for (; &*I != StopAt; ++I) {
491139f7f9bSDimitry Andric
492139f7f9bSDimitry Andric // Stop scanning the block if we've reached the threshold.
493139f7f9bSDimitry Andric if (Size > Threshold)
494139f7f9bSDimitry Andric return Size;
495139f7f9bSDimitry Andric
496f22ef01cSRoman Divacky // Debugger intrinsics don't incur code size.
497f22ef01cSRoman Divacky if (isa<DbgInfoIntrinsic>(I)) continue;
498f22ef01cSRoman Divacky
499f22ef01cSRoman Divacky // If this is a pointer->pointer bitcast, it is free.
500f22ef01cSRoman Divacky if (isa<BitCastInst>(I) && I->getType()->isPointerTy())
501f22ef01cSRoman Divacky continue;
502f22ef01cSRoman Divacky
5037d523365SDimitry Andric // Bail out if this instruction gives back a token type, it is not possible
5047d523365SDimitry Andric // to duplicate it if it is used outside this BB.
5057d523365SDimitry Andric if (I->getType()->isTokenTy() && I->isUsedOutsideOfBlock(BB))
5067d523365SDimitry Andric return ~0U;
5077d523365SDimitry Andric
508f22ef01cSRoman Divacky // All other instructions count for at least one unit.
509f22ef01cSRoman Divacky ++Size;
510f22ef01cSRoman Divacky
511f22ef01cSRoman Divacky // Calls are more expensive. If they are non-intrinsic calls, we model them
512f22ef01cSRoman Divacky // as having cost of 4. If they are a non-vector intrinsic, we model them
513f22ef01cSRoman Divacky // as having cost of 2 total, and if they are a vector intrinsic, we model
514f22ef01cSRoman Divacky // them as having cost 1.
515f22ef01cSRoman Divacky if (const CallInst *CI = dyn_cast<CallInst>(I)) {
5167d523365SDimitry Andric if (CI->cannotDuplicate() || CI->isConvergent())
517139f7f9bSDimitry Andric // Blocks with NoDuplicate are modelled as having infinite cost, so they
518139f7f9bSDimitry Andric // are never duplicated.
519139f7f9bSDimitry Andric return ~0U;
520139f7f9bSDimitry Andric else if (!isa<IntrinsicInst>(CI))
521f22ef01cSRoman Divacky Size += 3;
522f22ef01cSRoman Divacky else if (!CI->getType()->isVectorTy())
523f22ef01cSRoman Divacky Size += 1;
524f22ef01cSRoman Divacky }
525f22ef01cSRoman Divacky }
526f22ef01cSRoman Divacky
5277d523365SDimitry Andric return Size > Bonus ? Size - Bonus : 0;
528f22ef01cSRoman Divacky }
529f22ef01cSRoman Divacky
530f22ef01cSRoman Divacky /// FindLoopHeaders - We do not want jump threading to turn proper loop
531f22ef01cSRoman Divacky /// structures into irreducible loops. Doing this breaks up the loop nesting
532f22ef01cSRoman Divacky /// hierarchy and pessimizes later transformations. To prevent this from
533f22ef01cSRoman Divacky /// happening, we first have to find the loop headers. Here we approximate this
534f22ef01cSRoman Divacky /// by finding targets of backedges in the CFG.
535f22ef01cSRoman Divacky ///
536f22ef01cSRoman Divacky /// Note that there definitely are cases when we want to allow threading of
537f22ef01cSRoman Divacky /// edges across a loop header. For example, threading a jump from outside the
538f22ef01cSRoman Divacky /// loop (the preheader) to an exit block of the loop is definitely profitable.
539f22ef01cSRoman Divacky /// It is also almost always profitable to thread backedges from within the loop
540f22ef01cSRoman Divacky /// to exit blocks, and is often profitable to thread backedges to other blocks
541f22ef01cSRoman Divacky /// within the loop (forming a nested loop). This simple analysis is not rich
542f22ef01cSRoman Divacky /// enough to track all of these properties and keep it up-to-date as the CFG
543f22ef01cSRoman Divacky /// mutates, so we don't allow any of these transformations.
FindLoopHeaders(Function & F)5443ca95b02SDimitry Andric void JumpThreadingPass::FindLoopHeaders(Function &F) {
545f22ef01cSRoman Divacky SmallVector<std::pair<const BasicBlock*,const BasicBlock*>, 32> Edges;
546f22ef01cSRoman Divacky FindFunctionBackedges(F, Edges);
547f22ef01cSRoman Divacky
548444ed5c5SDimitry Andric for (const auto &Edge : Edges)
549444ed5c5SDimitry Andric LoopHeaders.insert(Edge.second);
550f22ef01cSRoman Divacky }
551f22ef01cSRoman Divacky
5522754fe60SDimitry Andric /// getKnownConstant - Helper method to determine if we can thread over a
5532754fe60SDimitry Andric /// terminator with the given value as its condition, and if so what value to
5542754fe60SDimitry Andric /// use for that. What kind of value this is depends on whether we want an
5552754fe60SDimitry Andric /// integer or a block address, but an undef is always accepted.
5562754fe60SDimitry Andric /// Returns null if Val is null or not an appropriate constant.
getKnownConstant(Value * Val,ConstantPreference Preference)5572754fe60SDimitry Andric static Constant *getKnownConstant(Value *Val, ConstantPreference Preference) {
5582754fe60SDimitry Andric if (!Val)
55991bc56edSDimitry Andric return nullptr;
5602754fe60SDimitry Andric
5612754fe60SDimitry Andric // Undef is "known" enough.
5622754fe60SDimitry Andric if (UndefValue *U = dyn_cast<UndefValue>(Val))
5632754fe60SDimitry Andric return U;
5642754fe60SDimitry Andric
5652754fe60SDimitry Andric if (Preference == WantBlockAddress)
5662754fe60SDimitry Andric return dyn_cast<BlockAddress>(Val->stripPointerCasts());
5672754fe60SDimitry Andric
5682754fe60SDimitry Andric return dyn_cast<ConstantInt>(Val);
569e580952dSDimitry Andric }
570e580952dSDimitry Andric
571f22ef01cSRoman Divacky /// ComputeValueKnownInPredecessors - Given a basic block BB and a value V, see
5722754fe60SDimitry Andric /// if we can infer that the value is a known ConstantInt/BlockAddress or undef
5732754fe60SDimitry Andric /// in any of our predecessors. If so, return the known list of value and pred
5742754fe60SDimitry Andric /// BB in the result vector.
575f22ef01cSRoman Divacky ///
576f22ef01cSRoman Divacky /// This returns true if there were any known values.
ComputeValueKnownInPredecessorsImpl(Value * V,BasicBlock * BB,PredValueInfo & Result,ConstantPreference Preference,DenseSet<std::pair<Value *,BasicBlock * >> & RecursionSet,Instruction * CxtI)577*b5893f02SDimitry Andric bool JumpThreadingPass::ComputeValueKnownInPredecessorsImpl(
5783ca95b02SDimitry Andric Value *V, BasicBlock *BB, PredValueInfo &Result,
579*b5893f02SDimitry Andric ConstantPreference Preference,
580*b5893f02SDimitry Andric DenseSet<std::pair<Value *, BasicBlock *>> &RecursionSet,
581*b5893f02SDimitry Andric Instruction *CxtI) {
582e580952dSDimitry Andric // This method walks up use-def chains recursively. Because of this, we could
583e580952dSDimitry Andric // get into an infinite loop going around loops in the use-def chain. To
584e580952dSDimitry Andric // prevent this, keep track of what (value, block) pairs we've already visited
585e580952dSDimitry Andric // and terminate the search if we loop back to them
586e580952dSDimitry Andric if (!RecursionSet.insert(std::make_pair(V, BB)).second)
587e580952dSDimitry Andric return false;
588e580952dSDimitry Andric
5892754fe60SDimitry Andric // If V is a constant, then it is known in all predecessors.
5902754fe60SDimitry Andric if (Constant *KC = getKnownConstant(V, Preference)) {
591444ed5c5SDimitry Andric for (BasicBlock *Pred : predecessors(BB))
592444ed5c5SDimitry Andric Result.push_back(std::make_pair(KC, Pred));
593e580952dSDimitry Andric
5943ca95b02SDimitry Andric return !Result.empty();
595f22ef01cSRoman Divacky }
596f22ef01cSRoman Divacky
597f22ef01cSRoman Divacky // If V is a non-instruction value, or an instruction in a different block,
598f22ef01cSRoman Divacky // then it can't be derived from a PHI.
599f22ef01cSRoman Divacky Instruction *I = dyn_cast<Instruction>(V);
60091bc56edSDimitry Andric if (!I || I->getParent() != BB) {
601f22ef01cSRoman Divacky
602f22ef01cSRoman Divacky // Okay, if this is a live-in value, see if it has a known value at the end
603f22ef01cSRoman Divacky // of any of our predecessors.
604f22ef01cSRoman Divacky //
605f22ef01cSRoman Divacky // FIXME: This should be an edge property, not a block end property.
606f22ef01cSRoman Divacky /// TODO: Per PR2563, we could infer value range information about a
607f22ef01cSRoman Divacky /// predecessor based on its terminator.
608f22ef01cSRoman Divacky //
609f22ef01cSRoman Divacky // FIXME: change this to use the more-rich 'getPredicateOnEdge' method if
610f22ef01cSRoman Divacky // "I" is a non-local compare-with-a-constant instruction. This would be
611f22ef01cSRoman Divacky // able to handle value inequalities better, for example if the compare is
612f22ef01cSRoman Divacky // "X < 4" and "X < 3" is known true but "X < 4" itself is not available.
613f22ef01cSRoman Divacky // Perhaps getConstantOnEdge should be smart enough to do this?
614f22ef01cSRoman Divacky
615*b5893f02SDimitry Andric if (DTU->hasPendingDomTreeUpdates())
6164ba319b5SDimitry Andric LVI->disableDT();
6174ba319b5SDimitry Andric else
6184ba319b5SDimitry Andric LVI->enableDT();
619444ed5c5SDimitry Andric for (BasicBlock *P : predecessors(BB)) {
620f22ef01cSRoman Divacky // If the value is known by LazyValueInfo to be a constant in a
621f22ef01cSRoman Divacky // predecessor, use that information to try to thread this block.
62239d628a0SDimitry Andric Constant *PredCst = LVI->getConstantOnEdge(V, P, BB, CxtI);
6232754fe60SDimitry Andric if (Constant *KC = getKnownConstant(PredCst, Preference))
6242754fe60SDimitry Andric Result.push_back(std::make_pair(KC, P));
625f22ef01cSRoman Divacky }
626f22ef01cSRoman Divacky
627f22ef01cSRoman Divacky return !Result.empty();
628f22ef01cSRoman Divacky }
629f22ef01cSRoman Divacky
630f22ef01cSRoman Divacky /// If I is a PHI node, then we know the incoming values for any constants.
631f22ef01cSRoman Divacky if (PHINode *PN = dyn_cast<PHINode>(I)) {
632*b5893f02SDimitry Andric if (DTU->hasPendingDomTreeUpdates())
6334ba319b5SDimitry Andric LVI->disableDT();
6344ba319b5SDimitry Andric else
6354ba319b5SDimitry Andric LVI->enableDT();
636f22ef01cSRoman Divacky for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
637f22ef01cSRoman Divacky Value *InVal = PN->getIncomingValue(i);
6382754fe60SDimitry Andric if (Constant *KC = getKnownConstant(InVal, Preference)) {
6392754fe60SDimitry Andric Result.push_back(std::make_pair(KC, PN->getIncomingBlock(i)));
6402754fe60SDimitry Andric } else {
641e580952dSDimitry Andric Constant *CI = LVI->getConstantOnEdge(InVal,
64239d628a0SDimitry Andric PN->getIncomingBlock(i),
64339d628a0SDimitry Andric BB, CxtI);
6442754fe60SDimitry Andric if (Constant *KC = getKnownConstant(CI, Preference))
6452754fe60SDimitry Andric Result.push_back(std::make_pair(KC, PN->getIncomingBlock(i)));
646f22ef01cSRoman Divacky }
647f22ef01cSRoman Divacky }
648e580952dSDimitry Andric
649f22ef01cSRoman Divacky return !Result.empty();
650f22ef01cSRoman Divacky }
651f22ef01cSRoman Divacky
6523ca95b02SDimitry Andric // Handle Cast instructions. Only see through Cast when the source operand is
6534ba319b5SDimitry Andric // PHI or Cmp to save the compilation time.
6543ca95b02SDimitry Andric if (CastInst *CI = dyn_cast<CastInst>(I)) {
6553ca95b02SDimitry Andric Value *Source = CI->getOperand(0);
6563ca95b02SDimitry Andric if (!isa<PHINode>(Source) && !isa<CmpInst>(Source))
6573ca95b02SDimitry Andric return false;
658*b5893f02SDimitry Andric ComputeValueKnownInPredecessorsImpl(Source, BB, Result, Preference,
659*b5893f02SDimitry Andric RecursionSet, CxtI);
6603ca95b02SDimitry Andric if (Result.empty())
6613ca95b02SDimitry Andric return false;
6623ca95b02SDimitry Andric
6633ca95b02SDimitry Andric // Convert the known values.
6643ca95b02SDimitry Andric for (auto &R : Result)
6653ca95b02SDimitry Andric R.first = ConstantExpr::getCast(CI->getOpcode(), R.first, CI->getType());
6663ca95b02SDimitry Andric
6673ca95b02SDimitry Andric return true;
6683ca95b02SDimitry Andric }
6693ca95b02SDimitry Andric
670f22ef01cSRoman Divacky // Handle some boolean conditions.
671f22ef01cSRoman Divacky if (I->getType()->getPrimitiveSizeInBits() == 1) {
6722754fe60SDimitry Andric assert(Preference == WantInteger && "One-bit non-integer type?");
673f22ef01cSRoman Divacky // X | true -> true
674f22ef01cSRoman Divacky // X & false -> false
675f22ef01cSRoman Divacky if (I->getOpcode() == Instruction::Or ||
676f22ef01cSRoman Divacky I->getOpcode() == Instruction::And) {
6772cab237bSDimitry Andric PredValueInfoTy LHSVals, RHSVals;
6782cab237bSDimitry Andric
679*b5893f02SDimitry Andric ComputeValueKnownInPredecessorsImpl(I->getOperand(0), BB, LHSVals,
680*b5893f02SDimitry Andric WantInteger, RecursionSet, CxtI);
681*b5893f02SDimitry Andric ComputeValueKnownInPredecessorsImpl(I->getOperand(1), BB, RHSVals,
682*b5893f02SDimitry Andric WantInteger, RecursionSet, CxtI);
683f22ef01cSRoman Divacky
684f22ef01cSRoman Divacky if (LHSVals.empty() && RHSVals.empty())
685f22ef01cSRoman Divacky return false;
686f22ef01cSRoman Divacky
687f22ef01cSRoman Divacky ConstantInt *InterestingVal;
688f22ef01cSRoman Divacky if (I->getOpcode() == Instruction::Or)
689f22ef01cSRoman Divacky InterestingVal = ConstantInt::getTrue(I->getContext());
690f22ef01cSRoman Divacky else
691f22ef01cSRoman Divacky InterestingVal = ConstantInt::getFalse(I->getContext());
692f22ef01cSRoman Divacky
693e580952dSDimitry Andric SmallPtrSet<BasicBlock*, 4> LHSKnownBBs;
694e580952dSDimitry Andric
695f22ef01cSRoman Divacky // Scan for the sentinel. If we find an undef, force it to the
696f22ef01cSRoman Divacky // interesting value: x|undef -> true and x&undef -> false.
697444ed5c5SDimitry Andric for (const auto &LHSVal : LHSVals)
698444ed5c5SDimitry Andric if (LHSVal.first == InterestingVal || isa<UndefValue>(LHSVal.first)) {
699444ed5c5SDimitry Andric Result.emplace_back(InterestingVal, LHSVal.second);
700444ed5c5SDimitry Andric LHSKnownBBs.insert(LHSVal.second);
701f22ef01cSRoman Divacky }
702444ed5c5SDimitry Andric for (const auto &RHSVal : RHSVals)
703444ed5c5SDimitry Andric if (RHSVal.first == InterestingVal || isa<UndefValue>(RHSVal.first)) {
704ffd1746dSEd Schouten // If we already inferred a value for this block on the LHS, don't
705ffd1746dSEd Schouten // re-add it.
706444ed5c5SDimitry Andric if (!LHSKnownBBs.count(RHSVal.second))
707444ed5c5SDimitry Andric Result.emplace_back(InterestingVal, RHSVal.second);
708ffd1746dSEd Schouten }
709e580952dSDimitry Andric
710f22ef01cSRoman Divacky return !Result.empty();
711f22ef01cSRoman Divacky }
712f22ef01cSRoman Divacky
713f22ef01cSRoman Divacky // Handle the NOT form of XOR.
714f22ef01cSRoman Divacky if (I->getOpcode() == Instruction::Xor &&
715f22ef01cSRoman Divacky isa<ConstantInt>(I->getOperand(1)) &&
716f22ef01cSRoman Divacky cast<ConstantInt>(I->getOperand(1))->isOne()) {
717*b5893f02SDimitry Andric ComputeValueKnownInPredecessorsImpl(I->getOperand(0), BB, Result,
718*b5893f02SDimitry Andric WantInteger, RecursionSet, CxtI);
719f22ef01cSRoman Divacky if (Result.empty())
720f22ef01cSRoman Divacky return false;
721f22ef01cSRoman Divacky
722f22ef01cSRoman Divacky // Invert the known values.
723444ed5c5SDimitry Andric for (auto &R : Result)
724444ed5c5SDimitry Andric R.first = ConstantExpr::getNot(R.first);
725e580952dSDimitry Andric
726f22ef01cSRoman Divacky return true;
727f22ef01cSRoman Divacky }
728e580952dSDimitry Andric
729e580952dSDimitry Andric // Try to simplify some other binary operator values.
730e580952dSDimitry Andric } else if (BinaryOperator *BO = dyn_cast<BinaryOperator>(I)) {
7312754fe60SDimitry Andric assert(Preference != WantBlockAddress
7322754fe60SDimitry Andric && "A binary operator creating a block address?");
733e580952dSDimitry Andric if (ConstantInt *CI = dyn_cast<ConstantInt>(BO->getOperand(1))) {
7342754fe60SDimitry Andric PredValueInfoTy LHSVals;
735*b5893f02SDimitry Andric ComputeValueKnownInPredecessorsImpl(BO->getOperand(0), BB, LHSVals,
736*b5893f02SDimitry Andric WantInteger, RecursionSet, CxtI);
737e580952dSDimitry Andric
738e580952dSDimitry Andric // Try to use constant folding to simplify the binary operator.
739444ed5c5SDimitry Andric for (const auto &LHSVal : LHSVals) {
740444ed5c5SDimitry Andric Constant *V = LHSVal.first;
741e580952dSDimitry Andric Constant *Folded = ConstantExpr::get(BO->getOpcode(), V, CI);
742e580952dSDimitry Andric
7432754fe60SDimitry Andric if (Constant *KC = getKnownConstant(Folded, WantInteger))
744444ed5c5SDimitry Andric Result.push_back(std::make_pair(KC, LHSVal.second));
745e580952dSDimitry Andric }
746e580952dSDimitry Andric }
747e580952dSDimitry Andric
748e580952dSDimitry Andric return !Result.empty();
749f22ef01cSRoman Divacky }
750f22ef01cSRoman Divacky
751f22ef01cSRoman Divacky // Handle compare with phi operand, where the PHI is defined in this block.
752f22ef01cSRoman Divacky if (CmpInst *Cmp = dyn_cast<CmpInst>(I)) {
7532754fe60SDimitry Andric assert(Preference == WantInteger && "Compares only produce integers");
754edd7eaddSDimitry Andric Type *CmpType = Cmp->getType();
755edd7eaddSDimitry Andric Value *CmpLHS = Cmp->getOperand(0);
756edd7eaddSDimitry Andric Value *CmpRHS = Cmp->getOperand(1);
757edd7eaddSDimitry Andric CmpInst::Predicate Pred = Cmp->getPredicate();
758edd7eaddSDimitry Andric
759edd7eaddSDimitry Andric PHINode *PN = dyn_cast<PHINode>(CmpLHS);
7604ba319b5SDimitry Andric if (!PN)
7614ba319b5SDimitry Andric PN = dyn_cast<PHINode>(CmpRHS);
762f22ef01cSRoman Divacky if (PN && PN->getParent() == BB) {
763ff0cc061SDimitry Andric const DataLayout &DL = PN->getModule()->getDataLayout();
764f22ef01cSRoman Divacky // We can do this simplification if any comparisons fold to true or false.
765f22ef01cSRoman Divacky // See if any do.
766*b5893f02SDimitry Andric if (DTU->hasPendingDomTreeUpdates())
7674ba319b5SDimitry Andric LVI->disableDT();
7684ba319b5SDimitry Andric else
7694ba319b5SDimitry Andric LVI->enableDT();
770f22ef01cSRoman Divacky for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
771f22ef01cSRoman Divacky BasicBlock *PredBB = PN->getIncomingBlock(i);
7724ba319b5SDimitry Andric Value *LHS, *RHS;
7734ba319b5SDimitry Andric if (PN == CmpLHS) {
7744ba319b5SDimitry Andric LHS = PN->getIncomingValue(i);
7754ba319b5SDimitry Andric RHS = CmpRHS->DoPHITranslation(BB, PredBB);
7764ba319b5SDimitry Andric } else {
7774ba319b5SDimitry Andric LHS = CmpLHS->DoPHITranslation(BB, PredBB);
7784ba319b5SDimitry Andric RHS = PN->getIncomingValue(i);
7794ba319b5SDimitry Andric }
780edd7eaddSDimitry Andric Value *Res = SimplifyCmpInst(Pred, LHS, RHS, {DL});
78191bc56edSDimitry Andric if (!Res) {
7822754fe60SDimitry Andric if (!isa<Constant>(RHS))
783f22ef01cSRoman Divacky continue;
784f22ef01cSRoman Divacky
7854ba319b5SDimitry Andric // getPredicateOnEdge call will make no sense if LHS is defined in BB.
7864ba319b5SDimitry Andric auto LHSInst = dyn_cast<Instruction>(LHS);
7874ba319b5SDimitry Andric if (LHSInst && LHSInst->getParent() == BB)
7884ba319b5SDimitry Andric continue;
7894ba319b5SDimitry Andric
790f22ef01cSRoman Divacky LazyValueInfo::Tristate
791edd7eaddSDimitry Andric ResT = LVI->getPredicateOnEdge(Pred, LHS,
79239d628a0SDimitry Andric cast<Constant>(RHS), PredBB, BB,
79339d628a0SDimitry Andric CxtI ? CxtI : Cmp);
794f22ef01cSRoman Divacky if (ResT == LazyValueInfo::Unknown)
795f22ef01cSRoman Divacky continue;
796f22ef01cSRoman Divacky Res = ConstantInt::get(Type::getInt1Ty(LHS->getContext()), ResT);
797f22ef01cSRoman Divacky }
798f22ef01cSRoman Divacky
7992754fe60SDimitry Andric if (Constant *KC = getKnownConstant(Res, WantInteger))
8002754fe60SDimitry Andric Result.push_back(std::make_pair(KC, PredBB));
801f22ef01cSRoman Divacky }
802f22ef01cSRoman Divacky
803f22ef01cSRoman Divacky return !Result.empty();
804f22ef01cSRoman Divacky }
805f22ef01cSRoman Divacky
806f22ef01cSRoman Divacky // If comparing a live-in value against a constant, see if we know the
807f22ef01cSRoman Divacky // live-in value on any predecessors.
808edd7eaddSDimitry Andric if (isa<Constant>(CmpRHS) && !CmpType->isVectorTy()) {
809edd7eaddSDimitry Andric Constant *CmpConst = cast<Constant>(CmpRHS);
8100f5676f4SDimitry Andric
811edd7eaddSDimitry Andric if (!isa<Instruction>(CmpLHS) ||
812edd7eaddSDimitry Andric cast<Instruction>(CmpLHS)->getParent() != BB) {
813*b5893f02SDimitry Andric if (DTU->hasPendingDomTreeUpdates())
8144ba319b5SDimitry Andric LVI->disableDT();
8154ba319b5SDimitry Andric else
8164ba319b5SDimitry Andric LVI->enableDT();
817444ed5c5SDimitry Andric for (BasicBlock *P : predecessors(BB)) {
818f22ef01cSRoman Divacky // If the value is known by LazyValueInfo to be a constant in a
819f22ef01cSRoman Divacky // predecessor, use that information to try to thread this block.
820e580952dSDimitry Andric LazyValueInfo::Tristate Res =
821edd7eaddSDimitry Andric LVI->getPredicateOnEdge(Pred, CmpLHS,
8220f5676f4SDimitry Andric CmpConst, P, BB, CxtI ? CxtI : Cmp);
823f22ef01cSRoman Divacky if (Res == LazyValueInfo::Unknown)
824f22ef01cSRoman Divacky continue;
825f22ef01cSRoman Divacky
826edd7eaddSDimitry Andric Constant *ResC = ConstantInt::get(CmpType, Res);
8272754fe60SDimitry Andric Result.push_back(std::make_pair(ResC, P));
828f22ef01cSRoman Divacky }
829f22ef01cSRoman Divacky
830f22ef01cSRoman Divacky return !Result.empty();
831f22ef01cSRoman Divacky }
832e580952dSDimitry Andric
833edd7eaddSDimitry Andric // InstCombine can fold some forms of constant range checks into
834edd7eaddSDimitry Andric // (icmp (add (x, C1)), C2). See if we have we have such a thing with
835edd7eaddSDimitry Andric // x as a live-in.
836edd7eaddSDimitry Andric {
837edd7eaddSDimitry Andric using namespace PatternMatch;
8382cab237bSDimitry Andric
839edd7eaddSDimitry Andric Value *AddLHS;
840edd7eaddSDimitry Andric ConstantInt *AddConst;
841edd7eaddSDimitry Andric if (isa<ConstantInt>(CmpConst) &&
842edd7eaddSDimitry Andric match(CmpLHS, m_Add(m_Value(AddLHS), m_ConstantInt(AddConst)))) {
843edd7eaddSDimitry Andric if (!isa<Instruction>(AddLHS) ||
844edd7eaddSDimitry Andric cast<Instruction>(AddLHS)->getParent() != BB) {
845*b5893f02SDimitry Andric if (DTU->hasPendingDomTreeUpdates())
8464ba319b5SDimitry Andric LVI->disableDT();
8474ba319b5SDimitry Andric else
8484ba319b5SDimitry Andric LVI->enableDT();
849edd7eaddSDimitry Andric for (BasicBlock *P : predecessors(BB)) {
850edd7eaddSDimitry Andric // If the value is known by LazyValueInfo to be a ConstantRange in
851edd7eaddSDimitry Andric // a predecessor, use that information to try to thread this
852edd7eaddSDimitry Andric // block.
853edd7eaddSDimitry Andric ConstantRange CR = LVI->getConstantRangeOnEdge(
854edd7eaddSDimitry Andric AddLHS, P, BB, CxtI ? CxtI : cast<Instruction>(CmpLHS));
855edd7eaddSDimitry Andric // Propagate the range through the addition.
856edd7eaddSDimitry Andric CR = CR.add(AddConst->getValue());
857edd7eaddSDimitry Andric
858edd7eaddSDimitry Andric // Get the range where the compare returns true.
859edd7eaddSDimitry Andric ConstantRange CmpRange = ConstantRange::makeExactICmpRegion(
860edd7eaddSDimitry Andric Pred, cast<ConstantInt>(CmpConst)->getValue());
861edd7eaddSDimitry Andric
862edd7eaddSDimitry Andric Constant *ResC;
863edd7eaddSDimitry Andric if (CmpRange.contains(CR))
864edd7eaddSDimitry Andric ResC = ConstantInt::getTrue(CmpType);
865edd7eaddSDimitry Andric else if (CmpRange.inverse().contains(CR))
866edd7eaddSDimitry Andric ResC = ConstantInt::getFalse(CmpType);
867edd7eaddSDimitry Andric else
868edd7eaddSDimitry Andric continue;
869edd7eaddSDimitry Andric
870edd7eaddSDimitry Andric Result.push_back(std::make_pair(ResC, P));
871edd7eaddSDimitry Andric }
872edd7eaddSDimitry Andric
873edd7eaddSDimitry Andric return !Result.empty();
874edd7eaddSDimitry Andric }
875edd7eaddSDimitry Andric }
876edd7eaddSDimitry Andric }
877edd7eaddSDimitry Andric
878e580952dSDimitry Andric // Try to find a constant value for the LHS of a comparison,
879e580952dSDimitry Andric // and evaluate it statically if we can.
8802754fe60SDimitry Andric PredValueInfoTy LHSVals;
881*b5893f02SDimitry Andric ComputeValueKnownInPredecessorsImpl(I->getOperand(0), BB, LHSVals,
882*b5893f02SDimitry Andric WantInteger, RecursionSet, CxtI);
883e580952dSDimitry Andric
884444ed5c5SDimitry Andric for (const auto &LHSVal : LHSVals) {
885444ed5c5SDimitry Andric Constant *V = LHSVal.first;
886edd7eaddSDimitry Andric Constant *Folded = ConstantExpr::getCompare(Pred, V, CmpConst);
8872754fe60SDimitry Andric if (Constant *KC = getKnownConstant(Folded, WantInteger))
888444ed5c5SDimitry Andric Result.push_back(std::make_pair(KC, LHSVal.second));
889f22ef01cSRoman Divacky }
890e580952dSDimitry Andric
891e580952dSDimitry Andric return !Result.empty();
892e580952dSDimitry Andric }
893e580952dSDimitry Andric }
894e580952dSDimitry Andric
8952754fe60SDimitry Andric if (SelectInst *SI = dyn_cast<SelectInst>(I)) {
8962754fe60SDimitry Andric // Handle select instructions where at least one operand is a known constant
8972754fe60SDimitry Andric // and we can figure out the condition value for any predecessor block.
8982754fe60SDimitry Andric Constant *TrueVal = getKnownConstant(SI->getTrueValue(), Preference);
8992754fe60SDimitry Andric Constant *FalseVal = getKnownConstant(SI->getFalseValue(), Preference);
9002754fe60SDimitry Andric PredValueInfoTy Conds;
9012754fe60SDimitry Andric if ((TrueVal || FalseVal) &&
902*b5893f02SDimitry Andric ComputeValueKnownInPredecessorsImpl(SI->getCondition(), BB, Conds,
903*b5893f02SDimitry Andric WantInteger, RecursionSet, CxtI)) {
904444ed5c5SDimitry Andric for (auto &C : Conds) {
905444ed5c5SDimitry Andric Constant *Cond = C.first;
9062754fe60SDimitry Andric
9072754fe60SDimitry Andric // Figure out what value to use for the condition.
9082754fe60SDimitry Andric bool KnownCond;
9092754fe60SDimitry Andric if (ConstantInt *CI = dyn_cast<ConstantInt>(Cond)) {
9102754fe60SDimitry Andric // A known boolean.
9112754fe60SDimitry Andric KnownCond = CI->isOne();
9122754fe60SDimitry Andric } else {
9132754fe60SDimitry Andric assert(isa<UndefValue>(Cond) && "Unexpected condition value");
9142754fe60SDimitry Andric // Either operand will do, so be sure to pick the one that's a known
9152754fe60SDimitry Andric // constant.
9162754fe60SDimitry Andric // FIXME: Do this more cleverly if both values are known constants?
91791bc56edSDimitry Andric KnownCond = (TrueVal != nullptr);
9182754fe60SDimitry Andric }
9192754fe60SDimitry Andric
9202754fe60SDimitry Andric // See if the select has a known constant value for this predecessor.
9212754fe60SDimitry Andric if (Constant *Val = KnownCond ? TrueVal : FalseVal)
922444ed5c5SDimitry Andric Result.push_back(std::make_pair(Val, C.second));
9232754fe60SDimitry Andric }
9242754fe60SDimitry Andric
9252754fe60SDimitry Andric return !Result.empty();
9262754fe60SDimitry Andric }
9272754fe60SDimitry Andric }
9282754fe60SDimitry Andric
929e580952dSDimitry Andric // If all else fails, see if LVI can figure out a constant value for us.
930*b5893f02SDimitry Andric if (DTU->hasPendingDomTreeUpdates())
9314ba319b5SDimitry Andric LVI->disableDT();
9324ba319b5SDimitry Andric else
9334ba319b5SDimitry Andric LVI->enableDT();
93439d628a0SDimitry Andric Constant *CI = LVI->getConstant(V, BB, CxtI);
9352754fe60SDimitry Andric if (Constant *KC = getKnownConstant(CI, Preference)) {
936444ed5c5SDimitry Andric for (BasicBlock *Pred : predecessors(BB))
937444ed5c5SDimitry Andric Result.push_back(std::make_pair(KC, Pred));
938e580952dSDimitry Andric }
939e580952dSDimitry Andric
940e580952dSDimitry Andric return !Result.empty();
941e580952dSDimitry Andric }
942e580952dSDimitry Andric
943f22ef01cSRoman Divacky /// GetBestDestForBranchOnUndef - If we determine that the specified block ends
944f22ef01cSRoman Divacky /// in an undefined jump, decide which block is best to revector to.
945f22ef01cSRoman Divacky ///
946f22ef01cSRoman Divacky /// Since we can pick an arbitrary destination, we pick the successor with the
947f22ef01cSRoman Divacky /// fewest predecessors. This should reduce the in-degree of the others.
GetBestDestForJumpOnUndef(BasicBlock * BB)948f22ef01cSRoman Divacky static unsigned GetBestDestForJumpOnUndef(BasicBlock *BB) {
949*b5893f02SDimitry Andric Instruction *BBTerm = BB->getTerminator();
950f22ef01cSRoman Divacky unsigned MinSucc = 0;
951f22ef01cSRoman Divacky BasicBlock *TestBB = BBTerm->getSuccessor(MinSucc);
952f22ef01cSRoman Divacky // Compute the successor with the minimum number of predecessors.
9534ba319b5SDimitry Andric unsigned MinNumPreds = pred_size(TestBB);
954f22ef01cSRoman Divacky for (unsigned i = 1, e = BBTerm->getNumSuccessors(); i != e; ++i) {
955f22ef01cSRoman Divacky TestBB = BBTerm->getSuccessor(i);
9564ba319b5SDimitry Andric unsigned NumPreds = pred_size(TestBB);
95717a519f9SDimitry Andric if (NumPreds < MinNumPreds) {
958f22ef01cSRoman Divacky MinSucc = i;
95917a519f9SDimitry Andric MinNumPreds = NumPreds;
96017a519f9SDimitry Andric }
961f22ef01cSRoman Divacky }
962f22ef01cSRoman Divacky
963f22ef01cSRoman Divacky return MinSucc;
964f22ef01cSRoman Divacky }
965f22ef01cSRoman Divacky
hasAddressTakenAndUsed(BasicBlock * BB)9662754fe60SDimitry Andric static bool hasAddressTakenAndUsed(BasicBlock *BB) {
9672754fe60SDimitry Andric if (!BB->hasAddressTaken()) return false;
9682754fe60SDimitry Andric
9692754fe60SDimitry Andric // If the block has its address taken, it may be a tree of dead constants
9702754fe60SDimitry Andric // hanging off of it. These shouldn't keep the block alive.
9712754fe60SDimitry Andric BlockAddress *BA = BlockAddress::get(BB);
9722754fe60SDimitry Andric BA->removeDeadConstantUsers();
9732754fe60SDimitry Andric return !BA->use_empty();
9742754fe60SDimitry Andric }
9752754fe60SDimitry Andric
976f22ef01cSRoman Divacky /// ProcessBlock - If there are any predecessors whose control can be threaded
977f22ef01cSRoman Divacky /// through to a successor, transform them now.
ProcessBlock(BasicBlock * BB)9783ca95b02SDimitry Andric bool JumpThreadingPass::ProcessBlock(BasicBlock *BB) {
979f22ef01cSRoman Divacky // If the block is trivially dead, just return and let the caller nuke it.
980f22ef01cSRoman Divacky // This simplifies other transformations.
981*b5893f02SDimitry Andric if (DTU->isBBPendingDeletion(BB) ||
9824ba319b5SDimitry Andric (pred_empty(BB) && BB != &BB->getParent()->getEntryBlock()))
983f22ef01cSRoman Divacky return false;
984f22ef01cSRoman Divacky
985f22ef01cSRoman Divacky // If this block has a single predecessor, and if that pred has a single
986f22ef01cSRoman Divacky // successor, merge the blocks. This encourages recursive jump threading
987f22ef01cSRoman Divacky // because now the condition in this block can be threaded through
988f22ef01cSRoman Divacky // predecessors of our predecessor block.
989f22ef01cSRoman Divacky if (BasicBlock *SinglePred = BB->getSinglePredecessor()) {
990*b5893f02SDimitry Andric const Instruction *TI = SinglePred->getTerminator();
991*b5893f02SDimitry Andric if (!TI->isExceptionalTerminator() && TI->getNumSuccessors() == 1 &&
9922754fe60SDimitry Andric SinglePred != BB && !hasAddressTakenAndUsed(BB)) {
993f22ef01cSRoman Divacky // If SinglePred was a loop header, BB becomes one.
994f22ef01cSRoman Divacky if (LoopHeaders.erase(SinglePred))
995f22ef01cSRoman Divacky LoopHeaders.insert(BB);
996f22ef01cSRoman Divacky
9972754fe60SDimitry Andric LVI->eraseBlock(SinglePred);
998*b5893f02SDimitry Andric MergeBasicBlockIntoOnlyPred(BB, DTU);
999f22ef01cSRoman Divacky
1000edd7eaddSDimitry Andric // Now that BB is merged into SinglePred (i.e. SinglePred Code followed by
1001edd7eaddSDimitry Andric // BB code within one basic block `BB`), we need to invalidate the LVI
1002edd7eaddSDimitry Andric // information associated with BB, because the LVI information need not be
1003edd7eaddSDimitry Andric // true for all of BB after the merge. For example,
1004edd7eaddSDimitry Andric // Before the merge, LVI info and code is as follows:
1005edd7eaddSDimitry Andric // SinglePred: <LVI info1 for %p val>
1006edd7eaddSDimitry Andric // %y = use of %p
1007edd7eaddSDimitry Andric // call @exit() // need not transfer execution to successor.
1008edd7eaddSDimitry Andric // assume(%p) // from this point on %p is true
1009edd7eaddSDimitry Andric // br label %BB
1010edd7eaddSDimitry Andric // BB: <LVI info2 for %p val, i.e. %p is true>
1011edd7eaddSDimitry Andric // %x = use of %p
1012edd7eaddSDimitry Andric // br label exit
1013edd7eaddSDimitry Andric //
1014edd7eaddSDimitry Andric // Note that this LVI info for blocks BB and SinglPred is correct for %p
1015edd7eaddSDimitry Andric // (info2 and info1 respectively). After the merge and the deletion of the
1016edd7eaddSDimitry Andric // LVI info1 for SinglePred. We have the following code:
1017edd7eaddSDimitry Andric // BB: <LVI info2 for %p val>
1018edd7eaddSDimitry Andric // %y = use of %p
1019edd7eaddSDimitry Andric // call @exit()
1020edd7eaddSDimitry Andric // assume(%p)
1021edd7eaddSDimitry Andric // %x = use of %p <-- LVI info2 is correct from here onwards.
1022edd7eaddSDimitry Andric // br label exit
1023edd7eaddSDimitry Andric // LVI info2 for BB is incorrect at the beginning of BB.
1024edd7eaddSDimitry Andric
1025edd7eaddSDimitry Andric // Invalidate LVI information for BB if the LVI is not provably true for
1026edd7eaddSDimitry Andric // all of BB.
10274ba319b5SDimitry Andric if (!isGuaranteedToTransferExecutionToSuccessor(BB))
1028edd7eaddSDimitry Andric LVI->eraseBlock(BB);
1029f22ef01cSRoman Divacky return true;
1030f22ef01cSRoman Divacky }
1031f22ef01cSRoman Divacky }
1032f22ef01cSRoman Divacky
1033444ed5c5SDimitry Andric if (TryToUnfoldSelectInCurrBB(BB))
1034444ed5c5SDimitry Andric return true;
1035444ed5c5SDimitry Andric
10367a7e6055SDimitry Andric // Look if we can propagate guards to predecessors.
10377a7e6055SDimitry Andric if (HasGuards && ProcessGuards(BB))
10387a7e6055SDimitry Andric return true;
10397a7e6055SDimitry Andric
10402754fe60SDimitry Andric // What kind of constant we're looking for.
10412754fe60SDimitry Andric ConstantPreference Preference = WantInteger;
10422754fe60SDimitry Andric
10432754fe60SDimitry Andric // Look to see if the terminator is a conditional branch, switch or indirect
10442754fe60SDimitry Andric // branch, if not we can't thread it.
1045f22ef01cSRoman Divacky Value *Condition;
10462754fe60SDimitry Andric Instruction *Terminator = BB->getTerminator();
10472754fe60SDimitry Andric if (BranchInst *BI = dyn_cast<BranchInst>(Terminator)) {
1048f22ef01cSRoman Divacky // Can't thread an unconditional jump.
1049f22ef01cSRoman Divacky if (BI->isUnconditional()) return false;
1050f22ef01cSRoman Divacky Condition = BI->getCondition();
10512754fe60SDimitry Andric } else if (SwitchInst *SI = dyn_cast<SwitchInst>(Terminator)) {
1052f22ef01cSRoman Divacky Condition = SI->getCondition();
10532754fe60SDimitry Andric } else if (IndirectBrInst *IB = dyn_cast<IndirectBrInst>(Terminator)) {
10547ae0e2c9SDimitry Andric // Can't thread indirect branch with no successors.
10557ae0e2c9SDimitry Andric if (IB->getNumSuccessors() == 0) return false;
10562754fe60SDimitry Andric Condition = IB->getAddress()->stripPointerCasts();
10572754fe60SDimitry Andric Preference = WantBlockAddress;
10582754fe60SDimitry Andric } else {
1059f22ef01cSRoman Divacky return false; // Must be an invoke.
1060f22ef01cSRoman Divacky }
1061f22ef01cSRoman Divacky
10623b0f4066SDimitry Andric // Run constant folding to see if we can reduce the condition to a simple
10633b0f4066SDimitry Andric // constant.
10643b0f4066SDimitry Andric if (Instruction *I = dyn_cast<Instruction>(Condition)) {
1065ff0cc061SDimitry Andric Value *SimpleVal =
1066ff0cc061SDimitry Andric ConstantFoldInstruction(I, BB->getModule()->getDataLayout(), TLI);
10673b0f4066SDimitry Andric if (SimpleVal) {
10683b0f4066SDimitry Andric I->replaceAllUsesWith(SimpleVal);
10696c4bc1bdSDimitry Andric if (isInstructionTriviallyDead(I, TLI))
10703b0f4066SDimitry Andric I->eraseFromParent();
10713b0f4066SDimitry Andric Condition = SimpleVal;
10723b0f4066SDimitry Andric }
10733b0f4066SDimitry Andric }
10743b0f4066SDimitry Andric
1075f22ef01cSRoman Divacky // If the terminator is branching on an undef, we can pick any of the
1076f22ef01cSRoman Divacky // successors to branch to. Let GetBestDestForJumpOnUndef decide.
1077f22ef01cSRoman Divacky if (isa<UndefValue>(Condition)) {
1078f22ef01cSRoman Divacky unsigned BestSucc = GetBestDestForJumpOnUndef(BB);
10794ba319b5SDimitry Andric std::vector<DominatorTree::UpdateType> Updates;
1080f22ef01cSRoman Divacky
1081f22ef01cSRoman Divacky // Fold the branch/switch.
1082*b5893f02SDimitry Andric Instruction *BBTerm = BB->getTerminator();
10834ba319b5SDimitry Andric Updates.reserve(BBTerm->getNumSuccessors());
1084f22ef01cSRoman Divacky for (unsigned i = 0, e = BBTerm->getNumSuccessors(); i != e; ++i) {
1085f22ef01cSRoman Divacky if (i == BestSucc) continue;
10864ba319b5SDimitry Andric BasicBlock *Succ = BBTerm->getSuccessor(i);
10874ba319b5SDimitry Andric Succ->removePredecessor(BB, true);
10884ba319b5SDimitry Andric Updates.push_back({DominatorTree::Delete, BB, Succ});
1089f22ef01cSRoman Divacky }
1090f22ef01cSRoman Divacky
10914ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << " In block '" << BB->getName()
1092f22ef01cSRoman Divacky << "' folding undef terminator: " << *BBTerm << '\n');
1093f22ef01cSRoman Divacky BranchInst::Create(BBTerm->getSuccessor(BestSucc), BBTerm);
1094f22ef01cSRoman Divacky BBTerm->eraseFromParent();
1095*b5893f02SDimitry Andric DTU->applyUpdates(Updates);
1096f22ef01cSRoman Divacky return true;
1097f22ef01cSRoman Divacky }
1098f22ef01cSRoman Divacky
10992754fe60SDimitry Andric // If the terminator of this block is branching on a constant, simplify the
11002754fe60SDimitry Andric // terminator to an unconditional branch. This can occur due to threading in
11012754fe60SDimitry Andric // other blocks.
11022754fe60SDimitry Andric if (getKnownConstant(Condition, Preference)) {
11034ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << " In block '" << BB->getName()
11044ba319b5SDimitry Andric << "' folding terminator: " << *BB->getTerminator()
11054ba319b5SDimitry Andric << '\n');
11062754fe60SDimitry Andric ++NumFolds;
1107*b5893f02SDimitry Andric ConstantFoldTerminator(BB, true, nullptr, DTU);
11082754fe60SDimitry Andric return true;
11092754fe60SDimitry Andric }
1110f22ef01cSRoman Divacky
11112754fe60SDimitry Andric Instruction *CondInst = dyn_cast<Instruction>(Condition);
1112f22ef01cSRoman Divacky
1113f22ef01cSRoman Divacky // All the rest of our checks depend on the condition being an instruction.
111491bc56edSDimitry Andric if (!CondInst) {
1115f22ef01cSRoman Divacky // FIXME: Unify this with code below.
111639d628a0SDimitry Andric if (ProcessThreadableEdges(Condition, BB, Preference, Terminator))
1117f22ef01cSRoman Divacky return true;
1118f22ef01cSRoman Divacky return false;
1119f22ef01cSRoman Divacky }
1120f22ef01cSRoman Divacky
1121f22ef01cSRoman Divacky if (CmpInst *CondCmp = dyn_cast<CmpInst>(CondInst)) {
11228f0fd8f6SDimitry Andric // If we're branching on a conditional, LVI might be able to determine
11238f0fd8f6SDimitry Andric // it's value at the branch instruction. We only handle comparisons
11248f0fd8f6SDimitry Andric // against a constant at this time.
11258f0fd8f6SDimitry Andric // TODO: This should be extended to handle switches as well.
1126e580952dSDimitry Andric BranchInst *CondBr = dyn_cast<BranchInst>(BB->getTerminator());
1127e580952dSDimitry Andric Constant *CondConst = dyn_cast<Constant>(CondCmp->getOperand(1));
11287a7e6055SDimitry Andric if (CondBr && CondConst) {
11297a7e6055SDimitry Andric // We should have returned as soon as we turn a conditional branch to
11307a7e6055SDimitry Andric // unconditional. Because its no longer interesting as far as jump
11317a7e6055SDimitry Andric // threading is concerned.
11327a7e6055SDimitry Andric assert(CondBr->isConditional() && "Threading on unconditional terminator");
11337a7e6055SDimitry Andric
1134*b5893f02SDimitry Andric if (DTU->hasPendingDomTreeUpdates())
11354ba319b5SDimitry Andric LVI->disableDT();
11364ba319b5SDimitry Andric else
11374ba319b5SDimitry Andric LVI->enableDT();
11382754fe60SDimitry Andric LazyValueInfo::Tristate Ret =
11398f0fd8f6SDimitry Andric LVI->getPredicateAt(CondCmp->getPredicate(), CondCmp->getOperand(0),
11408f0fd8f6SDimitry Andric CondConst, CondBr);
11418f0fd8f6SDimitry Andric if (Ret != LazyValueInfo::Unknown) {
11428f0fd8f6SDimitry Andric unsigned ToRemove = Ret == LazyValueInfo::True ? 1 : 0;
11438f0fd8f6SDimitry Andric unsigned ToKeep = Ret == LazyValueInfo::True ? 0 : 1;
11444ba319b5SDimitry Andric BasicBlock *ToRemoveSucc = CondBr->getSuccessor(ToRemove);
11454ba319b5SDimitry Andric ToRemoveSucc->removePredecessor(BB, true);
1146e580952dSDimitry Andric BranchInst::Create(CondBr->getSuccessor(ToKeep), CondBr);
1147e580952dSDimitry Andric CondBr->eraseFromParent();
1148ff0cc061SDimitry Andric if (CondCmp->use_empty())
1149ff0cc061SDimitry Andric CondCmp->eraseFromParent();
1150302affcbSDimitry Andric // We can safely replace *some* uses of the CondInst if it has
1151d8866befSDimitry Andric // exactly one value as returned by LVI. RAUW is incorrect in the
1152d8866befSDimitry Andric // presence of guards and assumes, that have the `Cond` as the use. This
1153d8866befSDimitry Andric // is because we use the guards/assume to reason about the `Cond` value
1154d8866befSDimitry Andric // at the end of block, but RAUW unconditionally replaces all uses
1155d8866befSDimitry Andric // including the guards/assumes themselves and the uses before the
1156d8866befSDimitry Andric // guard/assume.
1157302affcbSDimitry Andric else if (CondCmp->getParent() == BB) {
1158302affcbSDimitry Andric auto *CI = Ret == LazyValueInfo::True ?
1159302affcbSDimitry Andric ConstantInt::getTrue(CondCmp->getType()) :
1160302affcbSDimitry Andric ConstantInt::getFalse(CondCmp->getType());
1161302affcbSDimitry Andric ReplaceFoldableUses(CondCmp, CI);
1162302affcbSDimitry Andric }
1163*b5893f02SDimitry Andric DTU->deleteEdgeRelaxed(BB, ToRemoveSucc);
1164e580952dSDimitry Andric return true;
1165e580952dSDimitry Andric }
1166f785676fSDimitry Andric
11677a7e6055SDimitry Andric // We did not manage to simplify this branch, try to see whether
11687a7e6055SDimitry Andric // CondCmp depends on a known phi-select pattern.
11697a7e6055SDimitry Andric if (TryToUnfoldSelect(CondCmp, BB))
1170f785676fSDimitry Andric return true;
1171ffd1746dSEd Schouten }
11727a7e6055SDimitry Andric }
1173f22ef01cSRoman Divacky
1174*b5893f02SDimitry Andric if (SwitchInst *SI = dyn_cast<SwitchInst>(BB->getTerminator()))
1175*b5893f02SDimitry Andric TryToUnfoldSelect(SI, BB);
1176*b5893f02SDimitry Andric
1177f22ef01cSRoman Divacky // Check for some cases that are worth simplifying. Right now we want to look
1178f22ef01cSRoman Divacky // for loads that are used by a switch or by the condition for the branch. If
1179f22ef01cSRoman Divacky // we see one, check to see if it's partially redundant. If so, insert a PHI
1180f22ef01cSRoman Divacky // which can then be used to thread the values.
1181f22ef01cSRoman Divacky Value *SimplifyValue = CondInst;
1182f22ef01cSRoman Divacky if (CmpInst *CondCmp = dyn_cast<CmpInst>(SimplifyValue))
1183f22ef01cSRoman Divacky if (isa<Constant>(CondCmp->getOperand(1)))
1184f22ef01cSRoman Divacky SimplifyValue = CondCmp->getOperand(0);
1185f22ef01cSRoman Divacky
1186f22ef01cSRoman Divacky // TODO: There are other places where load PRE would be profitable, such as
1187f22ef01cSRoman Divacky // more complex comparisons.
11884ba319b5SDimitry Andric if (LoadInst *LoadI = dyn_cast<LoadInst>(SimplifyValue))
11894ba319b5SDimitry Andric if (SimplifyPartiallyRedundantLoad(LoadI))
1190f22ef01cSRoman Divacky return true;
1191f22ef01cSRoman Divacky
11922cab237bSDimitry Andric // Before threading, try to propagate profile data backwards:
11932cab237bSDimitry Andric if (PHINode *PN = dyn_cast<PHINode>(CondInst))
11942cab237bSDimitry Andric if (PN->getParent() == BB && isa<BranchInst>(BB->getTerminator()))
11952cab237bSDimitry Andric updatePredecessorProfileMetadata(PN, BB);
11962cab237bSDimitry Andric
1197f22ef01cSRoman Divacky // Handle a variety of cases where we are branching on something derived from
1198f22ef01cSRoman Divacky // a PHI node in the current block. If we can prove that any predecessors
1199f22ef01cSRoman Divacky // compute a predictable value based on a PHI node, thread those predecessors.
120039d628a0SDimitry Andric if (ProcessThreadableEdges(CondInst, BB, Preference, Terminator))
1201f22ef01cSRoman Divacky return true;
1202f22ef01cSRoman Divacky
1203f22ef01cSRoman Divacky // If this is an otherwise-unfoldable branch on a phi node in the current
1204f22ef01cSRoman Divacky // block, see if we can simplify.
1205f22ef01cSRoman Divacky if (PHINode *PN = dyn_cast<PHINode>(CondInst))
1206f22ef01cSRoman Divacky if (PN->getParent() == BB && isa<BranchInst>(BB->getTerminator()))
1207f22ef01cSRoman Divacky return ProcessBranchOnPHI(PN);
1208f22ef01cSRoman Divacky
1209f22ef01cSRoman Divacky // If this is an otherwise-unfoldable branch on a XOR, see if we can simplify.
1210f22ef01cSRoman Divacky if (CondInst->getOpcode() == Instruction::Xor &&
1211f22ef01cSRoman Divacky CondInst->getParent() == BB && isa<BranchInst>(BB->getTerminator()))
1212f22ef01cSRoman Divacky return ProcessBranchOnXOR(cast<BinaryOperator>(CondInst));
1213f22ef01cSRoman Divacky
12147d523365SDimitry Andric // Search for a stronger dominating condition that can be used to simplify a
12157d523365SDimitry Andric // conditional branch leaving BB.
12167d523365SDimitry Andric if (ProcessImpliedCondition(BB))
12177d523365SDimitry Andric return true;
1218f22ef01cSRoman Divacky
12197d523365SDimitry Andric return false;
12207d523365SDimitry Andric }
12217d523365SDimitry Andric
ProcessImpliedCondition(BasicBlock * BB)12223ca95b02SDimitry Andric bool JumpThreadingPass::ProcessImpliedCondition(BasicBlock *BB) {
12237d523365SDimitry Andric auto *BI = dyn_cast<BranchInst>(BB->getTerminator());
12247d523365SDimitry Andric if (!BI || !BI->isConditional())
12257d523365SDimitry Andric return false;
12267d523365SDimitry Andric
12277d523365SDimitry Andric Value *Cond = BI->getCondition();
12287d523365SDimitry Andric BasicBlock *CurrentBB = BB;
12297d523365SDimitry Andric BasicBlock *CurrentPred = BB->getSinglePredecessor();
12307d523365SDimitry Andric unsigned Iter = 0;
12317d523365SDimitry Andric
12327d523365SDimitry Andric auto &DL = BB->getModule()->getDataLayout();
12337d523365SDimitry Andric
12347d523365SDimitry Andric while (CurrentPred && Iter++ < ImplicationSearchThreshold) {
12357d523365SDimitry Andric auto *PBI = dyn_cast<BranchInst>(CurrentPred->getTerminator());
12363ca95b02SDimitry Andric if (!PBI || !PBI->isConditional())
12373ca95b02SDimitry Andric return false;
12383ca95b02SDimitry Andric if (PBI->getSuccessor(0) != CurrentBB && PBI->getSuccessor(1) != CurrentBB)
12397d523365SDimitry Andric return false;
12407d523365SDimitry Andric
12412cab237bSDimitry Andric bool CondIsTrue = PBI->getSuccessor(0) == CurrentBB;
12423ca95b02SDimitry Andric Optional<bool> Implication =
12432cab237bSDimitry Andric isImpliedCondition(PBI->getCondition(), Cond, DL, CondIsTrue);
12443ca95b02SDimitry Andric if (Implication) {
12454ba319b5SDimitry Andric BasicBlock *KeepSucc = BI->getSuccessor(*Implication ? 0 : 1);
12464ba319b5SDimitry Andric BasicBlock *RemoveSucc = BI->getSuccessor(*Implication ? 1 : 0);
12474ba319b5SDimitry Andric RemoveSucc->removePredecessor(BB);
12484ba319b5SDimitry Andric BranchInst::Create(KeepSucc, BI);
12497d523365SDimitry Andric BI->eraseFromParent();
1250*b5893f02SDimitry Andric DTU->deleteEdgeRelaxed(BB, RemoveSucc);
12517d523365SDimitry Andric return true;
12527d523365SDimitry Andric }
12537d523365SDimitry Andric CurrentBB = CurrentPred;
12547d523365SDimitry Andric CurrentPred = CurrentBB->getSinglePredecessor();
12557d523365SDimitry Andric }
1256f22ef01cSRoman Divacky
1257f22ef01cSRoman Divacky return false;
1258f22ef01cSRoman Divacky }
1259f22ef01cSRoman Divacky
12607a7e6055SDimitry Andric /// Return true if Op is an instruction defined in the given block.
isOpDefinedInBlock(Value * Op,BasicBlock * BB)12617a7e6055SDimitry Andric static bool isOpDefinedInBlock(Value *Op, BasicBlock *BB) {
12627a7e6055SDimitry Andric if (Instruction *OpInst = dyn_cast<Instruction>(Op))
12637a7e6055SDimitry Andric if (OpInst->getParent() == BB)
12647a7e6055SDimitry Andric return true;
12657a7e6055SDimitry Andric return false;
12667a7e6055SDimitry Andric }
12677a7e6055SDimitry Andric
12684ba319b5SDimitry Andric /// SimplifyPartiallyRedundantLoad - If LoadI is an obviously partially
12694ba319b5SDimitry Andric /// redundant load instruction, eliminate it by replacing it with a PHI node.
12704ba319b5SDimitry Andric /// This is an important optimization that encourages jump threading, and needs
12714ba319b5SDimitry Andric /// to be run interlaced with other jump threading tasks.
SimplifyPartiallyRedundantLoad(LoadInst * LoadI)12724ba319b5SDimitry Andric bool JumpThreadingPass::SimplifyPartiallyRedundantLoad(LoadInst *LoadI) {
12733ca95b02SDimitry Andric // Don't hack volatile and ordered loads.
12744ba319b5SDimitry Andric if (!LoadI->isUnordered()) return false;
1275f22ef01cSRoman Divacky
1276f22ef01cSRoman Divacky // If the load is defined in a block with exactly one predecessor, it can't be
1277f22ef01cSRoman Divacky // partially redundant.
12784ba319b5SDimitry Andric BasicBlock *LoadBB = LoadI->getParent();
1279f22ef01cSRoman Divacky if (LoadBB->getSinglePredecessor())
1280f22ef01cSRoman Divacky return false;
1281f22ef01cSRoman Divacky
12827d523365SDimitry Andric // If the load is defined in an EH pad, it can't be partially redundant,
12837d523365SDimitry Andric // because the edges between the invoke and the EH pad cannot have other
1284f785676fSDimitry Andric // instructions between them.
12857d523365SDimitry Andric if (LoadBB->isEHPad())
1286f785676fSDimitry Andric return false;
1287f785676fSDimitry Andric
12884ba319b5SDimitry Andric Value *LoadedPtr = LoadI->getOperand(0);
1289f22ef01cSRoman Divacky
12907a7e6055SDimitry Andric // If the loaded operand is defined in the LoadBB and its not a phi,
12917a7e6055SDimitry Andric // it can't be available in predecessors.
12927a7e6055SDimitry Andric if (isOpDefinedInBlock(LoadedPtr, LoadBB) && !isa<PHINode>(LoadedPtr))
1293f22ef01cSRoman Divacky return false;
1294f22ef01cSRoman Divacky
1295f22ef01cSRoman Divacky // Scan a few instructions up from the load, to see if it is obviously live at
1296f22ef01cSRoman Divacky // the entry to its block.
12974ba319b5SDimitry Andric BasicBlock::iterator BBIt(LoadI);
1298d88c1a5aSDimitry Andric bool IsLoadCSE;
12997a7e6055SDimitry Andric if (Value *AvailableVal = FindAvailableLoadedValue(
13004ba319b5SDimitry Andric LoadI, LoadBB, BBIt, DefMaxInstsToScan, AA, &IsLoadCSE)) {
13017d523365SDimitry Andric // If the value of the load is locally available within the block, just use
1302f22ef01cSRoman Divacky // it. This frequently occurs for reg2mem'd allocas.
1303f22ef01cSRoman Divacky
1304d88c1a5aSDimitry Andric if (IsLoadCSE) {
13054ba319b5SDimitry Andric LoadInst *NLoadI = cast<LoadInst>(AvailableVal);
1306*b5893f02SDimitry Andric combineMetadataForCSE(NLoadI, LoadI, false);
1307d88c1a5aSDimitry Andric };
1308d88c1a5aSDimitry Andric
1309f22ef01cSRoman Divacky // If the returned value is the load itself, replace with an undef. This can
1310f22ef01cSRoman Divacky // only happen in dead loops.
13114ba319b5SDimitry Andric if (AvailableVal == LoadI)
13124ba319b5SDimitry Andric AvailableVal = UndefValue::get(LoadI->getType());
13134ba319b5SDimitry Andric if (AvailableVal->getType() != LoadI->getType())
13144ba319b5SDimitry Andric AvailableVal = CastInst::CreateBitOrPointerCast(
13154ba319b5SDimitry Andric AvailableVal, LoadI->getType(), "", LoadI);
13164ba319b5SDimitry Andric LoadI->replaceAllUsesWith(AvailableVal);
13174ba319b5SDimitry Andric LoadI->eraseFromParent();
1318f22ef01cSRoman Divacky return true;
1319f22ef01cSRoman Divacky }
1320f22ef01cSRoman Divacky
1321f22ef01cSRoman Divacky // Otherwise, if we scanned the whole block and got to the top of the block,
1322f22ef01cSRoman Divacky // we know the block is locally transparent to the load. If not, something
1323f22ef01cSRoman Divacky // might clobber its value.
1324f22ef01cSRoman Divacky if (BBIt != LoadBB->begin())
1325f22ef01cSRoman Divacky return false;
1326f22ef01cSRoman Divacky
132739d628a0SDimitry Andric // If all of the loads and stores that feed the value have the same AA tags,
132839d628a0SDimitry Andric // then we can propagate them onto any newly inserted loads.
132939d628a0SDimitry Andric AAMDNodes AATags;
13304ba319b5SDimitry Andric LoadI->getAAMetadata(AATags);
1331f22ef01cSRoman Divacky
1332f22ef01cSRoman Divacky SmallPtrSet<BasicBlock*, 8> PredsScanned;
13332cab237bSDimitry Andric
13342cab237bSDimitry Andric using AvailablePredsTy = SmallVector<std::pair<BasicBlock *, Value *>, 8>;
13352cab237bSDimitry Andric
1336f22ef01cSRoman Divacky AvailablePredsTy AvailablePreds;
133791bc56edSDimitry Andric BasicBlock *OneUnavailablePred = nullptr;
1338d88c1a5aSDimitry Andric SmallVector<LoadInst*, 8> CSELoads;
1339f22ef01cSRoman Divacky
1340f22ef01cSRoman Divacky // If we got here, the loaded value is transparent through to the start of the
1341f22ef01cSRoman Divacky // block. Check to see if it is available in any of the predecessor blocks.
1342444ed5c5SDimitry Andric for (BasicBlock *PredBB : predecessors(LoadBB)) {
1343f22ef01cSRoman Divacky // If we already scanned this predecessor, skip it.
134439d628a0SDimitry Andric if (!PredsScanned.insert(PredBB).second)
1345f22ef01cSRoman Divacky continue;
1346f22ef01cSRoman Divacky
1347f22ef01cSRoman Divacky BBIt = PredBB->end();
13487a7e6055SDimitry Andric unsigned NumScanedInst = 0;
13497a7e6055SDimitry Andric Value *PredAvailable = nullptr;
13507a7e6055SDimitry Andric // NOTE: We don't CSE load that is volatile or anything stronger than
13517a7e6055SDimitry Andric // unordered, that should have been checked when we entered the function.
13524ba319b5SDimitry Andric assert(LoadI->isUnordered() &&
13534ba319b5SDimitry Andric "Attempting to CSE volatile or atomic loads");
13547a7e6055SDimitry Andric // If this is a load on a phi pointer, phi-translate it and search
13557a7e6055SDimitry Andric // for available load/store to the pointer in predecessors.
13567a7e6055SDimitry Andric Value *Ptr = LoadedPtr->DoPHITranslation(LoadBB, PredBB);
13577a7e6055SDimitry Andric PredAvailable = FindAvailablePtrLoadStore(
13584ba319b5SDimitry Andric Ptr, LoadI->getType(), LoadI->isAtomic(), PredBB, BBIt,
13594ba319b5SDimitry Andric DefMaxInstsToScan, AA, &IsLoadCSE, &NumScanedInst);
13607a7e6055SDimitry Andric
13617a7e6055SDimitry Andric // If PredBB has a single predecessor, continue scanning through the
13624ba319b5SDimitry Andric // single predecessor.
13637a7e6055SDimitry Andric BasicBlock *SinglePredBB = PredBB;
13647a7e6055SDimitry Andric while (!PredAvailable && SinglePredBB && BBIt == SinglePredBB->begin() &&
13657a7e6055SDimitry Andric NumScanedInst < DefMaxInstsToScan) {
13667a7e6055SDimitry Andric SinglePredBB = SinglePredBB->getSinglePredecessor();
13677a7e6055SDimitry Andric if (SinglePredBB) {
13687a7e6055SDimitry Andric BBIt = SinglePredBB->end();
13697a7e6055SDimitry Andric PredAvailable = FindAvailablePtrLoadStore(
13704ba319b5SDimitry Andric Ptr, LoadI->getType(), LoadI->isAtomic(), SinglePredBB, BBIt,
13717a7e6055SDimitry Andric (DefMaxInstsToScan - NumScanedInst), AA, &IsLoadCSE,
13727a7e6055SDimitry Andric &NumScanedInst);
13737a7e6055SDimitry Andric }
13747a7e6055SDimitry Andric }
13757a7e6055SDimitry Andric
1376f22ef01cSRoman Divacky if (!PredAvailable) {
1377f22ef01cSRoman Divacky OneUnavailablePred = PredBB;
1378f22ef01cSRoman Divacky continue;
1379f22ef01cSRoman Divacky }
1380f22ef01cSRoman Divacky
1381d88c1a5aSDimitry Andric if (IsLoadCSE)
1382d88c1a5aSDimitry Andric CSELoads.push_back(cast<LoadInst>(PredAvailable));
1383dff0c46cSDimitry Andric
1384f22ef01cSRoman Divacky // If so, this load is partially redundant. Remember this info so that we
1385f22ef01cSRoman Divacky // can create a PHI node.
1386f22ef01cSRoman Divacky AvailablePreds.push_back(std::make_pair(PredBB, PredAvailable));
1387f22ef01cSRoman Divacky }
1388f22ef01cSRoman Divacky
1389f22ef01cSRoman Divacky // If the loaded value isn't available in any predecessor, it isn't partially
1390f22ef01cSRoman Divacky // redundant.
1391f22ef01cSRoman Divacky if (AvailablePreds.empty()) return false;
1392f22ef01cSRoman Divacky
1393f22ef01cSRoman Divacky // Okay, the loaded value is available in at least one (and maybe all!)
1394f22ef01cSRoman Divacky // predecessors. If the value is unavailable in more than one unique
1395f22ef01cSRoman Divacky // predecessor, we want to insert a merge block for those common predecessors.
1396f22ef01cSRoman Divacky // This ensures that we only have to insert one reload, thus not increasing
1397f22ef01cSRoman Divacky // code size.
139891bc56edSDimitry Andric BasicBlock *UnavailablePred = nullptr;
1399f22ef01cSRoman Divacky
1400da09e106SDimitry Andric // If the value is unavailable in one of predecessors, we will end up
1401da09e106SDimitry Andric // inserting a new instruction into them. It is only valid if all the
14024ba319b5SDimitry Andric // instructions before LoadI are guaranteed to pass execution to its
14034ba319b5SDimitry Andric // successor, or if LoadI is safe to speculate.
1404da09e106SDimitry Andric // TODO: If this logic becomes more complex, and we will perform PRE insertion
1405da09e106SDimitry Andric // farther than to a predecessor, we need to reuse the code from GVN's PRE.
1406da09e106SDimitry Andric // It requires domination tree analysis, so for this simple case it is an
1407da09e106SDimitry Andric // overkill.
1408da09e106SDimitry Andric if (PredsScanned.size() != AvailablePreds.size() &&
14094ba319b5SDimitry Andric !isSafeToSpeculativelyExecute(LoadI))
14104ba319b5SDimitry Andric for (auto I = LoadBB->begin(); &*I != LoadI; ++I)
1411da09e106SDimitry Andric if (!isGuaranteedToTransferExecutionToSuccessor(&*I))
1412da09e106SDimitry Andric return false;
1413da09e106SDimitry Andric
1414f22ef01cSRoman Divacky // If there is exactly one predecessor where the value is unavailable, the
1415f22ef01cSRoman Divacky // already computed 'OneUnavailablePred' block is it. If it ends in an
1416f22ef01cSRoman Divacky // unconditional branch, we know that it isn't a critical edge.
1417f22ef01cSRoman Divacky if (PredsScanned.size() == AvailablePreds.size()+1 &&
1418f22ef01cSRoman Divacky OneUnavailablePred->getTerminator()->getNumSuccessors() == 1) {
1419f22ef01cSRoman Divacky UnavailablePred = OneUnavailablePred;
1420f22ef01cSRoman Divacky } else if (PredsScanned.size() != AvailablePreds.size()) {
1421f22ef01cSRoman Divacky // Otherwise, we had multiple unavailable predecessors or we had a critical
1422f22ef01cSRoman Divacky // edge from the one.
1423f22ef01cSRoman Divacky SmallVector<BasicBlock*, 8> PredsToSplit;
1424f22ef01cSRoman Divacky SmallPtrSet<BasicBlock*, 8> AvailablePredSet;
1425f22ef01cSRoman Divacky
1426444ed5c5SDimitry Andric for (const auto &AvailablePred : AvailablePreds)
1427444ed5c5SDimitry Andric AvailablePredSet.insert(AvailablePred.first);
1428f22ef01cSRoman Divacky
1429f22ef01cSRoman Divacky // Add all the unavailable predecessors to the PredsToSplit list.
1430444ed5c5SDimitry Andric for (BasicBlock *P : predecessors(LoadBB)) {
1431ffd1746dSEd Schouten // If the predecessor is an indirect goto, we can't split the edge.
1432ffd1746dSEd Schouten if (isa<IndirectBrInst>(P->getTerminator()))
1433ffd1746dSEd Schouten return false;
1434ffd1746dSEd Schouten
1435ffd1746dSEd Schouten if (!AvailablePredSet.count(P))
1436ffd1746dSEd Schouten PredsToSplit.push_back(P);
1437ffd1746dSEd Schouten }
1438f22ef01cSRoman Divacky
1439f22ef01cSRoman Divacky // Split them out to their own block.
14407d523365SDimitry Andric UnavailablePred = SplitBlockPreds(LoadBB, PredsToSplit, "thread-pre-split");
1441f22ef01cSRoman Divacky }
1442f22ef01cSRoman Divacky
1443f22ef01cSRoman Divacky // If the value isn't available in all predecessors, then there will be
1444f22ef01cSRoman Divacky // exactly one where it isn't available. Insert a load on that edge and add
1445f22ef01cSRoman Divacky // it to the AvailablePreds list.
1446f22ef01cSRoman Divacky if (UnavailablePred) {
1447f22ef01cSRoman Divacky assert(UnavailablePred->getTerminator()->getNumSuccessors() == 1 &&
1448f22ef01cSRoman Divacky "Can't handle critical edge here!");
14494ba319b5SDimitry Andric LoadInst *NewVal =
14504ba319b5SDimitry Andric new LoadInst(LoadedPtr->DoPHITranslation(LoadBB, UnavailablePred),
14514ba319b5SDimitry Andric LoadI->getName() + ".pr", false, LoadI->getAlignment(),
14524ba319b5SDimitry Andric LoadI->getOrdering(), LoadI->getSyncScopeID(),
14534ba319b5SDimitry Andric UnavailablePred->getTerminator());
14544ba319b5SDimitry Andric NewVal->setDebugLoc(LoadI->getDebugLoc());
145539d628a0SDimitry Andric if (AATags)
145639d628a0SDimitry Andric NewVal->setAAMetadata(AATags);
1457dff0c46cSDimitry Andric
1458f22ef01cSRoman Divacky AvailablePreds.push_back(std::make_pair(UnavailablePred, NewVal));
1459f22ef01cSRoman Divacky }
1460f22ef01cSRoman Divacky
1461f22ef01cSRoman Divacky // Now we know that each predecessor of this block has a value in
1462f22ef01cSRoman Divacky // AvailablePreds, sort them for efficient access as we're walking the preds.
1463f22ef01cSRoman Divacky array_pod_sort(AvailablePreds.begin(), AvailablePreds.end());
1464f22ef01cSRoman Divacky
1465f22ef01cSRoman Divacky // Create a PHI node at the start of the block for the PRE'd load value.
14663b0f4066SDimitry Andric pred_iterator PB = pred_begin(LoadBB), PE = pred_end(LoadBB);
14674ba319b5SDimitry Andric PHINode *PN = PHINode::Create(LoadI->getType(), std::distance(PB, PE), "",
14687d523365SDimitry Andric &LoadBB->front());
14694ba319b5SDimitry Andric PN->takeName(LoadI);
14704ba319b5SDimitry Andric PN->setDebugLoc(LoadI->getDebugLoc());
1471f22ef01cSRoman Divacky
1472f22ef01cSRoman Divacky // Insert new entries into the PHI for each predecessor. A single block may
1473f22ef01cSRoman Divacky // have multiple entries here.
14743b0f4066SDimitry Andric for (pred_iterator PI = PB; PI != PE; ++PI) {
1475ffd1746dSEd Schouten BasicBlock *P = *PI;
1476f22ef01cSRoman Divacky AvailablePredsTy::iterator I =
1477f22ef01cSRoman Divacky std::lower_bound(AvailablePreds.begin(), AvailablePreds.end(),
147891bc56edSDimitry Andric std::make_pair(P, (Value*)nullptr));
1479f22ef01cSRoman Divacky
1480ffd1746dSEd Schouten assert(I != AvailablePreds.end() && I->first == P &&
1481f22ef01cSRoman Divacky "Didn't find entry for predecessor!");
1482f22ef01cSRoman Divacky
148339d628a0SDimitry Andric // If we have an available predecessor but it requires casting, insert the
148439d628a0SDimitry Andric // cast in the predecessor and use the cast. Note that we have to update the
148539d628a0SDimitry Andric // AvailablePreds vector as we go so that all of the PHI entries for this
148639d628a0SDimitry Andric // predecessor use the same bitcast.
148739d628a0SDimitry Andric Value *&PredV = I->second;
14884ba319b5SDimitry Andric if (PredV->getType() != LoadI->getType())
14894ba319b5SDimitry Andric PredV = CastInst::CreateBitOrPointerCast(PredV, LoadI->getType(), "",
149039d628a0SDimitry Andric P->getTerminator());
149139d628a0SDimitry Andric
149239d628a0SDimitry Andric PN->addIncoming(PredV, I->first);
1493f22ef01cSRoman Divacky }
1494f22ef01cSRoman Divacky
14954ba319b5SDimitry Andric for (LoadInst *PredLoadI : CSELoads) {
1496*b5893f02SDimitry Andric combineMetadataForCSE(PredLoadI, LoadI, true);
1497d88c1a5aSDimitry Andric }
1498d88c1a5aSDimitry Andric
14994ba319b5SDimitry Andric LoadI->replaceAllUsesWith(PN);
15004ba319b5SDimitry Andric LoadI->eraseFromParent();
1501f22ef01cSRoman Divacky
1502f22ef01cSRoman Divacky return true;
1503f22ef01cSRoman Divacky }
1504f22ef01cSRoman Divacky
1505f22ef01cSRoman Divacky /// FindMostPopularDest - The specified list contains multiple possible
1506f22ef01cSRoman Divacky /// threadable destinations. Pick the one that occurs the most frequently in
1507f22ef01cSRoman Divacky /// the list.
1508f22ef01cSRoman Divacky static BasicBlock *
FindMostPopularDest(BasicBlock * BB,const SmallVectorImpl<std::pair<BasicBlock *,BasicBlock * >> & PredToDestList)1509f22ef01cSRoman Divacky FindMostPopularDest(BasicBlock *BB,
1510f22ef01cSRoman Divacky const SmallVectorImpl<std::pair<BasicBlock *,
1511f22ef01cSRoman Divacky BasicBlock *>> &PredToDestList) {
1512f22ef01cSRoman Divacky assert(!PredToDestList.empty());
1513f22ef01cSRoman Divacky
1514f22ef01cSRoman Divacky // Determine popularity. If there are multiple possible destinations, we
1515f22ef01cSRoman Divacky // explicitly choose to ignore 'undef' destinations. We prefer to thread
1516f22ef01cSRoman Divacky // blocks with known and real destinations to threading undef. We'll handle
1517f22ef01cSRoman Divacky // them later if interesting.
1518f22ef01cSRoman Divacky DenseMap<BasicBlock*, unsigned> DestPopularity;
1519444ed5c5SDimitry Andric for (const auto &PredToDest : PredToDestList)
1520444ed5c5SDimitry Andric if (PredToDest.second)
1521444ed5c5SDimitry Andric DestPopularity[PredToDest.second]++;
1522f22ef01cSRoman Divacky
15236ccc06f6SDimitry Andric if (DestPopularity.empty())
15246ccc06f6SDimitry Andric return nullptr;
15256ccc06f6SDimitry Andric
1526f22ef01cSRoman Divacky // Find the most popular dest.
1527f22ef01cSRoman Divacky DenseMap<BasicBlock*, unsigned>::iterator DPI = DestPopularity.begin();
1528f22ef01cSRoman Divacky BasicBlock *MostPopularDest = DPI->first;
1529f22ef01cSRoman Divacky unsigned Popularity = DPI->second;
1530f22ef01cSRoman Divacky SmallVector<BasicBlock*, 4> SamePopularity;
1531f22ef01cSRoman Divacky
1532f22ef01cSRoman Divacky for (++DPI; DPI != DestPopularity.end(); ++DPI) {
1533f22ef01cSRoman Divacky // If the popularity of this entry isn't higher than the popularity we've
1534f22ef01cSRoman Divacky // seen so far, ignore it.
1535f22ef01cSRoman Divacky if (DPI->second < Popularity)
1536f22ef01cSRoman Divacky ; // ignore.
1537f22ef01cSRoman Divacky else if (DPI->second == Popularity) {
1538f22ef01cSRoman Divacky // If it is the same as what we've seen so far, keep track of it.
1539f22ef01cSRoman Divacky SamePopularity.push_back(DPI->first);
1540f22ef01cSRoman Divacky } else {
1541f22ef01cSRoman Divacky // If it is more popular, remember it.
1542f22ef01cSRoman Divacky SamePopularity.clear();
1543f22ef01cSRoman Divacky MostPopularDest = DPI->first;
1544f22ef01cSRoman Divacky Popularity = DPI->second;
1545f22ef01cSRoman Divacky }
1546f22ef01cSRoman Divacky }
1547f22ef01cSRoman Divacky
15482754fe60SDimitry Andric // Okay, now we know the most popular destination. If there is more than one
1549f22ef01cSRoman Divacky // destination, we need to determine one. This is arbitrary, but we need
1550f22ef01cSRoman Divacky // to make a deterministic decision. Pick the first one that appears in the
1551f22ef01cSRoman Divacky // successor list.
1552f22ef01cSRoman Divacky if (!SamePopularity.empty()) {
1553f22ef01cSRoman Divacky SamePopularity.push_back(MostPopularDest);
1554*b5893f02SDimitry Andric Instruction *TI = BB->getTerminator();
1555f22ef01cSRoman Divacky for (unsigned i = 0; ; ++i) {
1556f22ef01cSRoman Divacky assert(i != TI->getNumSuccessors() && "Didn't find any successor!");
1557f22ef01cSRoman Divacky
1558d88c1a5aSDimitry Andric if (!is_contained(SamePopularity, TI->getSuccessor(i)))
1559f22ef01cSRoman Divacky continue;
1560f22ef01cSRoman Divacky
1561f22ef01cSRoman Divacky MostPopularDest = TI->getSuccessor(i);
1562f22ef01cSRoman Divacky break;
1563f22ef01cSRoman Divacky }
1564f22ef01cSRoman Divacky }
1565f22ef01cSRoman Divacky
1566f22ef01cSRoman Divacky // Okay, we have finally picked the most popular destination.
1567f22ef01cSRoman Divacky return MostPopularDest;
1568f22ef01cSRoman Divacky }
1569f22ef01cSRoman Divacky
ProcessThreadableEdges(Value * Cond,BasicBlock * BB,ConstantPreference Preference,Instruction * CxtI)15703ca95b02SDimitry Andric bool JumpThreadingPass::ProcessThreadableEdges(Value *Cond, BasicBlock *BB,
157139d628a0SDimitry Andric ConstantPreference Preference,
157239d628a0SDimitry Andric Instruction *CxtI) {
1573f22ef01cSRoman Divacky // If threading this would thread across a loop header, don't even try to
1574f22ef01cSRoman Divacky // thread the edge.
1575f22ef01cSRoman Divacky if (LoopHeaders.count(BB))
1576f22ef01cSRoman Divacky return false;
1577f22ef01cSRoman Divacky
15782754fe60SDimitry Andric PredValueInfoTy PredValues;
157939d628a0SDimitry Andric if (!ComputeValueKnownInPredecessors(Cond, BB, PredValues, Preference, CxtI))
1580f22ef01cSRoman Divacky return false;
1581e580952dSDimitry Andric
1582f22ef01cSRoman Divacky assert(!PredValues.empty() &&
1583f22ef01cSRoman Divacky "ComputeValueKnownInPredecessors returned true with no values");
1584f22ef01cSRoman Divacky
15854ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "IN BB: " << *BB;
1586444ed5c5SDimitry Andric for (const auto &PredValue : PredValues) {
15874ba319b5SDimitry Andric dbgs() << " BB '" << BB->getName()
15884ba319b5SDimitry Andric << "': FOUND condition = " << *PredValue.first
1589444ed5c5SDimitry Andric << " for pred '" << PredValue.second->getName() << "'.\n";
1590f22ef01cSRoman Divacky });
1591f22ef01cSRoman Divacky
1592f22ef01cSRoman Divacky // Decide what we want to thread through. Convert our list of known values to
1593f22ef01cSRoman Divacky // a list of known destinations for each pred. This also discards duplicate
1594f22ef01cSRoman Divacky // predecessors and keeps track of the undefined inputs (which are represented
1595f22ef01cSRoman Divacky // as a null dest in the PredToDestList).
1596f22ef01cSRoman Divacky SmallPtrSet<BasicBlock*, 16> SeenPreds;
1597f22ef01cSRoman Divacky SmallVector<std::pair<BasicBlock*, BasicBlock*>, 16> PredToDestList;
1598f22ef01cSRoman Divacky
159991bc56edSDimitry Andric BasicBlock *OnlyDest = nullptr;
1600f22ef01cSRoman Divacky BasicBlock *MultipleDestSentinel = (BasicBlock*)(intptr_t)~0ULL;
1601f37b6182SDimitry Andric Constant *OnlyVal = nullptr;
1602f37b6182SDimitry Andric Constant *MultipleVal = (Constant *)(intptr_t)~0ULL;
1603f22ef01cSRoman Divacky
1604f37b6182SDimitry Andric unsigned PredWithKnownDest = 0;
1605444ed5c5SDimitry Andric for (const auto &PredValue : PredValues) {
1606444ed5c5SDimitry Andric BasicBlock *Pred = PredValue.second;
160739d628a0SDimitry Andric if (!SeenPreds.insert(Pred).second)
1608f22ef01cSRoman Divacky continue; // Duplicate predecessor entry.
1609f22ef01cSRoman Divacky
1610444ed5c5SDimitry Andric Constant *Val = PredValue.first;
1611f22ef01cSRoman Divacky
1612f22ef01cSRoman Divacky BasicBlock *DestBB;
16132754fe60SDimitry Andric if (isa<UndefValue>(Val))
161491bc56edSDimitry Andric DestBB = nullptr;
1615f37b6182SDimitry Andric else if (BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator())) {
1616f37b6182SDimitry Andric assert(isa<ConstantInt>(Val) && "Expecting a constant integer");
16172754fe60SDimitry Andric DestBB = BI->getSuccessor(cast<ConstantInt>(Val)->isZero());
1618f37b6182SDimitry Andric } else if (SwitchInst *SI = dyn_cast<SwitchInst>(BB->getTerminator())) {
1619f37b6182SDimitry Andric assert(isa<ConstantInt>(Val) && "Expecting a constant integer");
16207a7e6055SDimitry Andric DestBB = SI->findCaseValue(cast<ConstantInt>(Val))->getCaseSuccessor();
1621dff0c46cSDimitry Andric } else {
16222754fe60SDimitry Andric assert(isa<IndirectBrInst>(BB->getTerminator())
16232754fe60SDimitry Andric && "Unexpected terminator");
1624f37b6182SDimitry Andric assert(isa<BlockAddress>(Val) && "Expecting a constant blockaddress");
16252754fe60SDimitry Andric DestBB = cast<BlockAddress>(Val)->getBasicBlock();
1626f22ef01cSRoman Divacky }
1627f22ef01cSRoman Divacky
1628f22ef01cSRoman Divacky // If we have exactly one destination, remember it for efficiency below.
1629f37b6182SDimitry Andric if (PredToDestList.empty()) {
1630f22ef01cSRoman Divacky OnlyDest = DestBB;
1631f37b6182SDimitry Andric OnlyVal = Val;
1632f37b6182SDimitry Andric } else {
1633f37b6182SDimitry Andric if (OnlyDest != DestBB)
1634f22ef01cSRoman Divacky OnlyDest = MultipleDestSentinel;
1635f37b6182SDimitry Andric // It possible we have same destination, but different value, e.g. default
1636f37b6182SDimitry Andric // case in switchinst.
1637f37b6182SDimitry Andric if (Val != OnlyVal)
1638f37b6182SDimitry Andric OnlyVal = MultipleVal;
1639f37b6182SDimitry Andric }
1640f37b6182SDimitry Andric
1641f37b6182SDimitry Andric // We know where this predecessor is going.
1642f37b6182SDimitry Andric ++PredWithKnownDest;
1643f37b6182SDimitry Andric
1644f37b6182SDimitry Andric // If the predecessor ends with an indirect goto, we can't change its
1645f37b6182SDimitry Andric // destination.
1646f37b6182SDimitry Andric if (isa<IndirectBrInst>(Pred->getTerminator()))
1647f37b6182SDimitry Andric continue;
1648f22ef01cSRoman Divacky
1649f22ef01cSRoman Divacky PredToDestList.push_back(std::make_pair(Pred, DestBB));
1650f22ef01cSRoman Divacky }
1651f22ef01cSRoman Divacky
1652f22ef01cSRoman Divacky // If all edges were unthreadable, we fail.
1653f22ef01cSRoman Divacky if (PredToDestList.empty())
1654f22ef01cSRoman Divacky return false;
1655f22ef01cSRoman Divacky
165651690af2SDimitry Andric // If all the predecessors go to a single known successor, we want to fold,
165751690af2SDimitry Andric // not thread. By doing so, we do not need to duplicate the current block and
165851690af2SDimitry Andric // also miss potential opportunities in case we dont/cant duplicate.
165951690af2SDimitry Andric if (OnlyDest && OnlyDest != MultipleDestSentinel) {
16604ba319b5SDimitry Andric if (PredWithKnownDest == (size_t)pred_size(BB)) {
166151690af2SDimitry Andric bool SeenFirstBranchToOnlyDest = false;
16624ba319b5SDimitry Andric std::vector <DominatorTree::UpdateType> Updates;
16634ba319b5SDimitry Andric Updates.reserve(BB->getTerminator()->getNumSuccessors() - 1);
166451690af2SDimitry Andric for (BasicBlock *SuccBB : successors(BB)) {
16654ba319b5SDimitry Andric if (SuccBB == OnlyDest && !SeenFirstBranchToOnlyDest) {
166651690af2SDimitry Andric SeenFirstBranchToOnlyDest = true; // Don't modify the first branch.
16674ba319b5SDimitry Andric } else {
166851690af2SDimitry Andric SuccBB->removePredecessor(BB, true); // This is unreachable successor.
16694ba319b5SDimitry Andric Updates.push_back({DominatorTree::Delete, BB, SuccBB});
16704ba319b5SDimitry Andric }
167151690af2SDimitry Andric }
167251690af2SDimitry Andric
167351690af2SDimitry Andric // Finally update the terminator.
1674*b5893f02SDimitry Andric Instruction *Term = BB->getTerminator();
167551690af2SDimitry Andric BranchInst::Create(OnlyDest, Term);
167651690af2SDimitry Andric Term->eraseFromParent();
1677*b5893f02SDimitry Andric DTU->applyUpdates(Updates);
167851690af2SDimitry Andric
167951690af2SDimitry Andric // If the condition is now dead due to the removal of the old terminator,
168051690af2SDimitry Andric // erase it.
1681f37b6182SDimitry Andric if (auto *CondInst = dyn_cast<Instruction>(Cond)) {
1682f37b6182SDimitry Andric if (CondInst->use_empty() && !CondInst->mayHaveSideEffects())
168351690af2SDimitry Andric CondInst->eraseFromParent();
1684302affcbSDimitry Andric // We can safely replace *some* uses of the CondInst if it has
1685d8866befSDimitry Andric // exactly one value as returned by LVI. RAUW is incorrect in the
1686d8866befSDimitry Andric // presence of guards and assumes, that have the `Cond` as the use. This
1687d8866befSDimitry Andric // is because we use the guards/assume to reason about the `Cond` value
1688d8866befSDimitry Andric // at the end of block, but RAUW unconditionally replaces all uses
1689d8866befSDimitry Andric // including the guards/assumes themselves and the uses before the
1690d8866befSDimitry Andric // guard/assume.
1691302affcbSDimitry Andric else if (OnlyVal && OnlyVal != MultipleVal &&
1692302affcbSDimitry Andric CondInst->getParent() == BB)
1693302affcbSDimitry Andric ReplaceFoldableUses(CondInst, OnlyVal);
1694f37b6182SDimitry Andric }
169551690af2SDimitry Andric return true;
169651690af2SDimitry Andric }
169751690af2SDimitry Andric }
169851690af2SDimitry Andric
1699f22ef01cSRoman Divacky // Determine which is the most common successor. If we have many inputs and
1700f22ef01cSRoman Divacky // this block is a switch, we want to start by threading the batch that goes
1701f22ef01cSRoman Divacky // to the most popular destination first. If we only know about one
1702f22ef01cSRoman Divacky // threadable destination (the common case) we can avoid this.
1703f22ef01cSRoman Divacky BasicBlock *MostPopularDest = OnlyDest;
1704f22ef01cSRoman Divacky
17056ccc06f6SDimitry Andric if (MostPopularDest == MultipleDestSentinel) {
17066ccc06f6SDimitry Andric // Remove any loop headers from the Dest list, ThreadEdge conservatively
17076ccc06f6SDimitry Andric // won't process them, but we might have other destination that are eligible
17086ccc06f6SDimitry Andric // and we still want to process.
17096ccc06f6SDimitry Andric erase_if(PredToDestList,
17106ccc06f6SDimitry Andric [&](const std::pair<BasicBlock *, BasicBlock *> &PredToDest) {
17116ccc06f6SDimitry Andric return LoopHeaders.count(PredToDest.second) != 0;
17126ccc06f6SDimitry Andric });
17136ccc06f6SDimitry Andric
17146ccc06f6SDimitry Andric if (PredToDestList.empty())
17156ccc06f6SDimitry Andric return false;
17166ccc06f6SDimitry Andric
1717f22ef01cSRoman Divacky MostPopularDest = FindMostPopularDest(BB, PredToDestList);
17186ccc06f6SDimitry Andric }
1719f22ef01cSRoman Divacky
1720f22ef01cSRoman Divacky // Now that we know what the most popular destination is, factor all
1721f22ef01cSRoman Divacky // predecessors that will jump to it into a single predecessor.
1722f22ef01cSRoman Divacky SmallVector<BasicBlock*, 16> PredsToFactor;
1723444ed5c5SDimitry Andric for (const auto &PredToDest : PredToDestList)
1724444ed5c5SDimitry Andric if (PredToDest.second == MostPopularDest) {
1725444ed5c5SDimitry Andric BasicBlock *Pred = PredToDest.first;
1726f22ef01cSRoman Divacky
1727f22ef01cSRoman Divacky // This predecessor may be a switch or something else that has multiple
1728f22ef01cSRoman Divacky // edges to the block. Factor each of these edges by listing them
1729f22ef01cSRoman Divacky // according to # occurrences in PredsToFactor.
1730444ed5c5SDimitry Andric for (BasicBlock *Succ : successors(Pred))
1731444ed5c5SDimitry Andric if (Succ == BB)
1732f22ef01cSRoman Divacky PredsToFactor.push_back(Pred);
1733f22ef01cSRoman Divacky }
1734f22ef01cSRoman Divacky
1735f22ef01cSRoman Divacky // If the threadable edges are branching on an undefined value, we get to pick
1736f22ef01cSRoman Divacky // the destination that these predecessors should get to.
173791bc56edSDimitry Andric if (!MostPopularDest)
1738f22ef01cSRoman Divacky MostPopularDest = BB->getTerminator()->
1739f22ef01cSRoman Divacky getSuccessor(GetBestDestForJumpOnUndef(BB));
1740f22ef01cSRoman Divacky
1741f22ef01cSRoman Divacky // Ok, try to thread it!
1742f22ef01cSRoman Divacky return ThreadEdge(BB, PredsToFactor, MostPopularDest);
1743f22ef01cSRoman Divacky }
1744f22ef01cSRoman Divacky
1745f22ef01cSRoman Divacky /// ProcessBranchOnPHI - We have an otherwise unthreadable conditional branch on
1746f22ef01cSRoman Divacky /// a PHI node in the current block. See if there are any simplifications we
1747f22ef01cSRoman Divacky /// can do based on inputs to the phi node.
ProcessBranchOnPHI(PHINode * PN)17483ca95b02SDimitry Andric bool JumpThreadingPass::ProcessBranchOnPHI(PHINode *PN) {
1749f22ef01cSRoman Divacky BasicBlock *BB = PN->getParent();
1750f22ef01cSRoman Divacky
1751f22ef01cSRoman Divacky // TODO: We could make use of this to do it once for blocks with common PHI
1752f22ef01cSRoman Divacky // values.
1753f22ef01cSRoman Divacky SmallVector<BasicBlock*, 1> PredBBs;
1754f22ef01cSRoman Divacky PredBBs.resize(1);
1755f22ef01cSRoman Divacky
1756f22ef01cSRoman Divacky // If any of the predecessor blocks end in an unconditional branch, we can
1757f22ef01cSRoman Divacky // *duplicate* the conditional branch into that block in order to further
1758f22ef01cSRoman Divacky // encourage jump threading and to eliminate cases where we have branch on a
1759f22ef01cSRoman Divacky // phi of an icmp (branch on icmp is much better).
1760f22ef01cSRoman Divacky for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
1761f22ef01cSRoman Divacky BasicBlock *PredBB = PN->getIncomingBlock(i);
1762f22ef01cSRoman Divacky if (BranchInst *PredBr = dyn_cast<BranchInst>(PredBB->getTerminator()))
1763f22ef01cSRoman Divacky if (PredBr->isUnconditional()) {
1764f22ef01cSRoman Divacky PredBBs[0] = PredBB;
1765f22ef01cSRoman Divacky // Try to duplicate BB into PredBB.
1766f22ef01cSRoman Divacky if (DuplicateCondBranchOnPHIIntoPred(BB, PredBBs))
1767f22ef01cSRoman Divacky return true;
1768f22ef01cSRoman Divacky }
1769f22ef01cSRoman Divacky }
1770f22ef01cSRoman Divacky
1771f22ef01cSRoman Divacky return false;
1772f22ef01cSRoman Divacky }
1773f22ef01cSRoman Divacky
1774f22ef01cSRoman Divacky /// ProcessBranchOnXOR - We have an otherwise unthreadable conditional branch on
1775f22ef01cSRoman Divacky /// a xor instruction in the current block. See if there are any
1776f22ef01cSRoman Divacky /// simplifications we can do based on inputs to the xor.
ProcessBranchOnXOR(BinaryOperator * BO)17773ca95b02SDimitry Andric bool JumpThreadingPass::ProcessBranchOnXOR(BinaryOperator *BO) {
1778f22ef01cSRoman Divacky BasicBlock *BB = BO->getParent();
1779f22ef01cSRoman Divacky
1780f22ef01cSRoman Divacky // If either the LHS or RHS of the xor is a constant, don't do this
1781f22ef01cSRoman Divacky // optimization.
1782f22ef01cSRoman Divacky if (isa<ConstantInt>(BO->getOperand(0)) ||
1783f22ef01cSRoman Divacky isa<ConstantInt>(BO->getOperand(1)))
1784f22ef01cSRoman Divacky return false;
1785f22ef01cSRoman Divacky
1786f22ef01cSRoman Divacky // If the first instruction in BB isn't a phi, we won't be able to infer
1787f22ef01cSRoman Divacky // anything special about any particular predecessor.
1788f22ef01cSRoman Divacky if (!isa<PHINode>(BB->front()))
1789f22ef01cSRoman Divacky return false;
1790f22ef01cSRoman Divacky
1791f41fbc90SDimitry Andric // If this BB is a landing pad, we won't be able to split the edge into it.
1792f41fbc90SDimitry Andric if (BB->isEHPad())
1793f41fbc90SDimitry Andric return false;
1794f41fbc90SDimitry Andric
1795f22ef01cSRoman Divacky // If we have a xor as the branch input to this block, and we know that the
1796f22ef01cSRoman Divacky // LHS or RHS of the xor in any predecessor is true/false, then we can clone
1797f22ef01cSRoman Divacky // the condition into the predecessor and fix that value to true, saving some
1798f22ef01cSRoman Divacky // logical ops on that path and encouraging other paths to simplify.
1799f22ef01cSRoman Divacky //
1800f22ef01cSRoman Divacky // This copies something like this:
1801f22ef01cSRoman Divacky //
1802f22ef01cSRoman Divacky // BB:
1803f22ef01cSRoman Divacky // %X = phi i1 [1], [%X']
1804f22ef01cSRoman Divacky // %Y = icmp eq i32 %A, %B
1805f22ef01cSRoman Divacky // %Z = xor i1 %X, %Y
1806f22ef01cSRoman Divacky // br i1 %Z, ...
1807f22ef01cSRoman Divacky //
1808f22ef01cSRoman Divacky // Into:
1809f22ef01cSRoman Divacky // BB':
1810f22ef01cSRoman Divacky // %Y = icmp ne i32 %A, %B
18117d523365SDimitry Andric // br i1 %Y, ...
1812f22ef01cSRoman Divacky
18132754fe60SDimitry Andric PredValueInfoTy XorOpValues;
1814f22ef01cSRoman Divacky bool isLHS = true;
18152754fe60SDimitry Andric if (!ComputeValueKnownInPredecessors(BO->getOperand(0), BB, XorOpValues,
181639d628a0SDimitry Andric WantInteger, BO)) {
1817f22ef01cSRoman Divacky assert(XorOpValues.empty());
18182754fe60SDimitry Andric if (!ComputeValueKnownInPredecessors(BO->getOperand(1), BB, XorOpValues,
181939d628a0SDimitry Andric WantInteger, BO))
1820f22ef01cSRoman Divacky return false;
1821f22ef01cSRoman Divacky isLHS = false;
1822f22ef01cSRoman Divacky }
1823f22ef01cSRoman Divacky
1824f22ef01cSRoman Divacky assert(!XorOpValues.empty() &&
1825f22ef01cSRoman Divacky "ComputeValueKnownInPredecessors returned true with no values");
1826f22ef01cSRoman Divacky
1827f22ef01cSRoman Divacky // Scan the information to see which is most popular: true or false. The
1828f22ef01cSRoman Divacky // predecessors can be of the set true, false, or undef.
1829f22ef01cSRoman Divacky unsigned NumTrue = 0, NumFalse = 0;
1830444ed5c5SDimitry Andric for (const auto &XorOpValue : XorOpValues) {
1831444ed5c5SDimitry Andric if (isa<UndefValue>(XorOpValue.first))
18322754fe60SDimitry Andric // Ignore undefs for the count.
18332754fe60SDimitry Andric continue;
1834444ed5c5SDimitry Andric if (cast<ConstantInt>(XorOpValue.first)->isZero())
1835f22ef01cSRoman Divacky ++NumFalse;
1836f22ef01cSRoman Divacky else
1837f22ef01cSRoman Divacky ++NumTrue;
1838f22ef01cSRoman Divacky }
1839f22ef01cSRoman Divacky
1840f22ef01cSRoman Divacky // Determine which value to split on, true, false, or undef if neither.
184191bc56edSDimitry Andric ConstantInt *SplitVal = nullptr;
1842f22ef01cSRoman Divacky if (NumTrue > NumFalse)
1843f22ef01cSRoman Divacky SplitVal = ConstantInt::getTrue(BB->getContext());
1844f22ef01cSRoman Divacky else if (NumTrue != 0 || NumFalse != 0)
1845f22ef01cSRoman Divacky SplitVal = ConstantInt::getFalse(BB->getContext());
1846f22ef01cSRoman Divacky
1847f22ef01cSRoman Divacky // Collect all of the blocks that this can be folded into so that we can
1848f22ef01cSRoman Divacky // factor this once and clone it once.
1849f22ef01cSRoman Divacky SmallVector<BasicBlock*, 8> BlocksToFoldInto;
1850444ed5c5SDimitry Andric for (const auto &XorOpValue : XorOpValues) {
1851444ed5c5SDimitry Andric if (XorOpValue.first != SplitVal && !isa<UndefValue>(XorOpValue.first))
18522754fe60SDimitry Andric continue;
1853f22ef01cSRoman Divacky
1854444ed5c5SDimitry Andric BlocksToFoldInto.push_back(XorOpValue.second);
1855f22ef01cSRoman Divacky }
1856f22ef01cSRoman Divacky
1857f22ef01cSRoman Divacky // If we inferred a value for all of the predecessors, then duplication won't
1858f22ef01cSRoman Divacky // help us. However, we can just replace the LHS or RHS with the constant.
1859f22ef01cSRoman Divacky if (BlocksToFoldInto.size() ==
1860f22ef01cSRoman Divacky cast<PHINode>(BB->front()).getNumIncomingValues()) {
186191bc56edSDimitry Andric if (!SplitVal) {
1862f22ef01cSRoman Divacky // If all preds provide undef, just nuke the xor, because it is undef too.
1863f22ef01cSRoman Divacky BO->replaceAllUsesWith(UndefValue::get(BO->getType()));
1864f22ef01cSRoman Divacky BO->eraseFromParent();
1865f22ef01cSRoman Divacky } else if (SplitVal->isZero()) {
1866f22ef01cSRoman Divacky // If all preds provide 0, replace the xor with the other input.
1867f22ef01cSRoman Divacky BO->replaceAllUsesWith(BO->getOperand(isLHS));
1868f22ef01cSRoman Divacky BO->eraseFromParent();
1869f22ef01cSRoman Divacky } else {
1870f22ef01cSRoman Divacky // If all preds provide 1, set the computed value to 1.
1871f22ef01cSRoman Divacky BO->setOperand(!isLHS, SplitVal);
1872f22ef01cSRoman Divacky }
1873f22ef01cSRoman Divacky
1874f22ef01cSRoman Divacky return true;
1875f22ef01cSRoman Divacky }
1876f22ef01cSRoman Divacky
1877f22ef01cSRoman Divacky // Try to duplicate BB into PredBB.
1878f22ef01cSRoman Divacky return DuplicateCondBranchOnPHIIntoPred(BB, BlocksToFoldInto);
1879f22ef01cSRoman Divacky }
1880f22ef01cSRoman Divacky
1881f22ef01cSRoman Divacky /// AddPHINodeEntriesForMappedBlock - We're adding 'NewPred' as a new
1882f22ef01cSRoman Divacky /// predecessor to the PHIBB block. If it has PHI nodes, add entries for
1883f22ef01cSRoman Divacky /// NewPred using the entries from OldPred (suitably mapped).
AddPHINodeEntriesForMappedBlock(BasicBlock * PHIBB,BasicBlock * OldPred,BasicBlock * NewPred,DenseMap<Instruction *,Value * > & ValueMap)1884f22ef01cSRoman Divacky static void AddPHINodeEntriesForMappedBlock(BasicBlock *PHIBB,
1885f22ef01cSRoman Divacky BasicBlock *OldPred,
1886f22ef01cSRoman Divacky BasicBlock *NewPred,
1887f22ef01cSRoman Divacky DenseMap<Instruction*, Value*> &ValueMap) {
188830785c0eSDimitry Andric for (PHINode &PN : PHIBB->phis()) {
1889f22ef01cSRoman Divacky // Ok, we have a PHI node. Figure out what the incoming value was for the
1890f22ef01cSRoman Divacky // DestBlock.
189130785c0eSDimitry Andric Value *IV = PN.getIncomingValueForBlock(OldPred);
1892f22ef01cSRoman Divacky
1893f22ef01cSRoman Divacky // Remap the value if necessary.
1894f22ef01cSRoman Divacky if (Instruction *Inst = dyn_cast<Instruction>(IV)) {
1895f22ef01cSRoman Divacky DenseMap<Instruction*, Value*>::iterator I = ValueMap.find(Inst);
1896f22ef01cSRoman Divacky if (I != ValueMap.end())
1897f22ef01cSRoman Divacky IV = I->second;
1898f22ef01cSRoman Divacky }
1899f22ef01cSRoman Divacky
190030785c0eSDimitry Andric PN.addIncoming(IV, NewPred);
1901f22ef01cSRoman Divacky }
1902f22ef01cSRoman Divacky }
1903f22ef01cSRoman Divacky
1904f22ef01cSRoman Divacky /// ThreadEdge - We have decided that it is safe and profitable to factor the
1905f22ef01cSRoman Divacky /// blocks in PredBBs to one predecessor, then thread an edge from it to SuccBB
1906f22ef01cSRoman Divacky /// across BB. Transform the IR to reflect this change.
ThreadEdge(BasicBlock * BB,const SmallVectorImpl<BasicBlock * > & PredBBs,BasicBlock * SuccBB)19073ca95b02SDimitry Andric bool JumpThreadingPass::ThreadEdge(BasicBlock *BB,
1908f22ef01cSRoman Divacky const SmallVectorImpl<BasicBlock *> &PredBBs,
1909f22ef01cSRoman Divacky BasicBlock *SuccBB) {
1910f22ef01cSRoman Divacky // If threading to the same block as we come from, we would infinite loop.
1911f22ef01cSRoman Divacky if (SuccBB == BB) {
19124ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << " Not threading across BB '" << BB->getName()
1913f22ef01cSRoman Divacky << "' - would thread to self!\n");
1914f22ef01cSRoman Divacky return false;
1915f22ef01cSRoman Divacky }
1916f22ef01cSRoman Divacky
1917f22ef01cSRoman Divacky // If threading this would thread across a loop header, don't thread the edge.
1918f22ef01cSRoman Divacky // See the comments above FindLoopHeaders for justifications and caveats.
19192cab237bSDimitry Andric if (LoopHeaders.count(BB) || LoopHeaders.count(SuccBB)) {
19204ba319b5SDimitry Andric LLVM_DEBUG({
19212cab237bSDimitry Andric bool BBIsHeader = LoopHeaders.count(BB);
19222cab237bSDimitry Andric bool SuccIsHeader = LoopHeaders.count(SuccBB);
19232cab237bSDimitry Andric dbgs() << " Not threading across "
19242cab237bSDimitry Andric << (BBIsHeader ? "loop header BB '" : "block BB '") << BB->getName()
19252cab237bSDimitry Andric << "' to dest " << (SuccIsHeader ? "loop header BB '" : "block BB '")
19262cab237bSDimitry Andric << SuccBB->getName() << "' - it might create an irreducible loop!\n";
19272cab237bSDimitry Andric });
1928f22ef01cSRoman Divacky return false;
1929f22ef01cSRoman Divacky }
1930f22ef01cSRoman Divacky
19317a7e6055SDimitry Andric unsigned JumpThreadCost =
19327a7e6055SDimitry Andric getJumpThreadDuplicationCost(BB, BB->getTerminator(), BBDupThreshold);
193339d628a0SDimitry Andric if (JumpThreadCost > BBDupThreshold) {
19344ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << " Not threading BB '" << BB->getName()
1935f22ef01cSRoman Divacky << "' - Cost is too high: " << JumpThreadCost << "\n");
1936f22ef01cSRoman Divacky return false;
1937f22ef01cSRoman Divacky }
1938f22ef01cSRoman Divacky
19397d523365SDimitry Andric // And finally, do it! Start by factoring the predecessors if needed.
1940f22ef01cSRoman Divacky BasicBlock *PredBB;
1941f22ef01cSRoman Divacky if (PredBBs.size() == 1)
1942f22ef01cSRoman Divacky PredBB = PredBBs[0];
1943f22ef01cSRoman Divacky else {
19444ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << " Factoring out " << PredBBs.size()
1945f22ef01cSRoman Divacky << " common predecessors.\n");
19467d523365SDimitry Andric PredBB = SplitBlockPreds(BB, PredBBs, ".thr_comm");
1947f22ef01cSRoman Divacky }
1948f22ef01cSRoman Divacky
1949f22ef01cSRoman Divacky // And finally, do it!
19504ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << " Threading edge from '" << PredBB->getName()
19514ba319b5SDimitry Andric << "' to '" << SuccBB->getName()
19524ba319b5SDimitry Andric << "' with cost: " << JumpThreadCost
19534ba319b5SDimitry Andric << ", across block:\n " << *BB << "\n");
1954f22ef01cSRoman Divacky
1955*b5893f02SDimitry Andric if (DTU->hasPendingDomTreeUpdates())
19564ba319b5SDimitry Andric LVI->disableDT();
19574ba319b5SDimitry Andric else
19584ba319b5SDimitry Andric LVI->enableDT();
1959e580952dSDimitry Andric LVI->threadEdge(PredBB, BB, SuccBB);
1960e580952dSDimitry Andric
1961f22ef01cSRoman Divacky // We are going to have to map operands from the original BB block to the new
1962f22ef01cSRoman Divacky // copy of the block 'NewBB'. If there are PHI nodes in BB, evaluate them to
1963f22ef01cSRoman Divacky // account for entry from PredBB.
1964f22ef01cSRoman Divacky DenseMap<Instruction*, Value*> ValueMapping;
1965f22ef01cSRoman Divacky
1966f22ef01cSRoman Divacky BasicBlock *NewBB = BasicBlock::Create(BB->getContext(),
1967f22ef01cSRoman Divacky BB->getName()+".thread",
1968f22ef01cSRoman Divacky BB->getParent(), BB);
1969f22ef01cSRoman Divacky NewBB->moveAfter(PredBB);
1970f22ef01cSRoman Divacky
19717d523365SDimitry Andric // Set the block frequency of NewBB.
19727d523365SDimitry Andric if (HasProfileData) {
19737d523365SDimitry Andric auto NewBBFreq =
19747d523365SDimitry Andric BFI->getBlockFreq(PredBB) * BPI->getEdgeProbability(PredBB, BB);
19757d523365SDimitry Andric BFI->setBlockFreq(NewBB, NewBBFreq.getFrequency());
19767d523365SDimitry Andric }
19777d523365SDimitry Andric
1978f22ef01cSRoman Divacky BasicBlock::iterator BI = BB->begin();
1979f22ef01cSRoman Divacky for (; PHINode *PN = dyn_cast<PHINode>(BI); ++BI)
1980f22ef01cSRoman Divacky ValueMapping[PN] = PN->getIncomingValueForBlock(PredBB);
1981f22ef01cSRoman Divacky
1982f22ef01cSRoman Divacky // Clone the non-phi instructions of BB into NewBB, keeping track of the
1983f22ef01cSRoman Divacky // mapping and using it to remap operands in the cloned instructions.
1984*b5893f02SDimitry Andric for (; !BI->isTerminator(); ++BI) {
1985f22ef01cSRoman Divacky Instruction *New = BI->clone();
1986f22ef01cSRoman Divacky New->setName(BI->getName());
1987f22ef01cSRoman Divacky NewBB->getInstList().push_back(New);
19887d523365SDimitry Andric ValueMapping[&*BI] = New;
1989f22ef01cSRoman Divacky
1990f22ef01cSRoman Divacky // Remap operands to patch up intra-block references.
1991f22ef01cSRoman Divacky for (unsigned i = 0, e = New->getNumOperands(); i != e; ++i)
1992f22ef01cSRoman Divacky if (Instruction *Inst = dyn_cast<Instruction>(New->getOperand(i))) {
1993f22ef01cSRoman Divacky DenseMap<Instruction*, Value*>::iterator I = ValueMapping.find(Inst);
1994f22ef01cSRoman Divacky if (I != ValueMapping.end())
1995f22ef01cSRoman Divacky New->setOperand(i, I->second);
1996f22ef01cSRoman Divacky }
1997f22ef01cSRoman Divacky }
1998f22ef01cSRoman Divacky
1999f22ef01cSRoman Divacky // We didn't copy the terminator from BB over to NewBB, because there is now
2000f22ef01cSRoman Divacky // an unconditional jump to SuccBB. Insert the unconditional jump.
2001bd5abe19SDimitry Andric BranchInst *NewBI = BranchInst::Create(SuccBB, NewBB);
2002bd5abe19SDimitry Andric NewBI->setDebugLoc(BB->getTerminator()->getDebugLoc());
2003f22ef01cSRoman Divacky
2004f22ef01cSRoman Divacky // Check to see if SuccBB has PHI nodes. If so, we need to add entries to the
2005f22ef01cSRoman Divacky // PHI nodes for NewBB now.
2006f22ef01cSRoman Divacky AddPHINodeEntriesForMappedBlock(SuccBB, BB, NewBB, ValueMapping);
2007f22ef01cSRoman Divacky
20084ba319b5SDimitry Andric // Update the terminator of PredBB to jump to NewBB instead of BB. This
20094ba319b5SDimitry Andric // eliminates predecessors from BB, which requires us to simplify any PHI
20104ba319b5SDimitry Andric // nodes in BB.
2011*b5893f02SDimitry Andric Instruction *PredTerm = PredBB->getTerminator();
20124ba319b5SDimitry Andric for (unsigned i = 0, e = PredTerm->getNumSuccessors(); i != e; ++i)
20134ba319b5SDimitry Andric if (PredTerm->getSuccessor(i) == BB) {
20144ba319b5SDimitry Andric BB->removePredecessor(PredBB, true);
20154ba319b5SDimitry Andric PredTerm->setSuccessor(i, NewBB);
20164ba319b5SDimitry Andric }
20174ba319b5SDimitry Andric
20184ba319b5SDimitry Andric // Enqueue required DT updates.
2019*b5893f02SDimitry Andric DTU->applyUpdates({{DominatorTree::Insert, NewBB, SuccBB},
20204ba319b5SDimitry Andric {DominatorTree::Insert, PredBB, NewBB},
20214ba319b5SDimitry Andric {DominatorTree::Delete, PredBB, BB}});
20224ba319b5SDimitry Andric
2023f22ef01cSRoman Divacky // If there were values defined in BB that are used outside the block, then we
2024f22ef01cSRoman Divacky // now have to update all uses of the value to use either the original value,
2025f22ef01cSRoman Divacky // the cloned value, or some PHI derived value. This can require arbitrary
2026f22ef01cSRoman Divacky // PHI insertion, of which we are prepared to do, clean these up now.
2027f22ef01cSRoman Divacky SSAUpdater SSAUpdate;
2028f22ef01cSRoman Divacky SmallVector<Use*, 16> UsesToRename;
20294ba319b5SDimitry Andric
2030444ed5c5SDimitry Andric for (Instruction &I : *BB) {
20314ba319b5SDimitry Andric // Scan all uses of this instruction to see if their uses are no longer
20324ba319b5SDimitry Andric // dominated by the previous def and if so, record them in UsesToRename.
20334ba319b5SDimitry Andric // Also, skip phi operands from PredBB - we'll remove them anyway.
2034444ed5c5SDimitry Andric for (Use &U : I.uses()) {
203591bc56edSDimitry Andric Instruction *User = cast<Instruction>(U.getUser());
2036f22ef01cSRoman Divacky if (PHINode *UserPN = dyn_cast<PHINode>(User)) {
203791bc56edSDimitry Andric if (UserPN->getIncomingBlock(U) == BB)
2038f22ef01cSRoman Divacky continue;
2039f22ef01cSRoman Divacky } else if (User->getParent() == BB)
2040f22ef01cSRoman Divacky continue;
2041f22ef01cSRoman Divacky
204291bc56edSDimitry Andric UsesToRename.push_back(&U);
2043f22ef01cSRoman Divacky }
2044f22ef01cSRoman Divacky
2045f22ef01cSRoman Divacky // If there are no uses outside the block, we're done with this instruction.
2046f22ef01cSRoman Divacky if (UsesToRename.empty())
2047f22ef01cSRoman Divacky continue;
20484ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "JT: Renaming non-local uses of: " << I << "\n");
2049f22ef01cSRoman Divacky
2050f22ef01cSRoman Divacky // We found a use of I outside of BB. Rename all uses of I that are outside
2051f22ef01cSRoman Divacky // its block to be uses of the appropriate PHI node etc. See ValuesInBlocks
2052f22ef01cSRoman Divacky // with the two values we know.
2053444ed5c5SDimitry Andric SSAUpdate.Initialize(I.getType(), I.getName());
2054444ed5c5SDimitry Andric SSAUpdate.AddAvailableValue(BB, &I);
2055444ed5c5SDimitry Andric SSAUpdate.AddAvailableValue(NewBB, ValueMapping[&I]);
2056f22ef01cSRoman Divacky
2057f22ef01cSRoman Divacky while (!UsesToRename.empty())
2058f22ef01cSRoman Divacky SSAUpdate.RewriteUse(*UsesToRename.pop_back_val());
20594ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "\n");
2060f22ef01cSRoman Divacky }
2061f22ef01cSRoman Divacky
2062f22ef01cSRoman Divacky // At this point, the IR is fully up to date and consistent. Do a quick scan
2063f22ef01cSRoman Divacky // over the new instructions and zap any that are constants or dead. This
2064f22ef01cSRoman Divacky // frequently happens because of phi translation.
2065ff0cc061SDimitry Andric SimplifyInstructionsInBlock(NewBB, TLI);
2066f22ef01cSRoman Divacky
20677d523365SDimitry Andric // Update the edge weight from BB to SuccBB, which should be less than before.
20687d523365SDimitry Andric UpdateBlockFreqAndEdgeWeight(PredBB, BB, NewBB, SuccBB);
20697d523365SDimitry Andric
2070f22ef01cSRoman Divacky // Threaded an edge!
2071f22ef01cSRoman Divacky ++NumThreads;
2072f22ef01cSRoman Divacky return true;
2073f22ef01cSRoman Divacky }
2074f22ef01cSRoman Divacky
20757d523365SDimitry Andric /// Create a new basic block that will be the predecessor of BB and successor of
2076d88c1a5aSDimitry Andric /// all blocks in Preds. When profile data is available, update the frequency of
20777d523365SDimitry Andric /// this new block.
SplitBlockPreds(BasicBlock * BB,ArrayRef<BasicBlock * > Preds,const char * Suffix)20783ca95b02SDimitry Andric BasicBlock *JumpThreadingPass::SplitBlockPreds(BasicBlock *BB,
20797d523365SDimitry Andric ArrayRef<BasicBlock *> Preds,
20807d523365SDimitry Andric const char *Suffix) {
20814ba319b5SDimitry Andric SmallVector<BasicBlock *, 2> NewBBs;
20824ba319b5SDimitry Andric
20837d523365SDimitry Andric // Collect the frequencies of all predecessors of BB, which will be used to
20844ba319b5SDimitry Andric // update the edge weight of the result of splitting predecessors.
20854ba319b5SDimitry Andric DenseMap<BasicBlock *, BlockFrequency> FreqMap;
20867d523365SDimitry Andric if (HasProfileData)
20877d523365SDimitry Andric for (auto Pred : Preds)
20884ba319b5SDimitry Andric FreqMap.insert(std::make_pair(
20894ba319b5SDimitry Andric Pred, BFI->getBlockFreq(Pred) * BPI->getEdgeProbability(Pred, BB)));
20907d523365SDimitry Andric
20914ba319b5SDimitry Andric // In the case when BB is a LandingPad block we create 2 new predecessors
20924ba319b5SDimitry Andric // instead of just one.
20934ba319b5SDimitry Andric if (BB->isLandingPad()) {
20944ba319b5SDimitry Andric std::string NewName = std::string(Suffix) + ".split-lp";
20954ba319b5SDimitry Andric SplitLandingPadPredecessors(BB, Preds, Suffix, NewName.c_str(), NewBBs);
20964ba319b5SDimitry Andric } else {
20974ba319b5SDimitry Andric NewBBs.push_back(SplitBlockPredecessors(BB, Preds, Suffix));
20984ba319b5SDimitry Andric }
20997d523365SDimitry Andric
21004ba319b5SDimitry Andric std::vector<DominatorTree::UpdateType> Updates;
21014ba319b5SDimitry Andric Updates.reserve((2 * Preds.size()) + NewBBs.size());
21024ba319b5SDimitry Andric for (auto NewBB : NewBBs) {
21034ba319b5SDimitry Andric BlockFrequency NewBBFreq(0);
21044ba319b5SDimitry Andric Updates.push_back({DominatorTree::Insert, NewBB, BB});
21054ba319b5SDimitry Andric for (auto Pred : predecessors(NewBB)) {
21064ba319b5SDimitry Andric Updates.push_back({DominatorTree::Delete, Pred, BB});
21074ba319b5SDimitry Andric Updates.push_back({DominatorTree::Insert, Pred, NewBB});
21084ba319b5SDimitry Andric if (HasProfileData) // Update frequencies between Pred -> NewBB.
21094ba319b5SDimitry Andric NewBBFreq += FreqMap.lookup(Pred);
21104ba319b5SDimitry Andric }
21114ba319b5SDimitry Andric if (HasProfileData) // Apply the summed frequency to NewBB.
21124ba319b5SDimitry Andric BFI->setBlockFreq(NewBB, NewBBFreq.getFrequency());
21134ba319b5SDimitry Andric }
21144ba319b5SDimitry Andric
2115*b5893f02SDimitry Andric DTU->applyUpdates(Updates);
21164ba319b5SDimitry Andric return NewBBs[0];
21177d523365SDimitry Andric }
21187d523365SDimitry Andric
doesBlockHaveProfileData(BasicBlock * BB)2119d88c1a5aSDimitry Andric bool JumpThreadingPass::doesBlockHaveProfileData(BasicBlock *BB) {
2120*b5893f02SDimitry Andric const Instruction *TI = BB->getTerminator();
2121d88c1a5aSDimitry Andric assert(TI->getNumSuccessors() > 1 && "not a split");
2122d88c1a5aSDimitry Andric
2123d88c1a5aSDimitry Andric MDNode *WeightsNode = TI->getMetadata(LLVMContext::MD_prof);
2124d88c1a5aSDimitry Andric if (!WeightsNode)
2125d88c1a5aSDimitry Andric return false;
2126d88c1a5aSDimitry Andric
2127d88c1a5aSDimitry Andric MDString *MDName = cast<MDString>(WeightsNode->getOperand(0));
2128d88c1a5aSDimitry Andric if (MDName->getString() != "branch_weights")
2129d88c1a5aSDimitry Andric return false;
2130d88c1a5aSDimitry Andric
2131d88c1a5aSDimitry Andric // Ensure there are weights for all of the successors. Note that the first
2132d88c1a5aSDimitry Andric // operand to the metadata node is a name, not a weight.
2133d88c1a5aSDimitry Andric return WeightsNode->getNumOperands() == TI->getNumSuccessors() + 1;
2134d88c1a5aSDimitry Andric }
2135d88c1a5aSDimitry Andric
21367d523365SDimitry Andric /// Update the block frequency of BB and branch weight and the metadata on the
21377d523365SDimitry Andric /// edge BB->SuccBB. This is done by scaling the weight of BB->SuccBB by 1 -
21387d523365SDimitry Andric /// Freq(PredBB->BB) / Freq(BB->SuccBB).
UpdateBlockFreqAndEdgeWeight(BasicBlock * PredBB,BasicBlock * BB,BasicBlock * NewBB,BasicBlock * SuccBB)21393ca95b02SDimitry Andric void JumpThreadingPass::UpdateBlockFreqAndEdgeWeight(BasicBlock *PredBB,
21407d523365SDimitry Andric BasicBlock *BB,
21417d523365SDimitry Andric BasicBlock *NewBB,
21427d523365SDimitry Andric BasicBlock *SuccBB) {
21437d523365SDimitry Andric if (!HasProfileData)
21447d523365SDimitry Andric return;
21457d523365SDimitry Andric
21467d523365SDimitry Andric assert(BFI && BPI && "BFI & BPI should have been created here");
21477d523365SDimitry Andric
21487d523365SDimitry Andric // As the edge from PredBB to BB is deleted, we have to update the block
21497d523365SDimitry Andric // frequency of BB.
21507d523365SDimitry Andric auto BBOrigFreq = BFI->getBlockFreq(BB);
21517d523365SDimitry Andric auto NewBBFreq = BFI->getBlockFreq(NewBB);
21527d523365SDimitry Andric auto BB2SuccBBFreq = BBOrigFreq * BPI->getEdgeProbability(BB, SuccBB);
21537d523365SDimitry Andric auto BBNewFreq = BBOrigFreq - NewBBFreq;
21547d523365SDimitry Andric BFI->setBlockFreq(BB, BBNewFreq.getFrequency());
21557d523365SDimitry Andric
21567d523365SDimitry Andric // Collect updated outgoing edges' frequencies from BB and use them to update
21577d523365SDimitry Andric // edge probabilities.
21587d523365SDimitry Andric SmallVector<uint64_t, 4> BBSuccFreq;
2159444ed5c5SDimitry Andric for (BasicBlock *Succ : successors(BB)) {
2160444ed5c5SDimitry Andric auto SuccFreq = (Succ == SuccBB)
21617d523365SDimitry Andric ? BB2SuccBBFreq - NewBBFreq
2162444ed5c5SDimitry Andric : BBOrigFreq * BPI->getEdgeProbability(BB, Succ);
21637d523365SDimitry Andric BBSuccFreq.push_back(SuccFreq.getFrequency());
21647d523365SDimitry Andric }
21657d523365SDimitry Andric
21667d523365SDimitry Andric uint64_t MaxBBSuccFreq =
21677d523365SDimitry Andric *std::max_element(BBSuccFreq.begin(), BBSuccFreq.end());
21687d523365SDimitry Andric
21697d523365SDimitry Andric SmallVector<BranchProbability, 4> BBSuccProbs;
21707d523365SDimitry Andric if (MaxBBSuccFreq == 0)
21717d523365SDimitry Andric BBSuccProbs.assign(BBSuccFreq.size(),
21727d523365SDimitry Andric {1, static_cast<unsigned>(BBSuccFreq.size())});
21737d523365SDimitry Andric else {
21747d523365SDimitry Andric for (uint64_t Freq : BBSuccFreq)
21757d523365SDimitry Andric BBSuccProbs.push_back(
21767d523365SDimitry Andric BranchProbability::getBranchProbability(Freq, MaxBBSuccFreq));
21777d523365SDimitry Andric // Normalize edge probabilities so that they sum up to one.
21787d523365SDimitry Andric BranchProbability::normalizeProbabilities(BBSuccProbs.begin(),
21797d523365SDimitry Andric BBSuccProbs.end());
21807d523365SDimitry Andric }
21817d523365SDimitry Andric
21827d523365SDimitry Andric // Update edge probabilities in BPI.
21837d523365SDimitry Andric for (int I = 0, E = BBSuccProbs.size(); I < E; I++)
21847d523365SDimitry Andric BPI->setEdgeProbability(BB, I, BBSuccProbs[I]);
21857d523365SDimitry Andric
2186d88c1a5aSDimitry Andric // Update the profile metadata as well.
2187d88c1a5aSDimitry Andric //
2188d88c1a5aSDimitry Andric // Don't do this if the profile of the transformed blocks was statically
2189d88c1a5aSDimitry Andric // estimated. (This could occur despite the function having an entry
2190d88c1a5aSDimitry Andric // frequency in completely cold parts of the CFG.)
2191d88c1a5aSDimitry Andric //
2192d88c1a5aSDimitry Andric // In this case we don't want to suggest to subsequent passes that the
2193d88c1a5aSDimitry Andric // calculated weights are fully consistent. Consider this graph:
2194d88c1a5aSDimitry Andric //
2195d88c1a5aSDimitry Andric // check_1
2196d88c1a5aSDimitry Andric // 50% / |
2197d88c1a5aSDimitry Andric // eq_1 | 50%
2198d88c1a5aSDimitry Andric // \ |
2199d88c1a5aSDimitry Andric // check_2
2200d88c1a5aSDimitry Andric // 50% / |
2201d88c1a5aSDimitry Andric // eq_2 | 50%
2202d88c1a5aSDimitry Andric // \ |
2203d88c1a5aSDimitry Andric // check_3
2204d88c1a5aSDimitry Andric // 50% / |
2205d88c1a5aSDimitry Andric // eq_3 | 50%
2206d88c1a5aSDimitry Andric // \ |
2207d88c1a5aSDimitry Andric //
2208d88c1a5aSDimitry Andric // Assuming the blocks check_* all compare the same value against 1, 2 and 3,
2209d88c1a5aSDimitry Andric // the overall probabilities are inconsistent; the total probability that the
2210d88c1a5aSDimitry Andric // value is either 1, 2 or 3 is 150%.
2211d88c1a5aSDimitry Andric //
2212d88c1a5aSDimitry Andric // As a consequence if we thread eq_1 -> check_2 to check_3, check_2->check_3
2213d88c1a5aSDimitry Andric // becomes 0%. This is even worse if the edge whose probability becomes 0% is
2214d88c1a5aSDimitry Andric // the loop exit edge. Then based solely on static estimation we would assume
2215d88c1a5aSDimitry Andric // the loop was extremely hot.
2216d88c1a5aSDimitry Andric //
2217d88c1a5aSDimitry Andric // FIXME this locally as well so that BPI and BFI are consistent as well. We
2218d88c1a5aSDimitry Andric // shouldn't make edges extremely likely or unlikely based solely on static
2219d88c1a5aSDimitry Andric // estimation.
2220d88c1a5aSDimitry Andric if (BBSuccProbs.size() >= 2 && doesBlockHaveProfileData(BB)) {
22217d523365SDimitry Andric SmallVector<uint32_t, 4> Weights;
22227d523365SDimitry Andric for (auto Prob : BBSuccProbs)
22237d523365SDimitry Andric Weights.push_back(Prob.getNumerator());
22247d523365SDimitry Andric
22257d523365SDimitry Andric auto TI = BB->getTerminator();
22267d523365SDimitry Andric TI->setMetadata(
22277d523365SDimitry Andric LLVMContext::MD_prof,
22287d523365SDimitry Andric MDBuilder(TI->getParent()->getContext()).createBranchWeights(Weights));
22297d523365SDimitry Andric }
22307d523365SDimitry Andric }
22317d523365SDimitry Andric
2232f22ef01cSRoman Divacky /// DuplicateCondBranchOnPHIIntoPred - PredBB contains an unconditional branch
2233f22ef01cSRoman Divacky /// to BB which contains an i1 PHI node and a conditional branch on that PHI.
2234f22ef01cSRoman Divacky /// If we can duplicate the contents of BB up into PredBB do so now, this
2235f22ef01cSRoman Divacky /// improves the odds that the branch will be on an analyzable instruction like
2236f22ef01cSRoman Divacky /// a compare.
DuplicateCondBranchOnPHIIntoPred(BasicBlock * BB,const SmallVectorImpl<BasicBlock * > & PredBBs)22373ca95b02SDimitry Andric bool JumpThreadingPass::DuplicateCondBranchOnPHIIntoPred(
22383ca95b02SDimitry Andric BasicBlock *BB, const SmallVectorImpl<BasicBlock *> &PredBBs) {
2239f22ef01cSRoman Divacky assert(!PredBBs.empty() && "Can't handle an empty set");
2240f22ef01cSRoman Divacky
2241f22ef01cSRoman Divacky // If BB is a loop header, then duplicating this block outside the loop would
2242f22ef01cSRoman Divacky // cause us to transform this into an irreducible loop, don't do this.
2243f22ef01cSRoman Divacky // See the comments above FindLoopHeaders for justifications and caveats.
2244f22ef01cSRoman Divacky if (LoopHeaders.count(BB)) {
22454ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << " Not duplicating loop header '" << BB->getName()
2246f22ef01cSRoman Divacky << "' into predecessor block '" << PredBBs[0]->getName()
2247f22ef01cSRoman Divacky << "' - it might create an irreducible loop!\n");
2248f22ef01cSRoman Divacky return false;
2249f22ef01cSRoman Divacky }
2250f22ef01cSRoman Divacky
22517a7e6055SDimitry Andric unsigned DuplicationCost =
22527a7e6055SDimitry Andric getJumpThreadDuplicationCost(BB, BB->getTerminator(), BBDupThreshold);
225339d628a0SDimitry Andric if (DuplicationCost > BBDupThreshold) {
22544ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << " Not duplicating BB '" << BB->getName()
2255f22ef01cSRoman Divacky << "' - Cost is too high: " << DuplicationCost << "\n");
2256f22ef01cSRoman Divacky return false;
2257f22ef01cSRoman Divacky }
2258f22ef01cSRoman Divacky
22597d523365SDimitry Andric // And finally, do it! Start by factoring the predecessors if needed.
22604ba319b5SDimitry Andric std::vector<DominatorTree::UpdateType> Updates;
2261f22ef01cSRoman Divacky BasicBlock *PredBB;
2262f22ef01cSRoman Divacky if (PredBBs.size() == 1)
2263f22ef01cSRoman Divacky PredBB = PredBBs[0];
2264f22ef01cSRoman Divacky else {
22654ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << " Factoring out " << PredBBs.size()
2266f22ef01cSRoman Divacky << " common predecessors.\n");
22677d523365SDimitry Andric PredBB = SplitBlockPreds(BB, PredBBs, ".thr_comm");
2268f22ef01cSRoman Divacky }
22694ba319b5SDimitry Andric Updates.push_back({DominatorTree::Delete, PredBB, BB});
2270f22ef01cSRoman Divacky
2271f22ef01cSRoman Divacky // Okay, we decided to do this! Clone all the instructions in BB onto the end
2272f22ef01cSRoman Divacky // of PredBB.
22734ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << " Duplicating block '" << BB->getName()
22744ba319b5SDimitry Andric << "' into end of '" << PredBB->getName()
22754ba319b5SDimitry Andric << "' to eliminate branch on phi. Cost: "
2276f22ef01cSRoman Divacky << DuplicationCost << " block is:" << *BB << "\n");
2277f22ef01cSRoman Divacky
2278f22ef01cSRoman Divacky // Unless PredBB ends with an unconditional branch, split the edge so that we
2279f22ef01cSRoman Divacky // can just clone the bits from BB into the end of the new PredBB.
2280f22ef01cSRoman Divacky BranchInst *OldPredBranch = dyn_cast<BranchInst>(PredBB->getTerminator());
2281f22ef01cSRoman Divacky
228291bc56edSDimitry Andric if (!OldPredBranch || !OldPredBranch->isUnconditional()) {
22834ba319b5SDimitry Andric BasicBlock *OldPredBB = PredBB;
22844ba319b5SDimitry Andric PredBB = SplitEdge(OldPredBB, BB);
22854ba319b5SDimitry Andric Updates.push_back({DominatorTree::Insert, OldPredBB, PredBB});
22864ba319b5SDimitry Andric Updates.push_back({DominatorTree::Insert, PredBB, BB});
22874ba319b5SDimitry Andric Updates.push_back({DominatorTree::Delete, OldPredBB, BB});
2288f22ef01cSRoman Divacky OldPredBranch = cast<BranchInst>(PredBB->getTerminator());
2289f22ef01cSRoman Divacky }
2290f22ef01cSRoman Divacky
2291f22ef01cSRoman Divacky // We are going to have to map operands from the original BB block into the
2292f22ef01cSRoman Divacky // PredBB block. Evaluate PHI nodes in BB.
2293f22ef01cSRoman Divacky DenseMap<Instruction*, Value*> ValueMapping;
2294f22ef01cSRoman Divacky
2295f22ef01cSRoman Divacky BasicBlock::iterator BI = BB->begin();
2296f22ef01cSRoman Divacky for (; PHINode *PN = dyn_cast<PHINode>(BI); ++BI)
2297f22ef01cSRoman Divacky ValueMapping[PN] = PN->getIncomingValueForBlock(PredBB);
2298f22ef01cSRoman Divacky // Clone the non-phi instructions of BB into PredBB, keeping track of the
2299f22ef01cSRoman Divacky // mapping and using it to remap operands in the cloned instructions.
2300f22ef01cSRoman Divacky for (; BI != BB->end(); ++BI) {
2301f22ef01cSRoman Divacky Instruction *New = BI->clone();
2302f22ef01cSRoman Divacky
2303f22ef01cSRoman Divacky // Remap operands to patch up intra-block references.
2304f22ef01cSRoman Divacky for (unsigned i = 0, e = New->getNumOperands(); i != e; ++i)
2305f22ef01cSRoman Divacky if (Instruction *Inst = dyn_cast<Instruction>(New->getOperand(i))) {
2306f22ef01cSRoman Divacky DenseMap<Instruction*, Value*>::iterator I = ValueMapping.find(Inst);
2307f22ef01cSRoman Divacky if (I != ValueMapping.end())
2308f22ef01cSRoman Divacky New->setOperand(i, I->second);
2309f22ef01cSRoman Divacky }
2310f22ef01cSRoman Divacky
2311f22ef01cSRoman Divacky // If this instruction can be simplified after the operands are updated,
2312f22ef01cSRoman Divacky // just use the simplified value instead. This frequently happens due to
2313f22ef01cSRoman Divacky // phi translation.
2314f37b6182SDimitry Andric if (Value *IV = SimplifyInstruction(
2315f37b6182SDimitry Andric New,
2316f37b6182SDimitry Andric {BB->getModule()->getDataLayout(), TLI, nullptr, nullptr, New})) {
23177d523365SDimitry Andric ValueMapping[&*BI] = IV;
23183ca95b02SDimitry Andric if (!New->mayHaveSideEffects()) {
2319d8866befSDimitry Andric New->deleteValue();
23203ca95b02SDimitry Andric New = nullptr;
23213ca95b02SDimitry Andric }
2322f22ef01cSRoman Divacky } else {
23233ca95b02SDimitry Andric ValueMapping[&*BI] = New;
23243ca95b02SDimitry Andric }
23253ca95b02SDimitry Andric if (New) {
2326f22ef01cSRoman Divacky // Otherwise, insert the new instruction into the block.
2327f22ef01cSRoman Divacky New->setName(BI->getName());
23287d523365SDimitry Andric PredBB->getInstList().insert(OldPredBranch->getIterator(), New);
23294ba319b5SDimitry Andric // Update Dominance from simplified New instruction operands.
23304ba319b5SDimitry Andric for (unsigned i = 0, e = New->getNumOperands(); i != e; ++i)
23314ba319b5SDimitry Andric if (BasicBlock *SuccBB = dyn_cast<BasicBlock>(New->getOperand(i)))
23324ba319b5SDimitry Andric Updates.push_back({DominatorTree::Insert, PredBB, SuccBB});
2333f22ef01cSRoman Divacky }
2334f22ef01cSRoman Divacky }
2335f22ef01cSRoman Divacky
2336f22ef01cSRoman Divacky // Check to see if the targets of the branch had PHI nodes. If so, we need to
2337f22ef01cSRoman Divacky // add entries to the PHI nodes for branch from PredBB now.
2338f22ef01cSRoman Divacky BranchInst *BBBranch = cast<BranchInst>(BB->getTerminator());
2339f22ef01cSRoman Divacky AddPHINodeEntriesForMappedBlock(BBBranch->getSuccessor(0), BB, PredBB,
2340f22ef01cSRoman Divacky ValueMapping);
2341f22ef01cSRoman Divacky AddPHINodeEntriesForMappedBlock(BBBranch->getSuccessor(1), BB, PredBB,
2342f22ef01cSRoman Divacky ValueMapping);
2343f22ef01cSRoman Divacky
2344f22ef01cSRoman Divacky // If there were values defined in BB that are used outside the block, then we
2345f22ef01cSRoman Divacky // now have to update all uses of the value to use either the original value,
2346f22ef01cSRoman Divacky // the cloned value, or some PHI derived value. This can require arbitrary
2347f22ef01cSRoman Divacky // PHI insertion, of which we are prepared to do, clean these up now.
2348f22ef01cSRoman Divacky SSAUpdater SSAUpdate;
2349f22ef01cSRoman Divacky SmallVector<Use*, 16> UsesToRename;
2350444ed5c5SDimitry Andric for (Instruction &I : *BB) {
2351f22ef01cSRoman Divacky // Scan all uses of this instruction to see if it is used outside of its
2352f22ef01cSRoman Divacky // block, and if so, record them in UsesToRename.
2353444ed5c5SDimitry Andric for (Use &U : I.uses()) {
235491bc56edSDimitry Andric Instruction *User = cast<Instruction>(U.getUser());
2355f22ef01cSRoman Divacky if (PHINode *UserPN = dyn_cast<PHINode>(User)) {
235691bc56edSDimitry Andric if (UserPN->getIncomingBlock(U) == BB)
2357f22ef01cSRoman Divacky continue;
2358f22ef01cSRoman Divacky } else if (User->getParent() == BB)
2359f22ef01cSRoman Divacky continue;
2360f22ef01cSRoman Divacky
236191bc56edSDimitry Andric UsesToRename.push_back(&U);
2362f22ef01cSRoman Divacky }
2363f22ef01cSRoman Divacky
2364f22ef01cSRoman Divacky // If there are no uses outside the block, we're done with this instruction.
2365f22ef01cSRoman Divacky if (UsesToRename.empty())
2366f22ef01cSRoman Divacky continue;
2367f22ef01cSRoman Divacky
23684ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "JT: Renaming non-local uses of: " << I << "\n");
2369f22ef01cSRoman Divacky
2370f22ef01cSRoman Divacky // We found a use of I outside of BB. Rename all uses of I that are outside
2371f22ef01cSRoman Divacky // its block to be uses of the appropriate PHI node etc. See ValuesInBlocks
2372f22ef01cSRoman Divacky // with the two values we know.
2373444ed5c5SDimitry Andric SSAUpdate.Initialize(I.getType(), I.getName());
2374444ed5c5SDimitry Andric SSAUpdate.AddAvailableValue(BB, &I);
2375444ed5c5SDimitry Andric SSAUpdate.AddAvailableValue(PredBB, ValueMapping[&I]);
2376f22ef01cSRoman Divacky
2377f22ef01cSRoman Divacky while (!UsesToRename.empty())
2378f22ef01cSRoman Divacky SSAUpdate.RewriteUse(*UsesToRename.pop_back_val());
23794ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "\n");
2380f22ef01cSRoman Divacky }
2381f22ef01cSRoman Divacky
2382f22ef01cSRoman Divacky // PredBB no longer jumps to BB, remove entries in the PHI node for the edge
2383f22ef01cSRoman Divacky // that we nuked.
23842754fe60SDimitry Andric BB->removePredecessor(PredBB, true);
2385f22ef01cSRoman Divacky
2386f22ef01cSRoman Divacky // Remove the unconditional branch at the end of the PredBB block.
2387f22ef01cSRoman Divacky OldPredBranch->eraseFromParent();
2388*b5893f02SDimitry Andric DTU->applyUpdates(Updates);
2389f22ef01cSRoman Divacky
2390f22ef01cSRoman Divacky ++NumDupes;
2391f22ef01cSRoman Divacky return true;
2392f22ef01cSRoman Divacky }
2393f22ef01cSRoman Divacky
2394*b5893f02SDimitry Andric // Pred is a predecessor of BB with an unconditional branch to BB. SI is
2395*b5893f02SDimitry Andric // a Select instruction in Pred. BB has other predecessors and SI is used in
2396*b5893f02SDimitry Andric // a PHI node in BB. SI has no other use.
2397*b5893f02SDimitry Andric // A new basic block, NewBB, is created and SI is converted to compare and
2398*b5893f02SDimitry Andric // conditional branch. SI is erased from parent.
UnfoldSelectInstr(BasicBlock * Pred,BasicBlock * BB,SelectInst * SI,PHINode * SIUse,unsigned Idx)2399*b5893f02SDimitry Andric void JumpThreadingPass::UnfoldSelectInstr(BasicBlock *Pred, BasicBlock *BB,
2400*b5893f02SDimitry Andric SelectInst *SI, PHINode *SIUse,
2401*b5893f02SDimitry Andric unsigned Idx) {
2402*b5893f02SDimitry Andric // Expand the select.
2403*b5893f02SDimitry Andric //
2404*b5893f02SDimitry Andric // Pred --
2405*b5893f02SDimitry Andric // | v
2406*b5893f02SDimitry Andric // | NewBB
2407*b5893f02SDimitry Andric // | |
2408*b5893f02SDimitry Andric // |-----
2409*b5893f02SDimitry Andric // v
2410*b5893f02SDimitry Andric // BB
2411*b5893f02SDimitry Andric BranchInst *PredTerm = dyn_cast<BranchInst>(Pred->getTerminator());
2412*b5893f02SDimitry Andric BasicBlock *NewBB = BasicBlock::Create(BB->getContext(), "select.unfold",
2413*b5893f02SDimitry Andric BB->getParent(), BB);
2414*b5893f02SDimitry Andric // Move the unconditional branch to NewBB.
2415*b5893f02SDimitry Andric PredTerm->removeFromParent();
2416*b5893f02SDimitry Andric NewBB->getInstList().insert(NewBB->end(), PredTerm);
2417*b5893f02SDimitry Andric // Create a conditional branch and update PHI nodes.
2418*b5893f02SDimitry Andric BranchInst::Create(NewBB, BB, SI->getCondition(), Pred);
2419*b5893f02SDimitry Andric SIUse->setIncomingValue(Idx, SI->getFalseValue());
2420*b5893f02SDimitry Andric SIUse->addIncoming(SI->getTrueValue(), NewBB);
2421*b5893f02SDimitry Andric
2422*b5893f02SDimitry Andric // The select is now dead.
2423*b5893f02SDimitry Andric SI->eraseFromParent();
2424*b5893f02SDimitry Andric DTU->applyUpdates({{DominatorTree::Insert, NewBB, BB},
2425*b5893f02SDimitry Andric {DominatorTree::Insert, Pred, NewBB}});
2426*b5893f02SDimitry Andric
2427*b5893f02SDimitry Andric // Update any other PHI nodes in BB.
2428*b5893f02SDimitry Andric for (BasicBlock::iterator BI = BB->begin();
2429*b5893f02SDimitry Andric PHINode *Phi = dyn_cast<PHINode>(BI); ++BI)
2430*b5893f02SDimitry Andric if (Phi != SIUse)
2431*b5893f02SDimitry Andric Phi->addIncoming(Phi->getIncomingValueForBlock(Pred), NewBB);
2432*b5893f02SDimitry Andric }
2433*b5893f02SDimitry Andric
TryToUnfoldSelect(SwitchInst * SI,BasicBlock * BB)2434*b5893f02SDimitry Andric bool JumpThreadingPass::TryToUnfoldSelect(SwitchInst *SI, BasicBlock *BB) {
2435*b5893f02SDimitry Andric PHINode *CondPHI = dyn_cast<PHINode>(SI->getCondition());
2436*b5893f02SDimitry Andric
2437*b5893f02SDimitry Andric if (!CondPHI || CondPHI->getParent() != BB)
2438*b5893f02SDimitry Andric return false;
2439*b5893f02SDimitry Andric
2440*b5893f02SDimitry Andric for (unsigned I = 0, E = CondPHI->getNumIncomingValues(); I != E; ++I) {
2441*b5893f02SDimitry Andric BasicBlock *Pred = CondPHI->getIncomingBlock(I);
2442*b5893f02SDimitry Andric SelectInst *PredSI = dyn_cast<SelectInst>(CondPHI->getIncomingValue(I));
2443*b5893f02SDimitry Andric
2444*b5893f02SDimitry Andric // The second and third condition can be potentially relaxed. Currently
2445*b5893f02SDimitry Andric // the conditions help to simplify the code and allow us to reuse existing
2446*b5893f02SDimitry Andric // code, developed for TryToUnfoldSelect(CmpInst *, BasicBlock *)
2447*b5893f02SDimitry Andric if (!PredSI || PredSI->getParent() != Pred || !PredSI->hasOneUse())
2448*b5893f02SDimitry Andric continue;
2449*b5893f02SDimitry Andric
2450*b5893f02SDimitry Andric BranchInst *PredTerm = dyn_cast<BranchInst>(Pred->getTerminator());
2451*b5893f02SDimitry Andric if (!PredTerm || !PredTerm->isUnconditional())
2452*b5893f02SDimitry Andric continue;
2453*b5893f02SDimitry Andric
2454*b5893f02SDimitry Andric UnfoldSelectInstr(Pred, BB, PredSI, CondPHI, I);
2455*b5893f02SDimitry Andric return true;
2456*b5893f02SDimitry Andric }
2457*b5893f02SDimitry Andric return false;
2458*b5893f02SDimitry Andric }
2459*b5893f02SDimitry Andric
2460f785676fSDimitry Andric /// TryToUnfoldSelect - Look for blocks of the form
2461f785676fSDimitry Andric /// bb1:
2462f785676fSDimitry Andric /// %a = select
24637a7e6055SDimitry Andric /// br bb2
2464f785676fSDimitry Andric ///
2465f785676fSDimitry Andric /// bb2:
24667a7e6055SDimitry Andric /// %p = phi [%a, %bb1] ...
2467f785676fSDimitry Andric /// %c = icmp %p
2468f785676fSDimitry Andric /// br i1 %c
2469f785676fSDimitry Andric ///
2470f785676fSDimitry Andric /// And expand the select into a branch structure if one of its arms allows %c
2471f785676fSDimitry Andric /// to be folded. This later enables threading from bb1 over bb2.
TryToUnfoldSelect(CmpInst * CondCmp,BasicBlock * BB)24723ca95b02SDimitry Andric bool JumpThreadingPass::TryToUnfoldSelect(CmpInst *CondCmp, BasicBlock *BB) {
2473f785676fSDimitry Andric BranchInst *CondBr = dyn_cast<BranchInst>(BB->getTerminator());
2474f785676fSDimitry Andric PHINode *CondLHS = dyn_cast<PHINode>(CondCmp->getOperand(0));
2475f785676fSDimitry Andric Constant *CondRHS = cast<Constant>(CondCmp->getOperand(1));
2476f22ef01cSRoman Divacky
2477f785676fSDimitry Andric if (!CondBr || !CondBr->isConditional() || !CondLHS ||
2478f785676fSDimitry Andric CondLHS->getParent() != BB)
2479f785676fSDimitry Andric return false;
2480f785676fSDimitry Andric
2481f785676fSDimitry Andric for (unsigned I = 0, E = CondLHS->getNumIncomingValues(); I != E; ++I) {
2482f785676fSDimitry Andric BasicBlock *Pred = CondLHS->getIncomingBlock(I);
2483f785676fSDimitry Andric SelectInst *SI = dyn_cast<SelectInst>(CondLHS->getIncomingValue(I));
2484f785676fSDimitry Andric
2485f785676fSDimitry Andric // Look if one of the incoming values is a select in the corresponding
2486f785676fSDimitry Andric // predecessor.
2487f785676fSDimitry Andric if (!SI || SI->getParent() != Pred || !SI->hasOneUse())
2488f785676fSDimitry Andric continue;
2489f785676fSDimitry Andric
2490f785676fSDimitry Andric BranchInst *PredTerm = dyn_cast<BranchInst>(Pred->getTerminator());
2491f785676fSDimitry Andric if (!PredTerm || !PredTerm->isUnconditional())
2492f785676fSDimitry Andric continue;
2493f785676fSDimitry Andric
2494f785676fSDimitry Andric // Now check if one of the select values would allow us to constant fold the
2495f785676fSDimitry Andric // terminator in BB. We don't do the transform if both sides fold, those
2496f785676fSDimitry Andric // cases will be threaded in any case.
2497*b5893f02SDimitry Andric if (DTU->hasPendingDomTreeUpdates())
24984ba319b5SDimitry Andric LVI->disableDT();
24994ba319b5SDimitry Andric else
25004ba319b5SDimitry Andric LVI->enableDT();
2501f785676fSDimitry Andric LazyValueInfo::Tristate LHSFolds =
2502f785676fSDimitry Andric LVI->getPredicateOnEdge(CondCmp->getPredicate(), SI->getOperand(1),
250339d628a0SDimitry Andric CondRHS, Pred, BB, CondCmp);
2504f785676fSDimitry Andric LazyValueInfo::Tristate RHSFolds =
2505f785676fSDimitry Andric LVI->getPredicateOnEdge(CondCmp->getPredicate(), SI->getOperand(2),
250639d628a0SDimitry Andric CondRHS, Pred, BB, CondCmp);
2507f785676fSDimitry Andric if ((LHSFolds != LazyValueInfo::Unknown ||
2508f785676fSDimitry Andric RHSFolds != LazyValueInfo::Unknown) &&
2509f785676fSDimitry Andric LHSFolds != RHSFolds) {
2510*b5893f02SDimitry Andric UnfoldSelectInstr(Pred, BB, SI, CondLHS, I);
2511f785676fSDimitry Andric return true;
2512f785676fSDimitry Andric }
2513f785676fSDimitry Andric }
2514f785676fSDimitry Andric return false;
2515f785676fSDimitry Andric }
2516444ed5c5SDimitry Andric
2517b40b48b8SDimitry Andric /// TryToUnfoldSelectInCurrBB - Look for PHI/Select or PHI/CMP/Select in the
2518b40b48b8SDimitry Andric /// same BB in the form
2519444ed5c5SDimitry Andric /// bb:
2520444ed5c5SDimitry Andric /// %p = phi [false, %bb1], [true, %bb2], [false, %bb3], [true, %bb4], ...
2521b40b48b8SDimitry Andric /// %s = select %p, trueval, falseval
2522444ed5c5SDimitry Andric ///
2523b40b48b8SDimitry Andric /// or
2524b40b48b8SDimitry Andric ///
2525b40b48b8SDimitry Andric /// bb:
2526b40b48b8SDimitry Andric /// %p = phi [0, %bb1], [1, %bb2], [0, %bb3], [1, %bb4], ...
2527b40b48b8SDimitry Andric /// %c = cmp %p, 0
2528b40b48b8SDimitry Andric /// %s = select %c, trueval, falseval
25292cab237bSDimitry Andric ///
2530444ed5c5SDimitry Andric /// And expand the select into a branch structure. This later enables
2531444ed5c5SDimitry Andric /// jump-threading over bb in this pass.
2532444ed5c5SDimitry Andric ///
2533444ed5c5SDimitry Andric /// Using the similar approach of SimplifyCFG::FoldCondBranchOnPHI(), unfold
2534444ed5c5SDimitry Andric /// select if the associated PHI has at least one constant. If the unfolded
2535444ed5c5SDimitry Andric /// select is not jump-threaded, it will be folded again in the later
2536444ed5c5SDimitry Andric /// optimizations.
TryToUnfoldSelectInCurrBB(BasicBlock * BB)25373ca95b02SDimitry Andric bool JumpThreadingPass::TryToUnfoldSelectInCurrBB(BasicBlock *BB) {
2538444ed5c5SDimitry Andric // If threading this would thread across a loop header, don't thread the edge.
2539444ed5c5SDimitry Andric // See the comments above FindLoopHeaders for justifications and caveats.
2540444ed5c5SDimitry Andric if (LoopHeaders.count(BB))
2541444ed5c5SDimitry Andric return false;
2542444ed5c5SDimitry Andric
2543444ed5c5SDimitry Andric for (BasicBlock::iterator BI = BB->begin();
2544444ed5c5SDimitry Andric PHINode *PN = dyn_cast<PHINode>(BI); ++BI) {
2545b40b48b8SDimitry Andric // Look for a Phi having at least one constant incoming value.
2546b40b48b8SDimitry Andric if (llvm::all_of(PN->incoming_values(),
2547b40b48b8SDimitry Andric [](Value *V) { return !isa<ConstantInt>(V); }))
2548444ed5c5SDimitry Andric continue;
2549444ed5c5SDimitry Andric
2550b40b48b8SDimitry Andric auto isUnfoldCandidate = [BB](SelectInst *SI, Value *V) {
2551b40b48b8SDimitry Andric // Check if SI is in BB and use V as condition.
2552b40b48b8SDimitry Andric if (SI->getParent() != BB)
2553444ed5c5SDimitry Andric return false;
2554b40b48b8SDimitry Andric Value *Cond = SI->getCondition();
2555b40b48b8SDimitry Andric return (Cond && Cond == V && Cond->getType()->isIntegerTy(1));
2556b40b48b8SDimitry Andric };
2557b40b48b8SDimitry Andric
2558b40b48b8SDimitry Andric SelectInst *SI = nullptr;
2559b40b48b8SDimitry Andric for (Use &U : PN->uses()) {
2560b40b48b8SDimitry Andric if (ICmpInst *Cmp = dyn_cast<ICmpInst>(U.getUser())) {
2561b40b48b8SDimitry Andric // Look for a ICmp in BB that compares PN with a constant and is the
2562b40b48b8SDimitry Andric // condition of a Select.
2563b40b48b8SDimitry Andric if (Cmp->getParent() == BB && Cmp->hasOneUse() &&
2564b40b48b8SDimitry Andric isa<ConstantInt>(Cmp->getOperand(1 - U.getOperandNo())))
2565b40b48b8SDimitry Andric if (SelectInst *SelectI = dyn_cast<SelectInst>(Cmp->user_back()))
2566b40b48b8SDimitry Andric if (isUnfoldCandidate(SelectI, Cmp->use_begin()->get())) {
2567b40b48b8SDimitry Andric SI = SelectI;
2568b40b48b8SDimitry Andric break;
2569b40b48b8SDimitry Andric }
2570b40b48b8SDimitry Andric } else if (SelectInst *SelectI = dyn_cast<SelectInst>(U.getUser())) {
25714ba319b5SDimitry Andric // Look for a Select in BB that uses PN as condition.
2572b40b48b8SDimitry Andric if (isUnfoldCandidate(SelectI, U.get())) {
2573b40b48b8SDimitry Andric SI = SelectI;
2574b40b48b8SDimitry Andric break;
2575b40b48b8SDimitry Andric }
2576b40b48b8SDimitry Andric }
2577444ed5c5SDimitry Andric }
2578444ed5c5SDimitry Andric
2579b40b48b8SDimitry Andric if (!SI)
2580b40b48b8SDimitry Andric continue;
2581444ed5c5SDimitry Andric // Expand the select.
2582*b5893f02SDimitry Andric Instruction *Term =
2583444ed5c5SDimitry Andric SplitBlockAndInsertIfThen(SI->getCondition(), SI, false);
25844ba319b5SDimitry Andric BasicBlock *SplitBB = SI->getParent();
25854ba319b5SDimitry Andric BasicBlock *NewBB = Term->getParent();
2586444ed5c5SDimitry Andric PHINode *NewPN = PHINode::Create(SI->getType(), 2, "", SI);
2587444ed5c5SDimitry Andric NewPN->addIncoming(SI->getTrueValue(), Term->getParent());
2588444ed5c5SDimitry Andric NewPN->addIncoming(SI->getFalseValue(), BB);
2589444ed5c5SDimitry Andric SI->replaceAllUsesWith(NewPN);
2590444ed5c5SDimitry Andric SI->eraseFromParent();
25914ba319b5SDimitry Andric // NewBB and SplitBB are newly created blocks which require insertion.
25924ba319b5SDimitry Andric std::vector<DominatorTree::UpdateType> Updates;
25934ba319b5SDimitry Andric Updates.reserve((2 * SplitBB->getTerminator()->getNumSuccessors()) + 3);
25944ba319b5SDimitry Andric Updates.push_back({DominatorTree::Insert, BB, SplitBB});
25954ba319b5SDimitry Andric Updates.push_back({DominatorTree::Insert, BB, NewBB});
25964ba319b5SDimitry Andric Updates.push_back({DominatorTree::Insert, NewBB, SplitBB});
2597*b5893f02SDimitry Andric // BB's successors were moved to SplitBB, update DTU accordingly.
25984ba319b5SDimitry Andric for (auto *Succ : successors(SplitBB)) {
25994ba319b5SDimitry Andric Updates.push_back({DominatorTree::Delete, BB, Succ});
26004ba319b5SDimitry Andric Updates.push_back({DominatorTree::Insert, SplitBB, Succ});
26014ba319b5SDimitry Andric }
2602*b5893f02SDimitry Andric DTU->applyUpdates(Updates);
2603444ed5c5SDimitry Andric return true;
2604444ed5c5SDimitry Andric }
2605444ed5c5SDimitry Andric return false;
2606444ed5c5SDimitry Andric }
26077a7e6055SDimitry Andric
26087a7e6055SDimitry Andric /// Try to propagate a guard from the current BB into one of its predecessors
26097a7e6055SDimitry Andric /// in case if another branch of execution implies that the condition of this
26107a7e6055SDimitry Andric /// guard is always true. Currently we only process the simplest case that
26117a7e6055SDimitry Andric /// looks like:
26127a7e6055SDimitry Andric ///
26137a7e6055SDimitry Andric /// Start:
26147a7e6055SDimitry Andric /// %cond = ...
26157a7e6055SDimitry Andric /// br i1 %cond, label %T1, label %F1
26167a7e6055SDimitry Andric /// T1:
26177a7e6055SDimitry Andric /// br label %Merge
26187a7e6055SDimitry Andric /// F1:
26197a7e6055SDimitry Andric /// br label %Merge
26207a7e6055SDimitry Andric /// Merge:
26217a7e6055SDimitry Andric /// %condGuard = ...
26227a7e6055SDimitry Andric /// call void(i1, ...) @llvm.experimental.guard( i1 %condGuard )[ "deopt"() ]
26237a7e6055SDimitry Andric ///
26247a7e6055SDimitry Andric /// And cond either implies condGuard or !condGuard. In this case all the
26257a7e6055SDimitry Andric /// instructions before the guard can be duplicated in both branches, and the
26267a7e6055SDimitry Andric /// guard is then threaded to one of them.
ProcessGuards(BasicBlock * BB)26277a7e6055SDimitry Andric bool JumpThreadingPass::ProcessGuards(BasicBlock *BB) {
26287a7e6055SDimitry Andric using namespace PatternMatch;
26292cab237bSDimitry Andric
26307a7e6055SDimitry Andric // We only want to deal with two predecessors.
26317a7e6055SDimitry Andric BasicBlock *Pred1, *Pred2;
26327a7e6055SDimitry Andric auto PI = pred_begin(BB), PE = pred_end(BB);
26337a7e6055SDimitry Andric if (PI == PE)
26347a7e6055SDimitry Andric return false;
26357a7e6055SDimitry Andric Pred1 = *PI++;
26367a7e6055SDimitry Andric if (PI == PE)
26377a7e6055SDimitry Andric return false;
26387a7e6055SDimitry Andric Pred2 = *PI++;
26397a7e6055SDimitry Andric if (PI != PE)
26407a7e6055SDimitry Andric return false;
26417a7e6055SDimitry Andric if (Pred1 == Pred2)
26427a7e6055SDimitry Andric return false;
26437a7e6055SDimitry Andric
26447a7e6055SDimitry Andric // Try to thread one of the guards of the block.
26457a7e6055SDimitry Andric // TODO: Look up deeper than to immediate predecessor?
26467a7e6055SDimitry Andric auto *Parent = Pred1->getSinglePredecessor();
26477a7e6055SDimitry Andric if (!Parent || Parent != Pred2->getSinglePredecessor())
26487a7e6055SDimitry Andric return false;
26497a7e6055SDimitry Andric
26507a7e6055SDimitry Andric if (auto *BI = dyn_cast<BranchInst>(Parent->getTerminator()))
26517a7e6055SDimitry Andric for (auto &I : *BB)
2652*b5893f02SDimitry Andric if (isGuard(&I) && ThreadGuard(BB, cast<IntrinsicInst>(&I), BI))
26537a7e6055SDimitry Andric return true;
26547a7e6055SDimitry Andric
26557a7e6055SDimitry Andric return false;
26567a7e6055SDimitry Andric }
26577a7e6055SDimitry Andric
26587a7e6055SDimitry Andric /// Try to propagate the guard from BB which is the lower block of a diamond
26597a7e6055SDimitry Andric /// to one of its branches, in case if diamond's condition implies guard's
26607a7e6055SDimitry Andric /// condition.
ThreadGuard(BasicBlock * BB,IntrinsicInst * Guard,BranchInst * BI)26617a7e6055SDimitry Andric bool JumpThreadingPass::ThreadGuard(BasicBlock *BB, IntrinsicInst *Guard,
26627a7e6055SDimitry Andric BranchInst *BI) {
26637a7e6055SDimitry Andric assert(BI->getNumSuccessors() == 2 && "Wrong number of successors?");
26647a7e6055SDimitry Andric assert(BI->isConditional() && "Unconditional branch has 2 successors?");
26657a7e6055SDimitry Andric Value *GuardCond = Guard->getArgOperand(0);
26667a7e6055SDimitry Andric Value *BranchCond = BI->getCondition();
26677a7e6055SDimitry Andric BasicBlock *TrueDest = BI->getSuccessor(0);
26687a7e6055SDimitry Andric BasicBlock *FalseDest = BI->getSuccessor(1);
26697a7e6055SDimitry Andric
26707a7e6055SDimitry Andric auto &DL = BB->getModule()->getDataLayout();
26717a7e6055SDimitry Andric bool TrueDestIsSafe = false;
26727a7e6055SDimitry Andric bool FalseDestIsSafe = false;
26737a7e6055SDimitry Andric
26747a7e6055SDimitry Andric // True dest is safe if BranchCond => GuardCond.
26757a7e6055SDimitry Andric auto Impl = isImpliedCondition(BranchCond, GuardCond, DL);
26767a7e6055SDimitry Andric if (Impl && *Impl)
26777a7e6055SDimitry Andric TrueDestIsSafe = true;
26787a7e6055SDimitry Andric else {
26797a7e6055SDimitry Andric // False dest is safe if !BranchCond => GuardCond.
26802cab237bSDimitry Andric Impl = isImpliedCondition(BranchCond, GuardCond, DL, /* LHSIsTrue */ false);
26817a7e6055SDimitry Andric if (Impl && *Impl)
26827a7e6055SDimitry Andric FalseDestIsSafe = true;
26837a7e6055SDimitry Andric }
26847a7e6055SDimitry Andric
26857a7e6055SDimitry Andric if (!TrueDestIsSafe && !FalseDestIsSafe)
26867a7e6055SDimitry Andric return false;
26877a7e6055SDimitry Andric
26884ba319b5SDimitry Andric BasicBlock *PredUnguardedBlock = TrueDestIsSafe ? TrueDest : FalseDest;
26894ba319b5SDimitry Andric BasicBlock *PredGuardedBlock = FalseDestIsSafe ? TrueDest : FalseDest;
26907a7e6055SDimitry Andric
26917a7e6055SDimitry Andric ValueToValueMapTy UnguardedMapping, GuardedMapping;
26927a7e6055SDimitry Andric Instruction *AfterGuard = Guard->getNextNode();
26937a7e6055SDimitry Andric unsigned Cost = getJumpThreadDuplicationCost(BB, AfterGuard, BBDupThreshold);
26947a7e6055SDimitry Andric if (Cost > BBDupThreshold)
26957a7e6055SDimitry Andric return false;
26967a7e6055SDimitry Andric // Duplicate all instructions before the guard and the guard itself to the
26977a7e6055SDimitry Andric // branch where implication is not proved.
26984ba319b5SDimitry Andric BasicBlock *GuardedBlock = DuplicateInstructionsInSplitBetween(
2699*b5893f02SDimitry Andric BB, PredGuardedBlock, AfterGuard, GuardedMapping, *DTU);
27007a7e6055SDimitry Andric assert(GuardedBlock && "Could not create the guarded block?");
27017a7e6055SDimitry Andric // Duplicate all instructions before the guard in the unguarded branch.
27027a7e6055SDimitry Andric // Since we have successfully duplicated the guarded block and this block
27037a7e6055SDimitry Andric // has fewer instructions, we expect it to succeed.
27044ba319b5SDimitry Andric BasicBlock *UnguardedBlock = DuplicateInstructionsInSplitBetween(
2705*b5893f02SDimitry Andric BB, PredUnguardedBlock, Guard, UnguardedMapping, *DTU);
27067a7e6055SDimitry Andric assert(UnguardedBlock && "Could not create the unguarded block?");
27074ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "Moved guard " << *Guard << " to block "
27087a7e6055SDimitry Andric << GuardedBlock->getName() << "\n");
27097a7e6055SDimitry Andric // Some instructions before the guard may still have uses. For them, we need
27107a7e6055SDimitry Andric // to create Phi nodes merging their copies in both guarded and unguarded
27117a7e6055SDimitry Andric // branches. Those instructions that have no uses can be just removed.
27127a7e6055SDimitry Andric SmallVector<Instruction *, 4> ToRemove;
27137a7e6055SDimitry Andric for (auto BI = BB->begin(); &*BI != AfterGuard; ++BI)
27147a7e6055SDimitry Andric if (!isa<PHINode>(&*BI))
27157a7e6055SDimitry Andric ToRemove.push_back(&*BI);
27167a7e6055SDimitry Andric
27177a7e6055SDimitry Andric Instruction *InsertionPoint = &*BB->getFirstInsertionPt();
27187a7e6055SDimitry Andric assert(InsertionPoint && "Empty block?");
27197a7e6055SDimitry Andric // Substitute with Phis & remove.
27207a7e6055SDimitry Andric for (auto *Inst : reverse(ToRemove)) {
27217a7e6055SDimitry Andric if (!Inst->use_empty()) {
27227a7e6055SDimitry Andric PHINode *NewPN = PHINode::Create(Inst->getType(), 2);
27237a7e6055SDimitry Andric NewPN->addIncoming(UnguardedMapping[Inst], UnguardedBlock);
27247a7e6055SDimitry Andric NewPN->addIncoming(GuardedMapping[Inst], GuardedBlock);
27257a7e6055SDimitry Andric NewPN->insertBefore(InsertionPoint);
27267a7e6055SDimitry Andric Inst->replaceAllUsesWith(NewPN);
27277a7e6055SDimitry Andric }
27287a7e6055SDimitry Andric Inst->eraseFromParent();
27297a7e6055SDimitry Andric }
27307a7e6055SDimitry Andric return true;
27317a7e6055SDimitry Andric }
2732