Skip to content

[InstCombine] Resolve TODO: nnan nsz X / -0.0 -> copysign(inf, X) #79766

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 2 commits into from
Feb 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
Original file line number Diff line number Diff line change
@@ -1598,9 +1598,11 @@ Instruction *InstCombinerImpl::foldFDivConstantDivisor(BinaryOperator &I) {
return BinaryOperator::CreateFDivFMF(X, NegC, &I);

// nnan X / +0.0 -> copysign(inf, X)
if (I.hasNoNaNs() && match(I.getOperand(1), m_Zero())) {
// nnan nsz X / -0.0 -> copysign(inf, X)
if (I.hasNoNaNs() &&
(match(I.getOperand(1), m_PosZeroFP()) ||
(I.hasNoSignedZeros() && match(I.getOperand(1), m_AnyZeroFP())))) {
IRBuilder<> B(&I);
// TODO: nnan nsz X / -0.0 -> copysign(inf, X)
CallInst *CopySign = B.CreateIntrinsic(
Intrinsic::copysign, {C->getType()},
{ConstantFP::getInfinity(I.getType()), I.getOperand(0)}, &I);
72 changes: 72 additions & 0 deletions llvm/test/Transforms/InstCombine/fdiv.ll
Original file line number Diff line number Diff line change
@@ -992,3 +992,75 @@ define float @fdiv_nnan_neg_zero_f32(float %x) {
%fdiv = fdiv nnan float %x, -0.0
ret float %fdiv
}

define double @test_positive_zero_nsz(double %X) {
; CHECK-LABEL: @test_positive_zero_nsz(
; CHECK-NEXT: [[TMP1:%.*]] = call nnan nsz double @llvm.copysign.f64(double 0x7FF0000000000000, double [[X:%.*]])
; CHECK-NEXT: ret double [[TMP1]]
;
%1 = fdiv nnan nsz double %X, 0.0
ret double %1
}

define double @test_negative_zero_nsz(double %X) {
; CHECK-LABEL: @test_negative_zero_nsz(
; CHECK-NEXT: [[TMP1:%.*]] = call nnan nsz double @llvm.copysign.f64(double 0x7FF0000000000000, double [[X:%.*]])
; CHECK-NEXT: ret double [[TMP1]]
;
%1 = fdiv nnan nsz double %X, -0.0
ret double %1
}

define double @test_positive_zero(double %X) {
; CHECK-LABEL: @test_positive_zero(
; CHECK-NEXT: [[TMP1:%.*]] = call nnan double @llvm.copysign.f64(double 0x7FF0000000000000, double [[X:%.*]])
; CHECK-NEXT: ret double [[TMP1]]
;
%1 = fdiv nnan double %X, 0.0
ret double %1
}

define double @test_negative_zero(double %X) {
; CHECK-LABEL: @test_negative_zero(
; CHECK-NEXT: [[TMP1:%.*]] = fdiv nnan double [[X:%.*]], -0.000000e+00
; CHECK-NEXT: ret double [[TMP1]]
;
%1 = fdiv nnan double %X, -0.0
ret double %1
}

define <2 x double> @test_positive_zero_vector_nsz(<2 x double> %X) {
; CHECK-LABEL: @test_positive_zero_vector_nsz(
; CHECK-NEXT: [[TMP1:%.*]] = call nnan nsz <2 x double> @llvm.copysign.v2f64(<2 x double> <double 0x7FF0000000000000, double 0x7FF0000000000000>, <2 x double> [[X:%.*]])
; CHECK-NEXT: ret <2 x double> [[TMP1]]
;
%1 = fdiv nnan nsz <2 x double> %X, <double 0.0, double 0.0>
ret <2 x double> %1
}

define <2 x double> @test_negative_zero_vector_nsz(<2 x double> %X) {
; CHECK-LABEL: @test_negative_zero_vector_nsz(
; CHECK-NEXT: [[TMP1:%.*]] = call nnan nsz <2 x double> @llvm.copysign.v2f64(<2 x double> <double 0x7FF0000000000000, double 0x7FF0000000000000>, <2 x double> [[X:%.*]])
; CHECK-NEXT: ret <2 x double> [[TMP1]]
;
%1 = fdiv nnan nsz <2 x double> %X, <double -0.0, double 0.0>
ret <2 x double> %1
}

define <2 x double> @test_positive_zero_vector(<2 x double> %X) {
; CHECK-LABEL: @test_positive_zero_vector(
; CHECK-NEXT: [[TMP1:%.*]] = call nnan <2 x double> @llvm.copysign.v2f64(<2 x double> <double 0x7FF0000000000000, double 0x7FF0000000000000>, <2 x double> [[X:%.*]])
; CHECK-NEXT: ret <2 x double> [[TMP1]]
;
%1 = fdiv nnan <2 x double> %X, <double 0.0, double 0.0>
ret <2 x double> %1
}

define <2 x double> @test_negative_zero_vector(<2 x double> %X) {
; CHECK-LABEL: @test_negative_zero_vector(
; CHECK-NEXT: [[TMP1:%.*]] = fdiv nnan <2 x double> [[X:%.*]], <double -0.000000e+00, double 0.000000e+00>
; CHECK-NEXT: ret <2 x double> [[TMP1]]
;
%1 = fdiv nnan <2 x double> %X, <double -0.0, double 0.0>
ret <2 x double> %1
}