fneg.ll
12.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
; RUN: opt < %s -instcombine -S | FileCheck %s
declare void @use(float)
define float @fneg_fneg(float %a) {
;
; CHECK-LABEL: @fneg_fneg(
; CHECK-NEXT: ret float [[A:%.*]]
;
%f = fneg float %a
%r = fneg float %f
ret float %r
}
; -(X * C) --> X * (-C)
define float @fmul_fsub(float %x) {
; CHECK-LABEL: @fmul_fsub(
; CHECK-NEXT: [[R:%.*]] = fmul float [[X:%.*]], -4.200000e+01
; CHECK-NEXT: ret float [[R]]
;
%m = fmul float %x, 42.0
%r = fsub float -0.0, %m
ret float %r
}
define float @fmul_fneg(float %x) {
; CHECK-LABEL: @fmul_fneg(
; CHECK-NEXT: [[R:%.*]] = fmul float [[X:%.*]], -4.200000e+01
; CHECK-NEXT: ret float [[R]]
;
%m = fmul float %x, 42.0
%r = fneg float %m
ret float %r
}
; Fast math is not required, but it should be propagated.
define float @fmul_fsub_fmf(float %x) {
; CHECK-LABEL: @fmul_fsub_fmf(
; CHECK-NEXT: [[R:%.*]] = fmul reassoc nsz float [[X:%.*]], -4.200000e+01
; CHECK-NEXT: ret float [[R]]
;
%m = fmul float %x, 42.0
%r = fsub reassoc nsz float -0.0, %m
ret float %r
}
define float @fmul_fneg_fmf(float %x) {
; CHECK-LABEL: @fmul_fneg_fmf(
; CHECK-NEXT: [[R:%.*]] = fmul reassoc nsz float [[X:%.*]], -4.200000e+01
; CHECK-NEXT: ret float [[R]]
;
%m = fmul float %x, 42.0
%r = fneg reassoc nsz float %m
ret float %r
}
; Extra use prevents the fold. We don't want to replace the fneg with an fmul.
define float @fmul_fsub_extra_use(float %x) {
; CHECK-LABEL: @fmul_fsub_extra_use(
; CHECK-NEXT: [[M:%.*]] = fmul float [[X:%.*]], 4.200000e+01
; CHECK-NEXT: [[R:%.*]] = fsub float -0.000000e+00, [[M]]
; CHECK-NEXT: call void @use(float [[M]])
; CHECK-NEXT: ret float [[R]]
;
%m = fmul float %x, 42.0
%r = fsub float -0.0, %m
call void @use(float %m)
ret float %r
}
define float @fmul_fneg_extra_use(float %x) {
; CHECK-LABEL: @fmul_fneg_extra_use(
; CHECK-NEXT: [[M:%.*]] = fmul float [[X:%.*]], 4.200000e+01
; CHECK-NEXT: [[R:%.*]] = fneg float [[M]]
; CHECK-NEXT: call void @use(float [[M]])
; CHECK-NEXT: ret float [[R]]
;
%m = fmul float %x, 42.0
%r = fneg float %m
call void @use(float %m)
ret float %r
}
; Try a vector. Use special constants (NaN, INF, undef) because they don't change anything.
define <4 x double> @fmul_fsub_vec(<4 x double> %x) {
; CHECK-LABEL: @fmul_fsub_vec(
; CHECK-NEXT: [[R:%.*]] = fmul <4 x double> [[X:%.*]], <double -4.200000e+01, double 0x7F80000000000000, double 0xFFF0000000000000, double undef>
; CHECK-NEXT: ret <4 x double> [[R]]
;
%m = fmul <4 x double> %x, <double 42.0, double 0x7FF80000000000000, double 0x7FF0000000000000, double undef>
%r = fsub <4 x double> <double -0.0, double -0.0, double -0.0, double -0.0>, %m
ret <4 x double> %r
}
define <4 x double> @fmul_fneg_vec(<4 x double> %x) {
; CHECK-LABEL: @fmul_fneg_vec(
; CHECK-NEXT: [[R:%.*]] = fmul <4 x double> [[X:%.*]], <double -4.200000e+01, double 0x7F80000000000000, double 0xFFF0000000000000, double undef>
; CHECK-NEXT: ret <4 x double> [[R]]
;
%m = fmul <4 x double> %x, <double 42.0, double 0x7FF80000000000000, double 0x7FF0000000000000, double undef>
%r = fneg <4 x double> %m
ret <4 x double> %r
}
; -(X / C) --> X / (-C)
define float @fdiv_op1_constant_fsub(float %x) {
; CHECK-LABEL: @fdiv_op1_constant_fsub(
; CHECK-NEXT: [[R:%.*]] = fdiv float [[X:%.*]], 4.200000e+01
; CHECK-NEXT: ret float [[R]]
;
%d = fdiv float %x, -42.0
%r = fsub float -0.0, %d
ret float %r
}
define float @fdiv_op1_constant_fneg(float %x) {
; CHECK-LABEL: @fdiv_op1_constant_fneg(
; CHECK-NEXT: [[R:%.*]] = fdiv float [[X:%.*]], 4.200000e+01
; CHECK-NEXT: ret float [[R]]
;
%d = fdiv float %x, -42.0
%r = fneg float %d
ret float %r
}
; Fast math is not required, but it should be propagated.
define float @fdiv_op1_constant_fsub_fmf(float %x) {
; CHECK-LABEL: @fdiv_op1_constant_fsub_fmf(
; CHECK-NEXT: [[R:%.*]] = fdiv nnan float [[X:%.*]], 4.200000e+01
; CHECK-NEXT: ret float [[R]]
;
%d = fdiv float %x, -42.0
%r = fsub nnan float -0.0, %d
ret float %r
}
define float @fdiv_op1_constant_fneg_fmf(float %x) {
; CHECK-LABEL: @fdiv_op1_constant_fneg_fmf(
; CHECK-NEXT: [[R:%.*]] = fdiv nnan float [[X:%.*]], 4.200000e+01
; CHECK-NEXT: ret float [[R]]
;
%d = fdiv float %x, -42.0
%r = fneg nnan float %d
ret float %r
}
; Extra use prevents the fold. We don't want to replace the fneg with an fdiv.
define float @fdiv_op1_constant_fsub_extra_use(float %x) {
; CHECK-LABEL: @fdiv_op1_constant_fsub_extra_use(
; CHECK-NEXT: [[D:%.*]] = fdiv float [[X:%.*]], 4.200000e+01
; CHECK-NEXT: [[R:%.*]] = fsub float -0.000000e+00, [[D]]
; CHECK-NEXT: call void @use(float [[D]])
; CHECK-NEXT: ret float [[R]]
;
%d = fdiv float %x, 42.0
%r = fsub float -0.0, %d
call void @use(float %d)
ret float %r
}
define float @fdiv_op1_constant_fneg_extra_use(float %x) {
; CHECK-LABEL: @fdiv_op1_constant_fneg_extra_use(
; CHECK-NEXT: [[D:%.*]] = fdiv float [[X:%.*]], 4.200000e+01
; CHECK-NEXT: [[R:%.*]] = fneg float [[D]]
; CHECK-NEXT: call void @use(float [[D]])
; CHECK-NEXT: ret float [[R]]
;
%d = fdiv float %x, 42.0
%r = fneg float %d
call void @use(float %d)
ret float %r
}
; Try a vector. Use special constants (NaN, INF, undef) because they don't change anything.
define <4 x double> @fdiv_op1_constant_fsub_vec(<4 x double> %x) {
; CHECK-LABEL: @fdiv_op1_constant_fsub_vec(
; CHECK-NEXT: [[R:%.*]] = fdiv <4 x double> [[X:%.*]], <double 4.200000e+01, double 0x7FF800000ABCD000, double 0x7FF0000000000000, double undef>
; CHECK-NEXT: ret <4 x double> [[R]]
;
%d = fdiv <4 x double> %x, <double -42.0, double 0xFFF800000ABCD000, double 0xFFF0000000000000, double undef>
%r = fsub <4 x double> <double -0.0, double -0.0, double -0.0, double -0.0>, %d
ret <4 x double> %r
}
define <4 x double> @fdiv_op1_constant_fneg_vec(<4 x double> %x) {
; CHECK-LABEL: @fdiv_op1_constant_fneg_vec(
; CHECK-NEXT: [[R:%.*]] = fdiv <4 x double> [[X:%.*]], <double 4.200000e+01, double 0x7FF800000ABCD000, double 0x7FF0000000000000, double undef>
; CHECK-NEXT: ret <4 x double> [[R]]
;
%d = fdiv <4 x double> %x, <double -42.0, double 0xFFF800000ABCD000, double 0xFFF0000000000000, double undef>
%r = fneg <4 x double> %d
ret <4 x double> %r
}
; -(C / X) --> (-C) / X
define float @fdiv_op0_constant_fsub(float %x) {
; CHECK-LABEL: @fdiv_op0_constant_fsub(
; CHECK-NEXT: [[R:%.*]] = fdiv float -4.200000e+01, [[X:%.*]]
; CHECK-NEXT: ret float [[R]]
;
%d = fdiv float 42.0, %x
%r = fsub float -0.0, %d
ret float %r
}
define float @fdiv_op0_constant_fneg(float %x) {
; CHECK-LABEL: @fdiv_op0_constant_fneg(
; CHECK-NEXT: [[R:%.*]] = fdiv float -4.200000e+01, [[X:%.*]]
; CHECK-NEXT: ret float [[R]]
;
%d = fdiv float 42.0, %x
%r = fneg float %d
ret float %r
}
; Fast math is not required, but it should be propagated.
define float @fdiv_op0_constant_fsub_fmf(float %x) {
; CHECK-LABEL: @fdiv_op0_constant_fsub_fmf(
; CHECK-NEXT: [[R:%.*]] = fdiv fast float -4.200000e+01, [[X:%.*]]
; CHECK-NEXT: ret float [[R]]
;
%d = fdiv float 42.0, %x
%r = fsub fast float -0.0, %d
ret float %r
}
define float @fdiv_op0_constant_fneg_fmf(float %x) {
; CHECK-LABEL: @fdiv_op0_constant_fneg_fmf(
; CHECK-NEXT: [[R:%.*]] = fdiv fast float -4.200000e+01, [[X:%.*]]
; CHECK-NEXT: ret float [[R]]
;
%d = fdiv float 42.0, %x
%r = fneg fast float %d
ret float %r
}
; Extra use prevents the fold. We don't want to replace the fneg with an fdiv.
define float @fdiv_op0_constant_fsub_extra_use(float %x) {
; CHECK-LABEL: @fdiv_op0_constant_fsub_extra_use(
; CHECK-NEXT: [[D:%.*]] = fdiv float -4.200000e+01, [[X:%.*]]
; CHECK-NEXT: [[R:%.*]] = fsub float -0.000000e+00, [[D]]
; CHECK-NEXT: call void @use(float [[D]])
; CHECK-NEXT: ret float [[R]]
;
%d = fdiv float -42.0, %x
%r = fsub float -0.0, %d
call void @use(float %d)
ret float %r
}
define float @fdiv_op0_constant_fneg_extra_use(float %x) {
; CHECK-LABEL: @fdiv_op0_constant_fneg_extra_use(
; CHECK-NEXT: [[D:%.*]] = fdiv float -4.200000e+01, [[X:%.*]]
; CHECK-NEXT: [[R:%.*]] = fneg float [[D]]
; CHECK-NEXT: call void @use(float [[D]])
; CHECK-NEXT: ret float [[R]]
;
%d = fdiv float -42.0, %x
%r = fneg float %d
call void @use(float %d)
ret float %r
}
; Try a vector. Use special constants (NaN, INF, undef) because they don't change anything.
define <4 x double> @fdiv_op0_constant_fsub_vec(<4 x double> %x) {
; CHECK-LABEL: @fdiv_op0_constant_fsub_vec(
; CHECK-NEXT: [[R:%.*]] = fdiv <4 x double> <double 4.200000e+01, double 0x7F80000000000000, double 0x7FF0000000000000, double undef>, [[X:%.*]]
; CHECK-NEXT: ret <4 x double> [[R]]
;
%d = fdiv <4 x double> <double -42.0, double 0x7FF80000000000000, double 0xFFF0000000000000, double undef>, %x
%r = fsub <4 x double> <double -0.0, double -0.0, double -0.0, double -0.0>, %d
ret <4 x double> %r
}
define <4 x double> @fdiv_op0_constant_fneg_vec(<4 x double> %x) {
; CHECK-LABEL: @fdiv_op0_constant_fneg_vec(
; CHECK-NEXT: [[R:%.*]] = fdiv <4 x double> <double 4.200000e+01, double 0x7F80000000000000, double 0x7FF0000000000000, double undef>, [[X:%.*]]
; CHECK-NEXT: ret <4 x double> [[R]]
;
%d = fdiv <4 x double> <double -42.0, double 0x7FF80000000000000, double 0xFFF0000000000000, double undef>, %x
%r = fneg <4 x double> %d
ret <4 x double> %r
}
; Sink FP negation through a select:
; c ? -x : -y --> -(c ? x : y)
define <2 x double> @fneg_fneg_sel(<2 x double> %x, <2 x double> %y, i1 %cond) {
; CHECK-LABEL: @fneg_fneg_sel(
; CHECK-NEXT: [[SEL_V:%.*]] = select i1 [[COND:%.*]], <2 x double> [[X:%.*]], <2 x double> [[Y:%.*]]
; CHECK-NEXT: [[SEL:%.*]] = fneg <2 x double> [[SEL_V]]
; CHECK-NEXT: ret <2 x double> [[SEL]]
;
%n1 = fneg <2 x double> %x
%n2 = fneg <2 x double> %y
%sel = select i1 %cond, <2 x double> %n1, <2 x double> %n2
ret <2 x double> %sel
}
; An extra use is allowed.
define float @fneg_fneg_sel_extra_use1(float %x, float %y, i1 %cond) {
; CHECK-LABEL: @fneg_fneg_sel_extra_use1(
; CHECK-NEXT: [[N1:%.*]] = fneg float [[X:%.*]]
; CHECK-NEXT: call void @use(float [[N1]])
; CHECK-NEXT: [[SEL_V:%.*]] = select i1 [[COND:%.*]], float [[X]], float [[Y:%.*]]
; CHECK-NEXT: [[SEL:%.*]] = fneg float [[SEL_V]]
; CHECK-NEXT: ret float [[SEL]]
;
%n1 = fneg float %x
call void @use(float %n1)
%n2 = fneg float %y
%sel = select i1 %cond, float %n1, float %n2
ret float %sel
}
define float @fneg_fneg_sel_extra_use2(float %x, float %y, i1 %cond) {
; CHECK-LABEL: @fneg_fneg_sel_extra_use2(
; CHECK-NEXT: [[N2:%.*]] = fneg float [[Y:%.*]]
; CHECK-NEXT: call void @use(float [[N2]])
; CHECK-NEXT: [[SEL_V:%.*]] = select i1 [[COND:%.*]], float [[X:%.*]], float [[Y]]
; CHECK-NEXT: [[SEL:%.*]] = fneg float [[SEL_V]]
; CHECK-NEXT: ret float [[SEL]]
;
%n1 = fneg float %x
%n2 = fneg float %y
call void @use(float %n2)
%sel = select i1 %cond, float %n1, float %n2
ret float %sel
}
; Legacy form of fneg should work too.
define float @fsub_fsub_sel_extra_use1(float %x, float %y, i1 %cond) {
; CHECK-LABEL: @fsub_fsub_sel_extra_use1(
; CHECK-NEXT: [[N1:%.*]] = fsub float -0.000000e+00, [[X:%.*]]
; CHECK-NEXT: call void @use(float [[N1]])
; CHECK-NEXT: [[SEL_V:%.*]] = select i1 [[COND:%.*]], float [[X]], float [[Y:%.*]]
; CHECK-NEXT: [[SEL:%.*]] = fsub float -0.000000e+00, [[SEL_V]]
; CHECK-NEXT: ret float [[SEL]]
;
%n1 = fsub float -0.0, %x
call void @use(float %n1)
%n2 = fsub float -0.0, %y
%sel = select i1 %cond, float %n1, float %n2
ret float %sel
}
; Negative test: but 2 extra uses would require an extra instruction.
define float @fneg_fneg_sel_extra_use3(float %x, float %y, i1 %cond) {
; CHECK-LABEL: @fneg_fneg_sel_extra_use3(
; CHECK-NEXT: [[N1:%.*]] = fneg float [[X:%.*]]
; CHECK-NEXT: call void @use(float [[N1]])
; CHECK-NEXT: [[N2:%.*]] = fneg float [[Y:%.*]]
; CHECK-NEXT: call void @use(float [[N2]])
; CHECK-NEXT: [[SEL:%.*]] = select i1 [[COND:%.*]], float [[N1]], float [[N2]]
; CHECK-NEXT: ret float [[SEL]]
;
%n1 = fneg float %x
call void @use(float %n1)
%n2 = fneg float %y
call void @use(float %n2)
%sel = select i1 %cond, float %n1, float %n2
ret float %sel
}