[llvm] [InstCombine] Fix #163110: Fold icmp (shl X, L), (add (shl Y, L), 1<<L) to icmp X, (Y + 1) (PR #165975)

via llvm-commits llvm-commits at lists.llvm.org
Thu Feb 12 03:56:21 PST 2026


https://github.com/Michael-Chen-NJU updated https://github.com/llvm/llvm-project/pull/165975

>From 3eacc3647f45513cfb34932fe943c91dce42f5c8 Mon Sep 17 00:00:00 2001
From: Michael-Chen-NJU <2802328816 at qq.com>
Date: Sat, 1 Nov 2025 14:58:47 +0800
Subject: [PATCH 01/14] [InstCombine] Add test case for fold (X << 5) == ((Y <<
 5) + 32)

---
 .../InstCombine/icmp-shl-add-to-add.ll        | 112 ++++++++++++++++++
 1 file changed, 112 insertions(+)
 create mode 100644 llvm/test/Transforms/InstCombine/icmp-shl-add-to-add.ll

diff --git a/llvm/test/Transforms/InstCombine/icmp-shl-add-to-add.ll b/llvm/test/Transforms/InstCombine/icmp-shl-add-to-add.ll
new file mode 100644
index 0000000000000..95dda32394c58
--- /dev/null
+++ b/llvm/test/Transforms/InstCombine/icmp-shl-add-to-add.ll
@@ -0,0 +1,112 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt < %s -passes=instcombine -S | FileCheck %s
+
+; Test case: Fold (X << 5) == ((Y << 5) + 32) into X == (Y + 1).
+; This corresponds to the provided alive2 proof.
+
+define i1 @shl_add_const_eq_base(i64 %v0, i64 %v3) {
+; CHECK-LABEL: @shl_add_const_eq_base(
+; CHECK-NEXT:    [[V1:%.*]] = shl nsw i64 %v0, 5
+; CHECK-NEXT:    [[V4:%.*]] = shl nsw i64 %v3, 5
+; CHECK-NEXT:    [[V5:%.*]] = add nsw i64 [[V4]], 32
+; CHECK-NEXT:    [[V6:%.*]] = icmp eq i64 [[V1]], [[V5]]
+; CHECK-NEXT:    ret i1 [[V6]]
+;
+  %v1 = shl nsw i64 %v0, 5
+  %v4 = shl nsw i64 %v3, 5
+  %v5 = add nsw i64 %v4, 32
+  %v6 = icmp eq i64 %v1, %v5
+  ret i1 %v6
+}
+
+; Test: icmp ne
+define i1 @shl_add_const_ne(i64 %v0, i64 %v3) {
+; CHECK-LABEL: @shl_add_const_ne(
+; CHECK-NEXT:    [[V1:%.*]] = shl nsw i64 [[V0:%.*]], 5
+; CHECK-NEXT:    [[V4:%.*]] = shl nsw i64 [[V3:%.*]], 5
+; CHECK-NEXT:    [[V5:%.*]] = add nsw i64 [[V4]], 32
+; CHECK-NEXT:    [[V6:%.*]] = icmp ne i64 [[V1]], [[V5]]
+; CHECK-NEXT:    ret i1 [[V6]]
+;
+  %v1 = shl nsw i64 %v0, 5
+  %v4 = shl nsw i64 %v3, 5
+  %v5 = add nsw i64 %v4, 32
+  %v6 = icmp ne i64 %v1, %v5 ; Note: icmp ne
+  ret i1 %v6
+}
+
+; Test: shl amounts do not match (5 vs 4).
+define i1 @shl_add_const_eq_mismatch_shl_amt(i64 %v0, i64 %v3) {
+; CHECK-LABEL: @shl_add_const_eq_mismatch_shl_amt(
+; CHECK-NEXT:    [[V1:%.*]] = shl nsw i64 %v0, 5
+; CHECK-NEXT:    [[V4:%.*]] = shl nsw i64 %v3, 4
+; CHECK-NEXT:    [[V5:%.*]] = add nsw i64 [[V4]], 16
+; CHECK-NEXT:    [[V6:%.*]] = icmp eq i64 [[V1]], [[V5]]
+; CHECK-NEXT:    ret i1 [[V6]]
+;
+  %v1 = shl nsw i64 %v0, 5
+  %v4 = shl nsw i64 %v3, 4  ; Shift amount mismatch
+  %v5 = add nsw i64 %v4, 16
+  %v6 = icmp eq i64 %v1, %v5
+  ret i1 %v6
+}
+
+; Test: Constant is wrong (32 vs 64).
+define i1 @shl_add_const_eq_wrong_constant(i64 %v0, i64 %v3) {
+; CHECK-LABEL: @shl_add_const_eq_wrong_constant(
+; CHECK-NEXT:    [[V1:%.*]] = shl nsw i64 %v0, 5
+; CHECK-NEXT:    [[V4:%.*]] = shl nsw i64 %v3, 5
+; CHECK-NEXT:    [[V5:%.*]] = add nsw i64 [[V4]], 64
+; CHECK-NEXT:    [[V6:%.*]] = icmp eq i64 [[V1]], [[V5]]
+; CHECK-NEXT:    ret i1 [[V6]]
+;
+  %v1 = shl nsw i64 %v0, 5
+  %v4 = shl nsw i64 %v3, 5
+  %v5 = add nsw i64 %v4, 64  ; Constant mismatch
+  %v6 = icmp eq i64 %v1, %v5
+  ret i1 %v6
+}
+
+; Test: Missing NSW flag on one of the shl instructions.
+define i1 @shl_add_const_eq_no_nsw_on_v1(i64 %v0, i64 %v3) {
+; CHECK-LABEL: @shl_add_const_eq_no_nsw_on_v1(
+; CHECK-NEXT:    [[V1:%.*]] = shl i64 %v0, 5
+; CHECK-NEXT:    [[V4:%.*]] = shl nsw i64 %v3, 5
+; CHECK-NEXT:    [[V5:%.*]] = add nsw i64 [[V4]], 32
+; CHECK-NEXT:    [[V6:%.*]] = icmp eq i64 [[V1]], [[V5]]
+; CHECK-NEXT:    ret i1 [[V6]]
+;
+  %v1 = shl i64 %v0, 5 ; Missing nsw
+  %v4 = shl nsw i64 %v3, 5
+  %v5 = add nsw i64 %v4, 32
+  %v6 = icmp eq i64 %v1, %v5
+  ret i1 %v6
+}
+
+; Test: Lower bit width (i8) and different shift amount (3). Constant is 8.
+define i1 @shl_add_const_eq_i8(i8 %v0, i8 %v3) {
+; CHECK-LABEL: @shl_add_const_eq_i8(
+; CHECK-NEXT:    [[V7:%.*]] = add nsw i8 %v3, 1
+; CHECK-NEXT:    [[V6:%.*]] = icmp eq i8 %v0, [[V7]]
+; CHECK-NEXT:    ret i1 [[V6]]
+;
+  %v1 = shl nsw i8 %v0, 3
+  %v4 = shl nsw i8 %v3, 3
+  %v5 = add nsw i8 %v4, 8 ; 2^3 = 8
+  %v6 = icmp eq i8 %v1, %v5
+  ret i1 %v6
+}
+
+; Test: i32 bit width and larger shift amount (10). Constant is 1024.
+define i1 @shl_add_const_eq_i32(i32 %v0, i32 %v3) {
+; CHECK-LABEL: @shl_add_const_eq_i32(
+; CHECK-NEXT:    [[V7:%.*]] = add nsw i32 %v3, 1
+; CHECK-NEXT:    [[V6:%.*]] = icmp eq i32 %v0, [[V7]]
+; CHECK-NEXT:    ret i1 [[V6]]
+;
+  %v1 = shl nsw i32 %v0, 10
+  %v4 = shl nsw i32 %v3, 10
+  %v5 = add nsw i32 %v4, 1024 ; 2^10 = 1024
+  %v6 = icmp eq i32 %v1, %v5
+  ret i1 %v6
+}

>From cb022137e9459586a606b22d1f3d7d2bf8ac81f8 Mon Sep 17 00:00:00 2001
From: Michael-Chen-NJU <2802328816 at qq.com>
Date: Sat, 1 Nov 2025 15:19:28 +0800
Subject: [PATCH 02/14] [InstCombine] Optimize icmp with shl and add by folding
 (X << Log2) == ((Y << Log2) + K) into X == (Y + 1)

---
 .../InstCombine/InstCombineCompares.cpp       | 20 ++++++++++++
 .../InstCombine/icmp-shl-add-to-add.ll        | 32 ++++++++-----------
 2 files changed, 34 insertions(+), 18 deletions(-)

diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
index fba1ccf2c8c9b..28d3c772acdcc 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -6001,6 +6001,26 @@ Instruction *InstCombinerImpl::foldICmpEquality(ICmpInst &I) {
 
   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
   const CmpInst::Predicate Pred = I.getPredicate();
+
+  //icmp (shl nsw X, Log2), (add nsw (shl nsw Y, Log2), K) -> icmp X, (add nsw Y, 1)
+  Value *X, *Y;
+  ConstantInt *CLog2M0, *CLog2M1, *CVal;
+  auto M0 = m_NSWShl(m_Value(X), m_ConstantInt(CLog2M0));
+  auto M1 = m_NSWAdd(m_NSWShl (m_Value(Y), m_ConstantInt(CLog2M1)),
+                      m_ConstantInt(CVal));
+
+  if (match(&I, m_c_ICmp(M0, M1)) && CLog2M0->getValue() == CLog2M1->getValue()) {
+    unsigned BitWidth = CLog2M0->getBitWidth();
+    unsigned ShAmt = (unsigned)CLog2M0->getLimitedValue(BitWidth);
+    APInt ExpectedK = APInt::getOneBitSet(BitWidth, ShAmt);
+    if (CVal->getValue() == ExpectedK) {
+      Value *NewRHS = Builder.CreateAdd(
+          Y, ConstantInt::get(Y->getType(), 1),
+          "", /*HasNUW=*/false, /*HasNSW=*/true);
+      return new ICmpInst(Pred, X, NewRHS);
+    }
+  }
+  
   Value *A, *B, *C, *D;
   if (match(Op0, m_Xor(m_Value(A), m_Value(B)))) {
     if (A == Op1 || B == Op1) { // (A^B) == A  ->  B == 0
diff --git a/llvm/test/Transforms/InstCombine/icmp-shl-add-to-add.ll b/llvm/test/Transforms/InstCombine/icmp-shl-add-to-add.ll
index 95dda32394c58..0f375a05528a2 100644
--- a/llvm/test/Transforms/InstCombine/icmp-shl-add-to-add.ll
+++ b/llvm/test/Transforms/InstCombine/icmp-shl-add-to-add.ll
@@ -6,10 +6,8 @@
 
 define i1 @shl_add_const_eq_base(i64 %v0, i64 %v3) {
 ; CHECK-LABEL: @shl_add_const_eq_base(
-; CHECK-NEXT:    [[V1:%.*]] = shl nsw i64 %v0, 5
-; CHECK-NEXT:    [[V4:%.*]] = shl nsw i64 %v3, 5
-; CHECK-NEXT:    [[V5:%.*]] = add nsw i64 [[V4]], 32
-; CHECK-NEXT:    [[V6:%.*]] = icmp eq i64 [[V1]], [[V5]]
+; CHECK-NEXT:    [[V5:%.*]] = add nsw i64 [[V3:%.*]], 1
+; CHECK-NEXT:    [[V6:%.*]] = icmp eq i64 [[V1:%.*]], [[V5]]
 ; CHECK-NEXT:    ret i1 [[V6]]
 ;
   %v1 = shl nsw i64 %v0, 5
@@ -22,10 +20,8 @@ define i1 @shl_add_const_eq_base(i64 %v0, i64 %v3) {
 ; Test: icmp ne
 define i1 @shl_add_const_ne(i64 %v0, i64 %v3) {
 ; CHECK-LABEL: @shl_add_const_ne(
-; CHECK-NEXT:    [[V1:%.*]] = shl nsw i64 [[V0:%.*]], 5
-; CHECK-NEXT:    [[V4:%.*]] = shl nsw i64 [[V3:%.*]], 5
-; CHECK-NEXT:    [[V5:%.*]] = add nsw i64 [[V4]], 32
-; CHECK-NEXT:    [[V6:%.*]] = icmp ne i64 [[V1]], [[V5]]
+; CHECK-NEXT:    [[V5:%.*]] = add nsw i64 [[V3:%.*]], 1
+; CHECK-NEXT:    [[V6:%.*]] = icmp ne i64 [[V1:%.*]], [[V5]]
 ; CHECK-NEXT:    ret i1 [[V6]]
 ;
   %v1 = shl nsw i64 %v0, 5
@@ -38,8 +34,8 @@ define i1 @shl_add_const_ne(i64 %v0, i64 %v3) {
 ; Test: shl amounts do not match (5 vs 4).
 define i1 @shl_add_const_eq_mismatch_shl_amt(i64 %v0, i64 %v3) {
 ; CHECK-LABEL: @shl_add_const_eq_mismatch_shl_amt(
-; CHECK-NEXT:    [[V1:%.*]] = shl nsw i64 %v0, 5
-; CHECK-NEXT:    [[V4:%.*]] = shl nsw i64 %v3, 4
+; CHECK-NEXT:    [[V1:%.*]] = shl nsw i64 [[V0:%.*]], 5
+; CHECK-NEXT:    [[V4:%.*]] = shl nsw i64 [[V3:%.*]], 4
 ; CHECK-NEXT:    [[V5:%.*]] = add nsw i64 [[V4]], 16
 ; CHECK-NEXT:    [[V6:%.*]] = icmp eq i64 [[V1]], [[V5]]
 ; CHECK-NEXT:    ret i1 [[V6]]
@@ -54,8 +50,8 @@ define i1 @shl_add_const_eq_mismatch_shl_amt(i64 %v0, i64 %v3) {
 ; Test: Constant is wrong (32 vs 64).
 define i1 @shl_add_const_eq_wrong_constant(i64 %v0, i64 %v3) {
 ; CHECK-LABEL: @shl_add_const_eq_wrong_constant(
-; CHECK-NEXT:    [[V1:%.*]] = shl nsw i64 %v0, 5
-; CHECK-NEXT:    [[V4:%.*]] = shl nsw i64 %v3, 5
+; CHECK-NEXT:    [[V1:%.*]] = shl nsw i64 [[V0:%.*]], 5
+; CHECK-NEXT:    [[V4:%.*]] = shl nsw i64 [[V3:%.*]], 5
 ; CHECK-NEXT:    [[V5:%.*]] = add nsw i64 [[V4]], 64
 ; CHECK-NEXT:    [[V6:%.*]] = icmp eq i64 [[V1]], [[V5]]
 ; CHECK-NEXT:    ret i1 [[V6]]
@@ -70,8 +66,8 @@ define i1 @shl_add_const_eq_wrong_constant(i64 %v0, i64 %v3) {
 ; Test: Missing NSW flag on one of the shl instructions.
 define i1 @shl_add_const_eq_no_nsw_on_v1(i64 %v0, i64 %v3) {
 ; CHECK-LABEL: @shl_add_const_eq_no_nsw_on_v1(
-; CHECK-NEXT:    [[V1:%.*]] = shl i64 %v0, 5
-; CHECK-NEXT:    [[V4:%.*]] = shl nsw i64 %v3, 5
+; CHECK-NEXT:    [[V1:%.*]] = shl i64 [[V0:%.*]], 5
+; CHECK-NEXT:    [[V4:%.*]] = shl nsw i64 [[V3:%.*]], 5
 ; CHECK-NEXT:    [[V5:%.*]] = add nsw i64 [[V4]], 32
 ; CHECK-NEXT:    [[V6:%.*]] = icmp eq i64 [[V1]], [[V5]]
 ; CHECK-NEXT:    ret i1 [[V6]]
@@ -86,8 +82,8 @@ define i1 @shl_add_const_eq_no_nsw_on_v1(i64 %v0, i64 %v3) {
 ; Test: Lower bit width (i8) and different shift amount (3). Constant is 8.
 define i1 @shl_add_const_eq_i8(i8 %v0, i8 %v3) {
 ; CHECK-LABEL: @shl_add_const_eq_i8(
-; CHECK-NEXT:    [[V7:%.*]] = add nsw i8 %v3, 1
-; CHECK-NEXT:    [[V6:%.*]] = icmp eq i8 %v0, [[V7]]
+; CHECK-NEXT:    [[TMP1:%.*]] = add nsw i8 [[V3:%.*]], 1
+; CHECK-NEXT:    [[V6:%.*]] = icmp eq i8 [[V0:%.*]], [[TMP1]]
 ; CHECK-NEXT:    ret i1 [[V6]]
 ;
   %v1 = shl nsw i8 %v0, 3
@@ -100,8 +96,8 @@ define i1 @shl_add_const_eq_i8(i8 %v0, i8 %v3) {
 ; Test: i32 bit width and larger shift amount (10). Constant is 1024.
 define i1 @shl_add_const_eq_i32(i32 %v0, i32 %v3) {
 ; CHECK-LABEL: @shl_add_const_eq_i32(
-; CHECK-NEXT:    [[V7:%.*]] = add nsw i32 %v3, 1
-; CHECK-NEXT:    [[V6:%.*]] = icmp eq i32 %v0, [[V7]]
+; CHECK-NEXT:    [[TMP1:%.*]] = add nsw i32 [[V3:%.*]], 1
+; CHECK-NEXT:    [[V6:%.*]] = icmp eq i32 [[V0:%.*]], [[TMP1]]
 ; CHECK-NEXT:    ret i1 [[V6]]
 ;
   %v1 = shl nsw i32 %v0, 10

>From c32b54fc00827a65085f0e5eab67832d1b1cad63 Mon Sep 17 00:00:00 2001
From: Michael-Chen-NJU <2802328816 at qq.com>
Date: Sat, 1 Nov 2025 18:45:17 +0800
Subject: [PATCH 03/14] [InstCombine] Add tests for multi-use cases and vector
 operations in icmp-shl-add optimization

---
 .../InstCombine/icmp-shl-add-to-add.ll        | 69 +++++++++++++++++++
 1 file changed, 69 insertions(+)

diff --git a/llvm/test/Transforms/InstCombine/icmp-shl-add-to-add.ll b/llvm/test/Transforms/InstCombine/icmp-shl-add-to-add.ll
index 0f375a05528a2..f0f3ff0b9b2ef 100644
--- a/llvm/test/Transforms/InstCombine/icmp-shl-add-to-add.ll
+++ b/llvm/test/Transforms/InstCombine/icmp-shl-add-to-add.ll
@@ -4,6 +4,8 @@
 ; Test case: Fold (X << 5) == ((Y << 5) + 32) into X == (Y + 1).
 ; This corresponds to the provided alive2 proof.
 
+declare void @use_i64(i64)
+
 define i1 @shl_add_const_eq_base(i64 %v0, i64 %v3) {
 ; CHECK-LABEL: @shl_add_const_eq_base(
 ; CHECK-NEXT:    [[V5:%.*]] = add nsw i64 [[V3:%.*]], 1
@@ -106,3 +108,70 @@ define i1 @shl_add_const_eq_i32(i32 %v0, i32 %v3) {
   %v6 = icmp eq i32 %v1, %v5
   ret i1 %v6
 }
+
+; Test: Multi-use case. The optimization should still occur if applicable,
+; but the extraneous call must be preserved.
+define i1 @shl_add_const_eq_multi_use(i64 %v0, i64 %v3) {
+; CHECK-LABEL: @shl_add_const_eq_multi_use(
+; CHECK-NEXT:    [[V1:%.*]] = shl nsw i64 [[V0:%.*]], 5
+; CHECK-NEXT:    call void @use_i64(i64 [[V1]])
+; CHECK-NEXT:    [[V4:%.*]] = shl nsw i64 [[V3:%.*]], 5
+; CHECK-NEXT:    [[V5:%.*]] = add nsw i64 [[V4]], 32
+; CHECK-NEXT:    [[V6:%.*]] = icmp eq i64 [[V1]], [[V5]]
+; CHECK-NEXT:    ret i1 [[V6]]
+;
+  %v1 = shl nsw i64 %v0, 5
+  call void @use_i64(i64 %v1) ; Additional use of v1
+  %v4 = shl nsw i64 %v3, 5
+  %v5 = add nsw i64 %v4, 32
+  %v6 = icmp eq i64 %v1, %v5
+  ret i1 %v6
+}
+
+; Test: Vector splat. Should fold once optimization is applied.
+define <2 x i1> @shl_add_const_eq_vec_splat(<2 x i64> %v0, <2 x i64> %v3) {
+; CHECK-LABEL: @shl_add_const_eq_vec_splat(
+; CHECK-NEXT:    [[V1:%.*]] = shl nsw <2 x i64> [[V0:%.*]], <i64 5, i64 5>
+; CHECK-NEXT:    [[V4:%.*]] = shl nsw <2 x i64> [[V3:%.*]], <i64 5, i64 5>
+; CHECK-NEXT:    [[V5:%.*]] = add nsw <2 x i64> [[V4]], <i64 32, i64 32>
+; CHECK-NEXT:    [[V6:%.*]] = icmp eq <2 x i64> [[V1]], [[V5]]
+; CHECK-NEXT:    ret <2 x i1> [[V6]]
+;
+  %v1 = shl nsw <2 x i64> %v0, <i64 5, i64 5>
+  %v4 = shl nsw <2 x i64> %v3, <i64 5, i64 5>
+  %v5 = add nsw <2 x i64> %v4, <i64 32, i64 32>
+  %v6 = icmp eq <2 x i64> %v1, %v5
+  ret <2 x i1> %v6
+}
+
+; Test: Vector splat with poison. Should fold once optimization is applied.
+define <2 x i1> @shl_add_const_eq_vec_splat_poison(<2 x i64> %v0, <2 x i64> %v3) {
+; CHECK-LABEL: @shl_add_const_eq_vec_splat_poison(
+; CHECK-NEXT:    [[V1:%.*]] = shl nsw <2 x i64> [[V0:%.*]], <i64 5, i64 5>
+; CHECK-NEXT:    [[V4:%.*]] = shl nsw <2 x i64> [[V3:%.*]], <i64 5, i64 5>
+; CHECK-NEXT:    [[V5:%.*]] = add nsw <2 x i64> [[V4]], <i64 32, i64 poison>
+; CHECK-NEXT:    [[V6:%.*]] = icmp eq <2 x i64> [[V1]], [[V5]]
+; CHECK-NEXT:    ret <2 x i1> [[V6]]
+;
+  %v1 = shl nsw <2 x i64> %v0, <i64 5, i64 5>
+  %v4 = shl nsw <2 x i64> %v3, <i64 5, i64 5>
+  %v5 = add nsw <2 x i64> %v4, <i64 32, i64 poison>
+  %v6 = icmp eq <2 x i64> %v1, %v5
+  ret <2 x i1> %v6
+}
+
+; Test: Vector non-splat (should not fold).
+define <2 x i1> @shl_add_const_eq_vec_non_splat(<2 x i64> %v0, <2 x i64> %v3) {
+; CHECK-LABEL: @shl_add_const_eq_vec_non_splat(
+; CHECK-NEXT:    [[V1:%.*]] = shl nsw <2 x i64> [[V0:%.*]], <i64 5, i64 6>
+; CHECK-NEXT:    [[V4:%.*]] = shl nsw <2 x i64> [[V3:%.*]], <i64 5, i64 6>
+; CHECK-NEXT:    [[V5:%.*]] = add nsw <2 x i64> [[V4]], <i64 32, i64 64>
+; CHECK-NEXT:    [[V6:%.*]] = icmp eq <2 x i64> [[V1]], [[V5]]
+; CHECK-NEXT:    ret <2 x i1> [[V6]]
+;
+  %v1 = shl nsw <2 x i64> %v0, <i64 5, i64 6>
+  %v4 = shl nsw <2 x i64> %v3, <i64 5, i64 6>
+  %v5 = add nsw <2 x i64> %v4, <i64 32, i64 64>
+  %v6 = icmp eq <2 x i64> %v1, %v5
+  ret <2 x i1> %v6
+}

>From 6dc8405a47d4664c2ed345ee7b30e3f61938c4ae Mon Sep 17 00:00:00 2001
From: Michael-Chen-NJU <2802328816 at qq.com>
Date: Sat, 1 Nov 2025 19:22:15 +0800
Subject: [PATCH 04/14] [InstCombine] Refactor icmp folding logic for shl and
 add operations with APInt

---
 .../InstCombine/InstCombineCompares.cpp       | 22 +++++++++----------
 .../InstCombine/icmp-shl-add-to-add.ll        | 17 +++++---------
 2 files changed, 17 insertions(+), 22 deletions(-)

diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
index 28d3c772acdcc..734d27c70705e 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -6002,25 +6002,25 @@ Instruction *InstCombinerImpl::foldICmpEquality(ICmpInst &I) {
   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
   const CmpInst::Predicate Pred = I.getPredicate();
 
-  //icmp (shl nsw X, Log2), (add nsw (shl nsw Y, Log2), K) -> icmp X, (add nsw Y, 1)
+  // icmp (shl nsw X, Log2), (add nsw (shl nsw Y, Log2), K) -> icmp X, (add nsw
+  // Y, 1)
   Value *X, *Y;
-  ConstantInt *CLog2M0, *CLog2M1, *CVal;
-  auto M0 = m_NSWShl(m_Value(X), m_ConstantInt(CLog2M0));
-  auto M1 = m_NSWAdd(m_NSWShl (m_Value(Y), m_ConstantInt(CLog2M1)),
-                      m_ConstantInt(CVal));
+  const APInt *CLog2M0, *CLog2M1, *CVal;
+  auto M0 = m_NSWShl(m_Value(X), m_APIntAllowPoison(CLog2M0));
+  auto M1 = m_NSWAdd(m_NSWShl(m_Value(Y), m_APIntAllowPoison(CLog2M1)),
+                     m_APIntAllowPoison(CVal));
 
-  if (match(&I, m_c_ICmp(M0, M1)) && CLog2M0->getValue() == CLog2M1->getValue()) {
+  if (match(&I, m_c_ICmp(M0, M1)) && *CLog2M0 == *CLog2M1) {
     unsigned BitWidth = CLog2M0->getBitWidth();
     unsigned ShAmt = (unsigned)CLog2M0->getLimitedValue(BitWidth);
     APInt ExpectedK = APInt::getOneBitSet(BitWidth, ShAmt);
-    if (CVal->getValue() == ExpectedK) {
-      Value *NewRHS = Builder.CreateAdd(
-          Y, ConstantInt::get(Y->getType(), 1),
-          "", /*HasNUW=*/false, /*HasNSW=*/true);
+    if (*CVal == ExpectedK) {
+      Value *NewRHS = Builder.CreateAdd(Y, ConstantInt::get(Y->getType(), 1),
+                                        "", /*HasNUW=*/false, /*HasNSW=*/true);
       return new ICmpInst(Pred, X, NewRHS);
     }
   }
-  
+
   Value *A, *B, *C, *D;
   if (match(Op0, m_Xor(m_Value(A), m_Value(B)))) {
     if (A == Op1 || B == Op1) { // (A^B) == A  ->  B == 0
diff --git a/llvm/test/Transforms/InstCombine/icmp-shl-add-to-add.ll b/llvm/test/Transforms/InstCombine/icmp-shl-add-to-add.ll
index f0f3ff0b9b2ef..1523b283b8b08 100644
--- a/llvm/test/Transforms/InstCombine/icmp-shl-add-to-add.ll
+++ b/llvm/test/Transforms/InstCombine/icmp-shl-add-to-add.ll
@@ -115,9 +115,8 @@ define i1 @shl_add_const_eq_multi_use(i64 %v0, i64 %v3) {
 ; CHECK-LABEL: @shl_add_const_eq_multi_use(
 ; CHECK-NEXT:    [[V1:%.*]] = shl nsw i64 [[V0:%.*]], 5
 ; CHECK-NEXT:    call void @use_i64(i64 [[V1]])
-; CHECK-NEXT:    [[V4:%.*]] = shl nsw i64 [[V3:%.*]], 5
-; CHECK-NEXT:    [[V5:%.*]] = add nsw i64 [[V4]], 32
-; CHECK-NEXT:    [[V6:%.*]] = icmp eq i64 [[V1]], [[V5]]
+; CHECK-NEXT:    [[TMP1:%.*]] = add nsw i64 [[V3:%.*]], 1
+; CHECK-NEXT:    [[V6:%.*]] = icmp eq i64 [[V0]], [[TMP1]]
 ; CHECK-NEXT:    ret i1 [[V6]]
 ;
   %v1 = shl nsw i64 %v0, 5
@@ -131,10 +130,8 @@ define i1 @shl_add_const_eq_multi_use(i64 %v0, i64 %v3) {
 ; Test: Vector splat. Should fold once optimization is applied.
 define <2 x i1> @shl_add_const_eq_vec_splat(<2 x i64> %v0, <2 x i64> %v3) {
 ; CHECK-LABEL: @shl_add_const_eq_vec_splat(
-; CHECK-NEXT:    [[V1:%.*]] = shl nsw <2 x i64> [[V0:%.*]], <i64 5, i64 5>
-; CHECK-NEXT:    [[V4:%.*]] = shl nsw <2 x i64> [[V3:%.*]], <i64 5, i64 5>
-; CHECK-NEXT:    [[V5:%.*]] = add nsw <2 x i64> [[V4]], <i64 32, i64 32>
-; CHECK-NEXT:    [[V6:%.*]] = icmp eq <2 x i64> [[V1]], [[V5]]
+; CHECK-NEXT:    [[V5:%.*]] = add nsw <2 x i64> [[V3:%.*]], splat (i64 1)
+; CHECK-NEXT:    [[V6:%.*]] = icmp eq <2 x i64> [[V1:%.*]], [[V5]]
 ; CHECK-NEXT:    ret <2 x i1> [[V6]]
 ;
   %v1 = shl nsw <2 x i64> %v0, <i64 5, i64 5>
@@ -147,10 +144,8 @@ define <2 x i1> @shl_add_const_eq_vec_splat(<2 x i64> %v0, <2 x i64> %v3) {
 ; Test: Vector splat with poison. Should fold once optimization is applied.
 define <2 x i1> @shl_add_const_eq_vec_splat_poison(<2 x i64> %v0, <2 x i64> %v3) {
 ; CHECK-LABEL: @shl_add_const_eq_vec_splat_poison(
-; CHECK-NEXT:    [[V1:%.*]] = shl nsw <2 x i64> [[V0:%.*]], <i64 5, i64 5>
-; CHECK-NEXT:    [[V4:%.*]] = shl nsw <2 x i64> [[V3:%.*]], <i64 5, i64 5>
-; CHECK-NEXT:    [[V5:%.*]] = add nsw <2 x i64> [[V4]], <i64 32, i64 poison>
-; CHECK-NEXT:    [[V6:%.*]] = icmp eq <2 x i64> [[V1]], [[V5]]
+; CHECK-NEXT:    [[V5:%.*]] = add nsw <2 x i64> [[V3:%.*]], splat (i64 1)
+; CHECK-NEXT:    [[V6:%.*]] = icmp eq <2 x i64> [[V1:%.*]], [[V5]]
 ; CHECK-NEXT:    ret <2 x i1> [[V6]]
 ;
   %v1 = shl nsw <2 x i64> %v0, <i64 5, i64 5>

>From 2c8b91de405cc7cd000286dcd19c24385f1d2db8 Mon Sep 17 00:00:00 2001
From: Michael-Chen-NJU <2802328816 at qq.com>
Date: Mon, 8 Dec 2025 13:18:15 +0800
Subject: [PATCH 05/14] [InstCombine] Fold icmp (shl nsw X, L), (add nsw (shl
 nsw Y, L), K).

---
 .../Transforms/InstCombine/InstCombineCompares.cpp   | 11 ++++++-----
 .../Transforms/InstCombine/icmp-shl-add-to-add.ll    | 12 +++++-------
 2 files changed, 11 insertions(+), 12 deletions(-)

diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
index e1404d50c8a75..8f4f7220aa0fc 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -6024,8 +6024,9 @@ Instruction *InstCombinerImpl::foldICmpEquality(ICmpInst &I) {
   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
   const CmpInst::Predicate Pred = I.getPredicate();
 
-  // icmp (shl nsw X, Log2), (add nsw (shl nsw Y, Log2), K) -> icmp X, (add nsw
-  // Y, 1)
+  // icmp (shl nsw X, L), (add nsw (shl nsw Y, L), K) where K is a multiple of
+  // 2^L
+  // -> icmp X, (add nsw Y, K / 2^L)
   Value *X, *Y;
   const APInt *CLog2M0, *CLog2M1, *CVal;
   auto M0 = m_NSWShl(m_Value(X), m_APIntAllowPoison(CLog2M0));
@@ -6035,9 +6036,9 @@ Instruction *InstCombinerImpl::foldICmpEquality(ICmpInst &I) {
   if (match(&I, m_c_ICmp(M0, M1)) && *CLog2M0 == *CLog2M1) {
     unsigned BitWidth = CLog2M0->getBitWidth();
     unsigned ShAmt = (unsigned)CLog2M0->getLimitedValue(BitWidth);
-    APInt ExpectedK = APInt::getOneBitSet(BitWidth, ShAmt);
-    if (*CVal == ExpectedK) {
-      Value *NewRHS = Builder.CreateAdd(Y, ConstantInt::get(Y->getType(), 1),
+    if (CVal->countr_zero() >= ShAmt) {
+      APInt NewK = CVal->lshr(ShAmt);
+      Value *NewRHS = Builder.CreateAdd(Y, ConstantInt::get(Y->getType(), NewK),
                                         "", /*HasNUW=*/false, /*HasNSW=*/true);
       return new ICmpInst(Pred, X, NewRHS);
     }
diff --git a/llvm/test/Transforms/InstCombine/icmp-shl-add-to-add.ll b/llvm/test/Transforms/InstCombine/icmp-shl-add-to-add.ll
index 1523b283b8b08..52dc3b0f5376a 100644
--- a/llvm/test/Transforms/InstCombine/icmp-shl-add-to-add.ll
+++ b/llvm/test/Transforms/InstCombine/icmp-shl-add-to-add.ll
@@ -49,13 +49,11 @@ define i1 @shl_add_const_eq_mismatch_shl_amt(i64 %v0, i64 %v3) {
   ret i1 %v6
 }
 
-; Test: Constant is wrong (32 vs 64).
-define i1 @shl_add_const_eq_wrong_constant(i64 %v0, i64 %v3) {
-; CHECK-LABEL: @shl_add_const_eq_wrong_constant(
-; CHECK-NEXT:    [[V1:%.*]] = shl nsw i64 [[V0:%.*]], 5
-; CHECK-NEXT:    [[V4:%.*]] = shl nsw i64 [[V3:%.*]], 5
-; CHECK-NEXT:    [[V5:%.*]] = add nsw i64 [[V4]], 64
-; CHECK-NEXT:    [[V6:%.*]] = icmp eq i64 [[V1]], [[V5]]
+; Test: Constant K is a multiple of 2^L (64 vs 32). Should simplify to K/2^L = 2.
+define i1 @shl_add_const_eq_k_multiple_of_pow2(i64 %v0, i64 %v3) {
+; CHECK-LABEL: @shl_add_const_eq_k_multiple_of_pow2(
+; CHECK-NEXT:    [[V5:%.*]] = add nsw i64 [[V3:%.*]], 2
+; CHECK-NEXT:    [[V6:%.*]] = icmp eq i64 [[V1:%.*]], [[V5]]
 ; CHECK-NEXT:    ret i1 [[V6]]
 ;
   %v1 = shl nsw i64 %v0, 5

>From 64addefd3e337e349018c3fce55f66a8edc98b45 Mon Sep 17 00:00:00 2001
From: Michael-Chen-NJU <2802328816 at qq.com>
Date: Mon, 12 Jan 2026 01:32:47 +0800
Subject: [PATCH 06/14] using canEvaluateShifted & getShiftedValue

---
 .../InstCombine/InstCombineCompares.cpp       | 22 ++---
 .../InstCombine/InstCombineInternal.h         |  4 +
 .../InstCombine/InstCombineShifts.cpp         | 82 ++++++++++++-------
 3 files changed, 65 insertions(+), 43 deletions(-)

diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
index 8f4f7220aa0fc..e8327421973b0 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -6027,20 +6027,14 @@ Instruction *InstCombinerImpl::foldICmpEquality(ICmpInst &I) {
   // icmp (shl nsw X, L), (add nsw (shl nsw Y, L), K) where K is a multiple of
   // 2^L
   // -> icmp X, (add nsw Y, K / 2^L)
-  Value *X, *Y;
-  const APInt *CLog2M0, *CLog2M1, *CVal;
-  auto M0 = m_NSWShl(m_Value(X), m_APIntAllowPoison(CLog2M0));
-  auto M1 = m_NSWAdd(m_NSWShl(m_Value(Y), m_APIntAllowPoison(CLog2M1)),
-                     m_APIntAllowPoison(CVal));
-
-  if (match(&I, m_c_ICmp(M0, M1)) && *CLog2M0 == *CLog2M1) {
-    unsigned BitWidth = CLog2M0->getBitWidth();
-    unsigned ShAmt = (unsigned)CLog2M0->getLimitedValue(BitWidth);
-    if (CVal->countr_zero() >= ShAmt) {
-      APInt NewK = CVal->lshr(ShAmt);
-      Value *NewRHS = Builder.CreateAdd(Y, ConstantInt::get(Y->getType(), NewK),
-                                        "", /*HasNUW=*/false, /*HasNSW=*/true);
-      return new ICmpInst(Pred, X, NewRHS);
+  Value *X;
+  const APInt *CShAmt;
+  if (match(Op0, m_NSWShl(m_Value(X), m_APIntAllowPoison(CShAmt)))) {
+    unsigned ShAmt = CShAmt->getZExtValue();
+    if (canEvaluateShifted(Op1, ShAmt, false, &I)) {
+      Value *NewOp0 = X;
+      Value *NewOp1 = getShiftedValue(Op1, ShAmt, false);
+      return new ICmpInst(Pred, NewOp0, NewOp1);
     }
   }
 
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineInternal.h b/llvm/lib/Transforms/InstCombine/InstCombineInternal.h
index 9bdd8cb71f7f3..dd4138b55421f 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineInternal.h
+++ b/llvm/lib/Transforms/InstCombine/InstCombineInternal.h
@@ -448,6 +448,10 @@ class LLVM_LIBRARY_VISIBILITY InstCombinerImpl final
                               bool InvertFalseVal = false);
   Value *getSelectCondition(Value *A, Value *B, bool ABIsTheSame);
 
+  bool canEvaluateShifted(Value *V, unsigned NumBits, bool IsLeftShift,
+                          Instruction *CxtI);
+  Value *getShiftedValue(Value *V, unsigned NumBits, bool isLeftShift);
+
   Instruction *foldLShrOverflowBit(BinaryOperator &I);
   Instruction *foldExtractOfOverflowIntrinsic(ExtractValueInst &EV);
   Instruction *foldIntrinsicWithOverflowCommon(IntrinsicInst *II);
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp b/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
index 899a3c16554c9..d7624be48314a 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
@@ -584,8 +584,8 @@ static bool canEvaluateShiftedShift(unsigned OuterShAmt, bool IsOuterShl,
 ///      %F = lshr i128 %E, 64
 /// where the client will ask if E can be computed shifted right by 64-bits. If
 /// this succeeds, getShiftedValue() will be called to produce the value.
-static bool canEvaluateShifted(Value *V, unsigned NumBits, bool IsLeftShift,
-                               InstCombinerImpl &IC, Instruction *CxtI) {
+bool InstCombinerImpl::canEvaluateShifted(Value *V, unsigned NumBits,
+                                          bool IsLeftShift, Instruction *CxtI) {
   // We can always evaluate immediate constants.
   if (match(V, m_ImmConstant()))
     return true;
@@ -603,19 +603,19 @@ static bool canEvaluateShifted(Value *V, unsigned NumBits, bool IsLeftShift,
   case Instruction::Or:
   case Instruction::Xor:
     // Bitwise operators can all arbitrarily be arbitrarily evaluated shifted.
-    return canEvaluateShifted(I->getOperand(0), NumBits, IsLeftShift, IC, I) &&
-           canEvaluateShifted(I->getOperand(1), NumBits, IsLeftShift, IC, I);
+    return canEvaluateShifted(I->getOperand(0), NumBits, IsLeftShift, I) &&
+           canEvaluateShifted(I->getOperand(1), NumBits, IsLeftShift, I);
 
   case Instruction::Shl:
   case Instruction::LShr:
-    return canEvaluateShiftedShift(NumBits, IsLeftShift, I, IC, CxtI);
+    return canEvaluateShiftedShift(NumBits, IsLeftShift, I, *this, CxtI);
 
   case Instruction::Select: {
     SelectInst *SI = cast<SelectInst>(I);
     Value *TrueVal = SI->getTrueValue();
     Value *FalseVal = SI->getFalseValue();
-    return canEvaluateShifted(TrueVal, NumBits, IsLeftShift, IC, SI) &&
-           canEvaluateShifted(FalseVal, NumBits, IsLeftShift, IC, SI);
+    return canEvaluateShifted(TrueVal, NumBits, IsLeftShift, SI) &&
+           canEvaluateShifted(FalseVal, NumBits, IsLeftShift, SI);
   }
   case Instruction::PHI: {
     // We can change a phi if we can change all operands.  Note that we never
@@ -623,7 +623,7 @@ static bool canEvaluateShifted(Value *V, unsigned NumBits, bool IsLeftShift,
     // instructions with a single use.
     PHINode *PN = cast<PHINode>(I);
     for (Value *IncValue : PN->incoming_values())
-      if (!canEvaluateShifted(IncValue, NumBits, IsLeftShift, IC, PN))
+      if (!canEvaluateShifted(IncValue, NumBits, IsLeftShift, PN))
         return false;
     return true;
   }
@@ -633,6 +633,21 @@ static bool canEvaluateShifted(Value *V, unsigned NumBits, bool IsLeftShift,
     return !IsLeftShift && match(I->getOperand(1), m_APInt(MulConst)) &&
            MulConst->isNegatedPowerOf2() && MulConst->countr_zero() == NumBits;
   }
+  case Instruction::Add: {
+    bool CanHandleAdd = false;
+    if (IsLeftShift)
+      CanHandleAdd = cast<BinaryOperator>(I)->hasNoSignedWrap();
+    else {
+      const APInt *C;
+      if (match(I->getOperand(1), m_APIntAllowPoison(C)))
+        CanHandleAdd = C->countr_zero() >= NumBits;
+    }
+    if (CanHandleAdd)
+      return canEvaluateShifted(I->getOperand(0), NumBits, IsLeftShift, I) &&
+             canEvaluateShifted(I->getOperand(1), NumBits, IsLeftShift, I);
+
+    return false;
+  }
   }
 }
 
@@ -701,18 +716,18 @@ static Value *foldShiftedShift(BinaryOperator *InnerShift, unsigned OuterShAmt,
 
 /// When canEvaluateShifted() returns true for an expression, this function
 /// inserts the new computation that produces the shifted value.
-static Value *getShiftedValue(Value *V, unsigned NumBits, bool isLeftShift,
-                              InstCombinerImpl &IC, const DataLayout &DL) {
+Value *InstCombinerImpl::getShiftedValue(Value *V, unsigned NumBits,
+                                         bool isLeftShift) {
   // We can always evaluate constants shifted.
   if (Constant *C = dyn_cast<Constant>(V)) {
     if (isLeftShift)
-      return IC.Builder.CreateShl(C, NumBits);
+      return Builder.CreateShl(C, NumBits);
     else
-      return IC.Builder.CreateLShr(C, NumBits);
+      return Builder.CreateAShr(C, NumBits);
   }
 
   Instruction *I = cast<Instruction>(V);
-  IC.addToWorklist(I);
+  addToWorklist(I);
 
   switch (I->getOpcode()) {
   default: llvm_unreachable("Inconsistency with CanEvaluateShifted");
@@ -720,22 +735,25 @@ static Value *getShiftedValue(Value *V, unsigned NumBits, bool isLeftShift,
   case Instruction::Or:
   case Instruction::Xor:
     // Bitwise operators can all arbitrarily be arbitrarily evaluated shifted.
-    I->setOperand(
-        0, getShiftedValue(I->getOperand(0), NumBits, isLeftShift, IC, DL));
-    I->setOperand(
-        1, getShiftedValue(I->getOperand(1), NumBits, isLeftShift, IC, DL));
+    I->setOperand(0, getShiftedValue(I->getOperand(0), NumBits, isLeftShift));
+    I->setOperand(1, getShiftedValue(I->getOperand(1), NumBits, isLeftShift));
     return I;
 
   case Instruction::Shl:
-  case Instruction::LShr:
+  case Instruction::LShr: {
+    auto *OBO = dyn_cast<OverflowingBinaryOperator>(I);
+
+    if (!isLeftShift && OBO && OBO->hasNoSignedWrap() &&
+        match(I->getOperand(1), m_SpecificInt(NumBits)))
+      return I->getOperand(0);
+
     return foldShiftedShift(cast<BinaryOperator>(I), NumBits, isLeftShift,
-                            IC.Builder);
+                            Builder);
+  }
 
   case Instruction::Select:
-    I->setOperand(
-        1, getShiftedValue(I->getOperand(1), NumBits, isLeftShift, IC, DL));
-    I->setOperand(
-        2, getShiftedValue(I->getOperand(2), NumBits, isLeftShift, IC, DL));
+    I->setOperand(1, getShiftedValue(I->getOperand(1), NumBits, isLeftShift));
+    I->setOperand(2, getShiftedValue(I->getOperand(2), NumBits, isLeftShift));
     return I;
   case Instruction::PHI: {
     // We can change a phi if we can change all operands.  Note that we never
@@ -743,20 +761,26 @@ static Value *getShiftedValue(Value *V, unsigned NumBits, bool isLeftShift,
     // instructions with a single use.
     PHINode *PN = cast<PHINode>(I);
     for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
-      PN->setIncomingValue(i, getShiftedValue(PN->getIncomingValue(i), NumBits,
-                                              isLeftShift, IC, DL));
+      PN->setIncomingValue(
+          i, getShiftedValue(PN->getIncomingValue(i), NumBits, isLeftShift));
     return PN;
   }
   case Instruction::Mul: {
     assert(!isLeftShift && "Unexpected shift direction!");
     auto *Neg = BinaryOperator::CreateNeg(I->getOperand(0));
-    IC.InsertNewInstWith(Neg, I->getIterator());
+    InsertNewInstWith(Neg, I->getIterator());
     unsigned TypeWidth = I->getType()->getScalarSizeInBits();
     APInt Mask = APInt::getLowBitsSet(TypeWidth, TypeWidth - NumBits);
     auto *And = BinaryOperator::CreateAnd(Neg,
                                           ConstantInt::get(I->getType(), Mask));
     And->takeName(I);
-    return IC.InsertNewInstWith(And, I->getIterator());
+    return InsertNewInstWith(And, I->getIterator());
+  }
+  case Instruction::Add: {
+    Value *LHS = getShiftedValue(I->getOperand(0), NumBits, isLeftShift);
+    Value *RHS = getShiftedValue(I->getOperand(1), NumBits, isLeftShift);
+    bool HasNSW = cast<BinaryOperator>(I)->hasNoSignedWrap();
+    return Builder.CreateAdd(LHS, RHS, I->getName(), /*HasNUW=*/false, HasNSW);
   }
   }
 }
@@ -828,14 +852,14 @@ Instruction *InstCombinerImpl::FoldShiftByConstant(Value *Op0, Constant *C1,
   // See if we can propagate this shift into the input, this covers the trivial
   // cast of lshr(shl(x,c1),c2) as well as other more complex cases.
   if (I.getOpcode() != Instruction::AShr &&
-      canEvaluateShifted(Op0, Op1C->getZExtValue(), IsLeftShift, *this, &I)) {
+      canEvaluateShifted(Op0, Op1C->getZExtValue(), IsLeftShift, &I)) {
     LLVM_DEBUG(
         dbgs() << "ICE: GetShiftedValue propagating shift through expression"
                   " to eliminate shift:\n  IN: "
                << *Op0 << "\n  SH: " << I << "\n");
 
     return replaceInstUsesWith(
-        I, getShiftedValue(Op0, Op1C->getZExtValue(), IsLeftShift, *this, DL));
+        I, getShiftedValue(Op0, Op1C->getZExtValue(), IsLeftShift));
   }
 
   if (Instruction *FoldedShift = foldBinOpIntoSelectOrPhi(I))

>From e7613410cc4c1a7983fcd8288c663d8fa6bea00c Mon Sep 17 00:00:00 2001
From: Michael-Chen-NJU <2802328816 at qq.com>
Date: Mon, 12 Jan 2026 01:35:06 +0800
Subject: [PATCH 07/14] update tests

---
 llvm/test/Transforms/InstCombine/apint-shift.ll   |  6 +++---
 .../Transforms/InstCombine/icmp-shl-add-to-add.ll |  2 +-
 llvm/test/Transforms/InstCombine/lshr.ll          | 15 ++++++++-------
 3 files changed, 12 insertions(+), 11 deletions(-)

diff --git a/llvm/test/Transforms/InstCombine/apint-shift.ll b/llvm/test/Transforms/InstCombine/apint-shift.ll
index 4dd0811bb7ecb..d404c5cd344f6 100644
--- a/llvm/test/Transforms/InstCombine/apint-shift.ll
+++ b/llvm/test/Transforms/InstCombine/apint-shift.ll
@@ -538,9 +538,9 @@ define <2 x i43> @lshr_shl_eq_amt_multi_use_splat_vec(<2 x i43> %A) {
 define i37 @test25(i37 %AA, i37 %BB) {
 ; CHECK-LABEL: @test25(
 ; CHECK-NEXT:    [[D:%.*]] = and i37 [[AA:%.*]], -131072
-; CHECK-NEXT:    [[C2:%.*]] = add i37 [[BB:%.*]], [[D]]
-; CHECK-NEXT:    [[F:%.*]] = and i37 [[C2]], -131072
-; CHECK-NEXT:    ret i37 [[F]]
+; CHECK-NEXT:    [[F:%.*]] = and i37 [[C2:%.*]], -131072
+; CHECK-NEXT:    [[E1:%.*]] = add nsw i37 [[F]], [[D]]
+; CHECK-NEXT:    ret i37 [[E1]]
 ;
   %C = lshr i37 %BB, 17
   %D = lshr i37 %AA, 17
diff --git a/llvm/test/Transforms/InstCombine/icmp-shl-add-to-add.ll b/llvm/test/Transforms/InstCombine/icmp-shl-add-to-add.ll
index 52dc3b0f5376a..4b0d61db52b43 100644
--- a/llvm/test/Transforms/InstCombine/icmp-shl-add-to-add.ll
+++ b/llvm/test/Transforms/InstCombine/icmp-shl-add-to-add.ll
@@ -142,7 +142,7 @@ define <2 x i1> @shl_add_const_eq_vec_splat(<2 x i64> %v0, <2 x i64> %v3) {
 ; Test: Vector splat with poison. Should fold once optimization is applied.
 define <2 x i1> @shl_add_const_eq_vec_splat_poison(<2 x i64> %v0, <2 x i64> %v3) {
 ; CHECK-LABEL: @shl_add_const_eq_vec_splat_poison(
-; CHECK-NEXT:    [[V5:%.*]] = add nsw <2 x i64> [[V3:%.*]], splat (i64 1)
+; CHECK-NEXT:    [[V5:%.*]] = add nsw <2 x i64> [[V3:%.*]], <i64 1, i64 poison>
 ; CHECK-NEXT:    [[V6:%.*]] = icmp eq <2 x i64> [[V1:%.*]], [[V5]]
 ; CHECK-NEXT:    ret <2 x i1> [[V6]]
 ;
diff --git a/llvm/test/Transforms/InstCombine/lshr.ll b/llvm/test/Transforms/InstCombine/lshr.ll
index ff358c6bc772a..e085be958d317 100644
--- a/llvm/test/Transforms/InstCombine/lshr.ll
+++ b/llvm/test/Transforms/InstCombine/lshr.ll
@@ -141,9 +141,9 @@ define i8 @lshr_cttz_zero_is_undef_vec(<2 x i8> %x) {
 
 define i8 @lshr_exact(i8 %x) {
 ; CHECK-LABEL: @lshr_exact(
-; CHECK-NEXT:    [[TMP1:%.*]] = add i8 [[X:%.*]], 1
-; CHECK-NEXT:    [[LSHR:%.*]] = and i8 [[TMP1]], 63
-; CHECK-NEXT:    ret i8 [[LSHR]]
+; CHECK-NEXT:    [[LSHR:%.*]] = and i8 [[TMP1:%.*]], 63
+; CHECK-NEXT:    [[ADD1:%.*]] = add nuw nsw i8 [[LSHR]], 1
+; CHECK-NEXT:    ret i8 [[ADD1]]
 ;
   %shl = shl i8 %x, 2
   %add = add i8 %shl, 4
@@ -153,9 +153,9 @@ define i8 @lshr_exact(i8 %x) {
 
 define <2 x i8> @lshr_exact_splat_vec(<2 x i8> %x) {
 ; CHECK-LABEL: @lshr_exact_splat_vec(
-; CHECK-NEXT:    [[TMP1:%.*]] = add <2 x i8> [[X:%.*]], splat (i8 1)
-; CHECK-NEXT:    [[LSHR:%.*]] = and <2 x i8> [[TMP1]], splat (i8 63)
-; CHECK-NEXT:    ret <2 x i8> [[LSHR]]
+; CHECK-NEXT:    [[LSHR:%.*]] = and <2 x i8> [[TMP1:%.*]], splat (i8 63)
+; CHECK-NEXT:    [[ADD1:%.*]] = add nuw nsw <2 x i8> [[LSHR]], splat (i8 1)
+; CHECK-NEXT:    ret <2 x i8> [[ADD1]]
 ;
   %shl = shl <2 x i8> %x, <i8 2, i8 2>
   %add = add <2 x i8> %shl, <i8 4, i8 4>
@@ -165,7 +165,8 @@ define <2 x i8> @lshr_exact_splat_vec(<2 x i8> %x) {
 
 define <2 x i8> @lshr_exact_splat_vec_nuw(<2 x i8> %x) {
 ; CHECK-LABEL: @lshr_exact_splat_vec_nuw(
-; CHECK-NEXT:    [[LSHR:%.*]] = add nuw <2 x i8> [[X:%.*]], splat (i8 1)
+; CHECK-NEXT:    [[SHL:%.*]] = and <2 x i8> [[X:%.*]], splat (i8 63)
+; CHECK-NEXT:    [[LSHR:%.*]] = add nuw nsw <2 x i8> [[SHL]], splat (i8 1)
 ; CHECK-NEXT:    ret <2 x i8> [[LSHR]]
 ;
   %shl = shl nuw <2 x i8> %x, <i8 2, i8 2>

>From 83155398feaf7c8602d9e226cd27c9f30a38547a Mon Sep 17 00:00:00 2001
From: Michael-Chen-NJU <2802328816 at qq.com>
Date: Mon, 12 Jan 2026 03:27:16 +0800
Subject: [PATCH 08/14] Handle Add in getShiftedValue via operand remapping

---
 llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp | 7 +++----
 llvm/test/Transforms/InstCombine/apint-shift.ll       | 2 +-
 2 files changed, 4 insertions(+), 5 deletions(-)

diff --git a/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp b/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
index d7624be48314a..4dadaa842e9cc 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
@@ -777,10 +777,9 @@ Value *InstCombinerImpl::getShiftedValue(Value *V, unsigned NumBits,
     return InsertNewInstWith(And, I->getIterator());
   }
   case Instruction::Add: {
-    Value *LHS = getShiftedValue(I->getOperand(0), NumBits, isLeftShift);
-    Value *RHS = getShiftedValue(I->getOperand(1), NumBits, isLeftShift);
-    bool HasNSW = cast<BinaryOperator>(I)->hasNoSignedWrap();
-    return Builder.CreateAdd(LHS, RHS, I->getName(), /*HasNUW=*/false, HasNSW);
+    I->setOperand(0, getShiftedValue(I->getOperand(0), NumBits, isLeftShift));
+    I->setOperand(1, getShiftedValue(I->getOperand(1), NumBits, isLeftShift));
+    return I;
   }
   }
 }
diff --git a/llvm/test/Transforms/InstCombine/apint-shift.ll b/llvm/test/Transforms/InstCombine/apint-shift.ll
index d404c5cd344f6..ed6f7a1ffab01 100644
--- a/llvm/test/Transforms/InstCombine/apint-shift.ll
+++ b/llvm/test/Transforms/InstCombine/apint-shift.ll
@@ -539,7 +539,7 @@ define i37 @test25(i37 %AA, i37 %BB) {
 ; CHECK-LABEL: @test25(
 ; CHECK-NEXT:    [[D:%.*]] = and i37 [[AA:%.*]], -131072
 ; CHECK-NEXT:    [[F:%.*]] = and i37 [[C2:%.*]], -131072
-; CHECK-NEXT:    [[E1:%.*]] = add nsw i37 [[F]], [[D]]
+; CHECK-NEXT:    [[E1:%.*]] = add nuw nsw i37 [[F]], [[D]]
 ; CHECK-NEXT:    ret i37 [[E1]]
 ;
   %C = lshr i37 %BB, 17

>From 62dee0d20771f0ce80c1b46a227dbcb322ca97da Mon Sep 17 00:00:00 2001
From: Michael-Chen-NJU <2802328816 at qq.com>
Date: Sat, 24 Jan 2026 17:40:10 +0800
Subject: [PATCH 09/14] [InstCombine]Improved icmp(shl, add(shl, K)) folding by
 supporting both NSW/AShr and NUW/LShr paths

---
 .../InstCombine/InstCombineCompares.cpp       | 38 ++++++----
 .../InstCombine/InstCombineInternal.h         |  4 +-
 .../InstCombine/InstCombineShifts.cpp         | 76 +++++++++++--------
 .../Transforms/InstCombine/icmp-select.ll     |  2 +-
 .../InstCombine/icmp-shl-add-to-add.ll        | 42 ++++++++++
 llvm/test/Transforms/InstCombine/lshr.ll      |  7 +-
 6 files changed, 115 insertions(+), 54 deletions(-)

diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
index e8327421973b0..6fdb56ca6fef1 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -6023,21 +6023,6 @@ Instruction *InstCombinerImpl::foldICmpEquality(ICmpInst &I) {
 
   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
   const CmpInst::Predicate Pred = I.getPredicate();
-
-  // icmp (shl nsw X, L), (add nsw (shl nsw Y, L), K) where K is a multiple of
-  // 2^L
-  // -> icmp X, (add nsw Y, K / 2^L)
-  Value *X;
-  const APInt *CShAmt;
-  if (match(Op0, m_NSWShl(m_Value(X), m_APIntAllowPoison(CShAmt)))) {
-    unsigned ShAmt = CShAmt->getZExtValue();
-    if (canEvaluateShifted(Op1, ShAmt, false, &I)) {
-      Value *NewOp0 = X;
-      Value *NewOp1 = getShiftedValue(Op1, ShAmt, false);
-      return new ICmpInst(Pred, NewOp0, NewOp1);
-    }
-  }
-
   Value *A, *B, *C, *D;
   if (match(Op0, m_Xor(m_Value(A), m_Value(B)))) {
     if (A == Op1 || B == Op1) { // (A^B) == A  ->  B == 0
@@ -7644,6 +7629,29 @@ Instruction *InstCombinerImpl::foldICmpCommutative(CmpPredicate Pred,
     }
   }
 
+  // icmp (shl nsw/nuw X, L), (add nsw/nuw (shl nsw/nuw Y, L), K)
+  //   -> icmp X, (add nsw/nuw Y, K >> L)
+  // We use AShr for nsw and LShr for nuw to safely peel off the shift.
+  Value *X;
+  const APInt *CShAmt;
+  unsigned ShAmt;
+  if (match(Op0, m_NSWShl(m_Value(X), m_APIntAllowPoison(CShAmt))) &&
+      !CxtI.isUnsigned()) {
+    ShAmt = CShAmt->getZExtValue();
+    if (canEvaluateShifted(Op1, ShAmt, Instruction::AShr, &CxtI)) {
+      Value *NewOp1 = getShiftedValue(Op1, ShAmt, Instruction::AShr);
+      return new ICmpInst(Pred, X, NewOp1);
+    }
+  }
+
+  if (match(Op0, m_NUWShl(m_Value(X), m_APIntAllowPoison(CShAmt))) &&
+      !CxtI.isSigned()) {
+    ShAmt = CShAmt->getZExtValue();
+    if (canEvaluateShifted(Op1, ShAmt, Instruction::LShr, &CxtI)) {
+      Value *NewOp1 = getShiftedValue(Op1, ShAmt, Instruction::LShr);
+      return new ICmpInst(Pred, X, NewOp1);
+    }
+  }
   return nullptr;
 }
 
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineInternal.h b/llvm/lib/Transforms/InstCombine/InstCombineInternal.h
index dd4138b55421f..985a235b4aa40 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineInternal.h
+++ b/llvm/lib/Transforms/InstCombine/InstCombineInternal.h
@@ -448,9 +448,9 @@ class LLVM_LIBRARY_VISIBILITY InstCombinerImpl final
                               bool InvertFalseVal = false);
   Value *getSelectCondition(Value *A, Value *B, bool ABIsTheSame);
 
-  bool canEvaluateShifted(Value *V, unsigned NumBits, bool IsLeftShift,
+  bool canEvaluateShifted(Value *V, unsigned NumBits, unsigned ShiftOp,
                           Instruction *CxtI);
-  Value *getShiftedValue(Value *V, unsigned NumBits, bool isLeftShift);
+  Value *getShiftedValue(Value *V, unsigned NumBits, unsigned ShiftOp);
 
   Instruction *foldLShrOverflowBit(BinaryOperator &I);
   Instruction *foldExtractOfOverflowIntrinsic(ExtractValueInst &EV);
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp b/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
index 4dadaa842e9cc..a555b1fad64d4 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
@@ -585,10 +585,17 @@ static bool canEvaluateShiftedShift(unsigned OuterShAmt, bool IsOuterShl,
 /// where the client will ask if E can be computed shifted right by 64-bits. If
 /// this succeeds, getShiftedValue() will be called to produce the value.
 bool InstCombinerImpl::canEvaluateShifted(Value *V, unsigned NumBits,
-                                          bool IsLeftShift, Instruction *CxtI) {
-  // We can always evaluate immediate constants.
-  if (match(V, m_ImmConstant()))
-    return true;
+                                          unsigned ShiftOp, Instruction *CxtI) {
+  // We can always evaluate immediate constants shifted left. For right shifts,
+  // the constant must be a multiple of 2^NumBits to avoid losing information.
+  if (match(V, m_ImmConstant())) {
+    if (ShiftOp == Instruction::Shl)
+      return true;
+    const APInt *C;
+    if (match(V, m_APIntAllowPoison(C)))
+      return C->countr_zero() >= NumBits;
+    return false;
+  }
 
   Instruction *I = dyn_cast<Instruction>(V);
   if (!I) return false;
@@ -603,19 +610,20 @@ bool InstCombinerImpl::canEvaluateShifted(Value *V, unsigned NumBits,
   case Instruction::Or:
   case Instruction::Xor:
     // Bitwise operators can all arbitrarily be arbitrarily evaluated shifted.
-    return canEvaluateShifted(I->getOperand(0), NumBits, IsLeftShift, I) &&
-           canEvaluateShifted(I->getOperand(1), NumBits, IsLeftShift, I);
+    return canEvaluateShifted(I->getOperand(0), NumBits, ShiftOp, I) &&
+           canEvaluateShifted(I->getOperand(1), NumBits, ShiftOp, I);
 
   case Instruction::Shl:
   case Instruction::LShr:
-    return canEvaluateShiftedShift(NumBits, IsLeftShift, I, *this, CxtI);
+    return canEvaluateShiftedShift(NumBits, ShiftOp == Instruction::Shl, I,
+                                   *this, CxtI);
 
   case Instruction::Select: {
     SelectInst *SI = cast<SelectInst>(I);
     Value *TrueVal = SI->getTrueValue();
     Value *FalseVal = SI->getFalseValue();
-    return canEvaluateShifted(TrueVal, NumBits, IsLeftShift, SI) &&
-           canEvaluateShifted(FalseVal, NumBits, IsLeftShift, SI);
+    return canEvaluateShifted(TrueVal, NumBits, ShiftOp, SI) &&
+           canEvaluateShifted(FalseVal, NumBits, ShiftOp, SI);
   }
   case Instruction::PHI: {
     // We can change a phi if we can change all operands.  Note that we never
@@ -623,19 +631,20 @@ bool InstCombinerImpl::canEvaluateShifted(Value *V, unsigned NumBits,
     // instructions with a single use.
     PHINode *PN = cast<PHINode>(I);
     for (Value *IncValue : PN->incoming_values())
-      if (!canEvaluateShifted(IncValue, NumBits, IsLeftShift, PN))
+      if (!canEvaluateShifted(IncValue, NumBits, ShiftOp, PN))
         return false;
     return true;
   }
   case Instruction::Mul: {
     const APInt *MulConst;
     // We can fold (shr (mul X, -(1 << C)), C) -> (and (neg X), C`)
-    return !IsLeftShift && match(I->getOperand(1), m_APInt(MulConst)) &&
+    return ShiftOp != Instruction::Shl &&
+           match(I->getOperand(1), m_APInt(MulConst)) &&
            MulConst->isNegatedPowerOf2() && MulConst->countr_zero() == NumBits;
   }
   case Instruction::Add: {
     bool CanHandleAdd = false;
-    if (IsLeftShift)
+    if (ShiftOp == Instruction::Shl)
       CanHandleAdd = cast<BinaryOperator>(I)->hasNoSignedWrap();
     else {
       const APInt *C;
@@ -643,8 +652,8 @@ bool InstCombinerImpl::canEvaluateShifted(Value *V, unsigned NumBits,
         CanHandleAdd = C->countr_zero() >= NumBits;
     }
     if (CanHandleAdd)
-      return canEvaluateShifted(I->getOperand(0), NumBits, IsLeftShift, I) &&
-             canEvaluateShifted(I->getOperand(1), NumBits, IsLeftShift, I);
+      return canEvaluateShifted(I->getOperand(0), NumBits, ShiftOp, I) &&
+             canEvaluateShifted(I->getOperand(1), NumBits, ShiftOp, I);
 
     return false;
   }
@@ -717,13 +726,14 @@ static Value *foldShiftedShift(BinaryOperator *InnerShift, unsigned OuterShAmt,
 /// When canEvaluateShifted() returns true for an expression, this function
 /// inserts the new computation that produces the shifted value.
 Value *InstCombinerImpl::getShiftedValue(Value *V, unsigned NumBits,
-                                         bool isLeftShift) {
+                                         unsigned ShiftOp) {
   // We can always evaluate constants shifted.
   if (Constant *C = dyn_cast<Constant>(V)) {
-    if (isLeftShift)
+    if (ShiftOp == Instruction::Shl)
       return Builder.CreateShl(C, NumBits);
     else
-      return Builder.CreateAShr(C, NumBits);
+      return Builder.CreateBinOp((Instruction::BinaryOps)ShiftOp, C,
+                                 ConstantInt::get(C->getType(), NumBits));
   }
 
   Instruction *I = cast<Instruction>(V);
@@ -735,25 +745,27 @@ Value *InstCombinerImpl::getShiftedValue(Value *V, unsigned NumBits,
   case Instruction::Or:
   case Instruction::Xor:
     // Bitwise operators can all arbitrarily be arbitrarily evaluated shifted.
-    I->setOperand(0, getShiftedValue(I->getOperand(0), NumBits, isLeftShift));
-    I->setOperand(1, getShiftedValue(I->getOperand(1), NumBits, isLeftShift));
+    I->setOperand(0, getShiftedValue(I->getOperand(0), NumBits, ShiftOp));
+    I->setOperand(1, getShiftedValue(I->getOperand(1), NumBits, ShiftOp));
     return I;
 
   case Instruction::Shl:
   case Instruction::LShr: {
     auto *OBO = dyn_cast<OverflowingBinaryOperator>(I);
 
-    if (!isLeftShift && OBO && OBO->hasNoSignedWrap() &&
-        match(I->getOperand(1), m_SpecificInt(NumBits)))
-      return I->getOperand(0);
+    if (OBO && match(I->getOperand(1), m_SpecificInt(NumBits))) {
+      if ((ShiftOp == Instruction::AShr && OBO->hasNoSignedWrap()) ||
+          (ShiftOp == Instruction::LShr && OBO->hasNoUnsignedWrap()))
+        return I->getOperand(0);
+    }
 
-    return foldShiftedShift(cast<BinaryOperator>(I), NumBits, isLeftShift,
-                            Builder);
+    return foldShiftedShift(cast<BinaryOperator>(I), NumBits,
+                            ShiftOp == Instruction::Shl, Builder);
   }
 
   case Instruction::Select:
-    I->setOperand(1, getShiftedValue(I->getOperand(1), NumBits, isLeftShift));
-    I->setOperand(2, getShiftedValue(I->getOperand(2), NumBits, isLeftShift));
+    I->setOperand(1, getShiftedValue(I->getOperand(1), NumBits, ShiftOp));
+    I->setOperand(2, getShiftedValue(I->getOperand(2), NumBits, ShiftOp));
     return I;
   case Instruction::PHI: {
     // We can change a phi if we can change all operands.  Note that we never
@@ -762,11 +774,11 @@ Value *InstCombinerImpl::getShiftedValue(Value *V, unsigned NumBits,
     PHINode *PN = cast<PHINode>(I);
     for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
       PN->setIncomingValue(
-          i, getShiftedValue(PN->getIncomingValue(i), NumBits, isLeftShift));
+          i, getShiftedValue(PN->getIncomingValue(i), NumBits, ShiftOp));
     return PN;
   }
   case Instruction::Mul: {
-    assert(!isLeftShift && "Unexpected shift direction!");
+    assert(ShiftOp != Instruction::Shl && "Unexpected shift direction!");
     auto *Neg = BinaryOperator::CreateNeg(I->getOperand(0));
     InsertNewInstWith(Neg, I->getIterator());
     unsigned TypeWidth = I->getType()->getScalarSizeInBits();
@@ -777,8 +789,8 @@ Value *InstCombinerImpl::getShiftedValue(Value *V, unsigned NumBits,
     return InsertNewInstWith(And, I->getIterator());
   }
   case Instruction::Add: {
-    I->setOperand(0, getShiftedValue(I->getOperand(0), NumBits, isLeftShift));
-    I->setOperand(1, getShiftedValue(I->getOperand(1), NumBits, isLeftShift));
+    I->setOperand(0, getShiftedValue(I->getOperand(0), NumBits, ShiftOp));
+    I->setOperand(1, getShiftedValue(I->getOperand(1), NumBits, ShiftOp));
     return I;
   }
   }
@@ -851,14 +863,14 @@ Instruction *InstCombinerImpl::FoldShiftByConstant(Value *Op0, Constant *C1,
   // See if we can propagate this shift into the input, this covers the trivial
   // cast of lshr(shl(x,c1),c2) as well as other more complex cases.
   if (I.getOpcode() != Instruction::AShr &&
-      canEvaluateShifted(Op0, Op1C->getZExtValue(), IsLeftShift, &I)) {
+      canEvaluateShifted(Op0, Op1C->getZExtValue(), I.getOpcode(), &I)) {
     LLVM_DEBUG(
         dbgs() << "ICE: GetShiftedValue propagating shift through expression"
                   " to eliminate shift:\n  IN: "
                << *Op0 << "\n  SH: " << I << "\n");
 
     return replaceInstUsesWith(
-        I, getShiftedValue(Op0, Op1C->getZExtValue(), IsLeftShift));
+        I, getShiftedValue(Op0, Op1C->getZExtValue(), I.getOpcode()));
   }
 
   if (Instruction *FoldedShift = foldBinOpIntoSelectOrPhi(I))
diff --git a/llvm/test/Transforms/InstCombine/icmp-select.ll b/llvm/test/Transforms/InstCombine/icmp-select.ll
index c29527a3c3c5e..e571d46d621ca 100644
--- a/llvm/test/Transforms/InstCombine/icmp-select.ll
+++ b/llvm/test/Transforms/InstCombine/icmp-select.ll
@@ -914,7 +914,7 @@ define i1 @shl_failed_to_simplify(i8 %a, i1 %cond) {
 define i1 @shl_nuw_ne(i8 %a, i8 %b, i8 %c, i1 %cond) {
 ; CHECK-LABEL: @shl_nuw_ne(
 ; CHECK-NEXT:    [[TMP1:%.*]] = select i1 [[COND:%.*]], i8 [[B:%.*]], i8 4
-; CHECK-NEXT:    [[CMP:%.*]] = icmp ne i8 [[TMP1]], [[A:%.*]]
+; CHECK-NEXT:    [[CMP:%.*]] = icmp ne i8 [[A:%.*]], [[TMP1]]
 ; CHECK-NEXT:    ret i1 [[CMP]]
 ;
   %shl_a = shl nuw i8 %a, 3
diff --git a/llvm/test/Transforms/InstCombine/icmp-shl-add-to-add.ll b/llvm/test/Transforms/InstCombine/icmp-shl-add-to-add.ll
index 4b0d61db52b43..7c8042ff38ded 100644
--- a/llvm/test/Transforms/InstCombine/icmp-shl-add-to-add.ll
+++ b/llvm/test/Transforms/InstCombine/icmp-shl-add-to-add.ll
@@ -168,3 +168,45 @@ define <2 x i1> @shl_add_const_eq_vec_non_splat(<2 x i64> %v0, <2 x i64> %v3) {
   %v6 = icmp eq <2 x i64> %v1, %v5
   ret <2 x i1> %v6
 }
+
+; Test: Commutative (shl on the right side).
+define i1 @shl_add_const_eq_commutative(i64 %v0, i64 %v3) {
+; CHECK-LABEL: @shl_add_const_eq_commutative(
+; CHECK-NEXT:    [[V5:%.*]] = add nsw i64 [[V3:%.*]], 1
+; CHECK-NEXT:    [[V6:%.*]] = icmp eq i64 [[V0:%.*]], [[V5]]
+; CHECK-NEXT:    ret i1 [[V6]]
+;
+  %v1 = shl nsw i64 %v0, 5
+  %v4 = shl nsw i64 %v3, 5
+  %v5 = add nsw i64 %v4, 32
+  %v6 = icmp eq i64 %v5, %v1
+  ret i1 %v6
+}
+
+; Test: Variable shift amount with nuw (Logical)
+define i1 @icmp_shl_var_amount_nuw(i32 %x, i32 %y) {
+; CHECK-LABEL: @icmp_shl_var_amount_nuw(
+; CHECK-NEXT:    [[Y:%.*]] = add nuw i32 [[Y1:%.*]], 1
+; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i32 [[X:%.*]], [[Y]]
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %shlx = shl nuw i32 %x, 5
+  %shly = shl nuw i32 %y, 5
+  %add = add nuw i32 %shly, 32
+  %cmp = icmp eq i32 %shlx, %add
+  ret i1 %cmp
+}
+
+; Test: Variable shift amount with nsw (Arithmetic)
+define i1 @icmp_shl_var_amount_nsw(i32 %x, i32 %y) {
+; CHECK-LABEL: @icmp_shl_var_amount_nsw(
+; CHECK-NEXT:    [[Y:%.*]] = add nsw i32 [[Y1:%.*]], 1
+; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i32 [[X:%.*]], [[Y]]
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %shlx = shl nsw i32 %x, 5
+  %shly = shl nsw i32 %y, 5
+  %add = add nsw i32 %shly, 32
+  %cmp = icmp eq i32 %shlx, %add
+  ret i1 %cmp
+}
diff --git a/llvm/test/Transforms/InstCombine/lshr.ll b/llvm/test/Transforms/InstCombine/lshr.ll
index e085be958d317..4af532ae8d1d3 100644
--- a/llvm/test/Transforms/InstCombine/lshr.ll
+++ b/llvm/test/Transforms/InstCombine/lshr.ll
@@ -154,8 +154,8 @@ define i8 @lshr_exact(i8 %x) {
 define <2 x i8> @lshr_exact_splat_vec(<2 x i8> %x) {
 ; CHECK-LABEL: @lshr_exact_splat_vec(
 ; CHECK-NEXT:    [[LSHR:%.*]] = and <2 x i8> [[TMP1:%.*]], splat (i8 63)
-; CHECK-NEXT:    [[ADD1:%.*]] = add nuw nsw <2 x i8> [[LSHR]], splat (i8 1)
-; CHECK-NEXT:    ret <2 x i8> [[ADD1]]
+; CHECK-NEXT:    [[ADD:%.*]] = add nuw nsw <2 x i8> [[LSHR]], splat (i8 1)
+; CHECK-NEXT:    ret <2 x i8> [[ADD]]
 ;
   %shl = shl <2 x i8> %x, <i8 2, i8 2>
   %add = add <2 x i8> %shl, <i8 4, i8 4>
@@ -165,8 +165,7 @@ define <2 x i8> @lshr_exact_splat_vec(<2 x i8> %x) {
 
 define <2 x i8> @lshr_exact_splat_vec_nuw(<2 x i8> %x) {
 ; CHECK-LABEL: @lshr_exact_splat_vec_nuw(
-; CHECK-NEXT:    [[SHL:%.*]] = and <2 x i8> [[X:%.*]], splat (i8 63)
-; CHECK-NEXT:    [[LSHR:%.*]] = add nuw nsw <2 x i8> [[SHL]], splat (i8 1)
+; CHECK-NEXT:    [[LSHR:%.*]] = add nuw <2 x i8> [[X:%.*]], splat (i8 1)
 ; CHECK-NEXT:    ret <2 x i8> [[LSHR]]
 ;
   %shl = shl nuw <2 x i8> %x, <i8 2, i8 2>

>From 388a635ac5956bfbd0606d5e5f3d3d6534d62246 Mon Sep 17 00:00:00 2001
From: Michael-Chen-NJU <2802328816 at qq.com>
Date: Wed, 28 Jan 2026 20:08:41 +0800
Subject: [PATCH 10/14] [InstCombine] Consolidate shift evaluation logic and
 fix poison flag propagation

---
 .../InstCombine/InstCombineCompares.cpp       | 25 +++----
 .../InstCombine/InstCombineInternal.h         |  7 +-
 .../InstCombine/InstCombineShifts.cpp         | 52 ++++++--------
 .../Transforms/InstCombine/apint-shift.ll     |  2 +-
 .../InstCombine/icmp-shl-add-to-add.ll        | 68 +++++++++++++++++++
 llvm/test/Transforms/InstCombine/lshr.ll      |  4 +-
 6 files changed, 107 insertions(+), 51 deletions(-)

diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
index 6fdb56ca6fef1..db9417e0b01ba 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -7633,22 +7633,23 @@ Instruction *InstCombinerImpl::foldICmpCommutative(CmpPredicate Pred,
   //   -> icmp X, (add nsw/nuw Y, K >> L)
   // We use AShr for nsw and LShr for nuw to safely peel off the shift.
   Value *X;
-  const APInt *CShAmt;
-  unsigned ShAmt;
-  if (match(Op0, m_NSWShl(m_Value(X), m_APIntAllowPoison(CShAmt))) &&
-      !CxtI.isUnsigned()) {
-    ShAmt = CShAmt->getZExtValue();
-    if (canEvaluateShifted(Op1, ShAmt, Instruction::AShr, &CxtI)) {
-      Value *NewOp1 = getShiftedValue(Op1, ShAmt, Instruction::AShr);
+  uint64_t ShAmt;
+  if (match(Op0, m_NUWShl(m_Value(X), m_ConstantInt(ShAmt))) &&
+      !CxtI.isSigned()) {
+    if (ShAmt >= X->getType()->getScalarSizeInBits())
+      return nullptr;
+    if (canEvaluateShifted(Op1, ShAmt, Instruction::LShr, &CxtI)) {
+      Value *NewOp1 = getShiftedValue(Op1, ShAmt, Instruction::LShr);
       return new ICmpInst(Pred, X, NewOp1);
     }
   }
 
-  if (match(Op0, m_NUWShl(m_Value(X), m_APIntAllowPoison(CShAmt))) &&
-      !CxtI.isSigned()) {
-    ShAmt = CShAmt->getZExtValue();
-    if (canEvaluateShifted(Op1, ShAmt, Instruction::LShr, &CxtI)) {
-      Value *NewOp1 = getShiftedValue(Op1, ShAmt, Instruction::LShr);
+  if (match(Op0, m_NSWShl(m_Value(X), m_ConstantInt(ShAmt))) &&
+      !CxtI.isUnsigned()) {
+    if (ShAmt >= X->getType()->getScalarSizeInBits())
+      return nullptr;
+    if (canEvaluateShifted(Op1, ShAmt, Instruction::AShr, &CxtI)) {
+      Value *NewOp1 = getShiftedValue(Op1, ShAmt, Instruction::AShr);
       return new ICmpInst(Pred, X, NewOp1);
     }
   }
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineInternal.h b/llvm/lib/Transforms/InstCombine/InstCombineInternal.h
index 985a235b4aa40..7f5eb832ba44e 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineInternal.h
+++ b/llvm/lib/Transforms/InstCombine/InstCombineInternal.h
@@ -448,9 +448,10 @@ class LLVM_LIBRARY_VISIBILITY InstCombinerImpl final
                               bool InvertFalseVal = false);
   Value *getSelectCondition(Value *A, Value *B, bool ABIsTheSame);
 
-  bool canEvaluateShifted(Value *V, unsigned NumBits, unsigned ShiftOp,
-                          Instruction *CxtI);
-  Value *getShiftedValue(Value *V, unsigned NumBits, unsigned ShiftOp);
+  bool canEvaluateShifted(Value *V, unsigned NumBits,
+                          Instruction::BinaryOps ShiftOp, Instruction *CxtI);
+  Value *getShiftedValue(Value *V, unsigned NumBits,
+                         Instruction::BinaryOps ShiftOp);
 
   Instruction *foldLShrOverflowBit(BinaryOperator &I);
   Instruction *foldExtractOfOverflowIntrinsic(ExtractValueInst &EV);
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp b/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
index a555b1fad64d4..4b7903876f424 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
@@ -585,7 +585,8 @@ static bool canEvaluateShiftedShift(unsigned OuterShAmt, bool IsOuterShl,
 /// where the client will ask if E can be computed shifted right by 64-bits. If
 /// this succeeds, getShiftedValue() will be called to produce the value.
 bool InstCombinerImpl::canEvaluateShifted(Value *V, unsigned NumBits,
-                                          unsigned ShiftOp, Instruction *CxtI) {
+                                          Instruction::BinaryOps ShiftOp,
+                                          Instruction *CxtI) {
   // We can always evaluate immediate constants shifted left. For right shifts,
   // the constant must be a multiple of 2^NumBits to avoid losing information.
   if (match(V, m_ImmConstant())) {
@@ -643,19 +644,8 @@ bool InstCombinerImpl::canEvaluateShifted(Value *V, unsigned NumBits,
            MulConst->isNegatedPowerOf2() && MulConst->countr_zero() == NumBits;
   }
   case Instruction::Add: {
-    bool CanHandleAdd = false;
-    if (ShiftOp == Instruction::Shl)
-      CanHandleAdd = cast<BinaryOperator>(I)->hasNoSignedWrap();
-    else {
-      const APInt *C;
-      if (match(I->getOperand(1), m_APIntAllowPoison(C)))
-        CanHandleAdd = C->countr_zero() >= NumBits;
-    }
-    if (CanHandleAdd)
-      return canEvaluateShifted(I->getOperand(0), NumBits, ShiftOp, I) &&
-             canEvaluateShifted(I->getOperand(1), NumBits, ShiftOp, I);
-
-    return false;
+    return canEvaluateShifted(I->getOperand(0), NumBits, ShiftOp, I) &&
+           canEvaluateShifted(I->getOperand(1), NumBits, ShiftOp, I);
   }
   }
 }
@@ -663,9 +653,10 @@ bool InstCombinerImpl::canEvaluateShifted(Value *V, unsigned NumBits,
 /// Fold OuterShift (InnerShift X, C1), C2.
 /// See canEvaluateShiftedShift() for the constraints on these instructions.
 static Value *foldShiftedShift(BinaryOperator *InnerShift, unsigned OuterShAmt,
-                               bool IsOuterShl,
+                               Instruction::BinaryOps OuterOpc,
                                InstCombiner::BuilderTy &Builder) {
   bool IsInnerShl = InnerShift->getOpcode() == Instruction::Shl;
+  bool IsOuterShl = OuterOpc == Instruction::Shl;
   Type *ShType = InnerShift->getType();
   unsigned TypeWidth = ShType->getScalarSizeInBits();
 
@@ -701,6 +692,10 @@ static Value *foldShiftedShift(BinaryOperator *InnerShift, unsigned OuterShAmt,
   // lshr (shl X, C), C --> and X, C'
   // shl (lshr X, C), C --> and X, C'
   if (InnerShAmt == OuterShAmt) {
+    if (IsInnerShl &&
+        ((OuterOpc == Instruction::AShr && InnerShift->hasNoSignedWrap()) ||
+         (OuterOpc == Instruction::LShr && InnerShift->hasNoUnsignedWrap())))
+      return InnerShift->getOperand(0);
     APInt Mask = IsInnerShl
                      ? APInt::getLowBitsSet(TypeWidth, TypeWidth - OuterShAmt)
                      : APInt::getHighBitsSet(TypeWidth, TypeWidth - OuterShAmt);
@@ -726,14 +721,11 @@ static Value *foldShiftedShift(BinaryOperator *InnerShift, unsigned OuterShAmt,
 /// When canEvaluateShifted() returns true for an expression, this function
 /// inserts the new computation that produces the shifted value.
 Value *InstCombinerImpl::getShiftedValue(Value *V, unsigned NumBits,
-                                         unsigned ShiftOp) {
+                                         Instruction::BinaryOps ShiftOp) {
   // We can always evaluate constants shifted.
   if (Constant *C = dyn_cast<Constant>(V)) {
-    if (ShiftOp == Instruction::Shl)
-      return Builder.CreateShl(C, NumBits);
-    else
-      return Builder.CreateBinOp((Instruction::BinaryOps)ShiftOp, C,
-                                 ConstantInt::get(C->getType(), NumBits));
+    return Builder.CreateBinOp((Instruction::BinaryOps)ShiftOp, C,
+                               ConstantInt::get(C->getType(), NumBits));
   }
 
   Instruction *I = cast<Instruction>(V);
@@ -750,18 +742,8 @@ Value *InstCombinerImpl::getShiftedValue(Value *V, unsigned NumBits,
     return I;
 
   case Instruction::Shl:
-  case Instruction::LShr: {
-    auto *OBO = dyn_cast<OverflowingBinaryOperator>(I);
-
-    if (OBO && match(I->getOperand(1), m_SpecificInt(NumBits))) {
-      if ((ShiftOp == Instruction::AShr && OBO->hasNoSignedWrap()) ||
-          (ShiftOp == Instruction::LShr && OBO->hasNoUnsignedWrap()))
-        return I->getOperand(0);
-    }
-
-    return foldShiftedShift(cast<BinaryOperator>(I), NumBits,
-                            ShiftOp == Instruction::Shl, Builder);
-  }
+  case Instruction::LShr:
+    return foldShiftedShift(cast<BinaryOperator>(I), NumBits, ShiftOp, Builder);
 
   case Instruction::Select:
     I->setOperand(1, getShiftedValue(I->getOperand(1), NumBits, ShiftOp));
@@ -789,6 +771,10 @@ Value *InstCombinerImpl::getShiftedValue(Value *V, unsigned NumBits,
     return InsertNewInstWith(And, I->getIterator());
   }
   case Instruction::Add: {
+    if (ShiftOp == Instruction::Shl) {
+      I->setHasNoUnsignedWrap(false);
+      I->setHasNoSignedWrap(false);
+    }
     I->setOperand(0, getShiftedValue(I->getOperand(0), NumBits, ShiftOp));
     I->setOperand(1, getShiftedValue(I->getOperand(1), NumBits, ShiftOp));
     return I;
diff --git a/llvm/test/Transforms/InstCombine/apint-shift.ll b/llvm/test/Transforms/InstCombine/apint-shift.ll
index ed6f7a1ffab01..a2d9159e58e88 100644
--- a/llvm/test/Transforms/InstCombine/apint-shift.ll
+++ b/llvm/test/Transforms/InstCombine/apint-shift.ll
@@ -539,7 +539,7 @@ define i37 @test25(i37 %AA, i37 %BB) {
 ; CHECK-LABEL: @test25(
 ; CHECK-NEXT:    [[D:%.*]] = and i37 [[AA:%.*]], -131072
 ; CHECK-NEXT:    [[F:%.*]] = and i37 [[C2:%.*]], -131072
-; CHECK-NEXT:    [[E1:%.*]] = add nuw nsw i37 [[F]], [[D]]
+; CHECK-NEXT:    [[E1:%.*]] = add i37 [[F]], [[D]]
 ; CHECK-NEXT:    ret i37 [[E1]]
 ;
   %C = lshr i37 %BB, 17
diff --git a/llvm/test/Transforms/InstCombine/icmp-shl-add-to-add.ll b/llvm/test/Transforms/InstCombine/icmp-shl-add-to-add.ll
index 7c8042ff38ded..7b8d01866fc96 100644
--- a/llvm/test/Transforms/InstCombine/icmp-shl-add-to-add.ll
+++ b/llvm/test/Transforms/InstCombine/icmp-shl-add-to-add.ll
@@ -210,3 +210,71 @@ define i1 @icmp_shl_var_amount_nsw(i32 %x, i32 %y) {
   %cmp = icmp eq i32 %shlx, %add
   ret i1 %cmp
 }
+
+; Test: ult (Unsigned Less Than) with nuw
+define i1 @icmp_shl_nuw_ult(i32 %x, i32 %y) {
+; CHECK-LABEL: @icmp_shl_nuw_ult(
+; CHECK-NEXT:    [[CMP:%.*]] = icmp ule i32 [[X:%.*]], [[Y:%.*]]
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %shlx = shl nuw i32 %x, 5
+  %shly = shl nuw i32 %y, 5
+  %add = add nuw i32 %shly, 32
+  %cmp = icmp ult i32 %shlx, %add
+  ret i1 %cmp
+}
+
+; Test: ugt (Unsigned Greater Than) with nuw
+define i1 @icmp_shl_nuw_ugt(i32 %x, i32 %y) {
+; CHECK-LABEL: @icmp_shl_nuw_ugt(
+; CHECK-NEXT:    [[TMP1:%.*]] = add nuw i32 [[Y:%.*]], 1
+; CHECK-NEXT:    [[CMP:%.*]] = icmp ugt i32 [[X:%.*]], [[TMP1]]
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %shlx = shl nuw i32 %x, 5
+  %shly = shl nuw i32 %y, 5
+  %add = add nuw i32 %shly, 32
+  %cmp = icmp ugt i32 %shlx, %add
+  ret i1 %cmp
+}
+
+; Test: slt (Signed Less Than) with nsw
+define i1 @icmp_shl_nsw_slt(i32 %x, i32 %y) {
+; CHECK-LABEL: @icmp_shl_nsw_slt(
+; CHECK-NEXT:    [[CMP:%.*]] = icmp sle i32 [[X:%.*]], [[Y:%.*]]
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %shlx = shl nsw i32 %x, 5
+  %shly = shl nsw i32 %y, 5
+  %add = add nsw i32 %shly, 32
+  %cmp = icmp slt i32 %shlx, %add
+  ret i1 %cmp
+}
+
+; Test: sgt (Signed Greater Than) with nsw
+define i1 @icmp_shl_nsw_sgt(i32 %x, i32 %y) {
+; CHECK-LABEL: @icmp_shl_nsw_sgt(
+; CHECK-NEXT:    [[TMP1:%.*]] = add nsw i32 [[Y:%.*]], 1
+; CHECK-NEXT:    [[CMP:%.*]] = icmp sgt i32 [[X:%.*]], [[TMP1]]
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %shlx = shl nsw i32 %x, 5
+  %shly = shl nsw i32 %y, 5
+  %add = add nsw i32 %shly, 32
+  %cmp = icmp sgt i32 %shlx, %add
+  ret i1 %cmp
+}
+
+; Test: sle (Signed Less or Equal) with nsw
+define i1 @icmp_shl_nsw_sle(i32 %x, i32 %y) {
+; CHECK-LABEL: @icmp_shl_nsw_sle(
+; CHECK-NEXT:    [[TMP1:%.*]] = add nsw i32 [[Y:%.*]], 1
+; CHECK-NEXT:    [[CMP:%.*]] = icmp sle i32 [[X:%.*]], [[TMP1]]
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %shlx = shl nsw i32 %x, 5
+  %shly = shl nsw i32 %y, 5
+  %add = add nsw i32 %shly, 32
+  %cmp = icmp sle i32 %shlx, %add
+  ret i1 %cmp
+}
diff --git a/llvm/test/Transforms/InstCombine/lshr.ll b/llvm/test/Transforms/InstCombine/lshr.ll
index 4af532ae8d1d3..811f5d2e4f529 100644
--- a/llvm/test/Transforms/InstCombine/lshr.ll
+++ b/llvm/test/Transforms/InstCombine/lshr.ll
@@ -142,8 +142,8 @@ define i8 @lshr_cttz_zero_is_undef_vec(<2 x i8> %x) {
 define i8 @lshr_exact(i8 %x) {
 ; CHECK-LABEL: @lshr_exact(
 ; CHECK-NEXT:    [[LSHR:%.*]] = and i8 [[TMP1:%.*]], 63
-; CHECK-NEXT:    [[ADD1:%.*]] = add nuw nsw i8 [[LSHR]], 1
-; CHECK-NEXT:    ret i8 [[ADD1]]
+; CHECK-NEXT:    [[ADD:%.*]] = add nuw nsw i8 [[LSHR]], 1
+; CHECK-NEXT:    ret i8 [[ADD]]
 ;
   %shl = shl i8 %x, 2
   %add = add i8 %shl, 4

>From 43811fb235c8ffddaca35bf1c30c3f4105981f03 Mon Sep 17 00:00:00 2001
From: Michael-Chen-NJU <2802328816 at qq.com>
Date: Wed, 4 Feb 2026 12:17:41 +0800
Subject: [PATCH 11/14] Enhance logic in canEvaluateShifted and update test
 cases

---
 .../lib/Transforms/InstCombine/InstCombineShifts.cpp |  9 +++++++--
 .../Transforms/InstCombine/icmp-shl-add-to-add.ll    |  4 ++--
 llvm/test/Transforms/InstCombine/lshr.ll             | 12 ++++++------
 3 files changed, 15 insertions(+), 10 deletions(-)

diff --git a/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp b/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
index 4b7903876f424..4000cb6a6b569 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
@@ -644,8 +644,13 @@ bool InstCombinerImpl::canEvaluateShifted(Value *V, unsigned NumBits,
            MulConst->isNegatedPowerOf2() && MulConst->countr_zero() == NumBits;
   }
   case Instruction::Add: {
-    return canEvaluateShifted(I->getOperand(0), NumBits, ShiftOp, I) &&
-           canEvaluateShifted(I->getOperand(1), NumBits, ShiftOp, I);
+    auto *BinOp = cast<BinaryOperator>(I);
+    if (ShiftOp == Instruction::Shl || BinOp->hasNoUnsignedWrap() ||
+        BinOp->hasNoSignedWrap())
+      return canEvaluateShifted(I->getOperand(0), NumBits, ShiftOp, I) &&
+             canEvaluateShifted(I->getOperand(1), NumBits, ShiftOp, I);
+
+    return false;
   }
   }
 }
diff --git a/llvm/test/Transforms/InstCombine/icmp-shl-add-to-add.ll b/llvm/test/Transforms/InstCombine/icmp-shl-add-to-add.ll
index 7b8d01866fc96..f4c2e3317dc9a 100644
--- a/llvm/test/Transforms/InstCombine/icmp-shl-add-to-add.ll
+++ b/llvm/test/Transforms/InstCombine/icmp-shl-add-to-add.ll
@@ -113,8 +113,8 @@ define i1 @shl_add_const_eq_multi_use(i64 %v0, i64 %v3) {
 ; CHECK-LABEL: @shl_add_const_eq_multi_use(
 ; CHECK-NEXT:    [[V1:%.*]] = shl nsw i64 [[V0:%.*]], 5
 ; CHECK-NEXT:    call void @use_i64(i64 [[V1]])
-; CHECK-NEXT:    [[TMP1:%.*]] = add nsw i64 [[V3:%.*]], 1
-; CHECK-NEXT:    [[V6:%.*]] = icmp eq i64 [[V0]], [[TMP1]]
+; CHECK-NEXT:    [[V5:%.*]] = add nsw i64 [[V3:%.*]], 1
+; CHECK-NEXT:    [[V6:%.*]] = icmp eq i64 [[V0]], [[V5]]
 ; CHECK-NEXT:    ret i1 [[V6]]
 ;
   %v1 = shl nsw i64 %v0, 5
diff --git a/llvm/test/Transforms/InstCombine/lshr.ll b/llvm/test/Transforms/InstCombine/lshr.ll
index 811f5d2e4f529..ff358c6bc772a 100644
--- a/llvm/test/Transforms/InstCombine/lshr.ll
+++ b/llvm/test/Transforms/InstCombine/lshr.ll
@@ -141,9 +141,9 @@ define i8 @lshr_cttz_zero_is_undef_vec(<2 x i8> %x) {
 
 define i8 @lshr_exact(i8 %x) {
 ; CHECK-LABEL: @lshr_exact(
-; CHECK-NEXT:    [[LSHR:%.*]] = and i8 [[TMP1:%.*]], 63
-; CHECK-NEXT:    [[ADD:%.*]] = add nuw nsw i8 [[LSHR]], 1
-; CHECK-NEXT:    ret i8 [[ADD]]
+; CHECK-NEXT:    [[TMP1:%.*]] = add i8 [[X:%.*]], 1
+; CHECK-NEXT:    [[LSHR:%.*]] = and i8 [[TMP1]], 63
+; CHECK-NEXT:    ret i8 [[LSHR]]
 ;
   %shl = shl i8 %x, 2
   %add = add i8 %shl, 4
@@ -153,9 +153,9 @@ define i8 @lshr_exact(i8 %x) {
 
 define <2 x i8> @lshr_exact_splat_vec(<2 x i8> %x) {
 ; CHECK-LABEL: @lshr_exact_splat_vec(
-; CHECK-NEXT:    [[LSHR:%.*]] = and <2 x i8> [[TMP1:%.*]], splat (i8 63)
-; CHECK-NEXT:    [[ADD:%.*]] = add nuw nsw <2 x i8> [[LSHR]], splat (i8 1)
-; CHECK-NEXT:    ret <2 x i8> [[ADD]]
+; CHECK-NEXT:    [[TMP1:%.*]] = add <2 x i8> [[X:%.*]], splat (i8 1)
+; CHECK-NEXT:    [[LSHR:%.*]] = and <2 x i8> [[TMP1]], splat (i8 63)
+; CHECK-NEXT:    ret <2 x i8> [[LSHR]]
 ;
   %shl = shl <2 x i8> %x, <i8 2, i8 2>
   %add = add <2 x i8> %shl, <i8 4, i8 4>

>From 98cbd5f6d28aeecf7fd764b3ce779518c5213923 Mon Sep 17 00:00:00 2001
From: Michael-Chen-NJU <2802328816 at qq.com>
Date: Sun, 8 Feb 2026 02:45:51 +0800
Subject: [PATCH 12/14] [InstCombine] Refactor shift evaluation logic to use
 opcode directly and add regression test for fuzzing issue

---
 .../Transforms/InstCombine/InstCombineShifts.cpp  | 10 +++++++---
 .../Transforms/InstCombine/icmp-shl-add-to-add.ll | 15 +++++++++++++++
 2 files changed, 22 insertions(+), 3 deletions(-)

diff --git a/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp b/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
index 4000cb6a6b569..40b8c215e8aae 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
@@ -532,10 +532,12 @@ Instruction *InstCombinerImpl::commonShiftTransforms(BinaryOperator &I) {
 
 /// Return true if we can simplify two logical (either left or right) shifts
 /// that have constant shift amounts: OuterShift (InnerShift X, C1), C2.
-static bool canEvaluateShiftedShift(unsigned OuterShAmt, bool IsOuterShl,
+static bool canEvaluateShiftedShift(unsigned OuterShAmt,
+                                    Instruction::BinaryOps OuterOpc,
                                     Instruction *InnerShift,
                                     InstCombinerImpl &IC, Instruction *CxtI) {
   assert(InnerShift->isLogicalShift() && "Unexpected instruction type");
+  bool IsOuterShl = OuterOpc == Instruction::Shl;
 
   // We need constant scalar or constant splat shifts.
   const APInt *InnerShiftConst;
@@ -546,6 +548,9 @@ static bool canEvaluateShiftedShift(unsigned OuterShAmt, bool IsOuterShl,
   // shl (shl X, C1), C2 -->  shl X, C1 + C2
   // lshr (lshr X, C1), C2 --> lshr X, C1 + C2
   bool IsInnerShl = InnerShift->getOpcode() == Instruction::Shl;
+  if (OuterOpc == Instruction::AShr && IsInnerShl &&
+      !cast<BinaryOperator>(InnerShift)->hasNoSignedWrap())
+    return false;
   if (IsInnerShl == IsOuterShl)
     return true;
 
@@ -616,8 +621,7 @@ bool InstCombinerImpl::canEvaluateShifted(Value *V, unsigned NumBits,
 
   case Instruction::Shl:
   case Instruction::LShr:
-    return canEvaluateShiftedShift(NumBits, ShiftOp == Instruction::Shl, I,
-                                   *this, CxtI);
+    return canEvaluateShiftedShift(NumBits, ShiftOp, I, *this, CxtI);
 
   case Instruction::Select: {
     SelectInst *SI = cast<SelectInst>(I);
diff --git a/llvm/test/Transforms/InstCombine/icmp-shl-add-to-add.ll b/llvm/test/Transforms/InstCombine/icmp-shl-add-to-add.ll
index f4c2e3317dc9a..37197bfd67c73 100644
--- a/llvm/test/Transforms/InstCombine/icmp-shl-add-to-add.ll
+++ b/llvm/test/Transforms/InstCombine/icmp-shl-add-to-add.ll
@@ -278,3 +278,18 @@ define i1 @icmp_shl_nsw_sle(i32 %x, i32 %y) {
   %cmp = icmp sle i32 %shlx, %add
   ret i1 %cmp
 }
+
+; NOTE: This is a regression test for a miscompile discovered by fuzzing.
+define i1 @icmp_slt_shl_nsw_add_nsw_fuzz(i32 %x, i32 %y) {
+; CHECK-LABEL: @icmp_slt_shl_nsw_add_nsw_fuzz(
+; CHECK-NEXT:    [[SHLX:%.*]] = shl nsw i32 [[X:%.*]], 5
+; CHECK-NEXT:    [[SHLY:%.*]] = shl nuw i32 [[Y:%.*]], 5
+; CHECK-NEXT:    [[CMP:%.*]] = icmp sle i32 [[SHLX]], [[SHLY]]
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %shlx = shl nsw i32 %x, 5
+  %shly = shl nuw i32 %y, 5
+  %add = add nsw i32 32, %shly
+  %cmp = icmp slt i32 %shlx, %add
+  ret i1 %cmp
+}

>From 650faecb81968f7bfc4dd085e4b44c0b36451feb Mon Sep 17 00:00:00 2001
From: Michael-Chen-NJU <2802328816 at qq.com>
Date: Tue, 10 Feb 2026 22:19:25 +0800
Subject: [PATCH 13/14] Refactor canEvaluateShifted to improve handling of
 shift operations and add regression test for unsigned comparison issue

---
 .../Transforms/InstCombine/InstCombineShifts.cpp |  5 +++--
 .../InstCombine/icmp-shl-add-to-add.ll           | 16 ++++++++++++++++
 2 files changed, 19 insertions(+), 2 deletions(-)

diff --git a/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp b/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
index 40b8c215e8aae..37e902fe984f2 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
@@ -649,8 +649,9 @@ bool InstCombinerImpl::canEvaluateShifted(Value *V, unsigned NumBits,
   }
   case Instruction::Add: {
     auto *BinOp = cast<BinaryOperator>(I);
-    if (ShiftOp == Instruction::Shl || BinOp->hasNoUnsignedWrap() ||
-        BinOp->hasNoSignedWrap())
+    if (ShiftOp == Instruction::Shl ||
+        (ShiftOp == Instruction::LShr && BinOp->hasNoUnsignedWrap()) ||
+        (ShiftOp == Instruction::AShr && BinOp->hasNoSignedWrap()))
       return canEvaluateShifted(I->getOperand(0), NumBits, ShiftOp, I) &&
              canEvaluateShifted(I->getOperand(1), NumBits, ShiftOp, I);
 
diff --git a/llvm/test/Transforms/InstCombine/icmp-shl-add-to-add.ll b/llvm/test/Transforms/InstCombine/icmp-shl-add-to-add.ll
index 37197bfd67c73..eb0a19f9459ad 100644
--- a/llvm/test/Transforms/InstCombine/icmp-shl-add-to-add.ll
+++ b/llvm/test/Transforms/InstCombine/icmp-shl-add-to-add.ll
@@ -293,3 +293,19 @@ define i1 @icmp_slt_shl_nsw_add_nsw_fuzz(i32 %x, i32 %y) {
   %cmp = icmp slt i32 %shlx, %add
   ret i1 %cmp
 }
+
+; NOTE: This is a regression test for a miscompile discovered by fuzzing (unsigned comparison).
+define i1 @icmp_ugt_shl_nuw_add_nsw_fuzz(i32 %x, i32 %y) {
+; CHECK-LABEL: @icmp_ugt_shl_nuw_add_nsw_fuzz(
+; CHECK-NEXT:    [[SHLX:%.*]] = shl nuw i32 [[X:%.*]], 5
+; CHECK-NEXT:    [[SHLY:%.*]] = shl nsw i32 [[Y:%.*]], 5
+; CHECK-NEXT:    [[ADD:%.*]] = add nsw i32 [[SHLY]], 32
+; CHECK-NEXT:    [[CMP:%.*]] = icmp ugt i32 [[SHLX]], [[ADD]]
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %shlx = shl nuw i32 %x, 5
+  %shly = shl nsw i32 %y, 5
+  %add = add nsw i32 %shly, 32
+  %cmp = icmp ugt i32 %shlx, %add
+  ret i1 %cmp
+}

>From f22719f8c785d44112e940d2cd13990bef7265b5 Mon Sep 17 00:00:00 2001
From: Michael-Chen-NJU <2802328816 at qq.com>
Date: Thu, 12 Feb 2026 19:52:57 +0800
Subject: [PATCH 14/14] Refactor getShiftedValue to drop poison generating
 flags for left shifts

---
 llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)

diff --git a/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp b/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
index 37e902fe984f2..22e10f696e33f 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
@@ -781,10 +781,8 @@ Value *InstCombinerImpl::getShiftedValue(Value *V, unsigned NumBits,
     return InsertNewInstWith(And, I->getIterator());
   }
   case Instruction::Add: {
-    if (ShiftOp == Instruction::Shl) {
-      I->setHasNoUnsignedWrap(false);
-      I->setHasNoSignedWrap(false);
-    }
+    if (ShiftOp == Instruction::Shl)
+      I->dropPoisonGeneratingFlags();
     I->setOperand(0, getShiftedValue(I->getOperand(0), NumBits, ShiftOp));
     I->setOperand(1, getShiftedValue(I->getOperand(1), NumBits, ShiftOp));
     return I;



More information about the llvm-commits mailing list