phase handling
[libfirm] / ir / ir / iropt.c
index c093a64..ce0e569 100644 (file)
@@ -494,13 +494,26 @@ static ir_node *equivalent_node_Block(ir_node *n)
      This should be true, as the block is matured before optimize is called.
      But what about Phi-cycles with the Phi0/Id that could not be resolved?
      Remaining Phi nodes are just Ids. */
-  if ((get_Block_n_cfgpreds(n) == 1) &&
-      (get_irn_op(get_Block_cfgpred(n, 0)) == op_Jmp) &&
-      (get_opt_control_flow_straightening())) {
-    n = get_nodes_Block(get_Block_cfgpred(n, 0));                     DBG_OPT_STG;
-
-  } else if ((get_Block_n_cfgpreds(n) == 2) &&
-            (get_opt_control_flow_weak_simplification())) {
+   if ((get_Block_n_cfgpreds(n) == 1) &&
+       (get_irn_op(get_Block_cfgpred(n, 0)) == op_Jmp)) {
+     ir_node *predblock = get_nodes_Block(get_Block_cfgpred(n, 0));
+     if (predblock == oldn) {
+       /* Jmp jumps into the block it is in -- deal self cycle. */
+       n = new_Bad();                                      DBG_OPT_DEAD;
+     } else if (get_opt_control_flow_straightening()) {
+       n = predblock;                                      DBG_OPT_STG;
+     }
+   }
+   else if ((get_Block_n_cfgpreds(n) == 1) &&
+           (get_irn_op(skip_Proj(get_Block_cfgpred(n, 0))) == op_Cond)) {
+     ir_node *predblock = get_nodes_Block(get_Block_cfgpred(n, 0));
+     if (predblock == oldn) {
+       /* Jmp jumps into the block it is in -- deal self cycle. */
+       n = new_Bad();                                      DBG_OPT_DEAD;
+     }
+   }
+   else if ((get_Block_n_cfgpreds(n) == 2) &&
+           (get_opt_control_flow_weak_simplification())) {
     /* Test whether Cond jumps twice to this block
        @@@ we could do this also with two loops finding two preds from several ones. */
     ir_node *a = get_Block_cfgpred(n, 0);
@@ -774,7 +787,7 @@ static ir_node *equivalent_node_Phi(ir_node *n)
 
   n_preds = get_Phi_n_preds(n);
 
-  block = get_nodes_Block(n);
+  block = get_nodes_block(n);
   /* @@@ fliegt 'raus, sollte aber doch immer wahr sein!!!
      assert(get_irn_arity(block) == n_preds && "phi in wrong block!"); */
   if ((is_Bad(block)) ||                         /* Control dead */
@@ -789,8 +802,8 @@ static ir_node *equivalent_node_Phi(ir_node *n)
      value that is known at a certain point.  This is useful for
      dataflow analysis. */
   if (n_preds == 2) {
-    ir_node *a = follow_Id (get_Phi_pred(n, 0));
-    ir_node *b = follow_Id (get_Phi_pred(n, 1));
+    ir_node *a = get_Phi_pred(n, 0);
+    ir_node *b = get_Phi_pred(n, 1);
     if (   (get_irn_op(a) == op_Confirm)
        && (get_irn_op(b) == op_Confirm)
        && follow_Id (get_irn_n(a, 0) == get_irn_n(b, 0))
@@ -801,15 +814,20 @@ static ir_node *equivalent_node_Phi(ir_node *n)
   }
 #endif
 
+  /* If the Block has a Bad pred, we also have one. */
+  for (i = 0;  i < n_preds;  ++i)
+    if (is_Bad (get_Block_cfgpred(block, i)))
+      set_Phi_pred(n, i, new_Bad());
+
   /* Find first non-self-referencing input */
   for (i = 0;  i < n_preds;  ++i) {
-    first_val = follow_Id(get_Phi_pred(n, i));
-    /* skip Id's */
-    set_Phi_pred(n, i, first_val);
+    first_val = get_Phi_pred(n, i);
     if (   (first_val != n)                            /* not self pointer */
-       && (get_irn_op(first_val) != op_Bad)           /* value not dead */
-       && !(is_Bad (get_Block_cfgpred(block, i))) ) { /* not dead control flow */
-      break;                         /* then found first value. */
+#if 1
+       && (get_irn_op(first_val) != op_Bad)
+#endif
+          ) {        /* value not dead */
+      break;          /* then found first value. */
     }
   }
 
@@ -821,13 +839,13 @@ static ir_node *equivalent_node_Phi(ir_node *n)
   /* follow_Id () for rest of inputs, determine if any of these
      are non-self-referencing */
   while (++i < n_preds) {
-    scnd_val = follow_Id(get_Phi_pred(n, i));
-    /* skip Id's */
-    set_Phi_pred(n, i, scnd_val);
+    scnd_val = get_Phi_pred(n, i);
     if (   (scnd_val != n)
        && (scnd_val != first_val)
+#if 1
        && (get_irn_op(scnd_val) != op_Bad)
-       && !(is_Bad (get_Block_cfgpred(block, i))) ) {
+#endif
+          ) {
       break;
     }
   }
@@ -836,10 +854,9 @@ static ir_node *equivalent_node_Phi(ir_node *n)
   if (i >= n_preds) {
     n = first_val;                                     DBG_OPT_PHI;
   } else {
-    /* skip the remaining Ids. */
-    while (++i < n_preds) {
-      set_Phi_pred(n, i, follow_Id(get_Phi_pred(n, i)));
-    }
+    /* skip the remaining Ids (done in get_Phi_pred). */
+    /* superfluous, since we walk all to propagate Block's Bads.
+       while (++i < n_preds) get_Phi_pred(n, i);     */
   }
   return n;
 }
@@ -1543,7 +1560,7 @@ optimize_node (ir_node *n)
   /* constant expression evaluation / constant folding */
   if (get_opt_constant_folding()) {
     /* constants can not be evaluated */
-    if  (get_irn_op(n) != op_Const) {
+    if (iro != iro_Const) {
       /* try to evaluate */
       tv = computed_value (n);
       if ((get_irn_mode(n) != mode_T) && (tv != tarval_bad)) {
@@ -1553,7 +1570,7 @@ optimize_node (ir_node *n)
         */
        ir_node x = *n;
        oldn = &x;
-        /* evaluation was succesful -- replace the node. */
+        /* evaluation was successful -- replace the node. */
        obstack_free (current_ir_graph->obst, n);
        n = new_Const (get_tarval_mode (tv), tv);
                                                        DBG_OPT_ALGSIM0;
@@ -1637,7 +1654,7 @@ optimize_in_place_2 (ir_node *n)
       /* try to evaluate */
       tv = computed_value (n);
       if ((get_irn_mode(n) != mode_T) && (tv != tarval_bad)) {
-        /* evaluation was succesful -- replace the node. */
+        /* evaluation was successful -- replace the node. */
        n = new_Const (get_tarval_mode (tv), tv);
                                                DBG_OPT_ALGSIM0;
        return n;