This is the mail archive of the egcs@egcs.cygnus.com mailing list for the EGCS project. See the EGCS home page for more information.


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

Re: Optimization



  In message <Pine.SGI.3.96.980326123604.8153E-100000@purcell>you write:
  > 
  > Hi! I'm working with computational mathematics using c and c++.
  > 
  > Does g++/gcc have any optimization that brings out constant subexpressions
  > outside loops? Consider the following:
  > 
  > void sqr(float *x, int *step) {
  >   int i,j,k;
  > 
  >   for (i=0; i<100; i++)
  >     for (j=0; j<100; j++)
  >       for (k=0; k<100; k++)
  >         *(x+i*step[2]+j*step[1]+k*step[0]) *= 
  >           *(x+i*step[2]+j*step[1]+k*step[0]);
  > }
  > 
  > The resulting assembler with egcs 1.0.2 (i686-pc-linux-gnulibc1) is not
  > very efficient, because the integer expressions are calculated again
  > and again inside the innermost loop. Can I make the compiler translate the
  > above code into something like the following:
  > 
  > void sqr(float *x, int *step) {
  >   int i,j,k; 
  >   float *xi,*xj;
  > 
  >   for (i=0; i<100; i++) {
  >     xi=x+i*step[2];
  >     for (j=0; j<100; j++)  {
  >       xj=xi+j*step[1];
  >       for (k=0; k<100; k++)  {
  >         *(xj+k*step[0]) *=
  >           *(xj+k*step[0]);
  >       }
  >     }
  >   }
  > }
  > 
  > If this is impossible with the current version, would it be hard to
  > add this optimization to the compiler? The resulting assembler is much
  > more efficient.
An FYI -- current development snapshots perform this optimization and thus
egcs-1.2 will perform this optimization when released.

This optimization was made possible by Mark Mitchell's type based alias
analysis will allows the compiler to realize that the floating point stores
in the loop can not access the same memory as the integer loads that were
initially in the loop.  This exposes the integer loads to loop invariant
code motion optimizations.


jeff