Index: lib/Transforms/InstCombine/InstCombineSelect.cpp =================================================================== --- lib/Transforms/InstCombine/InstCombineSelect.cpp +++ lib/Transforms/InstCombine/InstCombineSelect.cpp @@ -1569,7 +1569,35 @@ // NOTE: if we wanted to, this is where to detect MIN/MAX } - // NOTE: if we wanted to, this is where to detect ABS + + // 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. + Value *X = FCI->getOperand(0); + FCmpInst::Predicate Pred = FCI->getPredicate(); + if (match(FCI->getOperand(1), m_AnyZeroFP())) { + // (X <= +/-0.0) ? (0.0 - X) : X --> fabs(X) + // (X > +/-0.0) ? X : (0.0 - X) --> fabs(X) + if ((X == FalseVal && match(TrueVal, m_FSub(m_Zero(), m_Specific(X))) && + Pred == FCmpInst::FCMP_OLE) || + (X == TrueVal && match(FalseVal, m_FSub(m_Zero(), m_Specific(X))) && + Pred == FCmpInst::FCMP_OGT)) { + Value *Fabs = Builder.CreateIntrinsic(Intrinsic::fabs, { X }, FCI); + return replaceInstUsesWith(SI, Fabs); + } + // With nsz: + // (X < +/-0.0) ? -X : X --> fabs(X) + // (X <= +/-0.0) ? -X : X --> fabs(X) + // (X > +/-0.0) ? X : -X --> fabs(X) + // (X >= +/-0.0) ? X : -X --> fabs(X) + if (FCI->hasNoSignedZeros() && + ((X == FalseVal && match(TrueVal, m_FNeg(m_Specific(X))) && + (Pred == FCmpInst::FCMP_OLT || Pred == FCmpInst::FCMP_OLE)) || + (X == TrueVal && match(FalseVal, m_FNeg(m_Specific(X))) && + (Pred == FCmpInst::FCMP_OGT || Pred == FCmpInst::FCMP_OGE)))) { + Value *Fabs = Builder.CreateIntrinsic(Intrinsic::fabs, { X }, FCI); + return replaceInstUsesWith(SI, Fabs); + } + } } // See if we are selecting two values based on a comparison of the two values. Index: test/Transforms/InstCombine/fabs.ll =================================================================== --- test/Transforms/InstCombine/fabs.ll +++ test/Transforms/InstCombine/fabs.ll @@ -250,10 +250,8 @@ 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 @@ -265,10 +263,8 @@ define float @select_fcmp_ole_negzero(float %x) { ; CHECK-LABEL: @select_fcmp_ole_negzero( -; CHECK-NEXT: [[LEZERO:%.*]] = fcmp ole float [[X:%.*]], -0.000000e+00 -; CHECK-NEXT: [[NEGX:%.*]] = fsub float 0.000000e+00, [[X]] -; CHECK-NEXT: [[FABS:%.*]] = select i1 [[LEZERO]], float [[NEGX]], float [[X]] -; CHECK-NEXT: ret float [[FABS]] +; CHECK-NEXT: [[TMP1:%.*]] = call float @llvm.fabs.f32(float [[X:%.*]]) +; CHECK-NEXT: ret float [[TMP1]] ; %lezero = fcmp ole float %x, -0.0 %negx = fsub float 0.0, %x @@ -280,10 +276,8 @@ 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 @@ -295,10 +289,8 @@ define half @select_fcmp_ogt_negzero(half %x) { ; CHECK-LABEL: @select_fcmp_ogt_negzero( -; CHECK-NEXT: [[GTZERO:%.*]] = fcmp ogt half [[X:%.*]], 0xH8000 -; CHECK-NEXT: [[NEGX:%.*]] = fsub half 0xH0000, [[X]] -; CHECK-NEXT: [[FABS:%.*]] = select i1 [[GTZERO]], half [[X]], half [[NEGX]] -; CHECK-NEXT: ret half [[FABS]] +; CHECK-NEXT: [[TMP1:%.*]] = call half @llvm.fabs.f16(half [[X:%.*]]) +; CHECK-NEXT: ret half [[TMP1]] ; %gtzero = fcmp ogt half %x, -0.0 %negx = fsub half 0.0, %x @@ -310,10 +302,8 @@ define double @select_fcmp_nsz_olt_zero(double %x) { ; CHECK-LABEL: @select_fcmp_nsz_olt_zero( -; CHECK-NEXT: [[LTZERO:%.*]] = fcmp nsz olt double [[X:%.*]], 0.000000e+00 -; CHECK-NEXT: [[NEGX:%.*]] = fsub double -0.000000e+00, [[X]] -; CHECK-NEXT: [[FABS:%.*]] = select 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 nsz olt double %x, 0.0 %negx = fsub double -0.0, %x @@ -325,10 +315,8 @@ define float @select_fcmp_nsz_olt_negzero(float %x) { ; CHECK-LABEL: @select_fcmp_nsz_olt_negzero( -; CHECK-NEXT: [[LTZERO:%.*]] = fcmp ninf nsz olt float [[X:%.*]], -0.000000e+00 -; CHECK-NEXT: [[NEGX:%.*]] = fsub float -0.000000e+00, [[X]] -; CHECK-NEXT: [[FABS:%.*]] = select i1 [[LTZERO]], float [[NEGX]], float [[X]] -; CHECK-NEXT: ret float [[FABS]] +; CHECK-NEXT: [[TMP1:%.*]] = call ninf nsz float @llvm.fabs.f32(float [[X:%.*]]) +; CHECK-NEXT: ret float [[TMP1]] ; %ltzero = fcmp nsz ninf olt float %x, -0.0 %negx = fsub float -0.0, %x @@ -340,10 +328,8 @@ define double @select_fcmp_nsz_ole_zero(double %x) { ; CHECK-LABEL: @select_fcmp_nsz_ole_zero( -; CHECK-NEXT: [[LEZERO:%.*]] = fcmp fast 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 fast double @llvm.fabs.f64(double [[X:%.*]]) +; CHECK-NEXT: ret double [[TMP1]] ; %lezero = fcmp fast ole double %x, 0.0 %negx = fsub double -0.0, %x @@ -355,10 +341,8 @@ define float @select_fcmp_nsz_ole_negzero(float %x) { ; CHECK-LABEL: @select_fcmp_nsz_ole_negzero( -; CHECK-NEXT: [[LEZERO:%.*]] = fcmp nsz ole float [[X:%.*]], -0.000000e+00 -; CHECK-NEXT: [[NEGX:%.*]] = fsub float -0.000000e+00, [[X]] -; CHECK-NEXT: [[FABS:%.*]] = select i1 [[LEZERO]], float [[NEGX]], float [[X]] -; CHECK-NEXT: ret float [[FABS]] +; CHECK-NEXT: [[TMP1:%.*]] = call nsz float @llvm.fabs.f32(float [[X:%.*]]) +; CHECK-NEXT: ret float [[TMP1]] ; %lezero = fcmp nsz ole float %x, -0.0 %negx = fsub float -0.0, %x @@ -370,10 +354,8 @@ define <2 x float> @select_fcmp_nsz_ogt_zero(<2 x float> %x) { ; CHECK-LABEL: @select_fcmp_nsz_ogt_zero( -; CHECK-NEXT: [[GTZERO:%.*]] = fcmp nsz arcp ogt <2 x float> [[X:%.*]], zeroinitializer -; CHECK-NEXT: [[NEGX:%.*]] = fsub <2 x float> , [[X]] -; CHECK-NEXT: [[FABS:%.*]] = select <2 x i1> [[GTZERO]], <2 x float> [[X]], <2 x float> [[NEGX]] -; CHECK-NEXT: ret <2 x float> [[FABS]] +; CHECK-NEXT: [[TMP1:%.*]] = call nsz arcp <2 x float> @llvm.fabs.v2f32(<2 x float> [[X:%.*]]) +; CHECK-NEXT: ret <2 x float> [[TMP1]] ; %gtzero = fcmp nsz arcp ogt <2 x float> %x, zeroinitializer %negx = fsub <2 x float> , %x @@ -385,10 +367,8 @@ define half @select_fcmp_nsz_ogt_negzero(half %x) { ; CHECK-LABEL: @select_fcmp_nsz_ogt_negzero( -; CHECK-NEXT: [[GTZERO:%.*]] = fcmp fast ogt half [[X:%.*]], 0xH8000 -; CHECK-NEXT: [[NEGX:%.*]] = fsub half 0xH0000, [[X]] -; CHECK-NEXT: [[FABS:%.*]] = select i1 [[GTZERO]], half [[X]], half [[NEGX]] -; CHECK-NEXT: ret half [[FABS]] +; CHECK-NEXT: [[TMP1:%.*]] = call fast half @llvm.fabs.f16(half [[X:%.*]]) +; CHECK-NEXT: ret half [[TMP1]] ; %gtzero = fcmp fast ogt half %x, -0.0 %negx = fsub half 0.0, %x @@ -400,10 +380,8 @@ define <2 x double> @select_fcmp_nsz_oge_zero(<2 x double> %x) { ; CHECK-LABEL: @select_fcmp_nsz_oge_zero( -; CHECK-NEXT: [[GEZERO:%.*]] = fcmp reassoc nsz oge <2 x double> [[X:%.*]], zeroinitializer -; CHECK-NEXT: [[NEGX:%.*]] = fsub <2 x double> , [[X]] -; CHECK-NEXT: [[FABS:%.*]] = select <2 x i1> [[GEZERO]], <2 x double> [[X]], <2 x double> [[NEGX]] -; CHECK-NEXT: ret <2 x double> [[FABS]] +; CHECK-NEXT: [[TMP1:%.*]] = call reassoc nsz <2 x double> @llvm.fabs.v2f64(<2 x double> [[X:%.*]]) +; CHECK-NEXT: ret <2 x double> [[TMP1]] ; %gezero = fcmp nsz reassoc oge <2 x double> %x, zeroinitializer %negx = fsub <2 x double> , %x @@ -415,10 +393,8 @@ define half @select_fcmp_nsz_oge_negzero(half %x) { ; CHECK-LABEL: @select_fcmp_nsz_oge_negzero( -; CHECK-NEXT: [[GEZERO:%.*]] = fcmp nsz oge half [[X:%.*]], 0xH8000 -; CHECK-NEXT: [[NEGX:%.*]] = fsub half 0xH8000, [[X]] -; CHECK-NEXT: [[FABS:%.*]] = select i1 [[GEZERO]], half [[X]], half [[NEGX]] -; CHECK-NEXT: ret half [[FABS]] +; CHECK-NEXT: [[TMP1:%.*]] = call nsz half @llvm.fabs.f16(half [[X:%.*]]) +; CHECK-NEXT: ret half [[TMP1]] ; %gezero = fcmp nsz oge half %x, -0.0 %negx = fsub half -0.0, %x