LLVM 20.0.0git
SizeOpts.h
Go to the documentation of this file.
1//===- llvm/Transforms/Utils/SizeOpts.h - size optimization -----*- 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// This file contains some shared code size optimization related code.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_TRANSFORMS_UTILS_SIZEOPTS_H
14#define LLVM_TRANSFORMS_UTILS_SIZEOPTS_H
15
18
19namespace llvm {
20extern cl::opt<bool> EnablePGSO;
21extern cl::opt<bool> PGSOLargeWorkingSetSizeOnly;
22extern cl::opt<bool> PGSOColdCodeOnly;
23extern cl::opt<bool> PGSOColdCodeOnlyForInstrPGO;
24extern cl::opt<bool> PGSOColdCodeOnlyForSamplePGO;
25extern cl::opt<bool> PGSOColdCodeOnlyForPartialSamplePGO;
26extern cl::opt<bool> ForcePGSO;
27extern cl::opt<int> PgsoCutoffInstrProf;
28extern cl::opt<int> PgsoCutoffSampleProf;
29
30class BasicBlock;
31class BlockFrequencyInfo;
32class Function;
33
34enum class PGSOQueryType {
35 IRPass, // A query call from an IR-level transform pass.
36 Test, // A query call from a unit test.
37 Other, // Others.
38};
39
40static inline bool isPGSOColdCodeOnly(ProfileSummaryInfo *PSI) {
41 return PGSOColdCodeOnly ||
43 (PSI->hasSampleProfile() &&
48}
49
50template <typename FuncT, typename BFIT>
52 BFIT *BFI, PGSOQueryType QueryType) {
53 assert(F);
54 if (!PSI || !BFI || !PSI->hasProfileSummary())
55 return false;
56 if (ForcePGSO)
57 return true;
58 if (!EnablePGSO)
59 return false;
60 if (isPGSOColdCodeOnly(PSI))
61 return PSI->isFunctionColdInCallGraph(F, *BFI);
62 if (PSI->hasSampleProfile())
63 // The "isCold" check seems to work better for Sample PGO as it could have
64 // many profile-unannotated functions.
66 *BFI);
68 *BFI);
69}
70
71template <typename BlockTOrBlockFreq, typename BFIT>
72bool shouldOptimizeForSizeImpl(BlockTOrBlockFreq BBOrBlockFreq,
73 ProfileSummaryInfo *PSI, BFIT *BFI,
74 PGSOQueryType QueryType) {
75 if (!PSI || !BFI || !PSI->hasProfileSummary())
76 return false;
77 if (ForcePGSO)
78 return true;
79 if (!EnablePGSO)
80 return false;
81 if (isPGSOColdCodeOnly(PSI))
82 return PSI->isColdBlock(BBOrBlockFreq, BFI);
83 if (PSI->hasSampleProfile())
84 // The "isCold" check seems to work better for Sample PGO as it could have
85 // many profile-unannotated functions.
86 return PSI->isColdBlockNthPercentile(PgsoCutoffSampleProf, BBOrBlockFreq,
87 BFI);
88 return !PSI->isHotBlockNthPercentile(PgsoCutoffInstrProf, BBOrBlockFreq, BFI);
89}
90
91/// Returns true if function \p F is suggested to be size-optimized based on the
92/// profile.
93bool shouldOptimizeForSize(const Function *F, ProfileSummaryInfo *PSI,
94 BlockFrequencyInfo *BFI,
96
97/// Returns true if basic block \p BB is suggested to be size-optimized based on
98/// the profile.
99bool shouldOptimizeForSize(const BasicBlock *BB, ProfileSummaryInfo *PSI,
100 BlockFrequencyInfo *BFI,
102
103} // end namespace llvm
104
105#endif // LLVM_TRANSFORMS_UTILS_SIZEOPTS_H
#define F(x, y, z)
Definition: MD5.cpp:55
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Analysis providing profile information.
bool hasProfileSummary() const
Returns true if profile summary is available.
bool isHotBlockNthPercentile(int PercentileCutoff, const BBType *BB, BFIT *BFI) const
bool isFunctionColdInCallGraph(const FuncT *F, BFIT &BFI) const
Returns true if F contains only cold code.
bool hasInstrumentationProfile() const
Returns true if module M has instrumentation profile.
bool isFunctionColdInCallGraphNthPercentile(int PercentileCutoff, const FuncT *F, BFIT &BFI) const
Returns true if F contains cold code with regard to a given cold percentile cutoff value.
bool hasSampleProfile() const
Returns true if module M has sample profile.
bool isColdBlock(const BBType *BB, BFIT *BFI) const
Returns true if BasicBlock BB is considered cold.
bool isFunctionHotInCallGraphNthPercentile(int PercentileCutoff, const FuncT *F, BFIT &BFI) const
Returns true if F contains hot code with regard to a given hot percentile cutoff value.
bool hasPartialSampleProfile() const
Returns true if module M has partial-profile sample profile.
bool hasLargeWorkingSetSize() const
Returns true if the working set size of the code is considered large.
bool isColdBlockNthPercentile(int PercentileCutoff, const BBType *BB, BFIT *BFI) const
Returns true if BasicBlock BB is considered cold with regard to a given cold percentile cutoff value.
@ BasicBlock
Various leaf nodes.
Definition: ISDOpcodes.h:71
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
cl::opt< bool > PGSOColdCodeOnlyForSamplePGO
cl::opt< bool > PGSOColdCodeOnlyForInstrPGO
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.
bool shouldFuncOptimizeForSizeImpl(const FuncT *F, ProfileSummaryInfo *PSI, BFIT *BFI, PGSOQueryType QueryType)
Definition: SizeOpts.h:51
cl::opt< int > PgsoCutoffSampleProf
cl::opt< bool > EnablePGSO
cl::opt< bool > PGSOColdCodeOnlyForPartialSamplePGO
cl::opt< int > PgsoCutoffInstrProf
bool shouldOptimizeForSizeImpl(BlockTOrBlockFreq BBOrBlockFreq, ProfileSummaryInfo *PSI, BFIT *BFI, PGSOQueryType QueryType)
Definition: SizeOpts.h:72
@ Other
Any other memory.
cl::opt< bool > ForcePGSO
cl::opt< bool > PGSOLargeWorkingSetSizeOnly
PGSOQueryType
Definition: SizeOpts.h:34
static bool isPGSOColdCodeOnly(ProfileSummaryInfo *PSI)
Definition: SizeOpts.h:40
cl::opt< bool > PGSOColdCodeOnly