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 
15 using namespace llvm;
16 
17 cl::opt<bool> EnablePGSO(
18     "pgso", cl::Hidden, cl::init(true),
19     cl::desc("Enable the profile guided size optimizations. "));
20 
21 cl::opt<bool> PGSOLargeWorkingSetSizeOnly(
22     "pgso-lwss-only", cl::Hidden, cl::init(true),
23     cl::desc("Apply the profile guided size optimizations only "
24              "if the working set size is large (except for cold code.)"));
25 
26 cl::opt<bool> ForcePGSO(
27     "force-pgso", cl::Hidden, cl::init(false),
28     cl::desc("Force the (profiled-guided) size optimizations. "));
29 
30 cl::opt<int> PgsoCutoffInstrProf(
31     "pgso-cutoff-instr-prof", cl::Hidden, cl::init(250000), cl::ZeroOrMore,
32     cl::desc("The profile guided size optimization profile summary cutoff "
33              "for instrumentation profile."));
34 
35 cl::opt<int> PgsoCutoffSampleProf(
36     "pgso-cutoff-sample-prof", cl::Hidden, cl::init(800000), cl::ZeroOrMore,
37     cl::desc("The profile guided size optimization profile summary cutoff "
38              "for sample profile."));
39 
40 namespace {
41 struct BasicBlockBFIAdapter {
42   static bool isFunctionColdInCallGraph(const Function *F,
43                                         ProfileSummaryInfo *PSI,
44                                         BlockFrequencyInfo &BFI) {
45     return PSI->isFunctionColdInCallGraph(F, BFI);
46   }
47   static bool isFunctionHotInCallGraphNthPercentile(int CutOff,
48                                                     const Function *F,
49                                                     ProfileSummaryInfo *PSI,
50                                                     BlockFrequencyInfo &BFI) {
51     return PSI->isFunctionHotInCallGraphNthPercentile(CutOff, F, BFI);
52   }
53   static bool isColdBlock(const BasicBlock *BB,
54                           ProfileSummaryInfo *PSI,
55                           BlockFrequencyInfo *BFI) {
56     return PSI->isColdBlock(BB, BFI);
57   }
58   static bool isHotBlockNthPercentile(int CutOff,
59                                       const BasicBlock *BB,
60                                       ProfileSummaryInfo *PSI,
61                                       BlockFrequencyInfo *BFI) {
62     return PSI->isHotBlockNthPercentile(CutOff, BB, BFI);
63   }
64 };
65 } // end anonymous namespace
66 
67 bool llvm::shouldOptimizeForSize(const Function *F, ProfileSummaryInfo *PSI,
68                                  BlockFrequencyInfo *BFI) {
69   return shouldFuncOptimizeForSizeImpl<BasicBlockBFIAdapter>(F, PSI, BFI);
70 }
71 
72 bool llvm::shouldOptimizeForSize(const BasicBlock *BB, ProfileSummaryInfo *PSI,
73                                  BlockFrequencyInfo *BFI) {
74   return shouldOptimizeForSizeImpl<BasicBlockBFIAdapter>(BB, PSI, BFI);
75 }
76