mirror of
https://github.com/python/cpython.git
synced 2025-08-31 14:07:50 +00:00
Teach the peepholer to fold unary operations on constants.
Afterwards, -0.5 loads in a single step and no longer requires a runtime UNARY_NEGATIVE operation.
This commit is contained in:
parent
80121491e0
commit
afd842f5b2
1 changed files with 19 additions and 0 deletions
|
@ -133,6 +133,25 @@ class TestTranforms(unittest.TestCase):
|
|||
asm = dis_single('a="x"*1000')
|
||||
self.assert_('(1000)' in asm)
|
||||
|
||||
def test_folding_of_unaryops_on_constants(self):
|
||||
for line, elem in (
|
||||
('`1`', "('1')"), # unary convert
|
||||
('-0.5', '(-0.5)'), # unary negative
|
||||
('~-2', '(1)'), # unary invert
|
||||
):
|
||||
asm = dis_single(line)
|
||||
self.assert_(elem in asm, asm)
|
||||
self.assert_('UNARY_' not in asm)
|
||||
|
||||
# Verify that unfoldables are skipped
|
||||
for line, elem in (
|
||||
('-"abc"', "('abc')"), # unary negative
|
||||
('~"abc"', "('abc')"), # unary invert
|
||||
):
|
||||
asm = dis_single(line)
|
||||
self.assert_(elem in asm, asm)
|
||||
self.assert_('UNARY_' in asm)
|
||||
|
||||
def test_elim_extra_return(self):
|
||||
# RETURN LOAD_CONST None RETURN --> RETURN
|
||||
def f(x):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue