fehler76: assertion when converting float constant to int
[libfirm] / ir / ir / iropt.c
index 92a9ec6..7d25747 100644 (file)
@@ -1912,7 +1912,8 @@ static ir_node *transform_node_Add(ir_node *n) {
                return n;
 
        if (mode_is_num(mode)) {
-               if (a == b && mode_is_int(mode)) {
+               /* the following code leads to endless recursion when Mul are replaced by a simple instruction chain */
+               if (!get_opt_arch_dep_running() && a == b && mode_is_int(mode)) {
                        ir_node *block = get_irn_n(n, -1);
 
                        n = new_rd_Mul(
@@ -1923,7 +1924,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 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,
@@ -1932,7 +1935,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,
@@ -1941,77 +1946,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;
@@ -2025,6 +2038,7 @@ static ir_node *transform_node_Add(ir_node *n) {
  *   Sub(Sub(x, y), b) -> Sub(x, Add(y,b))
  *   Sub(Add(a, x), x) -> a
  *   Sub(x, Add(x, a)) -> -a
+ *   Sub(x, Const)     -> Add(x, -Const)
  */
 static ir_node *transform_node_Sub(ir_node *n) {
        ir_mode *mode;
@@ -2045,6 +2059,33 @@ restart:
        if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
                return n;
 
+       /* Sub(a, Const) -> Add(a, -Const) */
+       if (is_Const(b) && get_irn_mode(b) != mode_P) {
+               tarval *tv = get_Const_tarval(b);
+
+               tv = tarval_neg(tv);
+               if(tv != tarval_bad) {
+                       ir_node  *cnst  = new_Const(get_irn_mode(b), tv);
+                       ir_node  *block = get_nodes_block(n);
+                       dbg_info *dbgi  = get_irn_dbg_info(n);
+                       ir_graph *irg   = get_irn_irg(n);
+                       ir_node  *add   = new_rd_Add(dbgi, irg, block, a, cnst, mode);
+
+                       return add;
+               }
+       }
+
+       /* Beware of Sub(P, P) which cannot be optimized into a simple Minus ... */
+       if (mode_is_num(mode) && mode == get_irn_mode(a) && (classify_Const(a) == CNST_NULL)) {
+               n = new_rd_Minus(
+                               get_irn_dbg_info(n),
+                               current_ir_graph,
+                               get_irn_n(n, -1),
+                               b,
+                               mode);
+               DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_0_A);
+               return n;
+       }
        if (is_Add(a)) {
                if (mode_wrap_around(mode)) {
                        ir_node *left  = get_Add_left(a);
@@ -2058,6 +2099,7 @@ restart:
                                }
                                n = right;
                                DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
+                               return n;
                        } else if (right == b) {
                                if (mode != get_irn_mode(left)) {
                                        /* This Sub is an effective Cast */
@@ -2065,9 +2107,11 @@ restart:
                                }
                                n = left;
                                DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
+                               return n;
                        }
                }
-       } else if (is_Add(b)) {
+       }
+       if (is_Add(b)) {
                if (mode_wrap_around(mode)) {
                        ir_node *left  = get_Add_left(b);
                        ir_node *right = get_Add_right(b);
@@ -2082,6 +2126,7 @@ restart:
                                        n = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), n, mode);
                                }
                                DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
+                               return n;
                        } else if (right == a) {
                                ir_mode *l_mode = get_irn_mode(left);
 
@@ -2091,9 +2136,11 @@ restart:
                                        n = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), n, mode);
                                }
                                DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
+                               return n;
                        }
                }
-       } else if (mode_is_int(mode) && is_Conv(a) && is_Conv(b)) {
+       }
+       if (mode_is_int(mode) && is_Conv(a) && is_Conv(b)) {
                ir_mode *mode = get_irn_mode(a);
 
                if (mode == get_irn_mode(b)) {
@@ -2115,18 +2162,8 @@ restart:
                        }
                }
        }
-       /* Beware of Sub(P, P) which cannot be optimized into a simple Minus ... */
-       else if (mode_is_num(mode) && mode == get_irn_mode(a) && (classify_Const(a) == CNST_NULL)) {
-               n = new_rd_Minus(
-                               get_irn_dbg_info(n),
-                               current_ir_graph,
-                               get_irn_n(n, -1),
-                               b,
-                               mode);
-               DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_0_A);
-       }
        /* do NOT execute this code if reassociation is enabled, it does the inverse! */
