[InstCombine] reassociate fsub+fadd with FMF to increase adds and throughput
authorSanjay Patel <spatel@rotateright.com>
Tue, 26 May 2020 16:48:22 +0000 (12:48 -0400)
committerSanjay Patel <spatel@rotateright.com>
Tue, 26 May 2020 17:17:15 +0000 (13:17 -0400)
The -reassociate pass tends to transform this kind of pattern into
something that is worse for vectorization and codegen. See PR43953:
https://bugs.llvm.org/show_bug.cgi?id=43953

llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
llvm/test/Transforms/InstCombine/fsub.ll

index 85d6f47..233e0c7 100644 (file)
@@ -2195,6 +2195,17 @@ Instruction *InstCombiner::visitFSub(BinaryOperator &I) {
       return BinaryOperator::CreateFMulFMF(Op0, OneSubC, &I);
     }
 
+    // Reassociate fsub/fadd sequences to create more fadd instructions and
+    // reduce dependency chains:
+    // ((X - Y) + Z) - Op1 --> (X + Z) - (Y + Op1)
+    Value *Z;
+    if (match(Op0, m_OneUse(m_c_FAdd(m_OneUse(m_FSub(m_Value(X), m_Value(Y))),
+                                     m_Value(Z))))) {
+      Value *XZ = Builder.CreateFAddFMF(X, Z, &I);
+      Value *YW = Builder.CreateFAddFMF(Y, Op1, &I);
+      return BinaryOperator::CreateFSubFMF(XZ, YW, &I);
+    }
+
     if (Instruction *F = factorizeFAddFSub(I, Builder))
       return F;
 
index 68e49c2..a0f02de 100644 (file)
@@ -785,11 +785,13 @@ define float @fneg_fsub_constant(float %x) {
   ret float %sub
 }
 
