Skip to content

[IR] Handle fabs LHS in fcmpImpliesClass #152913

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 10, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions llvm/include/llvm/IR/GenericFloatingPointPredicateUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,26 +151,40 @@ template <typename ContextT> class GenericFloatingPointPredicateUtils {
case FCmpInst::FCMP_UNO:
return exactClass(Src, fcNan);
case FCmpInst::FCMP_OGT: // x > 0
if (IsFabs)
return exactClass(Src, fcSubnormal | fcNormal | fcInf);
return exactClass(Src, fcPosSubnormal | fcPosNormal | fcPosInf);
case FCmpInst::FCMP_UGT: // isnan(x) || x > 0
if (IsFabs)
return exactClass(Src, fcSubnormal | fcNormal | fcInf | fcNan);
return exactClass(Src, fcPosSubnormal | fcPosNormal | fcPosInf | fcNan);
case FCmpInst::FCMP_OGE: // x >= 0
if (IsFabs)
return exactClass(Src, ~fcNan);
return exactClass(Src, fcPositive | fcNegZero);
case FCmpInst::FCMP_UGE: // isnan(x) || x >= 0
if (IsFabs)
return exactClass(Src, fcAllFlags);
return exactClass(Src, fcPositive | fcNegZero | fcNan);
case FCmpInst::FCMP_OLT: // x < 0
if (IsFabs)
return exactClass(Src, fcNone);
return exactClass(Src, fcNegSubnormal | fcNegNormal | fcNegInf);
case FCmpInst::FCMP_ULT: // isnan(x) || x < 0
if (IsFabs)
return exactClass(Src, fcNan);
return exactClass(Src, fcNegSubnormal | fcNegNormal | fcNegInf | fcNan);
case FCmpInst::FCMP_OLE: // x <= 0
if (IsFabs)
return exactClass(Src, fcZero);
return exactClass(Src, fcNegative | fcPosZero);
case FCmpInst::FCMP_ULE: // isnan(x) || x <= 0
if (IsFabs)
return exactClass(Src, fcZero | fcNan);
return exactClass(Src, fcNegative | fcPosZero | fcNan);
default:
llvm_unreachable("all compare types are handled");
}

return {Invalid, fcAllFlags, fcAllFlags};
}

