forked from OSchip/llvm-project
[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:
parent
262289c103
commit
a9a176ca3b
|
@ -2863,14 +2863,12 @@ Instruction *InstCombinerImpl::visitSelectInst(SelectInst &SI) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Canonicalize select with fcmp to fabs(). -0.0 makes this tricky. We need
|
// 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
|
// fast-math-flags (nsz) or fsub with +0.0 (not fneg) for this to work.
|
||||||
// also require nnan because we do not want to unintentionally change the
|
|
||||||
// sign of a NaN value.
|
|
||||||
// (X <= +/-0.0) ? (0.0 - X) : X --> fabs(X)
|
// (X <= +/-0.0) ? (0.0 - X) : X --> fabs(X)
|
||||||
Instruction *FSub;
|
Instruction *FSub;
|
||||||
if (match(CondVal, m_FCmp(Pred, m_Specific(FalseVal), m_AnyZeroFP())) &&
|
if (match(CondVal, m_FCmp(Pred, m_Specific(FalseVal), m_AnyZeroFP())) &&
|
||||||
match(TrueVal, m_FSub(m_PosZeroFP(), m_Specific(FalseVal))) &&
|
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)) {
|
(Pred == FCmpInst::FCMP_OLE || Pred == FCmpInst::FCMP_ULE)) {
|
||||||
Value *Fabs = Builder.CreateUnaryIntrinsic(Intrinsic::fabs, FalseVal, &SI);
|
Value *Fabs = Builder.CreateUnaryIntrinsic(Intrinsic::fabs, FalseVal, &SI);
|
||||||
return replaceInstUsesWith(SI, Fabs);
|
return replaceInstUsesWith(SI, Fabs);
|
||||||
|
@ -2878,7 +2876,7 @@ Instruction *InstCombinerImpl::visitSelectInst(SelectInst &SI) {
|
||||||
// (X > +/-0.0) ? X : (0.0 - X) --> fabs(X)
|
// (X > +/-0.0) ? X : (0.0 - X) --> fabs(X)
|
||||||
if (match(CondVal, m_FCmp(Pred, m_Specific(TrueVal), m_AnyZeroFP())) &&
|
if (match(CondVal, m_FCmp(Pred, m_Specific(TrueVal), m_AnyZeroFP())) &&
|
||||||
match(FalseVal, m_FSub(m_PosZeroFP(), m_Specific(TrueVal))) &&
|
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)) {
|
(Pred == FCmpInst::FCMP_OGT || Pred == FCmpInst::FCMP_UGT)) {
|
||||||
Value *Fabs = Builder.CreateUnaryIntrinsic(Intrinsic::fabs, TrueVal, &SI);
|
Value *Fabs = Builder.CreateUnaryIntrinsic(Intrinsic::fabs, TrueVal, &SI);
|
||||||
return replaceInstUsesWith(SI, Fabs);
|
return replaceInstUsesWith(SI, Fabs);
|
||||||
|
@ -2889,8 +2887,7 @@ Instruction *InstCombinerImpl::visitSelectInst(SelectInst &SI) {
|
||||||
Instruction *FNeg;
|
Instruction *FNeg;
|
||||||
if (match(CondVal, m_FCmp(Pred, m_Specific(FalseVal), m_AnyZeroFP())) &&
|
if (match(CondVal, m_FCmp(Pred, m_Specific(FalseVal), m_AnyZeroFP())) &&
|
||||||
match(TrueVal, m_FNeg(m_Specific(FalseVal))) &&
|
match(TrueVal, m_FNeg(m_Specific(FalseVal))) &&
|
||||||
match(TrueVal, m_Instruction(FNeg)) && FNeg->hasNoNaNs() &&
|
match(TrueVal, m_Instruction(FNeg)) && SI.hasNoSignedZeros() &&
|
||||||
FNeg->hasNoSignedZeros() && SI.hasNoSignedZeros() &&
|
|
||||||
(Pred == FCmpInst::FCMP_OLT || Pred == FCmpInst::FCMP_OLE ||
|
(Pred == FCmpInst::FCMP_OLT || Pred == FCmpInst::FCMP_OLE ||
|
||||||
Pred == FCmpInst::FCMP_ULT || Pred == FCmpInst::FCMP_ULE)) {
|
Pred == FCmpInst::FCMP_ULT || Pred == FCmpInst::FCMP_ULE)) {
|
||||||
Value *Fabs = Builder.CreateUnaryIntrinsic(Intrinsic::fabs, FalseVal, &SI);
|
Value *Fabs = Builder.CreateUnaryIntrinsic(Intrinsic::fabs, FalseVal, &SI);
|
||||||
|
@ -2901,8 +2898,7 @@ Instruction *InstCombinerImpl::visitSelectInst(SelectInst &SI) {
|
||||||
// (X >= +/-0.0) ? X : -X --> fabs(X)
|
// (X >= +/-0.0) ? X : -X --> fabs(X)
|
||||||
if (match(CondVal, m_FCmp(Pred, m_Specific(TrueVal), m_AnyZeroFP())) &&
|
if (match(CondVal, m_FCmp(Pred, m_Specific(TrueVal), m_AnyZeroFP())) &&
|
||||||
match(FalseVal, m_FNeg(m_Specific(TrueVal))) &&
|
match(FalseVal, m_FNeg(m_Specific(TrueVal))) &&
|
||||||
match(FalseVal, m_Instruction(FNeg)) && FNeg->hasNoNaNs() &&
|
match(FalseVal, m_Instruction(FNeg)) && SI.hasNoSignedZeros() &&
|
||||||
FNeg->hasNoSignedZeros() && SI.hasNoSignedZeros() &&
|
|
||||||
(Pred == FCmpInst::FCMP_OGT || Pred == FCmpInst::FCMP_OGE ||
|
(Pred == FCmpInst::FCMP_OGT || Pred == FCmpInst::FCMP_OGE ||
|
||||||
Pred == FCmpInst::FCMP_UGT || Pred == FCmpInst::FCMP_UGE)) {
|
Pred == FCmpInst::FCMP_UGT || Pred == FCmpInst::FCMP_UGE)) {
|
||||||
Value *Fabs = Builder.CreateUnaryIntrinsic(Intrinsic::fabs, TrueVal, &SI);
|
Value *Fabs = Builder.CreateUnaryIntrinsic(Intrinsic::fabs, TrueVal, &SI);
|
||||||
|
|
|
@ -254,10 +254,8 @@ define double @multi_use_fabs_fpext(float %x) {
|
||||||
|
|
||||||
define double @select_fcmp_ole_zero(double %x) {
|
define double @select_fcmp_ole_zero(double %x) {
|
||||||
; CHECK-LABEL: @select_fcmp_ole_zero(
|
; CHECK-LABEL: @select_fcmp_ole_zero(
|
||||||
; CHECK-NEXT: [[LEZERO:%.*]] = fcmp ole double [[X:%.*]], 0.000000e+00
|
; CHECK-NEXT: [[TMP1:%.*]] = call double @llvm.fabs.f64(double [[X:%.*]])
|
||||||
; CHECK-NEXT: [[NEGX:%.*]] = fsub double 0.000000e+00, [[X]]
|
; CHECK-NEXT: ret double [[TMP1]]
|
||||||
; CHECK-NEXT: [[FABS:%.*]] = select i1 [[LEZERO]], double [[NEGX]], double [[X]]
|
|
||||||
; CHECK-NEXT: ret double [[FABS]]
|
|
||||||
;
|
;
|
||||||
%lezero = fcmp ole double %x, 0.0
|
%lezero = fcmp ole double %x, 0.0
|
||||||
%negx = fsub double 0.0, %x
|
%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) {
|
define fp128 @select_fcmp_ogt_zero(fp128 %x) {
|
||||||
; CHECK-LABEL: @select_fcmp_ogt_zero(
|
; CHECK-LABEL: @select_fcmp_ogt_zero(
|
||||||
; CHECK-NEXT: [[GTZERO:%.*]] = fcmp ogt fp128 [[X:%.*]], 0xL00000000000000000000000000000000
|
; CHECK-NEXT: [[TMP1:%.*]] = call fp128 @llvm.fabs.f128(fp128 [[X:%.*]])
|
||||||
; CHECK-NEXT: [[NEGX:%.*]] = fsub fp128 0xL00000000000000000000000000000000, [[X]]
|
; CHECK-NEXT: ret fp128 [[TMP1]]
|
||||||
; CHECK-NEXT: [[FABS:%.*]] = select i1 [[GTZERO]], fp128 [[X]], fp128 [[NEGX]]
|
|
||||||
; CHECK-NEXT: ret fp128 [[FABS]]
|
|
||||||
;
|
;
|
||||||
%gtzero = fcmp ogt fp128 %x, zeroinitializer
|
%gtzero = fcmp ogt fp128 %x, zeroinitializer
|
||||||
%negx = fsub fp128 zeroinitializer, %x
|
%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) {
|
define double @select_fcmp_olt_zero_unary_fneg(double %x) {
|
||||||
; CHECK-LABEL: @select_fcmp_olt_zero_unary_fneg(
|
; CHECK-LABEL: @select_fcmp_olt_zero_unary_fneg(
|
||||||
; CHECK-NEXT: [[LTZERO:%.*]] = fcmp olt double [[X:%.*]], 0.000000e+00
|
; CHECK-NEXT: [[TMP1:%.*]] = call nsz double @llvm.fabs.f64(double [[X:%.*]])
|
||||||
; CHECK-NEXT: [[NEGX:%.*]] = fneg double [[X]]
|
; CHECK-NEXT: ret double [[TMP1]]
|
||||||
; CHECK-NEXT: [[FABS:%.*]] = select nsz i1 [[LTZERO]], double [[NEGX]], double [[X]]
|
|
||||||
; CHECK-NEXT: ret double [[FABS]]
|
|
||||||
;
|
;
|
||||||
%ltzero = fcmp olt double %x, 0.0
|
%ltzero = fcmp olt double %x, 0.0
|
||||||
%negx = fneg double %x
|
%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) {
|
define <2 x float> @select_fcmp_ogt_zero_unary_fneg(<2 x float> %x) {
|
||||||
; CHECK-LABEL: @select_fcmp_ogt_zero_unary_fneg(
|
; CHECK-LABEL: @select_fcmp_ogt_zero_unary_fneg(
|
||||||
; CHECK-NEXT: [[GTZERO:%.*]] = fcmp ogt <2 x float> [[X:%.*]], zeroinitializer
|
; CHECK-NEXT: [[TMP1:%.*]] = call nsz <2 x float> @llvm.fabs.v2f32(<2 x float> [[X:%.*]])
|
||||||
; CHECK-NEXT: [[NEGX:%.*]] = fneg <2 x float> [[X]]
|
; CHECK-NEXT: ret <2 x float> [[TMP1]]
|
||||||
; CHECK-NEXT: [[FABS:%.*]] = select nsz <2 x i1> [[GTZERO]], <2 x float> [[X]], <2 x float> [[NEGX]]
|
|
||||||
; CHECK-NEXT: ret <2 x float> [[FABS]]
|
|
||||||
;
|
;
|
||||||
%gtzero = fcmp ogt <2 x float> %x, zeroinitializer
|
%gtzero = fcmp ogt <2 x float> %x, zeroinitializer
|
||||||
%negx = fneg <2 x float> %x
|
%negx = fneg <2 x float> %x
|
||||||
|
|
Loading…
Reference in New Issue