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

    <tr>
        <th>Summary</th>
        <td>
            [Debuginfo][TailCallElim] Misleading stepping caused by new BranchInst in NewEntry
        </td>
    </tr>

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

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

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

<pre>
    The source code:
```C
int func(int a) {
    if (a > 10) {
        return 5 * func(a - 1);
    }
    if (a > 1) {
        return a * func(a - 1);
    } 
    return 1;
}

int main() {
    func(1);
}
```

The compilation commands:
```bash
$ clang -S -emit-llvm -Xclang -disable-O0-optnone main.c -o main.ll -g
$ opt -S -passes=mem2reg,tailcallelim main.ll -o opt.ll
$ clang opt.ll
```

The misleading stepping:
```bash
$ debug a.out 
(lldb) target create "a.out"
Current executable set to '/llvm-test/TailCallElim/a.out' (x86_64).
(lldb) b func
Breakpoint 1: where = a.out`func + 8 at main.c:3:20, address = 0x0000000000001138
(lldb) r
Process 2859495 launched: '/llvm-test/TailCallElim/a.out' (x86_64)
Process 2859495 stopped
* thread #1, name = 'a.out', stop reason = breakpoint 1.1
    frame #0: 0x0000555555555138 a.out`func(a=<unavailable>) at main.c:3:20
   1    int func(int a) {
 2        if (a > 10) {
-> 3            return 5 * func(a - 1);
   4 }
   5        if (a > 1) {
   6            return a * func(a - 1);
   7        } 
(lldb) s
Process 2859495 stopped
* thread #1, name = 'a.out', stop reason = step in
    frame #0: 0x0000555555555154 a.out`func(a=1) at main.c:2:11
   1    int func(int a) {
-> 2        if (a > 10) {
   3            return 5 * func(a - 1);
   4        }
 5        if (a > 1) {
   6            return a * func(a - 1);
   7 } 
(lldb) v
(int) a = 1
```

The debugger gets into then branch when the condition is not satisfied. This is caused by [the newly created BranchInst `BI` in `NewEntry`](https://github.com/llvm/llvm-project/blob/2fe81edef6f0b35abffbbc59b649b30ea9c15a62/llvm/lib/Transforms/Scalar/TailRecursionElimination.cpp#L513).
`BI` is inserted in the entry of the function (before the first if-statement), however, it preserves the debug location of the tail recursion call `CI` (the call `func(a-1)`), which could be located in a conditional branch.

The debug location of `BI` should be dropped.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy0VkuPozgX_TXO5ioR2EBgkUUeFaml76WvazG7kTGX4BljI9tUdf37kQ1UUl3p7uqWGmVBLr6vc66PzZ2TF424I_mB5KcVH31n7G4_GNGhdqvaNC-7xw7BmdEKBGEaJGxPkhNJ9qRIpt9x-i-1h3bUgtAyvHJCKyDbw_QRAEC2QGjJgbAHSJN3n8Nj0Y9WQw6E7pdgHNaQEloRdrOYbE_fCPy9uPwjceH6b3ZLX5e8pr223HOpCS3fpZ2zvMlwdV-wu40WgBamH6TiXhod3nuuG_ce8Zq7bjbRDITi-gLrz7DGXvq1Uk89rP-YrY10vFa4_m-yNoPXRmOseCNgbaY3pWB9uUYzg4-xBu4cOsJOPfbU4oXQo-dSCa4UKtlffU1w2Sj1dUFvrN_qt5dOIW-kvoDzOAxSX37UboP1eAG-MaOHxVoq1dSBAs_tBT0Ii9wjEErjOkLptPI4WovaA35BMfoADDj04A0QuiX0HLBbe3Se0PMjl-rIlXpQsif0PAfahlH7UhZ_Fhmh1eZdAfVEfDQfLPK_BxOmJCVsD88dWgTCTlP1pEjCWiD0ACVwPxND2J4RtqcJoUfgTWPRueiUfElunjRl5bvsdrL8zxoRvGiZV1mVg-KjFh02oYhfa_R-XOfNMGCzlLEH31nkDRDK0lC95v3UL6HbJWywBz-wyJ3R8XN9A9QmvdlDNgagLAmVT_3ny5Oy8g2OYUsTdiLsOGr-xKUK9BL2EHC5A-6SJI0K8l3toouMfEfB1sHG4Ob5qJZlb8Qsv5vra3Up7iT6obhtF4eryN2Mjvu9FIfdDVJ_lNs8u8dt-jWXlLB9mv4El5Gmj_AJ8KtsXkGejb-V0rtc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