[llvm] [AArch64] Improve pow(x, y) cost model for some constant values of y (PR #185607)

Paul Walker via llvm-commits llvm-commits at lists.llvm.org
Thu Mar 12 07:26:42 PDT 2026


================
@@ -1109,10 +1109,53 @@ AArch64TTIImpl::getIntrinsicInstrCost(const IntrinsicCostAttributes &ICA,
     }
     break;
   case Intrinsic::pow: {
-    EVT VT = getTLI()->getValueType(DL, RetTy);
-    RTLIB::Libcall LC = RTLIB::getPOW(VT);
-    if (getTLI()->getLibcallImpl(LC) != RTLIB::Unsupported)
-      return getCallInstrCost(nullptr, RetTy, ICA.getArgTypes(), CostKind);
+    // For scalar calls we know the target has the libcall, and for fixed-width
+    // vectors we know for the worst case it can be scalarised.
+    bool CanLowerWithLibcalls = true;
+    bool UsesSingleLibcall = true;
+    if (isa<VectorType>(RetTy)) {
+      EVT VT = getTLI()->getValueType(DL, RetTy);
+      RTLIB::Libcall LC = RTLIB::getPOW(VT);
+      bool HasVectorLibcall =
+          getTLI()->getLibcallImpl(LC) != RTLIB::Unsupported;
+      CanLowerWithLibcalls = isa<FixedVectorType>(RetTy) || HasVectorLibcall;
+      UsesSingleLibcall = HasVectorLibcall;
+    }
+    // If we know that the call can be lowered with libcalls then it's safe to
+    // reduce the costs in some cases. This is important for scalable vectors,
+    // since we cannot scalarize the call in the absence of a vector math
+    // library.
+    if (CanLowerWithLibcalls && ICA.getInst() && !ICA.getArgs().empty()) {
+      // If we know the fast math flags and the exponent is a constant then the
+      // cost may be less for some exponents like 0.25 and 0.75.
+      const Constant *ExpC = dyn_cast<Constant>(ICA.getArgs()[1]);
+      if (ExpC && isa<VectorType>(ExpC->getType()))
+        ExpC = ExpC->getSplatValue();
+      if (ExpC) {
+        // The argument must be a FP constant.
+        const ConstantFP *ExpF = cast<ConstantFP>(ExpC);
----------------
paulwalker-arm wrote:

```suggestion
      if (auto *ExpF = dyn_cast_or_null<ConstantFP>(ExpC)) {
```
I think `ExpC` could be a ConstantExpr.

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


More information about the llvm-commits mailing list