-       else if (get_opt_reassociation() && get_irn_op(a) == op_Mul) {
+       if (get_opt_reassociation() && get_irn_op(a) == op_Mul) {
                ir_node *ma = get_Mul_left(a);
                ir_node *mb = get_Mul_right(a);
 
@@ -2144,6 +2181,7 @@ restart:
                                                mode),
                                        mode);
                        DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
+                       return n;
                } else if (mb == b) {
                        ir_node *blk = get_irn_n(n, -1);
                        n = new_rd_Mul(
@@ -2158,8 +2196,10 @@ restart:
                                                mode),
                                        mode);
                        DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
+                       return n;
                }
-       } else if (get_irn_op(a) == op_Sub) {
+       }
+       if (is_Sub(a)) {
                ir_node *x   = get_Sub_left(a);
                ir_node *y   = get_Sub_right(a);
                ir_node *blk = get_irn_n(n, -1);
@@ -2189,6 +2229,7 @@ restart:
                set_Sub_left(n, x);
                set_Sub_right(n, add);
                DBG_OPT_ALGSIM0(n, n, FS_OPT_SUB_SUB_X_Y_Z);
+               return n;
        }
        return n;
 }  /* transform_node_Sub */
@@ -3034,7 +3075,6 @@ static ir_node *transform_node_Proj_Cmp(ir_node *proj) {
                ir_mode* mode_left   = get_irn_mode(op_left);
                ir_mode* mode_right  = get_irn_mode(op_right);
 
-               ir_fprintf(stderr, "%+F(%+F(%+F), %+F(%+F))? %d\n", n, left, op_left, right, op_right, smaller_mode(mode_left, mode));
                if (smaller_mode(mode_left, mode) && smaller_mode(mode_right, mode)) {
                        ir_graph* irg   = current_ir_graph;
                        ir_node*  block = get_nodes_block(n);
@@ -3053,7 +3093,6 @@ static ir_node *transform_node_Proj_Cmp(ir_node *proj) {
                        } else {
                                goto no_remove_conv;
                        }
-                       ir_fprintf(stderr, "%+F(%+F(%+F), %+F(%+F)) -> %+F(%+F, %+F)\n", n, left, op_left, right, op_right, n, new_left, new_right);
                        left  = new_left;
                        right = new_right;
                        set_Cmp_left( n, left);
@@ -4488,6 +4527,8 @@ ir_node *optimize_node(ir_node *n) {
        if (get_opt_constant_folding()) {
                /* neither constants nor Tuple values can be evaluated */
                if (iro != iro_Const && (get_irn_mode(n) != mode_T)) {
+                       unsigned fp_model = get_irg_fp_model(current_ir_graph);
+                       int old_fp_mode = tarval_enable_fp_ops((fp_model & fp_strict_algebraic) == 0);
                        /* try to evaluate */
                        tv = computed_value(n);
                        if (tv != tarval_bad) {
@@ -4525,8 +4566,10 @@ ir_node *optimize_node(ir_node *n) {
                                if (old_tp && get_type_mode(old_tp) == get_tarval_mode (tv))
                                        set_Const_type(nw, old_tp);
                                DBG_OPT_CSTEVAL(oldn, nw);
+                               tarval_enable_fp_ops(old_fp_mode);
                                return nw;
                        }
+                       tarval_enable_fp_ops(old_fp_mode);
                }
        }
 
@@ -4595,6 +4638,8 @@ ir_node *optimize_in_place_2(ir_node *n) {
        if (get_opt_constant_folding()) {
                /* neither constants nor Tuple values can be evaluated */
                if (iro != iro_Const && get_irn_mode(n) != mode_T) {
+                       unsigned fp_model = get_irg_fp_model(current_ir_graph);
+                       int old_fp_mode = tarval_enable_fp_ops((fp_model & fp_strict_algebraic) == 0);
                        /* try to evaluate */
                        tv = computed_value(n);
                        if (tv != tarval_bad) {
@@ -4614,8 +4659,10 @@ ir_node *optimize_in_place_2(ir_node *n) {
                                        set_Const_type(n, old_tp);
 
                                DBG_OPT_CSTEVAL(oldn, n);
+                               tarval_enable_fp_ops(old_fp_mode);
                                return n;
                        }
+                       tarval_enable_fp_ops(old_fp_mode);
                }
        }