Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [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 | |
| 17 | #ifndef ART_COMPILER_DEX_BB_OPTIMIZATIONS_H_ |
| 18 | #define ART_COMPILER_DEX_BB_OPTIMIZATIONS_H_ |
| 19 | |
Andreas Gampe | 53c913b | 2014-08-12 23:19:23 -0700 | [diff] [blame] | 20 | #include "base/casts.h" |
Andreas Gampe | 0b9203e | 2015-01-22 20:39:27 -0800 | [diff] [blame] | 21 | #include "compiler_ir.h" |
| 22 | #include "dex_flags.h" |
James C Scott | 4f59668 | 2014-05-01 05:52:04 -0700 | [diff] [blame] | 23 | #include "pass_me.h" |
Andreas Gampe | 0b9203e | 2015-01-22 20:39:27 -0800 | [diff] [blame] | 24 | #include "mir_graph.h" |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 25 | |
| 26 | namespace art { |
| 27 | |
| 28 | /** |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 29 | * @class String Change |
| 30 | * @brief Converts calls to String.<init> to StringFactory instead. |
| 31 | */ |
| 32 | class StringChange : public PassME { |
| 33 | public: |
| 34 | StringChange() : PassME("StringChange", kNoNodes) { |
| 35 | } |
| 36 | |
| 37 | void Start(PassDataHolder* data) const { |
| 38 | DCHECK(data != nullptr); |
| 39 | CompilationUnit* c_unit = down_cast<PassMEDataHolder*>(data)->c_unit; |
| 40 | DCHECK(c_unit != nullptr); |
| 41 | c_unit->mir_graph->StringChange(); |
| 42 | } |
| 43 | |
| 44 | bool Gate(const PassDataHolder* data) const { |
| 45 | DCHECK(data != nullptr); |
| 46 | CompilationUnit* c_unit = down_cast<const PassMEDataHolder*>(data)->c_unit; |
| 47 | DCHECK(c_unit != nullptr); |
| 48 | return c_unit->mir_graph->HasInvokes(); |
| 49 | } |
| 50 | }; |
| 51 | |
| 52 | /** |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 53 | * @class CacheFieldLoweringInfo |
| 54 | * @brief Cache the lowering info for fields used by IGET/IPUT/SGET/SPUT insns. |
| 55 | */ |
James C Scott | 4f59668 | 2014-05-01 05:52:04 -0700 | [diff] [blame] | 56 | class CacheFieldLoweringInfo : public PassME { |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 57 | public: |
James C Scott | 4f59668 | 2014-05-01 05:52:04 -0700 | [diff] [blame] | 58 | CacheFieldLoweringInfo() : PassME("CacheFieldLoweringInfo", kNoNodes) { |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 59 | } |
| 60 | |
Jean Christophe Beyler | e1f65bc | 2014-06-09 10:18:26 -0700 | [diff] [blame] | 61 | void Start(PassDataHolder* data) const { |
James C Scott | 4f59668 | 2014-05-01 05:52:04 -0700 | [diff] [blame] | 62 | DCHECK(data != nullptr); |
Jean Christophe Beyler | 75bcc37 | 2014-09-04 08:15:11 -0700 | [diff] [blame] | 63 | CompilationUnit* c_unit = down_cast<PassMEDataHolder*>(data)->c_unit; |
| 64 | DCHECK(c_unit != nullptr); |
| 65 | c_unit->mir_graph->DoCacheFieldLoweringInfo(); |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 66 | } |
Vladimir Marko | 3d73ba2 | 2014-03-06 15:18:04 +0000 | [diff] [blame] | 67 | |
James C Scott | 4f59668 | 2014-05-01 05:52:04 -0700 | [diff] [blame] | 68 | bool Gate(const PassDataHolder* data) const { |
| 69 | DCHECK(data != nullptr); |
Jean Christophe Beyler | 75bcc37 | 2014-09-04 08:15:11 -0700 | [diff] [blame] | 70 | CompilationUnit* c_unit = down_cast<const PassMEDataHolder*>(data)->c_unit; |
| 71 | DCHECK(c_unit != nullptr); |
| 72 | return c_unit->mir_graph->HasFieldAccess(); |
Vladimir Marko | 3d73ba2 | 2014-03-06 15:18:04 +0000 | [diff] [blame] | 73 | } |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 74 | }; |
| 75 | |
| 76 | /** |
Vladimir Marko | f096aad | 2014-01-23 15:51:58 +0000 | [diff] [blame] | 77 | * @class CacheMethodLoweringInfo |
| 78 | * @brief Cache the lowering info for methods called by INVOKEs. |
| 79 | */ |
James C Scott | 4f59668 | 2014-05-01 05:52:04 -0700 | [diff] [blame] | 80 | class CacheMethodLoweringInfo : public PassME { |
Vladimir Marko | f096aad | 2014-01-23 15:51:58 +0000 | [diff] [blame] | 81 | public: |
James C Scott | 4f59668 | 2014-05-01 05:52:04 -0700 | [diff] [blame] | 82 | CacheMethodLoweringInfo() : PassME("CacheMethodLoweringInfo", kNoNodes) { |
Vladimir Marko | f096aad | 2014-01-23 15:51:58 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Jean Christophe Beyler | e1f65bc | 2014-06-09 10:18:26 -0700 | [diff] [blame] | 85 | void Start(PassDataHolder* data) const { |
James C Scott | 4f59668 | 2014-05-01 05:52:04 -0700 | [diff] [blame] | 86 | DCHECK(data != nullptr); |
Jean Christophe Beyler | 75bcc37 | 2014-09-04 08:15:11 -0700 | [diff] [blame] | 87 | CompilationUnit* c_unit = down_cast<PassMEDataHolder*>(data)->c_unit; |
| 88 | DCHECK(c_unit != nullptr); |
| 89 | c_unit->mir_graph->DoCacheMethodLoweringInfo(); |
Vladimir Marko | f096aad | 2014-01-23 15:51:58 +0000 | [diff] [blame] | 90 | } |
Vladimir Marko | 3d73ba2 | 2014-03-06 15:18:04 +0000 | [diff] [blame] | 91 | |
James C Scott | 4f59668 | 2014-05-01 05:52:04 -0700 | [diff] [blame] | 92 | bool Gate(const PassDataHolder* data) const { |
| 93 | DCHECK(data != nullptr); |
Jean Christophe Beyler | 75bcc37 | 2014-09-04 08:15:11 -0700 | [diff] [blame] | 94 | CompilationUnit* c_unit = down_cast<const PassMEDataHolder*>(data)->c_unit; |
| 95 | DCHECK(c_unit != nullptr); |
| 96 | return c_unit->mir_graph->HasInvokes(); |
Vladimir Marko | 3d73ba2 | 2014-03-06 15:18:04 +0000 | [diff] [blame] | 97 | } |
Vladimir Marko | f096aad | 2014-01-23 15:51:58 +0000 | [diff] [blame] | 98 | }; |
| 99 | |
| 100 | /** |
Razvan A Lupusoru | cb80474 | 2014-07-09 16:42:19 -0700 | [diff] [blame] | 101 | * @class SpecialMethodInliner |
| 102 | * @brief Performs method inlining pass on special kinds of methods. |
| 103 | * @details Special methods are methods that fall in one of the following categories: |
| 104 | * empty, instance getter, instance setter, argument return, and constant return. |
Vladimir Marko | 9820b7c | 2014-01-02 16:40:37 +0000 | [diff] [blame] | 105 | */ |
Razvan A Lupusoru | cb80474 | 2014-07-09 16:42:19 -0700 | [diff] [blame] | 106 | class SpecialMethodInliner : public PassME { |
Vladimir Marko | 9820b7c | 2014-01-02 16:40:37 +0000 | [diff] [blame] | 107 | public: |
Razvan A Lupusoru | cb80474 | 2014-07-09 16:42:19 -0700 | [diff] [blame] | 108 | SpecialMethodInliner() : PassME("SpecialMethodInliner") { |
Vladimir Marko | 9820b7c | 2014-01-02 16:40:37 +0000 | [diff] [blame] | 109 | } |
| 110 | |
James C Scott | 4f59668 | 2014-05-01 05:52:04 -0700 | [diff] [blame] | 111 | bool Gate(const PassDataHolder* data) const { |
| 112 | DCHECK(data != nullptr); |
Jean Christophe Beyler | 75bcc37 | 2014-09-04 08:15:11 -0700 | [diff] [blame] | 113 | CompilationUnit* c_unit = down_cast<const PassMEDataHolder*>(data)->c_unit; |
| 114 | DCHECK(c_unit != nullptr); |
| 115 | return c_unit->mir_graph->InlineSpecialMethodsGate(); |
Vladimir Marko | 9820b7c | 2014-01-02 16:40:37 +0000 | [diff] [blame] | 116 | } |
| 117 | |
Jean Christophe Beyler | e1f65bc | 2014-06-09 10:18:26 -0700 | [diff] [blame] | 118 | void Start(PassDataHolder* data) const { |
James C Scott | 4f59668 | 2014-05-01 05:52:04 -0700 | [diff] [blame] | 119 | DCHECK(data != nullptr); |
Jean Christophe Beyler | 75bcc37 | 2014-09-04 08:15:11 -0700 | [diff] [blame] | 120 | CompilationUnit* c_unit = down_cast<PassMEDataHolder*>(data)->c_unit; |
| 121 | DCHECK(c_unit != nullptr); |
| 122 | c_unit->mir_graph->InlineSpecialMethodsStart(); |
Vladimir Marko | 9820b7c | 2014-01-02 16:40:37 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Jean Christophe Beyler | 09321df | 2014-07-18 15:33:57 -0700 | [diff] [blame] | 125 | bool Worker(PassDataHolder* data) const { |
James C Scott | 4f59668 | 2014-05-01 05:52:04 -0700 | [diff] [blame] | 126 | DCHECK(data != nullptr); |
Jean Christophe Beyler | 09321df | 2014-07-18 15:33:57 -0700 | [diff] [blame] | 127 | PassMEDataHolder* pass_me_data_holder = down_cast<PassMEDataHolder*>(data); |
Jean Christophe Beyler | 75bcc37 | 2014-09-04 08:15:11 -0700 | [diff] [blame] | 128 | CompilationUnit* c_unit = pass_me_data_holder->c_unit; |
| 129 | DCHECK(c_unit != nullptr); |
James C Scott | 4f59668 | 2014-05-01 05:52:04 -0700 | [diff] [blame] | 130 | BasicBlock* bb = pass_me_data_holder->bb; |
| 131 | DCHECK(bb != nullptr); |
Jean Christophe Beyler | 75bcc37 | 2014-09-04 08:15:11 -0700 | [diff] [blame] | 132 | c_unit->mir_graph->InlineSpecialMethods(bb); |
Vladimir Marko | 9820b7c | 2014-01-02 16:40:37 +0000 | [diff] [blame] | 133 | // No need of repeating, so just return false. |
| 134 | return false; |
| 135 | } |
| 136 | |
Jean Christophe Beyler | e1f65bc | 2014-06-09 10:18:26 -0700 | [diff] [blame] | 137 | void End(PassDataHolder* data) const { |
James C Scott | 4f59668 | 2014-05-01 05:52:04 -0700 | [diff] [blame] | 138 | DCHECK(data != nullptr); |
Jean Christophe Beyler | 75bcc37 | 2014-09-04 08:15:11 -0700 | [diff] [blame] | 139 | CompilationUnit* c_unit = down_cast<PassMEDataHolder*>(data)->c_unit; |
| 140 | DCHECK(c_unit != nullptr); |
| 141 | c_unit->mir_graph->InlineSpecialMethodsEnd(); |
Vladimir Marko | 9820b7c | 2014-01-02 16:40:37 +0000 | [diff] [blame] | 142 | } |
| 143 | }; |
| 144 | |
| 145 | /** |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 146 | * @class CodeLayout |
| 147 | * @brief Perform the code layout pass. |
| 148 | */ |
James C Scott | 4f59668 | 2014-05-01 05:52:04 -0700 | [diff] [blame] | 149 | class CodeLayout : public PassME { |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 150 | public: |
Jean Christophe Beyler | 2469e60 | 2014-05-06 20:36:55 -0700 | [diff] [blame] | 151 | CodeLayout() : PassME("CodeLayout", kAllNodes, kOptimizationBasicBlockChange, "2_post_layout_cfg") { |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 152 | } |
| 153 | |
Jean Christophe Beyler | e1f65bc | 2014-06-09 10:18:26 -0700 | [diff] [blame] | 154 | void Start(PassDataHolder* data) const { |
James C Scott | 4f59668 | 2014-05-01 05:52:04 -0700 | [diff] [blame] | 155 | DCHECK(data != nullptr); |
Jean Christophe Beyler | 75bcc37 | 2014-09-04 08:15:11 -0700 | [diff] [blame] | 156 | CompilationUnit* c_unit = down_cast<PassMEDataHolder*>(data)->c_unit; |
| 157 | DCHECK(c_unit != nullptr); |
| 158 | c_unit->mir_graph->VerifyDataflow(); |
| 159 | c_unit->mir_graph->ClearAllVisitedFlags(); |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 160 | } |
| 161 | |
Jean Christophe Beyler | 09321df | 2014-07-18 15:33:57 -0700 | [diff] [blame] | 162 | bool Worker(PassDataHolder* data) const; |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 163 | }; |
| 164 | |
| 165 | /** |
Vladimir Marko | 67c72b8 | 2014-10-09 12:26:10 +0100 | [diff] [blame] | 166 | * @class NullCheckElimination |
| 167 | * @brief Null check elimination pass. |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 168 | */ |
Vladimir Marko | 67c72b8 | 2014-10-09 12:26:10 +0100 | [diff] [blame] | 169 | class NullCheckElimination : public PassME { |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 170 | public: |
Vladimir Marko | 67c72b8 | 2014-10-09 12:26:10 +0100 | [diff] [blame] | 171 | NullCheckElimination() |
Vladimir Marko | 7baa6f8 | 2014-10-09 18:01:24 +0100 | [diff] [blame] | 172 | : PassME("NCE", kRepeatingPreOrderDFSTraversal, "3_post_nce_cfg") { |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 173 | } |
| 174 | |
Vladimir Marko | 67c72b8 | 2014-10-09 12:26:10 +0100 | [diff] [blame] | 175 | bool Gate(const PassDataHolder* data) const { |
James C Scott | 4f59668 | 2014-05-01 05:52:04 -0700 | [diff] [blame] | 176 | DCHECK(data != nullptr); |
Vladimir Marko | 67c72b8 | 2014-10-09 12:26:10 +0100 | [diff] [blame] | 177 | CompilationUnit* c_unit = down_cast<const PassMEDataHolder*>(data)->c_unit; |
Jean Christophe Beyler | 75bcc37 | 2014-09-04 08:15:11 -0700 | [diff] [blame] | 178 | DCHECK(c_unit != nullptr); |
Vladimir Marko | 67c72b8 | 2014-10-09 12:26:10 +0100 | [diff] [blame] | 179 | return c_unit->mir_graph->EliminateNullChecksGate(); |
Vladimir Marko | bfea9c2 | 2014-01-17 17:49:33 +0000 | [diff] [blame] | 180 | } |
| 181 | |
Jean Christophe Beyler | 09321df | 2014-07-18 15:33:57 -0700 | [diff] [blame] | 182 | bool Worker(PassDataHolder* data) const { |
James C Scott | 4f59668 | 2014-05-01 05:52:04 -0700 | [diff] [blame] | 183 | DCHECK(data != nullptr); |
Jean Christophe Beyler | 09321df | 2014-07-18 15:33:57 -0700 | [diff] [blame] | 184 | PassMEDataHolder* pass_me_data_holder = down_cast<PassMEDataHolder*>(data); |
Jean Christophe Beyler | 75bcc37 | 2014-09-04 08:15:11 -0700 | [diff] [blame] | 185 | CompilationUnit* c_unit = pass_me_data_holder->c_unit; |
| 186 | DCHECK(c_unit != nullptr); |
James C Scott | 4f59668 | 2014-05-01 05:52:04 -0700 | [diff] [blame] | 187 | BasicBlock* bb = pass_me_data_holder->bb; |
| 188 | DCHECK(bb != nullptr); |
Vladimir Marko | 67c72b8 | 2014-10-09 12:26:10 +0100 | [diff] [blame] | 189 | return c_unit->mir_graph->EliminateNullChecks(bb); |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 190 | } |
Vladimir Marko | bfea9c2 | 2014-01-17 17:49:33 +0000 | [diff] [blame] | 191 | |
Jean Christophe Beyler | e1f65bc | 2014-06-09 10:18:26 -0700 | [diff] [blame] | 192 | void End(PassDataHolder* data) const { |
James C Scott | 4f59668 | 2014-05-01 05:52:04 -0700 | [diff] [blame] | 193 | DCHECK(data != nullptr); |
Jean Christophe Beyler | 75bcc37 | 2014-09-04 08:15:11 -0700 | [diff] [blame] | 194 | CompilationUnit* c_unit = down_cast<PassMEDataHolder*>(data)->c_unit; |
| 195 | DCHECK(c_unit != nullptr); |
Vladimir Marko | 67c72b8 | 2014-10-09 12:26:10 +0100 | [diff] [blame] | 196 | c_unit->mir_graph->EliminateNullChecksEnd(); |
| 197 | } |
| 198 | }; |
| 199 | |
James C Scott | 4f59668 | 2014-05-01 05:52:04 -0700 | [diff] [blame] | 200 | class ClassInitCheckElimination : public PassME { |
Vladimir Marko | bfea9c2 | 2014-01-17 17:49:33 +0000 | [diff] [blame] | 201 | public: |
Vladimir Marko | 622bdbe | 2014-06-19 14:59:05 +0100 | [diff] [blame] | 202 | ClassInitCheckElimination() |
Vladimir Marko | 7baa6f8 | 2014-10-09 18:01:24 +0100 | [diff] [blame] | 203 | : PassME("ClInitCheckElimination", kRepeatingPreOrderDFSTraversal) { |
Vladimir Marko | bfea9c2 | 2014-01-17 17:49:33 +0000 | [diff] [blame] | 204 | } |
| 205 | |
James C Scott | 4f59668 | 2014-05-01 05:52:04 -0700 | [diff] [blame] | 206 | bool Gate(const PassDataHolder* data) const { |
| 207 | DCHECK(data != nullptr); |
Jean Christophe Beyler | 75bcc37 | 2014-09-04 08:15:11 -0700 | [diff] [blame] | 208 | CompilationUnit* c_unit = down_cast<const PassMEDataHolder*>(data)->c_unit; |
| 209 | DCHECK(c_unit != nullptr); |
| 210 | return c_unit->mir_graph->EliminateClassInitChecksGate(); |
Vladimir Marko | bfea9c2 | 2014-01-17 17:49:33 +0000 | [diff] [blame] | 211 | } |
| 212 | |
Jean Christophe Beyler | 09321df | 2014-07-18 15:33:57 -0700 | [diff] [blame] | 213 | bool Worker(PassDataHolder* data) const { |
James C Scott | 4f59668 | 2014-05-01 05:52:04 -0700 | [diff] [blame] | 214 | DCHECK(data != nullptr); |
Jean Christophe Beyler | 09321df | 2014-07-18 15:33:57 -0700 | [diff] [blame] | 215 | PassMEDataHolder* pass_me_data_holder = down_cast<PassMEDataHolder*>(data); |
Jean Christophe Beyler | 75bcc37 | 2014-09-04 08:15:11 -0700 | [diff] [blame] | 216 | CompilationUnit* c_unit = pass_me_data_holder->c_unit; |
| 217 | DCHECK(c_unit != nullptr); |
James C Scott | 4f59668 | 2014-05-01 05:52:04 -0700 | [diff] [blame] | 218 | BasicBlock* bb = pass_me_data_holder->bb; |
| 219 | DCHECK(bb != nullptr); |
Jean Christophe Beyler | 75bcc37 | 2014-09-04 08:15:11 -0700 | [diff] [blame] | 220 | return c_unit->mir_graph->EliminateClassInitChecks(bb); |
Vladimir Marko | bfea9c2 | 2014-01-17 17:49:33 +0000 | [diff] [blame] | 221 | } |
| 222 | |
Jean Christophe Beyler | e1f65bc | 2014-06-09 10:18:26 -0700 | [diff] [blame] | 223 | void End(PassDataHolder* data) const { |
James C Scott | 4f59668 | 2014-05-01 05:52:04 -0700 | [diff] [blame] | 224 | DCHECK(data != nullptr); |
Jean Christophe Beyler | 75bcc37 | 2014-09-04 08:15:11 -0700 | [diff] [blame] | 225 | CompilationUnit* c_unit = down_cast<PassMEDataHolder*>(data)->c_unit; |
| 226 | DCHECK(c_unit != nullptr); |
| 227 | c_unit->mir_graph->EliminateClassInitChecksEnd(); |
Vladimir Marko | bfea9c2 | 2014-01-17 17:49:33 +0000 | [diff] [blame] | 228 | } |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 229 | }; |
| 230 | |
| 231 | /** |
Vladimir Marko | 95a0597 | 2014-05-30 10:01:32 +0100 | [diff] [blame] | 232 | * @class GlobalValueNumberingPass |
| 233 | * @brief Performs the global value numbering pass. |
| 234 | */ |
| 235 | class GlobalValueNumberingPass : public PassME { |
| 236 | public: |
| 237 | GlobalValueNumberingPass() |
Vladimir Marko | 55fff04 | 2014-07-10 12:42:52 +0100 | [diff] [blame] | 238 | : PassME("GVN", kLoopRepeatingTopologicalSortTraversal, "4_post_gvn_cfg") { |
Vladimir Marko | 95a0597 | 2014-05-30 10:01:32 +0100 | [diff] [blame] | 239 | } |
| 240 | |
Vladimir Marko | 55fff04 | 2014-07-10 12:42:52 +0100 | [diff] [blame] | 241 | bool Gate(const PassDataHolder* data) const OVERRIDE { |
Vladimir Marko | 95a0597 | 2014-05-30 10:01:32 +0100 | [diff] [blame] | 242 | DCHECK(data != nullptr); |
Jean Christophe Beyler | 75bcc37 | 2014-09-04 08:15:11 -0700 | [diff] [blame] | 243 | CompilationUnit* c_unit = down_cast<const PassMEDataHolder*>(data)->c_unit; |
| 244 | DCHECK(c_unit != nullptr); |
| 245 | return c_unit->mir_graph->ApplyGlobalValueNumberingGate(); |
Vladimir Marko | 95a0597 | 2014-05-30 10:01:32 +0100 | [diff] [blame] | 246 | } |
| 247 | |
Jean Christophe Beyler | 09321df | 2014-07-18 15:33:57 -0700 | [diff] [blame] | 248 | bool Worker(PassDataHolder* data) const { |
Vladimir Marko | 95a0597 | 2014-05-30 10:01:32 +0100 | [diff] [blame] | 249 | DCHECK(data != nullptr); |
Jean Christophe Beyler | 09321df | 2014-07-18 15:33:57 -0700 | [diff] [blame] | 250 | PassMEDataHolder* pass_me_data_holder = down_cast<PassMEDataHolder*>(data); |
Jean Christophe Beyler | 75bcc37 | 2014-09-04 08:15:11 -0700 | [diff] [blame] | 251 | CompilationUnit* c_unit = pass_me_data_holder->c_unit; |
| 252 | DCHECK(c_unit != nullptr); |
Vladimir Marko | 95a0597 | 2014-05-30 10:01:32 +0100 | [diff] [blame] | 253 | BasicBlock* bb = pass_me_data_holder->bb; |
| 254 | DCHECK(bb != nullptr); |
Jean Christophe Beyler | 75bcc37 | 2014-09-04 08:15:11 -0700 | [diff] [blame] | 255 | return c_unit->mir_graph->ApplyGlobalValueNumbering(bb); |
Vladimir Marko | 95a0597 | 2014-05-30 10:01:32 +0100 | [diff] [blame] | 256 | } |
| 257 | |
Vladimir Marko | 55fff04 | 2014-07-10 12:42:52 +0100 | [diff] [blame] | 258 | void End(PassDataHolder* data) const OVERRIDE { |
Vladimir Marko | 95a0597 | 2014-05-30 10:01:32 +0100 | [diff] [blame] | 259 | DCHECK(data != nullptr); |
Jean Christophe Beyler | 75bcc37 | 2014-09-04 08:15:11 -0700 | [diff] [blame] | 260 | CompilationUnit* c_unit = down_cast<PassMEDataHolder*>(data)->c_unit; |
| 261 | DCHECK(c_unit != nullptr); |
| 262 | c_unit->mir_graph->ApplyGlobalValueNumberingEnd(); |
Vladimir Marko | 95a0597 | 2014-05-30 10:01:32 +0100 | [diff] [blame] | 263 | } |
| 264 | }; |
| 265 | |
| 266 | /** |
Vladimir Marko | 7a01dc2 | 2015-01-02 17:00:44 +0000 | [diff] [blame] | 267 | * @class DeadCodeEliminationPass |
| 268 | * @brief Performs the GVN-based dead code elimination pass. |
| 269 | */ |
| 270 | class DeadCodeEliminationPass : public PassME { |
| 271 | public: |
| 272 | DeadCodeEliminationPass() : PassME("DCE", kPreOrderDFSTraversal, "4_post_dce_cfg") { |
| 273 | } |
| 274 | |
| 275 | bool Gate(const PassDataHolder* data) const OVERRIDE { |
| 276 | DCHECK(data != nullptr); |
| 277 | CompilationUnit* c_unit = down_cast<const PassMEDataHolder*>(data)->c_unit; |
| 278 | DCHECK(c_unit != nullptr); |
| 279 | return c_unit->mir_graph->EliminateDeadCodeGate(); |
| 280 | } |
| 281 | |
| 282 | bool Worker(PassDataHolder* data) const { |
| 283 | DCHECK(data != nullptr); |
| 284 | PassMEDataHolder* pass_me_data_holder = down_cast<PassMEDataHolder*>(data); |
| 285 | CompilationUnit* c_unit = pass_me_data_holder->c_unit; |
| 286 | DCHECK(c_unit != nullptr); |
| 287 | BasicBlock* bb = pass_me_data_holder->bb; |
| 288 | DCHECK(bb != nullptr); |
| 289 | return c_unit->mir_graph->EliminateDeadCode(bb); |
| 290 | } |
| 291 | |
| 292 | void End(PassDataHolder* data) const OVERRIDE { |
| 293 | DCHECK(data != nullptr); |
| 294 | CompilationUnit* c_unit = down_cast<PassMEDataHolder*>(data)->c_unit; |
| 295 | DCHECK(c_unit != nullptr); |
| 296 | c_unit->mir_graph->EliminateDeadCodeEnd(); |
Vladimir Marko | ad67727 | 2015-04-20 10:48:13 +0100 | [diff] [blame] | 297 | } |
| 298 | }; |
| 299 | |
| 300 | /** |
| 301 | * @class GlobalValueNumberingCleanupPass |
| 302 | * @brief Performs the cleanup after global value numbering pass and the dependent |
| 303 | * dead code elimination pass that needs the GVN data. |
| 304 | */ |
| 305 | class GlobalValueNumberingCleanupPass : public PassME { |
| 306 | public: |
| 307 | GlobalValueNumberingCleanupPass() |
| 308 | : PassME("GVNCleanup", kNoNodes, "") { |
| 309 | } |
| 310 | |
| 311 | void Start(PassDataHolder* data) const OVERRIDE { |
| 312 | DCHECK(data != nullptr); |
| 313 | CompilationUnit* c_unit = down_cast<const PassMEDataHolder*>(data)->c_unit; |
| 314 | DCHECK(c_unit != nullptr); |
| 315 | return c_unit->mir_graph->GlobalValueNumberingCleanup(); |
Vladimir Marko | 7a01dc2 | 2015-01-02 17:00:44 +0000 | [diff] [blame] | 316 | } |
| 317 | }; |
| 318 | |
| 319 | /** |
Vladimir Marko | 95a0597 | 2014-05-30 10:01:32 +0100 | [diff] [blame] | 320 | * @class BBCombine |
| 321 | * @brief Perform the basic block combination pass. |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 322 | */ |
James C Scott | 4f59668 | 2014-05-01 05:52:04 -0700 | [diff] [blame] | 323 | class BBCombine : public PassME { |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 324 | public: |
James C Scott | 4f59668 | 2014-05-01 05:52:04 -0700 | [diff] [blame] | 325 | BBCombine() : PassME("BBCombine", kPreOrderDFSTraversal, "5_post_bbcombine_cfg") { |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 326 | } |
| 327 | |
James C Scott | 4f59668 | 2014-05-01 05:52:04 -0700 | [diff] [blame] | 328 | bool Gate(const PassDataHolder* data) const { |
| 329 | DCHECK(data != nullptr); |
Jean Christophe Beyler | 75bcc37 | 2014-09-04 08:15:11 -0700 | [diff] [blame] | 330 | CompilationUnit* c_unit = down_cast<const PassMEDataHolder*>(data)->c_unit; |
| 331 | DCHECK(c_unit != nullptr); |
Vladimir Marko | 312eb25 | 2014-10-07 15:01:57 +0100 | [diff] [blame] | 332 | return c_unit->mir_graph->HasTryCatchBlocks() || |
| 333 | ((c_unit->disable_opt & (1 << kSuppressExceptionEdges)) != 0); |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 334 | } |
| 335 | |
Jean Christophe Beyler | 09321df | 2014-07-18 15:33:57 -0700 | [diff] [blame] | 336 | bool Worker(PassDataHolder* data) const; |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 337 | }; |
| 338 | |
| 339 | /** |
Vladimir Marko | 066f9e4 | 2015-01-16 16:04:43 +0000 | [diff] [blame] | 340 | * @class ConstantPropagation |
| 341 | * @brief Perform a constant propagation pass. |
| 342 | */ |
| 343 | class ConstantPropagation : public PassME { |
| 344 | public: |
| 345 | ConstantPropagation() : PassME("ConstantPropagation") { |
| 346 | } |
| 347 | |
| 348 | void Start(PassDataHolder* data) const { |
| 349 | DCHECK(data != nullptr); |
| 350 | CompilationUnit* c_unit = down_cast<PassMEDataHolder*>(data)->c_unit; |
| 351 | DCHECK(c_unit != nullptr); |
| 352 | c_unit->mir_graph->InitializeConstantPropagation(); |
| 353 | } |
| 354 | |
| 355 | bool Worker(PassDataHolder* data) const { |
| 356 | DCHECK(data != nullptr); |
| 357 | CompilationUnit* c_unit = down_cast<PassMEDataHolder*>(data)->c_unit; |
| 358 | DCHECK(c_unit != nullptr); |
| 359 | BasicBlock* bb = down_cast<PassMEDataHolder*>(data)->bb; |
| 360 | DCHECK(bb != nullptr); |
| 361 | c_unit->mir_graph->DoConstantPropagation(bb); |
| 362 | // No need of repeating, so just return false. |
| 363 | return false; |
| 364 | } |
| 365 | }; |
| 366 | |
| 367 | /** |
| 368 | * @class MethodUseCount |
| 369 | * @brief Count the register uses of the method |
| 370 | */ |
| 371 | class MethodUseCount : public PassME { |
| 372 | public: |
| 373 | MethodUseCount() : PassME("UseCount") { |
| 374 | } |
| 375 | |
| 376 | bool Worker(PassDataHolder* data) const; |
| 377 | |
| 378 | bool Gate(const PassDataHolder* data) const; |
| 379 | }; |
| 380 | |
| 381 | /** |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 382 | * @class BasicBlock Optimizations |
| 383 | * @brief Any simple BasicBlock optimization can be put here. |
| 384 | */ |
James C Scott | 4f59668 | 2014-05-01 05:52:04 -0700 | [diff] [blame] | 385 | class BBOptimizations : public PassME { |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 386 | public: |
Vladimir Marko | 341e425 | 2014-12-19 10:29:51 +0000 | [diff] [blame] | 387 | BBOptimizations() |
| 388 | : PassME("BBOptimizations", kNoNodes, kOptimizationBasicBlockChange, "5_post_bbo_cfg") { |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 389 | } |
| 390 | |
James C Scott | 4f59668 | 2014-05-01 05:52:04 -0700 | [diff] [blame] | 391 | bool Gate(const PassDataHolder* data) const { |
| 392 | DCHECK(data != nullptr); |
Jean Christophe Beyler | 75bcc37 | 2014-09-04 08:15:11 -0700 | [diff] [blame] | 393 | CompilationUnit* c_unit = down_cast<const PassMEDataHolder*>(data)->c_unit; |
| 394 | DCHECK(c_unit != nullptr); |
| 395 | return ((c_unit->disable_opt & (1 << kBBOpt)) == 0); |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 396 | } |
| 397 | |
Vladimir Marko | ffda499 | 2014-12-18 17:05:58 +0000 | [diff] [blame] | 398 | void Start(PassDataHolder* data) const { |
| 399 | DCHECK(data != nullptr); |
| 400 | CompilationUnit* c_unit = down_cast<PassMEDataHolder*>(data)->c_unit; |
| 401 | DCHECK(c_unit != nullptr); |
| 402 | c_unit->mir_graph->BasicBlockOptimizationStart(); |
| 403 | |
| 404 | /* |
| 405 | * This pass has a different ordering depending on the suppress exception, |
| 406 | * so do the pass here for now: |
| 407 | * - Later, the Start should just change the ordering and we can move the extended |
| 408 | * creation into the pass driver's main job with a new iterator |
| 409 | */ |
| 410 | c_unit->mir_graph->BasicBlockOptimization(); |
| 411 | } |
| 412 | |
| 413 | void End(PassDataHolder* data) const { |
| 414 | DCHECK(data != nullptr); |
| 415 | CompilationUnit* c_unit = down_cast<const PassMEDataHolder*>(data)->c_unit; |
| 416 | DCHECK(c_unit != nullptr); |
| 417 | c_unit->mir_graph->BasicBlockOptimizationEnd(); |
Vladimir Marko | 341e425 | 2014-12-19 10:29:51 +0000 | [diff] [blame] | 418 | down_cast<PassMEDataHolder*>(data)->dirty = !c_unit->mir_graph->DfsOrdersUpToDate(); |
Vladimir Marko | ffda499 | 2014-12-18 17:05:58 +0000 | [diff] [blame] | 419 | } |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 420 | }; |
| 421 | |
Vladimir Marko | 8b858e1 | 2014-11-27 14:52:37 +0000 | [diff] [blame] | 422 | /** |
| 423 | * @class SuspendCheckElimination |
| 424 | * @brief Any simple BasicBlock optimization can be put here. |
| 425 | */ |
| 426 | class SuspendCheckElimination : public PassME { |
| 427 | public: |
| 428 | SuspendCheckElimination() |
| 429 | : PassME("SuspendCheckElimination", kTopologicalSortTraversal, "6_post_sce_cfg") { |
| 430 | } |
| 431 | |
| 432 | bool Gate(const PassDataHolder* data) const { |
| 433 | DCHECK(data != nullptr); |
| 434 | CompilationUnit* c_unit = down_cast<const PassMEDataHolder*>(data)->c_unit; |
| 435 | DCHECK(c_unit != nullptr); |
| 436 | return c_unit->mir_graph->EliminateSuspendChecksGate(); |
| 437 | } |
| 438 | |
| 439 | bool Worker(PassDataHolder* data) const { |
| 440 | DCHECK(data != nullptr); |
| 441 | PassMEDataHolder* pass_me_data_holder = down_cast<PassMEDataHolder*>(data); |
| 442 | CompilationUnit* c_unit = pass_me_data_holder->c_unit; |
| 443 | DCHECK(c_unit != nullptr); |
| 444 | BasicBlock* bb = pass_me_data_holder->bb; |
| 445 | DCHECK(bb != nullptr); |
| 446 | return c_unit->mir_graph->EliminateSuspendChecks(bb); |
| 447 | } |
Vladimir Marko | 8b858e1 | 2014-11-27 14:52:37 +0000 | [diff] [blame] | 448 | }; |
| 449 | |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 450 | } // namespace art |
| 451 | |
| 452 | #endif // ART_COMPILER_DEX_BB_OPTIMIZATIONS_H_ |