OSDN Git Service

H
[pf3gnuchains/gcc-fork.git] / gcc / sched.c
index 48b13ff..e27f70e 100644 (file)
@@ -1,5 +1,5 @@
 /* Instruction scheduling pass.
-   Copyright (C) 1992 Free Software Foundation, Inc.
+   Copyright (C) 1992, 93-97, 1998 Free Software Foundation, Inc.
    Contributed by Michael Tiemann (tiemann@cygnus.com)
    Enhanced by, and currently maintained by, Jim Wilson (wilson@cygnus.com)
 
@@ -17,7 +17,8 @@ GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with GNU CC; see the file COPYING.  If not, write to
-the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
+the Free Software Foundation, 59 Temple Place - Suite 330,
+Boston, MA 02111-1307, USA.  */
 
 /* Instruction scheduling pass.
 
@@ -110,12 +111,14 @@ the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
    reg_n_calls_crossed, and reg_live_length.  Also, basic_block_head,
    basic_block_end.
 
-   The information in the line number notes is carefully retained by this
-   pass.  All other NOTE insns are grouped in their same relative order at
-   the beginning of basic blocks that have been scheduled.  */
+   The information in the line number notes is carefully retained by
+   this pass.  Notes that refer to the starting and ending of
+   exception regions are also carefully retained by this pass.  All
+   other NOTE insns are grouped in their same relative order at the
+   beginning of basic blocks that have been scheduled.  */
 \f
-#include <stdio.h>
 #include "config.h"
+#include "system.h"
 #include "rtl.h"
 #include "basic-block.h"
 #include "regs.h"
@@ -123,8 +126,19 @@ the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
 #include "flags.h"
 #include "insn-config.h"
 #include "insn-attr.h"
+#include "recog.h"
+
+#ifndef INSN_SCHEDULING
+void
+schedule_insns (dump_file)
+     FILE *dump_file ATTRIBUTE_UNUSED;
+{
+}
+#else /* INSN_SCHEDULING -- rest of file */
+
+extern char *reg_known_equiv_p;
+extern rtx *reg_known_value;
 
-#ifdef INSN_SCHEDULING
 /* Arrays set up by scheduling for the same respective purposes as
    similar-named arrays set up by flow analysis.  We work with these
    arrays during the scheduling pass so we can compare values against
@@ -132,7 +146,6 @@ the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
 
    Values of these arrays are copied at the end of this pass into the
    arrays set up by flow analysis.  */
-static short *sched_reg_n_deaths;
 static int *sched_reg_n_calls_crossed;
 static int *sched_reg_live_length;
 
@@ -142,6 +155,8 @@ static int *sched_reg_live_length;
    by splitting insns.  */
 static rtx *reg_last_uses;
 static rtx *reg_last_sets;
+static regset reg_pending_sets;
+static int reg_pending_sets_all;
 
 /* Vector indexed by INSN_UID giving the original ordering of the insns.  */
 static int *insn_luid;
@@ -172,7 +187,7 @@ static unsigned int *insn_blockage;
 #define UNIT_BLOCKED(B) ((B) >> (2 * BLOCKAGE_BITS))
 #define BLOCKAGE_RANGE(B) \
   (((((B) >> BLOCKAGE_BITS) & BLOCKAGE_MASK) << (HOST_BITS_PER_INT / 2)) \
-   | (B) & BLOCKAGE_MASK)
+   | ((B) & BLOCKAGE_MASK))
 
 /* Encodings of the `<name>_unit_blockage_range' function.  */
 #define MIN_BLOCKAGE_COST(R) ((R) >> (HOST_BITS_PER_INT / 2))
@@ -257,7 +272,7 @@ static rtx dead_notes;
    The transition (R->S) is implemented in the scheduling loop in
    `schedule_block' when the best insn to schedule is chosen.
    The transition (R->Q) is implemented in `schedule_select' when an
