X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=ir%2Fir%2Firopt.c;h=481f9cd661191f007cb8888de66c0bb26ad13a8e;hb=357886575cb0becb5bd9be376fde49b57edd5385;hp=ba62501a6ff7801daf77d550aa8ce7bfc9b2212c;hpb=c64fe299008a45e1bd3b90656d357a77edbb79c9;p=libfirm diff --git a/ir/ir/iropt.c b/ir/ir/iropt.c index ba62501a6..481f9cd66 100644 --- a/ir/ir/iropt.c +++ b/ir/ir/iropt.c @@ -327,6 +327,28 @@ static ir_tarval *computed_value_Not(const ir_node *n) return tarval_bad; } /* computed_value_Not */ +/** + * Tests wether a shift shifts more bits than available in the mode + */ +static bool is_oversize_shift(const ir_node *n) +{ + ir_node *count = get_binop_right(n); + ir_mode *mode = get_irn_mode(n); + ir_tarval *tv = value_of(count); + long modulo_shift; + long shiftval; + if (tv == tarval_bad) + return false; + if (!tarval_is_long(tv)) + return false; + shiftval = get_tarval_long(tv); + modulo_shift = get_mode_modulo_shift(mode); + if (shiftval < 0 || (modulo_shift > 0 && shiftval >= modulo_shift)) + return false; + + return shiftval >= (long)get_mode_size_bits(mode); +} + /** * Return the value of a Shl. */ @@ -341,6 +363,10 @@ static ir_tarval *computed_value_Shl(const ir_node *n) if ((ta != tarval_bad) && (tb != tarval_bad)) { return tarval_shl(ta, tb); } + + if (is_oversize_shift(n)) + return get_mode_null(get_irn_mode(n)); + return tarval_bad; } /* computed_value_Shl */ @@ -358,6 +384,9 @@ static ir_tarval *computed_value_Shr(const ir_node *n) if ((ta != tarval_bad) && (tb != tarval_bad)) { return tarval_shr(ta, tb); } + if (is_oversize_shift(n)) + return get_mode_null(get_irn_mode(n)); + return tarval_bad; } /* computed_value_Shr */ @@ -395,17 +424,52 @@ static ir_tarval *computed_value_Rotl(const ir_node *n) return tarval_bad; } /* computed_value_Rotl */ +bool ir_zero_when_converted(const ir_node *node, ir_mode *dest_mode) +{ + ir_mode *mode = get_irn_mode(node); + if (get_mode_arithmetic(mode) != irma_twos_complement + || get_mode_arithmetic(dest_mode) != irma_twos_complement) + return false; + + if (is_Shl(node)) { + ir_node *count = get_Shl_right(node); + if (is_Const(count)) { + ir_tarval *tv = get_Const_tarval(count); + if (tarval_is_long(tv)) { + long shiftval = get_tarval_long(tv); + long destbits = get_mode_size_bits(dest_mode); + if (shiftval >= destbits + && shiftval < (long)get_mode_modulo_shift(mode)) + return true; + } + } + } + if (is_And(node)) { + ir_node *right = get_And_right(node); + if (is_Const(right)) { + ir_tarval *tv = get_Const_tarval(right); + ir_tarval *conved = tarval_convert_to(tv, dest_mode); + return tarval_is_null(conved); + } + } + return false; +} + /** * Return the value of a Conv. */ static ir_tarval *computed_value_Conv(const ir_node *n) { - ir_node *a = get_Conv_op(n); - ir_tarval *ta = value_of(a); + ir_node *a = get_Conv_op(n); + ir_tarval *ta = value_of(a); + ir_mode *mode = get_irn_mode(n); if (ta != tarval_bad) return tarval_convert_to(ta, get_irn_mode(n)); + if (ir_zero_when_converted(a, mode)) + return get_mode_null(mode); + return tarval_bad; } /* computed_value_Conv */ @@ -661,72 +725,6 @@ static ir_op_ops *firm_set_default_computed_value(ir_opcode code, ir_op_ops *ops #undef CASE } /* firm_set_default_computed_value */ -/** - * Returns a equivalent block for another block. - * If the block has only one predecessor, this is - * the equivalent one. If the only predecessor of a block is - * the block itself, this is a dead block. - * - * If both predecessors of a block are the branches of a binary - * Cond, the equivalent block is Cond's block. - * - * If all predecessors of a block are bad or lies in a dead - * block, the current block is dead as well. - */ -static ir_node *equivalent_node_Block(ir_node *n) -{ - ir_node *oldn = n; - int n_preds; - ir_graph *irg; - - /* don't optimize labeled blocks */ - if (has_Block_entity(n)) - return n; - if (!get_Block_matured(n)) - return n; - - n_preds = get_Block_n_cfgpreds(n); - - irg = get_irn_irg(n); - - /* Straightening: a single entry Block following a single exit Block - * can be merged. */ - if (n_preds == 1) { - ir_node *pred = get_Block_cfgpred(n, 0); - - if (is_Jmp(pred)) { - ir_node *pred_block = get_nodes_block(pred); - DBG_OPT_STG(n, pred_block); - return pred_block; - } - } else if (n_preds == 2) { - /* Test whether Cond jumps twice to this block - * The more general case which more than 2 predecessors is handles - * in optimize_cf(), we handle only this special case for speed here. - */ - ir_node *a = get_Block_cfgpred(n, 0); - ir_node *b = get_Block_cfgpred(n, 1); - - if (is_Proj(a) && is_Proj(b)) { - ir_node *cond = get_Proj_pred(a); - - if (cond == get_Proj_pred(b) && is_Cond(cond) && - get_irn_mode(get_Cond_selector(cond)) == mode_b) { - /* Also a single entry Block following a single exit Block. - * Phis have twice the same operand and will be optimized away. - */ - n = get_nodes_block(cond); - DBG_OPT_IFSIM1(oldn, a, b, n); - } - } - } - - return n; -} /* equivalent_node_Block */ - -/* We do not evaluate Cond here as we replace it by a new node, a Jmp. - See transform_node_Proj_Cond(). */ - /** * Optimize operations that are commutative and have neutral 0, * so a op 0 = 0 op a = a. @@ -1576,7 +1574,6 @@ static ir_op_ops *firm_set_default_equivalent_node(ir_opcode code, ir_op_ops *op break switch (code) { - CASE(Block); CASE(Eor); CASE(Add); CASE(Shl); @@ -4714,9 +4711,11 @@ static ir_node *transform_node_shift(ir_node *n) { ir_node *left, *right; ir_mode *mode; + ir_mode *count_mode; ir_tarval *tv1, *tv2, *res; ir_node *in[2], *irn, *block; ir_graph *irg; + int modulo_shf; left = get_binop_left(n); @@ -4725,7 +4724,7 @@ static ir_node *transform_node_shift(ir_node *n) return n; right = get_binop_right(n); - tv1 = value_of(right); + tv1 = value_of(right); if (tv1 == tarval_bad) return n; @@ -4733,37 +4732,55 @@ static ir_node *transform_node_shift(ir_node *n) if (tv2 == tarval_bad) return n; - res = tarval_add(tv1, tv2); - mode = get_irn_mode(n); - irg = get_irn_irg(n); + count_mode = get_tarval_mode(tv1); + if (get_tarval_mode(tv2) != count_mode) { + /* TODO: search bigger mode or something and convert... */ + return n; + } - /* beware: a simple replacement works only, if res < modulo shift */ - if (!is_Rotl(n)) { - int modulo_shf = get_mode_modulo_shift(mode); - if (modulo_shf > 0) { - ir_tarval *modulo = new_tarval_from_long(modulo_shf, - get_tarval_mode(res)); + mode = get_irn_mode(n); + modulo_shf = get_mode_modulo_shift(mode); - assert(modulo_shf >= (int) get_mode_size_bits(mode)); + if (modulo_shf > 0) { + ir_tarval *modulo_mask = new_tarval_from_long(modulo_shf-1, count_mode); - /* shifting too much */ - if (!(tarval_cmp(res, modulo) & ir_relation_less)) { - if (is_Shrs(n)) { - ir_node *block = get_nodes_block(n); - dbg_info *dbgi = get_irn_dbg_info(n); - ir_mode *smode = get_irn_mode(right); - ir_node *cnst = new_r_Const_long(irg, smode, get_mode_size_bits(mode) - 1); - return new_rd_Shrs(dbgi, block, get_binop_left(left), cnst, mode); - } + /* I'm not so sure what happens in one complement... */ + assert(get_mode_arithmetic(count_mode) == irma_twos_complement); + /* modulo shifts should always be a power of 2 (otherwise modulo_mask + * above will be invalid) */ + assert(modulo_shf<=0 || is_po2(modulo_shf)); - return new_r_Const(irg, get_mode_null(mode)); + tv1 = tarval_and(tv1, modulo_mask); + tv2 = tarval_and(tv2, modulo_mask); + } + res = tarval_add(tv1, tv2); + irg = get_irn_irg(n); + + /* beware: a simple replacement works only, if res < modulo shift */ + if (is_Rotl(n)) { + int bits = get_mode_size_bits(mode); + ir_tarval *modulo = new_tarval_from_long(bits, count_mode); + res = tarval_mod(res, modulo); + } else { + long bits = get_mode_size_bits(mode); + ir_tarval *mode_size = new_tarval_from_long(bits, count_mode); + + /* shifting too much */ + if (!(tarval_cmp(res, mode_size) & ir_relation_less)) { + if (is_Shrs(n)) { + ir_node *block = get_nodes_block(n); + dbg_info *dbgi = get_irn_dbg_info(n); + ir_mode *smode = get_irn_mode(right); + ir_node *cnst = new_r_Const_long(irg, smode, get_mode_size_bits(mode) - 1); + return new_rd_Shrs(dbgi, block, get_binop_left(left), cnst, mode); } + + return new_r_Const(irg, get_mode_null(mode)); } - } else { - res = tarval_mod(res, new_tarval_from_long(get_mode_size_bits(mode), get_tarval_mode(res))); } /* ok, we can replace it */ + assert(modulo_shf >= (int) get_mode_size_bits(mode)); block = get_nodes_block(n); in[0] = get_binop_left(left); @@ -4774,7 +4791,7 @@ static ir_node *transform_node_shift(ir_node *n) DBG_OPT_ALGSIM0(n, irn, FS_OPT_REASSOC_SHIFT); return transform_node(irn); -} /* transform_node_shift */ +} /** * normalisation: (x & c1) >> c2 to (x >> c2) & (c1 >> c2) @@ -5082,6 +5099,16 @@ static ir_node *transform_node_Shrs(ir_node *n) ir_node *b = get_Shrs_right(n); ir_mode *mode = get_irn_mode(n); + if (is_oversize_shift(n)) { + ir_node *block = get_nodes_block(n); + dbg_info *dbgi = get_irn_dbg_info(n); + ir_mode *cmode = get_irn_mode(b); + long val = get_mode_size_bits(cmode)-1; + ir_graph *irg = get_irn_irg(n); + ir_node *cnst = new_r_Const_long(irg, cmode, val); + return new_rd_Shrs(dbgi, block, a, cnst, mode); + } + HANDLE_BINOP_PHI((eval_func) tarval_shrs, a, b, c, mode); n = transform_node_shift(n); @@ -6129,16 +6156,6 @@ ir_node *identify_remember(ir_node *n) if (value_table == NULL) return n; - if (get_opt_global_cse()) { - /* do not remember unreachable nodes */ - if (get_irg_dom_state(irg) == dom_consistent && !is_Block(n)) { - ir_node *block = get_nodes_block(n); - if (get_Block_dom_depth(block) < 0) { - return n; - } - } - } - ir_normalize_node(n); /* lookup or insert in hash table with given hash key. */ nn = (ir_node*)pset_insert(value_table, n, ir_node_hash(n));