[llvm] [IVDescriptors] Identify min/max recurrences in single pass. (PR #163460)
Florian Hahn via llvm-commits
llvm-commits at lists.llvm.org
Wed Nov 5 07:58:43 PST 2025
================
@@ -214,6 +214,173 @@ static bool checkOrderedReduction(RecurKind Kind, Instruction *ExactFPMathInst,
return true;
}
+// Helper to collect FMF from a value and its associated fcmp in select patterns
+static FastMathFlags collectMinMaxFMF(Value *V) {
+ FastMathFlags FMF = cast<FPMathOperator>(V)->getFastMathFlags();
+ if (auto *Sel = dyn_cast<SelectInst>(V)) {
+ // Accept FMF on either fcmp or select of a min/max idiom.
+ // TODO: This is a hack to work-around the fact that FMF may not be
+ // assigned/propagated correctly. If that problem is fixed or we
+ // standardize on fmin/fmax via intrinsics, this can be removed.
+ if (auto *FCmp = dyn_cast<FCmpInst>(Sel->getCondition()))
+ FMF |= FCmp->getFastMathFlags();
+ }
+ return FMF;
+}
+
+static std::optional<FastMathFlags>
+hasRequiredFastMathFlags(FPMathOperator *FPOp, RecurKind &RK,
+ FastMathFlags FuncFMF) {
+ bool HasRequiredFMF =
+ (FuncFMF.noNaNs() && FuncFMF.noSignedZeros()) ||
+ (FPOp && FPOp->hasNoNaNs() && FPOp->hasNoSignedZeros()) ||
+ RK == RecurKind::FMinimum || RK == RecurKind::FMaximum ||
+ RK == RecurKind::FMinimumNum || RK == RecurKind::FMaximumNum;
+ if (!HasRequiredFMF) {
+ if (RK == RecurKind::FMax &&
+ match(FPOp, m_Intrinsic<Intrinsic::maxnum>(m_Value(), m_Value())))
+ RK = RecurKind::FMaxNum;
+ else if (RK == RecurKind::FMin &&
+ match(FPOp, m_Intrinsic<Intrinsic::minnum>(m_Value(), m_Value())))
+ RK = RecurKind::FMinNum;
+ else
+ return std::nullopt;
+ }
+ return {collectMinMaxFMF(FPOp)};
+}
+
+static std::optional<RecurrenceDescriptor>
----------------
fhahn wrote:
We need to return a `RecurrenceDescriptor`, but we don't need to use std::optional; we can check the recurrence kind is none, done thanks
https://github.com/llvm/llvm-project/pull/163460
More information about the llvm-commits
mailing list