Cleaned up, removed global File handle
[libfirm] / ir / ir / irgopt.c
index 6b13921..9b6e70e 100644 (file)
 # include <config.h>
 #endif
 
-# include <assert.h>
-# include <stdbool.h>
-
-# include "irprog.h"
-# include "irgopt.h"
-# include "irnode_t.h"
-# include "irgraph_t.h"
-# include "iropt_t.h"
-# include "irgwalk.h"
-# include "ircons.h"
-# include "irgmod.h"
-# include "array.h"
-# include "pset.h"
-# include "eset.h"
-# include "pdeq.h"       /* Fuer code placement */
-# include "irouts.h"
-# include "irloop.h"
-# include "irbackedge_t.h"
-# include "irflag_t.h"
-# include "firmstat.h"
+#include <assert.h>
+#include <stdbool.h>
+
+#include "irnode_t.h"
+#include "irgraph_t.h"
+#include "irprog_t.h"
+
+#include "ircons.h"
+#include "iropt_t.h"
+#include "irgopt.h"
+#include "irgmod.h"
+#include "irgwalk.h"
+
+#include "array.h"
+#include "pset.h"
+#include "eset.h"
+#include "pdeq.h"       /* Fuer code placement */
+
+#include "irouts.h"
+#include "irloop_t.h"
+#include "irbackedge_t.h"
+#include "cgana.h"
+
+#include "irflag_t.h"
+#include "firmstat.h"
 
 /* Defined in iropt.c */
 pset *new_identities (void);
