mirror of
https://github.com/python/cpython.git
synced 2025-07-24 11:44:31 +00:00
gh-93223: More aggressive Jump-To-Jump elimination (GH-93229)
This commit is contained in:
parent
5e34b494a0
commit
ddc4a782d3
4 changed files with 105 additions and 98 deletions
|
@ -43,11 +43,11 @@ class TestTranforms(BytecodeTestCase):
|
|||
continue
|
||||
tgt = targets[instr.argval]
|
||||
# jump to unconditional jump
|
||||
if tgt.opname in ('JUMP_ABSOLUTE', 'JUMP_FORWARD'):
|
||||
if tgt.opname in ('JUMP_BACKWARD', 'JUMP_FORWARD'):
|
||||
self.fail(f'{instr.opname} at {instr.offset} '
|
||||
f'jumps to {tgt.opname} at {tgt.offset}')
|
||||
# unconditional jump to RETURN_VALUE
|
||||
if (instr.opname in ('JUMP_ABSOLUTE', 'JUMP_FORWARD') and
|
||||
if (instr.opname in ('JUMP_BACKWARD', 'JUMP_FORWARD') and
|
||||
tgt.opname == 'RETURN_VALUE'):
|
||||
self.fail(f'{instr.opname} at {instr.offset} '
|
||||
f'jumps to {tgt.opname} at {tgt.offset}')
|
||||
|
@ -352,7 +352,7 @@ class TestTranforms(BytecodeTestCase):
|
|||
else false_value)
|
||||
self.check_jump_targets(f)
|
||||
self.assertNotInBytecode(f, 'JUMP_FORWARD')
|
||||
self.assertNotInBytecode(f, 'JUMP_ABSOLUTE')
|
||||
self.assertNotInBytecode(f, 'JUMP_BACKWARD')
|
||||
returns = [instr for instr in dis.get_instructions(f)
|
||||
if instr.opname == 'RETURN_VALUE']
|
||||
self.assertEqual(len(returns), 2)
|
||||
|
@ -372,7 +372,7 @@ class TestTranforms(BytecodeTestCase):
|
|||
self.check_lnotab(f)
|
||||
|
||||
def test_elim_jump_to_uncond_jump2(self):
|
||||
# POP_JUMP_IF_FALSE to JUMP_ABSOLUTE --> POP_JUMP_IF_FALSE to non-jump
|
||||
# POP_JUMP_IF_FALSE to JUMP_BACKWARD --> POP_JUMP_IF_FALSE to non-jump
|
||||
def f():
|
||||
while a:
|
||||
# Intentionally use two-line expression to test issue37213.
|
||||
|
@ -417,6 +417,13 @@ class TestTranforms(BytecodeTestCase):
|
|||
self.assertInBytecode(f, 'JUMP_IF_FALSE_OR_POP')
|
||||
self.assertInBytecode(f, 'POP_JUMP_FORWARD_IF_TRUE')
|
||||
|
||||
def test_elim_jump_to_uncond_jump4(self):
|
||||
def f():
|
||||
for i in range(5):
|
||||
if i > 3:
|
||||
print(i)
|
||||
self.check_jump_targets(f)
|
||||
|
||||
def test_elim_jump_after_return1(self):
|
||||
# Eliminate dead code: jumps immediately after returns can't be reached
|
||||
def f(cond1, cond2):
|
||||
|
@ -429,7 +436,7 @@ class TestTranforms(BytecodeTestCase):
|
|||
return 5
|
||||
return 6
|
||||
self.assertNotInBytecode(f, 'JUMP_FORWARD')
|
||||
self.assertNotInBytecode(f, 'JUMP_ABSOLUTE')
|
||||
self.assertNotInBytecode(f, 'JUMP_BACKWARD')
|
||||
returns = [instr for instr in dis.get_instructions(f)
|
||||
if instr.opname == 'RETURN_VALUE']
|
||||
self.assertLessEqual(len(returns), 6)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue