This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Invariant is not moved out of loop
- From: Astor Piaz <appiazzolla at gmail dot com>
- To: Alexander Monakov <amonakov at ispras dot ru>
- Cc: gcc-help at gcc dot gnu dot org
- Date: Tue, 25 Jul 2017 18:04:06 +0200
- Subject: Re: Invariant is not moved out of loop
- Authentication-results: sourceware.org; auth=none
- References: <CAFbmpW=QGdCC+Sd7PxaOzr=95TjJ89kdxqjurpu9-S_VjsEu6Q@mail.gmail.com> <alpine.LNX.2.20.13.1707251803300.2275@monopod.intra.ispras.ru>
Hi Alexander,
Thank you, your answer is pretty clear and your example is much better.
Could I ask you though, why wouldn't this be allowed once the user has
requested -fassociative-math ?
Is there a fundamental problem to provide this optimization or it is
just too difficult?
Thanks a lot.
Best,
--
Astor
On Tue, Jul 25, 2017 at 5:11 PM, Alexander Monakov <amonakov@ispras.ru> wrote:
> 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