Enqueue Phi nodes to reach fixpoint.
[libfirm] / ir / ir / iropt.c
index 0351fc7..ba9a141 100644 (file)
@@ -4623,7 +4623,7 @@ static ir_node *transform_node_Proj(ir_node *proj)
 }  /* transform_node_Proj */
 
 /**
- * Test wether a block is unreachable
+ * Test whether a block is unreachable
  * Note: That this only returns true when
  * IR_GRAPH_STATE_OPTIMIZE_UNREACHABLE_CODE is set.
  * This is important, as you easily end up producing invalid constructs in the
@@ -4692,18 +4692,26 @@ static ir_node *transform_node_Phi(ir_node *phi)
                if (n > 0) {
                        ir_node **in;
                        ir_node  *new_phi;
+                       bool      has_pin = false;
 
                        NEW_ARR_A(ir_node *, in, n);
 
                        for (i = 0; i < n; ++i) {
                                ir_node *pred = get_irn_n(phi, i);
 
-                               if (!is_Pin(pred))
+                               if (is_Pin(pred)) {
+                                       in[i]   = get_Pin_op(pred);
+                                       has_pin = true;
+                               } else if (is_Bad(pred)) {
+                                       in[i] = pred;
+                               } else {
                                        return phi;
-
-                               in[i] = get_Pin_op(pred);
+                               }
                        }
 
+                       if (!has_pin)
+                               return phi;
+
                        /* Move the Pin nodes "behind" the Phi. */
                        block   = get_irn_n(phi, -1);
                        new_phi = new_r_Phi(block, n, in, mode_M);
@@ -4719,6 +4727,7 @@ static ir_node *transform_node_Phi(ir_node *phi)
                        ir_node    *pred = get_irn_n(phi, 0);
                        ir_node    *bound, *new_phi, *block, **in;
                        ir_relation relation;
+                       bool        has_confirm = false;
 
                        if (! is_Confirm(pred))
                                return phi;
@@ -4732,12 +4741,21 @@ static ir_node *transform_node_Phi(ir_node *phi)
                        for (i = 1; i < n; ++i) {
                                pred = get_irn_n(phi, i);
 
-                               if (! is_Confirm(pred) ||
-                                       get_Confirm_bound(pred) != bound ||
-                                       get_Confirm_relation(pred) != relation)
+                               if (is_Confirm(pred) &&
+                                               get_Confirm_bound(pred) == bound &&
+                                               get_Confirm_relation(pred) == relation) {
+                                       in[i]       = get_Confirm_value(pred);
+                                       has_confirm = true;
+                               } else if (is_Bad(pred)) {
+                                       in[i] = pred;
+                               } else {
                                        return phi;
-                               in[i] = get_Confirm_value(pred);
+                               }
                        }
+
+                       if (!has_confirm)
+                               return phi;
+
                        /* move the Confirm nodes "behind" the Phi */
                        block = get_irn_n(phi, -1);
                        new_phi = new_r_Phi(block, n, in, get_irn_mode(phi));
@@ -4954,6 +4972,23 @@ static bool is_cmp_unequal(const ir_node *node)
        return false;
 }
 
+/**
+ * returns true for Cmp(x == 0) or Cmp(x != 0)
+ */
+static bool is_cmp_equality_zero(const ir_node *node)
+{
+       ir_relation relation;
+       ir_node    *right    = get_Cmp_right(node);
+
+       if (!is_Const(right) || !is_Const_null(right))
+               return false;
+       relation = get_Cmp_relation(node);
+       return relation == ir_relation_equal
+               || relation == ir_relation_less_greater
+               || (!mode_is_signed(get_irn_mode(right))
+                   && relation == ir_relation_greater);
+}
+
 /**
  * Transform an Or.
  */
@@ -5532,8 +5567,8 @@ static const ir_node *skip_upconv(const ir_node *node)
        return node;
 }
 
