Skip to content

Commit 1659ad1

Browse files
authored
Eliminate NOPs in extended blocks. (GH-24209)
1 parent 3bd6035 commit 1659ad1

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

Python/compile.c

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6328,10 +6328,9 @@ optimize_basic_block(basicblock *bb, PyObject *consts)
63286328

63296329

63306330
static void
6331-
clean_basic_block(basicblock *bb) {
6332-
/* Remove NOPs. */
6331+
clean_basic_block(basicblock *bb, int prev_lineno) {
6332+
/* Remove NOPs when legal to do so. */
63336333
int dest = 0;
6334-
int prev_lineno = -1;
63356334
for (int src = 0; src < bb->b_iused; src++) {
63366335
int lineno = bb->b_instr[src].i_lineno;
63376336
if (bb->b_instr[src].i_opcode == NOP) {
@@ -6531,7 +6530,7 @@ optimize_cfg(struct assembler *a, PyObject *consts)
65316530
if (optimize_basic_block(b, consts)) {
65326531
return -1;
65336532
}
6534-
clean_basic_block(b);
6533+
clean_basic_block(b, -1);
65356534
assert(b->b_predecessors == 0);
65366535
}
65376536
if (mark_reachable(a)) {
@@ -6544,6 +6543,15 @@ optimize_cfg(struct assembler *a, PyObject *consts)
65446543
b->b_nofallthrough = 0;
65456544
}
65466545
}
6546+
basicblock *pred = NULL;
6547+
for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
6548+
int prev_lineno = -1;
6549+
if (pred && pred->b_iused) {
6550+
prev_lineno = pred->b_instr[pred->b_iused-1].i_lineno;
6551+
}
6552+
clean_basic_block(b, prev_lineno);
6553+
pred = b->b_nofallthrough ? NULL : b;
6554+
}
65476555
eliminate_empty_basic_blocks(a->a_entry);
65486556
/* Delete jump instructions made redundant by previous step. If a non-empty
65496557
block ends with a jump instruction, check if the next non-empty block
@@ -6571,7 +6579,7 @@ optimize_cfg(struct assembler *a, PyObject *consts)
65716579
case JUMP_ABSOLUTE:
65726580
case JUMP_FORWARD:
65736581
b_last_instr->i_opcode = NOP;
6574-
clean_basic_block(b);
6582+
clean_basic_block(b, -1);
65756583
maybe_empty_blocks = 1;
65766584
break;
65776585
}

0 commit comments

Comments
 (0)