OSDN Git Service

PR middle-end/22239
[pf3gnuchains/gcc-fork.git] / gcc / loop.c
index 222d492..ae06307 100644 (file)
@@ -1,6 +1,7 @@
 /* Perform various loop optimizations, including strength reduction.
-   Copyright (C) 1987, 1988, 1989, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
-   1998, 1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
+   Copyright (C) 1987, 1988, 1989, 1991, 1992, 1993, 1994, 1995,
+   1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
+   Free Software Foundation, Inc.
 
 This file is part of GCC.
 
@@ -16,14 +17,22 @@ for more details.
 
 You should have received a copy of the GNU General Public License
 along with GCC; see the file COPYING.  If not, write to the Free
-Software Foundation, 59 Temple Place - Suite 330, Boston, MA
-02111-1307, USA.  */
+Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301, USA.  */
 
 /* This is the loop optimization pass of the compiler.
    It finds invariant computations within loops and moves them
    to the beginning of the loop.  Then it identifies basic and
-   general induction variables.  Strength reduction is applied to the general
-   induction variables, and induction variable elimination is applied to
+   general induction variables.
+
+   Basic induction variables (BIVs) are a pseudo registers which are set within
+   a loop only by incrementing or decrementing its value.  General induction
+   variables (GIVs) are pseudo registers with a value which is a linear function
+   of a basic induction variable.  BIVs are recognized by `basic_induction_var';
+   GIVs by `general_induction_var'.
+
+   Once induction variables are identified, strength reduction is applied to the
+   general induction variables, and induction variable elimination is applied to
    the basic induction variables.
 
    It also finds cases where
@@ -49,7 +58,6 @@ Software Foundation, 59 Temple Place - Suite 330, Boston, MA
 #include "recog.h"
 #include "flags.h"
 #include "real.h"
-#include "loop.h"
 #include "cselib.h"
 #include "except.h"
 #include "toplev.h"
@@ -57,6 +65,356 @@ Software Foundation, 59 Temple Place - Suite 330, Boston, MA
 #include "insn-flags.h"
 #include "optabs.h"
 #include "cfgloop.h"
+#include "ggc.h"
+#include "timevar.h"
+#include "tree-pass.h"
+
+/* Get the loop info pointer of a loop.  */
+#define LOOP_INFO(LOOP) ((struct loop_info *) (LOOP)->aux)
+
+/* Get a pointer to the loop movables structure.  */
+#define LOOP_MOVABLES(LOOP) (&LOOP_INFO (LOOP)->movables)
+
+/* Get a pointer to the loop registers structure.  */
+#define LOOP_REGS(LOOP) (&LOOP_INFO (LOOP)->regs)
+
+/* Get a pointer to the loop induction variables structure.  */
+#define LOOP_IVS(LOOP) (&LOOP_INFO (LOOP)->ivs)
+
+/* Get the luid of an insn.  Catch the error of trying to reference the LUID
+   of an insn added during loop, since these don't have LUIDs.  */
+
+#define INSN_LUID(INSN)                        \
+  (gcc_assert (INSN_UID (INSN) < max_uid_for_loop), uid_luid[INSN_UID (INSN)])
+
+#define REGNO_FIRST_LUID(REGNO)                        \
+  (REGNO_FIRST_UID (REGNO) < max_uid_for_loop  \
+       ? uid_luid[REGNO_FIRST_UID (REGNO)]     \
+       : 0)
+#define REGNO_LAST_LUID(REGNO)                 \
+  (REGNO_LAST_UID (REGNO) < max_uid_for_loop   \
+       ? uid_luid[REGNO_LAST_UID (REGNO)]      \
+       : INT_MAX)
+
+/* A "basic induction variable" or biv is a pseudo reg that is set
+   (within this loop) only by incrementing or decrementing it.  */
+/* A "general induction variable" or giv is a pseudo reg whose
+   value is a linear function of a biv.  */
+
+/* Bivs are recognized by `basic_induction_var';
+   Givs by `general_induction_var'.  */
+
+/* An enum for the two different types of givs, those that are used
+   as memory addresses and those that are calculated into registers.  */
+enum g_types
+{
+  DEST_ADDR,
+  DEST_REG
+};
+
+
+/* A `struct induction' is created for every instruction that sets
+   an induction variable (either a biv or a giv).  */
+
+struct induction
+{
+  rtx insn;                    /* The insn that sets a biv or giv */
+  rtx new_reg;                 /* New register, containing strength reduced
+                                  version of this giv.  */
+  rtx src_reg;                 /* Biv from which this giv is computed.
+                                  (If this is a biv, then this is the biv.) */
+  enum g_types giv_type;       /* Indicate whether DEST_ADDR or DEST_REG */
+  rtx dest_reg;                        /* Destination register for insn: this is the
+                                  register which was the biv or giv.
+                                  For a biv, this equals src_reg.
+                                  For a DEST_ADDR type giv, this is 0.  */
+  rtx *location;               /* Place in the insn where this giv occurs.
+                                  If GIV_TYPE is DEST_REG, this is 0.  */
+                               /* For a biv, this is the place where add_val
+                                  was found.  */
+  enum machine_mode mode;      /* The mode of this biv or giv */
+  rtx mem;                     /* For DEST_ADDR, the memory object.  */
+  rtx mult_val;                        /* Multiplicative factor for src_reg.  */
+  rtx add_val;                 /* Additive constant for that product.  */
+  int benefit;                 /* Gain from eliminating this insn.  */
+  rtx final_value;             /* If the giv is used outside the loop, and its
+                                  final value could be calculated, it is put
+                                  here, and the giv is made replaceable.  Set
+                                  the giv to this value before the loop.  */
+  unsigned combined_with;      /* The number of givs this giv has been
+                                  combined with.  If nonzero, this giv
+                                  cannot combine with any other giv.  */
+  unsigned replaceable : 1;    /* 1 if we can substitute the strength-reduced
+                                  variable for the original variable.
+                                  0 means they must be kept separate and the
+                                  new one must be copied into the old pseudo
+                                  reg each time the old one is set.  */
+  unsigned not_replaceable : 1;        /* Used to prevent duplicating work.  This is
+                                  1 if we know that the giv definitely can
+                                  not be made replaceable, in which case we
+                                  don't bother checking the variable again
+                                  even if further info is available.
+                                  Both this and the above can be zero.  */
+  unsigned ignore : 1;         /* 1 prohibits further processing of giv */
+  unsigned always_computable : 1;/* 1 if this value is computable every
+                                   iteration.  */
+  unsigned always_executed : 1; /* 1 if this set occurs each iteration.  */
+  unsigned maybe_multiple : 1; /* Only used for a biv and  1 if this biv
+                                  update may be done multiple times per
+                                  iteration.  */
+  unsigned cant_derive : 1;    /* For giv's, 1 if this giv cannot derive
+                                  another giv.  This occurs in many cases
+                                  where a giv's lifetime spans an update to
+                                  a biv.  */
+  unsigned maybe_dead : 1;     /* 1 if this giv might be dead.  In that case,
+                                  we won't use it to eliminate a biv, it
+                                  would probably lose.  */
+  unsigned auto_inc_opt : 1;   /* 1 if this giv had its increment output next
+                                  to it to try to form an auto-inc address.  */
+  unsigned shared : 1;
+  unsigned no_const_addval : 1; /* 1 if add_val does not contain a const.  */
+  int lifetime;                        /* Length of life of this giv */
+  rtx derive_adjustment;       /* If nonzero, is an adjustment to be
+                                  subtracted from add_val when this giv
+                                  derives another.  This occurs when the
+                                  giv spans a biv update by incrementation.  */
+  rtx ext_dependent;           /* If nonzero, is a sign or zero extension
+                                  if a biv on which this giv is dependent.  */
+  struct induction *next_iv;   /* For givs, links together all givs that are
+                                  based on the same biv.  For bivs, links
+                                  together all biv entries that refer to the
+                                  same biv register.  */
+  struct induction *same;      /* For givs, if the giv has been combined with
+                                  another giv, this points to the base giv.
+                                  The base giv will have COMBINED_WITH nonzero.
+                                  For bivs, if the biv has the same LOCATION
+                                  than another biv, this points to the base
+                                  biv.  */
+  struct induction *same_insn; /* If there are multiple identical givs in
+                                  the same insn, then all but one have this
+                                  field set, and they all point to the giv
+                                  that doesn't have this field set.  */
+  rtx last_use;                        /* For a giv made from a biv increment, this is
+                                  a substitute for the lifetime information.  */
+};
+
+
+/* A `struct iv_class' is created for each biv.  */
+
+struct iv_class
+{
+  unsigned int regno;          /* Pseudo reg which is the biv.  */
+  int biv_count;               /* Number of insns setting this reg.  */
+  struct induction *biv;       /* List of all insns that set this reg.  */
+  int giv_count;               /* Number of DEST_REG givs computed from this
+                                  biv.  The resulting count is only used in
+                                  check_dbra_loop.  */
+  struct induction *giv;       /* List of all insns that compute a giv
+                                  from this reg.  */
+  int total_benefit;           /* Sum of BENEFITs of all those givs.  */
+  rtx initial_value;           /* Value of reg at loop start.  */
+  rtx initial_test;            /* Test performed on BIV before loop.  */
+  rtx final_value;             /* Value of reg at loop end, if known.  */
+  struct iv_class *next;       /* Links all class structures together.  */
+  rtx init_insn;               /* insn which initializes biv, 0 if none.  */
+  rtx init_set;                        /* SET of INIT_INSN, if any.  */
+  unsigned incremented : 1;    /* 1 if somewhere incremented/decremented */
+  unsigned eliminable : 1;     /* 1 if plausible candidate for
+                                   elimination.  */
+  unsigned nonneg : 1;         /* 1 if we added a REG_NONNEG note for
+                                   this.  */
+  unsigned reversed : 1;       /* 1 if we reversed the loop that this
+                                  biv controls.  */
+  unsigned all_reduced : 1;    /* 1 if all givs using this biv have
+                                   been reduced.  */
+};
+
+
+/* Definitions used by the basic induction variable discovery code.  */
+enum iv_mode
+{
+  UNKNOWN_INDUCT,
+  BASIC_INDUCT,
+  NOT_BASIC_INDUCT,
+  GENERAL_INDUCT
+};
+
+
+/* A `struct iv' is created for every register.  */
+
+struct iv
+{
+  enum iv_mode type;
+  union
+  {
+    struct iv_class *class;
+    struct induction *info;
+  } iv;
+};
+
+
+#define REG_IV_TYPE(ivs, n) ivs->regs[n].type
+#define REG_IV_INFO(ivs, n) ivs->regs[n].iv.info
+#define REG_IV_CLASS(ivs, n) ivs->regs[n].iv.class
+
+
+struct loop_ivs
+{
+  /* Indexed by register number, contains pointer to `struct
+     iv' if register is an induction variable.  */
+  struct iv *regs;
+
+  /* Size of regs array.  */
+  unsigned int n_regs;
+
+  /* The head of a list which links together (via the next field)
+     every iv class for the current loop.  */
+  struct iv_class *list;
+};
+
+
+typedef struct loop_mem_info
+{
+  rtx mem;      /* The MEM itself.  */
+  rtx reg;      /* Corresponding pseudo, if any.  */
+  int optimize; /* Nonzero if we can optimize access to this MEM.  */
+} loop_mem_info;
+
+
+
+struct loop_reg
+{
+  /* Number of times the reg is set during the loop being scanned.
+     During code motion, a negative value indicates a reg that has
+     been made a candidate; in particular -2 means that it is an
+     candidate that we know is equal to a constant and -1 means that
+     it is a candidate not known equal to a constant.  After code
+     motion, regs moved have 0 (which is accurate now) while the
+     failed candidates have the original number of times set.
+
+     Therefore, at all times, == 0 indicates an invariant register;
+     < 0 a conditionally invariant one.  */
+  int set_in_loop;
+
+  /* Original value of set_in_loop; same except that this value
+     is not set negative for a reg whose sets have been made candidates
+     and not set to 0 for a reg that is moved.  */
+  int n_times_set;
+
+  /* Contains the insn in which a register was used if it was used
+     exactly once; contains const0_rtx if it was used more than once.  */
+  rtx single_usage;
+
+  /* Nonzero indicates that the register cannot be moved or strength
+     reduced.  */
+  char may_not_optimize;
+
+  /* Nonzero means reg N has already been moved out of one loop.
+     This reduces the desire to move it out of another.  */
+  char moved_once;
+};
+
+
+struct loop_regs
+{
+  int num;                     /* Number of regs used in table.  */
+  int size;                    /* Size of table.  */
+  struct loop_reg *array;      /* Register usage info. array.  */
+  int multiple_uses;           /* Nonzero if a reg has multiple uses.  */
+};
+
+
+
+struct loop_movables
+{
+  /* Head of movable chain.  */
+  struct movable *head;
+  /* Last movable in chain.  */
+  struct movable *last;
+};
+
+
+/* Information pertaining to a loop.  */
+
+struct loop_info
+{
+  /* Nonzero if there is a subroutine call in the current loop.  */
+  int has_call;
+  /* Nonzero if there is a libcall in the current loop.  */
+  int has_libcall;
+  /* Nonzero if there is a non constant call in the current loop.  */
+  int has_nonconst_call;
+  /* Nonzero if there is a prefetch instruction in the current loop.  */
+  int has_prefetch;
+  /* Nonzero if there is a volatile memory reference in the current
+     loop.  */
+  int has_volatile;
+  /* Nonzero if there is a tablejump in the current loop.  */
+  int has_tablejump;
+  /* Nonzero if there are ways to leave the loop other than falling
+     off the end.  */
+  int has_multiple_exit_targets;
+  /* Nonzero if there is an indirect jump in the current function.  */
+  int has_indirect_jump;
+  /* Register or constant initial loop value.  */
+  rtx initial_value;
+  /* Register or constant value used for comparison test.  */
+  rtx comparison_value;
+  /* Register or constant approximate final value.  */
+  rtx final_value;
+  /* Register or constant initial loop value with term common to
+     final_value removed.  */
+  rtx initial_equiv_value;
+  /* Register or constant final loop value with term common to
+     initial_value removed.  */
+  rtx final_equiv_value;
+  /* Register corresponding to iteration variable.  */
+  rtx iteration_var;
+  /* Constant loop increment.  */
+  rtx increment;
+  enum rtx_code comparison_code;
+  /* Holds the number of loop iterations.  It is zero if the number
+     could not be calculated.  Must be unsigned since the number of
+     iterations can be as high as 2^wordsize - 1.  For loops with a
+     wider iterator, this number will be zero if the number of loop
+     iterations is too large for an unsigned integer to hold.  */
+  unsigned HOST_WIDE_INT n_iterations;
+  int used_count_register;
+  /* The loop iterator induction variable.  */
+  struct iv_class *iv;
+  /* List of MEMs that are stored in this loop.  */
+  rtx store_mems;
+  /* Array of MEMs that are used (read or written) in this loop, but
+     cannot be aliased by anything in this loop, except perhaps
+     themselves.  In other words, if mems[i] is altered during
+     the loop, it is altered by an expression that is rtx_equal_p to
+     it.  */
+  loop_mem_info *mems;
+  /* The index of the next available slot in MEMS.  */
+  int mems_idx;
+  /* The number of elements allocated in MEMS.  */
+  int mems_allocated;
+  /* Nonzero if we don't know what MEMs were changed in the current
+     loop.  This happens if the loop contains a call (in which case
+     `has_call' will also be set) or if we store into more than
+     NUM_STORES MEMs.  */
+  int unknown_address_altered;
+  /* The above doesn't count any readonly memory locations that are
+     stored.  This does.  */
+  int unknown_constant_address_altered;
+  /* Count of memory write instructions discovered in the loop.  */
+  int num_mem_sets;
+  /* The insn where the first of these was found.  */
+  rtx first_loop_store_insn;
+  /* The chain of movable insns in loop.  */
+  struct loop_movables movables;
+  /* The registers used the in loop.  */
+  struct loop_regs regs;
+  /* The induction variable information in loop.  */
+  struct loop_ivs ivs;
+  /* Nonzero if call is in pre_header extended basic block.  */
+  int pre_header_has_call;
+};
 
 /* Not really meaningful values, but at least something.  */
 #ifndef SIMULTANEOUS_PREFETCHES
@@ -68,10 +426,10 @@ Software Foundation, 59 Temple Place - Suite 330, Boston, MA
 #ifndef HAVE_prefetch
 #define HAVE_prefetch 0
 #define CODE_FOR_prefetch 0
-#define gen_prefetch(a,b,c) (abort(), NULL_RTX)
+#define gen_prefetch(a,b,c) (gcc_unreachable (), NULL_RTX)
 #endif
 
-/* Give up the prefetch optimizations once we exceed a given threshhold.
+/* Give up the prefetch optimizations once we exceed a given threshold.
    It is unlikely that we would be able to optimize something in a loop
    with so many detected prefetches.  */
 #define MAX_PREFETCHES 100
@@ -154,23 +512,23 @@ Software Foundation, 59 Temple Place - Suite 330, Boston, MA
 
 #define LOOP_REGNO_NREGS(REGNO, SET_DEST) \
 ((REGNO) < FIRST_PSEUDO_REGISTER \
- ? (int) HARD_REGNO_NREGS ((REGNO), GET_MODE (SET_DEST)) : 1)
+ ? (int) hard_regno_nregs[(REGNO)][GET_MODE (SET_DEST)] : 1)
 
 
 /* Vector mapping INSN_UIDs to luids.
    The luids are like uids but increase monotonically always.
    We use them to see whether a jump comes from outside a given loop.  */
 
-int *uid_luid;
+static int *uid_luid;
 
 /* Indexed by INSN_UID, contains the ordinal giving the (innermost) loop
    number the insn is contained in.  */
 
-struct loop **uid_loop;
+static struct loop **uid_loop;
 
 /* 1 + largest uid of any insn.  */
 
-int max_uid_for_loop;
+static int max_uid_for_loop;
 
 /* Number of loops detected in current function.  Used as index to the
    next few tables.  */
@@ -179,7 +537,7 @@ static int max_loop_num;
 
 /* Bound on pseudo register number before loop optimization.
    A pseudo has valid regscan info if its number is < max_reg_before_loop.  */
-unsigned int max_reg_before_loop;
+static unsigned int max_reg_before_loop;
 
 /* The value to pass to the next call of reg_scan_update.  */
 static int loop_max_reg;
@@ -204,7 +562,7 @@ struct movable
   short savings;               /* Number of insns we can move for this reg,
                                   including other movables that force this
                                   or match this one.  */
-  ENUM_BITFIELD(machine_mode) savemode : 8;   /* Nonzero means it is a mode for 
+  ENUM_BITFIELD(machine_mode) savemode : 8;   /* Nonzero means it is a mode for
                                   a low part that we should avoid changing when
                                   clearing the rest of the reg.  */
   unsigned int cond : 1;       /* 1 if only conditionally movable */
@@ -225,149 +583,154 @@ struct movable
   unsigned int is_equiv : 1;   /* 1 means a REG_EQUIV is present on INSN.  */
   unsigned int insert_temp : 1;  /* 1 means we copy to a new pseudo and replace
                                    the original insn with a copy from that
-                                   pseudo, rather than deleting it. */
+                                   pseudo, rather than deleting it.  */
   struct movable *match;       /* First entry for same value */
   struct movable *forces;      /* An insn that must be moved if this is */
   struct movable *next;
 };
 
 
-FILE *loop_dump_stream;
+static FILE *loop_dump_stream;
 
 /* Forward declarations.  */
 
-static void invalidate_loops_containing_label PARAMS ((rtx));
-static void find_and_verify_loops PARAMS ((rtx, struct loops *));
-static void mark_loop_jump PARAMS ((rtx, struct loop *));
-static void prescan_loop PARAMS ((struct loop *));
-static int reg_in_basic_block_p PARAMS ((rtx, rtx));
-static int consec_sets_invariant_p PARAMS ((const struct loop *,
-                                           rtx, int, rtx));
-static int labels_in_range_p PARAMS ((rtx, int));
-static void count_one_set PARAMS ((struct loop_regs *, rtx, rtx, rtx *));
-static void note_addr_stored PARAMS ((rtx, rtx, void *));
-static void note_set_pseudo_multiple_uses PARAMS ((rtx, rtx, void *));
-static int loop_reg_used_before_p PARAMS ((const struct loop *, rtx, rtx));
-static void scan_loop PARAMS ((struct loop*, int));
+static void invalidate_loops_containing_label (rtx);
+static void find_and_verify_loops (rtx, struct loops *);
+static void mark_loop_jump (rtx, struct loop *);
+static void prescan_loop (struct loop *);
+static int reg_in_basic_block_p (rtx, rtx);
+static int consec_sets_invariant_p (const struct loop *, rtx, int, rtx);
+static int labels_in_range_p (rtx, int);
+static void count_one_set (struct loop_regs *, rtx, rtx, rtx *);
+static void note_addr_stored (rtx, rtx, void *);
+static void note_set_pseudo_multiple_uses (rtx, rtx, void *);
+static int loop_reg_used_before_p (const struct loop *, rtx, rtx);
+static rtx find_regs_nested (rtx, rtx);
+static void scan_loop (struct loop*, int);
 #if 0
-static void replace_call_address PARAMS ((rtx, rtx, rtx));
+static void replace_call_address (rtx, rtx, rtx);
 #endif
