aboutsummaryrefslogtreecommitdiff
path: root/llvm/test/CodeGen/X86/combine-fneg.ll
blob: 8ca7fb81563faa08dd231127681753803ef949b0 (plain)
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
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
; RUN: llc < %s -mtriple=i686-unknown-unknown -mattr=+sse  | FileCheck %s --check-prefixes=X86-SSE,X86-SSE1
; RUN: llc < %s -mtriple=i686-unknown-unknown -mattr=+sse2 | FileCheck %s --check-prefixes=X86-SSE,X86-SSE2
; RUN: llc < %s -mtriple=x86_64-unknown-unknown -mattr=-sse2 | FileCheck %s --check-prefixes=X64-SSE,X64-SSE1
; RUN: llc < %s -mtriple=x86_64-unknown-unknown -mattr=+sse2 | FileCheck %s --check-prefixes=X64-SSE,X64-SSE2

; FNEG is defined as subtraction from -0.0.

; This test verifies that we use an xor with a constant to flip the sign bits; no subtraction needed.
define <4 x float> @t1(<4 x float> %Q) nounwind {
; X86-SSE-LABEL: t1:
; X86-SSE:       # %bb.0:
; X86-SSE-NEXT:    xorps {{\.?LCPI[0-9]+_[0-9]+}}, %xmm0
; X86-SSE-NEXT:    retl
;
; X64-SSE-LABEL: t1:
; X64-SSE:       # %bb.0:
; X64-SSE-NEXT:    xorps {{\.?LCPI[0-9]+_[0-9]+}}(%rip), %xmm0
; X64-SSE-NEXT:    retq
  %tmp = fsub <4 x float> <float -0.0, float -0.0, float -0.0, float -0.0>, %Q
  ret <4 x float> %tmp
}

; Possibly misplaced test, but since we're checking undef scenarios...

define float @scalar_fsub_neg0_undef(float %x) nounwind {
; X86-SSE-LABEL: scalar_fsub_neg0_undef:
; X86-SSE:       # %bb.0:
; X86-SSE-NEXT:    fldz
; X86-SSE-NEXT:    retl
;
; X64-SSE-LABEL: scalar_fsub_neg0_undef:
; X64-SSE:       # %bb.0:
; X64-SSE-NEXT:    retq
  %r = fsub float -0.0, undef
  ret float %r
}

define float @scalar_fneg_undef(float %x) nounwind {
; X86-SSE-LABEL: scalar_fneg_undef:
; X86-SSE:       # %bb.0:
; X86-SSE-NEXT:    fldz
; X86-SSE-NEXT:    retl
;
; X64-SSE-LABEL: scalar_fneg_undef:
; X64-SSE:       # %bb.0:
; X64-SSE-NEXT:    retq
  %r = fneg float undef
  ret float %r
}

define <4 x float> @fsub_neg0_undef(<4 x float> %Q) nounwind {
; X86-SSE-LABEL: fsub_neg0_undef:
; X86-SSE:       # %bb.0:
; X86-SSE-NEXT:    retl
;
; X64-SSE-LABEL: fsub_neg0_undef:
; X64-SSE:       # %bb.0:
; X64-SSE-NEXT:    retq
  %r = fsub <4 x float> <float -0.0, float -0.0, float -0.0, float -0.0>, undef
  ret <4 x float> %r
}

define <4 x float> @fneg_undef(<4 x float> %Q) nounwind {
; X86-SSE-LABEL: fneg_undef:
; X86-SSE:       # %bb.0:
; X86-SSE-NEXT:    retl
;
; X64-SSE-LABEL: fneg_undef:
; X64-SSE:       # %bb.0:
; X64-SSE-NEXT:    retq
  %r = fneg <4 x float> undef
  ret <4 x float> %r
}

define <4 x float> @fsub_neg0_undef_elts_undef(<4 x float> %x) {
; X86-SSE-LABEL: fsub_neg0_undef_elts_undef:
; X86-SSE:       # %bb.0:
; X86-SSE-NEXT:    retl
;
; X64-SSE-LABEL: fsub_neg0_undef_elts_undef:
; X64-SSE:       # %bb.0:
; X64-SSE-NEXT:    retq
  %r = fsub <4 x float> <float -0.0, float undef, float undef, float -0.0>, undef
  ret <4 x float> %r
}

