OSDN Git Service

In libobjc/:
[pf3gnuchains/gcc-fork.git] / gcc / combine.c
index 6b507c2..bed5768 100644 (file)
@@ -1,6 +1,6 @@
 /* Optimize by combining instructions for GNU compiler.
    Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
-   1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
+   1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
    Free Software Foundation, Inc.
 
 This file is part of GCC.
@@ -92,7 +92,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "expr.h"
 #include "insn-attr.h"
 #include "recog.h"
-#include "real.h"
+#include "diagnostic-core.h"
 #include "toplev.h"
 #include "target.h"
 #include "optabs.h"
@@ -321,7 +321,7 @@ static rtx *uid_log_links;
 
 static int label_tick;
 
-/* Reset to label_tick for each label.  */
+/* Reset to label_tick for each extended basic block in scanning order.  */
 
 static int label_tick_ebb_start;
 
@@ -385,13 +385,13 @@ static void init_reg_last (void);
 static void setup_incoming_promotions (rtx);
 static void set_nonzero_bits_and_sign_copies (rtx, const_rtx, void *);
 static int cant_combine_insn_p (rtx);
-static int can_combine_p (rtx, rtx, rtx, rtx, rtx *, rtx *);
-static int combinable_i3pat (rtx, rtx *, rtx, rtx, int, rtx *);
+static int can_combine_p (rtx, rtx, rtx, rtx, rtx, rtx, rtx *, rtx *);
+static int combinable_i3pat (rtx, rtx *, rtx, rtx, rtx, int, int, rtx *);
 static int contains_muldiv (rtx);
-static rtx try_combine (rtx, rtx, rtx, int *);
+static rtx try_combine (rtx, rtx, rtx, rtx, int *);
 static void undo_all (void);
 static void undo_commit (void);
-static rtx *find_split_point (rtx *, rtx);
+static rtx *find_split_point (rtx *, rtx, bool);
 static rtx subst (rtx, rtx, rtx, int, int);
 static rtx combine_simplify_rtx (rtx, enum machine_mode, int);
 static rtx simplify_if_then_else (rtx);
@@ -438,7 +438,7 @@ static void reg_dead_at_p_1 (rtx, const_rtx, void *);
 static int reg_dead_at_p (rtx, rtx);
 static void move_deaths (rtx, rtx, int, rtx, rtx *);
 static int reg_bitfield_target_p (rtx, rtx);
-static void distribute_notes (rtx, rtx, rtx, rtx, rtx, rtx);
+static void distribute_notes (rtx, rtx, rtx, rtx, rtx, rtx, rtx);
 static void distribute_links (rtx);
 static void mark_used_regs_combine (rtx);
 static void record_promoted_value (rtx, rtx);
@@ -766,18 +766,18 @@ do_SUBST_MODE (rtx *into, enum machine_mode newval)
 \f
 /* Subroutine of try_combine.  Determine whether the combine replacement
    patterns NEWPAT, NEWI2PAT and NEWOTHERPAT are cheaper according to
-   insn_rtx_cost that the original instruction sequence I1, I2, I3 and
-   undobuf.other_insn.  Note that I1 and/or NEWI2PAT may be NULL_RTX. 
+   insn_rtx_cost that the original instruction sequence I0, I1, I2, I3 and
+   undobuf.other_insn.  Note that I1 and/or NEWI2PAT may be NULL_RTX.
    NEWOTHERPAT and undobuf.other_insn may also both be NULL_RTX.  This
    function returns false, if the costs of all instructions can be
    estimated, and the replacements are more expensive than the original
    sequence.  */
 
 static bool
