x86: Remove one instruction from unsigned-{word}-p
[sbcl.git] / src / compiler / x86-64 / type-vops.lisp
blob5dcc3fe20c51675b2c8bd97b9e820258d1e6c2d7
1 ;;;; type testing and checking VOPs for the x86-64 VM
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.
12 (in-package "SB!VM")
14 ;;;; test generation utilities
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 (reg-in-size value :dword))
22 (move rax-tn value)))
24 (defun generate-fixnum-test (value)
25 #!+sb-doc
26 "Set the Z flag if VALUE is fixnum"
27 (inst test
28 (cond ((sc-is value any-reg descriptor-reg)
29 (reg-in-size value :byte))
30 ;; This is hooey. None of the type-vops presently allow
31 ;; control-stack as a storage class.
32 ((sc-is value control-stack)
33 (make-ea :byte :base rbp-tn
34 :disp (frame-byte-offset (tn-offset value))))
36 value))
37 fixnum-tag-mask))
39 (defun %test-fixnum (value target not-p)
40 (generate-fixnum-test value)
41 (inst jmp (if not-p :nz :z) target))
43 ;;; General FIXME: it's fine that we wire these to use rAX which has
44 ;;; the shortest encoding, but for goodness sake can we pass the TN
45 ;;; from the VOP like every other backend does? Freely referencing the
46 ;;; permanent globals RAX-TN,EAX-TN,AL-TN is a bad way to go about it.
48 (defun %lea-for-lowtag-test (target value lowtag)
49 (inst lea target (make-ea :dword :base value :disp (- lowtag))))
51 ;; Numerics including fixnum, excluding short-float. (INTEGER,RATIONAL)
52 (defun %test-fixnum-and-headers (value target not-p headers)
53 (let ((drop-through (gen-label)))
54 (case n-fixnum-tag-bits
55 (1 (%lea-for-lowtag-test eax-tn value other-pointer-lowtag)
56 (inst test al-tn 1)
57 (inst jmp :nz (if not-p drop-through target)) ; inverted
58 (%test-headers value target not-p nil headers
59 :drop-through drop-through :compute-eax nil))
61 (generate-fixnum-test value)
62 (inst jmp :z (if not-p drop-through target))
63 (%test-headers value target not-p nil headers
64 :drop-through drop-through)))))
66 ;; I can see no reason this would ever be used.
67 ;; (or fixnum character|unbound-marker) is implausible.
68 (defun %test-fixnum-and-immediate (value target not-p immediate)
69 (let ((drop-through (gen-label)))
70 (generate-fixnum-test value)
71 (inst jmp :z (if not-p drop-through target))
72 (%test-immediate value target not-p immediate drop-through)))
74 ;; Numerics
75 (defun %test-fixnum-immediate-and-headers (value target not-p immediate
76 headers)
77 (let ((drop-through (gen-label)))
78 (case n-fixnum-tag-bits
79 (1 (%lea-for-lowtag-test eax-tn value other-pointer-lowtag)
80 (inst test al-tn 1)
81 (inst jmp :nz (if not-p drop-through target)) ; inverted
82 (inst cmp al-tn (- immediate other-pointer-lowtag))
83 (inst jmp :e (if not-p drop-through target))
84 (%test-headers value target not-p nil headers
85 :drop-through drop-through :compute-eax nil))
86 (t (generate-fixnum-test value)
87 (inst jmp :z (if not-p drop-through target))
88 (%test-immediate-and-headers value target not-p immediate headers
89 drop-through)))))
91 (defun %test-immediate (value target not-p immediate
92 &optional (drop-through (gen-label)))
93 ;; Code a single instruction byte test if possible.
94 (cond ((sc-is value any-reg descriptor-reg)
95 (inst cmp (reg-in-size value :byte) immediate))
97 (move rax-tn value)
98 (inst cmp al-tn immediate)))
99 (inst jmp (if not-p :ne :e) target)
100 (emit-label drop-through))
102 ;; Numerics including short-float, excluding fixnum
103 (defun %test-immediate-and-headers (value target not-p immediate headers
104 &optional (drop-through (gen-label)))
105 ;; Code a single instruction byte test if possible.
106 (cond ((sc-is value any-reg descriptor-reg)
107 (inst cmp (reg-in-size value :byte) immediate))
109 (move rax-tn value)
110 (inst cmp al-tn immediate)))
111 (inst jmp :e (if not-p drop-through target))
112 (%test-headers value target not-p nil headers :drop-through drop-through))
114 (defun %test-lowtag (value target not-p lowtag)
115 (%lea-for-lowtag-test eax-tn value lowtag)
116 (inst test al-tn lowtag-mask)
117 (inst jmp (if not-p :nz :z) target))
119 (defun %test-headers (value target not-p function-p headers
120 &key except
121 (drop-through (gen-label))
122 (compute-eax t))
123 (let ((lowtag (if function-p fun-pointer-lowtag other-pointer-lowtag)))
124 (multiple-value-bind (equal less-or-equal greater-or-equal when-true
125 when-false)
126 ;; EQUAL, LESS-OR-EQUAL, and GREATER-OR-EQUAL are the conditions
127 ;; for branching to TARGET. WHEN-TRUE and WHEN-FALSE are the
128 ;; labels to branch to when we know it's true and when we know
129 ;; it's false respectively.
130 (if not-p
131 (values :ne :a :b drop-through target)
132 (values :e :na :nb target drop-through))
133 (when compute-eax
134 (%lea-for-lowtag-test eax-tn value lowtag))
135 (inst test al-tn lowtag-mask)
136 (inst jmp :nz when-false)
137 ;; FIXME: this backend seems to be missing the special logic for
138 ;; testing exactly two widetags differing only in a single bit,
139 ;; which through evolution is almost totally unworkable anyway...
140 (do ((remaining headers (cdr remaining))
141 ;; It is preferable (smaller and faster code) to directly
142 ;; compare the value in memory instead of loading it into
143 ;; a register first. Find out if this is possible and set
144 ;; WIDETAG-TN accordingly. If impossible, generate the
145 ;; register load.
146 ;; Compared to x86 we additionally optimize the cases of a
147 ;; range starting with BIGNUM-WIDETAG (= min widetag)
148 ;; or ending with COMPLEX-ARRAY-WIDETAG (= max widetag)
149 (widetag-tn (if (and (null (cdr headers))
150 (not except)
151 (or (atom (car headers))
152 (= (caar headers) bignum-widetag)
153 (= (cdar headers) complex-array-widetag)))
154 (make-ea :byte :base value :disp (- lowtag))
155 (progn
156 (inst mov eax-tn (make-ea :dword :base value
157 :disp (- lowtag)))
158 al-tn))))
159 ((null remaining))
160 (dolist (widetag except) ; only after loading widetag-tn
161 (inst cmp al-tn widetag)
162 (inst jmp :e when-false))
163 (setq except nil)
164 (let ((header (car remaining))
165 (last (null (cdr remaining))))
166 (cond
167 ((atom header)
168 (inst cmp widetag-tn header)
169 (if last
170 (inst jmp equal target)
171 (inst jmp :e when-true)))
173 (let ((start (car header))
174 (end (cdr header)))
175 (cond
176 ((= start bignum-widetag)
177 (inst cmp widetag-tn end)
178 (if last
179 (inst jmp less-or-equal target)
180 (inst jmp :be when-true)))
181 ((= end complex-array-widetag)
182 (inst cmp widetag-tn start)
183 (if last
184 (inst jmp greater-or-equal target)
185 (inst jmp :b when-false)))
186 ((not last)
187 (inst cmp al-tn start)
188 (inst jmp :b when-false)
189 (inst cmp al-tn end)
190 (inst jmp :be when-true))
192 (inst sub al-tn start)
193 (inst cmp al-tn (- end start))
194 (inst jmp less-or-equal target))))))))
195 (emit-label drop-through))))
197 ;;;; other integer ranges
199 (define-vop (fixnump/unsigned-byte-64 simple-type-predicate)
200 (:args (value :scs (unsigned-reg)))
201 (:arg-types unsigned-num)
202 (:translate fixnump)
203 (:temporary (:sc unsigned-reg :from (:argument 0)) tmp)
204 (:info)
205 (:conditional :z)
206 (:generator 5
207 (move tmp value)
208 (inst shr tmp n-positive-fixnum-bits)))
210 #-#.(cl:if (cl:= sb!vm:n-fixnum-tag-bits 1) '(:and) '(:or))
211 (define-vop (fixnump/signed-byte-64 simple-type-predicate)
212 (:args (value :scs (signed-reg)))
213 (:info)
214 (:conditional :z)
215 (:temporary (:sc unsigned-reg) temp)
216 (:arg-types signed-num)
217 (:translate fixnump)
218 (:generator 5
219 ;; Hackers Delight, p. 53: signed
220 ;; a <= x <= a + 2^n - 1
221 ;; is equivalent to unsigned
222 ;; ((x-a) >> n) = 0
223 (inst mov temp #.(- sb!xc:most-negative-fixnum))
224 (inst add temp value)
225 (inst shr temp n-fixnum-bits)))
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) :target temp))
230 (:info)
231 (:conditional :no)
232 (:temporary (:sc unsigned-reg :from (:argument 0)) temp)
233 (:arg-types signed-num)
234 (:translate fixnump)
235 (:generator 5
236 (move temp value)
237 ;; The overflow flag will be set if the reg's sign bit changes.
238 (inst shl temp 1)))
240 ;;; A (SIGNED-BYTE 64) can be represented with either fixnum or a bignum with
241 ;;; exactly one digit.
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 #.(case n-fixnum-tag-bits
251 (1 '(progn
252 (%lea-for-lowtag-test eax-tn value other-pointer-lowtag)
253 (inst test al-tn fixnum-tag-mask) ; 0th bit = 1 => fixnum
254 (inst jmp :nz yep)
255 (inst test al-tn lowtag-mask)))
256 (t '(progn
257 (move-qword-to-eax value)
258 (inst test al-tn fixnum-tag-mask)
259 (inst jmp :e yep)
260 (inst and al-tn lowtag-mask)
261 (inst cmp al-tn other-pointer-lowtag))))
262 (inst jmp :ne nope)
263 (inst cmp (make-ea-for-object-slot value 0 other-pointer-lowtag)
264 (+ (ash 1 n-widetag-bits) bignum-widetag))
265 (inst jmp (if not-p :ne :e) target))
266 NOT-TARGET))
268 ;;; An (unsigned-byte 64) can be represented with either a positive
269 ;;; fixnum, a bignum with exactly one positive digit, or a bignum with
270 ;;; exactly two digits and the second digit all zeros.
271 (define-vop (unsigned-byte-64-p type-predicate)
272 (:translate unsigned-byte-64-p)
273 (:generator 45
274 (let ((not-target (gen-label))
275 (single-word (gen-label))
276 (fixnum (gen-label)))
277 (multiple-value-bind (yep nope)
278 (if not-p
279 (values not-target target)
280 (values target not-target))
281 ;; Is it a fixnum?
282 (move rax-tn value)
283 (inst test al-tn fixnum-tag-mask)
284 (inst jmp :e fixnum)
286 ;; If not, is it an other pointer?
287 (inst and al-tn lowtag-mask)
288 (inst cmp al-tn other-pointer-lowtag)
289 (inst jmp :ne nope)
290 ;; Get the header.
291 (loadw rax-tn value 0 other-pointer-lowtag)
292 ;; Is it one?
293 (inst cmp rax-tn (+ (ash 1 n-widetag-bits) bignum-widetag))
294 (inst jmp :e single-word)
295 ;; If it's other than two, we can't be an (unsigned-byte 64)
296 ;: Leave RAX holding 0 in the affirmative case.
297 (inst sub rax-tn (+ (ash 2 n-widetag-bits) bignum-widetag))
298 (inst jmp :ne nope)
299 ;; Compare the second digit to zero (in RAX).
300 (inst cmp (make-ea-for-object-slot value (1+ bignum-digits-offset)
301 other-pointer-lowtag) rax-tn)
302 (inst jmp :z yep) ; All zeros, its an (unsigned-byte 64).
303 (inst jmp nope)
305 (emit-label single-word)
306 ;; Get the single digit.
307 (loadw rax-tn value bignum-digits-offset other-pointer-lowtag)
309 ;; positive implies (unsigned-byte 64).
310 (emit-label fixnum)
311 (inst test rax-tn rax-tn)
312 (inst jmp (if not-p :s :ns) target)
314 (emit-label not-target)))))
316 (defun power-of-two-limit-p (x)
317 (and (fixnump x)
318 (= (logcount (1+ x)) 1)))
320 (define-vop (test-fixnum-mod-power-of-two)
321 (:args (value :scs (any-reg descriptor-reg
322 unsigned-reg signed-reg
323 immediate)))
324 (:arg-types *
325 (:constant (satisfies power-of-two-limit-p)))
326 (:translate fixnum-mod-p)
327 (:conditional :e)
328 (:info hi)
329 (:save-p :compute-only)
330 (:policy :fast-safe)
331 (:generator 4
332 (aver (not (sc-is value immediate)))
333 (let* ((fixnum-hi (if (sc-is value unsigned-reg signed-reg)
335 (fixnumize hi))))
336 (inst test value (constantize (lognot fixnum-hi))))))
338 (define-vop (test-fixnum-mod-tagged-unsigned)
339 (:args (value :scs (any-reg descriptor-reg
340 unsigned-reg signed-reg
341 immediate)))
342 (:arg-types (:or tagged-num unsigned-num signed-num)
343 (:constant fixnum))
344 (:translate fixnum-mod-p)
345 (:conditional :be)
346 (:info hi)
347 (:save-p :compute-only)
348 (:policy :fast-safe)
349 (:generator 5
350 (aver (not (sc-is value immediate)))
351 (let ((fixnum-hi (if (sc-is value unsigned-reg signed-reg)
353 (fixnumize hi))))
354 (inst cmp value (constantize fixnum-hi)))))
356 (define-vop (test-fixnum-mod-*)
357 (:args (value :scs (any-reg descriptor-reg)))
358 (:arg-types * (:constant fixnum))
359 (:translate fixnum-mod-p)
360 (:conditional)
361 (:info target not-p hi)
362 (:save-p :compute-only)
363 (:policy :fast-safe)
364 (:generator 6
365 (let* ((fixnum-hi (fixnumize hi))
366 (skip (gen-label)))
367 (generate-fixnum-test value)
368 (inst jmp :ne (if not-p target skip))
369 (inst cmp value (constantize fixnum-hi))
370 (inst jmp (if not-p :a :be) target)
371 (emit-label skip))))
373 ;;;; list/symbol types
375 ;;; symbolp (or symbol (eq nil))
376 ;;; consp (and list (not (eq nil)))
378 (define-vop (symbolp type-predicate)
379 (:translate symbolp)
380 (:generator 12
381 (let ((is-symbol-label (if not-p DROP-THRU target)))
382 (inst cmp value nil-value)
383 (inst jmp :e is-symbol-label)
384 (test-type value target not-p (symbol-header-widetag)))
385 DROP-THRU))
387 (define-vop (consp type-predicate)
388 (:translate consp)
389 (:generator 8
390 (let ((is-not-cons-label (if not-p target DROP-THRU)))
391 (inst cmp value nil-value)
392 (inst jmp :e is-not-cons-label)
393 (test-type value target not-p (list-pointer-lowtag)))
394 DROP-THRU))
396 ;; A vop that accepts a computed set of widetags.
397 (define-vop (%other-pointer-subtype-p type-predicate)
398 (:translate %other-pointer-subtype-p)
399 (:info target not-p widetags)
400 (:arg-types * (:constant t)) ; voodoo - 'target' and 'not-p' are absent
401 (:generator 15 ; arbitrary
402 (multiple-value-bind (headers exceptions)
403 (canonicalize-headers-and-exceptions widetags)
404 (%test-headers value target not-p nil headers
405 :except exceptions))))