Fixed optimization of Div/Mod/DivMod nodes
[libfirm] / ir / ir / irarch.c
index 3420044..81e118e 100644 (file)
@@ -16,7 +16,7 @@
 #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"
@@ -87,7 +87,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)
@@ -102,18 +102,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 +121,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;
@@ -294,17 +294,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 +318,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 = ", ";
           }
@@ -548,13 +546,13 @@ static struct mu magicu(tarval *d)
 /**
  * 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 +576,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 +604,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,7 +613,7 @@ 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);
     }
   }
@@ -645,7 +643,7 @@ ir_node *arch_dep_replace_div_by_const(ir_node *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);
 
@@ -671,16 +669,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 +691,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);
       }
     }
@@ -733,7 +731,7 @@ ir_node *arch_dep_replace_mod_by_const(ir_node *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);
 
@@ -760,16 +758,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 +775,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);
       }
     }
@@ -825,7 +823,7 @@ void arch_dep_replace_divmod_by_const(ir_node **div, ir_node **mod, ir_node *irn
 
     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);
 
@@ -851,16 +849,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 +869,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 +877,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);
       }
     }