Skip to content

Commit 57a31e1

Browse files
dtcxzywtstellar
authored andcommitted
[InstCombine] Do not fold logical is_finite test (llvm#136851)
This patch disables the fold for logical is_finite test (i.e., `and (fcmp ord x, 0), (fcmp u* x, inf) -> fcmp o* x, inf`). It is still possible to allow this fold for several logical cases (e.g., `stripSignOnlyFPOps(RHS0)` does not strip any operations). Since this patch has no real-world impact, I decided to disable this fold for all logical cases. Alive2: https://alive2.llvm.org/ce/z/aH4LC7 Closes llvm#136650. (cherry picked from commit 8abc917)
1 parent 1cf8c77 commit 57a31e1

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1475,7 +1475,9 @@ Value *InstCombinerImpl::foldLogicOfFCmps(FCmpInst *LHS, FCmpInst *RHS,
14751475
}
14761476
}
14771477

1478-
if (IsAnd && stripSignOnlyFPOps(LHS0) == stripSignOnlyFPOps(RHS0)) {
1478+
// This transform is not valid for a logical select.
1479+
if (!IsLogicalSelect && IsAnd &&
1480+
stripSignOnlyFPOps(LHS0) == stripSignOnlyFPOps(RHS0)) {
14791481
// and (fcmp ord x, 0), (fcmp u* x, inf) -> fcmp o* x, inf
14801482
// and (fcmp ord x, 0), (fcmp u* fabs(x), inf) -> fcmp o* x, inf
14811483
if (Value *Left = matchIsFiniteTest(Builder, LHS, RHS))

llvm/test/Transforms/InstCombine/and-fcmp.ll

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4990,6 +4990,34 @@ define i1 @clang_builtin_isnormal_inf_check_copysign(half %x, half %y) {
49904990
ret i1 %and
49914991
}
49924992

4993+
define i1 @clang_builtin_isnormal_inf_check_copysign_logical_select(half %x, half %y) {
4994+
; CHECK-LABEL: @clang_builtin_isnormal_inf_check_copysign_logical_select(
4995+
; CHECK-NEXT: [[COPYSIGN_X:%.*]] = call half @llvm.copysign.f16(half [[X:%.*]], half [[Y:%.*]])
4996+
; CHECK-NEXT: [[ORD:%.*]] = fcmp ord half [[X]], 0xH0000
4997+
; CHECK-NEXT: [[CMP:%.*]] = fcmp ueq half [[COPYSIGN_X]], 0xH7C00
4998+
; CHECK-NEXT: [[AND:%.*]] = select i1 [[ORD]], i1 [[CMP]], i1 false
4999+
; CHECK-NEXT: ret i1 [[AND]]
5000+
;
5001+
%copysign.x = call half @llvm.copysign.f16(half %x, half %y)
5002+
%ord = fcmp ord half %x, 0.0
5003+
%cmp = fcmp uge half %copysign.x, 0xH7C00
5004+
%and = select i1 %ord, i1 %cmp, i1 false
5005+
ret i1 %and
5006+
}
5007+
5008+
define i1 @clang_builtin_isnormal_inf_check_fabs_nnan_logical_select(half %x) {
5009+
; CHECK-LABEL: @clang_builtin_isnormal_inf_check_fabs_nnan_logical_select(
5010+
; CHECK-NEXT: [[COPYSIGN_X:%.*]] = call half @llvm.fabs.f16(half [[X:%.*]])
5011+
; CHECK-NEXT: [[AND:%.*]] = fcmp oeq half [[COPYSIGN_X]], 0xH7C00
5012+
; CHECK-NEXT: ret i1 [[AND]]
5013+
;
5014+
%copysign.x = call nnan half @llvm.fabs.f16(half %x)
5015+
%ord = fcmp ord half %x, 0.0
5016+
%cmp = fcmp uge half %copysign.x, 0xH7C00
5017+
%and = select i1 %ord, i1 %cmp, i1 false
5018+
ret i1 %and
5019+
}
5020+
49935021
define i1 @isnormal_logical_select_0(half %x) {
49945022
; CHECK-LABEL: @isnormal_logical_select_0(
49955023
; CHECK-NEXT: [[FABS_X:%.*]] = call half @llvm.fabs.f16(half [[X:%.*]])

0 commit comments

Comments
 (0)