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

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Wed Jan 3 08:06:35 PST 2024


================
@@ -2495,13 +2495,21 @@ 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);
+    // Cast exponent to FP if integer.
+    if (ArgID == Intrinsic::powi) {
+      if (ConstantInt *C = dyn_cast<ConstantInt>(Y))
+        Y = ConstantFoldCastOperand(Instruction::SIToFP, C, Ty, DL);
+      else
+        Y = B.CreateCast(Instruction::SIToFP, Y, Ty, "cast");
----------------
dtcxzyw wrote:

```suggestion
        Y = B.CreateCast(Instruction::SIToFP, Y, Ty, "cast");
```
`IRBuilder` will handle the constant folding.
https://github.com/llvm/llvm-project/blob/21fe8b635cfb04de486a3c4ca22eb9f5a5ed78ad/llvm/include/llvm/IR/IRBuilder.h#L2138-L2139


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


More information about the llvm-commits mailing list