Skip to content

Commit 15ace3b

Browse files
committed
address review
1 parent 59de8ab commit 15ace3b

2 files changed

Lines changed: 24 additions & 11 deletions

File tree

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Fix missed ``LOAD_FAST_BORROW`` optimization for ternary expressions by improving basic block inlining during the compiler's control flow graph optimization phase.
1+
Use ``LOAD_FAST_BORROW`` in both branches of a returned conditional expression by merging single-predecessor exit blocks in the CFG optimizer.

‎Python/flowgraph.c‎

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1226,7 +1226,6 @@ basicblock_has_no_lineno(basicblock *b) {
12261226
/* If this block ends with an unconditional jump to a small exit block or
12271227
* a block that has no line numbers (and no fallthrough), then
12281228
* remove the jump and extend this block with the target.
1229-
* Also handles the case where a block falls through to a small exit block.
12301229
* Returns 1 if extended, 0 if no change, and -1 on error.
12311230
*/
12321231
static int
@@ -1236,15 +1235,6 @@ basicblock_inline_small_or_no_lineno_blocks(basicblock *bb) {
12361235
return 0;
12371236
}
12381237
if (!IS_UNCONDITIONAL_JUMP_OPCODE(last->i_opcode)) {
1239-
// If the block ends with a fallthrough to a small exit block, inline it.
1240-
if (BB_HAS_FALLTHROUGH(bb) && bb->b_next != NULL && !is_jump(last)) {
1241-
basicblock *next = bb->b_next;
1242-
if (basicblock_exits_scope(next) && next->b_iused <= MAX_COPY_SIZE) {
1243-
RETURN_IF_ERROR(basicblock_append_instructions(bb, next));
1244-
next->b_predecessors--;
1245-
return 1;
1246-
}
1247-
}
12481238
return 0;
12491239
}
12501240
basicblock *target = last->i_target;
@@ -1288,6 +1278,28 @@ inline_small_or_no_lineno_blocks(basicblock *entryblock) {
12881278
return changes;
12891279
}
12901280

1281+
/* Merge an exit block into its fallthrough predecessor when it is the sole
1282+
* predecessor (requires b_predecessors to be up to date).
1283+
*/
1284+
static int
1285+
merge_single_predecessor_exit_blocks(basicblock *entryblock)
1286+
{
1287+
for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
1288+
cfg_instr *last = basicblock_last_instr(b);
1289+
basicblock *next = b->b_next;
1290+
if (last == NULL || next == NULL || is_jump(last) ||
1291+
!BB_HAS_FALLTHROUGH(b) || next->b_predecessors != 1 ||
1292+
!basicblock_exits_scope(next))
1293+
{
1294+
continue;
1295+
}
1296+
RETURN_IF_ERROR(basicblock_append_instructions(b, next));
1297+
next->b_iused = 0;
1298+
next->b_predecessors = 0;
1299+
}
1300+
return SUCCESS;
1301+
}
1302+
12911303
// Attempt to eliminate jumps to jumps by updating inst to jump to
12921304
// target->i_target using the provided opcode. Return whether or not the
12931305
// optimization was successful.
@@ -2687,6 +2699,7 @@ optimize_cfg(cfg_builder *g, PyObject *consts, PyObject *const_cache,
26872699
RETURN_IF_ERROR(check_cfg(g));
26882700
RETURN_IF_ERROR(inline_small_or_no_lineno_blocks(g->g_entryblock));
26892701
RETURN_IF_ERROR(remove_unreachable(g->g_entryblock));
2702+
RETURN_IF_ERROR(merge_single_predecessor_exit_blocks(g->g_entryblock));
26902703
RETURN_IF_ERROR(resolve_line_numbers(g, firstlineno));
26912704
RETURN_IF_ERROR(optimize_load_const(const_cache, g, consts, consts_index));
26922705
for (basicblock *b = g->g_entryblock; b != NULL; b = b->b_next) {

0 commit comments

Comments
 (0)