simplify transform_node_Mux by using is_single_bit
[libfirm] / ir / ir / iropt.c
index 4404bd5..ad965bd 100644 (file)
@@ -576,13 +576,7 @@ ir_relation ir_get_possible_cmp_relations(const ir_node *left,
        return possible;
 }
 
-/**
- * Return the value of a Cmp.
- *
- * The basic idea here is to determine which relations are possible and which
- * one are definitely impossible.
- */
-static ir_tarval *computed_value_Cmp(const ir_node *cmp)
+static ir_tarval *compute_cmp(const ir_node *cmp)
 {
        ir_node    *left     = get_Cmp_left(cmp);
        ir_node    *right    = get_Cmp_right(cmp);
@@ -599,6 +593,21 @@ static ir_tarval *computed_value_Cmp(const ir_node *cmp)
        return computed_value_Cmp_Confirm(cmp, left, right, relation);
 }
 
+/**
+ * Return the value of a Cmp.
+ *
+ * The basic idea here is to determine which relations are possible and which
+ * one are definitely impossible.
+ */
+static ir_tarval *computed_value_Cmp(const ir_node *cmp)
+{
+       /* we can't construct Constb after lowering mode_b nodes */
+       if (is_irg_state(get_irn_irg(cmp), IR_GRAPH_STATE_MODEB_LOWERED))
+               return tarval_bad;
+
+       return compute_cmp(cmp);
+}
+
 /**
  * Calculate the value of an integer Div.
  * Special case: 0 / b
@@ -1451,6 +1460,12 @@ static ir_node *equivalent_node_Mux(ir_node *n)
        ir_node   *n_t, *n_f;
        ir_tarval *ts = value_of(sel);
 
+       if (ts == tarval_bad && is_Cmp(sel)) {
+               /* try again with a direct call to compute_cmp, as we don't care
+                * about the MODEB_LOWERED flag here */
+               ts = compute_cmp(sel);
+       }
+
        /* Mux(true, f, t) == t */
        if (ts == tarval_b_true) {
                n = get_Mux_true(n);
@@ -2781,14 +2796,25 @@ static ir_node *transform_node_Cond(ir_node *n)
 {
 
        ir_node   *a   = get_Cond_selector(n);
-       ir_tarval *ta  = value_of(a);
        ir_graph  *irg = get_irn_irg(n);
+       ir_tarval *ta;
        ir_node   *jmp;
 
        /* we need block info which is not available in floating irgs */
        if (get_irg_pinned(irg) == op_pin_state_floats)
                return n;
 
+       /* we do not handle switches here */
+       if (get_irn_mode(a) != mode_b)
+               return n;
+
+       ta = value_of(a);
+       if (ta == tarval_bad && is_Cmp(a)) {
+               /* try again with a direct call to compute_cmp, as we don't care
+                * about the MODEB_LOWERED flag here */
+               ta = compute_cmp(a);
+       }
+
        if (ta != tarval_bad && get_irn_mode(a) == mode_b) {
                /* It's a boolean Cond, branching on a boolean constant.
                   Replace it by a tuple (Bad, Jmp) or (Jmp, Bad) */
@@ -4207,7 +4233,7 @@ static ir_node *transform_node_Cmp(ir_node *n)
                        }
 
                        /* for integer modes, we have more */
-                       if (mode_is_int(mode)) {
+                       if (mode_is_int(mode) && !is_Const(left)) {
                                /* c > 0 : a < c  ==>  a <= (c-1)    a >= c  ==>  a > (c-1) */
                                if ((relation == ir_relation_less || relation == ir_relation_greater_equal) &&
                                        tarval_cmp(tv, get_mode_null(mode)) == ir_relation_greater) {
@@ -4666,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);
@@ -4693,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;
@@ -4706,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));
@@ -4928,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.
  */
@@ -5739,73 +5800,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;
                        }
                }
        }
@@ -6108,6 +6138,14 @@ static ir_op_ops *firm_set_default_transform_node(ir_opcode code, ir_op_ops *ops
     in a graph. */
 #define N_IR_NODES 512
 
+/** Compares two exception attributes */
+static int node_cmp_exception(const ir_node *a, const ir_node *b)
+{
+       const except_attr *ea = &a->attr.except;
+       const except_attr *eb = &b->attr.except;
+       return ea->pin_state != eb->pin_state;
+}
+
 /** Compares the attributes of two Const nodes. */
 static int node_cmp_attr_Const(const ir_node *a, const ir_node *b)
 {
@@ -6125,7 +6163,9 @@ static int node_cmp_attr_Alloc(const ir_node *a, const ir_node *b)
 {
        const alloc_attr *pa = &a->attr.alloc;
        const alloc_attr *pb = &b->attr.alloc;
-       return (pa->where != pb->where) || (pa->type != pb->type);
+       if (pa->where != pb->where || pa->type != pb->type)
+               return 1;
+       return node_cmp_exception(a, b);
 }
 
 /** Compares the attributes of two Free nodes. */
@@ -6150,8 +6190,9 @@ static int node_cmp_attr_Call(const ir_node *a, const ir_node *b)
 {
        const call_attr *pa = &a->attr.call;
        const call_attr *pb = &b->attr.call;
-       return (pa->type != pb->type)
-               || (pa->tail_call != pb->tail_call);
+       if (pa->type != pb->type || pa->tail_call != pb->tail_call)
+               return 1;
+       return node_cmp_exception(a, b);
 }
 
 /** Compares the attributes of two Sel nodes. */
@@ -6196,8 +6237,9 @@ static int node_cmp_attr_Load(const ir_node *a, const ir_node *b)
        /* do not CSE Loads with different alignment. Be conservative. */
        if (get_Load_unaligned(a) != get_Load_unaligned(b))
                return 1;
-
-       return get_Load_mode(a) != get_Load_mode(b);
+       if (get_Load_mode(a) != get_Load_mode(b))
+               return 1;
+       return node_cmp_exception(a, b);
 }
 
 /** Compares the attributes of two Store nodes. */
@@ -6206,31 +6248,34 @@ static int node_cmp_attr_Store(const ir_node *a, const ir_node *b)
        /* do not CSE Stores with different alignment. Be conservative. */
        if (get_Store_unaligned(a) != get_Store_unaligned(b))
                return 1;
-
        /* NEVER do CSE on volatile Stores */
-       return (get_Store_volatility(a) == volatility_is_volatile ||
-               get_Store_volatility(b) == volatility_is_volatile);
+       if (get_Store_volatility(a) == volatility_is_volatile ||
+           get_Store_volatility(b) == volatility_is_volatile)
+               return 1;
+       return node_cmp_exception(a, b);
 }
 
