https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105239
Bug ID: 105239 Summary: Presence of unrelated function call makes a variable optimized out only at -Og Product: gcc Version: 12.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: debug Assignee: unassigned at gcc dot gnu.org Reporter: dc.delia at protonmail dot com Target Milestone: --- In this code, the value of variable l_31 from function e is available when debugging on line 14, which re-assigns it, and optimized out at line 27, which uses it as a call argument for test() from an external module. We found this at -Og. Looking at DWARF info, the update of l_31 seems not tracked with l_31’s DIE location defined only for instructions that precede the reassignment. At -O1/-O2/-O3 the reassignment appears to be tracked correctly as the updated value is available when debugging. Interestingly, by removing the call to external function nop() located right before the call to test(..., l_31, ...), the issue disappears and the DWARF info becomes identical to the one from -O1 for the original program. We found this issue on x64 gcc commit 500d3f0a302 using gdb 11.2; as for gcc releases, the issue is found in every tested version (6.4, 7.5, 8.4, 9.3, 10.3, 11.1). We were unable to pinpoint a specific optimization behind it. $ cat a.c short a; int c; short b() { return a; } void e(char f) { short g; int l_57 = 0 ; for (; c <= 0; c++) { int l_30 = 4073709551615, l_31 = 2, l_80 = 2; char l_81 = l_31 = f; g = b(); nop(); test(l_57, l_30, l_31, l_80, l_81); } } int main () { e(8); } $ cat lib.c #include <stdio.h> void nop() { printf("\n"); } void test(int l_51, int l_57, int l_30, int l_31, int l_80, int l_81) { printf("%d %d %d %d %d %d", l_51, l_57, l_30, l_31, l_80, l_81); } GDB trace: $ gcc -Og -g a.c lib.c -o opt $ gdb -q opt Reading symbols from opt... (gdb) b 17 Breakpoint 1 at 0x400566: file a.c, line 17. (gdb) r Starting program: /tmp/opt Breakpoint 1, e (f=f@entry=8 '\b') at a.c:17 17 test(l_57, l_30, l_31, l_80, l_81); (gdb) info loc l_30 = 2080555007 l_31 = <optimized out> l_80 = 2 l_81 = 8 '\b' g = <optimized out> l_57 = 0 Code at -Og: 000000000040054e <e>: 40054e: 55 push %rbp 40054f: 53 push %rbx 400550: 48 83 ec 08 sub $0x8,%rsp 400554: 89 fd mov %edi,%ebp 400556: eb 33 jmp 40058b <e+0x3d> 400558: 40 0f be dd movsbl %bpl,%ebx 40055c: b8 00 00 00 00 mov $0x0,%eax 400561: e8 4d 00 00 00 callq 4005b3 <nop> 400566: 89 da mov %ebx,%edx 400568: 41 89 d8 mov %ebx,%r8d 40056b: b9 02 00 00 00 mov $0x2,%ecx 400570: be ff bf 02 7c mov $0x7c02bfff,%esi 400575: bf 00 00 00 00 mov $0x0,%edi 40057a: b8 00 00 00 00 mov $0x0,%eax 40057f: e8 42 00 00 00 callq 4005c6 <test> 400584: 83 05 b1 0a 20 00 01 addl $0x1,0x200ab1(%rip) # 60103c <c> 40058b: 83 3d aa 0a 20 00 00 cmpl $0x0,0x200aaa(%rip) # 60103c <c> 400592: 7e c4 jle 400558 <e+0xa> 400594: 48 83 c4 08 add $0x8,%rsp 400598: 5b pop %rbx 400599: 5d pop %rbp 40059a: c3 retq DWARF at -Og: 0x0000011f: DW_TAG_variable DW_AT_name ("l_31") DW_AT_decl_line (13) DW_AT_decl_column (0x24) DW_AT_type (0x0000005b "int") DW_AT_location (0x00000034: [0x0000000000400558, 0x000000000040055c): DW_OP_lit2, DW_OP_stack_value) DW_AT_GNU_locviews (0x00000032) Code at -O1: 000000000040054e <e>: 40054e: 83 3d e7 0a 20 00 00 cmpl $0x0,0x200ae7(%rip) # 60103c <c> 400555: 7f 42 jg 400599 <e+0x4b> 400557: 53 push %rbx 400558: 40 0f be df movsbl %dil,%ebx 40055c: b8 00 00 00 00 mov $0x0,%eax 400561: e8 4c 00 00 00 callq 4005b2 <nop> 400566: 41 89 d8 mov %ebx,%r8d 400569: b9 02 00 00 00 mov $0x2,%ecx 40056e: 89 da mov %ebx,%edx 400570: be ff bf 02 7c mov $0x7c02bfff,%esi 400575: bf 00 00 00 00 mov $0x0,%edi 40057a: b8 00 00 00 00 mov $0x0,%eax 40057f: e8 41 00 00 00 callq 4005c5 <test> 400584: 8b 05 b2 0a 20 00 mov 0x200ab2(%rip),%eax # 60103c <c> 40058a: 83 c0 01 add $0x1,%eax 40058d: 89 05 a9 0a 20 00 mov %eax,0x200aa9(%rip) # 60103c <c> 400593: 85 c0 test %eax,%eax 400595: 7e c5 jle 40055c <e+0xe> 400597: 5b pop %rbx 400598: c3 retq DWARF at -O1: 0x0000011e: DW_TAG_variable DW_AT_name ("l_31") DW_AT_decl_column (0x24) DW_AT_type (0x0000005b "int") DW_AT_location (0x0000003f: [0x000000000040055c, 0x000000000040055c): DW_OP_lit2, DW_OP_stack_value [0x000000000040055c, 0x0000000000400598): DW_OP_reg3 RBX [0x0000000000400598, 0x0000000000400599): DW_OP_entry_value(DW_OP_reg5 RDI), DW_OP_const1u 0x38, DW_OP_shl, DW_OP_const1u 0x38, DW_OP_shra, DW_OP_stack_value) DW_AT_GNU_locviews (0x00000039)