diff --git a/gcc/testsuite/gcc.dg/tree-ssa/ssa-ccp-42.c b/gcc/testsuite/gcc.dg/tree-ssa/ssa-ccp-42.c
new file mode 100644
index 0000000000000000000000000000000000000000..b4e5c0f73f20f775c328e80513951471a1e994c3
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/ssa-ccp-42.c
@@ -0,0 +1,26 @@
+/* { dg-do compile } */
+/* { dg-options "-fgimple -O -fdump-tree-ccp1" } */
+
+__GIMPLE (ssa,startwith("ccp")) int foo (int n)
+{
+  int i;
+  int j;
+
+  __BB(2):
+    i_1 = 0;
+    goto __BB3;
+
+  __BB(3):
+    i_2 = __PHI (__BB2: i_1, __BB3: i_4);
+    j_3 = i_2;
+    i_4 = i_2 + 1;
+    if (i_4 < n_5(D))
+      goto __BB3;
+    else
+      goto __BB4;
+
+  __BB(4):
+    return j_3;
+}
+
+/* { dg-final { scan-tree-dump "return i_2;" "ccp1" } } */
diff --git a/gcc/tree-ssa-ccp.cc b/gcc/tree-ssa-ccp.cc
index 2bcd90646f63063b854ea417d7eb5a12e6ef81c3..69fd7f1d11d86ad8708126dada43b02437280499 100644
--- a/gcc/tree-ssa-ccp.cc
+++ b/gcc/tree-ssa-ccp.cc
@@ -532,7 +532,12 @@ set_lattice_value (tree var, ccp_prop_value_t *new_val)
      use the meet operator to retain a conservative value.
      Missed optimizations like PR65851 makes this necessary.
      It also ensures we converge to a stable lattice solution.  */
-  if (old_val->lattice_val != UNINITIALIZED)
+  if (old_val->lattice_val != UNINITIALIZED
+      /* But avoid using meet for constant -> copy transitions.  */
+      && !(old_val->lattice_val == CONSTANT
+	   && CONSTANT_CLASS_P (old_val->value)
+	   && new_val->lattice_val == CONSTANT
+	   && TREE_CODE (new_val->value) == SSA_NAME))
     ccp_lattice_meet (new_val, old_val);
 
   gcc_checking_assert (valid_lattice_transition (*old_val, *new_val));