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 bool runOnScop(Scop &S) override; 63 64 void printScop(raw_ostream &OS, Scop &S) const override; 65 void getAnalysisUsage(AnalysisUsage &AU) const override; 66 67 private: 68 /// @brief Return the set of live iterations. 69 /// 70 /// The set of live iterations are all iterations that write to memory and for 71 /// which we can not prove that there will be a later write that _must_ 72 /// overwrite the same memory location and is consequently the only one that 73 /// is visible after the execution of the SCoP. 74 /// 75 isl_union_set *getLiveOut(Scop &S); 76 bool eliminateDeadCode(Scop &S, int PreciseSteps); 77 }; 78 } 79 80 char DeadCodeElim::ID = 0; 81 82 // To compute the live outs, we compute for the data-locations that are 83 // must-written to the last statement that touches these locations. On top of 84 // this we add all statements that perform may-write accesses. 85 // 86 // We could be more precise by removing may-write accesses for which we know 87 // that they are overwritten by a must-write after. However, at the moment the 88 // only may-writes we introduce access the full (unbounded) array, such that 89 // bounded write accesses can not overwrite all of the data-locations. As 90 // this means may-writes are in the current situation always live, there is 91 // no point in trying to remove them from the live-out set. 92 isl_union_set *DeadCodeElim::getLiveOut(Scop &S) { 93 isl_union_map *Schedule = S.getSchedule(); 94 isl_union_map *WriteIterations = isl_union_map_reverse(S.getMustWrites()); 95 isl_union_map *WriteTimes = 96 isl_union_map_apply_range(WriteIterations, isl_union_map_copy(Schedule)); 97 98 isl_union_map *LastWriteTimes = isl_union_map_lexmax(WriteTimes); 99 isl_union_map *LastWriteIterations = isl_union_map_apply_range( 100 LastWriteTimes, isl_union_map_reverse(Schedule)); 101 102 isl_union_set *Live = isl_union_map_range(LastWriteIterations); 103 Live = isl_union_set_union(Live, isl_union_map_domain(S.getMayWrites())); 104 return isl_union_set_coalesce(Live); 105 } 106 107 /// Performs polyhedral dead iteration elimination by: 108 /// o Assuming that the last write to each location is live. 109 /// o Following each RAW dependency from a live iteration backwards and adding 110 /// that iteration to the live set. 111 /// 112 /// To ensure the set of live iterations does not get too complex we always 113 /// combine a certain number of precise steps with one approximating step that 114 /// simplifies the life set with an affine hull. 115 bool DeadCodeElim::eliminateDeadCode(Scop &S, int PreciseSteps) { 116 DependenceInfo &DI = getAnalysis<DependenceInfo>(); 117 const Dependences &D = DI.getDependences(); 118 119 if (!D.hasValidDependences()) 120 return false; 121 122 isl_union_set *Live = getLiveOut(S); 123 isl_union_map *Dep = 124 D.getDependences(Dependences::TYPE_RAW | Dependences::TYPE_RED); 125 Dep = isl_union_map_reverse(Dep); 126 127 if (PreciseSteps == -1) 128 Live = isl_union_set_affine_hull(Live); 129 130 isl_union_set *OriginalDomain = S.getDomains(); 131 int Steps = 0; 132 while (true) { 133 isl_union_set *Extra; 134 Steps++; 135 136 Extra = 137 isl_union_set_apply(isl_union_set_copy(Live), isl_union_map_copy(Dep)); 138 139 if (isl_union_set_is_subset(Extra, Live)) { 140 isl_union_set_free(Extra); 141 break; 142 } 143 144 Live = isl_union_set_union(Live, Extra); 145 146 if (Steps > PreciseSteps) { 147 Steps = 0; 148 Live = isl_union_set_affine_hull(Live); 149 } 150 151 Live = isl_union_set_intersect(Live, isl_union_set_copy(OriginalDomain)); 152 } 153 isl_union_map_free(Dep); 154 isl_union_set_free(OriginalDomain); 155 156 bool Changed = S.restrictDomains(isl_union_set_coalesce(Live)); 157 158 // FIXME: We can probably avoid the recomputation of all dependences by 159 // updating them explicitly. 160 if (Changed) 161 DI.recomputeDependences(); 162 return Changed; 163 } 164 165 bool DeadCodeElim::runOnScop(Scop &S) { 166 return eliminateDeadCode(S, DCEPreciseSteps); 167 } 168 169 void DeadCodeElim::printScop(raw_ostream &, Scop &) const {} 170 171 void DeadCodeElim::getAnalysisUsage(AnalysisUsage &AU) const { 172 ScopPass::getAnalysisUsage(AU); 173 AU.addRequired<DependenceInfo>(); 174 } 175 176 Pass *polly::createDeadCodeElimPass() { return new DeadCodeElim(); } 177 178 INITIALIZE_PASS_BEGIN(DeadCodeElim, "polly-dce", 179 "Polly - Remove dead iterations", false, false) 180 INITIALIZE_PASS_DEPENDENCY(DependenceInfo) 181 INITIALIZE_PASS_DEPENDENCY(ScopInfo) 182 INITIALIZE_PASS_END(DeadCodeElim, "polly-dce", "Polly - Remove dead iterations", 183 false, false) 184