-combine_validate_cost (rtx i1, rtx i2, rtx i3, rtx newpat, rtx newi2pat,
-                      rtx newotherpat)
+combine_validate_cost (rtx i0, rtx i1, rtx i2, rtx i3, rtx newpat,
+                      rtx newi2pat, rtx newotherpat)
 {
-  int i1_cost, i2_cost, i3_cost;
+  int i0_cost, i1_cost, i2_cost, i3_cost;
   int new_i2_cost, new_i3_cost;
   int old_cost, new_cost;
 
@@ -788,13 +788,23 @@ combine_validate_cost (rtx i1, rtx i2, rtx i3, rtx newpat, rtx newi2pat,
   if (i1)
     {
       i1_cost = INSN_COST (i1);
-      old_cost = (i1_cost > 0 && i2_cost > 0 && i3_cost > 0)
-                ? i1_cost + i2_cost + i3_cost : 0;
+      if (i0)
+       {
+         i0_cost = INSN_COST (i0);
+         old_cost = (i0_cost > 0 && i1_cost > 0 && i2_cost > 0 && i3_cost > 0
+                     ? i0_cost + i1_cost + i2_cost + i3_cost : 0);
+       }
+      else
+       {
+         old_cost = (i1_cost > 0 && i2_cost > 0 && i3_cost > 0
+                     ? i1_cost + i2_cost + i3_cost : 0);
+         i0_cost = 0;
+       }
     }
   else
     {
       old_cost = (i2_cost > 0 && i3_cost > 0) ? i2_cost + i3_cost : 0;
-      i1_cost = 0;
+      i1_cost = i0_cost = 0;
     }
 
   /* Calculate the replacement insn_rtx_costs.  */
@@ -833,7 +843,16 @@ combine_validate_cost (rtx i1, rtx i2, rtx i3, rtx newpat, rtx newi2pat,
     {
       if (dump_file)
        {
-         if (i1)
+         if (i0)
+           {
+             fprintf (dump_file,
+                      "rejecting combination of insns %d, %d, %d and %d\n",
+                      INSN_UID (i0), INSN_UID (i1), INSN_UID (i2),
+                      INSN_UID (i3));
+             fprintf (dump_file, "original costs %d + %d + %d + %d = %d\n",
+                      i0_cost, i1_cost, i2_cost, i3_cost, old_cost);
+           }
+         else if (i1)
            {
              fprintf (dump_file,
                       "rejecting combination of insns %d, %d and %d\n",
@@ -912,7 +931,7 @@ create_log_links (void)
      register and establishing log links when def is encountered.
      Note that we do not clear next_use array in order to save time,
      so we have to test whether the use is in the same basic block as def.
-              
+
      There are a few cases below when we do not consider the definition or
      usage -- these are taken from original flow.c did. Don't ask me why it is
      done this way; I don't know and if it works, I don't want to know.  */
@@ -1011,8 +1030,20 @@ clear_log_links (void)
       free_INSN_LIST_list (&LOG_LINKS (insn));
 }
 
+/* Walk the LOG_LINKS of insn B to see if we find a reference to A.  Return
+   true if we found a LOG_LINK that proves that A feeds B.  This only works
+   if there are no instructions between A and B which could have a link
+   depending on A, since in that case we would not record a link for B.  */
 
-
+static bool
+insn_a_feeds_b (rtx a, rtx b)
+{
+  rtx links;
+  for (links = LOG_LINKS (b); links; links = XEXP (links, 1))
+    if (XEXP (links, 0) == a)
+      return true;
+  return false;
+}
 \f
 /* Main entry point for combiner.  F is the first insn of the function.
    NREGS is the first unused pseudo-reg number.
@@ -1028,6 +1059,7 @@ combine_instructions (rtx f, unsigned int nregs)
 #endif
   rtx links, nextlinks;
   rtx first;
+  basic_block last_bb;
 
   int new_direct_jump_p = 0;
 
@@ -1058,6 +1090,7 @@ combine_instructions (rtx f, unsigned int nregs)
      problems when, for example, we have j <<= 1 in a loop.  */
 
   nonzero_sign_valid = 0;
+  label_tick = label_tick_ebb_start = 1;
 
   /* Scan all SETs and see if we can deduce anything about what
      bits are known to be zero for some registers and how many copies
@@ -1067,18 +1100,23 @@ combine_instructions (rtx f, unsigned int nregs)
      for what bits are known to be set.  */
 
   setup_incoming_promotions (first);
+  /* Allow the entry block and the first block to fall into the same EBB.
+     Conceptually the incoming promotions are assigned to the entry block.  */
+  last_bb = ENTRY_BLOCK_PTR;
 
   create_log_links ();
-  label_tick_ebb_start = ENTRY_BLOCK_PTR->index;
   FOR_EACH_BB (this_basic_block)
     {
       optimize_this_for_speed_p = optimize_bb_for_speed_p (this_basic_block);
       last_call_luid = 0;
       mem_last_set = -1;
-      label_tick = this_basic_block->index;
+
+      label_tick++;
       if (!single_pred_p (this_basic_block)
-         || single_pred (this_basic_block)->index != label_tick - 1)
+         || single_pred (this_basic_block) != last_bb)
        label_tick_ebb_start = label_tick;
+      last_bb = this_basic_block;
+
       FOR_BB_INSNS (this_basic_block, insn)
         if (INSN_P (insn) && BLOCK_FOR_INSN (insn))
          {
@@ -1109,20 +1147,23 @@ combine_instructions (rtx f, unsigned int nregs)
   nonzero_sign_valid = 1;
 
   /* Now scan all the insns in forward order.  */
-
-  label_tick_ebb_start = ENTRY_BLOCK_PTR->index;
+  label_tick = label_tick_ebb_start = 1;
   init_reg_last ();
   setup_incoming_promotions (first);
+  last_bb = ENTRY_BLOCK_PTR;
 
   FOR_EACH_BB (this_basic_block)
     {
       optimize_this_for_speed_p = optimize_bb_for_speed_p (this_basic_block);
       last_call_luid = 0;
       mem_last_set = -1;
-      label_tick = this_basic_block->index;
+
+      label_tick++;
       if (!single_pred_p (this_basic_block)
-         || single_pred (this_basic_block)->index != label_tick - 1)
+         || single_pred (this_basic_block) != last_bb)
        label_tick_ebb_start = label_tick;
+      last_bb = this_basic_block;
+
       rtl_profile_for_bb (this_basic_block);
       for (insn = BB_HEAD (this_basic_block);
           insn != NEXT_INSN (BB_END (this_basic_block));
@@ -1143,7 +1184,7 @@ combine_instructions (rtx f, unsigned int nregs)
              /* Try this insn with each insn it links back to.  */
 
              for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
-               if ((next = try_combine (insn, XEXP (links, 0),
+               if ((next = try_combine (insn, XEXP (links, 0), NULL_RTX,
                                         NULL_RTX, &new_direct_jump_p)) != 0)
                  goto retry;
 
@@ -1161,8 +1202,8 @@ combine_instructions (rtx f, unsigned int nregs)
                  for (nextlinks = LOG_LINKS (link);
                       nextlinks;
                       nextlinks = XEXP (nextlinks, 1))
-                   if ((next = try_combine (insn, link,
-                                            XEXP (nextlinks, 0),
+                   if ((next = try_combine (insn, link, XEXP (nextlinks, 0),
+                                            NULL_RTX,
                                             &new_direct_jump_p)) != 0)
                      goto retry;
                }
@@ -1180,14 +1221,14 @@ combine_instructions (rtx f, unsigned int nregs)
                  && NONJUMP_INSN_P (prev)
                  && sets_cc0_p (PATTERN (prev)))
                {
-                 if ((next = try_combine (insn, prev,
-                                          NULL_RTX, &new_direct_jump_p)) != 0)
+                 if ((next = try_combine (insn, prev, NULL_RTX, NULL_RTX,
+                                          &new_direct_jump_p)) != 0)
                    goto retry;
 
                  for (nextlinks = LOG_LINKS (prev); nextlinks;
                       nextlinks = XEXP (nextlinks, 1))
-                   if ((next = try_combine (insn, prev,
-                                            XEXP (nextlinks, 0),
+                   if ((next = try_combine (insn, prev, XEXP (nextlinks, 0),
+                                            NULL_RTX,
                                             &new_direct_jump_p)) != 0)
                      goto retry;
                }
@@ -1200,14 +1241,14 @@ combine_instructions (rtx f, unsigned int nregs)
                  && GET_CODE (PATTERN (insn)) == SET
                  && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (insn))))
                {
-                 if ((next = try_combine (insn, prev,
-                                          NULL_RTX, &new_direct_jump_p)) != 0)
+                 if ((next = try_combine (insn, prev, NULL_RTX, NULL_RTX,
+                                          &new_direct_jump_p)) != 0)
                    goto retry;
 
                  for (nextlinks = LOG_LINKS (prev); nextlinks;
                       nextlinks = XEXP (nextlinks, 1))
-                   if ((next = try_combine (insn, prev,
-                                            XEXP (nextlinks, 0),
+                   if ((next = try_combine (insn, prev, XEXP (nextlinks, 0),
+                                            NULL_RTX,
                                             &new_direct_jump_p)) != 0)
                      goto retry;
                }
@@ -1223,7 +1264,8 @@ combine_instructions (rtx f, unsigned int nregs)
                    && NONJUMP_INSN_P (prev)
                    && sets_cc0_p (PATTERN (prev))
                    && (next = try_combine (insn, XEXP (links, 0),
-                                           prev, &new_direct_jump_p)) != 0)
+                                           prev, NULL_RTX,
+                                           &new_direct_jump_p)) != 0)
                  goto retry;
 #endif
 
@@ -1233,10 +1275,64 @@ combine_instructions (rtx f, unsigned int nregs)
                for (nextlinks = XEXP (links, 1); nextlinks;
                     nextlinks = XEXP (nextlinks, 1))
                  if ((next = try_combine (insn, XEXP (links, 0),
-                                          XEXP (nextlinks, 0),
+                                          XEXP (nextlinks, 0), NULL_RTX,
                                           &new_direct_jump_p)) != 0)
                    goto retry;
 
+             /* Try four-instruction combinations.  */
+             for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
+               {
+                 rtx next1;
+                 rtx link = XEXP (links, 0);
+
+                 /* If the linked insn has been replaced by a note, then there
+                    is no point in pursuing this chain any further.  */
+                 if (NOTE_P (link))
+                   continue;
+
+                 for (next1 = LOG_LINKS (link); next1; next1 = XEXP (next1, 1))
+                   {
+                     rtx link1 = XEXP (next1, 0);
+                     if (NOTE_P (link1))
+                       continue;
+                     /* I0 -> I1 -> I2 -> I3.  */
+                     for (nextlinks = LOG_LINKS (link1); nextlinks;
+                          nextlinks = XEXP (nextlinks, 1))
+                       if ((next = try_combine (insn, link, link1,
+                                                XEXP (nextlinks, 0),
+                                                &new_direct_jump_p)) != 0)
+                         goto retry;
+                     /* I0, I1 -> I2, I2 -> I3.  */
+                     for (nextlinks = XEXP (next1, 1); nextlinks;
+                          nextlinks = XEXP (nextlinks, 1))
+                       if ((next = try_combine (insn, link, link1,
+                                                XEXP (nextlinks, 0),
+                                                &new_direct_jump_p)) != 0)
+                         goto retry;
+                   }
+
+                 for (next1 = XEXP (links, 1); next1; next1 = XEXP (next1, 1))
+                   {
+                     rtx link1 = XEXP (next1, 0);
+                     if (NOTE_P (link1))
+                       continue;
+                     /* I0 -> I2; I1, I2 -> I3.  */
+                     for (nextlinks = LOG_LINKS (link); nextlinks;
+                          nextlinks = XEXP (nextlinks, 1))
+                       if ((next = try_combine (insn, link, link1,
+                                                XEXP (nextlinks, 0),
+                                                &new_direct_jump_p)) != 0)
+                         goto retry;
+                     /* I0 -> I1; I1, I2 -> I3.  */
+                     for (nextlinks = LOG_LINKS (link1); nextlinks;
+                          nextlinks = XEXP (nextlinks, 1))
+                       if ((next = try_combine (insn, link, link1,
+                                                XEXP (nextlinks, 0),
+                                                &new_direct_jump_p)) != 0)
+                         goto retry;
+                   }
+               }
+
              /* Try this insn with each REG_EQUAL note it links back to.  */
              for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
                {
@@ -1260,7 +1356,7 @@ combine_instructions (rtx f, unsigned int nregs)
                      i2mod = temp;
                      i2mod_old_rhs = copy_rtx (orig);
                      i2mod_new_rhs = copy_rtx (note);
-                     next = try_combine (insn, i2mod, NULL_RTX,
+                     next = try_combine (insn, i2mod, NULL_RTX, NULL_RTX,
                                          &new_direct_jump_p);
                      i2mod = NULL_RTX;
                      if (next)
@@ -1321,7 +1417,7 @@ init_reg_last (void)
   unsigned int i;
   reg_stat_type *p;
 
-  for (i = 0; VEC_iterate (reg_stat_type, reg_stat, i, p); ++i)
+  FOR_EACH_VEC_ELT (reg_stat_type, reg_stat, i, p)
     memset (p, 0, offsetof (reg_stat_type, sign_bit_copies));
 }
 \f
@@ -1334,9 +1430,9 @@ setup_incoming_promotions (rtx first)
   bool strictly_local = false;
 
   for (arg = DECL_ARGUMENTS (current_function_decl); arg;
-       arg = TREE_CHAIN (arg))
+       arg = DECL_CHAIN (arg))
     {
-      rtx reg = DECL_INCOMING_RTL (arg);
+      rtx x, reg = DECL_INCOMING_RTL (arg);
       int uns1, uns3;
       enum machine_mode mode1, mode2, mode3, mode4;
 
@@ -1360,7 +1456,7 @@ setup_incoming_promotions (rtx first)
       mode2 = TYPE_MODE (DECL_ARG_TYPE (arg));
       uns3 = TYPE_UNSIGNED (DECL_ARG_TYPE (arg));
 
-      /* The mode and signedness of the argument as it is actually passed, 
+      /* The mode and signedness of the argument as it is actually passed,
          after any TARGET_PROMOTE_FUNCTION_ARGS-driven ABI promotions.  */
       mode3 = promote_function_mode (DECL_ARG_TYPE (arg), mode2, &uns3,
                                     TREE_TYPE (cfun->decl), 0);
@@ -1368,30 +1464,38 @@ setup_incoming_promotions (rtx first)
       /* The mode of the register in which the argument is being passed.  */
       mode4 = GET_MODE (reg);
 
-      /* Eliminate sign extensions in the callee when possible.  Only
-         do this when:
-        (a) a mode promotion has occurred;
-        (b) the mode of the register is the same as the mode of
-            the argument as it is passed; and
-        (c) the signedness does not change across any of the promotions; and
-        (d) when no language-level promotions (which we cannot guarantee
-            will have been done by an external caller) are necessary,
-            unless we know that this function is only ever called from
-            the current compilation unit -- all of whose call sites will
-            do the mode1 --> mode2 promotion.  */
-      if (mode1 != mode3
-          && mode3 == mode4
-          && uns1 == uns3
-         && (mode1 == mode2 || strictly_local))
-        {
-         /* Record that the value was promoted from mode1 to mode3,
-            so that any sign extension at the head of the current
-            function may be eliminated.  */
-         rtx x;
-         x = gen_rtx_CLOBBER (mode1, const0_rtx);
-         x = gen_rtx_fmt_e ((uns3 ? ZERO_EXTEND : SIGN_EXTEND), mode3, x);
-         record_value_for_reg (reg, first, x);
-       }
+      /* Eliminate sign extensions in the callee when:
+        (a) A mode promotion has occurred;  */
+      if (mode1 == mode3)
+       continue;
+      /* (b) The mode of the register is the same as the mode of
+            the argument as it is passed; */
+      if (mode3 != mode4)
+       continue;
+      /* (c) There's no language level extension;  */
+      if (mode1 == mode2)
+       ;
+      /* (c.1) All callers are from the current compilation unit.  If that's
+        the case we don't have to rely on an ABI, we only have to know
+        what we're generating right now, and we know that we will do the
+        mode1 to mode2 promotion with the given sign.  */
+      else if (!strictly_local)
+       continue;
+      /* (c.2) The combination of the two promotions is useful.  This is
+        true when the signs match, or if the first promotion is unsigned.
+        In the later case, (sign_extend (zero_extend x)) is the same as
+        (zero_extend (zero_extend x)), so make sure to force UNS3 true.  */
+      else if (uns1)
+       uns3 = true;
+      else if (uns3)
+       continue;
+
+      /* Record that the value was promoted from mode1 to mode3,
+        so that any sign extension at the head of the current
+        function may be eliminated.  */
+      x = gen_rtx_CLOBBER (mode1, const0_rtx);
+      x = gen_rtx_fmt_e ((uns3 ? ZERO_EXTEND : SIGN_EXTEND), mode3, x);
+      record_value_for_reg (reg, first, x);
     }
 }
 
@@ -1514,9 +1618,10 @@ set_nonzero_bits_and_sign_copies (rtx x, const_rtx set, void *data)
     }
 }
 \f
-/* See if INSN can be combined into I3.  PRED and SUCC are optionally
-   insns that were previously combined into I3 or that will be combined
-   into the merger of INSN and I3.
+/* See if INSN can be combined into I3.  PRED, PRED2, SUCC and SUCC2 are
+   optionally insns that were previously combined into I3 or that will be
+   combined into the merger of INSN and I3.  The order is PRED, PRED2,
+   INSN, SUCC, SUCC2, I3.
 
    Return 0 if the combination is not allowed for any reason.
 
@@ -1525,7 +1630,8 @@ set_nonzero_bits_and_sign_copies (rtx x, const_rtx set, void *data)
    will return 1.  */
 
 static int
-can_combine_p (rtx insn, rtx i3, rtx pred ATTRIBUTE_UNUSED, rtx succ,
+can_combine_p (rtx insn, rtx i3, rtx pred ATTRIBUTE_UNUSED,
+              rtx pred2 ATTRIBUTE_UNUSED, rtx succ, rtx succ2,
               rtx *pdest, rtx *psrc)
 {
   int i;
@@ -1535,10 +1641,25 @@ can_combine_p (rtx insn, rtx i3, rtx pred ATTRIBUTE_UNUSED, rtx succ,
 #ifdef AUTO_INC_DEC
   rtx link;
 #endif
-  int all_adjacent = (succ ? (next_active_insn (insn) == succ
-                             && next_active_insn (succ) == i3)
-                     : next_active_insn (insn) == i3);
+  bool all_adjacent = true;
 
+  if (succ)
+    {
+      if (succ2)
+       {
+         if (next_active_insn (succ2) != i3)
+           all_adjacent = false;
+         if (next_active_insn (succ) != succ2)
+           all_adjacent = false;
+       }
+      else if (next_active_insn (succ) != i3)
+       all_adjacent = false;
+      if (next_active_insn (insn) != succ)
+       all_adjacent = false;
+    }
+  else if (next_active_insn (insn) != i3)
+    all_adjacent = false;
+    
   /* Can combine only if previous insn is a SET of a REG, a SUBREG or CC0.
      or a PARALLEL consisting of such a SET and CLOBBERs.
 
@@ -1663,11 +1784,15 @@ can_combine_p (rtx insn, rtx i3, rtx pred ATTRIBUTE_UNUSED, rtx succ,
       /* Don't substitute into an incremented register.  */
       || FIND_REG_INC_NOTE (i3, dest)
       || (succ && FIND_REG_INC_NOTE (succ, dest))
+      || (succ2 && FIND_REG_INC_NOTE (succ2, dest))
       /* Don't substitute into a non-local goto, this confuses CFG.  */
       || (JUMP_P (i3) && find_reg_note (i3, REG_NON_LOCAL_GOTO, NULL_RTX))
       /* Make sure that DEST is not used after SUCC but before I3.  */
-      || (succ && ! all_adjacent
-         && reg_used_between_p (dest, succ, i3))
+      || (!all_adjacent
+         && ((succ2
+              && (reg_used_between_p (dest, succ2, i3)
+                  || reg_used_between_p (dest, succ, succ2)))
+             || (!succ2 && succ && reg_used_between_p (dest, succ, i3))))
       /* Make sure that the value that is to be substituted for the register
         does not use any registers whose values alter in between.  However,
         If the insns are adjacent, a use can't cross a set even though we
@@ -1750,13 +1875,12 @@ can_combine_p (rtx insn, rtx i3, rtx pred ATTRIBUTE_UNUSED, rtx succ,
 
   if (GET_CODE (src) == ASM_OPERANDS || volatile_refs_p (src))
     {
-      /* Make sure succ doesn't contain a volatile reference.  */
+      /* Make sure neither succ nor succ2 contains a volatile reference.  */
+      if (succ2 != 0 && volatile_refs_p (PATTERN (succ2)))
+       return 0;
       if (succ != 0 && volatile_refs_p (PATTERN (succ)))
        return 0;
-
-      for (p = NEXT_INSN (insn); p != i3; p = NEXT_INSN (p))
-       if (INSN_P (p) && p != succ && volatile_refs_p (PATTERN (p)))
-         return 0;
+      /* We'll check insns between INSN and I3 below.  */
     }
 
   /* If INSN is an asm, and DEST is a hard register, reject, since it has
@@ -1770,7 +1894,7 @@ can_combine_p (rtx insn, rtx i3, rtx pred ATTRIBUTE_UNUSED, rtx succ,
      they might affect machine state.  */
 
   for (p = NEXT_INSN (insn); p != i3; p = NEXT_INSN (p))
-    if (INSN_P (p) && p != succ && volatile_insn_p (PATTERN (p)))
+    if (INSN_P (p) && p != succ && p != succ2 && volatile_insn_p (PATTERN (p)))
       return 0;
 
   /* If INSN contains an autoincrement or autodecrement, make sure that
@@ -1786,8 +1910,12 @@ can_combine_p (rtx insn, rtx i3, rtx pred ATTRIBUTE_UNUSED, rtx succ,
            || reg_used_between_p (XEXP (link, 0), insn, i3)
            || (pred != NULL_RTX
                && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (pred)))
+           || (pred2 != NULL_RTX
+               && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (pred2)))
            || (succ != NULL_RTX
                && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (succ)))
+           || (succ2 != NULL_RTX
+               && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (succ2)))
            || reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i3))))
       return 0;
 #endif
@@ -1821,8 +1949,8 @@ can_combine_p (rtx insn, rtx i3, rtx pred ATTRIBUTE_UNUSED, rtx succ,
    of a PARALLEL of the pattern.  We validate that it is valid for combining.
 
    One problem is if I3 modifies its output, as opposed to replacing it
-   entirely, we can't allow the output to contain I2DEST or I1DEST as doing
-   so would produce an insn that is not equivalent to the original insns.
+   entirely, we can't allow the output to contain I2DEST, I1DEST or I0DEST as
+   doing so would produce an insn that is not equivalent to the original insns.
 
    Consider:
 
@@ -1843,7 +1971,8 @@ can_combine_p (rtx insn, rtx i3, rtx pred ATTRIBUTE_UNUSED, rtx succ,
    must reject the combination.  This case occurs when I2 and I1 both
    feed into I3, rather than when I1 feeds into I2, which feeds into I3.
    If I1_NOT_IN_SRC is nonzero, it means that finding I1 in the source
-   of a SET must prevent combination from occurring.
+   of a SET must prevent combination from occurring.  The same situation
+   can occur for I0, in which case I0_NOT_IN_SRC is set.
 
    Before doing the above check, we first try to expand a field assignment
    into a set of logical operations.
@@ -1855,8 +1984,8 @@ can_combine_p (rtx insn, rtx i3, rtx pred ATTRIBUTE_UNUSED, rtx succ,
    Return 1 if the combination is valid, zero otherwise.  */
 
 static int
-combinable_i3pat (rtx i3, rtx *loc, rtx i2dest, rtx i1dest,
-                 int i1_not_in_src, rtx *pi3dest_killed)
+combinable_i3pat (rtx i3, rtx *loc, rtx i2dest, rtx i1dest, rtx i0dest,
+                 int i1_not_in_src, int i0_not_in_src, rtx *pi3dest_killed)
 {
   rtx x = *loc;
 
@@ -1880,9 +2009,11 @@ combinable_i3pat (rtx i3, rtx *loc, rtx i2dest, rtx i1dest,
       if ((inner_dest != dest &&
           (!MEM_P (inner_dest)
            || rtx_equal_p (i2dest, inner_dest)
-           || (i1dest && rtx_equal_p (i1dest, inner_dest)))
+           || (i1dest && rtx_equal_p (i1dest, inner_dest))
+           || (i0dest && rtx_equal_p (i0dest, inner_dest)))
           && (reg_overlap_mentioned_p (i2dest, inner_dest)
-              || (i1dest && reg_overlap_mentioned_p (i1dest, inner_dest))))
+              || (i1dest && reg_overlap_mentioned_p (i1dest, inner_dest))
+              || (i0dest && reg_overlap_mentioned_p (i0dest, inner_dest))))
 
          /* This is the same test done in can_combine_p except we can't test
             all_adjacent; we don't have to, since this instruction will stay
@@ -1898,7 +2029,8 @@ combinable_i3pat (rtx i3, rtx *loc, rtx i2dest, rtx i1dest,
              && REGNO (inner_dest) < FIRST_PSEUDO_REGISTER
              && (! HARD_REGNO_MODE_OK (REGNO (inner_dest),
                                        GET_MODE (inner_dest))))
-         || (i1_not_in_src && reg_overlap_mentioned_p (i1dest, src)))
+         || (i1_not_in_src && reg_overlap_mentioned_p (i1dest, src))
+         || (i0_not_in_src && reg_overlap_mentioned_p (i0dest, src)))
        return 0;
 
       /* If DEST is used in I3, it is being killed in this insn, so
@@ -1938,8 +2070,8 @@ combinable_i3pat (rtx i3, rtx *loc, rtx i2dest, rtx i1dest,
       int i;
 
       for (i = 0; i < XVECLEN (x, 0); i++)
-       if (! combinable_i3pat (i3, &XVECEXP (x, 0, i), i2dest, i1dest,
-                               i1_not_in_src, pi3dest_killed))
+       if (! combinable_i3pat (i3, &XVECEXP (x, 0, i), i2dest, i1dest, i0dest,
+                               i1_not_in_src, i0_not_in_src, pi3dest_killed))
          return 0;
     }
 
@@ -2005,12 +2137,12 @@ cant_combine_insn_p (rtx insn)
   if (GET_CODE (dest) == SUBREG)
     dest = SUBREG_REG (dest);
   if (REG_P (src) && REG_P (dest)
-      && ((REGNO (src) < FIRST_PSEUDO_REGISTER
-          && ! fixed_regs[REGNO (src)]
-          && CLASS_LIKELY_SPILLED_P (REGNO_REG_CLASS (REGNO (src))))
-         || (REGNO (dest) < FIRST_PSEUDO_REGISTER
-             && ! fixed_regs[REGNO (dest)]
-             && CLASS_LIKELY_SPILLED_P (REGNO_REG_CLASS (REGNO (dest))))))
+      && ((HARD_REGISTER_P (src)
+          && ! TEST_HARD_REG_BIT (fixed_reg_set, REGNO (src))
+          && targetm.class_likely_spilled_p (REGNO_REG_CLASS (REGNO (src))))
+         || (HARD_REGISTER_P (dest)
+             && ! TEST_HARD_REG_BIT (fixed_reg_set, REGNO (dest))
+             && targetm.class_likely_spilled_p (REGNO_REG_CLASS (REGNO (dest))))))
     return 1;
 
   return 0;
@@ -2061,14 +2193,14 @@ likely_spilled_retval_p (rtx insn)
   unsigned regno, nregs;
   /* We assume here that no machine mode needs more than
      32 hard registers when the value overlaps with a register
-     for which FUNCTION_VALUE_REGNO_P is true.  */
+     for which TARGET_FUNCTION_VALUE_REGNO_P is true.  */
   unsigned mask;
   struct likely_spilled_retval_info info;
 
   if (!NONJUMP_INSN_P (use) || GET_CODE (PATTERN (use)) != USE || insn == use)
     return 0;
   reg = XEXP (PATTERN (use), 0);
-  if (!REG_P (reg) || !FUNCTION_VALUE_REGNO_P (REGNO (reg)))
+  if (!REG_P (reg) || !targetm.calls.function_value_regno_p (REGNO (reg)))
     return 0;
   regno = REGNO (reg);
   nregs = hard_regno_nregs[regno][GET_MODE (reg)];
@@ -2091,7 +2223,7 @@ likely_spilled_retval_p (rtx insn)
   do
     {
       if ((mask & 1 << nregs)
-         && CLASS_LIKELY_SPILLED_P (REGNO_REG_CLASS (regno + nregs)))
+         && targetm.class_likely_spilled_p (REGNO_REG_CLASS (regno + nregs)))
        return 1;
     } while (nregs--);
   return 0;
@@ -2263,61 +2395,33 @@ cleanup_auto_inc_dec (rtx src, bool after, enum machine_mode mem_mode)
 
 struct rtx_subst_pair
 {
-  rtx from, to;
-  bool changed;
-#ifdef AUTO_INC_DEC
+  rtx to;
   bool adjusted;
   bool after;
-#endif
 };
 
-/* Clean up any auto-updates in PAIR->to the first time it is called
-   for a PAIR.  PAIR->adjusted is used to tell whether we've cleaned
-   up before.  */
+/* DATA points to an rtx_subst_pair.  Return the value that should be
+   substituted.  */
 
-static void
-auto_adjust_pair (struct rtx_subst_pair *pair ATTRIBUTE_UNUSED)
+static rtx
+propagate_for_debug_subst (rtx from, const_rtx old_rtx, void *data)
 {
-#ifdef AUTO_INC_DEC
+  struct rtx_subst_pair *pair = (struct rtx_subst_pair *)data;
+
+  if (!rtx_equal_p (from, old_rtx))
+    return NULL_RTX;
   if (!pair->adjusted)
     {
       pair->adjusted = true;
+#ifdef AUTO_INC_DEC
       pair->to = cleanup_auto_inc_dec (pair->to, pair->after, VOIDmode);
-    }
+#else
+      pair->to = copy_rtx (pair->to);
 #endif
-}
-
-/* If *LOC is the same as FROM in the struct rtx_subst_pair passed as
-   DATA, replace it with a copy of TO.  Handle SUBREGs of *LOC as
-   well.  */
-
-static int
-propagate_for_debug_subst (rtx *loc, void *data)
-{
-  struct rtx_subst_pair *pair = (struct rtx_subst_pair *)data;
-  rtx from = pair->from, to = pair->to;
-  rtx x = *loc, s = x;
-
-  if (rtx_equal_p (x, from)
-      || (GET_CODE (x) == SUBREG && rtx_equal_p ((s = SUBREG_REG (x)), from)))
-    {
-      auto_adjust_pair (pair);
-      if (pair->to != to)
-       to = pair->to;
-      else
-       to = copy_rtx (to);
-      if (s != x)
-       {
-         gcc_assert (GET_CODE (x) == SUBREG && SUBREG_REG (x) == s);
-         to = simplify_gen_subreg (GET_MODE (x), to,
-                                   GET_MODE (from), SUBREG_BYTE (x));
-       }
-      *loc = wrap_constant (GET_MODE (x), to);
-      pair->changed = true;
-      return -1;
+      pair->to = make_compound_operation (pair->to, SET);
+      return pair->to;
     }
-
-  return 0;
+  return copy_rtx (pair->to);
 }
 
 /* Replace occurrences of DEST with SRC in DEBUG_INSNs between INSN
@@ -2327,17 +2431,12 @@ propagate_for_debug_subst (rtx *loc, void *data)
 static void
 propagate_for_debug (rtx insn, rtx last, rtx dest, rtx src, bool move)
 {
-  struct rtx_subst_pair p;
-  rtx next, move_pos = move ? last : NULL_RTX;
+  rtx next, move_pos = move ? last : NULL_RTX, loc;
 
-  p.from = dest;
+  struct rtx_subst_pair p;
   p.to = src;
-  p.changed = false;
-
-#ifdef AUTO_INC_DEC
   p.adjusted = false;
   p.after = move;
-#endif
 
   next = NEXT_INSN (insn);
   while (next != last)
@@ -2346,11 +2445,11 @@ propagate_for_debug (rtx insn, rtx last, rtx dest, rtx src, bool move)
       next = NEXT_INSN (insn);
       if (DEBUG_INSN_P (insn))
        {
-         for_each_rtx (&INSN_VAR_LOCATION_LOC (insn),
-                       propagate_for_debug_subst, &p);
-         if (!p.changed)
+         loc = simplify_replace_fn_rtx (INSN_VAR_LOCATION_LOC (insn),
+                                        dest, propagate_for_debug_subst, &p);
+         if (loc == INSN_VAR_LOCATION_LOC (insn))
            continue;
-         p.changed = false;
+         INSN_VAR_LOCATION_LOC (insn) = loc;
          if (move_pos)
            {
              remove_insn (insn);
@@ -2363,7 +2462,7 @@ propagate_for_debug (rtx insn, rtx last, rtx dest, rtx src, bool move)
     }
 }
 
-/* Delete the conditional jump INSN and adjust the CFG correspondingly.
+/* Delete the unconditional jump INSN and adjust the CFG correspondingly.
    Note that the INSN should be deleted *after* removing dead edges, so
    that the kept edge is the fallthrough edge for a (set (pc) (pc))
    but not for a (set (pc) (label_ref FOO)).  */
@@ -2372,24 +2471,25 @@ static void
 update_cfg_for_uncondjump (rtx insn)
 {
   basic_block bb = BLOCK_FOR_INSN (insn);
+  bool at_end = (BB_END (bb) == insn);
 
-  if (BB_END (bb) == insn)
+  if (at_end)
     purge_dead_edges (bb);
 
   delete_insn (insn);
-  if (EDGE_COUNT (bb->succs) == 1)
+  if (at_end && EDGE_COUNT (bb->succs) == 1)
     single_succ_edge (bb)->flags |= EDGE_FALLTHRU;
 }
 
+/* Try to combine the insns I0, I1 and I2 into I3.
+   Here I0, I1 and I2 appear earlier than I3.
+   I0 and I1 can be zero; then we combine just I2 into I3, or I1 and I2 into
+   I3.
 
-/* Try to combine the insns I1 and I2 into I3.
-   Here I1 and I2 appear earlier than I3.
-   I1 can be zero; then we combine just I2 into I3.
-
-   If we are combining three insns and the resulting insn is not recognized,
-   try splitting it into two insns.  If that happens, I2 and I3 are retained
-   and I1 is pseudo-deleted by turning it into a NOTE.  Otherwise, I1 and I2
-   are pseudo-deleted.
+   If we are combining more than two insns and the resulting insn is not
+   recognized, try splitting it into two insns.  If that happens, I2 and I3
+   are retained and I1/I0 are pseudo-deleted by turning them into a NOTE.
+   Otherwise, I0, I1 and I2 are pseudo-deleted.
 
    Return 0 if the combination does not work.  Then nothing is changed.
    If we did the combination, return the insn at which combine should
@@ -2399,34 +2499,36 @@ update_cfg_for_uncondjump (rtx insn)
    new direct jump instruction.  */
 
 static rtx
-try_combine (rtx i3, rtx i2, rtx i1, int *new_direct_jump_p)
+try_combine (rtx i3, rtx i2, rtx i1, rtx i0, int *new_direct_jump_p)
 {
   /* New patterns for I3 and I2, respectively.  */
   rtx newpat, newi2pat = 0;
   rtvec newpat_vec_with_clobbers = 0;
-  int substed_i2 = 0, substed_i1 = 0;
-  /* Indicates need to preserve SET in I1 or I2 in I3 if it is not dead.  */
-  int added_sets_1, added_sets_2;
+  int substed_i2 = 0, substed_i1 = 0, substed_i0 = 0;
+  /* Indicates need to preserve SET in I0, I1 or I2 in I3 if it is not
+     dead.  */
+  int added_sets_0, added_sets_1, added_sets_2;
   /* Total number of SETs to put into I3.  */
   int total_sets;
-  /* Nonzero if I2's body now appears in I3.  */
-  int i2_is_used;
+  /* Nonzero if I2's or I1's body now appears in I3.  */
+  int i2_is_used = 0, i1_is_used = 0;
   /* INSN_CODEs for new I3, new I2, and user of condition code.  */
   int insn_code_number, i2_code_number = 0, other_code_number = 0;
   /* Contains I3 if the destination of I3 is used in its source, which means
      that the old life of I3 is being killed.  If that usage is placed into
      I2 and not in I3, a REG_DEAD note must be made.  */
   rtx i3dest_killed = 0;
-  /* SET_DEST and SET_SRC of I2 and I1.  */
-  rtx i2dest = 0, i2src = 0, i1dest = 0, i1src = 0;
+  /* SET_DEST and SET_SRC of I2, I1 and I0.  */
+  rtx i2dest = 0, i2src = 0, i1dest = 0, i1src = 0, i0dest = 0, i0src = 0;
   /* Set if I2DEST was reused as a scratch register.  */
   bool i2scratch = false;
-  /* PATTERN (I1) and PATTERN (I2), or a copy of it in certain cases.  */
-  rtx i1pat = 0, i2pat = 0;
+  /* The PATTERNs of I0, I1, and I2, or a copy of them in certain cases.  */
+  rtx i0pat = 0, i1pat = 0, i2pat = 0;
   /* Indicates if I2DEST or I1DEST is in I2SRC or I1_SRC.  */
   int i2dest_in_i2src = 0, i1dest_in_i1src = 0, i2dest_in_i1src = 0;
-  int i2dest_killed = 0, i1dest_killed = 0;
-  int i1_feeds_i3 = 0;
+  int i0dest_in_i0src = 0, i1dest_in_i0src = 0, i2dest_in_i0src = 0;
+  int i2dest_killed = 0, i1dest_killed = 0, i0dest_killed = 0;
+  int i1_feeds_i2_n = 0, i0_feeds_i2_n = 0, i0_feeds_i1_n = 0;
   /* Notes that must be added to REG_NOTES in I3 and I2.  */
   rtx new_i3_notes, new_i2_notes;
   /* Notes that we substituted I3 into I2 instead of the normal case.  */
@@ -2443,11 +2545,47 @@ try_combine (rtx i3, rtx i2, rtx i1, int *new_direct_jump_p)
   rtx new_other_notes;
   int i;
 
+  /* Only try four-insn combinations when there's high likelihood of
+     success.  Look for simple insns, such as loads of constants or
+     binary operations involving a constant.  */
+  if (i0)
+    {
+      int i;
+      int ngood = 0;
+      int nshift = 0;
+
+      if (!flag_expensive_optimizations)
+       return 0;
+
+      for (i = 0; i < 4; i++)
+       {
+         rtx insn = i == 0 ? i0 : i == 1 ? i1 : i == 2 ? i2 : i3;
+         rtx set = single_set (insn);
+         rtx src;
+         if (!set)
+           continue;
+         src = SET_SRC (set);
+         if (CONSTANT_P (src))
+           {
+             ngood += 2;
+             break;
+           }
+         else if (BINARY_P (src) && CONSTANT_P (XEXP (src, 1)))
+           ngood++;
+         else if (GET_CODE (src) == ASHIFT || GET_CODE (src) == ASHIFTRT
+                  || GET_CODE (src) == LSHIFTRT)
+           nshift++;
+       }
+      if (ngood < 2 && nshift < 2)
+       return 0;
+    }
+
   /* Exit early if one of the insns involved can't be used for
      combinations.  */
   if (cant_combine_insn_p (i3)
       || cant_combine_insn_p (i2)
       || (i1 && cant_combine_insn_p (i1))
+      || (i0 && cant_combine_insn_p (i0))
       || likely_spilled_retval_p (i3))
     return 0;
 
@@ -2459,7 +2597,10 @@ try_combine (rtx i3, rtx i2, rtx i1, int *new_direct_jump_p)
 
   if (dump_file && (dump_flags & TDF_DETAILS))
     {
-      if (i1)
+      if (i0)
+       fprintf (dump_file, "\nTrying %d, %d, %d -> %d:\n",
+                INSN_UID (i0), INSN_UID (i1), INSN_UID (i2), INSN_UID (i3));
+      else if (i1)
        fprintf (dump_file, "\nTrying %d, %d -> %d:\n",
                 INSN_UID (i1), INSN_UID (i2), INSN_UID (i3));
       else
@@ -2467,8 +2608,12 @@ try_combine (rtx i3, rtx i2, rtx i1, int *new_direct_jump_p)
                 INSN_UID (i2), INSN_UID (i3));
     }
 
-  /* If I1 and I2 both feed I3, they can be in any order.  To simplify the
-     code below, set I1 to be the earlier of the two insns.  */
+  /* If multiple insns feed into one of I2 or I3, they can be in any
+     order.  To simplify the code below, reorder them in sequence.  */
+  if (i0 && DF_INSN_LUID (i0) > DF_INSN_LUID (i2))
+    temp = i2, i2 = i0, i0 = temp;
+  if (i0 && DF_INSN_LUID (i0) > DF_INSN_LUID (i1))
+    temp = i1, i1 = i0, i0 = temp;
   if (i1 && DF_INSN_LUID (i1) > DF_INSN_LUID (i2))
     temp = i1, i1 = i2, i2 = temp;
 
@@ -2536,7 +2681,7 @@ try_combine (rtx i3, rtx i2, rtx i1, int *new_direct_jump_p)
              subst_insn = i3;
              subst_low_luid = DF_INSN_LUID (i2);
 
-             added_sets_2 = added_sets_1 = 0;
+             added_sets_2 = added_sets_1 = added_sets_0 = 0;
              i2src = SET_DEST (PATTERN (i3));
              i2dest = SET_SRC (PATTERN (i3));
              i2dest_killed = dead_or_set_p (i2, i2dest);
@@ -2605,86 +2750,38 @@ try_combine (rtx i3, rtx i2, rtx i1, int *new_direct_jump_p)
 
       if (offset >= 0
          && (GET_MODE_BITSIZE (GET_MODE (SET_DEST (temp)))
-             <= HOST_BITS_PER_WIDE_INT * 2))
+             <= HOST_BITS_PER_DOUBLE_INT))
        {
-         HOST_WIDE_INT mhi, ohi, ihi;
-         HOST_WIDE_INT mlo, olo, ilo;
+         double_int m, o, i;
          rtx inner = SET_SRC (PATTERN (i3));
          rtx outer = SET_SRC (temp);
 
-         if (CONST_INT_P (outer))
-           {
-             olo = INTVAL (outer);
-             ohi = olo < 0 ? -1 : 0;
-           }
-         else
-           {
-             olo = CONST_DOUBLE_LOW (outer);
-             ohi = CONST_DOUBLE_HIGH (outer);
-           }
-
-         if (CONST_INT_P (inner))
-           {
-             ilo = INTVAL (inner);
-             ihi = ilo < 0 ? -1 : 0;
-           }
-         else
-           {
-             ilo = CONST_DOUBLE_LOW (inner);
-             ihi = CONST_DOUBLE_HIGH (inner);
-           }
-
-         if (width < HOST_BITS_PER_WIDE_INT)
-           {
-             mlo = ((unsigned HOST_WIDE_INT) 1 << width) - 1;
-             mhi = 0;
-           }
-         else if (width < HOST_BITS_PER_WIDE_INT * 2)
-           {
-             mhi = ((unsigned HOST_WIDE_INT) 1
-                    << (width - HOST_BITS_PER_WIDE_INT)) - 1;
-             mlo = -1;
-           }
-         else
-           {
-             mlo = -1;
-             mhi = -1;
-           }
+         o = rtx_to_double_int (outer);
+         i = rtx_to_double_int (inner);
 
-         ilo &= mlo;
-         ihi &= mhi;
-
-         if (offset >= HOST_BITS_PER_WIDE_INT)
-           {
-             mhi = mlo << (offset - HOST_BITS_PER_WIDE_INT);
-             mlo = 0;
-             ihi = ilo << (offset - HOST_BITS_PER_WIDE_INT);
-             ilo = 0;
-           }
-         else if (offset > 0)
-           {
-             mhi = (mhi << offset) | ((unsigned HOST_WIDE_INT) mlo
-                                      >> (HOST_BITS_PER_WIDE_INT - offset));
-             mlo = mlo << offset;
-             ihi = (ihi << offset) | ((unsigned HOST_WIDE_INT) ilo
-                                      >> (HOST_BITS_PER_WIDE_INT - offset));
-             ilo = ilo << offset;
-           }
-
-         olo = (olo & ~mlo) | ilo;
-         ohi = (ohi & ~mhi) | ihi;
+         m = double_int_mask (width);
+         i = double_int_and (i, m);
+         m = double_int_lshift (m, offset, HOST_BITS_PER_DOUBLE_INT, false);
+         i = double_int_lshift (i, offset, HOST_BITS_PER_DOUBLE_INT, false);
+         o = double_int_ior (double_int_and_not (o, m), i);
 
          combine_merges++;
          subst_insn = i3;
          subst_low_luid = DF_INSN_LUID (i2);
-         added_sets_2 = added_sets_1 = 0;
+         added_sets_2 = added_sets_1 = added_sets_0 = 0;
          i2dest = SET_DEST (temp);
          i2dest_killed = dead_or_set_p (i2, i2dest);
 
+         /* Replace the source in I2 with the new constant and make the
+            resulting insn the new pattern for I3.  Then skip to where we
+            validate the pattern.  Everything was set up above.  */
          SUBST (SET_SRC (temp),
-                immed_double_const (olo, ohi, GET_MODE (SET_DEST (temp))));
+                immed_double_int_const (o, GET_MODE (SET_DEST (temp))));
 
          newpat = PATTERN (i2);
+
+          /* The dest of I3 has been replaced with the dest of I2.  */
+          changed_i3_dest = 1;
          goto validate_replacement;
        }
     }
@@ -2727,8 +2824,8 @@ try_combine (rtx i3, rtx i2, rtx i1, int *new_direct_jump_p)
             as I2 will not cause a problem.  */
 
          i1 = gen_rtx_INSN (VOIDmode, INSN_UID (i2), NULL_RTX, i2,
-                            BLOCK_FOR_INSN (i2), INSN_LOCATOR (i2),
-                            XVECEXP (PATTERN (i2), 0, 1), -1, NULL_RTX);
+                            BLOCK_FOR_INSN (i2), XVECEXP (PATTERN (i2), 0, 1),
+                            INSN_LOCATOR (i2), -1, NULL_RTX);
 
          SUBST (PATTERN (i2), XVECEXP (PATTERN (i2), 0, 0));
          SUBST (XEXP (SET_SRC (PATTERN (i2)), 0),
@@ -2738,8 +2835,11 @@ try_combine (rtx i3, rtx i2, rtx i1, int *new_direct_jump_p)
 #endif
 
   /* Verify that I2 and I1 are valid for combining.  */
-  if (! can_combine_p (i2, i3, i1, NULL_RTX, &i2dest, &i2src)
-      || (i1 && ! can_combine_p (i1, i3, NULL_RTX, i2, &i1dest, &i1src)))
+  if (! can_combine_p (i2, i3, i0, i1, NULL_RTX, NULL_RTX, &i2dest, &i2src)
+      || (i1 && ! can_combine_p (i1, i3, i0, NULL_RTX, i2, NULL_RTX,
+                                &i1dest, &i1src))
+      || (i0 && ! can_combine_p (i0, i3, NULL_RTX, NULL_RTX, i1, i2,
+                                &i0dest, &i0src)))
     {
       undo_all ();
       return 0;
@@ -2750,16 +2850,26 @@ try_combine (rtx i3, rtx i2, rtx i1, int *new_direct_jump_p)
   i2dest_in_i2src = reg_overlap_mentioned_p (i2dest, i2src);
   i1dest_in_i1src = i1 && reg_overlap_mentioned_p (i1dest, i1src);
   i2dest_in_i1src = i1 && reg_overlap_mentioned_p (i2dest, i1src);
+  i0dest_in_i0src = i0 && reg_overlap_mentioned_p (i0dest, i0src);
+  i1dest_in_i0src = i0 && reg_overlap_mentioned_p (i1dest, i0src);
+  i2dest_in_i0src = i0 && reg_overlap_mentioned_p (i2dest, i0src);
   i2dest_killed = dead_or_set_p (i2, i2dest);
   i1dest_killed = i1 && dead_or_set_p (i1, i1dest);
+  i0dest_killed = i0 && dead_or_set_p (i0, i0dest);
 
-  /* See if I1 directly feeds into I3.  It does if I1DEST is not used
-     in I2SRC.  */
-  i1_feeds_i3 = i1 && ! reg_overlap_mentioned_p (i1dest, i2src);
+  /* For the earlier insns, determine which of the subsequent ones they
+     feed.  */
+  i1_feeds_i2_n = i1 && insn_a_feeds_b (i1, i2);
+  i0_feeds_i1_n = i0 && insn_a_feeds_b (i0, i1);
+  i0_feeds_i2_n = (i0 && (!i0_feeds_i1_n ? insn_a_feeds_b (i0, i2)
+                         : (!reg_overlap_mentioned_p (i1dest, i0dest)
+                            && reg_overlap_mentioned_p (i0dest, i2src))));
 
   /* Ensure that I3's pattern can be the destination of combines.  */
-  if (! combinable_i3pat (i3, &PATTERN (i3), i2dest, i1dest,
-                         i1 && i2dest_in_i1src && i1_feeds_i3,
+  if (! combinable_i3pat (i3, &PATTERN (i3), i2dest, i1dest, i0dest,
+                         i1 && i2dest_in_i1src && !i1_feeds_i2_n,
+                         i0 && ((i2dest_in_i0src && !i0_feeds_i2_n)
+                                || (i1dest_in_i0src && !i0_feeds_i1_n)),
                          &i3dest_killed))
     {
       undo_all ();
@@ -2771,6 +2881,7 @@ try_combine (rtx i3, rtx i2, rtx i1, int *new_direct_jump_p)
      here.  */
   if (GET_CODE (i2src) == MULT
       || (i1 != 0 && GET_CODE (i1src) == MULT)
+      || (i0 != 0 && GET_CODE (i0src) == MULT)
       || (GET_CODE (PATTERN (i3)) == SET
          && GET_CODE (SET_SRC (PATTERN (i3))) == MULT))
     have_mult = 1;
@@ -2810,14 +2921,22 @@ try_combine (rtx i3, rtx i2, rtx i1, int *new_direct_jump_p)
      feed into I3, the set in I1 needs to be kept around if I1DEST dies
      or is set in I3.  Otherwise (if I1 feeds I2 which feeds I3), the set
      in I1 needs to be kept around unless I1DEST dies or is set in either
-     I2 or I3.  We can distinguish these cases by seeing if I2SRC mentions
-     I1DEST.  If so, we know I1 feeds into I2.  */
+     I2 or I3.  The same consideration applies to I0.  */
 
-  added_sets_2 = ! dead_or_set_p (i3, i2dest);
+  added_sets_2 = !dead_or_set_p (i3, i2dest);
+
+  if (i1)
+    added_sets_1 = !(dead_or_set_p (i3, i1dest)
+                    || (i1_feeds_i2_n && dead_or_set_p (i2, i1dest)));
+  else
+    added_sets_1 = 0;
 
-  added_sets_1
-    = i1 && ! (i1_feeds_i3 ? dead_or_set_p (i3, i1dest)
-              : (dead_or_set_p (i3, i1dest) || dead_or_set_p (i2, i1dest)));
+  if (i0)
+    added_sets_0 =  !(dead_or_set_p (i3, i0dest)
+                     || (i0_feeds_i2_n && dead_or_set_p (i2, i0dest))
+                     || (i0_feeds_i1_n && dead_or_set_p (i1, i0dest)));
+  else
+    added_sets_0 = 0;
 
   /* If the set in I2 needs to be kept around, we must make a copy of
      PATTERN (I2), so that when we substitute I1SRC for I1DEST in
@@ -2842,6 +2961,14 @@ try_combine (rtx i3, rtx i2, rtx i1, int *new_direct_jump_p)
        i1pat = copy_rtx (PATTERN (i1));
     }
 
+  if (added_sets_0)
+    {
+      if (GET_CODE (PATTERN (i0)) == PARALLEL)
+       i0pat = gen_rtx_SET (VOIDmode, i0dest, copy_rtx (i0src));
+      else
+       i0pat = copy_rtx (PATTERN (i0));
+    }
+
   combine_merges++;
 
   /* Substitute in the latest insn for the regs set by the earlier ones.  */
@@ -2890,8 +3017,8 @@ try_combine (rtx i3, rtx i2, rtx i1, int *new_direct_jump_p)
                                              i2src, const0_rtx))
              != GET_MODE (SET_DEST (newpat))))
        {
-         if (can_change_dest_mode(SET_DEST (newpat), added_sets_2,
-                                  compare_mode))
+         if (can_change_dest_mode (SET_DEST (newpat), added_sets_2,
+                                   compare_mode))
            {
              unsigned int regno = REGNO (SET_DEST (newpat));
              rtx new_dest;
@@ -2954,13 +3081,14 @@ try_combine (rtx i3, rtx i2, rtx i1, int *new_direct_jump_p)
 
       n_occurrences = 0;               /* `subst' counts here */
 
-      /* If I1 feeds into I2 (not into I3) and I1DEST is in I1SRC, we
-        need to make a unique copy of I2SRC each time we substitute it
-        to avoid self-referential rtl.  */
+      /* If I1 feeds into I2 and I1DEST is in I1SRC, we need to make a
+        unique copy of I2SRC each time we substitute it to avoid
+        self-referential rtl.  */
 
       subst_low_luid = DF_INSN_LUID (i2);
       newpat = subst (PATTERN (i3), i2dest, i2src, 0,
-                     ! i1_feeds_i3 && i1dest_in_i1src);
+                     ((i1_feeds_i2_n && i1dest_in_i1src)
+                      || (i0_feeds_i2_n && i0dest_in_i0src)));
       substed_i2 = 1;
 
       /* Record whether i2's body now appears within i3's body.  */
@@ -2976,13 +3104,14 @@ try_combine (rtx i3, rtx i2, rtx i1, int *new_direct_jump_p)
         This happens if I1DEST is mentioned in I2 and dies there, and
         has disappeared from the new pattern.  */
       if ((FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
-          && !i1_feeds_i3
+          && i1_feeds_i2_n
           && dead_or_set_p (i2, i1dest)
           && !reg_overlap_mentioned_p (i1dest, newpat))
          /* Before we can do this substitution, we must redo the test done
             above (see detailed comments there) that ensures  that I1DEST
             isn't mentioned in any SETs in NEWPAT that are field assignments.  */
-          || !combinable_i3pat (NULL_RTX, &newpat, i1dest, NULL_RTX, 0, 0))
+          || !combinable_i3pat (NULL_RTX, &newpat, i1dest, NULL_RTX, NULL_RTX,
+                               0, 0, 0))
        {
          undo_all ();
          return 0;
@@ -2990,8 +3119,29 @@ try_combine (rtx i3, rtx i2, rtx i1, int *new_direct_jump_p)
 
       n_occurrences = 0;
       subst_low_luid = DF_INSN_LUID (i1);
-      newpat = subst (newpat, i1dest, i1src, 0, 0);
+      newpat = subst (newpat, i1dest, i1src, 0,
+                     i0_feeds_i1_n && i0dest_in_i0src);
       substed_i1 = 1;
+      i1_is_used = n_occurrences;
+    }
+  if (i0 && GET_CODE (newpat) != CLOBBER)
+    {
+      if ((FIND_REG_INC_NOTE (i0, NULL_RTX) != 0
+          && ((i0_feeds_i2_n && dead_or_set_p (i2, i0dest))
+              || (i0_feeds_i1_n && dead_or_set_p (i1, i0dest)))
+          && !reg_overlap_mentioned_p (i0dest, newpat))
+          || !combinable_i3pat (NULL_RTX, &newpat, i0dest, NULL_RTX, NULL_RTX,
+                               0, 0, 0))
+       {
+         undo_all ();
+         return 0;
+       }
+
+      n_occurrences = 0;
+      subst_low_luid = DF_INSN_LUID (i0);
+      newpat = subst (newpat, i0dest, i0src, 0,
+                     i0_feeds_i1_n && i0dest_in_i0src);
+      substed_i0 = 1;
     }
 
   /* Fail if an autoincrement side-effect has been duplicated.  Be careful
@@ -2999,7 +3149,12 @@ try_combine (rtx i3, rtx i2, rtx i1, int *new_direct_jump_p)
   if ((FIND_REG_INC_NOTE (i2, NULL_RTX) != 0
        && i2_is_used + added_sets_2 > 1)
       || (i1 != 0 && FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
-         && (n_occurrences + added_sets_1 + (added_sets_2 && ! i1_feeds_i3)
+         && (i1_is_used + added_sets_1 + (added_sets_2 && i1_feeds_i2_n)
+             > 1))
+      || (i0 != 0 && FIND_REG_INC_NOTE (i0, NULL_RTX) != 0
+         && (n_occurrences + added_sets_0
+             + (added_sets_1 && i0_feeds_i1_n)
+             + (added_sets_2 && i0_feeds_i2_n)
              > 1))
       /* Fail if we tried to make a new register.  */
       || max_reg_num () != maxreg
@@ -3019,14 +3174,15 @@ try_combine (rtx i3, rtx i2, rtx i1, int *new_direct_jump_p)
      we must make a new PARALLEL for the latest insn
      to hold additional the SETs.  */
 
-  if (added_sets_1 || added_sets_2)
+  if (added_sets_0 || added_sets_1 || added_sets_2)
     {
+      int extra_sets = added_sets_0 + added_sets_1 + added_sets_2;
       combine_extras++;
 
       if (GET_CODE (newpat) == PARALLEL)
        {
          rtvec old = XVEC (newpat, 0);
-         total_sets = XVECLEN (newpat, 0) + added_sets_1 + added_sets_2;
+         total_sets = XVECLEN (newpat, 0) + extra_sets;
          newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
          memcpy (XVEC (newpat, 0)->elem, &old->elem[0],
                  sizeof (old->elem[0]) * old->num_elem);
@@ -3034,30 +3190,34 @@ try_combine (rtx i3, rtx i2, rtx i1, int *new_direct_jump_p)
       else
        {
          rtx old = newpat;
-         total_sets = 1 + added_sets_1 + added_sets_2;
+         total_sets = 1 + extra_sets;
          newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
          XVECEXP (newpat, 0, 0) = old;
        }
 
+      if (added_sets_0)
+       XVECEXP (newpat, 0, --total_sets) = i0pat;
+
       if (added_sets_1)
-       XVECEXP (newpat, 0, --total_sets) = i1pat;
+       {
+         rtx t = i1pat;
+         if (i0_feeds_i1_n)
+           t = subst (t, i0dest, i0src, 0, 0);
 
+         XVECEXP (newpat, 0, --total_sets) = t;
+       }
       if (added_sets_2)
        {
-         /* If there is no I1, use I2's body as is.  We used to also not do
-            the subst call below if I2 was substituted into I3,
-            but that could lose a simplification.  */
-         if (i1 == 0)
-           XVECEXP (newpat, 0, --total_sets) = i2pat;
-         else
-           /* See comment where i2pat is assigned.  */
-           XVECEXP (newpat, 0, --total_sets)
-             = subst (i2pat, i1dest, i1src, 0, 0);
+         rtx t = i2pat;
+         if (i0_feeds_i2_n)
+           t = subst (t, i0dest, i0src, 0, 0);
+         if (i1_feeds_i2_n)
+           t = subst (t, i1dest, i1src, 0, 0);
+
+         XVECEXP (newpat, 0, --total_sets) = t;
        }
     }
 
-  /* We come here when we are replacing a destination in I2 with the
-     destination of I3.  */
  validate_replacement:
 
   /* Note which hard regs this insn has as inputs.  */
@@ -3279,7 +3439,8 @@ try_combine (rtx i3, rtx i2, rtx i1, int *new_direct_jump_p)
       /* If we can split it and use I2DEST, go ahead and see if that
         helps things be recognized.  Verify that none of the registers
         are set between I2 and I3.  */
-      if (insn_code_number < 0 && (split = find_split_point (&newpat, i3)) != 0
+      if (insn_code_number < 0
+          && (split = find_split_point (&newpat, i3, false)) != 0
 #ifdef HAVE_cc0
          && REG_P (i2dest)
 #endif
@@ -3305,6 +3466,14 @@ try_combine (rtx i3, rtx i2, rtx i1, int *new_direct_jump_p)
 
          i2scratch = true;
 
+         /* *SPLIT may be part of I2SRC, so make sure we have the
+            original expression around for later debug processing.
+            We should not need I2SRC any more in other cases.  */
+         if (MAY_HAVE_DEBUG_INSNS)
+           i2src = copy_rtx (i2src);
+         else
+           i2src = NULL;
+
          /* Get NEWDEST as a register in the proper mode.  We have already
             validated that we can do this.  */
          if (GET_MODE (i2dest) != split_mode && split_mode != VOIDmode)
@@ -3556,7 +3725,58 @@ try_combine (rtx i3, rtx i2, rtx i1, int *new_direct_jump_p)
       i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
 
       if (i2_code_number >= 0)
-       insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
+       {
+         /* recog_for_combine might have added CLOBBERs to newi2pat.
+            Make sure NEWPAT does not depend on the clobbered regs.  */
+         if (GET_CODE (newi2pat) == PARALLEL)
+           {
+             for (i = XVECLEN (newi2pat, 0) - 1; i >= 0; i--)
+               if (GET_CODE (XVECEXP (newi2pat, 0, i)) == CLOBBER)
+                 {
+                   rtx reg = XEXP (XVECEXP (newi2pat, 0, i), 0);
+                   if (reg_overlap_mentioned_p (reg, newpat))
+                     break;
+                 }
+
+             if (i >= 0)
+               {
+                 /* CLOBBERs on newi2pat prevent it going first.
+                    Try the other order of the insns if possible.  */
+                 temp = newpat;
+                 newpat = XVECEXP (newi2pat, 0, 0);
+                 newi2pat = temp;
+#ifdef HAVE_cc0
+                 if (reg_referenced_p (cc0_rtx, newpat))
+                   {
+                     undo_all ();
+                     return 0;
+                   }
+#endif
+
+                 i2_code_number = recog_for_combine (&newi2pat, i2,
+                                                     &new_i2_notes);
+                 if (i2_code_number < 0)
+                   {
+                     undo_all ();
+                     return 0;
+                   }
+
+                 if (GET_CODE (newi2pat) == PARALLEL)
+                   for (i = XVECLEN (newi2pat, 0) - 1; i >= 0; i--)
+                     if (GET_CODE (XVECEXP (newi2pat, 0, i)) == CLOBBER)
+                       {
+                         rtx reg = XEXP (XVECEXP (newi2pat, 0, i), 0);
+                         if (reg_overlap_mentioned_p (reg, newpat))
+                           {
+                             undo_all ();
+                             return 0;
+                           }
+                       }
+               }
+           }
+
+         insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
+       }
     }
 
   /* If it still isn't recognized, fail and change things back the way they
@@ -3601,7 +3821,7 @@ try_combine (rtx i3, rtx i2, rtx i1, int *new_direct_jump_p)
 
   /* Only allow this combination if insn_rtx_costs reports that the
      replacement instructions are cheaper than the originals.  */
-  if (!combine_validate_cost (i1, i2, i3, newpat, newi2pat, other_pat))
+  if (!combine_validate_cost (i0, i1, i2, i3, newpat, newi2pat, other_pat))
     {
       undo_all ();
       return 0;
@@ -3700,7 +3920,8 @@ try_combine (rtx i3, rtx i2, rtx i1, int *new_direct_jump_p)
        }
 
       distribute_notes (new_other_notes, undobuf.other_insn,
-                       undobuf.other_insn, NULL_RTX, NULL_RTX, NULL_RTX);
+                       undobuf.other_insn, NULL_RTX, NULL_RTX, NULL_RTX,
+                       NULL_RTX);
     }
 
   if (swap_i2i3)
@@ -3747,21 +3968,26 @@ try_combine (rtx i3, rtx i2, rtx i1, int *new_direct_jump_p)
     }
 
   {
-    rtx i3notes, i2notes, i1notes = 0;
-    rtx i3links, i2links, i1links = 0;
+    rtx i3notes, i2notes, i1notes = 0, i0notes = 0;
+    rtx i3links, i2links, i1links = 0, i0links = 0;
     rtx midnotes = 0;
+    int from_luid;
     unsigned int regno;
     /* Compute which registers we expect to eliminate.  newi2pat may be setting
        either i3dest or i2dest, so we must check it.  Also, i1dest may be the
        same as i3dest, in which case newi2pat may be setting i1dest.  */
     rtx elim_i2 = ((newi2pat && reg_set_p (i2dest, newi2pat))
-                  || i2dest_in_i2src || i2dest_in_i1src
+                  || i2dest_in_i2src || i2dest_in_i1src || i2dest_in_i0src
                   || !i2dest_killed
                   ? 0 : i2dest);
-    rtx elim_i1 = (i1 == 0 || i1dest_in_i1src
+    rtx elim_i1 = (i1 == 0 || i1dest_in_i1src || i1dest_in_i0src
                   || (newi2pat && reg_set_p (i1dest, newi2pat))
                   || !i1dest_killed
                   ? 0 : i1dest);
+    rtx elim_i0 = (i0 == 0 || i0dest_in_i0src
+                  || (newi2pat && reg_set_p (i0dest, newi2pat))
+                  || !i0dest_killed
+                  ? 0 : i0dest);
 
     /* Get the old REG_NOTES and LOG_LINKS from all our insns and
        clear them.  */
@@ -3769,6 +3995,8 @@ try_combine (rtx i3, rtx i2, rtx i1, int *new_direct_jump_p)
     i2notes = REG_NOTES (i2), i2links = LOG_LINKS (i2);
     if (i1)
       i1notes = REG_NOTES (i1), i1links = LOG_LINKS (i1);
+    if (i0)
+      i0notes = REG_NOTES (i0), i0links = LOG_LINKS (i0);
 
     /* Ensure that we do not have something that should not be shared but
        occurs multiple times in the new insns.  Check this by first
@@ -3777,6 +4005,7 @@ try_combine (rtx i3, rtx i2, rtx i1, int *new_direct_jump_p)
     reset_used_flags (i3notes);
     reset_used_flags (i2notes);
     reset_used_flags (i1notes);
+    reset_used_flags (i0notes);
     reset_used_flags (newpat);
     reset_used_flags (newi2pat);
     if (undobuf.other_insn)
@@ -3785,6 +4014,7 @@ try_combine (rtx i3, rtx i2, rtx i1, int *new_direct_jump_p)
     i3notes = copy_rtx_if_shared (i3notes);
     i2notes = copy_rtx_if_shared (i2notes);
     i1notes = copy_rtx_if_shared (i1notes);
+    i0notes = copy_rtx_if_shared (i0notes);
     newpat = copy_rtx_if_shared (newpat);
     newi2pat = copy_rtx_if_shared (newi2pat);
     if (undobuf.other_insn)
@@ -3801,10 +4031,18 @@ try_combine (rtx i3, rtx i2, rtx i1, int *new_direct_jump_p)
        call_usage = copy_rtx (call_usage);
 
        if (substed_i2)
-         replace_rtx (call_usage, i2dest, i2src);
+         {
+           /* I2SRC must still be meaningful at this point.  Some splitting
+              operations can invalidate I2SRC, but those operations do not
+              apply to calls.  */
+           gcc_assert (i2src);
+           replace_rtx (call_usage, i2dest, i2src);
+         }
 
        if (substed_i1)
          replace_rtx (call_usage, i1dest, i1src);
+       if (substed_i0)
+         replace_rtx (call_usage, i0dest, i0src);
 
        CALL_INSN_FUNCTION_USAGE (i3) = call_usage;
       }
@@ -3879,43 +4117,58 @@ try_combine (rtx i3, rtx i2, rtx i1, int *new_direct_jump_p)
        SET_INSN_DELETED (i1);
       }
 
+    if (i0)
+      {
+       LOG_LINKS (i0) = 0;
+       REG_NOTES (i0) = 0;
+       if (MAY_HAVE_DEBUG_INSNS)
+         propagate_for_debug (i0, i3, i0dest, i0src, false);
+       SET_INSN_DELETED (i0);
+      }
+
     /* Get death notes for everything that is now used in either I3 or
        I2 and used to die in a previous insn.  If we built two new
        patterns, move from I1 to I2 then I2 to I3 so that we get the
        proper movement on registers that I2 modifies.  */
 
-    if (newi2pat)
-      {
-       move_deaths (newi2pat, NULL_RTX, DF_INSN_LUID (i1), i2, &midnotes);
-       move_deaths (newpat, newi2pat, DF_INSN_LUID (i1), i3, &midnotes);
-      }
+    if (i0)
+      from_luid = DF_INSN_LUID (i0);
+    else if (i1)
+      from_luid = DF_INSN_LUID (i1);
     else
-      move_deaths (newpat, NULL_RTX, i1 ? DF_INSN_LUID (i1) : DF_INSN_LUID (i2),
-                  i3, &midnotes);
+      from_luid = DF_INSN_LUID (i2);
+    if (newi2pat)
+      move_deaths (newi2pat, NULL_RTX, from_luid, i2, &midnotes);
+    move_deaths (newpat, newi2pat, from_luid, i3, &midnotes);
 
     /* Distribute all the LOG_LINKS and REG_NOTES from I1, I2, and I3.  */
     if (i3notes)
       distribute_notes (i3notes, i3, i3, newi2pat ? i2 : NULL_RTX,
-                       elim_i2, elim_i1);
+                       elim_i2, elim_i1, elim_i0);
     if (i2notes)
       distribute_notes (i2notes, i2, i3, newi2pat ? i2 : NULL_RTX,
-                       elim_i2, elim_i1);
+                       elim_i2, elim_i1, elim_i0);
     if (i1notes)
       distribute_notes (i1notes, i1, i3, newi2pat ? i2 : NULL_RTX,
-                       elim_i2, elim_i1);
+                       elim_i2, elim_i1, elim_i0);
+    if (i0notes)
+      distribute_notes (i0notes, i0, i3, newi2pat ? i2 : NULL_RTX,
+                       elim_i2, elim_i1, elim_i0);
     if (midnotes)
       distribute_notes (midnotes, NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
-                       elim_i2, elim_i1);
+                       elim_i2, elim_i1, elim_i0);
 
     /* Distribute any notes added to I2 or I3 by recog_for_combine.  We
        know these are REG_UNUSED and want them to go to the desired insn,
        so we always pass it as i3.  */
 
     if (newi2pat && new_i2_notes)
