[llvm] f7c1982 - Revert "[JT][CT] Preserve exisiting BPI/BFI during JumpThreading"

Evgeniy Brevnov via llvm-commits llvm-commits at lists.llvm.org
Fri Jan 27 00:36:08 PST 2023


Author: Evgeniy Brevnov
Date: 2023-01-27T15:35:32+07:00
New Revision: f7c1982309082387c2f2d4587cbbc5618a9662f1

URL: https://github.com/llvm/llvm-project/commit/f7c1982309082387c2f2d4587cbbc5618a9662f1
DIFF: https://github.com/llvm/llvm-project/commit/f7c1982309082387c2f2d4587cbbc5618a9662f1.diff

LOG: Revert "[JT][CT] Preserve exisiting BPI/BFI during JumpThreading"

This reverts commit 26e7cb24cb5dfa560683064d37f560558f00aa67.

Added: 
    

Modified: 
    llvm/include/llvm/Transforms/Scalar/JumpThreading.h
    llvm/lib/Transforms/Scalar/JumpThreading.cpp
    llvm/test/Transforms/JumpThreading/select.ll
    llvm/test/Transforms/JumpThreading/thread-prob-2.ll
    llvm/test/Transforms/JumpThreading/thread-prob-3.ll
    llvm/test/Transforms/JumpThreading/thread-prob-4.ll
    llvm/test/Transforms/JumpThreading/thread-prob-5.ll
    llvm/test/Transforms/JumpThreading/thread-prob-6.ll
    llvm/test/Transforms/JumpThreading/threading_prof1.ll
    llvm/test/Transforms/JumpThreading/threading_prof2.ll
    llvm/test/Transforms/JumpThreading/threading_prof3.ll

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/Transforms/Scalar/JumpThreading.h b/llvm/include/llvm/Transforms/Scalar/JumpThreading.h
index bb0c015808034..09d08bf423a6a 100644
--- a/llvm/include/llvm/Transforms/Scalar/JumpThreading.h
+++ b/llvm/include/llvm/Transforms/Scalar/JumpThreading.h
@@ -20,9 +20,7 @@
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/Analysis/BlockFrequencyInfo.h"
 #include "llvm/Analysis/BranchProbabilityInfo.h"
-#include "llvm/Analysis/DomTreeUpdater.h"
 #include "llvm/IR/ValueHandle.h"
