LLVM  14.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 
20 namespace llvm {
21 extern cl::opt<bool> EnablePGSO;
22 extern cl::opt<bool> PGSOLargeWorkingSetSizeOnly;
23 extern cl::opt<bool> PGSOColdCodeOnly;
24 extern cl::opt<bool> PGSOColdCodeOnlyForInstrPGO;
25 extern cl::opt<bool> PGSOColdCodeOnlyForSamplePGO;
26 extern cl::opt<bool> PGSOColdCodeOnlyForPartialSamplePGO;
27 extern cl::opt<bool> ForcePGSO;
28 extern cl::opt<int> PgsoCutoffInstrProf;
29 extern cl::opt<int> PgsoCutoffSampleProf;
30 
31 class BasicBlock;
32 class BlockFrequencyInfo;
33 class Function;
34 
35 enum 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 
41 static inline bool isPGSOColdCodeOnly(ProfileSummaryInfo *PSI) {
42  return PGSOColdCodeOnly ||
44  (PSI->hasSampleProfile() &&
46  (PSI->hasPartialSampleProfile() &&
49 }
50 
51 template<typename AdapterT, 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 AdapterT::isFunctionColdInCallGraph(F, PSI, *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.
66  return AdapterT::isFunctionColdInCallGraphNthPercentile(
67  PgsoCutoffSampleProf, F, PSI, *BFI);
68  return !AdapterT::isFunctionHotInCallGraphNthPercentile(PgsoCutoffInstrProf,
69  F, PSI, *BFI);
70 }
71 
72 template<typename AdapterT, typename BlockTOrBlockFreq, typename BFIT>
73 bool shouldOptimizeForSizeImpl(BlockTOrBlockFreq BBOrBlockFreq, ProfileSummaryInfo *PSI,
74  BFIT *BFI, 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 AdapterT::isColdBlock(BBOrBlockFreq, PSI, 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 AdapterT::isColdBlockNthPercentile(PgsoCutoffSampleProf,
87  BBOrBlockFreq, PSI, BFI);
88  return !AdapterT::isHotBlockNthPercentile(PgsoCutoffInstrProf, BBOrBlockFreq,
89  PSI, BFI);
90 }
91 
92 /// Returns true if function \p F is suggested to be size-optimized based on the
93 /// profile.
94 bool shouldOptimizeForSize(const Function *F, ProfileSummaryInfo *PSI,
95  BlockFrequencyInfo *BFI,
97 
98 /// Returns true if basic block \p BB is suggested to be size-optimized based on
99 /// the profile.
100 bool shouldOptimizeForSize(const BasicBlock *BB, ProfileSummaryInfo *PSI,
101  BlockFrequencyInfo *BFI,
102  PGSOQueryType QueryType = PGSOQueryType::Other);
103 
104 } // end namespace llvm
105 
106 #endif // LLVM_TRANSFORMS_UTILS_SIZEOPTS_H
llvm
This file implements support for optimizing divisions by a constant.
Definition: AllocatorList.h:23
llvm::ProfileSummaryInfo::hasPartialSampleProfile
bool hasPartialSampleProfile() const
Returns true if module M has partial-profile sample profile.
Definition: ProfileSummaryInfo.cpp:375
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:56
CommandLine.h
llvm::isPGSOColdCodeOnly
static bool isPGSOColdCodeOnly(ProfileSummaryInfo *PSI)
Definition: SizeOpts.h:41
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:35
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:73
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
isColdBlock
static bool isColdBlock(const MachineBasicBlock &MBB, const MachineBlockFrequencyInfo *MBFI, ProfileSummaryInfo *PSI)
Definition: MachineFunctionSplitter.cpp:81
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::ReplayInlineScope::Function
@ Function
BlockFrequencyInfo.h
llvm::AMDGPUISD::BFI
@ BFI
Definition: AMDGPUISelLowering.h:421
llvm::ProfileSummaryInfo::hasLargeWorkingSetSize
bool hasLargeWorkingSetSize() const
Returns true if the working set size of the code is considered large.
Definition: ProfileSummaryInfo.cpp:286
llvm::shouldFuncOptimizeForSizeImpl
bool shouldFuncOptimizeForSizeImpl(const FuncT *F, ProfileSummaryInfo *PSI, BFIT *BFI, PGSOQueryType QueryType)
Definition: SizeOpts.h:52
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