X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;ds=sidebyside;f=ir%2Fopt%2Fopt_osr.c;h=3253615ae658a8b6943c2e311f8d60db248c79d7;hb=51ae0985ed587b82ae03ab2f752b5a9917a309b2;hp=689e4a5237032eee13f98077e803184f0f385a20;hpb=037b862d722d4392d020639e00d4018c072f3fe2;p=libfirm diff --git a/ir/opt/opt_osr.c b/ir/opt/opt_osr.c index 689e4a523..3253615ae 100644 --- a/ir/opt/opt_osr.c +++ b/ir/opt/opt_osr.c @@ -53,13 +53,17 @@ #include "array.h" #include "firmstat.h" #include "xmalloc.h" +#include "error.h" /** The debug handle. */ DEBUG_ONLY(static firm_dbg_module_t *dbg;) /** A scc. */ typedef struct scc { - ir_node *head; /**< the head of the list */ + ir_node *head; /**< the head of the list */ + tarval *init; /**< the init value iff only one exists. */ + tarval *incr; /**< the induction variable increment if only a single const exists. */ + unsigned code; /**< == iro_Add if +incr, iro_Sub if -incr, 0 if not analysed, iro_Bad else */ } scc; /** A node entry */ @@ -168,6 +172,14 @@ static node_entry *get_irn_ne(ir_node *irn, iv_env *env) { return e; } +/** + * Gets the scc from an IV. + */ +static scc *get_iv_scc(ir_node *iv, iv_env *env) { + node_entry *e = get_irn_ne(iv, env); + return e->pscc; +} + /** * Check if irn is an IV. * @@ -294,7 +306,7 @@ static ir_node *do_apply(ir_opcode code, dbg_info *db, ir_node *op1, ir_node *op result = new_rd_Sub(db, irg, block, op1, op2, mode); break; default: - assert(0); + panic("Unsupported opcode"); result = NULL; } return result; @@ -311,24 +323,25 @@ static ir_node *do_apply(ir_opcode code, dbg_info *db, ir_node *op1, ir_node *op * * @return the newly created node */ -static ir_node *apply(ir_node *orig, ir_node *op1, ir_node *op2, iv_env *env) { +static ir_node *apply(ir_node *header, ir_node *orig, ir_node *op1, ir_node *op2, iv_env *env) { ir_opcode code = get_irn_opcode(orig); ir_node *result = search(code, op1, op2, env); - if (! result) { + if (result == NULL) { dbg_info *db = get_irn_dbg_info(orig); ir_node *op1_header = get_irn_ne(op1, env)->header; ir_node *op2_header = get_irn_ne(op2, env)->header; - if (op1_header != NULL && is_rc(op2, op1_header)) { + if (op1_header == header && is_rc(op2, op1_header)) { result = reduce(orig, op1, op2, env); } - else if (op2_header != NULL && is_rc(op1, op2_header)) { + else if (op2_header == header && is_rc(op1, op2_header)) { result = reduce(orig, op2, op1, env); } else { result = do_apply(code, db, op1, op2, get_irn_mode(orig)); - get_irn_ne(result, env)->header = NULL; } + get_irn_ne(result, env)->header = NULL; + } } return result; } @@ -348,7 +361,7 @@ static ir_node *reduce(ir_node *orig, ir_node *iv, ir_node *rc, iv_env *env) { ir_opcode code = get_irn_opcode(orig); ir_node *result = search(code, iv, rc, env); - if (! result) { + if (result == NULL) { node_entry *e, *iv_e; int i, n; ir_mode *mode = get_irn_mode(orig); @@ -379,7 +392,7 @@ static ir_node *reduce(ir_node *orig, ir_node *iv, ir_node *rc, iv_env *env) { if (e->header == iv_e->header) o = reduce(orig, o, rc, env); else if (is_Phi(result) || code == iro_Mul) - o = apply(orig, o, rc, env); + o = apply(iv_e->header, orig, o, rc, env); set_irn_n(result, i, o); } } @@ -390,6 +403,42 @@ static ir_node *reduce(ir_node *orig, ir_node *iv, ir_node *rc, iv_env *env) { return result; } +/** + * Update the scc for a newly created IV. + */ +static void update_scc(ir_node *iv, node_entry *e, iv_env *env) { + scc *pscc = e->pscc; + ir_node *header = e->header; + waitq *wq = new_waitq(); + + DB((dbg, LEVEL_2, " Creating SCC for new an induction variable:\n ")); + pscc->head = NULL; + waitq_put(wq, iv); + do { + ir_node *irn = waitq_get(wq); + node_entry *ne = get_irn_ne(irn, env); + int i; + + ne->pscc = pscc; + ne->next = pscc->head; + pscc->head = irn; + DB((dbg, LEVEL_2, " %+F,", irn)); + + for (i = get_irn_arity(irn) - 1; i >= 0; --i) { + ir_node *pred = get_irn_n(irn, i); + node_entry *pe = get_irn_ne(pred, env); + + if (pe->header == header && pe->pscc == NULL) { + /* set the pscc here to ensure that the node is NOT enqueued another time */ + pe->pscc = pscc; + waitq_put(wq, pred); + } + } + } while (! waitq_empty(wq)); + del_waitq(wq); + DB((dbg, LEVEL_2, "\n")); +} + /** * The Replace operation. * @@ -405,19 +454,23 @@ static int replace(ir_node *irn, ir_node *iv, ir_node *rc, iv_env *env) { result = reduce(irn, iv, rc, env); if (result != irn) { - node_entry *e, *iv_e; + node_entry *e; hook_strength_red(current_ir_graph, irn); exchange(irn, result); e = get_irn_ne(result, env); - iv_e = get_irn_ne(iv, env); - e->header = iv_e->header; + if (e->pscc == NULL) { + e->pscc = obstack_alloc(&env->obst, sizeof(*e->pscc)); + memset(e->pscc, 0, sizeof(*e->pscc)); + update_scc(result, e, env); + } ++env->replaced; return 1; } return 0; } +#if 0 /** * check if a given node is a mul with 2, 4, 8 */ @@ -443,52 +496,109 @@ static int is_x86_shift_const(ir_node *mul) { } return 0; } +#endif + +/** + * Check if an IV represents a counter with constant limits. + */ +static int is_counter_iv(ir_node *iv, iv_env *env) { + node_entry *e = get_irn_ne(iv, env); + scc *pscc = e->pscc; + ir_node *have_init = NULL; + ir_node *have_incr = NULL; + ir_opcode code = iro_Bad; + ir_node *irn; + + if (pscc->code != 0) { + /* already analysed */ + return pscc->code != iro_Bad; + } + + pscc->code = iro_Bad; + for (irn = pscc->head; irn != NULL; irn = e->next) { + if (is_Add(irn)) { + if (have_incr != NULL) + return 0; + + have_incr = get_Add_right(irn); + if (! is_Const(have_incr)) { + have_incr = get_Add_left(irn); + if (! is_Const(have_incr)) + return 0; + } + code = iro_Add; + } else if (is_Sub(irn)) { + if (have_incr != NULL) + return 0; + + have_incr = get_Sub_right(irn); + if (! is_Const(have_incr)) + return 0; + code = iro_Sub; + } else if (is_Phi(irn)) { + int i; + + for (i = get_Phi_n_preds(irn) - 1; i >= 0; --i) { + ir_node *pred = get_Phi_pred(irn, i); + node_entry *ne = get_irn_ne(pred, env); + + if (ne->header == e->header) + continue; + if (have_init != NULL) + return 0; + have_init = pred; + if (! is_Const(pred)) + return 0; + } + } else + return 0; + e = get_irn_ne(irn, env); + } + pscc->init = get_Const_tarval(have_init); + pscc->incr = get_Const_tarval(have_incr); + pscc->code = code; + return code != iro_Bad; +} /** * Check the users of an induction variable for register pressure. */ static int check_users_for_reg_pressure(ir_node *iv, iv_env *env) { ir_node *irn, *header; - node_entry *e; ir_node *have_user = NULL; ir_node *have_cmp = NULL; - waitq *wq = new_waitq(); + node_entry *e = get_irn_ne(iv, env); + scc *pscc = e->pscc; - e = get_irn_ne(iv, env); header = e->header; - waitq_put(wq, iv); - - do { + for (irn = pscc->head; irn != NULL; irn = e->next) { const ir_edge_t *edge; - irn = waitq_get(wq); foreach_out_edge(irn, edge) { ir_node *user = get_edge_src_irn(edge); node_entry *ne = get_irn_ne(user, env); if (e->header == ne->header) { /* found user from the same IV */ - if (user != iv) - waitq_put(wq, user); continue; } if (is_Cmp(user)) { if (have_cmp != NULL) { /* more than one cmp, for now end here */ - goto fail; + return 0; } have_cmp = user; } else { /* user is a real user of the IV */ if (have_user != NULL) { /* found the second user */ - goto fail; + return 0; } have_user = user; } } - } while (! waitq_empty(wq)); - del_waitq(wq); + e = get_irn_ne(irn, env); + } if (have_user == NULL) { /* no user, ignore */ @@ -502,12 +612,19 @@ static int check_users_for_reg_pressure(ir_node *iv, iv_env *env) { /* * We found one user AND at least one cmp. * We should check here if we can transform the Cmp. - * For now do nothing ... + * + * For now our capabilities for doing linear function test + * are limited, so check if the iv has the right form: Only ONE + * phi, only one Add/Sub with a Const + */ + if (! is_counter_iv(iv, env)) + return 0; + + /* + * Ok, we have only one increment AND it is a Const, we might be able + * to do a linear function test replacement, so go on. */ return 1; -fail: - del_waitq(wq); - return 0; } /** @@ -548,11 +665,6 @@ static int check_replace(ir_node *irn, iv_env *env) { if (! check_users_for_reg_pressure(iv, env)) return 0; } - /* check for x86 constants */ - if (env->flags & osr_flag_ignore_x86_shift) - if (is_x86_shift_const(irn)) - return 0; - return replace(irn, iv, rc, env); } break; @@ -859,7 +971,7 @@ static void dfs(ir_node *irn, iv_env *env) scc *pscc = obstack_alloc(&env->obst, sizeof(*pscc)); ir_node *x; - pscc->head = NULL; + memset(pscc, 0, sizeof(*pscc)); do { node_entry *e; @@ -886,7 +998,7 @@ static void do_dfs(ir_graph *irg, iv_env *env) { ir_node *end = get_irg_end(irg); int i, n; - set_using_irn_visited(irg); + ir_reserve_resources(irg, IR_RESOURCE_IRN_VISITED); current_ir_graph = irg; inc_irg_visited(irg); @@ -903,7 +1015,7 @@ static void do_dfs(ir_graph *irg, iv_env *env) { dfs(ka, env); } - clear_using_irn_visited(irg); + ir_free_resources(irg, IR_RESOURCE_IRN_VISITED); current_ir_graph = rem; } @@ -943,7 +1055,8 @@ static ir_node *followEdges(ir_node *irn, iv_env *env) { * Return NULL if the transformation cannot be done safely without * an Overflow. * - * @param rc the IV node that should be translated + * @param iv the induction variable + * @param rc the constant that should be translated * @param e the LFTR edge * @param env the IV environment * @@ -954,10 +1067,16 @@ static ir_node *followEdges(ir_node *irn, iv_env *env) { * In the current implementation only the last edge is stored, so * only one chain exists. That's why we might miss some opportunities. */ -static ir_node *applyOneEdge(ir_node *rc, LFTR_edge *e, iv_env *env) { +static ir_node *applyOneEdge(ir_node *iv, ir_node *rc, LFTR_edge *e, iv_env *env) { if (env->flags & osr_flag_lftr_with_ov_check) { - tarval *tv_l, *tv_r, *tv; + tarval *tv_l, *tv_r, *tv, *tv_init, *tv_incr; tarval_int_overflow_mode_t ovmode; + scc *pscc; + + if (! is_counter_iv(iv, env)) { + DB((dbg, LEVEL_4, " not counter IV")); + return NULL; + } /* overflow can only be decided for Consts */ if (! is_Const(e->rc)) { @@ -971,26 +1090,48 @@ static ir_node *applyOneEdge(ir_node *rc, LFTR_edge *e, iv_env *env) { ovmode = tarval_get_integer_overflow_mode(); tarval_set_integer_overflow_mode(TV_OVERFLOW_BAD); + pscc = get_iv_scc(iv, env); + tv_incr = pscc->incr; + tv_init = pscc->init; + + /* + * Check that no overflow occurs: + * init must be transformed without overflow + * the new rc must be transformed without overflow + * rc +/- incr must be possible without overflow + */ switch (e->code) { case iro_Mul: - tv = tarval_mul(tv_l, tv_r); + tv = tarval_mul(tv_l, tv_r); + tv_init = tarval_mul(tv_init, tv_r); + tv_incr = tarval_mul(tv_incr, tv_r); DB((dbg, LEVEL_4, " * %+F", tv_r)); break; case iro_Add: - tv = tarval_add(tv_l, tv_r); + tv = tarval_add(tv_l, tv_r); + tv_init = tarval_add(tv_init, tv_r); DB((dbg, LEVEL_4, " + %+F", tv_r)); break; case iro_Sub: - tv = tarval_sub(tv_l, tv_r); + tv = tarval_sub(tv_l, tv_r, NULL); + tv_init = tarval_sub(tv_init, tv_r, NULL); DB((dbg, LEVEL_4, " - %+F", tv_r)); break; default: - assert(0); + panic("Unsupported opcode"); tv = tarval_bad; } + + if (pscc->code == iro_Add) { + tv = tarval_add(tv, tv_incr); + } else { + assert(pscc->code == iro_Sub); + tv = tarval_sub(tv, tv_incr, NULL); + } + tarval_set_integer_overflow_mode(ovmode); - if (tv == tarval_bad) { + if (tv == tarval_bad || tv_init == tarval_bad) { DB((dbg, LEVEL_4, " = OVERFLOW")); return NULL; } @@ -1013,10 +1154,12 @@ static ir_node *applyOneEdge(ir_node *rc, LFTR_edge *e, iv_env *env) { * if the translation was not possible */ static ir_node *applyEdges(ir_node *iv, ir_node *rc, iv_env *env) { - ir_node *irn = iv; - if (env->flags & osr_flag_lftr_with_ov_check) { /* overflow can only be decided for Consts */ + if (! is_counter_iv(iv, env)) { + DB((dbg, LEVEL_4, "not counter IV\n", rc)); + return NULL; + } if (! is_Const(rc)) { DB((dbg, LEVEL_4, " = UNKNOWN (%+F)\n", rc)); return NULL; @@ -1024,11 +1167,11 @@ static ir_node *applyEdges(ir_node *iv, ir_node *rc, iv_env *env) { DB((dbg, LEVEL_4, "%+F", get_Const_tarval(rc))); } - for (irn = iv; rc;) { - LFTR_edge *e = LFTR_find(irn, env); + for (; rc;) { + LFTR_edge *e = LFTR_find(iv, env); if (e) { - rc = applyOneEdge(rc, e, env); - irn = e->dst; + rc = applyOneEdge(iv, rc, e, env); + iv = e->dst; } else break; @@ -1047,7 +1190,7 @@ static void do_lftr(ir_node *cmp, void *ctx) { ir_node *iv, *rc; ir_node *nleft = NULL, *nright = NULL; - if (get_irn_op(cmp) != op_Cmp) + if (!is_Cmp(cmp)) return; left = get_Cmp_left(cmp); @@ -1121,7 +1264,6 @@ void opt_osr(ir_graph *irg, unsigned flags) { current_ir_graph = irg; FIRM_DBG_REGISTER(dbg, "firm.opt.osr"); - firm_dbg_set_mask(dbg, -1); DB((dbg, LEVEL_1, "Doing Operator Strength Reduction for %+F\n", irg)); @@ -1156,7 +1298,8 @@ void opt_osr(ir_graph *irg, unsigned flags) { if (env.replaced) { /* try linear function test replacements */ - lftr(irg, &env); + //lftr(irg, &env); // currently buggy :-( + (void) lftr; set_irg_outs_inconsistent(irg); DB((dbg, LEVEL_1, "Replacements: %u + %u (lftr)\n\n", env.replaced, env.lftr_replaced));