[InstCombine] convert fsub nsz with fneg operand to -(X + Y)

This was noted in D72521 - we need to match fneg specifically to
consistently handle that pattern along with (-0.0 - X).
This commit is contained in:
Sanjay Patel 2020-01-27 14:40:43 -05:00
parent bcfa0f592f
commit 242fed9d7f
3 changed files with 13 additions and 6 deletions

View File

@ -2159,6 +2159,13 @@ Instruction *InstCombiner::visitFSub(BinaryOperator &I) {
}
}
// (-X) - Op1 --> -(X + Op1)
if (I.hasNoSignedZeros() && !isa<ConstantExpr>(Op0) &&
match(Op0, m_OneUse(m_FNeg(m_Value(X))))) {
Value *FAdd = Builder.CreateFAddFMF(X, Op1, &I);
return UnaryOperator::CreateFNegFMF(FAdd, &I);
}
if (isa<Constant>(Op0))
if (SelectInst *SI = dyn_cast<SelectInst>(Op1))
if (Instruction *NV = FoldOpIntoSelect(I, SI))

View File

@ -731,8 +731,8 @@ define float @fneg_fsub(float %x, float %y) {
define float @fneg_fsub_nsz(float %x, float %y) {
; CHECK-LABEL: @fneg_fsub_nsz(
; CHECK-NEXT: [[NEGX:%.*]] = fneg float [[X:%.*]]
; CHECK-NEXT: [[SUB:%.*]] = fsub nsz float [[NEGX]], [[Y:%.*]]
; CHECK-NEXT: [[TMP1:%.*]] = fadd nsz float [[X:%.*]], [[Y:%.*]]
; CHECK-NEXT: [[SUB:%.*]] = fneg nsz float [[TMP1]]
; CHECK-NEXT: ret float [[SUB]]
;
%negx = fneg float %x
@ -743,7 +743,7 @@ define float @fneg_fsub_nsz(float %x, float %y) {
define float @fake_fneg_fsub_fast(float %x, float %y) {
; CHECK-LABEL: @fake_fneg_fsub_fast(
; CHECK-NEXT: [[TMP1:%.*]] = fadd fast float [[X:%.*]], [[Y:%.*]]
; CHECK-NEXT: [[SUB:%.*]] = fsub fast float -0.000000e+00, [[TMP1]]
; CHECK-NEXT: [[SUB:%.*]] = fneg fast float [[TMP1]]
; CHECK-NEXT: ret float [[SUB]]
;
%negx = fsub float -0.0, %x
@ -766,8 +766,8 @@ define float @fake_fneg_fsub_fast_extra_use(float %x, float %y) {
define <2 x float> @fake_fneg_fsub_vec(<2 x float> %x, <2 x float> %y) {
; CHECK-LABEL: @fake_fneg_fsub_vec(
; CHECK-NEXT: [[NEGX:%.*]] = fsub <2 x float> <float -0.000000e+00, float -0.000000e+00>, [[X:%.*]]
; CHECK-NEXT: [[SUB:%.*]] = fsub nsz <2 x float> [[NEGX]], [[Y:%.*]]
; CHECK-NEXT: [[TMP1:%.*]] = fadd nsz <2 x float> [[X:%.*]], [[Y:%.*]]
; CHECK-NEXT: [[SUB:%.*]] = fneg nsz <2 x float> [[TMP1]]
; CHECK-NEXT: ret <2 x float> [[SUB]]
;
%negx = fsub <2 x float> <float -0.0, float -0.0>, %x

View File

@ -671,7 +671,7 @@ define float @test19_reassoc(float %A, float %B) {
define float @test20(float %a, float %b, float %c) nounwind {
; CHECK-LABEL: @test20(
; CHECK-NEXT: [[TMP1:%.*]] = fadd fast float [[B:%.*]], [[C:%.*]]
; CHECK-NEXT: [[T7:%.*]] = fsub fast float -0.000000e+00, [[TMP1]]
; CHECK-NEXT: [[T7:%.*]] = fneg fast float [[TMP1]]
; CHECK-NEXT: ret float [[T7]]
;
%t3 = fsub fast float %a, %b