[llvm] [X86] Fold MOVAPS+SHUFPS into PSHUFD to eliminate redundant copies when … (PR #212626)
Simon Pilgrim via llvm-commits
llvm-commits at lists.llvm.org
Wed Aug 19 05:33:49 PDT 2026
================
@@ -138,6 +138,61 @@ bool X86FixupInstTuningImpl::processInstruction(
return ReplaceInTie;
};
+ // `movaps r1, r2` + `shufps r1, r2, i` -> `pshufd r1, r2, i`
+ // SHUFPS is destructive (2-address), so instruction selection may introduce
+ // a preceding copy to preserve the original source value. PSHUFD is
+ // non-destructive, making the copy unnecessary.
+ // We skip NewOpcPreferable here because we are explicitly replacing two
+ // instructions with one, which is universally profitable.
+ auto ProcessSHUFPS = [&]() -> bool {
+ if (!ST->hasSSE2() || !ST->hasNoDomainDelayShuffle())
+ return false;
+
+ if (I == MBB.begin())
+ return false;
+
+ MachineInstr &PrevMI = *std::prev(I);
+ unsigned PrevOpc = PrevMI.getOpcode();
+ if (PrevOpc != X86::MOVAPSrr && PrevOpc != X86::MOVAPDrr &&
+ PrevOpc != X86::MOVUPSrr && PrevOpc != X86::MOVUPDrr)
+ return false;
+
+ const MachineOperand &DstOp = MI.getOperand(0);
+ const MachineOperand &Src1Op = MI.getOperand(1);
+ const MachineOperand &Src2Op = MI.getOperand(2);
+
+ const MachineOperand &PrevDstOp = PrevMI.getOperand(0);
+ const MachineOperand &PrevSrcOp = PrevMI.getOperand(1);
+
+ const Register DstReg = DstOp.getReg();
+ const Register Src1Reg = Src1Op.getReg();
+ const Register Src2Reg = Src2Op.getReg();
+
+ if (DstReg != Src1Reg)
+ return false;
+
+ // Check that MOVAPS matches: `DstReg = MOVAPS Src2Reg`
+ if (PrevDstOp.getReg() != DstReg || PrevSrcOp.getReg() != Src2Reg)
+ return false;
+
+ LLVM_DEBUG(dbgs() << "Replacing: " << PrevMI << " and " << MI);
+ unsigned MaskImm = MI.getOperand(NumOperands - 1).getImm();
+ bool SrcIsKill = Src2Op.isKill();
+
+ MachineInstr *NewMI =
----------------
RKSimon wrote:
any chance you can manipulate the existing instruction like we do in other cases in the pass?
https://github.com/llvm/llvm-project/pull/212626
More information about the llvm-commits
mailing list