1.0.36.11: Make slam.sh work on Win32.
[sbcl.git] / src / compiler / 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 (defun maybe-array-data-vector-type-specifier (array-lvar)
268   (let ((atype (lvar-type array-lvar)))
269     (when (array-type-p atype)
270       (let ((dims (array-type-dimensions atype)))
271         (if (or (array-type-complexp atype)
272                 (eq '* dims)
273                 (notevery #'integerp dims))
274            `(simple-array ,(type-specifier
275                             (array-type-specialized-element-type atype))
276                           (*))
277            `(simple-array ,(type-specifier
278                             (array-type-specialized-element-type atype))
279                           (,(apply #'* dims))))))))
280
281 (macrolet ((def (name)
282              `(defoptimizer (,name derive-type) ((array-lvar))
283                 (let ((spec (maybe-array-data-vector-type-specifier array-lvar)))
284                   (when spec
285                     (specifier-type spec))))))
286   (def %array-data-vector)
287   (def array-storage-vector))
288
289 (defoptimizer (%data-vector-and-index derive-type) ((array index))
290   (let ((spec (maybe-array-data-vector-type-specifier array)))
291     (when spec
292       (values-specifier-type `(values ,spec index)))))
293
294 (deftransform %data-vector-and-index ((%array %index)
295                                       (simple-array t)
296                                       *)
297   ;; KLUDGE: why the percent signs?  Well, ARRAY and INDEX are
298   ;; respectively exported from the CL and SB!INT packages, which
299   ;; means that they're visible to all sorts of things.  If the
300   ;; compiler can prove that the call to ARRAY-HEADER-P, below, either
301   ;; returns T or NIL, it will delete the irrelevant branch.  However,
302   ;; user code might have got here with a variable named CL:ARRAY, and
303   ;; quite often compiler code with a variable named SB!INT:INDEX, so
304   ;; this can generate code deletion notes for innocuous user code:
305   ;; (DEFUN F (ARRAY I) (DECLARE (SIMPLE-VECTOR ARRAY)) (AREF ARRAY I))
306   ;; -- CSR, 2003-04-01
307
308   ;; We do this solely for the -OR-GIVE-UP side effect, since we want
309   ;; to know that the type can be figured out in the end before we
310   ;; proceed, but we don't care yet what the type will turn out to be.
311   (upgraded-element-type-specifier-or-give-up %array)
312
313   '(if (array-header-p %array)
314        (values (%array-data-vector %array) %index)
315        (values %array %index)))
316 \f
317 ;;;; BIT-VECTOR hackery
318
319 ;;; SIMPLE-BIT-VECTOR bit-array operations are transformed to a word
320 ;;; loop that does 32 bits at a time.
321 ;;;
322 ;;; FIXME: This is a lot of repeatedly macroexpanded code. It should
323 ;;; be a function call instead.
324 (macrolet ((def (bitfun wordfun)
325              `(deftransform ,bitfun ((bit-array-1 bit-array-2 result-bit-array)
326                                      (simple-bit-vector
327                                       simple-bit-vector
328                                       simple-bit-vector)
329                                      *
330                                      :node node :policy (>= speed space))
331                 `(progn
332                    ,@(unless (policy node (zerop safety))
333                              '((unless (= (length bit-array-1)
334                                           (length bit-array-2)
335                                           (length result-bit-array))
336                                  (error "Argument and/or result bit arrays are not the same length:~
337                          ~%  ~S~%  ~S  ~%  ~S"
338                                         bit-array-1
339                                         bit-array-2
340                                         result-bit-array))))
341                   (let ((length (length result-bit-array)))
342                     (if (= length 0)
343                         ;; We avoid doing anything to 0-length
344                         ;; bit-vectors, or rather, the memory that
345                         ;; follows them. Other divisible-by-32 cases
346                         ;; are handled by the (1- length), below.
347                         ;; CSR, 2002-04-24
348                         result-bit-array
349                         (do ((index 0 (1+ index))
350                              ;; bit-vectors of length 1-32 need
351                              ;; precisely one (SETF %VECTOR-RAW-BITS),
352                              ;; done here in the epilogue. - CSR,
353                              ;; 2002-04-24
354                              (end-1 (truncate (truly-the index (1- length))
355                                               sb!vm:n-word-bits)))
356                             ((>= index end-1)
357                              (setf (%vector-raw-bits result-bit-array index)
358                                    (,',wordfun (%vector-raw-bits bit-array-1 index)
359                                                (%vector-raw-bits bit-array-2 index)))
360                              result-bit-array)
361                           (declare (optimize (speed 3) (safety 0))
362                                    (type index index end-1))
363                           (setf (%vector-raw-bits result-bit-array index)
364                                 (,',wordfun (%vector-raw-bits bit-array-1 index)
365                                             (%vector-raw-bits bit-array-2 index))))))))))
366  (def bit-and word-logical-and)
367  (def bit-ior word-logical-or)
368  (def bit-xor word-logical-xor)
369  (def bit-eqv word-logical-eqv)
370  (def bit-nand word-logical-nand)
371  (def bit-nor word-logical-nor)
372  (def bit-andc1 word-logical-andc1)
373  (def bit-andc2 word-logical-andc2)
374  (def bit-orc1 word-logical-orc1)
375  (def bit-orc2 word-logical-orc2))
376
377 (deftransform bit-not
378               ((bit-array result-bit-array)
379                (simple-bit-vector simple-bit-vector) *
380                :node node :policy (>= speed space))
381   `(progn
382      ,@(unless (policy node (zerop safety))
383          '((unless (= (length bit-array)
384                       (length result-bit-array))
385              (error "Argument and result bit arrays are not the same length:~
386                      ~%  ~S~%  ~S"
387                     bit-array result-bit-array))))
388     (let ((length (length result-bit-array)))
389       (if (= length 0)
390           ;; We avoid doing anything to 0-length bit-vectors, or rather,
391           ;; the memory that follows them. Other divisible-by
392           ;; n-word-bits cases are handled by the (1- length), below.
393           ;; CSR, 2002-04-24
394           result-bit-array
395           (do ((index 0 (1+ index))
396                ;; bit-vectors of length 1 to n-word-bits need precisely
397                ;; one (SETF %VECTOR-RAW-BITS), done here in the
398                ;; epilogue. - CSR, 2002-04-24
399                (end-1 (truncate (truly-the index (1- length))
400                                 sb!vm:n-word-bits)))
401               ((>= index end-1)
402                (setf (%vector-raw-bits result-bit-array index)
403                      (word-logical-not (%vector-raw-bits bit-array index)))
404                result-bit-array)
405             (declare (optimize (speed 3) (safety 0))
406                      (type index index end-1))
407             (setf (%vector-raw-bits result-bit-array index)
408                   (word-logical-not (%vector-raw-bits bit-array index))))))))
409
410 (deftransform bit-vector-= ((x y) (simple-bit-vector simple-bit-vector))
411   `(and (= (length x) (length y))
412         (let ((length (length x)))
413           (or (= length 0)
414               (do* ((i 0 (+ i 1))
415                     (end-1 (floor (1- length) sb!vm:n-word-bits)))
416                    ((>= i end-1)
417                     (let* ((extra (1+ (mod (1- length) sb!vm:n-word-bits)))
418                            (mask (ash #.(1- (ash 1 sb!vm:n-word-bits))
419                                       (- extra sb!vm:n-word-bits)))
420                            (numx
421                             (logand
422                              (ash mask
423                                   ,(ecase sb!c:*backend-byte-order*
424                                      (:little-endian 0)
425                                      (:big-endian
426                                       '(- sb!vm:n-word-bits extra))))
427                              (%vector-raw-bits x i)))
428                            (numy
429                             (logand
430                              (ash mask
431                                   ,(ecase sb!c:*backend-byte-order*
432                                      (:little-endian 0)
433                                      (:big-endian
434                                       '(- sb!vm:n-word-bits extra))))
435                              (%vector-raw-bits y i))))
436                       (declare (type (integer 1 #.sb!vm:n-word-bits) extra)
437                                (type sb!vm:word mask numx numy))
438                       (= numx numy)))
439                 (declare (type index i end-1))
440                 (let ((numx (%vector-raw-bits x i))
441                       (numy (%vector-raw-bits y i)))
442                   (declare (type sb!vm:word numx numy))
443                   (unless (= numx numy)
444                     (return nil))))))))
445
446 (deftransform count ((item sequence) (bit simple-bit-vector) *
447                      :policy (>= speed space))
448   `(let ((length (length sequence)))
449     (if (zerop length)
450         0
451         (do ((index 0 (1+ index))
452              (count 0)
453              (end-1 (truncate (truly-the index (1- length))
454                               sb!vm:n-word-bits)))
455             ((>= index end-1)
456              (let* ((extra (1+ (mod (1- length) sb!vm:n-word-bits)))
457                     (mask (ash #.(1- (ash 1 sb!vm:n-word-bits))
458                                (- extra sb!vm:n-word-bits)))
459                     (bits (logand (ash mask
460                                        ,(ecase sb!c:*backend-byte-order*
461                                                (:little-endian 0)
462                                                (:big-endian
463                                                 '(- sb!vm:n-word-bits extra))))
464                                   (%vector-raw-bits sequence index))))
465                (declare (type (integer 1 #.sb!vm:n-word-bits) extra))
466                (declare (type sb!vm:word mask bits))
467                (incf count (logcount bits))
468                ,(if (constant-lvar-p item)
469                     (if (zerop (lvar-value item))
470                         '(- length count)
471                         'count)
472                     '(if (zerop item)
473                          (- length count)
474                          count))))
475           (declare (type index index count end-1)
476                    (optimize (speed 3) (safety 0)))
477           (incf count (logcount (%vector-raw-bits sequence index)))))))
478
479 (deftransform fill ((sequence item) (simple-bit-vector bit) *
480                     :policy (>= speed space))
481   (let ((value (if (constant-lvar-p item)
482                    (if (= (lvar-value item) 0)
483                        0
484                        #.(1- (ash 1 sb!vm:n-word-bits)))
485                    `(if (= item 0) 0 #.(1- (ash 1 sb!vm:n-word-bits))))))
486     `(let ((length (length sequence))
487            (value ,value))
488        (if (= length 0)
489            sequence
490            (do ((index 0 (1+ index))
491                 ;; bit-vectors of length 1 to n-word-bits need precisely
492                 ;; one (SETF %VECTOR-RAW-BITS), done here in the
493                 ;; epilogue. - CSR, 2002-04-24
494                 (end-1 (truncate (truly-the index (1- length))
495                                  sb!vm:n-word-bits)))
496                ((>= index end-1)
497                 (setf (%vector-raw-bits sequence index) value)
498                 sequence)
499              (declare (optimize (speed 3) (safety 0))
500                       (type index index end-1))
501              (setf (%vector-raw-bits sequence index) value))))))
502
503 (deftransform fill ((sequence item) (simple-base-string base-char) *
504                     :policy (>= speed space))
505   (let ((value (if (constant-lvar-p item)
506                    (let* ((char (lvar-value item))
507                           (code (sb!xc:char-code char))
508                           (accum 0))
509                      (dotimes (i sb!vm:n-word-bytes accum)
510                        (setf accum (logior accum (ash code (* 8 i))))))
511                    `(let ((code (sb!xc:char-code item)))
512                      (logior ,@(loop for i from 0 below sb!vm:n-word-bytes
513                                      collect `(ash code ,(* 8 i))))))))
514     `(let ((length (length sequence))
515            (value ,value))
516       (multiple-value-bind (times rem)
517           (truncate length sb!vm:n-word-bytes)
518         (do ((index 0 (1+ index))
519              (end times))
520             ((>= index end)
521              (let ((place (* times sb!vm:n-word-bytes)))
522                (declare (fixnum place))
523                (dotimes (j rem sequence)
524                  (declare (index j))
525                  (setf (schar sequence (the index (+ place j))) item))))
526           (declare (optimize (speed 3) (safety 0))
527                    (type index index))
528           (setf (%vector-raw-bits sequence index) value))))))
529 \f
530 ;;;; %BYTE-BLT
531
532 ;;; FIXME: The old CMU CL code used various COPY-TO/FROM-SYSTEM-AREA
533 ;;; stuff (with all the associated bit-index cruft and overflow
534 ;;; issues) even for byte moves. In SBCL, we're converting to byte
535 ;;; moves as problems are discovered with the old code, and this is
536 ;;; currently (ca. sbcl-0.6.12.30) the main interface for code in
537 ;;; SB!KERNEL and SB!SYS (e.g. i/o code). It's not clear that it's the
538 ;;; ideal interface, though, and it probably deserves some thought.
539 (deftransform %byte-blt ((src src-start dst dst-start dst-end)
540                          ((or (simple-unboxed-array (*)) system-area-pointer)
541                           index
542                           (or (simple-unboxed-array (*)) system-area-pointer)
543                           index
544                           index))
545   ;; FIXME: CMU CL had a hairier implementation of this (back when it
546   ;; was still called (%PRIMITIVE BYTE-BLT). It had the small problem
547   ;; that it didn't work for large (>16M) values of SRC-START or
548   ;; DST-START. However, it might have been more efficient. In
549   ;; particular, I don't really know how much the foreign function
550   ;; call costs us here. My guess is that if the overhead is
551   ;; acceptable for SQRT and COS, it's acceptable here, but this
552   ;; should probably be checked. -- WHN
553   '(flet ((sapify (thing)
554             (etypecase thing
555               (system-area-pointer thing)
556               ;; FIXME: The code here rather relies on the simple
557               ;; unboxed array here having byte-sized entries. That
558               ;; should be asserted explicitly, I just haven't found
559               ;; a concise way of doing it. (It would be nice to
560               ;; declare it in the DEFKNOWN too.)
561               ((simple-unboxed-array (*)) (vector-sap thing)))))
562      (declare (inline sapify))
563     (with-pinned-objects (dst src)
564       (memmove (sap+ (sapify dst) dst-start)
565                (sap+ (sapify src) src-start)
566                (- dst-end dst-start)))
567      (values)))
568 \f
569 ;;;; transforms for EQL of floating point values
570 #!-float-eql-vops
571 (deftransform eql ((x y) (single-float single-float))
572   '(= (single-float-bits x) (single-float-bits y)))
573
574 #!-float-eql-vops
575 (deftransform eql ((x y) (double-float double-float))
576   '(and (= (double-float-low-bits x) (double-float-low-bits y))
577         (= (double-float-high-bits x) (double-float-high-bits y))))
578
579 \f
580 ;;;; modular functions
581 ;;;
582 ;;; FIXME: I think that the :GOODness of a modular function boils down
583 ;;; to whether the normal definition can be used in the middle of a
584 ;;; modular arrangement.  LOGAND and LOGIOR can be for all unsigned
585 ;;; modular implementations, I believe, because for all unsigned
586 ;;; arguments of a given size the result of the ordinary definition is
587 ;;; the right one.  This should follow through to other logical
588 ;;; functions, such as LOGXOR, should it not?  -- CSR, 2007-12-29,
589 ;;; trying to understand a comment he wrote over four years
590 ;;; previously: "FIXME: XOR? ANDC1, ANDC2?  -- CSR, 2003-09-16"
591 (define-good-modular-fun logand :untagged nil)
592 (define-good-modular-fun logior :untagged nil)
593 (define-good-modular-fun logxor :untagged nil)
594 (macrolet ((define-good-signed-modular-funs (&rest funs)
595              (let (result)
596                `(progn
597                  ,@(dolist (fun funs (nreverse result))
598                      (push `(define-good-modular-fun ,fun :untagged t) result)
599                      (push `(define-good-modular-fun ,fun :tagged t) result))))))
600   (define-good-signed-modular-funs
601       logand logandc1 logandc2 logeqv logior lognand lognor lognot
602       logorc1 logorc2 logxor))
603
604 (macrolet
605     ((def (name kind width signedp)
606        (let ((type (ecase signedp
607                      ((nil) 'unsigned-byte)
608                      ((t) 'signed-byte))))
609          `(progn
610             (defknown ,name (integer (integer 0)) (,type ,width)
611                       (foldable flushable movable))
612             (define-modular-fun-optimizer ash ((integer count) ,kind ,signedp :width width)
613               (when (and (<= width ,width)
614                          (or (and (constant-lvar-p count)
615                                   (plusp (lvar-value count)))
616                              (csubtypep (lvar-type count)
617                                         (specifier-type '(and unsigned-byte fixnum)))))
618                 (cut-to-width integer ,kind width ,signedp)
619                 ',name))
620             (setf (gethash ',name (modular-class-versions (find-modular-class ',kind ',signedp)))
621                   `(ash ,',width))))))
622   ;; This should really be dependent on SB!VM:N-WORD-BITS, but since we
623   ;; don't have a true Alpha64 port yet, we'll have to stick to
624   ;; SB!VM:N-MACHINE-WORD-BITS for the time being.  --njf, 2004-08-14
625   #!+#.(cl:if (cl:= 32 sb!vm:n-machine-word-bits) '(and) '(or))
626   (progn
627     #!+x86 (def sb!vm::ash-left-smod30 :tagged 30 t)
628     (def sb!vm::ash-left-mod32 :untagged 32 nil))
629   #!+#.(cl:if (cl:= 64 sb!vm:n-machine-word-bits) '(and) '(or))
630   (progn
631     #!+x86-64 (def sb!vm::ash-left-smod61 :tagged 61 t)
632     (def sb!vm::ash-left-mod64 :untagged 64 nil)))
633 \f
634 ;;;; word-wise logical operations
635
636 ;;; These transforms assume the presence of modular arithmetic to
637 ;;; generate efficient code.
638
639 (define-source-transform word-logical-not (x)
640   `(logand (lognot (the sb!vm:word ,x)) #.(1- (ash 1 sb!vm:n-word-bits))))
641
642 (deftransform word-logical-and ((x y))
643   '(logand x y))
644
645 (deftransform word-logical-nand ((x y))
646   '(logand (lognand x y) #.(1- (ash 1 sb!vm:n-word-bits))))
647
648 (deftransform word-logical-or ((x y))
649   '(logior x y))
650
651 (deftransform word-logical-nor ((x y))
652   '(logand (lognor x y) #.(1- (ash 1 sb!vm:n-word-bits))))
653
654 (deftransform word-logical-xor ((x y))
655   '(logxor x y))
656
657 (deftransform word-logical-eqv ((x y))
658   '(logand (logeqv x y) #.(1- (ash 1 sb!vm:n-word-bits))))
659
660 (deftransform word-logical-orc1 ((x y))
661   '(logand (logorc1 x y) #.(1- (ash 1 sb!vm:n-word-bits))))
662
663 (deftransform word-logical-orc2 ((x y))
664   '(logand (logorc2 x y) #.(1- (ash 1 sb!vm:n-word-bits))))
665
666 (deftransform word-logical-andc1 ((x y))
667   '(logand (logandc1 x y) #.(1- (ash 1 sb!vm:n-word-bits))))
668
669 (deftransform word-logical-andc2 ((x y))
670   '(logand (logandc2 x y) #.(1- (ash 1 sb!vm:n-word-bits))))
671
672 \f
673 ;;; There are two different ways the multiplier can be recoded. The
674 ;;; more obvious is to shift X by the correct amount for each bit set
675 ;;; in Y and to sum the results. But if there is a string of bits that
676 ;;; are all set, you can add X shifted by one more then the bit
677 ;;; position of the first set bit and subtract X shifted by the bit
678 ;;; position of the last set bit. We can't use this second method when
679 ;;; the high order bit is bit 31 because shifting by 32 doesn't work
680 ;;; too well.
681 (defun ub32-strength-reduce-constant-multiply (arg num)
682   (declare (type (unsigned-byte 32) num))
683   (let ((adds 0) (shifts 0)
684         (result nil) first-one)
685     (labels ((add (next-factor)
686                (setf result
687                      (if result
688                          (progn (incf adds) `(+ ,result ,next-factor))
689                          next-factor))))
690       (declare (inline add))
691       (dotimes (bitpos 32)
692         (if first-one
693             (when (not (logbitp bitpos num))
694               (add (if (= (1+ first-one) bitpos)
695                        ;; There is only a single bit in the string.
696                        (progn (incf shifts) `(ash ,arg ,first-one))
697                        ;; There are at least two.
698                        (progn
699                          (incf adds)
700                          (incf shifts 2)
701                          `(- (ash ,arg ,bitpos)
702                              (ash ,arg ,first-one)))))
703               (setf first-one nil))
704             (when (logbitp bitpos num)
705               (setf first-one bitpos))))
706       (when first-one
707         (cond ((= first-one 31))
708               ((= first-one 30) (incf shifts) (add `(ash ,arg 30)))
709               (t
710                (incf shifts 2)
711                (incf adds)
712                (add `(- (ash ,arg 31)
713                         (ash ,arg ,first-one)))))
714         (incf shifts)
715         (add `(ash ,arg 31))))
716     (values (if (plusp adds)
717                 `(logand ,result #.(1- (ash 1 32))) ; using modular arithmetic
718                 result)
719             adds
720             shifts)))
721
722 \f
723 ;;; Transform GET-LISP-OBJ-ADDRESS for constant immediates, since the normal
724 ;;; VOP can't handle them.
725
726 (deftransform sb!vm::get-lisp-obj-address ((obj) ((constant-arg fixnum)))
727   (ash (lvar-value obj) sb!vm::n-fixnum-tag-bits))
728
729 (deftransform sb!vm::get-lisp-obj-address ((obj) ((constant-arg character)))
730   (logior sb!vm::character-widetag
731           (ash (char-code (lvar-value obj)) sb!vm::n-widetag-bits)))