[llvm] [InstCombine] Fold `lshr -> zext -> shl` patterns (PR #147737)

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 10 20:29:57 PDT 2025


================
@@ -978,6 +978,47 @@ Instruction *InstCombinerImpl::foldLShrOverflowBit(BinaryOperator &I) {
   return new ZExtInst(Overflow, Ty);
 }
 
+/// If the operand of a zext-ed left shift \p V is a logically right-shifted
+/// value, try to fold the opposing shifts.
+static Instruction *foldShrThroughZExtedShl(Type *DestTy, Value *V,
+                                            unsigned ShlAmt,
+                                            InstCombinerImpl &IC,
+                                            const DataLayout &DL) {
+  auto *I = dyn_cast<Instruction>(V);
+  if (!I)
+    return nullptr;
+
+  // Dig through operations until the first shift.
+  while (!I->isShift())
+    if (!match(I, m_BinOp(m_OneUse(m_Instruction(I)), m_Constant())))
+      return nullptr;
----------------
arsenm wrote:

Recursing like this in a combine is a bit unusual; can you do this in SimplifyDemandedBits?

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


More information about the llvm-commits mailing list