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