From 7d48c9e2cc0d77630d365efe8fa1803a224a9a0a Mon Sep 17 00:00:00 2001
From: Jeff Law <law@redhat.com>
Date: Fri, 16 Dec 2005 00:03:12 -0700
Subject: [PATCH] tree-ssa-dse.c (dse_optimize_stmt): Correctly handle PHI
 nodes which represent a use and definition of the same...

	* tree-ssa-dse.c (dse_optimize_stmt): Correctly handle PHI nodes which
	represent a use and definition of the same SSA_NAME.

	* gcc.dg/tree-ssa/ssa-dse-8.c: New test.

From-SVN: r108630
---
 gcc/ChangeLog                             |  5 +++++
 gcc/testsuite/ChangeLog                   |  4 ++++
 gcc/testsuite/gcc.dg/tree-ssa/ssa-dse-8.c | 26 +++++++++++++++++++++++
 gcc/tree-ssa-dse.c                        |  9 ++++++++
 4 files changed, 44 insertions(+)
 create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/ssa-dse-8.c

diff --git a/gcc/ChangeLog b/gcc/ChangeLog
index 661e5337a535..d90de63c4a95 100644
--- a/gcc/ChangeLog
+++ b/gcc/ChangeLog
@@ -1,3 +1,8 @@
+2005-12-16  Jeff Law  <law@redhat.com>
+
+	* tree-ssa-dse.c (dse_optimize_stmt): Correctly handle PHI nodes which
+	represent a use and definition of the same SSA_NAME.
+
 2005-12-16  Jon Grimm  <jgrimm2@us.ibm.com>
 	    Janis Johnson  <janis187@us.ibm.com>
 	    Ben Elliston  <bje@au.ibm.com>
diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog
index 6662c2eaffa0..9c4ba84cbe30 100644
--- a/gcc/testsuite/ChangeLog
+++ b/gcc/testsuite/ChangeLog
@@ -1,3 +1,7 @@
+2005-12-16  Jeff Law  <law@redhat.com>
+
+	* gcc.dg/tree-ssa/ssa-dse-8.c: New test.
+
 2005-12-16  Andrew Pinski  <pinskia@physics.uc.edu>
 
 	* objc.dg/gnu-encoding/struct-layout-encoding-1_generate.c (base_types):
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/ssa-dse-8.c b/gcc/testsuite/gcc.dg/tree-ssa/ssa-dse-8.c
new file mode 100644
index 000000000000..de1d5bb62403
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/ssa-dse-8.c
@@ -0,0 +1,26 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fno-tree-dce -fno-tree-ccp -fno-tree-copy-prop -fno-tree-dominator-opts" } */
+
+/* This caused the compiler to enter an infinite loop if copies are not
+   fully propagated.   The options are to disable copy propagation and
+   thus expose the bug.   */
+
+int foo (void);
+
+struct A {
+  struct B {
+    struct B *n;
+  } *p;
+};
+
+static inline void baz (struct A *a)
+{
+  a->p = a->p->n;
+}
+
+void bar (struct A a)
+{
+  while (foo ())
+    baz (&a);
+  while (foo ());
+}
diff --git a/gcc/tree-ssa-dse.c b/gcc/tree-ssa-dse.c
index fa3ba960a289..f1cc22ae3102 100644
--- a/gcc/tree-ssa-dse.c
+++ b/gcc/tree-ssa-dse.c
@@ -309,6 +309,15 @@ dse_optimize_stmt (struct dom_walk_data *walk_data,
 	     && TREE_CODE (use_stmt) == PHI_NODE
 	     && bitmap_bit_p (dse_gd->stores, get_stmt_uid (use_stmt)))
 	{
+	  /* A PHI node can both define and use the same SSA_NAME if
+	     the PHI is at the top of a loop and the PHI_RESULT is
+	     a loop invariant and copies have not been fully propagated.
+
+	     The safe thing to do is exit assuming no optimization is
+	     possible.  */
+	  if (SSA_NAME_DEF_STMT (PHI_RESULT (use_stmt)) == use_stmt)
+	    return;
+
 	  /* Skip past this PHI and loop again in case we had a PHI
 	     chain.  */
 	  if (single_imm_use (PHI_RESULT (use_stmt), &use_p, &use_stmt))
-- 
GitLab