LLVM 22.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
19
20namespace llvm {
30
31class BasicBlock;
32class BlockFrequencyInfo;
33class Function;
34
35enum class PGSOQueryType {
36 IRPass, // A query call from an IR-level transform pass.
37 Test, // A query call from a unit test.
38 Other, // Others.
39};
40
41static inline bool isPGSOColdCodeOnly(ProfileSummaryInfo *PSI) {
42 return PGSOColdCodeOnly ||
44 (PSI->hasSampleProfile() &&
49}
50
51template <typename FuncT, typename BFIT>
53 BFIT *BFI, PGSOQueryType QueryType) {
54 assert(F);
55 if (!PSI || !BFI || !PSI->hasProfileSummary())
56 return false;
57 if (ForcePGSO)
58 return true;
59 if (!EnablePGSO)
60 return false;
61 if (isPGSOColdCodeOnly(PSI))
62 return PSI->isFunctionColdInCallGraph(F, *BFI);
63 if (PSI->hasSampleProfile())
64 // The "isCold" check seems to work better for Sample PGO as it could have
65 // many profile-unannotated functions.
67 *BFI);
69 *BFI);
70}
71
72template <typename BlockTOrBlockFreq, typename BFIT>
73bool shouldOptimizeForSizeImpl(BlockTOrBlockFreq BBOrBlockFreq,
74 ProfileSummaryInfo *PSI, BFIT *BFI,
75 PGSOQueryType QueryType) {
76 if (!PSI || !BFI || !PSI->hasProfileSummary())
77 return false;
78 if (ForcePGSO)
79 return true;
80 if (!EnablePGSO)
81 return false;
82 if (isPGSOColdCodeOnly(PSI))
83 return PSI->isColdBlock(BBOrBlockFreq, BFI);
84 if (PSI->hasSampleProfile())
85 // The "isCold" check seems to work better for Sample PGO as it could have
86 // many profile-unannotated functions.
87 return PSI->isColdBlockNthPercentile(PgsoCutoffSampleProf, BBOrBlockFreq,
88 BFI);
89 return !PSI->isHotBlockNthPercentile(PgsoCutoffInstrProf, BBOrBlockFreq, BFI);
90}
91
92/// Returns true if function \p F is suggested to be size-optimized based on the
93/// profile.
94LLVM_ABI bool
95shouldOptimizeForSize(const Function *F, ProfileSummaryInfo *PSI,
96 BlockFrequencyInfo *BFI,
98
99/// Returns true if basic block \p BB is suggested to be size-optimized based on
100/// the profile.
101LLVM_ABI bool
102shouldOptimizeForSize(const BasicBlock *BB, ProfileSummaryInfo *PSI,
103 BlockFrequencyInfo *BFI,
105
106} // end namespace llvm
107
108#endif // LLVM_TRANSFORMS_UTILS_SIZEOPTS_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define LLVM_ABI
Definition: Compiler.h:213
#define F(x, y, z)
Definition: MD5.cpp:55
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.
LLVM_ABI bool hasPartialSampleProfile() const
Returns true if module M has partial-profile sample profile.
LLVM_ABI 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:81
template class LLVM_TEMPLATE_ABI opt< bool >
Definition: CommandLine.cpp:79
template class LLVM_TEMPLATE_ABI opt< int >
Definition: CommandLine.cpp:81
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
LLVM_ABI cl::opt< bool > PGSOColdCodeOnlyForSamplePGO
LLVM_ABI cl::opt< bool > PGSOColdCodeOnlyForInstrPGO
LLVM_ABI 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:52
LLVM_ABI cl::opt< int > PgsoCutoffSampleProf
LLVM_ABI cl::opt< bool > EnablePGSO
LLVM_ABI cl::opt< bool > PGSOColdCodeOnlyForPartialSamplePGO
LLVM_ABI cl::opt< int > PgsoCutoffInstrProf
bool shouldOptimizeForSizeImpl(BlockTOrBlockFreq BBOrBlockFreq, ProfileSummaryInfo *PSI, BFIT *BFI, PGSOQueryType QueryType)
Definition: SizeOpts.h:73
@ Other
Any other memory.
LLVM_ABI cl::opt< bool > ForcePGSO
LLVM_ABI cl::opt< bool > PGSOLargeWorkingSetSizeOnly
PGSOQueryType
Definition: SizeOpts.h:35
static bool isPGSOColdCodeOnly(ProfileSummaryInfo *PSI)
Definition: SizeOpts.h:41
LLVM_ABI cl::opt< bool > PGSOColdCodeOnly