Skip to content

gh-104635: Eliminate redundant STORE_FAST instructions in the compiler #105040

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 13 commits into from
Closed
Prev Previous commit
Move apply_static_swap to the last phase of bb optimization
  • Loading branch information
corona10 committed Jun 1, 2023
commit b0e0a0661677c31ecfa3be79cedd1d48e852876c
6 changes: 2 additions & 4 deletions Lib/test/test_peepholer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1074,9 +1074,7 @@ def test_no_unsafe_static_swap(self):
expected_insts = [
('LOAD_CONST', 0, 1),
('LOAD_CONST', 1, 2),
('LOAD_CONST', 2, 3),
('SWAP', 3, 4),
('POP_TOP', 0, 4),
('NOP', 0, 3),
('STORE_FAST', 1, 4),
('POP_TOP', 0, 4),
('RETURN_VALUE', 5)
Expand All @@ -1096,7 +1094,7 @@ def test_dead_store_elimination_in_same_lineno(self):
expected_insts = [
('LOAD_CONST', 0, 1),
('LOAD_CONST', 1, 2),
('NOP', None, 3),
('NOP', 0, 3),
('POP_TOP', 0, 4),
('STORE_FAST', 1, 4),
('RETURN_VALUE', 5)
Expand Down
14 changes: 9 additions & 5 deletions Python/flowgraph.c
Original file line number Diff line number Diff line change
Expand Up @@ -1526,12 +1526,7 @@ optimize_basic_block(PyObject *const_cache, basicblock *bb, PyObject *consts)
case SWAP:
if (oparg == 1) {
INSTR_SET_OP0(inst, NOP);
break;
}
if (swaptimize(bb, &i) < 0) {
goto error;
}
apply_static_swaps(bb, i);
break;
case KW_NAMES:
break;
Expand All @@ -1546,6 +1541,15 @@ optimize_basic_block(PyObject *const_cache, basicblock *bb, PyObject *consts)
assert (!HAS_CONST(inst->i_opcode));
}
}
for (int i = 0; i < bb->b_iused; i++) {
cfg_instr *inst = &bb->b_instr[i];
if (inst->i_opcode == SWAP) {
if (swaptimize(bb, &i) < 0) {
goto error;
}
apply_static_swaps(bb, i);
}
}
return SUCCESS;
error:
return ERROR;
Expand Down