Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Lib/test/test_peepholer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1129,6 +1129,14 @@ def f(self):
print(ExecError)
self.assertInBytecode(f, "LOAD_FAST_BORROW", "self")

def test_ternary_expression_uses_load_fast_borrow(self):
def f(a, b):
return a if a < b else b

self.assertNotInBytecode(f, "LOAD_FAST")
self.assertInBytecode(f, "LOAD_FAST_BORROW", "a")
self.assertInBytecode(f, "LOAD_FAST_BORROW", "b")

class DirectCfgOptimizerTests(CfgOptimizationTestCase):

def cfg_optimization_test(self, insts, expected_insts,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix missed ``LOAD_FAST_BORROW`` optimization for ternary expressions by improving basic block inlining during the compiler's control flow graph optimization phase.
10 changes: 10 additions & 0 deletions Python/flowgraph.c
Original file line number Diff line number Diff line change
Expand Up @@ -1218,6 +1218,7 @@ basicblock_has_no_lineno(basicblock *b) {
/* If this block ends with an unconditional jump to a small exit block or
* a block that has no line numbers (and no fallthrough), then
* remove the jump and extend this block with the target.
* Also handles the case where a block falls through to a small exit block.
* Returns 1 if extended, 0 if no change, and -1 on error.
*/
static int
Expand All @@ -1227,6 +1228,15 @@ basicblock_inline_small_or_no_lineno_blocks(basicblock *bb) {
return 0;
}
if (!IS_UNCONDITIONAL_JUMP_OPCODE(last->i_opcode)) {
// If the block ends with a fallthrough to a small exit block, inline it.
if (BB_HAS_FALLTHROUGH(bb) && bb->b_next != NULL && !is_jump(last)) {
basicblock *next = bb->b_next;
if (basicblock_exits_scope(next) && next->b_iused <= MAX_COPY_SIZE) {
RETURN_IF_ERROR(basicblock_append_instructions(bb, next));
next->b_predecessors--;
return 1;
}
}
return 0;
}
basicblock *target = last->i_target;
Expand Down
Loading