[first stent to Richard only, by accident]
Am 21.11.2026 um 11:23 schrieb Thomas Koenig <[email protected]>:
Hello world,
I have looked at reassociation for a bit. Fortran allows expressions to
be reassociated, but parentheses must be honored. This we do (at least as
long as -fprotect-parens is in force). But Fortran forbids reassociation
across different expressions or statements, and we do that with
-freassociative-math.
An example:
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
is translated to
add_v2_:
.LFB5:
addsd %xmm3, %xmm2
addsd %xmm1, %xmm0
addsd %xmm2, %xmm0
ret
so it is evaluated as (a + b) + (c + d). This is faster when there are
parallel or pipelined adders, but non-conforming.
That means the Frontend stops emitting PAREN_EXPR when -fassoicative-math?
It still emits them, but only where parentheses occur in the source.
It fails to do is to emit them everywhere where they would be needed
(at least to my understanding). In the test case above, it should emit
tmp = (a + b)
tmp = (tmp + c)
res = (tmp + d)
and the middle-end would handle everything correctly.
Because the middle-end should still honor those in that case. IIRC only -Ofast
and possibly -fast-math implicitly add -fno-protect-parens
The middle end is doing the right thing here, at least for floating point.
(Well, integer calculations can also be reassociated for Fortran, that is
also something that could be changed.)
Best regards
Thomas