5544c1b
From 8d5f3ca9ccace2374fd73d46fad56decc02e0a44 Mon Sep 17 00:00:00 2001
5544c1b
From: Aurelien Jarno <aurelien@aurel32.net>
5544c1b
Date: Tue, 18 Sep 2012 19:37:00 +0200
5544c1b
Subject: [PATCH] tcg/optimize: further optimize brcond/movcond/setcond
5544c1b
5544c1b
When both argument of brcond/movcond/setcond are the same or when one
5544c1b
of the two values is a constant equal to zero, it's possible to do
5544c1b
further optimizations.
5544c1b
5544c1b
Reviewed-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 | 127 ++++++++++++++++++++++++++++++++++-----------------------
5544c1b
 1 file changed, 76 insertions(+), 51 deletions(-)
5544c1b
5544c1b
diff --git a/tcg/optimize.c b/tcg/optimize.c
5544c1b
index ceea644..abe016a 100644
5544c1b
--- a/tcg/optimize.c
5544c1b
+++ b/tcg/optimize.c
5544c1b
@@ -292,58 +292,88 @@ static TCGArg do_constant_folding(TCGOpcode op, TCGArg x, TCGArg y)
5544c1b
     return res;
5544c1b
 }
5544c1b
 
5544c1b
+/* Return 2 if the condition can't be simplified, and the result
5544c1b
+   of the condition (0 or 1) if it can */
5544c1b
 static TCGArg do_constant_folding_cond(TCGOpcode op, TCGArg x,
5544c1b
                                        TCGArg y, TCGCond c)