-      distribute_notes (new_i2_notes, i2, i2, NULL_RTX, NULL_RTX, NULL_RTX);
-    
+      distribute_notes (new_i2_notes, i2, i2, NULL_RTX, NULL_RTX, NULL_RTX,
+                       NULL_RTX);
+
     if (new_i3_notes)
-      distribute_notes (new_i3_notes, i3, i3, NULL_RTX, NULL_RTX, NULL_RTX);
+      distribute_notes (new_i3_notes, i3, i3, NULL_RTX, NULL_RTX, NULL_RTX,
+                       NULL_RTX);
 
     /* If I3DEST was used in I3SRC, it really died in I3.  We may need to
        put a REG_DEAD note for it somewhere.  If NEWI2PAT exists and sets
@@ -3929,39 +4182,51 @@ try_combine (rtx i3, rtx i2, rtx i1, int *new_direct_jump_p)
        if (newi2pat && reg_set_p (i3dest_killed, newi2pat))
          distribute_notes (alloc_reg_note (REG_DEAD, i3dest_killed,
                                            NULL_RTX),
-                           NULL_RTX, i2, NULL_RTX, elim_i2, elim_i1);
+                           NULL_RTX, i2, NULL_RTX, elim_i2, elim_i1, elim_i0);
        else
          distribute_notes (alloc_reg_note (REG_DEAD, i3dest_killed,
                                            NULL_RTX),
                            NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
-                           elim_i2, elim_i1);
+                           elim_i2, elim_i1, elim_i0);
       }
 
     if (i2dest_in_i2src)
       {
+       rtx new_note = alloc_reg_note (REG_DEAD, i2dest, NULL_RTX);
        if (newi2pat && reg_set_p (i2dest, newi2pat))
-         distribute_notes (alloc_reg_note (REG_DEAD, i2dest, NULL_RTX),
-                           NULL_RTX, i2, NULL_RTX, NULL_RTX, NULL_RTX);
-       else
-         distribute_notes (alloc_reg_note (REG_DEAD, i2dest, NULL_RTX),
-                           NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
+         distribute_notes (new_note,  NULL_RTX, i2, NULL_RTX, NULL_RTX,
                            NULL_RTX, NULL_RTX);
+       else
+         distribute_notes (new_note, NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
+                           NULL_RTX, NULL_RTX, NULL_RTX);
       }
 
     if (i1dest_in_i1src)
       {
+       rtx new_note = alloc_reg_note (REG_DEAD, i1dest, NULL_RTX);
        if (newi2pat && reg_set_p (i1dest, newi2pat))
-         distribute_notes (alloc_reg_note (REG_DEAD, i1dest, NULL_RTX),
-                           NULL_RTX, i2, NULL_RTX, NULL_RTX, NULL_RTX);
+         distribute_notes (new_note, NULL_RTX, i2, NULL_RTX, NULL_RTX,
+                           NULL_RTX, NULL_RTX);
        else
-         distribute_notes (alloc_reg_note (REG_DEAD, i1dest, NULL_RTX),
-                           NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
+         distribute_notes (new_note, NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
+                           NULL_RTX, NULL_RTX, NULL_RTX);
+      }
+
+    if (i0dest_in_i0src)
+      {
+       rtx new_note = alloc_reg_note (REG_DEAD, i0dest, NULL_RTX);
+       if (newi2pat && reg_set_p (i0dest, newi2pat))
+         distribute_notes (new_note, NULL_RTX, i2, NULL_RTX, NULL_RTX,
                            NULL_RTX, NULL_RTX);
+       else
+         distribute_notes (new_note, NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
+                           NULL_RTX, NULL_RTX, NULL_RTX);
       }
 
     distribute_links (i3links);
     distribute_links (i2links);
     distribute_links (i1links);
+    distribute_links (i0links);
 
     if (REG_P (i2dest))
       {
@@ -4011,6 +4276,23 @@ try_combine (rtx i3, rtx i2, rtx i1, int *new_direct_jump_p)
          INC_REG_N_SETS (regno, -1);
       }
 
+    if (i0 && REG_P (i0dest))
+      {
+       rtx link;
+       rtx i0_insn = 0, i0_val = 0, set;
+
+       for (link = LOG_LINKS (i3); link; link = XEXP (link, 1))
+         if ((set = single_set (XEXP (link, 0))) != 0
+             && rtx_equal_p (i0dest, SET_DEST (set)))
+           i0_insn = XEXP (link, 0), i0_val = SET_SRC (set);
+
+       record_value_for_reg (i0dest, i0_insn, i0_val);
+
+       regno = REGNO (i0dest);
+       if (! added_sets_0 && ! i0dest_in_i0src)
+         INC_REG_N_SETS (regno, -1);
+      }
+
     /* Update reg_stat[].nonzero_bits et al for any changes that may have
        been made to this insn.  The order of
        set_nonzero_bits_and_sign_copies() is important.  Because newi2pat
@@ -4030,6 +4312,16 @@ try_combine (rtx i3, rtx i2, rtx i1, int *new_direct_jump_p)
       df_insn_rescan (undobuf.other_insn);
     }
 
+  if (i0 && !(NOTE_P(i0) && (NOTE_KIND (i0) == NOTE_INSN_DELETED)))
+    {
+      if (dump_file)
+       {
+         fprintf (dump_file, "modifying insn i1 ");
+         dump_insn_slim (dump_file, i0);
+       }
+      df_insn_rescan (i0);
+    }
+
   if (i1 && !(NOTE_P(i1) && (NOTE_KIND (i1) == NOTE_INSN_DELETED)))
     {
       if (dump_file)
@@ -4059,7 +4351,7 @@ try_combine (rtx i3, rtx i2, rtx i1, int *new_direct_jump_p)
        }
       df_insn_rescan (i3);
     }
-  
+
   /* Set new_direct_jump_p if a new return or simple jump instruction
      has been created.  Adjust the CFG accordingly.  */
 