-int ir_mux_is_abs(const ir_node *sel, const ir_node *mux_true,
-                  const ir_node *mux_false)
+int ir_mux_is_abs(const ir_node *sel, const ir_node *mux_false,
+                  const ir_node *mux_true)
 {
        ir_node    *cmp_left;
        ir_node    *cmp_right;
@@ -5589,13 +5624,50 @@ int ir_mux_is_abs(const ir_node *sel, const ir_node *mux_true,
        return 0;
 }
 
-ir_node *ir_get_abs_op(const ir_node *sel, ir_node *mux_true,
-                       ir_node *mux_false)
+ir_node *ir_get_abs_op(const ir_node *sel, ir_node *mux_false,
+                       ir_node *mux_true)
 {
        ir_node *cmp_left = get_Cmp_left(sel);
        return cmp_left == skip_upconv(mux_false) ? mux_false : mux_true;
 }
 
+bool ir_is_optimizable_mux(const ir_node *sel, const ir_node *mux_false,
+                           const ir_node *mux_true)
+{
+       /* this code should return true each time transform_node_Mux would
+        * optimize the Mux completely away */
+
+       ir_mode *mode = get_irn_mode(mux_false);
+       if (get_mode_arithmetic(mode) == irma_twos_complement
+           && ir_mux_is_abs(sel, mux_false, mux_true))
+           return true;
+
+       if (is_Cmp(sel) && mode_is_int(mode) && is_cmp_equality_zero(sel)) {
+               const ir_node *cmp_r = get_Cmp_right(sel);
+               const ir_node *cmp_l = get_Cmp_left(sel);
+               const ir_node *f     = mux_false;
+               const ir_node *t     = mux_true;
+
+               if (is_Const(t) && is_Const_null(t)) {
+                       t = mux_false;
+                       f = mux_true;
+               }
+
+               if (is_And(cmp_l) && f == cmp_r) {
+                       ir_node *and_r = get_And_right(cmp_l);
+                       ir_node *and_l;
+
+                       if (and_r == t && is_single_bit(and_r))
+                               return true;
+                       and_l = get_And_left(cmp_l);
+                       if (and_l == t && is_single_bit(and_l))
+                               return true;
+               }
+       }
+
+       return false;
+}
+
 /**
  * Optimize a Mux into some simpler cases.
  */
@@ -5610,11 +5682,11 @@ static ir_node *transform_node_Mux(ir_node *n)
 
        /* implement integer abs: abs(x) = x^(x >>s 31) - (x >>s 31) */
        if (get_mode_arithmetic(mode) == irma_twos_complement) {
-               int abs = ir_mux_is_abs(sel, t, f);
+               int abs = ir_mux_is_abs(sel, f, t);
                if (abs != 0) {
                        dbg_info *dbgi       = get_irn_dbg_info(n);
                        ir_node  *block      = get_nodes_block(n);
-                       ir_node  *op         = ir_get_abs_op(sel, t, f);
+                       ir_node  *op         = ir_get_abs_op(sel, f, t);
                        int       bits       = get_mode_size_bits(mode);
                        ir_node  *shiftconst = new_r_Const_long(irg, mode_Iu, bits-1);
                        ir_node  *sext       = new_rd_Shrs(dbgi, block, op, shiftconst, mode);
@@ -5765,73 +5837,42 @@ static ir_node *transform_node_Mux(ir_node *n)
                }
        }
 
-       if (is_Cmp(sel)) {
+       if (is_Cmp(sel) && mode_is_int(mode) && is_cmp_equality_zero(sel)) {
+               ir_relation relation = get_Cmp_relation(sel);
                ir_node    *cmp_r    = get_Cmp_right(sel);
-               if (is_Const(cmp_r) && is_Const_null(cmp_r)) {
-                       ir_node *block = get_nodes_block(n);
-                       ir_node *cmp_l = get_Cmp_left(sel);
-
-                       if (mode_is_int(mode)) {
-                               ir_relation relation = get_Cmp_relation(sel);
-                               /* integer only */
-                               if ((relation == ir_relation_less_greater || relation == ir_relation_equal) && is_And(cmp_l)) {
-                                       /* Mux((a & b) != 0, c, 0) */
-                                       ir_node *and_r = get_And_right(cmp_l);
-                                       ir_node *and_l;
-
-                                       if (and_r == t && f == cmp_r) {
-                                               if (is_Const(t) && tarval_is_single_bit(get_Const_tarval(t))) {
-                                                       if (relation == ir_relation_less_greater) {
-                                                               /* Mux((a & 2^C) != 0, 2^C, 0) == a & 2^c */
-                                                               n = cmp_l;
-                                                               DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
-                                                       } else {
-                                                               /* Mux((a & 2^C) == 0, 2^C, 0) == (a & 2^c) xor (2^c) */
-                                                               n = new_rd_Eor(get_irn_dbg_info(n),
-                                                                       block, cmp_l, t, mode);
-                                                               DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
-                                                       }
-                                                       return n;
-                                               }
-                                       }
-                                       if (is_Shl(and_r)) {
-                                               ir_node *shl_l = get_Shl_left(and_r);
-                                               if (is_Const(shl_l) && is_Const_one(shl_l)) {
-                                                       if (and_r == t && f == cmp_r) {
-                                                               if (relation == ir_relation_less_greater) {
-                                                                       /* (a & (1 << n)) != 0, (1 << n), 0) == a & (1<<n) */
-                                                                       n = cmp_l;
-                                                                       DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
-                                                               } else {
-                                                                       /* (a & (1 << n)) == 0, (1 << n), 0) == (a & (1<<n)) xor (1<<n) */
-                                                                       n = new_rd_Eor(get_irn_dbg_info(n),
-                                                                               block, cmp_l, t, mode);
-                                                                       DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
-                                                               }
-                                                               return n;
-                                                       }
-                                               }
-                                       }
-                                       and_l = get_And_left(cmp_l);
-                                       if (is_Shl(and_l)) {
-                                               ir_node *shl_l = get_Shl_left(and_l);
-                                               if (is_Const(shl_l) && is_Const_one(shl_l)) {
-                                                       if (and_l == t && f == cmp_r) {
-                                                               if (relation == ir_relation_less_greater) {
-                                                                       /* ((1 << n) & a) != 0, (1 << n), 0) */
-                                                                       n = cmp_l;
-                                                                       DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
-                                                               } else {
-                                                                       /* ((1 << n) & a) == 0, (1 << n), 0) */
-                                                                       n = new_rd_Eor(get_irn_dbg_info(n),
-                                                                               block, cmp_l, t, mode);
-                                                                       DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
-                                                               }
-                                                               return n;
-                                                       }
-                                               }
-                                       }
+               ir_node    *cmp_l    = get_Cmp_left(sel);
+               ir_node    *block    = get_nodes_block(n);
+
+               if (is_And(cmp_l) && f == cmp_r) {
+                       ir_node *and_r = get_And_right(cmp_l);
+                       ir_node *and_l;
+
+                       if (and_r == t && is_single_bit(and_r)) {
+                               if (relation == ir_relation_equal) {
+                                       /* Mux((a & (1<<n)) == 0, (1<<n), 0) == (a&(1<<n)) xor ((1<<n)) */
+                                       n = new_rd_Eor(get_irn_dbg_info(n),
+                                               block, cmp_l, t, mode);
+                                       DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
+                               } else {
+                                       /* Mux((a & (1<<n)) != 0, (1<<n), 0) == a & (1<<n) */
+                                       n = cmp_l;
+                                       DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
                                }
+                               return n;
+                       }
+                       and_l = get_And_left(cmp_l);
+                       if (and_l == t && is_single_bit(and_l)) {
+                               if (relation == ir_relation_equal) {
+                                       /* ((1 << n) & a) == 0, (1 << n), 0) */
+                                       n = new_rd_Eor(get_irn_dbg_info(n),
+                                               block, cmp_l, t, mode);
+                                       DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
+                               } else {
+                                       /* ((1 << n) & a) != 0, (1 << n), 0) */
+                                       n = cmp_l;
+                                       DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
+                               }
+                               return n;
                        }
                }
        }