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 @@ -1156,6 +1156,14 @@ def f(self):
print(heapify_max)
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 test_optimize_cfg_const_index_out_of_range(self):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Use ``LOAD_FAST_BORROW`` in both branches of a returned conditional expression by merging single-predecessor exit blocks in the CFG optimizer.
23 changes: 23 additions & 0 deletions Python/flowgraph.c
Original file line number Diff line number Diff line change
Expand Up @@ -1278,6 +1278,28 @@ inline_small_or_no_lineno_blocks(basicblock *entryblock) {
return changes;
}

/* Merge an exit block into its fallthrough predecessor when it is the sole
* predecessor (requires b_predecessors to be up to date).
*/
static int
merge_single_predecessor_exit_blocks(basicblock *entryblock)
{
for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
cfg_instr *last = basicblock_last_instr(b);
basicblock *next = b->b_next;
if (last == NULL || next == NULL || is_jump(last) ||
!BB_HAS_FALLTHROUGH(b) || next->b_predecessors != 1 ||
!basicblock_exits_scope(next))
{
continue;
}
RETURN_IF_ERROR(basicblock_append_instructions(b, next));
next->b_iused = 0;
next->b_predecessors = 0;
}
return SUCCESS;
}

// Attempt to eliminate jumps to jumps by updating inst to jump to
// target->i_target using the provided opcode. Return whether or not the
// optimization was successful.
Expand Down Expand Up @@ -2677,6 +2699,7 @@ optimize_cfg(cfg_builder *g, PyObject *consts, PyObject *const_cache,
RETURN_IF_ERROR(check_cfg(g));
RETURN_IF_ERROR(inline_small_or_no_lineno_blocks(g->g_entryblock));
RETURN_IF_ERROR(remove_unreachable(g->g_entryblock));
RETURN_IF_ERROR(merge_single_predecessor_exit_blocks(g->g_entryblock));
RETURN_IF_ERROR(resolve_line_numbers(g, firstlineno));
RETURN_IF_ERROR(optimize_load_const(const_cache, g, consts, consts_index));
for (basicblock *b = g->g_entryblock; b != NULL; b = b->b_next) {
Expand Down
Loading