[llvm] [AArch64][GlobalISel] Add pre-legalizer combines for AVGFLOOR and AVGCEIL (PR #192866)

David Green via llvm-commits llvm-commits at lists.llvm.org
Tue May 5 09:54:44 PDT 2026


================
@@ -8697,3 +8697,54 @@ bool CombinerHelper::matchCtls(MachineInstr &CtlzMI,
 
   return true;
 }
+
+// Fold shr ( add ( ext X, ext Y ), 1 ) -> avgfloor ( x, y )
+// Fold shr ( add ( ext X, ext Y, 1 ), 1 ) -> avgceil ( x, y )
+bool CombinerHelper::matchAVG(MachineInstr &MI, MachineRegisterInfo &MRI,
+                              bool IsSigned, bool IsCeil) const {
+  assert((MI.getOpcode() == TargetOpcode::G_LSHR ||
+          MI.getOpcode() == TargetOpcode::G_ASHR) &&
+         "Expected G_LSHR/G_ASHR");
+
+  Register Dst = MI.getOperand(0).getReg();
+  LLT DstTy = MRI.getType(Dst);
----------------
davemgreen wrote:

Can this check for legality of the Src type? Of X and Y (and make sure they are the same!)

This way we can generate avgfloor and avgceil from the src registers and zext/sext the result to the original type.

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


More information about the llvm-commits mailing list