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