This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: RFC: g++ aliasing (was: Re: g++ and aliasing bools)
- From: Dan Nicolaescu <dann at godzilla dot ICS dot UCI dot EDU>
- To: Mark Mitchell <mark at codesourcery dot com>
- Cc: "gcc at gcc dot gnu dot org" <gcc at gcc dot gnu dot org>, Joe Buck <jbuck at synopsys dot com>, Daniel Berlin <dan at dberlin dot org>
- Date: Wed, 06 Mar 2002 16:22:08 -0800
- Subject: Re: RFC: g++ aliasing (was: Re: g++ and aliasing bools)
- References: <200203061128.aa13858@gremlin-relay.ics.uci.edu><3220000.1015449461@gandalf.codesourcery.com>
Mark Mitchell <mark@codesourcery.com> writes:
> > + if (TYPE_USES_VIRTUAL_BASECLASSES (t))
> > + return false;
>
> You need to recursively check non-virtual base classes, too, don't you?
Yep. It's needed when one of the base classes has a member that has an
abstract base.
Updated patch with all your other comments incorporated below.
Plus I added an abort for sanity checking.
C++ alias analysis improvement.
* alias.c (record_component_aliases): Record aliases for base
classes too.
* cp/cp-lang.c (ok_to_generate_alias_set_for_type): New function.
(cxx_get_alias_set): Use it.
*** alias.c.~1.166.~ Tue Mar 5 23:56:32 2002
--- alias.c Tue Mar 5 23:57:14 2002
***************
*** 679,684 ****
--- 679,694 ----
case RECORD_TYPE:
case UNION_TYPE:
case QUAL_UNION_TYPE:
+ /* Recursively record aliases for the base classes, if there are any */
+ if (TYPE_BINFO (type) != NULL && TYPE_BINFO_BASETYPES (type) != NULL)
+ {
+ int i;
+ for (i = 0; i < TREE_VEC_LENGTH (TYPE_BINFO_BASETYPES (type)); i++)
+ {
+ tree binfo = TREE_VEC_ELT (TYPE_BINFO_BASETYPES (type), i);
+ record_alias_subset (superset, get_alias_set (BINFO_TYPE (binfo)));
+ }
+ }
for (field = TYPE_FIELDS (type); field != 0; field = TREE_CHAIN (field))
if (TREE_CODE (field) == FIELD_DECL && ! DECL_NONADDRESSABLE_P (field))
record_alias_subset (superset, get_alias_set (TREE_TYPE (field)));
Index: cp-lang.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cp/cp-lang.c,v
retrieving revision 1.10
diff -c -3 -p -c -r1.10 cp-lang.c
*** cp-lang.c 2002/02/28 07:39:37 1.10
--- cp-lang.c 2002/03/07 00:21:04
*************** static HOST_WIDE_INT cxx_get_alias_set P
*** 97,112 ****
/* Each front end provides its own hooks, for toplev.c. */
const struct lang_hooks lang_hooks = LANG_HOOKS_INITIALIZER;
/* Special routine to get the alias set for C++. */
static HOST_WIDE_INT
cxx_get_alias_set (t)
tree t;
{
! /* It's not yet safe to use alias sets for classes in C++ because
! the TYPE_FIELDs list for a class doesn't mention base classes. */
! if (AGGREGATE_TYPE_P (t))
return 0;
!
return c_common_get_alias_set (t);
}
--- 97,205 ----
/* Each front end provides its own hooks, for toplev.c. */
const struct lang_hooks lang_hooks = LANG_HOOKS_INITIALIZER;
+ /* Check if a C++ type is safe for aliasing.
+ Return TRUE if T safe for aliasing FALSE otherwise. */
+
+ static bool
+ ok_to_generate_alias_set_for_type (t)
+ tree t;
+ {
+ if (AGGREGATE_TYPE_P (t))
+ {
+ if ((TREE_CODE (t) == RECORD_TYPE) || (TREE_CODE (t) == UNION_TYPE))
+ {
+ tree fields;
+ /* PODs are safe. */
+ if (! CLASSTYPE_NON_POD_P(t))
+ return true;
+ /* Classes with virtual baseclasses are not. */
+ if (TYPE_USES_VIRTUAL_BASECLASSES (t))
+ return false;
+ /* Recursively check the base classes. */
+ if (TYPE_BINFO (t) != NULL && TYPE_BINFO_BASETYPES (t) != NULL)
+ {
+ int i;
+ for (i = 0; i < TREE_VEC_LENGTH (TYPE_BINFO_BASETYPES (t)); i++)
+ {
+ tree binfo = TREE_VEC_ELT (TYPE_BINFO_BASETYPES (t), i);
+ if (!ok_to_generate_alias_set_for_type (BINFO_TYPE (binfo)))
+ return false;
+ }
+ }
+ /* Check all the fields. */
+ for (fields = TYPE_FIELDS (t); fields; fields = TREE_CHAIN (fields))
+ {
+ if (TREE_CODE (fields) != FIELD_DECL)
+ continue;
+ if (! ok_to_generate_alias_set_for_type (TREE_TYPE (fields)))
+ return false;
+ }
+ return true;
+ }
+ else if (TREE_CODE (t) == ARRAY_TYPE)
+ return ok_to_generate_alias_set_for_type (TREE_TYPE (t));
+ else
+ /* This should never happen, we dealt with all the aggregate
+ types that can appear in C++ abover. */
+ abort ();
+ }
+ else
+ return true;
+ }
+
/* Special routine to get the alias set for C++. */
static HOST_WIDE_INT
cxx_get_alias_set (t)
tree t;
{
! /* It's not yet safe to use alias sets for some classes in C++ */
! if (!ok_to_generate_alias_set_for_type(t))
return 0;
!
return c_common_get_alias_set (t);
}