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 | |
| 27 | char MachineBranchProbabilityInfo::ID = 0; |
| 28 | |
David Blaikie | 2d24e2a | 2011-12-20 02:50:00 +0000 | [diff] [blame] | 29 | void MachineBranchProbabilityInfo::anchor() { } |
| 30 | |
Jakub Staszak | 7cc2b07 | 2011-06-16 20:22:37 +0000 | [diff] [blame] | 31 | uint32_t MachineBranchProbabilityInfo:: |
Jakub Staszak | 25101bb | 2011-12-20 20:03:10 +0000 | [diff] [blame] | 32 | getSumForBlock(const MachineBasicBlock *MBB, uint32_t &Scale) const { |
Chandler Carruth | 2770c14 | 2011-11-14 08:50:16 +0000 | [diff] [blame] | 33 | // First we compute the sum with 64-bits of precision, ensuring that cannot |
| 34 | // overflow by bounding the number of weights considered. Hopefully no one |
| 35 | // actually needs 2^32 successors. |
| 36 | assert(MBB->succ_size() < UINT32_MAX); |
| 37 | uint64_t Sum = 0; |
| 38 | Scale = 1; |
Jakub Staszak | 7cc2b07 | 2011-06-16 20:22:37 +0000 | [diff] [blame] | 39 | for (MachineBasicBlock::const_succ_iterator I = MBB->succ_begin(), |
| 40 | E = MBB->succ_end(); I != E; ++I) { |
Jakob Stoklund Olesen | 990ca55 | 2012-08-20 22:01:38 +0000 | [diff] [blame] | 41 | uint32_t Weight = getEdgeWeight(MBB, I); |
Jakub Staszak | 7cc2b07 | 2011-06-16 20:22:37 +0000 | [diff] [blame] | 42 | Sum += Weight; |
Jakub Staszak | 7cc2b07 | 2011-06-16 20:22:37 +0000 | [diff] [blame] | 43 | } |
| 44 | |
Chandler Carruth | 2770c14 | 2011-11-14 08:50:16 +0000 | [diff] [blame] | 45 | // If the computed sum fits in 32-bits, we're done. |
| 46 | if (Sum <= UINT32_MAX) |
| 47 | return Sum; |
| 48 | |
| 49 | // Otherwise, compute the scale necessary to cause the weights to fit, and |
| 50 | // re-sum with that scale applied. |
| 51 | assert((Sum / UINT32_MAX) < UINT32_MAX); |
| 52 | Scale = (Sum / UINT32_MAX) + 1; |
| 53 | Sum = 0; |
| 54 | for (MachineBasicBlock::const_succ_iterator I = MBB->succ_begin(), |
| 55 | E = MBB->succ_end(); I != E; ++I) { |
Jakob Stoklund Olesen | 990ca55 | 2012-08-20 22:01:38 +0000 | [diff] [blame] | 56 | uint32_t Weight = getEdgeWeight(MBB, I); |
Chandler Carruth | 2770c14 | 2011-11-14 08:50:16 +0000 | [diff] [blame] | 57 | Sum += Weight / Scale; |
| 58 | } |
| 59 | assert(Sum <= UINT32_MAX); |
Jakub Staszak | 7cc2b07 | 2011-06-16 20:22:37 +0000 | [diff] [blame] | 60 | return Sum; |
| 61 | } |
| 62 | |
Jakob Stoklund Olesen | 990ca55 | 2012-08-20 22:01:38 +0000 | [diff] [blame] | 63 | uint32_t MachineBranchProbabilityInfo:: |
| 64 | getEdgeWeight(const MachineBasicBlock *Src, |
| 65 | MachineBasicBlock::const_succ_iterator Dst) const { |
Jakub Staszak | 7cc2b07 | 2011-06-16 20:22:37 +0000 | [diff] [blame] | 66 | uint32_t Weight = Src->getSuccWeight(Dst); |
| 67 | if (!Weight) |
| 68 | return DEFAULT_WEIGHT; |
| 69 | return Weight; |
| 70 | } |
| 71 | |
Jakob Stoklund Olesen | 990ca55 | 2012-08-20 22:01:38 +0000 | [diff] [blame] | 72 | uint32_t MachineBranchProbabilityInfo:: |
| 73 | getEdgeWeight(const MachineBasicBlock *Src, |
| 74 | const MachineBasicBlock *Dst) const { |
| 75 | // This is a linear search. Try to use the const_succ_iterator version when |
| 76 | // possible. |
| 77 | return getEdgeWeight(Src, std::find(Src->succ_begin(), Src->succ_end(), Dst)); |
| 78 | } |
| 79 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame^] | 80 | bool |
| 81 | MachineBranchProbabilityInfo::isEdgeHot(const MachineBasicBlock *Src, |
| 82 | const MachineBasicBlock *Dst) const { |
Jakub Staszak | 7cc2b07 | 2011-06-16 20:22:37 +0000 | [diff] [blame] | 83 | // Hot probability is at least 4/5 = 80% |
Benjamin Kramer | 91bbe23 | 2011-10-28 11:14:31 +0000 | [diff] [blame] | 84 | // FIXME: Compare against a static "hot" BranchProbability. |
| 85 | return getEdgeProbability(Src, Dst) > BranchProbability(4, 5); |
Jakub Staszak | 7cc2b07 | 2011-06-16 20:22:37 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | MachineBasicBlock * |
| 89 | MachineBranchProbabilityInfo::getHotSucc(MachineBasicBlock *MBB) const { |
Jakub Staszak | 7cc2b07 | 2011-06-16 20:22:37 +0000 | [diff] [blame] | 90 | uint32_t MaxWeight = 0; |
| 91 | MachineBasicBlock *MaxSucc = 0; |
Jakub Staszak | 7cc2b07 | 2011-06-16 20:22:37 +0000 | [diff] [blame] | 92 | for (MachineBasicBlock::const_succ_iterator I = MBB->succ_begin(), |
| 93 | E = MBB->succ_end(); I != E; ++I) { |
Jakob Stoklund Olesen | 990ca55 | 2012-08-20 22:01:38 +0000 | [diff] [blame] | 94 | uint32_t Weight = getEdgeWeight(MBB, I); |
Jakub Staszak | 7cc2b07 | 2011-06-16 20:22:37 +0000 | [diff] [blame] | 95 | if (Weight > MaxWeight) { |
| 96 | MaxWeight = Weight; |
Chandler Carruth | c4e1562 | 2011-11-14 08:55:59 +0000 | [diff] [blame] | 97 | MaxSucc = *I; |
Jakub Staszak | 7cc2b07 | 2011-06-16 20:22:37 +0000 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | |
Chandler Carruth | c4e1562 | 2011-11-14 08:55:59 +0000 | [diff] [blame] | 101 | if (getEdgeProbability(MBB, MaxSucc) >= BranchProbability(4, 5)) |
Jakub Staszak | 7cc2b07 | 2011-06-16 20:22:37 +0000 | [diff] [blame] | 102 | return MaxSucc; |
| 103 | |
| 104 | return 0; |
| 105 | } |
| 106 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame^] | 107 | BranchProbability MachineBranchProbabilityInfo::getEdgeProbability( |
| 108 | const MachineBasicBlock *Src, const MachineBasicBlock *Dst) const { |
Chandler Carruth | 2770c14 | 2011-11-14 08:50:16 +0000 | [diff] [blame] | 109 | uint32_t Scale = 1; |
| 110 | uint32_t D = getSumForBlock(Src, Scale); |
| 111 | uint32_t N = getEdgeWeight(Src, Dst) / Scale; |
Jakub Staszak | 7cc2b07 | 2011-06-16 20:22:37 +0000 | [diff] [blame] | 112 | |
| 113 | return BranchProbability(N, D); |
| 114 | } |
| 115 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame^] | 116 | raw_ostream &MachineBranchProbabilityInfo::printEdgeProbability( |
| 117 | raw_ostream &OS, const MachineBasicBlock *Src, |
| 118 | const MachineBasicBlock *Dst) const { |
Jakub Staszak | 7cc2b07 | 2011-06-16 20:22:37 +0000 | [diff] [blame] | 119 | |
| 120 | const BranchProbability Prob = getEdgeProbability(Src, Dst); |
| 121 | OS << "edge MBB#" << Src->getNumber() << " -> MBB#" << Dst->getNumber() |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame^] | 122 | << " probability is " << Prob |
Jakub Staszak | 7cc2b07 | 2011-06-16 20:22:37 +0000 | [diff] [blame] | 123 | << (isEdgeHot(Src, Dst) ? " [HOT edge]\n" : "\n"); |
| 124 | |
| 125 | return OS; |
| 126 | } |