loop.c (basic_induction_var): A non-integer variable which is being set by a paradoxi...
[platform/upstream/gcc.git] / gcc / loop.c
index dcff685..2b2a837 100644 (file)
@@ -37,6 +37,7 @@ Boston, MA 02111-1307, USA.  */
 #include "config.h"
 #include "system.h"
 #include "rtl.h"
+#include "tm_p.h"
 #include "obstack.h"
 #include "function.h"
 #include "expr.h"
@@ -268,6 +269,9 @@ static struct movable *the_movables;
 
 FILE *loop_dump_stream;
 
+/* For communicating return values from note_set_pseudo_multiple_uses.  */
+static int note_set_pseudo_multiple_uses_retval;
+
 /* Forward declarations.  */
 
 static void verify_dominator PROTO((int));
@@ -282,6 +286,7 @@ static void count_one_set PROTO((rtx, rtx, varray_type, rtx *));
 static void count_loop_regs_set PROTO((rtx, rtx, varray_type, varray_type,
                                       int *, int)); 
 static void note_addr_stored PROTO((rtx, rtx));
+static void note_set_pseudo_multiple_uses PROTO((rtx, rtx));
 static int loop_reg_used_before_p PROTO((rtx, rtx, rtx, rtx, rtx));
 static void scan_loop PROTO((rtx, rtx, rtx, int, int));
 #if 0
