re PR target/64833 ([SH]: Error: pcrel too far when compiling imagemagick and graphic...
[gcc.git] / gcc / regcprop.c
index 893751886ebcf78d8d0953145b3ff00d327dfb07..cca495e0952d5b0eb7463bd7ead998d12dcc9942 100644 (file)
@@ -1,6 +1,5 @@
 /* Copy propagation on hard registers for the GNU compiler.
-   Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
-   Free Software Foundation, Inc.
+   Copyright (C) 2000-2015 Free Software Foundation, Inc.
 
    This file is part of GCC.
 
 #include "regs.h"
 #include "addresses.h"
 #include "hard-reg-set.h"
+#include "predict.h"
+#include "function.h"
+#include "dominance.h"
+#include "cfg.h"
 #include "basic-block.h"
 #include "reload.h"
-#include "output.h"
-#include "function.h"
 #include "recog.h"
 #include "flags.h"
-#include "toplev.h"
+#include "diagnostic-core.h"
 #include "obstack.h"
-#include "timevar.h"
 #include "tree-pass.h"
 #include "df.h"
+#include "rtl-iter.h"
+#include "emit-rtl.h"
 
 /* The following code does forward propagation of hard register copies.
    The object is to eliminate as many dependencies as possible, so that
    up some silly register allocation decisions made by reload.  This
    code may be obsoleted by a new register allocator.  */
 
+/* DEBUG_INSNs aren't changed right away, as doing so might extend the
+   lifetime of a register and get the DEBUG_INSN subsequently reset.
+   So they are queued instead, and updated only when the register is
+   used in some subsequent real insn before it is set.  */
+struct queued_debug_insn_change
+{
+  struct queued_debug_insn_change *next;
+  rtx_insn *insn;
+  rtx *loc;
+  rtx new_rtx;
+
+  /* Pool allocation new operator.  */
+  inline void *operator new (size_t)
+  {
+    return pool.allocate ();
+  }
+
+  /* Delete operator utilizing pool allocation.  */
+  inline void operator delete (void *ptr)
+  {
+    pool.remove ((queued_debug_insn_change *) ptr);
+  }
+
+  /* Memory allocation pool.  */
+  static pool_allocator<queued_debug_insn_change> pool;
+};
+
 /* For each register, we have a list of registers that contain the same
    value.  The OLDEST_REGNO field points to the head of the list, and
    the NEXT_REGNO field runs through the list.  The MODE field indicates
 
 struct value_data_entry
 {
-  enum machine_mode mode;
+  machine_mode mode;
   unsigned int oldest_regno;
   unsigned int next_regno;
+  struct queued_debug_insn_change *debug_insn_changes;
 };
 
 struct value_data
 {
   struct value_data_entry e[FIRST_PSEUDO_REGISTER];
   unsigned int max_value_regs;
+  unsigned int n_debug_insn_changes;
 };
 
+pool_allocator<queued_debug_insn_change> queued_debug_insn_change::pool
+  ("debug insn changes pool", 256);
+
+static bool skip_debug_insn_p;
+
 static void kill_value_one_regno (unsigned, struct value_data *);
 static void kill_value_regno (unsigned, unsigned, struct value_data *);
-static void kill_value (rtx, struct value_data *);
-static void set_value_regno (unsigned, enum machine_mode, struct value_data *);
+static void kill_value (const_rtx, struct value_data *);
+static void set_value_regno (unsigned, machine_mode, struct value_data *);
 static void init_value_data (struct value_data *);
 static void kill_clobbered_value (rtx, const_rtx, void *);
 static void kill_set_value (rtx, const_rtx, void *);
-static int kill_autoinc_value (rtx *, void *);
 static void copy_value (rtx, rtx, struct value_data *);
-static bool mode_change_ok (enum machine_mode, enum machine_mode,
+static bool mode_change_ok (machine_mode, machine_mode,
                            unsigned int);
-static rtx maybe_mode_change (enum machine_mode, enum machine_mode,
-                             enum machine_mode, unsigned int, unsigned int);
+static rtx maybe_mode_change (machine_mode, machine_mode,
+                             machine_mode, unsigned int, unsigned int);
 static rtx find_oldest_value_reg (enum reg_class, rtx, struct value_data *);
-static bool replace_oldest_value_reg (rtx *, enum reg_class, rtx,
+static bool replace_oldest_value_reg (rtx *, enum reg_class, rtx_insn *,
                                      struct value_data *);
 static bool replace_oldest_value_addr (rtx *, enum reg_class,
-                                      enum machine_mode, rtx,
-                                      struct value_data *);
-static bool replace_oldest_value_mem (rtx, rtx, struct value_data *);
+                                      machine_mode, addr_space_t,
+                                      rtx_insn *, struct value_data *);
+static bool replace_oldest_value_mem (rtx, rtx_insn *, struct value_data *);
 static bool copyprop_hardreg_forward_1 (basic_block, struct value_data *);
 extern void debug_value_data (struct value_data *);
 #ifdef ENABLE_CHECKING
 static void validate_value_data (struct value_data *);
 #endif
 
+/* Free all queued updates for DEBUG_INSNs that change some reg to
+   register REGNO.  */
+
+static void
+free_debug_insn_changes (struct value_data *vd, unsigned int regno)
+{
+  struct queued_debug_insn_change *cur, *next;
+  for (cur = vd->e[regno].debug_insn_changes; cur; cur = next)
+    {
+      next = cur->next;
+      --vd->n_debug_insn_changes;
+      delete cur;
+    }
+  vd->e[regno].debug_insn_changes = NULL;
+}
+
 /* Kill register REGNO.  This involves removing it from any value
    lists, and resetting the value mode to VOIDmode.  This is only a
    helper function; it does not handle any hard registers overlapping
@@ -118,6 +169,8 @@ kill_value_one_regno (unsigned int regno, struct value_data *vd)
   vd->e[regno].mode = VOIDmode;
   vd->e[regno].oldest_regno = regno;
   vd->e[regno].next_regno = INVALID_REGNUM;
+  if (vd->e[regno].debug_insn_changes)
+    free_debug_insn_changes (vd, regno);
 
 #ifdef ENABLE_CHECKING
   validate_value_data (vd);
@@ -158,30 +211,22 @@ kill_value_regno (unsigned int regno, unsigned int nregs,
    so that we mind the mode the register is in.  */
 
 static void
-kill_value (rtx x, struct value_data *vd)
+kill_value (const_rtx x, struct value_data *vd)
 {
-  rtx orig_rtx = x;
-
   if (GET_CODE (x) == SUBREG)
     {
-      x = simplify_subreg (GET_MODE (x), SUBREG_REG (x),
-                          GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
-      if (x == NULL_RTX)
-       x = SUBREG_REG (orig_rtx);
+      rtx tmp = simplify_subreg (GET_MODE (x), SUBREG_REG (x),
+                                GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
+      x = tmp ? tmp : SUBREG_REG (x);
     }
   if (REG_P (x))
-    {
-      unsigned int regno = REGNO (x);
-      unsigned int n = hard_regno_nregs[regno][GET_MODE (x)];
-
-      kill_value_regno (regno, n, vd);
-    }
+    kill_value_regno (REGNO (x), REG_NREGS (x), vd);
 }
 
 /* Remember that REGNO is valid in MODE.  */
 
 static void
-set_value_regno (unsigned int regno, enum machine_mode mode,
+set_value_regno (unsigned int regno, machine_mode mode,
                 struct value_data *vd)
 {
   unsigned int nregs;
@@ -204,8 +249,10 @@ init_value_data (struct value_data *vd)
       vd->e[i].mode = VOIDmode;
       vd->e[i].oldest_regno = i;
       vd->e[i].next_regno = INVALID_REGNUM;
+      vd->e[i].debug_insn_changes = NULL;
     }
   vd->max_value_regs = 0;
+  vd->n_debug_insn_changes = 0;
 }
 
 /* Called through note_stores.  If X is clobbered, kill its value.  */
@@ -218,40 +265,48 @@ kill_clobbered_value (rtx x, const_rtx set, void *data)
     kill_value (x, vd);
 }
 
+/* A structure passed as data to kill_set_value through note_stores.  */
+struct kill_set_value_data
+{
+  struct value_data *vd;
+  rtx ignore_set_reg;
+};
+  
 /* Called through note_stores.  If X is set, not clobbered, kill its
    current value and install it as the root of its own value list.  */
 
 static void
 kill_set_value (rtx x, const_rtx set, void *data)
 {
-  struct value_data *const vd = (struct value_data *) data;
+  struct kill_set_value_data *ksvd = (struct kill_set_value_data *) data;
+  if (rtx_equal_p (x, ksvd->ignore_set_reg))
+    return;
   if (GET_CODE (set) != CLOBBER)
     {
-      kill_value (x, vd);
+      kill_value (x, ksvd->vd);
       if (REG_P (x))
-       set_value_regno (REGNO (x), GET_MODE (x), vd);
+       set_value_regno (REGNO (x), GET_MODE (x), ksvd->vd);
     }
 }
 
-/* Called through for_each_rtx.  Kill any register used as the base of an
-   auto-increment expression, and install that register as the root of its
-   own value list.  */
+/* Kill any register used in X as the base of an auto-increment expression,
+   and install that register as the root of its own value list.  */
 
-static int
-kill_autoinc_value (rtx *px, void *data)
+static void
+kill_autoinc_value (rtx_insn *insn, struct value_data *vd)
 {
-  rtx x = *px;
-  struct value_data *const vd = (struct value_data *) data;
-
-  if (GET_RTX_CLASS (GET_CODE (x)) == RTX_AUTOINC)
+  subrtx_iterator::array_type array;
+  FOR_EACH_SUBRTX (iter, array, PATTERN (insn), NONCONST)
     {
-      x = XEXP (x, 0);
-      kill_value (x, vd);
-      set_value_regno (REGNO (x), Pmode, vd);
-      return -1;
+      const_rtx x = *iter;
+      if (GET_RTX_CLASS (GET_CODE (x)) == RTX_AUTOINC)
+       {
+         x = XEXP (x, 0);
+         kill_value (x, vd);
+         set_value_regno (REGNO (x), GET_MODE (x), vd);
+         iter.skip_subrtxes ();
+       }
     }
-
-  return 0;
 }
 
 /* Assert that SRC has been copied to DEST.  Adjust the data structures
@@ -286,8 +341,8 @@ copy_value (rtx dest, rtx src, struct value_data *vd)
     return;
 
   /* If SRC and DEST overlap, don't record anything.  */
-  dn = hard_regno_nregs[dr][GET_MODE (dest)];
-  sn = hard_regno_nregs[sr][GET_MODE (dest)];
+  dn = REG_NREGS (dest);
+  sn = REG_NREGS (src);
   if ((dr > sr && dr < sr + sn)
       || (sr > dr && sr < dr + dn))
     return;
@@ -340,7 +395,7 @@ copy_value (rtx dest, rtx src, struct value_data *vd)
 /* Return true if a mode change from ORIG to NEW is allowed for REGNO.  */
 
 static bool
-mode_change_ok (enum machine_mode orig_mode, enum machine_mode new_mode,
+mode_change_ok (machine_mode orig_mode, machine_mode new_mode,
                unsigned int regno ATTRIBUTE_UNUSED)
 {
   if (GET_MODE_SIZE (orig_mode) < GET_MODE_SIZE (new_mode))
@@ -359,8 +414,8 @@ mode_change_ok (enum machine_mode orig_mode, enum machine_mode new_mode,
    Return a NEW_MODE rtx for REGNO if that's OK, otherwise return NULL_RTX.  */
 
 static rtx
-maybe_mode_change (enum machine_mode orig_mode, enum machine_mode copy_mode,
-                  enum machine_mode new_mode, unsigned int regno,
+maybe_mode_change (machine_mode orig_mode, machine_mode copy_mode,
+                  machine_mode new_mode, unsigned int regno,
                   unsigned int copy_regno ATTRIBUTE_UNUSED)
 {
   if (GET_MODE_SIZE (copy_mode) < GET_MODE_SIZE (orig_mode)
@@ -368,7 +423,7 @@ maybe_mode_change (enum machine_mode orig_mode, enum machine_mode copy_mode,
     return NULL_RTX;
 
   if (orig_mode == new_mode)
-    return gen_rtx_raw_REG (new_mode, regno);
+    return gen_raw_REG (new_mode, regno);
   else if (mode_change_ok (orig_mode, new_mode, regno))
     {
       int copy_nregs = hard_regno_nregs[copy_regno][copy_mode];
@@ -382,10 +437,9 @@ maybe_mode_change (enum machine_mode orig_mode, enum machine_mode copy_mode,
 
       offset = ((WORDS_BIG_ENDIAN ? wordoffset : 0)
                + (BYTES_BIG_ENDIAN ? byteoffset : 0));
-      return gen_rtx_raw_REG (new_mode,
-                             regno + subreg_regno_offset (regno, orig_mode,
-                                                          offset,
-                                                          new_mode));
+      regno += subreg_regno_offset (regno, orig_mode, offset, new_mode);
+      if (HARD_REGNO_MODE_OK (regno, new_mode))
+       return gen_raw_REG (new_mode, regno);
     }
   return NULL_RTX;
 }
@@ -398,7 +452,7 @@ static rtx
 find_oldest_value_reg (enum reg_class cl, rtx reg, struct value_data *vd)
 {
   unsigned int regno = REGNO (reg);
-  enum machine_mode mode = GET_MODE (reg);
+  machine_mode mode = GET_MODE (reg);
   unsigned int i;
 
   /* If we are accessing REG in some mode other that what we set it in,
@@ -417,11 +471,11 @@ find_oldest_value_reg (enum reg_class cl, rtx reg, struct value_data *vd)
 
   for (i = vd->e[regno].oldest_regno; i != regno; i = vd->e[i].next_regno)
     {
-      enum machine_mode oldmode = vd->e[i].mode;
+      machine_mode oldmode = vd->e[i].mode;
       rtx new_rtx;
 
       if (!in_hard_reg_set_p (reg_class_contents[cl], mode, i))
-       return NULL_RTX;
+       continue;
 
       new_rtx = maybe_mode_change (oldmode, vd->e[regno].mode, mode, i, regno);
       if (new_rtx)
@@ -440,12 +494,29 @@ find_oldest_value_reg (enum reg_class cl, rtx reg, struct value_data *vd)
    in register class CL.  Return true if successfully replaced.  */
 
 static bool
-replace_oldest_value_reg (rtx *loc, enum reg_class cl, rtx insn,
+replace_oldest_value_reg (rtx *loc, enum reg_class cl, rtx_insn *insn,
                          struct value_data *vd)
 {
   rtx new_rtx = find_oldest_value_reg (cl, *loc, vd);
-  if (new_rtx)
+  if (new_rtx && (!DEBUG_INSN_P (insn) || !skip_debug_insn_p))
     {
+      if (DEBUG_INSN_P (insn))
+       {
+         struct queued_debug_insn_change *change;
+
+         if (dump_file)
+           fprintf (dump_file, "debug_insn %u: queued replacing reg %u with %u\n",
+                    INSN_UID (insn), REGNO (*loc), REGNO (new_rtx));
+
+         change = new queued_debug_insn_change;
+         change->next = vd->e[REGNO (new_rtx)].debug_insn_changes;
+         change->insn = insn;
+         change->loc = loc;
+         change->new_rtx = new_rtx;
+         vd->e[REGNO (new_rtx)].debug_insn_changes = change;
+         ++vd->n_debug_insn_changes;
+         return true;
+       }
       if (dump_file)
        fprintf (dump_file, "insn %u: replaced reg %u with %u\n",
                 INSN_UID (insn), REGNO (*loc), REGNO (new_rtx));
@@ -462,8 +533,8 @@ replace_oldest_value_reg (rtx *loc, enum reg_class cl, rtx insn,
 
 static bool
 replace_oldest_value_addr (rtx *loc, enum reg_class cl,
-                          enum machine_mode mode, rtx insn,
-                          struct value_data *vd)
+                          machine_mode mode, addr_space_t as,
+                          rtx_insn *insn, struct value_data *vd)
 {
   rtx x = *loc;
   RTX_CODE code = GET_CODE (x);
@@ -532,15 +603,15 @@ replace_oldest_value_addr (rtx *loc, enum reg_class cl,
            unsigned regno0 = REGNO (op0), regno1 = REGNO (op1);
 
            if (REGNO_OK_FOR_INDEX_P (regno1)
-               && regno_ok_for_base_p (regno0, mode, PLUS, REG))
+               && regno_ok_for_base_p (regno0, mode, as, PLUS, REG))
              index_op = 1;
            else if (REGNO_OK_FOR_INDEX_P (regno0)
-                    && regno_ok_for_base_p (regno1, mode, PLUS, REG))
+                    && regno_ok_for_base_p (regno1, mode, as, PLUS, REG))
              index_op = 0;
-           else if (regno_ok_for_base_p (regno0, mode, PLUS, REG)
+           else if (regno_ok_for_base_p (regno0, mode, as, PLUS, REG)
                     || REGNO_OK_FOR_INDEX_P (regno1))
              index_op = 1;
-           else if (regno_ok_for_base_p (regno1, mode, PLUS, REG))
+           else if (regno_ok_for_base_p (regno1, mode, as, PLUS, REG))
              index_op = 0;
            else
              index_op = 1;
@@ -563,13 +634,13 @@ replace_oldest_value_addr (rtx *loc, enum reg_class cl,
          }
 
        if (locI)
-         changed |= replace_oldest_value_addr (locI, INDEX_REG_CLASS, mode,
-                                               insn, vd);
+         changed |= replace_oldest_value_addr (locI, INDEX_REG_CLASS,
+                                               mode, as, insn, vd);
        if (locB)
          changed |= replace_oldest_value_addr (locB,
-                                               base_reg_class (mode, PLUS,
+                                               base_reg_class (mode, as, PLUS,
                                                                index_code),
-                                               mode, insn, vd);
+                                               mode, as, insn, vd);
        return changed;
       }
 
@@ -595,12 +666,12 @@ replace_oldest_value_addr (rtx *loc, enum reg_class cl,
   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
     {
       if (fmt[i] == 'e')
-       changed |= replace_oldest_value_addr (&XEXP (x, i), cl, mode,
+       changed |= replace_oldest_value_addr (&XEXP (x, i), cl, mode, as,
                                              insn, vd);
       else if (fmt[i] == 'E')
        for (j = XVECLEN (x, i) - 1; j >= 0; j--)
          changed |= replace_oldest_value_addr (&XVECEXP (x, i, j), cl,
-                                               mode, insn, vd);
+                                               mode, as, insn, vd);
     }
 
   return changed;
@@ -609,17 +680,85 @@ replace_oldest_value_addr (rtx *loc, enum reg_class cl,
 /* Similar to replace_oldest_value_reg, but X contains a memory.  */
 
 static bool
-replace_oldest_value_mem (rtx x, rtx insn, struct value_data *vd)
+replace_oldest_value_mem (rtx x, rtx_insn *insn, struct value_data *vd)
 {
   enum reg_class cl;
 
   if (DEBUG_INSN_P (insn))
     cl = ALL_REGS;
   else
-    cl = base_reg_class (GET_MODE (x), MEM, SCRATCH);
+    cl = base_reg_class (GET_MODE (x), MEM_ADDR_SPACE (x), MEM, SCRATCH);
 
   return replace_oldest_value_addr (&XEXP (x, 0), cl,
-                                   GET_MODE (x), insn, vd);
+                                   GET_MODE (x), MEM_ADDR_SPACE (x),
+                                   insn, vd);
+}
+
+/* Apply all queued updates for DEBUG_INSNs that change some reg to
+   register REGNO.  */
+
+static void
+apply_debug_insn_changes (struct value_data *vd, unsigned int regno)
+{
+  struct queued_debug_insn_change *change;
+  rtx_insn *last_insn = vd->e[regno].debug_insn_changes->insn;
+
+  for (change = vd->e[regno].debug_insn_changes;
+       change;
+       change = change->next)
+    {
+      if (last_insn != change->insn)
+       {
+         apply_change_group ();
+         last_insn = change->insn;
+       }
+      validate_change (change->insn, change->loc, change->new_rtx, 1);
+    }
+  apply_change_group ();
+}
+
+/* Called via note_uses, for all used registers in a real insn
+   apply DEBUG_INSN changes that change registers to the used
+   registers.  */
+
+static void
+cprop_find_used_regs (rtx *loc, void *data)
+{
+  struct value_data *const vd = (struct value_data *) data;
+  subrtx_iterator::array_type array;
+  FOR_EACH_SUBRTX (iter, array, *loc, NONCONST)
+    {
+      const_rtx x = *iter;
+      if (REG_P (x))
+       {
+         unsigned int regno = REGNO (x);
+         if (vd->e[regno].debug_insn_changes)
+           {
+             apply_debug_insn_changes (vd, regno);
+             free_debug_insn_changes (vd, regno);
+           }
+       }
+    }
+}
+
+/* Apply clobbers of INSN in PATTERN and C_I_F_U to value_data VD.  */
+
+static void
+kill_clobbered_values (rtx_insn *insn, struct value_data *vd)
+{
+  note_stores (PATTERN (insn), kill_clobbered_value, vd);
+
+  if (CALL_P (insn))
+    {
+      rtx exp;
+
+      for (exp = CALL_INSN_FUNCTION_USAGE (insn); exp; exp = XEXP (exp, 1))
+       {
+         rtx x = XEXP (exp, 0);
+         if (GET_CODE (x) == CLOBBER)
+           kill_value (SET_DEST (x), vd);
+       }
+    }
 }
 
 /* Perform the forward copy propagation on basic block BB.  */
@@ -628,31 +767,27 @@ static bool
 copyprop_hardreg_forward_1 (basic_block bb, struct value_data *vd)
 {
   bool anything_changed = false;
-  rtx insn;
+  rtx_insn *insn;
 
   for (insn = BB_HEAD (bb); ; insn = NEXT_INSN (insn))
     {
-      int n_ops, i, alt, predicated;
+      int n_ops, i, predicated;
       bool is_asm, any_replacements;
       rtx set;
+      rtx link;
       bool replaced[MAX_RECOG_OPERANDS];
       bool changed = false;
+      struct kill_set_value_data ksvd;
 
       if (!NONDEBUG_INSN_P (insn))
        {
          if (DEBUG_INSN_P (insn))
            {
              rtx loc = INSN_VAR_LOCATION_LOC (insn);
-             if (!VAR_LOC_UNKNOWN_P (loc)
-                 && replace_oldest_value_addr (&INSN_VAR_LOCATION_LOC (insn),
-                                               ALL_REGS, GET_MODE (loc),
-                                               insn, vd))
-               {
-                 changed = apply_change_group ();
-                 gcc_assert (changed);
-                 df_insn_rescan (insn);
-                 anything_changed = true;
-               }
+             if (!VAR_LOC_UNKNOWN_P (loc))
+               replace_oldest_value_addr (&INSN_VAR_LOCATION_LOC (insn),
+                                          ALL_REGS, GET_MODE (loc),
+                                          ADDR_SPACE_GENERIC, insn, vd);
            }
 
          if (insn == BB_END (bb))
@@ -662,55 +797,71 @@ copyprop_hardreg_forward_1 (basic_block bb, struct value_data *vd)
        }
 
       set = single_set (insn);
-      extract_insn (insn);
-      if (! constrain_operands (1))
-       fatal_insn_not_found (insn);
-      preprocess_constraints ();
-      alt = which_alternative;
+      extract_constrain_insn (insn);
+      preprocess_constraints (insn);
+      const operand_alternative *op_alt = which_op_alt ();
       n_ops = recog_data.n_operands;
       is_asm = asm_noperands (PATTERN (insn)) >= 0;
 
-      /* Simplify the code below by rewriting things to reflect
-        matching constraints.  Also promote OP_OUT to OP_INOUT
+      /* Simplify the code below by promoting OP_OUT to OP_INOUT
         in predicated instructions.  */
 
       predicated = GET_CODE (PATTERN (insn)) == COND_EXEC;
       for (i = 0; i < n_ops; ++i)
        {
-         int matches = recog_op_alt[i][alt].matches;
-         if (matches >= 0)
-           recog_op_alt[i][alt].cl = recog_op_alt[matches][alt].cl;
-         if (matches >= 0 || recog_op_alt[i][alt].matched >= 0
+         int matches = op_alt[i].matches;
+         if (matches >= 0 || op_alt[i].matched >= 0
              || (predicated && recog_data.operand_type[i] == OP_OUT))
            recog_data.operand_type[i] = OP_INOUT;
        }
 
+      /* Apply changes to earlier DEBUG_INSNs if possible.  */
+      if (vd->n_debug_insn_changes)
+       note_uses (&PATTERN (insn), cprop_find_used_regs, vd);
+
       /* For each earlyclobber operand, zap the value data.  */
       for (i = 0; i < n_ops; i++)
-       if (recog_op_alt[i][alt].earlyclobber)
+       if (op_alt[i].earlyclobber)
          kill_value (recog_data.operand[i], vd);
 
       /* Within asms, a clobber cannot overlap inputs or outputs.
         I wouldn't think this were true for regular insns, but
         scan_rtx treats them like that...  */
-      note_stores (PATTERN (insn), kill_clobbered_value, vd);
+      kill_clobbered_values (insn, vd);
 
       /* Kill all auto-incremented values.  */
       /* ??? REG_INC is useless, since stack pushes aren't done that way.  */
-      for_each_rtx (&PATTERN (insn), kill_autoinc_value, vd);
+      kill_autoinc_value (insn, vd);
 
       /* Kill all early-clobbered operands.  */
       for (i = 0; i < n_ops; i++)
-       if (recog_op_alt[i][alt].earlyclobber)
+       if (op_alt[i].earlyclobber)
          kill_value (recog_data.operand[i], vd);
 
+      /* If we have dead sets in the insn, then we need to note these as we
+        would clobbers.  */
+      for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
+       {
+         if (REG_NOTE_KIND (link) == REG_UNUSED)
+           {
+             kill_value (XEXP (link, 0), vd);
+             /* Furthermore, if the insn looked like a single-set,
+                but the dead store kills the source value of that
+                set, then we can no-longer use the plain move
+                special case below.  */
+             if (set
+                 && reg_overlap_mentioned_p (XEXP (link, 0), SET_SRC (set)))
+               set = NULL;
+           }
+       }
+
       /* Special-case plain move instructions, since we may well
         be able to do the move from a different register class.  */
       if (set && REG_P (SET_SRC (set)))
        {
          rtx src = SET_SRC (set);
          unsigned int regno = REGNO (src);
-         enum machine_mode mode = GET_MODE (src);
+         machine_mode mode = GET_MODE (src);
          unsigned int i;
          rtx new_rtx;
 
@@ -721,6 +872,14 @@ copyprop_hardreg_forward_1 (basic_block bb, struct value_data *vd)
              if (hard_regno_nregs[regno][mode]
                  > hard_regno_nregs[regno][vd->e[regno].mode])
                goto no_move_special_case;
+
+             /* And likewise, if we are narrowing on big endian the transformation
+                is also invalid.  */
+             if (hard_regno_nregs[regno][mode]
+                 < hard_regno_nregs[regno][vd->e[regno].mode]
+                 && (GET_MODE_SIZE (vd->e[regno].mode) > UNITS_PER_WORD
+                     ? WORDS_BIG_ENDIAN : BYTES_BIG_ENDIAN))
+               goto no_move_special_case;
            }
 
          /* If the destination is also a register, try to find a source
@@ -737,6 +896,10 @@ copyprop_hardreg_forward_1 (basic_block bb, struct value_data *vd)
                  changed = true;
                  goto did_replacement;
                }
+             /* We need to re-extract as validate_change clobbers
+                recog_data.  */
+             extract_constrain_insn (insn);
+             preprocess_constraints (insn);
            }
 
          /* Otherwise, try all valid registers and see if its valid.  */
@@ -759,6 +922,10 @@ copyprop_hardreg_forward_1 (basic_block bb, struct value_data *vd)
                      changed = true;
                      goto did_replacement;
                    }
+                 /* We need to re-extract as validate_change clobbers
+                    recog_data.  */
+                 extract_constrain_insn (insn);
+                 preprocess_constraints (insn);
                }
            }
        }
@@ -786,15 +953,16 @@ copyprop_hardreg_forward_1 (basic_block bb, struct value_data *vd)
 
          if (recog_data.operand_type[i] == OP_IN)
            {
-             if (recog_op_alt[i][alt].is_address)
+             if (op_alt[i].is_address)
                replaced[i]
                  = replace_oldest_value_addr (recog_data.operand_loc[i],
-                                              recog_op_alt[i][alt].cl,
-                                              VOIDmode, insn, vd);
+                                              alternative_class (op_alt, i),
+                                              VOIDmode, ADDR_SPACE_GENERIC,
+                                              insn, vd);
              else if (REG_P (recog_data.operand[i]))
                replaced[i]
                  = replace_oldest_value_reg (recog_data.operand_loc[i],
-                                             recog_op_alt[i][alt].cl,
+                                             alternative_class (op_alt, i),
                                              insn, vd);
              else if (MEM_P (recog_data.operand[i]))
                replaced[i] = replace_oldest_value_mem (recog_data.operand[i],
@@ -843,70 +1011,85 @@ copyprop_hardreg_forward_1 (basic_block bb, struct value_data *vd)
     did_replacement:
       if (changed)
        {
-         df_insn_rescan (insn);
          anything_changed = true;
+
+         /* If something changed, perhaps further changes to earlier
+            DEBUG_INSNs can be applied.  */
+         if (vd->n_debug_insn_changes)
+           note_uses (&PATTERN (insn), cprop_find_used_regs, vd);
        }
 
+      ksvd.vd = vd;
+      ksvd.ignore_set_reg = NULL_RTX;
+
       /* Clobber call-clobbered registers.  */
       if (CALL_P (insn))
-       for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
-         if (TEST_HARD_REG_BIT (regs_invalidated_by_call, i))
-           kill_value_regno (i, 1, vd);
-
-      /* Notice stores.  */
-      note_stores (PATTERN (insn), kill_set_value, vd);
-
-      /* Notice copies.  */
-      if (set && REG_P (SET_DEST (set)) && REG_P (SET_SRC (set)))
-       copy_value (SET_DEST (set), SET_SRC (set), vd);
-
-      if (insn == BB_END (bb))
-       break;
-    }
-
-  return anything_changed;
-}
-
-/* Main entry point for the forward copy propagation optimization.  */
+       {
+         unsigned int set_regno = INVALID_REGNUM;
+         unsigned int set_nregs = 0;
+         unsigned int regno;
+         rtx exp;
+         HARD_REG_SET regs_invalidated_by_this_call;
 
-static unsigned int
-copyprop_hardreg_forward (void)
-{
-  struct value_data *all_vd;
-  basic_block bb;
-  sbitmap visited;
+         for (exp = CALL_INSN_FUNCTION_USAGE (insn); exp; exp = XEXP (exp, 1))
+           {
+             rtx x = XEXP (exp, 0);
+             if (GET_CODE (x) == SET)
+               {
+                 rtx dest = SET_DEST (x);
+                 kill_value (dest, vd);
+                 set_value_regno (REGNO (dest), GET_MODE (dest), vd);
+                 copy_value (dest, SET_SRC (x), vd);
+                 ksvd.ignore_set_reg = dest;
+                 set_regno = REGNO (dest);
+                 set_nregs = REG_NREGS (dest);
+                 break;
+               }
+           }
 
-  all_vd = XNEWVEC (struct value_data, last_basic_block);
+         get_call_reg_set_usage (insn,
+                                 &regs_invalidated_by_this_call,
+                                 regs_invalidated_by_call);
+         for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
+           if ((TEST_HARD_REG_BIT (regs_invalidated_by_this_call, regno)
+                || HARD_REGNO_CALL_PART_CLOBBERED (regno, vd->e[regno].mode))
+               && (regno < set_regno || regno >= set_regno + set_nregs))
+             kill_value_regno (regno, 1, vd);
+
+         /* If SET was seen in CALL_INSN_FUNCTION_USAGE, and SET_SRC
+            of the SET isn't in regs_invalidated_by_call hard reg set,
+            but instead among CLOBBERs on the CALL_INSN, we could wrongly
+            assume the value in it is still live.  */
+         if (ksvd.ignore_set_reg)
+           kill_clobbered_values (insn, vd);
+       }
 
-  visited = sbitmap_alloc (last_basic_block);
-  sbitmap_zero (visited);
+      bool copy_p = (set
+                    && REG_P (SET_DEST (set))
+                    && REG_P (SET_SRC (set)));
+      bool noop_p = (copy_p
+                    && rtx_equal_p (SET_DEST (set), SET_SRC (set)));
 
-  FOR_EACH_BB (bb)
-    {
-      SET_BIT (visited, bb->index);
+      if (!noop_p)
+       {
+         /* Notice stores.  */
+         note_stores (PATTERN (insn), kill_set_value, &ksvd);
 
-      /* If a block has a single predecessor, that we've already
-        processed, begin with the value data that was live at
-        the end of the predecessor block.  */
-      /* ??? Ought to use more intelligent queuing of blocks.  */
-      if (single_pred_p (bb) 
-         && TEST_BIT (visited, single_pred (bb)->index)
-         && ! (single_pred_edge (bb)->flags & (EDGE_ABNORMAL_CALL | EDGE_EH)))
-       all_vd[bb->index] = all_vd[single_pred (bb)->index];
-      else
-       init_value_data (all_vd + bb->index);
+         /* Notice copies.  */
+         if (copy_p)
+           copy_value (SET_DEST (set), SET_SRC (set), vd);
+       }
 
-      copyprop_hardreg_forward_1 (bb, all_vd + bb->index);
+      if (insn == BB_END (bb))
+       break;
     }
 
-  sbitmap_free (visited);  
-  free (all_vd);
-  return 0;
+  return anything_changed;
 }
 
 /* Dump the value chain data to stderr.  */
 
-void
+DEBUG_FUNCTION void
 debug_value_data (struct value_data *vd)
 {
   HARD_REG_SET set;
@@ -960,6 +1143,26 @@ debug_value_data (struct value_data *vd)
               vd->e[i].next_regno);
 }
 
+/* Do copyprop_hardreg_forward_1 for a single basic block BB.
+   DEBUG_INSN is skipped since we do not want to involve DF related
+   staff as how it is handled in function pass_cprop_hardreg::execute.
+
+   NOTE: Currently it is only used for shrink-wrap.  Maybe extend it
+   to handle DEBUG_INSN for other uses.  */
+
+void
+copyprop_hardreg_forward_bb_without_debug_insn (basic_block bb)
+{
+  struct value_data *vd;
+  vd = XNEWVEC (struct value_data, 1);
+  init_value_data (vd);
+
+  skip_debug_insn_p = true;
+  copyprop_hardreg_forward_1 (bb, vd);
+  free (vd);
+  skip_debug_insn_p = false;
+}
+
 #ifdef ENABLE_CHECKING
 static void
 validate_value_data (struct value_data *vd)
@@ -1008,28 +1211,122 @@ validate_value_data (struct value_data *vd)
 }
 #endif
 \f
-static bool
-gate_handle_cprop (void)
+namespace {
+
+const pass_data pass_data_cprop_hardreg =
+{
+  RTL_PASS, /* type */
+  "cprop_hardreg", /* name */
+  OPTGROUP_NONE, /* optinfo_flags */
+  TV_CPROP_REGISTERS, /* tv_id */
+  0, /* properties_required */
+  0, /* properties_provided */
+  0, /* properties_destroyed */
+  0, /* todo_flags_start */
+  TODO_df_finish, /* todo_flags_finish */
+};
+
+class pass_cprop_hardreg : public rtl_opt_pass
 {
-  return (optimize > 0 && (flag_cprop_registers));
+public:
+  pass_cprop_hardreg (gcc::context *ctxt)
+    : rtl_opt_pass (pass_data_cprop_hardreg, ctxt)
+  {}
+
+  /* opt_pass methods: */
+  virtual bool gate (function *)
+    {
+      return (optimize > 0 && (flag_cprop_registers));
+    }
+
+  virtual unsigned int execute (function *);
+
+}; // class pass_cprop_hardreg
+
+unsigned int
+pass_cprop_hardreg::execute (function *fun)
+{
+  struct value_data *all_vd;
+  basic_block bb;
+  sbitmap visited;
+  bool analyze_called = false;
+
+  all_vd = XNEWVEC (struct value_data, last_basic_block_for_fn (fun));
+
+  visited = sbitmap_alloc (last_basic_block_for_fn (fun));
+  bitmap_clear (visited);
+
+  FOR_EACH_BB_FN (bb, fun)
+    {
+      bitmap_set_bit (visited, bb->index);
+
+      /* If a block has a single predecessor, that we've already
+        processed, begin with the value data that was live at
+        the end of the predecessor block.  */
+      /* ??? Ought to use more intelligent queuing of blocks.  */
+      if (single_pred_p (bb)
+         && bitmap_bit_p (visited, single_pred (bb)->index)
+         && ! (single_pred_edge (bb)->flags & (EDGE_ABNORMAL_CALL | EDGE_EH)))
+       {
+         all_vd[bb->index] = all_vd[single_pred (bb)->index];
+         if (all_vd[bb->index].n_debug_insn_changes)
+           {
+             unsigned int regno;
+
+             for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
+               {
+                 if (all_vd[bb->index].e[regno].debug_insn_changes)
+                   {
+                     all_vd[bb->index].e[regno].debug_insn_changes = NULL;
+                     if (--all_vd[bb->index].n_debug_insn_changes == 0)
+                       break;
+                   }
+               }
+           }
+       }
+      else
+       init_value_data (all_vd + bb->index);
+
+      copyprop_hardreg_forward_1 (bb, all_vd + bb->index);
+    }
+
+  if (MAY_HAVE_DEBUG_INSNS)
+    {
+      FOR_EACH_BB_FN (bb, fun)
+       if (bitmap_bit_p (visited, bb->index)
+           && all_vd[bb->index].n_debug_insn_changes)
+         {
+           unsigned int regno;
+           bitmap live;
+
+           if (!analyze_called)
+             {
+               df_analyze ();
+               analyze_called = true;
+             }
+           live = df_get_live_out (bb);
+           for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
+             if (all_vd[bb->index].e[regno].debug_insn_changes)
+               {
+                 if (REGNO_REG_SET_P (live, regno))
+                   apply_debug_insn_changes (all_vd + bb->index, regno);
+                 if (all_vd[bb->index].n_debug_insn_changes == 0)
+                   break;
+               }
+         }
+
+      queued_debug_insn_change::pool.release ();
+    }
+
+  sbitmap_free (visited);
+  free (all_vd);
+  return 0;
 }
 
+} // anon namespace
 
-struct rtl_opt_pass pass_cprop_hardreg =
+rtl_opt_pass *
+make_pass_cprop_hardreg (gcc::context *ctxt)
 {
- {
-  RTL_PASS,
-  "cprop_hardreg",                      /* name */
-  gate_handle_cprop,                    /* gate */
-  copyprop_hardreg_forward,             /* execute */
-  NULL,                                 /* sub */
-  NULL,                                 /* next */
-  0,                                    /* static_pass_number */
-  TV_CPROP_REGISTERS,                   /* tv_id */
-  0,                                    /* properties_required */
-  0,                                    /* properties_provided */
-  0,                                    /* properties_destroyed */
-  0,                                    /* todo_flags_start */
-  TODO_dump_func | TODO_verify_rtl_sharing /* todo_flags_finish */
- }
-};
+  return new pass_cprop_hardreg (ctxt);
+}