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.
39 (define-source-transform characterp (obj)
42 ;;;; simplifying HAIRY-DATA-VECTOR-REF and HAIRY-DATA-VECTOR-SET
44 (deftransform hairy-data-vector-ref ((string index) (simple-string t))
45 (let ((ctype (continuation-type string)))
46 (if (array-type-p ctype)
47 ;; the other transform will kick in, so that's OK
48 (give-up-ir1-transform)
50 ((simple-array character (*)) (data-vector-ref string index))
51 ((simple-array nil (*)) (data-vector-ref string index))))))
53 (deftransform hairy-data-vector-ref ((array index) (array t) * :important t)
54 "avoid runtime dispatch on array element type"
55 (let ((element-ctype (extract-upgraded-element-type array))
56 (declared-element-ctype (extract-declared-element-type array)))
57 (declare (type ctype element-ctype))
58 (when (eq *wild-type* element-ctype)
59 (give-up-ir1-transform
60 "Upgraded element type of array is not known at compile time."))
61 ;; (The expansion here is basically a degenerate case of
62 ;; WITH-ARRAY-DATA. Since WITH-ARRAY-DATA is implemented as a
63 ;; macro, and macros aren't expanded in transform output, we have
64 ;; to hand-expand it ourselves.)
65 (let ((element-type-specifier (type-specifier element-ctype)))
66 `(multiple-value-bind (array index)
67 (%data-vector-and-index array index)
68 (declare (type (simple-array ,element-type-specifier 1) array))
69 ,(let ((bare-form '(data-vector-ref array index)))
70 (if (type= element-ctype declared-element-ctype)
72 `(the ,(type-specifier declared-element-ctype)
75 (deftransform data-vector-ref ((array index)
77 (let ((array-type (continuation-type array)))
78 (unless (array-type-p array-type)
79 (give-up-ir1-transform))
80 (let ((dims (array-type-dimensions array-type)))
81 (when (or (atom dims) (= (length dims) 1))
82 (give-up-ir1-transform))
83 (let ((el-type (array-type-specialized-element-type array-type))
84 (total-size (if (member '* dims)
87 `(data-vector-ref (truly-the (simple-array ,(type-specifier el-type)
89 (%array-data-vector array))
92 (deftransform hairy-data-vector-set ((string index new-value)
94 (let ((ctype (continuation-type string)))
95 (if (array-type-p ctype)
96 ;; the other transform will kick in, so that's OK
97 (give-up-ir1-transform)
99 ((simple-array character (*))
100 (data-vector-set string index new-value))
101 ((simple-array nil (*))
102 (data-vector-set string index new-value))))))
104 (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 (continuation-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 (continuation-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)
158 ;; KLUDGE: why the percent signs? Well, ARRAY and INDEX are
159 ;; respectively exported from the CL and SB!INT packages, which
160 ;; means that they're visible to all sorts of things. If the
161 ;; compiler can prove that the call to ARRAY-HEADER-P, below, either
162 ;; returns T or NIL, it will delete the irrelevant branch. However,
163 ;; user code might have got here with a variable named CL:ARRAY, and
164 ;; quite often compiler code with a variable named SB!INT:INDEX, so
165 ;; this can generate code deletion notes for innocuous user code:
166 ;; (DEFUN F (ARRAY I) (DECLARE (SIMPLE-VECTOR ARRAY)) (AREF ARRAY I))
167 ;; -- CSR, 2003-04-01
169 ;; We do this solely for the -OR-GIVE-UP side effect, since we want
170 ;; to know that the type can be figured out in the end before we
171 ;; proceed, but we don't care yet what the type will turn out to be.
172 (upgraded-element-type-specifier-or-give-up %array)
174 '(if (array-header-p %array)
175 (values (%array-data-vector %array) %index)
176 (values %array %index)))
178 ;;; transforms for getting at simple arrays of (UNSIGNED-BYTE N) when (< N 8)
180 ;;; FIXME: In CMU CL, these were commented out with #+NIL. Why? Should
181 ;;; we fix them or should we delete them? (Perhaps these definitions
182 ;;; predate the various DATA-VECTOR-REF-FOO VOPs which have
183 ;;; (:TRANSLATE DATA-VECTOR-REF), and are redundant now?)
187 (let ((elements-per-word (truncate sb!vm:n-word-bits bits)))
189 (deftransform data-vector-ref ((vector index)
191 `(multiple-value-bind (word bit)
192 (floor index ,',elements-per-word)
193 (ldb ,(ecase sb!vm:target-byte-order
194 (:little-endian '(byte ,bits (* bit ,bits)))
195 (:big-endian '(byte ,bits (- sb!vm:n-word-bits
196 (* (1+ bit) ,bits)))))
197 (%raw-bits vector (+ word sb!vm:vector-data-offset)))))
198 (deftransform data-vector-set ((vector index new-value)
200 `(multiple-value-bind (word bit)
201 (floor index ,',elements-per-word)
202 (setf (ldb ,(ecase sb!vm:target-byte-order
203 (:little-endian '(byte ,bits (* bit ,bits)))
205 '(byte ,bits (- sb!vm:n-word-bits
206 (* (1+ bit) ,bits)))))
207 (%raw-bits vector (+ word sb!vm:vector-data-offset)))
209 (frob simple-bit-vector 1)
210 (frob (simple-array (unsigned-byte 2) (*)) 2)
211 (frob (simple-array (unsigned-byte 4) (*)) 4))
213 ;;;; BIT-VECTOR hackery
215 ;;; SIMPLE-BIT-VECTOR bit-array operations are transformed to a word
216 ;;; loop that does 32 bits at a time.
218 ;;; FIXME: This is a lot of repeatedly macroexpanded code. It should
219 ;;; be a function call instead.
220 (macrolet ((def (bitfun wordfun)
221 `(deftransform ,bitfun ((bit-array-1 bit-array-2 result-bit-array)
226 :node node :policy (>= speed space))
228 ,@(unless (policy node (zerop safety))
229 '((unless (= (length bit-array-1)
231 (length result-bit-array))
232 (error "Argument and/or result bit arrays are not the same length:~
237 (let ((length (length result-bit-array)))
239 ;; We avoid doing anything to 0-length
240 ;; bit-vectors, or rather, the memory that
241 ;; follows them. Other divisible-by-32 cases
242 ;; are handled by the (1- length), below.
245 (do ((index sb!vm:vector-data-offset (1+ index))
246 (end-1 (+ sb!vm:vector-data-offset
247 ;; bit-vectors of length 1-32
248 ;; need precisely one (SETF
249 ;; %RAW-BITS), done here in the
250 ;; epilogue. - CSR, 2002-04-24
251 (truncate (truly-the index (1- length))
252 sb!vm:n-word-bits))))
254 (setf (%raw-bits result-bit-array index)
255 (,',wordfun (%raw-bits bit-array-1 index)
256 (%raw-bits bit-array-2 index)))
258 (declare (optimize (speed 3) (safety 0))
259 (type index index end-1))
260 (setf (%raw-bits result-bit-array index)
261 (,',wordfun (%raw-bits bit-array-1 index)
262 (%raw-bits bit-array-2 index))))))))))
263 (def bit-and 32bit-logical-and)
264 (def bit-ior 32bit-logical-or)
265 (def bit-xor 32bit-logical-xor)
266 (def bit-eqv 32bit-logical-eqv)
267 (def bit-nand 32bit-logical-nand)
268 (def bit-nor 32bit-logical-nor)
269 (def bit-andc1 32bit-logical-andc1)
270 (def bit-andc2 32bit-logical-andc2)
271 (def bit-orc1 32bit-logical-orc1)
272 (def bit-orc2 32bit-logical-orc2))
274 (deftransform bit-not
275 ((bit-array result-bit-array)
276 (simple-bit-vector simple-bit-vector) *
277 :node node :policy (>= speed space))
279 ,@(unless (policy node (zerop safety))
280 '((unless (= (length bit-array)
281 (length result-bit-array))
282 (error "Argument and result bit arrays are not the same length:~
284 bit-array result-bit-array))))
285 (let ((length (length result-bit-array)))
287 ;; We avoid doing anything to 0-length bit-vectors, or
288 ;; rather, the memory that follows them. Other
289 ;; divisible-by-32 cases are handled by the (1- length),
290 ;; below. CSR, 2002-04-24
292 (do ((index sb!vm:vector-data-offset (1+ index))
293 (end-1 (+ sb!vm:vector-data-offset
294 ;; bit-vectors of length 1-32 need precisely
295 ;; one (SETF %RAW-BITS), done here in the
296 ;; epilogue. - CSR, 2002-04-24
297 (truncate (truly-the index (1- length))
298 sb!vm:n-word-bits))))
300 (setf (%raw-bits result-bit-array index)
301 (32bit-logical-not (%raw-bits bit-array index)))
303 (declare (optimize (speed 3) (safety 0))
304 (type index index end-1))
305 (setf (%raw-bits result-bit-array index)
306 (32bit-logical-not (%raw-bits bit-array index))))))))
308 (deftransform bit-vector-= ((x y) (simple-bit-vector simple-bit-vector))
309 `(and (= (length x) (length y))
310 (let ((length (length x)))
312 (do* ((i sb!vm:vector-data-offset (+ i 1))
313 (end-1 (+ sb!vm:vector-data-offset
314 (floor (1- length) sb!vm:n-word-bits))))
316 (let* ((extra (mod length sb!vm:n-word-bits))
317 (mask (1- (ash 1 extra)))
321 ,(ecase sb!c:*backend-byte-order*
324 '(- sb!vm:n-word-bits extra))))
329 ,(ecase sb!c:*backend-byte-order*
332 '(- sb!vm:n-word-bits extra))))
334 (declare (type (integer 0 31) extra)
335 (type (unsigned-byte 32) 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 (unsigned-byte 32) numx numy))
341 (unless (= numx numy)
344 (deftransform fill ((sequence item) (simple-bit-vector bit) *
345 :policy (>= speed space))
346 (let ((value (if (constant-continuation-p item)
347 (if (= (continuation-value item) 0)
350 `(if (= item 0) 0 #.(1- (ash 1 32))))))
351 `(let ((length (length sequence))
355 (do ((index sb!vm:vector-data-offset (1+ index))
356 (end-1 (+ sb!vm:vector-data-offset
357 ;; bit-vectors of length 1-32 need precisely
358 ;; one (SETF %RAW-BITS), done here in the
359 ;; epilogue. - CSR, 2002-04-24
360 (truncate (truly-the index (1- length))
361 sb!vm:n-word-bits))))
363 (setf (%raw-bits sequence index) value)
365 (declare (optimize (speed 3) (safety 0))
366 (type index index end-1))
367 (setf (%raw-bits sequence index) value))))))
369 (deftransform fill ((sequence item) (simple-base-string base-char) *
370 :policy (>= speed space))
371 (let ((value (if (constant-continuation-p item)
372 (let* ((char (continuation-value item))
373 (code (sb!xc:char-code char)))
374 (logior code (ash code 8) (ash code 16) (ash code 24)))
375 `(let ((code (sb!xc:char-code item)))
376 (logior code (ash code 8) (ash code 16) (ash code 24))))))
377 `(let ((length (length sequence))
379 (multiple-value-bind (times rem)
381 (do ((index sb!vm:vector-data-offset (1+ index))
382 (end (+ times sb!vm:vector-data-offset)))
384 (let ((place (* times 4)))
385 (declare (fixnum place))
386 (dotimes (j rem sequence)
388 (setf (schar sequence (the index (+ place j))) item))))
389 (declare (optimize (speed 3) (safety 0))
391 (setf (%raw-bits sequence index) value))))))
395 ;;; FIXME: The old CMU CL code used various COPY-TO/FROM-SYSTEM-AREA
396 ;;; stuff (with all the associated bit-index cruft and overflow
397 ;;; issues) even for byte moves. In SBCL, we're converting to byte
398 ;;; moves as problems are discovered with the old code, and this is
399 ;;; currently (ca. sbcl-0.6.12.30) the main interface for code in
400 ;;; SB!KERNEL and SB!SYS (e.g. i/o code). It's not clear that it's the
401 ;;; ideal interface, though, and it probably deserves some thought.
402 (deftransform %byte-blt ((src src-start dst dst-start dst-end)
403 ((or (simple-unboxed-array (*)) system-area-pointer)
405 (or (simple-unboxed-array (*)) system-area-pointer)
408 ;; FIXME: CMU CL had a hairier implementation of this (back when it
409 ;; was still called (%PRIMITIVE BYTE-BLT). It had the small problem
410 ;; that it didn't work for large (>16M) values of SRC-START or
411 ;; DST-START. However, it might have been more efficient. In
412 ;; particular, I don't really know how much the foreign function
413 ;; call costs us here. My guess is that if the overhead is
414 ;; acceptable for SQRT and COS, it's acceptable here, but this
415 ;; should probably be checked. -- WHN
416 '(flet ((sapify (thing)
418 (system-area-pointer thing)
419 ;; FIXME: The code here rather relies on the simple
420 ;; unboxed array here having byte-sized entries. That
421 ;; should be asserted explicitly, I just haven't found
422 ;; a concise way of doing it. (It would be nice to
423 ;; declare it in the DEFKNOWN too.)
424 ((simple-unboxed-array (*)) (vector-sap thing)))))
425 (declare (inline sapify))
427 (memmove (sap+ (sapify dst) dst-start)
428 (sap+ (sapify src) src-start)
429 (- dst-end dst-start)))
432 ;;;; transforms for EQL of floating point values
434 (deftransform eql ((x y) (single-float single-float))
435 '(= (single-float-bits x) (single-float-bits y)))
437 (deftransform eql ((x y) (double-float double-float))
438 '(and (= (double-float-low-bits x) (double-float-low-bits y))
439 (= (double-float-high-bits x) (double-float-high-bits y))))
442 ;;;; 32-bit operations
443 #!-x86 ; on X86 it is a modular function
444 (deftransform lognot ((x) ((unsigned-byte 32)) *
447 "32-bit implementation"
448 (let ((dest (continuation-dest result)))
449 (unless (and (combination-p dest)
450 (eq (continuation-fun-name (combination-fun dest))
452 (give-up-ir1-transform))
453 (unless (some (lambda (arg)
454 (csubtypep (continuation-type arg)
455 (specifier-type '(unsigned-byte 32))))
456 (combination-args dest))
457 (give-up-ir1-transform))
458 (setf (node-derived-type node)
459 (values-specifier-type '(values (unsigned-byte 32) &optional)))
460 '(32bit-logical-not x)))
462 (define-good-modular-fun logand)
463 (define-good-modular-fun logior)
465 ;;; There are two different ways the multiplier can be recoded. The
466 ;;; more obvious is to shift X by the correct amount for each bit set
467 ;;; in Y and to sum the results. But if there is a string of bits that
468 ;;; are all set, you can add X shifted by one more then the bit
469 ;;; position of the first set bit and subtract X shifted by the bit
470 ;;; position of the last set bit. We can't use this second method when
471 ;;; the high order bit is bit 31 because shifting by 32 doesn't work
473 (defun ub32-strength-reduce-constant-multiply (arg num)
474 (declare (type (unsigned-byte 32) numb))
475 (let ((adds 0) (shifts 0)
476 (result nil) first-one)
477 (labels ((tub32 (x) `(truly-the (unsigned-byte 32) ,x))
482 (progn (incf adds) `(+ ,result ,(tub32 next-factor)))
484 (declare (inline add))
487 (when (not (logbitp bitpos num))
488 (add (if (= (1+ first-one) bitpos)
489 ;; There is only a single bit in the string.
490 (progn (incf shifts) `(ash ,arg ,first-one))
491 ;; There are at least two.
495 `(- ,(tub32 `(ash ,arg ,bitpos))
496 ,(tub32 `(ash ,arg ,first-one))))))
497 (setf first-one nil))
498 (when (logbitp bitpos num)
499 (setf first-one bitpos))))
501 (cond ((= first-one 31))
502 ((= first-one 30) (incf shifts) (add `(ash ,arg 30)))
506 (add `(- ,(tub32 `(ash ,arg 31))
507 ,(tub32 `(ash ,arg ,first-one))))))
509 (add `(ash ,arg 31))))
510 (values result adds shifts)))