Index: lib/CodeGen/SelectionDAG/SelectionDAG.cpp =================================================================== --- lib/CodeGen/SelectionDAG/SelectionDAG.cpp +++ lib/CodeGen/SelectionDAG/SelectionDAG.cpp @@ -4442,7 +4442,7 @@ case ISD::FMUL: case ISD::FDIV: case ISD::FREM: - if (getTarget().Options.UnsafeFPMath) { + if (getTarget().Options.UnsafeFPMath || Flags.isFast()) { if (Opcode == ISD::FADD) { // x+0 --> x if (N2CFP && N2CFP->getValueAPF().isZero()) Index: lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp =================================================================== --- lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp +++ lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp @@ -2769,6 +2769,10 @@ Flags.setVectorReduction(true); LLVM_DEBUG(dbgs() << "Detected a reduction operation:" << I << "\n"); } + // need Flags for Selection DAG decisions here + if (auto *FPMO = dyn_cast(&I)) { + Flags.copyFMF(*FPMO); + } SDValue Op1 = getValue(I.getOperand(0)); SDValue Op2 = getValue(I.getOperand(1)); Index: test/CodeGen/X86/fp-fold.ll =================================================================== --- test/CodeGen/X86/fp-fold.ll +++ test/CodeGen/X86/fp-fold.ll @@ -0,0 +1,39 @@ +; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py +; RUN: llc < %s -mtriple=x86_64-unknown-unknown | FileCheck %s +; RUN: llc < %s -mtriple=x86_64-unknown-unknown -enable-unsafe-fp-math | FileCheck %s + +; This is duplicated from tests for InstSimplify. If you're +; adding something here, you should probably add it there too. + +define float @fadd_zero(float %x) { +; CHECK-LABEL: fadd_zero: +; CHECK: # %bb.0: +; CHECK-NEXT: retq + %r = fadd fast float %x, 0.0 + ret float %r +} + +define float @fsub_zero(float %x) { +; CHECK-LABEL: fsub_zero: +; CHECK: # %bb.0: +; CHECK-NEXT: retq + %r = fsub fast float %x, 0.0 + ret float %r +} + +define float @fmul_zero(float %x) { +; CHECK-LABEL: fmul_zero: +; CHECK: # %bb.0: +; CHECK: xorps %xmm0, %xmm0 +; CHECK-NEXT: retq + %r = fmul fast float %x, 0.0 + ret float %r +} + +define float @fmul_one(float %x) { +; CHECK-LABEL: fmul_one: +; CHECK: # %bb.0: +; CHECK-NEXT: retq + %r = fmul fast float %x, 1.0 + ret float %r +}