[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 10:37:49 PDT 2026
https://github.com/MaxGraey updated https://github.com/llvm/llvm-project/pull/190620
>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 1/2] 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;
>From 648f7e49ed3e73720aa041aadcab14d7c21813a9 Mon Sep 17 00:00:00 2001
From: MaxGraey <maxgraey at gmail.com>
Date: Mon, 6 Apr 2026 20:13:35 +0300
Subject: [PATCH 2/2] simplify
---
.../InstCombine/InstCombineCompares.cpp | 76 +++++--------------
1 file changed, 17 insertions(+), 59 deletions(-)
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
index a291cd8ca6c8b..c442a9fb65161 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -8811,6 +8811,23 @@ static Instruction *foldFCmpWithFloorAndCeil(FCmpInst &I,
}
switch (Pred) {
+ case FCmpInst::FCMP_OEQ:
+ case FCmpInst::FCMP_ONE: {
+ // fcmp oeq/one floor(x), x => fcmp oeq/one trunc(x), x
+ // fcmp oeq/one ceil(x), x => fcmp oeq/one trunc(x), x
+ // if x is known not to be NaN or Inf
+ if (!FloorX && !CeilX)
+ break;
+
+ KnownFPClass Known = computeKnownFPClass(
+ RHS, fcAllFlags, IC.getSimplifyQuery().getWithInstruction(&I));
+
+ if ((Known.KnownFPClasses & (fcNan | fcInf)) != 0)
+ break;
+
+ Value *TruncX = IC.Builder.CreateUnaryIntrinsic(Intrinsic::trunc, RHS);
+ return new FCmpInst(Pred, TruncX, RHS, "", &I);
+ }
case FCmpInst::FCMP_OLE:
// fcmp ole floor(x), x => fcmp ord x, 0
if (FloorX)
@@ -8862,62 +8879,6 @@ 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)
@@ -9160,9 +9121,6 @@ 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