LLVM 20.0.0git
MachineBlockFrequencyInfo.h
Go to the documentation of this file.
1//===- MachineBlockFrequencyInfo.h - MBB Frequency Analysis -----*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// Loops should be simplified before this analysis.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_CODEGEN_MACHINEBLOCKFREQUENCYINFO_H
14#define LLVM_CODEGEN_MACHINEBLOCKFREQUENCYINFO_H
15
19#include <cstdint>
20#include <memory>
21#include <optional>
22
23namespace llvm {
24
25template <class BlockT> class BlockFrequencyInfoImpl;
26class MachineBasicBlock;
27class MachineBranchProbabilityInfo;
28class MachineFunction;
29class MachineLoopInfo;
30class raw_ostream;
31
32/// MachineBlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation
33/// to estimate machine basic block frequencies.
36 std::unique_ptr<ImplType> MBFI;
37
38public:
39 MachineBlockFrequencyInfo(); // Legacy pass manager only.
42 MachineLoopInfo &MLI);
45
46 /// Handle invalidation explicitly.
49
50 /// calculate - compute block frequency info for the given function.
51 void calculate(const MachineFunction &F,
53 const MachineLoopInfo &MLI);
54
55 void print(raw_ostream &OS);
56
57 void releaseMemory();
58
59 /// getblockFreq - Return block frequency. Return 0 if we don't have the
60 /// information. Please note that initial frequency is equal to 1024. It means
61 /// that we should not rely on the value itself, but only on the comparison to
62 /// the other block frequencies. We do this to avoid using of floating points.
63 /// For example, to get the frequency of a block relative to the entry block,
64 /// divide the integral value returned by this function (the
65 /// BlockFrequency::getFrequency() value) by getEntryFreq().
67
68 /// Compute the frequency of the block, relative to the entry block.
69 /// This API assumes getEntryFreq() is non-zero.
72 "getEntryFreq() should not return 0 here!");
73 return static_cast<double>(getBlockFreq(MBB).getFrequency()) /
74 static_cast<double>(getEntryFreq().getFrequency());
75 }
76
77 std::optional<uint64_t>
79 std::optional<uint64_t> getProfileCountFromFreq(BlockFrequency Freq) const;
80
81 bool isIrrLoopHeader(const MachineBasicBlock *MBB) const;
82
83 /// incrementally calculate block frequencies when we split edges, to avoid
84 /// full CFG traversal.
85 void onEdgeSplit(const MachineBasicBlock &NewPredecessor,
86 const MachineBasicBlock &NewSuccessor,
88
89 const MachineFunction *getFunction() const;
91
92 /// Pop up a ghostview window with the current block frequency propagation
93 /// rendered using dot.
94 void view(const Twine &Name, bool isSimple = true) const;
95
96 /// Divide a block's BlockFrequency::getFrequency() value by this value to
97 /// obtain the entry block - relative frequency of said block.
99};
100
101/// Print the block frequency @p Freq relative to the current functions entry
102/// frequency. Returns a Printable object that can be piped via `<<` to a
103/// `raw_ostream`.
104Printable printBlockFreq(const MachineBlockFrequencyInfo &MBFI,
105 BlockFrequency Freq);
106
107/// Convenience function equivalent to calling
108/// `printBlockFreq(MBFI, MBFI.getBlockFreq(&MBB))`.
109Printable printBlockFreq(const MachineBlockFrequencyInfo &MBFI,
110 const MachineBasicBlock &MBB);
111
113 : public AnalysisInfoMixin<MachineBlockFrequencyAnalysis> {
115 static AnalysisKey Key;
116
117public:
119
121};
122
123/// Printer pass for the \c MachineBlockFrequencyInfo results.
125 : public PassInfoMixin<MachineBlockFrequencyPrinterPass> {
126 raw_ostream &OS;
127
128public:
130
133
134 static bool isRequired() { return true; }
135};
136
139
140public:
141 static char ID;
142
144
145 void getAnalysisUsage(AnalysisUsage &AU) const override;
146
147 bool runOnMachineFunction(MachineFunction &F) override;
148
149 void releaseMemory() override { MBFI.releaseMemory(); }
150
152
153 const MachineBlockFrequencyInfo &getMBFI() const { return MBFI; }
154};
155} // end namespace llvm
156
157#endif // LLVM_CODEGEN_MACHINEBLOCKFREQUENCYINFO_H
MachineBasicBlock & MBB
std::string Name
#define F(x, y, z)
Definition: MD5.cpp:55
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static bool isSimple(Instruction *I)
raw_pwrite_stream & OS
API to communicate dependencies between analyses during invalidation.
Definition: PassManager.h:292
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:253
Represent the analysis usage information of a pass.
Shared implementation for block frequency analysis.
uint64_t getFrequency() const
Returns the frequency as a fixpoint number scaled by the entry frequency.
Result run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
bool runOnMachineFunction(MachineFunction &F) override
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
const MachineBlockFrequencyInfo & getMBFI() const
void releaseMemory() override
releaseMemory() - This member can be implemented by a pass if it wants to be able to release its memo...
MachineBlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate machine basic b...
void view(const Twine &Name, bool isSimple=true) const
Pop up a ghostview window with the current block frequency propagation rendered using dot.
bool isIrrLoopHeader(const MachineBasicBlock *MBB) const
const MachineBranchProbabilityInfo * getMBPI() const
BlockFrequency getBlockFreq(const MachineBasicBlock *MBB) const
getblockFreq - Return block frequency.
void onEdgeSplit(const MachineBasicBlock &NewPredecessor, const MachineBasicBlock &NewSuccessor, const MachineBranchProbabilityInfo &MBPI)
incrementally calculate block frequencies when we split edges, to avoid full CFG traversal.
void calculate(const MachineFunction &F, const MachineBranchProbabilityInfo &MBPI, const MachineLoopInfo &MLI)
calculate - compute block frequency info for the given function.
const MachineFunction * getFunction() const
double getBlockFreqRelativeToEntryBlock(const MachineBasicBlock *MBB) const
Compute the frequency of the block, relative to the entry block.
std::optional< uint64_t > getProfileCountFromFreq(BlockFrequency Freq) const
bool invalidate(MachineFunction &F, const PreservedAnalyses &PA, MachineFunctionAnalysisManager::Invalidator &)
Handle invalidation explicitly.
BlockFrequency getEntryFreq() const
Divide a block's BlockFrequency::getFrequency() value by this value to obtain the entry block - relat...
std::optional< uint64_t > getBlockProfileCount(const MachineBasicBlock *MBB) const
MachineBlockFrequencyInfo(MachineBlockFrequencyInfo &&)
Printer pass for the MachineBlockFrequencyInfo results.
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
A set of analyses that are preserved following a run of a transformation pass.
Definition: Analysis.h:111
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
Printable printBlockFreq(const BlockFrequencyInfo &BFI, BlockFrequency Freq)
Print the block frequency Freq relative to the current functions entry frequency.
A CRTP mix-in that provides informational APIs needed for analysis passes.
Definition: PassManager.h:92
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition: Analysis.h:28
A CRTP mix-in to automatically provide informational APIs needed for passes.
Definition: PassManager.h:69