Skip to content

Commit fc2ed05

Browse files
committed
[InstCombine] Use isKnownNeverNaN
1 parent 98992d3 commit fc2ed05

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2796,8 +2796,11 @@ static Instruction *foldSelectWithFCmpToFabs(SelectInst &SI,
27962796
// Note: We require "nnan" for this fold because fcmp ignores the signbit
27972797
// of NAN, but IEEE-754 specifies the signbit of NAN values with
27982798
// fneg/fabs operations.
2799-
if (cast<FPMathOperator>(CondVal)->hasNoNaNs() &&
2800-
match(TrueVal, m_FSub(m_PosZeroFP(), m_Specific(X)))) {
2799+
if (match(TrueVal, m_FSub(m_PosZeroFP(), m_Specific(X))) &&
2800+
(cast<FPMathOperator>(CondVal)->hasNoNaNs() ||
2801+
isKnownNeverNaN(X, /*Depth=*/0,
2802+
IC.getSimplifyQuery().getWithInstruction(
2803+
cast<Instruction>(CondVal))))) {
28012804
if (!Swap && (Pred == FCmpInst::FCMP_OLE || Pred == FCmpInst::FCMP_ULE)) {
28022805
Value *Fabs = IC.Builder.CreateUnaryIntrinsic(Intrinsic::fabs, X, &SI);
28032806
return IC.replaceInstUsesWith(SI, Fabs);

llvm/test/Transforms/InstCombine/fabs.ll

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,17 @@ define double @select_fcmp_ole_zero_no_nnan(double %x) {
276276
ret double %fabs
277277
}
278278

279+
define double @select_fcmp_ole_zero_no_nnan_input_nofpclass_nan(double nofpclass(nan) %x) {
280+
; CHECK-LABEL: @select_fcmp_ole_zero_no_nnan_input_nofpclass_nan(
281+
; CHECK-NEXT: [[FABS:%.*]] = call double @llvm.fabs.f64(double [[X:%.*]])
282+
; CHECK-NEXT: ret double [[FABS]]
283+
;
284+
%lezero = fcmp ole double %x, 0.0
285+
%negx = fsub double 0.0, %x
286+
%fabs = select i1 %lezero, double %negx, double %x
287+
ret double %fabs
288+
}
289+
279290
define double @select_fcmp_nnan_ole_zero(double %x) {
280291
; CHECK-LABEL: @select_fcmp_nnan_ole_zero(
281292
; CHECK-NEXT: [[FABS:%.*]] = call double @llvm.fabs.f64(double [[X:%.*]])

0 commit comments

Comments
 (0)