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 // 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. 15 // 16 // This code: 17 // 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. 32 // 33 //===----------------------------------------------------------------------===// 34 35 #include "polly/DependenceInfo.h" 36 #include "polly/LinkAllPasses.h" 37 #include "polly/ScopInfo.h" 38 #include "llvm/Support/CommandLine.h" 39 #include "isl/flow.h" 40 #include "isl/map.h" 41 #include "isl/set.h" 42 #include "isl/union_map.h" 43 #include "isl/union_set.h" 44 45 using namespace llvm; 46 using namespace polly; 47 48 namespace { 49 50 cl::opt<int> DCEPreciseSteps( 51 "polly-dce-precise-steps", 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."), 55 cl::ZeroOrMore, cl::init(-1)); 56 57 class DeadCodeElim : public ScopPass { 58 public: 59 static char ID; 60 explicit DeadCodeElim() : ScopPass(ID) {} 61 62 /// @brief Remove dead iterations from the schedule of @p S. 63 bool runOnScop(Scop &S) override; 64 65 /// @brief Register all analyses and transformation required. 66 void getAnalysisUsage(AnalysisUsage &AU) const override; 67 68 private: 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); 77 bool eliminateDeadCode(Scop &S, int PreciseSteps); 78 }; 79 } 80 81 char DeadCodeElim::ID = 0; 82 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. 93 isl_union_set *DeadCodeElim::getLiveOut(Scop &S) { 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)); 98 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)); 102 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); 106 } 107 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. 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) { 117 DependenceInfo &DI = getAnalysis<DependenceInfo>(); 118 const Dependences &D = DI.getDependences(); 119 120 if (!D.hasValidDependences()) 121 return false; 122 123 isl_union_set *Live = getLiveOut(S); 124 isl_union_map *Dep = 125 D.getDependences(Dependences::TYPE_RAW | Dependences::TYPE_RED); 126 Dep = isl_union_map_reverse(Dep); 127 128 if (PreciseSteps == -1) 129 Live = isl_union_set_affine_hull(Live); 130 131 isl_union_set *OriginalDomain = S.getDomains(); 132 int Steps = 0; 133 while (true) { 134 isl_union_set *Extra; 135 Steps++; 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); 146 147 if (Steps > PreciseSteps) { 148 Steps = 0; 149 Live = isl_union_set_affine_hull(Live); 150 } 151 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 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) 162 DI.recomputeDependences(); 163 return Changed; 164 } 165 166 bool DeadCodeElim::runOnScop(Scop &S) { 167 return eliminateDeadCode(S, DCEPreciseSteps); 168 } 169 170 void DeadCodeElim::getAnalysisUsage(AnalysisUsage &AU) const { 171 ScopPass::getAnalysisUsage(AU); 172 AU.addRequired<DependenceInfo>(); 173 } 174 175 Pass *polly::createDeadCodeElimPass() { return new DeadCodeElim(); } 176 177 INITIALIZE_PASS_BEGIN(DeadCodeElim, "polly-dce", 178 "Polly - Remove dead iterations", false, false) 179 INITIALIZE_PASS_DEPENDENCY(DependenceInfo) 180 INITIALIZE_PASS_DEPENDENCY(ScopInfo) 181 INITIALIZE_PASS_END(DeadCodeElim, "polly-dce", "Polly - Remove dead iterations", 182 false, false) 183