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

via llvm-commits llvm-commits at lists.llvm.org
Fri Jul 11 20:48:44 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;
----------------
zGoldthorpe wrote:

Is there a way to avoid this loop in `SimplifyDemandedBits`? I'm not very familiar with the function, so I'm not sure how it can help identify an explicit right shift in the operand.

Recursing is also done indirectly through the calls to `canEvaluateShifted` and `getShiftedValue`. I could also modify `canEvaluateShifted` to provide (rather than just verify) shift amounts that can be simplified by `getShiftedValue`, which would remove the need for this explicit scan for the right shift as well.

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


More information about the llvm-commits mailing list