+ DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
+ }
+ }
+
+ /* the following reassociations work only for == and != */
+ if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
+
+#if 0 /* Might be not that good in general */
+ /* a-b == 0 ==> a == b, a-b != 0 ==> a != b */
+ if (tarval_is_null(tv) && is_Sub(left)) {
+ right = get_Sub_right(left);
+ left = get_Sub_left(left);
+
+ tv = value_of(right);
+ changed = 1;
+ DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
+ }
+#endif
+
+ if (tv != tarval_bad) {
+ /* a-c1 == c2 ==> a == c2+c1, a-c1 != c2 ==> a != c2+c1 */
+ if (is_Sub(left)) {
+ ir_node *c1 = get_Sub_right(left);
+ tarval *tv2 = value_of(c1);
+
+ if (tv2 != tarval_bad) {
+ tv2 = tarval_add(tv, value_of(c1));
+
+ if (tv2 != tarval_bad) {
+ left = get_Sub_left(left);
+ tv = tv2;
+ changed |= 2;
+ DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
+ }
+ }
+ }
+ /* a+c1 == c2 ==> a == c2-c1, a+c1 != c2 ==> a != c2-c1 */
+ else if (is_Add(left)) {
+ ir_node *a_l = get_Add_left(left);
+ ir_node *a_r = get_Add_right(left);
+ ir_node *a;
+ tarval *tv2;
+
+ if (is_Const(a_l)) {
+ a = a_r;
+ tv2 = value_of(a_l);
+ } else {
+ a = a_l;
+ tv2 = value_of(a_r);
+ }
+
+ if (tv2 != tarval_bad) {
+ tv2 = tarval_sub(tv, tv2, NULL);
+
+ if (tv2 != tarval_bad) {
+ left = a;
+ tv = tv2;
+ changed |= 2;
+ DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
+ }
+ }
+ }
+ /* -a == c ==> a == -c, -a != c ==> a != -c */
+ else if (is_Minus(left)) {
+ tarval *tv2 = tarval_sub(get_mode_null(mode), tv, NULL);
+
+ if (tv2 != tarval_bad) {
+ left = get_Minus_op(left);
+ tv = tv2;
+ changed |= 2;
+ DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
+ }
+ }
+ }
+ } /* == or != */
+ /* the following reassociations work only for <= */
+ else if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
+ if (tv != tarval_bad) {
+ /* c >= 0 : Abs(a) <= c ==> (unsigned)(a + c) <= 2*c */
+ if (get_irn_op(left) == op_Abs) { // TODO something is missing here
+ }
+ }
+ }
+ } /* mode_is_int */
+
+ if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
+ switch (get_irn_opcode(left)) {
+ ir_node *c1;
+
+ case iro_And:
+ c1 = get_And_right(left);
+ if (is_Const(c1)) {
+ /*
+ * And(x, C1) == C2 ==> FALSE if C2 & C1 != C2
+ * And(x, C1) != C2 ==> TRUE if C2 & C1 != C2
+ */
+ tarval *mask = tarval_and(get_Const_tarval(c1), tv);
+ if (mask != tv) {
+ /* TODO: move to constant evaluation */
+ tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
+ c1 = new_Const(mode_b, tv);
+ DBG_OPT_CSTEVAL(proj, c1);
+ return c1;
+ }
+
+ if (tarval_is_single_bit(tv)) {
+ /*
+ * optimization for AND:
+ * Optimize:
+ * And(x, C) == C ==> And(x, C) != 0
+ * And(x, C) != C ==> And(X, C) == 0
+ *
+ * if C is a single Bit constant.
+ */
+
+ /* check for Constant's match. We have check hare the tarvals,
+ because our const might be changed */
+ if (get_Const_tarval(c1) == tv) {
+ /* fine: do the transformation */
+ tv = get_mode_null(get_tarval_mode(tv));
+ proj_nr ^= pn_Cmp_Leg;
+ changed |= 2;
+ DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
+ }
+ }
+ }
+ break;
+ case iro_Or:
+ c1 = get_Or_right(left);
+ if (is_Const(c1) && tarval_is_null(tv)) {
+ /*
+ * Or(x, C) == 0 && C != 0 ==> FALSE
+ * Or(x, C) != 0 && C != 0 ==> TRUE
+ */
+ if (! tarval_is_null(get_Const_tarval(c1))) {
+ /* TODO: move to constant evaluation */
+ tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
+ c1 = new_Const(mode_b, tv);
+ DBG_OPT_CSTEVAL(proj, c1);
+ return c1;
+ }
+ }
+ break;
+ case iro_Shl:
+ /*
+ * optimize x << c1 == c into x & (-1 >>u c1) == c >> c1 if c & (-1 << c1) == c
+ * FALSE else
+ * optimize x << c1 != c into x & (-1 >>u c1) != c >> c1 if c & (-1 << c1) == c
+ * TRUE else
+ */
+ c1 = get_Shl_right(left);
+ if (is_Const(c1)) {
+ tarval *tv1 = get_Const_tarval(c1);
+ ir_mode *mode = get_irn_mode(left);
+ tarval *minus1 = get_mode_all_one(mode);
+ tarval *amask = tarval_shr(minus1, tv1);
+ tarval *cmask = tarval_shl(minus1, tv1);
+ ir_node *sl, *blk;
+
+ if (tarval_and(tv, cmask) != tv) {
+ /* condition not met */
+ tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
+ c1 = new_Const(mode_b, tv);
+ DBG_OPT_CSTEVAL(proj, c1);
+ return c1;
+ }
+ sl = get_Shl_left(left);
+ blk = get_nodes_block(n);
+ left = new_rd_And(get_irn_dbg_info(left), current_ir_graph, blk, sl, new_Const(mode, amask), mode);
+ tv = tarval_shr(tv, tv1);
+ changed |= 2;
+ DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
+ }
+ break;
+ case iro_Shr:
+ /*
+ * optimize x >>u c1 == c into x & (-1 << c1) == c << c1 if c & (-1 >>u c1) == c
+ * FALSE else
+ * optimize x >>u c1 != c into x & (-1 << c1) != c << c1 if c & (-1 >>u c1) == c
+ * TRUE else
+ */
+ c1 = get_Shr_right(left);
+ if (is_Const(c1)) {
+ tarval *tv1 = get_Const_tarval(c1);
+ ir_mode *mode = get_irn_mode(left);
+ tarval *minus1 = get_mode_all_one(mode);
+ tarval *amask = tarval_shl(minus1, tv1);
+ tarval *cmask = tarval_shr(minus1, tv1);
+ ir_node *sl, *blk;
+
+ if (tarval_and(tv, cmask) != tv) {
+ /* condition not met */
+ tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
+ c1 = new_Const(mode_b, tv);
+ DBG_OPT_CSTEVAL(proj, c1);
+ return c1;
+ }
+ sl = get_Shr_left(left);
+ blk = get_nodes_block(n);
+ left = new_rd_And(get_irn_dbg_info(left), current_ir_graph, blk, sl, new_Const(mode, amask), mode);
+ tv = tarval_shl(tv, tv1);
+ changed |= 2;
+ DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
+ }
+ break;
+ case iro_Shrs:
+ /*
+ * optimize x >>s c1 == c into x & (-1 << c1) == c << c1 if (c >>s (BITS - c1)) \in {0,-1}
+ * FALSE else
+ * optimize x >>s c1 != c into x & (-1 << c1) != c << c1 if (c >>s (BITS - c1)) \in {0,-1}
+ * TRUE else
+ */
+ c1 = get_Shrs_right(left);
+ if (is_Const(c1)) {
+ tarval *tv1 = get_Const_tarval(c1);
+ ir_mode *mode = get_irn_mode(left);
+ tarval *minus1 = get_mode_all_one(mode);
+ tarval *amask = tarval_shl(minus1, tv1);
+ tarval *cond = new_tarval_from_long(get_mode_size_bits(mode), get_tarval_mode(tv1));
+ ir_node *sl, *blk;
+
+ cond = tarval_sub(cond, tv1, NULL);
+ cond = tarval_shrs(tv, cond);
+
+ if (!tarval_is_all_one(cond) && !tarval_is_null(cond)) {
+ /* condition not met */
+ tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
+ c1 = new_Const(mode_b, tv);
+ DBG_OPT_CSTEVAL(proj, c1);
+ return c1;
+ }
+ sl = get_Shrs_left(left);
+ blk = get_nodes_block(n);
+ left = new_rd_And(get_irn_dbg_info(left), current_ir_graph, blk, sl, new_Const(mode, amask), mode);
+ tv = tarval_shl(tv, tv1);
+ changed |= 2;
+ DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);