[llvm] [InstCombine] optimize powi(X,Y) * X with Ofast (PR #69998)

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 5 03:34:08 PST 2024


================
@@ -571,6 +571,50 @@ Instruction *InstCombinerImpl::foldFPSignBitOps(BinaryOperator &I) {
   return nullptr;
 }
 
+Instruction *InstCombinerImpl::foldPowiReassoc(BinaryOperator &I) {
+  Value *X, *Y, *Z;
+
+  auto createPowiExpr = [](BinaryOperator &I, InstCombinerImpl &IC, Value *X,
+                           Value *Y, Value *Z) {
+    InstCombiner::BuilderTy &Builder = IC.Builder;
+
+    Value *YZ = Builder.CreateAdd(Y, Z);
+    auto *NewPow = Builder.CreateIntrinsic(
+        Intrinsic::powi, {X->getType(), YZ->getType()}, {X, YZ}, &I);
+    return IC.replaceInstUsesWith(I, NewPow);
+  };
+
+  // powi(X, Y) * X --> powi(X, Y+1)
+  // X * powi(X, Y) --> powi(X, Y+1)
+  if (match(&I, m_c_FMul(m_OneUse(m_Intrinsic<Intrinsic::powi>(m_Value(X),
+                                                               m_Value(Y))),
+                         m_Deferred(X)))) {
+    Constant *One = ConstantInt::get(Y->getType(), 1);
+    if (willNotOverflowSignedAdd(Y, One, I)) {
+      auto *Powi = dyn_cast<IntrinsicInst>(I.getOperand(0));
+      if (!Powi)
+        Powi = cast<IntrinsicInst>(I.getOperand(1));
+      if (Powi->hasAllowReassoc())
----------------
arsenm wrote:

We should really have a way to put this flag check inside the matcher 

https://github.com/llvm/llvm-project/pull/69998


More information about the llvm-commits mailing list