blob: 209ed3dca86579beed7f9d321fdcd1b693c6fb84 [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
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -0800202size_t MIRGraph::GetNumAvailableNonSpecialCompilerTemps() {
203 if (num_non_special_compiler_temps_ >= max_available_non_special_compiler_temps_) {
204 return 0;
205 } else {
206 return max_available_non_special_compiler_temps_ - num_non_special_compiler_temps_;
207 }
208}
209
210static const RegLocation temp_loc = {kLocCompilerTemp,
211 0, 1 /*defined*/, 0, 0, 0, 0, 0, 1 /*home*/,
212 kVectorNotUsed, INVALID_REG, INVALID_REG, INVALID_SREG,
213 INVALID_SREG};
214
215CompilerTemp* MIRGraph::GetNewCompilerTemp(CompilerTempType ct_type, bool wide) {
216 // There is a limit to the number of non-special temps so check to make sure it wasn't exceeded.
217 if (ct_type == kCompilerTempVR) {
218 size_t available_temps = GetNumAvailableNonSpecialCompilerTemps();
219 if (available_temps <= 0 || (available_temps <= 1 && wide)) {
220 return 0;
221 }
222 }
223
224 CompilerTemp *compiler_temp = static_cast<CompilerTemp *>(arena_->Alloc(sizeof(CompilerTemp),
225 ArenaAllocator::kAllocRegAlloc));
226
227 // Create the type of temp requested. Special temps need special handling because
228 // they have a specific virtual register assignment.
229 if (ct_type == kCompilerTempSpecialMethodPtr) {
230 DCHECK_EQ(wide, false);
231 compiler_temp->v_reg = static_cast<int>(kVRegMethodPtrBaseReg);
232 compiler_temp->s_reg_low = AddNewSReg(compiler_temp->v_reg);
233
234 // The MIR graph keeps track of the sreg for method pointer specially, so record that now.
235 method_sreg_ = compiler_temp->s_reg_low;
236 } else {
237 DCHECK_EQ(ct_type, kCompilerTempVR);
238
239 // The new non-special compiler temp must receive a unique v_reg with a negative value.
240 compiler_temp->v_reg = static_cast<int>(kVRegNonSpecialTempBaseReg) - num_non_special_compiler_temps_;
241 compiler_temp->s_reg_low = AddNewSReg(compiler_temp->v_reg);
242 num_non_special_compiler_temps_++;
243
244 if (wide) {
245 // Ensure that the two registers are consecutive. Since the virtual registers used for temps grow in a
246 // negative fashion, we need the smaller to refer to the low part. Thus, we redefine the v_reg and s_reg_low.
247 compiler_temp->v_reg--;
248 int ssa_reg_high = compiler_temp->s_reg_low;
249 compiler_temp->s_reg_low = AddNewSReg(compiler_temp->v_reg);
250 int ssa_reg_low = compiler_temp->s_reg_low;
251
252 // If needed initialize the register location for the high part.
253 // The low part is handled later in this method on a common path.
254 if (reg_location_ != nullptr) {
255 reg_location_[ssa_reg_high] = temp_loc;
256 reg_location_[ssa_reg_high].high_word = 1;
257 reg_location_[ssa_reg_high].s_reg_low = ssa_reg_low;
258 reg_location_[ssa_reg_high].wide = true;
259
260 // A new SSA needs new use counts.
261 use_counts_.Insert(0);
262 raw_use_counts_.Insert(0);
263 }
264
265 num_non_special_compiler_temps_++;
266 }
267 }
268
269 // Have we already allocated the register locations?
270 if (reg_location_ != nullptr) {
271 int ssa_reg_low = compiler_temp->s_reg_low;
272 reg_location_[ssa_reg_low] = temp_loc;
273 reg_location_[ssa_reg_low].s_reg_low = ssa_reg_low;
274 reg_location_[ssa_reg_low].wide = wide;
275
276 // A new SSA needs new use counts.
277 use_counts_.Insert(0);
278 raw_use_counts_.Insert(0);
279 }
280
281 compiler_temps_.Insert(compiler_temp);
282 return compiler_temp;
283}
buzbee311ca162013-02-28 15:56:43 -0800284
285/* Do some MIR-level extended basic block optimizations */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700286bool MIRGraph::BasicBlockOpt(BasicBlock* bb) {
buzbee311ca162013-02-28 15:56:43 -0800287 if (bb->block_type == kDead) {
288 return true;
289 }
buzbee1da1e2f2013-11-15 13:37:01 -0800290 bool use_lvn = bb->use_lvn;
291 UniquePtr<LocalValueNumbering> local_valnum;
292 if (use_lvn) {
293 local_valnum.reset(new LocalValueNumbering(cu_));
294 }
buzbee311ca162013-02-28 15:56:43 -0800295 while (bb != NULL) {
296 for (MIR* mir = bb->first_mir_insn; mir != NULL; mir = mir->next) {
297 // TUNING: use the returned value number for CSE.
buzbee1da1e2f2013-11-15 13:37:01 -0800298 if (use_lvn) {
299 local_valnum->GetValueNumber(mir);
300 }
buzbee311ca162013-02-28 15:56:43 -0800301 // Look for interesting opcodes, skip otherwise
302 Instruction::Code opcode = mir->dalvikInsn.opcode;
303 switch (opcode) {
304 case Instruction::CMPL_FLOAT:
305 case Instruction::CMPL_DOUBLE:
306 case Instruction::CMPG_FLOAT:
307 case Instruction::CMPG_DOUBLE:
308 case Instruction::CMP_LONG:
buzbee1fd33462013-03-25 13:40:45 -0700309 if ((cu_->disable_opt & (1 << kBranchFusing)) != 0) {
buzbee311ca162013-02-28 15:56:43 -0800310 // Bitcode doesn't allow this optimization.
311 break;
312 }
313 if (mir->next != NULL) {
314 MIR* mir_next = mir->next;
315 Instruction::Code br_opcode = mir_next->dalvikInsn.opcode;
316 ConditionCode ccode = kCondNv;
Brian Carlstromdf629502013-07-17 22:39:56 -0700317 switch (br_opcode) {
buzbee311ca162013-02-28 15:56:43 -0800318 case Instruction::IF_EQZ:
319 ccode = kCondEq;
320 break;
321 case Instruction::IF_NEZ:
322 ccode = kCondNe;
323 break;
324 case Instruction::IF_LTZ:
325 ccode = kCondLt;
326 break;
327 case Instruction::IF_GEZ:
328 ccode = kCondGe;
329 break;
330 case Instruction::IF_GTZ:
331 ccode = kCondGt;
332 break;
333 case Instruction::IF_LEZ:
334 ccode = kCondLe;
335 break;
336 default:
337 break;
338 }
339 // Make sure result of cmp is used by next insn and nowhere else
340 if ((ccode != kCondNv) &&
341 (mir->ssa_rep->defs[0] == mir_next->ssa_rep->uses[0]) &&
342 (GetSSAUseCount(mir->ssa_rep->defs[0]) == 1)) {
Vladimir Markoa8946072014-01-22 10:30:44 +0000343 mir_next->meta.ccode = ccode;
Brian Carlstromdf629502013-07-17 22:39:56 -0700344 switch (opcode) {
buzbee311ca162013-02-28 15:56:43 -0800345 case Instruction::CMPL_FLOAT:
346 mir_next->dalvikInsn.opcode =
347 static_cast<Instruction::Code>(kMirOpFusedCmplFloat);
348 break;
349 case Instruction::CMPL_DOUBLE:
350 mir_next->dalvikInsn.opcode =
351 static_cast<Instruction::Code>(kMirOpFusedCmplDouble);
352 break;
353 case Instruction::CMPG_FLOAT:
354 mir_next->dalvikInsn.opcode =
355 static_cast<Instruction::Code>(kMirOpFusedCmpgFloat);
356 break;
357 case Instruction::CMPG_DOUBLE:
358 mir_next->dalvikInsn.opcode =
359 static_cast<Instruction::Code>(kMirOpFusedCmpgDouble);
360 break;
361 case Instruction::CMP_LONG:
362 mir_next->dalvikInsn.opcode =
363 static_cast<Instruction::Code>(kMirOpFusedCmpLong);
364 break;
365 default: LOG(ERROR) << "Unexpected opcode: " << opcode;
366 }
367 mir->dalvikInsn.opcode = static_cast<Instruction::Code>(kMirOpNop);
368 mir_next->ssa_rep->num_uses = mir->ssa_rep->num_uses;
369 mir_next->ssa_rep->uses = mir->ssa_rep->uses;
370 mir_next->ssa_rep->fp_use = mir->ssa_rep->fp_use;
371 mir_next->ssa_rep->num_defs = 0;
372 mir->ssa_rep->num_uses = 0;
373 mir->ssa_rep->num_defs = 0;
374 }
375 }
376 break;
377 case Instruction::GOTO:
378 case Instruction::GOTO_16:
379 case Instruction::GOTO_32:
380 case Instruction::IF_EQ:
381 case Instruction::IF_NE:
382 case Instruction::IF_LT:
383 case Instruction::IF_GE:
384 case Instruction::IF_GT:
385 case Instruction::IF_LE:
386 case Instruction::IF_EQZ:
387 case Instruction::IF_NEZ:
388 case Instruction::IF_LTZ:
389 case Instruction::IF_GEZ:
390 case Instruction::IF_GTZ:
391 case Instruction::IF_LEZ:
buzbeecbcfaf32013-08-19 07:37:40 -0700392 // If we've got a backwards branch to return, no need to suspend check.
buzbee0d829482013-10-11 15:24:55 -0700393 if ((IsBackedge(bb, bb->taken) && GetBasicBlock(bb->taken)->dominates_return) ||
394 (IsBackedge(bb, bb->fall_through) &&
395 GetBasicBlock(bb->fall_through)->dominates_return)) {
buzbee311ca162013-02-28 15:56:43 -0800396 mir->optimization_flags |= MIR_IGNORE_SUSPEND_CHECK;
397 if (cu_->verbose) {
buzbee0d829482013-10-11 15:24:55 -0700398 LOG(INFO) << "Suppressed suspend check on branch to return at 0x" << std::hex
399 << mir->offset;
buzbee311ca162013-02-28 15:56:43 -0800400 }
401 }
402 break;
403 default:
404 break;
405 }
406 // Is this the select pattern?
Razvan A Lupusorue27b3bf2014-01-23 09:41:45 -0800407 // TODO: flesh out support for Mips. NOTE: llvm's select op doesn't quite work here.
buzbee311ca162013-02-28 15:56:43 -0800408 // TUNING: expand to support IF_xx compare & branches
Razvan A Lupusorue27b3bf2014-01-23 09:41:45 -0800409 if ((cu_->compiler_backend != kPortable) &&
410 (cu_->instruction_set == kThumb2 || cu_->instruction_set == kX86) &&
buzbee311ca162013-02-28 15:56:43 -0800411 ((mir->dalvikInsn.opcode == Instruction::IF_EQZ) ||
412 (mir->dalvikInsn.opcode == Instruction::IF_NEZ))) {
buzbee0d829482013-10-11 15:24:55 -0700413 BasicBlock* ft = GetBasicBlock(bb->fall_through);
buzbee311ca162013-02-28 15:56:43 -0800414 DCHECK(ft != NULL);
buzbee0d829482013-10-11 15:24:55 -0700415 BasicBlock* ft_ft = GetBasicBlock(ft->fall_through);
416 BasicBlock* ft_tk = GetBasicBlock(ft->taken);
buzbee311ca162013-02-28 15:56:43 -0800417
buzbee0d829482013-10-11 15:24:55 -0700418 BasicBlock* tk = GetBasicBlock(bb->taken);
buzbee311ca162013-02-28 15:56:43 -0800419 DCHECK(tk != NULL);
buzbee0d829482013-10-11 15:24:55 -0700420 BasicBlock* tk_ft = GetBasicBlock(tk->fall_through);
421 BasicBlock* tk_tk = GetBasicBlock(tk->taken);
buzbee311ca162013-02-28 15:56:43 -0800422
423 /*
424 * In the select pattern, the taken edge goes to a block that unconditionally
425 * transfers to the rejoin block and the fall_though edge goes to a block that
426 * unconditionally falls through to the rejoin block.
427 */
428 if ((tk_ft == NULL) && (ft_tk == NULL) && (tk_tk == ft_ft) &&
429 (Predecessors(tk) == 1) && (Predecessors(ft) == 1)) {
430 /*
431 * Okay - we have the basic diamond shape. At the very least, we can eliminate the
432 * suspend check on the taken-taken branch back to the join point.
433 */
434 if (SelectKind(tk->last_mir_insn) == kSelectGoto) {
435 tk->last_mir_insn->optimization_flags |= (MIR_IGNORE_SUSPEND_CHECK);
436 }
437 // Are the block bodies something we can handle?
438 if ((ft->first_mir_insn == ft->last_mir_insn) &&
439 (tk->first_mir_insn != tk->last_mir_insn) &&
440 (tk->first_mir_insn->next == tk->last_mir_insn) &&
441 ((SelectKind(ft->first_mir_insn) == kSelectMove) ||
442 (SelectKind(ft->first_mir_insn) == kSelectConst)) &&
443 (SelectKind(ft->first_mir_insn) == SelectKind(tk->first_mir_insn)) &&
444 (SelectKind(tk->last_mir_insn) == kSelectGoto)) {
445 // Almost there. Are the instructions targeting the same vreg?
446 MIR* if_true = tk->first_mir_insn;
447 MIR* if_false = ft->first_mir_insn;
448 // It's possible that the target of the select isn't used - skip those (rare) cases.
449 MIR* phi = FindPhi(tk_tk, if_true->ssa_rep->defs[0]);
450 if ((phi != NULL) && (if_true->dalvikInsn.vA == if_false->dalvikInsn.vA)) {
451 /*
452 * We'll convert the IF_EQZ/IF_NEZ to a SELECT. We need to find the
453 * Phi node in the merge block and delete it (while using the SSA name
454 * of the merge as the target of the SELECT. Delete both taken and
455 * fallthrough blocks, and set fallthrough to merge block.
456 * NOTE: not updating other dataflow info (no longer used at this point).
457 * If this changes, need to update i_dom, etc. here (and in CombineBlocks).
458 */
459 if (opcode == Instruction::IF_NEZ) {
460 // Normalize.
461 MIR* tmp_mir = if_true;
462 if_true = if_false;
463 if_false = tmp_mir;
464 }
465 mir->dalvikInsn.opcode = static_cast<Instruction::Code>(kMirOpSelect);
466 bool const_form = (SelectKind(if_true) == kSelectConst);
467 if ((SelectKind(if_true) == kSelectMove)) {
468 if (IsConst(if_true->ssa_rep->uses[0]) &&
469 IsConst(if_false->ssa_rep->uses[0])) {
470 const_form = true;
471 if_true->dalvikInsn.vB = ConstantValue(if_true->ssa_rep->uses[0]);
472 if_false->dalvikInsn.vB = ConstantValue(if_false->ssa_rep->uses[0]);
473 }
474 }
475 if (const_form) {
Razvan A Lupusorue27b3bf2014-01-23 09:41:45 -0800476 /*
477 * TODO: If both constants are the same value, then instead of generating
478 * a select, we should simply generate a const bytecode. This should be
479 * considered after inlining which can lead to CFG of this form.
480 */
buzbee311ca162013-02-28 15:56:43 -0800481 // "true" set val in vB
482 mir->dalvikInsn.vB = if_true->dalvikInsn.vB;
483 // "false" set val in vC
484 mir->dalvikInsn.vC = if_false->dalvikInsn.vB;
485 } else {
486 DCHECK_EQ(SelectKind(if_true), kSelectMove);
487 DCHECK_EQ(SelectKind(if_false), kSelectMove);
buzbee862a7602013-04-05 10:58:54 -0700488 int* src_ssa =
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700489 static_cast<int*>(arena_->Alloc(sizeof(int) * 3, ArenaAllocator::kAllocDFInfo));
buzbee311ca162013-02-28 15:56:43 -0800490 src_ssa[0] = mir->ssa_rep->uses[0];
491 src_ssa[1] = if_true->ssa_rep->uses[0];
492 src_ssa[2] = if_false->ssa_rep->uses[0];
493 mir->ssa_rep->uses = src_ssa;
494 mir->ssa_rep->num_uses = 3;
495 }
496 mir->ssa_rep->num_defs = 1;
buzbee862a7602013-04-05 10:58:54 -0700497 mir->ssa_rep->defs =
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700498 static_cast<int*>(arena_->Alloc(sizeof(int) * 1, ArenaAllocator::kAllocDFInfo));
buzbee862a7602013-04-05 10:58:54 -0700499 mir->ssa_rep->fp_def =
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700500 static_cast<bool*>(arena_->Alloc(sizeof(bool) * 1, ArenaAllocator::kAllocDFInfo));
buzbee311ca162013-02-28 15:56:43 -0800501 mir->ssa_rep->fp_def[0] = if_true->ssa_rep->fp_def[0];
buzbee817e45a2013-05-30 18:59:12 -0700502 // Match type of uses to def.
503 mir->ssa_rep->fp_use =
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700504 static_cast<bool*>(arena_->Alloc(sizeof(bool) * mir->ssa_rep->num_uses,
505 ArenaAllocator::kAllocDFInfo));
buzbee817e45a2013-05-30 18:59:12 -0700506 for (int i = 0; i < mir->ssa_rep->num_uses; i++) {
507 mir->ssa_rep->fp_use[i] = mir->ssa_rep->fp_def[0];
508 }
buzbee311ca162013-02-28 15:56:43 -0800509 /*
510 * There is usually a Phi node in the join block for our two cases. If the
511 * Phi node only contains our two cases as input, we will use the result
512 * SSA name of the Phi node as our select result and delete the Phi. If
513 * the Phi node has more than two operands, we will arbitrarily use the SSA
514 * name of the "true" path, delete the SSA name of the "false" path from the
515 * Phi node (and fix up the incoming arc list).
516 */
517 if (phi->ssa_rep->num_uses == 2) {
518 mir->ssa_rep->defs[0] = phi->ssa_rep->defs[0];
519 phi->dalvikInsn.opcode = static_cast<Instruction::Code>(kMirOpNop);
520 } else {
521 int dead_def = if_false->ssa_rep->defs[0];
522 int live_def = if_true->ssa_rep->defs[0];
523 mir->ssa_rep->defs[0] = live_def;
buzbee0d829482013-10-11 15:24:55 -0700524 BasicBlockId* incoming = phi->meta.phi_incoming;
buzbee311ca162013-02-28 15:56:43 -0800525 for (int i = 0; i < phi->ssa_rep->num_uses; i++) {
526 if (phi->ssa_rep->uses[i] == live_def) {
527 incoming[i] = bb->id;
528 }
529 }
530 for (int i = 0; i < phi->ssa_rep->num_uses; i++) {
531 if (phi->ssa_rep->uses[i] == dead_def) {
532 int last_slot = phi->ssa_rep->num_uses - 1;
533 phi->ssa_rep->uses[i] = phi->ssa_rep->uses[last_slot];
534 incoming[i] = incoming[last_slot];
535 }
536 }
537 }
538 phi->ssa_rep->num_uses--;
buzbee0d829482013-10-11 15:24:55 -0700539 bb->taken = NullBasicBlockId;
buzbee311ca162013-02-28 15:56:43 -0800540 tk->block_type = kDead;
541 for (MIR* tmir = ft->first_mir_insn; tmir != NULL; tmir = tmir->next) {
542 tmir->dalvikInsn.opcode = static_cast<Instruction::Code>(kMirOpNop);
543 }
544 }
545 }
546 }
547 }
548 }
buzbee1da1e2f2013-11-15 13:37:01 -0800549 bb = ((cu_->disable_opt & (1 << kSuppressExceptionEdges)) != 0) ? NextDominatedBlock(bb) : NULL;
buzbee311ca162013-02-28 15:56:43 -0800550 }
551
buzbee311ca162013-02-28 15:56:43 -0800552 return true;
553}
554
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700555void MIRGraph::NullCheckEliminationInit(struct BasicBlock* bb) {
buzbee862a7602013-04-05 10:58:54 -0700556 if (bb->data_flow_info != NULL) {
557 bb->data_flow_info->ending_null_check_v =
558 new (arena_) ArenaBitVector(arena_, GetNumSSARegs(), false, kBitMapNullCheck);
559 }
buzbee311ca162013-02-28 15:56:43 -0800560}
561
562/* Collect stats on number of checks removed */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700563void MIRGraph::CountChecks(struct BasicBlock* bb) {
buzbee862a7602013-04-05 10:58:54 -0700564 if (bb->data_flow_info != NULL) {
565 for (MIR* mir = bb->first_mir_insn; mir != NULL; mir = mir->next) {
566 if (mir->ssa_rep == NULL) {
567 continue;
buzbee311ca162013-02-28 15:56:43 -0800568 }
buzbee1da1e2f2013-11-15 13:37:01 -0800569 uint64_t df_attributes = oat_data_flow_attributes_[mir->dalvikInsn.opcode];
buzbee862a7602013-04-05 10:58:54 -0700570 if (df_attributes & DF_HAS_NULL_CHKS) {
571 checkstats_->null_checks++;
572 if (mir->optimization_flags & MIR_IGNORE_NULL_CHECK) {
573 checkstats_->null_checks_eliminated++;
574 }
575 }
576 if (df_attributes & DF_HAS_RANGE_CHKS) {
577 checkstats_->range_checks++;
578 if (mir->optimization_flags & MIR_IGNORE_RANGE_CHECK) {
579 checkstats_->range_checks_eliminated++;
580 }
buzbee311ca162013-02-28 15:56:43 -0800581 }
582 }
583 }
buzbee311ca162013-02-28 15:56:43 -0800584}
585
586/* Try to make common case the fallthrough path */
buzbee0d829482013-10-11 15:24:55 -0700587bool MIRGraph::LayoutBlocks(BasicBlock* bb) {
buzbee311ca162013-02-28 15:56:43 -0800588 // TODO: For now, just looking for direct throws. Consider generalizing for profile feedback
589 if (!bb->explicit_throw) {
590 return false;
591 }
592 BasicBlock* walker = bb;
593 while (true) {
594 // Check termination conditions
595 if ((walker->block_type == kEntryBlock) || (Predecessors(walker) != 1)) {
596 break;
597 }
buzbee0d829482013-10-11 15:24:55 -0700598 BasicBlock* prev = GetBasicBlock(walker->predecessors->Get(0));
buzbee311ca162013-02-28 15:56:43 -0800599 if (prev->conditional_branch) {
buzbee0d829482013-10-11 15:24:55 -0700600 if (GetBasicBlock(prev->fall_through) == walker) {
buzbee311ca162013-02-28 15:56:43 -0800601 // Already done - return
602 break;
603 }
buzbee0d829482013-10-11 15:24:55 -0700604 DCHECK_EQ(walker, GetBasicBlock(prev->taken));
buzbee311ca162013-02-28 15:56:43 -0800605 // Got one. Flip it and exit
606 Instruction::Code opcode = prev->last_mir_insn->dalvikInsn.opcode;
607 switch (opcode) {
608 case Instruction::IF_EQ: opcode = Instruction::IF_NE; break;
609 case Instruction::IF_NE: opcode = Instruction::IF_EQ; break;
610 case Instruction::IF_LT: opcode = Instruction::IF_GE; break;
611 case Instruction::IF_GE: opcode = Instruction::IF_LT; break;
612 case Instruction::IF_GT: opcode = Instruction::IF_LE; break;
613 case Instruction::IF_LE: opcode = Instruction::IF_GT; break;
614 case Instruction::IF_EQZ: opcode = Instruction::IF_NEZ; break;
615 case Instruction::IF_NEZ: opcode = Instruction::IF_EQZ; break;
616 case Instruction::IF_LTZ: opcode = Instruction::IF_GEZ; break;
617 case Instruction::IF_GEZ: opcode = Instruction::IF_LTZ; break;
618 case Instruction::IF_GTZ: opcode = Instruction::IF_LEZ; break;
619 case Instruction::IF_LEZ: opcode = Instruction::IF_GTZ; break;
620 default: LOG(FATAL) << "Unexpected opcode " << opcode;
621 }
622 prev->last_mir_insn->dalvikInsn.opcode = opcode;
buzbee0d829482013-10-11 15:24:55 -0700623 BasicBlockId t_bb = prev->taken;
buzbee311ca162013-02-28 15:56:43 -0800624 prev->taken = prev->fall_through;
625 prev->fall_through = t_bb;
626 break;
627 }
628 walker = prev;
629 }
630 return false;
631}
632
633/* Combine any basic blocks terminated by instructions that we now know can't throw */
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800634void MIRGraph::CombineBlocks(struct BasicBlock* bb) {
buzbee311ca162013-02-28 15:56:43 -0800635 // Loop here to allow combining a sequence of blocks
636 while (true) {
637 // Check termination conditions
638 if ((bb->first_mir_insn == NULL)
639 || (bb->data_flow_info == NULL)
640 || (bb->block_type == kExceptionHandling)
641 || (bb->block_type == kExitBlock)
642 || (bb->block_type == kDead)
buzbee0d829482013-10-11 15:24:55 -0700643 || (bb->taken == NullBasicBlockId)
644 || (GetBasicBlock(bb->taken)->block_type != kExceptionHandling)
645 || (bb->successor_block_list_type != kNotUsed)
buzbee311ca162013-02-28 15:56:43 -0800646 || (static_cast<int>(bb->last_mir_insn->dalvikInsn.opcode) != kMirOpCheck)) {
647 break;
648 }
649
650 // Test the kMirOpCheck instruction
651 MIR* mir = bb->last_mir_insn;
652 // Grab the attributes from the paired opcode
653 MIR* throw_insn = mir->meta.throw_insn;
buzbee1da1e2f2013-11-15 13:37:01 -0800654 uint64_t df_attributes = oat_data_flow_attributes_[throw_insn->dalvikInsn.opcode];
buzbee311ca162013-02-28 15:56:43 -0800655 bool can_combine = true;
656 if (df_attributes & DF_HAS_NULL_CHKS) {
657 can_combine &= ((throw_insn->optimization_flags & MIR_IGNORE_NULL_CHECK) != 0);
658 }
659 if (df_attributes & DF_HAS_RANGE_CHKS) {
660 can_combine &= ((throw_insn->optimization_flags & MIR_IGNORE_RANGE_CHECK) != 0);
661 }
662 if (!can_combine) {
663 break;
664 }
665 // OK - got one. Combine
buzbee0d829482013-10-11 15:24:55 -0700666 BasicBlock* bb_next = GetBasicBlock(bb->fall_through);
buzbee311ca162013-02-28 15:56:43 -0800667 DCHECK(!bb_next->catch_entry);
668 DCHECK_EQ(Predecessors(bb_next), 1U);
buzbee311ca162013-02-28 15:56:43 -0800669 // Overwrite the kOpCheck insn with the paired opcode
670 DCHECK_EQ(bb_next->first_mir_insn, throw_insn);
671 *bb->last_mir_insn = *throw_insn;
buzbee311ca162013-02-28 15:56:43 -0800672 // Use the successor info from the next block
buzbee0d829482013-10-11 15:24:55 -0700673 bb->successor_block_list_type = bb_next->successor_block_list_type;
674 bb->successor_blocks = bb_next->successor_blocks;
buzbee311ca162013-02-28 15:56:43 -0800675 // Use the ending block linkage from the next block
676 bb->fall_through = bb_next->fall_through;
buzbee0d829482013-10-11 15:24:55 -0700677 GetBasicBlock(bb->taken)->block_type = kDead; // Kill the unused exception block
buzbee311ca162013-02-28 15:56:43 -0800678 bb->taken = bb_next->taken;
679 // Include the rest of the instructions
680 bb->last_mir_insn = bb_next->last_mir_insn;
681 /*
682 * If lower-half of pair of blocks to combine contained a return, move the flag
683 * to the newly combined block.
684 */
685 bb->terminated_by_return = bb_next->terminated_by_return;
686
687 /*
688 * NOTE: we aren't updating all dataflow info here. Should either make sure this pass
689 * happens after uses of i_dominated, dom_frontier or update the dataflow info here.
690 */
691
692 // Kill bb_next and remap now-dead id to parent
693 bb_next->block_type = kDead;
buzbee1fd33462013-03-25 13:40:45 -0700694 block_id_map_.Overwrite(bb_next->id, bb->id);
buzbee311ca162013-02-28 15:56:43 -0800695
696 // Now, loop back and see if we can keep going
697 }
buzbee311ca162013-02-28 15:56:43 -0800698}
699
buzbee1da1e2f2013-11-15 13:37:01 -0800700/*
701 * Eliminate unnecessary null checks for a basic block. Also, while we're doing
702 * an iterative walk go ahead and perform type and size inference.
703 */
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800704bool MIRGraph::EliminateNullChecksAndInferTypes(BasicBlock* bb) {
buzbee311ca162013-02-28 15:56:43 -0800705 if (bb->data_flow_info == NULL) return false;
buzbee1da1e2f2013-11-15 13:37:01 -0800706 bool infer_changed = false;
707 bool do_nce = ((cu_->disable_opt & (1 << kNullCheckElimination)) == 0);
buzbee311ca162013-02-28 15:56:43 -0800708
buzbee1da1e2f2013-11-15 13:37:01 -0800709 if (do_nce) {
710 /*
711 * Set initial state. Be conservative with catch
712 * blocks and start with no assumptions about null check
713 * status (except for "this").
714 */
715 if ((bb->block_type == kEntryBlock) | bb->catch_entry) {
716 temp_ssa_register_v_->ClearAllBits();
717 // Assume all ins are objects.
718 for (uint16_t in_reg = cu_->num_dalvik_registers - cu_->num_ins;
719 in_reg < cu_->num_dalvik_registers; in_reg++) {
720 temp_ssa_register_v_->SetBit(in_reg);
721 }
722 if ((cu_->access_flags & kAccStatic) == 0) {
723 // If non-static method, mark "this" as non-null
724 int this_reg = cu_->num_dalvik_registers - cu_->num_ins;
725 temp_ssa_register_v_->ClearBit(this_reg);
726 }
727 } else if (bb->predecessors->Size() == 1) {
728 BasicBlock* pred_bb = GetBasicBlock(bb->predecessors->Get(0));
729 temp_ssa_register_v_->Copy(pred_bb->data_flow_info->ending_null_check_v);
730 if (pred_bb->block_type == kDalvikByteCode) {
731 // Check to see if predecessor had an explicit null-check.
732 MIR* last_insn = pred_bb->last_mir_insn;
733 Instruction::Code last_opcode = last_insn->dalvikInsn.opcode;
734 if (last_opcode == Instruction::IF_EQZ) {
735 if (pred_bb->fall_through == bb->id) {
736 // The fall-through of a block following a IF_EQZ, set the vA of the IF_EQZ to show that
737 // it can't be null.
738 temp_ssa_register_v_->ClearBit(last_insn->ssa_rep->uses[0]);
739 }
740 } else if (last_opcode == Instruction::IF_NEZ) {
741 if (pred_bb->taken == bb->id) {
742 // The taken block following a IF_NEZ, set the vA of the IF_NEZ to show that it can't be
743 // null.
744 temp_ssa_register_v_->ClearBit(last_insn->ssa_rep->uses[0]);
745 }
Ian Rogers22fd6a02013-06-13 15:06:54 -0700746 }
747 }
buzbee1da1e2f2013-11-15 13:37:01 -0800748 } else {
749 // Starting state is union of all incoming arcs
750 GrowableArray<BasicBlockId>::Iterator iter(bb->predecessors);
751 BasicBlock* pred_bb = GetBasicBlock(iter.Next());
752 DCHECK(pred_bb != NULL);
753 temp_ssa_register_v_->Copy(pred_bb->data_flow_info->ending_null_check_v);
754 while (true) {
755 pred_bb = GetBasicBlock(iter.Next());
756 if (!pred_bb) break;
757 if ((pred_bb->data_flow_info == NULL) ||
758 (pred_bb->data_flow_info->ending_null_check_v == NULL)) {
759 continue;
760 }
761 temp_ssa_register_v_->Union(pred_bb->data_flow_info->ending_null_check_v);
buzbee311ca162013-02-28 15:56:43 -0800762 }
buzbee311ca162013-02-28 15:56:43 -0800763 }
buzbee1da1e2f2013-11-15 13:37:01 -0800764 // At this point, temp_ssa_register_v_ shows which sregs have an object definition with
765 // no intervening uses.
buzbee311ca162013-02-28 15:56:43 -0800766 }
767
768 // Walk through the instruction in the block, updating as necessary
769 for (MIR* mir = bb->first_mir_insn; mir != NULL; mir = mir->next) {
770 if (mir->ssa_rep == NULL) {
771 continue;
772 }
buzbee1da1e2f2013-11-15 13:37:01 -0800773
774 // Propagate type info.
775 infer_changed = InferTypeAndSize(bb, mir, infer_changed);
776 if (!do_nce) {
777 continue;
778 }
779
780 uint64_t df_attributes = oat_data_flow_attributes_[mir->dalvikInsn.opcode];
buzbee311ca162013-02-28 15:56:43 -0800781
Bill Buzbee0b1191c2013-10-28 22:11:59 +0000782 // Might need a null check?
783 if (df_attributes & DF_HAS_NULL_CHKS) {
784 int src_idx;
785 if (df_attributes & DF_NULL_CHK_1) {
786 src_idx = 1;
787 } else if (df_attributes & DF_NULL_CHK_2) {
788 src_idx = 2;
789 } else {
790 src_idx = 0;
791 }
792 int src_sreg = mir->ssa_rep->uses[src_idx];
793 if (!temp_ssa_register_v_->IsBitSet(src_sreg)) {
794 // Eliminate the null check.
795 mir->optimization_flags |= MIR_IGNORE_NULL_CHECK;
796 } else {
797 // Do the null check.
798 mir->optimization_flags &= ~MIR_IGNORE_NULL_CHECK;
799 // Mark s_reg as null-checked
800 temp_ssa_register_v_->ClearBit(src_sreg);
801 }
802 }
803
804 if ((df_attributes & DF_A_WIDE) ||
805 (df_attributes & (DF_REF_A | DF_SETS_CONST | DF_NULL_TRANSFER)) == 0) {
806 continue;
807 }
808
809 /*
810 * First, mark all object definitions as requiring null check.
811 * Note: we can't tell if a CONST definition might be used as an object, so treat
812 * them all as object definitions.
813 */
814 if (((df_attributes & (DF_DA | DF_REF_A)) == (DF_DA | DF_REF_A)) ||
815 (df_attributes & DF_SETS_CONST)) {
Ian Rogers31aa97c2013-10-25 23:07:29 +0000816 temp_ssa_register_v_->SetBit(mir->ssa_rep->defs[0]);
buzbee4db179d2013-10-23 12:16:39 -0700817 }
818
Bill Buzbee0b1191c2013-10-28 22:11:59 +0000819 // Now, remove mark from all object definitions we know are non-null.
820 if (df_attributes & DF_NON_NULL_DST) {
821 // Mark target of NEW* as non-null
822 temp_ssa_register_v_->ClearBit(mir->ssa_rep->defs[0]);
823 }
824
buzbee311ca162013-02-28 15:56:43 -0800825 // Mark non-null returns from invoke-style NEW*
826 if (df_attributes & DF_NON_NULL_RET) {
827 MIR* next_mir = mir->next;
828 // Next should be an MOVE_RESULT_OBJECT
829 if (next_mir &&
830 next_mir->dalvikInsn.opcode == Instruction::MOVE_RESULT_OBJECT) {
831 // Mark as null checked
Bill Buzbee0b1191c2013-10-28 22:11:59 +0000832 temp_ssa_register_v_->ClearBit(next_mir->ssa_rep->defs[0]);
buzbee311ca162013-02-28 15:56:43 -0800833 } else {
834 if (next_mir) {
835 LOG(WARNING) << "Unexpected opcode following new: " << next_mir->dalvikInsn.opcode;
buzbee0d829482013-10-11 15:24:55 -0700836 } else if (bb->fall_through != NullBasicBlockId) {
buzbee311ca162013-02-28 15:56:43 -0800837 // Look in next basic block
buzbee0d829482013-10-11 15:24:55 -0700838 struct BasicBlock* next_bb = GetBasicBlock(bb->fall_through);
buzbee311ca162013-02-28 15:56:43 -0800839 for (MIR* tmir = next_bb->first_mir_insn; tmir != NULL;
840 tmir =tmir->next) {
841 if (static_cast<int>(tmir->dalvikInsn.opcode) >= static_cast<int>(kMirOpFirst)) {
842 continue;
843 }
844 // First non-pseudo should be MOVE_RESULT_OBJECT
845 if (tmir->dalvikInsn.opcode == Instruction::MOVE_RESULT_OBJECT) {
846 // Mark as null checked
Bill Buzbee0b1191c2013-10-28 22:11:59 +0000847 temp_ssa_register_v_->ClearBit(tmir->ssa_rep->defs[0]);
buzbee311ca162013-02-28 15:56:43 -0800848 } else {
849 LOG(WARNING) << "Unexpected op after new: " << tmir->dalvikInsn.opcode;
850 }
851 break;
852 }
853 }
854 }
855 }
856
857 /*
858 * Propagate nullcheck state on register copies (including
859 * Phi pseudo copies. For the latter, nullcheck state is
Bill Buzbee0b1191c2013-10-28 22:11:59 +0000860 * the "or" of all the Phi's operands.
buzbee311ca162013-02-28 15:56:43 -0800861 */
862 if (df_attributes & (DF_NULL_TRANSFER_0 | DF_NULL_TRANSFER_N)) {
863 int tgt_sreg = mir->ssa_rep->defs[0];
864 int operands = (df_attributes & DF_NULL_TRANSFER_0) ? 1 :
865 mir->ssa_rep->num_uses;
Bill Buzbee0b1191c2013-10-28 22:11:59 +0000866 bool needs_null_check = false;
buzbee311ca162013-02-28 15:56:43 -0800867 for (int i = 0; i < operands; i++) {
Bill Buzbee0b1191c2013-10-28 22:11:59 +0000868 needs_null_check |= temp_ssa_register_v_->IsBitSet(mir->ssa_rep->uses[i]);
buzbee311ca162013-02-28 15:56:43 -0800869 }
Bill Buzbee0b1191c2013-10-28 22:11:59 +0000870 if (needs_null_check) {
buzbee862a7602013-04-05 10:58:54 -0700871 temp_ssa_register_v_->SetBit(tgt_sreg);
Bill Buzbee0b1191c2013-10-28 22:11:59 +0000872 } else {
873 temp_ssa_register_v_->ClearBit(tgt_sreg);
buzbee311ca162013-02-28 15:56:43 -0800874 }
875 }
buzbee311ca162013-02-28 15:56:43 -0800876 }
877
878 // Did anything change?
buzbee1da1e2f2013-11-15 13:37:01 -0800879 bool nce_changed = do_nce && !temp_ssa_register_v_->Equal(bb->data_flow_info->ending_null_check_v);
880 if (nce_changed) {
buzbee862a7602013-04-05 10:58:54 -0700881 bb->data_flow_info->ending_null_check_v->Copy(temp_ssa_register_v_);
buzbee311ca162013-02-28 15:56:43 -0800882 }
buzbee1da1e2f2013-11-15 13:37:01 -0800883 return infer_changed | nce_changed;
buzbee311ca162013-02-28 15:56:43 -0800884}
885
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700886void MIRGraph::DumpCheckStats() {
buzbee311ca162013-02-28 15:56:43 -0800887 Checkstats* stats =
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700888 static_cast<Checkstats*>(arena_->Alloc(sizeof(Checkstats), ArenaAllocator::kAllocDFInfo));
buzbee1fd33462013-03-25 13:40:45 -0700889 checkstats_ = stats;
buzbee56c71782013-09-05 17:13:19 -0700890 AllNodesIterator iter(this);
buzbee311ca162013-02-28 15:56:43 -0800891 for (BasicBlock* bb = iter.Next(); bb != NULL; bb = iter.Next()) {
892 CountChecks(bb);
893 }
894 if (stats->null_checks > 0) {
895 float eliminated = static_cast<float>(stats->null_checks_eliminated);
896 float checks = static_cast<float>(stats->null_checks);
897 LOG(INFO) << "Null Checks: " << PrettyMethod(cu_->method_idx, *cu_->dex_file) << " "
898 << stats->null_checks_eliminated << " of " << stats->null_checks << " -> "
899 << (eliminated/checks) * 100.0 << "%";
900 }
901 if (stats->range_checks > 0) {
902 float eliminated = static_cast<float>(stats->range_checks_eliminated);
903 float checks = static_cast<float>(stats->range_checks);
904 LOG(INFO) << "Range Checks: " << PrettyMethod(cu_->method_idx, *cu_->dex_file) << " "
905 << stats->range_checks_eliminated << " of " << stats->range_checks << " -> "
906 << (eliminated/checks) * 100.0 << "%";
907 }
908}
909
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700910bool MIRGraph::BuildExtendedBBList(struct BasicBlock* bb) {
buzbee311ca162013-02-28 15:56:43 -0800911 if (bb->visited) return false;
912 if (!((bb->block_type == kEntryBlock) || (bb->block_type == kDalvikByteCode)
913 || (bb->block_type == kExitBlock))) {
914 // Ignore special blocks
915 bb->visited = true;
916 return false;
917 }
918 // Must be head of extended basic block.
919 BasicBlock* start_bb = bb;
buzbee0d829482013-10-11 15:24:55 -0700920 extended_basic_blocks_.push_back(bb->id);
buzbee311ca162013-02-28 15:56:43 -0800921 bool terminated_by_return = false;
buzbee1da1e2f2013-11-15 13:37:01 -0800922 bool do_local_value_numbering = false;
buzbee311ca162013-02-28 15:56:43 -0800923 // Visit blocks strictly dominated by this head.
924 while (bb != NULL) {
925 bb->visited = true;
926 terminated_by_return |= bb->terminated_by_return;
buzbee1da1e2f2013-11-15 13:37:01 -0800927 do_local_value_numbering |= bb->use_lvn;
buzbee311ca162013-02-28 15:56:43 -0800928 bb = NextDominatedBlock(bb);
929 }
buzbee1da1e2f2013-11-15 13:37:01 -0800930 if (terminated_by_return || do_local_value_numbering) {
931 // Do lvn for all blocks in this extended set.
buzbee311ca162013-02-28 15:56:43 -0800932 bb = start_bb;
933 while (bb != NULL) {
buzbee1da1e2f2013-11-15 13:37:01 -0800934 bb->use_lvn = do_local_value_numbering;
935 bb->dominates_return = terminated_by_return;
buzbee311ca162013-02-28 15:56:43 -0800936 bb = NextDominatedBlock(bb);
937 }
938 }
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700939 return false; // Not iterative - return value will be ignored
buzbee311ca162013-02-28 15:56:43 -0800940}
941
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700942void MIRGraph::BasicBlockOptimization() {
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800943 if ((cu_->disable_opt & (1 << kSuppressExceptionEdges)) != 0) {
944 ClearAllVisitedFlags();
945 PreOrderDfsIterator iter2(this);
946 for (BasicBlock* bb = iter2.Next(); bb != NULL; bb = iter2.Next()) {
947 BuildExtendedBBList(bb);
buzbee311ca162013-02-28 15:56:43 -0800948 }
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800949 // Perform extended basic block optimizations.
950 for (unsigned int i = 0; i < extended_basic_blocks_.size(); i++) {
951 BasicBlockOpt(GetBasicBlock(extended_basic_blocks_[i]));
952 }
953 } else {
954 PreOrderDfsIterator iter(this);
955 for (BasicBlock* bb = iter.Next(); bb != NULL; bb = iter.Next()) {
956 BasicBlockOpt(bb);
957 }
buzbee311ca162013-02-28 15:56:43 -0800958 }
959}
960
961} // namespace art