Skip to content

Commit a1cc021

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

File tree

3 files changed

+159
-0
lines changed

3 files changed

+159
-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: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3109,6 +3109,42 @@ 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+
// Cannot perform the fold unless the call has attribute memory(none)
3115+
if (!CI->doesNotAccessMemory())
3116+
return nullptr;
3117+
3118+
const APFloat *X, *Y;
3119+
// Check if both values are constants
3120+
if (!match(CI->getArgOperand(0), m_APFloat(X)) ||
3121+
!match(CI->getArgOperand(1), m_APFloat(Y)))
3122+
return nullptr;
3123+
3124+
// If either argument is NaN, NaN is returned
3125+
if (X->isNaN())
3126+
return ConstantFP::get(CI->getType(), *X);
3127+
if (Y->isNaN())
3128+
return ConstantFP::get(CI->getType(), *Y);
3129+
3130+
// if X - Y overflows, it will set the errno, so we avoid the fold
3131+
APFloat Difference = *X;
3132+
APFloat::opStatus Status =
3133+
Difference.subtract(*Y, RoundingMode::NearestTiesToEven);
3134+
switch (Status) {
3135+
case APFloat::opStatus::opOK:
3136+
break;
3137+
default:
3138+
return nullptr;
3139+
}
3140+
auto *FSubCall =
3141+
copyFlags(*CI, B.CreateFSub(CI->getOperand(0), CI->getOperand(1)));
3142+
auto *FMaxCall = copyFlags(
3143+
*CI, B.CreateBinaryIntrinsic(Intrinsic::maximum, FSubCall,
3144+
ConstantFP::getZero(CI->getType())));
3145+
return FMaxCall;
3146+
}
3147+
31123148
//===----------------------------------------------------------------------===//
31133149
// Integer Library Call Optimizations
31143150
//===----------------------------------------------------------------------===//
@@ -4042,6 +4078,10 @@ Value *LibCallSimplifier::optimizeFloatingPointLibCall(CallInst *CI,
40424078
if (hasFloatVersion(M, CI->getCalledFunction()->getName()))
40434079
return optimizeBinaryDoubleFP(CI, Builder, TLI);
40444080
return nullptr;
4081+
case LibFunc_fdim:
4082+
case LibFunc_fdimf:
4083+
case LibFunc_fdiml:
4084+
return optimizeFdim(CI, Builder);
40454085
case LibFunc_fminf:
40464086
case LibFunc_fmin:
40474087
case LibFunc_fminl:
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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_poison3() {
55+
; CHECK-LABEL: define double @fdim_poison3() {
56+
; CHECK-NEXT: [[DIM:%.*]] = call double @fdim(double poison, double poison)
57+
; CHECK-NEXT: ret double [[DIM]]
58+
;
59+
%dim = call double @fdim(double poison, double poison)
60+
ret double %dim
61+
}
62+
63+
define double @fdim_undef1() {
64+
; CHECK-LABEL: define double @fdim_undef1() {
65+
; CHECK-NEXT: [[DIM:%.*]] = call double @fdim(double undef, double 1.000000e+00)
66+
; CHECK-NEXT: ret double [[DIM]]
67+
;
68+
%dim = call double @fdim(double undef, double 1.0)
69+
ret double %dim
70+
}
71+
72+
define double @fdim_undef2() {
73+
; CHECK-LABEL: define double @fdim_undef2() {
74+
; CHECK-NEXT: [[DIM:%.*]] = call double @fdim(double 1.000000e+00, double undef)
75+
; CHECK-NEXT: ret double [[DIM]]
76+
;
77+
%dim = call double @fdim(double 1.0, double undef)
78+
ret double %dim
79+
}
80+
81+
define double @fdim_undef3() {
82+
; CHECK-LABEL: define double @fdim_undef3() {
83+
; CHECK-NEXT: [[DIM:%.*]] = call double @fdim(double undef, double undef)
84+
; CHECK-NEXT: ret double [[DIM]]
85+
;
86+
%dim = call double @fdim(double undef, double undef)
87+
ret double %dim
88+
}
89+
90+
define double @fdim_inf_ninf(){
91+
; CHECK-LABEL: define double @fdim_inf_ninf() {
92+
; CHECK-NEXT: ret double 0x7FF0000000000000
93+
;
94+
%dim = call double @fdim(double 0x7FF0000000000000, double 0x8000000000000000 )
95+
ret double %dim
96+
}
97+
98+
define double @fdim_inf(){
99+
; CHECK-LABEL: define double @fdim_inf() {
100+
; CHECK-NEXT: [[DIM:%.*]] = call double @fdim(double 0x7FF0000000000000, double 0x7FF0000000000000)
101+
; CHECK-NEXT: ret double [[DIM]]
102+
;
103+
%dim = call double @fdim(double 0x7FF0000000000000, double 0x7FF0000000000000)
104+
ret double %dim
105+
}
106+
107+
define double @fdim_nzero(){
108+
; CHECK-LABEL: define double @fdim_nzero() {
109+
; CHECK-NEXT: ret double 0.000000e+00
110+
;
111+
%dim = call double @fdim(double -0.0, double +0.0)
112+
ret double %dim
113+
}
114+
115+
declare double @fdim(double, double) #0
116+
declare float @fdimf(float, float) #0
117+
118+
attributes #0 = { memory(none)}

0 commit comments

Comments
 (0)