[llvm] [InstCombine] Simplify `floor`/`ceil` to `trunc` for integral tests (PR #190620)

Max Graey via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 7 02:46:47 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/6] 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/6] 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;
 

>From 987a4dde4a6ba2efdbfa07901f643e51f55cc847 Mon Sep 17 00:00:00 2001
From: MaxGraey <maxgraey at gmail.com>
Date: Mon, 6 Apr 2026 20:45:36 +0300
Subject: [PATCH 3/6] refactor

---
 llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
index c442a9fb65161..4a355c97a5762 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -8820,9 +8820,9 @@ static Instruction *foldFCmpWithFloorAndCeil(FCmpInst &I,
       break;
 
     KnownFPClass Known = computeKnownFPClass(
-        RHS, fcAllFlags, IC.getSimplifyQuery().getWithInstruction(&I));
+        RHS, fcNan | fcInf, IC.getSimplifyQuery().getWithInstruction(&I));
 
-    if ((Known.KnownFPClasses & (fcNan | fcInf)) != 0)
+    if (!Known.isKnownNeverInfOrNaN())
       break;
 
     Value *TruncX = IC.Builder.CreateUnaryIntrinsic(Intrinsic::trunc, RHS);

>From cecf31aaeb99ef62be4fe87699b35b7dd8b09bec Mon Sep 17 00:00:00 2001
From: MaxGraey <maxgraey at gmail.com>
Date: Mon, 6 Apr 2026 20:53:52 +0300
Subject: [PATCH 4/6] non-NaN contract is unnecessary

---
 llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
index 4a355c97a5762..9fa4eaa515fbd 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -8815,14 +8815,14 @@ static Instruction *foldFCmpWithFloorAndCeil(FCmpInst &I,
   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 x is known not to be Inf
     if (!FloorX && !CeilX)
       break;
 
     KnownFPClass Known = computeKnownFPClass(
-        RHS, fcNan | fcInf, IC.getSimplifyQuery().getWithInstruction(&I));
+        RHS, fcInf, IC.getSimplifyQuery().getWithInstruction(&I));
 
-    if (!Known.isKnownNeverInfOrNaN())
+    if (!Known.isKnownNeverInfinity())
       break;
 
     Value *TruncX = IC.Builder.CreateUnaryIntrinsic(Intrinsic::trunc, RHS);

>From 0d321723f5b2347de4007aaae3a793ee70571ad1 Mon Sep 17 00:00:00 2001
From: MaxGraey <maxgraey at gmail.com>
Date: Tue, 7 Apr 2026 02:43:24 +0300
Subject: [PATCH 5/6] simplify. Add more ord/unord predicates

---
 .../InstCombine/InstCombineCompares.cpp       | 29 +++++++++----------
 1 file changed, 13 insertions(+), 16 deletions(-)

diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
index 9fa4eaa515fbd..5b3e663c274ea 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -8812,22 +8812,19 @@ 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 Inf
-    if (!FloorX && !CeilX)
-      break;
-
-    KnownFPClass Known = computeKnownFPClass(
-        RHS, fcInf, IC.getSimplifyQuery().getWithInstruction(&I));
-
-    if (!Known.isKnownNeverInfinity())
-      break;
-
-    Value *TruncX = IC.Builder.CreateUnaryIntrinsic(Intrinsic::trunc, RHS);
-    return new FCmpInst(Pred, TruncX, RHS, "", &I);
-  }
+  case FCmpInst::FCMP_ONE:
+  case FCmpInst::FCMP_ORD:
+  case FCmpInst::FCMP_UEQ:
+  case FCmpInst::FCMP_UNE:
+  case FCmpInst::FCMP_UNO:
+    // fcmp pred floor(x), x => fcmp pred trunc(x), x
+    // fcmp pred  ceil(x), x => fcmp pred trunc(x), x
+    // where pred is oeq, one, ord, ueq, une, or uno.
+    if (FloorX || CeilX) {
+      Value *TruncX = IC.Builder.CreateUnaryIntrinsic(Intrinsic::trunc, RHS);
+      return new FCmpInst(Pred, TruncX, RHS, "", &I);
+    }
+    break;
   case FCmpInst::FCMP_OLE:
     // fcmp ole floor(x), x => fcmp ord x, 0
     if (FloorX)

