Debian Codegraph @

rpython/jit/backend/ppc/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.llsupport.regalloc import get_scale
8 from rpython.jit.metainterp.resoperation import (rop, ResOperation,
9 VectorOp, VectorGuardOp)
10 from rpython.rlib.objectmodel import we_are_translated
11 from rpython.rtyper.lltypesystem.lloperation import llop
12 from rpython.rtyper.lltypesystem import lltype
13 from rpython.jit.backend.ppc.locations import imm, RegisterLocation
14 from rpython.jit.backend.ppc.arch import IS_BIG_ENDIAN
15 from rpython.jit.backend.ppc.arch import PARAM_SAVE_AREA_OFFSET, WORD
16 import rpython.jit.backend.ppc.register as r
17 import rpython.jit.backend.ppc.condition as c
18 import rpython.jit.backend.ppc.locations as l
19 from rpython.jit.backend.llsupport.asmmemmgr import MachineDataBlockWrapper
20 from rpython.rtyper.lltypesystem import lltype, rffi
21 from rpython.jit.codewriter import longlong
22 from rpython.jit.backend.ppc.detect_feature import detect_vsx
23 from rpython.rlib.objectmodel import always_inline
24 from rpython.jit.backend.llsupport.vector_ext import (VectorExt,
25 OpRestrict, TR_INT64_2)
26
27 def not_implemented(msg):
28 msg = '[ppc/vector_ext] %s\n' % msg
29 if we_are_translated():
30 llop.debug_print(lltype.Void, msg)
31 raise NotImplementedError(msg)
32
33 @always_inline
34 def permi(v1, v2):
35 """ permute immediate for big and little endian """
36 # if v1 == 0 unpacks index 0 of param 1
37 # if v1 == 1 unpacks index 1 of param 1
38 # if v2 == 0 unpacks index 0 of param 2
39 # if v2 == 1 unpacks index 1 of param 2
40 mask = 0
41 if v1 == 0: mask |= 0b01
42 if v1 == 1: mask |= 0b00
43 if v2 == 0: mask |= 0b10
44 if v2 == 1: mask |= 0b00
45 return mask
46
47
48 def flush_vec_cc(asm, regalloc, condition, size, result_loc):
49 # After emitting an instruction that leaves a boolean result in
50 # a condition code (cc), call this. In the common case, result_loc
51 # will be set to SPP by the regalloc, which in this case means
52 # "propagate it between this operation and the next guard by keeping
53 # it in the cc". In the uncommon case, result_loc is another
54 # register, and we emit a load from the cc into this register.
55
56 # Possibly invert the bit in the CR
57 bit, invert = c.encoding[condition]
58 assert 24 <= bit <= 27
59 if invert == 12:
60 pass
61 elif invert == 4:
62 asm.mc.crnor(bit, bit, bit)
63 else:
64 assert 0
65 assert asm.guard_success_cc == c.cond_none
66 #
67 if result_loc is r.SPP:
68 asm.guard_success_cc = condition
69 else:
70 resval = result_loc.value
71 # either doubleword integer 1 (2x) or word integer 1 (4x)
72 ones = regalloc.vrm.get_scratch_reg(type=INT).value
73 zeros = regalloc.vrm.get_scratch_reg(type=INT).value
74 asm.mc.vxor(zeros, zeros, zeros)
75 if size == 4:
76 asm.mc.vspltisw(ones, 1)
77 else:
78 assert size == 8
79 tloc = regalloc.rm.get_scratch_reg()
80 asm.mc.load_imm(tloc, asm.VEC_DOUBLE_WORD_ONES)
81 asm.mc.lvx(ones, 0, tloc.value)
82 asm.mc.vsel(resval, zeros, ones, resval)
83
84 class AltiVectorExt(VectorExt):
85 def setup_once(self, asm):
86 if detect_vsx():
87 self.enable(16, accum=True)
88 asm.setup_once_vector()
89 self._setup = True
90 AltiVectorExt.TR_MAPPING[rop.VEC_CAST_INT_TO_FLOAT] = OpRestrict([TR_INT64_2])
91
92 class VectorAssembler(object):
93 _mixin_ = True
94
95 VEC_DOUBLE_WORD_ONES = 0
96
97 def setup_once_vector(self):
98 if IS_BIG_ENDIAN:
99 # 2x 64 bit signed integer(1) BE
100 data = (b'\x00' * 7 + b'\x01') * 2
101 else:
102 # 2x 64 bit signed integer(1) LE
103 data = (b'\x01' + b'\x00' * 7) * 2
104 datablockwrapper = MachineDataBlockWrapper(self.cpu.asmmemmgr, [])
105 mem = datablockwrapper.malloc_aligned(len(data), alignment=16)
106 datablockwrapper.done()
107 addr = rffi.cast(rffi.CArrayPtr(lltype.Char), mem)
108 for i in range(len(data)):
109 addr[i] = data[i]
110 self.VEC_DOUBLE_WORD_ONES = mem
111
112 def emit_vec_load_f(self, op, arglocs, regalloc):
113 resloc, baseloc, indexloc, size_loc, ofs, integer_loc = arglocs
114 indexloc = self._apply_offset(indexloc, ofs)
115 itemsize = size_loc.value
116 if integer_loc.value:
117 self.mc.lxvd2x(resloc.value, indexloc.value, baseloc.value)
118 elif itemsize == 4:
119 self.mc.lxvw4x(resloc.value, indexloc.value, baseloc.value)
120 elif itemsize == 8:
121 self.mc.lxvd2x(resloc.value, indexloc.value, baseloc.value)
122 else:
123 not_implemented("vec_load_f itemsize %d" % itemsize)
124
125 emit_vec_load_i = emit_vec_load_f
126
127 def emit_vec_store(self, op, arglocs, regalloc):
128 baseloc, indexloc, valueloc, sizeloc, baseofs, \
129 integer_loc = arglocs
130 indexloc = self._apply_offset(indexloc, baseofs)
131 assert baseofs.value == 0
132 if integer_loc.value:
133 self.mc.stxvd2x(valueloc.value, indexloc.value, baseloc.value)
134 else:
135 itemsize = sizeloc.value
136 if itemsize == 4:
137 self.mc.stxvw4x(valueloc.value, indexloc.value, baseloc.value)
138 elif itemsize == 8:
139 self.mc.stxvd2x(valueloc.value, indexloc.value, baseloc.value)
140 else:
141 not_implemented("vec_store itemsize %d" % itemsize)
142
143 def emit_vec_int_add(self, op, arglocs, regalloc):
144 resloc, loc0, loc1, size_loc = arglocs
145 size = size_loc.value
146 if size == 1:
147 self.mc.vaddubm(resloc.value, loc0.value, loc1.value)
148 elif size == 2:
149 self.mc.vadduhm(resloc.value, loc0.value, loc1.value)
150 elif size == 4:
151 self.mc.vadduwm(resloc.value, loc0.value, loc1.value)
152 elif size == 8:
153 self.mc.vaddudm(resloc.value, loc0.value, loc1.value)
154
155 def emit_vec_int_sub(self, op, arglocs, regalloc):
156 resloc, loc0, loc1, size_loc = arglocs
157 size = size_loc.value
158 if size == 1:
159 self.mc.vsububm(resloc.value, loc0.value, loc1.value)
160 elif size == 2:
161 self.mc.vsubuhm(resloc.value, loc0.value, loc1.value)
162 elif size == 4:
163 self.mc.vsubuwm(resloc.value, loc0.value, loc1.value)
164 elif size == 8:
165 self.mc.vsubudm(resloc.value, loc0.value, loc1.value)
166
167 def emit_vec_float_add(self, op, arglocs, regalloc):
168 resloc, loc0, loc1, itemsize_loc = arglocs
169 itemsize = itemsize_loc.value
170 if itemsize == 4:
171 self.mc.xvaddsp(resloc.value, loc0.value, loc1.value)
172 elif itemsize == 8:
173 self.mc.xvadddp(resloc.value, loc0.value, loc1.value)
174
175 def emit_vec_float_sub(self, op, arglocs, regalloc):
176 resloc, loc0, loc1, itemsize_loc = arglocs
177 itemsize = itemsize_loc.value
178 if itemsize == 4:
179 self.mc.xvsubsp(resloc.value, loc0.value, loc1.value)
180 elif itemsize == 8:
181 self.mc.xvsubdp(resloc.value, loc0.value, loc1.value)
182
183 def emit_vec_float_mul(self, op, arglocs, regalloc):
184 resloc, loc0, loc1, itemsize_loc = arglocs
185 itemsize = itemsize_loc.value
186 if itemsize == 4:
187 self.mc.xvmulsp(resloc.value, loc0.value, loc1.value)
188 elif itemsize == 8:
189 self.mc.xvmuldp(resloc.value, loc0.value, loc1.value)
190
191 def emit_vec_float_truediv(self, op, arglocs, regalloc):
192 resloc, loc0, loc1, itemsize_loc = arglocs
193 itemsize = itemsize_loc.value
194 if itemsize == 4:
195 self.mc.xvdivsp(resloc.value, loc0.value, loc1.value)
196 elif itemsize == 8:
197 self.mc.xvdivdp(resloc.value, loc0.value, loc1.value)
198
199 def emit_vec_int_mul(self, op, arglocs, regalloc):
200 not_implemented("vec_int_mul must never be emitted by the backend")
201
202 def emit_vec_int_and(self, op, arglocs, regalloc):
203 resloc, loc0, loc1, sizeloc = arglocs
204 self.mc.vand(resloc.value, loc0.value, loc1.value)
205
206 def emit_vec_int_or(self, op, arglocs, regalloc):
207 resloc, loc0, loc1, sizeloc = arglocs
208 self.mc.vor(resloc.value, loc0.value, loc1.value)
209
210 def emit_vec_int_xor(self, op, arglocs, regalloc):
211 resloc, loc0, loc1, sizeloc = arglocs
212 self.mc.vxor(resloc.value, loc0.value, loc1.value)
213
214 def emit_vec_int_signext(self, op, arglocs, regalloc):
215 resloc, loc0, osizeloc, nsizeloc = arglocs
216 # signext is only allowed if the data type sizes do not change.
217 # e.g. [byte,byte] = sign_ext([byte, byte]), a simple move is sufficient!
218 src = loc0.value
219 res = resloc.value
220 osize = osizeloc.value
221 nsize = nsizeloc.value
222 if osize == nsize:
223 self.regalloc_mov(loc0, resloc)
224 else:
225 assert (osize == 4 and nsize == 8) or (osize == 8 and nsize == 4)
226 self.mc.load_imm(r.SCRATCH2, PARAM_SAVE_AREA_OFFSET)
227 self.mc.stvx(src, r.SCRATCH2.value, r.SP.value)
228 self.mc.load_imm(r.SCRATCH2, PARAM_SAVE_AREA_OFFSET+16)
229 self.mc.stvx(res, r.SCRATCH2.value, r.SP.value)
230 for j in range(2): # at most 2 operations
231 off = PARAM_SAVE_AREA_OFFSET
232 i = j
233 if not IS_BIG_ENDIAN:
234 i = (16 // osize) - 1 - i
235 off += osize * i
236 self._load_from_sp(r.SCRATCH.value, osize, off)
237 off = PARAM_SAVE_AREA_OFFSET
238 i = j
239 if not IS_BIG_ENDIAN:
240 i = (16 // nsize) - 1 - i
241 off += nsize * i
242 self._store_to_sp(r.SCRATCH.value, nsize, off+16)
243 self.mc.lvx(res, r.SCRATCH2.value, r.SP.value)
244
245 def emit_vec_float_abs(self, op, arglocs, regalloc):
246 resloc, argloc, sizeloc = arglocs
247 size = sizeloc.value
248 if size == 4:
249 self.mc.xvabssp(resloc.value, argloc.value)
250 elif size == 8:
251 self.mc.xvabsdp(resloc.value, argloc.value)
252 else:
253 not_implemented("float abs for size %d" % size)
254
255 def emit_vec_float_neg(self, op, arglocs, regalloc):
256 resloc, argloc, sizeloc = arglocs
257 size = sizeloc.value
258 if size == 4:
259 self.mc.xvnegsp(resloc.value, argloc.value)
260 elif size == 8:
261 self.mc.xvnegdp(resloc.value, argloc.value)
262 else:
263 not_implemented("float neg for size %d" % size)
264
265 def emit_vec_guard_true(self, guard_op, arglocs, regalloc):
266 self._emit_guard(guard_op, arglocs)
267
268 def emit_vec_guard_false(self, guard_op, arglocs, regalloc):
269 self.guard_success_cc = c.negate(self.guard_success_cc)
270 self._emit_guard(guard_op, arglocs)
271
272 def _update_at_exit(self, fail_locs, fail_args, faildescr, regalloc):
273 """ If accumulation is done in this loop, at the guard exit
274 some vector registers must be adjusted to yield the correct value
275 """
276 if not isinstance(faildescr, ResumeGuardDescr):
277 return
278 accum_info = faildescr.rd_vector_info
279 while accum_info:
280 pos = accum_info.getpos_in_failargs()
281 scalar_loc = fail_locs[pos]
282 vector_loc = accum_info.location
283 # the upper elements will be lost if saved to the stack!
284 scalar_arg = accum_info.getoriginal()
285 orig_scalar_loc = scalar_loc
286 if not scalar_loc.is_reg():
287 if scalar_arg.type == FLOAT:
288 scalar_loc = r.FP_SCRATCH
289 else:
290 scalar_loc = r.SCRATCH
291 self.regalloc_mov(orig_scalar_loc, scalar_loc)
292 assert scalar_arg is not None
293 op = accum_info.accum_operation
294 self._accum_reduce(op, scalar_arg, vector_loc, scalar_loc)
295 if orig_scalar_loc is not scalar_loc:
296 self.regalloc_mov(scalar_loc, orig_scalar_loc)
297 accum_info = accum_info.next()
298
299 def _accum_reduce(self, op, arg, accumloc, targetloc):
300 # Currently the accumulator can ONLY be the biggest
301 # 64 bit float/int
302 tgt = targetloc.value
303 acc = accumloc.value
304 if arg.type == FLOAT:
305 # r = (r[0]+r[1],r[0]+r[1])
306 self.mc.xxpermdi(tgt, acc, acc, 0b10)
307 if op == '+':
308 self.mc.xsadddp(tgt, tgt, acc)
309 elif op == '*':
310 self.mc.xsmuldp(tgt, tgt, acc)
311 else:
312 not_implemented("sum not implemented")
313 return
314 else:
315 assert arg.type == INT
316 self.mc.load_imm(r.SCRATCH2, PARAM_SAVE_AREA_OFFSET)
317 self.mc.stvx(acc, r.SCRATCH2.value, r.SP.value)
318 self.mc.load(tgt, r.SP.value, PARAM_SAVE_AREA_OFFSET)
319 self.mc.load(r.SCRATCH2.value, r.SP.value, PARAM_SAVE_AREA_OFFSET+8)
320 if op == '+':
321 self.mc.add(tgt, tgt, r.SCRATCH2.value)
322 elif op == '*':
323 self.mc.mulld(tgt, tgt, r.SCRATCH2.value)
324 else:
325 not_implemented("sum not implemented")
326 return
327
328 not_implemented("reduce sum for %s not impl." % arg)
329
330 def emit_vec_int_is_true(self, op, arglocs, regalloc):
331 assert isinstance(op, VectorOp)
332 resloc, argloc, sizeloc = arglocs
333 size = sizeloc.value
334 tmp = regalloc.vrm.get_scratch_reg(type=INT).value
335 self.mc.vxor(tmp, tmp, tmp)
336 # argloc[i] > 0:
337 # For an unsigned integer that is equivalent to argloc[i] != 0
338 if size == 1:
339 self.mc.vcmpgtubx(resloc.value, argloc.value, tmp)
340 elif size == 2:
341 self.mc.vcmpgtuhx(resloc.value, argloc.value, tmp)
342 elif size == 4:
343 self.mc.vcmpgtuwx(resloc.value, argloc.value, tmp)
344 elif size == 8:
345 self.mc.vcmpgtudx(resloc.value, argloc.value, tmp)
346 flush_vec_cc(self, regalloc, c.VNEI, op.bytesize, resloc)
347
348 def emit_vec_float_eq(self, op, arglocs, regalloc):
349 assert isinstance(op, VectorOp)
350 resloc, loc1, loc2, sizeloc = arglocs
351 size = sizeloc.value
352 tmp = regalloc.vrm.get_scratch_reg().value
353 offloc = regalloc.rm.get_scratch_reg()
354 off = offloc.value
355 # SP is always 16 byte aligned, and PARAM_SAVE_AREA_OFFSET % 16 == 0
356 self.mc.load_imm(offloc, PARAM_SAVE_AREA_OFFSET)
357 if size == 4:
358 self.mc.xvcmpeqspx(tmp, loc1.value, loc2.value)
359 self.mc.stxvw4x(tmp, off, r.SP.value)
360 elif size == 8:
361 self.mc.xvcmpeqdpx(tmp, loc1.value, loc2.value)
362 self.mc.stxvd2x(tmp, off, r.SP.value)
363 else:
364 not_implemented("[ppc/assembler] float == for size %d" % size)
365 self.mc.lvx(resloc.value, off, r.SP.value)
366 flush_vec_cc(self, regalloc, c.VEQI, op.bytesize, resloc)
367
368 def emit_vec_float_xor(self, op, arglocs, regalloc):
369 resloc, l0, l1, sizeloc = arglocs
370 res = resloc.value
371 r0 = l0.value
372 r1 = l1.value
373 self.mc.xxlxor(res, r0, r1)
374
375 def emit_vec_float_ne(self, op, arglocs, regalloc):
376 assert isinstance(op, VectorOp)
377 resloc, loc1, loc2, sizeloc = arglocs
378 size = sizeloc.value
379 tmp = regalloc.vrm.get_scratch_reg().value
380 offloc = regalloc.rm.get_scratch_reg()
381 off = offloc.value
382 # SP is always 16 byte aligned, and PARAM_SAVE_AREA_OFFSET % 16 == 0
383 self.mc.load_imm(offloc, PARAM_SAVE_AREA_OFFSET)
384 if size == 4:
385 self.mc.xvcmpeqspx(tmp, loc1.value, loc2.value)
386 self.mc.stxvw4x(tmp, off, r.SP.value)
387 elif size == 8:
388 self.mc.xvcmpeqdpx(tmp, loc1.value, loc2.value)
389 self.mc.stxvd2x(tmp, off, r.SP.value)
390 else:
391 not_implemented("float == for size %d" % size)
392 res = resloc.value
393 self.mc.lvx(res, off, r.SP.value)
394 self.mc.vnor(res, res, res) # complement
395 flush_vec_cc(self, regalloc, c.VNEI, op.bytesize, resloc)
396
397 def emit_vec_cast_int_to_float(self, op, arglocs, regalloc):
398 res, l0 = arglocs
399 offloc = regalloc.rm.get_scratch_reg()
400 off = offloc.value
401 # SP is always 16 byte aligned, and PARAM_SAVE_AREA_OFFSET % 16 == 0
402 self.mc.load_imm(offloc, PARAM_SAVE_AREA_OFFSET)
403 self.mc.stvx(l0.value, off, r.SP.value)
404 self.mc.lxvd2x(res.value, off, r.SP.value)
405 self.mc.xvcvsxddp(res.value, res.value)
406
407 def emit_vec_int_eq(self, op, arglocs, regalloc):
408 assert isinstance(op, VectorOp)
409 res, l0, l1, sizeloc = arglocs
410 size = sizeloc.value
411 if size == 1:
412 self.mc.vcmpequbx(res.value, l0.value, l1.value)
413 elif size == 2:
414 self.mc.vcmpequhx(res.value, l0.value, l1.value)
415 elif size == 4:
416 self.mc.vcmpequwx(res.value, l0.value, l1.value)
417 elif size == 8:
418 self.mc.vcmpequdx(res.value, l0.value, l1.value)
419 flush_vec_cc(self, regalloc, c.VEQI, op.bytesize, res)
420
421 def emit_vec_int_ne(self, op, arglocs, regalloc):
422 assert isinstance(op, VectorOp)
423 res, l0, l1, sizeloc = arglocs
424 size = sizeloc.value
425 tmp = regalloc.vrm.get_scratch_reg(type=INT).value
426 self.mc.vxor(tmp, tmp, tmp)
427 if size == 1:
428 self.mc.vcmpequbx(res.value, res.value, tmp)
429 elif size == 2:
430 self.mc.vcmpequhx(res.value, res.value, tmp)
431 elif size == 4:
432 self.mc.vcmpequwx(res.value, res.value, tmp)
433 elif size == 8:
434 self.mc.vcmpequdx(res.value, res.value, tmp)
435 self.mc.vnor(res.value, res.value, res.value)
436 flush_vec_cc(self, regalloc, c.VEQI, op.bytesize, res)
437
438 def emit_vec_expand_f(self, op, arglocs, regalloc):
439 assert isinstance(op, VectorOp)
440 resloc, srcloc = arglocs
441 size = op.bytesize
442 res = resloc.value
443 if isinstance(srcloc, l.ConstFloatLoc):
444 # they are aligned!
445 assert size == 8
446 tloc = regalloc.rm.get_scratch_reg()
447 self.mc.load_imm(tloc, srcloc.value)
448 self.mc.lxvd2x(res, 0, tloc.value)
449 elif size == 8:
450 # splat the low of src to both slots in res
451 src = srcloc.value
452 self.mc.xxspltdl(res, src, src)
453 else:
454 not_implemented("vec expand in this combination not supported")
455
456 def emit_vec_expand_i(self, op, arglocs, regalloc):
457 assert isinstance(op, VectorOp)
458 res, l0, off = arglocs
459 size = op.bytesize
460
461 idx = 0
462 size = op.bytesize
463 if not IS_BIG_ENDIAN:
464 idx = (16 // size) - 1 - idx
465 idx *= size
466 self._store_to_sp(l0.value, size, idx+PARAM_SAVE_AREA_OFFSET)
467 if size == 8:
468 idx = 1
469 if not IS_BIG_ENDIAN:
470 idx = (16 // size) - 1 - idx
471 idx *= size
472 self._store_to_sp(l0.value, size, idx+PARAM_SAVE_AREA_OFFSET)
473 self.mc.load_imm(r.SCRATCH2, off.value)
474 self.mc.lvx(res.value, r.SCRATCH2.value, r.SP.value)
475 if size == 1:
476 self.mc.vspltb(res.value, res.value, 0b0000)
477 elif size == 2:
478 self.mc.vsplth(res.value, res.value, 0b000)
479 elif size == 4:
480 self.mc.vspltw(res.value, res.value, 0b00)
481 elif size == 8:
482 pass
483 else:
484 not_implemented("expand int size not impl")
485
486 def emit_vec_pack_i(self, op, arglocs, regalloc):
487 assert isinstance(op, VectorOp)
488 resultloc, vloc, sourceloc, residxloc, srcidxloc, countloc = arglocs
489 residx = residxloc.value
490 count = countloc.value
491 res = resultloc.value
492 vector = vloc.value
493 src = sourceloc.value
494 size = op.bytesize
495 assert resultloc.is_vector_reg() # vector <- reg
496 self.mc.load_imm(r.SCRATCH2, PARAM_SAVE_AREA_OFFSET)
497 self.mc.stvx(vector, r.SCRATCH2.value, r.SP.value)
498 idx = residx
499 if size == 8:
500 if not IS_BIG_ENDIAN:
501 idx = (16 // size) - 1 - idx
502 self.mc.store(src, r.SP.value, PARAM_SAVE_AREA_OFFSET+8*idx)
503 elif size == 4:
504 for j in range(count):
505 idx = j + residx
506 if not IS_BIG_ENDIAN:
507 idx = (16 // size) - 1 - idx
508 self.mc.stw(src, r.SP.value, PARAM_SAVE_AREA_OFFSET+4*idx)
509 elif size == 2:
510 for j in range(count):
511 idx = j + residx
512 if not IS_BIG_ENDIAN:
513 idx = (16 // size) - 1 - idx
514 self.mc.sth(src, r.SP.value, PARAM_SAVE_AREA_OFFSET+2*idx)
515 elif size == 1:
516 for j in range(count):
517 idx = j + residx
518 if not IS_BIG_ENDIAN:
519 idx = (16 // size) - 1 - idx
520 self.mc.stb(src, r.SP.value, PARAM_SAVE_AREA_OFFSET+idx)
521 self.mc.lvx(res, r.SCRATCH2.value, r.SP.value)
522
523 def _load_from_sp(self, res, size, off):
524 if size == 8:
525 self.mc.load(res, r.SP.value, off)
526 return True
527 elif size == 4:
528 self.mc.lwa(res, r.SP.value, off)
529 return True
530 elif size == 2:
531 self.mc.lha(res, r.SP.value, off)
532 return True
533 elif size == 1:
534 self.mc.lbz(res, r.SP.value, off)
535 self.mc.extsb(res, res)
536 return True
537 return False
538
539 def _store_to_sp(self, res, size, off):
540 if size == 8:
541 self.mc.store(res, r.SP.value, off)
542 return True
543 elif size == 4:
544 self.mc.stw(res, r.SP.value, off)
545 return True
546 elif size == 2:
547 self.mc.sth(res, r.SP.value, off)
548 return True
549 elif size == 1:
550 self.mc.stb(res, r.SP.value, off)
551 return True
552 return False
553
554 def emit_vec_unpack_i(self, op, arglocs, regalloc):
555 assert isinstance(op, VectorOp)
556 resloc, srcloc, idxloc, countloc, sizeloc = arglocs
557 idx = idxloc.value
558 res = resloc.value
559 src = srcloc.value
560 size = sizeloc.value
561 count = countloc.value
562 newsize = op.bytesize
563 if count == 1:
564 assert srcloc.is_vector_reg()
565 assert not resloc.is_vector_reg()
566 off = PARAM_SAVE_AREA_OFFSET
567 self.mc.load_imm(r.SCRATCH2, off)
568 self.mc.stvx(src, r.SCRATCH2.value, r.SP.value)
569 if not IS_BIG_ENDIAN:
570 idx = (16 // size) - 1 - idx
571 off += size * idx
572 if self._load_from_sp(res, size, off):
573 return
574 else:
575 # count is not 1, but only 2 is supported for i32
576 # 4 for i16 and 8 for i8.
577 src = srcloc.value
578 res = resloc.value
579
580 self.mc.load_imm(r.SCRATCH2, PARAM_SAVE_AREA_OFFSET)
581 self.mc.stvx(src, r.SCRATCH2.value, r.SP.value)
582 self.mc.load_imm(r.SCRATCH2, PARAM_SAVE_AREA_OFFSET+16)
583 self.mc.stvx(res, r.SCRATCH2.value, r.SP.value)
584 for j in range(count):
585 off = PARAM_SAVE_AREA_OFFSET
586 i = j+idx
587 if not IS_BIG_ENDIAN:
588 i = (16 // size) - 1 - i
589 off += size * i
590 self._load_from_sp(r.SCRATCH.value, size, off)
591 off = PARAM_SAVE_AREA_OFFSET
592 i = j
593 if not IS_BIG_ENDIAN:
594 i = (16 // size) - 1 - i
595 off += size * i
596 self._store_to_sp(r.SCRATCH.value, newsize, off+16)
597 self.mc.lvx(res, r.SCRATCH2.value, r.SP.value)
598 return
599
600 not_implemented("%d bit integer, count %d" % \
601 (size*8, count))
602
603 def emit_vec_pack_f(self, op, arglocs, regalloc):
604 assert isinstance(op, VectorOp)
605 resloc, vloc, srcloc, residxloc, srcidxloc, countloc = arglocs
606 vec = vloc.value
607 res = resloc.value
608 src = srcloc.value
609 residx = residxloc.value
610 srcidx = srcidxloc.value
611 # srcloc is always a floating point register f, this means it is
612 # vsr[0] == valueof(f)
613 if srcidx == 0:
614 if residx == 0:
615 # r = (s[0], v[1])
616 self.mc.xxpermdi(res, src, vec, permi(0,1))
617 else:
618 assert residx == 1
619 # r = (v[0], s[0])
620 self.mc.xxpermdi(res, vec, src, permi(1,1))
621 else:
622 assert srcidx == 1
623 if residx == 0:
624 # r = (s[1], v[1])
625 self.mc.xxpermdi(res, src, vec, permi(1,1))
626 else:
627 assert residx == 1
628 # r = (v[0], s[1])
629 self.mc.xxpermdi(res, vec, src, permi(0,1))
630
631 def emit_vec_unpack_f(self, op, arglocs, regalloc):
632 assert isinstance(op, VectorOp)
633 resloc, srcloc, srcidxloc, countloc = arglocs
634 res = resloc.value
635 src = srcloc.value
636 srcidx = srcidxloc.value
637 size = op.bytesize
638 # srcloc is always a floating point register f, this means it is
639 # vsr[0] == valueof(f)
640 if srcidx == 0:
641 # r = (s[0], s[1])
642 self.mc.xxpermdi(res, src, src, permi(0,1))
643 return
644 else:
645 # r = (s[1], s[0])
646 self.mc.xxpermdi(res, src, src, permi(1,0))
647 return
648 not_implemented("unpack for combination src %d -> res %d" % (srcidx, residx))
649
650 def emit_vec_cast_float_to_int(self, op, arglocs, regalloc):
651 res, l0 = arglocs
652 offloc = regalloc.rm.get_scratch_reg()
653 v0 = regalloc.vrm.get_scratch_reg(type=INT)
654 off = offloc.value
655 # SP is always 16 byte aligned, and PARAM_SAVE_AREA_OFFSET % 16 == 0
656 self.mc.load_imm(offloc, PARAM_SAVE_AREA_OFFSET)
657 self.mc.xvcvdpsxds(res.value, l0.value)
658
659 # needed as soon as PPC's support_singlefloat is implemented!
660 #def genop_vec_cast_singlefloat_to_float(self, op, arglocs, regalloc):
661 # self.mc.CVTPS2PD(resloc, arglocs[0])
662
663 def emit_vec_f(self, op, arglocs, regalloc):
664 pass
665 emit_vec_i = emit_vec_f
666
667 class VectorRegalloc(object):
668 _mixin_ = True
669
670 def force_allocate_vector_reg(self, op):
671 forbidden_vars = self.vrm.temp_boxes
672 return self.vrm.force_allocate_reg(op, forbidden_vars)
673
674 def force_allocate_vector_reg_or_cc(self, op):
675 assert op.type == INT
676 if self.next_op_can_accept_cc(self.operations, self.rm.position):
677 # hack: return the SPP location to mean "lives in CC". This
678 # SPP will not actually be used, and the location will be freed
679 # after the next op as usual.
680 self.rm.force_allocate_frame_reg(op)
681 return r.SPP
682 else:
683 return self.force_allocate_vector_reg(op)
684
685 def ensure_vector_reg(self, box):
686 return self.vrm.make_sure_var_in_reg(box,
687 forbidden_vars=self.vrm.temp_boxes)
688
689 def _prepare_load(self, op):
690 descr = op.getdescr()
691 assert isinstance(descr, ArrayDescr)
692 assert not descr.is_array_of_pointers() and \
693 not descr.is_array_of_structs()
694 itemsize, ofs, _ = unpack_arraydescr(descr)
695 integer = not (descr.is_array_of_floats() or descr.getconcrete_type() == FLOAT)
696 args = op.getarglist()
697 a0 = op.getarg(0)
698 a1 = op.getarg(1)
699 base_loc = self.ensure_reg(a0)
700 ofs_loc = self.ensure_reg(a1)
701 result_loc = self.force_allocate_vector_reg(op)
702 return [result_loc, base_loc, ofs_loc, imm(itemsize), imm(ofs),
703 imm(integer)]
704
705 prepare_vec_load_i = _prepare_load
706 prepare_vec_load_f = _prepare_load
707
708 def prepare_vec_arith(self, op):
709 assert isinstance(op, VectorOp)
710 a0 = op.getarg(0)
711 a1 = op.getarg(1)
712 size = op.bytesize
713 args = op.getarglist()
714 loc0 = self.ensure_vector_reg(a0)
715 loc1 = self.ensure_vector_reg(a1)
716 resloc = self.force_allocate_vector_reg(op)
717 return [resloc, loc0, loc1, imm(size)]
718
719 prepare_vec_int_add = prepare_vec_arith
720 prepare_vec_int_sub = prepare_vec_arith
721 prepare_vec_int_mul = prepare_vec_arith
722 prepare_vec_float_add = prepare_vec_arith
723 prepare_vec_float_sub = prepare_vec_arith
724 prepare_vec_float_mul = prepare_vec_arith
725 prepare_vec_float_truediv = prepare_vec_arith
726
727 # logic functions
728 prepare_vec_int_and = prepare_vec_arith
729 prepare_vec_int_or = prepare_vec_arith
730 prepare_vec_int_xor = prepare_vec_arith
731 prepare_vec_float_xor = prepare_vec_arith
732 del prepare_vec_arith
733
734 def prepare_vec_bool(self, op):
735 assert isinstance(op, VectorOp)
736 a0 = op.getarg(0)
737 a1 = op.getarg(1)
738 size = op.bytesize
739 args = op.getarglist()
740 loc0 = self.ensure_vector_reg(a0)
741 loc1 = self.ensure_vector_reg(a1)
742 resloc = self.force_allocate_vector_reg_or_cc(op)
743 return [resloc, loc0, loc1, imm(size)]
744
745 prepare_vec_float_eq = prepare_vec_bool
746 prepare_vec_float_ne = prepare_vec_bool
747 prepare_vec_int_eq = prepare_vec_bool
748 prepare_vec_int_ne = prepare_vec_bool
749 del prepare_vec_bool
750
751 def prepare_vec_store(self, op):
752 descr = op.getdescr()
753 assert isinstance(descr, ArrayDescr)
754 assert not descr.is_array_of_pointers() and \
755 not descr.is_array_of_structs()
756 itemsize, ofs, _ = unpack_arraydescr(descr)
757 a0 = op.getarg(0)
758 a1 = op.getarg(1)
759 a2 = op.getarg(2)
760 baseloc = self.ensure_reg(a0)
761 ofsloc = self.ensure_reg(a1)
762 valueloc = self.ensure_vector_reg(a2)
763
764 integer = not (descr.is_array_of_floats() or descr.getconcrete_type() == FLOAT)
765 return [baseloc, ofsloc, valueloc,
766 imm(itemsize), imm(ofs), imm(integer)]
767
768 def prepare_vec_int_signext(self, op):
769 assert isinstance(op, VectorOp)
770 a0 = op.getarg(0)
771 assert isinstance(a0, VectorOp)
772 loc0 = self.ensure_vector_reg(a0)
773 resloc = self.force_allocate_vector_reg(op)
774 return [resloc, loc0, imm(a0.bytesize), imm(op.bytesize)]
775
776 def prepare_vec_arith_unary(self, op):
777 assert isinstance(op, VectorOp)
778 a0 = op.getarg(0)
779 loc0 = self.ensure_vector_reg(a0)
780 resloc = self.force_allocate_vector_reg(op)
781 sizeloc = imm(op.bytesize)
782 return [resloc, loc0, sizeloc]
783
784 prepare_vec_float_neg = prepare_vec_arith_unary
785 prepare_vec_float_abs = prepare_vec_arith_unary
786 del prepare_vec_arith_unary
787
788 def prepare_vec_pack_i(self, op):
789 # new_res = vec_pack_i(res, src, index, count)
790 assert isinstance(op, VectorOp)
791 arg = op.getarg(1)
792 index = op.getarg(2)
793 count = op.getarg(3)
794 assert isinstance(index, ConstInt)
795 assert isinstance(count, ConstInt)
796 vloc = self.ensure_vector_reg(op.getarg(0))
797 srcloc = self.ensure_reg(arg)
798 resloc = self.force_allocate_vector_reg(op)
799 residx = index.value # where to put it in result?
800 srcidx = 0
801 return [resloc, vloc, srcloc, imm(residx), imm(srcidx), imm(count.value)]
802
803 def prepare_vec_pack_f(self, op):
804 # new_res = vec_pack_f(res, src, index, count)
805 assert isinstance(op, VectorOp)
806 arg = op.getarg(1)
807 index = op.getarg(2)
808 count = op.getarg(3)
809 assert isinstance(index, ConstInt)
810 assert isinstance(count, ConstInt)
811 assert not arg.is_vector()
812 srcloc = self.ensure_reg(arg)
813 vloc = self.ensure_vector_reg(op.getarg(0))
814 if op.is_vector():
815 resloc = self.force_allocate_vector_reg(op)
816 else:
817 resloc = self.force_allocate_reg(op)
818 residx = index.value # where to put it in result?
819 srcidx = 0
820 return [resloc, vloc, srcloc, imm(residx), imm(srcidx), imm(count.value)]
821
822 def prepare_vec_unpack_f(self, op):
823 index = op.getarg(1)
824 count = op.getarg(2)
825 assert isinstance(index, ConstInt)
826 assert isinstance(count, ConstInt)
827 srcloc = self.ensure_vector_reg(op.getarg(0))
828 resloc = self.force_allocate_reg(op)
829 return [resloc, srcloc, imm(index.value), imm(count.value)]
830
831 def prepare_vec_unpack_i(self, op):
832 assert isinstance(op, VectorOp)
833 index = op.getarg(1)
834 count = op.getarg(2)
835 assert isinstance(index, ConstInt)
836 assert isinstance(count, ConstInt)
837 arg = op.getarg(0)
838 if arg.is_vector():
839 srcloc = self.ensure_vector_reg(arg)
840 assert isinstance(arg, VectorOp)
841 size = arg.bytesize
842 else:
843 # unpack
844 srcloc = self.ensure_reg(arg)
845 size = WORD
846 if op.is_vector():
847 resloc = self.force_allocate_vector_reg(op)
848 else:
849 resloc = self.force_allocate_reg(op)
850 return [resloc, srcloc, imm(index.value), imm(count.value), imm(size)]
851
852 def expand_float(self, size, box):
853 adr = self.assembler.datablockwrapper.malloc_aligned(16, 16)
854 fs = box.getfloatstorage()
855 rffi.cast(rffi.CArrayPtr(longlong.FLOATSTORAGE), adr)[0] = fs
856 rffi.cast(rffi.CArrayPtr(longlong.FLOATSTORAGE), adr)[1] = fs
857 return l.ConstFloatLoc(adr)
858
859 def prepare_vec_expand_f(self, op):
860 assert isinstance(op, VectorOp)
861 arg = op.getarg(0)
862 if arg.is_constant():
863 l0 = self.expand_float(op.bytesize, arg)
864 res = self.force_allocate_vector_reg(op)
865 else:
866 l0 = self.ensure_reg(arg)
867 res = self.force_allocate_vector_reg(op)
868 return [res, l0]
869
870 def prepare_vec_expand_i(self, op):
871 assert isinstance(op, VectorOp)
872 arg = op.getarg(0)
873 mc = self.assembler.mc
874 if arg.is_constant():
875 assert isinstance(arg, ConstInt)
876 l0 = self.rm.get_scratch_reg()
877 mc.load_imm(l0, arg.value)
878 else:
879 l0 = self.ensure_reg(arg)
880 res = self.force_allocate_vector_reg(op)
881 return [res, l0, imm(PARAM_SAVE_AREA_OFFSET)]
882
883 def prepare_vec_int_is_true(self, op):
884 assert isinstance(op, VectorOp)
885 arg = op.getarg(0)
886 assert isinstance(arg, VectorOp)
887 argloc = self.ensure_vector_reg(arg)
888 resloc = self.force_allocate_vector_reg_or_cc(op)
889 return [resloc, argloc, imm(arg.bytesize)]
890
891 def _prepare_vec(self, op):
892 # pseudo instruction, needed to allocate a register for a new variable
893 return [self.force_allocate_vector_reg(op)]
894
895 prepare_vec_i = _prepare_vec
896 prepare_vec_f = _prepare_vec
897
898 def prepare_vec_cast_float_to_int(self, op):
899 l0 = self.ensure_vector_reg(op.getarg(0))
900 res = self.force_allocate_vector_reg(op)
901 return [res, l0]
902
903 prepare_vec_cast_int_to_float = prepare_vec_cast_float_to_int
904
905 def prepare_vec_guard_true(self, op):
906 self.assembler.guard_success_cc = c.VEQ
907 return self._prepare_guard(op)
908
909 def prepare_vec_guard_false(self, op):
910 self.assembler.guard_success_cc = c.VNE
911 return self._prepare_guard(op)
912