LLVM 22.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
20#include <cstdint>
21#include <memory>
22#include <optional>
23
24namespace llvm {
25
26template <class BlockT> class BlockFrequencyInfoImpl;
27class MachineBasicBlock;
28class MachineBranchProbabilityInfo;
29class MachineFunction;
30class MachineLoopInfo;
31class raw_ostream;
32
33/// MachineBlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation
34/// to estimate machine basic block frequencies.
37 std::unique_ptr<ImplType> MBFI;
38
39public:
40 LLVM_ABI MachineBlockFrequencyInfo(); // Legacy pass manager only.
43 const MachineLoopInfo &MLI);
46
47 /// Handle invalidation explicitly.
50
51 /// calculate - compute block frequency info for the given function.
54 const MachineLoopInfo &MLI);
55
57
59
60 /// getblockFreq - Return block frequency. Return 0 if we don't have the
61 /// information. Please note that initial frequency is equal to 1024. It means
62 /// that we should not rely on the value itself, but only on the comparison to
63 /// the other block frequencies. We do this to avoid using of floating points.
64 /// For example, to get the frequency of a block relative to the entry block,
65 /// divide the integral value returned by this function (the
66 /// BlockFrequency::getFrequency() value) by getEntryFreq().
68
69 /// Compute the frequency of the block, relative to the entry block.
70 /// This API assumes getEntryFreq() is non-zero.
73 "getEntryFreq() should not return 0 here!");
74 return static_cast<double>(getBlockFreq(MBB).getFrequency()) /
75 static_cast<double>(getEntryFreq().getFrequency());
76 }
77
78 LLVM_ABI std::optional<uint64_t>
80 LLVM_ABI std::optional<uint64_t>
82
84
85 /// incrementally calculate block frequencies when we split edges, to avoid
86 /// full CFG traversal.
87 LLVM_ABI void onEdgeSplit(const MachineBasicBlock &NewPredecessor,
88 const MachineBasicBlock &NewSuccessor,
90
93
94 /// Pop up a ghostview window with the current block frequency propagation
95 /// rendered using dot.
96 LLVM_ABI void view(const Twine &Name, bool isSimple = true) const;
97
98 /// Divide a block's BlockFrequency::getFrequency() value by this value to
99 /// obtain the entry block - relative frequency of said block.
101};
102
103/// Print the block frequency @p Freq relative to the current functions entry
104/// frequency. Returns a Printable object that can be piped via `<<` to a
105/// `raw_ostream`.
106LLVM_ABI Printable printBlockFreq(const MachineBlockFrequencyInfo &MBFI,
107 BlockFrequency Freq);
108
109/// Convenience function equivalent to calling
110/// `printBlockFreq(MBFI, MBFI.getBlockFreq(&MBB))`.
111LLVM_ABI Printable printBlockFreq(const MachineBlockFrequencyInfo &MBFI,
112 const MachineBasicBlock &MBB);
113
115 : public AnalysisInfoMixin<MachineBlockFrequencyAnalysis> {
117 LLVM_ABI static AnalysisKey Key;
118
119public:
121
124};
125
126/// Printer pass for the \c MachineBlockFrequencyInfo results.
128 : public PassInfoMixin<MachineBlockFrequencyPrinterPass> {
129 raw_ostream &OS;
130
131public:
133
136
137 static bool isRequired() { return true; }
138};
139
141 : public MachineFunctionPass {
143
144public:
145 static char ID;
146
148
149 void getAnalysisUsage(AnalysisUsage &AU) const override;
150
151 bool runOnMachineFunction(MachineFunction &F) override;
152
153 void releaseMemory() override { MBFI.releaseMemory(); }
154
156
157 const MachineBlockFrequencyInfo &getMBFI() const { return MBFI; }
158};
159} // end namespace llvm
160
161#endif // LLVM_CODEGEN_MACHINEBLOCKFREQUENCYINFO_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock & MBB
#define LLVM_ABI
Definition: Compiler.h:213
std::string Name
#define F(x, y, z)
Definition: MD5.cpp:55
static bool isSimple(Instruction *I)
raw_pwrite_stream & OS
API to communicate dependencies between analyses during invalidation.
Definition: PassManager.h:294
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:255
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.
LLVM_ABI Result run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
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...
LLVM_ABI void view(const Twine &Name, bool isSimple=true) const
Pop up a ghostview window with the current block frequency propagation rendered using dot.
LLVM_ABI void print(raw_ostream &OS)
LLVM_ABI bool isIrrLoopHeader(const MachineBasicBlock *MBB) const
LLVM_ABI const MachineBranchProbabilityInfo * getMBPI() const
LLVM_ABI BlockFrequency getBlockFreq(const MachineBasicBlock *MBB) const
getblockFreq - Return block frequency.
LLVM_ABI void onEdgeSplit(const MachineBasicBlock &NewPredecessor, const MachineBasicBlock &NewSuccessor, const MachineBranchProbabilityInfo &MBPI)
incrementally calculate block frequencies when we split edges, to avoid full CFG traversal.
LLVM_ABI void calculate(const MachineFunction &F, const MachineBranchProbabilityInfo &MBPI, const MachineLoopInfo &MLI)
calculate - compute block frequency info for the given function.
LLVM_ABI const MachineFunction * getFunction() const
double getBlockFreqRelativeToEntryBlock(const MachineBasicBlock *MBB) const
Compute the frequency of the block, relative to the entry block.
LLVM_ABI std::optional< uint64_t > getProfileCountFromFreq(BlockFrequency Freq) const
LLVM_ABI bool invalidate(MachineFunction &F, const PreservedAnalyses &PA, MachineFunctionAnalysisManager::Invalidator &)
Handle invalidation explicitly.
LLVM_ABI BlockFrequency getEntryFreq() const
Divide a block's BlockFrequency::getFrequency() value by this value to obtain the entry block - relat...
LLVM_ABI std::optional< uint64_t > getBlockProfileCount(const MachineBasicBlock *MBB) const
LLVM_ABI MachineBlockFrequencyInfo(MachineBlockFrequencyInfo &&)
Printer pass for the MachineBlockFrequencyInfo results.
LLVM_ABI 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:112
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:82
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
LLVM_ABI 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:93
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition: Analysis.h:29
A CRTP mix-in to automatically provide informational APIs needed for passes.
Definition: PassManager.h:70