@@ -4087,7 +4379,7 @@ try_combine (rtx i3, rtx i2, rtx i1, int *new_direct_jump_p)
       *new_direct_jump_p = 1;
       update_cfg_for_uncondjump (i3);
     }
-  
+
   combine_successes++;
   undo_commit ();
 
@@ -4156,7 +4448,7 @@ undo_commit (void)
    two insns.  */
 
 static rtx *
-find_split_point (rtx *loc, rtx insn)
+find_split_point (rtx *loc, rtx insn, bool set_src)
 {
   rtx x = *loc;
   enum rtx_code code = GET_CODE (x);
@@ -4176,7 +4468,7 @@ find_split_point (rtx *loc, rtx insn)
       if (MEM_P (SUBREG_REG (x)))
        return loc;
 #endif
-      return find_split_point (&SUBREG_REG (x), insn);
+      return find_split_point (&SUBREG_REG (x), insn, false);
 
     case MEM:
 #ifdef HAVE_lo_sum
@@ -4185,9 +4477,12 @@ find_split_point (rtx *loc, rtx insn)
       if (GET_CODE (XEXP (x, 0)) == CONST
          || GET_CODE (XEXP (x, 0)) == SYMBOL_REF)
        {
+         enum machine_mode address_mode
+           = targetm.addr_space.address_mode (MEM_ADDR_SPACE (x));
+
          SUBST (XEXP (x, 0),
-                gen_rtx_LO_SUM (Pmode,
-                                gen_rtx_HIGH (Pmode, XEXP (x, 0)),
+                gen_rtx_LO_SUM (address_mode,
+                                gen_rtx_HIGH (address_mode, XEXP (x, 0)),
                                 XEXP (x, 0)));
          return &XEXP (XEXP (x, 0), 0);
        }
@@ -4200,7 +4495,8 @@ find_split_point (rtx *loc, rtx insn)
         it will not remain in the result.  */
       if (GET_CODE (XEXP (x, 0)) == PLUS
          && CONST_INT_P (XEXP (XEXP (x, 0), 1))
-         && ! memory_address_p (GET_MODE (x), XEXP (x, 0)))
+         && ! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
+                                           MEM_ADDR_SPACE (x)))
        {
          rtx reg = regno_reg_rtx[FIRST_PSEUDO_REGISTER];
          rtx seq = combine_split_insns (gen_rtx_SET (VOIDmode, reg,
@@ -4223,8 +4519,9 @@ find_split_point (rtx *loc, rtx insn)
              && NONJUMP_INSN_P (NEXT_INSN (seq))
              && GET_CODE (PATTERN (NEXT_INSN (seq))) == SET
              && SET_DEST (PATTERN (NEXT_INSN (seq))) == reg
-             && memory_address_p (GET_MODE (x),
-                                  SET_SRC (PATTERN (NEXT_INSN (seq)))))
+             && memory_address_addr_space_p
+                  (GET_MODE (x), SET_SRC (PATTERN (NEXT_INSN (seq))),
+                   MEM_ADDR_SPACE (x)))
            {
              rtx src1 = SET_SRC (PATTERN (seq));
              rtx src2 = SET_SRC (PATTERN (NEXT_INSN (seq)));
@@ -4263,7 +4560,8 @@ find_split_point (rtx *loc, rtx insn)
       /* If we have a PLUS whose first operand is complex, try computing it
          separately by making a split there.  */
       if (GET_CODE (XEXP (x, 0)) == PLUS
-          && ! memory_address_p (GET_MODE (x), XEXP (x, 0))
+          && ! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
+                                           MEM_ADDR_SPACE (x))
           && ! OBJECT_P (XEXP (XEXP (x, 0), 0))
           && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
                 && OBJECT_P (SUBREG_REG (XEXP (XEXP (x, 0), 0)))))
