OSDN Git Service

(c_sizeof, build_c_cast): Set TREE_OVERFLOW in addition
[pf3gnuchains/gcc-fork.git] / gcc / unroll.c
index 86c229d..fc8a3f6 100644 (file)
@@ -1,5 +1,5 @@
 /* Try to unroll loops, and split induction variables.
-   Copyright (C) 1992 Free Software Foundation, Inc.
+   Copyright (C) 1992, 1993 Free Software Foundation, Inc.
    Contributed by James E. Wilson, Cygnus Support/UC Berkeley.
 
 This file is part of GNU CC.
@@ -59,7 +59,7 @@ the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
 
    - On traditional architectures, unrolling a non-constant bound loop
      is a win if there is a giv whose only use is in memory addresses, the
-     memory addresses can be split, and hence giv incremenets can be
+     memory addresses can be split, and hence giv increments can be
      eliminated.
    - It is also a win if the loop is executed many times, and preconditioning
      can be performed for the loop.
@@ -205,7 +205,7 @@ static rtx fold_rtx_mult_add ();
 /* 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_BEDFORE indicates where insns should be added
+   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.
@@ -324,12 +324,12 @@ unroll_loop (loop_end, insn_count, loop_start, end_insert_before,
       else if (GET_CODE (last_loop_insn) == JUMP_INSN)
        {
 #ifdef HAVE_cc0
-         /* The immediately preceeding insn is a compare which must be
+         /* The immediately preceding insn is a compare which must be
             deleted.  */
          delete_insn (last_loop_insn);
          delete_insn (PREV_INSN (last_loop_insn));
 #else
-         /* The immediately preceeding insn may not be the compare, so don't
+         /* The immediately preceding insn may not be the compare, so don't
             delete it.  */
          delete_insn (last_loop_insn);
 #endif
