[llvm] [InstCombine] Fold zext(sub(0, trunc(X))) to and(sub(0, X), mask) (PR #207564)

Simon Pilgrim via llvm-commits llvm-commits at lists.llvm.org
Wed Jul 8 03:14:49 PDT 2026


https://github.com/RKSimon updated https://github.com/llvm/llvm-project/pull/207564

>From 10d477c6d15960ea0206ee7cf2e14106d205176d Mon Sep 17 00:00:00 2001
From: Ayaan Khan <ayaanandayaan2 at gmail.com>
Date: Sun, 5 Jul 2026 13:17:41 +0530
Subject: [PATCH 1/3] canonicalize pattern

---
 .../InstCombine/InstCombineCasts.cpp          |  9 +++
 .../Transforms/InstCombine/zext-sub-trunc.ll  | 55 +++++++++++++++++++
 2 files changed, 64 insertions(+)
 create mode 100644 llvm/test/Transforms/InstCombine/zext-sub-trunc.ll

diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
index 7e747ddb9013e..84c143897c8d4 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
@@ -1686,6 +1686,15 @@ Instruction *InstCombinerImpl::visitZExt(ZExtInst &Zext) {
     return BinaryOperator::CreateXor(Builder.CreateAnd(X, ZC), ZC);
   }
 
+  // zext(sub(0, trunc(X))) -> and(sub(0, X), mask)
+  if (match(Src, m_Sub(m_Zero(), m_Trunc(m_Value(X)))) &&
+      X->getType() == DestTy) {
+    APInt Mask = APInt::getLowBitsSet(DestTy->getScalarSizeInBits(),
+                                      SrcTy->getScalarSizeInBits());
+    Value *Neg = Builder.CreateSub(ConstantInt::get(DestTy, 0), X);
+    return BinaryOperator::CreateAnd(Neg, ConstantInt::get(DestTy, Mask));
+  }
+
   // If we are truncating, masking, and then zexting back to the original type,
   // that's just a mask. This is not handled by canEvaluateZextd if the
   // intermediate values have extra uses. This could be generalized further for
diff --git a/llvm/test/Transforms/InstCombine/zext-sub-trunc.ll b/llvm/test/Transforms/InstCombine/zext-sub-trunc.ll
new file mode 100644
index 0000000000000..47c31acc800c5
--- /dev/null
+++ b/llvm/test/Transforms/InstCombine/zext-sub-trunc.ll
@@ -0,0 +1,55 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
+; RUN: opt < %s -passes=instcombine -S | FileCheck %s
+
+define i64 @test_scalar_mask(i64 %a) {
+; CHECK-LABEL: define i64 @test_scalar_mask(
+; CHECK-SAME: i64 [[A:%.*]]) {
+; CHECK-NEXT:    [[TMP1:%.*]] = sub i64 0, [[A]]
+; CHECK-NEXT:    [[ZEXT:%.*]] = and i64 [[TMP1]], 63
+; CHECK-NEXT:    ret i64 [[ZEXT]]
+;
+  %trunc = trunc i64 %a to i6
+  %neg = sub i6 0, %trunc
+  %zext = zext i6 %neg to i64
+  ret i64 %zext
+}
+
+define <8 x i64> @test_vector_mask_v8i64(<8 x i64> %a0) {
+; CHECK-LABEL: define <8 x i64> @test_vector_mask_v8i64(
+; CHECK-SAME: <8 x i64> [[A0:%.*]]) {
+; CHECK-NEXT:    [[TMP1:%.*]] = sub <8 x i64> zeroinitializer, [[A0]]
+; CHECK-NEXT:    [[ZEXT:%.*]] = and <8 x i64> [[TMP1]], splat (i64 63)
+; CHECK-NEXT:    ret <8 x i64> [[ZEXT]]
+;
+  %trunc = trunc <8 x i64> %a0 to <8 x i6>
+  %neg = sub <8 x i6> zeroinitializer, %trunc
+  %zext = zext <8 x i6> %neg to <8 x i64>
+  ret <8 x i64> %zext
+}
+
+define <8 x i32> @test_vector_mask_v8i32(<8 x i32> %a0) {
+; CHECK-LABEL: define <8 x i32> @test_vector_mask_v8i32(
+; CHECK-SAME: <8 x i32> [[A0:%.*]]) {
+; CHECK-NEXT:    [[TMP1:%.*]] = sub <8 x i32> zeroinitializer, [[A0]]
+; CHECK-NEXT:    [[ZEXT:%.*]] = and <8 x i32> [[TMP1]], splat (i32 15)
+; CHECK-NEXT:    ret <8 x i32> [[ZEXT]]
+;
+  %trunc = trunc <8 x i32> %a0 to <8 x i4>
+  %neg = sub <8 x i4> zeroinitializer, %trunc
+  %zext = zext <8 x i4> %neg to <8 x i32>
+  ret <8 x i32> %zext
+}
+
+define <8 x i64> @test_negative_vector_mask_v8i32(<8 x i32> %a0) {
+; CHECK-LABEL: define <8 x i64> @test_negative_vector_mask_v8i32(
+; CHECK-SAME: <8 x i32> [[A0:%.*]]) {
+; CHECK-NEXT:    [[TRUNC:%.*]] = trunc <8 x i32> [[A0]] to <8 x i4>
+; CHECK-NEXT:    [[NEG:%.*]] = sub <8 x i4> zeroinitializer, [[TRUNC]]
+; CHECK-NEXT:    [[ZEXT:%.*]] = zext <8 x i4> [[NEG]] to <8 x i64>
+; CHECK-NEXT:    ret <8 x i64> [[ZEXT]]
+;
+  %trunc = trunc <8 x i32> %a0 to <8 x i4>
+  %neg = sub <8 x i4> zeroinitializer, %trunc
+  %zext = zext <8 x i4> %neg to <8 x i64>
+  ret <8 x i64> %zext
+}

>From 2a0f4bc961f843b95ca1d779b91d97f3749d4ea5 Mon Sep 17 00:00:00 2001
From: Ayaan Khan <ayaanandayaan2 at gmail.com>
Date: Tue, 7 Jul 2026 21:11:20 +0530
Subject: [PATCH 2/3] add tests to rotate.ll

---
 llvm/test/Transforms/InstCombine/rotate.ll | 30 ++++++++++++++++++++++
 1 file changed, 30 insertions(+)

diff --git a/llvm/test/Transforms/InstCombine/rotate.ll b/llvm/test/Transforms/InstCombine/rotate.ll
index a4d4ec375954f..affdb35931268 100644
--- a/llvm/test/Transforms/InstCombine/rotate.ll
+++ b/llvm/test/Transforms/InstCombine/rotate.ll
@@ -1086,3 +1086,33 @@ define i32 @not_rotl_i32_add_less(i32 %x, i32 %y) {
   %r = add i32 %shr, %shl
   ret i32 %r
 }
+
+define <8 x i64> @fold_rot_fshr_v8i64(<8 x i64> %x, <8 x i64> %y) {
+; CHECK-LABEL: @fold_rot_fshr_v8i64(
+; CHECK-NEXT:    [[OR:%.*]] = call <8 x i64> @llvm.fshr.v8i64(<8 x i64> [[X:%.*]], <8 x i64> [[X]], <8 x i64> [[Y:%.*]])
+; CHECK-NEXT:    ret <8 x i64> [[OR]]
+;
+  %trunc = trunc <8 x i64> %y to <8 x i6>
+  %neg = sub <8 x i6> zeroinitializer, %trunc
+  %zext = zext <8 x i6> %neg to <8 x i64>
+  %shl = shl <8 x i64> %x, %zext
+  %mask = and <8 x i64> %y, splat (i64 63)
+  %lshr = lshr <8 x i64> %x, %mask
+  %or = or <8 x i64> %shl, %lshr
+  ret <8 x i64> %or
+}
+
+define i32 @fold_rot_fshl_i32(i32 %x, i32 %y) {
+; CHECK-LABEL: @fold_rot_fshl_i32(
+; CHECK-NEXT:    [[OR:%.*]] = call i32 @llvm.fshl.i32(i32 [[X:%.*]], i32 [[X]], i32 [[Y:%.*]])
+; CHECK-NEXT:    ret i32 [[OR]]
+;
+  %trunc = trunc i32 %y to i5
+  %neg = sub i5 0, %trunc
+  %zext = zext i5 %neg to i32
+  %lshr = lshr i32 %x, %zext
+  %mask = and i32 %y, 31
+  %shl = shl i32 %x, %mask
+  %or = or i32 %shl, %lshr
+  ret i32 %or
+}

>From 3a475de682d1d5f58b0a2a2fadf7ec21db1855c9 Mon Sep 17 00:00:00 2001
From: Ayaan Khan <ayaanandayaan2 at gmail.com>
Date: Wed, 8 Jul 2026 14:16:13 +0530
Subject: [PATCH 3/3] comments

---
 llvm/test/Transforms/InstCombine/rotate.ll         | 2 ++
 llvm/test/Transforms/InstCombine/zext-sub-trunc.ll | 2 ++
 2 files changed, 4 insertions(+)

diff --git a/llvm/test/Transforms/InstCombine/rotate.ll b/llvm/test/Transforms/InstCombine/rotate.ll
index affdb35931268..dd787c0c9d753 100644
--- a/llvm/test/Transforms/InstCombine/rotate.ll
+++ b/llvm/test/Transforms/InstCombine/rotate.ll
@@ -1087,6 +1087,7 @@ define i32 @not_rotl_i32_add_less(i32 %x, i32 %y) {
   ret i32 %r
 }
 
+; PR165306
 define <8 x i64> @fold_rot_fshr_v8i64(<8 x i64> %x, <8 x i64> %y) {
 ; CHECK-LABEL: @fold_rot_fshr_v8i64(
 ; CHECK-NEXT:    [[OR:%.*]] = call <8 x i64> @llvm.fshr.v8i64(<8 x i64> [[X:%.*]], <8 x i64> [[X]], <8 x i64> [[Y:%.*]])
@@ -1102,6 +1103,7 @@ define <8 x i64> @fold_rot_fshr_v8i64(<8 x i64> %x, <8 x i64> %y) {
   ret <8 x i64> %or
 }
 
+; PR165306
 define i32 @fold_rot_fshl_i32(i32 %x, i32 %y) {
 ; CHECK-LABEL: @fold_rot_fshl_i32(
 ; CHECK-NEXT:    [[OR:%.*]] = call i32 @llvm.fshl.i32(i32 [[X:%.*]], i32 [[X]], i32 [[Y:%.*]])
diff --git a/llvm/test/Transforms/InstCombine/zext-sub-trunc.ll b/llvm/test/Transforms/InstCombine/zext-sub-trunc.ll
index 47c31acc800c5..d4eaacbb8631d 100644
--- a/llvm/test/Transforms/InstCombine/zext-sub-trunc.ll
+++ b/llvm/test/Transforms/InstCombine/zext-sub-trunc.ll
@@ -40,6 +40,8 @@ define <8 x i32> @test_vector_mask_v8i32(<8 x i32> %a0) {
   ret <8 x i32> %zext
 }
 
+; This must not be folded because the source and dest types of trunc/zext pair
+; do not match
 define <8 x i64> @test_negative_vector_mask_v8i32(<8 x i32> %a0) {
 ; CHECK-LABEL: define <8 x i64> @test_negative_vector_mask_v8i32(
 ; CHECK-SAME: <8 x i32> [[A0:%.*]]) {



More information about the llvm-commits mailing list