OSDN Git Service

2001-07-30 H.J. Lu (hjl@gnu.org)
[pf3gnuchains/gcc-fork.git] / gcc / unroll.c
index bc6655c..c26a297 100644 (file)
@@ -1,6 +1,6 @@
 /* Try to unroll loops, and split induction variables.
-   Copyright (C) 1992, 1993, 1994, 1995, 1997, 1998,
-   1999, 2000 Free Software Foundation, Inc.
+   Copyright (C) 1992, 1993, 1994, 1995, 1997, 1998, 1999, 2000, 2001
+   Free Software Foundation, Inc.
    Contributed by James E. Wilson, Cygnus Support/UC Berkeley.
 
 This file is part of GNU CC.
@@ -140,12 +140,17 @@ Boston, MA 02111-1307, USA.  */
 
 #define NUM_FACTORS 4
 
-struct _factor { int factor, count; } factors[NUM_FACTORS]
-  = { {2, 0}, {3, 0}, {5, 0}, {7, 0}};
+struct _factor { int factor, count; }
+factors[NUM_FACTORS] = { {2, 0}, {3, 0}, {5, 0}, {7, 0}};
 
 /* Describes the different types of loop unrolling performed.  */
 
-enum unroll_types { UNROLL_COMPLETELY, UNROLL_MODULO, UNROLL_NAIVE };
+enum unroll_types
+{
+  UNROLL_COMPLETELY,
+  UNROLL_MODULO,
+  UNROLL_NAIVE
+};
 
 #include "config.h"
 #include "system.h"
@@ -162,6 +167,7 @@ enum unroll_types { UNROLL_COMPLETELY, UNROLL_MODULO, UNROLL_NAIVE };
 #include "toplev.h"
 #include "hard-reg-set.h"
 #include "basic-block.h"
+#include "predict.h"
 
 /* This controls which loops are unrolled, and by how much we unroll
    them.  */
@@ -185,10 +191,6 @@ static struct induction **addr_combined_regs;
 static rtx *splittable_regs;
 
 /* Indexed by register number, if this is a splittable induction variable,
-   this indicates if it was made from a derived giv.  */
-static char *derived_regs;
-
-/* Indexed by register number, if this is a splittable induction variable,
    then this will hold the number of instructions in the loop that modify
    the induction variable.  Used to ensure that only the last insn modifying
    a split iv will update the original iv of the dest.  */
@@ -200,18 +202,19 @@ static int *splittable_regs_updates;
 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 final_reg_note_copy PARAMS ((rtx *, struct inline_remap *));
+static void copy_loop_body PARAMS ((struct loop *, rtx, rtx,
+                                   struct inline_remap *, rtx, int,
+                                   enum unroll_types, rtx, 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 *, 
+                                        enum unroll_types, 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 remap_split_bivs PARAMS ((struct loop *, 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));
@@ -220,21 +223,20 @@ static rtx ujump_to_loop_cont PARAMS ((rtx, rtx));
 /* Try to unroll one loop and split induction variables in the loop.
 
    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.
+   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, insn_count, end_insert_before, strength_reduce_p)
+unroll_loop (loop, insn_count, strength_reduce_p)
      struct loop *loop;
      int insn_count;
-     rtx end_insert_before;
      int strength_reduce_p;
 {
+  struct loop_info *loop_info = LOOP_INFO (loop);
+  struct loop_ivs *ivs = LOOP_IVS (loop);
   int i, j;
   unsigned int r;
   unsigned HOST_WIDE_INT temp;
@@ -261,7 +263,6 @@ unroll_loop (loop, insn_count, end_insert_before, strength_reduce_p)
   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
@@ -355,7 +356,7 @@ unroll_loop (loop, insn_count, end_insert_before, strength_reduce_p)
       rtx ujump = ujump_to_loop_cont (loop->start, loop->cont);
       if (ujump)
        delete_insn (ujump);
-       
+
       /* If number of iterations is exactly 1, then eliminate the compare and
         branch at the end of the loop since they will never be taken.
         Then return, since no other action is needed here.  */
@@ -441,14 +442,12 @@ unroll_loop (loop, insn_count, end_insert_before, strength_reduce_p)
       if (unroll_number == 1)
        {
          if (loop_dump_stream)
-           fprintf (loop_dump_stream,
-                    "Loop unrolling: No factors found.\n");
+           fprintf (loop_dump_stream, "Loop unrolling: No factors found.\n");
        }
       else
        unroll_type = UNROLL_MODULO;
     }
 
-
   /* Default case, calculate number of times to unroll loop based on its
      size.  */
   if (unroll_type == UNROLL_NAIVE)
@@ -464,9 +463,7 @@ unroll_loop (loop, insn_count, end_insert_before, strength_reduce_p)
   /* Now we know how many times to unroll the loop.  */
 
   if (loop_dump_stream)
-    fprintf (loop_dump_stream,
-            "Unrolling loop %d times.\n", unroll_number);
-
+    fprintf (loop_dump_stream, "Unrolling loop %d times.\n", unroll_number);
 
   if (unroll_type == UNROLL_COMPLETELY || unroll_type == UNROLL_MODULO)
     {
@@ -765,13 +762,11 @@ unroll_loop (loop, insn_count, end_insert_before, strength_reduce_p)
              for (i = 0; i < len; i++)
                {
                  label = XEXP (XVECEXP (pat, diff_vec_p, i), 0);
-                 set_label_in_map (map,
-                                   CODE_LABEL_NUMBER (label),
-                                   label);
+                 set_label_in_map (map, CODE_LABEL_NUMBER (label), label);
                }
            }
        }
-      else if ((note = find_reg_note (insn, REG_LABEL, NULL_RTX)))
+      if ((note = find_reg_note (insn, REG_LABEL, NULL_RTX)))
        set_label_in_map (map, CODE_LABEL_NUMBER (XEXP (note, 0)),
                          XEXP (note, 0));
     }
@@ -805,7 +800,6 @@ unroll_loop (loop, insn_count, end_insert_before, strength_reduce_p)
      to access the splittable_regs[] and addr_combined_regs[] arrays.  */
 
   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 **) xcalloc (maxregnum, sizeof (struct induction *));
@@ -826,7 +820,8 @@ unroll_loop (loop, insn_count, end_insert_before, strength_reduce_p)
       /* If we have a target that uses cc0, then we also must not duplicate
         the insn that sets cc0 before the jump insn, if one is present.  */
 #ifdef HAVE_cc0
-      if (GET_CODE (copy_end) == JUMP_INSN && sets_cc0_p (PREV_INSN (copy_end)))
+      if (GET_CODE (copy_end) == JUMP_INSN
+         && sets_cc0_p (PREV_INSN (copy_end)))
        copy_end_luid--;
 #endif
 
@@ -844,9 +839,9 @@ unroll_loop (loop, insn_count, end_insert_before, strength_reduce_p)
         these pseudo registers have valid regno_first_uid info.  */
       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_FIRST_LUID (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)
+           && REGNO_LAST_LUID (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
@@ -870,14 +865,6 @@ unroll_loop (loop, insn_count, end_insert_before, strength_reduce_p)
                           r);
              }
          }
