1 //===- MachineBranchProbabilityInfo.cpp - Machine Branch Probability Info -===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This analysis uses probability info stored in Machine Basic Blocks.
12 //===----------------------------------------------------------------------===//
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"
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)
27 char MachineBranchProbabilityInfo::ID
= 0;
29 uint32_t MachineBranchProbabilityInfo::
30 getSumForBlock(MachineBasicBlock
*MBB
) const {
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
;
40 assert(Sum
> PrevSum
); (void) PrevSum
;
47 MachineBranchProbabilityInfo::getEdgeWeight(MachineBasicBlock
*Src
,
48 MachineBasicBlock
*Dst
) const {
49 uint32_t Weight
= Src
->getSuccWeight(Dst
);
51 return DEFAULT_WEIGHT
;
55 bool MachineBranchProbabilityInfo::isEdgeHot(MachineBasicBlock
*Src
,
56 MachineBasicBlock
*Dst
) const {
57 // Hot probability is at least 4/5 = 80%
58 uint32_t Weight
= getEdgeWeight(Src
, Dst
);
59 uint32_t Sum
= getSumForBlock(Src
);
61 // FIXME: Implement BranchProbability::compare then change this code to
62 // compare this BranchProbability against a static "hot" BranchProbability.
63 return (uint64_t)Weight
* 5 > (uint64_t)Sum
* 4;
67 MachineBranchProbabilityInfo::getHotSucc(MachineBasicBlock
*MBB
) const {
69 uint32_t MaxWeight
= 0;
70 MachineBasicBlock
*MaxSucc
= 0;
72 for (MachineBasicBlock::const_succ_iterator I
= MBB
->succ_begin(),
73 E
= MBB
->succ_end(); I
!= E
; ++I
) {
74 MachineBasicBlock
*Succ
= *I
;
75 uint32_t Weight
= getEdgeWeight(MBB
, Succ
);
76 uint32_t PrevSum
= Sum
;
79 assert(Sum
> PrevSum
); (void) PrevSum
;
81 if (Weight
> MaxWeight
) {
87 // FIXME: Use BranchProbability::compare.
88 if ((uint64_t)MaxWeight
* 5 >= (uint64_t)Sum
* 4)
95 MachineBranchProbabilityInfo::getEdgeProbability(MachineBasicBlock
*Src
,
96 MachineBasicBlock
*Dst
) const {
97 uint32_t N
= getEdgeWeight(Src
, Dst
);
98 uint32_t D
= getSumForBlock(Src
);
100 return BranchProbability(N
, D
);
103 raw_ostream
&MachineBranchProbabilityInfo::
104 printEdgeProbability(raw_ostream
&OS
, MachineBasicBlock
*Src
,
105 MachineBasicBlock
*Dst
) const {
107 const BranchProbability Prob
= getEdgeProbability(Src
, Dst
);
108 OS
<< "edge MBB#" << Src
->getNumber() << " -> MBB#" << Dst
->getNumber()
109 << " probability is " << Prob
110 << (isEdgeHot(Src
, Dst
) ? " [HOT edge]\n" : "\n");