OSDN Git Service

2000-07-21 Alexandre Petit-Bianco <apbianco@cygnus.com>
[pf3gnuchains/gcc-fork.git] / gcc / unroll.c
index 23932fb..3197053 100644 (file)
@@ -1,5 +1,6 @@
 /* Try to unroll loops, and split induction variables.
-   Copyright (C) 1992, 93, 94, 95, 97, 98, 1999 Free Software Foundation, Inc.
+   Copyright (C) 1992, 1993, 1994, 1995, 1997, 1998,
+   1999, 2000 Free Software Foundation, Inc.
    Contributed by James E. Wilson, Cygnus Support/UC Berkeley.
 
 This file is part of GNU CC.
@@ -149,6 +150,7 @@ enum unroll_types { UNROLL_COMPLETELY, UNROLL_MODULO, UNROLL_NAIVE };
 #include "config.h"
 #include "system.h"
 #include "rtl.h"
+#include "tm_p.h"
 #include "insn-config.h"
 #include "integrate.h"
 #include "regs.h"
@@ -158,6 +160,8 @@ enum unroll_types { UNROLL_COMPLETELY, UNROLL_MODULO, UNROLL_NAIVE };
 #include "expr.h"
 #include "loop.h"
 #include "toplev.h"
+#include "hard-reg-set.h"
+#include "basic-block.h"
 
 /* This controls which loops are unrolled, and by how much we unroll
    them.  */
@@ -193,44 +197,47 @@ static int *splittable_regs_updates;
 
 /* Forward declarations.  */
 