-      /* Givs that have been created from multiple biv increments always have
-        local registers.  */
-      for (r = first_increment_giv; r <= last_increment_giv; r++)
-       {
-         local_regno[r] = 1;
-         if (loop_dump_stream)
-           fprintf (loop_dump_stream, "Marked reg %d as local\n", r);
-       }
     }
 
   /* If this loop requires exit tests when unrolled, check to see if we
@@ -911,7 +898,7 @@ unroll_loop (loop, insn_count, end_insert_before, strength_reduce_p)
                               &initial_value, &final_value, &increment,
                               &mode))
        {
-         register rtx diff ;
+         register rtx diff;
          rtx *labels;
          int abs_inc, neg_inc;
 
@@ -934,7 +921,7 @@ unroll_loop (loop, insn_count, end_insert_before, strength_reduce_p)
          abs_inc = INTVAL (increment);
          if (abs_inc < 0)
            {
-             abs_inc = - abs_inc;
+             abs_inc = -abs_inc;
              neg_inc = 1;
            }
 
@@ -976,6 +963,7 @@ unroll_loop (loop, insn_count, end_insert_before, strength_reduce_p)
              emit_cmp_and_jump_insns (initial_value, final_value,
                                       neg_inc ? LE : GE,
                                       NULL_RTX, mode, 0, 0, labels[1]);
+             predict_insn_def (get_last_insn (), PRED_LOOP_CONDITION, NOT_TAKEN);
              JUMP_LABEL (get_last_insn ()) = labels[1];
              LABEL_NUSES (labels[1])++;
            }
@@ -1021,6 +1009,8 @@ unroll_loop (loop, insn_count, end_insert_before, strength_reduce_p)
                                       labels[i]);
              JUMP_LABEL (get_last_insn ()) = labels[i];
              LABEL_NUSES (labels[i])++;
+             predict_insn (get_last_insn (), PRED_LOOP_PRECONDITIONING,
+                           REG_BR_PROB_BASE / (unroll_number - i));
            }
 
          /* If the increment is greater than one, then we need another branch,
@@ -1056,7 +1046,7 @@ unroll_loop (loop, insn_count, end_insert_before, strength_reduce_p)
 
          sequence = gen_sequence ();
          end_sequence ();
-         emit_insn_before (sequence, loop_start);
+         loop_insn_hoist (loop, sequence);
 
          /* Only the last copy of the loop body here needs the exit
             test, so set copy_end to exclude the compare/branch here,
@@ -1069,8 +1059,8 @@ unroll_loop (loop, insn_count, end_insert_before, strength_reduce_p)
            {
              copy_end = PREV_INSN (last_loop_insn);
 #ifdef HAVE_cc0
-             /* The immediately preceding insn may be a compare which we do not
-                want to copy.  */
+             /* The immediately preceding insn may be a compare which
+                we do not want to copy.  */
              if (sets_cc0_p (PREV_INSN (copy_end)))
                copy_end = PREV_INSN (copy_end);
 #endif
@@ -1083,10 +1073,10 @@ unroll_loop (loop, insn_count, end_insert_before, strength_reduce_p)
              emit_label_after (labels[unroll_number - i],
                                PREV_INSN (loop_start));
 
-             bzero ((char *) map->insn_map, max_insnno * sizeof (rtx));
-             bzero ((char *) &VARRAY_CONST_EQUIV (map->const_equiv_varray, 0),
-                    (VARRAY_SIZE (map->const_equiv_varray)
-                     * sizeof (struct const_equiv_data)));
+             memset ((char *) map->insn_map, 0, max_insnno * sizeof (rtx));
+             memset ((char *) &VARRAY_CONST_EQUIV (map->const_equiv_varray, 0),
+                     0, (VARRAY_SIZE (map->const_equiv_varray)
+                         * sizeof (struct const_equiv_data)));
              map->const_age = 0;
 
              for (j = 0; j < max_labelno; j++)
@@ -1115,7 +1105,7 @@ unroll_loop (loop, insn_count, end_insert_before, strength_reduce_p)
 
              /* None of the copies are the `last_iteration', so just
                 pass zero for that parameter.  */
-             copy_loop_body (copy_start, copy_end, map, exit_label, 0,
+             copy_loop_body (loop, copy_start, copy_end, map, exit_label, 0,
                              unroll_type, start_label, loop_end,
                              loop_start, copy_end);
            }