+; ((w-x) + y) - z --> (w+y) - (x+z)
+
 define float @fsub_fadd_fsub_reassoc(float %w, float %x, float %y, float %z) {
 ; CHECK-LABEL: @fsub_fadd_fsub_reassoc(
-; CHECK-NEXT:    [[S1:%.*]] = fsub reassoc nsz float [[W:%.*]], [[X:%.*]]
-; CHECK-NEXT:    [[A:%.*]] = fadd reassoc nsz float [[S1]], [[Y:%.*]]
-; CHECK-NEXT:    [[S2:%.*]] = fsub reassoc nsz float [[A]], [[Z:%.*]]
+; CHECK-NEXT:    [[TMP1:%.*]] = fadd reassoc nsz float [[W:%.*]], [[Y:%.*]]
+; CHECK-NEXT:    [[TMP2:%.*]] = fadd reassoc nsz float [[X:%.*]], [[Z:%.*]]
+; CHECK-NEXT:    [[S2:%.*]] = fsub reassoc nsz float [[TMP1]], [[TMP2]]
 ; CHECK-NEXT:    ret float [[S2]]
 ;
   %s1 = fsub reassoc nsz float %w, %x
@@ -798,12 +800,14 @@ define float @fsub_fadd_fsub_reassoc(float %w, float %x, float %y, float %z) {
   ret float %s2
 }
 
+; FMF on the last op is enough to do the transform; vectors work too.
+
 define <2 x float> @fsub_fadd_fsub_reassoc_commute(<2 x float> %w, <2 x float> %x, <2 x float> %y, <2 x float> %z) {
 ; CHECK-LABEL: @fsub_fadd_fsub_reassoc_commute(
 ; CHECK-NEXT:    [[D:%.*]] = fdiv <2 x float> [[Y:%.*]], <float 4.200000e+01, float -4.200000e+01>
-; CHECK-NEXT:    [[S1:%.*]] = fsub <2 x float> [[W:%.*]], [[X:%.*]]
-; CHECK-NEXT:    [[A:%.*]] = fadd <2 x float> [[D]], [[S1]]
-; CHECK-NEXT:    [[S2:%.*]] = fsub fast <2 x float> [[A]], [[Z:%.*]]
+; CHECK-NEXT:    [[TMP1:%.*]] = fadd fast <2 x float> [[D]], [[W:%.*]]
+; CHECK-NEXT:    [[TMP2:%.*]] = fadd fast <2 x float> [[X:%.*]], [[Z:%.*]]
+; CHECK-NEXT:    [[S2:%.*]] = fsub fast <2 x float> [[TMP1]], [[TMP2]]
 ; CHECK-NEXT:    ret <2 x float> [[S2]]
 ;
   %d = fdiv <2 x float> %y, <float 42.0, float -42.0> ; thwart complexity-based canonicalization
@@ -813,12 +817,14 @@ define <2 x float> @fsub_fadd_fsub_reassoc_commute(<2 x float> %w, <2 x float> %
   ret <2 x float> %s2
 }
 
+; (v-w) + (x-y) - z --> (v+x) - (w+y+z)
+
 define float @fsub_fadd_fsub_reassoc_twice(float %v, float %w, float %x, float %y, float %z) {
 ; CHECK-LABEL: @fsub_fadd_fsub_reassoc_twice(
-; CHECK-NEXT:    [[S1:%.*]] = fsub reassoc nsz float [[V:%.*]], [[W:%.*]]
-; CHECK-NEXT:    [[S2:%.*]] = fsub reassoc nsz float [[X:%.*]], [[Y:%.*]]
-; CHECK-NEXT:    [[A:%.*]] = fadd reassoc nsz float [[S1]], [[S2]]
-; CHECK-NEXT:    [[S3:%.*]] = fsub reassoc nsz float [[A]], [[Z:%.*]]
+; CHECK-NEXT:    [[TMP1:%.*]] = fadd reassoc nsz float [[W:%.*]], [[Z:%.*]]
+; CHECK-NEXT:    [[TMP2:%.*]] = fadd reassoc nsz float [[X:%.*]], [[V:%.*]]
+; CHECK-NEXT:    [[TMP3:%.*]] = fadd reassoc nsz float [[TMP1]], [[Y:%.*]]
+; CHECK-NEXT:    [[S3:%.*]] = fsub reassoc nsz float [[TMP2]], [[TMP3]]
 ; CHECK-NEXT:    ret float [[S3]]
 ;
   %s1 = fsub reassoc nsz float %v, %w
@@ -828,6 +834,8 @@ define float @fsub_fadd_fsub_reassoc_twice(float %v, float %w, float %x, float %
   ret float %s3
 }
 
+; negative test - FMF
+
 define float @fsub_fadd_fsub_not_reassoc(float %w, float %x, float %y, float %z) {
 ; CHECK-LABEL: @fsub_fadd_fsub_not_reassoc(
 ; CHECK-NEXT:    [[S1:%.*]] = fsub fast float [[W:%.*]], [[X:%.*]]
@@ -841,6 +849,8 @@ define float @fsub_fadd_fsub_not_reassoc(float %w, float %x, float %y, float %z)
   ret float %s2
 }
 
+; negative test - uses
+
 define float @fsub_fadd_fsub_reassoc_use1(float %w, float %x, float %y, float %z) {
 ; CHECK-LABEL: @fsub_fadd_fsub_reassoc_use1(
 ; CHECK-NEXT:    [[S1:%.*]] = fsub fast float [[W:%.*]], [[X:%.*]]
@@ -856,6 +866,8 @@ define float @fsub_fadd_fsub_reassoc_use1(float %w, float %x, float %y, float %z
   ret float %s2
 }
 
+; negative test - uses
+
 define float @fsub_fadd_fsub_reassoc_use2(float %w, float %x, float %y, float %z) {
 ; CHECK-LABEL: @fsub_fadd_fsub_reassoc_use2(
 ; CHECK-NEXT:    [[S1:%.*]] = fsub fast float [[W:%.*]], [[X:%.*]]