8000 gh-93678: apply remove_redundant_jumps in optimize_cfg (GH-96274) · python/cpython@894cafd · GitHub
[go: up one dir, main page]

Skip to content

Commit 894cafd

Browse files
authored
gh-93678: apply remove_redundant_jumps in optimize_cfg (GH-96274)
1 parent e93d1bd commit 894cafd

File tree

2 files changed

+36
-14
lines changed

2 files changed

+36
-14
lines changed

Lib/test/test_peepholer.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -892,7 +892,8 @@ def test_conditional_jump_forward_non_const_condition(self):
892892
self.cfg_optimization_test(insts, expected, consts=list(range(5)))
893893

894894
def test_conditional_jump_forward_const_condition(self):
895-
# The unreachable branch of the jump is removed
895+
# The unreachable branch of the jump is removed, the jump
896+
# becomes redundant and is replaced by a NOP (for the lineno)
896897

897898
insts = [
898899
('LOAD_CONST', 3, 11),
@@ -903,8 +904,7 @@ def test_conditional_jump_forward_const_condition(self):
903904
]
904905
expected = [
905906
('NOP', None, 11),
906-
('JUMP', lbl := self.Label(), 12),
907-
lbl,
907+
('NOP', None, 12),
908908
('LOAD_CONST', '3', 14)
909909
]
910910
self.cfg_optimization_test(insts, expected, consts=list(range(5)))

Python/compile.c

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7396,6 +7396,9 @@ mark_cold(basicblock *entryblock) {
73967396
return 0;
73977397
}
73987398

7399+
static int
7400+
remove_redundant_jumps(cfg_builder *g);
7401+
73997402
static int
74007403
push_cold_blocks_to_end(cfg_builder *g, int code_flags) {
74017404
basicblock *entryblock = g->g_entryblock;
@@ -7465,6 +7468,12 @@ push_cold_blocks_to_end(cfg_builder *g, int code_flags) {
74657468
}
74667469
assert(b != NULL && b->b_next == NULL);
74677470
b->b_next = cold_blocks;
7471+
7472+
if (cold_blocks != NULL) {
7473+
if (remove_redundant_jumps(g) < 0) {
7474+
return -1;
7475+
}
7476+
}
74687477
return 0;
74697478
}
74707479

@@ -8269,9 +8278,6 @@ trim_unused_consts(basicblock *entryblock, PyObject *consts);
82698278
static int
82708279
duplicate_exits_without_lineno(cfg_builder *g);
82718280

8272-
static int
8273-
extend_block(basicblock *bb);
8274-
82758281
static int *
82768282
build_cellfixedoffsets(struct compiler *c)
82778283
{
@@ -8476,6 +8482,21 @@ propagate_line_numbers(basicblock *entryblock);
84768482
static void
84778483
eliminate_empty_basic_blocks(cfg_builder *g);
84788484

8485+
#ifndef NDEBUG
8486+
static bool
8487+
no_redundant_jumps(cfg_builder *g) {
8488+
for (basicblock *b = g->g_entryblock; b != NULL; b = b->b_next) {
8489+
struct instr *last = basicblock_last_instr(b);
8490+
if (last != NULL) {
8491+
if (last->i_opcode == JUMP || last->i_opcode == JUMP_NO_INTERRUPT) {
8492+
assert(last->i_target != b->b_next);
8493+
return false;
8494+
}
8495+
}
8496+
}
8497+
return true;
8498+
}
8499+
#endif
84798500

84808501
static int
84818502
remove_redundant_jumps(cfg_builder *g) {
@@ -8592,8 +8613,8 @@ assemble(struct compiler *c, int addNone)
85928613
if (trim_unused_consts(g->g_entryblock, consts)) {
85938614
goto error;
85948615
}
8595-
if (duplicate_exits_without_lineno(g)) {
8596-
return NULL;
8616+
if (duplicate_exits_without_lineno(g) < 0) {
8617+
goto error;
85978618
}
85988619
propagate_line_numbers(g->g_entryblock);
85998620
guarantee_lineno_for_exits(g->g_entryblock, c->u->u_firstlineno);
@@ -8612,10 +8633,6 @@ assemble(struct compiler *c, int addNone)
86128633
if (push_cold_blocks_to_end(g, code_flags) < 0) {
86138634
goto error;
86148635
}
8615-
8616-
if (remove_redundant_jumps(g) < 0) {
8617-
goto error;
8618-
}
86198636
for (basicblock *b = g->g_entryblock; b != NULL; b = b->b_next) {
86208637
clean_basic_block(b);
86218638
}
@@ -8627,6 +8644,8 @@ assemble(struct compiler *c, int addNone)
86278644
goto error;
86288645
}
86298646

8647+
assert(no_redundant_jumps(g));
8648+
86308649
/* Can't modify the bytecode after computing jump offsets. */
86318650
assemble_jump_offsets(g->g_entryblock);
86328651

@@ -9488,7 +9507,7 @@ optimize_cfg(cfg_builder *g, PyObject *consts, PyObject *const_cache)
94889507
}
94899508
}
94909509
for (basicblock *b = g->g_entryblock; b != NULL; b = b->b_next) {
9491-
if (extend_block(b)) {
9510+
if (extend_block(b) < 0) {
94929511
return -1;
94939512
}
94949513
}
@@ -9500,7 +9519,7 @@ optimize_cfg(cfg_builder *g, PyObject *consts, PyObject *const_cache)
95009519
assert(b->b_predecessors == 0);
95019520
}
95029521
for (basicblock *b = g->g_entryblock; b != NULL; b = b->b_next) {
9503-
if (extend_block(b)) {
9522+
if (extend_block(b) < 0) {
95049523
return -1;
95059524
}
95069525
}
@@ -9517,6 +9536,9 @@ optimize_cfg(cfg_builder *g, PyObject *consts, PyObject *const_cache)
95179536
for (basicblock *b = g->g_entryblock; b != NULL; b = b->b_next) {
95189537
clean_basic_block(b);
95199538
}
9539+
if (remove_redundant_jumps(g) < 0) {
9540+
return -1;
9541+
}
95209542
return 0;
95219543
}
95229544

0 commit comments

Comments
 (0)
0