@@ -4287,12 +4585,12 @@ find_split_point (rtx *loc, rtx insn)
 #endif
 
       /* See if we can split SET_SRC as it stands.  */
-      split = find_split_point (&SET_SRC (x), insn);
+      split = find_split_point (&SET_SRC (x), insn, true);
       if (split && split != &SET_SRC (x))
        return split;
 
       /* See if we can split SET_DEST as it stands.  */
-      split = find_split_point (&SET_DEST (x), insn);
+      split = find_split_point (&SET_DEST (x), insn, false);
       if (split && split != &SET_DEST (x))
        return split;
 
@@ -4336,7 +4634,7 @@ find_split_point (rtx *loc, rtx insn)
 
          SUBST (SET_DEST (x), dest);
 
-         split = find_split_point (&SET_SRC (x), insn);
+         split = find_split_point (&SET_SRC (x), insn, true);
          if (split && split != &SET_SRC (x))
            return split;
        }
@@ -4372,7 +4670,7 @@ find_split_point (rtx *loc, rtx insn)
              if (extraction != 0)
                {
                  SUBST (SET_SRC (x), extraction);
-                 return find_split_point (loc, insn);
+                 return find_split_point (loc, insn, false);
                }
            }
          break;
@@ -4394,7 +4692,7 @@ find_split_point (rtx *loc, rtx insn)
                                                    XEXP (SET_SRC (x), 0),
                                                    GEN_INT (pos))));
 
