From 11f1e3ab239b3a45b085ca5eb1327f1254fb6205 Mon Sep 17 00:00:00 2001
From: Jakub Jelinek <jakub@redhat.com>
Date: Tue, 5 Feb 2013 16:54:39 +0100
Subject: [PATCH] re PR tree-optimization/56205 (stdarg pass confused by jump
 threading)

	PR tree-optimization/56205
	* tree-stdarg.c (check_all_va_list_escapes): Return true if
	there are any PHI nodes that set non-va_list_escape_vars SSA_NAME
	and some va_list_escape_vars SSA_NAME appears in some PHI argument.

	* gcc.dg/tree-ssa/stdarg-6.c: New test.
	* gcc.c-torture/execute/pr56205.c: New test.

From-SVN: r195760
---
 gcc/ChangeLog                                 |  7 +++
 gcc/testsuite/ChangeLog                       |  6 ++
 gcc/testsuite/gcc.c-torture/execute/pr56205.c | 61 +++++++++++++++++++
 gcc/testsuite/gcc.dg/tree-ssa/stdarg-6.c      | 35 +++++++++++
 gcc/tree-stdarg.c                             | 31 ++++++++++
 5 files changed, 140 insertions(+)
 create mode 100644 gcc/testsuite/gcc.c-torture/execute/pr56205.c
 create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/stdarg-6.c

diff --git a/gcc/ChangeLog b/gcc/ChangeLog
index 595dad4cf5b7..a6ab680cc8ad 100644
--- a/gcc/ChangeLog
+++ b/gcc/ChangeLog
@@ -1,3 +1,10 @@
+2013-02-05  Jakub Jelinek  <jakub@redhat.com>
+
+	PR tree-optimization/56205
+	* tree-stdarg.c (check_all_va_list_escapes): Return true if
+	there are any PHI nodes that set non-va_list_escape_vars SSA_NAME
+	and some va_list_escape_vars SSA_NAME appears in some PHI argument.
+
 2013-02-05  Richard Biener  <rguenther@suse.de>
 
 	PR tree-optimization/53342
diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog
index a9d19c9cfab3..dc716f97bb94 100644
--- a/gcc/testsuite/ChangeLog
+++ b/gcc/testsuite/ChangeLog
@@ -1,3 +1,9 @@
+2013-02-05  Jakub Jelinek  <jakub@redhat.com>
+
+	PR tree-optimization/56205
+	* gcc.dg/tree-ssa/stdarg-6.c: New test.
+	* gcc.c-torture/execute/pr56205.c: New test.
+
 2013-02-05  Richard Biener  <rguenther@suse.de>
 
 	PR tree-optimization/53342
