summaryrefslogtreecommitdiffstats
path: root/Python
diff options
context:
space:
mode:
authorIrit Katriel <1055913+iritkatriel@users.noreply.github.com>2022-09-01 10:03:52 (GMT)
committerGitHub <noreply@github.com>2022-09-01 10:03:52 (GMT)
commit894cafd9a53f2423adc6b1fb111006883aeac03c (patch)
tree3118d625cc4b82d8bb617071be218641242af8e1 /Python
parente93d1bda77b71db7ca150187f841fbb237772b2c (diff)
downloadcpython-894cafd9a53f2423adc6b1fb111006883aeac03c.zip
cpython-894cafd9a53f2423adc6b1fb111006883aeac03c.tar.gz
cpython-894cafd9a53f2423adc6b1fb111006883aeac03c.tar.bz2
gh-93678: apply remove_redundant_jumps in optimize_cfg (GH-96274)
Diffstat (limited to 'Python')
-rw-r--r--Python/compile.c44
1 files changed, 33 insertions, 11 deletions
diff --git a/Python/compile.c b/Python/compile.c
index 627f86a..857fca4 100644
--- a/Python/compile.c
+++ b/Python/compile.c
@@ -7397,6 +7397,9 @@ mark_cold(basicblock *entryblock) {
}
static int
+remove_redundant_jumps(cfg_builder *g);
+
+static int
push_cold_blocks_to_end(cfg_builder *g, int code_flags) {
basicblock *entryblock = g->g_entryblock;
if (entryblock->b_next == NULL) {
@@ -7465,6 +7468,12 @@ push_cold_blocks_to_end(cfg_builder *g, int code_flags) {
}
assert(b != NULL && b->b_next == NULL);
b->b_next = cold_blocks;
+
+ if (cold_blocks != NULL) {
+ if (remove_redundant_jumps(g) < 0) {
+ return -1;
+ }
+ }
return 0;
}
@@ -8269,9 +8278,6 @@ trim_unused_consts(basicblock *entryblock, PyObject *consts);
static int
duplicate_exits_without_lineno(cfg_builder *g);
-static int
-extend_block(basicblock *bb);
-
static int *
build_cellfixedoffsets(struct compiler *c)
{
@@ -8476,6 +8482,21 @@ propagate_line_numbers(basicblock *entryblock);
static void
eliminate_empty_basic_blocks(cfg_builder *g);
+#ifndef NDEBUG
+static bool
+no_redundant_jumps(cfg_builder *g) {
+ for (basicblock *b = g->g_entryblock; b != NULL; b = b->b_next) {
+ struct instr *last = basicblock_last_instr(b);
+ if (last != NULL) {
+ if (last->i_opcode == JUMP || last->i_opcode == JUMP_NO_INTERRUPT) {
+ assert(last->i_target != b->b_next);
+ return false;
+ }
+ }
+ }
+ return true;
+}
+#endif
static int
remove_redundant_jumps(cfg_builder *g) {
@@ -8592,8 +8613,8 @@ assemble(struct compiler *c, int addNone)
if (trim_unused_consts(g->g_entryblock, consts)) {
goto error;
}
- if (duplicate_exits_without_lineno(g)) {
- return NULL;
+ if (duplicate_exits_without_lineno(g) < 0) {
+ goto error;
}
propagate_line_numbers(g->g_entryblock);
guarantee_lineno_for_exits(g->g_entryblock, c->u->u_firstlineno);
@@ -8612,10 +8633,6 @@ assemble(struct compiler *c, int addNone)
if (push_cold_blocks_to_end(g, code_flags) < 0) {
goto error;
}
-
- if (remove_redundant_jumps(g) < 0) {
- goto error;
- }
for (basicblock *b = g->g_entryblock; b != NULL; b = b->b_next) {
clean_basic_block(b);
}
@@ -8627,6 +8644,8 @@ assemble(struct compiler *c, int addNone)
goto error;
}
+ assert(no_redundant_jumps(g));
+
/* Can't modify the bytecode after computing jump offsets. */
assemble_jump_offsets(g->g_entryblock);
@@ -9488,7 +9507,7 @@ optimize_cfg(cfg_builder *g, PyObject *consts, PyObject *const_cache)
}
}
for (basicblock *b = g->g_entryblock; b != NULL; b = b->b_next) {
- if (extend_block(b)) {
+ if (extend_block(b) < 0) {
return -1;
}
}
@@ -9500,7 +9519,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)) {
+ if (extend_block(b) < 0) {
return -1;
}
}
@@ -9517,6 +9536,9 @@ optimize_cfg(cfg_builder *g, PyObject *consts, PyObject *const_cache)
for (basicblock *b = g->g_entryblock; b != NULL; b = b->b_next) {
clean_basic_block(b);
}
+ if (remove_redundant_jumps(g) < 0) {
+ return -1;
+ }
return 0;
}