blob: 3cd158ffc06bcfc318f0eb9a59efe7668e2cd1b6 [file] [log] [blame]
buzbee311ca162013-02-28 15:56:43 -08001/*
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 Rogers8d3a1172013-06-04 01:13:28 -070019#include "dataflow_iterator-inl.h"
buzbee311ca162013-02-28 15:56:43 -080020
21namespace art {
22
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070023static unsigned int Predecessors(BasicBlock* bb) {
buzbee862a7602013-04-05 10:58:54 -070024 return bb->predecessors->Size();
buzbee311ca162013-02-28 15:56:43 -080025}
26
27/* Setup a constant value for opcodes thare have the DF_SETS_CONST attribute */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070028void MIRGraph::SetConstant(int32_t ssa_reg, int value) {
buzbee862a7602013-04-05 10:58:54 -070029 is_constant_v_->SetBit(ssa_reg);
buzbee311ca162013-02-28 15:56:43 -080030 constant_values_[ssa_reg] = value;
31}
32
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070033void MIRGraph::SetConstantWide(int ssa_reg, int64_t value) {
buzbee862a7602013-04-05 10:58:54 -070034 is_constant_v_->SetBit(ssa_reg);
buzbee311ca162013-02-28 15:56:43 -080035 constant_values_[ssa_reg] = Low32Bits(value);
36 constant_values_[ssa_reg + 1] = High32Bits(value);
37}
38
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070039void MIRGraph::DoConstantPropogation(BasicBlock* bb) {
buzbee311ca162013-02-28 15:56:43 -080040 MIR* mir;
buzbee311ca162013-02-28 15:56:43 -080041
42 for (mir = bb->first_mir_insn; mir != NULL; mir = mir->next) {
buzbee1fd33462013-03-25 13:40:45 -070043 int df_attributes = oat_data_flow_attributes_[mir->dalvikInsn.opcode];
buzbee311ca162013-02-28 15:56:43 -080044
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 Carlstromb1eba212013-07-17 18:07:19 -070067 SetConstantWide(mir->ssa_rep->defs[0], d_insn->vB_wide);
buzbee311ca162013-02-28 15:56:43 -080068 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++) {
buzbee862a7602013-04-05 10:58:54 -070081 if (!is_constant_v_->IsBitSet(mir->ssa_rep->uses[i])) break;
buzbee311ca162013-02-28 15:56:43 -080082 }
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 */
buzbee311ca162013-02-28 15:56:43 -080093}
94
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070095void MIRGraph::PropagateConstants() {
buzbee862a7602013-04-05 10:58:54 -070096 is_constant_v_ = new (arena_) ArenaBitVector(arena_, GetNumSSARegs(), false);
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -070097 constant_values_ = static_cast<int*>(arena_->Alloc(sizeof(int) * GetNumSSARegs(),
98 ArenaAllocator::kAllocDFInfo));
buzbee56c71782013-09-05 17:13:19 -070099 AllNodesIterator iter(this);
buzbee311ca162013-02-28 15:56:43 -0800100 for (BasicBlock* bb = iter.Next(); bb != NULL; bb = iter.Next()) {
101 DoConstantPropogation(bb);
102 }
buzbee311ca162013-02-28 15:56:43 -0800103}
104
105/* Advance to next strictly dominated MIR node in an extended basic block */
buzbee0d829482013-10-11 15:24:55 -0700106MIR* MIRGraph::AdvanceMIR(BasicBlock** p_bb, MIR* mir) {
buzbee311ca162013-02-28 15:56:43 -0800107 BasicBlock* bb = *p_bb;
108 if (mir != NULL) {
109 mir = mir->next;
110 if (mir == NULL) {
buzbee0d829482013-10-11 15:24:55 -0700111 bb = GetBasicBlock(bb->fall_through);
buzbee311ca162013-02-28 15:56:43 -0800112 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 Carlstrom2ce745c2013-07-17 17:44:30 -0700130MIR* MIRGraph::FindMoveResult(BasicBlock* bb, MIR* mir) {
buzbee311ca162013-02-28 15:56:43 -0800131 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
buzbee0d829482013-10-11 15:24:55 -0700150BasicBlock* MIRGraph::NextDominatedBlock(BasicBlock* bb) {
buzbee311ca162013-02-28 15:56:43 -0800151 if (bb->block_type == kDead) {
152 return NULL;
153 }
154 DCHECK((bb->block_type == kEntryBlock) || (bb->block_type == kDalvikByteCode)
155 || (bb->block_type == kExitBlock));
buzbee0d829482013-10-11 15:24:55 -0700156 BasicBlock* bb_taken = GetBasicBlock(bb->taken);
157 BasicBlock* bb_fall_through = GetBasicBlock(bb->fall_through);
158 if (((bb_taken != NULL) && (bb_fall_through == NULL)) &&
159 ((bb_taken->block_type == kDalvikByteCode) || (bb_taken->block_type == kExitBlock))) {
buzbeecbcfaf32013-08-19 07:37:40 -0700160 // Follow simple unconditional branches.
buzbee0d829482013-10-11 15:24:55 -0700161 bb = bb_taken;
buzbeecbcfaf32013-08-19 07:37:40 -0700162 } else {
163 // Follow simple fallthrough
buzbee0d829482013-10-11 15:24:55 -0700164 bb = (bb_taken != NULL) ? NULL : bb_fall_through;
buzbeecbcfaf32013-08-19 07:37:40 -0700165 }
buzbee311ca162013-02-28 15:56:43 -0800166 if (bb == NULL || (Predecessors(bb) != 1)) {
167 return NULL;
168 }
169 DCHECK((bb->block_type == kDalvikByteCode) || (bb->block_type == kExitBlock));
170 return bb;
171}
172
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700173static MIR* FindPhi(BasicBlock* bb, int ssa_name) {
buzbee311ca162013-02-28 15:56:43 -0800174 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
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700186static SelectInstructionKind SelectKind(MIR* mir) {
buzbee311ca162013-02-28 15:56:43 -0800187 switch (mir->dalvikInsn.opcode) {
188 case Instruction::MOVE:
189 case Instruction::MOVE_OBJECT:
190 case Instruction::MOVE_16:
191 case Instruction::MOVE_OBJECT_16:
192 case Instruction::MOVE_FROM16:
193 case Instruction::MOVE_OBJECT_FROM16:
194 return kSelectMove;
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700195 case Instruction::CONST:
196 case Instruction::CONST_4:
197 case Instruction::CONST_16:
buzbee311ca162013-02-28 15:56:43 -0800198 return kSelectConst;
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700199 case Instruction::GOTO:
200 case Instruction::GOTO_16:
201 case Instruction::GOTO_32:
buzbee311ca162013-02-28 15:56:43 -0800202 return kSelectGoto;
Brian Carlstrom02c8cc62013-07-18 15:54:44 -0700203 default:
204 return kSelectNone;
buzbee311ca162013-02-28 15:56:43 -0800205 }
buzbee311ca162013-02-28 15:56:43 -0800206}
207
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700208int MIRGraph::GetSSAUseCount(int s_reg) {
buzbee862a7602013-04-05 10:58:54 -0700209 return raw_use_counts_.Get(s_reg);
buzbee311ca162013-02-28 15:56:43 -0800210}
211
212
213/* Do some MIR-level extended basic block optimizations */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700214bool MIRGraph::BasicBlockOpt(BasicBlock* bb) {
buzbee311ca162013-02-28 15:56:43 -0800215 if (bb->block_type == kDead) {
216 return true;
217 }
218 int num_temps = 0;
219 LocalValueNumbering local_valnum(cu_);
220 while (bb != NULL) {
221 for (MIR* mir = bb->first_mir_insn; mir != NULL; mir = mir->next) {
222 // TUNING: use the returned value number for CSE.
223 local_valnum.GetValueNumber(mir);
224 // Look for interesting opcodes, skip otherwise
225 Instruction::Code opcode = mir->dalvikInsn.opcode;
226 switch (opcode) {
227 case Instruction::CMPL_FLOAT:
228 case Instruction::CMPL_DOUBLE:
229 case Instruction::CMPG_FLOAT:
230 case Instruction::CMPG_DOUBLE:
231 case Instruction::CMP_LONG:
buzbee1fd33462013-03-25 13:40:45 -0700232 if ((cu_->disable_opt & (1 << kBranchFusing)) != 0) {
buzbee311ca162013-02-28 15:56:43 -0800233 // Bitcode doesn't allow this optimization.
234 break;
235 }
236 if (mir->next != NULL) {
237 MIR* mir_next = mir->next;
238 Instruction::Code br_opcode = mir_next->dalvikInsn.opcode;
239 ConditionCode ccode = kCondNv;
Brian Carlstromdf629502013-07-17 22:39:56 -0700240 switch (br_opcode) {
buzbee311ca162013-02-28 15:56:43 -0800241 case Instruction::IF_EQZ:
242 ccode = kCondEq;
243 break;
244 case Instruction::IF_NEZ:
245 ccode = kCondNe;
246 break;
247 case Instruction::IF_LTZ:
248 ccode = kCondLt;
249 break;
250 case Instruction::IF_GEZ:
251 ccode = kCondGe;
252 break;
253 case Instruction::IF_GTZ:
254 ccode = kCondGt;
255 break;
256 case Instruction::IF_LEZ:
257 ccode = kCondLe;
258 break;
259 default:
260 break;
261 }
262 // Make sure result of cmp is used by next insn and nowhere else
263 if ((ccode != kCondNv) &&
264 (mir->ssa_rep->defs[0] == mir_next->ssa_rep->uses[0]) &&
265 (GetSSAUseCount(mir->ssa_rep->defs[0]) == 1)) {
266 mir_next->dalvikInsn.arg[0] = ccode;
Brian Carlstromdf629502013-07-17 22:39:56 -0700267 switch (opcode) {
buzbee311ca162013-02-28 15:56:43 -0800268 case Instruction::CMPL_FLOAT:
269 mir_next->dalvikInsn.opcode =
270 static_cast<Instruction::Code>(kMirOpFusedCmplFloat);
271 break;
272 case Instruction::CMPL_DOUBLE:
273 mir_next->dalvikInsn.opcode =
274 static_cast<Instruction::Code>(kMirOpFusedCmplDouble);
275 break;
276 case Instruction::CMPG_FLOAT:
277 mir_next->dalvikInsn.opcode =
278 static_cast<Instruction::Code>(kMirOpFusedCmpgFloat);
279 break;
280 case Instruction::CMPG_DOUBLE:
281 mir_next->dalvikInsn.opcode =
282 static_cast<Instruction::Code>(kMirOpFusedCmpgDouble);
283 break;
284 case Instruction::CMP_LONG:
285 mir_next->dalvikInsn.opcode =
286 static_cast<Instruction::Code>(kMirOpFusedCmpLong);
287 break;
288 default: LOG(ERROR) << "Unexpected opcode: " << opcode;
289 }
290 mir->dalvikInsn.opcode = static_cast<Instruction::Code>(kMirOpNop);
291 mir_next->ssa_rep->num_uses = mir->ssa_rep->num_uses;
292 mir_next->ssa_rep->uses = mir->ssa_rep->uses;
293 mir_next->ssa_rep->fp_use = mir->ssa_rep->fp_use;
294 mir_next->ssa_rep->num_defs = 0;
295 mir->ssa_rep->num_uses = 0;
296 mir->ssa_rep->num_defs = 0;
297 }
298 }
299 break;
300 case Instruction::GOTO:
301 case Instruction::GOTO_16:
302 case Instruction::GOTO_32:
303 case Instruction::IF_EQ:
304 case Instruction::IF_NE:
305 case Instruction::IF_LT:
306 case Instruction::IF_GE:
307 case Instruction::IF_GT:
308 case Instruction::IF_LE:
309 case Instruction::IF_EQZ:
310 case Instruction::IF_NEZ:
311 case Instruction::IF_LTZ:
312 case Instruction::IF_GEZ:
313 case Instruction::IF_GTZ:
314 case Instruction::IF_LEZ:
buzbeecbcfaf32013-08-19 07:37:40 -0700315 // If we've got a backwards branch to return, no need to suspend check.
buzbee0d829482013-10-11 15:24:55 -0700316 if ((IsBackedge(bb, bb->taken) && GetBasicBlock(bb->taken)->dominates_return) ||
317 (IsBackedge(bb, bb->fall_through) &&
318 GetBasicBlock(bb->fall_through)->dominates_return)) {
buzbee311ca162013-02-28 15:56:43 -0800319 mir->optimization_flags |= MIR_IGNORE_SUSPEND_CHECK;
320 if (cu_->verbose) {
buzbee0d829482013-10-11 15:24:55 -0700321 LOG(INFO) << "Suppressed suspend check on branch to return at 0x" << std::hex
322 << mir->offset;
buzbee311ca162013-02-28 15:56:43 -0800323 }
324 }
325 break;
326 default:
327 break;
328 }
329 // Is this the select pattern?
330 // TODO: flesh out support for Mips and X86. NOTE: llvm's select op doesn't quite work here.
331 // TUNING: expand to support IF_xx compare & branches
buzbee409fe942013-10-11 10:49:56 -0700332 if (!(cu_->compiler_backend == kPortable) && (cu_->instruction_set == kThumb2) &&
buzbee311ca162013-02-28 15:56:43 -0800333 ((mir->dalvikInsn.opcode == Instruction::IF_EQZ) ||
334 (mir->dalvikInsn.opcode == Instruction::IF_NEZ))) {
buzbee0d829482013-10-11 15:24:55 -0700335 BasicBlock* ft = GetBasicBlock(bb->fall_through);
buzbee311ca162013-02-28 15:56:43 -0800336 DCHECK(ft != NULL);
buzbee0d829482013-10-11 15:24:55 -0700337 BasicBlock* ft_ft = GetBasicBlock(ft->fall_through);
338 BasicBlock* ft_tk = GetBasicBlock(ft->taken);
buzbee311ca162013-02-28 15:56:43 -0800339
buzbee0d829482013-10-11 15:24:55 -0700340 BasicBlock* tk = GetBasicBlock(bb->taken);
buzbee311ca162013-02-28 15:56:43 -0800341 DCHECK(tk != NULL);
buzbee0d829482013-10-11 15:24:55 -0700342 BasicBlock* tk_ft = GetBasicBlock(tk->fall_through);
343 BasicBlock* tk_tk = GetBasicBlock(tk->taken);
buzbee311ca162013-02-28 15:56:43 -0800344
345 /*
346 * In the select pattern, the taken edge goes to a block that unconditionally
347 * transfers to the rejoin block and the fall_though edge goes to a block that
348 * unconditionally falls through to the rejoin block.
349 */
350 if ((tk_ft == NULL) && (ft_tk == NULL) && (tk_tk == ft_ft) &&
351 (Predecessors(tk) == 1) && (Predecessors(ft) == 1)) {
352 /*
353 * Okay - we have the basic diamond shape. At the very least, we can eliminate the
354 * suspend check on the taken-taken branch back to the join point.
355 */
356 if (SelectKind(tk->last_mir_insn) == kSelectGoto) {
357 tk->last_mir_insn->optimization_flags |= (MIR_IGNORE_SUSPEND_CHECK);
358 }
359 // Are the block bodies something we can handle?
360 if ((ft->first_mir_insn == ft->last_mir_insn) &&
361 (tk->first_mir_insn != tk->last_mir_insn) &&
362 (tk->first_mir_insn->next == tk->last_mir_insn) &&
363 ((SelectKind(ft->first_mir_insn) == kSelectMove) ||
364 (SelectKind(ft->first_mir_insn) == kSelectConst)) &&
365 (SelectKind(ft->first_mir_insn) == SelectKind(tk->first_mir_insn)) &&
366 (SelectKind(tk->last_mir_insn) == kSelectGoto)) {
367 // Almost there. Are the instructions targeting the same vreg?
368 MIR* if_true = tk->first_mir_insn;
369 MIR* if_false = ft->first_mir_insn;
370 // It's possible that the target of the select isn't used - skip those (rare) cases.
371 MIR* phi = FindPhi(tk_tk, if_true->ssa_rep->defs[0]);
372 if ((phi != NULL) && (if_true->dalvikInsn.vA == if_false->dalvikInsn.vA)) {
373 /*
374 * We'll convert the IF_EQZ/IF_NEZ to a SELECT. We need to find the
375 * Phi node in the merge block and delete it (while using the SSA name
376 * of the merge as the target of the SELECT. Delete both taken and
377 * fallthrough blocks, and set fallthrough to merge block.
378 * NOTE: not updating other dataflow info (no longer used at this point).
379 * If this changes, need to update i_dom, etc. here (and in CombineBlocks).
380 */
381 if (opcode == Instruction::IF_NEZ) {
382 // Normalize.
383 MIR* tmp_mir = if_true;
384 if_true = if_false;
385 if_false = tmp_mir;
386 }
387 mir->dalvikInsn.opcode = static_cast<Instruction::Code>(kMirOpSelect);
388 bool const_form = (SelectKind(if_true) == kSelectConst);
389 if ((SelectKind(if_true) == kSelectMove)) {
390 if (IsConst(if_true->ssa_rep->uses[0]) &&
391 IsConst(if_false->ssa_rep->uses[0])) {
392 const_form = true;
393 if_true->dalvikInsn.vB = ConstantValue(if_true->ssa_rep->uses[0]);
394 if_false->dalvikInsn.vB = ConstantValue(if_false->ssa_rep->uses[0]);
395 }
396 }
397 if (const_form) {
398 // "true" set val in vB
399 mir->dalvikInsn.vB = if_true->dalvikInsn.vB;
400 // "false" set val in vC
401 mir->dalvikInsn.vC = if_false->dalvikInsn.vB;
402 } else {
403 DCHECK_EQ(SelectKind(if_true), kSelectMove);
404 DCHECK_EQ(SelectKind(if_false), kSelectMove);
buzbee862a7602013-04-05 10:58:54 -0700405 int* src_ssa =
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700406 static_cast<int*>(arena_->Alloc(sizeof(int) * 3, ArenaAllocator::kAllocDFInfo));
buzbee311ca162013-02-28 15:56:43 -0800407 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;
buzbee862a7602013-04-05 10:58:54 -0700414 mir->ssa_rep->defs =
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700415 static_cast<int*>(arena_->Alloc(sizeof(int) * 1, ArenaAllocator::kAllocDFInfo));
buzbee862a7602013-04-05 10:58:54 -0700416 mir->ssa_rep->fp_def =
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700417 static_cast<bool*>(arena_->Alloc(sizeof(bool) * 1, ArenaAllocator::kAllocDFInfo));
buzbee311ca162013-02-28 15:56:43 -0800418 mir->ssa_rep->fp_def[0] = if_true->ssa_rep->fp_def[0];
buzbee817e45a2013-05-30 18:59:12 -0700419 // Match type of uses to def.
420 mir->ssa_rep->fp_use =
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700421 static_cast<bool*>(arena_->Alloc(sizeof(bool) * mir->ssa_rep->num_uses,
422 ArenaAllocator::kAllocDFInfo));
buzbee817e45a2013-05-30 18:59:12 -0700423 for (int i = 0; i < mir->ssa_rep->num_uses; i++) {
424 mir->ssa_rep->fp_use[i] = mir->ssa_rep->fp_def[0];
425 }
buzbee311ca162013-02-28 15:56:43 -0800426 /*
427 * There is usually a Phi node in the join block for our two cases. If the
428 * Phi node only contains our two cases as input, we will use the result
429 * SSA name of the Phi node as our select result and delete the Phi. If
430 * the Phi node has more than two operands, we will arbitrarily use the SSA
431 * name of the "true" path, delete the SSA name of the "false" path from the
432 * Phi node (and fix up the incoming arc list).
433 */
434 if (phi->ssa_rep->num_uses == 2) {
435 mir->ssa_rep->defs[0] = phi->ssa_rep->defs[0];
436 phi->dalvikInsn.opcode = static_cast<Instruction::Code>(kMirOpNop);
437 } else {
438 int dead_def = if_false->ssa_rep->defs[0];
439 int live_def = if_true->ssa_rep->defs[0];
440 mir->ssa_rep->defs[0] = live_def;
buzbee0d829482013-10-11 15:24:55 -0700441 BasicBlockId* incoming = phi->meta.phi_incoming;
buzbee311ca162013-02-28 15:56:43 -0800442 for (int i = 0; i < phi->ssa_rep->num_uses; i++) {
443 if (phi->ssa_rep->uses[i] == live_def) {
444 incoming[i] = bb->id;
445 }
446 }
447 for (int i = 0; i < phi->ssa_rep->num_uses; i++) {
448 if (phi->ssa_rep->uses[i] == dead_def) {
449 int last_slot = phi->ssa_rep->num_uses - 1;
450 phi->ssa_rep->uses[i] = phi->ssa_rep->uses[last_slot];
451 incoming[i] = incoming[last_slot];
452 }
453 }
454 }
455 phi->ssa_rep->num_uses--;
buzbee0d829482013-10-11 15:24:55 -0700456 bb->taken = NullBasicBlockId;
buzbee311ca162013-02-28 15:56:43 -0800457 tk->block_type = kDead;
458 for (MIR* tmir = ft->first_mir_insn; tmir != NULL; tmir = tmir->next) {
459 tmir->dalvikInsn.opcode = static_cast<Instruction::Code>(kMirOpNop);
460 }
461 }
462 }
463 }
464 }
465 }
466 bb = NextDominatedBlock(bb);
467 }
468
469 if (num_temps > cu_->num_compiler_temps) {
470 cu_->num_compiler_temps = num_temps;
471 }
472 return true;
473}
474
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700475void MIRGraph::NullCheckEliminationInit(struct BasicBlock* bb) {
buzbee862a7602013-04-05 10:58:54 -0700476 if (bb->data_flow_info != NULL) {
477 bb->data_flow_info->ending_null_check_v =
478 new (arena_) ArenaBitVector(arena_, GetNumSSARegs(), false, kBitMapNullCheck);
479 }
buzbee311ca162013-02-28 15:56:43 -0800480}
481
482/* Collect stats on number of checks removed */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700483void MIRGraph::CountChecks(struct BasicBlock* bb) {
buzbee862a7602013-04-05 10:58:54 -0700484 if (bb->data_flow_info != NULL) {
485 for (MIR* mir = bb->first_mir_insn; mir != NULL; mir = mir->next) {
486 if (mir->ssa_rep == NULL) {
487 continue;
buzbee311ca162013-02-28 15:56:43 -0800488 }
buzbee862a7602013-04-05 10:58:54 -0700489 int df_attributes = oat_data_flow_attributes_[mir->dalvikInsn.opcode];
490 if (df_attributes & DF_HAS_NULL_CHKS) {
491 checkstats_->null_checks++;
492 if (mir->optimization_flags & MIR_IGNORE_NULL_CHECK) {
493 checkstats_->null_checks_eliminated++;
494 }
495 }
496 if (df_attributes & DF_HAS_RANGE_CHKS) {
497 checkstats_->range_checks++;
498 if (mir->optimization_flags & MIR_IGNORE_RANGE_CHECK) {
499 checkstats_->range_checks_eliminated++;
500 }
buzbee311ca162013-02-28 15:56:43 -0800501 }
502 }
503 }
buzbee311ca162013-02-28 15:56:43 -0800504}
505
506/* Try to make common case the fallthrough path */
buzbee0d829482013-10-11 15:24:55 -0700507bool MIRGraph::LayoutBlocks(BasicBlock* bb) {
buzbee311ca162013-02-28 15:56:43 -0800508 // TODO: For now, just looking for direct throws. Consider generalizing for profile feedback
509 if (!bb->explicit_throw) {
510 return false;
511 }
512 BasicBlock* walker = bb;
513 while (true) {
514 // Check termination conditions
515 if ((walker->block_type == kEntryBlock) || (Predecessors(walker) != 1)) {
516 break;
517 }
buzbee0d829482013-10-11 15:24:55 -0700518 BasicBlock* prev = GetBasicBlock(walker->predecessors->Get(0));
buzbee311ca162013-02-28 15:56:43 -0800519 if (prev->conditional_branch) {
buzbee0d829482013-10-11 15:24:55 -0700520 if (GetBasicBlock(prev->fall_through) == walker) {
buzbee311ca162013-02-28 15:56:43 -0800521 // Already done - return
522 break;
523 }
buzbee0d829482013-10-11 15:24:55 -0700524 DCHECK_EQ(walker, GetBasicBlock(prev->taken));
buzbee311ca162013-02-28 15:56:43 -0800525 // Got one. Flip it and exit
526 Instruction::Code opcode = prev->last_mir_insn->dalvikInsn.opcode;
527 switch (opcode) {
528 case Instruction::IF_EQ: opcode = Instruction::IF_NE; break;
529 case Instruction::IF_NE: opcode = Instruction::IF_EQ; break;
530 case Instruction::IF_LT: opcode = Instruction::IF_GE; break;
531 case Instruction::IF_GE: opcode = Instruction::IF_LT; break;
532 case Instruction::IF_GT: opcode = Instruction::IF_LE; break;
533 case Instruction::IF_LE: opcode = Instruction::IF_GT; break;
534 case Instruction::IF_EQZ: opcode = Instruction::IF_NEZ; break;
535 case Instruction::IF_NEZ: opcode = Instruction::IF_EQZ; break;
536 case Instruction::IF_LTZ: opcode = Instruction::IF_GEZ; break;
537 case Instruction::IF_GEZ: opcode = Instruction::IF_LTZ; break;
538 case Instruction::IF_GTZ: opcode = Instruction::IF_LEZ; break;
539 case Instruction::IF_LEZ: opcode = Instruction::IF_GTZ; break;
540 default: LOG(FATAL) << "Unexpected opcode " << opcode;
541 }
542 prev->last_mir_insn->dalvikInsn.opcode = opcode;
buzbee0d829482013-10-11 15:24:55 -0700543 BasicBlockId t_bb = prev->taken;
buzbee311ca162013-02-28 15:56:43 -0800544 prev->taken = prev->fall_through;
545 prev->fall_through = t_bb;
546 break;
547 }
548 walker = prev;
549 }
550 return false;
551}
552
553/* Combine any basic blocks terminated by instructions that we now know can't throw */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700554bool MIRGraph::CombineBlocks(struct BasicBlock* bb) {
buzbee311ca162013-02-28 15:56:43 -0800555 // Loop here to allow combining a sequence of blocks
556 while (true) {
557 // Check termination conditions
558 if ((bb->first_mir_insn == NULL)
559 || (bb->data_flow_info == NULL)
560 || (bb->block_type == kExceptionHandling)
561 || (bb->block_type == kExitBlock)
562 || (bb->block_type == kDead)
buzbee0d829482013-10-11 15:24:55 -0700563 || (bb->taken == NullBasicBlockId)
564 || (GetBasicBlock(bb->taken)->block_type != kExceptionHandling)
565 || (bb->successor_block_list_type != kNotUsed)
buzbee311ca162013-02-28 15:56:43 -0800566 || (static_cast<int>(bb->last_mir_insn->dalvikInsn.opcode) != kMirOpCheck)) {
567 break;
568 }
569
570 // Test the kMirOpCheck instruction
571 MIR* mir = bb->last_mir_insn;
572 // Grab the attributes from the paired opcode
573 MIR* throw_insn = mir->meta.throw_insn;
buzbee1fd33462013-03-25 13:40:45 -0700574 int df_attributes = oat_data_flow_attributes_[throw_insn->dalvikInsn.opcode];
buzbee311ca162013-02-28 15:56:43 -0800575 bool can_combine = true;
576 if (df_attributes & DF_HAS_NULL_CHKS) {
577 can_combine &= ((throw_insn->optimization_flags & MIR_IGNORE_NULL_CHECK) != 0);
578 }
579 if (df_attributes & DF_HAS_RANGE_CHKS) {
580 can_combine &= ((throw_insn->optimization_flags & MIR_IGNORE_RANGE_CHECK) != 0);
581 }
582 if (!can_combine) {
583 break;
584 }
585 // OK - got one. Combine
buzbee0d829482013-10-11 15:24:55 -0700586 BasicBlock* bb_next = GetBasicBlock(bb->fall_through);
buzbee311ca162013-02-28 15:56:43 -0800587 DCHECK(!bb_next->catch_entry);
588 DCHECK_EQ(Predecessors(bb_next), 1U);
buzbee311ca162013-02-28 15:56:43 -0800589 // Overwrite the kOpCheck insn with the paired opcode
590 DCHECK_EQ(bb_next->first_mir_insn, throw_insn);
591 *bb->last_mir_insn = *throw_insn;
buzbee311ca162013-02-28 15:56:43 -0800592 // Use the successor info from the next block
buzbee0d829482013-10-11 15:24:55 -0700593 bb->successor_block_list_type = bb_next->successor_block_list_type;
594 bb->successor_blocks = bb_next->successor_blocks;
buzbee311ca162013-02-28 15:56:43 -0800595 // Use the ending block linkage from the next block
596 bb->fall_through = bb_next->fall_through;
buzbee0d829482013-10-11 15:24:55 -0700597 GetBasicBlock(bb->taken)->block_type = kDead; // Kill the unused exception block
buzbee311ca162013-02-28 15:56:43 -0800598 bb->taken = bb_next->taken;
599 // Include the rest of the instructions
600 bb->last_mir_insn = bb_next->last_mir_insn;
601 /*
602 * If lower-half of pair of blocks to combine contained a return, move the flag
603 * to the newly combined block.
604 */
605 bb->terminated_by_return = bb_next->terminated_by_return;
606
607 /*
608 * NOTE: we aren't updating all dataflow info here. Should either make sure this pass
609 * happens after uses of i_dominated, dom_frontier or update the dataflow info here.
610 */
611
612 // Kill bb_next and remap now-dead id to parent
613 bb_next->block_type = kDead;
buzbee1fd33462013-03-25 13:40:45 -0700614 block_id_map_.Overwrite(bb_next->id, bb->id);
buzbee311ca162013-02-28 15:56:43 -0800615
616 // Now, loop back and see if we can keep going
617 }
618 return false;
619}
620
621/* Eliminate unnecessary null checks for a basic block. */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700622bool MIRGraph::EliminateNullChecks(struct BasicBlock* bb) {
buzbee311ca162013-02-28 15:56:43 -0800623 if (bb->data_flow_info == NULL) return false;
624
625 /*
626 * Set initial state. Be conservative with catch
627 * blocks and start with no assumptions about null check
628 * status (except for "this").
629 */
630 if ((bb->block_type == kEntryBlock) | bb->catch_entry) {
buzbee862a7602013-04-05 10:58:54 -0700631 temp_ssa_register_v_->ClearAllBits();
buzbee311ca162013-02-28 15:56:43 -0800632 if ((cu_->access_flags & kAccStatic) == 0) {
633 // If non-static method, mark "this" as non-null
634 int this_reg = cu_->num_dalvik_registers - cu_->num_ins;
Ian Rogers31aa97c2013-10-25 23:07:29 +0000635 temp_ssa_register_v_->SetBit(this_reg);
buzbee311ca162013-02-28 15:56:43 -0800636 }
Ian Rogers22fd6a02013-06-13 15:06:54 -0700637 } else if (bb->predecessors->Size() == 1) {
buzbee0d829482013-10-11 15:24:55 -0700638 BasicBlock* pred_bb = GetBasicBlock(bb->predecessors->Get(0));
Ian Rogers22fd6a02013-06-13 15:06:54 -0700639 temp_ssa_register_v_->Copy(pred_bb->data_flow_info->ending_null_check_v);
640 if (pred_bb->block_type == kDalvikByteCode) {
641 // Check to see if predecessor had an explicit null-check.
642 MIR* last_insn = pred_bb->last_mir_insn;
643 Instruction::Code last_opcode = last_insn->dalvikInsn.opcode;
644 if (last_opcode == Instruction::IF_EQZ) {
buzbee0d829482013-10-11 15:24:55 -0700645 if (pred_bb->fall_through == bb->id) {
Ian Rogers22fd6a02013-06-13 15:06:54 -0700646 // The fall-through of a block following a IF_EQZ, set the vA of the IF_EQZ to show that
647 // it can't be null.
Ian Rogers31aa97c2013-10-25 23:07:29 +0000648 temp_ssa_register_v_->SetBit(last_insn->ssa_rep->uses[0]);
Ian Rogers22fd6a02013-06-13 15:06:54 -0700649 }
650 } else if (last_opcode == Instruction::IF_NEZ) {
buzbee0d829482013-10-11 15:24:55 -0700651 if (pred_bb->taken == bb->id) {
Ian Rogers22fd6a02013-06-13 15:06:54 -0700652 // The taken block following a IF_NEZ, set the vA of the IF_NEZ to show that it can't be
653 // null.
Ian Rogers31aa97c2013-10-25 23:07:29 +0000654 temp_ssa_register_v_->SetBit(last_insn->ssa_rep->uses[0]);
Ian Rogers22fd6a02013-06-13 15:06:54 -0700655 }
656 }
657 }
buzbee311ca162013-02-28 15:56:43 -0800658 } else {
Ian Rogers31aa97c2013-10-25 23:07:29 +0000659 // Starting state is intersection of all incoming arcs
buzbee0d829482013-10-11 15:24:55 -0700660 GrowableArray<BasicBlockId>::Iterator iter(bb->predecessors);
661 BasicBlock* pred_bb = GetBasicBlock(iter.Next());
buzbee311ca162013-02-28 15:56:43 -0800662 DCHECK(pred_bb != NULL);
buzbee862a7602013-04-05 10:58:54 -0700663 temp_ssa_register_v_->Copy(pred_bb->data_flow_info->ending_null_check_v);
buzbee311ca162013-02-28 15:56:43 -0800664 while (true) {
buzbee0d829482013-10-11 15:24:55 -0700665 pred_bb = GetBasicBlock(iter.Next());
buzbee311ca162013-02-28 15:56:43 -0800666 if (!pred_bb) break;
667 if ((pred_bb->data_flow_info == NULL) ||
668 (pred_bb->data_flow_info->ending_null_check_v == NULL)) {
669 continue;
670 }
Ian Rogers31aa97c2013-10-25 23:07:29 +0000671 temp_ssa_register_v_->Intersect(pred_bb->data_flow_info->ending_null_check_v);
buzbee311ca162013-02-28 15:56:43 -0800672 }
673 }
674
675 // Walk through the instruction in the block, updating as necessary
676 for (MIR* mir = bb->first_mir_insn; mir != NULL; mir = mir->next) {
677 if (mir->ssa_rep == NULL) {
678 continue;
679 }
buzbee1fd33462013-03-25 13:40:45 -0700680 int df_attributes = oat_data_flow_attributes_[mir->dalvikInsn.opcode];
buzbee311ca162013-02-28 15:56:43 -0800681
Ian Rogers31aa97c2013-10-25 23:07:29 +0000682 // Mark target of NEW* as non-null
buzbee4db179d2013-10-23 12:16:39 -0700683 if (df_attributes & DF_NON_NULL_DST) {
Ian Rogers31aa97c2013-10-25 23:07:29 +0000684 temp_ssa_register_v_->SetBit(mir->ssa_rep->defs[0]);
buzbee4db179d2013-10-23 12:16:39 -0700685 }
686
buzbee311ca162013-02-28 15:56:43 -0800687 // Mark non-null returns from invoke-style NEW*
688 if (df_attributes & DF_NON_NULL_RET) {
689 MIR* next_mir = mir->next;
690 // Next should be an MOVE_RESULT_OBJECT
691 if (next_mir &&
692 next_mir->dalvikInsn.opcode == Instruction::MOVE_RESULT_OBJECT) {
693 // Mark as null checked
Ian Rogers31aa97c2013-10-25 23:07:29 +0000694 temp_ssa_register_v_->SetBit(next_mir->ssa_rep->defs[0]);
buzbee311ca162013-02-28 15:56:43 -0800695 } else {
696 if (next_mir) {
697 LOG(WARNING) << "Unexpected opcode following new: " << next_mir->dalvikInsn.opcode;
buzbee0d829482013-10-11 15:24:55 -0700698 } else if (bb->fall_through != NullBasicBlockId) {
buzbee311ca162013-02-28 15:56:43 -0800699 // Look in next basic block
buzbee0d829482013-10-11 15:24:55 -0700700 struct BasicBlock* next_bb = GetBasicBlock(bb->fall_through);
buzbee311ca162013-02-28 15:56:43 -0800701 for (MIR* tmir = next_bb->first_mir_insn; tmir != NULL;
702 tmir =tmir->next) {
703 if (static_cast<int>(tmir->dalvikInsn.opcode) >= static_cast<int>(kMirOpFirst)) {
704 continue;
705 }
706 // First non-pseudo should be MOVE_RESULT_OBJECT
707 if (tmir->dalvikInsn.opcode == Instruction::MOVE_RESULT_OBJECT) {
708 // Mark as null checked
Ian Rogers31aa97c2013-10-25 23:07:29 +0000709 temp_ssa_register_v_->SetBit(tmir->ssa_rep->defs[0]);
buzbee311ca162013-02-28 15:56:43 -0800710 } else {
711 LOG(WARNING) << "Unexpected op after new: " << tmir->dalvikInsn.opcode;
712 }
713 break;
714 }
715 }
716 }
717 }
718
719 /*
720 * Propagate nullcheck state on register copies (including
721 * Phi pseudo copies. For the latter, nullcheck state is
Ian Rogers31aa97c2013-10-25 23:07:29 +0000722 * the "and" of all the Phi's operands.
buzbee311ca162013-02-28 15:56:43 -0800723 */
724 if (df_attributes & (DF_NULL_TRANSFER_0 | DF_NULL_TRANSFER_N)) {
725 int tgt_sreg = mir->ssa_rep->defs[0];
726 int operands = (df_attributes & DF_NULL_TRANSFER_0) ? 1 :
727 mir->ssa_rep->num_uses;
Ian Rogers31aa97c2013-10-25 23:07:29 +0000728 bool null_checked = true;
buzbee311ca162013-02-28 15:56:43 -0800729 for (int i = 0; i < operands; i++) {
Ian Rogers31aa97c2013-10-25 23:07:29 +0000730 null_checked &= temp_ssa_register_v_->IsBitSet(mir->ssa_rep->uses[i]);
buzbee311ca162013-02-28 15:56:43 -0800731 }
Ian Rogers31aa97c2013-10-25 23:07:29 +0000732 if (null_checked) {
buzbee862a7602013-04-05 10:58:54 -0700733 temp_ssa_register_v_->SetBit(tgt_sreg);
buzbee311ca162013-02-28 15:56:43 -0800734 }
735 }
Ian Rogers31aa97c2013-10-25 23:07:29 +0000736
737 // Already nullchecked?
738 if ((df_attributes & DF_HAS_NULL_CHKS) && !(mir->optimization_flags & MIR_IGNORE_NULL_CHECK)) {
739 int src_idx;
740 if (df_attributes & DF_NULL_CHK_1) {
741 src_idx = 1;
742 } else if (df_attributes & DF_NULL_CHK_2) {
743 src_idx = 2;
744 } else {
745 src_idx = 0;
746 }
747 int src_sreg = mir->ssa_rep->uses[src_idx];
748 if (temp_ssa_register_v_->IsBitSet(src_sreg)) {
749 // Eliminate the null check
750 mir->optimization_flags |= MIR_IGNORE_NULL_CHECK;
751 } else {
752 // Mark s_reg as null-checked
753 temp_ssa_register_v_->SetBit(src_sreg);
754 }
755 }
buzbee311ca162013-02-28 15:56:43 -0800756 }
757
758 // Did anything change?
buzbee862a7602013-04-05 10:58:54 -0700759 bool changed = !temp_ssa_register_v_->Equal(bb->data_flow_info->ending_null_check_v);
760 if (changed) {
761 bb->data_flow_info->ending_null_check_v->Copy(temp_ssa_register_v_);
buzbee311ca162013-02-28 15:56:43 -0800762 }
buzbee862a7602013-04-05 10:58:54 -0700763 return changed;
buzbee311ca162013-02-28 15:56:43 -0800764}
765
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700766void MIRGraph::NullCheckElimination() {
buzbee311ca162013-02-28 15:56:43 -0800767 if (!(cu_->disable_opt & (1 << kNullCheckElimination))) {
768 DCHECK(temp_ssa_register_v_ != NULL);
buzbee56c71782013-09-05 17:13:19 -0700769 AllNodesIterator iter(this);
buzbee311ca162013-02-28 15:56:43 -0800770 for (BasicBlock* bb = iter.Next(); bb != NULL; bb = iter.Next()) {
771 NullCheckEliminationInit(bb);
772 }
buzbee56c71782013-09-05 17:13:19 -0700773 RepeatingPreOrderDfsIterator iter2(this);
buzbee311ca162013-02-28 15:56:43 -0800774 bool change = false;
775 for (BasicBlock* bb = iter2.Next(change); bb != NULL; bb = iter2.Next(change)) {
776 change = EliminateNullChecks(bb);
777 }
778 }
779 if (cu_->enable_debug & (1 << kDebugDumpCFG)) {
780 DumpCFG("/sdcard/4_post_nce_cfg/", false);
781 }
782}
783
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700784void MIRGraph::BasicBlockCombine() {
buzbee56c71782013-09-05 17:13:19 -0700785 PreOrderDfsIterator iter(this);
buzbee311ca162013-02-28 15:56:43 -0800786 for (BasicBlock* bb = iter.Next(); bb != NULL; bb = iter.Next()) {
787 CombineBlocks(bb);
788 }
789 if (cu_->enable_debug & (1 << kDebugDumpCFG)) {
790 DumpCFG("/sdcard/5_post_bbcombine_cfg/", false);
791 }
792}
793
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700794void MIRGraph::CodeLayout() {
buzbee1fd33462013-03-25 13:40:45 -0700795 if (cu_->enable_debug & (1 << kDebugVerifyDataflow)) {
796 VerifyDataflow();
797 }
buzbee56c71782013-09-05 17:13:19 -0700798 AllNodesIterator iter(this);
buzbee311ca162013-02-28 15:56:43 -0800799 for (BasicBlock* bb = iter.Next(); bb != NULL; bb = iter.Next()) {
800 LayoutBlocks(bb);
801 }
802 if (cu_->enable_debug & (1 << kDebugDumpCFG)) {
803 DumpCFG("/sdcard/2_post_layout_cfg/", true);
804 }
805}
806
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700807void MIRGraph::DumpCheckStats() {
buzbee311ca162013-02-28 15:56:43 -0800808 Checkstats* stats =
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700809 static_cast<Checkstats*>(arena_->Alloc(sizeof(Checkstats), ArenaAllocator::kAllocDFInfo));
buzbee1fd33462013-03-25 13:40:45 -0700810 checkstats_ = stats;
buzbee56c71782013-09-05 17:13:19 -0700811 AllNodesIterator iter(this);
buzbee311ca162013-02-28 15:56:43 -0800812 for (BasicBlock* bb = iter.Next(); bb != NULL; bb = iter.Next()) {
813 CountChecks(bb);
814 }
815 if (stats->null_checks > 0) {
816 float eliminated = static_cast<float>(stats->null_checks_eliminated);
817 float checks = static_cast<float>(stats->null_checks);
818 LOG(INFO) << "Null Checks: " << PrettyMethod(cu_->method_idx, *cu_->dex_file) << " "
819 << stats->null_checks_eliminated << " of " << stats->null_checks << " -> "
820 << (eliminated/checks) * 100.0 << "%";
821 }
822 if (stats->range_checks > 0) {
823 float eliminated = static_cast<float>(stats->range_checks_eliminated);
824 float checks = static_cast<float>(stats->range_checks);
825 LOG(INFO) << "Range Checks: " << PrettyMethod(cu_->method_idx, *cu_->dex_file) << " "
826 << stats->range_checks_eliminated << " of " << stats->range_checks << " -> "
827 << (eliminated/checks) * 100.0 << "%";
828 }
829}
830
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700831bool MIRGraph::BuildExtendedBBList(struct BasicBlock* bb) {
buzbee311ca162013-02-28 15:56:43 -0800832 if (bb->visited) return false;
833 if (!((bb->block_type == kEntryBlock) || (bb->block_type == kDalvikByteCode)
834 || (bb->block_type == kExitBlock))) {
835 // Ignore special blocks
836 bb->visited = true;
837 return false;
838 }
839 // Must be head of extended basic block.
840 BasicBlock* start_bb = bb;
buzbee0d829482013-10-11 15:24:55 -0700841 extended_basic_blocks_.push_back(bb->id);
buzbee311ca162013-02-28 15:56:43 -0800842 bool terminated_by_return = false;
843 // Visit blocks strictly dominated by this head.
844 while (bb != NULL) {
845 bb->visited = true;
846 terminated_by_return |= bb->terminated_by_return;
847 bb = NextDominatedBlock(bb);
848 }
849 if (terminated_by_return) {
850 // This extended basic block contains a return, so mark all members.
851 bb = start_bb;
852 while (bb != NULL) {
853 bb->dominates_return = true;
854 bb = NextDominatedBlock(bb);
855 }
856 }
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700857 return false; // Not iterative - return value will be ignored
buzbee311ca162013-02-28 15:56:43 -0800858}
859
860
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700861void MIRGraph::BasicBlockOptimization() {
buzbee311ca162013-02-28 15:56:43 -0800862 if (!(cu_->disable_opt & (1 << kBBOpt))) {
buzbee311ca162013-02-28 15:56:43 -0800863 DCHECK_EQ(cu_->num_compiler_temps, 0);
buzbeea5abf702013-04-12 14:39:29 -0700864 ClearAllVisitedFlags();
buzbee56c71782013-09-05 17:13:19 -0700865 PreOrderDfsIterator iter2(this);
buzbee311ca162013-02-28 15:56:43 -0800866 for (BasicBlock* bb = iter2.Next(); bb != NULL; bb = iter2.Next()) {
867 BuildExtendedBBList(bb);
868 }
869 // Perform extended basic block optimizations.
870 for (unsigned int i = 0; i < extended_basic_blocks_.size(); i++) {
buzbee0d829482013-10-11 15:24:55 -0700871 BasicBlockOpt(GetBasicBlock(extended_basic_blocks_[i]));
buzbee311ca162013-02-28 15:56:43 -0800872 }
873 }
874 if (cu_->enable_debug & (1 << kDebugDumpCFG)) {
875 DumpCFG("/sdcard/6_post_bbo_cfg/", false);
876 }
877}
878
879} // namespace art