https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128219
Bug ID: 128219
Summary: Missed optimization: extra lea in Fortran but not in C
Product: gcc
Version: 16.2.1
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: fortran
Assignee: unassigned at gcc dot gnu.org
Reporter: henri at henrimenke dot de
Target Milestone: ---
Below I show two code examples that are functionally equivalent in Fortran and
C. They both simply compute the sum of three array elements. However, I note
that the Fortran version emits an extra lea statement, whereas the C version
packs the address arithmetic in the pointer dereference. This is particularly
surprising since both compilers come from the same copy of GCC and should
therefore use the same backend. The Fortran code nevertheless leads to larger
code size and higher instruction cache pressure for no apparent reason.
$ gfortran --version
GNU Fortran (GCC) 16.2.1 20260819 (Red Hat 16.2.1-2)
Copyright (C) 2026 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$ cat test.f90
function s(a, e)
real(8), intent(in) :: a(0:*)
integer(8), value :: e
real(8) :: s
s = a(e) + a(e + 1) + a(e + 2)
end function
$ gfortran -O3 -c test.f90
$ objdump --disassemble --disassembler-options=intel test.o
test.o: file format elf64-x86-64
Disassembly of section .text:
0000000000000000 <s_>:
0: 48 8d 04 f7 lea rax,[rdi+rsi*8]
4: f2 0f 10 00 movsd xmm0,QWORD PTR [rax]
8: f2 0f 58 40 08 addsd xmm0,QWORD PTR [rax+0x8]
d: f2 0f 58 40 10 addsd xmm0,QWORD PTR [rax+0x10]
12: c3 ret
$ cat test.c
double s(const double *a, long e) {
return a[e] + a[e + 1] + a[e + 2];
}
$ gcc -O3 -c test.c
$ objdump --disassemble --disassembler-options=intel test.o
test.o: file format elf64-x86-64
Disassembly of section .text:
0000000000000000 <s>:
0: f2 0f 10 44 f7 08 movsd xmm0,QWORD PTR [rdi+rsi*8+0x8]
6: f2 0f 58 04 f7 addsd xmm0,QWORD PTR [rdi+rsi*8]
b: f2 0f 58 44 f7 10 addsd xmm0,QWORD PTR [rdi+rsi*8+0x10]
11: c3 ret