From e02a048f461136c26abbde5e609db372b0b692d6 Mon Sep 17 00:00:00 2001
From: Jakub Jelinek <jakub@redhat.com>
Date: Wed, 7 Feb 2007 13:16:22 +0100
Subject: [PATCH] re PR c++/30703 (ICE Segmentation fault on using OpenMP)

	PR c++/30703
	* gimplify.c (gimplify_scan_omp_clauses): Remove special casing
	of INDIRECT_REF <RESULT_DECL>.

	* cp-gimplify.c (cp_genericize_r): Don't dereference invisiref
	parameters and result decls in omp clauses.
	(cxx_omp_privatize_by_reference): Pass also invisiref PARM_DECLs
	by reference.

	* testsuite/libgomp.c++/pr30703.C: New test.

From-SVN: r121688
---
 gcc/ChangeLog                           |  4 ++
 gcc/cp/ChangeLog                        |  8 +++
 gcc/cp/cp-gimplify.c                    | 21 ++++++-
 gcc/gimplify.c                          | 10 ----
 libgomp/ChangeLog                       |  5 ++
 libgomp/testsuite/libgomp.c++/pr30703.C | 73 +++++++++++++++++++++++++
 6 files changed, 110 insertions(+), 11 deletions(-)
 create mode 100644 libgomp/testsuite/libgomp.c++/pr30703.C

diff --git a/gcc/ChangeLog b/gcc/ChangeLog
index 13cdde386b14..b76b3dd189b1 100644
--- a/gcc/ChangeLog
+++ b/gcc/ChangeLog
@@ -1,5 +1,9 @@
 2007-02-07  Jakub Jelinek  <jakub@redhat.com>
 
+	PR c++/30703
+	* gimplify.c (gimplify_scan_omp_clauses): Remove special casing
+	of INDIRECT_REF <RESULT_DECL>.
+
 	* config/i386/i386.c (override_options): Set PTA_SSSE3 for core2.
 
 2007-02-06  J"orn Rennecke  <joern.rennecke@arc.com>
diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog
index 00892e968054..59f7260dfb0c 100644
--- a/gcc/cp/ChangeLog
+++ b/gcc/cp/ChangeLog
@@ -1,3 +1,11 @@
+2007-02-07  Jakub Jelinek  <jakub@redhat.com>
+
+	PR c++/30703
+	* cp-gimplify.c (cp_genericize_r): Don't dereference invisiref
+	parameters and result decls in omp clauses.
+	(cxx_omp_privatize_by_reference): Pass also invisiref PARM_DECLs
+	by reference.
+
 2007-02-05  Dirk Mueller  <dmueller@suse.de>
 
 	PR bootstrap/30510
diff --git a/gcc/cp/cp-gimplify.c b/gcc/cp/cp-gimplify.c
index 08d22d59eb1a..5ee1931dde12 100644
--- a/gcc/cp/cp-gimplify.c
+++ b/gcc/cp/cp-gimplify.c
@@ -672,6 +672,25 @@ cp_genericize_r (tree *stmt_p, int *walk_subtrees, void *data)
 	   && is_invisiref_parm (TREE_OPERAND (stmt, 0)))
     /* Don't dereference an invisiref RESULT_DECL inside a RETURN_EXPR.  */
     *walk_subtrees = 0;
+  else if (TREE_CODE (stmt) == OMP_CLAUSE)
+    switch (OMP_CLAUSE_CODE (stmt))
+      {
+      case OMP_CLAUSE_PRIVATE:
+      case OMP_CLAUSE_SHARED:
+      case OMP_CLAUSE_FIRSTPRIVATE:
+      case OMP_CLAUSE_LASTPRIVATE:
+      case OMP_CLAUSE_COPYIN:
+      case OMP_CLAUSE_COPYPRIVATE:
+	/* Don't dereference an invisiref in OpenMP clauses.  */
+	if (is_invisiref_parm (OMP_CLAUSE_DECL (stmt)))
+	  *walk_subtrees = 0;
+	break;
+      case OMP_CLAUSE_REDUCTION:
+	gcc_assert (!is_invisiref_parm (OMP_CLAUSE_DECL (stmt)));
+	break;
+      default:
+	break;
+      }
   else if (IS_TYPE_OR_DECL_P (stmt))
     *walk_subtrees = 0;
 
