Skip to content

Commit 8866ee6

Browse files
committed
rename extend_block --> inline_small_exit_blocks. Make it inline blocks with linenos too.
1 parent 42a7525 commit 8866ee6

File tree

2 files changed

+6
-25
lines changed

2 files changed

+6
-25
lines changed

Lib/test/test_peepholer.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -344,8 +344,6 @@ def f(x):
344344
self.assertEqual(len(returns), 1)
345345
self.check_lnotab(f)
346346

347-
@unittest.skip("Following gh-92228 the return has two predecessors "
348-
"and that prevents jump elimination.")
349347
def test_elim_jump_to_return(self):
350348
# JUMP_FORWARD to RETURN --> RETURN
351349
def f(cond, true_value, false_value):

Python/compile.c

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9006,10 +9006,7 @@ optimize_basic_block(PyObject *const_cache, basicblock *bb, PyObject *consts)
90069006
int oparg = inst->i_oparg;
90079007
int nextop = i+1 < bb->b_iused ? bb->b_instr[i+1].i_opcode : 0;
90089008
if (HAS_TARGET(inst->i_opcode)) {
9009-
/* Skip over empty basic blocks. */
9010-
while (inst->i_target->b_iused == 0) {
9011-
inst->i_target = inst->i_target->b_next;
9012-
}
9009+
assert(inst->i_target->b_iused > 0);
90139010
target = &inst->i_target->b_instr[0];
90149011
assert(!IS_ASSEMBLER_OPCODE(target->i_opcode));
90159012
}
@@ -9233,22 +9230,12 @@ optimize_basic_block(PyObject *const_cache, basicblock *bb, PyObject *consts)
92339230
return -1;
92349231
}
92359232

9236-
static bool
9237-
basicblock_has_lineno(const basicblock *bb) {
9238-
for (int i = 0; i < bb->b_iused; i++) {
9239-
if (bb->b_instr[i].i_loc.lineno > 0) {
9240-
return true;
9241-
}
9242-
}
9243-
return false;
9244-
}
9245-
9246-
/* If this block ends with an unconditional jump to a small exit block that does
9247-
* not have linenos, then remove the jump and extend this block with the target.
9233+
/* If this block ends with an unconditional jump to a small exit block, then
9234+
* remove the jump and extend this block with the target.
92489235
* Returns 1 if extended, 0 if no change, and -1 on error.
92499236
*/
92509237
static int
9251-
extend_block(basicblock *bb) {
9238+
inline_small_exit_blocks(basicblock *bb) {
92529239
struct instr *last = basicblock_last_instr(bb);
92539240
if (last == NULL) {
92549241
return 0;
@@ -9258,10 +9245,6 @@ extend_block(basicblock *bb) {
92589245
}
92599246
basicblock *target = last->i_target;
92609247
if (basicblock_exits_scope(target) && target->b_iused <= MAX_COPY_SIZE) {
9261-
if (basicblock_has_lineno(target)) {
9262-
/* copy only blocks without line number (like implicit 'return None's) */
9263-
return 0;
9264-
}
92659248
last->i_opcode = NOP;
92669249
for (int i = 0; i < target->b_iused; i++) {
92679250
int index = basicblock_next_instr(bb);
@@ -9513,7 +9496,7 @@ optimize_cfg(cfg_builder *g, PyObject *consts, PyObject *const_cache)
95139496
}
95149497
eliminate_empty_basic_blocks(g);
95159498
for (basicblock *b = g->g_entryblock; b != NULL; b = b->b_next) {
9516-
if (extend_block(b) < 0) {
9499+
if (inline_small_exit_blocks(b) < 0) {
95179500
return -1;
95189501
}
95199502
}
@@ -9526,7 +9509,7 @@ optimize_cfg(cfg_builder *g, PyObject *consts, PyObject *const_cache)
95269509
assert(b->b_predecessors == 0);
95279510
}
95289511
for (basicblock *b = g->g_entryblock; b != NULL; b = b->b_next) {
9529-
if (extend_block(b) < 0) {
9512+
if (inline_small_exit_blocks(b) < 0) {
95309513
return -1;
95319514
}
95329515
}

0 commit comments

Comments
 (0)