	PR middle-end/39326
	* dse.c (replace_read): If the stored value is a pseudo-register
	that is set only once, re-use it to replace the load instead of
	creating a new register.

Index: dse.c
===================================================================
--- dse.c	(revision 196576)
+++ dse.c	(working copy)
@@ -2011,7 +2011,7 @@ replace_read (store_info_t store_info, insn_info_t
 {
   enum machine_mode store_mode = GET_MODE (store_info->mem);
   enum machine_mode read_mode = GET_MODE (read_info->mem);
-  rtx insns, this_insn, read_reg;
+  rtx insns, this_insn, read_val, read_reg;
   basic_block bb;
 
   if (!dbg_cnt (dse))
@@ -2033,19 +2033,27 @@ replace_read (store_info_t store_info, insn_info_t
 	     GET_MODE_NAME (store_mode), INSN_UID (store_insn->insn));
   start_sequence ();
   bb = BLOCK_FOR_INSN (read_insn->insn);
-  read_reg = get_stored_val (store_info,
+  read_val = get_stored_val (store_info,
 			     read_mode, read_info->begin, read_info->end,
 			     bb, false);
-  if (read_reg == NULL_RTX)
+  if (read_val == NULL_RTX)
     {
       end_sequence ();
       if (dump_file && (dump_flags & TDF_DETAILS))
 	fprintf (dump_file, " -- could not extract bits of stored value\n");
       return false;
     }
-  /* Force the value into a new register so that it won't be clobbered
-     between the store and the load.  */
-  read_reg = copy_to_mode_reg (read_mode, read_reg);
+
+  /* If READ_REG is a pseudo that is only set once, re-use the register.
+     Otherwise, force the value into a new register so that it won't be
+     clobbered between the store and the load.  */
+  if (! REG_P (read_val) || HARD_REGISTER_P (read_val)
+      || REGNO (read_val) >= DF_REG_SIZE (df)
+      || DF_REG_DEF_COUNT (REGNO (read_val)) != 1)
+    read_reg = copy_to_mode_reg (read_mode, read_val);
+  else
+    read_reg = read_val;
+
   insns = get_insns ();
   end_sequence ();
 
@@ -2079,16 +2087,15 @@ replace_read (store_info_t store_info, insn_info_t
 
   if (validate_change (read_insn->insn, loc, read_reg, 0))
     {
-      deferred_change_t deferred_change =
-	(deferred_change_t) pool_alloc (deferred_change_pool);
-
-      /* Insert this right before the store insn where it will be safe
-	 from later insns that might change it before the read.  */
-      emit_insn_before (insns, store_insn->insn);
-
-      /* And now for the kludge part: cselib croaks if you just
-	 return at this point.  There are two reasons for this:
-
+      if (insns != NULL_RTX)
+	/* Insert this right before the store insn where it will be safe
+	   from later insns that might change it before the read.  */
+	emit_insn_before (insns, store_insn->insn);
+
+      /* And now for the kludge part: cselib croaks if you have
+	 create a new register and just return at this point.
+	 There are two reasons for this:
+
 	 1) Cselib has an idea of how many pseudos there are and
 	 that does not include the new ones we just added.
 
@@ -2108,13 +2115,17 @@ replace_read (store_info_t store_info, insn_info_t
 	 and when we are finished with the block, we undo this.  We
 	 keep a table of mems to get rid of.  At the end of the basic
 	 block we can put them back.  */
+      if (read_reg != read_val)
+	{
+	  deferred_change_t deferred_change =
+	    (deferred_change_t) pool_alloc (deferred_change_pool);
+	  *loc = read_info->mem;
+	  deferred_change->next = deferred_change_list;
+	  deferred_change_list = deferred_change;
+	  deferred_change->loc = loc;
+	  deferred_change->reg = read_reg;
+	}
 
-      *loc = read_info->mem;
-      deferred_change->next = deferred_change_list;
-      deferred_change_list = deferred_change;
-      deferred_change->loc = loc;
-      deferred_change->reg = read_reg;
-
       /* Get rid of the read_info, from the point of view of the
 	 rest of dse, play like this read never happened.  */
       read_insn->read_rec = read_info->next;
