[llvm] Added fix to enable constant folding that is missing for math library functions (scalbln , scalbn, ldexp) (PR #169893)
via llvm-commits
llvm-commits at lists.llvm.org
Fri Nov 28 02:16:42 PST 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-transforms
Author: Raja Sudha Sri Harika (Sriharika1506)
<details>
<summary>Changes</summary>
Issue : #<!-- -->112631
This patch adds support for constant folding for certain math library functions wherever it was previously missing.
---
Full diff: https://github.com/llvm/llvm-project/pull/169893.diff
2 Files Affected:
- (modified) llvm/include/llvm/Transforms/Utils/SimplifyLibCalls.h (+3)
- (modified) llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp (+29)
``````````diff
diff --git a/llvm/include/llvm/Transforms/Utils/SimplifyLibCalls.h b/llvm/include/llvm/Transforms/Utils/SimplifyLibCalls.h
index 4e7c97194cc59..72b8f4b427b3b 100644
--- a/llvm/include/llvm/Transforms/Utils/SimplifyLibCalls.h
+++ b/llvm/include/llvm/Transforms/Utils/SimplifyLibCalls.h
@@ -214,6 +214,9 @@ class LibCallSimplifier {
Value *optimizeSymmetric(CallInst *CI, LibFunc Func, IRBuilderBase &B);
Value *optimizeRemquo(CallInst *CI, IRBuilderBase &B);
Value *optimizeFdim(CallInst *CI, IRBuilderBase &B);
+
+ Value *foldLdexp(CallInst *CI, IRBuilderBase &B);
+
// Wrapper for all floating point library call optimizations
Value *optimizeFloatingPointLibCall(CallInst *CI, LibFunc Func,
IRBuilderBase &B);
diff --git a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
index 4a1565977b91c..7508048d3032e 100644
--- a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
@@ -2464,6 +2464,24 @@ Value *LibCallSimplifier::optimizePow(CallInst *Pow, IRBuilderBase &B) {
return nullptr;
}
+Value *LibCallSimplifier::foldLdexp(CallInst *CI,IRBuilderBase &B) {
+
+ Type*RequiredType = CI->getType();
+ if(!RequiredType->isFPOrFPVectorTy())
+ return nullptr;
+
+ Value*x = CI->getArgOperand(0);
+ Value*exp = CI->getArgOperand(1);
+ Module*M = CI->getModule();
+
+ Function*ldexpDecl = llvm::Intrinsic::getOrInsertDeclaration(M, Intrinsic::ldexp,{RequiredType, exp->getType()});
+
+ CallInst *NewCall = B.CreateCall(ldexpDecl, {x, exp}, CI->getName());
+ NewCall->setAttributes(CI->getAttributes());
+ return copyFlags(*CI, NewCall);
+}
+
+
Value *LibCallSimplifier::optimizeExp2(CallInst *CI, IRBuilderBase &B) {
Module *M = CI->getModule();
Function *Callee = CI->getCalledFunction();
@@ -4031,6 +4049,17 @@ Value *LibCallSimplifier::optimizeFloatingPointLibCall(CallInst *CI,
case LibFunc_exp2:
case LibFunc_exp2f:
return optimizeExp2(CI, Builder);
+ // New ldexp / scalbn / scalbln family:
+ case LibFunc_ldexp:
+ case LibFunc_ldexpf:
+ case LibFunc_ldexpl:
+ case LibFunc_scalbn:
+ case LibFunc_scalbnf:
+ case LibFunc_scalbnl:
+ case LibFunc_scalbln:
+ case LibFunc_scalblnf:
+ case LibFunc_scalblnl:
+ return foldLdexp(CI, Builder);
case LibFunc_fabsf:
case LibFunc_fabs:
case LibFunc_fabsl:
``````````
</details>
https://github.com/llvm/llvm-project/pull/169893
More information about the llvm-commits
mailing list