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 | |
| 35 | #include "polly/Dependences.h" |
| 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/set.h" |
| 41 | #include "isl/map.h" |
| 42 | #include "isl/union_map.h" |
Tobias Grosser | 1d34867 | 2012-01-31 14:00:27 +0000 | [diff] [blame] | 43 | |
Tobias Grosser | 1d34867 | 2012-01-31 14:00:27 +0000 | [diff] [blame] | 44 | using namespace llvm; |
| 45 | using namespace polly; |
| 46 | |
| 47 | namespace { |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 48 | |
Tobias Grosser | 817d51d | 2014-02-21 20:51:46 +0000 | [diff] [blame] | 49 | cl::opt<int> DCEPreciseSteps( |
| 50 | "polly-dce-precise-steps", |
Tobias Grosser | 356faa8 | 2014-02-24 08:52:20 +0000 | [diff] [blame] | 51 | cl::desc("The number of precise steps between two approximating " |
| 52 | "iterations. (A value of -1 schedules another approximation stage " |
| 53 | "before the actual dead code elimination."), |
Tobias Grosser | 64e8e37 | 2014-03-13 23:37:43 +0000 | [diff] [blame] | 54 | cl::ZeroOrMore, cl::init(-1)); |
Tobias Grosser | 1d34867 | 2012-01-31 14:00:27 +0000 | [diff] [blame] | 55 | |
Tobias Grosser | 184e2fc | 2013-01-08 08:27:46 +0000 | [diff] [blame] | 56 | class DeadCodeElim : public ScopPass { |
Tobias Grosser | 184e2fc | 2013-01-08 08:27:46 +0000 | [diff] [blame] | 57 | public: |
| 58 | static char ID; |
Tobias Grosser | c30d9cc | 2013-03-23 00:16:05 +0000 | [diff] [blame] | 59 | explicit DeadCodeElim() : ScopPass(ID) {} |
Tobias Grosser | 1d34867 | 2012-01-31 14:00:27 +0000 | [diff] [blame] | 60 | |
Tobias Grosser | 184e2fc | 2013-01-08 08:27:46 +0000 | [diff] [blame] | 61 | virtual bool runOnScop(Scop &S); |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 62 | |
Tobias Grosser | 184e2fc | 2013-01-08 08:27:46 +0000 | [diff] [blame] | 63 | void printScop(llvm::raw_ostream &OS) const; |
| 64 | void getAnalysisUsage(AnalysisUsage &AU) const; |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 65 | |
| 66 | private: |
Tobias Grosser | 780ce0f | 2014-07-11 07:12:10 +0000 | [diff] [blame^] | 67 | /// @brief Return the set of live iterations. |
| 68 | /// |
| 69 | /// The set of live iterations are all iterations that write to memory and for |
| 70 | /// which we can not prove that there will be a later write that _must_ |
| 71 | /// overwrite the same memory location and is consequently the only one that |
| 72 | /// is visible after the execution of the SCoP. |
| 73 | /// |
| 74 | isl_union_set *getLiveOut(Scop &S); |
Tobias Grosser | 817d51d | 2014-02-21 20:51:46 +0000 | [diff] [blame] | 75 | bool eliminateDeadCode(Scop &S, int PreciseSteps); |
Tobias Grosser | 184e2fc | 2013-01-08 08:27:46 +0000 | [diff] [blame] | 76 | }; |
Tobias Grosser | 1d34867 | 2012-01-31 14:00:27 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | char DeadCodeElim::ID = 0; |
| 80 | |
Tobias Grosser | 780ce0f | 2014-07-11 07:12:10 +0000 | [diff] [blame^] | 81 | // To compute the live outs, we first assume all must and may-writes are exposed |
| 82 | // and then subtract the set of statements that are definitely overwritten. |
| 83 | isl_union_set *DeadCodeElim::getLiveOut(Scop &S) { |
| 84 | __isl_take isl_union_map *Kills = S.getMustWrites(); |
| 85 | __isl_take isl_union_map *Empty = isl_union_map_empty(S.getParamSpace()); |
Tobias Grosser | 1d34867 | 2012-01-31 14:00:27 +0000 | [diff] [blame] | 86 | |
Tobias Grosser | 780ce0f | 2014-07-11 07:12:10 +0000 | [diff] [blame^] | 87 | isl_union_map *Covering; |
| 88 | isl_union_map *Writes = S.getWrites(); |
| 89 | isl_union_map_compute_flow(Kills, Empty, isl_union_map_copy(Writes), |
| 90 | S.getSchedule(), NULL, &Covering, NULL, NULL); |
Tobias Grosser | 1d34867 | 2012-01-31 14:00:27 +0000 | [diff] [blame] | 91 | |
Tobias Grosser | 780ce0f | 2014-07-11 07:12:10 +0000 | [diff] [blame^] | 92 | isl_union_map *Exposed = Writes; |
| 93 | Exposed = |
| 94 | isl_union_map_subtract_domain(Exposed, isl_union_map_domain(Covering)); |
| 95 | return isl_union_map_domain(Exposed); |
Tobias Grosser | 1d34867 | 2012-01-31 14:00:27 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 98 | /// Performs polyhedral dead iteration elimination by: |
| 99 | /// o Assuming that the last write to each location is live. |
| 100 | /// o Following each RAW dependency from a live iteration backwards and adding |
| 101 | /// that iteration to the live set. |
Tobias Grosser | 817d51d | 2014-02-21 20:51:46 +0000 | [diff] [blame] | 102 | /// |
| 103 | /// To ensure the set of live iterations does not get too complex we always |
| 104 | /// combine a certain number of precise steps with one approximating step that |
| 105 | /// simplifies the life set with an affine hull. |
| 106 | bool DeadCodeElim::eliminateDeadCode(Scop &S, int PreciseSteps) { |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 107 | Dependences *D = &getAnalysis<Dependences>(); |
Tobias Grosser | 38c36ea | 2014-02-23 15:15:44 +0000 | [diff] [blame] | 108 | |
| 109 | if (!D->hasValidDependences()) |
| 110 | return false; |
| 111 | |
Tobias Grosser | 780ce0f | 2014-07-11 07:12:10 +0000 | [diff] [blame^] | 112 | isl_union_set *Live = getLiveOut(S); |
Johannes Doerfert | f190613 | 2014-06-20 16:37:11 +0000 | [diff] [blame] | 113 | isl_union_map *Dep = |
| 114 | D->getDependences(Dependences::TYPE_RAW | Dependences::TYPE_RED); |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 115 | Dep = isl_union_map_reverse(Dep); |
| 116 | |
Tobias Grosser | 356faa8 | 2014-02-24 08:52:20 +0000 | [diff] [blame] | 117 | if (PreciseSteps == -1) |
| 118 | Live = isl_union_set_affine_hull(Live); |
| 119 | |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 120 | isl_union_set *OriginalDomain = S.getDomains(); |
Tobias Grosser | 817d51d | 2014-02-21 20:51:46 +0000 | [diff] [blame] | 121 | int Steps = 0; |
Tobias Grosser | 0bc0ad0 | 2014-02-21 21:06:08 +0000 | [diff] [blame] | 122 | while (true) { |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 123 | isl_union_set *Extra; |
Tobias Grosser | 817d51d | 2014-02-21 20:51:46 +0000 | [diff] [blame] | 124 | Steps++; |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 125 | |
| 126 | Extra = |
| 127 | isl_union_set_apply(isl_union_set_copy(Live), isl_union_map_copy(Dep)); |
| 128 | |
| 129 | if (isl_union_set_is_subset(Extra, Live)) { |
| 130 | isl_union_set_free(Extra); |
| 131 | break; |
| 132 | } |
| 133 | |
| 134 | Live = isl_union_set_union(Live, Extra); |
Tobias Grosser | 817d51d | 2014-02-21 20:51:46 +0000 | [diff] [blame] | 135 | |
| 136 | if (Steps > PreciseSteps) { |
| 137 | Steps = 0; |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 138 | Live = isl_union_set_affine_hull(Live); |
Tobias Grosser | 817d51d | 2014-02-21 20:51:46 +0000 | [diff] [blame] | 139 | } |
| 140 | |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 141 | Live = isl_union_set_intersect(Live, isl_union_set_copy(OriginalDomain)); |
| 142 | } |
| 143 | isl_union_map_free(Dep); |
| 144 | isl_union_set_free(OriginalDomain); |
| 145 | |
| 146 | return S.restrictDomains(isl_union_set_coalesce(Live)); |
| 147 | } |
| 148 | |
Tobias Grosser | 817d51d | 2014-02-21 20:51:46 +0000 | [diff] [blame] | 149 | bool DeadCodeElim::runOnScop(Scop &S) { |
| 150 | return eliminateDeadCode(S, DCEPreciseSteps); |
| 151 | } |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 152 | |
Tobias Grosser | c30d9cc | 2013-03-23 00:16:05 +0000 | [diff] [blame] | 153 | void DeadCodeElim::printScop(raw_ostream &OS) const {} |
Tobias Grosser | 1d34867 | 2012-01-31 14:00:27 +0000 | [diff] [blame] | 154 | |
| 155 | void DeadCodeElim::getAnalysisUsage(AnalysisUsage &AU) const { |
| 156 | ScopPass::getAnalysisUsage(AU); |
| 157 | AU.addRequired<Dependences>(); |
| 158 | } |
| 159 | |
Tobias Grosser | c30d9cc | 2013-03-23 00:16:05 +0000 | [diff] [blame] | 160 | Pass *polly::createDeadCodeElimPass() { return new DeadCodeElim(); } |
Tobias Grosser | 1d34867 | 2012-01-31 14:00:27 +0000 | [diff] [blame] | 161 | |
Tobias Grosser | c30d9cc | 2013-03-23 00:16:05 +0000 | [diff] [blame] | 162 | INITIALIZE_PASS_BEGIN(DeadCodeElim, "polly-dce", |
Tobias Grosser | d7e5864 | 2013-04-10 06:55:45 +0000 | [diff] [blame] | 163 | "Polly - Remove dead iterations", false, false) |
| 164 | INITIALIZE_PASS_DEPENDENCY(Dependences) |
| 165 | INITIALIZE_PASS_DEPENDENCY(ScopInfo) |
Tobias Grosser | c30d9cc | 2013-03-23 00:16:05 +0000 | [diff] [blame] | 166 | INITIALIZE_PASS_END(DeadCodeElim, "polly-dce", "Polly - Remove dead iterations", |
| 167 | false, false) |