aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/Analysis/ConstantFolding.cpp
diff options
context:
space:
mode:
authorc8ef <c8ef@outlook.com>2024-11-01 09:07:55 +0800
committerGitHub <noreply@github.com>2024-11-01 09:07:55 +0800
commit1f07f995cc994dfb46b65fe97986efca15cf304b (patch)
tree6e1e7c66768ae883cd899a38222290afeb35c1b8 /llvm/lib/Analysis/ConstantFolding.cpp
parentda9788359d35f4294bc6ec5323751e40981cdbe0 (diff)
downloadllvm-1f07f995cc994dfb46b65fe97986efca15cf304b.zip
llvm-1f07f995cc994dfb46b65fe97986efca15cf304b.tar.gz
llvm-1f07f995cc994dfb46b65fe97986efca15cf304b.tar.bz2
[ConstantFold] Fold `tgamma` and `tgammaf` when the input parameter is a constant value. (#114065)
This patch adds support for constant folding for the `tgamma` and `tgammaf` libc functions.
Diffstat (limited to 'llvm/lib/Analysis/ConstantFolding.cpp')
-rw-r--r--llvm/lib/Analysis/ConstantFolding.cpp19
1 files changed, 16 insertions, 3 deletions
diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp
index c5a2c2f..a96c3be 100644
--- a/llvm/lib/Analysis/ConstantFolding.cpp
+++ b/llvm/lib/Analysis/ConstantFolding.cpp
@@ -57,6 +57,7 @@
#include <cassert>
#include <cerrno>
#include <cfenv>
+#include <cfloat>
#include <cmath>
#include <cstdint>
@@ -1698,9 +1699,9 @@ bool llvm::canConstantFoldCallTo(const CallBase *Call, const Function *F) {
Name == "sinh" || Name == "sinhf" ||
Name == "sqrt" || Name == "sqrtf";
case 't':
- return Name == "tan" || Name == "tanf" ||
- Name == "tanh" || Name == "tanhf" ||
- Name == "trunc" || Name == "truncf";
+ return Name == "tan" || Name == "tanf" || Name == "tanh" ||
+ Name == "tanhf" || Name == "trunc" || Name == "truncf" ||
+ Name == "tgamma" || Name == "tgammaf";
case '_':
// Check for various function names that get used for the math functions
// when the header files are preprocessed with the macro
@@ -2417,6 +2418,14 @@ static Constant *ConstantFoldScalarCall1(StringRef Name,
if (TLI->has(Func))
return ConstantFoldFP(erf, APF, Ty);
break;
+ case LibFunc_tgamma:
+ case LibFunc_tgammaf:
+ // NOTE: These boundaries are somewhat conservative.
+ if (TLI->has(Func) &&
+ (Ty->isDoubleTy() && APF > APFloat(DBL_MIN) && APF < APFloat(171.0) ||
+ Ty->isFloatTy() && APF > APFloat(FLT_MIN) && APF < APFloat(35.0f)))
+ return ConstantFoldFP(tgamma, APF, Ty);
+ break;
case LibFunc_nearbyint:
case LibFunc_nearbyintf:
case LibFunc_rint:
@@ -3629,6 +3638,10 @@ bool llvm::isMathLibCallNoop(const CallBase *Call,
case LibFunc_sqrtf:
return Op.isNaN() || Op.isZero() || !Op.isNegative();
+ case LibFunc_tgamma:
+ case LibFunc_tgammaf:
+ return true;
+
// FIXME: Add more functions: sqrt_finite, atanh, expm1, log1p,
// maybe others?
default: