[llvm] Missing opt with ctlz and shifts of power of 2 constants (#41333) (PR #74175)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Sun Dec 3 21:41:11 PST 2023


================
@@ -549,6 +551,37 @@ static Instruction *foldCttzCtlz(IntrinsicInst &II, InstCombinerImpl &IC) {
 
     if (match(Op0, m_Intrinsic<Intrinsic::abs>(m_Value(X))))
       return IC.replaceOperand(II, 0, X);
+
+    // cttz(shl(%const, %val), 1) --> add(cttz(%const, 1), %val)
+    if (match(Op0, m_Shl(m_Constant(C), m_Value(X))) && match(Op1, m_One())) {
+      Value *ConstCttz =
+          IC.Builder.CreateBinaryIntrinsic(Intrinsic::cttz, C, Op1);
+      return BinaryOperator::CreateAdd(ConstCttz, X);
+    }
+
+    // cttz(lshr exact (%const, %val), 0) --> sub(cttz(%const, 0), %val)
+    if (match(Op0, m_Exact(m_LShr(m_Constant(C), m_Value(X))))) {
+      Value *ConstCttz =
+          IC.Builder.CreateBinaryIntrinsic(Intrinsic::cttz, C, Op1);
+      return BinaryOperator::CreateSub(ConstCttz, X);
+    }
----------------
dtcxzyw wrote:

This transform is incorrect when `%const` is a zero.
Alive2: https://alive2.llvm.org/ce/z/f2rpnj


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


More information about the llvm-commits mailing list