This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[patch] COMPONENT_REFs referring to non-aliasing structures don't alias - take 2



[this version moves the new code to the proper place]


The following code:

struct first
{
  double d;
  int f1;
};

struct second 
{
  int f2;
  short a;
};

void
g0 (struct second  s2, struct first s1)
{
  s1.f1++;
  s2.f2++;
  s1.f1++;
}

void
g1 (struct second *s2, struct first *s1)
{
  s1->f1++;
  s2->f2++;
  s1->f1++;
}

compiled with -O2 on sparc-sun-solaris2.7 produces:

g0:
	!#PROLOGUE# 0
	save	%sp, -112, %sp
	!#PROLOGUE# 1
	ld	[%i1+8], %i3
	add	%i3, 1, %i3
	st	%i3, [%i1+8]
	ld	[%i0], %i2
	add	%i2, 1, %i2
	st	%i2, [%i0]
	ld	[%i1+8], %i0    <= extra load
	add	%i0, 1, %i0
	st	%i0, [%i1+8]
	ret
	restore

g1:
	!#PROLOGUE# 0
	save	%sp, -112, %sp
	!#PROLOGUE# 1
	ld	[%i1+8], %i3
	add	%i3, 1, %i3
	st	%i3, [%i1+8]
	ld	[%i0], %i2
	add	%i2, 1, %i2
	st	%i2, [%i0]
	ld	[%i1+8], %i0   <= extra load
	add	%i0, 1, %i0
	st	%i0, [%i1+8]
	ret
	restore


As "struct first" and "struct second" cannot alias, s1.f1 and s2.f2
cannot alias either.
GCC currently has enough information to determine that "struct first"
and "struct second" don't alias, it's just not using it.

The attached patch fixes that. GCC now generates:

g0:
	!#PROLOGUE# 0
	save	%sp, -112, %sp
	!#PROLOGUE# 1
	ld	[%i1+8], %i3
	ld	[%i0], %i2
	add	%i3, 2, %i3
	add	%i2, 1, %i2
	st	%i2, [%i0]
	st	%i3, [%i1+8]
	ret
	restore

g1:
	!#PROLOGUE# 0
	save	%sp, -112, %sp
	!#PROLOGUE# 1
	ld	[%i1+8], %i3
	ld	[%i0], %i2
	add	%i3, 2, %i3
	add	%i2, 1, %i2
	st	%i2, [%i0]
	st	%i3, [%i1+8]
	ret
	restore


after the patch the stripped cc1 binary is slightly smaller:

-rwx------   1 dann     amrm     3227972 Mar  7 10:33 cc1-new*
-rwx------   1 dann     amrm     3230356 Mar  7 10:33 cc1-old*


Comments? 

Can unions get the same treatment? 


2002-03-07  Dan Nicolaescu  <dann@ics.uci.edu>

	* alias.c (nonoverlapping_component_refs_p): COMPONENT_REFs
	referring to non-aliasing structures don't overlap.


*** alias.c.~1.166.~	Tue Mar  5 23:56:32 2002
--- alias.c	Mon Mar 11 01:01:09 2002
*************** nonoverlapping_component_refs_p (x, y)
*** 1799,1804 ****
--- 1809,1825 ----
  {
    tree fieldx, fieldy, typex, typey, orig_y;
  
+   /* Component refs that belong to structures 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 1;
+   
    do
      {
        /* The comparison has to be done at a common type, since we don't


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]