Skip to content

Commit c14b181

Browse files
arsenmchencha3
authored andcommitted
ValueTracking: Fix bug with fcmp false to nan constant
If we had a comparison to a literal nan with a false predicate, we were incorrectly treating it as an unordered compare. This was correct for fcmp true, but not fcmp false. I noticed this in the review for e44d3b3 but misdiagnosed the reason. Also change the test for the fcmp true case to be more useful, but it wasn't wrong previously.
1 parent cf25fb0 commit c14b181

File tree

2 files changed

+13
-13
lines changed

2 files changed

+13
-13
lines changed

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3972,10 +3972,16 @@ std::tuple<Value *, FPClassTest, FPClassTest>
39723972
llvm::fcmpImpliesClass(CmpInst::Predicate Pred, const Function &F, Value *LHS,
39733973
FPClassTest RHSClass, bool LookThroughSrc) {
39743974
assert(RHSClass != fcNone);
3975+
Value *Src = LHS;
3976+
3977+
if (Pred == FCmpInst::FCMP_TRUE)
3978+
return exactClass(Src, fcAllFlags);
3979+
3980+
if (Pred == FCmpInst::FCMP_FALSE)
3981+
return exactClass(Src, fcNone);
39753982

39763983
const FPClassTest OrigClass = RHSClass;
39773984

3978-
Value *Src = LHS;
39793985
const bool IsNegativeRHS = (RHSClass & fcNegative) == RHSClass;
39803986
const bool IsPositiveRHS = (RHSClass & fcPositive) == RHSClass;
39813987
const bool IsNaN = (RHSClass & ~fcNan) == fcNone;
@@ -3994,12 +4000,6 @@ llvm::fcmpImpliesClass(CmpInst::Predicate Pred, const Function &F, Value *LHS,
39944000
if (Pred == FCmpInst::FCMP_UNO)
39954001
return exactClass(Src, fcNan);
39964002

3997-
if (Pred == FCmpInst::FCMP_TRUE)
3998-
return exactClass(Src, fcAllFlags);
3999-
4000-
if (Pred == FCmpInst::FCMP_FALSE)
4001-
return exactClass(Src, fcNone);
4002-
40034003
const bool IsFabs = LookThroughSrc && match(LHS, m_FAbs(m_Value(Src)));
40044004
if (IsFabs)
40054005
RHSClass = llvm::inverse_fabs(RHSClass);

llvm/test/Transforms/Attributor/nofpclass-implied-by-fcmp.ll

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2641,8 +2641,8 @@ define float @assume_false_smallest_normal(float %arg) {
26412641
}
26422642

26432643
define float @clamp_false_nan(float %arg) {
2644-
; CHECK-LABEL: define nofpclass(nan inf nzero sub norm) float @clamp_false_nan(
2645-
; CHECK-SAME: float returned nofpclass(nan inf nzero sub norm) [[ARG:%.*]]) #[[ATTR2]] {
2644+
; CHECK-LABEL: define float @clamp_false_nan(
2645+
; CHECK-SAME: float returned [[ARG:%.*]]) #[[ATTR2]] {
26462646
; CHECK-NEXT: ret float [[ARG]]
26472647
;
26482648
%fcmp = fcmp false float %arg, 0x7FF8000000000000
@@ -2784,12 +2784,12 @@ define float @clamp_true_smallest_normal_0.0(float %arg) {
27842784
}
27852785

27862786
define float @clamp_true_nan(float %arg) {
2787-
; CHECK-LABEL: define noundef nofpclass(nan inf nzero sub norm) float @clamp_true_nan(
2788-
; CHECK-SAME: float nofpclass(nan inf nzero sub norm) [[ARG:%.*]]) #[[ATTR2]] {
2789-
; CHECK-NEXT: ret float 0.000000e+00
2787+
; CHECK-LABEL: define float @clamp_true_nan(
2788+
; CHECK-SAME: float returned [[ARG:%.*]]) #[[ATTR2]] {
2789+
; CHECK-NEXT: ret float [[ARG]]
27902790
;
27912791
%fcmp = fcmp true float %arg, 0x7FF8000000000000
2792-
%select = select i1 %fcmp, float 0.0, float %arg
2792+
%select = select i1 %fcmp, float %arg, float 0.0
27932793
ret float %select
27942794
}
27952795

0 commit comments

Comments
 (0)