xref: /freebsd/contrib/llvm-project/llvm/lib/CodeGen/MachineBranchProbabilityInfo.cpp (revision c7a063741720ef81d4caa4613242579d12f1d605)
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 namespace llvm {
29 cl::opt<unsigned>
30     StaticLikelyProb("static-likely-prob",
31                      cl::desc("branch probability threshold in percentage"
32                               "to be considered very likely"),
33                      cl::init(80), cl::Hidden);
34 
35 cl::opt<unsigned> ProfileLikelyProb(
36     "profile-likely-prob",
37     cl::desc("branch probability threshold in percentage to be considered"
38              " very likely when profile is available"),
39     cl::init(51), cl::Hidden);
40 } // namespace llvm
41 
42 char MachineBranchProbabilityInfo::ID = 0;
43 
44 MachineBranchProbabilityInfo::MachineBranchProbabilityInfo()
45     : ImmutablePass(ID) {
46   PassRegistry &Registry = *PassRegistry::getPassRegistry();
47   initializeMachineBranchProbabilityInfoPass(Registry);
48 }
49 
50 void MachineBranchProbabilityInfo::anchor() {}
51 
52 BranchProbability MachineBranchProbabilityInfo::getEdgeProbability(
53     const MachineBasicBlock *Src,
54     MachineBasicBlock::const_succ_iterator Dst) const {
55   return Src->getSuccProbability(Dst);
56 }
57 
58 BranchProbability MachineBranchProbabilityInfo::getEdgeProbability(
59     const MachineBasicBlock *Src, const MachineBasicBlock *Dst) const {
60   // This is a linear search. Try to use the const_succ_iterator version when
61   // possible.
62   return getEdgeProbability(Src, find(Src->successors(), Dst));
63 }
64 
65 bool MachineBranchProbabilityInfo::isEdgeHot(
66     const MachineBasicBlock *Src, const MachineBasicBlock *Dst) const {
67   BranchProbability HotProb(StaticLikelyProb, 100);
68   return getEdgeProbability(Src, Dst) > HotProb;
69 }
70 
71 raw_ostream &MachineBranchProbabilityInfo::printEdgeProbability(
72     raw_ostream &OS, const MachineBasicBlock *Src,
73     const MachineBasicBlock *Dst) const {
74 
75   const BranchProbability Prob = getEdgeProbability(Src, Dst);
76   OS << "edge " << printMBBReference(*Src) << " -> " << printMBBReference(*Dst)
77      << " probability is " << Prob
78      << (isEdgeHot(Src, Dst) ? " [HOT edge]\n" : "\n");
79 
80   return OS;
81 }
82