buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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 | |
Ian Rogers | e77493c | 2014-08-20 15:08:45 -0700 | [diff] [blame] | 17 | #include "base/bit_vector-inl.h" |
Andreas Gampe | 0b9203e | 2015-01-22 20:39:27 -0800 | [diff] [blame] | 18 | #include "base/logging.h" |
Mathieu Chartier | b666f48 | 2015-02-18 14:33:14 -0800 | [diff] [blame] | 19 | #include "base/scoped_arena_containers.h" |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 20 | #include "dataflow_iterator-inl.h" |
Andreas Gampe | 0b9203e | 2015-01-22 20:39:27 -0800 | [diff] [blame] | 21 | #include "dex_flags.h" |
| 22 | #include "driver/compiler_driver.h" |
| 23 | #include "driver/dex_compilation_unit.h" |
Vladimir Marko | 95a0597 | 2014-05-30 10:01:32 +0100 | [diff] [blame] | 24 | #include "global_value_numbering.h" |
Vladimir Marko | 7a01dc2 | 2015-01-02 17:00:44 +0000 | [diff] [blame] | 25 | #include "gvn_dead_code_elimination.h" |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 26 | #include "local_value_numbering.h" |
Vladimir Marko | af6925b | 2014-10-31 16:37:32 +0000 | [diff] [blame] | 27 | #include "mir_field_info.h" |
Vladimir Marko | e490b01 | 2015-02-24 11:32:46 +0000 | [diff] [blame] | 28 | #include "type_inference.h" |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 29 | #include "quick/dex_file_method_inliner.h" |
| 30 | #include "quick/dex_file_to_method_inliner_map.h" |
Andreas Gampe | 53c913b | 2014-08-12 23:19:23 -0700 | [diff] [blame] | 31 | #include "stack.h" |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 32 | |
| 33 | namespace art { |
| 34 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 35 | static unsigned int Predecessors(BasicBlock* bb) { |
Vladimir Marko | e39c54e | 2014-09-22 14:50:02 +0100 | [diff] [blame] | 36 | return bb->predecessors.size(); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | /* Setup a constant value for opcodes thare have the DF_SETS_CONST attribute */ |
Razvan A Lupusoru | d04d309 | 2014-08-04 12:30:20 -0700 | [diff] [blame] | 40 | void MIRGraph::SetConstant(int32_t ssa_reg, int32_t value) { |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 41 | is_constant_v_->SetBit(ssa_reg); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 42 | constant_values_[ssa_reg] = value; |
Vladimir Marko | 066f9e4 | 2015-01-16 16:04:43 +0000 | [diff] [blame] | 43 | reg_location_[ssa_reg].is_const = true; |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 44 | } |
| 45 | |
Razvan A Lupusoru | d04d309 | 2014-08-04 12:30:20 -0700 | [diff] [blame] | 46 | void MIRGraph::SetConstantWide(int32_t ssa_reg, int64_t value) { |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 47 | is_constant_v_->SetBit(ssa_reg); |
Serguei Katkov | 597da1f | 2014-07-15 17:25:46 +0700 | [diff] [blame] | 48 | is_constant_v_->SetBit(ssa_reg + 1); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 49 | constant_values_[ssa_reg] = Low32Bits(value); |
| 50 | constant_values_[ssa_reg + 1] = High32Bits(value); |
Vladimir Marko | 066f9e4 | 2015-01-16 16:04:43 +0000 | [diff] [blame] | 51 | reg_location_[ssa_reg].is_const = true; |
| 52 | reg_location_[ssa_reg + 1].is_const = true; |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 53 | } |
| 54 | |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 55 | void MIRGraph::DoConstantPropagation(BasicBlock* bb) { |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 56 | MIR* mir; |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 57 | |
| 58 | for (mir = bb->first_mir_insn; mir != NULL; mir = mir->next) { |
Alexei Zavjalov | 9d89466 | 2014-04-21 20:45:24 +0700 | [diff] [blame] | 59 | // Skip pass if BB has MIR without SSA representation. |
Jean Christophe Beyler | cc794c3 | 2014-05-02 09:34:13 -0700 | [diff] [blame] | 60 | if (mir->ssa_rep == nullptr) { |
Alexei Zavjalov | 9d89466 | 2014-04-21 20:45:24 +0700 | [diff] [blame] | 61 | return; |
| 62 | } |
| 63 | |
Jean Christophe Beyler | cc794c3 | 2014-05-02 09:34:13 -0700 | [diff] [blame] | 64 | uint64_t df_attributes = GetDataFlowAttributes(mir); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 65 | |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 66 | MIR::DecodedInstruction* d_insn = &mir->dalvikInsn; |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 67 | |
| 68 | if (!(df_attributes & DF_HAS_DEFS)) continue; |
| 69 | |
| 70 | /* Handle instructions that set up constants directly */ |
| 71 | if (df_attributes & DF_SETS_CONST) { |
| 72 | if (df_attributes & DF_DA) { |
| 73 | int32_t vB = static_cast<int32_t>(d_insn->vB); |
| 74 | switch (d_insn->opcode) { |
| 75 | case Instruction::CONST_4: |
| 76 | case Instruction::CONST_16: |
| 77 | case Instruction::CONST: |
| 78 | SetConstant(mir->ssa_rep->defs[0], vB); |
| 79 | break; |
| 80 | case Instruction::CONST_HIGH16: |
| 81 | SetConstant(mir->ssa_rep->defs[0], vB << 16); |
| 82 | break; |
| 83 | case Instruction::CONST_WIDE_16: |
| 84 | case Instruction::CONST_WIDE_32: |
| 85 | SetConstantWide(mir->ssa_rep->defs[0], static_cast<int64_t>(vB)); |
| 86 | break; |
| 87 | case Instruction::CONST_WIDE: |
Brian Carlstrom | b1eba21 | 2013-07-17 18:07:19 -0700 | [diff] [blame] | 88 | SetConstantWide(mir->ssa_rep->defs[0], d_insn->vB_wide); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 89 | break; |
| 90 | case Instruction::CONST_WIDE_HIGH16: |
| 91 | SetConstantWide(mir->ssa_rep->defs[0], static_cast<int64_t>(vB) << 48); |
| 92 | break; |
| 93 | default: |
| 94 | break; |
| 95 | } |
| 96 | } |
| 97 | /* Handle instructions that set up constants directly */ |
| 98 | } else if (df_attributes & DF_IS_MOVE) { |
| 99 | int i; |
| 100 | |
| 101 | for (i = 0; i < mir->ssa_rep->num_uses; i++) { |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 102 | if (!is_constant_v_->IsBitSet(mir->ssa_rep->uses[i])) break; |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 103 | } |
| 104 | /* Move a register holding a constant to another register */ |
| 105 | if (i == mir->ssa_rep->num_uses) { |
| 106 | SetConstant(mir->ssa_rep->defs[0], constant_values_[mir->ssa_rep->uses[0]]); |
| 107 | if (df_attributes & DF_A_WIDE) { |
| 108 | SetConstant(mir->ssa_rep->defs[1], constant_values_[mir->ssa_rep->uses[1]]); |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | /* TODO: implement code to handle arithmetic operations */ |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 114 | } |
| 115 | |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 116 | /* Advance to next strictly dominated MIR node in an extended basic block */ |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 117 | MIR* MIRGraph::AdvanceMIR(BasicBlock** p_bb, MIR* mir) { |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 118 | BasicBlock* bb = *p_bb; |
| 119 | if (mir != NULL) { |
| 120 | mir = mir->next; |
Serguei Katkov | ea39216 | 2015-01-29 17:08:05 +0600 | [diff] [blame] | 121 | while (mir == NULL) { |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 122 | bb = GetBasicBlock(bb->fall_through); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 123 | if ((bb == NULL) || Predecessors(bb) != 1) { |
Serguei Katkov | ea39216 | 2015-01-29 17:08:05 +0600 | [diff] [blame] | 124 | // mir is null and we cannot proceed further. |
| 125 | break; |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 126 | } else { |
Serguei Katkov | ea39216 | 2015-01-29 17:08:05 +0600 | [diff] [blame] | 127 | *p_bb = bb; |
| 128 | mir = bb->first_mir_insn; |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 129 | } |
| 130 | } |
| 131 | } |
| 132 | return mir; |
| 133 | } |
| 134 | |
| 135 | /* |
| 136 | * To be used at an invoke mir. If the logically next mir node represents |
| 137 | * a move-result, return it. Else, return NULL. If a move-result exists, |
| 138 | * it is required to immediately follow the invoke with no intervening |
| 139 | * opcodes or incoming arcs. However, if the result of the invoke is not |
| 140 | * used, a move-result may not be present. |
| 141 | */ |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 142 | MIR* MIRGraph::FindMoveResult(BasicBlock* bb, MIR* mir) { |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 143 | BasicBlock* tbb = bb; |
| 144 | mir = AdvanceMIR(&tbb, mir); |
| 145 | while (mir != NULL) { |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 146 | if ((mir->dalvikInsn.opcode == Instruction::MOVE_RESULT) || |
| 147 | (mir->dalvikInsn.opcode == Instruction::MOVE_RESULT_OBJECT) || |
| 148 | (mir->dalvikInsn.opcode == Instruction::MOVE_RESULT_WIDE)) { |
| 149 | break; |
| 150 | } |
| 151 | // Keep going if pseudo op, otherwise terminate |
Jean Christophe Beyler | 2ab40eb | 2014-06-02 09:03:14 -0700 | [diff] [blame] | 152 | if (MIR::DecodedInstruction::IsPseudoMirOp(mir->dalvikInsn.opcode)) { |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 153 | mir = AdvanceMIR(&tbb, mir); |
buzbee | 35ba7f3 | 2014-05-31 08:59:01 -0700 | [diff] [blame] | 154 | } else { |
| 155 | mir = NULL; |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | return mir; |
| 159 | } |
| 160 | |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 161 | BasicBlock* MIRGraph::NextDominatedBlock(BasicBlock* bb) { |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 162 | if (bb->block_type == kDead) { |
| 163 | return NULL; |
| 164 | } |
| 165 | DCHECK((bb->block_type == kEntryBlock) || (bb->block_type == kDalvikByteCode) |
| 166 | || (bb->block_type == kExitBlock)); |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 167 | BasicBlock* bb_taken = GetBasicBlock(bb->taken); |
| 168 | BasicBlock* bb_fall_through = GetBasicBlock(bb->fall_through); |
buzbee | 1da1e2f | 2013-11-15 13:37:01 -0800 | [diff] [blame] | 169 | if (((bb_fall_through == NULL) && (bb_taken != NULL)) && |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 170 | ((bb_taken->block_type == kDalvikByteCode) || (bb_taken->block_type == kExitBlock))) { |
buzbee | cbcfaf3 | 2013-08-19 07:37:40 -0700 | [diff] [blame] | 171 | // Follow simple unconditional branches. |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 172 | bb = bb_taken; |
buzbee | cbcfaf3 | 2013-08-19 07:37:40 -0700 | [diff] [blame] | 173 | } else { |
| 174 | // Follow simple fallthrough |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 175 | bb = (bb_taken != NULL) ? NULL : bb_fall_through; |
buzbee | cbcfaf3 | 2013-08-19 07:37:40 -0700 | [diff] [blame] | 176 | } |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 177 | if (bb == NULL || (Predecessors(bb) != 1)) { |
| 178 | return NULL; |
| 179 | } |
| 180 | DCHECK((bb->block_type == kDalvikByteCode) || (bb->block_type == kExitBlock)); |
| 181 | return bb; |
| 182 | } |
| 183 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 184 | static MIR* FindPhi(BasicBlock* bb, int ssa_name) { |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 185 | for (MIR* mir = bb->first_mir_insn; mir != NULL; mir = mir->next) { |
| 186 | if (static_cast<int>(mir->dalvikInsn.opcode) == kMirOpPhi) { |
| 187 | for (int i = 0; i < mir->ssa_rep->num_uses; i++) { |
| 188 | if (mir->ssa_rep->uses[i] == ssa_name) { |
| 189 | return mir; |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | return NULL; |
| 195 | } |
| 196 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 197 | static SelectInstructionKind SelectKind(MIR* mir) { |
Chao-ying Fu | 8ac41af | 2014-10-01 16:53:04 -0700 | [diff] [blame] | 198 | // Work with the case when mir is nullptr. |
| 199 | if (mir == nullptr) { |
| 200 | return kSelectNone; |
| 201 | } |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 202 | switch (mir->dalvikInsn.opcode) { |
| 203 | case Instruction::MOVE: |
| 204 | case Instruction::MOVE_OBJECT: |
| 205 | case Instruction::MOVE_16: |
| 206 | case Instruction::MOVE_OBJECT_16: |
| 207 | case Instruction::MOVE_FROM16: |
| 208 | case Instruction::MOVE_OBJECT_FROM16: |
| 209 | return kSelectMove; |
Brian Carlstrom | 6f485c6 | 2013-07-18 15:35:35 -0700 | [diff] [blame] | 210 | case Instruction::CONST: |
| 211 | case Instruction::CONST_4: |
| 212 | case Instruction::CONST_16: |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 213 | return kSelectConst; |
Brian Carlstrom | 6f485c6 | 2013-07-18 15:35:35 -0700 | [diff] [blame] | 214 | case Instruction::GOTO: |
| 215 | case Instruction::GOTO_16: |
| 216 | case Instruction::GOTO_32: |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 217 | return kSelectGoto; |
Brian Carlstrom | 02c8cc6 | 2013-07-18 15:54:44 -0700 | [diff] [blame] | 218 | default: |
| 219 | return kSelectNone; |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 220 | } |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 221 | } |
| 222 | |
Vladimir Marko | a1a7074 | 2014-03-03 10:28:05 +0000 | [diff] [blame] | 223 | static constexpr ConditionCode kIfCcZConditionCodes[] = { |
| 224 | kCondEq, kCondNe, kCondLt, kCondGe, kCondGt, kCondLe |
| 225 | }; |
| 226 | |
Andreas Gampe | 785d2f2 | 2014-11-03 22:57:30 -0800 | [diff] [blame] | 227 | static_assert(arraysize(kIfCcZConditionCodes) == Instruction::IF_LEZ - Instruction::IF_EQZ + 1, |
| 228 | "if_ccz_ccodes_size1"); |
Vladimir Marko | a1a7074 | 2014-03-03 10:28:05 +0000 | [diff] [blame] | 229 | |
Vladimir Marko | a1a7074 | 2014-03-03 10:28:05 +0000 | [diff] [blame] | 230 | static constexpr ConditionCode ConditionCodeForIfCcZ(Instruction::Code opcode) { |
| 231 | return kIfCcZConditionCodes[opcode - Instruction::IF_EQZ]; |
| 232 | } |
| 233 | |
Andreas Gampe | 785d2f2 | 2014-11-03 22:57:30 -0800 | [diff] [blame] | 234 | static_assert(ConditionCodeForIfCcZ(Instruction::IF_EQZ) == kCondEq, "if_eqz ccode"); |
| 235 | static_assert(ConditionCodeForIfCcZ(Instruction::IF_NEZ) == kCondNe, "if_nez ccode"); |
| 236 | static_assert(ConditionCodeForIfCcZ(Instruction::IF_LTZ) == kCondLt, "if_ltz ccode"); |
| 237 | static_assert(ConditionCodeForIfCcZ(Instruction::IF_GEZ) == kCondGe, "if_gez ccode"); |
| 238 | static_assert(ConditionCodeForIfCcZ(Instruction::IF_GTZ) == kCondGt, "if_gtz ccode"); |
| 239 | static_assert(ConditionCodeForIfCcZ(Instruction::IF_LEZ) == kCondLe, "if_lez ccode"); |
Vladimir Marko | a1a7074 | 2014-03-03 10:28:05 +0000 | [diff] [blame] | 240 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 241 | int MIRGraph::GetSSAUseCount(int s_reg) { |
Vladimir Marko | e39c54e | 2014-09-22 14:50:02 +0100 | [diff] [blame] | 242 | DCHECK_LT(static_cast<size_t>(s_reg), ssa_subscripts_.size()); |
| 243 | return raw_use_counts_[s_reg]; |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 244 | } |
| 245 | |
Razvan A Lupusoru | 8d0d03e | 2014-06-06 17:04:52 -0700 | [diff] [blame] | 246 | size_t MIRGraph::GetNumBytesForSpecialTemps() const { |
| 247 | // This logic is written with assumption that Method* is only special temp. |
| 248 | DCHECK_EQ(max_available_special_compiler_temps_, 1u); |
| 249 | return sizeof(StackReference<mirror::ArtMethod>); |
Razvan A Lupusoru | da7a69b | 2014-01-08 15:09:50 -0800 | [diff] [blame] | 250 | } |
| 251 | |
Razvan A Lupusoru | 8d0d03e | 2014-06-06 17:04:52 -0700 | [diff] [blame] | 252 | size_t MIRGraph::GetNumAvailableVRTemps() { |
| 253 | // First take into account all temps reserved for backend. |
| 254 | if (max_available_non_special_compiler_temps_ < reserved_temps_for_backend_) { |
| 255 | return 0; |
| 256 | } |
| 257 | |
| 258 | // Calculate remaining ME temps available. |
| 259 | size_t remaining_me_temps = max_available_non_special_compiler_temps_ - reserved_temps_for_backend_; |
| 260 | |
| 261 | if (num_non_special_compiler_temps_ >= remaining_me_temps) { |
| 262 | return 0; |
| 263 | } else { |
| 264 | return remaining_me_temps - num_non_special_compiler_temps_; |
| 265 | } |
| 266 | } |
Bill Buzbee | 00e1ec6 | 2014-02-27 23:44:13 +0000 | [diff] [blame] | 267 | |
| 268 | // FIXME - will probably need to revisit all uses of this, as type not defined. |
Razvan A Lupusoru | da7a69b | 2014-01-08 15:09:50 -0800 | [diff] [blame] | 269 | static const RegLocation temp_loc = {kLocCompilerTemp, |
buzbee | 091cc40 | 2014-03-31 10:14:40 -0700 | [diff] [blame] | 270 | 0, 1 /*defined*/, 0, 0, 0, 0, 0, 1 /*home*/, |
Bill Buzbee | 00e1ec6 | 2014-02-27 23:44:13 +0000 | [diff] [blame] | 271 | RegStorage(), INVALID_SREG, INVALID_SREG}; |
Razvan A Lupusoru | da7a69b | 2014-01-08 15:09:50 -0800 | [diff] [blame] | 272 | |
| 273 | CompilerTemp* MIRGraph::GetNewCompilerTemp(CompilerTempType ct_type, bool wide) { |
Razvan A Lupusoru | 8d0d03e | 2014-06-06 17:04:52 -0700 | [diff] [blame] | 274 | // Once the compiler temps have been committed, new ones cannot be requested anymore. |
| 275 | DCHECK_EQ(compiler_temps_committed_, false); |
| 276 | // Make sure that reserved for BE set is sane. |
| 277 | DCHECK_LE(reserved_temps_for_backend_, max_available_non_special_compiler_temps_); |
| 278 | |
| 279 | bool verbose = cu_->verbose; |
| 280 | const char* ct_type_str = nullptr; |
| 281 | |
| 282 | if (verbose) { |
| 283 | switch (ct_type) { |
| 284 | case kCompilerTempBackend: |
| 285 | ct_type_str = "backend"; |
| 286 | break; |
| 287 | case kCompilerTempSpecialMethodPtr: |
| 288 | ct_type_str = "method*"; |
| 289 | break; |
| 290 | case kCompilerTempVR: |
| 291 | ct_type_str = "VR"; |
| 292 | break; |
| 293 | default: |
| 294 | ct_type_str = "unknown"; |
| 295 | break; |
Razvan A Lupusoru | da7a69b | 2014-01-08 15:09:50 -0800 | [diff] [blame] | 296 | } |
Razvan A Lupusoru | 8d0d03e | 2014-06-06 17:04:52 -0700 | [diff] [blame] | 297 | LOG(INFO) << "CompilerTemps: A compiler temp of type " << ct_type_str << " that is " |
| 298 | << (wide ? "wide is being requested." : "not wide is being requested."); |
Razvan A Lupusoru | da7a69b | 2014-01-08 15:09:50 -0800 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | CompilerTemp *compiler_temp = static_cast<CompilerTemp *>(arena_->Alloc(sizeof(CompilerTemp), |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 302 | kArenaAllocRegAlloc)); |
Razvan A Lupusoru | da7a69b | 2014-01-08 15:09:50 -0800 | [diff] [blame] | 303 | |
| 304 | // Create the type of temp requested. Special temps need special handling because |
| 305 | // they have a specific virtual register assignment. |
| 306 | if (ct_type == kCompilerTempSpecialMethodPtr) { |
Razvan A Lupusoru | 8d0d03e | 2014-06-06 17:04:52 -0700 | [diff] [blame] | 307 | // This has a special location on stack which is 32-bit or 64-bit depending |
| 308 | // on mode. However, we don't want to overlap with non-special section |
| 309 | // and thus even for 64-bit, we allow only a non-wide temp to be requested. |
Razvan A Lupusoru | da7a69b | 2014-01-08 15:09:50 -0800 | [diff] [blame] | 310 | DCHECK_EQ(wide, false); |
Razvan A Lupusoru | da7a69b | 2014-01-08 15:09:50 -0800 | [diff] [blame] | 311 | |
Razvan A Lupusoru | 8d0d03e | 2014-06-06 17:04:52 -0700 | [diff] [blame] | 312 | // The vreg is always the first special temp for method ptr. |
| 313 | compiler_temp->v_reg = GetFirstSpecialTempVR(); |
| 314 | |
| 315 | } else if (ct_type == kCompilerTempBackend) { |
| 316 | requested_backend_temp_ = true; |
| 317 | |
| 318 | // Make sure that we are not exceeding temps reserved for BE. |
| 319 | // Since VR temps cannot be requested once the BE temps are requested, we |
| 320 | // allow reservation of VR temps as well for BE. We |
| 321 | size_t available_temps = reserved_temps_for_backend_ + GetNumAvailableVRTemps(); |
Vladimir Marko | cc23481 | 2015-04-07 09:36:09 +0100 | [diff] [blame] | 322 | size_t needed_temps = wide ? 2u : 1u; |
| 323 | if (available_temps < needed_temps) { |
Razvan A Lupusoru | 8d0d03e | 2014-06-06 17:04:52 -0700 | [diff] [blame] | 324 | if (verbose) { |
Vladimir Marko | cc23481 | 2015-04-07 09:36:09 +0100 | [diff] [blame] | 325 | LOG(INFO) << "CompilerTemps: Not enough temp(s) of type " << ct_type_str |
| 326 | << " are available."; |
Razvan A Lupusoru | 8d0d03e | 2014-06-06 17:04:52 -0700 | [diff] [blame] | 327 | } |
| 328 | return nullptr; |
| 329 | } |
| 330 | |
| 331 | // Update the remaining reserved temps since we have now used them. |
| 332 | // Note that the code below is actually subtracting to remove them from reserve |
| 333 | // once they have been claimed. It is careful to not go below zero. |
Vladimir Marko | cc23481 | 2015-04-07 09:36:09 +0100 | [diff] [blame] | 334 | reserved_temps_for_backend_ = |
| 335 | std::max(reserved_temps_for_backend_, needed_temps) - needed_temps; |
Razvan A Lupusoru | 8d0d03e | 2014-06-06 17:04:52 -0700 | [diff] [blame] | 336 | |
| 337 | // The new non-special compiler temp must receive a unique v_reg. |
| 338 | compiler_temp->v_reg = GetFirstNonSpecialTempVR() + num_non_special_compiler_temps_; |
| 339 | num_non_special_compiler_temps_++; |
| 340 | } else if (ct_type == kCompilerTempVR) { |
| 341 | // Once we start giving out BE temps, we don't allow anymore ME temps to be requested. |
| 342 | // This is done in order to prevent problems with ssa since these structures are allocated |
| 343 | // and managed by the ME. |
| 344 | DCHECK_EQ(requested_backend_temp_, false); |
| 345 | |
| 346 | // There is a limit to the number of non-special temps so check to make sure it wasn't exceeded. |
| 347 | size_t available_temps = GetNumAvailableVRTemps(); |
| 348 | if (available_temps <= 0 || (available_temps <= 1 && wide)) { |
| 349 | if (verbose) { |
| 350 | LOG(INFO) << "CompilerTemps: Not enough temp(s) of type " << ct_type_str << " are available."; |
| 351 | } |
| 352 | return nullptr; |
| 353 | } |
| 354 | |
| 355 | // The new non-special compiler temp must receive a unique v_reg. |
| 356 | compiler_temp->v_reg = GetFirstNonSpecialTempVR() + num_non_special_compiler_temps_; |
| 357 | num_non_special_compiler_temps_++; |
Razvan A Lupusoru | da7a69b | 2014-01-08 15:09:50 -0800 | [diff] [blame] | 358 | } else { |
Razvan A Lupusoru | 8d0d03e | 2014-06-06 17:04:52 -0700 | [diff] [blame] | 359 | UNIMPLEMENTED(FATAL) << "No handling for compiler temp type " << ct_type_str << "."; |
| 360 | } |
Razvan A Lupusoru | da7a69b | 2014-01-08 15:09:50 -0800 | [diff] [blame] | 361 | |
Razvan A Lupusoru | 8d0d03e | 2014-06-06 17:04:52 -0700 | [diff] [blame] | 362 | // We allocate an sreg as well to make developer life easier. |
| 363 | // However, if this is requested from an ME pass that will recalculate ssa afterwards, |
| 364 | // this sreg is no longer valid. The caller should be aware of this. |
| 365 | compiler_temp->s_reg_low = AddNewSReg(compiler_temp->v_reg); |
| 366 | |
| 367 | if (verbose) { |
| 368 | LOG(INFO) << "CompilerTemps: New temp of type " << ct_type_str << " with v" << compiler_temp->v_reg |
| 369 | << " and s" << compiler_temp->s_reg_low << " has been created."; |
| 370 | } |
| 371 | |
| 372 | if (wide) { |
| 373 | // Only non-special temps are handled as wide for now. |
| 374 | // Note that the number of non special temps is incremented below. |
| 375 | DCHECK(ct_type == kCompilerTempBackend || ct_type == kCompilerTempVR); |
| 376 | |
| 377 | // Ensure that the two registers are consecutive. |
| 378 | int ssa_reg_low = compiler_temp->s_reg_low; |
| 379 | int ssa_reg_high = AddNewSReg(compiler_temp->v_reg + 1); |
Razvan A Lupusoru | da7a69b | 2014-01-08 15:09:50 -0800 | [diff] [blame] | 380 | num_non_special_compiler_temps_++; |
| 381 | |
Razvan A Lupusoru | 8d0d03e | 2014-06-06 17:04:52 -0700 | [diff] [blame] | 382 | if (verbose) { |
| 383 | LOG(INFO) << "CompilerTemps: The wide part of temp of type " << ct_type_str << " is v" |
| 384 | << compiler_temp->v_reg + 1 << " and s" << ssa_reg_high << "."; |
| 385 | } |
Chao-ying Fu | 54d36b6 | 2014-05-22 17:25:02 -0700 | [diff] [blame] | 386 | |
Razvan A Lupusoru | 8d0d03e | 2014-06-06 17:04:52 -0700 | [diff] [blame] | 387 | if (reg_location_ != nullptr) { |
| 388 | reg_location_[ssa_reg_high] = temp_loc; |
| 389 | reg_location_[ssa_reg_high].high_word = true; |
| 390 | reg_location_[ssa_reg_high].s_reg_low = ssa_reg_low; |
| 391 | reg_location_[ssa_reg_high].wide = true; |
Razvan A Lupusoru | da7a69b | 2014-01-08 15:09:50 -0800 | [diff] [blame] | 392 | } |
| 393 | } |
| 394 | |
Razvan A Lupusoru | 8d0d03e | 2014-06-06 17:04:52 -0700 | [diff] [blame] | 395 | // If the register locations have already been allocated, add the information |
| 396 | // about the temp. We will not overflow because they have been initialized |
| 397 | // to support the maximum number of temps. For ME temps that have multiple |
| 398 | // ssa versions, the structures below will be expanded on the post pass cleanup. |
Razvan A Lupusoru | da7a69b | 2014-01-08 15:09:50 -0800 | [diff] [blame] | 399 | if (reg_location_ != nullptr) { |
| 400 | int ssa_reg_low = compiler_temp->s_reg_low; |
| 401 | reg_location_[ssa_reg_low] = temp_loc; |
| 402 | reg_location_[ssa_reg_low].s_reg_low = ssa_reg_low; |
| 403 | reg_location_[ssa_reg_low].wide = wide; |
Razvan A Lupusoru | da7a69b | 2014-01-08 15:09:50 -0800 | [diff] [blame] | 404 | } |
| 405 | |
Razvan A Lupusoru | da7a69b | 2014-01-08 15:09:50 -0800 | [diff] [blame] | 406 | return compiler_temp; |
| 407 | } |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 408 | |
Vladimir Marko | cc23481 | 2015-04-07 09:36:09 +0100 | [diff] [blame] | 409 | void MIRGraph::RemoveLastCompilerTemp(CompilerTempType ct_type, bool wide, CompilerTemp* temp) { |
| 410 | // Once the compiler temps have been committed, it's too late for any modifications. |
| 411 | DCHECK_EQ(compiler_temps_committed_, false); |
| 412 | |
| 413 | size_t used_temps = wide ? 2u : 1u; |
| 414 | |
| 415 | if (ct_type == kCompilerTempBackend) { |
| 416 | DCHECK(requested_backend_temp_); |
| 417 | |
| 418 | // Make the temps available to backend again. |
| 419 | reserved_temps_for_backend_ += used_temps; |
| 420 | } else if (ct_type == kCompilerTempVR) { |
| 421 | DCHECK(!requested_backend_temp_); |
| 422 | } else { |
| 423 | UNIMPLEMENTED(FATAL) << "No handling for compiler temp type " << static_cast<int>(ct_type); |
| 424 | } |
| 425 | |
| 426 | // Reduce the number of non-special compiler temps. |
| 427 | DCHECK_LE(used_temps, num_non_special_compiler_temps_); |
| 428 | num_non_special_compiler_temps_ -= used_temps; |
| 429 | |
| 430 | // Check that this was really the last temp. |
| 431 | DCHECK_EQ(static_cast<size_t>(temp->v_reg), |
| 432 | GetFirstNonSpecialTempVR() + num_non_special_compiler_temps_); |
| 433 | |
| 434 | if (cu_->verbose) { |
| 435 | LOG(INFO) << "Last temporary has been removed."; |
| 436 | } |
| 437 | } |
| 438 | |
Vladimir Marko | 7ab2fce | 2014-11-28 13:38:28 +0000 | [diff] [blame] | 439 | static bool EvaluateBranch(Instruction::Code opcode, int32_t src1, int32_t src2) { |
| 440 | bool is_taken; |
| 441 | switch (opcode) { |
| 442 | case Instruction::IF_EQ: is_taken = (src1 == src2); break; |
| 443 | case Instruction::IF_NE: is_taken = (src1 != src2); break; |
| 444 | case Instruction::IF_LT: is_taken = (src1 < src2); break; |
| 445 | case Instruction::IF_GE: is_taken = (src1 >= src2); break; |
| 446 | case Instruction::IF_GT: is_taken = (src1 > src2); break; |
| 447 | case Instruction::IF_LE: is_taken = (src1 <= src2); break; |
| 448 | case Instruction::IF_EQZ: is_taken = (src1 == 0); break; |
| 449 | case Instruction::IF_NEZ: is_taken = (src1 != 0); break; |
| 450 | case Instruction::IF_LTZ: is_taken = (src1 < 0); break; |
| 451 | case Instruction::IF_GEZ: is_taken = (src1 >= 0); break; |
| 452 | case Instruction::IF_GTZ: is_taken = (src1 > 0); break; |
| 453 | case Instruction::IF_LEZ: is_taken = (src1 <= 0); break; |
| 454 | default: |
| 455 | LOG(FATAL) << "Unexpected opcode " << opcode; |
| 456 | UNREACHABLE(); |
| 457 | } |
| 458 | return is_taken; |
| 459 | } |
| 460 | |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 461 | /* Do some MIR-level extended basic block optimizations */ |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 462 | bool MIRGraph::BasicBlockOpt(BasicBlock* bb) { |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 463 | if (bb->block_type == kDead) { |
| 464 | return true; |
| 465 | } |
Ningsheng Jian | a262f77 | 2014-11-25 16:48:07 +0800 | [diff] [blame] | 466 | // Currently multiply-accumulate backend supports are only available on arm32 and arm64. |
| 467 | if (cu_->instruction_set == kArm64 || cu_->instruction_set == kThumb2) { |
| 468 | MultiplyAddOpt(bb); |
| 469 | } |
Vladimir Marko | 415ac88 | 2014-09-30 18:09:14 +0100 | [diff] [blame] | 470 | bool use_lvn = bb->use_lvn && (cu_->disable_opt & (1u << kLocalValueNumbering)) == 0u; |
Vladimir Marko | 2ac01fc | 2014-05-22 12:09:08 +0100 | [diff] [blame] | 471 | std::unique_ptr<ScopedArenaAllocator> allocator; |
Vladimir Marko | 95a0597 | 2014-05-30 10:01:32 +0100 | [diff] [blame] | 472 | std::unique_ptr<GlobalValueNumbering> global_valnum; |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 473 | std::unique_ptr<LocalValueNumbering> local_valnum; |
buzbee | 1da1e2f | 2013-11-15 13:37:01 -0800 | [diff] [blame] | 474 | if (use_lvn) { |
Vladimir Marko | 2ac01fc | 2014-05-22 12:09:08 +0100 | [diff] [blame] | 475 | allocator.reset(ScopedArenaAllocator::Create(&cu_->arena_stack)); |
Vladimir Marko | 415ac88 | 2014-09-30 18:09:14 +0100 | [diff] [blame] | 476 | global_valnum.reset(new (allocator.get()) GlobalValueNumbering(cu_, allocator.get(), |
| 477 | GlobalValueNumbering::kModeLvn)); |
Vladimir Marko | b19955d | 2014-07-29 12:04:10 +0100 | [diff] [blame] | 478 | local_valnum.reset(new (allocator.get()) LocalValueNumbering(global_valnum.get(), bb->id, |
| 479 | allocator.get())); |
buzbee | 1da1e2f | 2013-11-15 13:37:01 -0800 | [diff] [blame] | 480 | } |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 481 | while (bb != NULL) { |
| 482 | for (MIR* mir = bb->first_mir_insn; mir != NULL; mir = mir->next) { |
| 483 | // TUNING: use the returned value number for CSE. |
buzbee | 1da1e2f | 2013-11-15 13:37:01 -0800 | [diff] [blame] | 484 | if (use_lvn) { |
| 485 | local_valnum->GetValueNumber(mir); |
| 486 | } |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 487 | // Look for interesting opcodes, skip otherwise |
| 488 | Instruction::Code opcode = mir->dalvikInsn.opcode; |
| 489 | switch (opcode) { |
Vladimir Marko | 7ab2fce | 2014-11-28 13:38:28 +0000 | [diff] [blame] | 490 | case Instruction::IF_EQ: |
| 491 | case Instruction::IF_NE: |
| 492 | case Instruction::IF_LT: |
| 493 | case Instruction::IF_GE: |
| 494 | case Instruction::IF_GT: |
| 495 | case Instruction::IF_LE: |
| 496 | if (!IsConst(mir->ssa_rep->uses[1])) { |
| 497 | break; |
| 498 | } |
| 499 | FALLTHROUGH_INTENDED; |
| 500 | case Instruction::IF_EQZ: |
| 501 | case Instruction::IF_NEZ: |
| 502 | case Instruction::IF_LTZ: |
| 503 | case Instruction::IF_GEZ: |
| 504 | case Instruction::IF_GTZ: |
| 505 | case Instruction::IF_LEZ: |
| 506 | // Result known at compile time? |
| 507 | if (IsConst(mir->ssa_rep->uses[0])) { |
| 508 | int32_t rhs = (mir->ssa_rep->num_uses == 2) ? ConstantValue(mir->ssa_rep->uses[1]) : 0; |
| 509 | bool is_taken = EvaluateBranch(opcode, ConstantValue(mir->ssa_rep->uses[0]), rhs); |
| 510 | BasicBlockId edge_to_kill = is_taken ? bb->fall_through : bb->taken; |
| 511 | if (is_taken) { |
| 512 | // Replace with GOTO. |
| 513 | bb->fall_through = NullBasicBlockId; |
| 514 | mir->dalvikInsn.opcode = Instruction::GOTO; |
| 515 | mir->dalvikInsn.vA = |
| 516 | IsInstructionIfCc(opcode) ? mir->dalvikInsn.vC : mir->dalvikInsn.vB; |
| 517 | } else { |
| 518 | // Make NOP. |
| 519 | bb->taken = NullBasicBlockId; |
| 520 | mir->dalvikInsn.opcode = static_cast<Instruction::Code>(kMirOpNop); |
| 521 | } |
| 522 | mir->ssa_rep->num_uses = 0; |
| 523 | BasicBlock* successor_to_unlink = GetBasicBlock(edge_to_kill); |
| 524 | successor_to_unlink->ErasePredecessor(bb->id); |
Vladimir Marko | 341e425 | 2014-12-19 10:29:51 +0000 | [diff] [blame] | 525 | // We have changed the graph structure. |
| 526 | dfs_orders_up_to_date_ = false; |
| 527 | domination_up_to_date_ = false; |
| 528 | topological_order_up_to_date_ = false; |
| 529 | // Keep MIR SSA rep, the worst that can happen is a Phi with just 1 input. |
Vladimir Marko | 7ab2fce | 2014-11-28 13:38:28 +0000 | [diff] [blame] | 530 | } |
| 531 | break; |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 532 | case Instruction::CMPL_FLOAT: |
| 533 | case Instruction::CMPL_DOUBLE: |
| 534 | case Instruction::CMPG_FLOAT: |
| 535 | case Instruction::CMPG_DOUBLE: |
| 536 | case Instruction::CMP_LONG: |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 537 | if ((cu_->disable_opt & (1 << kBranchFusing)) != 0) { |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 538 | // Bitcode doesn't allow this optimization. |
| 539 | break; |
| 540 | } |
| 541 | if (mir->next != NULL) { |
| 542 | MIR* mir_next = mir->next; |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 543 | // Make sure result of cmp is used by next insn and nowhere else |
Jean Christophe Beyler | c26efa8 | 2014-06-01 11:39:39 -0700 | [diff] [blame] | 544 | if (IsInstructionIfCcZ(mir_next->dalvikInsn.opcode) && |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 545 | (mir->ssa_rep->defs[0] == mir_next->ssa_rep->uses[0]) && |
| 546 | (GetSSAUseCount(mir->ssa_rep->defs[0]) == 1)) { |
Vladimir Marko | a1a7074 | 2014-03-03 10:28:05 +0000 | [diff] [blame] | 547 | mir_next->meta.ccode = ConditionCodeForIfCcZ(mir_next->dalvikInsn.opcode); |
Brian Carlstrom | df62950 | 2013-07-17 22:39:56 -0700 | [diff] [blame] | 548 | switch (opcode) { |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 549 | case Instruction::CMPL_FLOAT: |
| 550 | mir_next->dalvikInsn.opcode = |
| 551 | static_cast<Instruction::Code>(kMirOpFusedCmplFloat); |
| 552 | break; |
| 553 | case Instruction::CMPL_DOUBLE: |
| 554 | mir_next->dalvikInsn.opcode = |
| 555 | static_cast<Instruction::Code>(kMirOpFusedCmplDouble); |
| 556 | break; |
| 557 | case Instruction::CMPG_FLOAT: |
| 558 | mir_next->dalvikInsn.opcode = |
| 559 | static_cast<Instruction::Code>(kMirOpFusedCmpgFloat); |
| 560 | break; |
| 561 | case Instruction::CMPG_DOUBLE: |
| 562 | mir_next->dalvikInsn.opcode = |
| 563 | static_cast<Instruction::Code>(kMirOpFusedCmpgDouble); |
| 564 | break; |
| 565 | case Instruction::CMP_LONG: |
| 566 | mir_next->dalvikInsn.opcode = |
| 567 | static_cast<Instruction::Code>(kMirOpFusedCmpLong); |
| 568 | break; |
| 569 | default: LOG(ERROR) << "Unexpected opcode: " << opcode; |
| 570 | } |
| 571 | mir->dalvikInsn.opcode = static_cast<Instruction::Code>(kMirOpNop); |
Zheng Xu | b218c85 | 2014-12-08 18:18:01 +0800 | [diff] [blame] | 572 | // Clear use count of temp VR. |
| 573 | use_counts_[mir->ssa_rep->defs[0]] = 0; |
| 574 | raw_use_counts_[mir->ssa_rep->defs[0]] = 0; |
Jean Christophe Beyler | c26efa8 | 2014-06-01 11:39:39 -0700 | [diff] [blame] | 575 | // Copy the SSA information that is relevant. |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 576 | mir_next->ssa_rep->num_uses = mir->ssa_rep->num_uses; |
| 577 | mir_next->ssa_rep->uses = mir->ssa_rep->uses; |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 578 | mir_next->ssa_rep->num_defs = 0; |
| 579 | mir->ssa_rep->num_uses = 0; |
| 580 | mir->ssa_rep->num_defs = 0; |
Jean Christophe Beyler | c26efa8 | 2014-06-01 11:39:39 -0700 | [diff] [blame] | 581 | // Copy in the decoded instruction information for potential SSA re-creation. |
| 582 | mir_next->dalvikInsn.vA = mir->dalvikInsn.vB; |
| 583 | mir_next->dalvikInsn.vB = mir->dalvikInsn.vC; |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 584 | } |
| 585 | } |
| 586 | break; |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 587 | default: |
| 588 | break; |
| 589 | } |
| 590 | // Is this the select pattern? |
Razvan A Lupusoru | e27b3bf | 2014-01-23 09:41:45 -0800 | [diff] [blame] | 591 | // TODO: flesh out support for Mips. NOTE: llvm's select op doesn't quite work here. |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 592 | // TUNING: expand to support IF_xx compare & branches |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 593 | if ((cu_->instruction_set == kArm64 || cu_->instruction_set == kThumb2 || |
Serban Constantinescu | 05e27ff | 2014-05-28 13:21:45 +0100 | [diff] [blame] | 594 | cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) && |
Vladimir Marko | a1a7074 | 2014-03-03 10:28:05 +0000 | [diff] [blame] | 595 | IsInstructionIfCcZ(mir->dalvikInsn.opcode)) { |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 596 | BasicBlock* ft = GetBasicBlock(bb->fall_through); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 597 | DCHECK(ft != NULL); |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 598 | BasicBlock* ft_ft = GetBasicBlock(ft->fall_through); |
| 599 | BasicBlock* ft_tk = GetBasicBlock(ft->taken); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 600 | |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 601 | BasicBlock* tk = GetBasicBlock(bb->taken); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 602 | DCHECK(tk != NULL); |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 603 | BasicBlock* tk_ft = GetBasicBlock(tk->fall_through); |
| 604 | BasicBlock* tk_tk = GetBasicBlock(tk->taken); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 605 | |
| 606 | /* |
| 607 | * In the select pattern, the taken edge goes to a block that unconditionally |
| 608 | * transfers to the rejoin block and the fall_though edge goes to a block that |
| 609 | * unconditionally falls through to the rejoin block. |
| 610 | */ |
| 611 | if ((tk_ft == NULL) && (ft_tk == NULL) && (tk_tk == ft_ft) && |
| 612 | (Predecessors(tk) == 1) && (Predecessors(ft) == 1)) { |
| 613 | /* |
Vladimir Marko | 8b858e1 | 2014-11-27 14:52:37 +0000 | [diff] [blame] | 614 | * Okay - we have the basic diamond shape. |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 615 | */ |
Serban Constantinescu | 05e27ff | 2014-05-28 13:21:45 +0100 | [diff] [blame] | 616 | |
| 617 | // TODO: Add logic for LONG. |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 618 | // Are the block bodies something we can handle? |
| 619 | if ((ft->first_mir_insn == ft->last_mir_insn) && |
| 620 | (tk->first_mir_insn != tk->last_mir_insn) && |
| 621 | (tk->first_mir_insn->next == tk->last_mir_insn) && |
| 622 | ((SelectKind(ft->first_mir_insn) == kSelectMove) || |
| 623 | (SelectKind(ft->first_mir_insn) == kSelectConst)) && |
| 624 | (SelectKind(ft->first_mir_insn) == SelectKind(tk->first_mir_insn)) && |
| 625 | (SelectKind(tk->last_mir_insn) == kSelectGoto)) { |
| 626 | // Almost there. Are the instructions targeting the same vreg? |
| 627 | MIR* if_true = tk->first_mir_insn; |
| 628 | MIR* if_false = ft->first_mir_insn; |
| 629 | // It's possible that the target of the select isn't used - skip those (rare) cases. |
| 630 | MIR* phi = FindPhi(tk_tk, if_true->ssa_rep->defs[0]); |
| 631 | if ((phi != NULL) && (if_true->dalvikInsn.vA == if_false->dalvikInsn.vA)) { |
| 632 | /* |
| 633 | * We'll convert the IF_EQZ/IF_NEZ to a SELECT. We need to find the |
| 634 | * Phi node in the merge block and delete it (while using the SSA name |
| 635 | * of the merge as the target of the SELECT. Delete both taken and |
| 636 | * fallthrough blocks, and set fallthrough to merge block. |
| 637 | * NOTE: not updating other dataflow info (no longer used at this point). |
| 638 | * If this changes, need to update i_dom, etc. here (and in CombineBlocks). |
| 639 | */ |
Vladimir Marko | a1a7074 | 2014-03-03 10:28:05 +0000 | [diff] [blame] | 640 | mir->meta.ccode = ConditionCodeForIfCcZ(mir->dalvikInsn.opcode); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 641 | mir->dalvikInsn.opcode = static_cast<Instruction::Code>(kMirOpSelect); |
| 642 | bool const_form = (SelectKind(if_true) == kSelectConst); |
| 643 | if ((SelectKind(if_true) == kSelectMove)) { |
| 644 | if (IsConst(if_true->ssa_rep->uses[0]) && |
| 645 | IsConst(if_false->ssa_rep->uses[0])) { |
| 646 | const_form = true; |
| 647 | if_true->dalvikInsn.vB = ConstantValue(if_true->ssa_rep->uses[0]); |
| 648 | if_false->dalvikInsn.vB = ConstantValue(if_false->ssa_rep->uses[0]); |
| 649 | } |
| 650 | } |
| 651 | if (const_form) { |
Razvan A Lupusoru | e27b3bf | 2014-01-23 09:41:45 -0800 | [diff] [blame] | 652 | /* |
| 653 | * TODO: If both constants are the same value, then instead of generating |
| 654 | * a select, we should simply generate a const bytecode. This should be |
| 655 | * considered after inlining which can lead to CFG of this form. |
| 656 | */ |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 657 | // "true" set val in vB |
| 658 | mir->dalvikInsn.vB = if_true->dalvikInsn.vB; |
| 659 | // "false" set val in vC |
| 660 | mir->dalvikInsn.vC = if_false->dalvikInsn.vB; |
| 661 | } else { |
| 662 | DCHECK_EQ(SelectKind(if_true), kSelectMove); |
| 663 | DCHECK_EQ(SelectKind(if_false), kSelectMove); |
Vladimir Marko | e4fcc5b | 2015-02-13 10:28:29 +0000 | [diff] [blame] | 664 | int32_t* src_ssa = arena_->AllocArray<int32_t>(3, kArenaAllocDFInfo); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 665 | src_ssa[0] = mir->ssa_rep->uses[0]; |
| 666 | src_ssa[1] = if_true->ssa_rep->uses[0]; |
| 667 | src_ssa[2] = if_false->ssa_rep->uses[0]; |
| 668 | mir->ssa_rep->uses = src_ssa; |
| 669 | mir->ssa_rep->num_uses = 3; |
| 670 | } |
Vladimir Marko | e490b01 | 2015-02-24 11:32:46 +0000 | [diff] [blame] | 671 | AllocateSSADefData(mir, 1); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 672 | /* |
| 673 | * There is usually a Phi node in the join block for our two cases. If the |
| 674 | * Phi node only contains our two cases as input, we will use the result |
| 675 | * SSA name of the Phi node as our select result and delete the Phi. If |
| 676 | * the Phi node has more than two operands, we will arbitrarily use the SSA |
Vladimir Marko | 341e425 | 2014-12-19 10:29:51 +0000 | [diff] [blame] | 677 | * name of the "false" path, delete the SSA name of the "true" path from the |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 678 | * Phi node (and fix up the incoming arc list). |
| 679 | */ |
| 680 | if (phi->ssa_rep->num_uses == 2) { |
| 681 | mir->ssa_rep->defs[0] = phi->ssa_rep->defs[0]; |
Vladimir Marko | 341e425 | 2014-12-19 10:29:51 +0000 | [diff] [blame] | 682 | // Rather than changing the Phi to kMirOpNop, remove it completely. |
| 683 | // This avoids leaving other Phis after kMirOpNop (i.e. a non-Phi) insn. |
| 684 | tk_tk->RemoveMIR(phi); |
| 685 | int dead_false_def = if_false->ssa_rep->defs[0]; |
| 686 | raw_use_counts_[dead_false_def] = use_counts_[dead_false_def] = 0; |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 687 | } else { |
Vladimir Marko | 341e425 | 2014-12-19 10:29:51 +0000 | [diff] [blame] | 688 | int live_def = if_false->ssa_rep->defs[0]; |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 689 | mir->ssa_rep->defs[0] = live_def; |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 690 | } |
Vladimir Marko | 341e425 | 2014-12-19 10:29:51 +0000 | [diff] [blame] | 691 | int dead_true_def = if_true->ssa_rep->defs[0]; |
| 692 | raw_use_counts_[dead_true_def] = use_counts_[dead_true_def] = 0; |
Vladimir Marko | 6e07183 | 2015-03-25 11:13:39 +0000 | [diff] [blame] | 693 | // Update ending vreg->sreg map for GC maps generation. |
| 694 | int def_vreg = SRegToVReg(mir->ssa_rep->defs[0]); |
| 695 | bb->data_flow_info->vreg_to_ssa_map_exit[def_vreg] = mir->ssa_rep->defs[0]; |
Vladimir Marko | 341e425 | 2014-12-19 10:29:51 +0000 | [diff] [blame] | 696 | // We want to remove ft and tk and link bb directly to ft_ft. First, we need |
| 697 | // to update all Phi inputs correctly with UpdatePredecessor(ft->id, bb->id) |
| 698 | // since the live_def above comes from ft->first_mir_insn (if_false). |
| 699 | DCHECK(if_false == ft->first_mir_insn); |
| 700 | ft_ft->UpdatePredecessor(ft->id, bb->id); |
| 701 | // Correct the rest of the links between bb, ft and ft_ft. |
| 702 | ft->ErasePredecessor(bb->id); |
| 703 | ft->fall_through = NullBasicBlockId; |
| 704 | bb->fall_through = ft_ft->id; |
| 705 | // Now we can kill tk and ft. |
| 706 | tk->Kill(this); |
| 707 | ft->Kill(this); |
| 708 | // NOTE: DFS order, domination info and topological order are still usable |
| 709 | // despite the newly dead blocks. |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 710 | } |
| 711 | } |
| 712 | } |
| 713 | } |
| 714 | } |
buzbee | 1da1e2f | 2013-11-15 13:37:01 -0800 | [diff] [blame] | 715 | bb = ((cu_->disable_opt & (1 << kSuppressExceptionEdges)) != 0) ? NextDominatedBlock(bb) : NULL; |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 716 | } |
Vladimir Marko | 95a0597 | 2014-05-30 10:01:32 +0100 | [diff] [blame] | 717 | if (use_lvn && UNLIKELY(!global_valnum->Good())) { |
Vladimir Marko | 2ac01fc | 2014-05-22 12:09:08 +0100 | [diff] [blame] | 718 | LOG(WARNING) << "LVN overflow in " << PrettyMethod(cu_->method_idx, *cu_->dex_file); |
| 719 | } |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 720 | |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 721 | return true; |
| 722 | } |
| 723 | |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 724 | /* Collect stats on number of checks removed */ |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 725 | void MIRGraph::CountChecks(class BasicBlock* bb) { |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 726 | if (bb->data_flow_info != NULL) { |
| 727 | for (MIR* mir = bb->first_mir_insn; mir != NULL; mir = mir->next) { |
| 728 | if (mir->ssa_rep == NULL) { |
| 729 | continue; |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 730 | } |
Jean Christophe Beyler | cc794c3 | 2014-05-02 09:34:13 -0700 | [diff] [blame] | 731 | uint64_t df_attributes = GetDataFlowAttributes(mir); |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 732 | if (df_attributes & DF_HAS_NULL_CHKS) { |
| 733 | checkstats_->null_checks++; |
| 734 | if (mir->optimization_flags & MIR_IGNORE_NULL_CHECK) { |
| 735 | checkstats_->null_checks_eliminated++; |
| 736 | } |
| 737 | } |
| 738 | if (df_attributes & DF_HAS_RANGE_CHKS) { |
| 739 | checkstats_->range_checks++; |
| 740 | if (mir->optimization_flags & MIR_IGNORE_RANGE_CHECK) { |
| 741 | checkstats_->range_checks_eliminated++; |
| 742 | } |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 743 | } |
| 744 | } |
| 745 | } |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 746 | } |
| 747 | |
Jean Christophe Beyler | 75bcc37 | 2014-09-04 08:15:11 -0700 | [diff] [blame] | 748 | /* Try to make common case the fallthrough path. */ |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 749 | bool MIRGraph::LayoutBlocks(BasicBlock* bb) { |
Jean Christophe Beyler | 75bcc37 | 2014-09-04 08:15:11 -0700 | [diff] [blame] | 750 | // TODO: For now, just looking for direct throws. Consider generalizing for profile feedback. |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 751 | if (!bb->explicit_throw) { |
| 752 | return false; |
| 753 | } |
Jean Christophe Beyler | 75bcc37 | 2014-09-04 08:15:11 -0700 | [diff] [blame] | 754 | |
| 755 | // If we visited it, we are done. |
| 756 | if (bb->visited) { |
| 757 | return false; |
| 758 | } |
| 759 | bb->visited = true; |
| 760 | |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 761 | BasicBlock* walker = bb; |
| 762 | while (true) { |
Jean Christophe Beyler | 75bcc37 | 2014-09-04 08:15:11 -0700 | [diff] [blame] | 763 | // Check termination conditions. |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 764 | if ((walker->block_type == kEntryBlock) || (Predecessors(walker) != 1)) { |
| 765 | break; |
| 766 | } |
Vladimir Marko | e39c54e | 2014-09-22 14:50:02 +0100 | [diff] [blame] | 767 | DCHECK(!walker->predecessors.empty()); |
| 768 | BasicBlock* prev = GetBasicBlock(walker->predecessors[0]); |
Jean Christophe Beyler | 75bcc37 | 2014-09-04 08:15:11 -0700 | [diff] [blame] | 769 | |
| 770 | // If we visited the predecessor, we are done. |
| 771 | if (prev->visited) { |
| 772 | return false; |
| 773 | } |
| 774 | prev->visited = true; |
| 775 | |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 776 | if (prev->conditional_branch) { |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 777 | if (GetBasicBlock(prev->fall_through) == walker) { |
Jean Christophe Beyler | 75bcc37 | 2014-09-04 08:15:11 -0700 | [diff] [blame] | 778 | // Already done - return. |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 779 | break; |
| 780 | } |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 781 | DCHECK_EQ(walker, GetBasicBlock(prev->taken)); |
Jean Christophe Beyler | 75bcc37 | 2014-09-04 08:15:11 -0700 | [diff] [blame] | 782 | // Got one. Flip it and exit. |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 783 | Instruction::Code opcode = prev->last_mir_insn->dalvikInsn.opcode; |
| 784 | switch (opcode) { |
| 785 | case Instruction::IF_EQ: opcode = Instruction::IF_NE; break; |
| 786 | case Instruction::IF_NE: opcode = Instruction::IF_EQ; break; |
| 787 | case Instruction::IF_LT: opcode = Instruction::IF_GE; break; |
| 788 | case Instruction::IF_GE: opcode = Instruction::IF_LT; break; |
| 789 | case Instruction::IF_GT: opcode = Instruction::IF_LE; break; |
| 790 | case Instruction::IF_LE: opcode = Instruction::IF_GT; break; |
| 791 | case Instruction::IF_EQZ: opcode = Instruction::IF_NEZ; break; |
| 792 | case Instruction::IF_NEZ: opcode = Instruction::IF_EQZ; break; |
| 793 | case Instruction::IF_LTZ: opcode = Instruction::IF_GEZ; break; |
| 794 | case Instruction::IF_GEZ: opcode = Instruction::IF_LTZ; break; |
| 795 | case Instruction::IF_GTZ: opcode = Instruction::IF_LEZ; break; |
| 796 | case Instruction::IF_LEZ: opcode = Instruction::IF_GTZ; break; |
| 797 | default: LOG(FATAL) << "Unexpected opcode " << opcode; |
| 798 | } |
| 799 | prev->last_mir_insn->dalvikInsn.opcode = opcode; |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 800 | BasicBlockId t_bb = prev->taken; |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 801 | prev->taken = prev->fall_through; |
| 802 | prev->fall_through = t_bb; |
| 803 | break; |
| 804 | } |
| 805 | walker = prev; |
| 806 | } |
| 807 | return false; |
| 808 | } |
| 809 | |
| 810 | /* Combine any basic blocks terminated by instructions that we now know can't throw */ |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 811 | void MIRGraph::CombineBlocks(class BasicBlock* bb) { |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 812 | // Loop here to allow combining a sequence of blocks |
Vladimir Marko | 312eb25 | 2014-10-07 15:01:57 +0100 | [diff] [blame] | 813 | while ((bb->block_type == kDalvikByteCode) && |
| 814 | (bb->last_mir_insn != nullptr) && |
| 815 | (static_cast<int>(bb->last_mir_insn->dalvikInsn.opcode) == kMirOpCheck)) { |
| 816 | MIR* mir = bb->last_mir_insn; |
| 817 | DCHECK(bb->first_mir_insn != nullptr); |
| 818 | |
Vladimir Marko | 315cc20 | 2014-12-18 17:01:02 +0000 | [diff] [blame] | 819 | // Get the paired insn and check if it can still throw. |
Vladimir Marko | 312eb25 | 2014-10-07 15:01:57 +0100 | [diff] [blame] | 820 | MIR* throw_insn = mir->meta.throw_insn; |
Vladimir Marko | 315cc20 | 2014-12-18 17:01:02 +0000 | [diff] [blame] | 821 | if (CanThrow(throw_insn)) { |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 822 | break; |
| 823 | } |
| 824 | |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 825 | // OK - got one. Combine |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 826 | BasicBlock* bb_next = GetBasicBlock(bb->fall_through); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 827 | DCHECK(!bb_next->catch_entry); |
Vladimir Marko | 312eb25 | 2014-10-07 15:01:57 +0100 | [diff] [blame] | 828 | DCHECK_EQ(bb_next->predecessors.size(), 1u); |
Razvan A Lupusoru | c7a77bf | 2014-10-29 18:42:27 -0700 | [diff] [blame] | 829 | |
| 830 | // Now move instructions from bb_next to bb. Start off with doing a sanity check |
| 831 | // that kMirOpCheck's throw instruction is first one in the bb_next. |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 832 | DCHECK_EQ(bb_next->first_mir_insn, throw_insn); |
Razvan A Lupusoru | c7a77bf | 2014-10-29 18:42:27 -0700 | [diff] [blame] | 833 | // Now move all instructions (throw instruction to last one) from bb_next to bb. |
| 834 | MIR* last_to_move = bb_next->last_mir_insn; |
| 835 | bb_next->RemoveMIRList(throw_insn, last_to_move); |
| 836 | bb->InsertMIRListAfter(bb->last_mir_insn, throw_insn, last_to_move); |
| 837 | // The kMirOpCheck instruction is not needed anymore. |
| 838 | mir->dalvikInsn.opcode = static_cast<Instruction::Code>(kMirOpNop); |
| 839 | bb->RemoveMIR(mir); |
| 840 | |
Vladimir Marko | 312eb25 | 2014-10-07 15:01:57 +0100 | [diff] [blame] | 841 | // Before we overwrite successors, remove their predecessor links to bb. |
| 842 | bb_next->ErasePredecessor(bb->id); |
| 843 | if (bb->taken != NullBasicBlockId) { |
| 844 | DCHECK_EQ(bb->successor_block_list_type, kNotUsed); |
| 845 | BasicBlock* bb_taken = GetBasicBlock(bb->taken); |
| 846 | // bb->taken will be overwritten below. |
| 847 | DCHECK_EQ(bb_taken->block_type, kExceptionHandling); |
| 848 | DCHECK_EQ(bb_taken->predecessors.size(), 1u); |
| 849 | DCHECK_EQ(bb_taken->predecessors[0], bb->id); |
| 850 | bb_taken->predecessors.clear(); |
| 851 | bb_taken->block_type = kDead; |
| 852 | DCHECK(bb_taken->data_flow_info == nullptr); |
| 853 | } else { |
| 854 | DCHECK_EQ(bb->successor_block_list_type, kCatch); |
| 855 | for (SuccessorBlockInfo* succ_info : bb->successor_blocks) { |
| 856 | if (succ_info->block != NullBasicBlockId) { |
| 857 | BasicBlock* succ_bb = GetBasicBlock(succ_info->block); |
| 858 | DCHECK(succ_bb->catch_entry); |
| 859 | succ_bb->ErasePredecessor(bb->id); |
Vladimir Marko | 312eb25 | 2014-10-07 15:01:57 +0100 | [diff] [blame] | 860 | } |
| 861 | } |
| 862 | } |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 863 | // Use the successor info from the next block |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 864 | bb->successor_block_list_type = bb_next->successor_block_list_type; |
Vladimir Marko | e39c54e | 2014-09-22 14:50:02 +0100 | [diff] [blame] | 865 | bb->successor_blocks.swap(bb_next->successor_blocks); // Swap instead of copying. |
Vladimir Marko | 312eb25 | 2014-10-07 15:01:57 +0100 | [diff] [blame] | 866 | bb_next->successor_block_list_type = kNotUsed; |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 867 | // Use the ending block linkage from the next block |
| 868 | bb->fall_through = bb_next->fall_through; |
Vladimir Marko | 312eb25 | 2014-10-07 15:01:57 +0100 | [diff] [blame] | 869 | bb_next->fall_through = NullBasicBlockId; |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 870 | bb->taken = bb_next->taken; |
Vladimir Marko | 312eb25 | 2014-10-07 15:01:57 +0100 | [diff] [blame] | 871 | bb_next->taken = NullBasicBlockId; |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 872 | /* |
Junmo Park | f1770fd | 2014-08-12 09:34:54 +0900 | [diff] [blame] | 873 | * If lower-half of pair of blocks to combine contained |
| 874 | * a return or a conditional branch or an explicit throw, |
| 875 | * move the flag to the newly combined block. |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 876 | */ |
| 877 | bb->terminated_by_return = bb_next->terminated_by_return; |
Junmo Park | f1770fd | 2014-08-12 09:34:54 +0900 | [diff] [blame] | 878 | bb->conditional_branch = bb_next->conditional_branch; |
| 879 | bb->explicit_throw = bb_next->explicit_throw; |
Vladimir Marko | 312eb25 | 2014-10-07 15:01:57 +0100 | [diff] [blame] | 880 | // Merge the use_lvn flag. |
| 881 | bb->use_lvn |= bb_next->use_lvn; |
| 882 | |
| 883 | // Kill the unused block. |
| 884 | bb_next->data_flow_info = nullptr; |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 885 | |
| 886 | /* |
| 887 | * NOTE: we aren't updating all dataflow info here. Should either make sure this pass |
| 888 | * happens after uses of i_dominated, dom_frontier or update the dataflow info here. |
Vladimir Marko | 312eb25 | 2014-10-07 15:01:57 +0100 | [diff] [blame] | 889 | * NOTE: GVN uses bb->data_flow_info->live_in_v which is unaffected by the block merge. |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 890 | */ |
| 891 | |
Vladimir Marko | 312eb25 | 2014-10-07 15:01:57 +0100 | [diff] [blame] | 892 | // Kill bb_next and remap now-dead id to parent. |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 893 | bb_next->block_type = kDead; |
Vladimir Marko | 312eb25 | 2014-10-07 15:01:57 +0100 | [diff] [blame] | 894 | bb_next->data_flow_info = nullptr; // Must be null for dead blocks. (Relied on by the GVN.) |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 895 | block_id_map_.Overwrite(bb_next->id, bb->id); |
Vladimir Marko | 312eb25 | 2014-10-07 15:01:57 +0100 | [diff] [blame] | 896 | // Update predecessors in children. |
| 897 | ChildBlockIterator iter(bb, this); |
| 898 | for (BasicBlock* child = iter.Next(); child != nullptr; child = iter.Next()) { |
| 899 | child->UpdatePredecessor(bb_next->id, bb->id); |
| 900 | } |
| 901 | |
Vladimir Marko | ffda499 | 2014-12-18 17:05:58 +0000 | [diff] [blame] | 902 | // DFS orders, domination and topological order are not up to date anymore. |
Vladimir Marko | 312eb25 | 2014-10-07 15:01:57 +0100 | [diff] [blame] | 903 | dfs_orders_up_to_date_ = false; |
Vladimir Marko | ffda499 | 2014-12-18 17:05:58 +0000 | [diff] [blame] | 904 | domination_up_to_date_ = false; |
| 905 | topological_order_up_to_date_ = false; |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 906 | |
| 907 | // Now, loop back and see if we can keep going |
| 908 | } |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 909 | } |
| 910 | |
Vladimir Marko | 67c72b8 | 2014-10-09 12:26:10 +0100 | [diff] [blame] | 911 | bool MIRGraph::EliminateNullChecksGate() { |
| 912 | if ((cu_->disable_opt & (1 << kNullCheckElimination)) != 0 || |
| 913 | (merged_df_flags_ & DF_HAS_NULL_CHKS) == 0) { |
| 914 | return false; |
Vladimir Marko | bfea9c2 | 2014-01-17 17:49:33 +0000 | [diff] [blame] | 915 | } |
Vladimir Marko | 67c72b8 | 2014-10-09 12:26:10 +0100 | [diff] [blame] | 916 | |
Vladimir Marko | 67c72b8 | 2014-10-09 12:26:10 +0100 | [diff] [blame] | 917 | DCHECK(temp_scoped_alloc_.get() == nullptr); |
| 918 | temp_scoped_alloc_.reset(ScopedArenaAllocator::Create(&cu_->arena_stack)); |
Razvan A Lupusoru | c7a77bf | 2014-10-29 18:42:27 -0700 | [diff] [blame] | 919 | temp_.nce.num_vregs = GetNumOfCodeAndTempVRs(); |
Vladimir Marko | f585e54 | 2014-11-21 13:41:32 +0000 | [diff] [blame] | 920 | temp_.nce.work_vregs_to_check = new (temp_scoped_alloc_.get()) ArenaBitVector( |
| 921 | temp_scoped_alloc_.get(), temp_.nce.num_vregs, false, kBitMapNullCheck); |
Vladimir Marko | e4fcc5b | 2015-02-13 10:28:29 +0000 | [diff] [blame] | 922 | temp_.nce.ending_vregs_to_check_matrix = |
| 923 | temp_scoped_alloc_->AllocArray<ArenaBitVector*>(GetNumBlocks(), kArenaAllocMisc); |
Vladimir Marko | f585e54 | 2014-11-21 13:41:32 +0000 | [diff] [blame] | 924 | std::fill_n(temp_.nce.ending_vregs_to_check_matrix, GetNumBlocks(), nullptr); |
Yevgeny Rouban | 423b137 | 2014-10-15 17:32:25 +0700 | [diff] [blame] | 925 | |
| 926 | // reset MIR_MARK |
| 927 | AllNodesIterator iter(this); |
| 928 | for (BasicBlock* bb = iter.Next(); bb != nullptr; bb = iter.Next()) { |
| 929 | for (MIR* mir = bb->first_mir_insn; mir != NULL; mir = mir->next) { |
| 930 | mir->optimization_flags &= ~MIR_MARK; |
| 931 | } |
| 932 | } |
| 933 | |
Vladimir Marko | 67c72b8 | 2014-10-09 12:26:10 +0100 | [diff] [blame] | 934 | return true; |
Vladimir Marko | bfea9c2 | 2014-01-17 17:49:33 +0000 | [diff] [blame] | 935 | } |
| 936 | |
buzbee | 1da1e2f | 2013-11-15 13:37:01 -0800 | [diff] [blame] | 937 | /* |
Vladimir Marko | 67c72b8 | 2014-10-09 12:26:10 +0100 | [diff] [blame] | 938 | * Eliminate unnecessary null checks for a basic block. |
buzbee | 1da1e2f | 2013-11-15 13:37:01 -0800 | [diff] [blame] | 939 | */ |
Vladimir Marko | 67c72b8 | 2014-10-09 12:26:10 +0100 | [diff] [blame] | 940 | bool MIRGraph::EliminateNullChecks(BasicBlock* bb) { |
Vladimir Marko | 7baa6f8 | 2014-10-09 18:01:24 +0100 | [diff] [blame] | 941 | if (bb->block_type != kDalvikByteCode && bb->block_type != kEntryBlock) { |
| 942 | // Ignore the kExitBlock as well. |
| 943 | DCHECK(bb->first_mir_insn == nullptr); |
| 944 | return false; |
| 945 | } |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 946 | |
Vladimir Marko | f585e54 | 2014-11-21 13:41:32 +0000 | [diff] [blame] | 947 | ArenaBitVector* vregs_to_check = temp_.nce.work_vregs_to_check; |
Vladimir Marko | 67c72b8 | 2014-10-09 12:26:10 +0100 | [diff] [blame] | 948 | /* |
| 949 | * Set initial state. Catch blocks don't need any special treatment. |
| 950 | */ |
| 951 | if (bb->block_type == kEntryBlock) { |
Vladimir Marko | 7baa6f8 | 2014-10-09 18:01:24 +0100 | [diff] [blame] | 952 | vregs_to_check->ClearAllBits(); |
Vladimir Marko | 67c72b8 | 2014-10-09 12:26:10 +0100 | [diff] [blame] | 953 | // Assume all ins are objects. |
| 954 | for (uint16_t in_reg = GetFirstInVR(); |
| 955 | in_reg < GetNumOfCodeVRs(); in_reg++) { |
Vladimir Marko | 7baa6f8 | 2014-10-09 18:01:24 +0100 | [diff] [blame] | 956 | vregs_to_check->SetBit(in_reg); |
Vladimir Marko | 67c72b8 | 2014-10-09 12:26:10 +0100 | [diff] [blame] | 957 | } |
| 958 | if ((cu_->access_flags & kAccStatic) == 0) { |
Vladimir Marko | 7baa6f8 | 2014-10-09 18:01:24 +0100 | [diff] [blame] | 959 | // If non-static method, mark "this" as non-null. |
Vladimir Marko | 67c72b8 | 2014-10-09 12:26:10 +0100 | [diff] [blame] | 960 | int this_reg = GetFirstInVR(); |
Vladimir Marko | 7baa6f8 | 2014-10-09 18:01:24 +0100 | [diff] [blame] | 961 | vregs_to_check->ClearBit(this_reg); |
Vladimir Marko | 67c72b8 | 2014-10-09 12:26:10 +0100 | [diff] [blame] | 962 | } |
Vladimir Marko | 7baa6f8 | 2014-10-09 18:01:24 +0100 | [diff] [blame] | 963 | } else { |
| 964 | DCHECK_EQ(bb->block_type, kDalvikByteCode); |
| 965 | // Starting state is union of all incoming arcs. |
| 966 | bool copied_first = false; |
| 967 | for (BasicBlockId pred_id : bb->predecessors) { |
Vladimir Marko | f585e54 | 2014-11-21 13:41:32 +0000 | [diff] [blame] | 968 | if (temp_.nce.ending_vregs_to_check_matrix[pred_id] == nullptr) { |
Vladimir Marko | 7baa6f8 | 2014-10-09 18:01:24 +0100 | [diff] [blame] | 969 | continue; |
| 970 | } |
| 971 | BasicBlock* pred_bb = GetBasicBlock(pred_id); |
| 972 | DCHECK(pred_bb != nullptr); |
| 973 | MIR* null_check_insn = nullptr; |
| 974 | if (pred_bb->block_type == kDalvikByteCode) { |
| 975 | // Check to see if predecessor had an explicit null-check. |
| 976 | MIR* last_insn = pred_bb->last_mir_insn; |
| 977 | if (last_insn != nullptr) { |
| 978 | Instruction::Code last_opcode = last_insn->dalvikInsn.opcode; |
| 979 | if ((last_opcode == Instruction::IF_EQZ && pred_bb->fall_through == bb->id) || |
| 980 | (last_opcode == Instruction::IF_NEZ && pred_bb->taken == bb->id)) { |
| 981 | // Remember the null check insn if there's no other predecessor requiring null check. |
| 982 | if (!copied_first || !vregs_to_check->IsBitSet(last_insn->dalvikInsn.vA)) { |
| 983 | null_check_insn = last_insn; |
| 984 | } |
buzbee | 1da1e2f | 2013-11-15 13:37:01 -0800 | [diff] [blame] | 985 | } |
Ian Rogers | 22fd6a0 | 2013-06-13 15:06:54 -0700 | [diff] [blame] | 986 | } |
| 987 | } |
Vladimir Marko | 67c72b8 | 2014-10-09 12:26:10 +0100 | [diff] [blame] | 988 | if (!copied_first) { |
| 989 | copied_first = true; |
Vladimir Marko | f585e54 | 2014-11-21 13:41:32 +0000 | [diff] [blame] | 990 | vregs_to_check->Copy(temp_.nce.ending_vregs_to_check_matrix[pred_id]); |
Vladimir Marko | 67c72b8 | 2014-10-09 12:26:10 +0100 | [diff] [blame] | 991 | } else { |
Vladimir Marko | f585e54 | 2014-11-21 13:41:32 +0000 | [diff] [blame] | 992 | vregs_to_check->Union(temp_.nce.ending_vregs_to_check_matrix[pred_id]); |
Vladimir Marko | 7baa6f8 | 2014-10-09 18:01:24 +0100 | [diff] [blame] | 993 | } |
| 994 | if (null_check_insn != nullptr) { |
| 995 | vregs_to_check->ClearBit(null_check_insn->dalvikInsn.vA); |
Vladimir Marko | 67c72b8 | 2014-10-09 12:26:10 +0100 | [diff] [blame] | 996 | } |
| 997 | } |
| 998 | DCHECK(copied_first); // At least one predecessor must have been processed before this bb. |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 999 | } |
Vladimir Marko | 7baa6f8 | 2014-10-09 18:01:24 +0100 | [diff] [blame] | 1000 | // At this point, vregs_to_check shows which sregs have an object definition with |
Vladimir Marko | 67c72b8 | 2014-10-09 12:26:10 +0100 | [diff] [blame] | 1001 | // no intervening uses. |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 1002 | |
| 1003 | // Walk through the instruction in the block, updating as necessary |
| 1004 | for (MIR* mir = bb->first_mir_insn; mir != NULL; mir = mir->next) { |
Jean Christophe Beyler | cc794c3 | 2014-05-02 09:34:13 -0700 | [diff] [blame] | 1005 | uint64_t df_attributes = GetDataFlowAttributes(mir); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 1006 | |
Razvan A Lupusoru | c7a77bf | 2014-10-29 18:42:27 -0700 | [diff] [blame] | 1007 | if ((df_attributes & DF_NULL_TRANSFER_N) != 0u) { |
| 1008 | // The algorithm was written in a phi agnostic way. |
| 1009 | continue; |
| 1010 | } |
Vladimir Marko | 7baa6f8 | 2014-10-09 18:01:24 +0100 | [diff] [blame] | 1011 | |
Bill Buzbee | 0b1191c | 2013-10-28 22:11:59 +0000 | [diff] [blame] | 1012 | // Might need a null check? |
| 1013 | if (df_attributes & DF_HAS_NULL_CHKS) { |
Vladimir Marko | 7baa6f8 | 2014-10-09 18:01:24 +0100 | [diff] [blame] | 1014 | int src_vreg; |
| 1015 | if (df_attributes & DF_NULL_CHK_OUT0) { |
| 1016 | DCHECK_NE(df_attributes & DF_IS_INVOKE, 0u); |
| 1017 | src_vreg = mir->dalvikInsn.vC; |
| 1018 | } else if (df_attributes & DF_NULL_CHK_B) { |
| 1019 | DCHECK_NE(df_attributes & DF_REF_B, 0u); |
| 1020 | src_vreg = mir->dalvikInsn.vB; |
Bill Buzbee | 0b1191c | 2013-10-28 22:11:59 +0000 | [diff] [blame] | 1021 | } else { |
Vladimir Marko | 7baa6f8 | 2014-10-09 18:01:24 +0100 | [diff] [blame] | 1022 | DCHECK_NE(df_attributes & DF_NULL_CHK_A, 0u); |
| 1023 | DCHECK_NE(df_attributes & DF_REF_A, 0u); |
| 1024 | src_vreg = mir->dalvikInsn.vA; |
Bill Buzbee | 0b1191c | 2013-10-28 22:11:59 +0000 | [diff] [blame] | 1025 | } |
Vladimir Marko | 7baa6f8 | 2014-10-09 18:01:24 +0100 | [diff] [blame] | 1026 | if (!vregs_to_check->IsBitSet(src_vreg)) { |
Bill Buzbee | 0b1191c | 2013-10-28 22:11:59 +0000 | [diff] [blame] | 1027 | // Eliminate the null check. |
Yevgeny Rouban | 423b137 | 2014-10-15 17:32:25 +0700 | [diff] [blame] | 1028 | mir->optimization_flags |= MIR_MARK; |
Bill Buzbee | 0b1191c | 2013-10-28 22:11:59 +0000 | [diff] [blame] | 1029 | } else { |
| 1030 | // Do the null check. |
Yevgeny Rouban | 423b137 | 2014-10-15 17:32:25 +0700 | [diff] [blame] | 1031 | mir->optimization_flags &= ~MIR_MARK; |
Vladimir Marko | 7baa6f8 | 2014-10-09 18:01:24 +0100 | [diff] [blame] | 1032 | // Mark src_vreg as null-checked. |
| 1033 | vregs_to_check->ClearBit(src_vreg); |
Bill Buzbee | 0b1191c | 2013-10-28 22:11:59 +0000 | [diff] [blame] | 1034 | } |
| 1035 | } |
| 1036 | |
| 1037 | if ((df_attributes & DF_A_WIDE) || |
| 1038 | (df_attributes & (DF_REF_A | DF_SETS_CONST | DF_NULL_TRANSFER)) == 0) { |
| 1039 | continue; |
| 1040 | } |
| 1041 | |
| 1042 | /* |
| 1043 | * First, mark all object definitions as requiring null check. |
| 1044 | * Note: we can't tell if a CONST definition might be used as an object, so treat |
| 1045 | * them all as object definitions. |
| 1046 | */ |
Vladimir Marko | 7baa6f8 | 2014-10-09 18:01:24 +0100 | [diff] [blame] | 1047 | if ((df_attributes & (DF_DA | DF_REF_A)) == (DF_DA | DF_REF_A) || |
Bill Buzbee | 0b1191c | 2013-10-28 22:11:59 +0000 | [diff] [blame] | 1048 | (df_attributes & DF_SETS_CONST)) { |
Vladimir Marko | 7baa6f8 | 2014-10-09 18:01:24 +0100 | [diff] [blame] | 1049 | vregs_to_check->SetBit(mir->dalvikInsn.vA); |
buzbee | 4db179d | 2013-10-23 12:16:39 -0700 | [diff] [blame] | 1050 | } |
| 1051 | |
Vladimir Marko | 7baa6f8 | 2014-10-09 18:01:24 +0100 | [diff] [blame] | 1052 | // Then, remove mark from all object definitions we know are non-null. |
Bill Buzbee | 0b1191c | 2013-10-28 22:11:59 +0000 | [diff] [blame] | 1053 | if (df_attributes & DF_NON_NULL_DST) { |
| 1054 | // Mark target of NEW* as non-null |
Vladimir Marko | 7baa6f8 | 2014-10-09 18:01:24 +0100 | [diff] [blame] | 1055 | DCHECK_NE(df_attributes & DF_REF_A, 0u); |
| 1056 | vregs_to_check->ClearBit(mir->dalvikInsn.vA); |
Bill Buzbee | 0b1191c | 2013-10-28 22:11:59 +0000 | [diff] [blame] | 1057 | } |
| 1058 | |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 1059 | // Mark non-null returns from invoke-style NEW* |
| 1060 | if (df_attributes & DF_NON_NULL_RET) { |
| 1061 | MIR* next_mir = mir->next; |
| 1062 | // Next should be an MOVE_RESULT_OBJECT |
Vladimir Marko | 7baa6f8 | 2014-10-09 18:01:24 +0100 | [diff] [blame] | 1063 | if (UNLIKELY(next_mir == nullptr)) { |
| 1064 | // The MethodVerifier makes sure there's no MOVE_RESULT at the catch entry or branch |
| 1065 | // target, so the MOVE_RESULT cannot be broken away into another block. |
| 1066 | LOG(WARNING) << "Unexpected end of block following new"; |
| 1067 | } else if (UNLIKELY(next_mir->dalvikInsn.opcode != Instruction::MOVE_RESULT_OBJECT)) { |
| 1068 | LOG(WARNING) << "Unexpected opcode following new: " << next_mir->dalvikInsn.opcode; |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 1069 | } else { |
Vladimir Marko | 7baa6f8 | 2014-10-09 18:01:24 +0100 | [diff] [blame] | 1070 | // Mark as null checked. |
| 1071 | vregs_to_check->ClearBit(next_mir->dalvikInsn.vA); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 1072 | } |
| 1073 | } |
| 1074 | |
Vladimir Marko | 7baa6f8 | 2014-10-09 18:01:24 +0100 | [diff] [blame] | 1075 | // Propagate null check state on register copies. |
| 1076 | if (df_attributes & DF_NULL_TRANSFER_0) { |
| 1077 | DCHECK_EQ(df_attributes | ~(DF_DA | DF_REF_A | DF_UB | DF_REF_B), static_cast<uint64_t>(-1)); |
| 1078 | if (vregs_to_check->IsBitSet(mir->dalvikInsn.vB)) { |
| 1079 | vregs_to_check->SetBit(mir->dalvikInsn.vA); |
Bill Buzbee | 0b1191c | 2013-10-28 22:11:59 +0000 | [diff] [blame] | 1080 | } else { |
Vladimir Marko | 7baa6f8 | 2014-10-09 18:01:24 +0100 | [diff] [blame] | 1081 | vregs_to_check->ClearBit(mir->dalvikInsn.vA); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 1082 | } |
| 1083 | } |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 1084 | } |
| 1085 | |
| 1086 | // Did anything change? |
Vladimir Marko | bfea9c2 | 2014-01-17 17:49:33 +0000 | [diff] [blame] | 1087 | bool nce_changed = false; |
Vladimir Marko | f585e54 | 2014-11-21 13:41:32 +0000 | [diff] [blame] | 1088 | ArenaBitVector* old_ending_ssa_regs_to_check = temp_.nce.ending_vregs_to_check_matrix[bb->id]; |
Vladimir Marko | 5229cf1 | 2014-10-09 14:57:59 +0100 | [diff] [blame] | 1089 | if (old_ending_ssa_regs_to_check == nullptr) { |
Vladimir Marko | 67c72b8 | 2014-10-09 12:26:10 +0100 | [diff] [blame] | 1090 | DCHECK(temp_scoped_alloc_.get() != nullptr); |
Vladimir Marko | 7baa6f8 | 2014-10-09 18:01:24 +0100 | [diff] [blame] | 1091 | nce_changed = vregs_to_check->GetHighestBitSet() != -1; |
Vladimir Marko | f585e54 | 2014-11-21 13:41:32 +0000 | [diff] [blame] | 1092 | temp_.nce.ending_vregs_to_check_matrix[bb->id] = vregs_to_check; |
Vladimir Marko | 7baa6f8 | 2014-10-09 18:01:24 +0100 | [diff] [blame] | 1093 | // Create a new vregs_to_check for next BB. |
Vladimir Marko | f585e54 | 2014-11-21 13:41:32 +0000 | [diff] [blame] | 1094 | temp_.nce.work_vregs_to_check = new (temp_scoped_alloc_.get()) ArenaBitVector( |
| 1095 | temp_scoped_alloc_.get(), temp_.nce.num_vregs, false, kBitMapNullCheck); |
Vladimir Marko | 7baa6f8 | 2014-10-09 18:01:24 +0100 | [diff] [blame] | 1096 | } else if (!vregs_to_check->SameBitsSet(old_ending_ssa_regs_to_check)) { |
Vladimir Marko | 67c72b8 | 2014-10-09 12:26:10 +0100 | [diff] [blame] | 1097 | nce_changed = true; |
Vladimir Marko | f585e54 | 2014-11-21 13:41:32 +0000 | [diff] [blame] | 1098 | temp_.nce.ending_vregs_to_check_matrix[bb->id] = vregs_to_check; |
| 1099 | temp_.nce.work_vregs_to_check = old_ending_ssa_regs_to_check; // Reuse for next BB. |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 1100 | } |
Vladimir Marko | 67c72b8 | 2014-10-09 12:26:10 +0100 | [diff] [blame] | 1101 | return nce_changed; |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 1102 | } |
| 1103 | |
Vladimir Marko | 67c72b8 | 2014-10-09 12:26:10 +0100 | [diff] [blame] | 1104 | void MIRGraph::EliminateNullChecksEnd() { |
| 1105 | // Clean up temporaries. |
Vladimir Marko | f585e54 | 2014-11-21 13:41:32 +0000 | [diff] [blame] | 1106 | temp_.nce.num_vregs = 0u; |
| 1107 | temp_.nce.work_vregs_to_check = nullptr; |
| 1108 | temp_.nce.ending_vregs_to_check_matrix = nullptr; |
Vladimir Marko | 67c72b8 | 2014-10-09 12:26:10 +0100 | [diff] [blame] | 1109 | DCHECK(temp_scoped_alloc_.get() != nullptr); |
| 1110 | temp_scoped_alloc_.reset(); |
Yevgeny Rouban | 423b137 | 2014-10-15 17:32:25 +0700 | [diff] [blame] | 1111 | |
| 1112 | // converge MIR_MARK with MIR_IGNORE_NULL_CHECK |
Yevgeny Rouban | 423b137 | 2014-10-15 17:32:25 +0700 | [diff] [blame] | 1113 | AllNodesIterator iter(this); |
| 1114 | for (BasicBlock* bb = iter.Next(); bb != nullptr; bb = iter.Next()) { |
| 1115 | for (MIR* mir = bb->first_mir_insn; mir != NULL; mir = mir->next) { |
Vladimir Marko | 7baa6f8 | 2014-10-09 18:01:24 +0100 | [diff] [blame] | 1116 | constexpr int kMarkToIgnoreNullCheckShift = kMIRMark - kMIRIgnoreNullCheck; |
Andreas Gampe | 785d2f2 | 2014-11-03 22:57:30 -0800 | [diff] [blame] | 1117 | static_assert(kMarkToIgnoreNullCheckShift > 0, "Not a valid right-shift"); |
Yevgeny Rouban | 423b137 | 2014-10-15 17:32:25 +0700 | [diff] [blame] | 1118 | uint16_t mirMarkAdjustedToIgnoreNullCheck = |
Vladimir Marko | 7baa6f8 | 2014-10-09 18:01:24 +0100 | [diff] [blame] | 1119 | (mir->optimization_flags & MIR_MARK) >> kMarkToIgnoreNullCheckShift; |
Yevgeny Rouban | 423b137 | 2014-10-15 17:32:25 +0700 | [diff] [blame] | 1120 | mir->optimization_flags |= mirMarkAdjustedToIgnoreNullCheck; |
| 1121 | } |
| 1122 | } |
Vladimir Marko | 67c72b8 | 2014-10-09 12:26:10 +0100 | [diff] [blame] | 1123 | } |
| 1124 | |
Vladimir Marko | e490b01 | 2015-02-24 11:32:46 +0000 | [diff] [blame] | 1125 | void MIRGraph::InferTypesStart() { |
| 1126 | DCHECK(temp_scoped_alloc_ != nullptr); |
| 1127 | temp_.ssa.ti = new (temp_scoped_alloc_.get()) TypeInference(this, temp_scoped_alloc_.get()); |
| 1128 | } |
| 1129 | |
Vladimir Marko | 67c72b8 | 2014-10-09 12:26:10 +0100 | [diff] [blame] | 1130 | /* |
| 1131 | * Perform type and size inference for a basic block. |
| 1132 | */ |
| 1133 | bool MIRGraph::InferTypes(BasicBlock* bb) { |
| 1134 | if (bb->data_flow_info == nullptr) return false; |
| 1135 | |
Vladimir Marko | e490b01 | 2015-02-24 11:32:46 +0000 | [diff] [blame] | 1136 | DCHECK(temp_.ssa.ti != nullptr); |
| 1137 | return temp_.ssa.ti->Apply(bb); |
| 1138 | } |
Vladimir Marko | 67c72b8 | 2014-10-09 12:26:10 +0100 | [diff] [blame] | 1139 | |
Vladimir Marko | e490b01 | 2015-02-24 11:32:46 +0000 | [diff] [blame] | 1140 | void MIRGraph::InferTypesEnd() { |
| 1141 | DCHECK(temp_.ssa.ti != nullptr); |
| 1142 | temp_.ssa.ti->Finish(); |
| 1143 | delete temp_.ssa.ti; |
| 1144 | temp_.ssa.ti = nullptr; |
Vladimir Marko | bfea9c2 | 2014-01-17 17:49:33 +0000 | [diff] [blame] | 1145 | } |
| 1146 | |
| 1147 | bool MIRGraph::EliminateClassInitChecksGate() { |
| 1148 | if ((cu_->disable_opt & (1 << kClassInitCheckElimination)) != 0 || |
Vladimir Marko | 66c6d7b | 2014-10-16 15:41:48 +0100 | [diff] [blame] | 1149 | (merged_df_flags_ & DF_CLINIT) == 0) { |
Vladimir Marko | bfea9c2 | 2014-01-17 17:49:33 +0000 | [diff] [blame] | 1150 | return false; |
| 1151 | } |
| 1152 | |
Vladimir Marko | bfea9c2 | 2014-01-17 17:49:33 +0000 | [diff] [blame] | 1153 | DCHECK(temp_scoped_alloc_.get() == nullptr); |
| 1154 | temp_scoped_alloc_.reset(ScopedArenaAllocator::Create(&cu_->arena_stack)); |
| 1155 | |
| 1156 | // Each insn we use here has at least 2 code units, offset/2 will be a unique index. |
Razvan A Lupusoru | 7503597 | 2014-09-11 15:24:59 -0700 | [diff] [blame] | 1157 | const size_t end = (GetNumDalvikInsns() + 1u) / 2u; |
Vladimir Marko | e4fcc5b | 2015-02-13 10:28:29 +0000 | [diff] [blame] | 1158 | temp_.cice.indexes = temp_scoped_alloc_->AllocArray<uint16_t>(end, kArenaAllocGrowableArray); |
Vladimir Marko | f585e54 | 2014-11-21 13:41:32 +0000 | [diff] [blame] | 1159 | std::fill_n(temp_.cice.indexes, end, 0xffffu); |
Vladimir Marko | bfea9c2 | 2014-01-17 17:49:33 +0000 | [diff] [blame] | 1160 | |
| 1161 | uint32_t unique_class_count = 0u; |
| 1162 | { |
| 1163 | // Get unique_class_count and store indexes in temp_insn_data_ using a map on a nested |
| 1164 | // ScopedArenaAllocator. |
| 1165 | |
| 1166 | // Embed the map value in the entry to save space. |
| 1167 | struct MapEntry { |
| 1168 | // Map key: the class identified by the declaring dex file and type index. |
| 1169 | const DexFile* declaring_dex_file; |
| 1170 | uint16_t declaring_class_idx; |
| 1171 | // Map value: index into bit vectors of classes requiring initialization checks. |
| 1172 | uint16_t index; |
| 1173 | }; |
| 1174 | struct MapEntryComparator { |
| 1175 | bool operator()(const MapEntry& lhs, const MapEntry& rhs) const { |
| 1176 | if (lhs.declaring_class_idx != rhs.declaring_class_idx) { |
| 1177 | return lhs.declaring_class_idx < rhs.declaring_class_idx; |
| 1178 | } |
| 1179 | return lhs.declaring_dex_file < rhs.declaring_dex_file; |
| 1180 | } |
| 1181 | }; |
| 1182 | |
Vladimir Marko | bfea9c2 | 2014-01-17 17:49:33 +0000 | [diff] [blame] | 1183 | ScopedArenaAllocator allocator(&cu_->arena_stack); |
Vladimir Marko | 69f08ba | 2014-04-11 12:28:11 +0100 | [diff] [blame] | 1184 | ScopedArenaSet<MapEntry, MapEntryComparator> class_to_index_map(MapEntryComparator(), |
| 1185 | allocator.Adapter()); |
Vladimir Marko | bfea9c2 | 2014-01-17 17:49:33 +0000 | [diff] [blame] | 1186 | |
| 1187 | // First, find all SGET/SPUTs that may need class initialization checks, record INVOKE_STATICs. |
| 1188 | AllNodesIterator iter(this); |
| 1189 | for (BasicBlock* bb = iter.Next(); bb != nullptr; bb = iter.Next()) { |
Vladimir Marko | 7baa6f8 | 2014-10-09 18:01:24 +0100 | [diff] [blame] | 1190 | if (bb->block_type == kDalvikByteCode) { |
| 1191 | for (MIR* mir = bb->first_mir_insn; mir != nullptr; mir = mir->next) { |
Vladimir Marko | af6925b | 2014-10-31 16:37:32 +0000 | [diff] [blame] | 1192 | if (IsInstructionSGetOrSPut(mir->dalvikInsn.opcode)) { |
Vladimir Marko | 7baa6f8 | 2014-10-09 18:01:24 +0100 | [diff] [blame] | 1193 | const MirSFieldLoweringInfo& field_info = GetSFieldLoweringInfo(mir); |
Vladimir Marko | 66c6d7b | 2014-10-16 15:41:48 +0100 | [diff] [blame] | 1194 | if (!field_info.IsReferrersClass()) { |
Vladimir Marko | 7baa6f8 | 2014-10-09 18:01:24 +0100 | [diff] [blame] | 1195 | DCHECK_LT(class_to_index_map.size(), 0xffffu); |
| 1196 | MapEntry entry = { |
| 1197 | // Treat unresolved fields as if each had its own class. |
| 1198 | field_info.IsResolved() ? field_info.DeclaringDexFile() |
| 1199 | : nullptr, |
| 1200 | field_info.IsResolved() ? field_info.DeclaringClassIndex() |
| 1201 | : field_info.FieldIndex(), |
| 1202 | static_cast<uint16_t>(class_to_index_map.size()) |
| 1203 | }; |
Vladimir Marko | 66c6d7b | 2014-10-16 15:41:48 +0100 | [diff] [blame] | 1204 | uint16_t index = class_to_index_map.insert(entry).first->index; |
Vladimir Marko | f585e54 | 2014-11-21 13:41:32 +0000 | [diff] [blame] | 1205 | // Using offset/2 for index into temp_.cice.indexes. |
| 1206 | temp_.cice.indexes[mir->offset / 2u] = index; |
Vladimir Marko | 7baa6f8 | 2014-10-09 18:01:24 +0100 | [diff] [blame] | 1207 | } |
Vladimir Marko | af6925b | 2014-10-31 16:37:32 +0000 | [diff] [blame] | 1208 | } else if (IsInstructionInvokeStatic(mir->dalvikInsn.opcode)) { |
Vladimir Marko | 66c6d7b | 2014-10-16 15:41:48 +0100 | [diff] [blame] | 1209 | const MirMethodLoweringInfo& method_info = GetMethodLoweringInfo(mir); |
| 1210 | DCHECK(method_info.IsStatic()); |
| 1211 | if (method_info.FastPath() && !method_info.IsReferrersClass()) { |
| 1212 | MapEntry entry = { |
| 1213 | method_info.DeclaringDexFile(), |
| 1214 | method_info.DeclaringClassIndex(), |
| 1215 | static_cast<uint16_t>(class_to_index_map.size()) |
| 1216 | }; |
| 1217 | uint16_t index = class_to_index_map.insert(entry).first->index; |
Vladimir Marko | f585e54 | 2014-11-21 13:41:32 +0000 | [diff] [blame] | 1218 | // Using offset/2 for index into temp_.cice.indexes. |
| 1219 | temp_.cice.indexes[mir->offset / 2u] = index; |
Vladimir Marko | 66c6d7b | 2014-10-16 15:41:48 +0100 | [diff] [blame] | 1220 | } |
Vladimir Marko | bfea9c2 | 2014-01-17 17:49:33 +0000 | [diff] [blame] | 1221 | } |
Vladimir Marko | bfea9c2 | 2014-01-17 17:49:33 +0000 | [diff] [blame] | 1222 | } |
| 1223 | } |
| 1224 | } |
| 1225 | unique_class_count = static_cast<uint32_t>(class_to_index_map.size()); |
| 1226 | } |
| 1227 | |
| 1228 | if (unique_class_count == 0u) { |
| 1229 | // All SGET/SPUTs refer to initialized classes. Nothing to do. |
Vladimir Marko | f585e54 | 2014-11-21 13:41:32 +0000 | [diff] [blame] | 1230 | temp_.cice.indexes = nullptr; |
Vladimir Marko | bfea9c2 | 2014-01-17 17:49:33 +0000 | [diff] [blame] | 1231 | temp_scoped_alloc_.reset(); |
| 1232 | return false; |
| 1233 | } |
| 1234 | |
Vladimir Marko | 66c6d7b | 2014-10-16 15:41:48 +0100 | [diff] [blame] | 1235 | // 2 bits for each class: is class initialized, is class in dex cache. |
Vladimir Marko | f585e54 | 2014-11-21 13:41:32 +0000 | [diff] [blame] | 1236 | temp_.cice.num_class_bits = 2u * unique_class_count; |
| 1237 | temp_.cice.work_classes_to_check = new (temp_scoped_alloc_.get()) ArenaBitVector( |
| 1238 | temp_scoped_alloc_.get(), temp_.cice.num_class_bits, false, kBitMapClInitCheck); |
Vladimir Marko | e4fcc5b | 2015-02-13 10:28:29 +0000 | [diff] [blame] | 1239 | temp_.cice.ending_classes_to_check_matrix = |
| 1240 | temp_scoped_alloc_->AllocArray<ArenaBitVector*>(GetNumBlocks(), kArenaAllocMisc); |
Vladimir Marko | f585e54 | 2014-11-21 13:41:32 +0000 | [diff] [blame] | 1241 | std::fill_n(temp_.cice.ending_classes_to_check_matrix, GetNumBlocks(), nullptr); |
| 1242 | DCHECK_GT(temp_.cice.num_class_bits, 0u); |
Vladimir Marko | bfea9c2 | 2014-01-17 17:49:33 +0000 | [diff] [blame] | 1243 | return true; |
| 1244 | } |
| 1245 | |
| 1246 | /* |
| 1247 | * Eliminate unnecessary class initialization checks for a basic block. |
| 1248 | */ |
| 1249 | bool MIRGraph::EliminateClassInitChecks(BasicBlock* bb) { |
| 1250 | DCHECK_EQ((cu_->disable_opt & (1 << kClassInitCheckElimination)), 0u); |
Vladimir Marko | 7baa6f8 | 2014-10-09 18:01:24 +0100 | [diff] [blame] | 1251 | if (bb->block_type != kDalvikByteCode && bb->block_type != kEntryBlock) { |
| 1252 | // Ignore the kExitBlock as well. |
| 1253 | DCHECK(bb->first_mir_insn == nullptr); |
Vladimir Marko | bfea9c2 | 2014-01-17 17:49:33 +0000 | [diff] [blame] | 1254 | return false; |
| 1255 | } |
| 1256 | |
| 1257 | /* |
Vladimir Marko | 0a810d2 | 2014-07-11 14:44:36 +0100 | [diff] [blame] | 1258 | * Set initial state. Catch blocks don't need any special treatment. |
Vladimir Marko | bfea9c2 | 2014-01-17 17:49:33 +0000 | [diff] [blame] | 1259 | */ |
Vladimir Marko | f585e54 | 2014-11-21 13:41:32 +0000 | [diff] [blame] | 1260 | ArenaBitVector* classes_to_check = temp_.cice.work_classes_to_check; |
Vladimir Marko | bfea9c2 | 2014-01-17 17:49:33 +0000 | [diff] [blame] | 1261 | DCHECK(classes_to_check != nullptr); |
Vladimir Marko | 0a810d2 | 2014-07-11 14:44:36 +0100 | [diff] [blame] | 1262 | if (bb->block_type == kEntryBlock) { |
Vladimir Marko | f585e54 | 2014-11-21 13:41:32 +0000 | [diff] [blame] | 1263 | classes_to_check->SetInitialBits(temp_.cice.num_class_bits); |
Vladimir Marko | bfea9c2 | 2014-01-17 17:49:33 +0000 | [diff] [blame] | 1264 | } else { |
Vladimir Marko | e39c54e | 2014-09-22 14:50:02 +0100 | [diff] [blame] | 1265 | // Starting state is union of all incoming arcs. |
| 1266 | bool copied_first = false; |
| 1267 | for (BasicBlockId pred_id : bb->predecessors) { |
Vladimir Marko | f585e54 | 2014-11-21 13:41:32 +0000 | [diff] [blame] | 1268 | if (temp_.cice.ending_classes_to_check_matrix[pred_id] == nullptr) { |
Vladimir Marko | bfea9c2 | 2014-01-17 17:49:33 +0000 | [diff] [blame] | 1269 | continue; |
| 1270 | } |
Vladimir Marko | e39c54e | 2014-09-22 14:50:02 +0100 | [diff] [blame] | 1271 | if (!copied_first) { |
| 1272 | copied_first = true; |
Vladimir Marko | f585e54 | 2014-11-21 13:41:32 +0000 | [diff] [blame] | 1273 | classes_to_check->Copy(temp_.cice.ending_classes_to_check_matrix[pred_id]); |
Vladimir Marko | e39c54e | 2014-09-22 14:50:02 +0100 | [diff] [blame] | 1274 | } else { |
Vladimir Marko | f585e54 | 2014-11-21 13:41:32 +0000 | [diff] [blame] | 1275 | classes_to_check->Union(temp_.cice.ending_classes_to_check_matrix[pred_id]); |
Vladimir Marko | e39c54e | 2014-09-22 14:50:02 +0100 | [diff] [blame] | 1276 | } |
Vladimir Marko | bfea9c2 | 2014-01-17 17:49:33 +0000 | [diff] [blame] | 1277 | } |
Vladimir Marko | e39c54e | 2014-09-22 14:50:02 +0100 | [diff] [blame] | 1278 | DCHECK(copied_first); // At least one predecessor must have been processed before this bb. |
Vladimir Marko | bfea9c2 | 2014-01-17 17:49:33 +0000 | [diff] [blame] | 1279 | } |
| 1280 | // At this point, classes_to_check shows which classes need clinit checks. |
| 1281 | |
| 1282 | // Walk through the instruction in the block, updating as necessary |
| 1283 | for (MIR* mir = bb->first_mir_insn; mir != nullptr; mir = mir->next) { |
Vladimir Marko | f585e54 | 2014-11-21 13:41:32 +0000 | [diff] [blame] | 1284 | uint16_t index = temp_.cice.indexes[mir->offset / 2u]; |
Vladimir Marko | 66c6d7b | 2014-10-16 15:41:48 +0100 | [diff] [blame] | 1285 | if (index != 0xffffu) { |
| 1286 | bool check_initialization = false; |
| 1287 | bool check_dex_cache = false; |
| 1288 | |
| 1289 | // NOTE: index != 0xffff does not guarantee that this is an SGET/SPUT/INVOKE_STATIC. |
| 1290 | // Dex instructions with width 1 can have the same offset/2. |
| 1291 | |
Vladimir Marko | af6925b | 2014-10-31 16:37:32 +0000 | [diff] [blame] | 1292 | if (IsInstructionSGetOrSPut(mir->dalvikInsn.opcode)) { |
Vladimir Marko | 66c6d7b | 2014-10-16 15:41:48 +0100 | [diff] [blame] | 1293 | check_initialization = true; |
| 1294 | check_dex_cache = true; |
Vladimir Marko | af6925b | 2014-10-31 16:37:32 +0000 | [diff] [blame] | 1295 | } else if (IsInstructionInvokeStatic(mir->dalvikInsn.opcode)) { |
Vladimir Marko | 66c6d7b | 2014-10-16 15:41:48 +0100 | [diff] [blame] | 1296 | check_initialization = true; |
| 1297 | // NOTE: INVOKE_STATIC doesn't guarantee that the type will be in the dex cache. |
| 1298 | } |
| 1299 | |
| 1300 | if (check_dex_cache) { |
| 1301 | uint32_t check_dex_cache_index = 2u * index + 1u; |
| 1302 | if (!classes_to_check->IsBitSet(check_dex_cache_index)) { |
| 1303 | // Eliminate the class init check. |
| 1304 | mir->optimization_flags |= MIR_CLASS_IS_IN_DEX_CACHE; |
| 1305 | } else { |
| 1306 | // Do the class init check. |
| 1307 | mir->optimization_flags &= ~MIR_CLASS_IS_IN_DEX_CACHE; |
| 1308 | } |
| 1309 | classes_to_check->ClearBit(check_dex_cache_index); |
| 1310 | } |
| 1311 | if (check_initialization) { |
| 1312 | uint32_t check_clinit_index = 2u * index; |
| 1313 | if (!classes_to_check->IsBitSet(check_clinit_index)) { |
| 1314 | // Eliminate the class init check. |
| 1315 | mir->optimization_flags |= MIR_CLASS_IS_INITIALIZED; |
| 1316 | } else { |
| 1317 | // Do the class init check. |
| 1318 | mir->optimization_flags &= ~MIR_CLASS_IS_INITIALIZED; |
Vladimir Marko | bfea9c2 | 2014-01-17 17:49:33 +0000 | [diff] [blame] | 1319 | } |
| 1320 | // Mark the class as initialized. |
Vladimir Marko | 66c6d7b | 2014-10-16 15:41:48 +0100 | [diff] [blame] | 1321 | classes_to_check->ClearBit(check_clinit_index); |
Vladimir Marko | bfea9c2 | 2014-01-17 17:49:33 +0000 | [diff] [blame] | 1322 | } |
| 1323 | } |
| 1324 | } |
| 1325 | |
| 1326 | // Did anything change? |
| 1327 | bool changed = false; |
Vladimir Marko | f585e54 | 2014-11-21 13:41:32 +0000 | [diff] [blame] | 1328 | ArenaBitVector* old_ending_classes_to_check = temp_.cice.ending_classes_to_check_matrix[bb->id]; |
Vladimir Marko | 5229cf1 | 2014-10-09 14:57:59 +0100 | [diff] [blame] | 1329 | if (old_ending_classes_to_check == nullptr) { |
Vladimir Marko | bfea9c2 | 2014-01-17 17:49:33 +0000 | [diff] [blame] | 1330 | DCHECK(temp_scoped_alloc_.get() != nullptr); |
Vladimir Marko | bfea9c2 | 2014-01-17 17:49:33 +0000 | [diff] [blame] | 1331 | changed = classes_to_check->GetHighestBitSet() != -1; |
Vladimir Marko | f585e54 | 2014-11-21 13:41:32 +0000 | [diff] [blame] | 1332 | temp_.cice.ending_classes_to_check_matrix[bb->id] = classes_to_check; |
Vladimir Marko | 5229cf1 | 2014-10-09 14:57:59 +0100 | [diff] [blame] | 1333 | // Create a new classes_to_check for next BB. |
Vladimir Marko | f585e54 | 2014-11-21 13:41:32 +0000 | [diff] [blame] | 1334 | temp_.cice.work_classes_to_check = new (temp_scoped_alloc_.get()) ArenaBitVector( |
| 1335 | temp_scoped_alloc_.get(), temp_.cice.num_class_bits, false, kBitMapClInitCheck); |
Vladimir Marko | 5229cf1 | 2014-10-09 14:57:59 +0100 | [diff] [blame] | 1336 | } else if (!classes_to_check->Equal(old_ending_classes_to_check)) { |
Vladimir Marko | bfea9c2 | 2014-01-17 17:49:33 +0000 | [diff] [blame] | 1337 | changed = true; |
Vladimir Marko | f585e54 | 2014-11-21 13:41:32 +0000 | [diff] [blame] | 1338 | temp_.cice.ending_classes_to_check_matrix[bb->id] = classes_to_check; |
| 1339 | temp_.cice.work_classes_to_check = old_ending_classes_to_check; // Reuse for next BB. |
Vladimir Marko | bfea9c2 | 2014-01-17 17:49:33 +0000 | [diff] [blame] | 1340 | } |
| 1341 | return changed; |
| 1342 | } |
| 1343 | |
| 1344 | void MIRGraph::EliminateClassInitChecksEnd() { |
| 1345 | // Clean up temporaries. |
Vladimir Marko | f585e54 | 2014-11-21 13:41:32 +0000 | [diff] [blame] | 1346 | temp_.cice.num_class_bits = 0u; |
| 1347 | temp_.cice.work_classes_to_check = nullptr; |
| 1348 | temp_.cice.ending_classes_to_check_matrix = nullptr; |
| 1349 | DCHECK(temp_.cice.indexes != nullptr); |
| 1350 | temp_.cice.indexes = nullptr; |
Vladimir Marko | bfea9c2 | 2014-01-17 17:49:33 +0000 | [diff] [blame] | 1351 | DCHECK(temp_scoped_alloc_.get() != nullptr); |
| 1352 | temp_scoped_alloc_.reset(); |
| 1353 | } |
| 1354 | |
Vladimir Marko | 95a0597 | 2014-05-30 10:01:32 +0100 | [diff] [blame] | 1355 | bool MIRGraph::ApplyGlobalValueNumberingGate() { |
Vladimir Marko | 415ac88 | 2014-09-30 18:09:14 +0100 | [diff] [blame] | 1356 | if (GlobalValueNumbering::Skip(cu_)) { |
Vladimir Marko | 95a0597 | 2014-05-30 10:01:32 +0100 | [diff] [blame] | 1357 | return false; |
| 1358 | } |
| 1359 | |
| 1360 | DCHECK(temp_scoped_alloc_ == nullptr); |
| 1361 | temp_scoped_alloc_.reset(ScopedArenaAllocator::Create(&cu_->arena_stack)); |
Vladimir Marko | 7a01dc2 | 2015-01-02 17:00:44 +0000 | [diff] [blame] | 1362 | temp_.gvn.ifield_ids = |
Vladimir Marko | af6925b | 2014-10-31 16:37:32 +0000 | [diff] [blame] | 1363 | GlobalValueNumbering::PrepareGvnFieldIds(temp_scoped_alloc_.get(), ifield_lowering_infos_); |
Vladimir Marko | 7a01dc2 | 2015-01-02 17:00:44 +0000 | [diff] [blame] | 1364 | temp_.gvn.sfield_ids = |
Vladimir Marko | af6925b | 2014-10-31 16:37:32 +0000 | [diff] [blame] | 1365 | GlobalValueNumbering::PrepareGvnFieldIds(temp_scoped_alloc_.get(), sfield_lowering_infos_); |
Vladimir Marko | f585e54 | 2014-11-21 13:41:32 +0000 | [diff] [blame] | 1366 | DCHECK(temp_.gvn.gvn == nullptr); |
| 1367 | temp_.gvn.gvn = new (temp_scoped_alloc_.get()) GlobalValueNumbering( |
| 1368 | cu_, temp_scoped_alloc_.get(), GlobalValueNumbering::kModeGvn); |
Vladimir Marko | 95a0597 | 2014-05-30 10:01:32 +0100 | [diff] [blame] | 1369 | return true; |
| 1370 | } |
| 1371 | |
| 1372 | bool MIRGraph::ApplyGlobalValueNumbering(BasicBlock* bb) { |
Vladimir Marko | f585e54 | 2014-11-21 13:41:32 +0000 | [diff] [blame] | 1373 | DCHECK(temp_.gvn.gvn != nullptr); |
| 1374 | LocalValueNumbering* lvn = temp_.gvn.gvn->PrepareBasicBlock(bb); |
Vladimir Marko | 95a0597 | 2014-05-30 10:01:32 +0100 | [diff] [blame] | 1375 | if (lvn != nullptr) { |
| 1376 | for (MIR* mir = bb->first_mir_insn; mir != nullptr; mir = mir->next) { |
| 1377 | lvn->GetValueNumber(mir); |
| 1378 | } |
| 1379 | } |
Vladimir Marko | f585e54 | 2014-11-21 13:41:32 +0000 | [diff] [blame] | 1380 | bool change = (lvn != nullptr) && temp_.gvn.gvn->FinishBasicBlock(bb); |
Vladimir Marko | 95a0597 | 2014-05-30 10:01:32 +0100 | [diff] [blame] | 1381 | return change; |
| 1382 | } |
| 1383 | |
| 1384 | void MIRGraph::ApplyGlobalValueNumberingEnd() { |
| 1385 | // Perform modifications. |
Vladimir Marko | f585e54 | 2014-11-21 13:41:32 +0000 | [diff] [blame] | 1386 | DCHECK(temp_.gvn.gvn != nullptr); |
| 1387 | if (temp_.gvn.gvn->Good()) { |
Vladimir Marko | 7a01dc2 | 2015-01-02 17:00:44 +0000 | [diff] [blame] | 1388 | temp_.gvn.gvn->StartPostProcessing(); |
Vladimir Marko | 415ac88 | 2014-09-30 18:09:14 +0100 | [diff] [blame] | 1389 | if (max_nested_loops_ != 0u) { |
Vladimir Marko | 415ac88 | 2014-09-30 18:09:14 +0100 | [diff] [blame] | 1390 | TopologicalSortIterator iter(this); |
| 1391 | for (BasicBlock* bb = iter.Next(); bb != nullptr; bb = iter.Next()) { |
| 1392 | ScopedArenaAllocator allocator(&cu_->arena_stack); // Reclaim memory after each LVN. |
Vladimir Marko | f585e54 | 2014-11-21 13:41:32 +0000 | [diff] [blame] | 1393 | LocalValueNumbering* lvn = temp_.gvn.gvn->PrepareBasicBlock(bb, &allocator); |
Vladimir Marko | 415ac88 | 2014-09-30 18:09:14 +0100 | [diff] [blame] | 1394 | if (lvn != nullptr) { |
| 1395 | for (MIR* mir = bb->first_mir_insn; mir != nullptr; mir = mir->next) { |
| 1396 | lvn->GetValueNumber(mir); |
| 1397 | } |
Vladimir Marko | f585e54 | 2014-11-21 13:41:32 +0000 | [diff] [blame] | 1398 | bool change = temp_.gvn.gvn->FinishBasicBlock(bb); |
Vladimir Marko | 415ac88 | 2014-09-30 18:09:14 +0100 | [diff] [blame] | 1399 | DCHECK(!change) << PrettyMethod(cu_->method_idx, *cu_->dex_file); |
Vladimir Marko | 95a0597 | 2014-05-30 10:01:32 +0100 | [diff] [blame] | 1400 | } |
Vladimir Marko | 95a0597 | 2014-05-30 10:01:32 +0100 | [diff] [blame] | 1401 | } |
| 1402 | } |
Vladimir Marko | 415ac88 | 2014-09-30 18:09:14 +0100 | [diff] [blame] | 1403 | // GVN was successful, running the LVN would be useless. |
| 1404 | cu_->disable_opt |= (1u << kLocalValueNumbering); |
Vladimir Marko | 95a0597 | 2014-05-30 10:01:32 +0100 | [diff] [blame] | 1405 | } else { |
| 1406 | LOG(WARNING) << "GVN failed for " << PrettyMethod(cu_->method_idx, *cu_->dex_file); |
Vladimir Marko | 7a01dc2 | 2015-01-02 17:00:44 +0000 | [diff] [blame] | 1407 | cu_->disable_opt |= (1u << kGvnDeadCodeElimination); |
Vladimir Marko | 95a0597 | 2014-05-30 10:01:32 +0100 | [diff] [blame] | 1408 | } |
| 1409 | |
Vladimir Marko | 7a01dc2 | 2015-01-02 17:00:44 +0000 | [diff] [blame] | 1410 | if ((cu_->disable_opt & (1 << kGvnDeadCodeElimination)) != 0) { |
| 1411 | EliminateDeadCodeEnd(); |
| 1412 | } // else preserve GVN data for CSE. |
| 1413 | } |
| 1414 | |
| 1415 | bool MIRGraph::EliminateDeadCodeGate() { |
| 1416 | if ((cu_->disable_opt & (1 << kGvnDeadCodeElimination)) != 0) { |
| 1417 | return false; |
| 1418 | } |
| 1419 | DCHECK(temp_scoped_alloc_ != nullptr); |
| 1420 | temp_.gvn.dce = new (temp_scoped_alloc_.get()) GvnDeadCodeElimination(temp_.gvn.gvn, |
| 1421 | temp_scoped_alloc_.get()); |
| 1422 | return true; |
| 1423 | } |
| 1424 | |
| 1425 | bool MIRGraph::EliminateDeadCode(BasicBlock* bb) { |
| 1426 | DCHECK(temp_scoped_alloc_ != nullptr); |
| 1427 | DCHECK(temp_.gvn.gvn != nullptr); |
| 1428 | if (bb->block_type != kDalvikByteCode) { |
| 1429 | return false; |
| 1430 | } |
| 1431 | DCHECK(temp_.gvn.dce != nullptr); |
| 1432 | temp_.gvn.dce->Apply(bb); |
| 1433 | return false; // No need to repeat. |
| 1434 | } |
| 1435 | |
| 1436 | void MIRGraph::EliminateDeadCodeEnd() { |
| 1437 | DCHECK_EQ(temp_.gvn.dce != nullptr, (cu_->disable_opt & (1 << kGvnDeadCodeElimination)) == 0); |
| 1438 | if (temp_.gvn.dce != nullptr) { |
| 1439 | delete temp_.gvn.dce; |
| 1440 | temp_.gvn.dce = nullptr; |
| 1441 | } |
Vladimir Marko | f585e54 | 2014-11-21 13:41:32 +0000 | [diff] [blame] | 1442 | delete temp_.gvn.gvn; |
| 1443 | temp_.gvn.gvn = nullptr; |
Vladimir Marko | 7a01dc2 | 2015-01-02 17:00:44 +0000 | [diff] [blame] | 1444 | temp_.gvn.ifield_ids = nullptr; |
| 1445 | temp_.gvn.sfield_ids = nullptr; |
Vladimir Marko | 95a0597 | 2014-05-30 10:01:32 +0100 | [diff] [blame] | 1446 | DCHECK(temp_scoped_alloc_ != nullptr); |
| 1447 | temp_scoped_alloc_.reset(); |
| 1448 | } |
| 1449 | |
Vladimir Marko | 9820b7c | 2014-01-02 16:40:37 +0000 | [diff] [blame] | 1450 | void MIRGraph::ComputeInlineIFieldLoweringInfo(uint16_t field_idx, MIR* invoke, MIR* iget_or_iput) { |
| 1451 | uint32_t method_index = invoke->meta.method_lowering_info; |
Vladimir Marko | f585e54 | 2014-11-21 13:41:32 +0000 | [diff] [blame] | 1452 | if (temp_.smi.processed_indexes->IsBitSet(method_index)) { |
| 1453 | iget_or_iput->meta.ifield_lowering_info = temp_.smi.lowering_infos[method_index]; |
Vladimir Marko | 9820b7c | 2014-01-02 16:40:37 +0000 | [diff] [blame] | 1454 | DCHECK_EQ(field_idx, GetIFieldLoweringInfo(iget_or_iput).FieldIndex()); |
| 1455 | return; |
| 1456 | } |
| 1457 | |
| 1458 | const MirMethodLoweringInfo& method_info = GetMethodLoweringInfo(invoke); |
| 1459 | MethodReference target = method_info.GetTargetMethod(); |
| 1460 | DexCompilationUnit inlined_unit( |
| 1461 | cu_, cu_->class_loader, cu_->class_linker, *target.dex_file, |
| 1462 | nullptr /* code_item not used */, 0u /* class_def_idx not used */, target.dex_method_index, |
| 1463 | 0u /* access_flags not used */, nullptr /* verified_method not used */); |
Vladimir Marko | af6925b | 2014-10-31 16:37:32 +0000 | [diff] [blame] | 1464 | DexMemAccessType type = IGetOrIPutMemAccessType(iget_or_iput->dalvikInsn.opcode); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 1465 | MirIFieldLoweringInfo inlined_field_info(field_idx, type, false); |
Vladimir Marko | 9820b7c | 2014-01-02 16:40:37 +0000 | [diff] [blame] | 1466 | MirIFieldLoweringInfo::Resolve(cu_->compiler_driver, &inlined_unit, &inlined_field_info, 1u); |
| 1467 | DCHECK(inlined_field_info.IsResolved()); |
| 1468 | |
Vladimir Marko | e39c54e | 2014-09-22 14:50:02 +0100 | [diff] [blame] | 1469 | uint32_t field_info_index = ifield_lowering_infos_.size(); |
| 1470 | ifield_lowering_infos_.push_back(inlined_field_info); |
Vladimir Marko | f585e54 | 2014-11-21 13:41:32 +0000 | [diff] [blame] | 1471 | temp_.smi.processed_indexes->SetBit(method_index); |
| 1472 | temp_.smi.lowering_infos[method_index] = field_info_index; |
Vladimir Marko | 9820b7c | 2014-01-02 16:40:37 +0000 | [diff] [blame] | 1473 | iget_or_iput->meta.ifield_lowering_info = field_info_index; |
| 1474 | } |
| 1475 | |
Razvan A Lupusoru | cb80474 | 2014-07-09 16:42:19 -0700 | [diff] [blame] | 1476 | bool MIRGraph::InlineSpecialMethodsGate() { |
Vladimir Marko | 9820b7c | 2014-01-02 16:40:37 +0000 | [diff] [blame] | 1477 | if ((cu_->disable_opt & (1 << kSuppressMethodInlining)) != 0 || |
Vladimir Marko | e39c54e | 2014-09-22 14:50:02 +0100 | [diff] [blame] | 1478 | method_lowering_infos_.size() == 0u) { |
Vladimir Marko | 9820b7c | 2014-01-02 16:40:37 +0000 | [diff] [blame] | 1479 | return false; |
| 1480 | } |
| 1481 | if (cu_->compiler_driver->GetMethodInlinerMap() == nullptr) { |
| 1482 | // This isn't the Quick compiler. |
| 1483 | return false; |
| 1484 | } |
| 1485 | return true; |
| 1486 | } |
| 1487 | |
Razvan A Lupusoru | cb80474 | 2014-07-09 16:42:19 -0700 | [diff] [blame] | 1488 | void MIRGraph::InlineSpecialMethodsStart() { |
Vladimir Marko | 9820b7c | 2014-01-02 16:40:37 +0000 | [diff] [blame] | 1489 | // Prepare for inlining getters/setters. Since we're inlining at most 1 IGET/IPUT from |
| 1490 | // each INVOKE, we can index the data by the MIR::meta::method_lowering_info index. |
| 1491 | |
| 1492 | DCHECK(temp_scoped_alloc_.get() == nullptr); |
| 1493 | temp_scoped_alloc_.reset(ScopedArenaAllocator::Create(&cu_->arena_stack)); |
Vladimir Marko | f585e54 | 2014-11-21 13:41:32 +0000 | [diff] [blame] | 1494 | temp_.smi.num_indexes = method_lowering_infos_.size(); |
| 1495 | temp_.smi.processed_indexes = new (temp_scoped_alloc_.get()) ArenaBitVector( |
| 1496 | temp_scoped_alloc_.get(), temp_.smi.num_indexes, false, kBitMapMisc); |
| 1497 | temp_.smi.processed_indexes->ClearAllBits(); |
Vladimir Marko | e4fcc5b | 2015-02-13 10:28:29 +0000 | [diff] [blame] | 1498 | temp_.smi.lowering_infos = |
| 1499 | temp_scoped_alloc_->AllocArray<uint16_t>(temp_.smi.num_indexes, kArenaAllocGrowableArray); |
Vladimir Marko | 9820b7c | 2014-01-02 16:40:37 +0000 | [diff] [blame] | 1500 | } |
| 1501 | |
Razvan A Lupusoru | cb80474 | 2014-07-09 16:42:19 -0700 | [diff] [blame] | 1502 | void MIRGraph::InlineSpecialMethods(BasicBlock* bb) { |
Vladimir Marko | 9820b7c | 2014-01-02 16:40:37 +0000 | [diff] [blame] | 1503 | if (bb->block_type != kDalvikByteCode) { |
| 1504 | return; |
| 1505 | } |
| 1506 | for (MIR* mir = bb->first_mir_insn; mir != NULL; mir = mir->next) { |
Jean Christophe Beyler | 2ab40eb | 2014-06-02 09:03:14 -0700 | [diff] [blame] | 1507 | if (MIR::DecodedInstruction::IsPseudoMirOp(mir->dalvikInsn.opcode)) { |
buzbee | 35ba7f3 | 2014-05-31 08:59:01 -0700 | [diff] [blame] | 1508 | continue; |
| 1509 | } |
Jean Christophe Beyler | fb0ea2d | 2014-07-29 13:20:42 -0700 | [diff] [blame] | 1510 | if (!(mir->dalvikInsn.FlagsOf() & Instruction::kInvoke)) { |
Vladimir Marko | 9820b7c | 2014-01-02 16:40:37 +0000 | [diff] [blame] | 1511 | continue; |
| 1512 | } |
| 1513 | const MirMethodLoweringInfo& method_info = GetMethodLoweringInfo(mir); |
Vladimir Marko | 87b7c52 | 2015-04-08 10:01:01 +0100 | [diff] [blame] | 1514 | if (!method_info.FastPath() || !method_info.IsSpecial()) { |
Vladimir Marko | 9820b7c | 2014-01-02 16:40:37 +0000 | [diff] [blame] | 1515 | continue; |
| 1516 | } |
Razvan A Lupusoru | c80605d | 2014-09-11 14:12:17 -0700 | [diff] [blame] | 1517 | |
Vladimir Marko | 9820b7c | 2014-01-02 16:40:37 +0000 | [diff] [blame] | 1518 | InvokeType sharp_type = method_info.GetSharpType(); |
Razvan A Lupusoru | c80605d | 2014-09-11 14:12:17 -0700 | [diff] [blame] | 1519 | if ((sharp_type != kDirect) && (sharp_type != kStatic)) { |
Vladimir Marko | 9820b7c | 2014-01-02 16:40:37 +0000 | [diff] [blame] | 1520 | continue; |
| 1521 | } |
Razvan A Lupusoru | c80605d | 2014-09-11 14:12:17 -0700 | [diff] [blame] | 1522 | |
| 1523 | if (sharp_type == kStatic) { |
Vladimir Marko | 66c6d7b | 2014-10-16 15:41:48 +0100 | [diff] [blame] | 1524 | bool needs_clinit = !method_info.IsClassInitialized() && |
| 1525 | ((mir->optimization_flags & MIR_CLASS_IS_INITIALIZED) == 0); |
Razvan A Lupusoru | c80605d | 2014-09-11 14:12:17 -0700 | [diff] [blame] | 1526 | if (needs_clinit) { |
| 1527 | continue; |
| 1528 | } |
| 1529 | } |
| 1530 | |
Vladimir Marko | 9820b7c | 2014-01-02 16:40:37 +0000 | [diff] [blame] | 1531 | DCHECK(cu_->compiler_driver->GetMethodInlinerMap() != nullptr); |
| 1532 | MethodReference target = method_info.GetTargetMethod(); |
| 1533 | if (cu_->compiler_driver->GetMethodInlinerMap()->GetMethodInliner(target.dex_file) |
| 1534 | ->GenInline(this, bb, mir, target.dex_method_index)) { |
Razvan A Lupusoru | cb80474 | 2014-07-09 16:42:19 -0700 | [diff] [blame] | 1535 | if (cu_->verbose || cu_->print_pass) { |
| 1536 | LOG(INFO) << "SpecialMethodInliner: Inlined " << method_info.GetInvokeType() << " (" |
| 1537 | << sharp_type << ") call to \"" << PrettyMethod(target.dex_method_index, *target.dex_file) |
| 1538 | << "\" from \"" << PrettyMethod(cu_->method_idx, *cu_->dex_file) |
| 1539 | << "\" @0x" << std::hex << mir->offset; |
Vladimir Marko | 9820b7c | 2014-01-02 16:40:37 +0000 | [diff] [blame] | 1540 | } |
| 1541 | } |
| 1542 | } |
| 1543 | } |
| 1544 | |
Razvan A Lupusoru | cb80474 | 2014-07-09 16:42:19 -0700 | [diff] [blame] | 1545 | void MIRGraph::InlineSpecialMethodsEnd() { |
Vladimir Marko | f585e54 | 2014-11-21 13:41:32 +0000 | [diff] [blame] | 1546 | // Clean up temporaries. |
| 1547 | DCHECK(temp_.smi.lowering_infos != nullptr); |
| 1548 | temp_.smi.lowering_infos = nullptr; |
| 1549 | temp_.smi.num_indexes = 0u; |
| 1550 | DCHECK(temp_.smi.processed_indexes != nullptr); |
| 1551 | temp_.smi.processed_indexes = nullptr; |
Vladimir Marko | 9820b7c | 2014-01-02 16:40:37 +0000 | [diff] [blame] | 1552 | DCHECK(temp_scoped_alloc_.get() != nullptr); |
| 1553 | temp_scoped_alloc_.reset(); |
| 1554 | } |
| 1555 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 1556 | void MIRGraph::DumpCheckStats() { |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 1557 | Checkstats* stats = |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 1558 | static_cast<Checkstats*>(arena_->Alloc(sizeof(Checkstats), kArenaAllocDFInfo)); |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 1559 | checkstats_ = stats; |
buzbee | 56c7178 | 2013-09-05 17:13:19 -0700 | [diff] [blame] | 1560 | AllNodesIterator iter(this); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 1561 | for (BasicBlock* bb = iter.Next(); bb != NULL; bb = iter.Next()) { |
| 1562 | CountChecks(bb); |
| 1563 | } |
| 1564 | if (stats->null_checks > 0) { |
| 1565 | float eliminated = static_cast<float>(stats->null_checks_eliminated); |
| 1566 | float checks = static_cast<float>(stats->null_checks); |
| 1567 | LOG(INFO) << "Null Checks: " << PrettyMethod(cu_->method_idx, *cu_->dex_file) << " " |
| 1568 | << stats->null_checks_eliminated << " of " << stats->null_checks << " -> " |
| 1569 | << (eliminated/checks) * 100.0 << "%"; |
| 1570 | } |
| 1571 | if (stats->range_checks > 0) { |
| 1572 | float eliminated = static_cast<float>(stats->range_checks_eliminated); |
| 1573 | float checks = static_cast<float>(stats->range_checks); |
| 1574 | LOG(INFO) << "Range Checks: " << PrettyMethod(cu_->method_idx, *cu_->dex_file) << " " |
| 1575 | << stats->range_checks_eliminated << " of " << stats->range_checks << " -> " |
| 1576 | << (eliminated/checks) * 100.0 << "%"; |
| 1577 | } |
| 1578 | } |
| 1579 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 1580 | bool MIRGraph::BuildExtendedBBList(class BasicBlock* bb) { |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 1581 | if (bb->visited) return false; |
| 1582 | if (!((bb->block_type == kEntryBlock) || (bb->block_type == kDalvikByteCode) |
| 1583 | || (bb->block_type == kExitBlock))) { |
| 1584 | // Ignore special blocks |
| 1585 | bb->visited = true; |
| 1586 | return false; |
| 1587 | } |
| 1588 | // Must be head of extended basic block. |
| 1589 | BasicBlock* start_bb = bb; |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 1590 | extended_basic_blocks_.push_back(bb->id); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 1591 | bool terminated_by_return = false; |
buzbee | 1da1e2f | 2013-11-15 13:37:01 -0800 | [diff] [blame] | 1592 | bool do_local_value_numbering = false; |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 1593 | // Visit blocks strictly dominated by this head. |
| 1594 | while (bb != NULL) { |
| 1595 | bb->visited = true; |
| 1596 | terminated_by_return |= bb->terminated_by_return; |
buzbee | 1da1e2f | 2013-11-15 13:37:01 -0800 | [diff] [blame] | 1597 | do_local_value_numbering |= bb->use_lvn; |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 1598 | bb = NextDominatedBlock(bb); |
| 1599 | } |
buzbee | 1da1e2f | 2013-11-15 13:37:01 -0800 | [diff] [blame] | 1600 | if (terminated_by_return || do_local_value_numbering) { |
| 1601 | // Do lvn for all blocks in this extended set. |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 1602 | bb = start_bb; |
| 1603 | while (bb != NULL) { |
buzbee | 1da1e2f | 2013-11-15 13:37:01 -0800 | [diff] [blame] | 1604 | bb->use_lvn = do_local_value_numbering; |
| 1605 | bb->dominates_return = terminated_by_return; |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 1606 | bb = NextDominatedBlock(bb); |
| 1607 | } |
| 1608 | } |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 1609 | return false; // Not iterative - return value will be ignored |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 1610 | } |
| 1611 | |
Vladimir Marko | ffda499 | 2014-12-18 17:05:58 +0000 | [diff] [blame] | 1612 | void MIRGraph::BasicBlockOptimizationStart() { |
Vladimir Marko | af6925b | 2014-10-31 16:37:32 +0000 | [diff] [blame] | 1613 | if ((cu_->disable_opt & (1 << kLocalValueNumbering)) == 0) { |
| 1614 | temp_scoped_alloc_.reset(ScopedArenaAllocator::Create(&cu_->arena_stack)); |
Vladimir Marko | 7a01dc2 | 2015-01-02 17:00:44 +0000 | [diff] [blame] | 1615 | temp_.gvn.ifield_ids = |
Vladimir Marko | af6925b | 2014-10-31 16:37:32 +0000 | [diff] [blame] | 1616 | GlobalValueNumbering::PrepareGvnFieldIds(temp_scoped_alloc_.get(), ifield_lowering_infos_); |
Vladimir Marko | 7a01dc2 | 2015-01-02 17:00:44 +0000 | [diff] [blame] | 1617 | temp_.gvn.sfield_ids = |
Vladimir Marko | af6925b | 2014-10-31 16:37:32 +0000 | [diff] [blame] | 1618 | GlobalValueNumbering::PrepareGvnFieldIds(temp_scoped_alloc_.get(), sfield_lowering_infos_); |
| 1619 | } |
Vladimir Marko | ffda499 | 2014-12-18 17:05:58 +0000 | [diff] [blame] | 1620 | } |
Vladimir Marko | af6925b | 2014-10-31 16:37:32 +0000 | [diff] [blame] | 1621 | |
Vladimir Marko | ffda499 | 2014-12-18 17:05:58 +0000 | [diff] [blame] | 1622 | void MIRGraph::BasicBlockOptimization() { |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 1623 | if ((cu_->disable_opt & (1 << kSuppressExceptionEdges)) != 0) { |
| 1624 | ClearAllVisitedFlags(); |
| 1625 | PreOrderDfsIterator iter2(this); |
| 1626 | for (BasicBlock* bb = iter2.Next(); bb != NULL; bb = iter2.Next()) { |
| 1627 | BuildExtendedBBList(bb); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 1628 | } |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 1629 | // Perform extended basic block optimizations. |
| 1630 | for (unsigned int i = 0; i < extended_basic_blocks_.size(); i++) { |
| 1631 | BasicBlockOpt(GetBasicBlock(extended_basic_blocks_[i])); |
| 1632 | } |
| 1633 | } else { |
| 1634 | PreOrderDfsIterator iter(this); |
| 1635 | for (BasicBlock* bb = iter.Next(); bb != NULL; bb = iter.Next()) { |
| 1636 | BasicBlockOpt(bb); |
| 1637 | } |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 1638 | } |
Vladimir Marko | ffda499 | 2014-12-18 17:05:58 +0000 | [diff] [blame] | 1639 | } |
Vladimir Marko | af6925b | 2014-10-31 16:37:32 +0000 | [diff] [blame] | 1640 | |
Vladimir Marko | ffda499 | 2014-12-18 17:05:58 +0000 | [diff] [blame] | 1641 | void MIRGraph::BasicBlockOptimizationEnd() { |
Vladimir Marko | af6925b | 2014-10-31 16:37:32 +0000 | [diff] [blame] | 1642 | // Clean up after LVN. |
Vladimir Marko | 7a01dc2 | 2015-01-02 17:00:44 +0000 | [diff] [blame] | 1643 | temp_.gvn.ifield_ids = nullptr; |
| 1644 | temp_.gvn.sfield_ids = nullptr; |
Vladimir Marko | af6925b | 2014-10-31 16:37:32 +0000 | [diff] [blame] | 1645 | temp_scoped_alloc_.reset(); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 1646 | } |
| 1647 | |
Vladimir Marko | 8b858e1 | 2014-11-27 14:52:37 +0000 | [diff] [blame] | 1648 | bool MIRGraph::EliminateSuspendChecksGate() { |
| 1649 | if ((cu_->disable_opt & (1 << kSuspendCheckElimination)) != 0 || // Disabled. |
| 1650 | GetMaxNestedLoops() == 0u || // Nothing to do. |
| 1651 | GetMaxNestedLoops() >= 32u || // Only 32 bits in suspend_checks_in_loops_[.]. |
| 1652 | // Exclude 32 as well to keep bit shifts well-defined. |
| 1653 | !HasInvokes()) { // No invokes to actually eliminate any suspend checks. |
| 1654 | return false; |
| 1655 | } |
Vladimir Marko | e4fcc5b | 2015-02-13 10:28:29 +0000 | [diff] [blame] | 1656 | suspend_checks_in_loops_ = arena_->AllocArray<uint32_t>(GetNumBlocks(), kArenaAllocMisc); |
Vladimir Marko | 8b858e1 | 2014-11-27 14:52:37 +0000 | [diff] [blame] | 1657 | return true; |
| 1658 | } |
| 1659 | |
| 1660 | bool MIRGraph::EliminateSuspendChecks(BasicBlock* bb) { |
| 1661 | if (bb->block_type != kDalvikByteCode) { |
| 1662 | return false; |
| 1663 | } |
| 1664 | DCHECK_EQ(GetTopologicalSortOrderLoopHeadStack()->size(), bb->nesting_depth); |
| 1665 | if (bb->nesting_depth == 0u) { |
| 1666 | // Out of loops. |
| 1667 | DCHECK_EQ(suspend_checks_in_loops_[bb->id], 0u); // The array was zero-initialized. |
| 1668 | return false; |
| 1669 | } |
| 1670 | uint32_t suspend_checks_in_loops = (1u << bb->nesting_depth) - 1u; // Start with all loop heads. |
| 1671 | bool found_invoke = false; |
| 1672 | for (MIR* mir = bb->first_mir_insn; mir != nullptr; mir = mir->next) { |
Vladimir Marko | 87b7c52 | 2015-04-08 10:01:01 +0100 | [diff] [blame] | 1673 | if ((IsInstructionInvoke(mir->dalvikInsn.opcode) || |
| 1674 | IsInstructionQuickInvoke(mir->dalvikInsn.opcode)) && |
| 1675 | !GetMethodLoweringInfo(mir).IsIntrinsic()) { |
Vladimir Marko | 8b858e1 | 2014-11-27 14:52:37 +0000 | [diff] [blame] | 1676 | // Non-intrinsic invoke, rely on a suspend point in the invoked method. |
| 1677 | found_invoke = true; |
| 1678 | break; |
| 1679 | } |
| 1680 | } |
| 1681 | if (!found_invoke) { |
| 1682 | // Intersect suspend checks from predecessors. |
| 1683 | uint16_t bb_topo_idx = topological_order_indexes_[bb->id]; |
| 1684 | uint32_t pred_mask_union = 0u; |
| 1685 | for (BasicBlockId pred_id : bb->predecessors) { |
| 1686 | uint16_t pred_topo_idx = topological_order_indexes_[pred_id]; |
| 1687 | if (pred_topo_idx < bb_topo_idx) { |
| 1688 | // Determine the loop depth of the predecessors relative to this block. |
| 1689 | size_t pred_loop_depth = topological_order_loop_head_stack_.size(); |
| 1690 | while (pred_loop_depth != 0u && |
| 1691 | pred_topo_idx < topological_order_loop_head_stack_[pred_loop_depth - 1].first) { |
| 1692 | --pred_loop_depth; |
| 1693 | } |
| 1694 | DCHECK_LE(pred_loop_depth, GetBasicBlock(pred_id)->nesting_depth); |
| 1695 | uint32_t pred_mask = (1u << pred_loop_depth) - 1u; |
| 1696 | // Intersect pred_mask bits in suspend_checks_in_loops with |
| 1697 | // suspend_checks_in_loops_[pred_id]. |
| 1698 | uint32_t pred_loops_without_checks = pred_mask & ~suspend_checks_in_loops_[pred_id]; |
| 1699 | suspend_checks_in_loops = suspend_checks_in_loops & ~pred_loops_without_checks; |
| 1700 | pred_mask_union |= pred_mask; |
| 1701 | } |
| 1702 | } |
| 1703 | DCHECK_EQ(((1u << (IsLoopHead(bb->id) ? bb->nesting_depth - 1u: bb->nesting_depth)) - 1u), |
| 1704 | pred_mask_union); |
| 1705 | suspend_checks_in_loops &= pred_mask_union; |
| 1706 | } |
| 1707 | suspend_checks_in_loops_[bb->id] = suspend_checks_in_loops; |
| 1708 | if (suspend_checks_in_loops == 0u) { |
| 1709 | return false; |
| 1710 | } |
| 1711 | // Apply MIR_IGNORE_SUSPEND_CHECK if appropriate. |
| 1712 | if (bb->taken != NullBasicBlockId) { |
| 1713 | DCHECK(bb->last_mir_insn != nullptr); |
| 1714 | DCHECK(IsInstructionIfCc(bb->last_mir_insn->dalvikInsn.opcode) || |
| 1715 | IsInstructionIfCcZ(bb->last_mir_insn->dalvikInsn.opcode) || |
| 1716 | IsInstructionGoto(bb->last_mir_insn->dalvikInsn.opcode) || |
| 1717 | (static_cast<int>(bb->last_mir_insn->dalvikInsn.opcode) >= kMirOpFusedCmplFloat && |
| 1718 | static_cast<int>(bb->last_mir_insn->dalvikInsn.opcode) <= kMirOpFusedCmpLong)); |
| 1719 | if (!IsSuspendCheckEdge(bb, bb->taken) && |
| 1720 | (bb->fall_through == NullBasicBlockId || !IsSuspendCheckEdge(bb, bb->fall_through))) { |
| 1721 | bb->last_mir_insn->optimization_flags |= MIR_IGNORE_SUSPEND_CHECK; |
| 1722 | } |
| 1723 | } else if (bb->fall_through != NullBasicBlockId && IsSuspendCheckEdge(bb, bb->fall_through)) { |
| 1724 | // We've got a fall-through suspend edge. Add an artificial GOTO to force suspend check. |
| 1725 | MIR* mir = NewMIR(); |
| 1726 | mir->dalvikInsn.opcode = Instruction::GOTO; |
| 1727 | mir->dalvikInsn.vA = 0; // Branch offset. |
| 1728 | mir->offset = GetBasicBlock(bb->fall_through)->start_offset; |
| 1729 | mir->m_unit_index = current_method_; |
| 1730 | mir->ssa_rep = reinterpret_cast<SSARepresentation*>( |
| 1731 | arena_->Alloc(sizeof(SSARepresentation), kArenaAllocDFInfo)); // Zero-initialized. |
| 1732 | bb->AppendMIR(mir); |
| 1733 | std::swap(bb->fall_through, bb->taken); // The fall-through has become taken. |
| 1734 | } |
| 1735 | return true; |
| 1736 | } |
| 1737 | |
Vladimir Marko | 7a01dc2 | 2015-01-02 17:00:44 +0000 | [diff] [blame] | 1738 | bool MIRGraph::CanThrow(MIR* mir) const { |
Ningsheng Jian | a262f77 | 2014-11-25 16:48:07 +0800 | [diff] [blame] | 1739 | if ((mir->dalvikInsn.FlagsOf() & Instruction::kThrow) == 0) { |
| 1740 | return false; |
| 1741 | } |
| 1742 | const int opt_flags = mir->optimization_flags; |
| 1743 | uint64_t df_attributes = GetDataFlowAttributes(mir); |
| 1744 | |
Vladimir Marko | 315cc20 | 2014-12-18 17:01:02 +0000 | [diff] [blame] | 1745 | // First, check if the insn can still throw NPE. |
Ningsheng Jian | a262f77 | 2014-11-25 16:48:07 +0800 | [diff] [blame] | 1746 | if (((df_attributes & DF_HAS_NULL_CHKS) != 0) && ((opt_flags & MIR_IGNORE_NULL_CHECK) == 0)) { |
| 1747 | return true; |
| 1748 | } |
Vladimir Marko | 315cc20 | 2014-12-18 17:01:02 +0000 | [diff] [blame] | 1749 | |
| 1750 | // Now process specific instructions. |
Ningsheng Jian | a262f77 | 2014-11-25 16:48:07 +0800 | [diff] [blame] | 1751 | if ((df_attributes & DF_IFIELD) != 0) { |
Vladimir Marko | 315cc20 | 2014-12-18 17:01:02 +0000 | [diff] [blame] | 1752 | // The IGET/IPUT family. We have processed the IGET/IPUT null check above. |
| 1753 | DCHECK_NE(opt_flags & MIR_IGNORE_NULL_CHECK, 0); |
| 1754 | // If not fast, weird things can happen and the insn can throw. |
Ningsheng Jian | a262f77 | 2014-11-25 16:48:07 +0800 | [diff] [blame] | 1755 | const MirIFieldLoweringInfo& field_info = GetIFieldLoweringInfo(mir); |
Vladimir Marko | 315cc20 | 2014-12-18 17:01:02 +0000 | [diff] [blame] | 1756 | bool fast = (df_attributes & DF_DA) != 0 ? field_info.FastGet() : field_info.FastPut(); |
| 1757 | return !fast; |
Ningsheng Jian | a262f77 | 2014-11-25 16:48:07 +0800 | [diff] [blame] | 1758 | } else if ((df_attributes & DF_SFIELD) != 0) { |
Vladimir Marko | 315cc20 | 2014-12-18 17:01:02 +0000 | [diff] [blame] | 1759 | // The SGET/SPUT family. Check for potentially throwing class initialization. |
| 1760 | // Also, if not fast, weird things can happen and the insn can throw. |
Ningsheng Jian | a262f77 | 2014-11-25 16:48:07 +0800 | [diff] [blame] | 1761 | const MirSFieldLoweringInfo& field_info = GetSFieldLoweringInfo(mir); |
Vladimir Marko | 315cc20 | 2014-12-18 17:01:02 +0000 | [diff] [blame] | 1762 | bool fast = (df_attributes & DF_DA) != 0 ? field_info.FastGet() : field_info.FastPut(); |
Ningsheng Jian | a262f77 | 2014-11-25 16:48:07 +0800 | [diff] [blame] | 1763 | bool is_class_initialized = field_info.IsClassInitialized() || |
| 1764 | ((mir->optimization_flags & MIR_CLASS_IS_INITIALIZED) != 0); |
Vladimir Marko | 315cc20 | 2014-12-18 17:01:02 +0000 | [diff] [blame] | 1765 | return !(fast && is_class_initialized); |
| 1766 | } else if ((df_attributes & DF_HAS_RANGE_CHKS) != 0) { |
| 1767 | // Only AGET/APUT have range checks. We have processed the AGET/APUT null check above. |
| 1768 | DCHECK_NE(opt_flags & MIR_IGNORE_NULL_CHECK, 0); |
| 1769 | // Non-throwing only if range check has been eliminated. |
| 1770 | return ((opt_flags & MIR_IGNORE_RANGE_CHECK) == 0); |
Vladimir Marko | 22fe45d | 2015-03-18 11:33:58 +0000 | [diff] [blame] | 1771 | } else if (mir->dalvikInsn.opcode == Instruction::CHECK_CAST && |
| 1772 | (opt_flags & MIR_IGNORE_CHECK_CAST) != 0) { |
| 1773 | return false; |
Vladimir Marko | 315cc20 | 2014-12-18 17:01:02 +0000 | [diff] [blame] | 1774 | } else if (mir->dalvikInsn.opcode == Instruction::ARRAY_LENGTH || |
Vladimir Marko | 315cc20 | 2014-12-18 17:01:02 +0000 | [diff] [blame] | 1775 | static_cast<int>(mir->dalvikInsn.opcode) == kMirOpNullCheck) { |
| 1776 | // No more checks for these (null check was processed above). |
| 1777 | return false; |
Ningsheng Jian | a262f77 | 2014-11-25 16:48:07 +0800 | [diff] [blame] | 1778 | } |
| 1779 | return true; |
| 1780 | } |
| 1781 | |
| 1782 | bool MIRGraph::HasAntiDependency(MIR* first, MIR* second) { |
| 1783 | DCHECK(first->ssa_rep != nullptr); |
| 1784 | DCHECK(second->ssa_rep != nullptr); |
| 1785 | if ((second->ssa_rep->num_defs > 0) && (first->ssa_rep->num_uses > 0)) { |
| 1786 | int vreg0 = SRegToVReg(second->ssa_rep->defs[0]); |
| 1787 | int vreg1 = (second->ssa_rep->num_defs == 2) ? |
| 1788 | SRegToVReg(second->ssa_rep->defs[1]) : INVALID_VREG; |
| 1789 | for (int i = 0; i < first->ssa_rep->num_uses; i++) { |
| 1790 | int32_t use = SRegToVReg(first->ssa_rep->uses[i]); |
| 1791 | if (use == vreg0 || use == vreg1) { |
| 1792 | return true; |
| 1793 | } |
| 1794 | } |
| 1795 | } |
| 1796 | return false; |
| 1797 | } |
| 1798 | |
| 1799 | void MIRGraph::CombineMultiplyAdd(MIR* mul_mir, MIR* add_mir, bool mul_is_first_addend, |
| 1800 | bool is_wide, bool is_sub) { |
| 1801 | if (is_wide) { |
| 1802 | if (is_sub) { |
| 1803 | add_mir->dalvikInsn.opcode = static_cast<Instruction::Code>(kMirOpMsubLong); |
| 1804 | } else { |
| 1805 | add_mir->dalvikInsn.opcode = static_cast<Instruction::Code>(kMirOpMaddLong); |
| 1806 | } |
| 1807 | } else { |
| 1808 | if (is_sub) { |
| 1809 | add_mir->dalvikInsn.opcode = static_cast<Instruction::Code>(kMirOpMsubInt); |
| 1810 | } else { |
| 1811 | add_mir->dalvikInsn.opcode = static_cast<Instruction::Code>(kMirOpMaddInt); |
| 1812 | } |
| 1813 | } |
| 1814 | add_mir->ssa_rep->num_uses = is_wide ? 6 : 3; |
| 1815 | int32_t addend0 = INVALID_SREG; |
| 1816 | int32_t addend1 = INVALID_SREG; |
| 1817 | if (is_wide) { |
| 1818 | addend0 = mul_is_first_addend ? add_mir->ssa_rep->uses[2] : add_mir->ssa_rep->uses[0]; |
| 1819 | addend1 = mul_is_first_addend ? add_mir->ssa_rep->uses[3] : add_mir->ssa_rep->uses[1]; |
| 1820 | } else { |
| 1821 | addend0 = mul_is_first_addend ? add_mir->ssa_rep->uses[1] : add_mir->ssa_rep->uses[0]; |
| 1822 | } |
| 1823 | |
| 1824 | AllocateSSAUseData(add_mir, add_mir->ssa_rep->num_uses); |
| 1825 | add_mir->ssa_rep->uses[0] = mul_mir->ssa_rep->uses[0]; |
| 1826 | add_mir->ssa_rep->uses[1] = mul_mir->ssa_rep->uses[1]; |
| 1827 | // Clear the original multiply product ssa use count, as it is not used anymore. |
| 1828 | raw_use_counts_[mul_mir->ssa_rep->defs[0]] = 0; |
| 1829 | use_counts_[mul_mir->ssa_rep->defs[0]] = 0; |
| 1830 | if (is_wide) { |
| 1831 | DCHECK_EQ(add_mir->ssa_rep->num_uses, 6); |
| 1832 | add_mir->ssa_rep->uses[2] = mul_mir->ssa_rep->uses[2]; |
| 1833 | add_mir->ssa_rep->uses[3] = mul_mir->ssa_rep->uses[3]; |
| 1834 | add_mir->ssa_rep->uses[4] = addend0; |
| 1835 | add_mir->ssa_rep->uses[5] = addend1; |
| 1836 | raw_use_counts_[mul_mir->ssa_rep->defs[1]] = 0; |
| 1837 | use_counts_[mul_mir->ssa_rep->defs[1]] = 0; |
| 1838 | } else { |
| 1839 | DCHECK_EQ(add_mir->ssa_rep->num_uses, 3); |
| 1840 | add_mir->ssa_rep->uses[2] = addend0; |
| 1841 | } |
| 1842 | // Copy in the decoded instruction information. |
| 1843 | add_mir->dalvikInsn.vB = SRegToVReg(add_mir->ssa_rep->uses[0]); |
| 1844 | if (is_wide) { |
| 1845 | add_mir->dalvikInsn.vC = SRegToVReg(add_mir->ssa_rep->uses[2]); |
| 1846 | add_mir->dalvikInsn.arg[0] = SRegToVReg(add_mir->ssa_rep->uses[4]); |
| 1847 | } else { |
| 1848 | add_mir->dalvikInsn.vC = SRegToVReg(add_mir->ssa_rep->uses[1]); |
| 1849 | add_mir->dalvikInsn.arg[0] = SRegToVReg(add_mir->ssa_rep->uses[2]); |
| 1850 | } |
| 1851 | // Original multiply MIR is set to Nop. |
| 1852 | mul_mir->dalvikInsn.opcode = static_cast<Instruction::Code>(kMirOpNop); |
| 1853 | } |
| 1854 | |
| 1855 | void MIRGraph::MultiplyAddOpt(BasicBlock* bb) { |
| 1856 | if (bb->block_type == kDead) { |
| 1857 | return; |
| 1858 | } |
| 1859 | ScopedArenaAllocator allocator(&cu_->arena_stack); |
| 1860 | ScopedArenaSafeMap<uint32_t, MIR*> ssa_mul_map(std::less<uint32_t>(), allocator.Adapter()); |
| 1861 | ScopedArenaSafeMap<uint32_t, MIR*>::iterator map_it; |
| 1862 | for (MIR* mir = bb->first_mir_insn; mir != nullptr; mir = mir->next) { |
| 1863 | Instruction::Code opcode = mir->dalvikInsn.opcode; |
| 1864 | bool is_sub = true; |
| 1865 | bool is_candidate_multiply = false; |
| 1866 | switch (opcode) { |
| 1867 | case Instruction::MUL_INT: |
| 1868 | case Instruction::MUL_INT_2ADDR: |
| 1869 | is_candidate_multiply = true; |
| 1870 | break; |
| 1871 | case Instruction::MUL_LONG: |
| 1872 | case Instruction::MUL_LONG_2ADDR: |
| 1873 | if (cu_->target64) { |
| 1874 | is_candidate_multiply = true; |
| 1875 | } |
| 1876 | break; |
| 1877 | case Instruction::ADD_INT: |
| 1878 | case Instruction::ADD_INT_2ADDR: |
| 1879 | is_sub = false; |
| 1880 | FALLTHROUGH_INTENDED; |
| 1881 | case Instruction::SUB_INT: |
| 1882 | case Instruction::SUB_INT_2ADDR: |
| 1883 | if (((map_it = ssa_mul_map.find(mir->ssa_rep->uses[0])) != ssa_mul_map.end()) && !is_sub) { |
| 1884 | // a*b+c |
| 1885 | CombineMultiplyAdd(map_it->second, mir, true /* product is the first addend */, |
| 1886 | false /* is_wide */, false /* is_sub */); |
| 1887 | ssa_mul_map.erase(mir->ssa_rep->uses[0]); |
| 1888 | } else if ((map_it = ssa_mul_map.find(mir->ssa_rep->uses[1])) != ssa_mul_map.end()) { |
| 1889 | // c+a*b or c-a*b |
| 1890 | CombineMultiplyAdd(map_it->second, mir, false /* product is the second addend */, |
| 1891 | false /* is_wide */, is_sub); |
| 1892 | ssa_mul_map.erase(map_it); |
| 1893 | } |
| 1894 | break; |
| 1895 | case Instruction::ADD_LONG: |
| 1896 | case Instruction::ADD_LONG_2ADDR: |
| 1897 | is_sub = false; |
| 1898 | FALLTHROUGH_INTENDED; |
| 1899 | case Instruction::SUB_LONG: |
| 1900 | case Instruction::SUB_LONG_2ADDR: |
| 1901 | if (!cu_->target64) { |
| 1902 | break; |
| 1903 | } |
| 1904 | if ((map_it = ssa_mul_map.find(mir->ssa_rep->uses[0])) != ssa_mul_map.end() && !is_sub) { |
| 1905 | // a*b+c |
| 1906 | CombineMultiplyAdd(map_it->second, mir, true /* product is the first addend */, |
| 1907 | true /* is_wide */, false /* is_sub */); |
| 1908 | ssa_mul_map.erase(map_it); |
| 1909 | } else if ((map_it = ssa_mul_map.find(mir->ssa_rep->uses[2])) != ssa_mul_map.end()) { |
| 1910 | // c+a*b or c-a*b |
| 1911 | CombineMultiplyAdd(map_it->second, mir, false /* product is the second addend */, |
| 1912 | true /* is_wide */, is_sub); |
| 1913 | ssa_mul_map.erase(map_it); |
| 1914 | } |
| 1915 | break; |
| 1916 | default: |
| 1917 | if (!ssa_mul_map.empty() && CanThrow(mir)) { |
| 1918 | // Should not combine multiply and add MIRs across potential exception. |
| 1919 | ssa_mul_map.clear(); |
| 1920 | } |
| 1921 | break; |
| 1922 | } |
| 1923 | |
| 1924 | // Exclude the case when an MIR writes a vreg which is previous candidate multiply MIR's uses. |
| 1925 | // It is because that current RA may allocate the same physical register to them. For this |
| 1926 | // kind of cases, the multiplier has been updated, we should not use updated value to the |
| 1927 | // multiply-add insn. |
| 1928 | if (ssa_mul_map.size() > 0) { |
| 1929 | for (auto it = ssa_mul_map.begin(); it != ssa_mul_map.end();) { |
| 1930 | MIR* mul = it->second; |
| 1931 | if (HasAntiDependency(mul, mir)) { |
| 1932 | it = ssa_mul_map.erase(it); |
| 1933 | } else { |
| 1934 | ++it; |
| 1935 | } |
| 1936 | } |
| 1937 | } |
| 1938 | |
| 1939 | if (is_candidate_multiply && |
| 1940 | (GetRawUseCount(mir->ssa_rep->defs[0]) == 1) && (mir->next != nullptr)) { |
| 1941 | ssa_mul_map.Put(mir->ssa_rep->defs[0], mir); |
| 1942 | } |
| 1943 | } |
| 1944 | } |
| 1945 | |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 1946 | } // namespace art |