@@ -1130,8 +1120,9 @@ unroll_loop (loop, insn_count, end_insert_before, strength_reduce_p)
            {
              insert_before = last_loop_insn;
 #ifdef HAVE_cc0
-             /* The instruction immediately before the JUMP_INSN may be a compare
-                instruction which we do not want to copy or delete.  */
+             /* The instruction immediately before the JUMP_INSN may
+                be a compare instruction which we do not want to copy
+                or delete.  */
              if (sets_cc0_p (PREV_INSN (insert_before)))
                insert_before = PREV_INSN (insert_before);
 #endif
@@ -1152,7 +1143,8 @@ unroll_loop (loop, insn_count, end_insert_before, strength_reduce_p)
   if (unroll_type == UNROLL_NAIVE && ! flag_unroll_all_loops)
     {
       if (loop_dump_stream)
-       fprintf (loop_dump_stream, "Unrolling failure: Naive unrolling not being done.\n");
+       fprintf (loop_dump_stream,
+                "Unrolling failure: Naive unrolling not being done.\n");
       goto egress;
     }
 
@@ -1172,8 +1164,7 @@ unroll_loop (loop, insn_count, end_insert_before, strength_reduce_p)
   if (splitting_not_safe)
     temp = 0;
   else
-    temp = find_splittable_regs (loop, unroll_type,
-                                end_insert_before, unroll_number);
+    temp = find_splittable_regs (loop, unroll_type, 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
@@ -1193,7 +1184,7 @@ unroll_loop (loop, insn_count, end_insert_before, strength_reduce_p)
   /* Search the list of bivs and givs to find ones which need to be remapped
      when split, and set their reg_map entry appropriately.  */
 
-  for (bl = loop_iv_list; bl; bl = bl->next)
+  for (bl = ivs->list; bl; bl = bl->next)
     {
       if (REGNO (bl->biv->src_reg) != bl->regno)
        map->reg_map[bl->regno] = bl->biv->src_reg;
@@ -1206,8 +1197,8 @@ unroll_loop (loop, insn_count, end_insert_before, strength_reduce_p)
     }
 
   /* Use our current register alignment and pointer flags.  */
-  map->regno_pointer_flag = cfun->emit->regno_pointer_flag;
   map->regno_pointer_align = cfun->emit->regno_pointer_align;
+  map->x_regno_reg_rtx = cfun->emit->x_regno_reg_rtx;
 
   /* 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
@@ -1219,7 +1210,7 @@ unroll_loop (loop, insn_count, end_insert_before, strength_reduce_p)
     {
       insn = NEXT_INSN (copy_end);
       if (GET_CODE (insn) == INSN || GET_CODE (insn) == JUMP_INSN)
-       PATTERN (insn) = remap_split_bivs (PATTERN (insn));
+       PATTERN (insn) = remap_split_bivs (loop, PATTERN (insn));
     }
 
   /* For unroll_number times, make a copy of each instruction
@@ -1228,9 +1219,9 @@ unroll_loop (loop, insn_count, end_insert_before, strength_reduce_p)
 
   for (i = 0; i < unroll_number; i++)
     {
-      bzero ((char *) map->insn_map, max_insnno * sizeof (rtx));
-      bzero ((char *) &VARRAY_CONST_EQUIV (map->const_equiv_varray, 0),
-            VARRAY_SIZE (map->const_equiv_varray) * sizeof (struct const_equiv_data));
+      memset ((char *) map->insn_map, 0, max_insnno * sizeof (rtx));
+      memset ((char *) &VARRAY_CONST_EQUIV (map->const_equiv_varray, 0), 0,
+             VARRAY_SIZE (map->const_equiv_varray) * sizeof (struct const_equiv_data));
       map->const_age = 0;
 
       for (j = 0; j < max_labelno; j++)
@@ -1263,7 +1254,7 @@ unroll_loop (loop, insn_count, end_insert_before, strength_reduce_p)
          LABEL_NUSES (tem)++;
        }
 
-      copy_loop_body (copy_start, copy_end, map, exit_label,
+      copy_loop_body (loop, copy_start, copy_end, map, exit_label,
                      i == unroll_number - 1, unroll_type, start_label,
                      loop_end, insert_before, insert_before);
     }
@@ -1342,7 +1333,6 @@ unroll_loop (loop, insn_count, end_insert_before, strength_reduce_p)
     }
   free (map->insn_map);
   free (splittable_regs);
-  free (derived_regs);
   free (splittable_regs_updates);
   free (addr_combined_regs);
   free (local_regno);
@@ -1395,7 +1385,14 @@ precondition_loop_p (loop, initial_value, final_value, increment, mode)
       return 1;
     }
 
-  if (loop_info->initial_value == 0)
+  if (loop_info->iteration_var == 0)
+    {
+      if (loop_dump_stream)
+       fprintf (loop_dump_stream,
+                "Preconditioning: Could not find iteration variable.\n");
+      return 0;
+    }
+  else if (loop_info->initial_value == 0)
     {
       if (loop_dump_stream)
        fprintf (loop_dump_stream,
@@ -1417,7 +1414,7 @@ precondition_loop_p (loop, initial_value, final_value, increment, mode)
       return 0;
     }
   else if ((exact_log2 (INTVAL (loop_info->increment)) < 0)
-          && (exact_log2 (- INTVAL (loop_info->increment)) < 0))
+          && (exact_log2 (-INTVAL (loop_info->increment)) < 0))
     {
       if (loop_dump_stream)
        fprintf (loop_dump_stream,
@@ -1467,7 +1464,7 @@ precondition_loop_p (loop, initial_value, final_value, increment, mode)
   /* Fail if loop_info->iteration_var is not live before loop_start,
      since we need to test its value in the preconditioning code.  */
 
-  if (uid_luid[REGNO_FIRST_UID (REGNO (loop_info->iteration_var))]
+  if (REGNO_FIRST_LUID (REGNO (loop_info->iteration_var))
       > INSN_LUID (loop_start))
     {
       if (loop_dump_stream)
@@ -1508,7 +1505,6 @@ precondition_loop_p (loop, initial_value, final_value, increment, mode)
   return 1;
 }
 
-
 /* All pseudo-registers must be mapped to themselves.  Two hard registers
    must be mapped, VIRTUAL_STACK_VARS_REGNUM and VIRTUAL_INCOMING_ARGS_
    REGNUM, to avoid function-inlining specific conversions of these
@@ -1674,13 +1670,13 @@ initial_reg_note_copy (notes, map)
     return 0;
 
   copy = rtx_alloc (GET_CODE (notes));
-  PUT_MODE (copy, GET_MODE (notes));
+  PUT_REG_NOTE_KIND (copy, REG_NOTE_KIND (notes));
 
   if (GET_CODE (notes) == EXPR_LIST)
     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);
+    XEXP (copy, 0) = copy_rtx (XEXP (notes, 0));
   else
     abort ();
 
@@ -1692,24 +1688,48 @@ initial_reg_note_copy (notes, map)
 /* Fixup insn references in copied REG_NOTES.  */
 
 static void
-final_reg_note_copy (notes, map)
-     rtx notes;
+final_reg_note_copy (notesp, map)
+     rtx *notesp;
      struct inline_remap *map;
 {
-  rtx note;
+  while (*notesp)
+    {
+      rtx note = *notesp;
+      
+      if (GET_CODE (note) == INSN_LIST)
+       {
+         /* Sometimes, we have a REG_WAS_0 note that points to a
+            deleted instruction.  In that case, we can just delete the
+            note.  */
+         if (REG_NOTE_KIND (note) == REG_WAS_0)
+           {
+             *notesp = XEXP (note, 1);
+             continue;
+           }
+         else
+           {
+             rtx insn = map->insn_map[INSN_UID (XEXP (note, 0))];
+
+             /* If we failed to remap the note, something is awry.  */
+             if (!insn)
+               abort ();
 
-  for (note = notes; note; note = XEXP (note, 1))
-    if (GET_CODE (note) == INSN_LIST)
-      XEXP (note, 0) = map->insn_map[INSN_UID (XEXP (note, 0))];
+             XEXP (note, 0) = insn;
+           }
+       }
+
+      notesp = &XEXP (note, 1);
+    }
 }
 
 /* Copy each instruction in the loop, substituting from map as appropriate.
    This is very similar to a loop in expand_inline_function.  */
 
 static void
-copy_loop_body (copy_start, copy_end, map, exit_label, last_iteration,
+copy_loop_body (loop, copy_start, copy_end, map, exit_label, last_iteration,
                unroll_type, start_label, loop_end, insert_before,
                copy_notes_from)
+     struct loop *loop;
      rtx copy_start, copy_end;
      struct inline_remap *map;
      rtx exit_label;
@@ -1717,6 +1737,7 @@ copy_loop_body (copy_start, copy_end, map, exit_label, last_iteration,
      enum unroll_types unroll_type;
      rtx start_label, loop_end, insert_before, copy_notes_from;
 {
+  struct loop_ivs *ivs = LOOP_IVS (loop);
   rtx insn, pattern;
   rtx set, tem, copy = NULL_RTX;
   int dest_reg_was_split, i;
@@ -1735,8 +1756,7 @@ copy_loop_body (copy_start, copy_end, map, exit_label, last_iteration,
   if (! last_iteration)
     {
       final_label = gen_label_rtx ();
-      set_label_in_map (map, CODE_LABEL_NUMBER (start_label),
-                       final_label);
+      set_label_in_map (map, CODE_LABEL_NUMBER (start_label), final_label);
     }
   else
     set_label_in_map (map, CODE_LABEL_NUMBER (start_label), start_label);
@@ -1780,7 +1800,7 @@ copy_loop_body (copy_start, copy_end, map, exit_label, last_iteration,
              unsigned int regno = REGNO (SET_DEST (set));
 
              v = addr_combined_regs[REGNO (SET_DEST (set))];
-             bl = reg_biv_class[REGNO (v->src_reg)];
+             bl = REG_IV_CLASS (ivs, REGNO (v->src_reg));
 
              /* Although the giv_inc amount is not needed here, we must call
                 calculate_giv_inc here since it might try to delete the
@@ -1788,8 +1808,7 @@ copy_loop_body (copy_start, copy_end, map, exit_label, last_iteration,
                 we might accidentally delete insns generated immediately
                 below by emit_unrolled_add.  */
 
-             if (! derived_regs[regno])
-               giv_inc = calculate_giv_inc (set, insn, regno);
+             giv_inc = calculate_giv_inc (set, insn, regno);
 
              /* Now find all address giv's that were combined with this
                 giv 'v'.  */
@@ -1853,7 +1872,7 @@ copy_loop_body (copy_start, copy_end, map, exit_label, last_iteration,
                           We must subtract the const_adjust factor added in
                           above.  */
                        tv->dest_reg = plus_constant (dest_reg,
-                                                     - tv->const_adjust);
+                                                     -tv->const_adjust);
                        *tv->location = tv->dest_reg;
                      }
                  }
@@ -1876,23 +1895,12 @@ copy_loop_body (copy_start, copy_end, map, exit_label, last_iteration,
              dest_reg_was_split = 1;
 
              giv_dest_reg = SET_DEST (set);
-             if (derived_regs[regno])
-               {
-                 /* ??? This relies on SET_SRC (SET) to be of
-                    the form (plus (reg) (const_int)), and thus
-                    forces recombine_givs to restrict the kind
-                    of giv derivations it does before unrolling.  */
-                 giv_src_reg = XEXP (SET_SRC (set), 0);
-                 giv_inc = XEXP (SET_SRC (set), 1);
-               }
-             else
-               {
-                 giv_src_reg = giv_dest_reg;
-                 /* Compute the increment value for the giv, if it wasn't
-                    already computed above.  */
-                 if (giv_inc == 0)
-                   giv_inc = calculate_giv_inc (set, insn, regno);
-               }
+             giv_src_reg = giv_dest_reg;
+             /* Compute the increment value for the giv, if it wasn't
+                already computed above.  */
+             if (giv_inc == 0)
+               giv_inc = calculate_giv_inc (set, insn, regno);
+
              src_regno = REGNO (giv_src_reg);
 
              if (unroll_type == UNROLL_COMPLETELY)
@@ -1934,10 +1942,10 @@ copy_loop_body (copy_start, copy_end, map, exit_label, last_iteration,
                     for the biv was stored in the biv's first struct
                     induction entry by find_splittable_regs.  */
 
-                 if (regno < max_reg_before_loop
-                     && REG_IV_TYPE (regno) == BASIC_INDUCT)
+                 if (regno < ivs->n_regs
+                     && REG_IV_TYPE (ivs, regno) == BASIC_INDUCT)
                    {
-                     giv_src_reg = reg_biv_class[regno]->biv->src_reg;
+                     giv_src_reg = REG_IV_CLASS (ivs, regno)->biv->src_reg;
                      giv_dest_reg = giv_src_reg;
                    }
 
@@ -2046,10 +2054,12 @@ copy_loop_body (copy_start, copy_end, map, exit_label, last_iteration,
          if (JUMP_LABEL (insn) == start_label && insn == copy_end
              && ! last_iteration)
            {
-             /* Update JUMP_LABEL correctly to make invert_jump working.  */
+             /* Update JUMP_LABEL make invert_jump work correctly.  */
              JUMP_LABEL (copy) = get_label_from_map (map,
                                                      CODE_LABEL_NUMBER
                                                      (JUMP_LABEL (insn)));
+             LABEL_NUSES (JUMP_LABEL (copy))++;
+
              /* 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
@@ -2069,7 +2079,7 @@ copy_loop_body (copy_start, copy_end, map, exit_label, last_iteration,
                  emit_label_after (lab, jmp);
                  LABEL_NUSES (lab) = 0;
                  if (!redirect_jump (copy, lab, 0))
-                   abort();
+                   abort ();
                }
            }
 
@@ -2205,7 +2215,7 @@ copy_loop_body (copy_start, copy_end, map, exit_label, last_iteration,
          /* VTOP and CONT notes are valid only before the loop exit test.
             If placed anywhere else, loop may generate bad code.  */
          /* BASIC_BLOCK notes exist to stabilize basic block structures with
-            the associated rtl.  We do not want to share the structure in 
+            the associated rtl.  We do not want to share the structure in
             this new block.  */
 
          if (NOTE_LINE_NUMBER (insn) != NOTE_INSN_DELETED
@@ -2236,7 +2246,7 @@ copy_loop_body (copy_start, copy_end, map, exit_label, last_iteration,
       if ((GET_CODE (insn) == INSN || GET_CODE (insn) == JUMP_INSN
           || GET_CODE (insn) == CALL_INSN)
          && map->insn_map[INSN_UID (insn)])
-       final_reg_note_copy (REG_NOTES (map->insn_map[INSN_UID (insn)]), map);
+       final_reg_note_copy (&REG_NOTES (map->insn_map[INSN_UID (insn)]), map);
     }
   while (insn != copy_end);
 
@@ -2274,7 +2284,7 @@ copy_loop_body (copy_start, copy_end, map, exit_label, last_iteration,
 
   tem = gen_sequence ();
   end_sequence ();
-  emit_insn_before (tem, insert_before);
+  loop_insn_emit_before (loop, 0, insert_before, tem);
 }
 \f
 /* Emit an insn, using the expand_binop to ensure that a valid insn is
@@ -2401,7 +2411,7 @@ fold_rtx_mult_add (mult1, mult2, add1, mode)
 
 rtx
 biv_total_increment (bl)
-     struct iv_class *bl;
+     const struct iv_class *bl;
 {
   struct induction *v;
   rtx result;
@@ -2426,7 +2436,6 @@ biv_total_increment (bl)
   return result;
 }
 
-
 /* For each biv and giv, determine whether it can be safely split into
    a different variable for each unrolled copy of the loop body.  If it
    is safe to split, then indicate that by saving some useful info
@@ -2452,22 +2461,20 @@ biv_total_increment (bl)
    times, since multiplies by small integers (1,2,3,4) are very cheap.  */
 
 static int
-find_splittable_regs (loop, unroll_type, end_insert_before, unroll_number)
+find_splittable_regs (loop, unroll_type, unroll_number)
      const struct loop *loop;
      enum unroll_types unroll_type;
-     rtx end_insert_before;
      int unroll_number;
 {
+  struct loop_ivs *ivs = LOOP_IVS (loop);
   struct iv_class *bl;
   struct induction *v;
   rtx increment, tem;
   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)
+  for (bl = ivs->list; bl; bl = bl->next)
     {
       /* Biv_total_increment must return a constant value,
         otherwise we can not calculate the split values.  */
@@ -2489,10 +2496,10 @@ find_splittable_regs (loop, unroll_type, end_insert_before, unroll_number)
       biv_final_value = 0;
       if (unroll_type != UNROLL_COMPLETELY
          && (loop->exit_count || unroll_type == UNROLL_NAIVE)
-         && (uid_luid[REGNO_LAST_UID (bl->regno)] >= INSN_LUID (loop_end)
+         && (REGNO_LAST_LUID (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)]
+             || (REGNO_FIRST_LUID (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 (loop, bl)))
@@ -2532,11 +2539,12 @@ find_splittable_regs (loop, unroll_type, end_insert_before, unroll_number)
                  rtx tem = gen_reg_rtx (bl->biv->mode);
 
                  record_base_value (REGNO (tem), bl->biv->add_val, 0);
-                 emit_insn_before (gen_move_insn (tem, bl->biv->src_reg),
-                                   loop_start);
+                 loop_insn_hoist (loop, 
+                                  gen_move_insn (tem, bl->biv->src_reg));
 
                  if (loop_dump_stream)
-                   fprintf (loop_dump_stream, "Biv %d initial value remapped to %d.\n",
+                   fprintf (loop_dump_stream,
+                            "Biv %d initial value remapped to %d.\n",
                             bl->regno, REGNO (tem));
 
                  splittable_regs[bl->regno] = tem;
@@ -2563,7 +2571,7 @@ find_splittable_regs (loop, unroll_type, end_insert_before, unroll_number)
         depend on it may be splittable if the biv is live outside the
         loop, and the givs aren't.  */
 
-      result += find_splittable_givs (loop, bl, unroll_type, increment, 
+      result += find_splittable_givs (loop, bl, unroll_type, increment,
                                      unroll_number);
 
       /* If final value is non-zero, then must emit an instruction which sets
@@ -2577,9 +2585,8 @@ find_splittable_regs (loop, unroll_type, end_insert_before, unroll_number)
             how the loop exits.  Otherwise emit the insn after the loop,
             since this is slightly more efficient.  */
          if (! loop->exit_count)
-           emit_insn_before (gen_move_insn (bl->biv->src_reg,
-                                            biv_final_value),
-                             end_insert_before);
+           loop_insn_sink (loop, gen_move_insn (bl->biv->src_reg,
+                                                biv_final_value));
          else
            {
              /* Create a new register to hold the value of the biv, and then
@@ -2590,11 +2597,9 @@ find_splittable_regs (loop, unroll_type, end_insert_before, unroll_number)
              rtx tem = gen_reg_rtx (bl->biv->mode);
              record_base_value (REGNO (tem), bl->biv->add_val, 0);
 
-             emit_insn_before (gen_move_insn (tem, bl->biv->src_reg),
-                               loop_start);
-             emit_insn_before (gen_move_insn (bl->biv->src_reg,
-                                              biv_final_value),
-                               loop_start);
+             loop_insn_hoist (loop, gen_move_insn (tem, bl->biv->src_reg));
+             loop_insn_hoist (loop, gen_move_insn (bl->biv->src_reg,
+                                                   biv_final_value));
 
              if (loop_dump_stream)
                fprintf (loop_dump_stream, "Biv %d mapped to %d for split.\n",
@@ -2651,6 +2656,7 @@ find_splittable_givs (loop, bl, unroll_type, increment, unroll_number)
      rtx increment;
      int unroll_number;
 {
+  struct loop_ivs *ivs = LOOP_IVS (loop);
   struct induction *v, *v2;
   rtx final_value;
   rtx tem;
@@ -2716,12 +2722,8 @@ find_splittable_givs (loop, bl, unroll_type, increment, unroll_number)
                      || (REGNO_FIRST_UID (REGNO (v->dest_reg))
                          != 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))]
+             || (REGNO_LAST_LUID (REGNO (v->dest_reg))
                  >= 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
-             || REGNO (v->dest_reg) > last_increment_giv)
          && ! (final_value = v->final_value))
        continue;
 
@@ -2739,9 +2741,8 @@ find_splittable_givs (loop, bl, unroll_type, increment, unroll_number)
             to its final value before loop start to ensure that this insn
             will always be executed, no matter how we exit.  */
          tem = gen_reg_rtx (v->mode);
-         emit_insn_before (gen_move_insn (tem, v->dest_reg), loop_start);
-         emit_insn_before (gen_move_insn (v->dest_reg, final_value),
-                           loop_start);
+         loop_insn_hoist (loop, gen_move_insn (tem, v->dest_reg));
+         loop_insn_hoist (loop, gen_move_insn (v->dest_reg, final_value));
 
          if (loop_dump_stream)
            fprintf (loop_dump_stream, "Giv %d mapped to %d for split.\n",
@@ -2774,8 +2775,7 @@ find_splittable_givs (loop, bl, unroll_type, increment, unroll_number)
              rtx tem = gen_reg_rtx (bl->biv->mode);
 
              record_base_value (REGNO (tem), bl->biv->add_val, 0);
-             emit_insn_before (gen_move_insn (tem, bl->biv->src_reg),
-                               loop->start);
+             loop_insn_hoist (loop, gen_move_insn (tem, bl->biv->src_reg));
              biv_initial_value = tem;
            }
          biv_initial_value = extend_value_for_giv (v, biv_initial_value);
@@ -2817,13 +2817,12 @@ find_splittable_givs (loop, bl, unroll_type, increment, unroll_number)
                {
                  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);
+                 loop_iv_add_mult_hoist (loop, bl->initial_value, v->mult_val,
+                                         v->add_val, tem);
                  value = tem;
                }
 
              splittable_regs[REGNO (v->new_reg)] = value;
-             derived_regs[REGNO (v->new_reg)] = v->derived_from != 0;
            }
          else
            {
@@ -2883,25 +2882,6 @@ find_splittable_givs (loop, bl, unroll_type, increment, unroll_number)
                  rtx new_reg = v->new_reg;
                  record_base_value (REGNO (tem), v->add_val, 0);
 
-                 if (same && same->derived_from)
-                   {
-                     /* calculate_giv_inc doesn't work for derived givs.
-                        copy_loop_body works around the problem for the
-                        DEST_REG givs themselves, but it can't handle
-                        DEST_ADDR givs that have been combined with
-                        a derived DEST_REG giv.
-                        So Handle V as if the giv from which V->SAME has
-                        been derived has been combined with V.
-                        recombine_givs only derives givs from givs that
-                        are reduced the ordinary, so we need not worry
-                        about same->derived_from being in turn derived.  */
-
-                     same = same->derived_from;
-                     new_reg = express_from (same, v);
-                     new_reg = replace_rtx (new_reg, same->dest_reg,
-                                            same->new_reg);
-                   }
-
                  /* If the address giv has a constant in its new_reg value,
                     then this constant can be pulled out and put in value,
                     instead of being part of the initialization code.  */
@@ -2921,7 +2901,7 @@ find_splittable_givs (loop, bl, unroll_type, increment, unroll_number)
                          /* Save the negative of the eliminated const, so
                             that we can calculate the dest_reg's increment
                             value later.  */
-                         v->const_adjust = - INTVAL (XEXP (new_reg, 1));
+                         v->const_adjust = -INTVAL (XEXP (new_reg, 1));
 
                          new_reg = XEXP (new_reg, 0);
                          if (loop_dump_stream)
@@ -2965,17 +2945,15 @@ find_splittable_givs (loop, bl, unroll_type, increment, unroll_number)
                     instruction on machines with complex addressing modes.
                     If we can't recognize it, then delete it and emit insns
                     to calculate the value from scratch.  */
-                 emit_insn_before (gen_rtx_SET (VOIDmode, tem,
-                                                copy_rtx (v->new_reg)),
-                                   loop->start);
+                 loop_insn_hoist (loop, gen_rtx_SET (VOIDmode, tem,
+                                                     copy_rtx (v->new_reg)));
                  if (recog_memoized (PREV_INSN (loop->start)) < 0)
                    {
                      rtx sequence, ret;
 
                      /* We can't use bl->initial_value to compute the initial
                         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.  */
+                        We must calculate it from NEW_REG.  */
                      delete_insn (PREV_INSN (loop->start));
 
                      start_sequence ();
@@ -2984,7 +2962,7 @@ find_splittable_givs (loop, bl, unroll_type, increment, unroll_number)
                        emit_move_insn (tem, ret);
                      sequence = gen_sequence ();
                      end_sequence ();
-                     emit_insn_before (sequence, loop->start);
+                     loop_insn_hoist (loop, sequence);
 
                      if (loop_dump_stream)
                        fprintf (loop_dump_stream,
@@ -3009,17 +2987,6 @@ find_splittable_givs (loop, bl, unroll_type, increment, unroll_number)
                                 INSN_UID (v->insn));
                      continue;
                    }
-                 if (v->same && v->same->derived_from)
-                   {
-                     /* Handle V as if the giv from which V->SAME has
-                        been derived has been combined with V.  */
-
-                     v->same = v->same->derived_from;
-                     v->new_reg = express_from (v->same, v);
-                     v->new_reg = replace_rtx (v->new_reg, v->same->dest_reg,
-                                               v->same->new_reg);
-                   }
-
                }
 
              /* Store the value of dest_reg into the insn.  This sharing
@@ -3042,7 +3009,6 @@ find_splittable_givs (loop, bl, unroll_type, increment, unroll_number)
                     Make sure that it's giv is marked as splittable here.  */
 
                  splittable_regs[REGNO (v->new_reg)] = value;
-                 derived_regs[REGNO (v->new_reg)] = v->derived_from != 0;
 
                  /* Make it appear to depend upon itself, so that the
                     giv will be properly split in the main loop above.  */
@@ -3084,12 +3050,7 @@ find_splittable_givs (loop, bl, unroll_type, increment, unroll_number)
        {
          int count = 1;
          if (! v->ignore)
-           count = reg_biv_class[REGNO (v->src_reg)]->biv_count;
-
-         if (count > 1 && v->derived_from)
-            /* In this case, there is one set where the giv insn was and one
-               set each after each biv increment.  (Most are likely dead.)  */
-           count++;
+           count = REG_IV_CLASS (ivs, REGNO (v->src_reg))->biv_count;
 
          splittable_regs_updates[REGNO (v->new_reg)] = count;
        }
@@ -3149,7 +3110,7 @@ reg_dead_after_loop (loop, reg)
   label = gen_rtx_LABEL_REF (VOIDmode, loop->end);
   LABEL_NEXTREF (label) = loop->exit_labels;
 
-  for ( ; label; label = LABEL_NEXTREF (label))
+  for (; label; label = LABEL_NEXTREF (label))
     {
       /* Succeed if find an insn which sets the biv or if reach end of
         function.  Fail if find an insn that uses the biv, or if come to
@@ -3199,7 +3160,6 @@ final_biv_value (loop, bl)
      const struct loop *loop;
      struct iv_class *bl;
 {
-  rtx loop_end = loop->end;
   unsigned HOST_WIDE_INT n_iterations = LOOP_INFO (loop)->n_iterations;
   rtx increment, tem;
 
@@ -3209,7 +3169,7 @@ final_biv_value (loop, bl)
     return 0;
 
   /* The final value for reversed bivs must be calculated differently than
-      for ordinary bivs.  In this case, there is already an insn after the
+     for ordinary bivs.  In this case, there is already an insn after the
      loop which sets this biv's final value (if necessary), and there are
      no other loop exits, so we can return any value.  */
   if (bl->reversed)
@@ -3241,11 +3201,8 @@ final_biv_value (loop, bl)
 
          tem = gen_reg_rtx (bl->biv->mode);
          record_base_value (REGNO (tem), bl->biv->add_val, 0);
-         /* Make sure loop_end is not the last insn.  */
-         if (NEXT_INSN (loop_end) == 0)
-           emit_note_after (NOTE_INSN_DELETED, loop_end);
-         emit_iv_add_mult (increment, GEN_INT (n_iterations),
-                           bl->initial_value, tem, NEXT_INSN (loop_end));
+         loop_iv_add_mult_sink (loop, increment, GEN_INT (n_iterations),
+                                bl->initial_value, tem);
 
          if (loop_dump_stream)
            fprintf (loop_dump_stream,
@@ -3277,14 +3234,15 @@ final_giv_value (loop, v)
      const struct loop *loop;
      struct induction *v;
 {
+  struct loop_ivs *ivs = LOOP_IVS (loop);
   struct iv_class *bl;
   rtx insn;
   rtx increment, tem;
-  rtx insert_before, seq;
+  rtx 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)];
+  bl = REG_IV_CLASS (ivs, REGNO (v->src_reg));
 
   /* The final value for givs which depend on reversed bivs must be calculated
      differently than for ordinary givs.  In this case, there is already an
@@ -3337,14 +3295,13 @@ final_giv_value (loop, v)
             We must search from the insn that sets the giv to the end
             of the loop to calculate this value.  */
 
-         insert_before = NEXT_INSN (loop_end);
-
          /* Put the final biv value in tem.  */
          tem = gen_reg_rtx (v->mode);
          record_base_value (REGNO (tem), bl->biv->add_val, 0);
-         emit_iv_add_mult (increment, GEN_INT (n_iterations),
-                           extend_value_for_giv (v, bl->initial_value),
-                           tem, insert_before);
+         loop_iv_add_mult_sink (loop, extend_value_for_giv (v, increment),
+                                GEN_INT (n_iterations),
+                                extend_value_for_giv (v, bl->initial_value),
+                                tem);
 
          /* Subtract off extra increments as we find them.  */
          for (insn = NEXT_INSN (v->insn); insn != loop_end;
@@ -3361,13 +3318,12 @@ final_giv_value (loop, v)
                                        OPTAB_LIB_WIDEN);
                    seq = gen_sequence ();
                    end_sequence ();
-                   emit_insn_before (seq, insert_before);
+                   loop_insn_sink (loop, seq);
                  }
            }
 
          /* Now calculate the giv's final value.  */
-         emit_iv_add_mult (tem, v->mult_val, v->add_val, tem,
-                           insert_before);
+         loop_iv_add_mult_sink (loop, tem, v->mult_val, v->add_val, tem);
 
          if (loop_dump_stream)
            fprintf (loop_dump_stream,
@@ -3396,7 +3352,6 @@ final_giv_value (loop, v)
   return 0;
 }
 
-
 /* 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.  */
@@ -3411,7 +3366,7 @@ loop_find_equiv_value (loop, reg)
   rtx ret;
 
   ret = reg;
-  for (insn = PREV_INSN (loop_start); insn ; insn = PREV_INSN (insn))
+  for (insn = PREV_INSN (loop_start); insn; insn = PREV_INSN (insn))
     {
       if (GET_CODE (insn) == CODE_LABEL)
        break;
@@ -3422,7 +3377,7 @@ loop_find_equiv_value (loop, reg)
             If it sets the entire register, and has a REG_EQUAL note,
             then use the value of the REG_EQUAL note.  */
          if ((set = single_set (insn))
-                 && (SET_DEST (set) == reg))
+             && (SET_DEST (set) == reg))
            {
              rtx note = find_reg_note (insn, REG_EQUAL, NULL_RTX);
 
@@ -3473,7 +3428,6 @@ subtract_reg_term (op, reg)
   abort ();
 }
 
-
 /* Find and return register term common to both expressions OP0 and
    OP1 or NULL_RTX if no such term exists.  Each expression must be a
    REG or a PLUS of a REG.  */
@@ -3511,7 +3465,6 @@ find_common_reg_term (op0, op1)
   return NULL_RTX;
 }
 
-
 /* Determine the loop iterator and calculate the number of loop
    iterations.  Returns the exact number of loop iterations if it can
    be calculated, otherwise returns zero.  */
@@ -3520,17 +3473,19 @@ unsigned HOST_WIDE_INT
 loop_iterations (loop)
      struct loop *loop;
 {
+  struct loop_info *loop_info = LOOP_INFO (loop);
+  struct loop_ivs *ivs = LOOP_IVS (loop);
   rtx comparison, comparison_value;
   rtx iteration_var, initial_value, increment, final_value;
   enum rtx_code comparison_code;
-  HOST_WIDE_INT abs_inc;
+  HOST_WIDE_INT inc;
+  unsigned HOST_WIDE_INT abs_inc;
   unsigned HOST_WIDE_INT abs_diff;
   int off_by_one;
   int increment_dir;
   int unsigned_p, compare_dir, final_larger;
   rtx last_loop_insn;
   rtx reg_term;
-  struct loop_info *loop_info = LOOP_INFO (loop);
   struct iv_class *bl;
 
   loop_info->n_iterations = 0;
@@ -3605,7 +3560,7 @@ loop_iterations (loop)
      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) >= ivs->n_regs
       && ! REG_USERVAR_P (iteration_var))
     abort ();
 
@@ -3623,7 +3578,7 @@ loop_iterations (loop)
 
   /* If this is a new register, can't handle it since we don't have any
      reg_iv_type entry for it.  */
-  if ((unsigned) REGNO (iteration_var) >= reg_iv_type->num_elements)
+  if ((unsigned) REGNO (iteration_var) >= ivs->n_regs)
     {
       if (loop_dump_stream)
        fprintf (loop_dump_stream,
@@ -3649,30 +3604,42 @@ loop_iterations (loop)
                 "Loop iterations: Iteration var not an integer.\n");
       return 0;
     }
-  else if (REG_IV_TYPE (REGNO (iteration_var)) == BASIC_INDUCT)
+  else if (REG_IV_TYPE (ivs, REGNO (iteration_var)) == BASIC_INDUCT)
     {
-      /* When reg_iv_type / reg_iv_info is resized for biv increments
-        that are turned into givs, reg_biv_class is not resized.
-        So check here that we don't make an out-of-bounds access.  */
-      if (REGNO (iteration_var) >= max_reg_before_loop)
+      if (REGNO (iteration_var) >= ivs->n_regs)
        abort ();
 
       /* Grab initial value, only useful if it is a constant.  */
-      bl = reg_biv_class[REGNO (iteration_var)];
+      bl = REG_IV_CLASS (ivs, REGNO (iteration_var));
       initial_value = bl->initial_value;
+      if (!bl->biv->always_executed || bl->biv->maybe_multiple)
+       {
+         if (loop_dump_stream)
+           fprintf (loop_dump_stream,
+                    "Loop iterations: Basic induction var not set once in each iteration.\n");
+         return 0;
+       }
 
       increment = biv_total_increment (bl);
     }
-  else if (REG_IV_TYPE (REGNO (iteration_var)) == GENERAL_INDUCT)
+  else if (REG_IV_TYPE (ivs, REGNO (iteration_var)) == GENERAL_INDUCT)
     {
       HOST_WIDE_INT offset = 0;
-      struct induction *v = REG_IV_INFO (REGNO (iteration_var));
+      struct induction *v = REG_IV_INFO (ivs, REGNO (iteration_var));
       rtx biv_initial_value;
 
-      if (REGNO (v->src_reg) >= max_reg_before_loop)
+      if (REGNO (v->src_reg) >= ivs->n_regs)
        abort ();
 
-      bl = reg_biv_class[REGNO (v->src_reg)];
+      if (!v->always_executed || v->maybe_multiple)
+       {
+         if (loop_dump_stream)
+           fprintf (loop_dump_stream,
+                    "Loop iterations: General induction var not set once in each iteration.\n");
+         return 0;
+       }
+
+      bl = REG_IV_CLASS (ivs, REGNO (v->src_reg));
 
       /* Increment value is mult_val times the increment value of the biv.  */
 
@@ -3681,8 +3648,9 @@ loop_iterations (loop)
        {
          struct induction *biv_inc;
 
-         increment
-           = fold_rtx_mult_add (v->mult_val, increment, const0_rtx, v->mode);
+         increment = fold_rtx_mult_add (v->mult_val,
+                                        extend_value_for_giv (v, increment),
+                                        const0_rtx, v->mode);
          /* The caller assumes that one full increment has occured at the
             first loop test.  But that's not true when the biv is incremented
             after the giv is set (which is the usual case), e.g.:
@@ -3694,7 +3662,6 @@ loop_iterations (loop)
              if (loop_insn_first_p (v->insn, biv_inc->insn))
                offset -= INTVAL (biv_inc->add_val);
            }
-         offset *= INTVAL (v->mult_val);
        }
       if (loop_dump_stream)
        fprintf (loop_dump_stream,
@@ -3760,7 +3727,7 @@ loop_iterations (loop)
      its value from the insns before the start of the loop.  */
 
   final_value = comparison_value;
-  if (GET_CODE (comparison_value) == REG 
+  if (GET_CODE (comparison_value) == REG
       && loop_invariant_p (loop, comparison_value))
     {
       final_value = loop_find_equiv_value (loop, comparison_value);
@@ -3847,12 +3814,12 @@ loop_iterations (loop)
        {
          rtx temp;
 
-         /*  When running the loop optimizer twice, check_dbra_loop
-             further obfuscates reversible loops of the form:
-             for (i = init; i < init + const; i++).  We often end up with
-             final_value = 0, initial_value = temp, temp = temp2 - init,
-             where temp2 = init + const.  If the loop has a vtop we
-             can replace initial_value with const.  */
+         /* When running the loop optimizer twice, check_dbra_loop
+            further obfuscates reversible loops of the form:
+            for (i = init; i < init + const; i++).  We often end up with
+            final_value = 0, initial_value = temp, temp = temp2 - init,
+            where temp2 = init + const.  If the loop has a vtop we
+            can replace initial_value with const.  */
 
          temp = loop_find_equiv_value (loop, reg1);
 
@@ -3890,7 +3857,7 @@ loop_iterations (loop)
      Check this now so that we won't leave an invalid value if we
      return early for any other reason.  */
   if (comparison_code == EQ)
-      loop_info->final_equiv_value = loop_info->final_value = 0;
+    loop_info->final_equiv_value = loop_info->final_value = 0;
 
   if (increment == 0)
     {
@@ -3914,7 +3881,7 @@ loop_iterations (loop)
            {
              fprintf (loop_dump_stream,
                       "Loop iterations: Increment value not constant ");
-             print_rtl (loop_dump_stream, increment);
+             print_simple_rtl (loop_dump_stream, increment);
              fprintf (loop_dump_stream, ".\n");
            }
          return 0;
@@ -3928,7 +3895,7 @@ loop_iterations (loop)
        {
          fprintf (loop_dump_stream,
                   "Loop iterations: Initial value not constant ");
-         print_rtl (loop_dump_stream, initial_value);
+         print_simple_rtl (loop_dump_stream, initial_value);
          fprintf (loop_dump_stream, ".\n");
        }
       return 0;
@@ -3936,8 +3903,7 @@ loop_iterations (loop)
   else if (comparison_code == EQ)
     {
       if (loop_dump_stream)
-       fprintf (loop_dump_stream,
-                "Loop iterations: EQ comparison loop.\n");
+       fprintf (loop_dump_stream, "Loop iterations: EQ comparison loop.\n");
       return 0;
     }
   else if (GET_CODE (final_value) != CONST_INT)
@@ -3946,7 +3912,7 @@ loop_iterations (loop)
        {
          fprintf (loop_dump_stream,
                   "Loop iterations: Final value not constant ");
-         print_rtl (loop_dump_stream, final_value);
+         print_simple_rtl (loop_dump_stream, final_value);
          fprintf (loop_dump_stream, ".\n");
        }
       return 0;
@@ -4004,8 +3970,7 @@ loop_iterations (loop)
   else
     {
       if (loop_dump_stream)
-       fprintf (loop_dump_stream,
-                "Loop iterations: Not normal loop.\n");
+       fprintf (loop_dump_stream, "Loop iterations: Not normal loop.\n");
       return 0;
     }
 
@@ -4013,17 +3978,28 @@ loop_iterations (loop)
      so correct for that.  Note that abs_diff and n_iterations are
      unsigned, because they can be as large as 2^n - 1.  */
 
-  abs_inc = INTVAL (increment);
-  if (abs_inc > 0)
-    abs_diff = INTVAL (final_value) - INTVAL (initial_value);
-  else if (abs_inc < 0)
+  inc = INTVAL (increment);
+  if (inc > 0)
+    {
+      abs_diff = INTVAL (final_value) - INTVAL (initial_value);
+      abs_inc = inc;
+    }
+  else if (inc < 0)
     {
       abs_diff = INTVAL (initial_value) - INTVAL (final_value);
-      abs_inc = -abs_inc;
+      abs_inc = -inc;
     }
   else
     abort ();
 
+  /* Given that iteration_var is going to iterate over its own mode,
+     not HOST_WIDE_INT, disregard higher bits that might have come
+     into the picture due to sign extension of initial and final
+     values.  */
+  abs_diff &= ((unsigned HOST_WIDE_INT)1
+              << (GET_MODE_BITSIZE (GET_MODE (iteration_var)) - 1)
+              << 1) - 1;
+
   /* For NE tests, make sure that the iteration variable won't miss
      the final value.  If abs_diff mod abs_incr is not zero, then the
      iteration variable will overflow before the loop exits, and we
@@ -4038,15 +4014,16 @@ loop_iterations (loop)
   return loop_info->n_iterations;
 }
 
-
 /* Replace uses of split bivs with their split pseudo register.  This is
    for original instructions which remain after loop unrolling without
    copying.  */
 
 static rtx
-remap_split_bivs (x)
+remap_split_bivs (loop, x)
+     struct loop *loop;
      rtx x;
 {
+  struct loop_ivs *ivs = LOOP_IVS (loop);
   register enum rtx_code code;
   register int i;
   register const char *fmt;
@@ -4072,9 +4049,9 @@ remap_split_bivs (x)
       /* If non-reduced/final-value givs were split, then this would also
         have to remap those givs also.  */
 #endif
-      if (REGNO (x) < max_reg_before_loop
-         && REG_IV_TYPE (REGNO (x)) == BASIC_INDUCT)
-       return reg_biv_class[REGNO (x)]->biv->src_reg;
+      if (REGNO (x) < ivs->n_regs
+         && REG_IV_TYPE (ivs, REGNO (x)) == BASIC_INDUCT)
+       return REG_IV_CLASS (ivs, REGNO (x))->biv->src_reg;
       break;
 
     default:
@@ -4085,12 +4062,12 @@ remap_split_bivs (x)
   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
     {
       if (fmt[i] == 'e')
-       XEXP (x, i) = remap_split_bivs (XEXP (x, i));
+       XEXP (x, i) = remap_split_bivs (loop, XEXP (x, i));
       else if (fmt[i] == 'E')
        {
          register int j;
          for (j = 0; j < XVECLEN (x, i); j++)
-           XVECEXP (x, i, j) = remap_split_bivs (XVECEXP (x, i, j));
+           XVECEXP (x, i, j) = remap_split_bivs (loop, XVECEXP (x, i, j));
        }
     }
   return x;
@@ -4125,7 +4102,7 @@ set_dominates_use (regno, first_uid, last_uid, copy_start, copy_end)
   while (INSN_UID (p) != first_uid)
     {
       if (GET_CODE (p) == JUMP_INSN)
-       passed_jump= 1;
+       passed_jump = 1;
       /* Could not find FIRST_UID.  */
       if (p == copy_end)
        return 0;
@@ -4162,10 +4139,11 @@ set_dominates_use (regno, first_uid, last_uid, copy_start, copy_end)
    unconditional branch to the loop continuation note (or a label just after).
    In this case, the unconditional branch that starts the loop needs to be
    deleted so that we execute the single iteration.  */
+
 static rtx
 ujump_to_loop_cont (loop_start, loop_cont)
-      rtx loop_start;
-      rtx loop_cont;
+     rtx loop_start;
+     rtx loop_cont;
 {
   rtx x, label, label_ref;
 
@@ -4186,11 +4164,8 @@ ujump_to_loop_cont (loop_start, loop_cont)
     return NULL_RTX;
 
   /* Return the loop start if the branch label matches the code label.  */
-  if (CODE_LABEL_NUMBER (label) == CODE_LABEL_NUMBER (XEXP (label_ref,0)))
+  if (CODE_LABEL_NUMBER (label) == CODE_LABEL_NUMBER (XEXP (label_ref, 0)))
     return loop_start;
   else
     return NULL_RTX;
-
 }
-
-