- first experimental approach of flag modeling in add/adc
[libfirm] / ir / ir / iropt.c
index f4e199c..a526a41 100644 (file)
@@ -191,19 +191,25 @@ static tarval *computed_value_Minus(ir_node *n) {
 static tarval *computed_value_Mul(ir_node *n) {
        ir_node *a = get_Mul_left(n);
        ir_node *b = get_Mul_right(n);
+       ir_mode *mode;
 
        tarval *ta = value_of(a);
        tarval *tb = value_of(b);
 
-       if ((ta != tarval_bad) && (tb != tarval_bad) && (get_irn_mode(a) == get_irn_mode(b))) {
+       mode = get_irn_mode(n);
+       if (mode != get_irn_mode(a)) {
+               /* n * n = 2n bit multiplication */
+               ta = tarval_convert_to(ta, mode);
+               tb = tarval_convert_to(tb, mode);
+       }
+
+       if (ta != tarval_bad && tb != tarval_bad) {
                return tarval_mul(ta, tb);
        } else {
-       /* a*0 = 0 or 0*b = 0:
-          calls computed_value recursive and returns the 0 with proper
-          mode. */
-               if ((ta != tarval_bad) && (ta == get_mode_null(get_tarval_mode(ta))))
+               /* a*0 = 0 or 0*b = 0 */
+               if (ta == get_mode_null(mode))
                        return ta;
-               if ((tb != tarval_bad) && (tb == get_mode_null(get_tarval_mode(tb))))
+               if (tb == get_mode_null(mode))
                        return tb;
        }
        return tarval_bad;
@@ -1008,15 +1014,19 @@ static ir_node *equivalent_node_idempotent_unop(ir_node *n) {
 static ir_node *equivalent_node_Mul(ir_node *n) {
        ir_node *oldn = n;
        ir_node *a = get_Mul_left(n);
-       ir_node *b = get_Mul_right(n);
 
-       /* Mul is commutative and has again an other neutral element. */
-       if (classify_tarval(value_of(a)) == TV_CLASSIFY_ONE) {
-               n = b;
-               DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
-       } else if (classify_tarval(value_of(b)) == TV_CLASSIFY_ONE) {
-               n = a;
-               DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
+       /* we can handle here only the n * n = n bit cases */
+       if (get_irn_mode(n) == get_irn_mode(a)) {
+               ir_node *b = get_Mul_right(n);
+
+               /* Mul is commutative and has again an other neutral element. */
+               if (classify_tarval(value_of(a)) == TV_CLASSIFY_ONE) {
+                       n = b;
+                       DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
+               } else if (classify_tarval(value_of(b)) == TV_CLASSIFY_ONE) {
+                       n = a;
+                       DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
+               }
        }
        return n;
 }  /* equivalent_node_Mul */
@@ -1873,18 +1883,17 @@ static ir_node *transform_node_AddSub(ir_node *n) {
     return c;                                               \
   }
 
-#define HANDLE_UNOP_PHI(op,a,c)                 \
-  c = NULL;                                     \
-  if (is_const_Phi(a)) {                        \
-    /* check for Op(Phi) */                     \
-    c = apply_unop_on_phi(a, op);               \
-  }                                             \
-  if (c) {                                      \
-    DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
-    return c;                                   \
+#define HANDLE_UNOP_PHI(op,a,c)                   \
+  c = NULL;                                       \
+  if (is_const_Phi(a)) {                          \
+    /* check for Op(Phi) */                       \
+    c = apply_unop_on_phi(a, op);                 \
+    if (c) {                                      \
+      DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
+      return c;                                   \
+    }                                             \
   }
 
-
 /**
  * Do the AddSub optimization, then Transform
  *   Constant folding on Phi
@@ -1912,9 +1921,8 @@ static ir_node *transform_node_Add(ir_node *n) {
                return n;
 
        if (mode_is_num(mode)) {
-#if 0
                /* the following code leads to endless recursion when Mul are replaced by a simple instruction chain */
-               if (a == b && mode_is_int(mode)) {
+               if (!get_opt_arch_dep_running() && a == b && mode_is_int(mode)) {
                        ir_node *block = get_irn_n(n, -1);
 
                        n = new_rd_Mul(
@@ -1925,9 +1933,9 @@ static ir_node *transform_node_Add(ir_node *n) {
                                new_r_Const_long(current_ir_graph, block, mode, 2),
                                mode);
                        DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_A);
-               } else
-#endif
-               if (get_irn_op(a) == op_Minus) {
+                       return n;
+               }
+               if (is_Minus(a)) {
                        n = new_rd_Sub(
                                        get_irn_dbg_info(n),
                                        current_ir_graph,
@@ -1936,7 +1944,9 @@ static ir_node *transform_node_Add(ir_node *n) {
                                        get_Minus_op(a),
                                        mode);
                        DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
-               } else if (get_irn_op(b) == op_Minus) {
+                       return n;
+               }
+               if (is_Minus(b)) {
                        n = new_rd_Sub(
                                        get_irn_dbg_info(n),
                                        current_ir_graph,
@@ -1945,77 +1955,85 @@ static ir_node *transform_node_Add(ir_node *n) {
                                        get_Minus_op(b),
                                        mode);
                        DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
+                       return n;
                }
-               /* do NOT execute this code if reassociation is enabled, it does the inverse! */
-               else if (!get_opt_reassociation() && get_irn_op(a) == op_Mul) {
-                       ir_node *ma = get_Mul_left(a);
-                       ir_node *mb = get_Mul_right(a);
-
-                       if (b == ma) {
-                               ir_node *blk = get_irn_n(n, -1);
-                               n = new_rd_Mul(
-                                               get_irn_dbg_info(n), current_ir_graph, blk,
-                                               ma,
-                                               new_rd_Add(
-                                                       get_irn_dbg_info(n), current_ir_graph, blk,
-                                                       mb,
-                                                       new_r_Const_long(current_ir_graph, blk, mode, 1),
-                                                       mode),
-                                               mode);
-                               DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
-                       } else if (b == mb) {
-                               ir_node *blk = get_irn_n(n, -1);
-                               n = new_rd_Mul(
-                                               get_irn_dbg_info(n), current_ir_graph, blk,
-                                               mb,
-                                               new_rd_Add(
+               if (! get_opt_reassociation()) {
+                       /* do NOT execute this code if reassociation is enabled, it does the inverse! */
+                       if (is_Mul(a)) {
+                               ir_node *ma = get_Mul_left(a);
+                               ir_node *mb = get_Mul_right(a);
+
+                               if (b == ma) {
+                                       ir_node *blk = get_irn_n(n, -1);
+                                       n = new_rd_Mul(
                                                        get_irn_dbg_info(n), current_ir_graph, blk,
                                                        ma,
-                                                       new_r_Const_long(current_ir_graph, blk, mode, 1),
-                                                       mode),
-                                               mode);
-                               DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
-                       }
-               }
-               /* do NOT execute this code if reassociation is enabled, it does the inverse! */
-               else if (!get_opt_reassociation() && get_irn_op(b) == op_Mul) {
-                       ir_node *ma = get_Mul_left(b);
-                       ir_node *mb = get_Mul_right(b);
-
-                       if (a == ma) {
-                               ir_node *blk = get_irn_n(n, -1);
-                               n = new_rd_Mul(
-                                               get_irn_dbg_info(n), current_ir_graph, blk,
-                                               ma,
-                                               new_rd_Add(
+                                                       new_rd_Add(
+                                                               get_irn_dbg_info(n), current_ir_graph, blk,
+                                                               mb,
+                                                               new_r_Const_long(current_ir_graph, blk, mode, 1),
+                                                               mode),
+                                                       mode);
+                                       DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
+                                       return n;
+                               } else if (b == mb) {
+                                       ir_node *blk = get_irn_n(n, -1);
+                                       n = new_rd_Mul(
                                                        get_irn_dbg_info(n), current_ir_graph, blk,
                                                        mb,
-                                                       new_r_Const_long(current_ir_graph, blk, mode, 1),
-                                                       mode),
-                                               mode);
-                               DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
-                       } else if (a == mb) {
-                               ir_node *blk = get_irn_n(n, -1);
-                               n = new_rd_Mul(
-                                               get_irn_dbg_info(n), current_ir_graph, blk,
-                                               mb,
-                                               new_rd_Add(
+                                                       new_rd_Add(
+                                                               get_irn_dbg_info(n), current_ir_graph, blk,
+                                                               ma,
+                                                               new_r_Const_long(current_ir_graph, blk, mode, 1),
+                                                               mode),
+                                                       mode);
+                                       DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
+                                       return n;
+                               }
+                       }
+                       if (is_Mul(b)) {
+                               ir_node *ma = get_Mul_left(b);
+                               ir_node *mb = get_Mul_right(b);
+
+                               if (a == ma) {
+                                       ir_node *blk = get_irn_n(n, -1);
+                                       n = new_rd_Mul(
                                                        get_irn_dbg_info(n), current_ir_graph, blk,
                                                        ma,
-                                                       new_r_Const_long(current_ir_graph, blk, mode, 1),
-                                                       mode),
-                                               mode);
-                               DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
+                                                       new_rd_Add(
+                                                               get_irn_dbg_info(n), current_ir_graph, blk,
+                                                               mb,
+                                                               new_r_Const_long(current_ir_graph, blk, mode, 1),
+                                                               mode),
+                                                       mode);
+                                       DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
+                                       return n;
+                               }
+                               if (a == mb) {
+                                       ir_node *blk = get_irn_n(n, -1);
+                                       n = new_rd_Mul(
+                                                       get_irn_dbg_info(n), current_ir_graph, blk,
+                                                       mb,
+                                                       new_rd_Add(
+                                                               get_irn_dbg_info(n), current_ir_graph, blk,
+                                                               ma,
+                                                               new_r_Const_long(current_ir_graph, blk, mode, 1),
+                                                               mode),
+                                                       mode);
+                                       DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
+                                       return n;
+                               }
                        }
                }
                /* Here we rely on constants be on the RIGHT side */
-               else if (get_mode_arithmetic(mode) == irma_twos_complement &&
+               if (get_mode_arithmetic(mode) == irma_twos_complement &&
                         is_Not(a) && classify_Const(b) == CNST_ONE) {
                        /* ~x + 1 = -x */
                        ir_node *op = get_Not_op(a);
                        ir_node *blk = get_irn_n(n, -1);
                        n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, op, mode);
                        DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_PLUS_1);
+                       return n;
                }
        }
        return n;
@@ -2225,6 +2243,47 @@ restart:
        return n;
 }  /* transform_node_Sub */
 
+/**
+ * Several transformation done on n*n=2n bits mul.
+ * These transformations must be done here because new nodes may be produced.
+ */
+static ir_node *transform_node_Mul2n(ir_node *n, ir_mode *mode) {
+       ir_node *oldn = n;
+       ir_node *a = get_Mul_left(n);
+       ir_node *b = get_Mul_right(n);
+       tarval *ta = value_of(a);
+       tarval *tb = value_of(b);
+       ir_mode *smode = get_irn_mode(a);
+
+       if (ta == get_mode_one(smode)) {
+               ir_node *blk = get_irn_n(n, -1);
+               n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, b, mode);
+               DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
+               return n;
+       }
+       else if (ta == get_mode_minus_one(smode)) {
+               ir_node *blk = get_irn_n(n, -1);
+               n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, b, smode);
+               n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, n, mode);
+               DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
+               return n;
+       }
+       if (tb == get_mode_one(smode)) {
+               ir_node *blk = get_irn_n(a, -1);
+               n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, a, mode);
+               DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
+               return n;
+       }
+       else if (tb == get_mode_minus_one(smode)) {
+               ir_node *blk = get_irn_n(n, -1);
+               n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, a, smode);
+               n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, n, mode);
+               DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
+               return n;
+       }
+       return n;
+}
+
 /**
  * Transform Mul(a,-1) into -a.
  * Do constant evaluation of Phi nodes.
@@ -2232,13 +2291,15 @@ restart:
  */
 static ir_node *transform_node_Mul(ir_node *n) {
        ir_node *c, *oldn = n;
+       ir_mode *mode = get_irn_mode(n);
        ir_node *a = get_Mul_left(n);
        ir_node *b = get_Mul_right(n);
-       ir_mode *mode;
+
+       if (mode != get_irn_mode(a))
+               return transform_node_Mul2n(n, mode);
 
        HANDLE_BINOP_PHI(tarval_mul, a,b,c);
 
-       mode = get_irn_mode(n);
        if (mode_is_signed(mode)) {
                ir_node *r = NULL;