MachineBranchProbabilityInfo.cpp revision 91bbe237167bf84ce41d01eff3c028ff2b10be26
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 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 27char MachineBranchProbabilityInfo::ID = 0; 28 29uint32_t MachineBranchProbabilityInfo:: 30getSumForBlock(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 46uint32_t 47MachineBranchProbabilityInfo::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 55bool MachineBranchProbabilityInfo::isEdgeHot(MachineBasicBlock *Src, 56 MachineBasicBlock *Dst) const { 57 // Hot probability is at least 4/5 = 80% 58 // FIXME: Compare against a static "hot" BranchProbability. 59 return getEdgeProbability(Src, Dst) > BranchProbability(4, 5); 60} 61 62MachineBasicBlock * 63MachineBranchProbabilityInfo::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 83 if (BranchProbability(MaxWeight, Sum) >= BranchProbability(4, 5)) 84 return MaxSucc; 85 86 return 0; 87} 88 89BranchProbability 90MachineBranchProbabilityInfo::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 98raw_ostream &MachineBranchProbabilityInfo:: 99printEdgeProbability(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} 109