<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/57782>57782</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            Wrong argument value during debug at Og
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          ycwan9
      </td>
    </tr>
</table>

<pre>
    When calling function `j` at line 20, the value of argument `r` should be 0, but 1 is shown in both LLDB and GDB.

Source program:
```
$ cat b.c
#include <stdint.h>
struct a {
  uint8_t b;
  int32_t c;
  int32_t d;
} e, g[10][8][2];
struct {
  uint32_t f;
} i;
int32_t w(int, signed char);
uint16_t j(unsigned short, unsigned, unsigned char, unsigned char);
uint32_t k(void) {
  uint8_t l = 5;
  struct a m = {1};
  w(l, 1);
  return m.b;
}
int32_t w(int n, signed char o) {
  int8_t p = 0;
  j(n, p < e.d, p, o);
  return i.f;
}
uint16_t j(unsigned short q, unsigned r, unsigned char s, unsigned char v) {
  for (;;) {
    uint32_t t;
    {
      uint32_t u;
      if (g[9][6][0].b)
        e.c = q;
      else
        return e.b;
      if (r)
        continue;
    }
    g[4][0][0] = g[1][1][1];
  }
}
int main(void) { k(); }
```

```
$ clang --version
clang version 16.0.0 (https://github.com/llvm/llvm-project.git c63e05dc0776b4a0a75685ba8a8d7667e0dbaee3)
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 c63e05dc0776b4a0a75685ba8a8d7667e0dbaee3)
  clang revision c63e05dc0776b4a0a75685ba8a8d7667e0dbaee3
  llvm revision c63e05dc0776b4a0a75685ba8a8d7667e0dbaee3

$ gdb -v
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 b.elf b.c
```

