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/set.h" 41 #include "isl/map.h" 42 #include "isl/union_map.h" 43 44 using namespace llvm; 45 using namespace polly; 46 47 namespace { 48 49 cl::opt<int> DCEPreciseSteps( 50 "polly-dce-precise-steps", 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."), 54 cl::ZeroOrMore, cl::init(-1)); 55 56 class DeadCodeElim : public ScopPass { 57 public: 58 static char ID; 59 explicit DeadCodeElim() : ScopPass(ID) {} 60 61 bool runOnScop(Scop &S) override; 62 63 void printScop(raw_ostream &OS, Scop &S) const override; 64 void getAnalysisUsage(AnalysisUsage &AU) const override; 65 66 private: 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); 75 bool eliminateDeadCode(Scop &S, int PreciseSteps); 76 }; 77 } 78 79 char DeadCodeElim::ID = 0; 80 81 // To compute the live outs, we compute for the data-locations that are 82 // must-written to the last statement that touches these locations. On top of 83 // this we add all statements that perform may-write accesses. 84 // 85 // We could be more precise by removing may-write accesses for which we know 86 // that they are overwritten by a must-write after. However, at the moment the 87 // only may-writes we introduce access the full (unbounded) array, such that 88 // bounded write accesses can not overwrite all of the data-locations. As 89 // this means may-writes are in the current situation always live, there is 90 // no point in trying to remove them from the live-out set. 91 isl_union_set *DeadCodeElim::getLiveOut(Scop &S) { 92 isl_union_map *Schedule = S.getSchedule(); 93 isl_union_map *WriteIterations = isl_union_map_reverse(S.getMustWrites()); 94 isl_union_map *WriteTimes = 95 isl_union_map_apply_range(WriteIterations, isl_union_map_copy(Schedule)); 96 97 isl_union_map *LastWriteTimes = isl_union_map_lexmax(WriteTimes); 98 isl_union_map *LastWriteIterations = isl_union_map_apply_range( 99 LastWriteTimes, isl_union_map_reverse(Schedule)); 100 101 isl_union_set *Live = isl_union_map_range(LastWriteIterations); 102 Live = isl_union_set_union(Live, isl_union_map_domain(S.getMayWrites())); 103 return isl_union_set_coalesce(Live); 104 } 105 106 /// Performs polyhedral dead iteration elimination by: 107 /// o Assuming that the last write to each location is live. 108 /// o Following each RAW dependency from a live iteration backwards and adding 109 /// that iteration to the live set. 110 /// 111 /// To ensure the set of live iterations does not get too complex we always 112 /// combine a certain number of precise steps with one approximating step that 113 /// simplifies the life set with an affine hull. 114 bool DeadCodeElim::eliminateDeadCode(Scop &S, int PreciseSteps) { 115 DependenceInfo &DI = getAnalysis<DependenceInfo>(); 116 const Dependences &D = DI.getDependences(); 117 118 if (!D.hasValidDependences()) 119 return false; 120 121 isl_union_set *Live = getLiveOut(S); 122 isl_union_map *Dep = 123 D.getDependences(Dependences::TYPE_RAW | Dependences::TYPE_RED); 124 Dep = isl_union_map_reverse(Dep); 125 126 if (PreciseSteps == -1) 127 Live = isl_union_set_affine_hull(Live); 128 129 isl_union_set *OriginalDomain = S.getDomains(); 130 int Steps = 0; 131 while (true) { 132 isl_union_set *Extra; 133 Steps++; 134 135 Extra = 136 isl_union_set_apply(isl_union_set_copy(Live), isl_union_map_copy(Dep)); 137 138 if (isl_union_set_is_subset(Extra, Live)) { 139 isl_union_set_free(Extra); 140 break; 141 } 142 143 Live = isl_union_set_union(Live, Extra); 144 145 if (Steps > PreciseSteps) { 146 Steps = 0; 147 Live = isl_union_set_affine_hull(Live); 148 } 149 150 Live = isl_union_set_intersect(Live, isl_union_set_copy(OriginalDomain)); 151 } 152 isl_union_map_free(Dep); 153 isl_union_set_free(OriginalDomain); 154 155 bool Changed = S.restrictDomains(isl_union_set_coalesce(Live)); 156 157 // FIXME: We can probably avoid the recomputation of all dependences by 158 // updating them explicitly. 159 if (Changed) 160 DI.recomputeDependences(); 161 return Changed; 162 } 163 164 bool DeadCodeElim::runOnScop(Scop &S) { 165 return eliminateDeadCode(S, DCEPreciseSteps); 166 } 167 168 void DeadCodeElim::printScop(raw_ostream &, Scop &) const {} 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