DeadCodeElimination.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. //===- DeadCodeElimination.cpp - Eliminate dead iteration ----------------===//
  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. // The polyhedral dead code elimination pass analyses a SCoP to eliminate
  10. // statement instances that can be proven dead.
  11. // As a consequence, the code generated for this SCoP may execute a statement
  12. // less often. This means, a statement may be executed only in certain loop
  13. // iterations or it may not even be part of the generated code at all.
  14. //
  15. // This code:
  16. //
  17. // for (i = 0; i < N; i++)
  18. // arr[i] = 0;
  19. // for (i = 0; i < N; i++)
  20. // arr[i] = 10;
  21. // for (i = 0; i < N; i++)
  22. // arr[i] = i;
  23. //
  24. // is e.g. simplified to:
  25. //
  26. // for (i = 0; i < N; i++)
  27. // arr[i] = i;
  28. //
  29. // The idea and the algorithm used was first implemented by Sven Verdoolaege in
  30. // the 'ppcg' tool.
  31. //
  32. //===----------------------------------------------------------------------===//
  33. #include "polly/DeadCodeElimination.h"
  34. #include "polly/DependenceInfo.h"
  35. #include "polly/LinkAllPasses.h"
  36. #include "polly/Options.h"
  37. #include "polly/ScopInfo.h"
  38. #include "llvm/Support/CommandLine.h"
  39. #include "isl/isl-noexceptions.h"
  40. using namespace llvm;
  41. using namespace polly;
  42. namespace {
  43. cl::opt<int> DCEPreciseSteps(
  44. "polly-dce-precise-steps",
  45. cl::desc("The number of precise steps between two approximating "
  46. "iterations. (A value of -1 schedules another approximation stage "
  47. "before the actual dead code elimination."),
  48. cl::ZeroOrMore, cl::init(-1), cl::cat(PollyCategory));
  49. class DeadCodeElimWrapperPass : public ScopPass {
  50. public:
  51. static char ID;
  52. explicit DeadCodeElimWrapperPass() : ScopPass(ID) {}
  53. /// Remove dead iterations from the schedule of @p S.
  54. bool runOnScop(Scop &S) override;
  55. /// Register all analyses and transformation required.
  56. void getAnalysisUsage(AnalysisUsage &AU) const override;
  57. };
  58. char DeadCodeElimWrapperPass::ID = 0;
  59. /// Return the set of live iterations.
  60. ///
  61. /// The set of live iterations are all iterations that write to memory and for
  62. /// which we can not prove that there will be a later write that _must_
  63. /// overwrite the same memory location and is consequently the only one that
  64. /// is visible after the execution of the SCoP.
  65. ///
  66. /// To compute the live outs, we compute for the data-locations that are
  67. /// must-written to the last statement that touches these locations. On top of
  68. /// this we add all statements that perform may-write accesses.
  69. ///
  70. /// We could be more precise by removing may-write accesses for which we know
  71. /// that they are overwritten by a must-write after. However, at the moment the
  72. /// only may-writes we introduce access the full (unbounded) array, such that
  73. /// bounded write accesses can not overwrite all of the data-locations. As
  74. /// this means may-writes are in the current situation always live, there is
  75. /// no point in trying to remove them from the live-out set.
  76. static isl::union_set getLiveOut(Scop &S) {
  77. isl::union_map Schedule = S.getSchedule();
  78. isl::union_map MustWrites = S.getMustWrites();
  79. isl::union_map WriteIterations = MustWrites.reverse();
  80. isl::union_map WriteTimes = WriteIterations.apply_range(Schedule);
  81. isl::union_map LastWriteTimes = WriteTimes.lexmax();
  82. isl::union_map LastWriteIterations =
  83. LastWriteTimes.apply_range(Schedule.reverse());
  84. isl::union_set Live = LastWriteIterations.range();
  85. isl::union_map MayWrites = S.getMayWrites();
  86. Live = Live.unite(MayWrites.domain());
  87. return Live.coalesce();
  88. }
  89. /// Performs polyhedral dead iteration elimination by:
  90. /// o Assuming that the last write to each location is live.
  91. /// o Following each RAW dependency from a live iteration backwards and adding
  92. /// that iteration to the live set.
  93. ///
  94. /// To ensure the set of live iterations does not get too complex we always
  95. /// combine a certain number of precise steps with one approximating step that
  96. /// simplifies the life set with an affine hull.
  97. static bool runDeadCodeElimination(Scop &S, int PreciseSteps,
  98. const Dependences &D) {
  99. if (!D.hasValidDependences())
  100. return false;
  101. isl::union_set Live = getLiveOut(S);
  102. isl::union_map Dep =
  103. D.getDependences(Dependences::TYPE_RAW | Dependences::TYPE_RED);
  104. Dep = Dep.reverse();
  105. if (PreciseSteps == -1)
  106. Live = Live.affine_hull();
  107. isl::union_set OriginalDomain = S.getDomains();
  108. int Steps = 0;
  109. while (true) {
  110. Steps++;
  111. isl::union_set Extra = Live.apply(Dep);
  112. if (Extra.is_subset(Live))
  113. break;
  114. Live = Live.unite(Extra);
  115. if (Steps > PreciseSteps) {
  116. Steps = 0;
  117. Live = Live.affine_hull();
  118. }
  119. Live = Live.intersect(OriginalDomain);
  120. }
  121. Live = Live.coalesce();
  122. return S.restrictDomains(Live);
  123. }
  124. bool DeadCodeElimWrapperPass::runOnScop(Scop &S) {
  125. auto &DI = getAnalysis<DependenceInfo>();
  126. const Dependences &Deps = DI.getDependences(Dependences::AL_Statement);
  127. bool Changed = runDeadCodeElimination(S, DCEPreciseSteps, Deps);
  128. // FIXME: We can probably avoid the recomputation of all dependences by
  129. // updating them explicitly.
  130. if (Changed)
  131. DI.recomputeDependences(Dependences::AL_Statement);
  132. return false;
  133. }
  134. void DeadCodeElimWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
  135. ScopPass::getAnalysisUsage(AU);
  136. AU.addRequired<DependenceInfo>();
  137. }
  138. } // namespace
  139. Pass *polly::createDeadCodeElimWrapperPass() {
  140. return new DeadCodeElimWrapperPass();
  141. }
  142. llvm::PreservedAnalyses DeadCodeElimPass::run(Scop &S, ScopAnalysisManager &SAM,
  143. ScopStandardAnalysisResults &SAR,
  144. SPMUpdater &U) {
  145. DependenceAnalysis::Result &DA = SAM.getResult<DependenceAnalysis>(S, SAR);
  146. const Dependences &Deps = DA.getDependences(Dependences::AL_Statement);
  147. bool Changed = runDeadCodeElimination(S, DCEPreciseSteps, Deps);
  148. // FIXME: We can probably avoid the recomputation of all dependences by
  149. // updating them explicitly.
  150. if (Changed)
  151. DA.recomputeDependences(Dependences::AL_Statement);
  152. if (!Changed)
  153. return PreservedAnalyses::all();
  154. PreservedAnalyses PA;
  155. PA.preserveSet<AllAnalysesOn<Module>>();
  156. PA.preserveSet<AllAnalysesOn<Function>>();
  157. PA.preserveSet<AllAnalysesOn<Loop>>();
  158. return PA;
  159. }
  160. INITIALIZE_PASS_BEGIN(DeadCodeElimWrapperPass, "polly-dce",
  161. "Polly - Remove dead iterations", false, false)
  162. INITIALIZE_PASS_DEPENDENCY(DependenceInfo)
  163. INITIALIZE_PASS_DEPENDENCY(ScopInfoRegionPass)
  164. INITIALIZE_PASS_END(DeadCodeElimWrapperPass, "polly-dce",
  165. "Polly - Remove dead iterations", false, false)