Skip to content

Commit 817d0cb

Browse files
authored
[InstCombine] Simplify commutative compares of symmetric pairs (#80134)
Fixes #78038.
1 parent d9e875d commit 817d0cb

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7055,6 +7055,14 @@ Instruction *InstCombinerImpl::visitICmpInst(ICmpInst &I) {
70557055
foldICmpCommutative(I.getSwappedPredicate(), Op1, Op0, I))
70567056
return Res;
70577057

7058+
if (I.isCommutative()) {
7059+
if (auto Pair = matchSymmetricPair(I.getOperand(0), I.getOperand(1))) {
7060+
replaceOperand(I, 0, Pair->first);
7061+
replaceOperand(I, 1, Pair->second);
7062+
return &I;
7063+
}
7064+
}
7065+
70587066
// In case of a comparison with two select instructions having the same
70597067
// condition, check whether one of the resulting branches can be simplified.
70607068
// If so, just compare the other branch and select the appropriate result.
@@ -7668,6 +7676,14 @@ Instruction *InstCombinerImpl::visitFCmpInst(FCmpInst &I) {
76687676
}
76697677
}
76707678

7679+
if (I.isCommutative()) {
7680+
if (auto Pair = matchSymmetricPair(I.getOperand(0), I.getOperand(1))) {
7681+
replaceOperand(I, 0, Pair->first);
7682+
replaceOperand(I, 1, Pair->second);
7683+
return &I;
7684+
}
7685+
}
7686+
76717687
// If we're just checking for a NaN (ORD/UNO) and have a non-NaN operand,
76727688
// then canonicalize the operand to 0.0.
76737689
if (Pred == CmpInst::FCMP_ORD || Pred == CmpInst::FCMP_UNO) {

llvm/test/Transforms/InstCombine/fcmp-select.ll

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,37 @@ define double @one_swapped(double %x) {
114114
ret double %cond
115115
}
116116

117+
define i1 @fcmp_oeq_select(i1 %cond, float %a, float %b) {
118+
; CHECK-LABEL: @fcmp_oeq_select(
119+
; CHECK-NEXT: [[RES:%.*]] = fcmp oeq float [[A:%.*]], [[B:%.*]]
120+
; CHECK-NEXT: ret i1 [[RES]]
121+
;
122+
%lhs = select i1 %cond, float %a, float %b
123+
%rhs = select i1 %cond, float %b, float %a
124+
%res = fcmp oeq float %lhs, %rhs
125+
ret i1 %res
126+
}
127+
128+
define i1 @fcmp_uno_select(i1 %cond, float %a, float %b) {
129+
; CHECK-LABEL: @fcmp_uno_select(
130+
; CHECK-NEXT: [[RES:%.*]] = fcmp uno float [[A:%.*]], [[B:%.*]]
131+
; CHECK-NEXT: ret i1 [[RES]]
132+
;
133+
%lhs = select i1 %cond, float %a, float %b
134+
%rhs = select i1 %cond, float %b, float %a
135+
%res = fcmp uno float %lhs, %rhs
136+
ret i1 %res
137+
}
138+
139+
define i1 @fcmp_ogt_select(i1 %cond, float %a, float %b) {
140+
; CHECK-LABEL: @fcmp_ogt_select(
141+
; CHECK-NEXT: [[LHS:%.*]] = select i1 [[COND:%.*]], float [[A:%.*]], float [[B:%.*]]
142+
; CHECK-NEXT: [[RHS:%.*]] = select i1 [[COND]], float [[B]], float [[A]]
143+
; CHECK-NEXT: [[RES:%.*]] = fcmp ogt float [[LHS]], [[RHS]]
144+
; CHECK-NEXT: ret i1 [[RES]]
145+
;
146+
%lhs = select i1 %cond, float %a, float %b
147+
%rhs = select i1 %cond, float %b, float %a
148+
%res = fcmp ogt float %lhs, %rhs
149+
ret i1 %res
150+
}

llvm/test/Transforms/InstCombine/icmp-select.ll

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,3 +604,27 @@ define i1 @select_constants_and_icmp_ne_fval(i1 %x, i1 %y) {
604604
%cmp = icmp ne i8 %and, 3
605605
ret i1 %cmp
606606
}
607+
608+
define i1 @icmp_eq_select(i1 %cond, i32 %a, i32 %b) {
609+
; CHECK-LABEL: @icmp_eq_select(
610+
; CHECK-NEXT: [[RES:%.*]] = icmp eq i32 [[A:%.*]], [[B:%.*]]
611+
; CHECK-NEXT: ret i1 [[RES]]
612+
;
613+
%lhs = select i1 %cond, i32 %a, i32 %b
614+
%rhs = select i1 %cond, i32 %b, i32 %a
615+
%res = icmp eq i32 %lhs, %rhs
616+
ret i1 %res
617+
}
618+
619+
define i1 @icmp_slt_select(i1 %cond, i32 %a, i32 %b) {
620+
; CHECK-LABEL: @icmp_slt_select(
621+
; CHECK-NEXT: [[LHS:%.*]] = select i1 [[COND:%.*]], i32 [[A:%.*]], i32 [[B:%.*]]
622+
; CHECK-NEXT: [[RHS:%.*]] = select i1 [[COND]], i32 [[B]], i32 [[A]]
623+
; CHECK-NEXT: [[RES:%.*]] = icmp slt i32 [[LHS]], [[RHS]]
624+
; CHECK-NEXT: ret i1 [[RES]]
625+
;
626+
%lhs = select i1 %cond, i32 %a, i32 %b
627+
%rhs = select i1 %cond, i32 %b, i32 %a
628+
%res = icmp slt i32 %lhs, %rhs
629+
ret i1 %res
630+
}

0 commit comments

Comments
 (0)