+ ir_node *oldn = n;
+ ir_node *a = get_binop_left(n);
+ ir_node *b = get_binop_right(n);
+ ir_op *op = get_irn_op(a);
+ ir_op *op_root = get_irn_op(n);
+
+ if(op != get_irn_op(b))
+ return n;
+
+ if (op == op_Conv) {
+ ir_node *a_op = get_Conv_op(a);
+ ir_node *b_op = get_Conv_op(b);
+ ir_mode *a_mode = get_irn_mode(a_op);
+ ir_mode *b_mode = get_irn_mode(b_op);
+ if(a_mode == b_mode && (mode_is_int(a_mode) || a_mode == mode_b)) {
+ ir_node *blk = get_irn_n(n, -1);
+
+ n = exact_copy(n);
+ set_binop_left(n, a_op);
+ set_binop_right(n, b_op);
+ set_irn_mode(n, a_mode);
+ n = trans_func(n);
+ n = new_r_Conv(current_ir_graph, blk, n, get_irn_mode(oldn));
+
+ DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_SHIFT_AND);
+ return n;
+ }
+ }
+
+ if (op == op_Eor) {
+ /* nothing to gain here */
+ return n;
+ }
+
+ if (op == op_Shrs || op == op_Shr || op == op_Shl
+ || op == op_And || op == op_Or || op == op_Eor) {
+ ir_node *a_left = get_binop_left(a);
+ ir_node *a_right = get_binop_right(a);
+ ir_node *b_left = get_binop_left(b);
+ ir_node *b_right = get_binop_right(b);
+ ir_node *c = NULL;
+ ir_node *op1 = NULL;
+ ir_node *op2 = NULL;
+
+ if (is_op_commutative(op)) {
+ if (a_left == b_left) {
+ c = a_left;
+ op1 = a_right;
+ op2 = b_right;
+ } else if(a_left == b_right) {
+ c = a_left;
+ op1 = a_right;
+ op2 = b_left;
+ } else if(a_right == b_left) {
+ c = a_right;
+ op1 = a_left;
+ op2 = b_right;
+ }
+ }
+ if(a_right == b_right) {
+ c = a_right;
+ op1 = a_left;
+ op2 = b_left;
+ }
+
+ if (c != NULL) {
+ /* (a sop c) & (b sop c) => (a & b) sop c */
+ ir_node *blk = get_irn_n(n, -1);
+
+ ir_node *new_n = exact_copy(n);
+ set_binop_left(new_n, op1);
+ set_binop_right(new_n, op2);
+ new_n = trans_func(new_n);
+
+ if(op_root == op_Eor && op == op_Or) {
+ dbg_info *dbgi = get_irn_dbg_info(n);
+ ir_graph *irg = current_ir_graph;
+ ir_mode *mode = get_irn_mode(c);
+
+ c = new_rd_Not(dbgi, irg, blk, c, mode);
+ n = new_rd_And(dbgi, irg, blk, new_n, c, mode);
+ } else {
+ n = exact_copy(a);
+ set_irn_n(n, -1, blk);
+ set_binop_left(n, new_n);
+ set_binop_right(n, c);
+ add_identities(current_ir_graph->value_table, n);
+ }
+
+ DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_SHIFT_AND);
+ return n;
+ }
+ }
+
+ return n;
+}
+
+/**
+ * Transform an And.
+ */
+static ir_node *transform_node_And(ir_node *n) {
+ ir_node *c, *oldn = n;
+ ir_node *a = get_And_left(n);
+ ir_node *b = get_And_right(n);
+ ir_mode *mode;
+
+ mode = get_irn_mode(n);
+ HANDLE_BINOP_PHI(tarval_and, a, b, c, mode);
+
+ /* we can evaluate 2 Projs of the same Cmp */
+ if (mode == mode_b && is_Proj(a) && is_Proj(b)) {
+ ir_node *pred_a = get_Proj_pred(a);
+ ir_node *pred_b = get_Proj_pred(b);
+ if (pred_a == pred_b) {
+ dbg_info *dbgi = get_irn_dbg_info(n);
+ ir_node *block = get_nodes_block(pred_a);
+ pn_Cmp pn_a = get_Proj_proj(a);
+ pn_Cmp pn_b = get_Proj_proj(b);
+ /* yes, we can simply calculate with pncs */
+ pn_Cmp new_pnc = pn_a & pn_b;
+
+ return new_rd_Proj(dbgi, current_ir_graph, block, pred_a, mode_b, new_pnc);
+ }
+ }
+ if (is_Or(a)) {
+ if (is_Not(b)) {
+ ir_node *op = get_Not_op(b);
+ if (is_And(op)) {
+ ir_node *ba = get_And_left(op);
+ ir_node *bb = get_And_right(op);
+
+ /* it's enough to test the following cases due to normalization! */
+ if (get_Or_left(a) == ba && get_Or_right(a) == bb) {
+ /* (a|b) & ~(a&b) = a^b */
+ ir_node *block = get_nodes_block(n);
+
+ n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph, block, ba, bb, mode);
+ DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
+ return n;
+ }
+ }
+ }
+ }
+ if (is_Or(b)) {
+ if (is_Not(a)) {
+ ir_node *op = get_Not_op(a);
+ if (is_And(op)) {
+ ir_node *aa = get_And_left(op);
+ ir_node *ab = get_And_right(op);
+
+ /* it's enough to test the following cases due to normalization! */
+ if (get_Or_left(b) == aa && get_Or_right(b) == ab) {
+ /* (a|b) & ~(a&b) = a^b */
+ ir_node *block = get_nodes_block(n);
+
+ n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph, block, aa, ab, mode);
+ DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
+ return n;
+ }
+ }
+ }
+ }
+ if (is_Eor(a)) {
+ ir_node *al = get_Eor_left(a);
+ ir_node *ar = get_Eor_right(a);
+
+ if (al == b) {
+ /* (b ^ a) & b -> ~a & b */
+ dbg_info *dbg = get_irn_dbg_info(n);
+ ir_node *block = get_nodes_block(n);
+
+ ar = new_rd_Not(dbg, current_ir_graph, block, ar, mode);
+ n = new_rd_And(dbg, current_ir_graph, block, ar, b, mode);
+ DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
+ return n;
+ }
+ if (ar == b) {
+ /* (a ^ b) & b -> ~a & b */
+ dbg_info *dbg = get_irn_dbg_info(n);
+ ir_node *block = get_nodes_block(n);
+
+ al = new_rd_Not(dbg, current_ir_graph, block, al, mode);
+ n = new_rd_And(dbg, current_ir_graph, block, al, b, mode);
+ DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
+ return n;
+ }
+ }
+ if (is_Eor(b)) {
+ ir_node *bl = get_Eor_left(b);
+ ir_node *br = get_Eor_right(b);
+
+ if (bl == a) {
+ /* a & (a ^ b) -> a & ~b */
+ dbg_info *dbg = get_irn_dbg_info(n);
+ ir_node *block = get_nodes_block(n);
+
+ br = new_rd_Not(dbg, current_ir_graph, block, br, mode);
+ n = new_rd_And(dbg, current_ir_graph, block, br, a, mode);
+ DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
+ return n;
+ }
+ if (br == a) {
+ /* a & (b ^ a) -> a & ~b */
+ dbg_info *dbg = get_irn_dbg_info(n);
+ ir_node *block = get_nodes_block(n);
+
+ bl = new_rd_Not(dbg, current_ir_graph, block, bl, mode);
+ n = new_rd_And(dbg, current_ir_graph, block, bl, a, mode);
+ DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
+ return n;
+ }
+ }
+ if (is_Not(a) && is_Not(b)) {
+ /* ~a & ~b = ~(a|b) */
+ ir_node *block = get_nodes_block(n);
+ ir_mode *mode = get_irn_mode(n);
+
+ a = get_Not_op(a);
+ b = get_Not_op(b);
+ n = new_rd_Or(get_irn_dbg_info(n), current_ir_graph, block, a, b, mode);
+ n = new_rd_Not(get_irn_dbg_info(n), current_ir_graph, block, n, mode);
+ DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
+ return n;
+ }
+
+ n = transform_bitwise_distributive(n, transform_node_And);
+
+ return n;
+} /* transform_node_And */
+
+/**
+ * Transform an Eor.
+ */
+static ir_node *transform_node_Eor(ir_node *n) {
+ ir_node *c, *oldn = n;
+ ir_node *a = get_Eor_left(n);
+ ir_node *b = get_Eor_right(n);
+ ir_mode *mode = get_irn_mode(n);
+
+ HANDLE_BINOP_PHI(tarval_eor, a, b, c, mode);
+
+ /* we can evaluate 2 Projs of the same Cmp */
+ if (mode == mode_b && is_Proj(a) && is_Proj(b)) {
+ ir_node *pred_a = get_Proj_pred(a);
+ ir_node *pred_b = get_Proj_pred(b);
+ if(pred_a == pred_b) {
+ dbg_info *dbgi = get_irn_dbg_info(n);
+ ir_node *block = get_nodes_block(pred_a);
+ pn_Cmp pn_a = get_Proj_proj(a);
+ pn_Cmp pn_b = get_Proj_proj(b);
+ /* yes, we can simply calculate with pncs */
+ pn_Cmp new_pnc = pn_a ^ pn_b;
+
+ return new_rd_Proj(dbgi, current_ir_graph, block, pred_a, mode_b,
+ new_pnc);
+ }
+ }
+
+ if (a == b) {
+ /* a ^ a = 0 */
+ n = new_rd_Const(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1),
+ mode, get_mode_null(mode));
+ DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_A_A);
+ } else if (mode == mode_b &&
+ is_Proj(a) &&
+ is_Const(b) && is_Const_one(b) &&
+ is_Cmp(get_Proj_pred(a))) {
+ /* The Eor negates a Cmp. The Cmp has the negated result anyways! */
+ n = new_r_Proj(current_ir_graph, get_irn_n(n, -1), get_Proj_pred(a),
+ mode_b, get_negated_pnc(get_Proj_proj(a), mode));
+
+ DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT_BOOL);
+ } else if (is_Const(b)) {
+ if (is_Not(a)) { /* ~x ^ const -> x ^ ~const */
+ ir_node *cnst = new_Const(mode, tarval_not(get_Const_tarval(b)));
+ ir_node *not_op = get_Not_op(a);
+ dbg_info *dbg = get_irn_dbg_info(n);
+ ir_graph *irg = current_ir_graph;
+ ir_node *block = get_nodes_block(n);
+ ir_mode *mode = get_irn_mode(n);
+ n = new_rd_Eor(dbg, irg, block, not_op, cnst, mode);
+ return n;
+ } else if (is_Const_all_one(b)) { /* x ^ 1...1 -> ~1 */
+ n = new_r_Not(current_ir_graph, get_nodes_block(n), a, mode);
+ DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
+ }
+ } else {
+ n = transform_bitwise_distributive(n, transform_node_Eor);
+ }
+
+ return n;
+} /* transform_node_Eor */
+
+/**
+ * Transform a Not.
+ */
+static ir_node *transform_node_Not(ir_node *n) {
+ ir_node *c, *oldn = n;
+ ir_node *a = get_Not_op(n);
+ ir_mode *mode = get_irn_mode(n);
+
+ HANDLE_UNOP_PHI(tarval_not,a,c);
+
+ /* check for a boolean Not */
+ if (mode == mode_b &&
+ is_Proj(a) &&
+ is_Cmp(get_Proj_pred(a))) {
+ /* We negate a Cmp. The Cmp has the negated result anyways! */
+ n = new_r_Proj(current_ir_graph, get_irn_n(n, -1), get_Proj_pred(a),
+ mode_b, get_negated_pnc(get_Proj_proj(a), mode_b));
+ DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_CMP);
+ return n;
+ }
+ if (is_Eor(a)) {
+ ir_node *eor_b = get_Eor_right(a);
+ if (is_Const(eor_b)) { /* ~(x ^ const) -> x ^ ~const */
+ ir_node *cnst = new_Const(mode, tarval_not(get_Const_tarval(eor_b)));
+ ir_node *eor_a = get_Eor_left(a);
+ dbg_info *dbg = get_irn_dbg_info(n);
+ ir_graph *irg = current_ir_graph;
+ ir_node *block = get_nodes_block(n);
+ ir_mode *mode = get_irn_mode(n);
+ n = new_rd_Eor(dbg, irg, block, eor_a, cnst, mode);
+ return n;
+ }
+ }
+ if (get_mode_arithmetic(mode) == irma_twos_complement) {
+ if (is_Minus(a)) { /* ~-x -> x + -1 */
+ dbg_info *dbg = get_irn_dbg_info(n);
+ ir_graph *irg = current_ir_graph;
+ ir_node *block = get_nodes_block(n);
+ ir_node *add_l = get_Minus_op(a);
+ ir_node *add_r = new_rd_Const(dbg, irg, block, mode, get_mode_minus_one(mode));
+ n = new_rd_Add(dbg, irg, block, add_l, add_r, mode);
+ } else if (is_Add(a)) {
+ ir_node *add_r = get_Add_right(a);
+ if (is_Const(add_r) && is_Const_all_one(add_r)) {
+ /* ~(x + -1) = -x */
+ ir_node *op = get_Add_left(a);
+ ir_node *blk = get_irn_n(n, -1);
+ n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, op, get_irn_mode(n));
+ DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_MINUS_1);
+ }
+ }
+ }
+ return n;
+} /* transform_node_Not */
+
+/**
+ * Transform a Minus.
+ * Optimize:
+ * -(~x) = x + 1
+ * -(a-b) = b - a
+ * -(a >>u (size-1)) = a >>s (size-1)
+ * -(a >>s (size-1)) = a >>u (size-1)
+ * -(a * const) -> a * -const
+ */
+static ir_node *transform_node_Minus(ir_node *n) {
+ ir_node *c, *oldn = n;
+ ir_node *a = get_Minus_op(n);
+ ir_mode *mode;
+
+ HANDLE_UNOP_PHI(tarval_neg,a,c);
+
+ mode = get_irn_mode(a);
+ if (get_mode_arithmetic(mode) == irma_twos_complement) {
+ /* the following rules are only to twos-complement */
+ if (is_Not(a)) {
+ /* -(~x) = x + 1 */
+ ir_node *op = get_Not_op(a);
+ tarval *tv = get_mode_one(mode);
+ ir_node *blk = get_irn_n(n, -1);
+ ir_node *c = new_r_Const(current_ir_graph, blk, mode, tv);
+ n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, blk, op, c, mode);
+ DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_NOT);
+ return n;
+ }
+ if (is_Shr(a)) {
+ ir_node *c = get_Shr_right(a);
+
+ if (is_Const(c)) {
+ tarval *tv = get_Const_tarval(c);
+
+ if (tarval_is_long(tv) && get_tarval_long(tv) == get_mode_size_bits(mode) - 1) {
+ /* -(a >>u (size-1)) = a >>s (size-1) */
+ ir_node *v = get_Shr_left(a);
+
+ n = new_rd_Shrs(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), v, c, mode);
+ DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
+ return n;
+ }
+ }
+ }
+ if (is_Shrs(a)) {
+ ir_node *c = get_Shrs_right(a);
+
+ if (is_Const(c)) {
+ tarval *tv = get_Const_tarval(c);
+
+ if (tarval_is_long(tv) && get_tarval_long(tv) == get_mode_size_bits(mode) - 1) {
+ /* -(a >>s (size-1)) = a >>u (size-1) */
+ ir_node *v = get_Shrs_left(a);
+
+ n = new_rd_Shr(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), v, c, mode);
+ DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
+ return n;
+ }
+ }
+ }
+ }
+ if (is_Sub(a)) {
+ /* - (a-b) = b - a */
+ ir_node *la = get_Sub_left(a);
+ ir_node *ra = get_Sub_right(a);
+ ir_node *blk = get_irn_n(n, -1);
+
+ n = new_rd_Sub(get_irn_dbg_info(n), current_ir_graph, blk, ra, la, mode);
+ DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_SUB);
+ return n;
+ }
+
+ if (is_Mul(a)) { /* -(a * const) -> a * -const */
+ ir_node *mul_l = get_Mul_left(a);
+ ir_node *mul_r = get_Mul_right(a);
+ if (is_Const(mul_r)) {
+ tarval *tv = tarval_neg(get_Const_tarval(mul_r));
+ ir_node *cnst = new_Const(mode, tv);
+ dbg_info *dbg = get_irn_dbg_info(a);
+ ir_graph *irg = current_ir_graph;
+ ir_node *block = get_nodes_block(a);
+ n = new_rd_Mul(dbg, irg, block, mul_l, cnst, mode);
+ DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_MUL_C);
+ return n;
+ }
+ }
+
+ return n;
+} /* transform_node_Minus */
+
+/**
+ * Transform a Cast_type(Const) into a new Const_type
+ */
+static ir_node *transform_node_Cast(ir_node *n) {
+ ir_node *oldn = n;
+ ir_node *pred = get_Cast_op(n);
+ ir_type *tp = get_irn_type(n);
+
+ if (is_Const(pred) && get_Const_type(pred) != tp) {
+ n = new_rd_Const_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_irn_mode(pred),
+ get_Const_tarval(pred), tp);
+ DBG_OPT_CSTEVAL(oldn, n);
+ } else if (is_SymConst(pred) && get_SymConst_value_type(pred) != tp) {
+ n = new_rd_SymConst_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_SymConst_symbol(pred),
+ get_SymConst_kind(pred), tp);
+ DBG_OPT_CSTEVAL(oldn, n);
+ }
+
+ return n;
+} /* transform_node_Cast */
+
+/**
+ * Transform a Proj(Div) with a non-zero value.
+ * Removes the exceptions and routes the memory to the NoMem node.
+ */
+static ir_node *transform_node_Proj_Div(ir_node *proj) {
+ ir_node *div = get_Proj_pred(proj);
+ ir_node *b = get_Div_right(div);
+ ir_node *confirm, *res, *new_mem;
+ long proj_nr;
+
+ if (value_not_zero(b, &confirm)) {
+ /* div(x, y) && y != 0 */
+ if (confirm == NULL) {
+ /* we are sure we have a Const != 0 */
+ new_mem = get_Div_mem(div);
+ if (is_Pin(new_mem))
+ new_mem = get_Pin_op(new_mem);
+ set_Div_mem(div, new_mem);
+ set_irn_pinned(div, op_pin_state_floats);
+ }
+
+ proj_nr = get_Proj_proj(proj);
+ switch (proj_nr) {
+ case pn_Div_X_regular:
+ return new_r_Jmp(current_ir_graph, get_irn_n(div, -1));
+
+ case pn_Div_X_except:
+ /* we found an exception handler, remove it */
+ DBG_OPT_EXC_REM(proj);
+ return new_Bad();
+
+ case pn_Div_M:
+ res = get_Div_mem(div);
+ new_mem = get_irg_no_mem(current_ir_graph);
+
+ if (confirm) {
+ /* This node can only float up to the Confirm block */
+ new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
+ }
+ set_irn_pinned(div, op_pin_state_floats);
+ /* this is a Div without exception, we can remove the memory edge */
+ set_Div_mem(div, new_mem);
+ return res;
+ }
+ }
+ return proj;
+} /* transform_node_Proj_Div */
+
+/**
+ * Transform a Proj(Mod) with a non-zero value.
+ * Removes the exceptions and routes the memory to the NoMem node.
+ */
+static ir_node *transform_node_Proj_Mod(ir_node *proj) {
+ ir_node *mod = get_Proj_pred(proj);
+ ir_node *b = get_Mod_right(mod);
+ ir_node *confirm, *res, *new_mem;
+ long proj_nr;
+
+ if (value_not_zero(b, &confirm)) {
+ /* mod(x, y) && y != 0 */
+ proj_nr = get_Proj_proj(proj);
+
+ if (confirm == NULL) {
+ /* we are sure we have a Const != 0 */
+ new_mem = get_Mod_mem(mod);
+ if (is_Pin(new_mem))
+ new_mem = get_Pin_op(new_mem);
+ set_Mod_mem(mod, new_mem);
+ set_irn_pinned(mod, op_pin_state_floats);
+ }
+
+ switch (proj_nr) {
+
+ case pn_Mod_X_regular:
+ return new_r_Jmp(current_ir_graph, get_irn_n(mod, -1));
+
+ case pn_Mod_X_except:
+ /* we found an exception handler, remove it */
+ DBG_OPT_EXC_REM(proj);
+ return new_Bad();
+
+ case pn_Mod_M:
+ res = get_Mod_mem(mod);
+ new_mem = get_irg_no_mem(current_ir_graph);
+
+ if (confirm) {
+ /* This node can only float up to the Confirm block */
+ new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
+ }
+ /* this is a Mod without exception, we can remove the memory edge */
+ set_Mod_mem(mod, new_mem);
+ return res;
+ case pn_Mod_res:
+ if (get_Mod_left(mod) == b) {
+ /* a % a = 0 if a != 0 */
+ ir_mode *mode = get_irn_mode(proj);
+ ir_node *res = new_Const(mode, get_mode_null(mode));
+
+ DBG_OPT_CSTEVAL(mod, res);
+ return res;
+ }
+ }
+ }
+ return proj;
+} /* transform_node_Proj_Mod */
+
+/**
+ * Transform a Proj(DivMod) with a non-zero value.
+ * Removes the exceptions and routes the memory to the NoMem node.
+ */
+static ir_node *transform_node_Proj_DivMod(ir_node *proj) {
+ ir_node *divmod = get_Proj_pred(proj);
+ ir_node *b = get_DivMod_right(divmod);
+ ir_node *confirm, *res, *new_mem;
+ long proj_nr;
+
+ if (value_not_zero(b, &confirm)) {
+ /* DivMod(x, y) && y != 0 */
+ proj_nr = get_Proj_proj(proj);
+
+ if (confirm == NULL) {
+ /* we are sure we have a Const != 0 */
+ new_mem = get_DivMod_mem(divmod);
+ if (is_Pin(new_mem))
+ new_mem = get_Pin_op(new_mem);
+ set_DivMod_mem(divmod, new_mem);
+ set_irn_pinned(divmod, op_pin_state_floats);
+ }
+
+ switch (proj_nr) {
+
+ case pn_DivMod_X_regular:
+ return new_r_Jmp(current_ir_graph, get_irn_n(divmod, -1));
+
+ case pn_DivMod_X_except:
+ /* we found an exception handler, remove it */
+ DBG_OPT_EXC_REM(proj);
+ return new_Bad();
+
+ case pn_DivMod_M:
+ res = get_DivMod_mem(divmod);
+ new_mem = get_irg_no_mem(current_ir_graph);
+
+ if (confirm) {
+ /* This node can only float up to the Confirm block */
+ new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
+ }
+ /* this is a DivMod without exception, we can remove the memory edge */
+ set_DivMod_mem(divmod, new_mem);
+ return res;
+
+ case pn_DivMod_res_mod:
+ if (get_DivMod_left(divmod) == b) {
+ /* a % a = 0 if a != 0 */
+ ir_mode *mode = get_irn_mode(proj);
+ ir_node *res = new_Const(mode, get_mode_null(mode));
+
+ DBG_OPT_CSTEVAL(divmod, res);
+ return res;
+ }
+ }
+ }
+ return proj;
+} /* transform_node_Proj_DivMod */
+
+/**
+ * Optimizes jump tables (CondIs or CondIu) by removing all impossible cases.
+ */
+static ir_node *transform_node_Proj_Cond(ir_node *proj) {
+ if (get_opt_unreachable_code()) {
+ ir_node *n = get_Proj_pred(proj);
+ ir_node *b = get_Cond_selector(n);
+
+ if (mode_is_int(get_irn_mode(b))) {
+ tarval *tb = value_of(b);
+
+ if (tb != tarval_bad) {
+ /* we have a constant switch */
+ long num = get_Proj_proj(proj);
+
+ if (num != get_Cond_defaultProj(n)) { /* we cannot optimize default Proj's yet */
+ if (get_tarval_long(tb) == num) {
+ /* Do NOT create a jump here, or we will have 2 control flow ops
+ * in a block. This case is optimized away in optimize_cf(). */
+ return proj;
+ } else {
+ /* this case will NEVER be taken, kill it */
+ return new_Bad();
+ }
+ }
+ }
+ }
+ }
+ return proj;
+} /* transform_node_Proj_Cond */
+
+/**
+ * Create a 0 constant of given mode.
+ */
+static ir_node *create_zero_const(ir_mode *mode) {
+ tarval *tv = get_mode_null(mode);
+ ir_node *cnst = new_Const(mode, tv);
+
+ return cnst;
+}
+
+/* the order of the values is important! */
+typedef enum const_class {
+ const_const = 0,
+ const_like = 1,
+ const_other = 2
+} const_class;
+
+static const_class classify_const(const ir_node* n)
+{
+ if (is_Const(n)) return const_const;
+ if (is_irn_constlike(n)) return const_like;
+ return const_other;
+}
+
+/**
+ * Determines whether r is more constlike or has a larger index (in that order)
+ * than l.
+ */
+static int operands_are_normalized(const ir_node *l, const ir_node *r)
+{
+ const const_class l_order = classify_const(l);
+ const const_class r_order = classify_const(r);
+ return
+ l_order > r_order ||
+ (l_order == r_order && get_irn_idx(l) <= get_irn_idx(r));
+}
+
+/**
+ * Normalizes and optimizes Cmp nodes.
+ */
+static ir_node *transform_node_Proj_Cmp(ir_node *proj) {
+ ir_node *n = get_Proj_pred(proj);
+ ir_node *left = get_Cmp_left(n);
+ ir_node *right = get_Cmp_right(n);
+ tarval *tv = NULL;
+ int changed = 0;
+ ir_mode *mode = NULL;
+ long proj_nr = get_Proj_proj(proj);
+
+ /* we can evaluate some cases directly */
+ switch (proj_nr) {
+ case pn_Cmp_False:
+ return new_Const(mode_b, get_tarval_b_false());
+ case pn_Cmp_True:
+ return new_Const(mode_b, get_tarval_b_true());
+ case pn_Cmp_Leg:
+ if (!mode_is_float(get_irn_mode(left)))
+ return new_Const(mode_b, get_tarval_b_true());
+ break;
+ default:
+ break;
+ }
+
+ /* remove Casts */
+ if (is_Cast(left))
+ left = get_Cast_op(left);
+ if (is_Cast(right))
+ right = get_Cast_op(right);
+
+ /* Remove unnecessary conversions */
+ /* TODO handle constants */
+ if (is_Conv(left) && is_Conv(right)) {
+ ir_mode *mode = get_irn_mode(left);
+ ir_node *op_left = get_Conv_op(left);
+ ir_node *op_right = get_Conv_op(right);
+ ir_mode *mode_left = get_irn_mode(op_left);
+ ir_mode *mode_right = get_irn_mode(op_right);
+
+ if (smaller_mode(mode_left, mode) && smaller_mode(mode_right, mode)) {
+ ir_graph *irg = current_ir_graph;
+ ir_node *block = get_nodes_block(n);
+
+ if (mode_left == mode_right) {
+ left = op_left;
+ right = op_right;
+ changed |= 1;
+ DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV_CONV);
+ } else if (smaller_mode(mode_left, mode_right)) {
+ left = new_r_Conv(irg, block, op_left, mode_right);
+ right = op_right;
+ changed |= 1;
+ DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
+ } else if (smaller_mode(mode_right, mode_left)) {
+ left = op_left;
+ right = new_r_Conv(irg, block, op_right, mode_left);
+ changed |= 1;
+ DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
+ }
+ }
+ }
+
+ /* remove operation of both sides if possible */
+ if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
+ /*
+ * The following operations are NOT safe for floating point operations, for instance
+ * 1.0 + inf == 2.0 + inf, =/=> x == y
+ */
+ if (mode_is_int(get_irn_mode(left))) {
+ unsigned lop = get_irn_opcode(left);
+
+ if (lop == get_irn_opcode(right)) {
+ ir_node *ll, *lr, *rl, *rr;
+
+ /* same operation on both sides, try to remove */
+ switch (lop) {
+ case iro_Not:
+ case iro_Minus:
+ /* ~a CMP ~b => a CMP b, -a CMP -b ==> a CMP b */
+ left = get_unop_op(left);
+ right = get_unop_op(right);
+ changed |= 1;
+ DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
+ break;
+ case iro_Add:
+ ll = get_Add_left(left);
+ lr = get_Add_right(left);
+ rl = get_Add_left(right);
+ rr = get_Add_right(right);
+
+ if (ll == rl) {
+ /* X + a CMP X + b ==> a CMP b */
+ left = lr;
+ right = rr;
+ changed |= 1;
+ DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
+ } else if (ll == rr) {
+ /* X + a CMP b + X ==> a CMP b */
+ left = lr;
+ right = rl;
+ changed |= 1;
+ DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
+ } else if (lr == rl) {
+ /* a + X CMP X + b ==> a CMP b */
+ left = ll;
+ right = rr;
+ changed |= 1;
+ DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
+ } else if (lr == rr) {
+ /* a + X CMP b + X ==> a CMP b */
+ left = ll;
+ right = rl;
+ changed |= 1;
+ DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
+ }
+ break;
+ case iro_Sub:
+ ll = get_Sub_left(left);
+ lr = get_Sub_right(left);
+ rl = get_Sub_left(right);
+ rr = get_Sub_right(right);
+
+ if (ll == rl) {
+ /* X - a CMP X - b ==> a CMP b */
+ left = lr;
+ right = rr;
+ changed |= 1;
+ DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
+ } else if (lr == rr) {
+ /* a - X CMP b - X ==> a CMP b */
+ left = ll;
+ right = rl;
+ changed |= 1;
+ DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
+ }
+ break;
+ case iro_Rot:
+ if (get_Rot_right(left) == get_Rot_right(right)) {
+ /* a ROT X CMP b ROT X ==> a CMP b */
+ left = get_Rot_left(left);
+ right = get_Rot_left(right);
+ changed |= 1;
+ DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
+ }
+ break;
+ default:
+ break;
+ }
+ }
+
+ /* X+A == A, A+X == A, A-X == A -> X == 0 */
+ if (is_Add(left) || is_Sub(left)) {
+ ir_node *ll = get_binop_left(left);
+ ir_node *lr = get_binop_right(left);
+
+ if (lr == right && is_Add(left)) {
+ ir_node *tmp = ll;
+ ll = lr;
+ lr = tmp;
+ }
+ if (ll == right) {
+ left = lr;
+ right = create_zero_const(get_irn_mode(left));
+ changed |= 1;
+ DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
+ }
+ }
+ if (is_Add(right) || is_Sub(right)) {
+ ir_node *rl = get_binop_left(right);
+ ir_node *rr = get_binop_right(right);
+
+ if (rr == left && is_Add(right)) {
+ ir_node *tmp = rl;
+ rl = rr;
+ rr = tmp;
+ }
+ if (rl == left) {
+ left = rr;
+ right = create_zero_const(get_irn_mode(left));
+ changed |= 1;
+ DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
+ }
+ }
+ } /* mode_is_int(...) */
+ } /* proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg */
+
+ /* replace mode_b compares with ands/ors */
+ if (get_irn_mode(left) == mode_b) {
+ ir_graph *irg = current_ir_graph;
+ ir_node *block = get_nodes_block(n);
+ ir_node *bres;
+
+ switch (proj_nr) {
+ case pn_Cmp_Le: bres = new_r_Or( irg, block, new_r_Not(irg, block, left, mode_b), right, mode_b); break;
+ case pn_Cmp_Lt: bres = new_r_And(irg, block, new_r_Not(irg, block, left, mode_b), right, mode_b); break;
+ case pn_Cmp_Ge: bres = new_r_Or( irg, block, left, new_r_Not(irg, block, right, mode_b), mode_b); break;
+ case pn_Cmp_Gt: bres = new_r_And(irg, block, left, new_r_Not(irg, block, right, mode_b), mode_b); break;
+ case pn_Cmp_Lg: bres = new_r_Eor(irg, block, left, right, mode_b); break;
+ case pn_Cmp_Eq: bres = new_r_Not(irg, block, new_r_Eor(irg, block, left, right, mode_b), mode_b); break;
+ default: bres = NULL;
+ }
+ if (bres) {
+ DBG_OPT_ALGSIM0(n, bres, FS_OPT_CMP_TO_BOOL);
+ return bres;
+ }
+ }
+
+ if (!get_opt_reassociation())
+ return proj;
+
+ /*
+ * First step: normalize the compare op
+ * by placing the constant on the right side
+ * or moving the lower address node to the left.
+ */
+ if (!operands_are_normalized(left, right)) {
+ ir_node *t = left;
+
+ left = right;
+ right = t;
+
+ proj_nr = get_inversed_pnc(proj_nr);
+ changed |= 1;
+ }
+
+ /*
+ * Second step: Try to reduce the magnitude
+ * of a constant. This may help to generate better code
+ * later and may help to normalize more compares.
+ * Of course this is only possible for integer values.
+ */
+ if (is_Const(right)) {
+ mode = get_irn_mode(right);
+ tv = get_Const_tarval(right);
+
+ /* TODO extend to arbitrary constants */
+ if (is_Conv(left) && tarval_is_null(tv)) {
+ ir_node *op = get_Conv_op(left);
+ ir_mode *op_mode = get_irn_mode(op);
+
+ /*
+ * UpConv(x) REL 0 ==> x REL 0
+ */
+ if (get_mode_size_bits(mode) > get_mode_size_bits(op_mode) &&
+ ((proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) ||
+ mode_is_signed(mode) || !mode_is_signed(op_mode))) {
+ tv = get_mode_null(op_mode);
+ left = op;
+ mode = op_mode;
+ changed |= 2;
+ DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
+ }
+ }
+
+ if (tv != tarval_bad) {
+ /* the following optimization is possible on modes without Overflow
+ * on Unary Minus or on == and !=:
+ * -a CMP c ==> a swap(CMP) -c
+ *
+ * Beware: for two-complement Overflow may occur, so only == and != can
+ * be optimized, see this:
+ * -MININT < 0 =/=> MININT > 0 !!!
+ */
+ if (is_Minus(left) &&
+ (!mode_overflow_on_unary_Minus(mode) ||
+ (mode_is_int(mode) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)))) {
+ tv = tarval_neg(tv);
+
+ if (tv != tarval_bad) {
+ left = get_Minus_op(left);
+ proj_nr = get_inversed_pnc(proj_nr);
+ changed |= 2;
+ DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
+ }
+ } else if (is_Not(left) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)) {
+ /* Not(a) ==/!= c ==> a ==/!= Not(c) */
+ tv = tarval_not(tv);
+
+ if (tv != tarval_bad) {
+ left = get_Not_op(left);
+ changed |= 2;
+ DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
+ }
+ }
+
+ /* for integer modes, we have more */
+ if (mode_is_int(mode)) {
+ /* Ne includes Unordered which is not possible on integers.
+ * However, frontends often use this wrong, so fix it here */
+ if (proj_nr & pn_Cmp_Uo) {
+ proj_nr &= ~pn_Cmp_Uo;
+ set_Proj_proj(proj, proj_nr);
+ }
+
+ /* c > 0 : a < c ==> a <= (c-1) a >= c ==> a > (c-1) */
+ if ((proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Ge) &&
+ tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Gt) {
+ tv = tarval_sub(tv, get_mode_one(mode));
+
+ if (tv != tarval_bad) {
+ proj_nr ^= pn_Cmp_Eq;
+ changed |= 2;
+ DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
+ }
+ }
+ /* c < 0 : a > c ==> a >= (c+1) a <= c ==> a < (c+1) */
+ else if ((proj_nr == pn_Cmp_Gt || proj_nr == pn_Cmp_Le) &&
+ tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Lt) {
+ tv = tarval_add(tv, get_mode_one(mode));
+
+ if (tv != tarval_bad) {
+ proj_nr ^= pn_Cmp_Eq;
+ changed |= 2;
+ 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);
+
+ 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);
+
+ 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);
+ 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);
+ }
+ break;
+ } /* switch */
+ }
+ } /* tarval != bad */
+ }
+
+ if (changed & 2) /* need a new Const */
+ right = new_Const(mode, tv);
+
+ if ((proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) && is_Const(right) && is_Const_null(right) && is_Proj(left)) {
+ ir_node *op = get_Proj_pred(left);
+
+ if ((is_Mod(op) && get_Proj_proj(left) == pn_Mod_res) ||
+ (is_DivMod(op) && get_Proj_proj(left) == pn_DivMod_res_mod)) {
+ ir_node *c = get_binop_right(op);
+
+ if (is_Const(c)) {
+ tarval *tv = get_Const_tarval(c);
+
+ if (tarval_is_single_bit(tv)) {
+ /* special case: (x % 2^n) CMP 0 ==> x & (2^n-1) CMP 0 */
+ ir_node *v = get_binop_left(op);
+ ir_node *blk = get_irn_n(op, -1);
+ ir_mode *mode = get_irn_mode(v);
+
+ tv = tarval_sub(tv, get_mode_one(mode));
+ left = new_rd_And(get_irn_dbg_info(op), current_ir_graph, blk, v, new_Const(mode, tv), mode);
+ changed |= 1;
+ DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_MOD_TO_AND);
+ }
+ }
+ }
+ }
+
+ if (changed) {
+ ir_node *block = get_irn_n(n, -1); /* Beware of get_nodes_Block() */
+
+ /* create a new compare */
+ n = new_rd_Cmp(get_irn_dbg_info(n), current_ir_graph, block, left, right);
+ proj = new_rd_Proj(get_irn_dbg_info(proj), current_ir_graph, block, n, get_irn_mode(proj), proj_nr);
+ }
+
+ return proj;
+} /* transform_node_Proj_Cmp */
+
+/**
+ * Does all optimizations on nodes that must be done on it's Proj's
+ * because of creating new nodes.
+ */
+static ir_node *transform_node_Proj(ir_node *proj) {
+ ir_node *n = get_Proj_pred(proj);
+
+ switch (get_irn_opcode(n)) {
+ case iro_Div:
+ return transform_node_Proj_Div(proj);
+
+ case iro_Mod:
+ return transform_node_Proj_Mod(proj);
+
+ case iro_DivMod:
+ return transform_node_Proj_DivMod(proj);
+
+ case iro_Cond:
+ return transform_node_Proj_Cond(proj);
+
+ case iro_Cmp:
+ return transform_node_Proj_Cmp(proj);
+
+ case iro_Tuple:
+ /* should not happen, but if it does will be optimized away */
+ return equivalent_node_Proj(proj);
+
+ default:
+ /* do nothing */
+ return proj;
+ }
+} /* transform_node_Proj */
+
+/**
+ * Move Confirms down through Phi nodes.
+ */
+static ir_node *transform_node_Phi(ir_node *phi) {
+ int i, n;
+ ir_mode *mode = get_irn_mode(phi);
+
+ if (mode_is_reference(mode)) {
+ n = get_irn_arity(phi);
+
+ /* Beware of Phi0 */
+ if (n > 0) {
+ ir_node *pred = get_irn_n(phi, 0);
+ ir_node *bound, *new_Phi, *block, **in;
+ pn_Cmp pnc;
+
+ if (! is_Confirm(pred))
+ return phi;
+
+ bound = get_Confirm_bound(pred);
+ pnc = get_Confirm_cmp(pred);
+
+ NEW_ARR_A(ir_node *, in, n);
+ in[0] = get_Confirm_value(pred);
+
+ for (i = 1; i < n; ++i) {
+ pred = get_irn_n(phi, i);
+
+ if (! is_Confirm(pred) ||
+ get_Confirm_bound(pred) != bound ||
+ get_Confirm_cmp(pred) != pnc)
+ return phi;
+ in[i] = get_Confirm_value(pred);
+ }
+ /* move the Confirm nodes "behind" the Phi */
+ block = get_irn_n(phi, -1);
+ new_Phi = new_r_Phi(current_ir_graph, block, n, in, get_irn_mode(phi));
+ return new_r_Confirm(current_ir_graph, block, new_Phi, bound, pnc);
+ }
+ }
+ return phi;
+} /* transform_node_Phi */
+
+/**
+ * Returns the operands of a commutative bin-op, if one operand is
+ * a const, it is returned as the second one.
+ */
+static void get_comm_Binop_Ops(ir_node *binop, ir_node **a, ir_node **c) {
+ ir_node *op_a = get_binop_left(binop);
+ ir_node *op_b = get_binop_right(binop);
+
+ assert(is_op_commutative(get_irn_op(binop)));
+
+ if (is_Const(op_a)) {
+ *a = op_b;
+ *c = op_a;
+ } else {
+ *a = op_a;
+ *c = op_b;
+ }
+} /* get_comm_Binop_Ops */
+
+/**
+ * Optimize a Or(And(Or(And(v,c4),c3),c2),c1) pattern if possible.
+ * Such pattern may arise in bitfield stores.
+ *
+ * value c4 value c4 & c2
+ * AND c3 AND c1 | c3
+ * OR c2 ===> OR
+ * AND c1
+ * OR
+ *
+ *
+ * value c2 value c1
+ * AND c1 ===> OR if (c1 | c2) == 0x111..11
+ * OR
+ */
+static ir_node *transform_node_Or_bf_store(ir_node *or) {
+ ir_node *and, *c1;
+ ir_node *or_l, *c2;
+ ir_node *and_l, *c3;
+ ir_node *value, *c4;
+ ir_node *new_and, *new_const, *block;
+ ir_mode *mode = get_irn_mode(or);
+
+ tarval *tv1, *tv2, *tv3, *tv4, *tv, *n_tv4, *n_tv2;
+
+ while (1) {
+ get_comm_Binop_Ops(or, &and, &c1);
+ if (!is_Const(c1) || !is_And(and))
+ return or;
+
+ get_comm_Binop_Ops(and, &or_l, &c2);
+ if (!is_Const(c2))
+ return or;
+
+ tv1 = get_Const_tarval(c1);
+ tv2 = get_Const_tarval(c2);
+
+ tv = tarval_or(tv1, tv2);
+ if (tarval_is_all_one(tv)) {
+ /* the AND does NOT clear a bit with isn't set by the OR */
+ set_Or_left(or, or_l);
+ set_Or_right(or, c1);
+
+ /* check for more */
+ continue;
+ }
+
+ if (!is_Or(or_l))
+ return or;
+
+ get_comm_Binop_Ops(or_l, &and_l, &c3);
+ if (!is_Const(c3) || !is_And(and_l))
+ return or;
+
+ get_comm_Binop_Ops(and_l, &value, &c4);
+ if (!is_Const(c4))
+ return or;
+
+ /* ok, found the pattern, check for conditions */
+ assert(mode == get_irn_mode(and));
+ assert(mode == get_irn_mode(or_l));
+ assert(mode == get_irn_mode(and_l));
+
+ tv3 = get_Const_tarval(c3);
+ tv4 = get_Const_tarval(c4);
+
+ tv = tarval_or(tv4, tv2);
+ if (!tarval_is_all_one(tv)) {
+ /* have at least one 0 at the same bit position */
+ return or;
+ }
+
+ n_tv4 = tarval_not(tv4);
+ if (tv3 != tarval_and(tv3, n_tv4)) {
+ /* bit in the or_mask is outside the and_mask */
+ return or;
+ }
+
+ n_tv2 = tarval_not(tv2);
+ if (tv1 != tarval_and(tv1, n_tv2)) {
+ /* bit in the or_mask is outside the and_mask */
+ return or;
+ }
+
+ /* ok, all conditions met */
+ block = get_irn_n(or, -1);
+
+ new_and = new_r_And(current_ir_graph, block,
+ value, new_r_Const(current_ir_graph, block, mode, tarval_and(tv4, tv2)), mode);
+
+ new_const = new_r_Const(current_ir_graph, block, mode, tarval_or(tv3, tv1));
+
+ set_Or_left(or, new_and);
+ set_Or_right(or, new_const);
+
+ /* check for more */
+ }
+} /* transform_node_Or_bf_store */
+
+/**
+ * Optimize an Or(shl(x, c), shr(x, bits - c)) into a Rot
+ */
+static ir_node *transform_node_Or_Rot(ir_node *or) {
+ ir_mode *mode = get_irn_mode(or);
+ ir_node *shl, *shr, *block;
+ ir_node *irn, *x, *c1, *c2, *v, *sub, *n;
+ tarval *tv1, *tv2;
+
+ if (! mode_is_int(mode))
+ return or;
+
+ shl = get_binop_left(or);
+ shr = get_binop_right(or);
+
+ if (is_Shr(shl)) {
+ if (!is_Shl(shr))
+ return or;
+
+ irn = shl;
+ shl = shr;
+ shr = irn;
+ } else if (!is_Shl(shl)) {
+ return or;
+ } else if (!is_Shr(shr)) {
+ return or;
+ }
+ x = get_Shl_left(shl);
+ if (x != get_Shr_left(shr))
+ return or;
+
+ c1 = get_Shl_right(shl);
+ c2 = get_Shr_right(shr);
+ if (is_Const(c1) && is_Const(c2)) {
+ tv1 = get_Const_tarval(c1);
+ if (! tarval_is_long(tv1))
+ return or;
+
+ tv2 = get_Const_tarval(c2);
+ if (! tarval_is_long(tv2))
+ return or;
+
+ if (get_tarval_long(tv1) + get_tarval_long(tv2)
+ != get_mode_size_bits(mode))
+ return or;
+
+ /* yet, condition met */
+ block = get_irn_n(or, -1);
+
+ n = new_r_Rot(current_ir_graph, block, x, c1, mode);
+
+ DBG_OPT_ALGSIM1(or, shl, shr, n, FS_OPT_OR_SHFT_TO_ROT);
+ return n;
+ } else if (is_Sub(c1)) {
+ v = c2;
+ sub = c1;