-
Notifications
You must be signed in to change notification settings - Fork 14.8k
[InstCombine] Handle isnormal idiom #125454
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
@llvm/pr-subscribers-llvm-transforms Author: Yingwei Zheng (dtcxzyw) ChangesThis patch improves the codegen of Rust's Full diff: https://github.com/llvm/llvm-project/pull/125454.diff 2 Files Affected:
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
index ca8a20b4b7312d..55b6d4880814fe 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
@@ -3500,6 +3500,22 @@ Value *InstCombinerImpl::foldAndOrOfICmps(ICmpInst *LHS, ICmpInst *RHS,
}
}
+ // (X & ExpMask) != 0 && (X & ExpMask) != ExpMask -> isnormal(X)
+ // (X & ExpMask) == 0 || (X & ExpMask) == ExpMask -> !isnormal(X)
+ Value *X;
+ const APInt *MaskC;
+ if (LHS0 == RHS0 && PredL == PredR &&
+ PredL == (IsAnd ? ICmpInst::ICMP_NE : ICmpInst::ICMP_EQ) &&
+ !I.getFunction()->hasFnAttribute(Attribute::NoImplicitFloat) &&
+ match(LHS0, m_And(m_ElementWiseBitCast(m_Value(X)), m_APInt(MaskC))) &&
+ X->getType()->getScalarType()->isIEEELikeFPTy() &&
+ APFloat(X->getType()->getScalarType()->getFltSemantics(), *MaskC)
+ .isPosInfinity() &&
+ ((LHSC->isZero() && *RHSC == *MaskC) ||
+ (RHSC->isZero() && *LHSC == *MaskC)))
+ return Builder.createIsFPClass(X, IsAnd ? FPClassTest::fcNormal
+ : ~FPClassTest::fcNormal);
+
return foldAndOrOfICmpsUsingRanges(LHS, RHS, IsAnd);
}
diff --git a/llvm/test/Transforms/InstCombine/fpclass-check-idioms.ll b/llvm/test/Transforms/InstCombine/fpclass-check-idioms.ll
index 66970a9d48ddf1..1d7e2bd96d1897 100644
--- a/llvm/test/Transforms/InstCombine/fpclass-check-idioms.ll
+++ b/llvm/test/Transforms/InstCombine/fpclass-check-idioms.ll
@@ -877,6 +877,162 @@ define i1 @isnan_idiom_ppc_fp128(ppc_fp128 %x) {
ret i1 %ret
}
+define i1 @fpclass_test_normal(float %num) {
+; CHECK-LABEL: define i1 @fpclass_test_normal(
+; CHECK-SAME: float [[NUM:%.*]]) {
+; CHECK-NEXT: [[RES:%.*]] = call i1 @llvm.is.fpclass.f32(float [[NUM]], i32 264)
+; CHECK-NEXT: ret i1 [[RES]]
+;
+ %cast = bitcast float %num to i32
+ %masked = and i32 %cast, 2139095040
+ %test1 = icmp ne i32 %masked, 2139095040
+ %test2 = icmp ne i32 %masked, 0
+ %res = and i1 %test1, %test2
+ ret i1 %res
+}
+
+define i1 @fpclass_test_normal_half(half %num) {
+; CHECK-LABEL: define i1 @fpclass_test_normal_half(
+; CHECK-SAME: half [[NUM:%.*]]) {
+; CHECK-NEXT: [[RES:%.*]] = call i1 @llvm.is.fpclass.f16(half [[NUM]], i32 264)
+; CHECK-NEXT: ret i1 [[RES]]
+;
+ %cast = bitcast half %num to i16
+ %masked = and i16 %cast, 31744
+ %test1 = icmp ne i16 %masked, 31744
+ %test2 = icmp ne i16 %masked, 0
+ %res = and i1 %test1, %test2
+ ret i1 %res
+}
+
+define <2 x i1> @fpclass_test_normal_half_vec(<2 x half> %num) {
+; CHECK-LABEL: define <2 x i1> @fpclass_test_normal_half_vec(
+; CHECK-SAME: <2 x half> [[NUM:%.*]]) {
+; CHECK-NEXT: [[RES:%.*]] = call <2 x i1> @llvm.is.fpclass.v2f16(<2 x half> [[NUM]], i32 264)
+; CHECK-NEXT: ret <2 x i1> [[RES]]
+;
+ %cast = bitcast <2 x half> %num to <2 x i16>
+ %masked = and <2 x i16> %cast, splat(i16 31744)
+ %test1 = icmp ne <2 x i16> %masked, splat(i16 31744)
+ %test2 = icmp ne <2 x i16> %masked, zeroinitializer
+ %res = and <2 x i1> %test1, %test2
+ ret <2 x i1> %res
+}
+
+define i1 @fpclass_test_not_normal(float %num) {
+; CHECK-LABEL: define i1 @fpclass_test_not_normal(
+; CHECK-SAME: float [[NUM:%.*]]) {
+; CHECK-NEXT: [[RES:%.*]] = call i1 @llvm.is.fpclass.f32(float [[NUM]], i32 759)
+; CHECK-NEXT: ret i1 [[RES]]
+;
+ %cast = bitcast float %num to i32
+ %masked = and i32 %cast, 2139095040
+ %test1 = icmp eq i32 %masked, 2139095040
+ %test2 = icmp eq i32 %masked, 0
+ %res = or i1 %test1, %test2
+ ret i1 %res
+}
+
+define i1 @fpclass_test_normal_commuted(float %num) {
+; CHECK-LABEL: define i1 @fpclass_test_normal_commuted(
+; CHECK-SAME: float [[NUM:%.*]]) {
+; CHECK-NEXT: [[RES:%.*]] = call i1 @llvm.is.fpclass.f32(float [[NUM]], i32 264)
+; CHECK-NEXT: ret i1 [[RES]]
+;
+ %cast = bitcast float %num to i32
+ %masked = and i32 %cast, 2139095040
+ %test1 = icmp ne i32 %masked, 2139095040
+ %test2 = icmp ne i32 %masked, 0
+ %res = and i1 %test2, %test1
+ ret i1 %res
+}
+
+define i1 @fpclass_test_normal_mismatch_pred(float %num) {
+; CHECK-LABEL: define i1 @fpclass_test_normal_mismatch_pred(
+; CHECK-SAME: float [[NUM:%.*]]) {
+; CHECK-NEXT: [[CAST:%.*]] = bitcast float [[NUM]] to i32
+; CHECK-NEXT: [[MASKED:%.*]] = and i32 [[CAST]], 2139095040
+; CHECK-NEXT: [[TEST2:%.*]] = icmp eq i32 [[MASKED]], 0
+; CHECK-NEXT: ret i1 [[TEST2]]
+;
+ %cast = bitcast float %num to i32
+ %masked = and i32 %cast, 2139095040
+ %test1 = icmp ne i32 %masked, 2139095040
+ %test2 = icmp eq i32 %masked, 0
+ %res = and i1 %test1, %test2
+ ret i1 %res
+}
+
+define i1 @fpclass_test_normal_no_implicit_fp(float %num) #0 {
+; CHECK-LABEL: define i1 @fpclass_test_normal_no_implicit_fp(
+; CHECK-SAME: float [[NUM:%.*]]) #[[ATTR1]] {
+; CHECK-NEXT: [[CAST:%.*]] = bitcast float [[NUM]] to i32
+; CHECK-NEXT: [[MASKED:%.*]] = and i32 [[CAST]], 2139095040
+; CHECK-NEXT: [[TEST1:%.*]] = icmp ne i32 [[MASKED]], 2139095040
+; CHECK-NEXT: [[TEST2:%.*]] = icmp ne i32 [[MASKED]], 0
+; CHECK-NEXT: [[RES:%.*]] = and i1 [[TEST1]], [[TEST2]]
+; CHECK-NEXT: ret i1 [[RES]]
+;
+ %cast = bitcast float %num to i32
+ %masked = and i32 %cast, 2139095040
+ %test1 = icmp ne i32 %masked, 2139095040
+ %test2 = icmp ne i32 %masked, 0
+ %res = and i1 %test1, %test2
+ ret i1 %res
+}
+
+define i1 @fpclass_test_normal_invalid_constant1(float %num) {
+; CHECK-LABEL: define i1 @fpclass_test_normal_invalid_constant1(
+; CHECK-SAME: float [[NUM:%.*]]) {
+; CHECK-NEXT: [[CAST:%.*]] = bitcast float [[NUM]] to i32
+; CHECK-NEXT: [[MASKED:%.*]] = and i32 [[CAST]], 2139095039
+; CHECK-NEXT: [[TEST1:%.*]] = icmp ne i32 [[MASKED]], 2139095039
+; CHECK-NEXT: [[TEST2:%.*]] = icmp ne i32 [[MASKED]], 0
+; CHECK-NEXT: [[RES:%.*]] = and i1 [[TEST1]], [[TEST2]]
+; CHECK-NEXT: ret i1 [[RES]]
+;
+ %cast = bitcast float %num to i32
+ %masked = and i32 %cast, 2139095039
+ %test1 = icmp ne i32 %masked, 2139095039
+ %test2 = icmp ne i32 %masked, 0
+ %res = and i1 %test1, %test2
+ ret i1 %res
+}
+
+define i1 @fpclass_test_normal_invalid_constant2(float %num) {
+; CHECK-LABEL: define i1 @fpclass_test_normal_invalid_constant2(
+; CHECK-SAME: float [[NUM:%.*]]) {
+; CHECK-NEXT: [[CAST:%.*]] = bitcast float [[NUM]] to i32
+; CHECK-NEXT: [[MASKED:%.*]] = and i32 [[CAST]], 2139095040
+; CHECK-NEXT: [[TEST1:%.*]] = icmp ne i32 [[MASKED]], 2130706432
+; CHECK-NEXT: [[TEST2:%.*]] = icmp ne i32 [[MASKED]], 0
+; CHECK-NEXT: [[RES:%.*]] = and i1 [[TEST1]], [[TEST2]]
+; CHECK-NEXT: ret i1 [[RES]]
+;
+ %cast = bitcast float %num to i32
+ %masked = and i32 %cast, 2139095040
+ %test1 = icmp ne i32 %masked, 2130706432
+ %test2 = icmp ne i32 %masked, 0
+ %res = and i1 %test1, %test2
+ ret i1 %res
+}
+
+define i1 @fpclass_test_normal_invalid_constant3(float %num) {
+; CHECK-LABEL: define i1 @fpclass_test_normal_invalid_constant3(
+; CHECK-SAME: float [[NUM:%.*]]) {
+; CHECK-NEXT: [[CAST:%.*]] = bitcast float [[NUM]] to i32
+; CHECK-NEXT: [[TMP1:%.*]] = and i32 [[CAST]], 2130706432
+; CHECK-NEXT: [[RES:%.*]] = icmp ne i32 [[TMP1]], 2130706432
+; CHECK-NEXT: ret i1 [[RES]]
+;
+ %cast = bitcast float %num to i32
+ %masked = and i32 %cast, 2139095040
+ %test1 = icmp ne i32 %masked, 2139095040
+ %test2 = icmp ne i32 %masked, 2130706432
+ %res = and i1 %test1, %test2
+ ret i1 %res
+}
+
declare void @usei32(i32)
attributes #0 = { noimplicitfloat }
|
arsenm
reviewed
Feb 3, 2025
This was referenced Feb 9, 2025
arsenm
approved these changes
May 5, 2025
408aa81
to
a31fc9f
Compare
GeorgeARM
pushed a commit
to GeorgeARM/llvm-project
that referenced
this pull request
May 7, 2025
This patch improves the codegen of Rust's `is_normal` implementation: https://godbolt.org/z/1MPzcrrYG Alive2: https://alive2.llvm.org/ce/z/hF9RWQ
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
floating-point
Floating-point math
llvm:instcombine
Covers the InstCombine, InstSimplify and AggressiveInstCombine passes
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.
This patch improves the codegen of Rust's
is_normal
implementation: https://godbolt.org/z/1MPzcrrYGAlive2: https://alive2.llvm.org/ce/z/hF9RWQ