-   insn is found to to have a function unit conflict with the already
+   insn is found to have a function unit conflict with the already
    committed insns.
    The transitions (P->R and P->Q) are implemented in `schedule_insn' as
    insns move from the ready list to the scheduled list.
@@ -280,542 +295,71 @@ static int q_size = 0;
 static int *insn_tick;
 #define INSN_TICK(INSN) (insn_tick[INSN_UID (INSN)])
 
+/* Data structure for keeping track of register information
+   during that register's life.  */
+
+struct sometimes
+{
+  int regno;
+  int live_length;
+  int calls_crossed;
+};
+
 /* Forward declarations.  */
-static void sched_analyze_2 ();
-static void schedule_block ();
+static void add_dependence             PROTO((rtx, rtx, enum reg_note));
+static void remove_dependence          PROTO((rtx, rtx));
+static rtx find_insn_list              PROTO((rtx, rtx));
+static int insn_unit                   PROTO((rtx));
+static unsigned int blockage_range     PROTO((int, rtx));
+static void clear_units                        PROTO((void));
+static void prepare_unit               PROTO((int));
+static int actual_hazard_this_instance PROTO((int, int, rtx, int, int));
+static void schedule_unit              PROTO((int, rtx, int));
+static int actual_hazard               PROTO((int, rtx, int, int));
+static int potential_hazard            PROTO((int, rtx, int));
+static int insn_cost                   PROTO((rtx, rtx, rtx));
+static int priority                    PROTO((rtx));
+static void free_pending_lists         PROTO((void));
+static void add_insn_mem_dependence    PROTO((rtx *, rtx *, rtx, rtx));
+static void flush_pending_lists                PROTO((rtx, int));
+static void sched_analyze_1            PROTO((rtx, rtx));
+static void sched_analyze_2            PROTO((rtx, rtx));
+static void sched_analyze_insn         PROTO((rtx, rtx, rtx));
+static int sched_analyze               PROTO((rtx, rtx));
+static void sched_note_set             PROTO((rtx, int));
+static int rank_for_schedule           PROTO((const GENERIC_PTR, const GENERIC_PTR));
+static void swap_sort                  PROTO((rtx *, int));
+static void queue_insn                 PROTO((rtx, int));
+static int birthing_insn_p             PROTO((rtx));
+static void adjust_priority            PROTO((rtx));
+static int schedule_insn               PROTO((rtx, rtx *, int, int));
+static int schedule_select             PROTO((rtx *, int, int, FILE *));
+static void create_reg_dead_note       PROTO((rtx, rtx));
+static void attach_deaths              PROTO((rtx, rtx, int));
+static void attach_deaths_insn         PROTO((rtx));
+static rtx unlink_notes                        PROTO((rtx, rtx));
+static int new_sometimes_live          PROTO((struct sometimes *, int, int));
+static void finish_sometimes_live      PROTO((struct sometimes *, int));
+static rtx reemit_notes                        PROTO((rtx, rtx));
+static void schedule_block             PROTO((int, FILE *));
+static rtx regno_use_in                        PROTO((int, rtx));
+static void split_hard_reg_notes       PROTO((rtx, rtx, rtx));
+static void new_insn_dead_notes                PROTO((rtx, rtx, rtx, rtx));
+static void update_n_sets              PROTO((rtx, int));
+static void update_flow_info           PROTO((rtx, rtx, rtx, rtx));
 
 /* Main entry point of this file.  */
-void schedule_insns ();
-#endif /* INSN_SCHEDULING */
+void schedule_insns    PROTO((FILE *));
 \f
 #define SIZE_FOR_MODE(X) (GET_MODE_SIZE (GET_MODE (X)))
 
-/* Vector indexed by N giving the initial (unchanging) value known
-   for pseudo-register N.  */
-static rtx *reg_known_value;
-
-/* Vector recording for each reg_known_value whether it is due to a
-   REG_EQUIV note.  Future passes (viz., reload) may replace the
-   pseudo with the equivalent expression and so we account for the
-   dependences that would be introduced if that happens. */
-/* ??? This is a problem only on the Convex.  The REG_EQUIV notes created in
-   assign_parms mention the arg pointer, and there are explicit insns in the
-   RTL that modify the arg pointer.  Thus we must ensure that such insns don't
-   get scheduled across each other because that would invalidate the REG_EQUIV
-   notes.  One could argue that the REG_EQUIV notes are wrong, but solving
-   the problem in the scheduler will likely give better code, so we do it
-   here.  */
-static char *reg_known_equiv_p;
-
-/* Indicates number of valid entries in reg_known_value.  */
-static int reg_known_value_size;
-
-static rtx
-canon_rtx (x)
-     rtx x;
-{
-  if (GET_CODE (x) == REG && REGNO (x) >= FIRST_PSEUDO_REGISTER
-      && REGNO (x) <= reg_known_value_size)
-    return reg_known_value[REGNO (x)];
-  else if (GET_CODE (x) == PLUS)
-    {
-      rtx x0 = canon_rtx (XEXP (x, 0));
-      rtx x1 = canon_rtx (XEXP (x, 1));
-
-      if (x0 != XEXP (x, 0) || x1 != XEXP (x, 1))
-       {
-         /* We can tolerate LO_SUMs being offset here; these
-            rtl are used for nothing other than comparisons.  */
-         if (GET_CODE (x0) == CONST_INT)
-           return plus_constant_for_output (x1, INTVAL (x0));
-         else if (GET_CODE (x1) == CONST_INT)
-           return plus_constant_for_output (x0, INTVAL (x1));
-         return gen_rtx (PLUS, GET_MODE (x), x0, x1);
-       }
-    }
-  return x;
-}
-
-/* Set up all info needed to perform alias analysis on memory references.  */
-
-void
-init_alias_analysis ()
-{
-  int maxreg = max_reg_num ();
-  rtx insn;
-  rtx note;
-  rtx set;
-
-  reg_known_value_size = maxreg;
-
-  reg_known_value
-    = (rtx *) oballoc ((maxreg-FIRST_PSEUDO_REGISTER) * sizeof (rtx))
-      - FIRST_PSEUDO_REGISTER;
-  bzero (reg_known_value+FIRST_PSEUDO_REGISTER,
-        (maxreg-FIRST_PSEUDO_REGISTER) * sizeof (rtx));
-
-  reg_known_equiv_p
-    = (char *) oballoc ((maxreg-FIRST_PSEUDO_REGISTER) * sizeof (char))
-      - FIRST_PSEUDO_REGISTER;
-  bzero (reg_known_equiv_p+FIRST_PSEUDO_REGISTER,
-        (maxreg-FIRST_PSEUDO_REGISTER) * sizeof (char));
-
-  /* Fill in the entries with known constant values.  */
-  for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
-    if ((set = single_set (insn)) != 0
-       && GET_CODE (SET_DEST (set)) == REG
-       && REGNO (SET_DEST (set)) >= FIRST_PSEUDO_REGISTER
-       && (((note = find_reg_note (insn, REG_EQUAL, 0)) != 0
-            && reg_n_sets[REGNO (SET_DEST (set))] == 1)
-           || (note = find_reg_note (insn, REG_EQUIV, NULL_RTX)) != 0)
-       && GET_CODE (XEXP (note, 0)) != EXPR_LIST)
-      {
-       int regno = REGNO (SET_DEST (set));
-       reg_known_value[regno] = XEXP (note, 0);
-       reg_known_equiv_p[regno] = REG_NOTE_KIND (note) == REG_EQUIV;
-      }
-
-  /* Fill in the remaining entries.  */
-  while (--maxreg >= FIRST_PSEUDO_REGISTER)
-    if (reg_known_value[maxreg] == 0)
-      reg_known_value[maxreg] = regno_reg_rtx[maxreg];
-}
-
-/* Return 1 if X and Y are identical-looking rtx's.
-
-   We use the data in reg_known_value above to see if two registers with
-   different numbers are, in fact, equivalent.  */
-
-static int
-rtx_equal_for_memref_p (x, y)
-     rtx x, y;
-{
-  register int i;
-  register int j;
-  register enum rtx_code code;
-  register char *fmt;
-
-  if (x == 0 && y == 0)
-    return 1;
-  if (x == 0 || y == 0)
-    return 0;
-  x = canon_rtx (x);
-  y = canon_rtx (y);
-
-  if (x == y)
-    return 1;
-
-  code = GET_CODE (x);
-  /* Rtx's of different codes cannot be equal.  */
-  if (code != GET_CODE (y))
-    return 0;
-
-  /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent.
-     (REG:SI x) and (REG:HI x) are NOT equivalent.  */
-
-  if (GET_MODE (x) != GET_MODE (y))
-    return 0;
-
-  /* REG, LABEL_REF, and SYMBOL_REF can be compared nonrecursively.  */
-
-  if (code == REG)
-    return REGNO (x) == REGNO (y);
-  if (code == LABEL_REF)
-    return XEXP (x, 0) == XEXP (y, 0);
-  if (code == SYMBOL_REF)
-    return XSTR (x, 0) == XSTR (y, 0);
-
-  /* Compare the elements.  If any pair of corresponding elements
-     fail to match, return 0 for the whole things.  */
-
-  fmt = GET_RTX_FORMAT (code);
-  for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
-    {
-      switch (fmt[i])
-       {
-       case 'w':
-         if (XWINT (x, i) != XWINT (y, i))
-           return 0;
-         break;
-
-       case 'n':
-       case 'i':
-         if (XINT (x, i) != XINT (y, i))
-           return 0;
-         break;
-
-       case 'V':
-       case 'E':
-         /* Two vectors must have the same length.  */
-         if (XVECLEN (x, i) != XVECLEN (y, i))
-           return 0;
-
-         /* And the corresponding elements must match.  */
-         for (j = 0; j < XVECLEN (x, i); j++)
-           if (rtx_equal_for_memref_p (XVECEXP (x, i, j), XVECEXP (y, i, j)) == 0)
-             return 0;
-         break;
-
-       case 'e':
-         if (rtx_equal_for_memref_p (XEXP (x, i), XEXP (y, i)) == 0)
-           return 0;
-         break;
-
-       case 'S':
-       case 's':
-         if (strcmp (XSTR (x, i), XSTR (y, i)))
-           return 0;
-         break;
-
-       case 'u':
-         /* These are just backpointers, so they don't matter.  */
-         break;
-
-       case '0':
-         break;
-
-         /* It is believed that rtx's at this level will never
-            contain anything but integers and other rtx's,
-            except for within LABEL_REFs and SYMBOL_REFs.  */
-       default:
-         abort ();
-       }
-    }
-  return 1;
-}
-
-/* Given an rtx X, find a SYMBOL_REF or LABEL_REF within
-   X and return it, or return 0 if none found.  */
-
-static rtx
-find_symbolic_term (x)
-     rtx x;
-{
-  register int i;
-  register enum rtx_code code;
-  register char *fmt;
-
-  code = GET_CODE (x);
-  if (code == SYMBOL_REF || code == LABEL_REF)
-    return x;
-  if (GET_RTX_CLASS (code) == 'o')
-    return 0;
-
-  fmt = GET_RTX_FORMAT (code);
-  for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
-    {
-      rtx t;
-
-      if (fmt[i] == 'e')
-       {
-         t = find_symbolic_term (XEXP (x, i));
-         if (t != 0)
-           return t;
-       }
-      else if (fmt[i] == 'E')
-       break;
-    }
-  return 0;
-}
-
-/* Return nonzero if X and Y (memory addresses) could reference the
-   same location in memory.  C is an offset accumulator.  When
-   C is nonzero, we are testing aliases between X and Y + C.
-   XSIZE is the size in bytes of the X reference,
-   similarly YSIZE is the size in bytes for Y.
-
-   If XSIZE or YSIZE is zero, we do not know the amount of memory being
-   referenced (the reference was BLKmode), so make the most pessimistic
-   assumptions.
-
-   We recognize the following cases of non-conflicting memory:
-
-       (1) addresses involving the frame pointer cannot conflict
-           with addresses involving static variables.
-       (2) static variables with different addresses cannot conflict.
-
-   Nice to notice that varying addresses cannot conflict with fp if no
-   local variables had their addresses taken, but that's too hard now.  */
-
-/* ??? In Fortran, references to a array parameter can never conflict with
-   another array parameter.  */
-
-static int
-memrefs_conflict_p (xsize, x, ysize, y, c)
-     rtx x, y;
-     int xsize, ysize;
-     HOST_WIDE_INT c;
-{
-  if (GET_CODE (x) == HIGH)
-    x = XEXP (x, 0);
-  else if (GET_CODE (x) == LO_SUM)
-    x = XEXP (x, 1);
-  else
-    x = canon_rtx (x);
-  if (GET_CODE (y) == HIGH)
-    y = XEXP (y, 0);
-  else if (GET_CODE (y) == LO_SUM)
-    y = XEXP (y, 1);
-  else
-    y = canon_rtx (y);
-
-  if (rtx_equal_for_memref_p (x, y))
-    return (xsize == 0 || ysize == 0 ||
-           (c >= 0 && xsize > c) || (c < 0 && ysize+c > 0));
-
-  if (y == frame_pointer_rtx || y == stack_pointer_rtx)
-    {
-      rtx t = y;
-      int tsize = ysize;
-      y = x; ysize = xsize;
-      x = t; xsize = tsize;
-    }
-
-  if (x == frame_pointer_rtx || x == stack_pointer_rtx)
-    {
-      rtx y1;
-
-      if (CONSTANT_P (y))
-       return 0;
-
-      if (GET_CODE (y) == PLUS
-         && canon_rtx (XEXP (y, 0)) == x
-         && (y1 = canon_rtx (XEXP (y, 1)))
-         && GET_CODE (y1) == CONST_INT)
-       {
-         c += INTVAL (y1);
-         return (xsize == 0 || ysize == 0
-                 || (c >= 0 && xsize > c) || (c < 0 && ysize+c > 0));
-       }
-
-      if (GET_CODE (y) == PLUS
-         && (y1 = canon_rtx (XEXP (y, 0)))
-         && CONSTANT_P (y1))
-       return 0;
-
-      return 1;
-    }
-
-  if (GET_CODE (x) == PLUS)
-    {
-      /* The fact that X is canonicalized means that this
-        PLUS rtx is canonicalized.  */
-      rtx x0 = XEXP (x, 0);
-      rtx x1 = XEXP (x, 1);
-
-      if (GET_CODE (y) == PLUS)
-       {
-         /* The fact that Y is canonicalized means that this
-            PLUS rtx is canonicalized.  */
-         rtx y0 = XEXP (y, 0);
-         rtx y1 = XEXP (y, 1);
-
-         if (rtx_equal_for_memref_p (x1, y1))
-           return memrefs_conflict_p (xsize, x0, ysize, y0, c);
-         if (rtx_equal_for_memref_p (x0, y0))
-           return memrefs_conflict_p (xsize, x1, ysize, y1, c);
-         if (GET_CODE (x1) == CONST_INT)
-           if (GET_CODE (y1) == CONST_INT)
-             return memrefs_conflict_p (xsize, x0, ysize, y0,
-                                        c - INTVAL (x1) + INTVAL (y1));
-           else
-             return memrefs_conflict_p (xsize, x0, ysize, y, c - INTVAL (x1));
-         else if (GET_CODE (y1) == CONST_INT)
-           return memrefs_conflict_p (xsize, x, ysize, y0, c + INTVAL (y1));
-
-         /* Handle case where we cannot understand iteration operators,
-            but we notice that the base addresses are distinct objects.  */
-         x = find_symbolic_term (x);
-         if (x == 0)
-           return 1;
-         y = find_symbolic_term (y);
-         if (y == 0)
-           return 1;
-         return rtx_equal_for_memref_p (x, y);
-       }
-      else if (GET_CODE (x1) == CONST_INT)
-       return memrefs_conflict_p (xsize, x0, ysize, y, c - INTVAL (x1));
-    }
-  else if (GET_CODE (y) == PLUS)
-    {
-      /* The fact that Y is canonicalized means that this
-        PLUS rtx is canonicalized.  */
-      rtx y0 = XEXP (y, 0);
-      rtx y1 = XEXP (y, 1);
-
-      if (GET_CODE (y1) == CONST_INT)
-       return memrefs_conflict_p (xsize, x, ysize, y0, c + INTVAL (y1));
-      else
-       return 1;
-    }
-
-  if (GET_CODE (x) == GET_CODE (y))
-    switch (GET_CODE (x))
-      {
-      case MULT:
-       {
-         /* Handle cases where we expect the second operands to be the
-            same, and check only whether the first operand would conflict
-            or not.  */
-         rtx x0, y0;
-         rtx x1 = canon_rtx (XEXP (x, 1));
-         rtx y1 = canon_rtx (XEXP (y, 1));
-         if (! rtx_equal_for_memref_p (x1, y1))
-           return 1;
-         x0 = canon_rtx (XEXP (x, 0));
-         y0 = canon_rtx (XEXP (y, 0));
-         if (rtx_equal_for_memref_p (x0, y0))
-           return (xsize == 0 || ysize == 0
-                   || (c >= 0 && xsize > c) || (c < 0 && ysize+c > 0));
-
-         /* Can't properly adjust our sizes.  */
-         if (GET_CODE (x1) != CONST_INT)
-           return 1;
-         xsize /= INTVAL (x1);
-         ysize /= INTVAL (x1);
-         c /= INTVAL (x1);
-         return memrefs_conflict_p (xsize, x0, ysize, y0, c);
-       }
-      }
-
-  if (CONSTANT_P (x))
-    {
-      if (GET_CODE (x) == CONST_INT && GET_CODE (y) == CONST_INT)
-       {
-         c += (INTVAL (y) - INTVAL (x));
-         return (xsize == 0 || ysize == 0
-                 || (c >= 0 && xsize > c) || (c < 0 && ysize+c > 0));
-       }
-
-      if (GET_CODE (x) == CONST)
-       {
-         if (GET_CODE (y) == CONST)
-           return memrefs_conflict_p (xsize, canon_rtx (XEXP (x, 0)),
-                                      ysize, canon_rtx (XEXP (y, 0)), c);
-         else
-           return memrefs_conflict_p (xsize, canon_rtx (XEXP (x, 0)),
-                                      ysize, y, c);
-       }
-      if (GET_CODE (y) == CONST)
-       return memrefs_conflict_p (xsize, x, ysize,
-                                  canon_rtx (XEXP (y, 0)), c);
-
-      if (CONSTANT_P (y))
-       return (rtx_equal_for_memref_p (x, y)
-               && (xsize == 0 || ysize == 0
-                   || (c >= 0 && xsize > c) || (c < 0 && ysize+c > 0)));
-
-      return 1;
-    }
-  return 1;
-}
-
-/* Functions to compute memory dependencies.
-
-   Since we process the insns in execution order, we can build tables
-   to keep track of what registers are fixed (and not aliased), what registers
-   are varying in known ways, and what registers are varying in unknown
-   ways.
-
-   If both memory references are volatile, then there must always be a
-   dependence between the two references, since their order can not be
-   changed.  A volatile and non-volatile reference can be interchanged
-   though. 
-
-   A MEM_IN_STRUCT reference at a non-QImode varying address can never
-   conflict with a non-MEM_IN_STRUCT reference at a fixed address.   We must
-   allow QImode aliasing because the ANSI C standard allows character
-   pointers to alias anything.  We are assuming that characters are
-   always QImode here.  */
-
-/* Read dependence: X is read after read in MEM takes place.  There can
-   only be a dependence here if both reads are volatile.  */
-
-int
-read_dependence (mem, x)
-     rtx mem;
-     rtx x;
-{
-  return MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem);
-}
-
-/* True dependence: X is read after store in MEM takes place.  */
-
-int
-true_dependence (mem, x)
-     rtx mem;
-     rtx x;
-{
-  /* If X is an unchanging read, then it can't possibly conflict with any
-     non-unchanging store.  It may conflict with an unchanging write though,
-     because there may be a single store to this address to initialize it.
-     Just fall through to the code below to resolve the case where we have
-     both an unchanging read and an unchanging write.  This won't handle all
-     cases optimally, but the possible performance loss should be
-     negligible.  */
-  if (RTX_UNCHANGING_P (x) && ! RTX_UNCHANGING_P (mem))
-    return 0;
-
-  return ((MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem))
-         || (memrefs_conflict_p (SIZE_FOR_MODE (mem), XEXP (mem, 0),
-                                 SIZE_FOR_MODE (x), XEXP (x, 0), 0)
-             && ! (MEM_IN_STRUCT_P (mem) && rtx_addr_varies_p (mem)
-                   && GET_MODE (mem) != QImode
-                   && ! MEM_IN_STRUCT_P (x) && ! rtx_addr_varies_p (x))
-             && ! (MEM_IN_STRUCT_P (x) && rtx_addr_varies_p (x)
-                   && GET_MODE (x) != QImode
-                   && ! MEM_IN_STRUCT_P (mem) && ! rtx_addr_varies_p (mem))));
-}
-
-/* Anti dependence: X is written after read in MEM takes place.  */
-
-int
-anti_dependence (mem, x)
-     rtx mem;
-     rtx x;
-{
-  /* If MEM is an unchanging read, then it can't possibly conflict with
-     the store to X, because there is at most one store to MEM, and it must
-     have occured somewhere before MEM.  */
-  if (RTX_UNCHANGING_P (mem))
-    return 0;
-
-  return ((MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem))
-         || (memrefs_conflict_p (SIZE_FOR_MODE (mem), XEXP (mem, 0),
-                                 SIZE_FOR_MODE (x), XEXP (x, 0), 0)
-             && ! (MEM_IN_STRUCT_P (mem) && rtx_addr_varies_p (mem)
-                   && GET_MODE (mem) != QImode
-                   && ! MEM_IN_STRUCT_P (x) && ! rtx_addr_varies_p (x))
-             && ! (MEM_IN_STRUCT_P (x) && rtx_addr_varies_p (x)
-                   && GET_MODE (x) != QImode
-                   && ! MEM_IN_STRUCT_P (mem) && ! rtx_addr_varies_p (mem))));
-}
-
-/* Output dependence: X is written after store in MEM takes place.  */
-
-int
-output_dependence (mem, x)
-     rtx mem;
-     rtx x;
-{
-  return ((MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem))
-         || (memrefs_conflict_p (SIZE_FOR_MODE (mem), XEXP (mem, 0),
-                                 SIZE_FOR_MODE (x), XEXP (x, 0), 0)
-             && ! (MEM_IN_STRUCT_P (mem) && rtx_addr_varies_p (mem)
-                   && GET_MODE (mem) != QImode
-                   && ! MEM_IN_STRUCT_P (x) && ! rtx_addr_varies_p (x))
-             && ! (MEM_IN_STRUCT_P (x) && rtx_addr_varies_p (x)
-                   && GET_MODE (x) != QImode
-                   && ! MEM_IN_STRUCT_P (mem) && ! rtx_addr_varies_p (mem))));
-}
-\f
 /* Helper functions for instruction scheduling.  */
 
 /* Add ELEM wrapped in an INSN_LIST with reg note kind DEP_TYPE to the
    LOG_LINKS of INSN, if not already there.  DEP_TYPE indicates the type
    of dependence that this link represents.  */
 
-void
+static void
 add_dependence (insn, elem, dep_type)
      rtx insn;
      rtx elem;
@@ -840,7 +384,8 @@ add_dependence (insn, elem, dep_type)
     next = NEXT_INSN (next);
 #endif
 
-  if (next && SCHED_GROUP_P (next))
+  if (next && SCHED_GROUP_P (next)
+      && GET_CODE (next) != CODE_LABEL)
     {
       /* Notes will never intervene here though, so don't bother checking
         for them.  */
@@ -883,7 +428,8 @@ add_dependence (insn, elem, dep_type)
 
 /* Remove ELEM wrapped in an INSN_LIST from the LOG_LINKS
    of INSN.  Abort if not found.  */
-void
+
+static void
 remove_dependence (insn, elem)
      rtx insn;
      rtx elem;
@@ -891,17 +437,19 @@ remove_dependence (insn, elem)
   rtx prev, link;
   int found = 0;
 
-  for (prev = 0, link = LOG_LINKS (insn); link;
-       prev = link, link = XEXP (link, 1))
+  for (prev = 0, link = LOG_LINKS (insn); link; link = XEXP (link, 1))
     {
       if (XEXP (link, 0) == elem)
        {
+         RTX_INTEGRATED_P (link) = 1;
          if (prev)
            XEXP (prev, 1) = XEXP (link, 1);
          else
            LOG_LINKS (insn) = XEXP (link, 1);
          found = 1;
        }
+      else
+       prev = link;
     }
 
   if (! found)
@@ -909,9 +457,6 @@ remove_dependence (insn, elem)
   return;
 }
 \f
-#ifndef INSN_SCHEDULING
-void schedule_insns () {}
-#else
 #ifndef __GNUC__
 #define __inline
 #endif
@@ -1062,7 +607,7 @@ blockage_range (unit, insn)
   unsigned int blockage = INSN_BLOCKAGE (insn);
   unsigned int range;
 
-  if (UNIT_BLOCKED (blockage) != unit + 1)
+  if ((int) UNIT_BLOCKED (blockage) != unit + 1)
     {
       range = function_units[unit].blockage_range_function (insn);
       /* We only cache the blockage range for one unit and then only if
@@ -1094,11 +639,9 @@ static int unit_n_insns[FUNCTION_UNITS_SIZE];
 static void
 clear_units ()
 {
-  int unit;
-
-  bzero (unit_last_insn, sizeof (unit_last_insn));
-  bzero (unit_tick, sizeof (unit_tick));
-  bzero (unit_n_insns, sizeof (unit_n_insns));
+  bzero ((char *) unit_last_insn, sizeof (unit_last_insn));
+  bzero ((char *) unit_tick, sizeof (unit_tick));
+  bzero ((char *) unit_n_insns, sizeof (unit_n_insns));
 }
 
 /* Record an insn as one that will use the units encoded by UNIT.  */
@@ -1126,7 +669,6 @@ actual_hazard_this_instance (unit, instance, insn, clock, cost)
      int unit, instance, clock, cost;
      rtx insn;
 {
-  int i;
   int tick = unit_tick[instance];
 
   if (tick - clock > cost)
@@ -1202,12 +744,11 @@ actual_hazard (unit, insn, clock, cost)
     {
       /* Find the instance of the function unit with the minimum hazard.  */
       int instance = unit;
-      int best = instance;
       int best_cost = actual_hazard_this_instance (unit, instance, insn,
                                                   clock, cost);
+#if MAX_MULTIPLICITY > 1
       int this_cost;
 
-#if MAX_MULTIPLICITY > 1
       if (best_cost > cost)
        {
          for (i = function_units[unit].multiplicity - 1; i > 0; i--)
@@ -1217,7 +758,6 @@ actual_hazard (unit, insn, clock, cost)
                                                       clock, cost);
              if (this_cost < best_cost)
                {
-                 best = instance;
                  best_cost = this_cost;
                  if (this_cost <= cost)
                    break;
@@ -1380,11 +920,17 @@ priority (insn)
        {
          rtx x = XEXP (prev, 0);
 
-         /* A dependence pointing to a note is always obsolete, because
-            sched_analyze_insn will have created any necessary new dependences
-            which replace it.  Notes can be created when instructions are
-            deleted by insn splitting, or by register allocation.  */
-         if (GET_CODE (x) == NOTE)
+         /* If this was a duplicate of a dependence we already deleted,
+            ignore it.  */
+         if (RTX_INTEGRATED_P (prev))
+           continue;
+
+         /* A dependence pointing to a note or deleted insn is always
+            obsolete, because sched_analyze_insn will have created any
+            necessary new dependences which replace it.  Notes and deleted
+            insns can be created when instructions are deleted by insn
+            splitting, or by register allocation.  */
+         if (GET_CODE (x) == NOTE || INSN_DELETED_P (x))
            {
              remove_dependence (insn, x);
              continue;
@@ -1412,13 +958,7 @@ priority (insn)
             insn_cost of the current instruction to its priority (e.g.
             move the insn_cost call down to the end).  */
 
-         if (REG_NOTE_KIND (prev) == 0)
-           /* Data dependence.  */
-           prev_priority = priority (x) + insn_cost (x, prev, insn) - 1;
-         else
-           /* Anti or output dependence.  Don't add the latency of this
-              insn's result, because it isn't being used.  */
-           prev_priority = priority (x);
+         prev_priority = priority (x) + insn_cost (x, prev, insn) - 1;
 
          if (prev_priority > max_priority)
            max_priority = prev_priority;
@@ -1541,15 +1081,17 @@ add_insn_mem_dependence (insn_list, mem_list, insn, mem)
 }
 \f
 /* Make a dependency between every memory reference on the pending lists
-   and INSN, thus flushing the pending lists.  */
+   and INSN, thus flushing the pending lists.  If ONLY_WRITE, don't flush
+   the read list.  */
 
 static void
-flush_pending_lists (insn)
+flush_pending_lists (insn, only_write)
      rtx insn;
+     int only_write;
 {
   rtx link;
 
-  while (pending_read_insns)
+  while (pending_read_insns && ! only_write)
     {
       add_dependence (insn, XEXP (pending_read_insns, 0), REG_DEP_ANTI);
 
@@ -1613,7 +1155,7 @@ sched_analyze_1 (x, insn)
 
   if (GET_CODE (dest) == REG)
     {
-      register int offset, bit, i;
+      register int i;
 
       regno = REGNO (dest);
 
@@ -1632,7 +1174,7 @@ sched_analyze_1 (x, insn)
              if (reg_last_sets[regno + i])
                add_dependence (insn, reg_last_sets[regno + i],
                                REG_DEP_OUTPUT);
-             reg_last_sets[regno + i] = insn;
+             SET_REGNO_REG_SET (reg_pending_sets, regno + i);
              if ((call_used_regs[i] || global_regs[i])
                  && last_function_call)
                /* Function calls clobber all call_used regs.  */
@@ -1648,7 +1190,7 @@ sched_analyze_1 (x, insn)
          reg_last_uses[regno] = 0;
          if (reg_last_sets[regno])
            add_dependence (insn, reg_last_sets[regno], REG_DEP_OUTPUT);
-         reg_last_sets[regno] = insn;
+         SET_REGNO_REG_SET (reg_pending_sets, regno);
 
          /* Pseudos that are REG_EQUIV to something may be replaced
             by that during reloading.  We need only add dependencies for
@@ -1660,7 +1202,7 @@ sched_analyze_1 (x, insn)
 
          /* Don't let it cross a call after scheduling if it doesn't
             already cross one.  */
-         if (reg_n_calls_crossed[regno] == 0 && last_function_call)
+         if (REG_N_CALLS_CROSSED (regno) == 0 && last_function_call)
            add_dependence (insn, last_function_call, REG_DEP_ANTI);
        }
     }
@@ -1676,7 +1218,7 @@ sched_analyze_1 (x, insn)
             seems like a reasonable number.  When compiling GCC with itself,
             this flush occurs 8 times for sparc, and 10 times for m88k using
             the number 32.  */
-         flush_pending_lists (insn);
+         flush_pending_lists (insn, 0);
        }
       else
        {
@@ -1756,22 +1298,19 @@ sched_analyze_2 (x, insn)
       {
        rtx link, prev;
 
+       /* User of CC0 depends on immediately preceding insn.  */
+       SCHED_GROUP_P (insn) = 1;
+
        /* There may be a note before this insn now, but all notes will
           be removed before we actually try to schedule the insns, so
           it won't cause a problem later.  We must avoid it here though.  */
-
-       /* User of CC0 depends on immediately preceding insn.  */
-       SCHED_GROUP_P (insn) = 1;
+       prev = prev_nonnote_insn (insn);
 
        /* Make a copy of all dependencies on the immediately previous insn,
           and add to this insn.  This is so that all the dependencies will
           apply to the group.  Remove an explicit dependence on this insn
           as SCHED_GROUP_P now represents it.  */
 
-       prev = PREV_INSN (insn);
-       while (GET_CODE (prev) == NOTE)
-         prev = PREV_INSN (prev);
-
        if (find_insn_list (prev, LOG_LINKS (insn)))
          remove_dependence (insn, prev);
 
@@ -1793,8 +1332,8 @@ sched_analyze_2 (x, insn)
            while (--i >= 0)
              {
                reg_last_uses[regno + i]
-                 = gen_rtx (INSN_LIST, VOIDmode,
-                            insn, reg_last_uses[regno + i]);
+                 = gen_rtx_INSN_LIST (VOIDmode,
+                                      insn, reg_last_uses[regno + i]);
                if (reg_last_sets[regno + i])
                  add_dependence (insn, reg_last_sets[regno + i], 0);
                if ((call_used_regs[regno + i] || global_regs[regno + i])
@@ -1806,7 +1345,7 @@ sched_analyze_2 (x, insn)
        else
          {
            reg_last_uses[regno]
-             = gen_rtx (INSN_LIST, VOIDmode, insn, reg_last_uses[regno]);
+             = gen_rtx_INSN_LIST (VOIDmode, insn, reg_last_uses[regno]);
            if (reg_last_sets[regno])
              add_dependence (insn, reg_last_sets[regno], 0);
 
@@ -1821,7 +1360,7 @@ sched_analyze_2 (x, insn)
            /* If the register does not already cross any calls, then add this
               insn to the sched_before_next_call list so that it will still
               not cross calls after scheduling.  */
-           if (reg_n_calls_crossed[regno] == 0)
+           if (REG_N_CALLS_CROSSED (regno) == 0)
              add_dependence (sched_before_next_call, insn, REG_DEP_ANTI);
          }
        return;
@@ -1852,7 +1391,8 @@ sched_analyze_2 (x, insn)
          {
            /* If a dependency already exists, don't create a new one.  */
            if (! find_insn_list (XEXP (pending, 0), LOG_LINKS (insn)))
-             if (true_dependence (XEXP (pending_mem, 0), x))
+             if (true_dependence (XEXP (pending_mem, 0), VOIDmode,
+                                  x, rtx_varies_p))
                add_dependence (insn, XEXP (pending, 0), 0);
 
            pending = XEXP (pending, 1);
@@ -1879,21 +1419,26 @@ sched_analyze_2 (x, insn)
        rtx u;
 
        /* Traditional and volatile asm instructions must be considered to use
-          and clobber all hard registers and all of memory.  So must
-          TRAP_IF and UNSPEC_VOLATILE operations.  */
+          and clobber all hard registers, all pseudo-registers and all of
+          memory.  So must TRAP_IF and UNSPEC_VOLATILE operations.
+
+          Consider for instance a volatile asm that changes the fpu rounding
+          mode.  An insn should not be moved across this even if it only uses
+          pseudo-regs because it might give an incorrectly rounded result.  */
        if (code != ASM_OPERANDS || MEM_VOLATILE_P (x))
          {
-           for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
+           int max_reg = max_reg_num ();
+           for (i = 0; i < max_reg; i++)
              {
                for (u = reg_last_uses[i]; u; u = XEXP (u, 1))
                  add_dependence (insn, XEXP (u, 0), REG_DEP_ANTI);
                reg_last_uses[i] = 0;
                if (reg_last_sets[i])
                  add_dependence (insn, reg_last_sets[i], 0);
-               reg_last_sets[i] = insn;
              }
+           reg_pending_sets_all = 1;
 
-           flush_pending_lists (insn);
+           flush_pending_lists (insn, 0);
          }
 
        /* For all ASM_OPERANDS, we must traverse the vector of input operands.
@@ -1923,6 +1468,9 @@ sched_analyze_2 (x, insn)
       sched_analyze_2 (XEXP (x, 0), insn);
       sched_analyze_1 (x, insn);
       return;
+      
+    default:
+      break;
     }
 
   /* Other cases: walk the insn.  */
@@ -1940,11 +1488,14 @@ sched_analyze_2 (x, insn)
 /* Analyze an INSN with pattern X to find all dependencies.  */
 
 static void
-sched_analyze_insn (x, insn)
+sched_analyze_insn (x, insn, loop_notes)
      rtx x, insn;
+     rtx loop_notes;
 {
   register RTX_CODE code = GET_CODE (x);
   rtx link;
+  int maxreg = max_reg_num ();
+  int i;
 
   if (code == SET || code == CLOBBER)
     sched_analyze_1 (x, insn);
@@ -1963,19 +1514,80 @@ sched_analyze_insn (x, insn)
   else
     sched_analyze_2 (x, insn);
 
-  /* Handle function calls.  */
+  /* Mark registers CLOBBERED or used by called function.  */
   if (GET_CODE (insn) == CALL_INSN)
+    for (link = CALL_INSN_FUNCTION_USAGE (insn); link; link = XEXP (link, 1))
+      {
+       if (GET_CODE (XEXP (link, 0)) == CLOBBER)
+         sched_analyze_1 (XEXP (link, 0), insn);
+       else
+         sched_analyze_2 (XEXP (link, 0), insn);
+      }
+
+  /* If there is a {LOOP,EHREGION}_{BEG,END} note in the middle of a basic block, then
+     we must be sure that no instructions are scheduled across it.
+     Otherwise, the reg_n_refs info (which depends on loop_depth) would
+     become incorrect.  */
+
+  if (loop_notes)
+    {
+      int max_reg = max_reg_num ();
+      rtx link;
+
+      for (i = 0; i < max_reg; i++)
+       {
+         rtx u;
+         for (u = reg_last_uses[i]; u; u = XEXP (u, 1))
+           add_dependence (insn, XEXP (u, 0), REG_DEP_ANTI);
+         reg_last_uses[i] = 0;
+         if (reg_last_sets[i])
+           add_dependence (insn, reg_last_sets[i], 0);
+       }
+      reg_pending_sets_all = 1;
+
+      flush_pending_lists (insn, 0);
+
+      link = loop_notes;
+      while (XEXP (link, 1))
+       link = XEXP (link, 1);
+      XEXP (link, 1) = REG_NOTES (insn);
+      REG_NOTES (insn) = loop_notes;
+    }
+
+  EXECUTE_IF_SET_IN_REG_SET (reg_pending_sets, 0, i,
+                            {
+                              reg_last_sets[i] = insn;
+                            });
+  CLEAR_REG_SET (reg_pending_sets);
+
+  if (reg_pending_sets_all)
+    {
+      for (i = 0; i < maxreg; i++)
+       reg_last_sets[i] = insn;
+      reg_pending_sets_all = 0;
+    }
+
+  /* Handle function calls and function returns created by the epilogue
+     threading code.  */
+  if (GET_CODE (insn) == CALL_INSN || GET_CODE (insn) == JUMP_INSN)
     {
       rtx dep_insn;
       rtx prev_dep_insn;
 
       /* When scheduling instructions, we make sure calls don't lose their
-        accompanying USE insns by depending them one on another in order.   */
+        accompanying USE insns by depending them one on another in order.
+
+        Also, we must do the same thing for returns created by the epilogue
+        threading code.  Note this code works only in this special case,
+        because other passes make no guarantee that they will never emit
+        an instruction between a USE and a RETURN.  There is such a guarantee
+        for USE instructions immediately before a call.  */
 
       prev_dep_insn = insn;
       dep_insn = PREV_INSN (insn);
       while (GET_CODE (dep_insn) == INSN
-            && GET_CODE (PATTERN (dep_insn)) == USE)
+            && GET_CODE (PATTERN (dep_insn)) == USE
+            && GET_CODE (XEXP (PATTERN (dep_insn), 0)) == REG)
        {
          SCHED_GROUP_P (prev_dep_insn) = 1;
 
@@ -2003,6 +1615,7 @@ sched_analyze (head, tail)
   register int n_insns = 0;
   register rtx u;
   register int luid = 0;
+  rtx loop_notes = 0;
 
   for (insn = head; ; insn = NEXT_INSN (insn))
     {
@@ -2010,12 +1623,12 @@ sched_analyze (head, tail)
 
       if (GET_CODE (insn) == INSN || GET_CODE (insn) == JUMP_INSN)
        {
-         sched_analyze_insn (PATTERN (insn), insn);
+         sched_analyze_insn (PATTERN (insn), insn, loop_notes);
+         loop_notes = 0;
          n_insns += 1;
        }
       else if (GET_CODE (insn) == CALL_INSN)
        {
-         rtx dest = 0;
          rtx x;
          register int i;
 
@@ -2025,18 +1638,51 @@ sched_analyze (head, tail)
             past a void call (i.e. it does not explicitly set the hard
             return reg).  */
 
-         for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
-           if (call_used_regs[i] || global_regs[i])
-             {
-               for (u = reg_last_uses[i]; u; u = XEXP (u, 1))
-                 add_dependence (insn, XEXP (u, 0), REG_DEP_ANTI);
-               reg_last_uses[i] = 0;
-               if (reg_last_sets[i])
-                 add_dependence (insn, reg_last_sets[i], REG_DEP_ANTI);
-               reg_last_sets[i] = insn;
-               /* Insn, being a CALL_INSN, magically depends on
-                  `last_function_call' already.  */
-             }
+         /* If this call is followed by a NOTE_INSN_SETJMP, then assume that
+            all registers, not just hard registers, may be clobbered by this
+            call.  */
+
+         /* Insn, being a CALL_INSN, magically depends on
+            `last_function_call' already.  */
+
+         if (NEXT_INSN (insn) && GET_CODE (NEXT_INSN (insn)) == NOTE
+             && NOTE_LINE_NUMBER (NEXT_INSN (insn)) == NOTE_INSN_SETJMP)
+           {
+             int max_reg = max_reg_num ();
+             for (i = 0; i < max_reg; i++)
+               {
+                 for (u = reg_last_uses[i]; u; u = XEXP (u, 1))
+                   add_dependence (insn, XEXP (u, 0), REG_DEP_ANTI);
+                 reg_last_uses[i] = 0;
+                 if (reg_last_sets[i])
+                   add_dependence (insn, reg_last_sets[i], 0);
+               }
+             reg_pending_sets_all = 1;
+
+             /* Add a pair of fake REG_NOTEs which we will later
+                convert back into a NOTE_INSN_SETJMP note.  See
+                reemit_notes for why we use a pair of NOTEs.  */
+
+             REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_DEAD,
+                                                   GEN_INT (0),
+                                                   REG_NOTES (insn));
+             REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_DEAD,
+                                                   GEN_INT (NOTE_INSN_SETJMP),
+                                                   REG_NOTES (insn));
+           }
+         else
+           {
+             for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
+               if (call_used_regs[i] || global_regs[i])
+                 {
+                   for (u = reg_last_uses[i]; u; u = XEXP (u, 1))
+                     add_dependence (insn, XEXP (u, 0), REG_DEP_ANTI);
+                   reg_last_uses[i] = 0;
+                   if (reg_last_sets[i])
+                     add_dependence (insn, reg_last_sets[i], REG_DEP_ANTI);
+                   SET_REGNO_REG_SET (reg_pending_sets, i);
+                 }
+           }
 
          /* For each insn which shouldn't cross a call, add a dependence
             between that insn and this call insn.  */
