[llvm] [RISCV] Use MachineCombiner to do vector floating point FMA combining (PR #177238)
Craig Topper via llvm-commits
llvm-commits at lists.llvm.org
Thu Jan 22 09:29:11 PST 2026
================
@@ -2847,6 +2994,88 @@ static void combineFPFusedMultiply(MachineInstr &Root, MachineInstr &Prev,
DelInstrs.push_back(&Root);
}
+static void
+combineVecFPFusedMultiply(MachineInstr &Root, MachineInstr &Prev,
+ unsigned Pattern,
+ SmallVectorImpl<MachineInstr *> &InsInstrs,
+ SmallVectorImpl<MachineInstr *> &DelInstrs) {
+ MachineFunction *MF = Root.getMF();
+ MachineRegisterInfo &MRI = MF->getRegInfo();
+ const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo();
+
+ unsigned Mul1Idx = 2, Mul2Idx = 3;
+ // If we're mul-add patterns and the passthru of Prev is not NoReg,
+ // we need to make sure Mul1 is the same as passthru.
+ switch (Pattern) {
+ case RISCVMachineCombinerPattern::VFMADD_AX:
+ case RISCVMachineCombinerPattern::VFMADD_XA:
+ case RISCVMachineCombinerPattern::VFMSUB:
+ case RISCVMachineCombinerPattern::VFNMSUB:
+ if (Register Passthru = Prev.getOperand(1).getReg();
+ Passthru && Passthru != Prev.getOperand(Mul1Idx).getReg()) {
+ assert(Passthru == Prev.getOperand(Mul2Idx).getReg());
+ // Swap Mul1 and Mul2.
+ Mul1Idx = 3;
+ Mul2Idx = 2;
+ }
+ break;
+ default:
+ break;
+ }
+
+ MachineOperand &Mul1 = Prev.getOperand(Mul1Idx);
+ MachineOperand &Mul2 = Prev.getOperand(Mul2Idx);
+ MachineOperand &Dst = Root.getOperand(0);
+ MachineOperand &Addend = Root.getOperand(getAddendOperandIdx(Pattern));
+
+ Register DstReg = Dst.getReg();
+ unsigned FusedOpc = getFPFusedMultiplyOpcode(Root.getOpcode(), Pattern);
+ uint32_t IntersectedFlags = Root.getFlags() & Prev.getFlags();
+ DebugLoc MergedLoc =
+ DILocation::getMergedLocation(Root.getDebugLoc(), Prev.getDebugLoc());
+
+ bool Mul1IsKill = Mul1.isKill();
+ bool Mul2IsKill = Mul2.isKill();
+ bool AddendIsKill = Addend.isKill();
+
+ // We need to clear kill flags since we may be extending the live range past
+ // a kill. If the mul had kill flags, we can preserve those since we know
+ // where the previous range stopped.
+ MRI.clearKillFlags(Mul1.getReg());
+ MRI.clearKillFlags(Mul2.getReg());
+
+ MachineInstrBuilder MIB = BuildMI(*MF, MergedLoc, TII->get(FusedOpc), DstReg)
+ .setMIFlags(IntersectedFlags);
+ switch (Pattern) {
+ case RISCVMachineCombinerPattern::VFMADD_XA:
+ case RISCVMachineCombinerPattern::VFMADD_AX:
+ case RISCVMachineCombinerPattern::VFMSUB:
+ case RISCVMachineCombinerPattern::VFNMSUB:
+ MIB.addReg(Mul1.getReg(), getKillRegState(Mul1IsKill))
+ .addReg(Mul2.getReg(), getKillRegState(Mul2IsKill))
+ .addReg(Addend.getReg(), getKillRegState(AddendIsKill));
+ break;
+ case RISCVMachineCombinerPattern::VFMACC_XA:
+ case RISCVMachineCombinerPattern::VFMACC_AX:
+ case RISCVMachineCombinerPattern::VFMSAC:
+ case RISCVMachineCombinerPattern::VFNMSAC:
+ MIB.addReg(Addend.getReg(), getKillRegState(AddendIsKill))
+ .addReg(Mul1.getReg(), getKillRegState(Mul1IsKill))
+ .addReg(Mul2.getReg(), getKillRegState(Mul2IsKill));
+ break;
+ default:
+ llvm_unreachable("unexpected RISCVMachineCombinerPattern");
+ }
+ // Append rest of the mask, VTYPE, and VL operands.
+ MIB.add(
+ ArrayRef<MachineOperand>(Root.operands_begin() + 4, Root.operands_end()));
----------------
topperc wrote:
Do we need `<MachineOperand>` or can we rely on template argument deduction?
https://github.com/llvm/llvm-project/pull/177238
More information about the llvm-commits
mailing list