[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:14 PDT 2026


================
@@ -1406,6 +1418,19 @@ Instruction *InstCombinerImpl::visitLShr(BinaryOperator &I) {
   if (match(Op0, m_One()))
     return new ZExtInst(Builder.CreateIsNull(Op1), Ty);
 
+  // 2 >> X --> 2 - X if 0 <= X <= 1.
+  if (match(Op0, m_SpecificInt(2)) &&
+      computeKnownBits(Op1, &I).countMaxActiveBits() <= 1) {
+    auto *Sub = BinaryOperator::CreateSub(ConstantInt::get(Ty, 2), Op1);
+    Sub->setHasNoUnsignedWrap();
----------------
iamaayushrivastava wrote:

Done. Thanks!

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


More information about the llvm-commits mailing list