added missing include
[libfirm] / ir / opt / tailrec.c
index 2bec6b4..f6210c2 100644 (file)
  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
  */
 #ifdef HAVE_CONFIG_H
-# include <config.h>
+# include "config.h"
+#endif
+
+#ifdef HAVE_ALLOCA_H
+#include <alloca.h>
+#endif
+#ifdef HAVE_MALLOC_H
+#include <malloc.h>
+#endif
+#ifdef HAVE_STRING_H
+#include <string.h>
 #endif
 
 #include <assert.h>
 #include "irgraph_t.h"
 #include "ircons.h"
 #include "irflag.h"
-#include "firmstat.h"
+#include "trouts.h"
+#include "return.h"
 
 /**
- * the environment for colelcting data
+ * the environment for collecting data
  */
 typedef struct _collect_t {
-  ir_node *proj_X;             /**< initial exec proj */
-  ir_node *block;              /**< old first block */
-  int     blk_idx;             /**< cfgpred index of the initial exec in block */
-  ir_node *proj_m;             /**< linked list of memory from start proj's */
-  ir_node *proj_data;          /**< linked list of all parameter access proj's */
+  ir_node *proj_X;      /**< initial exec proj */
+  ir_node *block;       /**< old first block */
+  int     blk_idx;      /**< cfgpred index of the initial exec in block */
+  ir_node *proj_m;      /**< linked list of memory from start proj's */
+  ir_node *proj_data;   /**< linked list of all parameter access proj's */
 } collect_t;
 
 /**
- * walker for collecting data
+ * walker for collecting data, fills a collect_t environment
  */
 static void collect_data(ir_node *node, void *env)
 {
@@ -55,26 +66,26 @@ static void collect_data(ir_node *node, void *env)
       ir_node *start = get_Proj_pred(pred);
 
       if (get_irn_op(start) == op_Start) {
-       if (get_Proj_proj(pred) == pn_Start_T_args) {
-         /* found Proj(ProjT(Start)) */
-         set_irn_link(node, data->proj_data);
-         data->proj_data = node;
-       }
+           if (get_Proj_proj(pred) == pn_Start_T_args) {
+             /* found Proj(ProjT(Start)) */
+             set_irn_link(node, data->proj_data);
+             data->proj_data = node;
+           }
       }
     }
     else if (op == op_Start) {
       switch (get_Proj_proj(node)) {
         case pn_Start_M:
           /* found ProjM(Start) */
-         set_irn_link(node, data->proj_m);
-         data->proj_m = node;
-         break;
-       case pn_Start_X_initial_exec:
-         /* found ProjX(Start) */
-         data->proj_X = node;
-         break;
-       default:
-         break;
+             set_irn_link(node, data->proj_m);
+             data->proj_m = node;
+             break;
+           case pn_Start_X_initial_exec:
+             /* found ProjX(Start) */
+             data->proj_X = node;
+             break;
+           default:
+             break;
       }
     }
     break;
@@ -86,11 +97,11 @@ static void collect_data(ir_node *node, void *env)
      */
     if (node != current_ir_graph->start_block) {
       for (i = 0; i < n_pred; ++i) {
-       if (get_Block_cfgpred(node, i) == data->proj_X) {
-         data->block   = node;
-         data->blk_idx = i;
-         break;
-       }
+           if (get_Block_cfgpred(node, i) == data->proj_X) {
+             data->block   = node;
+             data->blk_idx = i;
+             break;
+           }
       }
     }
     break;
@@ -189,6 +200,8 @@ static void do_opt_tail_rec(ir_graph *irg, ir_node *rets, int n_tail_calls)
     in[i] = get_Call_mem(calls);
     ++i;
   }
+  assert(i == n_tail_calls + 1);
+
   phis[0] = new_rd_Phi(NULL, irg, block, n_tail_calls + 1, in, mode_M);
 
   /* build the data phi's */
@@ -229,6 +242,13 @@ static void do_opt_tail_rec(ir_graph *irg, ir_node *rets, int n_tail_calls)
     exchange(p, phis[proj + 1]);
   }
 
+  /* tail recursion was done, all info is invalid */
+  set_irg_dom_inconsistent(irg);
+  set_irg_outs_inconsistent(irg);
+  set_irg_loopinfo_state(current_ir_graph, loopinfo_cf_inconsistent);
+  set_trouts_inconsistent();
+  set_irg_callee_info_state(irg, irg_callee_info_inconsistent);
+
   current_ir_graph = rem_irg;
   set_optimize(rem);
 }
