[llvm] [InstCombine] Fix miscompilation for `select` + `FCmp` (PR #177872)
Tirthankar Mazumder via llvm-commits
llvm-commits at lists.llvm.org
Fri Jan 30 07:58:53 PST 2026
================
@@ -4511,13 +4523,35 @@ Instruction *InstCombinerImpl::visitSelectInst(SelectInst &SI) {
CmpInst::Predicate MinMaxPred = getMinMaxPred(SPF, SPR.Ordered);
Value *Cmp;
- if (CmpInst::isIntPredicate(MinMaxPred))
+ Value *NewSI;
+ if (CmpInst::isIntPredicate(MinMaxPred)) {
Cmp = Builder.CreateICmp(MinMaxPred, LHS, RHS);
- else
- Cmp = Builder.CreateFCmpFMF(MinMaxPred, LHS, RHS,
- cast<Instruction>(SI.getCondition()));
+ NewSI = Builder.CreateSelect(Cmp, LHS, RHS, SI.getName(), &SI);
+ } else {
+ // Intersect fast-math flags between the original select and its
+ // condition before forming a min/max fcmp+select pattern.
+ //
+ // Therefore, only retain fast-math flags that were already required
+ // by both the original select and its condition.
+ FastMathFlags CmpFMF =
+ cast<Instruction>(SI.getCondition())->getFastMathFlags();
+
+ // We can't unconditionally query fast-math flags from the select
+ // because only selects that produce floating-point values implement
+ // FPMathOperator. Treat non-FP selects as having no fast-math
+ // guarantees.
+ FastMathFlags SelFMF;
+ if (SIFPOp)
+ SelFMF = SI.getFastMathFlags();
+
+ FastMathFlags NewFMF =
+ FastMathFlags::intersectRewrite(SelFMF, CmpFMF);
----------------
wermos wrote:
You're right. I completely misunderstood what that function does.
https://github.com/llvm/llvm-project/pull/177872
More information about the llvm-commits
mailing list