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