[llvm] [InstCombine] Fold `(trunc X)` into `X & Mask` inside `decomposeBitTestICmp` (PR #171195)

Tirthankar Mazumder via llvm-commits llvm-commits at lists.llvm.org
Thu Dec 11 13:00:39 PST 2025


https://github.com/wermos updated https://github.com/llvm/llvm-project/pull/171195

>From a46674a3f4b015e05abc5a2fa4cd73af8429749f Mon Sep 17 00:00:00 2001
From: Tirthankar Mazumder <tmazumder.github at gmail.com>
Date: Fri, 12 Dec 2025 00:21:49 +0530
Subject: [PATCH 1/4] Pre-commit tests

---
 llvm/test/Transforms/InstCombine/and-or-icmps.ll | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/llvm/test/Transforms/InstCombine/and-or-icmps.ll b/llvm/test/Transforms/InstCombine/and-or-icmps.ll
index 290e344acb980..bd62e7726b629 100644
--- a/llvm/test/Transforms/InstCombine/and-or-icmps.ll
+++ b/llvm/test/Transforms/InstCombine/and-or-icmps.ll
@@ -3721,3 +3721,19 @@ define i1 @merge_range_check_or(i8 %a) {
   %and = or i1 %cmp1, %cmp2
   ret i1 %and
 }
+
+; Just a very complicated way of checking if v1 == 0.
+define i1 @complicated_zero_equality_test(i64 %v1) {
+; CHECK-LABEL: @complicated_zero_equality_test(
+; CHECK-NEXT:    [[V2:%.*]] = trunc i64 [[V1:%.*]] to i32
+; CHECK-NEXT:    [[V3:%.*]] = icmp eq i32 [[V2]], 0
+; CHECK-NEXT:    [[V4:%.*]] = icmp ult i64 [[V1]], 4294967296
+; CHECK-NEXT:    [[V5:%.*]] = and i1 [[V4]], [[V3]]
+; CHECK-NEXT:    ret i1 [[V5]]
+;
+  %v2 = trunc i64 %v1 to i32
+  %v3 = icmp eq i32 %v2, 0
+  %v4 = icmp ult i64 %v1, 4294967296 ; 2 ^ 32
+  %v5 = and i1 %v4, %v3
+  ret i1 %v5
+}

>From 43126d0ca25288e0f2cc90c2d83c57b3296ed565 Mon Sep 17 00:00:00 2001
From: Tirthankar Mazumder <tmazumder.github at gmail.com>
Date: Fri, 12 Dec 2025 00:22:47 +0530
Subject: [PATCH 2/4] Implement missing optimization in `decompostBitTestICmp`.

---
 llvm/lib/Analysis/CmpInstAnalysis.cpp           | 17 ++++++++++++++---
 .../InstCombine/InstCombineCompares.cpp         |  5 +++--
 .../test/Transforms/InstCombine/and-or-icmps.ll |  5 +----
 3 files changed, 18 insertions(+), 9 deletions(-)

diff --git a/llvm/lib/Analysis/CmpInstAnalysis.cpp b/llvm/lib/Analysis/CmpInstAnalysis.cpp
index a6d0d3ff4fcd4..c6bd538a9fe7a 100644
--- a/llvm/lib/Analysis/CmpInstAnalysis.cpp
+++ b/llvm/lib/Analysis/CmpInstAnalysis.cpp
@@ -153,15 +153,26 @@ llvm::decomposeBitTestICmp(Value *LHS, Value *RHS, CmpInst::Predicate Pred,
   case ICmpInst::ICMP_NE: {
     assert(DecomposeAnd);
     const APInt *AndC;
-    Value *AndVal;
-    if (match(LHS, m_And(m_Value(AndVal), m_APIntAllowPoison(AndC)))) {
-      LHS = AndVal;
+    Value *X;
+
+    if (match(LHS, m_And(m_Value(X), m_APIntAllowPoison(AndC)))) {
+      LHS = X;
       Result.Mask = *AndC;
       Result.C = C;
       Result.Pred = Pred;
       break;
     }
 
+    // Try to convert (trunc X) eq/ne C into (X & Mask) eq/ne C
+    if (LookThroughTrunc && match(LHS, m_Trunc(m_Value(X)))) {
+      Result.X = X;
+      Result.Pred = Pred;
+      unsigned BitWidth = X->getType()->getScalarSizeInBits();
+      Result.Mask = APInt::getLowBitsSet(BitWidth, C.getBitWidth());
+      Result.C = C.zext(BitWidth);
+      break;
+    }
+
     return std::nullopt;
   }
   }
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
index 616cb04be9dbc..752402799932a 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -6290,8 +6290,9 @@ Instruction *InstCombinerImpl::foldICmpWithTrunc(ICmpInst &ICmp) {
 
   // This matches patterns corresponding to tests of the signbit as well as:
   // (trunc X) pred C2 --> (X & Mask) == C
-  if (auto Res = decomposeBitTestICmp(Op0, Op1, Pred, /*LookThroughTrunc=*/true,
-                                      /*AllowNonZeroC=*/true)) {
+  if (auto Res =
+          decomposeBitTestICmp(Op0, Op1, Pred, /*LookThroughTrunc=*/true,
+                               /*AllowNonZeroC=*/true, /*DecomposeAnd=*/true)) {
     Value *And = Builder.CreateAnd(Res->X, Res->Mask);
     Constant *C = ConstantInt::get(Res->X->getType(), Res->C);
     return new ICmpInst(Res->Pred, And, C);
diff --git a/llvm/test/Transforms/InstCombine/and-or-icmps.ll b/llvm/test/Transforms/InstCombine/and-or-icmps.ll
index bd62e7726b629..975d3a072bcd3 100644
--- a/llvm/test/Transforms/InstCombine/and-or-icmps.ll
+++ b/llvm/test/Transforms/InstCombine/and-or-icmps.ll
@@ -3725,10 +3725,7 @@ define i1 @merge_range_check_or(i8 %a) {
 ; Just a very complicated way of checking if v1 == 0.
 define i1 @complicated_zero_equality_test(i64 %v1) {
 ; CHECK-LABEL: @complicated_zero_equality_test(
-; CHECK-NEXT:    [[V2:%.*]] = trunc i64 [[V1:%.*]] to i32
-; CHECK-NEXT:    [[V3:%.*]] = icmp eq i32 [[V2]], 0
-; CHECK-NEXT:    [[V4:%.*]] = icmp ult i64 [[V1]], 4294967296
-; CHECK-NEXT:    [[V5:%.*]] = and i1 [[V4]], [[V3]]
+; CHECK-NEXT:    [[V5:%.*]] = icmp eq i64 [[V1:%.*]], 0
 ; CHECK-NEXT:    ret i1 [[V5]]
 ;
   %v2 = trunc i64 %v1 to i32

>From 2a7cba22af199c889bb184f91bc57e21d268234c Mon Sep 17 00:00:00 2001
From: Tirthankar Mazumder <tmazumder.github at gmail.com>
Date: Fri, 12 Dec 2025 01:44:18 +0530
Subject: [PATCH 3/4] Updated tests.

---
 .../Transforms/InstCombine/2012-02-28-ICmp.ll |  9 ++--
 .../test/Transforms/InstCombine/icmp-trunc.ll | 46 ++++++++-----------
 .../test/Transforms/InstCombine/known-bits.ll |  8 ++--
 3 files changed, 26 insertions(+), 37 deletions(-)

diff --git a/llvm/test/Transforms/InstCombine/2012-02-28-ICmp.ll b/llvm/test/Transforms/InstCombine/2012-02-28-ICmp.ll
index a112fb9aae396..fdf4300debcf4 100644
--- a/llvm/test/Transforms/InstCombine/2012-02-28-ICmp.ll
+++ b/llvm/test/Transforms/InstCombine/2012-02-28-ICmp.ll
@@ -3,11 +3,10 @@
 
 ; <rdar://problem/10803154>
 
-; There should be no transformation.
 define i1 @f1(i32 %x) {
 ; CHECK-LABEL: @f1(
-; CHECK-NEXT:    [[A:%.*]] = trunc i32 [[X:%.*]] to i8
-; CHECK-NEXT:    [[B:%.*]] = icmp ne i8 [[A]], 0
+; CHECK-NEXT:    [[TMP1:%.*]] = and i32 [[X:%.*]], 255
+; CHECK-NEXT:    [[B:%.*]] = icmp ne i32 [[TMP1]], 0
 ; CHECK-NEXT:    [[C:%.*]] = and i32 [[X]], 16711680
 ; CHECK-NEXT:    [[D:%.*]] = icmp ne i32 [[C]], 0
 ; CHECK-NEXT:    [[E:%.*]] = and i1 [[B]], [[D]]
@@ -23,8 +22,8 @@ define i1 @f1(i32 %x) {
 
 define i1 @f1_logical(i32 %x) {
 ; CHECK-LABEL: @f1_logical(
-; CHECK-NEXT:    [[A:%.*]] = trunc i32 [[X:%.*]] to i8
-; CHECK-NEXT:    [[B:%.*]] = icmp ne i8 [[A]], 0
+; CHECK-NEXT:    [[TMP1:%.*]] = and i32 [[X:%.*]], 255
+; CHECK-NEXT:    [[B:%.*]] = icmp ne i32 [[TMP1]], 0
 ; CHECK-NEXT:    [[C:%.*]] = and i32 [[X]], 16711680
 ; CHECK-NEXT:    [[D:%.*]] = icmp ne i32 [[C]], 0
 ; CHECK-NEXT:    [[E:%.*]] = and i1 [[B]], [[D]]
diff --git a/llvm/test/Transforms/InstCombine/icmp-trunc.ll b/llvm/test/Transforms/InstCombine/icmp-trunc.ll
index 431fc2b4f062c..d2fd2d7f3d921 100644
--- a/llvm/test/Transforms/InstCombine/icmp-trunc.ll
+++ b/llvm/test/Transforms/InstCombine/icmp-trunc.ll
@@ -314,15 +314,10 @@ define i1 @sgt_n1_use(i32 %x) {
 }
 
 define i1 @trunc_eq_i32_i8(i32 %x) {
-; DL64-LABEL: @trunc_eq_i32_i8(
-; DL64-NEXT:    [[TMP1:%.*]] = and i32 [[X:%.*]], 255
-; DL64-NEXT:    [[R:%.*]] = icmp eq i32 [[TMP1]], 42
-; DL64-NEXT:    ret i1 [[R]]
-;
-; DL8-LABEL: @trunc_eq_i32_i8(
-; DL8-NEXT:    [[T:%.*]] = trunc i32 [[X:%.*]] to i8
-; DL8-NEXT:    [[R:%.*]] = icmp eq i8 [[T]], 42
-; DL8-NEXT:    ret i1 [[R]]
+; CHECK-LABEL: @trunc_eq_i32_i8(
+; CHECK-NEXT:    [[TMP1:%.*]] = and i32 [[X:%.*]], 255
+; CHECK-NEXT:    [[R:%.*]] = icmp eq i32 [[TMP1]], 42
+; CHECK-NEXT:    ret i1 [[R]]
 ;
   %t = trunc i32 %x to i8
   %r = icmp eq i8 %t, 42
@@ -331,8 +326,8 @@ define i1 @trunc_eq_i32_i8(i32 %x) {
 
 define <2 x i1> @trunc_eq_v2i32_v2i8(<2 x i32> %x) {
 ; CHECK-LABEL: @trunc_eq_v2i32_v2i8(
-; CHECK-NEXT:    [[T:%.*]] = trunc <2 x i32> [[X:%.*]] to <2 x i8>
-; CHECK-NEXT:    [[R:%.*]] = icmp eq <2 x i8> [[T]], splat (i8 42)
+; CHECK-NEXT:    [[TMP1:%.*]] = and <2 x i32> [[X:%.*]], splat (i32 255)
+; CHECK-NEXT:    [[R:%.*]] = icmp eq <2 x i32> [[TMP1]], splat (i32 42)
 ; CHECK-NEXT:    ret <2 x i1> [[R]]
 ;
   %t = trunc <2 x i32> %x to <2 x i8>
@@ -341,15 +336,10 @@ define <2 x i1> @trunc_eq_v2i32_v2i8(<2 x i32> %x) {
 }
 
 define i1 @trunc_ne_i64_i10(i64 %x) {
-; DL64-LABEL: @trunc_ne_i64_i10(
-; DL64-NEXT:    [[TMP1:%.*]] = and i64 [[X:%.*]], 1023
-; DL64-NEXT:    [[R:%.*]] = icmp eq i64 [[TMP1]], 42
-; DL64-NEXT:    ret i1 [[R]]
-;
-; DL8-LABEL: @trunc_ne_i64_i10(
-; DL8-NEXT:    [[T:%.*]] = trunc i64 [[X:%.*]] to i10
-; DL8-NEXT:    [[R:%.*]] = icmp eq i10 [[T]], 42
-; DL8-NEXT:    ret i1 [[R]]
+; CHECK-LABEL: @trunc_ne_i64_i10(
+; CHECK-NEXT:    [[TMP1:%.*]] = and i64 [[X:%.*]], 1023
+; CHECK-NEXT:    [[R:%.*]] = icmp eq i64 [[TMP1]], 42
+; CHECK-NEXT:    ret i1 [[R]]
 ;
   %t = trunc i64 %x to i10
   %r = icmp eq i10 %t, 42
@@ -432,8 +422,8 @@ define i1 @shl2_trunc_ne0(i9 %a) {
 define i1 @shl3_trunc_eq0(i9 %a) {
 ; CHECK-LABEL: @shl3_trunc_eq0(
 ; CHECK-NEXT:    [[SHL:%.*]] = shl i9 3, [[A:%.*]]
-; CHECK-NEXT:    [[T:%.*]] = trunc i9 [[SHL]] to i6
-; CHECK-NEXT:    [[R:%.*]] = icmp eq i6 [[T]], 0
+; CHECK-NEXT:    [[TMP1:%.*]] = and i9 [[SHL]], 63
+; CHECK-NEXT:    [[R:%.*]] = icmp eq i9 [[TMP1]], 0
 ; CHECK-NEXT:    ret i1 [[R]]
 ;
   %shl = shl i9 3, %a
@@ -569,8 +559,8 @@ define i1 @eq_nuw(i32 %x) {
 ; DL64-NEXT:    ret i1 [[R]]
 ;
 ; DL8-LABEL: @eq_nuw(
-; DL8-NEXT:    [[T:%.*]] = trunc nuw i32 [[X:%.*]] to i8
-; DL8-NEXT:    [[R:%.*]] = icmp eq i8 [[T]], 123
+; DL8-NEXT:    [[TMP1:%.*]] = and i32 [[X:%.*]], 255
+; DL8-NEXT:    [[R:%.*]] = icmp eq i32 [[TMP1]], 123
 ; DL8-NEXT:    ret i1 [[R]]
 ;
   %t = trunc nuw i32 %x to i8
@@ -676,8 +666,8 @@ define i1 @ne_nsw(i32 %x) {
 ; DL64-NEXT:    ret i1 [[R]]
 ;
 ; DL8-LABEL: @ne_nsw(
-; DL8-NEXT:    [[T:%.*]] = trunc nsw i32 [[X:%.*]] to i8
-; DL8-NEXT:    [[R:%.*]] = icmp ne i8 [[T]], -123
+; DL8-NEXT:    [[TMP1:%.*]] = and i32 [[X:%.*]], 255
+; DL8-NEXT:    [[R:%.*]] = icmp ne i32 [[TMP1]], 133
 ; DL8-NEXT:    ret i1 [[R]]
 ;
   %t = trunc nsw i32 %x to i8
@@ -813,8 +803,8 @@ define i1 @do_not_mask_trunc_eq_i32_i8(i32 %x) {
 ; DL64-NEXT:    ret i1 [[R]]
 ;
 ; DL8-LABEL: @do_not_mask_trunc_eq_i32_i8(
-; DL8-NEXT:    [[T:%.*]] = trunc nuw i32 [[X:%.*]] to i8
-; DL8-NEXT:    [[R:%.*]] = icmp eq i8 [[T]], 42
+; DL8-NEXT:    [[TMP1:%.*]] = and i32 [[X:%.*]], 255
+; DL8-NEXT:    [[R:%.*]] = icmp eq i32 [[TMP1]], 42
 ; DL8-NEXT:    ret i1 [[R]]
 ;
   %t = trunc nuw i32 %x to i8
diff --git a/llvm/test/Transforms/InstCombine/known-bits.ll b/llvm/test/Transforms/InstCombine/known-bits.ll
index da2123a5dfe74..2da28a1bef6ec 100644
--- a/llvm/test/Transforms/InstCombine/known-bits.ll
+++ b/llvm/test/Transforms/InstCombine/known-bits.ll
@@ -343,8 +343,8 @@ exit:
 define i32 @test_icmp_trunc1(i32 %x) {
 ; CHECK-LABEL: @test_icmp_trunc1(
 ; CHECK-NEXT:  entry:
-; CHECK-NEXT:    [[Y:%.*]] = trunc i32 [[X:%.*]] to i16
-; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i16 [[Y]], 7
+; CHECK-NEXT:    [[TMP0:%.*]] = and i32 [[X:%.*]], 65535
+; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i32 [[TMP0]], 7
 ; CHECK-NEXT:    br i1 [[CMP]], label [[THEN:%.*]], label [[ELSE:%.*]]
 ; CHECK:       then:
 ; CHECK-NEXT:    ret i32 7
@@ -365,8 +365,8 @@ else:
 define i32 @test_icmp_trunc_assume(i32 %x) {
 ; CHECK-LABEL: @test_icmp_trunc_assume(
 ; CHECK-NEXT:  entry:
-; CHECK-NEXT:    [[Y:%.*]] = trunc i32 [[X:%.*]] to i16
-; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i16 [[Y]], 7
+; CHECK-NEXT:    [[TMP0:%.*]] = and i32 [[X:%.*]], 65535
+; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i32 [[TMP0]], 7
 ; CHECK-NEXT:    call void @llvm.assume(i1 [[CMP]])
 ; CHECK-NEXT:    ret i32 7
 ;

>From 43ddc4dbc788ec1a06e1ed4e84a465bc81aed844 Mon Sep 17 00:00:00 2001
From: Tirthankar Mazumder <tmazumder.github at gmail.com>
Date: Fri, 12 Dec 2025 02:17:57 +0530
Subject: [PATCH 4/4] Updated more tests.

---
 llvm/test/Transforms/InstCombine/getelementptr.ll | 10 +++++-----
 llvm/test/Transforms/InstCombine/memchr-11.ll     |  8 ++++----
 llvm/test/Transforms/InstCombine/memchr-6.ll      | 12 ++++++------
 llvm/test/Transforms/InstCombine/memrchr-4.ll     | 12 ++++++------
 llvm/test/Transforms/InstCombine/pr39908.ll       |  8 ++++----
 llvm/test/Transforms/InstCombine/strchr-3.ll      |  4 ++--
 llvm/test/Transforms/InstCombine/strchr-4.ll      |  4 ++--
 llvm/test/Transforms/InstCombine/strrchr-3.ll     |  4 ++--
 8 files changed, 31 insertions(+), 31 deletions(-)

diff --git a/llvm/test/Transforms/InstCombine/getelementptr.ll b/llvm/test/Transforms/InstCombine/getelementptr.ll
index 92b76c5d1b46a..d8ae03bf13913 100644
--- a/llvm/test/Transforms/InstCombine/getelementptr.ll
+++ b/llvm/test/Transforms/InstCombine/getelementptr.ll
@@ -356,8 +356,8 @@ define i1 @test13_i16(i16 %X, ptr %P) {
 
 define i1 @test13_i128(i128 %X, ptr %P) {
 ; CHECK-LABEL: @test13_i128(
-; CHECK-NEXT:    [[TMP1:%.*]] = trunc nsw i128 [[X:%.*]] to i64
-; CHECK-NEXT:    [[C:%.*]] = icmp eq i64 [[TMP1]], -1
+; CHECK-NEXT:    [[TMP1:%.*]] = and i128 [[X:%.*]], 18446744073709551615
+; CHECK-NEXT:    [[C:%.*]] = icmp eq i128 [[TMP1]], 18446744073709551615
 ; CHECK-NEXT:    ret i1 [[C]]
 ;
   %A = getelementptr inbounds %S, ptr %P, i128 0, i32 1, i128 %X
@@ -568,8 +568,8 @@ define i32 @test20(ptr %P, i32 %A, i32 %B) {
 
 define i32 @test20_as1(ptr addrspace(1) %P, i32 %A, i32 %B) {
 ; CHECK-LABEL: @test20_as1(
-; CHECK-NEXT:    [[TMP1:%.*]] = trunc nsw i32 [[A:%.*]] to i16
-; CHECK-NEXT:    [[T6:%.*]] = icmp eq i16 [[TMP1]], 0
+; CHECK-NEXT:    [[TMP1:%.*]] = and i32 [[A:%.*]], 65535
+; CHECK-NEXT:    [[T6:%.*]] = icmp eq i32 [[TMP1]], 0
 ; CHECK-NEXT:    [[T7:%.*]] = zext i1 [[T6]] to i32
 ; CHECK-NEXT:    ret i32 [[T7]]
 ;
@@ -680,7 +680,7 @@ entry:
 define i32 @test28() nounwind  {
 ; CHECK-LABEL: @test28(
 ; CHECK-NEXT:  entry:
-; CHECK-NEXT:    [[ORIENTATIONS:%.*]] = alloca [1 x [1 x %struct.x]], align 8
+; CHECK-NEXT:    [[ORIENTATIONS:%.*]] = alloca [1 x [1 x [[STRUCT_X:%.*]]]], align 8
 ; CHECK-NEXT:    [[T3:%.*]] = call i32 @puts(ptr noundef nonnull dereferenceable(1) @.str) #[[ATTR0]]
 ; CHECK-NEXT:    br label [[BB10:%.*]]
 ; CHECK:       bb10:
diff --git a/llvm/test/Transforms/InstCombine/memchr-11.ll b/llvm/test/Transforms/InstCombine/memchr-11.ll
index 426b34463b915..bb250d70ca86f 100644
--- a/llvm/test/Transforms/InstCombine/memchr-11.ll
+++ b/llvm/test/Transforms/InstCombine/memchr-11.ll
@@ -13,8 +13,8 @@ declare ptr @memchr(ptr, i32, i64)
 
 define i1 @fold_memchr_a_c_5_eq_a(i32 %c) {
 ; CHECK-LABEL: @fold_memchr_a_c_5_eq_a(
-; CHECK-NEXT:    [[TMP1:%.*]] = trunc i32 [[C:%.*]] to i8
-; CHECK-NEXT:    [[CHAR0CMP:%.*]] = icmp eq i8 [[TMP1]], 49
+; CHECK-NEXT:    [[TMP1:%.*]] = and i32 [[C:%.*]], 255
+; CHECK-NEXT:    [[CHAR0CMP:%.*]] = icmp eq i32 [[TMP1]], 49
 ; CHECK-NEXT:    ret i1 [[CHAR0CMP]]
 ;
   %q = call ptr @memchr(ptr @a5, i32 %c, i64 5)
@@ -29,8 +29,8 @@ define i1 @fold_memchr_a_c_5_eq_a(i32 %c) {
 
 define i1 @fold_memchr_a_c_n_eq_a(i32 %c, i64 %n) {
 ; CHECK-LABEL: @fold_memchr_a_c_n_eq_a(
-; CHECK-NEXT:    [[TMP1:%.*]] = trunc i32 [[C:%.*]] to i8
-; CHECK-NEXT:    [[CHAR0CMP:%.*]] = icmp eq i8 [[TMP1]], 49
+; CHECK-NEXT:    [[TMP1:%.*]] = and i32 [[C:%.*]], 255
+; CHECK-NEXT:    [[CHAR0CMP:%.*]] = icmp eq i32 [[TMP1]], 49
 ; CHECK-NEXT:    [[TMP2:%.*]] = icmp ne i64 [[N:%.*]], 0
 ; CHECK-NEXT:    [[TMP3:%.*]] = select i1 [[TMP2]], i1 [[CHAR0CMP]], i1 false
 ; CHECK-NEXT:    ret i1 [[TMP3]]
diff --git a/llvm/test/Transforms/InstCombine/memchr-6.ll b/llvm/test/Transforms/InstCombine/memchr-6.ll
index 3c55f418cd8e0..0af0f41b493fd 100644
--- a/llvm/test/Transforms/InstCombine/memchr-6.ll
+++ b/llvm/test/Transforms/InstCombine/memchr-6.ll
@@ -31,8 +31,8 @@ define ptr @fold_memchr_a00000_c_5(i32 %C) {
 
 define ptr @fold_memchr_a11111_c_5(i32 %C) {
 ; CHECK-LABEL: @fold_memchr_a11111_c_5(
-; CHECK-NEXT:    [[TMP1:%.*]] = trunc i32 [[C:%.*]] to i8
-; CHECK-NEXT:    [[TMP2:%.*]] = icmp eq i8 [[TMP1]], 1
+; CHECK-NEXT:    [[TMP1:%.*]] = and i32 [[C:%.*]], 255
+; CHECK-NEXT:    [[TMP2:%.*]] = icmp eq i32 [[TMP1]], 1
 ; CHECK-NEXT:    [[MEMCHR_SEL2:%.*]] = select i1 [[TMP2]], ptr @a11111, ptr null
 ; CHECK-NEXT:    ret ptr [[MEMCHR_SEL2]]
 ;
@@ -47,8 +47,8 @@ define ptr @fold_memchr_a11111_c_5(i32 %C) {
 
 define ptr @fold_memchr_a11111_c_n(i32 %C, i64 %N) {
 ; CHECK-LABEL: @fold_memchr_a11111_c_n(
-; CHECK-NEXT:    [[TMP1:%.*]] = trunc i32 [[C:%.*]] to i8
-; CHECK-NEXT:    [[TMP2:%.*]] = icmp eq i8 [[TMP1]], 1
+; CHECK-NEXT:    [[TMP1:%.*]] = and i32 [[C:%.*]], 255
+; CHECK-NEXT:    [[TMP2:%.*]] = icmp eq i32 [[TMP1]], 1
 ; CHECK-NEXT:    [[TMP3:%.*]] = icmp ne i64 [[N:%.*]], 0
 ; CHECK-NEXT:    [[TMP4:%.*]] = and i1 [[TMP3]], [[TMP2]]
 ; CHECK-NEXT:    [[MEMCHR_SEL2:%.*]] = select i1 [[TMP4]], ptr @a11111, ptr null
@@ -86,8 +86,8 @@ define ptr @fold_memchr_a111122_c_n(i32 %C, i64 %N) {
 
 define ptr @fold_memchr_a1110111_c_3(i32 %C) {
 ; CHECK-LABEL: @fold_memchr_a1110111_c_3(
-; CHECK-NEXT:    [[TMP1:%.*]] = trunc i32 [[C:%.*]] to i8
-; CHECK-NEXT:    [[TMP2:%.*]] = icmp eq i8 [[TMP1]], 1
+; CHECK-NEXT:    [[TMP1:%.*]] = and i32 [[C:%.*]], 255
+; CHECK-NEXT:    [[TMP2:%.*]] = icmp eq i32 [[TMP1]], 1
 ; CHECK-NEXT:    [[MEMCHR_SEL2:%.*]] = select i1 [[TMP2]], ptr @a1110111, ptr null
 ; CHECK-NEXT:    ret ptr [[MEMCHR_SEL2]]
 ;
diff --git a/llvm/test/Transforms/InstCombine/memrchr-4.ll b/llvm/test/Transforms/InstCombine/memrchr-4.ll
index 7362916e28fb9..2b90c93429b51 100644
--- a/llvm/test/Transforms/InstCombine/memrchr-4.ll
+++ b/llvm/test/Transforms/InstCombine/memrchr-4.ll
@@ -14,8 +14,8 @@ declare ptr @memrchr(ptr, i32, i64)
 
 define ptr @fold_memrchr_a11111_c_5(i32 %C) {
 ; CHECK-LABEL: @fold_memrchr_a11111_c_5(
-; CHECK-NEXT:    [[TMP1:%.*]] = trunc i32 [[C:%.*]] to i8
-; CHECK-NEXT:    [[TMP2:%.*]] = icmp eq i8 [[TMP1]], 1
+; CHECK-NEXT:    [[TMP1:%.*]] = and i32 [[C:%.*]], 255
+; CHECK-NEXT:    [[TMP2:%.*]] = icmp eq i32 [[TMP1]], 1
 ; CHECK-NEXT:    [[MEMRCHR_SEL:%.*]] = select i1 [[TMP2]], ptr getelementptr inbounds nuw (i8, ptr @a11111, i64 4), ptr null
 ; CHECK-NEXT:    ret ptr [[MEMRCHR_SEL]]
 ;
@@ -31,8 +31,8 @@ define ptr @fold_memrchr_a11111_c_5(i32 %C) {
 define ptr @fold_memrchr_a11111_c_n(i32 %C, i64 %N) {
 ; CHECK-LABEL: @fold_memrchr_a11111_c_n(
 ; CHECK-NEXT:    [[TMP1:%.*]] = icmp ne i64 [[N:%.*]], 0
-; CHECK-NEXT:    [[TMP2:%.*]] = trunc i32 [[C:%.*]] to i8
-; CHECK-NEXT:    [[TMP3:%.*]] = icmp eq i8 [[TMP2]], 1
+; CHECK-NEXT:    [[TMP2:%.*]] = and i32 [[C:%.*]], 255
+; CHECK-NEXT:    [[TMP3:%.*]] = icmp eq i32 [[TMP2]], 1
 ; CHECK-NEXT:    [[TMP4:%.*]] = select i1 [[TMP1]], i1 [[TMP3]], i1 false
 ; CHECK-NEXT:    [[TMP5:%.*]] = getelementptr i8, ptr @a11111, i64 [[N]]
 ; CHECK-NEXT:    [[MEMRCHR_PTR_PLUS:%.*]] = getelementptr i8, ptr [[TMP5]], i64 -1
@@ -49,8 +49,8 @@ define ptr @fold_memrchr_a11111_c_n(i32 %C, i64 %N) {
 
 define ptr @fold_memrchr_a1110111_c_3(i32 %C) {
 ; CHECK-LABEL: @fold_memrchr_a1110111_c_3(
-; CHECK-NEXT:    [[TMP1:%.*]] = trunc i32 [[C:%.*]] to i8
-; CHECK-NEXT:    [[TMP2:%.*]] = icmp eq i8 [[TMP1]], 1
+; CHECK-NEXT:    [[TMP1:%.*]] = and i32 [[C:%.*]], 255
+; CHECK-NEXT:    [[TMP2:%.*]] = icmp eq i32 [[TMP1]], 1
 ; CHECK-NEXT:    [[MEMRCHR_SEL:%.*]] = select i1 [[TMP2]], ptr getelementptr inbounds nuw (i8, ptr @a1110111, i64 2), ptr null
 ; CHECK-NEXT:    ret ptr [[MEMRCHR_SEL]]
 ;
diff --git a/llvm/test/Transforms/InstCombine/pr39908.ll b/llvm/test/Transforms/InstCombine/pr39908.ll
index c36495ddcf10f..82b760aed1b78 100644
--- a/llvm/test/Transforms/InstCombine/pr39908.ll
+++ b/llvm/test/Transforms/InstCombine/pr39908.ll
@@ -19,8 +19,8 @@ define i1 @test(ptr %p, i32 %n) {
 ; Same test using 64-bit indices.
 define i1 @test64(ptr %p, i64 %n) {
 ; CHECK-LABEL: @test64(
-; CHECK-NEXT:    [[TMP1:%.*]] = trunc nsw i64 [[N:%.*]] to i32
-; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i32 [[TMP1]], 1
+; CHECK-NEXT:    [[TMP1:%.*]] = and i64 [[N:%.*]], 4294967295
+; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i64 [[TMP1]], 1
 ; CHECK-NEXT:    ret i1 [[CMP]]
 ;
   %end = getelementptr inbounds [0 x %S], ptr %p, i64 0, i64 %n, i32 0, i64 0
@@ -32,8 +32,8 @@ define i1 @test64(ptr %p, i64 %n) {
 ; Here the offset overflows and is treated modulo 2^32. This is UB.
 define i1 @test64_overflow(ptr %p, i64 %n) {
 ; CHECK-LABEL: @test64_overflow(
-; CHECK-NEXT:    [[TMP1:%.*]] = trunc nsw i64 [[N:%.*]] to i32
-; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i32 [[TMP1]], 1
+; CHECK-NEXT:    [[TMP1:%.*]] = and i64 [[N:%.*]], 4294967295
+; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i64 [[TMP1]], 1
 ; CHECK-NEXT:    ret i1 [[CMP]]
 ;
   %end = getelementptr inbounds [0 x %S], ptr %p, i64 0, i64 %n, i32 0, i64 8589934592
diff --git a/llvm/test/Transforms/InstCombine/strchr-3.ll b/llvm/test/Transforms/InstCombine/strchr-3.ll
index 36518a0bf496b..50a5df73aca0f 100644
--- a/llvm/test/Transforms/InstCombine/strchr-3.ll
+++ b/llvm/test/Transforms/InstCombine/strchr-3.ll
@@ -66,8 +66,8 @@ define ptr @fold_strchr_s111_C(i32 %C) {
 
 define ptr @fold_strchr_s000_C(i32 %C) {
 ; CHECK-LABEL: @fold_strchr_s000_C(
-; CHECK-NEXT:    [[TMP1:%.*]] = trunc i32 [[C:%.*]] to i8
-; CHECK-NEXT:    [[MEMCHR_CHAR0CMP:%.*]] = icmp eq i8 [[TMP1]], 0
+; CHECK-NEXT:    [[TMP1:%.*]] = and i32 [[C:%.*]], 255
+; CHECK-NEXT:    [[MEMCHR_CHAR0CMP:%.*]] = icmp eq i32 [[TMP1]], 0
 ; CHECK-NEXT:    [[MEMCHR_SEL:%.*]] = select i1 [[MEMCHR_CHAR0CMP]], ptr @s000, ptr null
 ; CHECK-NEXT:    ret ptr [[MEMCHR_SEL]]
 ;
diff --git a/llvm/test/Transforms/InstCombine/strchr-4.ll b/llvm/test/Transforms/InstCombine/strchr-4.ll
index 933bfa36b1c87..a12ea13ba2dee 100644
--- a/llvm/test/Transforms/InstCombine/strchr-4.ll
+++ b/llvm/test/Transforms/InstCombine/strchr-4.ll
@@ -68,8 +68,8 @@ define i1 @fold_strchr_s_nul_nez(ptr %s) {
 
 define i1 @fold_strchr_a_c_eq_a(i32 %c) {
 ; CHECK-LABEL: @fold_strchr_a_c_eq_a(
-; CHECK-NEXT:    [[TMP1:%.*]] = trunc i32 [[C:%.*]] to i8
-; CHECK-NEXT:    [[CHAR0CMP:%.*]] = icmp eq i8 [[TMP1]], 49
+; CHECK-NEXT:    [[TMP1:%.*]] = and i32 [[C:%.*]], 255
+; CHECK-NEXT:    [[CHAR0CMP:%.*]] = icmp eq i32 [[TMP1]], 49
 ; CHECK-NEXT:    ret i1 [[CHAR0CMP]]
 ;
   %q = call ptr @strchr(ptr @a5, i32 %c)
diff --git a/llvm/test/Transforms/InstCombine/strrchr-3.ll b/llvm/test/Transforms/InstCombine/strrchr-3.ll
index fda02114bc94e..87da72828f03a 100644
--- a/llvm/test/Transforms/InstCombine/strrchr-3.ll
+++ b/llvm/test/Transforms/InstCombine/strrchr-3.ll
@@ -11,8 +11,8 @@ declare ptr @strrchr(ptr, i32)
 
 define ptr @fold_strrchr_sp10_x(i32 %c) {
 ; CHECK-LABEL: @fold_strrchr_sp10_x(
-; CHECK-NEXT:    [[TMP1:%.*]] = trunc i32 [[C:%.*]] to i8
-; CHECK-NEXT:    [[MEMRCHR_CHAR0CMP:%.*]] = icmp eq i8 [[TMP1]], 0
+; CHECK-NEXT:    [[TMP1:%.*]] = and i32 [[C:%.*]], 255
+; CHECK-NEXT:    [[MEMRCHR_CHAR0CMP:%.*]] = icmp eq i32 [[TMP1]], 0
 ; CHECK-NEXT:    [[MEMRCHR_SEL:%.*]] = select i1 [[MEMRCHR_CHAR0CMP]], ptr getelementptr inbounds nuw (i8, ptr @s10, i64 10), ptr null
 ; CHECK-NEXT:    ret ptr [[MEMRCHR_SEL]]
 ;



More information about the llvm-commits mailing list