[llvm] [InstCombine] Missed optimization: Fold (sext(a) & sext(c1)) == c2 to (a & c1) == c2 (PR #112646)

Lee Wei via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 24 20:08:53 PDT 2024


https://github.com/leewei05 updated https://github.com/llvm/llvm-project/pull/112646

>From d228da4751f576ba0f7d10cde2ff8a5ab5140bbc Mon Sep 17 00:00:00 2001
From: Lee <lee10202013 at gmail.com>
Date: Sat, 12 Oct 2024 15:13:47 -0600
Subject: [PATCH 1/2] Add pre-commit tests

---
 llvm/test/Transforms/InstCombine/sext-and.ll | 50 ++++++++++++++++++++
 1 file changed, 50 insertions(+)
 create mode 100644 llvm/test/Transforms/InstCombine/sext-and.ll

diff --git a/llvm/test/Transforms/InstCombine/sext-and.ll b/llvm/test/Transforms/InstCombine/sext-and.ll
new file mode 100644
index 00000000000000..c45b9838311d8f
--- /dev/null
+++ b/llvm/test/Transforms/InstCombine/sext-and.ll
@@ -0,0 +1,50 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
+; RUN: opt < %s -passes=instcombine -S | FileCheck %s
+
+declare void @use(i8)
+
+define i1 @fold_sext_to_and(i8 %x) {
+; CHECK-LABEL: define i1 @fold_sext_to_and(
+; CHECK-SAME: i8 [[X:%.*]]) {
+; CHECK-NEXT:    [[TMP1:%.*]] = sext i8 [[X]] to i32
+; CHECK-NEXT:    [[TMP2:%.*]] = and i32 [[TMP1]], -2147483647
+; CHECK-NEXT:    [[TMP3:%.*]] = icmp eq i32 [[TMP2]], 1
+; CHECK-NEXT:    ret i1 [[TMP3]]
+;
+  %1 = sext i8 %x to i32
+  %2 = and i32 %1, -2147483647
+  %3 = icmp eq i32 %2, 1
+  ret i1 %3
+}
+
+define i1 @fold_sext_to_and_multi_use(i8 %x) {
+; CHECK-LABEL: define i1 @fold_sext_to_and_multi_use(
+; CHECK-SAME: i8 [[X:%.*]]) {
+; CHECK-NEXT:    [[TMP1:%.*]] = sext i8 [[X]] to i32
+; CHECK-NEXT:    call void @use(i32 [[TMP1]])
+; CHECK-NEXT:    [[TMP2:%.*]] = and i32 [[TMP1]], -2147483647
+; CHECK-NEXT:    [[TMP3:%.*]] = icmp eq i32 [[TMP2]], 1
+; CHECK-NEXT:    ret i1 [[TMP3]]
+;
+  %1 = sext i8 %x to i32
+  call void @use(i32 %1)
+  %2 = and i32 %1, -2147483647
+  %3 = icmp eq i32 %2, 1
+  ret i1 %3
+}
+
+; Negative tests
+
+define i1 @fold_sext_to_and_wrong(i8 %x) {
+; CHECK-LABEL: define i1 @fold_sext_to_and_wrong(
+; CHECK-SAME: i2 [[X:%.*]]) {
+; CHECK-NEXT:    [[TMP1:%.*]] = sext i2 [[X]] to i32
+; CHECK-NEXT:    [[TMP2:%.*]] = and i32 [[TMP1]], -2147483647
+; CHECK-NEXT:    [[TMP3:%.*]] = icmp eq i32 [[TMP2]], 1
+; CHECK-NEXT:    ret i1 [[TMP3]]
+;
+  %1 = sext i8 %x to i32
+  %2 = and i32 %1, -2147483647
+  %3 = icmp eq i32 %2, -1
+  ret i1 %3
+}

>From c1ee93be52eb3c69936473e65e5625e11c4cf31e Mon Sep 17 00:00:00 2001
From: Lee <lee10202013 at gmail.com>
Date: Thu, 24 Oct 2024 21:06:30 -0600
Subject: [PATCH 2/2] Apply transformation

---
 .../InstCombine/InstCombineCompares.cpp       | 24 +++++++++++++++++++
 llvm/test/Transforms/InstCombine/sext-and.ll  | 16 +++++--------
 2 files changed, 30 insertions(+), 10 deletions(-)

diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
index 18a6fdcec1728e..97fdea2d4e1a07 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -7687,6 +7687,30 @@ Instruction *InstCombinerImpl::visitICmpInst(ICmpInst &I) {
   if (Instruction *Res = foldReductionIdiom(I, Builder, DL))
     return Res;
 
+  {
+    Value *A;
+    const APInt *C1, *C2;
+    ICmpInst::Predicate PredEq = ICmpInst::ICMP_EQ;
+    if (I.getPredicate() == PredEq) {
+      // c2 must be positive
+      // sext(a) & c1 == c2 --> a & c3 == trunc(c2)
+      if (match(Op0, m_And(m_SExtLike(m_Value(A)), m_APInt(C1))) && match(Op1, m_APInt(C2))) {
+        if (C2->isNonNegative()) {
+          Type *InputTy = A->getType();
+          unsigned TargetBitSize = InputTy->getScalarSizeInBits();
+          // Count the number of 1s in C1 high bits of size TargetBitSize.
+          unsigned Leading1sCount = C1->lshr(C1->getBitWidth() - TargetBitSize).popcount();
+          APInt TruncC1 = C1->trunc(TargetBitSize);
+          if (Leading1sCount > 0) {
+            TruncC1.setBit(TargetBitSize - 1);
+          }
+          Value *AndInst = Builder.CreateAnd(A, TruncC1);
+          return new ICmpInst(PredEq, AndInst, ConstantInt::get(InputTy, C2->trunc(TargetBitSize)));
+        }
+      }
+    }
+  }
+
   return Changed ? &I : nullptr;
 }
 
diff --git a/llvm/test/Transforms/InstCombine/sext-and.ll b/llvm/test/Transforms/InstCombine/sext-and.ll
index c45b9838311d8f..661b06b3f43614 100644
--- a/llvm/test/Transforms/InstCombine/sext-and.ll
+++ b/llvm/test/Transforms/InstCombine/sext-and.ll
@@ -6,9 +6,8 @@ declare void @use(i8)
 define i1 @fold_sext_to_and(i8 %x) {
 ; CHECK-LABEL: define i1 @fold_sext_to_and(
 ; CHECK-SAME: i8 [[X:%.*]]) {
-; CHECK-NEXT:    [[TMP1:%.*]] = sext i8 [[X]] to i32
-; CHECK-NEXT:    [[TMP2:%.*]] = and i32 [[TMP1]], -2147483647
-; CHECK-NEXT:    [[TMP3:%.*]] = icmp eq i32 [[TMP2]], 1
+; CHECK-NEXT:    [[TMP1:%.*]] = and i8 [[X]], -127
+; CHECK-NEXT:    [[TMP3:%.*]] = icmp eq i8 [[TMP1]], 1
 ; CHECK-NEXT:    ret i1 [[TMP3]]
 ;
   %1 = sext i8 %x to i32
@@ -22,8 +21,8 @@ define i1 @fold_sext_to_and_multi_use(i8 %x) {
 ; CHECK-SAME: i8 [[X:%.*]]) {
 ; CHECK-NEXT:    [[TMP1:%.*]] = sext i8 [[X]] to i32
 ; CHECK-NEXT:    call void @use(i32 [[TMP1]])
-; CHECK-NEXT:    [[TMP2:%.*]] = and i32 [[TMP1]], -2147483647
-; CHECK-NEXT:    [[TMP3:%.*]] = icmp eq i32 [[TMP2]], 1
+; CHECK-NEXT:    [[TMP2:%.*]] = and i8 [[X]], -127
+; CHECK-NEXT:    [[TMP3:%.*]] = icmp eq i8 [[TMP2]], 1
 ; CHECK-NEXT:    ret i1 [[TMP3]]
 ;
   %1 = sext i8 %x to i32
@@ -37,11 +36,8 @@ define i1 @fold_sext_to_and_multi_use(i8 %x) {
 
 define i1 @fold_sext_to_and_wrong(i8 %x) {
 ; CHECK-LABEL: define i1 @fold_sext_to_and_wrong(
-; CHECK-SAME: i2 [[X:%.*]]) {
-; CHECK-NEXT:    [[TMP1:%.*]] = sext i2 [[X]] to i32
-; CHECK-NEXT:    [[TMP2:%.*]] = and i32 [[TMP1]], -2147483647
-; CHECK-NEXT:    [[TMP3:%.*]] = icmp eq i32 [[TMP2]], 1
-; CHECK-NEXT:    ret i1 [[TMP3]]
+; CHECK-SAME: i8 [[X:%.*]]) {
+; CHECK-NEXT:    ret i1 false
 ;
   %1 = sext i8 %x to i32
   %2 = and i32 %1, -2147483647



More information about the llvm-commits mailing list