[PATCH] Fix useless_type_conversion_p with incomplete types (PR middle-end/38505)

Jakub Jelinek jakub@redhat.com
Mon Dec 15 12:02:00 GMT 2008


On Fri, Dec 12, 2008 at 10:58:49PM +0100, Jakub Jelinek wrote:
> On this testcase memcpy folding creates
>   extern unsigned short d[];
>   short unsigned int[20] * {ref-all} d.0;
>   struct S s;
> ...
>   d.0 = (short unsigned int[20] * {ref-all}) &d;
>   s.b = *d.0;
> but then forwprop1 changes it into:
>   s.b ={v} d;
> which is invalid, given that d has incomplete type.  Fixed thusly,
> bootstrapped/regtested on x86_64-linux.  Ok for trunk?
> 
> 2008-12-12  Jakub Jelinek  <jakub@redhat.com>
> 
> 	PR middle-end/38505
> 	* tree-ssa-ccp.c (may_propagate_address_into_dereference): Return
> 	false if ADDR's operand has incomplete type.
> 
> 	* gcc.c-torture/compile/pr38505.c: New test.

Or alternatively useless_type_conversion_p could return false
when converting an incomplete aggregate type to complete aggregate type,
because throwing away that conversion loses important information (how big
the access is).

Bootstrapped/regtested on x86_64-linux.  Ok for trunk?

2008-12-15  Jakub Jelinek  <jakub@redhat.com>

	PR middle-end/38505
	* tree-ssa.c (useless_type_conversion_p_1): Return
	false if inner_type is incomplete and outer_type is complete.

	* gcc.c-torture/compile/pr38505.c: New test.

--- gcc/tree-ssa.c.jj	2008-11-07 23:00:33.000000000 +0100
+++ gcc/tree-ssa.c	2008-12-15 11:29:44.000000000 +0100
@@ -1188,6 +1188,11 @@ useless_type_conversion_p_1 (tree outer_
       if (TREE_CODE (inner_type) != TREE_CODE (outer_type))
 	return false;
 
+      /* Conversion from an incomplete to a complete type is never
+	 useless.  */
+      if (!COMPLETE_TYPE_P (inner_type) && COMPLETE_TYPE_P (outer_type))
+	return false;
+
       /* ???  This seems to be necessary even for aggregates that don't
 	 have TYPE_STRUCTURAL_EQUALITY_P set.  */
 
--- gcc/testsuite/gcc.c-torture/compile/pr38505.c.jj	2008-12-15 11:25:42.000000000 +0100
+++ gcc/testsuite/gcc.c-torture/compile/pr38505.c	2008-12-15 11:25:42.000000000 +0100
@@ -0,0 +1,23 @@
+/* PR middle-end/38505 */
+/* { dg-do compile } */
+
+struct S
+{
+  unsigned short a[50];
+  unsigned short b[20];
+};
+extern void bar (struct S *);
+extern void baz (unsigned short *);
+extern unsigned short d[];
+
+void
+foo (void)
+{
+  struct S s;
+  unsigned short g[50];
+
+  baz (g);
+  __builtin_memcpy (&s, g, sizeof (g));
+  __builtin_memcpy (s.b, d, sizeof (s.b));
+  bar (&s);
+}

	Jakub



More information about the Gcc-patches mailing list