LCOV - code coverage report
Current view: top level - lib/CodeGen - MachineBranchProbabilityInfo.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 13 35 37.1 %
Date: 2017-09-14 15:23:50 Functions: 6 10 60.0 %
Legend: Lines: hit not hit

          Line data    Source code
       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/CodeGen/MachineBranchProbabilityInfo.h"
      15             : #include "llvm/CodeGen/MachineBasicBlock.h"
      16             : #include "llvm/IR/Instructions.h"
      17             : #include "llvm/Support/Debug.h"
      18             : #include "llvm/Support/raw_ostream.h"
      19             : 
      20             : using namespace llvm;
      21             : 
      22       53279 : INITIALIZE_PASS_BEGIN(MachineBranchProbabilityInfo, "machine-branch-prob",
      23             :                       "Machine Branch Probability Analysis", false, true)
      24     1477993 : INITIALIZE_PASS_END(MachineBranchProbabilityInfo, "machine-branch-prob",
      25             :                     "Machine Branch Probability Analysis", false, true)
      26             : 
      27             : cl::opt<unsigned>
      28       72306 :     StaticLikelyProb("static-likely-prob",
      29      216918 :                      cl::desc("branch probability threshold in percentage"
      30             :                               "to be considered very likely"),
      31      289224 :                      cl::init(80), cl::Hidden);
      32             : 
      33       72306 : cl::opt<unsigned> ProfileLikelyProb(
      34             :     "profile-likely-prob",
      35      216918 :     cl::desc("branch probability threshold in percentage to be considered"
      36             :              " very likely when profile is available"),
      37      289224 :     cl::init(51), cl::Hidden);
      38             : 
      39             : char MachineBranchProbabilityInfo::ID = 0;
      40             : 
      41           0 : void MachineBranchProbabilityInfo::anchor() {}
      42             : 
      43     1275218 : BranchProbability MachineBranchProbabilityInfo::getEdgeProbability(
      44             :     const MachineBasicBlock *Src,
      45             :     MachineBasicBlock::const_succ_iterator Dst) const {
      46     1275218 :   return Src->getSuccProbability(Dst);
      47             : }
      48             : 
      49     1275218 : 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.
      53     2550436 :   return getEdgeProbability(Src, find(Src->successors(), Dst));
      54             : }
      55             : 
      56           0 : bool MachineBranchProbabilityInfo::isEdgeHot(
      57             :     const MachineBasicBlock *Src, const MachineBasicBlock *Dst) const {
      58           0 :   BranchProbability HotProb(StaticLikelyProb, 100);
      59           0 :   return getEdgeProbability(Src, Dst) > HotProb;
      60             : }
      61             : 
      62             : MachineBasicBlock *
      63           0 : MachineBranchProbabilityInfo::getHotSucc(MachineBasicBlock *MBB) const {
      64           0 :   auto MaxProb = BranchProbability::getZero();
      65           0 :   MachineBasicBlock *MaxSucc = nullptr;
      66           0 :   for (MachineBasicBlock::const_succ_iterator I = MBB->succ_begin(),
      67           0 :        E = MBB->succ_end(); I != E; ++I) {
      68           0 :     auto Prob = getEdgeProbability(MBB, I);
      69           0 :     if (Prob > MaxProb) {
      70           0 :       MaxProb = Prob;
      71           0 :       MaxSucc = *I;
      72             :     }
      73             :   }
      74             : 
      75           0 :   BranchProbability HotProb(StaticLikelyProb, 100);
      76           0 :   if (getEdgeProbability(MBB, MaxSucc) >= HotProb)
      77           0 :     return MaxSucc;
      78             : 
      79             :   return nullptr;
      80             : }
      81             : 
      82           0 : raw_ostream &MachineBranchProbabilityInfo::printEdgeProbability(
      83             :     raw_ostream &OS, const MachineBasicBlock *Src,
      84             :     const MachineBasicBlock *Dst) const {
      85             : 
      86           0 :   const BranchProbability Prob = getEdgeProbability(Src, Dst);
      87           0 :   OS << "edge MBB#" << Src->getNumber() << " -> MBB#" << Dst->getNumber()
      88           0 :      << " probability is " << Prob
      89           0 :      << (isEdgeHot(Src, Dst) ? " [HOT edge]\n" : "\n");
      90             : 
      91           0 :   return OS;
      92      216918 : }

Generated by: LCOV version 1.13