@@ -2048,17 +1694,14 @@ sched_analyze (head, tail)
            }
          LOG_LINKS (sched_before_next_call) = 0;
 
-         sched_analyze_insn (PATTERN (insn), insn);
+         sched_analyze_insn (PATTERN (insn), insn, loop_notes);
+         loop_notes = 0;
 
-         /* We don't need to flush memory for a function call which does
-            not involve memory.  */
-         if (! CONST_CALL_P (insn))
-           {
-             /* In the absence of interprocedural alias analysis,
-                we must flush all pending reads and writes, and
-                start new dependencies starting from here.  */
-             flush_pending_lists (insn);
-           }
+         /* In the absence of interprocedural alias analysis, we must flush
+            all pending reads and writes, and start new dependencies starting
+            from here.  But only flush writes for constant calls (which may
+            be passed a pointer to something we haven't written yet).  */
+         flush_pending_lists (insn, CONST_CALL_P (insn));
 
          /* Depend this function call (actually, the user of this
             function call) on all hard register clobberage.  */
@@ -2066,9 +1709,31 @@ sched_analyze (head, tail)
          n_insns += 1;
        }
 
+      /* See comments on reemit_notes as to why we do this.  */
+      else if (GET_CODE (insn) == NOTE
+              && (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG
+                  || NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END
+                  || NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_BEG
+                  || NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_END
+                  || NOTE_LINE_NUMBER (insn) == NOTE_INSN_RANGE_START
+                  || NOTE_LINE_NUMBER (insn) == NOTE_INSN_RANGE_END
+                  || (NOTE_LINE_NUMBER (insn) == NOTE_INSN_SETJMP
+                      && GET_CODE (PREV_INSN (insn)) != CALL_INSN)))
+       {
+         loop_notes = gen_rtx_EXPR_LIST (REG_DEAD,
+                                         GEN_INT (NOTE_BLOCK_NUMBER (insn)),
+                                         loop_notes);
+         loop_notes = gen_rtx_EXPR_LIST (REG_DEAD,
+                                         GEN_INT (NOTE_LINE_NUMBER (insn)),
+                                         loop_notes);
+         CONST_CALL_P (loop_notes) = CONST_CALL_P (insn);
+       }
+
       if (insn == tail)
        return n_insns;
     }
