From f98625f6a5077fa1554d7ea94016f452b79a00e2 Mon Sep 17 00:00:00 2001
From: Mark Mitchell <mark@codesourcery.com>
Date: Thu, 23 Dec 2004 08:14:33 +0000
Subject: [PATCH] re PR c++/16405 (Temporary aggregate copy not elided)

	PR c++/16405
	* gimplify.c (gimplify_modify_expr_rhs): Handle
	INDIRECT_REF/ADDR_EXPR combinations.

	PR c++/16405
	* g++.dg/opt/temp1.C: New test.

From-SVN: r92539
---
 gcc/ChangeLog                    |  6 +++++
 gcc/gimplify.c                   | 27 ++++++++++++++++++++++
 gcc/testsuite/ChangeLog          |  5 ++++
 gcc/testsuite/g++.dg/opt/temp1.C | 39 ++++++++++++++++++++++++++++++++
 4 files changed, 77 insertions(+)
 create mode 100644 gcc/testsuite/g++.dg/opt/temp1.C

diff --git a/gcc/ChangeLog b/gcc/ChangeLog
index 58b8baddd89c..4f8f3a61f016 100644
--- a/gcc/ChangeLog
+++ b/gcc/ChangeLog
@@ -1,3 +1,9 @@
+2004-12-23  Mark Mitchell  <mark@codesourcery.com>
+
+	PR c++/16405
+	* gimplify.c (gimplify_modify_expr_rhs): Handle
+	INDIRECT_REF/ADDR_EXPR combinations.
+
 2004-12-22  Richard Henderson  <rth@redhat.com>
 
 	* config/alpha/alpha.c (alpha_expand_mov): Split out ...
diff --git a/gcc/gimplify.c b/gcc/gimplify.c
index 2434a815c7c3..0f610e4d2424 100644
--- a/gcc/gimplify.c
+++ b/gcc/gimplify.c
@@ -2801,6 +2801,33 @@ gimplify_modify_expr_rhs (tree *expr_p, tree *from_p, tree *to_p, tree *pre_p,
   while (ret != GS_UNHANDLED)
     switch (TREE_CODE (*from_p))
       {
+      case INDIRECT_REF:
+	{
+	  /* If we have code like 
+
+	        *(const A*)(A*)&x
+
+	     where the type of "x" is a (possibly cv-qualified variant
+	     of "A"), treat the entire expression as identical to "x".
+	     This kind of code arises in C++ when an object is bound
+	     to a const reference, and if "x" is a TARGET_EXPR we want
+	     to take advantage of the optimization below.  */
+	  tree pointer;
+
+	  pointer = TREE_OPERAND (*from_p, 0);
+	  STRIP_NOPS (pointer);
+	  if (TREE_CODE (pointer) == ADDR_EXPR
+	      && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (pointer, 0)))
+		  == TYPE_MAIN_VARIANT (TREE_TYPE (*from_p))))
+	    {
+	      *from_p = TREE_OPERAND (pointer, 0); 
+	      ret = GS_OK;
+	    }
+	  else
+	    ret = GS_UNHANDLED;
+	  break;
+	}
+
       case TARGET_EXPR:
 	{
 	  /* If we are initializing something from a TARGET_EXPR, strip the
diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog
index b966939c2ac1..61347db2c40c 100644
--- a/gcc/testsuite/ChangeLog
+++ b/gcc/testsuite/ChangeLog
@@ -1,3 +1,8 @@
+2004-12-23  Mark Mitchell  <mark@codesourcery.com>
+
+	PR c++/16405
+	* g++.dg/opt/temp1.C: New test.
+
 2004-12-23  Giovanni Bajo  <giovannibajo@gcc.gnu.org>
 
 	PR c++/18733
diff --git a/gcc/testsuite/g++.dg/opt/temp1.C b/gcc/testsuite/g++.dg/opt/temp1.C
new file mode 100644
index 000000000000..f34bb8fff582
--- /dev/null
+++ b/gcc/testsuite/g++.dg/opt/temp1.C
@@ -0,0 +1,39 @@
+// PR c++/16405
+// { dg-options "-O2" } 
+
+// There should be exactly one temporary generated for the code in "f"
+// below when optimizing -- for the result of "b + c".  We have no
+// easy way of checking that directly, so we count the number of calls
+// to "memcpy", which is used on (some?) targets to copy temporaries.
+// If there is more than two calls (one for coping "*this" to "t", and
+// one for copying the temporary to "a"), then there are too many
+// temporaries. 
+
+int i;
+
+extern "C"
+void *memcpy (void *dest, const void *src, __SIZE_TYPE__ n)
+{
+  ++i;
+}
+ 
+struct T {
+  int a[128];
+  T &operator+=(T const &v) __attribute__((noinline));
+  T operator+(T const &v) const { T t = *this; t += v; return t; }
+};
+
+T &T::operator+=(T const &v) {
+  return *this;
+}
+
+T a, b, c;
+
+void f() { a = b + c; }
+
+int main () {
+  i = 0;
+  f();
+  if (i > 2)
+    return 1;
+}
-- 
GitLab