; This test verifies that we generate an FP subtraction because "0.0 - x" is not an fneg.
define <4 x float> @t2(<4 x float> %Q) nounwind {
; X86-SSE-LABEL: t2:
; X86-SSE:       # %bb.0:
; X86-SSE-NEXT:    xorps %xmm1, %xmm1
; X86-SSE-NEXT:    subps %xmm0, %xmm1
; X86-SSE-NEXT:    movaps %xmm1, %xmm0
; X86-SSE-NEXT:    retl
;
; X64-SSE-LABEL: t2:
; X64-SSE:       # %bb.0:
; X64-SSE-NEXT:    xorps %xmm1, %xmm1
; X64-SSE-NEXT:    subps %xmm0, %xmm1
; X64-SSE-NEXT:    movaps %xmm1, %xmm0
; X64-SSE-NEXT:    retq
  %tmp = fsub <4 x float> zeroinitializer, %Q
  ret <4 x float> %tmp
}

; If we're bitcasting an integer to an FP vector, we should avoid the FPU/vector unit entirely.
; Make sure that we're flipping the sign bit and only the sign bit of each float.
; So instead of something like this:
;    movd	%rdi, %xmm0
;    xorps	.LCPI2_0(%rip), %xmm0
;
; We should generate:
;    movabsq     (put sign bit mask in integer register))
;    xorq        (flip sign bits)
;    movd        (move to xmm return register)

