1317017Sdim///===- LazyMachineBlockFrequencyInfo.h - Lazy Block Frequency -*- C++ -*--===//
2317017Sdim///
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
6317017Sdim///
7317017Sdim///===---------------------------------------------------------------------===//
8317017Sdim/// \file
9317017Sdim/// This is an alternative analysis pass to MachineBlockFrequencyInfo.  The
10317017Sdim/// difference is that with this pass the block frequencies are not computed
11317017Sdim/// when the analysis pass is executed but rather when the BFI result is
12317017Sdim/// explicitly requested by the analysis client.
13317017Sdim///
14317017Sdim///===---------------------------------------------------------------------===//
15317017Sdim
16317017Sdim#ifndef LLVM_ANALYSIS_LAZYMACHINEBLOCKFREQUENCYINFO_H
17317017Sdim#define LLVM_ANALYSIS_LAZYMACHINEBLOCKFREQUENCYINFO_H
18317017Sdim
19317017Sdim#include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
20317017Sdim#include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
21317017Sdim#include "llvm/CodeGen/MachineDominators.h"
22317017Sdim#include "llvm/CodeGen/MachineLoopInfo.h"
23317017Sdim
24317017Sdimnamespace llvm {
25341825Sdim/// This is an alternative analysis pass to MachineBlockFrequencyInfo.
26317017Sdim/// The difference is that with this pass, the block frequencies are not
27317017Sdim/// computed when the analysis pass is executed but rather when the BFI result
28317017Sdim/// is explicitly requested by the analysis client.
29317017Sdim///
30317017Sdim/// This works by checking querying if MBFI is available and otherwise
31317017Sdim/// generating MBFI on the fly.  In this case the passes required for (LI, DT)
32317017Sdim/// are also queried before being computed on the fly.
33317017Sdim///
34317017Sdim/// Note that it is expected that we wouldn't need this functionality for the
35317017Sdim/// new PM since with the new PM, analyses are executed on demand.
36317017Sdim
37317017Sdimclass LazyMachineBlockFrequencyInfoPass : public MachineFunctionPass {
38317017Sdimprivate:
39317017Sdim  /// If generated on the fly this own the instance.
40317017Sdim  mutable std::unique_ptr<MachineBlockFrequencyInfo> OwnedMBFI;
41317017Sdim
42317017Sdim  /// If generated on the fly this own the instance.
43317017Sdim  mutable std::unique_ptr<MachineLoopInfo> OwnedMLI;
44317017Sdim
45317017Sdim  /// If generated on the fly this own the instance.
46317017Sdim  mutable std::unique_ptr<MachineDominatorTree> OwnedMDT;
47317017Sdim
48317017Sdim  /// The function.
49317017Sdim  MachineFunction *MF = nullptr;
50317017Sdim
51341825Sdim  /// Calculate MBFI and all other analyses that's not available and
52317017Sdim  /// required by BFI.
53317017Sdim  MachineBlockFrequencyInfo &calculateIfNotAvailable() const;
54317017Sdim
55317017Sdimpublic:
56317017Sdim  static char ID;
57317017Sdim
58317017Sdim  LazyMachineBlockFrequencyInfoPass();
59317017Sdim
60341825Sdim  /// Compute and return the block frequencies.
61317017Sdim  MachineBlockFrequencyInfo &getBFI() { return calculateIfNotAvailable(); }
62317017Sdim
63341825Sdim  /// Compute and return the block frequencies.
64317017Sdim  const MachineBlockFrequencyInfo &getBFI() const {
65317017Sdim    return calculateIfNotAvailable();
66317017Sdim  }
67317017Sdim
68317017Sdim  void getAnalysisUsage(AnalysisUsage &AU) const override;
69317017Sdim
70317017Sdim  bool runOnMachineFunction(MachineFunction &F) override;
71317017Sdim  void releaseMemory() override;
72317017Sdim  void print(raw_ostream &OS, const Module *M) const override;
73317017Sdim};
74317017Sdim}
75317017Sdim#endif
76