| Issue |
235692
|
| Summary |
[RISCV] -msave-restore: shrink-wrapping can place __riscv_save_N in a block where t1 is live
|
| Labels |
new issue
|
| Assignees |
|
| Reporter |
kdupontbe
|
RISCVFrameLowering::canUseAsPrologue only rejects a prologue block when t0 (x5) is live-in, because call t0, __riscv_save_N writes the return address to t0. The save libcalls also write t1 (x6). In compiler-rt lib/builtins/riscv/save.S, the RV64 versions of __riscv_save_2…__riscv_save_12 (and the RV32 versions from __riscv_save_4 up) compute the stack adjustment in t1:
__riscv_save_5: # RV64, alias of __riscv_save_4
addi sp, sp, -112
li t1, 64
...
add sp, sp, t1
jr t0
Shrink-wrapping can therefore move the prologue into a block where t1 holds a live value, and the save call overwrites it.
Reproducer (llc -mtriple=riscv64 -mattr=+save-restore), https://godbolt.org/z/9PWKjzvfe:
define void @f(i1 %c) nounwind {
entry:
%x = call i64 asm sideeffect "li $0, 42", "={x6}"()
br i1 %c, label %slow, label %fast
slow:
call void asm sideeffect "# clobber s0-s4", "~{x8},~{x9},~{x18},~{x19},~{x20}"()
call void asm sideeffect "# use $0", "{x6}"(i64 %x)
br label %fast
fast:
ret void
}
Output from llc trunk:
f:
andi a0, a0, 1
li t1, 42
beqz a0, .LBB0_2
# %bb.1: # %slow
call t0, __riscv_save_5 # clobbers t1
# clobber s0-s4
# use t1 # reads the clobbered value
tail __riscv_restore_5
.LBB0_2:
ret
The inline asm only forces the situation so the reproducer stays small. Ordinary register allocation can also leave a value in t1 across the shrink-wrapped prologue point.
Expected: t1 live-in to %bb.1 should stop shrink-wrapping from choosing it, as t0 already does. The save then stays in the entry block.
Possible fix. We carry this downstream; it is a one-line change to the same check:
// Inserting a call to a __riscv_save libcall requires the use of the register
// t0 (X5) to hold the return address. Therefore if this register is already
// used we can't insert the call.
+ // The save libcalls also use t1 (X6) internally to adjust sp.
RegScavenger RS;
RS.enterBasicBlock(*TmpMBB);
- return !RS.isRegUsed(RISCV::X5);
+ return !RS.isRegUsed(RISCV::X5) && !RS.isRegUsed(RISCV::X6);
With this change the save is emitted in the entry block and t1 is preserved.
Assisted by AI when creating the issue text
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs