[llvm] [InstCombine] Add log-pow simplification for FP exponent edge case. (PR #76641)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Sat Dec 30 16:11:22 PST 2023


================
@@ -2495,13 +2495,17 @@ Value *LibCallSimplifier::optimizeLog(CallInst *Log, IRBuilderBase &B) {
 
   // log(pow(x,y)) -> y*log(x)
   AttributeList NoAttrs;
-  if (ArgLb == PowLb || ArgID == Intrinsic::pow) {
+  if (ArgLb == PowLb || ArgID == Intrinsic::pow || ArgID == Intrinsic::powi) {
     Value *LogX =
         Log->doesNotAccessMemory()
             ? B.CreateCall(Intrinsic::getDeclaration(Mod, LogID, Ty),
                            Arg->getOperand(0), "log")
             : emitUnaryFloatFnCall(Arg->getOperand(0), TLI, LogNm, B, NoAttrs);
-    Value *MulY = B.CreateFMul(Arg->getArgOperand(1), LogX, "mul");
+    Value *Y = Arg->getArgOperand(1);
+    // If power is integer constant, convert to FP for FMul.
+    if (ConstantInt *C = dyn_cast<ConstantInt>(Y))
+      Y = ConstantFP::get(Ty, C->getSExtValue());
----------------
dtcxzyw wrote:

> @dtcxzyw I did a standard sitofp initially, but that adds a casting instruction right? In the compiled asm there's an addition `scvtf` if using a non-constant cast.

GCC doesn't apply the transform if `y` is not a constant: https://godbolt.org/z/qTd6Tnh3c
If the `powi` is only used by the `log`, `sitofp + fmul` is definitely cheaper than a call to `pow`.


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


More information about the llvm-commits mailing list