Fix make-array transforms.
[sbcl.git] / generic / vm-tran.lisp
1 ;;;; implementation-dependent transforms
2
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
5 ;;;;
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.
11
12 (in-package "SB!C")
13
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))
18 #!-long-float
19 (define-source-transform long-float-p (x) `(double-float-p ,x))
20
21 (define-source-transform compiled-function-p (x)
22   #!-sb-eval
23   `(functionp ,x)
24   #!+sb-eval
25   (once-only ((x x))
26     `(and (functionp ,x)
27           (not (sb!eval:interpreted-function-p ,x)))))
28
29 (define-source-transform char-int (x)
30   `(char-code ,x))
31
32 (deftransform abs ((x) (rational))
33   '(if (< x 0) (- x) x))
34
35 ;;; We don't want to clutter the bignum code.
36 #!+(or x86 x86-64)
37 (define-source-transform sb!bignum:%bignum-ref (bignum index)
38   ;; KLUDGE: We use TRULY-THE here because even though the bignum code
39   ;; is (currently) compiled with (SAFETY 0), the compiler insists on
40   ;; inserting CAST nodes to ensure that INDEX is of the correct type.
41   ;; These CAST nodes do not generate any type checks, but they do
42   ;; interfere with the operation of FOLD-INDEX-ADDRESSING, below.
43   ;; This scenario is a problem for the more user-visible case of
44   ;; folding as well.  --njf, 2006-12-01
45   `(sb!bignum:%bignum-ref-with-offset ,bignum
46                                       (truly-the bignum-index ,index) 0))
47
48 #!+(or x86 x86-64)
49 (defun fold-index-addressing (fun-name element-size lowtag data-offset
50                               index offset &optional setter-p)
51   (multiple-value-bind (func index-args) (extract-fun-args index '(+ -) 2)
52     (destructuring-bind (x constant) index-args
53       (declare (ignorable x))
54       (unless (constant-lvar-p constant)
55         (give-up-ir1-transform))
56       (let ((value (lvar-value constant)))
57         (unless (and (integerp value)
58                      (sb!vm::foldable-constant-offset-p
59                       element-size lowtag data-offset
60                       (funcall func value (lvar-value offset))))
61           (give-up-ir1-transform "constant is too large for inlining"))
62         (splice-fun-args index func 2)
63         `(lambda (thing index off1 off2 ,@(when setter-p
64                                             '(value)))
65            (,fun-name thing index (,func off2 off1) ,@(when setter-p
66                                                         '(value))))))))
67
68 #!+(or x86 x86-64)
69 (deftransform sb!bignum:%bignum-ref-with-offset
70     ((bignum index offset) * * :node node)
71   (fold-index-addressing 'sb!bignum:%bignum-ref-with-offset
72                          sb!vm:n-word-bits sb!vm:other-pointer-lowtag
73                          sb!vm:bignum-digits-offset
74                          index offset))
75
76 ;;; The layout is stored in slot 0.
77 (define-source-transform %instance-layout (x)
78   `(truly-the layout (%instance-ref ,x 0)))
79 (define-source-transform %set-instance-layout (x val)
80   `(%instance-set ,x 0 (the layout ,val)))
81 (define-source-transform %funcallable-instance-layout (x)
82   `(truly-the layout (%funcallable-instance-info ,x 0)))
83 (define-source-transform %set-funcallable-instance-layout (x val)
84   `(setf (%funcallable-instance-info ,x 0) (the layout ,val)))
85 \f
86 ;;;; character support
87
88 ;;; In our implementation there are really only BASE-CHARs.
89 #+nil
90 (define-source-transform characterp (obj)
91   `(base-char-p ,obj))
92 \f
93 ;;;; simplifying HAIRY-DATA-VECTOR-REF and HAIRY-DATA-VECTOR-SET
94
95 (deftransform hairy-data-vector-ref ((string index) (simple-string t))
96   (let ((ctype (lvar-type string)))
97     (if (array-type-p ctype)
98         ;; the other transform will kick in, so that's OK
99         (give-up-ir1-transform)
100         `(etypecase string
101           ((simple-array character (*))
102            (data-vector-ref string index))
103           #!+sb-unicode
104           ((simple-array base-char (*))
105            (data-vector-ref string index))
106           ((simple-array nil (*))
107            (data-vector-ref string index))))))
108
109 ;;; This and the corresponding -SET transform work equally well on non-simple
110 ;;; arrays, but after benchmarking (on x86), Nikodemus didn't find any cases
111 ;;; where it actually helped with non-simple arrays -- to the contrary, it
112 ;;; only made for bigger and up 1o 100% slower code.
113 (deftransform hairy-data-vector-ref ((array index) (simple-array t) *)
114   "avoid runtime dispatch on array element type"
115   (let ((element-ctype (extract-upgraded-element-type array))
116         (declared-element-ctype (extract-declared-element-type array)))
117     (declare (type ctype element-ctype))
118     (when (eq *wild-type* element-ctype)
119       (give-up-ir1-transform
120        "Upgraded element type of array is not known at compile time."))
121     ;; (The expansion here is basically a degenerate case of
122     ;; WITH-ARRAY-DATA. Since WITH-ARRAY-DATA is implemented as a
123     ;; macro, and macros aren't expanded in transform output, we have
124     ;; to hand-expand it ourselves.)
125     (let* ((element-type-specifier (type-specifier element-ctype)))
126       `(multiple-value-bind (array index)
127            (%data-vector-and-index array index)
128          (declare (type (simple-array ,element-type-specifier 1) array))
129          ,(let ((bare-form '(data-vector-ref array index)))
130             (if (type= element-ctype declared-element-ctype)
131                 bare-form
132                 `(the ,(type-specifier declared-element-ctype)
133                       ,bare-form)))))))
134
135 ;;; Transform multi-dimensional array to one dimensional data vector
136 ;;; access.
137 (deftransform data-vector-ref ((array index) (simple-array t))
138   (let ((array-type (lvar-type array)))
139     (unless (array-type-p array-type)
140       (give-up-ir1-transform))
141     (let ((dims (array-type-dimensions array-type)))
142       (when (or (atom dims) (= (length dims) 1))
143         (give-up-ir1-transform))
144       (let ((el-type (array-type-specialized-element-type array-type))
145             (total-size (if (member '* dims)
146                             '*
147                             (reduce #'* dims))))
148         `(data-vector-ref (truly-the (simple-array ,(type-specifier el-type)
149                                                    (,total-size))
150                                      (%array-data-vector array))
151                           index)))))
152
153 ;;; Transform data vector access to a form that opens up optimization
154 ;;; opportunities. On platforms that support DATA-VECTOR-REF-WITH-OFFSET
155 ;;; DATA-VECTOR-REF is not supported at all.
156 #!+(or x86 x86-64)
157 (define-source-transform data-vector-ref (array index)
158   `(data-vector-ref-with-offset ,array ,index 0))
159
160 #!+(or x86 x86-64)
161 (deftransform data-vector-ref-with-offset ((array index offset))
162   (let ((array-type (lvar-type array)))
163     (when (or (not (array-type-p array-type))
164               (eql (array-type-specialized-element-type array-type)
165                    *wild-type*))
166       (give-up-ir1-transform))
167     ;; It shouldn't be possible to get here with anything but a non-complex
168     ;; vector.
169     (aver (not (array-type-complexp array-type)))
170     (let* ((element-type (type-specifier (array-type-specialized-element-type array-type)))
171            (saetp (find-saetp element-type)))
172       (when (< (sb!vm:saetp-n-bits saetp) sb!vm:n-byte-bits)
173         (give-up-ir1-transform))
174       (fold-index-addressing 'data-vector-ref-with-offset
175                              (sb!vm:saetp-n-bits saetp)
176                              sb!vm:other-pointer-lowtag
177                              sb!vm:vector-data-offset
178                              index offset))))
179
180 (deftransform hairy-data-vector-set ((string index new-value)
181                                      (simple-string t t))
182   (let ((ctype (lvar-type string)))
183     (if (array-type-p ctype)
184         ;; the other transform will kick in, so that's OK
185         (give-up-ir1-transform)
186         `(etypecase string
187           ((simple-array character (*))
188            (data-vector-set string index new-value))
189           #!+sb-unicode
190           ((simple-array base-char (*))
191            (data-vector-set string index new-value))
192           ((simple-array nil (*))
193            (data-vector-set string index new-value))))))
194
195 ;;; This and the corresponding -REF transform work equally well on non-simple
196 ;;; arrays, but after benchmarking (on x86), Nikodemus didn't find any cases
197 ;;; where it actually helped with non-simple arrays -- to the contrary, it
198 ;;; only made for bigger and up 1o 100% slower code.
199 (deftransform hairy-data-vector-set ((array index new-value)
200                                      (simple-array t t)
201                                      *)
202   "avoid runtime dispatch on array element type"
203   (let ((element-ctype (extract-upgraded-element-type array))
204         (declared-element-ctype (extract-declared-element-type array)))
205     (declare (type ctype element-ctype))
206     (when (eq *wild-type* element-ctype)
207       (give-up-ir1-transform
208        "Upgraded element type of array is not known at compile time."))
209     (let ((element-type-specifier (type-specifier element-ctype)))
210       `(multiple-value-bind (array index)
211            (%data-vector-and-index array index)
212          (declare (type (simple-array ,element-type-specifier 1) array)
213                   (type ,element-type-specifier new-value))
214          ,(if (type= element-ctype declared-element-ctype)
215               '(data-vector-set array index new-value)
216               `(truly-the ,(type-specifier declared-element-ctype)
217                  (data-vector-set array index
218                   (the ,(type-specifier declared-element-ctype)
219                        new-value))))))))
220
221 ;;; Transform multi-dimensional array to one dimensional data vector
222 ;;; access.
223 (deftransform data-vector-set ((array index new-value)
224                                (simple-array t t))
225   (let ((array-type (lvar-type array)))
226     (unless (array-type-p array-type)
227       (give-up-ir1-transform))
228     (let ((dims (array-type-dimensions array-type)))
229       (when (or (atom dims) (= (length dims) 1))
230         (give-up-ir1-transform))
231       (let ((el-type (array-type-specialized-element-type array-type))
232             (total-size (if (member '* dims)
233                             '*
234                             (reduce #'* dims))))
235         `(data-vector-set (truly-the (simple-array ,(type-specifier el-type)
236                                                    (,total-size))
237                                      (%array-data-vector array))
238                           index
239                           new-value)))))
240
241 ;;; Transform data vector access to a form that opens up optimization
242 ;;; opportunities.
243 #!+(or x86 x86-64)
244 (define-source-transform data-vector-set (array index new-value)
245   `(data-vector-set-with-offset ,array ,index 0 ,new-value))
246
247 #!+(or x86 x86-64)
248 (deftransform data-vector-set-with-offset ((array index offset new-value))
249   (let ((array-type (lvar-type array)))
250     (when (or (not (array-type-p array-type))
251               (eql (array-type-specialized-element-type array-type)
252                    *wild-type*))
253       ;; We don't yet know the exact element type, but will get that
254       ;; knowledge after some more type propagation.
255       (give-up-ir1-transform))
256     (aver (not (array-type-complexp array-type)))
257     (let* ((element-type (type-specifier (array-type-specialized-element-type array-type)))
258            (saetp (find-saetp element-type)))
259       (when (< (sb!vm:saetp-n-bits saetp) sb!vm:n-byte-bits)
260         (give-up-ir1-transform))
261       (fold-index-addressing 'data-vector-set-with-offset
262                              (sb!vm:saetp-n-bits saetp)
263                              sb!vm:other-pointer-lowtag
264                              sb!vm:vector-data-offset
265                              index offset t))))
266
267 (defoptimizer (%data-vector-and-index derive-type) ((array index))
268   (let ((atype (lvar-type array)))
269     (when (array-type-p atype)
270       (values-specifier-type
271        `(values (simple-array ,(type-specifier
272                                 (array-type-specialized-element-type atype))
273                               (*))
274                 index)))))
275
276 (deftransform %data-vector-and-index ((%array %index)
277                                       (simple-array t)
278                                       *)
279   ;; KLUDGE: why the percent signs?  Well, ARRAY and INDEX are
280   ;; respectively exported from the CL and SB!INT packages, which
281   ;; means that they're visible to all sorts of things.  If the
282   ;; compiler can prove that the call to ARRAY-HEADER-P, below, either
283   ;; returns T or NIL, it will delete the irrelevant branch.  However,
284   ;; user code might have got here with a variable named CL:ARRAY, and
285   ;; quite often compiler code with a variable named SB!INT:INDEX, so
286   ;; this can generate code deletion notes for innocuous user code:
287   ;; (DEFUN F (ARRAY I) (DECLARE (SIMPLE-VECTOR ARRAY)) (AREF ARRAY I))
288   ;; -- CSR, 2003-04-01
289
290   ;; We do this solely for the -OR-GIVE-UP side effect, since we want
291   ;; to know that the type can be figured out in the end before we
292   ;; proceed, but we don't care yet what the type will turn out to be.
293   (upgraded-element-type-specifier-or-give-up %array)
294
295   '(if (array-header-p %array)
296        (values (%array-data-vector %array) %index)
297        (values %array %index)))
298 \f
299 ;;;; BIT-VECTOR hackery
300
301 ;;; SIMPLE-BIT-VECTOR bit-array operations are transformed to a word
302 ;;; loop that does 32 bits at a time.
303 ;;;
304 ;;; FIXME: This is a lot of repeatedly macroexpanded code. It should
305 ;;; be a function call instead.
306 (macrolet ((def (bitfun wordfun)
307              `(deftransform ,bitfun ((bit-array-1 bit-array-2 result-bit-array)
308                                      (simple-bit-vector
309                                       simple-bit-vector
310                                       simple-bit-vector)
311                                      *
312                                      :node node :policy (>= speed space))
313                 `(progn
314                    ,@(unless (policy node (zerop safety))
315                              '((unless (= (length bit-array-1)
316                                           (length bit-array-2)
317                                           (length result-bit-array))
318                                  (error "Argument and/or result bit arrays are not the same length:~
319                          ~%  ~S~%  ~S  ~%  ~S"
320                                         bit-array-1
321                                         bit-array-2
322                                         result-bit-array))))
323                   (let ((length (length result-bit-array)))
324                     (if (= length 0)
325                         ;; We avoid doing anything to 0-length
326                         ;; bit-vectors, or rather, the memory that
327                         ;; follows them. Other divisible-by-32 cases
328                         ;; are handled by the (1- length), below.
329                         ;; CSR, 2002-04-24
330                         result-bit-array
331                         (do ((index 0 (1+ index))
332                              ;; bit-vectors of length 1-32 need
333                              ;; precisely one (SETF %VECTOR-RAW-BITS),
334                              ;; done here in the epilogue. - CSR,
335                              ;; 2002-04-24
336                              (end-1 (truncate (truly-the index (1- length))
337                                               sb!vm:n-word-bits)))
338                             ((>= index end-1)
339                              (setf (%vector-raw-bits result-bit-array index)
340                                    (,',wordfun (%vector-raw-bits bit-array-1 index)
341                                                (%vector-raw-bits bit-array-2 index)))
342                              result-bit-array)
343                           (declare (optimize (speed 3) (safety 0))
344                                    (type index index end-1))
345                           (setf (%vector-raw-bits result-bit-array index)
346                                 (,',wordfun (%vector-raw-bits bit-array-1 index)
347                                             (%vector-raw-bits bit-array-2 index))))))))))
348  (def bit-and word-logical-and)
349  (def bit-ior word-logical-or)
350  (def bit-xor word-logical-xor)
351  (def bit-eqv word-logical-eqv)
352  (def bit-nand word-logical-nand)
353  (def bit-nor word-logical-nor)
354  (def bit-andc1 word-logical-andc1)
355  (def bit-andc2 word-logical-andc2)
356  (def bit-orc1 word-logical-orc1)
357  (def bit-orc2 word-logical-orc2))
358
359 (deftransform bit-not
360               ((bit-array result-bit-array)
361                (simple-bit-vector simple-bit-vector) *
362                :node node :policy (>= speed space))
363   `(progn
364      ,@(unless (policy node (zerop safety))
365          '((unless (= (length bit-array)
366                       (length result-bit-array))
367              (error "Argument and result bit arrays are not the same length:~
368                      ~%  ~S~%  ~S"
369                     bit-array result-bit-array))))
370     (let ((length (length result-bit-array)))
371       (if (= length 0)
372           ;; We avoid doing anything to 0-length bit-vectors, or rather,
373           ;; the memory that follows them. Other divisible-by
374           ;; n-word-bits cases are handled by the (1- length), below.
375           ;; CSR, 2002-04-24
376           result-bit-array
377           (do ((index 0 (1+ index))
378                ;; bit-vectors of length 1 to n-word-bits need precisely
379                ;; one (SETF %VECTOR-RAW-BITS), done here in the
380                ;; epilogue. - CSR, 2002-04-24
381                (end-1 (truncate (truly-the index (1- length))
382                                 sb!vm:n-word-bits)))
383               ((>= index end-1)
384                (setf (%vector-raw-bits result-bit-array index)
385                      (word-logical-not (%vector-raw-bits bit-array index)))
386                result-bit-array)
387             (declare (optimize (speed 3) (safety 0))
388                      (type index index end-1))
389             (setf (%vector-raw-bits result-bit-array index)
390                   (word-logical-not (%vector-raw-bits bit-array index))))))))
391
392 (deftransform bit-vector-= ((x y) (simple-bit-vector simple-bit-vector))
393   `(and (= (length x) (length y))
394         (let ((length (length x)))
395           (or (= length 0)
396               (do* ((i 0 (+ i 1))
397                     (end-1 (floor (1- length) sb!vm:n-word-bits)))
398                    ((>= i end-1)
399                     (let* ((extra (1+ (mod (1- length) sb!vm:n-word-bits)))
400                            (mask (ash #.(1- (ash 1 sb!vm:n-word-bits))
401                                       (- extra sb!vm:n-word-bits)))
402                            (numx
403                             (logand
404                              (ash mask
405                                   ,(ecase sb!c:*backend-byte-order*
406                                      (:little-endian 0)
407                                      (:big-endian
408                                       '(- sb!vm:n-word-bits extra))))
409                              (%vector-raw-bits x i)))
410                            (numy
411                             (logand
412                              (ash mask
413                                   ,(ecase sb!c:*backend-byte-order*
414                                      (:little-endian 0)
415                                      (:big-endian
416                                       '(- sb!vm:n-word-bits extra))))
417                              (%vector-raw-bits y i))))
418                       (declare (type (integer 1 #.sb!vm:n-word-bits) extra)
419                                (type sb!vm:word mask numx numy))
420                       (= numx numy)))
421                 (declare (type index i end-1))
422                 (let ((numx (%vector-raw-bits x i))
423                       (numy (%vector-raw-bits y i)))
424                   (declare (type sb!vm:word numx numy))
425                   (unless (= numx numy)
426                     (return nil))))))))
427
428 (deftransform count ((item sequence) (bit simple-bit-vector) *
429                      :policy (>= speed space))
430   `(let ((length (length sequence)))
431     (if (zerop length)
432         0
433         (do ((index 0 (1+ index))
434              (count 0)
435              (end-1 (truncate (truly-the index (1- length))
436                               sb!vm:n-word-bits)))
437             ((>= index end-1)
438              (let* ((extra (1+ (mod (1- length) sb!vm:n-word-bits)))
439                     (mask (ash #.(1- (ash 1 sb!vm:n-word-bits))
440                                (- extra sb!vm:n-word-bits)))
441                     (bits (logand (ash mask
442                                        ,(ecase sb!c:*backend-byte-order*
443                                                (:little-endian 0)
444                                                (:big-endian
445                                                 '(- sb!vm:n-word-bits extra))))
446                                   (%vector-raw-bits sequence index))))
447                (declare (type (integer 1 #.sb!vm:n-word-bits) extra))
448                (declare (type sb!vm:word mask bits))
449                (incf count (logcount bits))
450                ,(if (constant-lvar-p item)
451                     (if (zerop (lvar-value item))
452                         '(- length count)
453                         'count)
454                     '(if (zerop item)
455                          (- length count)
456                          count))))
457           (declare (type index index count end-1)
458                    (optimize (speed 3) (safety 0)))
459           (incf count (logcount (%vector-raw-bits sequence index)))))))
460
461 (deftransform fill ((sequence item) (simple-bit-vector bit) *
462                     :policy (>= speed space))
463   (let ((value (if (constant-lvar-p item)
464                    (if (= (lvar-value item) 0)
465                        0
466                        #.(1- (ash 1 sb!vm:n-word-bits)))
467                    `(if (= item 0) 0 #.(1- (ash 1 sb!vm:n-word-bits))))))
468     `(let ((length (length sequence))
469            (value ,value))
470        (if (= length 0)
471            sequence
472            (do ((index 0 (1+ index))
473                 ;; bit-vectors of length 1 to n-word-bits need precisely
474                 ;; one (SETF %VECTOR-RAW-BITS), done here in the
475                 ;; epilogue. - CSR, 2002-04-24
476                 (end-1 (truncate (truly-the index (1- length))
477                                  sb!vm:n-word-bits)))
478                ((>= index end-1)
479                 (setf (%vector-raw-bits sequence index) value)
480                 sequence)
481              (declare (optimize (speed 3) (safety 0))
482                       (type index index end-1))
483              (setf (%vector-raw-bits sequence index) value))))))
484
485 (deftransform fill ((sequence item) (simple-base-string base-char) *
486                     :policy (>= speed space))
487   (let ((value (if (constant-lvar-p item)
488                    (let* ((char (lvar-value item))
489                           (code (sb!xc:char-code char))
490                           (accum 0))
491                      (dotimes (i sb!vm:n-word-bytes accum)
492                        (setf accum (logior accum (ash code (* 8 i))))))
493                    `(let ((code (sb!xc:char-code item)))
494                      (logior ,@(loop for i from 0 below sb!vm:n-word-bytes
495                                      collect `(ash code ,(* 8 i))))))))
496     `(let ((length (length sequence))
497            (value ,value))
498       (multiple-value-bind (times rem)
499           (truncate length sb!vm:n-word-bytes)
500         (do ((index 0 (1+ index))
501              (end times))
502             ((>= index end)
503              (let ((place (* times sb!vm:n-word-bytes)))
504                (declare (fixnum place))
505                (dotimes (j rem sequence)
506                  (declare (index j))
507                  (setf (schar sequence (the index (+ place j))) item))))
508           (declare (optimize (speed 3) (safety 0))
509                    (type index index))
510           (setf (%vector-raw-bits sequence index) value))))))
511 \f
512 ;;;; %BYTE-BLT
513
514 ;;; FIXME: The old CMU CL code used various COPY-TO/FROM-SYSTEM-AREA
515 ;;; stuff (with all the associated bit-index cruft and overflow
516 ;;; issues) even for byte moves. In SBCL, we're converting to byte
517 ;;; moves as problems are discovered with the old code, and this is
518 ;;; currently (ca. sbcl-0.6.12.30) the main interface for code in
519 ;;; SB!KERNEL and SB!SYS (e.g. i/o code). It's not clear that it's the
520 ;;; ideal interface, though, and it probably deserves some thought.
521 (deftransform %byte-blt ((src src-start dst dst-start dst-end)
522                          ((or (simple-unboxed-array (*)) system-area-pointer)
523                           index
524                           (or (simple-unboxed-array (*)) system-area-pointer)
525                           index
526                           index))
527   ;; FIXME: CMU CL had a hairier implementation of this (back when it
528   ;; was still called (%PRIMITIVE BYTE-BLT). It had the small problem
529   ;; that it didn't work for large (>16M) values of SRC-START or
530   ;; DST-START. However, it might have been more efficient. In
531   ;; particular, I don't really know how much the foreign function
532   ;; call costs us here. My guess is that if the overhead is
533   ;; acceptable for SQRT and COS, it's acceptable here, but this
534   ;; should probably be checked. -- WHN
535   '(flet ((sapify (thing)
536             (etypecase thing
537               (system-area-pointer thing)
538               ;; FIXME: The code here rather relies on the simple
539               ;; unboxed array here having byte-sized entries. That
540               ;; should be asserted explicitly, I just haven't found
541               ;; a concise way of doing it. (It would be nice to
542               ;; declare it in the DEFKNOWN too.)
543               ((simple-unboxed-array (*)) (vector-sap thing)))))
544      (declare (inline sapify))
545     (with-pinned-objects (dst src)
546       (memmove (sap+ (sapify dst) dst-start)
547                (sap+ (sapify src) src-start)
548                (- dst-end dst-start)))
549      (values)))
550 \f
551 ;;;; transforms for EQL of floating point values
552 #!-float-eql-vops
553 (deftransform eql ((x y) (single-float single-float))
554   '(= (single-float-bits x) (single-float-bits y)))
555
556 #!-float-eql-vops
557 (deftransform eql ((x y) (double-float double-float))
558   '(and (= (double-float-low-bits x) (double-float-low-bits y))
559         (= (double-float-high-bits x) (double-float-high-bits y))))
560
561 \f
562 ;;;; modular functions
563 ;;;
564 ;;; FIXME: I think that the :GOODness of a modular function boils down
565 ;;; to whether the normal definition can be used in the middle of a
566 ;;; modular arrangement.  LOGAND and LOGIOR can be for all unsigned
567 ;;; modular implementations, I believe, because for all unsigned
568 ;;; arguments of a given size the result of the ordinary definition is
569 ;;; the right one.  This should follow through to other logical
570 ;;; functions, such as LOGXOR, should it not?  -- CSR, 2007-12-29,
571 ;;; trying to understand a comment he wrote over four years
572 ;;; previously: "FIXME: XOR? ANDC1, ANDC2?  -- CSR, 2003-09-16"
573 (define-good-modular-fun logand :untagged nil)
574 (define-good-modular-fun logior :untagged nil)
575 (define-good-modular-fun logxor :untagged nil)
576 (macrolet ((define-good-signed-modular-funs (&rest funs)
577              (let (result)
578                `(progn
579                  ,@(dolist (fun funs (nreverse result))
580                      (push `(define-good-modular-fun ,fun :untagged t) result)
581                      (push `(define-good-modular-fun ,fun :tagged t) result))))))
582   (define-good-signed-modular-funs
583       logand logandc1 logandc2 logeqv logior lognand lognor lognot
584       logorc1 logorc2 logxor))
585
586 (macrolet
587     ((def (name kind width signedp)
588        (let ((type (ecase signedp
589                      ((nil) 'unsigned-byte)
590                      ((t) 'signed-byte))))
591          `(progn
592             (defknown ,name (integer (integer 0)) (,type ,width)
593                       (foldable flushable movable))
594             (define-modular-fun-optimizer ash ((integer count) ,kind ,signedp :width width)
595               (when (and (<= width ,width)
596                          (or (and (constant-lvar-p count)
597                                   (plusp (lvar-value count)))
598                              (csubtypep (lvar-type count)
599                                         (specifier-type '(and unsigned-byte fixnum)))))
600                 (cut-to-width integer ,kind width ,signedp)
601                 ',name))
602             (setf (gethash ',name (modular-class-versions (find-modular-class ',kind ',signedp)))
603                   `(ash ,',width))))))
604   ;; This should really be dependent on SB!VM:N-WORD-BITS, but since we
605   ;; don't have a true Alpha64 port yet, we'll have to stick to
606   ;; SB!VM:N-MACHINE-WORD-BITS for the time being.  --njf, 2004-08-14
607   #!+#.(cl:if (cl:= 32 sb!vm:n-machine-word-bits) '(and) '(or))
608   (progn
609     #!+x86 (def sb!vm::ash-left-smod30 :tagged 30 t)
610     (def sb!vm::ash-left-mod32 :untagged 32 nil))
611   #!+#.(cl:if (cl:= 64 sb!vm:n-machine-word-bits) '(and) '(or))
612   (progn
613     #!+x86-64 (def sb!vm::ash-left-smod61 :tagged 61 t)
614     (def sb!vm::ash-left-mod64 :untagged 64 nil)))
615 \f
616 ;;;; word-wise logical operations
617
618 ;;; These transforms assume the presence of modular arithmetic to
619 ;;; generate efficient code.
620
621 (define-source-transform word-logical-not (x)
622   `(logand (lognot (the sb!vm:word ,x)) #.(1- (ash 1 sb!vm:n-word-bits))))
623
624 (deftransform word-logical-and ((x y))
625   '(logand x y))
626
627 (deftransform word-logical-nand ((x y))
628   '(logand (lognand x y) #.(1- (ash 1 sb!vm:n-word-bits))))
629
630 (deftransform word-logical-or ((x y))
631   '(logior x y))
632
633 (deftransform word-logical-nor ((x y))
634   '(logand (lognor x y) #.(1- (ash 1 sb!vm:n-word-bits))))
635
636 (deftransform word-logical-xor ((x y))
637   '(logxor x y))
638
639 (deftransform word-logical-eqv ((x y))
640   '(logand (logeqv x y) #.(1- (ash 1 sb!vm:n-word-bits))))
641
642 (deftransform word-logical-orc1 ((x y))
643   '(logand (logorc1 x y) #.(1- (ash 1 sb!vm:n-word-bits))))
644
645 (deftransform word-logical-orc2 ((x y))
646   '(logand (logorc2 x y) #.(1- (ash 1 sb!vm:n-word-bits))))
647
648 (deftransform word-logical-andc1 ((x y))
649   '(logand (logandc1 x y) #.(1- (ash 1 sb!vm:n-word-bits))))
650
651 (deftransform word-logical-andc2 ((x y))
652   '(logand (logandc2 x y) #.(1- (ash 1 sb!vm:n-word-bits))))
653
654 \f
655 ;;; There are two different ways the multiplier can be recoded. The
656 ;;; more obvious is to shift X by the correct amount for each bit set
657 ;;; in Y and to sum the results. But if there is a string of bits that
658 ;;; are all set, you can add X shifted by one more then the bit
659 ;;; position of the first set bit and subtract X shifted by the bit
660 ;;; position of the last set bit. We can't use this second method when
661 ;;; the high order bit is bit 31 because shifting by 32 doesn't work
662 ;;; too well.
663 (defun ub32-strength-reduce-constant-multiply (arg num)
664   (declare (type (unsigned-byte 32) num))
665   (let ((adds 0) (shifts 0)
666         (result nil) first-one)
667     (labels ((add (next-factor)
668                (setf result
669                      (if result
670                          (progn (incf adds) `(+ ,result ,next-factor))
671                          next-factor))))
672       (declare (inline add))
673       (dotimes (bitpos 32)
674         (if first-one
675             (when (not (logbitp bitpos num))
676               (add (if (= (1+ first-one) bitpos)
677                        ;; There is only a single bit in the string.
678                        (progn (incf shifts) `(ash ,arg ,first-one))
679                        ;; There are at least two.
680                        (progn
681                          (incf adds)
682                          (incf shifts 2)
683                          `(- (ash ,arg ,bitpos)
684                              (ash ,arg ,first-one)))))
685               (setf first-one nil))
686             (when (logbitp bitpos num)
687               (setf first-one bitpos))))
688       (when first-one
689         (cond ((= first-one 31))
690               ((= first-one 30) (incf shifts) (add `(ash ,arg 30)))
691               (t
692                (incf shifts 2)
693                (incf adds)
694                (add `(- (ash ,arg 31)
695                         (ash ,arg ,first-one)))))
696         (incf shifts)
697         (add `(ash ,arg 31))))
698     (values (if (plusp adds)
699                 `(logand ,result #.(1- (ash 1 32))) ; using modular arithmetic
700                 result)
701             adds
702             shifts)))
703
704 \f
705 ;;; Transform GET-LISP-OBJ-ADDRESS for constant immediates, since the normal
706 ;;; VOP can't handle them.
707
708 (deftransform sb!vm::get-lisp-obj-address ((obj) ((constant-arg fixnum)))
709   (ash (lvar-value obj) sb!vm::n-fixnum-tag-bits))
710
711 (deftransform sb!vm::get-lisp-obj-address ((obj) ((constant-arg character)))
712   (logior sb!vm::character-widetag
713           (ash (char-code (lvar-value obj)) sb!vm::n-widetag-bits)))