Index: lib/CodeGen/SelectionDAG/DAGCombiner.cpp =================================================================== --- lib/CodeGen/SelectionDAG/DAGCombiner.cpp +++ lib/CodeGen/SelectionDAG/DAGCombiner.cpp @@ -6860,13 +6860,25 @@ if (N1CFP && N1CFP->isExactlyValue(1.0)) return N0; - // If allowed, fold (fmul (fmul x, c1), c2) -> (fmul x, (fmul c1, c2)) - if (DAG.getTarget().Options.UnsafeFPMath && - N1CFP && N0.getOpcode() == ISD::FMUL && - N0.getNode()->hasOneUse() && isConstOrConstSplatFP(N0.getOperand(1))) { - return DAG.getNode(ISD::FMUL, SDLoc(N), VT, N0.getOperand(0), - DAG.getNode(ISD::FMUL, SDLoc(N), VT, - N0.getOperand(1), N1)); + if (DAG.getTarget().Options.UnsafeFPMath) { + // If allowed, fold (fmul (fmul x, c1), c2) -> (fmul x, (fmul c1, c2)) + if (N1CFP && N0.getOpcode() == ISD::FMUL && + N0.getNode()->hasOneUse() && isConstOrConstSplatFP(N0.getOperand(1))) { + SDLoc SL(N); + SDValue MulConsts = DAG.getNode(ISD::FMUL, SL, VT, N0.getOperand(1), N1); + return DAG.getNode(ISD::FMUL, SL, VT, N0.getOperand(0), MulConsts); + } + + // If allowed, fold (fmul (fadd x, x), c) -> (fmul x, (fmul 2.0, c)) + // Undo the fmul 2.0, x -> fadd x, x transformation, since if it occurs + // during an early run of DAGCombiner can prevent folding with fmuls + // inserted during lowering. + if (N0.getOpcode() == ISD::FADD && N0.getOperand(0) == N0.getOperand(1)) { + SDLoc SL(N); + const SDValue Two = DAG.getConstantFP(2.0, VT); + SDValue MulConsts = DAG.getNode(ISD::FMUL, SL, VT, Two, N1); + return DAG.getNode(ISD::FMUL, SDLoc(N), VT, N0.getOperand(0), MulConsts); + } } // fold (fmul X, 2.0) -> (fadd X, X) Index: test/CodeGen/R600/llvm.sin.ll =================================================================== --- test/CodeGen/R600/llvm.sin.ll +++ test/CodeGen/R600/llvm.sin.ll @@ -1,53 +1,84 @@ -;RUN: llc -march=r600 -mcpu=redwood < %s | FileCheck -check-prefix=EG -check-prefix=FUNC %s -;RUN: llc -march=r600 -mcpu=SI < %s | FileCheck -check-prefix=SI -check-prefix=SI-SAFE -check-prefix=FUNC %s -;RUN: llc -march=r600 -mcpu=SI -enable-unsafe-fp-math < %s | FileCheck -check-prefix=SI -check-prefix=SI-UNSAFE -check-prefix=FUNC %s - -;FUNC-LABEL: test -;EG: MULADD_IEEE * -;EG: FRACT * -;EG: ADD * -;EG: SIN * T{{[0-9]+\.[XYZW], PV\.[XYZW]}} -;EG-NOT: SIN -;SI: V_MUL_F32 -;SI: V_FRACT_F32 -;SI: V_SIN_F32 -;SI-NOT: V_SIN_F32 - -define void @test(float addrspace(1)* %out, float %x) #1 { +; RUN: llc -march=r600 -mcpu=redwood < %s | FileCheck -check-prefix=EG -check-prefix=FUNC %s +; RUN: llc -march=r600 -mcpu=SI < %s | FileCheck -check-prefix=SI -check-prefix=SI-SAFE -check-prefix=FUNC %s +; RUN: llc -march=r600 -mcpu=SI -enable-unsafe-fp-math < %s | FileCheck -check-prefix=SI -check-prefix=SI-UNSAFE -check-prefix=FUNC %s + +; FUNC-LABEL: sin_f32 +; EG: MULADD_IEEE * +; EG: FRACT * +; EG: ADD * +; EG: SIN * T{{[0-9]+\.[XYZW], PV\.[XYZW]}} +; EG-NOT: SIN +; SI: V_MUL_F32 +; SI: V_FRACT_F32 +; SI: V_SIN_F32 +; SI-NOT: V_SIN_F32 + +define void @sin_f32(float addrspace(1)* %out, float %x) #1 { %sin = call float @llvm.sin.f32(float %x) store float %sin, float addrspace(1)* %out ret void } -;FUNC-LABEL: testf -;SI-UNSAFE: 4.774 -;SI-UNSAFE: V_MUL_F32 -;SI-SAFE: V_MUL_F32 -;SI-SAFE: V_MUL_F32 -;SI: V_FRACT_F32 -;SI: V_SIN_F32 -;SI-NOT: V_SIN_F32 - -define void @testf(float addrspace(1)* %out, float %x) #1 { - %y = fmul float 3.0, %x +; FUNC-LABEL: @sin_3x_f32 +; SI-UNSAFE-NOT: V_ADD_F32 +; SI-UNSAFE: 4.774648e-01 +; SI-UNSAFE: V_MUL_F32 +; SI-SAFE: V_MUL_F32 +; SI-SAFE: V_MUL_F32 +; SI: V_FRACT_F32 +; SI: V_SIN_F32 +; SI-NOT: V_SIN_F32 +define void @sin_3x_f32(float addrspace(1)* %out, float %x) #1 { + %y = fmul float 3.0, %x + %sin = call float @llvm.sin.f32(float %y) + store float %sin, float addrspace(1)* %out + ret void +} + +; FUNC-LABEL: @sin_2x_f32 +; SI-UNSAFE-NOT: V_ADD_F32 +; SI-UNSAFE: 3.183099e-01 +; SI-UNSAFE: V_MUL_F32 +; SI-SAFE: V_ADD_F32 +; SI-SAFE: V_MUL_F32 +; SI: V_FRACT_F32 +; SI: V_SIN_F32 +; SI-NOT: V_SIN_F32 +define void @sin_2x_f32(float addrspace(1)* %out, float %x) #1 { + %y = fmul float 2.0, %x + %sin = call float @llvm.sin.f32(float %y) + store float %sin, float addrspace(1)* %out + ret void +} + +; FUNC-LABEL: @test_2sin_f32 +; SI-UNSAFE: 3.183099e-01 +; SI-UNSAFE: V_MUL_F32 +; SI-SAFE: V_ADD_F32 +; SI-SAFE: V_MUL_F32 +; SI: V_FRACT_F32 +; SI: V_SIN_F32 +; SI-NOT: V_SIN_F32 +define void @test_2sin_f32(float addrspace(1)* %out, float %x) #1 { + %y = fmul float 2.0, %x %sin = call float @llvm.sin.f32(float %y) store float %sin, float addrspace(1)* %out ret void } -;FUNC-LABEL: testv -;EG: SIN * T{{[0-9]+\.[XYZW], PV\.[XYZW]}} -;EG: SIN * T{{[0-9]+\.[XYZW], PV\.[XYZW]}} -;EG: SIN * T{{[0-9]+\.[XYZW], PV\.[XYZW]}} -;EG: SIN * T{{[0-9]+\.[XYZW], PV\.[XYZW]}} -;EG-NOT: SIN -;SI: V_SIN_F32 -;SI: V_SIN_F32 -;SI: V_SIN_F32 -;SI: V_SIN_F32 -;SI-NOT: V_SIN_F32 - -define void @testv(<4 x float> addrspace(1)* %out, <4 x float> %vx) #1 { +; FUNC-LABEL: @sin_v4f32 +; EG: SIN * T{{[0-9]+\.[XYZW], PV\.[XYZW]}} +; EG: SIN * T{{[0-9]+\.[XYZW], PV\.[XYZW]}} +; EG: SIN * T{{[0-9]+\.[XYZW], PV\.[XYZW]}} +; EG: SIN * T{{[0-9]+\.[XYZW], PV\.[XYZW]}} +; EG-NOT: SIN +; SI: V_SIN_F32 +; SI: V_SIN_F32 +; SI: V_SIN_F32 +; SI: V_SIN_F32 +; SI-NOT: V_SIN_F32 + +define void @sin_v4f32(<4 x float> addrspace(1)* %out, <4 x float> %vx) #1 { %sin = call <4 x float> @llvm.sin.v4f32( <4 x float> %vx) store <4 x float> %sin, <4 x float> addrspace(1)* %out ret void