[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 22:02:24 PST 2023


================
@@ -549,6 +551,36 @@ 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_ConstantInt(CInt), m_Value(X))) &&
+        match(Op1, m_One())) {
+      Value *ConstCttz =
+          IC.Builder.CreateBinaryIntrinsic(Intrinsic::cttz, CInt, Op1);
+      return IC.replaceInstUsesWith(II, IC.Builder.CreateAdd(ConstCttz, X));
----------------
dtcxzyw wrote:

Inferring `nsw/nuw` flags can exploit more optimization opportunities. You don't need to do anything until we find it is a missed optimization in some real-world applications.

But if you think it has value I'm OK with it. Beware of the type as `nsw/nuw` flags may not be applicable to `i1/i2` :)


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


More information about the llvm-commits mailing list