[llvm] [RegisterScavenging] Respect early-clobber when scavenging registers (PR #184814)
Ulrich Weigand via llvm-commits
llvm-commits at lists.llvm.org
Fri May 8 00:28:35 PDT 2026
uweigand wrote:
Hi @dominik-steenken , I think @arsenm is right that we shouldn't need any extra flag - we already *have* such a flag in `RestoreAfter`! There are two possible use cases for `scavengeRegisterBackward`:
- We need a scratch register available for use between `To` and (before) `MBBI`, but it's not needed in `MBBI`. In these cases, the callers set `RestoreAfter` to false, to indicate that if the old value in the scratch register needs to be spilled, it can be restored before `MBBI`.
- We need a scratch register available for use between `To` and `MBBI`, to be used as input to `MBBI`. In these cases, the callers set `RestoreAfter` to true, to indicate that any restore may only happen after `MBBI`.
This is already handled in `findSurvivorBackwards` - note that any defs in `MBBI` are excluded as possible scratch registers in the `RestoreAfter` case if they need to be spilled. But if they do not need to be spilled, they can be used ... which is correct *unless* it's an earlyclobber def - that is the case that isn't handled here.
It seems to me a simple fix should be to add something like this in `findSurvivorBackwards` before the loop:
```
if (RestoreAfter)
for (const MachineOperand &MOP : std::next(From)->operands())
if (MOP.isReg() && MOP.getReg().isPhysical() && MOP.isDef() && MOP.isEarlyClobber())
Used.addReg(MOP.getReg());
```
https://github.com/llvm/llvm-project/pull/184814
More information about the llvm-commits
mailing list