X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=ir%2Fir%2Firgopt.c;h=ae510a8016b6c67038ab74d8a69572265cb24b97;hb=3142e505b37d6e992ebfe599ee69027fea55f75c;hp=b1afa380101c35224acd2480eb07aba800e275f7;hpb=bb9f2e36362333c6635b89f5258171b06c786608;p=libfirm diff --git a/ir/ir/irgopt.c b/ir/ir/irgopt.c index b1afa3801..ae510a801 100644 --- a/ir/ir/irgopt.c +++ b/ir/ir/irgopt.c @@ -39,6 +39,7 @@ #include "adt/pdeq.h" +#include "irpass_t.h" #include "irflag_t.h" #include "iredges_t.h" #include "irtools.h" @@ -50,7 +51,8 @@ /** * A wrapper around optimize_inplace_2() to be called from a walker. */ -static void optimize_in_place_wrapper (ir_node *n, void *env) { +static void optimize_in_place_wrapper(ir_node *n, void *env) +{ ir_node *optimized = optimize_in_place_2(n); (void) env; @@ -67,26 +69,27 @@ static void optimize_in_place_wrapper (ir_node *n, void *env) { * * @note current_ir_graph must be set */ -static inline void do_local_optimize(ir_node *n) { +static inline void do_local_optimize(ir_node *n) +{ + ir_graph *irg = get_irn_irg(n); + /* Handle graph state */ - assert(get_irg_phase_state(current_ir_graph) != phase_building); + assert(get_irg_phase_state(irg) != phase_building); if (get_opt_global_cse()) - set_irg_pinned(current_ir_graph, op_pin_state_floats); - set_irg_outs_inconsistent(current_ir_graph); - set_irg_doms_inconsistent(current_ir_graph); - set_irg_loopinfo_inconsistent(current_ir_graph); + set_irg_pinned(irg, op_pin_state_floats); + set_irg_doms_inconsistent(irg); /* Clean the value_table in irg for the CSE. */ - del_identities(current_ir_graph->value_table); - current_ir_graph->value_table = new_identities(); + new_identities(irg); /* walk over the graph */ irg_walk(n, firm_clear_link, optimize_in_place_wrapper, NULL); } /* Applies local optimizations (see iropt.h) to all nodes reachable from node n */ -void local_optimize_node(ir_node *n) { +void local_optimize_node(ir_node *n) +{ ir_graph *rem = current_ir_graph; current_ir_graph = get_irn_irg(n); @@ -95,48 +98,26 @@ void local_optimize_node(ir_node *n) { current_ir_graph = rem; } -/** - * Block-Walker: uses dominance depth to mark dead blocks. - */ -static void kill_dead_blocks(ir_node *block, void *env) { - (void) env; - - if (get_Block_dom_depth(block) < 0) { - /* - * Note that the new dominance code correctly handles - * the End block, i.e. it is always reachable from Start - */ - set_Block_dead(block); - } -} - -/* Applies local optimizations (see iropt.h) to all nodes reachable from node n. */ -void local_optimize_graph(ir_graph *irg) { - ir_graph *rem = current_ir_graph; - current_ir_graph = irg; - - if (get_irg_dom_state(irg) == dom_consistent) - irg_block_walk_graph(irg, NULL, kill_dead_blocks, NULL); - - do_local_optimize(get_irg_end(irg)); - - current_ir_graph = rem; +static void enqueue_node(ir_node *node, pdeq *waitq) +{ + if (get_irn_link(node) == waitq) + return; + pdeq_putr(waitq, node); + set_irn_link(node, waitq); } /** * Enqueue all users of a node to a wait queue. * Handles mode_T nodes. */ -static void enqueue_users(ir_node *n, pdeq *waitq) { +static void enqueue_users(ir_node *n, pdeq *waitq) +{ const ir_edge_t *edge; foreach_out_edge(n, edge) { ir_node *succ = get_edge_src_irn(edge); - if (get_irn_link(succ) != waitq) { - pdeq_putr(waitq, succ); - set_irn_link(succ, waitq); - } + enqueue_node(succ, waitq); if (get_irn_mode(succ) == mode_T) { /* A mode_T node has Proj's. Because most optimizations run on the Proj's we have to enqueue them also. */ @@ -145,13 +126,51 @@ static void enqueue_users(ir_node *n, pdeq *waitq) { } } +/** + * Block-Walker: uses dominance depth to mark dead blocks. + */ +static void find_unreachable_blocks(ir_node *block, void *env) +{ + pdeq *waitq = (pdeq*) env; + + if (get_Block_dom_depth(block) < 0) { + ir_graph *irg = get_irn_irg(block); + ir_node *end = get_irg_end(irg); + + const ir_edge_t *edge; + foreach_block_succ(block, edge) { + const ir_edge_t *edge2; + ir_node *succ_block = get_edge_src_irn(edge); + enqueue_node(succ_block, waitq); + foreach_out_edge(succ_block, edge2) { + ir_node *succ = get_edge_src_irn(edge2); + if (is_Phi(succ)) + enqueue_node(succ, waitq); + } + } + enqueue_node(end, waitq); + } +} + +/* Applies local optimizations (see iropt.h) to all nodes reachable from node n. */ +void local_optimize_graph(ir_graph *irg) +{ + ir_graph *rem = current_ir_graph; + current_ir_graph = irg; + + do_local_optimize(get_irg_end(irg)); + + current_ir_graph = rem; +} + /** * Data flow optimization walker. - * Optimizes all nodes and enqueue it's users + * Optimizes all nodes and enqueue its users * if done. */ -static void opt_walker(ir_node *n, void *env) { - pdeq *waitq = env; +static void opt_walker(ir_node *n, void *env) +{ + pdeq *waitq = (pdeq*)env; ir_node *optimized; optimized = optimize_in_place_2(n); @@ -163,71 +182,156 @@ static void opt_walker(ir_node *n, void *env) { } } +/** + * Return the number of non-Bad predecessors of the given node. + */ +static int count_non_bads(ir_node *node) +{ + int arity = get_irn_arity(node); + int count = 0; + int i; + for (i = 0; i < arity; ++i) { + if (!is_Bad(get_irn_n(node, i))) + ++count; + } + return count; +} + +/** + * Block-walker, remove Bad block predecessors and shorten Phis. + * Phi links must be uptodate. + */ +static void block_remove_bads(ir_node *block, void *env) +{ + int *changed = (int *)env; + int i, j; + ir_node **new_in, *new_block, *phi; + const int max = get_irn_arity(block); + const int new_max = count_non_bads(block); + assert(max >= new_max); + + if (is_Bad(block) || max == new_max) + return; + + new_in = ALLOCAN(ir_node*, new_max); + *changed = 1; + + assert(get_Block_dom_depth(block) >= 0); + + /* 1. Create a new block without Bad inputs */ + for (i = j = 0; i < max; ++i) { + ir_node *block_pred = get_irn_n(block, i); + if (!is_Bad(block_pred)) { + new_in[j++] = block_pred; + } + } + assert(j == new_max); + + /* If the end block is unreachable, it might have zero predecessors. */ + if (new_max == 0) { + ir_node *end_block = get_irg_end_block(get_irn_irg(block)); + if (block == end_block) { + set_irn_in(block, new_max, new_in); + return; + } + } + + new_block = new_r_Block(get_irn_irg(block), new_max, new_in); + + /* 2. Remove inputs on Phis, where the block input is Bad. */ + phi = get_Block_phis(block); + if (phi != NULL) { + do { + ir_node *next = get_Phi_next(phi); + if (get_irn_arity(phi) != new_max) { + ir_node *new_phi; + + for (i = j = 0; i < max; ++i) { + ir_node *block_pred = get_irn_n(block, i); + + if (!is_Bad(block_pred)) { + ir_node *pred = get_irn_n(phi, i); + new_in[j++] = pred; + } + } + assert(j == new_max); + + new_phi = new_r_Phi(new_block, new_max, new_in, get_irn_mode(phi)); + exchange(phi, new_phi); + } + phi = next; + } while (phi != NULL); + } + + exchange(block, new_block); +} + +/* Remove Bad nodes from Phi and Block inputs. + * + * Precondition: No unreachable code. + * Postcondition: No Bad nodes. + */ +static int remove_Bads(ir_graph *irg) +{ + int changed = 0; + /* build phi list per block */ + irg_walk_graph(irg, firm_clear_block_phis, firm_collect_block_phis, NULL); + + /* actually remove Bads */ + irg_block_walk_graph(irg, NULL, block_remove_bads, (void *)&changed); + + return changed; +} + /* Applies local optimizations to all nodes in the graph until fixpoint. */ -int optimize_graph_df(ir_graph *irg) { +int optimize_graph_df(ir_graph *irg) +{ pdeq *waitq = new_pdeq(); ir_graph *rem = current_ir_graph; ir_node *end; - int i, state, n_ka, changed; + int state, changed; current_ir_graph = irg; state = edges_assure(irg); - if (get_opt_global_cse()) - set_irg_pinned(current_ir_graph, op_pin_state_floats); - /* Clean the value_table in irg for the CSE. */ - del_identities(irg->value_table); - irg->value_table = new_identities(); + new_identities(irg); + + if (get_opt_global_cse()) { + set_irg_pinned(irg, op_pin_state_floats); + } - if (get_irg_dom_state(irg) == dom_consistent) - irg_block_walk_graph(irg, NULL, kill_dead_blocks, NULL); + /* The following enables unreachable code elimination (=Blocks may be + * Bad). */ + set_irg_state(irg, IR_GRAPH_STATE_BAD_BLOCK); /* invalidate info */ - set_irg_outs_inconsistent(irg); set_irg_doms_inconsistent(irg); - set_irg_loopinfo_inconsistent(irg); ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK); - end = get_irg_end(irg); - n_ka = get_End_n_keepalives(end); + /* Calculate dominance so we can kill unreachable code */ + assure_doms(irg); /* walk over the graph, but don't touch keep-alives */ - irg_walk(get_irg_end_block(irg), NULL, opt_walker, waitq); - - /* - * Optimize keep-alives by removing superfluous ones. - * Beware: the last transformation might add new keep-alives - * that keep blocks that are where visited! So, check only the - * "old" keep-alives, not the new ones! - * - * FIXME: it might be better to completely remove this - * optimization here ... - */ - for (i = n_ka - 1; i >= 0; --i) { - ir_node *ka = get_End_keepalive(end, i); - - if (irn_visited(ka) && !is_irn_keep(ka)) { - /* this node can be regularly visited, no need to keep it */ - set_End_keepalive(end, i, get_irg_bad(irg)); - } - } - /* now walk again and visit all not yet visited nodes */ - set_irg_visited(current_ir_graph, get_irg_visited(irg) - 1); - irg_walk(get_irg_end(irg), NULL, opt_walker, waitq); + irg_walk_graph(irg, NULL, opt_walker, waitq); /* any optimized nodes are stored in the wait queue, * so if it's not empty, the graph has been changed */ changed = !pdeq_empty(waitq); - /* finish the wait queue */ - while (! pdeq_empty(waitq)) { - ir_node *n = pdeq_getl(waitq); - if (! is_Bad(n)) + while (!pdeq_empty(waitq)) { + /* finish the wait queue */ + while (! pdeq_empty(waitq)) { + ir_node *n = (ir_node*)pdeq_getl(waitq); opt_walker(n, waitq); + } + /* Calculate dominance so we can kill unreachable code */ + compute_doms(irg); + irg_block_walk_graph(irg, NULL, find_unreachable_blocks, waitq); } + set_irg_doms_inconsistent(irg); del_pdeq(waitq); @@ -236,6 +340,23 @@ int optimize_graph_df(ir_graph *irg) { if (! state) edges_deactivate(irg); + if (remove_Bads(irg)) { + edges_deactivate(irg); + } + + /* Finally kill BAD and doublets from the keep alives. + Doing this AFTER edges where deactivated saves cycles */ + end = get_irg_end(irg); + remove_End_Bads_and_doublets(end); + + clear_irg_state(irg, IR_GRAPH_STATE_BAD_BLOCK); + current_ir_graph = rem; return changed; } + +/* Creates an ir_graph pass for optimize_graph_df. */ +ir_graph_pass_t *optimize_graph_df_pass(const char *name) +{ + return def_graph_pass_ret(name ? name : "optimize_graph_df", optimize_graph_df); +} /* optimize_graph_df_pass */