@@ -236,7 +256,7 @@ static void do_opt_tail_rec(ir_graph *irg, ir_node *rets, int n_tail_calls)
 /*
  * convert simple tail-calls into loops
  */
-void opt_tail_rec_irg(ir_graph *irg)
+int opt_tail_rec_irg(ir_graph *irg)
 {
   ir_node *end_block = irg->end_block;
   int n_preds;
@@ -244,14 +264,20 @@ void opt_tail_rec_irg(ir_graph *irg)
   ir_node *rets = NULL;
 
   if (! get_opt_tail_recursion() || ! get_opt_optimize())
-    return;
+    return 0;
+
+  /*
+   * This tail recursion optimization works best
+   * if the Returns are normalized.
+   */
+  normalize_n_returns(irg);
 
   set_irn_link(end_block, NULL);
 
   n_preds = get_Block_n_cfgpreds(end_block);
   for (i = 0; i < n_preds; ++i) {
     ir_node *ret = get_Block_cfgpred(end_block, i);
-    ir_node *proj_m, *call, *call_ptr;
+    ir_node *call, *call_ptr;
     entity *ent;
     int j, n_ress;
     ir_node **ress;
@@ -261,12 +287,7 @@ void opt_tail_rec_irg(ir_graph *irg)
       continue;
 
     /* check, if it's a Return self() */
-    proj_m = get_Return_mem(ret);
-
-    if (get_irn_op(proj_m) != op_Proj)
-      continue;
-
-    call = get_Proj_pred(proj_m);
+    call = skip_Proj(get_Return_mem(ret));
     if (get_irn_op(call) != op_Call)
       continue;
 
@@ -276,6 +297,9 @@ void opt_tail_rec_irg(ir_graph *irg)
     if (get_irn_op(call_ptr) != op_SymConst)
       continue;
 
+    if (get_SymConst_kind(call_ptr) != symconst_addr_ent)
+      continue;
+
     ent = get_SymConst_entity(call_ptr);
     if (!ent || get_entity_irg(ent) != irg)
       continue;
@@ -285,27 +309,11 @@ void opt_tail_rec_irg(ir_graph *irg)
     ress = get_Return_res_arr(ret);
 
     for (j = 0; j < n_ress; ++j) {
-      ir_node *proj = ress[j];
-      ir_node *proj_proj;
-      ir_node *irn;
-
-      if (get_irn_op(proj) != op_Proj) {
-       /* not routed to a call */
-       break;
-      }
-
-      proj_proj = get_Proj_pred(proj);
-
-      if (get_irn_op(proj) != op_Proj) {
-       /* not routed to a call */
-       break;
-      }
-
-      irn = get_Proj_pred(proj_proj);
+      ir_node *irn = skip_Proj(skip_Proj(ress[j]));
 
       if (irn != call) {
-       /* not routed to a call */
-       break;
+           /* not routed to a call */
+           break;
       }
     }
     if (j < n_ress)
@@ -323,9 +331,15 @@ void opt_tail_rec_irg(ir_graph *irg)
 
   /* now, end_block->link contains the list of all tail calls */
   if (! n_tail_calls)
-    return;
+    return 0;
+
+  if (get_opt_tail_recursion_verbose() && get_firm_verbosity() > 1)
+    printf("  Performing tail recursion for graph %s and %d Calls\n",
+          get_entity_ld_name(get_irg_entity(irg)), n_tail_calls);
 
   do_opt_tail_rec(irg, rets, n_tail_calls);
+
+  return n_tail_calls;
 }
 
 /*
@@ -334,6 +348,7 @@ void opt_tail_rec_irg(ir_graph *irg)
 void opt_tail_recursion(void)
 {
   int i;
+  int n_opt_applications = 0;
 
   if (! get_opt_tail_recursion() || ! get_opt_optimize())
     return;
@@ -341,6 +356,10 @@ void opt_tail_recursion(void)
   for (i = 0; i < get_irp_n_irgs(); i++) {
     current_ir_graph = get_irp_irg(i);
 
-    opt_tail_rec_irg(current_ir_graph);
+    if (opt_tail_rec_irg(current_ir_graph))
+      ++n_opt_applications;
   }
+
+  if (get_opt_tail_recursion_verbose())
+    printf("Performed tail recursion for %d of %d graphs\n", n_opt_applications, get_irp_n_irgs());
 }