X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=ir%2Fir%2Firgopt.c;h=2f494b9c62f8ab36c94261e2b1e8bee63cddc715;hb=83e6c267e052f2ca4392e1267f2382baaf7083c5;hp=0f7437c3d43f3570eadd56093a4e6146272713e6;hpb=818856f8558cbee4f730e3b6b79795224aa4fda7;p=libfirm diff --git a/ir/ir/irgopt.c b/ir/ir/irgopt.c index 0f7437c3d..2f494b9c6 100644 --- a/ir/ir/irgopt.c +++ b/ir/ir/irgopt.c @@ -22,7 +22,6 @@ * @brief Optimizations for a whole ir graph, i.e., a procedure. * @author Christian Schaefer, Goetz Lindenmaier, Sebastian Felis, * Michael Beck - * @version $Id$ */ #include "config.h" @@ -36,21 +35,20 @@ #include "irgopt.h" #include "irgmod.h" #include "irgwalk.h" +#include "ircons.h" #include "adt/pdeq.h" +#include "irpass_t.h" #include "irflag_t.h" #include "iredges_t.h" #include "irtools.h" -/*------------------------------------------------------------------*/ -/* apply optimizations of iropt to all nodes. */ -/*------------------------------------------------------------------*/ - /** * 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 +65,26 @@ 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); + clear_irg_properties(irg, IR_GRAPH_PROPERTY_CONSISTENT_DOMINANCE); /* 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,63 +93,84 @@ void local_optimize_node(ir_node *n) { 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) +{ + foreach_out_edge(n, edge) { + ir_node *succ = get_edge_src_irn(edge); + + enqueue_node(succ, waitq); + + /* Also enqueue Phis to prevent inconsistencies. */ + if (is_Block(succ)) { + foreach_out_edge(succ, edge2) { + ir_node *succ2 = get_edge_src_irn(edge2); + + if (is_Phi(succ2)) { + enqueue_node(succ2, waitq); + } + } + } else 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. */ + enqueue_users(succ, waitq); + } + } +} + /** * Block-Walker: uses dominance depth to mark dead blocks. */ -static void kill_dead_blocks(ir_node *block, void *env) { - (void) env; +static void find_unreachable_blocks(ir_node *block, void *env) +{ + pdeq *waitq = (pdeq*) 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); + ir_graph *irg = get_irn_irg(block); + ir_node *end = get_irg_end(irg); + + foreach_block_succ(block, edge) { + 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) { +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; } -/** - * Enqueue all users of a node to a wait queue. - * Handles mode_T nodes. - */ -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); - } - 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. */ - enqueue_users(succ, waitq); - } - } -} - /** * 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,60 +182,68 @@ static void opt_walker(ir_node *n, void *env) { } } -/* 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 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); + set_irg_pinned(irg, op_pin_state_floats); - /* Clean the value_table in irg for the CSE. */ - del_identities(irg->value_table); - irg->value_table = new_identities(); + /* enable unreachable code elimination */ + assert(!irg_is_constrained(irg, IR_GRAPH_CONSTRAINT_OPTIMIZE_UNREACHABLE_CODE)); + add_irg_constraints(irg, IR_GRAPH_CONSTRAINT_OPTIMIZE_UNREACHABLE_CODE); - if (get_irg_dom_state(irg) == dom_consistent) - irg_block_walk_graph(irg, NULL, kill_dead_blocks, NULL); + new_identities(irg); + assure_edges(irg); + assure_doms(irg); - /* 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); - - /* walk over the graph, but don't touch keep-alives */ 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 + * We want this intertwined with localopts for better optimization (phase coupling) */ + compute_doms(irg); + irg_block_walk_graph(irg, NULL, find_unreachable_blocks, waitq); } - del_pdeq(waitq); - ir_free_resources(irg, IR_RESOURCE_IRN_LINK); - if (! state) - edges_deactivate(irg); + /* disable unreachable code elimination */ + clear_irg_constraints(irg, IR_GRAPH_CONSTRAINT_OPTIMIZE_UNREACHABLE_CODE); + add_irg_properties(irg, IR_GRAPH_PROPERTY_NO_UNREACHABLE_CODE); + + /* invalidate infos */ + clear_irg_properties(irg, IR_GRAPH_PROPERTY_CONSISTENT_DOMINANCE); + clear_irg_properties(irg, IR_GRAPH_PROPERTY_CONSISTENT_LOOPINFO); + 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); + * Doing this AFTER edges where deactivated saves cycles */ + end = get_irg_end(irg); remove_End_Bads_and_doublets(end); current_ir_graph = rem; - return changed; + + /* Note we do not have a reliable way to detect changes, since some + * localopt rules change the inputs of a node and do not return a new + * node, so we conservatively say true here */ + return true; +} + +ir_graph_pass_t *optimize_graph_df_pass(const char *name) +{ + return def_graph_pass_ret(name ? name : "optimize_graph_df", optimize_graph_df); }