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 | |
| 17 | #include "compiler_internals.h" |
| 18 | #include "local_value_numbering.h" |
Ian Rogers | 8d3a117 | 2013-06-04 01:13:28 -0700 | [diff] [blame] | 19 | #include "dataflow_iterator-inl.h" |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 20 | |
| 21 | namespace art { |
| 22 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 23 | static unsigned int Predecessors(BasicBlock* bb) { |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 24 | return bb->predecessors->Size(); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | /* Setup a constant value for opcodes thare have the DF_SETS_CONST attribute */ |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 28 | void MIRGraph::SetConstant(int32_t ssa_reg, int value) { |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 29 | is_constant_v_->SetBit(ssa_reg); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 30 | constant_values_[ssa_reg] = value; |
| 31 | } |
| 32 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 33 | void MIRGraph::SetConstantWide(int ssa_reg, int64_t value) { |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 34 | is_constant_v_->SetBit(ssa_reg); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 35 | constant_values_[ssa_reg] = Low32Bits(value); |
| 36 | constant_values_[ssa_reg + 1] = High32Bits(value); |
| 37 | } |
| 38 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 39 | void MIRGraph::DoConstantPropogation(BasicBlock* bb) { |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 40 | MIR* mir; |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 41 | |
| 42 | for (mir = bb->first_mir_insn; mir != NULL; mir = mir->next) { |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 43 | int df_attributes = oat_data_flow_attributes_[mir->dalvikInsn.opcode]; |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 44 | |
| 45 | DecodedInstruction *d_insn = &mir->dalvikInsn; |
| 46 | |
| 47 | if (!(df_attributes & DF_HAS_DEFS)) continue; |
| 48 | |
| 49 | /* Handle instructions that set up constants directly */ |
| 50 | if (df_attributes & DF_SETS_CONST) { |
| 51 | if (df_attributes & DF_DA) { |
| 52 | int32_t vB = static_cast<int32_t>(d_insn->vB); |
| 53 | switch (d_insn->opcode) { |
| 54 | case Instruction::CONST_4: |
| 55 | case Instruction::CONST_16: |
| 56 | case Instruction::CONST: |
| 57 | SetConstant(mir->ssa_rep->defs[0], vB); |
| 58 | break; |
| 59 | case Instruction::CONST_HIGH16: |
| 60 | SetConstant(mir->ssa_rep->defs[0], vB << 16); |
| 61 | break; |
| 62 | case Instruction::CONST_WIDE_16: |
| 63 | case Instruction::CONST_WIDE_32: |
| 64 | SetConstantWide(mir->ssa_rep->defs[0], static_cast<int64_t>(vB)); |
| 65 | break; |
| 66 | case Instruction::CONST_WIDE: |
Brian Carlstrom | b1eba21 | 2013-07-17 18:07:19 -0700 | [diff] [blame^] | 67 | SetConstantWide(mir->ssa_rep->defs[0], d_insn->vB_wide); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 68 | break; |
| 69 | case Instruction::CONST_WIDE_HIGH16: |
| 70 | SetConstantWide(mir->ssa_rep->defs[0], static_cast<int64_t>(vB) << 48); |
| 71 | break; |
| 72 | default: |
| 73 | break; |
| 74 | } |
| 75 | } |
| 76 | /* Handle instructions that set up constants directly */ |
| 77 | } else if (df_attributes & DF_IS_MOVE) { |
| 78 | int i; |
| 79 | |
| 80 | for (i = 0; i < mir->ssa_rep->num_uses; i++) { |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 81 | if (!is_constant_v_->IsBitSet(mir->ssa_rep->uses[i])) break; |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 82 | } |
| 83 | /* Move a register holding a constant to another register */ |
| 84 | if (i == mir->ssa_rep->num_uses) { |
| 85 | SetConstant(mir->ssa_rep->defs[0], constant_values_[mir->ssa_rep->uses[0]]); |
| 86 | if (df_attributes & DF_A_WIDE) { |
| 87 | SetConstant(mir->ssa_rep->defs[1], constant_values_[mir->ssa_rep->uses[1]]); |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | /* TODO: implement code to handle arithmetic operations */ |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 93 | } |
| 94 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 95 | void MIRGraph::PropagateConstants() { |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 96 | is_constant_v_ = new (arena_) ArenaBitVector(arena_, GetNumSSARegs(), false); |
| 97 | constant_values_ = static_cast<int*>(arena_->NewMem(sizeof(int) * GetNumSSARegs(), true, |
| 98 | ArenaAllocator::kAllocDFInfo)); |
buzbee | 0665fe0 | 2013-03-21 12:32:21 -0700 | [diff] [blame] | 99 | AllNodesIterator iter(this, false /* not iterative */); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 100 | for (BasicBlock* bb = iter.Next(); bb != NULL; bb = iter.Next()) { |
| 101 | DoConstantPropogation(bb); |
| 102 | } |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | /* Advance to next strictly dominated MIR node in an extended basic block */ |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 106 | static MIR* AdvanceMIR(BasicBlock** p_bb, MIR* mir) { |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 107 | BasicBlock* bb = *p_bb; |
| 108 | if (mir != NULL) { |
| 109 | mir = mir->next; |
| 110 | if (mir == NULL) { |
| 111 | bb = bb->fall_through; |
| 112 | if ((bb == NULL) || Predecessors(bb) != 1) { |
| 113 | mir = NULL; |
| 114 | } else { |
| 115 | *p_bb = bb; |
| 116 | mir = bb->first_mir_insn; |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | return mir; |
| 121 | } |
| 122 | |
| 123 | /* |
| 124 | * To be used at an invoke mir. If the logically next mir node represents |
| 125 | * a move-result, return it. Else, return NULL. If a move-result exists, |
| 126 | * it is required to immediately follow the invoke with no intervening |
| 127 | * opcodes or incoming arcs. However, if the result of the invoke is not |
| 128 | * used, a move-result may not be present. |
| 129 | */ |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 130 | MIR* MIRGraph::FindMoveResult(BasicBlock* bb, MIR* mir) { |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 131 | BasicBlock* tbb = bb; |
| 132 | mir = AdvanceMIR(&tbb, mir); |
| 133 | while (mir != NULL) { |
| 134 | int opcode = mir->dalvikInsn.opcode; |
| 135 | if ((mir->dalvikInsn.opcode == Instruction::MOVE_RESULT) || |
| 136 | (mir->dalvikInsn.opcode == Instruction::MOVE_RESULT_OBJECT) || |
| 137 | (mir->dalvikInsn.opcode == Instruction::MOVE_RESULT_WIDE)) { |
| 138 | break; |
| 139 | } |
| 140 | // Keep going if pseudo op, otherwise terminate |
| 141 | if (opcode < kNumPackedOpcodes) { |
| 142 | mir = NULL; |
| 143 | } else { |
| 144 | mir = AdvanceMIR(&tbb, mir); |
| 145 | } |
| 146 | } |
| 147 | return mir; |
| 148 | } |
| 149 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 150 | static BasicBlock* NextDominatedBlock(BasicBlock* bb) { |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 151 | if (bb->block_type == kDead) { |
| 152 | return NULL; |
| 153 | } |
| 154 | DCHECK((bb->block_type == kEntryBlock) || (bb->block_type == kDalvikByteCode) |
| 155 | || (bb->block_type == kExitBlock)); |
| 156 | bb = bb->fall_through; |
| 157 | if (bb == NULL || (Predecessors(bb) != 1)) { |
| 158 | return NULL; |
| 159 | } |
| 160 | DCHECK((bb->block_type == kDalvikByteCode) || (bb->block_type == kExitBlock)); |
| 161 | return bb; |
| 162 | } |
| 163 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 164 | static MIR* FindPhi(BasicBlock* bb, int ssa_name) { |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 165 | for (MIR* mir = bb->first_mir_insn; mir != NULL; mir = mir->next) { |
| 166 | if (static_cast<int>(mir->dalvikInsn.opcode) == kMirOpPhi) { |
| 167 | for (int i = 0; i < mir->ssa_rep->num_uses; i++) { |
| 168 | if (mir->ssa_rep->uses[i] == ssa_name) { |
| 169 | return mir; |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | return NULL; |
| 175 | } |
| 176 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 177 | static SelectInstructionKind SelectKind(MIR* mir) { |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 178 | switch (mir->dalvikInsn.opcode) { |
| 179 | case Instruction::MOVE: |
| 180 | case Instruction::MOVE_OBJECT: |
| 181 | case Instruction::MOVE_16: |
| 182 | case Instruction::MOVE_OBJECT_16: |
| 183 | case Instruction::MOVE_FROM16: |
| 184 | case Instruction::MOVE_OBJECT_FROM16: |
| 185 | return kSelectMove; |
| 186 | case Instruction::CONST: |
| 187 | case Instruction::CONST_4: |
| 188 | case Instruction::CONST_16: |
| 189 | return kSelectConst; |
| 190 | case Instruction::GOTO: |
| 191 | case Instruction::GOTO_16: |
| 192 | case Instruction::GOTO_32: |
| 193 | return kSelectGoto; |
| 194 | default:; |
| 195 | } |
| 196 | return kSelectNone; |
| 197 | } |
| 198 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 199 | int MIRGraph::GetSSAUseCount(int s_reg) { |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 200 | return raw_use_counts_.Get(s_reg); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | |
| 204 | /* Do some MIR-level extended basic block optimizations */ |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 205 | bool MIRGraph::BasicBlockOpt(BasicBlock* bb) { |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 206 | if (bb->block_type == kDead) { |
| 207 | return true; |
| 208 | } |
| 209 | int num_temps = 0; |
| 210 | LocalValueNumbering local_valnum(cu_); |
| 211 | while (bb != NULL) { |
| 212 | for (MIR* mir = bb->first_mir_insn; mir != NULL; mir = mir->next) { |
| 213 | // TUNING: use the returned value number for CSE. |
| 214 | local_valnum.GetValueNumber(mir); |
| 215 | // Look for interesting opcodes, skip otherwise |
| 216 | Instruction::Code opcode = mir->dalvikInsn.opcode; |
| 217 | switch (opcode) { |
| 218 | case Instruction::CMPL_FLOAT: |
| 219 | case Instruction::CMPL_DOUBLE: |
| 220 | case Instruction::CMPG_FLOAT: |
| 221 | case Instruction::CMPG_DOUBLE: |
| 222 | case Instruction::CMP_LONG: |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 223 | if ((cu_->disable_opt & (1 << kBranchFusing)) != 0) { |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 224 | // Bitcode doesn't allow this optimization. |
| 225 | break; |
| 226 | } |
| 227 | if (mir->next != NULL) { |
| 228 | MIR* mir_next = mir->next; |
| 229 | Instruction::Code br_opcode = mir_next->dalvikInsn.opcode; |
| 230 | ConditionCode ccode = kCondNv; |
| 231 | switch(br_opcode) { |
| 232 | case Instruction::IF_EQZ: |
| 233 | ccode = kCondEq; |
| 234 | break; |
| 235 | case Instruction::IF_NEZ: |
| 236 | ccode = kCondNe; |
| 237 | break; |
| 238 | case Instruction::IF_LTZ: |
| 239 | ccode = kCondLt; |
| 240 | break; |
| 241 | case Instruction::IF_GEZ: |
| 242 | ccode = kCondGe; |
| 243 | break; |
| 244 | case Instruction::IF_GTZ: |
| 245 | ccode = kCondGt; |
| 246 | break; |
| 247 | case Instruction::IF_LEZ: |
| 248 | ccode = kCondLe; |
| 249 | break; |
| 250 | default: |
| 251 | break; |
| 252 | } |
| 253 | // Make sure result of cmp is used by next insn and nowhere else |
| 254 | if ((ccode != kCondNv) && |
| 255 | (mir->ssa_rep->defs[0] == mir_next->ssa_rep->uses[0]) && |
| 256 | (GetSSAUseCount(mir->ssa_rep->defs[0]) == 1)) { |
| 257 | mir_next->dalvikInsn.arg[0] = ccode; |
| 258 | switch(opcode) { |
| 259 | case Instruction::CMPL_FLOAT: |
| 260 | mir_next->dalvikInsn.opcode = |
| 261 | static_cast<Instruction::Code>(kMirOpFusedCmplFloat); |
| 262 | break; |
| 263 | case Instruction::CMPL_DOUBLE: |
| 264 | mir_next->dalvikInsn.opcode = |
| 265 | static_cast<Instruction::Code>(kMirOpFusedCmplDouble); |
| 266 | break; |
| 267 | case Instruction::CMPG_FLOAT: |
| 268 | mir_next->dalvikInsn.opcode = |
| 269 | static_cast<Instruction::Code>(kMirOpFusedCmpgFloat); |
| 270 | break; |
| 271 | case Instruction::CMPG_DOUBLE: |
| 272 | mir_next->dalvikInsn.opcode = |
| 273 | static_cast<Instruction::Code>(kMirOpFusedCmpgDouble); |
| 274 | break; |
| 275 | case Instruction::CMP_LONG: |
| 276 | mir_next->dalvikInsn.opcode = |
| 277 | static_cast<Instruction::Code>(kMirOpFusedCmpLong); |
| 278 | break; |
| 279 | default: LOG(ERROR) << "Unexpected opcode: " << opcode; |
| 280 | } |
| 281 | mir->dalvikInsn.opcode = static_cast<Instruction::Code>(kMirOpNop); |
| 282 | mir_next->ssa_rep->num_uses = mir->ssa_rep->num_uses; |
| 283 | mir_next->ssa_rep->uses = mir->ssa_rep->uses; |
| 284 | mir_next->ssa_rep->fp_use = mir->ssa_rep->fp_use; |
| 285 | mir_next->ssa_rep->num_defs = 0; |
| 286 | mir->ssa_rep->num_uses = 0; |
| 287 | mir->ssa_rep->num_defs = 0; |
| 288 | } |
| 289 | } |
| 290 | break; |
| 291 | case Instruction::GOTO: |
| 292 | case Instruction::GOTO_16: |
| 293 | case Instruction::GOTO_32: |
| 294 | case Instruction::IF_EQ: |
| 295 | case Instruction::IF_NE: |
| 296 | case Instruction::IF_LT: |
| 297 | case Instruction::IF_GE: |
| 298 | case Instruction::IF_GT: |
| 299 | case Instruction::IF_LE: |
| 300 | case Instruction::IF_EQZ: |
| 301 | case Instruction::IF_NEZ: |
| 302 | case Instruction::IF_LTZ: |
| 303 | case Instruction::IF_GEZ: |
| 304 | case Instruction::IF_GTZ: |
| 305 | case Instruction::IF_LEZ: |
| 306 | if (bb->taken->dominates_return) { |
| 307 | mir->optimization_flags |= MIR_IGNORE_SUSPEND_CHECK; |
| 308 | if (cu_->verbose) { |
| 309 | LOG(INFO) << "Suppressed suspend check on branch to return at 0x" << std::hex << mir->offset; |
| 310 | } |
| 311 | } |
| 312 | break; |
| 313 | default: |
| 314 | break; |
| 315 | } |
| 316 | // Is this the select pattern? |
| 317 | // TODO: flesh out support for Mips and X86. NOTE: llvm's select op doesn't quite work here. |
| 318 | // TUNING: expand to support IF_xx compare & branches |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 319 | if (!(cu_->compiler_backend == kPortable) && (cu_->instruction_set == kThumb2) && |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 320 | ((mir->dalvikInsn.opcode == Instruction::IF_EQZ) || |
| 321 | (mir->dalvikInsn.opcode == Instruction::IF_NEZ))) { |
| 322 | BasicBlock* ft = bb->fall_through; |
| 323 | DCHECK(ft != NULL); |
| 324 | BasicBlock* ft_ft = ft->fall_through; |
| 325 | BasicBlock* ft_tk = ft->taken; |
| 326 | |
| 327 | BasicBlock* tk = bb->taken; |
| 328 | DCHECK(tk != NULL); |
| 329 | BasicBlock* tk_ft = tk->fall_through; |
| 330 | BasicBlock* tk_tk = tk->taken; |
| 331 | |
| 332 | /* |
| 333 | * In the select pattern, the taken edge goes to a block that unconditionally |
| 334 | * transfers to the rejoin block and the fall_though edge goes to a block that |
| 335 | * unconditionally falls through to the rejoin block. |
| 336 | */ |
| 337 | if ((tk_ft == NULL) && (ft_tk == NULL) && (tk_tk == ft_ft) && |
| 338 | (Predecessors(tk) == 1) && (Predecessors(ft) == 1)) { |
| 339 | /* |
| 340 | * Okay - we have the basic diamond shape. At the very least, we can eliminate the |
| 341 | * suspend check on the taken-taken branch back to the join point. |
| 342 | */ |
| 343 | if (SelectKind(tk->last_mir_insn) == kSelectGoto) { |
| 344 | tk->last_mir_insn->optimization_flags |= (MIR_IGNORE_SUSPEND_CHECK); |
| 345 | } |
| 346 | // Are the block bodies something we can handle? |
| 347 | if ((ft->first_mir_insn == ft->last_mir_insn) && |
| 348 | (tk->first_mir_insn != tk->last_mir_insn) && |
| 349 | (tk->first_mir_insn->next == tk->last_mir_insn) && |
| 350 | ((SelectKind(ft->first_mir_insn) == kSelectMove) || |
| 351 | (SelectKind(ft->first_mir_insn) == kSelectConst)) && |
| 352 | (SelectKind(ft->first_mir_insn) == SelectKind(tk->first_mir_insn)) && |
| 353 | (SelectKind(tk->last_mir_insn) == kSelectGoto)) { |
| 354 | // Almost there. Are the instructions targeting the same vreg? |
| 355 | MIR* if_true = tk->first_mir_insn; |
| 356 | MIR* if_false = ft->first_mir_insn; |
| 357 | // It's possible that the target of the select isn't used - skip those (rare) cases. |
| 358 | MIR* phi = FindPhi(tk_tk, if_true->ssa_rep->defs[0]); |
| 359 | if ((phi != NULL) && (if_true->dalvikInsn.vA == if_false->dalvikInsn.vA)) { |
| 360 | /* |
| 361 | * We'll convert the IF_EQZ/IF_NEZ to a SELECT. We need to find the |
| 362 | * Phi node in the merge block and delete it (while using the SSA name |
| 363 | * of the merge as the target of the SELECT. Delete both taken and |
| 364 | * fallthrough blocks, and set fallthrough to merge block. |
| 365 | * NOTE: not updating other dataflow info (no longer used at this point). |
| 366 | * If this changes, need to update i_dom, etc. here (and in CombineBlocks). |
| 367 | */ |
| 368 | if (opcode == Instruction::IF_NEZ) { |
| 369 | // Normalize. |
| 370 | MIR* tmp_mir = if_true; |
| 371 | if_true = if_false; |
| 372 | if_false = tmp_mir; |
| 373 | } |
| 374 | mir->dalvikInsn.opcode = static_cast<Instruction::Code>(kMirOpSelect); |
| 375 | bool const_form = (SelectKind(if_true) == kSelectConst); |
| 376 | if ((SelectKind(if_true) == kSelectMove)) { |
| 377 | if (IsConst(if_true->ssa_rep->uses[0]) && |
| 378 | IsConst(if_false->ssa_rep->uses[0])) { |
| 379 | const_form = true; |
| 380 | if_true->dalvikInsn.vB = ConstantValue(if_true->ssa_rep->uses[0]); |
| 381 | if_false->dalvikInsn.vB = ConstantValue(if_false->ssa_rep->uses[0]); |
| 382 | } |
| 383 | } |
| 384 | if (const_form) { |
| 385 | // "true" set val in vB |
| 386 | mir->dalvikInsn.vB = if_true->dalvikInsn.vB; |
| 387 | // "false" set val in vC |
| 388 | mir->dalvikInsn.vC = if_false->dalvikInsn.vB; |
| 389 | } else { |
| 390 | DCHECK_EQ(SelectKind(if_true), kSelectMove); |
| 391 | DCHECK_EQ(SelectKind(if_false), kSelectMove); |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 392 | int* src_ssa = |
| 393 | static_cast<int*>(arena_->NewMem(sizeof(int) * 3, false, |
| 394 | ArenaAllocator::kAllocDFInfo)); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 395 | src_ssa[0] = mir->ssa_rep->uses[0]; |
| 396 | src_ssa[1] = if_true->ssa_rep->uses[0]; |
| 397 | src_ssa[2] = if_false->ssa_rep->uses[0]; |
| 398 | mir->ssa_rep->uses = src_ssa; |
| 399 | mir->ssa_rep->num_uses = 3; |
| 400 | } |
| 401 | mir->ssa_rep->num_defs = 1; |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 402 | mir->ssa_rep->defs = |
| 403 | static_cast<int*>(arena_->NewMem(sizeof(int) * 1, false, |
| 404 | ArenaAllocator::kAllocDFInfo)); |
| 405 | mir->ssa_rep->fp_def = |
| 406 | static_cast<bool*>(arena_->NewMem(sizeof(bool) * 1, false, |
| 407 | ArenaAllocator::kAllocDFInfo)); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 408 | mir->ssa_rep->fp_def[0] = if_true->ssa_rep->fp_def[0]; |
buzbee | 817e45a | 2013-05-30 18:59:12 -0700 | [diff] [blame] | 409 | // Match type of uses to def. |
| 410 | mir->ssa_rep->fp_use = |
| 411 | static_cast<bool*>(arena_->NewMem(sizeof(bool) * mir->ssa_rep->num_uses, false, |
| 412 | ArenaAllocator::kAllocDFInfo)); |
| 413 | for (int i = 0; i < mir->ssa_rep->num_uses; i++) { |
| 414 | mir->ssa_rep->fp_use[i] = mir->ssa_rep->fp_def[0]; |
| 415 | } |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 416 | /* |
| 417 | * There is usually a Phi node in the join block for our two cases. If the |
| 418 | * Phi node only contains our two cases as input, we will use the result |
| 419 | * SSA name of the Phi node as our select result and delete the Phi. If |
| 420 | * the Phi node has more than two operands, we will arbitrarily use the SSA |
| 421 | * name of the "true" path, delete the SSA name of the "false" path from the |
| 422 | * Phi node (and fix up the incoming arc list). |
| 423 | */ |
| 424 | if (phi->ssa_rep->num_uses == 2) { |
| 425 | mir->ssa_rep->defs[0] = phi->ssa_rep->defs[0]; |
| 426 | phi->dalvikInsn.opcode = static_cast<Instruction::Code>(kMirOpNop); |
| 427 | } else { |
| 428 | int dead_def = if_false->ssa_rep->defs[0]; |
| 429 | int live_def = if_true->ssa_rep->defs[0]; |
| 430 | mir->ssa_rep->defs[0] = live_def; |
| 431 | int* incoming = reinterpret_cast<int*>(phi->dalvikInsn.vB); |
| 432 | for (int i = 0; i < phi->ssa_rep->num_uses; i++) { |
| 433 | if (phi->ssa_rep->uses[i] == live_def) { |
| 434 | incoming[i] = bb->id; |
| 435 | } |
| 436 | } |
| 437 | for (int i = 0; i < phi->ssa_rep->num_uses; i++) { |
| 438 | if (phi->ssa_rep->uses[i] == dead_def) { |
| 439 | int last_slot = phi->ssa_rep->num_uses - 1; |
| 440 | phi->ssa_rep->uses[i] = phi->ssa_rep->uses[last_slot]; |
| 441 | incoming[i] = incoming[last_slot]; |
| 442 | } |
| 443 | } |
| 444 | } |
| 445 | phi->ssa_rep->num_uses--; |
| 446 | bb->taken = NULL; |
| 447 | tk->block_type = kDead; |
| 448 | for (MIR* tmir = ft->first_mir_insn; tmir != NULL; tmir = tmir->next) { |
| 449 | tmir->dalvikInsn.opcode = static_cast<Instruction::Code>(kMirOpNop); |
| 450 | } |
| 451 | } |
| 452 | } |
| 453 | } |
| 454 | } |
| 455 | } |
| 456 | bb = NextDominatedBlock(bb); |
| 457 | } |
| 458 | |
| 459 | if (num_temps > cu_->num_compiler_temps) { |
| 460 | cu_->num_compiler_temps = num_temps; |
| 461 | } |
| 462 | return true; |
| 463 | } |
| 464 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 465 | void MIRGraph::NullCheckEliminationInit(struct BasicBlock* bb) { |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 466 | if (bb->data_flow_info != NULL) { |
| 467 | bb->data_flow_info->ending_null_check_v = |
| 468 | new (arena_) ArenaBitVector(arena_, GetNumSSARegs(), false, kBitMapNullCheck); |
| 469 | } |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | /* Collect stats on number of checks removed */ |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 473 | void MIRGraph::CountChecks(struct BasicBlock* bb) { |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 474 | if (bb->data_flow_info != NULL) { |
| 475 | for (MIR* mir = bb->first_mir_insn; mir != NULL; mir = mir->next) { |
| 476 | if (mir->ssa_rep == NULL) { |
| 477 | continue; |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 478 | } |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 479 | int df_attributes = oat_data_flow_attributes_[mir->dalvikInsn.opcode]; |
| 480 | if (df_attributes & DF_HAS_NULL_CHKS) { |
| 481 | checkstats_->null_checks++; |
| 482 | if (mir->optimization_flags & MIR_IGNORE_NULL_CHECK) { |
| 483 | checkstats_->null_checks_eliminated++; |
| 484 | } |
| 485 | } |
| 486 | if (df_attributes & DF_HAS_RANGE_CHKS) { |
| 487 | checkstats_->range_checks++; |
| 488 | if (mir->optimization_flags & MIR_IGNORE_RANGE_CHECK) { |
| 489 | checkstats_->range_checks_eliminated++; |
| 490 | } |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 491 | } |
| 492 | } |
| 493 | } |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | /* Try to make common case the fallthrough path */ |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 497 | static bool LayoutBlocks(struct BasicBlock* bb) { |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 498 | // TODO: For now, just looking for direct throws. Consider generalizing for profile feedback |
| 499 | if (!bb->explicit_throw) { |
| 500 | return false; |
| 501 | } |
| 502 | BasicBlock* walker = bb; |
| 503 | while (true) { |
| 504 | // Check termination conditions |
| 505 | if ((walker->block_type == kEntryBlock) || (Predecessors(walker) != 1)) { |
| 506 | break; |
| 507 | } |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 508 | BasicBlock* prev = walker->predecessors->Get(0); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 509 | if (prev->conditional_branch) { |
| 510 | if (prev->fall_through == walker) { |
| 511 | // Already done - return |
| 512 | break; |
| 513 | } |
| 514 | DCHECK_EQ(walker, prev->taken); |
| 515 | // Got one. Flip it and exit |
| 516 | Instruction::Code opcode = prev->last_mir_insn->dalvikInsn.opcode; |
| 517 | switch (opcode) { |
| 518 | case Instruction::IF_EQ: opcode = Instruction::IF_NE; break; |
| 519 | case Instruction::IF_NE: opcode = Instruction::IF_EQ; break; |
| 520 | case Instruction::IF_LT: opcode = Instruction::IF_GE; break; |
| 521 | case Instruction::IF_GE: opcode = Instruction::IF_LT; break; |
| 522 | case Instruction::IF_GT: opcode = Instruction::IF_LE; break; |
| 523 | case Instruction::IF_LE: opcode = Instruction::IF_GT; break; |
| 524 | case Instruction::IF_EQZ: opcode = Instruction::IF_NEZ; break; |
| 525 | case Instruction::IF_NEZ: opcode = Instruction::IF_EQZ; break; |
| 526 | case Instruction::IF_LTZ: opcode = Instruction::IF_GEZ; break; |
| 527 | case Instruction::IF_GEZ: opcode = Instruction::IF_LTZ; break; |
| 528 | case Instruction::IF_GTZ: opcode = Instruction::IF_LEZ; break; |
| 529 | case Instruction::IF_LEZ: opcode = Instruction::IF_GTZ; break; |
| 530 | default: LOG(FATAL) << "Unexpected opcode " << opcode; |
| 531 | } |
| 532 | prev->last_mir_insn->dalvikInsn.opcode = opcode; |
| 533 | BasicBlock* t_bb = prev->taken; |
| 534 | prev->taken = prev->fall_through; |
| 535 | prev->fall_through = t_bb; |
| 536 | break; |
| 537 | } |
| 538 | walker = prev; |
| 539 | } |
| 540 | return false; |
| 541 | } |
| 542 | |
| 543 | /* Combine any basic blocks terminated by instructions that we now know can't throw */ |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 544 | bool MIRGraph::CombineBlocks(struct BasicBlock* bb) { |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 545 | // Loop here to allow combining a sequence of blocks |
| 546 | while (true) { |
| 547 | // Check termination conditions |
| 548 | if ((bb->first_mir_insn == NULL) |
| 549 | || (bb->data_flow_info == NULL) |
| 550 | || (bb->block_type == kExceptionHandling) |
| 551 | || (bb->block_type == kExitBlock) |
| 552 | || (bb->block_type == kDead) |
| 553 | || ((bb->taken == NULL) || (bb->taken->block_type != kExceptionHandling)) |
| 554 | || (bb->successor_block_list.block_list_type != kNotUsed) |
| 555 | || (static_cast<int>(bb->last_mir_insn->dalvikInsn.opcode) != kMirOpCheck)) { |
| 556 | break; |
| 557 | } |
| 558 | |
| 559 | // Test the kMirOpCheck instruction |
| 560 | MIR* mir = bb->last_mir_insn; |
| 561 | // Grab the attributes from the paired opcode |
| 562 | MIR* throw_insn = mir->meta.throw_insn; |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 563 | int df_attributes = oat_data_flow_attributes_[throw_insn->dalvikInsn.opcode]; |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 564 | bool can_combine = true; |
| 565 | if (df_attributes & DF_HAS_NULL_CHKS) { |
| 566 | can_combine &= ((throw_insn->optimization_flags & MIR_IGNORE_NULL_CHECK) != 0); |
| 567 | } |
| 568 | if (df_attributes & DF_HAS_RANGE_CHKS) { |
| 569 | can_combine &= ((throw_insn->optimization_flags & MIR_IGNORE_RANGE_CHECK) != 0); |
| 570 | } |
| 571 | if (!can_combine) { |
| 572 | break; |
| 573 | } |
| 574 | // OK - got one. Combine |
| 575 | BasicBlock* bb_next = bb->fall_through; |
| 576 | DCHECK(!bb_next->catch_entry); |
| 577 | DCHECK_EQ(Predecessors(bb_next), 1U); |
| 578 | MIR* t_mir = bb->last_mir_insn->prev; |
| 579 | // Overwrite the kOpCheck insn with the paired opcode |
| 580 | DCHECK_EQ(bb_next->first_mir_insn, throw_insn); |
| 581 | *bb->last_mir_insn = *throw_insn; |
| 582 | bb->last_mir_insn->prev = t_mir; |
| 583 | // Use the successor info from the next block |
| 584 | bb->successor_block_list = bb_next->successor_block_list; |
| 585 | // Use the ending block linkage from the next block |
| 586 | bb->fall_through = bb_next->fall_through; |
| 587 | bb->taken->block_type = kDead; // Kill the unused exception block |
| 588 | bb->taken = bb_next->taken; |
| 589 | // Include the rest of the instructions |
| 590 | bb->last_mir_insn = bb_next->last_mir_insn; |
| 591 | /* |
| 592 | * If lower-half of pair of blocks to combine contained a return, move the flag |
| 593 | * to the newly combined block. |
| 594 | */ |
| 595 | bb->terminated_by_return = bb_next->terminated_by_return; |
| 596 | |
| 597 | /* |
| 598 | * NOTE: we aren't updating all dataflow info here. Should either make sure this pass |
| 599 | * happens after uses of i_dominated, dom_frontier or update the dataflow info here. |
| 600 | */ |
| 601 | |
| 602 | // Kill bb_next and remap now-dead id to parent |
| 603 | bb_next->block_type = kDead; |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 604 | block_id_map_.Overwrite(bb_next->id, bb->id); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 605 | |
| 606 | // Now, loop back and see if we can keep going |
| 607 | } |
| 608 | return false; |
| 609 | } |
| 610 | |
| 611 | /* Eliminate unnecessary null checks for a basic block. */ |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 612 | bool MIRGraph::EliminateNullChecks(struct BasicBlock* bb) { |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 613 | if (bb->data_flow_info == NULL) return false; |
| 614 | |
| 615 | /* |
| 616 | * Set initial state. Be conservative with catch |
| 617 | * blocks and start with no assumptions about null check |
| 618 | * status (except for "this"). |
| 619 | */ |
| 620 | if ((bb->block_type == kEntryBlock) | bb->catch_entry) { |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 621 | temp_ssa_register_v_->ClearAllBits(); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 622 | if ((cu_->access_flags & kAccStatic) == 0) { |
| 623 | // If non-static method, mark "this" as non-null |
| 624 | int this_reg = cu_->num_dalvik_registers - cu_->num_ins; |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 625 | temp_ssa_register_v_->SetBit(this_reg); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 626 | } |
Ian Rogers | 22fd6a0 | 2013-06-13 15:06:54 -0700 | [diff] [blame] | 627 | } else if (bb->predecessors->Size() == 1) { |
| 628 | BasicBlock* pred_bb = bb->predecessors->Get(0); |
| 629 | temp_ssa_register_v_->Copy(pred_bb->data_flow_info->ending_null_check_v); |
| 630 | if (pred_bb->block_type == kDalvikByteCode) { |
| 631 | // Check to see if predecessor had an explicit null-check. |
| 632 | MIR* last_insn = pred_bb->last_mir_insn; |
| 633 | Instruction::Code last_opcode = last_insn->dalvikInsn.opcode; |
| 634 | if (last_opcode == Instruction::IF_EQZ) { |
| 635 | if (pred_bb->fall_through == bb) { |
| 636 | // The fall-through of a block following a IF_EQZ, set the vA of the IF_EQZ to show that |
| 637 | // it can't be null. |
| 638 | temp_ssa_register_v_->SetBit(last_insn->ssa_rep->uses[0]); |
| 639 | } |
| 640 | } else if (last_opcode == Instruction::IF_NEZ) { |
| 641 | if (pred_bb->taken == bb) { |
| 642 | // The taken block following a IF_NEZ, set the vA of the IF_NEZ to show that it can't be |
| 643 | // null. |
| 644 | temp_ssa_register_v_->SetBit(last_insn->ssa_rep->uses[0]); |
| 645 | } |
| 646 | } |
| 647 | } |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 648 | } else { |
Ian Rogers | 22fd6a0 | 2013-06-13 15:06:54 -0700 | [diff] [blame] | 649 | // Starting state is intersection of all incoming arcs |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 650 | GrowableArray<BasicBlock*>::Iterator iter(bb->predecessors); |
| 651 | BasicBlock* pred_bb = iter.Next(); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 652 | DCHECK(pred_bb != NULL); |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 653 | temp_ssa_register_v_->Copy(pred_bb->data_flow_info->ending_null_check_v); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 654 | while (true) { |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 655 | pred_bb = iter.Next(); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 656 | if (!pred_bb) break; |
| 657 | if ((pred_bb->data_flow_info == NULL) || |
| 658 | (pred_bb->data_flow_info->ending_null_check_v == NULL)) { |
| 659 | continue; |
| 660 | } |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 661 | temp_ssa_register_v_->Intersect(pred_bb->data_flow_info->ending_null_check_v); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 662 | } |
| 663 | } |
| 664 | |
| 665 | // Walk through the instruction in the block, updating as necessary |
| 666 | for (MIR* mir = bb->first_mir_insn; mir != NULL; mir = mir->next) { |
| 667 | if (mir->ssa_rep == NULL) { |
| 668 | continue; |
| 669 | } |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 670 | int df_attributes = oat_data_flow_attributes_[mir->dalvikInsn.opcode]; |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 671 | |
| 672 | // Mark target of NEW* as non-null |
| 673 | if (df_attributes & DF_NON_NULL_DST) { |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 674 | temp_ssa_register_v_->SetBit(mir->ssa_rep->defs[0]); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 675 | } |
| 676 | |
| 677 | // Mark non-null returns from invoke-style NEW* |
| 678 | if (df_attributes & DF_NON_NULL_RET) { |
| 679 | MIR* next_mir = mir->next; |
| 680 | // Next should be an MOVE_RESULT_OBJECT |
| 681 | if (next_mir && |
| 682 | next_mir->dalvikInsn.opcode == Instruction::MOVE_RESULT_OBJECT) { |
| 683 | // Mark as null checked |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 684 | temp_ssa_register_v_->SetBit(next_mir->ssa_rep->defs[0]); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 685 | } else { |
| 686 | if (next_mir) { |
| 687 | LOG(WARNING) << "Unexpected opcode following new: " << next_mir->dalvikInsn.opcode; |
| 688 | } else if (bb->fall_through) { |
| 689 | // Look in next basic block |
| 690 | struct BasicBlock* next_bb = bb->fall_through; |
| 691 | for (MIR* tmir = next_bb->first_mir_insn; tmir != NULL; |
| 692 | tmir =tmir->next) { |
| 693 | if (static_cast<int>(tmir->dalvikInsn.opcode) >= static_cast<int>(kMirOpFirst)) { |
| 694 | continue; |
| 695 | } |
| 696 | // First non-pseudo should be MOVE_RESULT_OBJECT |
| 697 | if (tmir->dalvikInsn.opcode == Instruction::MOVE_RESULT_OBJECT) { |
| 698 | // Mark as null checked |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 699 | temp_ssa_register_v_->SetBit(tmir->ssa_rep->defs[0]); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 700 | } else { |
| 701 | LOG(WARNING) << "Unexpected op after new: " << tmir->dalvikInsn.opcode; |
| 702 | } |
| 703 | break; |
| 704 | } |
| 705 | } |
| 706 | } |
| 707 | } |
| 708 | |
| 709 | /* |
| 710 | * Propagate nullcheck state on register copies (including |
| 711 | * Phi pseudo copies. For the latter, nullcheck state is |
| 712 | * the "and" of all the Phi's operands. |
| 713 | */ |
| 714 | if (df_attributes & (DF_NULL_TRANSFER_0 | DF_NULL_TRANSFER_N)) { |
| 715 | int tgt_sreg = mir->ssa_rep->defs[0]; |
| 716 | int operands = (df_attributes & DF_NULL_TRANSFER_0) ? 1 : |
| 717 | mir->ssa_rep->num_uses; |
| 718 | bool null_checked = true; |
| 719 | for (int i = 0; i < operands; i++) { |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 720 | null_checked &= temp_ssa_register_v_->IsBitSet(mir->ssa_rep->uses[i]); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 721 | } |
| 722 | if (null_checked) { |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 723 | temp_ssa_register_v_->SetBit(tgt_sreg); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 724 | } |
| 725 | } |
| 726 | |
| 727 | // Already nullchecked? |
| 728 | if ((df_attributes & DF_HAS_NULL_CHKS) && !(mir->optimization_flags & MIR_IGNORE_NULL_CHECK)) { |
| 729 | int src_idx; |
| 730 | if (df_attributes & DF_NULL_CHK_1) { |
| 731 | src_idx = 1; |
| 732 | } else if (df_attributes & DF_NULL_CHK_2) { |
| 733 | src_idx = 2; |
| 734 | } else { |
| 735 | src_idx = 0; |
| 736 | } |
| 737 | int src_sreg = mir->ssa_rep->uses[src_idx]; |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 738 | if (temp_ssa_register_v_->IsBitSet(src_sreg)) { |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 739 | // Eliminate the null check |
| 740 | mir->optimization_flags |= MIR_IGNORE_NULL_CHECK; |
| 741 | } else { |
| 742 | // Mark s_reg as null-checked |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 743 | temp_ssa_register_v_->SetBit(src_sreg); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 744 | } |
| 745 | } |
| 746 | } |
| 747 | |
| 748 | // Did anything change? |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 749 | bool changed = !temp_ssa_register_v_->Equal(bb->data_flow_info->ending_null_check_v); |
| 750 | if (changed) { |
| 751 | bb->data_flow_info->ending_null_check_v->Copy(temp_ssa_register_v_); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 752 | } |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 753 | return changed; |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 754 | } |
| 755 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 756 | void MIRGraph::NullCheckElimination() { |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 757 | if (!(cu_->disable_opt & (1 << kNullCheckElimination))) { |
| 758 | DCHECK(temp_ssa_register_v_ != NULL); |
buzbee | 0665fe0 | 2013-03-21 12:32:21 -0700 | [diff] [blame] | 759 | AllNodesIterator iter(this, false /* not iterative */); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 760 | for (BasicBlock* bb = iter.Next(); bb != NULL; bb = iter.Next()) { |
| 761 | NullCheckEliminationInit(bb); |
| 762 | } |
buzbee | 0665fe0 | 2013-03-21 12:32:21 -0700 | [diff] [blame] | 763 | PreOrderDfsIterator iter2(this, true /* iterative */); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 764 | bool change = false; |
| 765 | for (BasicBlock* bb = iter2.Next(change); bb != NULL; bb = iter2.Next(change)) { |
| 766 | change = EliminateNullChecks(bb); |
| 767 | } |
| 768 | } |
| 769 | if (cu_->enable_debug & (1 << kDebugDumpCFG)) { |
| 770 | DumpCFG("/sdcard/4_post_nce_cfg/", false); |
| 771 | } |
| 772 | } |
| 773 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 774 | void MIRGraph::BasicBlockCombine() { |
buzbee | 0665fe0 | 2013-03-21 12:32:21 -0700 | [diff] [blame] | 775 | PreOrderDfsIterator iter(this, false /* not iterative */); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 776 | for (BasicBlock* bb = iter.Next(); bb != NULL; bb = iter.Next()) { |
| 777 | CombineBlocks(bb); |
| 778 | } |
| 779 | if (cu_->enable_debug & (1 << kDebugDumpCFG)) { |
| 780 | DumpCFG("/sdcard/5_post_bbcombine_cfg/", false); |
| 781 | } |
| 782 | } |
| 783 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 784 | void MIRGraph::CodeLayout() { |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 785 | if (cu_->enable_debug & (1 << kDebugVerifyDataflow)) { |
| 786 | VerifyDataflow(); |
| 787 | } |
buzbee | 0665fe0 | 2013-03-21 12:32:21 -0700 | [diff] [blame] | 788 | AllNodesIterator iter(this, false /* not iterative */); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 789 | for (BasicBlock* bb = iter.Next(); bb != NULL; bb = iter.Next()) { |
| 790 | LayoutBlocks(bb); |
| 791 | } |
| 792 | if (cu_->enable_debug & (1 << kDebugDumpCFG)) { |
| 793 | DumpCFG("/sdcard/2_post_layout_cfg/", true); |
| 794 | } |
| 795 | } |
| 796 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 797 | void MIRGraph::DumpCheckStats() { |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 798 | Checkstats* stats = |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 799 | static_cast<Checkstats*>(arena_->NewMem(sizeof(Checkstats), true, |
| 800 | ArenaAllocator::kAllocDFInfo)); |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 801 | checkstats_ = stats; |
buzbee | 0665fe0 | 2013-03-21 12:32:21 -0700 | [diff] [blame] | 802 | AllNodesIterator iter(this, false /* not iterative */); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 803 | for (BasicBlock* bb = iter.Next(); bb != NULL; bb = iter.Next()) { |
| 804 | CountChecks(bb); |
| 805 | } |
| 806 | if (stats->null_checks > 0) { |
| 807 | float eliminated = static_cast<float>(stats->null_checks_eliminated); |
| 808 | float checks = static_cast<float>(stats->null_checks); |
| 809 | LOG(INFO) << "Null Checks: " << PrettyMethod(cu_->method_idx, *cu_->dex_file) << " " |
| 810 | << stats->null_checks_eliminated << " of " << stats->null_checks << " -> " |
| 811 | << (eliminated/checks) * 100.0 << "%"; |
| 812 | } |
| 813 | if (stats->range_checks > 0) { |
| 814 | float eliminated = static_cast<float>(stats->range_checks_eliminated); |
| 815 | float checks = static_cast<float>(stats->range_checks); |
| 816 | LOG(INFO) << "Range Checks: " << PrettyMethod(cu_->method_idx, *cu_->dex_file) << " " |
| 817 | << stats->range_checks_eliminated << " of " << stats->range_checks << " -> " |
| 818 | << (eliminated/checks) * 100.0 << "%"; |
| 819 | } |
| 820 | } |
| 821 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 822 | bool MIRGraph::BuildExtendedBBList(struct BasicBlock* bb) { |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 823 | if (bb->visited) return false; |
| 824 | if (!((bb->block_type == kEntryBlock) || (bb->block_type == kDalvikByteCode) |
| 825 | || (bb->block_type == kExitBlock))) { |
| 826 | // Ignore special blocks |
| 827 | bb->visited = true; |
| 828 | return false; |
| 829 | } |
| 830 | // Must be head of extended basic block. |
| 831 | BasicBlock* start_bb = bb; |
| 832 | extended_basic_blocks_.push_back(bb); |
| 833 | bool terminated_by_return = false; |
| 834 | // Visit blocks strictly dominated by this head. |
| 835 | while (bb != NULL) { |
| 836 | bb->visited = true; |
| 837 | terminated_by_return |= bb->terminated_by_return; |
| 838 | bb = NextDominatedBlock(bb); |
| 839 | } |
| 840 | if (terminated_by_return) { |
| 841 | // This extended basic block contains a return, so mark all members. |
| 842 | bb = start_bb; |
| 843 | while (bb != NULL) { |
| 844 | bb->dominates_return = true; |
| 845 | bb = NextDominatedBlock(bb); |
| 846 | } |
| 847 | } |
| 848 | return false; // Not iterative - return value will be ignored |
| 849 | } |
| 850 | |
| 851 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 852 | void MIRGraph::BasicBlockOptimization() { |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 853 | if (!(cu_->disable_opt & (1 << kBBOpt))) { |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 854 | DCHECK_EQ(cu_->num_compiler_temps, 0); |
buzbee | a5abf70 | 2013-04-12 14:39:29 -0700 | [diff] [blame] | 855 | ClearAllVisitedFlags(); |
buzbee | 0665fe0 | 2013-03-21 12:32:21 -0700 | [diff] [blame] | 856 | PreOrderDfsIterator iter2(this, false /* not iterative */); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 857 | for (BasicBlock* bb = iter2.Next(); bb != NULL; bb = iter2.Next()) { |
| 858 | BuildExtendedBBList(bb); |
| 859 | } |
| 860 | // Perform extended basic block optimizations. |
| 861 | for (unsigned int i = 0; i < extended_basic_blocks_.size(); i++) { |
| 862 | BasicBlockOpt(extended_basic_blocks_[i]); |
| 863 | } |
| 864 | } |
| 865 | if (cu_->enable_debug & (1 << kDebugDumpCFG)) { |
| 866 | DumpCFG("/sdcard/6_post_bbo_cfg/", false); |
| 867 | } |
| 868 | } |
| 869 | |
| 870 | } // namespace art |