[pypy-commit] pypy default: Optimize f0 * -1.0 to -f0 in the JIT.

alex_gaynor noreply at buildbot.pypy.org
Wed Jun 15 16:55:53 CEST 2011


Author: Alex Gaynor <alex.gaynor at gmail.com>
Branch: 
Changeset: r44960:1b06773014db
Date: 2011-06-15 07:58 -0700
http://bitbucket.org/pypy/pypy/changeset/1b06773014db/

Log:	Optimize f0 * -1.0 to -f0 in the JIT.

diff --git a/pypy/jit/metainterp/optimizeopt/rewrite.py b/pypy/jit/metainterp/optimizeopt/rewrite.py
--- a/pypy/jit/metainterp/optimizeopt/rewrite.py
+++ b/pypy/jit/metainterp/optimizeopt/rewrite.py
@@ -185,16 +185,25 @@
             self.emit_operation(op)
 
     def optimize_FLOAT_MUL(self, op):
-        v1 = self.getvalue(op.getarg(0))
-        v2 = self.getvalue(op.getarg(1))
+        arg1 = op.getarg(0)
+        arg2 = op.getarg(1)
 
-        # Constant fold f0 * 1.0, this works in all cases, including NaN and inf
-        if v1.is_constant() and v1.box.getfloat() == 1.0:
-            self.make_equal_to(op.result, v2)
-        elif v2.is_constant() and v2.box.getfloat() == 1.0:
-            self.make_equal_to(op.result, v1)
-        else:
-            self.emit_operation(op)
+        # Constant fold f0 * 1.0 and turn f0 * -1.0 into a FLOAT_NEG, these
+        # work in all cases, including NaN and inf
+        for lhs, rhs in [(arg1, arg2), (arg2, arg1)]:
+            v1 = self.getvalue(lhs)
+            v2 = self.getvalue(rhs)
+
+            if v1.is_constant():
+                if v1.box.getfloat() == 1.0:
+                    self.make_equal_to(op.result, v2)
+                    return
+                elif v1.box.getfloat() == -1.0:
+                    self.emit_operation(ResOperation(
+                        rop.FLOAT_NEG, [rhs], op.result
+                    ))
+                    return
+        self.emit_operation(op)
 
     def optimize_CALL_PURE(self, op):
         arg_consts = []
diff --git a/pypy/jit/metainterp/optimizeopt/test/test_optimizebasic.py b/pypy/jit/metainterp/optimizeopt/test/test_optimizebasic.py
--- a/pypy/jit/metainterp/optimizeopt/test/test_optimizebasic.py
+++ b/pypy/jit/metainterp/optimizeopt/test/test_optimizebasic.py
@@ -2153,6 +2153,35 @@
         """
         self.optimize_loop(ops, expected)
 
+
+        ops = """
+        [f0]
+        f1 = float_mul(f0, -1.0)
+        f2 = escape(f1)
+        jump(f2)
+        """
+        expected = """
+        [f0]
+        f1 = float_neg(f0)
+        f2 = escape(f1)
+        jump(f2)
+        """
+        self.optimize_loop(ops, expected)
+
+        ops = """
+        [f0]
+        f1 = float_mul(-1.0, f0)
+        f2 = escape(f1)
+        jump(f2)
+        """
+        expected = """
+        [f0]
+        f1 = float_neg(f0)
+        f2 = escape(f1)
+        jump(f2)
+        """
+        self.optimize_loop(ops, expected)
+
     # ----------
 
     def make_fail_descr(self):


More information about the pypy-commit mailing list