Skip to content

Commit 33c27a2

Browse files
iritkatrielmdboom
authored andcommitted
gh-87092: use basicblock_last_instr consistently in the compiler (GH-96243)
1 parent 5a47b78 commit 33c27a2

File tree

1 file changed

+32
-29
lines changed

1 file changed

+32
-29
lines changed

Python/compile.c

+32-29
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,9 @@ typedef struct basicblock_ {
291291

292292
static struct instr *
293293
basicblock_last_instr(const basicblock *b) {
294-
if (b->b_iused) {
294+
assert(b->b_iused >= 0);
295+
if (b->b_iused > 0) {
296+
assert(b->b_instr != NULL);
295297
return &b->b_instr[b->b_iused - 1];
296298
}
297299
return NULL;
@@ -7168,10 +7170,8 @@ assemble_free(struct assembler *a)
71687170
static int
71697171
blocksize(basicblock *b)
71707172
{
7171-
int i;
71727173
int size = 0;
7173-
7174-
for (i = 0; i < b->b_iused; i++) {
7174+
for (int i = 0; i < b->b_iused; i++) {
71757175
size += instr_size(&b->b_instr[i]);
71767176
}
71777177
return size;
@@ -7746,10 +7746,10 @@ normalize_jumps(basicblock *entryblock)
77467746
}
77477747
for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
77487748
b->b_visited = 1;
7749-
if (b->b_iused == 0) {
7749+
struct instr *last = basicblock_last_instr(b);
7750+
if (last == NULL) {
77507751
continue;
77517752
}
7752-
struct instr *last = &b->b_instr[b->b_iused-1];
77537753
assert(!IS_ASSEMBLER_OPCODE(last->i_opcode));
77547754
if (is_jump(last)) {
77557755
bool is_forward = last->i_target->b_visited == 0;
@@ -7915,8 +7915,8 @@ scan_block_for_local(int target, basicblock *b, bool unsafe_to_start,
79157915
if (b->b_next && BB_HAS_FALLTHROUGH(b)) {
79167916
MAYBE_PUSH(b->b_next);
79177917
}
7918-
if (b->b_iused > 0) {
7919-
struct instr *last = &b->b_instr[b->b_iused-1];
7918+
struct instr *last = basicblock_last_instr(b);
7919+
if (last != NULL) {
79207920
if (is_jump(last)) {
79217921
assert(last->i_target != NULL);
79227922
MAYBE_PUSH(last->i_target);
@@ -8405,10 +8405,10 @@ guarantee_lineno_for_exits(basicblock *entryblock, int firstlineno) {
84058405
int lineno = firstlineno;
84068406
assert(lineno > 0);
84078407
for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
8408-
if (b->b_iused == 0) {
8408+
struct instr *last = basicblock_last_instr(b);
8409+
if (last == NULL) {
84098410
continue;
84108411
}
8411-
struct instr *last = &b->b_instr[b->b_iused-1];
84128412
if (last->i_loc.lineno < 0) {
84138413
if (last->i_opcode == RETURN_VALUE) {
84148414
for (int i = 0; i < b->b_iused; i++) {
@@ -8486,18 +8486,18 @@ remove_redundant_jumps(cfg_builder *g) {
84868486
*/
84878487
int removed = 0;
84888488
for (basicblock *b = g->g_entryblock; b != NULL; b = b->b_next) {
8489-
if (b->b_iused > 0) {
8490-
struct instr *b_last_instr = &b->b_instr[b->b_iused - 1];
8491-
assert(!IS_ASSEMBLER_OPCODE(b_last_instr->i_opcode));
8492-
if (b_last_instr->i_opcode == JUMP ||
8493-
b_last_instr->i_opcode == JUMP_NO_INTERRUPT) {
8494-
if (b_last_instr->i_target == NULL) {
8489+
struct instr *last = basicblock_last_instr(b);
8490+
if (last != NULL) {
8491+
assert(!IS_ASSEMBLER_OPCODE(last->i_opcode));
8492+
if (last->i_opcode == JUMP ||
8493+
last->i_opcode == JUMP_NO_INTERRUPT) {
8494+
if (last->i_target == NULL) {
84958495
PyErr_SetString(PyExc_SystemError, "jump with NULL target");
84968496
return -1;
84978497
}
8498-
if (b_last_instr->i_target == b->b_next) {
8498+
if (last->i_target == b->b_next) {
84998499
assert(b->b_next->b_iused);
8500-
b_last_instr->i_opcode = NOP;
8500+
last->i_opcode = NOP;
85018501
removed++;
85028502
}
85038503
}
@@ -9215,10 +9215,10 @@ basicblock_has_lineno(const basicblock *bb) {
92159215
*/
92169216
static int
92179217
extend_block(basicblock *bb) {
9218-
if (bb->b_iused == 0) {
9218+
struct instr *last = basicblock_last_instr(bb);
9219+
if (last == NULL) {
92199220
return 0;
92209221
}
9221-
struct instr *last = &bb->b_instr[bb->b_iused-1];
92229222
if (last->i_opcode != JUMP &&
92239223
last->i_opcode != JUMP_FORWARD &&
92249224
last->i_opcode != JUMP_BACKWARD) {
@@ -9399,7 +9399,8 @@ eliminate_empty_basic_blocks(cfg_builder *g) {
93999399
static void
94009400
propagate_line_numbers(basicblock *entryblock) {
94019401
for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
9402-
if (b->b_iused == 0) {
9402+
struct instr *last = basicblock_last_instr(b);
9403+
if (last == NULL) {
94039404
continue;
94049405
}
94059406

@@ -9418,8 +9419,8 @@ propagate_line_numbers(basicblock *entryblock) {
94189419
b->b_next->b_instr[0].i_loc = prev_location;
94199420
}
94209421
}
9421-
if (is_jump(&b->b_instr[b->b_iused-1])) {
9422-
basicblock *target = b->b_instr[b->b_iused-1].i_target;
9422+
if (is_jump(last)) {
9423+
basicblock *target = last->i_target;
94239424
if (target->b_predecessors == 1) {
94249425
if (target->b_instr[0].i_loc.lineno < 0) {
94259426
target->b_instr[0].i_loc = prev_location;
@@ -9576,15 +9577,16 @@ duplicate_exits_without_lineno(cfg_builder *g)
95769577
*/
95779578
basicblock *entryblock = g->g_entryblock;
95789579
for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
9579-
if (b->b_iused > 0 && is_jump(&b->b_instr[b->b_iused-1])) {
9580-
basicblock *target = b->b_instr[b->b_iused-1].i_target;
9580+
struct instr *last = basicblock_last_instr(b);
9581+
if (last != NULL && is_jump(last)) {
9582+
basicblock *target = last->i_target;
95819583
if (is_exit_without_lineno(target) && target->b_predecessors > 1) {
95829584
basicblock *new_target = copy_basicblock(g, target);
95839585
if (new_target == NULL) {
95849586
return -1;
95859587
}
9586-
new_target->b_instr[0].i_loc = b->b_instr[b->b_iused-1].i_loc;
9587-
b->b_instr[b->b_iused-1].i_target = new_target;
9588+
new_target->b_instr[0].i_loc = last->i_loc;
9589+
last->i_target = new_target;
95889590
target->b_predecessors--;
95899591
new_target->b_predecessors = 1;
95909592
new_target->b_next = target->b_next;
@@ -9603,8 +9605,9 @@ duplicate_exits_without_lineno(cfg_builder *g)
96039605
for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
96049606
if (BB_HAS_FALLTHROUGH(b) && b->b_next && b->b_iused > 0) {
96059607
if (is_exit_without_lineno(b->b_next)) {
9606-
assert(b->b_next->b_iused > 0);
9607-
b->b_next->b_instr[0].i_loc = b->b_instr[b->b_iused-1].i_loc;
9608+
struct instr *last = basicblock_last_instr(b);
9609+
assert(last != NULL);
9610+
b->b_next->b_instr[0].i_loc = last->i_loc;
96089611
}
96099612
}
96109613
}

0 commit comments

Comments
 (0)