blob: 2e7f0328d2053aee4a68d8883da25b703982a0dc [file] [log] [blame]
Vladimir Marko7a01dc22015-01-02 17:00:44 +00001/*
2 * Copyright (C) 2015 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 <sstream>
18
19#include "gvn_dead_code_elimination.h"
20
21#include "base/bit_vector-inl.h"
22#include "base/macros.h"
23#include "compiler_enums.h"
24#include "dataflow_iterator-inl.h"
25#include "dex_instruction.h"
26#include "dex/mir_graph.h"
27#include "local_value_numbering.h"
28#include "utils/arena_bit_vector.h"
29
30namespace art {
31
32constexpr uint16_t GvnDeadCodeElimination::kNoValue;
33constexpr uint16_t GvnDeadCodeElimination::kNPos;
34
35inline uint16_t GvnDeadCodeElimination::MIRData::PrevChange(int v_reg) const {
36 DCHECK(has_def);
37 DCHECK(v_reg == vreg_def || v_reg == vreg_def + 1);
38 return (v_reg == vreg_def) ? prev_value.change : prev_value_high.change;
39}
40
41inline void GvnDeadCodeElimination::MIRData::SetPrevChange(int v_reg, uint16_t change) {
42 DCHECK(has_def);
43 DCHECK(v_reg == vreg_def || v_reg == vreg_def + 1);
44 if (v_reg == vreg_def) {
45 prev_value.change = change;
46 } else {
47 prev_value_high.change = change;
48 }
49}
50
51inline void GvnDeadCodeElimination::MIRData::RemovePrevChange(int v_reg, MIRData* prev_data) {
52 DCHECK_NE(PrevChange(v_reg), kNPos);
53 DCHECK(v_reg == prev_data->vreg_def || v_reg == prev_data->vreg_def + 1);
54 if (vreg_def == v_reg) {
55 if (prev_data->vreg_def == v_reg) {
56 prev_value = prev_data->prev_value;
57 low_def_over_high_word = prev_data->low_def_over_high_word;
58 } else {
59 prev_value = prev_data->prev_value_high;
60 low_def_over_high_word =
61 prev_data->prev_value_high.value != kNPos && !prev_data->high_def_over_low_word;
62 }
63 } else {
64 if (prev_data->vreg_def == v_reg) {
65 prev_value_high = prev_data->prev_value;
66 high_def_over_low_word =
67 prev_data->prev_value.value != kNPos && !prev_data->low_def_over_high_word;
68 } else {
69 prev_value_high = prev_data->prev_value_high;
70 high_def_over_low_word = prev_data->high_def_over_low_word;
71 }
72 }
73}
74
75GvnDeadCodeElimination::VRegChains::VRegChains(uint32_t num_vregs, ScopedArenaAllocator* alloc)
76 : num_vregs_(num_vregs),
77 vreg_data_(alloc->AllocArray<VRegValue>(num_vregs, kArenaAllocMisc)),
78 mir_data_(alloc->Adapter()) {
79 mir_data_.reserve(100);
80}
81
82inline void GvnDeadCodeElimination::VRegChains::Reset() {
83 DCHECK(mir_data_.empty());
84 std::fill_n(vreg_data_, num_vregs_, VRegValue());
85}
86
87void GvnDeadCodeElimination::VRegChains::AddMIRWithDef(MIR* mir, int v_reg, bool wide,
88 uint16_t new_value) {
89 uint16_t pos = mir_data_.size();
90 mir_data_.emplace_back(mir);
91 MIRData* data = &mir_data_.back();
92 data->has_def = true;
93 data->wide_def = wide;
94 data->vreg_def = v_reg;
95
96 if (vreg_data_[v_reg].change != kNPos &&
97 mir_data_[vreg_data_[v_reg].change].vreg_def + 1 == v_reg) {
98 data->low_def_over_high_word = true;
99 }
100 data->prev_value = vreg_data_[v_reg];
101 DCHECK_LT(static_cast<size_t>(v_reg), num_vregs_);
102 vreg_data_[v_reg].value = new_value;
103 vreg_data_[v_reg].change = pos;
104
105 if (wide) {
106 if (vreg_data_[v_reg + 1].change != kNPos &&
107 mir_data_[vreg_data_[v_reg + 1].change].vreg_def == v_reg + 1) {
108 data->high_def_over_low_word = true;
109 }
110 data->prev_value_high = vreg_data_[v_reg + 1];
111 DCHECK_LT(static_cast<size_t>(v_reg + 1), num_vregs_);
112 vreg_data_[v_reg + 1].value = new_value;
113 vreg_data_[v_reg + 1].change = pos;
114 }
115}
116
117inline void GvnDeadCodeElimination::VRegChains::AddMIRWithoutDef(MIR* mir) {
118 mir_data_.emplace_back(mir);
119}
120
121void GvnDeadCodeElimination::VRegChains::RemoveLastMIRData() {
122 MIRData* data = LastMIRData();
123 if (data->has_def) {
124 DCHECK_EQ(vreg_data_[data->vreg_def].change, NumMIRs() - 1u);
125 vreg_data_[data->vreg_def] = data->prev_value;
126 if (data->wide_def) {
127 DCHECK_EQ(vreg_data_[data->vreg_def + 1].change, NumMIRs() - 1u);
128 vreg_data_[data->vreg_def + 1] = data->prev_value_high;
129 }
130 }
131 mir_data_.pop_back();
132}
133
134void GvnDeadCodeElimination::VRegChains::RemoveTrailingNops() {
135 // There's at least one NOP to drop. There may be more.
136 MIRData* last_data = LastMIRData();
137 DCHECK(!last_data->must_keep && !last_data->has_def);
138 do {
139 DCHECK_EQ(static_cast<int>(last_data->mir->dalvikInsn.opcode), static_cast<int>(kMirOpNop));
140 mir_data_.pop_back();
141 if (mir_data_.empty()) {
142 break;
143 }
144 last_data = LastMIRData();
145 } while (!last_data->must_keep && !last_data->has_def);
146}
147
148inline size_t GvnDeadCodeElimination::VRegChains::NumMIRs() const {
149 return mir_data_.size();
150}
151
152inline GvnDeadCodeElimination::MIRData* GvnDeadCodeElimination::VRegChains::GetMIRData(size_t pos) {
153 DCHECK_LT(pos, mir_data_.size());
154 return &mir_data_[pos];
155}
156
157inline GvnDeadCodeElimination::MIRData* GvnDeadCodeElimination::VRegChains::LastMIRData() {
158 DCHECK(!mir_data_.empty());
159 return &mir_data_.back();
160}
161
162uint32_t GvnDeadCodeElimination::VRegChains::NumVRegs() const {
163 return num_vregs_;
164}
165
166void GvnDeadCodeElimination::VRegChains::InsertInitialValueHigh(int v_reg, uint16_t value) {
167 DCHECK_NE(value, kNoValue);
168 DCHECK_LT(static_cast<size_t>(v_reg), num_vregs_);
169 uint16_t change = vreg_data_[v_reg].change;
170 if (change == kNPos) {
171 vreg_data_[v_reg].value = value;
172 } else {
173 while (true) {
174 MIRData* data = &mir_data_[change];
175 DCHECK(data->vreg_def == v_reg || data->vreg_def + 1 == v_reg);
176 if (data->vreg_def == v_reg) { // Low word, use prev_value.
177 if (data->prev_value.change == kNPos) {
178 DCHECK_EQ(data->prev_value.value, kNoValue);
179 data->prev_value.value = value;
180 data->low_def_over_high_word = true;
181 break;
182 }
183 change = data->prev_value.change;
184 } else { // High word, use prev_value_high.
185 if (data->prev_value_high.change == kNPos) {
186 DCHECK_EQ(data->prev_value_high.value, kNoValue);
187 data->prev_value_high.value = value;
188 break;
189 }
190 change = data->prev_value_high.change;
191 }
192 }
193 }
194}
195
196void GvnDeadCodeElimination::VRegChains::UpdateInitialVRegValue(int v_reg, bool wide,
197 const LocalValueNumbering* lvn) {
198 DCHECK_LT(static_cast<size_t>(v_reg), num_vregs_);
199 if (!wide) {
200 if (vreg_data_[v_reg].value == kNoValue) {
201 uint16_t old_value = lvn->GetStartingVregValueNumber(v_reg);
202 if (old_value == kNoValue) {
203 // Maybe there was a wide value in v_reg before. Do not check for wide value in v_reg-1,
204 // that will be done only if we see a definition of v_reg-1, otherwise it's unnecessary.
205 old_value = lvn->GetStartingVregValueNumberWide(v_reg);
206 if (old_value != kNoValue) {
207 InsertInitialValueHigh(v_reg + 1, old_value);
208 }
209 }
210 vreg_data_[v_reg].value = old_value;
211 }
212 } else {
213 DCHECK_LT(static_cast<size_t>(v_reg + 1), num_vregs_);
214 bool check_high = true;
215 if (vreg_data_[v_reg].value == kNoValue) {
216 uint16_t old_value = lvn->GetStartingVregValueNumberWide(v_reg);
217 if (old_value != kNoValue) {
218 InsertInitialValueHigh(v_reg + 1, old_value);
219 check_high = false; // High word has been processed.
220 } else {
221 // Maybe there was a narrow value before. Do not check for wide value in v_reg-1,
222 // that will be done only if we see a definition of v_reg-1, otherwise it's unnecessary.
223 old_value = lvn->GetStartingVregValueNumber(v_reg);
224 }
225 vreg_data_[v_reg].value = old_value;
226 }
227 if (check_high && vreg_data_[v_reg + 1].value == kNoValue) {
228 uint16_t old_value = lvn->GetStartingVregValueNumber(v_reg + 1);
229 if (old_value == kNoValue && static_cast<size_t>(v_reg + 2) < num_vregs_) {
230 // Maybe there was a wide value before.
231 old_value = lvn->GetStartingVregValueNumberWide(v_reg + 1);
232 if (old_value != kNoValue) {
233 InsertInitialValueHigh(v_reg + 2, old_value);
234 }
235 }
236 vreg_data_[v_reg + 1].value = old_value;
237 }
238 }
239}
240
241inline uint16_t GvnDeadCodeElimination::VRegChains::LastChange(int v_reg) {
242 DCHECK_LT(static_cast<size_t>(v_reg), num_vregs_);
243 return vreg_data_[v_reg].change;
244}
245
246inline uint16_t GvnDeadCodeElimination::VRegChains::CurrentValue(int v_reg) {
247 DCHECK_LT(static_cast<size_t>(v_reg), num_vregs_);
248 return vreg_data_[v_reg].value;
249}
250
251uint16_t GvnDeadCodeElimination::VRegChains::FindKillHead(int v_reg, uint16_t cutoff) {
252 uint16_t current_value = this->CurrentValue(v_reg);
253 DCHECK_NE(current_value, kNoValue);
254 uint16_t change = LastChange(v_reg);
255 DCHECK_LT(change, mir_data_.size());
256 DCHECK_GE(change, cutoff);
257 bool match_high_word = (mir_data_[change].vreg_def != v_reg);
258 do {
259 MIRData* data = &mir_data_[change];
260 DCHECK(data->vreg_def == v_reg || data->vreg_def + 1 == v_reg);
261 if (data->vreg_def == v_reg) { // Low word, use prev_value.
262 if (data->prev_value.value == current_value &&
263 match_high_word == data->low_def_over_high_word) {
264 break;
265 }
266 change = data->prev_value.change;
267 } else { // High word, use prev_value_high.
268 if (data->prev_value_high.value == current_value &&
269 match_high_word != data->high_def_over_low_word) {
270 break;
271 }
272 change = data->prev_value_high.change;
273 }
274 if (change < cutoff) {
275 change = kNPos;
276 }
277 } while (change != kNPos);
278 return change;
279}
280
281uint16_t GvnDeadCodeElimination::VRegChains::FindFirstChangeAfter(int v_reg,
282 uint16_t change) const {
283 DCHECK_LT(static_cast<size_t>(v_reg), num_vregs_);
284 DCHECK_LT(change, mir_data_.size());
285 uint16_t result = kNPos;
286 uint16_t search_change = vreg_data_[v_reg].change;
287 while (search_change != kNPos && search_change > change) {
288 result = search_change;
289 search_change = mir_data_[search_change].PrevChange(v_reg);
290 }
291 return result;
292}
293
294void GvnDeadCodeElimination::VRegChains::ReplaceChange(uint16_t old_change, uint16_t new_change) {
295 const MIRData* old_data = GetMIRData(old_change);
296 DCHECK(old_data->has_def);
297 int count = old_data->wide_def ? 2 : 1;
298 for (int v_reg = old_data->vreg_def, end = old_data->vreg_def + count; v_reg != end; ++v_reg) {
299 uint16_t next_change = FindFirstChangeAfter(v_reg, old_change);
300 if (next_change == kNPos) {
301 DCHECK_EQ(vreg_data_[v_reg].change, old_change);
302 vreg_data_[v_reg].change = new_change;
303 } else {
304 DCHECK_EQ(mir_data_[next_change].PrevChange(v_reg), old_change);
305 mir_data_[next_change].SetPrevChange(v_reg, new_change);
306 }
307 }
308}
309
310void GvnDeadCodeElimination::VRegChains::RemoveChange(uint16_t change) {
311 MIRData* data = &mir_data_[change];
312 DCHECK(data->has_def);
313 int count = data->wide_def ? 2 : 1;
314 for (int v_reg = data->vreg_def, end = data->vreg_def + count; v_reg != end; ++v_reg) {
315 uint16_t next_change = FindFirstChangeAfter(v_reg, change);
316 if (next_change == kNPos) {
317 DCHECK_EQ(vreg_data_[v_reg].change, change);
318 vreg_data_[v_reg] = (data->vreg_def == v_reg) ? data->prev_value : data->prev_value_high;
319 } else {
320 DCHECK_EQ(mir_data_[next_change].PrevChange(v_reg), change);
321 mir_data_[next_change].RemovePrevChange(v_reg, data);
322 }
323 }
324}
325
326inline bool GvnDeadCodeElimination::VRegChains::IsTopChange(uint16_t change) const {
327 DCHECK_LT(change, mir_data_.size());
328 const MIRData* data = &mir_data_[change];
329 DCHECK(data->has_def);
330 DCHECK_LT(data->wide_def ? data->vreg_def + 1u : data->vreg_def, num_vregs_);
331 return vreg_data_[data->vreg_def].change == change &&
332 (!data->wide_def || vreg_data_[data->vreg_def + 1u].change == change);
333}
334
335bool GvnDeadCodeElimination::VRegChains::IsSRegUsed(uint16_t first_change, uint16_t last_change,
336 int s_reg) const {
337 DCHECK_LE(first_change, last_change);
338 DCHECK_LE(last_change, mir_data_.size());
339 for (size_t c = first_change; c != last_change; ++c) {
340 SSARepresentation* ssa_rep = mir_data_[c].mir->ssa_rep;
341 for (int i = 0; i != ssa_rep->num_uses; ++i) {
342 if (ssa_rep->uses[i] == s_reg) {
343 return true;
344 }
345 }
346 }
347 return false;
348}
349
350void GvnDeadCodeElimination::VRegChains::RenameSRegUses(uint16_t first_change, uint16_t last_change,
351 int old_s_reg, int new_s_reg, bool wide) {
352 for (size_t c = first_change; c != last_change; ++c) {
353 SSARepresentation* ssa_rep = mir_data_[c].mir->ssa_rep;
354 for (int i = 0; i != ssa_rep->num_uses; ++i) {
355 if (ssa_rep->uses[i] == old_s_reg) {
356 ssa_rep->uses[i] = new_s_reg;
357 if (wide) {
358 ++i;
359 DCHECK_LT(i, ssa_rep->num_uses);
360 ssa_rep->uses[i] = new_s_reg + 1;
361 }
362 }
363 }
364 }
365}
366
367void GvnDeadCodeElimination::VRegChains::RenameVRegUses(uint16_t first_change, uint16_t last_change,
368 int old_s_reg, int old_v_reg,
369 int new_s_reg, int new_v_reg) {
370 for (size_t c = first_change; c != last_change; ++c) {
371 MIR* mir = mir_data_[c].mir;
372 if (IsInstructionBinOp2Addr(mir->dalvikInsn.opcode) &&
373 mir->ssa_rep->uses[0] == old_s_reg && old_v_reg != new_v_reg) {
374 // Rewrite binop_2ADDR with plain binop before doing the register rename.
375 ChangeBinOp2AddrToPlainBinOp(mir);
376 }
377 uint64_t df_attr = MIRGraph::GetDataFlowAttributes(mir);
378 size_t use = 0u;
379#define REPLACE_VREG(REG) \
380 if ((df_attr & DF_U##REG) != 0) { \
381 if (mir->ssa_rep->uses[use] == old_s_reg) { \
382 DCHECK_EQ(mir->dalvikInsn.v##REG, static_cast<uint32_t>(old_v_reg)); \
383 mir->dalvikInsn.v##REG = new_v_reg; \
384 mir->ssa_rep->uses[use] = new_s_reg; \
385 if ((df_attr & DF_##REG##_WIDE) != 0) { \
386 DCHECK_EQ(mir->ssa_rep->uses[use + 1], old_s_reg + 1); \
387 mir->ssa_rep->uses[use + 1] = new_s_reg + 1; \
388 } \
389 } \
390 use += ((df_attr & DF_##REG##_WIDE) != 0) ? 2 : 1; \
391 }
392 REPLACE_VREG(A)
393 REPLACE_VREG(B)
394 REPLACE_VREG(C)
395#undef REPLACE_VREG
396 // We may encounter an out-of-order Phi which we need to ignore, otherwise we should
397 // only be asked to rename registers specified by DF_UA, DF_UB and DF_UC.
398 DCHECK_EQ(use,
399 static_cast<int>(mir->dalvikInsn.opcode) == kMirOpPhi
400 ? 0u
401 : static_cast<size_t>(mir->ssa_rep->num_uses));
402 }
403}
404
405GvnDeadCodeElimination::GvnDeadCodeElimination(const GlobalValueNumbering* gvn,
406 ScopedArenaAllocator* alloc)
407 : gvn_(gvn),
408 mir_graph_(gvn_->GetMirGraph()),
409 vreg_chains_(mir_graph_->GetNumOfCodeAndTempVRs(), alloc),
410 bb_(nullptr),
411 lvn_(nullptr),
412 no_uses_all_since_(0u),
413 unused_vregs_(new (alloc) ArenaBitVector(alloc, vreg_chains_.NumVRegs(), false)),
414 vregs_to_kill_(new (alloc) ArenaBitVector(alloc, vreg_chains_.NumVRegs(), false)),
415 kill_heads_(alloc->AllocArray<uint16_t>(vreg_chains_.NumVRegs(), kArenaAllocMisc)),
416 changes_to_kill_(alloc->Adapter()),
417 dependent_vregs_(new (alloc) ArenaBitVector(alloc, vreg_chains_.NumVRegs(), false)) {
418 changes_to_kill_.reserve(16u);
419}
420
421void GvnDeadCodeElimination::Apply(BasicBlock* bb) {
422 bb_ = bb;
423 lvn_ = gvn_->GetLvn(bb->id);
424
425 RecordPass();
426 BackwardPass();
427
428 DCHECK_EQ(no_uses_all_since_, 0u);
429 lvn_ = nullptr;
430 bb_ = nullptr;
431}
432
433void GvnDeadCodeElimination::RecordPass() {
434 // Record MIRs with vreg definition data, eliminate single instructions.
435 vreg_chains_.Reset();
436 DCHECK_EQ(no_uses_all_since_, 0u);
437 for (MIR* mir = bb_->first_mir_insn; mir != nullptr; mir = mir->next) {
438 if (RecordMIR(mir)) {
439 RecordPassTryToKillOverwrittenMoveOrMoveSrc();
440 RecordPassTryToKillLastMIR();
441 }
442 }
443}
444
445void GvnDeadCodeElimination::BackwardPass() {
446 // Now process MIRs in reverse order, trying to eliminate them.
447 unused_vregs_->ClearAllBits(); // Implicitly depend on all vregs at the end of BB.
448 while (vreg_chains_.NumMIRs() != 0u) {
449 if (BackwardPassTryToKillLastMIR()) {
450 continue;
451 }
452 BackwardPassProcessLastMIR();
453 }
454}
455
456void GvnDeadCodeElimination::KillMIR(MIRData* data) {
457 DCHECK(!data->must_keep);
458 DCHECK(!data->uses_all_vregs);
459 DCHECK(data->has_def);
460 DCHECK(data->mir->ssa_rep->num_defs == 1 || data->mir->ssa_rep->num_defs == 2);
461
462 KillMIR(data->mir);
463 data->has_def = false;
464 data->is_move = false;
465 data->is_move_src = false;
466}
467
468void GvnDeadCodeElimination::KillMIR(MIR* mir) {
469 mir->dalvikInsn.opcode = static_cast<Instruction::Code>(kMirOpNop);
470 mir->ssa_rep->num_uses = 0;
471 mir->ssa_rep->num_defs = 0;
472}
473
474void GvnDeadCodeElimination::ChangeBinOp2AddrToPlainBinOp(MIR* mir) {
475 mir->dalvikInsn.vC = mir->dalvikInsn.vB;
476 mir->dalvikInsn.vB = mir->dalvikInsn.vA;
477 mir->dalvikInsn.opcode = static_cast<Instruction::Code>(
478 mir->dalvikInsn.opcode - Instruction::ADD_INT_2ADDR + Instruction::ADD_INT);
479}
480
481MIR* GvnDeadCodeElimination::CreatePhi(int s_reg, bool fp) {
482 int v_reg = mir_graph_->SRegToVReg(s_reg);
483 MIR* phi = mir_graph_->NewMIR();
484 phi->dalvikInsn.opcode = static_cast<Instruction::Code>(kMirOpPhi);
485 phi->dalvikInsn.vA = v_reg;
486 phi->offset = bb_->start_offset;
487 phi->m_unit_index = 0; // Arbitrarily assign all Phi nodes to outermost method.
488
489 phi->ssa_rep = static_cast<struct SSARepresentation *>(mir_graph_->GetArena()->Alloc(
490 sizeof(SSARepresentation), kArenaAllocDFInfo));
491
492 mir_graph_->AllocateSSADefData(phi, 1);
493 phi->ssa_rep->defs[0] = s_reg;
494 phi->ssa_rep->fp_def[0] = fp;
495
496 size_t num_uses = bb_->predecessors.size();
497 mir_graph_->AllocateSSAUseData(phi, num_uses);
498 std::fill_n(phi->ssa_rep->fp_use, num_uses, fp);
499 size_t idx = 0u;
500 for (BasicBlockId pred_id : bb_->predecessors) {
501 BasicBlock* pred_bb = mir_graph_->GetBasicBlock(pred_id);
502 DCHECK(pred_bb != nullptr);
503 phi->ssa_rep->uses[idx] = pred_bb->data_flow_info->vreg_to_ssa_map_exit[v_reg];
504 DCHECK_NE(phi->ssa_rep->uses[idx], INVALID_SREG);
505 idx++;
506 }
507
508 phi->meta.phi_incoming = static_cast<BasicBlockId*>(mir_graph_->GetArena()->Alloc(
509 sizeof(BasicBlockId) * num_uses, kArenaAllocDFInfo));
510 std::copy(bb_->predecessors.begin(), bb_->predecessors.end(), phi->meta.phi_incoming);
511 bb_->PrependMIR(phi);
512 return phi;
513}
514
515MIR* GvnDeadCodeElimination::RenameSRegDefOrCreatePhi(uint16_t def_change, uint16_t last_change,
516 MIR* mir_to_kill) {
517 DCHECK(mir_to_kill->ssa_rep->num_defs == 1 || mir_to_kill->ssa_rep->num_defs == 2);
518 bool wide = (mir_to_kill->ssa_rep->num_defs != 1);
519 int new_s_reg = mir_to_kill->ssa_rep->defs[0];
520
521 // Just before we kill mir_to_kill, we need to replace the previous SSA reg assigned to the
522 // same dalvik reg to keep consistency with subsequent instructions. However, if there's no
523 // defining MIR for that dalvik reg, the preserved valus must come from its predecessors
524 // and we need to create a new Phi (a degenerate Phi if there's only a single predecessor).
525 if (def_change == kNPos) {
526 bool fp = mir_to_kill->ssa_rep->fp_def[0];
527 if (wide) {
528 DCHECK_EQ(new_s_reg + 1, mir_to_kill->ssa_rep->defs[1]);
529 DCHECK_EQ(fp, mir_to_kill->ssa_rep->fp_def[1]);
530 DCHECK_EQ(mir_graph_->SRegToVReg(new_s_reg) + 1, mir_graph_->SRegToVReg(new_s_reg + 1));
531 CreatePhi(new_s_reg + 1, fp); // High word Phi.
532 }
533 return CreatePhi(new_s_reg, fp);
534 } else {
535 DCHECK_LT(def_change, last_change);
536 DCHECK_LE(last_change, vreg_chains_.NumMIRs());
537 MIRData* def_data = vreg_chains_.GetMIRData(def_change);
538 DCHECK(def_data->has_def);
539 int old_s_reg = def_data->mir->ssa_rep->defs[0];
540 DCHECK_NE(old_s_reg, new_s_reg);
541 DCHECK_EQ(mir_graph_->SRegToVReg(old_s_reg), mir_graph_->SRegToVReg(new_s_reg));
542 def_data->mir->ssa_rep->defs[0] = new_s_reg;
543 if (wide) {
544 if (static_cast<int>(def_data->mir->dalvikInsn.opcode) == kMirOpPhi) {
545 // Currently the high word Phi is always located after the low word Phi.
546 MIR* phi_high = def_data->mir->next;
547 DCHECK(phi_high != nullptr && static_cast<int>(phi_high->dalvikInsn.opcode) == kMirOpPhi);
548 DCHECK_EQ(phi_high->ssa_rep->defs[0], old_s_reg + 1);
549 phi_high->ssa_rep->defs[0] = new_s_reg + 1;
550 } else {
551 DCHECK_EQ(def_data->mir->ssa_rep->defs[1], old_s_reg + 1);
552 def_data->mir->ssa_rep->defs[1] = new_s_reg + 1;
553 }
554 }
555 vreg_chains_.RenameSRegUses(def_change + 1u, last_change, old_s_reg, new_s_reg, wide);
556 return nullptr;
557 }
558}
559
560
561void GvnDeadCodeElimination::BackwardPassProcessLastMIR() {
562 MIRData* data = vreg_chains_.LastMIRData();
563 if (data->uses_all_vregs) {
564 DCHECK(data->must_keep);
565 unused_vregs_->ClearAllBits();
566 DCHECK_EQ(no_uses_all_since_, vreg_chains_.NumMIRs());
567 --no_uses_all_since_;
568 while (no_uses_all_since_ != 0u &&
569 !vreg_chains_.GetMIRData(no_uses_all_since_ - 1u)->uses_all_vregs) {
570 --no_uses_all_since_;
571 }
572 } else {
573 if (data->has_def) {
574 unused_vregs_->SetBit(data->vreg_def);
575 if (data->wide_def) {
576 unused_vregs_->SetBit(data->vreg_def + 1);
577 }
578 }
579 for (int i = 0, num_uses = data->mir->ssa_rep->num_uses; i != num_uses; ++i) {
580 int v_reg = mir_graph_->SRegToVReg(data->mir->ssa_rep->uses[i]);
581 unused_vregs_->ClearBit(v_reg);
582 }
583 }
584 vreg_chains_.RemoveLastMIRData();
585}
586
587void GvnDeadCodeElimination::RecordPassKillMoveByRenamingSrcDef(uint16_t src_change,
588 uint16_t move_change) {
589 DCHECK_LT(src_change, move_change);
590 MIRData* src_data = vreg_chains_.GetMIRData(src_change);
591 MIRData* move_data = vreg_chains_.GetMIRData(move_change);
592 DCHECK(src_data->is_move_src);
593 DCHECK_EQ(src_data->wide_def, move_data->wide_def);
594 DCHECK(move_data->prev_value.change == kNPos || move_data->prev_value.change <= src_change);
595 DCHECK(!move_data->wide_def || move_data->prev_value_high.change == kNPos ||
596 move_data->prev_value_high.change <= src_change);
597
598 int old_s_reg = src_data->mir->ssa_rep->defs[0];
599 // NOTE: old_s_reg may differ from move_data->mir->ssa_rep->uses[0]; value names must match.
600 int new_s_reg = move_data->mir->ssa_rep->defs[0];
601 DCHECK_NE(old_s_reg, new_s_reg);
602
603 if (IsInstructionBinOp2Addr(src_data->mir->dalvikInsn.opcode) &&
604 src_data->vreg_def != move_data->vreg_def) {
605 // Rewrite binop_2ADDR with plain binop before doing the register rename.
606 ChangeBinOp2AddrToPlainBinOp(src_data->mir);
607 }
608 // Remove src_change from the vreg chain(s).
609 vreg_chains_.RemoveChange(src_change);
610 // Replace the move_change with the src_change, copying all necessary data.
611 src_data->is_move_src = move_data->is_move_src;
612 src_data->low_def_over_high_word = move_data->low_def_over_high_word;
613 src_data->high_def_over_low_word = move_data->high_def_over_low_word;
614 src_data->vreg_def = move_data->vreg_def;
615 src_data->prev_value = move_data->prev_value;
616 src_data->prev_value_high = move_data->prev_value_high;
617 src_data->mir->dalvikInsn.vA = move_data->vreg_def;
618 src_data->mir->ssa_rep->defs[0] = new_s_reg;
619 if (move_data->wide_def) {
620 DCHECK_EQ(src_data->mir->ssa_rep->defs[1], old_s_reg + 1);
621 src_data->mir->ssa_rep->defs[1] = new_s_reg + 1;
622 }
623 vreg_chains_.ReplaceChange(move_change, src_change);
624
625 // Rename uses and kill the move.
626 vreg_chains_.RenameVRegUses(src_change + 1u, vreg_chains_.NumMIRs(),
627 old_s_reg, mir_graph_->SRegToVReg(old_s_reg),
628 new_s_reg, mir_graph_->SRegToVReg(new_s_reg));
629 KillMIR(move_data);
630}
631
632void GvnDeadCodeElimination::RecordPassTryToKillOverwrittenMoveOrMoveSrc(uint16_t check_change) {
633 MIRData* data = vreg_chains_.GetMIRData(check_change);
634 DCHECK(data->is_move || data->is_move_src);
635 int32_t dest_s_reg = data->mir->ssa_rep->defs[0];
636
637 if (data->is_move) {
638 // Check if source vreg has changed since the MOVE.
639 int32_t src_s_reg = data->mir->ssa_rep->uses[0];
640 uint32_t src_v_reg = mir_graph_->SRegToVReg(src_s_reg);
641 uint16_t src_change = vreg_chains_.FindFirstChangeAfter(src_v_reg, check_change);
642 bool wide = data->wide_def;
643 if (wide) {
644 uint16_t src_change_high = vreg_chains_.FindFirstChangeAfter(src_v_reg + 1, check_change);
645 if (src_change_high != kNPos && (src_change == kNPos || src_change_high < src_change)) {
646 src_change = src_change_high;
647 }
648 }
649 if (src_change == kNPos ||
650 !vreg_chains_.IsSRegUsed(src_change + 1u, vreg_chains_.NumMIRs(), dest_s_reg)) {
651 // We can simply change all uses of dest to src.
652 size_t rename_end = (src_change != kNPos) ? src_change + 1u : vreg_chains_.NumMIRs();
653 vreg_chains_.RenameVRegUses(check_change + 1u, rename_end,
654 dest_s_reg, mir_graph_->SRegToVReg(dest_s_reg),
655 src_s_reg, mir_graph_->SRegToVReg(src_s_reg));
656
657 // Now, remove the MOVE from the vreg chain(s) and kill it.
658 vreg_chains_.RemoveChange(check_change);
659 KillMIR(data);
660 return;
661 }
662 }
663
664 if (data->is_move_src) {
665 // Try to find a MOVE to a vreg that wasn't changed since check_change.
666 uint16_t value_name =
667 data->wide_def ? lvn_->GetSregValueWide(dest_s_reg) : lvn_->GetSregValue(dest_s_reg);
668 for (size_t c = check_change + 1u, size = vreg_chains_.NumMIRs(); c != size; ++c) {
669 MIRData* d = vreg_chains_.GetMIRData(c);
670 if (d->is_move && d->wide_def == data->wide_def &&
671 (d->prev_value.change == kNPos || d->prev_value.change <= check_change) &&
672 (!d->wide_def ||
673 d->prev_value_high.change == kNPos || d->prev_value_high.change <= check_change)) {
674 // Compare value names to find move to move.
675 int32_t src_s_reg = d->mir->ssa_rep->uses[0];
676 uint16_t src_name =
677 (d->wide_def ? lvn_->GetSregValueWide(src_s_reg) : lvn_->GetSregValue(src_s_reg));
678 if (value_name == src_name) {
679 RecordPassKillMoveByRenamingSrcDef(check_change, c);
680 return;
681 }
682 }
683 }
684 }
685}
686
687void GvnDeadCodeElimination::RecordPassTryToKillOverwrittenMoveOrMoveSrc() {
688 // Check if we're overwriting a the result of a move or the definition of a source of a move.
689 // For MOVE_WIDE, we may be overwriting partially; if that's the case, check that the other
690 // word wasn't previously overwritten - we would have tried to rename back then.
691 MIRData* data = vreg_chains_.LastMIRData();
692 if (!data->has_def) {
693 return;
694 }
695 // NOTE: Instructions such as new-array implicitly use all vregs (if they throw) but they can
696 // define a move source which can be renamed. Therefore we allow the checked change to be the
697 // change before no_uses_all_since_. This has no effect on moves as they never use all vregs.
698 if (data->prev_value.change != kNPos && data->prev_value.change + 1u >= no_uses_all_since_) {
699 MIRData* check_data = vreg_chains_.GetMIRData(data->prev_value.change);
700 bool try_to_kill = false;
701 if (!check_data->is_move && !check_data->is_move_src) {
702 DCHECK(!try_to_kill);
703 } else if (!check_data->wide_def) {
704 // Narrow move; always fully overwritten by the last MIR.
705 try_to_kill = true;
706 } else if (data->low_def_over_high_word) {
707 // Overwriting only the high word; is the low word still valid?
708 DCHECK_EQ(check_data->vreg_def + 1u, data->vreg_def);
709 if (vreg_chains_.LastChange(check_data->vreg_def) == data->prev_value.change) {
710 try_to_kill = true;
711 }
712 } else if (!data->wide_def) {
713 // Overwriting only the low word, is the high word still valid?
714 if (vreg_chains_.LastChange(data->vreg_def + 1) == data->prev_value.change) {
715 try_to_kill = true;
716 }
717 } else {
718 // Overwriting both words; was the high word still from the same move?
719 if (data->prev_value_high.change == data->prev_value.change) {
720 try_to_kill = true;
721 }
722 }
723 if (try_to_kill) {
724 RecordPassTryToKillOverwrittenMoveOrMoveSrc(data->prev_value.change);
725 }
726 }
727 if (data->wide_def && data->high_def_over_low_word &&
728 data->prev_value_high.change != kNPos &&
729 data->prev_value_high.change + 1u >= no_uses_all_since_) {
730 MIRData* check_data = vreg_chains_.GetMIRData(data->prev_value_high.change);
731 bool try_to_kill = false;
732 if (!check_data->is_move && !check_data->is_move_src) {
733 DCHECK(!try_to_kill);
734 } else if (!check_data->wide_def) {
735 // Narrow move; always fully overwritten by the last MIR.
736 try_to_kill = true;
737 } else if (vreg_chains_.LastChange(check_data->vreg_def + 1) ==
738 data->prev_value_high.change) {
739 // High word is still valid.
740 try_to_kill = true;
741 }
742 if (try_to_kill) {
743 RecordPassTryToKillOverwrittenMoveOrMoveSrc(data->prev_value_high.change);
744 }
745 }
746}
747
748void GvnDeadCodeElimination::RecordPassTryToKillLastMIR() {
749 MIRData* last_data = vreg_chains_.LastMIRData();
750 if (last_data->must_keep) {
751 return;
752 }
753 if (UNLIKELY(!last_data->has_def)) {
754 // Must be an eliminated MOVE. Drop its data and data of all eliminated MIRs before it.
755 vreg_chains_.RemoveTrailingNops();
756 return;
757 }
758
759 // Try to kill a sequence of consecutive definitions of the same vreg. Allow mixing
760 // wide and non-wide defs; consider high word dead if low word has been overwritten.
761 uint16_t current_value = vreg_chains_.CurrentValue(last_data->vreg_def);
762 uint16_t change = vreg_chains_.NumMIRs() - 1u;
763 MIRData* data = last_data;
764 while (data->prev_value.value != current_value) {
765 --change;
766 if (data->prev_value.change == kNPos || data->prev_value.change != change) {
767 return;
768 }
769 data = vreg_chains_.GetMIRData(data->prev_value.change);
770 if (data->must_keep || !data->has_def || data->vreg_def != last_data->vreg_def) {
771 return;
772 }
773 }
774
775 bool wide = last_data->wide_def;
776 if (wide) {
777 // Check that the low word is valid.
778 if (data->low_def_over_high_word) {
779 return;
780 }
781 // Check that the high word is valid.
782 MIRData* high_data = data;
783 if (!high_data->wide_def) {
784 uint16_t high_change = vreg_chains_.FindFirstChangeAfter(data->vreg_def + 1, change);
785 DCHECK_NE(high_change, kNPos);
786 high_data = vreg_chains_.GetMIRData(high_change);
787 DCHECK_EQ(high_data->vreg_def, data->vreg_def);
788 }
789 if (high_data->prev_value_high.value != current_value || high_data->high_def_over_low_word) {
790 return;
791 }
792 }
793
794 MIR* phi = RenameSRegDefOrCreatePhi(data->prev_value.change, change, last_data->mir);
795 for (size_t i = 0, count = vreg_chains_.NumMIRs() - change; i != count; ++i) {
796 KillMIR(vreg_chains_.LastMIRData()->mir);
797 vreg_chains_.RemoveLastMIRData();
798 }
799 if (phi != nullptr) {
800 // Though the Phi has been added to the beginning, we can put the MIRData at the end.
801 vreg_chains_.AddMIRWithDef(phi, phi->dalvikInsn.vA, wide, current_value);
802 // Reset the previous value to avoid eventually eliminating the Phi itself (unless unused).
803 last_data = vreg_chains_.LastMIRData();
804 last_data->prev_value.value = kNoValue;
805 last_data->prev_value_high.value = kNoValue;
806 }
807}
808
809uint16_t GvnDeadCodeElimination::FindChangesToKill(uint16_t first_change, uint16_t last_change) {
810 // Process dependencies for changes in range [first_change, last_change) and record all
811 // changes that we need to kill. Return kNPos if there's a dependent change that must be
812 // kept unconditionally; otherwise the end of the range processed before encountering
813 // a change that defines a dalvik reg that we need to keep (last_change on full success).
814 changes_to_kill_.clear();
815 dependent_vregs_->ClearAllBits();
816 for (size_t change = first_change; change != last_change; ++change) {
817 MIRData* data = vreg_chains_.GetMIRData(change);
818 DCHECK(!data->uses_all_vregs);
819 bool must_not_depend = data->must_keep;
820 bool depends = false;
821 // Check if the MIR defines a vreg we're trying to eliminate.
822 if (data->has_def && vregs_to_kill_->IsBitSet(data->vreg_def)) {
823 if (change < kill_heads_[data->vreg_def]) {
824 must_not_depend = true;
825 } else {
826 depends = true;
827 }
828 }
829 if (data->has_def && data->wide_def && vregs_to_kill_->IsBitSet(data->vreg_def + 1)) {
830 if (change < kill_heads_[data->vreg_def + 1]) {
831 must_not_depend = true;
832 } else {
833 depends = true;
834 }
835 }
836 if (!depends) {
837 // Check for dependency through SSA reg uses.
838 SSARepresentation* ssa_rep = data->mir->ssa_rep;
839 for (int i = 0; i != ssa_rep->num_uses; ++i) {
840 if (dependent_vregs_->IsBitSet(mir_graph_->SRegToVReg(ssa_rep->uses[i]))) {
841 depends = true;
842 break;
843 }
844 }
845 }
846 // Now check if we can eliminate the insn if we need to.
847 if (depends && must_not_depend) {
848 return kNPos;
849 }
850 if (depends && data->has_def &&
851 vreg_chains_.IsTopChange(change) && !vregs_to_kill_->IsBitSet(data->vreg_def) &&
852 !unused_vregs_->IsBitSet(data->vreg_def) &&
853 (!data->wide_def || !unused_vregs_->IsBitSet(data->vreg_def + 1))) {
854 // This is a top change but neither unnecessary nor one of the top kill changes.
855 return change;
856 }
857 // Finally, update the data.
858 if (depends) {
859 changes_to_kill_.push_back(change);
860 if (data->has_def) {
861 dependent_vregs_->SetBit(data->vreg_def);
862 if (data->wide_def) {
863 dependent_vregs_->SetBit(data->vreg_def + 1);
864 }
865 }
866 } else {
867 if (data->has_def) {
868 dependent_vregs_->ClearBit(data->vreg_def);
869 if (data->wide_def) {
870 dependent_vregs_->ClearBit(data->vreg_def + 1);
871 }
872 }
873 }
874 }
875 return last_change;
876}
877
878void GvnDeadCodeElimination::BackwardPassTryToKillRevertVRegs() {
879}
880
881bool GvnDeadCodeElimination::BackwardPassTryToKillLastMIR() {
882 MIRData* last_data = vreg_chains_.LastMIRData();
883 if (last_data->must_keep) {
884 return false;
885 }
886 DCHECK(!last_data->uses_all_vregs);
887 if (!last_data->has_def) {
888 // Previously eliminated.
889 DCHECK_EQ(static_cast<int>(last_data->mir->dalvikInsn.opcode), static_cast<int>(kMirOpNop));
890 vreg_chains_.RemoveTrailingNops();
891 return true;
892 }
893 if (unused_vregs_->IsBitSet(last_data->vreg_def) ||
894 (last_data->wide_def && unused_vregs_->IsBitSet(last_data->vreg_def + 1))) {
895 if (last_data->wide_def) {
896 // For wide defs, one of the vregs may still be considered needed, fix that.
897 unused_vregs_->SetBit(last_data->vreg_def);
898 unused_vregs_->SetBit(last_data->vreg_def + 1);
899 }
900 KillMIR(last_data->mir);
901 vreg_chains_.RemoveLastMIRData();
902 return true;
903 }
904
905 vregs_to_kill_->ClearAllBits();
906 size_t num_mirs = vreg_chains_.NumMIRs();
907 DCHECK_NE(num_mirs, 0u);
908 uint16_t kill_change = num_mirs - 1u;
909 uint16_t start = num_mirs;
910 size_t num_killed_top_changes = 0u;
911 while (num_killed_top_changes != kMaxNumTopChangesToKill &&
912 kill_change != kNPos && kill_change != num_mirs) {
913 ++num_killed_top_changes;
914
915 DCHECK(vreg_chains_.IsTopChange(kill_change));
916 MIRData* data = vreg_chains_.GetMIRData(kill_change);
917 int count = data->wide_def ? 2 : 1;
918 for (int v_reg = data->vreg_def, end = data->vreg_def + count; v_reg != end; ++v_reg) {
919 uint16_t kill_head = vreg_chains_.FindKillHead(v_reg, no_uses_all_since_);
920 if (kill_head == kNPos) {
921 return false;
922 }
923 kill_heads_[v_reg] = kill_head;
924 vregs_to_kill_->SetBit(v_reg);
925 start = std::min(start, kill_head);
926 }
927 DCHECK_LT(start, vreg_chains_.NumMIRs());
928
929 kill_change = FindChangesToKill(start, num_mirs);
930 }
931
932 if (kill_change != num_mirs) {
933 return false;
934 }
935
936 // Kill all MIRs marked as dependent.
937 for (uint32_t v_reg : vregs_to_kill_->Indexes()) {
938 // Rename s_regs or create Phi only once for each MIR (only for low word).
939 MIRData* data = vreg_chains_.GetMIRData(vreg_chains_.LastChange(v_reg));
940 DCHECK(data->has_def);
941 if (data->vreg_def == v_reg) {
942 MIRData* kill_head_data = vreg_chains_.GetMIRData(kill_heads_[v_reg]);
943 RenameSRegDefOrCreatePhi(kill_head_data->PrevChange(v_reg), num_mirs, data->mir);
944 } else {
945 DCHECK_EQ(data->vreg_def + 1u, v_reg);
946 DCHECK_EQ(vreg_chains_.GetMIRData(kill_heads_[v_reg - 1u])->PrevChange(v_reg - 1u),
947 vreg_chains_.GetMIRData(kill_heads_[v_reg])->PrevChange(v_reg));
948 }
949 }
950 unused_vregs_->Union(vregs_to_kill_);
951 for (auto it = changes_to_kill_.rbegin(), end = changes_to_kill_.rend(); it != end; ++it) {
952 MIRData* data = vreg_chains_.GetMIRData(*it);
953 DCHECK(!data->must_keep);
954 DCHECK(data->has_def);
955 vreg_chains_.RemoveChange(*it);
956 KillMIR(data);
957 }
958
959 vreg_chains_.RemoveTrailingNops();
960 return true;
961}
962
963bool GvnDeadCodeElimination::RecordMIR(MIR* mir) {
964 bool must_keep = false;
965 bool uses_all_vregs = false;
966 bool is_move = false;
967 uint16_t opcode = mir->dalvikInsn.opcode;
968 switch (opcode) {
969 case kMirOpPhi: {
970 // We can't recognize wide variables in Phi from num_defs == 2 as we've got two Phis instead.
971 DCHECK_EQ(mir->ssa_rep->num_defs, 1);
972 int s_reg = mir->ssa_rep->defs[0];
973 bool wide = false;
974 uint16_t new_value = lvn_->GetSregValue(s_reg);
975 if (new_value == kNoValue) {
976 wide = true;
977 new_value = lvn_->GetSregValueWide(s_reg);
978 if (new_value == kNoValue) {
979 return false; // Ignore the high word Phi.
980 }
981 }
982
983 int v_reg = mir_graph_->SRegToVReg(s_reg);
984 DCHECK_EQ(vreg_chains_.CurrentValue(v_reg), kNoValue); // No previous def for v_reg.
985 if (wide) {
986 DCHECK_EQ(vreg_chains_.CurrentValue(v_reg + 1), kNoValue);
987 }
988 vreg_chains_.AddMIRWithDef(mir, v_reg, wide, new_value);
989 return true; // Avoid the common processing.
990 }
991
992 case kMirOpNop:
993 case Instruction::NOP:
994 // Don't record NOPs.
995 return false;
996
997 case kMirOpCheck:
998 must_keep = true;
999 uses_all_vregs = true;
1000 break;
1001
1002 case Instruction::RETURN_VOID:
1003 case Instruction::RETURN:
1004 case Instruction::RETURN_OBJECT:
1005 case Instruction::RETURN_WIDE:
1006 case Instruction::GOTO:
1007 case Instruction::GOTO_16:
1008 case Instruction::GOTO_32:
1009 case Instruction::PACKED_SWITCH:
1010 case Instruction::SPARSE_SWITCH:
1011 case Instruction::IF_EQ:
1012 case Instruction::IF_NE:
1013 case Instruction::IF_LT:
1014 case Instruction::IF_GE:
1015 case Instruction::IF_GT:
1016 case Instruction::IF_LE:
1017 case Instruction::IF_EQZ:
1018 case Instruction::IF_NEZ:
1019 case Instruction::IF_LTZ:
1020 case Instruction::IF_GEZ:
1021 case Instruction::IF_GTZ:
1022 case Instruction::IF_LEZ:
1023 case kMirOpFusedCmplFloat:
1024 case kMirOpFusedCmpgFloat:
1025 case kMirOpFusedCmplDouble:
1026 case kMirOpFusedCmpgDouble:
1027 case kMirOpFusedCmpLong:
1028 must_keep = true;
1029 uses_all_vregs = true; // Keep the implicit dependencies on all vregs.
1030 break;
1031
1032 case Instruction::CONST_CLASS:
1033 case Instruction::CONST_STRING:
1034 case Instruction::CONST_STRING_JUMBO:
1035 // NOTE: While we're currently treating CONST_CLASS, CONST_STRING and CONST_STRING_JUMBO
1036 // as throwing but we could conceivably try and eliminate those exceptions if we're
1037 // retrieving the class/string repeatedly.
1038 must_keep = true;
1039 uses_all_vregs = true;
1040 break;
1041
1042 case Instruction::MONITOR_ENTER:
1043 case Instruction::MONITOR_EXIT:
1044 // We can actually try and optimize across the acquire operation of MONITOR_ENTER,
1045 // the value names provided by GVN reflect the possible changes to memory visibility.
1046 // NOTE: In ART, MONITOR_ENTER and MONITOR_EXIT can throw only NPE.
1047 must_keep = true;
1048 uses_all_vregs = (mir->optimization_flags & MIR_IGNORE_NULL_CHECK) == 0;
1049 break;
1050
1051 case Instruction::INVOKE_DIRECT:
1052 case Instruction::INVOKE_DIRECT_RANGE:
1053 case Instruction::INVOKE_VIRTUAL:
1054 case Instruction::INVOKE_VIRTUAL_RANGE:
1055 case Instruction::INVOKE_SUPER:
1056 case Instruction::INVOKE_SUPER_RANGE:
1057 case Instruction::INVOKE_INTERFACE:
1058 case Instruction::INVOKE_INTERFACE_RANGE:
1059 case Instruction::INVOKE_STATIC:
1060 case Instruction::INVOKE_STATIC_RANGE:
1061 case Instruction::CHECK_CAST:
1062 case Instruction::THROW:
1063 case Instruction::FILLED_NEW_ARRAY:
1064 case Instruction::FILLED_NEW_ARRAY_RANGE:
1065 case Instruction::FILL_ARRAY_DATA:
1066 must_keep = true;
1067 uses_all_vregs = true;
1068 break;
1069
1070 case Instruction::NEW_INSTANCE:
1071 case Instruction::NEW_ARRAY:
1072 must_keep = true;
1073 uses_all_vregs = true;
1074 break;
1075
1076 case kMirOpNullCheck:
1077 DCHECK_EQ(mir->ssa_rep->num_uses, 1);
1078 if ((mir->optimization_flags & MIR_IGNORE_NULL_CHECK) != 0) {
1079 mir->ssa_rep->num_uses = 0;
1080 mir->dalvikInsn.opcode = static_cast<Instruction::Code>(kMirOpNop);
1081 return false;
1082 }
1083 must_keep = true;
1084 uses_all_vregs = true;
1085 break;
1086
1087 case Instruction::MOVE_RESULT:
1088 case Instruction::MOVE_RESULT_OBJECT:
1089 case Instruction::MOVE_RESULT_WIDE:
1090 break;
1091
1092 case Instruction::INSTANCE_OF:
1093 break;
1094
1095 case Instruction::MOVE_EXCEPTION:
1096 must_keep = true;
1097 break;
1098
1099 case kMirOpCopy:
1100 case Instruction::MOVE:
1101 case Instruction::MOVE_FROM16:
1102 case Instruction::MOVE_16:
1103 case Instruction::MOVE_WIDE:
1104 case Instruction::MOVE_WIDE_FROM16:
1105 case Instruction::MOVE_WIDE_16:
1106 case Instruction::MOVE_OBJECT:
1107 case Instruction::MOVE_OBJECT_FROM16:
1108 case Instruction::MOVE_OBJECT_16: {
1109 is_move = true;
1110 // If the MIR defining src vreg is known, allow renaming all uses of src vreg to dest vreg
1111 // while updating the defining MIR to directly define dest vreg. However, changing Phi's
1112 // def this way doesn't work without changing MIRs in other BBs.
1113 int src_v_reg = mir_graph_->SRegToVReg(mir->ssa_rep->uses[0]);
1114 int src_change = vreg_chains_.LastChange(src_v_reg);
1115 if (src_change != kNPos) {
1116 MIRData* src_data = vreg_chains_.GetMIRData(src_change);
1117 if (static_cast<int>(src_data->mir->dalvikInsn.opcode) != kMirOpPhi) {
1118 src_data->is_move_src = true;
1119 }
1120 }
1121 break;
1122 }
1123
1124 case Instruction::CONST_4:
1125 case Instruction::CONST_16:
1126 case Instruction::CONST:
1127 case Instruction::CONST_HIGH16:
1128 case Instruction::CONST_WIDE_16:
1129 case Instruction::CONST_WIDE_32:
1130 case Instruction::CONST_WIDE:
1131 case Instruction::CONST_WIDE_HIGH16:
1132 case Instruction::ARRAY_LENGTH:
1133 case Instruction::CMPL_FLOAT:
1134 case Instruction::CMPG_FLOAT:
1135 case Instruction::CMPL_DOUBLE:
1136 case Instruction::CMPG_DOUBLE:
1137 case Instruction::CMP_LONG:
1138 case Instruction::NEG_INT:
1139 case Instruction::NOT_INT:
1140 case Instruction::NEG_LONG:
1141 case Instruction::NOT_LONG:
1142 case Instruction::NEG_FLOAT:
1143 case Instruction::NEG_DOUBLE:
1144 case Instruction::INT_TO_LONG:
1145 case Instruction::INT_TO_FLOAT:
1146 case Instruction::INT_TO_DOUBLE:
1147 case Instruction::LONG_TO_INT:
1148 case Instruction::LONG_TO_FLOAT:
1149 case Instruction::LONG_TO_DOUBLE:
1150 case Instruction::FLOAT_TO_INT:
1151 case Instruction::FLOAT_TO_LONG:
1152 case Instruction::FLOAT_TO_DOUBLE:
1153 case Instruction::DOUBLE_TO_INT:
1154 case Instruction::DOUBLE_TO_LONG:
1155 case Instruction::DOUBLE_TO_FLOAT:
1156 case Instruction::INT_TO_BYTE:
1157 case Instruction::INT_TO_CHAR:
1158 case Instruction::INT_TO_SHORT:
1159 case Instruction::ADD_INT:
1160 case Instruction::SUB_INT:
1161 case Instruction::MUL_INT:
1162 case Instruction::AND_INT:
1163 case Instruction::OR_INT:
1164 case Instruction::XOR_INT:
1165 case Instruction::SHL_INT:
1166 case Instruction::SHR_INT:
1167 case Instruction::USHR_INT:
1168 case Instruction::ADD_LONG:
1169 case Instruction::SUB_LONG:
1170 case Instruction::MUL_LONG:
1171 case Instruction::AND_LONG:
1172 case Instruction::OR_LONG:
1173 case Instruction::XOR_LONG:
1174 case Instruction::SHL_LONG:
1175 case Instruction::SHR_LONG:
1176 case Instruction::USHR_LONG:
1177 case Instruction::ADD_FLOAT:
1178 case Instruction::SUB_FLOAT:
1179 case Instruction::MUL_FLOAT:
1180 case Instruction::DIV_FLOAT:
1181 case Instruction::REM_FLOAT:
1182 case Instruction::ADD_DOUBLE:
1183 case Instruction::SUB_DOUBLE:
1184 case Instruction::MUL_DOUBLE:
1185 case Instruction::DIV_DOUBLE:
1186 case Instruction::REM_DOUBLE:
1187 case Instruction::ADD_INT_2ADDR:
1188 case Instruction::SUB_INT_2ADDR:
1189 case Instruction::MUL_INT_2ADDR:
1190 case Instruction::AND_INT_2ADDR:
1191 case Instruction::OR_INT_2ADDR:
1192 case Instruction::XOR_INT_2ADDR:
1193 case Instruction::SHL_INT_2ADDR:
1194 case Instruction::SHR_INT_2ADDR:
1195 case Instruction::USHR_INT_2ADDR:
1196 case Instruction::ADD_LONG_2ADDR:
1197 case Instruction::SUB_LONG_2ADDR:
1198 case Instruction::MUL_LONG_2ADDR:
1199 case Instruction::AND_LONG_2ADDR:
1200 case Instruction::OR_LONG_2ADDR:
1201 case Instruction::XOR_LONG_2ADDR:
1202 case Instruction::SHL_LONG_2ADDR:
1203 case Instruction::SHR_LONG_2ADDR:
1204 case Instruction::USHR_LONG_2ADDR:
1205 case Instruction::ADD_FLOAT_2ADDR:
1206 case Instruction::SUB_FLOAT_2ADDR:
1207 case Instruction::MUL_FLOAT_2ADDR:
1208 case Instruction::DIV_FLOAT_2ADDR:
1209 case Instruction::REM_FLOAT_2ADDR:
1210 case Instruction::ADD_DOUBLE_2ADDR:
1211 case Instruction::SUB_DOUBLE_2ADDR:
1212 case Instruction::MUL_DOUBLE_2ADDR:
1213 case Instruction::DIV_DOUBLE_2ADDR:
1214 case Instruction::REM_DOUBLE_2ADDR:
1215 case Instruction::ADD_INT_LIT16:
1216 case Instruction::RSUB_INT:
1217 case Instruction::MUL_INT_LIT16:
1218 case Instruction::AND_INT_LIT16:
1219 case Instruction::OR_INT_LIT16:
1220 case Instruction::XOR_INT_LIT16:
1221 case Instruction::ADD_INT_LIT8:
1222 case Instruction::RSUB_INT_LIT8:
1223 case Instruction::MUL_INT_LIT8:
1224 case Instruction::AND_INT_LIT8:
1225 case Instruction::OR_INT_LIT8:
1226 case Instruction::XOR_INT_LIT8:
1227 case Instruction::SHL_INT_LIT8:
1228 case Instruction::SHR_INT_LIT8:
1229 case Instruction::USHR_INT_LIT8:
1230 break;
1231
1232 case Instruction::DIV_INT:
1233 case Instruction::REM_INT:
1234 case Instruction::DIV_LONG:
1235 case Instruction::REM_LONG:
1236 case Instruction::DIV_INT_2ADDR:
1237 case Instruction::REM_INT_2ADDR:
1238 case Instruction::DIV_LONG_2ADDR:
1239 case Instruction::REM_LONG_2ADDR:
1240 if ((mir->optimization_flags & MIR_IGNORE_DIV_ZERO_CHECK) == 0) {
1241 must_keep = true;
1242 uses_all_vregs = true;
1243 }
1244 break;
1245
1246 case Instruction::DIV_INT_LIT16:
1247 case Instruction::REM_INT_LIT16:
1248 case Instruction::DIV_INT_LIT8:
1249 case Instruction::REM_INT_LIT8:
1250 if (mir->dalvikInsn.vC == 0) { // Explicit division by 0?
1251 must_keep = true;
1252 uses_all_vregs = true;
1253 }
1254 break;
1255
1256 case Instruction::AGET_OBJECT:
1257 case Instruction::AGET:
1258 case Instruction::AGET_WIDE:
1259 case Instruction::AGET_BOOLEAN:
1260 case Instruction::AGET_BYTE:
1261 case Instruction::AGET_CHAR:
1262 case Instruction::AGET_SHORT:
1263 if ((mir->optimization_flags & MIR_IGNORE_NULL_CHECK) == 0 ||
1264 (mir->optimization_flags & MIR_IGNORE_RANGE_CHECK) == 0) {
1265 must_keep = true;
1266 uses_all_vregs = true;
1267 }
1268 break;
1269
1270 case Instruction::APUT_OBJECT:
1271 case Instruction::APUT:
1272 case Instruction::APUT_WIDE:
1273 case Instruction::APUT_BYTE:
1274 case Instruction::APUT_BOOLEAN:
1275 case Instruction::APUT_SHORT:
1276 case Instruction::APUT_CHAR:
1277 must_keep = true;
1278 if ((mir->optimization_flags & MIR_IGNORE_NULL_CHECK) == 0 ||
1279 (mir->optimization_flags & MIR_IGNORE_RANGE_CHECK) == 0) {
1280 uses_all_vregs = true;
1281 }
1282 break;
1283
1284 case Instruction::IGET_OBJECT:
1285 case Instruction::IGET:
1286 case Instruction::IGET_WIDE:
1287 case Instruction::IGET_BOOLEAN:
1288 case Instruction::IGET_BYTE:
1289 case Instruction::IGET_CHAR:
1290 case Instruction::IGET_SHORT: {
1291 const MirIFieldLoweringInfo& info = mir_graph_->GetIFieldLoweringInfo(mir);
1292 if ((mir->optimization_flags & MIR_IGNORE_NULL_CHECK) == 0 ||
1293 !info.IsResolved() || !info.FastGet()) {
1294 must_keep = true;
1295 uses_all_vregs = true;
1296 } else if (info.IsVolatile()) {
1297 must_keep = true;
1298 }
1299 break;
1300 }
1301
1302 case Instruction::IPUT_OBJECT:
1303 case Instruction::IPUT:
1304 case Instruction::IPUT_WIDE:
1305 case Instruction::IPUT_BOOLEAN:
1306 case Instruction::IPUT_BYTE:
1307 case Instruction::IPUT_CHAR:
1308 case Instruction::IPUT_SHORT: {
1309 must_keep = true;
1310 const MirIFieldLoweringInfo& info = mir_graph_->GetIFieldLoweringInfo(mir);
1311 if ((mir->optimization_flags & MIR_IGNORE_NULL_CHECK) == 0 ||
1312 !info.IsResolved() || !info.FastPut()) {
1313 uses_all_vregs = true;
1314 }
1315 break;
1316 }
1317
1318 case Instruction::SGET_OBJECT:
1319 case Instruction::SGET:
1320 case Instruction::SGET_WIDE:
1321 case Instruction::SGET_BOOLEAN:
1322 case Instruction::SGET_BYTE:
1323 case Instruction::SGET_CHAR:
1324 case Instruction::SGET_SHORT: {
1325 const MirSFieldLoweringInfo& info = mir_graph_->GetSFieldLoweringInfo(mir);
1326 if ((mir->optimization_flags & MIR_CLASS_IS_INITIALIZED) == 0 ||
1327 !info.IsResolved() || !info.FastGet()) {
1328 must_keep = true;
1329 uses_all_vregs = true;
1330 } else if (info.IsVolatile()) {
1331 must_keep = true;
1332 }
1333 break;
1334 }
1335
1336 case Instruction::SPUT_OBJECT:
1337 case Instruction::SPUT:
1338 case Instruction::SPUT_WIDE:
1339 case Instruction::SPUT_BOOLEAN:
1340 case Instruction::SPUT_BYTE:
1341 case Instruction::SPUT_CHAR:
1342 case Instruction::SPUT_SHORT: {
1343 must_keep = true;
1344 const MirSFieldLoweringInfo& info = mir_graph_->GetSFieldLoweringInfo(mir);
1345 if ((mir->optimization_flags & MIR_CLASS_IS_INITIALIZED) == 0 ||
1346 !info.IsResolved() || !info.FastPut()) {
1347 uses_all_vregs = true;
1348 }
1349 break;
1350 }
1351
1352 default:
1353 LOG(FATAL) << "Unexpected opcode: " << opcode;
1354 UNREACHABLE();
1355 break;
1356 }
1357
1358 if (mir->ssa_rep->num_defs != 0) {
1359 DCHECK(mir->ssa_rep->num_defs == 1 || mir->ssa_rep->num_defs == 2);
1360 bool wide = (mir->ssa_rep->num_defs == 2);
1361 int s_reg = mir->ssa_rep->defs[0];
1362 int v_reg = mir_graph_->SRegToVReg(s_reg);
1363 uint16_t new_value = wide ? lvn_->GetSregValueWide(s_reg) : lvn_->GetSregValue(s_reg);
1364 DCHECK_NE(new_value, kNoValue);
1365
1366 vreg_chains_.UpdateInitialVRegValue(v_reg, wide, lvn_);
1367 vreg_chains_.AddMIRWithDef(mir, v_reg, wide, new_value);
1368 if (is_move) {
1369 // Allow renaming all uses of dest vreg to src vreg.
1370 vreg_chains_.LastMIRData()->is_move = true;
1371 }
1372 } else {
1373 vreg_chains_.AddMIRWithoutDef(mir);
1374 DCHECK(!is_move) << opcode;
1375 }
1376
1377 if (must_keep) {
1378 MIRData* last_data = vreg_chains_.LastMIRData();
1379 last_data->must_keep = true;
1380 if (uses_all_vregs) {
1381 last_data->uses_all_vregs = true;
1382 no_uses_all_since_ = vreg_chains_.NumMIRs();
1383 }
1384 } else {
1385 DCHECK_NE(mir->ssa_rep->num_defs, 0) << opcode;
1386 DCHECK(!uses_all_vregs) << opcode;
1387 }
1388 return true;
1389}
1390
1391} // namespace art