This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH] Optimize COMPOUND_LITERAL_EXPRs inside of CONSTRUCTORs in automatic var initializers (PR c/34146)


Hi!

This patch extends a little bit the compound literal optimizations.
In CONSTRUCTORs which initialize automatic vars there is no reason to
differentiate between CONSTRUCTORs and COMPOUND_LITERAL_EXPRs
if their addresses aren't being taken.

The pr33723.c change is to avoid FAILs on that test on many architectures
- the test regexps depended on the exact move_by_pieces etc. target
parameters.

Bootstrapped/regtested on x86_64-linux, bootstrap/regression testing
pending on ppc64-linux.  Ok for trunk?

2007-11-19  Jakub Jelinek  <jakub@redhat.com>

	PR c/34146
	* c-gimplify.c (optimize_compound_literals_in_ctor): New function.
	(c_gimplify_expr): Use it.

	PR c/34146
	* gcc.dg/tree-ssa/pr34146.c: New test.

	PR testsuite/33978
	* gcc.dg/tree-ssa/pr33723.c: Adjust scan pattern to make it less
	dependent on target settings like move_by_pieces etc.

--- gcc/c-gimplify.c.jj	2007-10-29 20:21:02.000000000 +0100
+++ gcc/c-gimplify.c	2007-11-19 15:46:23.000000000 +0100
@@ -208,6 +208,47 @@ gimplify_compound_literal_expr (tree *ex
   return GS_OK;
 }
 
+/* Optimize embedded COMPOUND_LITERAL_EXPRs within a CONSTRUCTOR,
+   return a new CONSTRUCTOR if something changed.  */
+
+static tree
+optimize_compound_literals_in_ctor (tree orig_ctor)
+{
+  tree ctor = orig_ctor;
+  VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (ctor);
+  unsigned int idx, num = VEC_length (constructor_elt, elts);
+
+  for (idx = 0; idx < num; idx++)
+    {
+      tree value = VEC_index (constructor_elt, elts, idx)->value;
+      tree newval = value;
+      if (TREE_CODE (value) == CONSTRUCTOR)
+	newval = optimize_compound_literals_in_ctor (value);
+      else if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
+	{
+	  tree decl_s = COMPOUND_LITERAL_EXPR_DECL_STMT (value);
+	  tree decl = DECL_EXPR_DECL (decl_s);
+	  tree init = DECL_INITIAL (decl);
+
+	  if (!TREE_ADDRESSABLE (value)
+	      && !TREE_ADDRESSABLE (decl)
+	      && init)
+	    newval = init;
+	}
+      if (newval == value)
+	continue;
+
+      if (ctor == orig_ctor)
+	{
+	  ctor = copy_node (orig_ctor);
+	  CONSTRUCTOR_ELTS (ctor) = VEC_copy (constructor_elt, gc, elts);
+	  elts = CONSTRUCTOR_ELTS (ctor);
+	}
+      VEC_index (constructor_elt, elts, idx)->value = newval;
+    }
+  return ctor;
+}
+
 /* Do C-specific gimplification.  Args are as for gimplify_expr.  */
 
 int
@@ -254,6 +295,18 @@ c_gimplify_expr (tree *expr_p, tree *pre
 	      return GS_OK;
 	    }
 	}
+      else if (TREE_CODE (TREE_OPERAND (*expr_p, 1)) == CONSTRUCTOR)
+	{
+	  tree ctor
+	    = optimize_compound_literals_in_ctor (TREE_OPERAND (*expr_p, 1));
+
+	  if (ctor != TREE_OPERAND (*expr_p, 1))
+	    {
+	      *expr_p = copy_node (*expr_p);
+	      TREE_OPERAND (*expr_p, 1) = ctor;
+	      return GS_OK;
+	    }
+	}
       return GS_UNHANDLED;
 
     default:
--- gcc/testsuite/gcc.dg/tree-ssa/pr34146.c.jj	2007-11-19 17:07:22.000000000 +0100
+++ gcc/testsuite/gcc.dg/tree-ssa/pr34146.c	2007-11-19 17:05:19.000000000 +0100
@@ -0,0 +1,53 @@
+/* PR c/34146 */
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-gimple" } */
+
+struct A
+{
+  int f1, f2, f3;
+};
+
+struct B
+{
+  struct A g1, g2;
+};
+
+struct C
+{
+  struct B h1, h2;
+};
+
+typedef union
+{
+  struct C c;
+  char s[4];
+  long int a;
+} T;
+
+void
+foo (void)
+{
+  T t = { { { { 0, 0, 0 }, { 0, 0, 0 } }, { { 0, 0, 0 }, { 0, 0, 0 } } } };
+  test (&t);
+}
+
+void
+bar (void)
+{
+  T t = { { { { 0, 0, 0 }, (struct A) { 0, 0, 0 } },
+	    (struct B) { (struct A) { 0, 0, 0 }, { 0, 0, 0 } } } };
+  test (&t);
+}
+
+void
+baz (void)
+{
+  T t = { { { { 0, 0, 0 }, (struct A) { 1, 1, 1 } },
+	    (struct B) { (struct A) { 0, 0, 0 }, { 1, 1, 1 } } } };
+  test (&t);
+}
+
+/* { dg-final { scan-tree-dump-not "t = D" "gimple"} } */
+/* { dg-final { scan-tree-dump-not "t\.c\.h\[12\] = D" "gimple"} } */
+/* { dg-final { scan-tree-dump-not "\.g\[12\] = D" "gimple"} } */
+/* { dg-final { cleanup-tree-dump "gimple" } } */
--- gcc/testsuite/gcc.dg/tree-ssa/pr33723.c.jj	2007-10-30 14:24:09.000000000 +0100
+++ gcc/testsuite/gcc.dg/tree-ssa/pr33723.c	2007-11-19 16:57:27.000000000 +0100
@@ -68,7 +68,5 @@ baz3 (void)
   test (&t);
 }
 
-/* { dg-final { scan-tree-dump-times "t = {}" 3 "gimple"} } */
-/* { dg-final { scan-tree-dump-times "t.f.f1 = 1" 4 "gimple"} } */
-/* { dg-final { scan-tree-dump-times "t.f.f8 = 8" 4 "gimple"} } */
+/* { dg-final { scan-tree-dump-not "t = D" "gimple"} } */
 /* { dg-final { cleanup-tree-dump "gimple" } } */

	Jakub


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]