blob: ca9226dd076f029a45c2765f7093f7029db1a858 [file] [log] [blame]
Sanjay Patel2580c952016-12-08 20:23:56 +00001; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
Michael Ilsemanbb6f6912012-12-12 00:27:46 +00002; RUN: opt < %s -instsimplify -S | FileCheck %s
3
Sanjay Patel49b7dc22018-03-12 15:47:32 +00004define <2 x float> @fsub_negzero_vec_undef_elts(<2 x float> %x) {
5; CHECK-LABEL: @fsub_negzero_vec_undef_elts(
Sanjay Patela0d8d122018-03-12 18:17:01 +00006; CHECK-NEXT: ret <2 x float> [[X:%.*]]
Sanjay Patel49b7dc22018-03-12 15:47:32 +00007;
8 %r = fsub nsz <2 x float> %x, <float undef, float -0.0>
9 ret <2 x float> %r
10}
11
Benjamin Kramerf5b2a472016-02-29 11:12:23 +000012; fsub -0.0, (fsub -0.0, X) ==> X
Benjamin Kramerf5b2a472016-02-29 11:12:23 +000013define float @fsub_-0_-0_x(float %a) {
Sanjay Patel69632442016-03-25 20:12:25 +000014; CHECK-LABEL: @fsub_-0_-0_x(
Sanjay Patelb13fcd52018-02-13 18:22:53 +000015; CHECK-NEXT: ret float [[A:%.*]]
Sanjay Patel69632442016-03-25 20:12:25 +000016;
Michael Ilsemanbb6f6912012-12-12 00:27:46 +000017 %t1 = fsub float -0.0, %a
18 %ret = fsub float -0.0, %t1
Michael Ilsemanbb6f6912012-12-12 00:27:46 +000019 ret float %ret
20}
21
Sanjay Patel66911b12018-02-26 23:23:02 +000022define <2 x float> @fsub_-0_-0_x_vec(<2 x float> %a) {
23; CHECK-LABEL: @fsub_-0_-0_x_vec(
24; CHECK-NEXT: ret <2 x float> [[A:%.*]]
25;
26 %t1 = fsub <2 x float> <float -0.0, float -0.0>, %a
27 %ret = fsub <2 x float> <float -0.0, float -0.0>, %t1
28 ret <2 x float> %ret
29}
30
31define <2 x float> @fsub_-0_-0_x_vec_undef_elts(<2 x float> %a) {
32; CHECK-LABEL: @fsub_-0_-0_x_vec_undef_elts(
Sanjay Patela0d8d122018-03-12 18:17:01 +000033; CHECK-NEXT: ret <2 x float> [[A:%.*]]
Sanjay Patel66911b12018-02-26 23:23:02 +000034;
35 %t1 = fsub <2 x float> <float undef, float -0.0>, %a
36 %ret = fsub <2 x float> <float -0.0, float undef>, %t1
37 ret <2 x float> %ret
38}
39
Benjamin Kramerf5b2a472016-02-29 11:12:23 +000040; fsub 0.0, (fsub -0.0, X) != X
Benjamin Kramerf5b2a472016-02-29 11:12:23 +000041define float @fsub_0_-0_x(float %a) {
Sanjay Patel69632442016-03-25 20:12:25 +000042; CHECK-LABEL: @fsub_0_-0_x(
Sanjay Patelb13fcd52018-02-13 18:22:53 +000043; CHECK-NEXT: [[T1:%.*]] = fsub float 0.000000e+00, [[A:%.*]]
Sanjay Patel69632442016-03-25 20:12:25 +000044; CHECK-NEXT: [[RET:%.*]] = fsub float -0.000000e+00, [[T1]]
45; CHECK-NEXT: ret float [[RET]]
46;
Benjamin Kramerf5b2a472016-02-29 11:12:23 +000047 %t1 = fsub float 0.0, %a
48 %ret = fsub float -0.0, %t1
Benjamin Kramerf5b2a472016-02-29 11:12:23 +000049 ret float %ret
50}
51
52; fsub -0.0, (fsub 0.0, X) != X
Benjamin Kramerf5b2a472016-02-29 11:12:23 +000053define float @fsub_-0_0_x(float %a) {
Sanjay Patel69632442016-03-25 20:12:25 +000054; CHECK-LABEL: @fsub_-0_0_x(
Sanjay Patelb13fcd52018-02-13 18:22:53 +000055; CHECK-NEXT: [[T1:%.*]] = fsub float -0.000000e+00, [[A:%.*]]
Sanjay Patel69632442016-03-25 20:12:25 +000056; CHECK-NEXT: [[RET:%.*]] = fsub float 0.000000e+00, [[T1]]
57; CHECK-NEXT: ret float [[RET]]
58;
Benjamin Kramerf5b2a472016-02-29 11:12:23 +000059 %t1 = fsub float -0.0, %a
60 %ret = fsub float 0.0, %t1
Benjamin Kramerf5b2a472016-02-29 11:12:23 +000061 ret float %ret
62}
63
Michael Ilsemanbb6f6912012-12-12 00:27:46 +000064; fsub X, 0 ==> X
Sanjay Patelc84b48e2018-03-25 17:48:20 +000065define float @fsub_x_0(float %x) {
Sanjay Patel69632442016-03-25 20:12:25 +000066; CHECK-LABEL: @fsub_x_0(
Sanjay Patelc84b48e2018-03-25 17:48:20 +000067; CHECK-NEXT: ret float [[X:%.*]]
Sanjay Patel69632442016-03-25 20:12:25 +000068;
Sanjay Patelc84b48e2018-03-25 17:48:20 +000069 %r = fsub float %x, 0.0
70 ret float %r
71}
72
73define <2 x float> @fsub_x_0_vec_undef(<2 x float> %x) {
74; CHECK-LABEL: @fsub_x_0_vec_undef(
Sanjay Patel93e64dd2018-03-25 21:16:33 +000075; CHECK-NEXT: ret <2 x float> [[X:%.*]]
Sanjay Patelc84b48e2018-03-25 17:48:20 +000076;
77 %r = fsub <2 x float> %x, <float undef, float 0.0>
78 ret <2 x float> %r
Michael Ilsemanbb6f6912012-12-12 00:27:46 +000079}
80
81; fadd X, -0 ==> X
Michael Ilsemanbb6f6912012-12-12 00:27:46 +000082define float @fadd_x_n0(float %a) {
Sanjay Patel69632442016-03-25 20:12:25 +000083; CHECK-LABEL: @fadd_x_n0(
Sanjay Patelb13fcd52018-02-13 18:22:53 +000084; CHECK-NEXT: ret float [[A:%.*]]
Sanjay Patel69632442016-03-25 20:12:25 +000085;
Michael Ilsemanbb6f6912012-12-12 00:27:46 +000086 %ret = fadd float %a, -0.0
Michael Ilsemanbb6f6912012-12-12 00:27:46 +000087 ret float %ret
88}
89
Sanjay Patel66911b12018-02-26 23:23:02 +000090define <2 x float> @fadd_x_n0_vec_undef_elt(<2 x float> %a) {
91; CHECK-LABEL: @fadd_x_n0_vec_undef_elt(
Sanjay Patela0d8d122018-03-12 18:17:01 +000092; CHECK-NEXT: ret <2 x float> [[A:%.*]]
Sanjay Patel66911b12018-02-26 23:23:02 +000093;
94 %ret = fadd <2 x float> %a, <float -0.0, float undef>
95 ret <2 x float> %ret
96}
97
Michael Ilsemanbb6f6912012-12-12 00:27:46 +000098; fmul X, 1.0 ==> X
Michael Ilsemanbb6f6912012-12-12 00:27:46 +000099define double @fmul_X_1(double %a) {
Sanjay Patel69632442016-03-25 20:12:25 +0000100; CHECK-LABEL: @fmul_X_1(
Sanjay Patelb13fcd52018-02-13 18:22:53 +0000101; CHECK-NEXT: ret double [[A:%.*]]
Sanjay Patel69632442016-03-25 20:12:25 +0000102;
Sanjay Patelb13fcd52018-02-13 18:22:53 +0000103 %b = fmul double 1.0, %a
Michael Ilsemanbb6f6912012-12-12 00:27:46 +0000104 ret double %b
105}
Sanjay Patel40eaa8d2015-02-25 18:00:15 +0000106
Sanjay Patelb13fcd52018-02-13 18:22:53 +0000107; PR2642
108define <4 x float> @fmul_X_1_vec(<4 x float> %x) {
109; CHECK-LABEL: @fmul_X_1_vec(
110; CHECK-NEXT: ret <4 x float> [[X:%.*]]
111;
112 %m = fmul <4 x float> %x, <float 1.0, float 1.0, float 1.0, float 1.0>
113 ret <4 x float> %m
114}
115
Sanjay Patel2580c952016-12-08 20:23:56 +0000116; fdiv X, 1.0 ==> X
117define float @fdiv_x_1(float %a) {
118; CHECK-LABEL: @fdiv_x_1(
Sanjay Patelb13fcd52018-02-13 18:22:53 +0000119; CHECK-NEXT: ret float [[A:%.*]]
Sanjay Patel2580c952016-12-08 20:23:56 +0000120;
121 %ret = fdiv float %a, 1.0
122 ret float %ret
123}
124
Sanjay Patel40eaa8d2015-02-25 18:00:15 +0000125; We can't optimize away the fadd in this test because the input
Sanjay Patel69632442016-03-25 20:12:25 +0000126; value to the function and subsequently to the fadd may be -0.0.
Sanjay Patel40eaa8d2015-02-25 18:00:15 +0000127; In that one special case, the result of the fadd should be +0.0
128; rather than the first parameter of the fadd.
129
Sanjay Patel69632442016-03-25 20:12:25 +0000130; Fragile test warning: We need 6 sqrt calls to trigger the bug
131; because the internal logic has a magic recursion limit of 6.
Sanjay Patel40eaa8d2015-02-25 18:00:15 +0000132; This is presented without any explanation or ability to customize.
133
134declare float @sqrtf(float)
135
136define float @PR22688(float %x) {
Sanjay Patel69632442016-03-25 20:12:25 +0000137; CHECK-LABEL: @PR22688(
Sanjay Patelb13fcd52018-02-13 18:22:53 +0000138; CHECK-NEXT: [[TMP1:%.*]] = call float @sqrtf(float [[X:%.*]])
Sanjay Patel2580c952016-12-08 20:23:56 +0000139; CHECK-NEXT: [[TMP2:%.*]] = call float @sqrtf(float [[TMP1]])
140; CHECK-NEXT: [[TMP3:%.*]] = call float @sqrtf(float [[TMP2]])
141; CHECK-NEXT: [[TMP4:%.*]] = call float @sqrtf(float [[TMP3]])
142; CHECK-NEXT: [[TMP5:%.*]] = call float @sqrtf(float [[TMP4]])
143; CHECK-NEXT: [[TMP6:%.*]] = call float @sqrtf(float [[TMP5]])
144; CHECK-NEXT: [[TMP7:%.*]] = fadd float [[TMP6]], 0.000000e+00
Sanjay Patel69632442016-03-25 20:12:25 +0000145; CHECK-NEXT: ret float [[TMP7]]
146;
Sanjay Patel40eaa8d2015-02-25 18:00:15 +0000147 %1 = call float @sqrtf(float %x)
148 %2 = call float @sqrtf(float %1)
149 %3 = call float @sqrtf(float %2)
150 %4 = call float @sqrtf(float %3)
151 %5 = call float @sqrtf(float %4)
152 %6 = call float @sqrtf(float %5)
153 %7 = fadd float %6, 0.0
154 ret float %7
Sanjay Patel40eaa8d2015-02-25 18:00:15 +0000155}
156
Matt Arsenault1e0edbf2017-01-11 00:33:24 +0000157declare float @llvm.fabs.f32(float)
Craig Topperaee341e2018-02-25 06:51:52 +0000158declare <2 x float> @llvm.fabs.v2f32(<2 x float>)
Justin Lebar7e3184c2017-01-26 00:10:26 +0000159declare float @llvm.sqrt.f32(float)
Matt Arsenault1e0edbf2017-01-11 00:33:24 +0000160
Matt Arsenault1e0edbf2017-01-11 00:33:24 +0000161define float @fabs_select_positive_constants(i32 %c) {
Sanjay Patelb13fcd52018-02-13 18:22:53 +0000162; CHECK-LABEL: @fabs_select_positive_constants(
163; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[C:%.*]], 0
164; CHECK-NEXT: [[SELECT:%.*]] = select i1 [[CMP]], float 1.000000e+00, float 2.000000e+00
165; CHECK-NEXT: ret float [[SELECT]]
166;
Matt Arsenault1e0edbf2017-01-11 00:33:24 +0000167 %cmp = icmp eq i32 %c, 0
168 %select = select i1 %cmp, float 1.0, float 2.0
169 %fabs = call float @llvm.fabs.f32(float %select)
170 ret float %fabs
171}
172
Craig Topperaee341e2018-02-25 06:51:52 +0000173define <2 x float> @fabs_select_positive_constants_vector(i32 %c) {
174; CHECK-LABEL: @fabs_select_positive_constants_vector(
175; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[C:%.*]], 0
176; CHECK-NEXT: [[SELECT:%.*]] = select i1 [[CMP]], <2 x float> <float 1.000000e+00, float 1.000000e+00>, <2 x float> <float 2.000000e+00, float 2.000000e+00>
Craig Topper69c89722018-02-26 22:33:17 +0000177; CHECK-NEXT: ret <2 x float> [[SELECT]]
Craig Topperaee341e2018-02-25 06:51:52 +0000178;
179 %cmp = icmp eq i32 %c, 0
180 %select = select i1 %cmp, <2 x float> <float 1.0, float 1.0>, <2 x float> <float 2.0, float 2.0>
181 %fabs = call <2 x float> @llvm.fabs.v2f32(<2 x float> %select)
182 ret <2 x float> %fabs
183}
184
Matt Arsenault1e0edbf2017-01-11 00:33:24 +0000185define float @fabs_select_constant_variable(i32 %c, float %x) {
Sanjay Patelb13fcd52018-02-13 18:22:53 +0000186; CHECK-LABEL: @fabs_select_constant_variable(
187; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[C:%.*]], 0
188; CHECK-NEXT: [[SELECT:%.*]] = select i1 [[CMP]], float 1.000000e+00, float [[X:%.*]]
189; CHECK-NEXT: [[FABS:%.*]] = call float @llvm.fabs.f32(float [[SELECT]])
190; CHECK-NEXT: ret float [[FABS]]
191;
Matt Arsenault1e0edbf2017-01-11 00:33:24 +0000192 %cmp = icmp eq i32 %c, 0
193 %select = select i1 %cmp, float 1.0, float %x
194 %fabs = call float @llvm.fabs.f32(float %select)
195 ret float %fabs
196}
197
Craig Topperaee341e2018-02-25 06:51:52 +0000198define <2 x float> @fabs_select_constant_variable_vector(i32 %c, <2 x float> %x) {
199; CHECK-LABEL: @fabs_select_constant_variable_vector(
200; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[C:%.*]], 0
201; CHECK-NEXT: [[SELECT:%.*]] = select i1 [[CMP]], <2 x float> <float 1.000000e+00, float 1.000000e+00>, <2 x float> [[X:%.*]]
202; CHECK-NEXT: [[FABS:%.*]] = call <2 x float> @llvm.fabs.v2f32(<2 x float> [[SELECT]])
203; CHECK-NEXT: ret <2 x float> [[FABS]]
204;
205 %cmp = icmp eq i32 %c, 0
206 %select = select i1 %cmp, <2 x float> <float 1.0, float 1.0>, <2 x float> %x
207 %fabs = call <2 x float> @llvm.fabs.v2f32(<2 x float> %select)
208 ret <2 x float> %fabs
209}
210
Craig Topper2b8f0512018-02-25 06:51:51 +0000211define float @fabs_select_neg0_pos0(i32 %c) {
Sanjay Patelb13fcd52018-02-13 18:22:53 +0000212; CHECK-LABEL: @fabs_select_neg0_pos0(
213; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[C:%.*]], 0
214; CHECK-NEXT: [[SELECT:%.*]] = select i1 [[CMP]], float -0.000000e+00, float 0.000000e+00
215; CHECK-NEXT: [[FABS:%.*]] = call float @llvm.fabs.f32(float [[SELECT]])
216; CHECK-NEXT: ret float [[FABS]]
217;
Matt Arsenault1e0edbf2017-01-11 00:33:24 +0000218 %cmp = icmp eq i32 %c, 0
219 %select = select i1 %cmp, float -0.0, float 0.0
220 %fabs = call float @llvm.fabs.f32(float %select)
221 ret float %fabs
222}
223
Craig Topperaee341e2018-02-25 06:51:52 +0000224define <2 x float> @fabs_select_neg0_pos0_vector(i32 %c) {
225; CHECK-LABEL: @fabs_select_neg0_pos0_vector(
226; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[C:%.*]], 0
227; CHECK-NEXT: [[SELECT:%.*]] = select i1 [[CMP]], <2 x float> <float -0.000000e+00, float -0.000000e+00>, <2 x float> zeroinitializer
228; CHECK-NEXT: [[FABS:%.*]] = call <2 x float> @llvm.fabs.v2f32(<2 x float> [[SELECT]])
229; CHECK-NEXT: ret <2 x float> [[FABS]]
230;
231 %cmp = icmp eq i32 %c, 0
232 %select = select i1 %cmp, <2 x float> <float -0.0, float -0.0>, <2 x float> <float 0.0, float 0.0>
233 %fabs = call <2 x float> @llvm.fabs.v2f32(<2 x float> %select)
234 ret <2 x float> %fabs
235}
236
Craig Topper2b8f0512018-02-25 06:51:51 +0000237define float @fabs_select_neg0_neg1(i32 %c) {
Sanjay Patelb13fcd52018-02-13 18:22:53 +0000238; CHECK-LABEL: @fabs_select_neg0_neg1(
239; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[C:%.*]], 0
240; CHECK-NEXT: [[SELECT:%.*]] = select i1 [[CMP]], float -0.000000e+00, float -1.000000e+00
241; CHECK-NEXT: [[FABS:%.*]] = call float @llvm.fabs.f32(float [[SELECT]])
242; CHECK-NEXT: ret float [[FABS]]
243;
Matt Arsenault1e0edbf2017-01-11 00:33:24 +0000244 %cmp = icmp eq i32 %c, 0
245 %select = select i1 %cmp, float -0.0, float -1.0
246 %fabs = call float @llvm.fabs.f32(float %select)
247 ret float %fabs
248}
249
Craig Topperaee341e2018-02-25 06:51:52 +0000250define <2 x float> @fabs_select_neg0_neg1_vector(i32 %c) {
251; CHECK-LABEL: @fabs_select_neg0_neg1_vector(
252; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[C:%.*]], 0
253; CHECK-NEXT: [[SELECT:%.*]] = select i1 [[CMP]], <2 x float> <float -0.000000e+00, float -0.000000e+00>, <2 x float> <float -1.000000e+00, float -1.000000e+00>
254; CHECK-NEXT: [[FABS:%.*]] = call <2 x float> @llvm.fabs.v2f32(<2 x float> [[SELECT]])
255; CHECK-NEXT: ret <2 x float> [[FABS]]
256;
257 %cmp = icmp eq i32 %c, 0
258 %select = select i1 %cmp, <2 x float> <float -0.0, float -0.0>, <2 x float> <float -1.0, float -1.0>
259 %fabs = call <2 x float> @llvm.fabs.v2f32(<2 x float> %select)
260 ret <2 x float> %fabs
261}
262
Craig Topper2b8f0512018-02-25 06:51:51 +0000263define float @fabs_select_nan_nan(i32 %c) {
Sanjay Patelb13fcd52018-02-13 18:22:53 +0000264; CHECK-LABEL: @fabs_select_nan_nan(
265; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[C:%.*]], 0
266; CHECK-NEXT: [[SELECT:%.*]] = select i1 [[CMP]], float 0x7FF8000000000000, float 0x7FF8000100000000
267; CHECK-NEXT: ret float [[SELECT]]
268;
Matt Arsenault1e0edbf2017-01-11 00:33:24 +0000269 %cmp = icmp eq i32 %c, 0
270 %select = select i1 %cmp, float 0x7FF8000000000000, float 0x7FF8000100000000
271 %fabs = call float @llvm.fabs.f32(float %select)
272 ret float %fabs
273}
274
Craig Topperaee341e2018-02-25 06:51:52 +0000275define <2 x float> @fabs_select_nan_nan_vector(i32 %c) {
276; CHECK-LABEL: @fabs_select_nan_nan_vector(
277; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[C:%.*]], 0
278; CHECK-NEXT: [[SELECT:%.*]] = select i1 [[CMP]], <2 x float> <float 0x7FF8000000000000, float 0x7FF8000000000000>, <2 x float> <float 0x7FF8000100000000, float 0x7FF8000100000000>
Craig Topper69c89722018-02-26 22:33:17 +0000279; CHECK-NEXT: ret <2 x float> [[SELECT]]
Craig Topperaee341e2018-02-25 06:51:52 +0000280;
281 %cmp = icmp eq i32 %c, 0
282 %select = select i1 %cmp, <2 x float> <float 0x7FF8000000000000, float 0x7FF8000000000000>, <2 x float> <float 0x7FF8000100000000, float 0x7FF8000100000000>
283 %fabs = call <2 x float> @llvm.fabs.v2f32(<2 x float> %select)
284 ret <2 x float> %fabs
285}
286
Craig Topper2b8f0512018-02-25 06:51:51 +0000287define float @fabs_select_negnan_nan(i32 %c) {
Sanjay Patelb13fcd52018-02-13 18:22:53 +0000288; CHECK-LABEL: @fabs_select_negnan_nan(
289; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[C:%.*]], 0
290; CHECK-NEXT: [[SELECT:%.*]] = select i1 [[CMP]], float 0xFFF8000000000000, float 0x7FF8000000000000
291; CHECK-NEXT: [[FABS:%.*]] = call float @llvm.fabs.f32(float [[SELECT]])
292; CHECK-NEXT: ret float [[FABS]]
293;
Matt Arsenault1e0edbf2017-01-11 00:33:24 +0000294 %cmp = icmp eq i32 %c, 0
295 %select = select i1 %cmp, float 0xFFF8000000000000, float 0x7FF8000000000000
296 %fabs = call float @llvm.fabs.f32(float %select)
297 ret float %fabs
298}
299
Craig Topperaee341e2018-02-25 06:51:52 +0000300define <2 x float> @fabs_select_negnan_nan_vector(i32 %c) {
301; CHECK-LABEL: @fabs_select_negnan_nan_vector(
302; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[C:%.*]], 0
303; CHECK-NEXT: [[SELECT:%.*]] = select i1 [[CMP]], <2 x float> <float 0xFFF8000000000000, float 0xFFF8000000000000>, <2 x float> <float 0x7FF8000000000000, float 0x7FF8000000000000>
304; CHECK-NEXT: [[FABS:%.*]] = call <2 x float> @llvm.fabs.v2f32(<2 x float> [[SELECT]])
305; CHECK-NEXT: ret <2 x float> [[FABS]]
306;
307 %cmp = icmp eq i32 %c, 0
308 %select = select i1 %cmp, <2 x float> <float 0xFFF8000000000000, float 0xFFF8000000000000>, <2 x float> <float 0x7FF8000000000000, float 0x7FF8000000000000>
309 %fabs = call <2 x float> @llvm.fabs.v2f32(<2 x float> %select)
310 ret <2 x float> %fabs
311}
312
Craig Topper2b8f0512018-02-25 06:51:51 +0000313define float @fabs_select_negnan_negnan(i32 %c) {
Sanjay Patelb13fcd52018-02-13 18:22:53 +0000314; CHECK-LABEL: @fabs_select_negnan_negnan(
315; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[C:%.*]], 0
316; CHECK-NEXT: [[SELECT:%.*]] = select i1 [[CMP]], float 0xFFF8000000000000, float 0x7FF8000100000000
317; CHECK-NEXT: [[FABS:%.*]] = call float @llvm.fabs.f32(float [[SELECT]])
318; CHECK-NEXT: ret float [[FABS]]
319;
Matt Arsenault1e0edbf2017-01-11 00:33:24 +0000320 %cmp = icmp eq i32 %c, 0
321 %select = select i1 %cmp, float 0xFFF8000000000000, float 0x7FF8000100000000
322 %fabs = call float @llvm.fabs.f32(float %select)
323 ret float %fabs
324}
325
Craig Topperaee341e2018-02-25 06:51:52 +0000326define <2 x float> @fabs_select_negnan_negnan_vector(i32 %c) {
327; CHECK-LABEL: @fabs_select_negnan_negnan_vector(
328; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[C:%.*]], 0
329; CHECK-NEXT: [[SELECT:%.*]] = select i1 [[CMP]], <2 x float> <float 0xFFF8000000000000, float 0xFFF8000000000000>, <2 x float> <float 0x7FF8000100000000, float 0x7FF8000100000000>
330; CHECK-NEXT: [[FABS:%.*]] = call <2 x float> @llvm.fabs.v2f32(<2 x float> [[SELECT]])
331; CHECK-NEXT: ret <2 x float> [[FABS]]
332;
333 %cmp = icmp eq i32 %c, 0
334 %select = select i1 %cmp, <2 x float> <float 0xFFF8000000000000, float 0xFFF8000000000000>, <2 x float> <float 0x7FF8000100000000, float 0x7FF8000100000000>
335 %fabs = call <2 x float> @llvm.fabs.v2f32(<2 x float> %select)
336 ret <2 x float> %fabs
337}
338
Craig Topper2b8f0512018-02-25 06:51:51 +0000339define float @fabs_select_negnan_negzero(i32 %c) {
Sanjay Patelb13fcd52018-02-13 18:22:53 +0000340; CHECK-LABEL: @fabs_select_negnan_negzero(
341; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[C:%.*]], 0
342; CHECK-NEXT: [[SELECT:%.*]] = select i1 [[CMP]], float 0xFFF8000000000000, float -0.000000e+00
343; CHECK-NEXT: [[FABS:%.*]] = call float @llvm.fabs.f32(float [[SELECT]])
344; CHECK-NEXT: ret float [[FABS]]
345;
Matt Arsenault1e0edbf2017-01-11 00:33:24 +0000346 %cmp = icmp eq i32 %c, 0
347 %select = select i1 %cmp, float 0xFFF8000000000000, float -0.0
348 %fabs = call float @llvm.fabs.f32(float %select)
349 ret float %fabs
350}
351
Craig Topperaee341e2018-02-25 06:51:52 +0000352define <2 x float> @fabs_select_negnan_negzero_vector(i32 %c) {
353; CHECK-LABEL: @fabs_select_negnan_negzero_vector(
354; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[C:%.*]], 0
355; CHECK-NEXT: [[SELECT:%.*]] = select i1 [[CMP]], <2 x float> <float 0xFFF8000000000000, float 0xFFF8000000000000>, <2 x float> <float -0.000000e+00, float -0.000000e+00>
356; CHECK-NEXT: [[FABS:%.*]] = call <2 x float> @llvm.fabs.v2f32(<2 x float> [[SELECT]])
357; CHECK-NEXT: ret <2 x float> [[FABS]]
358;
359 %cmp = icmp eq i32 %c, 0
360 %select = select i1 %cmp, <2 x float> <float 0xFFF8000000000000, float 0xFFF8000000000000>, <2 x float> <float -0.0, float -0.0>
361 %fabs = call <2 x float> @llvm.fabs.v2f32(<2 x float> %select)
362 ret <2 x float> %fabs
363}
364
Craig Topper2b8f0512018-02-25 06:51:51 +0000365define float @fabs_select_negnan_zero(i32 %c) {
Sanjay Patelb13fcd52018-02-13 18:22:53 +0000366; CHECK-LABEL: @fabs_select_negnan_zero(
367; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[C:%.*]], 0
368; CHECK-NEXT: [[SELECT:%.*]] = select i1 [[CMP]], float 0xFFF8000000000000, float 0.000000e+00
369; CHECK-NEXT: [[FABS:%.*]] = call float @llvm.fabs.f32(float [[SELECT]])
370; CHECK-NEXT: ret float [[FABS]]
371;
Matt Arsenault1e0edbf2017-01-11 00:33:24 +0000372 %cmp = icmp eq i32 %c, 0
373 %select = select i1 %cmp, float 0xFFF8000000000000, float 0.0
374 %fabs = call float @llvm.fabs.f32(float %select)
375 ret float %fabs
376}
Justin Lebar7e3184c2017-01-26 00:10:26 +0000377
Craig Topperaee341e2018-02-25 06:51:52 +0000378define <2 x float> @fabs_select_negnan_zero_vector(i32 %c) {
379; CHECK-LABEL: @fabs_select_negnan_zero_vector(
380; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[C:%.*]], 0
381; CHECK-NEXT: [[SELECT:%.*]] = select i1 [[CMP]], <2 x float> <float 0xFFF8000000000000, float 0xFFF8000000000000>, <2 x float> zeroinitializer
382; CHECK-NEXT: [[FABS:%.*]] = call <2 x float> @llvm.fabs.v2f32(<2 x float> [[SELECT]])
383; CHECK-NEXT: ret <2 x float> [[FABS]]
384;
385 %cmp = icmp eq i32 %c, 0
386 %select = select i1 %cmp, <2 x float> <float 0xFFF8000000000000, float 0xFFF8000000000000>, <2 x float> <float 0.0, float 0.0>
387 %fabs = call <2 x float> @llvm.fabs.v2f32(<2 x float> %select)
388 ret <2 x float> %fabs
389}
390
Justin Lebar7e3184c2017-01-26 00:10:26 +0000391; The fabs can't be eliminated because llvm.sqrt.f32 may return -0 or NaN with
392; an arbitrary sign bit.
Sanjay Patelb13fcd52018-02-13 18:22:53 +0000393define float @fabs_sqrt(float %a) {
394; CHECK-LABEL: @fabs_sqrt(
395; CHECK-NEXT: [[SQRT:%.*]] = call float @llvm.sqrt.f32(float [[A:%.*]])
396; CHECK-NEXT: [[FABS:%.*]] = call float @llvm.fabs.f32(float [[SQRT]])
397; CHECK-NEXT: ret float [[FABS]]
398;
Justin Lebar7e3184c2017-01-26 00:10:26 +0000399 %sqrt = call float @llvm.sqrt.f32(float %a)
400 %fabs = call float @llvm.fabs.f32(float %sqrt)
401 ret float %fabs
402}
403
Justin Lebar7e3184c2017-01-26 00:10:26 +0000404; The fabs can't be eliminated because the nnan sqrt may still return -0.
Sanjay Patelb13fcd52018-02-13 18:22:53 +0000405define float @fabs_sqrt_nnan(float %a) {
406; CHECK-LABEL: @fabs_sqrt_nnan(
407; CHECK-NEXT: [[SQRT:%.*]] = call nnan float @llvm.sqrt.f32(float [[A:%.*]])
408; CHECK-NEXT: [[FABS:%.*]] = call float @llvm.fabs.f32(float [[SQRT]])
409; CHECK-NEXT: ret float [[FABS]]
410;
Justin Lebar7e3184c2017-01-26 00:10:26 +0000411 %sqrt = call nnan float @llvm.sqrt.f32(float %a)
412 %fabs = call float @llvm.fabs.f32(float %sqrt)
413 ret float %fabs
414}
415
Justin Lebar7e3184c2017-01-26 00:10:26 +0000416; The fabs can't be eliminated because the nsz sqrt may still return NaN.
Sanjay Patelb13fcd52018-02-13 18:22:53 +0000417define float @fabs_sqrt_nsz(float %a) {
418; CHECK-LABEL: @fabs_sqrt_nsz(
419; CHECK-NEXT: [[SQRT:%.*]] = call nsz float @llvm.sqrt.f32(float [[A:%.*]])
420; CHECK-NEXT: [[FABS:%.*]] = call float @llvm.fabs.f32(float [[SQRT]])
421; CHECK-NEXT: ret float [[FABS]]
422;
Justin Lebar7e3184c2017-01-26 00:10:26 +0000423 %sqrt = call nsz float @llvm.sqrt.f32(float %a)
424 %fabs = call float @llvm.fabs.f32(float %sqrt)
425 ret float %fabs
426}
427
Justin Lebar7e3184c2017-01-26 00:10:26 +0000428; The fabs can be eliminated because we're nsz and nnan.
Sanjay Patelb13fcd52018-02-13 18:22:53 +0000429define float @fabs_sqrt_nnan_nsz(float %a) {
430; CHECK-LABEL: @fabs_sqrt_nnan_nsz(
431; CHECK-NEXT: [[SQRT:%.*]] = call nnan nsz float @llvm.sqrt.f32(float [[A:%.*]])
432; CHECK-NEXT: ret float [[SQRT]]
433;
Justin Lebar7e3184c2017-01-26 00:10:26 +0000434 %sqrt = call nnan nsz float @llvm.sqrt.f32(float %a)
435 %fabs = call float @llvm.fabs.f32(float %sqrt)
436 ret float %fabs
437}
438
Justin Lebar7e3184c2017-01-26 00:10:26 +0000439; The second fabs can be eliminated because the operand to sqrt cannot be -0.
Sanjay Patelb13fcd52018-02-13 18:22:53 +0000440define float @fabs_sqrt_nnan_fabs(float %a) {
441; CHECK-LABEL: @fabs_sqrt_nnan_fabs(
442; CHECK-NEXT: [[B:%.*]] = call float @llvm.fabs.f32(float [[A:%.*]])
443; CHECK-NEXT: [[SQRT:%.*]] = call nnan float @llvm.sqrt.f32(float [[B]])
444; CHECK-NEXT: ret float [[SQRT]]
445;
Justin Lebar7e3184c2017-01-26 00:10:26 +0000446 %b = call float @llvm.fabs.f32(float %a)
447 %sqrt = call nnan float @llvm.sqrt.f32(float %b)
448 %fabs = call float @llvm.fabs.f32(float %sqrt)
449 ret float %fabs
450}
Craig Topper30199102018-02-27 19:53:45 +0000451
452define float @fabs_select_positive_constants_vector_extract(i32 %c) {
453; CHECK-LABEL: @fabs_select_positive_constants_vector_extract(
454; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[C:%.*]], 0
455; CHECK-NEXT: [[SELECT:%.*]] = select i1 [[CMP]], <2 x float> <float 1.000000e+00, float 1.000000e+00>, <2 x float> <float 2.000000e+00, float 2.000000e+00>
456; CHECK-NEXT: [[EXTRACT:%.*]] = extractelement <2 x float> [[SELECT]], i32 0
457; CHECK-NEXT: ret float [[EXTRACT]]
458;
459 %cmp = icmp eq i32 %c, 0
460 %select = select i1 %cmp, <2 x float> <float 1.0, float 1.0>, <2 x float> <float 2.0, float 2.0>
461 %extract = extractelement <2 x float> %select, i32 0
462 %fabs = call float @llvm.fabs.f32(float %extract)
463 ret float %fabs
464}
Sanjay Patelef71b702018-07-15 14:46:48 +0000465
Sanjay Patel28c7e412018-08-01 23:05:55 +0000466declare float @llvm.minnum.f32(float, float)
467declare float @llvm.maxnum.f32(float, float)
Sanjay Patelef71b702018-07-15 14:46:48 +0000468declare double @llvm.minnum.f64(double, double)
469declare double @llvm.maxnum.f64(double, double)
470declare <2 x double> @llvm.minnum.v2f64(<2 x double>, <2 x double>)
471declare <2 x double> @llvm.maxnum.v2f64(<2 x double>, <2 x double>)
472
473; From the LangRef for minnum/maxnum:
Sanjay Patel28c7e412018-08-01 23:05:55 +0000474; "If either operand is a NaN, returns the other non-NaN operand."
Sanjay Patelef71b702018-07-15 14:46:48 +0000475
476define double @maxnum_nan_op0(double %x) {
477; CHECK-LABEL: @maxnum_nan_op0(
Sanjay Patel92d0c1c122018-07-15 14:52:16 +0000478; CHECK-NEXT: ret double [[X:%.*]]
Sanjay Patelef71b702018-07-15 14:46:48 +0000479;
480 %r = call double @llvm.maxnum.f64(double 0x7ff8000000000000, double %x)
481 ret double %r
482}
483
484define double @maxnum_nan_op1(double %x) {
485; CHECK-LABEL: @maxnum_nan_op1(
Sanjay Patel92d0c1c122018-07-15 14:52:16 +0000486; CHECK-NEXT: ret double [[X:%.*]]
Sanjay Patelef71b702018-07-15 14:46:48 +0000487;
488 %r = call double @llvm.maxnum.f64(double %x, double 0x7ff800000000dead)
489 ret double %r
490}
491
492define double @minnum_nan_op0(double %x) {
493; CHECK-LABEL: @minnum_nan_op0(
Sanjay Patel92d0c1c122018-07-15 14:52:16 +0000494; CHECK-NEXT: ret double [[X:%.*]]
Sanjay Patelef71b702018-07-15 14:46:48 +0000495;
496 %r = call double @llvm.minnum.f64(double 0x7ff8000dead00000, double %x)
497 ret double %r
498}
499
500define double @minnum_nan_op1(double %x) {
501; CHECK-LABEL: @minnum_nan_op1(
Sanjay Patel92d0c1c122018-07-15 14:52:16 +0000502; CHECK-NEXT: ret double [[X:%.*]]
Sanjay Patelef71b702018-07-15 14:46:48 +0000503;
504 %r = call double @llvm.minnum.f64(double %x, double 0x7ff800dead00dead)
505 ret double %r
506}
507
508define <2 x double> @maxnum_nan_op0_vec(<2 x double> %x) {
509; CHECK-LABEL: @maxnum_nan_op0_vec(
Sanjay Patel92d0c1c122018-07-15 14:52:16 +0000510; CHECK-NEXT: ret <2 x double> [[X:%.*]]
Sanjay Patelef71b702018-07-15 14:46:48 +0000511;
512 %r = call <2 x double> @llvm.maxnum.v2f64(<2 x double> <double 0x7ff8000000000000, double undef>, <2 x double> %x)
513 ret <2 x double> %r
514}
515
516define <2 x double> @maxnum_nan_op1_vec(<2 x double> %x) {
517; CHECK-LABEL: @maxnum_nan_op1_vec(
Sanjay Patel92d0c1c122018-07-15 14:52:16 +0000518; CHECK-NEXT: ret <2 x double> [[X:%.*]]
Sanjay Patelef71b702018-07-15 14:46:48 +0000519;
520 %r = call <2 x double> @llvm.maxnum.v2f64(<2 x double> %x, <2 x double> <double 0x7ff800000000dead, double 0x7ff8ffffffffffff>)
521 ret <2 x double> %r
522}
523
524define <2 x double> @minnum_nan_op0_vec(<2 x double> %x) {
525; CHECK-LABEL: @minnum_nan_op0_vec(
Sanjay Patel92d0c1c122018-07-15 14:52:16 +0000526; CHECK-NEXT: ret <2 x double> [[X:%.*]]
Sanjay Patelef71b702018-07-15 14:46:48 +0000527;
528 %r = call <2 x double> @llvm.minnum.v2f64(<2 x double> <double undef, double 0x7ff8000dead00000>, <2 x double> %x)
529 ret <2 x double> %r
530}
531
532define <2 x double> @minnum_nan_op1_vec(<2 x double> %x) {
533; CHECK-LABEL: @minnum_nan_op1_vec(
Sanjay Patel92d0c1c122018-07-15 14:52:16 +0000534; CHECK-NEXT: ret <2 x double> [[X:%.*]]
Sanjay Patelef71b702018-07-15 14:46:48 +0000535;
536 %r = call <2 x double> @llvm.minnum.v2f64(<2 x double> %x, <2 x double> <double 0x7ff800dead00dead, double 0x7ff800dead00dead>)
537 ret <2 x double> %r
538}
539
Sanjay Patel28c7e412018-08-01 23:05:55 +0000540define float @minnum_same_args(float %x) {
541; CHECK-LABEL: @minnum_same_args(
542; CHECK-NEXT: ret float [[X:%.*]]
543;
544 %y = call float @llvm.minnum.f32(float %x, float %x)
545 ret float %y
546}
547
548define float @maxnum_same_args(float %x) {
549; CHECK-LABEL: @maxnum_same_args(
550; CHECK-NEXT: ret float [[X:%.*]]
551;
552 %y = call float @llvm.maxnum.f32(float %x, float %x)
553 ret float %y
554}
555