Skip to content

Commit 755ca75

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

File tree

3 files changed

+155
-0
lines changed

3 files changed

+155
-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+
// Cannot perform the fold unless the call has attribute memory(none)
3115+
if (!CI->doesNotAccessMemory())
3116+
return nullptr;
3117+
3118+
// TODO : Handle undef values
3119+
// Propagate poison if any
3120+
if (isa<PoisonValue>(CI->getArgOperand(0)))
3121+
return CI->getArgOperand(0);
3122+
if (isa<PoisonValue>(CI->getArgOperand(1)))
3123+
return CI->getArgOperand(1);
3124+
3125+
const APFloat *X, *Y;
3126+
// Check if both values are constants
3127+
if (!match(CI->getArgOperand(0), m_APFloat(X)) ||
3128+
!match(CI->getArgOperand(1), m_APFloat(Y)))
3129+
return nullptr;
3130+
3131+
APFloat Difference = *X;
3132+
Difference.subtract(*Y, RoundingMode::NearestTiesToEven);
3133+
3134+
APFloat MaxVal =
3135+
maximum(Difference, APFloat::getZero(CI->getType()->getFltSemantics()));
3136+
return ConstantFP::get(CI->getType(), MaxVal);
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: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
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: ret double poison
39+
;
40+
%dim = call double @fdim(double poison, double 1.0)
41+
ret double %dim
42+
}
43+
44+
define double @fdim_poison2() {
45+
; CHECK-LABEL: define double @fdim_poison2() {
46+
; CHECK-NEXT: ret double poison
47+
;
48+
%dim = call double @fdim(double 1.0, double poison)
49+
ret double %dim
50+
}
51+
52+
define double @fdim_poison3() {
53+
; CHECK-LABEL: define double @fdim_poison3() {
54+
; CHECK-NEXT: ret double poison
55+
;
56+
%dim = call double @fdim(double poison, double poison)
57+
ret double %dim
58+
}
59+
60+
; undef folding is not implemented yet
61+
define double @fdim_undef1() {
62+
; CHECK-LABEL: define double @fdim_undef1() {
63+
; CHECK-NEXT: [[DIM:%.*]] = call double @fdim(double undef, double 1.000000e+00)
64+
; CHECK-NEXT: ret double [[DIM]]
65+
;
66+
%dim = call double @fdim(double undef, double 1.0)
67+
ret double %dim
68+
}
69+
70+
define double @fdim_inf_ninf() {
71+
; CHECK-LABEL: define double @fdim_inf_ninf() {
72+
; CHECK-NEXT: ret double 0x7FF0000000000000
73+
;
74+
%dim = call double @fdim(double 0x7FF0000000000000, double 0x8000000000000000 )
75+
ret double %dim
76+
}
77+
78+
define double @fdim_inf() {
79+
; CHECK-LABEL: define double @fdim_inf() {
80+
; CHECK-NEXT: ret double 0x7FF8000000000000
81+
;
82+
%dim = call double @fdim(double 0x7FF0000000000000, double 0x7FF0000000000000)
83+
ret double %dim
84+
}
85+
86+
define double @fdim_nzero() {
87+
; CHECK-LABEL: define double @fdim_nzero() {
88+
; CHECK-NEXT: ret double 0.000000e+00
89+
;
90+
%dim = call double @fdim(double -0.0, double +0.0)
91+
ret double %dim
92+
}
93+
94+
define double @fdim_strictfp() {
95+
; CHECK-LABEL: define double @fdim_strictfp() {
96+
; CHECK-NEXT: [[DIM:%.*]] = call double @fdim(double 1.000000e+01, double 8.000000e+00) #[[ATTR1:[0-9]+]]
97+
; CHECK-NEXT: ret double [[DIM]]
98+
;
99+
%dim = call double @fdim(double 10.0, double 8.0) strictfp
100+
ret double %dim
101+
}
102+
103+
define double @fdim_nan1() {
104+
; CHECK-LABEL: define double @fdim_nan1() {
105+
; CHECK-NEXT: ret double 0x7FF8000000000000
106+
;
107+
%dim = call double @fdim(double 10.0, double 0x7FF8000000000000)
108+
ret double %dim
109+
}
110+
111+
112+
define double @fdim_nan2() {
113+
; CHECK-LABEL: define double @fdim_nan2() {
114+
; CHECK-NEXT: ret double 0x7FF8000000000000
115+
;
116+
%dim = call double @fdim(double 0x7FF8000000000000, double 1.4)
117+
ret double %dim
118+
}
119+
120+
declare double @fdim(double, double) #0
121+
declare float @fdimf(float, float) #0
122+
123+
attributes #0 = { memory(none) }

0 commit comments

Comments
 (0)