X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=ir%2Fir%2Firopt.c;h=4fc0169aa03e8ecfbbd4038809d7cbc981c15fbd;hb=203717b8dd44597fb10c126b33a28528a2432e9c;hp=d6fe83e7a6d55796d56c9b7bcc63e8895d0178a8;hpb=4ee6ddb2bb214c77a0d307ef1acaf3bc3d16e3b3;p=libfirm diff --git a/ir/ir/iropt.c b/ir/ir/iropt.c index d6fe83e7a..4fc0169aa 100644 --- a/ir/ir/iropt.c +++ b/ir/ir/iropt.c @@ -1756,6 +1756,50 @@ static ir_node *apply_binop_on_phi(ir_node *phi, tarval *other, tarval *(*eval)( return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode); } /* apply_binop_on_phi */ +/** + * Apply an evaluator on a binop with two constant Phi. + * + * @param a the left Phi node + * @param b the right Phi node + * @param eval an evaluator function + * @param mode the mode of the result, may be different from the mode of the Phi! + * + * @return a new Phi node if the conversion was successful, NULL else + */ +static ir_node *apply_binop_on_2_phis(ir_node *a, ir_node *b, tarval *(*eval)(tarval *, tarval *), ir_mode *mode) { + tarval *tv_l, *tv_r, *tv; + void **res; + ir_node *pred; + ir_graph *irg; + int i, n; + + if (get_nodes_block(a) != get_nodes_block(b)) + return NULL; + + n = get_irn_arity(a); + NEW_ARR_A(void *, res, n); + + for (i = 0; i < n; ++i) { + pred = get_irn_n(a, i); + tv_l = get_Const_tarval(pred); + pred = get_irn_n(b, i); + tv_r = get_Const_tarval(pred); + tv = eval(tv_l, tv_r); + + if (tv == tarval_bad) { + /* folding failed, bad */ + return NULL; + } + res[i] = tv; + } + 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)); + } + return new_r_Phi(irg, get_nodes_block(a), n, (ir_node **)res, mode); +} /* apply_binop_on_2_phis */ + /** * Apply an evaluator on a unop with a constant operator (a Phi). * @@ -1901,6 +1945,10 @@ static ir_node *transform_node_AddSub(ir_node *n) { /* check for Op(Const, Phi) */ \ c = apply_binop_on_phi(b, get_Const_tarval(a), eval, mode, 1);\ } \ + else if (is_const_Phi(a) && is_const_Phi(b)) { \ + /* check for Op(Phi, Phi) */ \ + c = apply_binop_on_2_phis(a, b, eval, mode); \ + } \ if (c) { \ DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \ return c; \ @@ -1979,74 +2027,6 @@ static ir_node *transform_node_Add(ir_node *n) { DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B); return n; } - if (! is_reassoc_running()) { - /* do NOT execute this code if reassociation is enabled, it does the inverse! */ - if (is_Mul(a)) { - ir_node *ma = get_Mul_left(a); - ir_node *mb = get_Mul_right(a); - - if (b == ma) { - ir_node *blk = get_irn_n(n, -1); - n = new_rd_Mul( - get_irn_dbg_info(n), current_ir_graph, blk, - ma, - new_rd_Add( - get_irn_dbg_info(n), current_ir_graph, blk, - mb, - new_r_Const_long(current_ir_graph, blk, mode, 1), - mode), - mode); - DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A); - return n; - } else if (b == mb) { - ir_node *blk = get_irn_n(n, -1); - n = new_rd_Mul( - get_irn_dbg_info(n), current_ir_graph, blk, - mb, - new_rd_Add( - get_irn_dbg_info(n), current_ir_graph, blk, - ma, - new_r_Const_long(current_ir_graph, blk, mode, 1), - mode), - mode); - DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A); - return n; - } - } - if (is_Mul(b)) { - ir_node *ma = get_Mul_left(b); - ir_node *mb = get_Mul_right(b); - - if (a == ma) { - ir_node *blk = get_irn_n(n, -1); - n = new_rd_Mul( - get_irn_dbg_info(n), current_ir_graph, blk, - ma, - new_rd_Add( - get_irn_dbg_info(n), current_ir_graph, blk, - mb, - new_r_Const_long(current_ir_graph, blk, mode, 1), - mode), - mode); - DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A); - return n; - } - if (a == mb) { - ir_node *blk = get_irn_n(n, -1); - n = new_rd_Mul( - get_irn_dbg_info(n), current_ir_graph, blk, - mb, - new_rd_Add( - get_irn_dbg_info(n), current_ir_graph, blk, - ma, - new_r_Const_long(current_ir_graph, blk, mode, 1), - mode), - mode); - DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A); - return n; - } - } - } if (get_mode_arithmetic(mode) == irma_twos_complement) { /* Here we rely on constants be on the RIGHT side */ if (is_Not(a)) { @@ -2263,16 +2243,16 @@ restart: if (mode == get_irn_mode(b)) { ir_mode *ma, *mb; - - a = get_Conv_op(a); - b = get_Conv_op(b); + ir_node *op_a = get_Conv_op(a); + ir_node *op_b = get_Conv_op(b); /* check if it's allowed to skip the conv */ - ma = get_irn_mode(a); - mb = get_irn_mode(b); + ma = get_irn_mode(op_a); + mb = get_irn_mode(op_b); if (mode_is_reference(ma) && mode_is_reference(mb)) { /* SubInt(ConvInt(aP), ConvInt(bP)) -> SubInt(aP,bP) */ + a = op_a; b = op_b; set_Sub_left(n, a); set_Sub_right(n, b); @@ -2506,10 +2486,39 @@ static ir_node *transform_node_Mul(ir_node *n) { * Transform a Div Node. */ static ir_node *transform_node_Div(ir_node *n) { - tarval *tv = value_of(n); ir_mode *mode = get_Div_resmode(n); - ir_node *value = n; + ir_node *a = get_Div_left(n); + ir_node *b = get_Div_right(n); + ir_node *value; + tarval *tv; + + if (is_Const(b) && is_const_Phi(a)) { + /* check for Div(Phi, Const) */ + value = apply_binop_on_phi(a, get_Const_tarval(b), tarval_div, mode, 0); + if (value) { + DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI); + goto make_tuple; + } + } + else if (is_Const(a) && is_const_Phi(b)) { + /* check for Div(Const, Phi) */ + value = apply_binop_on_phi(b, get_Const_tarval(a), tarval_div, mode, 1); + if (value) { + DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI); + goto make_tuple; + } + } + else if (is_const_Phi(a) && is_const_Phi(b)) { + /* check for Div(Phi, Phi) */ + value = apply_binop_on_2_phis(a, b, tarval_div, mode); + if (value) { + DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI); + goto make_tuple; + } + } + value = n; + tv = value_of(n); if (tv != tarval_bad) { value = new_Const(get_tarval_mode(tv), tv); @@ -2565,10 +2574,39 @@ make_tuple: * Transform a Mod node. */ static ir_node *transform_node_Mod(ir_node *n) { - tarval *tv = value_of(n); ir_mode *mode = get_Mod_resmode(n); - ir_node *value = n; + ir_node *a = get_Mod_left(n); + ir_node *b = get_Mod_right(n); + ir_node *value; + tarval *tv; + + if (is_Const(b) && is_const_Phi(a)) { + /* check for Div(Phi, Const) */ + value = apply_binop_on_phi(a, get_Const_tarval(b), tarval_mod, mode, 0); + if (value) { + DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI); + goto make_tuple; + } + } + else if (is_Const(a) && is_const_Phi(b)) { + /* check for Div(Const, Phi) */ + value = apply_binop_on_phi(b, get_Const_tarval(a), tarval_mod, mode, 1); + if (value) { + DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI); + goto make_tuple; + } + } + else if (is_const_Phi(a) && is_const_Phi(b)) { + /* check for Div(Phi, Phi) */ + value = apply_binop_on_2_phis(a, b, tarval_mod, mode); + if (value) { + DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI); + goto make_tuple; + } + } + value = n; + tv = value_of(n); if (tv != tarval_bad) { value = new_Const(get_tarval_mode(tv), tv); @@ -2628,14 +2666,48 @@ static ir_node *transform_node_DivMod(ir_node *n) { ir_node *a = get_DivMod_left(n); ir_node *b = get_DivMod_right(n); ir_mode *mode = get_DivMod_resmode(n); - tarval *ta = value_of(a); - tarval *tb = value_of(b); + tarval *ta, *tb; int evaluated = 0; + ir_node *va, *vb; + + if (is_Const(b) && is_const_Phi(a)) { + /* check for Div(Phi, Const) */ + va = apply_binop_on_phi(a, get_Const_tarval(b), tarval_div, mode, 0); + vb = apply_binop_on_phi(a, get_Const_tarval(b), tarval_mod, mode, 0); + if (va && vb) { + DBG_OPT_ALGSIM0(n, va, FS_OPT_CONST_PHI); + DBG_OPT_ALGSIM0(n, vb, FS_OPT_CONST_PHI); + goto make_tuple; + } + } + else if (is_Const(a) && is_const_Phi(b)) { + /* check for Div(Const, Phi) */ + va = apply_binop_on_phi(b, get_Const_tarval(a), tarval_div, mode, 1); + vb = apply_binop_on_phi(b, get_Const_tarval(a), tarval_mod, mode, 1); + if (va && vb) { + DBG_OPT_ALGSIM0(n, va, FS_OPT_CONST_PHI); + DBG_OPT_ALGSIM0(n, vb, FS_OPT_CONST_PHI); + goto make_tuple; + } + } + else if (is_const_Phi(a) && is_const_Phi(b)) { + /* check for Div(Phi, Phi) */ + va = apply_binop_on_2_phis(a, b, tarval_div, mode); + vb = apply_binop_on_2_phis(a, b, tarval_mod, mode); + if (va && vb) { + DBG_OPT_ALGSIM0(n, va, FS_OPT_CONST_PHI); + DBG_OPT_ALGSIM0(n, vb, FS_OPT_CONST_PHI); + goto make_tuple; + } + } + ta = value_of(a); + tb = value_of(b); if (tb != tarval_bad) { if (tb == get_mode_one(get_tarval_mode(tb))) { - b = new_Const(mode, get_mode_null(mode)); - DBG_OPT_CSTEVAL(n, b); + va = a; + vb = new_Const(mode, get_mode_null(mode)); + DBG_OPT_CSTEVAL(n, vb); goto make_tuple; } else if (ta != tarval_bad) { tarval *resa, *resb; @@ -2644,28 +2716,30 @@ 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! */ - a = new_Const(mode, resa); - b = new_Const(mode, resb); - DBG_OPT_CSTEVAL(n, a); - DBG_OPT_CSTEVAL(n, b); + va = new_Const(mode, resa); + vb = new_Const(mode, 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)) { - a = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), a, mode); - b = new_Const(mode, get_mode_null(mode)); - DBG_OPT_CSTEVAL(n, a); - DBG_OPT_CSTEVAL(n, b); + va = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), a, mode); + vb = new_Const(mode, get_mode_null(mode)); + DBG_OPT_CSTEVAL(n, va); + DBG_OPT_CSTEVAL(n, vb); goto make_tuple; } else { /* Try architecture dependent optimization */ - arch_dep_replace_divmod_by_const(&a, &b, n); - evaluated = a != NULL; + va = a; + vb = b; + arch_dep_replace_divmod_by_const(&va, &vb, n); + evaluated = va != NULL; } } else if (a == b) { if (value_not_zero(a, &dummy)) { /* a/a && a != 0 */ - a = new_Const(mode, get_mode_one(mode)); - b = new_Const(mode, get_mode_null(mode)); - DBG_OPT_CSTEVAL(n, a); - DBG_OPT_CSTEVAL(n, b); + va = new_Const(mode, get_mode_one(mode)); + vb = new_Const(mode, get_mode_null(mode)); + DBG_OPT_CSTEVAL(n, va); + DBG_OPT_CSTEVAL(n, vb); goto make_tuple; } else { /* BEWARE: it is NOT possible to optimize a/a to 1, as this may cause a exception */ @@ -2673,7 +2747,7 @@ static ir_node *transform_node_DivMod(ir_node *n) { } } else if (ta == get_mode_null(mode) && value_not_zero(b, &dummy)) { /* 0 / non-Const = 0 */ - b = a; + vb = va = a; goto make_tuple; } @@ -2691,8 +2765,8 @@ make_tuple: set_Tuple_pred(n, pn_DivMod_M, mem); set_Tuple_pred(n, pn_DivMod_X_regular, new_r_Jmp(current_ir_graph, blk)); set_Tuple_pred(n, pn_DivMod_X_except, new_Bad()); /* no exception */ - set_Tuple_pred(n, pn_DivMod_res_div, a); - set_Tuple_pred(n, pn_DivMod_res_mod, b); + set_Tuple_pred(n, pn_DivMod_res_div, va); + set_Tuple_pred(n, pn_DivMod_res_mod, vb); } return n; @@ -2863,7 +2937,8 @@ static ir_node *transform_bitwise_distributive(ir_node *n, ir_node *b_left = get_binop_left(b); ir_node *b_right = get_binop_right(b); ir_node *c = NULL; - ir_node *op1, *op2; + ir_node *op1 = NULL; + ir_node *op2 = NULL; if (is_op_commutative(op)) { if (a_left == b_left) { @@ -4593,6 +4668,12 @@ static ir_node *transform_node_Conv(ir_node *n) { return c; } } + + 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); + } + return n; } /* transform_node_Conv */