PruneUnprofitable.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. //===- PruneUnprofitable.cpp ----------------------------------------------===//
  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. // Mark a SCoP as unfeasible if not deemed profitable to optimize.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "polly/PruneUnprofitable.h"
  13. #include "polly/ScopDetection.h"
  14. #include "polly/ScopInfo.h"
  15. #include "polly/ScopPass.h"
  16. #include "llvm/ADT/Statistic.h"
  17. #include "llvm/IR/DebugLoc.h"
  18. #include "llvm/Support/Debug.h"
  19. #include "llvm/Support/raw_ostream.h"
  20. using namespace llvm;
  21. using namespace polly;
  22. #define DEBUG_TYPE "polly-prune-unprofitable"
  23. namespace {
  24. STATISTIC(ScopsProcessed,
  25. "Number of SCoPs considered for unprofitability pruning");
  26. STATISTIC(ScopsPruned, "Number of pruned SCoPs because it they cannot be "
  27. "optimized in a significant way");
  28. STATISTIC(ScopsSurvived, "Number of SCoPs after pruning");
  29. STATISTIC(NumPrunedLoops, "Number of pruned loops");
  30. STATISTIC(NumPrunedBoxedLoops, "Number of pruned boxed loops");
  31. STATISTIC(NumPrunedAffineLoops, "Number of pruned affine loops");
  32. STATISTIC(NumLoopsInScop, "Number of loops in scops after pruning");
  33. STATISTIC(NumBoxedLoops, "Number of boxed loops in SCoPs after pruning");
  34. STATISTIC(NumAffineLoops, "Number of affine loops in SCoPs after pruning");
  35. static void updateStatistics(Scop &S, bool Pruned) {
  36. Scop::ScopStatistics ScopStats = S.getStatistics();
  37. if (Pruned) {
  38. ScopsPruned++;
  39. NumPrunedLoops += ScopStats.NumAffineLoops + ScopStats.NumBoxedLoops;
  40. NumPrunedBoxedLoops += ScopStats.NumBoxedLoops;
  41. NumPrunedAffineLoops += ScopStats.NumAffineLoops;
  42. } else {
  43. ScopsSurvived++;
  44. NumLoopsInScop += ScopStats.NumAffineLoops + ScopStats.NumBoxedLoops;
  45. NumBoxedLoops += ScopStats.NumBoxedLoops;
  46. NumAffineLoops += ScopStats.NumAffineLoops;
  47. }
  48. }
  49. static bool runPruneUnprofitable(Scop &S) {
  50. if (PollyProcessUnprofitable) {
  51. LLVM_DEBUG(
  52. dbgs() << "NOTE: -polly-process-unprofitable active, won't prune "
  53. "anything\n");
  54. return false;
  55. }
  56. ScopsProcessed++;
  57. if (!S.isProfitable(true)) {
  58. LLVM_DEBUG(
  59. dbgs() << "SCoP pruned because it probably cannot be optimized in "
  60. "a significant way\n");
  61. S.invalidate(PROFITABLE, DebugLoc());
  62. updateStatistics(S, true);
  63. } else {
  64. updateStatistics(S, false);
  65. }
  66. return false;
  67. }
  68. class PruneUnprofitableWrapperPass : public ScopPass {
  69. public:
  70. static char ID;
  71. explicit PruneUnprofitableWrapperPass() : ScopPass(ID) {}
  72. PruneUnprofitableWrapperPass(const PruneUnprofitableWrapperPass &) = delete;
  73. PruneUnprofitableWrapperPass &
  74. operator=(const PruneUnprofitableWrapperPass &) = delete;
  75. void getAnalysisUsage(AnalysisUsage &AU) const override {
  76. AU.addRequired<ScopInfoRegionPass>();
  77. AU.setPreservesAll();
  78. }
  79. bool runOnScop(Scop &S) override { return runPruneUnprofitable(S); }
  80. };
  81. } // namespace
  82. char PruneUnprofitableWrapperPass::ID;
  83. Pass *polly::createPruneUnprofitableWrapperPass() {
  84. return new PruneUnprofitableWrapperPass();
  85. }
  86. INITIALIZE_PASS_BEGIN(PruneUnprofitableWrapperPass, "polly-prune-unprofitable",
  87. "Polly - Prune unprofitable SCoPs", false, false)
  88. INITIALIZE_PASS_END(PruneUnprofitableWrapperPass, "polly-prune-unprofitable",
  89. "Polly - Prune unprofitable SCoPs", false, false)
  90. llvm::PreservedAnalyses
  91. PruneUnprofitablePass::run(Scop &S, ScopAnalysisManager &SAM,
  92. ScopStandardAnalysisResults &SAR, SPMUpdater &U) {
  93. bool Changed = runPruneUnprofitable(S);
  94. if (!Changed)
  95. return PreservedAnalyses::all();
  96. PreservedAnalyses PA;
  97. PA.preserveSet<AllAnalysesOn<Module>>();
  98. PA.preserveSet<AllAnalysesOn<Function>>();
  99. PA.preserveSet<AllAnalysesOn<Loop>>();
  100. return PA;
  101. }