-             split = find_split_point (&SET_SRC (x), insn);
+             split = find_split_point (&SET_SRC (x), insn, true);
              if (split && split != &SET_SRC (x))
                return split;
            }
@@ -4453,7 +4751,7 @@ find_split_point (rtx *loc, rtx insn)
                                   GEN_INT (pos)),
                                  GEN_INT (((HOST_WIDE_INT) 1 << len) - 1)));
 
-             split = find_split_point (&SET_SRC (x), insn);
+             split = find_split_point (&SET_SRC (x), insn, true);
              if (split && split != &SET_SRC (x))
                return split;
            }
@@ -4468,7 +4766,7 @@ find_split_point (rtx *loc, rtx insn)
                                               - len - pos)),
                      GEN_INT (GET_MODE_BITSIZE (mode) - len)));
 
-             split = find_split_point (&SET_SRC (x), insn);
+             split = find_split_point (&SET_SRC (x), insn, true);
              if (split && split != &SET_SRC (x))
                return split;
            }
@@ -4508,7 +4806,7 @@ find_split_point (rtx *loc, rtx insn)
                                              GET_MODE (x),
                                              XEXP (XEXP (x, 0), 0),
                                              XEXP (XEXP (x, 1), 0))));
-         return find_split_point (loc, insn);
+         return find_split_point (loc, insn, set_src);
        }
 
       /* Many RISC machines have a large set of logical insns.  If the
@@ -4522,6 +4820,31 @@ find_split_point (rtx *loc, rtx insn)
        }
       break;
 
+    case PLUS:
+    case MINUS:
+      /* Canonicalization can produce (minus A (mult B C)), where C is a
+        constant.  It may be better to try splitting (plus (mult B -C) A)
+        instead if this isn't a multiply by a power of two.  */
+      if (set_src && code == MINUS && GET_CODE (XEXP (x, 1)) == MULT
+         && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
+         && exact_log2 (INTVAL (XEXP (XEXP (x, 1), 1))) < 0)
+       {
+         enum machine_mode mode = GET_MODE (x);
+         unsigned HOST_WIDE_INT this_int = INTVAL (XEXP (XEXP (x, 1), 1));
+         HOST_WIDE_INT other_int = trunc_int_for_mode (-this_int, mode);
+         SUBST (*loc, gen_rtx_PLUS (mode, gen_rtx_MULT (mode,
+                                                        XEXP (XEXP (x, 1), 0),
+                                                        GEN_INT (other_int)),
+                                    XEXP (x, 0)));
+         return find_split_point (loc, insn, set_src);
+       }
+
+      /* Split at a multiply-accumulate instruction.  However if this is
+         the SET_SRC, we likely do not have such an instruction and it's
+         worthless to try this split.  */
+      if (!set_src && GET_CODE (XEXP (x, 0)) == MULT)
+        return loc;
+
     default:
       break;
     }