5544c1b
 {
5544c1b
-    switch (op_bits(op)) {
5544c1b
-    case 32:
5544c1b
+    if (temps[x].state == TCG_TEMP_CONST && temps[y].state == TCG_TEMP_CONST) {
5544c1b
+        switch (op_bits(op)) {
5544c1b
+        case 32:
5544c1b
+            switch (c) {
5544c1b
+            case TCG_COND_EQ:
5544c1b
+                return (uint32_t)temps[x].val == (uint32_t)temps[y].val;
5544c1b
+            case TCG_COND_NE:
5544c1b
+                return (uint32_t)temps[x].val != (uint32_t)temps[y].val;
5544c1b
+            case TCG_COND_LT:
5544c1b
+                return (int32_t)temps[x].val < (int32_t)temps[y].val;
5544c1b
+            case TCG_COND_GE:
5544c1b
+                return (int32_t)temps[x].val >= (int32_t)temps[y].val;
5544c1b
+            case TCG_COND_LE:
5544c1b
+                return (int32_t)temps[x].val <= (int32_t)temps[y].val;
5544c1b
+            case TCG_COND_GT:
5544c1b
+                return (int32_t)temps[x].val > (int32_t)temps[y].val;
5544c1b
+            case TCG_COND_LTU:
5544c1b
+                return (uint32_t)temps[x].val < (uint32_t)temps[y].val;
5544c1b
+            case TCG_COND_GEU:
5544c1b
+                return (uint32_t)temps[x].val >= (uint32_t)temps[y].val;
5544c1b
+            case TCG_COND_LEU:
5544c1b
+                return (uint32_t)temps[x].val <= (uint32_t)temps[y].val;
5544c1b
+            case TCG_COND_GTU:
5544c1b
+                return (uint32_t)temps[x].val > (uint32_t)temps[y].val;
5544c1b
+            }
5544c1b
+            break;
5544c1b
+        case 64:
5544c1b
+            switch (c) {
5544c1b
+            case TCG_COND_EQ:
5544c1b
+                return (uint64_t)temps[x].val == (uint64_t)temps[y].val;
5544c1b
+            case TCG_COND_NE:
5544c1b
+                return (uint64_t)temps[x].val != (uint64_t)temps[y].val;
5544c1b
+            case TCG_COND_LT:
5544c1b
+                return (int64_t)temps[x].val < (int64_t)temps[y].val;
5544c1b
+            case TCG_COND_GE:
5544c1b
+                return (int64_t)temps[x].val >= (int64_t)temps[y].val;
5544c1b
+            case TCG_COND_LE:
5544c1b
+                return (int64_t)temps[x].val <= (int64_t)temps[y].val;
5544c1b
+            case TCG_COND_GT:
5544c1b
+                return (int64_t)temps[x].val > (int64_t)temps[y].val;
5544c1b
+            case TCG_COND_LTU:
5544c1b
+                return (uint64_t)temps[x].val < (uint64_t)temps[y].val;
5544c1b
+            case TCG_COND_GEU:
5544c1b
+                return (uint64_t)temps[x].val >= (uint64_t)temps[y].val;
5544c1b
+            case TCG_COND_LEU:
5544c1b
+                return (uint64_t)temps[x].val <= (uint64_t)temps[y].val;
5544c1b
+            case TCG_COND_GTU:
5544c1b
+                return (uint64_t)temps[x].val > (uint64_t)temps[y].val;
5544c1b
+            }
5544c1b
+            break;
5544c1b
+        }
5544c1b
+    } else if (temps_are_copies(x, y)) {
5544c1b
         switch (c) {
5544c1b
-        case TCG_COND_EQ:
5544c1b
-            return (uint32_t)x == (uint32_t)y;
5544c1b
-        case TCG_COND_NE:
5544c1b
-            return (uint32_t)x != (uint32_t)y;
5544c1b
-        case TCG_COND_LT:
5544c1b
-            return (int32_t)x < (int32_t)y;
5544c1b
-        case TCG_COND_GE:
5544c1b
-            return (int32_t)x >= (int32_t)y;
5544c1b
-        case TCG_COND_LE:
5544c1b
-            return (int32_t)x <= (int32_t)y;
5544c1b
         case TCG_COND_GT:
5544c1b
-            return (int32_t)x > (int32_t)y;
5544c1b
         case TCG_COND_LTU:
5544c1b
-            return (uint32_t)x < (uint32_t)y;
5544c1b
-        case TCG_COND_GEU:
5544c1b
-            return (uint32_t)x >= (uint32_t)y;
5544c1b
-        case TCG_COND_LEU:
5544c1b
-            return (uint32_t)x <= (uint32_t)y;
5544c1b
+        case TCG_COND_LT:
5544c1b
         case TCG_COND_GTU:
5544c1b
-            return (uint32_t)x > (uint32_t)y;
5544c1b
-        }
5544c1b
-        break;
5544c1b
-    case 64:
5544c1b
-        switch (c) {
5544c1b
-        case TCG_COND_EQ:
5544c1b
-            return (uint64_t)x == (uint64_t)y;
5544c1b
         case TCG_COND_NE:
5544c1b
-            return (uint64_t)x != (uint64_t)y;
5544c1b
-        case TCG_COND_LT:
5544c1b
-            return (int64_t)x < (int64_t)y;
5544c1b
+            return 0;
5544c1b
         case TCG_COND_GE:
5544c1b
-            return (int64_t)x >= (int64_t)y;
5544c1b
+        case TCG_COND_GEU:
5544c1b
         case TCG_COND_LE:
5544c1b
-            return (int64_t)x <= (int64_t)y;
5544c1b
-        case TCG_COND_GT:
5544c1b
-            return (int64_t)x > (int64_t)y;
5544c1b
+        case TCG_COND_LEU:
5544c1b
+        case TCG_COND_EQ:
5544c1b
+            return 1;
5544c1b
+        }
5544c1b
+    } else if (temps[y].state == TCG_TEMP_CONST && temps[y].val == 0) {
5544c1b
+        switch (c) {
5544c1b
         case TCG_COND_LTU:
5544c1b
-            return (uint64_t)x < (uint64_t)y;
5544c1b
+            return 0;
5544c1b
         case TCG_COND_GEU:
5544c1b
-            return (uint64_t)x >= (uint64_t)y;
5544c1b
-        case TCG_COND_LEU:
5544c1b
-            return (uint64_t)x <= (uint64_t)y;
5544c1b
-        case TCG_COND_GTU:
5544c1b
-            return (uint64_t)x > (uint64_t)y;
5544c1b
+            return 1;
5544c1b
+        default:
5544c1b
+            return 2;
5544c1b
         }
5544c1b
-        break;
5544c1b
+    } else {
5544c1b
+        return 2;
5544c1b
     }
5544c1b
 
5544c1b
     fprintf(stderr,
5544c1b
@@ -636,11 +666,9 @@ static TCGArg *tcg_constant_folding(TCGContext *s, uint16_t *tcg_opc_ptr,
5544c1b
             args += 3;
5544c1b
             break;
5544c1b
         CASE_OP_32_64(setcond):
5544c1b
-            if (temps[args[1]].state == TCG_TEMP_CONST
5544c1b
-                && temps[args[2]].state == TCG_TEMP_CONST) {
5544c1b
+            tmp = do_constant_folding_cond(op, args[1], args[2], args[3]);
5544c1b
+            if (tmp != 2) {
5544c1b
                 gen_opc_buf[op_index] = op_to_movi(op);
5544c1b
-                tmp = do_constant_folding_cond(op, temps[args[1]].val,
5544c1b
-                                               temps[args[2]].val, args[3]);
5544c1b
                 tcg_opt_gen_movi(gen_args, args[0], tmp);
5544c1b
                 gen_args += 2;
5544c1b
             } else {
5544c1b
@@ -654,10 +682,9 @@ static TCGArg *tcg_constant_folding(TCGContext *s, uint16_t *tcg_opc_ptr,
5544c1b
             args += 4;
5544c1b
             break;
5544c1b
         CASE_OP_32_64(brcond):
5544c1b
-            if (temps[args[0]].state == TCG_TEMP_CONST
5544c1b
-                && temps[args[1]].state == TCG_TEMP_CONST) {
5544c1b
-                if (do_constant_folding_cond(op, temps[args[0]].val,
5544c1b
-                                             temps[args[1]].val, args[2])) {
5544c1b
+            tmp = do_constant_folding_cond(op, args[0], args[1], args[2]);
5544c1b
+            if (tmp != 2) {
5544c1b
+                if (tmp) {
5544c1b
                     memset(temps, 0, nb_temps * sizeof(struct tcg_temp_info));
5544c1b
                     gen_opc_buf[op_index] = INDEX_op_br;
5544c1b
                     gen_args[0] = args[3];
5544c1b
@@ -677,10 +704,8 @@ static TCGArg *tcg_constant_folding(TCGContext *s, uint16_t *tcg_opc_ptr,
5544c1b
             args += 4;
5544c1b
             break;
5544c1b
         CASE_OP_32_64(movcond):
5544c1b
-            if (temps[args[1]].state == TCG_TEMP_CONST
5544c1b
-                && temps[args[2]].state == TCG_TEMP_CONST) {
5544c1b
-                tmp = do_constant_folding_cond(op, temps[args[1]].val,
5544c1b
-                                               temps[args[2]].val, args[5]);
5544c1b
+            tmp = do_constant_folding_cond(op, args[1], args[2], args[5]);
5544c1b
+            if (tmp != 2) {
5544c1b
                 if (temps_are_copies(args[0], args[4-tmp])) {
5544c1b
                     gen_opc_buf[op_index] = INDEX_op_nop;
5544c1b
                 } else if (temps[args[4-tmp]].state == TCG_TEMP_CONST) {