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