blob: 7b84ba88c56cc8110f0cf907ea8da24ef88022f6 [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
Andreas Gampe53c913b2014-08-12 23:19:23 -070020#include "dex/quick/mir_to_lir.h"
Jean Christophe Beyler2469e602014-05-06 20:36:55 -070021#include "compiler_internals.h"
22#include "pass_me.h"
23
24namespace art {
25
26/**
27 * @class InitializeData
28 * @brief There is some data that needs to be initialized before performing
29 * the post optimization passes.
30 */
31class InitializeData : public PassME {
32 public:
Vladimir Markoaa7b8a32014-10-15 11:35:44 +010033 InitializeData() : PassME("InitializeData", kNoNodes) {
Jean Christophe Beyler2469e602014-05-06 20:36:55 -070034 }
35
Jean Christophe Beylere1f65bc2014-06-09 10:18:26 -070036 void Start(PassDataHolder* data) const {
Jean Christophe Beyler2469e602014-05-06 20:36:55 -070037 // New blocks may have been inserted so the first thing we do is ensure that
38 // the c_unit's number of blocks matches the actual count of basic blocks.
39 DCHECK(data != nullptr);
Jean Christophe Beylere1f65bc2014-06-09 10:18:26 -070040 CompilationUnit* c_unit = down_cast<PassMEDataHolder*>(data)->c_unit;
Jean Christophe Beyler2469e602014-05-06 20:36:55 -070041 DCHECK(c_unit != nullptr);
42 c_unit->mir_graph.get()->InitializeBasicBlockData();
43 c_unit->mir_graph.get()->SSATransformationStart();
44 }
45};
46
47/**
48 * @class MethodUseCount
49 * @brief Count the register uses of the method
50 */
51class MethodUseCount : public PassME {
52 public:
53 MethodUseCount() : PassME("UseCount") {
54 }
55
Jean Christophe Beyler09321df2014-07-18 15:33:57 -070056 bool Worker(PassDataHolder* data) const;
Jean Christophe Beyler2469e602014-05-06 20:36:55 -070057
58 bool Gate(const PassDataHolder* data) const;
59};
60
61/**
62 * @class ClearPhiInformation
63 * @brief Clear the PHI nodes from the CFG.
64 */
65class ClearPhiInstructions : public PassME {
66 public:
67 ClearPhiInstructions() : PassME("ClearPhiInstructions") {
68 }
69
Jean Christophe Beyler09321df2014-07-18 15:33:57 -070070 bool Worker(PassDataHolder* data) const;
Jean Christophe Beyler2469e602014-05-06 20:36:55 -070071};
72
73/**
74 * @class CalculatePredecessors
75 * @brief Calculate the predecessor BitVector of each Basicblock.
76 */
77class CalculatePredecessors : public PassME {
78 public:
Vladimir Markoaa7b8a32014-10-15 11:35:44 +010079 CalculatePredecessors() : PassME("CalculatePredecessors", kNoNodes) {
Jean Christophe Beyler2469e602014-05-06 20:36:55 -070080 }
81
Jean Christophe Beylere1f65bc2014-06-09 10:18:26 -070082 void Start(PassDataHolder* data) const;
Jean Christophe Beyler2469e602014-05-06 20:36:55 -070083};
84
85/**
86 * @class DFSOrders
87 * @brief Compute the DFS order of the MIR graph
88 */
89class DFSOrders : public PassME {
90 public:
Vladimir Markoaa7b8a32014-10-15 11:35:44 +010091 DFSOrders() : PassME("DFSOrders", kNoNodes) {
Jean Christophe Beyler2469e602014-05-06 20:36:55 -070092 }
93
Vladimir Marko312eb252014-10-07 15:01:57 +010094 bool Gate(const PassDataHolder* data) const {
95 DCHECK(data != nullptr);
96 CompilationUnit* c_unit = down_cast<const PassMEDataHolder*>(data)->c_unit;
97 DCHECK(c_unit != nullptr);
98 return !c_unit->mir_graph->DfsOrdersUpToDate();
99 }
100
Jean Christophe Beylere1f65bc2014-06-09 10:18:26 -0700101 void Start(PassDataHolder* data) const {
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700102 DCHECK(data != nullptr);
Jean Christophe Beylere1f65bc2014-06-09 10:18:26 -0700103 CompilationUnit* c_unit = down_cast<PassMEDataHolder*>(data)->c_unit;
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700104 DCHECK(c_unit != nullptr);
105 c_unit->mir_graph.get()->ComputeDFSOrders();
106 }
107};
108
109/**
110 * @class BuildDomination
111 * @brief Build the domination information of the MIR Graph
112 */
113class BuildDomination : public PassME {
114 public:
Vladimir Markoaa7b8a32014-10-15 11:35:44 +0100115 BuildDomination() : PassME("BuildDomination", kNoNodes) {
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700116 }
117
Jean Christophe Beylere1f65bc2014-06-09 10:18:26 -0700118 void Start(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 c_unit->mir_graph.get()->ComputeDominators();
123 c_unit->mir_graph.get()->CompilerInitializeSSAConversion();
124 }
125
Jean Christophe Beylere1f65bc2014-06-09 10:18:26 -0700126 void End(PassDataHolder* data) const {
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700127 DCHECK(data != nullptr);
Jean Christophe Beylere1f65bc2014-06-09 10:18:26 -0700128 CompilationUnit* c_unit = down_cast<PassMEDataHolder*>(data)->c_unit;
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700129 DCHECK(c_unit != nullptr);
130 // Verify the dataflow information after the pass.
131 if (c_unit->enable_debug & (1 << kDebugVerifyDataflow)) {
132 c_unit->mir_graph->VerifyDataflow();
133 }
134 }
135};
136
137/**
Vladimir Marko622bdbe2014-06-19 14:59:05 +0100138 * @class TopologicalSortOrders
139 * @brief Compute the topological sort order of the MIR graph
140 */
141class TopologicalSortOrders : public PassME {
142 public:
Vladimir Markoaa7b8a32014-10-15 11:35:44 +0100143 TopologicalSortOrders() : PassME("TopologicalSortOrders", kNoNodes) {
Vladimir Marko622bdbe2014-06-19 14:59:05 +0100144 }
145
146 void Start(PassDataHolder* data) const {
147 DCHECK(data != nullptr);
148 CompilationUnit* c_unit = down_cast<PassMEDataHolder*>(data)->c_unit;
149 DCHECK(c_unit != nullptr);
150 c_unit->mir_graph.get()->ComputeTopologicalSortOrder();
151 }
152};
153
154/**
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700155 * @class DefBlockMatrix
156 * @brief Calculate the matrix of definition per basic block
157 */
158class DefBlockMatrix : public PassME {
159 public:
Vladimir Markoaa7b8a32014-10-15 11:35:44 +0100160 DefBlockMatrix() : PassME("DefBlockMatrix", kNoNodes) {
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700161 }
162
Jean Christophe Beylere1f65bc2014-06-09 10:18:26 -0700163 void Start(PassDataHolder* data) const {
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700164 DCHECK(data != nullptr);
Jean Christophe Beylere1f65bc2014-06-09 10:18:26 -0700165 CompilationUnit* c_unit = down_cast<PassMEDataHolder*>(data)->c_unit;
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700166 DCHECK(c_unit != nullptr);
167 c_unit->mir_graph.get()->ComputeDefBlockMatrix();
168 }
169};
170
171/**
172 * @class CreatePhiNodes
173 * @brief Pass to create the phi nodes after SSA calculation
174 */
175class CreatePhiNodes : public PassME {
176 public:
Vladimir Markoaa7b8a32014-10-15 11:35:44 +0100177 CreatePhiNodes() : PassME("CreatePhiNodes", kNoNodes) {
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700178 }
179
Jean Christophe Beylere1f65bc2014-06-09 10:18:26 -0700180 void Start(PassDataHolder* data) const {
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700181 DCHECK(data != nullptr);
Jean Christophe Beylere1f65bc2014-06-09 10:18:26 -0700182 CompilationUnit* c_unit = down_cast<PassMEDataHolder*>(data)->c_unit;
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700183 DCHECK(c_unit != nullptr);
184 c_unit->mir_graph.get()->InsertPhiNodes();
185 }
186};
187
188/**
189 * @class ClearVisitedFlag
190 * @brief Pass to clear the visited flag for all basic blocks.
191 */
192
193class ClearVisitedFlag : public PassME {
194 public:
Vladimir Markoaa7b8a32014-10-15 11:35:44 +0100195 ClearVisitedFlag() : PassME("ClearVisitedFlag", kNoNodes) {
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700196 }
197
Jean Christophe Beylere1f65bc2014-06-09 10:18:26 -0700198 void Start(PassDataHolder* data) const {
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700199 DCHECK(data != nullptr);
Jean Christophe Beylere1f65bc2014-06-09 10:18:26 -0700200 CompilationUnit* c_unit = down_cast<PassMEDataHolder*>(data)->c_unit;
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700201 DCHECK(c_unit != nullptr);
202 c_unit->mir_graph.get()->ClearAllVisitedFlags();
203 }
204};
205
206/**
207 * @class SSAConversion
208 * @brief Pass for SSA conversion of MIRs
209 */
210class SSAConversion : public PassME {
211 public:
Vladimir Markoaa7b8a32014-10-15 11:35:44 +0100212 SSAConversion() : PassME("SSAConversion", kNoNodes) {
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700213 }
214
Jean Christophe Beylere1f65bc2014-06-09 10:18:26 -0700215 void Start(PassDataHolder* data) const {
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700216 DCHECK(data != nullptr);
Jean Christophe Beylere1f65bc2014-06-09 10:18:26 -0700217 CompilationUnit* c_unit = down_cast<PassMEDataHolder*>(data)->c_unit;
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700218 DCHECK(c_unit != nullptr);
219 MIRGraph *mir_graph = c_unit->mir_graph.get();
220 mir_graph->DoDFSPreOrderSSARename(mir_graph->GetEntryBlock());
221 }
222};
223
224/**
225 * @class PhiNodeOperands
226 * @brief Pass to insert the Phi node operands to basic blocks
227 */
228class PhiNodeOperands : public PassME {
229 public:
230 PhiNodeOperands() : PassME("PhiNodeOperands", kPreOrderDFSTraversal) {
231 }
232
Jean Christophe Beyler09321df2014-07-18 15:33:57 -0700233 bool Worker(PassDataHolder* data) const {
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700234 DCHECK(data != nullptr);
Jean Christophe Beyler09321df2014-07-18 15:33:57 -0700235 CompilationUnit* c_unit = down_cast<PassMEDataHolder*>(data)->c_unit;
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700236 DCHECK(c_unit != nullptr);
Jean Christophe Beyler09321df2014-07-18 15:33:57 -0700237 BasicBlock* bb = down_cast<PassMEDataHolder*>(data)->bb;
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700238 DCHECK(bb != nullptr);
239 c_unit->mir_graph->InsertPhiNodeOperands(bb);
240 // No need of repeating, so just return false.
241 return false;
242 }
243};
244
245/**
246 * @class InitRegLocations
247 * @brief Initialize Register Locations.
248 */
249class PerformInitRegLocations : public PassME {
250 public:
Vladimir Markoaa7b8a32014-10-15 11:35:44 +0100251 PerformInitRegLocations() : PassME("PerformInitRegLocation", kNoNodes) {
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700252 }
253
Jean Christophe Beylere1f65bc2014-06-09 10:18:26 -0700254 void Start(PassDataHolder* data) const {
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700255 DCHECK(data != nullptr);
Jean Christophe Beylere1f65bc2014-06-09 10:18:26 -0700256 CompilationUnit* c_unit = down_cast<PassMEDataHolder*>(data)->c_unit;
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700257 DCHECK(c_unit != nullptr);
258 c_unit->mir_graph->InitRegLocations();
259 }
260};
261
262/**
263 * @class ConstantPropagation
264 * @brief Perform a constant propagation pass.
265 */
266class ConstantPropagation : public PassME {
267 public:
268 ConstantPropagation() : PassME("ConstantPropagation") {
269 }
270
Jean Christophe Beyler09321df2014-07-18 15:33:57 -0700271 bool Worker(PassDataHolder* data) const {
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700272 DCHECK(data != nullptr);
Jean Christophe Beyler09321df2014-07-18 15:33:57 -0700273 CompilationUnit* c_unit = down_cast<PassMEDataHolder*>(data)->c_unit;
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700274 DCHECK(c_unit != nullptr);
Jean Christophe Beyler09321df2014-07-18 15:33:57 -0700275 BasicBlock* bb = down_cast<PassMEDataHolder*>(data)->bb;
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700276 DCHECK(bb != nullptr);
277 c_unit->mir_graph->DoConstantPropagation(bb);
278 // No need of repeating, so just return false.
279 return false;
280 }
281
Jean Christophe Beylere1f65bc2014-06-09 10:18:26 -0700282 void Start(PassDataHolder* data) const {
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700283 DCHECK(data != nullptr);
Jean Christophe Beylere1f65bc2014-06-09 10:18:26 -0700284 CompilationUnit* c_unit = down_cast<PassMEDataHolder*>(data)->c_unit;
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700285 DCHECK(c_unit != nullptr);
286 c_unit->mir_graph->InitializeConstantPropagation();
287 }
288};
289
290/**
291 * @class FreeData
292 * @brief There is some data that needs to be freed after performing the post optimization passes.
293 */
294class FreeData : public PassME {
295 public:
Vladimir Markoaa7b8a32014-10-15 11:35:44 +0100296 FreeData() : PassME("FreeData", kNoNodes) {
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700297 }
298
Jean Christophe Beylere1f65bc2014-06-09 10:18:26 -0700299 void End(PassDataHolder* data) const {
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700300 DCHECK(data != nullptr);
Jean Christophe Beylere1f65bc2014-06-09 10:18:26 -0700301 CompilationUnit* c_unit = down_cast<PassMEDataHolder*>(data)->c_unit;
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700302 DCHECK(c_unit != nullptr);
303 c_unit->mir_graph.get()->SSATransformationEnd();
304 }
305};
306
307} // namespace art
308
Narayan Kamathfd5a8522014-05-30 11:58:09 +0100309#endif // ART_COMPILER_DEX_POST_OPT_PASSES_H_