LLVM  13.0.0git
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 namespace llvm {
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 
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 
43 
45  : ImmutablePass(ID) {
48 }
49 
50 void MachineBranchProbabilityInfo::anchor() {}
51 
53  const MachineBasicBlock *Src,
55  return Src->getSuccProbability(Dst);
56 }
57 
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 
66  const MachineBasicBlock *Src, const MachineBasicBlock *Dst) const {
67  BranchProbability HotProb(StaticLikelyProb, 100);
68  return getEdgeProbability(Src, Dst) > HotProb;
69 }
70 
73  auto MaxProb = BranchProbability::getZero();
74  MachineBasicBlock *MaxSucc = nullptr;
76  E = MBB->succ_end(); I != E; ++I) {
77  auto Prob = getEdgeProbability(MBB, I);
78  if (Prob > MaxProb) {
79  MaxProb = Prob;
80  MaxSucc = *I;
81  }
82  }
83 
84  BranchProbability HotProb(StaticLikelyProb, 100);
85  if (getEdgeProbability(MBB, MaxSucc) >= HotProb)
86  return MaxSucc;
87 
88  return nullptr;
89 }
90 
92  raw_ostream &OS, const MachineBasicBlock *Src,
93  const MachineBasicBlock *Dst) const {
94 
95  const BranchProbability Prob = getEdgeProbability(Src, Dst);
96  OS << "edge " << printMBBReference(*Src) << " -> " << printMBBReference(*Dst)
97  << " probability is " << Prob
98  << (isEdgeHot(Src, Dst) ? " [HOT edge]\n" : "\n");
99 
100  return OS;
101 }
llvm::MachineBranchProbabilityInfo::MachineBranchProbabilityInfo
MachineBranchProbabilityInfo()
Definition: MachineBranchProbabilityInfo.cpp:44
llvm
Definition: AllocatorList.h:23
llvm::ImmutablePass
ImmutablePass class - This class is used to provide information that does not need to be run.
Definition: Pass.h:269
llvm::printMBBReference
Printable printMBBReference(const MachineBasicBlock &MBB)
Prints a machine basic block reference.
Definition: MachineBasicBlock.cpp:118
llvm::MachineBranchProbabilityInfo::ID
static char ID
Definition: MachineBranchProbabilityInfo.h:36
MachineBasicBlock.h
llvm::cl::Hidden
@ Hidden
Definition: CommandLine.h:143
llvm::MachineBranchProbabilityInfo::getHotSucc
MachineBasicBlock * getHotSucc(MachineBasicBlock *MBB) const
Definition: MachineBranchProbabilityInfo.cpp:72
llvm::BranchProbability::getZero
static BranchProbability getZero()
Definition: BranchProbability.h:49
llvm::initializeMachineBranchProbabilityInfoPass
void initializeMachineBranchProbabilityInfoPass(PassRegistry &)
CommandLine.h
llvm::MachineBranchProbabilityInfo::printEdgeProbability
raw_ostream & printEdgeProbability(raw_ostream &OS, const MachineBasicBlock *Src, const MachineBasicBlock *Dst) const
Definition: MachineBranchProbabilityInfo.cpp:91
llvm::PassRegistry::getPassRegistry
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
Definition: PassRegistry.cpp:31
llvm::MachineBranchProbabilityInfo
Definition: MachineBranchProbabilityInfo.h:24
E
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
llvm::MachineBasicBlock::succ_end
succ_iterator succ_end()
Definition: MachineBasicBlock.h:334
false
Definition: StackSlotColoring.cpp:142
llvm::PassRegistry
PassRegistry - This class manages the registration and intitialization of the pass subsystem as appli...
Definition: PassRegistry.h:38
llvm::StaticLikelyProb
cl::opt< unsigned > StaticLikelyProb
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:50
llvm::ProfileLikelyProb
cl::opt< unsigned > ProfileLikelyProb
llvm::MachineBasicBlock
Definition: MachineBasicBlock.h:95
INITIALIZE_PASS_END
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:58
llvm::cl::opt
Definition: CommandLine.h:1422
prob
machine branch prob
Definition: MachineBranchProbabilityInfo.cpp:25
llvm::find
auto find(R &&Range, const T &Val)
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1502
I
#define I(x, y, z)
Definition: MD5.cpp:59
llvm::cl::init
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:443
MachineBranchProbabilityInfo.h
llvm::MachineBasicBlock::succ_begin
succ_iterator succ_begin()
Definition: MachineBasicBlock.h:332
llvm::MachineBranchProbabilityInfo::getEdgeProbability
BranchProbability getEdgeProbability(const MachineBasicBlock *Src, const MachineBasicBlock *Dst) const
Definition: MachineBranchProbabilityInfo.cpp:58
llvm::MachineBasicBlock::successors
iterator_range< succ_iterator > successors()
Definition: MachineBasicBlock.h:355
llvm::BranchProbability
Definition: BranchProbability.h:30
MBB
MachineBasicBlock & MBB
Definition: AArch64SLSHardening.cpp:74
llvm::MCID::Branch
@ Branch
Definition: MCInstrDesc.h:156
Instructions.h
llvm::Registry
A global registry used in conjunction with static constructors to make pluggable components (like tar...
Definition: Registry.h:44
Machine
COFF::MachineTypes Machine
Definition: COFFYAML.cpp:365
llvm::MachineBasicBlock::const_succ_iterator
std::vector< MachineBasicBlock * >::const_iterator const_succ_iterator
Definition: MachineBasicBlock.h:307
llvm::cl::desc
Definition: CommandLine.h:414
raw_ostream.h
true
basic Basic Alias true
Definition: BasicAliasAnalysis.cpp:1797
INITIALIZE_PASS_BEGIN
INITIALIZE_PASS_BEGIN(MachineBranchProbabilityInfo, "machine-branch-prob", "Machine Branch Probability Analysis", false, true) INITIALIZE_PASS_END(MachineBranchProbabilityInfo
InitializePasses.h
Debug.h
machine
coro Split coroutine into a set of functions driving its state machine
Definition: CoroSplit.cpp:2246
llvm::MachineBranchProbabilityInfo::isEdgeHot
bool isEdgeHot(const MachineBasicBlock *Src, const MachineBasicBlock *Dst) const
Definition: MachineBranchProbabilityInfo.cpp:65
llvm::Intrinsic::ID
unsigned ID
Definition: TargetTransformInfo.h:38