This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
COMPONENT_REFs referring to non-aliasing structures don't alias - take 3
- From: Dan Nicolaescu <dann at godzilla dot ICS dot UCI dot EDU>
- To: gcc-patches at gcc dot gnu dot org
- Cc: Geoff Keating <geoffk at geoffk dot org>, Tom Tromey <tromey at redhat dot com>
- Date: Thu, 28 Mar 2002 00:43:54 -0800
- Subject: COMPONENT_REFs referring to non-aliasing structures don't alias - take 3
- References: <200203110805.aa03993@gremlin-relay.ics.uci.edu><jmpu2b561c.fsf@desire.geoffk.org>
Given that it's not clear yet that this optimization is valid for
C, it makes sense to allow other languages to implement it using a
langhook (for example it's perfectly valid in Java).
The new version introduces a langhook and implements the optimization
for Java.
Example java code:
class first { double i; char a; char f1;};
class second { char b; char f2;};
public class t
{
public void f (first ps1, second ps2)
{
ps1.f1++;
ps2.f2++;
ps1.f1++;
ps2.f2++;
}
}
generated SPARC assembly:
before: after:
!#PROLOGUE# 0 !#PROLOGUE# 0
!#PROLOGUE# 1 !#PROLOGUE# 1
lduh [%o1+18], %o3 lduh [%o1+18], %o0
add %o3, 1, %o3 lduh [%o2+10], %o3
sth %o3, [%o1+18] add %o0, 2, %o0
lduh [%o2+10], %o0 add %o3, 2, %o3
add %o0, 1, %o0 sth %o0, [%o1+18]
sth %o0, [%o2+10] retl
lduh [%o1+18], %o3 sth %o3, [%o2+10]
add %o3, 1, %o3
sth %o3, [%o1+18]
lduh [%o2+10], %o0
add %o0, 1, %o0
retl
sth %o0, [%o2+10]
2002-03-28 Dan Nicolaescu <dann@ics.uci.edu>
* langhooks-def.h (LANG_HOOKS_NONOVERLAPPING_COMPONENT_REFS_P): New.
* langhooks-def.h (LANG_HOOKS_INITIALIZER): Add it.
* langhooks.h (struct lang_hooks): New hook
nonoverlapping_component_refs_p.
* langhooks.c (lhd_nonoverlapping_component_refs_p): New.
* alias.c (nonoverlapping_component_refs_p): Call the new langhook.
* java/lang.c (LANG_HOOKS_NONOVERLAPPING_COMPONENT_REFS_P): Define.
(java_nonoverlapping_component_refs_p): New.
Index: langhooks-def.h
===================================================================
RCS file: /cvs/gcc/gcc/gcc/langhooks-def.h,v
retrieving revision 1.10
diff -c -3 -p -r1.10 langhooks-def.h
*** langhooks-def.h 2002/03/08 19:20:47 1.10
--- langhooks-def.h 2002/03/28 08:20:48
*************** extern void lhd_do_nothing PARAMS ((void
*** 41,46 ****
--- 41,47 ----
extern void lhd_do_nothing_t PARAMS ((tree));
extern int lhd_decode_option PARAMS ((int, char **));
extern HOST_WIDE_INT lhd_get_alias_set PARAMS ((tree));
+ extern bool lhd_nonoverlapping_component_refs_p PARAMS ((tree, tree));
extern tree lhd_return_tree PARAMS ((tree));
extern int lhd_safe_from_p PARAMS ((rtx, tree));
extern int lhd_staticp PARAMS ((tree));
*************** void lhd_tree_inlining_end_inlining PAR
*** 73,78 ****
--- 74,81 ----
#define LANG_HOOKS_DECODE_OPTION lhd_decode_option
#define LANG_HOOKS_POST_OPTIONS lhd_do_nothing
#define LANG_HOOKS_GET_ALIAS_SET lhd_get_alias_set
+ #define LANG_HOOKS_NONOVERLAPPING_COMPONENT_REFS_P \
+ lhd_nonoverlapping_component_refs_p
#define LANG_HOOKS_EXPAND_CONSTANT lhd_return_tree
#define LANG_HOOKS_SAFE_FROM_P lhd_safe_from_p
#define LANG_HOOKS_STATICP lhd_staticp
*************** int lhd_tree_dump_type_quals PARAMS ((
*** 143,148 ****
--- 146,152 ----
LANG_HOOKS_FINISH, \
LANG_HOOKS_CLEAR_BINDING_STACK, \
LANG_HOOKS_GET_ALIAS_SET, \
+ LANG_HOOKS_NONOVERLAPPING_COMPONENT_REFS_P, \
LANG_HOOKS_EXPAND_CONSTANT, \
LANG_HOOKS_SAFE_FROM_P, \
LANG_HOOKS_STATICP, \
Index: langhooks.h
===================================================================
RCS file: /cvs/gcc/gcc/gcc/langhooks.h,v
retrieving revision 1.17
diff -c -3 -p -r1.17 langhooks.h
*** langhooks.h 2002/03/08 19:20:47 1.17
--- langhooks.h 2002/03/28 08:20:48
*************** struct lang_hooks
*** 111,116 ****
--- 111,119 ----
Returns -1 if the language does nothing special for it. */
HOST_WIDE_INT (*get_alias_set) PARAMS ((tree));
+ /* Called to determine if 2 COMPONENT_REFs can not alias. */
+ bool (*nonoverlapping_component_refs_p) PARAMS ((tree, tree));
+
/* Called with an expression that is to be processed as a constant.
Returns either the same expression or a language-independent
constant equivalent to its input. */
Index: langhooks.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/langhooks.c,v
retrieving revision 1.17
diff -c -3 -p -r1.17 langhooks.c
*** langhooks.c 2002/02/28 07:39:24 1.17
--- langhooks.c 2002/03/28 08:20:48
*************** lhd_get_alias_set (t)
*** 124,129 ****
--- 124,140 ----
return -1;
}
+ /* Provide a default routine for nonoverlapping COMPONENT_REF that
+ always returns false. This is used by languages that don't need to
+ do anything special. */
+
+ bool
+ lhd_nonoverlapping_component_refs_p (x, y)
+ tree x ATTRIBUTE_UNUSED, y ATTRIBUTE_UNUSED;
+ {
+ return false;
+ }
+
/* Provide a hook routine for alias sets that always returns 0. This is
used by languages that haven't deal with alias sets yet. */
Index: alias.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/alias.c,v
retrieving revision 1.167
diff -c -3 -p -r1.167 alias.c
*** alias.c 2002/03/11 18:01:53 1.167
--- alias.c 2002/03/28 08:20:48
*************** nonoverlapping_component_refs_p (x, y)
*** 1810,1815 ****
--- 1855,1864 ----
{
tree fieldx, fieldy, typex, typey, orig_y;
+ /* Some languages can determine that COMPONENT_REFs don't
+ overlap by just looking at their types. */
+ if ((*lang_hooks.nonoverlapping_component_refs_p) (x, y))
+ return true;
do
{
/* The comparison has to be done at a common type, since we don't
Index: java/lang.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/java/lang.c,v
retrieving revision 1.89
diff -c -3 -p -r1.89 lang.c
*** lang.c 2002/03/13 01:42:42 1.89
--- lang.c 2002/03/28 08:20:48
*************** static void lang_print_error PARAMS ((di
*** 59,64 ****
--- 59,65 ----
static int process_option_with_no PARAMS ((const char *,
const struct string_option *,
int));
+ static bool java_nonoverlapping_component_refs_p PARAMS ((tree, tree));
#ifndef TARGET_OBJECT_SUFFIX
# define TARGET_OBJECT_SUFFIX ".o"
*************** static int dependency_tracking = 0;
*** 223,228 ****
--- 224,232 ----
#define LANG_HOOKS_SET_YYDEBUG java_set_yydebug
#undef LANG_HOOKS_DUP_LANG_SPECIFIC_DECL
#define LANG_HOOKS_DUP_LANG_SPECIFIC_DECL java_dup_lang_specific_decl
+ #undef LANG_HOOKS_NONOVERLAPPING_COMPONENT_REFS_P
+ #define LANG_HOOKS_NONOVERLAPPING_COMPONENT_REFS_P \
+ java_nonoverlapping_component_refs_p
/* Each front end provides its own. */
const struct lang_hooks lang_hooks = LANG_HOOKS_INITIALIZER;
*************** java_init_options ()
*** 746,749 ****
--- 750,774 ----
flag_bounds_check = 1;
flag_exceptions = 1;
flag_non_call_exceptions = 1;
+ }
+
+
+ /* Return true if the COMPONENT_REFs X and Y cannot alias. */
+
+ static bool
+ java_nonoverlapping_component_refs_p (x, y)
+ tree x, y;
+ {
+ tree typex, typey;
+ /* Component refs that refer to types that are known not to
+ alias, don't alias. */
+ typex = DECL_CONTEXT (TREE_OPERAND (x, 1));
+ typey = DECL_CONTEXT (TREE_OPERAND (y, 1));
+ if (typex != NULL && typey != NULL
+ && TREE_CODE (typex) == RECORD_TYPE
+ && TREE_CODE (typey) == RECORD_TYPE
+ && ! alias_sets_conflict_p (get_alias_set (typex),
+ get_alias_set (typey)))
+ return true;
+ return false;
}