OSDN Git Service

CLIX patch from Thomas Dickey via urs@akk.uni-karlsruhe.de (Urs Janssen).
[pf3gnuchains/gcc-fork.git] / gcc / unroll.c
index 4470c5b..2f9d969 100644 (file)
@@ -191,6 +191,7 @@ static rtx loop_iteration_var;
 static rtx loop_initial_value;
 static rtx loop_increment;
 static rtx loop_final_value;
+static enum rtx_code loop_comparison_code;
 
 /* Forward declarations.  */
 
@@ -267,8 +268,12 @@ unroll_loop (loop_end, insn_count, loop_start, end_insert_before,
      of block_beg and block_end notes, because that would unbalance the block
      structure of the function.  This can happen as a result of the
      "if (foo) bar; else break;" optimization in jump.c.  */
+  /* ??? Gcc has a general policy that -g is never supposed to change the code
+     that the compiler emits, so we must disable this optimization always,
+     even if debug info is not being output.  This is rare, so this should
+     not be a significant performance problem.  */
 
-  if (write_symbols != NO_DEBUG)
+  if (1 /* write_symbols != NO_DEBUG */)
     {
       int block_begins = 0;
       int block_ends = 0;
@@ -632,6 +637,23 @@ unroll_loop (loop_end, insn_count, loop_start, end_insert_before,
       copy_end = last_loop_insn;
     }
 
+  if (unroll_type == UNROLL_NAIVE
+      && GET_CODE (last_loop_insn) == JUMP_INSN
+      && start_label != JUMP_LABEL (last_loop_insn))
+    {
+      /* ??? The loop ends with a conditional branch that does not branch back
+        to the loop start label.  In this case, we must emit an unconditional
+        branch to the loop exit after emitting the final branch.
+        copy_loop_body does not have support for this currently, so we
+        give up.  It doesn't seem worthwhile to unroll anyways since
+        unrolling would increase the number of branch instructions
+        executed.  */
+      if (loop_dump_stream)
+       fprintf (loop_dump_stream,
+                "Unrolling failure: final conditional branch not to loop start\n");
+      return;
+    }
+
   /* Allocate a translation table for the labels and insn numbers.
      They will be filled in as we copy the insns in the loop.  */
 
@@ -743,12 +765,37 @@ unroll_loop (loop_end, insn_count, loop_start, end_insert_before,
     if (copy_start == loop_start)
       copy_start_luid++;
 
+    /* If a pseudo's lifetime is entirely contained within this loop, then we
+       can use a different pseudo in each unrolled copy of the loop.  This
+       results in better code.  */
     for (j = FIRST_PSEUDO_REGISTER; j < max_reg_before_loop; ++j)
       if (regno_first_uid[j] > 0 && regno_first_uid[j] <= max_uid_for_loop
          && uid_luid[regno_first_uid[j]] >= copy_start_luid
          && regno_last_uid[j] > 0 && regno_last_uid[j] <= max_uid_for_loop
          && uid_luid[regno_last_uid[j]] <= copy_end_luid)
-       local_regno[j] = 1;
+       {
+         /* However, we must also check for loop-carried dependencies.
+            If the value the pseudo has at the end of iteration X is
+            used by iteration X+1, then we can not use a different pseudo
+            for each unrolled copy of the loop.  */
+         /* A pseudo is safe if regno_first_uid is a set, and this
+            set dominates all instructions from regno_first_uid to
+            regno_last_uid.  */
+         /* ??? This check is simplistic.  We would get better code if
+            this check was more sophisticated.  */
+         if (set_dominates_use (j, regno_first_uid[j], regno_last_uid[j],
+                                copy_start, copy_end))
+           local_regno[j] = 1;
+
+         if (loop_dump_stream)
+           {
+             if (local_regno[j])
+               fprintf (loop_dump_stream, "Marked reg %d as local\n", j);
+             else
+               fprintf (loop_dump_stream, "Did not mark reg %d as local\n",
+                        j);
+           }
+       }
   }
 
   /* If this loop requires exit tests when unrolled, check to see if we
@@ -853,18 +900,23 @@ unroll_loop (loop_end, insn_count, loop_start, end_insert_before,
          for (i = 0; i < unroll_number; i++)
            labels[i] = gen_label_rtx ();
 
-         /* Check for the case where the initial value is greater than or equal
-            to the final value.  In that case, we want to execute exactly
-            one loop iteration.  The code below will fail for this case.  */
+         /* Check for the case where the initial value is greater than or
+            equal to the final value.  In that case, we want to execute
+            exactly one loop iteration.  The code below will fail for this
+            case.  This check does not apply if the loop has a NE
+            comparison at the end.  */
 
-         emit_cmp_insn (initial_value, final_value, neg_inc ? LE : GE,
-                        NULL_RTX, mode, 0, 0);
-         if (neg_inc)
-           emit_jump_insn (gen_ble (labels[1]));
-         else
-           emit_jump_insn (gen_bge (labels[1]));
-         JUMP_LABEL (get_last_insn ()) = labels[1];
-         LABEL_NUSES (labels[1])++;
+         if (loop_comparison_code != NE)
+           {
+             emit_cmp_insn (initial_value, final_value, neg_inc ? LE : GE,
+                            NULL_RTX, mode, 0, 0);
+             if (neg_inc)
+               emit_jump_insn (gen_ble (labels[1]));
+             else
+               emit_jump_insn (gen_bge (labels[1]));
+             JUMP_LABEL (get_last_insn ()) = labels[1];
+             LABEL_NUSES (labels[1])++;
+           }
 
          /* Assuming the unroll_number is 4, and the increment is 2, then
             for a negative increment:  for a positive increment:
@@ -2228,14 +2280,16 @@ iteration_info (iteration_var, initial_value, increment, loop_start, loop_end)
                 "Loop unrolling: No reg_iv_type entry for iteration var.\n");
       return;
     }
-  /* Reject iteration variables larger than the host long size, since they
+
+  /* Reject iteration variables larger than the host wide int size, since they
      could result in a number of iterations greater than the range of our
-     `unsigned long' variable loop_n_iterations.  */
-  else if (GET_MODE_BITSIZE (GET_MODE (iteration_var)) > HOST_BITS_PER_LONG)
+     `unsigned HOST_WIDE_INT' variable loop_n_iterations.  */
+  else if ((GET_MODE_BITSIZE (GET_MODE (iteration_var))
+           > HOST_BITS_PER_WIDE_INT))
     {
       if (loop_dump_stream)
        fprintf (loop_dump_stream,
-                "Loop unrolling: Iteration var rejected because mode larger than host long.\n");
+                "Loop unrolling: Iteration var rejected because mode too large.\n");
       return;
     }
   else if (GET_MODE_CLASS (GET_MODE (iteration_var)) != MODE_INT)
@@ -3330,6 +3384,7 @@ loop_iterations (loop_start, loop_end)
   loop_initial_value = initial_value;
   loop_increment = increment;
   loop_final_value = final_value;
+  loop_comparison_code = comparison_code;
 
   if (increment == 0)
     {
@@ -3510,3 +3565,65 @@ remap_split_bivs (x)
     }
   return x;
 }
+
+/* If FIRST_UID is a set of REGNO, and FIRST_UID dominates LAST_UID (e.g.
+   FIST_UID is always executed if LAST_UID is), then return 1.  Otherwise
+   return 0.  COPY_START is where we can start looking for the insns
+   FIRST_UID and LAST_UID.  COPY_END is where we stop looking for these
+   insns.
+
+   If there is no JUMP_INSN between LOOP_START and FIRST_UID, then FIRST_UID
+   must dominate LAST_UID.
+
+   If there is a CODE_LABEL between FIRST_UID and LAST_UID, then FIRST_UID
+   may not dominate LAST_UID.
+
+   If there is no CODE_LABEL between FIRST_UID and LAST_UID, then FIRST_UID
+   must dominate LAST_UID.  */
+
+int
+set_dominates_use (regno, first_uid, last_uid, copy_start, copy_end)
+     int regno;
+     int first_uid;
+     int last_uid;
+     rtx copy_start;
+     rtx copy_end;
+{
+  int passed_jump = 0;
+  rtx p = NEXT_INSN (copy_start);
+
+  while (INSN_UID (p) != first_uid)
+    {
+      if (GET_CODE (p) == JUMP_INSN)
+       passed_jump= 1;
+      /* Could not find FIRST_UID.  */
+      if (p == copy_end)
+       return 0;
+      p = NEXT_INSN (p);
+    }
+
+  /* Verify that FIRST_UID is an insn that entirely sets REGNO.  */
+  if (GET_RTX_CLASS (GET_CODE (p)) != 'i'
+      || ! dead_or_set_regno_p (p, regno))
+    return 0;
+
+  /* FIRST_UID is always executed.  */
+  if (passed_jump == 0)
+    return 1;
+
+  while (INSN_UID (p) != last_uid)
+    {
+      /* If we see a CODE_LABEL between FIRST_UID and LAST_UID, then we
+        can not be sure that FIRST_UID dominates LAST_UID.  */
+      if (GET_CODE (p) == CODE_LABEL)
+       return 0;
+      /* Could not find LAST_UID, but we reached the end of the loop, so
+        it must be safe.  */
+      else if (p == copy_end)
+       return 1;
+      p = NEXT_INSN (p);
+    }
+
+  /* FIRST_UID is always executed if LAST_UID is executed.  */
+  return 1;
+}