1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -instcombine -S | FileCheck %s
6 define float @test1(float %x, float %y) {
8 ; CHECK-NEXT: [[T1:%.*]] = fsub float [[X:%.*]], [[Y:%.*]]
9 ; CHECK-NEXT: [[T2:%.*]] = fsub float -0.000000e+00, [[T1]]
10 ; CHECK-NEXT: ret float [[T2]]
12 %t1 = fsub float %x, %y
13 %t2 = fsub float -0.0, %t1
17 define float @test1_unary(float %x, float %y) {
18 ; CHECK-LABEL: @test1_unary(
19 ; CHECK-NEXT: [[T1:%.*]] = fsub float [[X:%.*]], [[Y:%.*]]
20 ; CHECK-NEXT: [[T2:%.*]] = fneg float [[T1]]
21 ; CHECK-NEXT: ret float [[T2]]
23 %t1 = fsub float %x, %y
28 ; Can't do anything with the test above because -0.0 - 0.0 = -0.0, but if we have nsz:
31 define float @neg_sub_nsz(float %x, float %y) {
32 ; CHECK-LABEL: @neg_sub_nsz(
33 ; CHECK-NEXT: [[TMP1:%.*]] = fsub nsz float [[Y:%.*]], [[X:%.*]]
34 ; CHECK-NEXT: ret float [[TMP1]]
36 %t1 = fsub float %x, %y
37 %t2 = fsub nsz float -0.0, %t1
41 define float @unary_neg_sub_nsz(float %x, float %y) {
42 ; CHECK-LABEL: @unary_neg_sub_nsz(
43 ; CHECK-NEXT: [[T2:%.*]] = fsub nsz float [[Y:%.*]], [[X:%.*]]
44 ; CHECK-NEXT: ret float [[T2]]
46 %t1 = fsub float %x, %y
47 %t2 = fneg nsz float %t1
51 ; If the subtract has another use, we don't do the transform (even though it
52 ; doesn't increase the IR instruction count) because we assume that fneg is
53 ; easier to analyze and generally cheaper than generic fsub.
55 declare void @use(float)
56 declare void @use2(float, double)
58 define float @neg_sub_nsz_extra_use(float %x, float %y) {
59 ; CHECK-LABEL: @neg_sub_nsz_extra_use(
60 ; CHECK-NEXT: [[T1:%.*]] = fsub float [[X:%.*]], [[Y:%.*]]
61 ; CHECK-NEXT: [[T2:%.*]] = fsub nsz float -0.000000e+00, [[T1]]
62 ; CHECK-NEXT: call void @use(float [[T1]])
63 ; CHECK-NEXT: ret float [[T2]]
65 %t1 = fsub float %x, %y
66 %t2 = fsub nsz float -0.0, %t1
67 call void @use(float %t1)
71 define float @unary_neg_sub_nsz_extra_use(float %x, float %y) {
72 ; CHECK-LABEL: @unary_neg_sub_nsz_extra_use(
73 ; CHECK-NEXT: [[T1:%.*]] = fsub float [[X:%.*]], [[Y:%.*]]
74 ; CHECK-NEXT: [[T2:%.*]] = fneg nsz float [[T1]]
75 ; CHECK-NEXT: call void @use(float [[T1]])
76 ; CHECK-NEXT: ret float [[T2]]
78 %t1 = fsub float %x, %y
79 %t2 = fneg nsz float %t1
80 call void @use(float %t1)
84 ; With nsz: Z - (X - Y) --> Z + (Y - X)
86 define float @sub_sub_nsz(float %x, float %y, float %z) {
87 ; CHECK-LABEL: @sub_sub_nsz(
88 ; CHECK-NEXT: [[TMP1:%.*]] = fsub nsz float [[Y:%.*]], [[X:%.*]]
89 ; CHECK-NEXT: [[T2:%.*]] = fadd nsz float [[TMP1]], [[Z:%.*]]
90 ; CHECK-NEXT: ret float [[T2]]
92 %t1 = fsub float %x, %y
93 %t2 = fsub nsz float %z, %t1
97 ; With nsz and reassoc: Y - ((X * 5) + Y) --> X * -5
99 define float @sub_add_neg_x(float %x, float %y) {
100 ; CHECK-LABEL: @sub_add_neg_x(
101 ; CHECK-NEXT: [[R:%.*]] = fmul reassoc nsz float [[X:%.*]], -5.000000e+00
102 ; CHECK-NEXT: ret float [[R]]
104 %mul = fmul float %x, 5.000000e+00
105 %add = fadd float %mul, %y
106 %r = fsub nsz reassoc float %y, %add
110 ; Same as above: if 'Z' is not -0.0, swap fsub operands and convert to fadd.
112 define float @sub_sub_known_not_negzero(float %x, float %y) {
113 ; CHECK-LABEL: @sub_sub_known_not_negzero(
114 ; CHECK-NEXT: [[TMP1:%.*]] = fsub float [[Y:%.*]], [[X:%.*]]
115 ; CHECK-NEXT: [[T2:%.*]] = fadd float [[TMP1]], 4.200000e+01
116 ; CHECK-NEXT: ret float [[T2]]
118 %t1 = fsub float %x, %y
119 %t2 = fsub float 42.0, %t1
123 ; <rdar://problem/7530098>
125 define double @test2(double %x, double %y) {
126 ; CHECK-LABEL: @test2(
127 ; CHECK-NEXT: [[T1:%.*]] = fadd double [[X:%.*]], [[Y:%.*]]
128 ; CHECK-NEXT: [[T2:%.*]] = fsub double [[X]], [[T1]]
129 ; CHECK-NEXT: ret double [[T2]]
131 %t1 = fadd double %x, %y
132 %t2 = fsub double %x, %t1
138 define float @constant_op1(float %x, float %y) {
139 ; CHECK-LABEL: @constant_op1(
140 ; CHECK-NEXT: [[R:%.*]] = fadd float [[X:%.*]], -4.200000e+01
141 ; CHECK-NEXT: ret float [[R]]
143 %r = fsub float %x, 42.0
147 define <2 x float> @constant_op1_vec(<2 x float> %x, <2 x float> %y) {
148 ; CHECK-LABEL: @constant_op1_vec(
149 ; CHECK-NEXT: [[R:%.*]] = fadd <2 x float> [[X:%.*]], <float -4.200000e+01, float 4.200000e+01>
150 ; CHECK-NEXT: ret <2 x float> [[R]]
152 %r = fsub <2 x float> %x, <float 42.0, float -42.0>
156 define <2 x float> @constant_op1_vec_undef(<2 x float> %x, <2 x float> %y) {
157 ; CHECK-LABEL: @constant_op1_vec_undef(
158 ; CHECK-NEXT: [[R:%.*]] = fadd <2 x float> [[X:%.*]], <float undef, float 4.200000e+01>
159 ; CHECK-NEXT: ret <2 x float> [[R]]
161 %r = fsub <2 x float> %x, <float undef, float -42.0>
167 define float @neg_op1(float %x, float %y) {
168 ; CHECK-LABEL: @neg_op1(
169 ; CHECK-NEXT: [[R:%.*]] = fadd float [[X:%.*]], [[Y:%.*]]
170 ; CHECK-NEXT: ret float [[R]]
172 %negy = fsub float -0.0, %y
173 %r = fsub float %x, %negy
177 define float @unary_neg_op1(float %x, float %y) {
178 ; CHECK-LABEL: @unary_neg_op1(
179 ; CHECK-NEXT: [[R:%.*]] = fadd float [[X:%.*]], [[Y:%.*]]
180 ; CHECK-NEXT: ret float [[R]]
182 %negy = fneg float %y
183 %r = fsub float %x, %negy
187 define <2 x float> @neg_op1_vec(<2 x float> %x, <2 x float> %y) {
188 ; CHECK-LABEL: @neg_op1_vec(
189 ; CHECK-NEXT: [[R:%.*]] = fadd <2 x float> [[X:%.*]], [[Y:%.*]]
190 ; CHECK-NEXT: ret <2 x float> [[R]]
192 %negy = fsub <2 x float> <float -0.0, float -0.0>, %y
193 %r = fsub <2 x float> %x, %negy
197 define <2 x float> @unary_neg_op1_vec(<2 x float> %x, <2 x float> %y) {
198 ; CHECK-LABEL: @unary_neg_op1_vec(
199 ; CHECK-NEXT: [[R:%.*]] = fadd <2 x float> [[X:%.*]], [[Y:%.*]]
200 ; CHECK-NEXT: ret <2 x float> [[R]]
202 %negy = fneg <2 x float> %y
203 %r = fsub <2 x float> %x, %negy
207 define <2 x float> @neg_op1_vec_undef(<2 x float> %x, <2 x float> %y) {
208 ; CHECK-LABEL: @neg_op1_vec_undef(
209 ; CHECK-NEXT: [[R:%.*]] = fadd <2 x float> [[X:%.*]], [[Y:%.*]]
210 ; CHECK-NEXT: ret <2 x float> [[R]]
212 %negy = fsub <2 x float> <float -0.0, float undef>, %y
213 %r = fsub <2 x float> %x, %negy
217 ; Similar to above - but look through fpext/fptrunc casts to find the fneg.
219 define double @neg_ext_op1(float %a, double %b) {
220 ; CHECK-LABEL: @neg_ext_op1(
221 ; CHECK-NEXT: [[TMP1:%.*]] = fpext float [[A:%.*]] to double
222 ; CHECK-NEXT: [[T3:%.*]] = fadd double [[TMP1]], [[B:%.*]]
223 ; CHECK-NEXT: ret double [[T3]]
225 %t1 = fsub float -0.0, %a
226 %t2 = fpext float %t1 to double
227 %t3 = fsub double %b, %t2
231 define double @unary_neg_ext_op1(float %a, double %b) {
232 ; CHECK-LABEL: @unary_neg_ext_op1(
233 ; CHECK-NEXT: [[TMP1:%.*]] = fpext float [[A:%.*]] to double
234 ; CHECK-NEXT: [[T3:%.*]] = fadd double [[TMP1]], [[B:%.*]]
235 ; CHECK-NEXT: ret double [[T3]]
238 %t2 = fpext float %t1 to double
239 %t3 = fsub double %b, %t2
243 ; Verify that vectors work too.
245 define <2 x float> @neg_trunc_op1(<2 x double> %a, <2 x float> %b) {
246 ; CHECK-LABEL: @neg_trunc_op1(
247 ; CHECK-NEXT: [[TMP1:%.*]] = fptrunc <2 x double> [[A:%.*]] to <2 x float>
248 ; CHECK-NEXT: [[T3:%.*]] = fadd <2 x float> [[TMP1]], [[B:%.*]]
249 ; CHECK-NEXT: ret <2 x float> [[T3]]
251 %t1 = fsub <2 x double> <double -0.0, double -0.0>, %a
252 %t2 = fptrunc <2 x double> %t1 to <2 x float>
253 %t3 = fsub <2 x float> %b, %t2
257 define <2 x float> @unary_neg_trunc_op1(<2 x double> %a, <2 x float> %b) {
258 ; CHECK-LABEL: @unary_neg_trunc_op1(
259 ; CHECK-NEXT: [[TMP1:%.*]] = fptrunc <2 x double> [[A:%.*]] to <2 x float>
260 ; CHECK-NEXT: [[T3:%.*]] = fadd <2 x float> [[TMP1]], [[B:%.*]]
261 ; CHECK-NEXT: ret <2 x float> [[T3]]
263 %t1 = fneg <2 x double> %a
264 %t2 = fptrunc <2 x double> %t1 to <2 x float>
265 %t3 = fsub <2 x float> %b, %t2
269 ; No FMF needed, but they should propagate to the fadd.
271 define double @neg_ext_op1_fast(float %a, double %b) {
272 ; CHECK-LABEL: @neg_ext_op1_fast(
273 ; CHECK-NEXT: [[TMP1:%.*]] = fpext float [[A:%.*]] to double
274 ; CHECK-NEXT: [[T3:%.*]] = fadd fast double [[TMP1]], [[B:%.*]]
275 ; CHECK-NEXT: ret double [[T3]]
277 %t1 = fsub float -0.0, %a
278 %t2 = fpext float %t1 to double
279 %t3 = fsub fast double %b, %t2
283 define double @unary_neg_ext_op1_fast(float %a, double %b) {
284 ; CHECK-LABEL: @unary_neg_ext_op1_fast(
285 ; CHECK-NEXT: [[TMP1:%.*]] = fpext float [[A:%.*]] to double
286 ; CHECK-NEXT: [[T3:%.*]] = fadd fast double [[TMP1]], [[B:%.*]]
287 ; CHECK-NEXT: ret double [[T3]]
290 %t2 = fpext float %t1 to double
291 %t3 = fsub fast double %b, %t2
295 ; Extra use should prevent the transform.
297 define float @neg_ext_op1_extra_use(half %a, float %b) {
298 ; CHECK-LABEL: @neg_ext_op1_extra_use(
299 ; CHECK-NEXT: [[T1:%.*]] = fsub half 0xH8000, [[A:%.*]]
300 ; CHECK-NEXT: [[T2:%.*]] = fpext half [[T1]] to float
301 ; CHECK-NEXT: [[T3:%.*]] = fsub float [[B:%.*]], [[T2]]
302 ; CHECK-NEXT: call void @use(float [[T2]])
303 ; CHECK-NEXT: ret float [[T3]]
305 %t1 = fsub half -0.0, %a
306 %t2 = fpext half %t1 to float
307 %t3 = fsub float %b, %t2
308 call void @use(float %t2)
312 define float @unary_neg_ext_op1_extra_use(half %a, float %b) {
313 ; CHECK-LABEL: @unary_neg_ext_op1_extra_use(
314 ; CHECK-NEXT: [[T1:%.*]] = fneg half [[A:%.*]]
315 ; CHECK-NEXT: [[T2:%.*]] = fpext half [[T1]] to float
316 ; CHECK-NEXT: [[T3:%.*]] = fsub float [[B:%.*]], [[T2]]
317 ; CHECK-NEXT: call void @use(float [[T2]])
318 ; CHECK-NEXT: ret float [[T3]]
321 %t2 = fpext half %t1 to float
322 %t3 = fsub float %b, %t2
323 call void @use(float %t2)
327 ; One-use fptrunc is always hoisted above fneg, so the corresponding
328 ; multi-use bug for fptrunc isn't visible with a fold starting from
331 define float @neg_trunc_op1_extra_use(double %a, float %b) {
332 ; CHECK-LABEL: @neg_trunc_op1_extra_use(
333 ; CHECK-NEXT: [[TMP1:%.*]] = fptrunc double [[A:%.*]] to float
334 ; CHECK-NEXT: [[T2:%.*]] = fsub float -0.000000e+00, [[TMP1]]
335 ; CHECK-NEXT: [[T3:%.*]] = fadd float [[TMP1]], [[B:%.*]]
336 ; CHECK-NEXT: call void @use(float [[T2]])
337 ; CHECK-NEXT: ret float [[T3]]
339 %t1 = fsub double -0.0, %a
340 %t2 = fptrunc double %t1 to float
341 %t3 = fsub float %b, %t2
342 call void @use(float %t2)
346 define float @unary_neg_trunc_op1_extra_use(double %a, float %b) {
347 ; CHECK-LABEL: @unary_neg_trunc_op1_extra_use(
348 ; CHECK-NEXT: [[TMP1:%.*]] = fptrunc double [[A:%.*]] to float
349 ; CHECK-NEXT: [[T2:%.*]] = fneg float [[TMP1]]
350 ; CHECK-NEXT: [[T3:%.*]] = fadd float [[TMP1]], [[B:%.*]]
351 ; CHECK-NEXT: call void @use(float [[T2]])
352 ; CHECK-NEXT: ret float [[T3]]
355 %t2 = fptrunc double %t1 to float
356 %t3 = fsub float %b, %t2
357 call void @use(float %t2)
361 ; Extra uses should prevent the transform.
363 define float @neg_trunc_op1_extra_uses(double %a, float %b) {
364 ; CHECK-LABEL: @neg_trunc_op1_extra_uses(
365 ; CHECK-NEXT: [[T1:%.*]] = fsub double -0.000000e+00, [[A:%.*]]
366 ; CHECK-NEXT: [[T2:%.*]] = fptrunc double [[T1]] to float
367 ; CHECK-NEXT: [[T3:%.*]] = fsub float [[B:%.*]], [[T2]]
368 ; CHECK-NEXT: call void @use2(float [[T2]], double [[T1]])
369 ; CHECK-NEXT: ret float [[T3]]
371 %t1 = fsub double -0.0, %a
372 %t2 = fptrunc double %t1 to float
373 %t3 = fsub float %b, %t2
374 call void @use2(float %t2, double %t1)
378 define float @unary_neg_trunc_op1_extra_uses(double %a, float %b) {
379 ; CHECK-LABEL: @unary_neg_trunc_op1_extra_uses(
380 ; CHECK-NEXT: [[T1:%.*]] = fneg double [[A:%.*]]
381 ; CHECK-NEXT: [[T2:%.*]] = fptrunc double [[T1]] to float
382 ; CHECK-NEXT: [[T3:%.*]] = fsub float [[B:%.*]], [[T2]]
383 ; CHECK-NEXT: call void @use2(float [[T2]], double [[T1]])
384 ; CHECK-NEXT: ret float [[T3]]
387 %t2 = fptrunc double %t1 to float
388 %t3 = fsub float %b, %t2
389 call void @use2(float %t2, double %t1)
393 ; Don't negate a constant expression to form fadd and induce infinite looping:
394 ; https://bugs.llvm.org/show_bug.cgi?id=37605
396 @b = external global i16, align 1
398 define float @PR37605(float %conv) {
399 ; CHECK-LABEL: @PR37605(
400 ; CHECK-NEXT: [[SUB:%.*]] = fsub float [[CONV:%.*]], bitcast (i32 ptrtoint (i16* @b to i32) to float)
401 ; CHECK-NEXT: ret float [[SUB]]
403 %sub = fsub float %conv, bitcast (i32 ptrtoint (i16* @b to i32) to float)