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
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"
18
19using namespace llvm;
20
21INITIALIZE_PASS_BEGIN(MachineBranchProbabilityInfo, "machine-branch-prob",
22                      "Machine Branch Probability Analysis", false, true)
23INITIALIZE_PASS_END(MachineBranchProbabilityInfo, "machine-branch-prob",
24                    "Machine Branch Probability Analysis", false, true)
25
26namespace llvm {
27cl::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
33cl::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} // namespace llvm
39
40char MachineBranchProbabilityInfo::ID = 0;
41
42MachineBranchProbabilityInfo::MachineBranchProbabilityInfo()
43    : ImmutablePass(ID) {
44  PassRegistry &Registry = *PassRegistry::getPassRegistry();
45  initializeMachineBranchProbabilityInfoPass(Registry);
46}
47
48void MachineBranchProbabilityInfo::anchor() {}
49
50BranchProbability MachineBranchProbabilityInfo::getEdgeProbability(
51    const MachineBasicBlock *Src,
52    MachineBasicBlock::const_succ_iterator Dst) const {
53  return Src->getSuccProbability(Dst);
54}
55
56BranchProbability 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
59  // possible.
60  return getEdgeProbability(Src, find(Src->successors(), Dst));
61}
62
63bool MachineBranchProbabilityInfo::isEdgeHot(
64    const MachineBasicBlock *Src, const MachineBasicBlock *Dst) const {
65  BranchProbability HotProb(StaticLikelyProb, 100);
66  return getEdgeProbability(Src, Dst) > HotProb;
67}
68
69raw_ostream &MachineBranchProbabilityInfo::printEdgeProbability(
70    raw_ostream &OS, const MachineBasicBlock *Src,
71    const MachineBasicBlock *Dst) const {
72
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");
77
78  return OS;
79}
80