[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 15:55:26 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:

If the `exponent` is not a constant, you will create an illegal fmul with an integer LHS.
Please add the following negative test and pre-commit these tests in a separate commit.
```
define double @log_pow_nonconst(double %x, i32 %y) {
  %pow = call fast double @llvm.powi.f64.i32(double %x, i32 %y)
  %log = call fast double @log(double %pow)
  ret double %log
}
```

Another question: why don't we fold `log(powi(x,y)) -> (sitofp y) * log(x)` even if `Y` is not a constant? I think it is also profitable if `powi` is only used by the `log`.


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


More information about the llvm-commits mailing list