[PATCH] Fix __builtin_object_size (x, 1) regression (PR tree-optimization/39343)

Jakub Jelinek jakub@redhat.com
Mon Mar 2 19:33:00 GMT 2009


Hi!

The
http://gcc.gnu.org/ml/gcc-patches/2008-08/msg00877.html
patch broke __builtin_object_size (, 1) when unions are involved.
When another union member contains a field of the desired non-array type,
one component ref might be transformed into a different one, for which
bos will return sizeof that type instead of the possibly much larger array.

The following fixes this, bootstrapped/regtested on x86_64-linux.
I've looked also where it makes a difference during bootstrap and it seemed
to trigger just with similar cases with unions, e.g. &t->exp.operands
is no longer changed into &t->decl_minimal.context (completely unrelated,
just happens to have the same field offset), etc.

Ok for trunk?

2009-03-02  Jakub Jelinek  <jakub@redhat.com>

	PR tree-optimization/39343
	* tree-ssa-ccp.c (maybe_fold_offset_to_address): Don't check if
	COMPONENT_REF t has ARRAY_TYPE.

	* gcc.dg/pr39343.c: New test.

--- gcc/tree-ssa-ccp.c.jj	2009-02-20 15:56:54.000000000 +0100
+++ gcc/tree-ssa-ccp.c	2009-03-02 15:46:34.000000000 +0100
@@ -1942,8 +1942,7 @@ maybe_fold_offset_to_address (tree addr,
 	   || (TREE_CODE (orig) == COMPONENT_REF
 	       && TREE_CODE (TREE_TYPE (TREE_OPERAND (orig, 1))) == ARRAY_TYPE))
 	  && (TREE_CODE (t) == ARRAY_REF
-	      || (TREE_CODE (t) == COMPONENT_REF
-		  && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 1))) == ARRAY_TYPE))
+	      || TREE_CODE (t) == COMPONENT_REF)
 	  && !operand_equal_p (TREE_CODE (orig) == ARRAY_REF
 			       ? TREE_OPERAND (orig, 0) : orig,
 			       TREE_CODE (t) == ARRAY_REF
--- gcc/testsuite/gcc.dg/pr39343.c.jj	2009-03-02 15:52:00.000000000 +0100
+++ gcc/testsuite/gcc.dg/pr39343.c	2009-03-02 15:51:25.000000000 +0100
@@ -0,0 +1,29 @@
+/* PR tree-optimization/39343 */
+/* { dg-do run } */
+/* { dg-options "-O2" } */
+
+extern void abort (void);
+
+extern inline __attribute__ ((__always_inline__)) int
+foo (char *dest)
+{
+  return __builtin_object_size (dest, 1);
+}
+
+struct S
+{
+  union
+  {
+    struct { int a, b; char c, d; } f;
+    struct { struct { int a, b; char c, d[255]; } e; } g;
+  } u;
+};
+
+int
+main (void)
+{
+  struct S s;
+  if (foo (s.u.g.e.d) != 255)
+    abort ();
+  return 0;
+}


	Jakub



More information about the Gcc-patches mailing list