This is the mail archive of the gcc-help@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]

Re: Invariant is not moved out of loop


Hi,

I think what you're asking is not the "usual" loop invariant motion, but rather
applying distributive law to reductions. In your example you want the optimizer
to replace

  R = 0;
  for ( ... )
    R += X * C;

by

  R = 0;
  for ( ... )
    R += X;
  R *= C;

We don't do that even for integer operands, the following isn't optimized either:
(neither do Clang and ICC according to my experiments on gcc.godbolt.org)

  int f(int *a)
  {
    int r = 0;
    for (int i = 0; i < 1024; i++)
      r += a[i] * 5;
    return r;
  }

Alexander


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