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

    <tr>
        <th>Summary</th>
        <td>
            LSR makes value of constant variable not available when debugging and X86 DAG->DAG Instruction Selection then drops it entirely
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

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

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

<pre>
    
In this minimized C example, variable `l_189`, defined within the scope of function b, is not available during debugging when it is used as an argument for a call to the externally defined function `test`. This happens only when the optimization level is -O2, correct debug information is available at -Og/-O1/-O3.

With opt-bisect-limit we found out that there are two passes involved in this information loss: `Loop Strength Reduction` and `X86 DAG->DAG Instruction Selection on function (main)`. If we stop the optimization pipeline right after the former, variable `l_189` will be marked as optimized out instead of having the correct value available. If we stop the optimization pipeline right after the latter, variable `l_189` will even disappear from the list retrieved with the frame variables command in lldb.

We tested clang and lldb 14.0.0 commit `116dc70` on x64.

```
$ cat a.c
short a[10];
static int b()
{
    int c = 0, i;
    short l_189 = 3;
    for (; c <= 9; c ++)
    {
        i = 0;
        for (; i < 5; i++)
        {
            test(l_189, i);
        }
        ++l_189;
        if (a[c])
            break;
    }
}
int main()
{
    b();
}

$ cat lib.c
#include <stdio.h>
 
void test(int l_189, int i) {
    printf("%d %d", l_189, i);
}
```

LLDB trace:
```
$ clang -O2 -g a.c lib.c -o opt
$ lldb opt
(lldb) target create "opt"
Current executable set to '/tmp/opt' (x86_64).
(lldb) b 11
Breakpoint 1: where = opt`main + 16 [inlined] b at a.c:20, address = 0x00000000004004e0
(lldb) r
Process 614 launched: '/tmp/opt' (x86_64)
Process 614 stopped
* thread #1, name = 'opt', stop reason = breakpoint 1.1
    frame #0: 0x00000000004004e0 opt`main [inlined] b at a.c:11:13
   8            i = 0;
   9            for (; i < 5; i++)
   10           {
-> 11               test(l_189, i);
   12           }
   13           ++l_189;
   14           if (a[c])
(lldb) frame var
(int) c = 0
(int) i = 0
```

ASM at -O2 before Loop Strength Reduction:
```
00000000004004d0 <main>:
  4004d0:       41 56                   push   %r14
  4004d2:       53                      push   %rbx
  4004d3:       50                      push   %rax
  4004d4:       45 31 f6                xor    %r14d,%r14d
  4004d7:       bb 03 00 00 00          mov    $0x3,%ebx
  4004dc:       eb 00                   jmp    4004de <main+0xe>
  4004de:       89 df                   mov    %ebx,%edi
  4004e0:       31 f6                   xor    %esi,%esi
  4004e2:       b0 00                   mov    $0x0,%al
  4004e4:       e8 67 00 00 00          callq  400550 <test>
  4004e9:       89 df                   mov    %ebx,%edi
  4004eb:       be 01 00 00 00          mov    $0x1,%esi
  4004f0:       b0 00                   mov    $0x0,%al
  4004f2:       e8 59 00 00 00          callq  400550 <test>
  4004f7:       89 df                   mov    %ebx,%edi
  4004f9:       be 02 00 00 00          mov    $0x2,%esi
  4004fe:       b0 00                   mov    $0x0,%al
  400500:       e8 4b 00 00 00          callq  400550 <test>
  400505:       89 df                   mov    %ebx,%edi
  400507:       be 03 00 00 00          mov    $0x3,%esi
  40050c:       b0 00                   mov    $0x0,%al
  40050e:       e8 3d 00 00 00          callq  400550 <test>
  400513:       89 df                   mov    %ebx,%edi
  400515:       be 04 00 00 00          mov    $0x4,%esi
  40051a:       b0 00                   mov    $0x0,%al
  40051c:       e8 2f 00 00 00          callq  400550 <test>
  400521:       66 42 83 3c 75 40 10    cmpw   $0x0,0x601040(,%r14,2)
  400528:       60 00 00 
  40052b:       75 0d                   jne    40053a <main+0x6a>
  40052d:       83 c3 01                add    $0x1,%ebx
  400530:       49 83 c6 01             add    $0x1,%r14
  400534:       49 83 fe 0a             cmp    $0xa,%r14
  400538:       75 a4                   jne    4004de <main+0xe>
  40053a:       31 c0                   xor    %eax,%eax
  40053c:       48 83 c4 08             add    $0x8,%rsp
  400540:       5b                      pop    %rbx
  400541:       41 5e                   pop    %r14
  400543:       c3                      retq
