Implement better magic to handle changing control dependencies when welding blocks
[libfirm] / ir / opt / tailrec.c
index 7666bd8..0784291 100644 (file)
@@ -26,7 +26,7 @@
 #include <assert.h>
 #include "tailrec.h"
 #include "array.h"
-#include "irprog.h"
+#include "irprog_t.h"
 #include "irgwalk.h"
 #include "irgmod.h"
 #include "irop.h"
@@ -36,6 +36,8 @@
 #include "irflag.h"
 #include "trouts.h"
 #include "return.h"
+#include "scalar_replace.h"
+#include "irouts.h"
 #include "irhooks.h"
 
 /**
@@ -67,26 +69,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;
+      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;
       }
     }
     break;
@@ -96,13 +98,13 @@ static void collect_data(ir_node *node, void *env)
     /*
      * the first block has the initial exec as cfg predecessor
      */
-    if (node != current_ir_graph->start_block) {
+    if (node != get_irg_start_block(current_ir_graph)) {
       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;
@@ -126,20 +128,21 @@ static void do_opt_tail_rec(ir_graph *irg, ir_node *rets, int n_tail_calls)
   ir_node **in;
   ir_node **phis;
   ir_node ***call_params;
-  ir_node *p;
+  ir_node *p, *n;
   int i, j, n_params;
   collect_t data;
-  int rem         = get_optimize();
-  entity *ent     = get_irg_entity(irg);
-  type *method_tp = get_entity_type(ent);
+  int rem            = get_optimize();
+  entity *ent        = get_irg_entity(irg);
+  ir_type *method_tp = get_entity_type(ent);
 
   assert(n_tail_calls);
 
-  /* we add new nodes, so the outs are inconsistant */
+  /* we add new nodes, so the outs are inconsistent */
   set_irg_outs_inconsistent(irg);
 
   /* we add new blocks and change the control flow */
-  set_irg_dom_inconsistent(irg);
+  set_irg_doms_inconsistent(irg);
+  set_irg_extblk_inconsistent(irg);
 
   /* we add a new loop */
   set_irg_loopinfo_inconsistent(irg);
@@ -173,14 +176,13 @@ static void do_opt_tail_rec(ir_graph *irg, ir_node *rets, int n_tail_calls)
   in[0] = data.proj_X;
 
   /* turn Return's into Jmp's */
-  for (i = 1, p = rets; p; p = get_irn_link(p)) {
-    ir_node *jmp;
+  for (i = 1, p = rets; p; p = n) {
     ir_node *block = get_nodes_block(p);
 
-    jmp = new_r_Jmp(irg, block);
+    n = get_irn_link(p);
+    in[i++] = new_r_Jmp(irg, block);
 
     exchange(p, new_r_Bad(irg));
-    in[i++] = jmp;
 
     /* we might generate an endless loop, so add
      * the block to the keep-alive list */
@@ -210,9 +212,11 @@ static void do_opt_tail_rec(ir_graph *irg, ir_node *rets, int n_tail_calls)
 
   phis[0] = new_r_Phi(irg, block, n_tail_calls + 1, in, mode_M);
 
-  /* build the data phi's */
+  /* build the data Phi's */
   if (n_params > 0) {
     ir_node *calls;
+    ir_node *args;
+    ir_node *args_bl;
 
     NEW_ARR_A(ir_node **, call_params, n_tail_calls);
 
@@ -222,13 +226,15 @@ static void do_opt_tail_rec(ir_graph *irg, ir_node *rets, int n_tail_calls)
       ++i;
     }
 
-    /* build new projs and Phi's */
+    /* build new Proj's and Phi's */
+    args    = get_irg_args(irg);
+    args_bl = get_nodes_block(args);
     for (i = 0; i < n_params; ++i) {
       ir_mode *mode = get_type_mode(get_method_param_type(method_tp, i));
 
-      in[0] = new_r_Proj(irg, block, irg->args, mode, i);
+      in[0] = new_r_Proj(irg, args_bl, args, mode, i);
       for (j = 0; j < n_tail_calls; ++j)
-       in[j + 1] = call_params[j][i];
+        in[j + 1] = call_params[j][i];
 
       phis[i + 1] = new_r_Phi(irg, block, n_tail_calls + 1, in, mode);
     }
@@ -238,19 +244,22 @@ static void do_opt_tail_rec(ir_graph *irg, ir_node *rets, int n_tail_calls)
    * ok, we are here, so we have build and collected all needed Phi's
    * now exchange all Projs into links to Phi
    */
-  for (p = data.proj_m; p; p = get_irn_link(p)) {
+  for (p = data.proj_m; p; p = n) {
+    n = get_irn_link(p);
     exchange(p, phis[0]);
   }
-  for (p = data.proj_data; p; p = get_irn_link(p)) {
+  for (p = data.proj_data; p; p = n) {
     long proj = get_Proj_proj(p);
 
     assert(0 <= proj && proj < n_params);
+    n = get_irn_link(p);
     exchange(p, phis[proj + 1]);
   }
 
   /* tail recursion was done, all info is invalid */
-  set_irg_dom_inconsistent(irg);
+  set_irg_doms_inconsistent(irg);
   set_irg_outs_inconsistent(irg);
+  set_irg_extblk_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);
@@ -258,20 +267,48 @@ static void do_opt_tail_rec(ir_graph *irg, ir_node *rets, int n_tail_calls)
   set_optimize(rem);
 }
 
+/**
+ * Check the lifetime of locals in the given graph.
+ * Tail recursion can only be done, if we can prove that
+ * the lifetime of locals end with the recursive call.
+ * We do this by checking that no address of a local variable is
+ * stored or transmitted as an argument to a call.
+ *
+ * @return non-zero if it's ok to do tail recursion
+ */
+static int check_lifetime_of_locals(ir_graph *irg)
+{
+  ir_node *irg_frame = get_irg_frame(irg);
+  int i;
+
+  if (get_irg_outs_state(irg) != outs_consistent)
+    compute_irg_outs(irg);
+
+  for (i = get_irn_n_outs(irg_frame) - 1; i >= 0; --i) {
+    ir_node *succ = get_irn_out(irg_frame, i);
+
+    if (is_Sel(succ) && is_address_taken(succ))
+      return 0;
+  }
+  return 1;
+}
+
 /*
  * convert simple tail-calls into loops
  */
 int opt_tail_rec_irg(ir_graph *irg)
 {
   ir_node *end_block;
-  int n_preds;
   int i, n_tail_calls = 0;
   ir_node *rets = NULL;
-  type *mtd_type, *call_type;
+  ir_type *mtd_type, *call_type;
 
   if (! get_opt_tail_recursion() || ! get_opt_optimize())
     return 0;
 
+  if (! check_lifetime_of_locals(irg))
+    return 0;
+
   /*
    * This tail recursion optimization works best
    * if the Returns are normalized.
@@ -281,21 +318,20 @@ int opt_tail_rec_irg(ir_graph *irg)
   end_block = get_irg_end_block(irg);
   set_irn_link(end_block, NULL);
 
-  n_preds = get_Block_n_cfgpreds(end_block);
-  for (i = 0; i < n_preds; ++i) {
+  for (i = get_Block_n_cfgpreds(end_block) - 1; i >= 0; --i) {
     ir_node *ret = get_Block_cfgpred(end_block, i);
     ir_node *call, *call_ptr;
     entity *ent;
-    int j, n_ress;
+    int j;
     ir_node **ress;
 
-    /* search all returns of a block */
-    if (get_irn_op(ret) != op_Return)
+    /* search all Returns of a block */
+    if (! is_Return(ret))
       continue;
 
     /* check, if it's a Return self() */
     call = skip_Proj(get_Return_mem(ret));
-    if (get_irn_op(call) != op_Call)
+    if (! is_Call(call))
       continue;
 
     /* check if it's a recursive call */
@@ -312,18 +348,16 @@ int opt_tail_rec_irg(ir_graph *irg)
       continue;
 
     /* ok, mem is routed to a recursive call, check return args */
-    n_ress = get_Return_n_ress(ret);
     ress = get_Return_res_arr(ret);
-
-    for (j = 0; j < n_ress; ++j) {
+    for (j = get_Return_n_ress(ret) - 1; j >= 0; --j) {
       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)
+    if (j >= 0)
       continue;
 
     /*
@@ -336,7 +370,7 @@ int opt_tail_rec_irg(ir_graph *irg)
     if (mtd_type != call_type) {
       /*
        * Hmm, the types did not match, bad.
-       * This can happen in C when no prototyp is given
+       * This can happen in C when no prototype is given
        * or K&R style is used.
        */
 #if 0
@@ -363,7 +397,7 @@ int opt_tail_rec_irg(ir_graph *irg)
 
   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);
+       get_entity_ld_name(get_irg_entity(irg)), n_tail_calls);
 
   hook_tail_rec(irg, n_tail_calls);
   do_opt_tail_rec(irg, rets, n_tail_calls);
@@ -378,14 +412,15 @@ void opt_tail_recursion(void)
 {
   int i;
   int n_opt_applications = 0;
+  ir_graph *irg;
 
   if (! get_opt_tail_recursion() || ! get_opt_optimize())
     return;
 
-  for (i = 0; i < get_irp_n_irgs(); i++) {
-    current_ir_graph = get_irp_irg(i);
+  for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
+    irg = get_irp_irg(i);
 
-    if (opt_tail_rec_irg(current_ir_graph))
+    if (opt_tail_rec_irg(irg))
       ++n_opt_applications;
   }