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/Dependences.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   virtual bool runOnScop(Scop &S);
62 
63   void printScop(llvm::raw_ostream &OS) const;
64   void getAnalysisUsage(AnalysisUsage &AU) const;
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   Dependences *D = &getAnalysis<Dependences>();
116 
117   if (!D->hasValidDependences())
118     return false;
119 
120   isl_union_set *Live = getLiveOut(S);
121   isl_union_map *Dep =
122       D->getDependences(Dependences::TYPE_RAW | Dependences::TYPE_RED);
123   Dep = isl_union_map_reverse(Dep);
124 
125   if (PreciseSteps == -1)
126     Live = isl_union_set_affine_hull(Live);
127 
128   isl_union_set *OriginalDomain = S.getDomains();
129   int Steps = 0;
130   while (true) {
131     isl_union_set *Extra;
132     Steps++;
133 
134     Extra =
135         isl_union_set_apply(isl_union_set_copy(Live), isl_union_map_copy(Dep));
136 
137     if (isl_union_set_is_subset(Extra, Live)) {
138       isl_union_set_free(Extra);
139       break;
140     }
141 
142     Live = isl_union_set_union(Live, Extra);
143 
144     if (Steps > PreciseSteps) {
145       Steps = 0;
146       Live = isl_union_set_affine_hull(Live);
147     }
148 
149     Live = isl_union_set_intersect(Live, isl_union_set_copy(OriginalDomain));
150   }
151   isl_union_map_free(Dep);
152   isl_union_set_free(OriginalDomain);
153 
154   return S.restrictDomains(isl_union_set_coalesce(Live));
155 }
156 
157 bool DeadCodeElim::runOnScop(Scop &S) {
158   return eliminateDeadCode(S, DCEPreciseSteps);
159 }
160 
161 void DeadCodeElim::printScop(raw_ostream &OS) const {}
162 
163 void DeadCodeElim::getAnalysisUsage(AnalysisUsage &AU) const {
164   ScopPass::getAnalysisUsage(AU);
165   AU.addRequired<Dependences>();
166 }
167 
168 Pass *polly::createDeadCodeElimPass() { return new DeadCodeElim(); }
169 
170 INITIALIZE_PASS_BEGIN(DeadCodeElim, "polly-dce",
171                       "Polly - Remove dead iterations", false, false)
172 INITIALIZE_PASS_DEPENDENCY(Dependences)
173 INITIALIZE_PASS_DEPENDENCY(ScopInfo)
174 INITIALIZE_PASS_END(DeadCodeElim, "polly-dce", "Polly - Remove dead iterations",
175                     false, false)
176