xref: /llvm-project/llvm/lib/Transforms/Utils/SizeOpts.cpp (revision 6ab26eab4f1e06f2da7b3183c55666ad57f8866e)
1 //===-- SizeOpts.cpp - code size optimization related code ----------------===//
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 #include "llvm/Transforms/Utils/SizeOpts.h"
14 #include "llvm/Analysis/BlockFrequencyInfo.h"
15 
16 using namespace llvm;
17 
18 cl::opt<bool> llvm::EnablePGSO(
19     "pgso", cl::Hidden, cl::init(true),
20     cl::desc("Enable the profile guided size optimizations. "));
21 
22 cl::opt<bool> llvm::PGSOLargeWorkingSetSizeOnly(
23     "pgso-lwss-only", cl::Hidden, cl::init(true),
24     cl::desc("Apply the profile guided size optimizations only "
25              "if the working set size is large (except for cold code.)"));
26 
27 cl::opt<bool> llvm::PGSOColdCodeOnly(
28     "pgso-cold-code-only", cl::Hidden, cl::init(false),
29     cl::desc("Apply the profile guided size optimizations only "
30              "to cold code."));
31 
32 cl::opt<bool> llvm::PGSOColdCodeOnlyForInstrPGO(
33     "pgso-cold-code-only-for-instr-pgo", cl::Hidden, cl::init(false),
34     cl::desc("Apply the profile guided size optimizations only "
35              "to cold code under instrumentation PGO."));
36 
37 cl::opt<bool> llvm::PGSOColdCodeOnlyForSamplePGO(
38     "pgso-cold-code-only-for-sample-pgo", cl::Hidden, cl::init(false),
39     cl::desc("Apply the profile guided size optimizations only "
40              "to cold code under sample PGO."));
41 
42 cl::opt<bool> llvm::PGSOColdCodeOnlyForPartialSamplePGO(
43     "pgso-cold-code-only-for-partial-sample-pgo", cl::Hidden, cl::init(false),
44     cl::desc("Apply the profile guided size optimizations only "
45              "to cold code under partial-profile sample PGO."));
46 
47 cl::opt<bool> llvm::ForcePGSO(
48     "force-pgso", cl::Hidden, cl::init(false),
49     cl::desc("Force the (profiled-guided) size optimizations. "));
50 
51 cl::opt<int> llvm::PgsoCutoffInstrProf(
52     "pgso-cutoff-instr-prof", cl::Hidden, cl::init(950000),
53     cl::desc("The profile guided size optimization profile summary cutoff "
54              "for instrumentation profile."));
55 
56 cl::opt<int> llvm::PgsoCutoffSampleProf(
57     "pgso-cutoff-sample-prof", cl::Hidden, cl::init(990000),
58     cl::desc("The profile guided size optimization profile summary cutoff "
59              "for sample profile."));
60 
61 namespace {
62 struct BasicBlockBFIAdapter {
63   static bool isFunctionColdInCallGraph(const Function *F,
64                                         ProfileSummaryInfo *PSI,
65                                         BlockFrequencyInfo &BFI) {
66     return PSI->isFunctionColdInCallGraph(F, BFI);
67   }
68   static bool isFunctionHotInCallGraphNthPercentile(int CutOff,
69                                                     const Function *F,
70                                                     ProfileSummaryInfo *PSI,
71                                                     BlockFrequencyInfo &BFI) {
72     return PSI->isFunctionHotInCallGraphNthPercentile(CutOff, F, BFI);
73   }
74   static bool isFunctionColdInCallGraphNthPercentile(int CutOff,
75                                                      const Function *F,
76                                                      ProfileSummaryInfo *PSI,
77                                                      BlockFrequencyInfo &BFI) {
78     return PSI->isFunctionColdInCallGraphNthPercentile(CutOff, F, BFI);
79   }
80   static bool isColdBlock(const BasicBlock *BB,
81                           ProfileSummaryInfo *PSI,
82                           BlockFrequencyInfo *BFI) {
83     return PSI->isColdBlock(BB, BFI);
84   }
85   static bool isHotBlockNthPercentile(int CutOff,
86                                       const BasicBlock *BB,
87                                       ProfileSummaryInfo *PSI,
88                                       BlockFrequencyInfo *BFI) {
89     return PSI->isHotBlockNthPercentile(CutOff, BB, BFI);
90   }
91   static bool isColdBlockNthPercentile(int CutOff, const BasicBlock *BB,
92                                        ProfileSummaryInfo *PSI,
93                                        BlockFrequencyInfo *BFI) {
94     return PSI->isColdBlockNthPercentile(CutOff, BB, BFI);
95   }
96 };
97 } // end anonymous namespace
98 
99 bool llvm::shouldOptimizeForSize(const Function *F, ProfileSummaryInfo *PSI,
100                                  BlockFrequencyInfo *BFI,
101                                  PGSOQueryType QueryType) {
102   if (F->hasOptSize())
103     return true;
104   return shouldFuncOptimizeForSizeImpl(F, PSI, BFI, QueryType);
105 }
106 
107 bool llvm::shouldOptimizeForSize(const BasicBlock *BB, ProfileSummaryInfo *PSI,
108                                  BlockFrequencyInfo *BFI,
109                                  PGSOQueryType QueryType) {
110   assert(BB);
111   if (BB->getParent()->hasOptSize())
112     return true;
113   return shouldOptimizeForSizeImpl(BB, PSI, BFI, QueryType);
114 }
115