+
+  abort ();
 }
 \f
 /* Called when we see a set of a register.  If death is true, then we are
@@ -2077,12 +1742,11 @@ sched_analyze (head, tail)
    are scanning forwards.  Mark that register as being born.  */
 
 static void
-sched_note_set (b, x, death)
-     int b;
+sched_note_set (x, death)
      rtx x;
      int death;
 {
-  register int regno, j;
+  register int regno;
   register rtx reg = SET_DEST (x);
   int subreg_p = 0;
 
@@ -2112,10 +1776,6 @@ sched_note_set (b, x, death)
   regno = REGNO (reg);
   if (regno >= FIRST_PSEUDO_REGISTER || ! global_regs[regno])
     {
-      register int offset = regno / REGSET_ELT_BITS;
-      register REGSET_ELT_TYPE bit
-       = (REGSET_ELT_TYPE) 1 << (regno % REGSET_ELT_BITS);
-
       if (death)
        {
          /* If we only set part of the register, then this set does not
@@ -2129,17 +1789,14 @@ sched_note_set (b, x, death)
              int j = HARD_REGNO_NREGS (regno, GET_MODE (reg));
              while (--j >= 0)
                {
-                 offset = (regno + j) / REGSET_ELT_BITS;
-                 bit = (REGSET_ELT_TYPE) 1 << ((regno + j) % REGSET_ELT_BITS);
-                 
-                 bb_live_regs[offset] &= ~bit;
-                 bb_dead_regs[offset] |= bit;
+                 CLEAR_REGNO_REG_SET (bb_live_regs, regno + j);
+                 SET_REGNO_REG_SET (bb_dead_regs, regno + j);
                }
            }
          else
            {
-             bb_live_regs[offset] &= ~bit;
-             bb_dead_regs[offset] |= bit;
+             CLEAR_REGNO_REG_SET (bb_live_regs, regno);
+             SET_REGNO_REG_SET (bb_dead_regs, regno);
            }
        }
       else
@@ -2150,24 +1807,21 @@ sched_note_set (b, x, death)
              int j = HARD_REGNO_NREGS (regno, GET_MODE (reg));
              while (--j >= 0)
                {
-                 offset = (regno + j) / REGSET_ELT_BITS;
-                 bit = (REGSET_ELT_TYPE) 1 << ((regno + j) % REGSET_ELT_BITS);
-                 
-                 bb_live_regs[offset] |= bit;
-                 bb_dead_regs[offset] &= ~bit;
+                 SET_REGNO_REG_SET (bb_live_regs, regno + j);
+                 CLEAR_REGNO_REG_SET (bb_dead_regs, regno + j);
                }
            }
          else
            {
-             bb_live_regs[offset] |= bit;
-             bb_dead_regs[offset] &= ~bit;
+             SET_REGNO_REG_SET (bb_live_regs, regno);
+             CLEAR_REGNO_REG_SET (bb_dead_regs, regno);
            }
        }
     }
 }
 \f
 /* Macros and functions for keeping the priority queue sorted, and
-   dealing with queueing and unqueueing of instructions.  */
+   dealing with queueing and dequeueing of instructions.  */
 
 #define SCHED_SORT(READY, NEW_READY, OLD_READY) \
   do { if ((NEW_READY) - (OLD_READY) == 1)                             \
@@ -2182,16 +1836,17 @@ sched_note_set (b, x, death)
 
 static int
 rank_for_schedule (x, y)
-     rtx *x, *y;
+     const GENERIC_PTR x;
+     const GENERIC_PTR y;
 {
-  rtx tmp = *y;
-  rtx tmp2 = *x;
+  rtx tmp = *(rtx *)y;
+  rtx tmp2 = *(rtx *)x;
   rtx link;
   int tmp_class, tmp2_class;
   int value;
 
   /* Choose the instruction with the highest priority, if different.  */
-  if (value = INSN_PRIORITY (tmp) - INSN_PRIORITY (tmp2))
+  if ((value = INSN_PRIORITY (tmp) - INSN_PRIORITY (tmp2)))
     return value;
 
   if (last_scheduled_insn)
@@ -2217,7 +1872,7 @@ rank_for_schedule (x, y)
       else
        tmp2_class = 2;
 
-      if (value = tmp_class - tmp2_class)
+      if ((value = tmp_class - tmp2_class))
        return value;
     }
 
@@ -2278,15 +1933,13 @@ birthing_insn_p (pat)
     {
       rtx dest = SET_DEST (pat);
       int i = REGNO (dest);
-      int offset = i / REGSET_ELT_BITS;
-      REGSET_ELT_TYPE bit = (REGSET_ELT_TYPE) 1 << (i % REGSET_ELT_BITS);
 
       /* It would be more accurate to use refers_to_regno_p or
         reg_mentioned_p to determine when the dest is not live before this
         insn.  */
 
-      if (bb_live_regs[offset] & bit)
-       return (reg_n_sets[i] == 1);
+      if (REGNO_REG_SET_P (bb_live_regs, i))
+       return (REG_N_SETS (i) == 1);
 
       return 0;
     }
@@ -2344,6 +1997,9 @@ adjust_priority (prev)
            }
          break;
        }
+#ifdef ADJUST_PRIORITY
+      ADJUST_PRIORITY (prev);
+#endif
     }
 }
 
