1 //===- MachineBranchProbabilityInfo.cpp - Machine Branch Probability Info -===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This analysis uses probability info stored in Machine Basic Blocks.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
14 #include "llvm/CodeGen/MachineBasicBlock.h"
15 #include "llvm/IR/Instructions.h"
16 #include "llvm/InitializePasses.h"
17 #include "llvm/Support/CommandLine.h"
18 #include "llvm/Support/Debug.h"
19 #include "llvm/Support/raw_ostream.h"
20 
21 using namespace llvm;
22 
23 INITIALIZE_PASS_BEGIN(MachineBranchProbabilityInfo, "machine-branch-prob",
24                       "Machine Branch Probability Analysis", false, true)
25 INITIALIZE_PASS_END(MachineBranchProbabilityInfo, "machine-branch-prob",
26                     "Machine Branch Probability Analysis", false, true)
27 
28 cl::opt<unsigned>
29     StaticLikelyProb("static-likely-prob",
30                      cl::desc("branch probability threshold in percentage"
31                               "to be considered very likely"),
32                      cl::init(80), cl::Hidden);
33 
34 cl::opt<unsigned> ProfileLikelyProb(
35     "profile-likely-prob",
36     cl::desc("branch probability threshold in percentage to be considered"
37              " very likely when profile is available"),
38     cl::init(51), cl::Hidden);
39 
40 char MachineBranchProbabilityInfo::ID = 0;
41 
MachineBranchProbabilityInfo()42 MachineBranchProbabilityInfo::MachineBranchProbabilityInfo()
43     : ImmutablePass(ID) {
44   PassRegistry &Registry = *PassRegistry::getPassRegistry();
45   initializeMachineBranchProbabilityInfoPass(Registry);
46 }
47 
anchor()48 void MachineBranchProbabilityInfo::anchor() {}
49 
getEdgeProbability(const MachineBasicBlock * Src,MachineBasicBlock::const_succ_iterator Dst) const50 BranchProbability MachineBranchProbabilityInfo::getEdgeProbability(
51     const MachineBasicBlock *Src,
52     MachineBasicBlock::const_succ_iterator Dst) const {
53   return Src->getSuccProbability(Dst);
54 }
55 
getEdgeProbability(const MachineBasicBlock * Src,const MachineBasicBlock * Dst) const56 BranchProbability MachineBranchProbabilityInfo::getEdgeProbability(
57     const MachineBasicBlock *Src, const MachineBasicBlock *Dst) const {
58   // This is a linear search. Try to use the const_succ_iterator version when
59   // possible.
60   return getEdgeProbability(Src, find(Src->successors(), Dst));
61 }
62 
isEdgeHot(const MachineBasicBlock * Src,const MachineBasicBlock * Dst) const63 bool MachineBranchProbabilityInfo::isEdgeHot(
64     const MachineBasicBlock *Src, const MachineBasicBlock *Dst) const {
65   BranchProbability HotProb(StaticLikelyProb, 100);
66   return getEdgeProbability(Src, Dst) > HotProb;
67 }
68 
69 MachineBasicBlock *
getHotSucc(MachineBasicBlock * MBB) const70 MachineBranchProbabilityInfo::getHotSucc(MachineBasicBlock *MBB) const {
71   auto MaxProb = BranchProbability::getZero();
72   MachineBasicBlock *MaxSucc = nullptr;
73   for (MachineBasicBlock::const_succ_iterator I = MBB->succ_begin(),
74        E = MBB->succ_end(); I != E; ++I) {
75     auto Prob = getEdgeProbability(MBB, I);
76     if (Prob > MaxProb) {
77       MaxProb = Prob;
78       MaxSucc = *I;
79     }
80   }
81 
82   BranchProbability HotProb(StaticLikelyProb, 100);
83   if (getEdgeProbability(MBB, MaxSucc) >= HotProb)
84     return MaxSucc;
85 
86   return nullptr;
87 }
88 
printEdgeProbability(raw_ostream & OS,const MachineBasicBlock * Src,const MachineBasicBlock * Dst) const89 raw_ostream &MachineBranchProbabilityInfo::printEdgeProbability(
90     raw_ostream &OS, const MachineBasicBlock *Src,
91     const MachineBasicBlock *Dst) const {
92 
93   const BranchProbability Prob = getEdgeProbability(Src, Dst);
94   OS << "edge " << printMBBReference(*Src) << " -> " << printMBBReference(*Dst)
95      << " probability is " << Prob
96      << (isEdgeHot(Src, Dst) ? " [HOT edge]\n" : "\n");
97 
98   return OS;
99 }
100