blob: 1ab862503bfa639e98a3b018069bd46062c93c6b [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 Gampe0b9203e2015-01-22 20:39:27 -080020#include "base/casts.h"
21#include "base/logging.h"
22#include "compiler_ir.h"
23#include "dex_flags.h"
24#include "mir_graph.h"
Jean Christophe Beyler2469e602014-05-06 20:36:55 -070025#include "pass_me.h"
26
27namespace art {
28
29/**
Vladimir Markoffda4992014-12-18 17:05:58 +000030 * @class PassMEMirSsaRep
31 * @brief Convenience class for passes that check MIRGraph::MirSsaRepUpToDate().
32 */
33class PassMEMirSsaRep : public PassME {
34 public:
35 PassMEMirSsaRep(const char* name, DataFlowAnalysisMode type = kAllNodes)
36 : PassME(name, type) {
37 }
38
39 bool Gate(const PassDataHolder* data) const OVERRIDE {
40 DCHECK(data != nullptr);
41 CompilationUnit* c_unit = down_cast<const PassMEDataHolder*>(data)->c_unit;
42 DCHECK(c_unit != nullptr);
43 return !c_unit->mir_graph->MirSsaRepUpToDate();
44 }
45};
46
47/**
48 * @class InitializeSSATransformation
Jean Christophe Beyler2469e602014-05-06 20:36:55 -070049 * @brief There is some data that needs to be initialized before performing
50 * the post optimization passes.
51 */
Vladimir Markoffda4992014-12-18 17:05:58 +000052class InitializeSSATransformation : public PassMEMirSsaRep {
Jean Christophe Beyler2469e602014-05-06 20:36:55 -070053 public:
Vladimir Markoffda4992014-12-18 17:05:58 +000054 InitializeSSATransformation() : PassMEMirSsaRep("InitializeSSATransformation", kNoNodes) {
Jean Christophe Beyler2469e602014-05-06 20:36:55 -070055 }
56
Jean Christophe Beylere1f65bc2014-06-09 10:18:26 -070057 void Start(PassDataHolder* data) const {
Jean Christophe Beyler2469e602014-05-06 20:36:55 -070058 // New blocks may have been inserted so the first thing we do is ensure that
59 // the c_unit's number of blocks matches the actual count of basic blocks.
60 DCHECK(data != nullptr);
Jean Christophe Beylere1f65bc2014-06-09 10:18:26 -070061 CompilationUnit* c_unit = down_cast<PassMEDataHolder*>(data)->c_unit;
Jean Christophe Beyler2469e602014-05-06 20:36:55 -070062 DCHECK(c_unit != nullptr);
Vladimir Markoffda4992014-12-18 17:05:58 +000063 c_unit->mir_graph->SSATransformationStart();
64 c_unit->mir_graph->CompilerInitializeSSAConversion();
Jean Christophe Beyler2469e602014-05-06 20:36:55 -070065 }
66};
67
68/**
Jean Christophe Beyler2469e602014-05-06 20:36:55 -070069 * @class ClearPhiInformation
70 * @brief Clear the PHI nodes from the CFG.
71 */
Vladimir Markoffda4992014-12-18 17:05:58 +000072class ClearPhiInstructions : public PassMEMirSsaRep {
Jean Christophe Beyler2469e602014-05-06 20:36:55 -070073 public:
Vladimir Markoffda4992014-12-18 17:05:58 +000074 ClearPhiInstructions() : PassMEMirSsaRep("ClearPhiInstructions") {
Jean Christophe Beyler2469e602014-05-06 20:36:55 -070075 }
76
Jean Christophe Beyler09321df2014-07-18 15:33:57 -070077 bool Worker(PassDataHolder* data) const;
Jean Christophe Beyler2469e602014-05-06 20:36:55 -070078};
79
80/**
81 * @class CalculatePredecessors
82 * @brief Calculate the predecessor BitVector of each Basicblock.
83 */
84class CalculatePredecessors : public PassME {
85 public:
Vladimir Markoaa7b8a32014-10-15 11:35:44 +010086 CalculatePredecessors() : PassME("CalculatePredecessors", kNoNodes) {
Jean Christophe Beyler2469e602014-05-06 20:36:55 -070087 }
88
Jean Christophe Beylere1f65bc2014-06-09 10:18:26 -070089 void Start(PassDataHolder* data) const;
Jean Christophe Beyler2469e602014-05-06 20:36:55 -070090};
91
92/**
93 * @class DFSOrders
94 * @brief Compute the DFS order of the MIR graph
95 */
96class DFSOrders : public PassME {
97 public:
Vladimir Markoaa7b8a32014-10-15 11:35:44 +010098 DFSOrders() : PassME("DFSOrders", kNoNodes) {
Jean Christophe Beyler2469e602014-05-06 20:36:55 -070099 }
100
Vladimir Marko312eb252014-10-07 15:01:57 +0100101 bool Gate(const PassDataHolder* data) const {
102 DCHECK(data != nullptr);
103 CompilationUnit* c_unit = down_cast<const PassMEDataHolder*>(data)->c_unit;
104 DCHECK(c_unit != nullptr);
105 return !c_unit->mir_graph->DfsOrdersUpToDate();
106 }
107
Jean Christophe Beylere1f65bc2014-06-09 10:18:26 -0700108 void Start(PassDataHolder* data) const {
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700109 DCHECK(data != nullptr);
Jean Christophe Beylere1f65bc2014-06-09 10:18:26 -0700110 CompilationUnit* c_unit = down_cast<PassMEDataHolder*>(data)->c_unit;
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700111 DCHECK(c_unit != nullptr);
112 c_unit->mir_graph.get()->ComputeDFSOrders();
113 }
114};
115
116/**
117 * @class BuildDomination
118 * @brief Build the domination information of the MIR Graph
119 */
120class BuildDomination : public PassME {
121 public:
Vladimir Markoaa7b8a32014-10-15 11:35:44 +0100122 BuildDomination() : PassME("BuildDomination", kNoNodes) {
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700123 }
124
Vladimir Markoffda4992014-12-18 17:05:58 +0000125 bool Gate(const PassDataHolder* data) const {
126 DCHECK(data != nullptr);
127 CompilationUnit* c_unit = down_cast<const PassMEDataHolder*>(data)->c_unit;
128 DCHECK(c_unit != nullptr);
129 return !c_unit->mir_graph->DominationUpToDate();
130 }
131
Jean Christophe Beylere1f65bc2014-06-09 10:18:26 -0700132 void Start(PassDataHolder* data) const {
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700133 DCHECK(data != nullptr);
Jean Christophe Beylere1f65bc2014-06-09 10:18:26 -0700134 CompilationUnit* c_unit = down_cast<PassMEDataHolder*>(data)->c_unit;
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700135 DCHECK(c_unit != nullptr);
Vladimir Markoffda4992014-12-18 17:05:58 +0000136 c_unit->mir_graph->ComputeDominators();
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700137 }
138
Jean Christophe Beylere1f65bc2014-06-09 10:18:26 -0700139 void End(PassDataHolder* data) const {
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700140 DCHECK(data != nullptr);
Jean Christophe Beylere1f65bc2014-06-09 10:18:26 -0700141 CompilationUnit* c_unit = down_cast<PassMEDataHolder*>(data)->c_unit;
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700142 DCHECK(c_unit != nullptr);
143 // Verify the dataflow information after the pass.
144 if (c_unit->enable_debug & (1 << kDebugVerifyDataflow)) {
145 c_unit->mir_graph->VerifyDataflow();
146 }
147 }
148};
149
150/**
Vladimir Marko622bdbe2014-06-19 14:59:05 +0100151 * @class TopologicalSortOrders
152 * @brief Compute the topological sort order of the MIR graph
153 */
154class TopologicalSortOrders : public PassME {
155 public:
Vladimir Markoaa7b8a32014-10-15 11:35:44 +0100156 TopologicalSortOrders() : PassME("TopologicalSortOrders", kNoNodes) {
Vladimir Marko622bdbe2014-06-19 14:59:05 +0100157 }
158
Vladimir Markoffda4992014-12-18 17:05:58 +0000159 bool Gate(const PassDataHolder* data) const {
160 DCHECK(data != nullptr);
161 CompilationUnit* c_unit = down_cast<const PassMEDataHolder*>(data)->c_unit;
162 DCHECK(c_unit != nullptr);
163 return !c_unit->mir_graph->TopologicalOrderUpToDate();
164 }
165
Vladimir Marko622bdbe2014-06-19 14:59:05 +0100166 void Start(PassDataHolder* data) const {
167 DCHECK(data != nullptr);
168 CompilationUnit* c_unit = down_cast<PassMEDataHolder*>(data)->c_unit;
169 DCHECK(c_unit != nullptr);
170 c_unit->mir_graph.get()->ComputeTopologicalSortOrder();
171 }
172};
173
174/**
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700175 * @class DefBlockMatrix
176 * @brief Calculate the matrix of definition per basic block
177 */
Vladimir Markoffda4992014-12-18 17:05:58 +0000178class DefBlockMatrix : public PassMEMirSsaRep {
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700179 public:
Vladimir Markoffda4992014-12-18 17:05:58 +0000180 DefBlockMatrix() : PassMEMirSsaRep("DefBlockMatrix", kNoNodes) {
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700181 }
182
Jean Christophe Beylere1f65bc2014-06-09 10:18:26 -0700183 void Start(PassDataHolder* data) const {
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700184 DCHECK(data != nullptr);
Jean Christophe Beylere1f65bc2014-06-09 10:18:26 -0700185 CompilationUnit* c_unit = down_cast<PassMEDataHolder*>(data)->c_unit;
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700186 DCHECK(c_unit != nullptr);
187 c_unit->mir_graph.get()->ComputeDefBlockMatrix();
188 }
189};
190
191/**
Vladimir Marko6a8946b2015-02-09 12:35:05 +0000192 * @class FindPhiNodeBlocksPass
193 * @brief Pass to find out where we need to insert the phi nodes for the SSA conversion.
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700194 */
Vladimir Marko6a8946b2015-02-09 12:35:05 +0000195class FindPhiNodeBlocksPass : public PassMEMirSsaRep {
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700196 public:
Vladimir Marko6a8946b2015-02-09 12:35:05 +0000197 FindPhiNodeBlocksPass() : PassMEMirSsaRep("FindPhiNodeBlocks", kNoNodes) {
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700198 }
199
Jean Christophe Beylere1f65bc2014-06-09 10:18:26 -0700200 void Start(PassDataHolder* data) const {
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700201 DCHECK(data != nullptr);
Jean Christophe Beylere1f65bc2014-06-09 10:18:26 -0700202 CompilationUnit* c_unit = down_cast<PassMEDataHolder*>(data)->c_unit;
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700203 DCHECK(c_unit != nullptr);
Vladimir Marko6a8946b2015-02-09 12:35:05 +0000204 c_unit->mir_graph.get()->FindPhiNodeBlocks();
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700205 }
206};
207
208/**
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700209 * @class SSAConversion
210 * @brief Pass for SSA conversion of MIRs
211 */
Vladimir Markoffda4992014-12-18 17:05:58 +0000212class SSAConversion : public PassMEMirSsaRep {
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700213 public:
Vladimir Markoffda4992014-12-18 17:05:58 +0000214 SSAConversion() : PassMEMirSsaRep("SSAConversion", kNoNodes) {
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700215 }
216
Jean Christophe Beylere1f65bc2014-06-09 10:18:26 -0700217 void Start(PassDataHolder* data) const {
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700218 DCHECK(data != nullptr);
Jean Christophe Beylere1f65bc2014-06-09 10:18:26 -0700219 CompilationUnit* c_unit = down_cast<PassMEDataHolder*>(data)->c_unit;
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700220 DCHECK(c_unit != nullptr);
221 MIRGraph *mir_graph = c_unit->mir_graph.get();
Vladimir Markoffda4992014-12-18 17:05:58 +0000222 mir_graph->ClearAllVisitedFlags();
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700223 mir_graph->DoDFSPreOrderSSARename(mir_graph->GetEntryBlock());
224 }
225};
226
227/**
228 * @class PhiNodeOperands
229 * @brief Pass to insert the Phi node operands to basic blocks
230 */
Vladimir Markoffda4992014-12-18 17:05:58 +0000231class PhiNodeOperands : public PassMEMirSsaRep {
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700232 public:
Vladimir Markoffda4992014-12-18 17:05:58 +0000233 PhiNodeOperands() : PassMEMirSsaRep("PhiNodeOperands", kPreOrderDFSTraversal) {
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700234 }
235
Jean Christophe Beyler09321df2014-07-18 15:33:57 -0700236 bool Worker(PassDataHolder* data) const {
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700237 DCHECK(data != nullptr);
Jean Christophe Beyler09321df2014-07-18 15:33:57 -0700238 CompilationUnit* c_unit = down_cast<PassMEDataHolder*>(data)->c_unit;
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700239 DCHECK(c_unit != nullptr);
Jean Christophe Beyler09321df2014-07-18 15:33:57 -0700240 BasicBlock* bb = down_cast<PassMEDataHolder*>(data)->bb;
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700241 DCHECK(bb != nullptr);
242 c_unit->mir_graph->InsertPhiNodeOperands(bb);
243 // No need of repeating, so just return false.
244 return false;
245 }
246};
247
248/**
249 * @class InitRegLocations
250 * @brief Initialize Register Locations.
251 */
Vladimir Markoffda4992014-12-18 17:05:58 +0000252class PerformInitRegLocations : public PassMEMirSsaRep {
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700253 public:
Vladimir Markoffda4992014-12-18 17:05:58 +0000254 PerformInitRegLocations() : PassMEMirSsaRep("PerformInitRegLocation", kNoNodes) {
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700255 }
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->InitRegLocations();
262 }
263};
264
265/**
Vladimir Marko066f9e42015-01-16 16:04:43 +0000266 * @class TypeInference
267 * @brief Type inference pass.
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700268 */
Vladimir Marko066f9e42015-01-16 16:04:43 +0000269class TypeInference : public PassMEMirSsaRep {
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700270 public:
Vladimir Marko066f9e42015-01-16 16:04:43 +0000271 TypeInference() : PassMEMirSsaRep("TypeInference", kRepeatingPreOrderDFSTraversal) {
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700272 }
273
Jean Christophe Beyler09321df2014-07-18 15:33:57 -0700274 bool Worker(PassDataHolder* data) const {
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700275 DCHECK(data != nullptr);
Vladimir Marko066f9e42015-01-16 16:04:43 +0000276 PassMEDataHolder* pass_me_data_holder = down_cast<PassMEDataHolder*>(data);
277 CompilationUnit* c_unit = pass_me_data_holder->c_unit;
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700278 DCHECK(c_unit != nullptr);
Vladimir Marko066f9e42015-01-16 16:04:43 +0000279 BasicBlock* bb = pass_me_data_holder->bb;
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700280 DCHECK(bb != nullptr);
Vladimir Marko066f9e42015-01-16 16:04:43 +0000281 return c_unit->mir_graph->InferTypes(bb);
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700282 }
283};
284
285/**
Vladimir Markoffda4992014-12-18 17:05:58 +0000286 * @class FinishSSATransformation
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700287 * @brief There is some data that needs to be freed after performing the post optimization passes.
288 */
Vladimir Markoffda4992014-12-18 17:05:58 +0000289class FinishSSATransformation : public PassMEMirSsaRep {
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700290 public:
Vladimir Markoffda4992014-12-18 17:05:58 +0000291 FinishSSATransformation() : PassMEMirSsaRep("FinishSSATransformation", kNoNodes) {
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700292 }
293
Jean Christophe Beylere1f65bc2014-06-09 10:18:26 -0700294 void End(PassDataHolder* data) const {
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700295 DCHECK(data != nullptr);
Jean Christophe Beylere1f65bc2014-06-09 10:18:26 -0700296 CompilationUnit* c_unit = down_cast<PassMEDataHolder*>(data)->c_unit;
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700297 DCHECK(c_unit != nullptr);
298 c_unit->mir_graph.get()->SSATransformationEnd();
299 }
300};
301
302} // namespace art
303
Narayan Kamathfd5a8522014-05-30 11:58:09 +0100304#endif // ART_COMPILER_DEX_POST_OPT_PASSES_H_