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