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

via llvm-commits llvm-commits at lists.llvm.org
Fri Jan 9 08:25:48 PST 2026


Author: Sayan Sivakumaran
Date: 2026-01-09T17:25:43+01:00
New Revision: 9e1a185be463eba0808f30cc4a9521ed1f38b538

URL: https://github.com/llvm/llvm-project/commit/9e1a185be463eba0808f30cc4a9521ed1f38b538
DIFF: https://github.com/llvm/llvm-project/commit/9e1a185be463eba0808f30cc4a9521ed1f38b538.diff

LOG: [InstCombine] Fold intrinsics over multi-use selects when the intrinsic is the only user (#172723)

Closes #172176.

Previously, `FoldOpIntoSelect` wouldn't fold multi-use selects if
`MultiUse` wasn't explicitly true. This prevents useful folding when the
select is used multiple times in the same intrinsic call. Similar to
what is done in `foldOpIntoPhi`, we'll now check that all of the uses
come from a single user, rather than checking that there is only one
use.

Added: 
    

Modified: 
    llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
    llvm/test/Transforms/InstCombine/intrinsic-select.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
index d84d26df9bd20..a4c92c75cbd9d 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 38cf02dc2a795..c8f5a216ed534 100644
--- a/llvm/test/Transforms/InstCombine/intrinsic-select.ll
+++ b/llvm/test/Transforms/InstCombine/intrinsic-select.ll
@@ -426,3 +426,109 @@ 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:    [[RET:%.*]] = select i1 [[TMP0:%.*]], i8 2, 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_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:    [[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>
+  %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:    [[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>
+  %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:    [[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>
+  %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:    [[RET:%.*]] = select i1 [[TMP0:%.*]], i8 2, 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_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:    [[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>
+  %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:    [[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>
+  %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:    [[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>
+  %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
+}


        


More information about the llvm-commits mailing list