[llvm] [InstCombine] Fold shl of constant by cttz into multiply of lowest set bit (PR #214517)

via llvm-commits llvm-commits at lists.llvm.org
Fri Aug 14 23:07:47 PDT 2026


================
@@ -1382,6 +1382,19 @@ Instruction *InstCombinerImpl::visitShl(BinaryOperator &I) {
     }
   }
 
+  // C << (cttz X, true) --> (-X & X) * C  (C must be a scalar constant, cttz
+  // must have one use)
+  {
+    Value *X;
+    const APInt *C;
+    if (match(Op0, m_APInt(C)) &&
+        match(Op1, m_OneUse(m_Cttz(m_Value(X), m_One())))) {
+      Value *NegX = Builder.CreateNeg(X, "neg");
+      Value *LowBit = Builder.CreateAnd(NegX, X);
+      return BinaryOperator::CreateMul(LowBit, Op0);
----------------
milkHongYe wrote:

Thank you for your suggestions. I'll update this patch.

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


More information about the llvm-commits mailing list