https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108735
Bug ID: 108735
Summary: Wrong line reported on -Wmaybe-uninitialized false
positive at -O2 and missing optimizations
Product: gcc
Version: 13.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: fortran
Assignee: unassigned at gcc dot gnu.org
Reporter: rimvydas.jas at gmail dot com
Target Milestone: ---
Reduced testcase:
$ cat ilev.f90
subroutine foo(lev,p,r)
implicit none
integer :: lev,i,j
integer :: p,r,z,w
i = 1
lev = max(min(abs(lev),99),2) ! [2,99]
do j=i, lev, +1
if (j==i) then
w = 2
else
r = w
endif
w = r + 1
enddo
do j=lev, i, -1
if (j==lev) then
z = 2
else
p = z
endif
z = p + 1 ! this is comment
enddo
if (loc(p)>loc(r)) continue ! suppress unused
if (loc(z)>loc(w)) continue ! suppress unused
end subroutine
$ gfortran -Wall -Wextra -O2 -c ilev.f90
ilev.f90:21:29:
21 | z = p + 1 ! this is comment
| ^
Warning: 'z' may be used uninitialized [-Wmaybe-uninitialized]
ilev.f90:4:16:
4 | integer :: p,r,z,w
| ^
note: 'z' was declared here
$ gfortran -Wall -Wextra -O1 -c ilev.f90
ilev.f90:19:9:
19 | p = z
| ^
Warning: 'z' may be used uninitialized [-Wmaybe-uninitialized]
ilev.f90:4:16:
4 | integer :: p,r,z,w
| ^
note: 'z' was declared here
The -O2 and above points to wrong line (also end of the comment).
Comparison of -fdump-tree-optimized of first do loop shows that second loop
gets pessimized, the "if (j==lev) then" branch in first iteration is not
considered in initialization analysis while still being store optimized out.
Changing (j==lev) to (j==lev-1) does not promote diagnostic to -Wuninitialized.
Moreover both do loops could be fully optimized down to:
lev = max(min(abs(lev),99),2)
r = r + 1*lev - 1
p = p + 1*lev - 1