Skip to content

Commit 8f502a7

Browse files
committed
[SimplifyLibCalls] Constant fold fdim
Signed-off-by: Kushal Pal <[email protected]>
1 parent 29168e8 commit 8f502a7

File tree

3 files changed

+121
-0
lines changed

3 files changed

+121
-0
lines changed

llvm/include/llvm/Transforms/Utils/SimplifyLibCalls.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ class LibCallSimplifier {
212212
Value *optimizeTrigInversionPairs(CallInst *CI, IRBuilderBase &B);
213213
Value *optimizeSymmetric(CallInst *CI, LibFunc Func, IRBuilderBase &B);
214214
Value *optimizeRemquo(CallInst *CI, IRBuilderBase &B);
215+
Value *optimizeFdim(CallInst *CI, IRBuilderBase &B);
215216
// Wrapper for all floating point library call optimizations
216217
Value *optimizeFloatingPointLibCall(CallInst *CI, LibFunc Func,
217218
IRBuilderBase &B);

llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3109,6 +3109,33 @@ Value *LibCallSimplifier::optimizeRemquo(CallInst *CI, IRBuilderBase &B) {
31093109
return ConstantFP::get(CI->getType(), Rem);
31103110
}
31113111

3112+
/// Constant folds fdim
3113+
Value *LibCallSimplifier::optimizeFdim(CallInst *CI, IRBuilderBase &B) {
3114+
const APFloat *X, *Y;
3115+
// Check if both values are constants
3116+
if (!match(CI->getArgOperand(0), m_APFloat(X)) ||
3117+
!match(CI->getArgOperand(1), m_APFloat(Y)))
3118+
return nullptr;
3119+
// If either argument is NaN, NaN is returned
3120+
if (X->isNaN() || Y->isNaN())
3121+
return ConstantFP::getQNaN(CI->getType());
3122+
3123+
IRBuilderBase::FastMathFlagGuard Guard(B);
3124+
FastMathFlags FMF = CI->getFastMathFlags();
3125+
// set no-NaN fast-math-flag as we already checked for NaN for both operands
3126+
FMF.setNoNaNs();
3127+
// set no-signed-zeroes as fdim will never return -0.0
3128+
FMF.setNoSignedZeros();
3129+
B.setFastMathFlags(FMF);
3130+
// fdim is equivalent to fmax(x - y, 0), except for the NaN handling
3131+
// requirements.
3132+
Intrinsic::ID IID = Intrinsic::maxnum;
3133+
return copyFlags(
3134+
*CI, B.CreateBinaryIntrinsic(
3135+
IID, B.CreateFSub(CI->getArgOperand(0), CI->getArgOperand(1)),
3136+
ConstantFP::get(CI->getType(), 0)));
3137+
}
3138+
31123139
//===----------------------------------------------------------------------===//
31133140
// Integer Library Call Optimizations
31143141
//===----------------------------------------------------------------------===//
@@ -4042,6 +4069,10 @@ Value *LibCallSimplifier::optimizeFloatingPointLibCall(CallInst *CI,
40424069
if (hasFloatVersion(M, CI->getCalledFunction()->getName()))
40434070
return optimizeBinaryDoubleFP(CI, Builder, TLI);
40444071
return nullptr;
4072+
case LibFunc_fdim:
4073+
case LibFunc_fdimf:
4074+
case LibFunc_fdiml:
4075+
return optimizeFdim(CI, Builder);
40454076
case LibFunc_fminf:
40464077
case LibFunc_fmin:
40474078
case LibFunc_fminl:
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
2+
; RUN: opt < %s -passes=instcombine -S | FileCheck %s
3+
4+
define double @fdim_double() {
5+
; CHECK-LABEL: define double @fdim_double() {
6+
; CHECK-NEXT: ret double 2.500000e+00
7+
;
8+
%dim = call double @fdim(double 10.5, double 8.0)
9+
ret double %dim
10+
}
11+
12+
define double @fdim_double1() {
13+
; CHECK-LABEL: define double @fdim_double1() {
14+
; CHECK-NEXT: ret double 0.000000e+00
15+
;
16+
%dim = call double @fdim(double 7.0, double 8.0)
17+
ret double %dim
18+
}
19+
20+
define float @fdim_float() {
21+
; CHECK-LABEL: define float @fdim_float() {
22+
; CHECK-NEXT: ret float 0.000000e+00
23+
;
24+
%dim = call float @fdimf(float 1.500000e+00, float 8.0)
25+
ret float %dim
26+
}
27+
28+
define float @fdim_float1() {
29+
; CHECK-LABEL: define float @fdim_float1() {
30+
; CHECK-NEXT: ret float 2.000000e+00
31+
;
32+
%dim = call float @fdimf(float 1.000000e+01, float 8.0)
33+
ret float %dim
34+
}
35+
36+
define double @fdim_poison1() {
37+
; CHECK-LABEL: define double @fdim_poison1() {
38+
; CHECK-NEXT: [[DIM:%.*]] = call double @fdim(double poison, double 1.000000e+00)
39+
; CHECK-NEXT: ret double [[DIM]]
40+
;
41+
%dim = call double @fdim(double poison, double 1.0)
42+
ret double %dim
43+
}
44+
45+
define double @fdim_poison2() {
46+
; CHECK-LABEL: define double @fdim_poison2() {
47+
; CHECK-NEXT: [[DIM:%.*]] = call double @fdim(double 1.000000e+00, double poison)
48+
; CHECK-NEXT: ret double [[DIM]]
49+
;
50+
%dim = call double @fdim(double 1.0, double poison)
51+
ret double %dim
52+
}
53+
54+
define double @fdim_undef1() {
55+
; CHECK-LABEL: define double @fdim_undef1() {
56+
; CHECK-NEXT: [[DIM:%.*]] = call double @fdim(double undef, double 1.000000e+00)
57+
; CHECK-NEXT: ret double [[DIM]]
58+
;
59+
%dim = call double @fdim(double undef, double 1.0)
60+
ret double %dim
61+
}
62+
63+
define double @fdim_undef2() {
64+
; CHECK-LABEL: define double @fdim_undef2() {
65+
; CHECK-NEXT: [[DIM:%.*]] = call double @fdim(double 1.000000e+00, double undef)
66+
; CHECK-NEXT: ret double [[DIM]]
67+
;
68+
%dim = call double @fdim(double 1.0, double undef)
69+
ret double %dim
70+
}
71+
72+
define double @fdim_inf(){
73+
; CHECK-LABEL: define double @fdim_inf() {
74+
; CHECK-NEXT: ret double 0x7FF0000000000000
75+
;
76+
%dim = call double @fdim(double 0x7FF0000000000000, double 0x8000000000000000 )
77+
ret double %dim
78+
}
79+
80+
define double @fdim_nzero(){
81+
; CHECK-LABEL: define double @fdim_nzero() {
82+
; CHECK-NEXT: ret double 0.000000e+00
83+
;
84+
%dim = call double @fdim(double -0.0, double +0.0)
85+
ret double %dim
86+
}
87+
88+
declare double @fdim(double, double)
89+
declare float @fdimf(float, float)

0 commit comments

Comments
 (0)