1 ;;;; implementation-dependent transforms
3 ;;;; This software is part of the SBCL system. See the README file for
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.
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))
19 (define-source-transform long-float-p (x) `(double-float-p ,x))
21 (define-source-transform compiled-function-p (x)
24 (define-source-transform char-int (x)
27 (deftransform abs ((x) (rational))
28 '(if (< x 0) (- x) x))
30 ;;; The layout is stored in slot 0.
31 (define-source-transform %instance-layout (x)
32 `(truly-the layout (%instance-ref ,x 0)))
33 (define-source-transform %set-instance-layout (x val)
34 `(%instance-set ,x 0 (the layout ,val)))
35 (define-source-transform %funcallable-instance-layout (x)
36 `(truly-the layout (%funcallable-instance-info ,x 0)))
37 (define-source-transform %set-funcallable-instance-layout (x val)
38 `(setf (%funcallable-instance-info ,x 0) (the layout ,val)))
40 ;;;; character support
42 ;;; In our implementation there are really only BASE-CHARs.
44 (define-source-transform characterp (obj)
47 ;;;; simplifying HAIRY-DATA-VECTOR-REF and HAIRY-DATA-VECTOR-SET
49 (deftransform hairy-data-vector-ref ((string index) (simple-string t))
50 (let ((ctype (lvar-type string)))
51 (if (array-type-p ctype)
52 ;; the other transform will kick in, so that's OK
53 (give-up-ir1-transform)
55 ((simple-array character (*)) (data-vector-ref string index))
57 ((simple-array base-char (*)) (data-vector-ref string index))
58 ((simple-array nil (*)) (data-vector-ref string index))))))
60 (deftransform hairy-data-vector-ref ((array index) (array t) *)
61 "avoid runtime dispatch on array element type"
62 (let ((element-ctype (extract-upgraded-element-type array))
63 (declared-element-ctype (extract-declared-element-type array)))
64 (declare (type ctype element-ctype))
65 (when (eq *wild-type* element-ctype)
66 (give-up-ir1-transform
67 "Upgraded element type of array is not known at compile time."))
68 ;; (The expansion here is basically a degenerate case of
69 ;; WITH-ARRAY-DATA. Since WITH-ARRAY-DATA is implemented as a
70 ;; macro, and macros aren't expanded in transform output, we have
71 ;; to hand-expand it ourselves.)
72 (let ((element-type-specifier (type-specifier element-ctype)))
73 `(multiple-value-bind (array index)
74 (%data-vector-and-index array index)
75 (declare (type (simple-array ,element-type-specifier 1) array))
76 ,(let ((bare-form '(data-vector-ref array index)))
77 (if (type= element-ctype declared-element-ctype)
79 `(the ,(type-specifier declared-element-ctype)
82 ;;; Transform multi-dimensional array to one dimensional data vector
84 (deftransform data-vector-ref ((array index)
86 (let ((array-type (lvar-type array)))
87 (unless (array-type-p array-type)
88 (give-up-ir1-transform))
89 (let ((dims (array-type-dimensions array-type)))
90 (when (or (atom dims) (= (length dims) 1))
91 (give-up-ir1-transform))
92 (let ((el-type (array-type-specialized-element-type array-type))
93 (total-size (if (member '* dims)
96 `(data-vector-ref (truly-the (simple-array ,(type-specifier el-type)
98 (%array-data-vector array))
101 (deftransform hairy-data-vector-set ((string index new-value)
103 (let ((ctype (lvar-type string)))
104 (if (array-type-p ctype)
105 ;; the other transform will kick in, so that's OK
106 (give-up-ir1-transform)
108 ((simple-array character (*))
109 (data-vector-set string index new-value))
111 ((simple-array base-char (*))
112 (data-vector-set string index new-value))
113 ((simple-array nil (*))
114 (data-vector-set string index new-value))))))
116 (deftransform hairy-data-vector-set ((array index new-value)
119 "avoid runtime dispatch on array element type"
120 (let ((element-ctype (extract-upgraded-element-type array))
121 (declared-element-ctype (extract-declared-element-type array)))
122 (declare (type ctype element-ctype))
123 (when (eq *wild-type* element-ctype)
124 (give-up-ir1-transform
125 "Upgraded element type of array is not known at compile time."))
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 (type ,element-type-specifier new-value))
131 ,(if (type= element-ctype declared-element-ctype)
132 '(data-vector-set array index new-value)
133 `(truly-the ,(type-specifier declared-element-ctype)
134 (data-vector-set array index
135 (the ,(type-specifier declared-element-ctype)
138 (deftransform data-vector-set ((array index new-value)
140 (let ((array-type (lvar-type array)))
141 (unless (array-type-p array-type)
142 (give-up-ir1-transform))
143 (let ((dims (array-type-dimensions array-type)))
144 (when (or (atom dims) (= (length dims) 1))
145 (give-up-ir1-transform))
146 (let ((el-type (array-type-specialized-element-type array-type))
147 (total-size (if (member '* dims)
150 `(data-vector-set (truly-the (simple-array ,(type-specifier el-type)
152 (%array-data-vector array))
156 (defoptimizer (%data-vector-and-index derive-type) ((array index))
157 (let ((atype (lvar-type array)))
158 (when (array-type-p atype)
159 (values-specifier-type
160 `(values (simple-array ,(type-specifier
161 (array-type-specialized-element-type atype))
165 (deftransform %data-vector-and-index ((%array %index)
168 ;; KLUDGE: why the percent signs? Well, ARRAY and INDEX are
169 ;; respectively exported from the CL and SB!INT packages, which
170 ;; means that they're visible to all sorts of things. If the
171 ;; compiler can prove that the call to ARRAY-HEADER-P, below, either
172 ;; returns T or NIL, it will delete the irrelevant branch. However,
173 ;; user code might have got here with a variable named CL:ARRAY, and
174 ;; quite often compiler code with a variable named SB!INT:INDEX, so
175 ;; this can generate code deletion notes for innocuous user code:
176 ;; (DEFUN F (ARRAY I) (DECLARE (SIMPLE-VECTOR ARRAY)) (AREF ARRAY I))
177 ;; -- CSR, 2003-04-01
179 ;; We do this solely for the -OR-GIVE-UP side effect, since we want
180 ;; to know that the type can be figured out in the end before we
181 ;; proceed, but we don't care yet what the type will turn out to be.
182 (upgraded-element-type-specifier-or-give-up %array)
184 '(if (array-header-p %array)
185 (values (%array-data-vector %array) %index)
186 (values %array %index)))
188 ;;; transforms for getting at simple arrays of (UNSIGNED-BYTE N) when (< N 8)
190 ;;; FIXME: In CMU CL, these were commented out with #+NIL. Why? Should
191 ;;; we fix them or should we delete them? (Perhaps these definitions
192 ;;; predate the various DATA-VECTOR-REF-FOO VOPs which have
193 ;;; (:TRANSLATE DATA-VECTOR-REF), and are redundant now?)
197 (let ((elements-per-word (truncate sb!vm:n-word-bits bits)))
199 (deftransform data-vector-ref ((vector index)
201 `(multiple-value-bind (word bit)
202 (floor index ,',elements-per-word)
203 (ldb ,(ecase sb!vm:target-byte-order
204 (:little-endian '(byte ,bits (* bit ,bits)))
205 (:big-endian '(byte ,bits (- sb!vm:n-word-bits
206 (* (1+ bit) ,bits)))))
207 (%raw-bits vector (+ word sb!vm:vector-data-offset)))))
208 (deftransform data-vector-set ((vector index new-value)
210 `(multiple-value-bind (word bit)
211 (floor index ,',elements-per-word)
212 (setf (ldb ,(ecase sb!vm:target-byte-order
213 (:little-endian '(byte ,bits (* bit ,bits)))
215 '(byte ,bits (- sb!vm:n-word-bits
216 (* (1+ bit) ,bits)))))
217 (%raw-bits vector (+ word sb!vm:vector-data-offset)))
219 (frob simple-bit-vector 1)
220 (frob (simple-array (unsigned-byte 2) (*)) 2)
221 (frob (simple-array (unsigned-byte 4) (*)) 4))
223 ;;;; BIT-VECTOR hackery
225 ;;; SIMPLE-BIT-VECTOR bit-array operations are transformed to a word
226 ;;; loop that does 32 bits at a time.
228 ;;; FIXME: This is a lot of repeatedly macroexpanded code. It should
229 ;;; be a function call instead.
230 (macrolet ((def (bitfun wordfun)
231 `(deftransform ,bitfun ((bit-array-1 bit-array-2 result-bit-array)
236 :node node :policy (>= speed space))
238 ,@(unless (policy node (zerop safety))
239 '((unless (= (length bit-array-1)
241 (length result-bit-array))
242 (error "Argument and/or result bit arrays are not the same length:~
247 (let ((length (length result-bit-array)))
249 ;; We avoid doing anything to 0-length
250 ;; bit-vectors, or rather, the memory that
251 ;; follows them. Other divisible-by-32 cases
252 ;; are handled by the (1- length), below.
255 (do ((index sb!vm:vector-data-offset (1+ index))
256 (end-1 (+ sb!vm:vector-data-offset
257 ;; bit-vectors of length 1-32
258 ;; need precisely one (SETF
259 ;; %RAW-BITS), done here in the
260 ;; epilogue. - CSR, 2002-04-24
261 (truncate (truly-the index (1- length))
262 sb!vm:n-word-bits))))
264 (setf (%raw-bits result-bit-array index)
265 (,',wordfun (%raw-bits bit-array-1 index)
266 (%raw-bits bit-array-2 index)))
268 (declare (optimize (speed 3) (safety 0))
269 (type index index end-1))
270 (setf (%raw-bits result-bit-array index)
271 (,',wordfun (%raw-bits bit-array-1 index)
272 (%raw-bits bit-array-2 index))))))))))
273 (def bit-and word-logical-and)
274 (def bit-ior word-logical-or)
275 (def bit-xor word-logical-xor)
276 (def bit-eqv word-logical-eqv)
277 (def bit-nand word-logical-nand)
278 (def bit-nor word-logical-nor)
279 (def bit-andc1 word-logical-andc1)
280 (def bit-andc2 word-logical-andc2)
281 (def bit-orc1 word-logical-orc1)
282 (def bit-orc2 word-logical-orc2))
284 (deftransform bit-not
285 ((bit-array result-bit-array)
286 (simple-bit-vector simple-bit-vector) *
287 :node node :policy (>= speed space))
289 ,@(unless (policy node (zerop safety))
290 '((unless (= (length bit-array)
291 (length result-bit-array))
292 (error "Argument and result bit arrays are not the same length:~
294 bit-array result-bit-array))))
295 (let ((length (length result-bit-array)))
297 ;; We avoid doing anything to 0-length bit-vectors, or rather,
298 ;; the memory that follows them. Other divisible-by
299 ;; n-word-bits cases are handled by the (1- length), below.
302 (do ((index sb!vm:vector-data-offset (1+ index))
303 (end-1 (+ sb!vm:vector-data-offset
304 ;; bit-vectors of length 1 to n-word-bits need
305 ;; precisely one (SETF %RAW-BITS), done here in
306 ;; the epilogue. - CSR, 2002-04-24
307 (truncate (truly-the index (1- length))
308 sb!vm:n-word-bits))))
310 (setf (%raw-bits result-bit-array index)
311 (word-logical-not (%raw-bits bit-array index)))
313 (declare (optimize (speed 3) (safety 0))
314 (type index index end-1))
315 (setf (%raw-bits result-bit-array index)
316 (word-logical-not (%raw-bits bit-array index))))))))
318 (deftransform bit-vector-= ((x y) (simple-bit-vector simple-bit-vector))
319 `(and (= (length x) (length y))
320 (let ((length (length x)))
322 (do* ((i sb!vm:vector-data-offset (+ i 1))
323 (end-1 (+ sb!vm:vector-data-offset
324 (floor (1- length) sb!vm:n-word-bits))))
326 (let* ((extra (1+ (mod (1- length) sb!vm:n-word-bits)))
327 (mask (ash #.(1- (ash 1 sb!vm:n-word-bits))
328 (- extra sb!vm:n-word-bits)))
332 ,(ecase sb!c:*backend-byte-order*
335 '(- sb!vm:n-word-bits extra))))
340 ,(ecase sb!c:*backend-byte-order*
343 '(- sb!vm:n-word-bits extra))))
345 (declare (type (integer 1 #.sb!vm:n-word-bits) extra)
346 (type sb!vm:word mask numx numy))
348 (declare (type index i end-1))
349 (let ((numx (%raw-bits x i))
350 (numy (%raw-bits y i)))
351 (declare (type sb!vm:word numx numy))
352 (unless (= numx numy)
355 (deftransform count ((item sequence) (bit simple-bit-vector) *
356 :policy (>= speed space))
357 `(let ((length (length sequence)))
360 (do ((index sb!vm:vector-data-offset (1+ index))
362 (end-1 (+ sb!vm:vector-data-offset
363 (truncate (truly-the index (1- length))
364 sb!vm:n-word-bits))))
366 (let* ((extra (1+ (mod (1- length) sb!vm:n-word-bits)))
367 (mask (ash #.(1- (ash 1 sb!vm:n-word-bits))
368 (- extra sb!vm:n-word-bits)))
369 (bits (logand (ash mask
370 ,(ecase sb!c:*backend-byte-order*
373 '(- sb!vm:n-word-bits extra))))
374 (%raw-bits sequence index))))
375 (declare (type (integer 1 #.sb!vm:n-word-bits) extra))
376 (declare (type sb!vm:word mask bits))
377 ;; could consider LOGNOT for the zero case instead of
378 ;; doing the subtraction...
379 (incf count ,(if (constant-lvar-p item)
380 (if (zerop (lvar-value item))
381 '(- extra (logcount bits))
384 (- extra (logcount bits))
386 (declare (type index index count end-1)
387 (optimize (speed 3) (safety 0)))
388 (incf count ,(if (constant-lvar-p item)
389 (if (zerop (lvar-value item))
390 '(- sb!vm:n-word-bits (logcount (%raw-bits sequence index)))
391 '(logcount (%raw-bits sequence index)))
393 (- sb!vm:n-word-bits (logcount (%raw-bits sequence index)))
394 (logcount (%raw-bits sequence index)))))))))
396 (deftransform fill ((sequence item) (simple-bit-vector bit) *
397 :policy (>= speed space))
398 (let ((value (if (constant-lvar-p item)
399 (if (= (lvar-value item) 0)
401 #.(1- (ash 1 sb!vm:n-word-bits)))
402 `(if (= item 0) 0 #.(1- (ash 1 sb!vm:n-word-bits))))))
403 `(let ((length (length sequence))
407 (do ((index sb!vm:vector-data-offset (1+ index))
408 (end-1 (+ sb!vm:vector-data-offset
409 ;; bit-vectors of length 1 to n-word-bits need
410 ;; precisely one (SETF %RAW-BITS), done here
411 ;; in the epilogue. - CSR, 2002-04-24
412 (truncate (truly-the index (1- length))
413 sb!vm:n-word-bits))))
415 (setf (%raw-bits sequence index) value)
417 (declare (optimize (speed 3) (safety 0))
418 (type index index end-1))
419 (setf (%raw-bits sequence index) value))))))
421 (deftransform fill ((sequence item) (simple-base-string base-char) *
422 :policy (>= speed space))
423 (let ((value (if (constant-lvar-p item)
424 (let* ((char (lvar-value item))
425 (code (sb!xc:char-code char))
427 (dotimes (i sb!vm:n-word-bytes accum)
428 (setf accum (logior accum (ash code (* 8 i))))))
429 `(let ((code (sb!xc:char-code item)))
430 (logior ,@(loop for i from 0 below sb!vm:n-word-bytes
431 collect `(ash code ,(* 8 i))))))))
432 `(let ((length (length sequence))
434 (multiple-value-bind (times rem)
435 (truncate length sb!vm:n-word-bytes)
436 (do ((index sb!vm:vector-data-offset (1+ index))
437 (end (+ times sb!vm:vector-data-offset)))
439 (let ((place (* times sb!vm:n-word-bytes)))
440 (declare (fixnum place))
441 (dotimes (j rem sequence)
443 (setf (schar sequence (the index (+ place j))) item))))
444 (declare (optimize (speed 3) (safety 0))
446 (setf (%raw-bits sequence index) value))))))
450 ;;; FIXME: The old CMU CL code used various COPY-TO/FROM-SYSTEM-AREA
451 ;;; stuff (with all the associated bit-index cruft and overflow
452 ;;; issues) even for byte moves. In SBCL, we're converting to byte
453 ;;; moves as problems are discovered with the old code, and this is
454 ;;; currently (ca. sbcl-0.6.12.30) the main interface for code in
455 ;;; SB!KERNEL and SB!SYS (e.g. i/o code). It's not clear that it's the
456 ;;; ideal interface, though, and it probably deserves some thought.
457 (deftransform %byte-blt ((src src-start dst dst-start dst-end)
458 ((or (simple-unboxed-array (*)) system-area-pointer)
460 (or (simple-unboxed-array (*)) system-area-pointer)
463 ;; FIXME: CMU CL had a hairier implementation of this (back when it
464 ;; was still called (%PRIMITIVE BYTE-BLT). It had the small problem
465 ;; that it didn't work for large (>16M) values of SRC-START or
466 ;; DST-START. However, it might have been more efficient. In
467 ;; particular, I don't really know how much the foreign function
468 ;; call costs us here. My guess is that if the overhead is
469 ;; acceptable for SQRT and COS, it's acceptable here, but this
470 ;; should probably be checked. -- WHN
471 '(flet ((sapify (thing)
473 (system-area-pointer thing)
474 ;; FIXME: The code here rather relies on the simple
475 ;; unboxed array here having byte-sized entries. That
476 ;; should be asserted explicitly, I just haven't found
477 ;; a concise way of doing it. (It would be nice to
478 ;; declare it in the DEFKNOWN too.)
479 ((simple-unboxed-array (*)) (vector-sap thing)))))
480 (declare (inline sapify))
482 (memmove (sap+ (sapify dst) dst-start)
483 (sap+ (sapify src) src-start)
484 (- dst-end dst-start)))
487 ;;;; transforms for EQL of floating point values
489 (deftransform eql ((x y) (single-float single-float))
490 '(= (single-float-bits x) (single-float-bits y)))
492 (deftransform eql ((x y) (double-float double-float))
493 '(and (= (double-float-low-bits x) (double-float-low-bits y))
494 (= (double-float-high-bits x) (double-float-high-bits y))))
497 ;;;; modular functions
498 (define-good-modular-fun logand :unsigned)
499 (define-good-modular-fun logior :unsigned)
500 ;;; FIXME: XOR? ANDC1, ANDC2? -- CSR, 2003-09-16
503 ((def (name class width)
504 (let ((type (ecase class
505 (:unsigned 'unsigned-byte)
506 (:signed 'signed-byte))))
508 (defknown ,name (integer (integer 0)) (,type ,width)
509 (foldable flushable movable))
510 (define-modular-fun-optimizer ash ((integer count) ,class :width width)
511 (when (and (<= width ,width)
512 (or (and (constant-lvar-p count)
513 (plusp (lvar-value count)))
514 (csubtypep (lvar-type count)
515 (specifier-type '(and unsigned-byte fixnum)))))
516 (cut-to-width integer ,class width)
518 (setf (gethash ',name (modular-class-versions (find-modular-class ',class)))
520 ;; This should really be dependent on SB!VM:N-WORD-BITS, but since we
521 ;; don't have a true Alpha64 port yet, we'll have to stick to
522 ;; SB!VM:N-MACHINE-WORD-BITS for the time being. --njf, 2004-08-14
523 #!+#.(cl:if (cl:= 32 sb!vm:n-machine-word-bits) '(and) '(or))
525 #!+x86 (def sb!vm::ash-left-smod30 :signed 30)
526 (def sb!vm::ash-left-mod32 :unsigned 32))
527 #!+#.(cl:if (cl:= 64 sb!vm:n-machine-word-bits) '(and) '(or))
529 #!+x86-64 (def sb!vm::ash-left-smod61 :signed 61)
530 (def sb!vm::ash-left-mod64 :unsigned 64)))
533 ;;;; word-wise logical operations
535 ;;; These transforms assume the presence of modular arithmetic to
536 ;;; generate efficient code.
538 (define-source-transform word-logical-not (x)
539 `(logand (lognot (the sb!vm:word ,x)) #.(1- (ash 1 sb!vm:n-word-bits))))
541 (deftransform word-logical-and ((x y))
544 (deftransform word-logical-nand ((x y))
545 '(logand (lognand x y) #.(1- (ash 1 sb!vm:n-word-bits))))
547 (deftransform word-logical-or ((x y))
550 (deftransform word-logical-nor ((x y))
551 '(logand (lognor x y) #.(1- (ash 1 sb!vm:n-word-bits))))
553 (deftransform word-logical-xor ((x y))
556 (deftransform word-logical-eqv ((x y))
557 '(logand (logeqv x y) #.(1- (ash 1 sb!vm:n-word-bits))))
559 (deftransform word-logical-orc1 ((x y))
560 '(logand (logorc1 x y) #.(1- (ash 1 sb!vm:n-word-bits))))
562 (deftransform word-logical-orc2 ((x y))
563 '(logand (logorc2 x y) #.(1- (ash 1 sb!vm:n-word-bits))))
565 (deftransform word-logical-andc1 ((x y))
566 '(logand (logandc1 x y) #.(1- (ash 1 sb!vm:n-word-bits))))
568 (deftransform word-logical-andc2 ((x y))
569 '(logand (logandc2 x y) #.(1- (ash 1 sb!vm:n-word-bits))))
572 ;;; There are two different ways the multiplier can be recoded. The
573 ;;; more obvious is to shift X by the correct amount for each bit set
574 ;;; in Y and to sum the results. But if there is a string of bits that
575 ;;; are all set, you can add X shifted by one more then the bit
576 ;;; position of the first set bit and subtract X shifted by the bit
577 ;;; position of the last set bit. We can't use this second method when
578 ;;; the high order bit is bit 31 because shifting by 32 doesn't work
580 (defun ub32-strength-reduce-constant-multiply (arg num)
581 (declare (type (unsigned-byte 32) num))
582 (let ((adds 0) (shifts 0)
583 (result nil) first-one)
584 (labels ((add (next-factor)
587 (progn (incf adds) `(+ ,result ,next-factor))
589 (declare (inline add))
592 (when (not (logbitp bitpos num))
593 (add (if (= (1+ first-one) bitpos)
594 ;; There is only a single bit in the string.
595 (progn (incf shifts) `(ash ,arg ,first-one))
596 ;; There are at least two.
600 `(- (ash ,arg ,bitpos)
601 (ash ,arg ,first-one)))))
602 (setf first-one nil))
603 (when (logbitp bitpos num)
604 (setf first-one bitpos))))
606 (cond ((= first-one 31))
607 ((= first-one 30) (incf shifts) (add `(ash ,arg 30)))
611 (add `(- (ash ,arg 31)
612 (ash ,arg ,first-one)))))
614 (add `(ash ,arg 31))))
615 (values (if (plusp adds)
616 `(logand ,result #.(1- (ash 1 32))) ; using modular arithmetic