[llvm] [InstCombine] Fold isnan idioms (PR #101510)
via llvm-commits
llvm-commits at lists.llvm.org
Thu Aug 1 09:16:51 PDT 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-transforms
Author: Yingwei Zheng (dtcxzyw)
<details>
<summary>Changes</summary>
Folds `(icmp ne ((bitcast X) & FractionBits), 0) & (icmp eq ((bitcast X) & ExpBits), ExpBits) -> fcmp uno X, 0.0` and
`(icmp eq ((bitcast X) & FractionBits), 0) | (icmp ne ((bitcast X) & ExpBits), ExpBits) -> fcmp ord X, 0.0`
Alive2: https://alive2.llvm.org/ce/z/mrLn_x
---
Full diff: https://github.com/llvm/llvm-project/pull/101510.diff
2 Files Affected:
- (modified) llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp (+23-3)
- (modified) llvm/test/Transforms/InstCombine/fpclass-check-idioms.ll (+217-12)
``````````diff
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
index 4ca12d5b92f18..e0021893f7a46 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
@@ -339,8 +339,8 @@ static Value *foldLogOpOfMaskedICmps_NotAllZeros_BMask_Mixed(
//
// We currently handle the case of B, C, D, E are constant.
//
- const APInt *BCst, *CCst, *DCst, *OrigECst;
- if (!match(B, m_APInt(BCst)) || !match(C, m_APInt(CCst)) ||
+ const APInt *BCst, *DCst, *OrigECst;
+ if (!match(B, m_APInt(BCst)) || !match(C, m_Zero()) ||
!match(D, m_APInt(DCst)) || !match(E, m_APInt(OrigECst)))
return nullptr;
@@ -363,8 +363,28 @@ static Value *foldLogOpOfMaskedICmps_NotAllZeros_BMask_Mixed(
// deduce anything from it.
// For example,
// (icmp ne (A & 12), 0) & (icmp eq (A & 3), 1) -> no folding.
- if ((*BCst & *DCst) == 0)
+ if ((*BCst & *DCst) == 0) {
+ // Try to fold isNaN idiom:
+ // (icmp ne (A & FractionBits), 0) & (icmp eq (A & ExpBits), ExpBits) ->
+ // isNaN(A)
+ Value *Src;
+ if (match(A, m_ElementWiseBitCast(m_Value(Src))) &&
+ Src->getType()->getScalarType()->isIEEELikeFPTy() &&
+ !Builder.GetInsertBlock()->getParent()->hasFnAttribute(
+ Attribute::NoImplicitFloat)) {
+ APInt ExpBits =
+ APFloat::getInf(Src->getType()->getScalarType()->getFltSemantics())
+ .bitcastToAPInt();
+ APInt FractionBits = ~ExpBits;
+ FractionBits.clearSignBit();
+
+ if (*BCst == FractionBits && *DCst == ExpBits && ECst == ExpBits)
+ return Builder.CreateFCmp(IsAnd ? FCmpInst::FCMP_UNO
+ : FCmpInst::FCMP_ORD,
+ Src, ConstantFP::getZero(Src->getType()));
+ }
return nullptr;
+ }
// If the following two conditions are met:
//
diff --git a/llvm/test/Transforms/InstCombine/fpclass-check-idioms.ll b/llvm/test/Transforms/InstCombine/fpclass-check-idioms.ll
index 42c6506e34cf4..2ffa6e4da922f 100644
--- a/llvm/test/Transforms/InstCombine/fpclass-check-idioms.ll
+++ b/llvm/test/Transforms/InstCombine/fpclass-check-idioms.ll
@@ -217,12 +217,7 @@ define i1 @f32_fczero_strictfp(float %a) strictfp {
define i1 @f32_fcnan(float %a) {
; CHECK-LABEL: define i1 @f32_fcnan(
; CHECK-SAME: float [[A:%.*]]) {
-; CHECK-NEXT: [[I32:%.*]] = bitcast float [[A]] to i32
-; CHECK-NEXT: [[AND1:%.*]] = and i32 [[I32]], 2139095040
-; CHECK-NEXT: [[CMP1:%.*]] = icmp eq i32 [[AND1]], 2139095040
-; CHECK-NEXT: [[AND2:%.*]] = and i32 [[I32]], 8388607
-; CHECK-NEXT: [[CMP2:%.*]] = icmp ne i32 [[AND2]], 0
-; CHECK-NEXT: [[RES:%.*]] = and i1 [[CMP1]], [[CMP2]]
+; CHECK-NEXT: [[RES:%.*]] = fcmp uno float [[A]], 0.000000e+00
; CHECK-NEXT: ret i1 [[RES]]
;
%i32 = bitcast float %a to i32
@@ -237,12 +232,7 @@ define i1 @f32_fcnan(float %a) {
define i1 @f32_fcnan_strictfp(float %a) strictfp {
; CHECK-LABEL: define i1 @f32_fcnan_strictfp(
; CHECK-SAME: float [[A:%.*]]) #[[ATTR0]] {
-; CHECK-NEXT: [[I32:%.*]] = bitcast float [[A]] to i32
-; CHECK-NEXT: [[AND1:%.*]] = and i32 [[I32]], 2139095040
-; CHECK-NEXT: [[CMP1:%.*]] = icmp eq i32 [[AND1]], 2139095040
-; CHECK-NEXT: [[AND2:%.*]] = and i32 [[I32]], 8388607
-; CHECK-NEXT: [[CMP2:%.*]] = icmp ne i32 [[AND2]], 0
-; CHECK-NEXT: [[RES:%.*]] = and i1 [[CMP1]], [[CMP2]]
+; CHECK-NEXT: [[RES:%.*]] = fcmp uno float [[A]], 0.000000e+00
; CHECK-NEXT: ret i1 [[RES]]
;
%i32 = bitcast float %a to i32
@@ -647,6 +637,221 @@ define i1 @f32_fcposinf_multiuse_strictfp(float %a) strictfp {
ret i1 %cmp
}
+define i1 @isnan_idiom(double %x) {
+; CHECK-LABEL: define i1 @isnan_idiom(
+; CHECK-SAME: double [[X:%.*]]) {
+; CHECK-NEXT: [[RET:%.*]] = fcmp uno double [[X]], 0.000000e+00
+; CHECK-NEXT: ret i1 [[RET]]
+;
+ %bits = bitcast double %x to i64
+ %mask1 = and i64 %bits, 9218868437227405312
+ %cond1 = icmp eq i64 %mask1, 9218868437227405312
+ %mask2 = and i64 %bits, 4503599627370495
+ %cond2 = icmp ne i64 %mask2, 0
+ %ret = and i1 %cond1, %cond2
+ ret i1 %ret
+}
+
+define <2 x i1> @isnan_idiom_vec(<2 x double> %x) {
+; CHECK-LABEL: define <2 x i1> @isnan_idiom_vec(
+; CHECK-SAME: <2 x double> [[X:%.*]]) {
+; CHECK-NEXT: [[RET:%.*]] = fcmp uno <2 x double> [[X]], zeroinitializer
+; CHECK-NEXT: ret <2 x i1> [[RET]]
+;
+ %bits = bitcast <2 x double> %x to <2 x i64>
+ %mask1 = and <2 x i64> %bits, splat(i64 9218868437227405312)
+ %cond1 = icmp eq <2 x i64> %mask1, splat(i64 9218868437227405312)
+ %mask2 = and <2 x i64> %bits, splat(i64 4503599627370495)
+ %cond2 = icmp ne <2 x i64> %mask2, zeroinitializer
+ %ret = and <2 x i1> %cond1, %cond2
+ ret <2 x i1> %ret
+}
+
+define i1 @isnan_idiom_commuted(double %x) {
+; CHECK-LABEL: define i1 @isnan_idiom_commuted(
+; CHECK-SAME: double [[X:%.*]]) {
+; CHECK-NEXT: [[RET:%.*]] = fcmp uno double [[X]], 0.000000e+00
+; CHECK-NEXT: ret i1 [[RET]]
+;
+ %bits = bitcast double %x to i64
+ %mask1 = and i64 %bits, 9218868437227405312
+ %cond1 = icmp eq i64 %mask1, 9218868437227405312
+ %mask2 = and i64 %bits, 4503599627370495
+ %cond2 = icmp ne i64 %mask2, 0
+ %ret = and i1 %cond2, %cond1
+ ret i1 %ret
+}
+
+define i1 @isnotnan_idiom(double %x) {
+; CHECK-LABEL: define i1 @isnotnan_idiom(
+; CHECK-SAME: double [[X:%.*]]) {
+; CHECK-NEXT: [[RET:%.*]] = fcmp ord double [[X]], 0.000000e+00
+; CHECK-NEXT: ret i1 [[RET]]
+;
+ %bits = bitcast double %x to i64
+ %mask1 = and i64 %bits, 9218868437227405312
+ %cond1 = icmp ne i64 %mask1, 9218868437227405312
+ %mask2 = and i64 %bits, 4503599627370495
+ %cond2 = icmp eq i64 %mask2, 0
+ %ret = or i1 %cond1, %cond2
+ ret i1 %ret
+}
+
+; negative tests
+
+define i1 @isnan_idiom_no_implicit(double %x) noimplicitfloat {
+; CHECK-LABEL: define i1 @isnan_idiom_no_implicit(
+; CHECK-SAME: double [[X:%.*]]) #[[ATTR1]] {
+; CHECK-NEXT: [[BITS:%.*]] = bitcast double [[X]] to i64
+; CHECK-NEXT: [[MASK1:%.*]] = and i64 [[BITS]], 9218868437227405312
+; CHECK-NEXT: [[COND1:%.*]] = icmp eq i64 [[MASK1]], 9218868437227405312
+; CHECK-NEXT: [[MASK2:%.*]] = and i64 [[BITS]], 4503599627370495
+; CHECK-NEXT: [[COND2:%.*]] = icmp ne i64 [[MASK2]], 0
+; CHECK-NEXT: [[RET:%.*]] = and i1 [[COND1]], [[COND2]]
+; CHECK-NEXT: ret i1 [[RET]]
+;
+ %bits = bitcast double %x to i64
+ %mask1 = and i64 %bits, 9218868437227405312
+ %cond1 = icmp eq i64 %mask1, 9218868437227405312
+ %mask2 = and i64 %bits, 4503599627370495
+ %cond2 = icmp ne i64 %mask2, 0
+ %ret = and i1 %cond1, %cond2
+ ret i1 %ret
+}
+
+define i1 @isnan_idiom_wrong_pred1(double %x) {
+; CHECK-LABEL: define i1 @isnan_idiom_wrong_pred1(
+; CHECK-SAME: double [[X:%.*]]) {
+; CHECK-NEXT: [[BITS:%.*]] = bitcast double [[X]] to i64
+; CHECK-NEXT: [[MASK1:%.*]] = and i64 [[BITS]], 9218868437227405312
+; CHECK-NEXT: [[COND1:%.*]] = icmp ne i64 [[MASK1]], 9218868437227405312
+; CHECK-NEXT: [[MASK2:%.*]] = and i64 [[BITS]], 4503599627370495
+; CHECK-NEXT: [[COND2:%.*]] = icmp ne i64 [[MASK2]], 0
+; CHECK-NEXT: [[RET:%.*]] = and i1 [[COND1]], [[COND2]]
+; CHECK-NEXT: ret i1 [[RET]]
+;
+ %bits = bitcast double %x to i64
+ %mask1 = and i64 %bits, 9218868437227405312
+ %cond1 = icmp ne i64 %mask1, 9218868437227405312
+ %mask2 = and i64 %bits, 4503599627370495
+ %cond2 = icmp ne i64 %mask2, 0
+ %ret = and i1 %cond1, %cond2
+ ret i1 %ret
+}
+
+define i1 @isnan_idiom_wrong_pred2(double %x) {
+; CHECK-LABEL: define i1 @isnan_idiom_wrong_pred2(
+; CHECK-SAME: double [[X:%.*]]) {
+; CHECK-NEXT: [[TMP1:%.*]] = call double @llvm.fabs.f64(double [[X]])
+; CHECK-NEXT: [[RET:%.*]] = fcmp oeq double [[TMP1]], 0x7FF0000000000000
+; CHECK-NEXT: ret i1 [[RET]]
+;
+ %bits = bitcast double %x to i64
+ %mask1 = and i64 %bits, 9218868437227405312
+ %cond1 = icmp eq i64 %mask1, 9218868437227405312
+ %mask2 = and i64 %bits, 4503599627370495
+ %cond2 = icmp eq i64 %mask2, 0
+ %ret = and i1 %cond1, %cond2
+ ret i1 %ret
+}
+
+define i1 @isnan_idiom_wrong_pred3(double %x) {
+; CHECK-LABEL: define i1 @isnan_idiom_wrong_pred3(
+; CHECK-SAME: double [[X:%.*]]) {
+; CHECK-NEXT: [[BITS:%.*]] = bitcast double [[X]] to i64
+; CHECK-NEXT: [[MASK1:%.*]] = and i64 [[BITS]], 9218868437227405312
+; CHECK-NEXT: [[COND1:%.*]] = icmp eq i64 [[MASK1]], 9218868437227405312
+; CHECK-NEXT: [[MASK2:%.*]] = and i64 [[BITS]], 4503599627370495
+; CHECK-NEXT: [[COND2:%.*]] = icmp ne i64 [[MASK2]], 0
+; CHECK-NEXT: [[RET:%.*]] = or i1 [[COND1]], [[COND2]]
+; CHECK-NEXT: ret i1 [[RET]]
+;
+ %bits = bitcast double %x to i64
+ %mask1 = and i64 %bits, 9218868437227405312
+ %cond1 = icmp eq i64 %mask1, 9218868437227405312
+ %mask2 = and i64 %bits, 4503599627370495
+ %cond2 = icmp ne i64 %mask2, 0
+ %ret = or i1 %cond1, %cond2
+ ret i1 %ret
+}
+
+define i1 @isnan_idiom_wrong_mask1(double %x) {
+; CHECK-LABEL: define i1 @isnan_idiom_wrong_mask1(
+; CHECK-SAME: double [[X:%.*]]) {
+; CHECK-NEXT: [[BITS:%.*]] = bitcast double [[X]] to i64
+; CHECK-NEXT: [[MASK1:%.*]] = and i64 [[BITS]], 9218868437227405311
+; CHECK-NEXT: [[COND1:%.*]] = icmp eq i64 [[MASK1]], 9218868437227405311
+; CHECK-NEXT: ret i1 [[COND1]]
+;
+ %bits = bitcast double %x to i64
+ %mask1 = and i64 %bits, 9218868437227405311
+ %cond1 = icmp eq i64 %mask1, 9218868437227405311
+ %mask2 = and i64 %bits, 4503599627370495
+ %cond2 = icmp ne i64 %mask2, 0
+ %ret = and i1 %cond1, %cond2
+ ret i1 %ret
+}
+
+define i1 @isnan_idiom_wrong_mask2(double %x) {
+; CHECK-LABEL: define i1 @isnan_idiom_wrong_mask2(
+; CHECK-SAME: double [[X:%.*]]) {
+; CHECK-NEXT: [[BITS:%.*]] = bitcast double [[X]] to i64
+; CHECK-NEXT: [[MASK1:%.*]] = and i64 [[BITS]], 9218868437227405312
+; CHECK-NEXT: [[COND1:%.*]] = icmp eq i64 [[MASK1]], 9218868437227405312
+; CHECK-NEXT: [[MASK2:%.*]] = and i64 [[BITS]], 4503599627370494
+; CHECK-NEXT: [[COND2:%.*]] = icmp ne i64 [[MASK2]], 0
+; CHECK-NEXT: [[RET:%.*]] = and i1 [[COND1]], [[COND2]]
+; CHECK-NEXT: ret i1 [[RET]]
+;
+ %bits = bitcast double %x to i64
+ %mask1 = and i64 %bits, 9218868437227405312
+ %cond1 = icmp eq i64 %mask1, 9218868437227405312
+ %mask2 = and i64 %bits, 4503599627370494
+ %cond2 = icmp ne i64 %mask2, 0
+ %ret = and i1 %cond1, %cond2
+ ret i1 %ret
+}
+
+define i1 @isnan_idiom_wrong_mask3(double %x) {
+; CHECK-LABEL: define i1 @isnan_idiom_wrong_mask3(
+; CHECK-SAME: double [[X:%.*]]) {
+; CHECK-NEXT: [[BITS:%.*]] = bitcast double [[X]] to i64
+; CHECK-NEXT: [[MASK1:%.*]] = and i64 [[BITS]], 9218868437227405312
+; CHECK-NEXT: [[COND1:%.*]] = icmp eq i64 [[MASK1]], 9218868437227405312
+; CHECK-NEXT: [[MASK2:%.*]] = and i64 [[BITS]], 4503599627370495
+; CHECK-NEXT: [[COND2:%.*]] = icmp ne i64 [[MASK2]], 4503599627370495
+; CHECK-NEXT: [[RET:%.*]] = and i1 [[COND1]], [[COND2]]
+; CHECK-NEXT: ret i1 [[RET]]
+;
+ %bits = bitcast double %x to i64
+ %mask1 = and i64 %bits, 9218868437227405312
+ %cond1 = icmp eq i64 %mask1, 9218868437227405312
+ %mask2 = and i64 %bits, 4503599627370495
+ %cond2 = icmp ne i64 %mask2, 4503599627370495
+ %ret = and i1 %cond1, %cond2
+ ret i1 %ret
+}
+
+define i1 @isnan_idiom_invalid_bitcast(<2 x float> %x) {
+; CHECK-LABEL: define i1 @isnan_idiom_invalid_bitcast(
+; CHECK-SAME: <2 x float> [[X:%.*]]) {
+; CHECK-NEXT: [[BITS:%.*]] = bitcast <2 x float> [[X]] to i64
+; CHECK-NEXT: [[MASK1:%.*]] = and i64 [[BITS]], 9218868437227405312
+; CHECK-NEXT: [[COND1:%.*]] = icmp eq i64 [[MASK1]], 9218868437227405312
+; CHECK-NEXT: [[MASK2:%.*]] = and i64 [[BITS]], 4503599627370495
+; CHECK-NEXT: [[COND2:%.*]] = icmp ne i64 [[MASK2]], 0
+; CHECK-NEXT: [[RET:%.*]] = and i1 [[COND1]], [[COND2]]
+; CHECK-NEXT: ret i1 [[RET]]
+;
+ %bits = bitcast <2 x float> %x to i64
+ %mask1 = and i64 %bits, 9218868437227405312
+ %cond1 = icmp eq i64 %mask1, 9218868437227405312
+ %mask2 = and i64 %bits, 4503599627370495
+ %cond2 = icmp ne i64 %mask2, 0
+ %ret = and i1 %cond1, %cond2
+ ret i1 %ret
+}
+
declare void @usei32(i32)
attributes #0 = { noimplicitfloat }
``````````
</details>
https://github.com/llvm/llvm-project/pull/101510
More information about the llvm-commits
mailing list