[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:

```suggestion
      Y = ConstantFoldCastOperand(Instruction::SIToFP, C, Log->getType(), DL);
      if (!Y) return nullptr;
```
It would be better than `C->getSExtValue()` since it can handle integers wider than 64 bits.


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


More information about the llvm-commits mailing list