diff --git a/gcc/testsuite/gcc.c-torture/execute/pr56205.c b/gcc/testsuite/gcc.c-torture/execute/pr56205.c
new file mode 100644
index 000000000000..bf39685946b5
--- /dev/null
+++ b/gcc/testsuite/gcc.c-torture/execute/pr56205.c
@@ -0,0 +1,61 @@
+/* PR tree-optimization/56205 */
+
+#include <stdarg.h>
+
+int a, b;
+char c[128];
+
+__attribute__((noinline, noclone)) static void
+f1 (const char *fmt, ...)
+{
+  va_list ap;
+  asm volatile ("" : : : "memory");
+  if (__builtin_strcmp (fmt, "%s %d %s") != 0)
+    __builtin_abort ();
+  va_start (ap, fmt);
+  if (__builtin_strcmp (va_arg (ap, const char *), "foo") != 0
+      || va_arg (ap, int) != 1
+      || __builtin_strcmp (va_arg (ap, const char *), "bar") != 0)
+    __builtin_abort ();
+  va_end (ap);
+}
+
+__attribute__((noinline, noclone)) static void
+f2 (const char *fmt, va_list ap)
+{
+  asm volatile ("" : : : "memory");
+  if (__builtin_strcmp (fmt, "baz") != 0
+      || __builtin_strcmp (va_arg (ap, const char *), "foo") != 0
+      || va_arg (ap, double) != 12.0
+      || va_arg (ap, int) != 26)
+    __builtin_abort ();
+}
+
+static void
+f3 (int x, char const *y, va_list z)
+{
+  f1 ("%s %d %s", x ? "" : "foo", ++a, (y && *y) ? "bar" : "");
+  if (y && *y)
+    f2 (y, z);
+}
+
+__attribute__((noinline, noclone)) void
+f4 (int x, char const *y, ...)
+{
+  va_list z;
+  va_start (z, y);
+  if (!x && *c == '\0')
+    ++b;
+  f3 (x, y, z);
+  va_end (z);
+}
+
+int
+main ()
+{
+  asm volatile ("" : : : "memory");
+  f4 (0, "baz", "foo", 12.0, 26);
+  if (a != 1 || b != 1)
+    __builtin_abort ();
+  return 0;
+}
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/stdarg-6.c b/gcc/testsuite/gcc.dg/tree-ssa/stdarg-6.c
new file mode 100644
index 000000000000..c21dc17b527d
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/stdarg-6.c
@@ -0,0 +1,35 @@
+/* PR tree-optimization/56205 */
+/* { dg-do compile } */
+/* { dg-options "-O3 -fdump-tree-stdarg" } */
+
+#include <stdarg.h>
+
+int a, b;
+char c[128];
+
+static inline void
+foo (int x, char const *y, va_list z)
+{
+  __builtin_printf ("%s %d %s", x ? "" : "foo", ++a, (y && *y) ? "bar" : "");
+  if (y && *y)
+    __builtin_vprintf (y, z);
+}
+
+void
+bar (int x, char const *y, ...)
+{
+  va_list z;
+  va_start (z, y);
+  if (!x && *c == '\0')
+    ++b;
+  foo (x, y, z);
+  va_end (z);
+}
+
+/* { dg-final { scan-tree-dump "bar: va_list escapes 1, needs to save all GPR units and all FPR units" "stdarg" { target { { i?86-*-* x86_64-*-* } && { ! { ia32 } } } } } } */
+/* { dg-final { scan-tree-dump "bar: va_list escapes 1, needs to save all GPR units and all FPR units" "stdarg" { target { powerpc*-*-linux* && ilp32 } } } } */
+/* { dg-final { scan-tree-dump "bar: va_list escapes 1, needs to save all GPR units and all FPR units" "stdarg" { target alpha*-*-linux* } } } */
+/* { dg-final { scan-tree-dump "bar: va_list escapes 1, needs to save all GPR units and all FPR units" "stdarg" { target s390*-*-linux* } } } */
+/* { dg-final { scan-tree-dump "bar: va_list escapes 1, needs to save all GPR units" "stdarg" { target { { i?86-*-* x86_64-*-* } && ia32 } } } } */
+/* { dg-final { scan-tree-dump "bar: va_list escapes 1, needs to save all GPR units" "stdarg" { target ia64-*-* } } } */
+/* { dg-final { scan-tree-dump "bar: va_list escapes 1, needs to save all GPR units" "stdarg" { target { powerpc*-*-* && lp64 } } } } */
diff --git a/gcc/tree-stdarg.c b/gcc/tree-stdarg.c
index cd573a339cfc..8ad9fc2d22e4 100644
--- a/gcc/tree-stdarg.c
+++ b/gcc/tree-stdarg.c
@@ -526,6 +526,37 @@ check_all_va_list_escapes (struct stdarg_info *si)
     {
       gimple_stmt_iterator i;
 
+      for (i = gsi_start_phis (bb); !gsi_end_p (i); gsi_next (&i))
+	{
+	  tree lhs;
+	  use_operand_p uop;
+	  ssa_op_iter soi;
+	  gimple phi = gsi_stmt (i);
+
+	  lhs = PHI_RESULT (phi);
+	  if (virtual_operand_p (lhs)
+	      || bitmap_bit_p (si->va_list_escape_vars,
+			       SSA_NAME_VERSION (lhs)))
+	    continue;
+
+	  FOR_EACH_PHI_ARG (uop, phi, soi, SSA_OP_USE)
+	    {
+	      tree rhs = USE_FROM_PTR (uop);
+	      if (TREE_CODE (rhs) == SSA_NAME
+		  && bitmap_bit_p (si->va_list_escape_vars,
+				SSA_NAME_VERSION (rhs)))
+		{
+		  if (dump_file && (dump_flags & TDF_DETAILS))
+		    {
+		      fputs ("va_list escapes in ", dump_file);
+		      print_gimple_stmt (dump_file, phi, 0, dump_flags);
+		      fputc ('\n', dump_file);
+		    }
+		  return true;
+		}
+	    }
+	}
+
       for (i = gsi_start_bb (bb); !gsi_end_p (i); gsi_next (&i))
 	{
 	  gimple stmt = gsi_stmt (i);
-- 
GitLab