[ValueTracking] determine sign of 0.0 from select when matching min/max FP

In PR39475:
https://bugs.llvm.org/show_bug.cgi?id=39475
..we may fail to recognize/simplify fabs() in some cases because we do not 
canonicalize fcmp with a -0.0 operand.

Adding that canonicalization can cause regressions on min/max FP tests, so 
that's this patch: for the purpose of determining whether something is min/max, 
let the value returned by the select determine how we treat a 0.0 operand in the fcmp.

This patch doesn't actually change the -0.0 to +0.0. It just changes the analysis, so 
we don't fail to recognize equivalent min/max patterns that only differ in the 
signbit of 0.0.

Differential Revision: https://reviews.llvm.org/D54001

llvm-svn: 346097
This commit is contained in:
Sanjay Patel 2018-11-04 14:28:48 +00:00
parent d96bdd2402
commit e7c94ef1de
3 changed files with 89 additions and 54 deletions

View File

@ -4760,6 +4760,27 @@ static SelectPatternResult matchSelectPattern(CmpInst::Predicate Pred,
Value *TrueVal, Value *FalseVal,
Value *&LHS, Value *&RHS,
unsigned Depth) {
if (CmpInst::isFPPredicate(Pred)) {
// IEEE-754 ignores the sign of 0.0 in comparisons. So if the select has one
// 0.0 operand, set the compare's 0.0 operands to that same value for the
// purpose of identifying min/max. Disregard vector constants with undefined
// elements because those can not be back-propagated for analysis.
Value *OutputZeroVal = nullptr;
if (match(TrueVal, m_AnyZeroFP()) && !match(FalseVal, m_AnyZeroFP()) &&
!cast<Constant>(TrueVal)->containsUndefElement())
OutputZeroVal = TrueVal;
else if (match(FalseVal, m_AnyZeroFP()) && !match(TrueVal, m_AnyZeroFP()) &&
!cast<Constant>(FalseVal)->containsUndefElement())
OutputZeroVal = FalseVal;
if (OutputZeroVal) {
if (match(CmpLHS, m_AnyZeroFP()))
CmpLHS = OutputZeroVal;
if (match(CmpRHS, m_AnyZeroFP()))
CmpRHS = OutputZeroVal;
}
}
LHS = CmpLHS;
RHS = CmpRHS;

View File

@ -57,16 +57,15 @@ define double @t5(float %a) {
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 @@ define double @t6(float %a) {
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 @@ define double @t7(float %a) {
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 @@ define float @fmin_fmin_zero_mismatch(float %x) {
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

View File

@ -156,8 +156,8 @@ TEST_F(MatchSelectPatternTest, FMinMismatchConstantZero1) {
" %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 @@ TEST_F(MatchSelectPatternTest, FMinMismatchConstantZero2) {
" %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 @@ TEST_F(MatchSelectPatternTest, FMinMismatchConstantZero3) {
" %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 @@ TEST_F(MatchSelectPatternTest, FMinMismatchConstantZero4) {
" %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 @@ TEST_F(MatchSelectPatternTest, FMinMismatchConstantZero5) {
" %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 @@ TEST_F(MatchSelectPatternTest, FMinMismatchConstantZero6) {
" %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 @@ TEST_F(MatchSelectPatternTest, FMinMismatchConstantZero7) {
" %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 @@ TEST_F(MatchSelectPatternTest, FMinMismatchConstantZero8) {
" %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 @@ TEST_F(MatchSelectPatternTest, FMaxMismatchConstantZero1) {
" %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 @@ TEST_F(MatchSelectPatternTest, FMaxMismatchConstantZero2) {
" %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 @@ TEST_F(MatchSelectPatternTest, FMaxMismatchConstantZero3) {
" %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 @@ TEST_F(MatchSelectPatternTest, FMaxMismatchConstantZero4) {
" %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 @@ TEST_F(MatchSelectPatternTest, FMaxMismatchConstantZero5) {
" %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 @@ TEST_F(MatchSelectPatternTest, FMaxMismatchConstantZero6) {
" %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 @@ TEST_F(MatchSelectPatternTest, FMaxMismatchConstantZero7) {
" %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,7 +321,29 @@ TEST_F(MatchSelectPatternTest, FMaxMismatchConstantZero8) {
" %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.
// The sign of zero doesn't matter in fcmp.
expectPattern({SPF_FMAXNUM, SPNB_RETURNS_OTHER, true});
}
TEST_F(MatchSelectPatternTest, FMinMismatchConstantZeroVecUndef) {
parseAssembly(
"define <2 x float> @test(<2 x float> %a) {\n"
" %1 = fcmp ogt <2 x float> %a, <float -0.0, float -0.0>\n"
" %A = select <2 x i1> %1, <2 x float> <float undef, float 0.0>, <2 x float> %a\n"
" ret <2 x float> %A\n"
"}\n");
// An undef in a vector constant can not be back-propagated for this analysis.
expectPattern({SPF_UNKNOWN, SPNB_NA, false});
}
TEST_F(MatchSelectPatternTest, FMaxMismatchConstantZeroVecUndef) {
parseAssembly(
"define <2 x float> @test(<2 x float> %a) {\n"
" %1 = fcmp ogt <2 x float> %a, zeroinitializer\n"
" %A = select <2 x i1> %1, <2 x float> %a, <2 x float> <float -0.0, float undef>\n"
" ret <2 x float> %A\n"
"}\n");
// An undef in a vector constant can not be back-propagated for this analysis.
expectPattern({SPF_UNKNOWN, SPNB_NA, false});
}