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 (1+ (mod (1- length) sb!vm:n-word-bits)))
321 (mask (ash #.(1- (ash 1 sb!vm:n-word-bits))
322 (- extra sb!vm:n-word-bits)))
326 ,(ecase sb!c:*backend-byte-order*
329 '(- sb!vm:n-word-bits extra))))
334 ,(ecase sb!c:*backend-byte-order*
337 '(- sb!vm:n-word-bits extra))))
339 (declare (type (integer 1 #.sb!vm:n-word-bits) extra)
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 (1+ (mod (1- length) sb!vm:n-word-bits)))
361 (mask (ash #.(1- (ash 1 sb!vm:n-word-bits))
362 (- extra sb!vm:n-word-bits)))
363 (bits (logand (ash mask
364 ,(ecase sb!c:*backend-byte-order*
367 '(- sb!vm:n-word-bits extra))))
368 (%raw-bits sequence index))))
369 (declare (type (integer 1 #.sb!vm:n-word-bits) extra))
370 (declare (type sb!vm:word mask bits))
371 ;; could consider LOGNOT for the zero case instead of
372 ;; doing the subtraction...
373 (incf count ,(if (constant-lvar-p item)
374 (if (zerop (lvar-value item))
375 '(- extra (logcount bits))
378 (- extra (logcount bits))
380 (declare (type index index count end-1)
381 (optimize (speed 3) (safety 0)))
382 (incf count ,(if (constant-lvar-p item)
383 (if (zerop (lvar-value item))
384 '(- sb!vm:n-word-bits (logcount (%raw-bits sequence index)))
385 '(logcount (%raw-bits sequence index)))
387 (- sb!vm:n-word-bits (logcount (%raw-bits sequence index)))
388 (logcount (%raw-bits sequence index)))))))))
390 (deftransform fill ((sequence item) (simple-bit-vector bit) *
391 :policy (>= speed space))
392 (let ((value (if (constant-lvar-p item)
393 (if (= (lvar-value item) 0)
395 #.(1- (ash 1 sb!vm:n-word-bits)))
396 `(if (= item 0) 0 #.(1- (ash 1 sb!vm:n-word-bits))))))
397 `(let ((length (length sequence))
401 (do ((index sb!vm:vector-data-offset (1+ index))
402 (end-1 (+ sb!vm:vector-data-offset
403 ;; bit-vectors of length 1 to n-word-bits need
404 ;; precisely one (SETF %RAW-BITS), done here
405 ;; in the epilogue. - CSR, 2002-04-24
406 (truncate (truly-the index (1- length))
407 sb!vm:n-word-bits))))
409 (setf (%raw-bits sequence index) value)
411 (declare (optimize (speed 3) (safety 0))
412 (type index index end-1))
413 (setf (%raw-bits sequence index) value))))))
415 (deftransform fill ((sequence item) (simple-base-string base-char) *
416 :policy (>= speed space))
417 (let ((value (if (constant-lvar-p item)
418 (let* ((char (lvar-value item))
419 (code (sb!xc:char-code char))
421 (dotimes (i sb!vm:n-word-bytes accum)
422 (setf accum (logior accum (ash code (* 8 i))))))
423 `(let ((code (sb!xc:char-code item)))
424 (logior ,@(loop for i from 0 below sb!vm:n-word-bytes
425 collect `(ash code ,(* 8 i))))))))
426 `(let ((length (length sequence))
428 (multiple-value-bind (times rem)
429 (truncate length sb!vm:n-word-bytes)
430 (do ((index sb!vm:vector-data-offset (1+ index))
431 (end (+ times sb!vm:vector-data-offset)))
433 (let ((place (* times sb!vm:n-word-bytes)))
434 (declare (fixnum place))
435 (dotimes (j rem sequence)
437 (setf (schar sequence (the index (+ place j))) item))))
438 (declare (optimize (speed 3) (safety 0))
440 (setf (%raw-bits sequence index) value))))))
444 ;;; FIXME: The old CMU CL code used various COPY-TO/FROM-SYSTEM-AREA
445 ;;; stuff (with all the associated bit-index cruft and overflow
446 ;;; issues) even for byte moves. In SBCL, we're converting to byte
447 ;;; moves as problems are discovered with the old code, and this is
448 ;;; currently (ca. sbcl-0.6.12.30) the main interface for code in
449 ;;; SB!KERNEL and SB!SYS (e.g. i/o code). It's not clear that it's the
450 ;;; ideal interface, though, and it probably deserves some thought.
451 (deftransform %byte-blt ((src src-start dst dst-start dst-end)
452 ((or (simple-unboxed-array (*)) system-area-pointer)
454 (or (simple-unboxed-array (*)) system-area-pointer)
457 ;; FIXME: CMU CL had a hairier implementation of this (back when it
458 ;; was still called (%PRIMITIVE BYTE-BLT). It had the small problem
459 ;; that it didn't work for large (>16M) values of SRC-START or
460 ;; DST-START. However, it might have been more efficient. In
461 ;; particular, I don't really know how much the foreign function
462 ;; call costs us here. My guess is that if the overhead is
463 ;; acceptable for SQRT and COS, it's acceptable here, but this
464 ;; should probably be checked. -- WHN
465 '(flet ((sapify (thing)
467 (system-area-pointer thing)
468 ;; FIXME: The code here rather relies on the simple
469 ;; unboxed array here having byte-sized entries. That
470 ;; should be asserted explicitly, I just haven't found
471 ;; a concise way of doing it. (It would be nice to
472 ;; declare it in the DEFKNOWN too.)
473 ((simple-unboxed-array (*)) (vector-sap thing)))))
474 (declare (inline sapify))
476 (memmove (sap+ (sapify dst) dst-start)
477 (sap+ (sapify src) src-start)
478 (- dst-end dst-start)))
481 ;;;; transforms for EQL of floating point values
483 (deftransform eql ((x y) (single-float single-float))
484 '(= (single-float-bits x) (single-float-bits y)))
486 (deftransform eql ((x y) (double-float double-float))
487 '(and (= (double-float-low-bits x) (double-float-low-bits y))
488 (= (double-float-high-bits x) (double-float-high-bits y))))
491 ;;;; modular functions
492 (define-good-modular-fun logand :unsigned)
493 (define-good-modular-fun logior :unsigned)
494 ;;; FIXME: XOR? ANDC1, ANDC2? -- CSR, 2003-09-16
497 ((def (name class width)
498 (let ((type (ecase class
499 (:unsigned 'unsigned-byte)
500 (:signed 'signed-byte))))
502 (defknown ,name (integer (integer 0)) (,type ,width)
503 (foldable flushable movable))
504 (define-modular-fun-optimizer ash ((integer count) ,class :width width)
505 (when (and (<= width ,width)
506 (or (and (constant-lvar-p count)
507 (plusp (lvar-value count)))
508 (csubtypep (lvar-type count)
509 (specifier-type '(and unsigned-byte fixnum)))))
510 (cut-to-width integer ,class width)
512 (setf (gethash ',name (modular-class-versions (find-modular-class ',class)))
514 ;; This should really be dependent on SB!VM:N-WORD-BITS, but since we
515 ;; don't have a true Alpha64 port yet, we'll have to stick to
516 ;; SB!VM:N-MACHINE-WORD-BITS for the time being. --njf, 2004-08-14
517 #!+#.(cl:if (cl:= 32 sb!vm:n-machine-word-bits) '(and) '(or))
519 #!+x86 (def sb!vm::ash-left-smod30 :signed 30)
520 (def sb!vm::ash-left-mod32 :unsigned 32))
521 #!+#.(cl:if (cl:= 64 sb!vm:n-machine-word-bits) '(and) '(or))
523 #!+x86-64 (def sb!vm::ash-left-smod61 :signed 61)
524 (def sb!vm::ash-left-mod64 :unsigned 64)))
527 ;;;; word-wise logical operations
529 ;;; These transforms assume the presence of modular arithmetic to
530 ;;; generate efficient code.
532 (define-source-transform word-logical-not (x)
533 `(logand (lognot (the sb!vm:word ,x)) #.(1- (ash 1 sb!vm:n-word-bits))))
535 (deftransform word-logical-and ((x y))
538 (deftransform word-logical-nand ((x y))
539 '(logand (lognand x y) #.(1- (ash 1 sb!vm:n-word-bits))))
541 (deftransform word-logical-or ((x y))
544 (deftransform word-logical-nor ((x y))
545 '(logand (lognor x y) #.(1- (ash 1 sb!vm:n-word-bits))))
547 (deftransform word-logical-xor ((x y))
550 (deftransform word-logical-eqv ((x y))
551 '(logand (logeqv x y) #.(1- (ash 1 sb!vm:n-word-bits))))
553 (deftransform word-logical-orc1 ((x y))
554 '(logand (logorc1 x y) #.(1- (ash 1 sb!vm:n-word-bits))))
556 (deftransform word-logical-orc2 ((x y))
557 '(logand (logorc2 x y) #.(1- (ash 1 sb!vm:n-word-bits))))
559 (deftransform word-logical-andc1 ((x y))
560 '(logand (logandc1 x y) #.(1- (ash 1 sb!vm:n-word-bits))))
562 (deftransform word-logical-andc2 ((x y))
563 '(logand (logandc2 x y) #.(1- (ash 1 sb!vm:n-word-bits))))
566 ;;; There are two different ways the multiplier can be recoded. The
567 ;;; more obvious is to shift X by the correct amount for each bit set
568 ;;; in Y and to sum the results. But if there is a string of bits that
569 ;;; are all set, you can add X shifted by one more then the bit
570 ;;; position of the first set bit and subtract X shifted by the bit
571 ;;; position of the last set bit. We can't use this second method when
572 ;;; the high order bit is bit 31 because shifting by 32 doesn't work
574 (defun ub32-strength-reduce-constant-multiply (arg num)
575 (declare (type (unsigned-byte 32) num))
576 (let ((adds 0) (shifts 0)
577 (result nil) first-one)
578 (labels ((add (next-factor)
581 (progn (incf adds) `(+ ,result ,next-factor))
583 (declare (inline add))
586 (when (not (logbitp bitpos num))
587 (add (if (= (1+ first-one) bitpos)
588 ;; There is only a single bit in the string.
589 (progn (incf shifts) `(ash ,arg ,first-one))
590 ;; There are at least two.
594 `(- (ash ,arg ,bitpos)
595 (ash ,arg ,first-one)))))
596 (setf first-one nil))
597 (when (logbitp bitpos num)
598 (setf first-one bitpos))))
600 (cond ((= first-one 31))
601 ((= first-one 30) (incf shifts) (add `(ash ,arg 30)))
605 (add `(- (ash ,arg 31)
606 (ash ,arg ,first-one)))))
608 (add `(ash ,arg 31))))
609 (values (if (plusp adds)
610 `(logand ,result #.(1- (ash 1 32))) ; using modular arithmetic