Skip to content

Commit 1f07f99

Browse files
authored
[ConstantFold] Fold tgamma and tgammaf when the input parameter is a constant value. (llvm#114065)
This patch adds support for constant folding for the `tgamma` and `tgammaf` libc functions.
1 parent da97883 commit 1f07f99

File tree

2 files changed

+271
-3
lines changed

2 files changed

+271
-3
lines changed

llvm/lib/Analysis/ConstantFolding.cpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
#include <cassert>
5858
#include <cerrno>
5959
#include <cfenv>
60+
#include <cfloat>
6061
#include <cmath>
6162
#include <cstdint>
6263

@@ -1698,9 +1699,9 @@ bool llvm::canConstantFoldCallTo(const CallBase *Call, const Function *F) {
16981699
Name == "sinh" || Name == "sinhf" ||
16991700
Name == "sqrt" || Name == "sqrtf";
17001701
case 't':
1701-
return Name == "tan" || Name == "tanf" ||
1702-
Name == "tanh" || Name == "tanhf" ||
1703-
Name == "trunc" || Name == "truncf";
1702+
return Name == "tan" || Name == "tanf" || Name == "tanh" ||
1703+
Name == "tanhf" || Name == "trunc" || Name == "truncf" ||
1704+
Name == "tgamma" || Name == "tgammaf";
17041705
case '_':
17051706
// Check for various function names that get used for the math functions
17061707
// when the header files are preprocessed with the macro
@@ -2417,6 +2418,14 @@ static Constant *ConstantFoldScalarCall1(StringRef Name,
24172418
if (TLI->has(Func))
24182419
return ConstantFoldFP(erf, APF, Ty);
24192420
break;
2421+
case LibFunc_tgamma:
2422+
case LibFunc_tgammaf:
2423+
// NOTE: These boundaries are somewhat conservative.
2424+
if (TLI->has(Func) &&
2425+
(Ty->isDoubleTy() && APF > APFloat(DBL_MIN) && APF < APFloat(171.0) ||
2426+
Ty->isFloatTy() && APF > APFloat(FLT_MIN) && APF < APFloat(35.0f)))
2427+
return ConstantFoldFP(tgamma, APF, Ty);
2428+
break;
24202429
case LibFunc_nearbyint:
24212430
case LibFunc_nearbyintf:
24222431
case LibFunc_rint:
@@ -3629,6 +3638,10 @@ bool llvm::isMathLibCallNoop(const CallBase *Call,
36293638
case LibFunc_sqrtf:
36303639
return Op.isNaN() || Op.isZero() || !Op.isNegative();
36313640

3641+
case LibFunc_tgamma:
3642+
case LibFunc_tgammaf:
3643+
return true;
3644+
36323645
// FIXME: Add more functions: sqrt_finite, atanh, expm1, log1p,
36333646
// maybe others?
36343647
default:
Lines changed: 255 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,255 @@
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 float @tgammaf_in_range() {
5+
; CHECK-LABEL: define float @tgammaf_in_range() {
6+
; CHECK-NEXT: ret float 0x479A216280000000
7+
;
8+
%r = call float @tgammaf(float 34.0)
9+
ret float %r
10+
}
11+
12+
define double @tgamma_in_range() {
13+
; CHECK-LABEL: define double @tgamma_in_range() {
14+
; CHECK-NEXT: ret double 0x605166C698CF183B
15+
;
16+
%r = call double @tgamma(double 100.0)
17+
ret double %r
18+
}
19+
20+
define float @tgammaf_const_left_range() {
21+
; CHECK-LABEL: define float @tgammaf_const_left_range() {
22+
; CHECK-NEXT: [[R:%.*]] = call float @tgammaf(float 0x3810000000000000)
23+
; CHECK-NEXT: ret float [[R]]
24+
;
25+
%r = call float @tgammaf(float 0x3810000000000000)
26+
ret float %r
27+
}
28+
29+
define double @tgamma_const_left_range() {
30+
; CHECK-LABEL: define double @tgamma_const_left_range() {
31+
; CHECK-NEXT: [[R:%.*]] = call double @tgamma(double 0x10000000000000)
32+
; CHECK-NEXT: ret double [[R]]
33+
;
34+
%r = call double @tgamma(double 0x0010000000000000)
35+
ret double %r
36+
}
37+
38+
define float @tgammaf_const_right_range() {
39+
; CHECK-LABEL: define float @tgammaf_const_right_range() {
40+
; CHECK-NEXT: [[R:%.*]] = call float @tgammaf(float 3.600000e+01)
41+
; CHECK-NEXT: ret float [[R]]
42+
;
43+
%r = call float @tgammaf(float 36.0)
44+
ret float %r
45+
}
46+
47+
define double @tgamma_const_right_range() {
48+
; CHECK-LABEL: define double @tgamma_const_right_range() {
49+
; CHECK-NEXT: [[R:%.*]] = call double @tgamma(double 1.720000e+02)
50+
; CHECK-NEXT: ret double [[R]]
51+
;
52+
%r = call double @tgamma(double 172.0)
53+
ret double %r
54+
}
55+
56+
define float @tgammaf_minus_one() {
57+
; CHECK-LABEL: define float @tgammaf_minus_one() {
58+
; CHECK-NEXT: [[R:%.*]] = call float @tgammaf(float -1.000000e+00)
59+
; CHECK-NEXT: ret float [[R]]
60+
;
61+
%r = call float @tgammaf(float -1.000000e+00)
62+
ret float %r
63+
}
64+
65+
define double @tgamma_minus_one() {
66+
; CHECK-LABEL: define double @tgamma_minus_one() {
67+
; CHECK-NEXT: [[R:%.*]] = call double @tgamma(double -1.000000e+00)
68+
; CHECK-NEXT: ret double [[R]]
69+
;
70+
%r = call double @tgamma(double -1.000000e+00)
71+
ret double %r
72+
}
73+
74+
define float @tgammaf_minus_one_memory_none() {
75+
; CHECK-LABEL: define float @tgammaf_minus_one_memory_none() {
76+
; CHECK-NEXT: [[R:%.*]] = call float @tgammaf(float -1.000000e+00) #[[ATTR1:[0-9]+]]
77+
; CHECK-NEXT: ret float [[R]]
78+
;
79+
%r = call float @tgammaf(float -1.000000e+00) readnone
80+
ret float %r
81+
}
82+
83+
define double @tgamma_minus_one_memory_none() {
84+
; CHECK-LABEL: define double @tgamma_minus_one_memory_none() {
85+
; CHECK-NEXT: [[R:%.*]] = call double @tgamma(double -1.000000e+00) #[[ATTR1]]
86+
; CHECK-NEXT: ret double [[R]]
87+
;
88+
%r = call double @tgamma(double -1.000000e+00) readnone
89+
ret double %r
90+
}
91+
92+
define float @tgammaf_zero() {
93+
; CHECK-LABEL: define float @tgammaf_zero() {
94+
; CHECK-NEXT: [[R:%.*]] = call float @tgammaf(float 0.000000e+00)
95+
; CHECK-NEXT: ret float [[R]]
96+
;
97+
%r = call float @tgammaf(float 0.000000e+00)
98+
ret float %r
99+
}
100+
101+
define double @tgamma_zero() {
102+
; CHECK-LABEL: define double @tgamma_zero() {
103+
; CHECK-NEXT: [[R:%.*]] = call double @tgamma(double 0.000000e+00)
104+
; CHECK-NEXT: ret double [[R]]
105+
;
106+
%r = call double @tgamma(double 0.000000e+00)
107+
ret double %r
108+
}
109+
110+
define float @tgammaf_neg_zero() {
111+
; CHECK-LABEL: define float @tgammaf_neg_zero() {
112+
; CHECK-NEXT: [[R:%.*]] = call float @tgammaf(float -0.000000e+00)
113+
; CHECK-NEXT: ret float [[R]]
114+
;
115+
%r = call float @tgammaf(float -0.000000e+00)
116+
ret float %r
117+
}
118+
119+
define double @tgamma_neg_zero() {
120+
; CHECK-LABEL: define double @tgamma_neg_zero() {
121+
; CHECK-NEXT: [[R:%.*]] = call double @tgamma(double -0.000000e+00)
122+
; CHECK-NEXT: ret double [[R]]
123+
;
124+
%r = call double @tgamma(double -0.000000e+00)
125+
ret double %r
126+
}
127+
128+
define float @tgammaf_inf() {
129+
; CHECK-LABEL: define float @tgammaf_inf() {
130+
; CHECK-NEXT: [[R:%.*]] = call float @tgammaf(float 0x7FF0000000000000)
131+
; CHECK-NEXT: ret float [[R]]
132+
;
133+
%r = call float @tgammaf(float 0x7FF0000000000000)
134+
ret float %r
135+
}
136+
137+
define double @tgamma_inf() {
138+
; CHECK-LABEL: define double @tgamma_inf() {
139+
; CHECK-NEXT: [[R:%.*]] = call double @tgamma(double 0x7FF0000000000000)
140+
; CHECK-NEXT: ret double [[R]]
141+
;
142+
%r = call double @tgamma(double 0x7FF0000000000000)
143+
ret double %r
144+
}
145+
146+
define float @tgammaf_inf_memory_none() {
147+
; CHECK-LABEL: define float @tgammaf_inf_memory_none() {
148+
; CHECK-NEXT: [[R:%.*]] = call float @tgammaf(float 0x7FF0000000000000) #[[ATTR1]]
149+
; CHECK-NEXT: ret float [[R]]
150+
;
151+
%r = call float @tgammaf(float 0x7FF0000000000000) readnone
152+
ret float %r
153+
}
154+
155+
define double @tgamma_inf_memory_none() {
156+
; CHECK-LABEL: define double @tgamma_inf_memory_none() {
157+
; CHECK-NEXT: [[R:%.*]] = call double @tgamma(double 0x7FF0000000000000) #[[ATTR1]]
158+
; CHECK-NEXT: ret double [[R]]
159+
;
160+
%r = call double @tgamma(double 0x7FF0000000000000) readnone
161+
ret double %r
162+
}
163+
164+
define float @tgammaf_neg_inf() {
165+
; CHECK-LABEL: define float @tgammaf_neg_inf() {
166+
; CHECK-NEXT: [[R:%.*]] = call float @tgammaf(float 0xFFF0000000000000)
167+
; CHECK-NEXT: ret float [[R]]
168+
;
169+
%r = call float @tgammaf(float 0xFFF0000000000000)
170+
ret float %r
171+
}
172+
173+
define double @tgamma_neg_inf() {
174+
; CHECK-LABEL: define double @tgamma_neg_inf() {
175+
; CHECK-NEXT: [[R:%.*]] = call double @tgamma(double 0xFFF0000000000000)
176+
; CHECK-NEXT: ret double [[R]]
177+
;
178+
%r = call double @tgamma(double 0xFFF0000000000000)
179+
ret double %r
180+
}
181+
182+
define float @tgammaf_neg_inf_memory_none() {
183+
; CHECK-LABEL: define float @tgammaf_neg_inf_memory_none() {
184+
; CHECK-NEXT: [[R:%.*]] = call float @tgammaf(float 0xFFF0000000000000) #[[ATTR1]]
185+
; CHECK-NEXT: ret float [[R]]
186+
;
187+
%r = call float @tgammaf(float 0xFFF0000000000000) readnone
188+
ret float %r
189+
}
190+
191+
define double @tgamma_neg_inf_memory_none() {
192+
; CHECK-LABEL: define double @tgamma_neg_inf_memory_none() {
193+
; CHECK-NEXT: [[R:%.*]] = call double @tgamma(double 0xFFF0000000000000) #[[ATTR1]]
194+
; CHECK-NEXT: ret double [[R]]
195+
;
196+
%r = call double @tgamma(double 0xFFF0000000000000) readnone
197+
ret double %r
198+
}
199+
200+
define float @tgammaf_nan() {
201+
; CHECK-LABEL: define float @tgammaf_nan() {
202+
; CHECK-NEXT: [[R:%.*]] = call float @tgammaf(float 0x7FF8000000000000)
203+
; CHECK-NEXT: ret float [[R]]
204+
;
205+
%r = call float @tgammaf(float 0x7FF8000000000000)
206+
ret float %r
207+
}
208+
209+
define double @tgamma_nan() {
210+
; CHECK-LABEL: define double @tgamma_nan() {
211+
; CHECK-NEXT: [[R:%.*]] = call double @tgamma(double 0x7FF8000000000000)
212+
; CHECK-NEXT: ret double [[R]]
213+
;
214+
%r = call double @tgamma(double 0x7FF8000000000000)
215+
ret double %r
216+
}
217+
218+
define float @tgammaf_nan_memory_none() {
219+
; CHECK-LABEL: define float @tgammaf_nan_memory_none() {
220+
; CHECK-NEXT: [[R:%.*]] = call float @tgammaf(float 0x7FF8000000000000) #[[ATTR1]]
221+
; CHECK-NEXT: ret float [[R]]
222+
;
223+
%r = call float @tgammaf(float 0x7FF8000000000000) readnone
224+
ret float %r
225+
}
226+
227+
define double @tgamma_nan_memory_none() {
228+
; CHECK-LABEL: define double @tgamma_nan_memory_none() {
229+
; CHECK-NEXT: [[R:%.*]] = call double @tgamma(double 0x7FF8000000000000) #[[ATTR1]]
230+
; CHECK-NEXT: ret double [[R]]
231+
;
232+
%r = call double @tgamma(double 0x7FF8000000000000) readnone
233+
ret double %r
234+
}
235+
236+
define float @tgammaf_poison() {
237+
; CHECK-LABEL: define float @tgammaf_poison() {
238+
; CHECK-NEXT: [[R:%.*]] = call float @tgammaf(float poison)
239+
; CHECK-NEXT: ret float [[R]]
240+
;
241+
%r = call float @tgammaf(float poison)
242+
ret float %r
243+
}
244+
245+
define double @tgamma_poison() {
246+
; CHECK-LABEL: define double @tgamma_poison() {
247+
; CHECK-NEXT: [[R:%.*]] = call double @tgamma(double poison)
248+
; CHECK-NEXT: ret double [[R]]
249+
;
250+
%r = call double @tgamma(double poison)
251+
ret double %r
252+
}
253+
254+
declare float @tgammaf(float) willreturn
255+
declare double @tgamma(double) willreturn

0 commit comments

Comments
 (0)