-#include <optional>
 #include <utility>
 
 namespace llvm {
@@ -33,6 +31,7 @@ class BinaryOperator;
 class BranchInst;
 class CmpInst;
 class Constant;
+class DomTreeUpdater;
 class Function;
 class Instruction;
 class IntrinsicInst;
@@ -76,18 +75,15 @@ enum ConstantPreference { WantInteger, WantBlockAddress };
 /// In this case, the unconditional branch at the end of the first if can be
 /// revectored to the false side of the second if.
 class JumpThreadingPass : public PassInfoMixin<JumpThreadingPass> {
-  Function *F = nullptr;
-  FunctionAnalysisManager *FAM = nullptr;
-  TargetLibraryInfo *TLI = nullptr;
-  TargetTransformInfo *TTI = nullptr;
-  LazyValueInfo *LVI = nullptr;
-  AAResults *AA = nullptr;
-  std::unique_ptr<DomTreeUpdater> DTU;
-  std::optional<BlockFrequencyInfo *> BFI = std::nullopt;
-  std::optional<BranchProbabilityInfo *> BPI = std::nullopt;
-  bool ChangedSinceLastAnalysisUpdate = false;
+  TargetLibraryInfo *TLI;
+  TargetTransformInfo *TTI;
+  LazyValueInfo *LVI;
+  AAResults *AA;
+  DomTreeUpdater *DTU;
+  std::unique_ptr<BlockFrequencyInfo> BFI;
+  std::unique_ptr<BranchProbabilityInfo> BPI;
+  bool HasProfileData = false;
   bool HasGuards = false;
-  bool HasProfile = false;
 #ifndef LLVM_ENABLE_ABI_BREAKING_CHECKS
   SmallPtrSet<const BasicBlock *, 16> LoopHeaders;
 #else
@@ -101,16 +97,18 @@ class JumpThreadingPass : public PassInfoMixin<JumpThreadingPass> {
   JumpThreadingPass(int T = -1);
 
   // Glue for old PM.
-  bool runImpl(Function &F, FunctionAnalysisManager *FAM,
-               TargetLibraryInfo *TLI, TargetTransformInfo *TTI,
-               LazyValueInfo *LVI, AAResults *AA,
-               std::unique_ptr<DomTreeUpdater> DTU,
-               std::optional<BlockFrequencyInfo *> BFI,
-               std::optional<BranchProbabilityInfo *> BPI);
+  bool runImpl(Function &F, TargetLibraryInfo *TLI, TargetTransformInfo *TTI,
+               LazyValueInfo *LVI, AAResults *AA, DomTreeUpdater *DTU,
+               bool HasProfileData, std::unique_ptr<BlockFrequencyInfo> BFI,
+               std::unique_ptr<BranchProbabilityInfo> BPI);
 
   PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
 
-  DomTreeUpdater *getDomTreeUpdater() const { return DTU.get(); }
+  void releaseMemory() {
+    BFI.reset();
+    BPI.reset();
+  }
+
   void findLoopHeaders(Function &F);
   bool processBlock(BasicBlock *BB);
   bool maybeMergeBasicBlockIntoOnlyPred(BasicBlock *BB);
@@ -173,35 +171,6 @@ class JumpThreadingPass : public PassInfoMixin<JumpThreadingPass> {
                                     BasicBlock *NewBB, BasicBlock *SuccBB);
   /// Check if the block has profile metadata for its outgoing edges.
   bool doesBlockHaveProfileData(BasicBlock *BB);
-
-  /// Returns analysis preserved by the pass.
-  PreservedAnalyses getPreservedAnalysis() const;
-
-  /// Helper function to run "external" analysis in the middle of JumpThreading.
-  /// It takes care of updating/invalidating other existing analysis
-  /// before/after  running the "external" one.
-  template <typename AnalysisT>
-  typename AnalysisT::Result *runExternalAnalysis();
-
-  /// Returns an existing instance of BPI if any, otherwise nullptr. By
-  /// "existing" we mean either cached result provided by FunctionAnalysisManger
-  /// or created by preceding call to 'getOrCreateBPI'.
-  BranchProbabilityInfo *getBPI();
-
-  /// Returns an existing instance of BFI if any, otherwise nullptr. By
-  /// "existing" we mean either cached result provided by FunctionAnalysisManger
-  /// or created by preceding call to 'getOrCreateBFI'.
-  BlockFrequencyInfo *getBFI();
-
-  /// Returns an existing instance of BPI if any, otherwise:
-  ///   if 'HasProfile' is true creates new instance through
-  ///   FunctionAnalysisManager, otherwise nullptr.
-  BranchProbabilityInfo *getOrCreateBPI(bool Force = false);
-
-  /// Returns an existing instance of BFI if any, otherwise:
-  ///   if 'HasProfile' is true creates new instance through
-  ///   FunctionAnalysisManager, otherwise nullptr.
-  BlockFrequencyInfo *getOrCreateBFI(bool Force = false);
 };
 
 } // end namespace llvm

diff  --git a/llvm/lib/Transforms/Scalar/JumpThreading.cpp b/llvm/lib/Transforms/Scalar/JumpThreading.cpp
index 0490b7d925abc..f41eaed2e3e7e 100644
--- a/llvm/lib/Transforms/Scalar/JumpThreading.cpp
+++ b/llvm/lib/Transforms/Scalar/JumpThreading.cpp
@@ -23,6 +23,7 @@
 #include "llvm/Analysis/BranchProbabilityInfo.h"
 #include "llvm/Analysis/CFG.h"
 #include "llvm/Analysis/ConstantFolding.h"
+#include "llvm/Analysis/DomTreeUpdater.h"
 #include "llvm/Analysis/GlobalsModRef.h"
 #include "llvm/Analysis/GuardUtils.h"
 #include "llvm/Analysis/InstructionSimplify.h"
@@ -30,7 +31,6 @@
 #include "llvm/Analysis/Loads.h"
 #include "llvm/Analysis/LoopInfo.h"
 #include "llvm/Analysis/MemoryLocation.h"
-#include "llvm/Analysis/PostDominators.h"
 #include "llvm/Analysis/TargetLibraryInfo.h"
 #include "llvm/Analysis/TargetTransformInfo.h"
 #include "llvm/Analysis/ValueTracking.h"
@@ -133,10 +133,12 @@ namespace {
   /// In this case, the unconditional branch at the end of the first if can be
   /// revectored to the false side of the second if.
   class JumpThreading : public FunctionPass {
+    JumpThreadingPass Impl;
+
   public:
     static char ID; // Pass identification
 
-    JumpThreading(int T = -1) : FunctionPass(ID) {
+    JumpThreading(int T = -1) : FunctionPass(ID), Impl(T) {
       initializeJumpThreadingPass(*PassRegistry::getPassRegistry());
     }
 
@@ -152,6 +154,8 @@ namespace {
       AU.addRequired<TargetLibraryInfoWrapperPass>();
       AU.addRequired<TargetTransformInfoWrapperPass>();
     }
+
+    void releaseMemory() override { Impl.releaseMemory(); }
   };
 
 } // end anonymous namespace
@@ -314,6 +318,7 @@ bool JumpThreading::runOnFunction(Function &F) {
   auto DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
   auto LVI = &getAnalysis<LazyValueInfoWrapperPass>().getLVI();
   auto AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
+  DomTreeUpdater DTU(*DT, DomTreeUpdater::UpdateStrategy::Lazy);
   std::unique_ptr<BlockFrequencyInfo> BFI;
   std::unique_ptr<BranchProbabilityInfo> BPI;
   if (F.hasProfileData()) {
@@ -322,14 +327,11 @@ bool JumpThreading::runOnFunction(Function &F) {
     BFI.reset(new BlockFrequencyInfo(F, *BPI, LI));
   }
 
-  JumpThreadingPass Impl;
-  bool Changed = Impl.runImpl(F, nullptr, TLI, TTI, LVI, AA,
-                              std::make_unique<DomTreeUpdater>(
-                                  DT, DomTreeUpdater::UpdateStrategy::Lazy),
-                              BFI.get(), BPI.get());
+  bool Changed = Impl.runImpl(F, TLI, TTI, LVI, AA, &DTU, F.hasProfileData(),
+                              std::move(BFI), std::move(BPI));
   if (PrintLVIAfterJumpThreading) {
     dbgs() << "LVI for function '" << F.getName() << "':\n";
-    LVI->printLVI(F, Impl.getDomTreeUpdater()->getDomTree(), dbgs());
+    LVI->printLVI(F, DTU.getDomTree(), dbgs());
   }
   return Changed;
 }
@@ -341,74 +343,65 @@ PreservedAnalyses JumpThreadingPass::run(Function &F,
   if (TTI.hasBranchDivergence())
     return PreservedAnalyses::all();
   auto &TLI = AM.getResult<TargetLibraryAnalysis>(F);
+  auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
   auto &LVI = AM.getResult<LazyValueAnalysis>(F);
   auto &AA = AM.getResult<AAManager>(F);
-  auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
+  DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Lazy);
+
+  std::unique_ptr<BlockFrequencyInfo> BFI;
+  std::unique_ptr<BranchProbabilityInfo> BPI;
+  if (F.hasProfileData()) {
+    LoopInfo LI{DT};
+    BPI.reset(new BranchProbabilityInfo(F, LI, &TLI));
+    BFI.reset(new BlockFrequencyInfo(F, *BPI, LI));
+  }
 
-  bool Changed =
-      runImpl(F, &AM, &TLI, &TTI, &LVI, &AA,
-              std::make_unique<DomTreeUpdater>(
-                  &DT, nullptr, DomTreeUpdater::UpdateStrategy::Lazy),
-              std::nullopt, std::nullopt);
+  bool Changed = runImpl(F, &TLI, &TTI, &LVI, &AA, &DTU, F.hasProfileData(),
+                         std::move(BFI), std::move(BPI));
 
   if (PrintLVIAfterJumpThreading) {
     dbgs() << "LVI for function '" << F.getName() << "':\n";
-    LVI.printLVI(F, getDomTreeUpdater()->getDomTree(), dbgs());
+    LVI.printLVI(F, DTU.getDomTree(), dbgs());
   }
 
   if (!Changed)
     return PreservedAnalyses::all();
-
-
-  getDomTreeUpdater()->flush();
-
-#if defined(EXPENSIVE_CHECKS)
-  assert(getDomTreeUpdater()->getDomTree().verify(
-             DominatorTree::VerificationLevel::Full) &&
-         "DT broken after JumpThreading");
-  assert((!getDomTreeUpdater()->hasPostDomTree() ||
-          getDomTreeUpdater()->getPostDomTree().verify(
-              PostDominatorTree::VerificationLevel::Full)) &&
-         "PDT broken after JumpThreading");
-#else
-  assert(getDomTreeUpdater()->getDomTree().verify(
-             DominatorTree::VerificationLevel::Fast) &&
-         "DT broken after JumpThreading");
-  assert((!getDomTreeUpdater()->hasPostDomTree() ||
-          getDomTreeUpdater()->getPostDomTree().verify(
-              PostDominatorTree::VerificationLevel::Fast)) &&
-         "PDT broken after JumpThreading");
-#endif
-
-  return getPreservedAnalysis();
+  PreservedAnalyses PA;
+  PA.preserve<DominatorTreeAnalysis>();
+  PA.preserve<LazyValueAnalysis>();
+  return PA;
 }
 
-bool JumpThreadingPass::runImpl(Function &F_, FunctionAnalysisManager *FAM_,
-                                TargetLibraryInfo *TLI_,
+bool JumpThreadingPass::runImpl(Function &F, TargetLibraryInfo *TLI_,
                                 TargetTransformInfo *TTI_, LazyValueInfo *LVI_,
-                                AliasAnalysis *AA_,
-                                std::unique_ptr<DomTreeUpdater> DTU_,
-                                std::optional<BlockFrequencyInfo *> BFI_,
-                                std::optional<BranchProbabilityInfo *> BPI_) {
-  LLVM_DEBUG(dbgs() << "Jump threading on function '" << F_.getName() << "'\n");
-  F = &F_;
-  FAM = FAM_;
+                                AliasAnalysis *AA_, DomTreeUpdater *DTU_,
+                                bool HasProfileData_,
+                                std::unique_ptr<BlockFrequencyInfo> BFI_,
+                                std::unique_ptr<BranchProbabilityInfo> BPI_) {
+  LLVM_DEBUG(dbgs() << "Jump threading on function '" << F.getName() << "'\n");
   TLI = TLI_;
   TTI = TTI_;
   LVI = LVI_;
   AA = AA_;
-  DTU = std::move(DTU_);
-  BFI = BFI_;
-  BPI = BPI_;
-  auto *GuardDecl = F->getParent()->getFunction(
+  DTU = DTU_;
+  BFI.reset();
+  BPI.reset();
+  // When profile data is available, we need to update edge weights after
+  // successful jump threading, which requires both BPI and BFI being available.
+  HasProfileData = HasProfileData_;
+  auto *GuardDecl = F.getParent()->getFunction(
       Intrinsic::getName(Intrinsic::experimental_guard));
   HasGuards = GuardDecl && !GuardDecl->use_empty();
+  if (HasProfileData) {
+    BPI = std::move(BPI_);
+    BFI = std::move(BFI_);
+  }
 
   // Reduce the number of instructions duplicated when optimizing strictly for
   // size.
   if (BBDuplicateThreshold.getNumOccurrences())
     BBDupThreshold = BBDuplicateThreshold;
-  else if (F->hasFnAttribute(Attribute::MinSize))
+  else if (F.hasFnAttribute(Attribute::MinSize))
     BBDupThreshold = 3;
   else
     BBDupThreshold = DefaultBBDupThreshold;
@@ -419,26 +412,22 @@ bool JumpThreadingPass::runImpl(Function &F_, FunctionAnalysisManager *FAM_,
   assert(DTU && "DTU isn't passed into JumpThreading before using it.");
   assert(DTU->hasDomTree() && "JumpThreading relies on DomTree to proceed.");
   DominatorTree &DT = DTU->getDomTree();
-  for (auto &BB : *F)
+  for (auto &BB : F)
     if (!DT.isReachableFromEntry(&BB))
       Unreachable.insert(&BB);
 
   if (!ThreadAcrossLoopHeaders)
-    findLoopHeaders(*F);
-
-  HasProfile = llvm::any_of(*F, [&](BasicBlock &BB) {
-    return this->doesBlockHaveProfileData(&BB);
-  });
+    findLoopHeaders(F);
 
   bool EverChanged = false;
   bool Changed;
   do {
     Changed = false;
-    for (auto &BB : *F) {
+    for (auto &BB : F) {
       if (Unreachable.count(&BB))
         continue;
       while (processBlock(&BB)) // Thread all of the branches we can over BB.
-        Changed = ChangedSinceLastAnalysisUpdate = true;
+        Changed = true;
 
       // Jump threading may have introduced redundant debug values into BB
       // which should be removed.
@@ -448,7 +437,7 @@ bool JumpThreadingPass::runImpl(Function &F_, FunctionAnalysisManager *FAM_,
       // Stop processing BB if it's the entry or is now deleted. The following
       // routines attempt to eliminate BB and locating a suitable replacement
       // for the entry is non-trivial.
-      if (&BB == &F->getEntryBlock() || DTU->isBBPendingDeletion(&BB))
+      if (&BB == &F.getEntryBlock() || DTU->isBBPendingDeletion(&BB))
         continue;
 
       if (pred_empty(&BB)) {
@@ -459,8 +448,8 @@ bool JumpThreadingPass::runImpl(Function &F_, FunctionAnalysisManager *FAM_,
                           << '\n');
         LoopHeaders.erase(&BB);
         LVI->eraseBlock(&BB);
-        DeleteDeadBlock(&BB, DTU.get());
-        Changed = ChangedSinceLastAnalysisUpdate = true;
+        DeleteDeadBlock(&BB, DTU);
+        Changed = true;
         continue;
       }
 
@@ -475,12 +464,12 @@ bool JumpThreadingPass::runImpl(Function &F_, FunctionAnalysisManager *FAM_,
             // Don't alter Loop headers and latches to ensure another pass can
             // detect and transform nested loops later.
             !LoopHeaders.count(&BB) && !LoopHeaders.count(Succ) &&
-            TryToSimplifyUncondBranchFromEmptyBlock(&BB, DTU.get())) {
+            TryToSimplifyUncondBranchFromEmptyBlock(&BB, DTU)) {
           RemoveRedundantDbgInstrs(Succ);
           // BB is valid for cleanup here because we passed in DTU. F remains
           // BB's parent until a DTU->getDomTree() event.
           LVI->eraseBlock(&BB);
-          Changed = ChangedSinceLastAnalysisUpdate = true;
+          Changed = true;
         }
       }
     }
@@ -1151,8 +1140,8 @@ bool JumpThreadingPass::processBlock(BasicBlock *BB) {
                       << "' folding terminator: " << *BB->getTerminator()
                       << '\n');
     ++NumFolds;
-    ConstantFoldTerminator(BB, true, nullptr, DTU.get());
-    if (auto *BPI = getBPI())
+    ConstantFoldTerminator(BB, true, nullptr, DTU);
+    if (HasProfileData)
       BPI->eraseBlock(BB);
     return true;
   }
@@ -1307,7 +1296,7 @@ bool JumpThreadingPass::processImpliedCondition(BasicBlock *BB) {
         FICond->eraseFromParent();
 
       DTU->applyUpdatesPermissive({{DominatorTree::Delete, BB, RemoveSucc}});
-      if (auto *BPI = getBPI())
+      if (HasProfileData)
         BPI->eraseBlock(BB);
       return true;
     }
@@ -1751,7 +1740,7 @@ bool JumpThreadingPass::processThreadableEdges(Value *Cond, BasicBlock *BB,
       ++NumFolds;
       Term->eraseFromParent();
       DTU->applyUpdatesPermissive(Updates);
-      if (auto *BPI = getBPI())
+      if (HasProfileData)
         BPI->eraseBlock(BB);
 
       // If the condition is now dead due to the removal of the old terminator,
@@ -2004,7 +1993,7 @@ bool JumpThreadingPass::maybeMergeBasicBlockIntoOnlyPred(BasicBlock *BB) {
     LoopHeaders.insert(BB);
 
   LVI->eraseBlock(SinglePred);
-  MergeBasicBlockIntoOnlyPred(BB, DTU.get());
+  MergeBasicBlockIntoOnlyPred(BB, DTU);
 
   // Now that BB is merged into SinglePred (i.e. SinglePred code followed by
   // BB code within one basic block `BB`), we need to invalidate the LVI
@@ -2309,10 +2298,6 @@ void JumpThreadingPass::threadThroughTwoBasicBlocks(BasicBlock *PredPredBB,
   LLVM_DEBUG(dbgs() << "  Threading through '" << PredBB->getName() << "' and '"
                     << BB->getName() << "'\n");
 
-  // Build BPI/BFI before any changes are made to IR.
-  auto *BFI = getOrCreateBFI();
-  auto *BPI = getOrCreateBPI(BFI != nullptr);
-
   BranchInst *CondBr = cast<BranchInst>(BB->getTerminator());
   BranchInst *PredBBBranch = cast<BranchInst>(PredBB->getTerminator());
 
@@ -2322,8 +2307,7 @@ void JumpThreadingPass::threadThroughTwoBasicBlocks(BasicBlock *PredPredBB,
   NewBB->moveAfter(PredBB);
 
   // Set the block frequency of NewBB.
-  if (BFI) {
-    assert(BPI && "It's expected BPI to exist along with BFI");
+  if (HasProfileData) {
     auto NewBBFreq = BFI->getBlockFreq(PredPredBB) *
                      BPI->getEdgeProbability(PredPredBB, PredBB);
     BFI->setBlockFreq(NewBB, NewBBFreq.getFrequency());
@@ -2336,7 +2320,7 @@ void JumpThreadingPass::threadThroughTwoBasicBlocks(BasicBlock *PredPredBB,
       cloneInstructions(PredBB->begin(), PredBB->end(), NewBB, PredPredBB);
 
   // Copy the edge probabilities from PredBB to NewBB.
-  if (BPI)
+  if (HasProfileData)
     BPI->copyEdgeProbabilities(PredBB, NewBB);
 
   // Update the terminator of PredPredBB to jump to NewBB instead of PredBB.
@@ -2420,10 +2404,6 @@ void JumpThreadingPass::threadEdge(BasicBlock *BB,
   assert(!LoopHeaders.count(BB) && !LoopHeaders.count(SuccBB) &&
          "Don't thread across loop headers");
 
-  // Build BPI/BFI before any changes are made to IR.
-  auto *BFI = getOrCreateBFI();
-  auto *BPI = getOrCreateBPI(BFI != nullptr);
-
   // And finally, do it!  Start by factoring the predecessors if needed.
   BasicBlock *PredBB;
   if (PredBBs.size() == 1)
@@ -2447,8 +2427,7 @@ void JumpThreadingPass::threadEdge(BasicBlock *BB,
   NewBB->moveAfter(PredBB);
 
   // Set the block frequency of NewBB.
-  if (BFI) {
-    assert(BPI && "It's expected BPI to exist along with BFI");
+  if (HasProfileData) {
     auto NewBBFreq =
         BFI->getBlockFreq(PredBB) * BPI->getEdgeProbability(PredBB, BB);
     BFI->setBlockFreq(NewBB, NewBBFreq.getFrequency());
@@ -2507,13 +2486,10 @@ BasicBlock *JumpThreadingPass::splitBlockPreds(BasicBlock *BB,
   // Collect the frequencies of all predecessors of BB, which will be used to
   // update the edge weight of the result of splitting predecessors.
   DenseMap<BasicBlock *, BlockFrequency> FreqMap;
-  auto *BFI = getBFI();
-  if (BFI) {
-    auto *BPI = getOrCreateBPI(true);
+  if (HasProfileData)
     for (auto *Pred : Preds)
       FreqMap.insert(std::make_pair(
           Pred, BFI->getBlockFreq(Pred) * BPI->getEdgeProbability(Pred, BB)));
-  }
 
   // In the case when BB is a LandingPad block we create 2 new predecessors
   // instead of just one.
@@ -2532,10 +2508,10 @@ BasicBlock *JumpThreadingPass::splitBlockPreds(BasicBlock *BB,
     for (auto *Pred : predecessors(NewBB)) {
       Updates.push_back({DominatorTree::Delete, Pred, BB});
       Updates.push_back({DominatorTree::Insert, Pred, NewBB});
-      if (BFI) // Update frequencies between Pred -> NewBB.
+      if (HasProfileData) // Update frequencies between Pred -> NewBB.
         NewBBFreq += FreqMap.lookup(Pred);
     }
-    if (BFI) // Apply the summed frequency to NewBB.
+    if (HasProfileData) // Apply the summed frequency to NewBB.
       BFI->setBlockFreq(NewBB, NewBBFreq.getFrequency());
   }
 
@@ -2545,9 +2521,7 @@ BasicBlock *JumpThreadingPass::splitBlockPreds(BasicBlock *BB,
 
 bool JumpThreadingPass::doesBlockHaveProfileData(BasicBlock *BB) {
   const Instruction *TI = BB->getTerminator();
-  if (!TI || TI->getNumSuccessors() < 2)
-    return false;
-
+  assert(TI->getNumSuccessors() > 1 && "not a split");
   return hasValidBranchWeightMD(*TI);
 }
 
@@ -2558,19 +2532,11 @@ void JumpThreadingPass::updateBlockFreqAndEdgeWeight(BasicBlock *PredBB,
                                                      BasicBlock *BB,
                                                      BasicBlock *NewBB,
                                                      BasicBlock *SuccBB) {
-  bool DoesBlockHaveProfile = doesBlockHaveProfileData(BB);
-  auto *BFI = getBFI();
-  auto *BPI = getBPI();
-  assert(
-      (!DoesBlockHaveProfile || (BFI && BPI))
-          && "BFI & BPI should have already been created");
-  assert(
-      ((BFI && BPI) || (!BFI && !BFI))
-          && "It's not expected to have only either BPI or BFI");
-
-  if (!BFI)
+  if (!HasProfileData)
     return;
 
+  assert(BFI && BPI && "BFI & BPI should have been created here");
+
   // As the edge from PredBB to BB is deleted, we have to update the block
   // frequency of BB.
   auto BBOrigFreq = BFI->getBlockFreq(BB);
@@ -2642,7 +2608,7 @@ void JumpThreadingPass::updateBlockFreqAndEdgeWeight(BasicBlock *PredBB,
   // FIXME this locally as well so that BPI and BFI are consistent as well.  We
   // shouldn't make edges extremely likely or unlikely based solely on static
   // estimation.
-  if (BBSuccProbs.size() >= 2 && DoesBlockHaveProfile) {
+  if (BBSuccProbs.size() >= 2 && doesBlockHaveProfileData(BB)) {
     SmallVector<uint32_t, 4> Weights;
     for (auto Prob : BBSuccProbs)
       Weights.push_back(Prob.getNumerator());
@@ -2774,7 +2740,7 @@ bool JumpThreadingPass::duplicateCondBranchOnPHIIntoPred(
 
   // Remove the unconditional branch at the end of the PredBB block.
   OldPredBranch->eraseFromParent();
-  if (auto *BPI = getBPI())
+  if (HasProfileData)
     BPI->copyEdgeProbabilities(BB, PredBB);
   DTU->applyUpdatesPermissive(Updates);
 
@@ -2811,30 +2777,21 @@ void JumpThreadingPass::unfoldSelectInstr(BasicBlock *Pred, BasicBlock *BB,
   BI->copyMetadata(*SI, {LLVMContext::MD_prof});
   SIUse->setIncomingValue(Idx, SI->getFalseValue());
   SIUse->addIncoming(SI->getTrueValue(), NewBB);
-
-  uint64_t TrueWeight = 1;
-  uint64_t FalseWeight = 1;
-  // Copy probabilities from 'SI' to created conditional branch in 'Pred'.
-  if (extractBranchWeights(*SI, TrueWeight, FalseWeight) &&
-      (TrueWeight + FalseWeight) != 0) {
-    SmallVector<BranchProbability, 2> BP;
-    BP.emplace_back(BranchProbability::getBranchProbability(
-        TrueWeight, TrueWeight + FalseWeight));
-    BP.emplace_back(BranchProbability::getBranchProbability(
-        FalseWeight, TrueWeight + FalseWeight));
-    // Update BPI if exists.
-    if (auto *BPI = getBPI())
-      BPI->setEdgeProbability(Pred, BP);
-  }
   // Set the block frequency of NewBB.
-  if (auto *BFI = getBFI()) {
-    if ((TrueWeight + FalseWeight) == 0) {
-      TrueWeight = 1;
-      FalseWeight = 1;
+  if (HasProfileData) {
+    uint64_t TrueWeight, FalseWeight;
+    if (extractBranchWeights(*SI, TrueWeight, FalseWeight) &&
+        (TrueWeight + FalseWeight) != 0) {
+      SmallVector<BranchProbability, 2> BP;
+      BP.emplace_back(BranchProbability::getBranchProbability(
+          TrueWeight, TrueWeight + FalseWeight));
+      BP.emplace_back(BranchProbability::getBranchProbability(
+          FalseWeight, TrueWeight + FalseWeight));
+      BPI->setEdgeProbability(Pred, BP);
     }
-    BranchProbability PredToNewBBProb = BranchProbability::getBranchProbability(
-        TrueWeight, TrueWeight + FalseWeight);
-    auto NewBBFreq = BFI->getBlockFreq(Pred) * PredToNewBBProb;
+
+    auto NewBBFreq =
+        BFI->getBlockFreq(Pred) * BPI->getEdgeProbability(Pred, NewBB);
     BFI->setBlockFreq(NewBB, NewBBFreq.getFrequency());
   }
 
@@ -3155,93 +3112,3 @@ bool JumpThreadingPass::threadGuard(BasicBlock *BB, IntrinsicInst *Guard,
   }
   return true;
 }
-
-PreservedAnalyses JumpThreadingPass::getPreservedAnalysis() const {
-  PreservedAnalyses PA;
-  PA.preserve<LazyValueAnalysis>();
-  PA.preserve<DominatorTreeAnalysis>();
-
-  // TODO: We would like to preserve BPI/BFI. Enable once all paths update them.
-  // TODO: Would be nice to verify BPI/BFI consistency as well.
-  return PA;
-}
-
-template <typename AnalysisT>
-typename AnalysisT::Result *JumpThreadingPass::runExternalAnalysis() {
-  assert(FAM && "Can't run external analysis without FunctionAnalysisManager");
-
-  // If there were no changes since last call to 'runExternalAnalysis' then all
-  // analysis is either up to date or explicitly invalidated. Just go ahead and
-  // run the "external" analysis.
-  if (!ChangedSinceLastAnalysisUpdate) {
-    assert(!DTU->hasPendingUpdates() &&
-           "Lost update of 'ChangedSinceLastAnalysisUpdate'?");
-    // Run the "external" analysis.
-    return &FAM->getResult<AnalysisT>(*F);
-  }
-  ChangedSinceLastAnalysisUpdate = false;
-
-  auto PA = getPreservedAnalysis();
-  // TODO: This shouldn't be needed once 'getPreservedAnalysis' reports BPI/BFI
-  // as preserved.
-  PA.preserve<BranchProbabilityAnalysis>();
-  PA.preserve<BlockFrequencyAnalysis>();
-  // Report everything except explicitly preserved as invalid.
-  FAM->invalidate(*F, PA);
-  // Update DT/PDT.
-  DTU->flush();
-  // Make sure DT/PDT are valid before running "external" analysis.
-  assert(DTU->getDomTree().verify(DominatorTree::VerificationLevel::Fast));
-  assert((!DTU->hasPostDomTree() ||
-          DTU->getPostDomTree().verify(
-              PostDominatorTree::VerificationLevel::Fast)));
-  // Run the "external" analysis.
-  auto *Result = &FAM->getResult<AnalysisT>(*F);
-  // Update analysis JumpThreading depends on and not explicitly preserved.
-  TTI = &FAM->getResult<TargetIRAnalysis>(*F);
-  TLI = &FAM->getResult<TargetLibraryAnalysis>(*F);
-  AA = &FAM->getResult<AAManager>(*F);
-
-  return Result;
-}
-
-BranchProbabilityInfo *JumpThreadingPass::getBPI() {
-  if (!BPI) {
-    assert(FAM && "Can't create BPI without FunctionAnalysisManager");
-    BPI = FAM->getCachedResult<BranchProbabilityAnalysis>(*F);
-  }
-  return *BPI;
-}
-
-BlockFrequencyInfo *JumpThreadingPass::getBFI() {
-  if (!BFI) {
-    assert(FAM && "Can't create BFI without FunctionAnalysisManager");
-    BFI = FAM->getCachedResult<BlockFrequencyAnalysis>(*F);
-  }
-  return *BFI;
-}
-
-// Important note on validity of BPI/BFI. JumpThreading tries to preserve
-// BPI/BFI as it goes. Thus if cached instance exists it will be updated.
-// Otherwise, new instance of BPI/BFI is created (up to date by definition).
-BranchProbabilityInfo *JumpThreadingPass::getOrCreateBPI(bool Force) {
-  auto *Res = getBPI();
-  if (Res)
-    return Res;
-
-  if (Force || HasProfile)
-    BPI = runExternalAnalysis<BranchProbabilityAnalysis>();
-
-  return *BPI;
-}
-
-BlockFrequencyInfo *JumpThreadingPass::getOrCreateBFI(bool Force) {
-  auto *Res = getBFI();
-  if (Res)
-    return Res;
-
-  if (Force || HasProfile)
-    BFI = runExternalAnalysis<BlockFrequencyAnalysis>();
-
-  return *BFI;
-}

diff  --git a/llvm/test/Transforms/JumpThreading/select.ll b/llvm/test/Transforms/JumpThreading/select.ll
index 4ec55a66bb8ac..67025b286953c 100644
--- a/llvm/test/Transforms/JumpThreading/select.ll
+++ b/llvm/test/Transforms/JumpThreading/select.ll
@@ -1,14 +1,13 @@
 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --check-globals
-; RUN: opt -S -passes="jump-threading" -debug-only=branch-prob < %s 2>&1 | FileCheck %s
-; RUN: opt -S -passes="require<branch-prob>,jump-threading" -debug-only=branch-prob < %s 2>&1 | FileCheck -check-prefixes=CHECK,CHECK-BPI %s
+; RUN: opt -S -passes=jump-threading -debug-only=branch-prob < %s 2>&1 | FileCheck %s
 ; REQUIRES: asserts
 
-; CHECK-BPI-LABEL:  ---- Branch Probability Info : unfold1 ----
-; CHECK-BPI:       set edge cond.false -> 0 successor probability to 0x20000000 / 0x80000000 = 25.00%
-; CHECK-BPI:       set edge cond.false -> 1 successor probability to 0x60000000 / 0x80000000 = 75.00%
-; CHECK-BPI-LABEL:  ---- Branch Probability Info : unfold2 ----
-; CHECK-BPI:       set edge cond.false -> 0 successor probability to 0x20000000 / 0x80000000 = 25.00%
-; CHECK-BPI:       set edge cond.false -> 1 successor probability to 0x60000000 / 0x80000000 = 75.00%
+; CHECK-LABEL:  ---- Branch Probability Info : unfold1 ----
+; CHECK:       set edge cond.false -> 0 successor probability to 0x20000000 / 0x80000000 = 25.00%
+; CHECK:       set edge cond.false -> 1 successor probability to 0x60000000 / 0x80000000 = 75.00%
+; CHECK-LABEL:  ---- Branch Probability Info : unfold2 ----
+; CHECK:       set edge cond.false -> 0 successor probability to 0x20000000 / 0x80000000 = 25.00%
+; CHECK:       set edge cond.false -> 1 successor probability to 0x60000000 / 0x80000000 = 75.00%
 
 declare void @foo()
 declare void @bar()

diff  --git a/llvm/test/Transforms/JumpThreading/thread-prob-2.ll b/llvm/test/Transforms/JumpThreading/thread-prob-2.ll
index cbb8339597df0..d2b5b9e0e2a12 100644
--- a/llvm/test/Transforms/JumpThreading/thread-prob-2.ll
+++ b/llvm/test/Transforms/JumpThreading/thread-prob-2.ll
@@ -1,12 +1,10 @@
-; RUN: opt -debug-only=branch-prob -passes="require<branch-prob>,jump-threading" -S %s 2>&1 | FileCheck %s
-; RUN: opt -debug-only=branch-prob -passes=jump-threading -S %s 2>&1 | FileCheck %s --check-prefix=CHECK-NOBPI
+; RUN: opt -debug-only=branch-prob -passes=jump-threading -S %s 2>&1 | FileCheck %s
 ; REQUIRES: asserts
 
 ; Make sure that we clear edge probabilities for bb.cond as we fold
 ; the conditional branch in it.
 
 ; CHECK: eraseBlock bb.cond
-; CHECK-NOBPI-NOT: eraseBlock bb.cond
 
 define i32 @foo(i1 %cond) !prof !0 {
 ; CHECK-LABEL: @foo

diff  --git a/llvm/test/Transforms/JumpThreading/thread-prob-3.ll b/llvm/test/Transforms/JumpThreading/thread-prob-3.ll
index 30afd8e6033b9..705bb475287c3 100644
--- a/llvm/test/Transforms/JumpThreading/thread-prob-3.ll
+++ b/llvm/test/Transforms/JumpThreading/thread-prob-3.ll
@@ -1,5 +1,4 @@
-; RUN: opt -debug-only=branch-prob -passes="require<branch-prob>,jump-threading" -S %s 2>&1 | FileCheck %s
-; RUN: opt -debug-only=branch-prob -passes=jump-threading -S %s 2>&1 | FileCheck %s --check-prefix=CHECK-NOBPI
+; RUN: opt -debug-only=branch-prob -passes=jump-threading -S %s 2>&1 | FileCheck %s
 ; REQUIRES: asserts
 
 ; Make sure that we set edge probabilities for bb2 as we
@@ -8,7 +7,6 @@
 ; CHECK-LABEL: ---- Branch Probability Info : foo
 ; CHECK:      set edge bb2 -> 0 successor probability to 0x80000000 / 0x80000000 = 100.00%
 ; CHECK-NEXT: set edge bb2 -> 1 successor probability to 0x00000000 / 0x80000000 = 0.00%
-; CHECK-NOBPI-NOT: ---- Branch Probability Info : foo
 define void @foo(i1 %f0, i1 %f1, i1 %f2) !prof !{!"function_entry_count", i64 0} {
 ; CHECK-LABEL: @foo(
 bb1:

diff  --git a/llvm/test/Transforms/JumpThreading/thread-prob-4.ll b/llvm/test/Transforms/JumpThreading/thread-prob-4.ll
index fcdb51924b3e1..e80985c7f8b07 100644
--- a/llvm/test/Transforms/JumpThreading/thread-prob-4.ll
+++ b/llvm/test/Transforms/JumpThreading/thread-prob-4.ll
@@ -1,12 +1,10 @@
-; RUN: opt -debug-only=branch-prob -passes="require<branch-prob>,jump-threading" -S %s 2>&1 | FileCheck %s
-; RUN: opt -debug-only=branch-prob -passes=jump-threading -S %s 2>&1 | FileCheck %s --check-prefix=CHECK-NOBPI
+; RUN: opt -debug-only=branch-prob -passes=jump-threading -S %s 2>&1 | FileCheck %s
 ; REQUIRES: asserts
 
 ; Make sure that we clear edge probabilities for bb1 as we fold
 ; the conditional branch in it.
 
 ; CHECK: eraseBlock bb1
-; CHECK-NOBPI-NOT: eraseBlock bb1
 
 define i32 @foo(i32 %arg) !prof !0 {
 ; CHECK-LABEL: @foo

diff  --git a/llvm/test/Transforms/JumpThreading/thread-prob-5.ll b/llvm/test/Transforms/JumpThreading/thread-prob-5.ll
index 95371e1e3b071..a3bcfdd87a286 100644
--- a/llvm/test/Transforms/JumpThreading/thread-prob-5.ll
+++ b/llvm/test/Transforms/JumpThreading/thread-prob-5.ll
@@ -1,12 +1,10 @@
-; RUN: opt -debug-only=branch-prob -passes="require<branch-prob>,jump-threading" -S %s 2>&1 | FileCheck %s
-; RUN: opt -debug-only=branch-prob -passes=jump-threading -S %s 2>&1 | FileCheck %s --check-prefix=CHECK-NOBPI
+; RUN: opt -debug-only=branch-prob -passes=jump-threading -S %s 2>&1 | FileCheck %s
 ; REQUIRES: asserts
 
 ; Make sure that we clear edge probabilities for bb1 as we fold
 ; the conditional branch in it.
 
 ; CHECK: eraseBlock bb1
-; CHECK-NOBPI-NOT: eraseBlock bb1
 
 define void @foo(i32 %i, i32 %len) !prof !0 {
 ; CHECK-LABEL: @foo

diff  --git a/llvm/test/Transforms/JumpThreading/thread-prob-6.ll b/llvm/test/Transforms/JumpThreading/thread-prob-6.ll
index 6a0746a60e1e1..0384c73968b9a 100644
--- a/llvm/test/Transforms/JumpThreading/thread-prob-6.ll
+++ b/llvm/test/Transforms/JumpThreading/thread-prob-6.ll
@@ -1,12 +1,10 @@
-; RUN: opt -debug-only=branch-prob -passes="require<branch-prob>,jump-threading" -S %s 2>&1 | FileCheck %s
-; RUN: opt -debug-only=branch-prob -passes=jump-threading -S %s 2>&1 | FileCheck %s --check-prefix=CHECK-NOBPI
+; RUN: opt -debug-only=branch-prob -passes=jump-threading -S %s 2>&1 | FileCheck %s
 ; REQUIRES: asserts
 
 ; Make sure that we clear edge probabilities for bb1 as we fold
 ; the conditional branch in it.
 
 ; CHECK: eraseBlock bb1
-; CHECK-NOBPI-NOT: eraseBlock bb1
 
 define i32 @foo() !prof !0 {
 ; CHECK-LABEL: @foo

diff  --git a/llvm/test/Transforms/JumpThreading/threading_prof1.ll b/llvm/test/Transforms/JumpThreading/threading_prof1.ll
index 28f1abe2d0948..5abc89071d388 100644
--- a/llvm/test/Transforms/JumpThreading/threading_prof1.ll
+++ b/llvm/test/Transforms/JumpThreading/threading_prof1.ll
@@ -95,4 +95,4 @@ declare i32 @b()
 
 !0 = !{!"branch_weights", i32 2146410443, i32 1073205}
 ;CHECK: ![[PROF1]] = !{!"branch_weights", i32 1073205, i32 2146410443}
-;CHECK: ![[PROF2]] = !{!"branch_weights", i32 -2147483648, i32 0}
+;CHECK: ![[PROF2]] = !{!"branch_weights", i32 2146410443, i32 1073205}

diff  --git a/llvm/test/Transforms/JumpThreading/threading_prof2.ll b/llvm/test/Transforms/JumpThreading/threading_prof2.ll
index 8b217411cd1da..f81770285d6af 100644
--- a/llvm/test/Transforms/JumpThreading/threading_prof2.ll
+++ b/llvm/test/Transforms/JumpThreading/threading_prof2.ll
@@ -38,4 +38,4 @@ declare i32 @b()
 
 !0 = !{!"branch_weights", i32 2146410443, i32 1073205}
 ;CHECK: ![[PROF1]] = !{!"branch_weights", i32 1073205, i32 2146410443}
-;CHECK: ![[PROF2]] = !{!"branch_weights", i32 -2147483648, i32 0}
+;CHECK: ![[PROF2]] = !{!"branch_weights", i32 2146410443, i32 1073205}

diff  --git a/llvm/test/Transforms/JumpThreading/threading_prof3.ll b/llvm/test/Transforms/JumpThreading/threading_prof3.ll
index e7332018c4629..29a964d0ebff0 100644
--- a/llvm/test/Transforms/JumpThreading/threading_prof3.ll
+++ b/llvm/test/Transforms/JumpThreading/threading_prof3.ll
@@ -26,4 +26,4 @@ bb9:
   ret void
 }
 
-;CHECK: ![[PROF]] = !{!"branch_weights", i32 -2147483648, i32 0}
+;CHECK: ![[PROF]] = !{!"branch_weights", i32 0, i32 0}


        


More information about the llvm-commits mailing list