improved partBlock check
[libfirm] / ir / ir / irgopt.c
index 1f552d9..192f9b0 100644 (file)
@@ -312,6 +312,7 @@ static void copy_node(ir_node *n, void *env) {
        ir_node *nn, *block;
        int new_arity;
        ir_op *op = get_irn_op(n);
+       (void) env;
 
        /* The end node looses it's flexible in array.  This doesn't matter,
           as dead node elimination builds End by hand, inlineing doesn't use
@@ -412,7 +413,7 @@ static void copy_preds(ir_node *n, void *env) {
                                exchange(nn, old);
                        }
                }
-       } else if (is_Phi(n)) {
+       } else if (is_Phi(n) && get_irn_arity(n) > 0) {
                /* Don't copy node if corresponding predecessor in block is Bad.
                   The Block itself should not be Bad. */
                block = get_nodes_block(n);
@@ -611,62 +612,65 @@ copy_graph_env(int copy_node_nr) {
  * Adds all new nodes to a new hash table for CSE.  Does not
  * perform CSE, so the hash table might contain common subexpressions.
  */
-void
-dead_node_elimination(ir_graph *irg) {
-       if (get_opt_optimize() && get_opt_dead_node_elimination()) {
-               ir_graph *rem;
-               int rem_ipview = get_interprocedural_view();
-               struct obstack *graveyard_obst = NULL;
-               struct obstack *rebirth_obst   = NULL;
-               assert(! edges_activated(irg) && "dead node elimination requires disabled edges");
-
-               /* inform statistics that we started a dead-node elimination run */
-               hook_dead_node_elim(irg, 1);
-
-               /* Remember external state of current_ir_graph. */
-               rem = current_ir_graph;
-               current_ir_graph = irg;
-               set_interprocedural_view(0);
+void dead_node_elimination(ir_graph *irg) {
+       ir_graph *rem;
+#ifdef INTERPROCEDURAL_VIEW
+       int rem_ipview = get_interprocedural_view();
+#endif
+       struct obstack *graveyard_obst = NULL;
+       struct obstack *rebirth_obst   = NULL;
+       assert(! edges_activated(irg) && "dead node elimination requires disabled edges");
 
-               assert(get_irg_phase_state(irg) != phase_building);
+       /* inform statistics that we started a dead-node elimination run */
+       hook_dead_node_elim(irg, 1);
 
-               /* Handle graph state */
-               free_callee_info(irg);
-               free_irg_outs(irg);
-               free_trouts();
+       /* Remember external state of current_ir_graph. */
+       rem = current_ir_graph;
+       current_ir_graph = irg;
+#ifdef INTERPROCEDURAL_VIEW
+       set_interprocedural_view(0);
+#endif
 
-               /* @@@ so far we loose loops when copying */
-               free_loop_information(irg);
+       assert(get_irg_phase_state(irg) != phase_building);
 
-               set_irg_doms_inconsistent(irg);
+       /* Handle graph state */
+       free_callee_info(irg);
+       free_irg_outs(irg);
+       free_trouts();
+
+       /* @@@ so far we loose loops when copying */
+       free_loop_information(irg);
 
-               /* A quiet place, where the old obstack can rest in peace,
-                  until it will be cremated. */
-               graveyard_obst = irg->obst;
+       set_irg_doms_inconsistent(irg);
 
-               /* A new obstack, where the reachable nodes will be copied to. */
-               rebirth_obst = xmalloc(sizeof(*rebirth_obst));
-               irg->obst = rebirth_obst;
-               obstack_init(irg->obst);
-               irg->last_node_idx = 0;
+       /* A quiet place, where the old obstack can rest in peace,
+          until it will be cremated. */
+       graveyard_obst = irg->obst;
 
-               /* We also need a new value table for CSE */
-               del_identities(irg->value_table);
-               irg->value_table = new_identities();
+       /* A new obstack, where the reachable nodes will be copied to. */
+       rebirth_obst = xmalloc(sizeof(*rebirth_obst));
+       irg->obst = rebirth_obst;
+       obstack_init(irg->obst);
+       irg->last_node_idx = 0;
 
-               /* Copy the graph from the old to the new obstack */
-               copy_graph_env(/*copy_node_nr=*/1);
+       /* We also need a new value table for CSE */
+       del_identities(irg->value_table);
+       irg->value_table = new_identities();
 
-               /* Free memory from old unoptimized obstack */
-               obstack_free(graveyard_obst, 0);  /* First empty the obstack ... */
-               xfree(graveyard_obst);            /* ... then free it.           */
+       /* Copy the graph from the old to the new obstack */
+       copy_graph_env(/*copy_node_nr=*/1);
 
-               /* inform statistics that the run is over */
-               hook_dead_node_elim(irg, 0);
+       /* Free memory from old unoptimized obstack */
+       obstack_free(graveyard_obst, 0);  /* First empty the obstack ... */
+       xfree(graveyard_obst);            /* ... then free it.           */
 
-               current_ir_graph = rem;
-               set_interprocedural_view(rem_ipview);
-       }
+       /* inform statistics that the run is over */
+       hook_dead_node_elim(irg, 0);
+
+       current_ir_graph = rem;
+#ifdef INTERPROCEDURAL_VIEW
+       set_interprocedural_view(rem_ipview);
+#endif
 }
 
 /**
@@ -988,8 +992,8 @@ int inline_method(ir_node *call, ir_graph *called_graph) {
        ir_type *called_frame;
        irg_inline_property prop = get_irg_inline_property(called_graph);
 
-       if ( (prop < irg_inline_forced) &&
-            (!get_opt_optimize() || !get_opt_inline() || (prop == irg_inline_forbidden))) return 0;
+       if ( (prop < irg_inline_forced) || (prop == irg_inline_forbidden))
+               return 0;
 
        /* Do not inline variadic functions. */
        if (get_method_variadicity(get_entity_type(get_irg_entity(called_graph))) == variadicity_variadic)
@@ -1381,8 +1385,6 @@ void inline_small_irgs(ir_graph *irg, int size) {
        call_entry *entry;
        DEBUG_ONLY(firm_dbg_module_t *dbg;)
 
-       if (!(get_opt_optimize() && get_opt_inline())) return;
-
        FIRM_DBG_REGISTER(dbg, "firm.opt.inline");
 
        current_ir_graph = irg;
@@ -1508,7 +1510,7 @@ static void collect_calls2(ir_node *call, void *ctx) {
 }
 
 /**
- * Returns TRUE if the number of callers in 0 in the irg's environment,
+ * Returns TRUE if the number of callers is 0 in the irg's environment,
  * hence this irg is a leave.
  */
 INLINE static int is_leave(ir_graph *irg) {
@@ -1517,7 +1519,8 @@ INLINE static int is_leave(ir_graph *irg) {
 }
 
 /**
- * Returns TRUE if the number of callers is smaller size in the irg's environment.
+ * Returns TRUE if the number of nodes in the callee is
+ * smaller then size in the irg's environment.
  */
 INLINE static int is_smaller(ir_graph *callee, int size) {
        inline_irg_env *env = get_irg_link(callee);
@@ -1563,8 +1566,6 @@ void inline_leave_functions(int maxsize, int leavesize, int size, int ignore_run
        struct obstack   obst;
        DEBUG_ONLY(firm_dbg_module_t *dbg;)
 
-       if (!(get_opt_optimize() && get_opt_inline())) return;
-
        FIRM_DBG_REGISTER(dbg, "firm.opt.inline");
        rem = current_ir_graph;
        obstack_init(&obst);
@@ -1704,11 +1705,12 @@ void inline_leave_functions(int maxsize, int leavesize, int size, int ignore_run
                        optimize_graph_df(irg);
                        optimize_cf(irg);
                }
-               if (env->got_inline || (env->n_callers_orig != env->n_callers))
+               if (env->got_inline || (env->n_callers_orig != env->n_callers)) {
                        DB((dbg, SET_LEVEL_1, "Nodes:%3d ->%3d, calls:%3d ->%3d, callers:%3d ->%3d, -- %s\n",
                        env->n_nodes_orig, env->n_nodes, env->n_call_nodes_orig, env->n_call_nodes,
                        env->n_callers_orig, env->n_callers,
                        get_entity_name(get_irg_entity(irg))));
+               }
        }
 
        obstack_free(&obst, NULL);
@@ -1964,9 +1966,7 @@ static ir_node *calc_dca(ir_node *dca, ir_node *block) {
  */
 static ir_node *consumer_dom_dca(ir_node *dca, ir_node *consumer, ir_node *producer)
 {
-       ir_node *block = NULL;
-
-       /* Compute the latest block into which we can place a node so that it is
+       /* Compute the last block into which we can place a node so that it is
           before consumer. */
        if (is_Phi(consumer)) {
                /* our consumer is a Phi-node, the effective use is in all those
@@ -1976,23 +1976,18 @@ static ir_node *consumer_dom_dca(ir_node *dca, ir_node *consumer, ir_node *produ
                int      i;
 
                for (i = 0;  i < arity; i++) {
-                       if (get_irn_n(consumer, i) == producer) {
-                               ir_node *new_block = get_nodes_block(get_Block_cfgpred(phi_block, i));
+                       if (get_Phi_pred(consumer, i) == producer) {
+                               ir_node *new_block = get_Block_cfgpred_block(phi_block, i);
 
                                if (!is_Block_unreachable(new_block))
-                                       block = calc_dca(block, new_block);
+                                       dca = calc_dca(dca, new_block);
                        }
                }
-
-               if (!block)
-                       block = get_nodes_block(producer);
        } else {
-               assert(!is_Block(consumer));
-               block = get_nodes_block(consumer);
+               dca = calc_dca(dca, get_nodes_block(consumer));
        }
 
-       /* Compute the deepest common ancestor of block and dca. */
-       return calc_dca(dca, block);
+       return dca;
 }
 
 /* FIXME: the name clashes here with the function from ana/field_temperature.c
@@ -2202,7 +2197,7 @@ void place_code(ir_graph *irg) {
 
        if (1 || get_irg_loopinfo_state(irg) != loopinfo_consistent) {
                free_loop_information(irg);
-               construct_backedges(irg);
+               construct_cf_backedges(irg);
        }
 
        /* Place all floating nodes as early as possible. This guarantees
@@ -2259,7 +2254,8 @@ static void walk_critical_cf_edges(ir_node *n, void *env) {
                                        goto insert;
                                continue;
                        }
-                       if (is_op_forking(cfop)) {
+                       /* we don't want place nodes in the start block, so handle it like forking */
+                       if (is_op_forking(cfop) || cfop == op_Start) {
                                /* Predecessor has multiple successors. Insert new control flow edge edges. */
 insert:
                                /* set predecessor of new block */