This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Invariant is not moved out of loop
- From: Astor Piaz <appiazzolla at gmail dot com>
- To: gcc-help at gcc dot gnu dot org
- Date: Tue, 25 Jul 2017 15:15:34 +0200
- Subject: Invariant is not moved out of loop
- Authentication-results: sourceware.org; auth=none
Hello All,
I have two nested loops and I cannot get the loop invariants to move
using gcc 7.1.0 as shown in the assembly.
I attach a simple example to the end of this e-mail. I am using -Ofast
-fmove-loop-invariants -fassociative-math.
Would you be so kind as to help me figuring out what I am doing wrong?
Thanks,
--
Astor
Original loop:
int main()
{
const unsigned int k=10;
const double u[k*k*k] = {3.14} ;
const double phi[k] = {3.14} ;
double f = 0.;
for (unsigned int i3 = 0;i3<k;++i3)
for (unsigned int i2 = 0;i2<k;++i2)
for (unsigned int i1 = 0;i1<k;++i1)
f += u[i1+k*(i2+k*i3)] * phi[i1] * phi[i2] * phi[i3];
return static_cast<unsigned int>(f);
}
Loop with the invariants moved:
int main()
{
const unsigned int k=10;
const double u[k*k*k] = {3.14} ;
const double phi[k] = {3.14} ;
double f3 = 0.;
for (unsigned int i3 = 0;i3<k;++i3)
{
const unsigned int k3 = k*i3;
double f2 = 0.;
for (unsigned int i2 = 0;i2<k;++i2)
{
const unsigned int k2 = k*(i2+k3);
double f1 = 0.;
for (unsigned int i1 = 0;i1<k;++i1)
{
f1 += u[i1+k2] * phi[i1];
}
f2 += f1 * phi[i2];
}
f3 += f2 * phi[i3];
}
return static_cast<unsigned int>(f3);
}