blob: 4f0e9d1b673e42f71a5961f108651a3473313695 [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
Vladimir Markoad677272015-04-20 10:48:13 +0100350bool GvnDeadCodeElimination::VRegChains::IsVRegUsed(uint16_t first_change, uint16_t last_change,
351 int v_reg, MIRGraph* mir_graph) const {
352 DCHECK_LE(first_change, last_change);
353 DCHECK_LE(last_change, mir_data_.size());
354 for (size_t c = first_change; c != last_change; ++c) {
355 SSARepresentation* ssa_rep = mir_data_[c].mir->ssa_rep;
356 for (int i = 0; i != ssa_rep->num_uses; ++i) {
357 if (mir_graph->SRegToVReg(ssa_rep->uses[i]) == v_reg) {
358 return true;
359 }
360 }
361 }
362 return false;
363}
364
Vladimir Marko7a01dc22015-01-02 17:00:44 +0000365void GvnDeadCodeElimination::VRegChains::RenameSRegUses(uint16_t first_change, uint16_t last_change,
366 int old_s_reg, int new_s_reg, bool wide) {
367 for (size_t c = first_change; c != last_change; ++c) {
368 SSARepresentation* ssa_rep = mir_data_[c].mir->ssa_rep;
369 for (int i = 0; i != ssa_rep->num_uses; ++i) {
370 if (ssa_rep->uses[i] == old_s_reg) {
371 ssa_rep->uses[i] = new_s_reg;
372 if (wide) {
373 ++i;
374 DCHECK_LT(i, ssa_rep->num_uses);
375 ssa_rep->uses[i] = new_s_reg + 1;
376 }
377 }
378 }
379 }
380}
381
382void GvnDeadCodeElimination::VRegChains::RenameVRegUses(uint16_t first_change, uint16_t last_change,
383 int old_s_reg, int old_v_reg,
384 int new_s_reg, int new_v_reg) {
385 for (size_t c = first_change; c != last_change; ++c) {
386 MIR* mir = mir_data_[c].mir;
387 if (IsInstructionBinOp2Addr(mir->dalvikInsn.opcode) &&
388 mir->ssa_rep->uses[0] == old_s_reg && old_v_reg != new_v_reg) {
389 // Rewrite binop_2ADDR with plain binop before doing the register rename.
390 ChangeBinOp2AddrToPlainBinOp(mir);
391 }
392 uint64_t df_attr = MIRGraph::GetDataFlowAttributes(mir);
393 size_t use = 0u;
394#define REPLACE_VREG(REG) \
395 if ((df_attr & DF_U##REG) != 0) { \
396 if (mir->ssa_rep->uses[use] == old_s_reg) { \
397 DCHECK_EQ(mir->dalvikInsn.v##REG, static_cast<uint32_t>(old_v_reg)); \
398 mir->dalvikInsn.v##REG = new_v_reg; \
399 mir->ssa_rep->uses[use] = new_s_reg; \
400 if ((df_attr & DF_##REG##_WIDE) != 0) { \
401 DCHECK_EQ(mir->ssa_rep->uses[use + 1], old_s_reg + 1); \
402 mir->ssa_rep->uses[use + 1] = new_s_reg + 1; \
403 } \
404 } \
405 use += ((df_attr & DF_##REG##_WIDE) != 0) ? 2 : 1; \
406 }
407 REPLACE_VREG(A)
408 REPLACE_VREG(B)
409 REPLACE_VREG(C)
410#undef REPLACE_VREG
411 // We may encounter an out-of-order Phi which we need to ignore, otherwise we should
412 // only be asked to rename registers specified by DF_UA, DF_UB and DF_UC.
413 DCHECK_EQ(use,
414 static_cast<int>(mir->dalvikInsn.opcode) == kMirOpPhi
415 ? 0u
416 : static_cast<size_t>(mir->ssa_rep->num_uses));
417 }
418}
419
420GvnDeadCodeElimination::GvnDeadCodeElimination(const GlobalValueNumbering* gvn,
421 ScopedArenaAllocator* alloc)
422 : gvn_(gvn),
423 mir_graph_(gvn_->GetMirGraph()),
424 vreg_chains_(mir_graph_->GetNumOfCodeAndTempVRs(), alloc),
425 bb_(nullptr),
426 lvn_(nullptr),
427 no_uses_all_since_(0u),
428 unused_vregs_(new (alloc) ArenaBitVector(alloc, vreg_chains_.NumVRegs(), false)),
429 vregs_to_kill_(new (alloc) ArenaBitVector(alloc, vreg_chains_.NumVRegs(), false)),
430 kill_heads_(alloc->AllocArray<uint16_t>(vreg_chains_.NumVRegs(), kArenaAllocMisc)),
431 changes_to_kill_(alloc->Adapter()),
432 dependent_vregs_(new (alloc) ArenaBitVector(alloc, vreg_chains_.NumVRegs(), false)) {
433 changes_to_kill_.reserve(16u);
434}
435
436void GvnDeadCodeElimination::Apply(BasicBlock* bb) {
437 bb_ = bb;
438 lvn_ = gvn_->GetLvn(bb->id);
439
440 RecordPass();
441 BackwardPass();
442
443 DCHECK_EQ(no_uses_all_since_, 0u);
444 lvn_ = nullptr;
445 bb_ = nullptr;
446}
447
448void GvnDeadCodeElimination::RecordPass() {
449 // Record MIRs with vreg definition data, eliminate single instructions.
450 vreg_chains_.Reset();
451 DCHECK_EQ(no_uses_all_since_, 0u);
452 for (MIR* mir = bb_->first_mir_insn; mir != nullptr; mir = mir->next) {
453 if (RecordMIR(mir)) {
454 RecordPassTryToKillOverwrittenMoveOrMoveSrc();
455 RecordPassTryToKillLastMIR();
456 }
457 }
458}
459
460void GvnDeadCodeElimination::BackwardPass() {
461 // Now process MIRs in reverse order, trying to eliminate them.
462 unused_vregs_->ClearAllBits(); // Implicitly depend on all vregs at the end of BB.
463 while (vreg_chains_.NumMIRs() != 0u) {
464 if (BackwardPassTryToKillLastMIR()) {
465 continue;
466 }
467 BackwardPassProcessLastMIR();
468 }
469}
470
471void GvnDeadCodeElimination::KillMIR(MIRData* data) {
472 DCHECK(!data->must_keep);
473 DCHECK(!data->uses_all_vregs);
474 DCHECK(data->has_def);
475 DCHECK(data->mir->ssa_rep->num_defs == 1 || data->mir->ssa_rep->num_defs == 2);
476
477 KillMIR(data->mir);
478 data->has_def = false;
479 data->is_move = false;
480 data->is_move_src = false;
481}
482
483void GvnDeadCodeElimination::KillMIR(MIR* mir) {
484 mir->dalvikInsn.opcode = static_cast<Instruction::Code>(kMirOpNop);
485 mir->ssa_rep->num_uses = 0;
486 mir->ssa_rep->num_defs = 0;
487}
488
489void GvnDeadCodeElimination::ChangeBinOp2AddrToPlainBinOp(MIR* mir) {
490 mir->dalvikInsn.vC = mir->dalvikInsn.vB;
491 mir->dalvikInsn.vB = mir->dalvikInsn.vA;
492 mir->dalvikInsn.opcode = static_cast<Instruction::Code>(
493 mir->dalvikInsn.opcode - Instruction::ADD_INT_2ADDR + Instruction::ADD_INT);
494}
495
Vladimir Markoc91df2d2015-04-23 09:29:21 +0000496MIR* GvnDeadCodeElimination::CreatePhi(int s_reg) {
Vladimir Marko7a01dc22015-01-02 17:00:44 +0000497 int v_reg = mir_graph_->SRegToVReg(s_reg);
498 MIR* phi = mir_graph_->NewMIR();
499 phi->dalvikInsn.opcode = static_cast<Instruction::Code>(kMirOpPhi);
500 phi->dalvikInsn.vA = v_reg;
501 phi->offset = bb_->start_offset;
502 phi->m_unit_index = 0; // Arbitrarily assign all Phi nodes to outermost method.
503
504 phi->ssa_rep = static_cast<struct SSARepresentation *>(mir_graph_->GetArena()->Alloc(
505 sizeof(SSARepresentation), kArenaAllocDFInfo));
506
507 mir_graph_->AllocateSSADefData(phi, 1);
508 phi->ssa_rep->defs[0] = s_reg;
Vladimir Marko7a01dc22015-01-02 17:00:44 +0000509
510 size_t num_uses = bb_->predecessors.size();
511 mir_graph_->AllocateSSAUseData(phi, num_uses);
Vladimir Marko7a01dc22015-01-02 17:00:44 +0000512 size_t idx = 0u;
513 for (BasicBlockId pred_id : bb_->predecessors) {
514 BasicBlock* pred_bb = mir_graph_->GetBasicBlock(pred_id);
515 DCHECK(pred_bb != nullptr);
516 phi->ssa_rep->uses[idx] = pred_bb->data_flow_info->vreg_to_ssa_map_exit[v_reg];
517 DCHECK_NE(phi->ssa_rep->uses[idx], INVALID_SREG);
518 idx++;
519 }
520
521 phi->meta.phi_incoming = static_cast<BasicBlockId*>(mir_graph_->GetArena()->Alloc(
522 sizeof(BasicBlockId) * num_uses, kArenaAllocDFInfo));
523 std::copy(bb_->predecessors.begin(), bb_->predecessors.end(), phi->meta.phi_incoming);
524 bb_->PrependMIR(phi);
525 return phi;
526}
527
528MIR* GvnDeadCodeElimination::RenameSRegDefOrCreatePhi(uint16_t def_change, uint16_t last_change,
529 MIR* mir_to_kill) {
530 DCHECK(mir_to_kill->ssa_rep->num_defs == 1 || mir_to_kill->ssa_rep->num_defs == 2);
531 bool wide = (mir_to_kill->ssa_rep->num_defs != 1);
532 int new_s_reg = mir_to_kill->ssa_rep->defs[0];
533
534 // Just before we kill mir_to_kill, we need to replace the previous SSA reg assigned to the
535 // same dalvik reg to keep consistency with subsequent instructions. However, if there's no
536 // defining MIR for that dalvik reg, the preserved valus must come from its predecessors
537 // and we need to create a new Phi (a degenerate Phi if there's only a single predecessor).
538 if (def_change == kNPos) {
Vladimir Marko7a01dc22015-01-02 17:00:44 +0000539 if (wide) {
540 DCHECK_EQ(new_s_reg + 1, mir_to_kill->ssa_rep->defs[1]);
Vladimir Marko7a01dc22015-01-02 17:00:44 +0000541 DCHECK_EQ(mir_graph_->SRegToVReg(new_s_reg) + 1, mir_graph_->SRegToVReg(new_s_reg + 1));
Vladimir Markoc91df2d2015-04-23 09:29:21 +0000542 CreatePhi(new_s_reg + 1); // High word Phi.
Vladimir Marko7a01dc22015-01-02 17:00:44 +0000543 }
Vladimir Markoc91df2d2015-04-23 09:29:21 +0000544 return CreatePhi(new_s_reg);
Vladimir Marko7a01dc22015-01-02 17:00:44 +0000545 } else {
546 DCHECK_LT(def_change, last_change);
547 DCHECK_LE(last_change, vreg_chains_.NumMIRs());
548 MIRData* def_data = vreg_chains_.GetMIRData(def_change);
549 DCHECK(def_data->has_def);
550 int old_s_reg = def_data->mir->ssa_rep->defs[0];
551 DCHECK_NE(old_s_reg, new_s_reg);
552 DCHECK_EQ(mir_graph_->SRegToVReg(old_s_reg), mir_graph_->SRegToVReg(new_s_reg));
553 def_data->mir->ssa_rep->defs[0] = new_s_reg;
554 if (wide) {
555 if (static_cast<int>(def_data->mir->dalvikInsn.opcode) == kMirOpPhi) {
556 // Currently the high word Phi is always located after the low word Phi.
557 MIR* phi_high = def_data->mir->next;
558 DCHECK(phi_high != nullptr && static_cast<int>(phi_high->dalvikInsn.opcode) == kMirOpPhi);
559 DCHECK_EQ(phi_high->ssa_rep->defs[0], old_s_reg + 1);
560 phi_high->ssa_rep->defs[0] = new_s_reg + 1;
561 } else {
562 DCHECK_EQ(def_data->mir->ssa_rep->defs[1], old_s_reg + 1);
563 def_data->mir->ssa_rep->defs[1] = new_s_reg + 1;
564 }
565 }
566 vreg_chains_.RenameSRegUses(def_change + 1u, last_change, old_s_reg, new_s_reg, wide);
567 return nullptr;
568 }
569}
570
571
572void GvnDeadCodeElimination::BackwardPassProcessLastMIR() {
573 MIRData* data = vreg_chains_.LastMIRData();
574 if (data->uses_all_vregs) {
575 DCHECK(data->must_keep);
576 unused_vregs_->ClearAllBits();
577 DCHECK_EQ(no_uses_all_since_, vreg_chains_.NumMIRs());
578 --no_uses_all_since_;
579 while (no_uses_all_since_ != 0u &&
580 !vreg_chains_.GetMIRData(no_uses_all_since_ - 1u)->uses_all_vregs) {
581 --no_uses_all_since_;
582 }
583 } else {
584 if (data->has_def) {
585 unused_vregs_->SetBit(data->vreg_def);
586 if (data->wide_def) {
587 unused_vregs_->SetBit(data->vreg_def + 1);
588 }
589 }
590 for (int i = 0, num_uses = data->mir->ssa_rep->num_uses; i != num_uses; ++i) {
591 int v_reg = mir_graph_->SRegToVReg(data->mir->ssa_rep->uses[i]);
592 unused_vregs_->ClearBit(v_reg);
593 }
594 }
595 vreg_chains_.RemoveLastMIRData();
596}
597
598void GvnDeadCodeElimination::RecordPassKillMoveByRenamingSrcDef(uint16_t src_change,
599 uint16_t move_change) {
600 DCHECK_LT(src_change, move_change);
601 MIRData* src_data = vreg_chains_.GetMIRData(src_change);
602 MIRData* move_data = vreg_chains_.GetMIRData(move_change);
603 DCHECK(src_data->is_move_src);
604 DCHECK_EQ(src_data->wide_def, move_data->wide_def);
605 DCHECK(move_data->prev_value.change == kNPos || move_data->prev_value.change <= src_change);
606 DCHECK(!move_data->wide_def || move_data->prev_value_high.change == kNPos ||
607 move_data->prev_value_high.change <= src_change);
608
609 int old_s_reg = src_data->mir->ssa_rep->defs[0];
610 // NOTE: old_s_reg may differ from move_data->mir->ssa_rep->uses[0]; value names must match.
611 int new_s_reg = move_data->mir->ssa_rep->defs[0];
612 DCHECK_NE(old_s_reg, new_s_reg);
613
614 if (IsInstructionBinOp2Addr(src_data->mir->dalvikInsn.opcode) &&
615 src_data->vreg_def != move_data->vreg_def) {
616 // Rewrite binop_2ADDR with plain binop before doing the register rename.
617 ChangeBinOp2AddrToPlainBinOp(src_data->mir);
618 }
619 // Remove src_change from the vreg chain(s).
620 vreg_chains_.RemoveChange(src_change);
621 // Replace the move_change with the src_change, copying all necessary data.
622 src_data->is_move_src = move_data->is_move_src;
623 src_data->low_def_over_high_word = move_data->low_def_over_high_word;
624 src_data->high_def_over_low_word = move_data->high_def_over_low_word;
625 src_data->vreg_def = move_data->vreg_def;
626 src_data->prev_value = move_data->prev_value;
627 src_data->prev_value_high = move_data->prev_value_high;
628 src_data->mir->dalvikInsn.vA = move_data->vreg_def;
629 src_data->mir->ssa_rep->defs[0] = new_s_reg;
630 if (move_data->wide_def) {
631 DCHECK_EQ(src_data->mir->ssa_rep->defs[1], old_s_reg + 1);
632 src_data->mir->ssa_rep->defs[1] = new_s_reg + 1;
633 }
634 vreg_chains_.ReplaceChange(move_change, src_change);
635
636 // Rename uses and kill the move.
637 vreg_chains_.RenameVRegUses(src_change + 1u, vreg_chains_.NumMIRs(),
638 old_s_reg, mir_graph_->SRegToVReg(old_s_reg),
639 new_s_reg, mir_graph_->SRegToVReg(new_s_reg));
640 KillMIR(move_data);
641}
642
643void GvnDeadCodeElimination::RecordPassTryToKillOverwrittenMoveOrMoveSrc(uint16_t check_change) {
644 MIRData* data = vreg_chains_.GetMIRData(check_change);
645 DCHECK(data->is_move || data->is_move_src);
646 int32_t dest_s_reg = data->mir->ssa_rep->defs[0];
647
648 if (data->is_move) {
649 // Check if source vreg has changed since the MOVE.
650 int32_t src_s_reg = data->mir->ssa_rep->uses[0];
651 uint32_t src_v_reg = mir_graph_->SRegToVReg(src_s_reg);
652 uint16_t src_change = vreg_chains_.FindFirstChangeAfter(src_v_reg, check_change);
653 bool wide = data->wide_def;
654 if (wide) {
655 uint16_t src_change_high = vreg_chains_.FindFirstChangeAfter(src_v_reg + 1, check_change);
656 if (src_change_high != kNPos && (src_change == kNPos || src_change_high < src_change)) {
657 src_change = src_change_high;
658 }
659 }
660 if (src_change == kNPos ||
661 !vreg_chains_.IsSRegUsed(src_change + 1u, vreg_chains_.NumMIRs(), dest_s_reg)) {
662 // We can simply change all uses of dest to src.
663 size_t rename_end = (src_change != kNPos) ? src_change + 1u : vreg_chains_.NumMIRs();
664 vreg_chains_.RenameVRegUses(check_change + 1u, rename_end,
665 dest_s_reg, mir_graph_->SRegToVReg(dest_s_reg),
666 src_s_reg, mir_graph_->SRegToVReg(src_s_reg));
667
668 // Now, remove the MOVE from the vreg chain(s) and kill it.
669 vreg_chains_.RemoveChange(check_change);
670 KillMIR(data);
671 return;
672 }
673 }
674
675 if (data->is_move_src) {
676 // Try to find a MOVE to a vreg that wasn't changed since check_change.
677 uint16_t value_name =
678 data->wide_def ? lvn_->GetSregValueWide(dest_s_reg) : lvn_->GetSregValue(dest_s_reg);
679 for (size_t c = check_change + 1u, size = vreg_chains_.NumMIRs(); c != size; ++c) {
680 MIRData* d = vreg_chains_.GetMIRData(c);
681 if (d->is_move && d->wide_def == data->wide_def &&
682 (d->prev_value.change == kNPos || d->prev_value.change <= check_change) &&
683 (!d->wide_def ||
684 d->prev_value_high.change == kNPos || d->prev_value_high.change <= check_change)) {
685 // Compare value names to find move to move.
686 int32_t src_s_reg = d->mir->ssa_rep->uses[0];
687 uint16_t src_name =
688 (d->wide_def ? lvn_->GetSregValueWide(src_s_reg) : lvn_->GetSregValue(src_s_reg));
689 if (value_name == src_name) {
Vladimir Markoad677272015-04-20 10:48:13 +0100690 // Check if the move's destination vreg is unused between check_change and the move.
691 uint32_t new_dest_v_reg = mir_graph_->SRegToVReg(d->mir->ssa_rep->defs[0]);
692 if (!vreg_chains_.IsVRegUsed(check_change + 1u, c, new_dest_v_reg, mir_graph_) &&
693 (!d->wide_def ||
694 !vreg_chains_.IsVRegUsed(check_change + 1u, c, new_dest_v_reg + 1, mir_graph_))) {
695 RecordPassKillMoveByRenamingSrcDef(check_change, c);
696 return;
697 }
Vladimir Marko7a01dc22015-01-02 17:00:44 +0000698 }
699 }
700 }
701 }
702}
703
704void GvnDeadCodeElimination::RecordPassTryToKillOverwrittenMoveOrMoveSrc() {
705 // Check if we're overwriting a the result of a move or the definition of a source of a move.
706 // For MOVE_WIDE, we may be overwriting partially; if that's the case, check that the other
707 // word wasn't previously overwritten - we would have tried to rename back then.
708 MIRData* data = vreg_chains_.LastMIRData();
709 if (!data->has_def) {
710 return;
711 }
712 // NOTE: Instructions such as new-array implicitly use all vregs (if they throw) but they can
713 // define a move source which can be renamed. Therefore we allow the checked change to be the
714 // change before no_uses_all_since_. This has no effect on moves as they never use all vregs.
715 if (data->prev_value.change != kNPos && data->prev_value.change + 1u >= no_uses_all_since_) {
716 MIRData* check_data = vreg_chains_.GetMIRData(data->prev_value.change);
717 bool try_to_kill = false;
718 if (!check_data->is_move && !check_data->is_move_src) {
719 DCHECK(!try_to_kill);
720 } else if (!check_data->wide_def) {
721 // Narrow move; always fully overwritten by the last MIR.
722 try_to_kill = true;
723 } else if (data->low_def_over_high_word) {
724 // Overwriting only the high word; is the low word still valid?
725 DCHECK_EQ(check_data->vreg_def + 1u, data->vreg_def);
726 if (vreg_chains_.LastChange(check_data->vreg_def) == data->prev_value.change) {
727 try_to_kill = true;
728 }
729 } else if (!data->wide_def) {
730 // Overwriting only the low word, is the high word still valid?
731 if (vreg_chains_.LastChange(data->vreg_def + 1) == data->prev_value.change) {
732 try_to_kill = true;
733 }
734 } else {
735 // Overwriting both words; was the high word still from the same move?
736 if (data->prev_value_high.change == data->prev_value.change) {
737 try_to_kill = true;
738 }
739 }
740 if (try_to_kill) {
741 RecordPassTryToKillOverwrittenMoveOrMoveSrc(data->prev_value.change);
742 }
743 }
744 if (data->wide_def && data->high_def_over_low_word &&
745 data->prev_value_high.change != kNPos &&
746 data->prev_value_high.change + 1u >= no_uses_all_since_) {
747 MIRData* check_data = vreg_chains_.GetMIRData(data->prev_value_high.change);
748 bool try_to_kill = false;
749 if (!check_data->is_move && !check_data->is_move_src) {
750 DCHECK(!try_to_kill);
751 } else if (!check_data->wide_def) {
752 // Narrow move; always fully overwritten by the last MIR.
753 try_to_kill = true;
754 } else if (vreg_chains_.LastChange(check_data->vreg_def + 1) ==
755 data->prev_value_high.change) {
756 // High word is still valid.
757 try_to_kill = true;
758 }
759 if (try_to_kill) {
760 RecordPassTryToKillOverwrittenMoveOrMoveSrc(data->prev_value_high.change);
761 }
762 }
763}
764
765void GvnDeadCodeElimination::RecordPassTryToKillLastMIR() {
766 MIRData* last_data = vreg_chains_.LastMIRData();
767 if (last_data->must_keep) {
768 return;
769 }
770 if (UNLIKELY(!last_data->has_def)) {
771 // Must be an eliminated MOVE. Drop its data and data of all eliminated MIRs before it.
772 vreg_chains_.RemoveTrailingNops();
773 return;
774 }
775
776 // Try to kill a sequence of consecutive definitions of the same vreg. Allow mixing
777 // wide and non-wide defs; consider high word dead if low word has been overwritten.
778 uint16_t current_value = vreg_chains_.CurrentValue(last_data->vreg_def);
779 uint16_t change = vreg_chains_.NumMIRs() - 1u;
780 MIRData* data = last_data;
781 while (data->prev_value.value != current_value) {
782 --change;
783 if (data->prev_value.change == kNPos || data->prev_value.change != change) {
784 return;
785 }
786 data = vreg_chains_.GetMIRData(data->prev_value.change);
787 if (data->must_keep || !data->has_def || data->vreg_def != last_data->vreg_def) {
788 return;
789 }
790 }
791
792 bool wide = last_data->wide_def;
793 if (wide) {
794 // Check that the low word is valid.
795 if (data->low_def_over_high_word) {
796 return;
797 }
798 // Check that the high word is valid.
799 MIRData* high_data = data;
800 if (!high_data->wide_def) {
801 uint16_t high_change = vreg_chains_.FindFirstChangeAfter(data->vreg_def + 1, change);
802 DCHECK_NE(high_change, kNPos);
803 high_data = vreg_chains_.GetMIRData(high_change);
804 DCHECK_EQ(high_data->vreg_def, data->vreg_def);
805 }
806 if (high_data->prev_value_high.value != current_value || high_data->high_def_over_low_word) {
807 return;
808 }
809 }
810
811 MIR* phi = RenameSRegDefOrCreatePhi(data->prev_value.change, change, last_data->mir);
812 for (size_t i = 0, count = vreg_chains_.NumMIRs() - change; i != count; ++i) {
813 KillMIR(vreg_chains_.LastMIRData()->mir);
814 vreg_chains_.RemoveLastMIRData();
815 }
816 if (phi != nullptr) {
817 // Though the Phi has been added to the beginning, we can put the MIRData at the end.
818 vreg_chains_.AddMIRWithDef(phi, phi->dalvikInsn.vA, wide, current_value);
819 // Reset the previous value to avoid eventually eliminating the Phi itself (unless unused).
820 last_data = vreg_chains_.LastMIRData();
821 last_data->prev_value.value = kNoValue;
822 last_data->prev_value_high.value = kNoValue;
823 }
824}
825
826uint16_t GvnDeadCodeElimination::FindChangesToKill(uint16_t first_change, uint16_t last_change) {
827 // Process dependencies for changes in range [first_change, last_change) and record all
828 // changes that we need to kill. Return kNPos if there's a dependent change that must be
829 // kept unconditionally; otherwise the end of the range processed before encountering
830 // a change that defines a dalvik reg that we need to keep (last_change on full success).
831 changes_to_kill_.clear();
832 dependent_vregs_->ClearAllBits();
833 for (size_t change = first_change; change != last_change; ++change) {
834 MIRData* data = vreg_chains_.GetMIRData(change);
835 DCHECK(!data->uses_all_vregs);
836 bool must_not_depend = data->must_keep;
837 bool depends = false;
838 // Check if the MIR defines a vreg we're trying to eliminate.
839 if (data->has_def && vregs_to_kill_->IsBitSet(data->vreg_def)) {
840 if (change < kill_heads_[data->vreg_def]) {
841 must_not_depend = true;
842 } else {
843 depends = true;
844 }
845 }
846 if (data->has_def && data->wide_def && vregs_to_kill_->IsBitSet(data->vreg_def + 1)) {
847 if (change < kill_heads_[data->vreg_def + 1]) {
848 must_not_depend = true;
849 } else {
850 depends = true;
851 }
852 }
853 if (!depends) {
854 // Check for dependency through SSA reg uses.
855 SSARepresentation* ssa_rep = data->mir->ssa_rep;
856 for (int i = 0; i != ssa_rep->num_uses; ++i) {
857 if (dependent_vregs_->IsBitSet(mir_graph_->SRegToVReg(ssa_rep->uses[i]))) {
858 depends = true;
859 break;
860 }
861 }
862 }
863 // Now check if we can eliminate the insn if we need to.
864 if (depends && must_not_depend) {
865 return kNPos;
866 }
867 if (depends && data->has_def &&
868 vreg_chains_.IsTopChange(change) && !vregs_to_kill_->IsBitSet(data->vreg_def) &&
869 !unused_vregs_->IsBitSet(data->vreg_def) &&
870 (!data->wide_def || !unused_vregs_->IsBitSet(data->vreg_def + 1))) {
871 // This is a top change but neither unnecessary nor one of the top kill changes.
872 return change;
873 }
874 // Finally, update the data.
875 if (depends) {
876 changes_to_kill_.push_back(change);
877 if (data->has_def) {
878 dependent_vregs_->SetBit(data->vreg_def);
879 if (data->wide_def) {
880 dependent_vregs_->SetBit(data->vreg_def + 1);
881 }
882 }
883 } else {
884 if (data->has_def) {
885 dependent_vregs_->ClearBit(data->vreg_def);
886 if (data->wide_def) {
887 dependent_vregs_->ClearBit(data->vreg_def + 1);
888 }
889 }
890 }
891 }
892 return last_change;
893}
894
895void GvnDeadCodeElimination::BackwardPassTryToKillRevertVRegs() {
896}
897
898bool GvnDeadCodeElimination::BackwardPassTryToKillLastMIR() {
899 MIRData* last_data = vreg_chains_.LastMIRData();
900 if (last_data->must_keep) {
901 return false;
902 }
903 DCHECK(!last_data->uses_all_vregs);
904 if (!last_data->has_def) {
905 // Previously eliminated.
906 DCHECK_EQ(static_cast<int>(last_data->mir->dalvikInsn.opcode), static_cast<int>(kMirOpNop));
907 vreg_chains_.RemoveTrailingNops();
908 return true;
909 }
910 if (unused_vregs_->IsBitSet(last_data->vreg_def) ||
911 (last_data->wide_def && unused_vregs_->IsBitSet(last_data->vreg_def + 1))) {
912 if (last_data->wide_def) {
913 // For wide defs, one of the vregs may still be considered needed, fix that.
914 unused_vregs_->SetBit(last_data->vreg_def);
915 unused_vregs_->SetBit(last_data->vreg_def + 1);
916 }
917 KillMIR(last_data->mir);
918 vreg_chains_.RemoveLastMIRData();
919 return true;
920 }
921
922 vregs_to_kill_->ClearAllBits();
923 size_t num_mirs = vreg_chains_.NumMIRs();
924 DCHECK_NE(num_mirs, 0u);
925 uint16_t kill_change = num_mirs - 1u;
926 uint16_t start = num_mirs;
927 size_t num_killed_top_changes = 0u;
928 while (num_killed_top_changes != kMaxNumTopChangesToKill &&
929 kill_change != kNPos && kill_change != num_mirs) {
930 ++num_killed_top_changes;
931
932 DCHECK(vreg_chains_.IsTopChange(kill_change));
933 MIRData* data = vreg_chains_.GetMIRData(kill_change);
934 int count = data->wide_def ? 2 : 1;
935 for (int v_reg = data->vreg_def, end = data->vreg_def + count; v_reg != end; ++v_reg) {
936 uint16_t kill_head = vreg_chains_.FindKillHead(v_reg, no_uses_all_since_);
937 if (kill_head == kNPos) {
938 return false;
939 }
940 kill_heads_[v_reg] = kill_head;
941 vregs_to_kill_->SetBit(v_reg);
942 start = std::min(start, kill_head);
943 }
944 DCHECK_LT(start, vreg_chains_.NumMIRs());
945
946 kill_change = FindChangesToKill(start, num_mirs);
947 }
948
949 if (kill_change != num_mirs) {
950 return false;
951 }
952
953 // Kill all MIRs marked as dependent.
954 for (uint32_t v_reg : vregs_to_kill_->Indexes()) {
955 // Rename s_regs or create Phi only once for each MIR (only for low word).
956 MIRData* data = vreg_chains_.GetMIRData(vreg_chains_.LastChange(v_reg));
957 DCHECK(data->has_def);
958 if (data->vreg_def == v_reg) {
959 MIRData* kill_head_data = vreg_chains_.GetMIRData(kill_heads_[v_reg]);
960 RenameSRegDefOrCreatePhi(kill_head_data->PrevChange(v_reg), num_mirs, data->mir);
961 } else {
962 DCHECK_EQ(data->vreg_def + 1u, v_reg);
963 DCHECK_EQ(vreg_chains_.GetMIRData(kill_heads_[v_reg - 1u])->PrevChange(v_reg - 1u),
964 vreg_chains_.GetMIRData(kill_heads_[v_reg])->PrevChange(v_reg));
965 }
966 }
967 unused_vregs_->Union(vregs_to_kill_);
968 for (auto it = changes_to_kill_.rbegin(), end = changes_to_kill_.rend(); it != end; ++it) {
969 MIRData* data = vreg_chains_.GetMIRData(*it);
970 DCHECK(!data->must_keep);
971 DCHECK(data->has_def);
972 vreg_chains_.RemoveChange(*it);
973 KillMIR(data);
974 }
975
976 vreg_chains_.RemoveTrailingNops();
977 return true;
978}
979
980bool GvnDeadCodeElimination::RecordMIR(MIR* mir) {
981 bool must_keep = false;
982 bool uses_all_vregs = false;
983 bool is_move = false;
984 uint16_t opcode = mir->dalvikInsn.opcode;
985 switch (opcode) {
986 case kMirOpPhi: {
Vladimir Markoa5e69e82015-04-24 19:03:51 +0100987 // Determine if this Phi is merging wide regs.
988 RegLocation raw_dest = gvn_->GetMirGraph()->GetRawDest(mir);
989 if (raw_dest.high_word) {
990 // This is the high part of a wide reg. Ignore the Phi.
991 return false;
992 }
993 bool wide = raw_dest.wide;
994 // Record the value.
Vladimir Marko7a01dc22015-01-02 17:00:44 +0000995 DCHECK_EQ(mir->ssa_rep->num_defs, 1);
996 int s_reg = mir->ssa_rep->defs[0];
Vladimir Markoa5e69e82015-04-24 19:03:51 +0100997 uint16_t new_value = wide ? lvn_->GetSregValueWide(s_reg) : lvn_->GetSregValue(s_reg);
Vladimir Marko7a01dc22015-01-02 17:00:44 +0000998
999 int v_reg = mir_graph_->SRegToVReg(s_reg);
1000 DCHECK_EQ(vreg_chains_.CurrentValue(v_reg), kNoValue); // No previous def for v_reg.
1001 if (wide) {
1002 DCHECK_EQ(vreg_chains_.CurrentValue(v_reg + 1), kNoValue);
1003 }
1004 vreg_chains_.AddMIRWithDef(mir, v_reg, wide, new_value);
1005 return true; // Avoid the common processing.
1006 }
1007
1008 case kMirOpNop:
1009 case Instruction::NOP:
1010 // Don't record NOPs.
1011 return false;
1012
1013 case kMirOpCheck:
1014 must_keep = true;
1015 uses_all_vregs = true;
1016 break;
1017
1018 case Instruction::RETURN_VOID:
1019 case Instruction::RETURN:
1020 case Instruction::RETURN_OBJECT:
1021 case Instruction::RETURN_WIDE:
1022 case Instruction::GOTO:
1023 case Instruction::GOTO_16:
1024 case Instruction::GOTO_32:
1025 case Instruction::PACKED_SWITCH:
1026 case Instruction::SPARSE_SWITCH:
1027 case Instruction::IF_EQ:
1028 case Instruction::IF_NE:
1029 case Instruction::IF_LT:
1030 case Instruction::IF_GE:
1031 case Instruction::IF_GT:
1032 case Instruction::IF_LE:
1033 case Instruction::IF_EQZ:
1034 case Instruction::IF_NEZ:
1035 case Instruction::IF_LTZ:
1036 case Instruction::IF_GEZ:
1037 case Instruction::IF_GTZ:
1038 case Instruction::IF_LEZ:
1039 case kMirOpFusedCmplFloat:
1040 case kMirOpFusedCmpgFloat:
1041 case kMirOpFusedCmplDouble:
1042 case kMirOpFusedCmpgDouble:
1043 case kMirOpFusedCmpLong:
1044 must_keep = true;
1045 uses_all_vregs = true; // Keep the implicit dependencies on all vregs.
1046 break;
1047
1048 case Instruction::CONST_CLASS:
1049 case Instruction::CONST_STRING:
1050 case Instruction::CONST_STRING_JUMBO:
1051 // NOTE: While we're currently treating CONST_CLASS, CONST_STRING and CONST_STRING_JUMBO
1052 // as throwing but we could conceivably try and eliminate those exceptions if we're
1053 // retrieving the class/string repeatedly.
1054 must_keep = true;
1055 uses_all_vregs = true;
1056 break;
1057
1058 case Instruction::MONITOR_ENTER:
1059 case Instruction::MONITOR_EXIT:
1060 // We can actually try and optimize across the acquire operation of MONITOR_ENTER,
1061 // the value names provided by GVN reflect the possible changes to memory visibility.
1062 // NOTE: In ART, MONITOR_ENTER and MONITOR_EXIT can throw only NPE.
1063 must_keep = true;
1064 uses_all_vregs = (mir->optimization_flags & MIR_IGNORE_NULL_CHECK) == 0;
1065 break;
1066
1067 case Instruction::INVOKE_DIRECT:
1068 case Instruction::INVOKE_DIRECT_RANGE:
1069 case Instruction::INVOKE_VIRTUAL:
1070 case Instruction::INVOKE_VIRTUAL_RANGE:
1071 case Instruction::INVOKE_SUPER:
1072 case Instruction::INVOKE_SUPER_RANGE:
1073 case Instruction::INVOKE_INTERFACE:
1074 case Instruction::INVOKE_INTERFACE_RANGE:
1075 case Instruction::INVOKE_STATIC:
1076 case Instruction::INVOKE_STATIC_RANGE:
Vladimir Marko7a01dc22015-01-02 17:00:44 +00001077 case Instruction::THROW:
1078 case Instruction::FILLED_NEW_ARRAY:
1079 case Instruction::FILLED_NEW_ARRAY_RANGE:
1080 case Instruction::FILL_ARRAY_DATA:
1081 must_keep = true;
1082 uses_all_vregs = true;
1083 break;
1084
1085 case Instruction::NEW_INSTANCE:
1086 case Instruction::NEW_ARRAY:
1087 must_keep = true;
1088 uses_all_vregs = true;
1089 break;
1090
Vladimir Marko22fe45d2015-03-18 11:33:58 +00001091 case Instruction::CHECK_CAST:
1092 DCHECK_EQ(mir->ssa_rep->num_uses, 1);
1093 must_keep = true; // Keep for type information even if MIR_IGNORE_CHECK_CAST.
1094 uses_all_vregs = (mir->optimization_flags & MIR_IGNORE_CHECK_CAST) == 0;
1095 break;
1096
Vladimir Marko7a01dc22015-01-02 17:00:44 +00001097 case kMirOpNullCheck:
1098 DCHECK_EQ(mir->ssa_rep->num_uses, 1);
1099 if ((mir->optimization_flags & MIR_IGNORE_NULL_CHECK) != 0) {
1100 mir->ssa_rep->num_uses = 0;
1101 mir->dalvikInsn.opcode = static_cast<Instruction::Code>(kMirOpNop);
1102 return false;
1103 }
1104 must_keep = true;
1105 uses_all_vregs = true;
1106 break;
1107
1108 case Instruction::MOVE_RESULT:
1109 case Instruction::MOVE_RESULT_OBJECT:
1110 case Instruction::MOVE_RESULT_WIDE:
1111 break;
1112
1113 case Instruction::INSTANCE_OF:
1114 break;
1115
1116 case Instruction::MOVE_EXCEPTION:
1117 must_keep = true;
1118 break;
1119
1120 case kMirOpCopy:
1121 case Instruction::MOVE:
1122 case Instruction::MOVE_FROM16:
1123 case Instruction::MOVE_16:
1124 case Instruction::MOVE_WIDE:
1125 case Instruction::MOVE_WIDE_FROM16:
1126 case Instruction::MOVE_WIDE_16:
1127 case Instruction::MOVE_OBJECT:
1128 case Instruction::MOVE_OBJECT_FROM16:
1129 case Instruction::MOVE_OBJECT_16: {
1130 is_move = true;
1131 // If the MIR defining src vreg is known, allow renaming all uses of src vreg to dest vreg
1132 // while updating the defining MIR to directly define dest vreg. However, changing Phi's
1133 // def this way doesn't work without changing MIRs in other BBs.
1134 int src_v_reg = mir_graph_->SRegToVReg(mir->ssa_rep->uses[0]);
1135 int src_change = vreg_chains_.LastChange(src_v_reg);
1136 if (src_change != kNPos) {
1137 MIRData* src_data = vreg_chains_.GetMIRData(src_change);
1138 if (static_cast<int>(src_data->mir->dalvikInsn.opcode) != kMirOpPhi) {
1139 src_data->is_move_src = true;
1140 }
1141 }
1142 break;
1143 }
1144
1145 case Instruction::CONST_4:
1146 case Instruction::CONST_16:
1147 case Instruction::CONST:
1148 case Instruction::CONST_HIGH16:
1149 case Instruction::CONST_WIDE_16:
1150 case Instruction::CONST_WIDE_32:
1151 case Instruction::CONST_WIDE:
1152 case Instruction::CONST_WIDE_HIGH16:
1153 case Instruction::ARRAY_LENGTH:
1154 case Instruction::CMPL_FLOAT:
1155 case Instruction::CMPG_FLOAT:
1156 case Instruction::CMPL_DOUBLE:
1157 case Instruction::CMPG_DOUBLE:
1158 case Instruction::CMP_LONG:
1159 case Instruction::NEG_INT:
1160 case Instruction::NOT_INT:
1161 case Instruction::NEG_LONG:
1162 case Instruction::NOT_LONG:
1163 case Instruction::NEG_FLOAT:
1164 case Instruction::NEG_DOUBLE:
1165 case Instruction::INT_TO_LONG:
1166 case Instruction::INT_TO_FLOAT:
1167 case Instruction::INT_TO_DOUBLE:
1168 case Instruction::LONG_TO_INT:
1169 case Instruction::LONG_TO_FLOAT:
1170 case Instruction::LONG_TO_DOUBLE:
1171 case Instruction::FLOAT_TO_INT:
1172 case Instruction::FLOAT_TO_LONG:
1173 case Instruction::FLOAT_TO_DOUBLE:
1174 case Instruction::DOUBLE_TO_INT:
1175 case Instruction::DOUBLE_TO_LONG:
1176 case Instruction::DOUBLE_TO_FLOAT:
1177 case Instruction::INT_TO_BYTE:
1178 case Instruction::INT_TO_CHAR:
1179 case Instruction::INT_TO_SHORT:
1180 case Instruction::ADD_INT:
1181 case Instruction::SUB_INT:
1182 case Instruction::MUL_INT:
1183 case Instruction::AND_INT:
1184 case Instruction::OR_INT:
1185 case Instruction::XOR_INT:
1186 case Instruction::SHL_INT:
1187 case Instruction::SHR_INT:
1188 case Instruction::USHR_INT:
1189 case Instruction::ADD_LONG:
1190 case Instruction::SUB_LONG:
1191 case Instruction::MUL_LONG:
1192 case Instruction::AND_LONG:
1193 case Instruction::OR_LONG:
1194 case Instruction::XOR_LONG:
1195 case Instruction::SHL_LONG:
1196 case Instruction::SHR_LONG:
1197 case Instruction::USHR_LONG:
1198 case Instruction::ADD_FLOAT:
1199 case Instruction::SUB_FLOAT:
1200 case Instruction::MUL_FLOAT:
1201 case Instruction::DIV_FLOAT:
1202 case Instruction::REM_FLOAT:
1203 case Instruction::ADD_DOUBLE:
1204 case Instruction::SUB_DOUBLE:
1205 case Instruction::MUL_DOUBLE:
1206 case Instruction::DIV_DOUBLE:
1207 case Instruction::REM_DOUBLE:
1208 case Instruction::ADD_INT_2ADDR:
1209 case Instruction::SUB_INT_2ADDR:
1210 case Instruction::MUL_INT_2ADDR:
1211 case Instruction::AND_INT_2ADDR:
1212 case Instruction::OR_INT_2ADDR:
1213 case Instruction::XOR_INT_2ADDR:
1214 case Instruction::SHL_INT_2ADDR:
1215 case Instruction::SHR_INT_2ADDR:
1216 case Instruction::USHR_INT_2ADDR:
1217 case Instruction::ADD_LONG_2ADDR:
1218 case Instruction::SUB_LONG_2ADDR:
1219 case Instruction::MUL_LONG_2ADDR:
1220 case Instruction::AND_LONG_2ADDR:
1221 case Instruction::OR_LONG_2ADDR:
1222 case Instruction::XOR_LONG_2ADDR:
1223 case Instruction::SHL_LONG_2ADDR:
1224 case Instruction::SHR_LONG_2ADDR:
1225 case Instruction::USHR_LONG_2ADDR:
1226 case Instruction::ADD_FLOAT_2ADDR:
1227 case Instruction::SUB_FLOAT_2ADDR:
1228 case Instruction::MUL_FLOAT_2ADDR:
1229 case Instruction::DIV_FLOAT_2ADDR:
1230 case Instruction::REM_FLOAT_2ADDR:
1231 case Instruction::ADD_DOUBLE_2ADDR:
1232 case Instruction::SUB_DOUBLE_2ADDR:
1233 case Instruction::MUL_DOUBLE_2ADDR:
1234 case Instruction::DIV_DOUBLE_2ADDR:
1235 case Instruction::REM_DOUBLE_2ADDR:
1236 case Instruction::ADD_INT_LIT16:
1237 case Instruction::RSUB_INT:
1238 case Instruction::MUL_INT_LIT16:
1239 case Instruction::AND_INT_LIT16:
1240 case Instruction::OR_INT_LIT16:
1241 case Instruction::XOR_INT_LIT16:
1242 case Instruction::ADD_INT_LIT8:
1243 case Instruction::RSUB_INT_LIT8:
1244 case Instruction::MUL_INT_LIT8:
1245 case Instruction::AND_INT_LIT8:
1246 case Instruction::OR_INT_LIT8:
1247 case Instruction::XOR_INT_LIT8:
1248 case Instruction::SHL_INT_LIT8:
1249 case Instruction::SHR_INT_LIT8:
1250 case Instruction::USHR_INT_LIT8:
1251 break;
1252
1253 case Instruction::DIV_INT:
1254 case Instruction::REM_INT:
1255 case Instruction::DIV_LONG:
1256 case Instruction::REM_LONG:
1257 case Instruction::DIV_INT_2ADDR:
1258 case Instruction::REM_INT_2ADDR:
1259 case Instruction::DIV_LONG_2ADDR:
1260 case Instruction::REM_LONG_2ADDR:
1261 if ((mir->optimization_flags & MIR_IGNORE_DIV_ZERO_CHECK) == 0) {
1262 must_keep = true;
1263 uses_all_vregs = true;
1264 }
1265 break;
1266
1267 case Instruction::DIV_INT_LIT16:
1268 case Instruction::REM_INT_LIT16:
1269 case Instruction::DIV_INT_LIT8:
1270 case Instruction::REM_INT_LIT8:
1271 if (mir->dalvikInsn.vC == 0) { // Explicit division by 0?
1272 must_keep = true;
1273 uses_all_vregs = true;
1274 }
1275 break;
1276
1277 case Instruction::AGET_OBJECT:
1278 case Instruction::AGET:
1279 case Instruction::AGET_WIDE:
1280 case Instruction::AGET_BOOLEAN:
1281 case Instruction::AGET_BYTE:
1282 case Instruction::AGET_CHAR:
1283 case Instruction::AGET_SHORT:
1284 if ((mir->optimization_flags & MIR_IGNORE_NULL_CHECK) == 0 ||
1285 (mir->optimization_flags & MIR_IGNORE_RANGE_CHECK) == 0) {
1286 must_keep = true;
1287 uses_all_vregs = true;
1288 }
1289 break;
1290
1291 case Instruction::APUT_OBJECT:
1292 case Instruction::APUT:
1293 case Instruction::APUT_WIDE:
1294 case Instruction::APUT_BYTE:
1295 case Instruction::APUT_BOOLEAN:
1296 case Instruction::APUT_SHORT:
1297 case Instruction::APUT_CHAR:
1298 must_keep = true;
1299 if ((mir->optimization_flags & MIR_IGNORE_NULL_CHECK) == 0 ||
1300 (mir->optimization_flags & MIR_IGNORE_RANGE_CHECK) == 0) {
1301 uses_all_vregs = true;
1302 }
1303 break;
1304
1305 case Instruction::IGET_OBJECT:
1306 case Instruction::IGET:
1307 case Instruction::IGET_WIDE:
1308 case Instruction::IGET_BOOLEAN:
1309 case Instruction::IGET_BYTE:
1310 case Instruction::IGET_CHAR:
1311 case Instruction::IGET_SHORT: {
1312 const MirIFieldLoweringInfo& info = mir_graph_->GetIFieldLoweringInfo(mir);
1313 if ((mir->optimization_flags & MIR_IGNORE_NULL_CHECK) == 0 ||
1314 !info.IsResolved() || !info.FastGet()) {
1315 must_keep = true;
1316 uses_all_vregs = true;
1317 } else if (info.IsVolatile()) {
1318 must_keep = true;
1319 }
1320 break;
1321 }
1322
1323 case Instruction::IPUT_OBJECT:
1324 case Instruction::IPUT:
1325 case Instruction::IPUT_WIDE:
1326 case Instruction::IPUT_BOOLEAN:
1327 case Instruction::IPUT_BYTE:
1328 case Instruction::IPUT_CHAR:
1329 case Instruction::IPUT_SHORT: {
1330 must_keep = true;
1331 const MirIFieldLoweringInfo& info = mir_graph_->GetIFieldLoweringInfo(mir);
1332 if ((mir->optimization_flags & MIR_IGNORE_NULL_CHECK) == 0 ||
1333 !info.IsResolved() || !info.FastPut()) {
1334 uses_all_vregs = true;
1335 }
1336 break;
1337 }
1338
1339 case Instruction::SGET_OBJECT:
1340 case Instruction::SGET:
1341 case Instruction::SGET_WIDE:
1342 case Instruction::SGET_BOOLEAN:
1343 case Instruction::SGET_BYTE:
1344 case Instruction::SGET_CHAR:
1345 case Instruction::SGET_SHORT: {
1346 const MirSFieldLoweringInfo& info = mir_graph_->GetSFieldLoweringInfo(mir);
1347 if ((mir->optimization_flags & MIR_CLASS_IS_INITIALIZED) == 0 ||
1348 !info.IsResolved() || !info.FastGet()) {
1349 must_keep = true;
1350 uses_all_vregs = true;
1351 } else if (info.IsVolatile()) {
1352 must_keep = true;
1353 }
1354 break;
1355 }
1356
1357 case Instruction::SPUT_OBJECT:
1358 case Instruction::SPUT:
1359 case Instruction::SPUT_WIDE:
1360 case Instruction::SPUT_BOOLEAN:
1361 case Instruction::SPUT_BYTE:
1362 case Instruction::SPUT_CHAR:
1363 case Instruction::SPUT_SHORT: {
1364 must_keep = true;
1365 const MirSFieldLoweringInfo& info = mir_graph_->GetSFieldLoweringInfo(mir);
1366 if ((mir->optimization_flags & MIR_CLASS_IS_INITIALIZED) == 0 ||
1367 !info.IsResolved() || !info.FastPut()) {
1368 uses_all_vregs = true;
1369 }
1370 break;
1371 }
1372
1373 default:
1374 LOG(FATAL) << "Unexpected opcode: " << opcode;
1375 UNREACHABLE();
Vladimir Marko7a01dc22015-01-02 17:00:44 +00001376 }
1377
1378 if (mir->ssa_rep->num_defs != 0) {
1379 DCHECK(mir->ssa_rep->num_defs == 1 || mir->ssa_rep->num_defs == 2);
1380 bool wide = (mir->ssa_rep->num_defs == 2);
1381 int s_reg = mir->ssa_rep->defs[0];
1382 int v_reg = mir_graph_->SRegToVReg(s_reg);
1383 uint16_t new_value = wide ? lvn_->GetSregValueWide(s_reg) : lvn_->GetSregValue(s_reg);
1384 DCHECK_NE(new_value, kNoValue);
1385
1386 vreg_chains_.UpdateInitialVRegValue(v_reg, wide, lvn_);
1387 vreg_chains_.AddMIRWithDef(mir, v_reg, wide, new_value);
1388 if (is_move) {
1389 // Allow renaming all uses of dest vreg to src vreg.
1390 vreg_chains_.LastMIRData()->is_move = true;
1391 }
1392 } else {
1393 vreg_chains_.AddMIRWithoutDef(mir);
1394 DCHECK(!is_move) << opcode;
1395 }
1396
1397 if (must_keep) {
1398 MIRData* last_data = vreg_chains_.LastMIRData();
1399 last_data->must_keep = true;
1400 if (uses_all_vregs) {
1401 last_data->uses_all_vregs = true;
1402 no_uses_all_since_ = vreg_chains_.NumMIRs();
1403 }
1404 } else {
1405 DCHECK_NE(mir->ssa_rep->num_defs, 0) << opcode;
1406 DCHECK(!uses_all_vregs) << opcode;
1407 }
1408 return true;
1409}
1410
1411} // namespace art