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

    <tr>
        <th>Summary</th>
        <td>
            [RISC-V] epilogue_begin is set incorrectly
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            bug,
            backend:RISC-V,
            debuginfo,
            new issue
      </td>
    </tr>

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

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

<pre>
    For the following test,
```
int main(int argc, char **argv)
{
  int foo = 1;

  return 0;
}
```
GCC (riscv32) generates (https://godbolt.org/z/Wqze6n8Gs):
```
main:
.LFB0:
 .file 1 "/app/example.c"
        .loc 1 2 1
 .cfi_startproc
        addi    sp,sp,-48
        .cfi_def_cfa_offset 48
 sw      ra,44(sp)
        sw      s0,40(sp)
        .cfi_offset 1, -4
        .cfi_offset 8, -8
        addi    s0,sp,48
        .cfi_def_cfa 8, 0
        sw      a0,-36(s0)
        sw      a1,-40(s0)
        .loc 1 3 7
        li      a5,1
        sw      a5,-20(s0)
        .loc 1 5 10
 li      a5,0
        .loc 1 6 1
        mv      a0,a5
        lw ra,44(sp)
        .cfi_restore 1
        lw      s0,40(sp)
 .cfi_restore 8
        .cfi_def_cfa 2, 48
        addi    sp,sp,48
 .cfi_def_cfa_offset 0
        jr      ra
        .cfi_endproc
.LFE0:
 .size   main, .-main
```
and Clang riscv32 generates (https://godbolt.org/z/TMGPsr44s):
```
main:
.Lfunc_begin0:
        .file 0 "/app" "/app/example.c" md5 0x8aa04db627be45b3215ab92eac2e23c5
 .file   1 "example.c" md5 0x8aa04db627be45b3215ab92eac2e23c5
 .loc    1 2 0
        .cfi_sections .debug_frame
        .cfi_startproc
 addi    sp, sp, -32
        .cfi_def_cfa_offset 32
        sw      ra, 28(sp)
        sw      s0, 24(sp)
        .cfi_offset ra, -4
 .cfi_offset s0, -8
        addi    s0, sp, 32
        .cfi_def_cfa s0, 0
 mv      a2, a0
        li      a0, 0
        sw      a0, -12(s0)
 sw      a2, -16(s0)
        sw      a1, -20(s0)
        li      a1, 1
.Ltmp0:
        .loc    1 3 7 prologue_end
        sw      a1, -24(s0)
 .cfi_def_cfa sp, 32
        lw      ra, 28(sp) <<<<<<<<<<
        lw      s0, 24(sp)
        .cfi_restore ra
        .cfi_restore s0
        .loc    1 5 3 epilogue_begin <<<<<<<<<<
        addi    sp, sp, 32
 .cfi_def_cfa_offset 0
        ret
.Ltmp1:
.Lfunc_end0:
        .size main, .Lfunc_end0-main
```
As can be seen from the Clang's output, the _epilogue_begin_ is set after the epilogue has actually begun. This creates a problem if we single step from line 3, or set a breakpoint on line 5, the FP has been restored to the parent frame's FP, and accessing variables goes to the wrong place.

$ gdb main.gcc
```
(gdb) b main
Breakpoint 1 at 0x10444: file main.c, line 3.
(gdb) c
Continuing.

Breakpoint 1, main (argc=1, argv=0x40800004) at main.c:3
3                 int foo = 1;
(gdb) si
0x00010446      3                 int foo = 1;
(gdb) p foo
$1 = 0
(gdb) si
5                   return 0;
(gdb) p foo
$2 = 1
(gdb) s
6 }
(gdb) p foo
$3 = 1
(gdb)

```
$ gdb main.llvm
```
(gdb) b main
Breakpoint 1 at 0x10496: file main.c, line 3.
(gdb) c
Continuing.

Breakpoint 1, main (argc=1, argv=0x407ffff4) at main.c:3
3 int foo = 1;
(gdb) si
0x0001049a      3                 int foo = 1;
(gdb) p foo
$1 = 1
(gdb) si
0x0001049c      3 int foo = 1;
(gdb) p foo
$2 = 1
(gdb) s
5                   return 0;
(gdb) p foo
$3 = -1242739775
(gdb)
```
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJy8V92O4joSfhpzUwI55YSfCy5oehmtdHY1OjvavWw5SSV4JsRZ2-numadf2Ql0AqF7ZrQ6CEFkV331_1FIa1VZE21Z8sAQ_5Snf8pKnrQ7tgyRJY8z2bqjNtvhzSzV-fftQRtwR4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