Blame 0074-tcg-optimize-further-optimize-brcond-movcond-setcond.patch

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