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