Commit 17b880a5 authored by Antoine Pitrou's avatar Antoine Pitrou

Issue #11244: The peephole optimizer is now able to constant-fold

arbitrarily complex expressions.  This also fixes a 3.2 regression where
operations involving negative numbers were not constant-folded.
parent 336c7295
......@@ -8,8 +8,10 @@ def disassemble(func):
f = StringIO()
tmp = sys.stdout
sys.stdout = f
dis.dis(func)
sys.stdout = tmp
try:
dis.dis(func)
finally:
sys.stdout = tmp
result = f.getvalue()
f.close()
return result
......@@ -99,6 +101,12 @@ class TestTranforms(unittest.TestCase):
self.assertIn(elem, asm)
self.assertNotIn('BUILD_TUPLE', asm)
# Long tuples should be folded too.
asm = dis_single(repr(tuple(range(10000))))
# One LOAD_CONST for the tuple, one for the None return value
self.assertEqual(asm.count('LOAD_CONST'), 2)
self.assertNotIn('BUILD_TUPLE', asm)
# Bug 1053819: Tuple of constants misidentified when presented with:
# . . . opcode_with_arg 100 unary_opcode BUILD_TUPLE 1 . . .
# The following would segfault upon compilation
......@@ -267,6 +275,25 @@ class TestTranforms(unittest.TestCase):
asm = disassemble(f)
self.assertNotIn('BINARY_ADD', asm)
def test_constant_folding(self):
# Issue #11244: aggressive constant folding.
exprs = [
"3 * -5",
"-3 * 5",
"2 * (3 * 4)",
"(2 * 3) * 4",
"(-1, 2, 3)",
"(1, -2, 3)",
"(1, 2, -3)",
"(1, 2, -3) * 6",
"lambda x: x in {(3 * -5) + (-1 - 6), (1, -2, 3) * 2, None}",
]
for e in exprs:
asm = dis_single(e)
self.assertNotIn('UNARY_', asm, e)
self.assertNotIn('BINARY_', asm, e)
self.assertNotIn('BUILD_', asm, e)
def test_main(verbose=None):
import sys
......
......@@ -10,6 +10,10 @@ What's New in Python 3.3 Alpha 1?
Core and Builtins
-----------------
- Issue #11244: The peephole optimizer is now able to constant-fold
arbitrarily complex expressions. This also fixes a 3.2 regression where
operations involving negative numbers were not constant-folded.
- Issue #11450: Don't truncate hg version info in Py_GetBuildInfo() when
there are many tags (e.g. when using mq). Patch by Nadeem Vawda.
......
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment