https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128737
Thomas Koenig <tkoenig at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Component|rtl-optimization |fortran
--- Comment #3 from Thomas Koenig <tkoenig at gcc dot gnu.org> ---
OK, changing this to Fortran.
Associative math is only applied to floating point types.
Fortran also allows this for integer.
With -fassociative-math -fprotect-parens (which is default) we do
protect parantheses, but we do *not* protect expressions in general.
For
real(8) function add_v1 (a,b,c,d) result(res)
real(8), value :: a, b, c, d
res = a + b + c + d
end function add_v1
real(8) function add_v2 (a,b,c,d) result(res)
real(8), value :: a, b, c, d
real(8) :: tmp
tmp = a + b
tmp = tmp + c
res = tmp + d
end function add_v2
real(8) function add_v3 (a,b,c,d) result(res)
real(8), value :: a, b, c, d
res = ((a + b) + c) + d
end function add_v3
real(8) function add_v4 (a,b,c,d) result(res)
real(8), value :: a, b, c, d
real(8) :: tmp
tmp = (a + b)
tmp = (tmp + c)
res = (tmp + d)
end function add_v4
with current trunk and
gfortran -S -Ofast -fprotect-parens -fassociative-math reassoc.f90
we get
add_v1_:
.LFB0:
addsd %xmm3, %xmm2
addsd %xmm1, %xmm0
addsd %xmm2, %xmm0
ret
add_v2_:
.LFB5:
addsd %xmm3, %xmm2
addsd %xmm1, %xmm0
addsd %xmm2, %xmm0
ret
add_v3_:
.LFB2:
addsd %xmm1, %xmm0
addsd %xmm2, %xmm0
addsd %xmm3, %xmm0
ret
add_v4_:
.LFB3:
addsd %xmm1, %xmm0
addsd %xmm2, %xmm0
addsd %xmm3, %xmm0
ret
The transformations in add_v1 conforms to the standard, parentheses
are correctly honored in v3 and v4, and (with these options) we
violate the standard in v2.
What to do? For Fortran, we can enable reassociation with integers
in general, and we can also enable it by default. We should also
put generated parentheses around each user-generated expression.