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