LLVM  8.0.1
MachineBranchProbabilityInfo.cpp
Go to the documentation of this file.
1 //===- MachineBranchProbabilityInfo.cpp - Machine Branch Probability Info -===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This analysis uses probability info stored in Machine Basic Blocks.
11 //
12 //===----------------------------------------------------------------------===//
13 
16 #include "llvm/IR/Instructions.h"
17 #include "llvm/Support/Debug.h"
19 
20 using namespace llvm;
21 
23  "Machine Branch Probability Analysis", false, true)
25  "Machine Branch Probability Analysis", false, true)
26 
27 cl::opt<unsigned>
28  StaticLikelyProb("static-likely-prob",
29  cl::desc("branch probability threshold in percentage"
30  "to be considered very likely"),
31  cl::init(80), cl::Hidden);
32 
33 cl::opt<unsigned> ProfileLikelyProb(
34  "profile-likely-prob",
35  cl::desc("branch probability threshold in percentage to be considered"
36  " very likely when profile is available"),
37  cl::init(51), cl::Hidden);
38 
39 char MachineBranchProbabilityInfo::ID = 0;
40 
41 void MachineBranchProbabilityInfo::anchor() {}
42 
44  const MachineBasicBlock *Src,
46  return Src->getSuccProbability(Dst);
47 }
48 
50  const MachineBasicBlock *Src, const MachineBasicBlock *Dst) const {
51  // This is a linear search. Try to use the const_succ_iterator version when
52  // possible.
53  return getEdgeProbability(Src, find(Src->successors(), Dst));
54 }
55 
57  const MachineBasicBlock *Src, const MachineBasicBlock *Dst) const {
58  BranchProbability HotProb(StaticLikelyProb, 100);
59  return getEdgeProbability(Src, Dst) > HotProb;
60 }
61 
64  auto MaxProb = BranchProbability::getZero();
65  MachineBasicBlock *MaxSucc = nullptr;
67  E = MBB->succ_end(); I != E; ++I) {
68  auto Prob = getEdgeProbability(MBB, I);
69  if (Prob > MaxProb) {
70  MaxProb = Prob;
71  MaxSucc = *I;
72  }
73  }
74 
75  BranchProbability HotProb(StaticLikelyProb, 100);
76  if (getEdgeProbability(MBB, MaxSucc) >= HotProb)
77  return MaxSucc;
78 
79  return nullptr;
80 }
81 
83  raw_ostream &OS, const MachineBasicBlock *Src,
84  const MachineBasicBlock *Dst) const {
85 
86  const BranchProbability Prob = getEdgeProbability(Src, Dst);
87  OS << "edge " << printMBBReference(*Src) << " -> " << printMBBReference(*Dst)
88  << " probability is " << Prob
89  << (isEdgeHot(Src, Dst) ? " [HOT edge]\n" : "\n");
90 
91  return OS;
92 }
This class represents lattice values for constants.
Definition: AllocatorList.h:24
machine branch prob
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()
machine branch Machine Branch Probability Analysis
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:363
return !LiveInRegUnits available(Reg)
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:423
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:1207
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE, "Assign register bank of generic virtual registers", false, false) RegBankSelect
BranchProbability getEdgeProbability(const MachineBasicBlock *Src, const MachineBasicBlock *Dst) const
pgo instr Read PGO instrumentation profile
#define I(x, y, z)
Definition: MD5.cpp:58
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
static BranchProbability getZero()