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 | |
| 14 | #include "llvm/Instructions.h" |
| 15 | #include "llvm/CodeGen/MachineBranchProbabilityInfo.h" |
| 16 | #include "llvm/CodeGen/MachineBasicBlock.h" |
| 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 | |
| 29 | uint32_t MachineBranchProbabilityInfo:: |
| 30 | getSumForBlock(MachineBasicBlock *MBB) const { |
| 31 | uint32_t Sum = 0; |
| 32 | |
| 33 | for (MachineBasicBlock::const_succ_iterator I = MBB->succ_begin(), |
| 34 | E = MBB->succ_end(); I != E; ++I) { |
| 35 | MachineBasicBlock *Succ = *I; |
| 36 | uint32_t Weight = getEdgeWeight(MBB, Succ); |
| 37 | uint32_t PrevSum = Sum; |
| 38 | |
| 39 | Sum += Weight; |
| 40 | assert(Sum > PrevSum); (void) PrevSum; |
| 41 | } |
| 42 | |
| 43 | return Sum; |
| 44 | } |
| 45 | |
| 46 | uint32_t |
| 47 | MachineBranchProbabilityInfo::getEdgeWeight(MachineBasicBlock *Src, |
| 48 | MachineBasicBlock *Dst) const { |
| 49 | uint32_t Weight = Src->getSuccWeight(Dst); |
| 50 | if (!Weight) |
| 51 | return DEFAULT_WEIGHT; |
| 52 | return Weight; |
| 53 | } |
| 54 | |
| 55 | bool MachineBranchProbabilityInfo::isEdgeHot(MachineBasicBlock *Src, |
| 56 | MachineBasicBlock *Dst) const { |
| 57 | // Hot probability is at least 4/5 = 80% |
Benjamin Kramer | 91bbe23 | 2011-10-28 11:14:31 +0000 | [diff] [blame^] | 58 | // FIXME: Compare against a static "hot" BranchProbability. |
| 59 | return getEdgeProbability(Src, Dst) > BranchProbability(4, 5); |
Jakub Staszak | 7cc2b07 | 2011-06-16 20:22:37 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | MachineBasicBlock * |
| 63 | MachineBranchProbabilityInfo::getHotSucc(MachineBasicBlock *MBB) const { |
| 64 | uint32_t Sum = 0; |
| 65 | uint32_t MaxWeight = 0; |
| 66 | MachineBasicBlock *MaxSucc = 0; |
| 67 | |
| 68 | for (MachineBasicBlock::const_succ_iterator I = MBB->succ_begin(), |
| 69 | E = MBB->succ_end(); I != E; ++I) { |
| 70 | MachineBasicBlock *Succ = *I; |
| 71 | uint32_t Weight = getEdgeWeight(MBB, Succ); |
| 72 | uint32_t PrevSum = Sum; |
| 73 | |
| 74 | Sum += Weight; |
| 75 | assert(Sum > PrevSum); (void) PrevSum; |
| 76 | |
| 77 | if (Weight > MaxWeight) { |
| 78 | MaxWeight = Weight; |
| 79 | MaxSucc = Succ; |
| 80 | } |
| 81 | } |
| 82 | |
Benjamin Kramer | 91bbe23 | 2011-10-28 11:14:31 +0000 | [diff] [blame^] | 83 | if (BranchProbability(MaxWeight, Sum) >= BranchProbability(4, 5)) |
Jakub Staszak | 7cc2b07 | 2011-06-16 20:22:37 +0000 | [diff] [blame] | 84 | return MaxSucc; |
| 85 | |
| 86 | return 0; |
| 87 | } |
| 88 | |
| 89 | BranchProbability |
| 90 | MachineBranchProbabilityInfo::getEdgeProbability(MachineBasicBlock *Src, |
| 91 | MachineBasicBlock *Dst) const { |
| 92 | uint32_t N = getEdgeWeight(Src, Dst); |
| 93 | uint32_t D = getSumForBlock(Src); |
| 94 | |
| 95 | return BranchProbability(N, D); |
| 96 | } |
| 97 | |
| 98 | raw_ostream &MachineBranchProbabilityInfo:: |
| 99 | printEdgeProbability(raw_ostream &OS, MachineBasicBlock *Src, |
| 100 | MachineBasicBlock *Dst) const { |
| 101 | |
| 102 | const BranchProbability Prob = getEdgeProbability(Src, Dst); |
| 103 | OS << "edge MBB#" << Src->getNumber() << " -> MBB#" << Dst->getNumber() |
| 104 | << " probability is " << Prob |
| 105 | << (isEdgeHot(Src, Dst) ? " [HOT edge]\n" : "\n"); |
| 106 | |
| 107 | return OS; |
| 108 | } |