blob: 0037d525151226ee9d3accfa14f8434deeefebd5 [file] [log] [blame]
Jakub Staszak7cc2b072011-06-16 20:22:37 +00001//===- 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::
Chandler Carruth2770c142011-11-14 08:50:16 +000030getSumForBlock(MachineBasicBlock *MBB, uint32_t &Scale) const {
31 // First we compute the sum with 64-bits of precision, ensuring that cannot
32 // overflow by bounding the number of weights considered. Hopefully no one
33 // actually needs 2^32 successors.
34 assert(MBB->succ_size() < UINT32_MAX);
35 uint64_t Sum = 0;
36 Scale = 1;
Jakub Staszak7cc2b072011-06-16 20:22:37 +000037 for (MachineBasicBlock::const_succ_iterator I = MBB->succ_begin(),
38 E = MBB->succ_end(); I != E; ++I) {
Chandler Carruth2770c142011-11-14 08:50:16 +000039 uint32_t Weight = getEdgeWeight(MBB, *I);
Jakub Staszak7cc2b072011-06-16 20:22:37 +000040 Sum += Weight;
Jakub Staszak7cc2b072011-06-16 20:22:37 +000041 }
42
Chandler Carruth2770c142011-11-14 08:50:16 +000043 // If the computed sum fits in 32-bits, we're done.
44 if (Sum <= UINT32_MAX)
45 return Sum;
46
47 // Otherwise, compute the scale necessary to cause the weights to fit, and
48 // re-sum with that scale applied.
49 assert((Sum / UINT32_MAX) < UINT32_MAX);
50 Scale = (Sum / UINT32_MAX) + 1;
51 Sum = 0;
52 for (MachineBasicBlock::const_succ_iterator I = MBB->succ_begin(),
53 E = MBB->succ_end(); I != E; ++I) {
54 uint32_t Weight = getEdgeWeight(MBB, *I);
55 Sum += Weight / Scale;
56 }
57 assert(Sum <= UINT32_MAX);
Jakub Staszak7cc2b072011-06-16 20:22:37 +000058 return Sum;
59}
60
61uint32_t
62MachineBranchProbabilityInfo::getEdgeWeight(MachineBasicBlock *Src,
63 MachineBasicBlock *Dst) const {
64 uint32_t Weight = Src->getSuccWeight(Dst);
65 if (!Weight)
66 return DEFAULT_WEIGHT;
67 return Weight;
68}
69
70bool MachineBranchProbabilityInfo::isEdgeHot(MachineBasicBlock *Src,
71 MachineBasicBlock *Dst) const {
72 // Hot probability is at least 4/5 = 80%
Benjamin Kramer91bbe232011-10-28 11:14:31 +000073 // FIXME: Compare against a static "hot" BranchProbability.
74 return getEdgeProbability(Src, Dst) > BranchProbability(4, 5);
Jakub Staszak7cc2b072011-06-16 20:22:37 +000075}
76
77MachineBasicBlock *
78MachineBranchProbabilityInfo::getHotSucc(MachineBasicBlock *MBB) const {
79 uint32_t Sum = 0;
80 uint32_t MaxWeight = 0;
81 MachineBasicBlock *MaxSucc = 0;
82
83 for (MachineBasicBlock::const_succ_iterator I = MBB->succ_begin(),
84 E = MBB->succ_end(); I != E; ++I) {
85 MachineBasicBlock *Succ = *I;
86 uint32_t Weight = getEdgeWeight(MBB, Succ);
87 uint32_t PrevSum = Sum;
88
89 Sum += Weight;
90 assert(Sum > PrevSum); (void) PrevSum;
91
92 if (Weight > MaxWeight) {
93 MaxWeight = Weight;
94 MaxSucc = Succ;
95 }
96 }
97
Benjamin Kramer91bbe232011-10-28 11:14:31 +000098 if (BranchProbability(MaxWeight, Sum) >= BranchProbability(4, 5))
Jakub Staszak7cc2b072011-06-16 20:22:37 +000099 return MaxSucc;
100
101 return 0;
102}
103
104BranchProbability
105MachineBranchProbabilityInfo::getEdgeProbability(MachineBasicBlock *Src,
106 MachineBasicBlock *Dst) const {
Chandler Carruth2770c142011-11-14 08:50:16 +0000107 uint32_t Scale = 1;
108 uint32_t D = getSumForBlock(Src, Scale);
109 uint32_t N = getEdgeWeight(Src, Dst) / Scale;
Jakub Staszak7cc2b072011-06-16 20:22:37 +0000110
111 return BranchProbability(N, D);
112}
113
114raw_ostream &MachineBranchProbabilityInfo::
115printEdgeProbability(raw_ostream &OS, MachineBasicBlock *Src,
116 MachineBasicBlock *Dst) const {
117
118 const BranchProbability Prob = getEdgeProbability(Src, Dst);
119 OS << "edge MBB#" << Src->getNumber() << " -> MBB#" << Dst->getNumber()
120 << " probability is " << Prob
121 << (isEdgeHot(Src, Dst) ? " [HOT edge]\n" : "\n");
122
123 return OS;
124}