@@ -2509,7 +2165,7 @@ static void
 create_reg_dead_note (reg, insn)
      rtx reg, insn;
 {
-  rtx link, backlink;
+  rtx link;
                
   /* The number of registers killed after scheduling must be the same as the
      number of registers killed before scheduling.  The number of REG_DEAD
@@ -2544,6 +2200,12 @@ create_reg_dead_note (reg, insn)
                                           GET_MODE (XEXP (link, 0))));
       while (reg_note_regs < regs_killed)
        {
+         /* LINK might be zero if we killed more registers after scheduling
+            than before, and the last hard register we kill is actually
+            multiple hard regs.  */
+         if (link == NULL_RTX)
+           abort ();
+  
          link = XEXP (link, 1);
          reg_note_regs += (REGNO (XEXP (link, 0)) >= FIRST_PSEUDO_REGISTER ? 1
                            : HARD_REGNO_NREGS (REGNO (XEXP (link, 0)),
@@ -2556,7 +2218,7 @@ create_reg_dead_note (reg, insn)
        {
          rtx temp_reg, temp_link;
 
-         temp_reg = gen_rtx (REG, word_mode, 0);
+         temp_reg = gen_rtx_REG (word_mode, 0);
          temp_link = rtx_alloc (EXPR_LIST);
          PUT_REG_NOTE_KIND (temp_link, REG_DEAD);
          XEXP (temp_link, 0) = temp_reg;
@@ -2610,16 +2272,15 @@ attach_deaths (x, insn, set_p)
        /* This code is very similar to mark_used_1 (if set_p is false)
           and mark_set_1 (if set_p is true) in flow.c.  */
 
-       register int regno = REGNO (x);
-       register int offset = regno / REGSET_ELT_BITS;
-       register REGSET_ELT_TYPE bit
-         = (REGSET_ELT_TYPE) 1 << (regno % REGSET_ELT_BITS);
-       REGSET_ELT_TYPE all_needed = (old_live_regs[offset] & bit);
-       REGSET_ELT_TYPE some_needed = (old_live_regs[offset] & bit);
+       register int regno;
+       int some_needed;
+       int all_needed;
 
        if (set_p)
          return;
 
+       regno = REGNO (x);
+       all_needed = some_needed = REGNO_REG_SET_P (old_live_regs, regno);
        if (regno < FIRST_PSEUDO_REGISTER)
          {
            int n;
@@ -2627,12 +2288,9 @@ attach_deaths (x, insn, set_p)
            n = HARD_REGNO_NREGS (regno, GET_MODE (x));
            while (--n > 0)
              {
-               some_needed |= (old_live_regs[(regno + n) / REGSET_ELT_BITS]
-                               & ((REGSET_ELT_TYPE) 1
-                                  << ((regno + n) % REGSET_ELT_BITS)));
-               all_needed &= (old_live_regs[(regno + n) / REGSET_ELT_BITS]
-                              & ((REGSET_ELT_TYPE) 1
-                                 << ((regno + n) % REGSET_ELT_BITS)));
+               int needed = (REGNO_REG_SET_P (old_live_regs, regno + n));
+               some_needed |= needed;
+               all_needed &= needed;
              }
          }
 
@@ -2656,6 +2314,9 @@ attach_deaths (x, insn, set_p)
               STACK_POINTER_REGNUM, since these are always considered to be
               live.  Similarly for ARG_POINTER_REGNUM if it is fixed.  */
            if (regno != FRAME_POINTER_REGNUM
+#if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
+               && ! (regno == HARD_FRAME_POINTER_REGNUM)
+#endif
 #if ARG_POINTER_REGNUM != FRAME_POINTER_REGNUM
                && ! (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
 #endif
@@ -2663,6 +2324,16 @@ attach_deaths (x, insn, set_p)
              {
                if (! all_needed && ! dead_or_set_p (insn, x))
                  {
+                   /* Check for the case where the register dying partially
+                      overlaps the register set by this insn.  */
+                   if (regno < FIRST_PSEUDO_REGISTER
+                       && HARD_REGNO_NREGS (regno, GET_MODE (x)) > 1)
+                     {
+                       int n = HARD_REGNO_NREGS (regno, GET_MODE (x));
+                       while (--n >= 0)
+                         some_needed |= dead_or_set_regno_p (insn, regno + n);
+                     }
+
                    /* If none of the words in X is needed, make a REG_DEAD
                       note.  Otherwise, we must make partial REG_DEAD
                       notes.  */
@@ -2676,12 +2347,10 @@ attach_deaths (x, insn, set_p)
                           register that is set in the insn.  */
                        for (i = HARD_REGNO_NREGS (regno, GET_MODE (x)) - 1;
                             i >= 0; i--)
-                         if ((old_live_regs[(regno + i) / REGSET_ELT_BITS]
-                              & ((REGSET_ELT_TYPE) 1
-                                 << ((regno +i) % REGSET_ELT_BITS))) == 0
+                         if (! REGNO_REG_SET_P (old_live_regs, regno + i)
                              && ! dead_or_set_regno_p (insn, regno + i))
-                           create_reg_dead_note (gen_rtx (REG, word_mode,
-                                                          regno + i),
+                           create_reg_dead_note (gen_rtx_REG (reg_raw_mode[regno + i],
+                                                              regno + i),
                                                  insn);
                      }
                  }
@@ -2692,18 +2361,14 @@ attach_deaths (x, insn, set_p)
                int j = HARD_REGNO_NREGS (regno, GET_MODE (x));
                while (--j >= 0)
                  {
-                   offset = (regno + j) / REGSET_ELT_BITS;
-                   bit
-                     = (REGSET_ELT_TYPE) 1 << ((regno + j) % REGSET_ELT_BITS);
-
-                   bb_dead_regs[offset] &= ~bit;
-                   bb_live_regs[offset] |= bit;
+                   CLEAR_REGNO_REG_SET (bb_dead_regs, regno + j);
+                   SET_REGNO_REG_SET (bb_live_regs, regno + j);
                  }
              }
            else
              {
-               bb_dead_regs[offset] &= ~bit;
-               bb_live_regs[offset] |= bit;
+               CLEAR_REGNO_REG_SET (bb_dead_regs, regno);
+               SET_REGNO_REG_SET (bb_live_regs, regno);
              }
          }
        return;
@@ -2715,17 +2380,20 @@ attach_deaths (x, insn, set_p)
       return;
 
     case SUBREG:
+      attach_deaths (SUBREG_REG (x), insn,
+                    set_p && ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (x)))
+                              <= UNITS_PER_WORD)
+                              || (GET_MODE_SIZE (GET_MODE (SUBREG_REG (x)))
+                                  == GET_MODE_SIZE (GET_MODE ((x))))));
+      return;
+
     case STRICT_LOW_PART:
-      /* These two cases preserve the value of SET_P, so handle them
-        separately.  */
-      attach_deaths (XEXP (x, 0), insn, set_p);
+      attach_deaths (XEXP (x, 0), insn, 0);
       return;
 
     case ZERO_EXTRACT:
     case SIGN_EXTRACT:
-      /* This case preserves the value of SET_P for the first operand, but
-        clears it for the other two.  */
-      attach_deaths (XEXP (x, 0), insn, set_p);
+      attach_deaths (XEXP (x, 0), insn, 0);
       attach_deaths (XEXP (x, 1), insn, 0);
       attach_deaths (XEXP (x, 2), insn, 0);
       return;
@@ -2753,6 +2421,7 @@ attach_deaths_insn (insn)
 {
   rtx x = PATTERN (insn);
   register RTX_CODE code = GET_CODE (x);
+  rtx link;
 
   if (code == SET)
     {
@@ -2781,10 +2450,19 @@ attach_deaths_insn (insn)
            attach_deaths (XVECEXP (x, 0, i), insn, 0);
        }
     }
-  /* Flow does not add REG_DEAD notes to registers that die in
-     clobbers, so we can't either.  */
+  /* If this is a CLOBBER, only add REG_DEAD notes to registers inside a
+     MEM being clobbered, just like flow.  */
+  else if (code == CLOBBER && GET_CODE (XEXP (x, 0)) == MEM)
+    attach_deaths (XEXP (XEXP (x, 0), 0), insn, 0);
+  /* Otherwise don't add a death note to things being clobbered.  */
   else if (code != CLOBBER)
     attach_deaths (x, insn, 0);
+
+  /* Make death notes for things used in the called function.  */
+  if (GET_CODE (insn) == CALL_INSN)
+    for (link = CALL_INSN_FUNCTION_USAGE (insn); link; link = XEXP (link, 1))
+      attach_deaths (XEXP (XEXP (link, 0), 0), insn,
+                    GET_CODE (XEXP (link, 0)) == CLOBBER);
 }
 
 /* Delete notes beginning with INSN and maybe put them in the chain
@@ -2809,7 +2487,18 @@ unlink_notes (insn, tail)
       if (write_symbols != NO_DEBUG && NOTE_LINE_NUMBER (insn) > 0)
        /* Record line-number notes so they can be reused.  */
        LINE_NOTE (insn) = insn;
-      else
+
+      /* Don't save away NOTE_INSN_SETJMPs, because they must remain
+        immediately after the call they follow.  We use a fake
+        (REG_DEAD (const_int -1)) note to remember them.
+        Likewise with NOTE_INSN_{LOOP,EHREGION}_{BEG, END}.  */
+      else if (NOTE_LINE_NUMBER (insn) != NOTE_INSN_SETJMP
+              && NOTE_LINE_NUMBER (insn) != NOTE_INSN_LOOP_BEG
+              && NOTE_LINE_NUMBER (insn) != NOTE_INSN_LOOP_END
+              && NOTE_LINE_NUMBER (insn) != NOTE_INSN_RANGE_START
+              && NOTE_LINE_NUMBER (insn) != NOTE_INSN_RANGE_END
+              && NOTE_LINE_NUMBER (insn) != NOTE_INSN_EH_REGION_BEG
+              && NOTE_LINE_NUMBER (insn) != NOTE_INSN_EH_REGION_END)
        {
          /* Insert the note at the end of the notes list.  */
          PREV_INSN (insn) = note_list;
@@ -2823,26 +2512,15 @@ unlink_notes (insn, tail)
   return insn;
 }
 
-/* Data structure for keeping track of register information
-   during that register's life.  */
-
-struct sometimes
-{
-  short offset; short bit;
-  short live_length; short calls_crossed;
-};
-
 /* Constructor for `sometimes' data structure.  */
 
 static int
-new_sometimes_live (regs_sometimes_live, offset, bit, sometimes_max)
+new_sometimes_live (regs_sometimes_live, regno, sometimes_max)
      struct sometimes *regs_sometimes_live;
-     int offset, bit;
+     int regno;
      int sometimes_max;
 {
   register struct sometimes *p;
-  register int regno = offset * REGSET_ELT_BITS + bit;
-  int i;
 
   /* There should never be a register greater than max_regno here.  If there
      is, it means that a define_split has created a new pseudo reg.  This
@@ -2852,8 +2530,7 @@ new_sometimes_live (regs_sometimes_live, offset, bit, sometimes_max)
     abort ();
 
   p = &regs_sometimes_live[sometimes_max];
-  p->offset = offset;
-  p->bit = bit;
+  p->regno = regno;
   p->live_length = 0;
   p->calls_crossed = 0;
   sometimes_max++;
@@ -2873,15 +2550,52 @@ finish_sometimes_live (regs_sometimes_live, sometimes_max)
   for (i = 0; i < sometimes_max; i++)
     {
       register struct sometimes *p = &regs_sometimes_live[i];
-      int regno;
-
-      regno = p->offset * REGSET_ELT_BITS + p->bit;
+      int regno = p->regno;
 
       sched_reg_live_length[regno] += p->live_length;
       sched_reg_n_calls_crossed[regno] += p->calls_crossed;
     }
 }
 
+/* Search INSN for fake REG_DEAD note pairs for NOTE_INSN_SETJMP,
+   NOTE_INSN_{LOOP,EHREGION}_{BEG,END}; and convert them back into
+   NOTEs.  The REG_DEAD note following first one is contains the saved
+   value for NOTE_BLOCK_NUMBER which is useful for
+   NOTE_INSN_EH_REGION_{BEG,END} NOTEs.  LAST is the last instruction
+   output by the instruction scheduler.  Return the new value of LAST.  */
+
+static rtx
+reemit_notes (insn, last)
+     rtx insn;
+     rtx last;
+{
+  rtx note;
+
+  for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
+    {
+      if (REG_NOTE_KIND (note) == REG_DEAD
+         && GET_CODE (XEXP (note, 0)) == CONST_INT)
+       {
+         if (INTVAL (XEXP (note, 0)) == NOTE_INSN_SETJMP)
+           {
+             CONST_CALL_P (emit_note_after (INTVAL (XEXP (note, 0)), insn))
+               = CONST_CALL_P (note);
+             remove_note (insn, note);
+             note = XEXP (note, 1);
+           }
+         else
+           {
+             last = emit_note_before (INTVAL (XEXP (note, 0)), last);
+             remove_note (insn, note);
+             note = XEXP (note, 1);
+             NOTE_BLOCK_NUMBER (last) = INTVAL (XEXP (note, 0));
+           }
+         remove_note (insn, note);
+       }
+    }
+  return last;
+}
+
 /* Use modified list scheduling to rearrange insns in basic block
    B.  FILE, if nonzero, is where we dump interesting output about
    this pass.  */
@@ -2892,9 +2606,8 @@ schedule_block (b, file)
      FILE *file;
 {
   rtx insn, last;
-  rtx last_note = 0;
   rtx *ready, link;
-  int i, j, n_ready = 0, new_ready, n_insns = 0;
+  int i, j, n_ready = 0, new_ready, n_insns;
   int sched_n_insns = 0;
   int clock;
 #define NEED_NOTHING   0
@@ -2922,11 +2635,22 @@ schedule_block (b, file)
 
   i = max_reg_num ();
   reg_last_uses = (rtx *) alloca (i * sizeof (rtx));
-  bzero (reg_last_uses, i * sizeof (rtx));
+  bzero ((char *) reg_last_uses, i * sizeof (rtx));
   reg_last_sets = (rtx *) alloca (i * sizeof (rtx));
-  bzero (reg_last_sets, i * sizeof (rtx));
+  bzero ((char *) reg_last_sets, i * sizeof (rtx));
+  reg_pending_sets = ALLOCA_REG_SET ();
+  CLEAR_REG_SET (reg_pending_sets);
+  reg_pending_sets_all = 0;
   clear_units ();
 
+#if 0
+  /* We used to have code to avoid getting parameters moved from hard
+     argument registers into pseudos.
+
+     However, it was removed when it proved to be of marginal benefit and
+     caused problems because of different notions of what the "head" insn
+     was.  */
+
   /* Remove certain insns at the beginning from scheduling,
      by advancing HEAD.  */
 
@@ -2957,6 +2681,7 @@ schedule_block (b, file)
          head = NEXT_INSN (head);
        }
     }
+#endif
 
   /* Don't include any notes or labels at the beginning of the
      basic block, or notes at the ends of basic blocks.  */
@@ -2974,7 +2699,7 @@ schedule_block (b, file)
      to schedule this block.  */
   if (head == tail
       && (GET_CODE (head) == NOTE || GET_CODE (head) == CODE_LABEL))
-    return;
+    goto ret;
 
 #if 0
   /* This short-cut doesn't work.  It does not count call insns crossed by
@@ -2987,7 +2712,7 @@ schedule_block (b, file)
      has one insn, so this won't slow down this pass by much.  */
 
   if (head == tail)
-    return;
+    goto ret;
 #endif
 
   /* Now HEAD through TAIL are the insns actually to be rearranged;
@@ -3008,11 +2733,11 @@ schedule_block (b, file)
 
   LOG_LINKS (sched_before_next_call) = 0;
 
-  n_insns += sched_analyze (head, tail);
+  n_insns = sched_analyze (head, tail);
   if (n_insns == 0)
     {
       free_pending_lists ();
-      return;
+      goto ret;
     }
 
   /* Allocate vector to hold insns to be rearranged (except those
@@ -3102,9 +2827,7 @@ schedule_block (b, file)
            {
              while (SCHED_GROUP_P (insn))
                {
-                 insn = PREV_INSN (insn);
-                 while (GET_CODE (insn) == NOTE)
-                   insn = PREV_INSN (insn);
+                 insn = prev_nonnote_insn (insn);
                  priority (insn);
                }
              continue;
@@ -3144,7 +2867,7 @@ schedule_block (b, file)
          finish_sometimes_live (regs_sometimes_live, sometimes_max);
        }
       free_pending_lists ();
-      return;
+      goto ret;
     }
 #endif
 
@@ -3158,8 +2881,8 @@ schedule_block (b, file)
 
   if (reload_completed == 0)
     {
-      bcopy (basic_block_live_at_start[b], bb_live_regs, regset_bytes);
-      bzero (bb_dead_regs, regset_bytes);
+      COPY_REG_SET (bb_live_regs, basic_block_live_at_start[b]);
+      CLEAR_REG_SET (bb_dead_regs);
 
       if (b == 0)
        {
@@ -3182,33 +2905,46 @@ schedule_block (b, file)
                   a register must be marked as dead after this insn.  */
                if (GET_CODE (PATTERN (insn)) == SET
                    || GET_CODE (PATTERN (insn)) == CLOBBER)
-                 sched_note_set (b, PATTERN (insn), 0);
+                 sched_note_set (PATTERN (insn), 0);
                else if (GET_CODE (PATTERN (insn)) == PARALLEL)
                  {
                    int j;
                    for (j = XVECLEN (PATTERN (insn), 0) - 1; j >= 0; j--)
                      if (GET_CODE (XVECEXP (PATTERN (insn), 0, j)) == SET
                          || GET_CODE (XVECEXP (PATTERN (insn), 0, j)) == CLOBBER)
-                       sched_note_set (b, XVECEXP (PATTERN (insn), 0, j), 0);
+                       sched_note_set (XVECEXP (PATTERN (insn), 0, j), 0);
 
                    /* ??? This code is obsolete and should be deleted.  It
                       is harmless though, so we will leave it in for now.  */
                    for (j = XVECLEN (PATTERN (insn), 0) - 1; j >= 0; j--)
                      if (GET_CODE (XVECEXP (PATTERN (insn), 0, j)) == USE)
-                       sched_note_set (b, XVECEXP (PATTERN (insn), 0, j), 0);
+                       sched_note_set (XVECEXP (PATTERN (insn), 0, j), 0);
+                 }
+
+               /* Each call clobbers (makes live) all call-clobbered regs
+                  that are not global or fixed.  Note that the function-value
+                  reg is a call_clobbered reg.  */
+
+               if (GET_CODE (insn) == CALL_INSN)
+                 {
+                   int j;
+                   for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
+                     if (call_used_regs[j] && ! global_regs[j]
+                         && ! fixed_regs[j])
+                       {
+                         SET_REGNO_REG_SET (bb_live_regs, j);
+                         CLEAR_REGNO_REG_SET (bb_dead_regs, j);
+                       }
                  }
 
                for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
                  {
                    if ((REG_NOTE_KIND (link) == REG_DEAD
                         || REG_NOTE_KIND (link) == REG_UNUSED)
-                       /* Verify that the REG_NOTE has a legal value.  */
+                       /* Verify that the REG_NOTE has a valid value.  */
                        && GET_CODE (XEXP (link, 0)) == REG)
                      {
                        register int regno = REGNO (XEXP (link, 0));
-                       register int offset = regno / REGSET_ELT_BITS;
-                       register REGSET_ELT_TYPE bit
-                         = (REGSET_ELT_TYPE) 1 << (regno % REGSET_ELT_BITS);
 
                        if (regno < FIRST_PSEUDO_REGISTER)
                          {
@@ -3216,18 +2952,14 @@ schedule_block (b, file)
                                                      GET_MODE (XEXP (link, 0)));
                            while (--j >= 0)
                              {
-                               offset = (regno + j) / REGSET_ELT_BITS;
-                               bit = ((REGSET_ELT_TYPE) 1
-                                      << ((regno + j) % REGSET_ELT_BITS));
-
-                               bb_live_regs[offset] &= ~bit;
-                               bb_dead_regs[offset] |= bit;
+                               CLEAR_REGNO_REG_SET (bb_live_regs, regno + j);
+                               SET_REGNO_REG_SET (bb_dead_regs, regno + j);
                              }
                          }
                        else
                          {
-                           bb_live_regs[offset] &= ~bit;
-                           bb_dead_regs[offset] |= bit;
+                           CLEAR_REGNO_REG_SET (bb_live_regs, regno);
+                           SET_REGNO_REG_SET (bb_dead_regs, regno);
                          }
                      }
                  }
