* c-common.c (c_common_attribute_table): Add "may_alias" entry.
authorrth <rth@138bc75d-0d04-0410-961f-82ee72b054a4>
Thu, 30 May 2002 21:28:17 +0000 (21:28 +0000)
committerrth <rth@138bc75d-0d04-0410-961f-82ee72b054a4>
Thu, 30 May 2002 21:28:17 +0000 (21:28 +0000)
        (c_common_get_alias_set): Handle it.
        * doc/extend.texi: Document it.

        * gcc.c-torture/execute/mayalias-1.c: New file.

git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@54074 138bc75d-0d04-0410-961f-82ee72b054a4

gcc/ChangeLog
gcc/c-common.c
gcc/doc/extend.texi
gcc/testsuite/ChangeLog
gcc/testsuite/gcc.c-torture/execute/mayalias-1.c [new file with mode: 0644]

index f145158..4890db3 100644 (file)
@@ -1,3 +1,9 @@
+2002-05-30  Osku Salerma  <osku@iki.fi>
+
+       * c-common.c (c_common_attribute_table): Add "may_alias" entry.
+       (c_common_get_alias_set): Handle it.
+       * doc/extend.texi: Document it.
+
 2002-05-30  Richard Henderson  <rth@redhat.com>
 
        * defaults.h (TARGET_ALLOWS_PROFILING_WITHOUT_FRAME_POINTER): Kill.
index b1a9205..8a554e8 100644 (file)
@@ -430,6 +430,7 @@ const struct attribute_spec c_common_attribute_table[] =
                              handle_nonnull_attribute },
   { "nothrow",                0, 0, true,  false, false,
                              handle_nothrow_attribute },
+  { "may_alias",             0, 0, false, true, false, NULL },
   { NULL,                     0, 0, false, false, false, NULL }
 };
 
@@ -2551,6 +2552,10 @@ c_common_get_alias_set (t)
       && TYPE_PRECISION (TREE_TYPE (t)) == TYPE_PRECISION (char_type_node))
     return 0;
 
+  /* If it has the may_alias attribute, it can alias anything.  */
+  if (TYPE_P (t) && lookup_attribute ("may_alias", TYPE_ATTRIBUTES (t)))
+    return 0;
+
   /* That's all the expressions we handle specially.  */
   if (! TYPE_P (t))
     return -1;
index 87701ef..4b8d496 100644 (file)
@@ -3149,10 +3149,11 @@ packed))}.
 The keyword @code{__attribute__} allows you to specify special
 attributes of @code{struct} and @code{union} types when you define such
 types.  This keyword is followed by an attribute specification inside
-double parentheses.  Five attributes are currently defined for types:
+double parentheses.  Six attributes are currently defined for types:
 @code{aligned}, @code{packed}, @code{transparent_union}, @code{unused},
-and @code{deprecated}.  Other attributes are defined for functions
-(@pxref{Function Attributes}) and for variables (@pxref{Variable Attributes}).
+@code{deprecated} and @code{may_alias}.  Other attributes are defined for
+functions (@pxref{Function Attributes}) and for variables
+(@pxref{Variable Attributes}).
 
 You may also specify any one of these attributes with @samp{__}
 preceding and following its keyword.  This allows you to use these
@@ -3363,6 +3364,36 @@ deprecated.  Similarly for line 6.
 The @code{deprecated} attribute can also be used for functions and
 variables (@pxref{Function Attributes}, @pxref{Variable Attributes}.)
 
+@item may_alias
+Accesses to objects with types with this attribute are not subjected to
+type-based alias analysis, but are instead assumed to be able to alias
+any other type of objects, just like the @code{char} type.  See
+@option{-fstrict-aliasing} for more information on aliasing issues.
+
+Example of use:
+
+@example
+typedef short __attribute__((__may_alias__)) short_a;
+
+int
+main (void)
+@{
+  int a = 0x12345678;
+  short_a *b = (short_a *) &a;
+
+  b[1] = 0;
+
+  if (a == 0x12345678)
+    abort();
+
+  exit(0);
+@}
+@end example
+
+If you replaced @code{short_a} with @code{short} in the variable
+declaration, the above program would abort when compiled with
+@option{-fstrict-aliasing}, which is on by default at @option{-O2} or
+above in recent GCC versions.
 @end table
 
 To specify multiple attributes, separate them by commas within the
index b3e2fde..8e06317 100644 (file)
@@ -1,3 +1,7 @@
+2002-05-30  Osku Salerma  <osku@iki.fi>
+
+       * gcc.c-torture/execute/mayalias-1.c: New file.
+
 2002-05-29  Neil Booth  <neil@daikokuya.demon.co.uk>
 
        * gcc.dg/cpp/c++98-pedantic.c, gcc.dg/cpp/c89-pedantic.c, 
diff --git a/gcc/testsuite/gcc.c-torture/execute/mayalias-1.c b/gcc/testsuite/gcc.c-torture/execute/mayalias-1.c
new file mode 100644 (file)
index 0000000..ba461ca
--- /dev/null
@@ -0,0 +1,21 @@
+/* Tests that the may_alias attribute works as expected.
+   Author: Osku Salerma <osku@iki.fi> Apr 2002.  */
+extern void abort(void);
+extern void exit(int);
+
+typedef short __attribute__((__may_alias__)) short_a;
+
+int
+main (void)
+{
+  int a = 0x12345678;
+  short_a *b = (short_a*) &a;
+
+  b[1] = 0;
+
+  if (a == 0x12345678)
+    abort();
+
+  exit(0);
+}