LLVM  11.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 
30 
31 namespace llvm {
32 
33 class BasicBlock;
34 class BlockFrequencyInfo;
35 class Function;
36 
37 enum class PGSOQueryType {
38  IRPass, // A query call from an IR-level transform pass.
39  Test, // A query call from a unit test.
40  Other, // Others.
41 };
42 
43 static inline bool isPGSOColdCodeOnly(ProfileSummaryInfo *PSI) {
44  return PGSOColdCodeOnly ||
46  (PSI->hasSampleProfile() &&
48  (PSI->hasPartialSampleProfile() &&
50  (PGSOLargeWorkingSetSizeOnly && !PSI->hasLargeWorkingSetSize());
51 }
52 
53 template<typename AdapterT, typename FuncT, typename BFIT>
55  BFIT *BFI, PGSOQueryType QueryType) {
56  assert(F);
57  if (!PSI || !BFI || !PSI->hasProfileSummary())
58  return false;
59  if (ForcePGSO)
60  return true;
61  if (!EnablePGSO)
62  return false;
63  // Temporarily enable size optimizations only for the IR pass or test query
64  // sites for gradual commit/rollout. This is to be removed later.
65  if (PGSOIRPassOrTestOnly && !(QueryType == PGSOQueryType::IRPass ||
66  QueryType == PGSOQueryType::Test))
67  return false;
68  if (isPGSOColdCodeOnly(PSI))
69  return AdapterT::isFunctionColdInCallGraph(F, PSI, *BFI);
70  if (PSI->hasSampleProfile())
71  // The "isCold" check seems to work better for Sample PGO as it could have
72  // many profile-unannotated functions.
73  return AdapterT::isFunctionColdInCallGraphNthPercentile(
74  PgsoCutoffSampleProf, F, PSI, *BFI);
75  return !AdapterT::isFunctionHotInCallGraphNthPercentile(PgsoCutoffInstrProf,
76  F, PSI, *BFI);
77 }
78 
79 template<typename AdapterT, typename BlockTOrBlockFreq, typename BFIT>
80 bool shouldOptimizeForSizeImpl(BlockTOrBlockFreq BBOrBlockFreq, ProfileSummaryInfo *PSI,
81  BFIT *BFI, PGSOQueryType QueryType) {
82  if (!PSI || !BFI || !PSI->hasProfileSummary())
83  return false;
84  if (ForcePGSO)
85  return true;
86  if (!EnablePGSO)
87  return false;
88  // Temporarily enable size optimizations only for the IR pass or test query
89  // sites for gradual commit/rollout. This is to be removed later.
90  if (PGSOIRPassOrTestOnly && !(QueryType == PGSOQueryType::IRPass ||
91  QueryType == PGSOQueryType::Test))
92  return false;
93  if (isPGSOColdCodeOnly(PSI))
94  return AdapterT::isColdBlock(BBOrBlockFreq, PSI, BFI);
95  if (PSI->hasSampleProfile())
96  // The "isCold" check seems to work better for Sample PGO as it could have
97  // many profile-unannotated functions.
98  return AdapterT::isColdBlockNthPercentile(PgsoCutoffSampleProf,
99  BBOrBlockFreq, PSI, BFI);
100  return !AdapterT::isHotBlockNthPercentile(PgsoCutoffInstrProf, BBOrBlockFreq,
101  PSI, BFI);
102 }
103 
104 /// Returns true if function \p F is suggested to be size-optimized based on the
105 /// profile.
108  PGSOQueryType QueryType = PGSOQueryType::Other);
109 
110 /// Returns true if basic block \p BB is suggested to be size-optimized based on
111 /// the profile.
113  BlockFrequencyInfo *BFI,
114  PGSOQueryType QueryType = PGSOQueryType::Other);
115 
116 } // end namespace llvm
117 
118 #endif // LLVM_TRANSFORMS_UTILS_SIZEOPTS_H
This class represents lattice values for constants.
Definition: AllocatorList.h:23
llvm::cl::opt< bool > EnablePGSO
Various leaf nodes.
Definition: ISDOpcodes.h:64
bool hasProfileSummary() const
Returns true if profile summary is available.
Analysis providing profile information.
llvm::cl::opt< bool > PGSOIRPassOrTestOnly
bool hasInstrumentationProfile() const
Returns true if module M has instrumentation profile.
F(f)
bool hasSampleProfile() const
Returns true if module M has sample profile.
llvm::cl::opt< int > PgsoCutoffInstrProf
llvm::cl::opt< bool > PGSOColdCodeOnlyForInstrPGO
bool shouldFuncOptimizeForSizeImpl(const FuncT *F, ProfileSummaryInfo *PSI, BFIT *BFI, PGSOQueryType QueryType)
Definition: SizeOpts.h:54
BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate IR basic block frequen...
bool hasLargeWorkingSetSize() const
Returns true if the working set size of the code is considered large.
LLVM Basic Block Representation.
Definition: BasicBlock.h:58
llvm::cl::opt< bool > PGSOColdCodeOnlyForSamplePGO
llvm::cl::opt< bool > PGSOLargeWorkingSetSizeOnly
llvm::cl::opt< bool > PGSOColdCodeOnlyForPartialSamplePGO
static bool isPGSOColdCodeOnly(ProfileSummaryInfo *PSI)
Definition: SizeOpts.h:43
llvm::cl::opt< bool > ForcePGSO
bool hasPartialSampleProfile() const
Returns true if module M has partial-profile sample profile.
bool shouldOptimizeForSizeImpl(BlockTOrBlockFreq BBOrBlockFreq, ProfileSummaryInfo *PSI, BFIT *BFI, PGSOQueryType QueryType)
Definition: SizeOpts.h:80
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...
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::cl::opt< int > PgsoCutoffSampleProf
PGSOQueryType
Definition: SizeOpts.h:37
llvm::cl::opt< bool > PGSOColdCodeOnly