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))
53 ((simple-array base-char (*)) (data-vector-ref string index))
54 ((simple-array nil (*)) (data-vector-ref string index))))))
56 (deftransform hairy-data-vector-ref ((array index) (array t) *)
57 "avoid runtime dispatch on array element type"
58 (let ((element-ctype (extract-upgraded-element-type array))
59 (declared-element-ctype (extract-declared-element-type array)))
60 (declare (type ctype element-ctype))
61 (when (eq *wild-type* element-ctype)
62 (give-up-ir1-transform
63 "Upgraded element type of array is not known at compile time."))
64 ;; (The expansion here is basically a degenerate case of
65 ;; WITH-ARRAY-DATA. Since WITH-ARRAY-DATA is implemented as a
66 ;; macro, and macros aren't expanded in transform output, we have
67 ;; to hand-expand it ourselves.)
68 (let ((element-type-specifier (type-specifier element-ctype)))
69 `(multiple-value-bind (array index)
70 (%data-vector-and-index array index)
71 (declare (type (simple-array ,element-type-specifier 1) array))
72 ,(let ((bare-form '(data-vector-ref array index)))
73 (if (type= element-ctype declared-element-ctype)
75 `(the ,(type-specifier declared-element-ctype)
78 (deftransform data-vector-ref ((array index)
80 (let ((array-type (lvar-type array)))
81 (unless (array-type-p array-type)
82 (give-up-ir1-transform))
83 (let ((dims (array-type-dimensions array-type)))
84 (when (or (atom dims) (= (length dims) 1))
85 (give-up-ir1-transform))
86 (let ((el-type (array-type-specialized-element-type array-type))
87 (total-size (if (member '* dims)
90 `(data-vector-ref (truly-the (simple-array ,(type-specifier el-type)
92 (%array-data-vector array))
95 (deftransform hairy-data-vector-set ((string index new-value)
97 (let ((ctype (lvar-type string)))
98 (if (array-type-p ctype)
99 ;; the other transform will kick in, so that's OK
100 (give-up-ir1-transform)
102 ((simple-array character (*))
103 (data-vector-set string index new-value))
105 ((simple-array base-char (*))
106 (data-vector-set string index new-value))
107 ((simple-array nil (*))
108 (data-vector-set string index new-value))))))
110 (deftransform hairy-data-vector-set ((array index new-value)
113 "avoid runtime dispatch on array element type"
114 (let ((element-ctype (extract-upgraded-element-type array))
115 (declared-element-ctype (extract-declared-element-type array)))
116 (declare (type ctype element-ctype))
117 (when (eq *wild-type* element-ctype)
118 (give-up-ir1-transform
119 "Upgraded element type of array is not known at compile time."))
120 (let ((element-type-specifier (type-specifier element-ctype)))
121 `(multiple-value-bind (array index)
122 (%data-vector-and-index array index)
123 (declare (type (simple-array ,element-type-specifier 1) array)
124 (type ,element-type-specifier new-value))
125 ,(if (type= element-ctype declared-element-ctype)
126 '(data-vector-set array index new-value)
127 `(truly-the ,(type-specifier declared-element-ctype)
128 (data-vector-set array index
129 (the ,(type-specifier declared-element-ctype)
132 (deftransform data-vector-set ((array index new-value)
134 (let ((array-type (lvar-type array)))
135 (unless (array-type-p array-type)
136 (give-up-ir1-transform))
137 (let ((dims (array-type-dimensions array-type)))
138 (when (or (atom dims) (= (length dims) 1))
139 (give-up-ir1-transform))
140 (let ((el-type (array-type-specialized-element-type array-type))
141 (total-size (if (member '* dims)
144 `(data-vector-set (truly-the (simple-array ,(type-specifier el-type)
146 (%array-data-vector array))
150 (defoptimizer (%data-vector-and-index derive-type) ((array index))
151 (let ((atype (lvar-type array)))
152 (when (array-type-p atype)
153 (values-specifier-type
154 `(values (simple-array ,(type-specifier
155 (array-type-specialized-element-type atype))
159 (deftransform %data-vector-and-index ((%array %index)
162 ;; KLUDGE: why the percent signs? Well, ARRAY and INDEX are
163 ;; respectively exported from the CL and SB!INT packages, which
164 ;; means that they're visible to all sorts of things. If the
165 ;; compiler can prove that the call to ARRAY-HEADER-P, below, either
166 ;; returns T or NIL, it will delete the irrelevant branch. However,
167 ;; user code might have got here with a variable named CL:ARRAY, and
168 ;; quite often compiler code with a variable named SB!INT:INDEX, so
169 ;; this can generate code deletion notes for innocuous user code:
170 ;; (DEFUN F (ARRAY I) (DECLARE (SIMPLE-VECTOR ARRAY)) (AREF ARRAY I))
171 ;; -- CSR, 2003-04-01
173 ;; We do this solely for the -OR-GIVE-UP side effect, since we want
174 ;; to know that the type can be figured out in the end before we
175 ;; proceed, but we don't care yet what the type will turn out to be.
176 (upgraded-element-type-specifier-or-give-up %array)
178 '(if (array-header-p %array)
179 (values (%array-data-vector %array) %index)
180 (values %array %index)))
182 ;;; transforms for getting at simple arrays of (UNSIGNED-BYTE N) when (< N 8)
184 ;;; FIXME: In CMU CL, these were commented out with #+NIL. Why? Should
185 ;;; we fix them or should we delete them? (Perhaps these definitions
186 ;;; predate the various DATA-VECTOR-REF-FOO VOPs which have
187 ;;; (:TRANSLATE DATA-VECTOR-REF), and are redundant now?)
191 (let ((elements-per-word (truncate sb!vm:n-word-bits bits)))
193 (deftransform data-vector-ref ((vector index)
195 `(multiple-value-bind (word bit)
196 (floor index ,',elements-per-word)
197 (ldb ,(ecase sb!vm:target-byte-order
198 (:little-endian '(byte ,bits (* bit ,bits)))
199 (:big-endian '(byte ,bits (- sb!vm:n-word-bits
200 (* (1+ bit) ,bits)))))
201 (%raw-bits vector (+ word sb!vm:vector-data-offset)))))
202 (deftransform data-vector-set ((vector index new-value)
204 `(multiple-value-bind (word bit)
205 (floor index ,',elements-per-word)
206 (setf (ldb ,(ecase sb!vm:target-byte-order
207 (:little-endian '(byte ,bits (* bit ,bits)))
209 '(byte ,bits (- sb!vm:n-word-bits
210 (* (1+ bit) ,bits)))))
211 (%raw-bits vector (+ word sb!vm:vector-data-offset)))
213 (frob simple-bit-vector 1)
214 (frob (simple-array (unsigned-byte 2) (*)) 2)
215 (frob (simple-array (unsigned-byte 4) (*)) 4))
217 ;;;; BIT-VECTOR hackery
219 ;;; SIMPLE-BIT-VECTOR bit-array operations are transformed to a word
220 ;;; loop that does 32 bits at a time.
222 ;;; FIXME: This is a lot of repeatedly macroexpanded code. It should
223 ;;; be a function call instead.
224 (macrolet ((def (bitfun wordfun)
225 `(deftransform ,bitfun ((bit-array-1 bit-array-2 result-bit-array)
230 :node node :policy (>= speed space))
232 ,@(unless (policy node (zerop safety))
233 '((unless (= (length bit-array-1)
235 (length result-bit-array))
236 (error "Argument and/or result bit arrays are not the same length:~
241 (let ((length (length result-bit-array)))
243 ;; We avoid doing anything to 0-length
244 ;; bit-vectors, or rather, the memory that
245 ;; follows them. Other divisible-by-32 cases
246 ;; are handled by the (1- length), below.
249 (do ((index sb!vm:vector-data-offset (1+ index))
250 (end-1 (+ sb!vm:vector-data-offset
251 ;; bit-vectors of length 1-32
252 ;; need precisely one (SETF
253 ;; %RAW-BITS), done here in the
254 ;; epilogue. - CSR, 2002-04-24
255 (truncate (truly-the index (1- length))
256 sb!vm:n-word-bits))))
258 (setf (%raw-bits result-bit-array index)
259 (,',wordfun (%raw-bits bit-array-1 index)
260 (%raw-bits bit-array-2 index)))
262 (declare (optimize (speed 3) (safety 0))
263 (type index index end-1))
264 (setf (%raw-bits result-bit-array index)
265 (,',wordfun (%raw-bits bit-array-1 index)
266 (%raw-bits bit-array-2 index))))))))))
267 (def bit-and word-logical-and)
268 (def bit-ior word-logical-or)
269 (def bit-xor word-logical-xor)
270 (def bit-eqv word-logical-eqv)
271 (def bit-nand word-logical-nand)
272 (def bit-nor word-logical-nor)
273 (def bit-andc1 word-logical-andc1)
274 (def bit-andc2 word-logical-andc2)
275 (def bit-orc1 word-logical-orc1)
276 (def bit-orc2 word-logical-orc2))
278 (deftransform bit-not
279 ((bit-array result-bit-array)
280 (simple-bit-vector simple-bit-vector) *
281 :node node :policy (>= speed space))
283 ,@(unless (policy node (zerop safety))
284 '((unless (= (length bit-array)
285 (length result-bit-array))
286 (error "Argument and result bit arrays are not the same length:~
288 bit-array result-bit-array))))
289 (let ((length (length result-bit-array)))
291 ;; We avoid doing anything to 0-length bit-vectors, or rather,
292 ;; the memory that follows them. Other divisible-by
293 ;; n-word-bits cases are handled by the (1- length), below.
296 (do ((index sb!vm:vector-data-offset (1+ index))
297 (end-1 (+ sb!vm:vector-data-offset
298 ;; bit-vectors of length 1 to n-word-bits need
299 ;; precisely one (SETF %RAW-BITS), done here in
300 ;; the epilogue. - CSR, 2002-04-24
301 (truncate (truly-the index (1- length))
302 sb!vm:n-word-bits))))
304 (setf (%raw-bits result-bit-array index)
305 (word-logical-not (%raw-bits bit-array index)))
307 (declare (optimize (speed 3) (safety 0))
308 (type index index end-1))
309 (setf (%raw-bits result-bit-array index)
310 (word-logical-not (%raw-bits bit-array index))))))))
312 (deftransform bit-vector-= ((x y) (simple-bit-vector simple-bit-vector))
313 `(and (= (length x) (length y))
314 (let ((length (length x)))
316 (do* ((i sb!vm:vector-data-offset (+ i 1))
317 (end-1 (+ sb!vm:vector-data-offset
318 (floor (1- length) sb!vm:n-word-bits))))
320 (let* ((extra (mod length sb!vm:n-word-bits))
321 (mask (1- (ash 1 extra)))
325 ,(ecase sb!c:*backend-byte-order*
328 '(- sb!vm:n-word-bits extra))))
333 ,(ecase sb!c:*backend-byte-order*
336 '(- sb!vm:n-word-bits extra))))
338 (declare (type (mod #.sb!vm:n-word-bits)
340 (type sb!vm:word mask numx numy))
342 (declare (type index i end-1))
343 (let ((numx (%raw-bits x i))
344 (numy (%raw-bits y i)))
345 (declare (type sb!vm:word numx numy))
346 (unless (= numx numy)
349 (deftransform count ((item sequence) (bit simple-bit-vector) *
350 :policy (>= speed space))
351 `(let ((length (length sequence)))
354 (do ((index sb!vm:vector-data-offset (1+ index))
356 (end-1 (+ sb!vm:vector-data-offset
357 (truncate (truly-the index (1- length))
358 sb!vm:n-word-bits))))
360 (let* ((extra (mod length sb!vm:n-word-bits))
361 (mask (1- (ash 1 extra)))
362 (bits (logand (ash mask
363 ,(ecase sb!c:*backend-byte-order*
366 '(- sb!vm:n-word-bits extra))))
367 (%raw-bits sequence index))))
368 (declare (type (mod #.sb!vm:n-word-bits) extra))
369 (declare (type sb!vm:word mask bits))
370 ;; could consider LOGNOT for the zero case instead of
371 ;; doing the subtraction...
372 (incf count ,(if (constant-lvar-p item)
373 (if (zerop (lvar-value item))
374 '(- extra (logcount bits))
377 (- extra (logcount bits))
379 (declare (type index index count end-1)
380 (optimize (speed 3) (safety 0)))
381 (incf count ,(if (constant-lvar-p item)
382 (if (zerop (lvar-value item))
383 '(- sb!vm:n-word-bits (logcount (%raw-bits sequence index)))
384 '(logcount (%raw-bits sequence index)))
386 (- sb!vm:n-word-bits (logcount (%raw-bits sequence index)))
387 (logcount (%raw-bits sequence index)))))))))
389 (deftransform fill ((sequence item) (simple-bit-vector bit) *
390 :policy (>= speed space))
391 (let ((value (if (constant-lvar-p item)
392 (if (= (lvar-value item) 0)
394 #.(1- (ash 1 sb!vm:n-word-bits)))
395 `(if (= item 0) 0 #.(1- (ash 1 sb!vm:n-word-bits))))))
396 `(let ((length (length sequence))
400 (do ((index sb!vm:vector-data-offset (1+ index))
401 (end-1 (+ sb!vm:vector-data-offset
402 ;; bit-vectors of length 1 to n-word-bits need
403 ;; precisely one (SETF %RAW-BITS), done here
404 ;; in the epilogue. - CSR, 2002-04-24
405 (truncate (truly-the index (1- length))
406 sb!vm:n-word-bits))))
408 (setf (%raw-bits sequence index) value)
410 (declare (optimize (speed 3) (safety 0))
411 (type index index end-1))
412 (setf (%raw-bits sequence index) value))))))
414 (deftransform fill ((sequence item) (simple-base-string base-char) *
415 :policy (>= speed space))
416 (let ((value (if (constant-lvar-p item)
417 (let* ((char (lvar-value item))
418 (code (sb!xc:char-code char))
420 (dotimes (i sb!vm:n-word-bytes accum)
421 (setf accum (logior accum (ash code (* 8 i))))))
422 `(let ((code (sb!xc:char-code item)))
423 (logior ,@(loop for i from 0 below sb!vm:n-word-bytes
424 collect `(ash code ,(* 8 i))))))))
425 `(let ((length (length sequence))
427 (multiple-value-bind (times rem)
428 (truncate length sb!vm:n-word-bytes)
429 (do ((index sb!vm:vector-data-offset (1+ index))
430 (end (+ times sb!vm:vector-data-offset)))
432 (let ((place (* times sb!vm:n-word-bytes)))
433 (declare (fixnum place))
434 (dotimes (j rem sequence)
436 (setf (schar sequence (the index (+ place j))) item))))
437 (declare (optimize (speed 3) (safety 0))
439 (setf (%raw-bits sequence index) value))))))
443 ;;; FIXME: The old CMU CL code used various COPY-TO/FROM-SYSTEM-AREA
444 ;;; stuff (with all the associated bit-index cruft and overflow
445 ;;; issues) even for byte moves. In SBCL, we're converting to byte
446 ;;; moves as problems are discovered with the old code, and this is
447 ;;; currently (ca. sbcl-0.6.12.30) the main interface for code in
448 ;;; SB!KERNEL and SB!SYS (e.g. i/o code). It's not clear that it's the
449 ;;; ideal interface, though, and it probably deserves some thought.
450 (deftransform %byte-blt ((src src-start dst dst-start dst-end)
451 ((or (simple-unboxed-array (*)) system-area-pointer)
453 (or (simple-unboxed-array (*)) system-area-pointer)
456 ;; FIXME: CMU CL had a hairier implementation of this (back when it
457 ;; was still called (%PRIMITIVE BYTE-BLT). It had the small problem
458 ;; that it didn't work for large (>16M) values of SRC-START or
459 ;; DST-START. However, it might have been more efficient. In
460 ;; particular, I don't really know how much the foreign function
461 ;; call costs us here. My guess is that if the overhead is
462 ;; acceptable for SQRT and COS, it's acceptable here, but this
463 ;; should probably be checked. -- WHN
464 '(flet ((sapify (thing)
466 (system-area-pointer thing)
467 ;; FIXME: The code here rather relies on the simple
468 ;; unboxed array here having byte-sized entries. That
469 ;; should be asserted explicitly, I just haven't found
470 ;; a concise way of doing it. (It would be nice to
471 ;; declare it in the DEFKNOWN too.)
472 ((simple-unboxed-array (*)) (vector-sap thing)))))
473 (declare (inline sapify))
475 (memmove (sap+ (sapify dst) dst-start)
476 (sap+ (sapify src) src-start)
477 (- dst-end dst-start)))
480 ;;;; transforms for EQL of floating point values
482 (deftransform eql ((x y) (single-float single-float))
483 '(= (single-float-bits x) (single-float-bits y)))
485 (deftransform eql ((x y) (double-float double-float))
486 '(and (= (double-float-low-bits x) (double-float-low-bits y))
487 (= (double-float-high-bits x) (double-float-high-bits y))))
490 ;;;; modular functions
491 (define-good-modular-fun logand)
492 (define-good-modular-fun logior)
493 ;;; FIXME: XOR? ANDC1, ANDC2? -- CSR, 2003-09-16
498 (defknown ,name (integer (integer 0)) (unsigned-byte ,width)
499 (foldable flushable movable))
500 (define-modular-fun-optimizer ash ((integer count) :width width)
501 (when (and (<= width ,width)
502 (or (and (constant-lvar-p count)
503 (plusp (lvar-value count)))
504 (csubtypep (lvar-type count)
505 (specifier-type '(and unsigned-byte
507 (cut-to-width integer width)
509 (setf (gethash ',name *modular-versions*) `(ash ,',width)))))
510 ;; This should really be dependent on SB!VM:N-WORD-BITS, but since we
511 ;; don't have a true Alpha64 port yet, we'll have to stick to
512 ;; SB!VM:N-MACHINE-WORD-BITS for the time being. --njf, 2004-08-14
513 #!+#.(cl:if (cl:= 32 sb!vm:n-machine-word-bits) '(and) '(or))
514 (def sb!vm::ash-left-mod32 32)
515 #!+#.(cl:if (cl:= 64 sb!vm:n-machine-word-bits) '(and) '(or))
516 (def sb!vm::ash-left-mod64 64))
519 ;;;; word-wise logical operations
521 ;;; These transforms assume the presence of modular arithmetic to
522 ;;; generate efficient code.
524 (define-source-transform word-logical-not (x)
525 `(logand (lognot (the sb!vm:word ,x)) #.(1- (ash 1 sb!vm:n-word-bits))))
527 (deftransform word-logical-and ((x y))
530 (deftransform word-logical-nand ((x y))
531 '(logand (lognand x y) #.(1- (ash 1 sb!vm:n-word-bits))))
533 (deftransform word-logical-or ((x y))
536 (deftransform word-logical-nor ((x y))
537 '(logand (lognor x y) #.(1- (ash 1 sb!vm:n-word-bits))))
539 (deftransform word-logical-xor ((x y))
542 (deftransform word-logical-eqv ((x y))
543 '(logand (logeqv x y) #.(1- (ash 1 sb!vm:n-word-bits))))
545 (deftransform word-logical-orc1 ((x y))
546 '(logand (logorc1 x y) #.(1- (ash 1 sb!vm:n-word-bits))))
548 (deftransform word-logical-orc2 ((x y))
549 '(logand (logorc2 x y) #.(1- (ash 1 sb!vm:n-word-bits))))
551 (deftransform word-logical-andc1 ((x y))
552 '(logand (logandc1 x y) #.(1- (ash 1 sb!vm:n-word-bits))))
554 (deftransform word-logical-andc2 ((x y))
555 '(logand (logandc2 x y) #.(1- (ash 1 sb!vm:n-word-bits))))
558 ;;; There are two different ways the multiplier can be recoded. The
559 ;;; more obvious is to shift X by the correct amount for each bit set
560 ;;; in Y and to sum the results. But if there is a string of bits that
561 ;;; are all set, you can add X shifted by one more then the bit
562 ;;; position of the first set bit and subtract X shifted by the bit
563 ;;; position of the last set bit. We can't use this second method when
564 ;;; the high order bit is bit 31 because shifting by 32 doesn't work
566 (defun ub32-strength-reduce-constant-multiply (arg num)
567 (declare (type (unsigned-byte 32) num))
568 (let ((adds 0) (shifts 0)
569 (result nil) first-one)
570 (labels ((add (next-factor)
573 (progn (incf adds) `(+ ,result ,next-factor))
575 (declare (inline add))
578 (when (not (logbitp bitpos num))
579 (add (if (= (1+ first-one) bitpos)
580 ;; There is only a single bit in the string.
581 (progn (incf shifts) `(ash ,arg ,first-one))
582 ;; There are at least two.
586 `(- (ash ,arg ,bitpos)
587 (ash ,arg ,first-one)))))
588 (setf first-one nil))
589 (when (logbitp bitpos num)
590 (setf first-one bitpos))))
592 (cond ((= first-one 31))
593 ((= first-one 30) (incf shifts) (add `(ash ,arg 30)))
597 (add `(- (ash ,arg 31)
598 (ash ,arg ,first-one)))))
600 (add `(ash ,arg 31))))
601 (values (if (plusp adds)
602 `(logand ,result #.(1- (ash 1 32))) ; using modular arithmetic