move get_irn_edge_kind to internal API
[libfirm] / ir / ir / irgopt.c
index 968bf5f..2f494b9 100644 (file)
@@ -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,6 +35,7 @@
 #include "irgopt.h"
 #include "irgmod.h"
 #include "irgwalk.h"
+#include "ircons.h"
 
 #include "adt/pdeq.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.
  */
@@ -78,7 +74,7 @@ static inline void do_local_optimize(ir_node *n)
 
        if (get_opt_global_cse())
                set_irg_pinned(irg, op_pin_state_floats);
-       set_irg_doms_inconsistent(irg);
+       clear_irg_properties(irg, IR_GRAPH_PROPERTY_CONSISTENT_DOMINANCE);
 
        /* Clean the value_table in irg for the CSE. */
        new_identities(irg);
@@ -87,7 +83,6 @@ static inline void do_local_optimize(ir_node *n)
        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)
 {
        ir_graph *rem = current_ir_graph;
@@ -112,13 +107,21 @@ static void enqueue_node(ir_node *node, 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);
+               ir_node *succ  = get_edge_src_irn(edge);
 
                enqueue_node(succ, waitq);
-               if (get_irn_mode(succ) == mode_T) {
+
+               /* 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);
@@ -137,9 +140,7 @@ static void find_unreachable_blocks(ir_node *block, void *env)
                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) {
@@ -152,7 +153,6 @@ static void find_unreachable_blocks(ir_node *block, void *env)
        }
 }
 
-/* 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;
@@ -182,181 +182,68 @@ static void opt_walker(ir_node *n, void *env)
        }
 }
 
-static void clear_block_phis(ir_node *node, void *env) {
-       (void) env;
-       if (is_Block(node)) {
-               set_Block_phis(node, NULL);
-       }
-}
-
-static void collect_block_phis(ir_node *node, void *env) {
-       (void) env;
-       if (is_Phi(node)) {
-               add_Block_phi(get_nodes_block(node), 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;
-}
-
-static void block_remove_bads(ir_node *block, int *changed) {
-       int i, j;
-       ir_node **new_in;
-       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 */
-       j = 0;
-       for (i = 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. */
-       ir_node *end_block = get_irg_end_block(get_irn_irg(block));
-       if (new_max == 0 && block == end_block) {
-               set_irn_in(block, new_max, new_in);
-               return;
-       }
-
-       ir_node *new_block =  new_r_Block(get_irn_irg(block), new_max, new_in);
-
-       /* 2. Remove inputs on Phis, where the block input is Bad. */
-       ir_node *phi = get_Block_phis(block);
-       if (phi != NULL) {
-               do {
-                       ir_node* next = get_Phi_next(phi);
-                       if (get_irn_arity(phi) != new_max) {
-                               j = 0;
-                               for (i = 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);
-
-                               ir_node *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, clear_block_phis, collect_block_phis, NULL);
-
-       /* actually remove Bads */
-       irg_block_walk_graph(irg, NULL, (void (*)(struct ir_node *, void *)) block_remove_bads, &changed);
-
-       return changed;
-}
-
-/* Applies local optimizations to all nodes in the graph until fixpoint. */
 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);
-
-       /* Clean the value_table in irg for the CSE. */
-       new_identities(irg);
-
-       if (get_opt_global_cse()) {
+       if (get_opt_global_cse())
                set_irg_pinned(irg, op_pin_state_floats);
-       }
 
-       /* The following enables unreachable code elimination (=Blocks may be
-        * Bad). */
-       set_irg_state(irg, IR_GRAPH_STATE_BAD_BLOCK);
+       /* 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);
 
-       /* invalidate info */
-       set_irg_doms_inconsistent(irg);
-
-       ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
-
-       /* Calculate dominance so we can kill unreachable code */
+       new_identities(irg);
+       assure_edges(irg);
        assure_doms(irg);
 
-       /* walk over the graph, but don't touch keep-alives */
+
+       ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
        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);
-
        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 */
+               /* 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);
        }
-       set_irg_doms_inconsistent(irg);
-
        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 */
+        * Doing this AFTER edges where deactivated saves cycles */
        end = get_irg_end(irg);
        remove_End_Bads_and_doublets(end);
 
-       if (remove_Bads(irg)) {
-               edges_deactivate(irg);
-       }
-
-       clear_irg_state(irg, IR_GRAPH_STATE_BAD_BLOCK);
-
        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;
 }
 
-/* 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 */
+}