Index: include/llvm/ADT/APFloat.h =================================================================== --- include/llvm/ADT/APFloat.h +++ include/llvm/ADT/APFloat.h @@ -1101,6 +1101,12 @@ return X; } +/// \brief Returns the negated value of the argument. +inline APFloat neg(APFloat X) { + X.changeSign(); + return X; +} + /// Implements IEEE minNum semantics. Returns the smaller of the 2 arguments if /// both are not NaN. If either argument is a NaN, returns the other argument. LLVM_READONLY Index: lib/CodeGen/SelectionDAG/DAGCombiner.cpp =================================================================== --- lib/CodeGen/SelectionDAG/DAGCombiner.cpp +++ lib/CodeGen/SelectionDAG/DAGCombiner.cpp @@ -607,10 +607,16 @@ switch (Op.getOpcode()) { default: return false; - case ISD::ConstantFP: - // Don't invert constant FP values after legalize. The negated constant - // isn't necessarily legal. - return LegalOperations ? 0 : 1; + case ISD::ConstantFP: { + if (!LegalOperations) + return 1; + + // Don't invert constant FP values after legalize, unless the target the + // negated constant is necessarily legal. + EVT VT = Op.getValueType(); + return TLI.isOperationLegal(ISD::ConstantFP, VT) || + TLI.isFPImmLegal(neg(cast(Op)->getValueAPF()), VT); + } case ISD::FADD: // FIXME: determine better conditions for this xform. if (!Options->UnsafeFPMath) return 0; Index: test/CodeGen/AMDGPU/fneg-combines.ll =================================================================== --- test/CodeGen/AMDGPU/fneg-combines.ll +++ test/CodeGen/AMDGPU/fneg-combines.ll @@ -1235,8 +1235,7 @@ ; GCN-LABEL: {{^}}v_fneg_sin_f32: ; GCN: {{buffer|flat}}_load_dword [[A:v[0-9]+]] -; GCN: v_mov_b32_e32 [[K:v[0-9]+]], 0x3e22f983 -; GCN: v_mul_f32_e64 [[MUL:v[0-9]+]], [[K]], -[[A]] +; GCN: v_mul_f32_e32 [[MUL:v[0-9]+]], 0xbe22f983, [[A]] ; GCN: v_fract_f32_e32 [[FRACT:v[0-9]+]], [[MUL]] ; GCN: v_sin_f32_e32 [[RESULT:v[0-9]+]], [[FRACT]] ; GCN: buffer_store_dword [[RESULT]] Index: unittests/ADT/APFloatTest.cpp =================================================================== --- unittests/ADT/APFloatTest.cpp +++ unittests/ADT/APFloatTest.cpp @@ -2829,6 +2829,28 @@ EXPECT_TRUE(PSmallestNormalized.bitwiseIsEqual(abs(MSmallestNormalized))); } +TEST(APFloatTest, neg) { + APFloat One = APFloat(APFloat::IEEEsingle(), "1.0"); + APFloat NegOne = APFloat(APFloat::IEEEsingle(), "-1.0"); + APFloat Zero = APFloat::getZero(APFloat::IEEEsingle(), false); + APFloat NegZero = APFloat::getZero(APFloat::IEEEsingle(), true); + APFloat Inf = APFloat::getInf(APFloat::IEEEsingle(), false); + APFloat NegInf = APFloat::getInf(APFloat::IEEEsingle(), true); + APFloat QNaN = APFloat::getNaN(APFloat::IEEEsingle(), false); + APFloat NegQNaN = APFloat::getNaN(APFloat::IEEEsingle(), true); + + EXPECT_TRUE(NegOne.bitwiseIsEqual(neg(One))); + EXPECT_TRUE(One.bitwiseIsEqual(neg(NegOne))); + EXPECT_TRUE(NegZero.bitwiseIsEqual(neg(Zero))); + EXPECT_TRUE(Zero.bitwiseIsEqual(neg(NegZero))); + EXPECT_TRUE(NegInf.bitwiseIsEqual(neg(Inf))); + EXPECT_TRUE(Inf.bitwiseIsEqual(neg(NegInf))); + EXPECT_TRUE(NegInf.bitwiseIsEqual(neg(Inf))); + EXPECT_TRUE(Inf.bitwiseIsEqual(neg(NegInf))); + EXPECT_TRUE(NegQNaN.bitwiseIsEqual(neg(QNaN))); + EXPECT_TRUE(QNaN.bitwiseIsEqual(neg(NegQNaN))); +} + TEST(APFloatTest, ilogb) { EXPECT_EQ(-1074, ilogb(APFloat::getSmallest(APFloat::IEEEdouble(), false))); EXPECT_EQ(-1074, ilogb(APFloat::getSmallest(APFloat::IEEEdouble(), true)));