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)
27 (not (sb!eval:interpreted-function-p ,x)))))
29 (define-source-transform char-int (x)
32 (deftransform abs ((x) (rational))
33 '(if (< x 0) (- x) x))
35 ;;; The layout is stored in slot 0.
36 (define-source-transform %instance-layout (x)
37 `(truly-the layout (%instance-ref ,x 0)))
38 (define-source-transform %set-instance-layout (x val)
39 `(%instance-set ,x 0 (the layout ,val)))
40 (define-source-transform %funcallable-instance-layout (x)
41 `(truly-the layout (%funcallable-instance-info ,x 0)))
42 (define-source-transform %set-funcallable-instance-layout (x val)
43 `(setf (%funcallable-instance-info ,x 0) (the layout ,val)))
45 ;;;; character support
47 ;;; In our implementation there are really only BASE-CHARs.
49 (define-source-transform characterp (obj)
52 ;;;; simplifying HAIRY-DATA-VECTOR-REF and HAIRY-DATA-VECTOR-SET
54 (deftransform hairy-data-vector-ref ((string index) (simple-string t))
55 (let ((ctype (lvar-type string)))
56 (if (array-type-p ctype)
57 ;; the other transform will kick in, so that's OK
58 (give-up-ir1-transform)
60 ((simple-array character (*)) (data-vector-ref string index))
62 ((simple-array base-char (*)) (data-vector-ref string index))
63 ((simple-array nil (*)) (data-vector-ref string index))))))
65 (deftransform hairy-data-vector-ref ((array index) (array t) *)
66 "avoid runtime dispatch on array element type"
67 (let ((element-ctype (extract-upgraded-element-type array))
68 (declared-element-ctype (extract-declared-element-type array)))
69 (declare (type ctype element-ctype))
70 (when (eq *wild-type* element-ctype)
71 (give-up-ir1-transform
72 "Upgraded element type of array is not known at compile time."))
73 ;; (The expansion here is basically a degenerate case of
74 ;; WITH-ARRAY-DATA. Since WITH-ARRAY-DATA is implemented as a
75 ;; macro, and macros aren't expanded in transform output, we have
76 ;; to hand-expand it ourselves.)
77 (let ((element-type-specifier (type-specifier element-ctype)))
78 `(multiple-value-bind (array index)
79 (%data-vector-and-index array index)
80 (declare (type (simple-array ,element-type-specifier 1) array))
81 ,(let ((bare-form '(data-vector-ref array index)))
82 (if (type= element-ctype declared-element-ctype)
84 `(the ,(type-specifier declared-element-ctype)
87 ;;; Transform multi-dimensional array to one dimensional data vector
89 (deftransform data-vector-ref ((array index)
91 (let ((array-type (lvar-type array)))
92 (unless (array-type-p array-type)
93 (give-up-ir1-transform))
94 (let ((dims (array-type-dimensions array-type)))
95 (when (or (atom dims) (= (length dims) 1))
96 (give-up-ir1-transform))
97 (let ((el-type (array-type-specialized-element-type array-type))
98 (total-size (if (member '* dims)
101 `(data-vector-ref (truly-the (simple-array ,(type-specifier el-type)
103 (%array-data-vector array))
106 (deftransform hairy-data-vector-set ((string index new-value)
108 (let ((ctype (lvar-type string)))
109 (if (array-type-p ctype)
110 ;; the other transform will kick in, so that's OK
111 (give-up-ir1-transform)
113 ((simple-array character (*))
114 (data-vector-set string index new-value))
116 ((simple-array base-char (*))
117 (data-vector-set string index new-value))
118 ((simple-array nil (*))
119 (data-vector-set string index new-value))))))
121 (deftransform hairy-data-vector-set ((array index new-value)
124 "avoid runtime dispatch on array element type"
125 (let ((element-ctype (extract-upgraded-element-type array))
126 (declared-element-ctype (extract-declared-element-type array)))
127 (declare (type ctype element-ctype))
128 (when (eq *wild-type* element-ctype)
129 (give-up-ir1-transform
130 "Upgraded element type of array is not known at compile time."))
131 (let ((element-type-specifier (type-specifier element-ctype)))
132 `(multiple-value-bind (array index)
133 (%data-vector-and-index array index)
134 (declare (type (simple-array ,element-type-specifier 1) array)
135 (type ,element-type-specifier new-value))
136 ,(if (type= element-ctype declared-element-ctype)
137 '(data-vector-set array index new-value)
138 `(truly-the ,(type-specifier declared-element-ctype)
139 (data-vector-set array index
140 (the ,(type-specifier declared-element-ctype)
143 (deftransform data-vector-set ((array index new-value)
145 (let ((array-type (lvar-type array)))
146 (unless (array-type-p array-type)
147 (give-up-ir1-transform))
148 (let ((dims (array-type-dimensions array-type)))
149 (when (or (atom dims) (= (length dims) 1))
150 (give-up-ir1-transform))
151 (let ((el-type (array-type-specialized-element-type array-type))
152 (total-size (if (member '* dims)
155 `(data-vector-set (truly-the (simple-array ,(type-specifier el-type)
157 (%array-data-vector array))
161 (defoptimizer (%data-vector-and-index derive-type) ((array index))
162 (let ((atype (lvar-type array)))
163 (when (array-type-p atype)
164 (values-specifier-type
165 `(values (simple-array ,(type-specifier
166 (array-type-specialized-element-type atype))
170 (deftransform %data-vector-and-index ((%array %index)
173 ;; KLUDGE: why the percent signs? Well, ARRAY and INDEX are
174 ;; respectively exported from the CL and SB!INT packages, which
175 ;; means that they're visible to all sorts of things. If the
176 ;; compiler can prove that the call to ARRAY-HEADER-P, below, either
177 ;; returns T or NIL, it will delete the irrelevant branch. However,
178 ;; user code might have got here with a variable named CL:ARRAY, and
179 ;; quite often compiler code with a variable named SB!INT:INDEX, so
180 ;; this can generate code deletion notes for innocuous user code:
181 ;; (DEFUN F (ARRAY I) (DECLARE (SIMPLE-VECTOR ARRAY)) (AREF ARRAY I))
182 ;; -- CSR, 2003-04-01
184 ;; We do this solely for the -OR-GIVE-UP side effect, since we want
185 ;; to know that the type can be figured out in the end before we
186 ;; proceed, but we don't care yet what the type will turn out to be.
187 (upgraded-element-type-specifier-or-give-up %array)
189 '(if (array-header-p %array)
190 (values (%array-data-vector %array) %index)
191 (values %array %index)))
193 ;;; transforms for getting at simple arrays of (UNSIGNED-BYTE N) when (< N 8)
195 ;;; FIXME: In CMU CL, these were commented out with #+NIL. Why? Should
196 ;;; we fix them or should we delete them? (Perhaps these definitions
197 ;;; predate the various DATA-VECTOR-REF-FOO VOPs which have
198 ;;; (:TRANSLATE DATA-VECTOR-REF), and are redundant now?)
202 (let ((elements-per-word (truncate sb!vm:n-word-bits bits)))
204 (deftransform data-vector-ref ((vector index)
206 `(multiple-value-bind (word bit)
207 (floor index ,',elements-per-word)
208 (ldb ,(ecase sb!vm:target-byte-order
209 (:little-endian '(byte ,bits (* bit ,bits)))
210 (:big-endian '(byte ,bits (- sb!vm:n-word-bits
211 (* (1+ bit) ,bits)))))
212 (%raw-bits vector (+ word sb!vm:vector-data-offset)))))
213 (deftransform data-vector-set ((vector index new-value)
215 `(multiple-value-bind (word bit)
216 (floor index ,',elements-per-word)
217 (setf (ldb ,(ecase sb!vm:target-byte-order
218 (:little-endian '(byte ,bits (* bit ,bits)))
220 '(byte ,bits (- sb!vm:n-word-bits
221 (* (1+ bit) ,bits)))))
222 (%raw-bits vector (+ word sb!vm:vector-data-offset)))
224 (frob simple-bit-vector 1)
225 (frob (simple-array (unsigned-byte 2) (*)) 2)
226 (frob (simple-array (unsigned-byte 4) (*)) 4))
228 ;;;; BIT-VECTOR hackery
230 ;;; SIMPLE-BIT-VECTOR bit-array operations are transformed to a word
231 ;;; loop that does 32 bits at a time.
233 ;;; FIXME: This is a lot of repeatedly macroexpanded code. It should
234 ;;; be a function call instead.
235 (macrolet ((def (bitfun wordfun)
236 `(deftransform ,bitfun ((bit-array-1 bit-array-2 result-bit-array)
241 :node node :policy (>= speed space))
243 ,@(unless (policy node (zerop safety))
244 '((unless (= (length bit-array-1)
246 (length result-bit-array))
247 (error "Argument and/or result bit arrays are not the same length:~
252 (let ((length (length result-bit-array)))
254 ;; We avoid doing anything to 0-length
255 ;; bit-vectors, or rather, the memory that
256 ;; follows them. Other divisible-by-32 cases
257 ;; are handled by the (1- length), below.
260 (do ((index sb!vm:vector-data-offset (1+ index))
261 (end-1 (+ sb!vm:vector-data-offset
262 ;; bit-vectors of length 1-32
263 ;; need precisely one (SETF
264 ;; %RAW-BITS), done here in the
265 ;; epilogue. - CSR, 2002-04-24
266 (truncate (truly-the index (1- length))
267 sb!vm:n-word-bits))))
269 (setf (%raw-bits result-bit-array index)
270 (,',wordfun (%raw-bits bit-array-1 index)
271 (%raw-bits bit-array-2 index)))
273 (declare (optimize (speed 3) (safety 0))
274 (type index index end-1))
275 (setf (%raw-bits result-bit-array index)
276 (,',wordfun (%raw-bits bit-array-1 index)
277 (%raw-bits bit-array-2 index))))))))))
278 (def bit-and word-logical-and)
279 (def bit-ior word-logical-or)
280 (def bit-xor word-logical-xor)
281 (def bit-eqv word-logical-eqv)
282 (def bit-nand word-logical-nand)
283 (def bit-nor word-logical-nor)
284 (def bit-andc1 word-logical-andc1)
285 (def bit-andc2 word-logical-andc2)
286 (def bit-orc1 word-logical-orc1)
287 (def bit-orc2 word-logical-orc2))
289 (deftransform bit-not
290 ((bit-array result-bit-array)
291 (simple-bit-vector simple-bit-vector) *
292 :node node :policy (>= speed space))
294 ,@(unless (policy node (zerop safety))
295 '((unless (= (length bit-array)
296 (length result-bit-array))
297 (error "Argument and result bit arrays are not the same length:~
299 bit-array result-bit-array))))
300 (let ((length (length result-bit-array)))
302 ;; We avoid doing anything to 0-length bit-vectors, or rather,
303 ;; the memory that follows them. Other divisible-by
304 ;; n-word-bits cases are handled by the (1- length), below.
307 (do ((index sb!vm:vector-data-offset (1+ index))
308 (end-1 (+ sb!vm:vector-data-offset
309 ;; bit-vectors of length 1 to n-word-bits need
310 ;; precisely one (SETF %RAW-BITS), done here in
311 ;; the epilogue. - CSR, 2002-04-24
312 (truncate (truly-the index (1- length))
313 sb!vm:n-word-bits))))
315 (setf (%raw-bits result-bit-array index)
316 (word-logical-not (%raw-bits bit-array index)))
318 (declare (optimize (speed 3) (safety 0))
319 (type index index end-1))
320 (setf (%raw-bits result-bit-array index)
321 (word-logical-not (%raw-bits bit-array index))))))))
323 (deftransform bit-vector-= ((x y) (simple-bit-vector simple-bit-vector))
324 `(and (= (length x) (length y))
325 (let ((length (length x)))
327 (do* ((i sb!vm:vector-data-offset (+ i 1))
328 (end-1 (+ sb!vm:vector-data-offset
329 (floor (1- length) sb!vm:n-word-bits))))
331 (let* ((extra (1+ (mod (1- length) sb!vm:n-word-bits)))
332 (mask (ash #.(1- (ash 1 sb!vm:n-word-bits))
333 (- extra sb!vm:n-word-bits)))
337 ,(ecase sb!c:*backend-byte-order*
340 '(- sb!vm:n-word-bits extra))))
345 ,(ecase sb!c:*backend-byte-order*
348 '(- sb!vm:n-word-bits extra))))
350 (declare (type (integer 1 #.sb!vm:n-word-bits) extra)
351 (type sb!vm:word mask numx numy))
353 (declare (type index i end-1))
354 (let ((numx (%raw-bits x i))
355 (numy (%raw-bits y i)))
356 (declare (type sb!vm:word numx numy))
357 (unless (= numx numy)
360 (deftransform count ((item sequence) (bit simple-bit-vector) *
361 :policy (>= speed space))
362 `(let ((length (length sequence)))
365 (do ((index sb!vm:vector-data-offset (1+ index))
367 (end-1 (+ sb!vm:vector-data-offset
368 (truncate (truly-the index (1- length))
369 sb!vm:n-word-bits))))
371 (let* ((extra (1+ (mod (1- length) sb!vm:n-word-bits)))
372 (mask (ash #.(1- (ash 1 sb!vm:n-word-bits))
373 (- extra sb!vm:n-word-bits)))
374 (bits (logand (ash mask
375 ,(ecase sb!c:*backend-byte-order*
378 '(- sb!vm:n-word-bits extra))))
379 (%raw-bits sequence index))))
380 (declare (type (integer 1 #.sb!vm:n-word-bits) extra))
381 (declare (type sb!vm:word mask bits))
382 (incf count (logcount bits))
383 ,(if (constant-lvar-p item)
384 (if (zerop (lvar-value item))
390 (declare (type index index count end-1)
391 (optimize (speed 3) (safety 0)))
392 (incf count (logcount (%raw-bits sequence index)))))))
394 (deftransform fill ((sequence item) (simple-bit-vector bit) *
395 :policy (>= speed space))
396 (let ((value (if (constant-lvar-p item)
397 (if (= (lvar-value item) 0)
399 #.(1- (ash 1 sb!vm:n-word-bits)))
400 `(if (= item 0) 0 #.(1- (ash 1 sb!vm:n-word-bits))))))
401 `(let ((length (length sequence))
405 (do ((index sb!vm:vector-data-offset (1+ index))
406 (end-1 (+ sb!vm:vector-data-offset
407 ;; bit-vectors of length 1 to n-word-bits need
408 ;; precisely one (SETF %RAW-BITS), done here
409 ;; in the epilogue. - CSR, 2002-04-24
410 (truncate (truly-the index (1- length))
411 sb!vm:n-word-bits))))
413 (setf (%raw-bits sequence index) value)
415 (declare (optimize (speed 3) (safety 0))
416 (type index index end-1))
417 (setf (%raw-bits sequence index) value))))))
419 (deftransform fill ((sequence item) (simple-base-string base-char) *
420 :policy (>= speed space))
421 (let ((value (if (constant-lvar-p item)
422 (let* ((char (lvar-value item))
423 (code (sb!xc:char-code char))
425 (dotimes (i sb!vm:n-word-bytes accum)
426 (setf accum (logior accum (ash code (* 8 i))))))
427 `(let ((code (sb!xc:char-code item)))
428 (logior ,@(loop for i from 0 below sb!vm:n-word-bytes
429 collect `(ash code ,(* 8 i))))))))
430 `(let ((length (length sequence))
432 (multiple-value-bind (times rem)
433 (truncate length sb!vm:n-word-bytes)
434 (do ((index sb!vm:vector-data-offset (1+ index))
435 (end (+ times sb!vm:vector-data-offset)))
437 (let ((place (* times sb!vm:n-word-bytes)))
438 (declare (fixnum place))
439 (dotimes (j rem sequence)
441 (setf (schar sequence (the index (+ place j))) item))))
442 (declare (optimize (speed 3) (safety 0))
444 (setf (%raw-bits sequence index) value))))))
448 ;;; FIXME: The old CMU CL code used various COPY-TO/FROM-SYSTEM-AREA
449 ;;; stuff (with all the associated bit-index cruft and overflow
450 ;;; issues) even for byte moves. In SBCL, we're converting to byte
451 ;;; moves as problems are discovered with the old code, and this is
452 ;;; currently (ca. sbcl-0.6.12.30) the main interface for code in
453 ;;; SB!KERNEL and SB!SYS (e.g. i/o code). It's not clear that it's the
454 ;;; ideal interface, though, and it probably deserves some thought.
455 (deftransform %byte-blt ((src src-start dst dst-start dst-end)
456 ((or (simple-unboxed-array (*)) system-area-pointer)
458 (or (simple-unboxed-array (*)) system-area-pointer)
461 ;; FIXME: CMU CL had a hairier implementation of this (back when it
462 ;; was still called (%PRIMITIVE BYTE-BLT). It had the small problem
463 ;; that it didn't work for large (>16M) values of SRC-START or
464 ;; DST-START. However, it might have been more efficient. In
465 ;; particular, I don't really know how much the foreign function
466 ;; call costs us here. My guess is that if the overhead is
467 ;; acceptable for SQRT and COS, it's acceptable here, but this
468 ;; should probably be checked. -- WHN
469 '(flet ((sapify (thing)
471 (system-area-pointer thing)
472 ;; FIXME: The code here rather relies on the simple
473 ;; unboxed array here having byte-sized entries. That
474 ;; should be asserted explicitly, I just haven't found
475 ;; a concise way of doing it. (It would be nice to
476 ;; declare it in the DEFKNOWN too.)
477 ((simple-unboxed-array (*)) (vector-sap thing)))))
478 (declare (inline sapify))
480 (memmove (sap+ (sapify dst) dst-start)
481 (sap+ (sapify src) src-start)
482 (- dst-end dst-start)))
485 ;;;; transforms for EQL of floating point values
487 (deftransform eql ((x y) (single-float single-float))
488 '(= (single-float-bits x) (single-float-bits y)))
490 (deftransform eql ((x y) (double-float double-float))
491 '(and (= (double-float-low-bits x) (double-float-low-bits y))
492 (= (double-float-high-bits x) (double-float-high-bits y))))
495 ;;;; modular functions
496 (define-good-modular-fun logand :unsigned)
497 (define-good-modular-fun logior :unsigned)
498 ;;; FIXME: XOR? ANDC1, ANDC2? -- CSR, 2003-09-16
501 ((def (name class width)
502 (let ((type (ecase class
503 (:unsigned 'unsigned-byte)
504 (:signed 'signed-byte))))
506 (defknown ,name (integer (integer 0)) (,type ,width)
507 (foldable flushable movable))
508 (define-modular-fun-optimizer ash ((integer count) ,class :width width)
509 (when (and (<= width ,width)
510 (or (and (constant-lvar-p count)
511 (plusp (lvar-value count)))
512 (csubtypep (lvar-type count)
513 (specifier-type '(and unsigned-byte fixnum)))))
514 (cut-to-width integer ,class width)
516 (setf (gethash ',name (modular-class-versions (find-modular-class ',class)))
518 ;; This should really be dependent on SB!VM:N-WORD-BITS, but since we
519 ;; don't have a true Alpha64 port yet, we'll have to stick to
520 ;; SB!VM:N-MACHINE-WORD-BITS for the time being. --njf, 2004-08-14
521 #!+#.(cl:if (cl:= 32 sb!vm:n-machine-word-bits) '(and) '(or))
523 #!+x86 (def sb!vm::ash-left-smod30 :signed 30)
524 (def sb!vm::ash-left-mod32 :unsigned 32))
525 #!+#.(cl:if (cl:= 64 sb!vm:n-machine-word-bits) '(and) '(or))
527 #!+x86-64 (def sb!vm::ash-left-smod61 :signed 61)
528 (def sb!vm::ash-left-mod64 :unsigned 64)))
531 ;;;; word-wise logical operations
533 ;;; These transforms assume the presence of modular arithmetic to
534 ;;; generate efficient code.
536 (define-source-transform word-logical-not (x)
537 `(logand (lognot (the sb!vm:word ,x)) #.(1- (ash 1 sb!vm:n-word-bits))))
539 (deftransform word-logical-and ((x y))
542 (deftransform word-logical-nand ((x y))
543 '(logand (lognand x y) #.(1- (ash 1 sb!vm:n-word-bits))))
545 (deftransform word-logical-or ((x y))
548 (deftransform word-logical-nor ((x y))
549 '(logand (lognor x y) #.(1- (ash 1 sb!vm:n-word-bits))))
551 (deftransform word-logical-xor ((x y))
554 (deftransform word-logical-eqv ((x y))
555 '(logand (logeqv x y) #.(1- (ash 1 sb!vm:n-word-bits))))
557 (deftransform word-logical-orc1 ((x y))
558 '(logand (logorc1 x y) #.(1- (ash 1 sb!vm:n-word-bits))))
560 (deftransform word-logical-orc2 ((x y))
561 '(logand (logorc2 x y) #.(1- (ash 1 sb!vm:n-word-bits))))
563 (deftransform word-logical-andc1 ((x y))
564 '(logand (logandc1 x y) #.(1- (ash 1 sb!vm:n-word-bits))))
566 (deftransform word-logical-andc2 ((x y))
567 '(logand (logandc2 x y) #.(1- (ash 1 sb!vm:n-word-bits))))
570 ;;; There are two different ways the multiplier can be recoded. The
571 ;;; more obvious is to shift X by the correct amount for each bit set
572 ;;; in Y and to sum the results. But if there is a string of bits that
573 ;;; are all set, you can add X shifted by one more then the bit
574 ;;; position of the first set bit and subtract X shifted by the bit
575 ;;; position of the last set bit. We can't use this second method when
576 ;;; the high order bit is bit 31 because shifting by 32 doesn't work
578 (defun ub32-strength-reduce-constant-multiply (arg num)
579 (declare (type (unsigned-byte 32) num))
580 (let ((adds 0) (shifts 0)
581 (result nil) first-one)
582 (labels ((add (next-factor)
585 (progn (incf adds) `(+ ,result ,next-factor))
587 (declare (inline add))
590 (when (not (logbitp bitpos num))
591 (add (if (= (1+ first-one) bitpos)
592 ;; There is only a single bit in the string.
593 (progn (incf shifts) `(ash ,arg ,first-one))
594 ;; There are at least two.
598 `(- (ash ,arg ,bitpos)
599 (ash ,arg ,first-one)))))
600 (setf first-one nil))
601 (when (logbitp bitpos num)
602 (setf first-one bitpos))))
604 (cond ((= first-one 31))
605 ((= first-one 30) (incf shifts) (add `(ash ,arg 30)))
609 (add `(- (ash ,arg 31)
610 (ash ,arg ,first-one)))))
612 (add `(ash ,arg 31))))
613 (values (if (plusp adds)
614 `(logand ,result #.(1- (ash 1 32))) ; using modular arithmetic