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, 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);
37 for (MachineBasicBlock::const_succ_iterator I = MBB->succ_begin(),
38 E = MBB->succ_end(); I != E; ++I) {
39 uint32_t Weight = getEdgeWeight(MBB, *I);
43 // If the computed sum fits in 32-bits, we're done.
44 if (Sum <= UINT32_MAX)
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;
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;
57 assert(Sum <= UINT32_MAX);
62 MachineBranchProbabilityInfo::getEdgeWeight(MachineBasicBlock *Src,
63 MachineBasicBlock *Dst) const {
64 uint32_t Weight = Src->getSuccWeight(Dst);
66 return DEFAULT_WEIGHT;
70 bool MachineBranchProbabilityInfo::isEdgeHot(MachineBasicBlock *Src,
71 MachineBasicBlock *Dst) const {
72 // Hot probability is at least 4/5 = 80%
73 // FIXME: Compare against a static "hot" BranchProbability.
74 return getEdgeProbability(Src, Dst) > BranchProbability(4, 5);
78 MachineBranchProbabilityInfo::getHotSucc(MachineBasicBlock *MBB) const {
79 uint32_t MaxWeight = 0;
80 MachineBasicBlock *MaxSucc = 0;
81 for (MachineBasicBlock::const_succ_iterator I = MBB->succ_begin(),
82 E = MBB->succ_end(); I != E; ++I) {
83 uint32_t Weight = getEdgeWeight(MBB, *I);
84 if (Weight > MaxWeight) {
90 if (getEdgeProbability(MBB, MaxSucc) >= BranchProbability(4, 5))
97 MachineBranchProbabilityInfo::getEdgeProbability(MachineBasicBlock *Src,
98 MachineBasicBlock *Dst) const {
100 uint32_t D = getSumForBlock(Src, Scale);
101 uint32_t N = getEdgeWeight(Src, Dst) / Scale;
103 return BranchProbability(N, D);
106 raw_ostream &MachineBranchProbabilityInfo::
107 printEdgeProbability(raw_ostream &OS, MachineBasicBlock *Src,
108 MachineBasicBlock *Dst) const {
110 const BranchProbability Prob = getEdgeProbability(Src, Dst);
111 OS << "edge MBB#" << Src->getNumber() << " -> MBB#" << Dst->getNumber()
112 << " probability is " << Prob
113 << (isEdgeHot(Src, Dst) ? " [HOT edge]\n" : "\n");