[llvm] [InstCombine] Recursively replace condition with constant in select arms (PR #120011)
Yingwei Zheng via llvm-commits
llvm-commits at lists.llvm.org
Sun Dec 15 23:28:11 PST 2024
https://github.com/dtcxzyw updated https://github.com/llvm/llvm-project/pull/120011
>From ef8e9f7ae59db389ffe58e22f34620ff65c2e545 Mon Sep 17 00:00:00 2001
From: Yingwei Zheng <dtcxzyw2333 at gmail.com>
Date: Sun, 15 Dec 2024 23:13:09 +0800
Subject: [PATCH 1/3] [InstCombine] Add pre-commit tests. NFC.
---
llvm/test/Transforms/InstCombine/select.ll | 70 ++++++++++++++++++++++
1 file changed, 70 insertions(+)
diff --git a/llvm/test/Transforms/InstCombine/select.ll b/llvm/test/Transforms/InstCombine/select.ll
index 82c079d681284d..b2c8be5a57ac02 100644
--- a/llvm/test/Transforms/InstCombine/select.ll
+++ b/llvm/test/Transforms/InstCombine/select.ll
@@ -4766,3 +4766,73 @@ define i32 @sel_extractvalue_simplify(i1 %c, { i32, i32 } %agg1, i32 %x, i32 %y)
%res = extractvalue { i32, i32 } %sel, 1
ret i32 %res
}
+
+define i1 @replace_select_cond_true(i1 %cond, i32 %v1, i32 %v2, i32 %v3) {
+; CHECK-LABEL: @replace_select_cond_true(
+; CHECK-NEXT: [[SEL:%.*]] = select i1 [[COND:%.*]], i32 [[V1:%.*]], i32 [[V3:%.*]]
+; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[SEL]], [[V2:%.*]]
+; CHECK-NEXT: [[AND:%.*]] = select i1 [[COND]], i1 [[CMP]], i1 false
+; CHECK-NEXT: ret i1 [[AND]]
+;
+ %sel = select i1 %cond, i32 %v1, i32 %v3
+ %cmp = icmp eq i32 %sel, %v2
+ %and = select i1 %cond, i1 %cmp, i1 false
+ ret i1 %and
+}
+
+define i1 @replace_select_cond_false(i1 %cond, i32 %v1, i32 %v2, i32 %v3) {
+; CHECK-LABEL: @replace_select_cond_false(
+; CHECK-NEXT: [[SEL:%.*]] = select i1 [[COND:%.*]], i32 [[V1:%.*]], i32 [[V3:%.*]]
+; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[SEL]], [[V2:%.*]]
+; CHECK-NEXT: [[OR:%.*]] = select i1 [[COND]], i1 true, i1 [[CMP]]
+; CHECK-NEXT: ret i1 [[OR]]
+;
+ %sel = select i1 %cond, i32 %v1, i32 %v3
+ %cmp = icmp eq i32 %sel, %v2
+ %or = select i1 %cond, i1 true, i1 %cmp
+ ret i1 %or
+}
+
+define i32 @replace_and_cond(i1 %cond1, i1 %cond2) {
+; CHECK-LABEL: @replace_and_cond(
+; CHECK-NEXT: [[AND:%.*]] = and i1 [[COND1:%.*]], [[COND2:%.*]]
+; CHECK-NEXT: [[SEL:%.*]] = select i1 [[AND]], i32 3, i32 2
+; CHECK-NEXT: [[MUX:%.*]] = select i1 [[COND1]], i32 [[SEL]], i32 1
+; CHECK-NEXT: ret i32 [[MUX]]
+;
+ %and = and i1 %cond1, %cond2
+ %sel = select i1 %and, i32 3, i32 2
+ %mux = select i1 %cond1, i32 %sel, i32 1
+ ret i32 %mux
+}
+
+; TODO: We can still replace the use of %and with %cond2
+define i32 @replace_and_cond_multiuse1(i1 %cond1, i1 %cond2) {
+; CHECK-LABEL: @replace_and_cond_multiuse1(
+; CHECK-NEXT: [[AND:%.*]] = and i1 [[COND1:%.*]], [[COND2:%.*]]
+; CHECK-NEXT: call void @use(i1 [[AND]])
+; CHECK-NEXT: [[SEL:%.*]] = select i1 [[AND]], i32 3, i32 2
+; CHECK-NEXT: [[MUX:%.*]] = select i1 [[COND1]], i32 [[SEL]], i32 1
+; CHECK-NEXT: ret i32 [[MUX]]
+;
+ %and = and i1 %cond1, %cond2
+ call void @use(i1 %and)
+ %sel = select i1 %and, i32 3, i32 2
+ %mux = select i1 %cond1, i32 %sel, i32 1
+ ret i32 %mux
+}
+
+define i32 @replace_and_cond_multiuse2(i1 %cond1, i1 %cond2) {
+; CHECK-LABEL: @replace_and_cond_multiuse2(
+; CHECK-NEXT: [[AND:%.*]] = and i1 [[COND1:%.*]], [[COND2:%.*]]
+; CHECK-NEXT: [[SEL:%.*]] = select i1 [[AND]], i32 3, i32 2
+; CHECK-NEXT: call void @use32(i32 [[SEL]])
+; CHECK-NEXT: [[MUX:%.*]] = select i1 [[COND1]], i32 [[SEL]], i32 1
+; CHECK-NEXT: ret i32 [[MUX]]
+;
+ %and = and i1 %cond1, %cond2
+ %sel = select i1 %and, i32 3, i32 2
+ call void @use32(i32 %sel)
+ %mux = select i1 %cond1, i32 %sel, i32 1
+ ret i32 %mux
+}
>From a85cd48584692405a2beecae29f8a035c5a32767 Mon Sep 17 00:00:00 2001
From: Yingwei Zheng <dtcxzyw2333 at gmail.com>
Date: Sun, 15 Dec 2024 23:34:07 +0800
Subject: [PATCH 2/3] [InstCombine] Replace condition with constant in the
select arms
---
.../InstCombine/InstCombineSelect.cpp | 6 +++++
llvm/test/Transforms/InstCombine/select.ll | 25 ++++++++-----------
2 files changed, 16 insertions(+), 15 deletions(-)
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
index 50dfb58cadb17b..5eebfdb774d2e2 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
@@ -3800,6 +3800,12 @@ Instruction *InstCombinerImpl::visitSelectInst(SelectInst &SI) {
ConstantInt::getFalse(CondType), SQ,
/* AllowRefinement */ true))
return replaceOperand(SI, 2, S);
+
+ if (replaceInInstruction(TrueVal, CondVal,
+ ConstantInt::getTrue(CondType)) ||
+ replaceInInstruction(FalseVal, CondVal,
+ ConstantInt::getFalse(CondType)))
+ return &SI;
}
if (Instruction *R = foldSelectOfBools(SI))
diff --git a/llvm/test/Transforms/InstCombine/select.ll b/llvm/test/Transforms/InstCombine/select.ll
index b2c8be5a57ac02..48af18b00ff5a7 100644
--- a/llvm/test/Transforms/InstCombine/select.ll
+++ b/llvm/test/Transforms/InstCombine/select.ll
@@ -4580,9 +4580,8 @@ define i32 @src_select_xxory_eq0_xorxy_y(i32 %x, i32 %y) {
define i32 @sequence_select_with_same_cond_false(i1 %c1, i1 %c2){
; CHECK-LABEL: @sequence_select_with_same_cond_false(
-; CHECK-NEXT: [[S1:%.*]] = select i1 [[C1:%.*]], i32 23, i32 45
-; CHECK-NEXT: [[S2:%.*]] = select i1 [[C2:%.*]], i32 666, i32 [[S1]]
-; CHECK-NEXT: [[S3:%.*]] = select i1 [[C1]], i32 789, i32 [[S2]]
+; CHECK-NEXT: [[S2:%.*]] = select i1 [[C2:%.*]], i32 666, i32 45
+; CHECK-NEXT: [[S3:%.*]] = select i1 [[C1:%.*]], i32 789, i32 [[S2]]
; CHECK-NEXT: ret i32 [[S3]]
;
%s1 = select i1 %c1, i32 23, i32 45
@@ -4593,9 +4592,8 @@ define i32 @sequence_select_with_same_cond_false(i1 %c1, i1 %c2){
define i32 @sequence_select_with_same_cond_true(i1 %c1, i1 %c2){
; CHECK-LABEL: @sequence_select_with_same_cond_true(
-; CHECK-NEXT: [[S1:%.*]] = select i1 [[C1:%.*]], i32 45, i32 23
-; CHECK-NEXT: [[S2:%.*]] = select i1 [[C2:%.*]], i32 [[S1]], i32 666
-; CHECK-NEXT: [[S3:%.*]] = select i1 [[C1]], i32 [[S2]], i32 789
+; CHECK-NEXT: [[S2:%.*]] = select i1 [[C2:%.*]], i32 45, i32 666
+; CHECK-NEXT: [[S3:%.*]] = select i1 [[C1:%.*]], i32 [[S2]], i32 789
; CHECK-NEXT: ret i32 [[S3]]
;
%s1 = select i1 %c1, i32 45, i32 23
@@ -4769,9 +4767,8 @@ define i32 @sel_extractvalue_simplify(i1 %c, { i32, i32 } %agg1, i32 %x, i32 %y)
define i1 @replace_select_cond_true(i1 %cond, i32 %v1, i32 %v2, i32 %v3) {
; CHECK-LABEL: @replace_select_cond_true(
-; CHECK-NEXT: [[SEL:%.*]] = select i1 [[COND:%.*]], i32 [[V1:%.*]], i32 [[V3:%.*]]
-; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[SEL]], [[V2:%.*]]
-; CHECK-NEXT: [[AND:%.*]] = select i1 [[COND]], i1 [[CMP]], i1 false
+; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[SEL:%.*]], [[V2:%.*]]
+; CHECK-NEXT: [[AND:%.*]] = select i1 [[COND:%.*]], i1 [[CMP]], i1 false
; CHECK-NEXT: ret i1 [[AND]]
;
%sel = select i1 %cond, i32 %v1, i32 %v3
@@ -4782,9 +4779,8 @@ define i1 @replace_select_cond_true(i1 %cond, i32 %v1, i32 %v2, i32 %v3) {
define i1 @replace_select_cond_false(i1 %cond, i32 %v1, i32 %v2, i32 %v3) {
; CHECK-LABEL: @replace_select_cond_false(
-; CHECK-NEXT: [[SEL:%.*]] = select i1 [[COND:%.*]], i32 [[V1:%.*]], i32 [[V3:%.*]]
-; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[SEL]], [[V2:%.*]]
-; CHECK-NEXT: [[OR:%.*]] = select i1 [[COND]], i1 true, i1 [[CMP]]
+; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[SEL:%.*]], [[V2:%.*]]
+; CHECK-NEXT: [[OR:%.*]] = select i1 [[COND:%.*]], i1 true, i1 [[CMP]]
; CHECK-NEXT: ret i1 [[OR]]
;
%sel = select i1 %cond, i32 %v1, i32 %v3
@@ -4795,9 +4791,8 @@ define i1 @replace_select_cond_false(i1 %cond, i32 %v1, i32 %v2, i32 %v3) {
define i32 @replace_and_cond(i1 %cond1, i1 %cond2) {
; CHECK-LABEL: @replace_and_cond(
-; CHECK-NEXT: [[AND:%.*]] = and i1 [[COND1:%.*]], [[COND2:%.*]]
-; CHECK-NEXT: [[SEL:%.*]] = select i1 [[AND]], i32 3, i32 2
-; CHECK-NEXT: [[MUX:%.*]] = select i1 [[COND1]], i32 [[SEL]], i32 1
+; CHECK-NEXT: [[SEL:%.*]] = select i1 [[AND:%.*]], i32 3, i32 2
+; CHECK-NEXT: [[MUX:%.*]] = select i1 [[COND1:%.*]], i32 [[SEL]], i32 1
; CHECK-NEXT: ret i32 [[MUX]]
;
%and = and i1 %cond1, %cond2
>From 6902d2a9e685b6311419c41f802beeb81a01533a Mon Sep 17 00:00:00 2001
From: Yingwei Zheng <dtcxzyw2333 at gmail.com>
Date: Mon, 16 Dec 2024 15:20:23 +0800
Subject: [PATCH 3/3] [InstCombine] Add vec tests. NFC.
---
llvm/test/Transforms/InstCombine/select.ll | 12 ++++++++++++
1 file changed, 12 insertions(+)
diff --git a/llvm/test/Transforms/InstCombine/select.ll b/llvm/test/Transforms/InstCombine/select.ll
index 48af18b00ff5a7..0168a804239a89 100644
--- a/llvm/test/Transforms/InstCombine/select.ll
+++ b/llvm/test/Transforms/InstCombine/select.ll
@@ -4801,6 +4801,18 @@ define i32 @replace_and_cond(i1 %cond1, i1 %cond2) {
ret i32 %mux
}
+define <2 x i32> @replace_and_cond_vec(<2 x i1> %cond1, <2 x i1> %cond2) {
+; CHECK-LABEL: @replace_and_cond_vec(
+; CHECK-NEXT: [[SEL:%.*]] = select <2 x i1> [[COND2:%.*]], <2 x i32> splat (i32 3), <2 x i32> splat (i32 2)
+; CHECK-NEXT: [[MUX:%.*]] = select <2 x i1> [[COND1:%.*]], <2 x i32> [[SEL]], <2 x i32> splat (i32 1)
+; CHECK-NEXT: ret <2 x i32> [[MUX]]
+;
+ %and = and <2 x i1> %cond1, %cond2
+ %sel = select <2 x i1> %and, <2 x i32> splat(i32 3), <2 x i32> splat(i32 2)
+ %mux = select <2 x i1> %cond1, <2 x i32> %sel, <2 x i32> splat(i32 1)
+ ret <2 x i32> %mux
+}
+
; TODO: We can still replace the use of %and with %cond2
define i32 @replace_and_cond_multiuse1(i1 %cond1, i1 %cond2) {
; CHECK-LABEL: @replace_and_cond_multiuse1(
More information about the llvm-commits
mailing list