https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101919
--- Comment #6 from Rimvydas (RJ) <rimvydas.jas at gmail dot com> ---
Additional reduced testcase.
$ cat bar.F90
subroutine bar()
implicit none
character(len=80) :: base
#ifdef V1
character(len=80),parameter :: f='longname_patterns.xml'
integer,parameter :: k = len_trim(f)
#else
# ifdef V2
character(len=80),parameter :: f='longname_patterns.xml'
# else
character(len=80) :: f='longname_patterns.xml'
# endif
integer :: k
k = len_trim(f)
#endif
base = f(10:k-4)
print *, base, '@'
end subroutine bar
$ gfortran -DV1 -flto -O0 -Wall -Wextra -c bar.F90
$ gcc -shared bar.o -o libbar.so -fdump-tree-optimized
$ gfortran -DV2 -flto -O1 -Wall -Wextra -c bar.F90
$ gcc -shared bar.o -o libbar.so -fdump-tree-optimized
$ gfortran -DV2 -flto -O0 -Wall -Wextra -c bar.F90
$ gcc -shared bar.o -o libbar.so -fdump-tree-optimized
bar.F90: In function ‘bar’:
bar.F90:16:18: warning: ‘__builtin_memmove’ reading 80 bytes from a region of
size 71 [-Wstringop-overread]
16 | base = f(10:k-4)
| ^
$ gfortran -flto -O3 -Wall -Wextra -c bar.F90
$ gcc -shared bar.o -o libbar.so -fdump-tree-optimized
bar.F90: In function ‘bar’:
bar.F90:16:18: warning: ‘__builtin_memcpy’ reading 80 bytes from a region of
size 71 [-Wstringop-overread]
16 | base = f(10:k-4)
| ^
bar.F90:11:24: note: at offset 9 into source object ‘f’ of size 80
11 | character(len=80) :: f='longname_patterns.xml'
|
V1 -O0 has:
__builtin_memcpy (&base, &"longname_patterns.xml
"[10]{lb: 1 sz: 1}, 8);
_1 = &base + 8;
__builtin_memset (_1, 32, 72);
V2 -O0 has:
unsigned char base[1:80];
k_10 = 21;
_1 = k_10 + -4;
_11 = (long int) _1;
_2 = _11 + -9;
_12 = MAX_EXPR <_2, 0>; < --- discarded ?
_3 = _11 + -9;
_13 = MAX_EXPR <_3, 0>;
_14 = &base;
_15 = &"longname_patterns.xml
"[10]{lb: 1 sz: 1};
if (_13 <= 79) < --- will always be false
goto <bb 3>; [INV]
else
goto <bb 4>; [INV]
<bb 3> :
_4 = (long unsigned int) _13;
__builtin_memmove (_14, _15, _4);
_5 = 80 - _13;
_6 = (long unsigned int) _5;
_7 = (sizetype) _13;
_8 = _14 + _7;
__builtin_memset (_8, 32, _6);
goto <bb 5>; [INV]
<bb 4> :
__builtin_memmove (_14, _15, 80);
V3 -O3 has:
static unsigned char f[1:80] = <<< error >>>;
_1 = _gfortran_string_len_trim (80, &f);
k_10 = (int) _1;
_2 = k_10 + -4;
if (_2 <= 88) < --- no lower bound and redundant
goto <bb 3>; [50.00%]
else
goto <bb 4>; [50.00%]
<bb 3> [local count: 536870913]:
_11 = (long int) _2;
_3 = _11 + -9;
_12 = MAX_EXPR <_3, 0>;
_4 = (long unsigned int) _12;
__builtin_memcpy (&base, &f[10]{lb: 1 sz: 1}, _4);
_5 = 80 - _12;
_6 = (long unsigned int) _5;
_7 = &base + _4;
__builtin_memset (_7, 32, _6);
goto <bb 5>; [100.00%]
<bb 4> [local count: 536870913]:
__builtin_memcpy (&base, &f[10]{lb: 1 sz: 1}, 80)