- fix most of the -Wunreachable-code and -Wlogical-op warnings
[libfirm] / ir / ir / irgopt.c
index 207c922..9c3eb8e 100644 (file)
@@ -24,9 +24,7 @@
  *           Michael Beck
  * @version  $Id$
  */
-#ifdef HAVE_CONFIG_H
-# include "config.h"
-#endif
+#include "config.h"
 
 #include <assert.h>
 
@@ -41,6 +39,7 @@
 
 #include "adt/pdeq.h"
 
+#include "irpass_t.h"
 #include "irflag_t.h"
 #include "iredges_t.h"
 #include "irtools.h"
@@ -52,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;
 
@@ -69,7 +69,8 @@ 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)
+{
        /* Handle graph state */
        assert(get_irg_phase_state(current_ir_graph) != phase_building);
 
@@ -88,7 +89,8 @@ static INLINE void do_local_optimize(ir_node *n) {
 }
 
 /* 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);
 
@@ -100,7 +102,8 @@ void local_optimize_node(ir_node *n) {
 /**
  * Block-Walker: uses dominance depth to mark dead blocks.
  */
-static void kill_dead_blocks(ir_node *block, void *env) {
+static void kill_dead_blocks(ir_node *block, void *env)
+{
        (void) env;
 
        if (get_Block_dom_depth(block) < 0) {
@@ -113,7 +116,8 @@ static void kill_dead_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) {
+void local_optimize_graph(ir_graph *irg)
+{
        ir_graph *rem = current_ir_graph;
        current_ir_graph = irg;
 
@@ -129,7 +133,8 @@ void local_optimize_graph(ir_graph *irg) {
  * 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) {
@@ -152,7 +157,8 @@ static void enqueue_users(ir_node *n, pdeq *waitq) {
  * Optimizes all nodes and enqueue it's users
  * if done.
  */
-static void opt_walker(ir_node *n, void *env) {
+static void opt_walker(ir_node *n, void *env)
+{
        pdeq *waitq = env;
        ir_node *optimized;
 
@@ -166,11 +172,12 @@ static void opt_walker(ir_node *n, void *env) {
 }
 
 /* Applies local optimizations to all nodes in the graph until fixpoint. */
-void 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;
+       int      state, changed;
 
        current_ir_graph = irg;
 
@@ -193,32 +200,12 @@ void optimize_graph_df(ir_graph *irg) {
 
        ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
 
-       end  = get_irg_end(irg);
-       n_ka = get_End_n_keepalives(end);
-
        /* 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)) {
@@ -234,5 +221,17 @@ void optimize_graph_df(ir_graph *irg) {
        if (! state)
                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);
+
        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 */