```

DWARF info at -O2 before Loop Strength Reduction:
```
0x000000c4:       DW_TAG_variable
                    DW_AT_location      (0x00000038: 
                       [0x00000000004004d4, 0x00000000004004de): DW_OP_consts +3, DW_OP_stack_value
                       [0x00000000004004de, 0x0000000000400521): DW_OP_reg3 RBX
                       [0x0000000000400521, 0x0000000000400530): DW_OP_breg3 RBX+1, DW_OP_constu 0xffffffff, DW_OP_and, DW_OP_stack_value
                       [0x0000000000400530, 0x000000000040053a): DW_OP_reg3 RBX)
                    DW_AT_abstract_origin       (0x00000070 "l_189")
```

ASM at -O2 after Loop Strength Reduction:
```
00000000004004d0 <main>:
  4004d0:       41 56                   push   %r14
  4004d2:       53                      push   %rbx
  4004d3:       50                      push   %rax
  4004d4:       49 c7 c6 f6 ff ff ff    mov    $0xfffffffffffffff6,%r14
  4004db:       eb 00                   jmp    4004dd <main+0xd>
  4004dd:       4c 89 f3                mov    %r14,%rbx
  4004e0:       48 83 c3 0d             add    $0xd,%rbx
  4004e4:       89 df                   mov    %ebx,%edi
  4004e6:       31 f6                   xor    %esi,%esi
  4004e8:       b0 00                   mov    $0x0,%al
  4004ea:       e8 61 00 00 00          callq  400550 <test>
  4004ef:       89 df                   mov    %ebx,%edi
  4004f1:       be 01 00 00 00          mov    $0x1,%esi
  4004f6:       b0 00                   mov    $0x0,%al
  4004f8:       e8 53 00 00 00          callq  400550 <test>
  4004fd:       89 df                   mov    %ebx,%edi
  4004ff:       be 02 00 00 00          mov    $0x2,%esi
  400504:       b0 00                   mov    $0x0,%al
  400506:       e8 45 00 00 00          callq  400550 <test>
  40050b:       89 df                   mov    %ebx,%edi
  40050d:       be 03 00 00 00          mov    $0x3,%esi
  400512:       b0 00                   mov    $0x0,%al
  400514:       e8 37 00 00 00          callq  400550 <test>
  400519:       89 df                   mov    %ebx,%edi
  40051b:       be 04 00 00 00          mov    $0x4,%esi
  400520:       b0 00                   mov    $0x0,%al
  400522:       e8 29 00 00 00          callq  400550 <test>
  400527:       4c 89 f0                mov    %r14,%rax
  40052a:       48 c1 e0 01             shl    $0x1,%rax
  40052e:       66 83 3c 05 54 10 60    cmpw   $0x0,0x601054(,%rax,1)
  400535:       00 00 
  400537:       75 0a                   jne    400543 <main+0x73>
  400539:       49 83 c6 01             add    $0x1,%r14
  40053d:       49 83 fe 00             cmp    $0x0,%r14
  400541:       75 9a                   jne    4004dd <main+0xd>
  400543:       31 c0                   xor    %eax,%eax
  400545:       48 83 c4 08             add    $0x8,%rsp
  400549:       5b                      pop    %rbx
  40054a:       41 5e                   pop    %r14
  40054c:       c3                      retq 

```

DWARF info at -O2 after Loop Strength Reduction:
```
0x000000c4:       DW_TAG_variable
                    DW_AT_location      (0x0000004f: 
                       [0x00000000004004d4, 0x00000000004004dd): DW_OP_consts +3, DW_OP_stack_value)
                    DW_AT_abstract_origin       (0x00000070 "l_189")
