[llvm] [InstCombine] Fold (1 << a) to (a + 1) and (2 >> a) to (2 - a) when a is in [0,1] (PR #207108)

Aayush Shrivastava via llvm-commits llvm-commits at lists.llvm.org
Sun Jul 5 11:22:04 PDT 2026


================
@@ -1380,6 +1380,18 @@ Instruction *InstCombinerImpl::visitShl(BinaryOperator &I) {
       Value *NegX = Builder.CreateNeg(X, "neg");
       return BinaryOperator::CreateAnd(NegX, X);
     }
+
+    // 1 << X --> X + 1 if 0 <= X <= 1.
+    if (computeKnownBits(Op1, &I).countMaxActiveBits() <= 1) {
+      auto *Add = BinaryOperator::CreateAdd(Op1, ConstantInt::get(Ty, 1));
+      Add->setHasNoUnsignedWrap();
----------------
iamaayushrivastava wrote:

Done. Thanks!

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


More information about the llvm-commits mailing list