[llvm] [InstCombine] Combine trunc (lshr X, BW-1) to i1 --> icmp slt X, 0 (#142593) (PR #143846)

via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 12 00:23:06 PDT 2025


https://github.com/mayanksolanki393 created https://github.com/llvm/llvm-project/pull/143846

Fixes #142593, the issue was fixed using the suggestion on the ticket itself. 

>From cc2dfd887c74abf744c18a36ecf093c64f11cc3e Mon Sep 17 00:00:00 2001
From: mayanksolanki393 <mayanksolanki393 at gmail.com>
Date: Thu, 12 Jun 2025 07:16:30 +0000
Subject: [PATCH] [InstCombine] Combine trunc (lshr X, BW-1) to i1 --> icmp slt
 X, 0 (#142593)

---
 .../InstCombine/InstCombineCasts.cpp          |  8 +++++++
 .../InstCombine/2025-06-12-trunc-lshr.ll      | 23 +++++++++++++++++++
 2 files changed, 31 insertions(+)
 create mode 100644 llvm/test/Transforms/InstCombine/2025-06-12-trunc-lshr.ll

diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
index 2db79228bf0e6..24e34b6a09543 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
@@ -823,6 +823,14 @@ Instruction *InstCombinerImpl::visitTrunc(TruncInst &Trunc) {
       Value *And = Builder.CreateAnd(X, MaskC);
       return new ICmpInst(ICmpInst::ICMP_NE, And, Zero);
     }
+
+    if (match(Src, m_AShr(m_Value(X), m_SpecificInt(SrcWidth - 1))) ||
+        match(Src, m_LShr(m_Value(X), m_SpecificInt(SrcWidth - 1)))) {
+      // trunc (ashr X, BW-1) to i1 --> icmp slt X, 0
+      // trunc (lshr X, BW-1) to i1 --> icmp slt X, 0
+      return new ICmpInst(ICmpInst::ICMP_SLT, X, Zero);
+    }
+
     if (match(Src, m_OneUse(m_c_Or(m_LShr(m_Value(X), m_ImmConstant(C)),
                                    m_Deferred(X))))) {
       // trunc (or (lshr X, C), X) to i1 --> icmp ne (and X, C'), 0
diff --git a/llvm/test/Transforms/InstCombine/2025-06-12-trunc-lshr.ll b/llvm/test/Transforms/InstCombine/2025-06-12-trunc-lshr.ll
new file mode 100644
index 0000000000000..74576cda76204
--- /dev/null
+++ b/llvm/test/Transforms/InstCombine/2025-06-12-trunc-lshr.ll
@@ -0,0 +1,23 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
+; RUN: opt -S -passes=instcombine < %s | FileCheck %s
+
+define i1 @src(i32 %0, ptr writeonly captures(none) initializes((0, 4)) %p) local_unnamed_addr #0 {
+; CHECK-LABEL: define i1 @src(
+; CHECK-SAME: i32 [[TMP0:%.*]], ptr writeonly captures(none) initializes((0, 4)) [[P:%.*]]) local_unnamed_addr #[[ATTR0:[0-9]+]] {
+; CHECK-NEXT:  [[COMMON_RET1:.*:]]
+; CHECK-NEXT:    [[DOTLOBIT:%.*]] = lshr i32 [[TMP0]], 31
+; CHECK-NEXT:    store i32 [[DOTLOBIT]], ptr [[P]], align 1
+; CHECK-NEXT:    ret i1 false
+;
+common.ret1:
+  %.lobit = lshr i32 %0, 31
+  %1 = trunc nuw i32 %.lobit to i1
+  %2 = icmp slt i32 %0, 0
+  %not. = xor i1 %1, true
+  %common.ret1.op = select i1 %not., i1 %2, i1 false
+  store i32 %.lobit, ptr %p, align 1
+  ret i1 %common.ret1.op
+}
+
+attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(argmem: write) }
+



More information about the llvm-commits mailing list