X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=ir%2Fana%2Fvrp.c;h=21c871ff5394fc4f1434c7fe6e4f2e491531c18d;hb=819e65a153b40fcec250c779dfab17b63ac0441d;hp=6a623f08924b1f2947e67404399a425332deeb93;hpb=ac58418f84f562871be5781e8530a6857b0b810f;p=libfirm diff --git a/ir/ana/vrp.c b/ir/ana/vrp.c index 6a623f089..21c871ff5 100644 --- a/ir/ana/vrp.c +++ b/ir/ana/vrp.c @@ -19,9 +19,9 @@ /** * @file - * @brief analyze graph to provide value range information - * @author Jonas Fietz - * @version $Id$ + * @brief analyze graph to provide value range information + * @author Jonas Fietz + * @version $Id$ */ #include "config.h" @@ -38,15 +38,15 @@ #include "irop.h" #include "pdeq.h" #include "irphase_t.h" +#include "bitset.h" #include "debug.h" -static char v; -static void *VISITED = &v; +DEBUG_ONLY(static firm_dbg_module_t *dbg;) -struct vrp_env_t { - waitq *workqueue; - DEBUG_ONLY(firm_dbg_module_t *dbg); -}; +typedef struct vrp_env_t { + waitq *workqueue; + bitset_t *visited; +} vrp_env_t; static vrp_attr *get_vrp_attr(const ir_node *node) { @@ -55,10 +55,10 @@ static vrp_attr *get_vrp_attr(const ir_node *node) static int vrp_update_node(ir_node *node) { - tarval *new_bits_set = get_tarval_bad(); - tarval *new_bits_not_set = get_tarval_bad(); - tarval *new_range_bottom = get_tarval_bad(); - tarval *new_range_top = get_tarval_bad(); + ir_tarval *new_bits_set = get_tarval_bad(); + ir_tarval *new_bits_not_set = get_tarval_bad(); + ir_tarval *new_range_bottom = get_tarval_bad(); + ir_tarval *new_range_top = get_tarval_bad(); enum range_types new_range_type = VRP_UNDEFINED; int something_changed = 0; vrp_attr *vrp; @@ -73,7 +73,7 @@ static int vrp_update_node(ir_node *node) switch (get_irn_opcode(node)) { case iro_Const: { - tarval *tv = get_Const_tarval(node); + ir_tarval *tv = get_Const_tarval(node); new_bits_set = tv; new_bits_not_set = tv; new_range_bottom = tv; @@ -97,7 +97,7 @@ static int vrp_update_node(ir_node *node) case iro_Add: { int overflow_top, overflow_bottom; - tarval *new_top, *new_bottom; + ir_tarval *new_top, *new_bottom; const vrp_attr *vrp_left, *vrp_right; vrp_left = get_vrp_attr(get_Add_left(node)); vrp_right = get_vrp_attr(get_Add_right(node)); @@ -129,7 +129,7 @@ static int vrp_update_node(ir_node *node) case iro_Sub: { int overflow_top, overflow_bottom; - tarval *new_top, *new_bottom; + ir_tarval *new_top, *new_bottom; const vrp_attr *vrp_left, *vrp_right; vrp_left = get_vrp_attr(get_Sub_left(node)); vrp_right = get_vrp_attr(get_Sub_right(node)); @@ -171,26 +171,23 @@ static int vrp_update_node(ir_node *node) } case iro_Rotl: { - const vrp_attr *vrp_left, *vrp_right; + const vrp_attr *vrp_left; const ir_node *right = get_Rotl_right(node); vrp_left = get_vrp_attr(get_Rotl_left(node)); - vrp_right = get_vrp_attr(get_Rotl_right(node)); /* We can only compute this if the right value is a constant*/ if (is_Const(right)) { - tarval *bits_set, *bits_not_set; - bits_set = tarval_rotl(vrp_left->bits_set, get_Const_tarval(right)); - bits_not_set = tarval_rotl(vrp_left->bits_not_set, get_Const_tarval(right)); + new_bits_set = tarval_rotl(vrp_left->bits_set, get_Const_tarval(right)); + new_bits_not_set = tarval_rotl(vrp_left->bits_not_set, get_Const_tarval(right)); } break; } case iro_Shl: { - const vrp_attr *vrp_left, *vrp_right; + const vrp_attr *vrp_left; const ir_node *right = get_Shl_right(node); vrp_left = get_vrp_attr(get_Shl_left(node)); - vrp_right = get_vrp_attr(get_Shl_right(node)); /* We can only compute this if the right value is a constant*/ if (is_Const(right)) { @@ -201,11 +198,10 @@ static int vrp_update_node(ir_node *node) } case iro_Shr: { - const vrp_attr *vrp_left, *vrp_right; + const vrp_attr *vrp_left; const ir_node *right = get_Shr_right(node); vrp_left = get_vrp_attr(get_Shr_left(node)); - vrp_right = get_vrp_attr(get_Shr_right(node)); /* We can only compute this if the right value is a constant*/ if (is_Const(right)) { @@ -216,11 +212,10 @@ static int vrp_update_node(ir_node *node) } case iro_Shrs: { - const vrp_attr *vrp_left, *vrp_right; + const vrp_attr *vrp_left; const ir_node *right = get_Shrs_right(node); vrp_left = get_vrp_attr(get_Shrs_left(node)); - vrp_right = get_vrp_attr(get_Shrs_right(node)); /* We can only compute this if the right value is a constant*/ if (is_Const(right)) { @@ -283,29 +278,31 @@ static int vrp_update_node(ir_node *node) new_bits_set = tarval_and( new_bits_not_set, tarval_convert_to(vrp_pred->bits_set, new_mode)); - if (tarval_cmp(vrp_pred->range_top, get_mode_max(new_mode)) == pn_Cmp_Le) { + /* Matze: TODO, BUGGY, tarval_cmp never returns ir_relation_less_equal */ + if (tarval_cmp(vrp_pred->range_top, get_mode_max(new_mode)) == ir_relation_less_equal) { vrp->range_top = vrp_pred->range_top; } - if (tarval_cmp(vrp_pred->range_bottom, get_mode_min(new_mode)) == pn_Cmp_Ge) { + /* Matze: TODO, BUGGY, tarval_cmp never returns ir_relation_greater_equal */ + if (tarval_cmp(vrp_pred->range_bottom, get_mode_min(new_mode)) == ir_relation_greater_equal) { vrp->range_bottom = vrp_pred->range_bottom; } break; } case iro_Confirm: { - const pn_Cmp cmp = get_Confirm_cmp(node); - const ir_node *bound = get_Confirm_bound(node); + const ir_relation relation = get_Confirm_relation(node); + const ir_node *bound = get_Confirm_bound(node); - if (cmp == pn_Cmp_Lg) { + if (relation == ir_relation_less_greater) { /** @todo: Handle non-Const bounds */ if (is_Const(bound)) { new_range_type = VRP_ANTIRANGE; new_range_top = get_Const_tarval(bound); new_range_bottom = get_Const_tarval(bound); } - } else if (cmp == pn_Cmp_Le) { + } else if (relation == ir_relation_less_equal) { if (is_Const(bound)) { new_range_type = VRP_RANGE; new_range_top = get_Const_tarval(bound); @@ -319,7 +316,7 @@ static int vrp_update_node(ir_node *node) /* combine all ranges*/ int num = get_Phi_n_preds(node); - pn_Cmp cmp; + ir_relation relation; int i; const ir_node *pred = get_Phi_pred(node,0); @@ -337,12 +334,12 @@ static int vrp_update_node(ir_node *node) vrp_pred = get_vrp_attr(pred); if (new_range_type == VRP_RANGE && vrp_pred->range_type == VRP_RANGE) { - cmp = tarval_cmp(new_range_top, vrp_pred->range_top); - if (cmp == pn_Cmp_Lt) { + relation = tarval_cmp(new_range_top, vrp_pred->range_top); + if (relation == ir_relation_less) { new_range_top = vrp_pred->range_top; } - cmp = tarval_cmp(new_range_bottom, vrp_pred->range_bottom); - if (cmp == pn_Cmp_Gt) { + relation = tarval_cmp(new_range_bottom, vrp_pred->range_bottom); + if (relation == ir_relation_greater) { new_range_bottom = vrp_pred->range_bottom; } } else { @@ -366,11 +363,11 @@ static int vrp_update_node(ir_node *node) is_Abs(node) is_Alloc(node) is_Anchor(node) is_Borrow(node) is_Bound(node) is_Break(node) is_Builtin(node) is_Call(node) is_Carry(node) is_Cast(node) is_Cmp(node) is_Cond(node) - is_CopyB(node) is_Div(node) is_DivMod(node) is_Dummy(node) + is_CopyB(node) is_Div(node) is_Dummy(node) is_End(node) is_Free(node) is_IJmp(node) is_InstOf(node) is_Jmp(node) is_Load(node) is_Minus(node) is_Mod(node) is_Mul(node) is_Mulh(node) is_Mux(node) is_NoMem(node) - is_Pin(node) is_Proj(node) is_Quot(node) + is_Pin(node) is_Proj(node) is_Raise(node) is_Return(node) is_Sel(node) is_Start(node) is_Store(node) is_SymConst(node) is_Sync(node) is_Tuple(node) */ @@ -395,7 +392,7 @@ static int vrp_update_node(ir_node *node) /* Merge the newly calculated values with those that might already exist*/ if (new_bits_set != tarval_bad) { new_bits_set = tarval_or(new_bits_set, vrp->bits_set); - if (tarval_cmp(new_bits_set, vrp->bits_set) != pn_Cmp_Eq) { + if (tarval_cmp(new_bits_set, vrp->bits_set) != ir_relation_equal) { something_changed = 1; vrp->bits_set = new_bits_set; } @@ -403,7 +400,7 @@ static int vrp_update_node(ir_node *node) if (new_bits_not_set != tarval_bad) { new_bits_not_set = tarval_and(new_bits_not_set, vrp->bits_not_set); - if (tarval_cmp(new_bits_not_set, vrp->bits_not_set) != pn_Cmp_Eq) { + if (tarval_cmp(new_bits_not_set, vrp->bits_not_set) != ir_relation_equal) { something_changed = 1; vrp->bits_not_set = new_bits_not_set; } @@ -418,11 +415,11 @@ static int vrp_update_node(ir_node *node) } else if (vrp->range_type == VRP_RANGE) { if (new_range_type == VRP_RANGE) { - if (tarval_cmp(vrp->range_bottom, new_range_bottom) == pn_Cmp_Lt) { + if (tarval_cmp(vrp->range_bottom, new_range_bottom) == ir_relation_less) { something_changed = 1; vrp->range_bottom = new_range_bottom; } - if (tarval_cmp(vrp->range_top, new_range_top) == pn_Cmp_Gt) { + if (tarval_cmp(vrp->range_top, new_range_top) == ir_relation_greater) { something_changed = 1; vrp->range_top = new_range_top; } @@ -431,13 +428,13 @@ static int vrp_update_node(ir_node *node) if (new_range_type == VRP_ANTIRANGE) { /* if they are overlapping, cut the range.*/ /* TODO: Maybe we can preserve more information here*/ - if (tarval_cmp(vrp->range_bottom, new_range_top) == pn_Cmp_Gt && - tarval_cmp(vrp->range_bottom, new_range_bottom) == pn_Cmp_Gt) { + if (tarval_cmp(vrp->range_bottom, new_range_top) == ir_relation_greater && + tarval_cmp(vrp->range_bottom, new_range_bottom) == ir_relation_greater) { something_changed = 1; vrp->range_bottom = new_range_top; - } else if (tarval_cmp(vrp->range_top, new_range_bottom) == pn_Cmp_Gt && - tarval_cmp(vrp->range_top, new_range_top) == pn_Cmp_Lt) { + } else if (tarval_cmp(vrp->range_top, new_range_bottom) == ir_relation_greater && + tarval_cmp(vrp->range_top, new_range_top) == ir_relation_less) { something_changed = 1; vrp->range_top = new_range_bottom; } @@ -448,22 +445,22 @@ static int vrp_update_node(ir_node *node) } } else if (vrp->range_type == VRP_ANTIRANGE) { if (new_range_type == VRP_ANTIRANGE) { - if (tarval_cmp(vrp->range_bottom, new_range_bottom) == pn_Cmp_Gt) { + if (tarval_cmp(vrp->range_bottom, new_range_bottom) == ir_relation_greater) { something_changed = 1; vrp->range_bottom = new_range_bottom; } - if (tarval_cmp(vrp->range_top, new_range_top) == pn_Cmp_Lt) { + if (tarval_cmp(vrp->range_top, new_range_top) == ir_relation_less) { something_changed = 1; vrp->range_top = new_range_top; } } if (new_range_type == VRP_RANGE) { - if (tarval_cmp(vrp->range_bottom, new_range_top) == pn_Cmp_Gt) { + if (tarval_cmp(vrp->range_bottom, new_range_top) == ir_relation_greater) { something_changed = 1; vrp->range_bottom = new_range_top; } - if (tarval_cmp(vrp->range_top, new_range_bottom) == pn_Cmp_Lt) { + if (tarval_cmp(vrp->range_top, new_range_bottom) == ir_relation_less) { something_changed = 1; vrp->range_top = new_range_bottom; } @@ -477,21 +474,20 @@ static int vrp_update_node(ir_node *node) static void vrp_first_pass(ir_node *n, void *e) { - ir_node *succ; int i; - struct vrp_env_t *env = e; + vrp_env_t *env = (vrp_env_t*) e; if (is_Block(n)) return; - set_irn_link(n, VISITED); + bitset_set(env->visited, get_irn_idx(n)); vrp_update_node(n); assure_irg_outs(get_current_ir_graph()); for (i = get_irn_n_outs(n) - 1; i >=0; --i) { - succ = get_irn_out(n, i); - if (get_irn_link(succ) == VISITED) { + ir_node *succ = get_irn_out(n, i); + if (bitset_is_set(env->visited, get_irn_idx(succ))) { /* we found a loop*/ waitq_put(env->workqueue, succ); } @@ -502,10 +498,9 @@ static void *vrp_init_node(ir_phase *phase, const ir_node *n) { ir_mode *mode; vrp_attr *vrp; - struct vrp_env_t *env = phase->priv; - DBG((env->dbg, LEVEL_2, "initialized node nr: %d\n", get_irn_node_nr(n))); - vrp = phase_alloc(phase, sizeof(vrp_attr)); + DBG((dbg, LEVEL_2, "initialized node nr: %d\n", get_irn_node_nr(n))); + vrp = (vrp_attr*) phase_alloc(phase, sizeof(vrp_attr)); memset(vrp, 0, sizeof(vrp_attr)); /* Initialize the vrp information to default */ @@ -545,74 +540,75 @@ void set_vrp_data(ir_graph *irg) { ir_node *succ, *node; int i; - struct vrp_env_t *env; + vrp_env_t *env; ir_phase *phase; + FIRM_DBG_REGISTER(dbg, "ir.ana.vrp"); + assure_irg_outs(irg); /* ensure that out edges are consistent*/ phase = irg_get_phase(irg, PHASE_VRP); if (phase == NULL) { /* this is our first run */ phase = new_phase(irg, vrp_init_node); irg_register_phase(irg, PHASE_VRP, phase); - env = phase_alloc(phase, sizeof(*env)); - FIRM_DBG_REGISTER(env->dbg, "ir.ana.vrp"); + env = (vrp_env_t*) phase_alloc(phase, sizeof(*env)); phase->priv = env; } else { - env = phase->priv; + env = (vrp_env_t*) phase->priv; } env->workqueue = new_waitq(); + env->visited = bitset_malloc(get_irg_last_idx(irg)); irg_walk_graph(irg, NULL, vrp_first_pass, env); + bitset_free(env->visited); /* while there are entries in the worklist, continue*/ while (!waitq_empty(env->workqueue)) { - node = waitq_get(env->workqueue); + node = (ir_node*) waitq_get(env->workqueue); if (vrp_update_node(node)) { /* if something changed, add successors to worklist*/ - for (i = get_irn_n_outs(node) - 1; i >=0; --i) { + for (i = get_irn_n_outs(node) - 1; i >= 0; --i) { succ = get_irn_out(node, i); - waitq_put(env->workqueue, node); + waitq_put(env->workqueue, succ); } } } del_waitq(env->workqueue); } - ir_graph_pass_t *set_vrp_pass(const char *name) { return def_graph_pass(name ? name : "set_vrp", set_vrp_data); } -pn_Cmp vrp_cmp(const ir_node *left, const ir_node *right) +ir_relation vrp_cmp(const ir_node *left, const ir_node *right) { vrp_attr *vrp_left, *vrp_right; vrp_left = vrp_get_info(left); vrp_right = vrp_get_info(right); - if (!vrp_left || !vrp_right) { - return pn_Cmp_False; - } + if (!vrp_left || !vrp_right) + return ir_relation_true; if (vrp_left->range_type == VRP_RANGE && vrp_right->range_type == VRP_RANGE) { - if (tarval_cmp(vrp_left->range_top, vrp_right->range_bottom) == pn_Cmp_Lt) { - return pn_Cmp_Lt; + if (tarval_cmp(vrp_left->range_top, vrp_right->range_bottom) == ir_relation_less) { + return ir_relation_less; } - if (tarval_cmp(vrp_left->range_bottom, vrp_right->range_top) == pn_Cmp_Gt) { - return pn_Cmp_Gt; + if (tarval_cmp(vrp_left->range_bottom, vrp_right->range_top) == ir_relation_greater) { + return ir_relation_greater; } } if (!tarval_is_null(tarval_and(vrp_left->bits_set, tarval_not(vrp_right->bits_not_set))) || !tarval_is_null(tarval_and(tarval_not(vrp_left->bits_not_set), vrp_right->bits_set))) { - return pn_Cmp_Lg; + return ir_relation_less_greater; } - /* TODO: We can get way more information here*/ - return pn_Cmp_False; + /* TODO: We can get way more information here*/ + return ir_relation_true; } vrp_attr *vrp_get_info(const ir_node *node) @@ -626,9 +622,10 @@ vrp_attr *vrp_get_info(const ir_node *node) return NULL; } - vrp = phase_get_irn_data(phase, node); + vrp = (vrp_attr*) phase_get_irn_data(phase, node); if (vrp && vrp->valid) { return vrp; } + return NULL; }