Optimize (mod FIXNUM) type-checks on x86oids.
[sbcl.git] / src / compiler / x86 / type-vops.lisp
1 ;;;; type testing and checking VOPs for the x86 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   (emit-optimized-test-inst value 3))
18
19 (defun %test-fixnum (value target not-p)
20   (generate-fixnum-test value)
21   (inst jmp (if not-p :nz :z) target))
22
23 (defun %test-fixnum-and-headers (value target not-p headers)
24   (let ((drop-through (gen-label)))
25     (generate-fixnum-test value)
26     (inst jmp :z (if not-p drop-through target))
27     (%test-headers value target not-p nil headers drop-through)))
28
29 (defun %test-immediate (value target not-p immediate)
30   ;; Code a single instruction byte test if possible.
31   (let ((offset (tn-offset value)))
32     (cond ((and (sc-is value any-reg descriptor-reg)
33                 (or (= offset eax-offset) (= offset ebx-offset)
34                     (= offset ecx-offset) (= offset edx-offset)))
35            (inst cmp (make-random-tn :kind :normal
36                                      :sc (sc-or-lose 'byte-reg)
37                                      :offset offset)
38                  immediate))
39           (t
40            (move eax-tn value)
41            (inst cmp al-tn immediate))))
42   (inst jmp (if not-p :ne :e) target))
43
44 (defun %test-lowtag (value target not-p lowtag)
45   (inst lea eax-tn (make-ea :dword :base value :disp (- lowtag)))
46   (inst test al-tn lowtag-mask)
47   ;; FIXME: another 'optimization' which doesn't appear to work:
48   ;; prefetching the hypothetically pointed-to version should help,
49   ;; but this is in fact non-ideal in plenty of ways: we emit way too
50   ;; many of these prefetch instructions; pointed-to objects are very
51   ;; often in the cache anyway; etc. etc.  Still, as proof-of-concept,
52   ;; not too bad.  -- CSR, 2004-07-27
53   (when (member :prefetch *backend-subfeatures*)
54     (inst prefetchnta (make-ea :byte :base value :disp (- lowtag))))
55   (inst jmp (if not-p :ne :e) target))
56
57 (defun %test-headers (value target not-p function-p headers
58                             &optional (drop-through (gen-label)))
59   (let ((lowtag (if function-p fun-pointer-lowtag other-pointer-lowtag)))
60     (multiple-value-bind (equal less-or-equal greater-or-equal when-true when-false)
61         ;; EQUAL, LESS-OR-EQUAL and GREATER-OR-EQUAL are the conditions for
62         ;; branching to TARGET.  WHEN-TRUE and WHEN-FALSE are the
63         ;; labels to branch to when we know it's true and when we know
64         ;; it's false respectively.
65         (if not-p
66             (values :ne :a :b drop-through target)
67             (values :e :na :nb target drop-through))
68       (%test-lowtag value when-false t lowtag)
69       (cond
70         ((and (null (cdr headers))
71               (numberp (car headers)))
72          ;; Optimize the common case: referencing the value from memory
73          ;; is slightly smaller than loading it and then doing the
74          ;; comparison.  Doing this for other cases (e.g. range of
75          ;; [BIGNUM-WIDETAG..FOO-WIDETAG]) is also possible, but such
76          ;; opportunities don't come up very often and the code would
77          ;; get pretty hairy...
78          (inst cmp (make-ea :byte :base value :disp (- lowtag)) (car headers))
79          (inst jmp equal target))
80         (t
81          (inst mov al-tn (make-ea :byte :base value :disp (- lowtag)))
82          (do ((remaining headers (cdr remaining)))
83              ((null remaining))
84            (let ((header (car remaining))
85                  (last (null (cdr remaining))))
86              (cond
87                ((atom header)
88                 (cond
89                   ((and (not last) (null (cddr remaining))
90                         (atom (cadr remaining))
91                         (= (logcount (logxor header (cadr remaining))) 1))
92                    ;; BASE-STRING, (VECTOR NIL), BIT-VECTOR, (VECTOR T)
93                    (inst and al-tn (ldb (byte 8 0) (logeqv header (cadr remaining))))
94                    (inst cmp al-tn (ldb (byte 8 0) (logand header (cadr remaining))))
95                    (inst jmp equal target)
96                    (return))
97                   (t
98                    (inst cmp al-tn header)
99                    (if last
100                        (inst jmp equal target)
101                        (inst jmp :e when-true)))))
102                (t
103                 (let ((start (car header))
104                       (end (cdr header)))
105                   (cond
106                     ;; LAST = don't need al-tn later
107                     ((and last (not (= start bignum-widetag))
108                           (= (+ start 4) end) (= (logcount (logxor start end)) 1))
109                      ;; SIMPLE-STRING
110                      (inst and al-tn (ldb (byte 8 0) (logeqv start end)))
111                      (inst cmp al-tn (ldb (byte 8 0) (logand start end)))
112                      (inst jmp equal target))
113                     ((and (not last) (null (cddr remaining))
114                           (= (+ start 4) end) (= (logcount (logxor start end)) 1)
115                           (listp (cadr remaining))
116                           (= (+ (caadr remaining) 4) (cdadr remaining))
117                           (= (logcount (logxor (caadr remaining) (cdadr remaining))) 1)
118                           (= (logcount (logxor (caadr remaining) start)) 1))
119                      ;; STRING
120                      (inst and al-tn (ldb (byte 8 0) (logeqv start (cdadr remaining))))
121                      (inst cmp al-tn (ldb (byte 8 0) (logand start (cdadr remaining))))
122                      (inst jmp equal target)
123                      ;; we've shortcircuited the DO, so we must return.
124                      ;; It's OK to do so, because (NULL (CDDR REMAINING))
125                      ;; was true.
126                      (return))
127                     (t
128                      (cond
129                        ((= start bignum-widetag)
130                         (inst cmp al-tn end)
131                         (if last
132                             (inst jmp less-or-equal target)
133                             (inst jmp :be when-true)))
134                        ((= end complex-array-widetag)
135                         (inst cmp al-tn start)
136                         (if last
137                             (inst jmp greater-or-equal target)
138                             (inst jmp :b when-false)))
139                        ((not last)
140                         (inst cmp al-tn start)
141                         (inst jmp :b when-false)
142                         (inst cmp al-tn end)
143                         (if last
144                             (inst jmp less-or-equal target)
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 \f
152 ;;;; type checking and testing
153
154 (define-vop (check-type)
155   (:args (value :target result :scs (any-reg descriptor-reg)))
156   (:results (result :scs (any-reg descriptor-reg)))
157   (:temporary (:sc unsigned-reg :offset eax-offset :to (:result 0)) eax)
158   (:ignore eax)
159   (:vop-var vop)
160   (:save-p :compute-only))
161
162 (define-vop (type-predicate)
163   (:args (value :scs (any-reg descriptor-reg)))
164   (:temporary (:sc unsigned-reg :offset eax-offset) eax)
165   (:ignore eax)
166   (:conditional)
167   (:info target not-p)
168   (:policy :fast-safe))
169
170 ;;; simpler VOP that don't need a temporary register
171 (define-vop (simple-check-type)
172   (:args (value :target result :scs (any-reg descriptor-reg)))
173   (:results (result :scs (any-reg descriptor-reg)
174                     :load-if (not (and (sc-is value any-reg descriptor-reg)
175                                        (sc-is result control-stack)))))
176   (:vop-var vop)
177   (:save-p :compute-only))
178
179 (define-vop (simple-type-predicate)
180   (:args (value :scs (any-reg descriptor-reg control-stack)))
181   (:conditional)
182   (:info target not-p)
183   (:policy :fast-safe))
184
185 (defun cost-to-test-types (type-codes)
186   (+ (* 2 (length type-codes))
187      (if (> (apply #'max type-codes) lowtag-limit) 7 2)))
188
189 (defmacro !define-type-vops (pred-name check-name ptype error-code
190                              (&rest type-codes)
191                              &key (variant nil variant-p) &allow-other-keys)
192   ;; KLUDGE: UGH. Why do we need this eval? Can't we put this in the
193   ;; expansion?
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-32 simple-type-predicate)
217   (:args (value :scs (unsigned-reg)))
218   (:info)
219   (:conditional :be)
220   (:arg-types unsigned-num)
221   (:translate fixnump)
222   (:generator 5
223     ;; We could encode this with :Z and SHR, analogously to the signed-byte-32
224     ;; case below -- as we do on x86-64 -- but that costs us an extra
225     ;; register. Compromises...
226     (inst cmp value #.sb!xc:most-positive-fixnum)))
227
228 (define-vop (fixnump/signed-byte-32 type-predicate)
229   (:args (value :scs (signed-reg)))
230   (:info)
231   (:conditional :z)
232   (:arg-types signed-num)
233   (:translate fixnump)
234   (:generator 5
235     ;; Hackers Delight, p. 53: signed
236     ;;    a <= x <= a + 2^n - 1
237     ;; is equivalent to unsigned
238     ;;    ((x-a) >> n) = 0
239     (inst mov eax-tn value)
240     (inst sub eax-tn #.sb!xc:most-negative-fixnum)
241     (inst shr eax-tn #.(integer-length (- sb!xc:most-positive-fixnum
242                                           sb!xc:most-negative-fixnum)))))
243
244 ;;; A (SIGNED-BYTE 32) can be represented with either fixnum or a bignum with
245 ;;; exactly one digit.
246
247 (define-vop (signed-byte-32-p type-predicate)
248   (:translate signed-byte-32-p)
249   (:generator 45
250     (multiple-value-bind (yep nope)
251         (if not-p
252             (values not-target target)
253             (values target not-target))
254       (generate-fixnum-test value)
255       (inst jmp :e yep)
256       (inst lea eax-tn (make-ea :dword :base value
257                                 :disp (- other-pointer-lowtag)))
258       (inst test al-tn lowtag-mask)
259       (inst jmp :ne nope)
260       (inst cmp (make-ea-for-object-slot value 0 other-pointer-lowtag)
261             (+ (ash 1 n-widetag-bits) bignum-widetag))
262       (inst jmp (if not-p :ne :e) target))
263     NOT-TARGET))
264
265 (define-vop (check-signed-byte-32 check-type)
266   (:generator 45
267     (let ((nope (generate-error-code vop
268                                      'object-not-signed-byte-32-error
269                                      value)))
270       (generate-fixnum-test value)
271       (inst jmp :e yep)
272       (inst lea eax-tn (make-ea :dword :base value
273                                 :disp (- other-pointer-lowtag)))
274       (inst test al-tn lowtag-mask)
275       (inst jmp :ne nope)
276       (inst cmp (make-ea-for-object-slot value 0 other-pointer-lowtag)
277             (+ (ash 1 n-widetag-bits) bignum-widetag))
278       (inst jmp :ne nope))
279     YEP
280     (move result value)))
281
282 ;;; An (unsigned-byte 32) can be represented with either a positive
283 ;;; fixnum, a bignum with exactly one positive digit, or a bignum with
284 ;;; exactly two digits and the second digit all zeros.
285 (define-vop (unsigned-byte-32-p type-predicate)
286   (:translate unsigned-byte-32-p)
287   (:generator 45
288     (let ((not-target (gen-label))
289           (single-word (gen-label))
290           (fixnum (gen-label)))
291       (multiple-value-bind (yep nope)
292           (if not-p
293               (values not-target target)
294               (values target not-target))
295         ;; Is it a fixnum?
296         (generate-fixnum-test value)
297         (move eax-tn value)
298         (inst jmp :e fixnum)
299
300         ;; If not, is it an other pointer?
301         (inst and al-tn lowtag-mask)
302         (inst cmp al-tn other-pointer-lowtag)
303         (inst jmp :ne nope)
304         ;; Get the header.
305         (loadw eax-tn value 0 other-pointer-lowtag)
306         ;; Is it one?
307         (inst cmp eax-tn (+ (ash 1 n-widetag-bits) bignum-widetag))
308         (inst jmp :e single-word)
309         ;; If it's other than two, we can't be an (unsigned-byte 32)
310         (inst cmp eax-tn (+ (ash 2 n-widetag-bits) bignum-widetag))
311         (inst jmp :ne nope)
312         ;; Get the second digit.
313         (loadw eax-tn value (1+ bignum-digits-offset) other-pointer-lowtag)
314         ;; All zeros, its an (unsigned-byte 32).
315         (inst test eax-tn eax-tn)
316         (inst jmp :z yep)
317         (inst jmp nope)
318
319         (emit-label single-word)
320         ;; Get the single digit.
321         (loadw eax-tn value bignum-digits-offset other-pointer-lowtag)
322
323         ;; positive implies (unsigned-byte 32).
324         (emit-label fixnum)
325         (inst test eax-tn eax-tn)
326         (inst jmp (if not-p :s :ns) target)
327
328         (emit-label not-target)))))
329
330 (define-vop (check-unsigned-byte-32 check-type)
331   (:generator 45
332     (let ((nope
333            (generate-error-code vop 'object-not-unsigned-byte-32-error value))
334           (yep (gen-label))
335           (fixnum (gen-label))
336           (single-word (gen-label)))
337
338       ;; Is it a fixnum?
339       (generate-fixnum-test value)
340       (move eax-tn value)
341       (inst jmp :e fixnum)
342
343       ;; If not, is it an other pointer?
344       (inst and al-tn lowtag-mask)
345       (inst cmp al-tn other-pointer-lowtag)
346       (inst jmp :ne nope)
347       ;; Get the header.
348       (loadw eax-tn value 0 other-pointer-lowtag)
349       ;; Is it one?
350       (inst cmp eax-tn (+ (ash 1 n-widetag-bits) bignum-widetag))
351       (inst jmp :e single-word)
352       ;; If it's other than two, we can't be an (unsigned-byte 32)
353       (inst cmp eax-tn (+ (ash 2 n-widetag-bits) bignum-widetag))
354       (inst jmp :ne nope)
355       ;; Get the second digit.
356       (loadw eax-tn value (1+ bignum-digits-offset) other-pointer-lowtag)
357       ;; All zeros, its an (unsigned-byte 32).
358       (inst test eax-tn eax-tn)
359       (inst jmp :z yep)
360       (inst jmp nope)
361
362       (emit-label single-word)
363       ;; Get the single digit.
364       (loadw eax-tn value bignum-digits-offset other-pointer-lowtag)
365
366       ;; positive implies (unsigned-byte 32).
367       (emit-label fixnum)
368       (inst test eax-tn eax-tn)
369       (inst jmp :s nope)
370
371       (emit-label yep)
372       (move result value))))
373
374 (define-vop (check-mod-fixnum check-type)
375   (:info type)
376   (:temporary (:sc any-reg) temp)
377   (:generator 30
378      (let* ((low (numeric-type-low type))
379             (hi (fixnumize (numeric-type-high type)))
380             (error (gen-label)))
381        ;; FIXME: abstract
382        (assemble (*elsewhere*)
383          (emit-label error)
384          (inst mov temp hi)
385          (emit-error-break vop error-trap
386                            (error-number-or-lose 'object-not-mod-error)
387                            (list value temp)))
388        (aver (zerop low))
389        (cond
390          ;; Handle powers of two specially
391          ;; The higher bits and the fixnum tag can be tested in one go
392          ((= (logcount (1+ hi)) 1)
393           (inst test value (lognot hi))
394           (inst jmp :ne error))
395          (t
396           (generate-fixnum-test value)
397           (inst jmp :ne error)
398           (inst cmp value hi)
399           (inst jmp :a error)))
400        (move result value))))
401
402 \f
403 ;;;; list/symbol types
404 ;;;
405 ;;; symbolp (or symbol (eq nil))
406 ;;; consp (and list (not (eq nil)))
407
408 (define-vop (symbolp type-predicate)
409   (:translate symbolp)
410   (:generator 12
411     (let ((is-symbol-label (if not-p drop-thru target)))
412       (inst cmp value nil-value)
413       (inst jmp :e is-symbol-label)
414       (test-type value target not-p (symbol-header-widetag)))
415     DROP-THRU))
416
417 (define-vop (check-symbol check-type)
418   (:generator 12
419     (let ((error (generate-error-code vop 'object-not-symbol-error value)))
420       (inst cmp value nil-value)
421       (inst jmp :e drop-thru)
422       (test-type value error t (symbol-header-widetag)))
423     DROP-THRU
424     (move result value)))
425
426 (define-vop (consp type-predicate)
427   (:translate consp)
428   (:generator 8
429     (let ((is-not-cons-label (if not-p target drop-thru)))
430       (inst cmp value nil-value)
431       (inst jmp :e is-not-cons-label)
432       (test-type value target not-p (list-pointer-lowtag)))
433     DROP-THRU))
434
435 (define-vop (check-cons check-type)
436   (:generator 8
437     (let ((error (generate-error-code vop 'object-not-cons-error value)))
438       (inst cmp value nil-value)
439       (inst jmp :e error)
440       (test-type value error t (list-pointer-lowtag))
441       (move result value))))