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