1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2025-01-31 12:41:49 +01:00

[InstCombine] reassociate fsub+fsub into fsub+fadd

As discussed in the motivating PR44509:
https://bugs.llvm.org/show_bug.cgi?id=44509

...we can end up with worse code using fast-math than without.
This is because the reassociate pass greedily transforms fsub
into fneg/fadd and apparently (based on the regression tests
seen here) expects instcombine to clean that up if it wasn't
profitable. But we were missing this fold:

(X - Y) - Z --> X - (Y + Z)

There's another, more specific case that I think we should
handle as shown in the "fake" fneg test (but missed with a real
fneg), but that's another patch. That may be tricky to get
right without conflicting with existing transforms for fneg.

Differential Revision: https://reviews.llvm.org/D72521
This commit is contained in:
Sanjay Patel 2020-01-15 08:23:46 -05:00
parent 7f70888b46
commit fd9e6d0724
4 changed files with 25 additions and 21 deletions

View File

@ -2285,6 +2285,12 @@ Instruction *InstCombiner::visitFSub(BinaryOperator &I) {
// 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;

View File

@ -642,6 +642,8 @@ define float @fsub_fmul_fneg2_extra_use3(float %x, float %y, float %z) {
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 @@ define float @fsub_fsub(float %x, float %y, float %z) {
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 @@ define float @fsub_fsub_nsz(float %x, float %y, float %z) {
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_reassoc(float %x, float %y, float %z) {
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 float @fsub_fsub_nsz_reassoc(float %x, float %y, float %z) {
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 @@ define <2 x double> @fsub_fsub_fast_vec(<2 x double> %x, <2 x double> %y, <2 x d
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 @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: [[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

View File

@ -79,16 +79,10 @@ define float @test3(float %A, float %B, float %C, float %D) {
; 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

View File

@ -669,13 +669,9 @@ define float @test19_reassoc(float %A, float %B) {
; 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