const bool IsDenormalRHS = (OrigClass & fcSubnormal) == OrigClass;
Expand Down
21 changes: 19 additions & 2 deletions llvm/test/Transforms/InstSimplify/floating-point-arithmetic.ll
Original file line number Diff line number Diff line change
Expand Up @@ -213,15 +213,15 @@ define double @fmul_nnan_ninf_nneg_n0.0_commute(i127 %x) {

define float @fmul_ninf_nnan_mul_zero_nsz(float nofpclass(inf nan) %f) {
; CHECK-LABEL: @fmul_ninf_nnan_mul_zero_nsz(
; CHECK-NEXT: ret float 0.000000e+00
; CHECK-NEXT: ret float 0.000000e+00
;
%r = fmul nsz float %f, 0.0
ret float %r
}

define float @fmul_ninf_nnan_mul_nzero_nsz(float nofpclass(inf nan) %f) {
; CHECK-LABEL: @fmul_ninf_nnan_mul_nzero_nsz(
; CHECK-NEXT: ret float 0.000000e+00
; CHECK-NEXT: ret float 0.000000e+00
;
%r = fmul nsz float %f, -0.0
ret float %r
Expand Down Expand Up @@ -1255,3 +1255,20 @@ define i1 @fptrunc_round_unknown_positive(double %unknown) {
%cmp = fcmp nnan oge float %op, 0.0
ret i1 %cmp
}

define half @fabs_select_fabs(half noundef %x) {
; CHECK-LABEL: @fabs_select_fabs(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[ABS1:%.*]] = call half @llvm.fabs.f16(half [[X:%.*]])
; CHECK-NEXT: [[CMP:%.*]] = fcmp ogt half [[ABS1]], 0xH0000
; CHECK-NEXT: [[SEL:%.*]] = select i1 [[CMP]], half [[X]], half 0xH0000
; CHECK-NEXT: [[ABS2:%.*]] = call half @llvm.fabs.f16(half [[SEL]])
; CHECK-NEXT: ret half [[ABS2]]
;
entry:
%abs1 = call half @llvm.fabs.f16(half %x)
%cmp = fcmp ogt half %abs1, 0xH0000
%sel = select i1 %cmp, half %x, half 0xH0000
%abs2 = call half @llvm.fabs.f16(half %sel)
ret half %abs2
}
36 changes: 36 additions & 0 deletions llvm/unittests/Analysis/ValueTrackingTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//===----------------------------------------------------------------------===//

#include "llvm/Analysis/ValueTracking.h"
#include "llvm/ADT/FloatingPointMode.h"
#include "llvm/Analysis/AssumptionCache.h"
#include "llvm/Analysis/FloatingPointPredicateUtils.h"
#include "llvm/AsmParser/Parser.h"
Expand Down Expand Up @@ -2208,6 +2209,41 @@ TEST_F(ComputeKnownFPClassTest, Constants) {
}
}

TEST_F(ComputeKnownFPClassTest, fcmpImpliesClass_fabs_zero) {
parseAssembly("define float @test(float %x) {\n"
" %A = call float @llvm.fabs.f32(float %x)\n"
" ret float %A\n"
"}\n");
EXPECT_EQ(std::get<1>(fcmpImpliesClass(FCmpInst::FCMP_OEQ, *F, A, fcZero)),
fcZero);
EXPECT_EQ(std::get<1>(fcmpImpliesClass(FCmpInst::FCMP_UEQ, *F, A, fcZero)),
fcZero | fcNan);
EXPECT_EQ(std::get<1>(fcmpImpliesClass(FCmpInst::FCMP_UNE, *F, A, fcZero)),
~fcZero);
EXPECT_EQ(std::get<1>(fcmpImpliesClass(FCmpInst::FCMP_ONE, *F, A, fcZero)),
~fcNan & ~fcZero);
EXPECT_EQ(std::get<1>(fcmpImpliesClass(FCmpInst::FCMP_ORD, *F, A, fcZero)),
~fcNan);
EXPECT_EQ(std::get<1>(fcmpImpliesClass(FCmpInst::FCMP_UNO, *F, A, fcZero)),
fcNan);
EXPECT_EQ(std::get<1>(fcmpImpliesClass(FCmpInst::FCMP_OGT, *F, A, fcZero)),
fcSubnormal | fcNormal | fcInf);
EXPECT_EQ(std::get<1>(fcmpImpliesClass(FCmpInst::FCMP_UGT, *F, A, fcZero)),
fcSubnormal | fcNormal | fcInf | fcNan);
EXPECT_EQ(std::get<1>(fcmpImpliesClass(FCmpInst::FCMP_OGE, *F, A, fcZero)),
~fcNan);
EXPECT_EQ(std::get<1>(fcmpImpliesClass(FCmpInst::FCMP_UGE, *F, A, fcZero)),
fcAllFlags);
EXPECT_EQ(std::get<1>(fcmpImpliesClass(FCmpInst::FCMP_OLT, *F, A, fcZero)),
fcNone);
EXPECT_EQ(std::get<1>(fcmpImpliesClass(FCmpInst::FCMP_ULT, *F, A, fcZero)),
fcNan);
EXPECT_EQ(std::get<1>(fcmpImpliesClass(FCmpInst::FCMP_OLE, *F, A, fcZero)),
fcZero);
EXPECT_EQ(std::get<1>(fcmpImpliesClass(FCmpInst::FCMP_ULE, *F, A, fcZero)),
fcZero | fcNan);
}

TEST_F(ValueTrackingTest, isNonZeroRecurrence) {
parseAssembly(R"(
define i1 @test(i8 %n, i8 %r) {
Expand Down
Loading