Skip to content

Commit 38be749

Browse files
committed
[InstCombine] Fold fcmp ogt (x - y), 0 into fcmp ogt x, y #85245
1 parent 864a886 commit 38be749

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7972,6 +7972,13 @@ Instruction *InstCombinerImpl::visitFCmpInst(FCmpInst &I) {
79727972
Constant *RHSC;
79737973
if (match(Op0, m_Instruction(LHSI)) && match(Op1, m_Constant(RHSC))) {
79747974
switch (LHSI->getOpcode()) {
7975+
case Instruction::FSub:
7976+
if ((Pred == FCmpInst::FCMP_OGT || Pred == FCmpInst::FCMP_OLT ||
7977+
Pred == FCmpInst::FCMP_ONE) &&
7978+
match(RHSC, m_AnyZeroFP()) &&
7979+
match(LHSI, m_FSub(m_Value(X), m_Value(Y))))
7980+
return new FCmpInst(Pred, X, Y);
7981+
break;
79757982
case Instruction::PHI:
79767983
if (Instruction *NV = foldOpIntoPhi(I, cast<PHINode>(LHSI)))
79777984
return NV;

llvm/test/Transforms/InstCombine/fcmp.ll

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1284,3 +1284,46 @@ define <1 x i1> @bitcast_1vec_eq0(i32 %x) {
12841284
%cmp = fcmp oeq <1 x float> %f, zeroinitializer
12851285
ret <1 x i1> %cmp
12861286
}
1287+
1288+
define i1 @fcmp_ogt_fsub_const(float %x, float %y) {
1289+
; CHECK-LABEL: @fcmp_ogt_fsub_const(
1290+
; CHECK-NEXT: [[CMP:%.*]] = fcmp ogt float [[X:%.*]], [[Y:%.*]]
1291+
; CHECK-NEXT: ret i1 [[CMP]]
1292+
;
1293+
%fs = fsub float %x, %y
1294+
%cmp = fcmp ogt float %fs, 0.000000e+00
1295+
ret i1 %cmp
1296+
}
1297+
1298+
define i1 @fcmp_olt_fsub_const(float %x, float %y) {
1299+
; CHECK-LABEL: @fcmp_olt_fsub_const(
1300+
; CHECK-NEXT: [[CMP:%.*]] = fcmp olt float [[X:%.*]], [[Y:%.*]]
1301+
; CHECK-NEXT: ret i1 [[CMP]]
1302+
;
1303+
%fs = fsub float %x, %y
1304+
%cmp = fcmp olt float %fs, 0.000000e+00
1305+
ret i1 %cmp
1306+
}
1307+
1308+
define i1 @fcmp_one_fsub_const(float %x, float %y) {
1309+
; CHECK-LABEL: @fcmp_one_fsub_const(
1310+
; CHECK-NEXT: [[CMP:%.*]] = fcmp one float [[X:%.*]], [[Y:%.*]]
1311+
; CHECK-NEXT: ret i1 [[CMP]]
1312+
;
1313+
%fs = fsub float %x, %y
1314+
%cmp = fcmp one float %fs, 0.000000e+00
1315+
ret i1 %cmp
1316+
}
1317+
1318+
define i1 @fcmp_fsub_neg_zero(float %x, float %y) {
1319+
; CHECK-LABEL: @fcmp_fsub_neg_zero(
1320+
; CHECK-NEXT: [[FS:%.*]] = fsub float [[X:%.*]], [[Y:%.*]]
1321+
; CHECK-NEXT: call void @use(float [[FS]])
1322+
; CHECK-NEXT: [[CMP:%.*]] = fcmp ogt float [[X]], [[Y]]
1323+
; CHECK-NEXT: ret i1 [[CMP]]
1324+
;
1325+
%fs = fsub float %x, %y
1326+
call void @use(float %fs)
1327+
%cmp = fcmp ogt float %fs, -0.000000e+00
1328+
ret i1 %cmp
1329+
}

0 commit comments

Comments
 (0)