Micro-optimize type tests using widetag 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 (defun generate-fixnum-test (value)
17   "zero flag set if VALUE is fixnum"
18   (inst test
19         (cond ((sc-is value any-reg descriptor-reg)
20                (make-byte-tn value))
21               ((sc-is value control-stack)
22                (make-ea :byte :base rbp-tn
23                         :disp (frame-byte-offset (tn-offset value))))
24               (t
25                value))
26         sb!vm::fixnum-tag-mask))
27
28 (defun %test-fixnum (value target not-p)
29   (generate-fixnum-test value)
30   (inst jmp (if not-p :nz :z) target))
31
32 (defun %test-fixnum-and-headers (value target not-p headers)
33   (let ((drop-through (gen-label)))
34     (generate-fixnum-test value)
35     (inst jmp :z (if not-p drop-through target))
36     (%test-headers value target not-p nil headers drop-through)))
37
38 (defun %test-fixnum-and-immediate (value target not-p immediate)
39   (let ((drop-through (gen-label)))
40     (generate-fixnum-test value)
41     (inst jmp :z (if not-p drop-through target))
42     (%test-immediate value target not-p immediate drop-through)))
43
44 (defun %test-fixnum-immediate-and-headers (value target not-p immediate
45                                            headers)
46   (let ((drop-through (gen-label)))
47     (generate-fixnum-test value)
48     (inst jmp :z (if not-p drop-through target))
49     (%test-immediate-and-headers value target not-p immediate headers
50                                  drop-through)))
51
52 (defun %test-immediate (value target not-p immediate
53                         &optional (drop-through (gen-label)))
54   ;; Code a single instruction byte test if possible.
55   (cond ((sc-is value any-reg descriptor-reg)
56          (inst cmp (make-byte-tn value) immediate))
57         (t
58          (move rax-tn value)
59          (inst cmp al-tn immediate)))
60   (inst jmp (if not-p :ne :e) target)
61   (emit-label drop-through))
62
63 (defun %test-immediate-and-headers (value target not-p immediate headers
64                                     &optional (drop-through (gen-label)))
65   ;; Code a single instruction byte test if possible.
66   (cond ((sc-is value any-reg descriptor-reg)
67          (inst cmp (make-byte-tn value) immediate))
68         (t
69          (move rax-tn value)
70          (inst cmp al-tn immediate)))
71   (inst jmp :e (if not-p drop-through target))
72   (%test-headers value target not-p nil headers drop-through))
73
74 (defun %test-lowtag (value target not-p lowtag)
75   (if (and (sc-is value any-reg descriptor-reg)
76            (< (tn-offset value) r8-offset))
77       (move eax-tn (make-dword-tn value)) ; shorter encoding (no REX prefix)
78       (move rax-tn value))
79   (inst and al-tn lowtag-mask)
80   (inst cmp al-tn lowtag)
81   (inst jmp (if not-p :ne :e) target))
82
83 (defun %test-headers (value target not-p function-p headers
84                             &optional (drop-through (gen-label)))
85   (let ((lowtag (if function-p fun-pointer-lowtag other-pointer-lowtag)))
86     (multiple-value-bind (equal less-or-equal greater-or-equal when-true
87                                 when-false)
88         ;; EQUAL, LESS-OR-EQUAL, and GREATER-OR-EQUAL are the conditions
89         ;; for branching to TARGET.  WHEN-TRUE and WHEN-FALSE are the
90         ;; labels to branch to when we know it's true and when we know
91         ;; it's false respectively.
92         (if not-p
93             (values :ne :a :b drop-through target)
94             (values :e :na :nb target drop-through))
95       (%test-lowtag value when-false t lowtag)
96       (do ((remaining headers (cdr remaining))
97            ;; It is preferable (smaller and faster code) to directly
98            ;; compare the value in memory instead of loading it into
99            ;; a register first. Find out if this is possible and set
100            ;; WIDETAG-TN accordingly. If impossible, generate the
101            ;; register load.
102            ;; Compared to x86 we additionally optimize the cases of a
103            ;; range starting with BIGNUM-WIDETAG or ending with
104            ;; COMPLEX-ARRAY-WIDETAG.
105            (widetag-tn (if (and (null (cdr headers))
106                                 (or (atom (car headers))
107                                     (= (caar headers) bignum-widetag)
108                                     (= (cdar headers) complex-array-widetag)))
109                            (make-ea :byte :base value :disp (- lowtag))
110                            (progn
111                              (inst mov eax-tn (make-ea :dword :base value
112                                                        :disp (- lowtag)))
113                              al-tn))))
114           ((null remaining))
115         (let ((header (car remaining))
116               (last (null (cdr remaining))))
117           (cond
118            ((atom header)
119             (inst cmp widetag-tn header)
120             (if last
121                 (inst jmp equal target)
122                 (inst jmp :e when-true)))
123            (t
124              (let ((start (car header))
125                    (end (cdr header)))
126                (cond
127                  ((= start bignum-widetag)
128                   (inst cmp widetag-tn end)
129                   (if last
130                       (inst jmp less-or-equal target)
131                       (inst jmp :be when-true)))
132                  ((= end complex-array-widetag)
133                   (inst cmp widetag-tn start)
134                   (if last
135                       (inst jmp greater-or-equal target)
136                       (inst jmp :b when-false)))
137                  ((not last)
138                   (inst cmp al-tn start)
139                   (inst jmp :b when-false)
140                   (inst cmp al-tn end)
141                   (inst jmp :be when-true))
142                  (t
143                   (inst sub al-tn start)
144                   (inst cmp al-tn (- end start))
145                   (inst jmp less-or-equal target))))))))
146       (emit-label drop-through))))
147
148 \f
149 ;;;; type checking and testing
150
151 (define-vop (check-type)
152   (:args (value :target result :scs (any-reg descriptor-reg)))
153   (:results (result :scs (any-reg descriptor-reg)))
154   (:temporary (:sc unsigned-reg :offset eax-offset :to (:result 0)) eax)
155   (:ignore eax)
156   (:vop-var vop)
157   (:save-p :compute-only))
158
159 (define-vop (type-predicate)
160   (:args (value :scs (any-reg descriptor-reg)))
161   (:temporary (:sc unsigned-reg :offset eax-offset) eax)
162   (:ignore eax)
163   (:conditional)
164   (:info target not-p)
165   (:policy :fast-safe))
166
167 ;;; simpler VOP that don't need a temporary register
168 (define-vop (simple-check-type)
169   (:args (value :target result :scs (any-reg descriptor-reg)))
170   (:results (result :scs (any-reg descriptor-reg)
171                     :load-if (not (and (sc-is value any-reg descriptor-reg)
172                                        (sc-is result control-stack)))))
173   (:vop-var vop)
174   (:save-p :compute-only))
175
176 (define-vop (simple-type-predicate)
177   (:args (value :scs (any-reg descriptor-reg control-stack)))
178   (:conditional)
179   (:info target not-p)
180   (:policy :fast-safe))
181
182 (defun cost-to-test-types (type-codes)
183   (+ (* 2 (length type-codes))
184      (if (> (apply #'max type-codes) lowtag-limit) 7 2)))
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   (let* ((cost (cost-to-test-types (mapcar #'eval type-codes)))
192          (prefix (if variant-p
193                      (concatenate 'string (string variant) "-")
194                      "")))
195     `(progn
196        ,@(when pred-name
197            `((define-vop (,pred-name ,(intern (concatenate 'string prefix "TYPE-PREDICATE")))
198                (:translate ,pred-name)
199                (:generator ,cost
200                  (test-type value target not-p (,@type-codes))))))
201        ,@(when check-name
202            `((define-vop (,check-name ,(intern (concatenate 'string prefix "CHECK-TYPE")))
203                (:generator ,cost
204                  (let ((err-lab
205                         (generate-error-code vop ',error-code value)))
206                    (test-type value err-lab t (,@type-codes))
207                    (move result value))))))
208        ,@(when ptype
209            `((primitive-type-vop ,check-name (:check) ,ptype))))))
210 \f
211 ;;;; other integer ranges
212
213 (define-vop (fixnump/unsigned-byte-64 simple-type-predicate)
214   (:args (value :scs (unsigned-reg)))
215   (:arg-types unsigned-num)
216   (:translate fixnump)
217   (:temporary (:sc unsigned-reg) tmp)
218   (:info)
219   (:conditional :z)
220   (:generator 5
221     (inst mov tmp value)
222     (inst shr tmp n-positive-fixnum-bits)))
223
224 (define-vop (fixnump/signed-byte-64 type-predicate)
225   (:args (value :scs (signed-reg)))
226   (:info)
227   (:conditional #.(if (= sb!vm:n-fixnum-tag-bits 1) :ns :z))
228   (:arg-types signed-num)
229   (:translate fixnump)
230   (:generator 5
231     ;; Hackers Delight, p. 53: signed
232     ;;    a <= x <= a + 2^n - 1
233     ;; is equivalent to unsigned
234     ;;    ((x-a) >> n) = 0
235     (inst mov rax-tn #.(- sb!xc:most-negative-fixnum))
236     (inst add rax-tn value)
237     (unless (= n-fixnum-tag-bits 1)
238       (inst shr rax-tn n-fixnum-bits))))
239
240 ;;; A (SIGNED-BYTE 64) can be represented with either fixnum or a bignum with
241 ;;; exactly one digit.
242
243 (define-vop (signed-byte-64-p type-predicate)
244   (:translate signed-byte-64-p)
245   (:generator 45
246     (multiple-value-bind (yep nope)
247         (if not-p
248             (values not-target target)
249             (values target not-target))
250       (generate-fixnum-test value)
251       (inst jmp :e yep)
252       (move rax-tn value)
253       (inst and al-tn lowtag-mask)
254       (inst cmp al-tn other-pointer-lowtag)
255       (inst jmp :ne nope)
256       (inst cmp (make-ea-for-object-slot value 0 other-pointer-lowtag)
257             (+ (ash 1 n-widetag-bits) bignum-widetag))
258       (inst jmp (if not-p :ne :e) target))
259     NOT-TARGET))
260
261 (define-vop (check-signed-byte-64 check-type)
262   (:generator 45
263     (let ((nope (generate-error-code vop
264                                      'object-not-signed-byte-64-error
265                                      value)))
266       (generate-fixnum-test value)
267       (inst jmp :e yep)
268       (move rax-tn value)
269       (inst and al-tn lowtag-mask)
270       (inst cmp al-tn other-pointer-lowtag)
271       (inst jmp :ne nope)
272       (inst cmp (make-ea-for-object-slot value 0 other-pointer-lowtag)
273             (+ (ash 1 n-widetag-bits) bignum-widetag))
274       (inst jmp :ne nope))
275     YEP
276     (move result value)))
277
278 ;;; An (unsigned-byte 64) can be represented with either a positive
279 ;;; fixnum, a bignum with exactly one positive digit, or a bignum with
280 ;;; exactly two digits and the second digit all zeros.
281 (define-vop (unsigned-byte-64-p type-predicate)
282   (:translate unsigned-byte-64-p)
283   (:generator 45
284     (let ((not-target (gen-label))
285           (single-word (gen-label))
286           (fixnum (gen-label)))
287       (multiple-value-bind (yep nope)
288           (if not-p
289               (values not-target target)
290               (values target not-target))
291         ;; Is it a fixnum?
292         (generate-fixnum-test value)
293         (move rax-tn value)
294         (inst jmp :e fixnum)
295
296         ;; If not, is it an other pointer?
297         (inst and rax-tn lowtag-mask)
298         (inst cmp rax-tn other-pointer-lowtag)
299         (inst jmp :ne nope)
300         ;; Get the header.
301         (loadw rax-tn value 0 other-pointer-lowtag)
302         ;; Is it one?
303         (inst cmp rax-tn (+ (ash 1 n-widetag-bits) bignum-widetag))
304         (inst jmp :e single-word)
305         ;; If it's other than two, we can't be an (unsigned-byte 64)
306         (inst cmp rax-tn (+ (ash 2 n-widetag-bits) bignum-widetag))
307         (inst jmp :ne nope)
308         ;; Get the second digit.
309         (loadw rax-tn value (1+ bignum-digits-offset) other-pointer-lowtag)
310         ;; All zeros, its an (unsigned-byte 64).
311         (inst or rax-tn rax-tn)
312         (inst jmp :z yep)
313         (inst jmp nope)
314
315         (emit-label single-word)
316         ;; Get the single digit.
317         (loadw rax-tn value bignum-digits-offset other-pointer-lowtag)
318
319         ;; positive implies (unsigned-byte 64).
320         (emit-label fixnum)
321         (inst or rax-tn rax-tn)
322         (inst jmp (if not-p :s :ns) target)
323
324         (emit-label not-target)))))
325
326 (define-vop (check-unsigned-byte-64 check-type)
327   (:generator 45
328     (let ((nope
329            (generate-error-code vop 'object-not-unsigned-byte-64-error value))
330           (yep (gen-label))
331           (fixnum (gen-label))
332           (single-word (gen-label)))
333
334       ;; Is it a fixnum?
335       (generate-fixnum-test value)
336       (move rax-tn value)
337       (inst jmp :e fixnum)
338
339       ;; If not, is it an other pointer?
340       (inst and rax-tn lowtag-mask)
341       (inst cmp rax-tn other-pointer-lowtag)
342       (inst jmp :ne nope)
343       ;; Get the header.
344       (loadw rax-tn value 0 other-pointer-lowtag)
345       ;; Is it one?
346       (inst cmp rax-tn (+ (ash 1 n-widetag-bits) bignum-widetag))
347       (inst jmp :e single-word)
348       ;; If it's other than two, we can't be an (unsigned-byte 64)
349       (inst cmp rax-tn (+ (ash 2 n-widetag-bits) bignum-widetag))
350       (inst jmp :ne nope)
351       ;; Get the second digit.
352       (loadw rax-tn value (1+ bignum-digits-offset) other-pointer-lowtag)
353       ;; All zeros, its an (unsigned-byte 64).
354       (inst or rax-tn rax-tn)
355       (inst jmp :z yep)
356       (inst jmp nope)
357
358       (emit-label single-word)
359       ;; Get the single digit.
360       (loadw rax-tn value bignum-digits-offset other-pointer-lowtag)
361
362       ;; positive implies (unsigned-byte 64).
363       (emit-label fixnum)
364       (inst or rax-tn rax-tn)
365       (inst jmp :s nope)
366
367       (emit-label yep)
368       (move result value))))
369 \f
370 ;;;; list/symbol types
371 ;;;
372 ;;; symbolp (or symbol (eq nil))
373 ;;; consp (and list (not (eq nil)))
374
375 (define-vop (symbolp type-predicate)
376   (:translate symbolp)
377   (:generator 12
378     (let ((is-symbol-label (if not-p DROP-THRU target)))
379       (inst cmp value nil-value)
380       (inst jmp :e is-symbol-label)
381       (test-type value target not-p (symbol-header-widetag)))
382     DROP-THRU))
383
384 (define-vop (check-symbol check-type)
385   (:generator 12
386     (let ((error (generate-error-code vop 'object-not-symbol-error value)))
387       (inst cmp value nil-value)
388       (inst jmp :e DROP-THRU)
389       (test-type value error t (symbol-header-widetag)))
390     DROP-THRU
391     (move result value)))
392
393 (define-vop (consp type-predicate)
394   (:translate consp)
395   (:generator 8
396     (let ((is-not-cons-label (if not-p target DROP-THRU)))
397       (inst cmp value nil-value)
398       (inst jmp :e is-not-cons-label)
399       (test-type value target not-p (list-pointer-lowtag)))
400     DROP-THRU))
401
402 (define-vop (check-cons check-type)
403   (:generator 8
404     (let ((error (generate-error-code vop 'object-not-cons-error value)))
405       (inst cmp value nil-value)
406       (inst jmp :e error)
407       (test-type value error t (list-pointer-lowtag))
408       (move result value))))