Allow the Bad node to be set in set_store(). This is neccessary, because Bad might...
[libfirm] / ir / ir / irarch.c
index 3420044..89bd0d5 100644 (file)
@@ -1,12 +1,19 @@
 /**
  * @file irarch.c
  * @date 28.9.2004
- * @author Sebastian Hack
- * @brief Machine dependent firm optimizations.
+ * @author Sebastian Hack, Michael Beck
+ * @brief Machine dependent Firm optimizations.
  *
  * $Id$
  */
-#include <stdlib.h>
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#ifdef HAVE_STDLIB_H
+# include <stdlib.h>
+#endif
+
 #include <assert.h>
 
 #include "irnode_t.h"
 #include "ircons_t.h"
 #include "irgmod.h"
 #include "irvrfy.h"
-#include "tv.h"
+#include "tv_t.h"
 #include "dbginfo_t.h"
 #include "iropt_dbg.h"
 #include "irflag_t.h"
 #include "irhooks.h"
 #include "ircons.h"
 #include "irarch.h"
+#include "irreflect.h"
 
 #undef DEB
 
@@ -54,10 +62,6 @@ new_rd_Mulh (dbg_info *db, ir_graph *irg, ir_node *block,
   ir_node *in[2];
   ir_node *res;
 
-  if (! op_Mulh) {
-    op_Mulh = new_ir_op(get_next_ir_opcode(), "Mulh",  op_pin_state_floats, irop_flag_commutative, oparity_binary, 0, 0);
-  }
-
   in[0] = op1;
   in[1] = op2;
   res = new_ir_node(db, irg, block, op_Mulh, mode, 2, in);
@@ -75,11 +79,20 @@ void arch_dep_init(arch_dep_params_factory_t factory)
   if (factory != NULL)
     params = factory();
 
-  if (params && (opts & (arch_dep_div_by_const|arch_dep_mod_by_const))) {
-    if (! op_Mulh) {
-      /* create the Mulh operation */
-      op_Mulh = new_ir_op(get_next_ir_opcode(), "Mulh",  op_pin_state_floats, irop_flag_commutative, oparity_binary, 0, 0);
-    }
+  if (! op_Mulh) {
+    rflct_sig_t *sig;
+    int mulh_opc = get_next_ir_opcode();
+
+    /* create the Mulh operation */
+    op_Mulh = new_ir_op(mulh_opc, "Mulh",  op_pin_state_floats, irop_flag_commutative, oparity_binary, 0, 0, NULL);
+    sig = rflct_signature_allocate(1, 3);
+    rflct_signature_set_arg(sig, 0, 0, "Res", RFLCT_MC(Int), 0, 0);
+    rflct_signature_set_arg(sig, 1, 0, "Block", RFLCT_MC(BB), 0, 0);
+    rflct_signature_set_arg(sig, 1, 1, "Op 0", RFLCT_MC(Int), 0, 0);
+    rflct_signature_set_arg(sig, 1, 2, "Op 1", RFLCT_MC(Int), 0, 0);
+
+    rflct_new_opcode(mulh_opc, "Mulh", 0);
+    rflct_opcode_add_signature(mulh_opc, sig);
   }
 }
 
@@ -87,7 +100,7 @@ void arch_dep_set_opts(arch_dep_opts_t the_opts) {
   opts = the_opts;
 }
 
-/* check, wheater a mode allows a Mulh instruction */
+/** check, whether a mode allows a Mulh instruction. */
 static int allow_Mulh(ir_mode *mode)
 {
   if (get_mode_size_bits(mode) > params->max_bits_for_mulh)
@@ -95,6 +108,7 @@ static int allow_Mulh(ir_mode *mode)
   return (mode_is_signed(mode) && params->allow_mulhs) || (!mode_is_signed(mode) && params->allow_mulhu);
 }
 
+/* Replace Muls with Shifts and Add/Subs. */
 ir_node *arch_dep_replace_mul_with_shifts(ir_node *irn)
 {
   ir_node *res = irn;
@@ -102,18 +116,18 @@ ir_node *arch_dep_replace_mul_with_shifts(ir_node *irn)
 
   /* If the architecture dependent optimizations were not initialized
      or this optimization was not enabled. */
-  if(params == NULL || (opts & arch_dep_mul_to_shift) == 0)
+  if (params == NULL || (opts & arch_dep_mul_to_shift) == 0)
     return irn;
 
-  if(get_irn_opcode(irn) == iro_Mul && mode_is_int(mode)) {
-    ir_node *block   = get_nodes_block(irn);
+  if (get_irn_op(irn) == op_Mul && mode_is_int(mode)) {
+    ir_node *block   = get_irn_n(irn, -1);
     ir_node *left    = get_binop_left(irn);
     ir_node *right   = get_binop_right(irn);
     tarval *tv       = NULL;
     ir_node *operand = NULL;
 
     /* Look, if one operand is a constant. */
-    if(get_irn_opcode(left) == iro_Const) {
+    if (get_irn_opcode(left) == iro_Const) {
       tv = get_Const_tarval(left);
       operand = right;
     } else if(get_irn_opcode(right) == iro_Const) {
@@ -121,7 +135,7 @@ ir_node *arch_dep_replace_mul_with_shifts(ir_node *irn)
       operand = left;
     }
 
-    if(tv != NULL) {
+    if (tv != NULL) {
       int maximum_shifts = params->maximum_shifts;
       int also_use_subs = params->also_use_subs;
       int highest_shift_amount = params->highest_shift_amount;
@@ -177,7 +191,7 @@ ir_node *arch_dep_replace_mul_with_shifts(ir_node *irn)
       }
 #endif
 
-      // Go over all recorded one groups.
+      /* Go over all recorded one groups. */
       curr_bit = compr[0];
 
       for(i = 1; i < compr_len; i = end_of_group + 2) {
@@ -186,7 +200,7 @@ ir_node *arch_dep_replace_mul_with_shifts(ir_node *irn)
         ones_in_group = compr[i];
         zeros_in_group = 0;
 
-        // Scan for singular 0s in a sequence
+        /* Scan for singular 0s in a sequence. */
         for(j = i + 1; j < compr_len && compr[j] == 1; j += 2) {
           zeros_in_group += 1;
           ones_in_group += (j + 1 < compr_len ? compr[j + 1] : 0);
@@ -210,8 +224,8 @@ ir_node *arch_dep_replace_mul_with_shifts(ir_node *irn)
           fprintf(stderr, "    j:%d, ones:%d\n", j, curr_ones);
 #endif
 
-          // If this ones group is a singleton group (it has no
-          // singleton zeros inside
+          /* If this ones group is a singleton group (it has no
+             singleton zeros inside. */
           if(singleton)
             shift_with_sub[shift_with_sub_pos++] = biased_curr_bit;
           else if(j == i)
@@ -294,17 +308,15 @@ ir_node *arch_dep_replace_mul_with_shifts(ir_node *irn)
               int amount = abs(curr_shift) - 1;
               ir_node *aux = operand;
 
-
               assert(amount >= 0 && "What is a negative shift??");
 
-              if(amount != 0) {
-                tarval *shift_amount = new_tarval_from_long(amount, mode_Iu);
-                ir_node *cnst = new_r_Const(current_ir_graph, block, mode_Iu, shift_amount);
+              if (amount != 0) {
+                ir_node *cnst = new_r_Const_long(current_ir_graph, block, mode_Iu, amount);
                 aux = new_r_Shl(current_ir_graph, block, operand, cnst, mode);
               }
 
-              if(curr) {
-                if(sub)
+              if (curr) {
+                if (sub)
                   curr = new_r_Sub(current_ir_graph, block, curr, aux, mode);
                 else
                   curr = new_r_Add(current_ir_graph, block, curr, aux, mode);
@@ -320,7 +332,7 @@ ir_node *arch_dep_replace_mul_with_shifts(ir_node *irn)
 #ifdef DEB
         {
           const char *prefix = "";
-          for(i = 0; i < n; i++) {
+          for (i = 0; i < n; ++i) {
             fprintf(stderr, "%s%d", prefix, shifts[i]);
             prefix = ", ";
           }
@@ -384,6 +396,7 @@ static int tv_ld2(tarval *tv, int bits)
 #define ONE(m)    get_mode_one(m)
 #define ZERO(m)   get_mode_null(m)
 
+/** The result of a the magic() function. */
 struct ms {
   tarval *M;        /**< magic number */
   int s;            /**< shift amount */
@@ -470,6 +483,7 @@ static struct ms magic(tarval *d)
   return mag;
 }
 
+/** The result of the magicu() function. */
 struct mu {
   tarval *M;        /**< magic add constant */
   int s;            /**< shift amount */
@@ -546,15 +560,15 @@ static struct mu magicu(tarval *d)
 }
 
 /**
- * build the Mulh replacement code for n / tv
+ * Build the Mulh replacement code for n / tv.
  *
- * Note thet 'div' might be a mod or DivMod operation as well
+ * Note that 'div' might be a mod or DivMod operation as well
  */
 static ir_node *replace_div_by_mulh(ir_node *div, tarval *tv)
 {
   dbg_info *dbg  = get_irn_dbg_info(div);
   ir_node *n     = get_binop_left(div);
-  ir_node *block = get_nodes_block(div);
+  ir_node *block = get_irn_n(div, -1);
   ir_mode *mode  = get_irn_mode(n);
   int bits       = get_mode_size_bits(mode);
   ir_node *q, *t, *c;
@@ -578,12 +592,12 @@ static ir_node *replace_div_by_mulh(ir_node *div, tarval *tv)
 
     /* Do we need the shift */
     if (mag.s > 0) {
-      c = new_r_Const(current_ir_graph, block, mode_Iu, new_tarval_from_long(mag.s, mode_Iu));
+      c = new_r_Const_long(current_ir_graph, block, mode_Iu, mag.s);
       q    = new_rd_Shrs(dbg, current_ir_graph, block, q, c, mode);
     }
 
     /* final */
-    c = new_r_Const(current_ir_graph, block, mode_Iu, new_tarval_from_long(bits-1, mode_Iu));
+    c = new_r_Const_long(current_ir_graph, block, mode_Iu, bits-1);
     t = new_rd_Shr(dbg, current_ir_graph, block, q, c, mode);
 
     q = new_rd_Add(dbg, current_ir_graph, block, q, t, mode);
@@ -606,7 +620,7 @@ static ir_node *replace_div_by_mulh(ir_node *div, tarval *tv)
 
         t = new_rd_Add(dbg, current_ir_graph, block, t, q, mode);
 
-        c = new_r_Const(current_ir_graph, block, mode_Iu, new_tarval_from_long(mag.s-1, mode_Iu));
+        c = new_r_Const_long(current_ir_graph, block, mode_Iu, mag.s-1);
         q = new_rd_Shr(dbg, current_ir_graph, block, t, c, mode);
       }
       else {
@@ -615,13 +629,14 @@ static ir_node *replace_div_by_mulh(ir_node *div, tarval *tv)
       }
     }
     else if (mag.s > 0) { /* default scheme, shift needed */
-      c = new_r_Const(current_ir_graph, block, mode_Iu, new_tarval_from_long(mag.s, mode_Iu));
+      c = new_r_Const_long(current_ir_graph, block, mode_Iu, mag.s);
       q = new_rd_Shr(dbg, current_ir_graph, block, q, c, mode);
     }
   }
   return q;
 }
 
+/* Replace Divs with Shifts and Add/Subs and Mulh. */
 ir_node *arch_dep_replace_div_by_const(ir_node *irn)
 {
   ir_node *res  = irn;
@@ -643,11 +658,16 @@ ir_node *arch_dep_replace_div_by_const(ir_node *irn)
     if (get_irn_op(c) != op_Const)
       return irn;
 
+    tv = get_Const_tarval(c);
+
+    /* check for division by zero */
+    if (classify_tarval(tv) == TV_CLASSIFY_NULL)
+      return irn;
+
     left  = get_Div_left(irn);
     mode  = get_irn_mode(left);
-    block = get_nodes_block(irn);
+    block = get_irn_n(irn, -1);
     dbg   = get_irn_dbg_info(irn);
-    tv    = get_Const_tarval(c);
 
     bits = get_mode_size_bits(mode);
     n    = (bits + 7) / 8;
@@ -671,16 +691,16 @@ ir_node *arch_dep_replace_div_by_const(ir_node *irn)
         ir_node *curr = left;
 
         if (k != 1) {
-          k_node = new_r_Const(current_ir_graph, block, mode_Iu, new_tarval_from_long(k - 1, mode_Iu));
+          k_node = new_r_Const_long(current_ir_graph, block, mode_Iu, k - 1);
           curr   = new_rd_Shrs(dbg, current_ir_graph, block, left, k_node, mode);
         }
 
-        k_node = new_r_Const(current_ir_graph, block, mode_Iu, new_tarval_from_long(bits - k, mode_Iu));
+        k_node = new_r_Const_long(current_ir_graph, block, mode_Iu, bits - k);
         curr   = new_rd_Shr(dbg, current_ir_graph, block, curr, k_node, mode);
 
         curr   = new_rd_Add(dbg, current_ir_graph, block, left, curr, mode);
 
-        k_node = new_r_Const(current_ir_graph, block, mode_Iu, new_tarval_from_long(k, mode_Iu));
+        k_node = new_r_Const_long(current_ir_graph, block, mode_Iu, k);
         res    = new_rd_Shrs(dbg, current_ir_graph, block, curr, k_node, mode);
 
         if (n_flag) { /* negate the result */
@@ -693,7 +713,7 @@ ir_node *arch_dep_replace_div_by_const(ir_node *irn)
       else {      /* unsigned case */
         ir_node *k_node;
 
-        k_node = new_r_Const(current_ir_graph, block, mode_Iu, new_tarval_from_long(k, mode_Iu));
+        k_node = new_r_Const_long(current_ir_graph, block, mode_Iu, k);
         res    = new_rd_Shr(dbg, current_ir_graph, block, left, k_node, mode);
       }
     }
@@ -705,11 +725,12 @@ ir_node *arch_dep_replace_div_by_const(ir_node *irn)
   }
 
   if (res != irn)
-    hook_arch_dep_replace_div_by_const(irn);
+    hook_arch_dep_replace_division_by_const(irn);
 
   return res;
 }
 
+/* Replace Mods with Shifts and Add/Subs and Mulh. */
 ir_node *arch_dep_replace_mod_by_const(ir_node *irn)
 {
   ir_node *res  = irn;
@@ -731,12 +752,16 @@ ir_node *arch_dep_replace_mod_by_const(ir_node *irn)
     if (get_irn_op(c) != op_Const)
       return irn;
 
+    tv = get_Const_tarval(c);
+
+    /* check for division by zero */
+    if (classify_tarval(tv) == TV_CLASSIFY_NULL)
+      return irn;
+
     left  = get_Mod_left(irn);
     mode  = get_irn_mode(left);
-    block = get_nodes_block(irn);
+    block = get_irn_n(irn, -1);
     dbg   = get_irn_dbg_info(irn);
-    tv    = get_Const_tarval(c);
-
     bits = get_mode_size_bits(mode);
     n    = (bits + 7) / 8;
 
@@ -760,16 +785,16 @@ ir_node *arch_dep_replace_mod_by_const(ir_node *irn)
         ir_node *curr = left;
 
         if (k != 1) {
-          k_node = new_r_Const(current_ir_graph, block, mode_Iu, new_tarval_from_long(k - 1, mode_Iu));
+          k_node = new_r_Const_long(current_ir_graph, block, mode_Iu, k - 1);
           curr   = new_rd_Shrs(dbg, current_ir_graph, block, left, k_node, mode);
         }
 
-        k_node = new_r_Const(current_ir_graph, block, mode_Iu, new_tarval_from_long(bits - k, mode_Iu));
+        k_node = new_r_Const_long(current_ir_graph, block, mode_Iu, bits - k);
         curr   = new_rd_Shr(dbg, current_ir_graph, block, curr, k_node, mode);
 
         curr   = new_rd_Add(dbg, current_ir_graph, block, left, curr, mode);
 
-        k_node = new_r_Const(current_ir_graph, block, mode, new_tarval_from_long((-1) << k, mode));
+        k_node = new_r_Const_long(current_ir_graph, block, mode, (-1) << k);
         curr   = new_rd_And(dbg, current_ir_graph, block, curr, k_node, mode);
 
         res    = new_rd_Sub(dbg, current_ir_graph, block, left, curr, mode);
@@ -777,7 +802,7 @@ ir_node *arch_dep_replace_mod_by_const(ir_node *irn)
       else {      /* unsigned case */
         ir_node *k_node;
 
-        k_node = new_r_Const(current_ir_graph, block, mode, new_tarval_from_long((1 << k) - 1, mode));
+        k_node = new_r_Const_long(current_ir_graph, block, mode, (1 << k) - 1);
         res    = new_rd_And(dbg, current_ir_graph, block, left, k_node, mode);
       }
     }
@@ -796,11 +821,12 @@ ir_node *arch_dep_replace_mod_by_const(ir_node *irn)
   }
 
   if (res != irn)
-    hook_arch_dep_replace_mod_by_const(irn);
+    hook_arch_dep_replace_division_by_const(irn);
 
   return res;
 }
 
+/* Replace DivMods with Shifts and Add/Subs and Mulh. */
 void arch_dep_replace_divmod_by_const(ir_node **div, ir_node **mod, ir_node *irn)
 {
   *div = *mod = NULL;
@@ -823,11 +849,16 @@ void arch_dep_replace_divmod_by_const(ir_node **div, ir_node **mod, ir_node *irn
     if (get_irn_op(c) != op_Const)
       return;
 
+    tv = get_Const_tarval(c);
+
+    /* check for division by zero */
+    if (classify_tarval(tv) == TV_CLASSIFY_NULL)
+      return;
+
     left  = get_DivMod_left(irn);
     mode  = get_irn_mode(left);
-    block = get_nodes_block(irn);
+    block = get_irn_n(irn, -1);
     dbg   = get_irn_dbg_info(irn);
-    tv    = get_Const_tarval(c);
 
     bits = get_mode_size_bits(mode);
     n    = (bits + 7) / 8;
@@ -851,16 +882,16 @@ void arch_dep_replace_divmod_by_const(ir_node **div, ir_node **mod, ir_node *irn
         ir_node *curr = left;
 
         if (k != 1) {
-          k_node = new_r_Const(current_ir_graph, block, mode_Iu, new_tarval_from_long(k - 1, mode_Iu));
+          k_node = new_r_Const_long(current_ir_graph, block, mode_Iu, k - 1);
           curr   = new_rd_Shrs(dbg, current_ir_graph, block, left, k_node, mode);
         }
 
-        k_node = new_r_Const(current_ir_graph, block, mode_Iu, new_tarval_from_long(bits - k, mode_Iu));
+        k_node = new_r_Const_long(current_ir_graph, block, mode_Iu, bits - k);
         curr   = new_rd_Shr(dbg, current_ir_graph, block, curr, k_node, mode);
 
         curr   = new_rd_Add(dbg, current_ir_graph, block, left, curr, mode);
 
-        c_k    = new_r_Const(current_ir_graph, block, mode_Iu, new_tarval_from_long(k, mode_Iu));
+        c_k    = new_r_Const_long(current_ir_graph, block, mode_Iu, k);
 
         *div   = new_rd_Shrs(dbg, current_ir_graph, block, curr, c_k, mode);
 
@@ -871,7 +902,7 @@ void arch_dep_replace_divmod_by_const(ir_node **div, ir_node **mod, ir_node *irn
           *div = new_rd_Sub(dbg, current_ir_graph, block, k_node, *div, mode);
         }
 
-        k_node = new_r_Const(current_ir_graph, block, mode, new_tarval_from_long((-1) << k, mode));
+        k_node = new_r_Const_long(current_ir_graph, block, mode, (-1) << k);
         curr   = new_rd_And(dbg, current_ir_graph, block, curr, k_node, mode);
 
         *mod   = new_rd_Sub(dbg, current_ir_graph, block, left, curr, mode);
@@ -879,10 +910,10 @@ void arch_dep_replace_divmod_by_const(ir_node **div, ir_node **mod, ir_node *irn
       else {      /* unsigned case */
         ir_node *k_node;
 
-        k_node = new_r_Const(current_ir_graph, block, mode_Iu, new_tarval_from_long(k, mode_Iu));
+        k_node = new_r_Const_long(current_ir_graph, block, mode_Iu, k);
         *div   = new_rd_Shr(dbg, current_ir_graph, block, left, k_node, mode);
 
-        k_node = new_r_Const(current_ir_graph, block, mode, new_tarval_from_long((1 << k) - 1, mode));
+        k_node = new_r_Const_long(current_ir_graph, block, mode, (1 << k) - 1);
         *mod   = new_rd_And(dbg, current_ir_graph, block, left, k_node, mode);
       }
     }
@@ -903,7 +934,7 @@ void arch_dep_replace_divmod_by_const(ir_node **div, ir_node **mod, ir_node *irn
   }
 
   if (*div)
-    hook_arch_dep_replace_DivMod_by_const(irn);
+    hook_arch_dep_replace_division_by_const(irn);
 }
 
 
@@ -917,6 +948,7 @@ static const arch_dep_params_t default_params = {
   32  /* Mulh allowed up to 32 bit */
 };
 
+/* A default parameter factory for testing purposes. */
 const arch_dep_params_t *arch_dep_default_factory(void) {
   return &default_params;
 }