Tobias Grosser | 1d34867 | 2012-01-31 14:00:27 +0000 | [diff] [blame] | 1 | //===- DeadCodeElimination.cpp - Eliminate dead iteration ----------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Tobias Grosser | ecae17f | 2014-02-21 20:51:40 +0000 | [diff] [blame] | 10 | // The polyhedral dead code elimination pass analyses a SCoP to eliminate |
| 11 | // statement instances that can be proven dead. |
| 12 | // As a consequence, the code generated for this SCoP may execute a statement |
| 13 | // less often. This means, a statement may be executed only in certain loop |
| 14 | // iterations or it may not even be part of the generated code at all. |
Tobias Grosser | c30d9cc | 2013-03-23 00:16:05 +0000 | [diff] [blame] | 15 | // |
Tobias Grosser | ecae17f | 2014-02-21 20:51:40 +0000 | [diff] [blame] | 16 | // This code: |
Tobias Grosser | c30d9cc | 2013-03-23 00:16:05 +0000 | [diff] [blame] | 17 | // |
Tobias Grosser | ecae17f | 2014-02-21 20:51:40 +0000 | [diff] [blame] | 18 | // for (i = 0; i < N; i++) |
| 19 | // arr[i] = 0; |
| 20 | // for (i = 0; i < N; i++) |
| 21 | // arr[i] = 10; |
| 22 | // for (i = 0; i < N; i++) |
| 23 | // arr[i] = i; |
| 24 | // |
| 25 | // is e.g. simplified to: |
| 26 | // |
| 27 | // for (i = 0; i < N; i++) |
| 28 | // arr[i] = i; |
| 29 | // |
| 30 | // The idea and the algorithm used was first implemented by Sven Verdoolaege in |
| 31 | // the 'ppcg' tool. |
Tobias Grosser | c30d9cc | 2013-03-23 00:16:05 +0000 | [diff] [blame] | 32 | // |
Tobias Grosser | 1d34867 | 2012-01-31 14:00:27 +0000 | [diff] [blame] | 33 | //===----------------------------------------------------------------------===// |
| 34 | |
Johannes Doerfert | f6557f9 | 2015-03-04 22:43:40 +0000 | [diff] [blame] | 35 | #include "polly/DependenceInfo.h" |
Tobias Grosser | 1d34867 | 2012-01-31 14:00:27 +0000 | [diff] [blame] | 36 | #include "polly/LinkAllPasses.h" |
| 37 | #include "polly/ScopInfo.h" |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 38 | #include "llvm/Support/CommandLine.h" |
Tobias Grosser | 780ce0f | 2014-07-11 07:12:10 +0000 | [diff] [blame] | 39 | #include "isl/flow.h" |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 40 | #include "isl/map.h" |
Tobias Grosser | ba0d092 | 2015-05-09 09:13:42 +0000 | [diff] [blame] | 41 | #include "isl/set.h" |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 42 | #include "isl/union_map.h" |
Tobias Grosser | cd524dc | 2015-05-09 09:36:38 +0000 | [diff] [blame] | 43 | #include "isl/union_set.h" |
Tobias Grosser | 1d34867 | 2012-01-31 14:00:27 +0000 | [diff] [blame] | 44 | |
Tobias Grosser | 1d34867 | 2012-01-31 14:00:27 +0000 | [diff] [blame] | 45 | using namespace llvm; |
| 46 | using namespace polly; |
| 47 | |
| 48 | namespace { |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 49 | |
Tobias Grosser | 817d51d | 2014-02-21 20:51:46 +0000 | [diff] [blame] | 50 | cl::opt<int> DCEPreciseSteps( |
| 51 | "polly-dce-precise-steps", |
Tobias Grosser | 356faa8 | 2014-02-24 08:52:20 +0000 | [diff] [blame] | 52 | cl::desc("The number of precise steps between two approximating " |
| 53 | "iterations. (A value of -1 schedules another approximation stage " |
| 54 | "before the actual dead code elimination."), |
Tobias Grosser | 64e8e37 | 2014-03-13 23:37:43 +0000 | [diff] [blame] | 55 | cl::ZeroOrMore, cl::init(-1)); |
Tobias Grosser | 1d34867 | 2012-01-31 14:00:27 +0000 | [diff] [blame] | 56 | |
Tobias Grosser | 184e2fc | 2013-01-08 08:27:46 +0000 | [diff] [blame] | 57 | class DeadCodeElim : public ScopPass { |
Tobias Grosser | 184e2fc | 2013-01-08 08:27:46 +0000 | [diff] [blame] | 58 | public: |
| 59 | static char ID; |
Tobias Grosser | c30d9cc | 2013-03-23 00:16:05 +0000 | [diff] [blame] | 60 | explicit DeadCodeElim() : ScopPass(ID) {} |
Tobias Grosser | 1d34867 | 2012-01-31 14:00:27 +0000 | [diff] [blame] | 61 | |
Johannes Doerfert | 45be644 | 2015-09-27 15:43:29 +0000 | [diff] [blame] | 62 | /// @brief Remove dead iterations from the schedule of @p S. |
Johannes Doerfert | 909a3bf | 2015-03-01 18:42:08 +0000 | [diff] [blame] | 63 | bool runOnScop(Scop &S) override; |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 64 | |
Johannes Doerfert | 45be644 | 2015-09-27 15:43:29 +0000 | [diff] [blame] | 65 | /// @brief Register all analyses and transformation required. |
Johannes Doerfert | 909a3bf | 2015-03-01 18:42:08 +0000 | [diff] [blame] | 66 | void getAnalysisUsage(AnalysisUsage &AU) const override; |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 67 | |
| 68 | private: |
Tobias Grosser | 780ce0f | 2014-07-11 07:12:10 +0000 | [diff] [blame] | 69 | /// @brief Return the set of live iterations. |
| 70 | /// |
| 71 | /// The set of live iterations are all iterations that write to memory and for |
| 72 | /// which we can not prove that there will be a later write that _must_ |
| 73 | /// overwrite the same memory location and is consequently the only one that |
| 74 | /// is visible after the execution of the SCoP. |
| 75 | /// |
| 76 | isl_union_set *getLiveOut(Scop &S); |
Tobias Grosser | 817d51d | 2014-02-21 20:51:46 +0000 | [diff] [blame] | 77 | bool eliminateDeadCode(Scop &S, int PreciseSteps); |
Tobias Grosser | 184e2fc | 2013-01-08 08:27:46 +0000 | [diff] [blame] | 78 | }; |
Tobias Grosser | 1d34867 | 2012-01-31 14:00:27 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | char DeadCodeElim::ID = 0; |
| 82 | |
Tobias Grosser | c2920ff | 2014-07-14 08:32:01 +0000 | [diff] [blame] | 83 | // To compute the live outs, we compute for the data-locations that are |
| 84 | // must-written to the last statement that touches these locations. On top of |
| 85 | // this we add all statements that perform may-write accesses. |
| 86 | // |
| 87 | // We could be more precise by removing may-write accesses for which we know |
| 88 | // that they are overwritten by a must-write after. However, at the moment the |
| 89 | // only may-writes we introduce access the full (unbounded) array, such that |
| 90 | // bounded write accesses can not overwrite all of the data-locations. As |
| 91 | // this means may-writes are in the current situation always live, there is |
| 92 | // no point in trying to remove them from the live-out set. |
Tobias Grosser | 780ce0f | 2014-07-11 07:12:10 +0000 | [diff] [blame] | 93 | isl_union_set *DeadCodeElim::getLiveOut(Scop &S) { |
Tobias Grosser | c2920ff | 2014-07-14 08:32:01 +0000 | [diff] [blame] | 94 | isl_union_map *Schedule = S.getSchedule(); |
| 95 | isl_union_map *WriteIterations = isl_union_map_reverse(S.getMustWrites()); |
| 96 | isl_union_map *WriteTimes = |
| 97 | isl_union_map_apply_range(WriteIterations, isl_union_map_copy(Schedule)); |
Tobias Grosser | 1d34867 | 2012-01-31 14:00:27 +0000 | [diff] [blame] | 98 | |
Tobias Grosser | c2920ff | 2014-07-14 08:32:01 +0000 | [diff] [blame] | 99 | isl_union_map *LastWriteTimes = isl_union_map_lexmax(WriteTimes); |
| 100 | isl_union_map *LastWriteIterations = isl_union_map_apply_range( |
| 101 | LastWriteTimes, isl_union_map_reverse(Schedule)); |
Tobias Grosser | 1d34867 | 2012-01-31 14:00:27 +0000 | [diff] [blame] | 102 | |
Tobias Grosser | c2920ff | 2014-07-14 08:32:01 +0000 | [diff] [blame] | 103 | isl_union_set *Live = isl_union_map_range(LastWriteIterations); |
| 104 | Live = isl_union_set_union(Live, isl_union_map_domain(S.getMayWrites())); |
| 105 | return isl_union_set_coalesce(Live); |
Tobias Grosser | 1d34867 | 2012-01-31 14:00:27 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 108 | /// Performs polyhedral dead iteration elimination by: |
| 109 | /// o Assuming that the last write to each location is live. |
| 110 | /// o Following each RAW dependency from a live iteration backwards and adding |
| 111 | /// that iteration to the live set. |
Tobias Grosser | 817d51d | 2014-02-21 20:51:46 +0000 | [diff] [blame] | 112 | /// |
| 113 | /// To ensure the set of live iterations does not get too complex we always |
| 114 | /// combine a certain number of precise steps with one approximating step that |
| 115 | /// simplifies the life set with an affine hull. |
| 116 | bool DeadCodeElim::eliminateDeadCode(Scop &S, int PreciseSteps) { |
Johannes Doerfert | 7e6424b | 2015-03-05 00:43:48 +0000 | [diff] [blame] | 117 | DependenceInfo &DI = getAnalysis<DependenceInfo>(); |
| 118 | const Dependences &D = DI.getDependences(); |
Tobias Grosser | 38c36ea | 2014-02-23 15:15:44 +0000 | [diff] [blame] | 119 | |
Johannes Doerfert | 7e6424b | 2015-03-05 00:43:48 +0000 | [diff] [blame] | 120 | if (!D.hasValidDependences()) |
Tobias Grosser | 38c36ea | 2014-02-23 15:15:44 +0000 | [diff] [blame] | 121 | return false; |
| 122 | |
Tobias Grosser | 780ce0f | 2014-07-11 07:12:10 +0000 | [diff] [blame] | 123 | isl_union_set *Live = getLiveOut(S); |
Johannes Doerfert | f190613 | 2014-06-20 16:37:11 +0000 | [diff] [blame] | 124 | isl_union_map *Dep = |
Johannes Doerfert | 7e6424b | 2015-03-05 00:43:48 +0000 | [diff] [blame] | 125 | D.getDependences(Dependences::TYPE_RAW | Dependences::TYPE_RED); |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 126 | Dep = isl_union_map_reverse(Dep); |
| 127 | |
Tobias Grosser | 356faa8 | 2014-02-24 08:52:20 +0000 | [diff] [blame] | 128 | if (PreciseSteps == -1) |
| 129 | Live = isl_union_set_affine_hull(Live); |
| 130 | |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 131 | isl_union_set *OriginalDomain = S.getDomains(); |
Tobias Grosser | 817d51d | 2014-02-21 20:51:46 +0000 | [diff] [blame] | 132 | int Steps = 0; |
Tobias Grosser | 0bc0ad0 | 2014-02-21 21:06:08 +0000 | [diff] [blame] | 133 | while (true) { |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 134 | isl_union_set *Extra; |
Tobias Grosser | 817d51d | 2014-02-21 20:51:46 +0000 | [diff] [blame] | 135 | Steps++; |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 136 | |
| 137 | Extra = |
| 138 | isl_union_set_apply(isl_union_set_copy(Live), isl_union_map_copy(Dep)); |
| 139 | |
| 140 | if (isl_union_set_is_subset(Extra, Live)) { |
| 141 | isl_union_set_free(Extra); |
| 142 | break; |
| 143 | } |
| 144 | |
| 145 | Live = isl_union_set_union(Live, Extra); |
Tobias Grosser | 817d51d | 2014-02-21 20:51:46 +0000 | [diff] [blame] | 146 | |
| 147 | if (Steps > PreciseSteps) { |
| 148 | Steps = 0; |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 149 | Live = isl_union_set_affine_hull(Live); |
Tobias Grosser | 817d51d | 2014-02-21 20:51:46 +0000 | [diff] [blame] | 150 | } |
| 151 | |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 152 | Live = isl_union_set_intersect(Live, isl_union_set_copy(OriginalDomain)); |
| 153 | } |
| 154 | isl_union_map_free(Dep); |
| 155 | isl_union_set_free(OriginalDomain); |
| 156 | |
Tobias Grosser | 11e3873 | 2014-12-17 21:13:55 +0000 | [diff] [blame] | 157 | bool Changed = S.restrictDomains(isl_union_set_coalesce(Live)); |
| 158 | |
| 159 | // FIXME: We can probably avoid the recomputation of all dependences by |
| 160 | // updating them explicitly. |
| 161 | if (Changed) |
Johannes Doerfert | 7e6424b | 2015-03-05 00:43:48 +0000 | [diff] [blame] | 162 | DI.recomputeDependences(); |
Tobias Grosser | 11e3873 | 2014-12-17 21:13:55 +0000 | [diff] [blame] | 163 | return Changed; |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Tobias Grosser | 817d51d | 2014-02-21 20:51:46 +0000 | [diff] [blame] | 166 | bool DeadCodeElim::runOnScop(Scop &S) { |
| 167 | return eliminateDeadCode(S, DCEPreciseSteps); |
| 168 | } |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 169 | |
Tobias Grosser | 1d34867 | 2012-01-31 14:00:27 +0000 | [diff] [blame] | 170 | void DeadCodeElim::getAnalysisUsage(AnalysisUsage &AU) const { |
| 171 | ScopPass::getAnalysisUsage(AU); |
Johannes Doerfert | f6557f9 | 2015-03-04 22:43:40 +0000 | [diff] [blame] | 172 | AU.addRequired<DependenceInfo>(); |
Tobias Grosser | 1d34867 | 2012-01-31 14:00:27 +0000 | [diff] [blame] | 173 | } |
| 174 | |
Tobias Grosser | c30d9cc | 2013-03-23 00:16:05 +0000 | [diff] [blame] | 175 | Pass *polly::createDeadCodeElimPass() { return new DeadCodeElim(); } |
Tobias Grosser | 1d34867 | 2012-01-31 14:00:27 +0000 | [diff] [blame] | 176 | |
Tobias Grosser | c30d9cc | 2013-03-23 00:16:05 +0000 | [diff] [blame] | 177 | INITIALIZE_PASS_BEGIN(DeadCodeElim, "polly-dce", |
Tobias Grosser | d7e5864 | 2013-04-10 06:55:45 +0000 | [diff] [blame] | 178 | "Polly - Remove dead iterations", false, false) |
Johannes Doerfert | f6557f9 | 2015-03-04 22:43:40 +0000 | [diff] [blame] | 179 | INITIALIZE_PASS_DEPENDENCY(DependenceInfo) |
Tobias Grosser | d7e5864 | 2013-04-10 06:55:45 +0000 | [diff] [blame] | 180 | INITIALIZE_PASS_DEPENDENCY(ScopInfo) |
Tobias Grosser | c30d9cc | 2013-03-23 00:16:05 +0000 | [diff] [blame] | 181 | INITIALIZE_PASS_END(DeadCodeElim, "polly-dce", "Polly - Remove dead iterations", |
| 182 | false, false) |