diff options
| author | Nick Desaulniers <nickdesaulniers@users.noreply.github.com> | 2023-11-17 10:03:14 -0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-11-17 10:03:14 -0800 |
| commit | 99ee2db198d86f685bcb07a1495a7115ffc31d7e (patch) | |
| tree | 3d6cc4eacbc47476e9221a34a312faaec16bd909 /llvm/lib/CodeGen/TargetInstrInfo.cpp | |
| parent | f99a02005970cdcb0aad0de80fa4e5b546c6546b (diff) | |
[TargetInstrInfo] enable foldMemoryOperand for InlineAsm (#70743)
foldMemoryOperand looks at pairs of instructions (generally a load to
virt reg then use of the virtreg, or def of a virtreg then a store) and
attempts to combine them. This can reduce register pressure.
A prior commit added the ability to mark such a MachineOperand as
foldable. In terms of INLINEASM, this means that "rm" was used (rather
than just "r") to denote that the INLINEASM may use a memory operand
rather than a register operand. This effectively undoes decisions made
by the instruction selection framework. Callers will be added in the
register allocation frameworks. This has been tested with all of the
above (which will come as follow up patches).
Thanks to @topperc who suggested this at last years LLVM US Dev Meeting
and @qcolombet who confirmed this was the right approach.
Link: https://github.com/llvm/llvm-project/issues/20571
Diffstat (limited to 'llvm/lib/CodeGen/TargetInstrInfo.cpp')
| -rw-r--r-- | llvm/lib/CodeGen/TargetInstrInfo.cpp | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/llvm/lib/CodeGen/TargetInstrInfo.cpp b/llvm/lib/CodeGen/TargetInstrInfo.cpp index 3013a768bc4d..5ede36505b5b 100644 --- a/llvm/lib/CodeGen/TargetInstrInfo.cpp +++ b/llvm/lib/CodeGen/TargetInstrInfo.cpp @@ -565,6 +565,64 @@ static MachineInstr *foldPatchpoint(MachineFunction &MF, MachineInstr &MI, return NewMI; } +static void foldInlineAsmMemOperand(MachineInstr *MI, unsigned OpNo, int FI, + const TargetInstrInfo &TII) { + MachineOperand &MO = MI->getOperand(OpNo); + const VirtRegInfo &RI = AnalyzeVirtRegInBundle(*MI, MO.getReg()); + + // If the machine operand is tied, untie it first. + if (MO.isTied()) { + unsigned TiedTo = MI->findTiedOperandIdx(OpNo); + MI->untieRegOperand(OpNo); + // Intentional recursion! + foldInlineAsmMemOperand(MI, TiedTo, FI, TII); + } + + // Change the operand from a register to a frame index. + MO.ChangeToFrameIndex(FI, MO.getTargetFlags()); + + SmallVector<MachineOperand, 4> NewOps; + TII.getFrameIndexOperands(NewOps); + assert(!NewOps.empty() && "getFrameIndexOperands didn't create any operands"); + MI->insert(MI->operands_begin() + OpNo + 1, NewOps); + + // Change the previous operand to a MemKind InlineAsm::Flag. The second param + // is the per-target number of operands that represent the memory operand + // excluding this one (MD). This includes MO. + InlineAsm::Flag F(InlineAsm::Kind::Mem, NewOps.size() + 1); + F.setMemConstraint(InlineAsm::ConstraintCode::m); + MachineOperand &MD = MI->getOperand(OpNo - 1); + MD.setImm(F); + + // Update mayload/maystore metadata. + MachineOperand &ExtraMO = MI->getOperand(InlineAsm::MIOp_ExtraInfo); + if (RI.Reads) + ExtraMO.setImm(ExtraMO.getImm() | InlineAsm::Extra_MayLoad); + if (RI.Writes) + ExtraMO.setImm(ExtraMO.getImm() | InlineAsm::Extra_MayStore); +} + +// Returns nullptr if not possible to fold. +static MachineInstr *foldInlineAsmMemOperand(MachineInstr &MI, + ArrayRef<unsigned> Ops, int FI, + const TargetInstrInfo &TII) { + assert(MI.isInlineAsm() && "wrong opcode"); + if (Ops.size() > 1) + return nullptr; + unsigned Op = Ops[0]; + assert(Op && "should never be first operand"); + assert(MI.getOperand(Op).isReg() && "shouldn't be folding non-reg operands"); + + if (!MI.mayFoldInlineAsmRegOp(Op)) + return nullptr; + + MachineInstr &NewMI = TII.duplicate(*MI.getParent(), MI.getIterator(), MI); + + foldInlineAsmMemOperand(&NewMI, Op, FI, TII); + + return &NewMI; +} + MachineInstr *TargetInstrInfo::foldMemoryOperand(MachineInstr &MI, ArrayRef<unsigned> Ops, int FI, LiveIntervals *LIS, @@ -612,6 +670,8 @@ MachineInstr *TargetInstrInfo::foldMemoryOperand(MachineInstr &MI, NewMI = foldPatchpoint(MF, MI, Ops, FI, *this); if (NewMI) MBB->insert(MI, NewMI); + } else if (MI.isInlineAsm()) { + NewMI = foldInlineAsmMemOperand(MI, Ops, FI, *this); } else { // Ask the target to do the actual folding. NewMI = foldMemoryOperandImpl(MF, MI, Ops, MI, FI, LIS, VRM); @@ -683,6 +743,8 @@ MachineInstr *TargetInstrInfo::foldMemoryOperand(MachineInstr &MI, NewMI = foldPatchpoint(MF, MI, Ops, FrameIndex, *this); if (NewMI) NewMI = &*MBB.insert(MI, NewMI); + } else if (MI.isInlineAsm() && isLoadFromStackSlot(LoadMI, FrameIndex)) { + NewMI = foldInlineAsmMemOperand(MI, Ops, FrameIndex, *this); } else { // Ask the target to do the actual folding. NewMI = foldMemoryOperandImpl(MF, MI, Ops, MI, LoadMI, LIS); |
