Jean Christophe Beyler | 2469e60 | 2014-05-06 20:36:55 -0700 | [diff] [blame] | 1 | /* |
| 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 Kamath | fd5a852 | 2014-05-30 11:58:09 +0100 | [diff] [blame] | 17 | #ifndef ART_COMPILER_DEX_POST_OPT_PASSES_H_ |
| 18 | #define ART_COMPILER_DEX_POST_OPT_PASSES_H_ |
Jean Christophe Beyler | 2469e60 | 2014-05-06 20:36:55 -0700 | [diff] [blame] | 19 | |
Andreas Gampe | 53c913b | 2014-08-12 23:19:23 -0700 | [diff] [blame] | 20 | #include "dex/quick/mir_to_lir.h" |
Jean Christophe Beyler | 2469e60 | 2014-05-06 20:36:55 -0700 | [diff] [blame] | 21 | #include "compiler_internals.h" |
| 22 | #include "pass_me.h" |
| 23 | |
| 24 | namespace 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 | */ |
| 31 | class InitializeData : public PassME { |
| 32 | public: |
Vladimir Marko | aa7b8a3 | 2014-10-15 11:35:44 +0100 | [diff] [blame] | 33 | InitializeData() : PassME("InitializeData", kNoNodes) { |
Jean Christophe Beyler | 2469e60 | 2014-05-06 20:36:55 -0700 | [diff] [blame] | 34 | } |
| 35 | |
Jean Christophe Beyler | e1f65bc | 2014-06-09 10:18:26 -0700 | [diff] [blame] | 36 | void Start(PassDataHolder* data) const { |
Jean Christophe Beyler | 2469e60 | 2014-05-06 20:36:55 -0700 | [diff] [blame] | 37 | // 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 Beyler | e1f65bc | 2014-06-09 10:18:26 -0700 | [diff] [blame] | 40 | CompilationUnit* c_unit = down_cast<PassMEDataHolder*>(data)->c_unit; |
Jean Christophe Beyler | 2469e60 | 2014-05-06 20:36:55 -0700 | [diff] [blame] | 41 | 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 | */ |
| 51 | class MethodUseCount : public PassME { |
| 52 | public: |
| 53 | MethodUseCount() : PassME("UseCount") { |
| 54 | } |
| 55 | |
Jean Christophe Beyler | 09321df | 2014-07-18 15:33:57 -0700 | [diff] [blame] | 56 | bool Worker(PassDataHolder* data) const; |
Jean Christophe Beyler | 2469e60 | 2014-05-06 20:36:55 -0700 | [diff] [blame] | 57 | |
| 58 | bool Gate(const PassDataHolder* data) const; |
| 59 | }; |
| 60 | |
| 61 | /** |
| 62 | * @class ClearPhiInformation |
| 63 | * @brief Clear the PHI nodes from the CFG. |
| 64 | */ |
| 65 | class ClearPhiInstructions : public PassME { |
| 66 | public: |
| 67 | ClearPhiInstructions() : PassME("ClearPhiInstructions") { |
| 68 | } |
| 69 | |
Jean Christophe Beyler | 09321df | 2014-07-18 15:33:57 -0700 | [diff] [blame] | 70 | bool Worker(PassDataHolder* data) const; |
Jean Christophe Beyler | 2469e60 | 2014-05-06 20:36:55 -0700 | [diff] [blame] | 71 | }; |
| 72 | |
| 73 | /** |
| 74 | * @class CalculatePredecessors |
| 75 | * @brief Calculate the predecessor BitVector of each Basicblock. |
| 76 | */ |
| 77 | class CalculatePredecessors : public PassME { |
| 78 | public: |
Vladimir Marko | aa7b8a3 | 2014-10-15 11:35:44 +0100 | [diff] [blame] | 79 | CalculatePredecessors() : PassME("CalculatePredecessors", kNoNodes) { |
Jean Christophe Beyler | 2469e60 | 2014-05-06 20:36:55 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Jean Christophe Beyler | e1f65bc | 2014-06-09 10:18:26 -0700 | [diff] [blame] | 82 | void Start(PassDataHolder* data) const; |
Jean Christophe Beyler | 2469e60 | 2014-05-06 20:36:55 -0700 | [diff] [blame] | 83 | }; |
| 84 | |
| 85 | /** |
| 86 | * @class DFSOrders |
| 87 | * @brief Compute the DFS order of the MIR graph |
| 88 | */ |
| 89 | class DFSOrders : public PassME { |
| 90 | public: |
Vladimir Marko | aa7b8a3 | 2014-10-15 11:35:44 +0100 | [diff] [blame] | 91 | DFSOrders() : PassME("DFSOrders", kNoNodes) { |
Jean Christophe Beyler | 2469e60 | 2014-05-06 20:36:55 -0700 | [diff] [blame] | 92 | } |
| 93 | |
Vladimir Marko | 312eb25 | 2014-10-07 15:01:57 +0100 | [diff] [blame] | 94 | 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 Beyler | e1f65bc | 2014-06-09 10:18:26 -0700 | [diff] [blame] | 101 | void Start(PassDataHolder* data) const { |
Jean Christophe Beyler | 2469e60 | 2014-05-06 20:36:55 -0700 | [diff] [blame] | 102 | DCHECK(data != nullptr); |
Jean Christophe Beyler | e1f65bc | 2014-06-09 10:18:26 -0700 | [diff] [blame] | 103 | CompilationUnit* c_unit = down_cast<PassMEDataHolder*>(data)->c_unit; |
Jean Christophe Beyler | 2469e60 | 2014-05-06 20:36:55 -0700 | [diff] [blame] | 104 | 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 | */ |
| 113 | class BuildDomination : public PassME { |
| 114 | public: |
Vladimir Marko | aa7b8a3 | 2014-10-15 11:35:44 +0100 | [diff] [blame] | 115 | BuildDomination() : PassME("BuildDomination", kNoNodes) { |
Jean Christophe Beyler | 2469e60 | 2014-05-06 20:36:55 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Jean Christophe Beyler | e1f65bc | 2014-06-09 10:18:26 -0700 | [diff] [blame] | 118 | void Start(PassDataHolder* data) const { |
Jean Christophe Beyler | 2469e60 | 2014-05-06 20:36:55 -0700 | [diff] [blame] | 119 | DCHECK(data != nullptr); |
Jean Christophe Beyler | e1f65bc | 2014-06-09 10:18:26 -0700 | [diff] [blame] | 120 | CompilationUnit* c_unit = down_cast<PassMEDataHolder*>(data)->c_unit; |
Jean Christophe Beyler | 2469e60 | 2014-05-06 20:36:55 -0700 | [diff] [blame] | 121 | DCHECK(c_unit != nullptr); |
| 122 | c_unit->mir_graph.get()->ComputeDominators(); |
| 123 | c_unit->mir_graph.get()->CompilerInitializeSSAConversion(); |
| 124 | } |
| 125 | |
Jean Christophe Beyler | e1f65bc | 2014-06-09 10:18:26 -0700 | [diff] [blame] | 126 | void End(PassDataHolder* data) const { |
Jean Christophe Beyler | 2469e60 | 2014-05-06 20:36:55 -0700 | [diff] [blame] | 127 | DCHECK(data != nullptr); |
Jean Christophe Beyler | e1f65bc | 2014-06-09 10:18:26 -0700 | [diff] [blame] | 128 | CompilationUnit* c_unit = down_cast<PassMEDataHolder*>(data)->c_unit; |
Jean Christophe Beyler | 2469e60 | 2014-05-06 20:36:55 -0700 | [diff] [blame] | 129 | 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 Marko | 622bdbe | 2014-06-19 14:59:05 +0100 | [diff] [blame] | 138 | * @class TopologicalSortOrders |
| 139 | * @brief Compute the topological sort order of the MIR graph |
| 140 | */ |
| 141 | class TopologicalSortOrders : public PassME { |
| 142 | public: |
Vladimir Marko | aa7b8a3 | 2014-10-15 11:35:44 +0100 | [diff] [blame] | 143 | TopologicalSortOrders() : PassME("TopologicalSortOrders", kNoNodes) { |
Vladimir Marko | 622bdbe | 2014-06-19 14:59:05 +0100 | [diff] [blame] | 144 | } |
| 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 Beyler | 2469e60 | 2014-05-06 20:36:55 -0700 | [diff] [blame] | 155 | * @class DefBlockMatrix |
| 156 | * @brief Calculate the matrix of definition per basic block |
| 157 | */ |
| 158 | class DefBlockMatrix : public PassME { |
| 159 | public: |
Vladimir Marko | aa7b8a3 | 2014-10-15 11:35:44 +0100 | [diff] [blame] | 160 | DefBlockMatrix() : PassME("DefBlockMatrix", kNoNodes) { |
Jean Christophe Beyler | 2469e60 | 2014-05-06 20:36:55 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Jean Christophe Beyler | e1f65bc | 2014-06-09 10:18:26 -0700 | [diff] [blame] | 163 | void Start(PassDataHolder* data) const { |
Jean Christophe Beyler | 2469e60 | 2014-05-06 20:36:55 -0700 | [diff] [blame] | 164 | DCHECK(data != nullptr); |
Jean Christophe Beyler | e1f65bc | 2014-06-09 10:18:26 -0700 | [diff] [blame] | 165 | CompilationUnit* c_unit = down_cast<PassMEDataHolder*>(data)->c_unit; |
Jean Christophe Beyler | 2469e60 | 2014-05-06 20:36:55 -0700 | [diff] [blame] | 166 | 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 | */ |
| 175 | class CreatePhiNodes : public PassME { |
| 176 | public: |
Vladimir Marko | aa7b8a3 | 2014-10-15 11:35:44 +0100 | [diff] [blame] | 177 | CreatePhiNodes() : PassME("CreatePhiNodes", kNoNodes) { |
Jean Christophe Beyler | 2469e60 | 2014-05-06 20:36:55 -0700 | [diff] [blame] | 178 | } |
| 179 | |
Jean Christophe Beyler | e1f65bc | 2014-06-09 10:18:26 -0700 | [diff] [blame] | 180 | void Start(PassDataHolder* data) const { |
Jean Christophe Beyler | 2469e60 | 2014-05-06 20:36:55 -0700 | [diff] [blame] | 181 | DCHECK(data != nullptr); |
Jean Christophe Beyler | e1f65bc | 2014-06-09 10:18:26 -0700 | [diff] [blame] | 182 | CompilationUnit* c_unit = down_cast<PassMEDataHolder*>(data)->c_unit; |
Jean Christophe Beyler | 2469e60 | 2014-05-06 20:36:55 -0700 | [diff] [blame] | 183 | 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 | |
| 193 | class ClearVisitedFlag : public PassME { |
| 194 | public: |
Vladimir Marko | aa7b8a3 | 2014-10-15 11:35:44 +0100 | [diff] [blame] | 195 | ClearVisitedFlag() : PassME("ClearVisitedFlag", kNoNodes) { |
Jean Christophe Beyler | 2469e60 | 2014-05-06 20:36:55 -0700 | [diff] [blame] | 196 | } |
| 197 | |
Jean Christophe Beyler | e1f65bc | 2014-06-09 10:18:26 -0700 | [diff] [blame] | 198 | void Start(PassDataHolder* data) const { |
Jean Christophe Beyler | 2469e60 | 2014-05-06 20:36:55 -0700 | [diff] [blame] | 199 | DCHECK(data != nullptr); |
Jean Christophe Beyler | e1f65bc | 2014-06-09 10:18:26 -0700 | [diff] [blame] | 200 | CompilationUnit* c_unit = down_cast<PassMEDataHolder*>(data)->c_unit; |
Jean Christophe Beyler | 2469e60 | 2014-05-06 20:36:55 -0700 | [diff] [blame] | 201 | 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 | */ |
| 210 | class SSAConversion : public PassME { |
| 211 | public: |
Vladimir Marko | aa7b8a3 | 2014-10-15 11:35:44 +0100 | [diff] [blame] | 212 | SSAConversion() : PassME("SSAConversion", kNoNodes) { |
Jean Christophe Beyler | 2469e60 | 2014-05-06 20:36:55 -0700 | [diff] [blame] | 213 | } |
| 214 | |
Jean Christophe Beyler | e1f65bc | 2014-06-09 10:18:26 -0700 | [diff] [blame] | 215 | void Start(PassDataHolder* data) const { |
Jean Christophe Beyler | 2469e60 | 2014-05-06 20:36:55 -0700 | [diff] [blame] | 216 | DCHECK(data != nullptr); |
Jean Christophe Beyler | e1f65bc | 2014-06-09 10:18:26 -0700 | [diff] [blame] | 217 | CompilationUnit* c_unit = down_cast<PassMEDataHolder*>(data)->c_unit; |
Jean Christophe Beyler | 2469e60 | 2014-05-06 20:36:55 -0700 | [diff] [blame] | 218 | 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 | */ |
| 228 | class PhiNodeOperands : public PassME { |
| 229 | public: |
| 230 | PhiNodeOperands() : PassME("PhiNodeOperands", kPreOrderDFSTraversal) { |
| 231 | } |
| 232 | |
Jean Christophe Beyler | 09321df | 2014-07-18 15:33:57 -0700 | [diff] [blame] | 233 | bool Worker(PassDataHolder* data) const { |
Jean Christophe Beyler | 2469e60 | 2014-05-06 20:36:55 -0700 | [diff] [blame] | 234 | DCHECK(data != nullptr); |
Jean Christophe Beyler | 09321df | 2014-07-18 15:33:57 -0700 | [diff] [blame] | 235 | CompilationUnit* c_unit = down_cast<PassMEDataHolder*>(data)->c_unit; |
Jean Christophe Beyler | 2469e60 | 2014-05-06 20:36:55 -0700 | [diff] [blame] | 236 | DCHECK(c_unit != nullptr); |
Jean Christophe Beyler | 09321df | 2014-07-18 15:33:57 -0700 | [diff] [blame] | 237 | BasicBlock* bb = down_cast<PassMEDataHolder*>(data)->bb; |
Jean Christophe Beyler | 2469e60 | 2014-05-06 20:36:55 -0700 | [diff] [blame] | 238 | 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 | */ |
| 249 | class PerformInitRegLocations : public PassME { |
| 250 | public: |
Vladimir Marko | aa7b8a3 | 2014-10-15 11:35:44 +0100 | [diff] [blame] | 251 | PerformInitRegLocations() : PassME("PerformInitRegLocation", kNoNodes) { |
Jean Christophe Beyler | 2469e60 | 2014-05-06 20:36:55 -0700 | [diff] [blame] | 252 | } |
| 253 | |
Jean Christophe Beyler | e1f65bc | 2014-06-09 10:18:26 -0700 | [diff] [blame] | 254 | void Start(PassDataHolder* data) const { |
Jean Christophe Beyler | 2469e60 | 2014-05-06 20:36:55 -0700 | [diff] [blame] | 255 | DCHECK(data != nullptr); |
Jean Christophe Beyler | e1f65bc | 2014-06-09 10:18:26 -0700 | [diff] [blame] | 256 | CompilationUnit* c_unit = down_cast<PassMEDataHolder*>(data)->c_unit; |
Jean Christophe Beyler | 2469e60 | 2014-05-06 20:36:55 -0700 | [diff] [blame] | 257 | 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 | */ |
| 266 | class ConstantPropagation : public PassME { |
| 267 | public: |
| 268 | ConstantPropagation() : PassME("ConstantPropagation") { |
| 269 | } |
| 270 | |
Jean Christophe Beyler | 09321df | 2014-07-18 15:33:57 -0700 | [diff] [blame] | 271 | bool Worker(PassDataHolder* data) const { |
Jean Christophe Beyler | 2469e60 | 2014-05-06 20:36:55 -0700 | [diff] [blame] | 272 | DCHECK(data != nullptr); |
Jean Christophe Beyler | 09321df | 2014-07-18 15:33:57 -0700 | [diff] [blame] | 273 | CompilationUnit* c_unit = down_cast<PassMEDataHolder*>(data)->c_unit; |
Jean Christophe Beyler | 2469e60 | 2014-05-06 20:36:55 -0700 | [diff] [blame] | 274 | DCHECK(c_unit != nullptr); |
Jean Christophe Beyler | 09321df | 2014-07-18 15:33:57 -0700 | [diff] [blame] | 275 | BasicBlock* bb = down_cast<PassMEDataHolder*>(data)->bb; |
Jean Christophe Beyler | 2469e60 | 2014-05-06 20:36:55 -0700 | [diff] [blame] | 276 | 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 Beyler | e1f65bc | 2014-06-09 10:18:26 -0700 | [diff] [blame] | 282 | void Start(PassDataHolder* data) const { |
Jean Christophe Beyler | 2469e60 | 2014-05-06 20:36:55 -0700 | [diff] [blame] | 283 | DCHECK(data != nullptr); |
Jean Christophe Beyler | e1f65bc | 2014-06-09 10:18:26 -0700 | [diff] [blame] | 284 | CompilationUnit* c_unit = down_cast<PassMEDataHolder*>(data)->c_unit; |
Jean Christophe Beyler | 2469e60 | 2014-05-06 20:36:55 -0700 | [diff] [blame] | 285 | 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 | */ |
| 294 | class FreeData : public PassME { |
| 295 | public: |
Vladimir Marko | aa7b8a3 | 2014-10-15 11:35:44 +0100 | [diff] [blame] | 296 | FreeData() : PassME("FreeData", kNoNodes) { |
Jean Christophe Beyler | 2469e60 | 2014-05-06 20:36:55 -0700 | [diff] [blame] | 297 | } |
| 298 | |
Jean Christophe Beyler | e1f65bc | 2014-06-09 10:18:26 -0700 | [diff] [blame] | 299 | void End(PassDataHolder* data) const { |
Jean Christophe Beyler | 2469e60 | 2014-05-06 20:36:55 -0700 | [diff] [blame] | 300 | DCHECK(data != nullptr); |
Jean Christophe Beyler | e1f65bc | 2014-06-09 10:18:26 -0700 | [diff] [blame] | 301 | CompilationUnit* c_unit = down_cast<PassMEDataHolder*>(data)->c_unit; |
Jean Christophe Beyler | 2469e60 | 2014-05-06 20:36:55 -0700 | [diff] [blame] | 302 | DCHECK(c_unit != nullptr); |
| 303 | c_unit->mir_graph.get()->SSATransformationEnd(); |
| 304 | } |
| 305 | }; |
| 306 | |
| 307 | } // namespace art |
| 308 | |
Narayan Kamath | fd5a852 | 2014-05-30 11:58:09 +0100 | [diff] [blame] | 309 | #endif // ART_COMPILER_DEX_POST_OPT_PASSES_H_ |