LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MachineBranchProbabilityInfo.cpp
Go to the documentation of this file.
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 
16 #include "llvm/IR/Instructions.h"
17 #include "llvm/Support/Debug.h"
19 
20 using namespace llvm;
21 
23  "Machine Branch Probability Analysis", false, true)
25  "Machine Branch Probability Analysis", false, true)
26 
27 char MachineBranchProbabilityInfo::ID = 0;
28 
29 void MachineBranchProbabilityInfo::anchor() { }
30 
32 getSumForBlock(const MachineBasicBlock *MBB, uint32_t &Scale) const {
33  // First we compute the sum with 64-bits of precision, ensuring that cannot
34  // overflow by bounding the number of weights considered. Hopefully no one
35  // actually needs 2^32 successors.
36  assert(MBB->succ_size() < UINT32_MAX);
37  uint64_t Sum = 0;
38  Scale = 1;
40  E = MBB->succ_end(); I != E; ++I) {
41  uint32_t Weight = getEdgeWeight(MBB, I);
42  Sum += Weight;
43  }
44 
45  // If the computed sum fits in 32-bits, we're done.
46  if (Sum <= UINT32_MAX)
47  return Sum;
48 
49  // Otherwise, compute the scale necessary to cause the weights to fit, and
50  // re-sum with that scale applied.
51  assert((Sum / UINT32_MAX) < UINT32_MAX);
52  Scale = (Sum / UINT32_MAX) + 1;
53  Sum = 0;
55  E = MBB->succ_end(); I != E; ++I) {
56  uint32_t Weight = getEdgeWeight(MBB, I);
57  Sum += Weight / Scale;
58  }
59  assert(Sum <= UINT32_MAX);
60  return Sum;
61 }
62 
66  uint32_t Weight = Src->getSuccWeight(Dst);
67  if (!Weight)
68  return DEFAULT_WEIGHT;
69  return Weight;
70 }
71 
74  const MachineBasicBlock *Dst) const {
75  // This is a linear search. Try to use the const_succ_iterator version when
76  // possible.
77  return getEdgeWeight(Src, std::find(Src->succ_begin(), Src->succ_end(), Dst));
78 }
79 
81  MachineBasicBlock *Dst) const {
82  // Hot probability is at least 4/5 = 80%
83  // FIXME: Compare against a static "hot" BranchProbability.
84  return getEdgeProbability(Src, Dst) > BranchProbability(4, 5);
85 }
86 
89  uint32_t MaxWeight = 0;
90  MachineBasicBlock *MaxSucc = 0;
92  E = MBB->succ_end(); I != E; ++I) {
93  uint32_t Weight = getEdgeWeight(MBB, I);
94  if (Weight > MaxWeight) {
95  MaxWeight = Weight;
96  MaxSucc = *I;
97  }
98  }
99 
100  if (getEdgeProbability(MBB, MaxSucc) >= BranchProbability(4, 5))
101  return MaxSucc;
102 
103  return 0;
104 }
105 
108  MachineBasicBlock *Dst) const {
109  uint32_t Scale = 1;
110  uint32_t D = getSumForBlock(Src, Scale);
111  uint32_t N = getEdgeWeight(Src, Dst) / Scale;
112 
113  return BranchProbability(N, D);
114 }
115 
118  MachineBasicBlock *Dst) const {
119 
120  const BranchProbability Prob = getEdgeProbability(Src, Dst);
121  OS << "edge MBB#" << Src->getNumber() << " -> MBB#" << Dst->getNumber()
122  << " probability is " << Prob
123  << (isEdgeHot(Src, Dst) ? " [HOT edge]\n" : "\n");
124 
125  return OS;
126 }
unsigned succ_size() const
machine branch prob
INITIALIZE_PASS_BEGIN(MachineBranchProbabilityInfo,"machine-branch-prob","Machine Branch Probability Analysis", false, true) INITIALIZE_PASS_END(MachineBranchProbabilityInfo
machine branch Machine Branch Probability false
machine branch Machine Branch Probability Analysis
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:172
BranchProbability getEdgeProbability(MachineBasicBlock *Src, MachineBasicBlock *Dst) const
ID
LLVM Calling Convention Representation.
Definition: CallingConv.h:26
COFF::MachineTypes Machine
Definition: COFFYAML.cpp:211
#define true
Definition: ConvertUTF.c:65
bool isEdgeHot(MachineBasicBlock *Src, MachineBasicBlock *Dst) const
MachineBasicBlock * getHotSucc(MachineBasicBlock *MBB) const
uint32_t getSumForBlock(const MachineBasicBlock *MBB, uint32_t &Scale) const
raw_ostream & printEdgeProbability(raw_ostream &OS, MachineBasicBlock *Src, MachineBasicBlock *Dst) const
#define I(x, y, z)
Definition: MD5.cpp:54
#define N
uint32_t getEdgeWeight(const MachineBasicBlock *Src, const MachineBasicBlock *Dst) const
std::vector< MachineBasicBlock * >::const_iterator const_succ_iterator