[llvm] [InstCombine] Recursively replace condition with constant in select arms (PR #120011)
Matt Arsenault via llvm-commits
llvm-commits at lists.llvm.org
Sun Dec 15 18:55:57 PST 2024
================
@@ -4766,3 +4764,70 @@ 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: [[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: [[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: [[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
+}
----------------
arsenm wrote:
Test a vector case
https://github.com/llvm/llvm-project/pull/120011
More information about the llvm-commits
mailing list