1263508Sdim//=- MachineBranchProbabilityInfo.h - Branch Probability Analysis -*- C++ -*-=//
2224133Sdim//
3224133Sdim//                     The LLVM Compiler Infrastructure
4224133Sdim//
5224133Sdim// This file is distributed under the University of Illinois Open Source
6224133Sdim// License. See LICENSE.TXT for details.
7224133Sdim//
8224133Sdim//===----------------------------------------------------------------------===//
9224133Sdim//
10224133Sdim// This pass is used to evaluate branch probabilties on machine basic blocks.
11224133Sdim//
12224133Sdim//===----------------------------------------------------------------------===//
13224133Sdim
14224133Sdim#ifndef LLVM_CODEGEN_MACHINEBRANCHPROBABILITYINFO_H
15224133Sdim#define LLVM_CODEGEN_MACHINEBRANCHPROBABILITYINFO_H
16224133Sdim
17249423Sdim#include "llvm/CodeGen/MachineBasicBlock.h"
18224133Sdim#include "llvm/Pass.h"
19224133Sdim#include "llvm/Support/BranchProbability.h"
20224133Sdim#include <climits>
21224133Sdim
22224133Sdimnamespace llvm {
23224133Sdim
24224133Sdimclass MachineBranchProbabilityInfo : public ImmutablePass {
25234353Sdim  virtual void anchor();
26224133Sdim
27224133Sdim  // Default weight value. Used when we don't have information about the edge.
28224133Sdim  // TODO: DEFAULT_WEIGHT makes sense during static predication, when none of
29224133Sdim  // the successors have a weight yet. But it doesn't make sense when providing
30224133Sdim  // weight to an edge that may have siblings with non-zero weights. This can
31224133Sdim  // be handled various ways, but it's probably fine for an edge with unknown
32224133Sdim  // weight to just "inherit" the non-zero weight of an adjacent successor.
33224133Sdim  static const uint32_t DEFAULT_WEIGHT = 16;
34224133Sdim
35224133Sdimpublic:
36224133Sdim  static char ID;
37224133Sdim
38224133Sdim  MachineBranchProbabilityInfo() : ImmutablePass(ID) {
39224133Sdim    PassRegistry &Registry = *PassRegistry::getPassRegistry();
40224133Sdim    initializeMachineBranchProbabilityInfoPass(Registry);
41224133Sdim  }
42224133Sdim
43224133Sdim  void getAnalysisUsage(AnalysisUsage &AU) const {
44224133Sdim    AU.setPreservesAll();
45224133Sdim  }
46224133Sdim
47224133Sdim  // Return edge weight. If we don't have any informations about it - return
48224133Sdim  // DEFAULT_WEIGHT.
49234353Sdim  uint32_t getEdgeWeight(const MachineBasicBlock *Src,
50234353Sdim                         const MachineBasicBlock *Dst) const;
51224133Sdim
52243830Sdim  // Same thing, but using a const_succ_iterator from Src. This is faster when
53243830Sdim  // the iterator is already available.
54243830Sdim  uint32_t getEdgeWeight(const MachineBasicBlock *Src,
55243830Sdim                         MachineBasicBlock::const_succ_iterator Dst) const;
56243830Sdim
57234353Sdim  // Get sum of the block successors' weights, potentially scaling them to fit
58234353Sdim  // within 32-bits. If scaling is required, sets Scale based on the necessary
59234353Sdim  // adjustment. Any edge weights used with the sum should be divided by Scale.
60234353Sdim  uint32_t getSumForBlock(const MachineBasicBlock *MBB, uint32_t &Scale) const;
61234353Sdim
62224133Sdim  // A 'Hot' edge is an edge which probability is >= 80%.
63224133Sdim  bool isEdgeHot(MachineBasicBlock *Src, MachineBasicBlock *Dst) const;
64224133Sdim
65224133Sdim  // Return a hot successor for the block BB or null if there isn't one.
66234353Sdim  // NB: This routine's complexity is linear on the number of successors.
67224133Sdim  MachineBasicBlock *getHotSucc(MachineBasicBlock *MBB) const;
68224133Sdim
69224133Sdim  // Return a probability as a fraction between 0 (0% probability) and
70224133Sdim  // 1 (100% probability), however the value is never equal to 0, and can be 1
71224133Sdim  // only iff SRC block has only one successor.
72234353Sdim  // NB: This routine's complexity is linear on the number of successors of
73234353Sdim  // Src. Querying sequentially for each successor's probability is a quadratic
74234353Sdim  // query pattern.
75224133Sdim  BranchProbability getEdgeProbability(MachineBasicBlock *Src,
76224133Sdim                                       MachineBasicBlock *Dst) const;
77224133Sdim
78224133Sdim  // Print value between 0 (0% probability) and 1 (100% probability),
79224133Sdim  // however the value is never equal to 0, and can be 1 only iff SRC block
80224133Sdim  // has only one successor.
81224133Sdim  raw_ostream &printEdgeProbability(raw_ostream &OS, MachineBasicBlock *Src,
82224133Sdim                                    MachineBasicBlock *Dst) const;
83224133Sdim};
84224133Sdim
85224133Sdim}
86224133Sdim
87224133Sdim
88224133Sdim#endif
89