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> PGSOColdCodeOnly( 27 "pgso-cold-code-only", cl::Hidden, cl::init(true), 28 cl::desc("Apply the profile guided size optimizations only " 29 "to cold code.")); 30 31 cl::opt<bool> PGSOColdCodeOnlyForInstrPGO( 32 "pgso-cold-code-only-for-instr-pgo", cl::Hidden, cl::init(true), 33 cl::desc("Apply the profile guided size optimizations only " 34 "to cold code under instrumentation PGO.")); 35 36 cl::opt<bool> PGSOColdCodeOnlyForSamplePGO( 37 "pgso-cold-code-only-for-sample-pgo", cl::Hidden, cl::init(true), 38 cl::desc("Apply the profile guided size optimizations only " 39 "to cold code under sample PGO.")); 40 41 cl::opt<bool> PGSOIRPassOrTestOnly( 42 "pgso-ir-pass-or-test-only", cl::Hidden, cl::init(false), 43 cl::desc("Apply the profile guided size optimizations only" 44 "to the IR passes or tests.")); 45 46 cl::opt<bool> ForcePGSO( 47 "force-pgso", cl::Hidden, cl::init(false), 48 cl::desc("Force the (profiled-guided) size optimizations. ")); 49 50 cl::opt<int> PgsoCutoffInstrProf( 51 "pgso-cutoff-instr-prof", cl::Hidden, cl::init(950000), cl::ZeroOrMore, 52 cl::desc("The profile guided size optimization profile summary cutoff " 53 "for instrumentation profile.")); 54 55 cl::opt<int> PgsoCutoffSampleProf( 56 "pgso-cutoff-sample-prof", cl::Hidden, cl::init(800000), cl::ZeroOrMore, 57 cl::desc("The profile guided size optimization profile summary cutoff " 58 "for sample profile.")); 59 60 namespace { 61 struct 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(CutOff, F, BFI); 72 } 73 static bool isColdBlock(const BasicBlock *BB, 74 ProfileSummaryInfo *PSI, 75 BlockFrequencyInfo *BFI) { 76 return PSI->isColdBlock(BB, BFI); 77 } 78 static bool isHotBlockNthPercentile(int CutOff, 79 const BasicBlock *BB, 80 ProfileSummaryInfo *PSI, 81 BlockFrequencyInfo *BFI) { 82 return PSI->isHotBlockNthPercentile(CutOff, BB, BFI); 83 } 84 }; 85 } // end anonymous namespace 86 87 bool llvm::shouldOptimizeForSize(const Function *F, ProfileSummaryInfo *PSI, 88 BlockFrequencyInfo *BFI, 89 PGSOQueryType QueryType) { 90 return shouldFuncOptimizeForSizeImpl<BasicBlockBFIAdapter>(F, PSI, BFI, 91 QueryType); 92 } 93 94 bool llvm::shouldOptimizeForSize(const BasicBlock *BB, ProfileSummaryInfo *PSI, 95 BlockFrequencyInfo *BFI, 96 PGSOQueryType QueryType) { 97 assert(BB); 98 return shouldOptimizeForSizeImpl<BasicBlockBFIAdapter>(BB, PSI, BFI, 99 QueryType); 100 } 101