@@ -4531,7 +4854,7 @@ find_split_point (rtx *loc, rtx insn)
     {
     case RTX_BITFIELD_OPS:             /* This is ZERO_EXTRACT and SIGN_EXTRACT.  */
     case RTX_TERNARY:
-      split = find_split_point (&XEXP (x, 2), insn);
+      split = find_split_point (&XEXP (x, 2), insn, false);
       if (split)
        return split;
       /* ... fall through ...  */
@@ -4539,7 +4862,7 @@ find_split_point (rtx *loc, rtx insn)
     case RTX_COMM_ARITH:
     case RTX_COMPARE:
     case RTX_COMM_COMPARE:
-      split = find_split_point (&XEXP (x, 1), insn);
+      split = find_split_point (&XEXP (x, 1), insn, false);
       if (split)
        return split;
       /* ... fall through ...  */
@@ -4549,7 +4872,7 @@ find_split_point (rtx *loc, rtx insn)
       if (GET_CODE (x) != AND && GET_CODE (XEXP (x, 0)) == AND)
        return &XEXP (x, 0);
 
-      split = find_split_point (&XEXP (x, 0), insn);
+      split = find_split_point (&XEXP (x, 0), insn, false);
       if (split)
        return split;
       return loc;
@@ -5176,6 +5499,10 @@ combine_simplify_rtx (rtx x, enum machine_mode op0_mode, int in_dest)
               force_to_mode (XEXP (x, 0), GET_MODE (XEXP (x, 0)),
                              GET_MODE_MASK (mode), 0));
 
+      /* We can truncate a constant value and return it.  */
+      if (CONST_INT_P (XEXP (x, 0)))
+       return gen_int_mode (INTVAL (XEXP (x, 0)), mode);
+
       /* Similarly to what we do in simplify-rtx.c, a truncate of a register
         whose value is a comparison can be replaced with a subreg if
         STORE_FLAG_VALUE permits.  */
@@ -5292,7 +5619,7 @@ combine_simplify_rtx (rtx x, enum machine_mode op0_mode, int in_dest)
        }
 
       /* Try simplify a*(b/c) as (a*b)/c.  */
