0.9.4.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   `(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 (define-source-transform %funcallable-instance-layout (x)
36   `(truly-the layout (%funcallable-instance-info ,x 0)))
37 (define-source-transform %set-funcallable-instance-layout (x val)
38   `(setf (%funcallable-instance-info ,x 0) (the layout ,val)))
39 \f
40 ;;;; character support
41
42 ;;; In our implementation there are really only BASE-CHARs.
43 #+nil
44 (define-source-transform characterp (obj)
45   `(base-char-p ,obj))
46 \f
47 ;;;; simplifying HAIRY-DATA-VECTOR-REF and HAIRY-DATA-VECTOR-SET
48
49 (deftransform hairy-data-vector-ref ((string index) (simple-string t))
50   (let ((ctype (lvar-type string)))
51     (if (array-type-p ctype)
52         ;; the other transform will kick in, so that's OK
53         (give-up-ir1-transform)
54         `(etypecase string
55           ((simple-array character (*)) (data-vector-ref string index))
56           #!+sb-unicode
57           ((simple-array base-char (*)) (data-vector-ref string index))
58           ((simple-array nil (*)) (data-vector-ref string index))))))
59
60 (deftransform hairy-data-vector-ref ((array index) (array t) *)
61   "avoid runtime dispatch on array element type"
62   (let ((element-ctype (extract-upgraded-element-type array))
63         (declared-element-ctype (extract-declared-element-type array)))
64     (declare (type ctype element-ctype))
65     (when (eq *wild-type* element-ctype)
66       (give-up-ir1-transform
67        "Upgraded element type of array is not known at compile time."))
68     ;; (The expansion here is basically a degenerate case of
69     ;; WITH-ARRAY-DATA. Since WITH-ARRAY-DATA is implemented as a
70     ;; macro, and macros aren't expanded in transform output, we have
71     ;; to hand-expand it ourselves.)
72     (let ((element-type-specifier (type-specifier element-ctype)))
73       `(multiple-value-bind (array index)
74            (%data-vector-and-index array index)
75          (declare (type (simple-array ,element-type-specifier 1) array))
76          ,(let ((bare-form '(data-vector-ref array index)))
77             (if (type= element-ctype declared-element-ctype)
78                 bare-form
79                 `(the ,(type-specifier declared-element-ctype)
80                       ,bare-form)))))))
81
82 (deftransform data-vector-ref ((array index)
83                                (simple-array t))
84   (let ((array-type (lvar-type array)))
85     (unless (array-type-p array-type)
86       (give-up-ir1-transform))
87     (let ((dims (array-type-dimensions array-type)))
88       (when (or (atom dims) (= (length dims) 1))
89         (give-up-ir1-transform))
90       (let ((el-type (array-type-specialized-element-type array-type))
91             (total-size (if (member '* dims)
92                             '*
93                             (reduce #'* dims))))
94         `(data-vector-ref (truly-the (simple-array ,(type-specifier el-type)
95                                                    (,total-size))
96                                      (%array-data-vector array))
97                           index)))))
98
99 (deftransform hairy-data-vector-set ((string index new-value)
100                                      (simple-string t t))
101   (let ((ctype (lvar-type string)))
102     (if (array-type-p ctype)
103         ;; the other transform will kick in, so that's OK
104         (give-up-ir1-transform)
105         `(etypecase string
106           ((simple-array character (*))
107            (data-vector-set string index new-value))
108           #!+sb-unicode
109           ((simple-array base-char (*))
110            (data-vector-set string index new-value))
111           ((simple-array nil (*))
112            (data-vector-set string index new-value))))))
113
114 (deftransform hairy-data-vector-set ((array index new-value)
115                                      (array t t)
116                                      *)
117   "avoid runtime dispatch on array element type"
118   (let ((element-ctype (extract-upgraded-element-type array))
119         (declared-element-ctype (extract-declared-element-type array)))
120     (declare (type ctype element-ctype))
121     (when (eq *wild-type* element-ctype)
122       (give-up-ir1-transform
123        "Upgraded element type of array is not known at compile time."))
124     (let ((element-type-specifier (type-specifier element-ctype)))
125       `(multiple-value-bind (array index)
126            (%data-vector-and-index array index)
127          (declare (type (simple-array ,element-type-specifier 1) array)
128                   (type ,element-type-specifier new-value))
129          ,(if (type= element-ctype declared-element-ctype)
130               '(data-vector-set array index new-value)
131               `(truly-the ,(type-specifier declared-element-ctype)
132                  (data-vector-set array index
133                   (the ,(type-specifier declared-element-ctype)
134                        new-value))))))))
135
136 (deftransform data-vector-set ((array index new-value)
137                                (simple-array t 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-set (truly-the (simple-array ,(type-specifier el-type)
149                                                    (,total-size))
150                                      (%array-data-vector array))
151                           index
152                           new-value)))))
153
154 (defoptimizer (%data-vector-and-index derive-type) ((array index))
155   (let ((atype (lvar-type array)))
156     (when (array-type-p atype)
157       (values-specifier-type
158        `(values (simple-array ,(type-specifier
159                                 (array-type-specialized-element-type atype))
160                               (*))
161                 index)))))
162
163 (deftransform %data-vector-and-index ((%array %index)
164                                       (simple-array t)
165                                       *)
166   ;; KLUDGE: why the percent signs?  Well, ARRAY and INDEX are
167   ;; respectively exported from the CL and SB!INT packages, which
168   ;; means that they're visible to all sorts of things.  If the
169   ;; compiler can prove that the call to ARRAY-HEADER-P, below, either
170   ;; returns T or NIL, it will delete the irrelevant branch.  However,
171   ;; user code might have got here with a variable named CL:ARRAY, and
172   ;; quite often compiler code with a variable named SB!INT:INDEX, so
173   ;; this can generate code deletion notes for innocuous user code:
174   ;; (DEFUN F (ARRAY I) (DECLARE (SIMPLE-VECTOR ARRAY)) (AREF ARRAY I))
175   ;; -- CSR, 2003-04-01
176
177   ;; We do this solely for the -OR-GIVE-UP side effect, since we want
178   ;; to know that the type can be figured out in the end before we
179   ;; proceed, but we don't care yet what the type will turn out to be.
180   (upgraded-element-type-specifier-or-give-up %array)
181
182   '(if (array-header-p %array)
183        (values (%array-data-vector %array) %index)
184        (values %array %index)))
185
186 ;;; transforms for getting at simple arrays of (UNSIGNED-BYTE N) when (< N 8)
187 ;;;
188 ;;; FIXME: In CMU CL, these were commented out with #+NIL. Why? Should
189 ;;; we fix them or should we delete them? (Perhaps these definitions
190 ;;; predate the various DATA-VECTOR-REF-FOO VOPs which have
191 ;;; (:TRANSLATE DATA-VECTOR-REF), and are redundant now?)
192 #+nil
193 (macrolet
194     ((frob (type bits)
195        (let ((elements-per-word (truncate sb!vm:n-word-bits bits)))
196          `(progn
197             (deftransform data-vector-ref ((vector index)
198                                            (,type *))
199               `(multiple-value-bind (word bit)
200                    (floor index ,',elements-per-word)
201                  (ldb ,(ecase sb!vm:target-byte-order
202                          (:little-endian '(byte ,bits (* bit ,bits)))
203                          (:big-endian '(byte ,bits (- sb!vm:n-word-bits
204                                                       (* (1+ bit) ,bits)))))
205                       (%raw-bits vector (+ word sb!vm:vector-data-offset)))))
206             (deftransform data-vector-set ((vector index new-value)
207                                            (,type * *))
208               `(multiple-value-bind (word bit)
209                    (floor index ,',elements-per-word)
210                  (setf (ldb ,(ecase sb!vm:target-byte-order
211                                (:little-endian '(byte ,bits (* bit ,bits)))
212                                (:big-endian
213                                 '(byte ,bits (- sb!vm:n-word-bits
214                                                 (* (1+ bit) ,bits)))))
215                             (%raw-bits vector (+ word sb!vm:vector-data-offset)))
216                        new-value)))))))
217   (frob simple-bit-vector 1)
218   (frob (simple-array (unsigned-byte 2) (*)) 2)
219   (frob (simple-array (unsigned-byte 4) (*)) 4))
220 \f
221 ;;;; BIT-VECTOR hackery
222
223 ;;; SIMPLE-BIT-VECTOR bit-array operations are transformed to a word
224 ;;; loop that does 32 bits at a time.
225 ;;;
226 ;;; FIXME: This is a lot of repeatedly macroexpanded code. It should
227 ;;; be a function call instead.
228 (macrolet ((def (bitfun wordfun)
229              `(deftransform ,bitfun ((bit-array-1 bit-array-2 result-bit-array)
230                                      (simple-bit-vector
231                                       simple-bit-vector
232                                       simple-bit-vector)
233                                      *
234                                      :node node :policy (>= speed space))
235                 `(progn
236                    ,@(unless (policy node (zerop safety))
237                              '((unless (= (length bit-array-1)
238                                           (length bit-array-2)
239                                           (length result-bit-array))
240                                  (error "Argument and/or result bit arrays are not the same length:~
241                          ~%  ~S~%  ~S  ~%  ~S"
242                                         bit-array-1
243                                         bit-array-2
244                                         result-bit-array))))
245                   (let ((length (length result-bit-array)))
246                     (if (= length 0)
247                         ;; We avoid doing anything to 0-length
248                         ;; bit-vectors, or rather, the memory that
249                         ;; follows them. Other divisible-by-32 cases
250                         ;; are handled by the (1- length), below.
251                         ;; CSR, 2002-04-24
252                         result-bit-array
253                         (do ((index sb!vm:vector-data-offset (1+ index))
254                              (end-1 (+ sb!vm:vector-data-offset
255                                        ;; bit-vectors of length 1-32
256                                        ;; need precisely one (SETF
257                                        ;; %RAW-BITS), done here in the
258                                        ;; epilogue. - CSR, 2002-04-24
259                                        (truncate (truly-the index (1- length))
260                                                  sb!vm:n-word-bits))))
261                             ((= index end-1)
262                              (setf (%raw-bits result-bit-array index)
263                                    (,',wordfun (%raw-bits bit-array-1 index)
264                                                (%raw-bits bit-array-2 index)))
265                              result-bit-array)
266                           (declare (optimize (speed 3) (safety 0))
267                                    (type index index end-1))
268                           (setf (%raw-bits result-bit-array index)
269                                 (,',wordfun (%raw-bits bit-array-1 index)
270                                             (%raw-bits bit-array-2 index))))))))))
271  (def bit-and word-logical-and)
272  (def bit-ior word-logical-or)
273  (def bit-xor word-logical-xor)
274  (def bit-eqv word-logical-eqv)
275  (def bit-nand word-logical-nand)
276  (def bit-nor word-logical-nor)
277  (def bit-andc1 word-logical-andc1)
278  (def bit-andc2 word-logical-andc2)
279  (def bit-orc1 word-logical-orc1)
280  (def bit-orc2 word-logical-orc2))
281
282 (deftransform bit-not
283               ((bit-array result-bit-array)
284                (simple-bit-vector simple-bit-vector) *
285                :node node :policy (>= speed space))
286   `(progn
287      ,@(unless (policy node (zerop safety))
288          '((unless (= (length bit-array)
289                       (length result-bit-array))
290              (error "Argument and result bit arrays are not the same length:~
291                      ~%  ~S~%  ~S"
292                     bit-array result-bit-array))))
293     (let ((length (length result-bit-array)))
294       (if (= length 0)
295           ;; We avoid doing anything to 0-length bit-vectors, or rather,
296           ;; the memory that follows them. Other divisible-by
297           ;; n-word-bits cases are handled by the (1- length), below.
298           ;; CSR, 2002-04-24
299           result-bit-array
300           (do ((index sb!vm:vector-data-offset (1+ index))
301                (end-1 (+ sb!vm:vector-data-offset
302                          ;; bit-vectors of length 1 to n-word-bits need
303                          ;; precisely one (SETF %RAW-BITS), done here in
304                          ;; the epilogue. - CSR, 2002-04-24
305                          (truncate (truly-the index (1- length))
306                                    sb!vm:n-word-bits))))
307               ((= index end-1)
308                (setf (%raw-bits result-bit-array index)
309                      (word-logical-not (%raw-bits bit-array index)))
310                result-bit-array)
311             (declare (optimize (speed 3) (safety 0))
312                      (type index index end-1))
313             (setf (%raw-bits result-bit-array index)
314                   (word-logical-not (%raw-bits bit-array index))))))))
315
316 (deftransform bit-vector-= ((x y) (simple-bit-vector simple-bit-vector))
317   `(and (= (length x) (length y))
318         (let ((length (length x)))
319           (or (= length 0)
320               (do* ((i sb!vm:vector-data-offset (+ i 1))
321                     (end-1 (+ sb!vm:vector-data-offset
322                               (floor (1- length) sb!vm:n-word-bits))))
323                    ((= i end-1)
324                     (let* ((extra (1+ (mod (1- length) sb!vm:n-word-bits)))
325                            (mask (ash #.(1- (ash 1 sb!vm:n-word-bits))
326                                       (- extra sb!vm:n-word-bits)))
327                            (numx
328                             (logand
329                              (ash mask
330                                   ,(ecase sb!c:*backend-byte-order*
331                                      (:little-endian 0)
332                                      (:big-endian
333                                       '(- sb!vm:n-word-bits extra))))
334                              (%raw-bits x i)))
335                            (numy
336                             (logand
337                              (ash mask
338                                   ,(ecase sb!c:*backend-byte-order*
339                                      (:little-endian 0)
340                                      (:big-endian
341                                       '(- sb!vm:n-word-bits extra))))
342                              (%raw-bits y i))))
343                       (declare (type (integer 1 #.sb!vm:n-word-bits) extra)
344                                (type sb!vm:word mask numx numy))
345                       (= numx numy)))
346                 (declare (type index i end-1))
347                 (let ((numx (%raw-bits x i))
348                       (numy (%raw-bits y i)))
349                   (declare (type sb!vm:word numx numy))
350                   (unless (= numx numy)
351                     (return nil))))))))
352
353 (deftransform count ((item sequence) (bit simple-bit-vector) *
354                      :policy (>= speed space))
355   `(let ((length (length sequence)))
356     (if (zerop length)
357         0
358         (do ((index sb!vm:vector-data-offset (1+ index))
359              (count 0)
360              (end-1 (+ sb!vm:vector-data-offset
361                        (truncate (truly-the index (1- length))
362                                  sb!vm:n-word-bits))))
363             ((= index end-1)
364              (let* ((extra (1+ (mod (1- length) sb!vm:n-word-bits)))
365                     (mask (ash #.(1- (ash 1 sb!vm:n-word-bits))
366                                (- extra sb!vm:n-word-bits)))
367                     (bits (logand (ash mask
368                                        ,(ecase sb!c:*backend-byte-order*
369                                                (:little-endian 0)
370                                                (:big-endian
371                                                 '(- sb!vm:n-word-bits extra))))
372                                   (%raw-bits sequence index))))
373                (declare (type (integer 1 #.sb!vm:n-word-bits) extra))
374                (declare (type sb!vm:word mask bits))
375                ;; could consider LOGNOT for the zero case instead of
376                ;; doing the subtraction...
377                (incf count ,(if (constant-lvar-p item)
378                                 (if (zerop (lvar-value item))
379                                     '(- extra (logcount bits))
380                                     '(logcount bits))
381                                 '(if (zerop item)
382                                      (- extra (logcount bits))
383                                      (logcount bits))))))
384           (declare (type index index count end-1)
385                    (optimize (speed 3) (safety 0)))
386           (incf count ,(if (constant-lvar-p item)
387                            (if (zerop (lvar-value item))
388                                '(- sb!vm:n-word-bits (logcount (%raw-bits sequence index)))
389                                '(logcount (%raw-bits sequence index)))
390                            '(if (zerop item)
391                              (- sb!vm:n-word-bits (logcount (%raw-bits sequence index)))
392                              (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)))