X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=ir%2Fopt%2Fopt_osr.c;h=3175ca94d6ab093c890e3411b4c2da307bb9a815;hb=555593b0aebec433b871920acc2b0a869b072055;hp=e30307248c9f1695025740f38e70809132b76624;hpb=7c36344d22a7c306a4e216f135c974bdb9f6b943;p=libfirm diff --git a/ir/opt/opt_osr.c b/ir/opt/opt_osr.c index e30307248..3175ca94d 100644 --- a/ir/opt/opt_osr.c +++ b/ir/opt/opt_osr.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved. + * Copyright (C) 1995-2011 University of Karlsruhe. All right reserved. * * This file is part of libFirm. * @@ -22,7 +22,6 @@ * @brief Operator Strength Reduction. * @date 12.5.2006 * @author Michael Beck - * @version $Id$ * @brief * Implementation of the Operator Strength Reduction algorithm * by Keith D. Cooper, L. Taylor Simpson, Christopher A. Vick. @@ -46,10 +45,11 @@ #include "set.h" #include "tv.h" #include "hashptr.h" +#include "util.h" #include "irtools.h" #include "irloop_t.h" #include "array.h" -#include "firmstat.h" +#include "firmstat_t.h" #include "error.h" #include "irpass_t.h" @@ -58,10 +58,10 @@ DEBUG_ONLY(static firm_dbg_module_t *dbg;) /** A scc. */ typedef struct scc { - 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 */ + ir_node *head; /**< the head of the list */ + ir_tarval *init; /**< the init value iff only one exists. */ + ir_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 */ @@ -79,7 +79,7 @@ typedef struct node_entry { typedef struct iv_env { struct obstack obst; /**< an obstack for allocations */ ir_node **stack; /**< the node stack */ - int tos; /**< tos index */ + size_t tos; /**< tos index */ unsigned nextDFSnum; /**< the current DFS number */ unsigned POnum; /**< current post order number */ set *quad_map; /**< a map from (op, iv, rc) to node */ @@ -96,7 +96,7 @@ typedef struct iv_env { * An entry in the (op, node, node) -> node map. */ typedef struct quadruple_t { - ir_opcode code; /**< the opcode of the reduced operation */ + unsigned code; /**< the opcode of the reduced operation */ ir_node *op1; /**< the first operand the reduced operation */ ir_node *op2; /**< the second operand of the reduced operation */ @@ -109,7 +109,7 @@ typedef struct quadruple_t { typedef struct LFTR_edge { ir_node *src; /**< the source node */ ir_node *dst; /**< the destination node */ - ir_opcode code; /**< the opcode that must be applied */ + unsigned code; /**< the opcode that must be applied */ ir_node *rc; /**< the region const that must be applied */ } LFTR_edge; @@ -121,8 +121,8 @@ static ir_node *reduce(ir_node *orig, ir_node *iv, ir_node *rc, iv_env *env); */ static int LFTR_cmp(const void *e1, const void *e2, size_t size) { - const LFTR_edge *l1 = e1; - const LFTR_edge *l2 = e2; + const LFTR_edge *l1 = (const LFTR_edge*)e1; + const LFTR_edge *l2 = (const LFTR_edge*)e2; (void) size; return l1->src != l2->src; @@ -139,7 +139,7 @@ static LFTR_edge *LFTR_find(ir_node *src, iv_env *env) key.src = src; - return set_find(env->lftr_edges, &key, sizeof(key), HASH_PTR(src)); + return set_find(LFTR_edge, env->lftr_edges, &key, sizeof(key), hash_ptr(src)); } /* LFTR_find */ /** @@ -151,7 +151,7 @@ static LFTR_edge *LFTR_find(ir_node *src, iv_env *env) * @param rc the region const used in the transition * @param env the environment */ -static void LFTR_add(ir_node *src, ir_node *dst, ir_opcode code, ir_node *rc, iv_env *env) +static void LFTR_add(ir_node *src, ir_node *dst, unsigned code, ir_node *rc, iv_env *env) { LFTR_edge key; @@ -164,8 +164,7 @@ static void LFTR_add(ir_node *src, ir_node *dst, ir_opcode code, ir_node *rc, iv * There might be more than one edge here. This is rather bad * because we currently store only one. */ -// assert(LFTR_find(src, env) == NULL); - set_insert(env->lftr_edges, &key, sizeof(key), HASH_PTR(src)); + (void)set_insert(LFTR_edge, env->lftr_edges, &key, sizeof(key), hash_ptr(src)); } /* LFTR_add */ /** @@ -176,7 +175,7 @@ static void LFTR_add(ir_node *src, ir_node *dst, ir_opcode code, ir_node *rc, iv */ static node_entry *get_irn_ne(ir_node *irn, iv_env *env) { - node_entry *e = get_irn_link(irn); + node_entry *e = (node_entry*)get_irn_link(irn); if (e == NULL) { e = OALLOCZ(&env->obst, node_entry); @@ -229,8 +228,8 @@ static int is_rc(ir_node *irn, ir_node *header_block) */ static int quad_cmp(const void *e1, const void *e2, size_t size) { - const quadruple_t *c1 = e1; - const quadruple_t *c2 = e2; + const quadruple_t *c1 = (const quadruple_t*)e1; + const quadruple_t *c2 = (const quadruple_t*)e2; (void) size; return c1->code != c2->code || c1->op1 != c2->op1 || c1->op2 != c2->op2; @@ -246,7 +245,7 @@ static int quad_cmp(const void *e1, const void *e2, size_t size) * * @return the already reduced node or NULL if this operation is not yet reduced */ -static ir_node *search(ir_opcode code, ir_node *op1, ir_node *op2, iv_env *env) +static ir_node *search(unsigned code, ir_node *op1, ir_node *op2, iv_env *env) { quadruple_t key, *entry; @@ -254,8 +253,7 @@ static ir_node *search(ir_opcode code, ir_node *op1, ir_node *op2, iv_env *env) key.op1 = op1; key.op2 = op2; - entry = set_find(env->quad_map, &key, sizeof(key), - (code * 9) ^ HASH_PTR(op1) ^HASH_PTR(op2)); + entry = set_find(quadruple_t, env->quad_map, &key, sizeof(key), (code * 9) ^ hash_ptr(op1) ^ hash_ptr(op2)); if (entry) return entry->res; return NULL; @@ -270,7 +268,7 @@ static ir_node *search(ir_opcode code, ir_node *op1, ir_node *op2, iv_env *env) * @param result the result of the reduced operation * @param env the environment */ -static void add(ir_opcode code, ir_node *op1, ir_node *op2, ir_node *result, iv_env *env) +static void add(unsigned code, ir_node *op1, ir_node *op2, ir_node *result, iv_env *env) { quadruple_t key; @@ -279,8 +277,7 @@ static void add(ir_opcode code, ir_node *op1, ir_node *op2, ir_node *result, iv_ key.op2 = op2; key.res = result; - set_insert(env->quad_map, &key, sizeof(key), - (code * 9) ^ HASH_PTR(op1) ^HASH_PTR(op2)); + (void)set_insert(quadruple_t, env->quad_map, &key, sizeof(key), (code * 9) ^ hash_ptr(op1) ^ hash_ptr(op2)); } /* add */ /** @@ -313,7 +310,7 @@ static ir_node *find_location(ir_node *block1, ir_node *block2) * * @return the newly created node */ -static ir_node *do_apply(ir_opcode code, dbg_info *db, ir_node *op1, ir_node *op2, ir_mode *mode) +static ir_node *do_apply(unsigned code, dbg_info *db, ir_node *op1, ir_node *op2, ir_mode *mode) { ir_node *result; ir_node *block = find_location(get_nodes_block(op1), get_nodes_block(op2)); @@ -347,7 +344,7 @@ static ir_node *do_apply(ir_opcode code, dbg_info *db, ir_node *op1, ir_node *op */ 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); + unsigned code = get_irn_opcode(orig); ir_node *result = search(code, op1, op2, env); if (result == NULL) { @@ -382,7 +379,7 @@ static ir_node *apply(ir_node *header, ir_node *orig, ir_node *op1, ir_node *op2 */ static ir_node *reduce(ir_node *orig, ir_node *iv, ir_node *rc, iv_env *env) { - ir_opcode code = get_irn_opcode(orig); + unsigned code = get_irn_opcode(orig); ir_node *result = search(code, iv, rc, env); /* check if we have already done this operation on the iv */ @@ -446,7 +443,7 @@ static void update_scc(ir_node *iv, node_entry *e, iv_env *env) pscc->head = NULL; waitq_put(wq, iv); do { - ir_node *irn = waitq_get(wq); + ir_node *irn = (ir_node*)waitq_get(wq); node_entry *ne = get_irn_ne(irn, env); int i; @@ -490,7 +487,7 @@ static int replace(ir_node *irn, ir_node *iv, ir_node *rc, iv_env *env) if (result != irn) { node_entry *e; - hook_strength_red(current_ir_graph, irn); + hook_strength_red(get_irn_irg(irn), irn); exchange(irn, result); e = get_irn_ne(result, env); if (e->pscc == NULL) { @@ -517,7 +514,7 @@ static int is_x86_shift_const(ir_node *mul) /* normalization put constants on the right side */ rc = get_Mul_right(mul); if (is_Const(rc)) { - tarval *tv = get_Const_tarval(rc); + ir_tarval *tv = get_Const_tarval(rc); if (tarval_is_long(tv)) { long value = get_tarval_long(tv); @@ -609,16 +606,13 @@ static int is_counter_iv(ir_node *iv, iv_env *env) */ static int check_users_for_reg_pressure(ir_node *iv, iv_env *env) { - ir_node *irn, *header; + ir_node *irn; ir_node *have_user = NULL; ir_node *have_cmp = NULL; node_entry *e = get_irn_ne(iv, env); scc *pscc = e->pscc; - header = e->header; for (irn = pscc->head; irn != NULL; irn = e->next) { - const ir_edge_t *edge; - foreach_out_edge(irn, edge) { ir_node *user = get_edge_src_irn(edge); node_entry *ne = get_irn_ne(user, env); @@ -684,7 +678,7 @@ static int check_replace(ir_node *irn, iv_env *env) { ir_node *left, *right, *iv, *rc; ir_op *op = get_irn_op(irn); - ir_opcode code = get_op_code(op); + unsigned code = get_op_code(op); ir_node *liv, *riv; switch (code) { @@ -735,7 +729,7 @@ static void classify_iv(scc *pscc, iv_env *env) /* find the header block for this scc */ for (irn = pscc->head; irn; irn = next) { - node_entry *e = get_irn_link(irn); + node_entry *e = (node_entry*)get_irn_link(irn); ir_node *block = get_nodes_block(irn); next = e->next; @@ -762,8 +756,26 @@ static void classify_iv(scc *pscc, iv_env *env) next = e->next; switch (get_irn_opcode(irn)) { - case iro_Add: case iro_Sub: + only_phi = 0; + { + ir_node *left = get_Sub_left(irn); + node_entry *left_entry = get_irn_ne(left, env); + ir_node *right = get_Sub_right(irn); + node_entry *right_entry = get_irn_ne(right, env); + + if (left_entry->pscc != e->pscc || + (right_entry->pscc != e->pscc && !is_rc(right, header))) { + /* + * Not an induction variable. + * Region constant are only allowed on right hand side. + */ + goto fail; + } + } + break; + + case iro_Add: only_phi = 0; /* fall through */ case iro_Phi: @@ -835,7 +847,7 @@ fail: static void process_scc(scc *pscc, iv_env *env) { ir_node *head = pscc->head; - node_entry *e = get_irn_link(head); + node_entry *e = (node_entry*)get_irn_link(head); #ifdef DEBUG_libfirm { @@ -843,7 +855,7 @@ static void process_scc(scc *pscc, iv_env *env) DB((dbg, LEVEL_4, " SCC at %p:\n ", pscc)); for (irn = pscc->head; irn != NULL; irn = next) { - node_entry *e = get_irn_link(irn); + node_entry *e = (node_entry*)get_irn_link(irn); next = e->next; @@ -873,7 +885,7 @@ static void remove_phi_cycle(scc *pscc, iv_env *env) int j; ir_node *out_rc; - /* check if this scc contains only Phi, Add or Sub nodes */ + /* check if this scc contains only Phi nodes */ out_rc = NULL; for (irn = pscc->head; irn; irn = next) { node_entry *e = get_irn_ne(irn, env); @@ -917,7 +929,7 @@ static void remove_phi_cycle(scc *pscc, iv_env *env) static void process_phi_only_scc(scc *pscc, iv_env *env) { ir_node *head = pscc->head; - node_entry *e = get_irn_link(head); + node_entry *e = (node_entry*)get_irn_link(head); #ifdef DEBUG_libfirm { @@ -925,7 +937,7 @@ static void process_phi_only_scc(scc *pscc, iv_env *env) DB((dbg, LEVEL_4, " SCC at %p:\n ", pscc)); for (irn = pscc->head; irn; irn = next) { - node_entry *e = get_irn_link(irn); + node_entry *e = (node_entry*)get_irn_link(irn); next = e->next; @@ -951,7 +963,7 @@ static void push(iv_env *env, ir_node *n) node_entry *e; if (env->tos == ARR_LEN(env->stack)) { - int nlen = ARR_LEN(env->stack) * 2; + size_t nlen = ARR_LEN(env->stack) * 2; ARR_RESIZE(ir_node *, env->stack, nlen); } env->stack[env->tos++] = n; @@ -1047,13 +1059,11 @@ static void dfs(ir_node *irn, iv_env *env) */ static void do_dfs(ir_graph *irg, iv_env *env) { - ir_graph *rem = current_ir_graph; ir_node *end = get_irg_end(irg); int i; ir_reserve_resources(irg, IR_RESOURCE_IRN_VISITED); - current_ir_graph = irg; inc_irg_visited(irg); /* visit all visible nodes */ @@ -1068,8 +1078,6 @@ static void do_dfs(ir_graph *irg, iv_env *env) } ir_free_resources(irg, IR_RESOURCE_IRN_VISITED); - - current_ir_graph = rem; } /* do_dfs */ /** @@ -1077,7 +1085,7 @@ static void do_dfs(ir_graph *irg, iv_env *env) */ static void assign_po(ir_node *block, void *ctx) { - iv_env *env = ctx; + iv_env *env = (iv_env*)ctx; node_entry *e = get_irn_ne(block, env); e->POnum = env->POnum++; @@ -1103,9 +1111,10 @@ static void assign_po(ir_node *block, void *ctx) static ir_node *applyOneEdge(ir_node *iv, ir_node *rc, LFTR_edge *e, iv_env *env) { if (env->osr_flags & osr_flag_lftr_with_ov_check) { - tarval *tv_l, *tv_r, *tv, *tv_init, *tv_incr, *tv_end; + ir_tarval *tv_l, *tv_r, *tv, *tv_init, *tv_incr, *tv_end; tarval_int_overflow_mode_t ovmode; scc *pscc; + ir_graph *irg; if (! is_counter_iv(iv, env)) { DB((dbg, LEVEL_4, " not counter IV")); @@ -1160,6 +1169,12 @@ static ir_node *applyOneEdge(ir_node *iv, ir_node *rc, LFTR_edge *e, iv_env *env panic("Unsupported opcode"); } + if (tv == tarval_bad || tv_init == tarval_bad) { + tarval_set_integer_overflow_mode(ovmode); + DB((dbg, LEVEL_4, " = OVERFLOW")); + return NULL; + } + if (pscc->code == iro_Add) { tv_end = tarval_add(tv, tv_incr); } else { @@ -1169,11 +1184,12 @@ static ir_node *applyOneEdge(ir_node *iv, ir_node *rc, LFTR_edge *e, iv_env *env tarval_set_integer_overflow_mode(ovmode); - if (tv == tarval_bad || tv_init == tarval_bad || tv_end == tarval_bad) { + if (tv_end == tarval_bad) { DB((dbg, LEVEL_4, " = OVERFLOW")); return NULL; } - return new_Const(tv); + irg = get_irn_irg(iv); + return new_r_Const(irg, tv); } return do_apply(e->code, NULL, rc, e->rc, get_irn_mode(e->dst)); } /* applyOneEdge */ @@ -1227,7 +1243,7 @@ static ir_node *applyEdges(ir_node **pIV, ir_node *rc, iv_env *env) */ static void do_lftr(ir_node *cmp, void *ctx) { - iv_env *env = ctx; + iv_env *env = (iv_env*)ctx; ir_node *left, *right, *liv, *riv; ir_node *iv, *rc; ir_node *nleft = NULL, *nright = NULL; @@ -1272,36 +1288,15 @@ static void lftr(ir_graph *irg, iv_env *env) irg_walk_graph(irg, NULL, do_lftr, env); } /* lftr */ -/** - * Pre-walker: set all node links to NULL and fix the - * block of Proj nodes. - */ -static void clear_and_fix(ir_node *irn, void *env) -{ - int *moved = env; - set_irn_link(irn, NULL); - - if (is_Proj(irn)) { - ir_node *pred = get_Proj_pred(irn); - ir_node *pred_block = get_nodes_block(pred); - - if (get_nodes_block(irn) != pred_block) { - set_nodes_block(irn, pred_block); - *moved = 1; - } - } -} /* clear_and_fix */ - - /* Remove any Phi cycles with only one real input. */ void remove_phi_cycles(ir_graph *irg) { - iv_env env; - ir_graph *rem; - int projs_moved; + iv_env env; - rem = current_ir_graph; - current_ir_graph = irg; + assure_irg_properties(irg, + IR_GRAPH_PROPERTY_CONSISTENT_DOMINANCE + | IR_GRAPH_PROPERTY_CONSISTENT_OUTS + | IR_GRAPH_PROPERTY_CONSISTENT_OUT_EDGES); FIRM_DBG_REGISTER(dbg, "firm.opt.remove_phi"); @@ -1324,13 +1319,7 @@ void remove_phi_cycles(ir_graph *irg) * the same block as their predecessors. * This can improve the placement of new nodes. */ - projs_moved = 0; - irg_walk_graph(irg, NULL, clear_and_fix, &projs_moved); - if (projs_moved) - set_irg_outs_inconsistent(irg); - - /* we need outs for calculating the post order */ - assure_irg_outs(irg); + irg_walk_graph(irg, NULL, firm_clear_link, NULL); /* calculate the post order number for blocks. */ irg_out_block_walk(get_irg_start_block(irg), NULL, assign_po, &env); @@ -1341,15 +1330,15 @@ void remove_phi_cycles(ir_graph *irg) ir_free_resources(irg, IR_RESOURCE_IRN_LINK); if (env.replaced) { - set_irg_outs_inconsistent(irg); - DB((dbg, LEVEL_1, "remove_phi_cycles: %u Cycles removed\n\n", env.replaced)); + DB((dbg, LEVEL_1, "remove_phi_cycles: %u Cycles removed\n\n", + env.replaced)); } DEL_ARR_F(env.stack); obstack_free(&env.obst, NULL); - current_ir_graph = rem; -} /* remove_phi_cycles */ + confirm_irg_properties(irg, IR_GRAPH_PROPERTIES_CONTROL_FLOW); +} ir_graph_pass_t *remove_phi_cycles_pass(const char *name) { @@ -1407,6 +1396,19 @@ static void fix_adds_and_subs(ir_node *irn, void *ctx) set_Sub_right(irn, right); } } + } else if (mode_is_reference(mode)) { + ir_node *left = get_Sub_left(irn); + ir_node *right = get_Sub_right(irn); + ir_mode *l_mode = get_irn_mode(left); + ir_mode *r_mode = get_irn_mode(right); + if (mode_is_int(l_mode)) { + /* Usually, Sub(I*,P) is an error, hence the verifier rejects it. + * However, it is correct in this case, so add Conv to make verifier happy. */ + ir_node *block = get_nodes_block(right); + ir_node *lconv = new_r_Conv(block, left, r_mode); + assert(mode_is_reference(r_mode)); + set_Sub_left(irn, lconv); + } } } } /* fix_adds_and_subs */ @@ -1414,16 +1416,15 @@ static void fix_adds_and_subs(ir_node *irn, void *ctx) /* Performs Operator Strength Reduction for the passed graph. */ void opt_osr(ir_graph *irg, unsigned flags) { - iv_env env; - ir_graph *rem; - int edges; - int projs_moved; - - rem = current_ir_graph; - current_ir_graph = irg; + iv_env env; FIRM_DBG_REGISTER(dbg, "firm.opt.osr"); + assure_irg_properties(irg, + IR_GRAPH_PROPERTY_CONSISTENT_DOMINANCE + | IR_GRAPH_PROPERTY_CONSISTENT_OUTS + | IR_GRAPH_PROPERTY_CONSISTENT_OUT_EDGES); + DB((dbg, LEVEL_1, "Doing Operator Strength Reduction for %+F\n", irg)); obstack_init(&env.obst); @@ -1440,21 +1441,11 @@ void opt_osr(ir_graph *irg, unsigned flags) env.process_scc = process_scc; /* Clear all links and move Proj nodes into the - the same block as it's predecessors. - This can improve the placement of new nodes. + * the same block as its predecessors. + * This can improve the placement of new nodes. */ - projs_moved = 0; - irg_walk_graph(irg, NULL, clear_and_fix, &projs_moved); - if (projs_moved) - set_irg_outs_inconsistent(irg); - - /* we need dominance */ - assure_doms(irg); + irg_walk_graph(irg, NULL, firm_clear_link, NULL); - edges = edges_assure(irg); - - /* calculate the post order number for blocks by walking the out edges. */ - assure_irg_outs(irg); irg_block_edges_walk(get_irg_start_block(irg), NULL, assign_po, &env); /* calculate the SCC's and drive OSR. */ @@ -1469,7 +1460,6 @@ void opt_osr(ir_graph *irg, unsigned flags) lftr(irg, &env); (void)lftr; - set_irg_outs_inconsistent(irg); DB((dbg, LEVEL_1, "Replacements: %u + %u (lftr)\n\n", env.replaced, env.lftr_replaced)); } ir_free_resources(irg, IR_RESOURCE_IRN_LINK); @@ -1479,30 +1469,27 @@ void opt_osr(ir_graph *irg, unsigned flags) DEL_ARR_F(env.stack); obstack_free(&env.obst, NULL); - if (! edges) - edges_deactivate(irg); - - current_ir_graph = rem; -} /* opt_osr */ + confirm_irg_properties(irg, IR_GRAPH_PROPERTIES_NONE); +} -struct pass_t { +typedef struct pass_t { ir_graph_pass_t pass; unsigned flags; -}; +} pass_t; /** * Wrapper for running opt_osr() as an ir_graph pass. */ static int pass_wrapper(ir_graph *irg, void *context) { - struct pass_t *pass = context; + pass_t *pass = (pass_t*)context; opt_osr(irg, pass->flags); return 0; } /* pass_wrapper */ ir_graph_pass_t *opt_osr_pass(const char *name, unsigned flags) { - struct pass_t *pass = XMALLOCZ(struct pass_t); + pass_t *pass = XMALLOCZ(pass_t); pass->flags = flags; return def_graph_pass_constructor(