blob: fe73406183743e6fd0c19ed57b258bd0e017575d [file] [log] [blame]
Jakub Staszak12a43bd2011-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 Staszak12a43bd2011-06-16 20:22:37 +000014#include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
15#include "llvm/CodeGen/MachineBasicBlock.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000016#include "llvm/IR/Instructions.h"
Jakub Staszak12a43bd2011-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 Lie34ed832016-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 Liff287372016-06-03 23:48:36 +000032
Dehao Chen9f2bdfb2016-06-14 22:27:17 +000033cl::opt<unsigned> ProfileLikelyProb(
34 "profile-likely-prob",
Xinliang David Lie34ed832016-06-15 03:03:30 +000035 cl::desc("branch probability threshold in percentage to be considered"
36 " very likely when profile is available"),
Dehao Chen9f2bdfb2016-06-14 22:27:17 +000037 cl::init(51), cl::Hidden);
38
Jakub Staszak12a43bd2011-06-16 20:22:37 +000039char MachineBranchProbabilityInfo::ID = 0;
40
Xinliang David Liff287372016-06-03 23:48:36 +000041void MachineBranchProbabilityInfo::anchor() {}
David Blaikiea379b1812011-12-20 02:50:00 +000042
Cong Houd97c1002015-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.
53 return getEdgeProbability(Src,
54 std::find(Src->succ_begin(), Src->succ_end(), Dst));
55}
56
Xinliang David Liff287372016-06-03 23:48:36 +000057bool MachineBranchProbabilityInfo::isEdgeHot(
58 const MachineBasicBlock *Src, const MachineBasicBlock *Dst) const {
59 BranchProbability HotProb(StaticLikelyProb, 100);
Cong Houd97c1002015-12-01 05:29:22 +000060 return getEdgeProbability(Src, Dst) > HotProb;
Jakub Staszak12a43bd2011-06-16 20:22:37 +000061}
62
63MachineBasicBlock *
64MachineBranchProbabilityInfo::getHotSucc(MachineBasicBlock *MBB) const {
Cong Houd97c1002015-12-01 05:29:22 +000065 auto MaxProb = BranchProbability::getZero();
Craig Topperc0196b12014-04-14 00:51:57 +000066 MachineBasicBlock *MaxSucc = nullptr;
Jakub Staszak12a43bd2011-06-16 20:22:37 +000067 for (MachineBasicBlock::const_succ_iterator I = MBB->succ_begin(),
68 E = MBB->succ_end(); I != E; ++I) {
Cong Houd97c1002015-12-01 05:29:22 +000069 auto Prob = getEdgeProbability(MBB, I);
70 if (Prob > MaxProb) {
71 MaxProb = Prob;
Chandler Carrutha9e71fa2011-11-14 08:55:59 +000072 MaxSucc = *I;
Jakub Staszak12a43bd2011-06-16 20:22:37 +000073 }
74 }
75
Xinliang David Liff287372016-06-03 23:48:36 +000076 BranchProbability HotProb(StaticLikelyProb, 100);
Cong Houd97c1002015-12-01 05:29:22 +000077 if (getEdgeProbability(MBB, MaxSucc) >= HotProb)
Jakub Staszak12a43bd2011-06-16 20:22:37 +000078 return MaxSucc;
79
Craig Topperc0196b12014-04-14 00:51:57 +000080 return nullptr;
Jakub Staszak12a43bd2011-06-16 20:22:37 +000081}
82
Duncan P. N. Exon Smith936aef92014-03-25 18:01:32 +000083raw_ostream &MachineBranchProbabilityInfo::printEdgeProbability(
84 raw_ostream &OS, const MachineBasicBlock *Src,
85 const MachineBasicBlock *Dst) const {
Jakub Staszak12a43bd2011-06-16 20:22:37 +000086
87 const BranchProbability Prob = getEdgeProbability(Src, Dst);
88 OS << "edge MBB#" << Src->getNumber() << " -> MBB#" << Dst->getNumber()
Duncan P. N. Exon Smith936aef92014-03-25 18:01:32 +000089 << " probability is " << Prob
Jakub Staszak12a43bd2011-06-16 20:22:37 +000090 << (isEdgeHot(Src, Dst) ? " [HOT edge]\n" : "\n");
91
92 return OS;
93}