@@ -3283,37 +3015,48 @@ schedule_block (b, file)
             must be marked as dead after this insn.  */
          if (GET_CODE (PATTERN (insn)) == SET
              || GET_CODE (PATTERN (insn)) == CLOBBER)
-           sched_note_set (b, PATTERN (insn), 0);
+           sched_note_set (PATTERN (insn), 0);
          else if (GET_CODE (PATTERN (insn)) == PARALLEL)
            {
              int j;
              for (j = XVECLEN (PATTERN (insn), 0) - 1; j >= 0; j--)
                if (GET_CODE (XVECEXP (PATTERN (insn), 0, j)) == SET
                    || GET_CODE (XVECEXP (PATTERN (insn), 0, j)) == CLOBBER)
-                 sched_note_set (b, XVECEXP (PATTERN (insn), 0, j), 0);
+                 sched_note_set (XVECEXP (PATTERN (insn), 0, j), 0);
 
              /* ??? This code is obsolete and should be deleted.  It
                 is harmless though, so we will leave it in for now.  */
              for (j = XVECLEN (PATTERN (insn), 0) - 1; j >= 0; j--)
                if (GET_CODE (XVECEXP (PATTERN (insn), 0, j)) == USE)
-                 sched_note_set (b, XVECEXP (PATTERN (insn), 0, j), 0);
+                 sched_note_set (XVECEXP (PATTERN (insn), 0, j), 0);
+           }
+
+         /* Each call clobbers (makes live) all call-clobbered regs that are
+            not global or fixed.  Note that the function-value reg is a
+            call_clobbered reg.  */
+
+         if (GET_CODE (insn) == CALL_INSN)
+           {
+             int j;
+             for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
+               if (call_used_regs[j] && ! global_regs[j]
+                   && ! fixed_regs[j])
+                 {
+                   SET_REGNO_REG_SET (bb_live_regs, j);
+                   CLEAR_REGNO_REG_SET (bb_dead_regs, j);
+                 }
            }
 
          /* Need to know what registers this insn kills.  */
          for (prev = 0, link = REG_NOTES (insn); link; link = next)
            {
-             int regno;
-
              next = XEXP (link, 1);
              if ((REG_NOTE_KIND (link) == REG_DEAD
                   || REG_NOTE_KIND (link) == REG_UNUSED)
-                 /* Verify that the REG_NOTE has a legal value.  */
+                 /* Verify that the REG_NOTE has a valid value.  */
                  && GET_CODE (XEXP (link, 0)) == REG)
                {
                  register int regno = REGNO (XEXP (link, 0));
-                 register int offset = regno / REGSET_ELT_BITS;
-                 register REGSET_ELT_TYPE bit
-                   = (REGSET_ELT_TYPE) 1 << (regno % REGSET_ELT_BITS);
 
                  /* Only unlink REG_DEAD notes; leave REG_UNUSED notes
                     alone.  */
@@ -3335,18 +3078,14 @@ schedule_block (b, file)
                                                GET_MODE (XEXP (link, 0)));
                      while (--j >= 0)
                        {
-                         offset = (regno + j) / REGSET_ELT_BITS;
-                         bit = ((REGSET_ELT_TYPE) 1
-                                << ((regno + j) % REGSET_ELT_BITS));
-
-                         bb_live_regs[offset] &= ~bit;
-                         bb_dead_regs[offset] |= bit;
+                         CLEAR_REGNO_REG_SET (bb_live_regs, regno + j);
+                         SET_REGNO_REG_SET (bb_dead_regs, regno + j);
                        }
                    }
                  else
                    {
-                     bb_live_regs[offset] &= ~bit;
-                     bb_dead_regs[offset] |= bit;
+                     CLEAR_REGNO_REG_SET (bb_live_regs, regno);
+                     SET_REGNO_REG_SET (bb_dead_regs, regno);
                    }
                }
              else
@@ -3358,25 +3097,19 @@ schedule_block (b, file)
   if (reload_completed == 0)
     {
       /* Keep track of register lives.  */
-      old_live_regs = (regset) alloca (regset_bytes);
+      old_live_regs = ALLOCA_REG_SET ();
       regs_sometimes_live
        = (struct sometimes *) alloca (max_regno * sizeof (struct sometimes));
       sometimes_max = 0;
 
       /* Start with registers live at end.  */
-      for (j = 0; j < regset_size; j++)
-       {
-         REGSET_ELT_TYPE live = bb_live_regs[j];
-         old_live_regs[j] = live;
-         if (live)
-           {
-             register REGSET_ELT_TYPE bit;
-             for (bit = 0; bit < REGSET_ELT_BITS; bit++)
-               if (live & ((REGSET_ELT_TYPE) 1 << bit))
-                 sometimes_max = new_sometimes_live (regs_sometimes_live, j,
-                                                     bit, sometimes_max);
-           }
-       }
+      COPY_REG_SET (old_live_regs, bb_live_regs);
+      EXECUTE_IF_SET_IN_REG_SET (bb_live_regs, 0, j,
+                                {
+                                  sometimes_max
+                                    = new_sometimes_live (regs_sometimes_live,
+                                                          j, sometimes_max);
+                                });
     }
 
   SCHED_SORT (ready, n_ready, 1);
@@ -3401,7 +3134,7 @@ schedule_block (b, file)
 
   /* Q_SIZE will always be zero here.  */
   q_ptr = 0; clock = 0;
-  bzero (insn_queue, sizeof (insn_queue));
+  bzero ((char *) insn_queue, sizeof (insn_queue));
 
   /* Now, perform list scheduling.  */
 
@@ -3437,7 +3170,7 @@ schedule_block (b, file)
          register int stalls;
 
          for (stalls = 1; stalls < INSN_QUEUE_SIZE; stalls++)
