Back end work for short vector SIMD packs
[sbcl.git] / src / compiler / x86-64 / type-vops.lisp
1 ;;;; type testing and checking VOPs for the x86-64 VM
2
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
5 ;;;;
6 ;;;; This software is derived from the CMU CL system, which was
7 ;;;; written at Carnegie Mellon University and released into the
8 ;;;; public domain. The software is in the public domain and is
9 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
10 ;;;; files for more information.
11
12 (in-package "SB!VM")
13 \f
14 ;;;; test generation utilities
15
16 ;;; Optimize the case of moving a 64-bit value into RAX when not caring
17 ;;; about the upper 32 bits: often the REX prefix can be spared.
18 (defun move-qword-to-eax (value)
19   (if (and (sc-is value any-reg descriptor-reg)
20            (< (tn-offset value) r8-offset))
21       (move eax-tn (make-dword-tn value))
22       (move rax-tn value)))
23
24 (defun generate-fixnum-test (value)
25   "zero flag set if VALUE is fixnum"
26   (inst test
27         (cond ((sc-is value any-reg descriptor-reg)
28                (make-byte-tn value))
29               ((sc-is value control-stack)
30                (make-ea :byte :base rbp-tn
31                         :disp (frame-byte-offset (tn-offset value))))
32               (t
33                value))
34         sb!vm::fixnum-tag-mask))
35
36 (defun %test-fixnum (value target not-p)
37   (generate-fixnum-test value)
38   (inst jmp (if not-p :nz :z) target))
39
40 (defun %test-fixnum-and-headers (value target not-p headers)
41   (let ((drop-through (gen-label)))
42     (generate-fixnum-test value)
43     (inst jmp :z (if not-p drop-through target))
44     (%test-headers value target not-p nil headers drop-through)))
45
46 (defun %test-fixnum-and-immediate (value target not-p immediate)
47   (let ((drop-through (gen-label)))
48     (generate-fixnum-test value)
49     (inst jmp :z (if not-p drop-through target))
50     (%test-immediate value target not-p immediate drop-through)))
51
52 (defun %test-fixnum-immediate-and-headers (value target not-p immediate
53                                            headers)
54   (let ((drop-through (gen-label)))
55     (generate-fixnum-test value)
56     (inst jmp :z (if not-p drop-through target))
57     (%test-immediate-and-headers value target not-p immediate headers
58                                  drop-through)))
59
60 (defun %test-immediate (value target not-p immediate
61                         &optional (drop-through (gen-label)))
62   ;; Code a single instruction byte test if possible.
63   (cond ((sc-is value any-reg descriptor-reg)
64          (inst cmp (make-byte-tn value) immediate))
65         (t
66          (move rax-tn value)
67          (inst cmp al-tn immediate)))
68   (inst jmp (if not-p :ne :e) target)
69   (emit-label drop-through))
70
71 (defun %test-immediate-and-headers (value target not-p immediate headers
72                                     &optional (drop-through (gen-label)))
73   ;; Code a single instruction byte test if possible.
74   (cond ((sc-is value any-reg descriptor-reg)
75          (inst cmp (make-byte-tn value) immediate))
76         (t
77          (move rax-tn value)
78          (inst cmp al-tn immediate)))
79   (inst jmp :e (if not-p drop-through target))
80   (%test-headers value target not-p nil headers drop-through))
81
82 (defun %test-lowtag (value target not-p lowtag)
83   (move-qword-to-eax value)
84   (inst and al-tn lowtag-mask)
85   (inst cmp al-tn lowtag)
86   (inst jmp (if not-p :ne :e) target))
87
88 (defun %test-headers (value target not-p function-p headers
89                             &optional (drop-through (gen-label)))
90   (let ((lowtag (if function-p fun-pointer-lowtag other-pointer-lowtag)))
91     (multiple-value-bind (equal less-or-equal greater-or-equal when-true
92                                 when-false)
93         ;; EQUAL, LESS-OR-EQUAL, and GREATER-OR-EQUAL are the conditions
94         ;; for branching to TARGET.  WHEN-TRUE and WHEN-FALSE are the
95         ;; labels to branch to when we know it's true and when we know
96         ;; it's false respectively.
97         (if not-p
98             (values :ne :a :b drop-through target)
99             (values :e :na :nb target drop-through))
100       (%test-lowtag value when-false t lowtag)
101       (do ((remaining headers (cdr remaining))
102            ;; It is preferable (smaller and faster code) to directly
103            ;; compare the value in memory instead of loading it into
104            ;; a register first. Find out if this is possible and set
105            ;; WIDETAG-TN accordingly. If impossible, generate the
106            ;; register load.
107            ;; Compared to x86 we additionally optimize the cases of a
108            ;; range starting with BIGNUM-WIDETAG or ending with
109            ;; COMPLEX-ARRAY-WIDETAG.
110            (widetag-tn (if (and (null (cdr headers))
111                                 (or (atom (car headers))
112                                     (= (caar headers) bignum-widetag)
113                                     (= (cdar headers) complex-array-widetag)))
114                            (make-ea :byte :base value :disp (- lowtag))
115                            (progn
116                              (inst mov eax-tn (make-ea :dword :base value
117                                                        :disp (- lowtag)))
118                              al-tn))))
119           ((null remaining))
120         (let ((header (car remaining))
121               (last (null (cdr remaining))))
122           (cond
123            ((atom header)
124             (inst cmp widetag-tn header)
125             (if last
126                 (inst jmp equal target)
127                 (inst jmp :e when-true)))
128            (t
129              (let ((start (car header))
130                    (end (cdr header)))
131                (cond
132                  ((= start bignum-widetag)
133                   (inst cmp widetag-tn end)
134                   (if last
135                       (inst jmp less-or-equal target)
136                       (inst jmp :be when-true)))
137                  ((= end complex-array-widetag)
138                   (inst cmp widetag-tn start)
139                   (if last
140                       (inst jmp greater-or-equal target)
141                       (inst jmp :b when-false)))
142                  ((not last)
143                   (inst cmp al-tn start)
144                   (inst jmp :b when-false)
145                   (inst cmp al-tn end)
146                   (inst jmp :be when-true))
147                  (t
148                   (inst sub al-tn start)
149                   (inst cmp al-tn (- end start))
150                   (inst jmp less-or-equal target))))))))
151       (emit-label drop-through))))
152
153 \f
154 ;;;; type checking and testing
155
156 (define-vop (check-type)
157   (:args (value :target result :scs (any-reg descriptor-reg)))
158   (:results (result :scs (any-reg descriptor-reg)))
159   (:temporary (:sc unsigned-reg :offset eax-offset :to (:result 0)) eax)
160   (:ignore eax)
161   (:vop-var vop)
162   (:save-p :compute-only))
163
164 (define-vop (type-predicate)
165   (:args (value :scs (any-reg descriptor-reg)))
166   (:temporary (:sc unsigned-reg :offset eax-offset) eax)
167   (:ignore eax)
168   (:conditional)
169   (:info target not-p)
170   (:policy :fast-safe))
171
172 ;;; simpler VOP that don't need a temporary register
173 (define-vop (simple-check-type)
174   (:args (value :target result :scs (any-reg descriptor-reg)))
175   (:results (result :scs (any-reg descriptor-reg)
176                     :load-if (not (and (sc-is value any-reg descriptor-reg)
177                                        (sc-is result control-stack)))))
178   (:vop-var vop)
179   (:save-p :compute-only))
180
181 (define-vop (simple-type-predicate)
182   (:args (value :scs (any-reg descriptor-reg control-stack)))
183   (:conditional)
184   (:info target not-p)
185   (:policy :fast-safe))
186
187 (defmacro !define-type-vops (pred-name check-name ptype error-code
188                              (&rest type-codes)
189                              &key (variant nil variant-p) &allow-other-keys)
190   ;; KLUDGE: UGH. Why do we need this eval? Can't we put this in the
191   ;; expansion?
192   (flet ((cost-to-test-types (type-codes)
193            (+ (* 2 (length type-codes))
194               (if (> (apply #'max type-codes) lowtag-limit) 7 2))))
195     (let* ((cost (cost-to-test-types (mapcar #'eval type-codes)))
196            (prefix (if variant-p
197                        (concatenate 'string (string variant) "-")
198                        "")))
199       `(progn
200          ,@(when pred-name
201              `((define-vop (,pred-name ,(intern (concatenate 'string prefix "TYPE-PREDICATE")))
202                  (:translate ,pred-name)
203                  (:generator ,cost
204                    (test-type value target not-p (,@type-codes))))))
205          ,@(when check-name
206              `((define-vop (,check-name ,(intern (concatenate 'string prefix "CHECK-TYPE")))
207                  (:generator ,cost
208                    (let ((err-lab
209                            (generate-error-code vop ',error-code value)))
210                      (test-type value err-lab t (,@type-codes))
211                      (move result value))))))
212          ,@(when ptype
213              `((primitive-type-vop ,check-name (:check) ,ptype)))))))
214 \f
215 ;;;; other integer ranges
216
217 (define-vop (fixnump/unsigned-byte-64 simple-type-predicate)
218   (:args (value :scs (unsigned-reg)))
219   (:arg-types unsigned-num)
220   (:translate fixnump)
221   (:temporary (:sc unsigned-reg :from (:argument 0)) tmp)
222   (:info)
223   (:conditional :z)
224   (:generator 5
225     (move tmp value)
226     (inst shr tmp n-positive-fixnum-bits)))
227
228 #-#.(cl:if (cl:= sb!vm:n-fixnum-tag-bits 1) '(:and) '(:or))
229 (define-vop (fixnump/signed-byte-64 simple-type-predicate)
230   (:args (value :scs (signed-reg)))
231   (:info)
232   (:conditional :z)
233   (:temporary (:sc unsigned-reg :offset eax-offset) eax)
234   (:arg-types signed-num)
235   (:translate fixnump)
236   (:generator 5
237     ;; Hackers Delight, p. 53: signed
238     ;;    a <= x <= a + 2^n - 1
239     ;; is equivalent to unsigned
240     ;;    ((x-a) >> n) = 0
241     (inst mov rax-tn #.(- sb!xc:most-negative-fixnum))
242     (inst add rax-tn value)
243     (inst shr rax-tn n-fixnum-bits)))
244
245 #+#.(cl:if (cl:= sb!vm:n-fixnum-tag-bits 1) '(:and) '(:or))
246 (define-vop (fixnump/signed-byte-64 simple-type-predicate)
247   (:args (value :scs (signed-reg) :target temp))
248   (:info)
249   (:conditional :no)
250   (:temporary (:sc unsigned-reg :from (:argument 0)) temp)
251   (:arg-types signed-num)
252   (:translate fixnump)
253   (:generator 5
254     (move temp value)
255     ;; The overflow flag will be set if the reg's sign bit changes.
256     (inst shl temp 1)))
257
258 ;;; A (SIGNED-BYTE 64) can be represented with either fixnum or a bignum with
259 ;;; exactly one digit.
260
261 (define-vop (signed-byte-64-p type-predicate)
262   (:translate signed-byte-64-p)
263   (:generator 45
264     (multiple-value-bind (yep nope)
265         (if not-p
266             (values not-target target)
267             (values target not-target))
268       (generate-fixnum-test value)
269       (inst jmp :e yep)
270       (move-qword-to-eax value)
271       (inst and al-tn lowtag-mask)
272       (inst cmp al-tn other-pointer-lowtag)
273       (inst jmp :ne nope)
274       (inst cmp (make-ea-for-object-slot value 0 other-pointer-lowtag)
275             (+ (ash 1 n-widetag-bits) bignum-widetag))
276       (inst jmp (if not-p :ne :e) target))
277     NOT-TARGET))
278
279 (define-vop (check-signed-byte-64 check-type)
280   (:generator 45
281     (let ((nope (generate-error-code vop
282                                      'object-not-signed-byte-64-error
283                                      value)))
284       (generate-fixnum-test value)
285       (inst jmp :e yep)
286       (move-qword-to-eax value)
287       (inst and al-tn lowtag-mask)
288       (inst cmp al-tn other-pointer-lowtag)
289       (inst jmp :ne nope)
290       (inst cmp (make-ea-for-object-slot value 0 other-pointer-lowtag)
291             (+ (ash 1 n-widetag-bits) bignum-widetag))
292       (inst jmp :ne nope))
293     YEP
294     (move result value)))
295
296 ;;; An (unsigned-byte 64) can be represented with either a positive
297 ;;; fixnum, a bignum with exactly one positive digit, or a bignum with
298 ;;; exactly two digits and the second digit all zeros.
299 (define-vop (unsigned-byte-64-p type-predicate)
300   (:translate unsigned-byte-64-p)
301   (:generator 45
302     (let ((not-target (gen-label))
303           (single-word (gen-label))
304           (fixnum (gen-label)))
305       (multiple-value-bind (yep nope)
306           (if not-p
307               (values not-target target)
308               (values target not-target))
309         ;; Is it a fixnum?
310         (generate-fixnum-test value)
311         (move rax-tn value)
312         (inst jmp :e fixnum)
313
314         ;; If not, is it an other pointer?
315         (inst and al-tn lowtag-mask)
316         (inst cmp al-tn other-pointer-lowtag)
317         (inst jmp :ne nope)
318         ;; Get the header.
319         (loadw rax-tn value 0 other-pointer-lowtag)
320         ;; Is it one?
321         (inst cmp rax-tn (+ (ash 1 n-widetag-bits) bignum-widetag))
322         (inst jmp :e single-word)
323         ;; If it's other than two, we can't be an (unsigned-byte 64)
324         (inst cmp rax-tn (+ (ash 2 n-widetag-bits) bignum-widetag))
325         (inst jmp :ne nope)
326         ;; Get the second digit.
327         (loadw rax-tn value (1+ bignum-digits-offset) other-pointer-lowtag)
328         ;; All zeros, its an (unsigned-byte 64).
329         (inst test rax-tn rax-tn)
330         (inst jmp :z yep)
331         (inst jmp nope)
332
333         (emit-label single-word)
334         ;; Get the single digit.
335         (loadw rax-tn value bignum-digits-offset other-pointer-lowtag)
336
337         ;; positive implies (unsigned-byte 64).
338         (emit-label fixnum)
339         (inst test rax-tn rax-tn)
340         (inst jmp (if not-p :s :ns) target)
341
342         (emit-label not-target)))))
343
344 (define-vop (check-unsigned-byte-64 check-type)
345   (:generator 45
346     (let ((nope
347            (generate-error-code vop 'object-not-unsigned-byte-64-error value))
348           (yep (gen-label))
349           (fixnum (gen-label))
350           (single-word (gen-label)))
351
352       ;; Is it a fixnum?
353       (generate-fixnum-test value)
354       (move rax-tn value)
355       (inst jmp :e fixnum)
356
357       ;; If not, is it an other pointer?
358       (inst and al-tn lowtag-mask)
359       (inst cmp al-tn other-pointer-lowtag)
360       (inst jmp :ne nope)
361       ;; Get the header.
362       (loadw rax-tn value 0 other-pointer-lowtag)
363       ;; Is it one?
364       (inst cmp rax-tn (+ (ash 1 n-widetag-bits) bignum-widetag))
365       (inst jmp :e single-word)
366       ;; If it's other than two, we can't be an (unsigned-byte 64)
367       (inst cmp rax-tn (+ (ash 2 n-widetag-bits) bignum-widetag))
368       (inst jmp :ne nope)
369       ;; Get the second digit.
370       (loadw rax-tn value (1+ bignum-digits-offset) other-pointer-lowtag)
371       ;; All zeros, its an (unsigned-byte 64).
372       (inst test rax-tn rax-tn)
373       (inst jmp :z yep)
374       (inst jmp nope)
375
376       (emit-label single-word)
377       ;; Get the single digit.
378       (loadw rax-tn value bignum-digits-offset other-pointer-lowtag)
379
380       ;; positive implies (unsigned-byte 64).
381       (emit-label fixnum)
382       (inst test rax-tn rax-tn)
383       (inst jmp :s nope)
384
385       (emit-label yep)
386       (move result value))))
387 \f
388 ;;;; list/symbol types
389 ;;;
390 ;;; symbolp (or symbol (eq nil))
391 ;;; consp (and list (not (eq nil)))
392
393 (define-vop (symbolp type-predicate)
394   (:translate symbolp)
395   (:generator 12
396     (let ((is-symbol-label (if not-p DROP-THRU target)))
397       (inst cmp value nil-value)
398       (inst jmp :e is-symbol-label)
399       (test-type value target not-p (symbol-header-widetag)))
400     DROP-THRU))
401
402 (define-vop (check-symbol check-type)
403   (:generator 12
404     (let ((error (generate-error-code vop 'object-not-symbol-error value)))
405       (inst cmp value nil-value)
406       (inst jmp :e DROP-THRU)
407       (test-type value error t (symbol-header-widetag)))
408     DROP-THRU
409     (move result value)))
410
411 (define-vop (consp type-predicate)
412   (:translate consp)
413   (:generator 8
414     (let ((is-not-cons-label (if not-p target DROP-THRU)))
415       (inst cmp value nil-value)
416       (inst jmp :e is-not-cons-label)
417       (test-type value target not-p (list-pointer-lowtag)))
418     DROP-THRU))
419
420 (define-vop (check-cons check-type)
421   (:generator 8
422     (let ((error (generate-error-code vop 'object-not-cons-error value)))
423       (inst cmp value nil-value)
424       (inst jmp :e error)
425       (test-type value error t (list-pointer-lowtag))
426       (move result value))))
427
428 #!+sb-simd-pack
429 (progn
430   (!define-type-vops simd-pack-p nil nil nil (simd-pack-widetag))
431
432   #!+x86-64
433   (define-vop (check-simd-pack check-type)
434     (:args (value :target result
435                   :scs (any-reg descriptor-reg
436                         int-sse-reg single-sse-reg double-sse-reg
437                         int-sse-stack single-sse-stack double-sse-stack)))
438     (:results (result :scs (any-reg descriptor-reg
439                            int-sse-reg single-sse-reg double-sse-reg)))
440     (:temporary (:sc unsigned-reg :offset eax-offset :to (:result 0)) eax)
441     (:ignore eax)
442     (:vop-var vop)
443     (:node-var node)
444     (:save-p :compute-only)
445     (:generator 50
446       (sc-case value
447         ((int-sse-reg single-sse-reg double-sse-reg
448           int-sse-stack single-sse-stack double-sse-stack)
449          (sc-case result
450            ((int-sse-reg single-sse-reg double-sse-reg)
451             (move result value))
452            ((any-reg descriptor-reg)
453             (with-fixed-allocation (result
454                                     simd-pack-widetag
455                                     simd-pack-size
456                                     node)
457               ;; see *simd-pack-element-types*
458               (storew (fixnumize
459                        (sc-case value
460                          ((int-sse-reg int-sse-stack) 0)
461                          ((single-sse-reg single-sse-stack) 1)
462                          ((double-sse-reg double-sse-stack) 2)))
463                   result simd-pack-tag-slot other-pointer-lowtag)
464               (let ((ea (make-ea-for-object-slot
465                          result simd-pack-lo-value-slot other-pointer-lowtag)))
466                 (if (float-simd-pack-p value)
467                     (inst movaps ea value)
468                     (inst movdqa ea value)))))))
469         ((any-reg descriptor-reg)
470          (let ((leaf (sb!c::tn-leaf value)))
471            (unless (and (sb!c::lvar-p leaf)
472                         (csubtypep (sb!c::lvar-type leaf)
473                                    (specifier-type 'simd-pack)))
474              (test-type
475                  value
476                  (generate-error-code vop 'object-not-simd-pack-error value)
477                  t (simd-pack-widetag))))
478          (sc-case result
479            ((int-sse-reg)
480             (let ((ea (make-ea-for-object-slot
481                        value simd-pack-lo-value-slot other-pointer-lowtag)))
482               (inst movdqa result ea)))
483            ((single-sse-reg double-sse-reg)
484             (let ((ea (make-ea-for-object-slot
485                        value simd-pack-lo-value-slot other-pointer-lowtag)))
486               (inst movaps result ea)))
487            ((any-reg descriptor-reg)
488             (move result value)))))))
489
490   (primitive-type-vop check-simd-pack (:check) simd-pack-int simd-pack-single simd-pack-double))