blob: 5c41520990f20f999d1100ce2b05728904d41d7c [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
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -080039void MIRGraph::DoConstantPropagation(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) {
buzbee1da1e2f2013-11-15 13:37:01 -080043 uint64_t 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
buzbee311ca162013-02-28 15:56:43 -080095/* Advance to next strictly dominated MIR node in an extended basic block */
buzbee0d829482013-10-11 15:24:55 -070096MIR* MIRGraph::AdvanceMIR(BasicBlock** p_bb, MIR* mir) {
buzbee311ca162013-02-28 15:56:43 -080097 BasicBlock* bb = *p_bb;
98 if (mir != NULL) {
99 mir = mir->next;
100 if (mir == NULL) {
buzbee0d829482013-10-11 15:24:55 -0700101 bb = GetBasicBlock(bb->fall_through);
buzbee311ca162013-02-28 15:56:43 -0800102 if ((bb == NULL) || Predecessors(bb) != 1) {
103 mir = NULL;
104 } else {
105 *p_bb = bb;
106 mir = bb->first_mir_insn;
107 }
108 }
109 }
110 return mir;
111}
112
113/*
114 * To be used at an invoke mir. If the logically next mir node represents
115 * a move-result, return it. Else, return NULL. If a move-result exists,
116 * it is required to immediately follow the invoke with no intervening
117 * opcodes or incoming arcs. However, if the result of the invoke is not
118 * used, a move-result may not be present.
119 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700120MIR* MIRGraph::FindMoveResult(BasicBlock* bb, MIR* mir) {
buzbee311ca162013-02-28 15:56:43 -0800121 BasicBlock* tbb = bb;
122 mir = AdvanceMIR(&tbb, mir);
123 while (mir != NULL) {
124 int opcode = mir->dalvikInsn.opcode;
125 if ((mir->dalvikInsn.opcode == Instruction::MOVE_RESULT) ||
126 (mir->dalvikInsn.opcode == Instruction::MOVE_RESULT_OBJECT) ||
127 (mir->dalvikInsn.opcode == Instruction::MOVE_RESULT_WIDE)) {
128 break;
129 }
130 // Keep going if pseudo op, otherwise terminate
131 if (opcode < kNumPackedOpcodes) {
132 mir = NULL;
133 } else {
134 mir = AdvanceMIR(&tbb, mir);
135 }
136 }
137 return mir;
138}
139
buzbee0d829482013-10-11 15:24:55 -0700140BasicBlock* MIRGraph::NextDominatedBlock(BasicBlock* bb) {
buzbee311ca162013-02-28 15:56:43 -0800141 if (bb->block_type == kDead) {
142 return NULL;
143 }
144 DCHECK((bb->block_type == kEntryBlock) || (bb->block_type == kDalvikByteCode)
145 || (bb->block_type == kExitBlock));
buzbee0d829482013-10-11 15:24:55 -0700146 BasicBlock* bb_taken = GetBasicBlock(bb->taken);
147 BasicBlock* bb_fall_through = GetBasicBlock(bb->fall_through);
buzbee1da1e2f2013-11-15 13:37:01 -0800148 if (((bb_fall_through == NULL) && (bb_taken != NULL)) &&
buzbee0d829482013-10-11 15:24:55 -0700149 ((bb_taken->block_type == kDalvikByteCode) || (bb_taken->block_type == kExitBlock))) {
buzbeecbcfaf32013-08-19 07:37:40 -0700150 // Follow simple unconditional branches.
buzbee0d829482013-10-11 15:24:55 -0700151 bb = bb_taken;
buzbeecbcfaf32013-08-19 07:37:40 -0700152 } else {
153 // Follow simple fallthrough
buzbee0d829482013-10-11 15:24:55 -0700154 bb = (bb_taken != NULL) ? NULL : bb_fall_through;
buzbeecbcfaf32013-08-19 07:37:40 -0700155 }
buzbee311ca162013-02-28 15:56:43 -0800156 if (bb == NULL || (Predecessors(bb) != 1)) {
157 return NULL;
158 }
159 DCHECK((bb->block_type == kDalvikByteCode) || (bb->block_type == kExitBlock));
160 return bb;
161}
162
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700163static MIR* FindPhi(BasicBlock* bb, int ssa_name) {
buzbee311ca162013-02-28 15:56:43 -0800164 for (MIR* mir = bb->first_mir_insn; mir != NULL; mir = mir->next) {
165 if (static_cast<int>(mir->dalvikInsn.opcode) == kMirOpPhi) {
166 for (int i = 0; i < mir->ssa_rep->num_uses; i++) {
167 if (mir->ssa_rep->uses[i] == ssa_name) {
168 return mir;
169 }
170 }
171 }
172 }
173 return NULL;
174}
175
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700176static SelectInstructionKind SelectKind(MIR* mir) {
buzbee311ca162013-02-28 15:56:43 -0800177 switch (mir->dalvikInsn.opcode) {
178 case Instruction::MOVE:
179 case Instruction::MOVE_OBJECT:
180 case Instruction::MOVE_16:
181 case Instruction::MOVE_OBJECT_16:
182 case Instruction::MOVE_FROM16:
183 case Instruction::MOVE_OBJECT_FROM16:
184 return kSelectMove;
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700185 case Instruction::CONST:
186 case Instruction::CONST_4:
187 case Instruction::CONST_16:
buzbee311ca162013-02-28 15:56:43 -0800188 return kSelectConst;
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700189 case Instruction::GOTO:
190 case Instruction::GOTO_16:
191 case Instruction::GOTO_32:
buzbee311ca162013-02-28 15:56:43 -0800192 return kSelectGoto;
Brian Carlstrom02c8cc62013-07-18 15:54:44 -0700193 default:
194 return kSelectNone;
buzbee311ca162013-02-28 15:56:43 -0800195 }
buzbee311ca162013-02-28 15:56:43 -0800196}
197
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700198int MIRGraph::GetSSAUseCount(int s_reg) {
buzbee862a7602013-04-05 10:58:54 -0700199 return raw_use_counts_.Get(s_reg);
buzbee311ca162013-02-28 15:56:43 -0800200}
201
202
203/* Do some MIR-level extended basic block optimizations */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700204bool MIRGraph::BasicBlockOpt(BasicBlock* bb) {
buzbee311ca162013-02-28 15:56:43 -0800205 if (bb->block_type == kDead) {
206 return true;
207 }
208 int num_temps = 0;
buzbee1da1e2f2013-11-15 13:37:01 -0800209 bool use_lvn = bb->use_lvn;
210 UniquePtr<LocalValueNumbering> local_valnum;
211 if (use_lvn) {
212 local_valnum.reset(new LocalValueNumbering(cu_));
213 }
buzbee311ca162013-02-28 15:56:43 -0800214 while (bb != NULL) {
215 for (MIR* mir = bb->first_mir_insn; mir != NULL; mir = mir->next) {
216 // TUNING: use the returned value number for CSE.
buzbee1da1e2f2013-11-15 13:37:01 -0800217 if (use_lvn) {
218 local_valnum->GetValueNumber(mir);
219 }
buzbee311ca162013-02-28 15:56:43 -0800220 // Look for interesting opcodes, skip otherwise
221 Instruction::Code opcode = mir->dalvikInsn.opcode;
222 switch (opcode) {
223 case Instruction::CMPL_FLOAT:
224 case Instruction::CMPL_DOUBLE:
225 case Instruction::CMPG_FLOAT:
226 case Instruction::CMPG_DOUBLE:
227 case Instruction::CMP_LONG:
buzbee1fd33462013-03-25 13:40:45 -0700228 if ((cu_->disable_opt & (1 << kBranchFusing)) != 0) {
buzbee311ca162013-02-28 15:56:43 -0800229 // Bitcode doesn't allow this optimization.
230 break;
231 }
232 if (mir->next != NULL) {
233 MIR* mir_next = mir->next;
234 Instruction::Code br_opcode = mir_next->dalvikInsn.opcode;
235 ConditionCode ccode = kCondNv;
Brian Carlstromdf629502013-07-17 22:39:56 -0700236 switch (br_opcode) {
buzbee311ca162013-02-28 15:56:43 -0800237 case Instruction::IF_EQZ:
238 ccode = kCondEq;
239 break;
240 case Instruction::IF_NEZ:
241 ccode = kCondNe;
242 break;
243 case Instruction::IF_LTZ:
244 ccode = kCondLt;
245 break;
246 case Instruction::IF_GEZ:
247 ccode = kCondGe;
248 break;
249 case Instruction::IF_GTZ:
250 ccode = kCondGt;
251 break;
252 case Instruction::IF_LEZ:
253 ccode = kCondLe;
254 break;
255 default:
256 break;
257 }
258 // Make sure result of cmp is used by next insn and nowhere else
259 if ((ccode != kCondNv) &&
260 (mir->ssa_rep->defs[0] == mir_next->ssa_rep->uses[0]) &&
261 (GetSSAUseCount(mir->ssa_rep->defs[0]) == 1)) {
Vladimir Markoa8946072014-01-22 10:30:44 +0000262 mir_next->meta.ccode = ccode;
Brian Carlstromdf629502013-07-17 22:39:56 -0700263 switch (opcode) {
buzbee311ca162013-02-28 15:56:43 -0800264 case Instruction::CMPL_FLOAT:
265 mir_next->dalvikInsn.opcode =
266 static_cast<Instruction::Code>(kMirOpFusedCmplFloat);
267 break;
268 case Instruction::CMPL_DOUBLE:
269 mir_next->dalvikInsn.opcode =
270 static_cast<Instruction::Code>(kMirOpFusedCmplDouble);
271 break;
272 case Instruction::CMPG_FLOAT:
273 mir_next->dalvikInsn.opcode =
274 static_cast<Instruction::Code>(kMirOpFusedCmpgFloat);
275 break;
276 case Instruction::CMPG_DOUBLE:
277 mir_next->dalvikInsn.opcode =
278 static_cast<Instruction::Code>(kMirOpFusedCmpgDouble);
279 break;
280 case Instruction::CMP_LONG:
281 mir_next->dalvikInsn.opcode =
282 static_cast<Instruction::Code>(kMirOpFusedCmpLong);
283 break;
284 default: LOG(ERROR) << "Unexpected opcode: " << opcode;
285 }
286 mir->dalvikInsn.opcode = static_cast<Instruction::Code>(kMirOpNop);
287 mir_next->ssa_rep->num_uses = mir->ssa_rep->num_uses;
288 mir_next->ssa_rep->uses = mir->ssa_rep->uses;
289 mir_next->ssa_rep->fp_use = mir->ssa_rep->fp_use;
290 mir_next->ssa_rep->num_defs = 0;
291 mir->ssa_rep->num_uses = 0;
292 mir->ssa_rep->num_defs = 0;
293 }
294 }
295 break;
296 case Instruction::GOTO:
297 case Instruction::GOTO_16:
298 case Instruction::GOTO_32:
299 case Instruction::IF_EQ:
300 case Instruction::IF_NE:
301 case Instruction::IF_LT:
302 case Instruction::IF_GE:
303 case Instruction::IF_GT:
304 case Instruction::IF_LE:
305 case Instruction::IF_EQZ:
306 case Instruction::IF_NEZ:
307 case Instruction::IF_LTZ:
308 case Instruction::IF_GEZ:
309 case Instruction::IF_GTZ:
310 case Instruction::IF_LEZ:
buzbeecbcfaf32013-08-19 07:37:40 -0700311 // If we've got a backwards branch to return, no need to suspend check.
buzbee0d829482013-10-11 15:24:55 -0700312 if ((IsBackedge(bb, bb->taken) && GetBasicBlock(bb->taken)->dominates_return) ||
313 (IsBackedge(bb, bb->fall_through) &&
314 GetBasicBlock(bb->fall_through)->dominates_return)) {
buzbee311ca162013-02-28 15:56:43 -0800315 mir->optimization_flags |= MIR_IGNORE_SUSPEND_CHECK;
316 if (cu_->verbose) {
buzbee0d829482013-10-11 15:24:55 -0700317 LOG(INFO) << "Suppressed suspend check on branch to return at 0x" << std::hex
318 << mir->offset;
buzbee311ca162013-02-28 15:56:43 -0800319 }
320 }
321 break;
322 default:
323 break;
324 }
325 // Is this the select pattern?
326 // TODO: flesh out support for Mips and X86. NOTE: llvm's select op doesn't quite work here.
327 // TUNING: expand to support IF_xx compare & branches
buzbee409fe942013-10-11 10:49:56 -0700328 if (!(cu_->compiler_backend == kPortable) && (cu_->instruction_set == kThumb2) &&
buzbee311ca162013-02-28 15:56:43 -0800329 ((mir->dalvikInsn.opcode == Instruction::IF_EQZ) ||
330 (mir->dalvikInsn.opcode == Instruction::IF_NEZ))) {
buzbee0d829482013-10-11 15:24:55 -0700331 BasicBlock* ft = GetBasicBlock(bb->fall_through);
buzbee311ca162013-02-28 15:56:43 -0800332 DCHECK(ft != NULL);
buzbee0d829482013-10-11 15:24:55 -0700333 BasicBlock* ft_ft = GetBasicBlock(ft->fall_through);
334 BasicBlock* ft_tk = GetBasicBlock(ft->taken);
buzbee311ca162013-02-28 15:56:43 -0800335
buzbee0d829482013-10-11 15:24:55 -0700336 BasicBlock* tk = GetBasicBlock(bb->taken);
buzbee311ca162013-02-28 15:56:43 -0800337 DCHECK(tk != NULL);
buzbee0d829482013-10-11 15:24:55 -0700338 BasicBlock* tk_ft = GetBasicBlock(tk->fall_through);
339 BasicBlock* tk_tk = GetBasicBlock(tk->taken);
buzbee311ca162013-02-28 15:56:43 -0800340
341 /*
342 * In the select pattern, the taken edge goes to a block that unconditionally
343 * transfers to the rejoin block and the fall_though edge goes to a block that
344 * unconditionally falls through to the rejoin block.
345 */
346 if ((tk_ft == NULL) && (ft_tk == NULL) && (tk_tk == ft_ft) &&
347 (Predecessors(tk) == 1) && (Predecessors(ft) == 1)) {
348 /*
349 * Okay - we have the basic diamond shape. At the very least, we can eliminate the
350 * suspend check on the taken-taken branch back to the join point.
351 */
352 if (SelectKind(tk->last_mir_insn) == kSelectGoto) {
353 tk->last_mir_insn->optimization_flags |= (MIR_IGNORE_SUSPEND_CHECK);
354 }
355 // Are the block bodies something we can handle?
356 if ((ft->first_mir_insn == ft->last_mir_insn) &&
357 (tk->first_mir_insn != tk->last_mir_insn) &&
358 (tk->first_mir_insn->next == tk->last_mir_insn) &&
359 ((SelectKind(ft->first_mir_insn) == kSelectMove) ||
360 (SelectKind(ft->first_mir_insn) == kSelectConst)) &&
361 (SelectKind(ft->first_mir_insn) == SelectKind(tk->first_mir_insn)) &&
362 (SelectKind(tk->last_mir_insn) == kSelectGoto)) {
363 // Almost there. Are the instructions targeting the same vreg?
364 MIR* if_true = tk->first_mir_insn;
365 MIR* if_false = ft->first_mir_insn;
366 // It's possible that the target of the select isn't used - skip those (rare) cases.
367 MIR* phi = FindPhi(tk_tk, if_true->ssa_rep->defs[0]);
368 if ((phi != NULL) && (if_true->dalvikInsn.vA == if_false->dalvikInsn.vA)) {
369 /*
370 * We'll convert the IF_EQZ/IF_NEZ to a SELECT. We need to find the
371 * Phi node in the merge block and delete it (while using the SSA name
372 * of the merge as the target of the SELECT. Delete both taken and
373 * fallthrough blocks, and set fallthrough to merge block.
374 * NOTE: not updating other dataflow info (no longer used at this point).
375 * If this changes, need to update i_dom, etc. here (and in CombineBlocks).
376 */
377 if (opcode == Instruction::IF_NEZ) {
378 // Normalize.
379 MIR* tmp_mir = if_true;
380 if_true = if_false;
381 if_false = tmp_mir;
382 }
383 mir->dalvikInsn.opcode = static_cast<Instruction::Code>(kMirOpSelect);
384 bool const_form = (SelectKind(if_true) == kSelectConst);
385 if ((SelectKind(if_true) == kSelectMove)) {
386 if (IsConst(if_true->ssa_rep->uses[0]) &&
387 IsConst(if_false->ssa_rep->uses[0])) {
388 const_form = true;
389 if_true->dalvikInsn.vB = ConstantValue(if_true->ssa_rep->uses[0]);
390 if_false->dalvikInsn.vB = ConstantValue(if_false->ssa_rep->uses[0]);
391 }
392 }
393 if (const_form) {
394 // "true" set val in vB
395 mir->dalvikInsn.vB = if_true->dalvikInsn.vB;
396 // "false" set val in vC
397 mir->dalvikInsn.vC = if_false->dalvikInsn.vB;
398 } else {
399 DCHECK_EQ(SelectKind(if_true), kSelectMove);
400 DCHECK_EQ(SelectKind(if_false), kSelectMove);
buzbee862a7602013-04-05 10:58:54 -0700401 int* src_ssa =
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700402 static_cast<int*>(arena_->Alloc(sizeof(int) * 3, ArenaAllocator::kAllocDFInfo));
buzbee311ca162013-02-28 15:56:43 -0800403 src_ssa[0] = mir->ssa_rep->uses[0];
404 src_ssa[1] = if_true->ssa_rep->uses[0];
405 src_ssa[2] = if_false->ssa_rep->uses[0];
406 mir->ssa_rep->uses = src_ssa;
407 mir->ssa_rep->num_uses = 3;
408 }
409 mir->ssa_rep->num_defs = 1;
buzbee862a7602013-04-05 10:58:54 -0700410 mir->ssa_rep->defs =
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700411 static_cast<int*>(arena_->Alloc(sizeof(int) * 1, ArenaAllocator::kAllocDFInfo));
buzbee862a7602013-04-05 10:58:54 -0700412 mir->ssa_rep->fp_def =
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700413 static_cast<bool*>(arena_->Alloc(sizeof(bool) * 1, ArenaAllocator::kAllocDFInfo));
buzbee311ca162013-02-28 15:56:43 -0800414 mir->ssa_rep->fp_def[0] = if_true->ssa_rep->fp_def[0];
buzbee817e45a2013-05-30 18:59:12 -0700415 // Match type of uses to def.
416 mir->ssa_rep->fp_use =
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700417 static_cast<bool*>(arena_->Alloc(sizeof(bool) * mir->ssa_rep->num_uses,
418 ArenaAllocator::kAllocDFInfo));
buzbee817e45a2013-05-30 18:59:12 -0700419 for (int i = 0; i < mir->ssa_rep->num_uses; i++) {
420 mir->ssa_rep->fp_use[i] = mir->ssa_rep->fp_def[0];
421 }
buzbee311ca162013-02-28 15:56:43 -0800422 /*
423 * There is usually a Phi node in the join block for our two cases. If the
424 * Phi node only contains our two cases as input, we will use the result
425 * SSA name of the Phi node as our select result and delete the Phi. If
426 * the Phi node has more than two operands, we will arbitrarily use the SSA
427 * name of the "true" path, delete the SSA name of the "false" path from the
428 * Phi node (and fix up the incoming arc list).
429 */
430 if (phi->ssa_rep->num_uses == 2) {
431 mir->ssa_rep->defs[0] = phi->ssa_rep->defs[0];
432 phi->dalvikInsn.opcode = static_cast<Instruction::Code>(kMirOpNop);
433 } else {
434 int dead_def = if_false->ssa_rep->defs[0];
435 int live_def = if_true->ssa_rep->defs[0];
436 mir->ssa_rep->defs[0] = live_def;
buzbee0d829482013-10-11 15:24:55 -0700437 BasicBlockId* incoming = phi->meta.phi_incoming;
buzbee311ca162013-02-28 15:56:43 -0800438 for (int i = 0; i < phi->ssa_rep->num_uses; i++) {
439 if (phi->ssa_rep->uses[i] == live_def) {
440 incoming[i] = bb->id;
441 }
442 }
443 for (int i = 0; i < phi->ssa_rep->num_uses; i++) {
444 if (phi->ssa_rep->uses[i] == dead_def) {
445 int last_slot = phi->ssa_rep->num_uses - 1;
446 phi->ssa_rep->uses[i] = phi->ssa_rep->uses[last_slot];
447 incoming[i] = incoming[last_slot];
448 }
449 }
450 }
451 phi->ssa_rep->num_uses--;
buzbee0d829482013-10-11 15:24:55 -0700452 bb->taken = NullBasicBlockId;
buzbee311ca162013-02-28 15:56:43 -0800453 tk->block_type = kDead;
454 for (MIR* tmir = ft->first_mir_insn; tmir != NULL; tmir = tmir->next) {
455 tmir->dalvikInsn.opcode = static_cast<Instruction::Code>(kMirOpNop);
456 }
457 }
458 }
459 }
460 }
461 }
buzbee1da1e2f2013-11-15 13:37:01 -0800462 bb = ((cu_->disable_opt & (1 << kSuppressExceptionEdges)) != 0) ? NextDominatedBlock(bb) : NULL;
buzbee311ca162013-02-28 15:56:43 -0800463 }
464
465 if (num_temps > cu_->num_compiler_temps) {
466 cu_->num_compiler_temps = num_temps;
467 }
468 return true;
469}
470
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700471void MIRGraph::NullCheckEliminationInit(struct BasicBlock* bb) {
buzbee862a7602013-04-05 10:58:54 -0700472 if (bb->data_flow_info != NULL) {
473 bb->data_flow_info->ending_null_check_v =
474 new (arena_) ArenaBitVector(arena_, GetNumSSARegs(), false, kBitMapNullCheck);
475 }
buzbee311ca162013-02-28 15:56:43 -0800476}
477
478/* Collect stats on number of checks removed */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700479void MIRGraph::CountChecks(struct BasicBlock* bb) {
buzbee862a7602013-04-05 10:58:54 -0700480 if (bb->data_flow_info != NULL) {
481 for (MIR* mir = bb->first_mir_insn; mir != NULL; mir = mir->next) {
482 if (mir->ssa_rep == NULL) {
483 continue;
buzbee311ca162013-02-28 15:56:43 -0800484 }
buzbee1da1e2f2013-11-15 13:37:01 -0800485 uint64_t df_attributes = oat_data_flow_attributes_[mir->dalvikInsn.opcode];
buzbee862a7602013-04-05 10:58:54 -0700486 if (df_attributes & DF_HAS_NULL_CHKS) {
487 checkstats_->null_checks++;
488 if (mir->optimization_flags & MIR_IGNORE_NULL_CHECK) {
489 checkstats_->null_checks_eliminated++;
490 }
491 }
492 if (df_attributes & DF_HAS_RANGE_CHKS) {
493 checkstats_->range_checks++;
494 if (mir->optimization_flags & MIR_IGNORE_RANGE_CHECK) {
495 checkstats_->range_checks_eliminated++;
496 }
buzbee311ca162013-02-28 15:56:43 -0800497 }
498 }
499 }
buzbee311ca162013-02-28 15:56:43 -0800500}
501
502/* Try to make common case the fallthrough path */
buzbee0d829482013-10-11 15:24:55 -0700503bool MIRGraph::LayoutBlocks(BasicBlock* bb) {
buzbee311ca162013-02-28 15:56:43 -0800504 // TODO: For now, just looking for direct throws. Consider generalizing for profile feedback
505 if (!bb->explicit_throw) {
506 return false;
507 }
508 BasicBlock* walker = bb;
509 while (true) {
510 // Check termination conditions
511 if ((walker->block_type == kEntryBlock) || (Predecessors(walker) != 1)) {
512 break;
513 }
buzbee0d829482013-10-11 15:24:55 -0700514 BasicBlock* prev = GetBasicBlock(walker->predecessors->Get(0));
buzbee311ca162013-02-28 15:56:43 -0800515 if (prev->conditional_branch) {
buzbee0d829482013-10-11 15:24:55 -0700516 if (GetBasicBlock(prev->fall_through) == walker) {
buzbee311ca162013-02-28 15:56:43 -0800517 // Already done - return
518 break;
519 }
buzbee0d829482013-10-11 15:24:55 -0700520 DCHECK_EQ(walker, GetBasicBlock(prev->taken));
buzbee311ca162013-02-28 15:56:43 -0800521 // Got one. Flip it and exit
522 Instruction::Code opcode = prev->last_mir_insn->dalvikInsn.opcode;
523 switch (opcode) {
524 case Instruction::IF_EQ: opcode = Instruction::IF_NE; break;
525 case Instruction::IF_NE: opcode = Instruction::IF_EQ; break;
526 case Instruction::IF_LT: opcode = Instruction::IF_GE; break;
527 case Instruction::IF_GE: opcode = Instruction::IF_LT; break;
528 case Instruction::IF_GT: opcode = Instruction::IF_LE; break;
529 case Instruction::IF_LE: opcode = Instruction::IF_GT; break;
530 case Instruction::IF_EQZ: opcode = Instruction::IF_NEZ; break;
531 case Instruction::IF_NEZ: opcode = Instruction::IF_EQZ; break;
532 case Instruction::IF_LTZ: opcode = Instruction::IF_GEZ; break;
533 case Instruction::IF_GEZ: opcode = Instruction::IF_LTZ; break;
534 case Instruction::IF_GTZ: opcode = Instruction::IF_LEZ; break;
535 case Instruction::IF_LEZ: opcode = Instruction::IF_GTZ; break;
536 default: LOG(FATAL) << "Unexpected opcode " << opcode;
537 }
538 prev->last_mir_insn->dalvikInsn.opcode = opcode;
buzbee0d829482013-10-11 15:24:55 -0700539 BasicBlockId t_bb = prev->taken;
buzbee311ca162013-02-28 15:56:43 -0800540 prev->taken = prev->fall_through;
541 prev->fall_through = t_bb;
542 break;
543 }
544 walker = prev;
545 }
546 return false;
547}
548
549/* Combine any basic blocks terminated by instructions that we now know can't throw */
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800550void MIRGraph::CombineBlocks(struct BasicBlock* bb) {
buzbee311ca162013-02-28 15:56:43 -0800551 // Loop here to allow combining a sequence of blocks
552 while (true) {
553 // Check termination conditions
554 if ((bb->first_mir_insn == NULL)
555 || (bb->data_flow_info == NULL)
556 || (bb->block_type == kExceptionHandling)
557 || (bb->block_type == kExitBlock)
558 || (bb->block_type == kDead)
buzbee0d829482013-10-11 15:24:55 -0700559 || (bb->taken == NullBasicBlockId)
560 || (GetBasicBlock(bb->taken)->block_type != kExceptionHandling)
561 || (bb->successor_block_list_type != kNotUsed)
buzbee311ca162013-02-28 15:56:43 -0800562 || (static_cast<int>(bb->last_mir_insn->dalvikInsn.opcode) != kMirOpCheck)) {
563 break;
564 }
565
566 // Test the kMirOpCheck instruction
567 MIR* mir = bb->last_mir_insn;
568 // Grab the attributes from the paired opcode
569 MIR* throw_insn = mir->meta.throw_insn;
buzbee1da1e2f2013-11-15 13:37:01 -0800570 uint64_t df_attributes = oat_data_flow_attributes_[throw_insn->dalvikInsn.opcode];
buzbee311ca162013-02-28 15:56:43 -0800571 bool can_combine = true;
572 if (df_attributes & DF_HAS_NULL_CHKS) {
573 can_combine &= ((throw_insn->optimization_flags & MIR_IGNORE_NULL_CHECK) != 0);
574 }
575 if (df_attributes & DF_HAS_RANGE_CHKS) {
576 can_combine &= ((throw_insn->optimization_flags & MIR_IGNORE_RANGE_CHECK) != 0);
577 }
578 if (!can_combine) {
579 break;
580 }
581 // OK - got one. Combine
buzbee0d829482013-10-11 15:24:55 -0700582 BasicBlock* bb_next = GetBasicBlock(bb->fall_through);
buzbee311ca162013-02-28 15:56:43 -0800583 DCHECK(!bb_next->catch_entry);
584 DCHECK_EQ(Predecessors(bb_next), 1U);
buzbee311ca162013-02-28 15:56:43 -0800585 // Overwrite the kOpCheck insn with the paired opcode
586 DCHECK_EQ(bb_next->first_mir_insn, throw_insn);
587 *bb->last_mir_insn = *throw_insn;
buzbee311ca162013-02-28 15:56:43 -0800588 // Use the successor info from the next block
buzbee0d829482013-10-11 15:24:55 -0700589 bb->successor_block_list_type = bb_next->successor_block_list_type;
590 bb->successor_blocks = bb_next->successor_blocks;
buzbee311ca162013-02-28 15:56:43 -0800591 // Use the ending block linkage from the next block
592 bb->fall_through = bb_next->fall_through;
buzbee0d829482013-10-11 15:24:55 -0700593 GetBasicBlock(bb->taken)->block_type = kDead; // Kill the unused exception block
buzbee311ca162013-02-28 15:56:43 -0800594 bb->taken = bb_next->taken;
595 // Include the rest of the instructions
596 bb->last_mir_insn = bb_next->last_mir_insn;
597 /*
598 * If lower-half of pair of blocks to combine contained a return, move the flag
599 * to the newly combined block.
600 */
601 bb->terminated_by_return = bb_next->terminated_by_return;
602
603 /*
604 * NOTE: we aren't updating all dataflow info here. Should either make sure this pass
605 * happens after uses of i_dominated, dom_frontier or update the dataflow info here.
606 */
607
608 // Kill bb_next and remap now-dead id to parent
609 bb_next->block_type = kDead;
buzbee1fd33462013-03-25 13:40:45 -0700610 block_id_map_.Overwrite(bb_next->id, bb->id);
buzbee311ca162013-02-28 15:56:43 -0800611
612 // Now, loop back and see if we can keep going
613 }
buzbee311ca162013-02-28 15:56:43 -0800614}
615
buzbee1da1e2f2013-11-15 13:37:01 -0800616/*
617 * Eliminate unnecessary null checks for a basic block. Also, while we're doing
618 * an iterative walk go ahead and perform type and size inference.
619 */
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800620bool MIRGraph::EliminateNullChecksAndInferTypes(BasicBlock* bb) {
buzbee311ca162013-02-28 15:56:43 -0800621 if (bb->data_flow_info == NULL) return false;
buzbee1da1e2f2013-11-15 13:37:01 -0800622 bool infer_changed = false;
623 bool do_nce = ((cu_->disable_opt & (1 << kNullCheckElimination)) == 0);
buzbee311ca162013-02-28 15:56:43 -0800624
buzbee1da1e2f2013-11-15 13:37:01 -0800625 if (do_nce) {
626 /*
627 * Set initial state. Be conservative with catch
628 * blocks and start with no assumptions about null check
629 * status (except for "this").
630 */
631 if ((bb->block_type == kEntryBlock) | bb->catch_entry) {
632 temp_ssa_register_v_->ClearAllBits();
633 // Assume all ins are objects.
634 for (uint16_t in_reg = cu_->num_dalvik_registers - cu_->num_ins;
635 in_reg < cu_->num_dalvik_registers; in_reg++) {
636 temp_ssa_register_v_->SetBit(in_reg);
637 }
638 if ((cu_->access_flags & kAccStatic) == 0) {
639 // If non-static method, mark "this" as non-null
640 int this_reg = cu_->num_dalvik_registers - cu_->num_ins;
641 temp_ssa_register_v_->ClearBit(this_reg);
642 }
643 } else if (bb->predecessors->Size() == 1) {
644 BasicBlock* pred_bb = GetBasicBlock(bb->predecessors->Get(0));
645 temp_ssa_register_v_->Copy(pred_bb->data_flow_info->ending_null_check_v);
646 if (pred_bb->block_type == kDalvikByteCode) {
647 // Check to see if predecessor had an explicit null-check.
648 MIR* last_insn = pred_bb->last_mir_insn;
649 Instruction::Code last_opcode = last_insn->dalvikInsn.opcode;
650 if (last_opcode == Instruction::IF_EQZ) {
651 if (pred_bb->fall_through == bb->id) {
652 // The fall-through of a block following a IF_EQZ, set the vA of the IF_EQZ to show that
653 // it can't be null.
654 temp_ssa_register_v_->ClearBit(last_insn->ssa_rep->uses[0]);
655 }
656 } else if (last_opcode == Instruction::IF_NEZ) {
657 if (pred_bb->taken == bb->id) {
658 // The taken block following a IF_NEZ, set the vA of the IF_NEZ to show that it can't be
659 // null.
660 temp_ssa_register_v_->ClearBit(last_insn->ssa_rep->uses[0]);
661 }
Ian Rogers22fd6a02013-06-13 15:06:54 -0700662 }
663 }
buzbee1da1e2f2013-11-15 13:37:01 -0800664 } else {
665 // Starting state is union of all incoming arcs
666 GrowableArray<BasicBlockId>::Iterator iter(bb->predecessors);
667 BasicBlock* pred_bb = GetBasicBlock(iter.Next());
668 DCHECK(pred_bb != NULL);
669 temp_ssa_register_v_->Copy(pred_bb->data_flow_info->ending_null_check_v);
670 while (true) {
671 pred_bb = GetBasicBlock(iter.Next());
672 if (!pred_bb) break;
673 if ((pred_bb->data_flow_info == NULL) ||
674 (pred_bb->data_flow_info->ending_null_check_v == NULL)) {
675 continue;
676 }
677 temp_ssa_register_v_->Union(pred_bb->data_flow_info->ending_null_check_v);
buzbee311ca162013-02-28 15:56:43 -0800678 }
buzbee311ca162013-02-28 15:56:43 -0800679 }
buzbee1da1e2f2013-11-15 13:37:01 -0800680 // At this point, temp_ssa_register_v_ shows which sregs have an object definition with
681 // no intervening uses.
buzbee311ca162013-02-28 15:56:43 -0800682 }
683
684 // Walk through the instruction in the block, updating as necessary
685 for (MIR* mir = bb->first_mir_insn; mir != NULL; mir = mir->next) {
686 if (mir->ssa_rep == NULL) {
687 continue;
688 }
buzbee1da1e2f2013-11-15 13:37:01 -0800689
690 // Propagate type info.
691 infer_changed = InferTypeAndSize(bb, mir, infer_changed);
692 if (!do_nce) {
693 continue;
694 }
695
696 uint64_t df_attributes = oat_data_flow_attributes_[mir->dalvikInsn.opcode];
buzbee311ca162013-02-28 15:56:43 -0800697
Bill Buzbee0b1191c2013-10-28 22:11:59 +0000698 // Might need a null check?
699 if (df_attributes & DF_HAS_NULL_CHKS) {
700 int src_idx;
701 if (df_attributes & DF_NULL_CHK_1) {
702 src_idx = 1;
703 } else if (df_attributes & DF_NULL_CHK_2) {
704 src_idx = 2;
705 } else {
706 src_idx = 0;
707 }
708 int src_sreg = mir->ssa_rep->uses[src_idx];
709 if (!temp_ssa_register_v_->IsBitSet(src_sreg)) {
710 // Eliminate the null check.
711 mir->optimization_flags |= MIR_IGNORE_NULL_CHECK;
712 } else {
713 // Do the null check.
714 mir->optimization_flags &= ~MIR_IGNORE_NULL_CHECK;
715 // Mark s_reg as null-checked
716 temp_ssa_register_v_->ClearBit(src_sreg);
717 }
718 }
719
720 if ((df_attributes & DF_A_WIDE) ||
721 (df_attributes & (DF_REF_A | DF_SETS_CONST | DF_NULL_TRANSFER)) == 0) {
722 continue;
723 }
724
725 /*
726 * First, mark all object definitions as requiring null check.
727 * Note: we can't tell if a CONST definition might be used as an object, so treat
728 * them all as object definitions.
729 */
730 if (((df_attributes & (DF_DA | DF_REF_A)) == (DF_DA | DF_REF_A)) ||
731 (df_attributes & DF_SETS_CONST)) {
Ian Rogers31aa97c2013-10-25 23:07:29 +0000732 temp_ssa_register_v_->SetBit(mir->ssa_rep->defs[0]);
buzbee4db179d2013-10-23 12:16:39 -0700733 }
734
Bill Buzbee0b1191c2013-10-28 22:11:59 +0000735 // Now, remove mark from all object definitions we know are non-null.
736 if (df_attributes & DF_NON_NULL_DST) {
737 // Mark target of NEW* as non-null
738 temp_ssa_register_v_->ClearBit(mir->ssa_rep->defs[0]);
739 }
740
buzbee311ca162013-02-28 15:56:43 -0800741 // Mark non-null returns from invoke-style NEW*
742 if (df_attributes & DF_NON_NULL_RET) {
743 MIR* next_mir = mir->next;
744 // Next should be an MOVE_RESULT_OBJECT
745 if (next_mir &&
746 next_mir->dalvikInsn.opcode == Instruction::MOVE_RESULT_OBJECT) {
747 // Mark as null checked
Bill Buzbee0b1191c2013-10-28 22:11:59 +0000748 temp_ssa_register_v_->ClearBit(next_mir->ssa_rep->defs[0]);
buzbee311ca162013-02-28 15:56:43 -0800749 } else {
750 if (next_mir) {
751 LOG(WARNING) << "Unexpected opcode following new: " << next_mir->dalvikInsn.opcode;
buzbee0d829482013-10-11 15:24:55 -0700752 } else if (bb->fall_through != NullBasicBlockId) {
buzbee311ca162013-02-28 15:56:43 -0800753 // Look in next basic block
buzbee0d829482013-10-11 15:24:55 -0700754 struct BasicBlock* next_bb = GetBasicBlock(bb->fall_through);
buzbee311ca162013-02-28 15:56:43 -0800755 for (MIR* tmir = next_bb->first_mir_insn; tmir != NULL;
756 tmir =tmir->next) {
757 if (static_cast<int>(tmir->dalvikInsn.opcode) >= static_cast<int>(kMirOpFirst)) {
758 continue;
759 }
760 // First non-pseudo should be MOVE_RESULT_OBJECT
761 if (tmir->dalvikInsn.opcode == Instruction::MOVE_RESULT_OBJECT) {
762 // Mark as null checked
Bill Buzbee0b1191c2013-10-28 22:11:59 +0000763 temp_ssa_register_v_->ClearBit(tmir->ssa_rep->defs[0]);
buzbee311ca162013-02-28 15:56:43 -0800764 } else {
765 LOG(WARNING) << "Unexpected op after new: " << tmir->dalvikInsn.opcode;
766 }
767 break;
768 }
769 }
770 }
771 }
772
773 /*
774 * Propagate nullcheck state on register copies (including
775 * Phi pseudo copies. For the latter, nullcheck state is
Bill Buzbee0b1191c2013-10-28 22:11:59 +0000776 * the "or" of all the Phi's operands.
buzbee311ca162013-02-28 15:56:43 -0800777 */
778 if (df_attributes & (DF_NULL_TRANSFER_0 | DF_NULL_TRANSFER_N)) {
779 int tgt_sreg = mir->ssa_rep->defs[0];
780 int operands = (df_attributes & DF_NULL_TRANSFER_0) ? 1 :
781 mir->ssa_rep->num_uses;
Bill Buzbee0b1191c2013-10-28 22:11:59 +0000782 bool needs_null_check = false;
buzbee311ca162013-02-28 15:56:43 -0800783 for (int i = 0; i < operands; i++) {
Bill Buzbee0b1191c2013-10-28 22:11:59 +0000784 needs_null_check |= temp_ssa_register_v_->IsBitSet(mir->ssa_rep->uses[i]);
buzbee311ca162013-02-28 15:56:43 -0800785 }
Bill Buzbee0b1191c2013-10-28 22:11:59 +0000786 if (needs_null_check) {
buzbee862a7602013-04-05 10:58:54 -0700787 temp_ssa_register_v_->SetBit(tgt_sreg);
Bill Buzbee0b1191c2013-10-28 22:11:59 +0000788 } else {
789 temp_ssa_register_v_->ClearBit(tgt_sreg);
buzbee311ca162013-02-28 15:56:43 -0800790 }
791 }
buzbee311ca162013-02-28 15:56:43 -0800792 }
793
794 // Did anything change?
buzbee1da1e2f2013-11-15 13:37:01 -0800795 bool nce_changed = do_nce && !temp_ssa_register_v_->Equal(bb->data_flow_info->ending_null_check_v);
796 if (nce_changed) {
buzbee862a7602013-04-05 10:58:54 -0700797 bb->data_flow_info->ending_null_check_v->Copy(temp_ssa_register_v_);
buzbee311ca162013-02-28 15:56:43 -0800798 }
buzbee1da1e2f2013-11-15 13:37:01 -0800799 return infer_changed | nce_changed;
buzbee311ca162013-02-28 15:56:43 -0800800}
801
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700802void MIRGraph::DumpCheckStats() {
buzbee311ca162013-02-28 15:56:43 -0800803 Checkstats* stats =
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700804 static_cast<Checkstats*>(arena_->Alloc(sizeof(Checkstats), ArenaAllocator::kAllocDFInfo));
buzbee1fd33462013-03-25 13:40:45 -0700805 checkstats_ = stats;
buzbee56c71782013-09-05 17:13:19 -0700806 AllNodesIterator iter(this);
buzbee311ca162013-02-28 15:56:43 -0800807 for (BasicBlock* bb = iter.Next(); bb != NULL; bb = iter.Next()) {
808 CountChecks(bb);
809 }
810 if (stats->null_checks > 0) {
811 float eliminated = static_cast<float>(stats->null_checks_eliminated);
812 float checks = static_cast<float>(stats->null_checks);
813 LOG(INFO) << "Null Checks: " << PrettyMethod(cu_->method_idx, *cu_->dex_file) << " "
814 << stats->null_checks_eliminated << " of " << stats->null_checks << " -> "
815 << (eliminated/checks) * 100.0 << "%";
816 }
817 if (stats->range_checks > 0) {
818 float eliminated = static_cast<float>(stats->range_checks_eliminated);
819 float checks = static_cast<float>(stats->range_checks);
820 LOG(INFO) << "Range Checks: " << PrettyMethod(cu_->method_idx, *cu_->dex_file) << " "
821 << stats->range_checks_eliminated << " of " << stats->range_checks << " -> "
822 << (eliminated/checks) * 100.0 << "%";
823 }
824}
825
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700826bool MIRGraph::BuildExtendedBBList(struct BasicBlock* bb) {
buzbee311ca162013-02-28 15:56:43 -0800827 if (bb->visited) return false;
828 if (!((bb->block_type == kEntryBlock) || (bb->block_type == kDalvikByteCode)
829 || (bb->block_type == kExitBlock))) {
830 // Ignore special blocks
831 bb->visited = true;
832 return false;
833 }
834 // Must be head of extended basic block.
835 BasicBlock* start_bb = bb;
buzbee0d829482013-10-11 15:24:55 -0700836 extended_basic_blocks_.push_back(bb->id);
buzbee311ca162013-02-28 15:56:43 -0800837 bool terminated_by_return = false;
buzbee1da1e2f2013-11-15 13:37:01 -0800838 bool do_local_value_numbering = false;
buzbee311ca162013-02-28 15:56:43 -0800839 // Visit blocks strictly dominated by this head.
840 while (bb != NULL) {
841 bb->visited = true;
842 terminated_by_return |= bb->terminated_by_return;
buzbee1da1e2f2013-11-15 13:37:01 -0800843 do_local_value_numbering |= bb->use_lvn;
buzbee311ca162013-02-28 15:56:43 -0800844 bb = NextDominatedBlock(bb);
845 }
buzbee1da1e2f2013-11-15 13:37:01 -0800846 if (terminated_by_return || do_local_value_numbering) {
847 // Do lvn for all blocks in this extended set.
buzbee311ca162013-02-28 15:56:43 -0800848 bb = start_bb;
849 while (bb != NULL) {
buzbee1da1e2f2013-11-15 13:37:01 -0800850 bb->use_lvn = do_local_value_numbering;
851 bb->dominates_return = terminated_by_return;
buzbee311ca162013-02-28 15:56:43 -0800852 bb = NextDominatedBlock(bb);
853 }
854 }
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700855 return false; // Not iterative - return value will be ignored
buzbee311ca162013-02-28 15:56:43 -0800856}
857
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700858void MIRGraph::BasicBlockOptimization() {
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800859 if ((cu_->disable_opt & (1 << kSuppressExceptionEdges)) != 0) {
860 ClearAllVisitedFlags();
861 PreOrderDfsIterator iter2(this);
862 for (BasicBlock* bb = iter2.Next(); bb != NULL; bb = iter2.Next()) {
863 BuildExtendedBBList(bb);
buzbee311ca162013-02-28 15:56:43 -0800864 }
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800865 // Perform extended basic block optimizations.
866 for (unsigned int i = 0; i < extended_basic_blocks_.size(); i++) {
867 BasicBlockOpt(GetBasicBlock(extended_basic_blocks_[i]));
868 }
869 } else {
870 PreOrderDfsIterator iter(this);
871 for (BasicBlock* bb = iter.Next(); bb != NULL; bb = iter.Next()) {
872 BasicBlockOpt(bb);
873 }
buzbee311ca162013-02-28 15:56:43 -0800874 }
875}
876
877} // namespace art