1 //===- llvm/Transforms/Utils/SizeOpts.h - size optimization -----*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // This file contains some shared code size optimization related code.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_TRANSFORMS_UTILS_SIZEOPTS_H
14 #define LLVM_TRANSFORMS_UTILS_SIZEOPTS_H
16 #include "llvm/Analysis/BlockFrequencyInfo.h"
17 #include "llvm/Analysis/ProfileSummaryInfo.h"
18 #include "llvm/Support/CommandLine.h"
20 extern llvm::cl::opt<bool> EnablePGSO;
21 extern llvm::cl::opt<bool> PGSOLargeWorkingSetSizeOnly;
22 extern llvm::cl::opt<bool> PGSOIRPassOrTestOnly;
23 extern llvm::cl::opt<bool> PGSOColdCodeOnly;
24 extern llvm::cl::opt<bool> PGSOColdCodeOnlyForInstrPGO;
25 extern llvm::cl::opt<bool> PGSOColdCodeOnlyForSamplePGO;
26 extern llvm::cl::opt<bool> PGSOColdCodeOnlyForPartialSamplePGO;
27 extern llvm::cl::opt<bool> ForcePGSO;
28 extern llvm::cl::opt<int> PgsoCutoffInstrProf;
29 extern llvm::cl::opt<int> PgsoCutoffSampleProf;
34 class BlockFrequencyInfo;
37 enum class PGSOQueryType {
38 IRPass, // A query call from an IR-level transform pass.
39 Test, // A query call from a unit test.
43 static inline bool isPGSOColdCodeOnly(ProfileSummaryInfo *PSI) {
44 return PGSOColdCodeOnly ||
45 (PSI->hasInstrumentationProfile() && PGSOColdCodeOnlyForInstrPGO) ||
46 (PSI->hasSampleProfile() &&
47 ((!PSI->hasPartialSampleProfile() && PGSOColdCodeOnlyForSamplePGO) ||
48 (PSI->hasPartialSampleProfile() &&
49 PGSOColdCodeOnlyForPartialSamplePGO))) ||
50 (PGSOLargeWorkingSetSizeOnly && !PSI->hasLargeWorkingSetSize());
53 template<typename AdapterT, typename FuncT, typename BFIT>
54 bool shouldFuncOptimizeForSizeImpl(const FuncT *F, ProfileSummaryInfo *PSI,
55 BFIT *BFI, PGSOQueryType QueryType) {
57 if (!PSI || !BFI || !PSI->hasProfileSummary())
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))
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,
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())
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))
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,
104 /// Returns true if function \p F is suggested to be size-optimized based on the
106 bool shouldOptimizeForSize(const Function *F, ProfileSummaryInfo *PSI,
107 BlockFrequencyInfo *BFI,
108 PGSOQueryType QueryType = PGSOQueryType::Other);
110 /// Returns true if basic block \p BB is suggested to be size-optimized based on
112 bool shouldOptimizeForSize(const BasicBlock *BB, ProfileSummaryInfo *PSI,
113 BlockFrequencyInfo *BFI,
114 PGSOQueryType QueryType = PGSOQueryType::Other);
116 } // end namespace llvm
118 #endif // LLVM_TRANSFORMS_UTILS_SIZEOPTS_H