https://gcc.gnu.org/bugzilla/show_bug.cgi?id=86638
--- Comment #1 from Tom de Vries <vries at gcc dot gnu.org> ---
F.i., take pr56154-1.c:
...
1 /* PR debug/56154 */
2 /* { dg-do run } */
3 /* { dg-options "-g" } */
4 /* { dg-additional-sources "pr56154-aux.c" } */
5
6 #include "../nop.h"
7
8 union U { int a, b; };
9 volatile int z;
10
11 __attribute__((noinline, noclone)) int
12 foo (int fd, union U x)
13 {
14 int result = x.a != 0;
15 if (fd != 0)
16 result = x.a == 0;
17 asm (NOP : : : "memory"); /* { dg-final { gdb-test pr56154-1.c:17
"x.a" "4" } } */
18 z = x.a;
19 x.a = 6;
20 asm (NOP : : : "memory"); /* { dg-final { gdb-test pr56154-1.c:20
"x.a" "6" } } */
21 return result;
22 }
23
24 void
25 test_main (void)
26 {
27 union U u = { .a = 4 };
28 foo (0, u);
29 }
...
which fails like this:
...
FAIL: gcc.dg/guality/pr56154-1.c -Og -DPREVENT_OPTIMIZATION line
pr56154-1.c:20 x.a == 6
...
Without -ftree-sra, we have:
...
$ grep DEBUG pr56154-1.c.228t.optimized | grep -v BEGIN_STMT
# DEBUG result => result_7
# DEBUG result => result_9
# DEBUG result => result_5
...
and with -ftree-sra, we have:
...
$ grep DEBUG pr56154-1.c.228t.optimized | grep -v BEGIN_STMT
# DEBUG x$a => x$a_11
# DEBUG result => result_5
# DEBUG result => result_7
# DEBUG result => result_3
# DEBUG x$a => 6
...
In general, we might be able to improve the situation by emitting var_location
at expand for non-ssa vars that we emit in registers.
But in this case it won't help us, because the store of 6 to x.a is already
removed by dce by the time we arrive at expand.
Using the fkeep-vars-live patch, we manage to prevent the dce, and are able to
print the '6' value of x one line later, at line 21, but not at line 20, due to
a "DEBUG x RESET".
AFAIU, the var-tracking manages to deduce from the artificial use inserted by
fkeep-vars-live that x is in reg si at ret, but it can't deduce that the store
of 6 into reg si is also related to x.
...
.loc 1 19 3 is_stmt 1
.loc 1 19 7 is_stmt 0
movl $6, %esi
.LVL4:
# DEBUG x RESET
.loc 1 20 3 is_stmt 1
nop
.loc 1 21 3
.LVL5:
# DEBUG x => si
.loc 1 22 1 is_stmt 0
ret
...
But, if we'd insert the var_location of x at expand (maybe after every assign
to x), we could deduce that the store of 6 is related to x, and we'd be able to
print the value of x at line 20.