LLVM 17.0.0git
MachineSizeOpts.cpp
Go to the documentation of this file.
1//===- MachineSizeOpts.cpp - code size optimization related code ----------===//
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// This file contains some shared machine IR code size optimization related
10// code.
11//
12//===----------------------------------------------------------------------===//
13
18
19using namespace llvm;
20
26
27namespace {
29
30/// Like ProfileSummaryInfo::isColdBlock but for MachineBasicBlock.
33 const MachineBlockFrequencyInfo *MBFI) {
34 auto Count = MBFI->getBlockProfileCount(MBB);
35 return Count && PSI->isColdCount(*Count);
36}
37
38bool isColdBlock(BlockFrequency BlockFreq,
40 const MachineBlockFrequencyInfo *MBFI) {
41 auto Count = MBFI->getProfileCountFromFreq(BlockFreq.getFrequency());
42 return Count && PSI->isColdCount(*Count);
43}
44
45/// Like ProfileSummaryInfo::isHotBlockNthPercentile but for MachineBasicBlock.
46static bool isHotBlockNthPercentile(int PercentileCutoff,
49 const MachineBlockFrequencyInfo *MBFI) {
50 auto Count = MBFI->getBlockProfileCount(MBB);
51 return Count && PSI->isHotCountNthPercentile(PercentileCutoff, *Count);
52}
53
54static bool isHotBlockNthPercentile(int PercentileCutoff,
55 BlockFrequency BlockFreq,
57 const MachineBlockFrequencyInfo *MBFI) {
58 auto Count = MBFI->getProfileCountFromFreq(BlockFreq.getFrequency());
59 return Count && PSI->isHotCountNthPercentile(PercentileCutoff, *Count);
60}
61
62static bool isColdBlockNthPercentile(int PercentileCutoff,
65 const MachineBlockFrequencyInfo *MBFI) {
66 auto Count = MBFI->getBlockProfileCount(MBB);
67 return Count && PSI->isColdCountNthPercentile(PercentileCutoff, *Count);
68}
69
70static bool isColdBlockNthPercentile(int PercentileCutoff,
71 BlockFrequency BlockFreq,
73 const MachineBlockFrequencyInfo *MBFI) {
74 auto Count = MBFI->getProfileCountFromFreq(BlockFreq.getFrequency());
75 return Count && PSI->isColdCountNthPercentile(PercentileCutoff, *Count);
76}
77
78/// Like ProfileSummaryInfo::isFunctionColdInCallGraph but for
79/// MachineFunction.
80bool isFunctionColdInCallGraph(
81 const MachineFunction *MF,
83 const MachineBlockFrequencyInfo &MBFI) {
84 if (auto FunctionCount = MF->getFunction().getEntryCount())
85 if (!PSI->isColdCount(FunctionCount->getCount()))
86 return false;
87 for (const auto &MBB : *MF)
88 if (!isColdBlock(&MBB, PSI, &MBFI))
89 return false;
90 return true;
91}
92
93/// Like ProfileSummaryInfo::isFunctionHotInCallGraphNthPercentile but for
94/// MachineFunction.
95bool isFunctionHotInCallGraphNthPercentile(
97 const MachineFunction *MF,
99 const MachineBlockFrequencyInfo &MBFI) {
100 if (auto FunctionCount = MF->getFunction().getEntryCount())
102 FunctionCount->getCount()))
103 return true;
104 for (const auto &MBB : *MF)
105 if (isHotBlockNthPercentile(PercentileCutoff, &MBB, PSI, &MBFI))
106 return true;
107 return false;
108}
109
110bool isFunctionColdInCallGraphNthPercentile(
112 const MachineBlockFrequencyInfo &MBFI) {
113 if (auto FunctionCount = MF->getFunction().getEntryCount())
115 FunctionCount->getCount()))
116 return false;
117 for (const auto &MBB : *MF)
118 if (!isColdBlockNthPercentile(PercentileCutoff, &MBB, PSI, &MBFI))
119 return false;
120 return true;
121}
122} // namespace machine_size_opts_detail
123
124struct MachineBasicBlockBFIAdapter {
125 static bool isFunctionColdInCallGraph(const MachineFunction *MF,
127 const MachineBlockFrequencyInfo &MBFI) {
128 return machine_size_opts_detail::isFunctionColdInCallGraph(MF, PSI, MBFI);
129 }
130 static bool isFunctionHotInCallGraphNthPercentile(
131 int CutOff,
132 const MachineFunction *MF,
134 const MachineBlockFrequencyInfo &MBFI) {
135 return machine_size_opts_detail::isFunctionHotInCallGraphNthPercentile(
136 CutOff, MF, PSI, MBFI);
137 }
138 static bool isFunctionColdInCallGraphNthPercentile(
139 int CutOff, const MachineFunction *MF, ProfileSummaryInfo *PSI,
140 const MachineBlockFrequencyInfo &MBFI) {
141 return machine_size_opts_detail::isFunctionColdInCallGraphNthPercentile(
142 CutOff, MF, PSI, MBFI);
143 }
144 static bool isColdBlock(const MachineBasicBlock *MBB,
146 const MachineBlockFrequencyInfo *MBFI) {
147 return machine_size_opts_detail::isColdBlock(MBB, PSI, MBFI);
148 }
149 static bool isColdBlock(BlockFrequency BlockFreq,
151 const MachineBlockFrequencyInfo *MBFI) {
152 return machine_size_opts_detail::isColdBlock(BlockFreq, PSI, MBFI);
153 }
154 static bool isHotBlockNthPercentile(int CutOff,
155 const MachineBasicBlock *MBB,
157 const MachineBlockFrequencyInfo *MBFI) {
158 return machine_size_opts_detail::isHotBlockNthPercentile(
159 CutOff, MBB, PSI, MBFI);
160 }
161 static bool isHotBlockNthPercentile(int CutOff,
162 BlockFrequency BlockFreq,
164 const MachineBlockFrequencyInfo *MBFI) {
165 return machine_size_opts_detail::isHotBlockNthPercentile(
166 CutOff, BlockFreq, PSI, MBFI);
167 }
168 static bool isColdBlockNthPercentile(int CutOff, const MachineBasicBlock *MBB,
170 const MachineBlockFrequencyInfo *MBFI) {
171 return machine_size_opts_detail::isColdBlockNthPercentile(CutOff, MBB, PSI,
172 MBFI);
173 }
174 static bool isColdBlockNthPercentile(int CutOff, BlockFrequency BlockFreq,
176 const MachineBlockFrequencyInfo *MBFI) {
177 return machine_size_opts_detail::isColdBlockNthPercentile(CutOff, BlockFreq,
178 PSI, MBFI);
179 }
180};
181} // end anonymous namespace
182
185 const MachineBlockFrequencyInfo *MBFI,
186 PGSOQueryType QueryType) {
187 return shouldFuncOptimizeForSizeImpl<MachineBasicBlockBFIAdapter>(
188 MF, PSI, MBFI, QueryType);
189}
190
193 const MachineBlockFrequencyInfo *MBFI,
194 PGSOQueryType QueryType) {
195 assert(MBB);
196 return shouldOptimizeForSizeImpl<MachineBasicBlockBFIAdapter>(
197 MBB, PSI, MBFI, QueryType);
198}
199
202 MBFIWrapper *MBFIW,
203 PGSOQueryType QueryType) {
204 assert(MBB);
205 if (!PSI || !MBFIW)
206 return false;
207 BlockFrequency BlockFreq = MBFIW->getBlockFreq(MBB);
208 return shouldOptimizeForSizeImpl<MachineBasicBlockBFIAdapter>(
209 BlockFreq, PSI, &MBFIW->getMBFI(), QueryType);
210}
MachineBasicBlock & MBB
static bool isColdBlock(const MachineBasicBlock &MBB, const MachineBlockFrequencyInfo *MBFI, ProfileSummaryInfo *PSI)
static cl::opt< unsigned > PercentileCutoff("mfs-psi-cutoff", cl::desc("Percentile profile summary cutoff used to " "determine cold blocks. Unused if set to zero."), cl::init(999950), cl::Hidden)
cl::opt< bool > PGSOLargeWorkingSetSizeOnly
cl::opt< bool > ForcePGSO
cl::opt< int > PgsoCutoffSampleProf
cl::opt< bool > EnablePGSO
cl::opt< int > PgsoCutoffInstrProf
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
uint64_t getFrequency() const
Returns the frequency as a fixpoint number scaled by the entry frequency.
std::optional< ProfileCount > getEntryCount(bool AllowSynthetic=false) const
Get the entry count for this function.
Definition: Function.cpp:2047
const MachineBlockFrequencyInfo & getMBFI()
Definition: MBFIWrapper.h:42
BlockFrequency getBlockFreq(const MachineBasicBlock *MBB) const
Definition: MBFIWrapper.cpp:20
MachineBlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate machine basic b...
std::optional< uint64_t > getBlockProfileCount(const MachineBasicBlock *MBB) const
std::optional< uint64_t > getProfileCountFromFreq(uint64_t Freq) const
Function & getFunction()
Return the LLVM function that this machine code represents.
Analysis providing profile information.
bool isColdCount(uint64_t C) const
Returns true if count C is considered cold.
bool isColdCountNthPercentile(int PercentileCutoff, uint64_t C) const
Returns true if count C is considered cold with regard to a given cold percentile cutoff value.
bool isHotCountNthPercentile(int PercentileCutoff, uint64_t C) const
Returns true if count C is considered hot with regard to a given hot percentile cutoff value.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
bool shouldOptimizeForSize(const MachineFunction *MF, ProfileSummaryInfo *PSI, const MachineBlockFrequencyInfo *BFI, PGSOQueryType QueryType=PGSOQueryType::Other)
Returns true if machine function MF is suggested to be size-optimized based on the profile.
PGSOQueryType
Definition: SizeOpts.h:34