[llvm] [InstCombine][NFC] Expose isKnownExactCastIntToFP as a public method (PR #190327)

Valeriy Savchenko via llvm-commits llvm-commits at lists.llvm.org
Fri Apr 3 02:42:44 PDT 2026


https://github.com/SavchenkoValeriy created https://github.com/llvm/llvm-project/pull/190327

[InstCombine][NFC] Expose isKnownExactCastIntToFP as a public method

>From 1eb9b34d4b52f7cbf2926baba7b75ebcf22a43e8 Mon Sep 17 00:00:00 2001
From: Valeriy Savchenko <vsavchenko at apple.com>
Date: Thu, 2 Apr 2026 20:33:04 +0100
Subject: [PATCH] [InstCombine][NFC] Expose isKnownExactCastIntToFP as a public
 method

---
 .../llvm/Transforms/InstCombine/InstCombiner.h    |  4 ++++
 .../Transforms/InstCombine/InstCombineCasts.cpp   | 15 ++++++---------
 2 files changed, 10 insertions(+), 9 deletions(-)

diff --git a/llvm/include/llvm/Transforms/InstCombine/InstCombiner.h b/llvm/include/llvm/Transforms/InstCombine/InstCombiner.h
index 6de06addac71c..bc52bf1168d4a 100644
--- a/llvm/include/llvm/Transforms/InstCombine/InstCombiner.h
+++ b/llvm/include/llvm/Transforms/InstCombine/InstCombiner.h
@@ -478,6 +478,10 @@ class LLVM_LIBRARY_VISIBILITY InstCombiner {
     return llvm::ComputeMaxSignificantBits(Op, DL, &AC, CxtI, &DT, Depth);
   }
 
+  /// Return true if the cast from integer to FP can be proven to be exact
+  /// for all possible inputs (the conversion does not lose any precision).
+  bool isKnownExactCastIntToFP(CastInst &I) const;
+
   OverflowResult computeOverflowForUnsignedMul(const Value *LHS,
                                                const Value *RHS,
                                                const Instruction *CxtI,
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
index f28048fb4c7c4..a1d67a0c60ac5 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
@@ -2030,9 +2030,7 @@ static Type *getMinimumFPType(Value *V, bool PreferBFloat) {
   return V->getType();
 }
 
-/// Return true if the cast from integer to FP can be proven to be exact for all
-/// possible inputs (the conversion does not lose any precision).
-static bool isKnownExactCastIntToFP(CastInst &I, InstCombinerImpl &IC) {
+bool InstCombiner::isKnownExactCastIntToFP(CastInst &I) const {
   CastInst::CastOps Opcode = I.getOpcode();
   assert((Opcode == CastInst::SIToFP || Opcode == CastInst::UIToFP) &&
          "Unexpected cast");
@@ -2068,7 +2066,7 @@ static bool isKnownExactCastIntToFP(CastInst &I, InstCombinerImpl &IC) {
 
   // Try harder to find if the source integer type has less significant bits.
   // Compute number of sign bits or determine trailing zeros.
-  KnownBits SrcKnown = IC.computeKnownBits(Src, &I);
+  KnownBits SrcKnown = computeKnownBits(Src, &I);
   int SigBits = (int)SrcTy->getScalarSizeInBits() -
                 SrcKnown.countMinLeadingZeros() -
                 SrcKnown.countMinTrailingZeros();
@@ -2078,8 +2076,7 @@ static bool isKnownExactCastIntToFP(CastInst &I, InstCombinerImpl &IC) {
   // For sitofp, the sign maps to the FP sign bit, so only magnitude bits
   // (BitWidth - NumSignBits) consume mantissa.
   if (IsSigned) {
-    SigBits =
-        (int)SrcTy->getScalarSizeInBits() - IC.ComputeNumSignBits(Src, &I);
+    SigBits = (int)SrcTy->getScalarSizeInBits() - ComputeNumSignBits(Src, &I);
     if (SigBits <= DestNumSigBits)
       return true;
   }
@@ -2268,7 +2265,7 @@ Instruction *InstCombinerImpl::visitFPTrunc(FPTruncInst &FPT) {
   Value *Src = FPT.getOperand(0);
   if (isa<SIToFPInst>(Src) || isa<UIToFPInst>(Src)) {
     auto *FPCast = cast<CastInst>(Src);
-    if (isKnownExactCastIntToFP(*FPCast, *this))
+    if (isKnownExactCastIntToFP(*FPCast))
       return CastInst::Create(FPCast->getOpcode(), FPCast->getOperand(0), Ty);
   }
 
@@ -2282,7 +2279,7 @@ Instruction *InstCombinerImpl::visitFPExt(CastInst &FPExt) {
   Value *Src = FPExt.getOperand(0);
   if (isa<SIToFPInst>(Src) || isa<UIToFPInst>(Src)) {
     auto *FPCast = cast<CastInst>(Src);
-    if (isKnownExactCastIntToFP(*FPCast, *this))
+    if (isKnownExactCastIntToFP(*FPCast))
       return CastInst::Create(FPCast->getOpcode(), FPCast->getOperand(0), Ty);
   }
 
@@ -2309,7 +2306,7 @@ Instruction *InstCombinerImpl::foldItoFPtoI(CastInst &FI) {
 
   // This means this is also safe for a signed input and unsigned output, since
   // a negative input would lead to undefined behavior.
-  if (!isKnownExactCastIntToFP(*OpI, *this)) {
+  if (!isKnownExactCastIntToFP(*OpI)) {
     // The first cast may not round exactly based on the source integer width
     // and FP width, but the overflow UB rules can still allow this to fold.
     // If the destination type is narrow, that means the intermediate FP value



More information about the llvm-commits mailing list