Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:44

0001 //===- llvm/Transforms/Utils/SizeOpts.h - size optimization -----*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // This file contains some shared code size optimization related code.
0010 //
0011 //===----------------------------------------------------------------------===//
0012 
0013 #ifndef LLVM_TRANSFORMS_UTILS_SIZEOPTS_H
0014 #define LLVM_TRANSFORMS_UTILS_SIZEOPTS_H
0015 
0016 #include "llvm/Analysis/ProfileSummaryInfo.h"
0017 #include "llvm/Support/CommandLine.h"
0018 
0019 namespace llvm {
0020 extern cl::opt<bool> EnablePGSO;
0021 extern cl::opt<bool> PGSOLargeWorkingSetSizeOnly;
0022 extern cl::opt<bool> PGSOColdCodeOnly;
0023 extern cl::opt<bool> PGSOColdCodeOnlyForInstrPGO;
0024 extern cl::opt<bool> PGSOColdCodeOnlyForSamplePGO;
0025 extern cl::opt<bool> PGSOColdCodeOnlyForPartialSamplePGO;
0026 extern cl::opt<bool> ForcePGSO;
0027 extern cl::opt<int> PgsoCutoffInstrProf;
0028 extern cl::opt<int> PgsoCutoffSampleProf;
0029 
0030 class BasicBlock;
0031 class BlockFrequencyInfo;
0032 class Function;
0033 
0034 enum class PGSOQueryType {
0035   IRPass, // A query call from an IR-level transform pass.
0036   Test,   // A query call from a unit test.
0037   Other,  // Others.
0038 };
0039 
0040 static inline bool isPGSOColdCodeOnly(ProfileSummaryInfo *PSI) {
0041   return PGSOColdCodeOnly ||
0042          (PSI->hasInstrumentationProfile() && PGSOColdCodeOnlyForInstrPGO) ||
0043          (PSI->hasSampleProfile() &&
0044           ((!PSI->hasPartialSampleProfile() && PGSOColdCodeOnlyForSamplePGO) ||
0045            (PSI->hasPartialSampleProfile() &&
0046             PGSOColdCodeOnlyForPartialSamplePGO))) ||
0047          (PGSOLargeWorkingSetSizeOnly && !PSI->hasLargeWorkingSetSize());
0048 }
0049 
0050 template <typename FuncT, typename BFIT>
0051 bool shouldFuncOptimizeForSizeImpl(const FuncT *F, ProfileSummaryInfo *PSI,
0052                                    BFIT *BFI, PGSOQueryType QueryType) {
0053   assert(F);
0054   if (!PSI || !BFI || !PSI->hasProfileSummary())
0055     return false;
0056   if (ForcePGSO)
0057     return true;
0058   if (!EnablePGSO)
0059     return false;
0060   if (isPGSOColdCodeOnly(PSI))
0061     return PSI->isFunctionColdInCallGraph(F, *BFI);
0062   if (PSI->hasSampleProfile())
0063     // The "isCold" check seems to work better for Sample PGO as it could have
0064     // many profile-unannotated functions.
0065     return PSI->isFunctionColdInCallGraphNthPercentile(PgsoCutoffSampleProf, F,
0066                                                        *BFI);
0067   return !PSI->isFunctionHotInCallGraphNthPercentile(PgsoCutoffInstrProf, F,
0068                                                      *BFI);
0069 }
0070 
0071 template <typename BlockTOrBlockFreq, typename BFIT>
0072 bool shouldOptimizeForSizeImpl(BlockTOrBlockFreq BBOrBlockFreq,
0073                                ProfileSummaryInfo *PSI, BFIT *BFI,
0074                                PGSOQueryType QueryType) {
0075   if (!PSI || !BFI || !PSI->hasProfileSummary())
0076     return false;
0077   if (ForcePGSO)
0078     return true;
0079   if (!EnablePGSO)
0080     return false;
0081   if (isPGSOColdCodeOnly(PSI))
0082     return PSI->isColdBlock(BBOrBlockFreq, BFI);
0083   if (PSI->hasSampleProfile())
0084     // The "isCold" check seems to work better for Sample PGO as it could have
0085     // many profile-unannotated functions.
0086     return PSI->isColdBlockNthPercentile(PgsoCutoffSampleProf, BBOrBlockFreq,
0087                                          BFI);
0088   return !PSI->isHotBlockNthPercentile(PgsoCutoffInstrProf, BBOrBlockFreq, BFI);
0089 }
0090 
0091 /// Returns true if function \p F is suggested to be size-optimized based on the
0092 /// profile.
0093 bool shouldOptimizeForSize(const Function *F, ProfileSummaryInfo *PSI,
0094                            BlockFrequencyInfo *BFI,
0095                            PGSOQueryType QueryType = PGSOQueryType::Other);
0096 
0097 /// Returns true if basic block \p BB is suggested to be size-optimized based on
0098 /// the profile.
0099 bool shouldOptimizeForSize(const BasicBlock *BB, ProfileSummaryInfo *PSI,
0100                            BlockFrequencyInfo *BFI,
0101                            PGSOQueryType QueryType = PGSOQueryType::Other);
0102 
0103 } // end namespace llvm
0104 
0105 #endif // LLVM_TRANSFORMS_UTILS_SIZEOPTS_H