1 //===- MachineBranchProbabilityInfo.cpp - Machine Branch Probability Info -===//
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
7 //===----------------------------------------------------------------------===//
9 // This analysis uses probability info stored in Machine Basic Blocks.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
14 #include "llvm/CodeGen/MachineBasicBlock.h"
15 #include "llvm/InitializePasses.h"
16 #include "llvm/Support/CommandLine.h"
17 #include "llvm/Support/raw_ostream.h"
21 INITIALIZE_PASS_BEGIN(MachineBranchProbabilityInfo
, "machine-branch-prob",
22 "Machine Branch Probability Analysis", false, true)
23 INITIALIZE_PASS_END(MachineBranchProbabilityInfo
, "machine-branch-prob",
24 "Machine Branch Probability Analysis", false, true)
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
);
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
);
40 char MachineBranchProbabilityInfo::ID
= 0;
42 MachineBranchProbabilityInfo::MachineBranchProbabilityInfo()
44 PassRegistry
&Registry
= *PassRegistry::getPassRegistry();
45 initializeMachineBranchProbabilityInfoPass(Registry
);
48 void MachineBranchProbabilityInfo::anchor() {}
50 BranchProbability
MachineBranchProbabilityInfo::getEdgeProbability(
51 const MachineBasicBlock
*Src
,
52 MachineBasicBlock::const_succ_iterator Dst
) const {
53 return Src
->getSuccProbability(Dst
);
56 BranchProbability
MachineBranchProbabilityInfo::getEdgeProbability(
57 const MachineBasicBlock
*Src
, const MachineBasicBlock
*Dst
) const {
58 // This is a linear search. Try to use the const_succ_iterator version when
60 return getEdgeProbability(Src
, find(Src
->successors(), Dst
));
63 bool MachineBranchProbabilityInfo::isEdgeHot(
64 const MachineBasicBlock
*Src
, const MachineBasicBlock
*Dst
) const {
65 BranchProbability
HotProb(StaticLikelyProb
, 100);
66 return getEdgeProbability(Src
, Dst
) > HotProb
;
69 raw_ostream
&MachineBranchProbabilityInfo::printEdgeProbability(
70 raw_ostream
&OS
, const MachineBasicBlock
*Src
,
71 const MachineBasicBlock
*Dst
) const {
73 const BranchProbability Prob
= getEdgeProbability(Src
, Dst
);
74 OS
<< "edge " << printMBBReference(*Src
) << " -> " << printMBBReference(*Dst
)
75 << " probability is " << Prob
76 << (isEdgeHot(Src
, Dst
) ? " [HOT edge]\n" : "\n");