tree-optimization/108574 - wrong-code with PRE PHI node processing

The PR108523 was too optimistic in replacing the same value with
an equivalence from a possibly not taken edge.  The following
rectifies this and instead refrains from using the equivalence in
the problematic cases.

	PR tree-optimization/108574
	* tree-ssa-sccvn.cc (visit_phi): Instead of swapping
	sameval and def, ignore the equivalence if there's the
	danger of oscillating between two values.

	* gcc.dg/torture/pr108574-1.c: New testcase.
	* gcc.dg/torture/pr108574-2.c: Likewise.
	* gcc.dg/torture/pr108574-3.c: Likewise.
diff --git a/gcc/testsuite/gcc.dg/torture/pr108574-1.c b/gcc/testsuite/gcc.dg/torture/pr108574-1.c
new file mode 100644
index 0000000..7066b5e
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/torture/pr108574-1.c
@@ -0,0 +1,19 @@
+/* { dg-do run } */
+
+int a = 1, b, c = 2, d;
+int main() {
+  if (b)
+    goto L2;
+ L1:
+  {
+    int e = c;
+    a = 1 % a;
+    while (e && 1 <= d)
+      ;
+    d >= b;
+  L2:
+    if (1 >= e)
+      goto L1;
+  }
+  return 0;
+}
diff --git a/gcc/testsuite/gcc.dg/torture/pr108574-2.c b/gcc/testsuite/gcc.dg/torture/pr108574-2.c
new file mode 100644
index 0000000..1e38d08
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/torture/pr108574-2.c
@@ -0,0 +1,25 @@
+/* { dg-do run } */
+
+int a, b, c, d, e, f, g = -1, h;
+void l() {
+  if (!e)
+    goto i;
+  for (; g; g++) {
+    b = ~d;
+    int j = 0, k = 1;
+    if (k && (b || f))
+      j = b;
+  i:
+    a = ~j;
+  }
+}
+int main() {
+  h = 3;
+  for (; h; h--) {
+    e = 1;
+    int m = ~a, n = 1 % m;
+    c = n;
+    l();
+  }
+  return 0;
+}
diff --git a/gcc/testsuite/gcc.dg/torture/pr108574-3.c b/gcc/testsuite/gcc.dg/torture/pr108574-3.c
new file mode 100644
index 0000000..3c9146e
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/torture/pr108574-3.c
@@ -0,0 +1,27 @@
+/* { dg-do run } */
+
+int a = 3557301289, d;
+char b, f;
+unsigned short c = 241;
+short e, g;
+static void h() {
+  if (!a)
+    goto i;
+  b = a;
+  for (; a < 2; a = b) {
+    unsigned short j;
+    if (c || !g) {
+      j = c;
+    i:
+      e = j;
+    }
+    f = j;
+    d = ~(f & ~2880764155);
+    while (d > -2316069)
+      ;
+  }
+}
+int main() {
+  h();
+  return 0;
+}
diff --git a/gcc/tree-ssa-sccvn.cc b/gcc/tree-ssa-sccvn.cc
index edb553b..028bedb 100644
--- a/gcc/tree-ssa-sccvn.cc
+++ b/gcc/tree-ssa-sccvn.cc
@@ -5908,7 +5908,8 @@
 		if (! val && vnresult && vnresult->predicated_values)
 		  {
 		    val = vn_nary_op_get_predicated_value (vnresult, e->src);
-		    if (val && integer_truep (val))
+		    if (val && integer_truep (val)
+			&& !(sameval_e && (sameval_e->flags & EDGE_DFS_BACK)))
 		      {
 			if (dump_file && (dump_flags & TDF_DETAILS))
 			  {
@@ -5919,8 +5920,6 @@
 			    fprintf (dump_file, " are equal on edge %d -> %d\n",
 				     e->src->index, e->dest->index);
 			  }
-			if (sameval_e && (sameval_e->flags & EDGE_DFS_BACK))
-			  sameval = def;
 			continue;
 		      }
 		    /* If on all previous edges the value was equal to def
@@ -5928,7 +5927,8 @@
 		    if (EDGE_COUNT (bb->preds) == 2
 			&& (val = vn_nary_op_get_predicated_value
 				    (vnresult, EDGE_PRED (bb, 0)->src))
-			&& integer_truep (val))
+			&& integer_truep (val)
+			&& !(e->flags & EDGE_DFS_BACK))
 		      {
 			if (dump_file && (dump_flags & TDF_DETAILS))
 			  {
@@ -5940,8 +5940,7 @@
 				     EDGE_PRED (bb, 0)->src->index,
 				     EDGE_PRED (bb, 0)->dest->index);
 			  }
-			if (!(e->flags & EDGE_DFS_BACK))
-			  sameval = def;
+			sameval = def;
 			continue;
 		      }
 		  }