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 (incf count (logcount bits))
378 ,(if (constant-lvar-p item)
379 (if (zerop (lvar-value item))
385 (declare (type index index count end-1)
386 (optimize (speed 3) (safety 0)))
387 (incf count (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 :unsigned)
492 (define-good-modular-fun logior :unsigned)
493 ;;; FIXME: XOR? ANDC1, ANDC2? -- CSR, 2003-09-16
496 ((def (name class width)
497 (let ((type (ecase class
498 (:unsigned 'unsigned-byte)
499 (:signed 'signed-byte))))
501 (defknown ,name (integer (integer 0)) (,type ,width)
502 (foldable flushable movable))
503 (define-modular-fun-optimizer ash ((integer count) ,class :width width)
504 (when (and (<= width ,width)
505 (or (and (constant-lvar-p count)
506 (plusp (lvar-value count)))
507 (csubtypep (lvar-type count)
508 (specifier-type '(and unsigned-byte fixnum)))))
509 (cut-to-width integer ,class width)
511 (setf (gethash ',name (modular-class-versions (find-modular-class ',class)))
513 ;; This should really be dependent on SB!VM:N-WORD-BITS, but since we
514 ;; don't have a true Alpha64 port yet, we'll have to stick to
515 ;; SB!VM:N-MACHINE-WORD-BITS for the time being. --njf, 2004-08-14
516 #!+#.(cl:if (cl:= 32 sb!vm:n-machine-word-bits) '(and) '(or))
518 #!+x86 (def sb!vm::ash-left-smod30 :signed 30)
519 (def sb!vm::ash-left-mod32 :unsigned 32))
520 #!+#.(cl:if (cl:= 64 sb!vm:n-machine-word-bits) '(and) '(or))
522 #!+x86-64 (def sb!vm::ash-left-smod61 :signed 61)
523 (def sb!vm::ash-left-mod64 :unsigned 64)))
526 ;;;; word-wise logical operations
528 ;;; These transforms assume the presence of modular arithmetic to
529 ;;; generate efficient code.
531 (define-source-transform word-logical-not (x)
532 `(logand (lognot (the sb!vm:word ,x)) #.(1- (ash 1 sb!vm:n-word-bits))))
534 (deftransform word-logical-and ((x y))
537 (deftransform word-logical-nand ((x y))
538 '(logand (lognand x y) #.(1- (ash 1 sb!vm:n-word-bits))))
540 (deftransform word-logical-or ((x y))
543 (deftransform word-logical-nor ((x y))
544 '(logand (lognor x y) #.(1- (ash 1 sb!vm:n-word-bits))))
546 (deftransform word-logical-xor ((x y))
549 (deftransform word-logical-eqv ((x y))
550 '(logand (logeqv x y) #.(1- (ash 1 sb!vm:n-word-bits))))
552 (deftransform word-logical-orc1 ((x y))
553 '(logand (logorc1 x y) #.(1- (ash 1 sb!vm:n-word-bits))))
555 (deftransform word-logical-orc2 ((x y))
556 '(logand (logorc2 x y) #.(1- (ash 1 sb!vm:n-word-bits))))
558 (deftransform word-logical-andc1 ((x y))
559 '(logand (logandc1 x y) #.(1- (ash 1 sb!vm:n-word-bits))))
561 (deftransform word-logical-andc2 ((x y))
562 '(logand (logandc2 x y) #.(1- (ash 1 sb!vm:n-word-bits))))
565 ;;; There are two different ways the multiplier can be recoded. The
566 ;;; more obvious is to shift X by the correct amount for each bit set
567 ;;; in Y and to sum the results. But if there is a string of bits that
568 ;;; are all set, you can add X shifted by one more then the bit
569 ;;; position of the first set bit and subtract X shifted by the bit
570 ;;; position of the last set bit. We can't use this second method when
571 ;;; the high order bit is bit 31 because shifting by 32 doesn't work
573 (defun ub32-strength-reduce-constant-multiply (arg num)
574 (declare (type (unsigned-byte 32) num))
575 (let ((adds 0) (shifts 0)
576 (result nil) first-one)
577 (labels ((add (next-factor)
580 (progn (incf adds) `(+ ,result ,next-factor))
582 (declare (inline add))
585 (when (not (logbitp bitpos num))
586 (add (if (= (1+ first-one) bitpos)
587 ;; There is only a single bit in the string.
588 (progn (incf shifts) `(ash ,arg ,first-one))
589 ;; There are at least two.
593 `(- (ash ,arg ,bitpos)
594 (ash ,arg ,first-one)))))
595 (setf first-one nil))
596 (when (logbitp bitpos num)
597 (setf first-one bitpos))))
599 (cond ((= first-one 31))
600 ((= first-one 30) (incf shifts) (add `(ash ,arg 30)))
604 (add `(- (ash ,arg 31)
605 (ash ,arg ,first-one)))))
607 (add `(ash ,arg 31))))
608 (values (if (plusp adds)
609 `(logand ,result #.(1- (ash 1 32))) ; using modular arithmetic