0.6.9.21:
[sbcl.git] / src / compiler / array-tran.lisp
1 ;;;; array-specific optimizers and 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 \f
14 ;;;; DERIVE-TYPE optimizers
15
16 ;;; Array operations that use a specific number of indices implicitly
17 ;;; assert that the array is of that rank.
18 (defun assert-array-rank (array rank)
19   (assert-continuation-type
20    array
21    (specifier-type `(array * ,(make-list rank :initial-element '*)))))
22
23 ;;; Array access functions return an object from the array, hence its
24 ;;; type will be asserted to be array element type.
25 (defun extract-element-type (array)
26   (let ((type (continuation-type array)))
27     (if (array-type-p type)
28         (array-type-element-type type)
29         *universal-type*)))
30
31 ;;; Array access functions return an object from the array, hence its
32 ;;; type is going to be the array upgraded element type.
33 (defun extract-upgraded-element-type (array)
34   (let ((type (continuation-type array)))
35     (if (array-type-p type)
36         (array-type-specialized-element-type type)
37         *universal-type*)))
38
39 ;;; The ``new-value'' for array setters must fit in the array, and the
40 ;;; return type is going to be the same as the new-value for SETF
41 ;;; functions.
42 (defun assert-new-value-type (new-value array)
43   (let ((type (continuation-type array)))
44     (when (array-type-p type)
45       (assert-continuation-type new-value (array-type-element-type type))))
46   (continuation-type new-value))
47
48 ;;; Return true if Arg is NIL, or is a constant-continuation whose
49 ;;; value is NIL, false otherwise.
50 (defun unsupplied-or-nil (arg)
51   (declare (type (or continuation null) arg))
52   (or (not arg)
53       (and (constant-continuation-p arg)
54            (not (continuation-value arg)))))
55
56 (defoptimizer (array-in-bounds-p derive-type) ((array &rest indices))
57   (assert-array-rank array (length indices))
58   *universal-type*)
59
60 (defoptimizer (aref derive-type) ((array &rest indices) node)
61   (assert-array-rank array (length indices))
62   ;; If the node continuation has a single use then assert its type.
63   (let ((cont (node-cont node)))
64     (when (= (length (find-uses cont)) 1)
65       (assert-continuation-type cont (extract-element-type array))))
66   (extract-upgraded-element-type array))
67
68 (defoptimizer (%aset derive-type) ((array &rest stuff))
69   (assert-array-rank array (1- (length stuff)))
70   (assert-new-value-type (car (last stuff)) array))
71
72 (defoptimizer (hairy-data-vector-ref derive-type) ((array index))
73   (extract-upgraded-element-type array))
74 (defoptimizer (data-vector-ref derive-type) ((array index))
75   (extract-upgraded-element-type array))
76
77 (defoptimizer (data-vector-set derive-type) ((array index new-value))
78   (assert-new-value-type new-value array))
79 (defoptimizer (hairy-data-vector-set derive-type) ((array index new-value))
80   (assert-new-value-type new-value array))
81
82 ;;; Figure out the type of the data vector if we know the argument
83 ;;; element type.
84 (defoptimizer (%with-array-data derive-type) ((array start end))
85   (let ((atype (continuation-type array)))
86     (when (array-type-p atype)
87       (values-specifier-type
88        `(values (simple-array ,(type-specifier
89                                 (array-type-element-type atype))
90                               (*))
91                 index index index)))))
92
93 (defoptimizer (array-row-major-index derive-type) ((array &rest indices))
94   (assert-array-rank array (length indices))
95   *universal-type*)
96
97 (defoptimizer (row-major-aref derive-type) ((array index))
98   (extract-upgraded-element-type array))
99
100 (defoptimizer (%set-row-major-aref derive-type) ((array index new-value))
101   (assert-new-value-type new-value array))
102
103 (defoptimizer (make-array derive-type)
104               ((dims &key initial-element element-type initial-contents
105                 adjustable fill-pointer displaced-index-offset displaced-to))
106   (let ((simple (and (unsupplied-or-nil adjustable)
107                      (unsupplied-or-nil displaced-to)
108                      (unsupplied-or-nil fill-pointer))))
109     (specifier-type
110      `(,(if simple 'simple-array 'array)
111        ,(cond ((not element-type) 't)
112               ((constant-continuation-p element-type)
113                (continuation-value element-type))
114               (t
115                '*))
116        ,(cond ((not simple)
117                '*)
118               ((constant-continuation-p dims)
119                (let ((val (continuation-value dims)))
120                  (if (listp val) val (list val))))
121               ((csubtypep (continuation-type dims)
122                           (specifier-type 'integer))
123                '(*))
124               (t
125                '*))))))
126 \f
127 ;;;; constructors
128
129 ;;; Convert VECTOR into a MAKE-ARRAY followed by SETFs of all the
130 ;;; elements.
131 (def-source-transform vector (&rest elements)
132   (if (byte-compiling)
133       (values nil t)
134       (let ((len (length elements))
135             (n -1))
136         (once-only ((n-vec `(make-array ,len)))
137           `(progn
138              ,@(mapcar #'(lambda (el)
139                            (once-only ((n-val el))
140                              `(locally (declare (optimize (safety 0)))
141                                        (setf (svref ,n-vec ,(incf n))
142                                              ,n-val))))
143                        elements)
144              ,n-vec)))))
145
146 ;;; Just convert it into a MAKE-ARRAY.
147 (def-source-transform make-string (length &key
148                                           (element-type ''base-char)
149                                           (initial-element default-init-char))
150   (if (byte-compiling)
151       (values nil t)
152       `(make-array (the index ,length)
153                    :element-type ,element-type
154                    :initial-element ,initial-element)))
155
156 (defparameter *array-info*
157   #((base-char #.default-init-char 8 sb!vm:simple-string-type)
158     (single-float 0.0s0 32 sb!vm:simple-array-single-float-type)
159     (double-float 0.0d0 64 sb!vm:simple-array-double-float-type)
160     #!+long-float (long-float 0.0l0 #!+x86 96 #!+sparc 128
161                               sb!vm:simple-array-long-float-type)
162     (bit 0 1 sb!vm:simple-bit-vector-type)
163     ((unsigned-byte 2) 0 2 sb!vm:simple-array-unsigned-byte-2-type)
164     ((unsigned-byte 4) 0 4 sb!vm:simple-array-unsigned-byte-4-type)
165     ((unsigned-byte 8) 0 8 sb!vm:simple-array-unsigned-byte-8-type)
166     ((unsigned-byte 16) 0 16 sb!vm:simple-array-unsigned-byte-16-type)
167     ((unsigned-byte 32) 0 32 sb!vm:simple-array-unsigned-byte-32-type)
168     ((signed-byte 8) 0 8 sb!vm:simple-array-signed-byte-8-type)
169     ((signed-byte 16) 0 16 sb!vm:simple-array-signed-byte-16-type)
170     ((signed-byte 30) 0 32 sb!vm:simple-array-signed-byte-30-type)
171     ((signed-byte 32) 0 32 sb!vm:simple-array-signed-byte-32-type)
172     ((complex single-float) #C(0.0s0 0.0s0) 64
173      sb!vm:simple-array-complex-single-float-type)
174     ((complex double-float) #C(0.0d0 0.0d0) 128
175      sb!vm:simple-array-complex-double-float-type)
176     #!+long-float
177     ((complex long-float) #C(0.0l0 0.0l0) #!+x86 192 #!+sparc 256
178      sb!vm:simple-array-complex-long-float-type)
179     (t 0 32 sb!vm:simple-vector-type)))
180
181 ;;; The integer type restriction on the length ensures that it will be
182 ;;; a vector. The lack of adjustable, fill-pointer, and displaced-to
183 ;;; keywords ensures that it will be simple.
184 (deftransform make-array ((length &key initial-element element-type)
185                           (integer &rest *))
186   (let* ((eltype (cond ((not element-type) t)
187                        ((not (constant-continuation-p element-type))
188                         (give-up-ir1-transform
189                          "ELEMENT-TYPE is not constant."))
190                        (t
191                         (continuation-value element-type))))
192          (len (if (constant-continuation-p length)
193                   (continuation-value length)
194                   '*))
195          (spec `(simple-array ,eltype (,len)))
196          (eltype-type (specifier-type eltype)))
197     (multiple-value-bind (default-initial-element element-size typecode)
198         (dovector (info *array-info*
199                         (give-up-ir1-transform
200                          "cannot open-code creation of ~S" spec))
201           (when (csubtypep eltype-type (specifier-type (car info)))
202             (return (values-list (cdr info)))))
203       (let* ((nwords-form
204               (if (>= element-size sb!vm:word-bits)
205                   `(* length ,(/ element-size sb!vm:word-bits))
206                   (let ((elements-per-word (/ 32 element-size)))
207                     `(truncate (+ length
208                                   ,(if (eq 'sb!vm:simple-string-type typecode)
209                                      ;; (Simple strings are stored with an
210                                      ;; extra trailing null for convenience
211                                      ;; in calling out to C.)
212                                      elements-per-word
213                                      (1- elements-per-word)))
214                                ,elements-per-word))))
215              (constructor
216               `(truly-the ,spec
217                           (allocate-vector ,typecode length ,nwords-form))))
218         (values
219          (cond ((and default-initial-element
220                      (or (null initial-element)
221                          (and (constant-continuation-p initial-element)
222                               (eql (continuation-value initial-element)
223                                    default-initial-element))))
224                 (unless (csubtypep (ctype-of default-initial-element)
225                                    eltype-type)
226                   ;; This situation arises e.g. in
227                   ;;   (MAKE-ARRAY 4 :ELEMENT-TYPE '(INTEGER 1 5))
228                   ;; ANSI's definition of MAKE-ARRAY says "If
229                   ;; INITIAL-ELEMENT is not supplied, the consequences
230                   ;; of later reading an uninitialized element of
231                   ;; new-array are undefined," so this could be legal
232                   ;; code as long as the user plans to write before he
233                   ;; reads, and if he doesn't we're free to do
234                   ;; anything we like. But in case the user doesn't
235                   ;; know to write before he reads, we'll signal a
236                   ;; STYLE-WARNING in case he didn't realize this.
237                   ;;
238                   ;; FIXME: should be STYLE-WARNING, not note
239                   (compiler-note "The default initial element ~S is not a ~S."
240                                  default-initial-element
241                                  eltype))
242                 constructor)
243                (t
244                 `(truly-the ,spec (fill ,constructor initial-element))))
245          '((declare (type index length))))))))
246
247 ;;; The list type restriction does not ensure that the result will be a
248 ;;; multi-dimensional array. But the lack of adjustable, fill-pointer,
249 ;;; and displaced-to keywords ensures that it will be simple.
250 (deftransform make-array ((dims &key initial-element element-type)
251                           (list &rest *))
252   (unless (or (null element-type) (constant-continuation-p element-type))
253     (give-up-ir1-transform
254      "The element-type is not constant; cannot open code array creation."))
255   (unless (constant-continuation-p dims)
256     (give-up-ir1-transform
257      "The dimension list is not constant; cannot open code array creation."))
258   (let ((dims (continuation-value dims)))
259     (unless (every #'integerp dims)
260       (give-up-ir1-transform
261        "The dimension list contains something other than an integer: ~S"
262        dims))
263     (if (= (length dims) 1)
264         `(make-array ',(car dims)
265                      ,@(when initial-element
266                          '(:initial-element initial-element))
267                      ,@(when element-type
268                          '(:element-type element-type)))
269         (let* ((total-size (reduce #'* dims))
270                (rank (length dims))
271                (spec `(simple-array
272                        ,(cond ((null element-type) t)
273                               ((constant-continuation-p element-type)
274                                (continuation-value element-type))
275                               (t '*))
276                            ,(make-list rank :initial-element '*))))
277           `(let ((header (make-array-header sb!vm:simple-array-type ,rank)))
278              (setf (%array-fill-pointer header) ,total-size)
279              (setf (%array-fill-pointer-p header) nil)
280              (setf (%array-available-elements header) ,total-size)
281              (setf (%array-data-vector header)
282                    (make-array ,total-size
283                                ,@(when element-type
284                                    '(:element-type element-type))
285                                ,@(when initial-element
286                                    '(:initial-element initial-element))))
287              (setf (%array-displaced-p header) nil)
288              ,@(let ((axis -1))
289                  (mapcar #'(lambda (dim)
290                              `(setf (%array-dimension header ,(incf axis))
291                                     ,dim))
292                          dims))
293              (truly-the ,spec header))))))
294 \f
295 ;;;; miscellaneous properties of arrays
296
297 ;;; Transforms for various array properties. If the property is know
298 ;;; at compile time because of a type spec, use that constant value.
299
300 ;;; If we can tell the rank from the type info, use it instead.
301 (deftransform array-rank ((array))
302   (let ((array-type (continuation-type array)))
303     (unless (array-type-p array-type)
304       (give-up-ir1-transform))
305     (let ((dims (array-type-dimensions array-type)))
306       (if (not (listp dims))
307           (give-up-ir1-transform
308            "The array rank is not known at compile time: ~S"
309            dims)
310           (length dims)))))
311
312 ;;; If we know the dimensions at compile time, just use it. Otherwise,
313 ;;; if we can tell that the axis is in bounds, convert to
314 ;;; %ARRAY-DIMENSION (which just indirects the array header) or length
315 ;;; (if it's simple and a vector).
316 (deftransform array-dimension ((array axis)
317                                (array index))
318   (unless (constant-continuation-p axis)
319     (give-up-ir1-transform "The axis is not constant."))
320   (let ((array-type (continuation-type array))
321         (axis (continuation-value axis)))
322     (unless (array-type-p array-type)
323       (give-up-ir1-transform))
324     (let ((dims (array-type-dimensions array-type)))
325       (unless (listp dims)
326         (give-up-ir1-transform
327          "The array dimensions are unknown; must call ARRAY-DIMENSION at runtime."))
328       (unless (> (length dims) axis)
329         (abort-ir1-transform "The array has dimensions ~S, ~D is too large."
330                              dims
331                              axis))
332       (let ((dim (nth axis dims)))
333         (cond ((integerp dim)
334                dim)
335               ((= (length dims) 1)
336                (ecase (array-type-complexp array-type)
337                  ((t)
338                   '(%array-dimension array 0))
339                  ((nil)
340                   '(length array))
341                  ((:maybe)
342                   (give-up-ir1-transform
343                    "can't tell whether array is simple"))))
344               (t
345                '(%array-dimension array axis)))))))
346
347 ;;; If the length has been declared and it's simple, just return it.
348 (deftransform length ((vector)
349                       ((simple-array * (*))))
350   (let ((type (continuation-type vector)))
351     (unless (array-type-p type)
352       (give-up-ir1-transform))
353     (let ((dims (array-type-dimensions type)))
354       (unless (and (listp dims) (integerp (car dims)))
355         (give-up-ir1-transform
356          "Vector length is unknown, must call LENGTH at runtime."))
357       (car dims))))
358
359 ;;; All vectors can get their length by using VECTOR-LENGTH. If it's
360 ;;; simple, it will extract the length slot from the vector. It it's
361 ;;; complex, it will extract the fill pointer slot from the array
362 ;;; header.
363 (deftransform length ((vector) (vector))
364   '(vector-length vector))
365
366 ;;; If a simple array with known dimensions, then VECTOR-LENGTH is a
367 ;;; compile-time constant.
368 (deftransform vector-length ((vector) ((simple-array * (*))))
369   (let ((vtype (continuation-type vector)))
370     (if (array-type-p vtype)
371         (let ((dim (first (array-type-dimensions vtype))))
372           (when (eq dim '*) (give-up-ir1-transform))
373           dim)
374         (give-up-ir1-transform))))
375
376 ;;; Again, if we can tell the results from the type, just use it.
377 ;;; Otherwise, if we know the rank, convert into a computation based
378 ;;; on array-dimension. We can wrap a TRULY-THE INDEX around the
379 ;;; multiplications because we know that the total size must be an
380 ;;; INDEX.
381 (deftransform array-total-size ((array)
382                                 (array))
383   (let ((array-type (continuation-type array)))
384     (unless (array-type-p array-type)
385       (give-up-ir1-transform))
386     (let ((dims (array-type-dimensions array-type)))
387       (unless (listp dims)
388         (give-up-ir1-transform "can't tell the rank at compile time"))
389       (if (member '* dims)
390           (do ((form 1 `(truly-the index
391                                    (* (array-dimension array ,i) ,form)))
392                (i 0 (1+ i)))
393               ((= i (length dims)) form))
394           (reduce #'* dims)))))
395
396 ;;; Only complex vectors have fill pointers.
397 (deftransform array-has-fill-pointer-p ((array))
398   (let ((array-type (continuation-type array)))
399     (unless (array-type-p array-type)
400       (give-up-ir1-transform))
401     (let ((dims (array-type-dimensions array-type)))
402       (if (and (listp dims) (not (= (length dims) 1)))
403           nil
404           (ecase (array-type-complexp array-type)
405             ((t)
406              t)
407             ((nil)
408              nil)
409             ((:maybe)
410              (give-up-ir1-transform
411               "The array type is ambiguous; must call ~
412               ARRAY-HAS-FILL-POINTER-P at runtime.")))))))
413
414 ;;; Primitive used to verify indices into arrays. If we can tell at
415 ;;; compile-time or we are generating unsafe code, don't bother with
416 ;;; the VOP.
417 (deftransform %check-bound ((array dimension index))
418   (unless (constant-continuation-p dimension)
419     (give-up-ir1-transform))
420   (let ((dim (continuation-value dimension)))
421     `(the (integer 0 ,dim) index)))
422 (deftransform %check-bound ((array dimension index) * *
423                             :policy (and (> speed safety) (= safety 0)))
424   'index)
425 \f
426 ;;;; array accessors
427
428 ;;; FIXME: This was commented out in sbcl-0.6.9.21 since it was
429 ;;; causing a problem in a CHAR form in HEXSTR. It's still important
430 ;;; to be able to inline this, so something along these lines
431 ;;; will probably be back, but it might be different in detail, e.g.
432 ;;; (DECLAIM (MAYBE-INLINE %WITH-ARRAY-DATA)).
433 #|
434 ;;; Handle the 1-dimensional case of %WITH-ARRAY-DATA specially. It's
435 ;;; important to do this efficiently if we want people to be able to
436 ;;; use vectors with fill pointers anywhere near inner loops, and
437 ;;; hence it's important to do this efficiently if we want people to
438 ;;; be able to use sequence functions anywhere near inner loops.
439 (deftransform %with-array-data ((array start end)
440                                 (vector index (or index null))
441                                 *
442                                 :important t
443                                 :node node
444                                 :policy (> speed space))
445   "avoid full call to %WITH-ARRAY-DATA at runtime"
446   (let* ((element-ctype (extract-upgraded-element-type array))
447          (element-type-specifier (type-specifier element-ctype))
448          (simple-array-type `(simple-array ,element-type-specifier 1)))
449     (declare (type ctype element-ctype))
450     `(let* (;; FIXME: Instead of doing this hairy expression for SIZE,
451             ;; it should just be (ARRAY-DIMENSION ARRAY 0), and there
452             ;; should be a DEFTRANSFORM for ARRAY-DIMENSION which
453             ;; expands that way.
454             (size (if (array-header-p array)
455                       (%array-dimension array 0)
456                       (length (the ,simple-array-type array))))
457             (end (if end
458                      (if (or ,(policy node (= safety 0))
459                              (<= (the index end) size))
460                          end
461                          (vector-data-start-out-of-range))
462                      size)))
463        (declare (type index end))
464        (unless (or ,(policy node (= safety 0))
465                    (<= start end))
466          (vector-data-end-out-of-range))
467        (do (;; cumulative displacement
468             (d 0 (truly-the index (+ d (%array-displacement array))))
469             ;; eventually becomes bare data vector
470             (v array (%array-data-vector v))) 
471            ((not (array-header-p v))
472             (values (the ,simple-array-type v)
473                     (truly-the index (+ d start))
474                     (truly-the index (+ d end))
475                     (the index d)))
476          (declare (type index d))))))
477 (defun vector-data-start-out-of-range ()
478   (error "The start of vector data was out of range."))
479 (defun vector-data-end-out-of-range ()
480   (error "The end of vector data was out of range."))
481 |#
482
483 ;;; We convert all typed array accessors into AREF and %ASET with type
484 ;;; assertions on the array.
485 (macrolet ((define-frob (reffer setter type)
486              `(progn
487                 (def-source-transform ,reffer (a &rest i)
488                   (if (byte-compiling)
489                       (values nil t)
490                       `(aref (the ,',type ,a) ,@i)))
491                 (def-source-transform ,setter (a &rest i)
492                   (if (byte-compiling)
493                       (values nil t)
494                       `(%aset (the ,',type ,a) ,@i))))))
495   (define-frob svref %svset simple-vector)
496   (define-frob schar %scharset simple-string)
497   (define-frob char %charset string)
498   (define-frob sbit %sbitset (simple-array bit))
499   (define-frob bit %bitset (array bit)))
500
501 (macrolet (;; This is a handy macro for computing the row-major index
502            ;; given a set of indices. We wrap each index with a call
503            ;; to %CHECK-BOUND to ensure that everything works out
504            ;; correctly. We can wrap all the interior arithmetic with
505            ;; TRULY-THE INDEX because we know the the resultant
506            ;; row-major index must be an index.
507            (with-row-major-index ((array indices index &optional new-value)
508                                   &rest body)
509              `(let (n-indices dims)
510                 (dotimes (i (length ,indices))
511                   (push (make-symbol (format nil "INDEX-~D" i)) n-indices)
512                   (push (make-symbol (format nil "DIM-~D" i)) dims))
513                 (setf n-indices (nreverse n-indices))
514                 (setf dims (nreverse dims))
515                 `(lambda (,',array ,@n-indices
516                                    ,@',(when new-value (list new-value)))
517                    (let* (,@(let ((,index -1))
518                               (mapcar #'(lambda (name)
519                                           `(,name (array-dimension
520                                                    ,',array
521                                                    ,(incf ,index))))
522                                       dims))
523                             (,',index
524                              ,(if (null dims)
525                                   0
526                                 (do* ((dims dims (cdr dims))
527                                       (indices n-indices (cdr indices))
528                                       (last-dim nil (car dims))
529                                       (form `(%check-bound ,',array
530                                                            ,(car dims)
531                                                            ,(car indices))
532                                             `(truly-the
533                                               index
534                                               (+ (truly-the index
535                                                             (* ,form
536                                                                ,last-dim))
537                                                  (%check-bound
538                                                   ,',array
539                                                   ,(car dims)
540                                                   ,(car indices))))))
541                                     ((null (cdr dims)) form)))))
542                      ,',@body)))))
543
544   ;; Just return the index after computing it.
545   (deftransform array-row-major-index ((array &rest indices))
546     (with-row-major-index (array indices index)
547       index))
548
549   ;; Convert AREF and %ASET into a HAIRY-DATA-VECTOR-REF (or
550   ;; HAIRY-DATA-VECTOR-SET) with the set of indices replaced with the an
551   ;; expression for the row major index.
552   (deftransform aref ((array &rest indices))
553     (with-row-major-index (array indices index)
554       (hairy-data-vector-ref array index)))
555   (deftransform %aset ((array &rest stuff))
556     (let ((indices (butlast stuff)))
557       (with-row-major-index (array indices index new-value)
558         (hairy-data-vector-set array index new-value)))))
559
560 ;;; Just convert into a HAIRY-DATA-VECTOR-REF (or
561 ;;; HAIRY-DATA-VECTOR-SET) after checking that the index is inside the
562 ;;; array total size.
563 (deftransform row-major-aref ((array index))
564   `(hairy-data-vector-ref array
565                           (%check-bound array (array-total-size array) index)))
566 (deftransform %set-row-major-aref ((array index new-value))
567   `(hairy-data-vector-set array
568                           (%check-bound array (array-total-size array) index)
569                           new-value))
570 \f
571 ;;;; bit-vector array operation canonicalization
572 ;;;;
573 ;;;; We convert all bit-vector operations to have the result array
574 ;;;; specified. This allows any result allocation to be open-coded,
575 ;;;; and eliminates the need for any VM-dependent transforms to handle
576 ;;;; these cases.
577
578 (dolist (fun '(bit-and bit-ior bit-xor bit-eqv bit-nand bit-nor bit-andc1
579                        bit-andc2 bit-orc1 bit-orc2))
580   ;; Make a result array if result is NIL or unsupplied.
581   (deftransform fun ((bit-array-1 bit-array-2 &optional result-bit-array)
582                      '(bit-vector bit-vector &optional null) '*
583                      :eval-name t
584                      :policy (>= speed space))
585     `(,fun bit-array-1 bit-array-2
586            (make-array (length bit-array-1) :element-type 'bit)))
587   ;; If result is T, make it the first arg.
588   (deftransform fun ((bit-array-1 bit-array-2 result-bit-array)
589                      '(bit-vector bit-vector (member t)) '*
590                      :eval-name t)
591     `(,fun bit-array-1 bit-array-2 bit-array-1)))
592
593 ;;; Similar for BIT-NOT, but there is only one arg...
594 (deftransform bit-not ((bit-array-1 &optional result-bit-array)
595                        (bit-vector &optional null) *
596                        :policy (>= speed space))
597   '(bit-not bit-array-1
598             (make-array (length bit-array-1) :element-type 'bit)))
599 (deftransform bit-not ((bit-array-1 result-bit-array)
600                        (bit-vector (constant-argument t)))
601   '(bit-not bit-array-1 bit-array-1))
602 ;;; FIXME: What does (CONSTANT-ARGUMENT T) mean? Is it the same thing
603 ;;; as (CONSTANT-ARGUMENT (MEMBER T)), or does it mean any constant
604 ;;; value?
605 \f
606 ;;; Pick off some constant cases.
607 (deftransform array-header-p ((array) (array))
608   (let ((type (continuation-type array)))
609     (declare (optimize (safety 3)))
610     (unless (array-type-p type)
611       (give-up-ir1-transform))
612     (let ((dims (array-type-dimensions type)))
613       (cond ((csubtypep type (specifier-type '(simple-array * (*))))
614              ;; No array header.
615              nil)
616             ((and (listp dims) (> (length dims) 1))
617              ;; Multi-dimensional array, will have a header.
618              t)
619             (t
620              (give-up-ir1-transform))))))