1321369Sdim//===- MachineBlockFrequencyInfo.h - MBB Frequency Analysis -----*- C++ -*-===//
2226584Sdim//
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
6226584Sdim//
7226584Sdim//===----------------------------------------------------------------------===//
8226584Sdim//
9226584Sdim// Loops should be simplified before this analysis.
10226584Sdim//
11226584Sdim//===----------------------------------------------------------------------===//
12226584Sdim
13226584Sdim#ifndef LLVM_CODEGEN_MACHINEBLOCKFREQUENCYINFO_H
14226584Sdim#define LLVM_CODEGEN_MACHINEBLOCKFREQUENCYINFO_H
15226584Sdim
16309124Sdim#include "llvm/ADT/Optional.h"
17226584Sdim#include "llvm/CodeGen/MachineFunctionPass.h"
18226584Sdim#include "llvm/Support/BlockFrequency.h"
19321369Sdim#include <cstdint>
20321369Sdim#include <memory>
21226584Sdim
22226584Sdimnamespace llvm {
23226584Sdim
24321369Sdimtemplate <class BlockT> class BlockFrequencyInfoImpl;
25234353Sdimclass MachineBasicBlock;
26226584Sdimclass MachineBranchProbabilityInfo;
27321369Sdimclass MachineFunction;
28321369Sdimclass MachineLoopInfo;
29321369Sdimclass raw_ostream;
30226584Sdim
31276479Sdim/// MachineBlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation
32276479Sdim/// to estimate machine basic block frequencies.
33226584Sdimclass MachineBlockFrequencyInfo : public MachineFunctionPass {
34321369Sdim  using ImplType = BlockFrequencyInfoImpl<MachineBasicBlock>;
35276479Sdim  std::unique_ptr<ImplType> MBFI;
36226584Sdim
37226584Sdimpublic:
38226584Sdim  static char ID;
39226584Sdim
40226584Sdim  MachineBlockFrequencyInfo();
41360784Sdim  explicit MachineBlockFrequencyInfo(MachineFunction &F,
42360784Sdim                                     MachineBranchProbabilityInfo &MBPI,
43360784Sdim                                     MachineLoopInfo &MLI);
44288943Sdim  ~MachineBlockFrequencyInfo() override;
45226584Sdim
46276479Sdim  void getAnalysisUsage(AnalysisUsage &AU) const override;
47226584Sdim
48276479Sdim  bool runOnMachineFunction(MachineFunction &F) override;
49226584Sdim
50321369Sdim  /// calculate - compute block frequency info for the given function.
51321369Sdim  void calculate(const MachineFunction &F,
52321369Sdim                 const MachineBranchProbabilityInfo &MBPI,
53321369Sdim                 const MachineLoopInfo &MLI);
54321369Sdim
55276479Sdim  void releaseMemory() override;
56276479Sdim
57226584Sdim  /// getblockFreq - Return block frequency. Return 0 if we don't have the
58226584Sdim  /// information. Please note that initial frequency is equal to 1024. It means
59226584Sdim  /// that we should not rely on the value itself, but only on the comparison to
60226584Sdim  /// the other block frequencies. We do this to avoid using of floating points.
61226584Sdim  ///
62234353Sdim  BlockFrequency getBlockFreq(const MachineBasicBlock *MBB) const;
63276479Sdim
64309124Sdim  Optional<uint64_t> getBlockProfileCount(const MachineBasicBlock *MBB) const;
65314564Sdim  Optional<uint64_t> getProfileCountFromFreq(uint64_t Freq) const;
66309124Sdim
67327952Sdim  bool isIrrLoopHeader(const MachineBasicBlock *MBB);
68327952Sdim
69276479Sdim  const MachineFunction *getFunction() const;
70309124Sdim  const MachineBranchProbabilityInfo *getMBPI() const;
71321369Sdim  void view(const Twine &Name, bool isSimple = true) const;
72276479Sdim
73276479Sdim  // Print the block frequency Freq to OS using the current functions entry
74276479Sdim  // frequency to convert freq into a relative decimal form.
75276479Sdim  raw_ostream &printBlockFreq(raw_ostream &OS, const BlockFrequency Freq) const;
76276479Sdim
77276479Sdim  // Convenience method that attempts to look up the frequency associated with
78276479Sdim  // BB and print it to OS.
79276479Sdim  raw_ostream &printBlockFreq(raw_ostream &OS,
80276479Sdim                              const MachineBasicBlock *MBB) const;
81276479Sdim
82276479Sdim  uint64_t getEntryFreq() const;
83226584Sdim};
84226584Sdim
85321369Sdim} // end namespace llvm
86226584Sdim
87321369Sdim#endif // LLVM_CODEGEN_MACHINEBLOCKFREQUENCYINFO_H
88