[llvm] [InstCombine] Fold fcmp ogt (x - y), 0 into fcmp ogt x, y #85245 (PR #85506)

via llvm-commits llvm-commits at lists.llvm.org
Sat Mar 16 01:18:00 PDT 2024


https://github.com/SahilPatidar created https://github.com/llvm/llvm-project/pull/85506

Resolve #85245
Alive2: https://alive2.llvm.org/ce/z/azHAHq

>From 879940dcdf9fbf2fafc1706d89d44e47f0b8ae14 Mon Sep 17 00:00:00 2001
From: SahilPatidar <patidarsahil2001 at gmail.com>
Date: Fri, 15 Mar 2024 16:49:36 +0530
Subject: [PATCH] [InstCombine] Fold fcmp ogt (x - y), 0 into fcmp ogt x, y
 #85245

---
 .../InstCombine/InstCombineCompares.cpp       |  5 ++++
 llvm/test/Transforms/InstCombine/fcmp.ll      | 23 +++++++++++++++++++
 2 files changed, 28 insertions(+)

diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
index 0dce0077bf1588..a881108602b4c8 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -7972,6 +7972,11 @@ Instruction *InstCombinerImpl::visitFCmpInst(FCmpInst &I) {
   Constant *RHSC;
   if (match(Op0, m_Instruction(LHSI)) && match(Op1, m_Constant(RHSC))) {
     switch (LHSI->getOpcode()) {
+    case Instruction::FSub:
+      if (Pred == FCmpInst::FCMP_OGT && match(RHSC, m_PosZeroFP()) &&
+          match(LHSI, m_OneUse(m_FSub(m_Value(X), m_Value(Y)))))
+        return new FCmpInst(Pred, X, Y);
+      break;
     case Instruction::PHI:
       if (Instruction *NV = foldOpIntoPhi(I, cast<PHINode>(LHSI)))
         return NV;
diff --git a/llvm/test/Transforms/InstCombine/fcmp.ll b/llvm/test/Transforms/InstCombine/fcmp.ll
index 159c84d0dd8aa9..28a28ef36fda4b 100644
--- a/llvm/test/Transforms/InstCombine/fcmp.ll
+++ b/llvm/test/Transforms/InstCombine/fcmp.ll
@@ -1284,3 +1284,26 @@ define <1 x i1> @bitcast_1vec_eq0(i32 %x) {
   %cmp = fcmp oeq <1 x float> %f, zeroinitializer
   ret <1 x i1> %cmp
 }
+
+define i1 @fcmp_fsub_const(float %x, float %y) {
+; CHECK-LABEL: @fcmp_fsub_const(
+; CHECK-NEXT:    [[CMP:%.*]] = fcmp ogt float [[X:%.*]], [[Y:%.*]]
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %fs = fsub float %x, %y
+  %cmp = fcmp ogt float %fs, 0.000000e+00
+  ret i1 %cmp
+}
+
+define i1 @fcmp_fsub_const_extra(float %x, float %y) {
+; CHECK-LABEL: @fcmp_fsub_const_extra(
+; CHECK-NEXT:    [[FS:%.*]] = fsub float [[X:%.*]], [[Y:%.*]]
+; CHECK-NEXT:    call void @use(float [[FS]])
+; CHECK-NEXT:    [[CMP:%.*]] = fcmp ogt float [[FS]], 0.000000e+00
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %fs = fsub float %x, %y
+  call void @use(float %fs)
+  %cmp = fcmp ogt float %fs, 0.000000e+00
+  ret i1 %cmp
+}



More information about the llvm-commits mailing list