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

Jay Foad via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 16 09:30:19 PDT 2024


================
@@ -7973,11 +7973,36 @@ Instruction *InstCombinerImpl::visitFCmpInst(FCmpInst &I) {
   if (match(Op0, m_Instruction(LHSI)) && match(Op1, m_Constant(RHSC))) {
     switch (LHSI->getOpcode()) {
     case Instruction::FSub:
-      if ((Pred == FCmpInst::FCMP_OGT || Pred == FCmpInst::FCMP_OLT ||
-           Pred == FCmpInst::FCMP_ONE) &&
-          match(RHSC, m_AnyZeroFP()) &&
-          match(LHSI, m_FSub(m_Value(X), m_Value(Y))))
-        return new FCmpInst(Pred, X, Y);
+      switch (Pred) {
+      default:
+        break;
+      case FCmpInst::FCMP_UGT:
+      case FCmpInst::FCMP_ULT:
+      case FCmpInst::FCMP_UNE:
+      case FCmpInst::FCMP_OEQ:
+      case FCmpInst::FCMP_OGE:
+      case FCmpInst::FCMP_OLE: {
+        BinaryOperator *SubI = cast<BinaryOperator>(LHSI);
+        if (!computeKnownFPClass(SubI->getOperand(0), SubI->getFastMathFlags(),
----------------
jayfoad wrote:

> ```
> if (!isKnownNeverNaN(SubI, /*Depth=*/0, getSimplifyQuery().getWithInstruction(&I)))
>   break;
> ```

That's not quite precise. It's OK for the result to be nan if one of the inputs is nan, e.g. `x - nan = nan`. The _only_ case we want to disallow is `inf - inf = nan`.

I think it's probably simplest to add explicit checks for the fast math flags, like:
```
  if (!SubI.hasNoNaNs() && !SubI.hasNoInfs() &&
      !isKnownNeverInf(SubI->getOperand(0)) &&
      !isKnownNeverInf(SubI->getOperand(1))
```
This also has the advantage of doing the quick tests for flags before any of the potentially slow calls to "isKnownNever" functions.

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


More information about the llvm-commits mailing list