LLVM 19.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
18#include <cstdint>
19#include <memory>
20#include <optional>
21
22namespace llvm {
23
24template <class BlockT> class BlockFrequencyInfoImpl;
25class MachineBasicBlock;
26class MachineBranchProbabilityInfo;
27class MachineFunction;
28class MachineLoopInfo;
29class raw_ostream;
30
31/// MachineBlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation
32/// to estimate machine basic block frequencies.
35 std::unique_ptr<ImplType> MBFI;
36
37public:
38 static char ID;
39
43 MachineLoopInfo &MLI);
45
46 void getAnalysisUsage(AnalysisUsage &AU) const override;
47
49
50 /// calculate - compute block frequency info for the given function.
51 void calculate(const MachineFunction &F,
53 const MachineLoopInfo &MLI);
54
55 void releaseMemory() override;
56
57 /// getblockFreq - Return block frequency. Return 0 if we don't have the
58 /// information. Please note that initial frequency is equal to 1024. It means
59 /// that we should not rely on the value itself, but only on the comparison to
60 /// the other block frequencies. We do this to avoid using of floating points.
61 /// For example, to get the frequency of a block relative to the entry block,
62 /// divide the integral value returned by this function (the
63 /// BlockFrequency::getFrequency() value) by getEntryFreq().
65
66 /// Compute the frequency of the block, relative to the entry block.
67 /// This API assumes getEntryFreq() is non-zero.
70 "getEntryFreq() should not return 0 here!");
71 return static_cast<double>(getBlockFreq(MBB).getFrequency()) /
72 static_cast<double>(getEntryFreq().getFrequency());
73 }
74
75 std::optional<uint64_t>
77 std::optional<uint64_t> getProfileCountFromFreq(BlockFrequency Freq) const;
78
79 bool isIrrLoopHeader(const MachineBasicBlock *MBB) const;
80
81 /// incrementally calculate block frequencies when we split edges, to avoid
82 /// full CFG traversal.
83 void onEdgeSplit(const MachineBasicBlock &NewPredecessor,
84 const MachineBasicBlock &NewSuccessor,
86
87 const MachineFunction *getFunction() const;
89
90 /// Pop up a ghostview window with the current block frequency propagation
91 /// rendered using dot.
92 void view(const Twine &Name, bool isSimple = true) const;
93
94 /// Divide a block's BlockFrequency::getFrequency() value by this value to
95 /// obtain the entry block - relative frequency of said block.
97};
98
99/// Print the block frequency @p Freq relative to the current functions entry
100/// frequency. Returns a Printable object that can be piped via `<<` to a
101/// `raw_ostream`.
102Printable printBlockFreq(const MachineBlockFrequencyInfo &MBFI,
103 BlockFrequency Freq);
104
105/// Convenience function equivalent to calling
106/// `printBlockFreq(MBFI, MBFI.getBlockFreq(&MBB))`.
107Printable printBlockFreq(const MachineBlockFrequencyInfo &MBFI,
108 const MachineBasicBlock &MBB);
109
110} // end namespace llvm
111
112#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)
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.
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
void releaseMemory() override
releaseMemory() - This member can be implemented by a pass if it wants to be able to release its memo...
const MachineBranchProbabilityInfo * getMBPI() const
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
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.
bool runOnMachineFunction(MachineFunction &F) override
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
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
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
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
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.