@@ -386,9 +391,10 @@ copy_graph_env (void) {
   /* Not all nodes remembered in current_ir_graph might be reachable
      from the end node.  Assure their link is set to NULL, so that
      we can test whether new nodes have been computed. */
-  set_irn_link(get_irg_frame  (current_ir_graph), NULL);
-  set_irn_link(get_irg_globals(current_ir_graph), NULL);
-  set_irn_link(get_irg_args   (current_ir_graph), NULL);
+  set_irn_link(get_irg_frame      (current_ir_graph), NULL);
+  set_irn_link(get_irg_globals    (current_ir_graph), NULL);
+  set_irn_link(get_irg_args       (current_ir_graph), NULL);
+  set_irn_link(get_irg_initial_mem(current_ir_graph), NULL);
 
   /* we use the block walk flag for removing Bads from Blocks ins. */
   inc_irg_block_visited(current_ir_graph);
@@ -411,17 +417,23 @@ copy_graph_env (void) {
     copy_node (get_irg_globals(current_ir_graph), NULL);
     copy_preds(get_irg_globals(current_ir_graph), NULL);
   }
+  if (get_irn_link(get_irg_initial_mem(current_ir_graph)) == NULL) {
+    copy_node (get_irg_initial_mem(current_ir_graph), NULL);
+    copy_preds(get_irg_initial_mem(current_ir_graph), NULL);
+  }
   if (get_irn_link(get_irg_args(current_ir_graph)) == NULL) {
     copy_node (get_irg_args(current_ir_graph), NULL);
     copy_preds(get_irg_args(current_ir_graph), NULL);
   }
-  set_irg_start  (current_ir_graph, get_new_node(get_irg_start(current_ir_graph)));
+  set_irg_start      (current_ir_graph, get_new_node(get_irg_start(current_ir_graph)));
 
   set_irg_start_block(current_ir_graph,
               get_new_node(get_irg_start_block(current_ir_graph)));
-  set_irg_frame  (current_ir_graph, get_new_node(get_irg_frame(current_ir_graph)));
-  set_irg_globals(current_ir_graph, get_new_node(get_irg_globals(current_ir_graph)));
-  set_irg_args   (current_ir_graph, get_new_node(get_irg_args(current_ir_graph)));
+  set_irg_frame      (current_ir_graph, get_new_node(get_irg_frame(current_ir_graph)));
+  set_irg_globals    (current_ir_graph, get_new_node(get_irg_globals(current_ir_graph)));
+  set_irg_initial_mem(current_ir_graph, get_new_node(get_irg_initial_mem(current_ir_graph)));
+  set_irg_args       (current_ir_graph, get_new_node(get_irg_args(current_ir_graph)));
+
   if (get_irn_link(get_irg_bad(current_ir_graph)) == NULL) {
     copy_node(get_irg_bad(current_ir_graph), NULL);
     copy_preds(get_irg_bad(current_ir_graph), NULL);
@@ -444,6 +456,7 @@ dead_node_elimination(ir_graph *irg) {
   struct obstack *graveyard_obst = NULL;
   struct obstack *rebirth_obst   = NULL;
 
+  /* inform statistics that we started a dead-node elimination run */
   stat_dead_node_elim_start(irg);
 
   /* Remember external state of current_ir_graph. */
@@ -453,9 +466,8 @@ dead_node_elimination(ir_graph *irg) {
 
   /* Handle graph state */
   assert(get_irg_phase_state(current_ir_graph) != phase_building);
-  assert(get_irg_callee_info_state(current_ir_graph) == irg_callee_info_none);
+  free_callee_info(current_ir_graph);
   free_outs(current_ir_graph);
-
   /* @@@ so far we loose loops when copying */
   free_loop_information(current_ir_graph);
 
@@ -482,6 +494,7 @@ dead_node_elimination(ir_graph *irg) {
     xfree (graveyard_obst);           /* ... then free it.           */
   }
 
+  /* inform statistics that the run is over */
   stat_dead_node_elim_stop(irg);
 
   current_ir_graph = rem;
@@ -678,7 +691,6 @@ int inline_method(ir_node *call, ir_graph *called_graph) {
   if ( (prop != irg_inline_forced) && (!get_opt_optimize() || !get_opt_inline() ||
                                        (prop == irg_inline_forbidden))) return 0;
 
-
   /*
    * currently, we cannot inline two cases:
    * - call with compound arguments
@@ -878,7 +890,10 @@ int inline_method(ir_node *call, ir_graph *called_graph) {
           n_ret++;
         }
       }
-      phi = new_Phi(n_ret, cf_pred, get_irn_mode(cf_pred[0]));
+      if (n_ret > 0)
+       phi = new_Phi(n_ret, cf_pred, get_irn_mode(cf_pred[0]));
+      else
+       phi = new_Bad();
       res_pred[j] = phi;
       /* Conserve Phi-list for further inlinings -- but might be optimized */
       if (get_nodes_Block(phi) == post_bl) {
@@ -1048,8 +1063,10 @@ static ir_graph *get_call_called_irg(ir_node *call) {
   if (get_irn_op(addr) == op_Const) {
     /* Check whether the constant is the pointer to a compiled entity. */
     tv = get_Const_tarval(addr);
-    if (tarval_to_entity(tv))
-      called_irg = get_entity_irg(tarval_to_entity(tv));
+    if (get_tarval_entity(tv))
+      called_irg = get_entity_irg(get_tarval_entity(tv));
+  } else if (get_irn_op(addr) == op_SymConst && get_SymConst_kind(addr) == symconst_addr_ent) {
+    called_irg = get_entity_irg(get_SymConst_entity(addr));
   }
   return called_irg;
 }
@@ -1066,8 +1083,8 @@ static void collect_calls(ir_node *call, void *env) {
   if (get_irn_op(addr) == op_Const) {
     /* Check whether the constant is the pointer to a compiled entity. */
     tv = get_Const_tarval(addr);
-    if (tarval_to_entity(tv)) {
-      called_irg = get_entity_irg(tarval_to_entity(tv));
+    if (get_tarval_entity(tv)) {
+      called_irg = get_entity_irg(get_tarval_entity(tv));
       if (called_irg && ienv->pos < MAX_INLINE) {
         /* The Call node calls a locally defined method.  Remember to inline. */
         ienv->calls[ienv->pos++] = call;
@@ -1094,7 +1111,7 @@ void inline_small_irgs(ir_graph *irg, int size) {
   current_ir_graph = irg;
   /* Handle graph state */
   assert(get_irg_phase_state(current_ir_graph) != phase_building);
-  assert(get_irg_callee_info_state(current_ir_graph) == irg_callee_info_none);
+  free_callee_info(current_ir_graph);
 
   /* Find Call nodes to inline.
      (We can not inline during a walk of the graph, as inlineing the same
@@ -1111,7 +1128,7 @@ void inline_small_irgs(ir_graph *irg, int size) {
       tarval *tv;
       ir_graph *callee;
       tv = get_Const_tarval(get_Call_ptr(env.calls[i]));
-      callee = get_entity_irg(tarval_to_entity(tv));
+      callee = get_entity_irg(get_tarval_entity(tv));
       if (((_obstack_memory_used(callee->obst) - obstack_room(callee->obst)) < size) ||
         (get_irg_inline_property(callee) == irg_inline_forced)) {
         inline_method(env.calls[i], callee);
@@ -1211,7 +1228,7 @@ void inline_leave_functions(int maxsize, int leavesize, int size) {
   for (i = 0; i < n_irgs; ++i) {
     current_ir_graph = get_irp_irg(i);
     assert(get_irg_phase_state(current_ir_graph) != phase_building);
-    assert(get_irg_callee_info_state(current_ir_graph) == irg_callee_info_none);
+    free_callee_info(current_ir_graph);
 
     irg_walk(get_irg_end(current_ir_graph), NULL, collect_calls2,
          get_irg_link(current_ir_graph));
@@ -1252,12 +1269,12 @@ void inline_leave_functions(int maxsize, int leavesize, int size) {
 /*         get_entity_name(get_irg_entity(callee))); */
           if (inline_method(call, callee)) {
             did_inline = 1;
-        env->n_call_nodes--;
-        eset_insert_all(env->call_nodes, callee_env->call_nodes);
-        env->n_call_nodes += callee_env->n_call_nodes;
-        env->n_nodes += callee_env->n_nodes;
-        callee_env->n_callers--;
-      }
+           env->n_call_nodes--;
+           eset_insert_all(env->call_nodes, callee_env->call_nodes);
+           env->n_call_nodes += callee_env->n_call_nodes;
+           env->n_nodes += callee_env->n_nodes;
+           callee_env->n_callers--;
+         }
         } else {
           eset_insert(env->call_nodes, call);
         }