>From fefe9b69be699028fc4a4d14bfac3c4e437dd1da Mon Sep 17 00:00:00 2001
From: MaxGraey <maxgraey at gmail.com>
Date: Tue, 7 Apr 2026 12:46:27 +0300
Subject: [PATCH 6/6] update fp-floor-ceil.ll

---
 llvm/test/Transforms/InstCombine/fp-floor-ceil.ll | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/llvm/test/Transforms/InstCombine/fp-floor-ceil.ll b/llvm/test/Transforms/InstCombine/fp-floor-ceil.ll
index c90b7ee11310c..6b28f738712bc 100644
--- a/llvm/test/Transforms/InstCombine/fp-floor-ceil.ll
+++ b/llvm/test/Transforms/InstCombine/fp-floor-ceil.ll
@@ -110,7 +110,7 @@ define i1 @x_floor_ogt_neg(float %x) {
 
 define i1 @x_floor_ueq_neg(float %x) {
 ; CHECK-LABEL: @x_floor_ueq_neg(
-; CHECK-NEXT:    [[FLOOR:%.*]] = call float @llvm.floor.f32(float [[X:%.*]])
+; CHECK-NEXT:    [[X:%.*]] = call float @llvm.trunc.f32(float [[FLOOR:%.*]])
 ; CHECK-NEXT:    [[RET:%.*]] = fcmp ueq float [[X]], [[FLOOR]]
 ; CHECK-NEXT:    ret i1 [[RET]]
 ;
@@ -121,7 +121,7 @@ define i1 @x_floor_ueq_neg(float %x) {
 
 define i1 @x_floor_une_neg(float %x) {
 ; CHECK-LABEL: @x_floor_une_neg(
-; CHECK-NEXT:    [[FLOOR:%.*]] = call float @llvm.floor.f32(float [[X:%.*]])
+; CHECK-NEXT:    [[X:%.*]] = call float @llvm.trunc.f32(float [[FLOOR:%.*]])
 ; CHECK-NEXT:    [[RET:%.*]] = fcmp une float [[X]], [[FLOOR]]
 ; CHECK-NEXT:    ret i1 [[RET]]
 ;
@@ -239,7 +239,7 @@ define i1 @x_ceil_olt_neg(float %x) {
 
 define i1 @x_ceil_oeq_neg(float %x) {
 ; CHECK-LABEL: @x_ceil_oeq_neg(
-; CHECK-NEXT:    [[CEIL:%.*]] = call float @llvm.ceil.f32(float [[X:%.*]])
+; CHECK-NEXT:    [[X:%.*]] = call float @llvm.trunc.f32(float [[CEIL:%.*]])
 ; CHECK-NEXT:    [[RET:%.*]] = fcmp oeq float [[X]], [[CEIL]]
 ; CHECK-NEXT:    ret i1 [[RET]]
 ;
@@ -250,7 +250,7 @@ define i1 @x_ceil_oeq_neg(float %x) {
 
 define i1 @x_ceil_one_neg(float %x) {
 ; CHECK-LABEL: @x_ceil_one_neg(
-; CHECK-NEXT:    [[CEIL:%.*]] = call float @llvm.ceil.f32(float [[X:%.*]])
+; CHECK-NEXT:    [[X:%.*]] = call float @llvm.trunc.f32(float [[CEIL:%.*]])
 ; CHECK-NEXT:    [[RET:%.*]] = fcmp one float [[X]], [[CEIL]]
 ; CHECK-NEXT:    ret i1 [[RET]]
 ;



More information about the llvm-commits mailing list