[llvm] [SimplifyLibCalls] Move constant folding logic in `ConstantFoldLibCall2` (PR #172139)
Ron Xavier via llvm-commits
llvm-commits at lists.llvm.org
Tue Sep 1 05:22:04 PDT 2026
https://github.com/Ronxvier updated https://github.com/llvm/llvm-project/pull/172139
>From ae2549c6b069e998021c1f42080640fa38658b46 Mon Sep 17 00:00:00 2001
From: Ron Xavier <Ronelxavi at gmail.com>
Date: Sat, 13 Dec 2025 03:45:49 -0500
Subject: [PATCH 1/7] Resolve #171877
Moved constant folding logic from SimplifyLibCalls.cpp to ConstantFoldLibCall2
---
llvm/lib/Analysis/ConstantFolding.cpp | 12 ++++++++++++
llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp | 14 ++------------
2 files changed, 14 insertions(+), 12 deletions(-)
diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp
index a9b51065a1d99..8a73c02bc363e 100644
--- a/llvm/lib/Analysis/ConstantFolding.cpp
+++ b/llvm/lib/Analysis/ConstantFolding.cpp
@@ -3241,6 +3241,18 @@ static Constant *ConstantFoldLibCall2(StringRef Name, Type *Ty,
if (TLI->has(Func))
return ConstantFoldBinaryFP(atan2, Op1V, Op2V, Ty);
break;
+ case LibFunc_fdim:
+ case LibFunc_fdimf:
+ case LibFunc_fdiml:
+ if (TLI->has(Func)){
+ APFloat Difference = Op1V;
+ Difference.subtract(Op2V, RoundingMode::NearestTiesToEven);
+
+ APFloat MaxVal =
+ maximum(Difference, APFloat::getZero(Ty->getFltSemantics()));
+ return ConstantFP::get(Ty->getContext(), MaxVal);
+ }
+ break;
}
return nullptr;
diff --git a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
index c3537f544c432..55cd08b3de855 100644
--- a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
@@ -3201,18 +3201,8 @@ Value *LibCallSimplifier::optimizeFdim(CallInst *CI, IRBuilderBase &B) {
if (isa<PoisonValue>(CI->getArgOperand(1)))
return CI->getArgOperand(1);
- const APFloat *X, *Y;
- // Check if both values are constants
- if (!match(CI->getArgOperand(0), m_APFloat(X)) ||
- !match(CI->getArgOperand(1), m_APFloat(Y)))
- return nullptr;
-
- APFloat Difference = *X;
- Difference.subtract(*Y, RoundingMode::NearestTiesToEven);
-
- APFloat MaxVal =
- maximum(Difference, APFloat::getZero(CI->getType()->getFltSemantics()));
- return ConstantFP::get(CI->getType(), MaxVal);
+ // Constant folding will be handled by ConstantFoldLibCall2
+ return nullptr;
}
//===----------------------------------------------------------------------===//
>From 9f6988ac52d3f3332f3582a0360a728b4373487c Mon Sep 17 00:00:00 2001
From: Ron Xavier <Ronelxavi at gmail.com>
Date: Sat, 13 Dec 2025 06:22:09 -0500
Subject: [PATCH 2/7] reformatted
---
llvm/lib/Analysis/ConstantFolding.cpp | 16 ++++++++--------
1 file changed, 8 insertions(+), 8 deletions(-)
diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp
index 8a73c02bc363e..901f0738cc6d4 100644
--- a/llvm/lib/Analysis/ConstantFolding.cpp
+++ b/llvm/lib/Analysis/ConstantFolding.cpp
@@ -3244,15 +3244,15 @@ static Constant *ConstantFoldLibCall2(StringRef Name, Type *Ty,
case LibFunc_fdim:
case LibFunc_fdimf:
case LibFunc_fdiml:
- if (TLI->has(Func)){
- APFloat Difference = Op1V;
- Difference.subtract(Op2V, RoundingMode::NearestTiesToEven);
+ if (TLI->has(Func)){
+ APFloat Difference = Op1V;
+ Difference.subtract(Op2V, RoundingMode::NearestTiesToEven);
- APFloat MaxVal =
- maximum(Difference, APFloat::getZero(Ty->getFltSemantics()));
- return ConstantFP::get(Ty->getContext(), MaxVal);
- }
- break;
+ APFloat MaxVal =
+ maximum(Difference, APFloat::getZero(Ty->getFltSemantics()));
+ return ConstantFP::get(Ty->getContext(), MaxVal);
+ }
+ break;
}
return nullptr;
>From 5d9b081df2966fe8064355d03d8cf0f491a08ccd Mon Sep 17 00:00:00 2001
From: Ron Xavier <Ronelxavi at gmail.com>
Date: Sat, 13 Dec 2025 13:21:01 -0500
Subject: [PATCH 3/7] Update ConstantFolding.cpp
Might be an overguarding issue
---
llvm/lib/Analysis/ConstantFolding.cpp | 12 +++++-------
1 file changed, 5 insertions(+), 7 deletions(-)
diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp
index 901f0738cc6d4..dd4a3abf8c89d 100644
--- a/llvm/lib/Analysis/ConstantFolding.cpp
+++ b/llvm/lib/Analysis/ConstantFolding.cpp
@@ -3244,14 +3244,12 @@ static Constant *ConstantFoldLibCall2(StringRef Name, Type *Ty,
case LibFunc_fdim:
case LibFunc_fdimf:
case LibFunc_fdiml:
- if (TLI->has(Func)){
- APFloat Difference = Op1V;
- Difference.subtract(Op2V, RoundingMode::NearestTiesToEven);
+ APFloat Difference = Op1V;
+ Difference.subtract(Op2V, RoundingMode::NearestTiesToEven);
- APFloat MaxVal =
- maximum(Difference, APFloat::getZero(Ty->getFltSemantics()));
- return ConstantFP::get(Ty->getContext(), MaxVal);
- }
+ APFloat MaxVal =
+ maximum(Difference, APFloat::getZero(Ty->getFltSemantics()));
+ return ConstantFP::get(Ty->getContext(), MaxVal);
break;
}
>From 1bebb8cffb176cac697cf876adf655e2f6754618 Mon Sep 17 00:00:00 2001
From: Ron Xavier <Ronelxavi at gmail.com>
Date: Mon, 15 Dec 2025 12:33:52 -0500
Subject: [PATCH 4/7] remove poison handling
---
llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp | 5 -----
1 file changed, 5 deletions(-)
diff --git a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
index 55cd08b3de855..2ca2986567180 100644
--- a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
@@ -3195,11 +3195,6 @@ Value *LibCallSimplifier::optimizeFdim(CallInst *CI, IRBuilderBase &B) {
return nullptr;
// TODO : Handle undef values
- // Propagate poison if any
- if (isa<PoisonValue>(CI->getArgOperand(0)))
- return CI->getArgOperand(0);
- if (isa<PoisonValue>(CI->getArgOperand(1)))
- return CI->getArgOperand(1);
// Constant folding will be handled by ConstantFoldLibCall2
return nullptr;
>From d3d4d7a02bf4b084cc22cd446ccb57b9dc17f851 Mon Sep 17 00:00:00 2001
From: Ron Xavier <Ronelxavi at gmail.com>
Date: Tue, 16 Dec 2025 01:32:16 +0530
Subject: [PATCH 5/7] Update llvm/lib/Analysis/ConstantFolding.cpp
remove break that isn't reached
Co-authored-by: Antonio Frighetto <me at antoniofrighetto.com>
---
llvm/lib/Analysis/ConstantFolding.cpp | 1 -
1 file changed, 1 deletion(-)
diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp
index dd4a3abf8c89d..7877cd0276e04 100644
--- a/llvm/lib/Analysis/ConstantFolding.cpp
+++ b/llvm/lib/Analysis/ConstantFolding.cpp
@@ -3250,7 +3250,6 @@ static Constant *ConstantFoldLibCall2(StringRef Name, Type *Ty,
APFloat MaxVal =
maximum(Difference, APFloat::getZero(Ty->getFltSemantics()));
return ConstantFP::get(Ty->getContext(), MaxVal);
- break;
}
return nullptr;
>From 0d14107fe2f54bfc3809773c8b49c618127cb0c4 Mon Sep 17 00:00:00 2001
From: Ron Xavier <Ronelxavi at gmail.com>
Date: Wed, 24 Dec 2025 22:17:29 -0500
Subject: [PATCH 6/7] Remove function + formatting
---
llvm/lib/Analysis/ConstantFolding.cpp | 2 +-
llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp | 12 ------------
2 files changed, 1 insertion(+), 13 deletions(-)
diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp
index dd4a3abf8c89d..8d8d011ee7f7c 100644
--- a/llvm/lib/Analysis/ConstantFolding.cpp
+++ b/llvm/lib/Analysis/ConstantFolding.cpp
@@ -3248,7 +3248,7 @@ static Constant *ConstantFoldLibCall2(StringRef Name, Type *Ty,
Difference.subtract(Op2V, RoundingMode::NearestTiesToEven);
APFloat MaxVal =
- maximum(Difference, APFloat::getZero(Ty->getFltSemantics()));
+ maximum(Difference, APFloat::getZero(Ty->getFltSemantics()));
return ConstantFP::get(Ty->getContext(), MaxVal);
break;
}
diff --git a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
index 2ca2986567180..665abc6007e88 100644
--- a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
@@ -3188,18 +3188,6 @@ Value *LibCallSimplifier::optimizeRemquo(CallInst *CI, IRBuilderBase &B) {
return ConstantFP::get(CI->getType(), Rem);
}
-/// Constant folds fdim
-Value *LibCallSimplifier::optimizeFdim(CallInst *CI, IRBuilderBase &B) {
- // Cannot perform the fold unless the call has attribute memory(none)
- if (!CI->doesNotAccessMemory())
- return nullptr;
-
- // TODO : Handle undef values
-
- // Constant folding will be handled by ConstantFoldLibCall2
- return nullptr;
-}
-
//===----------------------------------------------------------------------===//
// Integer Library Call Optimizations
//===----------------------------------------------------------------------===//
>From 515beb11c3a7c52d42943019f72ce4e0d8a878f9 Mon Sep 17 00:00:00 2001
From: Ron Xavier <Ronelxavi at gmail.com>
Date: Mon, 31 Aug 2026 16:28:26 -0400
Subject: [PATCH 7/7] Constant-fold fdim in ConstantFolding
Moved constant folding logic from SimplifyLibCalls.cpp to ConstantFoldLibCall2.
Meant to resolve #171877
---
.../llvm/Transforms/Utils/SimplifyLibCalls.h | 1 -
llvm/lib/Analysis/ConstantFolding.cpp | 20 +++++++++++-
.../lib/Transforms/Utils/SimplifyLibCalls.cpp | 31 -------------------
3 files changed, 19 insertions(+), 33 deletions(-)
diff --git a/llvm/include/llvm/Transforms/Utils/SimplifyLibCalls.h b/llvm/include/llvm/Transforms/Utils/SimplifyLibCalls.h
index 7298ad459a5da..f080f0f6fa8b8 100644
--- a/llvm/include/llvm/Transforms/Utils/SimplifyLibCalls.h
+++ b/llvm/include/llvm/Transforms/Utils/SimplifyLibCalls.h
@@ -212,7 +212,6 @@ class LibCallSimplifier {
Value *optimizeTrigInversionPairs(CallInst *CI, IRBuilderBase &B);
Value *optimizeSymmetric(CallInst *CI, LibFunc Func, IRBuilderBase &B);
Value *optimizeRemquo(CallInst *CI, IRBuilderBase &B);
- Value *optimizeFdim(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/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp
index c819e188cac95..c3a0a07180900 100644
--- a/llvm/lib/Analysis/ConstantFolding.cpp
+++ b/llvm/lib/Analysis/ConstantFolding.cpp
@@ -2144,6 +2144,7 @@ bool llvm::canConstantFoldCallTo(const CallBase *Call, const Function *F) {
Name == "exp2f" || Name == "erf" || Name == "erff";
case 'f':
return Name == "fabs" || Name == "fabsf" ||
+ Name == "fdim" || Name == "fdimf" || Name == "fdiml" ||
Name == "floor" || Name == "floorf" ||
Name == "fmod" || Name == "fmodf";
case 'i':
@@ -3391,6 +3392,20 @@ static Constant *ConstantFoldLibCall2(StringRef Name, Type *Ty,
if (TLI->has(Func))
return ConstantFoldBinaryFP(pow, Op1V, Op2V, Ty);
break;
+ case LibFunc_fdim:
+ case LibFunc_fdimf:
+ case LibFunc_fdiml:
+ if (TLI->has(Func)) {
+ // C99 fdim(x, y) = (x > y) ? x - y : +0.
+ if (!Op1V.isNaN() && !Op2V.isNaN() &&
+ Op1V.compare(Op2V) != APFloat::cmpGreaterThan){
+ return ConstantFP::getZero(Ty);
+ }
+ APFloat Difference = Op1V;
+ Difference.subtract(Op2V, RoundingMode::NearestTiesToEven);
+ return ConstantFP::get(Ty, Difference);
+ }
+ break;
case LibFunc_fmod:
case LibFunc_fmodf:
if (TLI->has(Func)) {
@@ -4906,7 +4921,10 @@ bool llvm::isMathLibCallNoop(const CallBase *Call,
}
break;
}
-
+ case LibFunc_fdim:
+ case LibFunc_fdimf:
+ case LibFunc_fdiml:
+ return true;
case LibFunc_fmodl:
case LibFunc_fmod:
case LibFunc_fmodf:
diff --git a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
index 1c92409b3379b..a9599abbc5c0e 100644
--- a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
@@ -3265,33 +3265,6 @@ Value *LibCallSimplifier::optimizeRemquo(CallInst *CI, IRBuilderBase &B) {
return ConstantFP::get(CI->getType(), Rem);
}
-/// Constant folds fdim
-Value *LibCallSimplifier::optimizeFdim(CallInst *CI, IRBuilderBase &B) {
- // Cannot perform the fold unless the call has attribute memory(none)
- if (!CI->doesNotAccessMemory())
- return nullptr;
-
- // TODO : Handle undef values
- // Propagate poison if any
- if (isa<PoisonValue>(CI->getArgOperand(0)))
- return CI->getArgOperand(0);
- if (isa<PoisonValue>(CI->getArgOperand(1)))
- return CI->getArgOperand(1);
-
- const APFloat *X, *Y;
- // Check if both values are constants
- if (!match(CI->getArgOperand(0), m_APFloat(X)) ||
- !match(CI->getArgOperand(1), m_APFloat(Y)))
- return nullptr;
-
- // C99 fdim(x, y) = (x > y) ? x - y : +0.
- if (X->compare(*Y) != APFloat::cmpGreaterThan && !X->isNaN() && !Y->isNaN())
- return ConstantFP::getZero(CI->getType());
- APFloat Difference = *X;
- Difference.subtract(*Y, RoundingMode::NearestTiesToEven);
- return ConstantFP::get(CI->getType(), Difference);
-}
-
//===----------------------------------------------------------------------===//
// Integer Library Call Optimizations
//===----------------------------------------------------------------------===//
@@ -4235,10 +4208,6 @@ Value *LibCallSimplifier::optimizeFloatingPointLibCall(CallInst *CI,
if (hasFloatVersion(M, CI->getCalledFunction()->getName()))
return optimizeBinaryDoubleFP(CI, Builder, TLI);
return nullptr;
- case LibFunc_fdim:
- case LibFunc_fdimf:
- case LibFunc_fdiml:
- return optimizeFdim(CI, Builder);
case LibFunc_fminf:
case LibFunc_fmin:
case LibFunc_fminl:
More information about the llvm-commits
mailing list