LLDB trace:
```
$ lldb b.elf
(lldb) target create "b.elf"
Current executable set to '/scratch/ycwang/vio-work/b.elf' (x86_64).
(lldb) b w
Breakpoint 1: 3 locations.
(lldb) r
Process 3980167 launched: '/scratch/ycwang/vio-work/b.elf' (x86_64)
Process 3980167 stopped
* thread #1, name = 'b.elf', stop reason = breakpoint 1.3
    frame #0: 0x0000555555555280 b.elf`main [inlined] w(n=5, o='\x01') at b.c:20:10
   17   }
   18   int32_t w(int n, signed char o) {
   19     int8_t p = 0;
-> 20          j(n, p < e.d, p, o);
   21     return i.f;
   22   }
   23   uint16_t j(unsigned short q, unsigned r, unsigned char s, unsigned char v) {
(lldb) p p < e.d
(bool) $0 = false
(lldb) s
Process 3980167 stopped
* thread #1, name = 'b.elf', stop reason = step in
    frame #0: 0x00005555555552a7 b.elf`main [inlined] j(q=5, r=1, s=<unavailable>, v=<unavailable>) at b.c:28:11
   25       uint32_t t;
   26       {
   27         uint32_t u;
-> 28              if (g[9][6][0].b)
   29           e.c = q;
   30         else
   31           return e.b;
(lldb) frame var
(unsigned short) q = 5
(unsigned int) r = 1
(unsigned char) s = <no location, value may have been optimized out>

(unsigned char) v = <no location, value may have been optimized out>
```


GDB trace:
```
$ gdb b.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 b.elf...
(gdb) b w
Breakpoint 1 at 0x1130: w. (3 locations)
(gdb) r
Starting program: /scratch/ycwang/vio-work/b.elf 

Breakpoint 1, w (n=5, o=1 '\001') at b.c:20
20        j(n, p < e.d, p, o);
(gdb) p p< e.d
$1 = 0
(gdb) s
j (q=5, r=1, s=<optimized out>, v=<optimized out>) at b.c:28
28            if (g[9][6][0].b)
(gdb) info arg
q = 5
r = 1
s = <optimized out>
v = <optimized out>
(gdb) quit
```


The behavior seems to be first present in optimization pass `InstCombinePass on w`
```
$ clang -Og -g -mllvm -opt-bisect-limit=80 -o b.elf b.c
BISECT: running pass (1) Annotation2MetadataPass on [module]
...
BISECT: running pass (80) SimplifyCFGPass on w
BISECT: NOT running pass (81) InstCombinePass on w
...
BISECT: NOT running pass (439) X86 LEA Fixup on function (main)

$ lldb b.elf
(lldb) target create "b.elf"
Current executable set to '/scratch/ycwang/vio-work/b.elf' (x86_64).
(lldb) b j
Breakpoint 1: 2 locations.
(lldb) r
Process 2238227 launched: '/scratch/ycwang/vio-work/b.elf' (x86_64)
Process 2238227 stopped
* thread #1, name = 'b.elf', stop reason = breakpoint 1.1
    frame #0: 0x000055555555518a b.elf`w [inlined] j(q=<unavailable>, r=<unavailable>, s=<unavailable>, v=<unavailable>) at b.c:28:11
   25       uint32_t t;
   26       {
   27         uint32_t u;
-> 28              if (g[9][6][0].b)
   29           e.c = q;
   30         else
   31           return e.b;
(lldb) frame var
(unsigned short) q = <no location, value may have been optimized out>

(unsigned int) r = <no location, value may have been optimized out>

(unsigned char) s = <no location, value may have been optimized out>

(unsigned char) v = <no location, value may have been optimized out>


$ clang -Og -g -mllvm -opt-bisect-limit=81 -o b.elf b.c
BISECT: running pass (1) Annotation2MetadataPass on [module]
...
BISECT: running pass (81) InstCombinePass on w
BISECT: NOT running pass (82) LibCallsShrinkWrapPass on w
...
BISECT: NOT running pass (439) X86 LEA Fixup on function (main)

$ lldb b.elf
(lldb) target create "b.elf"
Current executable set to '/scratch/ycwang/vio-work/b.elf' (x86_64).
(lldb) b j
Breakpoint 1: 2 locations.
(lldb) r
Process 2285222 launched: '/scratch/ycwang/vio-work/b.elf' (x86_64)
Process 2285222 stopped
* thread #1, name = 'b.elf', stop reason = breakpoint 1.1
    frame #0: 0x000055555555517a b.elf`w [inlined] j(q=<unavailable>, r=1, s=<unavailable>, v=<unavailable>) at b.c:28:11
   25       uint32_t t;
   26       {
   27         uint32_t u;
-> 28              if (g[9][6][0].b)
   29           e.c = q;
   30         else
   31           return e.b;
(lldb) frame var
(unsigned short) q = <no location, value may have been optimized out>

(unsigned int) r = 1
(unsigned char) s = <no location, value may have been optimized out>

(unsigned char) v = <no location, value may have been optimized out>
```

</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJztWl9z2zgO_zTOC8caibJl-cEPid10OpNrO01ueve0Q0m0zVQiFYqy4_v0B5CyLNnOv-5mb2-nHSeyQBAEARD4gWmist3s-5pLkrI8F3JFlrVMjVCSDCL_Hn4IMwQGOKH-gM6JWXOyYXnNiVoSpld1waVBXo281VrVeUYSTixzUhsSEFEhfSuJkCRRZk1ubhZXhMmMfFxceQN_MfAv3e9bVeuUk1KrlWbFIGzIILn5uFc6Am0NSbx0TwiFTPM642QQziuTCWm89SD84IYro-vUEEYGkytHIaQGlvg3kDEIWxqQQgq09Awta2mDyYJw3NxqML4K_MF4Ac_YPSg-9ozNskeLWmnLnjTRvu3HtwMaw3dcpRIryTOSrpke0GnLiaKCCFjvgbWWDReYWdtZe0r3eyPjhNAXatf_AUI3SsD06Tmj5WDmBRl3zNSauLBDMCeAnXUYcEc5Lh50FyREc1NrSQov6ZrkrDWIPLIHUUf6NeqVVge_swoayc7GoTnhnjVMib_UWX2EtzzV5xmbk4eeYU_NTKpT0uZI_aXSBCTjwvDpj3WCx3TUJX2eDlfd4wLbLFE2xuzUxWrkHhjAYHwwQoeZgIlSa8WHIzE8r3ifs7EY95KzK-oT0amSRsiaH21jcXhBLUcH9ZqH1cceOkfrPg6iWkHdMCIFE7If0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