define <2 x float> @fneg_bitcast(i64 %i) nounwind {
; X86-SSE1-LABEL: fneg_bitcast:
; X86-SSE1:       # %bb.0:
; X86-SSE1-NEXT:    pushl %ebp
; X86-SSE1-NEXT:    movl %esp, %ebp
; X86-SSE1-NEXT:    andl $-16, %esp
; X86-SSE1-NEXT:    subl $16, %esp
; X86-SSE1-NEXT:    movl $-2147483648, %eax # imm = 0x80000000
; X86-SSE1-NEXT:    movl 12(%ebp), %ecx
; X86-SSE1-NEXT:    xorl %eax, %ecx
; X86-SSE1-NEXT:    movl %ecx, {{[0-9]+}}(%esp)
; X86-SSE1-NEXT:    xorl 8(%ebp), %eax
; X86-SSE1-NEXT:    movl %eax, (%esp)
; X86-SSE1-NEXT:    movaps (%esp), %xmm0
; X86-SSE1-NEXT:    movl %ebp, %esp
; X86-SSE1-NEXT:    popl %ebp
; X86-SSE1-NEXT:    retl
;
; X86-SSE2-LABEL: fneg_bitcast:
; X86-SSE2:       # %bb.0:
; X86-SSE2-NEXT:    movl $-2147483648, %eax # imm = 0x80000000
; X86-SSE2-NEXT:    movl {{[0-9]+}}(%esp), %ecx
; X86-SSE2-NEXT:    xorl %eax, %ecx
; X86-SSE2-NEXT:    movd %ecx, %xmm1
; X86-SSE2-NEXT:    xorl {{[0-9]+}}(%esp), %eax
; X86-SSE2-NEXT:    movd %eax, %xmm0
; X86-SSE2-NEXT:    punpckldq {{.*#+}} xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
; X86-SSE2-NEXT:    retl
;
; X64-SSE1-LABEL: fneg_bitcast:
; X64-SSE1:       # %bb.0:
; X64-SSE1-NEXT:    movabsq $-9223372034707292160, %rax # imm = 0x8000000080000000
; X64-SSE1-NEXT:    xorq %rdi, %rax
; X64-SSE1-NEXT:    movq %rax, -{{[0-9]+}}(%rsp)
; X64-SSE1-NEXT:    movaps -{{[0-9]+}}(%rsp), %xmm0
; X64-SSE1-NEXT:    retq
;
; X64-SSE2-LABEL: fneg_bitcast:
; X64-SSE2:       # %bb.0:
; X64-SSE2-NEXT:    movabsq $-9223372034707292160, %rax # imm = 0x8000000080000000
; X64-SSE2-NEXT:    xorq %rdi, %rax
; X64-SSE2-NEXT:    movq %rax, %xmm0
; X64-SSE2-NEXT:    retq
  %bitcast = bitcast i64 %i to <2 x float>
  %fneg = fsub <2 x float> <float -0.0, float -0.0>, %bitcast
  ret <2 x float> %fneg
}

define <4 x float> @fneg_undef_elts_v4f32(<4 x float> %x) {
; X86-SSE-LABEL: fneg_undef_elts_v4f32:
; X86-SSE:       # %bb.0:
; X86-SSE-NEXT:    xorps {{\.?LCPI[0-9]+_[0-9]+}}, %xmm0
; X86-SSE-NEXT:    retl
;
; X64-SSE-LABEL: fneg_undef_elts_v4f32:
; X64-SSE:       # %bb.0:
; X64-SSE-NEXT:    xorps {{\.?LCPI[0-9]+_[0-9]+}}(%rip), %xmm0
; X64-SSE-NEXT:    retq
  %r = fsub <4 x float> <float -0.0, float undef, float undef, float -0.0>, %x
  ret <4 x float> %r
}

; This isn't fneg, but similarly check that (X - 0.0) is simplified.

define <4 x float> @fsub0_undef_elts_v4f32(<4 x float> %x) {
; X86-SSE-LABEL: fsub0_undef_elts_v4f32:
; X86-SSE:       # %bb.0:
; X86-SSE-NEXT:    retl
;
; X64-SSE-LABEL: fsub0_undef_elts_v4f32:
; X64-SSE:       # %bb.0:
; X64-SSE-NEXT:    retq
  %r = fsub <4 x float> %x, <float 0.0, float undef, float 0.0, float undef>
  ret <4 x float> %r
}

define <4 x float> @fneg(<4 x float> %Q) nounwind {
; X86-SSE-LABEL: fneg:
; X86-SSE:       # %bb.0:
; X86-SSE-NEXT:    xorps {{\.?LCPI[0-9]+_[0-9]+}}, %xmm0
; X86-SSE-NEXT:    retl
;
; X64-SSE-LABEL: fneg:
; X64-SSE:       # %bb.0:
; X64-SSE-NEXT:    xorps {{\.?LCPI[0-9]+_[0-9]+}}(%rip), %xmm0
; X64-SSE-NEXT:    retq
  %tmp = fneg <4 x float> %Q
  ret <4 x float> %tmp
}

; store(fneg(load())) - convert scalar to integer
define void @fneg_int_rmw_half(ptr %ptr) nounwind {
; X86-SSE-LABEL: fneg_int_rmw_half:
; X86-SSE:       # %bb.0:
; X86-SSE-NEXT:    movl {{[0-9]+}}(%esp), %eax
; X86-SSE-NEXT:    xorb $-128, 1(%eax)
; X86-SSE-NEXT:    retl
;
; X64-SSE-LABEL: fneg_int_rmw_half:
; X64-SSE:       # %bb.0:
; X64-SSE-NEXT:    xorb $-128, 1(%rdi)
; X64-SSE-NEXT:    retq
  %1 = load half, ptr %ptr
  %2 = fneg half %1
  store half %2, ptr %ptr
  ret void
}

define void @fneg_int_bfloat(ptr %src, ptr %dst) nounwind {
; X86-SSE-LABEL: fneg_int_bfloat:
; X86-SSE:       # %bb.0:
; X86-SSE-NEXT:    movl {{[0-9]+}}(%esp), %eax
; X86-SSE-NEXT:    movl {{[0-9]+}}(%esp), %ecx
; X86-SSE-NEXT:    movzwl (%ecx), %ecx
; X86-SSE-NEXT:    xorl $32768, %ecx # imm = 0x8000
; X86-SSE-NEXT:    movw %cx, (%eax)
; X86-SSE-NEXT:    retl
;
; X64-SSE-LABEL: fneg_int_bfloat:
; X64-SSE:       # %bb.0:
; X64-SSE-NEXT:    movzwl (%rdi), %eax
; X64-SSE-NEXT:    xorl $32768, %eax # imm = 0x8000
; X64-SSE-NEXT:    movw %ax, (%rsi)
; X64-SSE-NEXT:    retq
  %1 = load bfloat, ptr %src
  %2 = fneg bfloat %1
  store bfloat %2, ptr %dst
  ret void
}

define void @fneg_int_rmw_f32(ptr %ptr) {
; X86-SSE-LABEL: fneg_int_rmw_f32:
; X86-SSE:       # %bb.0:
; X86-SSE-NEXT:    movl {{[0-9]+}}(%esp), %eax
; X86-SSE-NEXT:    xorb $-128, 3(%eax)
; X86-SSE-NEXT:    retl
;
; X64-SSE-LABEL: fneg_int_rmw_f32:
; X64-SSE:       # %bb.0:
; X64-SSE-NEXT:    xorb $-128, 3(%rdi)
; X64-SSE-NEXT:    retq
  %1 = load float, ptr %ptr
  %2 = fneg float %1
  store float %2, ptr %ptr
  ret void
}

define void @fneg_int_f64(ptr %src, ptr %dst) {
; X86-SSE1-LABEL: fneg_int_f64:
; X86-SSE1:       # %bb.0:
; X86-SSE1-NEXT:    movl {{[0-9]+}}(%esp), %eax
; X86-SSE1-NEXT:    movl {{[0-9]+}}(%esp), %ecx
; X86-SSE1-NEXT:    fldl (%ecx)
; X86-SSE1-NEXT:    fchs
; X86-SSE1-NEXT:    fstpl (%eax)
; X86-SSE1-NEXT:    retl
;
; X86-SSE2-LABEL: fneg_int_f64:
; X86-SSE2:       # %bb.0:
; X86-SSE2-NEXT:    movl {{[0-9]+}}(%esp), %eax
; X86-SSE2-NEXT:    movl {{[0-9]+}}(%esp), %ecx
; X86-SSE2-NEXT:    movsd {{.*#+}} xmm0 = mem[0],zero
; X86-SSE2-NEXT:    xorps {{\.?LCPI[0-9]+_[0-9]+}}, %xmm0
; X86-SSE2-NEXT:    movlps %xmm0, (%eax)
; X86-SSE2-NEXT:    retl
;
; X64-SSE-LABEL: fneg_int_f64:
; X64-SSE:       # %bb.0:
; X64-SSE-NEXT:    movabsq $-9223372036854775808, %rax # imm = 0x8000000000000000
; X64-SSE-NEXT:    xorq (%rdi), %rax
; X64-SSE-NEXT:    movq %rax, (%rsi)
; X64-SSE-NEXT:    retq
  %1 = load double, ptr %src
  %2 = fneg double %1
  store double %2, ptr %dst
  ret void
}

; don't convert vector to scalar
define void @fneg_int_v4f32(ptr %src, ptr %dst) {
; X86-SSE-LABEL: fneg_int_v4f32:
; X86-SSE:       # %bb.0:
; X86-SSE-NEXT:    movl {{[0-9]+}}(%esp), %eax
; X86-SSE-NEXT:    movl {{[0-9]+}}(%esp), %ecx
; X86-SSE-NEXT:    movaps (%ecx), %xmm0
; X86-SSE-NEXT:    xorps {{\.?LCPI[0-9]+_[0-9]+}}, %xmm0
; X86-SSE-NEXT:    movaps %xmm0, (%eax)
; X86-SSE-NEXT:    retl
;
; X64-SSE-LABEL: fneg_int_v4f32:
; X64-SSE:       # %bb.0:
; X64-SSE-NEXT:    movaps (%rdi), %xmm0
; X64-SSE-NEXT:    xorps {{\.?LCPI[0-9]+_[0-9]+}}(%rip), %xmm0
; X64-SSE-NEXT:    movaps %xmm0, (%rsi)
; X64-SSE-NEXT:    retq
  %1 = load <4 x float>, ptr %src
  %2 = fneg <4 x float> %1
  store <4 x float> %2, ptr %dst
  ret void
}