Skip to content

Commit 6180ce2

Browse files
committed
[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). Differential Revision: https://reviews.llvm.org/D106872
1 parent 80c17bb commit 6180ce2

File tree

2 files changed

+13
-25
lines changed

2 files changed

+13
-25
lines changed

llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2863,22 +2863,20 @@ Instruction *InstCombinerImpl::visitSelectInst(SelectInst &SI) {
28632863
}
28642864

28652865
// Canonicalize select with fcmp to fabs(). -0.0 makes this tricky. We need
2866-
// fast-math-flags (nsz) or fsub with +0.0 (not fneg) for this to work. We
2867-
// also require nnan because we do not want to unintentionally change the
2868-
// sign of a NaN value.
2866+
// fast-math-flags (nsz) or fsub with +0.0 (not fneg) for this to work.
28692867
// (X <= +/-0.0) ? (0.0 - X) : X --> fabs(X)
28702868
Instruction *FSub;
28712869
if (match(CondVal, m_FCmp(Pred, m_Specific(FalseVal), m_AnyZeroFP())) &&
28722870
match(TrueVal, m_FSub(m_PosZeroFP(), m_Specific(FalseVal))) &&
2873-
match(TrueVal, m_Instruction(FSub)) && FSub->hasNoNaNs() &&
2871+
match(TrueVal, m_Instruction(FSub)) &&
28742872
(Pred == FCmpInst::FCMP_OLE || Pred == FCmpInst::FCMP_ULE)) {
28752873
Value *Fabs = Builder.CreateUnaryIntrinsic(Intrinsic::fabs, FalseVal, &SI);
28762874
return replaceInstUsesWith(SI, Fabs);
28772875
}
28782876
// (X > +/-0.0) ? X : (0.0 - X) --> fabs(X)
28792877
if (match(CondVal, m_FCmp(Pred, m_Specific(TrueVal), m_AnyZeroFP())) &&
28802878
match(FalseVal, m_FSub(m_PosZeroFP(), m_Specific(TrueVal))) &&
2881-
match(FalseVal, m_Instruction(FSub)) && FSub->hasNoNaNs() &&
2879+
match(FalseVal, m_Instruction(FSub)) &&
28822880
(Pred == FCmpInst::FCMP_OGT || Pred == FCmpInst::FCMP_UGT)) {
28832881
Value *Fabs = Builder.CreateUnaryIntrinsic(Intrinsic::fabs, TrueVal, &SI);
28842882
return replaceInstUsesWith(SI, Fabs);
@@ -2889,8 +2887,7 @@ Instruction *InstCombinerImpl::visitSelectInst(SelectInst &SI) {
28892887
Instruction *FNeg;
28902888
if (match(CondVal, m_FCmp(Pred, m_Specific(FalseVal), m_AnyZeroFP())) &&
28912889
match(TrueVal, m_FNeg(m_Specific(FalseVal))) &&
2892-
match(TrueVal, m_Instruction(FNeg)) && FNeg->hasNoNaNs() &&
2893-
FNeg->hasNoSignedZeros() && SI.hasNoSignedZeros() &&
2890+
match(TrueVal, m_Instruction(FNeg)) && SI.hasNoSignedZeros() &&
28942891
(Pred == FCmpInst::FCMP_OLT || Pred == FCmpInst::FCMP_OLE ||
28952892
Pred == FCmpInst::FCMP_ULT || Pred == FCmpInst::FCMP_ULE)) {
28962893
Value *Fabs = Builder.CreateUnaryIntrinsic(Intrinsic::fabs, FalseVal, &SI);
@@ -2901,8 +2898,7 @@ Instruction *InstCombinerImpl::visitSelectInst(SelectInst &SI) {
29012898
// (X >= +/-0.0) ? X : -X --> fabs(X)
29022899
if (match(CondVal, m_FCmp(Pred, m_Specific(TrueVal), m_AnyZeroFP())) &&
29032900
match(FalseVal, m_FNeg(m_Specific(TrueVal))) &&
2904-
match(FalseVal, m_Instruction(FNeg)) && FNeg->hasNoNaNs() &&
2905-
FNeg->hasNoSignedZeros() && SI.hasNoSignedZeros() &&
2901+
match(FalseVal, m_Instruction(FNeg)) && SI.hasNoSignedZeros() &&
29062902
(Pred == FCmpInst::FCMP_OGT || Pred == FCmpInst::FCMP_OGE ||
29072903
Pred == FCmpInst::FCMP_UGT || Pred == FCmpInst::FCMP_UGE)) {
29082904
Value *Fabs = Builder.CreateUnaryIntrinsic(Intrinsic::fabs, TrueVal, &SI);

llvm/test/Transforms/InstCombine/fabs.ll

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -254,10 +254,8 @@ define double @multi_use_fabs_fpext(float %x) {
254254

255255
define double @select_fcmp_ole_zero(double %x) {
256256
; CHECK-LABEL: @select_fcmp_ole_zero(
257-
; CHECK-NEXT: [[LEZERO:%.*]] = fcmp ole double [[X:%.*]], 0.000000e+00
258-
; CHECK-NEXT: [[NEGX:%.*]] = fsub double 0.000000e+00, [[X]]
259-
; CHECK-NEXT: [[FABS:%.*]] = select i1 [[LEZERO]], double [[NEGX]], double [[X]]
260-
; CHECK-NEXT: ret double [[FABS]]
257+
; CHECK-NEXT: [[TMP1:%.*]] = call double @llvm.fabs.f64(double [[X:%.*]])
258+
; CHECK-NEXT: ret double [[TMP1]]
261259
;
262260
%lezero = fcmp ole double %x, 0.0
263261
%negx = fsub double 0.0, %x
@@ -345,10 +343,8 @@ define <2 x float> @select_nnan_fcmp_nnan_ole_negzero(<2 x float> %x) {
345343

346344
define fp128 @select_fcmp_ogt_zero(fp128 %x) {
347345
; CHECK-LABEL: @select_fcmp_ogt_zero(
348-
; CHECK-NEXT: [[GTZERO:%.*]] = fcmp ogt fp128 [[X:%.*]], 0xL00000000000000000000000000000000
349-
; CHECK-NEXT: [[NEGX:%.*]] = fsub fp128 0xL00000000000000000000000000000000, [[X]]
350-
; CHECK-NEXT: [[FABS:%.*]] = select i1 [[GTZERO]], fp128 [[X]], fp128 [[NEGX]]
351-
; CHECK-NEXT: ret fp128 [[FABS]]
346+
; CHECK-NEXT: [[TMP1:%.*]] = call fp128 @llvm.fabs.f128(fp128 [[X:%.*]])
347+
; CHECK-NEXT: ret fp128 [[TMP1]]
352348
;
353349
%gtzero = fcmp ogt fp128 %x, zeroinitializer
354350
%negx = fsub fp128 zeroinitializer, %x
@@ -434,10 +430,8 @@ define half @select_fcmp_nnan_oge_negzero(half %x) {
434430

435431
define double @select_fcmp_olt_zero_unary_fneg(double %x) {
436432
; CHECK-LABEL: @select_fcmp_olt_zero_unary_fneg(
437-
; CHECK-NEXT: [[LTZERO:%.*]] = fcmp olt double [[X:%.*]], 0.000000e+00
438-
; CHECK-NEXT: [[NEGX:%.*]] = fneg double [[X]]
439-
; CHECK-NEXT: [[FABS:%.*]] = select nsz i1 [[LTZERO]], double [[NEGX]], double [[X]]
440-
; CHECK-NEXT: ret double [[FABS]]
433+
; CHECK-NEXT: [[TMP1:%.*]] = call nsz double @llvm.fabs.f64(double [[X:%.*]])
434+
; CHECK-NEXT: ret double [[TMP1]]
441435
;
442436
%ltzero = fcmp olt double %x, 0.0
443437
%negx = fneg double %x
@@ -723,10 +717,8 @@ define float @select_fcmp_nnan_nsz_ule_negzero_unary_fneg(float %x) {
723717

724718
define <2 x float> @select_fcmp_ogt_zero_unary_fneg(<2 x float> %x) {
725719
; CHECK-LABEL: @select_fcmp_ogt_zero_unary_fneg(
726-
; CHECK-NEXT: [[GTZERO:%.*]] = fcmp ogt <2 x float> [[X:%.*]], zeroinitializer
727-
; CHECK-NEXT: [[NEGX:%.*]] = fneg <2 x float> [[X]]
728-
; CHECK-NEXT: [[FABS:%.*]] = select nsz <2 x i1> [[GTZERO]], <2 x float> [[X]], <2 x float> [[NEGX]]
729-
; CHECK-NEXT: ret <2 x float> [[FABS]]
720+
; CHECK-NEXT: [[TMP1:%.*]] = call nsz <2 x float> @llvm.fabs.v2f32(<2 x float> [[X:%.*]])
721+
; CHECK-NEXT: ret <2 x float> [[TMP1]]
730722
;
731723
%gtzero = fcmp ogt <2 x float> %x, zeroinitializer
732724
%negx = fneg <2 x float> %x

0 commit comments

Comments
 (0)