1.0.37.57: better DEFMETHOD pretty-printing
[sbcl/pkhuong.git] / src / compiler / generic / vm-tran.lisp
blob287162b519b1d1425081143b0a3042acc0bb9221
1 ;;;; implementation-dependent transforms
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!C")
14 ;;; We need to define these predicates, since the TYPEP source
15 ;;; transform picks whichever predicate was defined last when there
16 ;;; are multiple predicates for equivalent types.
17 (define-source-transform short-float-p (x) `(single-float-p ,x))
18 #!-long-float
19 (define-source-transform long-float-p (x) `(double-float-p ,x))
21 (define-source-transform compiled-function-p (x)
22 #!-sb-eval
23 `(functionp ,x)
24 #!+sb-eval
25 (once-only ((x x))
26 `(and (functionp ,x)
27 (not (sb!eval:interpreted-function-p ,x)))))
29 (define-source-transform char-int (x)
30 `(char-code ,x))
32 (deftransform abs ((x) (rational))
33 '(if (< x 0) (- x) x))
35 ;;; We don't want to clutter the bignum code.
36 #!+(or x86 x86-64)
37 (define-source-transform sb!bignum:%bignum-ref (bignum index)
38 ;; KLUDGE: We use TRULY-THE here because even though the bignum code
39 ;; is (currently) compiled with (SAFETY 0), the compiler insists on
40 ;; inserting CAST nodes to ensure that INDEX is of the correct type.
41 ;; These CAST nodes do not generate any type checks, but they do
42 ;; interfere with the operation of FOLD-INDEX-ADDRESSING, below.
43 ;; This scenario is a problem for the more user-visible case of
44 ;; folding as well. --njf, 2006-12-01
45 `(sb!bignum:%bignum-ref-with-offset ,bignum
46 (truly-the bignum-index ,index) 0))
48 #!+(or x86 x86-64)
49 (defun fold-index-addressing (fun-name element-size lowtag data-offset
50 index offset &optional setter-p)
51 (multiple-value-bind (func index-args) (extract-fun-args index '(+ -) 2)
52 (destructuring-bind (x constant) index-args
53 (declare (ignorable x))
54 (unless (constant-lvar-p constant)
55 (give-up-ir1-transform))
56 (let ((value (lvar-value constant)))
57 (unless (and (integerp value)
58 (sb!vm::foldable-constant-offset-p
59 element-size lowtag data-offset
60 (funcall func value (lvar-value offset))))
61 (give-up-ir1-transform "constant is too large for inlining"))
62 (splice-fun-args index func 2)
63 `(lambda (thing index off1 off2 ,@(when setter-p
64 '(value)))
65 (,fun-name thing index (,func off2 off1) ,@(when setter-p
66 '(value))))))))
68 #!+(or x86 x86-64)
69 (deftransform sb!bignum:%bignum-ref-with-offset
70 ((bignum index offset) * * :node node)
71 (fold-index-addressing 'sb!bignum:%bignum-ref-with-offset
72 sb!vm:n-word-bits sb!vm:other-pointer-lowtag
73 sb!vm:bignum-digits-offset
74 index offset))
76 ;;; The layout is stored in slot 0.
77 (define-source-transform %instance-layout (x)
78 `(truly-the layout (%instance-ref ,x 0)))
79 (define-source-transform %set-instance-layout (x val)
80 `(%instance-set ,x 0 (the layout ,val)))
81 (define-source-transform %funcallable-instance-layout (x)
82 `(truly-the layout (%funcallable-instance-info ,x 0)))
83 (define-source-transform %set-funcallable-instance-layout (x val)
84 `(setf (%funcallable-instance-info ,x 0) (the layout ,val)))
86 ;;;; character support
88 ;;; In our implementation there are really only BASE-CHARs.
89 #+nil
90 (define-source-transform characterp (obj)
91 `(base-char-p ,obj))
93 ;;;; simplifying HAIRY-DATA-VECTOR-REF and HAIRY-DATA-VECTOR-SET
95 (deftransform hairy-data-vector-ref ((string index) (simple-string t))
96 (let ((ctype (lvar-type string)))
97 (if (array-type-p ctype)
98 ;; the other transform will kick in, so that's OK
99 (give-up-ir1-transform)
100 `(etypecase string
101 ((simple-array character (*))
102 (data-vector-ref string index))
103 #!+sb-unicode
104 ((simple-array base-char (*))
105 (data-vector-ref string index))
106 ((simple-array nil (*))
107 (data-vector-ref string index))))))
109 ;;; This and the corresponding -SET transform work equally well on non-simple
110 ;;; arrays, but after benchmarking (on x86), Nikodemus didn't find any cases
111 ;;; where it actually helped with non-simple arrays -- to the contrary, it
112 ;;; only made for bigger and up 1o 100% slower code.
113 (deftransform hairy-data-vector-ref ((array index) (simple-array t) *)
114 "avoid runtime dispatch on array element type"
115 (let* ((type (lvar-type array))
116 (element-ctype (array-type-upgraded-element-type type))
117 (declared-element-ctype (array-type-declared-element-type type)))
118 (declare (type ctype element-ctype))
119 (when (eq *wild-type* element-ctype)
120 (give-up-ir1-transform
121 "Upgraded element type of array is not known at compile time."))
122 ;; (The expansion here is basically a degenerate case of
123 ;; WITH-ARRAY-DATA. Since WITH-ARRAY-DATA is implemented as a
124 ;; macro, and macros aren't expanded in transform output, we have
125 ;; to hand-expand it ourselves.)
126 (let* ((element-type-specifier (type-specifier element-ctype)))
127 `(multiple-value-bind (array index)
128 (%data-vector-and-index array index)
129 (declare (type (simple-array ,element-type-specifier 1) array))
130 ,(let ((bare-form '(data-vector-ref array index)))
131 (if (type= element-ctype declared-element-ctype)
132 bare-form
133 `(the ,(type-specifier declared-element-ctype)
134 ,bare-form)))))))
136 ;;; Transform multi-dimensional array to one dimensional data vector
137 ;;; access.
138 (deftransform data-vector-ref ((array index) (simple-array t))
139 (let ((array-type (lvar-type array)))
140 (unless (array-type-p array-type)
141 (give-up-ir1-transform))
142 (let ((dims (array-type-dimensions array-type)))
143 (when (or (atom dims) (= (length dims) 1))
144 (give-up-ir1-transform))
145 (let ((el-type (array-type-specialized-element-type array-type))
146 (total-size (if (member '* dims)
148 (reduce #'* dims))))
149 `(data-vector-ref (truly-the (simple-array ,(type-specifier el-type)
150 (,total-size))
151 (%array-data-vector array))
152 index)))))
154 ;;; Transform data vector access to a form that opens up optimization
155 ;;; opportunities. On platforms that support DATA-VECTOR-REF-WITH-OFFSET
156 ;;; DATA-VECTOR-REF is not supported at all.
157 #!+(or x86 x86-64)
158 (define-source-transform data-vector-ref (array index)
159 `(data-vector-ref-with-offset ,array ,index 0))
161 #!+(or x86 x86-64)
162 (deftransform data-vector-ref-with-offset ((array index offset))
163 (let ((array-type (lvar-type array)))
164 (when (or (not (array-type-p array-type))
165 (eql (array-type-specialized-element-type array-type)
166 *wild-type*))
167 (give-up-ir1-transform))
168 ;; It shouldn't be possible to get here with anything but a non-complex
169 ;; vector.
170 (aver (not (array-type-complexp array-type)))
171 (let* ((element-type (type-specifier (array-type-specialized-element-type array-type)))
172 (saetp (find-saetp element-type)))
173 (when (< (sb!vm:saetp-n-bits saetp) sb!vm:n-byte-bits)
174 (give-up-ir1-transform))
175 (fold-index-addressing 'data-vector-ref-with-offset
176 (sb!vm:saetp-n-bits saetp)
177 sb!vm:other-pointer-lowtag
178 sb!vm:vector-data-offset
179 index offset))))
181 (deftransform hairy-data-vector-set ((string index new-value)
182 (simple-string t t))
183 (let ((ctype (lvar-type string)))
184 (if (array-type-p ctype)
185 ;; the other transform will kick in, so that's OK
186 (give-up-ir1-transform)
187 `(etypecase string
188 ((simple-array character (*))
189 (data-vector-set string index new-value))
190 #!+sb-unicode
191 ((simple-array base-char (*))
192 (data-vector-set string index new-value))
193 ((simple-array nil (*))
194 (data-vector-set string index new-value))))))
196 ;;; This and the corresponding -REF transform work equally well on non-simple
197 ;;; arrays, but after benchmarking (on x86), Nikodemus didn't find any cases
198 ;;; where it actually helped with non-simple arrays -- to the contrary, it
199 ;;; only made for bigger and up 1o 100% slower code.
200 (deftransform hairy-data-vector-set ((array index new-value)
201 (simple-array t t)
203 "avoid runtime dispatch on array element type"
204 (let* ((type (lvar-type array))
205 (element-ctype (array-type-upgraded-element-type type))
206 (declared-element-ctype (array-type-declared-element-type type)))
207 (declare (type ctype element-ctype))
208 (when (eq *wild-type* element-ctype)
209 (give-up-ir1-transform
210 "Upgraded element type of array is not known at compile time."))
211 (let ((element-type-specifier (type-specifier element-ctype)))
212 `(multiple-value-bind (array index)
213 (%data-vector-and-index array index)
214 (declare (type (simple-array ,element-type-specifier 1) array)
215 (type ,element-type-specifier new-value))
216 ,(if (type= element-ctype declared-element-ctype)
217 '(data-vector-set array index new-value)
218 `(truly-the ,(type-specifier declared-element-ctype)
219 (data-vector-set array index
220 (the ,(type-specifier declared-element-ctype)
221 new-value))))))))
223 ;;; Transform multi-dimensional array to one dimensional data vector
224 ;;; access.
225 (deftransform data-vector-set ((array index new-value)
226 (simple-array t t))
227 (let ((array-type (lvar-type array)))
228 (unless (array-type-p array-type)
229 (give-up-ir1-transform))
230 (let ((dims (array-type-dimensions array-type)))
231 (when (or (atom dims) (= (length dims) 1))
232 (give-up-ir1-transform))
233 (let ((el-type (array-type-specialized-element-type array-type))
234 (total-size (if (member '* dims)
236 (reduce #'* dims))))
237 `(data-vector-set (truly-the (simple-array ,(type-specifier el-type)
238 (,total-size))
239 (%array-data-vector array))
240 index
241 new-value)))))
243 ;;; Transform data vector access to a form that opens up optimization
244 ;;; opportunities.
245 #!+(or x86 x86-64)
246 (define-source-transform data-vector-set (array index new-value)
247 `(data-vector-set-with-offset ,array ,index 0 ,new-value))
249 #!+(or x86 x86-64)
250 (deftransform data-vector-set-with-offset ((array index offset new-value))
251 (let ((array-type (lvar-type array)))
252 (when (or (not (array-type-p array-type))
253 (eql (array-type-specialized-element-type array-type)
254 *wild-type*))
255 ;; We don't yet know the exact element type, but will get that
256 ;; knowledge after some more type propagation.
257 (give-up-ir1-transform))
258 (aver (not (array-type-complexp array-type)))
259 (let* ((element-type (type-specifier (array-type-specialized-element-type array-type)))
260 (saetp (find-saetp element-type)))
261 (when (< (sb!vm:saetp-n-bits saetp) sb!vm:n-byte-bits)
262 (give-up-ir1-transform))
263 (fold-index-addressing 'data-vector-set-with-offset
264 (sb!vm:saetp-n-bits saetp)
265 sb!vm:other-pointer-lowtag
266 sb!vm:vector-data-offset
267 index offset t))))
269 (defun maybe-array-data-vector-type-specifier (array-lvar)
270 (let ((atype (lvar-type array-lvar)))
271 (when (array-type-p atype)
272 (let ((dims (array-type-dimensions atype)))
273 (if (or (array-type-complexp atype)
274 (eq '* dims)
275 (notevery #'integerp dims))
276 `(simple-array ,(type-specifier
277 (array-type-specialized-element-type atype))
278 (*))
279 `(simple-array ,(type-specifier
280 (array-type-specialized-element-type atype))
281 (,(apply #'* dims))))))))
283 (macrolet ((def (name)
284 `(defoptimizer (,name derive-type) ((array-lvar))
285 (let ((spec (maybe-array-data-vector-type-specifier array-lvar)))
286 (when spec
287 (specifier-type spec))))))
288 (def %array-data-vector)
289 (def array-storage-vector))
291 (defoptimizer (%data-vector-and-index derive-type) ((array index))
292 (let ((spec (maybe-array-data-vector-type-specifier array)))
293 (when spec
294 (values-specifier-type `(values ,spec index)))))
296 (deftransform %data-vector-and-index ((%array %index)
297 (simple-array t)
299 ;; KLUDGE: why the percent signs? Well, ARRAY and INDEX are
300 ;; respectively exported from the CL and SB!INT packages, which
301 ;; means that they're visible to all sorts of things. If the
302 ;; compiler can prove that the call to ARRAY-HEADER-P, below, either
303 ;; returns T or NIL, it will delete the irrelevant branch. However,
304 ;; user code might have got here with a variable named CL:ARRAY, and
305 ;; quite often compiler code with a variable named SB!INT:INDEX, so
306 ;; this can generate code deletion notes for innocuous user code:
307 ;; (DEFUN F (ARRAY I) (DECLARE (SIMPLE-VECTOR ARRAY)) (AREF ARRAY I))
308 ;; -- CSR, 2003-04-01
310 ;; We do this solely for the -OR-GIVE-UP side effect, since we want
311 ;; to know that the type can be figured out in the end before we
312 ;; proceed, but we don't care yet what the type will turn out to be.
313 (upgraded-element-type-specifier-or-give-up %array)
315 '(if (array-header-p %array)
316 (values (%array-data-vector %array) %index)
317 (values %array %index)))
319 ;;;; BIT-VECTOR hackery
321 ;;; SIMPLE-BIT-VECTOR bit-array operations are transformed to a word
322 ;;; loop that does 32 bits at a time.
324 ;;; FIXME: This is a lot of repeatedly macroexpanded code. It should
325 ;;; be a function call instead.
326 (macrolet ((def (bitfun wordfun)
327 `(deftransform ,bitfun ((bit-array-1 bit-array-2 result-bit-array)
328 (simple-bit-vector
329 simple-bit-vector
330 simple-bit-vector)
332 :node node :policy (>= speed space))
333 `(progn
334 ,@(unless (policy node (zerop safety))
335 '((unless (= (length bit-array-1)
336 (length bit-array-2)
337 (length result-bit-array))
338 (error "Argument and/or result bit arrays are not the same length:~
339 ~% ~S~% ~S ~% ~S"
340 bit-array-1
341 bit-array-2
342 result-bit-array))))
343 (let ((length (length result-bit-array)))
344 (if (= length 0)
345 ;; We avoid doing anything to 0-length
346 ;; bit-vectors, or rather, the memory that
347 ;; follows them. Other divisible-by-32 cases
348 ;; are handled by the (1- length), below.
349 ;; CSR, 2002-04-24
350 result-bit-array
351 (do ((index 0 (1+ index))
352 ;; bit-vectors of length 1-32 need
353 ;; precisely one (SETF %VECTOR-RAW-BITS),
354 ;; done here in the epilogue. - CSR,
355 ;; 2002-04-24
356 (end-1 (truncate (truly-the index (1- length))
357 sb!vm:n-word-bits)))
358 ((>= index end-1)
359 (setf (%vector-raw-bits result-bit-array index)
360 (,',wordfun (%vector-raw-bits bit-array-1 index)
361 (%vector-raw-bits bit-array-2 index)))
362 result-bit-array)
363 (declare (optimize (speed 3) (safety 0))
364 (type index index end-1))
365 (setf (%vector-raw-bits result-bit-array index)
366 (,',wordfun (%vector-raw-bits bit-array-1 index)
367 (%vector-raw-bits bit-array-2 index))))))))))
368 (def bit-and word-logical-and)
369 (def bit-ior word-logical-or)
370 (def bit-xor word-logical-xor)
371 (def bit-eqv word-logical-eqv)
372 (def bit-nand word-logical-nand)
373 (def bit-nor word-logical-nor)
374 (def bit-andc1 word-logical-andc1)
375 (def bit-andc2 word-logical-andc2)
376 (def bit-orc1 word-logical-orc1)
377 (def bit-orc2 word-logical-orc2))
379 (deftransform bit-not
380 ((bit-array result-bit-array)
381 (simple-bit-vector simple-bit-vector) *
382 :node node :policy (>= speed space))
383 `(progn
384 ,@(unless (policy node (zerop safety))
385 '((unless (= (length bit-array)
386 (length result-bit-array))
387 (error "Argument and result bit arrays are not the same length:~
388 ~% ~S~% ~S"
389 bit-array result-bit-array))))
390 (let ((length (length result-bit-array)))
391 (if (= length 0)
392 ;; We avoid doing anything to 0-length bit-vectors, or rather,
393 ;; the memory that follows them. Other divisible-by
394 ;; n-word-bits cases are handled by the (1- length), below.
395 ;; CSR, 2002-04-24
396 result-bit-array
397 (do ((index 0 (1+ index))
398 ;; bit-vectors of length 1 to n-word-bits need precisely
399 ;; one (SETF %VECTOR-RAW-BITS), done here in the
400 ;; epilogue. - CSR, 2002-04-24
401 (end-1 (truncate (truly-the index (1- length))
402 sb!vm:n-word-bits)))
403 ((>= index end-1)
404 (setf (%vector-raw-bits result-bit-array index)
405 (word-logical-not (%vector-raw-bits bit-array index)))
406 result-bit-array)
407 (declare (optimize (speed 3) (safety 0))
408 (type index index end-1))
409 (setf (%vector-raw-bits result-bit-array index)
410 (word-logical-not (%vector-raw-bits bit-array index))))))))
412 (deftransform bit-vector-= ((x y) (simple-bit-vector simple-bit-vector))
413 `(and (= (length x) (length y))
414 (let ((length (length x)))
415 (or (= length 0)
416 (do* ((i 0 (+ i 1))
417 (end-1 (floor (1- length) sb!vm:n-word-bits)))
418 ((>= i end-1)
419 (let* ((extra (1+ (mod (1- length) sb!vm:n-word-bits)))
420 (mask (ash #.(1- (ash 1 sb!vm:n-word-bits))
421 (- extra sb!vm:n-word-bits)))
422 (numx
423 (logand
424 (ash mask
425 ,(ecase sb!c:*backend-byte-order*
426 (:little-endian 0)
427 (:big-endian
428 '(- sb!vm:n-word-bits extra))))
429 (%vector-raw-bits x i)))
430 (numy
431 (logand
432 (ash mask
433 ,(ecase sb!c:*backend-byte-order*
434 (:little-endian 0)
435 (:big-endian
436 '(- sb!vm:n-word-bits extra))))
437 (%vector-raw-bits y i))))
438 (declare (type (integer 1 #.sb!vm:n-word-bits) extra)
439 (type sb!vm:word mask numx numy))
440 (= numx numy)))
441 (declare (type index i end-1))
442 (let ((numx (%vector-raw-bits x i))
443 (numy (%vector-raw-bits y i)))
444 (declare (type sb!vm:word numx numy))
445 (unless (= numx numy)
446 (return nil))))))))
448 (deftransform count ((item sequence) (bit simple-bit-vector) *
449 :policy (>= speed space))
450 `(let ((length (length sequence)))
451 (if (zerop length)
453 (do ((index 0 (1+ index))
454 (count 0)
455 (end-1 (truncate (truly-the index (1- length))
456 sb!vm:n-word-bits)))
457 ((>= index end-1)
458 (let* ((extra (1+ (mod (1- length) sb!vm:n-word-bits)))
459 (mask (ash #.(1- (ash 1 sb!vm:n-word-bits))
460 (- extra sb!vm:n-word-bits)))
461 (bits (logand (ash mask
462 ,(ecase sb!c:*backend-byte-order*
463 (:little-endian 0)
464 (:big-endian
465 '(- sb!vm:n-word-bits extra))))
466 (%vector-raw-bits sequence index))))
467 (declare (type (integer 1 #.sb!vm:n-word-bits) extra))
468 (declare (type sb!vm:word mask bits))
469 (incf count (logcount bits))
470 ,(if (constant-lvar-p item)
471 (if (zerop (lvar-value item))
472 '(- length count)
473 'count)
474 '(if (zerop item)
475 (- length count)
476 count))))
477 (declare (type index index count end-1)
478 (optimize (speed 3) (safety 0)))
479 (incf count (logcount (%vector-raw-bits sequence index)))))))
481 (deftransform fill ((sequence item) (simple-bit-vector bit) *
482 :policy (>= speed space))
483 (let ((value (if (constant-lvar-p item)
484 (if (= (lvar-value item) 0)
486 #.(1- (ash 1 sb!vm:n-word-bits)))
487 `(if (= item 0) 0 #.(1- (ash 1 sb!vm:n-word-bits))))))
488 `(let ((length (length sequence))
489 (value ,value))
490 (if (= length 0)
491 sequence
492 (do ((index 0 (1+ index))
493 ;; bit-vectors of length 1 to n-word-bits need precisely
494 ;; one (SETF %VECTOR-RAW-BITS), done here in the
495 ;; epilogue. - CSR, 2002-04-24
496 (end-1 (truncate (truly-the index (1- length))
497 sb!vm:n-word-bits)))
498 ((>= index end-1)
499 (setf (%vector-raw-bits sequence index) value)
500 sequence)
501 (declare (optimize (speed 3) (safety 0))
502 (type index index end-1))
503 (setf (%vector-raw-bits sequence index) value))))))
505 (deftransform fill ((sequence item) (simple-base-string base-char) *
506 :policy (>= speed space))
507 (let ((value (if (constant-lvar-p item)
508 (let* ((char (lvar-value item))
509 (code (sb!xc:char-code char))
510 (accum 0))
511 (dotimes (i sb!vm:n-word-bytes accum)
512 (setf accum (logior accum (ash code (* 8 i))))))
513 `(let ((code (sb!xc:char-code item)))
514 (logior ,@(loop for i from 0 below sb!vm:n-word-bytes
515 collect `(ash code ,(* 8 i))))))))
516 `(let ((length (length sequence))
517 (value ,value))
518 (multiple-value-bind (times rem)
519 (truncate length sb!vm:n-word-bytes)
520 (do ((index 0 (1+ index))
521 (end times))
522 ((>= index end)
523 (let ((place (* times sb!vm:n-word-bytes)))
524 (declare (fixnum place))
525 (dotimes (j rem sequence)
526 (declare (index j))
527 (setf (schar sequence (the index (+ place j))) item))))
528 (declare (optimize (speed 3) (safety 0))
529 (type index index))
530 (setf (%vector-raw-bits sequence index) value))))))
532 ;;;; %BYTE-BLT
534 ;;; FIXME: The old CMU CL code used various COPY-TO/FROM-SYSTEM-AREA
535 ;;; stuff (with all the associated bit-index cruft and overflow
536 ;;; issues) even for byte moves. In SBCL, we're converting to byte
537 ;;; moves as problems are discovered with the old code, and this is
538 ;;; currently (ca. sbcl-0.6.12.30) the main interface for code in
539 ;;; SB!KERNEL and SB!SYS (e.g. i/o code). It's not clear that it's the
540 ;;; ideal interface, though, and it probably deserves some thought.
541 (deftransform %byte-blt ((src src-start dst dst-start dst-end)
542 ((or (simple-unboxed-array (*)) system-area-pointer)
543 index
544 (or (simple-unboxed-array (*)) system-area-pointer)
545 index
546 index))
547 ;; FIXME: CMU CL had a hairier implementation of this (back when it
548 ;; was still called (%PRIMITIVE BYTE-BLT). It had the small problem
549 ;; that it didn't work for large (>16M) values of SRC-START or
550 ;; DST-START. However, it might have been more efficient. In
551 ;; particular, I don't really know how much the foreign function
552 ;; call costs us here. My guess is that if the overhead is
553 ;; acceptable for SQRT and COS, it's acceptable here, but this
554 ;; should probably be checked. -- WHN
555 '(flet ((sapify (thing)
556 (etypecase thing
557 (system-area-pointer thing)
558 ;; FIXME: The code here rather relies on the simple
559 ;; unboxed array here having byte-sized entries. That
560 ;; should be asserted explicitly, I just haven't found
561 ;; a concise way of doing it. (It would be nice to
562 ;; declare it in the DEFKNOWN too.)
563 ((simple-unboxed-array (*)) (vector-sap thing)))))
564 (declare (inline sapify))
565 (with-pinned-objects (dst src)
566 (memmove (sap+ (sapify dst) dst-start)
567 (sap+ (sapify src) src-start)
568 (- dst-end dst-start)))
569 (values)))
571 ;;;; transforms for EQL of floating point values
572 #!-float-eql-vops
573 (deftransform eql ((x y) (single-float single-float))
574 '(= (single-float-bits x) (single-float-bits y)))
576 #!-float-eql-vops
577 (deftransform eql ((x y) (double-float double-float))
578 '(and (= (double-float-low-bits x) (double-float-low-bits y))
579 (= (double-float-high-bits x) (double-float-high-bits y))))
582 ;;;; modular functions
584 ;;; FIXME: I think that the :GOODness of a modular function boils down
585 ;;; to whether the normal definition can be used in the middle of a
586 ;;; modular arrangement. LOGAND and LOGIOR can be for all unsigned
587 ;;; modular implementations, I believe, because for all unsigned
588 ;;; arguments of a given size the result of the ordinary definition is
589 ;;; the right one. This should follow through to other logical
590 ;;; functions, such as LOGXOR, should it not? -- CSR, 2007-12-29,
591 ;;; trying to understand a comment he wrote over four years
592 ;;; previously: "FIXME: XOR? ANDC1, ANDC2? -- CSR, 2003-09-16"
593 (define-good-modular-fun logand :untagged nil)
594 (define-good-modular-fun logior :untagged nil)
595 (define-good-modular-fun logxor :untagged nil)
596 (macrolet ((define-good-signed-modular-funs (&rest funs)
597 (let (result)
598 `(progn
599 ,@(dolist (fun funs (nreverse result))
600 (push `(define-good-modular-fun ,fun :untagged t) result)
601 (push `(define-good-modular-fun ,fun :tagged t) result))))))
602 (define-good-signed-modular-funs
603 logand logandc1 logandc2 logeqv logior lognand lognor lognot
604 logorc1 logorc2 logxor))
606 (macrolet
607 ((def (name kind width signedp)
608 (let ((type (ecase signedp
609 ((nil) 'unsigned-byte)
610 ((t) 'signed-byte))))
611 `(progn
612 (defknown ,name (integer (integer 0)) (,type ,width)
613 (foldable flushable movable))
614 (define-modular-fun-optimizer ash ((integer count) ,kind ,signedp :width width)
615 (when (and (<= width ,width)
616 (or (and (constant-lvar-p count)
617 (plusp (lvar-value count)))
618 (csubtypep (lvar-type count)
619 (specifier-type '(and unsigned-byte fixnum)))))
620 (cut-to-width integer ,kind width ,signedp)
621 ',name))
622 (setf (gethash ',name (modular-class-versions (find-modular-class ',kind ',signedp)))
623 `(ash ,',width))))))
624 ;; This should really be dependent on SB!VM:N-WORD-BITS, but since we
625 ;; don't have a true Alpha64 port yet, we'll have to stick to
626 ;; SB!VM:N-MACHINE-WORD-BITS for the time being. --njf, 2004-08-14
627 #!+#.(cl:if (cl:= 32 sb!vm:n-machine-word-bits) '(and) '(or))
628 (progn
629 #!+x86 (def sb!vm::ash-left-smod30 :tagged 30 t)
630 (def sb!vm::ash-left-mod32 :untagged 32 nil))
631 #!+#.(cl:if (cl:= 64 sb!vm:n-machine-word-bits) '(and) '(or))
632 (progn
633 #!+x86-64 (def sb!vm::ash-left-smod61 :tagged 61 t)
634 (def sb!vm::ash-left-mod64 :untagged 64 nil)))
636 ;;;; word-wise logical operations
638 ;;; These transforms assume the presence of modular arithmetic to
639 ;;; generate efficient code.
641 (define-source-transform word-logical-not (x)
642 `(logand (lognot (the sb!vm:word ,x)) #.(1- (ash 1 sb!vm:n-word-bits))))
644 (deftransform word-logical-and ((x y))
645 '(logand x y))
647 (deftransform word-logical-nand ((x y))
648 '(logand (lognand x y) #.(1- (ash 1 sb!vm:n-word-bits))))
650 (deftransform word-logical-or ((x y))
651 '(logior x y))
653 (deftransform word-logical-nor ((x y))
654 '(logand (lognor x y) #.(1- (ash 1 sb!vm:n-word-bits))))
656 (deftransform word-logical-xor ((x y))
657 '(logxor x y))
659 (deftransform word-logical-eqv ((x y))
660 '(logand (logeqv x y) #.(1- (ash 1 sb!vm:n-word-bits))))
662 (deftransform word-logical-orc1 ((x y))
663 '(logand (logorc1 x y) #.(1- (ash 1 sb!vm:n-word-bits))))
665 (deftransform word-logical-orc2 ((x y))
666 '(logand (logorc2 x y) #.(1- (ash 1 sb!vm:n-word-bits))))
668 (deftransform word-logical-andc1 ((x y))
669 '(logand (logandc1 x y) #.(1- (ash 1 sb!vm:n-word-bits))))
671 (deftransform word-logical-andc2 ((x y))
672 '(logand (logandc2 x y) #.(1- (ash 1 sb!vm:n-word-bits))))
675 ;;; There are two different ways the multiplier can be recoded. The
676 ;;; more obvious is to shift X by the correct amount for each bit set
677 ;;; in Y and to sum the results. But if there is a string of bits that
678 ;;; are all set, you can add X shifted by one more then the bit
679 ;;; position of the first set bit and subtract X shifted by the bit
680 ;;; position of the last set bit. We can't use this second method when
681 ;;; the high order bit is bit 31 because shifting by 32 doesn't work
682 ;;; too well.
683 (defun ub32-strength-reduce-constant-multiply (arg num)
684 (declare (type (unsigned-byte 32) num))
685 (let ((adds 0) (shifts 0)
686 (result nil) first-one)
687 (labels ((add (next-factor)
688 (setf result
689 (if result
690 (progn (incf adds) `(+ ,result ,next-factor))
691 next-factor))))
692 (declare (inline add))
693 (dotimes (bitpos 32)
694 (if first-one
695 (when (not (logbitp bitpos num))
696 (add (if (= (1+ first-one) bitpos)
697 ;; There is only a single bit in the string.
698 (progn (incf shifts) `(ash ,arg ,first-one))
699 ;; There are at least two.
700 (progn
701 (incf adds)
702 (incf shifts 2)
703 `(- (ash ,arg ,bitpos)
704 (ash ,arg ,first-one)))))
705 (setf first-one nil))
706 (when (logbitp bitpos num)
707 (setf first-one bitpos))))
708 (when first-one
709 (cond ((= first-one 31))
710 ((= first-one 30) (incf shifts) (add `(ash ,arg 30)))
712 (incf shifts 2)
713 (incf adds)
714 (add `(- (ash ,arg 31)
715 (ash ,arg ,first-one)))))
716 (incf shifts)
717 (add `(ash ,arg 31))))
718 (values (if (plusp adds)
719 `(logand ,result #.(1- (ash 1 32))) ; using modular arithmetic
720 result)
721 adds
722 shifts)))
725 ;;; Transform GET-LISP-OBJ-ADDRESS for constant immediates, since the normal
726 ;;; VOP can't handle them.
728 (deftransform sb!vm::get-lisp-obj-address ((obj) ((constant-arg fixnum)))
729 (ash (lvar-value obj) sb!vm::n-fixnum-tag-bits))
731 (deftransform sb!vm::get-lisp-obj-address ((obj) ((constant-arg character)))
732 (logior sb!vm::character-widetag
733 (ash (char-code (lvar-value obj)) sb!vm::n-widetag-bits)))