]> granicus.if.org Git - llvm/commitdiff
[PM] Port Branch Probability Analysis pass to the new pass manager.
authorXinliang David Li <davidxl@google.com>
Thu, 5 May 2016 02:59:57 +0000 (02:59 +0000)
committerXinliang David Li <davidxl@google.com>
Thu, 5 May 2016 02:59:57 +0000 (02:59 +0000)
Differential Revision: http://reviews.llvm.org/D19839

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@268601 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/Analysis/BranchProbabilityInfo.h
lib/Analysis/BranchProbabilityInfo.cpp
lib/Passes/PassBuilder.cpp
lib/Passes/PassRegistry.def
test/Analysis/BranchProbabilityInfo/basic.ll
test/Analysis/BranchProbabilityInfo/deopt-intrinsic.ll
test/Analysis/BranchProbabilityInfo/loop.ll
test/Analysis/BranchProbabilityInfo/noreturn.ll
test/Analysis/BranchProbabilityInfo/pr18705.ll
test/Analysis/BranchProbabilityInfo/pr22718.ll

index 6a0221209a05cd291028e2b42fde8468463eb9a7..54514a9f1f39be14fc7a28a1dd6229940f9ed082 100644 (file)
@@ -17,6 +17,7 @@
 #include "llvm/ADT/DenseMap.h"
 #include "llvm/ADT/SmallPtrSet.h"
 #include "llvm/IR/CFG.h"
+#include "llvm/IR/PassManager.h"
 #include "llvm/InitializePasses.h"
 #include "llvm/Pass.h"
 #include "llvm/Support/BranchProbability.h"
@@ -44,6 +45,19 @@ public:
     calculate(F, LI);
   }
 
+  BranchProbabilityInfo(BranchProbabilityInfo &&Arg)
+      : Probs(std::move(Arg.Probs)), LastF(Arg.LastF),
+        PostDominatedByUnreachable(std::move(Arg.PostDominatedByUnreachable)),
+        PostDominatedByColdCall(std::move(Arg.PostDominatedByColdCall)) {}
+
+  BranchProbabilityInfo &operator=(BranchProbabilityInfo &&RHS) {
+    releaseMemory();
+    Probs = std::move(RHS.Probs);
+    PostDominatedByColdCall = std::move(RHS.PostDominatedByColdCall);
+    PostDominatedByUnreachable = std::move(RHS.PostDominatedByUnreachable);
+    return *this;
+  }
+
   void releaseMemory();
 
   void print(raw_ostream &OS) const;
@@ -103,6 +117,9 @@ public:
   void calculate(const Function &F, const LoopInfo &LI);
 
 private:
+  void operator=(const BranchProbabilityInfo &) = delete;
+  BranchProbabilityInfo(const BranchProbabilityInfo &) = delete;
+
   // Since we allow duplicate edges from one basic block to another, we use
   // a pair (PredBlock and an index in the successors) to specify an edge.
   typedef std::pair<const BasicBlock *, unsigned> Edge;
@@ -136,6 +153,30 @@ private:
   bool calcInvokeHeuristics(const BasicBlock *BB);
 };
 
