Jakub Staszak | 7cc2b07 | 2011-06-16 20:22:37 +0000 | [diff] [blame] | 1 | //===- 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 Staszak | 7cc2b07 | 2011-06-16 20:22:37 +0000 | [diff] [blame] | 14 | #include "llvm/CodeGen/MachineBranchProbabilityInfo.h" |
| 15 | #include "llvm/CodeGen/MachineBasicBlock.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 16 | #include "llvm/IR/Instructions.h" |
Jakub Staszak | 7cc2b07 | 2011-06-16 20:22:37 +0000 | [diff] [blame] | 17 | #include "llvm/Support/Debug.h" |
| 18 | #include "llvm/Support/raw_ostream.h" |
| 19 | |
| 20 | using namespace llvm; |
| 21 | |
| 22 | INITIALIZE_PASS_BEGIN(MachineBranchProbabilityInfo, "machine-branch-prob", |
| 23 | "Machine Branch Probability Analysis", false, true) |
| 24 | INITIALIZE_PASS_END(MachineBranchProbabilityInfo, "machine-branch-prob", |
| 25 | "Machine Branch Probability Analysis", false, true) |
| 26 | |
Xinliang David Li | b2ccb9b | 2016-06-15 03:03:30 +0000 | [diff] [blame] | 27 | cl::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 Li | 670f8e5 | 2016-06-03 23:48:36 +0000 | [diff] [blame] | 32 | |
Dehao Chen | 9761552 | 2016-06-14 22:27:17 +0000 | [diff] [blame] | 33 | cl::opt<unsigned> ProfileLikelyProb( |
| 34 | "profile-likely-prob", |
Xinliang David Li | b2ccb9b | 2016-06-15 03:03:30 +0000 | [diff] [blame] | 35 | cl::desc("branch probability threshold in percentage to be considered" |
| 36 | " very likely when profile is available"), |
Dehao Chen | 9761552 | 2016-06-14 22:27:17 +0000 | [diff] [blame] | 37 | cl::init(51), cl::Hidden); |
| 38 | |
Jakub Staszak | 7cc2b07 | 2011-06-16 20:22:37 +0000 | [diff] [blame] | 39 | char MachineBranchProbabilityInfo::ID = 0; |
| 40 | |
Xinliang David Li | 670f8e5 | 2016-06-03 23:48:36 +0000 | [diff] [blame] | 41 | void MachineBranchProbabilityInfo::anchor() {} |
David Blaikie | 2d24e2a | 2011-12-20 02:50:00 +0000 | [diff] [blame] | 42 | |
Cong Hou | 5155021 | 2015-12-01 05:29:22 +0000 | [diff] [blame] | 43 | BranchProbability MachineBranchProbabilityInfo::getEdgeProbability( |
| 44 | const MachineBasicBlock *Src, |
| 45 | MachineBasicBlock::const_succ_iterator Dst) const { |
| 46 | return Src->getSuccProbability(Dst); |
| 47 | } |
| 48 | |
| 49 | BranchProbability 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 Majnemer | 2d62ce6 | 2016-08-12 03:55:06 +0000 | [diff] [blame] | 53 | return getEdgeProbability(Src, find(Src->successors(), Dst)); |
Cong Hou | 5155021 | 2015-12-01 05:29:22 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Xinliang David Li | 670f8e5 | 2016-06-03 23:48:36 +0000 | [diff] [blame] | 56 | bool MachineBranchProbabilityInfo::isEdgeHot( |
| 57 | const MachineBasicBlock *Src, const MachineBasicBlock *Dst) const { |
| 58 | BranchProbability HotProb(StaticLikelyProb, 100); |
Cong Hou | 5155021 | 2015-12-01 05:29:22 +0000 | [diff] [blame] | 59 | return getEdgeProbability(Src, Dst) > HotProb; |
Jakub Staszak | 7cc2b07 | 2011-06-16 20:22:37 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | MachineBasicBlock * |
| 63 | MachineBranchProbabilityInfo::getHotSucc(MachineBasicBlock *MBB) const { |
Cong Hou | 5155021 | 2015-12-01 05:29:22 +0000 | [diff] [blame] | 64 | auto MaxProb = BranchProbability::getZero(); |
Craig Topper | 4ba8443 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 65 | MachineBasicBlock *MaxSucc = nullptr; |
Jakub Staszak | 7cc2b07 | 2011-06-16 20:22:37 +0000 | [diff] [blame] | 66 | for (MachineBasicBlock::const_succ_iterator I = MBB->succ_begin(), |
| 67 | E = MBB->succ_end(); I != E; ++I) { |
Cong Hou | 5155021 | 2015-12-01 05:29:22 +0000 | [diff] [blame] | 68 | auto Prob = getEdgeProbability(MBB, I); |
| 69 | if (Prob > MaxProb) { |
| 70 | MaxProb = Prob; |
Chandler Carruth | c4e1562 | 2011-11-14 08:55:59 +0000 | [diff] [blame] | 71 | MaxSucc = *I; |
Jakub Staszak | 7cc2b07 | 2011-06-16 20:22:37 +0000 | [diff] [blame] | 72 | } |
| 73 | } |
| 74 | |
Xinliang David Li | 670f8e5 | 2016-06-03 23:48:36 +0000 | [diff] [blame] | 75 | BranchProbability HotProb(StaticLikelyProb, 100); |
Cong Hou | 5155021 | 2015-12-01 05:29:22 +0000 | [diff] [blame] | 76 | if (getEdgeProbability(MBB, MaxSucc) >= HotProb) |
Jakub Staszak | 7cc2b07 | 2011-06-16 20:22:37 +0000 | [diff] [blame] | 77 | return MaxSucc; |
| 78 | |
Craig Topper | 4ba8443 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 79 | return nullptr; |
Jakub Staszak | 7cc2b07 | 2011-06-16 20:22:37 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Duncan P. N. Exon Smith | 8451e1b | 2014-03-25 18:01:32 +0000 | [diff] [blame] | 82 | raw_ostream &MachineBranchProbabilityInfo::printEdgeProbability( |
| 83 | raw_ostream &OS, const MachineBasicBlock *Src, |
| 84 | const MachineBasicBlock *Dst) const { |
Jakub Staszak | 7cc2b07 | 2011-06-16 20:22:37 +0000 | [diff] [blame] | 85 | |
| 86 | const BranchProbability Prob = getEdgeProbability(Src, Dst); |
Francis Visoiu Mistrih | ca0df55 | 2017-12-04 17:18:51 +0000 | [diff] [blame] | 87 | OS << "edge " << printMBBReference(*Src) << " -> " << printMBBReference(*Dst) |
Duncan P. N. Exon Smith | 8451e1b | 2014-03-25 18:01:32 +0000 | [diff] [blame] | 88 | << " probability is " << Prob |
Jakub Staszak | 7cc2b07 | 2011-06-16 20:22:37 +0000 | [diff] [blame] | 89 | << (isEdgeHot(Src, Dst) ? " [HOT edge]\n" : "\n"); |
| 90 | |
| 91 | return OS; |
| 92 | } |