From 8866ee6a63bfbc843881be84c4cb455482be516b Mon Sep 17 00:00:00 2001 From: Irit Katriel Date: Fri, 9 Sep 2022 16:50:30 +0100 Subject: [PATCH] rename extend_block --> inline_small_exit_blocks. Make it inline blocks with linenos too. --- Lib/test/test_peepholer.py | 2 -- Python/compile.c | 29 ++++++----------------------- 2 files changed, 6 insertions(+), 25 deletions(-) diff --git a/Lib/test/test_peepholer.py b/Lib/test/test_peepholer.py index 5f7e50d4e757af..ab45e3c52a039b 100644 --- a/Lib/test/test_peepholer.py +++ b/Lib/test/test_peepholer.py @@ -344,8 +344,6 @@ def f(x): self.assertEqual(len(returns), 1) self.check_lnotab(f) - @unittest.skip("Following gh-92228 the return has two predecessors " - "and that prevents jump elimination.") def test_elim_jump_to_return(self): # JUMP_FORWARD to RETURN --> RETURN def f(cond, true_value, false_value): diff --git a/Python/compile.c b/Python/compile.c index b153c0f2afcbd2..2602cb3d0df234 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -9006,10 +9006,7 @@ optimize_basic_block(PyObject *const_cache, basicblock *bb, PyObject *consts) int oparg = inst->i_oparg; int nextop = i+1 < bb->b_iused ? bb->b_instr[i+1].i_opcode : 0; if (HAS_TARGET(inst->i_opcode)) { - /* Skip over empty basic blocks. */ - while (inst->i_target->b_iused == 0) { - inst->i_target = inst->i_target->b_next; - } + assert(inst->i_target->b_iused > 0); target = &inst->i_target->b_instr[0]; assert(!IS_ASSEMBLER_OPCODE(target->i_opcode)); } @@ -9233,22 +9230,12 @@ optimize_basic_block(PyObject *const_cache, basicblock *bb, PyObject *consts) return -1; } -static bool -basicblock_has_lineno(const basicblock *bb) { - for (int i = 0; i < bb->b_iused; i++) { - if (bb->b_instr[i].i_loc.lineno > 0) { - return true; - } - } - return false; -} - -/* If this block ends with an unconditional jump to a small exit block that does - * not have linenos, then remove the jump and extend this block with the target. +/* If this block ends with an unconditional jump to a small exit block, then + * remove the jump and extend this block with the target. * Returns 1 if extended, 0 if no change, and -1 on error. */ static int -extend_block(basicblock *bb) { +inline_small_exit_blocks(basicblock *bb) { struct instr *last = basicblock_last_instr(bb); if (last == NULL) { return 0; @@ -9258,10 +9245,6 @@ extend_block(basicblock *bb) { } basicblock *target = last->i_target; if (basicblock_exits_scope(target) && target->b_iused <= MAX_COPY_SIZE) { - if (basicblock_has_lineno(target)) { - /* copy only blocks without line number (like implicit 'return None's) */ - return 0; - } last->i_opcode = NOP; for (int i = 0; i < target->b_iused; i++) { int index = basicblock_next_instr(bb); @@ -9513,7 +9496,7 @@ optimize_cfg(cfg_builder *g, PyObject *consts, PyObject *const_cache) } eliminate_empty_basic_blocks(g); for (basicblock *b = g->g_entryblock; b != NULL; b = b->b_next) { - if (extend_block(b) < 0) { + if (inline_small_exit_blocks(b) < 0) { return -1; } } @@ -9526,7 +9509,7 @@ optimize_cfg(cfg_builder *g, PyObject *consts, PyObject *const_cache) assert(b->b_predecessors == 0); } for (basicblock *b = g->g_entryblock; b != NULL; b = b->b_next) { - if (extend_block(b) < 0) { + if (inline_small_exit_blocks(b) < 0) { return -1; } }