[llvm] [GlobalISel] Combine (X >> C) << C to X & ((-1 >> C) << C) (PR #114821)

Thorsten Schütt via llvm-commits llvm-commits at lists.llvm.org
Tue Nov 5 05:22:15 PST 2024


================
@@ -1983,6 +1983,37 @@ void CombinerHelper::applyShiftOfShiftedLogic(MachineInstr &MI,
   MI.eraseFromParent();
 }
 
+bool CombinerHelper::matchLsbClearByShifts(MachineInstr &MI,
+                                           BuildFnTy &MatchInfo) {
+  // fold (A >> C) << C to A & K, where K = (-1 >> C) << C
+  const GShl *Shl = cast<GShl>(&MI);
+  GLShr *Lshr = cast<GLShr>(MRI.getVRegDef(Shl->getSrcReg()));
+
+  if (!MRI.hasOneNonDBGUse(Lshr->getReg(0)))
+    return false;
+
+  APInt C1, C2;
+  if (!mi_match(Shl->getShiftReg(), MRI, m_ICstOrSplat(C1)) ||
+      !mi_match(Lshr->getShiftReg(), MRI, m_ICstOrSplat(C2)))
+    return false;
+
+  if (C2.ne(C1))
+    return false;
+
+  Register Dst = Shl->getReg(0);
+  LLT DstTy = MRI.getType(Dst);
+
----------------
tschuett wrote:

legality checks for and, and constant.

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


More information about the llvm-commits mailing list