1261991Sdim//=- MachineBranchProbabilityInfo.h - Branch Probability Analysis -*- C++ -*-=//
2224133Sdim//
3353358Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4353358Sdim// See https://llvm.org/LICENSE.txt for license information.
5353358Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6224133Sdim//
7224133Sdim//===----------------------------------------------------------------------===//
8224133Sdim//
9224133Sdim// This pass is used to evaluate branch probabilties on machine basic blocks.
10224133Sdim//
11224133Sdim//===----------------------------------------------------------------------===//
12224133Sdim
13224133Sdim#ifndef LLVM_CODEGEN_MACHINEBRANCHPROBABILITYINFO_H
14224133Sdim#define LLVM_CODEGEN_MACHINEBRANCHPROBABILITYINFO_H
15224133Sdim
16249423Sdim#include "llvm/CodeGen/MachineBasicBlock.h"
17224133Sdim#include "llvm/Pass.h"
18224133Sdim#include "llvm/Support/BranchProbability.h"
19224133Sdim#include <climits>
20296417Sdim#include <numeric>
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
38360784Sdim  MachineBranchProbabilityInfo();
39224133Sdim
40276479Sdim  void getAnalysisUsage(AnalysisUsage &AU) const override {
41224133Sdim    AU.setPreservesAll();
42224133Sdim  }
43224133Sdim
44296417Sdim  // Return edge probability.
45296417Sdim  BranchProbability getEdgeProbability(const MachineBasicBlock *Src,
46296417Sdim                                       const MachineBasicBlock *Dst) const;
47224133Sdim
48296417Sdim  // Same as above, but using a const_succ_iterator from Src. This is faster
49296417Sdim  // when the iterator is already available.
50296417Sdim  BranchProbability
51296417Sdim  getEdgeProbability(const MachineBasicBlock *Src,
52296417Sdim                     MachineBasicBlock::const_succ_iterator Dst) const;
53243830Sdim
54224133Sdim  // A 'Hot' edge is an edge which probability is >= 80%.
55276479Sdim  bool isEdgeHot(const MachineBasicBlock *Src,
56276479Sdim                 const MachineBasicBlock *Dst) const;
57224133Sdim
58224133Sdim  // Return a hot successor for the block BB or null if there isn't one.
59234353Sdim  // NB: This routine's complexity is linear on the number of successors.
60224133Sdim  MachineBasicBlock *getHotSucc(MachineBasicBlock *MBB) const;
61224133Sdim
62224133Sdim  // Print value between 0 (0% probability) and 1 (100% probability),
63224133Sdim  // however the value is never equal to 0, and can be 1 only iff SRC block
64224133Sdim  // has only one successor.
65276479Sdim  raw_ostream &printEdgeProbability(raw_ostream &OS,
66276479Sdim                                    const MachineBasicBlock *Src,
67276479Sdim                                    const MachineBasicBlock *Dst) const;
68224133Sdim};
69224133Sdim
70224133Sdim}
71224133Sdim
72224133Sdim
73224133Sdim#endif
74