fixed multi-input join (thx, Boris) --flo
[libfirm] / ir / ir / iropt.c
index c1dbc08..4643d4e 100644 (file)
@@ -889,6 +889,13 @@ static ir_node *equivalent_node_Conv(ir_node *n)
   return n;
 }
 
+static ir_node *equivalent_node_Cast(ir_node *n) {
+  ir_node *pred = get_Cast_op(n);
+  if (get_irn_type(pred) == get_Cast_type(n))
+    n = pred;
+  return n;
+}
+
 static ir_node *equivalent_node_Phi(ir_node *n)
 {
   /* Several optimizations:
@@ -1065,6 +1072,7 @@ static ir_op *firm_set_default_equivalent_node(ir_op *op)
   CASE(DivMod);
   CASE(And);
   CASE(Conv);
+  CASE(Cast);
   CASE(Phi);
   CASE(Proj);
   CASE(Id);
@@ -1109,8 +1117,8 @@ optimize_preds(ir_node *n) {
   } /* end switch */
 }
 
-static ir_node *transform_node_Mul(ir_node *n)
-{
+/** Do architecture dependend optimizations on Mul nodes */
+static ir_node *transform_node_Mul(ir_node *n) {
   return arch_dep_replace_mul_with_shifts(n);
 }
 
@@ -1124,7 +1132,7 @@ static ir_node *transform_node_Div(ir_node *n)
   if (tv != tarval_bad)
     value = new_Const(get_tarval_mode(tv), tv);
   else /* Try architecture dependand optimization */
-    value = arch_dep_replace_div_with_shifts(n);
+    value = arch_dep_replace_div_by_const(n);
 
   if (value != n) {
     /* Turn Div into a tuple (mem, bad, value) */
@@ -1148,7 +1156,7 @@ static ir_node *transform_node_Mod(ir_node *n)
   if (tv != tarval_bad)
     value = new_Const(get_tarval_mode(tv), tv);
   else /* Try architecture dependand optimization */
-    value = arch_dep_replace_mod_with_shifts(n);
+    value = arch_dep_replace_mod_by_const(n);
 
   if (value != n) {
     /* Turn Mod into a tuple (mem, bad, value) */
@@ -1193,7 +1201,7 @@ static ir_node *transform_node_DivMod(ir_node *n)
       evaluated = 1;
     }
     else { /* Try architecture dependand optimization */
-      arch_dep_replace_divmod_with_shifts(&a, &b, n);
+      arch_dep_replace_divmod_by_const(&a, &b, n);
       evaluated = a != NULL;
     }
   } else if (ta == get_mode_null(mode)) {
@@ -1307,9 +1315,22 @@ static ir_node *transform_node_Not(ir_node *n)
   return n;
 }
 
+static ir_node *transform_node_Cast(ir_node *n) {
+  ir_node *pred = get_Cast_op(n);
+  type *tp = get_irn_type(pred);
+  if (get_irn_op(pred) == op_Const && get_Const_type(pred) != tp) {
+    n = new_rd_Const_type(NULL, current_ir_graph, get_nodes_block(pred), get_irn_mode(pred),
+                         get_Const_tarval(pred), tp);
+  } else if ((get_irn_op(pred) == op_SymConst) && (get_SymConst_value_type(pred) != tp)) {
+    n = new_rd_SymConst_type(NULL, current_ir_graph, get_nodes_block(pred), get_SymConst_symbol(pred),
+                            get_SymConst_kind(pred), tp);
+  }
+  return n;
+}
+
 /**
  * Transform a Div/Mod/DivMod with a non-zero constant. Must be
- * done here instead of equivalent node because in creates new
+ * done here instead of equivalent node because it creates new
  * nodes.
  * Removes the exceptions and routes the memory to the initial mem.
  *
@@ -1330,6 +1351,9 @@ static ir_node *transform_node_Proj(ir_node *proj)
     if (tb != tarval_bad && classify_tarval(tb) != TV_CLASSIFY_NULL) { /* div(x, c) && c != 0 */
       proj_nr = get_Proj_proj(proj);
 
+      /* this node may float */
+      set_irn_pinned(n, op_pin_state_floats);
+
       if (proj_nr == pn_Div_X_except) {
         /* we found an exception handler, remove it */
         return new_Bad();
@@ -1338,6 +1362,7 @@ static ir_node *transform_node_Proj(ir_node *proj)
        /* the memory Proj can be removed */
         ir_node *res = get_Div_mem(n);
         set_Div_mem(n, get_irg_initial_mem(current_ir_graph));
+
        if (proj_nr == pn_Div_M)
           return res;
       }
@@ -1350,6 +1375,9 @@ static ir_node *transform_node_Proj(ir_node *proj)
     if (tb != tarval_bad && classify_tarval(tb) != TV_CLASSIFY_NULL) { /* mod(x, c) && c != 0 */
       proj_nr = get_Proj_proj(proj);
 
+      /* this node may float */
+      set_irn_pinned(n, op_pin_state_floats);
+
       if (proj_nr == pn_Mod_X_except) {
         /* we found an exception handler, remove it */
         return new_Bad();
@@ -1370,6 +1398,9 @@ static ir_node *transform_node_Proj(ir_node *proj)
     if (tb != tarval_bad && classify_tarval(tb) != TV_CLASSIFY_NULL) { /* DivMod(x, c) && c != 0 */
       proj_nr = get_Proj_proj(proj);
 
+      /* this node may float */
+      set_irn_pinned(n, op_pin_state_floats);
+
       if (proj_nr == pn_DivMod_X_except) {
         /* we found an exception handler, remove it */
         return new_Bad();
@@ -1520,6 +1551,65 @@ static ir_node *transform_node_Or(ir_node *or)
   return transform_node_Or(or);
 }
 
+/* forward */
+static ir_node *transform_node(ir_node *n);
+
+/**
+ * Optimize (a >> c1) >> c2), works for Shr, Shrs, Shl
+ */
+static ir_node * transform_node_shift(ir_node *n)
+{
+  ir_node *left;
+  tarval *tv1, *tv2, *res;
+  ir_mode *mode;
+  int modulo_shf, flag;
+
+  left = get_binop_left(n);
+
+  /* different operations */
+  if (get_irn_op(left) != get_irn_op(n))
+    return n;
+
+  tv1 = computed_value(get_binop_right(n));
+  if (tv1 == tarval_bad)
+    return n;
+
+  tv2 = computed_value(get_binop_right(left));
+  if (tv2 == tarval_bad)
+    return n;
+
+  res = tarval_add(tv1, tv2);
+
+  /* beware: a simple replacement works only, if res < modulo shift */
+  mode = get_irn_mode(n);
+
+  flag = 0;
+
+  modulo_shf = get_mode_modulo_shift(mode);
+  if (modulo_shf > 0) {
+    tarval *modulo = new_tarval_from_long(modulo_shf, get_tarval_mode(res));
+
+    if (tarval_cmp(res, modulo) & Lt)
+      flag = 1;
+  }
+  else
+    flag = 1;
+
+  if (flag) {
+    /* ok, we can replace it */
+    ir_node *in[2], *irn, *block = get_nodes_block(n);
+
+    in[0] = get_binop_left(left);
+    in[1] = new_r_Const(current_ir_graph, block, get_tarval_mode(res), res);
+
+    irn = new_ir_node(NULL, current_ir_graph, block, get_irn_op(n), mode, 2, in);
+
+    return transform_node(irn);
+  }
+  return n;
+}
+
+
 /**
  * Tries several [inplace] [optimizing] transformations and returns an
  * equivalent node.  The difference to equivalent_node() is that these
@@ -1551,8 +1641,14 @@ static ir_op *firm_set_default_transform_node(ir_op *op)
   CASE(Cond);
   CASE(Eor);
   CASE(Not);
+  CASE(Cast);
   CASE(Proj);
   CASE(Or);
+  case iro_Shr:
+  case iro_Shrs:
+  case iro_Shl:
+    op->transform_node  = transform_node_shift;
+    break;
   default:
     op->transform_node  = NULL;
   }
@@ -1604,7 +1700,8 @@ static int node_cmp_attr_Free(ir_node *a, ir_node *b)
 static int node_cmp_attr_SymConst(ir_node *a, ir_node *b)
 {
     return (get_irn_symconst_attr(a).num != get_irn_symconst_attr(b).num)
-      || (get_irn_symconst_attr(a).sym.type_p != get_irn_symconst_attr(b).sym.type_p);
+      || (get_irn_symconst_attr(a).sym.type_p != get_irn_symconst_attr(b).sym.type_p)
+      || (get_irn_symconst_attr(a).tp != get_irn_symconst_attr(b).tp);
 }
 
 /** Compares the attributes of two Call nodes. */
@@ -1657,7 +1754,7 @@ static int node_cmp_attr_Store(ir_node *a, ir_node *b)
 {
   /* NEVER do CSE on volatile Stores */
   return (get_Store_volatility(a) == volatility_is_volatile ||
-      get_Load_volatility(b) == volatility_is_volatile);
+      get_Store_volatility(b) == volatility_is_volatile);
 }
 
 /**
@@ -1939,7 +2036,14 @@ optimize_node (ir_node *n)
   ir_node *oldn = n;
   opcode iro = get_irn_opcode(n);
 
-  /* Always optimize Phi nodes: part of the construction. */
+  type *old_tp = get_irn_type(n);
+  {
+    int i, arity = get_irn_arity(n);
+    for (i = 0; i < arity && !old_tp; ++i)
+      old_tp = get_irn_type(get_irn_n(n, i));
+  }
+
+  /* Allways optimize Phi nodes: part of the construction. */
   if ((!get_opt_optimize()) && (iro != iro_Phi)) return n;
 
   /* constant expression evaluation / constant folding */
@@ -1965,8 +2069,9 @@ optimize_node (ir_node *n)
         /* evaluation was successful -- replace the node. */
         obstack_free (current_ir_graph->obst, n);
         n = new_Const (get_tarval_mode (tv), tv);
-
-        DBG_OPT_ALGSIM0(oldn, n);
+       if (old_tp && get_type_mode(old_tp) == get_tarval_mode (tv))
+         set_Const_type(n, old_tp);
+                                                 DBG_OPT_ALGSIM0(oldn, n);
         return n;
       }
     }
@@ -2030,6 +2135,13 @@ optimize_in_place_2 (ir_node *n)
   ir_node *oldn = n;
   opcode iro = get_irn_opcode(n);
 
+  type *old_tp = get_irn_type(n);
+  {
+    int i, arity = get_irn_arity(n);
+    for (i = 0; i < arity && !old_tp; ++i)
+      old_tp = get_irn_type(get_irn_n(n, i));
+  }
+
   if (!get_opt_optimize() && (get_irn_op(n) != op_Phi)) return n;
 
   /* if not optimize return n */
@@ -2039,7 +2151,6 @@ optimize_in_place_2 (ir_node *n)
     return n;
   }
 
-
   /* constant expression evaluation / constant folding */
   if (get_opt_constant_folding()) {
     /* constants can not be evaluated */
@@ -2049,8 +2160,9 @@ optimize_in_place_2 (ir_node *n)
       if ((get_irn_mode(n) != mode_T) && (tv != tarval_bad)) {
         /* evaluation was successful -- replace the node. */
         n = new_Const (get_tarval_mode (tv), tv);
-
-        DBG_OPT_ALGSIM0(oldn, n);
+       if (old_tp && get_type_mode(old_tp) == get_tarval_mode (tv))
+         set_Const_type(n, old_tp);
+                                                            DBG_OPT_ALGSIM0(oldn, n);
         return n;
       }
     }