Skip to content

Commit 610738b

Browse files
committed
optimize CFG bb inline
1 parent 8e1469c commit 610738b

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

Lib/test/test_peepholer.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1129,6 +1129,14 @@ def f(self):
11291129
print(ExecError)
11301130
self.assertInBytecode(f, "LOAD_FAST_BORROW", "self")
11311131

1132+
def test_ternary_expression_uses_load_fast_borrow(self):
1133+
def f(a, b):
1134+
return a if a < b else b
1135+
1136+
self.assertNotInBytecode(f, "LOAD_FAST")
1137+
self.assertInBytecode(f, "LOAD_FAST_BORROW", "a")
1138+
self.assertInBytecode(f, "LOAD_FAST_BORROW", "b")
1139+
11321140
class DirectCfgOptimizerTests(CfgOptimizationTestCase):
11331141

11341142
def cfg_optimization_test(self, insts, expected_insts,

Python/flowgraph.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1218,6 +1218,7 @@ basicblock_has_no_lineno(basicblock *b) {
12181218
/* If this block ends with an unconditional jump to a small exit block or
12191219
* a block that has no line numbers (and no fallthrough), then
12201220
* remove the jump and extend this block with the target.
1221+
* Also handles the case where a block falls through to a small exit block.
12211222
* Returns 1 if extended, 0 if no change, and -1 on error.
12221223
*/
12231224
static int
@@ -1227,6 +1228,15 @@ basicblock_inline_small_or_no_lineno_blocks(basicblock *bb) {
12271228
return 0;
12281229
}
12291230
if (!IS_UNCONDITIONAL_JUMP_OPCODE(last->i_opcode)) {
1231+
// If the block ends with a fallthrough to a small exit block, inline it.
1232+
if (BB_HAS_FALLTHROUGH(bb) && bb->b_next != NULL && !is_jump(last)) {
1233+
basicblock *next = bb->b_next;
1234+
if (basicblock_exits_scope(next) && next->b_iused <= MAX_COPY_SIZE) {
1235+
RETURN_IF_ERROR(basicblock_append_instructions(bb, next));
1236+
next->b_predecessors--;
1237+
return 1;
1238+
}
1239+
}
12301240
return 0;
12311241
}
12321242
basicblock *target = last->i_target;

0 commit comments

Comments
 (0)