-
Notifications
You must be signed in to change notification settings - Fork 14.7k
[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
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dtcxzyw
commented
Aug 10, 2025
@llvm/pr-subscribers-llvm-transforms @llvm/pr-subscribers-llvm-ir Author: Yingwei Zheng (dtcxzyw) ChangesCloses #152824. Full diff: https://github.com/llvm/llvm-project/pull/152913.diff 3 Files Affected:
diff --git a/llvm/include/llvm/IR/GenericFloatingPointPredicateUtils.h b/llvm/include/llvm/IR/GenericFloatingPointPredicateUtils.h
index 8aac9d5b49dbb..6c592adbd7333 100644
--- a/llvm/include/llvm/IR/GenericFloatingPointPredicateUtils.h
+++ b/llvm/include/llvm/IR/GenericFloatingPointPredicateUtils.h
@@ -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;
diff --git a/llvm/test/Transforms/InstSimplify/floating-point-arithmetic.ll b/llvm/test/Transforms/InstSimplify/floating-point-arithmetic.ll
index ab4448b460bfc..820fff433e9e0 100644
--- a/llvm/test/Transforms/InstSimplify/floating-point-arithmetic.ll
+++ b/llvm/test/Transforms/InstSimplify/floating-point-arithmetic.ll
@@ -213,7 +213,7 @@ 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
@@ -221,7 +221,7 @@ define float @fmul_ninf_nnan_mul_zero_nsz(float nofpclass(inf nan) %f) {
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
@@ -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
+}
diff --git a/llvm/unittests/Analysis/ValueTrackingTest.cpp b/llvm/unittests/Analysis/ValueTrackingTest.cpp
index 6af20065213ac..559a0b724f383 100644
--- a/llvm/unittests/Analysis/ValueTrackingTest.cpp
+++ b/llvm/unittests/Analysis/ValueTrackingTest.cpp
@@ -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"
@@ -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) {
|
tgymnich
approved these changes
Aug 10, 2025
arsenm
approved these changes
Aug 10, 2025
/cherry-pick d8b1b46 |
/pull-request #152921 |
tru
pushed a commit
to llvmbot/llvm-project
that referenced
this pull request
Aug 11, 2025
Closes llvm#152824. (cherry picked from commit d8b1b46)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
llvm:analysis
Includes value tracking, cost tables and constant folding
llvm:instcombine
Covers the InstCombine, InstSimplify and AggressiveInstCombine passes
llvm:ir
llvm:transforms
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Closes #152824.