```

We can see how after the LSR pass, the DWARF location info is associated with instructions before the loop but the information within the loop is completely lost. Before the LSR pass we had the location info associated with instructions before the loop as well as entries for following instructions within the loop, thus preserving the l_189 variable value throughout the whole debugging process.

ASM at -O2:
```
00000000004004d0 <main>:
  4004d0:       41 56                   push   %r14
  4004d2:       53                      push   %rbx
  4004d3:       50                      push   %rax
  4004d4:       49 c7 c6 f6 ff ff ff    mov    $0xfffffffffffffff6,%r14
  4004db:       0f 1f 44 00 00          nopl   0x0(%rax,%rax,1)
  4004e0:       41 8d 5e 0d             lea    0xd(%r14),%ebx
  4004e4:       89 df                   mov    %ebx,%edi
  4004e6:       31 f6                   xor    %esi,%esi
  4004e8:       31 c0                   xor    %eax,%eax
  4004ea:       e8 61 00 00 00          callq  400550 <test>
  4004ef:       89 df                   mov    %ebx,%edi
  4004f1:       be 01 00 00 00          mov    $0x1,%esi
  4004f6:       31 c0                   xor    %eax,%eax
  4004f8:       e8 53 00 00 00          callq  400550 <test>
  4004fd:       89 df                   mov    %ebx,%edi
  4004ff:       be 02 00 00 00          mov    $0x2,%esi
  400504:       31 c0                   xor    %eax,%eax
  400506:       e8 45 00 00 00          callq  400550 <test>
  40050b:       89 df                   mov    %ebx,%edi
  40050d:       be 03 00 00 00          mov    $0x3,%esi
  400512:       31 c0                   xor    %eax,%eax
  400514:       e8 37 00 00 00          callq  400550 <test>
  400519:       89 df                   mov    %ebx,%edi
  40051b:       be 04 00 00 00          mov    $0x4,%esi
  400520:       31 c0                   xor    %eax,%eax
  400522:       e8 29 00 00 00          callq  400550 <test>
  400527:       66 43 83 bc 36 54 10    cmpw   $0x0,0x601054(%r14,%r14,1)
  40052e:       60 00 00 
  400531:       75 05                   jne    400538 <main+0x68>
  400533:       49 ff c6                inc    %r14
  400536:       75 a8                   jne    4004e0 <main+0x10>
  400538:       31 c0                   xor    %eax,%eax
  40053a:       48 83 c4 08             add    $0x8,%rsp
  40053e:       5b                      pop    %rbx
  40053f:       41 5e                   pop    %r14
  400541:       c3                      retq
```

DWARF information of the binary file generated at -O2 does not contain the DIE associated with variable l_189 in the inlined subroutine tree.




</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJztW1tv27gS_jXOCxFD1M3yQx7iZLso0EUXbYHuW0BJlM1WlrQi7aTn158ZUpZIWbk57mJRrOD4IpLD4TfkzHwUk9b5j6uZdzvzrt9XRG2EJFtRia34H8_JDeEPbNuUfObfkD1rBUtLTmaxV97RZAmfeD_nhaig8r2A1iiCE5nVDSd1QYpdlSlRVyTFmiC7qhVheyZKLSnftaJag4R0t17jt_sNr4hQWHMnQSaThFWEtevdlleKFHVLGMlYWRJV6574g-JtBTd-9Hr0fYJ-iksFH3PyBQe2YU3DK0nqCqrrrlBE3SgcLtNtSr7nJXZ_-dFHlbO6bXmmjIpEVKDB1tSEOsNAmIIG65n_7vIj1e_B3GBq3r8CNNjPZSokSLssoUNF7jkMaFflpN4p0IThG29BGPyp-5o0TEouodN9Xe5hYKKzj61FWUs5C65xrB_quiGfVcurNfT2iec7DQOUAIY51vgricnt9e-Xs-A3-CDvK6laU4l85iU33-A1IOgnWyaqmb_UIL4vUGepoJ8j4BrR8B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