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)))
36 ;;;; character support
38 ;;; In our implementation there are really only BASE-CHARs.
40 (define-source-transform characterp (obj)
43 ;;;; simplifying HAIRY-DATA-VECTOR-REF and HAIRY-DATA-VECTOR-SET
45 (deftransform hairy-data-vector-ref ((string index) (simple-string t))
46 (let ((ctype (lvar-type string)))
47 (if (array-type-p ctype)
48 ;; the other transform will kick in, so that's OK
49 (give-up-ir1-transform)
51 ((simple-array character (*)) (data-vector-ref string index))
52 ((simple-array nil (*)) (data-vector-ref string index))))))
54 (deftransform hairy-data-vector-ref ((array index) (array t) *)
55 "avoid runtime dispatch on array element type"
56 (let ((element-ctype (extract-upgraded-element-type array))
57 (declared-element-ctype (extract-declared-element-type array)))
58 (declare (type ctype element-ctype))
59 (when (eq *wild-type* element-ctype)
60 (give-up-ir1-transform
61 "Upgraded element type of array is not known at compile time."))
62 ;; (The expansion here is basically a degenerate case of
63 ;; WITH-ARRAY-DATA. Since WITH-ARRAY-DATA is implemented as a
64 ;; macro, and macros aren't expanded in transform output, we have
65 ;; to hand-expand it ourselves.)
66 (let ((element-type-specifier (type-specifier element-ctype)))
67 `(multiple-value-bind (array index)
68 (%data-vector-and-index array index)
69 (declare (type (simple-array ,element-type-specifier 1) array))
70 ,(let ((bare-form '(data-vector-ref array index)))
71 (if (type= element-ctype declared-element-ctype)
73 `(the ,(type-specifier declared-element-ctype)
76 (deftransform data-vector-ref ((array index)
78 (let ((array-type (lvar-type array)))
79 (unless (array-type-p array-type)
80 (give-up-ir1-transform))
81 (let ((dims (array-type-dimensions array-type)))
82 (when (or (atom dims) (= (length dims) 1))
83 (give-up-ir1-transform))
84 (let ((el-type (array-type-specialized-element-type array-type))
85 (total-size (if (member '* dims)
88 `(data-vector-ref (truly-the (simple-array ,(type-specifier el-type)
90 (%array-data-vector array))
93 (deftransform hairy-data-vector-set ((string index new-value)
95 (let ((ctype (lvar-type string)))
96 (if (array-type-p ctype)
97 ;; the other transform will kick in, so that's OK
98 (give-up-ir1-transform)
100 ((simple-array character (*))
101 (data-vector-set string index new-value))
102 ((simple-array nil (*))
103 (data-vector-set string index new-value))))))
105 (deftransform hairy-data-vector-set ((array index new-value)
108 "avoid runtime dispatch on array element type"
109 (let ((element-ctype (extract-upgraded-element-type array))
110 (declared-element-ctype (extract-declared-element-type array)))
111 (declare (type ctype element-ctype))
112 (when (eq *wild-type* element-ctype)
113 (give-up-ir1-transform
114 "Upgraded element type of array is not known at compile time."))
115 (let ((element-type-specifier (type-specifier element-ctype)))
116 `(multiple-value-bind (array index)
117 (%data-vector-and-index array index)
118 (declare (type (simple-array ,element-type-specifier 1) array)
119 (type ,element-type-specifier new-value))
120 ,(if (type= element-ctype declared-element-ctype)
121 '(data-vector-set array index new-value)
122 `(truly-the ,(type-specifier declared-element-ctype)
123 (data-vector-set array index
124 (the ,(type-specifier declared-element-ctype)
127 (deftransform data-vector-set ((array index new-value)
129 (let ((array-type (lvar-type array)))
130 (unless (array-type-p array-type)
131 (give-up-ir1-transform))
132 (let ((dims (array-type-dimensions array-type)))
133 (when (or (atom dims) (= (length dims) 1))
134 (give-up-ir1-transform))
135 (let ((el-type (array-type-specialized-element-type array-type))
136 (total-size (if (member '* dims)
139 `(data-vector-set (truly-the (simple-array ,(type-specifier el-type)
141 (%array-data-vector array))
145 (defoptimizer (%data-vector-and-index derive-type) ((array index))
146 (let ((atype (lvar-type array)))
147 (when (array-type-p atype)
148 (values-specifier-type
149 `(values (simple-array ,(type-specifier
150 (array-type-specialized-element-type atype))
154 (deftransform %data-vector-and-index ((%array %index)
157 ;; KLUDGE: why the percent signs? Well, ARRAY and INDEX are
158 ;; respectively exported from the CL and SB!INT packages, which
159 ;; means that they're visible to all sorts of things. If the
160 ;; compiler can prove that the call to ARRAY-HEADER-P, below, either
161 ;; returns T or NIL, it will delete the irrelevant branch. However,
162 ;; user code might have got here with a variable named CL:ARRAY, and
163 ;; quite often compiler code with a variable named SB!INT:INDEX, so
164 ;; this can generate code deletion notes for innocuous user code:
165 ;; (DEFUN F (ARRAY I) (DECLARE (SIMPLE-VECTOR ARRAY)) (AREF ARRAY I))
166 ;; -- CSR, 2003-04-01
168 ;; We do this solely for the -OR-GIVE-UP side effect, since we want
169 ;; to know that the type can be figured out in the end before we
170 ;; proceed, but we don't care yet what the type will turn out to be.
171 (upgraded-element-type-specifier-or-give-up %array)
173 '(if (array-header-p %array)
174 (values (%array-data-vector %array) %index)
175 (values %array %index)))
177 ;;; transforms for getting at simple arrays of (UNSIGNED-BYTE N) when (< N 8)
179 ;;; FIXME: In CMU CL, these were commented out with #+NIL. Why? Should
180 ;;; we fix them or should we delete them? (Perhaps these definitions
181 ;;; predate the various DATA-VECTOR-REF-FOO VOPs which have
182 ;;; (:TRANSLATE DATA-VECTOR-REF), and are redundant now?)
186 (let ((elements-per-word (truncate sb!vm:n-word-bits bits)))
188 (deftransform data-vector-ref ((vector index)
190 `(multiple-value-bind (word bit)
191 (floor index ,',elements-per-word)
192 (ldb ,(ecase sb!vm:target-byte-order
193 (:little-endian '(byte ,bits (* bit ,bits)))
194 (:big-endian '(byte ,bits (- sb!vm:n-word-bits
195 (* (1+ bit) ,bits)))))
196 (%raw-bits vector (+ word sb!vm:vector-data-offset)))))
197 (deftransform data-vector-set ((vector index new-value)
199 `(multiple-value-bind (word bit)
200 (floor index ,',elements-per-word)
201 (setf (ldb ,(ecase sb!vm:target-byte-order
202 (:little-endian '(byte ,bits (* bit ,bits)))
204 '(byte ,bits (- sb!vm:n-word-bits
205 (* (1+ bit) ,bits)))))
206 (%raw-bits vector (+ word sb!vm:vector-data-offset)))
208 (frob simple-bit-vector 1)
209 (frob (simple-array (unsigned-byte 2) (*)) 2)
210 (frob (simple-array (unsigned-byte 4) (*)) 4))
212 ;;;; BIT-VECTOR hackery
214 ;;; SIMPLE-BIT-VECTOR bit-array operations are transformed to a word
215 ;;; loop that does 32 bits at a time.
217 ;;; FIXME: This is a lot of repeatedly macroexpanded code. It should
218 ;;; be a function call instead.
219 (macrolet ((def (bitfun wordfun)
220 `(deftransform ,bitfun ((bit-array-1 bit-array-2 result-bit-array)
225 :node node :policy (>= speed space))
227 ,@(unless (policy node (zerop safety))
228 '((unless (= (length bit-array-1)
230 (length result-bit-array))
231 (error "Argument and/or result bit arrays are not the same length:~
236 (let ((length (length result-bit-array)))
238 ;; We avoid doing anything to 0-length
239 ;; bit-vectors, or rather, the memory that
240 ;; follows them. Other divisible-by-32 cases
241 ;; are handled by the (1- length), below.
244 (do ((index sb!vm:vector-data-offset (1+ index))
245 (end-1 (+ sb!vm:vector-data-offset
246 ;; bit-vectors of length 1-32
247 ;; need precisely one (SETF
248 ;; %RAW-BITS), done here in the
249 ;; epilogue. - CSR, 2002-04-24
250 (truncate (truly-the index (1- length))
251 sb!vm:n-word-bits))))
253 (setf (%raw-bits result-bit-array index)
254 (,',wordfun (%raw-bits bit-array-1 index)
255 (%raw-bits bit-array-2 index)))
257 (declare (optimize (speed 3) (safety 0))
258 (type index index end-1))
259 (setf (%raw-bits result-bit-array index)
260 (,',wordfun (%raw-bits bit-array-1 index)
261 (%raw-bits bit-array-2 index))))))))))
262 (def bit-and word-logical-and)
263 (def bit-ior word-logical-or)
264 (def bit-xor word-logical-xor)
265 (def bit-eqv word-logical-eqv)
266 (def bit-nand word-logical-nand)
267 (def bit-nor word-logical-nor)
268 (def bit-andc1 word-logical-andc1)
269 (def bit-andc2 word-logical-andc2)
270 (def bit-orc1 word-logical-orc1)
271 (def bit-orc2 word-logical-orc2))
273 (deftransform bit-not
274 ((bit-array result-bit-array)
275 (simple-bit-vector simple-bit-vector) *
276 :node node :policy (>= speed space))
278 ,@(unless (policy node (zerop safety))
279 '((unless (= (length bit-array)
280 (length result-bit-array))
281 (error "Argument and result bit arrays are not the same length:~
283 bit-array result-bit-array))))
284 (let ((length (length result-bit-array)))
286 ;; We avoid doing anything to 0-length bit-vectors, or rather,
287 ;; the memory that follows them. Other divisible-by
288 ;; n-word-bits cases are handled by the (1- length), below.
291 (do ((index sb!vm:vector-data-offset (1+ index))
292 (end-1 (+ sb!vm:vector-data-offset
293 ;; bit-vectors of length 1 to n-word-bits need
294 ;; precisely one (SETF %RAW-BITS), done here in
295 ;; the epilogue. - CSR, 2002-04-24
296 (truncate (truly-the index (1- length))
297 sb!vm:n-word-bits))))
299 (setf (%raw-bits result-bit-array index)
300 (word-logical-not (%raw-bits bit-array index)))
302 (declare (optimize (speed 3) (safety 0))
303 (type index index end-1))
304 (setf (%raw-bits result-bit-array index)
305 (word-logical-not (%raw-bits bit-array index))))))))
307 (deftransform bit-vector-= ((x y) (simple-bit-vector simple-bit-vector))
308 `(and (= (length x) (length y))
309 (let ((length (length x)))
311 (do* ((i sb!vm:vector-data-offset (+ i 1))
312 (end-1 (+ sb!vm:vector-data-offset
313 (floor (1- length) sb!vm:n-word-bits))))
315 (let* ((extra (mod length sb!vm:n-word-bits))
316 (mask (1- (ash 1 extra)))
320 ,(ecase sb!c:*backend-byte-order*
323 '(- sb!vm:n-word-bits extra))))
328 ,(ecase sb!c:*backend-byte-order*
331 '(- sb!vm:n-word-bits extra))))
333 (declare (type (mod #.sb!vm:n-word-bits)
335 (type sb!vm:word mask numx numy))
337 (declare (type index i end-1))
338 (let ((numx (%raw-bits x i))
339 (numy (%raw-bits y i)))
340 (declare (type sb!vm:word numx numy))
341 (unless (= numx numy)
344 (deftransform count ((item sequence) (bit simple-bit-vector) *
345 :policy (>= speed space))
346 `(let ((length (length sequence)))
349 (do ((index sb!vm:vector-data-offset (1+ index))
351 (end-1 (+ sb!vm:vector-data-offset
352 (truncate (truly-the index (1- length))
353 sb!vm:n-word-bits))))
355 (let* ((extra (mod length sb!vm:n-word-bits))
356 (mask (1- (ash 1 extra)))
357 (bits (logand (ash mask
358 ,(ecase sb!c:*backend-byte-order*
361 '(- sb!vm:n-word-bits extra))))
362 (%raw-bits sequence index))))
363 (declare (type (mod #.sb!vm:n-word-bits) extra))
364 (declare (type sb!vm:word mask bits))
365 ;; could consider LOGNOT for the zero case instead of
366 ;; doing the subtraction...
367 (incf count ,(if (constant-lvar-p item)
368 (if (zerop (lvar-value item))
369 '(- extra (logcount bits))
372 (- extra (logcount bits))
374 (declare (type index index count end-1)
375 (optimize (speed 3) (safety 0)))
376 (incf count ,(if (constant-lvar-p item)
377 (if (zerop (lvar-value item))
378 '(- sb!vm:n-word-bits (logcount (%raw-bits sequence index)))
379 '(logcount (%raw-bits sequence index)))
381 (- sb!vm:n-word-bits (logcount (%raw-bits sequence index)))
382 (logcount (%raw-bits sequence index)))))))))
384 (deftransform fill ((sequence item) (simple-bit-vector bit) *
385 :policy (>= speed space))
386 (let ((value (if (constant-lvar-p item)
387 (if (= (lvar-value item) 0)
389 #.(1- (ash 1 sb!vm:n-word-bits)))
390 `(if (= item 0) 0 #.(1- (ash 1 sb!vm:n-word-bits))))))
391 `(let ((length (length sequence))
395 (do ((index sb!vm:vector-data-offset (1+ index))
396 (end-1 (+ sb!vm:vector-data-offset
397 ;; bit-vectors of length 1 to n-word-bits need
398 ;; precisely one (SETF %RAW-BITS), done here
399 ;; in the epilogue. - CSR, 2002-04-24
400 (truncate (truly-the index (1- length))
401 sb!vm:n-word-bits))))
403 (setf (%raw-bits sequence index) value)
405 (declare (optimize (speed 3) (safety 0))
406 (type index index end-1))
407 (setf (%raw-bits sequence index) value))))))
409 (deftransform fill ((sequence item) (simple-base-string base-char) *
410 :policy (>= speed space))
411 (let ((value (if (constant-lvar-p item)
412 (let* ((char (lvar-value item))
413 (code (sb!xc:char-code char))
415 (dotimes (i sb!vm:n-word-bytes accum)
416 (setf accum (logior accum (ash code (* 8 i))))))
417 `(let ((code (sb!xc:char-code item)))
418 (logior ,@(loop for i from 0 below sb!vm:n-word-bytes
419 collect `(ash code ,(* 8 i))))))))
420 `(let ((length (length sequence))
422 (multiple-value-bind (times rem)
423 (truncate length sb!vm:n-word-bytes)
424 (do ((index sb!vm:vector-data-offset (1+ index))
425 (end (+ times sb!vm:vector-data-offset)))
427 (let ((place (* times sb!vm:n-word-bytes)))
428 (declare (fixnum place))
429 (dotimes (j rem sequence)
431 (setf (schar sequence (the index (+ place j))) item))))
432 (declare (optimize (speed 3) (safety 0))
434 (setf (%raw-bits sequence index) value))))))
438 ;;; FIXME: The old CMU CL code used various COPY-TO/FROM-SYSTEM-AREA
439 ;;; stuff (with all the associated bit-index cruft and overflow
440 ;;; issues) even for byte moves. In SBCL, we're converting to byte
441 ;;; moves as problems are discovered with the old code, and this is
442 ;;; currently (ca. sbcl-0.6.12.30) the main interface for code in
443 ;;; SB!KERNEL and SB!SYS (e.g. i/o code). It's not clear that it's the
444 ;;; ideal interface, though, and it probably deserves some thought.
445 (deftransform %byte-blt ((src src-start dst dst-start dst-end)
446 ((or (simple-unboxed-array (*)) system-area-pointer)
448 (or (simple-unboxed-array (*)) system-area-pointer)
451 ;; FIXME: CMU CL had a hairier implementation of this (back when it
452 ;; was still called (%PRIMITIVE BYTE-BLT). It had the small problem
453 ;; that it didn't work for large (>16M) values of SRC-START or
454 ;; DST-START. However, it might have been more efficient. In
455 ;; particular, I don't really know how much the foreign function
456 ;; call costs us here. My guess is that if the overhead is
457 ;; acceptable for SQRT and COS, it's acceptable here, but this
458 ;; should probably be checked. -- WHN
459 '(flet ((sapify (thing)
461 (system-area-pointer thing)
462 ;; FIXME: The code here rather relies on the simple
463 ;; unboxed array here having byte-sized entries. That
464 ;; should be asserted explicitly, I just haven't found
465 ;; a concise way of doing it. (It would be nice to
466 ;; declare it in the DEFKNOWN too.)
467 ((simple-unboxed-array (*)) (vector-sap thing)))))
468 (declare (inline sapify))
470 (memmove (sap+ (sapify dst) dst-start)
471 (sap+ (sapify src) src-start)
472 (- dst-end dst-start)))
475 ;;;; transforms for EQL of floating point values
477 (deftransform eql ((x y) (single-float single-float))
478 '(= (single-float-bits x) (single-float-bits y)))
480 (deftransform eql ((x y) (double-float double-float))
481 '(and (= (double-float-low-bits x) (double-float-low-bits y))
482 (= (double-float-high-bits x) (double-float-high-bits y))))
485 ;;;; modular functions
486 (define-good-modular-fun logand)
487 (define-good-modular-fun logior)
488 ;;; FIXME: XOR? ANDC1, ANDC2? -- CSR, 2003-09-16
493 (defknown ,name (integer (integer 0)) (unsigned-byte ,width)
494 (foldable flushable movable))
495 (define-modular-fun-optimizer ash ((integer count) :width width)
496 (when (and (<= width ,width)
497 (or (and (constant-lvar-p count)
498 (plusp (lvar-value count)))
499 (csubtypep (lvar-type count)
500 (specifier-type '(and unsigned-byte
502 (cut-to-width integer width)
504 (setf (gethash ',name *modular-versions*) `(ash ,',width)))))
505 ;; This should really be dependent on SB!VM:N-WORD-BITS, but since we
506 ;; don't have a true Alpha64 port yet, we'll have to stick to
507 ;; SB!VM:N-MACHINE-WORD-BITS for the time being. --njf, 2004-08-14
508 #!+#.(cl:if (cl:= 32 sb!vm:n-machine-word-bits) '(and) '(or))
509 (def sb!vm::ash-left-mod32 32)
510 #!+#.(cl:if (cl:= 64 sb!vm:n-machine-word-bits) '(and) '(or))
511 (def sb!vm::ash-left-mod64 64))
514 ;;;; word-wise logical operations
516 ;;; These transforms assume the presence of modular arithmetic to
517 ;;; generate efficient code.
519 (define-source-transform word-logical-not (x)
520 `(logand (lognot (the sb!vm:word ,x)) #.(1- (ash 1 sb!vm:n-word-bits))))
522 (deftransform word-logical-and ((x y))
525 (deftransform word-logical-nand ((x y))
526 '(logand (lognand x y) #.(1- (ash 1 sb!vm:n-word-bits))))
528 (deftransform word-logical-or ((x y))
531 (deftransform word-logical-nor ((x y))
532 '(logand (lognor x y) #.(1- (ash 1 sb!vm:n-word-bits))))
534 (deftransform word-logical-xor ((x y))
537 (deftransform word-logical-eqv ((x y))
538 '(logand (logeqv x y) #.(1- (ash 1 sb!vm:n-word-bits))))
540 (deftransform word-logical-orc1 ((x y))
541 '(logand (logorc1 x y) #.(1- (ash 1 sb!vm:n-word-bits))))
543 (deftransform word-logical-orc2 ((x y))
544 '(logand (logorc2 x y) #.(1- (ash 1 sb!vm:n-word-bits))))
546 (deftransform word-logical-andc1 ((x y))
547 '(logand (logandc1 x y) #.(1- (ash 1 sb!vm:n-word-bits))))
549 (deftransform word-logical-andc2 ((x y))
550 '(logand (logandc2 x y) #.(1- (ash 1 sb!vm:n-word-bits))))
553 ;;; There are two different ways the multiplier can be recoded. The
554 ;;; more obvious is to shift X by the correct amount for each bit set
555 ;;; in Y and to sum the results. But if there is a string of bits that
556 ;;; are all set, you can add X shifted by one more then the bit
557 ;;; position of the first set bit and subtract X shifted by the bit
558 ;;; position of the last set bit. We can't use this second method when
559 ;;; the high order bit is bit 31 because shifting by 32 doesn't work
561 (defun ub32-strength-reduce-constant-multiply (arg num)
562 (declare (type (unsigned-byte 32) num))
563 (let ((adds 0) (shifts 0)
564 (result nil) first-one)
565 (labels ((add (next-factor)
568 (progn (incf adds) `(+ ,result ,next-factor))
570 (declare (inline add))
573 (when (not (logbitp bitpos num))
574 (add (if (= (1+ first-one) bitpos)
575 ;; There is only a single bit in the string.
576 (progn (incf shifts) `(ash ,arg ,first-one))
577 ;; There are at least two.
581 `(- (ash ,arg ,bitpos)
582 (ash ,arg ,first-one)))))
583 (setf first-one nil))
584 (when (logbitp bitpos num)
585 (setf first-one bitpos))))
587 (cond ((= first-one 31))
588 ((= first-one 30) (incf shifts) (add `(ash ,arg 30)))
592 (add `(- (ash ,arg 31)
593 (ash ,arg ,first-one)))))
595 (add `(ash ,arg 31))))
596 (values (if (plusp adds)
597 `(logand ,result #.(1- (ash 1 32))) ; using modular arithmetic