[InstCombine] Z / (1.0 / Y) => (Y * Z)
author@raghesh (Raghesh Aloor) <raghesh.a@gmail.com>
Thu, 9 Jan 2020 15:52:39 +0000 (10:52 -0500)
committerSanjay Patel <spatel@rotateright.com>
Thu, 9 Jan 2020 15:52:39 +0000 (10:52 -0500)
This is a special case of Z / (X / Y) => (Y * Z) / X, with X = 1.0.
The m_OneUse check is avoided because even in the case of the
multiple uses for 1.0/Y, the number of instructions remain the same
and a division is replaced by a multiplication.

Differential Revision: https://reviews.llvm.org/D72319

llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
llvm/test/Transforms/InstCombine/fdiv.ll

index f7b39d9..2774e46 100644 (file)
@@ -1239,6 +1239,14 @@ Instruction *InstCombiner::visitFDiv(BinaryOperator &I) {
       Value *YZ = Builder.CreateFMulFMF(Y, Op0, &I);
       return BinaryOperator::CreateFDivFMF(YZ, X, &I);
     }
+    // Z / (1.0 / Y) => (Y * Z)
+    //
+    // This is a special case of Z / (X / Y) => (Y * Z) / X, with X = 1.0. The
+    // m_OneUse check is avoided because even in the case of the multiple uses
+    // for 1.0/Y, the number of instructions remain the same and a division is
+    // replaced by a multiplication.
+    if (match(Op1, m_FDiv(m_SpecificFP(1.0), m_Value(Y))))
+      return BinaryOperator::CreateFMulFMF(Y, Op0, &I);
   }
 
   if (I.hasAllowReassoc() && Op0->hasOneUse() && Op1->hasOneUse()) {
index 178fe1b..ec1119c 100644 (file)
@@ -187,12 +187,12 @@ define float @div_with_div_denominator_extra_use(float %x, float %y, float %z) {
   ret float %div2
 }
 
-; Z / (1.0 / Y)
+; Z / (1.0 / Y) ==> Y * Z
 
 define float @div_with_div_denominator_with_one_as_numerator_extra_use(float %x, float %y, float %z) {
 ; CHECK-LABEL: @div_with_div_denominator_with_one_as_numerator_extra_use(
 ; CHECK-NEXT:    [[DIV1:%.*]] = fdiv float 1.000000e+00, [[Y:%.*]]
-; CHECK-NEXT:    [[DIV2:%.*]] = fdiv reassoc arcp float [[Z:%.*]], [[DIV1]]
+; CHECK-NEXT:    [[DIV2:%.*]] = fmul reassoc arcp float [[Y]], [[Z:%.*]]
 ; CHECK-NEXT:    call void @use_f32(float [[DIV1]])
 ; CHECK-NEXT:    ret float [[DIV2]]
 ;