[llvm] Fold intrinsics over multi-use selects when the intrinsic is the only user (PR #172723)

Sayan Sivakumaran via llvm-commits llvm-commits at lists.llvm.org
Thu Jan 1 12:13:09 PST 2026


https://github.com/sivakusayan updated https://github.com/llvm/llvm-project/pull/172723

>From 0f566114b24ec3714cdd3a03fa2f6656194c96e0 Mon Sep 17 00:00:00 2001
From: Sayan Sivakumaran <sivakusayan at gmail.com>
Date: Thu, 1 Jan 2026 13:13:32 -0600
Subject: [PATCH 1/2] Precommit tests

---
 .../InstCombine/intrinsic-select.ll           | 132 ++++++++++++++++++
 1 file changed, 132 insertions(+)

diff --git a/llvm/test/Transforms/InstCombine/intrinsic-select.ll b/llvm/test/Transforms/InstCombine/intrinsic-select.ll
index 38cf02dc2a795..3094b9f92eadb 100644
--- a/llvm/test/Transforms/InstCombine/intrinsic-select.ll
+++ b/llvm/test/Transforms/InstCombine/intrinsic-select.ll
@@ -426,3 +426,135 @@ define { <4 x float>, <4 x float> } @test_select_of_sincos_intrinsic_operand(<4
   %result = call { <4 x float>, <4 x float> } @llvm.sincos.v4f32(<4 x float> %s)
   ret { <4 x float>, <4 x float> } %result
 }
+
+define i8 @test_select_rotate_left(i1 %0) {
+; CHECK-LABEL: @test_select_rotate_left(
+; CHECK-NEXT:    [[S:%.*]] = select i1 [[TMP0:%.*]], i8 1, i8 -1
+; CHECK-NEXT:    [[RET:%.*]] = call i8 @llvm.fshl.i8(i8 [[S]], i8 [[S]], i8 1)
+; CHECK-NEXT:    ret i8 [[RET]]
+;
+  %s = select i1 %0, i8 1, i8 -1
+  %ret = call i8 @llvm.fshl.i8(i8 %s, i8 %s, i8 1)
+  ret i8 %ret
+}
+
+define i8 @test_select_rotate_left_poison(i1 %0) {
+; CHECK-LABEL: @test_select_rotate_left_poison(
+; CHECK-NEXT:    ret i8 -1
+;
+  %s = select i1 %0, i8 poison, i8 -1
+  %ret = call i8 @llvm.fshl.i8(i8 %s, i8 %s, i8 1)
+  ret i8 %ret
+}
+
+define i8 @test_select_rotate_left_multiuser(i1 %0) {
+; CHECK-LABEL: @test_select_rotate_left_multiuser(
+; CHECK-NEXT:    [[S:%.*]] = select i1 [[TMP0:%.*]], i8 1, i8 -1
+; CHECK-NEXT:    [[RET:%.*]] = call i8 @llvm.fshl.i8(i8 [[S]], i8 [[S]], i8 1)
+; CHECK-NEXT:    call void @use(i8 [[S]])
+; CHECK-NEXT:    ret i8 [[RET]]
+;
+  %s = select i1 %0, i8 1, i8 -1
+  %ret = call i8 @llvm.fshl.i8(i8 %s, i8 %s, i8 1)
+  call void @use(i8 %s)
+  ret i8 %ret
+}
+
+define <2 x i8> @test_select_rotate_left_splat(i1 %0) {
+; CHECK-LABEL: @test_select_rotate_left_splat(
+; CHECK-NEXT:    [[S:%.*]] = select i1 [[TMP0:%.*]], <2 x i8> splat (i8 1), <2 x i8> splat (i8 -1)
+; CHECK-NEXT:    [[RET:%.*]] = call <2 x i8> @llvm.fshl.v2i8(<2 x i8> [[S]], <2 x i8> [[S]], <2 x i8> splat (i8 1))
+; CHECK-NEXT:    ret <2 x i8> [[RET]]
+;
+  %s = select i1 %0, <2 x i8> <i8 1, i8 1>, <2 x i8> <i8 -1, i8 -1>
+  %ret = call <2 x i8> @llvm.fshl.v2i8(<2 x i8> %s, <2 x i8> %s, <2 x i8> <i8 1, i8 1>)
+  ret <2 x i8> %ret
+}
+
+define <2 x i8> @test_select_rotate_left_splat_poison(i1 %0) {
+; CHECK-LABEL: @test_select_rotate_left_splat_poison(
+; CHECK-NEXT:    [[S:%.*]] = select i1 [[TMP0:%.*]], <2 x i8> <i8 1, i8 poison>, <2 x i8> splat (i8 -1)
+; CHECK-NEXT:    [[RET:%.*]] = call <2 x i8> @llvm.fshl.v2i8(<2 x i8> [[S]], <2 x i8> [[S]], <2 x i8> splat (i8 1))
+; CHECK-NEXT:    ret <2 x i8> [[RET]]
+;
+  %s = select i1 %0, <2 x i8> <i8 1, i8 poison>, <2 x i8> <i8 -1, i8 -1>
+  %ret = call <2 x i8> @llvm.fshl.v2i8(<2 x i8> %s, <2 x i8> %s, <2 x i8> <i8 1, i8 1>)
+  ret <2 x i8> %ret
+}
+
+define <2 x i8> @test_select_rotate_left_non_splat(i1 %0) {
+; CHECK-LABEL: @test_select_rotate_left_non_splat(
+; CHECK-NEXT:    [[S:%.*]] = select i1 [[TMP0:%.*]], <2 x i8> <i8 1, i8 2>, <2 x i8> <i8 -1, i8 -2>
+; CHECK-NEXT:    [[RET:%.*]] = call <2 x i8> @llvm.fshl.v2i8(<2 x i8> [[S]], <2 x i8> [[S]], <2 x i8> splat (i8 1))
+; CHECK-NEXT:    ret <2 x i8> [[RET]]
+;
+  %s = select i1 %0, <2 x i8> <i8 1, i8 2>, <2 x i8> <i8 -1, i8 -2>
+  %ret = call <2 x i8> @llvm.fshl.v2i8(<2 x i8> %s, <2 x i8> %s, <2 x i8> <i8 1, i8 1>)
+  ret <2 x i8> %ret
+}
+
+define i8 @test_select_rotate_right(i1 %0) {
+; CHECK-LABEL: @test_select_rotate_right(
+; CHECK-NEXT:    [[S:%.*]] = select i1 [[TMP0:%.*]], i8 1, i8 -1
+; CHECK-NEXT:    [[RET:%.*]] = call i8 @llvm.fshl.i8(i8 [[S]], i8 [[S]], i8 1)
+; CHECK-NEXT:    ret i8 [[RET]]
+;
+  %s = select i1 %0, i8 1, i8 -1
+  %ret = call i8 @llvm.fshr.i8(i8 %s, i8 %s, i8 7)
+  ret i8 %ret
+}
+
+define i8 @test_select_rotate_right_poison_args(i1 %0) {
+; CHECK-LABEL: @test_select_rotate_right_poison_args(
+; CHECK-NEXT:    ret i8 -1
+;
+  %s = select i1 %0, i8 poison, i8 -1
+  %ret = call i8 @llvm.fshr.i8(i8 %s, i8 %s, i8 7)
+  ret i8 %ret
+}
+
+define i8 @test_select_rotate_right_multiuser(i1 %0) {
+; CHECK-LABEL: @test_select_rotate_right_multiuser(
+; CHECK-NEXT:    [[S:%.*]] = select i1 [[TMP0:%.*]], i8 1, i8 -1
+; CHECK-NEXT:    [[RET:%.*]] = call i8 @llvm.fshl.i8(i8 [[S]], i8 [[S]], i8 1)
+; CHECK-NEXT:    call void @use(i8 [[S]])
+; CHECK-NEXT:    ret i8 [[RET]]
+;
+  %s = select i1 %0, i8 1, i8 -1
+  %ret = call i8 @llvm.fshr.i8(i8 %s, i8 %s, i8 7)
+  call void @use(i8 %s)
+  ret i8 %ret
+}
+
+define <2 x i8> @test_select_rotate_right_splat(i1 %0) {
+; CHECK-LABEL: @test_select_rotate_right_splat(
+; CHECK-NEXT:    [[S:%.*]] = select i1 [[TMP0:%.*]], <2 x i8> splat (i8 1), <2 x i8> splat (i8 -1)
+; CHECK-NEXT:    [[RET:%.*]] = call <2 x i8> @llvm.fshl.v2i8(<2 x i8> [[S]], <2 x i8> [[S]], <2 x i8> splat (i8 1))
+; CHECK-NEXT:    ret <2 x i8> [[RET]]
+;
+  %s = select i1 %0, <2 x i8> <i8 1, i8 1>, <2 x i8> <i8 -1, i8 -1>
+  %ret = call <2 x i8> @llvm.fshr.v2i8(<2 x i8> %s, <2 x i8> %s, <2 x i8> <i8 7, i8 7>)
+  ret <2 x i8> %ret
+}
+
+define <2 x i8> @test_select_rotate_right_splat_poison(i1 %0) {
+; CHECK-LABEL: @test_select_rotate_right_splat_poison(
+; CHECK-NEXT:    [[S:%.*]] = select i1 [[TMP0:%.*]], <2 x i8> <i8 1, i8 poison>, <2 x i8> splat (i8 -1)
+; CHECK-NEXT:    [[RET:%.*]] = call <2 x i8> @llvm.fshl.v2i8(<2 x i8> [[S]], <2 x i8> [[S]], <2 x i8> splat (i8 1))
+; CHECK-NEXT:    ret <2 x i8> [[RET]]
+;
+  %s = select i1 %0, <2 x i8> <i8 1, i8 poison>, <2 x i8> <i8 -1, i8 -1>
+  %ret = call <2 x i8> @llvm.fshr.v2i8(<2 x i8> %s, <2 x i8> %s, <2 x i8> <i8 7, i8 7>)
+  ret <2 x i8> %ret
+}
+
+define <2 x i8> @test_select_rotate_right_non_splat(i1 %0) {
+; CHECK-LABEL: @test_select_rotate_right_non_splat(
+; CHECK-NEXT:    [[S:%.*]] = select i1 [[TMP0:%.*]], <2 x i8> <i8 1, i8 2>, <2 x i8> <i8 -1, i8 -2>
+; CHECK-NEXT:    [[RET:%.*]] = call <2 x i8> @llvm.fshl.v2i8(<2 x i8> [[S]], <2 x i8> [[S]], <2 x i8> splat (i8 1))
+; CHECK-NEXT:    ret <2 x i8> [[RET]]
+;
+  %s = select i1 %0, <2 x i8> <i8 1, i8 2>, <2 x i8> <i8 -1, i8 -2>
+  %ret = call <2 x i8> @llvm.fshr.v2i8(<2 x i8> %s, <2 x i8> %s, <2 x i8> <i8 7, i8 7>)
+  ret <2 x i8> %ret
+}

>From 6055b40f921d2c2687a756c1fe5bca5373c17813 Mon Sep 17 00:00:00 2001
From: Sayan Sivakumaran <sivakusayan at gmail.com>
Date: Thu, 1 Jan 2026 14:12:39 -0600
Subject: [PATCH 2/2] Allow folding intrinsics into select when its the only
 user

---
 .../InstCombine/InstructionCombining.cpp      |  2 +-
 .../InstCombine/intrinsic-select.ll           | 24 +++++++------------
 2 files changed, 9 insertions(+), 17 deletions(-)

diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
index 14244236d75d5..2272cb868160f 100644
--- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -1783,7 +1783,7 @@ Instruction *InstCombinerImpl::FoldOpIntoSelect(Instruction &Op, SelectInst *SI,
                                                 bool FoldWithMultiUse,
                                                 bool SimplifyBothArms) {
   // Don't modify shared select instructions unless set FoldWithMultiUse
-  if (!SI->hasOneUse() && !FoldWithMultiUse)
+  if (!SI->hasOneUser() && !FoldWithMultiUse)
     return nullptr;
 
   Value *TV = SI->getTrueValue();
diff --git a/llvm/test/Transforms/InstCombine/intrinsic-select.ll b/llvm/test/Transforms/InstCombine/intrinsic-select.ll
index 3094b9f92eadb..ac2ad1f9837a2 100644
--- a/llvm/test/Transforms/InstCombine/intrinsic-select.ll
+++ b/llvm/test/Transforms/InstCombine/intrinsic-select.ll
@@ -429,8 +429,7 @@ define { <4 x float>, <4 x float> } @test_select_of_sincos_intrinsic_operand(<4
 
 define i8 @test_select_rotate_left(i1 %0) {
 ; CHECK-LABEL: @test_select_rotate_left(
-; CHECK-NEXT:    [[S:%.*]] = select i1 [[TMP0:%.*]], i8 1, i8 -1
-; CHECK-NEXT:    [[RET:%.*]] = call i8 @llvm.fshl.i8(i8 [[S]], i8 [[S]], i8 1)
+; CHECK-NEXT:    [[RET:%.*]] = select i1 [[TMP0:%.*]], i8 2, i8 -1
 ; CHECK-NEXT:    ret i8 [[RET]]
 ;
   %s = select i1 %0, i8 1, i8 -1
@@ -462,8 +461,7 @@ define i8 @test_select_rotate_left_multiuser(i1 %0) {
 
 define <2 x i8> @test_select_rotate_left_splat(i1 %0) {
 ; CHECK-LABEL: @test_select_rotate_left_splat(
-; CHECK-NEXT:    [[S:%.*]] = select i1 [[TMP0:%.*]], <2 x i8> splat (i8 1), <2 x i8> splat (i8 -1)
-; CHECK-NEXT:    [[RET:%.*]] = call <2 x i8> @llvm.fshl.v2i8(<2 x i8> [[S]], <2 x i8> [[S]], <2 x i8> splat (i8 1))
+; CHECK-NEXT:    [[RET:%.*]] = select i1 [[TMP0:%.*]], <2 x i8> splat (i8 2), <2 x i8> splat (i8 -1)
 ; CHECK-NEXT:    ret <2 x i8> [[RET]]
 ;
   %s = select i1 %0, <2 x i8> <i8 1, i8 1>, <2 x i8> <i8 -1, i8 -1>
@@ -473,8 +471,7 @@ define <2 x i8> @test_select_rotate_left_splat(i1 %0) {
 
 define <2 x i8> @test_select_rotate_left_splat_poison(i1 %0) {
 ; CHECK-LABEL: @test_select_rotate_left_splat_poison(
-; CHECK-NEXT:    [[S:%.*]] = select i1 [[TMP0:%.*]], <2 x i8> <i8 1, i8 poison>, <2 x i8> splat (i8 -1)
-; CHECK-NEXT:    [[RET:%.*]] = call <2 x i8> @llvm.fshl.v2i8(<2 x i8> [[S]], <2 x i8> [[S]], <2 x i8> splat (i8 1))
+; CHECK-NEXT:    [[RET:%.*]] = select i1 [[TMP0:%.*]], <2 x i8> <i8 2, i8 poison>, <2 x i8> splat (i8 -1)
 ; CHECK-NEXT:    ret <2 x i8> [[RET]]
 ;
   %s = select i1 %0, <2 x i8> <i8 1, i8 poison>, <2 x i8> <i8 -1, i8 -1>
@@ -484,8 +481,7 @@ define <2 x i8> @test_select_rotate_left_splat_poison(i1 %0) {
 
 define <2 x i8> @test_select_rotate_left_non_splat(i1 %0) {
 ; CHECK-LABEL: @test_select_rotate_left_non_splat(
-; CHECK-NEXT:    [[S:%.*]] = select i1 [[TMP0:%.*]], <2 x i8> <i8 1, i8 2>, <2 x i8> <i8 -1, i8 -2>
-; CHECK-NEXT:    [[RET:%.*]] = call <2 x i8> @llvm.fshl.v2i8(<2 x i8> [[S]], <2 x i8> [[S]], <2 x i8> splat (i8 1))
+; CHECK-NEXT:    [[RET:%.*]] = select i1 [[TMP0:%.*]], <2 x i8> <i8 2, i8 4>, <2 x i8> <i8 -1, i8 -3>
 ; CHECK-NEXT:    ret <2 x i8> [[RET]]
 ;
   %s = select i1 %0, <2 x i8> <i8 1, i8 2>, <2 x i8> <i8 -1, i8 -2>
@@ -495,8 +491,7 @@ define <2 x i8> @test_select_rotate_left_non_splat(i1 %0) {
 
 define i8 @test_select_rotate_right(i1 %0) {
 ; CHECK-LABEL: @test_select_rotate_right(
-; CHECK-NEXT:    [[S:%.*]] = select i1 [[TMP0:%.*]], i8 1, i8 -1
-; CHECK-NEXT:    [[RET:%.*]] = call i8 @llvm.fshl.i8(i8 [[S]], i8 [[S]], i8 1)
+; CHECK-NEXT:    [[RET:%.*]] = select i1 [[TMP0:%.*]], i8 2, i8 -1
 ; CHECK-NEXT:    ret i8 [[RET]]
 ;
   %s = select i1 %0, i8 1, i8 -1
@@ -528,8 +523,7 @@ define i8 @test_select_rotate_right_multiuser(i1 %0) {
 
 define <2 x i8> @test_select_rotate_right_splat(i1 %0) {
 ; CHECK-LABEL: @test_select_rotate_right_splat(
-; CHECK-NEXT:    [[S:%.*]] = select i1 [[TMP0:%.*]], <2 x i8> splat (i8 1), <2 x i8> splat (i8 -1)
-; CHECK-NEXT:    [[RET:%.*]] = call <2 x i8> @llvm.fshl.v2i8(<2 x i8> [[S]], <2 x i8> [[S]], <2 x i8> splat (i8 1))
+; CHECK-NEXT:    [[RET:%.*]] = select i1 [[TMP0:%.*]], <2 x i8> splat (i8 2), <2 x i8> splat (i8 -1)
 ; CHECK-NEXT:    ret <2 x i8> [[RET]]
 ;
   %s = select i1 %0, <2 x i8> <i8 1, i8 1>, <2 x i8> <i8 -1, i8 -1>
@@ -539,8 +533,7 @@ define <2 x i8> @test_select_rotate_right_splat(i1 %0) {
 
 define <2 x i8> @test_select_rotate_right_splat_poison(i1 %0) {
 ; CHECK-LABEL: @test_select_rotate_right_splat_poison(
-; CHECK-NEXT:    [[S:%.*]] = select i1 [[TMP0:%.*]], <2 x i8> <i8 1, i8 poison>, <2 x i8> splat (i8 -1)
-; CHECK-NEXT:    [[RET:%.*]] = call <2 x i8> @llvm.fshl.v2i8(<2 x i8> [[S]], <2 x i8> [[S]], <2 x i8> splat (i8 1))
+; CHECK-NEXT:    [[RET:%.*]] = select i1 [[TMP0:%.*]], <2 x i8> <i8 2, i8 poison>, <2 x i8> splat (i8 -1)
 ; CHECK-NEXT:    ret <2 x i8> [[RET]]
 ;
   %s = select i1 %0, <2 x i8> <i8 1, i8 poison>, <2 x i8> <i8 -1, i8 -1>
@@ -550,8 +543,7 @@ define <2 x i8> @test_select_rotate_right_splat_poison(i1 %0) {
 
 define <2 x i8> @test_select_rotate_right_non_splat(i1 %0) {
 ; CHECK-LABEL: @test_select_rotate_right_non_splat(
-; CHECK-NEXT:    [[S:%.*]] = select i1 [[TMP0:%.*]], <2 x i8> <i8 1, i8 2>, <2 x i8> <i8 -1, i8 -2>
-; CHECK-NEXT:    [[RET:%.*]] = call <2 x i8> @llvm.fshl.v2i8(<2 x i8> [[S]], <2 x i8> [[S]], <2 x i8> splat (i8 1))
+; CHECK-NEXT:    [[RET:%.*]] = select i1 [[TMP0:%.*]], <2 x i8> <i8 2, i8 4>, <2 x i8> <i8 -1, i8 -3>
 ; CHECK-NEXT:    ret <2 x i8> [[RET]]
 ;
   %s = select i1 %0, <2 x i8> <i8 1, i8 2>, <2 x i8> <i8 -1, i8 -2>



More information about the llvm-commits mailing list