[llvm] [MachineLateInstrsCleanup] Reuse redundant spill-slot reloads (PR #220180)
Anup Kumar via llvm-commits
llvm-commits at lists.llvm.org
Sun Oct 11 13:42:54 PDT 2026
anupkum-sgs wrote:
Thanks for the reviews.
**1. Fix to the reload check: exclude fixed objects**
Fixed objects, including fixed spill slots from `CreateFixedSpillStackObject()`, get their offset from the target or ABI when they're created. `MachineFrameInfo` doesn't check whether that offset overlaps another fixed object, so distinct fixed frame indices can name the same bytes. The current patch only checks `isSpillSlotObjectIndex(FI)`. In principle it could reuse a reload from a fixed spill slot across a store to an overlapping fixed object. I haven't found such a case, but for correctness I'll restrict candidates to non-fixed spill slots:
```cpp
return !MFI.isFixedObjectIndex(FrameIndex) &&
MFI.isSpillSlotObjectIndex(FrameIndex);
```
Non-fixed spill slots can't overlap. PEI gives each non-fixed object its own byte range: `AdjustStackOffset()` allocates past all fixed objects, and `scavengeStackSlot()` only uses bytes that are still free. When StackSlotColoring merges slots, it rewrites both the frame-index operands and the memory operands to the surviving index. AArch64 SVE and RISC-V RVV objects are laid out by the target with the same kind of sequential allocation.
**2. Reusing `MachineInstr::mayAlias()` (@nickdesaulniers, @arsenm)**
Treating any two distinct fixed-stack PSVs as no-alias is unsafe, for the reason above and also because of tail calls. The caller's incoming-argument area is reused for the outgoing arguments of a tail call. The incoming arguments are fixed objects, and the outgoing ones are new fixed objects at the same offsets. These aren't spill slots, but they show that distinct fixed frame indices can overlap. A safe version requires at least one side to be a non-fixed spill slot:
```cpp
// In MemOperandsHaveAlias(), inside `if (!SameVal)`.
const auto *FSa = dyn_cast_if_present<FixedStackPseudoSourceValue>(PSVa);
const auto *FSb = dyn_cast_if_present<FixedStackPseudoSourceValue>(PSVb);
auto IsNonFixedSpillSlot = [&](const FixedStackPseudoSourceValue *FS) {
int FI = FS->getFrameIndex();
return !MFI.isFixedObjectIndex(FI) && MFI.isSpillSlotObjectIndex(FI);
};
if (FSa && FSb && FSa != FSb &&
(IsNonFixedSpillSlot(FSa) || IsNonFixedSpillSlot(FSb)))
return false;
```
SelectionDAG relies on the same property: `BaseIndexOffset::computeAliasing()` treats different frame indices as no-alias when at least one is non-fixed, and compares offsets when both are fixed.
I tried calling the improved `mayAlias()` in this pass, and it works: redundant spill-slot reloads with an in-between store to a different spill slot got removed. The regressions were that it kept the reload across a call.
Forward path I am thinking of:
- **(a)** Propose the `MemOperandsHaveAlias()` change in a separate PR first, and switch this patch to use `mayAlias()` once it lands (we can check `mayStore()` along with `mayAlias()` to keep removing the redundant reload across an ordinary call), then
- **(b)** use `isLoadFromStackSlotPostFE()` plus the `isFixedObjectIndex()` and `isSpillSlotObjectIndex()` checks in `isSpillSlotReload()`, and then get this PR merged.
- **(c)** After (b), give `isLoadFromStackSlotPostFE()` a target-independent meaningful implementation in follow-up PR.
@arsenm @nickdesaulniers @JonPsson1, would this be better? Please suggest a better way than this.
--Assisted by Claude Opus 5.5
https://github.com/llvm/llvm-project/pull/220180
More information about the llvm-commits
mailing list