[llvm] Added fix to enable constant folding that is missing for math library functions (scalbln , scalbn, ldexp) (PR #169893)
Raja Sudha Sri Harika via llvm-commits
llvm-commits at lists.llvm.org
Fri Nov 28 02:15:46 PST 2025
https://github.com/Sriharika1506 created https://github.com/llvm/llvm-project/pull/169893
Issue : #112631
This patch adds support for constant folding for certain math library functions wherever it was previously missing.
>From 17d7b7a97d6589b8f0b52ca9d2827e1b48074f6f Mon Sep 17 00:00:00 2001
From: SriHarika <sudha200215 at gmail.com>
Date: Fri, 28 Nov 2025 15:30:14 +0530
Subject: [PATCH] Added fix to enable constant folding for math library
functions
---
.../llvm/Transforms/Utils/SimplifyLibCalls.h | 3 ++
.../lib/Transforms/Utils/SimplifyLibCalls.cpp | 29 +++++++++++++++++++
2 files changed, 32 insertions(+)
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:
More information about the llvm-commits
mailing list