LLVM  9.0.0svn
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/Support/Debug.h"
18 
19 using namespace llvm;
20 
22  "Machine Branch Probability Analysis", false, true)
24  "Machine Branch Probability Analysis", false, true)
25 
26 cl::opt<unsigned>
27  StaticLikelyProb("static-likely-prob",
28  cl::desc("branch probability threshold in percentage"
29  "to be considered very likely"),
30  cl::init(80), cl::Hidden);
31 
32 cl::opt<unsigned> ProfileLikelyProb(
33  "profile-likely-prob",
34  cl::desc("branch probability threshold in percentage to be considered"
35  " very likely when profile is available"),
36  cl::init(51), cl::Hidden);
37 
38 char MachineBranchProbabilityInfo::ID = 0;
39 
40 void MachineBranchProbabilityInfo::anchor() {}
41 
43  const MachineBasicBlock *Src,
45  return Src->getSuccProbability(Dst);
46 }
47 
49  const MachineBasicBlock *Src, const MachineBasicBlock *Dst) const {
50  // This is a linear search. Try to use the const_succ_iterator version when
51  // possible.
52  return getEdgeProbability(Src, find(Src->successors(), Dst));
53 }
54 
56  const MachineBasicBlock *Src, const MachineBasicBlock *Dst) const {
57  BranchProbability HotProb(StaticLikelyProb, 100);
58  return getEdgeProbability(Src, Dst) > HotProb;
59 }
60 
63  auto MaxProb = BranchProbability::getZero();
64  MachineBasicBlock *MaxSucc = nullptr;
66  E = MBB->succ_end(); I != E; ++I) {
67  auto Prob = getEdgeProbability(MBB, I);
68  if (Prob > MaxProb) {
69  MaxProb = Prob;
70  MaxSucc = *I;
71  }
72  }
73 
74  BranchProbability HotProb(StaticLikelyProb, 100);
75  if (getEdgeProbability(MBB, MaxSucc) >= HotProb)
76  return MaxSucc;
77 
78  return nullptr;
79 }
80 
82  raw_ostream &OS, const MachineBasicBlock *Src,
83  const MachineBasicBlock *Dst) const {
84 
85  const BranchProbability Prob = getEdgeProbability(Src, Dst);
86  OS << "edge " << printMBBReference(*Src) << " -> " << printMBBReference(*Dst)
87  << " probability is " << Prob
88  << (isEdgeHot(Src, Dst) ? " [HOT edge]\n" : "\n");
89 
90  return OS;
91 }
This class represents lattice values for constants.
Definition: AllocatorList.h:23
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()
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:427
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:1206
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:45
hexagon cext opt
static BranchProbability getZero()