LLVM  15.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 
19 namespace llvm {
20 extern cl::opt<bool> EnablePGSO;
21 extern cl::opt<bool> PGSOLargeWorkingSetSizeOnly;
22 extern cl::opt<bool> PGSOColdCodeOnly;
23 extern cl::opt<bool> PGSOColdCodeOnlyForInstrPGO;
24 extern cl::opt<bool> PGSOColdCodeOnlyForSamplePGO;
25 extern cl::opt<bool> PGSOColdCodeOnlyForPartialSamplePGO;
26 extern cl::opt<bool> ForcePGSO;
27 extern cl::opt<int> PgsoCutoffInstrProf;
28 extern cl::opt<int> PgsoCutoffSampleProf;
29 
30 class BasicBlock;
31 class BlockFrequencyInfo;
32 class Function;
33 
34 enum 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 
40 static inline bool isPGSOColdCodeOnly(ProfileSummaryInfo *PSI) {
41  return PGSOColdCodeOnly ||
43  (PSI->hasSampleProfile() &&
45  (PSI->hasPartialSampleProfile() &&
48 }
49 
50 template<typename AdapterT, 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 AdapterT::isFunctionColdInCallGraph(F, PSI, *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.
65  return AdapterT::isFunctionColdInCallGraphNthPercentile(
66  PgsoCutoffSampleProf, F, PSI, *BFI);
67  return !AdapterT::isFunctionHotInCallGraphNthPercentile(PgsoCutoffInstrProf,
68  F, PSI, *BFI);
69 }
70 
71 template<typename AdapterT, typename BlockTOrBlockFreq, typename BFIT>
72 bool shouldOptimizeForSizeImpl(BlockTOrBlockFreq BBOrBlockFreq, ProfileSummaryInfo *PSI,
73  BFIT *BFI, PGSOQueryType QueryType) {
74  if (!PSI || !BFI || !PSI->hasProfileSummary())
75  return false;
76  if (ForcePGSO)
77  return true;
78  if (!EnablePGSO)
79  return false;
80  if (isPGSOColdCodeOnly(PSI))
81  return AdapterT::isColdBlock(BBOrBlockFreq, PSI, BFI);
82  if (PSI->hasSampleProfile())
83  // The "isCold" check seems to work better for Sample PGO as it could have
84  // many profile-unannotated functions.
85  return AdapterT::isColdBlockNthPercentile(PgsoCutoffSampleProf,
86  BBOrBlockFreq, PSI, BFI);
87  return !AdapterT::isHotBlockNthPercentile(PgsoCutoffInstrProf, BBOrBlockFreq,
88  PSI, BFI);
89 }
90 
91 /// Returns true if function \p F is suggested to be size-optimized based on the
92 /// profile.
93 bool 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.
99 bool shouldOptimizeForSize(const BasicBlock *BB, ProfileSummaryInfo *PSI,
100  BlockFrequencyInfo *BFI,
101  PGSOQueryType QueryType = PGSOQueryType::Other);
102 
103 } // end namespace llvm
104 
105 #endif // LLVM_TRANSFORMS_UTILS_SIZEOPTS_H
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:17
llvm::ProfileSummaryInfo::hasPartialSampleProfile
bool hasPartialSampleProfile() const
Returns true if module M has partial-profile sample profile.
Definition: ProfileSummaryInfo.cpp:374
llvm::ProfileSummaryInfo::hasProfileSummary
bool hasProfileSummary() const
Returns true if profile summary is available.
Definition: ProfileSummaryInfo.h:68
llvm::EnablePGSO
cl::opt< bool > EnablePGSO
llvm::PGSOColdCodeOnlyForInstrPGO
cl::opt< bool > PGSOColdCodeOnlyForInstrPGO
F
#define F(x, y, z)
Definition: MD5.cpp:55
CommandLine.h
llvm::isPGSOColdCodeOnly
static bool isPGSOColdCodeOnly(ProfileSummaryInfo *PSI)
Definition: SizeOpts.h:40
llvm::shouldOptimizeForSize
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.
Definition: MachineSizeOpts.cpp:183
llvm::PGSOQueryType::Test
@ Test
llvm::PGSOColdCodeOnlyForSamplePGO
cl::opt< bool > PGSOColdCodeOnlyForSamplePGO
llvm::PGSOQueryType::IRPass
@ IRPass
llvm::ProfileSummaryInfo
Analysis providing profile information.
Definition: ProfileSummaryInfo.h:39
llvm::PgsoCutoffInstrProf
cl::opt< int > PgsoCutoffInstrProf
ProfileSummaryInfo.h
llvm::PGSOQueryType
PGSOQueryType
Definition: SizeOpts.h:34
llvm::ForcePGSO
cl::opt< bool > ForcePGSO
llvm::ProfileSummaryInfo::hasSampleProfile
bool hasSampleProfile() const
Returns true if module M has sample profile.
Definition: ProfileSummaryInfo.h:71
llvm::shouldOptimizeForSizeImpl
bool shouldOptimizeForSizeImpl(BlockTOrBlockFreq BBOrBlockFreq, ProfileSummaryInfo *PSI, BFIT *BFI, PGSOQueryType QueryType)
Definition: SizeOpts.h:72
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
isColdBlock
static bool isColdBlock(const MachineBasicBlock &MBB, const MachineBlockFrequencyInfo *MBFI, ProfileSummaryInfo *PSI)
Definition: MachineFunctionSplitter.cpp:79
llvm::ProfileSummaryInfo::hasInstrumentationProfile
bool hasInstrumentationProfile() const
Returns true if module M has instrumentation profile.
Definition: ProfileSummaryInfo.h:77
llvm::ISD::BasicBlock
@ BasicBlock
Various leaf nodes.
Definition: ISDOpcodes.h:71
llvm::AMDGPUISD::BFI
@ BFI
Definition: AMDGPUISelLowering.h:429
llvm::ProfileSummaryInfo::hasLargeWorkingSetSize
bool hasLargeWorkingSetSize() const
Returns true if the working set size of the code is considered large.
Definition: ProfileSummaryInfo.cpp:285
llvm::shouldFuncOptimizeForSizeImpl
bool shouldFuncOptimizeForSizeImpl(const FuncT *F, ProfileSummaryInfo *PSI, BFIT *BFI, PGSOQueryType QueryType)
Definition: SizeOpts.h:51
llvm::PGSOLargeWorkingSetSizeOnly
cl::opt< bool > PGSOLargeWorkingSetSizeOnly
llvm::PGSOColdCodeOnly
cl::opt< bool > PGSOColdCodeOnly
llvm::PGSOQueryType::Other
@ Other
BB
Common register allocation spilling lr str ldr sxth r3 ldr mla r4 can lr mov lr str ldr sxth r3 mla r4 and then merge mul and lr str ldr sxth r3 mla r4 It also increase the likelihood the store may become dead bb27 Successors according to LLVM BB
Definition: README.txt:39
llvm::PgsoCutoffSampleProf
cl::opt< int > PgsoCutoffSampleProf
llvm::PGSOColdCodeOnlyForPartialSamplePGO
cl::opt< bool > PGSOColdCodeOnlyForPartialSamplePGO
llvm::codeview::PublicSymFlags::Function
@ Function