-      if (FLOAT_MODE_P (mode) && flag_associative_math 
+      if (FLOAT_MODE_P (mode) && flag_associative_math
          && GET_CODE (XEXP (x, 0)) == DIV)
        {
          rtx tem = simplify_binary_operation (MULT, mode,
@@ -6795,8 +7122,10 @@ make_extraction (enum machine_mode mode, rtx inner, HOST_WIDE_INT pos,
       if (mode == tmode)
        return new_rtx;
 
-      if (CONST_INT_P (new_rtx))
-       return gen_int_mode (INTVAL (new_rtx), mode);
+      if (CONST_INT_P (new_rtx)
+         || GET_CODE (new_rtx) == CONST_DOUBLE)
+       return simplify_unary_operation (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
+                                        mode, new_rtx, tmode);
 
       /* If we know that no extraneous bits are set, and that the high
         bit is not set, convert the extraction to the cheaper of
@@ -7125,7 +7454,9 @@ make_compound_operation (rtx x, enum rtx_code in_code)
      address, we stay there.  If we have a comparison, set to COMPARE,
      but once inside, go back to our default of SET.  */
 
-  next_code = (code == MEM || code == PLUS || code == MINUS ? MEM
+  next_code = (code == MEM ? MEM
+              : ((code == PLUS || code == MINUS)
+                 && SCALAR_INT_MODE_P (mode)) ? MEM
               : ((code == COMPARE || COMPARISON_P (x))
                  && XEXP (x, 1) == const0_rtx) ? COMPARE
               : in_code == COMPARE ? SET : in_code);
@@ -7142,13 +7473,79 @@ make_compound_operation (rtx x, enum rtx_code in_code)
          && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
          && INTVAL (XEXP (x, 1)) >= 0)
        {
+         HOST_WIDE_INT count = INTVAL (XEXP (x, 1));
+         HOST_WIDE_INT multval = (HOST_WIDE_INT) 1 << count;
+
          new_rtx = make_compound_operation (XEXP (x, 0), next_code);
-         new_rtx = gen_rtx_MULT (mode, new_rtx,
-                             GEN_INT ((HOST_WIDE_INT) 1
-                                      << INTVAL (XEXP (x, 1))));
+         if (GET_CODE (new_rtx) == NEG)
+           {
+             new_rtx = XEXP (new_rtx, 0);
+             multval = -multval;
+           }
+         multval = trunc_int_for_mode (multval, mode);
+         new_rtx = gen_rtx_MULT (mode, new_rtx, GEN_INT (multval));
        }
       break;
 
+    case PLUS:
+      lhs = XEXP (x, 0);
+      rhs = XEXP (x, 1);
+      lhs = make_compound_operation (lhs, next_code);
+      rhs = make_compound_operation (rhs, next_code);
+      if (GET_CODE (lhs) == MULT && GET_CODE (XEXP (lhs, 0)) == NEG
+         && SCALAR_INT_MODE_P (mode))
+       {
+         tem = simplify_gen_binary (MULT, mode, XEXP (XEXP (lhs, 0), 0),
+                                    XEXP (lhs, 1));
+         new_rtx = simplify_gen_binary (MINUS, mode, rhs, tem);
+       }
+      else if (GET_CODE (lhs) == MULT
+              && (CONST_INT_P (XEXP (lhs, 1)) && INTVAL (XEXP (lhs, 1)) < 0))
+       {
+         tem = simplify_gen_binary (MULT, mode, XEXP (lhs, 0),
+                                    simplify_gen_unary (NEG, mode,
+                                                        XEXP (lhs, 1),
+                                                        mode));
+         new_rtx = simplify_gen_binary (MINUS, mode, rhs, tem);
+       }
+      else
+       {
+         SUBST (XEXP (x, 0), lhs);
+         SUBST (XEXP (x, 1), rhs);
+         goto maybe_swap;
+       }
+      x = gen_lowpart (mode, new_rtx);
+      goto maybe_swap;
+
+    case MINUS:
+      lhs = XEXP (x, 0);
+      rhs = XEXP (x, 1);
+      lhs = make_compound_operation (lhs, next_code);
+      rhs = make_compound_operation (rhs, next_code);
+      if (GET_CODE (rhs) == MULT && GET_CODE (XEXP (rhs, 0)) == NEG
+         && SCALAR_INT_MODE_P (mode))
+       {
+         tem = simplify_gen_binary (MULT, mode, XEXP (XEXP (rhs, 0), 0),
+                                    XEXP (rhs, 1));
+         new_rtx = simplify_gen_binary (PLUS, mode, tem, lhs);
+       }
+      else if (GET_CODE (rhs) == MULT
+              && (CONST_INT_P (XEXP (rhs, 1)) && INTVAL (XEXP (rhs, 1)) < 0))
+       {
+         tem = simplify_gen_binary (MULT, mode, XEXP (rhs, 0),
+                                    simplify_gen_unary (NEG, mode,
+                                                        XEXP (rhs, 1),
+                                                        mode));
+         new_rtx = simplify_gen_binary (PLUS, mode, tem, lhs);
+       }
+      else
+       {
+         SUBST (XEXP (x, 0), lhs);
+         SUBST (XEXP (x, 1), rhs);
+         return x;
+       }
+      return gen_lowpart (mode, new_rtx);
+
     case AND:
       /* If the second operand is not a constant, we can't do anything
         with it.  */
@@ -7310,23 +7707,21 @@ make_compound_operation (rtx x, enum rtx_code in_code)
       /* Call ourselves recursively on the inner expression.  If we are
         narrowing the object and it has a different RTL code from
         what it originally did, do this SUBREG as a force_to_mode.  */
-
-      tem = make_compound_operation (SUBREG_REG (x), in_code);
-
       {
-       rtx simplified;
-       simplified = simplify_subreg (GET_MODE (x), tem, GET_MODE (tem),
-                                     SUBREG_BYTE (x));
+       rtx inner = SUBREG_REG (x), simplified;
+       
+       tem = make_compound_operation (inner, in_code);
 
+       simplified
+         = simplify_subreg (mode, tem, GET_MODE (inner), SUBREG_BYTE (x));
        if (simplified)
          tem = simplified;
 
-       if (GET_CODE (tem) != GET_CODE (SUBREG_REG (x))
-           && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (tem))
+       if (GET_CODE (tem) != GET_CODE (inner)
+           && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (inner))
            && subreg_lowpart_p (x))
          {
-           rtx newer = force_to_mode (tem, mode, ~(HOST_WIDE_INT) 0,
-                                      0);
+           rtx newer = force_to_mode (tem, mode, ~(HOST_WIDE_INT) 0, 0);
 
            /* If we have something other than a SUBREG, we might have
               done an expansion, so rerun ourselves.  */
@@ -7334,9 +7729,16 @@ make_compound_operation (rtx x, enum rtx_code in_code)
              newer = make_compound_operation (newer, in_code);
 
            /* force_to_mode can expand compounds.  If it just re-expanded the
-              compound use gen_lowpart instead to convert to the desired
-              mode.  */
-           if (rtx_equal_p (newer, x))
+              compound, use gen_lowpart to convert to the desired mode.  */
+           if (rtx_equal_p (newer, x)
+               /* Likewise if it re-expanded the compound only partially.
+                  This happens for SUBREG of ZERO_EXTRACT if they extract
+                  the same number of bits.  */
+               || (GET_CODE (newer) == SUBREG
+                   && (GET_CODE (SUBREG_REG (newer)) == LSHIFTRT
+                       || GET_CODE (SUBREG_REG (newer)) == ASHIFTRT)
+                   && GET_CODE (inner) == AND
+                   && rtx_equal_p (SUBREG_REG (newer), XEXP (inner, 0))))
              return gen_lowpart (GET_MODE (x), tem);
 
            return newer;
@@ -7372,6 +7774,7 @@ make_compound_operation (rtx x, enum rtx_code in_code)
          SUBST (XVECEXP (x, i, j), new_rtx);
        }
 
+ maybe_swap:
   /* If this is a commutative operation, the changes to the operands
      may have made it noncanonical.  */
   if (COMMUTATIVE_ARITH_P (x)
@@ -7396,7 +7799,7 @@ static int
 get_pos_from_mask (unsigned HOST_WIDE_INT m, unsigned HOST_WIDE_INT *plen)
 {
   /* Get the bit number of the first 1 bit from the right, -1 if none.  */
-  int pos = exact_log2 (m & -m);
+  int pos = m ? ctz_hwi (m) : -1;
   int len = 0;
 
   if (pos >= 0)
@@ -8810,6 +9213,12 @@ distribute_and_simplify_rtx (rtx x, int n)
   enum rtx_code outer_code, inner_code;
   rtx decomposed, distributed, inner_op0, inner_op1, new_op0, new_op1, tmp;
 
+  /* Distributivity is not true for floating point as it can change the
+     value.  So we don't do it unless -funsafe-math-optimizations.  */
+  if (FLOAT_MODE_P (GET_MODE (x))
+      && ! flag_unsafe_math_optimizations)
+    return NULL_RTX;
+
   decomposed = XEXP (x, n);
   if (!ARITHMETIC_P (decomposed))
     return NULL_RTX;
@@ -9500,7 +9909,9 @@ simplify_shift_const_1 (enum rtx_code code, enum machine_mode result_mode,
                  > GET_MODE_SIZE (GET_MODE (varop)))
              && (unsigned int) ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
                                  + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
-                == mode_words)
+                == mode_words
+             && GET_MODE_CLASS (GET_MODE (varop)) == MODE_INT
+             && GET_MODE_CLASS (GET_MODE (SUBREG_REG (varop))) == MODE_INT)
            {
              varop = SUBREG_REG (varop);
              if (GET_MODE_SIZE (GET_MODE (varop)) > GET_MODE_SIZE (mode))
@@ -10189,7 +10600,7 @@ recog_for_combine (rtx *pnewpat, rtx insn, rtx *pnotes)
          if (REG_P (XEXP (XVECEXP (newpat, 0, i), 0))
              && ! reg_dead_at_p (XEXP (XVECEXP (newpat, 0, i), 0), insn))
            return -1;
-         if (GET_CODE (XEXP (XVECEXP (newpat, 0, i), 0)) != SCRATCH) 
+         if (GET_CODE (XEXP (XVECEXP (newpat, 0, i), 0)) != SCRATCH)
            {
              gcc_assert (REG_P (XEXP (XVECEXP (newpat, 0, i), 0)));
              notes = alloc_reg_note (REG_UNUSED,
@@ -11481,6 +11892,22 @@ simplify_comparison (enum rtx_code code, rtx *pop0, rtx *pop1)
        {
          int zero_extended;
 
+         /* If this is a test for negative, we can make an explicit
+            test of the sign bit.  Test this first so we can use
+            a paradoxical subreg to extend OP0.  */
+
+         if (op1 == const0_rtx && (code == LT || code == GE)
+             && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
+           {
+             op0 = simplify_gen_binary (AND, tmode,
+                                        gen_lowpart (tmode, op0),
+                                        GEN_INT ((HOST_WIDE_INT) 1
+                                                 << (GET_MODE_BITSIZE (mode)
+                                                     - 1)));
+             code = (code == LT) ? NE : EQ;
+             break;
+           }
+
          /* If the only nonzero bits in OP0 and OP1 are those in the
             narrower mode and this is an equality or unsigned comparison,
             we can use the wider mode.  Similarly for sign-extended
@@ -11511,27 +11938,20 @@ simplify_comparison (enum rtx_code code, rtx *pop0, rtx *pop1)
                                                        XEXP (op0, 0)),
                                           gen_lowpart (tmode,
                                                        XEXP (op0, 1)));
-
-             op0 = gen_lowpart (tmode, op0);
-             if (zero_extended && CONST_INT_P (op1))
-               op1 = GEN_INT (INTVAL (op1) & GET_MODE_MASK (mode));
-             op1 = gen_lowpart (tmode, op1);
-             break;
-           }
-
-         /* If this is a test for negative, we can make an explicit
-            test of the sign bit.  */
-
-         if (op1 == const0_rtx && (code == LT || code == GE)
-             && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
-           {
-             op0 = simplify_gen_binary (AND, tmode,
-                                        gen_lowpart (tmode, op0),
-                                        GEN_INT ((HOST_WIDE_INT) 1
-                                                 << (GET_MODE_BITSIZE (mode)
-                                                     - 1)));
-             code = (code == LT) ? NE : EQ;
-             break;
+             else
+               {
+                 if (zero_extended)
+                   {
+                     op0 = simplify_gen_unary (ZERO_EXTEND, tmode, op0, mode);
+                     op1 = simplify_gen_unary (ZERO_EXTEND, tmode, op1, mode);
+                   }
+                 else
+                   {
+                     op0 = simplify_gen_unary (SIGN_EXTEND, tmode, op0, mode);
+                     op1 = simplify_gen_unary (SIGN_EXTEND, tmode, op1, mode);
+                   }
+                 break;
+               }
            }
        }
 
@@ -11751,12 +12171,10 @@ record_value_for_reg (rtx reg, rtx insn, rtx value)
      case, we must replace it with (clobber (const_int 0)) to prevent
      infinite loops.  */
   rsp = VEC_index (reg_stat_type, reg_stat, regno);
-  if (value && ! get_last_value_validate (&value, insn,
-                                         rsp->last_set_label, 0))
+  if (value && !get_last_value_validate (&value, insn, label_tick, 0))
     {
       value = copy_rtx (value);
-      if (! get_last_value_validate (&value, insn,
-                                    rsp->last_set_label, 1))
+      if (!get_last_value_validate (&value, insn, label_tick, 1))
        value = 0;
     }
 
@@ -12048,15 +12466,14 @@ check_promoted_subreg (rtx insn, rtx x)
     }
 }
 \f
-/* Utility routine for the following function.  Verify that all the registers
-   mentioned in *LOC are valid when *LOC was part of a value set when
-   label_tick == TICK.  Return 0 if some are not.
-
-   If REPLACE is nonzero, replace the invalid reference with
-   (clobber (const_int 0)) and return 1.  This replacement is useful because
-   we often can get useful information about the form of a value (e.g., if
-   it was produced by a shift that always produces -1 or 0) even though
-   we don't know exactly what registers it was produced from.  */
+/* Verify that all the registers and memory references mentioned in *LOC are
+   still valid.  *LOC was part of a value set in INSN when label_tick was
+   equal to TICK.  Return 0 if some are not.  If REPLACE is nonzero, replace
+   the invalid references with (clobber (const_int 0)) and return 1.  This
+   replacement is useful because we often can get useful information about
+   the form of a value (e.g., if it was produced by a shift that always
+   produces -1 or 0) even though we don't know exactly what registers it
+   was produced from.  */
 
 static int
 get_last_value_validate (rtx *loc, rtx insn, int tick, int replace)
@@ -12092,11 +12509,12 @@ get_last_value_validate (rtx *loc, rtx insn, int tick, int replace)
 
       return 1;
     }
-  /* If this is a memory reference, make sure that there were
-     no stores after it that might have clobbered the value.  We don't
-     have alias info, so we assume any store invalidates it.  */
+  /* If this is a memory reference, make sure that there were no stores after
+     it that might have clobbered the value.  We don't have alias info, so we
+     assume any store invalidates it.  Moreover, we only have local UIDs, so
+     we also assume that there were stores in the intervening basic blocks.  */
   else if (MEM_P (x) && !MEM_READONLY_P (x)
-          && DF_INSN_LUID (insn) <= mem_last_set)
+          && (tick != label_tick || DF_INSN_LUID (insn) <= mem_last_set))
     {
       if (replace)
        *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
@@ -12206,16 +12624,14 @@ get_last_value (const_rtx x)
     return 0;
 
   /* If the value has all its registers valid, return it.  */
-  if (get_last_value_validate (&value, rsp->last_set,
-                              rsp->last_set_label, 0))
+  if (get_last_value_validate (&value, rsp->last_set, rsp->last_set_label, 0))
     return value;
 
   /* Otherwise, make a copy and replace any invalid register with
      (clobber (const_int 0)).  If that fails for some reason, return 0.  */
 
   value = copy_rtx (value);
-  if (get_last_value_validate (&value, rsp->last_set,
-                              rsp->last_set_label, 1))
+  if (get_last_value_validate (&value, rsp->last_set, rsp->last_set_label, 1))
     return value;
 
   return 0;
@@ -12670,29 +13086,6 @@ reg_bitfield_target_p (rtx x, rtx body)
 
   return 0;
 }
-
-/* Return the next insn after INSN that is neither a NOTE nor a
-   DEBUG_INSN.  This routine does not look inside SEQUENCEs.  */
-
-static rtx
-next_nonnote_nondebug_insn (rtx insn)
-{
-  while (insn)
-    {
-      insn = NEXT_INSN (insn);
-      if (insn == 0)
-       break;
-      if (NOTE_P (insn))
-       continue;
-      if (DEBUG_INSN_P (insn))
-       continue;
-      break;
-    }
-
-  return insn;
-}
-
-
 \f
 /* Given a chain of REG_NOTES originally from FROM_INSN, try to place them
    as appropriate.  I3 and I2 are the insns resulting from the combination
@@ -12707,7 +13100,7 @@ next_nonnote_nondebug_insn (rtx insn)
 
 static void
 distribute_notes (rtx notes, rtx from_insn, rtx i3, rtx i2, rtx elim_i2,
-                 rtx elim_i1)
+                 rtx elim_i1, rtx elim_i0)
 {
   rtx note, next_note;
   rtx tem;
@@ -12749,7 +13142,7 @@ distribute_notes (rtx notes, rtx from_insn, rtx i3, rtx i2, rtx elim_i2,
            place = i2;
          else
            {
-             gcc_assert (flag_non_call_exceptions);
+             gcc_assert (cfun->can_throw_non_call_exceptions);
              if (may_trap_p (i3))
                place = i3;
              else if (i2 && may_trap_p (i2))
@@ -12953,7 +13346,8 @@ distribute_notes (rtx notes, rtx from_insn, rtx i3, rtx i2, rtx elim_i2,
                        && !(i2mod
                             && reg_overlap_mentioned_p (XEXP (note, 0),
                                                         i2mod_old_rhs)))
-                      || rtx_equal_p (XEXP (note, 0), elim_i1))
+                      || rtx_equal_p (XEXP (note, 0), elim_i1)
+                      || rtx_equal_p (XEXP (note, 0), elim_i0))
                break;
              tem = i3;
            }
@@ -13020,7 +13414,7 @@ distribute_notes (rtx notes, rtx from_insn, rtx i3, rtx i2, rtx elim_i2,
                          REG_NOTES (tem) = NULL;
 
                          distribute_notes (old_notes, tem, tem, NULL_RTX,
-                                           NULL_RTX, NULL_RTX);
+                                           NULL_RTX, NULL_RTX, NULL_RTX);
                          distribute_links (LOG_LINKS (tem));
 
                          SET_INSN_DELETED (tem);
@@ -13037,7 +13431,7 @@ distribute_notes (rtx notes, rtx from_insn, rtx i3, rtx i2, rtx elim_i2,
 
                              distribute_notes (old_notes, cc0_setter,
                                                cc0_setter, NULL_RTX,
-                                               NULL_RTX, NULL_RTX);
+                                               NULL_RTX, NULL_RTX, NULL_RTX);
                              distribute_links (LOG_LINKS (cc0_setter));
 
                              SET_INSN_DELETED (cc0_setter);
@@ -13157,7 +13551,8 @@ distribute_notes (rtx notes, rtx from_insn, rtx i3, rtx i2, rtx elim_i2,
                                                             NULL_RTX);
 
                              distribute_notes (new_note, place, place,
-                                               NULL_RTX, NULL_RTX, NULL_RTX);
+                                               NULL_RTX, NULL_RTX, NULL_RTX,
+                                               NULL_RTX);
                            }
                          else if (! refers_to_regno_p (i, i + 1,
                                                        PATTERN (place), 0)