<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/57785>57785</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Step on dead code during debug at Og
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
ycwan9
</td>
</tr>
</table>
<pre>
When compiled with `-Og`, the debugger steps on line 23, which should not be reached. The behavior presents in both LLDB and GDB.
Source program:
```
$ cat c.c
#include <stdint.h>
uint16_t a = 5;
int32_t b;
int8_t c;
uint32_t d[10];
uint32_t e;
int32_t f;
uint32_t g;
int32_t m(uint16_t, uint32_t);
uint32_t i(void) {
uint32_t n = 0;
m(a, n);
return e;
}
int32_t m(uint16_t j, uint32_t k) {
int32_t l[4] = {0};
for (; j;) {
l[2] = b;
b = l[c];
for (; f <= 1;) {
if (d[8] == d[3])
return k;
l[2] = g;
}
}
return k;
}
int main(void) { i(); }
```
```
$ clang --version
clang version 16.0.0 (https://github.com/llvm/llvm-project.git 839ac62c5085d895d3165bc5024db623a7a78813)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /scratch/software/clang-trunk/bin
$ lldb --version
lldb version 16.0.0git (https://github.com/llvm/llvm-project.git revision 839ac62c5085d895d3165bc5024db623a7a78813)
clang revision 839ac62c5085d895d3165bc5024db623a7a78813
llvm revision 839ac62c5085d895d3165bc5024db623a7a78813
$ gdb --version
GNU gdb (GDB) 13.0.50.20220831-git
Copyright (C) 2022 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
```
```
$ clang -Og -g -o c.elf c.c
```
LLDB trace:
```
$ lldb c.elf
(lldb) target create "c.elf"
Current executable set to '/scratch/ycwang/vio-work/c.elf' (x86_64).
(lldb) b 21
Breakpoint 1: 3 locations.
(lldb) r
Process 2238627 launched: '/scratch/ycwang/vio-work/c.elf' (x86_64)
Process 2238627 stopped
* thread #1, name = 'c.elf', stop reason = breakpoint 1.3
frame #0: 0x0000555555555286 c.elf`main [inlined] m(j=<unavailable>, k=0) at c.c:21:11
18 l[2] = b;
19 b = l[c];
20 for (; f <= 1;) {
-> 21 if (d[8] == d[3])
22 return k;
23 l[2] = g;
24 }
(lldb) p d[8] == d[3]
(bool) $0 = true
(lldb) s
Process 2238627 stopped
* thread #1, name = 'c.elf', stop reason = step in
frame #0: 0x00005555555552b4 c.elf`main [inlined] m(j=<unavailable>, k=0) at c.c:19:7
16 int32_t l[4] = {0};
17 for (; j;) {
18 l[2] = b;
-> 19 b = l[c];
20 for (; f <= 1;) {
21 if (d[8] == d[3])
22 return k;
(lldb) s
Process 2238627 stopped
* thread #1, name = 'c.elf', stop reason = step in
frame #0: 0x00005555555552ba c.elf`main [inlined] m(j=<unavailable>, k=0) at c.c:23:12
20 for (; f <= 1;) {
21 if (d[8] == d[3])
22 return k;
-> 23 l[2] = g;
24 }
25 }
26 return k;
(lldb) di
c.elf`main:
0x555555555240 <+0>: cmpw $0x0, 0x2dc8(%rip) ; a
0x555555555248 <+8>: xorps %xmm0, %xmm0
0x55555555524b <+11>: movaps %xmm0, -0x18(%rsp)
0x555555555250 <+16>: je 0x5555555552be ; <+126> at c.c:28:23
0x555555555252 <+18>: movl 0x2ddc(%rip), %ecx ; b
0x555555555258 <+24>: movsbq 0x2dd8(%rip), %rdi ; c
0x555555555260 <+32>: movl 0x2dd6(%rip), %eax ; f
0x555555555266 <+38>: movl 0x2df3(%rip), %r8d ; d + 32
0x55555555526d <+45>: movl 0x2dd8(%rip), %r9d ; d + 12
0x555555555274 <+52>: movl %ecx, -0x10(%rsp)
0x555555555278 <+56>: movl -0x18(%rsp,%rdi,4), %edx
0x55555555527c <+60>: cmpl $0x2, %eax
0x55555555527f <+63>: jge 0x555555555292 ; <+82> [inlined] m + 74 at c.c
0x555555555281 <+65>: movl %edx, %esi
0x555555555283 <+67>: cmpl %r9d, %r8d
0x555555555286 <+70>: je 0x5555555552b4 ; <+116> [inlined] m + 108 at c.c:19:7
0x555555555288 <+72>: nopl (%rax,%rax)
0x555555555290 <+80>: jmp 0x555555555290 ; <+80> [inlined] m + 72 at c.c
0x555555555292 <+82>: movl %edx, %esi
0x555555555294 <+84>: nopw %cs:(%rax,%rax)
0x5555555552a0 <+96>: movl %esi, -0x10(%rsp)
0x5555555552a4 <+100>: movl -0x18(%rsp,%rdi,4), %esi
0x5555555552a8 <+104>: cmpl $0x1, %eax
0x5555555552ab <+107>: jg 0x5555555552a0 ; <+96> [inlined] m + 88 at c.c:18:10
0x5555555552ad <+109>: movl %edx, %ecx
0x5555555552af <+111>: cmpl %r9d, %r8d
0x5555555552b2 <+114>: jne 0x555555555290 ; <+80> [inlined] m + 72 at c.c
0x5555555552b4 <+116>: movl %esi, 0x2d7a(%rip) ; b
-> 0x5555555552ba <+122>: movl %ecx, -0x10(%rsp)
0x5555555552be <+126>: xorl %eax, %eax
0x5555555552c0 <+128>: retq
```
GDB trace:
```
$ gdb c.elf
GNU gdb (GDB) 13.0.50.20220831-git
Copyright (C) 2022 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-pc-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from c.elf...
(gdb) b 21
Breakpoint 1 at 0x1186: /scratch/ycwang/vio-work/c.c:21. (3 locations)
(gdb) r
Starting program: /scratch/ycwang/vio-work/c.elf
Breakpoint 1, m (k=0, j=<optimized out>) at c.c:21
21 if (d[8] == d[3])
(gdb) s
19 b = l[c];
(gdb)
23 l[2] = g;
(gdb)
main () at c.c:28
28 int main(void) { i(); }
(gdb) quit
```
The behavior seems to be cuased by optimization pass `LICMPass on for.cond2`
```
$ clang -Og -g -mllvm -opt-bisect-limit=46 -o c.elf c.c
BISECT: running pass (1) Annotation2MetadataPass on [module]
...
BISECT: running pass (46) LoopSimplifyCFGPass on for.cond2
BISECT: NOT running pass (47) LICMPass on for.cond2
...
BISECT: NOT running pass (341) X86 LEA Fixup on function (main)
$ lldb c.elf
(lldb) target create "c.elf"
Current executable set to '/scratch/ycwang/vio-work/c.elf' (x86_64).
(lldb) b 21
Breakpoint 1: where = c.elf`m + 78 at c.c:21:11, address = 0x00000000000011ae
(lldb) r
Process 2992726 launched: '/scratch/ycwang/vio-work/c.elf' (x86_64)
Process 2992726 stopped
* thread #1, name = 'c.elf', stop reason = breakpoint 1.1
frame #0: 0x00005555555551ae c.elf`m(j=5, k=0) at c.c:21:11
18 l[2] = b;
19 b = l[c];
20 for (; f <= 1;) {
-> 21 if (d[8] == d[3])
22 return k;
23 l[2] = g;
24 }
(lldb) s
Process 2992726 stopped
* thread #1, name = 'c.elf', stop reason = step in
frame #0: 0x00005555555551c6 c.elf`m(j=5, k=0) at c.c:27:1
24 }
25 }
26 return k;
-> 27 }
28 int main(void) { i(); }
(lldb) s
Process 2992726 stopped
* thread #1, name = 'c.elf', stop reason = step in
frame #0: 0x000055555555514f c.elf`i at c.c:13:10
10 uint32_t i(void) {
11 uint32_t n = 0;
12 m(a, n);
-> 13 return e;
14 }
15 int32_t m(uint16_t j, uint32_t k) {
16 int32_t l[4] = {0};
$ clang -Og -g -mllvm -opt-bisect-limit=47 -o c.elf c.c
BISECT: running pass (1) Annotation2MetadataPass on [module]
...
BISECT: running pass (47) LICMPass on for.cond2
BISECT: NOT running pass (48) LoopRotatePass on for.cond2
...
BISECT: NOT running pass (341) X86 LEA Fixup on function (main)
$ lldb c.elf
(lldb) target create "c.elf"
Current executable set to '/scratch/ycwang/vio-work/c.elf' (x86_64).
(lldb) b 21
Breakpoint 1: where = c.elf`m + 90 at c.c:21:11, address = 0x00000000000011ba
(lldb) r
Process 3075678 launched: '/scratch/ycwang/vio-work/c.elf' (x86_64)
Process 3075678 stopped
* thread #1, name = 'c.elf', stop reason = breakpoint 1.1
frame #0: 0x00005555555551ba c.elf`m(j=5, k=0) at c.c:21:11
18 l[2] = b;
19 b = l[c];
20 for (; f <= 1;) {
-> 21 if (d[8] == d[3])
22 return k;
23 l[2] = g;
24 }
(lldb) s
Process 3075678 stopped
* thread #1, name = 'c.elf', stop reason = step in
frame #0: 0x00005555555551d0 c.elf`m(j=5, k=0) at c.c:23:12
20 for (; f <= 1;) {
21 if (d[8] == d[3])
22 return k;
-> 23 l[2] = g;
24 }
25 }
26 return k;
(lldb) s
Process 3075678 stopped
* thread #1, name = 'c.elf', stop reason = step in
frame #0: 0x00005555555551d2 c.elf`m(j=5, k=0) at c.c:27:1
24 }
25 }
26 return k;
-> 27 }
28 int main(void) { i(); }
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJztW1tv2zgW_jXOC2FBou4PecilKQpk2qLNYnafBpRE22plUdUldvbXzzmkqIstxXGRYLqLGmliUeTHw-_cqEM1EsnT5Z8bnpNYbIs04wnZpfWGLDxz-WkNvxf0htQbThIeNes1L0lV86IiIidZmnNCbeyw26TxhlQb0WQJyUVNIk5KzuINTwzyAKMjvmGPqShJUfKK53VF0pxEAia6v7-9JixPyPvba2Nh3i7MK_X7q2jKmMMAsS7ZdmG3zSiS-lGX1CExq0lsxLrBTvM4axJOFvZNVSdpXhubhf1O3W7g0vL-qgmD27fEXdjX6ga02xTao2FLAA1x19DoPsnCvbbMhXt7fIsfAa6OO62POm0XNNCiIaG664KGx8NT6Pwo0gRukoXf3iXdGJLLpZndQCLhGeLmQ0ACOqqbMh8IvfBv5wUj34aike8H8-v2DNhxgBwpBdw3EbSfcwVWAKDQAnjQPAYhcjjVw6PBQEIi2YYd4iH5eGuAukLFY0dr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