@@ -911,5 +930,5 @@ cxx_omp_clause_dtor (tree clause, tree decl)
 bool
 cxx_omp_privatize_by_reference (tree decl)
 {
-  return TREE_CODE (decl) == RESULT_DECL && DECL_BY_REFERENCE (decl);
+  return is_invisiref_parm (decl);
 }
diff --git a/gcc/gimplify.c b/gcc/gimplify.c
index 1e9975777a3a..7ac43f421555 100644
--- a/gcc/gimplify.c
+++ b/gcc/gimplify.c
@@ -4747,11 +4747,6 @@ gimplify_scan_omp_clauses (tree *list_p, tree *pre_p, bool in_parallel,
 	      remove = true;
 	      break;
 	    }
-	  /* Handle NRV results passed by reference.  */
-	  if (TREE_CODE (decl) == INDIRECT_REF
-	      && TREE_CODE (TREE_OPERAND (decl, 0)) == RESULT_DECL
-	      && DECL_BY_REFERENCE (TREE_OPERAND (decl, 0)))
-	    OMP_CLAUSE_DECL (c) = decl = TREE_OPERAND (decl, 0);
 	  omp_add_variable (ctx, decl, flags);
 	  if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
 	      && OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
@@ -4779,11 +4774,6 @@ gimplify_scan_omp_clauses (tree *list_p, tree *pre_p, bool in_parallel,
 	      remove = true;
 	      break;
 	    }
-	  /* Handle NRV results passed by reference.  */
-	  if (TREE_CODE (decl) == INDIRECT_REF
-	      && TREE_CODE (TREE_OPERAND (decl, 0)) == RESULT_DECL
-	      && DECL_BY_REFERENCE (TREE_OPERAND (decl, 0)))
-	    OMP_CLAUSE_DECL (c) = decl = TREE_OPERAND (decl, 0);
 	do_notice:
 	  if (outer_ctx)
 	    omp_notice_variable (outer_ctx, decl, true);
diff --git a/libgomp/ChangeLog b/libgomp/ChangeLog
index e07a91d86c5f..300e733f54e6 100644
--- a/libgomp/ChangeLog
+++ b/libgomp/ChangeLog
@@ -1,3 +1,8 @@
+2007-02-07  Jakub Jelinek  <jakub@redhat.com>
+
+	PR c++/30703
+	* testsuite/libgomp.c++/pr30703.C: New test.
+
 2007-02-02  Jakub Jelinek  <jakub@redhat.com>
 
 	Revert:
diff --git a/libgomp/testsuite/libgomp.c++/pr30703.C b/libgomp/testsuite/libgomp.c++/pr30703.C
new file mode 100644
index 000000000000..d48efd952d4f
--- /dev/null
+++ b/libgomp/testsuite/libgomp.c++/pr30703.C
@@ -0,0 +1,73 @@
+// PR c++/30703
+// { dg-do run }
+
+#include <omp.h>
+
+extern "C" void abort ();
+
+int ctor, cctor, dtor;
+
+struct A
+{
+  A();
+  A(const A &);
+  ~A();
+  int i;
+};
+
+A::A()
+{
+#pragma omp atomic
+  ctor++;
+}
+
+A::A(const A &r)
+{
+  i = r.i;
+#pragma omp atomic
+  cctor++;
+}
+
+A::~A()
+{
+#pragma omp atomic
+  dtor++;
+}
+
+void
+foo (A a, A b)
+{
+  int i, j = 0;
+#pragma omp parallel for firstprivate (a) lastprivate (a) private (b) schedule (static, 1) num_threads (5)
+  for (i = 0; i < 5; i++)
+    {
+      b.i = 5;
+      if (a.i != 6)
+	#pragma omp atomic
+	  j += 1;
+      a.i = b.i + i + 6;
+    }
+
+  if (j || a.i != 15)
+    abort ();
+}
+
+void
+bar ()
+{
+  A a, b;
+  a.i = 6;
+  b.i = 7;
+  foo (a, b);
+}
+
+int
+main ()
+{
+  omp_set_dynamic (false);
+  if (ctor || cctor || dtor)
+    abort ();
+  bar ();
+  if (ctor + cctor != dtor)
+    abort ();
+}
-- 
GitLab