@@ -620,6 +620,8 @@ unroll_loop (loop_end, insn_count, loop_start, end_insert_before,
 
   map = (struct inline_remap *) alloca (sizeof (struct inline_remap));
 
+  map->integrating = 0;
+
   /* Allocate the label map.  */
 
   if (max_labelno > 0)
@@ -688,7 +690,7 @@ unroll_loop (loop_end, insn_count, loop_start, end_insert_before,
 
      It is safe to do this here, since the extra registers created by the
      preconditioning code and find_splittable_regs will never be used
-     to accees the splittable_regs[] and addr_combined_regs[] arrays.  */
+     to access the splittable_regs[] and addr_combined_regs[] arrays.  */
 
   splittable_regs = (rtx *) alloca (maxregnum * sizeof (rtx));
   bzero (splittable_regs, maxregnum * sizeof (rtx));
@@ -783,15 +785,14 @@ unroll_loop (loop_end, insn_count, loop_start, end_insert_before,
             improperly shared rtl.  */
 
          diff = expand_binop (mode, sub_optab, copy_rtx (final_value),
-                              copy_rtx (initial_value), 0, 0,
+                              copy_rtx (initial_value), NULL_RTX, 0,
                               OPTAB_LIB_WIDEN);
 
          /* Now calculate (diff % (unroll * abs (increment))) by using an
             and instruction.  */
          diff = expand_binop (GET_MODE (diff), and_optab, diff,
-                              gen_rtx (CONST_INT, VOIDmode,
-                                       unroll_number * abs_inc - 1),
-                              0, 0, OPTAB_LIB_WIDEN);
+                              GEN_INT (unroll_number * abs_inc - 1),
+                              NULL_RTX, 0, OPTAB_LIB_WIDEN);
 
          /* Now emit a sequence of branches to jump to the proper precond
             loop entry point.  */
@@ -826,9 +827,8 @@ unroll_loop (loop_end, insn_count, loop_start, end_insert_before,
              else
                cmp_const = i;
 
-             emit_cmp_insn (diff, gen_rtx (CONST_INT, VOIDmode,
-                                           abs_inc * cmp_const),
-                            EQ, 0, mode, 0, 0);
+             emit_cmp_insn (diff, GEN_INT (abs_inc * cmp_const),
+                            EQ, NULL_RTX, mode, 0, 0);
 
              if (i == 0)
                emit_jump_insn (gen_beq (labels[i]));
@@ -858,8 +858,8 @@ unroll_loop (loop_end, insn_count, loop_start, end_insert_before,
              else
                cmp_const = abs_inc * (unroll_number - 1) + 1;
 
-             emit_cmp_insn (diff, gen_rtx (CONST_INT, VOIDmode, cmp_const),
-                            EQ, 0, mode, 0, 0);
+             emit_cmp_insn (diff, GEN_INT (cmp_const), EQ, NULL_RTX,
+                            mode, 0, 0);
 
              if (neg_inc)
                emit_jump_insn (gen_ble (labels[0]));
@@ -883,11 +883,11 @@ unroll_loop (loop_end, insn_count, loop_start, end_insert_before,
          else if (GET_CODE (last_loop_insn) == JUMP_INSN)
            {
 #ifdef HAVE_cc0
-             /* The immediately preceeding insn is a compare which we do not
+             /* The immediately preceding insn is a compare which we do not
                 want to copy.  */
              copy_end = PREV_INSN (PREV_INSN (last_loop_insn));
 #else
-             /* The immediately preceeding insn may not be a compare, so we
+             /* The immediately preceding insn may not be a compare, so we
                 must copy it.  */
              copy_end = PREV_INSN (last_loop_insn);
 #endif
@@ -937,12 +937,12 @@ unroll_loop (loop_end, insn_count, loop_start, end_insert_before,
          else
            {
 #ifdef HAVE_cc0
-             /* The immediately preceeding insn is a compare which we do not
+             /* The immediately preceding insn is a compare which we do not
                 want to copy.  */
              insert_before = PREV_INSN (last_loop_insn);
              copy_end = PREV_INSN (insert_before);
 #else
-             /* The immediately preceeding insn may not be a compare, so we
+             /* The immediately preceding insn may not be a compare, so we
                 must copy it.  */
              insert_before = last_loop_insn;
              copy_end = PREV_INSN (last_loop_insn);
@@ -1137,19 +1137,6 @@ unroll_loop (loop_end, insn_count, loop_start, end_insert_before,
      not taken.  */
   if (exit_label)
     emit_label_after (exit_label, loop_end);
-
-  /* If debugging, we must replicate the tree nodes corresponsing to the blocks
-     inside the loop, so that the original one to one mapping will remain.  */
-
-  if (write_symbols != NO_DEBUG)
-    {
-      int copies = unroll_number;
-
-      if (loop_preconditioned)
-       copies += unroll_number - 1;
-
-      unroll_block_trees (uid_loop_num[INSN_UID (loop_start)], copies);
-    }
 }
 \f
 /* Return true if the loop can be safely, and profitably, preconditioned
@@ -1178,7 +1165,7 @@ precondition_loop_p (initial_value, final_value, increment, loop_start,
     {
       *initial_value = const0_rtx;
       *increment = const1_rtx;
-      *final_value = gen_rtx (CONST_INT, VOIDmode, loop_n_iterations);
+      *final_value = GEN_INT (loop_n_iterations);
 
       if (loop_dump_stream)
        fprintf (loop_dump_stream,
@@ -1230,7 +1217,7 @@ precondition_loop_p (initial_value, final_value, increment, loop_start,
 
   /* 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 convered by invariant_p.
+     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.  */
   if ((GET_CODE (loop_final_value) == REG
@@ -1248,7 +1235,7 @@ precondition_loop_p (initial_value, final_value, increment, loop_start,
   /* Fail for floating point values, since the caller of this function
      does not have code to deal with them.  */
   if (GET_MODE_CLASS (GET_MODE (loop_final_value)) == MODE_FLOAT
-      || GET_MODE_CLASS (GET_MODE (loop_initial_value) == MODE_FLOAT))
+      || GET_MODE_CLASS (GET_MODE (loop_initial_value)) == MODE_FLOAT)
     {
       if (loop_dump_stream)
        fprintf (loop_dump_stream,
@@ -1331,7 +1318,10 @@ calculate_giv_inc (pattern, src_insn, regno)
      int regno;
 {
   rtx increment;
+  rtx increment_total = 0;
+  int tries = 0;
 
+ retry:
   /* Verify that we have an increment insn here.  First check for a plus
      as the set source.  */
   if (GET_CODE (SET_SRC (pattern)) != PLUS)
@@ -1354,7 +1344,8 @@ calculate_giv_inc (pattern, src_insn, regno)
     {
       /* SR sometimes puts the constant in a register, especially if it is
         too big to be an add immed operand.  */
-      increment = SET_SRC (PATTERN (PREV_INSN (src_insn)));
+      src_insn = PREV_INSN (src_insn);
+      increment = SET_SRC (PATTERN (src_insn));
 
       /* SR may have used LO_SUM to compute the constant if it is too large
         for a load immed operand.  In this case, the constant is in operand
@@ -1370,14 +1361,81 @@ calculate_giv_inc (pattern, src_insn, regno)
       delete_insn (get_last_insn ());
     }
 
-  /* Check that the source register is the same as the dest register.  */
+  if (increment_total)
+    increment_total = GEN_INT (INTVAL (increment_total) + INTVAL (increment));
+  else
+    increment_total = increment;
+
+  /* Check that the source register is the same as the register we expected
+     to see as the source.  If not, something is seriously wrong.  */
   if (GET_CODE (XEXP (SET_SRC (pattern), 0)) != REG
       || REGNO (XEXP (SET_SRC (pattern), 0)) != regno)
+    {
+      /* Some machines (e.g. the romp), may emit two add instructions for
+        certain constants, so lets try looking for another add immediately
+        before this one if we have only seen one add insn so far.  */
+
+      if (tries == 0)
+       {
+         tries++;
+
+         src_insn = PREV_INSN (src_insn);
+         pattern = PATTERN (src_insn);
+
+         delete_insn (get_last_insn ());
+
+         goto retry;
+       }
+
+      abort ();
+    }
+
+  return increment_total;
+}
+
+/* Copy REG_NOTES, except for insn references, because not all insn_map
+   entries are valid yet.  We do need to copy registers now though, because
+   the reg_map entries can change during copying.  */
+
+static rtx
+initial_reg_note_copy (notes, map)
+     rtx notes;
+     struct inline_remap *map;
+{
+  rtx copy;
+
+  if (notes == 0)
+    return 0;
+
+  copy = rtx_alloc (GET_CODE (notes));
+  PUT_MODE (copy, GET_MODE (notes));
+
+  if (GET_CODE (notes) == EXPR_LIST)
+    XEXP (copy, 0) = copy_rtx_and_substitute (XEXP (notes, 0), map);
+  else if (GET_CODE (notes) == INSN_LIST)
+    /* Don't substitute for these yet.  */
+    XEXP (copy, 0) = XEXP (notes, 0);
+  else
     abort ();
 
-  return increment;
+  XEXP (copy, 1) = initial_reg_note_copy (XEXP (notes, 1), map);
+
+  return copy;
 }
 
+/* Fixup insn references in copied REG_NOTES.  */
+
+static void
+final_reg_note_copy (notes, map)
+     rtx notes;
+     struct inline_remap *map;
+{
+  rtx note;
+
+  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))];
+}
 
 /* Copy each instruction in the loop, substituting from map as appropriate.
    This is very similar to a loop in expand_inline_function.  */
@@ -1388,6 +1446,7 @@ copy_loop_body (copy_start, copy_end, map, exit_label, last_iteration,
                copy_notes_from)
      rtx copy_start, copy_end;
      struct inline_remap *map;
+     rtx exit_label;
      int last_iteration;
      enum unroll_types unroll_type;
      rtx start_label, loop_end, insert_before, copy_notes_from;
@@ -1461,10 +1520,15 @@ copy_loop_body (copy_start, copy_end, map, exit_label, last_iteration,
              for (tv = bl->giv; tv; tv = tv->next_iv)
                if (tv->giv_type == DEST_ADDR && tv->same == v)
                  {
-                   /* Increment the giv by the amount that was calculated in
-                      find_splittable_givs, and saved in add_val.  */
-                   tv->dest_reg = plus_constant (tv->dest_reg,
-                                                 INTVAL (tv->add_val));
+                   int this_giv_inc = INTVAL (giv_inc);
+
+                   /* Scale this_giv_inc if the multiplicative factors of
+                      the two givs are different.  */
+                   if (tv->mult_val != v->mult_val)
+                     this_giv_inc = (this_giv_inc / INTVAL (v->mult_val)
+                                     * INTVAL (tv->mult_val));
+                      
+                   tv->dest_reg = plus_constant (tv->dest_reg, this_giv_inc);
                    *tv->location = tv->dest_reg;
                    
                    if (last_iteration && unroll_type != UNROLL_COMPLETELY)
@@ -1493,8 +1557,11 @@ copy_loop_body (copy_start, copy_end, map, exit_label, last_iteration,
                                           XEXP (value, 1));
                        
                        /* Reset the giv to be just the register again, in case
-                          it is used after the set we have just emitted.  */
-                       tv->dest_reg = dest_reg;
+                          it is used after the set we have just emitted.
+                          We must subtract the const_adjust factor added in
+                          above.  */
+                       tv->dest_reg = plus_constant (dest_reg,
+                                                     - tv->const_adjust);
                        *tv->location = tv->dest_reg;
                      }
                  }
@@ -1575,8 +1642,7 @@ copy_loop_body (copy_start, copy_end, map, exit_label, last_iteration,
 #endif
                  
                  splittable_regs[regno]
-                   = gen_rtx (CONST_INT, VOIDmode,
-                              INTVAL (giv_inc)
+                   = GEN_INT (INTVAL (giv_inc)
                               + INTVAL (splittable_regs[regno]));
                  giv_inc = splittable_regs[regno];
                  
@@ -1616,7 +1682,7 @@ copy_loop_body (copy_start, copy_end, map, exit_label, last_iteration,
              pattern = copy_rtx_and_substitute (pattern, map);
              copy = emit_insn (pattern);
            }
-         /* REG_NOTES will be copied later.  */
+         REG_NOTES (copy) = initial_reg_note_copy (REG_NOTES (insn), map);
          
 #ifdef HAVE_cc0
          /* If this insn is setting CC0, it may need to look at
@@ -1658,6 +1724,10 @@ copy_loop_body (copy_start, copy_end, map, exit_label, last_iteration,
          break;
          
        case JUMP_INSN:
+         pattern = copy_rtx_and_substitute (PATTERN (insn), map);
+         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)
            {
@@ -1667,31 +1737,13 @@ copy_loop_body (copy_start, copy_end, map, exit_label, last_iteration,
                 case to be a branch past the end of the loop, and the
                 original jump label case to fall_through.  */
 
-             int fall_through;
-
-             /* Never map the label in this case.  */
-             pattern = copy_rtx (PATTERN (insn));
-             
-             /* Assume a conditional branch, since the code above
-                does not let unconditional branches be copied.  */
-             if (! condjump_p (insn))
+             if (! invert_exp (pattern, copy)
+                 || ! redirect_exp (&pattern,
+                                    map->label_map[CODE_LABEL_NUMBER
+                                                   (JUMP_LABEL (insn))],
+                                    exit_label, copy))
                abort ();
-             fall_through
-               = (XEXP (SET_SRC (PATTERN (insn)), 2) == pc_rtx) + 1;
-
-             /* Set the fall through case to the exit label.  Must
-                create a new label_ref since they can't be shared.  */
-             XEXP (SET_SRC (pattern), fall_through)
-               = gen_rtx (LABEL_REF, VOIDmode, exit_label);
-                     
-             /* Set the original branch case to fall through.  */
-             XEXP (SET_SRC (pattern), 3 - fall_through)
-               = pc_rtx;
            }
-         else
-           pattern = copy_rtx_and_substitute (PATTERN (insn), map);
-         
-         copy = emit_jump_insn (pattern);
          
 #ifdef HAVE_cc0
          if (cc0_insn)
@@ -1704,23 +1756,26 @@ copy_loop_body (copy_start, copy_end, map, exit_label, last_iteration,
             later passes of unroll_loop, if INSN had jump label set.  */
          if (JUMP_LABEL (insn))
            {
+             rtx label = 0;
+
              /* Can't use the label_map for every insn, since this may be
                 the backward branch, and hence the label was not mapped.  */
              if (GET_CODE (pattern) == SET)
                {
                  tem = SET_SRC (pattern);
                  if (GET_CODE (tem) == LABEL_REF)
-                   JUMP_LABEL (copy) = XEXP (tem, 0);
+                   label = XEXP (tem, 0);
                  else if (GET_CODE (tem) == IF_THEN_ELSE)
                    {
                      if (XEXP (tem, 1) != pc_rtx)
-                       JUMP_LABEL (copy) = XEXP (XEXP (tem, 1), 0);
+                       label = XEXP (XEXP (tem, 1), 0);
                      else
-                       JUMP_LABEL (copy) = XEXP (XEXP (tem, 2), 0);
+                       label = XEXP (XEXP (tem, 2), 0);
                    }
-                 else
-                   abort ();
                }
+
+             if (label && GET_CODE (label) == CODE_LABEL)
+               JUMP_LABEL (copy) = label;
              else
                {
                  /* An unrecognizable jump insn, probably the entry jump
@@ -1773,6 +1828,7 @@ copy_loop_body (copy_start, copy_end, map, exit_label, last_iteration,
        case CALL_INSN:
          pattern = copy_rtx_and_substitute (PATTERN (insn), map);
          copy = emit_call_insn (pattern);
+         REG_NOTES (copy) = initial_reg_note_copy (REG_NOTES (insn), map);
 
 #ifdef HAVE_cc0
          if (cc0_insn)
@@ -1802,7 +1858,12 @@ copy_loop_body (copy_start, copy_end, map, exit_label, last_iteration,
          break;
          
        case NOTE:
-         if (NOTE_LINE_NUMBER (insn) != NOTE_INSN_DELETED)
+         /* VTOP notes are valid only before the loop exit test.  If placed
+            anywhere else, loop may generate bad code.  */
+            
+         if (NOTE_LINE_NUMBER (insn) != NOTE_INSN_DELETED
+             && (NOTE_LINE_NUMBER (insn) != NOTE_INSN_LOOP_VTOP
+                 || (last_iteration && unroll_type != UNROLL_COMPLETELY)))
            copy = emit_note (NOTE_SOURCE_FILE (insn),
                              NOTE_LINE_NUMBER (insn));
          else
@@ -1818,7 +1879,7 @@ copy_loop_body (copy_start, copy_end, map, exit_label, last_iteration,
     }
   while (insn != copy_end);
   
-  /* Now copy the REG_NOTES.  */
+  /* Now finish coping the REG_NOTES.  */
   insn = copy_start;
   do
     {
@@ -1826,8 +1887,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)])
-       REG_NOTES (map->insn_map[INSN_UID (insn)])
-         = copy_rtx_and_substitute (REG_NOTES (insn), map);
+       final_reg_note_copy (REG_NOTES (map->insn_map[INSN_UID (insn)]), map);
     }
   while (insn != copy_end);
 
@@ -1880,7 +1940,7 @@ emit_unrolled_add (dest_reg, src_reg, increment)
    is a backward branch in that range that branches to somewhere between
    LOOP_START and INSN.  Returns 0 otherwise.  */
 
-/* ??? This is quadratic algorithm.  Could be rewriten to be linear.
+/* ??? 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.  */
 
@@ -2050,7 +2110,7 @@ iteration_info (iteration_var, initial_value, increment, loop_start, loop_end)
     {
       if (loop_dump_stream)
        fprintf (loop_dump_stream,
-                "Loop unrolling: Iteration var not an interger.\n");
+                "Loop unrolling: Iteration var not an integer.\n");
       return;
     }
   else if (reg_iv_type[REGNO (iteration_var)] == BASIC_INDUCT)
@@ -2186,6 +2246,7 @@ find_splittable_regs (unroll_type, loop_start, loop_end, end_insert_before,
      int unroll_number;
 {
   struct iv_class *bl;
+  struct induction *v;
   rtx increment, tem;
   rtx biv_final_value;
   int biv_splittable;
@@ -2223,6 +2284,15 @@ find_splittable_regs (unroll_type, loop_start, loop_end, end_insert_before,
          && ! (biv_final_value = final_biv_value (bl, loop_start, loop_end)))
        biv_splittable = 0;
 
+      /* If any of the insns setting the BIV don't do so with a simple
+        PLUS, we don't know how to split it.  */
+      for (v = bl->biv; biv_splittable && v; v = v->next_iv)
+       if ((tem = single_set (v->insn)) == 0
+           || GET_CODE (SET_DEST (tem)) != REG
+           || REGNO (SET_DEST (tem)) != bl->regno
+           || GET_CODE (SET_SRC (tem)) != PLUS)
+         biv_splittable = 0;
+
       /* 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
         handling all of the givs, since some of them may need to use the
@@ -2237,7 +2307,7 @@ find_splittable_regs (unroll_type, loop_start, loop_end, end_insert_before,
            {
              /* If the initial value of the biv is itself (i.e. it is too
                 complicated for strength_reduce to compute), or is a hard
-                register, then we must create a new psuedo reg to hold the
+                register, then we must create a new pseudo reg to hold the
                 initial value of the biv.  */
 
              if (GET_CODE (bl->initial_value) == REG
@@ -2383,7 +2453,7 @@ find_splittable_givs (bl, unroll_type, loop_start, loop_end, increment,
               /* Check for the case where the pseudo is set by a shift/add
                  sequence, in which case the first insn setting the pseudo
                  is the first insn of the shift/add sequence.  */
-              && (! (tem = find_reg_note (v->insn, REG_RETVAL, 0))
+              && (! (tem = find_reg_note (v->insn, REG_RETVAL, NULL_RTX))
                   || (regno_first_uid[REGNO (v->dest_reg)]
                       != INSN_UID (XEXP (tem, 0)))))
              /* Line above always fails if INSN was moved by loop opt.  */
@@ -2422,34 +2492,71 @@ find_splittable_givs (bl, unroll_type, loop_start, loop_end, increment,
         giv's initial value.  Otherwise, save the constant zero for it.  */
 
       if (unroll_type == UNROLL_COMPLETELY)
-       /* It is not safe to use bl->initial_value here, because it may not
-          be invariant.  It is safe to use the initial value stored in
-          the splittable_regs array.  */
-       value = fold_rtx_mult_add (v->mult_val, splittable_regs[bl->regno],
-                                  v->add_val, v->mode);
+       {
+         /* It is not safe to use bl->initial_value here, because it may not
+            be invariant.  It is safe to use the initial value stored in
+            the splittable_regs array if it is set.  In rare cases, it won't
+            be set, so then we do exactly the same thing as
+            find_splittable_regs does to get a safe value.  */
+         rtx biv_initial_value;
+
+         if (splittable_regs[bl->regno])
+           biv_initial_value = splittable_regs[bl->regno];
+         else if (GET_CODE (bl->initial_value) != REG
+                  || (REGNO (bl->initial_value) != bl->regno
+                      && REGNO (bl->initial_value) >= FIRST_PSEUDO_REGISTER))
+           biv_initial_value = bl->initial_value;
+         else
+           {
+             rtx tem = gen_reg_rtx (bl->biv->mode);
+
+             emit_insn_before (gen_move_insn (tem, bl->biv->src_reg),
+                               loop_start);
+             biv_initial_value = tem;
+           }
+         value = fold_rtx_mult_add (v->mult_val, biv_initial_value,
+                                    v->add_val, v->mode);
+       }
       else
        value = const0_rtx;
 
       if (v->new_reg)
        {
-         /* If the giv is an address destination, it could be something other
-            than a simple register, these have to be treated differently.  */
-         if (v->giv_type == DEST_REG)
-           splittable_regs[REGNO (v->new_reg)] = value;
-
-         /* If an addr giv was combined with another addr giv, then we
-            can only split this giv if the addr giv it was combined with
-            was reduced.  This is because the value of v->new_reg is
-            meaningless in this case.  (There is no problem if it was
-            combined with a dest_reg giv which wasn't reduced, v->new_reg
-            is still meaningful in this case.)  */
-
-         else if (v->same && v->same->giv_type == DEST_ADDR
-                 && ! v->same->new_reg) 
+         /* If a giv was combined with another giv, then we can only split
+            this giv if the giv it was combined with was reduced.  This
+            is because the value of v->new_reg is meaningless in this
+            case.  */
+         if (v->same && ! v->same->new_reg)
            {
              if (loop_dump_stream)
                fprintf (loop_dump_stream,
-                        "DEST_ADDR giv not split, because combined with unreduced DEST_ADDR giv.\n");
+                        "giv combined with unreduced giv not split.\n");
+             continue;
+           }
+         /* If the giv is an address destination, it could be something other
+            than a simple register, these have to be treated differently.  */
+         else if (v->giv_type == DEST_REG)
+           {
+             /* If value is not a constant, register, or register plus
+                constant, then compute its value into a register before
+                loop start.  This prevents illegal rtx sharing, and should
+                generate better code.  We can use bl->initial_value here
+                instead of splittable_regs[bl->regno] because this code
+                is going before the loop start.  */
+             if (unroll_type == UNROLL_COMPLETELY
+                 && GET_CODE (value) != CONST_INT
+                 && GET_CODE (value) != REG
+                 && (GET_CODE (value) != PLUS
+                     || GET_CODE (XEXP (value, 0)) != REG
+                     || GET_CODE (XEXP (value, 1)) != CONST_INT))
+               {
+                 rtx tem = gen_reg_rtx (v->mode);
+                 emit_iv_add_mult (bl->initial_value, v->mult_val,
+                                   v->add_val, tem, loop_start);
+                 value = tem;
+               }
+               
+             splittable_regs[REGNO (v->new_reg)] = value;
            }
          else
            {
@@ -2491,8 +2598,8 @@ find_splittable_givs (bl, unroll_type, loop_start, loop_end, increment,
                         Try to validate both the first and the last
                         address resulting from loop unrolling, if
                         one fails, then can't do const elim here.  */
-                     if (memory_address_p (v->mode, v->dest_reg)
-                         && memory_address_p (v->mode,
+                     if (memory_address_p (v->mem_mode, v->dest_reg)
+                         && memory_address_p (v->mem_mode,
                                       plus_constant (v->dest_reg,
                                                      INTVAL (giv_inc)
                                                      * (unroll_number - 1))))
@@ -2518,8 +2625,8 @@ find_splittable_givs (bl, unroll_type, loop_start, loop_end, increment,
                     now, and fail completely if either the first or the last
                     unrolled copy of the address is not a valid address.  */
                  if (v->dest_reg == tem
-                     && (! memory_address_p (v->mode, v->dest_reg)
-                         || ! memory_address_p (v->mode,
+                     && (! memory_address_p (v->mem_mode, v->dest_reg)
+                         || ! memory_address_p (v->mem_mode,
                                 plus_constant (v->dest_reg,
                                                INTVAL (giv_inc)
                                                * (unroll_number -1)))))
@@ -2539,7 +2646,7 @@ find_splittable_givs (bl, unroll_type, loop_start, loop_end, increment,
                  emit_insn_before (gen_rtx (SET, VOIDmode, tem,
                                             copy_rtx (v->new_reg)),
                                    loop_start);
-                 if (! recog_memoized (PREV_INSN (loop_start)))
+                 if (recog_memoized (PREV_INSN (loop_start)) < 0)
                    {
                      delete_insn (PREV_INSN (loop_start));
                      emit_iv_add_mult (bl->initial_value, v->mult_val,
@@ -2555,8 +2662,8 @@ find_splittable_givs (bl, unroll_type, loop_start, loop_end, increment,
                  
                  /* Check the resulting address for validity, and fail
                     if the resulting address would be illegal.  */
-                 if (! memory_address_p (v->mode, v->dest_reg)
-                     || ! memory_address_p (v->mode,
+                 if (! memory_address_p (v->mem_mode, v->dest_reg)
+                     || ! memory_address_p (v->mem_mode,
                                     plus_constant (v->dest_reg,
                                                    INTVAL (giv_inc) *
                                                    (unroll_number -1))))
@@ -2599,13 +2706,6 @@ find_splittable_givs (bl, unroll_type, loop_start, loop_end, increment,
                    }
                }
 
-             /* Overwrite the old add_val, which is no longer needed, and
-                substitute the amount that the giv is incremented on each
-                iteration.  We need to save this somewhere, so we know how
-                much to increment split DEST_ADDR giv's in copy_loop_body.  */
-
-             v->add_val = giv_inc;
-
              if (loop_dump_stream)
                fprintf (loop_dump_stream, "DEST_ADDR giv being split.\n");
            }
@@ -2669,6 +2769,7 @@ reg_dead_after_loop (reg, loop_start, loop_end)
 {
   rtx insn, label;
   enum rtx_code code;
+  int jump_count = 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
@@ -2684,54 +2785,33 @@ reg_dead_after_loop (reg, loop_start, loop_end)
         a conditional jump.  */
 
       insn = NEXT_INSN (XEXP (label, 0));
-      while (1)
+      while (insn)
        {
-         if (insn == 0)
-           break;
-
-         if ((code = GET_CODE (insn)) == INSN || code == JUMP_INSN
-             || code == CALL_INSN)
+         code = GET_CODE (insn);
+         if (GET_RTX_CLASS (code) == 'i')
            {
-             if (GET_CODE (PATTERN (insn)) == SET)
-               {
-                 if (reg_mentioned_p (reg, SET_SRC (PATTERN (insn))))
-                   return 0;
-                 if (SET_DEST (PATTERN (insn)) == reg)
-                   break;
-                 if (reg_mentioned_p (reg, SET_DEST (PATTERN (insn))))
-                   return 0;
-               }
-             else if (reg_mentioned_p (reg, PATTERN (insn)))
+             rtx set;
+
+             if (reg_referenced_p (reg, PATTERN (insn)))
                return 0;
+
+             set = single_set (insn);
+             if (set && rtx_equal_p (SET_DEST (set), reg))
+               break;
            }
+
          if (code == JUMP_INSN)
            {
              if (GET_CODE (PATTERN (insn)) == RETURN)
                break;
-             else if (! simplejump_p (insn))
+             else if (! simplejump_p (insn)
+                      /* Prevent infinite loop following infinite loops. */
+                      || jump_count++ > 20)
                return 0;
              else
-               {
-                 insn = JUMP_LABEL (insn);
-                 /* If this branches to a code label after a LOOP_BEG or
-                    a LOOP_CONT note, then assume it is a loop back edge.
-                    Must fail in that case to prevent going into an infinite
-                    loop trying to trace infinite loops.
-
-                    In the presence of syntax errors, this may be a jump to
-                    a CODE_LABEL that was never emitted.  Fail in this case
-                    also.  */
-
-                 if (! PREV_INSN (insn)
-                     || (GET_CODE (PREV_INSN (insn)) == NOTE
-                         && ((NOTE_LINE_NUMBER (PREV_INSN (insn))
-                              == NOTE_INSN_LOOP_BEG)
-                             || (NOTE_LINE_NUMBER (PREV_INSN (insn))
-                                 == NOTE_INSN_LOOP_CONT))))
-                   return 0;
-               }
+               insn = JUMP_LABEL (insn);
            }
-         
+
          insn = NEXT_INSN (insn);
        }
     }
@@ -2750,6 +2830,11 @@ final_biv_value (bl, loop_start, loop_end)
 {
   rtx increment, tem;
 
+  /* ??? This only works for MODE_INT biv's.  Reject all others for now.  */
+
+  if (GET_MODE_CLASS (bl->biv->mode) != MODE_INT)
+    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
      loop which sets this biv's final value (if necessary), and there are
@@ -2782,8 +2867,10 @@ final_biv_value (bl, loop_start, loop_end)
             case it is needed later.  */
 
          tem = gen_reg_rtx (bl->biv->mode);
-         emit_iv_add_mult (increment,
-                           gen_rtx (CONST_INT, VOIDmode, loop_n_iterations),
+         /* 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 (loop_n_iterations),
                            bl->initial_value, tem, NEXT_INSN (loop_end));
 
          if (loop_dump_stream)
@@ -2817,10 +2904,10 @@ final_giv_value (v, loop_start, loop_end)
      rtx loop_start, loop_end;
 {
   struct iv_class *bl;
-  rtx reg, insn, pattern;
+  rtx insn;
   rtx increment, tem;
   enum rtx_code code;
-  rtx insert_before;
+  rtx insert_before, seq;
 
   bl = reg_biv_class[REGNO (v->src_reg)];
 
@@ -2874,35 +2961,26 @@ final_giv_value (v, loop_start, loop_end)
 
          /* Put the final biv value in tem.  */
          tem = gen_reg_rtx (bl->biv->mode);
-         emit_iv_add_mult (increment,
-                           gen_rtx (CONST_INT, VOIDmode, loop_n_iterations),
+         emit_iv_add_mult (increment, GEN_INT (loop_n_iterations),
                            bl->initial_value, tem, insert_before);
 
          /* Subtract off extra increments as we find them.  */
          for (insn = NEXT_INSN (v->insn); insn != loop_end;
               insn = NEXT_INSN (insn))
            {
-             if (GET_CODE (insn) == INSN
-                 && GET_CODE (PATTERN (insn)) == SET
-                 && SET_DEST (PATTERN (insn)) == v->src_reg)
-               {
-                 pattern = PATTERN (insn);
-                 if (GET_CODE (SET_SRC (pattern)) != PLUS)
-                   {
-                     /* Sometimes a biv is computed in a temp reg,
-                        and then copied into the biv reg.  */
-                     pattern = PATTERN (PREV_INSN (insn));
-                     if (GET_CODE (SET_SRC (pattern)) != PLUS)
-                       abort ();
-                   }
-                 if (GET_CODE (XEXP (SET_SRC (pattern), 0)) != REG
-                     || REGNO (XEXP (SET_SRC (pattern), 0)) != bl->regno)
-                   abort ();
-                 
-                 tem = expand_binop (GET_MODE (tem), sub_optab, tem,
-                                     XEXP (SET_SRC (pattern), 1), 0, 0,
-                                     OPTAB_LIB_WIDEN);
-               }
+             struct induction *biv;
+
+             for (biv = bl->biv; biv; biv = biv->next_iv)
+               if (biv->insn == insn)
+                 {
+                   start_sequence ();
+                   tem = expand_binop (GET_MODE (tem), sub_optab, tem,
+                                       biv->add_val, NULL_RTX, 0,
+                                       OPTAB_LIB_WIDEN);
+                   seq = gen_sequence ();
+                   end_sequence ();
+                   emit_insn_before (seq, insert_before);
+                 }
            }
          
          /* Now calculate the giv's final value.  */
@@ -2938,16 +3016,17 @@ final_giv_value (v, loop_start, loop_end)
 
 
 /* Calculate the number of loop iterations.  Returns the exact number of loop
-   iterations if it can be calculated, otherwise retusns zero.  */
+   iterations if it can be calculated, otherwise returns zero.  */
 
-unsigned long
+unsigned HOST_WIDE_INT
 loop_iterations (loop_start, loop_end)
      rtx loop_start, loop_end;
 {
   rtx comparison, comparison_value;
   rtx iteration_var, initial_value, increment, final_value;
   enum rtx_code comparison_code;
-  int i, increment_dir;
+  HOST_WIDE_INT i;
+  int increment_dir;
   int unsigned_compare, compare_dir, final_larger;
   unsigned long tempu;
   rtx last_loop_insn;
@@ -3034,14 +3113,19 @@ loop_iterations (loop_start, loop_end)
            break;
 
          else if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
-                  && (set = single_set (insn))
-                  && (SET_DEST (set) == comparison_value))
+                  && reg_set_p (comparison_value, insn))
            {
-             rtx note = find_reg_note (insn, REG_EQUAL, 0);
-
-             if (note && GET_CODE (XEXP (note, 0)) != EXPR_LIST)
-               comparison_value = XEXP (note, 0);
+             /* We found the last insn before the loop that sets the register.
+                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) == comparison_value))
+               {
+                 rtx note = find_reg_note (insn, REG_EQUAL, NULL_RTX);
 
+                 if (note && GET_CODE (XEXP (note, 0)) != EXPR_LIST)
+                   comparison_value = XEXP (note, 0);
+               }
              break;
            }
        }
@@ -3081,17 +3165,19 @@ loop_iterations (loop_start, loop_end)
      be addresses with the same base but different constant offsets.
      Final value must be invariant for this to work.
 
-     To do this, need someway to find the values of registers which are
+     To do this, need some way to find the values of registers which are
      invariant.  */
 
   /* Final_larger is 1 if final larger, 0 if they are equal, otherwise -1.  */
   if (unsigned_compare)
     final_larger
-      = ((unsigned) INTVAL (final_value) > (unsigned) INTVAL (initial_value)) -
-       ((unsigned) INTVAL (final_value) < (unsigned) INTVAL (initial_value));
+      = ((unsigned HOST_WIDE_INT) INTVAL (final_value)
+        > (unsigned HOST_WIDE_INT) INTVAL (initial_value))
+       - ((unsigned HOST_WIDE_INT) INTVAL (final_value)
+          < (unsigned HOST_WIDE_INT) INTVAL (initial_value));
   else
-    final_larger = (INTVAL (final_value) > INTVAL (initial_value)) -
-      (INTVAL (final_value) < INTVAL (initial_value));
+    final_larger = (INTVAL (final_value) > INTVAL (initial_value))
+      (INTVAL (final_value) < INTVAL (initial_value));
 
   if (INTVAL (increment) > 0)
     increment_dir = 1;