[llvm] Resolving #171877 (PR #172139)
Ron Xavier via llvm-commits
llvm-commits at lists.llvm.org
Sat Dec 13 20:32:21 PST 2025
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/3] 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/3] 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/3] 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;
}
More information about the llvm-commits
mailing list