Skip to content

Commit abc27bd

Browse files
committed
[InstCombine] Avoid some FP cast constant expressions (NFCI)
Instead of doing fptoxi and xitofp casts to check for round-trip, directly check the IsExact flag on the convertToInteger() API.
1 parent ace4489 commit abc27bd

File tree

1 file changed

+7
-10
lines changed

1 file changed

+7
-10
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7324,17 +7324,14 @@ Instruction *InstCombinerImpl::foldFCmpIntToFPConst(FCmpInst &I,
73247324
}
73257325

73267326
// Okay, now we know that the FP constant fits in the range [SMIN, SMAX] or
7327-
// [0, UMAX], but it may still be fractional. See if it is fractional by
7328-
// casting the FP value to the integer value and back, checking for equality.
7327+
// [0, UMAX], but it may still be fractional. Check whether this is the case
7328+
// using the IsExact flag.
73297329
// Don't do this for zero, because -0.0 is not fractional.
7330-
Constant *RHSInt = LHSUnsigned
7331-
? ConstantExpr::getFPToUI(RHSC, IntTy)
7332-
: ConstantExpr::getFPToSI(RHSC, IntTy);
7330+
APSInt RHSInt(IntWidth, LHSUnsigned);
7331+
bool IsExact;
7332+
RHS.convertToInteger(RHSInt, APFloat::rmTowardZero, &IsExact);
73337333
if (!RHS.isZero()) {
7334-
bool Equal = LHSUnsigned
7335-
? ConstantExpr::getUIToFP(RHSInt, RHSC->getType()) == RHSC
7336-
: ConstantExpr::getSIToFP(RHSInt, RHSC->getType()) == RHSC;
7337-
if (!Equal) {
7334+
if (!IsExact) {
73387335
// If we had a comparison against a fractional value, we have to adjust
73397336
// the compare predicate and sometimes the value. RHSC is rounded towards
73407337
// zero at this point.
@@ -7400,7 +7397,7 @@ Instruction *InstCombinerImpl::foldFCmpIntToFPConst(FCmpInst &I,
74007397

74017398
// Lower this FP comparison into an appropriate integer version of the
74027399
// comparison.
7403-
return new ICmpInst(Pred, LHSI->getOperand(0), RHSInt);
7400+
return new ICmpInst(Pred, LHSI->getOperand(0), Builder.getInt(RHSInt));
74047401
}
74057402

74067403
/// Fold (C / X) < 0.0 --> X < 0.0 if possible. Swap predicate if necessary.

0 commit comments

Comments
 (0)