This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: [patch] may_alias attribute
- From: Osku Salerma <osku at iki dot fi>
- To: "Joseph S. Myers" <jsm28 at cam dot ac dot uk>
- Cc: gcc-patches at gcc dot gnu dot org
- Date: Mon, 28 Jan 2002 21:36:40 +0200 (EET)
- Subject: Re: [patch] may_alias attribute
Hopefully the last version of the patch. No code changes, testcase
moved to a different directory and some documentation polishing. No
cross-reference for strict aliasing though, didn't find any examples
of its use in cross-file references in the gcc docs, and anyway it's
no big deal to me at least, it takes less than 5 seconds to find the
strict aliasing section of the manual.
2002-01-28 Osku Salerma <osku@iki.fi>
* attribs.c (c_common_attribute_table): Add "may_alias" entry.
* c-common.c (c_common_get_alias_set): Handle "may_alias"
attribute.
* doc/extend.texi: Add documentation for "may_alias" attribute.
2002-01-28 Osku Salerma <osku@iki.fi>
* gcc.c-torture/execute/may_alias-1.c: New file.
Index: attribs.c
===================================================================
RCS file: /cvsroot/gcc/gcc/gcc/attribs.c,v
retrieving revision 1.11
diff -c -3 -p -r1.11 attribs.c
*** attribs.c 17 Jan 2002 20:34:40 -0000 1.11
--- attribs.c 28 Jan 2002 19:28:42 -0000
*************** static const struct attribute_spec c_com
*** 140,145 ****
--- 140,147 ----
handle_no_limit_stack_attribute },
{ "pure", 0, 0, true, false, false,
handle_pure_attribute },
+ { "may_alias", 0, 0, false, true, false,
+ NULL },
{ "deprecated", 0, 0, false, false, false,
handle_deprecated_attribute },
{ "vector_size", 1, 1, false, true, false,
Index: c-common.c
===================================================================
RCS file: /cvsroot/gcc/gcc/gcc/c-common.c,v
retrieving revision 1.287
diff -c -3 -p -r1.287 c-common.c
*** c-common.c 15 Jan 2002 22:27:07 -0000 1.287
--- c-common.c 28 Jan 2002 19:28:44 -0000
*************** c_common_get_alias_set (t)
*** 2239,2244 ****
--- 2239,2249 ----
&& 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))
+ != NULL_TREE))
+ return 0;
+
/* That's all the expressions we handle specially. */
if (! TYPE_P (t))
return -1;
Index: doc/extend.texi
===================================================================
RCS file: /cvsroot/gcc/gcc/gcc/doc/extend.texi,v
retrieving revision 1.58
diff -c -3 -p -r1.58 extend.texi
*** doc/extend.texi 23 Jan 2002 07:51:16 -0000 1.58
--- doc/extend.texi 28 Jan 2002 19:28:49 -0000
*************** packed))}.
*** 3066,3075 ****
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:
@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}).
You may also specify any one of these attributes with @samp{__}
preceding and following its keyword. This allows you to use these
--- 3066,3076 ----
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. Six attributes are currently defined for types:
@code{aligned}, @code{packed}, @code{transparent_union}, @code{unused},
! ,@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
*************** that type, even if the variable appears
*** 3251,3256 ****
--- 3252,3288 ----
the case with lock or thread classes, which are usually defined and then
not referenced, but contain constructors and destructors that have
nontrivial bookkeeping functions.
+
+ @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
+ -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 -O2 or above in
+ recent GCC versions.
@item deprecated
The @code{deprecated} attribute results in a warning if the type
gcc.c-torture/execute/:
/* Tests that the may_alias attribute works as expected.
Author: Osku Salerma <osku@iki.fi> Jan 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);
}
--
Osku Salerma - osku@iki.fi - http://www.iki.fi/osku/
Information wants to be free.