From 6d6ca20f3a4273e0280974a72d7dff2b23e1f848 Mon Sep 17 00:00:00 2001 From: Pablo Galindo Date: Fri, 14 Jun 2019 06:27:21 +0100 Subject: [PATCH 1/4] Correctly optimize constant conditionals of binops --- Lib/test/test_peepholer.py | 7 +++++++ Python/peephole.c | 4 ++++ 2 files changed, 11 insertions(+) diff --git a/Lib/test/test_peepholer.py b/Lib/test/test_peepholer.py index 860ceeb003e7b3d..4e50ce5e591b722 100644 --- a/Lib/test/test_peepholer.py +++ b/Lib/test/test_peepholer.py @@ -414,6 +414,13 @@ def forloop(): pass self.assertEqual(count_instr_recursively(forloop, 'BUILD_LIST'), 0) + def test_constant_bool_in_compose_if(self): + def f(): + if True or False: + return 1 + return 0 + self.assertEqual(f(), 1) + class TestBuglets(unittest.TestCase): diff --git a/Python/peephole.c b/Python/peephole.c index 6f3e2ed88b2bedc..2700dab3ca2efce 100644 --- a/Python/peephole.c +++ b/Python/peephole.c @@ -315,6 +315,10 @@ PyCode_Optimize(PyObject *code, PyObject* consts, PyObject *names, fill_nops(codestr, op_start, nexti + 1); cumlc = 0; } else if (is_true == 0) { + if (i > 1 && (_Py_OPCODE(codestr[i-1]) == POP_JUMP_IF_TRUE || + _Py_OPCODE(codestr[i-1]) == POP_JUMP_IF_FALSE)) { + break; + } h = get_arg(codestr, nexti) / sizeof(_Py_CODEUNIT); tgt = find_op(codestr, codelen, h); fill_nops(codestr, op_start, tgt); From 0fd6ad97c95c9b54e859d2213ffd5341d11f0a99 Mon Sep 17 00:00:00 2001 From: Pablo Galindo Date: Fri, 14 Jun 2019 06:32:38 +0100 Subject: [PATCH 2/4] Add News entry --- .../Core and Builtins/2019-06-14-06-32-33.bpo-37269.SjVVAe.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2019-06-14-06-32-33.bpo-37269.SjVVAe.rst diff --git a/Misc/NEWS.d/next/Core and Builtins/2019-06-14-06-32-33.bpo-37269.SjVVAe.rst b/Misc/NEWS.d/next/Core and Builtins/2019-06-14-06-32-33.bpo-37269.SjVVAe.rst new file mode 100644 index 000000000000000..b9b79066774f86d --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2019-06-14-06-32-33.bpo-37269.SjVVAe.rst @@ -0,0 +1,2 @@ +Fix a bug in the peephole optimizer that was not treating correctly constant +conditions with binary operators. Patch by Pablo Galindo. From 152f7004ba996dacfbde318e3eb1ca4f7d773a89 Mon Sep 17 00:00:00 2001 From: Pablo Galindo Date: Fri, 14 Jun 2019 06:34:48 +0100 Subject: [PATCH 3/4] Add formatting --- Python/peephole.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Python/peephole.c b/Python/peephole.c index 2700dab3ca2efce..d7b1dfc4d9c1419 100644 --- a/Python/peephole.c +++ b/Python/peephole.c @@ -315,8 +315,9 @@ PyCode_Optimize(PyObject *code, PyObject* consts, PyObject *names, fill_nops(codestr, op_start, nexti + 1); cumlc = 0; } else if (is_true == 0) { - if (i > 1 && (_Py_OPCODE(codestr[i-1]) == POP_JUMP_IF_TRUE || - _Py_OPCODE(codestr[i-1]) == POP_JUMP_IF_FALSE)) { + if (i > 1 && + (_Py_OPCODE(codestr[i - 1]) == POP_JUMP_IF_TRUE || + _Py_OPCODE(codestr[i - 1]) == POP_JUMP_IF_FALSE)) { break; } h = get_arg(codestr, nexti) / sizeof(_Py_CODEUNIT); From 14860e901c850c1f6a2269de15b30bb5d398801c Mon Sep 17 00:00:00 2001 From: Pablo Galindo Date: Fri, 14 Jun 2019 06:38:13 +0100 Subject: [PATCH 4/4] Better name --- Lib/test/test_peepholer.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_peepholer.py b/Lib/test/test_peepholer.py index 4e50ce5e591b722..5d00240e2595a8c 100644 --- a/Lib/test/test_peepholer.py +++ b/Lib/test/test_peepholer.py @@ -414,7 +414,7 @@ def forloop(): pass self.assertEqual(count_instr_recursively(forloop, 'BUILD_LIST'), 0) - def test_constant_bool_in_compose_if(self): + def test_condition_with_binop_with_bools(self): def f(): if True or False: return 1