blob: 445c46d038e381e5a79c6e494f84aff22a17efff [file] [log] [blame]
Jean Christophe Beyler2469e602014-05-06 20:36:55 -07001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Narayan Kamathfd5a8522014-05-30 11:58:09 +010017#ifndef ART_COMPILER_DEX_POST_OPT_PASSES_H_
18#define ART_COMPILER_DEX_POST_OPT_PASSES_H_
Jean Christophe Beyler2469e602014-05-06 20:36:55 -070019
20#include "compiler_internals.h"
21#include "pass_me.h"
22
23namespace art {
24
25/**
26 * @class InitializeData
27 * @brief There is some data that needs to be initialized before performing
28 * the post optimization passes.
29 */
30class InitializeData : public PassME {
31 public:
32 InitializeData() : PassME("InitializeData") {
33 }
34
Jean Christophe Beylere1f65bc2014-06-09 10:18:26 -070035 void Start(PassDataHolder* data) const {
Jean Christophe Beyler2469e602014-05-06 20:36:55 -070036 // New blocks may have been inserted so the first thing we do is ensure that
37 // the c_unit's number of blocks matches the actual count of basic blocks.
38 DCHECK(data != nullptr);
Jean Christophe Beylere1f65bc2014-06-09 10:18:26 -070039 CompilationUnit* c_unit = down_cast<PassMEDataHolder*>(data)->c_unit;
Jean Christophe Beyler2469e602014-05-06 20:36:55 -070040 DCHECK(c_unit != nullptr);
41 c_unit->mir_graph.get()->InitializeBasicBlockData();
42 c_unit->mir_graph.get()->SSATransformationStart();
43 }
44};
45
46/**
47 * @class MethodUseCount
48 * @brief Count the register uses of the method
49 */
50class MethodUseCount : public PassME {
51 public:
52 MethodUseCount() : PassME("UseCount") {
53 }
54
55 bool Worker(const PassDataHolder* data) const;
56
57 bool Gate(const PassDataHolder* data) const;
58};
59
60/**
61 * @class ClearPhiInformation
62 * @brief Clear the PHI nodes from the CFG.
63 */
64class ClearPhiInstructions : public PassME {
65 public:
66 ClearPhiInstructions() : PassME("ClearPhiInstructions") {
67 }
68
69 bool Worker(const PassDataHolder* data) const;
70};
71
72/**
73 * @class CalculatePredecessors
74 * @brief Calculate the predecessor BitVector of each Basicblock.
75 */
76class CalculatePredecessors : public PassME {
77 public:
78 CalculatePredecessors() : PassME("CalculatePredecessors") {
79 }
80
Jean Christophe Beylere1f65bc2014-06-09 10:18:26 -070081 void Start(PassDataHolder* data) const;
Jean Christophe Beyler2469e602014-05-06 20:36:55 -070082};
83
84/**
85 * @class DFSOrders
86 * @brief Compute the DFS order of the MIR graph
87 */
88class DFSOrders : public PassME {
89 public:
90 DFSOrders() : PassME("DFSOrders") {
91 }
92
Jean Christophe Beylere1f65bc2014-06-09 10:18:26 -070093 void Start(PassDataHolder* data) const {
Jean Christophe Beyler2469e602014-05-06 20:36:55 -070094 DCHECK(data != nullptr);
Jean Christophe Beylere1f65bc2014-06-09 10:18:26 -070095 CompilationUnit* c_unit = down_cast<PassMEDataHolder*>(data)->c_unit;
Jean Christophe Beyler2469e602014-05-06 20:36:55 -070096 DCHECK(c_unit != nullptr);
97 c_unit->mir_graph.get()->ComputeDFSOrders();
98 }
99};
100
101/**
102 * @class BuildDomination
103 * @brief Build the domination information of the MIR Graph
104 */
105class BuildDomination : public PassME {
106 public:
107 BuildDomination() : PassME("BuildDomination") {
108 }
109
Jean Christophe Beylere1f65bc2014-06-09 10:18:26 -0700110 void Start(PassDataHolder* data) const {
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700111 DCHECK(data != nullptr);
Jean Christophe Beylere1f65bc2014-06-09 10:18:26 -0700112 CompilationUnit* c_unit = down_cast<PassMEDataHolder*>(data)->c_unit;
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700113 DCHECK(c_unit != nullptr);
114 c_unit->mir_graph.get()->ComputeDominators();
115 c_unit->mir_graph.get()->CompilerInitializeSSAConversion();
116 }
117
Jean Christophe Beylere1f65bc2014-06-09 10:18:26 -0700118 void End(PassDataHolder* data) const {
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700119 DCHECK(data != nullptr);
Jean Christophe Beylere1f65bc2014-06-09 10:18:26 -0700120 CompilationUnit* c_unit = down_cast<PassMEDataHolder*>(data)->c_unit;
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700121 DCHECK(c_unit != nullptr);
122 // Verify the dataflow information after the pass.
123 if (c_unit->enable_debug & (1 << kDebugVerifyDataflow)) {
124 c_unit->mir_graph->VerifyDataflow();
125 }
126 }
127};
128
129/**
130 * @class DefBlockMatrix
131 * @brief Calculate the matrix of definition per basic block
132 */
133class DefBlockMatrix : public PassME {
134 public:
135 DefBlockMatrix() : PassME("DefBlockMatrix") {
136 }
137
Jean Christophe Beylere1f65bc2014-06-09 10:18:26 -0700138 void Start(PassDataHolder* data) const {
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700139 DCHECK(data != nullptr);
Jean Christophe Beylere1f65bc2014-06-09 10:18:26 -0700140 CompilationUnit* c_unit = down_cast<PassMEDataHolder*>(data)->c_unit;
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700141 DCHECK(c_unit != nullptr);
142 c_unit->mir_graph.get()->ComputeDefBlockMatrix();
143 }
144};
145
146/**
147 * @class CreatePhiNodes
148 * @brief Pass to create the phi nodes after SSA calculation
149 */
150class CreatePhiNodes : public PassME {
151 public:
152 CreatePhiNodes() : PassME("CreatePhiNodes") {
153 }
154
Jean Christophe Beylere1f65bc2014-06-09 10:18:26 -0700155 void Start(PassDataHolder* data) const {
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700156 DCHECK(data != nullptr);
Jean Christophe Beylere1f65bc2014-06-09 10:18:26 -0700157 CompilationUnit* c_unit = down_cast<PassMEDataHolder*>(data)->c_unit;
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700158 DCHECK(c_unit != nullptr);
159 c_unit->mir_graph.get()->InsertPhiNodes();
160 }
161};
162
163/**
164 * @class ClearVisitedFlag
165 * @brief Pass to clear the visited flag for all basic blocks.
166 */
167
168class ClearVisitedFlag : public PassME {
169 public:
170 ClearVisitedFlag() : PassME("ClearVisitedFlag") {
171 }
172
Jean Christophe Beylere1f65bc2014-06-09 10:18:26 -0700173 void Start(PassDataHolder* data) const {
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700174 DCHECK(data != nullptr);
Jean Christophe Beylere1f65bc2014-06-09 10:18:26 -0700175 CompilationUnit* c_unit = down_cast<PassMEDataHolder*>(data)->c_unit;
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700176 DCHECK(c_unit != nullptr);
177 c_unit->mir_graph.get()->ClearAllVisitedFlags();
178 }
179};
180
181/**
182 * @class SSAConversion
183 * @brief Pass for SSA conversion of MIRs
184 */
185class SSAConversion : public PassME {
186 public:
187 SSAConversion() : PassME("SSAConversion") {
188 }
189
Jean Christophe Beylere1f65bc2014-06-09 10:18:26 -0700190 void Start(PassDataHolder* data) const {
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700191 DCHECK(data != nullptr);
Jean Christophe Beylere1f65bc2014-06-09 10:18:26 -0700192 CompilationUnit* c_unit = down_cast<PassMEDataHolder*>(data)->c_unit;
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700193 DCHECK(c_unit != nullptr);
194 MIRGraph *mir_graph = c_unit->mir_graph.get();
195 mir_graph->DoDFSPreOrderSSARename(mir_graph->GetEntryBlock());
196 }
197};
198
199/**
200 * @class PhiNodeOperands
201 * @brief Pass to insert the Phi node operands to basic blocks
202 */
203class PhiNodeOperands : public PassME {
204 public:
205 PhiNodeOperands() : PassME("PhiNodeOperands", kPreOrderDFSTraversal) {
206 }
207
208 bool Worker(const PassDataHolder* data) const {
209 DCHECK(data != nullptr);
210 CompilationUnit* c_unit = down_cast<const PassMEDataHolder*>(data)->c_unit;
211 DCHECK(c_unit != nullptr);
212 BasicBlock* bb = down_cast<const PassMEDataHolder*>(data)->bb;
213 DCHECK(bb != nullptr);
214 c_unit->mir_graph->InsertPhiNodeOperands(bb);
215 // No need of repeating, so just return false.
216 return false;
217 }
218};
219
220/**
221 * @class InitRegLocations
222 * @brief Initialize Register Locations.
223 */
224class PerformInitRegLocations : public PassME {
225 public:
226 PerformInitRegLocations() : PassME("PerformInitRegLocation") {
227 }
228
Jean Christophe Beylere1f65bc2014-06-09 10:18:26 -0700229 void Start(PassDataHolder* data) const {
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700230 DCHECK(data != nullptr);
Jean Christophe Beylere1f65bc2014-06-09 10:18:26 -0700231 CompilationUnit* c_unit = down_cast<PassMEDataHolder*>(data)->c_unit;
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700232 DCHECK(c_unit != nullptr);
233 c_unit->mir_graph->InitRegLocations();
234 }
235};
236
237/**
238 * @class ConstantPropagation
239 * @brief Perform a constant propagation pass.
240 */
241class ConstantPropagation : public PassME {
242 public:
243 ConstantPropagation() : PassME("ConstantPropagation") {
244 }
245
246 bool Worker(const PassDataHolder* data) const {
247 DCHECK(data != nullptr);
248 CompilationUnit* c_unit = down_cast<const PassMEDataHolder*>(data)->c_unit;
249 DCHECK(c_unit != nullptr);
250 BasicBlock* bb = down_cast<const PassMEDataHolder*>(data)->bb;
251 DCHECK(bb != nullptr);
252 c_unit->mir_graph->DoConstantPropagation(bb);
253 // No need of repeating, so just return false.
254 return false;
255 }
256
Jean Christophe Beylere1f65bc2014-06-09 10:18:26 -0700257 void Start(PassDataHolder* data) const {
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700258 DCHECK(data != nullptr);
Jean Christophe Beylere1f65bc2014-06-09 10:18:26 -0700259 CompilationUnit* c_unit = down_cast<PassMEDataHolder*>(data)->c_unit;
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700260 DCHECK(c_unit != nullptr);
261 c_unit->mir_graph->InitializeConstantPropagation();
262 }
263};
264
265/**
266 * @class FreeData
267 * @brief There is some data that needs to be freed after performing the post optimization passes.
268 */
269class FreeData : public PassME {
270 public:
271 FreeData() : PassME("FreeData") {
272 }
273
Jean Christophe Beylere1f65bc2014-06-09 10:18:26 -0700274 void End(PassDataHolder* data) const {
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700275 DCHECK(data != nullptr);
Jean Christophe Beylere1f65bc2014-06-09 10:18:26 -0700276 CompilationUnit* c_unit = down_cast<PassMEDataHolder*>(data)->c_unit;
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700277 DCHECK(c_unit != nullptr);
278 c_unit->mir_graph.get()->SSATransformationEnd();
279 }
280};
281
282} // namespace art
283
Narayan Kamathfd5a8522014-05-30 11:58:09 +0100284#endif // ART_COMPILER_DEX_POST_OPT_PASSES_H_