LLVM  10.0.0
MachineBranchProbabilityInfo.cpp
Go to the documentation of this file.
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 
15 #include "llvm/IR/Instructions.h"
16 #include "llvm/InitializePasses.h"
18 #include "llvm/Support/Debug.h"
20 
21 using namespace llvm;
22 
24  "Machine Branch Probability Analysis", false, true)
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 
42 MachineBranchProbabilityInfo::MachineBranchProbabilityInfo()
43  : ImmutablePass(ID) {
46 }
47 
48 void MachineBranchProbabilityInfo::anchor() {}
49 
51  const MachineBasicBlock *Src,
53  return Src->getSuccProbability(Dst);
54 }
55 
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 
64  const MachineBasicBlock *Src, const MachineBasicBlock *Dst) const {
65  BranchProbability HotProb(StaticLikelyProb, 100);
66  return getEdgeProbability(Src, Dst) > HotProb;
67 }
68 
71  auto MaxProb = BranchProbability::getZero();
72  MachineBasicBlock *MaxSucc = nullptr;
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 
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 }
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
coro Split coroutine into a set of functions driving its state machine
Definition: CoroSplit.cpp:1612
This class represents lattice values for constants.
Definition: AllocatorList.h:23
machine branch prob
A global registry used in conjunction with static constructors to make pluggable components (like tar...
Definition: Registry.h:44
machine branch Machine Branch Probability true cl::opt< unsigned > StaticLikelyProb("static-likely-prob", cl::desc("branch probability threshold in percentage" "to be considered very likely"), cl::init(80), cl::Hidden)
raw_ostream & printEdgeProbability(raw_ostream &OS, const MachineBasicBlock *Src, const MachineBasicBlock *Dst) const
block Block Frequency true
iterator_range< succ_iterator > successors()
Printable printMBBReference(const MachineBasicBlock &MBB)
Prints a machine basic block reference.
MachineBasicBlock * getHotSucc(MachineBasicBlock *MBB) const
INITIALIZE_PASS_BEGIN(MachineBranchProbabilityInfo, "machine-branch-prob", "Machine Branch Probability Analysis", false, true) INITIALIZE_PASS_END(MachineBranchProbabilityInfo
COFF::MachineTypes Machine
Definition: COFFYAML.cpp:365
return !LiveInRegUnits available(Reg)
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:435
cl::opt< unsigned > ProfileLikelyProb("profile-likely-prob", cl::desc("branch probability threshold in percentage to be considered" " very likely when profile is available"), cl::init(51), cl::Hidden)
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
std::vector< MachineBasicBlock * >::const_iterator const_succ_iterator
auto find(R &&Range, const T &Val) -> decltype(adl_begin(Range))
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:1203
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE, "Assign register bank of generic virtual registers", false, false) RegBankSelect
ImmutablePass class - This class is used to provide information that does not need to be run...
Definition: Pass.h:253
BranchProbability getEdgeProbability(const MachineBasicBlock *Src, const MachineBasicBlock *Dst) const
pgo instr Read PGO instrumentation profile
#define I(x, y, z)
Definition: MD5.cpp:58
void initializeMachineBranchProbabilityInfoPass(PassRegistry &)
bool isEdgeHot(const MachineBasicBlock *Src, const MachineBasicBlock *Dst) const
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:46
hexagon cext opt
PassRegistry - This class manages the registration and intitialization of the pass subsystem as appli...
Definition: PassRegistry.h:38
static BranchProbability getZero()