Skip to content

[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 4 commits into from
May 6, 2025
Merged

Conversation

dtcxzyw
Copy link
Member

@dtcxzyw dtcxzyw commented Feb 3, 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

@dtcxzyw dtcxzyw requested review from arsenm and goldsteinn February 3, 2025 04:21
@dtcxzyw dtcxzyw requested a review from nikic as a code owner February 3, 2025 04:21
@llvmbot llvmbot added llvm:instcombine Covers the InstCombine, InstSimplify and AggressiveInstCombine passes llvm:transforms labels Feb 3, 2025
@llvmbot
Copy link
Member

llvmbot commented Feb 3, 2025

@llvm/pr-subscribers-llvm-transforms

Author: Yingwei Zheng (dtcxzyw)

Changes

This patch improves the codegen of Rust's is_normal implementation: https://godbolt.org/z/1MPzcrrYG
Alive2: https://alive2.llvm.org/ce/z/hF9RWQ


Full diff: https://github.com/llvm/llvm-project/pull/125454.diff

2 Files Affected:

  • (modified) llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp (+16)
  • (modified) llvm/test/Transforms/InstCombine/fpclass-check-idioms.ll (+156)
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 }

@dtcxzyw dtcxzyw force-pushed the perf/isnormal-idiom branch from 408aa81 to a31fc9f Compare May 6, 2025 14:19
@dtcxzyw dtcxzyw merged commit a10f6c1 into llvm:main May 6, 2025
9 of 10 checks passed
@dtcxzyw dtcxzyw deleted the perf/isnormal-idiom branch May 6, 2025 15:55
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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants