[llvm] [InstCombine] Fold zero compares of fsub with floor/ceil into `fcmp trunc(x), x` (PR #190620)

Max Graey via llvm-commits llvm-commits at lists.llvm.org
Mon Apr 6 08:42:34 PDT 2026


https://github.com/MaxGraey created https://github.com/llvm/llvm-project/pull/190620

Fix https://github.com/llvm/llvm-project/issues/188035

>From 002deac72c81c372e13084502d9972a1e3938575 Mon Sep 17 00:00:00 2001
From: MaxGraey <maxgraey at gmail.com>
Date: Mon, 6 Apr 2026 18:38:30 +0300
Subject: [PATCH] initial draft

---
 .../InstCombine/InstCombineCompares.cpp       | 61 ++++++++++++++++++-
 1 file changed, 60 insertions(+), 1 deletion(-)

diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
index 242e831045369..a291cd8ca6c8b 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -5802,7 +5802,7 @@ Instruction *InstCombinerImpl::foldICmpWithMinMax(Instruction &I,
 
 /// Match and fold patterns like:
 ///   icmp eq/ne X, min(max(X, Lo), Hi)
-/// which represents a range check and can be repsented as a ConstantRange.
+/// which represents a range check and can be represented as a ConstantRange.
 ///
 /// For icmp eq, build ConstantRange [Lo, Hi + 1) and convert to:
 ///   (X - Lo) u< (Hi + 1 - Lo)
@@ -8862,6 +8862,62 @@ static Instruction *foldFCmpWithFloorAndCeil(FCmpInst &I,
   return nullptr;
 }
 
+template <Intrinsic::ID IID> static Value *matchRoundDiff(Value *Sub) {
+  Value *X = nullptr;
+  if (match(Sub, m_FSub(m_Value(X), m_Intrinsic<IID>(m_Specific(X)))) ||
+      match(Sub, m_FSub(m_Intrinsic<IID>(m_Value(X)), m_Specific(X))))
+    return X;
+  return nullptr;
+}
+
+static Value *matchFract(Value *Sub) {
+  if (Value *X = matchRoundDiff<Intrinsic::floor>(Sub))
+    return X;
+  if (Value *X = matchRoundDiff<Intrinsic::ceil>(Sub))
+    return X;
+  return nullptr;
+}
+
+static Instruction *foldFCmpWithFSubFloorAndCeilEqZero(FCmpInst &I,
+                                                       InstCombinerImpl &IC) {
+  FCmpInst::Predicate Pred = I.getPredicate();
+  if (Pred != FCmpInst::FCMP_OEQ && Pred != FCmpInst::FCMP_ONE)
+    return nullptr;
+
+  Value *Fract;
+  // Extract:
+  //   fcmp pred Fract, 0.0
+  // or
+  //   fcmp pred 0.0, Fract
+  if (match(I.getOperand(1), m_AnyZeroFP()))
+    Fract = I.getOperand(0);
+  else if (match(I.getOperand(0), m_AnyZeroFP()))
+    Fract = I.getOperand(1);
+  else
+    return nullptr;
+
+  Value *X = matchFract(Fract);
+  if (!X)
+    return nullptr;
+
+  KnownFPClass Known = computeKnownFPClass(
+      X, fcAllFlags, IC.getSimplifyQuery().getWithInstruction(&I));
+  if ((Known.KnownFPClasses & (fcNan | fcInf)) != 0)
+    return nullptr;
+
+  // x - floor(x) == 0.0  =>  trunc(x) == x
+  // x - floor(x) != 0.0  =>  trunc(x) != x
+  // floor(x) - x == 0.0  =>  trunc(x) == x
+  // floor(x) - x != 0.0  =>  trunc(x) != x
+  // x - ceil(x)  == 0.0  =>  trunc(x) == x
+  // x - ceil(x)  != 0.0  =>  trunc(x) != x
+  // ceil(x) - x  == 0.0  =>  trunc(x) == x
+  // ceil(x) - x  != 0.0  =>  trunc(x) != x
+  // This is valid for finite, non-NaN x.
+  Value *TruncX = IC.Builder.CreateUnaryIntrinsic(Intrinsic::trunc, X);
+  return new FCmpInst(Pred, TruncX, X);
+}
+
 /// Returns true if a select that implements a min/max is redundant and
 /// select result can be replaced with its non-constant operand, e.g.,
 ///   select ( (si/ui-to-fp A) <= C ), C, (si/ui-to-fp A)
@@ -9104,6 +9160,9 @@ Instruction *InstCombinerImpl::visitFCmpInst(FCmpInst &I) {
   if (Instruction *R = foldFCmpWithFloorAndCeil(I, *this))
     return R;
 
+  if (Instruction *R = foldFCmpWithFSubFloorAndCeilEqZero(I, *this))
+    return R;
+
   if (Instruction *R = foldCmpSelectOfConstants(I))
     return R;
 



More information about the llvm-commits mailing list