[llvm] [ConstantFolding]Fold llvm.powi via square-and-multiply (PR #210771)

Alexey Bataev via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 20 10:52:25 PDT 2026


https://github.com/alexey-bataev created https://github.com/llvm/llvm-project/pull/210771

Compute constant-folded powi using APFloat multiplies in the operand's
own semantics (square-and-multiply), matching the expansion
ExpandPowI generates in SelectionDAGBuilder, instead of routing
through std::pow via double/float conversion.


>From 34e6be24b2683164aa18cbbeecbf5feadaaf9e8f Mon Sep 17 00:00:00 2001
From: Alexey Bataev <a.bataev at outlook.com>
Date: Mon, 20 Jul 2026 10:52:04 -0700
Subject: [PATCH] =?UTF-8?q?[=F0=9D=98=80=F0=9D=97=BD=F0=9D=97=BF]=20initia?=
 =?UTF-8?q?l=20version?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Created using spr 1.3.7
---
 llvm/lib/Analysis/ConstantFolding.cpp         | 31 ++++++++++---------
 .../InstSimplify/fold-intrinsics.ll           |  8 +++++
 2 files changed, 24 insertions(+), 15 deletions(-)

diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp
index 1d17f6a6cc2c7..8901a5384e37e 100644
--- a/llvm/lib/Analysis/ConstantFolding.cpp
+++ b/llvm/lib/Analysis/ConstantFolding.cpp
@@ -3742,23 +3742,24 @@ static Constant *ConstantFoldIntrinsicCall2(Intrinsic::ID IntrinsicID, Type *Ty,
         return ConstantInt::get(Ty, Result);
       }
       case Intrinsic::powi: {
+        // Square-and-multiply using the operand's own semantics, matching
+        // the multiply sequence ExpandPowI builds in SelectionDAG.
         int Exp = static_cast<int>(Op2C->getSExtValue());
-        switch (Ty->getTypeID()) {
-        case Type::HalfTyID:
-        case Type::FloatTyID: {
-          APFloat Res(static_cast<float>(std::pow(Op1V.convertToFloat(), Exp)));
-          if (Ty->isHalfTy()) {
-            bool Unused;
-            Res.convert(APFloat::IEEEhalf(), APFloat::rmNearestTiesToEven,
-                        &Unused);
-          }
-          return ConstantFP::get(Ty, Res);
-        }
-        case Type::DoubleTyID:
-          return ConstantFP::get(Ty, std::pow(Op1V.convertToDouble(), Exp));
-        default:
-          return nullptr;
+        unsigned UExp = static_cast<unsigned>(Exp);
+        if (Exp < 0)
+          UExp = -UExp;
+        const fltSemantics &Semantics = Op1V.getSemantics();
+        APFloat Res = APFloat::getOne(Semantics);
+        APFloat CurSquare = Op1V;
+        while (UExp) {
+          if (UExp & 1)
+            Res = Res * CurSquare;
+          CurSquare = CurSquare * CurSquare;
+          UExp >>= 1;
         }
+        if (Exp < 0)
+          Res = APFloat::getOne(Semantics) / Res;
+        return ConstantFP::get(Ty, Res);
       }
       default:
         break;
diff --git a/llvm/test/Transforms/InstSimplify/fold-intrinsics.ll b/llvm/test/Transforms/InstSimplify/fold-intrinsics.ll
index bec8a8bd34c7d..6d8967432e82e 100644
--- a/llvm/test/Transforms/InstSimplify/fold-intrinsics.ll
+++ b/llvm/test/Transforms/InstSimplify/fold-intrinsics.ll
@@ -45,6 +45,14 @@ define void @powi_i16(float %V, ptr%P) {
   ret void
 }
 
+define float @powi_const_fold() {
+; CHECK-LABEL: @powi_const_fold(
+; CHECK-NEXT:    ret float f0x41E8FBBA
+;
+  %r = tail call float @llvm.powi.f32.i16(float 1.234567, i16 16) nounwind
+  ret float %r
+}
+
 define i32 @test_ctpop_poison(i32 %a) {
 ; CHECK-LABEL: @test_ctpop_poison(
 ; CHECK-NEXT:    ret i32 poison



More information about the llvm-commits mailing list