5544c1b
From af2bf6bcc6614622c87d28e9d763b57408c17500 Mon Sep 17 00:00:00 2001
5544c1b
From: Richard Henderson <rth@twiddle.net>
5544c1b
Date: Fri, 21 Sep 2012 10:13:38 -0700
5544c1b
Subject: [PATCH] tcg: Optimize two-address commutative operations
5544c1b
5544c1b
While swapping constants to the second operand, swap
5544c1b
sources matching destinations to the first operand.
5544c1b
5544c1b
Signed-off-by: Richard Henderson <rth@twiddle.net>
5544c1b
Signed-off-by: Aurelien Jarno <aurelien@aurel32.net>
5544c1b
Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
5544c1b
---
5544c1b
 tcg/optimize.c | 15 ++++++++++++++-
5544c1b
 1 file changed, 14 insertions(+), 1 deletion(-)
5544c1b
5544c1b
diff --git a/tcg/optimize.c b/tcg/optimize.c
5544c1b
index 26038a6..1be7631 100644
5544c1b
--- a/tcg/optimize.c
5544c1b
+++ b/tcg/optimize.c
5544c1b
@@ -334,6 +334,8 @@ static TCGArg *tcg_constant_folding(TCGContext *s, uint16_t *tcg_opc_ptr,
5544c1b
     const TCGOpDef *def;
5544c1b
     TCGArg *gen_args;
5544c1b
     TCGArg tmp;
5544c1b
+    TCGCond cond;
5544c1b
+
5544c1b
     /* Array VALS has an element for each temp.
5544c1b
        If this temp holds a constant then its value is kept in VALS' element.
5544c1b
        If this temp is a copy of other ones then this equivalence class'
5544c1b
@@ -395,13 +397,24 @@ static TCGArg *tcg_constant_folding(TCGContext *s, uint16_t *tcg_opc_ptr,
5544c1b
             }
5544c1b
             break;
5544c1b
         CASE_OP_32_64(movcond):
5544c1b
+            cond = args[5];
5544c1b
             if (temps[args[1]].state == TCG_TEMP_CONST
5544c1b
                 && temps[args[2]].state != TCG_TEMP_CONST) {
5544c1b
                 tmp = args[1];
5544c1b
                 args[1] = args[2];
5544c1b
                 args[2] = tmp;
5544c1b
-                args[5] = tcg_swap_cond(args[5]);
5544c1b
+                cond = tcg_swap_cond(cond);
5544c1b
+            }
5544c1b
+            /* For movcond, we canonicalize the "false" input reg to match
5544c1b
+               the destination reg so that the tcg backend can implement
5544c1b
+               a "move if true" operation.  */
5544c1b
+            if (args[0] == args[3]) {
5544c1b
+                tmp = args[3];
5544c1b
+                args[3] = args[4];
5544c1b
+                args[4] = tmp;
5544c1b
+                cond = tcg_invert_cond(cond);
5544c1b
             }
5544c1b
+            args[5] = cond;
5544c1b
         default:
5544c1b
             break;
5544c1b
         }