-           if (insn = insn_queue[NEXT_Q_AFTER (q_ptr, stalls)])
+           if ((insn = insn_queue[NEXT_Q_AFTER (q_ptr, stalls)]))
              {
                for (; insn; insn = NEXT_INSN (insn))
                  {
@@ -3525,14 +3258,14 @@ schedule_block (b, file)
              /* See if this is the last notice we must take of a register.  */
              if (GET_CODE (PATTERN (insn)) == SET
                  || GET_CODE (PATTERN (insn)) == CLOBBER)
-               sched_note_set (b, PATTERN (insn), 1);
+               sched_note_set (PATTERN (insn), 1);
              else if (GET_CODE (PATTERN (insn)) == PARALLEL)
                {
                  int j;
                  for (j = XVECLEN (PATTERN (insn), 0) - 1; j >= 0; j--)
                    if (GET_CODE (XVECEXP (PATTERN (insn), 0, j)) == SET
                        || GET_CODE (XVECEXP (PATTERN (insn), 0, j)) == CLOBBER)
-                     sched_note_set (b, XVECEXP (PATTERN (insn), 0, j), 1);
+                     sched_note_set (XVECEXP (PATTERN (insn), 0, j), 1);
                }
              
              /* This code keeps life analysis information up to date.  */
@@ -3540,18 +3273,15 @@ schedule_block (b, file)
                {
                  register struct sometimes *p;
 
-                 /* A call kills all call used and global registers, except
-                    for those mentioned in the call pattern which will be
-                    made live again later.  */
+                 /* A call kills all call used registers that are not
+                    global or fixed, except for those mentioned in the call
+                    pattern which will be made live again later.  */
                  for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
-                   if (call_used_regs[i] || global_regs[i])
+                   if (call_used_regs[i] && ! global_regs[i]
+                       && ! fixed_regs[i])
                      {
-                       register int offset = i / REGSET_ELT_BITS;
-                       register REGSET_ELT_TYPE bit
-                         = (REGSET_ELT_TYPE) 1 << (i % REGSET_ELT_BITS);
-
-                       bb_live_regs[offset] &= ~bit;
-                       bb_dead_regs[offset] |= bit;
+                       CLEAR_REGNO_REG_SET (bb_live_regs, i);
+                       SET_REGNO_REG_SET (bb_dead_regs, i);
                      }
 
                  /* Regs live at the time of a call instruction must not
@@ -3562,8 +3292,7 @@ schedule_block (b, file)
                     (below).  */
                  p = regs_sometimes_live;
                  for (i = 0; i < sometimes_max; i++, p++)
-                   if (bb_live_regs[p->offset]
-                       & ((REGSET_ELT_TYPE) 1 << p->bit))
+                   if (REGNO_REG_SET_P (bb_live_regs, p->regno))
                      p->calls_crossed += 1;
                }
 
@@ -3572,20 +3301,13 @@ schedule_block (b, file)
              attach_deaths_insn (insn);
 
              /* Find registers now made live by that instruction.  */
-             for (i = 0; i < regset_size; i++)
-               {
-                 REGSET_ELT_TYPE diff = bb_live_regs[i] & ~old_live_regs[i];
-                 if (diff)
-                   {
-                     register int bit;
-                     old_live_regs[i] |= diff;
-                     for (bit = 0; bit < REGSET_ELT_BITS; bit++)
-                       if (diff & ((REGSET_ELT_TYPE) 1 << bit))
-                         sometimes_max
-                           = new_sometimes_live (regs_sometimes_live, i, bit,
-                                                 sometimes_max);
-                   }
-               }
+             EXECUTE_IF_AND_COMPL_IN_REG_SET (bb_live_regs, old_live_regs, 0, i,
+                                              {
+                                                sometimes_max
+                                                  = new_sometimes_live (regs_sometimes_live,
+                                                                        i, sometimes_max);
+                                              });
+             IOR_REG_SET (old_live_regs, bb_live_regs);
 
              /* Count lengths of all regs we are worrying about now,
                 and handle registers no longer live.  */
@@ -3593,20 +3315,18 @@ schedule_block (b, file)
              for (i = 0; i < sometimes_max; i++)
                {
                  register struct sometimes *p = &regs_sometimes_live[i];
-                 int regno = p->offset*REGSET_ELT_BITS + p->bit;
+                 int regno = p->regno;
 
                  p->live_length += 1;
 
-                 if ((bb_live_regs[p->offset]
-                      & ((REGSET_ELT_TYPE) 1 << p->bit)) == 0)
+                 if (!REGNO_REG_SET_P (bb_live_regs, p->regno))
                    {
                      /* This is the end of one of this register's lifetime
                         segments.  Save the lifetime info collected so far,
                         and clear its bit in the old_live_regs entry.  */
                      sched_reg_live_length[regno] += p->live_length;
                      sched_reg_n_calls_crossed[regno] += p->calls_crossed;
-                     old_live_regs[p->offset]
-                       &= ~((REGSET_ELT_TYPE) 1 << p->bit);
+                     CLEAR_REGNO_REG_SET (old_live_regs, p->regno);
 
                      /* Delete the reg_sometimes_live entry for this reg by
                         copying the last entry over top of it.  */
@@ -3633,7 +3353,6 @@ schedule_block (b, file)
       sched_n_insns += 1;
       NEXT_INSN (insn) = last;
       PREV_INSN (last) = insn;
-      last = insn;
 
       /* Everything that precedes INSN now either becomes "ready", if
         it can execute immediately before INSN, or "pending", if
@@ -3648,7 +3367,8 @@ schedule_block (b, file)
       /* Schedule all prior insns that must not be moved.  */
       if (SCHED_GROUP_P (insn))
        {
-         /* Disable these insns from being launched.  */
+         /* Disable these insns from being launched, in case one of the
+            insns in the group has a dependency on an earlier one.  */
          link = insn;
          while (SCHED_GROUP_P (link))
            {
@@ -3657,19 +3377,44 @@ schedule_block (b, file)
              INSN_REF_COUNT (link) = 0;
            }
 
-         /* None of these insns can move forward into delay slots.  */
-         while (SCHED_GROUP_P (insn))
+         /* Now handle each group insn like the main insn was handled
+            above.  */
+         link = insn;
+         while (SCHED_GROUP_P (link))
            {
-             insn = PREV_INSN (insn);
-             new_ready = schedule_insn (insn, ready, new_ready, clock);
-             INSN_PRIORITY (insn) = DONE_PRIORITY;
+             link = PREV_INSN (link);
 
              sched_n_insns += 1;
-             NEXT_INSN (insn) = last;
-             PREV_INSN (last) = insn;
-             last = insn;
+
+             /* ??? Why don't we set LAUNCH_PRIORITY here?  */
+             new_ready = schedule_insn (link, ready, new_ready, clock);
+             INSN_PRIORITY (link) = DONE_PRIORITY;
+           }
+       }
+
+      /* Put back NOTE_INSN_SETJMP,
+         NOTE_INSN_{LOOP,EHREGION}_{BEGIN,END} notes.  */
+
+      /* To prime the loop.  We need to handle INSN and all the insns in the
+         sched group.  */
+      last = NEXT_INSN (insn);
+      do
+       {
+         insn = PREV_INSN (last);
+
+         /* Maintain a valid chain so emit_note_before works.
+            This is necessary because PREV_INSN (insn) isn't valid
+            (if ! SCHED_GROUP_P) and if it points to an insn already
+            scheduled, a circularity will result.  */
+         if (! SCHED_GROUP_P (insn))
+           {
+             NEXT_INSN (prev_head) = insn;
+             PREV_INSN (insn) = prev_head;
            }
+
+         last = reemit_notes (insn, insn);
        }
+      while (SCHED_GROUP_P (insn));
     }
   if (q_size != 0)
     abort ();
@@ -3680,7 +3425,7 @@ schedule_block (b, file)
   /* HEAD is now the first insn in the chain of insns that
      been scheduled by the loop above.
      TAIL is the last of those insns.  */
-  head = insn;
+  head = last;
 
   /* NOTE_LIST is the end of a chain of notes previously found
      among the insns.  Insert them at the beginning of the insns.  */
@@ -3753,7 +3498,7 @@ schedule_block (b, file)
            prev = PREV_INSN (insn);
            if (LINE_NOTE (note))
              {
-               /* Re-use the original line-number note. */
+               /* Re-use the original line-number note.  */
                LINE_NOTE (note) = 0;
                PREV_INSN (note) = prev;
                NEXT_INSN (prev) = note;
@@ -3765,6 +3510,7 @@ schedule_block (b, file)
                notes++;
                new = emit_note_after (NOTE_LINE_NUMBER (note), prev);
                NOTE_SOURCE_FILE (new) = NOTE_SOURCE_FILE (note);
+               RTX_INTEGRATED_P (new) = RTX_INTEGRATED_P (note);
              }
          }
       if (file && notes)
@@ -3780,6 +3526,10 @@ schedule_block (b, file)
   /* Yow! We're done!  */
   free_pending_lists ();
 
+ret:
+  FREE_REG_SET (reg_pending_sets);
+  FREE_REG_SET (old_live_regs);
+
   return;
 }
 \f
@@ -3787,7 +3537,7 @@ schedule_block (b, file)
    REGNO, returning the rtx of the reference found if any.  Otherwise,
    returns 0.  */
 
-rtx
+static rtx
 regno_use_in (regno, x)
      int regno;
      rtx x;
@@ -3804,12 +3554,12 @@ regno_use_in (regno, x)
     {
       if (fmt[i] == 'e')
        {
-         if (tem = regno_use_in (regno, XEXP (x, i)))
+         if ((tem = regno_use_in (regno, XEXP (x, i))))
            return tem;
        }
       else if (fmt[i] == 'E')
        for (j = XVECLEN (x, i) - 1; j >= 0; j--)
-         if (tem = regno_use_in (regno , XVECEXP (x, i, j)))
+         if ((tem = regno_use_in (regno , XVECEXP (x, i, j))))
            return tem;
     }
 
@@ -3822,8 +3572,8 @@ regno_use_in (regno, x)
    several smaller hard register references in the split insns.  */
 
 static void
-split_hard_reg_notes (note, first, last, orig_insn)
-     rtx note, first, last, orig_insn;
+split_hard_reg_notes (note, first, last)
+  rtx note, first, last;
 {
   rtx reg, temp, link;
   int n_regs, i, new_reg;
@@ -3886,6 +3636,13 @@ new_insn_dead_notes (pat, insn, last, orig_insn)
 
   if (GET_CODE (dest) == REG)
     {
+      /* If the original insn already used this register, we may not add new
+         notes for it.  One example for a split that needs this test is
+        when a multi-word memory access with register-indirect addressing
+        is split into multiple memory accesses with auto-increment and
+        one adjusting add instruction for the address register.  */
+      if (reg_referenced_p (dest, PATTERN (orig_insn)))
+       return;
       for (tem = last; tem != insn; tem = PREV_INSN (tem))
        {
          if (GET_RTX_CLASS (GET_CODE (tem)) == 'i'
@@ -3900,7 +3657,7 @@ new_insn_dead_notes (pat, insn, last, orig_insn)
                     || GET_CODE (tem_dest) == SIGN_EXTRACT)
                tem_dest = XEXP (tem_dest, 0);
 
-             if (tem_dest != dest)
+             if (! rtx_equal_p (tem_dest, dest))
                {
                  /* Use the same scheme as combine.c, don't put both REG_DEAD
                     and REG_UNUSED notes on the same insn.  */
@@ -3992,10 +3749,10 @@ update_n_sets (x, inc)
          int endregno = regno + HARD_REGNO_NREGS (regno, GET_MODE (dest));
          
          for (i = regno; i < endregno; i++)
-           reg_n_sets[i] += inc;
+           REG_N_SETS (i) += inc;
        }
       else
-       reg_n_sets[regno] += inc;
+       REG_N_SETS (regno) += inc;
     }
 }
 
@@ -4045,7 +3802,7 @@ update_flow_info (notes, first, last, orig_insn)
                      && GET_CODE (temp) == REG
                      && REGNO (temp) < FIRST_PSEUDO_REGISTER
                      && HARD_REGNO_NREGS (REGNO (temp), GET_MODE (temp)) > 1)
-                   split_hard_reg_notes (note, first, last, orig_insn);
+                   split_hard_reg_notes (note, first, last);
                  else
                    {
                      XEXP (note, 1) = REG_NOTES (insn);
@@ -4057,6 +3814,7 @@ update_flow_info (notes, first, last, orig_insn)
                  /* ??? This won't handle multiple word registers correctly,
                     but should be good enough for now.  */
                  if (REG_NOTE_KIND (note) == REG_UNUSED
+                     && GET_CODE (XEXP (note, 0)) != SCRATCH
                      && ! dead_or_set_p (insn, XEXP (note, 0)))
                    PUT_REG_NOTE_KIND (note, REG_DEAD);
 
@@ -4070,7 +3828,7 @@ update_flow_info (notes, first, last, orig_insn)
                 register that was not needed by this instantiation of the
                 pattern, so we can safely ignore it.  */
              if (insn == first)
-               {
+               {                       
                  if (REG_NOTE_KIND (note) != REG_UNUSED)
                    abort ();
 
@@ -4080,6 +3838,14 @@ update_flow_info (notes, first, last, orig_insn)
          break;
 
        case REG_WAS_0:
+         /* If the insn that set the register to 0 was deleted, this
+            note cannot be relied on any longer.  The destination might
+            even have been moved to memory.
+             This was observed for SH4 with execute/920501-6.c compilation,
+            -O2 -fomit-frame-pointer -finline-functions .  */
+         if (GET_CODE (XEXP (note, 0)) == NOTE
+             || INSN_DELETED_P (XEXP (note, 0)))
+           break;
          /* This note applies to the dest of the original insn.  Find the
             first new insn that now has the same dest, and move the note
             there.  */
@@ -4099,6 +3865,15 @@ update_flow_info (notes, first, last, orig_insn)
                     uses it.  */
                  break;
                }
+             /* If this note refers to a multiple word hard
+                register, it may have been split into several smaller
+                hard register references.  We could split the notes,
+                but simply dropping them is good enough.  */
+             if (GET_CODE (orig_dest) == REG
+                 && REGNO (orig_dest) < FIRST_PSEUDO_REGISTER
+                 && HARD_REGNO_NREGS (REGNO (orig_dest),
+                                      GET_MODE (orig_dest)) > 1)
+                   break;
              /* It must be set somewhere, fail if we couldn't find where it
                 was set.  */
              if (insn == last)
@@ -4135,7 +3910,22 @@ update_flow_info (notes, first, last, orig_insn)
              /* The original dest must still be set someplace.  Abort if we
                 couldn't find it.  */
              if (insn == first)
-               abort ();
+               {
+                 /* However, if this note refers to a multiple word hard
+                    register, it may have been split into several smaller
+                    hard register references.  We could split the notes,
+                    but simply dropping them is good enough.  */
+                 if (GET_CODE (orig_dest) == REG
+                     && REGNO (orig_dest) < FIRST_PSEUDO_REGISTER
+                     && HARD_REGNO_NREGS (REGNO (orig_dest),
+                                          GET_MODE (orig_dest)) > 1)
+                   break;
+                 /* Likewise for multi-word memory references.  */
+                 if (GET_CODE (orig_dest) == MEM
+                     && SIZE_FOR_MODE (orig_dest) > MOVE_MAX)
+                   break;
+                 abort ();
+               }
            }
          break;
 
@@ -4151,6 +3941,12 @@ update_flow_info (notes, first, last, orig_insn)
            XEXP (note, 0) = first;
          break;
 
+       case REG_EXEC_COUNT:
+         /* Move a REG_EXEC_COUNT note to the first insn created.  */
+         XEXP (note, 1) = REG_NOTES (first);
+         REG_NOTES (first) = note;
+         break;
+
        case REG_RETVAL:
          /* Move a REG_RETVAL note to the last insn created, and update
             the corresponding REG_LIBCALL note.  */
@@ -4164,6 +3960,7 @@ update_flow_info (notes, first, last, orig_insn)
          break;
 
        case REG_NONNEG:
+       case REG_BR_PROB:
          /* This should be moved to whichever instruction is a JUMP_INSN.  */
 
          for (insn = last; ; insn = PREV_INSN (insn))
@@ -4182,6 +3979,9 @@ update_flow_info (notes, first, last, orig_insn)
          break;
 
        case REG_INC:
+         /* reload sometimes leaves obsolete REG_INC notes around.  */
+         if (reload_completed)
+           break;
          /* This should be moved to whichever instruction now has the
             increment operation.  */
          abort ();
@@ -4191,8 +3991,9 @@ update_flow_info (notes, first, last, orig_insn)
          for (insn = first; insn != NEXT_INSN (last); insn = NEXT_INSN (insn))
            if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
                && reg_mentioned_p (XEXP (note, 0), PATTERN (insn)))
-             REG_NOTES (insn) = gen_rtx (EXPR_LIST, REG_LABEL,
-                                         XEXP (note, 0), REG_NOTES (insn));
+             REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_LABEL,
+                                                   XEXP (note, 0),
+                                                   REG_NOTES (insn));
          break;
 
        case REG_CC_SETTER:
@@ -4232,10 +4033,7 @@ update_flow_info (notes, first, last, orig_insn)
   /* If any insn, except the last, uses the register set by the last insn,
      then we need a new REG_DEAD note on that insn.  In this case, there
      would not have been a REG_DEAD note for this register in the original
-     insn because it was used and set within one insn.
-
-     There is no new REG_DEAD note needed if the last insn uses the register
-     that it is setting.  */
+     insn because it was used and set within one insn.  */
 
   set = single_set (last);
   if (set)
