Microoptimize (signed-byte 64) type test on x86-64.
[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   (inst lea eax-tn (make-ea :dword :base value :disp (- lowtag)))
84   (inst test al-tn lowtag-mask)
85   (inst jmp (if not-p :nz :z) target))
86
87 (defun %test-headers (value target not-p function-p headers
88                             &optional (drop-through (gen-label)))
89   (let ((lowtag (if function-p fun-pointer-lowtag other-pointer-lowtag)))
90     (multiple-value-bind (equal less-or-equal greater-or-equal when-true
91                                 when-false)
92         ;; EQUAL, LESS-OR-EQUAL, and GREATER-OR-EQUAL are the conditions
93         ;; for branching to TARGET.  WHEN-TRUE and WHEN-FALSE are the
94         ;; labels to branch to when we know it's true and when we know
95         ;; it's false respectively.
96         (if not-p
97             (values :ne :a :b drop-through target)
98             (values :e :na :nb target drop-through))
99       (%test-lowtag value when-false t lowtag)
100       (do ((remaining headers (cdr remaining))
101            ;; It is preferable (smaller and faster code) to directly
102            ;; compare the value in memory instead of loading it into
103            ;; a register first. Find out if this is possible and set
104            ;; WIDETAG-TN accordingly. If impossible, generate the
105            ;; register load.
106            ;; Compared to x86 we additionally optimize the cases of a
107            ;; range starting with BIGNUM-WIDETAG or ending with
108            ;; COMPLEX-ARRAY-WIDETAG.
109            (widetag-tn (if (and (null (cdr headers))
110                                 (or (atom (car headers))
111                                     (= (caar headers) bignum-widetag)
112                                     (= (cdar headers) complex-array-widetag)))
113                            (make-ea :byte :base value :disp (- lowtag))
114                            (progn
115                              (inst mov eax-tn (make-ea :dword :base value
116                                                        :disp (- lowtag)))
117                              al-tn))))
118           ((null remaining))
119         (let ((header (car remaining))
120               (last (null (cdr remaining))))
121           (cond
122            ((atom header)
123             (inst cmp widetag-tn header)
124             (if last
125                 (inst jmp equal target)
126                 (inst jmp :e when-true)))
127            (t
128              (let ((start (car header))
129                    (end (cdr header)))
130                (cond
131                  ((= start bignum-widetag)
132                   (inst cmp widetag-tn end)
133                   (if last
134                       (inst jmp less-or-equal target)
135                       (inst jmp :be when-true)))
136                  ((= end complex-array-widetag)
137                   (inst cmp widetag-tn start)
138                   (if last
139                       (inst jmp greater-or-equal target)
140                       (inst jmp :b when-false)))
141                  ((not last)
142                   (inst cmp al-tn start)
143                   (inst jmp :b when-false)
144                   (inst cmp al-tn end)
145                   (inst jmp :be when-true))
146                  (t
147                   (inst sub al-tn start)
148                   (inst cmp al-tn (- end start))
149                   (inst jmp less-or-equal target))))))))
150       (emit-label drop-through))))
151
152 \f
153 ;;;; type checking and testing
154
155 (define-vop (check-type)
156   (:args (value :target result :scs (any-reg descriptor-reg)))
157   (:results (result :scs (any-reg descriptor-reg)))
158   (:temporary (:sc unsigned-reg :offset eax-offset :to (:result 0)) eax)
159   (:ignore eax)
160   (:vop-var vop)
161   (:save-p :compute-only))
162
163 (define-vop (type-predicate)
164   (:args (value :scs (any-reg descriptor-reg)))
165   (:temporary (:sc unsigned-reg :offset eax-offset) eax)
166   (:ignore eax)
167   (:conditional)
168   (:info target not-p)
169   (:policy :fast-safe))
170
171 ;;; simpler VOP that don't need a temporary register
172 (define-vop (simple-check-type)
173   (:args (value :target result :scs (any-reg descriptor-reg)))
174   (:results (result :scs (any-reg descriptor-reg)
175                     :load-if (not (and (sc-is value any-reg descriptor-reg)
176                                        (sc-is result control-stack)))))
177   (:vop-var vop)
178   (:save-p :compute-only))
179
180 (define-vop (simple-type-predicate)
181   (:args (value :scs (any-reg descriptor-reg control-stack)))
182   (:conditional)
183   (:info target not-p)
184   (:policy :fast-safe))
185
186 (defmacro !define-type-vops (pred-name check-name ptype error-code
187                              (&rest type-codes)
188                              &key (variant nil variant-p) &allow-other-keys)
189   ;; KLUDGE: UGH. Why do we need this eval? Can't we put this in the
190   ;; expansion?
191   (flet ((cost-to-test-types (type-codes)
192            (+ (* 2 (length type-codes))
193               (if (> (apply #'max type-codes) lowtag-limit) 7 2))))
194     (let* ((cost (cost-to-test-types (mapcar #'eval type-codes)))
195            (prefix (if variant-p
196                        (concatenate 'string (string variant) "-")
197                        "")))
198       `(progn
199          ,@(when pred-name
200              `((define-vop (,pred-name ,(intern (concatenate 'string prefix "TYPE-PREDICATE")))
201                  (:translate ,pred-name)
202                  (:generator ,cost
203                    (test-type value target not-p (,@type-codes))))))
204          ,@(when check-name
205              `((define-vop (,check-name ,(intern (concatenate 'string prefix "CHECK-TYPE")))
206                  (:generator ,cost
207                    (let ((err-lab
208                            (generate-error-code vop ',error-code value)))
209                      (test-type value err-lab t (,@type-codes))
210                      (move result value))))))
211          ,@(when ptype
212              `((primitive-type-vop ,check-name (:check) ,ptype)))))))
213 \f
214 ;;;; other integer ranges
215
216 (define-vop (fixnump/unsigned-byte-64 simple-type-predicate)
217   (:args (value :scs (unsigned-reg)))
218   (:arg-types unsigned-num)
219   (:translate fixnump)
220   (:temporary (:sc unsigned-reg :from (:argument 0)) tmp)
221   (:info)
222   (:conditional :z)
223   (:generator 5
224     (move tmp value)
225     (inst shr tmp n-positive-fixnum-bits)))
226
227 #-#.(cl:if (cl:= sb!vm:n-fixnum-tag-bits 1) '(:and) '(:or))
228 (define-vop (fixnump/signed-byte-64 simple-type-predicate)
229   (:args (value :scs (signed-reg)))
230   (:info)
231   (:conditional :z)
232   (:temporary (:sc unsigned-reg :offset eax-offset) eax)
233   (:arg-types signed-num)
234   (:translate fixnump)
235   (:generator 5
236     ;; Hackers Delight, p. 53: signed
237     ;;    a <= x <= a + 2^n - 1
238     ;; is equivalent to unsigned
239     ;;    ((x-a) >> n) = 0
240     (inst mov rax-tn #.(- sb!xc:most-negative-fixnum))
241     (inst add rax-tn value)
242     (inst shr rax-tn n-fixnum-bits)))
243
244 #+#.(cl:if (cl:= sb!vm:n-fixnum-tag-bits 1) '(:and) '(:or))
245 (define-vop (fixnump/signed-byte-64 simple-type-predicate)
246   (:args (value :scs (signed-reg) :target temp))
247   (:info)
248   (:conditional :no)
249   (:temporary (:sc unsigned-reg :from (:argument 0)) temp)
250   (:arg-types signed-num)
251   (:translate fixnump)
252   (:generator 5
253     (move temp value)
254     ;; The overflow flag will be set if the reg's sign bit changes.
255     (inst shl temp 1)))
256
257 ;;; A (SIGNED-BYTE 64) can be represented with either fixnum or a bignum with
258 ;;; exactly one digit.
259
260 (define-vop (signed-byte-64-p type-predicate)
261   (:translate signed-byte-64-p)
262   (:generator 45
263     (multiple-value-bind (yep nope)
264         (if not-p
265             (values not-target target)
266             (values target not-target))
267       (move-qword-to-eax value)
268       (inst test al-tn fixnum-tag-mask)
269       (inst jmp :e yep)
270
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         (move rax-tn value)
311         (inst test al-tn fixnum-tag-mask)
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
388 (defun power-of-two-limit-p (x)
389   (and (fixnump x)
390        (= (logcount (1+ x)) 1)))
391
392 (define-vop (test-fixnum-mod-power-of-two)
393   (:args (value :scs (any-reg descriptor-reg
394                               unsigned-reg signed-reg
395                               immediate)))
396   (:arg-types *
397               (:constant (satisfies power-of-two-limit-p)))
398   (:translate fixnum-mod-p)
399   (:conditional :e)
400   (:info hi)
401   (:save-p :compute-only)
402   (:policy :fast-safe)
403   (:generator 4
404      (aver (not (sc-is value immediate)))
405      (let* ((fixnum-hi (if (sc-is value unsigned-reg signed-reg)
406                            hi
407                            (fixnumize hi))))
408        (inst test value (constantize (lognot fixnum-hi))))))
409
410 (define-vop (test-fixnum-mod-tagged-unsigned)
411   (:args (value :scs (any-reg descriptor-reg
412                               unsigned-reg signed-reg
413                               immediate)))
414   (:arg-types (:or tagged-num unsigned-num signed-num)
415               (:constant fixnum))
416   (:translate fixnum-mod-p)
417   (:conditional :be)
418   (:info hi)
419   (:save-p :compute-only)
420   (:policy :fast-safe)
421   (:generator 5
422      (aver (not (sc-is value immediate)))
423      (let ((fixnum-hi (if (sc-is value unsigned-reg signed-reg)
424                           hi
425                           (fixnumize hi))))
426        (inst cmp value (constantize fixnum-hi)))))
427
428 (define-vop (test-fixnum-mod-*)
429   (:args (value :scs (any-reg descriptor-reg)))
430   (:arg-types * (:constant fixnum))
431   (:translate fixnum-mod-p)
432   (:conditional)
433   (:info target not-p hi)
434   (:save-p :compute-only)
435   (:policy :fast-safe)
436   (:generator 6
437      (let* ((fixnum-hi (fixnumize hi))
438             (skip (gen-label)))
439        (generate-fixnum-test value)
440        (inst jmp :ne (if not-p target skip))
441        (inst cmp value (constantize fixnum-hi))
442        (inst jmp (if not-p :a :be) target)
443        (emit-label skip))))
444 \f
445 ;;;; list/symbol types
446 ;;;
447 ;;; symbolp (or symbol (eq nil))
448 ;;; consp (and list (not (eq nil)))
449
450 (define-vop (symbolp type-predicate)
451   (:translate symbolp)
452   (:generator 12
453     (let ((is-symbol-label (if not-p DROP-THRU target)))
454       (inst cmp value nil-value)
455       (inst jmp :e is-symbol-label)
456       (test-type value target not-p (symbol-header-widetag)))
457     DROP-THRU))
458
459 (define-vop (check-symbol check-type)
460   (:generator 12
461     (let ((error (generate-error-code vop 'object-not-symbol-error value)))
462       (inst cmp value nil-value)
463       (inst jmp :e DROP-THRU)
464       (test-type value error t (symbol-header-widetag)))
465     DROP-THRU
466     (move result value)))
467
468 (define-vop (consp type-predicate)
469   (:translate consp)
470   (:generator 8
471     (let ((is-not-cons-label (if not-p target DROP-THRU)))
472       (inst cmp value nil-value)
473       (inst jmp :e is-not-cons-label)
474       (test-type value target not-p (list-pointer-lowtag)))
475     DROP-THRU))
476
477 (define-vop (check-cons check-type)
478   (:generator 8
479     (let ((error (generate-error-code vop 'object-not-cons-error value)))
480       (inst cmp value nil-value)
481       (inst jmp :e error)
482       (test-type value error t (list-pointer-lowtag))
483       (move result value))))
484
485 #!+sb-simd-pack
486 (progn
487   (!define-type-vops simd-pack-p nil nil nil (simd-pack-widetag))
488
489   (define-vop (check-simd-pack check-type)
490     (:args (value :target result
491                   :scs (any-reg descriptor-reg
492                         int-sse-reg single-sse-reg double-sse-reg
493                         int-sse-stack single-sse-stack double-sse-stack)))
494     (:results (result :scs (any-reg descriptor-reg
495                            int-sse-reg single-sse-reg double-sse-reg)))
496     (:temporary (:sc unsigned-reg :offset eax-offset :to (:result 0)) eax)
497     (:ignore eax)
498     (:vop-var vop)
499     (:node-var node)
500     (:save-p :compute-only)
501     (:generator 50
502       (sc-case value
503         ((int-sse-reg single-sse-reg double-sse-reg
504           int-sse-stack single-sse-stack double-sse-stack)
505          (sc-case result
506            ((int-sse-reg single-sse-reg double-sse-reg)
507             (move result value))
508            ((any-reg descriptor-reg)
509             (with-fixed-allocation (result
510                                     simd-pack-widetag
511                                     simd-pack-size
512                                     node)
513               ;; see *simd-pack-element-types*
514               (storew (fixnumize
515                        (sc-case value
516                          ((int-sse-reg int-sse-stack) 0)
517                          ((single-sse-reg single-sse-stack) 1)
518                          ((double-sse-reg double-sse-stack) 2)))
519                   result simd-pack-tag-slot other-pointer-lowtag)
520               (let ((ea (make-ea-for-object-slot
521                          result simd-pack-lo-value-slot other-pointer-lowtag)))
522                 (if (float-simd-pack-p value)
523                     (inst movaps ea value)
524                     (inst movdqa ea value)))))))
525         ((any-reg descriptor-reg)
526          (let ((leaf (sb!c::tn-leaf value)))
527            (unless (and (sb!c::lvar-p leaf)
528                         (csubtypep (sb!c::lvar-type leaf)
529                                    (specifier-type 'simd-pack)))
530              (test-type
531                  value
532                  (generate-error-code vop 'object-not-simd-pack-error value)
533                  t (simd-pack-widetag))))
534          (sc-case result
535            ((int-sse-reg)
536             (let ((ea (make-ea-for-object-slot
537                        value simd-pack-lo-value-slot other-pointer-lowtag)))
538               (inst movdqa result ea)))
539            ((single-sse-reg double-sse-reg)
540             (let ((ea (make-ea-for-object-slot
541                        value simd-pack-lo-value-slot other-pointer-lowtag)))
542               (inst movaps result ea)))
543            ((any-reg descriptor-reg)
544             (move result value)))))))
545
546   (primitive-type-vop check-simd-pack (:check) simd-pack-int simd-pack-single simd-pack-double))