[llvm] r324927 - [InstCombine] X / (X * Y) --> 1.0 / Y

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Mon Feb 12 11:39:22 PST 2018


Author: spatel
Date: Mon Feb 12 11:39:21 2018
New Revision: 324927

URL: http://llvm.org/viewvc/llvm-project?rev=324927&view=rev
Log:
[InstCombine] X / (X * Y) --> 1.0 / Y

This is similar to the instsimplify fold added with D42385 
( rL323716 )
...but this can't be in instsimplify because we're creating/morphing
a different instruction.

Modified:
    llvm/trunk/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
    llvm/trunk/test/Transforms/InstCombine/fdiv.ll

Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp?rev=324927&r1=324926&r2=324927&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp (original)
+++ llvm/trunk/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp Mon Feb 12 11:39:21 2018
@@ -1523,6 +1523,16 @@ Instruction *InstCombiner::visitFDiv(Bin
     return &I;
   }
 
+  // X / (X * Y) --> 1.0 / Y
+  // Reassociate to (X / X -> 1.0) is legal when NaNs are not allowed.
+  // We can ignore the possibility that X is infinity because INF/INF is NaN.
+  if (I.hasNoNaNs() && I.hasAllowReassoc() &&
+      match(Op1, m_c_FMul(m_Specific(Op0), m_Value(Y)))) {
+    I.setOperand(0, ConstantFP::get(I.getType(), 1.0));
+    I.setOperand(1, Y);
+    return &I;
+  }
+
   return nullptr;
 }
 

Modified: llvm/trunk/test/Transforms/InstCombine/fdiv.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/fdiv.ll?rev=324927&r1=324926&r2=324927&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/fdiv.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/fdiv.ll Mon Feb 12 11:39:21 2018
@@ -83,13 +83,11 @@ define float @fdiv_fneg_fneg_fast(float
   ret float %div
 }
 
-; FIXME: 
 ; X / (X * Y) --> 1.0 / Y
 
 define float @div_factor(float %x, float %y) {
 ; CHECK-LABEL: @div_factor(
-; CHECK-NEXT:    [[M:%.*]] = fmul float [[X:%.*]], [[Y:%.*]]
-; CHECK-NEXT:    [[D:%.*]] = fdiv reassoc nnan float [[X]], [[M]]
+; CHECK-NEXT:    [[D:%.*]] = fdiv reassoc nnan float 1.000000e+00, [[Y:%.*]]
 ; CHECK-NEXT:    ret float [[D]]
 ;
   %m = fmul float %x, %y
@@ -110,14 +108,12 @@ define float @div_factor_too_strict(floa
   ret float %d
 }
 
-; FIXME:
 ; Commute, verify vector types, and show that we are not dropping extra FMF.
 ; X / (Y * X) --> 1.0 / Y
 
 define <2 x float> @div_factor_commute(<2 x float> %x, <2 x float> %y) {
 ; CHECK-LABEL: @div_factor_commute(
-; CHECK-NEXT:    [[M:%.*]] = fmul <2 x float> [[Y:%.*]], [[X:%.*]]
-; CHECK-NEXT:    [[D:%.*]] = fdiv reassoc nnan ninf nsz <2 x float> [[X]], [[M]]
+; CHECK-NEXT:    [[D:%.*]] = fdiv reassoc nnan ninf nsz <2 x float> <float 1.000000e+00, float 1.000000e+00>, [[Y:%.*]]
 ; CHECK-NEXT:    ret <2 x float> [[D]]
 ;
   %m = fmul <2 x float> %y, %x




More information about the llvm-commits mailing list