fixed const mismatch
[libfirm] / ir / opt / return.c
index 3fa0cd2..2082d55 100644 (file)
@@ -33,7 +33,7 @@
 /*
  * Normalize the Returns of a graph by creating a new End block
  * with One Return(Phi).
- * This is the prefered input for the if-conversion.
+ * This is the preferred input for the if-conversion.
  *
  * In pseudocode, it means:
  *
@@ -61,13 +61,14 @@ void normalize_one_return(ir_graph *irg)
 
   /* look, if we have more than one return */
   n       = get_Block_n_cfgpreds(endbl);
+  assert(n > 0);
   returns = alloca((n + 7) >> 3);
   memset(returns, 0, (n + 7) >> 3);
 
   for (n_rets = i = 0; i < n; ++i) {
     ir_node *node = get_Block_cfgpred(endbl, i);
 
-    if (get_irn_op(node) == op_Return) {
+    if (is_Return(node)) {
       ++n_rets;
 
       set_bit(i);
@@ -94,12 +95,12 @@ void normalize_one_return(ir_graph *irg)
 
       /* save the return values and shuffle them */
       for (k = 0; k < n_ret_vals; ++k)
-        retvals[j + k*n_ret_vals] = get_irn_n(ret, k);
-
-      ++j;
+        retvals[j + k*n_rets] = get_irn_n(ret, k);
 
       set_Block_cfgpred(endbl, i, new_r_Bad(irg));
       last_idx = i;
+
+      ++j;
     }
   }
 
@@ -120,7 +121,7 @@ void normalize_one_return(ir_graph *irg)
    * a new Block was added, so dominator, outs and loop are inconsistent,
    * trouts and callee-state should be still valid
    */
-  set_irg_dom_inconsistent(irg);
+  set_irg_doms_inconsistent(irg);
   set_irg_outs_inconsistent(irg);
   set_irg_loopinfo_state(current_ir_graph, loopinfo_cf_inconsistent);
 }
@@ -169,7 +170,7 @@ static int can_move_ret(ir_node *ret)
 /*
  * Normalize the Returns of a graph by moving
  * the Returns upwards as much as possible.
- * This might be prefered for code generation.
+ * This might be preferred for code generation.
  *
  * In pseudocode, it means:
  *
@@ -193,13 +194,19 @@ void normalize_n_returns(ir_graph *irg)
   ir_node *final = NULL;
   ir_node **in;
   ir_node *endbl = get_irg_end_block(irg);
+  ir_node *end;
 
-  /* first, link all returns */
+  /*
+   * First, link all returns:
+   * These must be predecessors of the endblock.
+   * Place Returns that can be moved on list, all others
+   * on final.
+   */
   n = get_Block_n_cfgpreds(endbl);
   for (n_finals = n_rets = i = 0; i < n; ++i) {
     ir_node *ret = get_Block_cfgpred(endbl, i);
 
-    if (get_irn_op(ret) == op_Return && can_move_ret(ret)) {
+    if (is_Return(ret) && can_move_ret(ret)) {
       /*
        * Ok, all conditions met, we can move this Return, put it
        * on our work list.
@@ -224,39 +231,73 @@ void normalize_n_returns(ir_graph *irg)
    * new Returns), than we check if a newly created Return can be moved even further.
    * If yes, we simply add it to our work list, else to the final list.
    */
+  end        = get_irg_end(irg);
   n_ret_vals = get_irn_arity(list);
   in         = alloca(sizeof(*in) * n_ret_vals);
   while (list) {
     ir_node *ret   = list;
     ir_node *block = get_nodes_block(ret);
+    ir_node *phiM;
 
     list = get_irn_link(ret);
     --n_rets;
 
     n = get_Block_n_cfgpreds(block);
     for (i = 0; i < n; ++i) {
-      ir_node *jmp    = get_Block_cfgpred(block, i);
-      ir_node *new_bl = get_nodes_block(jmp);
-      ir_node *new_ret;
+      ir_node *jmp = get_Block_cfgpred(block, i);
+      ir_node *new_bl, *new_ret;
+
+      if (get_irn_op(jmp) != op_Jmp)
+        continue;
+
+      new_bl = get_nodes_block(jmp);
 
       /* create the in-array for the new Ret */
       for (j = 0; j < n_ret_vals; ++j) {
         ir_node *pred = get_irn_n(ret, j);
 
-        in[j] = is_Phi(pred) ? get_Phi_pred(pred, i) : pred;
+        in[j] = (is_Phi(pred) && get_nodes_block(pred) == block) ? get_Phi_pred(pred, i) : pred;
       }
 
       new_ret = new_r_Return(irg, new_bl, in[0], n_ret_vals - 1, &in[1]);
 
-      if (can_move_ret(new_ret)) {
-        set_irn_link(new_ret, list);
-        list = new_ret;
-        ++n_rets;
+      if (! is_Bad(new_ret)) {
+        /*
+         * The newly created node might be bad, if we
+         * create it in a block with only Bad predecessors.
+         * In that case ignore this block.
+         *
+         * We could even kill the jmp then ...
+         */
+        if (can_move_ret(new_ret)) {
+          set_irn_link(new_ret, list);
+          list = new_ret;
+          ++n_rets;
+        }
+        else {
+          set_irn_link(new_ret, final);
+          final = new_ret;
+          ++n_finals;
+        }
       }
-      else {
-        set_irn_link(new_ret, final);
-        final = new_ret;
-        ++n_finals;
+
+      /* remove the Jmp, we have placed a Return here */
+      exchange(jmp, new_r_Bad(irg));
+    }
+
+    /*
+     * if the memory of the old Return is a PhiM, remove it
+     * from the keep-alives, or it will keep the block which
+     * will crash the dominator algorithm.
+     */
+    phiM = get_Return_mem(ret);
+    if (is_Phi(phiM)) {
+      n = get_End_n_keepalives(end);
+      for (i = 0; i < n; ++i) {
+        if (get_End_keepalive(end, i) == phiM) {
+          set_End_keepalive(end, i, new_r_Bad(irg));
+          break;
+        }
       }
     }
   }
@@ -272,14 +313,14 @@ void normalize_n_returns(ir_graph *irg)
 
   exchange(endbl, new_r_Block(irg, n_finals, in));
 
-  /* the end block is not automatically skiped, so do it here */
+  /* the end block is not automatically skipped, so do it here */
   set_irg_end_block(irg, skip_Id(get_irg_end_block(irg)));
 
   /* Invalidate analysis information:
-   * Blocks become dead and new Eeturns were deleted, so dominator, outs and loop are inconsistent,
+   * Blocks become dead and new Returns were deleted, so dominator, outs and loop are inconsistent,
    * trouts and callee-state should be still valid
    */
-  set_irg_dom_inconsistent(irg);
+  set_irg_doms_inconsistent(irg);
   set_irg_outs_inconsistent(irg);
   set_irg_loopinfo_state(current_ir_graph, loopinfo_cf_inconsistent);
 }