X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=ir%2Fir%2Firopt.c;h=25d0cd8a185a0cb3fbabe4f603de93c82b90d3fc;hb=69d11aa49aa17358468bbb2f5506ef5f009514e9;hp=d7ab0b261c3d3564440b63ea989fed2e06970298;hpb=f4e5f09589f68012dbd8007389366dd992f7a7c5;p=libfirm diff --git a/ir/ir/iropt.c b/ir/ir/iropt.c index d7ab0b261..25d0cd8a1 100644 --- a/ir/ir/iropt.c +++ b/ir/ir/iropt.c @@ -23,9 +23,7 @@ * @author Christian Schaefer, Goetz Lindenmaier, Michael Beck * @version $Id$ */ -#ifdef HAVE_CONFIG_H -# include "config.h" -#endif +#include "config.h" #include @@ -44,11 +42,11 @@ #include "irhooks.h" #include "irarch.h" #include "hashptr.h" -#include "archop.h" #include "opt_confirms.h" #include "opt_polymorphy.h" #include "irtools.h" -#include "xmalloc.h" +#include "irhooks.h" +#include "array_t.h" /* Make types visible to allow most efficient access */ #include "entity_t.h" @@ -137,9 +135,12 @@ static tarval *computed_value_Sub(const ir_node *n) { tarval *ta; tarval *tb; - /* a - a */ - if (a == b && !is_Bad(a)) - return get_mode_null(mode); + /* NaN - NaN != 0 */ + if (! mode_is_float(mode)) { + /* a - a = 0 */ + if (a == b) + return get_mode_null(mode); + } ta = value_of(a); tb = value_of(b); @@ -226,11 +227,14 @@ static tarval *computed_value_Mul(const ir_node *n) { if (ta != tarval_bad && tb != tarval_bad) { return tarval_mul(ta, tb); } else { - /* a*0 = 0 or 0*b = 0 */ - if (ta == get_mode_null(mode)) - return ta; - if (tb == get_mode_null(mode)) - return tb; + /* a * 0 != 0 if a == NaN or a == Inf */ + if (!mode_is_float(mode)) { + /* a*0 = 0 or 0*b = 0 */ + if (ta == get_mode_null(mode)) + return ta; + if (tb == get_mode_null(mode)) + return tb; + } } return tarval_bad; } /* computed_value_Mul */ @@ -753,8 +757,8 @@ static ir_node *equivalent_node_Block(ir_node *n) ir_node *oldn = n; int n_preds; - /* don't optimize dead blocks */ - if (is_Block_dead(n)) + /* don't optimize dead or labeled blocks */ + if (is_Block_dead(n) || has_Block_label(n)) return n; n_preds = get_Block_n_cfgpreds(n); @@ -1164,13 +1168,28 @@ static ir_node *equivalent_node_And(ir_node *n) { DBG_OPT_ALGSIM0(oldn, n, FS_OPT_AND); return n; } - /* constants are cormalized to right, check this site first */ + /* constants are normalized to right, check this site first */ tv = value_of(b); if (tarval_is_all_one(tv)) { n = a; DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND); return n; } + if (tv != get_tarval_bad()) { + ir_mode *mode = get_irn_mode(n); + if (!mode_is_signed(mode) && is_Conv(a)) { + ir_node *convop = get_Conv_op(a); + ir_mode *convopmode = get_irn_mode(convop); + if (!mode_is_signed(convopmode)) { + if (tarval_is_all_one(tarval_convert_to(tv, convopmode))) { + /* Conv(X) & all_one(mode(X)) = Conv(X) */ + n = a; + DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND); + return n; + } + } + } + } tv = value_of(a); if (tarval_is_all_one(tv)) { n = b; @@ -1209,8 +1228,56 @@ static ir_node *equivalent_node_Conv(ir_node *n) { restart: if (n_mode == a_mode) { /* No Conv necessary */ if (get_Conv_strict(n)) { - /* special case: the predecessor might be a also a Conv */ + ir_node *p = a; + + /* neither Minus nor Abs nor Confirm change the precision, + so we can "look-through" */ + for (;;) { + if (is_Minus(p)) { + p = get_Minus_op(p); + } else if (is_Abs(p)) { + p = get_Abs_op(p); + } else if (is_Confirm(p)) { + p = get_Confirm_value(p); + } else { + /* stop here */ + break; + } + } + if (is_Conv(p) && get_Conv_strict(p)) { + /* we known already, that a_mode == n_mode, and neither + Abs nor Minus change the mode, so the second Conv + can be kicked */ + assert(get_irn_mode(p) == n_mode); + n = a; + DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV); + return n; + } + if (is_Proj(p)) { + ir_node *pred = get_Proj_pred(p); + if (is_Load(pred)) { + /* Loads always return with the exact precision of n_mode */ + assert(get_Load_mode(pred) == n_mode); + n = a; + DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV); + return n; + } + if (is_Proj(pred) && get_Proj_proj(pred) == pn_Start_T_args) { + pred = get_Proj_pred(pred); + if (is_Start(pred)) { + /* Arguments always return with the exact precision, + as strictConv's are place before Call -- if the + caller was compiled with the same setting. + Otherwise, the semantics is probably still right. */ + assert(get_irn_mode(p) == n_mode); + n = a; + DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV); + return n; + } + } + } if (is_Conv(a)) { + /* special case: the immediate predecessor is also a Conv */ if (! get_Conv_strict(a)) { /* first one is not strict, kick it */ a = get_Conv_op(a); @@ -1219,21 +1286,15 @@ restart: goto restart; } /* else both are strict conv, second is superfluous */ - } else { - if (is_Proj(a)) { - ir_node *pred = get_Proj_pred(a); - if (is_Load(pred)) { - /* loads always return with the exact precision of n_mode */ - assert(get_Load_mode(pred) == n_mode); - return a; - } - } - /* leave strict floating point Conv's */ + n = a; + DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV); return n; } + } else { + n = a; + DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV); + return n; } - n = a; - DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV); } else if (is_Conv(a)) { /* Conv(Conv(b)) */ ir_node *b = get_Conv_op(a); ir_mode *b_mode = get_irn_mode(b); @@ -1252,15 +1313,16 @@ restart: if (n_mode == mode_b) { n = b; /* Convb(Conv*(xxxb(...))) == xxxb(...) */ DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV); + return n; } else if (get_mode_arithmetic(n_mode) == get_mode_arithmetic(a_mode)) { - if (smaller_mode(b_mode, a_mode)) { + if (values_in_mode(b_mode, a_mode)) { n = b; /* ConvS(ConvL(xxxS(...))) == xxxS(...) */ DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV); + return n; } } } - if (get_mode_arithmetic(n_mode) == irma_twos_complement && - get_mode_arithmetic(a_mode) == irma_ieee754) { + if (mode_is_int(n_mode) && get_mode_arithmetic(a_mode) == irma_ieee754) { /* ConvI(ConvF(I)) -> I, iff float mantissa >= int mode */ unsigned int_mantissa = get_mode_size_bits(n_mode) - (mode_is_signed(n_mode) ? 1 : 0); unsigned float_mantissa = tarval_ieee754_get_mantissa_size(a_mode); @@ -1277,6 +1339,7 @@ restart: set_Conv_strict(b, 1); n = b; /* ConvA(ConvB(ConvA(...))) == ConvA(...) */ DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV); + return n; } } } @@ -1935,8 +1998,7 @@ static ir_node *apply_binop_on_phi(ir_node *phi, tarval *other, tarval *(*eval)( irg = current_ir_graph; for (i = 0; i < n; ++i) { pred = get_irn_n(phi, i); - res[i] = new_r_Const_type(irg, get_irg_start_block(irg), - mode, res[i], get_Const_type(pred)); + res[i] = new_r_Const_type(irg, res[i], get_Const_type(pred)); } return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode); } /* apply_binop_on_phi */ @@ -1980,7 +2042,7 @@ static ir_node *apply_binop_on_2_phis(ir_node *a, ir_node *b, tarval *(*eval)(), irg = current_ir_graph; for (i = 0; i < n; ++i) { pred = get_irn_n(a, i); - res[i] = new_r_Const_type(irg, get_irg_start_block(irg), mode, res[i], get_Const_type(pred)); + res[i] = new_r_Const_type(irg, res[i], get_Const_type(pred)); } return new_r_Phi(irg, get_nodes_block(a), n, (ir_node **)res, mode); } /* apply_binop_on_2_phis */ @@ -2017,8 +2079,7 @@ static ir_node *apply_unop_on_phi(ir_node *phi, tarval *(*eval)(tarval *)) { irg = current_ir_graph; for (i = 0; i < n; ++i) { pred = get_irn_n(phi, i); - res[i] = new_r_Const_type(irg, get_irg_start_block(irg), - mode, res[i], get_Const_type(pred)); + res[i] = new_r_Const_type(irg, res[i], get_Const_type(pred)); } return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode); } /* apply_unop_on_phi */ @@ -2052,8 +2113,7 @@ static ir_node *apply_conv_on_phi(ir_node *phi, ir_mode *mode) { irg = current_ir_graph; for (i = 0; i < n; ++i) { pred = get_irn_n(phi, i); - res[i] = new_r_Const_type(irg, get_irg_start_block(irg), - mode, res[i], get_Const_type(pred)); + res[i] = new_r_Const_type(irg, res[i], get_Const_type(pred)); } return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode); } /* apply_conv_on_phi */ @@ -2210,7 +2270,7 @@ static ir_node *transform_node_Add(ir_node *n) { current_ir_graph, block, a, - new_r_Const_long(current_ir_graph, block, mode, 2), + new_Const_long(mode, 2), mode); DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_A); return n; @@ -2251,8 +2311,7 @@ static ir_node *transform_node_Add(ir_node *n) { } if (op == b) { /* ~x + x = -1 */ - ir_node *blk = get_nodes_block(n); - n = new_r_Const(current_ir_graph, blk, mode, get_mode_minus_one(mode)); + n = new_Const(get_mode_minus_one(mode)); DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_X_NOT_X); return n; } @@ -2262,8 +2321,7 @@ static ir_node *transform_node_Add(ir_node *n) { if (op == a) { /* x + ~x = -1 */ - ir_node *blk = get_nodes_block(n); - n = new_r_Const(current_ir_graph, blk, mode, get_mode_minus_one(mode)); + n = new_Const(get_mode_minus_one(mode)); DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_X_NOT_X); return n; } @@ -2280,10 +2338,8 @@ static ir_node *const_negate(ir_node *cnst) { tarval *tv = tarval_neg(get_Const_tarval(cnst)); dbg_info *dbgi = get_irn_dbg_info(cnst); ir_graph *irg = get_irn_irg(cnst); - ir_node *block = get_nodes_block(cnst); - ir_mode *mode = get_irn_mode(cnst); if (tv == tarval_bad) return NULL; - return new_rd_Const(dbgi, irg, block, mode, tv); + return new_rd_Const(dbgi, irg, tv); } /** @@ -2338,7 +2394,7 @@ restart: if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic)) return n; - if (is_Const(b) && get_irn_mode(b) != mode_P) { + if (is_Const(b) && !mode_is_reference(get_irn_mode(b))) { /* a - C -> a + (-C) */ ir_node *cnst = const_negate(b); if (cnst != NULL) { @@ -2380,7 +2436,7 @@ restart: ir_node *s_left = get_Sub_left(b); ir_node *s_right = get_Sub_right(b); ir_mode *s_mode = get_irn_mode(b); - if (s_mode == mode_P) { + if (mode_is_reference(s_mode)) { ir_node *sub = new_rd_Sub(s_dbg, irg, s_block, a, s_left, mode); dbg_info *a_dbg = get_irn_dbg_info(n); ir_node *a_block = get_nodes_block(n); @@ -2520,7 +2576,7 @@ restart: get_irn_dbg_info(n), current_ir_graph, blk, mb, - new_r_Const_long(current_ir_graph, blk, mode, 1), + new_Const_long(mode, 1), mode), mode); DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A); @@ -2535,7 +2591,7 @@ restart: get_irn_dbg_info(n), current_ir_graph, blk, ma, - new_r_Const_long(current_ir_graph, blk, mode, 1), + new_Const_long(mode, 1), mode), mode); DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A); @@ -2583,7 +2639,7 @@ restart: tv = tarval_add(tv, get_mode_one(mode)); if (tv != tarval_bad) { ir_node *blk = get_nodes_block(n); - ir_node *c = new_r_Const(current_ir_graph, blk, mode, tv); + ir_node *c = new_Const(tv); n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, blk, get_Not_op(b), c, mode); DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_C_NOT_X); return n; @@ -2773,7 +2829,7 @@ static ir_node *transform_node_Div(ir_node *n) { value = n; tv = value_of(n); if (tv != tarval_bad) { - value = new_Const(get_tarval_mode(tv), tv); + value = new_Const(tv); DBG_OPT_CSTEVAL(n, value); goto make_tuple; @@ -2784,7 +2840,7 @@ static ir_node *transform_node_Div(ir_node *n) { if (a == b && value_not_zero(a, &dummy)) { /* BEWARE: we can optimize a/a to 1 only if this cannot cause a exception */ - value = new_Const(mode, get_mode_one(mode)); + value = new_Const(get_mode_one(mode)); DBG_OPT_CSTEVAL(n, value); goto make_tuple; } else { @@ -2860,7 +2916,7 @@ static ir_node *transform_node_Mod(ir_node *n) { value = n; tv = value_of(n); if (tv != tarval_bad) { - value = new_Const(get_tarval_mode(tv), tv); + value = new_Const(tv); DBG_OPT_CSTEVAL(n, value); goto make_tuple; @@ -2871,7 +2927,7 @@ static ir_node *transform_node_Mod(ir_node *n) { if (a == b && value_not_zero(a, &dummy)) { /* BEWARE: we can optimize a%a to 0 only if this cannot cause a exception */ - value = new_Const(mode, get_mode_null(mode)); + value = new_Const(get_mode_null(mode)); DBG_OPT_CSTEVAL(n, value); goto make_tuple; } else { @@ -2880,7 +2936,7 @@ static ir_node *transform_node_Mod(ir_node *n) { if (tv == get_mode_minus_one(mode)) { /* a % -1 = 0 */ - value = new_Const(mode, get_mode_null(mode)); + value = new_Const(get_mode_null(mode)); DBG_OPT_CSTEVAL(n, value); goto make_tuple; } @@ -2957,7 +3013,7 @@ static ir_node *transform_node_DivMod(ir_node *n) { if (tb != tarval_bad) { if (tb == get_mode_one(get_tarval_mode(tb))) { va = a; - vb = new_Const(mode, get_mode_null(mode)); + vb = new_Const(get_mode_null(mode)); DBG_OPT_CSTEVAL(n, vb); goto make_tuple; } else if (ta != tarval_bad) { @@ -2967,14 +3023,14 @@ static ir_node *transform_node_DivMod(ir_node *n) { Jmp for X result!? */ resb = tarval_mod(ta, tb); if (resb == tarval_bad) return n; /* Causes exception! */ - va = new_Const(mode, resa); - vb = new_Const(mode, resb); + va = new_Const(resa); + vb = new_Const(resb); DBG_OPT_CSTEVAL(n, va); DBG_OPT_CSTEVAL(n, vb); goto make_tuple; } else if (mode_is_signed(mode) && tb == get_mode_minus_one(mode)) { va = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_nodes_block(n), a, mode); - vb = new_Const(mode, get_mode_null(mode)); + vb = new_Const(get_mode_null(mode)); DBG_OPT_CSTEVAL(n, va); DBG_OPT_CSTEVAL(n, vb); goto make_tuple; @@ -2987,8 +3043,8 @@ static ir_node *transform_node_DivMod(ir_node *n) { } else if (a == b) { if (value_not_zero(a, &dummy)) { /* a/a && a != 0 */ - va = new_Const(mode, get_mode_one(mode)); - vb = new_Const(mode, get_mode_null(mode)); + va = new_Const(get_mode_one(mode)); + vb = new_Const(get_mode_null(mode)); DBG_OPT_CSTEVAL(n, va); DBG_OPT_CSTEVAL(n, vb); goto make_tuple; @@ -3034,7 +3090,7 @@ static ir_node *transform_node_Quot(ir_node *n) { tarval *tv = value_of(b); if (tv != tarval_bad) { - int rem; + int rem = tarval_fp_ops_enabled(); /* * Floating point constant folding might be disabled here to @@ -3042,16 +3098,16 @@ static ir_node *transform_node_Quot(ir_node *n) { * However, as we check for exact result, doing it is safe. * Switch it on. */ - rem = tarval_enable_fp_ops(1); + tarval_enable_fp_ops(1); tv = tarval_quo(get_mode_one(mode), tv); - (void)tarval_enable_fp_ops(rem); + tarval_enable_fp_ops(rem); /* Do the transformation if the result is either exact or we are not using strict rules. */ if (tv != tarval_bad && (tarval_ieee754_get_exact() || (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic) == 0)) { ir_node *blk = get_nodes_block(n); - ir_node *c = new_r_Const(current_ir_graph, blk, mode, tv); + ir_node *c = new_Const(tv); ir_node *a = get_Quot_left(n); ir_node *m = new_rd_Mul(get_irn_dbg_info(n), current_ir_graph, blk, a, c, mode); ir_node *mem = get_Quot_mem(n); @@ -3456,8 +3512,8 @@ static ir_node *transform_node_Eor(ir_node *n) { if (a == b) { /* a ^ a = 0 */ - n = new_rd_Const(get_irn_dbg_info(n), current_ir_graph, get_nodes_block(n), - mode, get_mode_null(mode)); + n = new_rd_Const(get_irn_dbg_info(n), current_ir_graph, + get_mode_null(mode)); DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_A_A); } else if (mode == mode_b && is_Proj(a) && @@ -3470,7 +3526,7 @@ static ir_node *transform_node_Eor(ir_node *n) { 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 *cnst = new_Const(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; @@ -3512,7 +3568,7 @@ static ir_node *transform_node_Not(ir_node *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 *cnst = new_Const(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; @@ -3528,7 +3584,7 @@ static ir_node *transform_node_Not(ir_node *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)); + ir_node *add_r = new_rd_Const(dbg, irg, 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); @@ -3568,7 +3624,7 @@ static ir_node *transform_node_Minus(ir_node *n) { ir_node *op = get_Not_op(a); tarval *tv = get_mode_one(mode); ir_node *blk = get_nodes_block(n); - ir_node *c = new_r_Const(current_ir_graph, blk, mode, tv); + ir_node *c = new_Const(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; @@ -3624,7 +3680,7 @@ static ir_node *transform_node_Minus(ir_node *n) { if (tv != tarval_bad) { tv = tarval_neg(tv); if (tv != tarval_bad) { - ir_node *cnst = new_Const(mode, tv); + ir_node *cnst = new_Const(tv); dbg_info *dbg = get_irn_dbg_info(a); ir_graph *irg = current_ir_graph; ir_node *block = get_nodes_block(a); @@ -3647,8 +3703,7 @@ static ir_node *transform_node_Cast(ir_node *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); + n = new_rd_Const_type(NULL, current_ir_graph, 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_irn_mode(pred), @@ -3815,7 +3870,7 @@ static ir_node *transform_node_Proj_Mod(ir_node *proj) { 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)); + ir_node *res = new_Const(get_mode_null(mode)); DBG_OPT_CSTEVAL(mod, res); return res; @@ -3874,7 +3929,7 @@ static ir_node *transform_node_Proj_DivMod(ir_node *proj) { 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)); + ir_node *res = new_Const(get_mode_null(mode)); DBG_OPT_CSTEVAL(divmod, res); return res; @@ -3899,7 +3954,7 @@ static ir_node *transform_node_Proj_Cond(ir_node *proj) { /* 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 (num != get_Cond_default_proj(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(). */ @@ -3920,7 +3975,7 @@ static ir_node *transform_node_Proj_Cond(ir_node *proj) { */ static ir_node *create_zero_const(ir_mode *mode) { tarval *tv = get_mode_null(mode); - ir_node *cnst = new_Const(mode, tv); + ir_node *cnst = new_Const(tv); return cnst; } @@ -3967,12 +4022,12 @@ static ir_node *transform_node_Proj_Cmp(ir_node *proj) { /* we can evaluate some cases directly */ switch (proj_nr) { case pn_Cmp_False: - return new_Const(mode_b, get_tarval_b_false()); + return new_Const(get_tarval_b_false()); case pn_Cmp_True: - return new_Const(mode_b, get_tarval_b_true()); + return new_Const(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()); + return new_Const(get_tarval_b_true()); break; default: break; @@ -4136,6 +4191,31 @@ static ir_node *transform_node_Proj_Cmp(ir_node *proj) { DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP); } } + if (is_And(left) && is_Const(right)) { + ir_node *ll = get_binop_left(left); + ir_node *lr = get_binop_right(left); + if (is_Shr(ll) && is_Const(lr)) { + /* Cmp((x >>u c1) & c2, c3) = Cmp(x & (c2 << c1), c3 << c1) */ + ir_node *block = get_nodes_block(n); + ir_mode *mode = get_irn_mode(left); + + ir_node *llr = get_Shr_right(ll); + if (is_Const(llr)) { + ir_graph *irg = current_ir_graph; + dbg_info *dbg = get_irn_dbg_info(left); + + tarval *c1 = get_Const_tarval(llr); + tarval *c2 = get_Const_tarval(lr); + tarval *c3 = get_Const_tarval(right); + tarval *mask = tarval_shl(c2, c1); + tarval *value = tarval_shl(c3, c1); + + left = new_rd_And(dbg, irg, block, get_Shr_left(ll), new_Const(mask), mode); + right = new_Const(value); + changed |= 1; + } + } + } } /* mode_is_int(...) */ } /* proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg */ @@ -4363,7 +4443,7 @@ static ir_node *transform_node_Proj_Cmp(ir_node *proj) { 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); + c1 = new_Const(tv); DBG_OPT_CSTEVAL(proj, c1); return c1; } @@ -4400,7 +4480,7 @@ static ir_node *transform_node_Proj_Cmp(ir_node *proj) { 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); + c1 = new_Const(tv); DBG_OPT_CSTEVAL(proj, c1); return c1; } @@ -4425,13 +4505,13 @@ static ir_node *transform_node_Proj_Cmp(ir_node *proj) { 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); + c1 = new_Const(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); + left = new_rd_And(get_irn_dbg_info(left), current_ir_graph, blk, sl, new_Const(amask), mode); tv = tarval_shr(tv, tv1); changed |= 2; DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND); @@ -4456,13 +4536,13 @@ static ir_node *transform_node_Proj_Cmp(ir_node *proj) { 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); + c1 = new_Const(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); + left = new_rd_And(get_irn_dbg_info(left), current_ir_graph, blk, sl, new_Const(amask), mode); tv = tarval_shl(tv, tv1); changed |= 2; DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND); @@ -4490,13 +4570,13 @@ static ir_node *transform_node_Proj_Cmp(ir_node *proj) { 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); + c1 = new_Const(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); + left = new_rd_And(get_irn_dbg_info(left), current_ir_graph, blk, sl, new_Const(amask), mode); tv = tarval_shl(tv, tv1); changed |= 2; DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND); @@ -4508,7 +4588,7 @@ static ir_node *transform_node_Proj_Cmp(ir_node *proj) { } if (changed & 2) /* need a new Const */ - right = new_Const(mode, tv); + right = new_Const(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); @@ -4527,7 +4607,7 @@ static ir_node *transform_node_Proj_Cmp(ir_node *proj) { ir_mode *mode = get_irn_mode(v); tv = tarval_sub(tv, get_mode_one(mode), NULL); - left = new_rd_And(get_irn_dbg_info(op), current_ir_graph, blk, v, new_Const(mode, tv), mode); + left = new_rd_And(get_irn_dbg_info(op), current_ir_graph, blk, v, new_Const(tv), mode); changed |= 1; DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_MOD_TO_AND); } @@ -4792,9 +4872,9 @@ static ir_node *transform_node_Or_bf_store(ir_node *or) { 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); + value, new_Const(tarval_and(tv4, tv2)), mode); - new_const = new_r_Const(current_ir_graph, block, mode, tarval_or(tv3, tv1)); + new_const = new_Const(tarval_or(tv3, tv1)); set_Or_left(or, new_and); set_Or_right(or, new_const); @@ -4979,23 +5059,25 @@ static ir_node *transform_node_shift(ir_node *n) { /* beware: a simple replacement works only, if res < modulo shift */ if (!is_Rotl(n)) { int modulo_shf = get_mode_modulo_shift(mode); - assert(modulo_shf >= (int) get_mode_size_bits(mode)); if (modulo_shf > 0) { tarval *modulo = new_tarval_from_long(modulo_shf, get_tarval_mode(res)); + assert(modulo_shf >= (int) get_mode_size_bits(mode)); + /* shifting too much */ if (!(tarval_cmp(res, modulo) & pn_Cmp_Lt)) { if (is_Shrs(n)) { ir_graph *irg = get_irn_irg(n); ir_node *block = get_nodes_block(n); dbg_info *dbgi = get_irn_dbg_info(n); - ir_node *cnst = new_Const(mode_Iu, new_tarval_from_long(get_mode_size_bits(mode)-1, mode_Iu)); + ir_mode *smode = get_irn_mode(right); + ir_node *cnst = new_Const_long(smode, get_mode_size_bits(mode) - 1); return new_rd_Shrs(dbgi, irg, block, get_binop_left(left), cnst, mode); } - return new_Const(mode, get_mode_null(mode)); + return new_Const(get_mode_null(mode)); } } } else { @@ -5006,7 +5088,7 @@ static ir_node *transform_node_shift(ir_node *n) { block = get_nodes_block(n); in[0] = get_binop_left(left); - in[1] = new_r_Const(current_ir_graph, block, get_tarval_mode(res), res); + in[1] = new_Const(res); irn = new_ir_node(NULL, current_ir_graph, block, get_irn_op(n), mode, 2, in); @@ -5085,7 +5167,7 @@ static ir_node *transform_node_bitop_shift(ir_node *n) { } assert(get_tarval_mode(tv_shift) == mode); - new_const = new_Const(mode, tv_shift); + new_const = new_Const(tv_shift); if (op_left == op_And) { new_bitop = new_rd_And(dbgi, irg, block, new_shift, new_const, mode); @@ -5168,7 +5250,10 @@ static ir_node *transform_node_shl_shr(ir_node *n) { return n; } - assert(get_tarval_mode(tv_shl) == get_tarval_mode(tv_shr)); + if (get_tarval_mode(tv_shl) != get_tarval_mode(tv_shr)) { + tv_shl = tarval_convert_to(tv_shl, get_tarval_mode(tv_shr)); + } + assert(tv_mask != tarval_bad); assert(get_tarval_mode(tv_mask) == mode); @@ -5179,7 +5264,7 @@ static ir_node *transform_node_shl_shr(ir_node *n) { pnc = tarval_cmp(tv_shl, tv_shr); if (pnc == pn_Cmp_Lt || pnc == pn_Cmp_Eq) { tv_shift = tarval_sub(tv_shr, tv_shl, NULL); - new_const = new_Const(get_tarval_mode(tv_shift), tv_shift); + new_const = new_Const(tv_shift); if (need_shrs) { new_shift = new_rd_Shrs(dbgi, irg, block, x, new_const, mode); } else { @@ -5188,11 +5273,11 @@ static ir_node *transform_node_shl_shr(ir_node *n) { } else { assert(pnc == pn_Cmp_Gt); tv_shift = tarval_sub(tv_shl, tv_shr, NULL); - new_const = new_Const(get_tarval_mode(tv_shift), tv_shift); + new_const = new_Const(tv_shift); new_shift = new_rd_Shl(dbgi, irg, block, x, new_const, mode); } - new_const = new_Const(mode, tv_mask); + new_const = new_Const(tv_mask); new_and = new_rd_And(dbgi, irg, block, new_shift, new_const, mode); return new_and; @@ -5279,10 +5364,15 @@ static ir_node *transform_node_Rotl(ir_node *n) { */ static ir_node *transform_node_Conv(ir_node *n) { ir_node *c, *oldn = n; - ir_node *a = get_Conv_op(n); + ir_mode *mode = get_irn_mode(n); + ir_node *a = get_Conv_op(n); - if (is_const_Phi(a)) { - c = apply_conv_on_phi(a, get_irn_mode(n)); + if (mode != mode_b && is_const_Phi(a)) { + /* Do NOT optimize mode_b Conv's, this leads to remaining + * Phib nodes later, because the conv_b_lower operation + * is instantly reverted, when it tries to insert a Convb. + */ + c = apply_conv_on_phi(a, mode); if (c) { DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); return c; @@ -5290,10 +5380,34 @@ static ir_node *transform_node_Conv(ir_node *n) { } if (is_Unknown(a)) { /* Conv_A(Unknown_B) -> Unknown_A */ - ir_mode *mode = get_irn_mode(n); return new_r_Unknown(current_ir_graph, mode); } + if (mode_is_reference(mode) && + get_mode_size_bits(mode) == get_mode_size_bits(get_irn_mode(a)) && + is_Add(a)) { + ir_node *l = get_Add_left(a); + ir_node *r = get_Add_right(a); + dbg_info *dbgi = get_irn_dbg_info(a); + ir_node *block = get_nodes_block(n); + if(is_Conv(l)) { + ir_node *lop = get_Conv_op(l); + if(get_irn_mode(lop) == mode) { + /* ConvP(AddI(ConvI(P), x)) -> AddP(P, x) */ + n = new_rd_Add(dbgi, current_ir_graph, block, lop, r, mode); + return n; + } + } + if(is_Conv(r)) { + ir_node *rop = get_Conv_op(r); + if(get_irn_mode(rop) == mode) { + /* ConvP(AddI(x, ConvI(P))) -> AddP(x, P) */ + n = new_rd_Add(dbgi, current_ir_graph, block, l, rop, mode); + return n; + } + } + } + return n; } /* transform_node_Conv */ @@ -5316,6 +5430,9 @@ static ir_node *transform_node_End(ir_node *n) { continue; } else if (is_irn_pinned_in_irg(ka) && is_Block_dead(get_nodes_block(ka))) { continue; + } else if (is_Bad(ka)) { + /* no need to keep Bad */ + continue; } in[j++] = ka; } @@ -5431,10 +5548,10 @@ static ir_node *transform_node_Mux(ir_node *n) { dbg_info *dbg = get_irn_dbg_info(n); ir_node *block = get_nodes_block(n); ir_graph *irg = current_ir_graph; - ir_node *t = new_Const(mode, tarval_sub(a, min, NULL)); - ir_node *f = new_Const(mode, tarval_sub(b, min, NULL)); + ir_node *t = new_Const(tarval_sub(a, min, NULL)); + ir_node *f = new_Const(tarval_sub(b, min, NULL)); n = new_rd_Mux(dbg, irg, block, sel, f, t, mode); - n = new_rd_Add(dbg, irg, block, n, new_Const(mode, min), mode); + n = new_rd_Add(dbg, irg, block, n, new_Const(min), mode); return n; } } @@ -5546,7 +5663,8 @@ static ir_node *transform_node_Mux(ir_node *n) { } } } - return arch_transform_node_Mux(n); + + return n; } /* transform_node_Mux */ /** @@ -5590,7 +5708,80 @@ static ir_node *transform_node_Sync(ir_node *n) { add_identities(current_ir_graph->value_table, n); return n; -} +} /* transform_node_Sync */ + +/** + * optimize a trampoline Call into a direct Call + */ +static ir_node *transform_node_Call(ir_node *call) { + ir_node *callee = get_Call_ptr(call); + ir_node *adr, *mem, *res, *bl, **in; + ir_type *ctp, *mtp, *tp; + ident *id; + dbg_info *db; + int i, n_res, n_param; + ir_variadicity var; + + if (! is_Proj(callee)) + return call; + callee = get_Proj_pred(callee); + if (! is_Builtin(callee)) + return call; + if (get_Builtin_kind(callee) != ir_bk_inner_trampoline) + return call; + + mem = get_Call_mem(call); + + if (skip_Proj(mem) == callee) { + /* memory is routed to the trampoline, skip */ + mem = get_Builtin_mem(callee); + } + + /* build a new call type */ + mtp = get_Call_type(call); + id = get_type_ident(mtp); + id = id_mangle(new_id_from_chars("T_", 2), id); + db = get_type_dbg_info(mtp); + + n_res = get_method_n_ress(mtp); + n_param = get_method_n_params(mtp); + ctp = new_d_type_method(id, n_param + 1, n_res, db); + + for (i = 0; i < n_res; ++i) + set_method_res_type(ctp, i, get_method_res_type(mtp, i)); + + NEW_ARR_A(ir_node *, in, n_param + 1); + + /* FIXME: we don't need a new pointer type in every step */ + tp = get_irg_frame_type(current_ir_graph); + id = id_mangle(get_type_ident(tp), new_id_from_chars("_ptr", 4)); + tp = new_type_pointer(id, tp, mode_P_data); + set_method_param_type(ctp, 0, tp); + + in[0] = get_Builtin_param(callee, 2); + for (i = 0; i < n_param; ++i) { + set_method_param_type(ctp, i + 1, get_method_param_type(mtp, i)); + in[i + 1] = get_Call_param(call, i); + } + var = get_method_variadicity(mtp); + set_method_variadicity(ctp, var); + if (var == variadicity_variadic) { + set_method_first_variadic_param_index(ctp, get_method_first_variadic_param_index(mtp) + 1); + } + /* When we resolve a trampoline, the function must be called by a this-call */ + set_method_calling_convention(ctp, get_method_calling_convention(mtp) | cc_this_call); + set_method_additional_properties(ctp, get_method_additional_properties(mtp)); + + adr = get_Builtin_param(callee, 1); + + db = get_irn_dbg_info(call); + bl = get_nodes_block(call); + + res = new_rd_Call(db, current_ir_graph, bl, mem, adr, n_param + 1, in, ctp); + if (get_irn_pinned(call) == op_pin_state_floats) + set_irn_pinned(res, op_pin_state_floats); + return res; +} /* transform_node_Call */ /** * Tries several [inplace] [optimizing] transformations and returns an @@ -5672,6 +5863,7 @@ static ir_op_ops *firm_set_default_transform_node(ir_opcode code, ir_op_ops *ops CASE(End); CASE(Mux); CASE(Sync); + CASE(Call); default: /* leave NULL */; } @@ -5810,7 +6002,7 @@ static int node_cmp_attr_Div(ir_node *a, ir_node *b) { const divmod_attr *ma = get_irn_divmod_attr(a); const divmod_attr *mb = get_irn_divmod_attr(b); return ma->exc.pin_state != mb->exc.pin_state || - ma->res_mode != mb->res_mode || + ma->resmode != mb->resmode || ma->no_remainder != mb->no_remainder; } /* node_cmp_attr_Div */ @@ -5819,7 +6011,7 @@ static int node_cmp_attr_DivMod(ir_node *a, ir_node *b) { const divmod_attr *ma = get_irn_divmod_attr(a); const divmod_attr *mb = get_irn_divmod_attr(b); return ma->exc.pin_state != mb->exc.pin_state || - ma->res_mode != mb->res_mode; + ma->resmode != mb->resmode; } /* node_cmp_attr_DivMod */ /** Compares the attributes of two Mod nodes. */ @@ -5827,7 +6019,7 @@ static int node_cmp_attr_Mod(ir_node *a, ir_node *b) { const divmod_attr *ma = get_irn_divmod_attr(a); const divmod_attr *mb = get_irn_divmod_attr(b); return ma->exc.pin_state != mb->exc.pin_state || - ma->res_mode != mb->res_mode; + ma->resmode != mb->resmode; } /* node_cmp_attr_Mod */ /** Compares the attributes of two Quot nodes. */ @@ -5835,14 +6027,24 @@ static int node_cmp_attr_Quot(ir_node *a, ir_node *b) { const divmod_attr *ma = get_irn_divmod_attr(a); const divmod_attr *mb = get_irn_divmod_attr(b); return ma->exc.pin_state != mb->exc.pin_state || - ma->res_mode != mb->res_mode; + ma->resmode != mb->resmode; } /* node_cmp_attr_Quot */ /** Compares the attributes of two Confirm nodes. */ static int node_cmp_attr_Confirm(ir_node *a, ir_node *b) { + /* no need to compare the bound, as this is a input */ return (get_Confirm_cmp(a) != get_Confirm_cmp(b)); } /* node_cmp_attr_Confirm */ +/** Compares the attributes of two Builtin nodes. */ +static int node_cmp_attr_Builtin(ir_node *a, ir_node *b) { + const builtin_attr *ma = get_irn_builtin_attr(a); + const builtin_attr *mb = get_irn_builtin_attr(b); + + /* no need to compare the type, equal kind means equal type */ + return ma->kind != mb->kind; +} /* node_cmp_attr_Builtin */ + /** Compares the attributes of two ASM nodes. */ static int node_cmp_attr_ASM(ir_node *a, ir_node *b) { int i, n; @@ -5889,6 +6091,14 @@ static int node_cmp_attr_ASM(ir_node *a, ir_node *b) { return 0; } /* node_cmp_attr_ASM */ +/** Compares the inexistent attributes of two Dummy nodes. */ +static int node_cmp_attr_Dummy(ir_node *a, ir_node *b) +{ + (void) a; + (void) b; + return 1; +} + /** * Set the default node attribute compare operation for an ir_op_ops. * @@ -5926,6 +6136,8 @@ static ir_op_ops *firm_set_default_node_cmp_attr(ir_opcode code, ir_op_ops *ops) CASE(Mod); CASE(Quot); CASE(Bound); + CASE(Builtin); + CASE(Dummy); /* FIXME CopyB */ default: /* leave NULL */; @@ -5997,13 +6209,9 @@ void del_identities(pset *value_table) { del_pset(value_table); } /* del_identities */ -/** - * Normalize a node by putting constants (and operands with larger - * node index) on the right (operator side). - * - * @param n The node to normalize - */ -static void normalize_node(ir_node *n) { +/* Normalize a node by putting constants (and operands with larger + * node index) on the right (operator side). */ +void ir_normalize_node(ir_node *n) { if (is_op_commutative(get_irn_op(n))) { ir_node *l = get_binop_left(n); ir_node *r = get_binop_right(n); @@ -6015,9 +6223,10 @@ static void normalize_node(ir_node *n) { if (!operands_are_normalized(l, r)) { set_binop_left(n, r); set_binop_right(n, l); + hook_normalize(n); } } -} /* normalize_node */ +} /* ir_normalize_node */ /** * Update the nodes after a match in the value table. If both nodes have @@ -6083,7 +6292,7 @@ ir_node *identify_remember(pset *value_table, ir_node *n) { if (!value_table) return n; - normalize_node(n); + ir_normalize_node(n); /* lookup or insert in hash table with given hash key. */ o = pset_insert(value_table, n, ir_node_hash(n)); @@ -6102,7 +6311,7 @@ ir_node *identify_remember(pset *value_table, ir_node *n) { * @param value_table The value table * @param n The node to lookup */ -static INLINE ir_node *identify_cons(pset *value_table, ir_node *n) { +static inline ir_node *identify_cons(pset *value_table, ir_node *n) { ir_node *old = n; n = identify_remember(value_table, n); @@ -6241,7 +6450,10 @@ ir_node *optimize_node(ir_node *n) { /* 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); + int old_fp_mode = tarval_fp_ops_enabled(); + + tarval_enable_fp_ops(! (fp_model & fp_no_float_fold)); + /* try to evaluate */ tv = computed_value(n); if (tv != tarval_bad) { @@ -6274,7 +6486,7 @@ ir_node *optimize_node(ir_node *n) { /* evaluation was successful -- replace the node. */ irg_kill_node(current_ir_graph, n); - nw = new_Const(get_tarval_mode(tv), tv); + nw = new_Const(tv); if (old_tp && get_type_mode(old_tp) == get_tarval_mode(tv)) set_Const_type(nw, old_tp); @@ -6353,7 +6565,9 @@ ir_node *optimize_in_place_2(ir_node *n) { /* 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); + int old_fp_mode = tarval_fp_ops_enabled(); + + tarval_enable_fp_ops((fp_model & fp_strict_algebraic) == 0); /* try to evaluate */ tv = computed_value(n); if (tv != tarval_bad) { @@ -6367,7 +6581,7 @@ ir_node *optimize_in_place_2(ir_node *n) { for (i = 0; i < arity && !old_tp; ++i) old_tp = get_irn_type(get_irn_n(n, i)); - n = new_Const(get_tarval_mode(tv), tv); + n = new_Const(tv); if (old_tp && get_type_mode(old_tp) == get_tarval_mode(tv)) set_Const_type(n, old_tp);