-/** Compares two exception attributes */
-static int node_cmp_exception(const ir_node *a, const ir_node *b)
+static int node_cmp_attr_CopyB(const ir_node *a, const ir_node *b)
 {
-       const except_attr *ea = &a->attr.except;
-       const except_attr *eb = &b->attr.except;
+       if (get_CopyB_type(a) != get_CopyB_type(b))
+               return 1;
 
-       return ea->pin_state != eb->pin_state;
+       return node_cmp_exception(a, b);
 }
 
-#define node_cmp_attr_Bound  node_cmp_exception
+static int node_cmp_attr_Bound(const ir_node *a, const ir_node *b)
+{
+       return node_cmp_exception(a, b);
+}
 
 /** Compares the attributes of two Div nodes. */
 static int node_cmp_attr_Div(const ir_node *a, const ir_node *b)
 {
        const div_attr *ma = &a->attr.div;
        const div_attr *mb = &b->attr.div;
-       return ma->exc.pin_state != mb->exc.pin_state ||
-                  ma->resmode       != mb->resmode ||
-                  ma->no_remainder  != mb->no_remainder;
+       if (ma->resmode != mb->resmode || ma->no_remainder  != mb->no_remainder)
+               return 1;
+       return node_cmp_exception(a, b);
 }
 
 /** Compares the attributes of two Mod nodes. */
@@ -6238,8 +6283,9 @@ static int node_cmp_attr_Mod(const ir_node *a, const ir_node *b)
 {
        const mod_attr *ma = &a->attr.mod;
        const mod_attr *mb = &b->attr.mod;
-       return ma->exc.pin_state != mb->exc.pin_state ||
-                  ma->resmode       != mb->resmode;
+       if (ma->resmode != mb->resmode)
+               return 1;
+       return node_cmp_exception(a, b);
 }
 
 static int node_cmp_attr_Cmp(const ir_node *a, const ir_node *b)
@@ -6260,8 +6306,11 @@ static int node_cmp_attr_Confirm(const ir_node *a, const ir_node *b)
 /** Compares the attributes of two Builtin nodes. */
 static int node_cmp_attr_Builtin(const ir_node *a, const ir_node *b)
 {
-       /* no need to compare the type, equal kind means equal type */
-       return get_Builtin_kind(a) != get_Builtin_kind(b);
+       if (get_Builtin_kind(a) != get_Builtin_kind(b))
+               return 1;
+       if (get_Builtin_type(a) != get_Builtin_type(b))
+               return 1;
+       return node_cmp_exception(a, b);
 }
 
 /** Compares the attributes of two ASM nodes. */
@@ -6310,7 +6359,8 @@ static int node_cmp_attr_ASM(const ir_node *a, const ir_node *b)
                if (cla[i] != clb[i])
                        return 1;
        }
-       return 0;
+
+       return node_cmp_exception(a, b);
 }
 
 /** Compares the inexistent attributes of two Dummy nodes. */
@@ -6318,9 +6368,17 @@ static int node_cmp_attr_Dummy(const ir_node *a, const ir_node *b)
 {
        (void) a;
        (void) b;
+       /* Dummy nodes never equal by definition */
        return 1;
 }
 
+static int node_cmp_attr_InstOf(const ir_node *a, const ir_node *b)
+{
+       if (get_InstOf_type(a) != get_InstOf_type(b))
+               return 1;
+       return node_cmp_exception(a, b);
+}
+
 /**
  * Set the default node attribute compare operation for an ir_op_ops.
  *
@@ -6338,27 +6396,28 @@ static ir_op_ops *firm_set_default_node_cmp_attr(ir_opcode code, ir_op_ops *ops)
                break
 
        switch (code) {
-       CASE(Const);
-       CASE(Proj);
+       CASE(ASM);
        CASE(Alloc);
-       CASE(Free);
-       CASE(SymConst);
+       CASE(Bound);
+       CASE(Builtin);
        CASE(Call);
-       CASE(Sel);
-       CASE(Phi);
-       CASE(Cmp);
-       CASE(Conv);
        CASE(Cast);
-       CASE(Load);
-       CASE(Store);
+       CASE(Cmp);
        CASE(Confirm);
-       CASE(ASM);
+       CASE(Const);
+       CASE(Conv);
+       CASE(CopyB);
        CASE(Div);
-       CASE(Mod);
-       CASE(Bound);
-       CASE(Builtin);
        CASE(Dummy);
-       /* FIXME CopyB */
+       CASE(Free);
+       CASE(InstOf);
+       CASE(Load);
+       CASE(Mod);
+       CASE(Phi);
+       CASE(Proj);
+       CASE(Sel);
+       CASE(Store);
+       CASE(SymConst);
        default:
                /* leave NULL */
                break;