]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm-project/llvm/include/llvm/Transforms/Utils/SizeOpts.h
MFV: r366539
[FreeBSD/FreeBSD.git] / contrib / llvm-project / llvm / include / llvm / Transforms / Utils / SizeOpts.h
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
16 #include "llvm/Analysis/BlockFrequencyInfo.h"
17 #include "llvm/Analysis/ProfileSummaryInfo.h"
18 #include "llvm/Support/CommandLine.h"
19
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;
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 ||
45          (PSI->hasInstrumentationProfile() && PGSOColdCodeOnlyForInstrPGO) ||
46          (PSI->hasSampleProfile() &&
47           ((!PSI->hasPartialSampleProfile() && PGSOColdCodeOnlyForSamplePGO) ||
48            (PSI->hasPartialSampleProfile() &&
49             PGSOColdCodeOnlyForPartialSamplePGO))) ||
50          (PGSOLargeWorkingSetSizeOnly && !PSI->hasLargeWorkingSetSize());
51 }
52
53 template<typename AdapterT, typename FuncT, typename BFIT>
54 bool shouldFuncOptimizeForSizeImpl(const FuncT *F, ProfileSummaryInfo *PSI,
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.
106 bool shouldOptimizeForSize(const Function *F, ProfileSummaryInfo *PSI,
107                            BlockFrequencyInfo *BFI,
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.
112 bool shouldOptimizeForSize(const BasicBlock *BB, ProfileSummaryInfo *PSI,
113                            BlockFrequencyInfo *BFI,
114                            PGSOQueryType QueryType = PGSOQueryType::Other);
115
116 } // end namespace llvm
117
118 #endif // LLVM_TRANSFORMS_UTILS_SIZEOPTS_H