Skip to content

Commit 3645c64

Browse files
authored
[SimplifyLibCalls] fdim constant fold (#109235)
2nd PR to fix #108695 based on #108702 --------- Signed-off-by: Kushal Pal <[email protected]>
1 parent bb34008 commit 3645c64

File tree

4 files changed

+207
-0
lines changed

4 files changed

+207
-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
@@ -3126,6 +3126,33 @@ Value *LibCallSimplifier::optimizeRemquo(CallInst *CI, IRBuilderBase &B) {
31263126
return ConstantFP::get(CI->getType(), Rem);
31273127
}
31283128

3129+
/// Constant folds fdim
3130+
Value *LibCallSimplifier::optimizeFdim(CallInst *CI, IRBuilderBase &B) {
3131+
// Cannot perform the fold unless the call has attribute memory(none)
3132+
if (!CI->doesNotAccessMemory())
3133+
return nullptr;
3134+
3135+
// TODO : Handle undef values
3136+
// Propagate poison if any
3137+
if (isa<PoisonValue>(CI->getArgOperand(0)))
3138+
return CI->getArgOperand(0);
3139+
if (isa<PoisonValue>(CI->getArgOperand(1)))
3140+
return CI->getArgOperand(1);
3141+
3142+
const APFloat *X, *Y;
3143+
// Check if both values are constants
3144+
if (!match(CI->getArgOperand(0), m_APFloat(X)) ||
3145+
!match(CI->getArgOperand(1), m_APFloat(Y)))
3146+
return nullptr;
3147+
3148+
APFloat Difference = *X;
3149+
Difference.subtract(*Y, RoundingMode::NearestTiesToEven);
3150+
3151+
APFloat MaxVal =
3152+
maximum(Difference, APFloat::getZero(CI->getType()->getFltSemantics()));
3153+
return ConstantFP::get(CI->getType(), MaxVal);
3154+
}
3155+
31293156
//===----------------------------------------------------------------------===//
31303157
// Integer Library Call Optimizations
31313158
//===----------------------------------------------------------------------===//
@@ -4059,6 +4086,10 @@ Value *LibCallSimplifier::optimizeFloatingPointLibCall(CallInst *CI,
40594086
if (hasFloatVersion(M, CI->getCalledFunction()->getName()))
40604087
return optimizeBinaryDoubleFP(CI, Builder, TLI);
40614088
return nullptr;
4089+
case LibFunc_fdim:
4090+
case LibFunc_fdimf:
4091+
case LibFunc_fdiml:
4092+
return optimizeFdim(CI, Builder);
40624093
case LibFunc_fminf:
40634094
case LibFunc_fmin:
40644095
case LibFunc_fminl:
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
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+
define double @fdim_snan1() {
121+
; CHECK-LABEL: define double @fdim_snan1() {
122+
; CHECK-NEXT: ret double 0x7FFC000000000000
123+
;
124+
%dim = call double @fdim(double 0x7FF4000000000000, double 1.4)
125+
ret double %dim
126+
}
127+
128+
define double @fdim_snan2() {
129+
; CHECK-LABEL: define double @fdim_snan2() {
130+
; CHECK-NEXT: ret double 0x7FFC000000000000
131+
;
132+
%dim = call double @fdim(double 1.7, double 0x7FF4000000000000)
133+
ret double %dim
134+
}
135+
136+
declare double @fdim(double, double) #0
137+
declare float @fdimf(float, float) #0
138+
139+
attributes #0 = { memory(none) }
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
2+
; RUN: opt %s -passes=instcombine -S -mtriple=i386-pc-windows-msvc | FileCheck %s --check-prefixes=MSVC19
3+
4+
define double @fdim_double() {
5+
; MSVC19-LABEL: define double @fdim_double() {
6+
; MSVC19-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+
;fdimf is not supported by windows
13+
define float @fdim_float() {
14+
; MSVC19-LABEL: define float @fdim_float() {
15+
; MSVC19-NEXT: [[DIM:%.*]] = call float @fdimf(float 1.500000e+00, float 8.000000e+00)
16+
; MSVC19-NEXT: ret float [[DIM]]
17+
;
18+
%dim = call float @fdimf(float 1.500000e+00, float 8.0)
19+
ret float %dim
20+
}
21+
22+
;fdiml is not supported by windows
23+
define fp128 @fdim_long() {
24+
; MSVC19-LABEL: define fp128 @fdim_long() {
25+
; MSVC19-NEXT: [[DIM:%.*]] = call fp128 @fdiml(fp128 0xL00000000000000000000000000000000, fp128 0xL00000000000000000000000000000000)
26+
; MSVC19-NEXT: ret fp128 [[DIM]]
27+
;
28+
%dim = call fp128 @fdiml(fp128 0xL00000000000000000000000000000000 , fp128 0xL00000000000000000000000000000000)
29+
ret fp128 %dim
30+
}
31+
32+
declare double @fdim(double, double) #0
33+
declare float @fdimf(float, float) #0
34+
declare fp128 @fdiml(fp128, fp128) #0
35+
36+
attributes #0 = { memory(none) }

0 commit comments

Comments
 (0)