[llvm] Optimize fptrunc(x)>=C1 --> x>=C2 (PR #99475)
Matt Arsenault via llvm-commits
llvm-commits at lists.llvm.org
Fri Aug 16 06:37:33 PDT 2024
================
@@ -7882,6 +7892,79 @@ static Instruction *foldFCmpReciprocalAndZero(FCmpInst &I, Instruction *LHSI,
return new FCmpInst(Pred, LHSI->getOperand(1), RHSC, "", &I);
}
+// Fold trunc(x) < constant --> x < constant if possible.
+static Instruction *foldFCmpFpTrunc(FCmpInst &I, Instruction *LHSI,
+ Constant *RHSC) {
+ FCmpInst::Predicate Pred = I.getPredicate();
+ bool RoundDown = false;
+
+ if ((Pred == FCmpInst::FCMP_OGE) || (Pred == FCmpInst::FCMP_UGE) ||
+ (Pred == FCmpInst::FCMP_OLT) || (Pred == FCmpInst::FCMP_ULT))
+ RoundDown = true;
+ else if ((Pred == FCmpInst::FCMP_OGT) || (Pred == FCmpInst::FCMP_UGT) ||
+ (Pred == FCmpInst::FCMP_OLE) || (Pred == FCmpInst::FCMP_ULE))
+ RoundDown = false;
+ else
+ return nullptr;
+
+ const APFloat *RValue;
+ if (!match(RHSC, m_APFloat(RValue)))
+ return nullptr;
+
+ Type *LType = LHSI->getOperand(0)->getType();
+ Type *RType = RHSC->getType();
+ Type *LEleType = LType->getScalarType();
+ Type *REleType = RType->getScalarType();
+
+ APFloat NextRValue = *RValue;
+ NextRValue.next(RoundDown);
+
+ // Round RValue to suitable value
+ APFloat ExtRValue = *RValue;
+ APFloat ExtNextRValue = NextRValue;
+ bool lossInfo;
+ ExtRValue.convert(LEleType->getFltSemantics(), APFloat::rmNearestTiesToEven,
+ &lossInfo);
+ ExtNextRValue.convert(LEleType->getFltSemantics(),
+ APFloat::rmNearestTiesToEven, &lossInfo);
+
+ APFloat RoundValue{LEleType->getFltSemantics()};
+ {
+ APFloat Two{LEleType->getFltSemantics(), 2};
+ APFloat LowBound = RoundDown ? ExtNextRValue : ExtRValue;
+ APFloat UpBound = RoundDown ? ExtRValue : ExtNextRValue;
+
+ while (true) {
----------------
arsenm wrote:
This loop is the hard to review part and needs some comments explaining what constants are legal
https://github.com/llvm/llvm-project/pull/99475
More information about the llvm-commits
mailing list