7c4cc00da832822b447fa959637e725d2d98e50d
[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 (define-source-transform characterp (obj)
40   `(base-char-p ,obj))
41 \f
42 ;;;; simplifying HAIRY-DATA-VECTOR-REF and HAIRY-DATA-VECTOR-SET
43
44 (deftransform hairy-data-vector-ref ((array index) (array t) * :important t)
45   "avoid runtime dispatch on array element type"
46   (let ((element-ctype (extract-upgraded-element-type array)))
47     (declare (type ctype element-ctype))
48     (when (eq *wild-type* element-ctype)
49       (give-up-ir1-transform
50        "Upgraded element type of array is not known at compile time."))
51     ;; (The expansion here is basically a degenerate case of
52     ;; WITH-ARRAY-DATA. Since WITH-ARRAY-DATA is implemented as a
53     ;; macro, and macros aren't expanded in transform output, we have
54     ;; to hand-expand it ourselves.)
55     (let ((element-type-specifier (type-specifier element-ctype)))
56       `(multiple-value-bind (array index)
57            (%data-vector-and-index array index)
58          (declare (type (simple-array ,element-type-specifier 1) array))
59          (data-vector-ref array index)))))
60
61 (deftransform data-vector-ref ((array index)
62                                (simple-array t))
63   (let ((array-type (continuation-type array)))
64     (unless (array-type-p array-type)
65       (give-up-ir1-transform))
66     (let ((dims (array-type-dimensions array-type)))
67       (when (or (atom dims) (= (length dims) 1))
68         (give-up-ir1-transform))
69       (let ((el-type (array-type-specialized-element-type array-type))
70             (total-size (if (member '* dims)
71                             '*
72                             (reduce #'* dims))))
73         `(data-vector-ref (truly-the (simple-array ,(type-specifier el-type)
74                                                    (,total-size))
75                                      (%array-data-vector array))
76                           index)))))
77
78 (deftransform hairy-data-vector-set ((array index new-value)
79                                      (array t t)
80                                      *
81                                      :important t)
82   "avoid runtime dispatch on array element type"
83   (let ((element-ctype (extract-upgraded-element-type array)))
84     (declare (type ctype element-ctype))
85     (when (eq *wild-type* element-ctype)
86       (give-up-ir1-transform
87        "Upgraded element type of array is not known at compile time."))
88     (let ((element-type-specifier (type-specifier element-ctype)))
89       `(multiple-value-bind (array index)
90            (%data-vector-and-index array index)
91          (declare (type (simple-array ,element-type-specifier 1) array)
92                   (type ,element-type-specifier new-value))
93          (data-vector-set array
94                           index
95                           new-value)))))
96
97 (deftransform data-vector-set ((array index new-value)
98                                (simple-array t t))
99   (let ((array-type (continuation-type array)))
100     (unless (array-type-p array-type)
101       (give-up-ir1-transform))
102     (let ((dims (array-type-dimensions array-type)))
103       (when (or (atom dims) (= (length dims) 1))
104         (give-up-ir1-transform))
105       (let ((el-type (array-type-specialized-element-type array-type))
106             (total-size (if (member '* dims)
107                             '*
108                             (reduce #'* dims))))
109         `(data-vector-set (truly-the (simple-array ,(type-specifier el-type)
110                                                    (,total-size))
111                                      (%array-data-vector array))
112                           index
113                           new-value)))))
114
115 (defoptimizer (%data-vector-and-index derive-type) ((array index))
116   (let ((atype (continuation-type array)))
117     (when (array-type-p atype)
118       (values-specifier-type
119        `(values (simple-array ,(type-specifier
120                                 (array-type-specialized-element-type atype))
121                               (*))
122                 index)))))
123
124 (deftransform %data-vector-and-index ((%array %index)
125                                       (simple-array t)
126                                       *
127                                       :important t)
128   ;; KLUDGE: why the percent signs?  Well, ARRAY and INDEX are
129   ;; respectively exported from the CL and SB!INT packages, which
130   ;; means that they're visible to all sorts of things.  If the
131   ;; compiler can prove that the call to ARRAY-HEADER-P, below, either
132   ;; returns T or NIL, it will delete the irrelevant branch.  However,
133   ;; user code might have got here with a variable named CL:ARRAY, and
134   ;; quite often compiler code with a variable named SB!INT:INDEX, so
135   ;; this can generate code deletion notes for innocuous user code:
136   ;; (DEFUN F (ARRAY I) (DECLARE (SIMPLE-VECTOR ARRAY)) (AREF ARRAY I))
137   ;; -- CSR, 2003-04-01
138
139   ;; We do this solely for the -OR-GIVE-UP side effect, since we want
140   ;; to know that the type can be figured out in the end before we
141   ;; proceed, but we don't care yet what the type will turn out to be.
142   (upgraded-element-type-specifier-or-give-up %array)
143
144   '(if (array-header-p %array)
145        (values (%array-data-vector %array) %index)
146        (values %array %index)))
147
148 ;;; transforms for getting at simple arrays of (UNSIGNED-BYTE N) when (< N 8)
149 ;;;
150 ;;; FIXME: In CMU CL, these were commented out with #+NIL. Why? Should
151 ;;; we fix them or should we delete them? (Perhaps these definitions
152 ;;; predate the various DATA-VECTOR-REF-FOO VOPs which have
153 ;;; (:TRANSLATE DATA-VECTOR-REF), and are redundant now?)
154 #+nil
155 (macrolet
156     ((frob (type bits)
157        (let ((elements-per-word (truncate sb!vm:n-word-bits bits)))
158          `(progn
159             (deftransform data-vector-ref ((vector index)
160                                            (,type *))
161               `(multiple-value-bind (word bit)
162                    (floor index ,',elements-per-word)
163                  (ldb ,(ecase sb!vm:target-byte-order
164                          (:little-endian '(byte ,bits (* bit ,bits)))
165                          (:big-endian '(byte ,bits (- sb!vm:n-word-bits
166                                                       (* (1+ bit) ,bits)))))
167                       (%raw-bits vector (+ word sb!vm:vector-data-offset)))))
168             (deftransform data-vector-set ((vector index new-value)
169                                            (,type * *))
170               `(multiple-value-bind (word bit)
171                    (floor index ,',elements-per-word)
172                  (setf (ldb ,(ecase sb!vm:target-byte-order
173                                (:little-endian '(byte ,bits (* bit ,bits)))
174                                (:big-endian
175                                 '(byte ,bits (- sb!vm:n-word-bits
176                                                 (* (1+ bit) ,bits)))))
177                             (%raw-bits vector (+ word sb!vm:vector-data-offset)))
178                        new-value)))))))
179   (frob simple-bit-vector 1)
180   (frob (simple-array (unsigned-byte 2) (*)) 2)
181   (frob (simple-array (unsigned-byte 4) (*)) 4))
182 \f
183 ;;;; BIT-VECTOR hackery
184
185 ;;; SIMPLE-BIT-VECTOR bit-array operations are transformed to a word
186 ;;; loop that does 32 bits at a time.
187 ;;;
188 ;;; FIXME: This is a lot of repeatedly macroexpanded code. It should
189 ;;; be a function call instead.
190 (macrolet ((def (bitfun wordfun)
191              `(deftransform ,bitfun ((bit-array-1 bit-array-2 result-bit-array)
192                                      (simple-bit-vector
193                                       simple-bit-vector
194                                       simple-bit-vector)
195                                      *
196                                      :node node :policy (>= speed space))
197                 `(progn
198                    ,@(unless (policy node (zerop safety))
199                              '((unless (= (length bit-array-1)
200                                           (length bit-array-2)
201                                           (length result-bit-array))
202                                  (error "Argument and/or result bit arrays are not the same length:~
203                          ~%  ~S~%  ~S  ~%  ~S"
204                                         bit-array-1
205                                         bit-array-2
206                                         result-bit-array))))
207                   (let ((length (length result-bit-array)))
208                     (if (= length 0)
209                         ;; We avoid doing anything to 0-length
210                         ;; bit-vectors, or rather, the memory that
211                         ;; follows them. Other divisible-by-32 cases
212                         ;; are handled by the (1- length), below.
213                         ;; CSR, 2002-04-24
214                         result-bit-array
215                         (do ((index sb!vm:vector-data-offset (1+ index))
216                              (end-1 (+ sb!vm:vector-data-offset
217                                        ;; bit-vectors of length 1-32
218                                        ;; need precisely one (SETF
219                                        ;; %RAW-BITS), done here in the
220                                        ;; epilogue. - CSR, 2002-04-24
221                                        (truncate (truly-the index (1- length))
222                                                  sb!vm:n-word-bits))))
223                             ((= index end-1)
224                              (setf (%raw-bits result-bit-array index)
225                                    (,',wordfun (%raw-bits bit-array-1 index)
226                                                (%raw-bits bit-array-2 index)))
227                              result-bit-array)
228                           (declare (optimize (speed 3) (safety 0))
229                                    (type index index end-1))
230                           (setf (%raw-bits result-bit-array index)
231                                 (,',wordfun (%raw-bits bit-array-1 index)
232                                             (%raw-bits bit-array-2 index))))))))))
233  (def bit-and 32bit-logical-and)
234  (def bit-ior 32bit-logical-or)
235  (def bit-xor 32bit-logical-xor)
236  (def bit-eqv 32bit-logical-eqv)
237  (def bit-nand 32bit-logical-nand)
238  (def bit-nor 32bit-logical-nor)
239  (def bit-andc1 32bit-logical-andc1)
240  (def bit-andc2 32bit-logical-andc2)
241  (def bit-orc1 32bit-logical-orc1)
242  (def bit-orc2 32bit-logical-orc2))
243
244 (deftransform bit-not
245               ((bit-array result-bit-array)
246                (simple-bit-vector simple-bit-vector) *
247                :node node :policy (>= speed space))
248   `(progn
249      ,@(unless (policy node (zerop safety))
250          '((unless (= (length bit-array)
251                       (length result-bit-array))
252              (error "Argument and result bit arrays are not the same length:~
253                      ~%  ~S~%  ~S"
254                     bit-array result-bit-array))))
255     (let ((length (length result-bit-array)))
256       (if (= length 0)
257           ;; We avoid doing anything to 0-length bit-vectors, or
258           ;; rather, the memory that follows them. Other
259           ;; divisible-by-32 cases are handled by the (1- length),
260           ;; below.  CSR, 2002-04-24
261           result-bit-array
262           (do ((index sb!vm:vector-data-offset (1+ index))
263                (end-1 (+ sb!vm:vector-data-offset
264                          ;; bit-vectors of length 1-32 need precisely
265                          ;; one (SETF %RAW-BITS), done here in the
266                          ;; epilogue. - CSR, 2002-04-24
267                          (truncate (truly-the index (1- length))
268                                    sb!vm:n-word-bits))))
269               ((= index end-1)
270                (setf (%raw-bits result-bit-array index)
271                      (32bit-logical-not (%raw-bits bit-array 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                   (32bit-logical-not (%raw-bits bit-array index))))))))
277
278 (deftransform bit-vector-= ((x y) (simple-bit-vector simple-bit-vector))
279   `(and (= (length x) (length y))
280         (let ((length (length x)))
281           (or (= length 0)
282               (do* ((i sb!vm:vector-data-offset (+ i 1))
283                     (end-1 (+ sb!vm:vector-data-offset
284                               (floor (1- length) sb!vm:n-word-bits))))
285                    ((= i end-1)
286                     (let* ((extra (mod length sb!vm:n-word-bits))
287                            (mask (1- (ash 1 extra)))
288                            (numx
289                             (logand
290                              (ash mask
291                                   ,(ecase sb!c:*backend-byte-order*
292                                      (:little-endian 0)
293                                      (:big-endian
294                                       '(- sb!vm:n-word-bits extra))))
295                              (%raw-bits x i)))
296                            (numy
297                             (logand
298                              (ash mask
299                                   ,(ecase sb!c:*backend-byte-order*
300                                      (:little-endian 0)
301                                      (:big-endian
302                                       '(- sb!vm:n-word-bits extra))))
303                              (%raw-bits y i))))
304                       (declare (type (integer 0 31) extra)
305                                (type (unsigned-byte 32) mask numx numy))
306                       (= numx numy)))
307                 (declare (type index i end-1))
308                 (let ((numx (%raw-bits x i))
309                       (numy (%raw-bits y i)))
310                   (declare (type (unsigned-byte 32) numx numy))
311                   (unless (= numx numy)
312                     (return nil))))))))
313 \f
314 ;;;; %BYTE-BLT
315
316 ;;; FIXME: The old CMU CL code used various COPY-TO/FROM-SYSTEM-AREA
317 ;;; stuff (with all the associated bit-index cruft and overflow
318 ;;; issues) even for byte moves. In SBCL, we're converting to byte
319 ;;; moves as problems are discovered with the old code, and this is
320 ;;; currently (ca. sbcl-0.6.12.30) the main interface for code in
321 ;;; SB!KERNEL and SB!SYS (e.g. i/o code). It's not clear that it's the
322 ;;; ideal interface, though, and it probably deserves some thought.
323 (deftransform %byte-blt ((src src-start dst dst-start dst-end)
324                          ((or (simple-unboxed-array (*)) system-area-pointer)
325                           index
326                           (or (simple-unboxed-array (*)) system-area-pointer)
327                           index
328                           index))
329   ;; FIXME: CMU CL had a hairier implementation of this (back when it
330   ;; was still called (%PRIMITIVE BYTE-BLT). It had the small problem
331   ;; that it didn't work for large (>16M) values of SRC-START or
332   ;; DST-START. However, it might have been more efficient. In
333   ;; particular, I don't really know how much the foreign function
334   ;; call costs us here. My guess is that if the overhead is
335   ;; acceptable for SQRT and COS, it's acceptable here, but this
336   ;; should probably be checked. -- WHN
337   '(flet ((sapify (thing)
338             (etypecase thing
339               (system-area-pointer thing)
340               ;; FIXME: The code here rather relies on the simple
341               ;; unboxed array here having byte-sized entries. That
342               ;; should be asserted explicitly, I just haven't found
343               ;; a concise way of doing it. (It would be nice to
344               ;; declare it in the DEFKNOWN too.)
345               ((simple-unboxed-array (*)) (vector-sap thing)))))
346      (declare (inline sapify))
347      (without-gcing
348       (memmove (sap+ (sapify dst) dst-start)
349                (sap+ (sapify src) src-start)
350                (- dst-end dst-start)))
351      nil))
352 \f
353 ;;;; transforms for EQL of floating point values
354
355 (deftransform eql ((x y) (single-float single-float))
356   '(= (single-float-bits x) (single-float-bits y)))
357
358 (deftransform eql ((x y) (double-float double-float))
359   '(and (= (double-float-low-bits x) (double-float-low-bits y))
360         (= (double-float-high-bits x) (double-float-high-bits y))))
361