[llvm] [AArch64] Peephole optimization to remove redundant csel instructions (PR #101483)

Marina Taylor via llvm-commits llvm-commits at lists.llvm.org
Fri Aug 2 04:19:46 PDT 2024


================
@@ -283,6 +287,26 @@ bool AArch64MIPeepholeOpt::visitORR(MachineInstr &MI) {
   return true;
 }
 
+bool AArch64MIPeepholeOpt::visitCSEL(MachineInstr &MI) {
+  // Replace CSEL with MOV when both inputs are the same register.
+  if (MI.getOperand(1).getReg() != MI.getOperand(2).getReg())
+    return false;
+
+  auto ZeroReg =
+      MI.getOpcode() == AArch64::CSELXr ? AArch64::XZR : AArch64::WZR;
+  auto OrOpcode =
+      MI.getOpcode() == AArch64::CSELXr ? AArch64::ORRXrs : AArch64::ORRWrs;
+
+  BuildMI(*MI.getParent(), MI, MI.getDebugLoc(), TII->get(OrOpcode))
+      .addReg(MI.getOperand(0).getReg(), RegState::Define)
+      .addReg(ZeroReg)
+      .addReg(MI.getOperand(1).getReg())
+      .addImm(0);
----------------
citymarina wrote:

As far as I understand, the `ORR` should be ok with the same sets of registers as the `CSEL`.

https://github.com/llvm/llvm-project/pull/101483


More information about the llvm-commits mailing list