public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* Alias sets for VLAs and strict aliasing warnings (PR 41673)
@ 2009-10-23  3:14 Joseph S. Myers
  2009-10-23  9:23 ` Richard Guenther
  0 siblings, 1 reply; 8+ messages in thread
From: Joseph S. Myers @ 2009-10-23  3:14 UTC (permalink / raw)
  To: gcc-patches

This patch fixes PR 41673, where a VLA being given alias set 0 caused
a bogus -Wstrict-aliasing warning from dereference of that array
converted to a pointer (which involves a conversion from
pointer-to-array to pointer-to-element that looks like a cast for the
purposes of these warnings).

I believe it is appropriate for both warnings and optimization for
VLAs to get the alias sets of their elements (rather than special
casing VLAs in the warning code, for example), so this patch makes it
possible for langhooks to override the default of alias set 0 for
types with structural comparison.

Bootstrapped with no regressions on x86_64-unknown-linux-gnu.  OK to
commit (the alias.c changes)?

2009-10-22  Joseph Myers  <joseph@codesourcery.com>

	PR c/41673
	* alias.c (get_alias_set): Call langhook before returning 0 for
	types with structural equality.
	* c-common.c (c_common_get_alias_set): Use alias set of element
	type for arrays with structural comparison.

testsuite:
2009-10-22  Joseph Myers  <joseph@codesourcery.com>

	PR c/41673
	* gcc.dg/Wstrict-aliasing-bogus-vla-1.c: New test.

Index: testsuite/gcc.dg/Wstrict-aliasing-bogus-vla-1.c
===================================================================
--- testsuite/gcc.dg/Wstrict-aliasing-bogus-vla-1.c	(revision 0)
+++ testsuite/gcc.dg/Wstrict-aliasing-bogus-vla-1.c	(revision 0)
@@ -0,0 +1,10 @@
+/* PR 41673: bogus -Wstrict-aliasing warning from VLA dereference.  */
+/* { dg-do compile } */
+/* { dg-options "-std=gnu99 -O2 -Wall" } */
+
+int main(int argc, char *argv[])
+{
+    float x[argc];
+    float y[argc];
+    return 0 == __builtin_memcpy(y, x, argc * sizeof(*x));
+}
Index: alias.c
===================================================================
--- alias.c	(revision 153482)
+++ alias.c	(working copy)
@@ -691,7 +691,14 @@ get_alias_set (tree t)
      requires structural comparisons to identify compatible types
      use alias set zero.  */
   if (TYPE_STRUCTURAL_EQUALITY_P (t))
-    return 0;
+    {
+      /* Allow the language to specify another alias set for this
+	 type.  */
+      set = lang_hooks.get_alias_set (t);
+      if (set != -1)
+	return set;
+      return 0;
+    }
   t = TYPE_CANONICAL (t);
   /* Canonical types shouldn't form a tree nor should the canonical
      type require structural equality checks.  */
Index: c-common.c
===================================================================
--- c-common.c	(revision 153482)
+++ c-common.c	(working copy)
@@ -4183,6 +4183,15 @@ c_common_get_alias_set (tree t)
   tree u;
   PTR *slot;
 
+  /* For VLAs, use the alias set of the element type rather than the
+     default of alias set 0 for types compared structurally.  */
+  if (TYPE_P (t) && TYPE_STRUCTURAL_EQUALITY_P (t))
+    {
+      if (TREE_CODE (t) == ARRAY_TYPE)
+	return get_alias_set (TREE_TYPE (t));
+      return -1;
+    }
+
   /* Permit type-punning when accessing a union, provided the access
      is directly through the union.  For example, this code does not
      permit taking the address of a union member and then storing

-- 
Joseph S. Myers
joseph@codesourcery.com

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2009-10-24 11:47 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-10-23  3:14 Alias sets for VLAs and strict aliasing warnings (PR 41673) Joseph S. Myers
2009-10-23  9:23 ` Richard Guenther
2009-10-23 12:29   ` Joseph S. Myers
2009-10-23 12:40     ` Richard Guenther
2009-10-23 12:41       ` Joseph S. Myers
2009-10-23 13:07         ` Richard Guenther
2009-10-23 14:28           ` Richard Guenther
2009-10-24 14:35             ` Richard Guenther

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).