SizeOpts.cpp 4.9 KB

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