MachineSizeOpts.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. //===- MachineSizeOpts.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 machine IR code size optimization related
  10. // code.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/CodeGen/MachineSizeOpts.h"
  14. #include "llvm/CodeGen/MBFIWrapper.h"
  15. #include "llvm/Analysis/ProfileSummaryInfo.h"
  16. #include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
  17. using namespace llvm;
  18. extern cl::opt<bool> EnablePGSO;
  19. extern cl::opt<bool> PGSOLargeWorkingSetSizeOnly;
  20. extern cl::opt<bool> ForcePGSO;
  21. extern cl::opt<int> PgsoCutoffInstrProf;
  22. extern cl::opt<int> PgsoCutoffSampleProf;
  23. namespace {
  24. namespace machine_size_opts_detail {
  25. /// Like ProfileSummaryInfo::isColdBlock but for MachineBasicBlock.
  26. bool isColdBlock(const MachineBasicBlock *MBB,
  27. ProfileSummaryInfo *PSI,
  28. const MachineBlockFrequencyInfo *MBFI) {
  29. auto Count = MBFI->getBlockProfileCount(MBB);
  30. return Count && PSI->isColdCount(*Count);
  31. }
  32. bool isColdBlock(BlockFrequency BlockFreq,
  33. ProfileSummaryInfo *PSI,
  34. const MachineBlockFrequencyInfo *MBFI) {
  35. auto Count = MBFI->getProfileCountFromFreq(BlockFreq.getFrequency());
  36. return Count && PSI->isColdCount(*Count);
  37. }
  38. /// Like ProfileSummaryInfo::isHotBlockNthPercentile but for MachineBasicBlock.
  39. static bool isHotBlockNthPercentile(int PercentileCutoff,
  40. const MachineBasicBlock *MBB,
  41. ProfileSummaryInfo *PSI,
  42. const MachineBlockFrequencyInfo *MBFI) {
  43. auto Count = MBFI->getBlockProfileCount(MBB);
  44. return Count && PSI->isHotCountNthPercentile(PercentileCutoff, *Count);
  45. }
  46. static bool isHotBlockNthPercentile(int PercentileCutoff,
  47. BlockFrequency BlockFreq,
  48. ProfileSummaryInfo *PSI,
  49. const MachineBlockFrequencyInfo *MBFI) {
  50. auto Count = MBFI->getProfileCountFromFreq(BlockFreq.getFrequency());
  51. return Count && PSI->isHotCountNthPercentile(PercentileCutoff, *Count);
  52. }
  53. static bool isColdBlockNthPercentile(int PercentileCutoff,
  54. const MachineBasicBlock *MBB,
  55. ProfileSummaryInfo *PSI,
  56. const MachineBlockFrequencyInfo *MBFI) {
  57. auto Count = MBFI->getBlockProfileCount(MBB);
  58. return Count && PSI->isColdCountNthPercentile(PercentileCutoff, *Count);
  59. }
  60. static bool isColdBlockNthPercentile(int PercentileCutoff,
  61. BlockFrequency BlockFreq,
  62. ProfileSummaryInfo *PSI,
  63. const MachineBlockFrequencyInfo *MBFI) {
  64. auto Count = MBFI->getProfileCountFromFreq(BlockFreq.getFrequency());
  65. return Count && PSI->isColdCountNthPercentile(PercentileCutoff, *Count);
  66. }
  67. /// Like ProfileSummaryInfo::isFunctionColdInCallGraph but for
  68. /// MachineFunction.
  69. bool isFunctionColdInCallGraph(
  70. const MachineFunction *MF,
  71. ProfileSummaryInfo *PSI,
  72. const MachineBlockFrequencyInfo &MBFI) {
  73. if (auto FunctionCount = MF->getFunction().getEntryCount())
  74. if (!PSI->isColdCount(FunctionCount->getCount()))
  75. return false;
  76. for (const auto &MBB : *MF)
  77. if (!isColdBlock(&MBB, PSI, &MBFI))
  78. return false;
  79. return true;
  80. }
  81. /// Like ProfileSummaryInfo::isFunctionHotInCallGraphNthPercentile but for
  82. /// MachineFunction.
  83. bool isFunctionHotInCallGraphNthPercentile(
  84. int PercentileCutoff,
  85. const MachineFunction *MF,
  86. ProfileSummaryInfo *PSI,
  87. const MachineBlockFrequencyInfo &MBFI) {
  88. if (auto FunctionCount = MF->getFunction().getEntryCount())
  89. if (PSI->isHotCountNthPercentile(PercentileCutoff,
  90. FunctionCount->getCount()))
  91. return true;
  92. for (const auto &MBB : *MF)
  93. if (isHotBlockNthPercentile(PercentileCutoff, &MBB, PSI, &MBFI))
  94. return true;
  95. return false;
  96. }
  97. bool isFunctionColdInCallGraphNthPercentile(
  98. int PercentileCutoff, const MachineFunction *MF, ProfileSummaryInfo *PSI,
  99. const MachineBlockFrequencyInfo &MBFI) {
  100. if (auto FunctionCount = MF->getFunction().getEntryCount())
  101. if (!PSI->isColdCountNthPercentile(PercentileCutoff,
  102. FunctionCount->getCount()))
  103. return false;
  104. for (const auto &MBB : *MF)
  105. if (!isColdBlockNthPercentile(PercentileCutoff, &MBB, PSI, &MBFI))
  106. return false;
  107. return true;
  108. }
  109. } // namespace machine_size_opts_detail
  110. struct MachineBasicBlockBFIAdapter {
  111. static bool isFunctionColdInCallGraph(const MachineFunction *MF,
  112. ProfileSummaryInfo *PSI,
  113. const MachineBlockFrequencyInfo &MBFI) {
  114. return machine_size_opts_detail::isFunctionColdInCallGraph(MF, PSI, MBFI);
  115. }
  116. static bool isFunctionHotInCallGraphNthPercentile(
  117. int CutOff,
  118. const MachineFunction *MF,
  119. ProfileSummaryInfo *PSI,
  120. const MachineBlockFrequencyInfo &MBFI) {
  121. return machine_size_opts_detail::isFunctionHotInCallGraphNthPercentile(
  122. CutOff, MF, PSI, MBFI);
  123. }
  124. static bool isFunctionColdInCallGraphNthPercentile(
  125. int CutOff, const MachineFunction *MF, ProfileSummaryInfo *PSI,
  126. const MachineBlockFrequencyInfo &MBFI) {
  127. return machine_size_opts_detail::isFunctionColdInCallGraphNthPercentile(
  128. CutOff, MF, PSI, MBFI);
  129. }
  130. static bool isColdBlock(const MachineBasicBlock *MBB,
  131. ProfileSummaryInfo *PSI,
  132. const MachineBlockFrequencyInfo *MBFI) {
  133. return machine_size_opts_detail::isColdBlock(MBB, PSI, MBFI);
  134. }
  135. static bool isColdBlock(BlockFrequency BlockFreq,
  136. ProfileSummaryInfo *PSI,
  137. const MachineBlockFrequencyInfo *MBFI) {
  138. return machine_size_opts_detail::isColdBlock(BlockFreq, PSI, MBFI);
  139. }
  140. static bool isHotBlockNthPercentile(int CutOff,
  141. const MachineBasicBlock *MBB,
  142. ProfileSummaryInfo *PSI,
  143. const MachineBlockFrequencyInfo *MBFI) {
  144. return machine_size_opts_detail::isHotBlockNthPercentile(
  145. CutOff, MBB, PSI, MBFI);
  146. }
  147. static bool isHotBlockNthPercentile(int CutOff,
  148. BlockFrequency BlockFreq,
  149. ProfileSummaryInfo *PSI,
  150. const MachineBlockFrequencyInfo *MBFI) {
  151. return machine_size_opts_detail::isHotBlockNthPercentile(
  152. CutOff, BlockFreq, PSI, MBFI);
  153. }
  154. static bool isColdBlockNthPercentile(int CutOff, const MachineBasicBlock *MBB,
  155. ProfileSummaryInfo *PSI,
  156. const MachineBlockFrequencyInfo *MBFI) {
  157. return machine_size_opts_detail::isColdBlockNthPercentile(CutOff, MBB, PSI,
  158. MBFI);
  159. }
  160. static bool isColdBlockNthPercentile(int CutOff, BlockFrequency BlockFreq,
  161. ProfileSummaryInfo *PSI,
  162. const MachineBlockFrequencyInfo *MBFI) {
  163. return machine_size_opts_detail::isColdBlockNthPercentile(CutOff, BlockFreq,
  164. PSI, MBFI);
  165. }
  166. };
  167. } // end anonymous namespace
  168. bool llvm::shouldOptimizeForSize(const MachineFunction *MF,
  169. ProfileSummaryInfo *PSI,
  170. const MachineBlockFrequencyInfo *MBFI,
  171. PGSOQueryType QueryType) {
  172. return shouldFuncOptimizeForSizeImpl<MachineBasicBlockBFIAdapter>(
  173. MF, PSI, MBFI, QueryType);
  174. }
  175. bool llvm::shouldOptimizeForSize(const MachineBasicBlock *MBB,
  176. ProfileSummaryInfo *PSI,
  177. const MachineBlockFrequencyInfo *MBFI,
  178. PGSOQueryType QueryType) {
  179. assert(MBB);
  180. return shouldOptimizeForSizeImpl<MachineBasicBlockBFIAdapter>(
  181. MBB, PSI, MBFI, QueryType);
  182. }
  183. bool llvm::shouldOptimizeForSize(const MachineBasicBlock *MBB,
  184. ProfileSummaryInfo *PSI,
  185. MBFIWrapper *MBFIW,
  186. PGSOQueryType QueryType) {
  187. assert(MBB);
  188. if (!PSI || !MBFIW)
  189. return false;
  190. BlockFrequency BlockFreq = MBFIW->getBlockFreq(MBB);
  191. return shouldOptimizeForSizeImpl<MachineBasicBlockBFIAdapter>(
  192. BlockFreq, PSI, &MBFIW->getMBFI(), QueryType);
  193. }