[InstCombine] fold pow(X,Y) * X -> pow(X, Y+1) (with fast-math)
authorSanjay Patel <spatel@rotateright.com>
Fri, 13 Jan 2023 20:31:14 +0000 (15:31 -0500)
committerSanjay Patel <spatel@rotateright.com>
Fri, 13 Jan 2023 22:13:46 +0000 (17:13 -0500)
This is one of the patterns suggested in issue #34943.

llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
llvm/test/Transforms/InstCombine/fmul-pow.ll

index 6ce3939..d685dd0 100644 (file)
@@ -661,6 +661,17 @@ Instruction *InstCombinerImpl::visitFMul(BinaryOperator &I) {
       }
     }
 
+    // pow(X, Y) * X --> pow(X, Y+1)
+    // X * pow(X, Y) --> pow(X, Y+1)
+    if (match(&I, m_c_FMul(m_OneUse(m_Intrinsic<Intrinsic::pow>(m_Value(X),
+                                                                m_Value(Y))),
+                           m_Deferred(X)))) {
+      Value *Y1 =
+          Builder.CreateFAddFMF(Y, ConstantFP::get(I.getType(), 1.0), &I);
+      Value *Pow = Builder.CreateBinaryIntrinsic(Intrinsic::pow, X, Y1, &I);
+      return replaceInstUsesWith(I, Pow);
+    }
+
     if (I.isOnlyUserOfAnyOperand()) {
       // pow(X, Y) * pow(X, Z) -> pow(X, Y + Z)
       if (match(Op0, m_Intrinsic<Intrinsic::pow>(m_Value(X), m_Value(Y))) &&
index e1f31d8..5984641 100644 (file)
@@ -4,6 +4,9 @@
 declare double @llvm.pow.f64(double, double)
 declare void @use(double)
 
+; negative test for:
+; pow(a,b) * a --> pow(a, b+1) (requires reassoc)
+
 define double @pow_ab_a(double %a, double %b)  {
 ; CHECK-LABEL: @pow_ab_a(
 ; CHECK-NEXT:    [[P:%.*]] = call double @llvm.pow.f64(double [[A:%.*]], double [[B:%.*]])
@@ -15,10 +18,12 @@ define double @pow_ab_a(double %a, double %b)  {
   ret double %m
 }
 
+; pow(a,b) * a --> pow(a, b+1)
+
 define double @pow_ab_a_reassoc(double %a, double %b)  {
 ; CHECK-LABEL: @pow_ab_a_reassoc(
-; CHECK-NEXT:    [[P:%.*]] = call double @llvm.pow.f64(double [[A:%.*]], double [[B:%.*]])
-; CHECK-NEXT:    [[M:%.*]] = fmul reassoc double [[P]], [[A]]
+; CHECK-NEXT:    [[TMP1:%.*]] = fadd reassoc double [[B:%.*]], 1.000000e+00
+; CHECK-NEXT:    [[M:%.*]] = call reassoc double @llvm.pow.f64(double [[A:%.*]], double [[TMP1]])
 ; CHECK-NEXT:    ret double [[M]]
 ;
   %p = call double @llvm.pow.f64(double %a, double %b)
@@ -26,11 +31,13 @@ define double @pow_ab_a_reassoc(double %a, double %b)  {
   ret double %m
 }
 
+; a * pow(a,b) --> pow(a, b+1)
+
 define double @pow_ab_a_reassoc_commute(double %pa, double %b)  {
 ; CHECK-LABEL: @pow_ab_a_reassoc_commute(
 ; CHECK-NEXT:    [[A:%.*]] = fadd double [[PA:%.*]], 4.200000e+01
-; CHECK-NEXT:    [[P:%.*]] = call double @llvm.pow.f64(double [[A]], double [[B:%.*]])
-; CHECK-NEXT:    [[M:%.*]] = fmul reassoc double [[A]], [[P]]
+; CHECK-NEXT:    [[TMP1:%.*]] = fadd reassoc double [[B:%.*]], 1.000000e+00
+; CHECK-NEXT:    [[M:%.*]] = call reassoc double @llvm.pow.f64(double [[A]], double [[TMP1]])
 ; CHECK-NEXT:    ret double [[M]]
 ;
   %a = fadd double %pa, 42.0 ; thwart complexity-based canonicalization
@@ -39,6 +46,8 @@ define double @pow_ab_a_reassoc_commute(double %pa, double %b)  {
   ret double %m
 }
 
+; negative test - extra uses not allowed
+
 define double @pow_ab_a_reassoc_use(double %a, double %b)  {
 ; CHECK-LABEL: @pow_ab_a_reassoc_use(
 ; CHECK-NEXT:    [[P:%.*]] = call double @llvm.pow.f64(double [[A:%.*]], double [[B:%.*]])