Debian Codegraph @

rpython/jit/backend/x86/vector_ext.py

raw symbols SCIP
1 import py
2 from rpython.jit.metainterp.compile import ResumeGuardDescr
3 from rpython.jit.metainterp.history import (ConstInt, INT, REF,
4 FLOAT, VECTOR, TargetToken)
5 from rpython.jit.backend.llsupport.descr import (ArrayDescr, CallDescr,
6 unpack_arraydescr, unpack_fielddescr, unpack_interiorfielddescr)
7 from rpython.jit.backend.x86.regloc import (FrameLoc, RegLoc, ConstFloatLoc,
8 FloatImmedLoc, ImmedLoc, imm, imm0, imm1, ecx, eax, edx, ebx, esi, edi,
9 ebp, r8, r9, r10, r11, r12, r13, r14, r15, xmm0, xmm1, xmm2, xmm3, xmm4,
10 xmm5, xmm6, xmm7, xmm8, xmm9, xmm10, xmm11, xmm12, xmm13, xmm14,
11 X86_64_SCRATCH_REG, X86_64_XMM_SCRATCH_REG, AddressLoc)
12 from rpython.jit.backend.llsupport.vector_ext import VectorExt
13 from rpython.jit.backend.llsupport.regalloc import (get_scale, TempVar,
14 NoVariableToSpill)
15 from rpython.jit.metainterp.resoperation import (rop, ResOperation,
16 VectorOp, VectorGuardOp)
17 from rpython.rlib.objectmodel import we_are_translated, always_inline
18 from rpython.rtyper.lltypesystem.lloperation import llop
19 from rpython.rtyper.lltypesystem import lltype
20 from rpython.jit.backend.x86 import rx86, detect_feature
21
22 # duplicated for easy migration, def in assembler.py as well
23 # DUP START
24 def addr_add(reg_or_imm1, reg_or_imm2, offset=0, scale=0):
25 return AddressLoc(reg_or_imm1, reg_or_imm2, scale, offset)
26
27 def heap(addr):
28 return AddressLoc(ImmedLoc(addr), imm0, 0, 0)
29
30 def not_implemented(msg):
31 msg = '[x86/vector_ext] %s\n' % msg
32 if we_are_translated():
33 llop.debug_print(lltype.Void, msg)
34 raise NotImplementedError(msg)
35 # DUP END
36
37 class TempVector(TempVar):
38 def __init__(self, type):
39 self.type = type
40 def is_vector(self):
41 return True
42 def __repr__(self):
43 return "<TempVector at %s>" % (id(self),)
44
45 class TempInt(TempVar):
46 type = INT
47
48 def __repr__(self):
49 return "<TempInt at %s>" % (id(self),)
50
51 class X86VectorExt(VectorExt):
52
53 should_align_unroll = True
54
55 def setup_once(self, asm):
56 if detect_feature.detect_sse4_1():
57 self.enable(16, accum=True)
58 asm.setup_once_vector()
59 self._setup = True
60
61 class VectorAssemblerMixin(object):
62 _mixin_ = True
63 element_ones = [] # overridden in assembler.py
64
65 def setup_once_vector(self):
66 pass
67
68 def genop_guard_vec_guard_true(self, guard_op, guard_token, locs, resloc):
69 self.implement_guard(guard_token)
70
71 def genop_guard_vec_guard_false(self, guard_op, guard_token, locs, resloc):
72 self.guard_success_cc = rx86.invert_condition(self.guard_success_cc)
73 self.implement_guard(guard_token)
74
75 def guard_vector(self, guard_op, loc, true):
76 assert isinstance(guard_op, VectorGuardOp)
77 arg = guard_op.getarg(0)
78 assert isinstance(arg, VectorOp)
79 size = arg.bytesize
80 temp = X86_64_XMM_SCRATCH_REG
81 ve = self.cpu.vector_ext
82 assert ve is not None # MUST hold, optimize_vector is never entered if vector_ext is entered
83 load = arg.bytesize * arg.count - ve.register_size
84 assert load <= 0
85 if true:
86 self.mc.PXOR(temp, temp)
87 # if the vector is not fully packed blend 1s
88 if load < 0:
89 self.mc.PCMPEQQ(temp, temp) # fill with ones
90 self._blend_unused_slots(loc, arg, temp)
91 # reset to zeros
92 self.mc.PXOR(temp, temp)
93
94 # cmp with zeros (in temp) creates ones at each slot where it is zero
95 self.mc.PCMPEQ(loc, temp, size)
96 # temp converted to ones
97 self.mc.PCMPEQQ(temp, temp)
98 # test if all slots are zero
99 self.mc.PTEST(loc, temp)
100 self.guard_success_cc = rx86.Conditions['Z']
101 else:
102 # if the vector is not fully packed blend 1s
103 if load < 0:
104 temp = X86_64_XMM_SCRATCH_REG
105 self.mc.PXOR(temp, temp)
106 self._blend_unused_slots(loc, arg, temp)
107 self.mc.PTEST(loc, loc)
108 self.guard_success_cc = rx86.Conditions['NZ']
109
110 def _blend_unused_slots(self, loc, arg, temp):
111 select = 0
112 bits_used = (arg.count * arg.bytesize * 8)
113 index = bits_used // 16
114 while index < 8:
115 select |= (1 << index)
116 index += 1
117 self.mc.PBLENDW_xxi(loc.value, temp.value, select)
118
119 def _update_at_exit(self, fail_locs, fail_args, faildescr, regalloc):
120 """ If accumulation is done in this loop, at the guard exit
121 some vector registers must be adjusted to yield the correct value
122 """
123 if not isinstance(faildescr, ResumeGuardDescr):
124 return
125 assert regalloc is not None
126 accum_info = faildescr.rd_vector_info
127 while accum_info:
128 pos = accum_info.getpos_in_failargs()
129 scalar_loc = fail_locs[pos]
130 vector_loc = accum_info.location
131 # the upper elements will be lost if saved to the stack!
132 scalar_arg = accum_info.getoriginal()
133 assert isinstance(vector_loc, RegLoc)
134 assert scalar_arg is not None
135 orig_scalar_loc = scalar_loc
136 tmpvar = None
137 if not isinstance(scalar_loc, RegLoc):
138 # scalar loc might live in memory, use scratch register and save it back later
139 if scalar_arg.type == FLOAT:
140 scalar_loc = X86_64_XMM_SCRATCH_REG
141 else:
142 tmpvar = TempInt()
143 scalar_loc = regalloc.rm.try_allocate_reg(tmpvar)
144 self.mov(orig_scalar_loc, scalar_loc)
145 if accum_info.accum_operation == '+':
146 self._accum_reduce_sum(scalar_arg, vector_loc, scalar_loc)
147 elif accum_info.accum_operation == '*':
148 self._accum_reduce_mul(scalar_arg, vector_loc, scalar_loc)
149 else:
150 not_implemented("accum operator %s not implemented" %
151 (accum_info.accum_operation))
152 if tmpvar:
153 regalloc.rm.possibly_free_var(tmpvar)
154 if scalar_loc is not orig_scalar_loc:
155 self.mov(scalar_loc, orig_scalar_loc)
156 accum_info = accum_info.next()
157
158 def _accum_reduce_mul(self, arg, accumloc, targetloc):
159 self.mov(accumloc, targetloc)
160 # swap the two elements
161 self.mc.SHUFPD_xxi(targetloc.value, targetloc.value, 0x01)
162 self.mc.MULSD(targetloc, accumloc)
163
164 def _accum_reduce_sum(self, arg, accumloc, targetloc):
165 # Currently the accumulator can ONLY be the biggest
166 # size for X86 -> 64 bit float/int
167 if arg.type == FLOAT:
168 # r = (r[0]+r[1],r[0]+r[1])
169 self.mc.HADDPD(accumloc, accumloc)
170 # upper bits (> 64) are dirty (but does not matter)
171 if accumloc is not targetloc:
172 self.mov(accumloc, targetloc)
173 return
174 elif arg.type == INT:
175 scratchloc = X86_64_SCRATCH_REG
176 self.mc.forget_scratch_register()
177 self.mc.PEXTRQ_rxi(targetloc.value, accumloc.value, 0)
178 self.mc.PEXTRQ_rxi(scratchloc.value, accumloc.value, 1)
179 self.mc.ADD_rr(targetloc.value, scratchloc.value)
180 return
181
182 not_implemented("reduce sum for %s not impl." % arg)
183
184 def _genop_vec_load(self, op, arglocs, resloc):
185 base_loc, ofs_loc, size_loc, scale, ofs, integer_loc = arglocs
186 src_addr = addr_add(base_loc, ofs_loc, ofs.value, scale.value)
187 self._vec_load(resloc, src_addr, integer_loc.value,
188 size_loc.value, False)
189
190 genop_vec_load_i = _genop_vec_load
191 genop_vec_load_f = _genop_vec_load
192
193 @always_inline
194 def _vec_load(self, resloc, src_addr, integer, itemsize, aligned):
195 if integer:
196 if aligned:
197 self.mc.MOVDQA(resloc, src_addr)
198 else:
199 self.mc.MOVDQU(resloc, src_addr)
200 else:
201 if itemsize == 4:
202 self.mc.MOVUPS(resloc, src_addr)
203 elif itemsize == 8:
204 self.mc.MOVUPD(resloc, src_addr)
205
206 def genop_discard_vec_store(self, op, arglocs):
207 base_loc, ofs_loc, value_loc, size_loc, scale,\
208 baseofs, integer_loc = arglocs
209 dest_loc = addr_add(base_loc, ofs_loc, baseofs.value, scale.value)
210 self._vec_store(dest_loc, value_loc, integer_loc.value,
211 size_loc.value, False)
212
213 @always_inline
214 def _vec_store(self, dest_loc, value_loc, integer, itemsize, aligned):
215 if integer:
216 if aligned:
217 self.mc.MOVDQA(dest_loc, value_loc)
218 else:
219 self.mc.MOVDQU(dest_loc, value_loc)
220 else:
221 if itemsize == 4:
222 self.mc.MOVUPS(dest_loc, value_loc)
223 elif itemsize == 8:
224 self.mc.MOVUPD(dest_loc, value_loc)
225
226 def genop_vec_int_is_true(self, op, arglocs, resloc):
227 loc, sizeloc = arglocs
228 temp = X86_64_XMM_SCRATCH_REG
229 # Use PXOR_xx directly (register-register) rather than the generic
230 # PXOR dispatcher, which has a dead AddressLoc branch that confuses
231 # GCC's bounds checker when this function is inlined with a RegLoc
232 # constant as argument.
233 self.mc.PXOR_xx(temp.value, temp.value)
234 # every entry that is non zero -> becomes zero
235 # zero entries become ones
236 self.mc.PCMPEQ(loc, temp, sizeloc.value)
237 # a second time -> every zero entry (corresponding to non zero
238 # entries before) become ones
239 self.mc.PCMPEQ(loc, temp, sizeloc.value)
240
241 def genop_vec_int_mul(self, op, arglocs, resloc):
242 loc0, loc1, itemsize_loc = arglocs
243 itemsize = itemsize_loc.value
244 if itemsize == 2:
245 self.mc.PMULLW(loc0, loc1)
246 elif itemsize == 4:
247 self.mc.PMULLD(loc0, loc1)
248 else:
249 # NOTE see http://stackoverflow.com/questions/8866973/can-long-integer-routines-benefit-from-sse/8867025#8867025
250 # There is no 64x64 bit packed mul. For 8 bit either. It is questionable if it gives any benefit?
251 not_implemented("int8/64 mul")
252
253 def genop_vec_int_add(self, op, arglocs, resloc):
254 loc0, loc1, size_loc = arglocs
255 size = size_loc.value
256 if size == 1:
257 self.mc.PADDB(loc0, loc1)
258 elif size == 2:
259 self.mc.PADDW(loc0, loc1)
260 elif size == 4:
261 self.mc.PADDD(loc0, loc1)
262 elif size == 8:
263 self.mc.PADDQ(loc0, loc1)
264
265 def genop_vec_int_sub(self, op, arglocs, resloc):
266 loc0, loc1, size_loc = arglocs
267 size = size_loc.value
268 if size == 1:
269 self.mc.PSUBB(loc0, loc1)
270 elif size == 2:
271 self.mc.PSUBW(loc0, loc1)
272 elif size == 4:
273 self.mc.PSUBD(loc0, loc1)
274 elif size == 8:
275 self.mc.PSUBQ(loc0, loc1)
276
277 def genop_vec_int_and(self, op, arglocs, resloc):
278 self.mc.PAND(resloc, arglocs[0])
279
280 def genop_vec_int_or(self, op, arglocs, resloc):
281 self.mc.POR(resloc, arglocs[0])
282
283 def genop_vec_int_xor(self, op, arglocs, resloc):
284 self.mc.PXOR(resloc, arglocs[0])
285
286 genop_vec_float_xor = genop_vec_int_xor
287
288 genop_vec_float_arith = """
289 def genop_vec_float_{type}(self, op, arglocs, resloc):
290 loc0, loc1, itemsize_loc = arglocs
291 itemsize = itemsize_loc.value
292 if itemsize == 4:
293 self.mc.{p_op_s}(loc0, loc1)
294 elif itemsize == 8:
295 self.mc.{p_op_d}(loc0, loc1)
296 """
297 for op in ['add','mul','sub']:
298 OP = op.upper()
299 _source = genop_vec_float_arith.format(type=op,
300 p_op_s=OP+'PS',
301 p_op_d=OP+'PD')
302 exec(py.code.Source(_source).compile())
303 del genop_vec_float_arith
304
305 def genop_vec_float_truediv(self, op, arglocs, resloc):
306 loc0, loc1, sizeloc = arglocs
307 size = sizeloc.value
308 if size == 4:
309 self.mc.DIVPS(loc0, loc1)
310 elif size == 8:
311 self.mc.DIVPD(loc0, loc1)
312
313 def genop_vec_float_abs(self, op, arglocs, resloc):
314 src, sizeloc = arglocs
315 size = sizeloc.value
316 if size == 4:
317 self.mc.ANDPS(src, heap(self.single_float_const_abs_addr))
318 elif size == 8:
319 self.mc.ANDPD(src, heap(self.float_const_abs_addr))
320
321 def genop_vec_float_neg(self, op, arglocs, resloc):
322 src, sizeloc = arglocs
323 size = sizeloc.value
324 if size == 4:
325 self.mc.XORPS(src, heap(self.single_float_const_neg_addr))
326 elif size == 8:
327 self.mc.XORPD(src, heap(self.float_const_neg_addr))
328
329 def genop_vec_float_eq(self, op, arglocs, resloc):
330 lhsloc, rhsloc, sizeloc = arglocs
331 size = sizeloc.value
332 if size == 4:
333 self.mc.CMPPS_xxi(lhsloc.value, rhsloc.value, 0) # 0 means equal
334 else:
335 self.mc.CMPPD_xxi(lhsloc.value, rhsloc.value, 0)
336 self.flush_vec_cc(rx86.Conditions["E"], lhsloc, resloc, sizeloc.value)
337
338 def flush_vec_cc(self, rev_cond, lhsloc, resloc, size):
339 # After emitting an instruction that leaves a boolean result in
340 # a condition code (cc), call this. In the common case, result_loc
341 # will be set to SPP by the regalloc, which in this case means
342 # "propagate it between this operation and the next guard by keeping
343 # it in the cc". In the uncommon case, result_loc is another
344 # register, and we emit a load from the cc into this register.
345
346 if resloc is ebp:
347 self.guard_success_cc = rev_cond
348 else:
349 assert lhsloc is xmm0
350 maskloc = X86_64_XMM_SCRATCH_REG
351 assert len(self.element_ones) > 0
352 self.mc.MOVAPD(maskloc, heap(self.element_ones[get_scale(size)]))
353 self.mc.PXOR(resloc, resloc)
354 # note that resloc contains true false for each element by the last compare operation
355 self.mc.PBLENDVB_xx(resloc.value, maskloc.value)
356
357 def genop_vec_float_ne(self, op, arglocs, resloc):
358 lhsloc, rhsloc, sizeloc = arglocs
359 size = sizeloc.value
360 # b(100) == 1 << 2 means not equal
361 if size == 4:
362 self.mc.CMPPS_xxi(lhsloc.value, rhsloc.value, 1 << 2)
363 else:
364 self.mc.CMPPD_xxi(lhsloc.value, rhsloc.value, 1 << 2)
365 self.flush_vec_cc(rx86.Conditions["NE"], lhsloc, resloc, sizeloc.value)
366
367 def genop_vec_int_eq(self, op, arglocs, resloc):
368 lhsloc, rhsloc, sizeloc = arglocs
369 size = sizeloc.value
370 self.mc.PCMPEQ(lhsloc, rhsloc, size)
371 self.flush_vec_cc(rx86.Conditions["E"], lhsloc, resloc, sizeloc.value)
372
373 def genop_vec_int_ne(self, op, arglocs, resloc):
374 lhsloc, rhsloc, sizeloc = arglocs
375 size = sizeloc.value
376 self.mc.PCMPEQ(resloc, rhsloc, size)
377 temp = X86_64_XMM_SCRATCH_REG
378 self.mc.PCMPEQQ(temp, temp) # set all bits to one
379 # need to invert the value in resloc
380 self.mc.PXOR(resloc, temp)
381 # 11 00 11 11
382 # 11 11 11 11
383 # ----------- pxor
384 # 00 11 00 00
385 self.flush_vec_cc(rx86.Conditions["NE"], lhsloc, resloc, sizeloc.value)
386
387 def genop_vec_int_signext(self, op, arglocs, resloc):
388 srcloc, sizeloc, tosizeloc = arglocs
389 size = sizeloc.value
390 tosize = tosizeloc.value
391 if size == tosize:
392 return # already the right size
393 if size == 4 and tosize == 8:
394 scratch = X86_64_SCRATCH_REG.value
395 self.mc.forget_scratch_register()
396 self.mc.PEXTRD_rxi(scratch, srcloc.value, 1)
397 self.mc.PINSRQ_xri(resloc.value, scratch, 1)
398 self.mc.PEXTRD_rxi(scratch, srcloc.value, 0)
399 self.mc.PINSRQ_xri(resloc.value, scratch, 0)
400 elif size == 8 and tosize == 4:
401 # is there a better sequence to move them?
402 scratch = X86_64_SCRATCH_REG.value
403 self.mc.forget_scratch_register()
404 self.mc.PEXTRQ_rxi(scratch, srcloc.value, 0)
405 self.mc.PINSRD_xri(resloc.value, scratch, 0)
406 self.mc.PEXTRQ_rxi(scratch, srcloc.value, 1)
407 self.mc.PINSRD_xri(resloc.value, scratch, 1)
408 else:
409 # note that all other conversions are not implemented
410 # on purpose. it needs many x86 op codes to implement
411 # the missing combinations. even if they are implemented
412 # the speedup might only be modest...
413 # the optimization does not emit such code!
414 msg = "vec int signext (%d->%d)" % (size, tosize)
415 not_implemented(msg)
416
417 def genop_vec_expand_f(self, op, arglocs, resloc):
418 srcloc, sizeloc = arglocs
419 size = sizeloc.value
420 if isinstance(srcloc, ConstFloatLoc):
421 # they are aligned!
422 self.mc.MOVAPD(resloc, srcloc)
423 elif size == 4:
424 # the register allocator forces src to be the same as resloc
425 # r = (s[0], s[0], r[0], r[0])
426 # since resloc == srcloc: r = (r[0], r[0], r[0], r[0])
427 self.mc.SHUFPS_xxi(resloc.value, srcloc.value, 0)
428 elif size == 8:
429 self.mc.MOVDDUP(resloc, srcloc)
430 else:
431 raise AssertionError("float of size %d not supported" % (size,))
432
433 def genop_vec_expand_i(self, op, arglocs, resloc):
434 srcloc, sizeloc = arglocs
435 if not isinstance(srcloc, RegLoc):
436 # self.mc.forget_scratch_register(): done by self.mov()
437 self.mov(srcloc, X86_64_SCRATCH_REG)
438 srcloc = X86_64_SCRATCH_REG
439 assert not srcloc.is_xmm
440 size = sizeloc.value
441 if size == 1:
442 self.mc.PINSRB_xri(resloc.value, srcloc.value, 0)
443 self.mc.PSHUFB(resloc, heap(self.expand_byte_mask_addr))
444 elif size == 2:
445 self.mc.PINSRW_xri(resloc.value, srcloc.value, 0)
446 self.mc.PINSRW_xri(resloc.value, srcloc.value, 4)
447 self.mc.PSHUFLW_xxi(resloc.value, resloc.value, 0)
448 self.mc.PSHUFHW_xxi(resloc.value, resloc.value, 0)
449 elif size == 4:
450 self.mc.PINSRD_xri(resloc.value, srcloc.value, 0)
451 self.mc.PSHUFD_xxi(resloc.value, resloc.value, 0)
452 elif size == 8:
453 self.mc.PINSRQ_xri(resloc.value, srcloc.value, 0)
454 self.mc.PINSRQ_xri(resloc.value, srcloc.value, 1)
455 else:
456 raise AssertionError("cannot handle size %d (int expand)" % (size,))
457
458 def genop_vec_pack_i(self, op, arglocs, resloc):
459 resultloc, sourceloc, residxloc, srcidxloc, countloc, sizeloc = arglocs
460 assert isinstance(resultloc, RegLoc)
461 assert isinstance(sourceloc, RegLoc)
462 size = sizeloc.value
463 srcidx = srcidxloc.value
464 residx = residxloc.value
465 count = countloc.value
466 # for small data type conversion this can be quite costy
467 # NOTE there might be some combinations that can be handled
468 # more efficiently! e.g.
469 # v2 = pack(v0,v1,4,4)
470 si = srcidx
471 ri = residx
472 k = count
473 while k > 0:
474 if size == 8:
475 if resultloc.is_xmm and sourceloc.is_xmm: # both xmm
476 self.mc.forget_scratch_register()
477 self.mc.PEXTRQ_rxi(X86_64_SCRATCH_REG.value, sourceloc.value, si)
478 self.mc.PINSRQ_xri(resultloc.value, X86_64_SCRATCH_REG.value, ri)
479 elif resultloc.is_xmm: # xmm <- reg
480 self.mc.PINSRQ_xri(resultloc.value, sourceloc.value, ri)
481 else: # reg <- xmm
482 self.mc.PEXTRQ_rxi(resultloc.value, sourceloc.value, si)
483 elif size == 4:
484 if resultloc.is_xmm and sourceloc.is_xmm:
485 self.mc.forget_scratch_register()
486 self.mc.PEXTRD_rxi(X86_64_SCRATCH_REG.value, sourceloc.value, si)
487 self.mc.PINSRD_xri(resultloc.value, X86_64_SCRATCH_REG.value, ri)
488 elif resultloc.is_xmm:
489 self.mc.PINSRD_xri(resultloc.value, sourceloc.value, ri)
490 else:
491 self.mc.PEXTRD_rxi(resultloc.value, sourceloc.value, si)
492 elif size == 2:
493 if resultloc.is_xmm and sourceloc.is_xmm:
494 self.mc.forget_scratch_register()
495 self.mc.PEXTRW_rxi(X86_64_SCRATCH_REG.value, sourceloc.value, si)
496 self.mc.PINSRW_xri(resultloc.value, X86_64_SCRATCH_REG.value, ri)
497 elif resultloc.is_xmm:
498 self.mc.PINSRW_xri(resultloc.value, sourceloc.value, ri)
499 else:
500 self.mc.PEXTRW_rxi(resultloc.value, sourceloc.value, si)
501 elif size == 1:
502 if resultloc.is_xmm and sourceloc.is_xmm:
503 self.mc.forget_scratch_register()
504 self.mc.PEXTRB_rxi(X86_64_SCRATCH_REG.value, sourceloc.value, si)
505 self.mc.PINSRB_xri(resultloc.value, X86_64_SCRATCH_REG.value, ri)
506 elif resultloc.is_xmm:
507 self.mc.PINSRB_xri(resultloc.value, sourceloc.value, ri)
508 else:
509 self.mc.PEXTRB_rxi(resultloc.value, sourceloc.value, si)
510 si += 1
511 ri += 1
512 k -= 1
513
514 genop_vec_unpack_i = genop_vec_pack_i
515
516 def genop_vec_pack_f(self, op, arglocs, resultloc):
517 resloc, srcloc, residxloc, srcidxloc, countloc, sizeloc = arglocs
518 assert isinstance(resloc, RegLoc)
519 assert isinstance(srcloc, RegLoc)
520 count = countloc.value
521 residx = residxloc.value
522 srcidx = srcidxloc.value
523 size = sizeloc.value
524 if size == 4:
525 si = srcidx
526 ri = residx
527 k = count
528 while k > 0:
529 if resloc.is_xmm:
530 src = srcloc.value
531 if not srcloc.is_xmm:
532 # if source is a normal register (unpack)
533 assert count == 1
534 assert si == 0
535 self.mov(srcloc, X86_64_XMM_SCRATCH_REG)
536 src = X86_64_XMM_SCRATCH_REG.value
537 select = ((si & 0x3) << 6)|((ri & 0x3) << 4)
538 self.mc.INSERTPS_xxi(resloc.value, src, select)
539 else:
540 self.mc.PEXTRD_rxi(resloc.value, srcloc.value, si)
541 si += 1
542 ri += 1
543 k -= 1
544 elif size == 8:
545 assert resloc.is_xmm
546 if srcloc.is_xmm:
547 if srcidx == 0:
548 if residx == 0:
549 # r = (s[0], r[1])
550 self.mc.MOVSD(resloc, srcloc)
551 else:
552 assert residx == 1
553 # r = (r[0], s[0])
554 self.mc.UNPCKLPD(resloc, srcloc)
555 else:
556 assert srcidx == 1
557 if residx == 0:
558 # r = (s[1], r[1])
559 if resloc != srcloc:
560 self.mc.UNPCKHPD(resloc, srcloc)
561 self.mc.SHUFPD_xxi(resloc.value, resloc.value, 1)
562 else:
563 assert residx == 1
564 # r = (r[0], s[1])
565 if resloc != srcloc:
566 self.mc.SHUFPD_xxi(resloc.value, resloc.value, 1)
567 self.mc.UNPCKHPD(resloc, srcloc)
568 # if they are equal nothing is to be done
569 else:
570 not_implemented("pack/unpack for size %d" % size)
571
572 genop_vec_unpack_f = genop_vec_pack_f
573
574 def genop_vec_cast_float_to_singlefloat(self, op, arglocs, resloc):
575 self.mc.CVTPD2PS(resloc, arglocs[0])
576
577 def genop_vec_cast_float_to_int(self, op, arglocs, resloc):
578 self.mc.CVTPD2DQ(resloc, arglocs[0])
579
580 def genop_vec_cast_int_to_float(self, op, arglocs, resloc):
581 self.mc.CVTDQ2PD(resloc, arglocs[0])
582
583 def genop_vec_cast_singlefloat_to_float(self, op, arglocs, resloc):
584 self.mc.CVTPS2PD(resloc, arglocs[0])
585
586 class VectorRegallocMixin(object):
587 _mixin_ = True
588
589 def _consider_vec_load(self, op):
590 descr = op.getdescr()
591 assert isinstance(descr, ArrayDescr)
592 assert not descr.is_array_of_pointers() and \
593 not descr.is_array_of_structs()
594 itemsize, _, _ = unpack_arraydescr(descr)
595 integer = not (descr.is_array_of_floats() or descr.getconcrete_type() == FLOAT)
596 args = op.getarglist()
597 scale = get_scale(op.getarg(2).getint())
598 ofs = op.getarg(3).getint()
599 base_loc = self.rm.make_sure_var_in_reg(op.getarg(0), args)
600 ofs_loc = self.rm.make_sure_var_in_reg(op.getarg(1), args)
601 result_loc = self.force_allocate_reg(op)
602 self.perform(op, [base_loc, ofs_loc, imm(itemsize), imm(scale),
603 imm(ofs), imm(integer)], result_loc)
604
605 consider_vec_load_i = _consider_vec_load
606 consider_vec_load_f = _consider_vec_load
607
608 def consider_vec_store(self, op):
609 # TODO
610 descr = op.getdescr()
611 assert isinstance(descr, ArrayDescr)
612 assert not descr.is_array_of_pointers() and \
613 not descr.is_array_of_structs()
614 itemsize, _, _ = unpack_arraydescr(descr)
615 args = op.getarglist()
616 base_loc = self.rm.make_sure_var_in_reg(op.getarg(0), args)
617 value_loc = self.make_sure_var_in_reg(op.getarg(2), args)
618 ofs_loc = self.rm.make_sure_var_in_reg(op.getarg(1), args)
619 scale = get_scale(op.getarg(3).getint())
620 ofs = op.getarg(4).getint()
621
622 integer = not (descr.is_array_of_floats() or \
623 descr.getconcrete_type() == FLOAT)
624 self.perform_discard(op, [base_loc, ofs_loc, value_loc, imm(itemsize),
625 imm(scale), imm(ofs), imm(integer)])
626
627 def consider_vec_arith(self, op):
628 lhs = op.getarg(0)
629 assert isinstance(op, VectorOp)
630 size = op.bytesize
631 args = op.getarglist()
632 loc1 = self.make_sure_var_in_reg(op.getarg(1), args)
633 loc0 = self.xrm.force_result_in_reg(op, op.getarg(0), args)
634 self.perform(op, [loc0, loc1, imm(size)], loc0)
635
636 consider_vec_int_add = consider_vec_arith
637 consider_vec_int_sub = consider_vec_arith
638 consider_vec_int_mul = consider_vec_arith
639 consider_vec_float_add = consider_vec_arith
640 consider_vec_float_sub = consider_vec_arith
641 consider_vec_float_mul = consider_vec_arith
642 consider_vec_float_truediv = consider_vec_arith
643 del consider_vec_arith
644
645 def consider_vec_arith_unary(self, op):
646 lhs = op.getarg(0)
647 assert isinstance(lhs, VectorOp)
648 args = op.getarglist()
649 res = self.xrm.force_result_in_reg(op, op.getarg(0), args)
650 self.perform(op, [res, imm(lhs.bytesize)], res)
651
652 consider_vec_float_neg = consider_vec_arith_unary
653 consider_vec_float_abs = consider_vec_arith_unary
654 del consider_vec_arith_unary
655
656 def consider_vec_logic(self, op):
657 lhs = op.getarg(0)
658 assert isinstance(lhs, VectorOp)
659 args = op.getarglist()
660 source = self.make_sure_var_in_reg(op.getarg(1), args)
661 result = self.xrm.force_result_in_reg(op, op.getarg(0), args)
662 self.perform(op, [source, imm(lhs.bytesize)], result)
663
664 def consider_vec_float_eq(self, op):
665 assert isinstance(op, VectorOp)
666 lhs = op.getarg(0)
667 assert isinstance(lhs, VectorOp)
668 args = op.getarglist()
669 # we need to use xmm0
670 lhsloc = self.enforce_var_in_vector_reg(op.getarg(0), args, selected_reg=xmm0)
671 rhsloc = self.make_sure_var_in_reg(op.getarg(1), args)
672 resloc = self.force_allocate_vector_reg_or_cc(op)
673 self.perform(op, [lhsloc, rhsloc, imm(lhs.bytesize)], resloc)
674
675 def enforce_var_in_vector_reg(self, arg, forbidden_vars, selected_reg):
676 """ Enforce the allocation in a specific register. This can even be a forbidden
677 register. If it is forbidden, it will be moved to another register.
678 Use with caution, currently this is only used for the vectorization backend
679 instructions.
680 """
681 xrm = self.xrm
682 curloc = self.loc(arg)
683 if curloc is selected_reg:
684 # nothing to do, it is already in the correct register
685 return selected_reg
686 if selected_reg not in xrm.free_regs:
687 # we need to move some registers
688 variable = None
689 candidate_to_spill = None
690 for var, reg in self.xrm.reg_bindings.items():
691 if reg is selected_reg:
692 variable = var
693 else:
694 if var not in forbidden_vars:
695 candidate_to_spill = var
696 # do we have a free register?
697 if len(xrm.free_regs) == 0:
698 # spill a non forbidden variable
699 if not candidate_to_spill:
700 raise NoVariableToSpill
701 reg = xrm.reg_bindings[candidate_to_spill]
702 xrm._spill_var(forbidden_vars, reg)
703 xrm.free_regs.append(reg)
704 loc = xrm.free_regs.pop()
705 self.assembler.mov(selected_reg, loc)
706 reg = xrm.reg_bindings.get(arg, None)
707 if reg:
708 xrm.free_regs.append(reg)
709 self.assembler.mov(reg, selected_reg)
710 xrm.reg_bindings[variable] = loc
711 xrm.reg_bindings[arg] = selected_reg
712
713 return selected_reg
714 return self.make_sure_var_in_reg(arg, forbidden_vars, selected_reg=selected_reg)
715
716 def force_allocate_vector_reg_or_cc(self, var):
717 assert var.type == INT
718 if self.next_op_can_accept_cc(self.operations, self.rm.position):
719 # hack: return the ebp location to mean "lives in CC". This
720 # ebp will not actually be used, and the location will be freed
721 # after the next op as usual.
722 self.xrm.force_allocate_frame_reg(var)
723 return ebp
724 else:
725 # else, return a regular register (not ebp).
726 return self.xrm.force_allocate_reg(var)
727
728 consider_vec_float_ne = consider_vec_float_eq
729 consider_vec_int_eq = consider_vec_float_eq
730 consider_vec_int_ne = consider_vec_float_eq
731
732 consider_vec_int_and = consider_vec_logic
733 consider_vec_int_or = consider_vec_logic
734 consider_vec_int_xor = consider_vec_logic
735 consider_vec_float_xor = consider_vec_logic
736 del consider_vec_logic
737
738 def consider_vec_pack_i(self, op):
739 # new_res = vec_pack_i(res, src, index, count)
740 assert isinstance(op, VectorOp)
741 arg = op.getarg(1)
742 index = op.getarg(2)
743 count = op.getarg(3)
744 assert isinstance(index, ConstInt)
745 assert isinstance(count, ConstInt)
746 args = op.getarglist()
747 srcloc = self.make_sure_var_in_reg(arg, args)
748 resloc = self.xrm.force_result_in_reg(op, op.getarg(0), args)
749 residx = index.value # where to put it in result?
750 srcidx = 0
751 arglocs = [resloc, srcloc, imm(residx), imm(srcidx),
752 imm(count.value), imm(op.bytesize)]
753 self.perform(op, arglocs, resloc)
754
755 consider_vec_pack_f = consider_vec_pack_i
756
757 def consider_vec_unpack_i(self, op):
758 assert isinstance(op, VectorOp)
759 index = op.getarg(1)
760 count = op.getarg(2)
761 assert isinstance(index, ConstInt)
762 assert isinstance(count, ConstInt)
763 args = op.getarglist()
764 srcloc = self.make_sure_var_in_reg(op.getarg(0), args)
765 if op.is_vector():
766 resloc = self.xrm.force_result_in_reg(op, op.getarg(0), args)
767 size = op.bytesize
768 else:
769 # unpack into iX box
770 resloc = self.force_allocate_reg(op, args)
771 arg = op.getarg(0)
772 assert isinstance(arg, VectorOp)
773 size = arg.bytesize
774 residx = 0
775 args = op.getarglist()
776 arglocs = [resloc, srcloc, imm(residx), imm(index.value), imm(count.value), imm(size)]
777 self.perform(op, arglocs, resloc)
778
779 consider_vec_unpack_f = consider_vec_unpack_i
780
781 def consider_vec_expand_f(self, op):
782 assert isinstance(op, VectorOp)
783 arg = op.getarg(0)
784 args = op.getarglist()
785 if arg.is_constant():
786 resloc = self.xrm.force_allocate_reg(op)
787 srcloc = self.xrm.expand_float(op.bytesize, arg)
788 else:
789 resloc = self.xrm.force_result_in_reg(op, arg, args)
790 srcloc = resloc
791 self.perform(op, [srcloc, imm(op.bytesize)], resloc)
792
793 def consider_vec_expand_i(self, op):
794 assert isinstance(op, VectorOp)
795 arg = op.getarg(0)
796 args = op.getarglist()
797 if arg.is_constant():
798 srcloc = self.rm.convert_to_imm(arg)
799 else:
800 srcloc = self.make_sure_var_in_reg(arg, args)
801 resloc = self.xrm.force_allocate_reg(op, args)
802 self.perform(op, [srcloc, imm(op.bytesize)], resloc)
803
804 def consider_vec_int_signext(self, op):
805 assert isinstance(op, VectorOp)
806 args = op.getarglist()
807 resloc = self.xrm.force_result_in_reg(op, op.getarg(0), args)
808 arg = op.getarg(0)
809 assert isinstance(arg, VectorOp)
810 size = arg.bytesize
811 assert size > 0
812 self.perform(op, [resloc, imm(size), imm(op.bytesize)], resloc)
813
814 def consider_vec_int_is_true(self, op):
815 args = op.getarglist()
816 arg = op.getarg(0)
817 assert isinstance(arg, VectorOp)
818 argloc = self.loc(arg)
819 resloc = self.xrm.force_result_in_reg(op, arg, args)
820 self.perform(op, [resloc,imm(arg.bytesize)], None)
821
822 def _consider_vec(self, op):
823 # pseudo instruction, needed to create a new variable
824 self.xrm.force_allocate_reg(op)
825
826 consider_vec_i = _consider_vec
827 consider_vec_f = _consider_vec
828
829 def consider_vec_cast_float_to_int(self, op):
830 args = op.getarglist()
831 srcloc = self.make_sure_var_in_reg(op.getarg(0), args)
832 resloc = self.xrm.force_result_in_reg(op, op.getarg(0), args)
833 self.perform(op, [srcloc], resloc)
834
835 consider_vec_cast_int_to_float = consider_vec_cast_float_to_int
836 consider_vec_cast_float_to_singlefloat = consider_vec_cast_float_to_int
837 consider_vec_cast_singlefloat_to_float = consider_vec_cast_float_to_int
838
839 def consider_vec_guard_true(self, op):
840 arg = op.getarg(0)
841 loc = self.loc(arg)
842 self.assembler.guard_vector(op, self.loc(arg), True)
843 self.perform_guard(op, [], None)
844
845 def consider_vec_guard_false(self, op):
846 arg = op.getarg(0)
847 loc = self.loc(arg)
848 self.assembler.guard_vector(op, self.loc(arg), False)
849 self.perform_guard(op, [], None)
850