diff --git a/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp --- a/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp @@ -2285,6 +2285,12 @@ // complex pattern matching and remove this from InstCombine. if (Value *V = FAddCombine(Builder).simplify(&I)) return replaceInstUsesWith(I, V); + + // (X - Y) - Op1 --> X - (Y + Op1) + if (match(Op0, m_OneUse(m_FSub(m_Value(X), m_Value(Y))))) { + Value *FAdd = Builder.CreateFAddFMF(Y, Op1, &I); + return BinaryOperator::CreateFSubFMF(X, FAdd, &I); + } } return nullptr; diff --git a/llvm/test/Transforms/InstCombine/fsub.ll b/llvm/test/Transforms/InstCombine/fsub.ll --- a/llvm/test/Transforms/InstCombine/fsub.ll +++ b/llvm/test/Transforms/InstCombine/fsub.ll @@ -642,6 +642,8 @@ ret float %r } +; Negative test - can't reassociate without FMF. + define float @fsub_fsub(float %x, float %y, float %z) { ; CHECK-LABEL: @fsub_fsub( ; CHECK-NEXT: [[XY:%.*]] = fsub float [[X:%.*]], [[Y:%.*]] @@ -653,6 +655,8 @@ ret float %xyz } +; Negative test - can't reassociate without enough FMF. + define float @fsub_fsub_nsz(float %x, float %y, float %z) { ; CHECK-LABEL: @fsub_fsub_nsz( ; CHECK-NEXT: [[XY:%.*]] = fsub float [[X:%.*]], [[Y:%.*]] @@ -664,6 +668,8 @@ ret float %xyz } +; Negative test - can't reassociate without enough FMF. + define float @fsub_fsub_reassoc(float %x, float %y, float %z) { ; CHECK-LABEL: @fsub_fsub_reassoc( ; CHECK-NEXT: [[XY:%.*]] = fsub float [[X:%.*]], [[Y:%.*]] @@ -677,8 +683,8 @@ define float @fsub_fsub_nsz_reassoc(float %x, float %y, float %z) { ; CHECK-LABEL: @fsub_fsub_nsz_reassoc( -; CHECK-NEXT: [[XY:%.*]] = fsub float [[X:%.*]], [[Y:%.*]] -; CHECK-NEXT: [[XYZ:%.*]] = fsub reassoc nsz float [[XY]], [[Z:%.*]] +; CHECK-NEXT: [[TMP1:%.*]] = fadd reassoc nsz float [[Y:%.*]], [[Z:%.*]] +; CHECK-NEXT: [[XYZ:%.*]] = fsub reassoc nsz float [[X:%.*]], [[TMP1]] ; CHECK-NEXT: ret float [[XYZ]] ; %xy = fsub float %x, %y @@ -688,8 +694,8 @@ define <2 x double> @fsub_fsub_fast_vec(<2 x double> %x, <2 x double> %y, <2 x double> %z) { ; CHECK-LABEL: @fsub_fsub_fast_vec( -; CHECK-NEXT: [[XY:%.*]] = fsub fast <2 x double> [[X:%.*]], [[Y:%.*]] -; CHECK-NEXT: [[XYZ:%.*]] = fsub fast <2 x double> [[XY]], [[Z:%.*]] +; CHECK-NEXT: [[TMP1:%.*]] = fadd fast <2 x double> [[Y:%.*]], [[Z:%.*]] +; CHECK-NEXT: [[XYZ:%.*]] = fsub fast <2 x double> [[X:%.*]], [[TMP1]] ; CHECK-NEXT: ret <2 x double> [[XYZ]] ; %xy = fsub fast <2 x double> %x, %y @@ -697,6 +703,8 @@ ret <2 x double> %xyz } +; Negative test - don't reassociate and increase instructions. + define float @fsub_fsub_nsz_reassoc_extra_use(float %x, float %y, float %z) { ; CHECK-LABEL: @fsub_fsub_nsz_reassoc_extra_use( ; CHECK-NEXT: [[XY:%.*]] = fsub float [[X:%.*]], [[Y:%.*]] @@ -734,8 +742,8 @@ define float @fake_fneg_fsub_fast(float %x, float %y) { ; CHECK-LABEL: @fake_fneg_fsub_fast( -; CHECK-NEXT: [[NEGX:%.*]] = fsub float -0.000000e+00, [[X:%.*]] -; CHECK-NEXT: [[SUB:%.*]] = fsub fast float [[NEGX]], [[Y:%.*]] +; CHECK-NEXT: [[TMP1:%.*]] = fadd fast float [[X:%.*]], [[Y:%.*]] +; CHECK-NEXT: [[SUB:%.*]] = fsub fast float -0.000000e+00, [[TMP1]] ; CHECK-NEXT: ret float [[SUB]] ; %negx = fsub float -0.0, %x diff --git a/llvm/test/Transforms/Reassociate/fast-SubReassociate.ll b/llvm/test/Transforms/Reassociate/fast-SubReassociate.ll --- a/llvm/test/Transforms/Reassociate/fast-SubReassociate.ll +++ b/llvm/test/Transforms/Reassociate/fast-SubReassociate.ll @@ -79,16 +79,10 @@ ; With sub reassociation, constant folding can eliminate the two 12 constants. define float @test4(float %A, float %B, float %C, float %D) { -; FIXME: InstCombine should be able to get us to the following: -; %sum = fadd fast float %B, %A -; %sum1 = fadd fast float %sum, %C -; %Q = fsub fast float %D, %sum1 -; ret i32 %Q ; CHECK-LABEL: @test4( -; CHECK-NEXT: [[B_NEG:%.*]] = fsub fast float -0.000000e+00, [[B:%.*]] -; CHECK-NEXT: [[O_NEG:%.*]] = fsub fast float [[B_NEG]], [[A:%.*]] -; CHECK-NEXT: [[P:%.*]] = fsub fast float [[O_NEG]], [[C:%.*]] -; CHECK-NEXT: [[Q:%.*]] = fadd fast float [[P]], [[D:%.*]] +; CHECK-NEXT: [[TMP1:%.*]] = fadd fast float [[B:%.*]], [[A:%.*]] +; CHECK-NEXT: [[TMP2:%.*]] = fadd fast float [[TMP1]], [[C:%.*]] +; CHECK-NEXT: [[Q:%.*]] = fsub fast float [[D:%.*]], [[TMP2]] ; CHECK-NEXT: ret float [[Q]] ; %M = fadd fast float 1.200000e+01, %A diff --git a/llvm/test/Transforms/Reassociate/fast-basictest.ll b/llvm/test/Transforms/Reassociate/fast-basictest.ll --- a/llvm/test/Transforms/Reassociate/fast-basictest.ll +++ b/llvm/test/Transforms/Reassociate/fast-basictest.ll @@ -669,13 +669,9 @@ ; With sub reassociation, constant folding can eliminate the uses of %a. define float @test20(float %a, float %b, float %c) nounwind { -; FIXME: Should be able to generate the below, which may expose more -; opportunites for FAdd reassociation. -; %sum = fadd fast float %c, %b -; %t7 = fsub fast float 0, %sum ; CHECK-LABEL: @test20( -; CHECK-NEXT: [[B_NEG:%.*]] = fsub fast float -0.000000e+00, [[B:%.*]] -; CHECK-NEXT: [[T7:%.*]] = fsub fast float [[B_NEG]], [[C:%.*]] +; CHECK-NEXT: [[TMP1:%.*]] = fadd fast float [[B:%.*]], [[C:%.*]] +; CHECK-NEXT: [[T7:%.*]] = fsub fast float -0.000000e+00, [[TMP1]] ; CHECK-NEXT: ret float [[T7]] ; %t3 = fsub fast float %a, %b