[InstCombine] Remove nnan requirement for transformation to fabs from select

In this patch, the "nnan" requirement is removed for the canonicalization of select with fcmp to fabs.
(i) FSub logic: Remove check for nnan flag presence in fsub. Example: https://alive2.llvm.org/ce/z/751svg (fsub).
(ii) FNeg logic: Remove check for the presence of nnan and nsz flag in fneg. Example: https://alive2.llvm.org/ce/z/a_fsdp (fneg).

Reviewed By: spatel

Differential Revision: https://reviews.llvm.org/D106872
This commit is contained in:
Krishna 2021-08-07 21:26:49 +05:30
parent 262289c103
commit a9a176ca3b
2 changed files with 13 additions and 25 deletions

View File

@ -2863,14 +2863,12 @@ Instruction *InstCombinerImpl::visitSelectInst(SelectInst &SI) {
}
// Canonicalize select with fcmp to fabs(). -0.0 makes this tricky. We need
// fast-math-flags (nsz) or fsub with +0.0 (not fneg) for this to work. We
// also require nnan because we do not want to unintentionally change the
// sign of a NaN value.
// fast-math-flags (nsz) or fsub with +0.0 (not fneg) for this to work.
// (X <= +/-0.0) ? (0.0 - X) : X --> fabs(X)
Instruction *FSub;
if (match(CondVal, m_FCmp(Pred, m_Specific(FalseVal), m_AnyZeroFP())) &&
match(TrueVal, m_FSub(m_PosZeroFP(), m_Specific(FalseVal))) &&
match(TrueVal, m_Instruction(FSub)) && FSub->hasNoNaNs() &&
match(TrueVal, m_Instruction(FSub)) &&
(Pred == FCmpInst::FCMP_OLE || Pred == FCmpInst::FCMP_ULE)) {
Value *Fabs = Builder.CreateUnaryIntrinsic(Intrinsic::fabs, FalseVal, &SI);
return replaceInstUsesWith(SI, Fabs);
@ -2878,7 +2876,7 @@ Instruction *InstCombinerImpl::visitSelectInst(SelectInst &SI) {
// (X > +/-0.0) ? X : (0.0 - X) --> fabs(X)
if (match(CondVal, m_FCmp(Pred, m_Specific(TrueVal), m_AnyZeroFP())) &&
match(FalseVal, m_FSub(m_PosZeroFP(), m_Specific(TrueVal))) &&
match(FalseVal, m_Instruction(FSub)) && FSub->hasNoNaNs() &&
match(FalseVal, m_Instruction(FSub)) &&
(Pred == FCmpInst::FCMP_OGT || Pred == FCmpInst::FCMP_UGT)) {
Value *Fabs = Builder.CreateUnaryIntrinsic(Intrinsic::fabs, TrueVal, &SI);
return replaceInstUsesWith(SI, Fabs);
@ -2889,8 +2887,7 @@ Instruction *InstCombinerImpl::visitSelectInst(SelectInst &SI) {
Instruction *FNeg;
if (match(CondVal, m_FCmp(Pred, m_Specific(FalseVal), m_AnyZeroFP())) &&
match(TrueVal, m_FNeg(m_Specific(FalseVal))) &&
match(TrueVal, m_Instruction(FNeg)) && FNeg->hasNoNaNs() &&
FNeg->hasNoSignedZeros() && SI.hasNoSignedZeros() &&
match(TrueVal, m_Instruction(FNeg)) && SI.hasNoSignedZeros() &&
(Pred == FCmpInst::FCMP_OLT || Pred == FCmpInst::FCMP_OLE ||
Pred == FCmpInst::FCMP_ULT || Pred == FCmpInst::FCMP_ULE)) {
Value *Fabs = Builder.CreateUnaryIntrinsic(Intrinsic::fabs, FalseVal, &SI);
@ -2901,8 +2898,7 @@ Instruction *InstCombinerImpl::visitSelectInst(SelectInst &SI) {
// (X >= +/-0.0) ? X : -X --> fabs(X)
if (match(CondVal, m_FCmp(Pred, m_Specific(TrueVal), m_AnyZeroFP())) &&
match(FalseVal, m_FNeg(m_Specific(TrueVal))) &&
match(FalseVal, m_Instruction(FNeg)) && FNeg->hasNoNaNs() &&
FNeg->hasNoSignedZeros() && SI.hasNoSignedZeros() &&
match(FalseVal, m_Instruction(FNeg)) && SI.hasNoSignedZeros() &&
(Pred == FCmpInst::FCMP_OGT || Pred == FCmpInst::FCMP_OGE ||
Pred == FCmpInst::FCMP_UGT || Pred == FCmpInst::FCMP_UGE)) {
Value *Fabs = Builder.CreateUnaryIntrinsic(Intrinsic::fabs, TrueVal, &SI);

View File

@ -254,10 +254,8 @@ define double @multi_use_fabs_fpext(float %x) {
define double @select_fcmp_ole_zero(double %x) {
; CHECK-LABEL: @select_fcmp_ole_zero(
; CHECK-NEXT: [[LEZERO:%.*]] = fcmp ole double [[X:%.*]], 0.000000e+00
; CHECK-NEXT: [[NEGX:%.*]] = fsub double 0.000000e+00, [[X]]
; CHECK-NEXT: [[FABS:%.*]] = select i1 [[LEZERO]], double [[NEGX]], double [[X]]
; CHECK-NEXT: ret double [[FABS]]
; CHECK-NEXT: [[TMP1:%.*]] = call double @llvm.fabs.f64(double [[X:%.*]])
; CHECK-NEXT: ret double [[TMP1]]
;
%lezero = fcmp ole double %x, 0.0
%negx = fsub double 0.0, %x
@ -345,10 +343,8 @@ define <2 x float> @select_nnan_fcmp_nnan_ole_negzero(<2 x float> %x) {
define fp128 @select_fcmp_ogt_zero(fp128 %x) {
; CHECK-LABEL: @select_fcmp_ogt_zero(
; CHECK-NEXT: [[GTZERO:%.*]] = fcmp ogt fp128 [[X:%.*]], 0xL00000000000000000000000000000000
; CHECK-NEXT: [[NEGX:%.*]] = fsub fp128 0xL00000000000000000000000000000000, [[X]]
; CHECK-NEXT: [[FABS:%.*]] = select i1 [[GTZERO]], fp128 [[X]], fp128 [[NEGX]]
; CHECK-NEXT: ret fp128 [[FABS]]
; CHECK-NEXT: [[TMP1:%.*]] = call fp128 @llvm.fabs.f128(fp128 [[X:%.*]])
; CHECK-NEXT: ret fp128 [[TMP1]]
;
%gtzero = fcmp ogt fp128 %x, zeroinitializer
%negx = fsub fp128 zeroinitializer, %x
@ -434,10 +430,8 @@ define half @select_fcmp_nnan_oge_negzero(half %x) {
define double @select_fcmp_olt_zero_unary_fneg(double %x) {
; CHECK-LABEL: @select_fcmp_olt_zero_unary_fneg(
; CHECK-NEXT: [[LTZERO:%.*]] = fcmp olt double [[X:%.*]], 0.000000e+00
; CHECK-NEXT: [[NEGX:%.*]] = fneg double [[X]]
; CHECK-NEXT: [[FABS:%.*]] = select nsz i1 [[LTZERO]], double [[NEGX]], double [[X]]
; CHECK-NEXT: ret double [[FABS]]
; CHECK-NEXT: [[TMP1:%.*]] = call nsz double @llvm.fabs.f64(double [[X:%.*]])
; CHECK-NEXT: ret double [[TMP1]]
;
%ltzero = fcmp olt double %x, 0.0
%negx = fneg double %x
@ -723,10 +717,8 @@ define float @select_fcmp_nnan_nsz_ule_negzero_unary_fneg(float %x) {
define <2 x float> @select_fcmp_ogt_zero_unary_fneg(<2 x float> %x) {
; CHECK-LABEL: @select_fcmp_ogt_zero_unary_fneg(
; CHECK-NEXT: [[GTZERO:%.*]] = fcmp ogt <2 x float> [[X:%.*]], zeroinitializer
; CHECK-NEXT: [[NEGX:%.*]] = fneg <2 x float> [[X]]
; CHECK-NEXT: [[FABS:%.*]] = select nsz <2 x i1> [[GTZERO]], <2 x float> [[X]], <2 x float> [[NEGX]]
; CHECK-NEXT: ret <2 x float> [[FABS]]
; CHECK-NEXT: [[TMP1:%.*]] = call nsz <2 x float> @llvm.fabs.v2f32(<2 x float> [[X:%.*]])
; CHECK-NEXT: ret <2 x float> [[TMP1]]
;
%gtzero = fcmp ogt <2 x float> %x, zeroinitializer
%negx = fneg <2 x float> %x