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