@@ -2779,6 +2784,7 @@ find_and_verify_loops (f)
          {
            rtx p;
            rtx our_next = next_real_insn (insn);
+           rtx last_insn_to_move = NEXT_INSN (insn);
            int dest_loop;
            int outer_loop = -1;
 
@@ -2830,21 +2836,39 @@ find_and_verify_loops (f)
                && INSN_UID (JUMP_LABEL (p)) != 0
                && condjump_p (p)
                && ! simplejump_p (p)
-               && next_real_insn (JUMP_LABEL (p)) == our_next)
+               && next_real_insn (JUMP_LABEL (p)) == our_next
+               /* If it's not safe to move the sequence, then we
+                  mustn't try.  */
+               && insns_safe_to_move_p (p, NEXT_INSN (insn), 
+                                        &last_insn_to_move))
              {
                rtx target
                  = JUMP_LABEL (insn) ? JUMP_LABEL (insn) : get_last_insn ();
                int target_loop_num = uid_loop_num[INSN_UID (target)];
-               rtx loc;
+               rtx loc, loc2;
 
                for (loc = target; loc; loc = PREV_INSN (loc))
                  if (GET_CODE (loc) == BARRIER
+                     /* Don't move things inside a tablejump.  */
+                     && ((loc2 = next_nonnote_insn (loc)) == 0
+                         || GET_CODE (loc2) != CODE_LABEL
+                         || (loc2 = next_nonnote_insn (loc2)) == 0
+                         || GET_CODE (loc2) != JUMP_INSN
+                         || (GET_CODE (PATTERN (loc2)) != ADDR_VEC
+                             && GET_CODE (PATTERN (loc2)) != ADDR_DIFF_VEC))
                      && uid_loop_num[INSN_UID (loc)] == target_loop_num)
                    break;
 
                if (loc == 0)
                  for (loc = target; loc; loc = NEXT_INSN (loc))
                    if (GET_CODE (loc) == BARRIER
+                       /* Don't move things inside a tablejump.  */
+                       && ((loc2 = next_nonnote_insn (loc)) == 0
+                           || GET_CODE (loc2) != CODE_LABEL
+                           || (loc2 = next_nonnote_insn (loc2)) == 0
+                           || GET_CODE (loc2) != JUMP_INSN
+                           || (GET_CODE (PATTERN (loc2)) != ADDR_VEC
+                               && GET_CODE (PATTERN (loc2)) != ADDR_DIFF_VEC))
                        && uid_loop_num[INSN_UID (loc)] == target_loop_num)
                      break;
 
@@ -2885,11 +2909,13 @@ find_and_verify_loops (f)
 
                       /* Include the BARRIER after INSN and copy the
                          block after LOC.  */
-                      new_label = squeeze_notes (new_label, NEXT_INSN (insn));
-                      reorder_insns (new_label, NEXT_INSN (insn), loc);
+                      new_label = squeeze_notes (new_label, 
+                                                 last_insn_to_move);
+                      reorder_insns (new_label, last_insn_to_move, loc);
 
                       /* All those insns are now in TARGET_LOOP_NUM.  */
-                      for (q = new_label; q != NEXT_INSN (NEXT_INSN (insn));
+                      for (q = new_label; 
+                           q != NEXT_INSN (last_insn_to_move);
                            q = NEXT_INSN (q))
                         uid_loop_num[INSN_UID (q)] = target_loop_num;
 
@@ -3150,6 +3176,36 @@ note_addr_stored (x, y)
 
   loop_store_mems = gen_rtx_EXPR_LIST (VOIDmode, x, loop_store_mems);
 }
+
+/* X is a value modified by an INSN that references a biv inside a loop
+   exit test (ie, X is somehow related to the value of the biv).  If X
+   is a pseudo that is used more than once, then the biv is (effectively)
+   used more than once.  */
+
+static void
+note_set_pseudo_multiple_uses (x, y)
+     rtx x;
+     rtx y ATTRIBUTE_UNUSED;
+{
+  if (x == 0)
+    return;
+
+  while (GET_CODE (x) == STRICT_LOW_PART
+        || GET_CODE (x) == SIGN_EXTRACT
+        || GET_CODE (x) == ZERO_EXTRACT
+        || GET_CODE (x) == SUBREG)
+    x = XEXP (x, 0);
+
+  if (GET_CODE (x) != REG || REGNO (x) < FIRST_PSEUDO_REGISTER)
+    return;
+
+  /* If we do not have usage information, or if we know the register
+     is used more than once, note that fact for check_dbra_loop.  */
+  if (REGNO (x) >= max_reg_before_loop
+      || ! VARRAY_RTX (reg_single_usage, REGNO (x))
+      || VARRAY_RTX (reg_single_usage, REGNO (x)) == const0_rtx)
+    note_set_pseudo_multiple_uses_retval = 1;
+}
 \f
 /* Return nonzero if the rtx X is invariant over the current loop.
 
@@ -3706,7 +3762,7 @@ strength_reduce (scan_start, end, loop_top, insn_count,
   rtx end_insert_before;
   int loop_depth = 0;
   int n_extra_increment;
-  int unrolled_insn_copies;
+  int unrolled_insn_copies = 0;
 
   /* If scan_start points to the loop exit test, we have to be wary of
      subversive use of gotos inside expression statements.  */
@@ -6062,6 +6118,8 @@ basic_induction_var (x, mode, dest_reg, p, inc_val, mult_val, location)
               || (GET_CODE (SET_DEST (set)) == SUBREG
                   && (GET_MODE_SIZE (GET_MODE (SET_DEST (set)))
                       <= UNITS_PER_WORD)
+                  && (GET_MODE_CLASS (GET_MODE (SET_DEST (set)))
+                      == MODE_INT)
                   && SUBREG_REG (SET_DEST (set)) == x))
              && basic_induction_var (SET_SRC (set),
                                      (GET_MODE (SET_SRC (set)) == VOIDmode
@@ -6260,8 +6318,10 @@ general_induction_var (x, src_reg, add_val, mult_val, is_addr, pbenefit)
 
    *BENEFIT will be incremented by the benefit of any sub-giv encountered.  */
 
-static rtx sge_plus PROTO ((enum machine_mode, rtx, rtx));
-static rtx sge_plus_constant PROTO ((rtx, rtx));
+static rtx sge_plus PARAMS ((enum machine_mode, rtx, rtx));
+static rtx sge_plus_constant PARAMS ((rtx, rtx));
+static int cmp_combine_givs_stats PARAMS ((const PTR, const PTR));
+static int cmp_recombine_givs_stats PARAMS ((const PTR, const PTR));
 
 static rtx
 simplify_giv_expr (x, benefit)
@@ -7007,9 +7067,14 @@ struct combine_givs_stats
 };
 
 static int
-cmp_combine_givs_stats (x, y)
-     struct combine_givs_stats *x, *y;
+cmp_combine_givs_stats (xp, yp)
+     const PTR xp;
+     const PTR yp;
 {
+  const struct combine_givs_stats * const x =
+    (const struct combine_givs_stats *) xp;
+  const struct combine_givs_stats * const y =
+    (const struct combine_givs_stats *) yp;
   int d;
   d = y->total_benefit - x->total_benefit;
   /* Stabilize the sort.  */
@@ -7194,9 +7259,14 @@ struct recombine_givs_stats
    when scanning the array starting at the end, thus the arguments are
    used in reverse.  */
 static int
-cmp_recombine_givs_stats (x, y)
-     struct recombine_givs_stats *x, *y;
+cmp_recombine_givs_stats (xp, yp)
+     const PTR xp;
+     const PTR yp;
 {
+  const struct recombine_givs_stats * const x =
+    (const struct recombine_givs_stats *) xp;
+  const struct recombine_givs_stats * const y =
+    (const struct recombine_givs_stats *) yp;
   int d;
   d = y->start_luid - x->start_luid;
   /* Stabilize the sort.  */
@@ -7898,10 +7968,22 @@ check_dbra_loop (loop_end, insn_count, loop_start, loop_info)
                    && REGNO (SET_DEST (set)) == bl->regno)
                  /* An insn that sets the biv is okay.  */
                  ;
-               else if (p == prev_nonnote_insn (prev_nonnote_insn (loop_end))
-                        || p == prev_nonnote_insn (loop_end))
-                 /* Don't bother about the end test.  */
-                 ;
+               else if ((p == prev_nonnote_insn (prev_nonnote_insn (loop_end))
+                         || p == prev_nonnote_insn (loop_end))
+                        && reg_mentioned_p (bivreg, PATTERN (p)))
+                 {
+                   /* If either of these insns uses the biv and sets a pseudo
+                      that has more than one usage, then the biv has uses
+                      other than counting since it's used to derive a value
+                      that is used more than one time.  */
+                   note_set_pseudo_multiple_uses_retval = 0;
+                   note_stores (PATTERN (p), note_set_pseudo_multiple_uses);
+                   if (note_set_pseudo_multiple_uses_retval)
+                     {
+                       no_use_except_counting = 0;
+                       break;
+                     }
+                 }
                else if (reg_mentioned_p (bivreg, PATTERN (p)))
                  {
                    no_use_except_counting = 0;
@@ -7986,7 +8068,7 @@ check_dbra_loop (loop_end, insn_count, loop_start, loop_info)
                  || (GET_CODE (comparison) == LE
                      && no_use_except_counting)))
            {
-             HOST_WIDE_INT add_val, add_adjust, comparison_val;
+             HOST_WIDE_INT add_val, add_adjust, comparison_val = 0;
              rtx initial_value, comparison_value;
              int nonneg = 0;
              enum rtx_code cmp_code;