Index: lib/Analysis/ValueTracking.cpp =================================================================== --- lib/Analysis/ValueTracking.cpp +++ lib/Analysis/ValueTracking.cpp @@ -4743,6 +4743,36 @@ Value *TrueVal, Value *FalseVal, Value *&LHS, Value *&RHS, unsigned Depth) { + if (CmpInst::isFPPredicate(Pred)) { + // If an FP compare has a 0.0 operand with a signbit that does not match + // a 0.0 operand of the select, then change the compare operand to match. + // This is allowed by IEEE-754: comparison ignores the sign of zero. + if (match(TrueVal, m_PosZeroFP()) && !match(FalseVal, m_NegZeroFP())) { + if (match(CmpLHS, m_NegZeroFP())) + CmpLHS = TrueVal; + if (match(CmpRHS, m_NegZeroFP())) + CmpRHS = TrueVal; + } + if (match(TrueVal, m_NegZeroFP()) && !match(FalseVal, m_PosZeroFP())) { + if (match(CmpLHS, m_PosZeroFP())) + CmpLHS = TrueVal; + if (match(CmpRHS, m_PosZeroFP())) + CmpRHS = TrueVal; + } + if (match(FalseVal, m_PosZeroFP()) && !match(TrueVal, m_NegZeroFP())) { + if (match(CmpLHS, m_NegZeroFP())) + CmpLHS = FalseVal; + if (match(CmpRHS, m_NegZeroFP())) + CmpRHS = FalseVal; + } + if (match(FalseVal, m_NegZeroFP()) && !match(TrueVal, m_PosZeroFP())) { + if (match(CmpLHS, m_PosZeroFP())) + CmpLHS = FalseVal; + if (match(CmpRHS, m_PosZeroFP())) + CmpRHS = FalseVal; + } + } + LHS = CmpLHS; RHS = CmpRHS; Index: test/Transforms/InstCombine/minmax-fp.ll =================================================================== --- test/Transforms/InstCombine/minmax-fp.ll +++ test/Transforms/InstCombine/minmax-fp.ll @@ -57,16 +57,15 @@ ret double %3 } -; TODO: -; From IEEE754: "Comparisons shall ignore the sign of zero (so +0 = −0)." +; From IEEE754: "Comparisons shall ignore the sign of zero (so +0 = -0)." ; So the compare constant may be treated as +0.0, and we sink the fpext. define double @t6(float %a) { ; CHECK-LABEL: @t6( -; CHECK-NEXT: [[TMP1:%.*]] = fcmp ult float [[A:%.*]], -0.000000e+00 -; CHECK-NEXT: [[TMP2:%.*]] = fpext float [[A]] to double -; CHECK-NEXT: [[TMP3:%.*]] = select i1 [[TMP1]], double [[TMP2]], double 0.000000e+00 -; CHECK-NEXT: ret double [[TMP3]] +; CHECK-NEXT: [[DOTINV:%.*]] = fcmp oge float [[A:%.*]], 0.000000e+00 +; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[DOTINV]], float 0.000000e+00, float [[A]] +; CHECK-NEXT: [[TMP2:%.*]] = fpext float [[TMP1]] to double +; CHECK-NEXT: ret double [[TMP2]] ; %1 = fcmp ult float %a, -0.0 %2 = fpext float %a to double @@ -74,16 +73,15 @@ ret double %3 } -; TODO: -; From IEEE754: "Comparisons shall ignore the sign of zero (so +0 = −0)." +; From IEEE754: "Comparisons shall ignore the sign of zero (so +0 = -0)." ; So the compare constant may be treated as -0.0, and we sink the fpext. define double @t7(float %a) { ; CHECK-LABEL: @t7( -; CHECK-NEXT: [[TMP1:%.*]] = fcmp ult float [[A:%.*]], 0.000000e+00 -; CHECK-NEXT: [[TMP2:%.*]] = fpext float [[A]] to double -; CHECK-NEXT: [[TMP3:%.*]] = select i1 [[TMP1]], double [[TMP2]], double -0.000000e+00 -; CHECK-NEXT: ret double [[TMP3]] +; CHECK-NEXT: [[DOTINV:%.*]] = fcmp oge float [[A:%.*]], -0.000000e+00 +; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[DOTINV]], float -0.000000e+00, float [[A]] +; CHECK-NEXT: [[TMP2:%.*]] = fpext float [[TMP1]] to double +; CHECK-NEXT: ret double [[TMP2]] ; %1 = fcmp ult float %a, 0.0 %2 = fpext float %a to double @@ -91,15 +89,12 @@ ret double %3 } -; TODO: ; min(min(x, 0.0), 0.0) --> min(x, 0.0) define float @fmin_fmin_zero_mismatch(float %x) { ; CHECK-LABEL: @fmin_fmin_zero_mismatch( -; CHECK-NEXT: [[CMP1:%.*]] = fcmp olt float [[X:%.*]], -0.000000e+00 -; CHECK-NEXT: [[MIN1:%.*]] = select i1 [[CMP1]], float [[X]], float 0.000000e+00 -; CHECK-NEXT: [[CMP2:%.*]] = fcmp olt float [[MIN1]], 0.000000e+00 -; CHECK-NEXT: [[MIN2:%.*]] = select i1 [[CMP2]], float [[MIN1]], float 0.000000e+00 +; CHECK-NEXT: [[TMP1:%.*]] = fcmp olt float [[X:%.*]], 0.000000e+00 +; CHECK-NEXT: [[MIN2:%.*]] = select i1 [[TMP1]], float [[X]], float 0.000000e+00 ; CHECK-NEXT: ret float [[MIN2]] ; %cmp1 = fcmp olt float %x, -0.0 @@ -109,16 +104,13 @@ ret float %min2 } -; TODO: ; max(max(x, -0.0), -0.0) --> max(x, -0.0) define float @fmax_fmax_zero_mismatch(float %x) { ; CHECK-LABEL: @fmax_fmax_zero_mismatch( -; CHECK-NEXT: [[CMP1:%.*]] = fcmp ogt float [[X:%.*]], 0.000000e+00 -; CHECK-NEXT: [[MAX1:%.*]] = select i1 [[CMP1]], float [[X]], float -0.000000e+00 -; CHECK-NEXT: [[CMP2:%.*]] = fcmp olt float [[MAX1]], 0.000000e+00 -; CHECK-NEXT: [[MAX2:%.*]] = select i1 [[CMP2]], float -0.000000e+00, float [[MAX1]] -; CHECK-NEXT: ret float [[MAX2]] +; CHECK-NEXT: [[TMP1:%.*]] = fcmp ogt float [[X:%.*]], -0.000000e+00 +; CHECK-NEXT: [[MAX11:%.*]] = select i1 [[TMP1]], float [[X]], float -0.000000e+00 +; CHECK-NEXT: ret float [[MAX11]] ; %cmp1 = fcmp ogt float %x, 0.0 %max1 = select i1 %cmp1, float %x, float -0.0 Index: unittests/Analysis/ValueTrackingTest.cpp =================================================================== --- unittests/Analysis/ValueTrackingTest.cpp +++ unittests/Analysis/ValueTrackingTest.cpp @@ -156,8 +156,8 @@ " %A = select i1 %1, float 0.0, float %a\n" " ret float %A\n" "}\n"); - // FIXME: The sign of zero doesn't matter in fcmp. - expectPattern({SPF_UNKNOWN, SPNB_NA, false}); + // The sign of zero doesn't matter in fcmp. + expectPattern({SPF_FMINNUM, SPNB_RETURNS_NAN, true}); } TEST_F(MatchSelectPatternTest, FMinMismatchConstantZero2) { @@ -167,8 +167,8 @@ " %A = select i1 %1, float 0.0, float %a\n" " ret float %A\n" "}\n"); - // FIXME: The sign of zero doesn't matter in fcmp. - expectPattern({SPF_UNKNOWN, SPNB_NA, false}); + // The sign of zero doesn't matter in fcmp. + expectPattern({SPF_FMINNUM, SPNB_RETURNS_NAN, false}); } TEST_F(MatchSelectPatternTest, FMinMismatchConstantZero3) { @@ -178,8 +178,8 @@ " %A = select i1 %1, float -0.0, float %a\n" " ret float %A\n" "}\n"); - // FIXME: The sign of zero doesn't matter in fcmp. - expectPattern({SPF_UNKNOWN, SPNB_NA, false}); + // The sign of zero doesn't matter in fcmp. + expectPattern({SPF_FMINNUM, SPNB_RETURNS_NAN, true}); } TEST_F(MatchSelectPatternTest, FMinMismatchConstantZero4) { @@ -189,8 +189,8 @@ " %A = select i1 %1, float -0.0, float %a\n" " ret float %A\n" "}\n"); - // FIXME: The sign of zero doesn't matter in fcmp. - expectPattern({SPF_UNKNOWN, SPNB_NA, false}); + // The sign of zero doesn't matter in fcmp. + expectPattern({SPF_FMINNUM, SPNB_RETURNS_NAN, false}); } TEST_F(MatchSelectPatternTest, FMinMismatchConstantZero5) { @@ -200,8 +200,8 @@ " %A = select i1 %1, float %a, float 0.0\n" " ret float %A\n" "}\n"); - // FIXME: The sign of zero doesn't matter in fcmp. - expectPattern({SPF_UNKNOWN, SPNB_NA, false}); + // The sign of zero doesn't matter in fcmp. + expectPattern({SPF_FMINNUM, SPNB_RETURNS_OTHER, false}); } TEST_F(MatchSelectPatternTest, FMinMismatchConstantZero6) { @@ -211,8 +211,8 @@ " %A = select i1 %1, float %a, float 0.0\n" " ret float %A\n" "}\n"); - // FIXME: The sign of zero doesn't matter in fcmp. - expectPattern({SPF_UNKNOWN, SPNB_NA, false}); + // The sign of zero doesn't matter in fcmp. + expectPattern({SPF_FMINNUM, SPNB_RETURNS_OTHER, true}); } TEST_F(MatchSelectPatternTest, FMinMismatchConstantZero7) { @@ -222,8 +222,8 @@ " %A = select i1 %1, float %a, float -0.0\n" " ret float %A\n" "}\n"); - // FIXME: The sign of zero doesn't matter in fcmp. - expectPattern({SPF_UNKNOWN, SPNB_NA, false}); + // The sign of zero doesn't matter in fcmp. + expectPattern({SPF_FMINNUM, SPNB_RETURNS_OTHER, false}); } TEST_F(MatchSelectPatternTest, FMinMismatchConstantZero8) { @@ -233,8 +233,8 @@ " %A = select i1 %1, float %a, float -0.0\n" " ret float %A\n" "}\n"); - // FIXME: The sign of zero doesn't matter in fcmp. - expectPattern({SPF_UNKNOWN, SPNB_NA, false}); + // The sign of zero doesn't matter in fcmp. + expectPattern({SPF_FMINNUM, SPNB_RETURNS_OTHER, true}); } TEST_F(MatchSelectPatternTest, FMaxMismatchConstantZero1) { @@ -244,8 +244,8 @@ " %A = select i1 %1, float 0.0, float %a\n" " ret float %A\n" "}\n"); - // FIXME: The sign of zero doesn't matter in fcmp. - expectPattern({SPF_UNKNOWN, SPNB_NA, false}); + // The sign of zero doesn't matter in fcmp. + expectPattern({SPF_FMAXNUM, SPNB_RETURNS_NAN, true}); } TEST_F(MatchSelectPatternTest, FMaxMismatchConstantZero2) { @@ -255,8 +255,8 @@ " %A = select i1 %1, float 0.0, float %a\n" " ret float %A\n" "}\n"); - // FIXME: The sign of zero doesn't matter in fcmp. - expectPattern({SPF_UNKNOWN, SPNB_NA, false}); + // The sign of zero doesn't matter in fcmp. + expectPattern({SPF_FMAXNUM, SPNB_RETURNS_NAN, false}); } TEST_F(MatchSelectPatternTest, FMaxMismatchConstantZero3) { @@ -266,8 +266,8 @@ " %A = select i1 %1, float -0.0, float %a\n" " ret float %A\n" "}\n"); - // FIXME: The sign of zero doesn't matter in fcmp. - expectPattern({SPF_UNKNOWN, SPNB_NA, false}); + // The sign of zero doesn't matter in fcmp. + expectPattern({SPF_FMAXNUM, SPNB_RETURNS_NAN, true}); } TEST_F(MatchSelectPatternTest, FMaxMismatchConstantZero4) { @@ -277,8 +277,8 @@ " %A = select i1 %1, float -0.0, float %a\n" " ret float %A\n" "}\n"); - // FIXME: The sign of zero doesn't matter in fcmp. - expectPattern({SPF_UNKNOWN, SPNB_NA, false}); + // The sign of zero doesn't matter in fcmp. + expectPattern({SPF_FMAXNUM, SPNB_RETURNS_NAN, false}); } TEST_F(MatchSelectPatternTest, FMaxMismatchConstantZero5) { @@ -288,8 +288,8 @@ " %A = select i1 %1, float %a, float 0.0\n" " ret float %A\n" "}\n"); - // FIXME: The sign of zero doesn't matter in fcmp. - expectPattern({SPF_UNKNOWN, SPNB_NA, false}); + // The sign of zero doesn't matter in fcmp. + expectPattern({SPF_FMAXNUM, SPNB_RETURNS_OTHER, false}); } TEST_F(MatchSelectPatternTest, FMaxMismatchConstantZero6) { @@ -299,8 +299,8 @@ " %A = select i1 %1, float %a, float 0.0\n" " ret float %A\n" "}\n"); - // FIXME: The sign of zero doesn't matter in fcmp. - expectPattern({SPF_UNKNOWN, SPNB_NA, false}); + // The sign of zero doesn't matter in fcmp. + expectPattern({SPF_FMAXNUM, SPNB_RETURNS_OTHER, true}); } TEST_F(MatchSelectPatternTest, FMaxMismatchConstantZero7) { @@ -310,8 +310,8 @@ " %A = select i1 %1, float %a, float -0.0\n" " ret float %A\n" "}\n"); - // FIXME: The sign of zero doesn't matter in fcmp. - expectPattern({SPF_UNKNOWN, SPNB_NA, false}); + // The sign of zero doesn't matter in fcmp. + expectPattern({SPF_FMAXNUM, SPNB_RETURNS_OTHER, false}); } TEST_F(MatchSelectPatternTest, FMaxMismatchConstantZero8) { @@ -321,8 +321,8 @@ " %A = select i1 %1, float %a, float -0.0\n" " ret float %A\n" "}\n"); - // FIXME: The sign of zero doesn't matter in fcmp. - expectPattern({SPF_UNKNOWN, SPNB_NA, false}); + // The sign of zero doesn't matter in fcmp. + expectPattern({SPF_FMAXNUM, SPNB_RETURNS_OTHER, true}); } TEST_F(MatchSelectPatternTest, VectorFMinimum) {