strict aliasing benefit examples

Paolo Bonzini paolo.bonzini@lu.unisi.ch
Wed Nov 29 08:07:00 GMT 2006


> for(i = 0;i<*t;i++)
>   *f += 1.0;

This one is pretty realistic, especially if you consider C++ and inlining:

	struct s {
	  int size;
	  float *data;
	};

	void f(struct s *d, struct s *s)
	{
	  int i;
	  for (i = 0; i < s->size; i++)
	    d->data[i] += s->data[i];
	}

can be optimized by GCC to this:

         lwz r0,0(r4)
         cmpwi cr7,r0,0
         blelr- cr7
         lwz r3,4(r3)
         mtctr r0
         lwz r4,4(r4)
         li r9,0
L4:
         slwi r2,r9,2
         addi r9,r9,1
         lfsx f0,r3,r2
         lfsx f13,r4,r2
         fadds f0,f0,f13
         stfsx f0,r3,r2
         bdnz L4
         blr

Note that the only load in the loop is for vec->data[i]. vec->size is 
known not to change during the loop.

With an "int *data" it is impossible to disambiguate.  However, 
base+offset alias analysis can disambiguate the accesses to s->size and 
d->data[i] if struct s is defined as follows:

struct s {
   int size;
   int data[1];
};

Paolo



More information about the Gcc mailing list