-static rtx skip_consec_insns PARAMS ((rtx, int));
-static int libcall_benefit PARAMS ((rtx));
-static void ignore_some_movables PARAMS ((struct loop_movables *));
-static void force_movables PARAMS ((struct loop_movables *));
-static void combine_movables PARAMS ((struct loop_movables *,
-                                     struct loop_regs *));
-static int num_unmoved_movables PARAMS ((const struct loop *));
-static int regs_match_p PARAMS ((rtx, rtx, struct loop_movables *));
-static int rtx_equal_for_loop_p PARAMS ((rtx, rtx, struct loop_movables *,
-                                        struct loop_regs *));
-static void add_label_notes PARAMS ((rtx, rtx));
-static void move_movables PARAMS ((struct loop *loop, struct loop_movables *,
-                                  int, int));
-static void loop_movables_add PARAMS((struct loop_movables *,
-                                     struct movable *));
-static void loop_movables_free PARAMS((struct loop_movables *));
-static int count_nonfixed_reads PARAMS ((const struct loop *, rtx));
-static void loop_bivs_find PARAMS((struct loop *));
-static void loop_bivs_init_find PARAMS((struct loop *));
-static void loop_bivs_check PARAMS((struct loop *));
-static void loop_givs_find PARAMS((struct loop *));
-static void loop_givs_check PARAMS((struct loop *));
-static int loop_biv_eliminable_p PARAMS((struct loop *, struct iv_class *,
-                                        int, int));
-static int loop_giv_reduce_benefit PARAMS((struct loop *, struct iv_class *,
-                                          struct induction *, rtx));
-static void loop_givs_dead_check PARAMS((struct loop *, struct iv_class *));
-static void loop_givs_reduce PARAMS((struct loop *, struct iv_class *));
-static void loop_givs_rescan PARAMS((struct loop *, struct iv_class *,
-                                    rtx *));
-static void loop_ivs_free PARAMS((struct loop *));
-static void strength_reduce PARAMS ((struct loop *, int));
-static void find_single_use_in_loop PARAMS ((struct loop_regs *, rtx, rtx));
-static int valid_initial_value_p PARAMS ((rtx, rtx, int, rtx));
-static void find_mem_givs PARAMS ((const struct loop *, rtx, rtx, int, int));
-static void record_biv PARAMS ((struct loop *, struct induction *,
-                               rtx, rtx, rtx, rtx, rtx *,
-                               int, int));
-static void check_final_value PARAMS ((const struct loop *,
-                                      struct induction *));
-static void loop_ivs_dump PARAMS((const struct loop *, FILE *, int));
-static void loop_iv_class_dump PARAMS((const struct iv_class *, FILE *, int));
-static void loop_biv_dump PARAMS((const struct induction *, FILE *, int));
-static void loop_giv_dump PARAMS((const struct induction *, FILE *, int));
-static void record_giv PARAMS ((const struct loop *, struct induction *,
-                               rtx, rtx, rtx, rtx, rtx, rtx, int,
-                               enum g_types, int, int, rtx *));
-static void update_giv_derive PARAMS ((const struct loop *, rtx));
-static void check_ext_dependent_givs PARAMS ((struct iv_class *,
-                                             struct loop_info *));
-static int basic_induction_var PARAMS ((const struct loop *, rtx,
-                                       enum machine_mode, rtx, rtx,
-                                       rtx *, rtx *, rtx **));
-static rtx simplify_giv_expr PARAMS ((const struct loop *, rtx, rtx *, int *));
-static int general_induction_var PARAMS ((const struct loop *loop, rtx, rtx *,
-                                         rtx *, rtx *, rtx *, int, int *,
-                                         enum machine_mode));
-static int consec_sets_giv PARAMS ((const struct loop *, int, rtx,
-                                   rtx, rtx, rtx *, rtx *, rtx *, rtx *));
-static int check_dbra_loop PARAMS ((struct loop *, int));
-static rtx express_from_1 PARAMS ((rtx, rtx, rtx));
-static rtx combine_givs_p PARAMS ((struct induction *, struct induction *));
-static int cmp_combine_givs_stats PARAMS ((const PTR, const PTR));
-static void combine_givs PARAMS ((struct loop_regs *, struct iv_class *));
-static int product_cheap_p PARAMS ((rtx, rtx));
-static int maybe_eliminate_biv PARAMS ((const struct loop *, struct iv_class *,
-                                       int, int, int));
-static int maybe_eliminate_biv_1 PARAMS ((const struct loop *, rtx, rtx,
-                                         struct iv_class *, int,
-                                         basic_block, rtx));
-static int last_use_this_basic_block PARAMS ((rtx, rtx));
-static void record_initial PARAMS ((rtx, rtx, void *));
-static void update_reg_last_use PARAMS ((rtx, rtx));
-static rtx next_insn_in_loop PARAMS ((const struct loop *, rtx));
-static void loop_regs_scan PARAMS ((const struct loop *, int));
-static int count_insns_in_loop PARAMS ((const struct loop *));
-static void load_mems PARAMS ((const struct loop *));
-static int insert_loop_mem PARAMS ((rtx *, void *));
-static int replace_loop_mem PARAMS ((rtx *, void *));
-static void replace_loop_mems PARAMS ((rtx, rtx, rtx));
-static int replace_loop_reg PARAMS ((rtx *, void *));
-static void replace_loop_regs PARAMS ((rtx insn, rtx, rtx));
-static void note_reg_stored PARAMS ((rtx, rtx, void *));
-static void try_copy_prop PARAMS ((const struct loop *, rtx, unsigned int));
-static void try_swap_copy_prop PARAMS ((const struct loop *, rtx,
-                                        unsigned int));
-static int replace_label PARAMS ((rtx *, void *));
-static rtx check_insn_for_givs PARAMS((struct loop *, rtx, int, int));
-static rtx check_insn_for_bivs PARAMS((struct loop *, rtx, int, int));
-static rtx gen_add_mult PARAMS ((rtx, rtx, rtx, rtx));
-static void loop_regs_update PARAMS ((const struct loop *, rtx));
-static int iv_add_mult_cost PARAMS ((rtx, rtx, rtx, rtx));
-
-static rtx loop_insn_emit_after PARAMS((const struct loop *, basic_block,
-                                       rtx, rtx));
-static rtx loop_call_insn_emit_before PARAMS((const struct loop *,
-                                             basic_block, rtx, rtx));
-static rtx loop_call_insn_hoist PARAMS((const struct loop *, rtx));
-static rtx loop_insn_sink_or_swim PARAMS((const struct loop *, rtx));
-
-static void loop_dump_aux PARAMS ((const struct loop *, FILE *, int));
-static void loop_delete_insns PARAMS ((rtx, rtx));
-static HOST_WIDE_INT remove_constant_addition PARAMS ((rtx *));
-static rtx gen_load_of_final_value PARAMS ((rtx, rtx));
-void debug_ivs PARAMS ((const struct loop *));
-void debug_iv_class PARAMS ((const struct iv_class *));
-void debug_biv PARAMS ((const struct induction *));
-void debug_giv PARAMS ((const struct induction *));
-void debug_loop PARAMS ((const struct loop *));
-void debug_loops PARAMS ((const struct loops *));
-
-typedef struct rtx_pair
-{
-  rtx r1;
-  rtx r2;
-} rtx_pair;
+static rtx skip_consec_insns (rtx, int);
+static int libcall_benefit (rtx);
+static rtx libcall_other_reg (rtx, rtx);
+static void record_excess_regs (rtx, rtx, rtx *);
+static void ignore_some_movables (struct loop_movables *);
+static void force_movables (struct loop_movables *);
+static void combine_movables (struct loop_movables *, struct loop_regs *);
+static int num_unmoved_movables (const struct loop *);
+static int regs_match_p (rtx, rtx, struct loop_movables *);
+static int rtx_equal_for_loop_p (rtx, rtx, struct loop_movables *,
+                                struct loop_regs *);
+static void add_label_notes (rtx, rtx);
+static void move_movables (struct loop *loop, struct loop_movables *, int,
+                          int);
+static void loop_movables_add (struct loop_movables *, struct movable *);
+static void loop_movables_free (struct loop_movables *);
+static int count_nonfixed_reads (const struct loop *, rtx);
+static void loop_bivs_find (struct loop *);
+static void loop_bivs_init_find (struct loop *);
+static void loop_bivs_check (struct loop *);
+static void loop_givs_find (struct loop *);
+static void loop_givs_check (struct loop *);
+static int loop_biv_eliminable_p (struct loop *, struct iv_class *, int, int);
+static int loop_giv_reduce_benefit (struct loop *, struct iv_class *,
+                                   struct induction *, rtx);
+static void loop_givs_dead_check (struct loop *, struct iv_class *);
+static void loop_givs_reduce (struct loop *, struct iv_class *);
+static void loop_givs_rescan (struct loop *, struct iv_class *, rtx *);
+static void loop_ivs_free (struct loop *);
+static void strength_reduce (struct loop *, int);
+static void find_single_use_in_loop (struct loop_regs *, rtx, rtx);
+static int valid_initial_value_p (rtx, rtx, int, rtx);
+static void find_mem_givs (const struct loop *, rtx, rtx, int, int);
+static void record_biv (struct loop *, struct induction *, rtx, rtx, rtx,
+                       rtx, rtx *, int, int);
+static void check_final_value (const struct loop *, struct induction *);
+static void loop_ivs_dump (const struct loop *, FILE *, int);
+static void loop_iv_class_dump (const struct iv_class *, FILE *, int);
+static void loop_biv_dump (const struct induction *, FILE *, int);
+static void loop_giv_dump (const struct induction *, FILE *, int);
+static void record_giv (const struct loop *, struct induction *, rtx, rtx,
+                       rtx, rtx, rtx, rtx, int, enum g_types, int, int,
+                       rtx *);
+static void update_giv_derive (const struct loop *, rtx);
+static HOST_WIDE_INT get_monotonic_increment (struct iv_class *);
+static bool biased_biv_fits_mode_p (const struct loop *, struct iv_class *,
+                                   HOST_WIDE_INT, enum machine_mode,
+                                   unsigned HOST_WIDE_INT);
+static bool biv_fits_mode_p (const struct loop *, struct iv_class *,
+                            HOST_WIDE_INT, enum machine_mode, bool);
+static bool extension_within_bounds_p (const struct loop *, struct iv_class *,
+                                      HOST_WIDE_INT, rtx);
+static void check_ext_dependent_givs (const struct loop *, struct iv_class *);
+static int basic_induction_var (const struct loop *, rtx, enum machine_mode,
+                               rtx, rtx, rtx *, rtx *, rtx **);
+static rtx simplify_giv_expr (const struct loop *, rtx, rtx *, int *);
+static int general_induction_var (const struct loop *loop, rtx, rtx *, rtx *,
+                                 rtx *, rtx *, int, int *, enum machine_mode);
+static int consec_sets_giv (const struct loop *, int, rtx, rtx, rtx, rtx *,
+                           rtx *, rtx *, rtx *);
+static int check_dbra_loop (struct loop *, int);
+static rtx express_from_1 (rtx, rtx, rtx);
+static rtx combine_givs_p (struct induction *, struct induction *);
+static int cmp_combine_givs_stats (const void *, const void *);
+static void combine_givs (struct loop_regs *, struct iv_class *);
+static int product_cheap_p (rtx, rtx);
+static int maybe_eliminate_biv (const struct loop *, struct iv_class *, int,
+                               int, int);
+static int maybe_eliminate_biv_1 (const struct loop *, rtx, rtx,
+                                 struct iv_class *, int, basic_block, rtx);
+static int last_use_this_basic_block (rtx, rtx);
+static void record_initial (rtx, rtx, void *);
+static void update_reg_last_use (rtx, rtx);
+static rtx next_insn_in_loop (const struct loop *, rtx);
+static void loop_regs_scan (const struct loop *, int);
+static int count_insns_in_loop (const struct loop *);
+static int find_mem_in_note_1 (rtx *, void *);
+static rtx find_mem_in_note (rtx);
+static void load_mems (const struct loop *);
+static int insert_loop_mem (rtx *, void *);
+static int replace_loop_mem (rtx *, void *);
+static void replace_loop_mems (rtx, rtx, rtx, int);
+static int replace_loop_reg (rtx *, void *);
+static void replace_loop_regs (rtx insn, rtx, rtx);
+static void note_reg_stored (rtx, rtx, void *);
+static void try_copy_prop (const struct loop *, rtx, unsigned int);
+static void try_swap_copy_prop (const struct loop *, rtx, unsigned int);
+static rtx check_insn_for_givs (struct loop *, rtx, int, int);
+static rtx check_insn_for_bivs (struct loop *, rtx, int, int);
+static rtx gen_add_mult (rtx, rtx, rtx, rtx);
+static void loop_regs_update (const struct loop *, rtx);
+static int iv_add_mult_cost (rtx, rtx, rtx, rtx);
+static int loop_invariant_p (const struct loop *, rtx);
+static rtx loop_insn_hoist (const struct loop *, rtx);
+static void loop_iv_add_mult_emit_before (const struct loop *, rtx, rtx, rtx,
+                                         rtx, basic_block, rtx);
+static rtx loop_insn_emit_before (const struct loop *, basic_block,
+                                 rtx, rtx);
+static int loop_insn_first_p (rtx, rtx);
+static rtx get_condition_for_loop (const struct loop *, rtx);
+static void loop_iv_add_mult_sink (const struct loop *, rtx, rtx, rtx, rtx);
+static void loop_iv_add_mult_hoist (const struct loop *, rtx, rtx, rtx, rtx);
+static rtx extend_value_for_giv (struct induction *, rtx);
+static rtx loop_insn_sink (const struct loop *, rtx);
+
+static rtx loop_insn_emit_after (const struct loop *, basic_block, rtx, rtx);
+static rtx loop_call_insn_emit_before (const struct loop *, basic_block,
+                                      rtx, rtx);
+static rtx loop_call_insn_hoist (const struct loop *, rtx);
+static rtx loop_insn_sink_or_swim (const struct loop *, rtx);
+
+static void loop_dump_aux (const struct loop *, FILE *, int);
+static void loop_delete_insns (rtx, rtx);
+static HOST_WIDE_INT remove_constant_addition (rtx *);
+static rtx gen_load_of_final_value (rtx, rtx);
+void debug_ivs (const struct loop *);
+void debug_iv_class (const struct iv_class *);
+void debug_biv (const struct induction *);
+void debug_giv (const struct induction *);
+void debug_loop (const struct loop *);
+void debug_loops (const struct loops *);
 
 typedef struct loop_replace_args
 {
@@ -384,13 +747,12 @@ typedef struct loop_replace_args
 
 /* Indirect_jump_in_function is computed once per function.  */
 static int indirect_jump_in_function;
-static int indirect_jump_in_function_p PARAMS ((rtx));
+static int indirect_jump_in_function_p (rtx);
 
-static int compute_luids PARAMS ((rtx, rtx, int));
+static int compute_luids (rtx, rtx, int);
 
-static int biv_elimination_giv_has_0_offset PARAMS ((struct induction *,
-                                                    struct induction *,
-                                                    rtx));
+static int biv_elimination_giv_has_0_offset (struct induction *,
+                                            struct induction *, rtx);
 \f
 /* Benefit penalty, if a giv is not replaceable, i.e. must emit an insn to
    copy the value of the strength reduced giv to its original register.  */
@@ -400,7 +762,7 @@ static int copy_cost;
 static int reg_address_cost;
 
 void
-init_loop ()
+init_loop (void)
 {
   rtx reg = gen_rtx_REG (word_mode, LAST_VIRTUAL_REGISTER + 1);
 
@@ -415,9 +777,7 @@ init_loop ()
    Start at insn START and stop just before END.  Assign LUIDs
    starting with PREV_LUID + 1.  Return the last assigned LUID + 1.  */
 static int
-compute_luids (start, end, prev_luid)
-     rtx start, end;
-     int prev_luid;
+compute_luids (rtx start, rtx end, int prev_luid)
 {
   int i;
   rtx insn;
@@ -428,7 +788,7 @@ compute_luids (start, end, prev_luid)
        continue;
       /* Don't assign luids to line-number NOTEs, so that the distance in
         luids between two insns is not affected by -g.  */
-      if (GET_CODE (insn) != NOTE
+      if (!NOTE_P (insn)
          || NOTE_LINE_NUMBER (insn) <= 0)
        uid_luid[INSN_UID (insn)] = ++i;
       else
@@ -444,11 +804,7 @@ compute_luids (start, end, prev_luid)
    (or 0 if none should be output).  */
 
 void
-loop_optimize (f, dumpfile, flags)
-     /* f is the first instruction of a chain of insns for one function */
-     rtx f;
-     FILE *dumpfile;
-     int flags;
+loop_optimize (rtx f, FILE *dumpfile, int flags)
 {
   rtx insn;
   int i;
@@ -470,7 +826,7 @@ loop_optimize (f, dumpfile, flags)
   max_loop_num = 0;
   for (insn = f; insn; insn = NEXT_INSN (insn))
     {
-      if (GET_CODE (insn) == NOTE
+      if (NOTE_P (insn)
          && NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
        max_loop_num++;
     }
@@ -485,13 +841,11 @@ loop_optimize (f, dumpfile, flags)
      Leave some space for labels allocated by find_and_verify_loops.  */
   max_uid_for_loop = get_max_uid () + 1 + max_loop_num * 32;
 
-  uid_luid = (int *) xcalloc (max_uid_for_loop, sizeof (int));
-  uid_loop = (struct loop **) xcalloc (max_uid_for_loop,
-                                      sizeof (struct loop *));
+  uid_luid = xcalloc (max_uid_for_loop, sizeof (int));
+  uid_loop = xcalloc (max_uid_for_loop, sizeof (struct loop *));
 
   /* Allocate storage for array of loops.  */
-  loops->array = (struct loop *)
-    xcalloc (loops->num, sizeof (struct loop));
+  loops->array = xcalloc (loops->num, sizeof (struct loop));
 
   /* Find and process each loop.
      First, find them, and record them in order of their beginnings.  */
@@ -505,7 +859,7 @@ loop_optimize (f, dumpfile, flags)
   /* Now find all register lifetimes.  This must be done after
      find_and_verify_loops, because it might reorder the insns in the
      function.  */
-  reg_scan (f, max_reg_before_loop, 1);
+  reg_scan (f, max_reg_before_loop);
 
   /* This must occur after reg_scan so that registers created by gcse
      will have entries in the register tables.
@@ -516,8 +870,7 @@ loop_optimize (f, dumpfile, flags)
 
   /* See if we went too far.  Note that get_max_uid already returns
      one more that the maximum uid of all insn.  */
-  if (get_max_uid () > max_uid_for_loop)
-    abort ();
+  gcc_assert (get_max_uid () <= max_uid_for_loop);
   /* Now reset it to the actual size we need.  See above.  */
   max_uid_for_loop = get_max_uid ();
 
@@ -552,12 +905,18 @@ loop_optimize (f, dumpfile, flags)
       struct loop *loop = &loops->array[i];
 
       if (! loop->invalid && loop->end)
-       scan_loop (loop, flags);
+       {
+         scan_loop (loop, flags);
+         ggc_collect ();
+       }
     }
 
   end_alias_analysis ();
 
   /* Clean up.  */
+  for (i = 0; i < (int) loops->num; i++)
+    free (loops_info[i].mems);
+  
   free (uid_luid);
   free (uid_loop);
   free (loops_info);
@@ -571,9 +930,7 @@ loop_optimize (f, dumpfile, flags)
    bottom.  */
 
 static rtx
-next_insn_in_loop (loop, insn)
-     const struct loop *loop;
-     rtx insn;
+next_insn_in_loop (const struct loop *loop, rtx insn)
 {
   insn = NEXT_INSN (insn);
 
@@ -594,6 +951,32 @@ next_insn_in_loop (loop, insn)
   return insn;
 }
 
+/* Find any register references hidden inside X and add them to
+   the dependency list DEPS.  This is used to look inside CLOBBER (MEM
+   when checking whether a PARALLEL can be pulled out of a loop.  */
+
+static rtx
+find_regs_nested (rtx deps, rtx x)
+{
+  enum rtx_code code = GET_CODE (x);
+  if (code == REG)
+    deps = gen_rtx_EXPR_LIST (VOIDmode, x, deps);
+  else
+    {
+      const char *fmt = GET_RTX_FORMAT (code);
+      int i, j;
+      for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
+       {
+         if (fmt[i] == 'e')
+           deps = find_regs_nested (deps, XEXP (x, i));
+         else if (fmt[i] == 'E')
+           for (j = 0; j < XVECLEN (x, i); j++)
+             deps = find_regs_nested (deps, XVECEXP (x, i, j));
+       }
+    }
+  return deps;
+}
+
 /* Optimize one loop described by LOOP.  */
 
 /* ??? Could also move memory writes out of loops if the destination address
@@ -603,9 +986,7 @@ next_insn_in_loop (loop, insn)
    write, then we can also mark the memory read as invariant.  */
 
 static void
-scan_loop (loop, flags)
-     struct loop *loop;
-     int flags;
+scan_loop (struct loop *loop, int flags)
 {
   struct loop_info *loop_info = LOOP_INFO (loop);
   struct loop_regs *regs = LOOP_REGS (loop);
@@ -631,8 +1012,6 @@ scan_loop (loop, flags)
      since in that case saving an insn makes more difference
      and more registers are available.  */
   int threshold;
-  /* Nonzero if we are scanning instructions in a sub-loop.  */
-  int loop_depth = 0;
   int in_libcall;
 
   loop->top = 0;
@@ -658,8 +1037,8 @@ scan_loop (loop, flags)
 
   for (p = NEXT_INSN (loop_start);
        p != loop_end
-        && GET_CODE (p) != CODE_LABEL && ! INSN_P (p)
-        && (GET_CODE (p) != NOTE
+        && !LABEL_P (p) && ! INSN_P (p)
+        && (!NOTE_P (p)
             || (NOTE_LINE_NUMBER (p) != NOTE_INSN_LOOP_BEG
                 && NOTE_LINE_NUMBER (p) != NOTE_INSN_LOOP_END));
        p = NEXT_INSN (p))
@@ -685,7 +1064,7 @@ scan_loop (loop, flags)
      Start scan from there.
      But record in LOOP->TOP the place where the end-test jumps
      back to so we can scan that after the end of the loop.  */
-  if (GET_CODE (p) == JUMP_INSN
+  if (JUMP_P (p)
       /* Loop entry must be unconditional jump (and not a RETURN)  */
       && any_uncondjump_p (p)
       && JUMP_LABEL (p) != 0
@@ -710,7 +1089,7 @@ scan_loop (loop, flags)
      test above.  */
 
   if (INSN_UID (loop->scan_start) >= max_uid_for_loop
-      || GET_CODE (loop->scan_start) != CODE_LABEL)
+      || !LABEL_P (loop->scan_start))
     {
       if (loop_dump_stream)
        fprintf (loop_dump_stream, "\nLoop from %d to %d is phony.\n\n",
@@ -725,13 +1104,8 @@ scan_loop (loop, flags)
   insn_count = count_insns_in_loop (loop);
 
   if (loop_dump_stream)
-    {
-      fprintf (loop_dump_stream, "\nLoop from %d to %d: %d real insns.\n",
-              INSN_UID (loop_start), INSN_UID (loop_end), insn_count);
-      if (loop->cont)
-       fprintf (loop_dump_stream, "Continue at insn %d.\n",
-                INSN_UID (loop->cont));
-    }
+    fprintf (loop_dump_stream, "\nLoop from %d to %d: %d real insns.\n",
+            INSN_UID (loop_start), INSN_UID (loop_end), insn_count);
 
   /* Scan through the loop finding insns that are safe to move.
      Set REGS->ARRAY[I].SET_IN_LOOP negative for the reg I being set, so that
@@ -752,14 +1126,18 @@ scan_loop (loop, flags)
     {
       if (in_libcall && INSN_P (p) && find_reg_note (p, REG_RETVAL, NULL_RTX))
        in_libcall--;
-      if (GET_CODE (p) == INSN)
+      if (NONJUMP_INSN_P (p))
        {
+         /* Do not scan past an optimization barrier.  */
+         if (GET_CODE (PATTERN (p)) == ASM_INPUT)
+           break;
          temp = find_reg_note (p, REG_LIBCALL, NULL_RTX);
          if (temp)
            in_libcall++;
          if (! in_libcall
              && (set = single_set (p))
-             && GET_CODE (SET_DEST (set)) == REG
+             && REG_P (SET_DEST (set))
+             && SET_DEST (set) != frame_pointer_rtx
 #ifdef PIC_OFFSET_TABLE_REG_CALL_CLOBBERED
              && SET_DEST (set) != pic_offset_table_rtx
 #endif
@@ -799,7 +1177,9 @@ scan_loop (loop, flags)
                }
 
              /* For parallels, add any possible uses to the dependencies, as
-                we can't move the insn without resolving them first.  */
+                we can't move the insn without resolving them first.
+                MEMs inside CLOBBERs may also reference registers; these
+                count as implicit uses.  */
              if (GET_CODE (PATTERN (p)) == PARALLEL)
                {
                  for (i = 0; i < XVECLEN (PATTERN (p), 0); i++)
@@ -809,6 +1189,10 @@ scan_loop (loop, flags)
                        dependencies
                          = gen_rtx_EXPR_LIST (VOIDmode, XEXP (x, 0),
                                               dependencies);
+                     else if (GET_CODE (x) == CLOBBER 
+                              && MEM_P (XEXP (x, 0)))
+                       dependencies = find_regs_nested (dependencies, 
+                                                 XEXP (XEXP (x, 0), 0));
                    }
                }
 
@@ -829,7 +1213,7 @@ scan_loop (loop, flags)
                   && (maybe_never
                       || loop_reg_used_before_p (loop, set, p)))
                /* It is unsafe to move the set.  However, it may be OK to
-                  move the source into a new psuedo, and subsitute a 
+                  move the source into a new pseudo, and substitute a
                   reg-to-reg copy for the original insn.
 
                   This code used to consider it OK to move a set of a variable
@@ -838,17 +1222,27 @@ scan_loop (loop, flags)
                   That behavior is incorrect and was removed.  */
                insert_temp = 1;
 
+             /* Don't try to optimize a MODE_CC set with a constant
+                source.  It probably will be combined with a conditional
+                jump.  */
+             if (GET_MODE_CLASS (GET_MODE (SET_DEST (set))) == MODE_CC
+                 && CONSTANT_P (src))
+               ;
              /* Don't try to optimize a register that was made
                 by loop-optimization for an inner loop.
                 We don't know its life-span, so we can't compute
                 the benefit.  */
-             if (REGNO (SET_DEST (set)) >= max_reg_before_loop)
+             else if (REGNO (SET_DEST (set)) >= max_reg_before_loop)
                ;
-             /* Don't move the source and add a reg-to-reg copy with -Os
-                (this certainly increases size) or if the source is
-                already a reg (the motion will gain nothing). */
-             else if (insert_temp 
-                      && (optimize_size || GET_CODE (SET_SRC (set)) == REG
+             /* Don't move the source and add a reg-to-reg copy:
+                - with -Os (this certainly increases size),
+                - if the mode doesn't support copy operations (obviously),
+                - if the source is already a reg (the motion will gain nothing),
+                - if the source is a legitimate constant (likewise).  */
+             else if (insert_temp
+                      && (optimize_size
+                          || ! can_copy_p (GET_MODE (SET_SRC (set)))
+                          || REG_P (SET_SRC (set))
                           || (CONSTANT_P (SET_SRC (set))
                               && LEGITIMATE_CONSTANT_P (SET_SRC (set)))))
                ;
@@ -899,9 +1293,10 @@ scan_loop (loop, flags)
                      && ! side_effects_p (SET_SRC (set))
                      && ! find_reg_note (p, REG_RETVAL, NULL_RTX)
                      && (! SMALL_REGISTER_CLASSES
-                         || (! (GET_CODE (SET_SRC (set)) == REG
+                         || (! (REG_P (SET_SRC (set))
                                 && (REGNO (SET_SRC (set))
                                     < FIRST_PSEUDO_REGISTER))))
+                     && regno >= FIRST_PSEUDO_REGISTER 
                      /* This test is not redundant; SET_SRC (set) might be
                         a call-clobbered register and the life of REGNO
                         might span a call.  */
@@ -927,7 +1322,7 @@ scan_loop (loop, flags)
                      continue;
                    }
 
-                 m = (struct movable *) xmalloc (sizeof (struct movable));
+                 m = xmalloc (sizeof (struct movable));
                  m->next = 0;
                  m->insn = p;
                  m->set_src = src;
@@ -1002,7 +1397,7 @@ scan_loop (loop, flags)
                 Also, if the value loaded into the register
                 depends on the same register, this cannot be done.  */
              else if (SET_SRC (set) == const0_rtx
-                      && GET_CODE (NEXT_INSN (p)) == INSN
+                      && NONJUMP_INSN_P (NEXT_INSN (p))
                       && (set1 = single_set (NEXT_INSN (p)))
                       && GET_CODE (set1) == SET
                       && (GET_CODE (SET_DEST (set1)) == STRICT_LOW_PART)
@@ -1015,7 +1410,7 @@ scan_loop (loop, flags)
                  if (regs->array[regno].set_in_loop == 2)
                    {
                      struct movable *m;
-                     m = (struct movable *) xmalloc (sizeof (struct movable));
+                     m = xmalloc (sizeof (struct movable));
                      m->next = 0;
                      m->insn = p;
                      m->set_dest = SET_DEST (set);
@@ -1072,7 +1467,7 @@ scan_loop (loop, flags)
       /* Past a call insn, we get to insns which might not be executed
         because the call might exit.  This matters for insns that trap.
         Constant and pure call insns always return, so they don't count.  */
-      else if (GET_CODE (p) == CALL_INSN && ! CONST_OR_PURE_CALL_P (p))
+      else if (CALL_P (p) && ! CONST_OR_PURE_CALL_P (p))
        call_passed = 1;
       /* Past a label or a jump, we get to insns for which we
         can't count on whether or how many times they will be
@@ -1080,28 +1475,16 @@ scan_loop (loop, flags)
         only move out sets of trivial variables
         (those not used after the loop).  */
       /* Similar code appears twice in strength_reduce.  */
-      else if ((GET_CODE (p) == CODE_LABEL || GET_CODE (p) == JUMP_INSN)
+      else if ((LABEL_P (p) || JUMP_P (p))
               /* If we enter the loop in the middle, and scan around to the
                  beginning, don't set maybe_never for that.  This must be an
                  unconditional jump, otherwise the code at the top of the
                  loop might never be executed.  Unconditional jumps are
                  followed by a barrier then the loop_end.  */
-              && ! (GET_CODE (p) == JUMP_INSN && JUMP_LABEL (p) == loop->top
+              && ! (JUMP_P (p) && JUMP_LABEL (p) == loop->top
                     && NEXT_INSN (NEXT_INSN (p)) == loop_end
                     && any_uncondjump_p (p)))
        maybe_never = 1;
-      else if (GET_CODE (p) == NOTE)
-       {
-         /* At the virtual top of a converted loop, insns are again known to
-            be executed: logically, the loop begins here even though the exit
-            code has been duplicated.  */
-         if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_VTOP && loop_depth == 0)
-           maybe_never = call_passed = 0;
-         else if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG)
-           loop_depth++;
-         else if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_END)
-           loop_depth--;
-       }
     }
 
   /* If one movable subsumes another, ignore that other.  */
@@ -1126,10 +1509,10 @@ scan_loop (loop, flags)
      Store 0 in regs->array[I].set_in_loop for each reg I that is moved.
 
      For machines with few registers this increases code size, so do not
-     move moveables when optimizing for code size on such machines.  
+     move moveables when optimizing for code size on such machines.
      (The 18 below is the value for i386.)  */
 
-  if (!optimize_size 
+  if (!optimize_size
       || (reg_class_size[GENERAL_REGS] > 18 && !loop_info->has_call))
     {
       move_movables (loop, movables, threshold, insn_count);
@@ -1141,7 +1524,7 @@ scan_loop (loop, flags)
          loop_regs_scan (loop, 0);
          for (update_start = loop_start;
               PREV_INSN (update_start)
-              && GET_CODE (PREV_INSN (update_start)) != CODE_LABEL;
+              && !LABEL_P (PREV_INSN (update_start));
               update_start = PREV_INSN (update_start))
            ;
          update_end = NEXT_INSN (loop_end);
@@ -1168,7 +1551,7 @@ scan_loop (loop, flags)
 
   for (update_start = loop_start;
        PREV_INSN (update_start)
-        && GET_CODE (PREV_INSN (update_start)) != CODE_LABEL;
+        && !LABEL_P (PREV_INSN (update_start));
        update_start = PREV_INSN (update_start))
     ;
   update_end = NEXT_INSN (loop_end);
@@ -1178,7 +1561,7 @@ scan_loop (loop, flags)
 
   if (flag_strength_reduce)
     {
-      if (update_end && GET_CODE (update_end) == CODE_LABEL)
+      if (update_end && LABEL_P (update_end))
        /* Ensure our label doesn't go away.  */
        LABEL_NUSES (update_end)++;
 
@@ -1187,7 +1570,7 @@ scan_loop (loop, flags)
       reg_scan_update (update_start, update_end, loop_max_reg);
       loop_max_reg = max_reg_num ();
 
-      if (update_end && GET_CODE (update_end) == CODE_LABEL
+      if (update_end && LABEL_P (update_end)
          && --LABEL_NUSES (update_end) == 0)
        delete_related_insns (update_end);
     }
@@ -1204,10 +1587,8 @@ scan_loop (loop, flags)
 /* Add elements to *OUTPUT to record all the pseudo-regs
    mentioned in IN_THIS but not mentioned in NOT_IN_THIS.  */
 
-void
-record_excess_regs (in_this, not_in_this, output)
-     rtx in_this, not_in_this;
-     rtx *output;
+static void
+record_excess_regs (rtx in_this, rtx not_in_this, rtx *output)
 {
   enum rtx_code code;
   const char *fmt;
@@ -1260,9 +1641,8 @@ record_excess_regs (in_this, not_in_this, output)
    If there are none, return 0.
    If there are one or more, return an EXPR_LIST containing all of them.  */
 
-rtx
-libcall_other_reg (insn, equiv)
-     rtx insn, equiv;
+static rtx
+libcall_other_reg (rtx insn, rtx equiv)
 {
   rtx note = find_reg_note (insn, REG_RETVAL, NULL_RTX);
   rtx p = XEXP (note, 0);
@@ -1273,8 +1653,7 @@ libcall_other_reg (insn, equiv)
 
   while (p != insn)
     {
-      if (GET_CODE (p) == INSN || GET_CODE (p) == JUMP_INSN
-         || GET_CODE (p) == CALL_INSN)
+      if (INSN_P (p))
        record_excess_regs (PATTERN (p), equiv, &output);
       p = NEXT_INSN (p);
     }
@@ -1286,8 +1665,7 @@ libcall_other_reg (insn, equiv)
    are between INSN and the end of the basic block.  */
 
 static int
-reg_in_basic_block_p (insn, reg)
-     rtx insn, reg;
+reg_in_basic_block_p (rtx insn, rtx reg)
 {
   int regno = REGNO (reg);
   rtx p;
@@ -1340,8 +1718,7 @@ reg_in_basic_block_p (insn, reg)
    value directly or can contain a library call.  */
 
 static int
-libcall_benefit (last)
-     rtx last;
+libcall_benefit (rtx last)
 {
   rtx insn;
   int benefit = 0;
@@ -1349,10 +1726,10 @@ libcall_benefit (last)
   for (insn = XEXP (find_reg_note (last, REG_RETVAL, NULL_RTX), 0);
        insn != last; insn = NEXT_INSN (insn))
     {
-      if (GET_CODE (insn) == CALL_INSN)
+      if (CALL_P (insn))
        benefit += 10;          /* Assume at least this many insns in a library
                                   routine.  */
-      else if (GET_CODE (insn) == INSN
+      else if (NONJUMP_INSN_P (insn)
               && GET_CODE (PATTERN (insn)) != USE
               && GET_CODE (PATTERN (insn)) != CLOBBER)
        benefit++;
@@ -1364,9 +1741,7 @@ libcall_benefit (last)
 /* Skip COUNT insns from INSN, counting library calls as 1 insn.  */
 
 static rtx
-skip_consec_insns (insn, count)
-     rtx insn;
-     int count;
+skip_consec_insns (rtx insn, int count)
 {
   for (; count > 0; count--)
     {
@@ -1375,13 +1750,13 @@ skip_consec_insns (insn, count)
       /* If first insn of libcall sequence, skip to end.  */
       /* Do this at start of loop, since INSN is guaranteed to
         be an insn here.  */
-      if (GET_CODE (insn) != NOTE
+      if (!NOTE_P (insn)
          && (temp = find_reg_note (insn, REG_LIBCALL, NULL_RTX)))
        insn = XEXP (temp, 0);
 
       do
        insn = NEXT_INSN (insn);
-      while (GET_CODE (insn) == NOTE);
+      while (NOTE_P (insn));
     }
 
   return insn;
@@ -1393,8 +1768,7 @@ skip_consec_insns (insn, count)
    was made later and so appears later on the chain.  */
 
 static void
-ignore_some_movables (movables)
-     struct loop_movables *movables;
+ignore_some_movables (struct loop_movables *movables)
 {
   struct movable *m, *m1;
 
@@ -1425,8 +1799,7 @@ ignore_some_movables (movables)
    since the second can be moved only if the first is.  */
 
 static void
-force_movables (movables)
-     struct loop_movables *movables;
+force_movables (struct loop_movables *movables)
 {
   struct movable *m, *m1;
 
@@ -1451,12 +1824,18 @@ force_movables (movables)
          m = 0;
 
        /* Increase the priority of the moving the first insn
-          since it permits the second to be moved as well.  */
+          since it permits the second to be moved as well.
+          Likewise for insns already forced by the first insn.  */
        if (m != 0)
          {
+           struct movable *m2;
+
            m->forces = m1;
-           m1->lifetime += m->lifetime;
-           m1->savings += m->savings;
+           for (m2 = m1; m2; m2 = m2->forces)
+             {
+               m2->lifetime += m->lifetime;
+               m2->savings += m->savings;
+             }
          }
       }
 }
@@ -1465,12 +1844,10 @@ force_movables (movables)
    one register.  */
 
 static void
-combine_movables (movables, regs)
-     struct loop_movables *movables;
-     struct loop_regs *regs;
+combine_movables (struct loop_movables *movables, struct loop_regs *regs)
 {
   struct movable *m;
-  char *matched_regs = (char *) xmalloc (regs->num);
+  char *matched_regs = xmalloc (regs->num);
   enum machine_mode mode;
 
   /* Regs that are set more than once are not allowed to match
@@ -1518,7 +1895,7 @@ combine_movables (movables, regs)
                        && (GET_MODE_BITSIZE (GET_MODE (m->set_dest))
                            >= GET_MODE_BITSIZE (GET_MODE (m1->set_dest)))))
                   /* See if the source of M1 says it matches M.  */
-                  && ((GET_CODE (m1->set_src) == REG
+                  && ((REG_P (m1->set_src)
                        && matched_regs[REGNO (m1->set_src)])
                       || rtx_equal_for_loop_p (m->set_src, m1->set_src,
                                                movables, regs))))
@@ -1592,8 +1969,7 @@ combine_movables (movables, regs)
    moved outside the loop.  */
 
 static int
-num_unmoved_movables (loop)
-     const struct loop *loop;
+num_unmoved_movables (const struct loop *loop)
 {
   int num = 0;
   struct movable *m;
@@ -1609,9 +1985,7 @@ num_unmoved_movables (loop)
 /* Return 1 if regs X and Y will become the same if moved.  */
 
 static int
-regs_match_p (x, y, movables)
-     rtx x, y;
-     struct loop_movables *movables;
+regs_match_p (rtx x, rtx y, struct loop_movables *movables)
 {
   unsigned int xn = REGNO (x);
   unsigned int yn = REGNO (y);
@@ -1638,10 +2012,8 @@ regs_match_p (x, y, movables)
    equivalent constant, consider them equal.  */
 
 static int
-rtx_equal_for_loop_p (x, y, movables, regs)
-     rtx x, y;
-     struct loop_movables *movables;
-     struct loop_regs *regs;
+rtx_equal_for_loop_p (rtx x, rtx y, struct loop_movables *movables,
+                     struct loop_regs *regs)
 {
   int i;
   int j;
@@ -1658,7 +2030,7 @@ rtx_equal_for_loop_p (x, y, movables, regs)
 
   /* If we have a register and a constant, they may sometimes be
      equal.  */
-  if (GET_CODE (x) == REG && regs->array[REGNO (x)].set_in_loop == -2
+  if (REG_P (x) && regs->array[REGNO (x)].set_in_loop == -2
       && CONSTANT_P (y))
     {
       for (m = movables->head; m; m = m->next)
@@ -1666,7 +2038,7 @@ rtx_equal_for_loop_p (x, y, movables, regs)
            && rtx_equal_p (m->set_src, y))
          return 1;
     }
-  else if (GET_CODE (y) == REG && regs->array[REGNO (y)].set_in_loop == -2
+  else if (REG_P (y) && regs->array[REGNO (y)].set_in_loop == -2
           && CONSTANT_P (x))
     {
       for (m = movables->head; m; m = m->next)
@@ -1746,7 +2118,7 @@ rtx_equal_for_loop_p (x, y, movables, regs)
             contain anything but integers and other rtx's,
             except for within LABEL_REFs and SYMBOL_REFs.  */
        default:
-         abort ();
+         gcc_unreachable ();
        }
     }
   return 1;
@@ -1757,9 +2129,7 @@ rtx_equal_for_loop_p (x, y, movables, regs)
    references is incremented once for each added note.  */
 
 static void
-add_label_notes (x, insns)
-     rtx x;
-     rtx insns;
+add_label_notes (rtx x, rtx insns)
 {
   enum rtx_code code = GET_CODE (x);
   int i, j;
@@ -1769,7 +2139,7 @@ add_label_notes (x, insns)
   if (code == LABEL_REF && !LABEL_REF_NONLOCAL_P (x))
     {
       /* This code used to ignore labels that referred to dispatch tables to
-         avoid flow generating (slighly) worse code.
+         avoid flow generating (slightly) worse code.
 
          We no longer ignore such label references (see LABEL_REF handling in
          mark_jump_label for additional information).  */
@@ -1799,11 +2169,8 @@ add_label_notes (x, insns)
    other throughout.  */
 
 static void
-move_movables (loop, movables, threshold, insn_count)
-     struct loop *loop;
-     struct loop_movables *movables;
-     int threshold;
-     int insn_count;
+move_movables (struct loop *loop, struct loop_movables *movables,
+              int threshold, int insn_count)
 {
   struct loop_regs *regs = LOOP_REGS (loop);
   int nregs = regs->num;
@@ -1815,8 +2182,8 @@ move_movables (loop, movables, threshold, insn_count)
   /* Map of pseudo-register replacements to handle combining
      when we move several insns that load the same value
      into different pseudo-registers.  */
-  rtx *reg_map = (rtx *) xcalloc (nregs, sizeof (rtx));
-  char *already_moved = (char *) xcalloc (nregs, sizeof (char));
+  rtx *reg_map = xcalloc (nregs, sizeof (rtx));
+  char *already_moved = xcalloc (nregs, sizeof (char));
 
   for (m = movables->head; m; m = m->next)
     {
@@ -1890,7 +2257,6 @@ move_movables (loop, movables, threshold, insn_count)
             extra cost because something else was already moved.  */
 
          if (already_moved[regno]
-             || flag_move_all_movables
              || (threshold * savings * m->lifetime) >=
                 (regs->array[regno].moved_once ? insn_count * 2 : insn_count)
              || (m->forces && m->forces->done
@@ -1945,21 +2311,26 @@ move_movables (loop, movables, threshold, insn_count)
 
                  for (count = m->consec; count >= 0; count--)
                    {
-                     /* If this is the first insn of a library call sequence,
-                        something is very wrong.  */
-                     if (GET_CODE (p) != NOTE
-                         && (temp = find_reg_note (p, REG_LIBCALL, NULL_RTX)))
-                       abort ();
-
-                     /* If this is the last insn of a libcall sequence, then
-                        delete every insn in the sequence except the last.
-                        The last insn is handled in the normal manner.  */
-                     if (GET_CODE (p) != NOTE
-                         && (temp = find_reg_note (p, REG_RETVAL, NULL_RTX)))
+                     if (!NOTE_P (p))
                        {
-                         temp = XEXP (temp, 0);
-                         while (temp != p)
-                           temp = delete_insn (temp);
+                         /* If this is the first insn of a library
+                            call sequence, something is very
+                            wrong.  */
+                         gcc_assert (!find_reg_note
+                                     (p, REG_LIBCALL, NULL_RTX));
+
+                         /* If this is the last insn of a libcall
+                            sequence, then delete every insn in the
+                            sequence except the last.  The last insn
+                            is handled in the normal manner.  */
+                         temp = find_reg_note (p, REG_RETVAL, NULL_RTX);
+                         
+                         if (temp)
+                           {
+                             temp = XEXP (temp, 0);
+                             while (temp != p)
+                               temp = delete_insn (temp);
+                           }
                        }
 
                      temp = p;
@@ -1971,15 +2342,15 @@ move_movables (loop, movables, threshold, insn_count)
                         pointers, but when we skip over a NOTE we must fix
                         it up.  Otherwise that code walks into the non-deleted
                         insn stream.  */
-                     while (p && GET_CODE (p) == NOTE)
+                     while (p && NOTE_P (p))
                        p = NEXT_INSN (temp) = NEXT_INSN (p);
 
                      if (m->insert_temp)
                        {
                          /* Replace the original insn with a move from
-                            our newly created temp. */
+                            our newly created temp.  */
                          start_sequence ();
-                         emit_move_insn (m->set_dest, newreg);
+                         emit_move_insn (m->set_dest, newreg);
                          seq = get_insns ();
                          end_sequence ();
                          emit_insn_before (seq, p);
@@ -1987,7 +2358,7 @@ move_movables (loop, movables, threshold, insn_count)
                    }
 
                  start_sequence ();
-                 emit_move_insn (m->insert_temp ? newreg : m->set_dest, 
+                 emit_move_insn (m->insert_temp ? newreg : m->set_dest,
                                  m->set_src);
                  seq = get_insns ();
                  end_sequence ();
@@ -2015,14 +2386,14 @@ move_movables (loop, movables, threshold, insn_count)
                      /* If first insn of libcall sequence, skip to end.  */
                      /* Do this at start of loop, since p is guaranteed to
                         be an insn here.  */
-                     if (GET_CODE (p) != NOTE
+                     if (!NOTE_P (p)
                          && (temp = find_reg_note (p, REG_LIBCALL, NULL_RTX)))
                        p = XEXP (temp, 0);
 
                      /* If last insn of libcall sequence, move all
                         insns except the last before the loop.  The last
                         insn is handled in the normal manner.  */
-                     if (GET_CODE (p) != NOTE
+                     if (!NOTE_P (p)
                          && (temp = find_reg_note (p, REG_RETVAL, NULL_RTX)))
                        {
                          rtx fn_address = 0;
@@ -2037,7 +2408,7 @@ move_movables (loop, movables, threshold, insn_count)
                              rtx n;
                              rtx next;
 
-                             if (GET_CODE (temp) == NOTE)
+                             if (NOTE_P (temp))
                                continue;
 
                              body = PATTERN (temp);
@@ -2046,9 +2417,9 @@ move_movables (loop, movables, threshold, insn_count)
                                 not counting USE or NOTE insns.  */
                              for (next = NEXT_INSN (temp); next != p;
                                   next = NEXT_INSN (next))
-                               if (! (GET_CODE (next) == INSN
+                               if (! (NONJUMP_INSN_P (next)
                                       && GET_CODE (PATTERN (next)) == USE)
-                                   && GET_CODE (next) != NOTE)
+                                   && !NOTE_P (next))
                                  break;
 
                              /* If that is the call, this may be the insn
@@ -2062,14 +2433,14 @@ move_movables (loop, movables, threshold, insn_count)
                                 function address into the register that the
                                 call insn will use.  flow.c will delete any
                                 redundant stores that we have created.  */
-                             if (GET_CODE (next) == CALL_INSN
+                             if (CALL_P (next)
                                  && GET_CODE (body) == SET
-                                 && GET_CODE (SET_DEST (body)) == REG
+                                 && REG_P (SET_DEST (body))
                                  && (n = find_reg_note (temp, REG_EQUAL,
                                                         NULL_RTX)))
                                {
                                  fn_reg = SET_SRC (body);
-                                 if (GET_CODE (fn_reg) != REG)
+                                 if (!REG_P (fn_reg))
                                    fn_reg = SET_DEST (body);
                                  fn_address = XEXP (n, 0);
                                  fn_address_insn = temp;
@@ -2077,14 +2448,14 @@ move_movables (loop, movables, threshold, insn_count)
                              /* We have the call insn.
                                 If it uses the register we suspect it might,
                                 load it with the correct address directly.  */
-                             if (GET_CODE (temp) == CALL_INSN
+                             if (CALL_P (temp)
                                  && fn_address != 0
                                  && reg_referenced_p (fn_reg, body))
                                loop_insn_emit_after (loop, 0, fn_address_insn,
                                                      gen_move_insn
                                                      (fn_reg, fn_address));
 
-                             if (GET_CODE (temp) == CALL_INSN)
+                             if (CALL_P (temp))
                                {
                                  i1 = loop_call_insn_hoist (loop, body);
                                  /* Because the USAGE information potentially
@@ -2123,15 +2494,14 @@ move_movables (loop, movables, threshold, insn_count)
                                        << GET_MODE_BITSIZE (m->savemode)))
                                      - 1),
                             reg, 1, OPTAB_LIB_WIDEN);
-                         if (tem == 0)
-                           abort ();
+                         gcc_assert (tem);
                          if (tem != reg)
                            emit_move_insn (reg, tem);
                          sequence = get_insns ();
                          end_sequence ();
                          i1 = loop_insn_hoist (loop, sequence);
                        }
-                     else if (GET_CODE (p) == CALL_INSN)
+                     else if (CALL_P (p))
                        {
                          i1 = loop_call_insn_hoist (loop, PATTERN (p));
                          /* Because the USAGE information potentially
@@ -2147,7 +2517,8 @@ move_movables (loop, movables, threshold, insn_count)
                          /* The SET_SRC might not be invariant, so we must
                             use the REG_EQUAL note.  */
                          start_sequence ();
-                         emit_move_insn (m->set_dest, m->set_src);
+                         emit_move_insn (m->insert_temp ? newreg : m->set_dest,
+                                         m->set_src);
                          seq = get_insns ();
                          end_sequence ();
 
@@ -2160,14 +2531,14 @@ move_movables (loop, movables, threshold, insn_count)
                        }
                      else if (m->insert_temp)
                        {
-                         rtx *reg_map2 = (rtx *) xcalloc (REGNO (newreg), 
-                               sizeof(rtx));
+                         rtx *reg_map2 = xcalloc (REGNO (newreg),
+                                                  sizeof(rtx));
                          reg_map2 [m->regno] = newreg;
 
                          i1 = loop_insn_hoist (loop, copy_rtx (PATTERN (p)));
                          replace_regs (i1, reg_map2, REGNO (newreg), 1);
                          free (reg_map2);
-                       }
+                       }
                      else
                        i1 = loop_insn_hoist (loop, PATTERN (p));
 
@@ -2214,16 +2585,16 @@ move_movables (loop, movables, threshold, insn_count)
                         pointers, but when we skip over a NOTE we must fix
                         it up.  Otherwise that code walks into the non-deleted
                         insn stream.  */
-                     while (p && GET_CODE (p) == NOTE)
+                     while (p && NOTE_P (p))
                        p = NEXT_INSN (temp) = NEXT_INSN (p);
 
                      if (m->insert_temp)
                        {
                          rtx seq;
                          /* Replace the original insn with a move from
-                            our newly created temp. */
+                            our newly created temp.  */
                          start_sequence ();
-                         emit_move_insn (m->set_dest, newreg);
+                         emit_move_insn (m->set_dest, newreg);
                          seq = get_insns ();
                          end_sequence ();
                          emit_insn_before (seq, p);
@@ -2296,7 +2667,7 @@ move_movables (loop, movables, threshold, insn_count)
                         and prevent further processing of it.  */
                      m1->done = 1;
 
-                     /* if library call, delete all insns.  */
+                     /* If library call, delete all insns.  */
                      if ((temp = find_reg_note (m1->insn, REG_RETVAL,
                                                 NULL_RTX)))
                        delete_insn_chain (XEXP (temp, 0), m1->insn);
@@ -2335,8 +2706,7 @@ move_movables (loop, movables, threshold, insn_count)
   /* Go through all the instructions in the loop, making
      all the register substitutions scheduled in REG_MAP.  */
   for (p = new_start; p != loop_end; p = NEXT_INSN (p))
-    if (GET_CODE (p) == INSN || GET_CODE (p) == JUMP_INSN
-       || GET_CODE (p) == CALL_INSN)
+    if (INSN_P (p))
       {
        replace_regs (PATTERN (p), reg_map, nregs, 0);
        replace_regs (REG_NOTES (p), reg_map, nregs, 0);
@@ -2350,9 +2720,7 @@ move_movables (loop, movables, threshold, insn_count)
 
 
 static void
-loop_movables_add (movables, m)
-     struct loop_movables *movables;
-     struct movable *m;
+loop_movables_add (struct loop_movables *movables, struct movable *m)
 {
   if (movables->head == 0)
     movables->head = m;
@@ -2363,8 +2731,7 @@ loop_movables_add (movables, m)
 
 
 static void
-loop_movables_free (movables)
-     struct loop_movables *movables;
+loop_movables_free (struct loop_movables *movables)
 {
   struct movable *m;
   struct movable *m_next;
@@ -2381,8 +2748,7 @@ loop_movables_free (movables)
    REG is the address that MEM should have before the replacement.  */
 
 static void
-replace_call_address (x, reg, addr)
-     rtx x, reg, addr;
+replace_call_address (rtx x, rtx reg, rtx addr)
 {
   enum rtx_code code;
   int i;
@@ -2416,8 +2782,7 @@ replace_call_address (x, reg, addr)
     case MEM:
       /* If this MEM uses a reg other than the one we expected,
         something is wrong.  */
-      if (XEXP (x, 0) != reg)
-       abort ();
+      gcc_assert (XEXP (x, 0) == reg);
       XEXP (x, 0) = addr;
       return;
 
@@ -2444,9 +2809,7 @@ replace_call_address (x, reg, addr)
    in the rtx X.  */
 
 static int
-count_nonfixed_reads (loop, x)
-     const struct loop *loop;
-     rtx x;
+count_nonfixed_reads (const struct loop *loop, rtx x)
 {
   enum rtx_code code;
   int i;
@@ -2493,15 +2856,14 @@ count_nonfixed_reads (loop, x)
   return value;
 }
 \f
-/* Scan a loop setting the elements `cont', `vtop', `loops_enclosed',
+/* Scan a loop setting the elements `loops_enclosed',
    `has_call', `has_nonconst_call', `has_volatile', `has_tablejump',
    `unknown_address_altered', `unknown_constant_address_altered', and
    `num_mem_sets' in LOOP.  Also, fill in the array `mems' and the
    list `store_mems' in LOOP.  */
 
 static void
-prescan_loop (loop)
-     struct loop *loop;
+prescan_loop (struct loop *loop)
 {
   int level = 1;
   rtx insn;
@@ -2530,13 +2892,11 @@ prescan_loop (loop)
   loop_info->first_loop_store_insn = NULL_RTX;
   loop_info->mems_idx = 0;
   loop_info->num_mem_sets = 0;
-  /* If loop opts run twice, this was set on 1st pass for 2nd.  */
-  loop_info->preconditioned = NOTE_PRECONDITIONED (end);
 
-  for (insn = start; insn && GET_CODE (insn) != CODE_LABEL;
+  for (insn = start; insn && !LABEL_P (insn);
        insn = PREV_INSN (insn))
     {
-      if (GET_CODE (insn) == CALL_INSN)
+      if (CALL_P (insn))
        {
          loop_info->pre_header_has_call = 1;
          break;
@@ -2623,13 +2983,13 @@ prescan_loop (loop)
                  loop_info->has_multiple_exit_targets = 1;
                }
            }
-         /* FALLTHRU */
+         /* Fall through.  */
 
        case INSN:
          if (volatile_refs_p (PATTERN (insn)))
            loop_info->has_volatile = 1;
 
-         if (GET_CODE (insn) == JUMP_INSN
+         if (JUMP_P (insn)
              && (GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC
                  || GET_CODE (PATTERN (insn)) == ADDR_VEC))
            loop_info->has_tablejump = 1;
@@ -2676,8 +3036,7 @@ prescan_loop (loop)
   if (loop_info->unknown_constant_address_altered)
     {
       rtx mem = gen_rtx_MEM (BLKmode, const0_rtx);
-
-      RTX_UNCHANGING_P (mem) = 1;
+      MEM_READONLY_P (mem) = 1;
       loop_info->store_mems
        = gen_rtx_EXPR_LIST (VOIDmode, mem, loop_info->store_mems);
     }
@@ -2686,8 +3045,7 @@ prescan_loop (loop)
 /* Invalidate all loops containing LABEL.  */
 
 static void
-invalidate_loops_containing_label (label)
-     rtx label;
+invalidate_loops_containing_label (rtx label)
 {
   struct loop *loop;
   for (loop = uid_loop[INSN_UID (label)]; loop; loop = loop->outer)
@@ -2699,9 +3057,7 @@ invalidate_loops_containing_label (label)
    to from outside the loop.  */
 
 static void
-find_and_verify_loops (f, loops)
-     rtx f;
-     struct loops *loops;
+find_and_verify_loops (rtx f, struct loops *loops)
 {
   rtx insn;
   rtx label;
@@ -2726,7 +3082,7 @@ find_and_verify_loops (f, loops)
   current_loop = NULL;
   for (insn = f; insn; insn = NEXT_INSN (insn))
     {
-      if (GET_CODE (insn) == NOTE)
+      if (NOTE_P (insn))
        switch (NOTE_LINE_NUMBER (insn))
          {
          case NOTE_INSN_LOOP_BEG:
@@ -2738,17 +3094,8 @@ find_and_verify_loops (f, loops)
            current_loop = next_loop;
            break;
 
-         case NOTE_INSN_LOOP_CONT:
-           current_loop->cont = insn;
-           break;
-
-         case NOTE_INSN_LOOP_VTOP:
-           current_loop->vtop = insn;
-           break;
-
          case NOTE_INSN_LOOP_END:
-           if (! current_loop)
-             abort ();
+           gcc_assert (current_loop);
 
            current_loop->end = insn;
            current_loop = current_loop->outer;
@@ -2758,7 +3105,7 @@ find_and_verify_loops (f, loops)
            break;
          }
 
-      if (GET_CODE (insn) == CALL_INSN
+      if (CALL_P (insn)
          && find_reg_note (insn, REG_SETJMP, NULL))
        {
          /* In this case, we must invalidate our current loop and any
@@ -2805,14 +3152,14 @@ find_and_verify_loops (f, loops)
       {
        struct loop *this_loop = uid_loop[INSN_UID (insn)];
 
-       if (GET_CODE (insn) == INSN || GET_CODE (insn) == CALL_INSN)
+       if (NONJUMP_INSN_P (insn) || CALL_P (insn))
          {
            rtx note = find_reg_note (insn, REG_LABEL, NULL_RTX);
            if (note)
              invalidate_loops_containing_label (XEXP (note, 0));
          }
 
-       if (GET_CODE (insn) != JUMP_INSN)
+       if (!JUMP_P (insn))
          continue;
 
        mark_loop_jump (PATTERN (insn), this_loop);
@@ -2835,10 +3182,10 @@ find_and_verify_loops (f, loops)
            /* Go backwards until we reach the start of the loop, a label,
               or a JUMP_INSN.  */
            for (p = PREV_INSN (insn);
-                GET_CODE (p) != CODE_LABEL
-                && ! (GET_CODE (p) == NOTE
+                !LABEL_P (p)
+                && ! (NOTE_P (p)
                       && NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG)
-                && GET_CODE (p) != JUMP_INSN;
+                && !JUMP_P (p);
                 p = PREV_INSN (p))
              ;
 
@@ -2859,7 +3206,7 @@ find_and_verify_loops (f, loops)
 
            /* Make sure that the target of P is within the current loop.  */
 
-           if (GET_CODE (p) == JUMP_INSN && JUMP_LABEL (p)
+           if (JUMP_P (p) && JUMP_LABEL (p)
                && uid_loop[INSN_UID (JUMP_LABEL (p))] != this_loop)
              outer_loop = this_loop;
 
@@ -2873,7 +3220,7 @@ find_and_verify_loops (f, loops)
               and move the block of code to the spot we found.  */
 
            if (! outer_loop
-               && GET_CODE (p) == JUMP_INSN
+               && JUMP_P (p)
                && JUMP_LABEL (p) != 0
                /* Just ignore jumps to labels that were never emitted.
                   These always indicate compilation errors.  */
@@ -2894,17 +3241,17 @@ find_and_verify_loops (f, loops)
                /* Search for possible garbage past the conditional jumps
                   and look for the last barrier.  */
                for (tmp = last_insn_to_move;
-                    tmp && GET_CODE (tmp) != CODE_LABEL; tmp = NEXT_INSN (tmp))
-                 if (GET_CODE (tmp) == BARRIER)
+                    tmp && !LABEL_P (tmp); tmp = NEXT_INSN (tmp))
+                 if (BARRIER_P (tmp))
                    last_insn_to_move = tmp;
 
                for (loc = target; loc; loc = PREV_INSN (loc))
-                 if (GET_CODE (loc) == BARRIER
+                 if (BARRIER_P (loc)
                      /* Don't move things inside a tablejump.  */
                      && ((loc2 = next_nonnote_insn (loc)) == 0
-                         || GET_CODE (loc2) != CODE_LABEL
+                         || !LABEL_P (loc2)
                          || (loc2 = next_nonnote_insn (loc2)) == 0
-                         || GET_CODE (loc2) != JUMP_INSN
+                         || !JUMP_P (loc2)
                          || (GET_CODE (PATTERN (loc2)) != ADDR_VEC
                              && GET_CODE (PATTERN (loc2)) != ADDR_DIFF_VEC))
                      && uid_loop[INSN_UID (loc)] == target_loop)
@@ -2912,12 +3259,12 @@ find_and_verify_loops (f, loops)
 
                if (loc == 0)
                  for (loc = target; loc; loc = NEXT_INSN (loc))
-                   if (GET_CODE (loc) == BARRIER
+                   if (BARRIER_P (loc)
                        /* Don't move things inside a tablejump.  */
                        && ((loc2 = next_nonnote_insn (loc)) == 0
-                           || GET_CODE (loc2) != CODE_LABEL
+                           || !LABEL_P (loc2)
                            || (loc2 = next_nonnote_insn (loc2)) == 0
-                           || GET_CODE (loc2) != JUMP_INSN
+                           || !JUMP_P (loc2)
                            || (GET_CODE (PATTERN (loc2)) != ADDR_VEC
                                && GET_CODE (PATTERN (loc2)) != ADDR_DIFF_VEC))
                        && uid_loop[INSN_UID (loc)] == target_loop)
@@ -2936,6 +3283,7 @@ find_and_verify_loops (f, loops)
                    if (invert_jump (p, new_label, 1))
                      {
                        rtx q, r;
+                       bool only_notes;
 
                        /* If no suitable BARRIER was found, create a suitable
                           one before TARGET.  Since TARGET is a fall through
@@ -2960,8 +3308,10 @@ find_and_verify_loops (f, loops)
 
                        /* Include the BARRIER after INSN and copy the
                           block after LOC.  */
-                       if (squeeze_notes (&new_label, &last_insn_to_move))
-                         abort ();
+                       only_notes = squeeze_notes (&new_label,
+                                                   &last_insn_to_move);
+                       gcc_assert (!only_notes);
+                       
                        reorder_insns (new_label, last_insn_to_move, loc);
 
                        /* All those insns are now in TARGET_LOOP.  */
@@ -2996,8 +3346,7 @@ find_and_verify_loops (f, loops)
 
                            /* If we didn't find it, then something is
                               wrong.  */
-                           if (! r)
-                             abort ();
+                           gcc_assert (r);
                          }
 
                        /* P is now a jump outside the loop, so it must be put
@@ -3037,9 +3386,7 @@ find_and_verify_loops (f, loops)
    For speed, we assume that X is part of a pattern of a JUMP_INSN.  */
 
 static void
-mark_loop_jump (x, loop)
-     rtx x;
-     struct loop *loop;
+mark_loop_jump (rtx x, struct loop *loop)
 {
   struct loop *dest_loop;
   struct loop *outer_loop;
@@ -3185,13 +3532,11 @@ mark_loop_jump (x, loop)
    been previously created by loop.c).  */
 
 static int
-labels_in_range_p (insn, end)
-     rtx insn;
-     int end;
+labels_in_range_p (rtx insn, int end)
 {
   while (insn && INSN_LUID (insn) <= end)
     {
-      if (GET_CODE (insn) == CODE_LABEL)
+      if (LABEL_P (insn))
        return 1;
       insn = NEXT_INSN (insn);
     }
@@ -3202,14 +3547,12 @@ labels_in_range_p (insn, end)
 /* Record that a memory reference X is being set.  */
 
 static void
-note_addr_stored (x, y, data)
-     rtx x;
-     rtx y ATTRIBUTE_UNUSED;
-     void *data ATTRIBUTE_UNUSED;
+note_addr_stored (rtx x, rtx y ATTRIBUTE_UNUSED,
+                 void *data ATTRIBUTE_UNUSED)
 {
   struct loop_info *loop_info = data;
 
-  if (x == 0 || GET_CODE (x) != MEM)
+  if (x == 0 || !MEM_P (x))
     return;
 
   /* Count number of memory writes.
@@ -3219,7 +3562,7 @@ note_addr_stored (x, y, data)
   /* BLKmode MEM means all memory is clobbered.  */
   if (GET_MODE (x) == BLKmode)
     {
-      if (RTX_UNCHANGING_P (x))
+      if (MEM_READONLY_P (x))
        loop_info->unknown_constant_address_altered = 1;
       else
        loop_info->unknown_address_altered = 1;
@@ -3232,15 +3575,12 @@ note_addr_stored (x, y, data)
 }
 
 /* X is a value modified by an INSN that references a biv inside a loop
-   exit test (ie, X is somehow related to the value of the biv).  If X
+   exit test (i.e., X is somehow related to the value of the biv).  If X
    is a pseudo that is used more than once, then the biv is (effectively)
    used more than once.  DATA is a pointer to a loop_regs structure.  */
 
 static void
-note_set_pseudo_multiple_uses (x, y, data)
-     rtx x;
-     rtx y ATTRIBUTE_UNUSED;
-     void *data;
+note_set_pseudo_multiple_uses (rtx x, rtx y ATTRIBUTE_UNUSED, void *data)
 {
   struct loop_regs *regs = (struct loop_regs *) data;
 
@@ -3253,7 +3593,7 @@ note_set_pseudo_multiple_uses (x, y, data)
         || GET_CODE (x) == SUBREG)
     x = XEXP (x, 0);
 
-  if (GET_CODE (x) != REG || REGNO (x) < FIRST_PSEUDO_REGISTER)
+  if (!REG_P (x) || REGNO (x) < FIRST_PSEUDO_REGISTER)
     return;
 
   /* If we do not have usage information, or if we know the register
@@ -3271,10 +3611,8 @@ note_set_pseudo_multiple_uses (x, y, data)
    A memory ref is invariant if it is not volatile and does not conflict
    with anything stored in `loop_info->store_mems'.  */
 
-int
-loop_invariant_p (loop, x)
-     const struct loop *loop;
-     rtx x;
+static int
+loop_invariant_p (const struct loop *loop, rtx x)
 {
   struct loop_info *loop_info = LOOP_INFO (loop);
   struct loop_regs *regs = LOOP_REGS (loop);
@@ -3296,19 +3634,7 @@ loop_invariant_p (loop, x)
       return 1;
 
     case LABEL_REF:
-      /* A LABEL_REF is normally invariant, however, if we are unrolling
-        loops, and this label is inside the loop, then it isn't invariant.
-        This is because each unrolled copy of the loop body will have
-        a copy of this label.  If this was invariant, then an insn loading
-        the address of this label into a register might get moved outside
-        the loop, and then each loop body would end up using the same label.
-
-        We don't know the loop bounds here though, so just fail for all
-        labels.  */
-      if (flag_old_unroll_loops)
-       return 0;
-      else
-       return 1;
+      return 1;
 
     case PC:
     case CC0:
@@ -3316,9 +3642,6 @@ loop_invariant_p (loop, x)
       return 0;
 
     case REG:
-      /* We used to check RTX_UNCHANGING_P (x) here, but that is invalid
-        since the reg might be set by initialization within the loop.  */
-
       if ((x == frame_pointer_rtx || x == hard_frame_pointer_rtx
           || x == arg_pointer_rtx || x == pic_offset_table_rtx)
          && ! current_function_has_nonlocal_goto)
@@ -3329,8 +3652,14 @@ loop_invariant_p (loop, x)
        return 0;
 
       /* Out-of-range regs can occur when we are called from unrolling.
-        These have always been created by the unroller and are set in
-        the loop, hence are never invariant.  */
+        These registers created by the unroller are set in the loop,
+        hence are never invariant.
+        Other out-of-range regs can be generated by load_mems; those that
+        are written to in the loop are not invariant, while those that are
+        not written to are invariant.  It would be easy for load_mems
+        to set n_times_set correctly for these registers, however, there
+        is no easy way to distinguish them from registers created by the
+        unroller.  */
 
       if (REGNO (x) >= (unsigned) regs->num)
        return 0;
@@ -3412,10 +3741,8 @@ loop_invariant_p (loop, x)
    and that its source is invariant.  */
 
 static int
-consec_sets_invariant_p (loop, reg, n_sets, insn)
-     const struct loop *loop;
-     int n_sets;
-     rtx reg, insn;
+consec_sets_invariant_p (const struct loop *loop, rtx reg, int n_sets,
+                        rtx insn)
 {
   struct loop_regs *regs = LOOP_REGS (loop);
   rtx p = insn;
@@ -3448,7 +3775,7 @@ consec_sets_invariant_p (loop, reg, n_sets, insn)
       this = 0;
       if (code == INSN
          && (set = single_set (p))
-         && GET_CODE (SET_DEST (set)) == REG
+         && REG_P (SET_DEST (set))
          && REGNO (SET_DEST (set)) == regno)
        {
          this = loop_invariant_p (loop, SET_SRC (set));
@@ -3479,49 +3806,13 @@ consec_sets_invariant_p (loop, reg, n_sets, insn)
   /* If loop_invariant_p ever returned 2, we return 2.  */
   return 1 + (value & 2);
 }
-
-#if 0
-/* I don't think this condition is sufficient to allow INSN
-   to be moved, so we no longer test it.  */
-
-/* Return 1 if all insns in the basic block of INSN and following INSN
-   that set REG are invariant according to TABLE.  */
-
-static int
-all_sets_invariant_p (reg, insn, table)
-     rtx reg, insn;
-     short *table;
-{
-  rtx p = insn;
-  int regno = REGNO (reg);
-
-  while (1)
-    {
-      enum rtx_code code;
-      p = NEXT_INSN (p);
-      code = GET_CODE (p);
-      if (code == CODE_LABEL || code == JUMP_INSN)
-       return 1;
-      if (code == INSN && GET_CODE (PATTERN (p)) == SET
-         && GET_CODE (SET_DEST (PATTERN (p))) == REG
-         && REGNO (SET_DEST (PATTERN (p))) == regno)
-       {
-         if (! loop_invariant_p (loop, SET_SRC (PATTERN (p)), table))
-           return 0;
-       }
-    }
-}
-#endif /* 0 */
 \f
 /* Look at all uses (not sets) of registers in X.  For each, if it is
    the single use, set USAGE[REGNO] to INSN; if there was a previous use in
    a different insn, set USAGE[REGNO] to const0_rtx.  */
 
 static void
-find_single_use_in_loop (regs, insn, x)
-     struct loop_regs *regs;
-     rtx insn;
-     rtx x;
+find_single_use_in_loop (struct loop_regs *regs, rtx insn, rtx x)
 {
   enum rtx_code code = GET_CODE (x);
   const char *fmt = GET_RTX_FORMAT (code);
@@ -3539,7 +3830,7 @@ find_single_use_in_loop (regs, insn, x)
         in SET_DEST because if a register is partially modified, it won't
         show up as a potential movable so we don't care how USAGE is set
         for it.  */
-      if (GET_CODE (SET_DEST (x)) != REG)
+      if (!REG_P (SET_DEST (x)))
        find_single_use_in_loop (regs, insn, SET_DEST (x));
       find_single_use_in_loop (regs, insn, SET_SRC (x));
     }
@@ -3559,12 +3850,9 @@ find_single_use_in_loop (regs, insn, x)
    in X.  */
 
 static void
-count_one_set (regs, insn, x, last_set)
-     struct loop_regs *regs;
-     rtx insn, x;
-     rtx *last_set;
+count_one_set (struct loop_regs *regs, rtx insn, rtx x, rtx *last_set)
 {
-  if (GET_CODE (x) == CLOBBER && GET_CODE (XEXP (x, 0)) == REG)
+  if (GET_CODE (x) == CLOBBER && REG_P (XEXP (x, 0)))
     /* Don't move a reg that has an explicit clobber.
        It's not worth the pain to try to do it correctly.  */
     regs->array[REGNO (XEXP (x, 0))].may_not_optimize = 1;
@@ -3574,10 +3862,9 @@ count_one_set (regs, insn, x, last_set)
       rtx dest = SET_DEST (x);
       while (GET_CODE (dest) == SUBREG
             || GET_CODE (dest) == ZERO_EXTRACT
-            || GET_CODE (dest) == SIGN_EXTRACT
             || GET_CODE (dest) == STRICT_LOW_PART)
        dest = XEXP (dest, 0);
-      if (GET_CODE (dest) == REG)
+      if (REG_P (dest))
        {
          int i;
          int regno = REGNO (dest);
@@ -3588,7 +3875,7 @@ count_one_set (regs, insn, x, last_set)
                 it must be set in two basic blocks, so it cannot
                 be moved out of the loop.  */
              if (regs->array[regno].set_in_loop > 0
-                 && last_set == 0)
+                 && last_set[regno] == 0)
                regs->array[regno+i].may_not_optimize = 1;
              /* If this is not first setting in current basic block,
                 see if reg was used in between previous one and this.
@@ -3614,9 +3901,7 @@ count_one_set (regs, insn, x, last_set)
    from an inner loop past two loops.  */
 
 static int
-loop_reg_used_before_p (loop, set, insn)
-     const struct loop *loop;
-     rtx set, insn;
+loop_reg_used_before_p (const struct loop *loop, rtx set, rtx insn)
 {
   rtx reg = SET_DEST (set);
   rtx p;
@@ -3664,20 +3949,18 @@ struct check_store_data
   int mem_write;
 };
 
-static void check_store PARAMS ((rtx, rtx, void *));
-static void emit_prefetch_instructions PARAMS ((struct loop *));
-static int rtx_equal_for_prefetch_p PARAMS ((rtx, rtx));
+static void check_store (rtx, rtx, void *);
+static void emit_prefetch_instructions (struct loop *);
+static int rtx_equal_for_prefetch_p (rtx, rtx);
 
 /* Set mem_write when mem_address is found.  Used as callback to
    note_stores.  */
 static void
-check_store (x, pat, data)
-     rtx x, pat ATTRIBUTE_UNUSED;
-     void *data;
+check_store (rtx x, rtx pat ATTRIBUTE_UNUSED, void *data)
 {
   struct check_store_data *d = (struct check_store_data *) data;
 
-  if ((GET_CODE (x) == MEM) && rtx_equal_p (d->mem_address, XEXP (x, 0)))
+  if ((MEM_P (x)) && rtx_equal_p (d->mem_address, XEXP (x, 0)))
     d->mem_write = 1;
 }
 \f
@@ -3689,8 +3972,7 @@ check_store (x, pat, data)
    It can be nice to develop some common way to handle this.  */
 
 static int
-rtx_equal_for_prefetch_p (x, y)
-     rtx x, y;
+rtx_equal_for_prefetch_p (rtx x, rtx y)
 {
   int i;
   int j;
@@ -3702,15 +3984,14 @@ rtx_equal_for_prefetch_p (x, y)
   if (code != GET_CODE (y))
     return 0;
 
-  code = GET_CODE (x);
-
-  if (GET_RTX_CLASS (code) == 'c')
+  if (COMMUTATIVE_ARITH_P (x))
     {
       return ((rtx_equal_for_prefetch_p (XEXP (x, 0), XEXP (y, 0))
               && rtx_equal_for_prefetch_p (XEXP (x, 1), XEXP (y, 1)))
              || (rtx_equal_for_prefetch_p (XEXP (x, 0), XEXP (y, 1))
                  && rtx_equal_for_prefetch_p (XEXP (x, 1), XEXP (y, 0))));
     }
+
   /* Compare the elements.  If any pair of corresponding elements fails to
      match, return 0 for the whole thing.  */
 
@@ -3762,7 +4043,7 @@ rtx_equal_for_prefetch_p (x, y)
             contain anything but integers and other rtx's,
             except for within LABEL_REFs and SYMBOL_REFs.  */
        default:
-         abort ();
+         gcc_unreachable ();
        }
     }
   return 1;
@@ -3772,8 +4053,7 @@ rtx_equal_for_prefetch_p (x, y)
    and return it.  */
 
 static HOST_WIDE_INT
-remove_constant_addition (x)
-     rtx *x;
+remove_constant_addition (rtx *x)
 {
   HOST_WIDE_INT addval = 0;
   rtx exp = *x;
@@ -3835,8 +4115,7 @@ remove_constant_addition (x)
    controlled by defined symbols that can be overridden for each target.  */
 
 static void
-emit_prefetch_instructions (loop)
-     struct loop *loop;
+emit_prefetch_instructions (struct loop *loop)
 {
   int num_prefetches = 0;
   int num_real_prefetches = 0;
@@ -3850,7 +4129,7 @@ emit_prefetch_instructions (loop)
   struct prefetch_info info[MAX_PREFETCHES];
   struct loop_ivs *ivs = LOOP_IVS (loop);
 
-  if (!HAVE_prefetch)
+  if (!HAVE_prefetch || PREFETCH_BLOCK == 0)
     return;
 
   /* Consider only loops w/o calls.  When a call is done, the loop is probably
@@ -4168,11 +4447,9 @@ emit_prefetch_instructions (loop)
                   " density: %d%%; bytes_accessed: %u; total_bytes: %u\n",
                   (int) (info[i].bytes_accessed * 100 / info[i].stride),
                   info[i].bytes_accessed, info[i].total_bytes);
-         fprintf (loop_dump_stream, " index: ");
-         fprintf (loop_dump_stream, HOST_WIDE_INT_PRINT_DEC, info[i].index);
-         fprintf (loop_dump_stream, "; stride: ");
-         fprintf (loop_dump_stream, HOST_WIDE_INT_PRINT_DEC, info[i].stride);
-         fprintf (loop_dump_stream, "; address: ");
+         fprintf (loop_dump_stream, " index: " HOST_WIDE_INT_PRINT_DEC
+                  "; stride: " HOST_WIDE_INT_PRINT_DEC "; address: ",
+                  info[i].index, info[i].stride);
          print_rtl (loop_dump_stream, info[i].base_address);
          fprintf (loop_dump_stream, "\n");
        }
@@ -4213,8 +4490,8 @@ emit_prefetch_instructions (loop)
            {
              rtx reg = gen_reg_rtx (Pmode);
              loop_iv_add_mult_emit_before (loop, loc, const1_rtx,
-                                           GEN_INT (bytes_ahead), reg,
-                                           0, before_insn);
+                                           GEN_INT (bytes_ahead), reg,
+                                           0, before_insn);
              loc = reg;
            }
 
@@ -4281,14 +4558,6 @@ emit_prefetch_instructions (loop)
   return;
 }
 \f
-/* A "basic induction variable" or biv is a pseudo reg that is set
-   (within this loop) only by incrementing or decrementing it.  */
-/* A "general induction variable" or giv is a pseudo reg whose
-   value is a linear function of a biv.  */
-
-/* Bivs are recognized by `basic_induction_var';
-   Givs by `general_induction_var'.  */
-
 /* Communication with routines called via `note_stores'.  */
 
 static rtx note_insn;
@@ -4324,6 +4593,56 @@ static rtx addr_placeholder;
    was rerun in loop_optimize whenever a register was added or moved.
    Also, some of the optimizations could be a little less conservative.  */
 \f
+/* Searches the insns between INSN and LOOP->END.  Returns 1 if there
+   is a backward branch in that range that branches to somewhere between
+   LOOP->START and INSN.  Returns 0 otherwise.  */
+
+/* ??? 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.  */
+
+static int
+back_branch_in_range_p (const struct loop *loop, rtx insn)
+{
+  rtx p, q, target_insn;
+  rtx loop_start = loop->start;
+  rtx loop_end = loop->end;
+  rtx orig_loop_end = loop->end;
+
+  /* Stop before we get to the backward branch at the end of the loop.  */
+  loop_end = prev_nonnote_insn (loop_end);
+  if (BARRIER_P (loop_end))
+    loop_end = PREV_INSN (loop_end);
+
+  /* Check in case insn has been deleted, search forward for first non
+     deleted insn following it.  */
+  while (INSN_DELETED_P (insn))
+    insn = NEXT_INSN (insn);
+
+  /* Check for the case where insn is the last insn in the loop.  Deal
+     with the case where INSN was a deleted loop test insn, in which case
+     it will now be the NOTE_LOOP_END.  */
+  if (insn == loop_end || insn == orig_loop_end)
+    return 0;
+
+  for (p = NEXT_INSN (insn); p != loop_end; p = NEXT_INSN (p))
+    {
+      if (JUMP_P (p))
+       {
+         target_insn = JUMP_LABEL (p);
+
+         /* Search from loop_start to insn, to see if one of them is
+            the target_insn.  We can't use INSN_LUID comparisons here,
+            since insn may not have an LUID entry.  */
+         for (q = loop_start; q != insn; q = NEXT_INSN (q))
+           if (q == target_insn)
+             return 1;
+       }
+    }
+
+  return 0;
+}
+
 /* Scan the loop body and call FNCALL for each insn.  In the addition to the
    LOOP and INSN parameters pass MAYBE_MULTIPLE and NOT_EVERY_ITERATION to the
    callback.
@@ -4334,21 +4653,25 @@ static rtx addr_placeholder;
    MAYBE_MULTIPLE is 1 if current insn may be executed more than once for every
    loop iteration.
  */
-void
-for_each_insn_in_loop (loop, fncall)
-     struct loop *loop;
-     loop_insn_callback fncall;
+typedef rtx (*loop_insn_callback) (struct loop *, rtx, int, int);
+static void
+for_each_insn_in_loop (struct loop *loop, loop_insn_callback fncall)
 {
   int not_every_iteration = 0;
   int maybe_multiple = 0;
   int past_loop_latch = 0;
-  int loop_depth = 0;
+  bool exit_test_is_entry = false;
   rtx p;
 
-  /* If loop_scan_start points to the loop exit test, we have to be wary of
-     subversive use of gotos inside expression statements.  */
+  /* If loop_scan_start points to the loop exit test, the loop body
+     cannot be counted on running on every iteration, and we have to
+     be wary of subversive use of gotos inside expression
+     statements.  */
   if (prev_nonnote_insn (loop->scan_start) != prev_nonnote_insn (loop->start))
-    maybe_multiple = back_branch_in_range_p (loop, loop->scan_start);
+    {
+      exit_test_is_entry = true;
+      maybe_multiple = back_branch_in_range_p (loop, loop->scan_start);
+    }
 
   /* Scan through loop and update NOT_EVERY_ITERATION and MAYBE_MULTIPLE.  */
   for (p = next_insn_in_loop (loop, loop->scan_start);
@@ -4363,7 +4686,7 @@ for_each_insn_in_loop (loop, fncall)
          returns, exits the loop, is a jump to a location that is still
          behind the label, or is a jump to the loop start.  */
 
-      if (GET_CODE (p) == CODE_LABEL)
+      if (LABEL_P (p))
        {
          rtx insn = p;
 
@@ -4384,7 +4707,7 @@ for_each_insn_in_loop (loop, fncall)
                    break;
                }
 
-             if (GET_CODE (insn) == JUMP_INSN
+             if (JUMP_P (insn)
                  && GET_CODE (PATTERN (insn)) != RETURN
                  && (!any_condjump_p (insn)
                      || (JUMP_LABEL (insn) != 0
@@ -4401,15 +4724,17 @@ for_each_insn_in_loop (loop, fncall)
          on whether they will be executed during each iteration.  */
       /* This code appears twice in strength_reduce.  There is also similar
          code in scan_loop.  */
-      if (GET_CODE (p) == JUMP_INSN
+      if (JUMP_P (p)
       /* If we enter the loop in the middle, and scan around to the
          beginning, don't set not_every_iteration for that.
          This can be any kind of jump, since we want to know if insns
          will be executed if the loop is executed.  */
-         && !(JUMP_LABEL (p) == loop->top
-              && ((NEXT_INSN (NEXT_INSN (p)) == loop->end
-                   && any_uncondjump_p (p))
-                  || (NEXT_INSN (p) == loop->end && any_condjump_p (p)))))
+         && (exit_test_is_entry
+             || !(JUMP_LABEL (p) == loop->top
+                  && ((NEXT_INSN (NEXT_INSN (p)) == loop->end
+                       && any_uncondjump_p (p))
+                      || (NEXT_INSN (p) == loop->end
+                          && any_condjump_p (p))))))
        {
          rtx label = 0;
 
@@ -4425,24 +4750,6 @@ for_each_insn_in_loop (loop, fncall)
            not_every_iteration = 1;
        }
 
-      else if (GET_CODE (p) == NOTE)
-       {
-         /* At the virtual top of a converted loop, insns are again known to
-            be executed each iteration: logically, the loop begins here
-            even though the exit code has been duplicated.
-
-            Insns are also again known to be executed each iteration at
-            the LOOP_CONT note.  */
-         if ((NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_VTOP
-              || NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_CONT)
-             && loop_depth == 0)
-           not_every_iteration = 0;
-         else if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG)
-           loop_depth++;
-         else if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_END)
-           loop_depth--;
-       }
-
       /* Note if we pass a loop latch.  If we do, then we can not clear
          NOT_EVERY_ITERATION below when we pass the last CODE_LABEL in
          a loop since a jump before the last CODE_LABEL may have started
@@ -4451,7 +4758,7 @@ for_each_insn_in_loop (loop, fncall)
          Note that LOOP_TOP is only set for rotated loops and we need
          this check for all loops, so compare against the CODE_LABEL
          which immediately follows LOOP_START.  */
-      if (GET_CODE (p) == JUMP_INSN
+      if (JUMP_P (p)
          && JUMP_LABEL (p) == NEXT_INSN (loop->start))
        past_loop_latch = 1;
 
@@ -4467,16 +4774,14 @@ for_each_insn_in_loop (loop, fncall)
 
       if (not_every_iteration
          && !past_loop_latch
-         && GET_CODE (p) == CODE_LABEL
-         && no_labels_between_p (p, loop->end)
-         && loop_insn_first_p (p, loop->cont))
+         && LABEL_P (p)
+         && no_labels_between_p (p, loop->end))
        not_every_iteration = 0;
     }
 }
 \f
 static void
-loop_bivs_find (loop)
-     struct loop *loop;
+loop_bivs_find (struct loop *loop)
 {
   struct loop_regs *regs = LOOP_REGS (loop);
   struct loop_ivs *ivs = LOOP_IVS (loop);
@@ -4524,8 +4829,7 @@ loop_bivs_find (loop)
 /* Determine how BIVS are initialized by looking through pre-header
    extended basic block.  */
 static void
-loop_bivs_init_find (loop)
-     struct loop *loop;
+loop_bivs_init_find (struct loop *loop)
 {
   struct loop_ivs *ivs = LOOP_IVS (loop);
   /* Temporary list pointers for traversing ivs->list.  */
@@ -4537,13 +4841,13 @@ loop_bivs_init_find (loop)
      halting at first label.  Also record any test condition.  */
 
   call_seen = 0;
-  for (p = loop->start; p && GET_CODE (p) != CODE_LABEL; p = PREV_INSN (p))
+  for (p = loop->start; p && !LABEL_P (p); p = PREV_INSN (p))
     {
       rtx test;
 
       note_insn = p;
 
-      if (GET_CODE (p) == CALL_INSN)
+      if (CALL_P (p))
        call_seen = 1;
 
       if (INSN_P (p))
@@ -4552,11 +4856,11 @@ loop_bivs_init_find (loop)
       /* Record any test of a biv that branches around the loop if no store
         between it and the start of loop.  We only care about tests with
         constants and registers and only certain of those.  */
-      if (GET_CODE (p) == JUMP_INSN
+      if (JUMP_P (p)
          && JUMP_LABEL (p) != 0
          && next_real_insn (JUMP_LABEL (p)) == next_real_insn (loop->end)
          && (test = get_condition_for_loop (loop, p)) != 0
-         && GET_CODE (XEXP (test, 0)) == REG
+         && REG_P (XEXP (test, 0))
          && REGNO (XEXP (test, 0)) < max_reg_before_loop
          && (bl = REG_IV_CLASS (ivs, REGNO (XEXP (test, 0)))) != 0
          && valid_initial_value_p (XEXP (test, 1), p, call_seen, loop->start)
@@ -4580,8 +4884,7 @@ loop_bivs_init_find (loop)
    initial value from any initializing insns set up above.  (This is done
    in two passes to avoid missing SETs in a PARALLEL.)  */
 static void
-loop_bivs_check (loop)
-     struct loop *loop;
+loop_bivs_check (struct loop *loop)
 {
   struct loop_ivs *ivs = LOOP_IVS (loop);
   /* Temporary list pointers for traversing ivs->list.  */
@@ -4636,8 +4939,7 @@ loop_bivs_check (loop)
 /* Search the loop for general induction variables.  */
 
 static void
-loop_givs_find (loop)
-     struct loop* loop;
+loop_givs_find (struct loop* loop)
 {
   for_each_insn_in_loop (loop, check_insn_for_givs);
 }
@@ -4648,8 +4950,7 @@ loop_givs_find (loop)
    can be calculated.  */
 
 static void
-loop_givs_check (loop)
-     struct loop *loop;
+loop_givs_check (struct loop *loop)
 {
   struct loop_ivs *ivs = LOOP_IVS (loop);
   struct iv_class *bl;
@@ -4664,99 +4965,336 @@ loop_givs_check (loop)
     }
 }
 
+/* Try to generate the simplest rtx for the expression
+   (PLUS (MULT mult1 mult2) add1).  This is used to calculate the initial
+   value of giv's.  */
 
-/* Return nonzero if it is possible to eliminate the biv BL provided
-   all givs are reduced.  This is possible if either the reg is not
-   used outside the loop, or we can compute what its final value will
-   be.  */
-
-static int
-loop_biv_eliminable_p (loop, bl, threshold, insn_count)
-     struct loop *loop;
-     struct iv_class *bl;
-     int threshold;
-     int insn_count;
+static rtx
+fold_rtx_mult_add (rtx mult1, rtx mult2, rtx add1, enum machine_mode mode)
 {
-  /* For architectures with a decrement_and_branch_until_zero insn,
-     don't do this if we put a REG_NONNEG note on the endtest for this
-     biv.  */
+  rtx temp, mult_res;
+  rtx result;
 
-#ifdef HAVE_decrement_and_branch_until_zero
-  if (bl->nonneg)
+  /* The modes must all be the same.  This should always be true.  For now,
+     check to make sure.  */
+  gcc_assert (GET_MODE (mult1) == mode || GET_MODE (mult1) == VOIDmode);
+  gcc_assert (GET_MODE (mult2) == mode || GET_MODE (mult2) == VOIDmode);
+  gcc_assert (GET_MODE (add1) == mode || GET_MODE (add1) == VOIDmode);
+
+  /* Ensure that if at least one of mult1/mult2 are constant, then mult2
+     will be a constant.  */
+  if (GET_CODE (mult1) == CONST_INT)
     {
-      if (loop_dump_stream)
-       fprintf (loop_dump_stream,
-                "Cannot eliminate nonneg biv %d.\n", bl->regno);
-      return 0;
+      temp = mult2;
+      mult2 = mult1;
+      mult1 = temp;
     }
-#endif
-
-  /* Check that biv is used outside loop or if it has a final value.
-     Compare against bl->init_insn rather than loop->start.  We aren't
-     concerned with any uses of the biv between init_insn and
-     loop->start since these won't be affected by the value of the biv
-     elsewhere in the function, so long as init_insn doesn't use the
-     biv itself.  */
 
-  if ((REGNO_LAST_LUID (bl->regno) < INSN_LUID (loop->end)
-       && bl->init_insn
-       && INSN_UID (bl->init_insn) < max_uid_for_loop
-       && REGNO_FIRST_LUID (bl->regno) >= INSN_LUID (bl->init_insn)
-       && ! reg_mentioned_p (bl->biv->dest_reg, SET_SRC (bl->init_set)))
-      || (bl->final_value = final_biv_value (loop, bl)))
-    return maybe_eliminate_biv (loop, bl, 0, threshold,        insn_count);
+  mult_res = simplify_binary_operation (MULT, mode, mult1, mult2);
+  if (! mult_res)
+    mult_res = gen_rtx_MULT (mode, mult1, mult2);
 
-  if (loop_dump_stream)
+  /* Again, put the constant second.  */
+  if (GET_CODE (add1) == CONST_INT)
     {
-      fprintf (loop_dump_stream,
-              "Cannot eliminate biv %d.\n",
-              bl->regno);
-      fprintf (loop_dump_stream,
-              "First use: insn %d, last use: insn %d.\n",
-              REGNO_FIRST_UID (bl->regno),
-              REGNO_LAST_UID (bl->regno));
+      temp = add1;
+      add1 = mult_res;
+      mult_res = temp;
     }
-  return 0;
+
+  result = simplify_binary_operation (PLUS, mode, add1, mult_res);
+  if (! result)
+    result = gen_rtx_PLUS (mode, add1, mult_res);
+
+  return result;
 }
 
+/* Searches the list of induction struct's for the biv BL, to try to calculate
+   the total increment value for one iteration of the loop as a constant.
 
-/* Reduce each giv of BL that we have decided to reduce.  */
+   Returns the increment value as an rtx, simplified as much as possible,
+   if it can be calculated.  Otherwise, returns 0.  */
 
-static void
-loop_givs_reduce (loop, bl)
-     struct loop *loop;
-     struct iv_class *bl;
+static rtx
+biv_total_increment (const struct iv_class *bl)
 {
   struct induction *v;
+  rtx result;
 
-  for (v = bl->giv; v; v = v->next_iv)
+  /* For increment, must check every instruction that sets it.  Each
+     instruction must be executed only once each time through the loop.
+     To verify this, we check that the insn is always executed, and that
+     there are no backward branches after the insn that branch to before it.
+     Also, the insn must have a mult_val of one (to make sure it really is
+     an increment).  */
+
+  result = const0_rtx;
+  for (v = bl->biv; v; v = v->next_iv)
     {
-      struct induction *tv;
-      if (! v->ignore && v->same == 0)
+      if (v->always_computable && v->mult_val == const1_rtx
+         && ! v->maybe_multiple
+         && SCALAR_INT_MODE_P (v->mode))
        {
-         int auto_inc_opt = 0;
+         /* If we have already counted it, skip it.  */
+         if (v->same)
+           continue;
 
-         /* If the code for derived givs immediately below has already
-            allocated a new_reg, we must keep it.  */
-         if (! v->new_reg)
-           v->new_reg = gen_reg_rtx (v->mode);
+         result = fold_rtx_mult_add (result, const1_rtx, v->add_val, v->mode);
+       }
+      else
+       return 0;
+    }
 
-#ifdef AUTO_INC_DEC
-         /* If the target has auto-increment addressing modes, and
-            this is an address giv, then try to put the increment
-            immediately after its use, so that flow can create an
-            auto-increment addressing mode.  */
-         if (v->giv_type == DEST_ADDR && bl->biv_count == 1
-             && bl->biv->always_executed && ! bl->biv->maybe_multiple
-             /* We don't handle reversed biv's because bl->biv->insn
-                does not have a valid INSN_LUID.  */
-             && ! bl->reversed
-             && v->always_executed && ! v->maybe_multiple
-             && INSN_UID (v->insn) < max_uid_for_loop)
-           {
-             /* If other giv's have been combined with this one, then
-                this will work only if all uses of the other giv's occur
-                before this giv's insn.  This is difficult to check.
+  return result;
+}
+
+/* Try to prove that the register is dead after the loop exits.  Trace every
+   loop exit looking for an insn that will always be executed, which sets
+   the register to some value, and appears before the first use of the register
+   is found.  If successful, then return 1, otherwise return 0.  */
+
+/* ?? Could be made more intelligent in the handling of jumps, so that
+   it can search past if statements and other similar structures.  */
+
+static int
+reg_dead_after_loop (const struct loop *loop, rtx reg)
+{
+  rtx insn, label;
+  int jump_count = 0;
+  int label_count = 0;
+
+  /* In addition to checking all exits of this loop, we must also check
+     all exits of inner nested loops that would exit this loop.  We don't
+     have any way to identify those, so we just give up if there are any
+     such inner loop exits.  */
+
+  for (label = loop->exit_labels; label; label = LABEL_NEXTREF (label))
+    label_count++;
+
+  if (label_count != loop->exit_count)
+    return 0;
+
+  /* HACK: Must also search the loop fall through exit, create a label_ref
+     here which points to the loop->end, and append the loop_number_exit_labels
+     list to it.  */
+  label = gen_rtx_LABEL_REF (Pmode, loop->end);
+  LABEL_NEXTREF (label) = loop->exit_labels;
+
+  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
+        a conditional jump.  */
+
+      insn = NEXT_INSN (XEXP (label, 0));
+      while (insn)
+       {
+         if (INSN_P (insn))
+           {
+             rtx set, note;
+
+             if (reg_referenced_p (reg, PATTERN (insn)))
+               return 0;
+
+             note = find_reg_equal_equiv_note (insn);
+             if (note && reg_overlap_mentioned_p (reg, XEXP (note, 0)))
+               return 0;
+
+             set = single_set (insn);
+             if (set && rtx_equal_p (SET_DEST (set), reg))
+               break;
+
+             if (JUMP_P (insn))
+               {
+                 if (GET_CODE (PATTERN (insn)) == RETURN)
+                   break;
+                 else if (!any_uncondjump_p (insn)
+                          /* Prevent infinite loop following infinite loops.  */
+                          || jump_count++ > 20)
+                   return 0;
+                 else
+                   insn = JUMP_LABEL (insn);
+               }
+           }
+
+         insn = NEXT_INSN (insn);
+       }
+    }
+
+  /* Success, the register is dead on all loop exits.  */
+  return 1;
+}
+
+/* Try to calculate the final value of the biv, the value it will have at
+   the end of the loop.  If we can do it, return that value.  */
+
+static rtx
+final_biv_value (const struct loop *loop, struct iv_class *bl)
+{
+  unsigned HOST_WIDE_INT n_iterations = LOOP_INFO (loop)->n_iterations;
+  rtx increment, tem;
+
+  /* ??? This only works for MODE_INT biv's.  Reject all others for now.  */
+
+  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
+     no other loop exits, so we can return any value.  */
+  if (bl->reversed)
+    {
+      if (loop_dump_stream)
+       fprintf (loop_dump_stream,
+                "Final biv value for %d, reversed biv.\n", bl->regno);
+
+      return const0_rtx;
+    }
+
+  /* Try to calculate the final value as initial value + (number of iterations
+     * increment).  For this to work, increment must be invariant, the only
+     exit from the loop must be the fall through at the bottom (otherwise
+     it may not have its final value when the loop exits), and the initial
+     value of the biv must be invariant.  */
+
+  if (n_iterations != 0
+      && ! loop->exit_count
+      && loop_invariant_p (loop, bl->initial_value))
+    {
+      increment = biv_total_increment (bl);
+
+      if (increment && loop_invariant_p (loop, increment))
+       {
+         /* Can calculate the loop exit value, emit insns after loop
+            end to calculate this value into a temporary register in
+            case it is needed later.  */
+
+         tem = gen_reg_rtx (bl->biv->mode);
+         record_base_value (REGNO (tem), bl->biv->add_val, 0);
+         loop_iv_add_mult_sink (loop, increment, GEN_INT (n_iterations),
+                                bl->initial_value, tem);
+
+         if (loop_dump_stream)
+           fprintf (loop_dump_stream,
+                    "Final biv value for %d, calculated.\n", bl->regno);
+
+         return tem;
+       }
+    }
+
+  /* Check to see if the biv is dead at all loop exits.  */
+  if (reg_dead_after_loop (loop, bl->biv->src_reg))
+    {
+      if (loop_dump_stream)
+       fprintf (loop_dump_stream,
+                "Final biv value for %d, biv dead after loop exit.\n",
+                bl->regno);
+
+      return const0_rtx;
+    }
+
+  return 0;
+}
+
+/* Return nonzero if it is possible to eliminate the biv BL provided
+   all givs are reduced.  This is possible if either the reg is not
+   used outside the loop, or we can compute what its final value will
+   be.  */
+
+static int
+loop_biv_eliminable_p (struct loop *loop, struct iv_class *bl,
+                      int threshold, int insn_count)
+{
+  /* For architectures with a decrement_and_branch_until_zero insn,
+     don't do this if we put a REG_NONNEG note on the endtest for this
+     biv.  */
+
+#ifdef HAVE_decrement_and_branch_until_zero
+  if (bl->nonneg)
+    {
+      if (loop_dump_stream)
+       fprintf (loop_dump_stream,
+                "Cannot eliminate nonneg biv %d.\n", bl->regno);
+      return 0;
+    }
+#endif
+
+  /* Check that biv is used outside loop or if it has a final value.
+     Compare against bl->init_insn rather than loop->start.  We aren't
+     concerned with any uses of the biv between init_insn and
+     loop->start since these won't be affected by the value of the biv
+     elsewhere in the function, so long as init_insn doesn't use the
+     biv itself.  */
+
+  if ((REGNO_LAST_LUID (bl->regno) < INSN_LUID (loop->end)
+       && bl->init_insn
+       && INSN_UID (bl->init_insn) < max_uid_for_loop
+       && REGNO_FIRST_LUID (bl->regno) >= INSN_LUID (bl->init_insn)
+       && ! reg_mentioned_p (bl->biv->dest_reg, SET_SRC (bl->init_set)))
+      || (bl->final_value = final_biv_value (loop, bl)))
+    return maybe_eliminate_biv (loop, bl, 0, threshold,        insn_count);
+
+  if (loop_dump_stream)
+    {
+      fprintf (loop_dump_stream,
+              "Cannot eliminate biv %d.\n",
+              bl->regno);
+      fprintf (loop_dump_stream,
+              "First use: insn %d, last use: insn %d.\n",
+              REGNO_FIRST_UID (bl->regno),
+              REGNO_LAST_UID (bl->regno));
+    }
+  return 0;
+}
+
+
+/* Reduce each giv of BL that we have decided to reduce.  */
+
+static void
+loop_givs_reduce (struct loop *loop, struct iv_class *bl)
+{
+  struct induction *v;
+
+  for (v = bl->giv; v; v = v->next_iv)
+    {
+      struct induction *tv;
+      if (! v->ignore && v->same == 0)
+       {
+         int auto_inc_opt = 0;
+
+         /* If the code for derived givs immediately below has already
+            allocated a new_reg, we must keep it.  */
+         if (! v->new_reg)
+           v->new_reg = gen_reg_rtx (v->mode);
+
+#ifdef AUTO_INC_DEC
+         /* If the target has auto-increment addressing modes, and
+            this is an address giv, then try to put the increment
+            immediately after its use, so that flow can create an
+            auto-increment addressing mode.  */
+         /* Don't do this for loops entered at the bottom, to avoid
+            this invalid transformation:
+               jmp L;          ->          jmp L;
+            TOP:                       TOP:
+               use giv                     use giv
+            L:                             inc giv
+               inc biv                 L:
+               test biv                    test giv
+               cbr TOP                     cbr TOP
+         */
+         if (v->giv_type == DEST_ADDR && bl->biv_count == 1
+             && bl->biv->always_executed && ! bl->biv->maybe_multiple
+             /* We don't handle reversed biv's because bl->biv->insn
+                does not have a valid INSN_LUID.  */
+             && ! bl->reversed
+             && v->always_executed && ! v->maybe_multiple
+             && INSN_UID (v->insn) < max_uid_for_loop
+             && !loop->top)    
+           {
+             /* If other giv's have been combined with this one, then
+                this will work only if all uses of the other giv's occur
+                before this giv's insn.  This is difficult to check.
 
                 We simplify this by looking for the common case where
                 there is one DEST_REG giv, and this giv's insn is the
@@ -4824,6 +5362,9 @@ loop_givs_reduce (loop, bl)
            {
              rtx insert_before;
 
+             /* Skip if location is the same as a previous one.  */
+             if (tv->same)
+               continue;
              if (! auto_inc_opt)
                insert_before = NEXT_INSN (tv->insn);
              else if (auto_inc_opt == 1)
@@ -4859,9 +5400,7 @@ loop_givs_reduce (loop, bl)
    eliminate a biv.  */
 
 static void
-loop_givs_dead_check (loop, bl)
-     struct loop *loop ATTRIBUTE_UNUSED;
-     struct iv_class *bl;
+loop_givs_dead_check (struct loop *loop ATTRIBUTE_UNUSED, struct iv_class *bl)
 {
   struct induction *v;
 
@@ -4885,10 +5424,7 @@ loop_givs_dead_check (loop, bl)
 
 
 static void
-loop_givs_rescan (loop, bl, reg_map)
-     struct loop *loop;
-     struct iv_class *bl;
-     rtx *reg_map;
+loop_givs_rescan (struct loop *loop, struct iv_class *bl, rtx *reg_map)
 {
   struct induction *v;
 
@@ -4913,12 +5449,12 @@ loop_givs_rescan (loop, bl, reg_map)
         computational information.  If not, and this is a DEST_ADDR
         giv, at least we know that it's a pointer, though we don't know
         the alignment.  */
-      if (GET_CODE (v->new_reg) == REG
+      if (REG_P (v->new_reg)
          && v->giv_type == DEST_REG
          && REG_POINTER (v->dest_reg))
        mark_reg_pointer (v->new_reg,
                          REGNO_POINTER_ALIGN (REGNO (v->dest_reg)));
-      else if (GET_CODE (v->new_reg) == REG
+      else if (REG_P (v->new_reg)
               && REG_POINTER (v->src_reg))
        {
          unsigned int align = REGNO_POINTER_ALIGN (REGNO (v->src_reg));
@@ -4930,8 +5466,8 @@ loop_givs_rescan (loop, bl, reg_map)
 
          mark_reg_pointer (v->new_reg, align);
        }
-      else if (GET_CODE (v->new_reg) == REG
-              && GET_CODE (v->add_val) == REG
+      else if (REG_P (v->new_reg)
+              && REG_P (v->add_val)
               && REG_POINTER (v->add_val))
        {
          unsigned int align = REGNO_POINTER_ALIGN (REGNO (v->add_val));
@@ -4942,13 +5478,49 @@ loop_givs_rescan (loop, bl, reg_map)
 
          mark_reg_pointer (v->new_reg, align);
        }
-      else if (GET_CODE (v->new_reg) == REG && v->giv_type == DEST_ADDR)
+      else if (REG_P (v->new_reg) && v->giv_type == DEST_ADDR)
        mark_reg_pointer (v->new_reg, 0);
 
       if (v->giv_type == DEST_ADDR)
-       /* Store reduced reg as the address in the memref where we found
-          this giv.  */
-       validate_change (v->insn, v->location, v->new_reg, 0);
+       {
+         /* Store reduced reg as the address in the memref where we found
+            this giv.  */
+         if (validate_change_maybe_volatile (v->insn, v->location,
+                                             v->new_reg))
+           /* Yay, it worked!  */;
+         /* Not replaceable; emit an insn to set the original
+            giv reg from the reduced giv.  */
+         else if (REG_P (*v->location))
+           loop_insn_emit_before (loop, 0, v->insn,
+                                  gen_move_insn (*v->location,
+                                                 v->new_reg));
+         else if (GET_CODE (*v->location) == PLUS
+                  && REG_P (XEXP (*v->location, 0))
+                  && CONSTANT_P (XEXP (*v->location, 1)))
+           {
+             rtx tem;
+             start_sequence ();
+             tem = expand_simple_binop (GET_MODE (*v->location), MINUS,
+                                        v->new_reg, XEXP (*v->location, 1),
+                                        NULL_RTX, 0, OPTAB_LIB_WIDEN);
+             emit_move_insn (XEXP (*v->location, 0), tem);
+             tem = get_insns ();
+             end_sequence ();
+             loop_insn_emit_before (loop, 0, v->insn, tem);
+           }
+         else
+           {
+             /* If it wasn't a reg, create a pseudo and use that.  */
+             rtx reg, seq;
+             start_sequence ();
+             reg = force_reg (v->mode, *v->location);
+             seq = get_insns ();
+             end_sequence ();
+             loop_insn_emit_before (loop, 0, v->insn, seq);
+             if (!validate_change_maybe_volatile (v->insn, v->location, reg))
+               gcc_unreachable ();
+           }
+       }
       else if (v->replaceable)
        {
          reg_map[REGNO (v->dest_reg)] = v->new_reg;
@@ -4964,13 +5536,13 @@ loop_givs_rescan (loop, bl, reg_map)
                                          gen_move_insn (v->dest_reg,
                                                         v->new_reg));
 
-         /* The original insn may have a REG_EQUAL note.  This note is
-            now incorrect and may result in invalid substitutions later.
-            The original insn is dead, but may be part of a libcall
-            sequence, which doesn't seem worth the bother of handling.  */
-         note = find_reg_note (original_insn, REG_EQUAL, NULL_RTX);
-         if (note)
-           remove_note (original_insn, note);
+         /* The original insn may have a REG_EQUAL note.  This note is
+            now incorrect and may result in invalid substitutions later.
+            The original insn is dead, but may be part of a libcall
+            sequence, which doesn't seem worth the bother of handling.  */
+         note = find_reg_note (original_insn, REG_EQUAL, NULL_RTX);
+         if (note)
+           remove_note (original_insn, note);
        }
 
       /* When a loop is reversed, givs which depend on the reversed
@@ -4999,11 +5571,9 @@ loop_givs_rescan (loop, bl, reg_map)
 
 
 static int
-loop_giv_reduce_benefit (loop, bl, v, test_reg)
-     struct loop *loop ATTRIBUTE_UNUSED;
-     struct iv_class *bl;
-     struct induction *v;
-     rtx test_reg;
+loop_giv_reduce_benefit (struct loop *loop ATTRIBUTE_UNUSED,
+                        struct iv_class *bl, struct induction *v,
+                        rtx test_reg)
 {
   int add_cost;
   int benefit;
@@ -5067,45 +5637,690 @@ loop_giv_reduce_benefit (loop, bl, v, test_reg)
               && -INTVAL (v->mult_val) == size)
        benefit += add_cost * bl->biv_count;
     }
-#endif
+#endif
+
+  return benefit;
+}
+
+
+/* Free IV structures for LOOP.  */
+
+static void
+loop_ivs_free (struct loop *loop)
+{
+  struct loop_ivs *ivs = LOOP_IVS (loop);
+  struct iv_class *iv = ivs->list;
+
+  free (ivs->regs);
+
+  while (iv)
+    {
+      struct iv_class *next = iv->next;
+      struct induction *induction;
+      struct induction *next_induction;
+
+      for (induction = iv->biv; induction; induction = next_induction)
+       {
+         next_induction = induction->next_iv;
+         free (induction);
+       }
+      for (induction = iv->giv; induction; induction = next_induction)
+       {
+         next_induction = induction->next_iv;
+         free (induction);
+       }
+
+      free (iv);
+      iv = next;
+    }
+}
+
+/* Look back before LOOP->START for the insn that sets REG and return
+   the equivalent constant if there is a REG_EQUAL note otherwise just
+   the SET_SRC of REG.  */
+
+static rtx
+loop_find_equiv_value (const struct loop *loop, rtx reg)
+{
+  rtx loop_start = loop->start;
+  rtx insn, set;
+  rtx ret;
+
+  ret = reg;
+  for (insn = PREV_INSN (loop_start); insn; insn = PREV_INSN (insn))
+    {
+      if (LABEL_P (insn))
+       break;
+
+      else if (INSN_P (insn) && reg_set_p (reg, insn))
+       {
+         /* 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) == reg))
+           {
+             rtx note = find_reg_note (insn, REG_EQUAL, NULL_RTX);
+
+             /* Only use the REG_EQUAL note if it is a constant.
+                Other things, divide in particular, will cause
+                problems later if we use them.  */
+             if (note && GET_CODE (XEXP (note, 0)) != EXPR_LIST
+                 && CONSTANT_P (XEXP (note, 0)))
+               ret = XEXP (note, 0);
+             else
+               ret = SET_SRC (set);
+
+             /* We cannot do this if it changes between the
+                assignment and loop start though.  */
+             if (modified_between_p (ret, insn, loop_start))
+               ret = reg;
+           }
+         break;
+       }
+    }
+  return ret;
+}
+
+/* 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.  */
+
+static rtx
+find_common_reg_term (rtx op0, rtx op1)
+{
+  if ((REG_P (op0) || GET_CODE (op0) == PLUS)
+      && (REG_P (op1) || GET_CODE (op1) == PLUS))
+    {
+      rtx op00;
+      rtx op01;
+      rtx op10;
+      rtx op11;
+
+      if (GET_CODE (op0) == PLUS)
+       op01 = XEXP (op0, 1), op00 = XEXP (op0, 0);
+      else
+       op01 = const0_rtx, op00 = op0;
+
+      if (GET_CODE (op1) == PLUS)
+       op11 = XEXP (op1, 1), op10 = XEXP (op1, 0);
+      else
+       op11 = const0_rtx, op10 = op1;
+
+      /* Find and return common register term if present.  */
+      if (REG_P (op00) && (op00 == op10 || op00 == op11))
+       return op00;
+      else if (REG_P (op01) && (op01 == op10 || op01 == op11))
+       return op01;
+    }
+
+  /* No common register term found.  */
+  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.  */
+
+static unsigned HOST_WIDE_INT
+loop_iterations (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 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;
+  struct iv_class *bl;
+
+  loop_info->n_iterations = 0;
+  loop_info->initial_value = 0;
+  loop_info->initial_equiv_value = 0;
+  loop_info->comparison_value = 0;
+  loop_info->final_value = 0;
+  loop_info->final_equiv_value = 0;
+  loop_info->increment = 0;
+  loop_info->iteration_var = 0;
+  loop_info->iv = 0;
+
+  /* We used to use prev_nonnote_insn here, but that fails because it might
+     accidentally get the branch for a contained loop if the branch for this
+     loop was deleted.  We can only trust branches immediately before the
+     loop_end.  */
+  last_loop_insn = PREV_INSN (loop->end);
+
+  /* ??? We should probably try harder to find the jump insn
+     at the end of the loop.  The following code assumes that
+     the last loop insn is a jump to the top of the loop.  */
+  if (!JUMP_P (last_loop_insn))
+    {
+      if (loop_dump_stream)
+       fprintf (loop_dump_stream,
+                "Loop iterations: No final conditional branch found.\n");
+      return 0;
+    }
+
+  /* If there is a more than a single jump to the top of the loop
+     we cannot (easily) determine the iteration count.  */
+  if (LABEL_NUSES (JUMP_LABEL (last_loop_insn)) > 1)
+    {
+      if (loop_dump_stream)
+       fprintf (loop_dump_stream,
+                "Loop iterations: Loop has multiple back edges.\n");
+      return 0;
+    }
+
+  /* Find the iteration variable.  If the last insn is a conditional
+     branch, and the insn before tests a register value, make that the
+     iteration variable.  */
+
+  comparison = get_condition_for_loop (loop, last_loop_insn);
+  if (comparison == 0)
+    {
+      if (loop_dump_stream)
+       fprintf (loop_dump_stream,
+                "Loop iterations: No final comparison found.\n");
+      return 0;
+    }
+
+  /* ??? Get_condition may switch position of induction variable and
+     invariant register when it canonicalizes the comparison.  */
+
+  comparison_code = GET_CODE (comparison);
+  iteration_var = XEXP (comparison, 0);
+  comparison_value = XEXP (comparison, 1);
+
+  if (!REG_P (iteration_var))
+    {
+      if (loop_dump_stream)
+       fprintf (loop_dump_stream,
+                "Loop iterations: Comparison not against register.\n");
+      return 0;
+    }
+
+  /* The only new registers that are created before loop iterations
+     are givs made from biv increments or registers created by
+     load_mems.  In the latter case, it is possible that try_copy_prop
+     will propagate a new pseudo into the old iteration register but
+     this will be marked by having the REG_USERVAR_P bit set.  */
+
+  gcc_assert ((unsigned) REGNO (iteration_var) < ivs->n_regs
+             || REG_USERVAR_P (iteration_var));
+
+  /* Determine the initial value of the iteration variable, and the amount
+     that it is incremented each loop.  Use the tables constructed by
+     the strength reduction pass to calculate these values.  */
+
+  /* Clear the result values, in case no answer can be found.  */
+  initial_value = 0;
+  increment = 0;
+
+  /* The iteration variable can be either a giv or a biv.  Check to see
+     which it is, and compute the variable's initial value, and increment
+     value if possible.  */
+
+  /* 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) >= ivs->n_regs)
+    {
+      if (loop_dump_stream)
+       fprintf (loop_dump_stream,
+                "Loop iterations: No reg_iv_type entry for iteration var.\n");
+      return 0;
+    }
+
+  /* Reject iteration variables larger than the host wide int size, since they
+     could result in a number of iterations greater than the range of our
+     `unsigned HOST_WIDE_INT' variable loop_info->n_iterations.  */
+  else if ((GET_MODE_BITSIZE (GET_MODE (iteration_var))
+           > HOST_BITS_PER_WIDE_INT))
+    {
+      if (loop_dump_stream)
+       fprintf (loop_dump_stream,
+                "Loop iterations: Iteration var rejected because mode too large.\n");
+      return 0;
+    }
+  else if (GET_MODE_CLASS (GET_MODE (iteration_var)) != MODE_INT)
+    {
+      if (loop_dump_stream)
+       fprintf (loop_dump_stream,
+                "Loop iterations: Iteration var not an integer.\n");
+      return 0;
+    }
+
+  /* Try swapping the comparison to identify a suitable iv.  */
+  if (REG_IV_TYPE (ivs, REGNO (iteration_var)) != BASIC_INDUCT
+      && REG_IV_TYPE (ivs, REGNO (iteration_var)) != GENERAL_INDUCT
+      && REG_P (comparison_value)
+      && REGNO (comparison_value) < ivs->n_regs)
+    {
+      rtx temp = comparison_value;
+      comparison_code = swap_condition (comparison_code);
+      comparison_value = iteration_var;
+      iteration_var = temp;
+    }
+
+  if (REG_IV_TYPE (ivs, REGNO (iteration_var)) == BASIC_INDUCT)
+    {
+      gcc_assert (REGNO (iteration_var) < ivs->n_regs);
+
+      /* Grab initial value, only useful if it is a constant.  */
+      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 (ivs, REGNO (iteration_var)) == GENERAL_INDUCT)
+    {
+      HOST_WIDE_INT offset = 0;
+      struct induction *v = REG_IV_INFO (ivs, REGNO (iteration_var));
+      rtx biv_initial_value;
+
+      gcc_assert (REGNO (v->src_reg) < ivs->n_regs);
+
+      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.  */
+
+      increment = biv_total_increment (bl);
+      if (increment)
+       {
+         struct induction *biv_inc;
+
+         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 occurred 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.:
+            i = 6; do {;} while (i++ < 9) .
+            Therefore, we bias the initial value by subtracting the amount of
+            the increment that occurs between the giv set and the giv test.  */
+         for (biv_inc = bl->biv; biv_inc; biv_inc = biv_inc->next_iv)
+           {
+             if (loop_insn_first_p (v->insn, biv_inc->insn))
+               {
+                 if (REG_P (biv_inc->add_val))
+                   {
+                     if (loop_dump_stream)
+                       fprintf (loop_dump_stream,
+                                "Loop iterations: Basic induction var add_val is REG %d.\n",
+                                REGNO (biv_inc->add_val));
+                       return 0;
+                   }
+
+                 /* If we have already counted it, skip it.  */
+                 if (biv_inc->same)
+                   continue;
+
+                 offset -= INTVAL (biv_inc->add_val);
+               }
+           }
+       }
+      if (loop_dump_stream)
+       fprintf (loop_dump_stream,
+                "Loop iterations: Giv iterator, initial value bias %ld.\n",
+                (long) offset);
+
+      /* Initial value is mult_val times the biv's initial value plus
+        add_val.  Only useful if it is a constant.  */
+      biv_initial_value = extend_value_for_giv (v, bl->initial_value);
+      initial_value
+       = fold_rtx_mult_add (v->mult_val,
+                            plus_constant (biv_initial_value, offset),
+                            v->add_val, v->mode);
+    }
+  else
+    {
+      if (loop_dump_stream)
+       fprintf (loop_dump_stream,
+                "Loop iterations: Not basic or general induction var.\n");
+      return 0;
+    }
 
-  return benefit;
-}
+  if (initial_value == 0)
+    return 0;
 
+  unsigned_p = 0;
+  off_by_one = 0;
+  switch (comparison_code)
+    {
+    case LEU:
+      unsigned_p = 1;
+    case LE:
+      compare_dir = 1;
+      off_by_one = 1;
+      break;
+    case GEU:
+      unsigned_p = 1;
+    case GE:
+      compare_dir = -1;
+      off_by_one = -1;
+      break;
+    case EQ:
+      /* Cannot determine loop iterations with this case.  */
+      compare_dir = 0;
+      break;
+    case LTU:
+      unsigned_p = 1;
+    case LT:
+      compare_dir = 1;
+      break;
+    case GTU:
+      unsigned_p = 1;
+    case GT:
+      compare_dir = -1;
+      break;
+    case NE:
+      compare_dir = 0;
+      break;
+    default:
+      gcc_unreachable ();
+    }
+
+  /* If the comparison value is an invariant register, then try to find
+     its value from the insns before the start of the loop.  */
+
+  final_value = comparison_value;
+  if (REG_P (comparison_value)
+      && loop_invariant_p (loop, comparison_value))
+    {
+      final_value = loop_find_equiv_value (loop, comparison_value);
+
+      /* If we don't get an invariant final value, we are better
+        off with the original register.  */
+      if (! loop_invariant_p (loop, final_value))
+       final_value = comparison_value;
+    }
+
+  /* Calculate the approximate final value of the induction variable
+     (on the last successful iteration).  The exact final value
+     depends on the branch operator, and increment sign.  It will be
+     wrong if the iteration variable is not incremented by one each
+     time through the loop and (comparison_value + off_by_one -
+     initial_value) % increment != 0.
+     ??? Note that the final_value may overflow and thus final_larger
+     will be bogus.  A potentially infinite loop will be classified
+     as immediate, e.g. for (i = 0x7ffffff0; i <= 0x7fffffff; i++)  */
+  if (off_by_one)
+    final_value = plus_constant (final_value, off_by_one);
+
+  /* Save the calculated values describing this loop's bounds, in case
+     precondition_loop_p will need them later.  These values can not be
+     recalculated inside precondition_loop_p because strength reduction
+     optimizations may obscure the loop's structure.
+
+     These values are only required by precondition_loop_p and insert_bct
+     whenever the number of iterations cannot be computed at compile time.
+     Only the difference between final_value and initial_value is
+     important.  Note that final_value is only approximate.  */
+  loop_info->initial_value = initial_value;
+  loop_info->comparison_value = comparison_value;
+  loop_info->final_value = plus_constant (comparison_value, off_by_one);
+  loop_info->increment = increment;
+  loop_info->iteration_var = iteration_var;
+  loop_info->comparison_code = comparison_code;
+  loop_info->iv = bl;
+
+  /* Try to determine the iteration count for loops such
+     as (for i = init; i < init + const; i++).  When running the
+     loop optimization twice, the first pass often converts simple
+     loops into this form.  */
+
+  if (REG_P (initial_value))
+    {
+      rtx reg1;
+      rtx reg2;
+      rtx const2;
+
+      reg1 = initial_value;
+      if (GET_CODE (final_value) == PLUS)
+       reg2 = XEXP (final_value, 0), const2 = XEXP (final_value, 1);
+      else
+       reg2 = final_value, const2 = const0_rtx;
 
-/* Free IV structures for LOOP.  */
+      /* Check for initial_value = reg1, final_value = reg2 + const2,
+        where reg1 != reg2.  */
+      if (REG_P (reg2) && reg2 != reg1)
+       {
+         rtx temp;
 
-static void
-loop_ivs_free (loop)
-     struct loop *loop;
-{
-  struct loop_ivs *ivs = LOOP_IVS (loop);
-  struct iv_class *iv = ivs->list;
+         /* Find what reg1 is equivalent to.  Hopefully it will
+            either be reg2 or reg2 plus a constant.  */
+         temp = loop_find_equiv_value (loop, reg1);
 
-  free (ivs->regs);
+         if (find_common_reg_term (temp, reg2))
+           initial_value = temp;
+         else if (loop_invariant_p (loop, reg2))
+           {
+             /* Find what reg2 is equivalent to.  Hopefully it will
+                either be reg1 or reg1 plus a constant.  Let's ignore
+                the latter case for now since it is not so common.  */
+             temp = loop_find_equiv_value (loop, reg2);
+
+             if (temp == loop_info->iteration_var)
+               temp = initial_value;
+             if (temp == reg1)
+               final_value = (const2 == const0_rtx)
+                 ? reg1 : gen_rtx_PLUS (GET_MODE (reg1), reg1, const2);
+           }
+       }
+    }
 
-  while (iv)
+  loop_info->initial_equiv_value = initial_value;
+  loop_info->final_equiv_value = final_value;
+
+  /* For EQ comparison loops, we don't have a valid final value.
+     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;
+
+  if (increment == 0)
     {
-      struct iv_class *next = iv->next;
-      struct induction *induction;
-      struct induction *next_induction;
+      if (loop_dump_stream)
+       fprintf (loop_dump_stream,
+                "Loop iterations: Increment value can't be calculated.\n");
+      return 0;
+    }
 
-      for (induction = iv->biv; induction; induction = next_induction)
+  if (GET_CODE (increment) != CONST_INT)
+    {
+      /* If we have a REG, check to see if REG holds a constant value.  */
+      /* ??? Other RTL, such as (neg (reg)) is possible here, but it isn't
+        clear if it is worthwhile to try to handle such RTL.  */
+      if (REG_P (increment) || GET_CODE (increment) == SUBREG)
+       increment = loop_find_equiv_value (loop, increment);
+
+      if (GET_CODE (increment) != CONST_INT)
        {
-         next_induction = induction->next_iv;
-         free (induction);
+         if (loop_dump_stream)
+           {
+             fprintf (loop_dump_stream,
+                      "Loop iterations: Increment value not constant ");
+             print_simple_rtl (loop_dump_stream, increment);
+             fprintf (loop_dump_stream, ".\n");
+           }
+         return 0;
        }
-      for (induction = iv->giv; induction; induction = next_induction)
+      loop_info->increment = increment;
+    }
+
+  if (GET_CODE (initial_value) != CONST_INT)
+    {
+      if (loop_dump_stream)
        {
-         next_induction = induction->next_iv;
-         free (induction);
+         fprintf (loop_dump_stream,
+                  "Loop iterations: Initial value not constant ");
+         print_simple_rtl (loop_dump_stream, initial_value);
+         fprintf (loop_dump_stream, ".\n");
+       }
+      return 0;
+    }
+  else if (GET_CODE (final_value) != CONST_INT)
+    {
+      if (loop_dump_stream)
+       {
+         fprintf (loop_dump_stream,
+                  "Loop iterations: Final value not constant ");
+         print_simple_rtl (loop_dump_stream, final_value);
+         fprintf (loop_dump_stream, ".\n");
        }
+      return 0;
+    }
+  else if (comparison_code == EQ)
+    {
+      rtx inc_once;
 
-      free (iv);
-      iv = next;
+      if (loop_dump_stream)
+       fprintf (loop_dump_stream, "Loop iterations: EQ comparison loop.\n");
+
+      inc_once = gen_int_mode (INTVAL (initial_value) + INTVAL (increment),
+                              GET_MODE (iteration_var));
+
+      if (inc_once == final_value)
+       {
+         /* The iterator value once through the loop is equal to the
+            comparison value.  Either we have an infinite loop, or
+            we'll loop twice.  */
+         if (increment == const0_rtx)
+           return 0;
+         loop_info->n_iterations = 2;
+       }
+      else
+       loop_info->n_iterations = 1;
+
+      if (GET_CODE (loop_info->initial_value) == CONST_INT)
+       loop_info->final_value
+         = gen_int_mode ((INTVAL (loop_info->initial_value)
+                          + loop_info->n_iterations * INTVAL (increment)),
+                         GET_MODE (iteration_var));
+      else
+       loop_info->final_value
+         = plus_constant (loop_info->initial_value,
+                          loop_info->n_iterations * INTVAL (increment));
+      loop_info->final_equiv_value
+       = gen_int_mode ((INTVAL (initial_value)
+                        + loop_info->n_iterations * INTVAL (increment)),
+                       GET_MODE (iteration_var));
+      return loop_info->n_iterations;
+    }
+
+  /* Final_larger is 1 if final larger, 0 if they are equal, otherwise -1.  */
+  if (unsigned_p)
+    final_larger
+      = ((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));
+
+  if (INTVAL (increment) > 0)
+    increment_dir = 1;
+  else if (INTVAL (increment) == 0)
+    increment_dir = 0;
+  else
+    increment_dir = -1;
+
+  /* There are 27 different cases: compare_dir = -1, 0, 1;
+     final_larger = -1, 0, 1; increment_dir = -1, 0, 1.
+     There are 4 normal cases, 4 reverse cases (where the iteration variable
+     will overflow before the loop exits), 4 infinite loop cases, and 15
+     immediate exit (0 or 1 iteration depending on loop type) cases.
+     Only try to optimize the normal cases.  */
+
+  /* (compare_dir/final_larger/increment_dir)
+     Normal cases: (0/-1/-1), (0/1/1), (-1/-1/-1), (1/1/1)
+     Reverse cases: (0/-1/1), (0/1/-1), (-1/-1/1), (1/1/-1)
+     Infinite loops: (0/-1/0), (0/1/0), (-1/-1/0), (1/1/0)
+     Immediate exit: (0/0/X), (-1/0/X), (-1/1/X), (1/0/X), (1/-1/X) */
+
+  /* ?? If the meaning of reverse loops (where the iteration variable
+     will overflow before the loop exits) is undefined, then could
+     eliminate all of these special checks, and just always assume
+     the loops are normal/immediate/infinite.  Note that this means
+     the sign of increment_dir does not have to be known.  Also,
+     since it does not really hurt if immediate exit loops or infinite loops
+     are optimized, then that case could be ignored also, and hence all
+     loops can be optimized.
+
+     According to ANSI Spec, the reverse loop case result is undefined,
+     because the action on overflow is undefined.
+
+     See also the special test for NE loops below.  */
+
+  if (final_larger == increment_dir && final_larger != 0
+      && (final_larger == compare_dir || compare_dir == 0))
+    /* Normal case.  */
+    ;
+  else
+    {
+      if (loop_dump_stream)
+       fprintf (loop_dump_stream, "Loop iterations: Not normal loop.\n");
+      return 0;
+    }
+
+  /* Calculate the number of iterations, final_value is only an approximation,
+     so correct for that.  Note that abs_diff and n_iterations are
+     unsigned, because they can be as large as 2^n - 1.  */
+
+  inc = INTVAL (increment);
+  gcc_assert (inc);
+  if (inc > 0)
+    {
+      abs_diff = INTVAL (final_value) - INTVAL (initial_value);
+      abs_inc = inc;
+    }
+  else
+    {
+      abs_diff = INTVAL (initial_value) - INTVAL (final_value);
+      abs_inc = -inc;
     }
-}
 
+  /* 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
+     can not calculate the number of iterations.  */
+  if (compare_dir == 0 && (abs_diff % abs_inc) != 0)
+    return 0;
+
+  /* Note that the number of iterations could be calculated using
+     (abs_diff + abs_inc - 1) / abs_inc, provided care was taken to
+     handle potential overflow of the summation.  */
+  loop_info->n_iterations = abs_diff / abs_inc + ((abs_diff % abs_inc) != 0);
+  return loop_info->n_iterations;
+}
 
 /* Perform strength reduction and induction variable elimination.
 
@@ -5117,9 +6332,7 @@ loop_ivs_free (loop)
    must check regnos to make sure they are in bounds.  */
 
 static void
-strength_reduce (loop, flags)
-     struct loop *loop;
-     int flags;
+strength_reduce (struct loop *loop, int flags)
 {
   struct loop_info *loop_info = LOOP_INFO (loop);
   struct loop_regs *regs = LOOP_REGS (loop);
@@ -5136,14 +6349,13 @@ strength_reduce (loop, flags)
   /* Map of pseudo-register replacements.  */
   rtx *reg_map = NULL;
   int reg_map_size;
-  int unrolled_insn_copies = 0;
   rtx test_reg = gen_rtx_REG (word_mode, LAST_VIRTUAL_REGISTER + 1);
   int insn_count = count_insns_in_loop (loop);
 
   addr_placeholder = gen_reg_rtx (Pmode);
 
   ivs->n_regs = max_reg_before_loop;
-  ivs->regs = (struct iv *) xcalloc (ivs->n_regs, sizeof (struct iv));
+  ivs->regs = xcalloc (ivs->n_regs, sizeof (struct iv));
 
   /* Find all BIVs in loop.  */
   loop_bivs_find (loop);
@@ -5151,11 +6363,6 @@ strength_reduce (loop, flags)
   /* Exit if there are no bivs.  */
   if (! ivs->list)
     {
-      /* Can still unroll the loop anyways, but indicate that there is no
-        strength reduction info available.  */
-      if (flags & LOOP_UNROLL)
-       unroll_loop (loop, insn_count, 0);
-
       loop_ivs_free (loop);
       return;
     }
@@ -5197,7 +6404,7 @@ strength_reduce (loop, flags)
      Some givs might have been made from biv increments, so look at
      ivs->reg_iv_type for a suitable size.  */
   reg_map_size = ivs->n_regs;
-  reg_map = (rtx *) xcalloc (reg_map_size, sizeof (rtx));
+  reg_map = xcalloc (reg_map_size, sizeof (rtx));
 
   /* Examine each iv class for feasibility of strength reduction/induction
      variable elimination.  */
@@ -5218,7 +6425,7 @@ strength_reduce (loop, flags)
 
       /* Check each extension dependent giv in this class to see if its
         root biv is safe from wrapping in the interior mode.  */
-      check_ext_dependent_givs (bl, loop_info);
+      check_ext_dependent_givs (loop, bl);
 
       /* Combine all giv's for this iv_class.  */
       combine_givs (regs, bl);
@@ -5241,8 +6448,7 @@ strength_reduce (loop, flags)
             of such giv's whether or not we know they are used after the loop
             exit.  */
 
-         if (! flag_reduce_all_givs
-             && v->lifetime * threshold * benefit < insn_count
+         if (v->lifetime * threshold * benefit < insn_count
              && ! bl->reversed)
            {
              if (loop_dump_stream)
@@ -5361,68 +6567,13 @@ strength_reduce (loop, flags)
      register substitutions scheduled in REG_MAP.  */
 
   for (p = loop->start; p != loop->end; p = NEXT_INSN (p))
-    if (GET_CODE (p) == INSN || GET_CODE (p) == JUMP_INSN
-       || GET_CODE (p) == CALL_INSN)
+    if (INSN_P (p))
       {
        replace_regs (PATTERN (p), reg_map, reg_map_size, 0);
        replace_regs (REG_NOTES (p), reg_map, reg_map_size, 0);
        INSN_CODE (p) = -1;
       }
 
-  if (loop_info->n_iterations > 0)
-    {
-      /* When we completely unroll a loop we will likely not need the increment
-        of the loop BIV and we will not need the conditional branch at the
-        end of the loop.  */
-      unrolled_insn_copies = insn_count - 2;
-
-#ifdef HAVE_cc0
-      /* When we completely unroll a loop on a HAVE_cc0 machine we will not
-        need the comparison before the conditional branch at the end of the
-        loop.  */
-      unrolled_insn_copies -= 1;
-#endif
-
-      /* We'll need one copy for each loop iteration.  */
-      unrolled_insn_copies *= loop_info->n_iterations;
-
-      /* A little slop to account for the ability to remove initialization
-        code, better CSE, and other secondary benefits of completely
-        unrolling some loops.  */
-      unrolled_insn_copies -= 1;
-
-      /* Clamp the value.  */
-      if (unrolled_insn_copies < 0)
-       unrolled_insn_copies = 0;
-    }
-
-  /* Unroll loops from within strength reduction so that we can use the
-     induction variable information that strength_reduce has already
-     collected.  Always unroll loops that would be as small or smaller
-     unrolled than when rolled.  */
-  if ((flags & LOOP_UNROLL)
-      || ((flags & LOOP_AUTO_UNROLL)
-         && loop_info->n_iterations > 0
-         && unrolled_insn_copies <= insn_count))
-    unroll_loop (loop, insn_count, 1);
-
-#ifdef HAVE_doloop_end
-  if (HAVE_doloop_end && (flags & LOOP_BCT) && flag_branch_on_count_reg)
-    doloop_optimize (loop);
-#endif  /* HAVE_doloop_end  */
-
-  /* In case number of iterations is known, drop branch prediction note
-     in the branch.  Do that only in second loop pass, as loop unrolling
-     may change the number of iterations performed.  */
-  if (flags & LOOP_BCT)
-    {
-      unsigned HOST_WIDE_INT n
-       = loop_info->n_iterations / loop_info->unroll_number;
-      if (n > 1)
-       predict_insn (prev_nonnote_insn (loop->end), PRED_LOOP_ITERATIONS,
-                     REG_BR_PROB_BASE - REG_BR_PROB_BASE / n);
-    }
-
   if (loop_dump_stream)
     fprintf (loop_dump_stream, "\n");
 
@@ -5433,11 +6584,8 @@ strength_reduce (loop, flags)
 \f
 /*Record all basic induction variables calculated in the insn.  */
 static rtx
-check_insn_for_bivs (loop, p, not_every_iteration, maybe_multiple)
-     struct loop *loop;
-     rtx p;
-     int not_every_iteration;
-     int maybe_multiple;
+check_insn_for_bivs (struct loop *loop, rtx p, int not_every_iteration,
+                    int maybe_multiple)
 {
   struct loop_ivs *ivs = LOOP_IVS (loop);
   rtx set;
@@ -5446,9 +6594,9 @@ check_insn_for_bivs (loop, p, not_every_iteration, maybe_multiple)
   rtx mult_val;
   rtx *location;
 
-  if (GET_CODE (p) == INSN
+  if (NONJUMP_INSN_P (p)
       && (set = single_set (p))
-      && GET_CODE (SET_DEST (set)) == REG)
+      && REG_P (SET_DEST (set)))
     {
       dest_reg = SET_DEST (set);
       if (REGNO (dest_reg) < max_reg_before_loop
@@ -5463,8 +6611,7 @@ check_insn_for_bivs (loop, p, not_every_iteration, maybe_multiple)
              /* It is a possible basic induction variable.
                 Create and initialize an induction structure for it.  */
 
-             struct induction *v
-               = (struct induction *) xmalloc (sizeof (struct induction));
+             struct induction *v = xmalloc (sizeof (struct induction));
 
              record_biv (loop, v, p, dest_reg, inc_val, mult_val, location,
                          not_every_iteration, maybe_multiple);
@@ -5481,19 +6628,16 @@ check_insn_for_bivs (loop, p, not_every_iteration, maybe_multiple)
    A register is a giv if: it is only set once, it is a function of a
    biv and a constant (or invariant), and it is not a biv.  */
 static rtx
-check_insn_for_givs (loop, p, not_every_iteration, maybe_multiple)
-     struct loop *loop;
-     rtx p;
-     int not_every_iteration;
-     int maybe_multiple;
+check_insn_for_givs (struct loop *loop, rtx p, int not_every_iteration,
+                    int maybe_multiple)
 {
   struct loop_regs *regs = LOOP_REGS (loop);
 
   rtx set;
   /* Look for a general induction variable in a register.  */
-  if (GET_CODE (p) == INSN
+  if (NONJUMP_INSN_P (p)
       && (set = single_set (p))
-      && GET_CODE (SET_DEST (set)) == REG
+      && REG_P (SET_DEST (set))
       && ! regs->array[REGNO (SET_DEST (set))].may_not_optimize)
     {
       rtx src_reg;
@@ -5530,8 +6674,7 @@ check_insn_for_givs (loop, p, not_every_iteration, maybe_multiple)
                                             &add_val, &mult_val, &ext_val,
                                             &last_consec_insn))))
        {
-         struct induction *v
-           = (struct induction *) xmalloc (sizeof (struct induction));
+         struct induction *v = xmalloc (sizeof (struct induction));
 
          /* If this is a library call, increase benefit.  */
          if (find_reg_note (p, REG_RETVAL, NULL_RTX))
@@ -5548,19 +6691,14 @@ check_insn_for_givs (loop, p, not_every_iteration, maybe_multiple)
        }
     }
 
-#ifndef DONT_REDUCE_ADDR
   /* Look for givs which are memory addresses.  */
-  /* This resulted in worse code on a VAX 8600.  I wonder if it
-     still does.  */
-  if (GET_CODE (p) == INSN)
+  if (NONJUMP_INSN_P (p))
     find_mem_givs (loop, PATTERN (p), p, not_every_iteration,
                   maybe_multiple);
-#endif
 
   /* Update the status of whether giv can derive other givs.  This can
      change when we pass a label or an insn that updates a biv.  */
-  if (GET_CODE (p) == INSN || GET_CODE (p) == JUMP_INSN
-      || GET_CODE (p) == CODE_LABEL)
+  if (INSN_P (p) || LABEL_P (p))
     update_giv_derive (loop, p);
   return p;
 }
@@ -5574,18 +6712,14 @@ check_insn_for_givs (loop, p, not_every_iteration, maybe_multiple)
    INSN is the insn containing X.  */
 
 static int
-valid_initial_value_p (x, insn, call_seen, loop_start)
-     rtx x;
-     rtx insn;
-     int call_seen;
-     rtx loop_start;
+valid_initial_value_p (rtx x, rtx insn, int call_seen, rtx loop_start)
 {
   if (CONSTANT_P (x))
     return 1;
 
   /* Only consider pseudos we know about initialized in insns whose luids
      we know.  */
-  if (GET_CODE (x) != REG
+  if (!REG_P (x)
       || REGNO (x) >= max_reg_before_loop)
     return 0;
 
@@ -5593,7 +6727,7 @@ valid_initial_value_p (x, insn, call_seen, loop_start)
      some machines, don't use any hard registers at all.  */
   if (REGNO (x) < FIRST_PSEUDO_REGISTER
       && (SMALL_REGISTER_CLASSES
-         || (call_used_regs[REGNO (x)] && call_seen)))
+         || (call_seen && call_used_regs[REGNO (x)])))
     return 0;
 
   /* Don't use registers that have been clobbered before the start of the
@@ -5611,11 +6745,8 @@ valid_initial_value_p (x, insn, call_seen, loop_start)
    more than once in each loop iteration.  */
 
 static void
-find_mem_givs (loop, x, insn, not_every_iteration, maybe_multiple)
-     const struct loop *loop;
-     rtx x;
-     rtx insn;
-     int not_every_iteration, maybe_multiple;
+find_mem_givs (const struct loop *loop, rtx x, rtx insn,
+              int not_every_iteration, int maybe_multiple)
 {
   int i, j;
   enum rtx_code code;
@@ -5659,8 +6790,7 @@ find_mem_givs (loop, x, insn, not_every_iteration, maybe_multiple)
                                   GET_MODE (x)))
          {
            /* Found one; record it.  */
-           struct induction *v
-             = (struct induction *) xmalloc (sizeof (struct induction));
+           struct induction *v = xmalloc (sizeof (struct induction));
 
            record_giv (loop, v, insn, src_reg, addr_placeholder, mult_val,
                        add_val, ext_val, benefit, DEST_ADDR,
@@ -5705,17 +6835,9 @@ find_mem_givs (loop, x, insn, not_every_iteration, maybe_multiple)
    executed exactly once per iteration.  */
 
 static void
-record_biv (loop, v, insn, dest_reg, inc_val, mult_val, location,
-           not_every_iteration, maybe_multiple)
-     struct loop *loop;
-     struct induction *v;
-     rtx insn;
-     rtx dest_reg;
-     rtx inc_val;
-     rtx mult_val;
-     rtx *location;
-     int not_every_iteration;
-     int maybe_multiple;
+record_biv (struct loop *loop, struct induction *v, rtx insn, rtx dest_reg,
+           rtx inc_val, rtx mult_val, rtx *location,
+           int not_every_iteration, int maybe_multiple)
 {
   struct loop_ivs *ivs = LOOP_IVS (loop);
   struct iv_class *bl;
@@ -5731,6 +6853,7 @@ record_biv (loop, v, insn, dest_reg, inc_val, mult_val, location,
   v->always_computable = ! not_every_iteration;
   v->always_executed = ! not_every_iteration;
   v->maybe_multiple = maybe_multiple;
+  v->same = 0;
 
   /* Add this to the reg's iv_class, creating a class
      if this is the first incrementation of the reg.  */
@@ -5740,7 +6863,7 @@ record_biv (loop, v, insn, dest_reg, inc_val, mult_val, location,
     {
       /* Create and initialize new iv_class.  */
 
-      bl = (struct iv_class *) xmalloc (sizeof (struct iv_class));
+      bl = xmalloc (sizeof (struct iv_class));
 
       bl->regno = REGNO (dest_reg);
       bl->biv = 0;
@@ -5751,7 +6874,7 @@ record_biv (loop, v, insn, dest_reg, inc_val, mult_val, location,
       /* Set initial value to the reg itself.  */
       bl->initial_value = dest_reg;
       bl->final_value = 0;
-      /* We haven't seen the initializing insn yet */
+      /* We haven't seen the initializing insn yet */
       bl->init_insn = 0;
       bl->init_set = 0;
       bl->initial_test = 0;
@@ -5768,6 +6891,17 @@ record_biv (loop, v, insn, dest_reg, inc_val, mult_val, location,
       /* Put it in the array of biv register classes.  */
       REG_IV_CLASS (ivs, REGNO (dest_reg)) = bl;
     }
+  else
+    {
+      /* Check if location is the same as a previous one.  */
+      struct induction *induction;
+      for (induction = bl->biv; induction; induction = induction->next_iv)
+       if (location == induction->location)
+         {
+           v->same = induction;
+           break;
+         }
+    }
 
   /* Update IV_CLASS entry for this biv.  */
   v->next_iv = bl->biv;
@@ -5794,18 +6928,10 @@ record_biv (loop, v, insn, dest_reg, inc_val, mult_val, location,
    LOCATION points to the place where this giv's value appears in INSN.  */
 
 static void
-record_giv (loop, v, insn, src_reg, dest_reg, mult_val, add_val, ext_val,
-           benefit, type, not_every_iteration, maybe_multiple, location)
-     const struct loop *loop;
-     struct induction *v;
-     rtx insn;
-     rtx src_reg;
-     rtx dest_reg;
-     rtx mult_val, add_val, ext_val;
-     int benefit;
-     enum g_types type;
-     int not_every_iteration, maybe_multiple;
-     rtx *location;
+record_giv (const struct loop *loop, struct induction *v, rtx insn,
+           rtx src_reg, rtx dest_reg, rtx mult_val, rtx add_val,
+           rtx ext_val, int benefit, enum g_types type,
+           int not_every_iteration, int maybe_multiple, rtx *location)
 {
   struct loop_ivs *ivs = LOOP_IVS (loop);
   struct induction *b;
@@ -5841,7 +6967,6 @@ record_giv (loop, v, insn, src_reg, dest_reg, mult_val, add_val, ext_val,
   v->final_value = 0;
   v->same_insn = 0;
   v->auto_inc_opt = 0;
-  v->unrolled = 0;
   v->shared = 0;
 
   /* The v->always_computable field is used in update_giv_derive, to
@@ -5883,19 +7008,15 @@ record_giv (loop, v, insn, src_reg, dest_reg, mult_val, add_val, ext_val,
   /* Add the giv to the class of givs computed from one biv.  */
 
   bl = REG_IV_CLASS (ivs, REGNO (src_reg));
-  if (bl)
-    {
-      v->next_iv = bl->giv;
-      bl->giv = v;
-      /* Don't count DEST_ADDR.  This is supposed to count the number of
-        insns that calculate givs.  */
-      if (type == DEST_REG)
-       bl->giv_count++;
-      bl->total_benefit += benefit;
-    }
-  else
-    /* Fatal error, biv missing for this giv?  */
-    abort ();
+  gcc_assert (bl);
+  v->next_iv = bl->giv;
+  bl->giv = v;
+  
+  /* Don't count DEST_ADDR.  This is supposed to count the number of
+     insns that calculate givs.  */
+  if (type == DEST_REG)
+    bl->giv_count++;
+  bl->total_benefit += benefit;
 
   if (type == DEST_ADDR)
     {
@@ -6001,6 +7122,131 @@ record_giv (loop, v, insn, src_reg, dest_reg, mult_val, add_val, ext_val,
     loop_giv_dump (v, loop_dump_stream, 0);
 }
 
+/* Try to calculate the final value of the giv, the value it will have at
+   the end of the loop.  If we can do it, return that value.  */
+
+static rtx
+final_giv_value (const struct loop *loop, struct induction *v)
+{
+  struct loop_ivs *ivs = LOOP_IVS (loop);
+  struct iv_class *bl;
+  rtx insn;
+  rtx increment, tem;
+  rtx seq;
+  rtx loop_end = loop->end;
+  unsigned HOST_WIDE_INT n_iterations = LOOP_INFO (loop)->n_iterations;
+
+  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
+     insn after the loop which sets this giv's final value (if necessary),
+     and there are no other loop exits, so we can return any value.  */
+  if (bl->reversed)
+    {
+      if (loop_dump_stream)
+       fprintf (loop_dump_stream,
+                "Final giv value for %d, depends on reversed biv\n",
+                REGNO (v->dest_reg));
+      return const0_rtx;
+    }
+
+  /* Try to calculate the final value as a function of the biv it depends
+     upon.  The only exit from the loop must be the fall through at the bottom
+     and the insn that sets the giv must be executed on every iteration
+     (otherwise the giv may not have its final value when the loop exits).  */
+
+  /* ??? Can calculate the final giv value by subtracting off the
+     extra biv increments times the giv's mult_val.  The loop must have
+     only one exit for this to work, but the loop iterations does not need
+     to be known.  */
+
+  if (n_iterations != 0
+      && ! loop->exit_count
+      && v->always_executed)
+    {
+      /* ?? It is tempting to use the biv's value here since these insns will
+        be put after the loop, and hence the biv will have its final value
+        then.  However, this fails if the biv is subsequently eliminated.
+        Perhaps determine whether biv's are eliminable before trying to
+        determine whether giv's are replaceable so that we can use the
+        biv value here if it is not eliminable.  */
+
+      /* We are emitting code after the end of the loop, so we must make
+        sure that bl->initial_value is still valid then.  It will still
+        be valid if it is invariant.  */
+
+      increment = biv_total_increment (bl);
+
+      if (increment && loop_invariant_p (loop, increment)
+         && loop_invariant_p (loop, bl->initial_value))
+       {
+         /* Can calculate the loop exit value of its biv as
+            (n_iterations * increment) + initial_value */
+
+         /* The loop exit value of the giv is then
+            (final_biv_value - extra increments) * mult_val + add_val.
+            The extra increments are any increments to the biv which
+            occur in the loop after the giv's value is calculated.
+            We must search from the insn that sets the giv to the end
+            of the loop to calculate this value.  */
+
+         /* Put the final biv value in tem.  */
+         tem = gen_reg_rtx (v->mode);
+         record_base_value (REGNO (tem), bl->biv->add_val, 0);
+         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;
+              insn = NEXT_INSN (insn))
+           {
+             struct induction *biv;
+
+             for (biv = bl->biv; biv; biv = biv->next_iv)
+               if (biv->insn == insn)
+                 {
+                   start_sequence ();
+                   tem = expand_simple_binop (GET_MODE (tem), MINUS, tem,
+                                              biv->add_val, NULL_RTX, 0,
+                                              OPTAB_LIB_WIDEN);
+                   seq = get_insns ();
+                   end_sequence ();
+                   loop_insn_sink (loop, seq);
+                 }
+           }
+
+         /* Now calculate the giv's final value.  */
+         loop_iv_add_mult_sink (loop, tem, v->mult_val, v->add_val, tem);
+
+         if (loop_dump_stream)
+           fprintf (loop_dump_stream,
+                    "Final giv value for %d, calc from biv's value.\n",
+                    REGNO (v->dest_reg));
+
+         return tem;
+       }
+    }
+
+  /* Replaceable giv's should never reach here.  */
+  gcc_assert (!v->replaceable);
+
+  /* Check to see if the biv is dead at all loop exits.  */
+  if (reg_dead_after_loop (loop, v->dest_reg))
+    {
+      if (loop_dump_stream)
+       fprintf (loop_dump_stream,
+                "Final giv value for %d, giv dead after loop exit.\n",
+                REGNO (v->dest_reg));
+
+      return const0_rtx;
+    }
+
+  return 0;
+}
+
 /* All this does is determine whether a giv can be made replaceable because
    its final value can be calculated.  This code can not be part of record_giv
    above, because final_giv_value requires that the number of loop iterations
@@ -6008,9 +7254,7 @@ record_giv (loop, v, insn, src_reg, dest_reg, mult_val, add_val, ext_val,
    have been identified.  */
 
 static void
-check_final_value (loop, v)
-     const struct loop *loop;
-     struct induction *v;
+check_final_value (const struct loop *loop, struct induction *v)
 {
   rtx final_value = 0;
 
@@ -6077,8 +7321,7 @@ check_final_value (loop, v)
          if (p == v->insn)
            break;
 
-         if (GET_CODE (p) == INSN || GET_CODE (p) == JUMP_INSN
-             || GET_CODE (p) == CALL_INSN)
+         if (INSN_P (p))
            {
              /* It is possible for the BIV increment to use the GIV if we
                 have a cycle.  Thus we must be sure to check each insn for
@@ -6117,7 +7360,7 @@ check_final_value (loop, v)
              if (p == last_giv_use)
                break;
 
-             if (GET_CODE (p) == JUMP_INSN && JUMP_LABEL (p)
+             if (JUMP_P (p) && JUMP_LABEL (p)
                  && LABEL_NAME (JUMP_LABEL (p))
                  && ((loop_insn_first_p (JUMP_LABEL (p), v->insn)
                       && loop_insn_first_p (loop->start, JUMP_LABEL (p)))
@@ -6158,9 +7401,7 @@ check_final_value (loop, v)
    The cases we look at are when a label or an update to a biv is passed.  */
 
 static void
-update_giv_derive (loop, p)
-     const struct loop *loop;
-     rtx p;
+update_giv_derive (const struct loop *loop, rtx p)
 {
   struct loop_ivs *ivs = LOOP_IVS (loop);
   struct iv_class *bl;
@@ -6197,9 +7438,13 @@ update_giv_derive (loop, p)
 
   for (bl = ivs->list; bl; bl = bl->next)
     for (biv = bl->biv; biv; biv = biv->next_iv)
-      if (GET_CODE (p) == CODE_LABEL || GET_CODE (p) == JUMP_INSN
+      if (LABEL_P (p) || JUMP_P (p)
          || biv->insn == p)
        {
+         /* Skip if location is the same as a previous one.  */
+         if (biv->same)
+           continue;
+
          for (giv = bl->giv; giv; giv = giv->next_iv)
            {
              /* If cant_derive is already true, there is no point in
@@ -6209,7 +7454,7 @@ update_giv_derive (loop, p)
 
              /* If this giv is conditionally set and we have passed a label,
                 it cannot derive anything.  */
-             if (GET_CODE (p) == CODE_LABEL && ! giv->always_computable)
+             if (LABEL_P (p) && ! giv->always_computable)
                giv->cant_derive = 1;
 
              /* Skip givs that have mult_val == 0, since
@@ -6246,8 +7491,8 @@ update_giv_derive (loop, p)
                  else
                    giv->cant_derive = 1;
                }
-             else if ((GET_CODE (p) == CODE_LABEL && ! biv->always_computable)
-                      || (GET_CODE (p) == JUMP_INSN && biv->maybe_multiple))
+             else if ((LABEL_P (p) && ! biv->always_computable)
+                      || (JUMP_P (p) && biv->maybe_multiple))
                giv->cant_derive = 1;
            }
        }
@@ -6270,7 +7515,7 @@ update_giv_derive (loop, p)
    *MULT_VAL to CONST0_RTX, and store the invariant into *INC_VAL.
 
    We also want to detect a BIV when it corresponds to a variable
-   whose mode was promoted via PROMOTED_MODE.  In that case, an increment
+   whose mode was promoted.  In that case, an increment
    of the variable may be a PLUS that adds a SUBREG of that variable to
    an invariant and then sign- or zero-extends the result of the PLUS
    into the variable.
@@ -6291,19 +7536,13 @@ update_giv_derive (loop, p)
    If we cannot find a biv, we return 0.  */
 
 static int
-basic_induction_var (loop, x, mode, dest_reg, p, inc_val, mult_val, location)
-     const struct loop *loop;
-     rtx x;
-     enum machine_mode mode;
-     rtx dest_reg;
-     rtx p;
-     rtx *inc_val;
-     rtx *mult_val;
-     rtx **location;
+basic_induction_var (const struct loop *loop, rtx x, enum machine_mode mode,
+                    rtx dest_reg, rtx p, rtx *inc_val, rtx *mult_val,
+                    rtx **location)
 {
   enum rtx_code code;
   rtx *argp, arg;
-  rtx insn, set = 0;
+  rtx insn, set = 0, last, inc;
 
   code = GET_CODE (x);
   *location = NULL;
@@ -6331,7 +7570,26 @@ basic_induction_var (loop, x, mode, dest_reg, p, inc_val, mult_val, location)
       if (loop_invariant_p (loop, arg) != 1)
        return 0;
 
-      *inc_val = convert_modes (GET_MODE (dest_reg), GET_MODE (x), arg, 0);
+      /* convert_modes can emit new instructions, e.g. when arg is a loop
+        invariant MEM and dest_reg has a different mode.
+        These instructions would be emitted after the end of the function
+        and then *inc_val would be an uninitialized pseudo.
+        Detect this and bail in this case.
+        Other alternatives to solve this can be introducing a convert_modes
+        variant which is allowed to fail but not allowed to emit new
+        instructions, emit these instructions before loop start and let
+        it be garbage collected if *inc_val is never used or saving the
+        *inc_val initialization sequence generated here and when *inc_val
+        is going to be actually used, emit it at some suitable place.  */
+      last = get_last_insn ();
+      inc = convert_modes (GET_MODE (dest_reg), GET_MODE (x), arg, 0);
+      if (get_last_insn () != last)
+       {
+         delete_insns_since (last);
+         return 0;
+       }
+
+      *inc_val = inc;
       *mult_val = const1_rtx;
       *location = argp;
       return 1;
@@ -6362,7 +7620,7 @@ basic_induction_var (loop, x, mode, dest_reg, p, inc_val, mult_val, location)
            {
              insn = PREV_INSN (insn);
            }
-         while (insn && GET_CODE (insn) == NOTE
+         while (insn && NOTE_P (insn)
                 && NOTE_LINE_NUMBER (insn) != NOTE_INSN_LOOP_BEG);
 
          if (!insn)
@@ -6383,9 +7641,8 @@ basic_induction_var (loop, x, mode, dest_reg, p, inc_val, mult_val, location)
                                        dest_reg, insn,
                                        inc_val, mult_val, location);
 
-         while (GET_CODE (dest) == SIGN_EXTRACT
+         while (GET_CODE (dest) == SUBREG
                 || GET_CODE (dest) == ZERO_EXTRACT
-                || GET_CODE (dest) == SUBREG
                 || GET_CODE (dest) == STRICT_LOW_PART)
            dest = XEXP (dest, 0);
          if (dest == x)
@@ -6403,16 +7660,24 @@ basic_induction_var (loop, x, mode, dest_reg, p, inc_val, mult_val, location)
     case CONST_INT:
     case SYMBOL_REF:
     case CONST:
-      /* convert_modes aborts if we try to convert to or from CCmode, so just
+      /* convert_modes dies if we try to convert to or from CCmode, so just
          exclude that case.  It is very unlikely that a condition code value
-        would be a useful iterator anyways.  convert_modes aborts if we try to
+        would be a useful iterator anyways.  convert_modes dies if we try to
         convert a float mode to non-float or vice versa too.  */
       if (loop->level == 1
          && GET_MODE_CLASS (mode) == GET_MODE_CLASS (GET_MODE (dest_reg))
          && GET_MODE_CLASS (mode) != MODE_CC)
        {
          /* Possible bug here?  Perhaps we don't know the mode of X.  */
-         *inc_val = convert_modes (GET_MODE (dest_reg), mode, x, 0);
+         last = get_last_insn ();
+         inc = convert_modes (GET_MODE (dest_reg), mode, x, 0);
+         if (get_last_insn () != last)
+           {
+             delete_insns_since (last);
+             return 0;
+           }
+
+         *inc_val = inc;
          *mult_val = const0_rtx;
          return 1;
        }
@@ -6420,13 +7685,16 @@ basic_induction_var (loop, x, mode, dest_reg, p, inc_val, mult_val, location)
        return 0;
 
     case SIGN_EXTEND:
+      /* Ignore this BIV if signed arithmetic overflow is defined.  */
+      if (flag_wrapv)
+       return 0;
       return basic_induction_var (loop, XEXP (x, 0), GET_MODE (XEXP (x, 0)),
                                  dest_reg, p, inc_val, mult_val, location);
 
     case ASHIFTRT:
       /* Similar, since this can be a sign extension.  */
       for (insn = PREV_INSN (p);
-          (insn && GET_CODE (insn) == NOTE
+          (insn && NOTE_P (insn)
            && NOTE_LINE_NUMBER (insn) != NOTE_INSN_LOOP_BEG);
           insn = PREV_INSN (insn))
        ;
@@ -6466,17 +7734,10 @@ basic_induction_var (loop, x, mode, dest_reg, p, inc_val, mult_val, location)
      such that the value of X is biv * mult + add;  */
 
 static int
-general_induction_var (loop, x, src_reg, add_val, mult_val, ext_val,
-                      is_addr, pbenefit, addr_mode)
-     const struct loop *loop;
-     rtx x;
-     rtx *src_reg;
-     rtx *add_val;
-     rtx *mult_val;
-     rtx *ext_val;
-     int is_addr;
-     int *pbenefit;
-     enum machine_mode addr_mode;
+general_induction_var (const struct loop *loop, rtx x, rtx *src_reg,
+                      rtx *add_val, rtx *mult_val, rtx *ext_val,
+                      int is_addr, int *pbenefit,
+                      enum machine_mode addr_mode)
 {
   struct loop_ivs *ivs = LOOP_IVS (loop);
   rtx orig_x = x;
@@ -6534,7 +7795,7 @@ general_induction_var (loop, x, src_reg, add_val, mult_val, ext_val,
       break;
 
     default:
-      abort ();
+      gcc_unreachable ();
     }
 
   /* Remove any enclosing USE from ADD_VAL and MULT_VAL (there will be
@@ -6575,15 +7836,11 @@ general_induction_var (loop, x, src_reg, add_val, mult_val, ext_val,
 
    *BENEFIT will be incremented by the benefit of any sub-giv encountered.  */
 
-static rtx sge_plus PARAMS ((enum machine_mode, rtx, rtx));
-static rtx sge_plus_constant PARAMS ((rtx, rtx));
+static rtx sge_plus (enum machine_mode, rtx, rtx);
+static rtx sge_plus_constant (rtx, rtx);
 
 static rtx
-simplify_giv_expr (loop, x, ext_val, benefit)
-     const struct loop *loop;
-     rtx x;
-     rtx *ext_val;
-     int *benefit;
+simplify_giv_expr (const struct loop *loop, rtx x, rtx *ext_val, int *benefit)
 {
   struct loop_ivs *ivs = LOOP_IVS (loop);
   struct loop_regs *regs = LOOP_REGS (loop);
@@ -6657,14 +7914,14 @@ simplify_giv_expr (loop, x, ext_val, benefit)
                                 ext_val, benefit);
 
          default:
-           abort ();
+           gcc_unreachable ();
          }
 
       /* Each argument must be either REG, PLUS, or MULT.  Convert REG to
         MULT to reduce cases.  */
-      if (GET_CODE (arg0) == REG)
+      if (REG_P (arg0))
        arg0 = gen_rtx_MULT (mode, arg0, const1_rtx);
-      if (GET_CODE (arg1) == REG)
+      if (REG_P (arg1))
        arg1 = gen_rtx_MULT (mode, arg1, const1_rtx);
 
       /* Now have PLUS + PLUS, PLUS + MULT, MULT + PLUS, or MULT + MULT.
@@ -6757,7 +8014,7 @@ simplify_giv_expr (loop, x, ext_val, benefit)
                                                                    arg1)),
                                        ext_val, benefit);
            }
-         /* Propagate the MULT expressions to the intermost nodes.  */
+         /* Propagate the MULT expressions to the innermost nodes.  */
          else if (GET_CODE (arg0) == PLUS)
            {
              /* (invar_0 + invar_1) * invar_2.  Distribute.  */
@@ -6798,7 +8055,7 @@ simplify_giv_expr (loop, x, ext_val, benefit)
                                    ext_val, benefit);
 
        default:
-         abort ();
+         gcc_unreachable ();
        }
 
     case ASHIFT:
@@ -6841,7 +8098,7 @@ simplify_giv_expr (loop, x, ext_val, benefit)
       if (*ext_val == NULL_RTX)
        {
          arg0 = simplify_giv_expr (loop, XEXP (x, 0), ext_val, benefit);
-         if (arg0 && *ext_val == NULL_RTX && GET_CODE (arg0) == REG)
+         if (arg0 && *ext_val == NULL_RTX && REG_P (arg0))
            {
              *ext_val = gen_rtx_fmt_e (GET_CODE (x), mode, arg0);
              return arg0;
@@ -7008,8 +8265,7 @@ simplify_giv_expr (loop, x, ext_val, benefit)
    CONST_INT in the summation.  It is only used by simplify_giv_expr.  */
 
 static rtx
-sge_plus_constant (x, c)
-     rtx x, c;
+sge_plus_constant (rtx x, rtx c)
 {
   if (GET_CODE (x) == CONST_INT)
     return GEN_INT (INTVAL (x) + INTVAL (c));
@@ -7034,9 +8290,7 @@ sge_plus_constant (x, c)
 }
 
 static rtx
-sge_plus (mode, x, y)
-     enum machine_mode mode;
-     rtx x, y;
+sge_plus (enum machine_mode mode, rtx x, rtx y)
 {
   while (GET_CODE (y) == PLUS)
     {
@@ -7074,17 +8328,9 @@ sge_plus (mode, x, y)
    *MULT_VAL and *ADD_VAL.  */
 
 static int
-consec_sets_giv (loop, first_benefit, p, src_reg, dest_reg,
-                add_val, mult_val, ext_val, last_consec_insn)
-     const struct loop *loop;
-     int first_benefit;
-     rtx p;
-     rtx src_reg;
-     rtx dest_reg;
-     rtx *add_val;
-     rtx *mult_val;
-     rtx *ext_val;
-     rtx *last_consec_insn;
+consec_sets_giv (const struct loop *loop, int first_benefit, rtx p,
+                rtx src_reg, rtx dest_reg, rtx *add_val, rtx *mult_val,
+                rtx *ext_val, rtx *last_consec_insn)
 {
   struct loop_ivs *ivs = LOOP_IVS (loop);
   struct loop_regs *regs = LOOP_REGS (loop);
@@ -7106,7 +8352,7 @@ consec_sets_giv (loop, first_benefit, p, src_reg, dest_reg,
   if (REG_IV_TYPE (ivs, REGNO (dest_reg)) != UNKNOWN_INDUCT)
     return 0;
 
-  v = (struct induction *) alloca (sizeof (struct induction));
+  v = alloca (sizeof (struct induction));
   v->src_reg = src_reg;
   v->mult_val = *mult_val;
   v->add_val = *add_val;
@@ -7131,7 +8377,7 @@ consec_sets_giv (loop, first_benefit, p, src_reg, dest_reg,
 
       if (code == INSN
          && (set = single_set (p))
-         && GET_CODE (SET_DEST (set)) == REG
+         && REG_P (SET_DEST (set))
          && SET_DEST (set) == dest_reg
          && (general_induction_var (loop, SET_SRC (set), &src_reg,
                                     add_val, mult_val, ext_val, 0,
@@ -7190,8 +8436,7 @@ consec_sets_giv (loop, first_benefit, p, src_reg, dest_reg,
    subtracting variables.  */
 
 static rtx
-express_from_1 (a, b, mult)
-     rtx a, b, mult;
+express_from_1 (rtx a, rtx b, rtx mult)
 {
   /* If MULT is zero, then A*MULT is zero, and our expression is B.  */
 
@@ -7288,9 +8533,8 @@ express_from_1 (a, b, mult)
   return NULL_RTX;
 }
 
-rtx
-express_from (g1, g2)
-     struct induction *g1, *g2;
+static rtx
+express_from (struct induction *g1, struct induction *g2)
 {
   rtx mult, add;
 
@@ -7301,6 +8545,9 @@ express_from (g1, g2)
       && GET_CODE (g2->mult_val) == CONST_INT)
     {
       if (g1->mult_val == const0_rtx
+         || (g1->mult_val == constm1_rtx
+             && INTVAL (g2->mult_val)
+                == (HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1))
          || INTVAL (g2->mult_val) % INTVAL (g1->mult_val) != 0)
        return NULL_RTX;
       mult = GEN_INT (INTVAL (g2->mult_val) / INTVAL (g1->mult_val));
@@ -7371,8 +8618,7 @@ express_from (g1, g2)
    used to represent G1.  */
 
 static rtx
-combine_givs_p (g1, g2)
-     struct induction *g1, *g2;
+combine_givs_p (struct induction *g1, struct induction *g2)
 {
   rtx comb, ret;
 
@@ -7398,175 +8644,215 @@ combine_givs_p (g1, g2)
       return ret;
     }
 
-  /* If G2 can be expressed as a function of G1 and that function is valid
-     as an address and no more expensive than using a register for G2,
-     the expression of G2 in terms of G1 can be used.  */
-  if (ret != NULL_RTX
-      && g2->giv_type == DEST_ADDR
-      && memory_address_p (GET_MODE (g2->mem), ret))
-    return ret;
+  /* If G2 can be expressed as a function of G1 and that function is valid
+     as an address and no more expensive than using a register for G2,
+     the expression of G2 in terms of G1 can be used.  */
+  if (ret != NULL_RTX
+      && g2->giv_type == DEST_ADDR
+      && memory_address_p (GET_MODE (g2->mem), ret))
+    return ret;
+
+  return NULL_RTX;
+}
+\f
+/* See if BL is monotonic and has a constant per-iteration increment.
+   Return the increment if so, otherwise return 0.  */
+
+static HOST_WIDE_INT
+get_monotonic_increment (struct iv_class *bl)
+{
+  struct induction *v;
+  rtx incr;
+
+  /* Get the total increment and check that it is constant.  */
+  incr = biv_total_increment (bl);
+  if (incr == 0 || GET_CODE (incr) != CONST_INT)
+    return 0;
+
+  for (v = bl->biv; v != 0; v = v->next_iv)
+    {
+      if (GET_CODE (v->add_val) != CONST_INT)
+       return 0;
+
+      if (INTVAL (v->add_val) < 0 && INTVAL (incr) >= 0)
+       return 0;
+
+      if (INTVAL (v->add_val) > 0 && INTVAL (incr) <= 0)
+       return 0;
+    }
+  return INTVAL (incr);
+}
+
+
+/* Subroutine of biv_fits_mode_p.  Return true if biv BL, when biased by
+   BIAS, will never exceed the unsigned range of MODE.  LOOP is the loop
+   to which the biv belongs and INCR is its per-iteration increment.  */
+
+static bool
+biased_biv_fits_mode_p (const struct loop *loop, struct iv_class *bl,
+                       HOST_WIDE_INT incr, enum machine_mode mode,
+                       unsigned HOST_WIDE_INT bias)
+{
+  unsigned HOST_WIDE_INT initial, maximum, span, delta;
+
+  /* We need to be able to manipulate MODE-size constants.  */
+  if (HOST_BITS_PER_WIDE_INT < GET_MODE_BITSIZE (mode))
+    return false;
+
+  /* The number of loop iterations must be constant.  */
+  if (LOOP_INFO (loop)->n_iterations == 0)
+    return false;
+
+  /* So must the biv's initial value.  */
+  if (bl->initial_value == 0 || GET_CODE (bl->initial_value) != CONST_INT)
+    return false;
+
+  initial = bias + INTVAL (bl->initial_value);
+  maximum = GET_MODE_MASK (mode);
+
+  /* Make sure that the initial value is within range.  */
+  if (initial > maximum)
+    return false;
+
+  /* Set up DELTA and SPAN such that the number of iterations * DELTA
+     (calculated to arbitrary precision) must be <= SPAN.  */
+  if (incr < 0)
+    {
+      delta = -incr;
+      span = initial;
+    }
+  else
+    {
+      delta = incr;
+      /* Handle the special case in which MAXIMUM is the largest
+        unsigned HOST_WIDE_INT and INITIAL is 0.  */
+      if (maximum + 1 == initial)
+       span = LOOP_INFO (loop)->n_iterations * delta;
+      else
+       span = maximum + 1 - initial;
+    }
+  return (span / LOOP_INFO (loop)->n_iterations >= delta);
+}
+
+
+/* Return true if biv BL will never exceed the bounds of MODE.  LOOP is
+   the loop to which BL belongs and INCR is its per-iteration increment.
+   UNSIGNEDP is true if the biv should be treated as unsigned.  */
+
+static bool
+biv_fits_mode_p (const struct loop *loop, struct iv_class *bl,
+                HOST_WIDE_INT incr, enum machine_mode mode, bool unsignedp)
+{
+  struct loop_info *loop_info;
+  unsigned HOST_WIDE_INT bias;
+
+  /* A biv's value will always be limited to its natural mode.
+     Larger modes will observe the same wrap-around.  */
+  if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (bl->biv->src_reg)))
+    mode = GET_MODE (bl->biv->src_reg);
+
+  loop_info = LOOP_INFO (loop);
+
+  bias = (unsignedp ? 0 : (GET_MODE_MASK (mode) >> 1) + 1);
+  if (biased_biv_fits_mode_p (loop, bl, incr, mode, bias))
+    return true;
+
+  if (mode == GET_MODE (bl->biv->src_reg)
+      && bl->biv->src_reg == loop_info->iteration_var
+      && loop_info->comparison_value
+      && loop_invariant_p (loop, loop_info->comparison_value))
+    {
+      /* If the increment is +1, and the exit test is a <, the BIV
+         cannot overflow.  (For <=, we have the problematic case that
+         the comparison value might be the maximum value of the range.)  */
+      if (incr == 1)
+       {
+         if (loop_info->comparison_code == LT)
+           return true;
+         if (loop_info->comparison_code == LTU && unsignedp)
+           return true;
+       }
+
+      /* Likewise for increment -1 and exit test >.  */
+      if (incr == -1)
+       {
+         if (loop_info->comparison_code == GT)
+           return true;
+         if (loop_info->comparison_code == GTU && unsignedp)
+           return true;
+       }
+    }
+  return false;
+}
+
+
+/* Given that X is an extension or truncation of BL, return true
+   if it is unaffected by overflow.  LOOP is the loop to which
+   BL belongs and INCR is its per-iteration increment.  */
+
+static bool
+extension_within_bounds_p (const struct loop *loop, struct iv_class *bl,
+                          HOST_WIDE_INT incr, rtx x)
+{
+  enum machine_mode mode;
+  bool signedp, unsignedp;
+
+  switch (GET_CODE (x))
+    {
+    case SIGN_EXTEND:
+    case ZERO_EXTEND:
+      mode = GET_MODE (XEXP (x, 0));
+      signedp = (GET_CODE (x) == SIGN_EXTEND);
+      unsignedp = (GET_CODE (x) == ZERO_EXTEND);
+      break;
+
+    case TRUNCATE:
+      /* We don't know whether this value is being used as signed
+        or unsigned, so check the conditions for both.  */
+      mode = GET_MODE (x);
+      signedp = unsignedp = true;
+      break;
 
-  return NULL_RTX;
+    default:
+      gcc_unreachable ();
+    }
+
+  return ((!signedp || biv_fits_mode_p (loop, bl, incr, mode, false))
+         && (!unsignedp || biv_fits_mode_p (loop, bl, incr, mode, true)));
 }
-\f
+
+
 /* Check each extension dependent giv in this class to see if its
    root biv is safe from wrapping in the interior mode, which would
    make the giv illegal.  */
 
 static void
-check_ext_dependent_givs (bl, loop_info)
-     struct iv_class *bl;
-     struct loop_info *loop_info;
-{
-  int ze_ok = 0, se_ok = 0, info_ok = 0;
-  enum machine_mode biv_mode = GET_MODE (bl->biv->src_reg);
-  HOST_WIDE_INT start_val;
-  unsigned HOST_WIDE_INT u_end_val = 0;
-  unsigned HOST_WIDE_INT u_start_val = 0;
-  rtx incr = pc_rtx;
+check_ext_dependent_givs (const struct loop *loop, struct iv_class *bl)
+{
   struct induction *v;
+  HOST_WIDE_INT incr;
 
-  /* Make sure the iteration data is available.  We must have
-     constants in order to be certain of no overflow.  */
-  /* ??? An unknown iteration count with an increment of +-1
-     combined with friendly exit tests of against an invariant
-     value is also amenable to optimization.  Not implemented.  */
-  if (loop_info->n_iterations > 0
-      && bl->initial_value
-      && GET_CODE (bl->initial_value) == CONST_INT
-      && (incr = biv_total_increment (bl))
-      && GET_CODE (incr) == CONST_INT
-      /* Make sure the host can represent the arithmetic.  */
-      && HOST_BITS_PER_WIDE_INT >= GET_MODE_BITSIZE (biv_mode))
-    {
-      unsigned HOST_WIDE_INT abs_incr, total_incr;
-      HOST_WIDE_INT s_end_val;
-      int neg_incr;
-
-      info_ok = 1;
-      start_val = INTVAL (bl->initial_value);
-      u_start_val = start_val;
-
-      neg_incr = 0, abs_incr = INTVAL (incr);
-      if (INTVAL (incr) < 0)
-       neg_incr = 1, abs_incr = -abs_incr;
-      total_incr = abs_incr * loop_info->n_iterations;
-
-      /* Check for host arithmetic overflow.  */
-      if (total_incr / loop_info->n_iterations == abs_incr)
-       {
-         unsigned HOST_WIDE_INT u_max;
-         HOST_WIDE_INT s_max;
-
-         u_end_val = start_val + (neg_incr ? -total_incr : total_incr);
-         s_end_val = u_end_val;
-         u_max = GET_MODE_MASK (biv_mode);
-         s_max = u_max >> 1;
-
-         /* Check zero extension of biv ok.  */
-         if (start_val >= 0
-             /* Check for host arithmetic overflow.  */
-             && (neg_incr
-                 ? u_end_val < u_start_val
-                 : u_end_val > u_start_val)
-             /* Check for target arithmetic overflow.  */
-             && (neg_incr
-                 ? 1 /* taken care of with host overflow */
-                 : u_end_val <= u_max))
-           {
-             ze_ok = 1;
-           }
-
-         /* Check sign extension of biv ok.  */
-         /* ??? While it is true that overflow with signed and pointer
-            arithmetic is undefined, I fear too many programmers don't
-            keep this fact in mind -- myself included on occasion.
-            So leave alone with the signed overflow optimizations.  */
-         if (start_val >= -s_max - 1
-             /* Check for host arithmetic overflow.  */
-             && (neg_incr
-                 ? s_end_val < start_val
-                 : s_end_val > start_val)
-             /* Check for target arithmetic overflow.  */
-             && (neg_incr
-                 ? s_end_val >= -s_max - 1
-                 : s_end_val <= s_max))
-           {
-             se_ok = 1;
-           }
-       }
-    }
+  incr = get_monotonic_increment (bl);
 
   /* Invalidate givs that fail the tests.  */
   for (v = bl->giv; v; v = v->next_iv)
     if (v->ext_dependent)
       {
-       enum rtx_code code = GET_CODE (v->ext_dependent);
-       int ok = 0;
-
-       switch (code)
-         {
-         case SIGN_EXTEND:
-           ok = se_ok;
-           break;
-         case ZERO_EXTEND:
-           ok = ze_ok;
-           break;
-
-         case TRUNCATE:
-           /* We don't know whether this value is being used as either
-              signed or unsigned, so to safely truncate we must satisfy
-              both.  The initial check here verifies the BIV itself;
-              once that is successful we may check its range wrt the
-              derived GIV.  */
-           if (se_ok && ze_ok)
-             {
-               enum machine_mode outer_mode = GET_MODE (v->ext_dependent);
-               unsigned HOST_WIDE_INT max = GET_MODE_MASK (outer_mode) >> 1;
-
-               /* We know from the above that both endpoints are nonnegative,
-                  and that there is no wrapping.  Verify that both endpoints
-                  are within the (signed) range of the outer mode.  */
-               if (u_start_val <= max && u_end_val <= max)
-                 ok = 1;
-             }
-           break;
-
-         default:
-           abort ();
-         }
-
-       if (ok)
+       if (incr != 0
+           && extension_within_bounds_p (loop, bl, incr, v->ext_dependent))
          {
            if (loop_dump_stream)
-             {
-               fprintf (loop_dump_stream,
-                        "Verified ext dependent giv at %d of reg %d\n",
-                        INSN_UID (v->insn), bl->regno);
-             }
+             fprintf (loop_dump_stream,
+                      "Verified ext dependent giv at %d of reg %d\n",
+                      INSN_UID (v->insn), bl->regno);
          }
        else
          {
            if (loop_dump_stream)
-             {
-               const char *why;
-
-               if (info_ok)
-                 why = "biv iteration values overflowed";
-               else
-                 {
-                   if (incr == pc_rtx)
-                     incr = biv_total_increment (bl);
-                   if (incr == const1_rtx)
-                     why = "biv iteration info incomplete; incr by 1";
-                   else
-                     why = "biv iteration info incomplete";
-                 }
+             fprintf (loop_dump_stream,
+                      "Failed ext dependent giv at %d\n",
+                      INSN_UID (v->insn));
 
-               fprintf (loop_dump_stream,
-                        "Failed ext dependent giv at %d, %s\n",
-                        INSN_UID (v->insn), why);
-             }
            v->ignore = 1;
            bl->all_reduced = 0;
          }
@@ -7575,10 +8861,8 @@ check_ext_dependent_givs (bl, loop_info)
 
 /* Generate a version of VALUE in a mode appropriate for initializing V.  */
 
-rtx
-extend_value_for_giv (v, value)
-     struct induction *v;
-     rtx value;
+static rtx
+extend_value_for_giv (struct induction *v, rtx value)
 {
   rtx ext_dep = v->ext_dependent;
 
@@ -7603,9 +8887,7 @@ struct combine_givs_stats
 };
 
 static int
-cmp_combine_givs_stats (xp, yp)
-     const PTR xp;
-     const PTR yp;
+cmp_combine_givs_stats (const void *xp, const void *yp)
 {
   const struct combine_givs_stats * const x =
     (const struct combine_givs_stats *) xp;
@@ -7625,9 +8907,7 @@ cmp_combine_givs_stats (xp, yp)
    giv.  Also, update BENEFIT and related fields for cost/benefit analysis.  */
 
 static void
-combine_givs (regs, bl)
-     struct loop_regs *regs;
-     struct iv_class *bl;
+combine_givs (struct loop_regs *regs, struct iv_class *bl)
 {
   /* Additional benefit to add for being combined multiple times.  */
   const int extra_benefit = 3;
@@ -7643,15 +8923,14 @@ combine_givs (regs, bl)
     if (!g1->ignore)
       giv_count++;
 
-  giv_array
-    = (struct induction **) alloca (giv_count * sizeof (struct induction *));
+  giv_array = alloca (giv_count * sizeof (struct induction *));
   i = 0;
   for (g1 = bl->giv; g1; g1 = g1->next_iv)
     if (!g1->ignore)
       giv_array[i++] = g1;
 
-  stats = (struct combine_givs_stats *) xcalloc (giv_count, sizeof (*stats));
-  can_combine = (rtx *) xcalloc (giv_count, giv_count * sizeof (rtx));
+  stats = xcalloc (giv_count, sizeof (*stats));
+  can_combine = xcalloc (giv_count, giv_count * sizeof (rtx));
 
   for (i = 0; i < giv_count; i++)
     {
@@ -7664,7 +8943,7 @@ combine_givs (regs, bl)
       /* If a DEST_REG GIV is used only once, do not allow it to combine
         with anything, for in doing so we will gain nothing that cannot
         be had by simply letting the GIV with which we would have combined
-        to be reduced on its own.  The losage shows up in particular with
+        to be reduced on its own.  The lossage shows up in particular with
         DEST_ADDR targets on hosts with reg+reg addressing, though it can
         be seen elsewhere as well.  */
       if (g1->giv_type == DEST_REG
@@ -7795,14 +9074,12 @@ restart:
   free (can_combine);
 }
 \f
-/* Generate sequence for REG = B * M + A.  */
+/* Generate sequence for REG = B * M + A.  B is the initial value of
+   the basic induction variable, M a multiplicative constant, A an
+   additive constant and REG the destination register.  */
 
 static rtx
-gen_add_mult (b, m, a, reg)
-     rtx b;          /* initial value of basic induction variable */
-     rtx m;          /* multiplicative constant */
-     rtx a;          /* additive constant */
-     rtx reg;        /* destination register */
+gen_add_mult (rtx b,  rtx m, rtx a, rtx reg)
 {
   rtx seq;
   rtx result;
@@ -7822,47 +9099,33 @@ gen_add_mult (b, m, a, reg)
 /* Update registers created in insn sequence SEQ.  */
 
 static void
-loop_regs_update (loop, seq)
-     const struct loop *loop ATTRIBUTE_UNUSED;
-     rtx seq;
+loop_regs_update (const struct loop *loop ATTRIBUTE_UNUSED, rtx seq)
 {
   rtx insn;
 
   /* Update register info for alias analysis.  */
 
-  if (seq == NULL_RTX)
-    return;
-
-  if (INSN_P (seq))
+  insn = seq;
+  while (insn != NULL_RTX)
     {
-      insn = seq;
-      while (insn != NULL_RTX)
-       {
-         rtx set = single_set (insn);
+      rtx set = single_set (insn);
 
-         if (set && GET_CODE (SET_DEST (set)) == REG)
-           record_base_value (REGNO (SET_DEST (set)), SET_SRC (set), 0);
+      if (set && REG_P (SET_DEST (set)))
+       record_base_value (REGNO (SET_DEST (set)), SET_SRC (set), 0);
 
-         insn = NEXT_INSN (insn);
-       }
+      insn = NEXT_INSN (insn);
     }
-  else if (GET_CODE (seq) == SET
-          && GET_CODE (SET_DEST (seq)) == REG)
-    record_base_value (REGNO (SET_DEST (seq)), SET_SRC (seq), 0);
 }
 
 
-/* EMIT code before BEFORE_BB/BEFORE_INSN to set REG = B * M + A.  */
+/* EMIT code before BEFORE_BB/BEFORE_INSN to set REG = B * M + A.  B
+   is the initial value of the basic induction variable, M a
+   multiplicative constant, A an additive constant and REG the
+   destination register.  */
 
-void
-loop_iv_add_mult_emit_before (loop, b, m, a, reg, before_bb, before_insn)
-     const struct loop *loop;
-     rtx b;          /* initial value of basic induction variable */
-     rtx m;          /* multiplicative constant */
-     rtx a;          /* additive constant */
-     rtx reg;        /* destination register */
-     basic_block before_bb;
-     rtx before_insn;
+static void
+loop_iv_add_mult_emit_before (const struct loop *loop, rtx b, rtx m, rtx a,
+                             rtx reg, basic_block before_bb, rtx before_insn)
 {
   rtx seq;
 
@@ -7880,23 +9143,22 @@ loop_iv_add_mult_emit_before (loop, b, m, a, reg, before_bb, before_insn)
   update_reg_last_use (b, before_insn);
   update_reg_last_use (m, before_insn);
 
-  loop_insn_emit_before (loop, before_bb, before_insn, seq);
-
   /* It is possible that the expansion created lots of new registers.
-     Iterate over the sequence we just created and record them all.  */
+     Iterate over the sequence we just created and record them all.  We
+     must do this before inserting the sequence.  */
   loop_regs_update (loop, seq);
+
+  loop_insn_emit_before (loop, before_bb, before_insn, seq);
 }
 
 
-/* Emit insns in loop pre-header to set REG = B * M + A.  */
+/* Emit insns in loop pre-header to set REG = B * M + A.  B is the
+   initial value of the basic induction variable, M a multiplicative
+   constant, A an additive constant and REG the destination
+   register.  */
 
-void
-loop_iv_add_mult_sink (loop, b, m, a, reg)
-     const struct loop *loop;
-     rtx b;          /* initial value of basic induction variable */
-     rtx m;          /* multiplicative constant */
-     rtx a;          /* additive constant */
-     rtx reg;        /* destination register */
+static void
+loop_iv_add_mult_sink (const struct loop *loop, rtx b, rtx m, rtx a, rtx reg)
 {
   rtx seq;
 
@@ -7909,34 +9171,33 @@ loop_iv_add_mult_sink (loop, b, m, a, reg)
   update_reg_last_use (b, loop->sink);
   update_reg_last_use (m, loop->sink);
 
-  loop_insn_sink (loop, seq);
-
   /* It is possible that the expansion created lots of new registers.
-     Iterate over the sequence we just created and record them all.  */
+     Iterate over the sequence we just created and record them all.  We
+     must do this before inserting the sequence.  */
   loop_regs_update (loop, seq);
+
+  loop_insn_sink (loop, seq);
 }
 
 
-/* Emit insns after loop to set REG = B * M + A.  */
+/* Emit insns after loop to set REG = B * M + A.  B is the initial
+   value of the basic induction variable, M a multiplicative constant,
+   A an additive constant and REG the destination register.  */
 
-void
-loop_iv_add_mult_hoist (loop, b, m, a, reg)
-     const struct loop *loop;
-     rtx b;          /* initial value of basic induction variable */
-     rtx m;          /* multiplicative constant */
-     rtx a;          /* additive constant */
-     rtx reg;        /* destination register */
+static void
+loop_iv_add_mult_hoist (const struct loop *loop, rtx b, rtx m, rtx a, rtx reg)
 {
   rtx seq;
 
   /* Use copy_rtx to prevent unexpected sharing of these rtx.  */
   seq = gen_add_mult (copy_rtx (b), copy_rtx (m), copy_rtx (a), reg);
 
-  loop_insn_hoist (loop, seq);
-
   /* It is possible that the expansion created lots of new registers.
-     Iterate over the sequence we just created and record them all.  */
+     Iterate over the sequence we just created and record them all.  We
+     must do this before inserting the sequence.  */
   loop_regs_update (loop, seq);
+
+  loop_insn_hoist (loop, seq);
 }
 
 
@@ -7945,11 +9206,7 @@ loop_iv_add_mult_hoist (loop, b, m, a, reg)
    sequence.  */
 
 static int
-iv_add_mult_cost (b, m, a, reg)
-     rtx b;          /* initial value of basic induction variable */
-     rtx m;          /* multiplicative constant */
-     rtx a;          /* additive constant */
-     rtx reg;        /* destination register */
+iv_add_mult_cost (rtx b, rtx m, rtx a, rtx reg)
 {
   int cost = 0;
   rtx last, result;
@@ -7979,9 +9236,7 @@ iv_add_mult_cost (b, m, a, reg)
   ??? thing, generate wasted RTL just to see if something is possible.  */
 
 static int
-product_cheap_p (a, b)
-     rtx a;
-     rtx b;
+product_cheap_p (rtx a, rtx b)
 {
   rtx tmp;
   int win, n_insns;
@@ -8008,7 +9263,9 @@ product_cheap_p (a, b)
   end_sequence ();
 
   win = 1;
-  if (INSN_P (tmp))
+  if (tmp == NULL_RTX)
+    ;
+  else if (INSN_P (tmp))
     {
       n_insns = 0;
       while (tmp != NULL_RTX)
@@ -8016,7 +9273,7 @@ product_cheap_p (a, b)
          rtx next = NEXT_INSN (tmp);
 
          if (++n_insns > 3
-             || GET_CODE (tmp) != INSN
+             || !NONJUMP_INSN_P (tmp)
              || (GET_CODE (PATTERN (tmp)) == SET
                  && GET_CODE (SET_SRC (PATTERN (tmp))) == MULT)
              || (GET_CODE (PATTERN (tmp)) == PARALLEL
@@ -8057,15 +9314,14 @@ product_cheap_p (a, b)
    final_[bg]iv_value.  */
 
 static int
-check_dbra_loop (loop, insn_count)
-     struct loop *loop;
-     int insn_count;
+check_dbra_loop (struct loop *loop, int insn_count)
 {
   struct loop_info *loop_info = LOOP_INFO (loop);
   struct loop_regs *regs = LOOP_REGS (loop);
   struct loop_ivs *ivs = LOOP_IVS (loop);
   struct iv_class *bl;
   rtx reg;
+  enum machine_mode mode;
   rtx jump_label;
   rtx final_value;
   rtx start_value;
@@ -8091,7 +9347,7 @@ check_dbra_loop (loop, insn_count)
 
   /* Try to compute whether the compare/branch at the loop end is one or
      two instructions.  */
-  get_condition (jump, &first_compare);
+  get_condition (jump, &first_compare, false, true);
   if (first_compare == jump)
     compare_and_branch = 1;
   else if (first_compare == prev_nonnote_insn (jump))
@@ -8108,8 +9364,8 @@ check_dbra_loop (loop, insn_count)
        sequence and see if we've got another comparison sequence.  */
 
     rtx jump1;
-    if ((jump1 = prev_nonnote_insn (first_compare)) != loop->cont)
-      if (GET_CODE (jump1) == JUMP_INSN)
+    if ((jump1 = prev_nonnote_insn (first_compare))
+       && JUMP_P (jump1))
        return 0;
   }
 
@@ -8128,6 +9384,22 @@ check_dbra_loop (loop, insn_count)
        break;
     }
 
+  /* Try swapping the comparison to identify a suitable biv.  */
+  if (!bl)
+    for (bl = ivs->list; bl; bl = bl->next)
+      if (bl->biv_count == 1
+         && ! bl->biv->maybe_multiple
+         && bl->biv->dest_reg == XEXP (comparison, 1)
+         && ! reg_used_between_p (regno_reg_rtx[bl->regno], bl->biv->insn,
+                                  first_compare))
+       {
+         comparison = gen_rtx_fmt_ee (swap_condition (GET_CODE (comparison)),
+                                      VOIDmode,
+                                      XEXP (comparison, 1),
+                                      XEXP (comparison, 0));
+         break;
+       }
+
   if (! bl)
     return 0;
 
@@ -8136,9 +9408,7 @@ check_dbra_loop (loop, insn_count)
      In this case, add a reg_note REG_NONNEG, which allows the
      m68k DBRA instruction to be used.  */
 
-  if (((GET_CODE (comparison) == GT
-       && GET_CODE (XEXP (comparison, 1)) == CONST_INT
-       && INTVAL (XEXP (comparison, 1)) == -1)
+  if (((GET_CODE (comparison) == GT && XEXP (comparison, 1) == constm1_rtx)
        || (GET_CODE (comparison) == NE && XEXP (comparison, 1) == const0_rtx))
       && GET_CODE (bl->biv->add_val) == CONST_INT
       && INTVAL (bl->biv->add_val) < 0)
@@ -8152,7 +9422,7 @@ check_dbra_loop (loop, insn_count)
          && (INTVAL (bl->initial_value)
              % (-INTVAL (bl->biv->add_val))) == 0)
        {
-         /* register always nonnegative, add REG_NOTE to branch */
+         /* Register always nonnegative, add REG_NOTE to branch.  */
          if (! find_reg_note (jump, REG_NONNEG, NULL_RTX))
            REG_NOTES (jump)
              = gen_rtx_EXPR_LIST (REG_NONNEG, bl->biv->dest_reg,
@@ -8166,15 +9436,16 @@ check_dbra_loop (loop, insn_count)
         the loop, then we can safely optimize.  */
       for (p = loop_start; p; p = PREV_INSN (p))
        {
-         if (GET_CODE (p) == CODE_LABEL)
+         if (LABEL_P (p))
            break;
-         if (GET_CODE (p) != JUMP_INSN)
+         if (!JUMP_P (p))
            continue;
 
          before_comparison = get_condition_for_loop (loop, p);
          if (before_comparison
              && XEXP (before_comparison, 0) == bl->biv->dest_reg
-             && GET_CODE (before_comparison) == LT
+             && (GET_CODE (before_comparison) == LT
+                 || GET_CODE (before_comparison) == LTU)
              && XEXP (before_comparison, 1) == const0_rtx
              && ! reg_set_between_p (bl->biv->dest_reg, p, loop_start)
              && INTVAL (bl->biv->add_val) == -1)
@@ -8226,13 +9497,15 @@ check_dbra_loop (loop, insn_count)
              {
                rtx set = single_set (p);
 
-               if (set && GET_CODE (SET_DEST (set)) == REG
+               if (set && REG_P (SET_DEST (set))
                    && REGNO (SET_DEST (set)) == bl->regno)
                  /* An insn that sets the biv is okay.  */
                  ;
-               else if ((p == prev_nonnote_insn (prev_nonnote_insn (loop_end))
-                         || p == prev_nonnote_insn (loop_end))
-                        && reg_mentioned_p (bivreg, PATTERN (p)))
+               else if (!reg_mentioned_p (bivreg, PATTERN (p)))
+                 /* An insn that doesn't mention the biv is okay.  */
+                 ;
+               else if (p == prev_nonnote_insn (prev_nonnote_insn (loop_end))
+                        || p == prev_nonnote_insn (loop_end))
                  {
                    /* If either of these insns uses the biv and sets a pseudo
                       that has more than one usage, then the biv has uses
@@ -8246,7 +9519,7 @@ check_dbra_loop (loop, insn_count)
                        break;
                      }
                  }
-               else if (reg_mentioned_p (bivreg, PATTERN (p)))
+               else
                  {
                    no_use_except_counting = 0;
                    break;
@@ -8343,7 +9616,8 @@ check_dbra_loop (loop, insn_count)
              /* for constants, LE gets turned into LT */
              && (GET_CODE (comparison) == LT
                  || (GET_CODE (comparison) == LE
-                     && no_use_except_counting)))
+                     && no_use_except_counting) 
+                 || GET_CODE (comparison) == LTU))
            {
              HOST_WIDE_INT add_val, add_adjust, comparison_val = 0;
              rtx initial_value, comparison_value;
@@ -8351,6 +9625,7 @@ check_dbra_loop (loop, insn_count)
              enum rtx_code cmp_code;
              int comparison_const_width;
              unsigned HOST_WIDE_INT comparison_sign_mask;
+             bool keep_first_compare;
 
              add_val = INTVAL (bl->biv->add_val);
              comparison_value = XEXP (comparison, 1);
@@ -8403,17 +9678,6 @@ check_dbra_loop (loop, insn_count)
 
              /* First check if we can do a vanilla loop reversal.  */
              if (initial_value == const0_rtx
-                 /* If we have a decrement_and_branch_on_count,
-                    prefer the NE test, since this will allow that
-                    instruction to be generated.  Note that we must
-                    use a vanilla loop reversal if the biv is used to
-                    calculate a giv or has a non-counting use.  */
-#if ! defined (HAVE_decrement_and_branch_until_zero) \
-&& defined (HAVE_decrement_and_branch_on_count)
-                 && (! (add_val == 1 && loop->vtop
-                        && (bl->biv_count == 0
-                            || no_use_except_counting)))
-#endif
                  && GET_CODE (comparison_value) == CONST_INT
                     /* Now do postponed overflow checks on COMPARISON_VAL.  */
                  && ! (((comparison_val - add_val) ^ INTVAL (comparison_value))
@@ -8425,13 +9689,6 @@ check_dbra_loop (loop, insn_count)
                  nonneg = 1;
                  cmp_code = GE;
                }
-             else if (add_val == 1 && loop->vtop
-                      && (bl->biv_count == 0
-                          || no_use_except_counting))
-               {
-                 add_adjust = 0;
-                 cmp_code = NE;
-               }
              else
                return 0;
 
@@ -8468,6 +9725,7 @@ check_dbra_loop (loop, insn_count)
 
              /* Save some info needed to produce the new insns.  */
              reg = bl->biv->dest_reg;
+             mode = GET_MODE (reg);
              jump_label = condjump_label (PREV_INSN (loop_end));
              new_add_val = GEN_INT (-INTVAL (bl->biv->add_val));
 
@@ -8479,12 +9737,12 @@ check_dbra_loop (loop, insn_count)
              if (initial_value == const0_rtx
                  && GET_CODE (comparison_value) == CONST_INT)
                {
-                 start_value = GEN_INT (comparison_val - add_adjust);
+                 start_value
+                   = gen_int_mode (comparison_val - add_adjust, mode);
                  loop_insn_hoist (loop, gen_move_insn (reg, start_value));
                }
              else if (GET_CODE (initial_value) == CONST_INT)
                {
-                 enum machine_mode mode = GET_MODE (reg);
                  rtx offset = GEN_INT (-INTVAL (initial_value) - add_adjust);
                  rtx add_insn = gen_add3_insn (reg, comparison_value, offset);
 
@@ -8500,7 +9758,6 @@ check_dbra_loop (loop, insn_count)
                }
              else if (! add_adjust)
                {
-                 enum machine_mode mode = GET_MODE (reg);
                  rtx sub_insn = gen_sub3_insn (reg, comparison_value,
                                                initial_value);
 
@@ -8543,13 +9800,26 @@ check_dbra_loop (loop, insn_count)
                 not delete the label.  */
              LABEL_NUSES (XEXP (jump_label, 0))++;
 
+             /* If we have a separate comparison insn that does more
+                than just set cc0, the result of the comparison might
+                be used outside the loop.  */
+             keep_first_compare = (compare_and_branch == 2
+#ifdef HAVE_CC0
+                                   && sets_cc0_p (first_compare) <= 0
+#endif
+                                   );
+
              /* Emit an insn after the end of the loop to set the biv's
                 proper exit value if it is used anywhere outside the loop.  */
-             if ((REGNO_LAST_UID (bl->regno) != INSN_UID (first_compare))
+             if (keep_first_compare
+                 || (REGNO_LAST_UID (bl->regno) != INSN_UID (first_compare))
                  || ! bl->init_insn
                  || REGNO_FIRST_UID (bl->regno) != INSN_UID (bl->init_insn))
                loop_insn_sink (loop, gen_load_of_final_value (reg, final_value));
 
+             if (keep_first_compare)
+               loop_insn_sink (loop, PATTERN (first_compare));
+
              /* Delete compare/branch at end of loop.  */
              delete_related_insns (PREV_INSN (loop_end));
              if (compare_and_branch == 2)
@@ -8558,14 +9828,14 @@ check_dbra_loop (loop, insn_count)
              /* Add new compare/branch insn at end of loop.  */
              start_sequence ();
              emit_cmp_and_jump_insns (reg, const0_rtx, cmp_code, NULL_RTX,
-                                      GET_MODE (reg), 0,
+                                      mode, 0,
                                       XEXP (jump_label, 0));
              tem = get_insns ();
              end_sequence ();
              emit_jump_insn_before (tem, loop_end);
 
              for (tem = PREV_INSN (loop_end);
-                  tem && GET_CODE (tem) != JUMP_INSN;
+                  tem && !JUMP_P (tem);
                   tem = PREV_INSN (tem))
                ;
 
@@ -8604,7 +9874,7 @@ check_dbra_loop (loop, insn_count)
                    /* If this is a set of a GIV based on the reversed biv, any
                       REG_EQUAL notes should still be correct.  */
                    if (! set
-                       || GET_CODE (SET_DEST (set)) != REG
+                       || !REG_P (SET_DEST (set))
                        || (size_t) REGNO (SET_DEST (set)) >= ivs->n_regs
                        || REG_IV_TYPE (ivs, REGNO (SET_DEST (set))) != GENERAL_INDUCT
                        || REG_IV_INFO (ivs, REGNO (SET_DEST (set)))->src_reg != bl->biv->src_reg)
@@ -8652,11 +9922,8 @@ check_dbra_loop (loop, insn_count)
    start of the loop.  */
 
 static int
-maybe_eliminate_biv (loop, bl, eliminate_p, threshold, insn_count)
-     const struct loop *loop;
-     struct iv_class *bl;
-     int eliminate_p;
-     int threshold, insn_count;
+maybe_eliminate_biv (const struct loop *loop, struct iv_class *bl,
+                    int eliminate_p, int threshold, int insn_count)
 {
   struct loop_ivs *ivs = LOOP_IVS (loop);
   rtx reg = bl->biv->dest_reg;
@@ -8673,7 +9940,7 @@ maybe_eliminate_biv (loop, bl, eliminate_p, threshold, insn_count)
       rtx note;
 
       /* If this is a libcall that sets a giv, skip ahead to its end.  */
-      if (GET_RTX_CLASS (code) == 'i')
+      if (INSN_P (p))
        {
          note = find_reg_note (p, REG_LIBCALL, NULL_RTX);
 
@@ -8682,7 +9949,7 @@ maybe_eliminate_biv (loop, bl, eliminate_p, threshold, insn_count)
              rtx last = XEXP (note, 0);
              rtx set = single_set (last);
 
-             if (set && GET_CODE (SET_DEST (set)) == REG)
+             if (set && REG_P (SET_DEST (set)))
                {
                  unsigned int regno = REGNO (SET_DEST (set));
 
@@ -8728,9 +9995,8 @@ maybe_eliminate_biv (loop, bl, eliminate_p, threshold, insn_count)
 /* INSN and REFERENCE are instructions in the same insn chain.
    Return nonzero if INSN is first.  */
 
-int
-loop_insn_first_p (insn, reference)
-     rtx insn, reference;
+static int
+loop_insn_first_p (rtx insn, rtx reference)
 {
   rtx p, q;
 
@@ -8748,11 +10014,11 @@ loop_insn_first_p (insn, reference)
         P is a note.  */
       if (INSN_UID (p) < max_uid_for_loop
          && INSN_UID (q) < max_uid_for_loop
-         && GET_CODE (p) != NOTE)
+         && !NOTE_P (p))
        return INSN_LUID (p) <= INSN_LUID (q);
 
       if (INSN_UID (p) >= max_uid_for_loop
-         || GET_CODE (p) == NOTE)
+         || NOTE_P (p))
        p = NEXT_INSN (p);
       if (INSN_UID (q) >= max_uid_for_loop)
        q = NEXT_INSN (q);
@@ -8763,9 +10029,8 @@ loop_insn_first_p (insn, reference)
    the offset that we have to take into account due to auto-increment /
    div derivation is zero.  */
 static int
-biv_elimination_giv_has_0_offset (biv, giv, insn)
-     struct induction *biv, *giv;
-     rtx insn;
+biv_elimination_giv_has_0_offset (struct induction *biv,
+                                 struct induction *giv, rtx insn)
 {
   /* If the giv V had the auto-inc address optimization applied
      to it, and INSN occurs between the giv insn and the biv
@@ -8793,13 +10058,9 @@ biv_elimination_giv_has_0_offset (biv, giv, insn)
    start of the loop (when WHERE_INSN is zero).  */
 
 static int
-maybe_eliminate_biv_1 (loop, x, insn, bl, eliminate_p, where_bb, where_insn)
-     const struct loop *loop;
-     rtx x, insn;
-     struct iv_class *bl;
-     int eliminate_p;
-     basic_block where_bb;
-     rtx where_insn;
+maybe_eliminate_biv_1 (const struct loop *loop, rtx x, rtx insn,
+                      struct iv_class *bl, int eliminate_p,
+                      basic_block where_bb, rtx where_insn)
 {
   enum rtx_code code = GET_CODE (x);
   rtx reg = bl->biv->dest_reg;
@@ -8882,7 +10143,7 @@ maybe_eliminate_biv_1 (loop, x, insn, bl, eliminate_p, where_bb, where_insn)
                && (GET_CODE (v->add_val) == SYMBOL_REF
                    || GET_CODE (v->add_val) == LABEL_REF
                    || GET_CODE (v->add_val) == CONST
-                   || (GET_CODE (v->add_val) == REG
+                   || (REG_P (v->add_val)
                        && REG_POINTER (v->add_val))))
              {
                if (! biv_elimination_giv_has_0_offset (bl->biv, v, insn))
@@ -8947,7 +10208,7 @@ maybe_eliminate_biv_1 (loop, x, insn, bl, eliminate_p, where_bb, where_insn)
                && (GET_CODE (v->add_val) == SYMBOL_REF
                    || GET_CODE (v->add_val) == LABEL_REF
                    || GET_CODE (v->add_val) == CONST
-                   || (GET_CODE (v->add_val) == REG
+                   || (REG_P (v->add_val)
                        && REG_POINTER (v->add_val)))
                && ! v->ignore && ! v->maybe_dead && v->always_computable
                && v->mode == mode)
@@ -9036,7 +10297,7 @@ maybe_eliminate_biv_1 (loop, x, insn, bl, eliminate_p, where_bb, where_insn)
                  return 1;
              }
        }
-      else if (GET_CODE (arg) == REG || GET_CODE (arg) == MEM)
+      else if (REG_P (arg) || MEM_P (arg))
        {
          if (loop_invariant_p (loop, arg) == 1)
            {
@@ -9088,7 +10349,7 @@ maybe_eliminate_biv_1 (loop, x, insn, bl, eliminate_p, where_bb, where_insn)
 
 #if 0
          /* Otherwise the reg compared with had better be a biv.  */
-         if (GET_CODE (arg) != REG
+         if (!REG_P (arg)
              || REG_IV_TYPE (ivs, REGNO (arg)) != BASIC_INDUCT)
            return 0;
 
@@ -9168,13 +10429,11 @@ maybe_eliminate_biv_1 (loop, x, insn, bl, eliminate_p, where_bb, where_insn)
    is in an insn following INSN in the same basic block.  */
 
 static int
-last_use_this_basic_block (reg, insn)
-     rtx reg;
-     rtx insn;
+last_use_this_basic_block (rtx reg, rtx insn)
 {
   rtx n;
   for (n = insn;
-       n && GET_CODE (n) != CODE_LABEL && GET_CODE (n) != JUMP_INSN;
+       n && !LABEL_P (n) && !JUMP_P (n);
        n = NEXT_INSN (n))
     {
       if (REGNO_LAST_UID (REGNO (reg)) == INSN_UID (n))
@@ -9187,15 +10446,12 @@ last_use_this_basic_block (reg, insn)
    just record the location of the set and process it later.  */
 
 static void
-record_initial (dest, set, data)
-     rtx dest;
-     rtx set;
-     void *data ATTRIBUTE_UNUSED;
+record_initial (rtx dest, rtx set, void *data ATTRIBUTE_UNUSED)
 {
   struct loop_ivs *ivs = (struct loop_ivs *) data;
   struct iv_class *bl;
 
-  if (GET_CODE (dest) != REG
+  if (!REG_P (dest)
       || REGNO (dest) >= ivs->n_regs
       || REG_IV_TYPE (ivs, REGNO (dest)) != BASIC_INDUCT)
     return;
@@ -9216,9 +10472,7 @@ record_initial (dest, set, data)
    use it.  X must be a source expression only.  */
 
 static void
-update_reg_last_use (x, insn)
-     rtx x;
-     rtx insn;
+update_reg_last_use (rtx x, rtx insn)
 {
   /* Check for the case where INSN does not have a valid luid.  In this case,
      there is no need to modify the regno_last_uid, as this can only happen
@@ -9226,7 +10480,7 @@ update_reg_last_use (x, insn)
      and hence this insn will never be the last use of x.
      ???? This comment is not correct.  See for example loop_givs_reduce.
      This may insert an insn before another new insn.  */
-  if (GET_CODE (x) == REG && REGNO (x) < max_reg_before_loop
+  if (REG_P (x) && REGNO (x) < max_reg_before_loop
       && INSN_UID (insn) < max_uid_for_loop
       && REGNO_LAST_LUID (REGNO (x)) < INSN_LUID (insn))
     {
@@ -9247,313 +10501,13 @@ update_reg_last_use (x, insn)
     }
 }
 \f
-/* Given an insn INSN and condition COND, return the condition in a
-   canonical form to simplify testing by callers.  Specifically:
-
-   (1) The code will always be a comparison operation (EQ, NE, GT, etc.).
-   (2) Both operands will be machine operands; (cc0) will have been replaced.
-   (3) If an operand is a constant, it will be the second operand.
-   (4) (LE x const) will be replaced with (LT x <const+1>) and similarly
-       for GE, GEU, and LEU.
-
-   If the condition cannot be understood, or is an inequality floating-point
-   comparison which needs to be reversed, 0 will be returned.
-
-   If REVERSE is nonzero, then reverse the condition prior to canonizing it.
-
-   If EARLIEST is nonzero, it is a pointer to a place where the earliest
-   insn used in locating the condition was found.  If a replacement test
-   of the condition is desired, it should be placed in front of that
-   insn and we will be sure that the inputs are still valid.
-
-   If WANT_REG is nonzero, we wish the condition to be relative to that
-   register, if possible.  Therefore, do not canonicalize the condition
-   further.  */
-
-rtx
-canonicalize_condition (insn, cond, reverse, earliest, want_reg)
-     rtx insn;
-     rtx cond;
-     int reverse;
-     rtx *earliest;
-     rtx want_reg;
-{
-  enum rtx_code code;
-  rtx prev = insn;
-  rtx set;
-  rtx tem;
-  rtx op0, op1;
-  int reverse_code = 0;
-  enum machine_mode mode;
-
-  code = GET_CODE (cond);
-  mode = GET_MODE (cond);
-  op0 = XEXP (cond, 0);
-  op1 = XEXP (cond, 1);
-
-  if (reverse)
-    code = reversed_comparison_code (cond, insn);
-  if (code == UNKNOWN)
-    return 0;
-
-  if (earliest)
-    *earliest = insn;
-
-  /* If we are comparing a register with zero, see if the register is set
-     in the previous insn to a COMPARE or a comparison operation.  Perform
-     the same tests as a function of STORE_FLAG_VALUE as find_comparison_args
-     in cse.c  */
-
-  while (GET_RTX_CLASS (code) == '<'
-        && op1 == CONST0_RTX (GET_MODE (op0))
-        && op0 != want_reg)
-    {
-      /* Set nonzero when we find something of interest.  */
-      rtx x = 0;
-
-#ifdef HAVE_cc0
-      /* If comparison with cc0, import actual comparison from compare
-        insn.  */
-      if (op0 == cc0_rtx)
-       {
-         if ((prev = prev_nonnote_insn (prev)) == 0
-             || GET_CODE (prev) != INSN
-             || (set = single_set (prev)) == 0
-             || SET_DEST (set) != cc0_rtx)
-           return 0;
-
-         op0 = SET_SRC (set);
-         op1 = CONST0_RTX (GET_MODE (op0));
-         if (earliest)
-           *earliest = prev;
-       }
-#endif
-
-      /* If this is a COMPARE, pick up the two things being compared.  */
-      if (GET_CODE (op0) == COMPARE)
-       {
-         op1 = XEXP (op0, 1);
-         op0 = XEXP (op0, 0);
-         continue;
-       }
-      else if (GET_CODE (op0) != REG)
-       break;
-
-      /* Go back to the previous insn.  Stop if it is not an INSN.  We also
-        stop if it isn't a single set or if it has a REG_INC note because
-        we don't want to bother dealing with it.  */
+/* Similar to rtlanal.c:get_condition, except that we also put an
+   invariant last unless both operands are invariants.  */
 
-      if ((prev = prev_nonnote_insn (prev)) == 0
-         || GET_CODE (prev) != INSN
-         || FIND_REG_INC_NOTE (prev, NULL_RTX))
-       break;
-
-      set = set_of (op0, prev);
-
-      if (set
-         && (GET_CODE (set) != SET
-             || !rtx_equal_p (SET_DEST (set), op0)))
-       break;
-
-      /* If this is setting OP0, get what it sets it to if it looks
-        relevant.  */
-      if (set)
-       {
-         enum machine_mode inner_mode = GET_MODE (SET_DEST (set));
-#ifdef FLOAT_STORE_FLAG_VALUE
-         REAL_VALUE_TYPE fsfv;
-#endif
-
-         /* ??? We may not combine comparisons done in a CCmode with
-            comparisons not done in a CCmode.  This is to aid targets
-            like Alpha that have an IEEE compliant EQ instruction, and
-            a non-IEEE compliant BEQ instruction.  The use of CCmode is
-            actually artificial, simply to prevent the combination, but
-            should not affect other platforms.
-
-            However, we must allow VOIDmode comparisons to match either
-            CCmode or non-CCmode comparison, because some ports have
-            modeless comparisons inside branch patterns.
-
-            ??? This mode check should perhaps look more like the mode check
-            in simplify_comparison in combine.  */
-
-         if ((GET_CODE (SET_SRC (set)) == COMPARE
-              || (((code == NE
-                    || (code == LT
-                        && GET_MODE_CLASS (inner_mode) == MODE_INT
-                        && (GET_MODE_BITSIZE (inner_mode)
-                            <= HOST_BITS_PER_WIDE_INT)
-                        && (STORE_FLAG_VALUE
-                            & ((HOST_WIDE_INT) 1
-                               << (GET_MODE_BITSIZE (inner_mode) - 1))))
-#ifdef FLOAT_STORE_FLAG_VALUE
-                    || (code == LT
-                        && GET_MODE_CLASS (inner_mode) == MODE_FLOAT
-                        && (fsfv = FLOAT_STORE_FLAG_VALUE (inner_mode),
-                            REAL_VALUE_NEGATIVE (fsfv)))
-#endif
-                    ))
-                  && GET_RTX_CLASS (GET_CODE (SET_SRC (set))) == '<'))
-             && (((GET_MODE_CLASS (mode) == MODE_CC)
-                  == (GET_MODE_CLASS (inner_mode) == MODE_CC))
-                 || mode == VOIDmode || inner_mode == VOIDmode))
-           x = SET_SRC (set);
-         else if (((code == EQ
-                    || (code == GE
-                        && (GET_MODE_BITSIZE (inner_mode)
-                            <= HOST_BITS_PER_WIDE_INT)
-                        && GET_MODE_CLASS (inner_mode) == MODE_INT
-                        && (STORE_FLAG_VALUE
-                            & ((HOST_WIDE_INT) 1
-                               << (GET_MODE_BITSIZE (inner_mode) - 1))))
-#ifdef FLOAT_STORE_FLAG_VALUE
-                    || (code == GE
-                        && GET_MODE_CLASS (inner_mode) == MODE_FLOAT
-                        && (fsfv = FLOAT_STORE_FLAG_VALUE (inner_mode),
-                            REAL_VALUE_NEGATIVE (fsfv)))
-#endif
-                    ))
-                  && GET_RTX_CLASS (GET_CODE (SET_SRC (set))) == '<'
-                  && (((GET_MODE_CLASS (mode) == MODE_CC)
-                       == (GET_MODE_CLASS (inner_mode) == MODE_CC))
-                      || mode == VOIDmode || inner_mode == VOIDmode))
-
-           {
-             reverse_code = 1;
-             x = SET_SRC (set);
-           }
-         else
-           break;
-       }
-
-      else if (reg_set_p (op0, prev))
-       /* If this sets OP0, but not directly, we have to give up.  */
-       break;
-
-      if (x)
-       {
-         if (GET_RTX_CLASS (GET_CODE (x)) == '<')
-           code = GET_CODE (x);
-         if (reverse_code)
-           {
-             code = reversed_comparison_code (x, prev);
-             if (code == UNKNOWN)
-               return 0;
-             reverse_code = 0;
-           }
-
-         op0 = XEXP (x, 0), op1 = XEXP (x, 1);
-         if (earliest)
-           *earliest = prev;
-       }
-    }
-
-  /* If constant is first, put it last.  */
-  if (CONSTANT_P (op0))
-    code = swap_condition (code), tem = op0, op0 = op1, op1 = tem;
-
-  /* If OP0 is the result of a comparison, we weren't able to find what
-     was really being compared, so fail.  */
-  if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC)
-    return 0;
-
-  /* Canonicalize any ordered comparison with integers involving equality
-     if we can do computations in the relevant mode and we do not
-     overflow.  */
-
-  if (GET_CODE (op1) == CONST_INT
-      && GET_MODE (op0) != VOIDmode
-      && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT)
-    {
-      HOST_WIDE_INT const_val = INTVAL (op1);
-      unsigned HOST_WIDE_INT uconst_val = const_val;
-      unsigned HOST_WIDE_INT max_val
-       = (unsigned HOST_WIDE_INT) GET_MODE_MASK (GET_MODE (op0));
-
-      switch (code)
-       {
-       case LE:
-         if ((unsigned HOST_WIDE_INT) const_val != max_val >> 1)
-           code = LT, op1 = gen_int_mode (const_val + 1, GET_MODE (op0));
-         break;
-
-       /* When cross-compiling, const_val might be sign-extended from
-          BITS_PER_WORD to HOST_BITS_PER_WIDE_INT */
-       case GE:
-         if ((HOST_WIDE_INT) (const_val & max_val)
-             != (((HOST_WIDE_INT) 1
-                  << (GET_MODE_BITSIZE (GET_MODE (op0)) - 1))))
-           code = GT, op1 = gen_int_mode (const_val - 1, GET_MODE (op0));
-         break;
-
-       case LEU:
-         if (uconst_val < max_val)
-           code = LTU, op1 = gen_int_mode (uconst_val + 1, GET_MODE (op0));
-         break;
-
-       case GEU:
-         if (uconst_val != 0)
-           code = GTU, op1 = gen_int_mode (uconst_val - 1, GET_MODE (op0));
-         break;
-
-       default:
-         break;
-       }
-    }
-
-  /* Never return CC0; return zero instead.  */
-  if (CC0_P (op0))
-    return 0;
-
-  return gen_rtx_fmt_ee (code, VOIDmode, op0, op1);
-}
-
-/* Given a jump insn JUMP, return the condition that will cause it to branch
-   to its JUMP_LABEL.  If the condition cannot be understood, or is an
-   inequality floating-point comparison which needs to be reversed, 0 will
-   be returned.
-
-   If EARLIEST is nonzero, it is a pointer to a place where the earliest
-   insn used in locating the condition was found.  If a replacement test
-   of the condition is desired, it should be placed in front of that
-   insn and we will be sure that the inputs are still valid.  */
-
-rtx
-get_condition (jump, earliest)
-     rtx jump;
-     rtx *earliest;
-{
-  rtx cond;
-  int reverse;
-  rtx set;
-
-  /* If this is not a standard conditional jump, we can't parse it.  */
-  if (GET_CODE (jump) != JUMP_INSN
-      || ! any_condjump_p (jump))
-    return 0;
-  set = pc_set (jump);
-
-  cond = XEXP (SET_SRC (set), 0);
-
-  /* If this branches to JUMP_LABEL when the condition is false, reverse
-     the condition.  */
-  reverse
-    = GET_CODE (XEXP (SET_SRC (set), 2)) == LABEL_REF
-      && XEXP (XEXP (SET_SRC (set), 2), 0) == JUMP_LABEL (jump);
-
-  return canonicalize_condition (jump, cond, reverse, earliest, NULL_RTX);
-}
-
-/* Similar to above routine, except that we also put an invariant last
-   unless both operands are invariants.  */
-
-rtx
-get_condition_for_loop (loop, x)
-     const struct loop *loop;
-     rtx x;
+static rtx
+get_condition_for_loop (const struct loop *loop, rtx x)
 {
-  rtx comparison = get_condition (x, (rtx*) 0);
+  rtx comparison = get_condition (x, (rtx*) 0, false, true);
 
   if (comparison == 0
       || ! loop_invariant_p (loop, XEXP (comparison, 0))
@@ -9569,8 +10523,7 @@ get_condition_for_loop (loop, x)
    This is taken mostly from flow.c; similar code exists elsewhere
    in the compiler.  It may be useful to put this into rtlanal.c.  */
 static int
-indirect_jump_in_function_p (start)
-     rtx start;
+indirect_jump_in_function_p (rtx start)
 {
   rtx insn;
 
@@ -9586,9 +10539,7 @@ indirect_jump_in_function_p (start)
    This function is called from prescan_loop via for_each_rtx.  */
 
 static int
-insert_loop_mem (mem, data)
-     rtx *mem;
-     void *data ATTRIBUTE_UNUSED;
+insert_loop_mem (rtx *mem, void *data ATTRIBUTE_UNUSED)
 {
   struct loop_info *loop_info = data;
   int i;
@@ -9624,6 +10575,8 @@ insert_loop_mem (mem, data)
   for (i = 0; i < loop_info->mems_idx; ++i)
     if (rtx_equal_p (m, loop_info->mems[i].mem))
       {
+       if (MEM_VOLATILE_P (m) && !MEM_VOLATILE_P (loop_info->mems[i].mem))
+         loop_info->mems[i].mem = m;
        if (GET_MODE (m) != GET_MODE (loop_info->mems[i].mem))
          /* The modes of the two memory accesses are different.  If
             this happens, something tricky is going on, and we just
@@ -9641,9 +10594,8 @@ insert_loop_mem (mem, data)
       else
        loop_info->mems_allocated = 32;
 
-      loop_info->mems = (loop_mem_info *)
-       xrealloc (loop_info->mems,
-                 loop_info->mems_allocated * sizeof (loop_mem_info));
+      loop_info->mems = xrealloc (loop_info->mems,
+                                 loop_info->mems_allocated * sizeof (loop_mem_info));
     }
 
   /* Actually insert the MEM.  */
@@ -9676,9 +10628,7 @@ insert_loop_mem (mem, data)
    optimize register I.  */
 
 static void
-loop_regs_scan (loop, extra_size)
-     const struct loop *loop;
-     int extra_size;
+loop_regs_scan (const struct loop *loop, int extra_size)
 {
   struct loop_regs *regs = LOOP_REGS (loop);
   int old_nregs;
@@ -9696,8 +10646,7 @@ loop_regs_scan (loop, extra_size)
     {
       regs->size = regs->num + extra_size;
 
-      regs->array = (struct loop_reg *)
-       xrealloc (regs->array, regs->size * sizeof (*regs->array));
+      regs->array = xrealloc (regs->array, regs->size * sizeof (*regs->array));
 
       /* Zero the new elements.  */
       memset (regs->array + old_nregs, 0,
@@ -9712,7 +10661,7 @@ loop_regs_scan (loop, extra_size)
       regs->array[i].single_usage = NULL_RTX;
     }
 
-  last_set = (rtx *) xcalloc (regs->num, sizeof (rtx));
+  last_set = xcalloc (regs->num, sizeof (rtx));
 
   /* Scan the loop, recording register usage.  */
   for (insn = loop->top ? loop->top : loop->start; insn != loop->end;
@@ -9739,23 +10688,23 @@ loop_regs_scan (loop, extra_size)
            }
        }
 
-      if (GET_CODE (insn) == CODE_LABEL || GET_CODE (insn) == JUMP_INSN)
+      if (LABEL_P (insn) || JUMP_P (insn))
        memset (last_set, 0, regs->num * sizeof (rtx));
 
       /* Invalidate all registers used for function argument passing.
         We check rtx_varies_p for the same reason as below, to allow
         optimizing PIC calculations.  */
-      if (GET_CODE (insn) == CALL_INSN)
+      if (CALL_P (insn))
        {
          rtx link;
-         for (link = CALL_INSN_FUNCTION_USAGE (insn); 
-              link; 
+         for (link = CALL_INSN_FUNCTION_USAGE (insn);
+              link;
               link = XEXP (link, 1))
            {
              rtx op, reg;
 
              if (GET_CODE (op = XEXP (link, 0)) == USE
-                 && GET_CODE (reg = XEXP (op, 0)) == REG
+                 && REG_P (reg = XEXP (op, 0))
                  && rtx_varies_p (reg, 1))
                regs->array[REGNO (reg)].may_not_optimize = 1;
            }
@@ -9793,8 +10742,7 @@ loop_regs_scan (loop, extra_size)
 /* Returns the number of real INSNs in the LOOP.  */
 
 static int
-count_insns_in_loop (loop)
-     const struct loop *loop;
+count_insns_in_loop (const struct loop *loop)
 {
   int count = 0;
   rtx insn;
@@ -9810,8 +10758,7 @@ count_insns_in_loop (loop)
 /* Move MEMs into registers for the duration of the loop.  */
 
 static void
-load_mems (loop)
-     const struct loop *loop;
+load_mems (const struct loop *loop)
 {
   struct loop_info *loop_info = LOOP_INFO (loop);
   struct loop_regs *regs = LOOP_REGS (loop);
@@ -9829,7 +10776,7 @@ load_mems (loop)
 
   /* We cannot use next_label here because it skips over normal insns.  */
   end_label = next_nonnote_insn (loop->end);
-  if (end_label && GET_CODE (end_label) != CODE_LABEL)
+  if (end_label && !LABEL_P (end_label))
     end_label = NULL_RTX;
 
   /* Check to see if it's possible that some instructions in the loop are
@@ -9839,16 +10786,16 @@ load_mems (loop)
        p != NULL_RTX;
        p = next_insn_in_loop (loop, p))
     {
-      if (GET_CODE (p) == CODE_LABEL)
+      if (LABEL_P (p))
        maybe_never = 1;
-      else if (GET_CODE (p) == JUMP_INSN
+      else if (JUMP_P (p)
               /* If we enter the loop in the middle, and scan
                  around to the beginning, don't set maybe_never
                  for that.  This must be an unconditional jump,
                  otherwise the code at the top of the loop might
                  never be executed.  Unconditional jumps are
                  followed a by barrier then loop end.  */
-              && ! (GET_CODE (p) == JUMP_INSN
+              && ! (JUMP_P (p)
                     && JUMP_LABEL (p) == loop->top
                     && NEXT_INSN (NEXT_INSN (p)) == loop->end
                     && any_uncondjump_p (p)))
@@ -9879,12 +10826,12 @@ load_mems (loop)
 
   /* Find start of the extended basic block that enters the loop.  */
   for (p = loop->start;
-       PREV_INSN (p) && GET_CODE (p) != CODE_LABEL;
+       PREV_INSN (p) && !LABEL_P (p);
        p = PREV_INSN (p))
     ;
   prev_ebb_head = p;
 
-  cselib_init ();
+  cselib_init (true);
 
   /* Build table of mems that get set to constant values before the
      loop.  */
@@ -9991,7 +10938,7 @@ load_mems (loop)
              if (set
                  /* @@@ This test is _way_ too conservative.  */
                  && ! maybe_never
-                 && GET_CODE (SET_DEST (set)) == REG
+                 && REG_P (SET_DEST (set))
                  && REGNO (SET_DEST (set)) >= FIRST_PSEUDO_REGISTER
                  && REGNO (SET_DEST (set)) < last_max_reg
                  && regs->array[REGNO (SET_DEST (set))].n_times_set == 1
@@ -10005,7 +10952,7 @@ load_mems (loop)
                 to untangle things for the BIV detection code.  */
              if (set
                  && ! maybe_never
-                 && GET_CODE (SET_SRC (set)) == REG
+                 && REG_P (SET_SRC (set))
                  && REGNO (SET_SRC (set)) >= FIRST_PSEUDO_REGISTER
                  && REGNO (SET_SRC (set)) < last_max_reg
                  && regs->array[REGNO (SET_SRC (set))].n_times_set == 1
@@ -10014,7 +10961,7 @@ load_mems (loop)
 
              /* If this is a call which uses / clobbers this memory
                 location, we must not change the interface here.  */
-             if (GET_CODE (p) == CALL_INSN
+             if (CALL_P (p)
                  && reg_mentioned_p (loop_info->mems[i].mem,
                                      CALL_INSN_FUNCTION_USAGE (p)))
                {
@@ -10025,11 +10972,11 @@ load_mems (loop)
              else
                /* Replace the memory reference with the shadow register.  */
                replace_loop_mems (p, loop_info->mems[i].mem,
-                                  loop_info->mems[i].reg);
+                                  loop_info->mems[i].reg, written);
            }
 
-         if (GET_CODE (p) == CODE_LABEL
-             || GET_CODE (p) == JUMP_INSN)
+         if (LABEL_P (p)
+             || JUMP_P (p))
            maybe_never = 1;
        }
 
@@ -10045,8 +10992,9 @@ load_mems (loop)
          cselib_val *e = cselib_lookup (mem, VOIDmode, 0);
          rtx set;
          rtx best = mem;
-         int j;
+         unsigned j;
          struct elt_loc_list *const_equiv = 0;
+         reg_set_iterator rsi;
 
          if (e)
            {
@@ -10056,7 +11004,7 @@ load_mems (loop)
                {
                  if (CONSTANT_P (equiv->loc))
                    const_equiv = equiv;
-                 else if (GET_CODE (equiv->loc) == REG
+                 else if (REG_P (equiv->loc)
                           /* Extending hard register lifetimes causes crash
                              on SRC targets.  Doing so on non-SRC is
                              probably also not good idea, since we most
@@ -10125,43 +11073,27 @@ load_mems (loop)
             data flow, and enables {basic,general}_induction_var to find
             more bivs/givs.  */
          EXECUTE_IF_SET_IN_REG_SET
-           (&load_copies, FIRST_PSEUDO_REGISTER, j,
-            {
-              try_copy_prop (loop, reg, j);
-            });
+           (&load_copies, FIRST_PSEUDO_REGISTER, j, rsi)
+           {
+             try_copy_prop (loop, reg, j);
+           }
          CLEAR_REG_SET (&load_copies);
 
          EXECUTE_IF_SET_IN_REG_SET
-           (&store_copies, FIRST_PSEUDO_REGISTER, j,
-            {
-              try_swap_copy_prop (loop, reg, j);
-            });
+           (&store_copies, FIRST_PSEUDO_REGISTER, j, rsi)
+           {
+             try_swap_copy_prop (loop, reg, j);
+           }
          CLEAR_REG_SET (&store_copies);
        }
     }
 
+  /* Now, we need to replace all references to the previous exit
+     label with the new one.  */
   if (label != NULL_RTX && end_label != NULL_RTX)
-    {
-      /* Now, we need to replace all references to the previous exit
-        label with the new one.  */
-      rtx_pair rr;
-      rr.r1 = end_label;
-      rr.r2 = label;
-
-      for (p = loop->start; p != loop->end; p = NEXT_INSN (p))
-       {
-         for_each_rtx (&p, replace_label, &rr);
-
-         /* If this is a JUMP_INSN, then we also need to fix the JUMP_LABEL
-            field.  This is not handled by for_each_rtx because it doesn't
-            handle unprinted ('0') fields.  We need to update JUMP_LABEL
-            because the immediately following unroll pass will use it.
-            replace_label would not work anyways, because that only handles
-            LABEL_REFs.  */
-         if (GET_CODE (p) == JUMP_INSN && JUMP_LABEL (p) == end_label)
-           JUMP_LABEL (p) = label;
-       }
-    }
+    for (p = loop->start; p != loop->end; p = NEXT_INSN (p))
+      if (JUMP_P (p) && JUMP_LABEL (p) == end_label)
+       redirect_jump (p, label, false);
 
   cselib_finish ();
 }
@@ -10176,9 +11108,7 @@ struct note_reg_stored_arg
 /* Called via note_stores, record in SET_SEEN whether X, which is written,
    is equal to ARG.  */
 static void
-note_reg_stored (x, setter, arg)
-     rtx x, setter ATTRIBUTE_UNUSED;
-     void *arg;
+note_reg_stored (rtx x, rtx setter ATTRIBUTE_UNUSED, void *arg)
 {
   struct note_reg_stored_arg *t = (struct note_reg_stored_arg *) arg;
   if (t->reg == x)
@@ -10191,10 +11121,7 @@ note_reg_stored (x, setter, arg)
    is not used after the loop.  */
 
 static void
-try_copy_prop (loop, replacement, regno)
-     const struct loop *loop;
-     rtx replacement;
-     unsigned int regno;
+try_copy_prop (const struct loop *loop, rtx replacement, unsigned int regno)
 {
   /* This is the reg that we are copying from.  */
   rtx reg_rtx = regno_reg_rtx[regno];
@@ -10212,7 +11139,7 @@ try_copy_prop (loop, replacement, regno)
 
       /* Only substitute within one extended basic block from the initializing
          insn.  */
-      if (GET_CODE (insn) == CODE_LABEL && init_insn)
+      if (LABEL_P (insn) && init_insn)
        break;
 
       if (! INSN_P (insn))
@@ -10221,11 +11148,10 @@ try_copy_prop (loop, replacement, regno)
       /* Is this the initializing insn?  */
       set = single_set (insn);
       if (set
-         && GET_CODE (SET_DEST (set)) == REG
+         && REG_P (SET_DEST (set))
          && REGNO (SET_DEST (set)) == regno)
        {
-         if (init_insn)
-           abort ();
+         gcc_assert (!init_insn);
 
          init_insn = insn;
          if (REGNO_FIRST_UID (regno) == INSN_UID (insn))
@@ -10258,8 +11184,7 @@ try_copy_prop (loop, replacement, regno)
            }
        }
     }
-  if (! init_insn)
-    abort ();
+  gcc_assert (init_insn);
   if (apply_change_group ())
     {
       if (loop_dump_stream)
@@ -10291,9 +11216,7 @@ try_copy_prop (loop, replacement, regno)
    with NOTE_INSN_DELETED notes.  */
 
 static void
-loop_delete_insns (first, last)
-     rtx first;
-     rtx last;
+loop_delete_insns (rtx first, rtx last)
 {
   while (1)
     {
@@ -10317,10 +11240,8 @@ loop_delete_insns (first, last)
    this pseudo followed immediately by a move insn that sets
    REPLACEMENT with REGNO.  */
 static void
-try_swap_copy_prop (loop, replacement, regno)
-     const struct loop *loop;
-     rtx replacement;
-     unsigned int regno;
+try_swap_copy_prop (const struct loop *loop, rtx replacement,
+                   unsigned int regno)
 {
   rtx insn;
   rtx set = NULL_RTX;
@@ -10335,9 +11256,9 @@ try_swap_copy_prop (loop, replacement, regno)
       /* Search for the insn that copies REGNO to NEW_REGNO?  */
       if (INSN_P (insn)
          && (set = single_set (insn))
-         && GET_CODE (SET_DEST (set)) == REG
+         && REG_P (SET_DEST (set))
          && REGNO (SET_DEST (set)) == new_regno
-         && GET_CODE (SET_SRC (set)) == REG
+         && REG_P (SET_SRC (set))
          && REGNO (SET_SRC (set)) == regno)
        break;
     }
@@ -10355,7 +11276,7 @@ try_swap_copy_prop (loop, replacement, regno)
 
       if (INSN_P (insn)
          && (prev_set = single_set (prev_insn))
-         && GET_CODE (SET_DEST (prev_set)) == REG
+         && REG_P (SET_DEST (prev_set))
          && REGNO (SET_DEST (prev_set)) == regno)
        {
          /* We have:
@@ -10398,15 +11319,37 @@ try_swap_copy_prop (loop, replacement, regno)
     }
 }
 
+/* Worker function for find_mem_in_note, called via for_each_rtx.  */
+
+static int
+find_mem_in_note_1 (rtx *x, void *data)
+{
+  if (*x != NULL_RTX && MEM_P (*x))
+    {
+      rtx *res = (rtx *) data;
+      *res = *x;
+      return 1;
+    }
+  return 0;
+}
+
+/* Returns the first MEM found in NOTE by depth-first search.  */
+
+static rtx
+find_mem_in_note (rtx note)
+{
+  if (note && for_each_rtx (&note, find_mem_in_note_1, &note))
+    return note;
+  return NULL_RTX;
+}
+
 /* Replace MEM with its associated pseudo register.  This function is
    called from load_mems via for_each_rtx.  DATA is actually a pointer
    to a structure describing the instruction currently being scanned
    and the MEM we are currently replacing.  */
 
 static int
-replace_loop_mem (mem, data)
-     rtx *mem;
-     void *data;
+replace_loop_mem (rtx *mem, void *data)
 {
   loop_replace_args *args = (loop_replace_args *) data;
   rtx m = *mem;
@@ -10440,10 +11383,7 @@ replace_loop_mem (mem, data)
 }
 
 static void
-replace_loop_mems (insn, mem, reg)
-     rtx insn;
-     rtx mem;
-     rtx reg;
+replace_loop_mems (rtx insn, rtx mem, rtx reg, int written)
 {
   loop_replace_args args;
 
@@ -10452,6 +11392,26 @@ replace_loop_mems (insn, mem, reg)
   args.replacement = reg;
 
   for_each_rtx (&insn, replace_loop_mem, &args);
+
+  /* If we hoist a mem write out of the loop, then REG_EQUAL
+     notes referring to the mem are no longer valid.  */
+  if (written)
+    {
+      rtx note, sub;
+      rtx *link;
+
+      for (link = &REG_NOTES (insn); (note = *link); link = &XEXP (note, 1))
+       {
+         if (REG_NOTE_KIND (note) == REG_EQUAL
+             && (sub = find_mem_in_note (note))
+             && true_dependence (mem, VOIDmode, sub, rtx_varies_p))
+           {
+             /* Remove the note.  */
+             validate_change (NULL_RTX, link, XEXP (note, 1), 1);
+             break;
+           }
+       }
+    }
 }
 
 /* Replace one register with another.  Called through for_each_rtx; PX points
@@ -10459,9 +11419,7 @@ replace_loop_mems (insn, mem, reg)
    a structure of arguments.  */
 
 static int
-replace_loop_reg (px, data)
-     rtx *px;
-     void *data;
+replace_loop_reg (rtx *px, void *data)
 {
   rtx x = *px;
   loop_replace_args *args = (loop_replace_args *) data;
@@ -10476,10 +11434,7 @@ replace_loop_reg (px, data)
 }
 
 static void
-replace_loop_regs (insn, reg, replacement)
-     rtx insn;
-     rtx reg;
-     rtx replacement;
+replace_loop_regs (rtx insn, rtx reg, rtx replacement)
 {
   loop_replace_args args;
 
@@ -10489,45 +11444,14 @@ replace_loop_regs (insn, reg, replacement)
 
   for_each_rtx (&insn, replace_loop_reg, &args);
 }
-
-/* Replace occurrences of the old exit label for the loop with the new
-   one.  DATA is an rtx_pair containing the old and new labels,
-   respectively.  */
-
-static int
-replace_label (x, data)
-     rtx *x;
-     void *data;
-{
-  rtx l = *x;
-  rtx old_label = ((rtx_pair *) data)->r1;
-  rtx new_label = ((rtx_pair *) data)->r2;
-
-  if (l == NULL_RTX)
-    return 0;
-
-  if (GET_CODE (l) != LABEL_REF)
-    return 0;
-
-  if (XEXP (l, 0) != old_label)
-    return 0;
-
-  XEXP (l, 0) = new_label;
-  ++LABEL_NUSES (new_label);
-  --LABEL_NUSES (old_label);
-
-  return 0;
-}
 \f
 /* Emit insn for PATTERN after WHERE_INSN in basic block WHERE_BB
    (ignored in the interim).  */
 
 static rtx
-loop_insn_emit_after (loop, where_bb, where_insn, pattern)
-     const struct loop *loop ATTRIBUTE_UNUSED;
-     basic_block where_bb ATTRIBUTE_UNUSED;
-     rtx where_insn;
-     rtx pattern;
+loop_insn_emit_after (const struct loop *loop ATTRIBUTE_UNUSED,
+                     basic_block where_bb ATTRIBUTE_UNUSED, rtx where_insn,
+                     rtx pattern)
 {
   return emit_insn_after (pattern, where_insn);
 }
@@ -10537,12 +11461,10 @@ loop_insn_emit_after (loop, where_bb, where_insn, pattern)
    in basic block WHERE_BB (ignored in the interim) within the loop
    otherwise hoist PATTERN into the loop pre-header.  */
 
-rtx
-loop_insn_emit_before (loop, where_bb, where_insn, pattern)
-     const struct loop *loop;
-     basic_block where_bb ATTRIBUTE_UNUSED;
-     rtx where_insn;
-     rtx pattern;
+static rtx
+loop_insn_emit_before (const struct loop *loop,
+                      basic_block where_bb ATTRIBUTE_UNUSED,
+                      rtx where_insn, rtx pattern)
 {
   if (! where_insn)
     return loop_insn_hoist (loop, pattern);
@@ -10554,11 +11476,9 @@ loop_insn_emit_before (loop, where_bb, where_insn, pattern)
    WHERE_BB (ignored in the interim) within the loop.  */
 
 static rtx
-loop_call_insn_emit_before (loop, where_bb, where_insn, pattern)
-     const struct loop *loop ATTRIBUTE_UNUSED;
-     basic_block where_bb ATTRIBUTE_UNUSED;
-     rtx where_insn;
-     rtx pattern;
+loop_call_insn_emit_before (const struct loop *loop ATTRIBUTE_UNUSED,
+                           basic_block where_bb ATTRIBUTE_UNUSED,
+                           rtx where_insn, rtx pattern)
 {
   return emit_call_insn_before (pattern, where_insn);
 }
@@ -10566,10 +11486,8 @@ loop_call_insn_emit_before (loop, where_bb, where_insn, pattern)
 
 /* Hoist insn for PATTERN into the loop pre-header.  */
 
-rtx
-loop_insn_hoist (loop, pattern)
-     const struct loop *loop;
-     rtx pattern;
+static rtx
+loop_insn_hoist (const struct loop *loop, rtx pattern)
 {
   return loop_insn_emit_before (loop, 0, loop->start, pattern);
 }
@@ -10578,9 +11496,7 @@ loop_insn_hoist (loop, pattern)
 /* Hoist call insn for PATTERN into the loop pre-header.  */
 
 static rtx
-loop_call_insn_hoist (loop, pattern)
-     const struct loop *loop;
-     rtx pattern;
+loop_call_insn_hoist (const struct loop *loop, rtx pattern)
 {
   return loop_call_insn_emit_before (loop, 0, loop->start, pattern);
 }
@@ -10588,19 +11504,16 @@ loop_call_insn_hoist (loop, pattern)
 
 /* Sink insn for PATTERN after the loop end.  */
 
-rtx
-loop_insn_sink (loop, pattern)
-     const struct loop *loop;
-     rtx pattern;
+static rtx
+loop_insn_sink (const struct loop *loop, rtx pattern)
 {
   return loop_insn_emit_before (loop, 0, loop->sink, pattern);
 }
 
-/* bl->final_value can be eighter general_operand or PLUS of general_operand
+/* bl->final_value can be either general_operand or PLUS of general_operand
    and constant.  Emit sequence of instructions to load it into REG.  */
 static rtx
-gen_load_of_final_value (reg, final_value)
-     rtx reg, final_value;
+gen_load_of_final_value (rtx reg, rtx final_value)
 {
   rtx seq;
   start_sequence ();
@@ -10618,9 +11531,7 @@ gen_load_of_final_value (reg, final_value)
    since this is slightly more efficient.  */
 
 static rtx
-loop_insn_sink_or_swim (loop, pattern)
-     const struct loop *loop;
-     rtx pattern;
+loop_insn_sink_or_swim (const struct loop *loop, rtx pattern)
 {
   if (loop->exit_count)
     return loop_insn_hoist (loop, pattern);
@@ -10629,10 +11540,7 @@ loop_insn_sink_or_swim (loop, pattern)
 }
 \f
 static void
-loop_ivs_dump (loop, file, verbose)
-     const struct loop *loop;
-     FILE *file;
-     int verbose;
+loop_ivs_dump (const struct loop *loop, FILE *file, int verbose)
 {
   struct iv_class *bl;
   int iv_num = 0;
@@ -10654,10 +11562,8 @@ loop_ivs_dump (loop, file, verbose)
 
 
 static void
-loop_iv_class_dump (bl, file, verbose)
-     const struct iv_class *bl;
-     FILE *file;
-     int verbose ATTRIBUTE_UNUSED;
+loop_iv_class_dump (const struct iv_class *bl, FILE *file,
+                   int verbose ATTRIBUTE_UNUSED)
 {
   struct induction *v;
   rtx incr;
@@ -10719,10 +11625,7 @@ loop_iv_class_dump (bl, file, verbose)
 
 
 static void
-loop_biv_dump (v, file, verbose)
-     const struct induction *v;
-     FILE *file;
-     int verbose;
+loop_biv_dump (const struct induction *v, FILE *file, int verbose)
 {
   if (! v || ! file)
     return;
@@ -10745,10 +11648,7 @@ loop_biv_dump (v, file, verbose)
 
 
 static void
-loop_giv_dump (v, file, verbose)
-     const struct induction *v;
-     FILE *file;
-     int verbose;
+loop_giv_dump (const struct induction *v, FILE *file, int verbose)
 {
   if (! v || ! file)
     return;
@@ -10785,7 +11685,7 @@ loop_giv_dump (v, file, verbose)
          fprintf (file, " ext tr");
          break;
        default:
-         abort ();
+         gcc_unreachable ();
        }
     }
 
@@ -10809,32 +11709,28 @@ loop_giv_dump (v, file, verbose)
 
 
 void
-debug_ivs (loop)
-     const struct loop *loop;
+debug_ivs (const struct loop *loop)
 {
   loop_ivs_dump (loop, stderr, 1);
 }
 
 
 void
-debug_iv_class (bl)
-     const struct iv_class *bl;
+debug_iv_class (const struct iv_class *bl)
 {
   loop_iv_class_dump (bl, stderr, 1);
 }
 
 
 void
-debug_biv (v)
-     const struct induction *v;
+debug_biv (const struct induction *v)
 {
   loop_biv_dump (v, stderr, 1);
 }
 
 
 void
-debug_giv (v)
-     const struct induction *v;
+debug_giv (const struct induction *v)
 {
   loop_giv_dump (v, stderr, 1);
 }
@@ -10845,7 +11741,7 @@ debug_giv (v)
 
 /* The notes do not have an assigned block, so look at the next insn.  */
 #define LOOP_BLOCK_NUM(INSN) \
-((INSN) ? (GET_CODE (INSN) == NOTE \
+((INSN) ? (NOTE_P (INSN) \
             ? LOOP_BLOCK_NUM_1 (next_nonnote_insn (INSN)) \
             : LOOP_BLOCK_NUM_1 (INSN)) \
         : -1)
@@ -10853,43 +11749,35 @@ debug_giv (v)
 #define LOOP_INSN_UID(INSN) ((INSN) ? INSN_UID (INSN) : -1)
 
 static void
-loop_dump_aux (loop, file, verbose)
-     const struct loop *loop;
-     FILE *file;
-     int verbose ATTRIBUTE_UNUSED;
+loop_dump_aux (const struct loop *loop, FILE *file,
+              int verbose ATTRIBUTE_UNUSED)
 {
   rtx label;
 
-  if (! loop || ! file)
+  if (! loop || ! file || !BB_HEAD (loop->first))
     return;
 
   /* Print diagnostics to compare our concept of a loop with
      what the loop notes say.  */
-  if (! PREV_INSN (loop->first->head)
-      || GET_CODE (PREV_INSN (loop->first->head)) != NOTE
-      || NOTE_LINE_NUMBER (PREV_INSN (loop->first->head))
+  if (! PREV_INSN (BB_HEAD (loop->first))
+      || !NOTE_P (PREV_INSN (BB_HEAD (loop->first)))
+      || NOTE_LINE_NUMBER (PREV_INSN (BB_HEAD (loop->first)))
       != NOTE_INSN_LOOP_BEG)
     fprintf (file, ";;  No NOTE_INSN_LOOP_BEG at %d\n",
-            INSN_UID (PREV_INSN (loop->first->head)));
-  if (! NEXT_INSN (loop->last->end)
-      || GET_CODE (NEXT_INSN (loop->last->end)) != NOTE
-      || NOTE_LINE_NUMBER (NEXT_INSN (loop->last->end))
+            INSN_UID (PREV_INSN (BB_HEAD (loop->first))));
+  if (! NEXT_INSN (BB_END (loop->last))
+      || !NOTE_P (NEXT_INSN (BB_END (loop->last)))
+      || NOTE_LINE_NUMBER (NEXT_INSN (BB_END (loop->last)))
       != NOTE_INSN_LOOP_END)
     fprintf (file, ";;  No NOTE_INSN_LOOP_END at %d\n",
-            INSN_UID (NEXT_INSN (loop->last->end)));
+            INSN_UID (NEXT_INSN (BB_END (loop->last))));
 
   if (loop->start)
     {
       fprintf (file,
-              ";;  start %d (%d), cont dom %d (%d), cont %d (%d), vtop %d (%d), end %d (%d)\n",
+              ";;  start %d (%d), end %d (%d)\n",
               LOOP_BLOCK_NUM (loop->start),
               LOOP_INSN_UID (loop->start),
-              LOOP_BLOCK_NUM (loop->cont),
-              LOOP_INSN_UID (loop->cont),
-              LOOP_BLOCK_NUM (loop->cont),
-              LOOP_INSN_UID (loop->cont),
-              LOOP_BLOCK_NUM (loop->vtop),
-              LOOP_INSN_UID (loop->vtop),
               LOOP_BLOCK_NUM (loop->end),
               LOOP_INSN_UID (loop->end));
       fprintf (file, ";;  top %d (%d), scan start %d (%d)\n",
@@ -10908,20 +11796,13 @@ loop_dump_aux (loop, file, verbose)
            }
        }
       fputs ("\n", file);
-
-      /* This can happen when a marked loop appears as two nested loops,
-        say from while (a || b) {}.  The inner loop won't match
-        the loop markers but the outer one will.  */
-      if (LOOP_BLOCK_NUM (loop->cont) != loop->latch->index)
-       fprintf (file, ";;  NOTE_INSN_LOOP_CONT not in loop latch\n");
     }
 }
 
 /* Call this function from the debugger to dump LOOP.  */
 
 void
-debug_loop (loop)
-     const struct loop *loop;
+debug_loop (const struct loop *loop)
 {
   flow_loop_dump (loop, stderr, loop_dump_aux, 1);
 }
@@ -10929,8 +11810,70 @@ debug_loop (loop)
 /* Call this function from the debugger to dump LOOPS.  */
 
 void
-debug_loops (loops)
-     const struct loops *loops;
+debug_loops (const struct loops *loops)
 {
   flow_loops_dump (loops, stderr, loop_dump_aux, 1);
 }
+\f
+static bool
+gate_handle_loop_optimize (void)
+{
+  return (optimize > 0 && flag_loop_optimize);
+}
+
+/* Move constant computations out of loops.  */
+static void
+rest_of_handle_loop_optimize (void)
+{
+  int do_prefetch;
+
+  /* CFG is no longer maintained up-to-date.  */
+  free_bb_for_insn ();
+  profile_status = PROFILE_ABSENT;
+  
+  do_prefetch = flag_prefetch_loop_arrays ? LOOP_PREFETCH : 0;
+  
+  if (flag_rerun_loop_opt)
+    {
+      cleanup_barriers ();
+      
+      /* We only want to perform unrolling once.  */
+      loop_optimize (get_insns (), dump_file, 0);
+      
+      /* The first call to loop_optimize makes some instructions
+         trivially dead.  We delete those instructions now in the
+         hope that doing so will make the heuristics in loop work
+         better and possibly speed up compilation.  */
+      delete_trivially_dead_insns (get_insns (), max_reg_num ());
+  
+      /* The regscan pass is currently necessary as the alias
+         analysis code depends on this information.  */
+      reg_scan (get_insns (), max_reg_num ());
+    } 
+  cleanup_barriers ();
+  loop_optimize (get_insns (), dump_file, do_prefetch);
+      
+  /* Loop can create trivially dead instructions.  */
+  delete_trivially_dead_insns (get_insns (), max_reg_num ());
+  find_basic_blocks (get_insns ());
+}
+
+struct tree_opt_pass pass_loop_optimize =
+{
+  "old-loop",                           /* name */
+  gate_handle_loop_optimize,            /* gate */   
+  rest_of_handle_loop_optimize,         /* execute */       
+  NULL,                                 /* sub */
+  NULL,                                 /* next */
+  0,                                    /* static_pass_number */
+  TV_LOOP,                              /* tv_id */
+  0,                                    /* properties_required */
+  0,                                    /* properties_provided */
+  0,                                    /* properties_destroyed */
+  0,                                    /* todo_flags_start */
+  TODO_dump_func |
+  TODO_ggc_collect,                     /* todo_flags_finish */
+  'L'                                   /* letter */
+};
+
+