blob: 21eff9dfff9c9b253ab1cfd84ffb4a00489f7820 [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.
David Majnemer42531262016-08-12 03:55:06 +000053 return getEdgeProbability(Src, find(Src->successors(), Dst));
Cong Houd97c1002015-12-01 05:29:22 +000054}
55
Xinliang David Liff287372016-06-03 23:48:36 +000056bool MachineBranchProbabilityInfo::isEdgeHot(
57 const MachineBasicBlock *Src, const MachineBasicBlock *Dst) const {
58 BranchProbability HotProb(StaticLikelyProb, 100);
Cong Houd97c1002015-12-01 05:29:22 +000059 return getEdgeProbability(Src, Dst) > HotProb;
Jakub Staszak12a43bd2011-06-16 20:22:37 +000060}
61
62MachineBasicBlock *
63MachineBranchProbabilityInfo::getHotSucc(MachineBasicBlock *MBB) const {
Cong Houd97c1002015-12-01 05:29:22 +000064 auto MaxProb = BranchProbability::getZero();
Craig Topperc0196b12014-04-14 00:51:57 +000065 MachineBasicBlock *MaxSucc = nullptr;
Jakub Staszak12a43bd2011-06-16 20:22:37 +000066 for (MachineBasicBlock::const_succ_iterator I = MBB->succ_begin(),
67 E = MBB->succ_end(); I != E; ++I) {
Cong Houd97c1002015-12-01 05:29:22 +000068 auto Prob = getEdgeProbability(MBB, I);
69 if (Prob > MaxProb) {
70 MaxProb = Prob;
Chandler Carrutha9e71fa2011-11-14 08:55:59 +000071 MaxSucc = *I;
Jakub Staszak12a43bd2011-06-16 20:22:37 +000072 }
73 }
74
Xinliang David Liff287372016-06-03 23:48:36 +000075 BranchProbability HotProb(StaticLikelyProb, 100);
Cong Houd97c1002015-12-01 05:29:22 +000076 if (getEdgeProbability(MBB, MaxSucc) >= HotProb)
Jakub Staszak12a43bd2011-06-16 20:22:37 +000077 return MaxSucc;
78
Craig Topperc0196b12014-04-14 00:51:57 +000079 return nullptr;
Jakub Staszak12a43bd2011-06-16 20:22:37 +000080}
81
Duncan P. N. Exon Smith936aef92014-03-25 18:01:32 +000082raw_ostream &MachineBranchProbabilityInfo::printEdgeProbability(
83 raw_ostream &OS, const MachineBasicBlock *Src,
84 const MachineBasicBlock *Dst) const {
Jakub Staszak12a43bd2011-06-16 20:22:37 +000085
86 const BranchProbability Prob = getEdgeProbability(Src, Dst);
87 OS << "edge MBB#" << Src->getNumber() << " -> MBB#" << Dst->getNumber()
Duncan P. N. Exon Smith936aef92014-03-25 18:01:32 +000088 << " probability is " << Prob
Jakub Staszak12a43bd2011-06-16 20:22:37 +000089 << (isEdgeHot(Src, Dst) ? " [HOT edge]\n" : "\n");
90
91 return OS;
92}