@@ -4248,9 +4046,34 @@ update_flow_info (notes, first, last, orig_insn)
        dest = XEXP (dest, 0);
 
       if (GET_CODE (dest) == REG
-         && ! reg_overlap_mentioned_p (dest, SET_SRC (set)))
+         /* Global registers are always live, so the code below does not
+            apply to them.  */
+         && (REGNO (dest) >= FIRST_PSEUDO_REGISTER
+             || ! global_regs[REGNO (dest)]))
        {
-         for (insn = PREV_INSN (last); ; insn = PREV_INSN (insn))
+         rtx stop_insn = PREV_INSN (first);
+
+         /* If the last insn uses the register that it is setting, then
+            we don't want to put a REG_DEAD note there.  Search backwards
+            to find the first insn that sets but does not use DEST.  */
+
+         insn = last;
+         if (reg_overlap_mentioned_p (dest, SET_SRC (set)))
+           {
+             for (insn = PREV_INSN (insn); insn != first;
+                  insn = PREV_INSN (insn))
+               {
+                 if ((set = single_set (insn))
+                     && reg_mentioned_p (dest, SET_DEST (set))
+                     && ! reg_overlap_mentioned_p (dest, SET_SRC (set)))
+                   break;
+               }
+           }
+
+         /* Now find the first insn that uses but does not set DEST.  */
+
+         for (insn = PREV_INSN (insn); insn != stop_insn;
+              insn = PREV_INSN (insn))
            {
              if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
                  && reg_mentioned_p (dest, PATTERN (insn))
@@ -4276,8 +4099,6 @@ update_flow_info (notes, first, last, orig_insn)
                      break;
                    }
                }
-             if (insn == first)
-               break;
            }
        }
     }
@@ -4301,21 +4122,29 @@ update_flow_info (notes, first, last, orig_insn)
 
       for (insn = first; ; insn = NEXT_INSN (insn))
        {
-         set = single_set (insn);
-         if (set)
+         rtx pat = PATTERN (insn);
+         int i = GET_CODE (pat) == PARALLEL ? XVECLEN (pat, 0) : 0;
+         set = pat;
+         for (;;)
            {
-             if (GET_CODE (SET_DEST (set)) == REG
-                 && REGNO (SET_DEST (set)) == REGNO (orig_dest))
-               {
-                 found_orig_dest = 1;
-                 break;
-               }
-             else if (GET_CODE (SET_DEST (set)) == SUBREG
-                      && SUBREG_REG (SET_DEST (set)) == orig_dest)
+             if (GET_CODE (set) == SET)
                {
-                 found_split_dest = 1;
-                 break;
+                 if (GET_CODE (SET_DEST (set)) == REG
+                     && REGNO (SET_DEST (set)) == REGNO (orig_dest))
+                   {
+                     found_orig_dest = 1;
+                     break;
+                   }
+                 else if (GET_CODE (SET_DEST (set)) == SUBREG
+                          && SUBREG_REG (SET_DEST (set)) == orig_dest)
+                   {
+                     found_split_dest = 1;
+                     break;
+                   }
                }
+             if (--i < 0)
+               break;
+             set = XVECEXP (pat, 0, i);
            }
 
          if (insn == last)
@@ -4345,8 +4174,28 @@ update_flow_info (notes, first, last, orig_insn)
        }
       else if (! found_orig_dest)
        {
-         /* This should never happen.  */
-         abort ();
+         int i, regno;
+
+         /* Should never reach here for a pseudo reg.  */
+         if (REGNO (orig_dest) >= FIRST_PSEUDO_REGISTER)
+           abort ();
+
+         /* This can happen for a hard register, if the splitter
+            does not bother to emit instructions which would be no-ops.
+            We try to verify that this is the case by checking to see if
+            the original instruction uses all of the registers that it
+            set.  This case is OK, because deleting a no-op can not affect
+            REG_DEAD notes on other insns.  If this is not the case, then
+            abort.  */
+         
+         regno = REGNO (orig_dest);
+         for (i = HARD_REGNO_NREGS (regno, GET_MODE (orig_dest)) - 1;
+              i >= 0; i--)
+           if (! refers_to_regno_p (regno + i, regno + i + 1, orig_insn,
+                                    NULL_PTR))
+             break;
+         if (i >= 0)
+           abort ();
        }
     }
 
@@ -4403,7 +4252,7 @@ schedule_insns (dump_file)
      FILE *dump_file;
 {
   int max_uid = MAX_INSNS_PER_SPLIT * (get_max_uid () + 1);
-  int i, b;
+  int b;
   rtx insn;
 
   /* Taking care of this degenerate case makes the rest of
@@ -4413,8 +4262,8 @@ schedule_insns (dump_file)
 
   /* Create an insn here so that we can hang dependencies off of it later.  */
   sched_before_next_call
-    = gen_rtx (INSN, VOIDmode, 0, NULL_RTX, NULL_RTX,
-              NULL_RTX, 0, NULL_RTX, 0);
+    = gen_rtx_INSN (VOIDmode, 0, NULL_RTX, NULL_RTX,
+                   NULL_RTX, 0, NULL_RTX, NULL_RTX);
 
   /* Initialize the unused_*_lists.  We can't use the ones left over from
      the previous function, because gcc has freed that memory.  We can use
@@ -4432,46 +4281,45 @@ schedule_insns (dump_file)
      remember how far we can cut back the stack on exit.  */
 
   /* Allocate data for this pass.  See comments, above,
-     for what these vectors do.  */
-  insn_luid = (int *) alloca (max_uid * sizeof (int));
-  insn_priority = (int *) alloca (max_uid * sizeof (int));
-  insn_tick = (int *) alloca (max_uid * sizeof (int));
-  insn_costs = (short *) alloca (max_uid * sizeof (short));
-  insn_units = (short *) alloca (max_uid * sizeof (short));
-  insn_blockage = (unsigned int *) alloca (max_uid * sizeof (unsigned int));
-  insn_ref_count = (int *) alloca (max_uid * sizeof (int));
+     for what these vectors do.
+
+     We use xmalloc instead of alloca, because max_uid can be very large
+     when there is a lot of function inlining.  If we used alloca, we could
+     exceed stack limits on some hosts for some inputs.  */
+  insn_luid = (int *) xmalloc (max_uid * sizeof (int));
+  insn_priority = (int *) xmalloc (max_uid * sizeof (int));
+  insn_tick = (int *) xmalloc (max_uid * sizeof (int));
+  insn_costs = (short *) xmalloc (max_uid * sizeof (short));
+  insn_units = (short *) xmalloc (max_uid * sizeof (short));
+  insn_blockage = (unsigned int *) xmalloc (max_uid * sizeof (unsigned int));
+  insn_ref_count = (int *) xmalloc (max_uid * sizeof (int));
 
   if (reload_completed == 0)
     {
-      sched_reg_n_deaths = (short *) alloca (max_regno * sizeof (short));
       sched_reg_n_calls_crossed = (int *) alloca (max_regno * sizeof (int));
       sched_reg_live_length = (int *) alloca (max_regno * sizeof (int));
-      bb_dead_regs = (regset) alloca (regset_bytes);
-      bb_live_regs = (regset) alloca (regset_bytes);
-      bzero (sched_reg_n_calls_crossed, max_regno * sizeof (int));
-      bzero (sched_reg_live_length, max_regno * sizeof (int));
-      bcopy (reg_n_deaths, sched_reg_n_deaths, max_regno * sizeof (short));
-      init_alias_analysis ();
+      bb_dead_regs = ALLOCA_REG_SET ();
+      bb_live_regs = ALLOCA_REG_SET ();
+      bzero ((char *) sched_reg_n_calls_crossed, max_regno * sizeof (int));
+      bzero ((char *) sched_reg_live_length, max_regno * sizeof (int));
     }
   else
     {
-      sched_reg_n_deaths = 0;
       sched_reg_n_calls_crossed = 0;
       sched_reg_live_length = 0;
       bb_dead_regs = 0;
       bb_live_regs = 0;
-      if (! flag_schedule_insns)
-       init_alias_analysis ();
     }
+  init_alias_analysis ();
 
   if (write_symbols != NO_DEBUG)
     {
       rtx line;
 
-      line_note = (rtx *) alloca (max_uid * sizeof (rtx));
-      bzero (line_note, max_uid * sizeof (rtx));
+      line_note = (rtx *) xmalloc (max_uid * sizeof (rtx));
+      bzero ((char *) line_note, max_uid * sizeof (rtx));
       line_note_head = (rtx *) alloca (n_basic_blocks * sizeof (rtx));
-      bzero (line_note_head, n_basic_blocks * sizeof (rtx));
+      bzero ((char *) line_note_head, n_basic_blocks * sizeof (rtx));
 
       /* Determine the line-number at the start of each basic block.
         This must be computed and saved now, because after a basic block's
@@ -4487,18 +4335,20 @@ schedule_insns (dump_file)
            }
     }
 
-  bzero (insn_luid, max_uid * sizeof (int));
-  bzero (insn_priority, max_uid * sizeof (int));
-  bzero (insn_tick, max_uid * sizeof (int));
-  bzero (insn_costs, max_uid * sizeof (short));
-  bzero (insn_units, max_uid * sizeof (short));
-  bzero (insn_blockage, max_uid * sizeof (unsigned int));
-  bzero (insn_ref_count, max_uid * sizeof (int));
+  bzero ((char *) insn_luid, max_uid * sizeof (int));
+  bzero ((char *) insn_priority, max_uid * sizeof (int));
+  bzero ((char *) insn_tick, max_uid * sizeof (int));
+  bzero ((char *) insn_costs, max_uid * sizeof (short));
+  bzero ((char *) insn_units, max_uid * sizeof (short));
+  bzero ((char *) insn_blockage, max_uid * sizeof (unsigned int));
+  bzero ((char *) insn_ref_count, max_uid * sizeof (int));
 
   /* Schedule each basic block, block by block.  */
 
   /* ??? Add a NOTE after the last insn of the last basic block.  It is not
      known why this is done.  */
+  /* ??? Perhaps it's done to ensure NEXT_TAIL in schedule_block is a
+     valid insn.  */
 
   insn = basic_block_end[n_basic_blocks-1];
   if (NEXT_INSN (insn) == 0
@@ -4513,7 +4363,6 @@ schedule_insns (dump_file)
   for (b = 0; b < n_basic_blocks; b++)
     {
       rtx insn, next;
-      rtx insns;
 
       note_list = 0;
 
@@ -4558,7 +4407,10 @@ schedule_insns (dump_file)
 
          /* Split insns here to get max fine-grain parallelism.  */
          prev = PREV_INSN (insn);
-         if (reload_completed == 0)
+         /* It is probably not worthwhile to try to split again in the
+            second pass.  However, if flag_schedule_insns is not set,
+            the first and only (if any) scheduling pass is after reload.  */
+         if (reload_completed == 0 || ! flag_schedule_insns)
            {
              rtx last, first = PREV_INSN (insn);
              rtx notes = REG_NOTES (insn);
@@ -4652,35 +4504,71 @@ schedule_insns (dump_file)
          {
            if (dump_file)
              {
-               if (reg_live_length[regno] > sched_reg_live_length[regno])
+               if (REG_LIVE_LENGTH (regno) > sched_reg_live_length[regno])
                  fprintf (dump_file,
                           ";; register %d life shortened from %d to %d\n",
-                          regno, reg_live_length[regno],
+                          regno, REG_LIVE_LENGTH (regno),
                           sched_reg_live_length[regno]);
                /* Negative values are special; don't overwrite the current
                   reg_live_length value if it is negative.  */
-               else if (reg_live_length[regno] < sched_reg_live_length[regno]
-                        && reg_live_length[regno] >= 0)
+               else if (REG_LIVE_LENGTH (regno) < sched_reg_live_length[regno]
+                        && REG_LIVE_LENGTH (regno) >= 0)
                  fprintf (dump_file,
                           ";; register %d life extended from %d to %d\n",
-                          regno, reg_live_length[regno],
+                          regno, REG_LIVE_LENGTH (regno),
                           sched_reg_live_length[regno]);
 
-               if (reg_n_calls_crossed[regno]
-                   && ! sched_reg_n_calls_crossed[regno])
-                 fprintf (dump_file,
-                          ";; register %d no longer crosses calls\n", regno);
-               else if (! reg_n_calls_crossed[regno]
-                        && sched_reg_n_calls_crossed[regno])
+               if (! REG_N_CALLS_CROSSED (regno)
+                   && sched_reg_n_calls_crossed[regno])
                  fprintf (dump_file,
                           ";; register %d now crosses calls\n", regno);
+               else if (REG_N_CALLS_CROSSED (regno)
+                        && ! sched_reg_n_calls_crossed[regno]
+                        && REG_BASIC_BLOCK (regno) != REG_BLOCK_GLOBAL)
+                 fprintf (dump_file,
+                          ";; register %d no longer crosses calls\n", regno);
+
              }
            /* Negative values are special; don't overwrite the current
               reg_live_length value if it is negative.  */
-           if (reg_live_length[regno] >= 0)
-             reg_live_length[regno] = sched_reg_live_length[regno];
-           reg_n_calls_crossed[regno] = sched_reg_n_calls_crossed[regno];
+           if (REG_LIVE_LENGTH (regno) >= 0)
+             REG_LIVE_LENGTH (regno) = sched_reg_live_length[regno];
+
+           /* We can't change the value of reg_n_calls_crossed to zero for
+              pseudos which are live in more than one block.
+
+              This is because combine might have made an optimization which
+              invalidated basic_block_live_at_start and reg_n_calls_crossed,
+              but it does not update them.  If we update reg_n_calls_crossed
+              here, the two variables are now inconsistent, and this might
+              confuse the caller-save code into saving a register that doesn't
+              need to be saved.  This is only a problem when we zero calls
+              crossed for a pseudo live in multiple basic blocks.
+
+              Alternatively, we could try to correctly update basic block live
+              at start here in sched, but that seems complicated.  */
+           if (sched_reg_n_calls_crossed[regno]
+               || REG_BASIC_BLOCK (regno) != REG_BLOCK_GLOBAL)
+             REG_N_CALLS_CROSSED (regno) = sched_reg_n_calls_crossed[regno];
          }
     }
+
+  free (insn_luid);
+  free (insn_priority);
+  free (insn_tick);
+  free (insn_costs);
+  free (insn_units);
+  free (insn_blockage);
+  free (insn_ref_count);
+
+  if (write_symbols != NO_DEBUG)
+    free (line_note);
+
+  if (reload_completed == 0)
+    {
+      FREE_REG_SET (bb_dead_regs);
+      FREE_REG_SET (bb_live_regs);
+    }
+
 }
 #endif /* INSN_SCHEDULING */