-static void init_reg_map PROTO((struct inline_remap *, int));
-static rtx calculate_giv_inc PROTO((rtx, rtx, int));
-static rtx initial_reg_note_copy PROTO((rtx, struct inline_remap *));
-static void final_reg_note_copy PROTO((rtx, struct inline_remap *));
-static void copy_loop_body PROTO((rtx, rtx, struct inline_remap *, rtx, int,
+static void init_reg_map PARAMS ((struct inline_remap *, int));
+static rtx calculate_giv_inc PARAMS ((rtx, rtx, unsigned int));
+static rtx initial_reg_note_copy PARAMS ((rtx, struct inline_remap *));
+static void final_reg_note_copy PARAMS ((rtx, struct inline_remap *));
+static void copy_loop_body PARAMS ((rtx, rtx, struct inline_remap *, rtx, int,
                                  enum unroll_types, rtx, rtx, rtx, rtx));
-static void iteration_info PROTO((rtx, rtx *, rtx *, rtx, rtx));
-static int find_splittable_regs PROTO((enum unroll_types, rtx, rtx, rtx, int,
-                                      unsigned HOST_WIDE_INT));
-static int find_splittable_givs PROTO((struct iv_class *, enum unroll_types,
-                                      rtx, rtx, rtx, int));
-static int reg_dead_after_loop PROTO((rtx, rtx, rtx));
-static rtx fold_rtx_mult_add PROTO((rtx, rtx, rtx, enum machine_mode));
-static int verify_addresses PROTO((struct induction *, rtx, int));
-static rtx remap_split_bivs PROTO((rtx));
+static void iteration_info PARAMS ((const struct loop *, rtx, rtx *, rtx *));
+static int find_splittable_regs PARAMS ((const struct loop *,
+                                        enum unroll_types, rtx, int));
+static int find_splittable_givs PARAMS ((const struct loop *, 
+                                        struct iv_class *, enum unroll_types,
+                                        rtx, int));
+static int reg_dead_after_loop PARAMS ((const struct loop *, rtx));
+static rtx fold_rtx_mult_add PARAMS ((rtx, rtx, rtx, enum machine_mode));
+static int verify_addresses PARAMS ((struct induction *, rtx, int));
+static rtx remap_split_bivs PARAMS ((rtx));
+static rtx find_common_reg_term PARAMS ((rtx, rtx));
+static rtx subtract_reg_term PARAMS ((rtx, rtx));
+static rtx loop_find_equiv_value PARAMS ((const struct loop *, rtx));
 
 /* Try to unroll one loop and split induction variables in the loop.
 
-   The loop is described by the arguments LOOP_END, INSN_COUNT, and
-   LOOP_START.  END_INSERT_BEFORE indicates where insns should be added
-   which need to be executed when the loop falls through.  STRENGTH_REDUCTION_P
-   indicates whether information generated in the strength reduction pass
-   is available.
+   The loop is described by the arguments LOOP and INSN_COUNT.
+   END_INSERT_BEFORE indicates where insns should be added which need
+   to be executed when the loop falls through.  STRENGTH_REDUCTION_P
+   indicates whether information generated in the strength reduction
+   pass is available.
 
    This function is intended to be called from within `strength_reduce'
    in loop.c.  */
 
 void
-unroll_loop (loop_end, insn_count, loop_start, end_insert_before,
-            loop_info, strength_reduce_p)
-     rtx loop_end;
+unroll_loop (loop, insn_count, end_insert_before, strength_reduce_p)
+     struct loop *loop;
      int insn_count;
-     rtx loop_start;
      rtx end_insert_before;
-     struct loop_info *loop_info;
      int strength_reduce_p;
 {
-  int i, j, temp;
+  int i, j;
+  unsigned int r;
+  unsigned HOST_WIDE_INT temp;
   int unroll_number = 1;
   rtx copy_start, copy_end;
   rtx insn, sequence, pattern, tem;
@@ -239,19 +246,22 @@ unroll_loop (loop_end, insn_count, loop_start, end_insert_before,
   struct inline_remap *map;
   char *local_label = NULL;
   char *local_regno;
-  int max_local_regnum;
-  int maxregnum;
+  unsigned int max_local_regnum;
+  unsigned int maxregnum;
   rtx exit_label = 0;
   rtx start_label;
   struct iv_class *bl;
   int splitting_not_safe = 0;
-  enum unroll_types unroll_type;
+  enum unroll_types unroll_type = UNROLL_NAIVE;
   int loop_preconditioned = 0;
   rtx safety_label;
   /* This points to the last real insn in the loop, which should be either
      a JUMP_INSN (for conditional jumps) or a BARRIER (for unconditional
      jumps).  */
   rtx last_loop_insn;
+  rtx loop_start = loop->start;
+  rtx loop_end = loop->end;
+  struct loop_info *loop_info = LOOP_INFO (loop);
 
   /* Don't bother unrolling huge loops.  Since the minimum factor is
      two, loops greater than one half of MAX_UNROLLED_INSNS will never
@@ -285,6 +295,19 @@ unroll_loop (loop_end, insn_count, loop_start, end_insert_before,
                block_begins++;
              else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_END)
                block_ends++;
+             if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_BEG
+                 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_END)
+               {
+                 /* Note, would be nice to add code to unroll EH
+                    regions, but until that time, we punt (don't
+                    unroll).  For the proper way of doing it, see
+                    expand_inline_function.  */
+
+                 if (loop_dump_stream)
+                   fprintf (loop_dump_stream,
+                            "Unrolling failure: cannot unroll EH regions.\n");
+                 return;
+               }
            }
        }
 
@@ -339,7 +362,9 @@ unroll_loop (loop_end, insn_count, loop_start, end_insert_before,
        }
       else if (GET_CODE (last_loop_insn) == JUMP_INSN)
        {
+#ifdef HAVE_cc0
          rtx prev = PREV_INSN (last_loop_insn);
+#endif
          delete_insn (last_loop_insn);
 #ifdef HAVE_cc0
          /* The immediately preceding insn may be a compare which must be
@@ -348,9 +373,22 @@ unroll_loop (loop_end, insn_count, loop_start, end_insert_before,
            delete_insn (prev);
 #endif
        }
+
+      /* Remove the loop notes since this is no longer a loop.  */
+      if (loop->vtop)
+       delete_insn (loop->vtop);
+      if (loop->cont)
+       delete_insn (loop->cont);
+      if (loop_start)
+       delete_insn (loop_start);
+      if (loop_end)
+       delete_insn (loop_end);
+
       return;
     }
   else if (loop_info->n_iterations > 0
+          /* Avoid overflow in the next expression.  */
+          && loop_info->n_iterations < MAX_UNROLLED_INSNS
           && loop_info->n_iterations * insn_count < MAX_UNROLLED_INSNS)
     {
       unroll_number = loop_info->n_iterations;
@@ -405,7 +443,7 @@ unroll_loop (loop_end, insn_count, loop_start, end_insert_before,
 
   /* Default case, calculate number of times to unroll loop based on its
      size.  */
-  if (unroll_number == 1)
+  if (unroll_type == UNROLL_NAIVE)
     {
       if (8 * insn_count < MAX_UNROLLED_INSNS)
        unroll_number = 8;
@@ -413,8 +451,6 @@ unroll_loop (loop_end, insn_count, loop_start, end_insert_before,
        unroll_number = 4;
       else
        unroll_number = 2;
-
-      unroll_type = UNROLL_NAIVE;
     }
 
   /* Now we know how many times to unroll the loop.  */
@@ -643,6 +679,7 @@ unroll_loop (loop_end, insn_count, loop_start, end_insert_before,
 
   if (unroll_type == UNROLL_NAIVE
       && GET_CODE (last_loop_insn) == BARRIER
+      && GET_CODE (PREV_INSN (last_loop_insn)) == JUMP_INSN
       && start_label != JUMP_LABEL (PREV_INSN (last_loop_insn)))
     {
       /* In this case, we must copy the jump and barrier, because they will
@@ -675,22 +712,20 @@ unroll_loop (loop_end, insn_count, loop_start, end_insert_before,
   max_labelno = max_label_num ();
   max_insnno = get_max_uid ();
 
-  map = (struct inline_remap *) alloca (sizeof (struct inline_remap));
+  /* Various paths through the unroll code may reach the "egress" label
+     without initializing fields within the map structure.
 
-  map->integrating = 0;
-  map->const_equiv_varray = 0;
+     To be safe, we use xcalloc to zero the memory.  */
+  map = (struct inline_remap *) xcalloc (1, sizeof (struct inline_remap));
 
   /* Allocate the label map.  */
 
   if (max_labelno > 0)
     {
-      map->label_map = (rtx *) alloca (max_labelno * sizeof (rtx));
+      map->label_map = (rtx *) xmalloc (max_labelno * sizeof (rtx));
 
-      local_label = (char *) alloca (max_labelno);
-      bzero (local_label, max_labelno);
+      local_label = (char *) xcalloc (max_labelno, sizeof (char));
     }
-  else
-    map->label_map = 0;
 
   /* Search the loop and mark all local labels, i.e. the ones which have to
      be distinct labels when copied.  For all labels which might be
@@ -735,7 +770,7 @@ unroll_loop (loop_end, insn_count, loop_start, end_insert_before,
 
   /* Allocate space for the insn map.  */
 
-  map->insn_map = (rtx *) alloca (max_insnno * sizeof (rtx));
+  map->insn_map = (rtx *) xmalloc (max_insnno * sizeof (rtx));
 
   /* Set this to zero, to indicate that we are doing loop unrolling,
      not function inlining.  */
@@ -761,17 +796,12 @@ unroll_loop (loop_end, insn_count, loop_start, end_insert_before,
      preconditioning code and find_splittable_regs will never be used
      to access the splittable_regs[] and addr_combined_regs[] arrays.  */
 
-  splittable_regs = (rtx *) alloca (maxregnum * sizeof (rtx));
-  bzero ((char *) splittable_regs, maxregnum * sizeof (rtx));
-  derived_regs = alloca (maxregnum);
-  bzero (derived_regs, maxregnum);
-  splittable_regs_updates = (int *) alloca (maxregnum * sizeof (int));
-  bzero ((char *) splittable_regs_updates, maxregnum * sizeof (int));
+  splittable_regs = (rtx *) xcalloc (maxregnum, sizeof (rtx));
+  derived_regs = (char *) xcalloc (maxregnum, sizeof (char));
+  splittable_regs_updates = (int *) xcalloc (maxregnum, sizeof (int));
   addr_combined_regs
-    = (struct induction **) alloca (maxregnum * sizeof (struct induction *));
-  bzero ((char *) addr_combined_regs, maxregnum * sizeof (struct induction *));
-  local_regno = (char *) alloca (maxregnum);
-  bzero (local_regno, maxregnum);
+    = (struct induction **) xcalloc (maxregnum, sizeof (struct induction *));
+  local_regno = (char *) xcalloc (maxregnum, sizeof (char));
 
   /* Mark all local registers, i.e. the ones which are referenced only
      inside the loop.  */
@@ -804,11 +834,11 @@ unroll_loop (loop_end, insn_count, loop_start, end_insert_before,
         results in better code.  */
       /* We must limit the generic test to max_reg_before_loop, because only
         these pseudo registers have valid regno_first_uid info.  */
-      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)
+      for (r = FIRST_PSEUDO_REGISTER; r < max_reg_before_loop; ++r)
+       if (REGNO_FIRST_UID (r) > 0 && REGNO_FIRST_UID (r) <= max_uid_for_loop
+           && uid_luid[REGNO_FIRST_UID (r)] >= copy_start_luid
+           && REGNO_LAST_UID (r) > 0 && REGNO_LAST_UID (r) <= max_uid_for_loop
+           && uid_luid[REGNO_LAST_UID (r)] <= copy_end_luid)
          {
            /* However, we must also check for loop-carried dependencies.
               If the value the pseudo has at the end of iteration X is
@@ -819,26 +849,26 @@ unroll_loop (loop_end, insn_count, loop_start, end_insert_before,
               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),
+           if (set_dominates_use (r, REGNO_FIRST_UID (r), REGNO_LAST_UID (r),
                                   copy_start, copy_end))
-             local_regno[j] = 1;
+             local_regno[r] = 1;
 
            if (loop_dump_stream)
              {
-               if (local_regno[j])
-                 fprintf (loop_dump_stream, "Marked reg %d as local\n", j);
+               if (local_regno[r])
+                 fprintf (loop_dump_stream, "Marked reg %d as local\n", r);
                else
                  fprintf (loop_dump_stream, "Did not mark reg %d as local\n",
-                          j);
+                          r);
              }
          }
       /* Givs that have been created from multiple biv increments always have
         local registers.  */
-      for (j = first_increment_giv; j <= last_increment_giv; j++)
+      for (r = first_increment_giv; r <= last_increment_giv; r++)
        {
-         local_regno[j] = 1;
+         local_regno[r] = 1;
          if (loop_dump_stream)
-           fprintf (loop_dump_stream, "Marked reg %d as local\n", j);
+           fprintf (loop_dump_stream, "Marked reg %d as local\n", r);
        }
     }
 
@@ -869,7 +899,7 @@ unroll_loop (loop_end, insn_count, loop_start, end_insert_before,
       rtx initial_value, final_value, increment;
       enum machine_mode mode;
 
-      if (precondition_loop_p (loop_start, loop_info,
+      if (precondition_loop_p (loop,
                               &initial_value, &final_value, &increment,
                               &mode))
        {
@@ -877,10 +907,10 @@ unroll_loop (loop_end, insn_count, loop_start, end_insert_before,
          rtx *labels;
          int abs_inc, neg_inc;
 
-         map->reg_map = (rtx *) alloca (maxregnum * sizeof (rtx));
+         map->reg_map = (rtx *) xmalloc (maxregnum * sizeof (rtx));
 
          VARRAY_CONST_EQUIV_INIT (map->const_equiv_varray, maxregnum,
-                                  "unroll_loop");
+                                  "unroll_loop_precondition");
          global_const_equiv_varray = map->const_equiv_varray;
 
          init_reg_map (map, maxregnum);
@@ -923,7 +953,7 @@ unroll_loop (loop_end, insn_count, loop_start, end_insert_before,
          /* Now emit a sequence of branches to jump to the proper precond
             loop entry point.  */
 
-         labels = (rtx *) alloca (sizeof (rtx) * unroll_number);
+         labels = (rtx *) xmalloc (sizeof (rtx) * unroll_number);
          for (i = 0; i < unroll_number; i++)
            labels[i] = gen_label_rtx ();
 
@@ -1055,12 +1085,13 @@ unroll_loop (loop_end, insn_count, loop_start, end_insert_before,
                if (local_label[j])
                  set_label_in_map (map, j, gen_label_rtx ());
 
-             for (j = FIRST_PSEUDO_REGISTER; j < max_local_regnum; j++)
-               if (local_regno[j])
+             for (r = FIRST_PSEUDO_REGISTER; r < max_local_regnum; r++)
+               if (local_regno[r])
                  {
-                   map->reg_map[j] = gen_reg_rtx (GET_MODE (regno_reg_rtx[j]));
-                   record_base_value (REGNO (map->reg_map[j]),
-                                      regno_reg_rtx[j], 0);
+                   map->reg_map[r]
+                     = gen_reg_rtx (GET_MODE (regno_reg_rtx[r]));
+                   record_base_value (REGNO (map->reg_map[r]),
+                                      regno_reg_rtx[r], 0);
                  }
              /* The last copy needs the compare/branch insns at the end,
                 so reset copy_end here if the loop ends with a conditional
@@ -1102,6 +1133,9 @@ unroll_loop (loop_end, insn_count, loop_start, end_insert_before,
          /* Set unroll type to MODULO now.  */
          unroll_type = UNROLL_MODULO;
          loop_preconditioned = 1;
+
+         /* Clean up.  */
+         free (labels);
        }
     }
 
@@ -1130,16 +1164,15 @@ unroll_loop (loop_end, insn_count, loop_start, end_insert_before,
   if (splitting_not_safe)
     temp = 0;
   else
-    temp = find_splittable_regs (unroll_type, loop_start, loop_end,
-                                end_insert_before, unroll_number,
-                                loop_info->n_iterations);
+    temp = find_splittable_regs (loop, unroll_type,
+                                end_insert_before, unroll_number);
 
   /* find_splittable_regs may have created some new registers, so must
      reallocate the reg_map with the new larger size, and must realloc
      the constant maps also.  */
 
   maxregnum = max_reg_num ();
-  map->reg_map = (rtx *) alloca (maxregnum * sizeof (rtx));
+  map->reg_map = (rtx *) xmalloc (maxregnum * sizeof (rtx));
 
   init_reg_map (map, maxregnum);
 
@@ -1165,8 +1198,8 @@ unroll_loop (loop_end, insn_count, loop_start, end_insert_before,
     }
 
   /* Use our current register alignment and pointer flags.  */
-  map->regno_pointer_flag = current_function->emit->regno_pointer_flag;
-  map->regno_pointer_align = current_function->emit->regno_pointer_align;
+  map->regno_pointer_flag = cfun->emit->regno_pointer_flag;
+  map->regno_pointer_align = cfun->emit->regno_pointer_align;
 
   /* If the loop is being partially unrolled, and the iteration variables
      are being split, and are being renamed for the split, then must fix up
@@ -1196,12 +1229,12 @@ unroll_loop (loop_end, insn_count, loop_start, end_insert_before,
        if (local_label[j])
          set_label_in_map (map, j, gen_label_rtx ());
 
-      for (j = FIRST_PSEUDO_REGISTER; j < max_local_regnum; j++)
-       if (local_regno[j])
+      for (r = FIRST_PSEUDO_REGISTER; r < max_local_regnum; r++)
+       if (local_regno[r])
          {
-           map->reg_map[j] = gen_reg_rtx (GET_MODE (regno_reg_rtx[j]));
-           record_base_value (REGNO (map->reg_map[j]),
-                              regno_reg_rtx[j], 0);
+           map->reg_map[r] = gen_reg_rtx (GET_MODE (regno_reg_rtx[r]));
+           record_base_value (REGNO (map->reg_map[r]),
+                              regno_reg_rtx[r], 0);
          }
 
       /* If loop starts with a branch to the test, then fix it so that
@@ -1279,8 +1312,35 @@ unroll_loop (loop_end, insn_count, loop_start, end_insert_before,
     emit_label_after (exit_label, loop_end);
 
  egress:
-  if (map && map->const_equiv_varray)
+  if (unroll_type == UNROLL_COMPLETELY)
+    {
+      /* Remove the loop notes since this is no longer a loop.  */
+      if (loop->vtop)
+       delete_insn (loop->vtop);
+      if (loop->cont)
+       delete_insn (loop->cont);
+      if (loop_start)
+       delete_insn (loop_start);
+      if (loop_end)
+       delete_insn (loop_end);
+    }
+
+  if (map->const_equiv_varray)
     VARRAY_FREE (map->const_equiv_varray);
+  if (map->label_map)
+    {
+      free (map->label_map);
+      free (local_label);
+    }
+  free (map->insn_map);
+  free (splittable_regs);
+  free (derived_regs);
+  free (splittable_regs_updates);
+  free (addr_combined_regs);
+  free (local_regno);
+  if (map->reg_map)
+    free (map->reg_map);
+  free (map);
 }
 \f
 /* Return true if the loop can be safely, and profitably, preconditioned
@@ -1301,13 +1361,13 @@ unroll_loop (loop_end, insn_count, loop_start, end_insert_before,
    reflected in RTX_COST.  */
 
 int
-precondition_loop_p (loop_start, loop_info,
-                    initial_value, final_value, increment, mode)
-     rtx loop_start;
-     struct loop_info *loop_info;
+precondition_loop_p (loop, initial_value, final_value, increment, mode)
+     const struct loop *loop;
      rtx *initial_value, *final_value, *increment;
      enum machine_mode *mode;
 {
+  rtx loop_start = loop->start;
+  struct loop_info *loop_info = LOOP_INFO (loop);
 
   if (loop_info->n_iterations > 0)
     {
@@ -1368,16 +1428,16 @@ precondition_loop_p (loop_start, loop_info,
       return 0;
     }
 
-  /* Must ensure that final_value is invariant, so call invariant_p to
-     check.  Before doing so, must check regno against max_reg_before_loop
-     to make sure that the register is in the range covered by invariant_p.
-     If it isn't, then it is most likely a biv/giv which by definition are
-     not invariant.  */
+  /* Must ensure that final_value is invariant, so call
+     loop_invariant_p to check.  Before doing so, must check regno
+     against max_reg_before_loop to make sure that the register is in
+     the range covered by loop_invariant_p.  If it isn't, then it is
+     most likely a biv/giv which by definition are not invariant.  */
   if ((GET_CODE (loop_info->final_value) == REG
        && REGNO (loop_info->final_value) >= max_reg_before_loop)
       || (GET_CODE (loop_info->final_value) == PLUS
          && REGNO (XEXP (loop_info->final_value, 0)) >= max_reg_before_loop)
-      || ! invariant_p (loop_info->final_value))
+      || ! loop_invariant_p (loop, loop_info->final_value))
     {
       if (loop_dump_stream)
        fprintf (loop_dump_stream,
@@ -1434,7 +1494,7 @@ precondition_loop_p (loop_start, loop_info,
               < GET_MODE_SIZE (GET_MODE (*initial_value))))
     *mode = GET_MODE (*initial_value);
 
-  /* Success! */
+  /* Success!  */
   if (loop_dump_stream)
     fprintf (loop_dump_stream, "Preconditioning: Successful.\n");
   return 1;
@@ -1478,7 +1538,7 @@ init_reg_map (map, maxregnum)
 static rtx
 calculate_giv_inc (pattern, src_insn, regno)
      rtx pattern, src_insn;
-     int regno;
+     unsigned int regno;
 {
   rtx increment;
   rtx increment_total = 0;
@@ -1609,7 +1669,7 @@ initial_reg_note_copy (notes, map)
   PUT_MODE (copy, GET_MODE (notes));
 
   if (GET_CODE (notes) == EXPR_LIST)
-    XEXP (copy, 0) = copy_rtx_and_substitute (XEXP (notes, 0), map);
+    XEXP (copy, 0) = copy_rtx_and_substitute (XEXP (notes, 0), map, 0);
   else if (GET_CODE (notes) == INSN_LIST)
     /* Don't substitute for these yet.  */
     XEXP (copy, 0) = XEXP (notes, 0);
@@ -1650,7 +1710,7 @@ copy_loop_body (copy_start, copy_end, map, exit_label, last_iteration,
      rtx start_label, loop_end, insert_before, copy_notes_from;
 {
   rtx insn, pattern;
-  rtx set, tem, copy;
+  rtx set, tem, copy = NULL_RTX;
   int dest_reg_was_split, i;
 #ifdef HAVE_cc0
   rtx cc0_insn = 0;
@@ -1709,7 +1769,7 @@ copy_loop_body (copy_start, copy_end, map, exit_label, last_iteration,
            {
              struct iv_class *bl;
              struct induction *v, *tv;
-             int regno = REGNO (SET_DEST (set));
+             unsigned int regno = REGNO (SET_DEST (set));
 
              v = addr_combined_regs[REGNO (SET_DEST (set))];
              bl = reg_biv_class[REGNO (v->src_reg)];
@@ -1770,11 +1830,14 @@ copy_loop_body (copy_start, copy_end, map, exit_label, last_iteration,
                            value = plus_constant (tv->dest_reg,
                                                   tv->const_adjust);
 
-                           /* The constant could be too large for an add
-                              immediate, so can't directly emit an insn
-                              here.  */
-                           emit_unrolled_add (dest_reg, XEXP (value, 0),
-                                              XEXP (value, 1));
+                           if (GET_CODE (value) == PLUS)
+                             {
+                               /* The constant could be too large for an add
+                                  immediate, so can't directly emit an insn
+                                  here.  */
+                               emit_unrolled_add (dest_reg, XEXP (value, 0),
+                                                  XEXP (value, 1));
+                             }
                          }
 
                        /* Reset the giv to be just the register again, in case
@@ -1799,8 +1862,8 @@ copy_loop_body (copy_start, copy_end, map, exit_label, last_iteration,
              && GET_CODE (SET_DEST (set)) == REG
              && splittable_regs[REGNO (SET_DEST (set))])
            {
-             int regno = REGNO (SET_DEST (set));
-             int src_regno;
+             unsigned int regno = REGNO (SET_DEST (set));
+             unsigned int src_regno;
 
              dest_reg_was_split = 1;
 
@@ -1877,8 +1940,9 @@ copy_loop_body (copy_start, copy_end, map, exit_label, last_iteration,
 #endif
 
                  splittable_regs[regno]
-                   = GEN_INT (INTVAL (giv_inc)
-                              + INTVAL (splittable_regs[src_regno]));
+                   = simplify_gen_binary (PLUS, GET_MODE (giv_src_reg),
+                                          giv_inc,
+                                          splittable_regs[src_regno]);
                  giv_inc = splittable_regs[regno];
 
                  /* Now split the induction variable by changing the dest
@@ -1920,7 +1984,7 @@ copy_loop_body (copy_start, copy_end, map, exit_label, last_iteration,
            }
          else
            {
-             pattern = copy_rtx_and_substitute (pattern, map);
+             pattern = copy_rtx_and_substitute (pattern, map, 0);
              copy = emit_insn (pattern);
            }
          REG_NOTES (copy) = initial_reg_note_copy (REG_NOTES (insn), map);
@@ -1957,7 +2021,7 @@ copy_loop_body (copy_start, copy_end, map, exit_label, last_iteration,
             settings which would invalidate this.  */
          if (dest_reg_was_split)
            {
-             int regno = REGNO (SET_DEST (pattern));
+             int regno = REGNO (SET_DEST (set));
 
              if ((size_t) regno < VARRAY_SIZE (map->const_equiv_varray)
                  && (VARRAY_CONST_EQUIV (map->const_equiv_varray, regno).age
@@ -1967,29 +2031,24 @@ copy_loop_body (copy_start, copy_end, map, exit_label, last_iteration,
          break;
 
        case JUMP_INSN:
-         pattern = copy_rtx_and_substitute (PATTERN (insn), map);
+         pattern = copy_rtx_and_substitute (PATTERN (insn), map, 0);
          copy = emit_jump_insn (pattern);
          REG_NOTES (copy) = initial_reg_note_copy (REG_NOTES (insn), map);
 
          if (JUMP_LABEL (insn) == start_label && insn == copy_end
              && ! last_iteration)
            {
+             /* Update JUMP_LABEL correctly to make invert_jump working.  */
+             JUMP_LABEL (copy) = get_label_from_map (map,
+                                                     CODE_LABEL_NUMBER
+                                                     (JUMP_LABEL (insn)));
              /* This is a branch to the beginning of the loop; this is the
                 last insn being copied; and this is not the last iteration.
                 In this case, we want to change the original fall through
                 case to be a branch past the end of the loop, and the
                 original jump label case to fall_through.  */
 
-             if (invert_exp (pattern, copy))
-               {
-                 if (! redirect_exp (&pattern,
-                                     get_label_from_map (map,
-                                                         CODE_LABEL_NUMBER
-                                                         (JUMP_LABEL (insn))),
-                                     exit_label, copy))
-                   abort ();
-               }
-             else
+             if (!invert_jump (copy, exit_label, 0))
                {
                  rtx jmp;
                  rtx lab = gen_label_rtx ();
@@ -2001,12 +2060,8 @@ copy_loop_body (copy_start, copy_end, map, exit_label, last_iteration,
                  jmp = emit_barrier_after (jmp);
                  emit_label_after (lab, jmp);
                  LABEL_NUSES (lab) = 0;
-                 if (! redirect_exp (&pattern,
-                                     get_label_from_map (map,
-                                                         CODE_LABEL_NUMBER
-                                                         (JUMP_LABEL (insn))),
-                                     lab, copy))
-                   abort ();
+                 if (!redirect_jump (copy, lab, 0))
+                   abort();
                }
            }
 
@@ -2070,7 +2125,7 @@ copy_loop_body (copy_start, copy_end, map, exit_label, last_iteration,
 
          /* If this used to be a conditional jump insn but whose branch
             direction is now known, we must do something special.  */
-         if (condjump_p (insn) && !simplejump_p (insn) && map->last_pc_value)
+         if (any_condjump_p (insn) && onlyjump_p (insn) && map->last_pc_value)
            {
 #ifdef HAVE_cc0
              /* If the previous insn set cc0 for us, delete it.  */
@@ -2100,14 +2155,15 @@ copy_loop_body (copy_start, copy_end, map, exit_label, last_iteration,
          break;
 
        case CALL_INSN:
-         pattern = copy_rtx_and_substitute (PATTERN (insn), map);
+         pattern = copy_rtx_and_substitute (PATTERN (insn), map, 0);
          copy = emit_call_insn (pattern);
          REG_NOTES (copy) = initial_reg_note_copy (REG_NOTES (insn), map);
 
          /* Because the USAGE information potentially contains objects other
             than hard registers, we need to copy it.  */
          CALL_INSN_FUNCTION_USAGE (copy)
-           = copy_rtx_and_substitute (CALL_INSN_FUNCTION_USAGE (insn), map);
+           = copy_rtx_and_substitute (CALL_INSN_FUNCTION_USAGE (insn),
+                                      map, 0);
 
 #ifdef HAVE_cc0
          if (cc0_insn)
@@ -2145,6 +2201,7 @@ copy_loop_body (copy_start, copy_end, map, exit_label, last_iteration,
             this new block.  */
 
          if (NOTE_LINE_NUMBER (insn) != NOTE_INSN_DELETED
+             && NOTE_LINE_NUMBER (insn) != NOTE_INSN_DELETED_LABEL
              && NOTE_LINE_NUMBER (insn) != NOTE_INSN_BASIC_BLOCK
              && ((NOTE_LINE_NUMBER (insn) != NOTE_INSN_LOOP_VTOP
                   && NOTE_LINE_NUMBER (insn) != NOTE_INSN_LOOP_CONT)
@@ -2157,7 +2214,6 @@ copy_loop_body (copy_start, copy_end, map, exit_label, last_iteration,
 
        default:
          abort ();
-         break;
        }
 
       map->insn_map[INSN_UID (insn)] = copy;
@@ -2230,21 +2286,23 @@ emit_unrolled_add (dest_reg, src_reg, increment)
     emit_move_insn (dest_reg, result);
 }
 \f
-/* Searches the insns between INSN and LOOP_END.  Returns 1 if there
+/* Searches the insns between INSN and LOOP->END.  Returns 1 if there
    is a backward branch in that range that branches to somewhere between
-   LOOP_START and INSN.  Returns 0 otherwise.  */
+   LOOP->START and INSN.  Returns 0 otherwise.  */
 
 /* ??? This is quadratic algorithm.  Could be rewritten to be linear.
    In practice, this is not a problem, because this function is seldom called,
    and uses a negligible amount of CPU time on average.  */
 
 int
-back_branch_in_range_p (insn, loop_start, loop_end)
+back_branch_in_range_p (loop, insn)
+     const struct loop *loop;
      rtx insn;
-     rtx loop_start, loop_end;
 {
   rtx p, q, target_insn;
-  rtx orig_loop_end = loop_end;
+  rtx loop_start = loop->start;
+  rtx loop_end = loop->end;
+  rtx orig_loop_end = loop->end;
 
   /* Stop before we get to the backward branch at the end of the loop.  */
   loop_end = prev_nonnote_insn (loop_end);
@@ -2334,9 +2392,8 @@ fold_rtx_mult_add (mult1, mult2, add1, mode)
    if it can be calculated.  Otherwise, returns 0.  */
 
 rtx
-biv_total_increment (bl, loop_start, loop_end)
+biv_total_increment (bl)
      struct iv_class *bl;
-     rtx loop_start, loop_end;
 {
   struct induction *v;
   rtx result;
@@ -2369,14 +2426,11 @@ biv_total_increment (bl, loop_start, loop_end)
    be calculated.  */
 
 static void
-iteration_info (iteration_var, initial_value, increment, loop_start, loop_end)
+iteration_info (loop, iteration_var, initial_value, increment)
+     const struct loop *loop ATTRIBUTE_UNUSED;
      rtx iteration_var, *initial_value, *increment;
-     rtx loop_start, loop_end;
 {
   struct iv_class *bl;
-#if 0
-  struct induction *v;
-#endif
 
   /* Clear the result values, in case no answer can be found.  */
   *initial_value = 0;
@@ -2426,7 +2480,7 @@ iteration_info (iteration_var, initial_value, increment, loop_start, loop_end)
       bl = reg_biv_class[REGNO (iteration_var)];
       *initial_value = bl->initial_value;
 
-      *increment = biv_total_increment (bl, loop_start, loop_end);
+      *increment = biv_total_increment (bl);
     }
   else if (REG_IV_TYPE (REGNO (iteration_var)) == GENERAL_INDUCT)
     {
@@ -2440,7 +2494,7 @@ iteration_info (iteration_var, initial_value, increment, loop_start, loop_end)
 
       /* Increment value is mult_val times the increment value of the biv.  */
 
-      *increment = biv_total_increment (bl, loop_start, loop_end);
+      *increment = biv_total_increment (bl);
       if (*increment)
        {
          struct induction *biv_inc;
@@ -2506,13 +2560,11 @@ iteration_info (iteration_var, initial_value, increment, loop_start, loop_end)
    times, since multiplies by small integers (1,2,3,4) are very cheap.  */
 
 static int
-find_splittable_regs (unroll_type, loop_start, loop_end, end_insert_before,
-                    unroll_number, n_iterations)
+find_splittable_regs (loop, unroll_type, end_insert_before, unroll_number)
+     const struct loop *loop;
      enum unroll_types unroll_type;
-     rtx loop_start, loop_end;
      rtx end_insert_before;
      int unroll_number;
-     unsigned HOST_WIDE_INT n_iterations;
 {
   struct iv_class *bl;
   struct induction *v;
@@ -2520,13 +2572,15 @@ find_splittable_regs (unroll_type, loop_start, loop_end, end_insert_before,
   rtx biv_final_value;
   int biv_splittable;
   int result = 0;
+  rtx loop_start = loop->start;
+  rtx loop_end = loop->end;
 
   for (bl = loop_iv_list; bl; bl = bl->next)
     {
       /* Biv_total_increment must return a constant value,
         otherwise we can not calculate the split values.  */
 
-      increment = biv_total_increment (bl, loop_start, loop_end);
+      increment = biv_total_increment (bl);
       if (! increment || GET_CODE (increment) != CONST_INT)
        continue;
 
@@ -2542,16 +2596,14 @@ find_splittable_regs (unroll_type, loop_start, loop_end, end_insert_before,
       biv_splittable = 1;
       biv_final_value = 0;
       if (unroll_type != UNROLL_COMPLETELY
-         && (loop_number_exit_count[uid_loop_num[INSN_UID (loop_start)]]
-             || unroll_type == UNROLL_NAIVE)
+         && (loop->exit_count || unroll_type == UNROLL_NAIVE)
          && (uid_luid[REGNO_LAST_UID (bl->regno)] >= INSN_LUID (loop_end)
              || ! bl->init_insn
              || INSN_UID (bl->init_insn) >= max_uid_for_loop
              || (uid_luid[REGNO_FIRST_UID (bl->regno)]
                  < INSN_LUID (bl->init_insn))
              || reg_mentioned_p (bl->biv->dest_reg, SET_SRC (bl->init_set)))
-         && ! (biv_final_value = final_biv_value (bl, loop_start, loop_end,
-                                                  n_iterations)))
+         && ! (biv_final_value = final_biv_value (loop, bl)))
        biv_splittable = 0;
 
       /* If any of the insns setting the BIV don't do so with a simple
@@ -2583,7 +2635,7 @@ find_splittable_regs (unroll_type, loop_start, loop_end, end_insert_before,
              if (GET_CODE (bl->initial_value) == REG
                  && (REGNO (bl->initial_value) == bl->regno
                      || REGNO (bl->initial_value) < FIRST_PSEUDO_REGISTER
-                     || ! invariant_p (bl->initial_value)))
+                     || ! loop_invariant_p (loop, bl->initial_value)))
                {
                  rtx tem = gen_reg_rtx (bl->biv->mode);
 
@@ -2619,8 +2671,8 @@ find_splittable_regs (unroll_type, loop_start, loop_end, end_insert_before,
         depend on it may be splittable if the biv is live outside the
         loop, and the givs aren't.  */
 
-      result += find_splittable_givs (bl, unroll_type, loop_start, loop_end,
-                                    increment, unroll_number);
+      result += find_splittable_givs (loop, bl, unroll_type, increment, 
+                                     unroll_number);
 
       /* If final value is non-zero, then must emit an instruction which sets
         the value of the biv to the proper value.  This is done after
@@ -2632,7 +2684,7 @@ find_splittable_regs (unroll_type, loop_start, loop_end, end_insert_before,
             loop to ensure that it will always be executed no matter
             how the loop exits.  Otherwise emit the insn after the loop,
             since this is slightly more efficient.  */
-         if (! loop_number_exit_count[uid_loop_num[INSN_UID (loop_start)]])
+         if (! loop->exit_count)
            emit_insn_before (gen_move_insn (bl->biv->src_reg,
                                             biv_final_value),
                              end_insert_before);
@@ -2700,11 +2752,10 @@ verify_addresses (v, giv_inc, unroll_number)
    Return the number of instructions that set splittable registers.  */
 
 static int
-find_splittable_givs (bl, unroll_type, loop_start, loop_end, increment,
-                     unroll_number)
+find_splittable_givs (loop, bl, unroll_type, increment, unroll_number)
+     const struct loop *loop;
      struct iv_class *bl;
      enum unroll_types unroll_type;
-     rtx loop_start, loop_end;
      rtx increment;
      int unroll_number;
 {
@@ -2738,7 +2789,7 @@ find_splittable_givs (bl, unroll_type, loop_start, loop_end, increment,
         won't reach here if they aren't.  */
       if (v->giv_type != DEST_ADDR
          && (! v->always_computable
-             || back_branch_in_range_p (v->insn, loop_start, loop_end)))
+             || back_branch_in_range_p (loop, v->insn)))
        continue;
 
       /* The giv increment value must be a constant.  */
@@ -2759,8 +2810,7 @@ find_splittable_givs (bl, unroll_type, loop_start, loop_end, increment,
 
       final_value = 0;
       if (unroll_type != UNROLL_COMPLETELY
-         && (loop_number_exit_count[uid_loop_num[INSN_UID (loop_start)]]
-             || unroll_type == UNROLL_NAIVE)
+         && (loop->exit_count || unroll_type == UNROLL_NAIVE)
          && v->giv_type != DEST_ADDR
          /* The next part is true if the pseudo is used outside the loop.
             We assume that this is true for any pseudo created after loop
@@ -2775,7 +2825,7 @@ find_splittable_givs (bl, unroll_type, loop_start, loop_end, increment,
                          != INSN_UID (XEXP (tem, 0)))))
              /* Line above always fails if INSN was moved by loop opt.  */
              || (uid_luid[REGNO_LAST_UID (REGNO (v->dest_reg))]
-                 >= INSN_LUID (loop_end)))
+                 >= INSN_LUID (loop->end)))
          /* Givs made from biv increments are missed by the above test, so
             test explicitly for them.  */
          && (REGNO (v->dest_reg) < first_increment_giv
@@ -2833,7 +2883,7 @@ find_splittable_givs (bl, unroll_type, loop_start, loop_end, increment,
 
              record_base_value (REGNO (tem), bl->biv->add_val, 0);
              emit_insn_before (gen_move_insn (tem, bl->biv->src_reg),
-                               loop_start);
+                               loop->start);
              biv_initial_value = tem;
            }
          value = fold_rtx_mult_add (v->mult_val, biv_initial_value,
@@ -2875,7 +2925,7 @@ find_splittable_givs (bl, unroll_type, loop_start, loop_end, increment,
                  rtx tem = gen_reg_rtx (v->mode);
                  record_base_value (REGNO (tem), v->add_val, 0);
                  emit_iv_add_mult (bl->initial_value, v->mult_val,
-                                   v->add_val, tem, loop_start);
+                                   v->add_val, tem, loop->start);
                  value = tem;
                }
 
@@ -3024,8 +3074,8 @@ find_splittable_givs (bl, unroll_type, loop_start, loop_end, increment,
                     to calculate the value from scratch.  */
                  emit_insn_before (gen_rtx_SET (VOIDmode, tem,
                                                 copy_rtx (v->new_reg)),
-                                   loop_start);
-                 if (recog_memoized (PREV_INSN (loop_start)) < 0)
+                                   loop->start);
+                 if (recog_memoized (PREV_INSN (loop->start)) < 0)
                    {
                      rtx sequence, ret;
 
@@ -3033,7 +3083,7 @@ find_splittable_givs (bl, unroll_type, loop_start, loop_end, increment,
                         value, because the loop may have been preconditioned.
                         We must calculate it from NEW_REG.  Try using
                         force_operand instead of emit_iv_add_mult.  */
-                     delete_insn (PREV_INSN (loop_start));
+                     delete_insn (PREV_INSN (loop->start));
 
                      start_sequence ();
                      ret = force_operand (v->new_reg, tem);
@@ -3041,7 +3091,7 @@ find_splittable_givs (bl, unroll_type, loop_start, loop_end, increment,
                        emit_move_insn (tem, ret);
                      sequence = gen_sequence ();
                      end_sequence ();
-                     emit_insn_before (sequence, loop_start);
+                     emit_insn_before (sequence, loop->start);
 
                      if (loop_dump_stream)
                        fprintf (loop_dump_stream,
@@ -3180,32 +3230,31 @@ find_splittable_givs (bl, unroll_type, loop_start, loop_end, increment,
    it can search past if statements and other similar structures.  */
 
 static int
-reg_dead_after_loop (reg, loop_start, loop_end)
-     rtx reg, loop_start, loop_end;
+reg_dead_after_loop (loop, reg)
+     const struct loop *loop;
+     rtx reg;
 {
   rtx insn, label;
   enum rtx_code code;
   int jump_count = 0;
   int label_count = 0;
-  int this_loop_num = uid_loop_num[INSN_UID (loop_start)];
 
   /* In addition to checking all exits of this loop, we must also check
      all exits of inner nested loops that would exit this loop.  We don't
      have any way to identify those, so we just give up if there are any
      such inner loop exits.  */
 
-  for (label = loop_number_exit_labels[this_loop_num]; label;
-       label = LABEL_NEXTREF (label))
+  for (label = loop->exit_labels; label; label = LABEL_NEXTREF (label))
     label_count++;
 
-  if (label_count != loop_number_exit_count[this_loop_num])
+  if (label_count != loop->exit_count)
     return 0;
 
   /* HACK: Must also search the loop fall through exit, create a label_ref
-     here which points to the loop_end, and append the loop_number_exit_labels
+     here which points to the loop->end, and append the loop_number_exit_labels
      list to it.  */
-  label = gen_rtx_LABEL_REF (VOIDmode, loop_end);
-  LABEL_NEXTREF (label) = loop_number_exit_labels[this_loop_num];
+  label = gen_rtx_LABEL_REF (VOIDmode, loop->end);
+  LABEL_NEXTREF (label) = loop->exit_labels;
 
   for ( ; label; label = LABEL_NEXTREF (label))
     {
@@ -3233,7 +3282,7 @@ reg_dead_after_loop (reg, loop_start, loop_end)
            {
              if (GET_CODE (PATTERN (insn)) == RETURN)
                break;
-             else if (! simplejump_p (insn)
+             else if (!any_uncondjump_p (insn)
                       /* Prevent infinite loop following infinite loops.  */
                       || jump_count++ > 20)
                return 0;
@@ -3253,11 +3302,12 @@ reg_dead_after_loop (reg, loop_start, loop_end)
    the end of the loop.  If we can do it, return that value.  */
 
 rtx
-final_biv_value (bl, loop_start, loop_end, n_iterations)
+final_biv_value (loop, bl)
+     const struct loop *loop;
      struct iv_class *bl;
-     rtx loop_start, loop_end;
-     unsigned HOST_WIDE_INT n_iterations;
 {
+  rtx loop_end = loop->end;
+  unsigned HOST_WIDE_INT n_iterations = LOOP_INFO (loop)->n_iterations;
   rtx increment, tem;
 
   /* ??? This only works for MODE_INT biv's.  Reject all others for now.  */
@@ -3285,12 +3335,12 @@ final_biv_value (bl, loop_start, loop_end, n_iterations)
      value of the biv must be invariant.  */
 
   if (n_iterations != 0
-      && ! loop_number_exit_count[uid_loop_num[INSN_UID (loop_start)]]
-      && invariant_p (bl->initial_value))
+      && ! loop->exit_count
+      && loop_invariant_p (loop, bl->initial_value))
     {
-      increment = biv_total_increment (bl, loop_start, loop_end);
+      increment = biv_total_increment (bl);
 
-      if (increment && invariant_p (increment))
+      if (increment && loop_invariant_p (loop, increment))
        {
          /* Can calculate the loop exit value, emit insns after loop
             end to calculate this value into a temporary register in
@@ -3313,7 +3363,7 @@ final_biv_value (bl, loop_start, loop_end, n_iterations)
     }
 
   /* Check to see if the biv is dead at all loop exits.  */
-  if (reg_dead_after_loop (bl->biv->src_reg, loop_start, loop_end))
+  if (reg_dead_after_loop (loop, bl->biv->src_reg))
     {
       if (loop_dump_stream)
        fprintf (loop_dump_stream,
@@ -3330,15 +3380,16 @@ final_biv_value (bl, loop_start, loop_end, n_iterations)
    the end of the loop.  If we can do it, return that value.  */
 
 rtx
-final_giv_value (v, loop_start, loop_end, n_iterations)
+final_giv_value (loop, v)
+     const struct loop *loop;
      struct induction *v;
-     rtx loop_start, loop_end;
-     unsigned HOST_WIDE_INT n_iterations;
 {
   struct iv_class *bl;
   rtx insn;
   rtx increment, tem;
   rtx insert_before, seq;
+  rtx loop_end = loop->end;
+  unsigned HOST_WIDE_INT n_iterations = LOOP_INFO (loop)->n_iterations;
 
   bl = reg_biv_class[REGNO (v->src_reg)];
 
@@ -3365,7 +3416,7 @@ final_giv_value (v, loop_start, loop_end, n_iterations)
      to be known.  */
 
   if (n_iterations != 0
-      && ! loop_number_exit_count[uid_loop_num[INSN_UID (loop_start)]])
+      && ! loop->exit_count)
     {
       /* ?? It is tempting to use the biv's value here since these insns will
         be put after the loop, and hence the biv will have its final value
@@ -3378,10 +3429,10 @@ final_giv_value (v, loop_start, loop_end, n_iterations)
         sure that bl->initial_value is still valid then.  It will still
         be valid if it is invariant.  */
 
-      increment = biv_total_increment (bl, loop_start, loop_end);
+      increment = biv_total_increment (bl);
 
-      if (increment && invariant_p (increment)
-         && invariant_p (bl->initial_value))
+      if (increment && loop_invariant_p (loop, increment)
+         && loop_invariant_p (loop, bl->initial_value))
        {
          /* Can calculate the loop exit value of its biv as
             (n_iterations * increment) + initial_value */
@@ -3438,7 +3489,7 @@ final_giv_value (v, loop_start, loop_end, n_iterations)
     abort ();
 
   /* Check to see if the biv is dead at all loop exits.  */
-  if (reg_dead_after_loop (v->dest_reg, loop_start, loop_end))
+  if (reg_dead_after_loop (loop, v->dest_reg))
     {
       if (loop_dump_stream)
        fprintf (loop_dump_stream,
@@ -3452,15 +3503,16 @@ final_giv_value (v, loop_start, loop_end, n_iterations)
 }
 
 
-/* Look back before LOOP_START for then insn that sets REG and return
+/* Look back before LOOP->START for then insn that sets REG and return
    the equivalent constant if there is a REG_EQUAL note otherwise just
    the SET_SRC of REG.  */
 
 static rtx
-loop_find_equiv_value (loop_start, reg)
-     rtx loop_start;
+loop_find_equiv_value (loop, reg)
+     const struct loop *loop;
      rtx reg;
 {
+  rtx loop_start = loop->start;
   rtx insn, set;
   rtx ret;
 
@@ -3565,9 +3617,8 @@ find_common_reg_term (op0, op1)
    iterations if it can be calculated, otherwise returns zero.  */
 
 unsigned HOST_WIDE_INT
-loop_iterations (loop_start, loop_end, loop_info)
-     rtx loop_start, loop_end;
-     struct loop_info *loop_info;
+loop_iterations (loop)
+     struct loop *loop;
 {
   rtx comparison, comparison_value;
   rtx iteration_var, initial_value, increment, final_value;
@@ -3579,6 +3630,7 @@ loop_iterations (loop_start, loop_end, loop_info)
   int unsigned_p, compare_dir, final_larger;
   rtx last_loop_insn;
   rtx reg_term;
+  struct loop_info *loop_info = LOOP_INFO (loop);
 
   loop_info->n_iterations = 0;
   loop_info->initial_value = 0;
@@ -3594,7 +3646,7 @@ loop_iterations (loop_start, loop_end, loop_info)
      accidentally get the branch for a contained loop if the branch for this
      loop was deleted.  We can only trust branches immediately before the
      loop_end.  */
-  last_loop_insn = PREV_INSN (loop_end);
+  last_loop_insn = PREV_INSN (loop->end);
 
   /* ??? We should probably try harder to find the jump insn
      at the end of the loop.  The following code assumes that
@@ -3621,7 +3673,7 @@ loop_iterations (loop_start, loop_end, loop_info)
      branch, and the insn before tests a register value, make that the
      iteration variable.  */
 
-  comparison = get_condition_for_loop (last_loop_insn);
+  comparison = get_condition_for_loop (loop, last_loop_insn);
   if (comparison == 0)
     {
       if (loop_dump_stream)
@@ -3645,14 +3697,18 @@ loop_iterations (loop_start, loop_end, loop_info)
       return 0;
     }
 
-  /* The only new registers that care created before loop iterations are
-     givs made from biv increments, so this should never occur.  */
+  /* The only new registers that are created before loop iterations
+     are givs made from biv increments or registers created by
+     load_mems.  In the latter case, it is possible that try_copy_prop
+     will propagate a new pseudo into the old iteration register but
+     this will be marked by having the REG_USERVAR_P bit set.  */
 
-  if ((unsigned) REGNO (iteration_var) >= reg_iv_type->num_elements)
+  if ((unsigned) REGNO (iteration_var) >= reg_iv_type->num_elements
+      && ! REG_USERVAR_P (iteration_var))
     abort ();
 
-  iteration_info (iteration_var, &initial_value, &increment,
-                 loop_start, loop_end);
+  iteration_info (loop, iteration_var, &initial_value, &increment);
+
   if (initial_value == 0)
     /* iteration_info already printed a message.  */
     return 0;
@@ -3697,12 +3753,14 @@ loop_iterations (loop_start, loop_end, loop_info)
      its value from the insns before the start of the loop.  */
 
   final_value = comparison_value;
-  if (GET_CODE (comparison_value) == REG && invariant_p (comparison_value))
+  if (GET_CODE (comparison_value) == REG 
+      && loop_invariant_p (loop, comparison_value))
     {
-      final_value = loop_find_equiv_value (loop_start, comparison_value);
+      final_value = loop_find_equiv_value (loop, comparison_value);
+
       /* If we don't get an invariant final value, we are better
         off with the original register.  */
-      if (!invariant_p (final_value))
+      if (! loop_invariant_p (loop, final_value))
        final_value = comparison_value;
     }
 
@@ -3759,7 +3817,8 @@ loop_iterations (loop_start, loop_end, loop_info)
 
          /* Find what reg1 is equivalent to.  Hopefully it will
             either be reg2 or reg2 plus a constant.  */
-         temp = loop_find_equiv_value (loop_start, reg1);
+         temp = loop_find_equiv_value (loop, reg1);
+
          if (find_common_reg_term (temp, reg2))
            initial_value = temp;
          else
@@ -3767,7 +3826,8 @@ loop_iterations (loop_start, loop_end, loop_info)
              /* Find what reg2 is equivalent to.  Hopefully it will
                 either be reg1 or reg1 plus a constant.  Let's ignore
                 the latter case for now since it is not so common.  */
-             temp = loop_find_equiv_value (loop_start, reg2);
+             temp = loop_find_equiv_value (loop, reg2);
+
              if (temp == loop_info->iteration_var)
                temp = initial_value;
              if (temp == reg1)
@@ -3775,7 +3835,7 @@ loop_iterations (loop_start, loop_end, loop_info)
                  ? reg1 : gen_rtx_PLUS (GET_MODE (reg1), reg1, const2);
            }
        }
-      else if (loop_info->vtop && GET_CODE (reg2) == CONST_INT)
+      else if (loop->vtop && GET_CODE (reg2) == CONST_INT)
        {
          rtx temp;
 
@@ -3786,10 +3846,12 @@ loop_iterations (loop_start, loop_end, loop_info)
              where temp2 = init + const.  If the loop has a vtop we
              can replace initial_value with const.  */
 
-         temp = loop_find_equiv_value (loop_start, reg1);
+         temp = loop_find_equiv_value (loop, reg1);
+
          if (GET_CODE (temp) == MINUS && REG_P (XEXP (temp, 0)))
            {
-             rtx temp2 = loop_find_equiv_value (loop_start, XEXP (temp, 0));
+             rtx temp2 = loop_find_equiv_value (loop, XEXP (temp, 0));
+
              if (GET_CODE (temp2) == PLUS
                  && XEXP (temp2, 0) == XEXP (temp, 1))
                initial_value = XEXP (temp2, 1);
@@ -3806,7 +3868,7 @@ loop_iterations (loop_start, loop_end, loop_info)
 
      ??? Without a vtop we could still perform the optimization if we check
      the initial and final values carefully.  */
-  if (loop_info->vtop
+  if (loop->vtop
       && (reg_term = find_common_reg_term (initial_value, final_value)))
     {
       initial_value = subtract_reg_term (initial_value, reg_term);
@@ -3836,7 +3898,7 @@ loop_iterations (loop_start, loop_end, loop_info)
       /* ??? Other RTL, such as (neg (reg)) is possible here, but it isn't
         clear if it is worthwhile to try to handle such RTL.  */
       if (GET_CODE (increment) == REG || GET_CODE (increment) == SUBREG)
-       increment = loop_find_equiv_value (loop_start, increment);
+       increment = loop_find_equiv_value (loop, increment);
 
       if (GET_CODE (increment) != CONST_INT)
        {
@@ -4016,7 +4078,7 @@ remap_split_bivs (x)
     {
       if (fmt[i] == 'e')
        XEXP (x, i) = remap_split_bivs (XEXP (x, i));
-      if (fmt[i] == 'E')
+      else if (fmt[i] == 'E')
        {
          register int j;
          for (j = 0; j < XVECLEN (x, i); j++)