+/// \brief Analysis pass which computes \c BranchProbabilityInfo.
+class BranchProbabilityAnalysis
+    : public AnalysisInfoMixin<BranchProbabilityAnalysis> {
+  friend AnalysisInfoMixin<BranchProbabilityAnalysis>;
+  static char PassID;
+
+public:
+  /// \brief Provide the result typedef for this analysis pass.
+  typedef BranchProbabilityInfo Result;
+
+  /// \brief Run the analysis pass over a function and produce BPI.
+  BranchProbabilityInfo run(Function &F, AnalysisManager<Function> &AM);
+};
+
+/// \brief Printer pass for the \c BranchProbabilityAnalysis results.
+class BranchProbabilityPrinterPass
+    : public PassInfoMixin<BranchProbabilityPrinterPass> {
+  raw_ostream &OS;
+
+public:
+  explicit BranchProbabilityPrinterPass(raw_ostream &OS) : OS(OS) {}
+  PreservedAnalyses run(Function &F, AnalysisManager<Function> &AM);
+};
+
 /// \brief Legacy analysis pass which computes \c BranchProbabilityInfo.
 class BranchProbabilityInfoWrapperPass : public FunctionPass {
   BranchProbabilityInfo BPI;
index b431e62162266d1458a5c1cfd6cc1418647ae59c..33036b7261557d2b57bab0ba145327fa93f5908b 100644 (file)
@@ -689,3 +689,20 @@ void BranchProbabilityInfoWrapperPass::print(raw_ostream &OS,
                                              const Module *) const {
   BPI.print(OS);
 }
+
+char BranchProbabilityAnalysis::PassID;
+BranchProbabilityInfo
+BranchProbabilityAnalysis::run(Function &F, AnalysisManager<Function> &AM) {
+  BranchProbabilityInfo BPI;
+  BPI.calculate(F, AM.getResult<LoopAnalysis>(F));
+  return BPI;
+}
+
+PreservedAnalyses
+BranchProbabilityPrinterPass::run(Function &F, AnalysisManager<Function> &AM) {
+  OS << "Printing analysis results of BPI for function "
+     << "'" << F.getName() << "':"
+     << "\n";
+  AM.getResult<BranchProbabilityAnalysis>(F).print(OS);
+  return PreservedAnalyses::all();
+}
index f17eb681f1c2f9e2ca65f43afe2b12694888000d..bc03cc1059ac37fe611efe344d6862cd0c13f681 100644 (file)
@@ -21,6 +21,7 @@
 #include "llvm/Analysis/AliasAnalysisEvaluator.h"
 #include "llvm/Analysis/AssumptionCache.h"
 #include "llvm/Analysis/BasicAliasAnalysis.h"
+#include "llvm/Analysis/BranchProbabilityInfo.h"
 #include "llvm/Analysis/CFLAliasAnalysis.h"
 #include "llvm/Analysis/CGSCCPassManager.h"
 #include "llvm/Analysis/CallGraph.h"
index 6bae08efea2bcda929b7d3cb2f303629c82762aa..37ef2245f43c73ac18ec451622c1705b61093e45 100644 (file)
@@ -71,6 +71,7 @@ CGSCC_PASS("no-op-cgscc", NoOpCGSCCPass())
 #endif
 FUNCTION_ANALYSIS("aa", AAManager())
 FUNCTION_ANALYSIS("assumptions", AssumptionAnalysis())
+FUNCTION_ANALYSIS("branch-prob", BranchProbabilityAnalysis())
 FUNCTION_ANALYSIS("domtree", DominatorTreeAnalysis())
 FUNCTION_ANALYSIS("postdomtree", PostDominatorTreeAnalysis())
 FUNCTION_ANALYSIS("demanded-bits", DemandedBitsAnalysis())
@@ -110,6 +111,7 @@ FUNCTION_PASS("lower-expect", LowerExpectIntrinsicPass())
 FUNCTION_PASS("gvn", GVN())
 FUNCTION_PASS("print", PrintFunctionPass(dbgs()))
 FUNCTION_PASS("print<assumptions>", AssumptionPrinterPass(dbgs()))
+FUNCTION_PASS("print<branch-prob>", BranchProbabilityPrinterPass(dbgs()))
 FUNCTION_PASS("print<domtree>", DominatorTreePrinterPass(dbgs()))
 FUNCTION_PASS("print<postdomtree>", PostDominatorTreePrinterPass(dbgs()))
 FUNCTION_PASS("print<demanded-bits>", DemandedBitsPrinterPass(dbgs()))
index d833b8339aac4c9c2b6dd51ce77c2818774daf13..d86709130f34ce8a1859cde4583a128cdcf8411e 100644 (file)
@@ -1,4 +1,5 @@
 ; RUN: opt < %s -analyze -branch-prob | FileCheck %s
+; RUN: opt < %s -passes='print<branch-prob>' -disable-output 2>&1 | FileCheck %s
 
 define i32 @test1(i32 %i, i32* %a) {
 ; CHECK: Printing analysis {{.*}} for function 'test1'
index 8728d4ab8fefc9b0b1333281f71dfa1695a153ed..faa09f9e8a0c489895afc85de54557b2ee31b827 100644 (file)
@@ -1,9 +1,10 @@
 ; RUN: opt -analyze -branch-prob < %s | FileCheck %s
+; RUN: opt < %s -passes='print<branch-prob>' -disable-output 2>&1 | FileCheck %s
 
 declare i32 @llvm.experimental.deoptimize.i32(...)
 
 define i32 @test1(i32 %a, i32 %b) {
-; CHECK-LABEL: Printing analysis 'Branch Probability Analysis' for function 'test1':
+; CHECK-LABEL: Printing analysis {{.*}} for function 'test1':
 entry:
   %cond = icmp eq i32 %a, 42
   br i1 %cond, label %exit, label %deopt
index 5be7adf3909df6d9d204d71d896ac3bf76e55c88..1a37a7a4659ef98ad4d46123b11950a0b0a791b3 100644 (file)
@@ -1,5 +1,6 @@
 ; Test the static branch probability heuristics for no-return functions.
 ; RUN: opt < %s -analyze -branch-prob | FileCheck %s
+; RUN: opt < %s -passes='print<branch-prob>' --disable-output 2>&1 | FileCheck %s
 
 declare void @g1()
 declare void @g2()
index 7098c2f7b8ccf4b161b121eb94762333cd14a047..0c2fe863d034d0475afe33497681c4babadfed16 100644 (file)
@@ -1,5 +1,6 @@
 ; Test the static branch probability heuristics for no-return functions.
 ; RUN: opt < %s -analyze -branch-prob | FileCheck %s
+; RUN: opt < %s -passes='print<branch-prob>' -disable-output 2>&1 | FileCheck %s
 
 declare void @abort() noreturn
 
index f5f9612fcdb6724317ffcdf0ac21f8741f651ab2..74983ef1b4f0128efb59779e6aa5aeb9729a239c 100644 (file)
@@ -1,4 +1,5 @@
 ; RUN: opt < %s -analyze -branch-prob | FileCheck %s
+; RUN: opt < %s -passes='print<branch-prob>' -disable-output 2>&1 | FileCheck %s
 
 ; Since neither of while.body's out-edges is an exit or a back edge,
 ; calcLoopBranchHeuristics should return early without setting the weights.
index 51bbd13e83c164a9f1697c534e3439c25e51a4d5..66ac89793b877cd3034109dbd78b0d7cab7b7044 100644 (file)
@@ -1,4 +1,5 @@
 ; RUN: opt < %s -analyze -branch-prob | FileCheck %s
+; RUN: opt < %s -passes='print<branch-prob>' -disable-output 2>&1 | FileCheck %s
 
 ; In this test, the else clause is taken about 90% of the time. This was not
 ; reflected in the probability computation because the weight is larger than