blob: e4952aaaba06fbf384d0ccd2e3e3886ed9865710 [file] [log] [blame]
Jakub Staszak7cc2b072011-06-16 20:22:37 +00001//===- 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
Jakub Staszak7cc2b072011-06-16 20:22:37 +000014#include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
15#include "llvm/CodeGen/MachineBasicBlock.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000016#include "llvm/IR/Instructions.h"
Jakub Staszak7cc2b072011-06-16 20:22:37 +000017#include "llvm/Support/Debug.h"
18#include "llvm/Support/raw_ostream.h"
19
20using namespace llvm;
21
22INITIALIZE_PASS_BEGIN(MachineBranchProbabilityInfo, "machine-branch-prob",
23 "Machine Branch Probability Analysis", false, true)
24INITIALIZE_PASS_END(MachineBranchProbabilityInfo, "machine-branch-prob",
25 "Machine Branch Probability Analysis", false, true)
26
Xinliang David Lib2ccb9b2016-06-15 03:03:30 +000027cl::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);
Xinliang David Li670f8e52016-06-03 23:48:36 +000032
Dehao Chen97615522016-06-14 22:27:17 +000033cl::opt<unsigned> ProfileLikelyProb(
34 "profile-likely-prob",
Xinliang David Lib2ccb9b2016-06-15 03:03:30 +000035 cl::desc("branch probability threshold in percentage to be considered"
36 " very likely when profile is available"),
Dehao Chen97615522016-06-14 22:27:17 +000037 cl::init(51), cl::Hidden);
38
Jakub Staszak7cc2b072011-06-16 20:22:37 +000039char MachineBranchProbabilityInfo::ID = 0;
40
Xinliang David Li670f8e52016-06-03 23:48:36 +000041void MachineBranchProbabilityInfo::anchor() {}
David Blaikie2d24e2a2011-12-20 02:50:00 +000042
Cong Hou51550212015-12-01 05:29:22 +000043BranchProbability MachineBranchProbabilityInfo::getEdgeProbability(
44 const MachineBasicBlock *Src,
45 MachineBasicBlock::const_succ_iterator Dst) const {
46 return Src->getSuccProbability(Dst);
47}
48
49BranchProbability MachineBranchProbabilityInfo::getEdgeProbability(
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.
David Majnemer2d62ce62016-08-12 03:55:06 +000053 return getEdgeProbability(Src, find(Src->successors(), Dst));
Cong Hou51550212015-12-01 05:29:22 +000054}
55
Xinliang David Li670f8e52016-06-03 23:48:36 +000056bool MachineBranchProbabilityInfo::isEdgeHot(
57 const MachineBasicBlock *Src, const MachineBasicBlock *Dst) const {
58 BranchProbability HotProb(StaticLikelyProb, 100);
Cong Hou51550212015-12-01 05:29:22 +000059 return getEdgeProbability(Src, Dst) > HotProb;
Jakub Staszak7cc2b072011-06-16 20:22:37 +000060}
61
62MachineBasicBlock *
63MachineBranchProbabilityInfo::getHotSucc(MachineBasicBlock *MBB) const {
Cong Hou51550212015-12-01 05:29:22 +000064 auto MaxProb = BranchProbability::getZero();
Craig Topper4ba84432014-04-14 00:51:57 +000065 MachineBasicBlock *MaxSucc = nullptr;
Jakub Staszak7cc2b072011-06-16 20:22:37 +000066 for (MachineBasicBlock::const_succ_iterator I = MBB->succ_begin(),
67 E = MBB->succ_end(); I != E; ++I) {
Cong Hou51550212015-12-01 05:29:22 +000068 auto Prob = getEdgeProbability(MBB, I);
69 if (Prob > MaxProb) {
70 MaxProb = Prob;
Chandler Carruthc4e15622011-11-14 08:55:59 +000071 MaxSucc = *I;
Jakub Staszak7cc2b072011-06-16 20:22:37 +000072 }
73 }
74
Xinliang David Li670f8e52016-06-03 23:48:36 +000075 BranchProbability HotProb(StaticLikelyProb, 100);
Cong Hou51550212015-12-01 05:29:22 +000076 if (getEdgeProbability(MBB, MaxSucc) >= HotProb)
Jakub Staszak7cc2b072011-06-16 20:22:37 +000077 return MaxSucc;
78
Craig Topper4ba84432014-04-14 00:51:57 +000079 return nullptr;
Jakub Staszak7cc2b072011-06-16 20:22:37 +000080}
81
Duncan P. N. Exon Smith8451e1b2014-03-25 18:01:32 +000082raw_ostream &MachineBranchProbabilityInfo::printEdgeProbability(
83 raw_ostream &OS, const MachineBasicBlock *Src,
84 const MachineBasicBlock *Dst) const {
Jakub Staszak7cc2b072011-06-16 20:22:37 +000085
86 const BranchProbability Prob = getEdgeProbability(Src, Dst);
Francis Visoiu Mistrihca0df552017-12-04 17:18:51 +000087 OS << "edge " << printMBBReference(*Src) << " -> " << printMBBReference(*Dst)
Duncan P. N. Exon Smith8451e1b2014-03-25 18:01:32 +000088 << " probability is " << Prob
Jakub Staszak7cc2b072011-06-16 20:22:37 +000089 << (isEdgeHot(Src, Dst) ? " [HOT edge]\n" : "\n");
90
91 return OS;
92}