Skip to content

Commit f454c9f

Browse files
author
Serguei Katkov
committed
[InlineSpiller] Re-tie operands if folding failed
InlineSpiller::foldMemoryOperand unties registers before an attempt to fold and does not restore tied-ness in case of failure. I do not have a particular test for demo of invalid behavior. This is something of clean-up. It is better to keep the behavior correct in case some time in future it happens. Reviewers: reames, dantrushin Reviewed By: dantrushin, reames Subscribers: llvm-commits Differential Revision: https://reviews.llvm.org/D94389
1 parent cd8a80d commit f454c9f

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

llvm/lib/CodeGen/InlineSpiller.cpp

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -853,16 +853,13 @@ foldMemoryOperand(ArrayRef<std::pair<MachineInstr *, unsigned>> Ops,
853853
continue;
854854
}
855855

856-
if (UntieRegs && MO.isTied())
857-
MI->untieRegOperand(Idx);
858-
859856
if (!SpillSubRegs && MO.getSubReg())
860857
return false;
861858
// We cannot fold a load instruction into a def.
862859
if (LoadMI && MO.isDef())
863860
return false;
864861
// Tied use operands should not be passed to foldMemoryOperand.
865-
if (!MI->isRegTiedToDefOperand(Idx))
862+
if (UntieRegs || !MI->isRegTiedToDefOperand(Idx))
866863
FoldOps.push_back(Idx);
867864
}
868865

@@ -873,11 +870,31 @@ foldMemoryOperand(ArrayRef<std::pair<MachineInstr *, unsigned>> Ops,
873870

874871
MachineInstrSpan MIS(MI, MI->getParent());
875872

873+
SmallVector<std::pair<unsigned, unsigned> > TiedOps;
874+
if (UntieRegs)
875+
for (unsigned Idx : FoldOps) {
876+
MachineOperand &MO = MI->getOperand(Idx);
877+
if (!MO.isTied())
878+
continue;
879+
unsigned Tied = MI->findTiedOperandIdx(Idx);
880+
if (MO.isUse())
881+
TiedOps.emplace_back(Tied, Idx);
882+
else {
883+
assert(MO.isDef() && "Tied to not use and def?");
884+
TiedOps.emplace_back(Idx, Tied);
885+
}
886+
MI->untieRegOperand(Idx);
887+
}
888+
876889
MachineInstr *FoldMI =
877890
LoadMI ? TII.foldMemoryOperand(*MI, FoldOps, *LoadMI, &LIS)
878891
: TII.foldMemoryOperand(*MI, FoldOps, StackSlot, &LIS, &VRM);
879-
if (!FoldMI)
892+
if (!FoldMI) {
893+
// Re-tie operands.
894+
for (auto Tied : TiedOps)
895+
MI->tieOperands(Tied.first, Tied.second);
880896
return false;
897+
}
881898

882899
// Remove LIS for any dead defs in the original MI not in FoldMI.
883900
for (MIBundleOperands MO(*MI); MO.isValid(); ++MO) {

0 commit comments

Comments
 (0)